diff mbox series

[net,v2] net: phy: mscc-miim: Validate bus frequency obtained from Device Tree

Message ID 20240702130247.18515-1-amishin@t-argos.ru (mailing list archive)
State Changes Requested
Delegated to: Netdev Maintainers
Headers show
Series [net,v2] net: phy: mscc-miim: Validate bus frequency obtained from Device Tree | expand

Checks

Context Check Description
netdev/series_format success Single patches do not need cover letters
netdev/tree_selection success Clearly marked for net
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag present in non-next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 856 this patch: 856
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers success CCed 8 of 8 maintainers
netdev/build_clang success Errors and warnings before: 860 this patch: 860
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 860 this patch: 860
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 11 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
netdev/contest fail net-next-2024-07-02--15-00 (tests: 665)

Commit Message

Aleksandr Mishin July 2, 2024, 1:02 p.m. UTC
In mscc_miim_clk_set() miim->bus_freq is taken from Device Tree and can
contain any value in case of any error or broken DT. A value of 2147483648
multiplied by 2 will result in an overflow and division by 0.

Add bus frequency value check to avoid overflow.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes: bb2a1934ca01 ("net: phy: mscc-miim: add support to set MDIO bus frequency")
Suggested-by: Michael Walle <michael@walle.cc>
Signed-off-by: Aleksandr Mishin <amishin@t-argos.ru>
---
v1->v2: Detect overflow event instead of using magic numbers as suggested by Michael

 drivers/net/mdio/mdio-mscc-miim.c | 5 +++++
 1 file changed, 5 insertions(+)

Comments

Michael Walle July 2, 2024, 1:16 p.m. UTC | #1
Hi,

Please post a new version of a patch at the earliest after 24 hours,
so reviewers got a chance to reply.

On Tue Jul 2, 2024 at 3:02 PM CEST, Aleksandr Mishin wrote:
> In mscc_miim_clk_set() miim->bus_freq is taken from Device Tree and can
> contain any value in case of any error or broken DT. A value of 2147483648
> multiplied by 2 will result in an overflow and division by 0.
>
> Add bus frequency value check to avoid overflow.
>
> Found by Linux Verification Center (linuxtesting.org) with SVACE.
>
> Fixes: bb2a1934ca01 ("net: phy: mscc-miim: add support to set MDIO bus frequency")
> Suggested-by: Michael Walle <michael@walle.cc>
> Signed-off-by: Aleksandr Mishin <amishin@t-argos.ru>
> ---
> v1->v2: Detect overflow event instead of using magic numbers as suggested by Michael
>
>  drivers/net/mdio/mdio-mscc-miim.c | 5 +++++
>  1 file changed, 5 insertions(+)
>
> diff --git a/drivers/net/mdio/mdio-mscc-miim.c b/drivers/net/mdio/mdio-mscc-miim.c
> index c29377c85307..b344d0b03d8e 100644
> --- a/drivers/net/mdio/mdio-mscc-miim.c
> +++ b/drivers/net/mdio/mdio-mscc-miim.c
> @@ -254,6 +254,11 @@ static int mscc_miim_clk_set(struct mii_bus *bus)
>  	if (!miim->bus_freq)
>  		return 0;
>  
> +	if (2 * miim->bus_freq == 0) {
> +		dev_err(&bus->dev, "Incorrect bus frequency\n");
> +		return -EINVAL;
> +	}

DRY. Save it in a variable and use it in DIV_ROUND_UP(). Also you
just check for the div-by-zero, but what if the value will overflow
beyond zero? The calculation will be wrong, right?

There's also check_mul_overflow() but not sure that's encouraged in
netdev.

-michael

> +
>  	rate = clk_get_rate(miim->clk);
>  
>  	div = DIV_ROUND_UP(rate, 2 * miim->bus_freq) - 1;
Andrew Lunn July 2, 2024, 1:40 p.m. UTC | #2
On Tue, Jul 02, 2024 at 03:16:21PM +0200, Michael Walle wrote:
> Hi,
> 
> Please post a new version of a patch at the earliest after 24 hours,
> so reviewers got a chance to reply.

Just adding to that, please also read:

https://www.kernel.org/doc/html/latest/process/maintainer-netdev.html

	Andrew
diff mbox series

Patch

diff --git a/drivers/net/mdio/mdio-mscc-miim.c b/drivers/net/mdio/mdio-mscc-miim.c
index c29377c85307..b344d0b03d8e 100644
--- a/drivers/net/mdio/mdio-mscc-miim.c
+++ b/drivers/net/mdio/mdio-mscc-miim.c
@@ -254,6 +254,11 @@  static int mscc_miim_clk_set(struct mii_bus *bus)
 	if (!miim->bus_freq)
 		return 0;
 
+	if (2 * miim->bus_freq == 0) {
+		dev_err(&bus->dev, "Incorrect bus frequency\n");
+		return -EINVAL;
+	}
+
 	rate = clk_get_rate(miim->clk);
 
 	div = DIV_ROUND_UP(rate, 2 * miim->bus_freq) - 1;