Skip to contentSkip to conversation

net: usb: asix: ax88772: Fix potential string cut

PATCH Applied Andy Shevchenko 3 participants 8 messages Mar 24, 2025 – Mar 25, 2025

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

v3 cover net: usb: asix: ax88772: Fix potential string cut

The agreement and also PHY_MAX_ADDR limit suggest that the PHY address can't occupy more than two hex digits. In some cases GCC complains about potential string cut. In course of fixing this, introduce the PHY_ID_SIZE predefined constant to make it easier for the users to know the bare minimum for the buffer that holds PHY ID string (patch 1). With that, fix the ASIX driver that triggers GCC accordingly (patch 2).

In v3:
- dropped format specifier changes (Russell, LKP)
- added predefined constant for a minimum buffer size (Russell)
- updated error message to refer to the address and not ID string (Russell)
- changed type of phy_addr to u8, otherwise GCC can't cope with its range

In v2:
- added first patch
- added a conditional to the ASIX driver (Andrew)

Andy Shevchenko (2):
net: phy: Introduce PHY_ID_SIZE — minimum size for PHY ID string
net: usb: asix: ax88772: Increase phy_name size

 drivers/net/usb/ax88172a.c | 12 ++++++++----
 include/linux/phy.h        |  1 +
 2 files changed, 9 insertions(+), 4 deletions(-)
Signature · 1 line
2.47.2
#1 lore raw

PATCH v3 1/2 net: phy: Introduce PHY_ID_SIZE — minimum size for PHY ID string

The PHY_ID_FMT defines the format specifier "%s:%02x" to form the PHY ID string, where the maximum of the first part is defined in MII_BUS_ID_SIZE, including NUL terminator, and the second part is implied to be 3 as the maximum address is limited to 32, meaning that 2 hex digits is more than enough, plus ':' (colon) delimiter. However, some drivers, which are using PHY_ID_FMT, customise buffer size and do that incorrectly. Introduce a new constant PHY_ID_SIZE that makes the minimum required size explicit, so drivers are encouraged to use it.

---
 include/linux/phy.h | 1 +
 1 file changed, 1 insertion(+)
include/linux/phy.h +1 -0
index 19f076a71f94..5bb8dfb3d15c 100644
@@ -310,6 +310,7 @@ static inline long rgmii_clock(int speed)
 
 /* Used when trying to connect to a specific phy (mii bus id:phy device id) */
 #define PHY_ID_FMT "%s:%02x"
+#define PHY_ID_SIZE	(MII_BUS_ID_SIZE + 3)
 
 #define MII_BUS_ID_SIZE	61
 
Signature · 1 line
2.47.2
reviewer Reviewed-by on v3 1/2
#2 lore raw

Re: [PATCH net v3 1/2] net: phy: Introduce PHY_ID_SIZE — minimum size for PHY ID string

Quoted 10 lines · Andy Shevchenko

The PHY_ID_FMT defines the format specifier "%s:%02x" to form the PHY ID string, where the maximum of the first part is defined in MII_BUS_ID_SIZE, including NUL terminator, and the second part is implied to be 3 as the maximum address is limited to 32, meaning that 2 hex digits is more than enough, plus ':' (colon) delimiter. However, some drivers, which are using PHY_ID_FMT, customise buffer size and do that incorrectly. Introduce a new constant PHY_ID_SIZE that makes the minimum required size explicit, so drivers are encouraged to use it.

Quoted 2 lines

Thanks!

Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!
#3 lore raw

Re: [PATCH net v3 1/2] net: phy: Introduce PHY_ID_SIZE — minimum size for PHY ID string

Quoted 11 lines · Russell King (Oracle)
Quoted 10 lines · Andy Shevchenko

The PHY_ID_FMT defines the format specifier "%s:%02x" to form the PHY ID string, where the maximum of the first part is defined in MII_BUS_ID_SIZE, including NUL terminator, and the second part is implied to be 3 as the maximum address is limited to 32, meaning that 2 hex digits is more than enough, plus ':' (colon) delimiter. However, some drivers, which are using PHY_ID_FMT, customise buffer size and do that incorrectly. Introduce a new constant PHY_ID_SIZE that makes the minimum required size explicit, so drivers are encouraged to use it.

Quoted 6 lines
Quoted 2 lines

Thanks!

Thank you!

And just a bit of offtopic, can you look at [email protected] and comment / apply?

That is one of the only few obstacles for me (and perhaps others, like CIs) to enable CONFIG_WERROR when build with make W=1 (implying existing defconfigs for x86).

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw

Re: [PATCH net v3 1/2] net: phy: Introduce PHY_ID_SIZE — minimum size for PHY ID string

