netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] net: dsa: lan9303: use ethtool_sprintf() for lan9303_get_strings()
@ 2023-10-05 18:56 Justin Stitt
  2023-10-05 22:42 ` Kees Cook
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Justin Stitt @ 2023-10-05 18:56 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_sprintf() 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].

One might consider this pattern:
|       ethtool_sprintf(&buf, lan9303_mib[u].name);
... but this triggers a -Wformat-security warning.

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 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
---
Note: build-tested only.
---
 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 ee67adeb2cdb..95a8e5168c2a 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_sprintf(&buf, "%s", lan9303_mib[u].name);
 	}
 }
 

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

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


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

* Re: [PATCH v2] net: dsa: lan9303: use ethtool_sprintf() for lan9303_get_strings()
  2023-10-05 18:56 [PATCH v2] net: dsa: lan9303: use ethtool_sprintf() for lan9303_get_strings() Justin Stitt
@ 2023-10-05 22:42 ` Kees Cook
  2023-10-06  9:37 ` Alexander Lobakin
  2023-11-30 21:59 ` Kees Cook
  2 siblings, 0 replies; 7+ messages in thread
From: Kees Cook @ 2023-10-05 22:42 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 Thu, Oct 05, 2023 at 06:56:50PM +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_sprintf() 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].
> 
> One might consider this pattern:
> |       ethtool_sprintf(&buf, lan9303_mib[u].name);
> ... but this triggers a -Wformat-security warning.
> 
> 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>

Ah, cool ethtool_sprintf() works. Maybe some day we can fix the whole
API to actually have bounds, but yes, this is fine.

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

-- 
Kees Cook

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

* Re: [PATCH v2] net: dsa: lan9303: use ethtool_sprintf() for lan9303_get_strings()
  2023-10-05 18:56 [PATCH v2] net: dsa: lan9303: use ethtool_sprintf() for lan9303_get_strings() Justin Stitt
  2023-10-05 22:42 ` Kees Cook
@ 2023-10-06  9:37 ` Alexander Lobakin
  2023-11-30 21:59 ` Kees Cook
  2 siblings, 0 replies; 7+ messages in thread
From: Alexander Lobakin @ 2023-10-06  9:37 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: Thu, 05 Oct 2023 18:56:50 +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_sprintf() as this has more obvious behavior and is less-error
> prone.

[...]

> diff --git a/drivers/net/dsa/lan9303-core.c b/drivers/net/dsa/lan9303-core.c
> index ee67adeb2cdb..95a8e5168c2a 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;

Is it needed here? I thought you could pass @data directly to
ethtool_sprintf(), if it doesn't mind.

>  	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_sprintf(&buf, "%s", lan9303_mib[u].name);
>  	}
>  }

Either way, this was a nitpick, so

Reviewed-by: Alexander Lobakin <aleksander.lobakin@intel.com>

>  
> 
> ---
> base-commit: cbf3a2cb156a2c911d8f38d8247814b4c07f49a2
> 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] 7+ messages in thread

* Re: [PATCH v2] net: dsa: lan9303: use ethtool_sprintf() for lan9303_get_strings()
  2023-10-05 18:56 [PATCH v2] net: dsa: lan9303: use ethtool_sprintf() for lan9303_get_strings() Justin Stitt
  2023-10-05 22:42 ` Kees Cook
  2023-10-06  9:37 ` Alexander Lobakin
@ 2023-11-30 21:59 ` Kees Cook
  2023-12-01  6:40   ` Jakub Kicinski
  2 siblings, 1 reply; 7+ messages in thread
From: Kees Cook @ 2023-11-30 21:59 UTC (permalink / raw)
  To: Andrew Lunn, Florian Fainelli, Vladimir Oltean, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, Justin Stitt
  Cc: Kees Cook, netdev, linux-kernel, linux-hardening,
	Alexander Lobakin

On Thu, 05 Oct 2023 18:56:50 +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_sprintf() 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].
> 
> [...]

Applied to for-next/hardening, thanks!

[1/1] net: dsa: lan9303: use ethtool_sprintf() for lan9303_get_strings()
      https://git.kernel.org/kees/c/f1c7720549bf

Take care,

-- 
Kees Cook


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

* Re: [PATCH v2] net: dsa: lan9303: use ethtool_sprintf() for lan9303_get_strings()
  2023-11-30 21:59 ` Kees Cook
