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=1796181791; b=3LaWGNfV9PJl08qCwis/mrheHGkiAcD+GMK0/67/N+THIbAL2uH/xYNlOKQRdVOO1bs5 LiRItkbmRVWVZKBpo0CSzxRCby0Q3vmnGqX/1P98F1ic1W9wJ79BwGM3dAryl3avRS49p gdUwodrajd0ngl5RtlWnZyoZ4wTxfumCnhpiV9kMQE41NaYqMHu8ZLc7p5I+s9g6mR2wk aBfcvqSvSEiM9tm7EBt19f5KIdQQq2CXYFGL3DGNmd/roveTO+M8Qr5KWKSoL0zfA7pkL yFrwPqVdfBw7ekBZ7yT3kvIomWuHWZWwsTa7npWO5zDMUkScum6YcYyR2pwPNXHr9hw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796181791; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=u+MSGE4AYtqauXbwa/bvVOsorIJEmoFpzFGADaVGrDg=; b=3WuwgFgH/nX+RBQljnSvZM6cTG8jUMlgLYvp7oYYzHbMbhjErZs0A8hlaWrktT7mSgHu yQ+ETR693eTWs8GpZR85+K79HcmiQ1RFVGe8P1lbIQ8GGPur/eVLXJfFnP+Gugo9bYf92 ksiYp2HDzIKpRP/2e0uO1QtSRE8hR75n1Bf6quv6XacWstOl1Usbic8AEfplW0iqlKmk5 7tF3nGbEbFtWe2AWo1v1UHL0PV6p1vzUISa3BbMjAw7ChPW7/SKEFfMYliVMBeKPIpvye mlXUOxpH0/ahAzJ8m7BQsJ6ynhXEDxi/RlvuQxymvuOGMpU1X1ez0A0lvqDKaurzmqA== 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=GOraGLpF; 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 80E54606A68C for ; Wed, 2 Dec 2026 03:23:11 +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=GOraGLpF Received: by smtp.kernel.org (Postfix) with ESMTPSA id 044521F000C1; Wed, 2 Dec 2026 03:23:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796181791; bh=u+MSGE4AYtqauXbwa/bvVOsorIJEmoFpzFGADaVGrDg=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=GOraGLpFacRFzEoEB4Pt90267ISOrd56znpO/mMbPmuY+QNAXjx1yrcO8qRwkISCh 2FusFRoPjYyuKFwNfL9o6q6GgAYV8Xw52J5CxYYHzdqm3njwpkNx0p87sCqiti5qsj Htfr5uefgNdmEExiLrzQmWlO9EbBdjiNN6lXXqebBbQ4PLHZZsjuTWTPY933V3mi81 WZ2avDeY/Cfylem85oo6V4pDUzELxEuMR/JGkURM1Nuim9+cYsVRju+3N4KWnGV8gP 7GifULozGjQ2MHkUKIX+eJI6A6ZF4ltnz6Xnt9m1VV3l6MCOWdVhnJpUk5AyzdcB3F iQXoBbhuD8iYQ== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH] ALSA: rawmidi: do not free a rawmidi with open substreams on single-device teardown Reply-To: sashiko-reviews@lists.linux.dev To: "Hanxiang Xu" Cc: linux-sound@vger.kernel.org In-Reply-To: <20261202100804.1-1-xuhx@hust.edu.cn> References: <20261202100804.1-1-xuhx@hust.edu.cn> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Wed, 02 Dec 2026 03:23:10 +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 2 potential issues to consider: - [High] ALSA: rawmidi: permanent resource leak on -EBUSY in snd_rawmidi_free() - [High] ALSA: rawmidi: UAF race in snd_rawmidi_free() against open and release -- commit a8c690d1a9c9b070160648c8b1f5677136e99bce Author: Hanxiang Xu ALSA: rawmidi: do not free a rawmidi with open substreams on single-device = teardown This commit skips freeing open substreams in snd_rawmidi_free_substreams() and returns -EBUSY from snd_rawmidi_free() when any substream is open. > diff --git a/sound/core/rawmidi.c b/sound/core/rawmidi.c > index 630d9fc8e9da0..851509e9d43ff 100644 > --- a/sound/core/rawmidi.c > +++ b/sound/core/rawmidi.c > @@ -1969,6 +1969,8 @@ static void snd_rawmidi_free_substreams(struct snd_= rawmidi_str *stream) > while (!list_empty(&stream->substreams)) { > substream =3D list_entry(stream->substreams.next, struct snd_rawmidi_s= ubstream, list); > list_del(&substream->list); > + if (substream->opened) > + continue; > kfree(substream); > } > } > @@ -1978,6 +1980,9 @@ int snd_rawmidi_free(struct snd_rawmidi *rmidi) > { > if (!rmidi) > return 0; > + if (rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened || > + rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened) > + return -EBUSY; > =20 > snd_info_free_entry(rmidi->proc_entry); > rmidi->proc_entry =3D NULL; [Severity: High] Does returning -EBUSY here permanently leak rmidi, its substreams, and its subdevice resources? __snd_device_free() unlinks the snd_device from card->devices and frees it regardless of the error returned by dev_free(): 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); } When the open file descriptors on /dev/snd/midiC*D* are later closed, snd_rawmidi_release() and close_substream() only decrement substream_opened and free the runtime and snd_rawmidi_file; nothing ever calls snd_rawmidi_free() again. In addition to leaking rmidi, the substreams, rmidi->private_data, and the rmidi->dev reference, bailing out early leaves rmidi->proc_entry registered in procfs and skips rmidi->ops->dev_unregister(). Likewise, snd_rawmidi_free_substreams() calls list_del(&substream->list) before checking substream->opened, which orphans and leaks any open substream. [Severity: High] Can checking substream_opened and substream->opened without holding rmidi->open_mutex race with concurrent open and release calls and cause a use-after-free? open_substream() and close_substream() update substream->opened and substream_opened under rmidi->open_mutex, but snd_rawmidi_free() runs unlocked. If the last open file descriptor is closing concurrently, close_substream() decrements substream_opened to 0 before rawmidi_release_priv() and snd_rawmidi_release() finish using rmidi: CPU0 CPU1 snd_rawmidi_release() mutex_lock(&rmidi->open_mutex) rawmidi_release_priv() close_substream() ... stream->substream_opened-- snd_device_free() ... snd_rawmidi_free() /* sees substream_opened =3D=3D= 0 */ kfree(rmidi) wake_up(&rmidi->open_wait) <-- use-after-free mutex_unlock(&rmidi->open_mutex) <-- use-after-free module =3D rmidi->card->module <-- use-after-free A similar race exists against open_substream(): during single-device teardown rmidi->card->shutdown is still 0, so a concurrent open can enter open_substream() and run snd_rawmidi_runtime_create() and substream->ops->open() before incrementing substream_opened, while snd_rawmidi_free() sees substream_opened =3D=3D 0 and frees the substreams, rmidi->private_data, and rmidi underneath it. If open_substream() sets substream->opened =3D 1 just after snd_rawmidi_free() passes the substream_opened check, snd_rawmidi_free_substreams() skips freeing that substream while snd_rawmidi_free() still frees rmidi->private_data and rmidi, leaving the open substream and file with dangling rmidi pointers. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261202100804.1-1-= xuhx@hust.edu.cn?part=3D1