From: Andrew Lunn <andrew@lunn.ch>
To: Daniil Tatianin <d-tatianin@yandex-team.ru>
Cc: netdev@vger.kernel.org, Michal Kubecek <mkubecek@suse.cz>,
yc-core@yandex-team.ru, lvc-project@linuxtesting.org
Subject: Re: [PATCH v1 3/3] net/ethtool/ioctl: correct & simplify ethtool_get_phy_stats if checks
Date: Fri, 25 Nov 2022 20:11:25 +0100 [thread overview]
Message-ID: <Y4ETXbZn3wSnZbfh@lunn.ch> (raw)
In-Reply-To: <20221125164913.360082-4-d-tatianin@yandex-team.ru>
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@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
next prev parent reply other threads:[~2022-11-25 19:11 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20221125164913.360082-1-d-tatianin@yandex-team.ru>
[not found] ` <20221125164913.360082-2-d-tatianin@yandex-team.ru>
2022-11-25 19:02 ` [PATCH v1 1/3] net/ethtool/ioctl: return -EOPNOTSUPP if we have no phy stats Andrew Lunn
[not found] ` <20221125164913.360082-4-d-tatianin@yandex-team.ru>
2022-11-25 19:11 ` Andrew Lunn [this message]
[not found] ` <55705e49-4b35-59be-5e41-7454dd12a0a4@yandex-team.ru>
2022-11-25 23:25 ` [PATCH v1 3/3] net/ethtool/ioctl: correct & simplify ethtool_get_phy_stats if checks Andrew Lunn
[not found] ` <11169dbe-2a26-6f31-3be6-f0439bb861f1@yandex-team.ru>
2022-11-28 13:35 ` Andrew Lunn
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Y4ETXbZn3wSnZbfh@lunn.ch \
--to=andrew@lunn.ch \
--cc=d-tatianin@yandex-team.ru \
--cc=lvc-project@linuxtesting.org \
--cc=mkubecek@suse.cz \
--cc=netdev@vger.kernel.org \
--cc=yc-core@yandex-team.ru \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).