From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796181746; b=kJucr56KaNaytry5IPClp3dXQ5exs05tAE28beTkwOfirLb2g7MYOO3788GLtg3BHB60 lYmyGr4/ja9mJ2ykrgf/YVtOSs7Ls3Aq++5+yHJ5dSJTUJ9M9yaJ07hgfTkjF2FuwIril kvT1NmBQGmsKjV7e1YVkfSV56RzNLN43XpacN+2tPumdn+Wwq2ME7pucyt59q5JZ2GO8b C2Uj3wGu5XRWPiwlQLSPPFWVkW1cp/mNh+d1l5qTrWzVvh80NFCHFVYT8BkBZzf10uLNS sGbYyUI1f8yPxvv8Mab9lb7/+ON/WPbYvhm2p8qo5zd3AOwkAbXztrHUK+TGjHilakA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796181746; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=vTvUaqaBTj7ZEWTY9EL1/gSUrH4CcfqXLqsl+lCcXp4=; b=ot2gupgCy04Y//7mPcvKwpP9aDiB6CjLMc8GWM7Pl7wzjaE8bRHfSzxSWNoQmR7wehdr H96wva1evekeSBP6jf79AIxm1d1/NVL12uvVkb8KAnS79LOTF7WT4qNca9qJPh+nW/p5r pKSkwHorV1lSHnij1QljZ4ZhenUsUGzu/h1eFUSUfVTFdDeVbniorc+ZhlS9phLUKbsTK ZH9FAACORpz8n/UkWZ/3SOM5tiOtDj4o/DBpVkVL8IQKPgzjJmRl/f+peASZx9ndzt8tk Sms1PYCu6CQvvCsXXnRu74vh++ed5c40X0NaCJ9DOdS30kfbW/eMJqTz3B9zOjU5sEQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=njEahrdu; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 43D1360A2615 for ; Wed, 2 Dec 2026 03:22:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=njEahrdu Received: by smtp.kernel.org (Postfix) with ESMTPSA id 58EAF1F00897; Wed, 2 Dec 2026 03:22:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796181745; bh=vTvUaqaBTj7ZEWTY9EL1/gSUrH4CcfqXLqsl+lCcXp4=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=njEahrdu/5w7NOkA5fyFrokiFfGRycg6aXZsEku68UB5K+m+Wqh4oRTNEEFiDwmBs ORE2gk9YAZcYBrd4eBjcW4og0oIbPen4spAaDLOTD6Z+aqkIBkWcKl0knkH88NLmYM k/IEccvR4zwOhVT/8inF4i+ESpORolcWKTbbZvPnZWQqkWK/6MgQly+wbgfzenzmq2 l2u6DoCHtyjtTtuzCa4RTlITWK9CX5QGdxbK5oOjnzBNte2PnRwuMHI+QH0+/17/wR jSrBJBSghCcRnPcKGpRMI9zZ0DyuyeVyTuSLOnA/irjUIArmo36b7LKqlffqiJJl0o 8CJyrny1jCFhQ== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH] ALSA: pcm: do not free a pcm with opened substreams on single-device teardown Reply-To: sashiko-reviews@lists.linux.dev To: "Hanxiang Xu" Cc: linux-sound@vger.kernel.org In-Reply-To: <20261202100905.1-1-xuhx@hust.edu.cn> References: <20261202100905.1-1-xuhx@hust.edu.cn> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Wed, 02 Dec 2026 03:22:24 +0000 X-Mailing-List: linux-sound@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] ALSA: pcm: permanent leak of struct snd_pcm on -EBUSY in snd_pcm_free() - [High] ALSA: pcm: driver state UAF on close after snd_pcm_free() returns -EBUSY - [Medium] ALSA: pcm: race and UAF in open/release when substream_opened is zero -- commit 52308033688b8a82f3d8bd6af3118abe043c7f42 Author: Hanxiang Xu ALSA: pcm: do not free a pcm with opened substreams on single-device teardo= wn This commit returns -EBUSY from snd_pcm_free() when either stream still has opened substreams so single-device teardown does not free an active struct snd_pcm. > diff --git a/sound/core/pcm.c b/sound/core/pcm.c > index da285358eb655..4e012707d8806 100644 > --- a/sound/core/pcm.c > +++ b/sound/core/pcm.c > @@ -861,6 +861,9 @@ static int snd_pcm_free(struct snd_pcm *pcm) > { > if (!pcm) > return 0; > + if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_opened || > + pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_opened) > + return -EBUSY; > if (!pcm->internal) { > scoped_guard(mutex, ®ister_mutex) > pcm_call_notify(pcm, n_unregister); [Severity: High] Does returning -EBUSY here permanently leak struct snd_pcm and all of its associated resources? __snd_device_free() unconditionally unlinks the struct snd_device from card->devices and frees it even when dev->ops->dev_free(dev) fails: sound/core/device.c:__snd_device_free() { /* unlink */ list_del(&dev->list); __snd_device_disconnect(dev); if (dev->ops->dev_free) { if (dev->ops->dev_free(dev)) dev_err(dev->card->dev, "device free failure\n"); } kfree(dev); } Neither snd_pcm_release() nor snd_card_do_free() has any remaining path to call snd_pcm_free(pcm) after the open descriptors close, so the PCM streams, preallocated DMA buffers, per-substream timer devices, driver private_free cleanup, and the snd-pcm-oss module reference released by pcm_call_notify(pcm, n_unregister) -> snd_pcm_oss_unregister_minor() are never freed. [Severity: High] Can leaving the open substream alive while snd_device_free() returns immediately cause a use-after-free and NULL pointer dereference in the driver when the descriptor is later closed? In the motivating snd_emu8000_remove() path, the driver frees and zeroes its state right after snd_device_free() returns: sound/isa/sb/emu8000_synth.c:snd_emu8000_remove() { ... if (hw->pcm) snd_device_free(dev->card, hw->pcm); snd_emux_free(hw->emu); snd_util_memhdr_free(hw->memhdr); hw->emu =3D NULL; hw->memhdr =3D NULL; ... } When the surviving file descriptor is subsequently closed, snd_pcm_release_substream() calls do_hw_free() -> emu8k_pcm_hw_free() and substream->ops->close(), which dereference the already-freed and NULLed rec->emu->emu, rec->emu->memhdr, and rec->block. [Severity: Medium] Can an in-flight open or release still suffer a use-after-free on struct snd_pcm while substream_opened is 0? substream_opened is updated under pcm->open_mutex inside snd_pcm_attach_substream() and snd_pcm_detach_substream(), and snd_pcm_release() continues accessing pcm after substream_opened drops to 0: CPU0 CPU1 snd_pcm_release() lock(&pcm->open_mutex) snd_pcm_release_substream() snd_pcm_detach_substream() pstr->substream_opened-- snd_device_free(card, pcm) ... snd_pcm_free(pcm) substream_opened =3D=3D 0 kfree(pcm) unlock(&pcm->open_mutex) wake_up(&pcm->open_wait) module_put(pcm->card->module) <-- use-after-free The same window exists in snd_pcm_open() and snd_pcm_oss_open() after snd_lookup_minor_data_file() returns pcm and before snd_pcm_attach_substream() increments substream_opened. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261202100905.1-1-= xuhx@hust.edu.cn?part=3D1