* [PATCH v3] net: macb: Add __nonstring annotations for unterminated strings
@ 2025-03-12 20:07 Kees Cook
2025-03-12 22:57 ` Jacob Keller
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Kees Cook @ 2025-03-12 20:07 UTC (permalink / raw)
To: Nicolas Ferre
Cc: Kees Cook, Claudiu Beznea, Andrew Lunn, David S. Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni, netdev, linux-kernel,
linux-hardening
When a character array without a terminating NUL character has a static
initializer, GCC 15's -Wunterminated-string-initialization will only
warn if the array lacks the "nonstring" attribute[1]. Mark the arrays
with __nonstring to correctly identify the char array as "not a C string"
and thereby eliminate the warning:
In file included from ../drivers/net/ethernet/cadence/macb_main.c:42:
../drivers/net/ethernet/cadence/macb.h:1070:35: warning: initializer-string for array of 'char' truncates NUL terminator but destination lacks 'nonstring' attribute (33 chars into 32 available) [-Wunterminated-string-initialization]
1070 | GEM_STAT_TITLE(TX1519CNT, "tx_greater_than_1518_byte_frames"),
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../drivers/net/ethernet/cadence/macb.h:1050:24: note: in definition of macro 'GEM_STAT_TITLE_BITS'
1050 | .stat_string = title, \
| ^~~~~
../drivers/net/ethernet/cadence/macb.h:1070:9: note: in expansion of macro 'GEM_STAT_TITLE'
1070 | GEM_STAT_TITLE(TX1519CNT, "tx_greater_than_1518_byte_frames"),
| ^~~~~~~~~~~~~~
../drivers/net/ethernet/cadence/macb.h:1097:35: warning: initializer-string for array of 'char' truncates NUL terminator but destination lacks 'nonstring' attribute (33 chars into 32 available) [-Wunterminated-string-initialization]
1097 | GEM_STAT_TITLE(RX1519CNT, "rx_greater_than_1518_byte_frames"),
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../drivers/net/ethernet/cadence/macb.h:1050:24: note: in definition of macro 'GEM_STAT_TITLE_BITS'
1050 | .stat_string = title, \
| ^~~~~
../drivers/net/ethernet/cadence/macb.h:1097:9: note: in expansion of macro 'GEM_STAT_TITLE'
1097 | GEM_STAT_TITLE(RX1519CNT, "rx_greater_than_1518_byte_frames"),
| ^~~~~~~~~~~~~~
Since these strings are copied with memcpy() they do not need to be
NUL terminated, and can use __nonstring:
memcpy(p, gem_statistics[i].stat_string,
ETH_GSTRING_LEN);
Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117178 [1]
Signed-off-by: Kees Cook <kees@kernel.org>
---
v3: improve commit message (Bill), drop whitespace change (Bill)
v2: https://lore.kernel.org/lkml/20250311224412.it.153-kees@kernel.org/
v1: https://lore.kernel.org/lkml/20250310222415.work.815-kees@kernel.org/
Cc: Nicolas Ferre <nicolas.ferre@microchip.com>
Cc: Claudiu Beznea <claudiu.beznea@tuxon.dev>
Cc: Andrew Lunn <andrew+netdev@lunn.ch>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Eric Dumazet <edumazet@google.com>
Cc: Jakub Kicinski <kuba@kernel.org>
Cc: Paolo Abeni <pabeni@redhat.com>
Cc: netdev@vger.kernel.org
---
drivers/net/ethernet/cadence/macb.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/net/ethernet/cadence/macb.h b/drivers/net/ethernet/cadence/macb.h
index 2847278d9cd4..d2a4c180d6a6 100644
--- a/drivers/net/ethernet/cadence/macb.h
+++ b/drivers/net/ethernet/cadence/macb.h
@@ -1027,7 +1027,7 @@ struct gem_stats {
* this register should contribute to.
*/
struct gem_statistic {
- char stat_string[ETH_GSTRING_LEN];
+ char stat_string[ETH_GSTRING_LEN] __nonstring;
int offset;
u32 stat_bits;
};
--
2.34.1
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH v3] net: macb: Add __nonstring annotations for unterminated strings
2025-03-12 20:07 [PATCH v3] net: macb: Add __nonstring annotations for unterminated strings Kees Cook
@ 2025-03-12 22:57 ` Jacob Keller
2025-03-13 13:25 ` Andrew Lunn
2025-03-19 18:40 ` patchwork-bot+netdevbpf
2 siblings, 0 replies; 5+ messages in thread
From: Jacob Keller @ 2025-03-12 22:57 UTC (permalink / raw)
To: Kees Cook, Nicolas Ferre
Cc: Claudiu Beznea, Andrew Lunn, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, netdev, linux-kernel,
linux-hardening
On 3/12/2025 1:07 PM, Kees Cook wrote:
> When a character array without a terminating NUL character has a static
> initializer, GCC 15's -Wunterminated-string-initialization will only
> warn if the array lacks the "nonstring" attribute[1]. Mark the arrays
> with __nonstring to correctly identify the char array as "not a C string"
> and thereby eliminate the warning:
>
> In file included from ../drivers/net/ethernet/cadence/macb_main.c:42:
> ../drivers/net/ethernet/cadence/macb.h:1070:35: warning: initializer-string for array of 'char' truncates NUL terminator but destination lacks 'nonstring' attribute (33 chars into 32 available) [-Wunterminated-string-initialization]
> 1070 | GEM_STAT_TITLE(TX1519CNT, "tx_greater_than_1518_byte_frames"),
> | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> ../drivers/net/ethernet/cadence/macb.h:1050:24: note: in definition of macro 'GEM_STAT_TITLE_BITS'
> 1050 | .stat_string = title, \
> | ^~~~~
> ../drivers/net/ethernet/cadence/macb.h:1070:9: note: in expansion of macro 'GEM_STAT_TITLE'
> 1070 | GEM_STAT_TITLE(TX1519CNT, "tx_greater_than_1518_byte_frames"),
> | ^~~~~~~~~~~~~~
> ../drivers/net/ethernet/cadence/macb.h:1097:35: warning: initializer-string for array of 'char' truncates NUL terminator but destination lacks 'nonstring' attribute (33 chars into 32 available) [-Wunterminated-string-initialization]
> 1097 | GEM_STAT_TITLE(RX1519CNT, "rx_greater_than_1518_byte_frames"),
> | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> ../drivers/net/ethernet/cadence/macb.h:1050:24: note: in definition of macro 'GEM_STAT_TITLE_BITS'
> 1050 | .stat_string = title, \
> | ^~~~~
> ../drivers/net/ethernet/cadence/macb.h:1097:9: note: in expansion of macro 'GEM_STAT_TITLE'
> 1097 | GEM_STAT_TITLE(RX1519CNT, "rx_greater_than_1518_byte_frames"),
> | ^~~~~~~~~~~~~~
>
> Since these strings are copied with memcpy() they do not need to be
> NUL terminated, and can use __nonstring:
>
> memcpy(p, gem_statistics[i].stat_string,
> ETH_GSTRING_LEN);
>
> Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117178 [1]
> Signed-off-by: Kees Cook <kees@kernel.org>
> ---
Reviewed-by: Jacob Keller <jacob.e.keller@intel.com>
> v3: improve commit message (Bill), drop whitespace change (Bill)
> v2: https://lore.kernel.org/lkml/20250311224412.it.153-kees@kernel.org/
> v1: https://lore.kernel.org/lkml/20250310222415.work.815-kees@kernel.org/
> Cc: Nicolas Ferre <nicolas.ferre@microchip.com>
> Cc: Claudiu Beznea <claudiu.beznea@tuxon.dev>
> Cc: Andrew Lunn <andrew+netdev@lunn.ch>
> Cc: "David S. Miller" <davem@davemloft.net>
> Cc: Eric Dumazet <edumazet@google.com>
> Cc: Jakub Kicinski <kuba@kernel.org>
> Cc: Paolo Abeni <pabeni@redhat.com>
> Cc: netdev@vger.kernel.org
> ---
> drivers/net/ethernet/cadence/macb.h | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/net/ethernet/cadence/macb.h b/drivers/net/ethernet/cadence/macb.h
> index 2847278d9cd4..d2a4c180d6a6 100644
> --- a/drivers/net/ethernet/cadence/macb.h
> +++ b/drivers/net/ethernet/cadence/macb.h
> @@ -1027,7 +1027,7 @@ struct gem_stats {
> * this register should contribute to.
> */
> struct gem_statistic {
> - char stat_string[ETH_GSTRING_LEN];
> + char stat_string[ETH_GSTRING_LEN] __nonstring;
> int offset;
> u32 stat_bits;
> };
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH v3] net: macb: Add __nonstring annotations for unterminated strings
2025-03-12 20:07 [PATCH v3] net: macb: Add __nonstring annotations for unterminated strings Kees Cook
2025-03-12 22:57 ` Jacob Keller
@ 2025-03-13 13:25 ` Andrew Lunn
2025-03-13 17:44 ` Kees Cook
2025-03-19 18:40 ` patchwork-bot+netdevbpf
2 siblings, 1 reply; 5+ messages in thread
From: Andrew Lunn @ 2025-03-13 13:25 UTC (permalink / raw)
To: Kees Cook
Cc: Nicolas Ferre, Claudiu Beznea, Andrew Lunn, David S. Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni, netdev, linux-kernel,
linux-hardening
> struct gem_statistic {
> - char stat_string[ETH_GSTRING_LEN];
> + char stat_string[ETH_GSTRING_LEN] __nonstring;
This general pattern of a char foo[ETH_GSTRING_LEN]' will appear
throughout drivers/net/ethernet. Maybe Coccinelle can find them all
and add the __nonstring ?
Reviewed-by: Andrew Lunn <andrew@lunn.ch>
Andrew
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH v3] net: macb: Add __nonstring annotations for unterminated strings
2025-03-13 13:25 ` Andrew Lunn
@ 2025-03-13 17:44 ` Kees Cook
0 siblings, 0 replies; 5+ messages in thread
From: Kees Cook @ 2025-03-13 17:44 UTC (permalink / raw)
To: Andrew Lunn
Cc: Nicolas Ferre, Claudiu Beznea, Andrew Lunn, David S. Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni, netdev, linux-kernel,
linux-hardening
On Thu, Mar 13, 2025 at 02:25:09PM +0100, Andrew Lunn wrote:
> > struct gem_statistic {
> > - char stat_string[ETH_GSTRING_LEN];
> > + char stat_string[ETH_GSTRING_LEN] __nonstring;
>
> This general pattern of a char foo[ETH_GSTRING_LEN]' will appear
> throughout drivers/net/ethernet. Maybe Coccinelle can find them all
> and add the __nonstring ?
I had that same thought but then rediscovered ethtool_puts() and
ethtool_sprintf(), which operate on C Strings and write out C Strings...
so _some_ ethtool stats are being constructed in a way that we can't
just universally apply __nonstring to all ETH_GSTRING_LEN strings. :(
--
Kees Cook
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v3] net: macb: Add __nonstring annotations for unterminated strings
2025-03-12 20:07 [PATCH v3] net: macb: Add __nonstring annotations for unterminated strings Kees Cook
2025-03-12 22:57 ` Jacob Keller
2025-03-13 13:25 ` Andrew Lunn
@ 2025-03-19 18:40 ` patchwork-bot+netdevbpf
2 siblings, 0 replies; 5+ messages in thread
From: patchwork-bot+netdevbpf @ 2025-03-19 18:40 UTC (permalink / raw)
To: Kees Cook
Cc: nicolas.ferre, claudiu.beznea, andrew+netdev, davem, edumazet,
kuba, pabeni, netdev, linux-kernel, linux-hardening
Hello:
This patch was applied to netdev/net-next.git (main)
by Paolo Abeni <pabeni@redhat.com>:
On Wed, 12 Mar 2025 13:07:01 -0700 you wrote:
> When a character array without a terminating NUL character has a static
> initializer, GCC 15's -Wunterminated-string-initialization will only
> warn if the array lacks the "nonstring" attribute[1]. Mark the arrays
> with __nonstring to correctly identify the char array as "not a C string"
> and thereby eliminate the warning:
>
> In file included from ../drivers/net/ethernet/cadence/macb_main.c:42:
> ../drivers/net/ethernet/cadence/macb.h:1070:35: warning: initializer-string for array of 'char' truncates NUL terminator but destination lacks 'nonstring' attribute (33 chars into 32 available) [-Wunterminated-string-initialization]
> 1070 | GEM_STAT_TITLE(TX1519CNT, "tx_greater_than_1518_byte_frames"),
> | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> ../drivers/net/ethernet/cadence/macb.h:1050:24: note: in definition of macro 'GEM_STAT_TITLE_BITS'
> 1050 | .stat_string = title, \
> | ^~~~~
> ../drivers/net/ethernet/cadence/macb.h:1070:9: note: in expansion of macro 'GEM_STAT_TITLE'
> 1070 | GEM_STAT_TITLE(TX1519CNT, "tx_greater_than_1518_byte_frames"),
> | ^~~~~~~~~~~~~~
> ../drivers/net/ethernet/cadence/macb.h:1097:35: warning: initializer-string for array of 'char' truncates NUL terminator but destination lacks 'nonstring' attribute (33 chars into 32 available) [-Wunterminated-string-initialization]
> 1097 | GEM_STAT_TITLE(RX1519CNT, "rx_greater_than_1518_byte_frames"),
> | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> ../drivers/net/ethernet/cadence/macb.h:1050:24: note: in definition of macro 'GEM_STAT_TITLE_BITS'
> 1050 | .stat_string = title, \
> | ^~~~~
> ../drivers/net/ethernet/cadence/macb.h:1097:9: note: in expansion of macro 'GEM_STAT_TITLE'
> 1097 | GEM_STAT_TITLE(RX1519CNT, "rx_greater_than_1518_byte_frames"),
> | ^~~~~~~~~~~~~~
>
> [...]
Here is the summary with links:
- [v3] net: macb: Add __nonstring annotations for unterminated strings
https://git.kernel.org/netdev/net-next/c/3d97da0ee625
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2025-03-19 18:39 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-12 20:07 [PATCH v3] net: macb: Add __nonstring annotations for unterminated strings Kees Cook
2025-03-12 22:57 ` Jacob Keller
2025-03-13 13:25 ` Andrew Lunn
2025-03-13 17:44 ` Kees Cook
2025-03-19 18:40 ` patchwork-bot+netdevbpf
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).