Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp1368853ybp; Thu, 17 Oct 2019 11:44:33 -0700 (PDT) X-Google-Smtp-Source: APXvYqzK1RvopOEojEHODY5/1rifPI49VNwBXEWGqDvLyml+fyDLYr7v0gVeDeOXh2lxbBbOcGsj X-Received: by 2002:aa7:dd0f:: with SMTP id i15mr5630004edv.0.1571337873412; Thu, 17 Oct 2019 11:44:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571337873; cv=none; d=google.com; s=arc-20160816; b=KpSSZoWQnD757a/c2xCuH9mJ7dfIAY8H9m2HtPs64peX/hGgKWrrUi9yIfbrwVYXA4 XRjl8H08AEeFsgyVTKPFvi8hqldBYHVO4coT2TcG5hv+zCXPBkvGGqbCfezaXHBSFk8f bAnlNKeVkUoCUG9Af0DQSrbXuTSuBPpl6z84HTErkml208l73EaM9hmUhtbyjytAZBLu RfaQxppNhte1guh+6idxY0pOFe5JxzMKiTI39hBkW+O2VxT/vqtgZafVMvATSaoLW8yx 9kJjo5HK34rGd8ZUwaVSoD6dWd8Hl9RE6D8nWKDeYwvDI3vPBgvdIYIGQHpxEdFYUEdh 3AXg== 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=fAezXaiTf6+JTqXirB5qZEynEmQ9Ft+yYqwINYe1RUY=; b=zDcPWRGxO3xNTGmbAkeYtsOPSQtUbbyYulvmaV6QQR4hrMLtuVEbev1ZTpxWgzfKkl fQaz5lfx6IvEUKLoxG18XOc8VtrTxOzgqS5azx85XDuVao9CIv/f5+Kvmi6Y6ogZgRrc A5U3lbfWtIH5j6ZcjY3AXNyb1213VbJnpTVC15CsVYPfBegTnSchR55s6YFtrxWmHUXZ OXjzlSfzwF4YLVAM0ZTSHktThO6QX8Wn3TC4EIVZxE8d3dlf5qlryi7GO9ae1kQdKAg4 jDsWdKi4wgS++ocl6IP5nyRlRhttjp3o8ePRYuHOY2FGXVSZzM4uM3kcQFhz+SZVWz41 lAYA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=vRqhlxeX; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m10si1780062ejc.337.2019.10.17.11.44.09; Thu, 17 Oct 2019 11:44:33 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=vRqhlxeX; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2407226AbfJPWKg (ORCPT + 99 others); Wed, 16 Oct 2019 18:10:36 -0400 Received: from mail.kernel.org ([198.145.29.99]:49552 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388823AbfJPV5K (ORCPT ); Wed, 16 Oct 2019 17:57:10 -0400 Received: from localhost (unknown [192.55.54.58]) (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 8A72120872; Wed, 16 Oct 2019 21:57:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1571263029; bh=Do8Ps45Db9i2Kq5cWrhLAuBtarWPj5Gh4AhA8R5NR3c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=vRqhlxeX/fbiZtNZKYBwT9B3YHxg+vo3HeGev6jd7+DqP37/WpFG+t0ocsSqH3foz CsV+hdrGMCDlxKlqLLx6mL7d9BFk6Q00NH7+I+J6f2PV1EbQOjE4Zbuki5B+tIZ34V Wax1ZgRK0co7SQYb3nXyPTKnfzEWpkOG3hjdwuIc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Johan Hovold Subject: [PATCH 4.19 30/81] USB: usblcd: fix I/O after disconnect Date: Wed, 16 Oct 2019 14:50:41 -0700 Message-Id: <20191016214832.557502371@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191016214805.727399379@linuxfoundation.org> References: <20191016214805.727399379@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: Johan Hovold commit eb7f5a490c5edfe8126f64bc58b9ba2edef0a425 upstream. Make sure to stop all I/O on disconnect by adding a disconnected flag which is used to prevent new I/O from being started and by stopping all ongoing I/O before returning. This also fixes a potential use-after-free on driver unbind in case the driver data is freed before the completion handler has run. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Cc: stable # 7bbe990c989e Signed-off-by: Johan Hovold Link: https://lore.kernel.org/r/20190926091228.24634-7-johan@kernel.org Signed-off-by: Greg Kroah-Hartman --- drivers/usb/misc/usblcd.c | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) --- a/drivers/usb/misc/usblcd.c +++ b/drivers/usb/misc/usblcd.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -57,6 +58,8 @@ struct usb_lcd { using up all RAM */ struct usb_anchor submitted; /* URBs to wait for before suspend */ + struct rw_semaphore io_rwsem; + unsigned long disconnected:1; }; #define to_lcd_dev(d) container_of(d, struct usb_lcd, kref) @@ -142,6 +145,13 @@ static ssize_t lcd_read(struct file *fil dev = file->private_data; + down_read(&dev->io_rwsem); + + if (dev->disconnected) { + retval = -ENODEV; + goto out_up_io; + } + /* do a blocking bulk read to get data from the device */ retval = usb_bulk_msg(dev->udev, usb_rcvbulkpipe(dev->udev, @@ -158,6 +168,9 @@ static ssize_t lcd_read(struct file *fil retval = bytes_read; } +out_up_io: + up_read(&dev->io_rwsem); + return retval; } @@ -237,11 +250,18 @@ static ssize_t lcd_write(struct file *fi if (r < 0) return -EINTR; + down_read(&dev->io_rwsem); + + if (dev->disconnected) { + retval = -ENODEV; + goto err_up_io; + } + /* create a urb, and a buffer for it, and copy the data to the urb */ urb = usb_alloc_urb(0, GFP_KERNEL); if (!urb) { retval = -ENOMEM; - goto err_no_buf; + goto err_up_io; } buf = usb_alloc_coherent(dev->udev, count, GFP_KERNEL, @@ -278,6 +298,7 @@ static ssize_t lcd_write(struct file *fi the USB core will eventually free it entirely */ usb_free_urb(urb); + up_read(&dev->io_rwsem); exit: return count; error_unanchor: @@ -285,7 +306,8 @@ error_unanchor: error: usb_free_coherent(dev->udev, count, buf, urb->transfer_dma); usb_free_urb(urb); -err_no_buf: +err_up_io: + up_read(&dev->io_rwsem); up(&dev->limit_sem); return retval; } @@ -325,6 +347,7 @@ static int lcd_probe(struct usb_interfac kref_init(&dev->kref); sema_init(&dev->limit_sem, USB_LCD_CONCURRENT_WRITES); + init_rwsem(&dev->io_rwsem); init_usb_anchor(&dev->submitted); dev->udev = usb_get_dev(interface_to_usbdev(interface)); @@ -422,6 +445,12 @@ static void lcd_disconnect(struct usb_in /* give back our minor */ usb_deregister_dev(interface, &lcd_class); + down_write(&dev->io_rwsem); + dev->disconnected = 1; + up_write(&dev->io_rwsem); + + usb_kill_anchored_urbs(&dev->submitted); + /* decrement our usage count */ kref_put(&dev->kref, lcd_delete);