linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/4] net: Use strlcpy() for ethtool::get_strings
@ 2018-03-02 23:08 Florian Fainelli
  2018-03-02 23:08 ` [PATCH v2 1/4] net: dsa: b53: " Florian Fainelli
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: Florian Fainelli @ 2018-03-02 23:08 UTC (permalink / raw)
  To: netdev
  Cc: david.laight, Florian Fainelli, Andrew Lunn, Vivien Didelot,
	open list, opendmb, davem

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

-- 
2.14.1

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

* [PATCH v2 1/4] net: dsa: b53: Use strlcpy() for ethtool::get_strings
  2018-03-02 23:08 [PATCH v2 0/4] net: Use strlcpy() for ethtool::get_strings Florian Fainelli
@ 2018-03-02 23:08 ` Florian Fainelli
  2018-03-02 23:08 ` [PATCH v2 2/4] net: phy: marvell: " Florian Fainelli
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Florian Fainelli @ 2018-03-02 23:08 UTC (permalink / raw)
  To: netdev
  Cc: david.laight, Florian Fainelli, Andrew Lunn, Vivien Didelot,
	open list, opendmb, davem

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.

Fixes: 967dd82ffc52 ("net: dsa: b53: Add support for Broadcom RoboSwitch")
Signed-off-by: Florian Fainelli <f.fainelli@gmail.com>
---
 drivers/net/dsa/b53/b53_common.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/net/dsa/b53/b53_common.c b/drivers/net/dsa/b53/b53_common.c
index db830a1141d9..63e02a54d537 100644
--- a/drivers/net/dsa/b53/b53_common.c
+++ b/drivers/net/dsa/b53/b53_common.c
@@ -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);
 
-- 
2.14.1

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

* [PATCH v2 2/4] net: phy: marvell: Use strlcpy() for ethtool::get_strings
  2018-03-02 23:08 [PATCH v2 0/4] net: Use strlcpy() for ethtool::get_strings Florian Fainelli
  2018-03-02 23:08 ` [PATCH v2 1/4] net: dsa: b53: " Florian Fainelli
@ 2018-03-02 23:08 ` Florian Fainelli
  2018-03-02 23:08 ` [PATCH v2 3/4] net: phy: micrel: " Florian Fainelli
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Florian Fainelli @ 2018-03-02 23:08 UTC (permalink / raw)
  To: netdev
  Cc: david.laight, Florian Fainelli, Andrew Lunn, Vivien Didelot,
	open list, opendmb, davem

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.

Fixes: d2fa47d9dd5c ("phy: marvell: Add ethtool statistics counters")
Signed-off-by: Florian Fainelli <f.fainelli@gmail.com>
---
 drivers/net/phy/marvell.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/net/phy/marvell.c b/drivers/net/phy/marvell.c
index 22d9bc9c33a4..0e0978d8a0eb 100644
--- a/drivers/net/phy/marvell.c
+++ b/drivers/net/phy/marvell.c
@@ -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);
 	}
 }
 
-- 
2.14.1

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

* [PATCH v2 3/4] net: phy: micrel: Use strlcpy() for ethtool::get_strings
  2018-03-02 23:08 [PATCH v2 0/4] net: Use strlcpy() for ethtool::get_strings Florian Fainelli
  2018-03-02 23:08 ` [PATCH v2 1/4] net: dsa: b53: " Florian Fainelli
  2018-03-02 23:08 ` [PATCH v2 2/4] net: phy: marvell: " Florian Fainelli
@ 2018-03-02 23:08 ` Florian Fainelli
  2018-03-02 23:08 ` [PATCH v2 4/4] net: phy: broadcom: " Florian Fainelli
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Florian Fainelli @ 2018-03-02 23:08 UTC (permalink / raw)
  To: netdev
  Cc: david.laight, Florian Fainelli, Andrew Lunn, Vivien Didelot,
	open list, opendmb, davem

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.

Fixes: 2b2427d06426 ("phy: micrel: Add ethtool statistics counters")
Signed-off-by: Florian Fainelli <f.fainelli@gmail.com>
---
 drivers/net/phy/micrel.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/net/phy/micrel.c b/drivers/net/phy/micrel.c
