All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] ALSA: usb-audio: Limit recursion depth when walking audio units
@ 2026-11-24 13:25 Yannick Müller
  2026-11-24 13:25 ` [PATCH 1/2] ALSA: usb-audio: Limit the depth of the clock topology walk Yannick Müller
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Yannick Müller @ 2026-11-24 13:25 UTC (permalink / raw)
  To: tiwai, perex; +Cc: linux-sound, Yannick Müller

Fix a bug that makes it possible to overflow the kernel stack by
specifying a long chain of unique nested audio units. The current
implementation lets an external device specify up to 255 audio units,
which results in a stack overflow, as walking through all of them
recursively leads to too many nested calls.

Limit the recursion depth to 16 nested units so that a device can no
longer exhaust the kernel stack with a long chain.

Yannick Müller (2):
  ALSA: usb-audio: Limit the depth of the clock topology walk
  ALSA: usb-audio: Limit the unit nesting depth in the mixer parser

 sound/usb/clock.c | 20 +++++++++++++++-----
 sound/usb/mixer.c | 25 ++++++++++++++++++++++++-
 2 files changed, 39 insertions(+), 6 deletions(-)


base-commit: 8d7fe9885c62fc9a95bf55d9db244d9672a8aadc
-- 
2.47.3


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/2] ALSA: usb-audio: Limit the depth of the clock topology walk
  2026-11-24 13:25 [PATCH 0/2] ALSA: usb-audio: Limit recursion depth when walking audio units Yannick Müller
@ 2026-11-24 13:25 ` Yannick Müller
  2026-11-24 13:30   ` sashiko-bot
  2026-11-24 13:25 ` [PATCH 2/2] ALSA: usb-audio: Limit the unit nesting depth in the mixer parser Yannick Müller
  2026-11-25 13:44 ` [PATCH 0/2] ALSA: usb-audio: Limit recursion depth when walking audio units Takashi Iwai
  2 siblings, 1 reply; 5+ messages in thread
From: Yannick Müller @ 2026-11-24 13:25 UTC (permalink / raw)
  To: tiwai, perex; +Cc: linux-sound, Yannick Müller

__uac_clock_find_source() recurses through clock selectors and
multipliers using entity IDs from the device's descriptors. The
visited bitmap stops loops, but a device can still chain up to 255
distinct clock entities. At about 96 bytes of stack per nested call
(x86-64, gcc 14) that is roughly 24 KB on a 16 KB kernel stack, so
a device with crafted descriptors can overflow it.

Pass the nesting depth down and fail with -EINVAL beyond 16 levels.

Signed-off-by: Yannick Müller <mail@ynnk.dev>
---
 sound/usb/clock.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/sound/usb/clock.c b/sound/usb/clock.c
index cc77eace7b59..74f9923e5015 100644
--- a/sound/usb/clock.c
+++ b/sound/usb/clock.c
@@ -292,9 +292,12 @@ static bool uac_clock_source_is_valid(struct snd_usb_audio *chip,
 		return uac_clock_source_is_valid_quirk(chip, fmt, source_id);
 }
 
+#define MAX_CLOCK_DEPTH	16
+
 static int __uac_clock_find_source(struct snd_usb_audio *chip,
 				   const struct audioformat *fmt, int entity_id,
-				   unsigned long *visited, bool validate)
+				   unsigned long *visited, bool validate,
+				   int depth)
 {
 	union uac23_clock_source_desc *source;
 	union uac23_clock_selector_desc *selector;
@@ -307,6 +310,13 @@ static int __uac_clock_find_source(struct snd_usb_audio *chip,
 
 	entity_id &= 0xff;
 
+	if (depth >= MAX_CLOCK_DEPTH) {
+		usb_audio_warn(chip,
+			 "%s(): clock topology is too deep, id %d.\n",
+			 __func__, entity_id);
+		return -EINVAL;
+	}
+
 	if (test_and_set_bit(entity_id, visited)) {
 		usb_audio_warn(chip,
 			 "%s(): recursive clock topology detected, id %d.\n",
@@ -381,7 +391,7 @@ static int __uac_clock_find_source(struct snd_usb_audio *chip,
 		cur = ret;
 		ret = __uac_clock_find_source(chip, fmt,
 					      sources[ret - 1],
-					      visited, validate);
+					      visited, validate, depth + 1);
 		if (ret > 0) {
 			/* Skip setting clock selector again for some devices */
 			if (chip->quirk_flags & QUIRK_FLAG_SKIP_CLOCK_SELECTOR ||
@@ -414,7 +424,7 @@ static int __uac_clock_find_source(struct snd_usb_audio *chip,
 
 			ret = __uac_clock_find_source(chip, fmt,
 						      sources[i - 1],
-						      visited, true);
+						      visited, true, depth + 1);
 			if (ret < 0)
 				continue;
 
@@ -436,7 +446,7 @@ static int __uac_clock_find_source(struct snd_usb_audio *chip,
 	if (multiplier)
 		return __uac_clock_find_source(chip, fmt,
 					       GET_VAL(multiplier, proto, bCSourceID),
-					       visited, validate);
+					       visited, validate, depth + 1);
 
 	return -EINVAL;
 }
@@ -462,7 +472,7 @@ int snd_usb_clock_find_source(struct snd_usb_audio *chip,
 	case UAC_VERSION_2:
 	case UAC_VERSION_3:
 		return __uac_clock_find_source(chip, fmt, fmt->clock, visited,
-					       validate);
+					       validate, 0);
 	default:
 		return -EINVAL;
 	}
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 2/2] ALSA: usb-audio: Limit the unit nesting depth in the mixer parser
  2026-11-24 13:25 [PATCH 0/2] ALSA: usb-audio: Limit recursion depth when walking audio units Yannick Müller
  2026-11-24 13:25 ` [PATCH 1/2] ALSA: usb-audio: Limit the depth of the clock topology walk Yannick Müller
