ALSA: usb-audio: Kill stray URB at exiting

Message ID 1531234709-6783-2-git-send-email-paolo.pisati@canonical.com
State New
Headers show
Series
  • ALSA: usb-audio: Kill stray URB at exiting
Related show

Commit Message

Paolo Pisati July 10, 2018, 2:58 p.m.
From: Takashi Iwai <tiwai@suse.de>

USB-audio driver may leave a stray URB for the mixer interrupt when it
exits by some error during probe.  This leads to a use-after-free
error as spotted by syzkaller like:
  ==================================================================
  BUG: KASAN: use-after-free in snd_usb_mixer_interrupt+0x604/0x6f0
  Call Trace:
   <IRQ>
   __dump_stack lib/dump_stack.c:16
   dump_stack+0x292/0x395 lib/dump_stack.c:52
   print_address_description+0x78/0x280 mm/kasan/report.c:252
   kasan_report_error mm/kasan/report.c:351
   kasan_report+0x23d/0x350 mm/kasan/report.c:409
   __asan_report_load8_noabort+0x19/0x20 mm/kasan/report.c:430
   snd_usb_mixer_interrupt+0x604/0x6f0 sound/usb/mixer.c:2490
   __usb_hcd_giveback_urb+0x2e0/0x650 drivers/usb/core/hcd.c:1779
   ....

  Allocated by task 1484:
   save_stack_trace+0x1b/0x20 arch/x86/kernel/stacktrace.c:59
   save_stack+0x43/0xd0 mm/kasan/kasan.c:447
   set_track mm/kasan/kasan.c:459
   kasan_kmalloc+0xad/0xe0 mm/kasan/kasan.c:551
   kmem_cache_alloc_trace+0x11e/0x2d0 mm/slub.c:2772
   kmalloc ./include/linux/slab.h:493
   kzalloc ./include/linux/slab.h:666
   snd_usb_create_mixer+0x145/0x1010 sound/usb/mixer.c:2540
   create_standard_mixer_quirk+0x58/0x80 sound/usb/quirks.c:516
   snd_usb_create_quirk+0x92/0x100 sound/usb/quirks.c:560
   create_composite_quirk+0x1c4/0x3e0 sound/usb/quirks.c:59
   snd_usb_create_quirk+0x92/0x100 sound/usb/quirks.c:560
   usb_audio_probe+0x1040/0x2c10 sound/usb/card.c:618
   ....

  Freed by task 1484:
   save_stack_trace+0x1b/0x20 arch/x86/kernel/stacktrace.c:59
   save_stack+0x43/0xd0 mm/kasan/kasan.c:447
   set_track mm/kasan/kasan.c:459
   kasan_slab_free+0x72/0xc0 mm/kasan/kasan.c:524
   slab_free_hook mm/slub.c:1390
   slab_free_freelist_hook mm/slub.c:1412
   slab_free mm/slub.c:2988
   kfree+0xf6/0x2f0 mm/slub.c:3919
   snd_usb_mixer_free+0x11a/0x160 sound/usb/mixer.c:2244
   snd_usb_mixer_dev_free+0x36/0x50 sound/usb/mixer.c:2250
   __snd_device_free+0x1ff/0x380 sound/core/device.c:91
   snd_device_free_all+0x8f/0xe0 sound/core/device.c:244
   snd_card_do_free sound/core/init.c:461
   release_card_device+0x47/0x170 sound/core/init.c:181
   device_release+0x13f/0x210 drivers/base/core.c:814
   ....

Actually such a URB is killed properly at disconnection when the
device gets probed successfully, and what we need is to apply it for
the error-path, too.

In this patch, we apply snd_usb_mixer_disconnect() at releasing.
Also introduce a new flag, disconnected, to struct usb_mixer_interface
for not performing the disconnection procedure twice.

Reported-by: Andrey Konovalov <andreyknvl@google.com>
Tested-by: Andrey Konovalov <andreyknvl@google.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
(cherry picked from commit 124751d5e63c823092060074bd0abaae61aaa9c4)
Signed-off-by: Paolo Pisati <paolo.pisati@canonical.com>
---
 sound/usb/card.c  |  5 ++++-
 sound/usb/mixer.c | 17 +++++++++++------
 sound/usb/mixer.h |  4 +++-
 3 files changed, 18 insertions(+), 8 deletions(-)

Comments

