Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp4456215ybz; Tue, 28 Apr 2020 11:36:00 -0700 (PDT) X-Google-Smtp-Source: APiQypIb/+VUwBV6szNRheBBDK9XNpTfC0huJioGvRlAOwnMpUZsM1O59zRK69HZqqnCgCLBUmCH X-Received: by 2002:a17:906:5958:: with SMTP id g24mr26853909ejr.58.1588098960758; Tue, 28 Apr 2020 11:36:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588098960; cv=none; d=google.com; s=arc-20160816; b=PiKpsjL+10JVGRlR//QxR+J9knNxbxQBZ9i5bHxt9lZOvc9+dnPJIRUxo9QXrOipDo m09bzd3odREt9vdoMkFYenr1ZufsARIruml35i0Nwsn78NCvRg7RRa1JU4jIG9ufbyKS +B6aOf1X4P7CPLNQ72gSH4IkPIO+6y0Tu67OSLcaX/Qypc5NEA+2BTc2u5gMqcgjjfj6 SMmnQcsJekqHSSNcEemgZ+VNZmbWa+ZRCkDPjG2BKs5fkE1Q9LSgPkxRNJyP77/I1wOq VXHKhcG9UPDz0MuiHVOLwbPVgSkhHGR7BaBQoiWZ/IxE5JB874p35+9ViYsjbd9dJF7b 6aAw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=mP12okAK3babgxdPWrBaS51sYvkGHhoh+TIAcMGwewA=; b=p7UWwlS6qYSOX4uSLxCBFRXq9mC7acdUc6sA1qXSOJYRXrW4zuL0B7bVvezo9LOxl5 gJMlooK4x85zXaA+Hh3MzpNDp2H4CFc0LFjtDBTjUUf/gvbNuLd99i69vwFABPuC9Unx qZiT5otGFem3AuTzlgGa8GkMELgiFCJKwnLHIxyqvs4ShB0Hz2d6JSQfo1R3u9RqnVoB Idq7OFIisA5U3VHSHDmH/gOqTnFPLa2WNTtWCtRoP2sUw8AX7YfoeANDswwTc5NWpXku VPMYpI8IVyUuIAB6OX3tKOge3cLFHbeIbNcLkbP67HIVRRryBMqIiszXstbBOoNMt0OL BkSg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=l3E5yr1v; 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 dk14si2300486ejb.124.2020.04.28.11.35.36; Tue, 28 Apr 2020 11:36:00 -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; dkim=pass header.i=@kernel.org header.s=default header.b=l3E5yr1v; 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 S1728771AbgD1SbG (ORCPT + 99 others); Tue, 28 Apr 2020 14:31:06 -0400 Received: from mail.kernel.org ([198.145.29.99]:46144 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729579AbgD1Sa5 (ORCPT ); Tue, 28 Apr 2020 14:30:57 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id C1EE021707; Tue, 28 Apr 2020 18:30:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1588098656; bh=btMmoRmbm0aFeh7V8QPWVWglOxt22/y9OzCifBjnb+c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=l3E5yr1vtajT14zdGBpy2ROuRXl7F3d5mAhvbeb+b7FEsBJQzZjOsLqwp7rQACYsl u19Zp4E6qYY0897X01FHmIQ1++EtBPTP39oX9TLv+SZkFtd+vZJYpi9N9rUEDlyQKh eIKDNGa8vbh8kB1y9nYec+r0/AkLiXpa4sxudDJw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Alan Stern , Kyungtae Kim Subject: [PATCH 5.6 093/167] USB: core: Fix free-while-in-use bug in the USB S-Glibrary Date: Tue, 28 Apr 2020 20:24:29 +0200 Message-Id: <20200428182236.786952756@linuxfoundation.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200428182225.451225420@linuxfoundation.org> References: <20200428182225.451225420@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Alan Stern commit 056ad39ee9253873522f6469c3364964a322912b upstream. FuzzUSB (a variant of syzkaller) found a free-while-still-in-use bug in the USB scatter-gather library: BUG: KASAN: use-after-free in atomic_read include/asm-generic/atomic-instrumented.h:26 [inline] BUG: KASAN: use-after-free in usb_hcd_unlink_urb+0x5f/0x170 drivers/usb/core/hcd.c:1607 Read of size 4 at addr ffff888065379610 by task kworker/u4:1/27 CPU: 1 PID: 27 Comm: kworker/u4:1 Not tainted 5.5.11 #2 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.10.2-1ubuntu1 04/01/2014 Workqueue: scsi_tmf_2 scmd_eh_abort_handler Call Trace: __dump_stack lib/dump_stack.c:77 [inline] dump_stack+0xce/0x128 lib/dump_stack.c:118 print_address_description.constprop.4+0x21/0x3c0 mm/kasan/report.c:374 __kasan_report+0x153/0x1cb mm/kasan/report.c:506 kasan_report+0x12/0x20 mm/kasan/common.c:639 check_memory_region_inline mm/kasan/generic.c:185 [inline] check_memory_region+0x152/0x1b0 mm/kasan/generic.c:192 __kasan_check_read+0x11/0x20 mm/kasan/common.c:95 atomic_read include/asm-generic/atomic-instrumented.h:26 [inline] usb_hcd_unlink_urb+0x5f/0x170 drivers/usb/core/hcd.c:1607 usb_unlink_urb+0x72/0xb0 drivers/usb/core/urb.c:657 usb_sg_cancel+0x14e/0x290 drivers/usb/core/message.c:602 usb_stor_stop_transport+0x5e/0xa0 drivers/usb/storage/transport.c:937 This bug occurs when cancellation of the S-G transfer races with transfer completion. When that happens, usb_sg_cancel() may continue to access the transfer's URBs after usb_sg_wait() has freed them. The bug is caused by the fact that usb_sg_cancel() does not take any sort of reference to the transfer, and so there is nothing to prevent the URBs from being deallocated while the routine is trying to use them. The fix is to take such a reference by incrementing the transfer's io->count field while the cancellation is in progres and decrementing it afterward. The transfer's URBs are not deallocated until io->complete is triggered, which happens when io->count reaches zero. Signed-off-by: Alan Stern Reported-and-tested-by: Kyungtae Kim CC: Link: https://lore.kernel.org/r/Pine.LNX.4.44L0.2003281615140.14837-100000@netrider.rowland.org Signed-off-by: Greg Kroah-Hartman --- drivers/usb/core/message.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) --- a/drivers/usb/core/message.c +++ b/drivers/usb/core/message.c @@ -588,12 +588,13 @@ void usb_sg_cancel(struct usb_sg_request int i, retval; spin_lock_irqsave(&io->lock, flags); - if (io->status) { + if (io->status || io->count == 0) { spin_unlock_irqrestore(&io->lock, flags); return; } /* shut everything down */ io->status = -ECONNRESET; + io->count++; /* Keep the request alive until we're done */ spin_unlock_irqrestore(&io->lock, flags); for (i = io->entries - 1; i >= 0; --i) { @@ -607,6 +608,12 @@ void usb_sg_cancel(struct usb_sg_request dev_warn(&io->dev->dev, "%s, unlink --> %d\n", __func__, retval); } + + spin_lock_irqsave(&io->lock, flags); + io->count--; + if (!io->count) + complete(&io->complete); + spin_unlock_irqrestore(&io->lock, flags); } EXPORT_SYMBOL_GPL(usb_sg_cancel);