Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp1396518ybp; Thu, 17 Oct 2019 12:10:11 -0700 (PDT) X-Google-Smtp-Source: APXvYqzoGWMJikzj7w1JKf+ZL07kfo/NeipOzDFzypX7SB9ofWEy3+NcjbErTQdLZjb60Iv9I4z/ X-Received: by 2002:a50:cb86:: with SMTP id k6mr5563298edi.270.1571339411369; Thu, 17 Oct 2019 12:10:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571339411; cv=none; d=google.com; s=arc-20160816; b=pXCTkpE7Az2M1PEsUgsD2JzHthffFe4RE2MeDiPlgLzIf84vLLwlqvbhRSuFgIZVk4 fccPDADBrPeK6/LD9Uj+uD2E3JRS+M7FhHUvR+Gj3QzgQ6sHoxf/yTIBg5nskmSxnPFL hzPBNXAddk0sc8Ro8RDXngm0+NlV7rgVT8iLRsz1EKhXfkC9nGNmrKuQzLnHBoVX2m8R Ah/Yw0Am5s/lFoUxeT55ujowco6av4jKWBT/HRJNGfBZcwqXuDYk83KwODq9TmzLq/k3 mARulB4WkPj3apJ7RxRgS1vDid955JuvH3fPXijYVgdytbWmx8YhILPjQgr+YZqSo98I Wnew== 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=XME8mTIbkUyeZxnvZZ3EYiZmpPl6s0zfaEAVSM3K4Qs=; b=vZLQ8R+OpcbusgO/ubguzFwGT7PbVorEDav0Nrg7q+d04PsUJwVqQp6P0KIegtakZm UpfoiGAZhetvvL0oqB/3dpRyfIMB0uld1TdW7JWRMJtChT73tyNhJ4Tp0uso2n5L0CnJ Z3heSgm19Z2bPU9/1+yJP5F5QqIVA3zfO2VMSV/R5Ua7dCjZ6mchDyHRaOL+5gFQ9sB5 lSNhn7WJ90SwGXD5wiKiH0w/O6hdr4Ot8gPuFmQ3yc3Bkfblru3X1jcXLwEDDS4NQ00j bDgIUoQgndvLNYPdDuBBts24eO3rJM3jeKopX92XE5GsQIIphpHK7+e5Ev8eOvJdyBwk QIDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=jkRGRnGS; 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 y8si2177020edd.355.2019.10.17.12.09.47; Thu, 17 Oct 2019 12:10:11 -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=jkRGRnGS; 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 S2407364AbfJPWMo (ORCPT + 99 others); Wed, 16 Oct 2019 18:12:44 -0400 Received: from mail.kernel.org ([198.145.29.99]:47640 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728722AbfJPV4N (ORCPT ); Wed, 16 Oct 2019 17:56:13 -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 3653F21D7E; Wed, 16 Oct 2019 21:56:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1571262972; bh=tJ+FtqbpKL42d5aFZiV5RNnGk8PkDxuEQPiFz8YEiC4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jkRGRnGSWJk93awzWr51Yy2Zc41p8h2uWXMERPuyV9T4e6aUuf9oE8G/KjTq6oDRA C91pOykBM0RqDLfjlqFg2I7dYbfRu5TWpS/bP8AWei9Bfg7IfDjV6EmCSpU0Tt+dVW cAOjjeYt846ju6z8QfSzozjk3nohbQ7HW+JujDNQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Johan Hovold Subject: [PATCH 4.14 07/65] USB: usb-skeleton: fix NULL-deref on disconnect Date: Wed, 16 Oct 2019 14:50:21 -0700 Message-Id: <20191016214801.530931910@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191016214756.457746573@linuxfoundation.org> References: <20191016214756.457746573@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 bed5ef230943863b9abf5eae226a20fad9a8ff71 upstream. The driver was using its struct usb_interface pointer as an inverted disconnected flag and was setting it to NULL before making sure all completion handlers had run. This could lead to NULL-pointer dereferences in the dev_err() statements in the completion handlers which relies on said pointer. Fix this by using a dedicated disconnected flag. Note that this is also addresses a NULL-pointer dereference at release() and a struct usb_interface reference leak introduced by a recent runtime PM fix, which depends on and should have been submitted together with this patch. Fixes: 4212cd74ca6f ("USB: usb-skeleton.c: remove err() usage") Fixes: 5c290a5e42c3 ("USB: usb-skeleton: fix runtime PM after driver unbind") Cc: stable Signed-off-by: Johan Hovold Link: https://lore.kernel.org/r/20191009170944.30057-2-johan@kernel.org Signed-off-by: Greg Kroah-Hartman --- drivers/usb/usb-skeleton.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) --- a/drivers/usb/usb-skeleton.c +++ b/drivers/usb/usb-skeleton.c @@ -63,6 +63,7 @@ struct usb_skel { spinlock_t err_lock; /* lock for errors */ struct kref kref; struct mutex io_mutex; /* synchronize I/O with disconnect */ + unsigned long disconnected:1; wait_queue_head_t bulk_in_wait; /* to wait for an ongoing read */ }; #define to_skel_dev(d) container_of(d, struct usb_skel, kref) @@ -239,7 +240,7 @@ static ssize_t skel_read(struct file *fi if (rv < 0) return rv; - if (!dev->interface) { /* disconnect() was called */ + if (dev->disconnected) { /* disconnect() was called */ rv = -ENODEV; goto exit; } @@ -420,7 +421,7 @@ static ssize_t skel_write(struct file *f /* this lock makes sure we don't submit URBs to gone devices */ mutex_lock(&dev->io_mutex); - if (!dev->interface) { /* disconnect() was called */ + if (dev->disconnected) { /* disconnect() was called */ mutex_unlock(&dev->io_mutex); retval = -ENODEV; goto error; @@ -571,7 +572,7 @@ static void skel_disconnect(struct usb_i /* prevent more I/O from starting */ mutex_lock(&dev->io_mutex); - dev->interface = NULL; + dev->disconnected = 1; mutex_unlock(&dev->io_mutex); usb_kill_anchored_urbs(&dev->submitted);