From: Takashi Iwai <tiwai@suse.de>
To: linux-sound@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, Michal Pecio <michal.pecio@gmail.com>
Subject: [PATCH v2 2/3] ALSA: usb: hiface: Avoid embedded URBs
Date: Thu, 3 Sep 2026 18:04:38 +0200 [thread overview]
Message-ID: <20260903160458.1938392-3-tiwai@suse.de> (raw)
In-Reply-To: <20260903160458.1938392-1-tiwai@suse.de>
The hiface driver uses URBs embedded in struct pcm_urb, and this is
basically a buggy implementation nowadays; since a URB is managed with
a refcount, this may lead to a UAF when the URB is released
asynchronously.
For addressing the problem, this patch converts the embedded URBs to
ones that are properly allocated via usb_alloc_urb().
The conversion is rather straightforward; pcm_urb.instance became a
pointer, assigned/freed via usb_alloc_urb() and usb_free_urb(), and
the call with this is corrected accordingly.
Along with it, the resource release is done in the common destructor
that is called from both at the error path and the disconnect.
No functional changes, only compile-tested.
Link: https://lore.kernel.org/20260903130757.0668310a.michal.pecio@gmail.com
Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
v1->v2: use a common destructor at error path and disconnect
sound/usb/hiface/pcm.c | 44 ++++++++++++++++++++++--------------------
1 file changed, 23 insertions(+), 21 deletions(-)
diff --git a/sound/usb/hiface/pcm.c b/sound/usb/hiface/pcm.c
index cd1a4c871c5d..3157952e4c1d 100644
--- a/sound/usb/hiface/pcm.c
+++ b/sound/usb/hiface/pcm.c
@@ -24,7 +24,7 @@
struct pcm_urb {
struct hiface_chip *chip;
- struct urb instance;
+ struct urb *instance;
struct usb_anchor submitted;
u8 *buffer;
};
@@ -193,7 +193,7 @@ static void hiface_pcm_stream_stop(struct pcm_runtime *rt)
if (!time)
usb_kill_anchored_urbs(
&rt->out_urbs[i].submitted);
- usb_kill_urb(&rt->out_urbs[i].instance);
+ usb_kill_urb(rt->out_urbs[i].instance);
}
rt->stream_state = STREAM_DISABLED;
@@ -215,9 +215,9 @@ static int hiface_pcm_stream_start(struct pcm_runtime *rt)
rt->stream_state = STREAM_STARTING;
for (i = 0; i < PCM_N_URBS; i++) {
memset(rt->out_urbs[i].buffer, 0, PCM_PACKET_SIZE);
- usb_anchor_urb(&rt->out_urbs[i].instance,
+ usb_anchor_urb(rt->out_urbs[i].instance,
&rt->out_urbs[i].submitted);
- ret = usb_submit_urb(&rt->out_urbs[i].instance,
+ ret = usb_submit_urb(rt->out_urbs[i].instance,
GFP_ATOMIC);
if (ret) {
hiface_pcm_stream_stop(rt);
@@ -334,7 +334,7 @@ static void hiface_pcm_out_urb_handler(struct urb *usb_urb)
if (do_period_elapsed)
snd_pcm_period_elapsed(sub->instance);
- ret = usb_submit_urb(&out_urb->instance, GFP_ATOMIC);
+ ret = usb_submit_urb(out_urb->instance, GFP_ATOMIC);
if (ret < 0)
goto out_fail;
@@ -492,16 +492,18 @@ static int hiface_pcm_init_urb(struct pcm_urb *urb,
void (*handler)(struct urb *))
{
urb->chip = chip;
- usb_init_urb(&urb->instance);
+ urb->instance = usb_alloc_urb(0, GFP_KERNEL);
+ if (!urb->instance)
+ return -ENOMEM;
urb->buffer = kzalloc(PCM_PACKET_SIZE, GFP_KERNEL);
if (!urb->buffer)
return -ENOMEM;
- usb_fill_bulk_urb(&urb->instance, chip->dev,
+ usb_fill_bulk_urb(urb->instance, chip->dev,
usb_sndbulkpipe(chip->dev, ep), (void *)urb->buffer,
PCM_PACKET_SIZE, handler, urb);
- if (usb_urb_ep_type_check(&urb->instance))
+ if (usb_urb_ep_type_check(urb->instance))
return -EINVAL;
init_usb_anchor(&urb->submitted);
@@ -520,24 +522,26 @@ void hiface_pcm_abort(struct hiface_chip *chip)
}
}
-static void hiface_pcm_destroy(struct hiface_chip *chip)
+static void hiface_pcm_destroy(struct pcm_runtime *rt)
{
- struct pcm_runtime *rt = chip->pcm;
int i;
- for (i = 0; i < PCM_N_URBS; i++)
- kfree(rt->out_urbs[i].buffer);
+ if (!rt)
+ return;
- kfree(chip->pcm);
- chip->pcm = NULL;
+ if (rt->chip)
+ rt->chip->pcm = NULL;
+
+ for (i = 0; i < PCM_N_URBS; i++) {
+ usb_free_urb(rt->out_urbs[i].instance);
+ kfree(rt->out_urbs[i].buffer);
+ }
+ kfree(rt);
}
static void hiface_pcm_free(struct snd_pcm *pcm)
{
- struct pcm_runtime *rt = pcm->private_data;
-
- if (rt)
- hiface_pcm_destroy(rt->chip);
+ hiface_pcm_destroy(pcm->private_data);
}
int hiface_pcm_init(struct hiface_chip *chip, u8 extra_freq)
@@ -587,8 +591,6 @@ int hiface_pcm_init(struct hiface_chip *chip, u8 extra_freq)
return 0;
error:
- for (i = 0; i < PCM_N_URBS; i++)
- kfree(rt->out_urbs[i].buffer);
- kfree(rt);
+ hiface_pcm_destroy(rt);
return ret;
}
--
2.55.0
next prev parent reply other threads:[~2026-09-03 16:05 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-03 16:04 [PATCH v2 0/3] ALSA: usb: Avoid embedded URBs Takashi Iwai
2026-09-03 16:04 ` [PATCH v2 1/3] ALSA: usb: ua101: " Takashi Iwai
2026-09-03 16:04 ` Takashi Iwai [this message]
2026-09-03 16:04 ` [PATCH v2 3/3] ALSA: usb: 6fire: " Takashi Iwai
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=20260903160458.1938392-3-tiwai@suse.de \
--to=tiwai@suse.de \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-sound@vger.kernel.org \
--cc=michal.pecio@gmail.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