* [PATCH v2] net/ethtool/ioctl: ensure that we have phy ops before using them
@ 2022-11-22 7:21 Daniil Tatianin
2022-11-22 17:37 ` Andrew Lunn
2022-11-23 16:06 ` Alexander Lobakin
0 siblings, 2 replies; 4+ messages in thread
From: Daniil Tatianin @ 2022-11-22 7:21 UTC (permalink / raw)
To: netdev; +Cc: Daniil Tatianin, Andrew Lunn, Michal Kubecek, Jakub Kicinski
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 checks to avoid unnecessary nesting and make them more
readable. Add an extra WARN_ON_ONCE(1) to emit a warning when a driver
declares that it has phy stats without a way to retrieve them.
Found by Linux Verification Center (linuxtesting.org) with the SVACE
static analysis tool.
Suggested-by: Jakub Kicinski <kuba@kernel.org>
Signed-off-by: Daniil Tatianin <d-tatianin@yandex-team.ru>
---
net/ethtool/ioctl.c | 31 ++++++++++++++++++-------------
1 file changed, 18 insertions(+), 13 deletions(-)
diff --git a/net/ethtool/ioctl.c b/net/ethtool/ioctl.c
index 57e7238a4136..04f9ba98b038 100644
--- a/net/ethtool/ioctl.c
+++ b/net/ethtool/ioctl.c
@@ -2100,23 +2100,28 @@ static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
stats.n_stats = n_stats;
- if (n_stats) {
- data = vzalloc(array_size(n_stats, sizeof(u64)));
- if (!data)
- return -ENOMEM;
+ if (!n_stats) {
+ data = NULL;
+ goto copy_back;
+ }
- if (phydev && !ops->get_ethtool_phy_stats &&
- phy_ops && phy_ops->get_stats) {
- ret = phy_ops->get_stats(phydev, &stats, data);
- if (ret < 0)
- goto out;
- } else {
- ops->get_ethtool_phy_stats(dev, &stats, data);
- }
+ data = vzalloc(array_size(n_stats, sizeof(u64)));
+ if (!data)
+ return -ENOMEM;
+
+ if (ops->get_ethtool_phy_stats) {
+ ops->get_ethtool_phy_stats(dev, &stats, data);
+ } else if (phydev && phy_ops && phy_ops->get_stats) {
+ ret = phy_ops->get_stats(phydev, &stats, data);
+ if (ret < 0)
+ goto out;
} else {
- data = NULL;
+ WARN_ON_ONCE(1);
+ n_stats = 0;
+ stats.n_stats = 0;
}
+copy_back:
ret = -EFAULT;
if (copy_to_user(useraddr, &stats, sizeof(stats)))
goto out;
--
2.25.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH v2] net/ethtool/ioctl: ensure that we have phy ops before using them
2022-11-22 7:21 [PATCH v2] net/ethtool/ioctl: ensure that we have phy ops before using them Daniil Tatianin
@ 2022-11-22 17:37 ` Andrew Lunn
2022-11-23 16:06 ` Alexander Lobakin
1 sibling, 0 replies; 4+ messages in thread
From: Andrew Lunn @ 2022-11-22 17:37 UTC (permalink / raw)
To: Daniil Tatianin; +Cc: netdev, Michal Kubecek, Jakub Kicinski
On Tue, Nov 22, 2022 at 10:21:43AM +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 checks to avoid unnecessary nesting and make them more
> readable. Add an extra WARN_ON_ONCE(1) to emit a warning when a driver
> declares that it has phy stats without a way to retrieve them.
So i have two different suggestions here, take your pick and even
merge them together.
I wonder if we can simply this some more. If there are 0 stats we
already issue a WARN_ON_ONCE():
https://elixir.bootlin.com/linux/v6.1-rc6/source/net/ethtool/ioctl.c#L2096
We will then copy back to user space the ethtool_stats and zero
statistics and return 0. If that useful? Would it make more sense to
just return -EOPNOTSUPP after the WARN_ON_ONCE()?
That would be patch 1/X.
Patch 2/X would then remove the if (n_stats) code, but otherwise make
no changes. That keeps the patch simple to review.
Patch 3/X would then add the additional verification of
ops->get_ethtool_phy_stats(). But do it at the top of the function,
along with all the other verification, and return -EOPNOTSUPP.
Alternatively, given the complexity of the checking and the function
as a whole, i'm wondering if it make sense to actually pull this
function apart. Add a ethtool_get_phy_stats_phydev() and
ethtool_get_phy_stats_ethtool(), and have ethtool_get_phy_stats() do
the copy_from_user(), call one of the two helpers, and if they don't
return an error do the two copy_to_user().
Andrew
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v2] net/ethtool/ioctl: ensure that we have phy ops before using them
2022-11-22 7:21 [PATCH v2] net/ethtool/ioctl: ensure that we have phy ops before using them Daniil Tatianin
2022-11-22 17:37 ` Andrew Lunn
@ 2022-11-23 16:06 ` Alexander Lobakin
1 sibling, 0 replies; 4+ messages in thread
From: Alexander Lobakin @ 2022-11-23 16:06 UTC (permalink / raw)
To: Daniil Tatianin
Cc: Alexander Lobakin, netdev, Andrew Lunn, Michal Kubecek, Jakub Kicinski
From: Daniil Tatianin <d-tatianin@yandex-team.ru>
Date: Tue, 22 Nov 2022 10:21:43 +0300
> 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 checks to avoid unnecessary nesting and make them more
> readable. Add an extra WARN_ON_ONCE(1) to emit a warning when a driver
> declares that it has phy stats without a way to retrieve them.
>
> Found by Linux Verification Center (linuxtesting.org) with the SVACE
> static analysis tool.
>
> Suggested-by: Jakub Kicinski <kuba@kernel.org>
> Signed-off-by: Daniil Tatianin <d-tatianin@yandex-team.ru>
> ---
> net/ethtool/ioctl.c | 31 ++++++++++++++++++-------------
> 1 file changed, 18 insertions(+), 13 deletions(-)
>
> diff --git a/net/ethtool/ioctl.c b/net/ethtool/ioctl.c
> index 57e7238a4136..04f9ba98b038 100644
> --- a/net/ethtool/ioctl.c
> +++ b/net/ethtool/ioctl.c
> @@ -2100,23 +2100,28 @@ static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
>
> stats.n_stats = n_stats;
>
> - if (n_stats) {
> - data = vzalloc(array_size(n_stats, sizeof(u64)));
> - if (!data)
> - return -ENOMEM;
> + if (!n_stats) {
> + data = NULL;
> + goto copy_back;
> + }
>
> - if (phydev && !ops->get_ethtool_phy_stats &&
> - phy_ops && phy_ops->get_stats) {
> - ret = phy_ops->get_stats(phydev, &stats, data);
> - if (ret < 0)
> - goto out;
> - } else {
> - ops->get_ethtool_phy_stats(dev, &stats, data);
> - }
> + data = vzalloc(array_size(n_stats, sizeof(u64)));
> + if (!data)
> + return -ENOMEM;
> +
> + if (ops->get_ethtool_phy_stats) {
> + ops->get_ethtool_phy_stats(dev, &stats, data);
I'd first check for the callback and only after allocate the array,
otherwise there's no optimization in here.
Also, I'd separate saving 1 level of indent from the functional
changes.
> + } else if (phydev && phy_ops && phy_ops->get_stats) {
> + ret = phy_ops->get_stats(phydev, &stats, data);
> + if (ret < 0)
> + goto out;
> } else {
> - data = NULL;
> + WARN_ON_ONCE(1);
> + n_stats = 0;
> + stats.n_stats = 0;
> }
>
> +copy_back:
> ret = -EFAULT;
> if (copy_to_user(useraddr, &stats, sizeof(stats)))
> goto out;
> --
> 2.25.1
Thanks,
Olek
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v2] net/ethtool/ioctl: ensure that we have phy ops before using them
[not found] <20221121140556.41763-1-d-tatianin@yandex-team.ru>
@ 2022-11-22 4:29 ` Jakub Kicinski
0 siblings, 0 replies; 4+ messages in thread
From: Jakub Kicinski @ 2022-11-22 4:29 UTC (permalink / raw)
To: Daniil Tatianin
Cc: David S. Miller, Eric Dumazet, Paolo Abeni, Hao Chen,
Guangbin Huang, Tom Rix, Julian Wiedmann, Marco Bonelli,
Wolfram Sang, netdev, linux-kernel, lvc-project, yc-core
On Mon, 21 Nov 2022 17:05:56 +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 checks to avoid unnecessary nesting and make them more
> readable. Add an extra WARN_ON_ONCE(1) to emit a warning when a driver
> declares that it has phy stats without a way to retrieve them.
>
> Found by Linux Verification Center (linuxtesting.org) with the SVACE
> static analysis tool.
>
> Suggested-by: Jakub Kicinski <kuba@kernel.org>
> Signed-off-by: Daniil Tatianin <d-tatianin@yandex-team.ru>
Didn't make it to the list again :S Maybe try stripping the To/CC to
just netdev@, Andrew Lunn and Michal Kubecek?
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2022-11-23 16:07 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-22 7:21 [PATCH v2] net/ethtool/ioctl: ensure that we have phy ops before using them Daniil Tatianin
2022-11-22 17:37 ` Andrew Lunn
2022-11-23 16:06 ` Alexander Lobakin
[not found] <20221121140556.41763-1-d-tatianin@yandex-team.ru>
2022-11-22 4:29 ` Jakub Kicinski
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).