@ 2026-11-24 13:25 ` Yannick Müller
  2026-11-25 13:44 ` [PATCH 0/2] ALSA: usb-audio: Limit recursion depth when walking audio units Takashi Iwai
  2 siblings, 0 replies; 5+ messages in thread
From: Yannick Müller @ 2026-11-24 13:25 UTC (permalink / raw)
  To: tiwai, perex; +Cc: linux-sound, Yannick Müller

parse_audio_unit() and __check_input_term() recurse along the source
IDs found in the device's descriptors. The unit and terminal bitmaps
only ensure that no ID is visited twice, so a device can still chain
all 255 unit IDs. At 176 to 304 bytes of stack per nested unit
(x86-64, gcc 14) that takes 44 to 76 KB and overflows the 16 KB
kernel stack while the device is probed.

Stop after 16 nested units and return -EINVAL, which the callers
already treat as a broken topology: that chain gets no controls and
the rest of the mixer is still created.  The deepest topology
documented in mixer_maps.c (SB Extigy) nests 12 units.

__check_input_term() nests at most once per call, so counting its
calls since check_input_term() gives the depth.

Signed-off-by: Yannick Müller <mail@ynnk.dev>
---
 sound/usb/mixer.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/sound/usb/mixer.c b/sound/usb/mixer.c
index 95209ec006b3..37850f34caae 100644
--- a/sound/usb/mixer.c
+++ b/sound/usb/mixer.c
@@ -51,6 +51,7 @@
 #include "power.h"
 
 #define MAX_ID_ELEMS	256
