Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp1058580imm; Fri, 15 Jun 2018 10:29:11 -0700 (PDT) X-Google-Smtp-Source: ADUXVKKDtaCXxLjaJO6uG10AyOSsUmtbNl8zEvqZmTDIPW8HnGeprB6lWc9Zqa9dJE3W3pCkEqUT X-Received: by 2002:a17:902:722:: with SMTP id 31-v6mr3110859pli.3.1529083751722; Fri, 15 Jun 2018 10:29:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1529083751; cv=none; d=google.com; s=arc-20160816; b=gWIcdrVn9FSuUg3evaH/PyhWc1fCKInMwFTel7kGX+jzSEKNudgp+ar4rXAvxf408+ ao0qsEO2DBABHBvE1QHjmQvJGa7ZEedQCJAgkUJUzNj33Kv3IXweGXMERuAra6DiE9UA Pfk8YUCjzGKttXp4gFIbZq3t9cTEg5C9EhDY1YpVlFc6UUQ8Z24kxMAcup5eohLV1ROT h4+gYMvELj+p5hdQOFI6b8ZTvif8Js3eH7st/vGuthwGKOEMIbsNinj3huFUCNpH9Kyy 0nUEHKY+hCHw1B8IwvXBrwD/5DEPZ0TNGq/EZIJODAFPep1qMBpsO2PGRq4wBxxiDIi2 tczw== 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=MOu+hg0kC0/LmbXOgXK+4B7f3LeOOGkdlGyiI8CVpjw=; b=qflVxreLl8cFdOY1/PVecoUrfOC2eFYKIRWrCfasevyiPlCl/IDQT9s0eWwIA3ODZG fAZbUyYR99oQ5ue7VN3XMoGStc+kGEmUz9oemieds/+o+o5UqLQtm3QydHE/wHLmQaGJ H85WcjLHZrCxWd9AEHCw5GA+3t2kKnhVonisEGXoadxblKuxa/pmAmfprF8TJV9Q6E46 f+thnbynrNxU1hfDT11Z30ywAdn5+zxH8x4fYoLWudTLKrf7RNBf/Wec4tjnO/+q1CQ2 pVTqxS8WeW2M/N4ssJrw4dV3hoeY8t+xJghSAbAmUv1LuaWELRrfx8+4BLg6r2N0vH45 ve5A== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=KBkEwt4x; 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 a26-v6si6737238pgf.557.2018.06.15.10.28.54; Fri, 15 Jun 2018 10:29:11 -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=KBkEwt4x; 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 S936334AbeFOR21 (ORCPT + 99 others); Fri, 15 Jun 2018 13:28:27 -0400 Received: from mail-pf0-f193.google.com ([209.85.192.193]:41414 "EHLO mail-pf0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S936136AbeFOR20 (ORCPT ); Fri, 15 Jun 2018 13:28:26 -0400 Received: by mail-pf0-f193.google.com with SMTP id a11-v6so5172879pff.8 for ; Fri, 15 Jun 2018 10:28:26 -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=MOu+hg0kC0/LmbXOgXK+4B7f3LeOOGkdlGyiI8CVpjw=; b=KBkEwt4xuLR+b36Tq969jT7E6pYuJQRGIrJe3Io7C1ArFkyAMQuYAo68SEL1mLRKG3 6D/KVwa9hdoyWvjckfELnAP3qnuoY01lGrZZRBZ/RtEpy8GM83QgGln3LQCS/z8M9gkt wEfA30SrvFnILaG+VVJkzmkj8Dj0IPwZsNU4XHv/ZGo1gyuGhzcRRemMawCCpNqno9Uz HU1z3zuVY6TQL/eLaEnA7jyAeF6sJOZ6SSmdHQV2dEHWMetBWB1KV7kQD4IiqZTBxxdK 6zhoUEDOYGXSaqNxgCJ0KBpC0vwIxyyZMXBYTLv+8urqpduiMgLxAbb6GXjn5F4ShPGY BI5g== 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=MOu+hg0kC0/LmbXOgXK+4B7f3LeOOGkdlGyiI8CVpjw=; b=FizuQtHnZFwrGu5TuOUSOMMzU44FN4dnreoSyBsNI9pQ8WtZTeWJ7DOu2SiYjd2zp7 iCp47Dxa3C+cMoPVOhew3eS2Ku1NfOoHyYxQV0idXx6Xl59mneuLCRxvzUN+8QjrjjPD YfvTO3uOiELoIUD/i5K+I939c3LeElB/pFyodG939oMirO+/AFedV7QcO9zpaosTBLZ9 5r55gAa8XMJsmE8STgByN2hIeSDXGNGBp1EuV09gjIOK9d36SM8o6xz0WoASrTuNxV7f AqB573w1xxSSKJP4yNtF3dwC1MRrRYv6xinjJ1CfNMb31M3Bi2sL7QgBzVOFvfWFJDJW uLBA== X-Gm-Message-State: APt69E1QpnLhvGe9mMThYvO0XwIjnDvbH+yhUg0pssXUXWV9c8H/tDvM qheC5qJE7BmbC1tZ6JNXo/g= X-Received: by 2002:a62:fd0b:: with SMTP id p11-v6mr2959877pfh.52.1529083706112; Fri, 15 Jun 2018 10:28:26 -0700 (PDT) Received: from localhost ([103.88.46.82]) by smtp.gmail.com with ESMTPSA id o77-v6sm16678348pfj.0.2018.06.15.10.28.24 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 15 Jun 2018 10:28:25 -0700 (PDT) From: Zhouyang Jia Cc: Zhouyang Jia , Greg Kroah-Hartman , Christophe JAILLET , Colin Ian King , Jia-Ju Bai , Shreeya Patel , Kees Cook , devel@driverdev.osuosl.org, linux-kernel@vger.kernel.org Subject: [PATCH v3] staging: rtl8192u: add error handling for usb_alloc_urb Date: Sat, 16 Jun 2018 01:28:16 +0800 Message-Id: <1529083699-59319-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. --- drivers/staging/rtl8192u/r8192U_core.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/drivers/staging/rtl8192u/r8192U_core.c b/drivers/staging/rtl8192u/r8192U_core.c index 7a0dbc0..1c980e9 100644 --- a/drivers/staging/rtl8192u/r8192U_core.c +++ b/drivers/staging/rtl8192u/r8192U_core.c @@ -1649,12 +1649,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 +1666,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 +1690,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