Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

compiler.h: Introduce __must_be_noncstr()

In preparation for adding more type checking to the memtostr/strtomem*()
helpers, introduce the ability to check for the "nonstring" attribute.
This is the reverse of what was added to strscpy*() in commit 559048d156ff
("string: Check for "nonstring" attribute on strscpy() arguments").

Note that __annotated() must be explicitly tested for, as GCC added
__builtin_has_attribute() after it added the "nonstring" attribute. Do
so here to avoid the !__annotated() test triggering build failures
when __builtin_has_attribute() was missing but __nonstring was defined.
(I've opted to squash this fix into this patch so we don't end up with
a possible bisection target that would leave the kernel unbuildable.)

Reported-by: Venkat Rao Bagalkote <venkat88@linux.vnet.ibm.com>
Reported-by: Stephen Rothwell <sfr@canb.auug.org.au>
Reported-by: Christophe Leroy <christophe.leroy@csgroup.eu>
Reported-by: Michael Kelley <mhklinux@outlook.com>
Closes: https://lore.kernel.org/all/adbe8dd1-a725-4811-ae7e-76fe770cf096@linux.vnet.ibm.com/
Tested-by: Michael Kelley <mhklinux@outlook.com>
Signed-off-by: Kees Cook <kees@kernel.org>

+23 -4
+17 -1
include/linux/compiler.h
··· 206 206 #define __must_be_byte_array(a) __BUILD_BUG_ON_ZERO_MSG(!__is_byte_array(a), \ 207 207 "must be byte array") 208 208 209 + /* 210 + * If the "nonstring" attribute isn't available, we have to return true 211 + * so the __must_*() checks pass when "nonstring" isn't supported. 212 + */ 213 + #if __has_attribute(__nonstring__) && defined(__annotated) 214 + #define __is_cstr(a) (!__annotated(a, nonstring)) 215 + #define __is_noncstr(a) (__annotated(a, nonstring)) 216 + #else 217 + #define __is_cstr(a) (true) 218 + #define __is_noncstr(a) (true) 219 + #endif 220 + 209 221 /* Require C Strings (i.e. NUL-terminated) lack the "nonstring" attribute. */ 210 222 #define __must_be_cstr(p) \ 211 - __BUILD_BUG_ON_ZERO_MSG(__annotated(p, nonstring), "must be cstr (NUL-terminated)") 223 + __BUILD_BUG_ON_ZERO_MSG(!__is_cstr(p), \ 224 + "must be C-string (NUL-terminated)") 225 + #define __must_be_noncstr(p) \ 226 + __BUILD_BUG_ON_ZERO_MSG(!__is_noncstr(p), \ 227 + "must be non-C-string (not NUL-terminated)") 212 228 213 229 #endif /* __KERNEL__ */ 214 230
+6 -3
include/linux/compiler_types.h
··· 446 446 #define __member_size(p) __builtin_object_size(p, 1) 447 447 #endif 448 448 449 - /* Determine if an attribute has been applied to a variable. */ 449 + /* 450 + * Determine if an attribute has been applied to a variable. 451 + * Using __annotated needs to check for __annotated being available, 452 + * or negative tests may fail when annotation cannot be checked. For 453 + * example, see the definition of __is_cstr(). 454 + */ 450 455 #if __has_builtin(__builtin_has_attribute) 451 456 #define __annotated(var, attr) __builtin_has_attribute(var, attr) 452 - #else 453 - #define __annotated(var, attr) (false) 454 457 #endif 455 458 456 459 /*