[lvc-project] [PATCH v1 3/3] net/ethtool/ioctl: correct & simplify ethtool_get_phy_stats if checks

Andrew Lunn andrew at lunn.ch
Fri Nov 25 22:11:25 MSK 2022


On Fri, Nov 25, 2022 at 07:49:13PM +0300, Daniil Tatianin wrote:
> ops->get_ethtool_phy_stats was getting called in an else branch
> of ethtool_get_phy_stats() unconditionally without making sure
> it was actually present.
> 
> Refactor the if checks so that it's more obvious what's going on and
> avoid accidental NULL derefs.
> 
> Found by Linux Verification Center (linuxtesting.org) with the SVACE
> static analysis tool.
> 
> Signed-off-by: Daniil Tatianin <d-tatianin at yandex-team.ru>
> ---
>  net/ethtool/ioctl.c | 23 ++++++++++++-----------
>  1 file changed, 12 insertions(+), 11 deletions(-)
> 
> diff --git a/net/ethtool/ioctl.c b/net/ethtool/ioctl.c
> index f83118c68e20..2b01e0042e6e 100644
> --- a/net/ethtool/ioctl.c
> +++ b/net/ethtool/ioctl.c
> @@ -2076,25 +2076,27 @@ static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
>  {
>  	const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
>  	const struct ethtool_ops *ops = dev->ethtool_ops;
> +	bool has_phy_stats_ops = ops->get_ethtool_phy_stats != NULL;
>  	struct phy_device *phydev = dev->phydev;
>  	struct ethtool_stats stats;
>  	u64 *data;
>  	int ret, n_stats;
>  
> -	if (!phydev && (!ops->get_ethtool_phy_stats || !ops->get_sset_count))
> -		return -EOPNOTSUPP;
> +	if (!phydev || !phy_ops) {
> +		if (!ops->get_sset_count)
> +			return -EOPNOTSUPP;
>  
> -	if (phydev && !ops->get_ethtool_phy_stats &&
> -	    phy_ops && phy_ops->get_sset_count)
> -		n_stats = phy_ops->get_sset_count(phydev);
> -	else
>  		n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
> +	} else {
> +		n_stats = phy_ops->get_sset_count(phydev);
> +		has_phy_stats_ops |= phy_ops->get_stats != NULL;

I'm not sure this is actually any clearer. You are mixing together
ethtool ops and phy ops.

This is part of why i suggested splitting phydev and !phydev into
helpers. The tests become a lot simpler. Please try it and see what
the resulting code looks like.

    Andrew



More information about the lvc-project mailing list