Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp2446782yba; Mon, 15 Apr 2019 11:49:20 -0700 (PDT) X-Google-Smtp-Source: APXvYqx5djGXlV+NpPjD3jAolT7K/C/eDJfZiTnH5EsOBCyF1/5Gv6n2nlPP1bnOMCuUO5utkIJg X-Received: by 2002:a62:47d0:: with SMTP id p77mr77495474pfi.95.1555354160247; Mon, 15 Apr 2019 11:49:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555354160; cv=none; d=google.com; s=arc-20160816; b=rMLd0z7CGg5pyNnFwRIotuJCofBuQPX/teQdPrnMv0cmr3W0N4xr513TdTn0ltNVZS QU8mgMWLXKvlLwsgEBGQncXbouct8SxMzpeWVTh+VPEzoiGKMDvqTij8qbFDoXzac1o7 nT+TiVOwGQQGrET/GqRhFrf0A8DWrQbT+cwYmjnO/W3Z6iSDJxqpagMTvPMUP7kV/Uhg SEHFQaxPzyieLsDVNIJOhhZYseHg0xTNcN5ozJjr96wGjmc4dRcB7JUBBf68qCCzJmNc aPqBvIfoQkzGQyJWuAg9X2LAeW7TLKVxFi6YtgnYB8tNLJyAAnNdN8dR5PncPbT8g9M6 KfkQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=2Yehoe6C5gfRbLZy7jB9AVs2lv1mH1Uwt8HEM58Ivg4=; b=e8G1iToz/WFKaaJcuxTlouipLQJUNx0rWbJ5ab5iS0oPuLONn9a7oRE8rGlKCBNg0Y 90C8bT6L6wgUz1eN4GCCXpaJ6/fF0cmkjJLvHwTPif26TXQWZaBYUwrRfXFVSRqT0Rke VmPezoQjgpHgcCXob/hNAOT6trYdFBiPmYSXijqlpNJ+JOfgcD73+V6MNDeiadC1WP/W s5jJR7G///gtZur+2KFlqs3wft4A6iLzhRuRlQvt7CKtjNyi6kn7k2Lj/Y/mtvyLituH pq8d8aKdVsKJUwNAzSqMdk2YyvIgKCKMG54l8PvfyO573HNAG0kqy/eiOxw2RgeU8LDW XiyQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=wLs2R1Qt; 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 y6si49660430pfb.269.2019.04.15.11.49.03; Mon, 15 Apr 2019 11:49:20 -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=pass header.i=@kernel.org header.s=default header.b=wLs2R1Qt; 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 S1729003AbfDOSrx (ORCPT + 99 others); Mon, 15 Apr 2019 14:47:53 -0400 Received: from mail.kernel.org ([198.145.29.99]:51440 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728992AbfDOSru (ORCPT ); Mon, 15 Apr 2019 14:47:50 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id B7B1B2087C; Mon, 15 Apr 2019 18:47:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1555354069; bh=/djra96ESKAR8279YAcz6FaVbmTLbnPyz9Fu0HNKhRs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=wLs2R1Qt5nSH2HROH5ByxlIqZffJWFthK7euRMDU4pfzjZYLK/2o10Qehu4P9exmt tmGXCbLVsboJFgIxT7057WrIL0mn6s9UcmWet0TWvXyVaBJA9lYq8Z0xDkxl4J09qg MmCYFTLgJuFN84uO4+CPfAUvBJUIqlQRcMSPTx4g= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Li RongQing , Wang Li , Michal Kubecek , "David S. Miller" Subject: [PATCH 4.9 61/76] net: ethtool: not call vzalloc for zero sized memory request Date: Mon, 15 Apr 2019 20:44:25 +0200 Message-Id: <20190415183725.347326538@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190415183707.712011689@linuxfoundation.org> References: <20190415183707.712011689@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Li RongQing [ Upstream commit 3d8830266ffc28c16032b859e38a0252e014b631 ] NULL or ZERO_SIZE_PTR will be returned for zero sized memory request, and derefencing them will lead to a segfault so it is unnecessory to call vzalloc for zero sized memory request and not call functions which maybe derefence the NULL allocated memory this also fixes a possible memory leak if phy_ethtool_get_stats returns error, memory should be freed before exit Signed-off-by: Li RongQing Reviewed-by: Wang Li Reviewed-by: Michal Kubecek Signed-off-by: David S. Miller Signed-off-by: Greg Kroah-Hartman --- net/core/ethtool.c | 49 +++++++++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 18 deletions(-) --- a/net/core/ethtool.c +++ b/net/core/ethtool.c @@ -1801,17 +1801,22 @@ static int ethtool_get_strings(struct ne gstrings.len = ret; - data = kcalloc(gstrings.len, ETH_GSTRING_LEN, GFP_USER); - if (!data) - return -ENOMEM; + if (gstrings.len) { + data = kcalloc(gstrings.len, ETH_GSTRING_LEN, GFP_USER); + if (!data) + return -ENOMEM; - __ethtool_get_strings(dev, gstrings.string_set, data); + __ethtool_get_strings(dev, gstrings.string_set, data); + } else { + data = NULL; + } ret = -EFAULT; if (copy_to_user(useraddr, &gstrings, sizeof(gstrings))) goto out; useraddr += sizeof(gstrings); - if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN)) + if (gstrings.len && + copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN)) goto out; ret = 0; @@ -1899,17 +1904,21 @@ static int ethtool_get_stats(struct net_ return -EFAULT; stats.n_stats = n_stats; - data = kmalloc(n_stats * sizeof(u64), GFP_USER); - if (!data) - return -ENOMEM; + if (n_stats) { + data = kmalloc(n_stats * sizeof(u64), GFP_USER); + if (!data) + return -ENOMEM; - ops->get_ethtool_stats(dev, &stats, data); + ops->get_ethtool_stats(dev, &stats, data); + } else { + data = NULL; + } ret = -EFAULT; if (copy_to_user(useraddr, &stats, sizeof(stats))) goto out; useraddr += sizeof(stats); - if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64))) + if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64))) goto out; ret = 0; @@ -1938,19 +1947,23 @@ static int ethtool_get_phy_stats(struct return -EFAULT; stats.n_stats = n_stats; - data = kmalloc_array(n_stats, sizeof(u64), GFP_USER); - if (!data) - return -ENOMEM; - - mutex_lock(&phydev->lock); - phydev->drv->get_stats(phydev, &stats, data); - mutex_unlock(&phydev->lock); + if (n_stats) { + data = kmalloc_array(n_stats, sizeof(u64), GFP_USER); + if (!data) + return -ENOMEM; + + mutex_lock(&phydev->lock); + phydev->drv->get_stats(phydev, &stats, data); + mutex_unlock(&phydev->lock); + } else { + data = NULL; + } ret = -EFAULT; if (copy_to_user(useraddr, &stats, sizeof(stats))) goto out; useraddr += sizeof(stats); - if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64))) + if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64))) goto out; ret = 0;