Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp433030pxj; Thu, 20 May 2021 12:46:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzYrlMlzEHlpqjjMo5O48mNv/H6GSz+dxebS1rmEuud0qoqtCH1lOXaVhi9ObzqI1bOs/Rf X-Received: by 2002:a05:6402:5211:: with SMTP id s17mr6901297edd.352.1621539976282; Thu, 20 May 2021 12:46:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621539976; cv=none; d=google.com; s=arc-20160816; b=OLrw9G/QlMj7osls8sA2spnOu8ibbNXtuT42eIwC2triEygNXrC9XPkQuGZKYdZJAE 6aXbH8g7o63C0GIlwji+QnV1HPiWtOAHEPH/yu1nuDHJGV3kGLGmRpMAgnomOUx+ceY4 skN7jv67Qv1EcVA0t5f4um3UPqDIaOFd/ucELai2LS6tYydkT3nsbNhYCmF1baQDtckE uR4GmYkWTujqLIQiabgN5Q0y4ob5RhjFX7P9cxQhFUUS6ntEwrqEOr/HfHFCcuWlZMK7 2xYpY988ggAPd5IcldKv5sE4E1osVzpRoTE58qjRMGp92aT1AFKLbbtmot+VFRVpnuEy K08A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=zhJ2Z1QlmWZBvzvoEaNJTan76asiMCHNCUjWdYmPgCs=; b=hSwoVxYHh4Tm8rPfuOJvSJE9zgdgODBP3UlIQHeXMeFQHvJWZNczGQgRgIWzvKfxif D8Jv6WHndP19HXbeL3yBonX4Sjtp0SybxmuMI1i8apf41iY4sp570gKuANxVSrSctPNP MhIR+Y+af16eUG+jCTLYkIKh58C/qZw3hojrgf5QMG65k0hp6HSUmay9Y8u65tfI+TGy loAakZeF5QC3hKfw6SR41LCPfpWladw7FGM+3xXjK5ujpbU0DiuxVBFiWVF1EvX+QWsm 0WyBUGQinSWRD66iWVkUp4+vo+srVRdV0DaSyovz/309CrxcSe1BtpTQ8hYl+jEJxwct lZtA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=uE0NJv6A; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b13si3614826ede.317.2021.05.20.12.45.52; Thu, 20 May 2021 12:46:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=uE0NJv6A; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236824AbhETKmP (ORCPT + 99 others); Thu, 20 May 2021 06:42:15 -0400 Received: from mail.kernel.org ([198.145.29.99]:58206 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236569AbhETK0j (ORCPT ); Thu, 20 May 2021 06:26:39 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 66E99613F6; Thu, 20 May 2021 09:50:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1621504219; bh=P5VetOXb/caIw4yGJnkNW/+Jngg0UanJmwN/TaF+Hio=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uE0NJv6A8npv5b5kgQnZwdQ13+KAZAXkC93C8I7fLr+HJYtkiYZ19qn/FHjl+crHG s6DFYJPkjHtA5tsIZ9l5rTkk5EjA0KXyOuzHD01lE28+sZdkptMUPyfVx7jmrdLe6F L+6fCF26tFu1a77CNrAGcxhPmfkb+Lw5c92RIOFc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Andy Shevchenko , Sasha Levin Subject: [PATCH 4.14 147/323] usb: gadget: pch_udc: Check if driver is present before calling ->setup() Date: Thu, 20 May 2021 11:20:39 +0200 Message-Id: <20210520092125.143665461@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210520092120.115153432@linuxfoundation.org> References: <20210520092120.115153432@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Andy Shevchenko [ Upstream commit fbdbbe6d3ee502b3bdeb4f255196bb45003614be ] Since we have a separate routine for VBUS sense, the interrupt may occur before gadget driver is present. Hence, ->setup() call may oops the kernel: [ 55.245843] BUG: kernel NULL pointer dereference, address: 00000010 ... [ 55.245843] EIP: pch_udc_isr.cold+0x162/0x33f ... [ 55.245843] [ 55.245843] ? pch_udc_svc_data_out+0x160/0x160 Check if driver is present before calling ->setup(). Fixes: f646cf94520e ("USB device driver of Topcliff PCH") Signed-off-by: Andy Shevchenko Link: https://lore.kernel.org/r/20210323153626.54908-2-andriy.shevchenko@linux.intel.com Signed-off-by: Greg Kroah-Hartman Signed-off-by: Sasha Levin --- drivers/usb/gadget/udc/pch_udc.c | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/drivers/usb/gadget/udc/pch_udc.c b/drivers/usb/gadget/udc/pch_udc.c index e9c2f67a8b10..1090b846df38 100644 --- a/drivers/usb/gadget/udc/pch_udc.c +++ b/drivers/usb/gadget/udc/pch_udc.c @@ -2329,6 +2329,21 @@ static void pch_udc_svc_data_out(struct pch_udc_dev *dev, int ep_num) pch_udc_set_dma(dev, DMA_DIR_RX); } +static int pch_udc_gadget_setup(struct pch_udc_dev *dev) + __must_hold(&dev->lock) +{ + int rc; + + /* In some cases we can get an interrupt before driver gets setup */ + if (!dev->driver) + return -ESHUTDOWN; + + spin_unlock(&dev->lock); + rc = dev->driver->setup(&dev->gadget, &dev->setup_data); + spin_lock(&dev->lock); + return rc; +} + /** * pch_udc_svc_control_in() - Handle Control IN endpoint interrupts * @dev: Reference to the device structure @@ -2400,15 +2415,12 @@ static void pch_udc_svc_control_out(struct pch_udc_dev *dev) dev->gadget.ep0 = &dev->ep[UDC_EP0IN_IDX].ep; else /* OUT */ dev->gadget.ep0 = &ep->ep; - spin_lock(&dev->lock); /* If Mass storage Reset */ if ((dev->setup_data.bRequestType == 0x21) && (dev->setup_data.bRequest == 0xFF)) dev->prot_stall = 0; /* call gadget with setup data received */ - setup_supported = dev->driver->setup(&dev->gadget, - &dev->setup_data); - spin_unlock(&dev->lock); + setup_supported = pch_udc_gadget_setup(dev); if (dev->setup_data.bRequestType & USB_DIR_IN) { ep->td_data->status = (ep->td_data->status & @@ -2656,9 +2668,7 @@ static void pch_udc_svc_intf_interrupt(struct pch_udc_dev *dev) dev->ep[i].halted = 0; } dev->stall = 0; - spin_unlock(&dev->lock); - dev->driver->setup(&dev->gadget, &dev->setup_data); - spin_lock(&dev->lock); + pch_udc_gadget_setup(dev); } /** @@ -2693,9 +2703,7 @@ static void pch_udc_svc_cfg_interrupt(struct pch_udc_dev *dev) dev->stall = 0; /* call gadget zero with setup data received */ - spin_unlock(&dev->lock); - dev->driver->setup(&dev->gadget, &dev->setup_data); - spin_lock(&dev->lock); + pch_udc_gadget_setup(dev); } /** -- 2.30.2