+#define MAX_UNIT_DEPTH	16
 
 struct usb_audio_term {
 	int id;
@@ -69,6 +70,8 @@ struct mixer_build {
 	unsigned int buflen;
 	DECLARE_BITMAP(unitbitmap, MAX_ID_ELEMS);
 	DECLARE_BITMAP(termbitmap, MAX_ID_ELEMS);
+	unsigned int unit_depth;
+	unsigned int term_depth;
 	struct usb_audio_term oterm;
 	const struct usbmix_name_map *map;
 	const struct usbmix_selector_map *selector_map;
@@ -1023,6 +1026,9 @@ static int __check_input_term(struct mixer_build *state, int id,
 	void *p1;
 	unsigned char *hdr;
 
+	if (++state->term_depth > MAX_UNIT_DEPTH)
+		return -EINVAL;
+
 	for (;;) {
 		/* a loop in the terminal chain? */
 		if (test_and_set_bit(id, state->termbitmap))
@@ -1093,6 +1099,7 @@ static int check_input_term(struct mixer_build *state, int id,
 {
 	memset(term, 0, sizeof(*term));
 	memset(state->termbitmap, 0, sizeof(state->termbitmap));
+	state->term_depth = 0;
 	return __check_input_term(state, id, term);
 }
 
@@ -3180,7 +3187,7 @@ static int parse_audio_selector_unit(struct mixer_build *state, int unitid,
  * parse an audio unit recursively
  */
 
-static int parse_audio_unit(struct mixer_build *state, int unitid)
+static int __parse_audio_unit(struct mixer_build *state, int unitid)
 {
 	unsigned char *p1;
 	int protocol = state->mixer->protocol;
@@ -3240,6 +3247,22 @@ static int parse_audio_unit(struct mixer_build *state, int unitid)
 	}
 }
 
+static int parse_audio_unit(struct mixer_build *state, int unitid)
+{
+	int err;
+
+	if (state->unit_depth >= MAX_UNIT_DEPTH) {
+		usb_audio_err(state->chip,
+			      "unit %d: unit chain is too deep\n", unitid);
+		return -EINVAL;
+	}
+
+	state->unit_depth++;
+	err = __parse_audio_unit(state, unitid);
+	state->unit_depth--;
+	return err;
+}
+
 static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
 {
 	struct usb_mixer_elem_list *list, *next;
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/2] ALSA: usb-audio: Limit the depth of the clock topology walk
  2026-11-24 13:25 ` [PATCH 1/2] ALSA: usb-audio: Limit the depth of the clock topology walk Yannick Müller
@ 2026-11-24 13:30   ` sashiko-bot
  0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-11-24 13:30 UTC (permalink / raw)
  To: Yannick Müller; +Cc: linux-sound

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] ALSA: usb-audio: missing Fixes: trailer for
  __uac_clock_find_source() recursion limit
--

commit 0d53f82d7f2ae58ca83e6e58e10001a394d6a384
Author: Yannick Müller <mail@ynnk.dev>

> ALSA: usb-audio: Limit the depth of the clock topology walk
>
> __uac_clock_find_source() recurses through clock selectors and
> multipliers using entity IDs from the device's descriptors. The
> visited bitmap stops loops, but a device can still chain up to 255
> distinct clock entities. At about 96 bytes of stack per nested call
> (x86-64, gcc 14) that is roughly 24 KB on a 16 KB kernel stack, so
> a device with crafted descriptors can overflow it.
>
> Pass the nesting depth down and fail with -EINVAL beyond 16 levels.
>
> Signed-off-by: Yannick Müller <mail@ynnk.dev>

[Severity: Low]
This fixes a potential kernel stack overflow in __uac_clock_find_source().
Should this include a Fixes: tag for the commit that introduced the
recursive clock topology walk?

  Fixes: 79f920fbff56 ("ALSA: usb-audio: parse clock topology of UAC2 devices")

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261124132551.137134-1-mail@ynnk.dev?part=1

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 0/2] ALSA: usb-audio: Limit recursion depth when walking audio units
  2026-11-24 13:25 [PATCH 0/2] ALSA: usb-audio: Limit recursion depth when walking audio units Yannick Müller
  2026-11-24 13:25 ` [PATCH 1/2] ALSA: usb-audio: Limit the depth of the clock topology walk Yannick Müller
  2026-11-24 13:25 ` [PATCH 2/2] ALSA: usb-audio: Limit the unit nesting depth in the mixer parser Yannick Müller
@ 2026-11-25 13:44 ` Takashi Iwai
  2 siblings, 0 replies; 5+ messages in thread
From: Takashi Iwai @ 2026-11-25 13:44 UTC (permalink / raw)
  To: Yannick Müller; +Cc: tiwai, perex, linux-sound

On Tue, 24 Nov 2026 14:25:49 +0100,
Yannick Müller wrote:
> 
> Fix a bug that makes it possible to overflow the kernel stack by
> specifying a long chain of unique nested audio units. The current
> implementation lets an external device specify up to 255 audio units,
> which results in a stack overflow, as walking through all of them
> recursively leads to too many nested calls.
> 
> Limit the recursion depth to 16 nested units so that a device can no
> longer exhaust the kernel stack with a long chain.
> 
> Yannick Müller (2):
>   ALSA: usb-audio: Limit the depth of the clock topology walk
>   ALSA: usb-audio: Limit the unit nesting depth in the mixer parser

Applied both patches to for-next branch.  Thanks.


Takashi

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-11-25 13:45 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24 13:25 [PATCH 0/2] ALSA: usb-audio: Limit recursion depth when walking audio units Yannick Müller
2026-11-24 13:25 ` [PATCH 1/2] ALSA: usb-audio: Limit the depth of the clock topology walk Yannick Müller
2026-11-24 13:30   ` sashiko-bot
2026-11-24 13:25 ` [PATCH 2/2] ALSA: usb-audio: Limit the unit nesting depth in the mixer parser Yannick Müller
2026-11-25 13:44 ` [PATCH 0/2] ALSA: usb-audio: Limit recursion depth when walking audio units Takashi Iwai

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.