Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp1330913ybp; Thu, 17 Oct 2019 11:09:52 -0700 (PDT) X-Google-Smtp-Source: APXvYqyMXq7RXjYwq/rRcox6HU9P8wG9+wK3iAbe2UyHMKsiWdbr/baTsX/z0XG6CKi/tyhrKmep X-Received: by 2002:a17:906:a986:: with SMTP id jr6mr4766273ejb.158.1571335792638; Thu, 17 Oct 2019 11:09:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571335792; cv=none; d=google.com; s=arc-20160816; b=l+Ae03dQb7/2nRBD1TGSImKth3m7yVZsbMMOfpTqrGikWUu7/moHXWJgDl8+W6VuHs iw2m9uzkrE1PbTOy8ijeKQt+wUSHVfzB+nN/jDwrqXFomGybed0EoX5V9dDOxs2El2fT ni2F0+CBYVLIh5xzBBIavpzuT9zYBkkNogK+77ZBd3GjnsEppA+086ks5RPHotT/WLZh Wh/7kN5UIovRv97TZLpSbqmSzqRzEep4e4IgcBitOaQ5cJO608t649W46oCL+HskuHUx cXW1sj3+OXBm14uHKhaki/u5WdXbqLT0VZOzPDeL/Afg056TPfrnUNZaV8CEdQjt5tAM +MTw== 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=XV25YT1K4HR7GJk+2c8xR2FuTDQrrfgKMqeRCNbrU0M=; b=daeEUt35uYACfzq+42t3WV4a4yu6j+MRkRLBi0JDd6cA+UcYNUVCmaBtY5jphN7muy IPhUhoxUc/4K9PhJuJid9DxQqXYTFuW0eNSEsC7j/lSOOwraxiYc0ztMmq9d1ZYBDYSu JypaFOmmGU5a/1sDpCzZ6+8zp4P6SOm/6ZZeOb/R4mwOk7Ig0S6hmlRMOwtZLbyQnbSh zZuQqudfv+6+SurfKDJiPCSyCxkoJUueVgC8JFO4TBo++U0yTM3xihcGYRs0VE65RNtp wlJma9xvu2r/FukvIBzcgjEIUqcvIbTaxubcwtvq1Ux/2+xSM1UHwXKLsGqqI+vF1PuO utiA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=f1om5Ycm; 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 g13si1968822edu.4.2019.10.17.11.09.29; Thu, 17 Oct 2019 11:09:52 -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=f1om5Ycm; 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 S2438558AbfJPV7f (ORCPT + 99 others); Wed, 16 Oct 2019 17:59:35 -0400 Received: from mail.kernel.org ([198.145.29.99]:52344 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2438213AbfJPV6j (ORCPT ); Wed, 16 Oct 2019 17:58:39 -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 C76E321E6F; Wed, 16 Oct 2019 21:58:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1571263118; bh=sWYa0PMEECtgIN2NOVbH131fBrFTFGRjDsy2EkvUyVw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=f1om5Ycm1sLKElE1Xe9/gBwIjF+SKedrkOS8BvJp5WS16cPjR7GY+U6HrYxHXVMGx FEhoaxElrsfwqE4iw4d3kxGcYERpRinVWAKWZgKT3rza2wIKrzNbcE/Ch9VURHAWOy 4dyLUrKCNUIKPb/kQ1BQ94QvRU8UMHf8bewa7XBE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Johan Hovold Subject: [PATCH 5.3 041/112] USB: legousbtower: fix potential NULL-deref on disconnect Date: Wed, 16 Oct 2019 14:50:33 -0700 Message-Id: <20191016214854.410237321@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 cd81e6fa8e033e7bcd59415b4a65672b4780030b upstream. The driver is using its struct usb_device pointer as an inverted disconnected flag, but was setting it to NULL before making sure all completion handlers had run. This could lead to a NULL-pointer dereference in a number of dev_dbg and dev_err statements in the completion handlers which relies on said pointer. Fix this by unconditionally stopping all I/O and preventing resubmissions by poisoning the interrupt URBs at disconnect and using a dedicated disconnected flag. This also makes sure that all I/O has completed by the time the disconnect callback returns. Fixes: 9d974b2a06e3 ("USB: legousbtower.c: remove err() usage") Fixes: fef526cae700 ("USB: legousbtower: remove custom debug macro") Fixes: 4dae99638097 ("USB: legotower: remove custom debug macro and module parameter") Cc: stable # 3.5 Signed-off-by: Johan Hovold Link: https://lore.kernel.org/r/20190919083039.30898-4-johan@kernel.org Signed-off-by: Greg Kroah-Hartman --- drivers/usb/misc/legousbtower.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) --- a/drivers/usb/misc/legousbtower.c +++ b/drivers/usb/misc/legousbtower.c @@ -190,6 +190,7 @@ struct lego_usb_tower { unsigned char minor; /* the starting minor number for this device */ int open_count; /* number of times this port has been opened */ + unsigned long disconnected:1; char* read_buffer; size_t read_buffer_length; /* this much came in */ @@ -289,8 +290,6 @@ static inline void lego_usb_tower_debug_ */ static inline void tower_delete (struct lego_usb_tower *dev) { - tower_abort_transfers (dev); - /* free data structures */ usb_free_urb(dev->interrupt_in_urb); usb_free_urb(dev->interrupt_out_urb); @@ -430,7 +429,8 @@ static int tower_release (struct inode * retval = -ENODEV; goto unlock_exit; } - if (dev->udev == NULL) { + + if (dev->disconnected) { /* the device was unplugged before the file was released */ /* unlock here as tower_delete frees dev */ @@ -466,10 +466,9 @@ static void tower_abort_transfers (struc if (dev->interrupt_in_running) { dev->interrupt_in_running = 0; mb(); - if (dev->udev) - usb_kill_urb (dev->interrupt_in_urb); + usb_kill_urb(dev->interrupt_in_urb); } - if (dev->interrupt_out_busy && dev->udev) + if (dev->interrupt_out_busy) usb_kill_urb(dev->interrupt_out_urb); } @@ -505,7 +504,7 @@ static __poll_t tower_poll (struct file dev = file->private_data; - if (!dev->udev) + if (dev->disconnected) return EPOLLERR | EPOLLHUP; poll_wait(file, &dev->read_wait, wait); @@ -552,7 +551,7 @@ static ssize_t tower_read (struct file * } /* verify that the device wasn't unplugged */ - if (dev->udev == NULL) { + if (dev->disconnected) { retval = -ENODEV; pr_err("No device or device unplugged %d\n", retval); goto unlock_exit; @@ -638,7 +637,7 @@ static ssize_t tower_write (struct file } /* verify that the device wasn't unplugged */ - if (dev->udev == NULL) { + if (dev->disconnected) { retval = -ENODEV; pr_err("No device or device unplugged %d\n", retval); goto unlock_exit; @@ -748,7 +747,7 @@ static void tower_interrupt_in_callback resubmit: /* resubmit if we're still running */ - if (dev->interrupt_in_running && dev->udev) { + if (dev->interrupt_in_running) { retval = usb_submit_urb (dev->interrupt_in_urb, GFP_ATOMIC); if (retval) dev_err(&dev->udev->dev, @@ -813,6 +812,7 @@ static int tower_probe (struct usb_inter dev->udev = udev; dev->open_count = 0; + dev->disconnected = 0; dev->read_buffer = NULL; dev->read_buffer_length = 0; @@ -938,6 +938,10 @@ static void tower_disconnect (struct usb /* give back our minor and prevent further open() */ usb_deregister_dev (interface, &tower_class); + /* stop I/O */ + usb_poison_urb(dev->interrupt_in_urb); + usb_poison_urb(dev->interrupt_out_urb); + mutex_lock(&dev->lock); /* if the device is not opened, then we clean up right now */ @@ -945,7 +949,7 @@ static void tower_disconnect (struct usb mutex_unlock(&dev->lock); tower_delete (dev); } else { - dev->udev = NULL; + dev->disconnected = 1; /* wake up pollers */ wake_up_interruptible_all(&dev->read_wait); wake_up_interruptible_all(&dev->write_wait);