Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp1352330ybp; Thu, 17 Oct 2019 11:28:53 -0700 (PDT) X-Google-Smtp-Source: APXvYqx1+uIQX+STXYX2BOvflaammB7d0EePFBLCZaOMeoUaW8fvyZs7LvSNPpTGkar9aVMAVmrg X-Received: by 2002:aa7:d358:: with SMTP id m24mr5347912edr.204.1571336933069; Thu, 17 Oct 2019 11:28:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571336933; cv=none; d=google.com; s=arc-20160816; b=fDGs7C0JQl3CByb96RhGbmouyA8KRTp56ycQ3taQun5gE1KyAIZAfc+4zvKmjb8fGF GWvHNP/H3dlbvH6A3/3LPjKTXaIquhoOmjUoACg/IuK649xHRJ06wIoQaOrmtMZcA/l7 SjLdDdYf7vavUMgQzp5XhETFAjqtppE5BvOHP8UcayoJYJF+eF9br21BVPMlNpwepkqV 7ArEHjRmm8WGwM2GAObgJkwqD+urRcj10kBAp/HkJkjLNF9eQ2yDao531Al5fzbbhDEP 4szFxpDC/nrUZnNBdPgxE4S9Y6jxLgHJgpHbT+c9uBDg8tkRLdyryjBZccajZV1cTNM1 SNBQ== 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=XGo13rwrsO6J1L4C2n5sFKyKTbOcXToKg3W7c/e7sn0=; b=Gk+230uFY2Krih9+jqEpu+v8IEq2VDu6zFNLqqbWYrQBOfqEo2bs1krU4V4YGFmIEr ALXEIC/0C7wCQklJz4odVRH7KgN0Jp8rx6RLm94UevgyBaRNws8logiP8qQ9Eq7vGGtI ufZN+QDtvO1246sDzQbhOT+l6cuH2c7AYgRr5jgUPfzO7JElMOoblHP561tNU8t5kg6U 81h6gFKvo6LGO1nO862Yr1ulZF4qdPka/BEzHO6l4jmzWpGdRS7lpcWQ137ZhV9m9chn Qu0iLx00e7MRTzr0N9BDOac3ZvCpyPjpUw94iCVXXyc/klPqSDXjrShKj3JTdzIO30cI YjYg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=b1AHQgIr; 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 i3si1903820edq.163.2019.10.17.11.28.29; Thu, 17 Oct 2019 11:28:53 -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=b1AHQgIr; 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 S2392362AbfJPWGF (ORCPT + 99 others); Wed, 16 Oct 2019 18:06:05 -0400 Received: from mail.kernel.org ([198.145.29.99]:52394 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2438209AbfJPV6i (ORCPT ); Wed, 16 Oct 2019 17:58:38 -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 D549021D7E; Wed, 16 Oct 2019 21:58:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1571263117; bh=yk9sT55xKYSbTU7SwJdDXKQs5jxFZqeLsk4v9Gu/aQY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=b1AHQgIr28ccZ1Z4oYH/XxjepXW1Bf90O/bh6w0ww5tEwQjD9Xw00rmseK6k5gKUt dSpYPgUm3MXnl3rHEFarPR2oFJuINoEz2dPdoQGwVp7TQaI5UwZDmsHci1pC56mDiw rnGSEdLVhVckZC5uvY+dAwKxjntaZnb8IZlYWGVc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Johan Hovold Subject: [PATCH 5.3 023/112] USB: ldusb: fix NULL-derefs on driver unbind Date: Wed, 16 Oct 2019 14:50:15 -0700 Message-Id: <20191016214851.712718557@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 58ecf131e74620305175a7aa103f81350bb37570 upstream. The driver was using its struct usb_interface 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, dev_warn 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: 2824bd250f0b ("[PATCH] USB: add ldusb driver") Cc: stable # 2.6.13 Signed-off-by: Johan Hovold Link: https://lore.kernel.org/r/20191009153848.8664-4-johan@kernel.org Signed-off-by: Greg Kroah-Hartman --- drivers/usb/misc/ldusb.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) --- a/drivers/usb/misc/ldusb.c +++ b/drivers/usb/misc/ldusb.c @@ -153,6 +153,7 @@ MODULE_PARM_DESC(min_interrupt_out_inter struct ld_usb { struct mutex mutex; /* locks this structure */ struct usb_interface *intf; /* save off the usb interface pointer */ + unsigned long disconnected:1; int open_count; /* number of times this port has been opened */ @@ -192,12 +193,10 @@ static void ld_usb_abort_transfers(struc /* shutdown transfer */ if (dev->interrupt_in_running) { dev->interrupt_in_running = 0; - if (dev->intf) - usb_kill_urb(dev->interrupt_in_urb); + usb_kill_urb(dev->interrupt_in_urb); } if (dev->interrupt_out_busy) - if (dev->intf) - usb_kill_urb(dev->interrupt_out_urb); + usb_kill_urb(dev->interrupt_out_urb); } /** @@ -205,8 +204,6 @@ static void ld_usb_abort_transfers(struc */ static void ld_usb_delete(struct ld_usb *dev) { - ld_usb_abort_transfers(dev); - /* free data structures */ usb_free_urb(dev->interrupt_in_urb); usb_free_urb(dev->interrupt_out_urb); @@ -263,7 +260,7 @@ static void ld_usb_interrupt_in_callback resubmit: /* resubmit if we're still running */ - if (dev->interrupt_in_running && !dev->buffer_overflow && dev->intf) { + if (dev->interrupt_in_running && !dev->buffer_overflow) { retval = usb_submit_urb(dev->interrupt_in_urb, GFP_ATOMIC); if (retval) { dev_err(&dev->intf->dev, @@ -392,7 +389,7 @@ static int ld_usb_release(struct inode * retval = -ENODEV; goto unlock_exit; } - if (dev->intf == NULL) { + if (dev->disconnected) { /* the device was unplugged before the file was released */ mutex_unlock(&dev->mutex); /* unlock here as ld_usb_delete frees dev */ @@ -423,7 +420,7 @@ static __poll_t ld_usb_poll(struct file dev = file->private_data; - if (!dev->intf) + if (dev->disconnected) return EPOLLERR | EPOLLHUP; poll_wait(file, &dev->read_wait, wait); @@ -462,7 +459,7 @@ static ssize_t ld_usb_read(struct file * } /* verify that the device wasn't unplugged */ - if (dev->intf == NULL) { + if (dev->disconnected) { retval = -ENODEV; printk(KERN_ERR "ldusb: No device or device unplugged %d\n", retval); goto unlock_exit; @@ -542,7 +539,7 @@ static ssize_t ld_usb_write(struct file } /* verify that the device wasn't unplugged */ - if (dev->intf == NULL) { + if (dev->disconnected) { retval = -ENODEV; printk(KERN_ERR "ldusb: No device or device unplugged %d\n", retval); goto unlock_exit; @@ -764,6 +761,9 @@ static void ld_usb_disconnect(struct usb /* give back our minor */ usb_deregister_dev(intf, &ld_usb_class); + usb_poison_urb(dev->interrupt_in_urb); + usb_poison_urb(dev->interrupt_out_urb); + mutex_lock(&dev->mutex); /* if the device is not opened, then we clean up right now */ @@ -771,7 +771,7 @@ static void ld_usb_disconnect(struct usb mutex_unlock(&dev->mutex); ld_usb_delete(dev); } else { - dev->intf = NULL; + dev->disconnected = 1; /* wake up pollers */ wake_up_interruptible_all(&dev->read_wait); wake_up_interruptible_all(&dev->write_wait);