Received: by 2002:a25:23cc:0:0:0:0:0 with SMTP id j195csp614176ybj; Thu, 7 May 2020 03:38:28 -0700 (PDT) X-Google-Smtp-Source: APiQypL8/kG8TGtmjEImpSXnBa2BHkGxKN/8XKqKK1JWYVKdQ9J7T3rw2cHYCPHQhahmVYPRIS5K X-Received: by 2002:a05:6402:c84:: with SMTP id cm4mr10592843edb.316.1588847908006; Thu, 07 May 2020 03:38:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588847907; cv=none; d=google.com; s=arc-20160816; b=oEP0guhT2QZZgOTJbmNFQdjlD3GEFlaaF7ycwPXnl2qrO2zJItA926g069dzaT0uKy 6sy8MXwJHUHjbJ5Q5FXnr2ouzdcYNo6EJrgs/+SerbiF4cdij1q0mqRpgB89y5SHidVt SI/Ygi2pOm5lZmWPa34UkGDjx5sNSPkWMbTRXpFSh2m4MBbKxHvR2+2bPU+cfoEcrSl8 olEnJ8Mu+YOF5+++fEPR/f6r/fX7CJ0p5PhUnx+xlAjqqQCmDKcPGjqoaFy9JVHlxWdQ iX8l9hKn1/8e5cFz6QI+zVPJin9SjLwrSOZdQ4Som3kYHZQeAozzV6Vxt85DT0ee6lTJ 5eCA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:subject:cc:to:from:message-id:date; bh=vlAw4McQ0E4gHRRjU6tEa+EVHF6GXSwpBYzvv2tXxak=; b=ag7+ouOBwmiix9bXV2ph3TIIlxXaJok1U4MlUb/OzfeSBEB4dqhh9k1XLO1WuII/2V uBGbKBYQajnWB54jvbCqVcipupwvgoKADLLK8EdkRuGlLU85CRPYcrsKsNXw5cjnZnhO UiKWbFYxMwwyekCQs2jqPWF1M2QRpwkz0bxotiu8aVAYLg5XidATcdKV9j2wWQZWF0PR DYqfdXPy0Sp8rUv1wCw6z89dUBHX2I3hjplh0feGhlEwNCyM2KLmyjS6t730LA2J7zMC uGqe53kLBpN8F7605JaL74zAo3FQkdkjR/cEqQP1J9EJMghghHvPcVyZJdQEt2veOTH5 BHmw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n12si3029778eds.584.2020.05.07.03.38.05; Thu, 07 May 2020 03:38:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725947AbgEGKgR (ORCPT + 99 others); Thu, 7 May 2020 06:36:17 -0400 Received: from mx2.suse.de ([195.135.220.15]:57824 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725848AbgEGKgQ (ORCPT ); Thu, 7 May 2020 06:36:16 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 3F974AE30; Thu, 7 May 2020 10:36:18 +0000 (UTC) Date: Thu, 07 May 2020 12:36:14 +0200 Message-ID: From: Takashi Iwai To: Amadeusz SX2awiX4ski Cc: Greg Kroah-Hartman , security@kernel.org, alsa-devel@alsa-project.org, butt3rflyh4ck , tiwai@suse.com, linux-kernel@vger.kernel.org, syzkaller Subject: Re: KASAN: use-after-free Write in snd_rawmidi_kernel_write1 In-Reply-To: References: <20200507082302.GF1024567@kroah.com> 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/25.3 (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 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 07 May 2020 12:27:41 +0200, Amadeusz SX2awiX4ski wrote: > > > > On 5/7/2020 11:56 AM, Takashi Iwai wrote: > > On Thu, 07 May 2020 10:23:02 +0200, > > Greg Kroah-Hartman wrote: > >> > >> On Thu, May 07, 2020 at 04:04:25PM +0800, butt3rflyh4ck wrote: > >>> I report a bug (in linux-5.7-rc1) found by syzkaller. > >>> > >>> kernel config: https://github.com/butterflyhack/syzkaller-fuzz/blob/master/v5.7.0-rc1.config > >>> reproducer: https://github.com/butterflyhack/syzkaller-fuzz/blob/master/repro.cprog > >>> > >>> I test the reproducer in linux-5.7-rc4 and crash too. > >> > >> Great, care to create a fix for this and send it to the proper > >> maintainers? That's the best way to get it fixed, otherwise it just > >> goes in the file with the rest of the syzbot reports we are burried > >> under. > > > > Don't worry, I already prepared a fix patch below :) > > > > > > thanks, > > > > Takashi > > > > -- 8< -- > > From: Takashi Iwai > > Subject: [PATCH] ALSA: rawmidi: Fix racy buffer resize under concurrent > > accesses > > > > The rawmidi core allows user to resize the runtime buffer via ioctl, > > and this may lead to UAF when performed during concurrent reads or > > writes. > > > > This patch fixes the race by introducing a reference counter for the > > runtime buffer access and returns -EBUSY error when the resize is > > performed concurrently. > > > > Reported-by: butt3rflyh4ck > > Cc: > > Link: https://lore.kernel.org/r/CAFcO6XMWpUVK_yzzCpp8_XP7+=oUpQvuBeCbMffEDkpe8jWrfg@mail.gmail.com > > Signed-off-by: Takashi Iwai > > --- > > include/sound/rawmidi.h | 1 + > > sound/core/rawmidi.c | 29 ++++++++++++++++++++++++++++- > > 2 files changed, 29 insertions(+), 1 deletion(-) > > > > diff --git a/include/sound/rawmidi.h b/include/sound/rawmidi.h > > index a36b7227a15a..334842daa904 100644 > > --- a/include/sound/rawmidi.h > > (...) > > > @@ -1021,6 +1039,7 @@ static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream, > > unsigned long appl_ptr; > > spin_lock_irqsave(&runtime->lock, flags); > > + snd_rawmidi_buffer_ref(runtime); > > while (count > 0 && runtime->avail) { > > count1 = runtime->buffer_size - runtime->appl_ptr; > > if (count1 > count) > > @@ -1040,13 +1059,17 @@ static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream, > > spin_unlock_irqrestore(&runtime->lock, flags); > First unlock > > if (copy_to_user(userbuf + result, > > runtime->buffer + appl_ptr, count1)) { > > - return result > 0 ? result : -EFAULT; > > + if (!result) > > + result = -EFAULT; > > + goto out; > > goto -> Second unlock > > } > > spin_lock_irqsave(&runtime->lock, flags); > > } > > result += count1; > > count -= count1; > > } > > + out: > > + snd_rawmidi_buffer_unref(runtime); > > spin_unlock_irqrestore(&runtime->lock, flags); > Second unlock > > return result; > > } > > So if I follow this correctly, you call spin_unlock_irqrestore twice > in case of error? Erm no, this is obviously wrong. The error path needs re-lock. Will respin the fix. thanks, Takashi