Stefan Bader July 19, 2018, 12:22 p.m. | #1
On 10.07.2018 16:58, Paolo Pisati wrote:
> From: Takashi Iwai <tiwai@suse.de>
> 
> USB-audio driver may leave a stray URB for the mixer interrupt when it
> exits by some error during probe.  This leads to a use-after-free
> error as spotted by syzkaller like:
>   ==================================================================
>   BUG: KASAN: use-after-free in snd_usb_mixer_interrupt+0x604/0x6f0
>   Call Trace:
>    <IRQ>
>    __dump_stack lib/dump_stack.c:16
>    dump_stack+0x292/0x395 lib/dump_stack.c:52
>    print_address_description+0x78/0x280 mm/kasan/report.c:252
>    kasan_report_error mm/kasan/report.c:351
>    kasan_report+0x23d/0x350 mm/kasan/report.c:409
>    __asan_report_load8_noabort+0x19/0x20 mm/kasan/report.c:430
>    snd_usb_mixer_interrupt+0x604/0x6f0 sound/usb/mixer.c:2490
>    __usb_hcd_giveback_urb+0x2e0/0x650 drivers/usb/core/hcd.c:1779
>    ....
> 
>   Allocated by task 1484:
>    save_stack_trace+0x1b/0x20 arch/x86/kernel/stacktrace.c:59
>    save_stack+0x43/0xd0 mm/kasan/kasan.c:447
>    set_track mm/kasan/kasan.c:459
>    kasan_kmalloc+0xad/0xe0 mm/kasan/kasan.c:551
>    kmem_cache_alloc_trace+0x11e/0x2d0 mm/slub.c:2772
>    kmalloc ./include/linux/slab.h:493
>    kzalloc ./include/linux/slab.h:666
>    snd_usb_create_mixer+0x145/0x1010 sound/usb/mixer.c:2540
>    create_standard_mixer_quirk+0x58/0x80 sound/usb/quirks.c:516
>    snd_usb_create_quirk+0x92/0x100 sound/usb/quirks.c:560
>    create_composite_quirk+0x1c4/0x3e0 sound/usb/quirks.c:59
>    snd_usb_create_quirk+0x92/0x100 sound/usb/quirks.c:560
>    usb_audio_probe+0x1040/0x2c10 sound/usb/card.c:618
>    ....
> 
>   Freed by task 1484:
>    save_stack_trace+0x1b/0x20 arch/x86/kernel/stacktrace.c:59
>    save_stack+0x43/0xd0 mm/kasan/kasan.c:447
>    set_track mm/kasan/kasan.c:459
>    kasan_slab_free+0x72/0xc0 mm/kasan/kasan.c:524
>    slab_free_hook mm/slub.c:1390
>    slab_free_freelist_hook mm/slub.c:1412
>    slab_free mm/slub.c:2988
>    kfree+0xf6/0x2f0 mm/slub.c:3919
>    snd_usb_mixer_free+0x11a/0x160 sound/usb/mixer.c:2244
>    snd_usb_mixer_dev_free+0x36/0x50 sound/usb/mixer.c:2250
>    __snd_device_free+0x1ff/0x380 sound/core/device.c:91
>    snd_device_free_all+0x8f/0xe0 sound/core/device.c:244
>    snd_card_do_free sound/core/init.c:461
>    release_card_device+0x47/0x170 sound/core/init.c:181
>    device_release+0x13f/0x210 drivers/base/core.c:814
>    ....
> 
> Actually such a URB is killed properly at disconnection when the
> device gets probed successfully, and what we need is to apply it for
> the error-path, too.
> 
> In this patch, we apply snd_usb_mixer_disconnect() at releasing.
> Also introduce a new flag, disconnected, to struct usb_mixer_interface
> for not performing the disconnection procedure twice.
> 
> Reported-by: Andrey Konovalov <andreyknvl@google.com>
> Tested-by: Andrey Konovalov <andreyknvl@google.com>
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Takashi Iwai <tiwai@suse.de>
> (cherry picked from commit 124751d5e63c823092060074bd0abaae61aaa9c4)
> Signed-off-by: Paolo Pisati <paolo.pisati@canonical.com>
Acked-by: Stefan Bader <stefan.bader@canonical.com>
> ---
>  sound/usb/card.c  |  5 ++++-
>  sound/usb/mixer.c | 17 +++++++++++------
>  sound/usb/mixer.h |  4 +++-
>  3 files changed, 18 insertions(+), 8 deletions(-)
> 
> diff --git a/sound/usb/card.c b/sound/usb/card.c
> index ab433a0..abd816d 100644
> --- a/sound/usb/card.c
> +++ b/sound/usb/card.c
> @@ -616,7 +616,10 @@ static void snd_usb_audio_disconnect(struct usb_device *dev,
>  		}
>  		/* release mixer resources */
>  		list_for_each(p, &chip->mixer_list) {
> -			snd_usb_mixer_disconnect(p);
> +			struct usb_mixer_interface *mixer;
> +
> +			mixer = list_entry(p, struct usb_mixer_interface, list);
> +			snd_usb_mixer_disconnect(mixer);
>  		}
>  	}
>  
> diff --git a/sound/usb/mixer.c b/sound/usb/mixer.c
> index 43492a6..b369c46 100644
> --- a/sound/usb/mixer.c
> +++ b/sound/usb/mixer.c
> @@ -2071,6 +2071,9 @@ static int parse_audio_unit(struct mixer_build *state, int unitid)
>  
>  static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
>  {
> +	/* kill pending URBs */
> +	snd_usb_mixer_disconnect(mixer);
> +
>  	kfree(mixer->id_elems);
>  	if (mixer->urb) {
>  		kfree(mixer->urb->transfer_buffer);
> @@ -2412,11 +2415,13 @@ _error:
>  	return err;
>  }
>  
> -void snd_usb_mixer_disconnect(struct list_head *p)
> +void snd_usb_mixer_disconnect(struct usb_mixer_interface *mixer)
>  {
> -	struct usb_mixer_interface *mixer;
> -
> -	mixer = list_entry(p, struct usb_mixer_interface, list);
> -	usb_kill_urb(mixer->urb);
> -	usb_kill_urb(mixer->rc_urb);
> +	if (mixer->disconnected)
> +		return;
> +	if (mixer->urb)
> +		usb_kill_urb(mixer->urb);
> +	if (mixer->rc_urb)
> +		usb_kill_urb(mixer->rc_urb);
> +	mixer->disconnected = true;
>  }
> diff --git a/sound/usb/mixer.h b/sound/usb/mixer.h
> index aab80df..f74a4d6 100644
> --- a/sound/usb/mixer.h
> +++ b/sound/usb/mixer.h
> @@ -23,6 +23,8 @@ struct usb_mixer_interface {
>  
>  	u8 audigy2nx_leds[3];
>  	u8 xonar_u1_status;
> +
> +	bool disconnected;
>  };
>  
>  #define MAX_CHANNELS	16	/* max logical channels */
> @@ -57,7 +59,7 @@ struct usb_mixer_elem_info {
>  
>  int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
>  			 int ignore_error);
> -void snd_usb_mixer_disconnect(struct list_head *p);
> +void snd_usb_mixer_disconnect(struct usb_mixer_interface *mixer);
>  
>  void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer, int unitid);
>  
>
Juerg Haefliger July 23, 2018, 12:24 p.m. | #2
On 07/10/2018 04:58 PM, Paolo Pisati wrote:
> From: Takashi Iwai <tiwai@suse.de>

