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

2018-03-02 Thread Florian Fainelli
On 03/02/2018 02:51 AM, David Laight wrote:
> From: 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.
> 
> 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).
-- 
Florian


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

2018-03-02 Thread Florian Fainelli
On 03/02/2018 02:51 AM, David Laight wrote:
> From: 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.
> 
> 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).
-- 
Florian


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

2018-03-02 Thread David Laight
From: 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.

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

2018-03-02 Thread David Laight
From: 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.

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



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

2018-03-01 Thread 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")
Signed-off-by: Florian Fainelli 
---
 drivers/net/dsa/microchip/ksz_common.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/net/dsa/microchip/ksz_common.c 
b/drivers/net/dsa/microchip/ksz_common.c
index 663b0d5b982b..db7f5c8c1dcb 100644
--- a/drivers/net/dsa/microchip/ksz_common.c
+++ b/drivers/net/dsa/microchip/ksz_common.c
@@ -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);
}
 }
 
-- 
2.14.1



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

2018-03-01 Thread 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")
Signed-off-by: Florian Fainelli 
---
 drivers/net/dsa/microchip/ksz_common.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/net/dsa/microchip/ksz_common.c 
b/drivers/net/dsa/microchip/ksz_common.c
index 663b0d5b982b..db7f5c8c1dcb 100644
--- a/drivers/net/dsa/microchip/ksz_common.c
+++ b/drivers/net/dsa/microchip/ksz_common.c
@@ -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);
}
 }
 
-- 
2.14.1