Skip to content

Commit 7b07be1

Browse files
gal-pressmanPaolo Abeni
authored andcommitted
ethtool: Avoid overflowing userspace buffer on stats query
The ethtool -S command operates across three ioctl calls: ETHTOOL_GSSET_INFO for the size, ETHTOOL_GSTRINGS for the names, and ETHTOOL_GSTATS for the values. If the number of stats changes between these calls (e.g., due to device reconfiguration), userspace's buffer allocation will be incorrect, potentially leading to buffer overflow. Drivers are generally expected to maintain stable stat counts, but some drivers (e.g., mlx5, bnx2x, bna, ksz884x) use dynamic counters, making this scenario possible. Some drivers try to handle this internally: - bnad_get_ethtool_stats() returns early in case stats.n_stats is not equal to the driver's stats count. - micrel/ksz884x also makes sure not to write anything beyond stats.n_stats and overflow the buffer. However, both use stats.n_stats which is already assigned with the value returned from get_sset_count(), hence won't solve the issue described here. Change ethtool_get_strings(), ethtool_get_stats(), ethtool_get_phy_stats() to not return anything in case of a mismatch between userspace's size and get_sset_size(), to prevent buffer overflow. The returned n_stats value will be equal to zero, to reflect that nothing has been returned. This could result in one of two cases when using upstream ethtool, depending on when the size change is detected: 1. When detected in ethtool_get_strings(): # ethtool -S eth2 no stats available 2. When detected in get stats, all stats will be reported as zero. Both cases are presumably transient, and a subsequent ethtool call should succeed. Other than the overflow avoidance, these two cases are very evident (no output/cleared stats), which is arguably better than presenting incorrect/shifted stats. I also considered returning an error instead of a "silent" response, but that seems more destructive towards userspace apps. Notes: - This patch does not claim to fix the inherent race, it only makes sure that we do not overflow the userspace buffer, and makes for a more predictable behavior. - RTNL lock is held during each ioctl, the race window exists between the separate ioctl calls when the lock is released. - Userspace ethtool always fills stats.n_stats, but it is likely that these stats ioctls are implemented in other userspace applications which might not fill it. The added code checks that it's not zero, to prevent any regressions. Fixes: 1da177e ("Linux-2.6.12-rc2") Reviewed-by: Dragos Tatulea <dtatulea@nvidia.com> Reviewed-by: Tariq Toukan <tariqt@nvidia.com> Signed-off-by: Gal Pressman <gal@nvidia.com> Link: https://patch.msgid.link/20251208121901.3203692-1-gal@nvidia.com Signed-off-by: Paolo Abeni <pabeni@redhat.com>
1 parent 885beba commit 7b07be1

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

net/ethtool/ioctl.c

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2383,7 +2383,10 @@ static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
23832383
return -ENOMEM;
23842384
WARN_ON_ONCE(!ret);
23852385

2386-
gstrings.len = ret;
2386+
if (gstrings.len && gstrings.len != ret)
2387+
gstrings.len = 0;
2388+
else
2389+
gstrings.len = ret;
23872390

23882391
if (gstrings.len) {
23892392
data = vzalloc(array_size(gstrings.len, ETH_GSTRING_LEN));
@@ -2509,10 +2512,13 @@ static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
25092512
if (copy_from_user(&stats, useraddr, sizeof(stats)))
25102513
return -EFAULT;
25112514

2512-
stats.n_stats = n_stats;
2515+
if (stats.n_stats && stats.n_stats != n_stats)
2516+
stats.n_stats = 0;
2517+
else
2518+
stats.n_stats = n_stats;
25132519

2514-
if (n_stats) {
2515-
data = vzalloc(array_size(n_stats, sizeof(u64)));
2520+
if (stats.n_stats) {
2521+
data = vzalloc(array_size(stats.n_stats, sizeof(u64)));
25162522
if (!data)
25172523
return -ENOMEM;
25182524
ops->get_ethtool_stats(dev, &stats, data);
@@ -2524,7 +2530,9 @@ static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
25242530
if (copy_to_user(useraddr, &stats, sizeof(stats)))
25252531
goto out;
25262532
useraddr += sizeof(stats);
2527-
if (n_stats && copy_to_user(useraddr, data, array_size(n_stats, sizeof(u64))))
2533+
if (stats.n_stats &&
2534+
copy_to_user(useraddr, data,
2535+
array_size(stats.n_stats, sizeof(u64))))
25282536
goto out;
25292537
ret = 0;
25302538

@@ -2560,6 +2568,10 @@ static int ethtool_get_phy_stats_phydev(struct phy_device *phydev,
25602568
return -EOPNOTSUPP;
25612569

25622570
n_stats = phy_ops->get_sset_count(phydev);
2571+
if (stats->n_stats && stats->n_stats != n_stats) {
2572+
stats->n_stats = 0;
2573+
return 0;
2574+
}
25632575

25642576
ret = ethtool_vzalloc_stats_array(n_stats, data);
25652577
if (ret)
@@ -2580,6 +2592,10 @@ static int ethtool_get_phy_stats_ethtool(struct net_device *dev,
25802592
return -EOPNOTSUPP;
25812593

25822594
n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
2595+
if (stats->n_stats && stats->n_stats != n_stats) {
2596+
stats->n_stats = 0;
2597+
return 0;
2598+
}
25832599

25842600
ret = ethtool_vzalloc_stats_array(n_stats, data);
25852601
if (ret)
@@ -2616,7 +2632,9 @@ static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
26162632
}
26172633

26182634
useraddr += sizeof(stats);
2619-
if (copy_to_user(useraddr, data, array_size(stats.n_stats, sizeof(u64))))
2635+
if (stats.n_stats &&
2636+
copy_to_user(useraddr, data,
2637+
array_size(stats.n_stats, sizeof(u64))))
26202638
ret = -EFAULT;
26212639

26222640
out:

0 commit comments

Comments
 (0)