From patchwork Thu Mar 28 11:52:08 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Heikki Krogerus X-Patchwork-Id: 10874827 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D3F271708 for ; Thu, 28 Mar 2019 11:52:13 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BE3AC28AF2 for ; Thu, 28 Mar 2019 11:52:13 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id ACE9B28AFA; Thu, 28 Mar 2019 11:52:13 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4ACA428AF2 for ; Thu, 28 Mar 2019 11:52:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726715AbfC1LwL (ORCPT ); Thu, 28 Mar 2019 07:52:11 -0400 Received: from mga12.intel.com ([192.55.52.136]:59094 "EHLO mga12.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725779AbfC1LwL (ORCPT ); Thu, 28 Mar 2019 07:52:11 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga106.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 28 Mar 2019 04:52:11 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.60,280,1549958400"; d="scan'208";a="159196620" Received: from black.fi.intel.com (HELO black.fi.intel.com.) ([10.237.72.28]) by fmsmga001.fm.intel.com with ESMTP; 28 Mar 2019 04:52:09 -0700 From: Heikki Krogerus To: Greg KH Cc: Hans de Goede , linux-usb@vger.kernel.org, stable@vger.kernel.org Subject: [PATCH] usb: typec: mux: Store module handle Date: Thu, 28 Mar 2019 14:52:08 +0300 Message-Id: <20190328115208.42086-1-heikki.krogerus@linux.intel.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP It is possible that the driver of the mux device has been unbind by the time typec_mux_put() or typec_switch_put() is called. To prevent the NULL Pointer Dereference from happening in this case when decrementing the reference count of the module by using dev->driver->owner, storing the module handle to the mux and switch data structures, and using the stored value instead. Fixes: ("3e3b81965cbf usb: typec: mux: Take care of driver module reference counting") Cc: stable@vger.kernel.org Signed-off-by: Heikki Krogerus --- drivers/usb/typec/mux.c | 10 ++++++---- include/linux/usb/typec_mux.h | 2 ++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/drivers/usb/typec/mux.c b/drivers/usb/typec/mux.c index 2ce54f3fc79c..617687c4eb20 100644 --- a/drivers/usb/typec/mux.c +++ b/drivers/usb/typec/mux.c @@ -63,7 +63,8 @@ struct typec_switch *typec_switch_get(struct device *dev) sw = device_connection_find_match(dev, "orientation-switch", NULL, typec_switch_match); if (!IS_ERR_OR_NULL(sw)) { - WARN_ON(!try_module_get(sw->dev->driver->owner)); + sw->module = sw->dev->driver->owner; + WARN_ON(!try_module_get(sw->module)); get_device(sw->dev); } mutex_unlock(&switch_lock); @@ -81,7 +82,7 @@ EXPORT_SYMBOL_GPL(typec_switch_get); void typec_switch_put(struct typec_switch *sw) { if (!IS_ERR_OR_NULL(sw)) { - module_put(sw->dev->driver->owner); + module_put(sw->module); put_device(sw->dev); } } @@ -206,7 +207,8 @@ struct typec_mux *typec_mux_get(struct device *dev, mux = device_connection_find_match(dev, "mode-switch", (void *)desc, typec_mux_match); if (!IS_ERR_OR_NULL(mux)) { - WARN_ON(!try_module_get(mux->dev->driver->owner)); + mux->module = mux->dev->driver->owner; + WARN_ON(!try_module_get(mux->module)); get_device(mux->dev); } mutex_unlock(&mux_lock); @@ -224,7 +226,7 @@ EXPORT_SYMBOL_GPL(typec_mux_get); void typec_mux_put(struct typec_mux *mux) { if (!IS_ERR_OR_NULL(mux)) { - module_put(mux->dev->driver->owner); + module_put(mux->module); put_device(mux->dev); } } diff --git a/include/linux/usb/typec_mux.h b/include/linux/usb/typec_mux.h index 43f40685e53c..b31498020bf3 100644 --- a/include/linux/usb/typec_mux.h +++ b/include/linux/usb/typec_mux.h @@ -20,6 +20,7 @@ struct device; */ struct typec_switch { struct device *dev; + struct module *module; struct list_head entry; int (*set)(struct typec_switch *sw, enum typec_orientation orientation); @@ -37,6 +38,7 @@ struct typec_switch { */ struct typec_mux { struct device *dev; + struct module *module; struct list_head entry; int (*set)(struct typec_mux *mux, int state);