Skip to content

Commit e74a14a

Browse files
nefigtutfourkbomb
authored andcommitted
ALSA: usb-audio: Fix double-free in error paths after snd_usb_add_audio_stream() call
create_fixed_stream_quirk(), snd_usb_parse_audio_interface() and create_uaxx_quirk() functions allocate the audioformat object by themselves and free it upon error before returning. However, once the object is linked to a stream, it's freed again in snd_usb_audio_pcm_free(), thus it'll be double-freed, eventually resulting in a memory corruption. This patch fixes these failures in the error paths by unlinking the audioformat object before freeing it. Based on a patch by Takashi Iwai <[email protected]> [Note for stable backports: this patch requires the commit 902eb7fd1e4a ('ALSA: usb-audio: Minor code cleanup in create_fixed_stream_quirk()')] Change-Id: I129dc4f3b0ae4cb6f790c16d24dd768c9ee06822 Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1283358 Reported-by: Ralf Spenneberg <[email protected]> Cc: <[email protected]> # see the note above Signed-off-by: Vladis Dronov <[email protected]> Signed-off-by: Takashi Iwai <[email protected]>
1 parent 74f5362 commit e74a14a

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

sound/usb/quirks.c

+4
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ static int create_fixed_stream_quirk(struct snd_usb_audio *chip,
136136
snd_printk(KERN_ERR "cannot memdup\n");
137137
return -ENOMEM;
138138
}
139+
INIT_LIST_HEAD(&fp->list);
139140
if (fp->nr_rates > MAX_NR_RATES) {
140141
kfree(fp);
141142
return -EINVAL;
@@ -169,6 +170,7 @@ static int create_fixed_stream_quirk(struct snd_usb_audio *chip,
169170
return 0;
170171

171172
error:
173+
list_del(&fp->list); /* unlink for avoiding double-free */
172174
kfree(fp);
173175
kfree(rate_table);
174176
return err;
@@ -238,6 +240,7 @@ static int create_uaxx_quirk(struct snd_usb_audio *chip,
238240
fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
239241
fp->datainterval = 0;
240242
fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
243+
INIT_LIST_HEAD(&fp->list);
241244

242245
switch (fp->maxpacksize) {
243246
case 0x120:
@@ -261,6 +264,7 @@ static int create_uaxx_quirk(struct snd_usb_audio *chip,
261264
? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
262265
err = snd_usb_add_audio_stream(chip, stream, fp);
263266
if (err < 0) {
267+
list_del(&fp->list); /* unlink for avoiding double-free */
264268
kfree(fp);
265269
return err;
266270
}

sound/usb/stream.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ static void snd_usb_audio_pcm_free(struct snd_pcm *pcm)
7777
/*
7878
* add this endpoint to the chip instance.
7979
* if a stream with the same endpoint already exists, append to it.
80-
* if not, create a new pcm stream.
80+
* if not, create a new pcm stream. note, fp is added to the substream
81+
* fmt_list and will be freed on the chip instance release. do not free
82+
* fp or do remove it from the substream fmt_list to avoid double-free.
8183
*/
8284
int snd_usb_add_audio_stream(struct snd_usb_audio *chip,
8385
int stream,
@@ -395,6 +397,7 @@ int snd_usb_parse_audio_interface(struct snd_usb_audio *chip, int iface_no)
395397
* (fp->maxpacksize & 0x7ff);
396398
fp->attributes = parse_uac_endpoint_attributes(chip, alts, protocol, iface_no);
397399
fp->clock = clock;
400+
INIT_LIST_HEAD(&fp->list);
398401

399402
/* some quirks for attributes here */
400403

@@ -438,6 +441,7 @@ int snd_usb_parse_audio_interface(struct snd_usb_audio *chip, int iface_no)
438441
snd_printdd(KERN_INFO "%d:%u:%d: add audio endpoint %#x\n", dev->devnum, iface_no, altno, fp->endpoint);
439442
err = snd_usb_add_audio_stream(chip, stream, fp);
440443
if (err < 0) {
444+
list_del(&fp->list); /* unlink for avoiding double-free */
441445
kfree(fp->rate_table);
442446
kfree(fp);
443447
return err;

0 commit comments

Comments
 (0)