Quoted 17 lines · Andy Shevchenko
Quoted 16 lines · Russell King (Oracle)
Quoted 10 lines · Andy Shevchenko

The PHY_ID_FMT defines the format specifier "%s:%02x" to form the PHY ID string, where the maximum of the first part is defined in MII_BUS_ID_SIZE, including NUL terminator, and the second part is implied to be 3 as the maximum address is limited to 32, meaning that 2 hex digits is more than enough, plus ':' (colon) delimiter. However, some drivers, which are using PHY_ID_FMT, customise buffer size and do that incorrectly. Introduce a new constant PHY_ID_SIZE that makes the minimum required size explicit, so drivers are encouraged to use it.

Quoted 2 lines
Quoted 7 lines
Quoted 1 line

Thanks!

Thank you!

And just a bit of offtopic, can you look at [email protected] and comment / apply?

That needs to go into my patch system please. Thanks.

Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!
#5 lore raw

Re: [PATCH net v3 1/2] net: phy: Introduce PHY_ID_SIZE — minimum size for PHY ID string

Quoted 2 lines · Russell King (Oracle)
Quoted 1 line · Andy Shevchenko

On Mon, Mar 24, 2025 at 03:06:22PM +0000, Russell King (Oracle) wrote:

...

Quoted 5 lines
Quoted 3 lines

And just a bit of offtopic, can you look at [email protected] and comment / apply?

That needs to go into my patch system please. Thanks.

Ah, cool, just made it to appear there.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#6 lore raw

PATCH v3 2/2 net: usb: asix: ax88772: Increase phy_name size

In reply to Andy Shevchenko

GCC compiler (Debian 14.2.0-17) is not happy about printing into a too short buffer (when build with make W=1):

drivers/net/usb/ax88172a.c:311:9: note: ‘snprintf’ output between 4 and 66 bytes into a destination of size 20

Indeed, the buffer size is chosen based on some assumptions, while in general the assigned name might not fit. Increase the buffer size to cover the minimum required one. With that, change snprintf() to use sizeof() instead of the hard coded value.

While at it, make sure that the PHY address is not bigger than the allowed maximum.

---
 drivers/net/usb/ax88172a.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
drivers/net/usb/ax88172a.c +8 -4
index e47bb125048d..f613e4bc68c8 100644
@@ -18,8 +18,8 @@
 struct ax88172a_private {
 	struct mii_bus *mdio;
 	struct phy_device *phydev;
-	char phy_name[20];
-	u16 phy_addr;
+	char phy_name[PHY_ID_SIZE];
+	u8 phy_addr;
 	u16 oldmode;
 	int use_embdphy;
 	struct asix_rx_fixup_info rx_fixup_info;
@@ -210,7 +210,11 @@ static int ax88172a_bind(struct usbnet *dev, struct usb_interface *intf)
 	ret = asix_read_phy_addr(dev, priv->use_embdphy);
 	if (ret < 0)
 		goto free;
-
+	if (ret >= PHY_MAX_ADDR) {
+		netdev_err(dev->net, "Invalid PHY address %#x\n", ret);
+		ret = -ENODEV;
+		goto free;
+	}
 	priv->phy_addr = ret;
 
 	ax88172a_reset_phy(dev, priv->use_embdphy);
@@ -308,7 +312,7 @@ static int ax88172a_reset(struct usbnet *dev)
 		   rx_ctl);
 
 	/* Connect to PHY */
-	snprintf(priv->phy_name, 20, PHY_ID_FMT,
+	snprintf(priv->phy_name, sizeof(priv->phy_name), PHY_ID_FMT,
 		 priv->mdio->id, priv->phy_addr);
 
 	priv->phydev = phy_connect(dev->net, priv->phy_name,
Signature · 1 line
2.47.2
#7 lore raw
In reply to Andy Shevchenko
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 8 lines · On Mon, 24 Mar 2025 16:39:28 +0200 you wrote:

The agreement and also PHY_MAX_ADDR limit suggest that the PHY address can't occupy more than two hex digits. In some cases GCC complains about potential string cut. In course of fixing this, introduce the PHY_ID_SIZE predefined constant to make it easier for the users to know the bare minimum for the buffer that holds PHY ID string (patch 1). With that, fix the ASIX driver that triggers GCC accordingly (patch 2).

[...]

Here is the summary with links:
- [net,v3,1/2] net: phy: Introduce PHY_ID_SIZE — minimum size for PHY ID string

    https://git.kernel.org/netdev/net-next/c/2c5ac026fd14
  - [net,v3,2/2] net: usb: asix: ax88772: Increase phy_name size
    https://git.kernel.org/netdev/net-next/c/61997271a5a7

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html