net: Use strlcpy() for ethtool::get_strings
Subsystems (6)
v2 cover net: Use strlcpy() for ethtool::get_strings
Hi all,
After turning on KASAN on one of my systems, I started getting lots of out of bounds errors while fetching a given port's statistics, and indeed using memcpy() is unsafe for copying strings which have not been declared as an array of ETH_GSTRING_LEN bytes, so let's use strlcpy() instead. This allows the best of both worlds: we still keep the efficient memory usage of variably sized strings, but we don't copy more than we need to.
Changes in v2:
- dropped the 3 other patches that were not necessary
- use strlcpy() instead of strncpy()
Florian Fainelli (4):
net: dsa: b53: Use strlcpy() for ethtool::get_strings
net: phy: marvell: Use strlcpy() for ethtool::get_strings
net: phy: micrel: Use strlcpy() for ethtool::get_strings
net: phy: broadcom: Use strlcpy() for ethtool::get_strings
drivers/net/dsa/b53/b53_common.c | 4 ++-- drivers/net/phy/bcm-phy-lib.c | 4 ++-- drivers/net/phy/marvell.c | 4 ++-- drivers/net/phy/micrel.c | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-)
Signature · 1 line
2.14.1
PATCH v2 1/4 net: dsa: b53: Use strlcpy() for ethtool::get_strings
Our statistics strings are allocated at initialization without being bound to a specific size, yet, we would copy ETH_GSTRING_LEN bytes using memcpy() which would create out of bounds accesses, this was flagged by KASAN. Replace this with strlcpy() to make sure we are bound the source buffer size and we also always NUL-terminate strings.
--- drivers/net/dsa/b53/b53_common.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/dsa/b53/b53_common.c +2 -2
@@ -814,8 +814,8 @@ void b53_get_strings(struct dsa_switch *ds, int port, uint8_t *data)unsigned int i;for (i =0 ; i < mib_size; i++) -memcpy (data + i *ETH_GSTRING_LEN , - mibs[i].name,ETH_GSTRING_LEN ); +strlcpy (data + i *ETH_GSTRING_LEN , + mibs[i].name,ETH_GSTRING_LEN ); }EXPORT_SYMBOL (b53_get_strings);
Signature · 1 line
2.14.1
PATCH v2 2/4 net: phy: marvell: Use strlcpy() for ethtool::get_strings
In reply to Florian FainelliOur statistics strings are allocated at initialization without being bound to a specific size, yet, we would copy ETH_GSTRING_LEN bytes using memcpy() which would create out of bounds accesses, this was flagged by KASAN. Replace this with strlcpy() to make sure we are bound the source buffer size and we also always NUL-terminate strings.
--- drivers/net/phy/marvell.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/phy/marvell.c +2 -2
@@ -1452,8 +1452,8 @@ static void marvell_get_strings(struct phy_device *phydev, u8 *data)int i;for (i =0 ; i <ARRAY_SIZE (marvell_hw_stats); i++) { -memcpy (data + i *ETH_GSTRING_LEN , - marvell_hw_stats[i].string,ETH_GSTRING_LEN ); +strlcpy (data + i *ETH_GSTRING_LEN , + marvell_hw_stats[i].string,ETH_GSTRING_LEN ); } }
Signature · 1 line
2.14.1
PATCH v2 3/4 net: phy: micrel: Use strlcpy() for ethtool::get_strings
In reply to Florian FainelliOur statistics strings are allocated at initialization without being bound to a specific size, yet, we would copy ETH_GSTRING_LEN bytes using memcpy() which would create out of bounds accesses, this was flagged by KASAN. Replace this with strlcpy() to make sure we are bound the source buffer size and we also always NUL-terminate strings.
--- drivers/net/phy/micrel.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/phy/micrel.c +2 -2
@@ -664,8 +664,8 @@ static void kszphy_get_strings(struct phy_device *phydev, u8 *data)int i;for (i =0 ; i <ARRAY_SIZE (kszphy_hw_stats); i++) { -memcpy (data + i *ETH_GSTRING_LEN , - kszphy_hw_stats[i].string,ETH_GSTRING_LEN ); +strlcpy (data + i *ETH_GSTRING_LEN , + kszphy_hw_stats[i].string,ETH_GSTRING_LEN ); } }
Signature · 1 line
2.14.1
PATCH v2 4/4 net: phy: broadcom: Use strlcpy() for ethtool::get_strings
In reply to Florian FainelliOur statistics strings are allocated at initialization without being bound to a specific size, yet, we would copy ETH_GSTRING_LEN bytes using memcpy() which would create out of bounds accesses, this was flagged by KASAN. Replace this with strlcpy() to make sure we are bound the source buffer size and we also always NUL-terminate strings.
--- drivers/net/phy/bcm-phy-lib.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/phy/bcm-phy-lib.c +2 -2
@@ -341,8 +341,8 @@ void bcm_phy_get_strings(struct phy_device *phydev, u8 *data)unsigned int i;for (i =0 ; i <ARRAY_SIZE (bcm_phy_hw_stats); i++) -memcpy (data + i *ETH_GSTRING_LEN , - bcm_phy_hw_stats[i].string,ETH_GSTRING_LEN ); +strlcpy (data + i *ETH_GSTRING_LEN , + bcm_phy_hw_stats[i].string,ETH_GSTRING_LEN ); }EXPORT_SYMBOL_GPL (bcm_phy_get_strings);
Signature · 1 line
2.14.1
Quoted 8 lines · Florian Fainelli
Hi all,
After turning on KASAN on one of my systems, I started getting lots of out of bounds errors while fetching a given port's statistics, and indeed using
memcpy()is unsafe for copying strings which have not been declared as an array ofETH_GSTRING_LENbytes, so let's usestrlcpy()instead. This allows the best of both worlds: we still keep the efficient memory usage of variably sized strings, but we don't copy more than we need to.
Andrew
David Miller
maintainerFrom: Florian Fainelli <[email protected]>
Date: Fri, 2 Mar 2018 15:08:35 -0800
Quoted 10 lines
After turning on KASAN on one of my systems, I started getting lots of out of bounds errors while fetching a given port's statistics, and indeed using
memcpy()is unsafe for copying strings which have not been declared as an array ofETH_GSTRING_LENbytes, so let's usestrlcpy()instead. This allows the best of both worlds: we still keep the efficient memory usage of variably sized strings, but we don't copy more than we need to.Changes in v2:
- dropped the 3 other patches that were not necessary
- usestrlcpy()instead ofstrncpy()
Series applied, thanks Florian.