Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp1354117ybp; Thu, 17 Oct 2019 11:30:40 -0700 (PDT) X-Google-Smtp-Source: APXvYqw8kpoJLyn8eaU//0y8e8+6vlkRT9vZ7Wx9rT8GYGhawgyOmIup3zzuHR2g8A9kfvF4DJMK X-Received: by 2002:a17:906:519:: with SMTP id j25mr4798380eja.65.1571337040274; Thu, 17 Oct 2019 11:30:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571337040; cv=none; d=google.com; s=arc-20160816; b=toOyUswK/55lK0ysPhh7twwaYmxwdOl7MVOq34EreiyONGKcmUmnrLXmEzJMrS5miN 8kZyRkAgrLqiP0mbbpt/3NVyNnhsuOyh2KdNB9K5MfPuEWGkGEWlwlOv5Dt0EUk9zJQe 95Is0yE6T5u/bBA5UiMtt/BkcgiPMhdys2PIkGx0v4yyjq8MBQNz0JzQ8eFZTOtFRrnQ Nys1n/oqP/vNoSGQDmDEE3egKTWzzi3Mp6MKA6RNj1h4JiLRUjt3kGJsulI+oXoBaeO9 SsZqrCG89xVdWOI7Bf9a09NrNtXShcmlgpojh77uTO9LKyvg0hiKLB7KO6mIN4vhO8sN pllg== 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=jkpps7CmrcfqxyhiHuKNdC/UVAK102jupJaN4wYbgHXOpl/bcvnbtyqjEdS8AW6Jnz ADlAs0JR6wGFNqBTMeg10nauuwW6rKT+HjScQ1V51oCsXm0sC9Rlkwd/QolKT5c5Fwl9 TLxaHVEGyVbpLAmMfCoO6BPsuLaYbsSJAZ4DJ+i29q7FvSAuNi7TxO4kXPqHdNRlVgnJ mR5Vz3KeBgnA2+/G9BqhcxIV2tGvbHm5XiA/qe1nQPoFXT1Lidi55NoapCbOlnKerYBx I6yN1qseX//f5n3iZBrQJIF9eX30bI3RzIlz1EAspQH21JRcyLC1IUTbVlr+AITs9gIh t3tA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=fMEx1bvU; 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 j12si1823941ejx.282.2019.10.17.11.30.16; Thu, 17 Oct 2019 11:30:40 -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=fMEx1bvU; 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 S2406996AbfJPWGn (ORCPT + 99 others); Wed, 16 Oct 2019 18:06:43 -0400 Received: from mail.kernel.org ([198.145.29.99]:52088 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2438151AbfJPV62 (ORCPT ); Wed, 16 Oct 2019 17:58:28 -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 D169D21925; Wed, 16 Oct 2019 21:58:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1571263108; bh=Do8Ps45Db9i2Kq5cWrhLAuBtarWPj5Gh4AhA8R5NR3c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fMEx1bvUJA6EeZXYfad5vqy/m2aGNaV+Y89Rv/EgKLfU9xkyRjmE5SZMwjE9yTbaI gUOmm9nuSLFeYp+zBa/cucjMexd2BvZGnYBXSjvkNBKdHxK3SbIWObWiPiZTolvQ/i rGuIBGZzT0fbgJ3f+Fca77Wb3Y6Gqs7ADgGaivV8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Johan Hovold Subject: [PATCH 5.3 031/112] USB: usblcd: fix I/O after disconnect Date: Wed, 16 Oct 2019 14:50:23 -0700 Message-Id: <20191016214853.185824638@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191016214844.038848564@linuxfoundation.org> References: <20191016214844.038848564@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);