From: "Yannick Müller" <mail@ynnk.dev>
To: tiwai@suse.com, perex@perex.cz
Cc: linux-sound@vger.kernel.org, "Yannick Müller" <mail@ynnk.dev>
Subject: [PATCH 2/2] ALSA: usb-audio: Limit the unit nesting depth in the mixer parser
Date: Tue, 24 Nov 2026 14:25:51 +0100 [thread overview]
Message-ID: <20261124132551.137134-3-mail@ynnk.dev> (raw)
In-Reply-To: <20261124132551.137134-1-mail@ynnk.dev>
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
prev parent reply other threads:[~2026-11-24 13:26 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
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 ` Yannick Müller [this message]
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=20261124132551.137134-3-mail@ynnk.dev \
--to=mail@ynnk.dev \
--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