@ 2023-12-01  6:40   ` Jakub Kicinski
  2023-12-01 18:19     ` Kees Cook
  0 siblings, 1 reply; 7+ messages in thread
From: Jakub Kicinski @ 2023-12-01  6:40 UTC (permalink / raw)
  To: Kees Cook
  Cc: Andrew Lunn, Florian Fainelli, Vladimir Oltean, David S. Miller,
	Eric Dumazet, Paolo Abeni, Justin Stitt, netdev, linux-kernel,
	linux-hardening, Alexander Lobakin

On Thu, 30 Nov 2023 13:59:58 -0800 Kees Cook wrote:
> Applied to for-next/hardening, thanks!
> 
> [1/1] net: dsa: lan9303: use ethtool_sprintf() for lan9303_get_strings()
>       https://git.kernel.org/kees/c/f1c7720549bf

Please drop this, it got changes requested on our end because
I figured Alexander's comment is worth addressing.

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

* Re: [PATCH v2] net: dsa: lan9303: use ethtool_sprintf() for lan9303_get_strings()
  2023-12-01  6:40   ` Jakub Kicinski
@ 2023-12-01 18:19     ` Kees Cook
  2023-12-07 19:51       ` Justin Stitt
  0 siblings, 1 reply; 7+ messages in thread
From: Kees Cook @ 2023-12-01 18:19 UTC (permalink / raw)
  To: Jakub Kicinski
  Cc: Andrew Lunn, Florian Fainelli, Vladimir Oltean, David S. Miller,
	Eric Dumazet, Paolo Abeni, Justin Stitt, netdev, linux-kernel,
	linux-hardening, Alexander Lobakin

On Thu, Nov 30, 2023 at 10:40:21PM -0800, Jakub Kicinski wrote:
> On Thu, 30 Nov 2023 13:59:58 -0800 Kees Cook wrote:
> > Applied to for-next/hardening, thanks!
> > 
> > [1/1] net: dsa: lan9303: use ethtool_sprintf() for lan9303_get_strings()
> >       https://git.kernel.org/kees/c/f1c7720549bf
> 
> Please drop this, it got changes requested on our end because
> I figured Alexander's comment is worth addressing.

Done. Justin, can you please refresh this patch (or, actually, make sure
the ethtool_puts() series lands?)

-- 
Kees Cook

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

* Re: [PATCH v2] net: dsa: lan9303: use ethtool_sprintf() for lan9303_get_strings()
  2023-12-01 18:19     ` Kees Cook
@ 2023-12-07 19:51       ` Justin Stitt
  0 siblings, 0 replies; 7+ messages in thread
From: Justin Stitt @ 2023-12-07 19:51 UTC (permalink / raw)
  To: Kees Cook
  Cc: Jakub Kicinski, Andrew Lunn, Florian Fainelli, Vladimir Oltean,
	David S. Miller, Eric Dumazet, Paolo Abeni, netdev, linux-kernel,
	linux-hardening, Alexander Lobakin

On Fri, Dec 1, 2023 at 10:19 AM Kees Cook <keescook@chromium.org> wrote:
>
> On Thu, Nov 30, 2023 at 10:40:21PM -0800, Jakub Kicinski wrote:
> > On Thu, 30 Nov 2023 13:59:58 -0800 Kees Cook wrote:
> > > Applied to for-next/hardening, thanks!
> > >
> > > [1/1] net: dsa: lan9303: use ethtool_sprintf() for lan9303_get_strings()
> > >       https://git.kernel.org/kees/c/f1c7720549bf
> >
> > Please drop this, it got changes requested on our end because
> > I figured Alexander's comment is worth addressing.
>
> Done. Justin, can you please refresh this patch (or, actually, make sure
> the ethtool_puts() series lands?)

Yeah, let's let this patch die. The ethtool_puts() is on v5 and is
getting good reviewed-by's. I suspect it
will be in soon. Then I'll double back and do right by the intent of this patch.

>
> --
> Kees Cook

Justin

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

end of thread, other threads:[~2023-12-07 19:51 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-10-05 18:56 [PATCH v2] net: dsa: lan9303: use ethtool_sprintf() for lan9303_get_strings() Justin Stitt
2023-10-05 22:42 ` Kees Cook
2023-10-06  9:37 ` Alexander Lobakin
2023-11-30 21:59 ` Kees Cook
2023-12-01  6:40   ` Jakub Kicinski
2023-12-01 18:19     ` Kees Cook
2023-12-07 19:51       ` Justin Stitt

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