diff mbox series

wireguard: noise: Add __nonstring annotations for unterminated strings

Message ID 20250310222249.work.154-kees@kernel.org (mailing list archive)
State New
Headers show
Series wireguard: noise: Add __nonstring annotations for unterminated strings | expand

Commit Message

Kees Cook March 10, 2025, 10:22 p.m. UTC
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 and correctly identify the char array as "not a C
string" and thereby eliminate the warning.

Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117178 [1]
Cc: Jason A. Donenfeld <Jason@zx2c4.com>
Cc: "Jason A. Donenfeld" <Jason@zx2c4.com>
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: wireguard@lists.zx2c4.com
Cc: netdev@vger.kernel.org
Signed-off-by: Kees Cook <kees@kernel.org>
---
 drivers/net/wireguard/noise.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Jakub Kicinski March 11, 2025, 10:19 a.m. UTC | #1
On Mon, 10 Mar 2025 15:22:50 -0700 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 and correctly identify the char array as "not a C
> string" and thereby eliminate the warning.

Hi! Would marking all of u8 as non-string not be an option? How many 
of such warnings do we have in the tree? Feel free to point me to a
previous conversation.
Kees Cook March 11, 2025, 10:30 p.m. UTC | #2
On Tue, Mar 11, 2025 at 11:19:27AM +0100, Jakub Kicinski wrote:
> On Mon, 10 Mar 2025 15:22:50 -0700 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 and correctly identify the char array as "not a C
> > string" and thereby eliminate the warning.
> 
> Hi! Would marking all of u8 as non-string not be an option? How many 
> of such warnings do we have in the tree? Feel free to point me to a
> previous conversation.

*thread merge*

On Mon, Mar 10, 2025 at 06:38:01PM -0400, James Bottomley wrote[1]:
> This looks a bit suboptimal ... is there anywhere in the kernel u8[] is
> actually used for real strings?  In which case it would seem the better
> place to put the annotation is in the typedef for u8 arrays.

So both of you asked basically same question, and I think the simple answer
is "no we can't mark u8 as nonstring". The use of u8 has become
synonymous with "char" for a long while now, and it's gotten even more
common after we made char unsigned by default.

The number of warning sources is pretty small. I think I have identified
and proposed fixes most of them already[2]. ACPICA needs an upstream
change, which I've submitted[3]. And ACPI needed multidimensional
nonstring annotation support, which had the last needed bit added to GCC
today[4], and I've proposed support for it in the kernel[5]. With 4 and 5
ready, I can send the final patch, which is basically just this (and
actually accounts for the vast majority of warnings emitted):

-static const char table_sigs[][ACPI_NAMESEG_SIZE] __initconst = {
+static const char table_sigs[][ACPI_NAMESEG_SIZE] __nonstring_array __initconst = {

-Kees

[1] https://lore.kernel.org/lkml/98ca3727d65a418e403b03f6b17341dbcb192764.camel@HansenPartnership.com/
[2] https://lore.kernel.org/lkml/?q=f%3AKees+%22-Wunterminated-string-initialization%22
[3] https://github.com/acpica/acpica/pull/1006
[4] https://github.com/gcc-mirror/gcc/commit/afb46540d3921e96c4cd7ba8fa2c8b0901759455
[5] https://lore.kernel.org/lkml/20250310214244.work.194-kees@kernel.org/
diff mbox series

Patch

diff --git a/drivers/net/wireguard/noise.c b/drivers/net/wireguard/noise.c
index 202a33af5a72..7eb9a23a3d4d 100644
--- a/drivers/net/wireguard/noise.c
+++ b/drivers/net/wireguard/noise.c
@@ -25,8 +25,8 @@ 
  * <- e, ee, se, psk, {}
  */
 
-static const u8 handshake_name[37] = "Noise_IKpsk2_25519_ChaChaPoly_BLAKE2s";
-static const u8 identifier_name[34] = "WireGuard v1 zx2c4 Jason@zx2c4.com";
+static const u8 handshake_name[37] __nonstring = "Noise_IKpsk2_25519_ChaChaPoly_BLAKE2s";
+static const u8 identifier_name[34] __nonstring = "WireGuard v1 zx2c4 Jason@zx2c4.com";
 static u8 handshake_init_hash[NOISE_HASH_LEN] __ro_after_init;
 static u8 handshake_init_chaining_key[NOISE_HASH_LEN] __ro_after_init;
 static atomic64_t keypair_counter = ATOMIC64_INIT(0);