Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp1477885imm; Fri, 15 Jun 2018 19:03:24 -0700 (PDT) X-Google-Smtp-Source: ADUXVKIwvc+/zc6WEL4CO5tHf5pMvrEQBBgQGu0lr0l0mCNY2+MQw7ELpO/z7K3yli3oHzkFd6bD X-Received: by 2002:a17:902:57d8:: with SMTP id g24-v6mr4717296plj.116.1529114604789; Fri, 15 Jun 2018 19:03:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1529114604; cv=none; d=google.com; s=arc-20160816; b=vzlL6rGzryMlXQ2jWMZwIEO4OFQVz8YY+5eCWm9m8uSTsOxRji1c/JNKSL3fthHVxZ Kz91fKF/n0Yr/0sbqPHoxY55WkMR/k7WJRKHXIUHNTHxPEdxP99A2XjHWvVtKH5mSKY3 PxlZzYFbXen73GB3tzaa432edlQ1CuCqap3XQYNlADXmzcGYI0A1A0HsvyvUi6i9PROn 7lv0ad8nKxGZpjyYhD8svukq1kVNOU64GzO3mvXKJX0LHdtg3CG7ijoJgslWP8NrmxU9 qL3O8plSCFGGm/H77nr0dvnXfF5KXorDswZXNlSUNbqt6l9+Qa5/yB65DwRnTnagu7Zs LbBQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:to:references:in-reply-to:message-id:date :subject:cc:from:dkim-signature:arc-authentication-results; bh=HrpNYrqFxxT8IA1rBbyF3mTf3I6JDal1GKiG4Kh1HVI=; b=CF+j6yY+Gx1CKAoS8G0I0bj34BqKvTkBJztSAhqcU1ofDFaOhVqEKiE2gFz1+8j6sq Tkiw5PBmH8XFRcnPlhZropwaP+Pg4x5BoBh9s6LmFCMyIzW3+EbJ36b/Dp4QmZfr2SLb XI4oHmI9/60VFyEx1vDEciO3m7rDpPli6mUQ9TC/bV5XYYNRF7WJHlHcecJOhp+Jv1Bo ix+6QaJ3OET6CvFvQCihFNpm8fGJtdGb14uwi0YzBzxmFmLjgSiQZldtdqbJsVBSAkFu a83LE8py8LokaIFjvKgqIximyWUJwrzOs94kv3tNYAPpk9NA4SoclYXMkkSRfTHANRu8 ERjg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b="b/uLxUpX"; 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; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d5-v6si9120023plo.3.2018.06.15.19.02.29; Fri, 15 Jun 2018 19:03:24 -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=fail header.i=@gmail.com header.s=20161025 header.b="b/uLxUpX"; 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; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753776AbeFPCBc (ORCPT + 99 others); Fri, 15 Jun 2018 22:01:32 -0400 Received: from mail-pl0-f66.google.com ([209.85.160.66]:36209 "EHLO mail-pl0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753539AbeFPCBb (ORCPT ); Fri, 15 Jun 2018 22:01:31 -0400 Received: by mail-pl0-f66.google.com with SMTP id a7-v6so6214058plp.3 for ; Fri, 15 Jun 2018 19:01:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=HrpNYrqFxxT8IA1rBbyF3mTf3I6JDal1GKiG4Kh1HVI=; b=b/uLxUpXd3pi5zrtL5xsG9eToU1TWY3/7E1E/26b9xOGwQqajvJKQ+YAQtTIM2Xn0z H3GsL1SRasMrj5/qqJ1O7RjsTp9Agu7OVTjlnK31x052NPMQvW1DPXBJerXemDziRneR uBTZhaN1HNfXTcRKQfrKUZh20zSTbSK2G/eQIihSvgyNCl/mbndq2b/JFQMW6mxR6n+N qHl5RrrRQoYM7GArJp04Ini+y1J9mKh/wm55jLhAIlx/57MgFyk48zRFyaaes5kYalED E1tl9hY7arRn6KvZfmDUBGJh8Y614Z591wFAg76paEbuOQ+gJMmoWkJ4kZZ61t5DjR3R HN2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=HrpNYrqFxxT8IA1rBbyF3mTf3I6JDal1GKiG4Kh1HVI=; b=CpYZsoBL74Jv0OFNRMAD4Dn2isLbe4yaQItacEskqSK5IX/kVQh2vQBt0icYLgBqAF ovC1H9sGmc++T319dtOYW4QhWqSrfcJZLx9whr2bzBFxIQwbA9F0UhuEMXnC3dgAsyRQ lnz81UYQxHYOiDItVNLQ3qm3AHtGBnOFa2B1pK/kULsw599gYnOSX8SgYhz79rrlDx5o RN9PgzSaWen/LIA7pMC8ZJHLqZDkMq+2PFYVRUpOluob6mQ+k81KDHGOVGVZu/VZ9Zgc QYVSXXLYnviuV1zdsSPRr6m5JtdkSp3hueulmtD/WAdqq0KbgEBTOtvnxgeQWr3f0n+b 6ntw== X-Gm-Message-State: APt69E3vt+sUfd2/LzhKpEifMt2Yhc86vd9RyNQ+Q3y86h4jHsqmtB6s x56SffB3uO507CuNFNZBn5E= X-Received: by 2002:a17:902:bd42:: with SMTP id b2-v6mr4678201plx.23.1529114491179; Fri, 15 Jun 2018 19:01:31 -0700 (PDT) Received: from localhost ([103.88.46.82]) by smtp.gmail.com with ESMTPSA id l62-v6sm13024702pga.21.2018.06.15.19.01.29 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 15 Jun 2018 19:01:30 -0700 (PDT) From: Zhouyang Jia Cc: Zhouyang Jia , Greg Kroah-Hartman , Christophe JAILLET , Jia-Ju Bai , Shreeya Patel , Colin Ian King , Kees Cook , devel@driverdev.osuosl.org, linux-kernel@vger.kernel.org Subject: [PATCH v4] staging: rtl8192u: add error handling for usb_alloc_urb Date: Sat, 16 Jun 2018 10:01:22 +0800 Message-Id: <1529114485-60684-1-git-send-email-jiazhouyang09@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1528705874-34845-1-git-send-email-jiazhouyang09@gmail.com> References: <1528705874-34845-1-git-send-email-jiazhouyang09@gmail.com> To: unlisted-recipients:; (no To-header on input) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When usb_alloc_urb fails, the lack of error-handling code may cause unexpected results. This patch adds error-handling code after calling usb_alloc_urb. Signed-off-by: Zhouyang Jia --- v1->v2: - Fix memory leak. v2->v3: - Release memory in error path. v3->v4: - Use kcalloc instead of kmalloc_array. --- drivers/staging/rtl8192u/r8192U_core.c | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/drivers/staging/rtl8192u/r8192U_core.c b/drivers/staging/rtl8192u/r8192U_core.c index 7a0dbc0..d15ee4f 100644 --- a/drivers/staging/rtl8192u/r8192U_core.c +++ b/drivers/staging/rtl8192u/r8192U_core.c @@ -1639,8 +1639,9 @@ short rtl8192_tx(struct net_device *dev, struct sk_buff *skb) static short rtl8192_usb_initendpoints(struct net_device *dev) { struct r8192_priv *priv = ieee80211_priv(dev); + int i; - priv->rx_urb = kmalloc(sizeof(struct urb *) * (MAX_RX_URB + 1), + priv->rx_urb = kcalloc(MAX_RX_URB + 1, sizeof(struct urb *), GFP_KERNEL); if (!priv->rx_urb) return -ENOMEM; @@ -1649,12 +1650,12 @@ static short rtl8192_usb_initendpoints(struct net_device *dev) for (i = 0; i < (MAX_RX_URB + 1); i++) { priv->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL); if (!priv->rx_urb[i]) - return -ENOMEM; + goto out_release_mem; priv->rx_urb[i]->transfer_buffer = kmalloc(RX_URB_SIZE, GFP_KERNEL); if (!priv->rx_urb[i]->transfer_buffer) - return -ENOMEM; + goto out_release_mem; priv->rx_urb[i]->transfer_buffer_length = RX_URB_SIZE; } @@ -1666,9 +1667,13 @@ static short rtl8192_usb_initendpoints(struct net_device *dev) void *oldaddr, *newaddr; priv->rx_urb[16] = usb_alloc_urb(0, GFP_KERNEL); + if (!priv->rx_urb[16]) + goto out_release_mem; + priv->oldaddr = kmalloc(16, GFP_KERNEL); if (!priv->oldaddr) - return -ENOMEM; + goto out_release_mem; + oldaddr = priv->oldaddr; align = ((long)oldaddr) & 3; if (align) { @@ -1686,17 +1691,19 @@ static short rtl8192_usb_initendpoints(struct net_device *dev) priv->pp_rxskb = kcalloc(MAX_RX_URB, sizeof(struct sk_buff *), GFP_KERNEL); if (!priv->pp_rxskb) { - kfree(priv->rx_urb); - - priv->pp_rxskb = NULL; - priv->rx_urb = NULL; - DMESGE("Endpoint Alloc Failure"); - return -ENOMEM; + goto out_release_mem; } netdev_dbg(dev, "End of initendpoints\n"); return 0; + +out_release_mem: + for (i = 0; i < (MAX_RX_URB + 1); i++) + kfree(priv->rx_urb[i]); + kfree(priv->rx_urb); + priv->rx_urb = NULL; + return -ENOMEM; } #ifdef THOMAS_BEACON -- 2.7.4