From: "Jason A. Donenfeld" <Jason@zx2c4.com>
To: Linus Torvalds <torvalds@linux-foundation.org>,
Eric Biggers <ebiggers@kernel.org>,
Ard Biesheuvel <ardb@kernel.org>, Kees Cook <kees@kernel.org>,
linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: "Jason A. Donenfeld" <Jason@zx2c4.com>
Subject: [PATCH libcrypto v2 2/3] compiler: introduce at_least parameter decoration pseudo keyword
Date: Thu, 20 Nov 2025 02:10:21 +0100 [thread overview]
Message-ID: <20251120011022.1558674-2-Jason@zx2c4.com> (raw)
In-Reply-To: <20251120011022.1558674-1-Jason@zx2c4.com>
Clang and recent gcc support warning if they are able to prove that the
user is passing to a function an array that is too short in size. For
example:
void blah(unsigned char herp[at_least 7]);
static void schma(void)
{
unsigned char good[] = { 1, 2, 3, 4, 5, 6, 7 };
unsigned char bad[] = { 1, 2, 3, 4, 5, 6 };
blah(good);
blah(bad);
}
The notation here, `static 7`, which this commit makes explicit by
allowing us to write it as `at_least 7`, means that it's incorrect to
pass anything less than 7 elements. This is section 6.7.5.3 of C99:
If the keyword static also appears within the [ and ] of the array
type derivation, then for each call to the function, the value of
the corresponding actual argument shall provide access to the first
element of an array with at least as many elements as specified by
the size expression.
Here is the output from gcc 15:
zx2c4@thinkpad /tmp $ gcc -c a.c
a.c: In function ‘schma’:
a.c:9:9: warning: ‘blah’ accessing 7 bytes in a region of size 6 [-Wstringop-overflow=]
9 | blah(bad);
| ^~~~~~~~~
a.c:9:9: note: referencing argument 1 of type ‘unsigned char[7]’
a.c:2:6: note: in a call to function ‘blah’
2 | void blah(unsigned char herp[at_least 7]);
| ^~~~
And from clang 21:
zx2c4@thinkpad /tmp $ clang -c a.c
a.c:9:2: warning: array argument is too small; contains 6 elements, callee requires at least 7
[-Warray-bounds]
9 | blah(bad);
| ^ ~~~
a.c:2:25: note: callee declares array parameter as static here
2 | void blah(unsigned char herp[at_least 7]);
| ^ ~~~~~~~~~~
1 warning generated.
So these are covered by, variously, -Wstringop-overflow and
-Warray-bounds.
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
include/linux/compiler.h | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/include/linux/compiler.h b/include/linux/compiler.h
index 5b45ea7dff3e..cbd3b466fdb9 100644
--- a/include/linux/compiler.h
+++ b/include/linux/compiler.h
@@ -379,6 +379,17 @@ static inline void *offset_to_ptr(const int *off)
*/
#define prevent_tail_call_optimization() mb()
+/*
+ * This designates the minimum number of elements a passed array parameter must
+ * have. For example:
+ *
+ * void some_function(u8 param[at_least 7]);
+ *
+ * If a caller passes an array with fewer than 7 elements, the compiler will
+ * emit a warning.
+ */
+#define at_least static
+
#include <asm/rwonce.h>
#endif /* __LINUX_COMPILER_H */
--
2.52.0
next prev parent reply other threads:[~2025-11-20 1:11 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-20 1:10 [PATCH libcrypto v2 1/3] wifi: iwlwifi: trans: rename at_least variable to min_mode Jason A. Donenfeld
2025-11-20 1:10 ` Jason A. Donenfeld [this message]
2025-11-21 11:03 ` [PATCH libcrypto v2 2/3] compiler: introduce at_least parameter decoration pseudo keyword Ard Biesheuvel
2025-11-22 2:45 ` Herbert Xu
2025-11-22 2:46 ` Jason A. Donenfeld
2025-11-22 3:08 ` Herbert Xu
2025-11-22 11:53 ` Ard Biesheuvel
2025-11-22 12:02 ` Jason A. Donenfeld
2025-11-22 19:19 ` Eric Biggers
2025-11-22 19:33 ` Eric Biggers
2025-11-23 13:21 ` Jason A. Donenfeld
2025-11-20 1:10 ` [PATCH libcrypto v2 3/3] crypto: chacha20poly1305: statically check fixed array lengths Jason A. Donenfeld
2025-11-21 11:04 ` Ard Biesheuvel
2025-11-21 11:03 ` [PATCH libcrypto v2 1/3] wifi: iwlwifi: trans: rename at_least variable to min_mode Ard Biesheuvel
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251120011022.1558674-2-Jason@zx2c4.com \
--to=jason@zx2c4.com \
--cc=ardb@kernel.org \
--cc=ebiggers@kernel.org \
--cc=kees@kernel.org \
--cc=linux-crypto@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=torvalds@linux-foundation.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®