public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3] net: dsa: lan9303: use ethtool_puts() for lan9303_get_strings()
@ 2024-04-08 21:01 Justin Stitt
  2024-04-08 23:20 ` Kees Cook
  2024-04-09  8:58 ` Alexander Lobakin
  0 siblings, 2 replies; 3+ messages in thread
From: Justin Stitt @ 2024-04-08 21:01 UTC (permalink / raw)
  To: Andrew Lunn, Florian Fainelli, Vladimir Oltean, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni
  Cc: netdev, linux-kernel, linux-hardening, Kees Cook, Justin Stitt,
	Alexander Lobakin

This pattern of strncpy with some pointer arithmetic setting fixed-sized
intervals with string literal data is a bit weird so let's use
ethtool_puts() as this has more obvious behavior and is less-error
prone.

Nicely, we also get to drop a usage of the now deprecated strncpy() [1].

Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1]
Link: https://github.com/KSPP/linux/issues/90
Cc: linux-hardening@vger.kernel.org
Cc: Kees Cook <keescook@chromium.org>
Signed-off-by: Justin Stitt <justinstitt@google.com>
Suggested-by: Alexander Lobakin <aleksander.lobakin@intel.com>
---
Changes in v3:
- use ethtool_puts over ethtool_sprintf
- Link to v2: https://lore.kernel.org/r/20231005-strncpy-drivers-net-dsa-lan9303-core-c-v2-1-feb452a532db@google.com

Changes in v2:
- use ethtool_sprintf (thanks Alexander)
- Link to v1: https://lore.kernel.org/r/20231005-strncpy-drivers-net-dsa-lan9303-core-c-v1-1-5a66c538147e@google.com
---
 drivers/net/dsa/lan9303-core.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/net/dsa/lan9303-core.c b/drivers/net/dsa/lan9303-core.c
index fcb20eac332a..04d3af0eb28e 100644
--- a/drivers/net/dsa/lan9303-core.c
+++ b/drivers/net/dsa/lan9303-core.c
@@ -1007,14 +1007,14 @@ static const struct lan9303_mib_desc lan9303_mib[] = {
 static void lan9303_get_strings(struct dsa_switch *ds, int port,
 				u32 stringset, uint8_t *data)
 {
+	u8 *buf = data;
 	unsigned int u;
 
 	if (stringset != ETH_SS_STATS)
 		return;
 
 	for (u = 0; u < ARRAY_SIZE(lan9303_mib); u++) {
-		strncpy(data + u * ETH_GSTRING_LEN, lan9303_mib[u].name,
-			ETH_GSTRING_LEN);
+		ethtool_puts(&buf, lan9303_mib[u].name);
 	}
 }
 

---
base-commit: c85af715cac0a951eea97393378e84bb49384734
change-id: 20231005-strncpy-drivers-net-dsa-lan9303-core-c-6386858e5c22

Best regards,
--
Justin Stitt <justinstitt@google.com>


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

* Re: [PATCH v3] net: dsa: lan9303: use ethtool_puts() for lan9303_get_strings()
  2024-04-08 21:01 [PATCH v3] net: dsa: lan9303: use ethtool_puts() for lan9303_get_strings() Justin Stitt
@ 2024-04-08 23:20 ` Kees Cook
  2024-04-09  8:58 ` Alexander Lobakin
  1 sibling, 0 replies; 3+ messages in thread
From: Kees Cook @ 2024-04-08 23:20 UTC (permalink / raw)
  To: Justin Stitt
  Cc: Andrew Lunn, Florian Fainelli, Vladimir Oltean, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, netdev, linux-kernel,
	linux-hardening, Alexander Lobakin

On Mon, Apr 08, 2024 at 09:01:57PM +0000, Justin Stitt wrote:
> This pattern of strncpy with some pointer arithmetic setting fixed-sized
> intervals with string literal data is a bit weird so let's use
> ethtool_puts() as this has more obvious behavior and is less-error
> prone.
> 
> Nicely, we also get to drop a usage of the now deprecated strncpy() [1].
> 
> Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1]
> Link: https://github.com/KSPP/linux/issues/90
> Cc: linux-hardening@vger.kernel.org
> Cc: Kees Cook <keescook@chromium.org>
> Signed-off-by: Justin Stitt <justinstitt@google.com>

