mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Luca Rodenhäuser" <otzelot2021@outlook.de>
To: perex@perex.cz, tiwai@suse.com
Cc: linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org,
	"Luca Rodenhäuser" <otzelot2021@outlook.de>
Subject: [RFC PATCH 2/8] ALSA: control: copy the card bytes outside snd_ioctl_rwsem
Date: Tue, 15 Sep 2026 17:59:02 +0200	[thread overview]
Message-ID: <AMBP191MB2886BC8F731179ADEAE6CB63CEBA2@AMBP191MB2886.EURP191.PROD.OUTLOOK.COM> (raw)
In-Reply-To: <AMBP191MB2886193B1F896974A7CE23E1CEBA2@AMBP191MB2886.EURP191.PROD.OUTLOOK.COM>

snd_ctl_card_bytes() holds snd_ioctl_rwsem for reading across its
copy_to_user().  That lock is global to ALSA and is taken for writing from
the card probe path, by snd_component_add().

A user buffer that faults can be made to take arbitrarily long: with
userfaultfd, or with the target buffer mapped from a FUSE file, an
unprivileged process controls when the fault resolves.  While it holds the
reader, a writer from a probing card queues up behind it, and because rwsem
is writer-fair every later reader queues behind the writer.  Card teardown
then waits on the probe path, so an unprivileged reader can stall the
removal of a card it does not own.

Take a copy of the string under the lock, drop the lock, and copy to user
space afterwards.  The extra allocation is a card name sized string on a
path that is not hot.

While at it, report the required length even when the caller's buffer was
too small.  The -ENOMEM return told the caller to try again with a bigger
buffer without telling it how big, and data_len is what the query form of
this ioctl exists for.

Assisted-by: LLM
Signed-off-by: Luca Rodenhäuser <otzelot2021@outlook.de>
---
 sound/core/control.c | 46 +++++++++++++++++++++++++++-----------------
 1 file changed, 28 insertions(+), 18 deletions(-)

diff --git a/sound/core/control.c b/sound/core/control.c
index 4199342d4f..e404cb55a5 100644
--- a/sound/core/control.c
+++ b/sound/core/control.c
@@ -14,6 +14,7 @@
 #include <linux/mm.h>
 #include <linux/math64.h>
 #include <linux/sched/signal.h>
+#include <linux/cleanup.h>
 #include <sound/core.h>
 #include <sound/minors.h>
 #include <sound/info.h>
@@ -904,36 +905,45 @@ static int snd_ctl_card_bytes(struct snd_card *card,
 			      struct snd_ctl_card_bytes *info,
 			      unsigned int __user *data_len_out)
 {
+	char *copy __free(kfree) = NULL;
+	bool too_small = false;
 	unsigned int data_len;
 
-	switch (info->type) {
-	case SND_CTL_CARD_BTYPE_COMPONENTS:
-		scoped_guard(rwsem_read, &snd_ioctl_rwsem) {
-			const char *components = card->components;
-
-			if (!components)
-				components = "";
+	if (info->type != SND_CTL_CARD_BTYPE_COMPONENTS)
+		return -EINVAL;
 
-			data_len = strlen(components) + 1;
+	scoped_guard(rwsem_read, &snd_ioctl_rwsem) {
+		const char *components = card->components;
 
-			if (!info->data || info->data_allocated == 0)
-				break;
+		if (!components)
+			components = "";
 
-			if (info->data_allocated < data_len)
-				return -ENOMEM;
+		data_len = strlen(components) + 1;
 
-			if (copy_to_user(u64_to_user_ptr(info->data), components, data_len))
-				return -EFAULT;
+		if (info->data && info->data_allocated != 0) {
+			if (info->data_allocated < data_len) {
+				too_small = true;
+			} else {
+				copy = kmemdup(components, data_len, GFP_KERNEL);
+				if (!copy)
+					return -ENOMEM;
+			}
 		}
-		break;
-	default:
-		return -EINVAL;
 	}
 
+	/* Copy outside the lock.  A user buffer that faults can be made to take
+	 * arbitrarily long (userfaultfd, FUSE), and snd_ioctl_rwsem is global:
+	 * holding it across the copy lets an unprivileged reader block the card
+	 * probe path, and with it a card teardown waiting on that path.
+	 */
+	if (copy && copy_to_user(u64_to_user_ptr(info->data), copy, data_len))
+		return -EFAULT;
+
+	/* report the required size even when the buffer was too small */
 	if (put_user(data_len, data_len_out))
 		return -EFAULT;
 
-	return 0;
+	return too_small ? -ENOMEM : 0;
 }
 
 static int snd_ctl_card_bytes_user(struct snd_card *card,
-- 
2.43.0


  parent reply	other threads:[~2026-09-15 15:59 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-15 15:59 [RFC PATCH 0/8] ALSA: untruncated card names Luca Rodenhäuser
2026-09-15 15:59 ` [RFC PATCH 1/8] ALSA: core: keep non-ASCII bytes out of the card id Luca Rodenhäuser
2026-09-15 15:59 ` Luca Rodenhäuser [this message]
2026-09-15 15:59 ` [RFC PATCH 3/8] ALSA: usb-audio: don't cut the card name inside a UTF-8 character Luca Rodenhäuser
2026-09-15 15:59 ` [RFC PATCH 4/8] ALSA: core: carry the untruncated form of the card names Luca Rodenhäuser
2026-09-15 15:59 ` [RFC PATCH 5/8] ALSA: control: return the untruncated card names via CARD_BYTES Luca Rodenhäuser
2026-09-15 15:59 ` [RFC PATCH 6/8] ALSA: core: add KUnit coverage for the card names Luca Rodenhäuser
2026-09-15 15:59 ` [RFC PATCH 7/8] ALSA: usb-audio: offer the untruncated " Luca Rodenhäuser
2026-09-15 15:59 ` [RFC PATCH 8/8] ALSA: Documentation: describe the card name fields Luca Rodenhäuser

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=AMBP191MB2886BC8F731179ADEAE6CB63CEBA2@AMBP191MB2886.EURP191.PROD.OUTLOOK.COM \
    --to=otzelot2021@outlook.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=perex@perex.cz \
    --cc=tiwai@suse.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®