Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp461849imm; Thu, 30 Aug 2018 03:22:25 -0700 (PDT) X-Google-Smtp-Source: ANB0VdZvt/ZCPWnEU7+YnBrwJ2HgPQbzQ5Rajo7eDiwyNHOFFEYbJ1n0Z/OPVN9WObTAeF/7fxj+ X-Received: by 2002:a62:2a48:: with SMTP id q69-v6mr9832799pfq.86.1535624545453; Thu, 30 Aug 2018 03:22:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535624545; cv=none; d=google.com; s=arc-20160816; b=H/2F27PoN9IC0ZrFEmWVrAOO2vUJIDBhek02ylb1fOowjeukwwzzyVSQ8v4Pqr+55V WINlc4O46Mu8JlXd/S7LS6xClml6O0YxjywQiyYWGrfkxSyCDkmv0Dc1rkiByJLJioZz 6cvDMSVjgjsmwSNyxvZKet7uO0lDH34gnLwMj0drYQ9Uvuiw8VYFNMt6vklVB4mjUIrq dvZAAHjuUO4oxZOm2P28WVu6xCFQREYTN03n8XqDDfvADXu3xGJBeC6EfcnFoDiEn7pL YRlGNwt+uhxBhahV6hR7uOKV4tSrbyZDSKO/akPvdlOgjMdnxM6VqGvQrRfGwenXAhOJ 8vcQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :arc-authentication-results; bh=puMWbmffRGYXe04zFfp1cHf6LOgrekgQdXR/kmIxRD8=; b=xk2Fh5Lo0Izs1GMSV3XKOOswFR7i0Nw3o/M80y2mZ1Dpak9+6ILhHklOg7MO7xHRKf 9frKXwE3HmElODexlbeEKNAb/4yA9Y5NTPOSSBlPDJ0MsVwIO1giQARZmHdHmUnVbnd8 bTxepQCj7RPPdWSW1B85Y2M6WEpv7CDizX307gWJ3lsdgbrL+JylW8maJS9WpmbT1WpX lISFJmpbCzGZbMFPtFpke5UDQ9stONlROqLqVXivZL2srGyXQY+qO3sArRLbhbE8PcM+ CL8YYqcbib0Dc6aqIpuC5tdJVMbQ6GanFbmFF9F7cVizDEXNH+d6F30v5sAv+6BUFemj JDWw== ARC-Authentication-Results: i=1; mx.google.com; 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 l64-v6si5822552pge.420.2018.08.30.03.22.10; Thu, 30 Aug 2018 03:22:25 -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; 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 S1728363AbeH3OWb (ORCPT + 99 others); Thu, 30 Aug 2018 10:22:31 -0400 Received: from www.osadl.org ([62.245.132.105]:38981 "EHLO www.osadl.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728098AbeH3OWa (ORCPT ); Thu, 30 Aug 2018 10:22:30 -0400 Received: from debian01.hofrr.at (178.115.242.59.static.drei.at [178.115.242.59]) by www.osadl.org (8.13.8/8.13.8/OSADL-2007092901) with ESMTP id w7UAITmK015957; Thu, 30 Aug 2018 12:18:29 +0200 From: Nicholas Mc Guire To: Li Yang Cc: Felipe Balbi , Greg Kroah-Hartman , linux-usb@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, Nicholas Mc Guire Subject: [PATCH 1/2] usb: gadget: fsl_udc_core: check allocation return value and cleanup on failure Date: Thu, 30 Aug 2018 12:16:58 +0200 Message-Id: <1535624219-17854-1-git-send-email-hofrat@osadl.org> X-Mailer: git-send-email 2.1.4 X-Spam-Status: No, score=-1.9 required=6.0 tests=BAYES_00 autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on www.osadl.org Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The allocation with fsl_alloc_request() and kmalloc() were unchecked fixed this up with a NULL check and appropriate cleanup. Additionally udc->ep_qh_size was reset to 0 on failure of allocation. Similar udc->phy_mode is initially 0 (as udc_controller was allocated with kzalloc in fsl_udc_probe()) so reset it to 0 as well so that this function is side-effect free on failure. Not clear if this is necessary or sensible as fsl_udc_release() probably can not be called if fsl_udc_probe() failed - but it should not hurt. Signed-off-by: Nicholas Mc Guire Fixes: b504882da5 ("USB: add Freescale high-speed USB SOC device controller driver") --- Problem located with experimental coccinelle script Patch was compile tested with: imx_v6_v7_defconfig (implies USB_FSL_USB2=y) (with a large number of sparse warnings not related to the proposed change and one smatch warning) Patch is against 4.19-rc1 (localversion-next is next-20180830) drivers/usb/gadget/udc/fsl_udc_core.c | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/drivers/usb/gadget/udc/fsl_udc_core.c b/drivers/usb/gadget/udc/fsl_udc_core.c index be59309..e637afb 100644 --- a/drivers/usb/gadget/udc/fsl_udc_core.c +++ b/drivers/usb/gadget/udc/fsl_udc_core.c @@ -2247,8 +2247,10 @@ static int struct_udc_setup(struct fsl_udc *udc, udc->phy_mode = pdata->phy_mode; udc->eps = kcalloc(udc->max_ep, sizeof(struct fsl_ep), GFP_KERNEL); - if (!udc->eps) - return -1; + if (!udc->eps) { + ERR("kmalloc udc endpoint status failed\n"); + goto eps_alloc_failed; + } /* initialized QHs, take care of alignment */ size = udc->max_ep * sizeof(struct ep_queue_head); @@ -2262,8 +2264,7 @@ static int struct_udc_setup(struct fsl_udc *udc, &udc->ep_qh_dma, GFP_KERNEL); if (!udc->ep_qh) { ERR("malloc QHs for udc failed\n"); - kfree(udc->eps); - return -1; + goto ep_queue_alloc_failed; } udc->ep_qh_size = size; @@ -2272,8 +2273,17 @@ static int struct_udc_setup(struct fsl_udc *udc, /* FIXME: fsl_alloc_request() ignores ep argument */ udc->status_req = container_of(fsl_alloc_request(NULL, GFP_KERNEL), struct fsl_req, req); + if (!udc->status_req) { + ERR("kzalloc for udc status request failed\n"); + goto udc_status_alloc_failed; + } + /* allocate a small amount of memory to get valid address */ udc->status_req->req.buf = kmalloc(8, GFP_KERNEL); + if (!udc->status_req->req.buf) { + ERR("kzalloc for udc request buffer failed\n"); + goto udc_req_buf_alloc_failed; + } udc->resume_state = USB_STATE_NOTATTACHED; udc->usb_state = USB_STATE_POWERED; @@ -2281,6 +2291,18 @@ static int struct_udc_setup(struct fsl_udc *udc, udc->remote_wakeup = 0; /* default to 0 on reset */ return 0; + +udc_req_buf_alloc_failed: + kfree(udc->status_req); +udc_status_alloc_failed: + kfree(udc->ep_qh); + udc->ep_qh_size = 0; +ep_queue_alloc_failed: + kfree(udc->eps); +eps_alloc_failed: + udc->phy_mode = 0; + return -1; + } /*---------------------------------------------------------------- -- 2.1.4