index 0f45310300f6..49be85afbea9 100644
--- a/drivers/net/phy/micrel.c
+++ b/drivers/net/phy/micrel.c
@@ -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);
 	}
 }
 
-- 
2.14.1

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

* [PATCH v2 4/4] net: phy: broadcom: Use strlcpy() for ethtool::get_strings
  2018-03-02 23:08 [PATCH v2 0/4] net: Use strlcpy() for ethtool::get_strings Florian Fainelli
                   ` (2 preceding siblings ...)
  2018-03-02 23:08 ` [PATCH v2 3/4] net: phy: micrel: " Florian Fainelli
@ 2018-03-02 23:08 ` Florian Fainelli
  2018-03-04 15:40 ` [PATCH v2 0/4] net: " Andrew Lunn
  2018-03-06 16:13 ` David Miller
  5 siblings, 0 replies; 7+ messages in thread
From: Florian Fainelli @ 2018-03-02 23:08 UTC (permalink / raw)
  To: netdev
  Cc: david.laight, Florian Fainelli, Andrew Lunn, Vivien Didelot,
	open list, opendmb, davem

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.

Fixes: 820ee17b8d3b ("net: phy: broadcom: Add support code for reading PHY counters")
Signed-off-by: Florian Fainelli <f.fainelli@gmail.com>
---
 drivers/net/phy/bcm-phy-lib.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/net/phy/bcm-phy-lib.c b/drivers/net/phy/bcm-phy-lib.c
index 171010eb4d9c..5ad130c3da43 100644
--- a/drivers/net/phy/bcm-phy-lib.c
+++ b/drivers/net/phy/bcm-phy-lib.c
@@ -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);
 
-- 
2.14.1

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

* Re: [PATCH v2 0/4] net: Use strlcpy() for ethtool::get_strings
  2018-03-02 23:08 [PATCH v2 0/4] net: Use strlcpy() for ethtool::get_strings Florian Fainelli
                   ` (3 preceding siblings ...)
  2018-03-02 23:08 ` [PATCH v2 4/4] net: phy: broadcom: " Florian Fainelli
@ 2018-03-04 15:40 ` Andrew Lunn
  2018-03-06 16:13 ` David Miller
  5 siblings, 0 replies; 7+ messages in thread
From: Andrew Lunn @ 2018-03-04 15:40 UTC (permalink / raw)
  To: Florian Fainelli
  Cc: netdev, david.laight, Vivien Didelot, open list, opendmb, davem

On Fri, Mar 02, 2018 at 03:08:35PM -0800, Florian Fainelli wrote:
> 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.

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

    Andrew

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

* Re: [PATCH v2 0/4] net: Use strlcpy() for ethtool::get_strings
  2018-03-02 23:08 [PATCH v2 0/4] net: Use strlcpy() for ethtool::get_strings Florian Fainelli
                   ` (4 preceding siblings ...)
  2018-03-04 15:40 ` [PATCH v2 0/4] net: " Andrew Lunn
@ 2018-03-06 16:13 ` David Miller
  5 siblings, 0 replies; 7+ messages in thread
From: David Miller @ 2018-03-06 16:13 UTC (permalink / raw)
  To: f.fainelli
  Cc: netdev, david.laight, andrew, vivien.didelot, linux-kernel, opendmb

From: Florian Fainelli <f.fainelli@gmail.com>
Date: Fri,  2 Mar 2018 15:08:35 -0800

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

Series applied, thanks Florian.

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

end of thread, other threads:[~2018-03-06 16:13 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-03-02 23:08 [PATCH v2 0/4] net: Use strlcpy() for ethtool::get_strings Florian Fainelli
2018-03-02 23:08 ` [PATCH v2 1/4] net: dsa: b53: " Florian Fainelli
2018-03-02 23:08 ` [PATCH v2 2/4] net: phy: marvell: " Florian Fainelli
2018-03-02 23:08 ` [PATCH v2 3/4] net: phy: micrel: " Florian Fainelli
2018-03-02 23:08 ` [PATCH v2 4/4] net: phy: broadcom: " Florian Fainelli
2018-03-04 15:40 ` [PATCH v2 0/4] net: " Andrew Lunn
2018-03-06 16:13 ` David Miller

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