Reviewed-by: Kees Cook <keescook@chromium.org>

-- 
Kees Cook

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

* Re: [PATCH v3] net: dsa: lan9303: use ethtool_puts() for lan9303_get_strings()
  2024-04-08 21:01 [PATCH v3] net: dsa: lan9303: use ethtool_puts() for lan9303_get_strings() Justin Stitt
  2024-04-08 23:20 ` Kees Cook
@ 2024-04-09  8:58 ` Alexander Lobakin
  1 sibling, 0 replies; 3+ messages in thread
From: Alexander Lobakin @ 2024-04-09  8:58 UTC (permalink / raw)
  To: Justin Stitt
  Cc: Andrew Lunn, Florian Fainelli, Vladimir Oltean, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, netdev, linux-kernel,
	linux-hardening, Kees Cook

From: Justin Stitt <justinstitt@google.com>
Date: Mon, 08 Apr 2024 21:01:57 +0000

> This pattern of strncpy with some pointer arithmetic setting fixed-sized
> intervals with string literal data is a bit weird so let's use
> ethtool_puts() as this has more obvious behavior and is less-error
> prone.
> 
> Nicely, we also get to drop a usage of the now deprecated strncpy() [1].
> 
> Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1]
> Link: https://github.com/KSPP/linux/issues/90
> Cc: linux-hardening@vger.kernel.org
> Cc: Kees Cook <keescook@chromium.org>
> Signed-off-by: Justin Stitt <justinstitt@google.com>
> Suggested-by: Alexander Lobakin <aleksander.lobakin@intel.com>
> ---
> Changes in v3:
> - use ethtool_puts over ethtool_sprintf
> - Link to v2: https://lore.kernel.org/r/20231005-strncpy-drivers-net-dsa-lan9303-core-c-v2-1-feb452a532db@google.com
> 
> Changes in v2:
> - use ethtool_sprintf (thanks Alexander)
> - Link to v1: https://lore.kernel.org/r/20231005-strncpy-drivers-net-dsa-lan9303-core-c-v1-1-5a66c538147e@google.com
> ---
>  drivers/net/dsa/lan9303-core.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/net/dsa/lan9303-core.c b/drivers/net/dsa/lan9303-core.c
> index fcb20eac332a..04d3af0eb28e 100644
> --- a/drivers/net/dsa/lan9303-core.c
> +++ b/drivers/net/dsa/lan9303-core.c
> @@ -1007,14 +1007,14 @@ static const struct lan9303_mib_desc lan9303_mib[] = {
>  static void lan9303_get_strings(struct dsa_switch *ds, int port,
>  				u32 stringset, uint8_t *data)
>  {
> +	u8 *buf = data;
>  	unsigned int u;
>  
>  	if (stringset != ETH_SS_STATS)
>  		return;
>  
>  	for (u = 0; u < ARRAY_SIZE(lan9303_mib); u++) {
> -		strncpy(data + u * ETH_GSTRING_LEN, lan9303_mib[u].name,
> -			ETH_GSTRING_LEN);
> +		ethtool_puts(&buf, lan9303_mib[u].name);
>  	}

The braces { } around ethtool_puts() aren't needed anymore.

>  }
>  
> 
> ---
> base-commit: c85af715cac0a951eea97393378e84bb49384734
> change-id: 20231005-strncpy-drivers-net-dsa-lan9303-core-c-6386858e5c22
> 
> Best regards,
> --
> Justin Stitt <justinstitt@google.com>

Thanks,
Olek

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

end of thread, other threads:[~2024-04-09  9:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-04-08 21:01 [PATCH v3] net: dsa: lan9303: use ethtool_puts() for lan9303_get_strings() Justin Stitt
2024-04-08 23:20 ` Kees Cook
2024-04-09  8:58 ` Alexander Lobakin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox