From patchwork Fri Jan 15 21:44:37 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Takashi Iwai X-Patchwork-Id: 8044711 Return-Path: X-Original-To: patchwork-alsa-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 207549F744 for ; Fri, 15 Jan 2016 21:45:17 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id D0FA820465 for ; Fri, 15 Jan 2016 21:45:13 +0000 (UTC) Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) by mail.kernel.org (Postfix) with ESMTP id 0314E2050B for ; Fri, 15 Jan 2016 21:44:59 +0000 (UTC) Received: by alsa0.perex.cz (Postfix, from userid 1000) id 9E26A2606C2; Fri, 15 Jan 2016 22:44:57 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,NO_DNS_FOR_FROM, RCVD_IN_DNSWL_NONE,UNPARSEABLE_RELAY autolearn=no version=3.3.1 Received: from alsa0.perex.cz (localhost [127.0.0.1]) by alsa0.perex.cz (Postfix) with ESMTP id 599562604EF; Fri, 15 Jan 2016 22:44:49 +0100 (CET) X-Original-To: alsa-devel@alsa-project.org Delivered-To: alsa-devel@alsa-project.org Received: by alsa0.perex.cz (Postfix, from userid 1000) id 95A13260523; Fri, 15 Jan 2016 22:44:48 +0100 (CET) Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) by alsa0.perex.cz (Postfix) with ESMTP id 2945E2604DD for ; Fri, 15 Jan 2016 22:44:41 +0100 (CET) X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id E2DB2AD31; Fri, 15 Jan 2016 21:44:36 +0000 (UTC) Date: Fri, 15 Jan 2016 22:44:37 +0100 Message-ID: From: Takashi Iwai To: Dmitry Vyukov 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") Cc: alsa-devel@alsa-project.org, LKML , Kostya Serebryany , Eric Dumazet , Mark Brown , Alexander Potapenko , syzkaller , Sasha Levin Subject: Re: [alsa-devel] sound: use-after-free in snd_timer_interrupt X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: alsa-devel-bounces@alsa-project.org Sender: alsa-devel-bounces@alsa-project.org X-Virus-Scanned: ClamAV using ClamSMTP On Fri, 15 Jan 2016 22:22:46 +0100, Takashi Iwai wrote: > > On Fri, 15 Jan 2016 20:47:05 +0100, > Dmitry Vyukov wrote: > > > > On Fri, Jan 15, 2016 at 8:18 PM, Takashi Iwai wrote: > > > On Fri, 15 Jan 2016 20:13:11 +0100, > > > Dmitry Vyukov wrote: > > >> > > >> On Fri, Jan 15, 2016 at 3:38 PM, Dmitry Vyukov wrote: > > >> > On Fri, Jan 15, 2016 at 2:51 PM, Takashi Iwai wrote: > > >> >> On Fri, 15 Jan 2016 12:03:17 +0100, > > >> >> Dmitry Vyukov wrote: > > >> >>> > > >> >>> On Fri, Jan 15, 2016 at 12:00 PM, Takashi Iwai wrote: > > >> >>> > On Fri, 15 Jan 2016 09:06:10 +0100, > > >> >>> > Dmitry Vyukov wrote: > > >> >>> >> > > >> >>> >> On Thu, Jan 14, 2016 at 5:09 PM, Takashi Iwai wrote: > > >> >>> >> > On Wed, 13 Jan 2016 21:54:10 +0100, > > >> >>> >> > Takashi Iwai wrote: > > >> >>> >> >> > > >> >>> >> >> OK, then this might be a possible race at the current snd_timer_stop() > > >> >>> >> >> implementation. There is no sync action there, so the ISR might be > > >> >>> >> >> still alive after snd_timer_close() call. Or might be another race. > > >> >>> >> >> This pattern looks a bit different, as it's involved with hrtimer. > > >> >>> >> >> > > >> >>> >> >> I'll take a look at it tomorrow. > > >> >>> >> > > > >> >>> >> > I've audited the code today, but the open window doesn't look like > > >> >>> >> > what I expected. I found only some possible cases with slave timer > > >> >>> >> > instances. > > >> >>> >> > > > >> >>> >> > In anyway, below is a test fix patch. Since I couldn't reproduce the > > >> >>> >> > issue on my local machines, it's hard to say whether this covers the > > >> >>> >> > holes you fell. Let's see... > > >> >>> >> > > >> >>> >> > > >> >>> >> Hi Takashi, > > >> >>> >> > > >> >>> >> I would be interested to understand why other people can't reproduce > > >> >>> >> issues that I hit pretty reliably. > > >> >>> >> I suspect that it can be due to .config. Please try with the following > > >> >>> >> config values. > > >> >>> > > > >> >>> > I guess rather other config, e.g. the kernel debug options. > > >> >>> > I suppose you enabled KASAN and DEBUG_LIST. What else? > > >> >>> > > >> >>> I've attached my config (you will need to disable CONFIG_KCOV, it is > > >> >>> not upstreamed). > > >> >> > > >> >> Hm, that has lots of other drivers built-in... > > >> >> > > >> >>> >> I also start qemu with "-soundhw all" arg. > > >> >>> > > > >> >>> > OK, so you're testing with VM? This makes easier to recheck. > > >> >>> > > >> >>> Yes, I start qemu as: > > >> >>> > > >> >>> qemu-system-x86_64 -hda wheezy.img -net > > >> >>> user,host=10.0.2.10,hostfwd=tcp::10022-:22 -net nic -nographic -kernel > > >> >>> arch/x86/boot/bzImage -append "console=ttyS0 root=/dev/sda debug > > >> >>> earlyprintk=serial slub_debug=UZ" -enable-kvm -m 2G -numa > > >> >>> node,nodeid=0,cpus=0-1 -numa node,nodeid=1,cpus=2-3 -smp > > >> >>> sockets=2,cores=2,threads=1 -usb -usbdevice mouse -usbdevice tablet > > >> >>> -soundhw all > > >> >> > > >> >> And which test did trigger use-after-free, even with all previous > > >> >> patches? > > >> > > > >> > I will try to extract a new reproducer now. > > >> > > >> Ok, I does not seem to see any crashes except the timer hangs below. > > >> Let's consider all other bugs as fixed. I will report anything new > > >> that I see separately. > > > > > > OK, good to hear. > > > > > >> > Meanwhile, can you try to reproduce this one: > > >> > https://groups.google.com/forum/#!msg/syzkaller/bbtG9_h1ONU/CPLblMC6FAAJ > > >> > ? I run the program in a tight parallel loop. > > > > > > I could reproduce this after your suggestion with parallel runs. > > > > > > This seems specific to hrtimer. Possibly it's not about the snd-timer > > > core itself. Could you check whether this doesn't happen when > > > CONFIG_SND_HRTIMER isn't set? > > > > > > Does not happen without CONFIG_SND_HRTIMER. > > Do you mean that this is hrtimer bug? > > I guess rather it's a bug in snd-hrtimer driver. > Will check it later. The patch below *might* fix the issue. There was a deadlock problem and the current code has a weird workaround for it. I suspect it being the cause. If this works, I'll happily apply it before submitting the next pull request for 4.5. If not, I'll take a closer look at it in the next week :) thanks, Takashi diff --git a/sound/core/hrtimer.c b/sound/core/hrtimer.c index f845ecf7e172..a8027350520d 100644 --- a/sound/core/hrtimer.c +++ b/sound/core/hrtimer.c @@ -39,6 +39,7 @@ struct snd_hrtimer { struct snd_timer *timer; struct hrtimer hrt; atomic_t running; + bool cancel_pending; }; static enum hrtimer_restart snd_hrtimer_callback(struct hrtimer *hrt) @@ -62,7 +63,7 @@ static int snd_hrtimer_open(struct snd_timer *t) { struct snd_hrtimer *stime; - stime = kmalloc(sizeof(*stime), GFP_KERNEL); + stime = kzalloc(sizeof(*stime), GFP_KERNEL); if (!stime) return -ENOMEM; hrtimer_init(&stime->hrt, CLOCK_MONOTONIC, HRTIMER_MODE_REL); @@ -90,7 +91,10 @@ static int snd_hrtimer_start(struct snd_timer *t) struct snd_hrtimer *stime = t->private_data; atomic_set(&stime->running, 0); - hrtimer_cancel(&stime->hrt); + if (stime->cancel_pending) { + hrtimer_cancel(&stime->hrt); + stime->cancel_pending = false; + } hrtimer_start(&stime->hrt, ns_to_ktime(t->sticks * resolution), HRTIMER_MODE_REL); atomic_set(&stime->running, 1); @@ -101,6 +105,8 @@ static int snd_hrtimer_stop(struct snd_timer *t) { struct snd_hrtimer *stime = t->private_data; atomic_set(&stime->running, 0); + if (hrtimer_try_to_cancel(&stime->hrt) < 0) + stime->cancel_pending = true; return 0; }