netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net] net: mdio: validate parameter addr in mdiobus_get_phy()
@ 2023-01-15 10:54 Heiner Kallweit
  2023-01-15 16:30 ` Andrew Lunn
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Heiner Kallweit @ 2023-01-15 10:54 UTC (permalink / raw)
  To: Andrew Lunn, Russell King - ARM Linux, David Miller,
	Jakub Kicinski, Eric Dumazet, Paolo Abeni
  Cc: netdev

The caller may pass any value as addr, what may result in an out-of-bounds
access to array mdio_map. One existing case is stmmac_init_phy() that
may pass -1 as addr. Therefore validate addr before using it.

Fixes: 7f854420fbfe ("phy: Add API for {un}registering an mdio device to a bus.")
Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>
---
 drivers/net/phy/mdio_bus.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/drivers/net/phy/mdio_bus.c b/drivers/net/phy/mdio_bus.c
index 902e1c88e..132dd1f90 100644
--- a/drivers/net/phy/mdio_bus.c
+++ b/drivers/net/phy/mdio_bus.c
@@ -108,7 +108,12 @@ EXPORT_SYMBOL(mdiobus_unregister_device);
 
 struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr)
 {
-	struct mdio_device *mdiodev = bus->mdio_map[addr];
+	struct mdio_device *mdiodev;
+
+	if (addr < 0 || addr >= ARRAY_SIZE(bus->mdio_map))
+		return NULL;
+
+	mdiodev = bus->mdio_map[addr];
 
 	if (!mdiodev)
 		return NULL;
-- 
2.39.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH net] net: mdio: validate parameter addr in mdiobus_get_phy()
  2023-01-15 10:54 [PATCH net] net: mdio: validate parameter addr in mdiobus_get_phy() Heiner Kallweit
@ 2023-01-15 16:30 ` Andrew Lunn
  2023-01-17 11:31 ` Paolo Abeni
  2023-01-17 11:50 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 5+ messages in thread
From: Andrew Lunn @ 2023-01-15 16:30 UTC (permalink / raw)
  To: Heiner Kallweit
  Cc: Russell King - ARM Linux, David Miller, Jakub Kicinski,
	Eric Dumazet, Paolo Abeni, netdev

On Sun, Jan 15, 2023 at 11:54:06AM +0100, Heiner Kallweit wrote:
> The caller may pass any value as addr, what may result in an out-of-bounds
> access to array mdio_map. One existing case is stmmac_init_phy() that
> may pass -1 as addr. Therefore validate addr before using it.
> 
> Fixes: 7f854420fbfe ("phy: Add API for {un}registering an mdio device to a bus.")
> Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>

Hi Heiner

This makes sense, but we should also fix stmmac to not actually do
this, since it is clearly wrong, and probably indicates a
configuration problem for that driver.

Reviewed-by: Andrew Lunn <andrew@lunn.ch>

    Andrew

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH net] net: mdio: validate parameter addr in mdiobus_get_phy()
  2023-01-15 10:54 [PATCH net] net: mdio: validate parameter addr in mdiobus_get_phy() Heiner Kallweit
  2023-01-15 16:30 ` Andrew Lunn
