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=1796181809; b=e+iHQHmPSI9ByaDFYxR+ZiyhA+4ASf/6Izlcj8Ra1gQOIv1DKxycUb+TF3nQDaX17fCe OIaq3aJzkt5M4wceL+mL0miVAgc2pInbuq4KAZOCsa2s03I4NS6TC0ZSE5YNqoLvztqCH 26Z2pOop7S3UfLF8xhgJJuO3i5smpC2j4NgzVTR/HKRwjUUdu3PDGLc+39RtFBIg6Rnea a5ib8DmuAFVkQQ9lqB6lLbAMiCeE13zlTywY31JmW1Sh4L/VTy0jsmookqzQBvjZHTlhy VjrxOxKQPev0iP2gLMlsOp1t7dYc0LvT8eeYm8U5rima1zP8hLnxs6A4Nc5zaNCFbeg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796181809; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=mQFx+7RLbrW1jI8CUpGIIuWUt3JqTRz0ZOuqXUOB4ec=; b=0GNnVgUWD+b07ARc2jRqsj9jJsvjddloXjm6tDcJNU4rqxCZolY1mE4af5N+RypcLEow xm7h5pxdW2+2GQGe6H9O68XWzUcDTg/WkuuwMqoXo4QhO99fcE+YNN4/qjhJI3r6tOslT 1lPm3R+hnvNqJJ5MjgLFf/85bQeBQtwOmFBp9bwspPayKLpxtNoz1ymp09EFaA9tY6F/5 G8KP4Ehs6UMZGsHH1J6SWbCZdoblOo9KNSCL3RXVihZzzi8qZEv1rQ6/f/3MHy5rrHtrb 5A2Ic5Y+7BAvqv3O8yKsQgDYCShn/xvzOU6OLn0Ey6oBUxPZ7sIRUnJhnenIDGt8BEw== 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=XFvJqLlB; 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 8BB09606A68E for ; Wed, 2 Dec 2026 03:23:29 +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=XFvJqLlB Received: by smtp.kernel.org (Postfix) with ESMTPSA id 549FC1F00893; Wed, 2 Dec 2026 03:23:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796181809; bh=mQFx+7RLbrW1jI8CUpGIIuWUt3JqTRz0ZOuqXUOB4ec=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=XFvJqLlBCVVOiXcD2QL0CN315oc3pcE41qJUJ+kLJwXueK4qzLF/x7TH3MtmqIMs6 EcP5NKY1RFd0YPUMdJ9bjRJUqtveMAB9kPlPCrrSlv3ItTQ8+6CXyjCsY+3yqfpiIh uaeJAkZK9iYdgOu6f3lwkSY3H4JfPN2MGOp4OG/2MO24pYXpUt9v6nAB2wnGOYcKgK /5GOUynAPr8GJg6Sv6Yb0EdJYDkrqlELSc2+MeDUa4o60DBSgV1mwGUMs8nCyHFOV4 +dRwc/ROIv9L32km/TlRaik8AR8lYUxtFQZ8Clvl4nOhwyJKOYAG9soG/acyfvw5hH 4C6U2iUy3cNtA== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH] ALSA: rawmidi: keep struct snd_rawmidi alive until the last open file is closed Reply-To: sashiko-reviews@lists.linux.dev To: "Hanxiang Xu" Cc: linux-sound@vger.kernel.org In-Reply-To: <20261202103100.2-1-xuhx@hust.edu.cn> References: <20261202103100.2-1-xuhx@hust.edu.cn> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Wed, 02 Dec 2026 03:23:28 +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 6 potential issues to consider: - [High] ALSA: rawmidi: undeclared snd_rawmidi_dev_release in snd_rawmidi_init() - [High] ALSA: rawmidi: invalid container_of() in snd_rawmidi_dev_release() - [High] ALSA: rawmidi: use-after-free of rmidi after put_device() on open errors - [High] ALSA: rawmidi: rmidi->private_free() deferred past snd_card_do_free() - [High] ALSA: rawmidi: race in snd_rawmidi_open() before get_device(rmidi->dev) - [Medium] ALSA: rawmidi: leak of rmidi in snd_rawmidi_free() on dev alloc error -- commit 09c08573d097cdce254107ff8d32fa804fdca7e7 Author: Hanxiang Xu ALSA: rawmidi: keep struct snd_rawmidi alive until the last open file is cl= osed This commit moves the freeing of substreams, driver private data, and struct snd_rawmidi into a device release callback and takes device references while rawmidi files are open. > diff --git a/sound/core/rawmidi.c b/sound/core/rawmidi.c > index 630d9fc8e9da0..824c37a7773d9 100644 > --- a/sound/core/rawmidi.c > +++ b/sound/core/rawmidi.c > @@ -469,11 +469,14 @@ int snd_rawmidi_kernel_open_nested(struct snd_rawmi= di *rmidi, int subdevice, > return -EINVAL; > if (!try_module_get(rmidi->card->module)) > return -ENXIO; > + get_device(rmidi->dev); > =20 > mutex_lock_nested(&rmidi->open_mutex, depth); > err =3D rawmidi_open_priv(rmidi, subdevice, mode, rfile); > - if (err < 0) > + if (err < 0) { > module_put(rmidi->card->module); > + put_device(rmidi->dev); > + } > mutex_unlock(&rmidi->open_mutex); > return err; > } [Severity: High] Can put_device(rmidi->dev) free rmidi while rmidi->open_mutex is still held? If snd_rawmidi_free() concurrently drops its reference while rawmidi_open_priv() fails, put_device(rmidi->dev) drops the final reference and frees rmidi before mutex_unlock(&rmidi->open_mutex) runs. Similarly, in snd_rawmidi_open(), when try_module_get() fails, put_device(rmidi->dev) is called before snd_card_unref(rmidi->card), which can read rmidi->card from freed memory. > @@ -508,8 +511,10 @@ static int snd_rawmidi_open(struct inode *inode, str= uct file *file) > =20 > if (rmidi =3D=3D NULL) > return -ENODEV; > + get_device(rmidi->dev); > =20 > if (!try_module_get(rmidi->card->module)) { > + put_device(rmidi->dev); > snd_card_unref(rmidi->card); > return -ENXIO; > } [Severity: High] Can rmidi be freed before snd_rawmidi_open() calls get_device(rmidi->dev)? snd_lookup_minor_data() only pins card->card_dev under sound_mutex and does not take a reference on rmidi->dev. If a sub-driver removes the rawmidi device from a live card right after snd_lookup_minor_data() returns, snd_rawmidi_free() drops the only reference on rmidi->dev before snd_rawmidi_open() reaches get_device(): CPU0 CPU1 snd_rawmidi_open() rmidi =3D snd_lookup_minor_data(...) snd_device_free(card, rmidi) ... snd_rawmidi_free(rmidi) put_device(rmidi->dev) snd_rawmidi_dev_release() kfree(rmidi) get_device(rmidi->dev) <-- use-after-free > @@ -577,6 +582,7 @@ static int snd_rawmidi_open(struct inode *inode, stru= ct file *file) > mutex_unlock(&rmidi->open_mutex); > module_put(rmidi->card->module); > snd_card_unref(rmidi->card); > + put_device(rmidi->dev); > return err; > } > =20 > @@ -650,6 +656,7 @@ int snd_rawmidi_kernel_release_nested(struct snd_rawm= idi_file *rfile, > rawmidi_release_priv(rfile); > mutex_unlock(&rmidi->open_mutex); > module_put(rmidi->card->module); > + put_device(rmidi->dev); > return 0; > } > EXPORT_SYMBOL(snd_rawmidi_kernel_release_nested); > @@ -668,6 +675,7 @@ static int snd_rawmidi_release(struct inode *inode, s= truct file *file) > module =3D rmidi->card->module; > snd_card_file_remove(rmidi->card, file); > module_put(module); > + put_device(rmidi->dev); > return 0; > } [Severity: High] Does calling put_device(rmidi->dev) after snd_card_file_remove() and module_put() cause rmidi->private_free(rmidi) to run after the card has already been freed? When a card is disconnected while a rawmidi file is open, snd_rawmidi_dev_disconnect() unregisters rmidi->dev (dropping its parent reference on card->card_dev), leaving the file reference from snd_card_file_add() as the last reference keeping the card alive. On close, snd_card_file_remove() drops that last card reference and runs snd_card_do_free(): sound/core/init.c:snd_card_do_free() { ... snd_device_free_all(card); ... if (card->private_free) card->private_free(card); ... kfree(card); return 0; } During snd_device_free_all(card), snd_rawmidi_free() calls put_device(rmidi->dev), but because snd_rawmidi_release() still holds its reference on rmidi->dev, snd_rawmidi_dev_release() does not run yet. snd_card_do_free() then finishes freeing the card and driver private data, snd_rawmidi_release() drops the module reference, and only then does put_device(rmidi->dev) invoke rmidi->private_free(rmidi) on the freed card. The same ordering issue occurs in snd_rawmidi_open()'s error path and in snd_rawmidi_kernel_release_nested(). > @@ -1900,6 +1908,7 @@ int snd_rawmidi_init(struct snd_rawmidi *rmidi, > err =3D snd_device_alloc(&rmidi->dev, card); > if (err < 0) > return err; > + rmidi->dev->release =3D snd_rawmidi_dev_release; > if (rawmidi_is_ump(rmidi)) > dev_set_name(rmidi->dev, "umpC%iD%i", card->number, device); > else [Severity: High] Does this fail to compile because snd_rawmidi_dev_release() is used here before its static definition later in sound/core/rawmidi.c without a forward declaration? > @@ -1973,6 +1982,21 @@ static void snd_rawmidi_free_substreams(struct snd= _rawmidi_str *stream) > } > } > =20 > +/* > + * The rawmidi memory is freed by the embedded device's release > + * callback once the last reference (an open file) is dropped. > + */ > +static void snd_rawmidi_dev_release(struct device *dev) > +{ > + struct snd_rawmidi *rmidi =3D container_of(dev, struct snd_rawmidi, dev= ); > + > + snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]= ); > + snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT= ]); > + if (rmidi->private_free) > + rmidi->private_free(rmidi); > + kfree(rmidi); > +} [Severity: High] Is container_of(dev, struct snd_rawmidi, dev) valid when rmidi->dev is a struct device * pointer rather than an embedded struct device? Since commit ea29a02fd802 ("ALSA: rawmidi: Don't embed device"), struct snd_rawmidi declares struct device *dev, and snd_device_alloc() allocates a separate struct device on the heap. Applying container_of() to dev fails the compile-time static_assert type check in container_of(), computes a wild pointer from the standalone struct device allocation, and leaks dev because default_release_alloc's kfree(dev) is replaced. > + > /* called from ump.c, too */ > int snd_rawmidi_free(struct snd_rawmidi *rmidi) > { > @@ -1984,12 +2008,7 @@ int snd_rawmidi_free(struct snd_rawmidi *rmidi) > if (rmidi->ops && rmidi->ops->dev_unregister) > rmidi->ops->dev_unregister(rmidi); > =20 > - snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]= ); > - snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT= ]); > - if (rmidi->private_free) > - rmidi->private_free(rmidi); > put_device(rmidi->dev); > - kfree(rmidi); > return 0; > } > EXPORT_SYMBOL_GPL(snd_rawmidi_free); [Severity: Medium] Does snd_rawmidi_free() leak rmidi when snd_device_alloc() fails in snd_rawmidi_init()? In that case rmidi->dev is still NULL when snd_rawmidi_new() (or snd_ump_endpoint_new()) calls snd_rawmidi_free(), so put_device(NULL) is a no-op and rmidi is never freed. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261202103100.2-1-= xuhx@hust.edu.cn?part=3D1