Skip to content

Commit 600457d

Browse files
keesShujathMohd
authored andcommitted
fortify: Add Clang support
Enable FORTIFY_SOURCE support for Clang: Use the new __pass_object_size and __overloadable attributes so that Clang will have appropriate visibility into argument sizes such that __builtin_object_size(p, 1) will behave correctly. Additional details available here: llvm/llvm-project#53516 ClangBuiltLinux/linux#1401 A bug with __builtin_constant_p() of globally defined variables was fixed in Clang 13 (and backported to 12.0.1), so FORTIFY support must depend on that version or later. Additional details here: https://bugs.llvm.org/show_bug.cgi?id=41459 commit a52f8a5 ("fortify: Explicitly disable Clang support") A bug with Clang's -mregparm=3 and -m32 makes some builtins unusable, so removing -ffreestanding (to gain the needed libcall optimizations with Clang) cannot be done. Without the libcall optimizations, Clang cannot provide appropriate FORTIFY coverage, so it must be disabled for CONFIG_X86_32. Additional details here; llvm/llvm-project#53645 Cc: Miguel Ojeda <[email protected]> Cc: Nick Desaulniers <[email protected]> Cc: Nathan Chancellor <[email protected]> Cc: George Burgess IV <[email protected]> Cc: [email protected] Signed-off-by: Kees Cook <[email protected]> Reviewed-by: Nick Desaulniers <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 52fa3e2 commit 600457d

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

include/linux/fortify-string.h

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#include <linux/const.h>
66

7-
#define __FORTIFY_INLINE extern __always_inline __gnu_inline
7+
#define __FORTIFY_INLINE extern __always_inline __gnu_inline __overloadable
88
#define __RENAME(x) __asm__(#x)
99

1010
void fortify_panic(const char *name) __noreturn __cold;
@@ -52,8 +52,17 @@ extern char *__underlying_strncpy(char *p, const char *q, __kernel_size_t size)
5252
#define __underlying_strncpy __builtin_strncpy
5353
#endif
5454

55+
/*
56+
* Clang's use of __builtin_object_size() within inlines needs hinting via
57+
* __pass_object_size(). The preference is to only ever use type 1 (member
58+
* size, rather than struct size), but there remain some stragglers using
59+
* type 0 that will be converted in the future.
60+
*/
61+
#define POS __pass_object_size(1)
62+
#define POS0 __pass_object_size(0)
63+
5564
__FORTIFY_INLINE __diagnose_as(__builtin_strncpy, 1, 2, 3)
56-
char *strncpy(char * const p, const char *q, __kernel_size_t size)
65+
char *strncpy(char * const POS p, const char *q, __kernel_size_t size)
5766
{
5867
size_t p_size = __builtin_object_size(p, 1);
5968

@@ -65,7 +74,7 @@ char *strncpy(char * const p, const char *q, __kernel_size_t size)
6574
}
6675

6776
__FORTIFY_INLINE __diagnose_as(__builtin_strcat, 1, 2)
68-
char *strcat(char * const p, const char *q)
77+
char *strcat(char * const POS p, const char *q)
6978
{
7079
size_t p_size = __builtin_object_size(p, 1);
7180

@@ -77,7 +86,7 @@ char *strcat(char * const p, const char *q)
7786
}
7887

