Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp341442pxj; Thu, 20 May 2021 10:31:48 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwb/VZTa9eg2gxt9AC028gIf298ELD6dmcfgvAH7Rq34kiWRONxKYpSbd264yf/sc4k+MlK X-Received: by 2002:a17:906:3e97:: with SMTP id a23mr5896269ejj.440.1621531907741; Thu, 20 May 2021 10:31:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621531907; cv=none; d=google.com; s=arc-20160816; b=ytA85p6XTTMO5TfP7xiQJbU1M75onBbTMoNG+Zn8KolmVrg7ATbLPG4zadOdPBlnf1 MlW+Xpo4vhckV9aySLcKkGZgnIBAgTndwkiicqtZ5VQTk3xts3PjjFncKIbiG3oM2Dz6 5XL6lAunckMf9L47gZje9UVFErAzhA+FW1r1e4LSprC9A8pJth65LvGRf1TQmO11jTAk LvkZPJMLLeAhla0UKp9GuuIrhaVwkKLXJ4I8FXfIF+H2ULDwLeTsP4LcrwIvHIFGFQUZ m0D5XqVkVm1espmf76fBjPbrkgohN7xzoN8YLgiPaIyNThd4x4NmZBxs1tEO7X47bqtU VF0w== 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=fR21fwR7B7eTLF25JfRXYirrgrGIrVN7Iv7JLIZrh4k=; b=ABanKmFy0d6wFSWltnwyY5+l69TtMnstXCWZommWhE5OR/DEIeD8SwP3OVurZS7VAW qhgn/eMkkqOouMzSxtGutC4KkDdh7c7GyarwrvFdJKqdxistxlmLbXbpesKlHRqFhOl/ 6D5ebU72gy8ltrTHJEuD48A0Lgggt88Ofnp/IWWRsIeHZaskGY8494Krj4ghkbUEkOhD rkkP3TSXH+MDmkjdBjxITp4PDPV/3cqzXS1Tpz/LOC5STFETWqxhntK8Go2poMgUdmGJ 1+abV/MaQr9+ZuPPryH9XvSfmT3Ye6GdwgLAfbn4a5oAVvUdzaZiimB4957lnTbX0/XN pPzA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=OYLJGdE5; 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 q23si3005627ejd.116.2021.05.20.10.31.24; Thu, 20 May 2021 10:31:47 -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=OYLJGdE5; 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 S234557AbhETKAU (ORCPT + 99 others); Thu, 20 May 2021 06:00:20 -0400 Received: from mail.kernel.org ([198.145.29.99]:59060 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235063AbhETJzq (ORCPT ); Thu, 20 May 2021 05:55:46 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 22A5C6140B; Thu, 20 May 2021 09:37:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1621503440; bh=IXTND7rIO9OMTI99DvWE6Bru+IiPAiVZddbHdk+YBgI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OYLJGdE5Ro3DaElmwpNeY/bX/GCojB0ie+LeVii76sNpn25ULnfIVx4qEncVR71po 1KMSmThrplA9Fu2neCCTPKOi0riTCPwyPr3E23WZ1ZXATXr6Pp/eTO5wWK48jAFJWb hDz/J0bzMpedtW25odLnKf0SbZfPbHEVBK1MfSJE= 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.19 179/425] usb: gadget: pch_udc: Check if driver is present before calling ->setup() Date: Thu, 20 May 2021 11:19:08 +0200 Message-Id: <20210520092137.315666696@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210520092131.308959589@linuxfoundation.org> References: <20210520092131.308959589@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 527814361c3d..d87c9217cb57 100644 --- a/drivers/usb/gadget/udc/pch_udc.c +++ b/drivers/usb/gadget/udc/pch_udc.c @@ -2325,6 +2325,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 @@ -2396,15 +2411,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 & @@ -2652,9 +2664,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); } /** @@ -2689,9 +2699,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