Added missing CVE line.


> USB-audio driver may leave a stray URB for the mixer interrupt when it
> exits by some error during probe.  This leads to a use-after-free
> error as spotted by syzkaller like:
>   ==================================================================
>   BUG: KASAN: use-after-free in snd_usb_mixer_interrupt+0x604/0x6f0
>   Call Trace:
>    <IRQ>
>    __dump_stack lib/dump_stack.c:16
>    dump_stack+0x292/0x395 lib/dump_stack.c:52
>    print_address_description+0x78/0x280 mm/kasan/report.c:252
>    kasan_report_error mm/kasan/report.c:351
>    kasan_report+0x23d/0x350 mm/kasan/report.c:409
>    __asan_report_load8_noabort+0x19/0x20 mm/kasan/report.c:430
>    snd_usb_mixer_interrupt+0x604/0x6f0 sound/usb/mixer.c:2490
>    __usb_hcd_giveback_urb+0x2e0/0x650 drivers/usb/core/hcd.c:1779
>    ....
> 
>   Allocated by task 1484:
>    save_stack_trace+0x1b/0x20 arch/x86/kernel/stacktrace.c:59
>    save_stack+0x43/0xd0 mm/kasan/kasan.c:447
>    set_track mm/kasan/kasan.c:459
>    kasan_kmalloc+0xad/0xe0 mm/kasan/kasan.c:551
>    kmem_cache_alloc_trace+0x11e/0x2d0 mm/slub.c:2772
>    kmalloc ./include/linux/slab.h:493
>    kzalloc ./include/linux/slab.h:666
>    snd_usb_create_mixer+0x145/0x1010 sound/usb/mixer.c:2540
>    create_standard_mixer_quirk+0x58/0x80 sound/usb/quirks.c:516
>    snd_usb_create_quirk+0x92/0x100 sound/usb/quirks.c:560
>    create_composite_quirk+0x1c4/0x3e0 sound/usb/quirks.c:59
>    snd_usb_create_quirk+0x92/0x100 sound/usb/quirks.c:560
>    usb_audio_probe+0x1040/0x2c10 sound/usb/card.c:618
>    ....
> 
>   Freed by task 1484:
>    save_stack_trace+0x1b/0x20 arch/x86/kernel/stacktrace.c:59
>    save_stack+0x43/0xd0 mm/kasan/kasan.c:447
>    set_track mm/kasan/kasan.c:459
>    kasan_slab_free+0x72/0xc0 mm/kasan/kasan.c:524
>    slab_free_hook mm/slub.c:1390
>    slab_free_freelist_hook mm/slub.c:1412
>    slab_free mm/slub.c:2988
>    kfree+0xf6/0x2f0 mm/slub.c:3919
>    snd_usb_mixer_free+0x11a/0x160 sound/usb/mixer.c:2244
>    snd_usb_mixer_dev_free+0x36/0x50 sound/usb/mixer.c:2250
>    __snd_device_free+0x1ff/0x380 sound/core/device.c:91
>    snd_device_free_all+0x8f/0xe0 sound/core/device.c:244
>    snd_card_do_free sound/core/init.c:461
>    release_card_device+0x47/0x170 sound/core/init.c:181
>    device_release+0x13f/0x210 drivers/base/core.c:814
>    ....
> 
> Actually such a URB is killed properly at disconnection when the
> device gets probed successfully, and what we need is to apply it for
> the error-path, too.
> 
> In this patch, we apply snd_usb_mixer_disconnect() at releasing.
> Also introduce a new flag, disconnected, to struct usb_mixer_interface
> for not performing the disconnection procedure twice.
> 
> Reported-by: Andrey Konovalov <andreyknvl@google.com>
> Tested-by: Andrey Konovalov <andreyknvl@google.com>
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Takashi Iwai <tiwai@suse.de>
> (cherry picked from commit 124751d5e63c823092060074bd0abaae61aaa9c4)
> Signed-off-by: Paolo Pisati <paolo.pisati@canonical.com>

