提交 12f5a43c 编写于 作者: L Li RongQing 提交者: Xie XiuQi

net: ethtool: not call vzalloc for zero sized memory request

mainline inclusion
from mainline-5.1
commit 3d883026
category: bugfix
bugzilla: 13607
CVE: NA

-------------------------------------------------

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: NLi RongQing <lirongqing@baidu.com>
Reviewed-by: NWang Li <wangli39@baidu.com>
Reviewed-by: NMichal Kubecek <mkubecek@suse.cz>
Signed-off-by: NDavid S. Miller <davem@davemloft.net>
Signed-off-by: NZhiqiang Liu <liuzhiqiang26@huawei.com>
Reviewed-by: NWenan Mao <maowenan@huawei.com>
Signed-off-by: NYang Yingliang <yangyingliang@huawei.com>
上级 23dda6fa
...@@ -1865,11 +1865,16 @@ static int ethtool_get_strings(struct net_device *dev, void __user *useraddr) ...@@ -1865,11 +1865,16 @@ static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
WARN_ON_ONCE(!ret); WARN_ON_ONCE(!ret);
gstrings.len = 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; ret = -EFAULT;
if (copy_to_user(useraddr, &gstrings, sizeof(gstrings))) if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
...@@ -1965,11 +1970,15 @@ static int ethtool_get_stats(struct net_device *dev, void __user *useraddr) ...@@ -1965,11 +1970,15 @@ static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
return -EFAULT; return -EFAULT;
stats.n_stats = n_stats; 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; ret = -EFAULT;
if (copy_to_user(useraddr, &stats, sizeof(stats))) if (copy_to_user(useraddr, &stats, sizeof(stats)))
...@@ -2009,16 +2018,21 @@ static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr) ...@@ -2009,16 +2018,21 @@ static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
return -EFAULT; return -EFAULT;
stats.n_stats = n_stats; 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) { if (n_stats) {
ret = phy_ethtool_get_stats(dev->phydev, &stats, data); data = vzalloc(array_size(n_stats, sizeof(u64)));
if (ret < 0) if (!data)
return ret; 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 { } else {
ops->get_ethtool_phy_stats(dev, &stats, data); data = NULL;
} }
ret = -EFAULT; ret = -EFAULT;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册