7988
extern __kernel_size_t __real_strnlen(const char *, __kernel_size_t) __RENAME(strnlen);
80-
__FORTIFY_INLINE __kernel_size_t strnlen(const char * const p, __kernel_size_t maxlen)
89+
__FORTIFY_INLINE __kernel_size_t strnlen(const char * const POS p, __kernel_size_t maxlen)
8190
{
8291
size_t p_size = __builtin_object_size(p, 1);
8392
size_t p_len = __compiletime_strlen(p);
@@ -106,7 +115,7 @@ __FORTIFY_INLINE __kernel_size_t strnlen(const char * const p, __kernel_size_t m
106115
__builtin_choose_expr(__is_constexpr(__builtin_strlen(p)), \
107116
__builtin_strlen(p), __fortify_strlen(p))
108117
__FORTIFY_INLINE __diagnose_as(__builtin_strlen, 1)
109-
__kernel_size_t __fortify_strlen(const char * const p)
118+
__kernel_size_t __fortify_strlen(const char * const POS p)
110119
{
111120
__kernel_size_t ret;
112121
size_t p_size = __builtin_object_size(p, 1);
@@ -122,7 +131,7 @@ __kernel_size_t __fortify_strlen(const char * const p)
122131

123132
/* defined after fortified strlen to reuse it */
124133
extern size_t __real_strlcpy(char *, const char *, size_t) __RENAME(strlcpy);
125-
__FORTIFY_INLINE size_t strlcpy(char * const p, const char * const q, size_t size)
134+
__FORTIFY_INLINE size_t strlcpy(char * const POS p, const char * const POS q, size_t size)
126135
{
127136
size_t p_size = __builtin_object_size(p, 1);
128137
size_t q_size = __builtin_object_size(q, 1);
@@ -149,7 +158,7 @@ __FORTIFY_INLINE size_t strlcpy(char * const p, const char * const q, size_t siz
149158

150159
/* defined after fortified strnlen to reuse it */
151160
extern ssize_t __real_strscpy(char *, const char *, size_t) __RENAME(strscpy);
152-
__FORTIFY_INLINE ssize_t strscpy(char * const p, const char * const q, size_t size)
161+
__FORTIFY_INLINE ssize_t strscpy(char * const POS p, const char * const POS q, size_t size)
153162
{
154163
size_t len;
155164
/* Use string size rather than possible enclosing struct size. */
@@ -196,7 +205,7 @@ __FORTIFY_INLINE ssize_t strscpy(char * const p, const char * const q, size_t si
196205

197206
/* defined after fortified strlen and strnlen to reuse them */
198207
__FORTIFY_INLINE __diagnose_as(__builtin_strncat, 1, 2, 3)
199-
char *strncat(char * const p, const char * const q, __kernel_size_t count)
208+
char *strncat(char * const POS p, const char * const POS q, __kernel_size_t count)
200209
{
201210
size_t p_len, copy_len;
202211
size_t p_size = __builtin_object_size(p, 1);
@@ -367,7 +376,7 @@ __FORTIFY_INLINE void fortify_memcpy_chk(__kernel_size_t size,
367376
memmove)
368377

369378
extern void *__real_memscan(void *, int, __kernel_size_t) __RENAME(memscan);
370-
__FORTIFY_INLINE void *memscan(void * const p, int c, __kernel_size_t size)
379+
__FORTIFY_INLINE void *memscan(void * const POS0 p, int c, __kernel_size_t size)
371380
{
372381
size_t p_size = __builtin_object_size(p, 0);
373382

@@ -379,7 +388,7 @@ __FORTIFY_INLINE void *memscan(void * const p, int c, __kernel_size_t size)
379388
}
380389

381390
__FORTIFY_INLINE __diagnose_as(__builtin_memcmp, 1, 2, 3)
382-
int memcmp(const void * const p, const void * const q, __kernel_size_t size)
391+
int memcmp(const void * const POS0 p, const void * const POS0 q, __kernel_size_t size)
383392
{
384393
size_t p_size = __builtin_object_size(p, 0);
385394
size_t q_size = __builtin_object_size(q, 0);
@@ -396,7 +405,7 @@ int memcmp(const void * const p, const void * const q, __kernel_size_t size)
396405
}
397406

398407
__FORTIFY_INLINE __diagnose_as(__builtin_memchr, 1, 2, 3)
399-
void *memchr(const void * const p, int c, __kernel_size_t size)
408+
void *memchr(const void * const POS0 p, int c, __kernel_size_t size)
400409
{
401410
size_t p_size = __builtin_object_size(p, 0);
402411

@@ -408,7 +417,7 @@ void *memchr(const void * const p, int c, __kernel_size_t size)
408417
}
409418

410419
void *__real_memchr_inv(const void *s, int c, size_t n) __RENAME(memchr_inv);
411-
__FORTIFY_INLINE void *memchr_inv(const void * const p, int c, size_t size)
420+
__FORTIFY_INLINE void *memchr_inv(const void * const POS0 p, int c, size_t size)
412421
{
413422
size_t p_size = __builtin_object_size(p, 0);
414423

@@ -420,7 +429,7 @@ __FORTIFY_INLINE void *memchr_inv(const void * const p, int c, size_t size)
420429
}
421430

422431
extern void *__real_kmemdup(const void *src, size_t len, gfp_t gfp) __RENAME(kmemdup);
423-
__FORTIFY_INLINE void *kmemdup(const void * const p, size_t size, gfp_t gfp)
432+
__FORTIFY_INLINE void *kmemdup(const void * const POS0 p, size_t size, gfp_t gfp)
424433
{
425434
size_t p_size = __builtin_object_size(p, 0);
426435

@@ -433,7 +442,7 @@ __FORTIFY_INLINE void *kmemdup(const void * const p, size_t size, gfp_t gfp)
433442

434443
/* Defined after fortified strlen to reuse it. */
435444
__FORTIFY_INLINE __diagnose_as(__builtin_strcpy, 1, 2)
436-
char *strcpy(char * const p, const char * const q)
445+
char *strcpy(char * const POS p, const char * const POS q)
437446
{
438447
size_t p_size = __builtin_object_size(p, 1);
439448
size_t q_size = __builtin_object_size(q, 1);
@@ -462,4 +471,7 @@ char *strcpy(char * const p, const char * const q)
462471
#undef __underlying_strncat
463472
#undef __underlying_strncpy
464473

474+
#undef POS
475+
#undef POS0
476+
465477
#endif /* _LINUX_FORTIFY_STRING_H_ */

security/Kconfig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,10 @@ config HARDENED_USERCOPY_PAGESPAN
191191
config FORTIFY_SOURCE
192192
bool "Harden common str/mem functions against buffer overflows"
193193
depends on ARCH_HAS_FORTIFY_SOURCE
194-
# https://bugs.llvm.org/show_bug.cgi?id=50322
195194
# https://bugs.llvm.org/show_bug.cgi?id=41459
196-
depends on !CC_IS_CLANG
195+
depends on !CC_IS_CLANG || CLANG_VERSION >= 120001
196+
# https://github.com/llvm/llvm-project/issues/53645
197+
depends on !CC_IS_CLANG || !X86_32
197198
help
198199
Detect overflows of buffers in common string and memory functions
199200
where the compiler can determine and validate the buffer sizes.

0 commit comments

Comments
 (0)