From nobody Sun Apr 28 19:48:59 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4VSH8W2PFSz5Jr7M; Sun, 28 Apr 2024 19:48:59 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4VSH8W1kzrz429h; Sun, 28 Apr 2024 19:48:59 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1714333739; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=oDvFfLEzy2gmbzqBh9k1UV/K+vsI2gg0ZKepjulkkCw=; b=DaRZtC0VvmnGtrxOHrS3LP39m0HYIPF1pyF1PXsun0hMMVIV0hEL70OX3b1xB55Xy7SU5T 8YuwsirtEHqe7gLOA8flnE70oOcFcxfQ9NMsZyfUXhrVWy6zZtruXXDhZz9VzcQGyWO5xV Dplc5lcAhW9vLnXG31yGCSXy7vX0zurW5DrQUrDuKvO+m6GEXou2BhWvRMnkJtMxruG3pW MupKR6RMYurzCz4BxE8436WrXXk+JBmdI3bCx5gFIqWPm2IJYIlKIIM+VgptHNKa3DQsl6 F2NQqYaLTnJ+G0/iKJ8MEBxbWxIeEGz6q7WwujYrJzaxa+pieK8JZrBWqjxUgQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1714333739; a=rsa-sha256; cv=none; b=kQLsKQlsifEzbeCq0lHtdiKMapKWuhOJYfS7+zqHWiJpRh93z/UvCmP7C/zx9cj/XTsB5w aArxtKUaqnqRQxq79Kzbs97x0ZqQExuCq9xKtU4B5rRXlGSvMsXFfU5os59IReImxv8IBe d7Urrb2A2oJgOfZyVttY5vPY+UuWR5Bl4vJMt4cJH6xFCc12JQs8rYB6KroEXCJVkU0otU 12HNIKnUZwSLw5+GZFoCP/PXP2k2OA+ic0C/YbtDUgKRBkh+/zcgP/CRgXHvvu8qun7U/e NePdcHl+ZMPrpPq+1pCoqfbuZSDKhQlYwH/dICrMyE9h5wNCwcsdUbe39LXlnw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1714333739; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=oDvFfLEzy2gmbzqBh9k1UV/K+vsI2gg0ZKepjulkkCw=; b=IvSSWMj8DqkRA0TUUVnPEnOCU1foqd3fSYlfHYk3Cu2RsPw+eTYsg37KJLEp7JUlhsX99V tfFtC7CFXVhb+x1wahiofKVxKGsjHN9uaQB29VbaHJRuYeFx8cU2xxC+eR+pxHCmGDbu4N 37dsvacwRkEQL2ikwJK3v+jYhRNm0co6BitffHeoDKmSz2076he8LLU93XOBDRiQ0OD6L9 SagAVYbmpzPOghjvWBHhRvaTv4Q6L2tXOEt2QQcei6E6aHQRR3yDWuhTyhPB2VMKLm2s1p X/428IuHU78uRO2JtC7T8JE9ov0J/pCD01JGhHzmauSBjoS0FymKc/ihevua7g== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4VSH8W1KmszfKw; Sun, 28 Apr 2024 19:48:59 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 43SJmxSN063123; Sun, 28 Apr 2024 19:48:59 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 43SJmxt6063120; Sun, 28 Apr 2024 19:48:59 GMT (envelope-from git) Date: Sun, 28 Apr 2024 19:48:59 GMT Message-Id: <202404281948.43SJmxt6063120@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Christos Margiolis Subject: git: 074d6fbebc16 - main - sound: Fix NULL dereference in dsp_clone() and mixer_clone() List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: christos X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 074d6fbebc160222cde6b726adcc7350881d7824 Auto-Submitted: auto-generated The branch main has been updated by christos: URL: https://cgit.FreeBSD.org/src/commit/?id=074d6fbebc160222cde6b726adcc7350881d7824 commit 074d6fbebc160222cde6b726adcc7350881d7824 Author: Christos Margiolis AuthorDate: 2024-04-28 19:40:14 +0000 Commit: Christos Margiolis CommitDate: 2024-04-28 19:48:24 +0000 sound: Fix NULL dereference in dsp_clone() and mixer_clone() If we only have a single soundcard attached and we detach it right before entering [dsp|mixer]_clone(), there is a chance pcm_unregister() will have returned already, meaning it will have set snd_unit to -1, and thus devclass_get_softc() will return NULL here. While here, 1) move the calls to dsp_destroy_dev() and mixer_uninit() below the point where we unset SD_F_REGISTERED, and 2) follow what mixer_clone() does and make sure we don't use a NULL d->dsp_dev in dsp_clone(). Reported by: KASAN Sponsored by: The FreeBSD Foundation MFC after: 1 day Reviewed by: markj Differential Revision: https://reviews.freebsd.org/D44924 --- sys/dev/sound/pcm/dsp.c | 14 ++++++++++---- sys/dev/sound/pcm/mixer.c | 3 ++- sys/dev/sound/pcm/sound.c | 7 +++---- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/sys/dev/sound/pcm/dsp.c b/sys/dev/sound/pcm/dsp.c index 1c020ba22611..c5bb27530d2b 100644 --- a/sys/dev/sound/pcm/dsp.c +++ b/sys/dev/sound/pcm/dsp.c @@ -2086,10 +2086,16 @@ dsp_clone(void *arg, struct ucred *cred, char *name, int namelen, return; found: d = devclass_get_softc(pcm_devclass, snd_unit); - if (!PCM_REGISTERED(d)) - return; - *dev = d->dsp_dev; - dev_ref(*dev); + /* + * If we only have a single soundcard attached and we detach it right + * before entering dsp_clone(), there is a chance pcm_unregister() will + * have returned already, meaning it will have set snd_unit to -1, and + * thus devclass_get_softc() will return NULL here. + */ + if (d != NULL && PCM_REGISTERED(d) && d->dsp_dev != NULL) { + *dev = d->dsp_dev; + dev_ref(*dev); + } } static void diff --git a/sys/dev/sound/pcm/mixer.c b/sys/dev/sound/pcm/mixer.c index 0178e1ae8c3e..33a7eb26606d 100644 --- a/sys/dev/sound/pcm/mixer.c +++ b/sys/dev/sound/pcm/mixer.c @@ -1378,7 +1378,8 @@ mixer_clone(void *arg, return; if (strcmp(name, "mixer") == 0) { d = devclass_get_softc(pcm_devclass, snd_unit); - if (PCM_REGISTERED(d) && d->mixer_dev != NULL) { + /* See related comment in dsp_clone(). */ + if (d != NULL && PCM_REGISTERED(d) && d->mixer_dev != NULL) { *dev = d->mixer_dev; dev_ref(*dev); } diff --git a/sys/dev/sound/pcm/sound.c b/sys/dev/sound/pcm/sound.c index e88ccb1f25a6..faf4bd1c7dce 100644 --- a/sys/dev/sound/pcm/sound.c +++ b/sys/dev/sound/pcm/sound.c @@ -1013,10 +1013,6 @@ pcm_unregister(device_t dev) CHN_UNLOCK(ch); } - dsp_destroy_dev(dev); - - (void)mixer_uninit(dev); - /* remove /dev/sndstat entry first */ sndstat_unregister(dev); @@ -1034,6 +1030,9 @@ pcm_unregister(device_t dev) d->rec_sysctl_tree = NULL; } + dsp_destroy_dev(dev); + (void)mixer_uninit(dev); + while (!CHN_EMPTY(d, channels.pcm)) pcm_killchan(dev);