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

Daniil Tatianin d-tatianin at yandex-team.ru
Sat Nov 26 01:38:08 MSK 2022


On 11/25/22 10:11 PM, Andrew Lunn wrote:
> 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.
I did experiment with that for a bit at the start, couldn't come up with 
a clear solution right away so went with this instead.

I think it's clearer than the original because you no longer have 
multiple convoluted if statements checking a bunch of different pointers 
(which introduced a logic error as a result in the original).

I'll try giving your approach another shot and see if I can come up with 
anything.

Thanks!



More information about the lvc-project mailing list