Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp2461316yba; Mon, 15 Apr 2019 12:08:02 -0700 (PDT) X-Google-Smtp-Source: APXvYqzrEcyddIL5NL3L6rQPqiYMmyDi9Xt8IYgclkbTkaHfrS5NeLD4A1AxP6YKpDyspWJbEgYP X-Received: by 2002:a62:5707:: with SMTP id l7mr49035374pfb.205.1555355282883; Mon, 15 Apr 2019 12:08:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555355282; cv=none; d=google.com; s=arc-20160816; b=0cVE4Dqag8grfd3EA74LfKrmH+wCHqfAxi2Nde6w55oN92vidNia2rU/jIiEK0xUOF 5tJWicwInvWG+RqRaYGS9GRNUTmSNUaogRyIslWdxkamfLyvUmb3mK7nSDGtCHWayC4i Hl9dpmaqIj6laO07WKYyReXPtEq/QtW2yvnF0IPmSxVtDScUXz0BEakHjcuICKYU2H8W 1BuDZSreO8zvpO2erlcAIrZuGLii6gtJdLB1QvSIfbfp+29ZSaZemtXRDaP9kTRq6SkR a4MU77rMgVke3t/Nd/rR8WJ/yCcF7HuP/Qc2GmK8uPAdE77l6zD1Ih/tyZNjV+6r1uge rF9A== 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=RJxrz1Zg/aPJJ3gvcHmVxArOHTHlZc05BSXjPXQqD4U=; b=HaU//BpE/9tKER98zk0H7EXWId4xz5vvuee9g67ewu8I0Mog2FnjN1yftHnS2xD2nC 4O+th317773bks/YWtwOCMJZzRNPKmBzTNflTCyRRjBUmkuH9m+62qaupY7gF3ul9XxS KXKGyi/1OjYXnDbBu2QaKbTKLvh3KEv0djRsqlZ8EQxpPQC6DumVbAV63mGlygRu9YQj wSPjlnDfWoK2Z84ZmOkNXSrWmPR/9yVeMLbW/qLo4V0f6Tw5piMY+CUFD5TKFmUN190b SRpXlPZhkLmNsWbgO5GBEy3gyJIxHE3LJOSzNOvnvDRSgrbx6OhLzvkWLIoef6rtdM+/ h9OQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="Kkf4pb/q"; 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 a192si27155154pge.50.2019.04.15.12.07.47; Mon, 15 Apr 2019 12:08:02 -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="Kkf4pb/q"; 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 S1729636AbfDOTEz (ORCPT + 99 others); Mon, 15 Apr 2019 15:04:55 -0400 Received: from mail.kernel.org ([198.145.29.99]:38174 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729091AbfDOTEx (ORCPT ); Mon, 15 Apr 2019 15:04:53 -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 36BFF218A1; Mon, 15 Apr 2019 19:04:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1555355092; bh=ijJWrPsLshffVG4EdDtDAmNhnR7vNho8Y8kZW7fmGOU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Kkf4pb/qJ6aqc1UI3x2gpa1CtuNOn7V1e2MHRPvbOxDrF67YvxYJnPWlCmEaT/GX+ DgDAGvWipgX+DbhZrMlfemNAHwyp24WWZZ5rSdj/8w/61vnC8VhltqD6qgPYMk53j5 HF0jR4UiA+v7Vdx4+ULUPcruowL8dqjFbQNY0VTs= 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" , Sasha Levin Subject: [PATCH 4.19 010/101] net: ethtool: not call vzalloc for zero sized memory request Date: Mon, 15 Apr 2019 20:58:08 +0200 Message-Id: <20190415183740.916616196@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190415183740.341577907@linuxfoundation.org> References: <20190415183740.341577907@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 [ 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: Sasha Levin --- net/core/ethtool.c | 46 ++++++++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/net/core/ethtool.c b/net/core/ethtool.c index aeabc4831fca..7cc97f43f138 100644 --- a/net/core/ethtool.c +++ b/net/core/ethtool.c @@ -1863,11 +1863,16 @@ static int ethtool_get_strings(struct net_device *dev, void __user *useraddr) WARN_ON_ONCE(!ret); gstrings.len = ret; - data = vzalloc(array_size(gstrings.len, ETH_GSTRING_LEN)); - if (gstrings.len && !data) - return -ENOMEM; - __ethtool_get_strings(dev, gstrings.string_set, data); + if (gstrings.len) { + data = vzalloc(array_size(gstrings.len, ETH_GSTRING_LEN)); + if (!data) + return -ENOMEM; + + __ethtool_get_strings(dev, gstrings.string_set, data); + } else { + data = NULL; + } ret = -EFAULT; if (copy_to_user(useraddr, &gstrings, sizeof(gstrings))) @@ -1963,11 +1968,15 @@ static int ethtool_get_stats(struct net_device *dev, void __user *useraddr) return -EFAULT; stats.n_stats = n_stats; - data = vzalloc(array_size(n_stats, sizeof(u64))); - if (n_stats && !data) - return -ENOMEM; - ops->get_ethtool_stats(dev, &stats, data); + if (n_stats) { + data = vzalloc(array_size(n_stats, sizeof(u64))); + if (!data) + return -ENOMEM; + ops->get_ethtool_stats(dev, &stats, data); + } else { + data = NULL; + } ret = -EFAULT; if (copy_to_user(useraddr, &stats, sizeof(stats))) @@ -2007,16 +2016,21 @@ static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr) return -EFAULT; stats.n_stats = n_stats; - data = vzalloc(array_size(n_stats, sizeof(u64))); - if (n_stats && !data) - return -ENOMEM; - if (dev->phydev && !ops->get_ethtool_phy_stats) { - ret = phy_ethtool_get_stats(dev->phydev, &stats, data); - if (ret < 0) - return ret; + if (n_stats) { + data = vzalloc(array_size(n_stats, sizeof(u64))); + if (!data) + return -ENOMEM; + + if (dev->phydev && !ops->get_ethtool_phy_stats) { + ret = phy_ethtool_get_stats(dev->phydev, &stats, data); + if (ret < 0) + goto out; + } else { + ops->get_ethtool_phy_stats(dev, &stats, data); + } } else { - ops->get_ethtool_phy_stats(dev, &stats, data); + data = NULL; } ret = -EFAULT; -- 2.19.1