Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp701851ybz; Fri, 1 May 2020 07:04:32 -0700 (PDT) X-Google-Smtp-Source: APiQypLw2boxC65S57oC8q5eTrDDdh+SswSCZ7Pck7yeI2jEAACVXMxw+cuTrv2F8z8Xp8LZOZeM X-Received: by 2002:a17:906:d7a2:: with SMTP id pk2mr3461425ejb.272.1588341872451; Fri, 01 May 2020 07:04:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588341872; cv=none; d=google.com; s=arc-20160816; b=rQ0FzFRtPLcYlJr1QBxTuRm61AiJ5ViYLKunh2SzeRVGjm4y5zDZ54EbyPxxHD758V Xc7ZF8SugMAzk/ief7IBf3hNvgCwc8NhWFYndyTPziw9cOuIXaXw/aKFlBYKXBQwWFtJ OCGG6cO9KInZP0R/J9kTr5OQ0UJvJX294mNjsg6wCKB3EWy59QD8Tn4yWIPVibGBbpIt 9XYepT2XySnmWREMrsWNEkyInNoBXQBbWF8SXcoTnWWN80DkS+PHYSaqPhGBHW8bKvdf iSA3gFSgYyfnqq9nyKolkKWAPqdIorQBlWucObAFY5HDZP4DpIwLU9iNcYC2N7o/2tTJ 3M2g== 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=/mSGbrpMd0OAQf5fJx45T/EqibfvZ3dHqrhJgKZvzFY=; b=farbSj4Sc3AFkN2eRWdZMa98GEiQbgtMAxnXuZOGiaPQTsDTS9PIC70KC+rg6WXmct xGbvrK/aVduS7r+YJDhzsM2WtYFQTXjBcYf3bDlofU1ZeejnPr6YDo07QN72bkdVI31o av1nQxme/oL7KMKUyhYMRnnV+XhHCSdmUuWcw/kl3b6c3Fjy0ghU41ZK+UcJDFCPjzOt nGqo8B5s00I/FJ7Jnpo5dFKofYs89qhxfoKiA571ds0GnLqDB4ZO4JJS4/LKN6znDC7t 6hRuIfUxrC26D92mIsBbu7LEiFbC7C9K+kEn+fWaiyBOSgScg1otGwU8X42c8N2OHy0a i9hw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=K0fWZb29; 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 y8si1528237edw.356.2020.05.01.07.03.57; Fri, 01 May 2020 07:04:32 -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=K0fWZb29; 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 S1729825AbgEAN3q (ORCPT + 99 others); Fri, 1 May 2020 09:29:46 -0400 Received: from mail.kernel.org ([198.145.29.99]:53390 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729821AbgEAN3n (ORCPT ); Fri, 1 May 2020 09:29:43 -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 BFE64208DB; Fri, 1 May 2020 13:29:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1588339783; bh=fAeSOCyDZWHZwB1xapsOFAfJK2fFtww/H82lacETJ7U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=K0fWZb29nILJjx9d5xiu8AMRU9KL+WBCxiNTqBXSXe+Zeo5+zhn3v0UYWpj7sWHLT fhAK6xAjict5FAfIEi4sXyWabPbvvKcojm9nch1/CZndUIy99zoIhTlF2+rsEbcueH AexUb+5Q6RdIS5LUZAcBPYmm6UvQKwkE2FwQ9En8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Alan Stern , Kyungtae Kim Subject: [PATCH 4.9 36/80] USB: core: Fix free-while-in-use bug in the USB S-Glibrary Date: Fri, 1 May 2020 15:21:30 +0200 Message-Id: <20200501131525.462585701@linuxfoundation.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200501131513.810761598@linuxfoundation.org> References: <20200501131513.810761598@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 @@ -585,12 +585,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) { @@ -604,6 +605,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);