net: dsa: Use strncpy() for ethtool::get_strings
Subsystems (6)
cover net: dsa: Use strncpy() 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, so let's use strncpy() instead.
Florian Fainelli (4):
net: dsa: b53: Use strncpy() for ethtool::get_strings
net: dsa: loop: Use strncpy() for ethtool::get_strings
net: dsa: microchip: Utilize strncpy() for ethtool::get_strings
net: dsa: mv88e6xxx: Utilize strncpy() for ethtool::get_strings
drivers/net/dsa/b53/b53_common.c | 4 ++-- drivers/net/dsa/dsa_loop.c | 4 ++-- drivers/net/dsa/microchip/ksz_common.c | 4 ++-- drivers/net/dsa/mv88e6xxx/chip.c | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-)
Signature · 1 line
2.14.1
PATCH 1/4 net: dsa: b53: Use strncpy() for ethtool::get_strings
Do not use memcpy() which is not safe, but instead use strncpy() which will make sure that the string is NUL terminated (in the Linux implementation) if the string is smaller than the length specified. This fixes KASAN out of bounds warnings while fetching port statistics.
--- 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 ); +strncpy (data + i *ETH_GSTRING_LEN , + mibs[i].name,ETH_GSTRING_LEN ); }EXPORT_SYMBOL (b53_get_strings);
Signature · 1 line
2.14.1
PATCH 2/4 net: dsa: loop: Use strncpy() for ethtool::get_strings
In reply to Florian FainelliDo not use memcpy() which is not safe, but instead use strncpy() which will make sure that the string is NUL terminated (in the Linux implementation) if the string is smaller than the length specified. This fixes KASAN out of bounds warnings while fetching port statistics.
--- drivers/net/dsa/dsa_loop.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/dsa/dsa_loop.c +2 -2
@@ -97,8 +97,8 @@ static void dsa_loop_get_strings(struct dsa_switch *ds, int port, uint8_t *data)unsigned int i;for (i =0 ; i < __DSA_LOOP_CNT_MAX; i++) -memcpy (data + i *ETH_GSTRING_LEN , - ps->ports[port].mib[i].name,ETH_GSTRING_LEN ); +strncpy (data + i *ETH_GSTRING_LEN , + ps->ports[port].mib[i].name,ETH_GSTRING_LEN ); } static void dsa_loop_get_ethtool_stats(struct dsa_switch *ds, int port,
Signature · 1 line
2.14.1
PATCH 3/4 net: dsa: microchip: Utilize strncpy() for ethtool::get_strings
In reply to Florian FainelliDo not use memcpy() which is not safe, but instead use strncpy() which will make sure that the string is NUL terminated (in the Linux implementation) if the string is smaller than the length specified. This fixes KASAN out of bounds warnings while fetching port statistics.
--- drivers/net/dsa/microchip/ksz_common.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/dsa/microchip/ksz_common.c +2 -2
@@ -449,8 +449,8 @@ static void ksz_get_strings(struct dsa_switch *ds, int port, uint8_t *buf)int i;for (i =0 ; i <TOTAL_SWITCH_COUNTER_NUM ; i++) { -memcpy (buf + i *ETH_GSTRING_LEN , mib_names[i].string, -ETH_GSTRING_LEN ); +strncpy (buf + i *ETH_GSTRING_LEN , mib_names[i].string, +ETH_GSTRING_LEN ); } }
Signature · 1 line
2.14.1
RE: [PATCH net 3/4] net: dsa: microchip: Utilize strncpy() for ethtool::get_strings
From: Florian Fainelli
Quoted 5 lines
Do not use
memcpy()which is not safe, but instead usestrncpy()which will make sure that the string is NUL terminated (in the Linux implementation) if the string is smaller than the length specified. This fixes KASAN out of bounds warnings while fetching port statistics.
You really ought to use a copy function that will truncate the string if it is too long.
Just assuming the string isn't too long is asking for trouble. You might (almost) just use strcpy().
strlcpy() will probably work best here.
David
Re: [PATCH net 3/4] net: dsa: microchip: Utilize strncpy() for ethtool::get_strings
Quoted 7 lines · David Laight
From: Florian Fainelli
Quoted 5 lines
Do not use
memcpy()which is not safe, but instead usestrncpy()which will make sure that the string is NUL terminated (in the Linux implementation) if the string is smaller than the length specified. This fixes KASAN out of bounds warnings while fetching port statistics.
Quoted 6 lines
You really ought to use a copy function that will truncate the string if it is too long.
Just assuming the string isn't too long is asking for trouble. You might (almost) just usestrcpy().
strlcpy()will probably work best here.
Right, or if we actually do size the statistics string to be ETH_GSTRING_LEN bytes, memcpy() can be used, provided that the strings are initialized correctly (which they are).
Signature · 1 line
Florian
PATCH 4/4 net: dsa: mv88e6xxx: Utilize strncpy() for ethtool::get_strings
In reply to Florian FainelliDo not use memcpy() which is not safe, but instead use strncpy() which will make sure that the string is NUL terminated (in the Linux implementation) if the string is smaller than the length specified. This fixes KASAN out of bounds warnings while fetching port statistics.
--- drivers/net/dsa/mv88e6xxx/chip.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/dsa/mv88e6xxx/chip.c +2 -2
@@ -636,8 +636,8 @@ static void mv88e6xxx_stats_get_strings(struct mv88e6xxx_chip *chip,for (i =0 , j =0 ; i <ARRAY_SIZE (mv88e6xxx_hw_stats); i++) { stat = &mv88e6xxx_hw_stats[i];if (stat->type & types) { -memcpy (data + j *ETH_GSTRING_LEN , stat->string, -ETH_GSTRING_LEN ); +strncpy (data + j *ETH_GSTRING_LEN , stat->string, +ETH_GSTRING_LEN ); j++; } }
Signature · 1 line
2.14.1
Re: [PATCH net 4/4] net: dsa: mv88e6xxx: Utilize strncpy() for ethtool::get_strings
Quoted 6 lines · Florian Fainelli
Do not use
memcpy()which is not safe, but instead usestrncpy()which will make sure that the string is NUL terminated (in the Linux implementation) if the string is smaller than the length specified. This fixes KASAN out of bounds warnings while fetching port statistics.Fixes f5e2ed022dff ("dsa: mv88e6xxx: Add Second back of statistics")
I'm sure it goes back much further than that.
Quoted 1 line
Signed-off-by Florian Fainelli <[email protected]>
Andrew
Re: [PATCH net 4/4] net: dsa: mv88e6xxx: Utilize strncpy() for ethtool::get_strings
Quoted 9 lines · Andrew Lunn
Quoted 6 lines · Florian Fainelli
Do not use
memcpy()which is not safe, but instead usestrncpy()which will make sure that the string is NUL terminated (in the Linux implementation) if the string is smaller than the length specified. This fixes KASAN out of bounds warnings while fetching port statistics.Fixes f5e2ed022dff ("dsa: mv88e6xxx: Add Second back of statistics")I'm sure it goes back much further than that.
You are right, it appears that I used the most recent commit that changed the stats last.
This is not actually needed per-se here because the string is defined to be ETH_GSTRING_LEN bytes, so unlike b53, we are not copying past the buffer, in fact only the first patch is really necessary.
Thanks!
Quoted 7 lines
Quoted 2 lines
Signed-off-by Florian Fainelli <[email protected]>Reviewed-by Andrew Lunn <[email protected]>Andrew
Signature · 1 line
Florian
Hi David,
Quoted 5 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, so let's usestrncpy()instead.
Looks like only patch 1 is necessary, but there are more drivers with the same pattern under drivers/net/phy: marvell.c, micrel.c and bcm-phy-lib.c, so I will submit a v2 with those fixed.
Quoted 13 lines
Florian Fainelli (4):
net: dsa: b53: Usestrncpy()forethtool::get_strings
net: dsa: loop: Usestrncpy()forethtool::get_strings
net: dsa: microchip: Utilizestrncpy()forethtool::get_strings
net: dsa: mv88e6xxx: Utilizestrncpy()forethtool::get_stringsdrivers/net/dsa/b53/b53_common.c | 4 ++-- drivers/net/dsa/dsa_loop.c | 4 ++-- drivers/net/dsa/microchip/ksz_common.c | 4 ++-- drivers/net/dsa/mv88e6xxx/chip.c | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-)
Signature · 1 line
Florian