Skip to content

Commit 559048d

Browse files
committed
string: Check for "nonstring" attribute on strscpy() arguments
GCC already checks for arguments that are marked with the "nonstring"[1] attribute when used on standard C String API functions (e.g. strcpy). Gain this compile-time checking also for the kernel's primary string copying function, strscpy(). Note that Clang has neither "nonstring" nor __builtin_has_attribute(). Link: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-nonstring-variable-attribute [1] Reviewed-by: Miguel Ojeda <[email protected]> Tested-by: Miguel Ojeda <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Kees Cook <[email protected]>
1 parent 32ef4b7 commit 559048d

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

include/linux/compiler.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,9 @@ static inline void *offset_to_ptr(const int *off)
242242
/* &a[0] degrades to a pointer: a different type from an array */
243243
#define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
244244

245+
/* Require C Strings (i.e. NUL-terminated) lack the "nonstring" attribute. */
246+
#define __must_be_cstr(p) BUILD_BUG_ON_ZERO(__annotated(p, nonstring))
247+
245248
/*
246249
* This returns a constant expression while determining if an argument is
247250
* a constant expression, most importantly without evaluating the argument.

include/linux/compiler_types.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,13 @@ struct ftrace_likely_data {
421421
#define __member_size(p) __builtin_object_size(p, 1)
422422
#endif
423423

424+
/* Determine if an attribute has been applied to a variable. */
425+
#if __has_builtin(__builtin_has_attribute)
426+
#define __annotated(var, attr) __builtin_has_attribute(var, attr)
427+
#else
428+
#define __annotated(var, attr) (false)
429+
#endif
430+
424431
/*
425432
* Some versions of gcc do not mark 'asm goto' volatile:
426433
*

include/linux/string.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,16 @@ ssize_t sized_strscpy(char *, const char *, size_t);
7676
* known size.
7777
*/
7878
#define __strscpy0(dst, src, ...) \
79-
sized_strscpy(dst, src, sizeof(dst) + __must_be_array(dst))
80-
#define __strscpy1(dst, src, size) sized_strscpy(dst, src, size)
79+
sized_strscpy(dst, src, sizeof(dst) + __must_be_array(dst) + \
80+
__must_be_cstr(dst) + __must_be_cstr(src))
81+
#define __strscpy1(dst, src, size) \
82+
sized_strscpy(dst, src, size + __must_be_cstr(dst) + __must_be_cstr(src))
8183

8284
#define __strscpy_pad0(dst, src, ...) \
83-
sized_strscpy_pad(dst, src, sizeof(dst) + __must_be_array(dst))
84-
#define __strscpy_pad1(dst, src, size) sized_strscpy_pad(dst, src, size)
85+
sized_strscpy_pad(dst, src, sizeof(dst) + __must_be_array(dst) + \
86+
__must_be_cstr(dst) + __must_be_cstr(src))
87+
#define __strscpy_pad1(dst, src, size) \
88+
sized_strscpy_pad(dst, src, size + __must_be_cstr(dst) + __must_be_cstr(src))
8589

8690
/**
8791
* strscpy - Copy a C-string into a sized buffer

0 commit comments

Comments
 (0)