Received: by 2002:a25:23cc:0:0:0:0:0 with SMTP id j195csp661036ybj; Thu, 7 May 2020 04:49:15 -0700 (PDT) X-Google-Smtp-Source: APiQypKzKuRpCxyrA4tPXqOqyWO3f7EvJ2/QITa0BQ0ZHrSis27rMA6KGTVFAD0oRdlIpOtrux3S X-Received: by 2002:aa7:db0b:: with SMTP id t11mr10717110eds.304.1588852155296; Thu, 07 May 2020 04:49:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588852155; cv=none; d=google.com; s=arc-20160816; b=q58k5dfnT6zybeElPjHeTWm0dd4IXQ+Z9VpiKsg/y9TOH08/tN5YY3B/dowYZrFh9K /YAMtA6dLX+DBZbySfO+7+fiN2kIL1BxS60F4IR2IKoZjWp7Fr4U9htx2CIrPjUkLFt6 eVXIvwzMI6bKIHQvwHH+HucWK79LyROxDfjRbVzTpArYma8qbvT+/ns66ByFsOq6y/nr UYQ1sugy22nzzfJFr5cDt7Wa6iWwsAKrDcimfnBFO9jay3Qzf+8pTOVi0Xak+0siy4zQ Vqy1m0dIwHgzWdEdm3gLGEGg6sksDHYvJgWLnlq0C0nYgNpL/vtYluFSXuLQ0MgqgNBs djtw== 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=RPiWa/Y0G2CD5xz/A+s9kd0uz4AzySPe//Vkq/CFVxA=; b=G4IIngrtjaMrQA4PhH1PJ0+KtUG0S83NxGueEVHgtzhltV5ElBisbhWNjcE3X58OwM py2WfmoFviNMq+AisULF5xiNH0q5yLWnbCoGeyG9dC+IofigfJFc7h0CIt8VuSFnT5EB tVXgF+RgxAYx7IBjq8DXb2fcsxH58BVv2RxcoZnxe3K+QZcCG3Jhd4odGNbj7fSYY8r5 4rfVQfr80fBdcX+lUBcgG5EUNF10HsamsmUGGSoB8fgJAaGypEUeaosxum4sDjIAeuyx g+tlwyA+tpmMLEYFuRWuv/mOJ1Adt0wPUeeZkaK1EK6hNdBZZhp2Xo0u6j1VnJzz7y1F 4vNg== 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 f22si2742261ejx.209.2020.05.07.04.48.49; Thu, 07 May 2020 04:49:15 -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 S1726572AbgEGLo6 (ORCPT + 99 others); Thu, 7 May 2020 07:44:58 -0400 Received: from mx2.suse.de ([195.135.220.15]:37608 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725848AbgEGLo6 (ORCPT ); Thu, 7 May 2020 07:44:58 -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 54958ABE6; Thu, 7 May 2020 11:44:59 +0000 (UTC) Date: Thu, 07 May 2020 13:44:56 +0200 Message-ID: From: Takashi Iwai To: butt3rflyh4ck Cc: Greg Kroah-Hartman , Amadeusz SX2awiX4ski , , , , , 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:42:27 +0200, Takashi Iwai wrote: > > On Thu, 07 May 2020 12:36:14 +0200, > Takashi Iwai wrote: > > > > On Thu, 07 May 2020 12:27:41 +0200, > > Amadeusz SX2awiX4ski wrote: > > > > > > 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. > > ... and below is the revised patch. and yet more brush up, with a slightly better description. This seems working on my local machine, but let me know if something still goes wrong. thanks, Takashi -- 8< -- From: Takashi Iwai Subject: [PATCH v3] 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: the read/write functions unlock the runtime lock temporarily during copying form/to user-space, and that's the race window. This patch fixes the hole by introducing a reference counter for the runtime buffer read/write access and returns -EBUSY error when the resize is performed concurrently against read/write. Note that the ref count field is a simple integer instead of refcount_t here, since the all contexts accessing the buffer is basically protected with a spinlock, hence we need no expensive atomic ops. Also, note that this busy check is needed only against read / write functions, and not in receive/transmit callbacks; the race can happen only at the spinlock hole mentioned in the above, while the whole function is protected for receive / transmit callbacks. Reported-by: butt3rflyh4ck Cc: Link: https://lore.kernel.org/r/CAFcO6XMWpUVK_yzzCpp8_XP7+=oUpQvuBeCbMffEDkpe8jWrfg@mail.gmail.com Signed-off-by: Takashi Iwai --- v1->v2: Fix spinlock unbalance at error path of snd_rawmidi_kernel_read1() v2->v3: Remove superfluous ref in receive/transmit; more detailed patch description include/sound/rawmidi.h | 1 + sound/core/rawmidi.c | 31 +++++++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 4 deletions(-) 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..2a688b711a9a 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; @@ -1019,8 +1035,10 @@ static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream, long result = 0, count1; struct snd_rawmidi_runtime *runtime = substream->runtime; unsigned long appl_ptr; + int err = 0; 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) @@ -1039,16 +1057,19 @@ static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream, if (userbuf) { spin_unlock_irqrestore(&runtime->lock, flags); if (copy_to_user(userbuf + result, - runtime->buffer + appl_ptr, count1)) { - return result > 0 ? result : -EFAULT; - } + runtime->buffer + appl_ptr, count1)) + err = -EFAULT; spin_lock_irqsave(&runtime->lock, flags); + if (err) + goto out; } result += count1; count -= count1; } + out: + snd_rawmidi_buffer_unref(runtime); spin_unlock_irqrestore(&runtime->lock, flags); - return result; + return result > 0 ? result : err; } long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream, @@ -1342,6 +1363,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 +1395,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