Signed-off-by: Juerg Haefliger <juergh@canonical.com>

Applied to trusty master-next.

...Juerg


> ---
>  sound/usb/card.c  |  5 ++++-
>  sound/usb/mixer.c | 17 +++++++++++------
>  sound/usb/mixer.h |  4 +++-
>  3 files changed, 18 insertions(+), 8 deletions(-)
> 
> diff --git a/sound/usb/card.c b/sound/usb/card.c
> index ab433a0..abd816d 100644
> --- a/sound/usb/card.c
> +++ b/sound/usb/card.c
> @@ -616,7 +616,10 @@ static void snd_usb_audio_disconnect(struct usb_device *dev,
>  		}
>  		/* release mixer resources */
>  		list_for_each(p, &chip->mixer_list) {
> -			snd_usb_mixer_disconnect(p);
> +			struct usb_mixer_interface *mixer;
> +
> +			mixer = list_entry(p, struct usb_mixer_interface, list);
> +			snd_usb_mixer_disconnect(mixer);
>  		}
>  	}
>  
> diff --git a/sound/usb/mixer.c b/sound/usb/mixer.c
> index 43492a6..b369c46 100644
> --- a/sound/usb/mixer.c
> +++ b/sound/usb/mixer.c
> @@ -2071,6 +2071,9 @@ static int parse_audio_unit(struct mixer_build *state, int unitid)
>  
>  static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
>  {
> +	/* kill pending URBs */
> +	snd_usb_mixer_disconnect(mixer);
> +
>  	kfree(mixer->id_elems);
>  	if (mixer->urb) {
>  		kfree(mixer->urb->transfer_buffer);
> @@ -2412,11 +2415,13 @@ _error:
>  	return err;
>  }
>  
> -void snd_usb_mixer_disconnect(struct list_head *p)
> +void snd_usb_mixer_disconnect(struct usb_mixer_interface *mixer)
>  {
> -	struct usb_mixer_interface *mixer;
> -
> -	mixer = list_entry(p, struct usb_mixer_interface, list);
> -	usb_kill_urb(mixer->urb);
> -	usb_kill_urb(mixer->rc_urb);
> +	if (mixer->disconnected)
> +		return;
> +	if (mixer->urb)
> +		usb_kill_urb(mixer->urb);
> +	if (mixer->rc_urb)
> +		usb_kill_urb(mixer->rc_urb);
> +	mixer->disconnected = true;
>  }
> diff --git a/sound/usb/mixer.h b/sound/usb/mixer.h
> index aab80df..f74a4d6 100644
> --- a/sound/usb/mixer.h
> +++ b/sound/usb/mixer.h
> @@ -23,6 +23,8 @@ struct usb_mixer_interface {
>  
>  	u8 audigy2nx_leds[3];
>  	u8 xonar_u1_status;
> +
> +	bool disconnected;
>  };
>  
>  #define MAX_CHANNELS	16	/* max logical channels */
> @@ -57,7 +59,7 @@ struct usb_mixer_elem_info {
>  
>  int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
>  			 int ignore_error);
> -void snd_usb_mixer_disconnect(struct list_head *p);
> +void snd_usb_mixer_disconnect(struct usb_mixer_interface *mixer);
>  
>  void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer, int unitid);
>  
>

