Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S936514AbcKNVGv (ORCPT ); Mon, 14 Nov 2016 16:06:51 -0500 Received: from mx2.suse.de ([195.135.220.15]:39592 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932462AbcKNVGt (ORCPT ); Mon, 14 Nov 2016 16:06:49 -0500 Date: Mon, 14 Nov 2016 22:06:47 +0100 Message-ID: From: Takashi Iwai To: Shuah Khan Cc: Shuah Khan , alsa-devel@alsa-project.org, LKML Subject: Re: BUG: KASAN: use-after-free in snd_usb_audio_free In-Reply-To: References: User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI/1.14.6 (Maruoka) FLIM/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL/10.8 Emacs/24.5 (x86_64-suse-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI 1.14.6 - "Maruoka") Content-Type: text/plain; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 4348 Lines: 126 On Mon, 14 Nov 2016 17:55:54 +0100, Shuah Khan wrote: > > On 11/14/2016 03:25 AM, Takashi Iwai wrote: > > On Sat, 12 Nov 2016 00:34:38 +0100, > > Shuah Khan wrote: > >> > >> Hi Takashi, > >> > >> I am seeing the following use-after-free error when I disconnect an > >> USB speaker. I saw this on 4.9-rc4 and 4.8.7. There might be race > >> condition between the disconnect and pcm close perhaps. > > > > Thanks, this looks like a new discovery. > > Could you check whether the patch below works? > > > > > > Takashi > > > > --- > > diff --git a/sound/usb/card.c b/sound/usb/card.c > > index 9e5276d6dda0..2ddc034673a8 100644 > > --- a/sound/usb/card.c > > +++ b/sound/usb/card.c > > @@ -315,7 +315,8 @@ static int snd_usb_audio_free(struct snd_usb_audio *chip) > > snd_usb_endpoint_free(ep); > > > > mutex_destroy(&chip->mutex); > > - dev_set_drvdata(&chip->dev->dev, NULL); > > + if (!atomic_read(&chip->shutdown)) > > + dev_set_drvdata(&chip->dev->dev, NULL); > > kfree(chip); > > return 0; > > } > > > > Hi Takashi, > > The patch fixed the problem on 4.9-rc4. Didn't get a chance > to test it on 4.8.7. Thanks, this should fix for 4.8.x, too, as there hasn't been any significant changes in usb-audio recently. FWIW, below is the proper patch I'm going to queue. Takashi -- 8< -- From: Takashi Iwai Subject: [PATCH] ALSA: usb-audio: Fix use-after-free of usb_device at disconnect The usb-audio driver implements the deferred device disconnection for the device in use. In this mode, the disconnection callback returns immediately while the actual ALSA card object removal happens later when all files get closed. As Shuah reported, this code flow, however, leads to a use-after-free, detected by KASAN: BUG: KASAN: use-after-free in snd_usb_audio_free+0x134/0x160 [snd_usb_audio] at addr ffff8801c863ce10 Write of size 8 by task pulseaudio/2244 Call Trace: [] dump_stack+0x67/0x94 [] kasan_object_err+0x21/0x70 [] kasan_report_error+0x1fa/0x4e0 [] ? kasan_slab_free+0x87/0xb0 [] __asan_report_store8_noabort+0x43/0x50 [] ? snd_usb_audio_free+0x134/0x160 [snd_usb_audio] [] snd_usb_audio_free+0x134/0x160 [snd_usb_audio] [] snd_usb_audio_dev_free+0x31/0x40 [snd_usb_audio] [] __snd_device_free+0x12a/0x210 [] snd_device_free_all+0x85/0xd0 [] release_card_device+0x34/0x130 [] device_release+0x76/0x1e0 [] kobject_release+0x107/0x370 ..... Object at ffff8801c863cc80, in cache kmalloc-2048 size: 2048 Allocated: [] save_stack_trace+0x2b/0x50 [] save_stack+0x46/0xd0 [] kasan_kmalloc+0xad/0xe0 [] kmem_cache_alloc_trace+0xfa/0x240 [] usb_alloc_dev+0x57/0xc90 [] hub_event+0xf1d/0x35f0 .... Freed: [] save_stack_trace+0x2b/0x50 [] save_stack+0x46/0xd0 [] kasan_slab_free+0x71/0xb0 [] kfree+0xd9/0x280 [] usb_release_dev+0xde/0x110 [] device_release+0x76/0x1e0 .... It's the code trying to clear drvdata of the assigned usb_device where the usb_device itself was already released in usb_release_dev() after the disconnect callback. This patch fixes it by checking whether the code path is via the disconnect callback, i.e. chip->shutdown flag is set. Fixes: 79289e24194a ('ALSA: usb-audio: Refer to chip->usb_id for quirks...') Reported-and-tested-by: Shuah Khan Cc: # v4.6+ Signed-off-by: Takashi Iwai --- sound/usb/card.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sound/usb/card.c b/sound/usb/card.c index 9e5276d6dda0..2ddc034673a8 100644 --- a/sound/usb/card.c +++ b/sound/usb/card.c @@ -315,7 +315,8 @@ static int snd_usb_audio_free(struct snd_usb_audio *chip) snd_usb_endpoint_free(ep); mutex_destroy(&chip->mutex); - dev_set_drvdata(&chip->dev->dev, NULL); + if (!atomic_read(&chip->shutdown)) + dev_set_drvdata(&chip->dev->dev, NULL); kfree(chip); return 0; } -- 2.10.2