Skip to contentSkip to conversation

string.h: Use ARRAY_SIZE() for memtostr*()/strtomem*()

PATCH In review Kees Cook 2 participants 6 messages Feb 6, 2025 – Feb 7, 2025

Subsystems (2)
lore
Share & tools
lore

Kees Cook

author maintainer
#0 lore raw

v2 cover string.h: Use ARRAY_SIZE() for memtostr*()/strtomem*()

Work around a Clang <15 bug by switching to ARRAY_SIZE() (with the added benefit of explicitly checking for char array arguments) in memtostr*/strtomem*().

v2:
- change __must_be_byte_array to __must_be_byte_array() (david)
- add missed Suggested-by (kent)
- clarify move log (miguel)
- clarify cover letter Clang version (andy)
v1: https://lore.kernel.org/lkml/[email protected]/

-Kees

Kees Cook (3):
compiler.h: Move C string helpers into C-only kernel section compiler.h: Introduce __must_be_byte_array() string.h: Use ARRAY_SIZE() for memtostr*()/strtomem*()

 include/linux/compiler.h | 32 +++++++++++++++++++-------------
 include/linux/string.h   | 12 ++++++++----
 2 files changed, 27 insertions(+), 17 deletions(-)
Signature · 1 line
2.34.1
author maintainer
#1 lore raw

PATCH v2 1/3 compiler.h: Move C string helpers into C-only kernel section

The C kernel helpers for evaluating C Strings were positioned where they were visible to assembly inclusion, which was not intended. Move them into the kernel and C-only area of the header so future changes won't confuse the assembler.

Fixes d7a516c6eeae ("compiler.h: Fix undefined BUILD_BUG_ON_ZERO()")
Fixes 559048d156ff ("string: Check for "nonstring" attribute on strscpy() arguments")
---
 include/linux/compiler.h | 26 +++++++++++++-------------
 1 file changed, 13 insertions(+), 13 deletions(-)
include/linux/compiler.h +13 -13
index 240c632c5b95..7af999a131cb 100644
@@ -214,6 +214,19 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
 	__v;								\
 })
 
+#ifdef __CHECKER__
+#define __BUILD_BUG_ON_ZERO_MSG(e, msg) (0)
+#else /* __CHECKER__ */
+#define __BUILD_BUG_ON_ZERO_MSG(e, msg) ((int)sizeof(struct {_Static_assert(!(e), msg);}))
+#endif /* __CHECKER__ */
+
+/* &a[0] degrades to a pointer: a different type from an array */
+#define __must_be_array(a)	__BUILD_BUG_ON_ZERO_MSG(__same_type((a), &(a)[0]), "must be array")
+
+/* Require C Strings (i.e. NUL-terminated) lack the "nonstring" attribute. */
+#define __must_be_cstr(p) \
+	__BUILD_BUG_ON_ZERO_MSG(__annotated(p, nonstring), "must be cstr (NUL-terminated)")
+
 #endif /* __KERNEL__ */
 
 /**
@@ -254,19 +267,6 @@ static inline void *offset_to_ptr(const int *off)
 
 #define __ADDRESSABLE_ASM_STR(sym) __stringify(__ADDRESSABLE_ASM(sym))
 
-#ifdef __CHECKER__
-#define __BUILD_BUG_ON_ZERO_MSG(e, msg) (0)
-#else /* __CHECKER__ */
-#define __BUILD_BUG_ON_ZERO_MSG(e, msg) ((int)sizeof(struct {_Static_assert(!(e), msg);}))
-#endif /* __CHECKER__ */
-
-/* &a[0] degrades to a pointer: a different type from an array */
-#define __must_be_array(a)	__BUILD_BUG_ON_ZERO_MSG(__same_type((a), &(a)[0]), "must be array")
-
-/* Require C Strings (i.e. NUL-terminated) lack the "nonstring" attribute. */
-#define __must_be_cstr(p) \
-	__BUILD_BUG_ON_ZERO_MSG(__annotated(p, nonstring), "must be cstr (NUL-terminated)")
-
 /*
  * This returns a constant expression while determining if an argument is
  * a constant expression, most importantly without evaluating the argument.
Signature · 1 line
2.34.1
author maintainer
#2 lore raw

PATCH v2 2/3 compiler.h: Introduce __must_be_byte_array()

In reply to Kees Cook

In preparation for adding stricter type checking to the str/mem*() helpers, provide a way to check that a variable is a byte array via __must_be_byte_array().

---
 include/linux/compiler.h | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
include/linux/compiler.h +7 -1
index 7af999a131cb..1c0688319435 100644
@@ -221,7 +221,13 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
 #endif /* __CHECKER__ */
 
 /* &a[0] degrades to a pointer: a different type from an array */
