Skip to contentSkip to conversation

net: dsa: Use strncpy() for ethtool::get_strings

PATCH Superseded Florian Fainelli 3 participants 10 messages Mar 2, 2018

Subsystems (6)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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
#1 lore raw

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.

Fixes 967dd82ffc52 ("net: dsa: b53: Add support for Broadcom RoboSwitch")
---
 drivers/net/dsa/b53/b53_common.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/dsa/b53/b53_common.c +2 -2
index db830a1141d9..c64ebb82df83 100644
@@ -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
#2 lore raw

PATCH 2/4 net: dsa: loop: Use strncpy() for ethtool::get_strings

In reply to Florian Fainelli

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.

Fixes 484c01720d84 ("net: dsa: loop: Implement ethtool statistics")
---
 drivers/net/dsa/dsa_loop.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/dsa/dsa_loop.c +2 -2
index 7aa84ee4e771..2e7e1cdbd2e9 100644
@@ -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
#3 lore raw

PATCH 3/4 net: dsa: microchip: Utilize strncpy() for ethtool::get_strings

In reply to Florian Fainelli

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.

Fixes b987e98e50ab ("dsa: add DSA switch driver for Microchip KSZ9477")
---
 drivers/net/dsa/microchip/ksz_common.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/dsa/microchip/ksz_common.c +2 -2
index 663b0d5b982b..db7f5c8c1dcb 100644
@@ -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
#4 lore raw

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

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

#5 lore raw

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

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 use strcpy().

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
#6 lore raw

PATCH 4/4 net: dsa: mv88e6xxx: Utilize strncpy() for ethtool::get_strings

In reply to Florian Fainelli

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.

Fixes f5e2ed022dff ("dsa: mv88e6xxx: Add Second back of statistics")
---
 drivers/net/dsa/mv88e6xxx/chip.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/dsa/mv88e6xxx/chip.c +2 -2
index eb328bade225..bec7540aae2b 100644
@@ -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
maintainer Reviewed-by on v1 4/4
#7 lore raw

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

Fixes f5e2ed022dff ("dsa: mv88e6xxx: Add Second back of statistics")

I'm sure it goes back much further than that.

Quoted 1 line

Andrew

#8 lore raw

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

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

Andrew

Signature · 1 line
Florian
#9 lore raw
In reply to Florian Fainelli

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 use strncpy() 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: 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
Florian