Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp2521176yba; Mon, 15 Apr 2019 13:28:55 -0700 (PDT) X-Google-Smtp-Source: APXvYqwUuGKK/XJNt//58Ud23l6f6w9bizgCUE5vNKO2975DV+X/7rgsJ8aIdJ2iyfi5ig3JgxPG X-Received: by 2002:a62:292:: with SMTP id 140mr78611138pfc.206.1555360135269; Mon, 15 Apr 2019 13:28:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555360135; cv=none; d=google.com; s=arc-20160816; b=SNP5WPzeYBl52K8CbP7RMXcBlxMVqy6TD9E0fhpAmvgsxg8gi5B5ZKDzF2PTg4df2k JmGoVIQWAGRBfF8RF1L6ASvf/bquxVUW3X6vfAHNTjvh2LinMDWhz73uR6SgA6lWQbIL LzZb2IaQbJNWU49MepbZGrmJoYW5E35M1BcIxGGsWHwB8IqqrDSsF9488URHflv4O2GX EmbDPxJTQcoELP5HiKu5af8EFNBLgjFsH35ksLbOjckQFErsxKlKsHeSMet4LgvxUGQq qIFpIN3cbC9QS6Da3MWjtds+kI2HOhxscLRPwV891SbomfD+s3V3zuMTD+gtd3/kErO3 /1CQ== 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=+xUdWVB73Zt3vU/SL6/Dt4EsSnBEv54KbB9p+LLnJNU=; b=TWKn0hmLaq8skkxKLgnqUf+W3+5LN2mjTjqnI4fe05PbfCM7FRmFl2M9Yc7AB85SGw GUXvX2bXu0AUPNEoatHhcLf9kxCTn2holxYE/kPQxRodr4xNFc/8p99O3RZ8IIR2lP8n aauk4NzNo5g3a5jObxykAM2UKlGXXxOwXbHqCTelLdmad2vmNf2u6tkEZ+TS5MnUgA2K qz+mQBt4mZDhVbhBSG8bzYydS/XVaTDGAO+qcpkXpJMYI6pWbt56OmfWHZs8OfRFemWY UcLe3p2FNFEhHsYFnd+XbK/0h98SYEqEndX0AbMpwxlfwg5M814NwA1fhudNYTGByn+3 e7cA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Ihb90o82; 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 4si30731286pfp.185.2019.04.15.13.28.39; Mon, 15 Apr 2019 13:28:55 -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=Ihb90o82; 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 S1729071AbfDOTC6 (ORCPT + 99 others); Mon, 15 Apr 2019 15:02:58 -0400 Received: from mail.kernel.org ([198.145.29.99]:34328 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729048AbfDOTCz (ORCPT ); Mon, 15 Apr 2019 15:02:55 -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 130812087C; Mon, 15 Apr 2019 19:02:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1555354974; bh=zNdLVdzahmNhp/Do/aeElOZd3or5toZWZOGereNie1o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ihb90o82ZyHGcbwCT/ZobKTwYUBb2JKk8vxMIvXTcuGXfMg1O0eqCLJaEYLuQwcW8 xBi/RkWGhWiNyNKD+gFrNRu1GdmkBHOZQjJC6g/avWHCu3kquajE+31yGEYJeV50Vt JSDcW1oTwsRLW/IkXEN3A9skPlOjpW9C6yMaKdQs= 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.14 35/69] net: ethtool: not call vzalloc for zero sized memory request Date: Mon, 15 Apr 2019 20:58:53 +0200 Message-Id: <20190415183732.029772029@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190415183726.036654568@linuxfoundation.org> References: <20190415183726.036654568@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 | 43 +++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-) --- a/net/core/ethtool.c +++ b/net/core/ethtool.c @@ -1815,11 +1815,15 @@ static int ethtool_get_strings(struct ne WARN_ON_ONCE(!ret); gstrings.len = ret; - data = vzalloc(gstrings.len * ETH_GSTRING_LEN); - if (gstrings.len && !data) - return -ENOMEM; + if (gstrings.len) { + data = vzalloc(gstrings.len * ETH_GSTRING_LEN); + 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))) @@ -1915,11 +1919,14 @@ static int ethtool_get_stats(struct net_ return -EFAULT; stats.n_stats = n_stats; - data = vzalloc(n_stats * sizeof(u64)); - if (n_stats && !data) - return -ENOMEM; - - ops->get_ethtool_stats(dev, &stats, data); + if (n_stats) { + data = vzalloc(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))) @@ -1955,13 +1962,17 @@ static int ethtool_get_phy_stats(struct return -EFAULT; stats.n_stats = n_stats; - data = vzalloc(n_stats * sizeof(u64)); - if (n_stats && !data) - return -ENOMEM; - - mutex_lock(&phydev->lock); - phydev->drv->get_stats(phydev, &stats, data); - mutex_unlock(&phydev->lock); + if (n_stats) { + data = vzalloc(n_stats * sizeof(u64)); + 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)))