Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp668447ybz; Fri, 1 May 2020 06:30:44 -0700 (PDT) X-Google-Smtp-Source: APiQypI3UBQ+2gGUw3Ix3ZXy2Te1QV3o7uU4+Yy3cpebQdF+zd4GDasJ+bOyvtnwBYtJO9OydJEp X-Received: by 2002:a17:906:78c:: with SMTP id l12mr2969460ejc.189.1588339844366; Fri, 01 May 2020 06:30:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588339844; cv=none; d=google.com; s=arc-20160816; b=E0KxD3vH6ku+j4ffiWw2l56KwyBR/hCjvDEiX5X2XY1PiOIjN2/xxU84/iCseX5BN7 DRaVyrnHTDgXoAyIgKTLpnbeMSdsOMzd/NSG+P74cOFVeLYda84Y51wIdMwD9YwLPM+A IdOa50Qp8O8MKyYHTAokgP1KnboVC4l7sDCe1fZBo2N+woRZDAao0ulCztKnaHX50WIj 4XZPAT6qq2U7q9h24/4/RInRk3nmQHaRhmLGruq+y8euvkCNAiAvrBkYZrCieexGni5s Ce9DitCHTswJWFAO9ZAx5a6Wk5LnFyMOVPt05JLJ0D3VGu9o1yplGUlHvr0G6gGVVgRA EcOQ== 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=2k+z6i2ajtTCt0F51gQBpmq9FYNOGWo17jHX5df+PF8=; b=sXdiRhcEAJLl57zOAM2F/ced2MNz6V4G1FVbzq+yl3k/KQnD720mak/kYiuR5zQYV5 H+aZ/QC4sAJXhD0BXcVr4z+jbG+78oRguWotnOTEDaFW3mM63vamZvyiR5J3FVQciBlu Kr4FaMMshvpyEjTzkUG1RcRKQSczwTadzW+q4ROZc0n8w2afcRLfYWSj7ssaCDwnGqRt nMoiU4zZckC52aCbKAMqPQDHZUxD27iADDuBP2K0P7iQLdKDwqOL7LHgUeIzHGMGdx9R 56IX4ulYJWhlrgcxfw/D6P43e38sxJUulbPss9IPi5K4XoxE/Iv7sD5Bs8a9MEm2hCZM WemA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=FpYpwSuU; 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 cz15si1803861edb.381.2020.05.01.06.30.21; Fri, 01 May 2020 06:30:44 -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=FpYpwSuU; 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 S1729222AbgEAN0T (ORCPT + 99 others); Fri, 1 May 2020 09:26:19 -0400 Received: from mail.kernel.org ([198.145.29.99]:47802 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728900AbgEAN0R (ORCPT ); Fri, 1 May 2020 09:26:17 -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 DEAD620757; Fri, 1 May 2020 13:26:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1588339576; bh=R6UamDJw4+3i3yj/3AJ3ccIrqCqsDtmzcI8grnRycKY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FpYpwSuUeyUVwwHOcwRWvAzo1pDe3k/2fB9IlqeJSfAe0+85jhDhgxVzpJjO3eIJc 6Oh+YLLTBASRIoChRttlr5VuYBf5Y/QkO0bbW2clvj2G+AmA5tISYRPUlKKrVBJWmq bmuxtwFrfGXD56LgpN6E9nH/O8PGt7GJntdCNzkY= 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.4 34/70] USB: core: Fix free-while-in-use bug in the USB S-Glibrary Date: Fri, 1 May 2020 15:21:22 +0200 Message-Id: <20200501131524.704761628@linuxfoundation.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200501131513.302599262@linuxfoundation.org> References: <20200501131513.302599262@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 @@ -584,12 +584,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) { @@ -603,6 +604,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);