Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp3950901pxb; Tue, 19 Apr 2022 13:21:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyqyZQQJIhPWrNe6PyeJXes+azvL+hndqmBcmb0nneZ7afan/+ClaUxTHahdDkjbWWarO1S X-Received: by 2002:a17:906:1e94:b0:6b9:6fcc:53fd with SMTP id e20-20020a1709061e9400b006b96fcc53fdmr14976573ejj.450.1650399699277; Tue, 19 Apr 2022 13:21:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650399699; cv=none; d=google.com; s=arc-20160816; b=nFjgqeGnFmj8Pgp3cSG0CizF6IcCfz8Ky6u0HhSNd7B9s9ElGGaPspmRQbke91Bg6s 1Qtd5WoG4PMHcQO5hSoEn5hp+JZnEloaienSFV/JoBU9bLJ+e1lKFIUkr3pAEnM25rqC Wt2pQ8PgoXq8EjcrWacCj+DgCjn4yHd80YnJDSlYyRyOBUjWCc+xtIuScSy0lkjksR7d ASh0Lo8p8G9b8TYDqWGop/DenhGI7Dfq5wP+n8M5JbT6a7BttLWQgCre17BdS91MN9f/ MUfVSXxcQaVL7t3cPZrXHFQS40K06ofojy8d8f86CVyvaE3nEEOh+5RwfxKcdCgAUOx2 4MVQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:references:in-reply-to :subject:cc:to:from:message-id:date:dkim-signature:dkim-signature; bh=7tmu8xW2ZdVQ7G42JQsUqYarrqw3vZczKi0AAynAqgs=; b=fo0ztVC2RVOHYpsoETFtVE0arQM4ou17DmVTkM7lpZlndhnKCwHCK3A/NhEHiTLvim clAXbKrIK8P4T5YG5tGD6q7ypq+qYQNXCykhVYpJQ/kbZZqv8KWsn0NjGh2vvS2zGhPl StSn0YtYZUvujGT5NBYYUzQrBAzMBiPXUQPOuE5PipsM2OEpcyJXIFnQC/KiIRb72GOv 69H25FwQMhX8a898pB3pkDkabBHOErHQY7IxWO7lRaWFe2V/2LJQuvjIf6i5uJXOkMr6 GeuRjZoQLC2rkhksvOOJsexFPYTYWW1roWb0wwOWAfqr2K3SzTWSkcgMjeRg75mFqOcp R2tg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=Hi3ztoMt; dkim=neutral (no key) header.i=@suse.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y14-20020a056402440e00b0041d96f8d4c8si10802688eda.432.2022.04.19.13.21.14; Tue, 19 Apr 2022 13:21:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=Hi3ztoMt; dkim=neutral (no key) header.i=@suse.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350263AbiDSJEb (ORCPT + 99 others); Tue, 19 Apr 2022 05:04:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60730 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241296AbiDSJEa (ORCPT ); Tue, 19 Apr 2022 05:04:30 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B47B821265 for ; Tue, 19 Apr 2022 02:01:48 -0700 (PDT) Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id 742BC210F1; Tue, 19 Apr 2022 09:01:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1650358907; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=7tmu8xW2ZdVQ7G42JQsUqYarrqw3vZczKi0AAynAqgs=; b=Hi3ztoMt7VAfNp7D2N610rfR3Z9oRO4g0B4pGiE+a02MGcEtQNagvYOe4XxP5BRMEvgFEt lZrwyMotHsvAub3vGNoBJnHZvZjLs/eIsnehJWYvnCl6ggGHMvRqArbBJk6v+3QAVosZB7 3S7QBRE0XZuyCvUGeL61yLf2jvBf+L8= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1650358907; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=7tmu8xW2ZdVQ7G42JQsUqYarrqw3vZczKi0AAynAqgs=; b=8guWCu+Ml7aWrhiMjov3KuKxQRUN66aMC/mN7a1pAijAExCi0UGVQo6ofVhvZkFti//Gjo kIFWUeH7O0HMCeDA== Received: from alsa1.suse.de (alsa1.suse.de [10.160.4.42]) by relay2.suse.de (Postfix) with ESMTP id 50E982C146; Tue, 19 Apr 2022 09:01:47 +0000 (UTC) Date: Tue, 19 Apr 2022 11:01:47 +0200 Message-ID: From: Takashi Iwai To: syzbot Cc: alsa-devel@alsa-project.org, coding@diwic.se, colin.king@intel.com, linux-kernel@vger.kernel.org, perex@perex.cz, syzkaller-bugs@googlegroups.com, tiwai@suse.com Subject: Re: [syzbot] KASAN: use-after-free Read in __snd_rawmidi_transmit_peek In-Reply-To: <00000000000011555605dceaff03@google.com> References: <00000000000011555605dceaff03@google.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 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SORTED_RECIPS, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 18 Apr 2022 12:12:23 +0200, syzbot wrote: > > Hello, > > syzbot found the following issue on: > > HEAD commit: a19944809fe9 Merge tag 'hardening-v5.18-rc3' of git://git... > git tree: upstream > console output: https://syzkaller.appspot.com/x/log.txt?x=16a40ae0f00000 > kernel config: https://syzkaller.appspot.com/x/.config?x=eb177500e563582f > dashboard link: https://syzkaller.appspot.com/bug?extid=70e777a39907d6d5fd0a > compiler: gcc (Debian 10.2.1-6) 10.2.1 20210110, GNU ld (GNU Binutils for Debian) 2.35.2 > syz repro: https://syzkaller.appspot.com/x/repro.syz?x=1590dfa8f00000 > > IMPORTANT: if you fix the issue, please add the following tag to the commit: > Reported-by: syzbot+70e777a39907d6d5fd0a@syzkaller.appspotmail.com > > ================================================================== > BUG: KASAN: use-after-free in __snd_rawmidi_transmit_peek+0x261/0x360 sound/core/rawmidi.c:1286 Looks like a leftover work. The fix patch is below. Takashi -- 8< -- From: Takashi Iwai Subject: [PATCH] ALSA: usb-audio: Fix potential use-after-free at releasing MIDI streams As recently spotted by syzbot, the USB MIDI code may leave a work that handles the pending output queue even after a stream gets released (either internally via sequencer or via device files), which may lead to use-after-free. We have already a proper drop of pending URBs but this is executed only at the device disconnection. This patch moves the URB clean-up code into both the code path that is called at releasing, namely, snd_usb_midi_in_endpoint_delete() and snd_usb_midi_out_endpoint_clear() functions, so that the pending URBs and work are cleared properly before releasing the resources. Reported-by: syzbot+70e777a39907d6d5fd0a@syzkaller.appspotmail.com Cc: Link: https://lore.kernel.org/r/00000000000011555605dceaff03@google.com Signed-off-by: Takashi Iwai --- sound/usb/midi.c | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/sound/usb/midi.c b/sound/usb/midi.c index 2c01649c70f6..8e5281f7f1fc 100644 --- a/sound/usb/midi.c +++ b/sound/usb/midi.c @@ -1247,6 +1247,10 @@ static void snd_usbmidi_in_endpoint_delete(struct snd_usb_midi_in_endpoint *ep) { unsigned int i; + for (i = 0; i < INPUT_URBS; ++i) + if (ep->urbs[i]) + usb_kill_urb(ep->urbs[i]); + for (i = 0; i < INPUT_URBS; ++i) if (ep->urbs[i]) free_urb_and_buffer(ep->umidi, ep->urbs[i], @@ -1327,6 +1331,18 @@ static void snd_usbmidi_out_endpoint_clear(struct snd_usb_midi_out_endpoint *ep) { unsigned int i; + cancel_work_sync(&ep->work); + for (i = 0; i < OUTPUT_URBS; ++i) + if (ep->urbs[i].urb) + usb_kill_urb(ep->urbs[i].urb); + if (ep->umidi->usb_protocol_ops->finish_out_endpoint) + ep->umidi->usb_protocol_ops->finish_out_endpoint(ep); + ep->active_urbs = 0; + if (ep->drain_urbs) { + ep->drain_urbs = 0; + wake_up(&ep->drain_wait); + } + for (i = 0; i < OUTPUT_URBS; ++i) if (ep->urbs[i].urb) { free_urb_and_buffer(ep->umidi, ep->urbs[i].urb, @@ -1469,7 +1485,7 @@ static void snd_usbmidi_free(struct snd_usb_midi *umidi) void snd_usbmidi_disconnect(struct list_head *p) { struct snd_usb_midi *umidi; - unsigned int i, j; + unsigned int i; umidi = list_entry(p, struct snd_usb_midi, list); /* @@ -1487,22 +1503,6 @@ void snd_usbmidi_disconnect(struct list_head *p) for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { struct snd_usb_midi_endpoint *ep = &umidi->endpoints[i]; - if (ep->out) - cancel_work_sync(&ep->out->work); - if (ep->out) { - for (j = 0; j < OUTPUT_URBS; ++j) - usb_kill_urb(ep->out->urbs[j].urb); - if (umidi->usb_protocol_ops->finish_out_endpoint) - umidi->usb_protocol_ops->finish_out_endpoint(ep->out); - ep->out->active_urbs = 0; - if (ep->out->drain_urbs) { - ep->out->drain_urbs = 0; - wake_up(&ep->out->drain_wait); - } - } - if (ep->in) - for (j = 0; j < INPUT_URBS; ++j) - usb_kill_urb(ep->in->urbs[j]); /* free endpoints here; later call can result in Oops */ if (ep->out) snd_usbmidi_out_endpoint_clear(ep->out); -- 2.31.1