@ 2023-01-17 11:31 ` Paolo Abeni
  2023-01-17 20:43   ` Heiner Kallweit
  2023-01-17 11:50 ` patchwork-bot+netdevbpf
  2 siblings, 1 reply; 5+ messages in thread
From: Paolo Abeni @ 2023-01-17 11:31 UTC (permalink / raw)
  To: Heiner Kallweit, Andrew Lunn, Russell King - ARM Linux,
	David Miller, Jakub Kicinski, Eric Dumazet
  Cc: netdev

On Sun, 2023-01-15 at 11:54 +0100, Heiner Kallweit wrote:
> The caller may pass any value as addr, what may result in an out-of-bounds
> access to array mdio_map. One existing case is stmmac_init_phy() that
> may pass -1 as addr. Therefore validate addr before using it.
> 
> Fixes: 7f854420fbfe ("phy: Add API for {un}registering an mdio device to a bus.")
> Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>
> ---
>  drivers/net/phy/mdio_bus.c | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/net/phy/mdio_bus.c b/drivers/net/phy/mdio_bus.c
> index 902e1c88e..132dd1f90 100644
> --- a/drivers/net/phy/mdio_bus.c
> +++ b/drivers/net/phy/mdio_bus.c
> @@ -108,7 +108,12 @@ EXPORT_SYMBOL(mdiobus_unregister_device);
>  
>  struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr)
>  {
> -	struct mdio_device *mdiodev = bus->mdio_map[addr];
> +	struct mdio_device *mdiodev;
> +
> +	if (addr < 0 || addr >= ARRAY_SIZE(bus->mdio_map))
> +		return NULL;

Speaking of possible follow-ups, would it make sense to add a
WARN_ON_ONCE() or similar on the above condition?

Thanks!

Paolo
> 


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH net] net: mdio: validate parameter addr in mdiobus_get_phy()
  2023-01-15 10:54 [PATCH net] net: mdio: validate parameter addr in mdiobus_get_phy() Heiner Kallweit
  2023-01-15 16:30 ` Andrew Lunn
  2023-01-17 11:31 ` Paolo Abeni
@ 2023-01-17 11:50 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 5+ messages in thread
From: patchwork-bot+netdevbpf @ 2023-01-17 11:50 UTC (permalink / raw)
  To: Heiner Kallweit; +Cc: andrew, linux, davem, kuba, edumazet, pabeni, netdev

Hello:

This patch was applied to netdev/net.git (master)
by Paolo Abeni <pabeni@redhat.com>:

On Sun, 15 Jan 2023 11:54:06 +0100 you wrote:
> The caller may pass any value as addr, what may result in an out-of-bounds
> access to array mdio_map. One existing case is stmmac_init_phy() that
> may pass -1 as addr. Therefore validate addr before using it.
> 
> Fixes: 7f854420fbfe ("phy: Add API for {un}registering an mdio device to a bus.")
> Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>
> 
> [...]

Here is the summary with links:
  - [net] net: mdio: validate parameter addr in mdiobus_get_phy()
    https://git.kernel.org/netdev/net/c/867dbe784c50

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH net] net: mdio: validate parameter addr in mdiobus_get_phy()
  2023-01-17 11:31 ` Paolo Abeni
@ 2023-01-17 20:43   ` Heiner Kallweit
  0 siblings, 0 replies; 5+ messages in thread
From: Heiner Kallweit @ 2023-01-17 20:43 UTC (permalink / raw)
  To: Paolo Abeni, Andrew Lunn, Russell King - ARM Linux, David Miller,
	Jakub Kicinski, Eric Dumazet
  Cc: netdev

On 17.01.2023 12:31, Paolo Abeni wrote:
> On Sun, 2023-01-15 at 11:54 +0100, Heiner Kallweit wrote:
>> The caller may pass any value as addr, what may result in an out-of-bounds
>> access to array mdio_map. One existing case is stmmac_init_phy() that
>> may pass -1 as addr. Therefore validate addr before using it.
>>
>> Fixes: 7f854420fbfe ("phy: Add API for {un}registering an mdio device to a bus.")
>> Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>
>> ---
>>  drivers/net/phy/mdio_bus.c | 7 ++++++-
>>  1 file changed, 6 insertions(+), 1 deletion(-)
>>
>> diff --git a/drivers/net/phy/mdio_bus.c b/drivers/net/phy/mdio_bus.c
>> index 902e1c88e..132dd1f90 100644
>> --- a/drivers/net/phy/mdio_bus.c
>> +++ b/drivers/net/phy/mdio_bus.c
>> @@ -108,7 +108,12 @@ EXPORT_SYMBOL(mdiobus_unregister_device);
>>  
>>  struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr)
>>  {
>> -	struct mdio_device *mdiodev = bus->mdio_map[addr];
>> +	struct mdio_device *mdiodev;
>> +
>> +	if (addr < 0 || addr >= ARRAY_SIZE(bus->mdio_map))
>> +		return NULL;
> 
> Speaking of possible follow-ups, would it make sense to add a
> WARN_ON_ONCE() or similar on the above condition?
> 
Yes, I think that's a good idea.
I'll send a follow-up patch.

> Thanks!
> 
> Paolo
>>
> 


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-01-17 22:02 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-15 10:54 [PATCH net] net: mdio: validate parameter addr in mdiobus_get_phy() Heiner Kallweit
2023-01-15 16:30 ` Andrew Lunn
2023-01-17 11:31 ` Paolo Abeni
2023-01-17 20:43   ` Heiner Kallweit
2023-01-17 11:50 ` patchwork-bot+netdevbpf

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).