-#define __must_be_array(a)	__BUILD_BUG_ON_ZERO_MSG(__same_type((a), &(a)[0]), "must be array")
+#define __is_array(a)		(!__same_type((a), &(a)[0]))
+#define __must_be_array(a)	__BUILD_BUG_ON_ZERO_MSG(!__is_array(a), \
+							"must be array")
+
+#define __is_byte_array(a)	(__is_array(a) && sizeof((a)[0]) == 1)
+#define __must_be_byte_array(a)	__BUILD_BUG_ON_ZERO_MSG(!__is_byte_array(a), \
+							"must be byte array")
 
 /* Require C Strings (i.e. NUL-terminated) lack the "nonstring" attribute. */
 #define __must_be_cstr(p) \
Signature · 1 line
2.34.1
author maintainer
#3 lore raw

PATCH v2 3/3 string.h: Use ARRAY_SIZE() for memtostr*()/strtomem*()

In reply to Kees Cook

The destination argument of memtostr*() and strtomem*() must be a fixed-size char array at compile time, so there is no need to use __builtin_object_size() (which is useful for when an argument is either a pointer or unknown). Instead use ARRAY_SIZE(), which has the benefit of working around a bug in Clang (fixed[1] in 15+) that got __builtin_object_size() wrong sometimes.

---
Cc: Suren Baghdasaryan <[email protected]>
Cc: [email protected]
Cc: Andy Shevchenko <[email protected]>
Cc: [email protected]

---
 include/linux/string.h | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
