Received: by 2002:a25:23cc:0:0:0:0:0 with SMTP id j195csp587353ybj; Thu, 7 May 2020 02:58:10 -0700 (PDT) X-Google-Smtp-Source: APiQypLTMcCewBjpJyh3FIMK1cOooaKxZBCnrA6HXzjdcKjZ5tMDjyjv2dOQZ7DkuxGvKb9RI7G8 X-Received: by 2002:a50:dac4:: with SMTP id s4mr4690640edj.277.1588845490183; Thu, 07 May 2020 02:58:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588845490; cv=none; d=google.com; s=arc-20160816; b=MtMRvXilqMDT9nUFKiiiKF0ngw492LNZrdDpJb3EN4NxpSJLJbDnUwQTS1ZYh/4EzT 2c9iJ8EL3lgRmPUeUQaGxTtM4/3TVq9bzpWWtjTCwFk5svKwDxI20q9BrJ959HtfCnUs w2eEga8gnGE+YjsZC1pAUcc/iifv8vrBRw9jHcqnCKXVzS548y7y9+rwd1ldO8lSgQ9f C1KVahhg/nwlBRjJC5uQuemD6EMUhZ4Z9QJBBTdwE8OLXfxS0JgnAcgNYXxB5GaL8jLb q2Hfn7/toN0EZPllAbuR5Qfdj362mAAE4+Tr7aoQ8JZdWEhM84Lic1kDlpFBtoaJMAjO L8pw== 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=Gda566Ol9wcd0IcqCiePYQCpbo3xueaj2Uk51nO8hbc=; b=uKH43vB3PsN/jR4c3EIIKElfFJE+KczumMEPgXkTzsGEPxBU8hUEU2mwrejIz7tbt1 lgwCQDzqeNllvrTPFu8svc9ej7I2rLDictg+4//MkXJKmAux+qn4ZfrljfMwCVbryUi9 k80k+hA3ZrVRHDe8J9pMQaW8MuMuU1nOnjWIRPhtEIwkWHeJr2DINSV9r6RpmBQMg1ZP YZhxPfV1eXUBFum+TI5TuRKz4/mXSYlTOTN6s7ZN2DwNjDnVsskWpKoysG3HCeq+IvKV okWpNutoaA0v+5f0ry5ZY8fHAoWJgSWjVIxv/yLcO/EENGbOJIwwozDvwg7CRnAkR5+T T+PQ== 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 j23si2775745ejm.361.2020.05.07.02.57.46; Thu, 07 May 2020 02:58:10 -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 S1725923AbgEGJ41 (ORCPT + 99 others); Thu, 7 May 2020 05:56:27 -0400 Received: from mx2.suse.de ([195.135.220.15]:33792 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725809AbgEGJ41 (ORCPT ); Thu, 7 May 2020 05:56:27 -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 6C14CB23A; Thu, 7 May 2020 09:56:27 +0000 (UTC) Date: Thu, 07 May 2020 11:56:22 +0200 Message-ID: From: Takashi Iwai To: Greg Kroah-Hartman Cc: butt3rflyh4ck , security@kernel.org, syzkaller , tiwai@suse.com, alsa-devel@alsa-project.org, linux-kernel@vger.kernel.org Subject: Re: KASAN: use-after-free Write in snd_rawmidi_kernel_write1 In-Reply-To: <20200507082302.GF1024567@kroah.com> 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 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 +++ b/include/sound/rawmidi.h @@ -61,6 +61,7 @@ struct snd_rawmidi_runtime { size_t avail_min; /* min avail for wakeup */ size_t avail; /* max used buffer for wakeup */ size_t xruns; /* over/underruns counter */ + int buffer_ref; /* buffer reference count */ /* misc */ spinlock_t lock; wait_queue_head_t sleep; diff --git a/sound/core/rawmidi.c b/sound/core/rawmidi.c index 20dd08e1f675..4185d9e81e3c 100644 --- a/sound/core/rawmidi.c +++ b/sound/core/rawmidi.c @@ -120,6 +120,17 @@ static void snd_rawmidi_input_event_work(struct work_struct *work) runtime->event(runtime->substream); } +/* buffer refcount management: call with runtime->lock held */ +static inline void snd_rawmidi_buffer_ref(struct snd_rawmidi_runtime *runtime) +{ + runtime->buffer_ref++; +} + +static inline void snd_rawmidi_buffer_unref(struct snd_rawmidi_runtime *runtime) +{ + runtime->buffer_ref--; +} + static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream *substream) { struct snd_rawmidi_runtime *runtime; @@ -669,6 +680,11 @@ static int resize_runtime_buffer(struct snd_rawmidi_runtime *runtime, if (!newbuf) return -ENOMEM; spin_lock_irq(&runtime->lock); + if (runtime->buffer_ref) { + spin_unlock_irq(&runtime->lock); + kvfree(newbuf); + return -EBUSY; + } oldbuf = runtime->buffer; runtime->buffer = newbuf; runtime->buffer_size = params->buffer_size; @@ -962,6 +978,7 @@ int snd_rawmidi_receive(struct snd_rawmidi_substream *substream, return -EINVAL; } spin_lock_irqsave(&runtime->lock, flags); + snd_rawmidi_buffer_ref(runtime); if (count == 1) { /* special case, faster code */ substream->bytes++; if (runtime->avail < runtime->buffer_size) { @@ -1006,6 +1023,7 @@ int snd_rawmidi_receive(struct snd_rawmidi_substream *substream, else if (snd_rawmidi_ready(substream)) wake_up(&runtime->sleep); } + snd_rawmidi_buffer_unref(runtime); spin_unlock_irqrestore(&runtime->lock, flags); return result; } @@ -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); if (copy_to_user(userbuf + result, runtime->buffer + appl_ptr, count1)) { - return result > 0 ? result : -EFAULT; + if (!result) + result = -EFAULT; + goto out; } spin_lock_irqsave(&runtime->lock, flags); } result += count1; count -= count1; } + out: + snd_rawmidi_buffer_unref(runtime); spin_unlock_irqrestore(&runtime->lock, flags); return result; } @@ -1156,6 +1179,7 @@ int __snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream, return -EINVAL; } result = 0; + snd_rawmidi_buffer_ref(runtime); if (runtime->avail >= runtime->buffer_size) { /* warning: lowlevel layer MUST trigger down the hardware */ goto __skip; @@ -1180,6 +1204,7 @@ int __snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream, } } __skip: + snd_rawmidi_buffer_unref(runtime); return result; } EXPORT_SYMBOL(__snd_rawmidi_transmit_peek); @@ -1342,6 +1367,7 @@ static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream, return -EAGAIN; } } + snd_rawmidi_buffer_ref(runtime); while (count > 0 && runtime->avail > 0) { count1 = runtime->buffer_size - runtime->appl_ptr; if (count1 > count) @@ -1373,6 +1399,7 @@ static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream, } __end: count1 = runtime->avail < runtime->buffer_size; + snd_rawmidi_buffer_unref(runtime); spin_unlock_irqrestore(&runtime->lock, flags); if (count1) snd_rawmidi_output_trigger(substream, 1); -- 2.25.0