Patch

diff --git a/sound/usb/card.c b/sound/usb/card.c
index ab433a0..abd816d 100644
--- a/sound/usb/card.c
+++ b/sound/usb/card.c
@@ -616,7 +616,10 @@  static void snd_usb_audio_disconnect(struct usb_device *dev,
 		}
 		/* release mixer resources */
 		list_for_each(p, &chip->mixer_list) {
-			snd_usb_mixer_disconnect(p);
+			struct usb_mixer_interface *mixer;
+
+			mixer = list_entry(p, struct usb_mixer_interface, list);
+			snd_usb_mixer_disconnect(mixer);
 		}
 	}
 
diff --git a/sound/usb/mixer.c b/sound/usb/mixer.c
index 43492a6..b369c46 100644
--- a/sound/usb/mixer.c
+++ b/sound/usb/mixer.c
@@ -2071,6 +2071,9 @@  static int parse_audio_unit(struct mixer_build *state, int unitid)
 
 static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
 {
+	/* kill pending URBs */
+	snd_usb_mixer_disconnect(mixer);
+
 	kfree(mixer->id_elems);
 	if (mixer->urb) {
 		kfree(mixer->urb->transfer_buffer);
@@ -2412,11 +2415,13 @@  _error:
 	return err;
 }
 
-void snd_usb_mixer_disconnect(struct list_head *p)
+void snd_usb_mixer_disconnect(struct usb_mixer_interface *mixer)
 {
-	struct usb_mixer_interface *mixer;
-
-	mixer = list_entry(p, struct usb_mixer_interface, list);
-	usb_kill_urb(mixer->urb);
-	usb_kill_urb(mixer->rc_urb);
+	if (mixer->disconnected)
+		return;
+	if (mixer->urb)
+		usb_kill_urb(mixer->urb);
+	if (mixer->rc_urb)
+		usb_kill_urb(mixer->rc_urb);
+	mixer->disconnected = true;
 }
diff --git a/sound/usb/mixer.h b/sound/usb/mixer.h
index aab80df..f74a4d6 100644
--- a/sound/usb/mixer.h
+++ b/sound/usb/mixer.h
@@ -23,6 +23,8 @@  struct usb_mixer_interface {
 
 	u8 audigy2nx_leds[3];
 	u8 xonar_u1_status;
+
+	bool disconnected;
 };
 
 #define MAX_CHANNELS	16	/* max logical channels */
@@ -57,7 +59,7 @@  struct usb_mixer_elem_info {
 
 int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif,
 			 int ignore_error);
-void snd_usb_mixer_disconnect(struct list_head *p);
+void snd_usb_mixer_disconnect(struct usb_mixer_interface *mixer);
 
 void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer, int unitid);