include/linux/string.h +8 -4
index 493ac4862c77..fc5ae145bd78 100644
@@ -411,7 +411,8 @@ void memcpy_and_pad(void *dest, size_t dest_len, const void *src, size_t count,
  * must be discoverable by the compiler.
  */
 #define strtomem_pad(dest, src, pad)	do {				\
-	const size_t _dest_len = __builtin_object_size(dest, 1);	\
+	const size_t _dest_len = __must_be_byte_array(dest) +		\
+				 ARRAY_SIZE(dest);			\
 	const size_t _src_len = __builtin_object_size(src, 1);		\
 									\
 	BUILD_BUG_ON(!__builtin_constant_p(_dest_len) ||		\
@@ -434,7 +435,8 @@ void memcpy_and_pad(void *dest, size_t dest_len, const void *src, size_t count,
  * must be discoverable by the compiler.
  */
 #define strtomem(dest, src)	do {					\
-	const size_t _dest_len = __builtin_object_size(dest, 1);	\
+	const size_t _dest_len = __must_be_byte_array(dest) +		\
+				 ARRAY_SIZE(dest);			\
 	const size_t _src_len = __builtin_object_size(src, 1);		\
 									\
 	BUILD_BUG_ON(!__builtin_constant_p(_dest_len) ||		\
@@ -453,7 +455,8 @@ void memcpy_and_pad(void *dest, size_t dest_len, const void *src, size_t count,
  * Note that sizes of @dest and @src must be known at compile-time.
  */
 #define memtostr(dest, src)	do {					\
-	const size_t _dest_len = __builtin_object_size(dest, 1);	\
+	const size_t _dest_len = __must_be_byte_array(dest) +		\
+				 ARRAY_SIZE(dest);			\
 	const size_t _src_len = __builtin_object_size(src, 1);		\
 	const size_t _src_chars = strnlen(src, _src_len);		\
 	const size_t _copy_len = min(_dest_len - 1, _src_chars);	\
@@ -478,7 +481,8 @@ void memcpy_and_pad(void *dest, size_t dest_len, const void *src, size_t count,
  * Note that sizes of @dest and @src must be known at compile-time.
  */
 #define memtostr_pad(dest, src)		do {				\
-	const size_t _dest_len = __builtin_object_size(dest, 1);	\
+	const size_t _dest_len = __must_be_byte_array(dest) +		\
+				 ARRAY_SIZE(dest);			\
 	const size_t _src_len = __builtin_object_size(src, 1);		\
 	const size_t _src_chars = strnlen(src, _src_len);		\
 	const size_t _copy_len = min(_dest_len - 1, _src_chars);	\
Signature · 1 line
2.34.1
Tested-by on v2 3/3
#4 lore raw
Quoted 8 lines · Kees Cook

The destination argument of memtostr*() and strtomem*() must be a fixed-size char array at compile time, so there is no need to use __builtin_object_size() (which is useful for when an argument is either a pointer or unknown). Instead use ARRAY_SIZE(), which has the benefit of working around a bug in Clang (fixed[1] in 15+) that got __builtin_object_size() wrong sometimes.

Quoted 5 lines

Thanks Kees!
CC'ing Andrew Morton since mm-unstable contains my workaround patch
[1] for the issue fixed by this patchset. I'm not sure which tree will be taking these fixes but once they are in we should drop [1]. Or maybe we just drop it now from mm-unstable? Seems like only testbot was the only one who complained...

[1] 0e91345e75b2 ("alloc_tag: work around clang-14 build issue with __builtin_object_size()")

Quoted 56 lines

---
Cc: Suren Baghdasaryan <[email protected]>
Cc: [email protected]
Cc: Andy Shevchenko <[email protected]>
Cc: [email protected]

---
 include/linux/string.h | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
include/linux/string.h +8 -5
index 493ac4862c77..fc5ae145bd78 100644
@@ -411,7 +411,8 @@ void memcpy_and_pad(void *dest, size_t dest_len, const void *src, size_t count,
  * must be discoverable by the compiler.
  */
 #define strtomem_pad(dest, src, pad)   do {                            \
-       const size_t _dest_len = __builtin_object_size(dest, 1);        \
+       const size_t _dest_len = __must_be_byte_array(dest) +           \
+                                ARRAY_SIZE(dest);                      \
        const size_t _src_len = __builtin_object_size(src, 1);          \
                                                                        \
        BUILD_BUG_ON(!__builtin_constant_p(_dest_len) ||                \
@@ -434,7 +435,8 @@ void memcpy_and_pad(void *dest, size_t dest_len, const void *src, size_t count,
  * must be discoverable by the compiler.
  */
 #define strtomem(dest, src)    do {                                    \
-       const size_t _dest_len = __builtin_object_size(dest, 1);        \
+       const size_t _dest_len = __must_be_byte_array(dest) +           \
+                                ARRAY_SIZE(dest);                      \
        const size_t _src_len = __builtin_object_size(src, 1);          \
                                                                        \
        BUILD_BUG_ON(!__builtin_constant_p(_dest_len) ||                \
@@ -453,7 +455,8 @@ void memcpy_and_pad(void *dest, size_t dest_len, const void *src, size_t count,
  * Note that sizes of @dest and @src must be known at compile-time.
  */
 #define memtostr(dest, src)    do {                                    \
-       const size_t _dest_len = __builtin_object_size(dest, 1);        \
+       const size_t _dest_len = __must_be_byte_array(dest) +           \
+                                ARRAY_SIZE(dest);                      \
        const size_t _src_len = __builtin_object_size(src, 1);          \
        const size_t _src_chars = strnlen(src, _src_len);               \
        const size_t _copy_len = min(_dest_len - 1, _src_chars);        \
@@ -478,7 +481,8 @@ void memcpy_and_pad(void *dest, size_t dest_len, const void *src, size_t count,
  * Note that sizes of @dest and @src must be known at compile-time.
  */
 #define memtostr_pad(dest, src)                do {                            \
-       const size_t _dest_len = __builtin_object_size(dest, 1);        \
+       const size_t _dest_len = __must_be_byte_array(dest) +           \
+                                ARRAY_SIZE(dest);                      \
        const size_t _src_len = __builtin_object_size(src, 1);          \
        const size_t _src_chars = strnlen(src, _src_len);               \
        const size_t _copy_len = min(_dest_len - 1, _src_chars);        \
--

2.34.1

author maintainer
#5 lore raw
Quoted 24 lines · Suren Baghdasaryan
Quoted 8 lines · Kees Cook

The destination argument of memtostr*() and strtomem*() must be a fixed-size char array at compile time, so there is no need to use __builtin_object_size() (which is useful for when an argument is either a pointer or unknown). Instead use ARRAY_SIZE(), which has the benefit of working around a bug in Clang (fixed[1] in 15+) that got __builtin_object_size() wrong sometimes.

Quoted 5 lines

Thanks Kees!
CC'ing Andrew Morton since mm-unstable contains my workaround patch
[1] for the issue fixed by this patchset. I'm not sure which tree will be taking these fixes but once they are in we should drop [1]. Or maybe we just drop it now from mm-unstable? Seems like only testbot was the only one who complained...

Quoted 2 lines

[1] 0e91345e75b2 ("alloc_tag: work around clang-14 build issue with __builtin_object_size()")

Yeah, that should be dropped. I intend to send this fix series to Linus tomorrow or Saturday before rc2.

-Kees

Quoted 57 lines
Quoted 57 lines

---
Cc: Suren Baghdasaryan <[email protected]>
Cc: [email protected]
Cc: Andy Shevchenko <[email protected]>
Cc: [email protected]

---
 include/linux/string.h | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
include/linux/string.h +8 -5
index 493ac4862c77..fc5ae145bd78 100644
@@ -411,7 +411,8 @@ void memcpy_and_pad(void *dest, size_t dest_len, const void *src, size_t count,
  * must be discoverable by the compiler.
  */
 #define strtomem_pad(dest, src, pad)   do {                            \
-       const size_t _dest_len = __builtin_object_size(dest, 1);        \
+       const size_t _dest_len = __must_be_byte_array(dest) +           \
+                                ARRAY_SIZE(dest);                      \
        const size_t _src_len = __builtin_object_size(src, 1);          \
                                                                        \
        BUILD_BUG_ON(!__builtin_constant_p(_dest_len) ||                \
@@ -434,7 +435,8 @@ void memcpy_and_pad(void *dest, size_t dest_len, const void *src, size_t count,
  * must be discoverable by the compiler.
  */
 #define strtomem(dest, src)    do {                                    \
-       const size_t _dest_len = __builtin_object_size(dest, 1);        \
+       const size_t _dest_len = __must_be_byte_array(dest) +           \
+                                ARRAY_SIZE(dest);                      \
        const size_t _src_len = __builtin_object_size(src, 1);          \
                                                                        \
        BUILD_BUG_ON(!__builtin_constant_p(_dest_len) ||                \
@@ -453,7 +455,8 @@ void memcpy_and_pad(void *dest, size_t dest_len, const void *src, size_t count,
  * Note that sizes of @dest and @src must be known at compile-time.
  */
 #define memtostr(dest, src)    do {                                    \
-       const size_t _dest_len = __builtin_object_size(dest, 1);        \
+       const size_t _dest_len = __must_be_byte_array(dest) +           \
+                                ARRAY_SIZE(dest);                      \
        const size_t _src_len = __builtin_object_size(src, 1);          \
        const size_t _src_chars = strnlen(src, _src_len);               \
        const size_t _copy_len = min(_dest_len - 1, _src_chars);        \
@@ -478,7 +481,8 @@ void memcpy_and_pad(void *dest, size_t dest_len, const void *src, size_t count,
  * Note that sizes of @dest and @src must be known at compile-time.
  */
 #define memtostr_pad(dest, src)                do {                            \
-       const size_t _dest_len = __builtin_object_size(dest, 1);        \
+       const size_t _dest_len = __must_be_byte_array(dest) +           \
+                                ARRAY_SIZE(dest);                      \
        const size_t _src_len = __builtin_object_size(src, 1);          \
        const size_t _src_chars = strnlen(src, _src_len);               \
        const size_t _copy_len = min(_dest_len - 1, _src_chars);        \
--

2.34.1

Signature · 1 line
Kees Cook