Skip to content

Commit 9f25b1f

Browse files
committed
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 559048d ("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 <[email protected]> Reported-by: Stephen Rothwell <[email protected]> Reported-by: Christophe Leroy <[email protected]> Reported-by: Michael Kelley <[email protected]> Closes: https://lore.kernel.org/all/[email protected]/ Tested-by: Michael Kelley <[email protected]> Signed-off-by: Kees Cook <[email protected]>
1 parent 4c2d8a6 commit 9f25b1f

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

include/linux/compiler.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,25 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
206206
#define __must_be_byte_array(a) __BUILD_BUG_ON_ZERO_MSG(!__is_byte_array(a), \
207207
"must be byte array")
208208

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+
209221
/* Require C Strings (i.e. NUL-terminated) lack the "nonstring" attribute. */
210222
#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)")
212228

213229
#endif /* __KERNEL__ */
214230

include/linux/compiler_types.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -446,11 +446,14 @@ struct ftrace_likely_data {
446446
#define __member_size(p) __builtin_object_size(p, 1)
447447
#endif
448448

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+
*/
450455
#if __has_builtin(__builtin_has_attribute)
451456
#define __annotated(var, attr) __builtin_has_attribute(var, attr)
452-
#else
453-
#define __annotated(var, attr) (false)
454457
#endif
455458

456459
/*

0 commit comments

Comments
 (0)