Skip to content

Commit 5ef19d0

Browse files
keescat658011
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 a52f8a59aef4 ("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] Change-Id: I4630f1c43d84839a90d91b9ba24d5bb9ad2edc0a Signed-off-by: Alexander Winkowski <[email protected]> Signed-off-by: onettboots <[email protected]>
1 parent 94a77b3 commit 5ef19d0

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

include/linux/fortify-string.h

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#ifndef _LINUX_FORTIFY_STRING_H_
33
#define _LINUX_FORTIFY_STRING_H_
44

5-
#define __FORTIFY_INLINE extern __always_inline __gnu_inline
5+
#define __FORTIFY_INLINE extern __always_inline __gnu_inline __overloadable
66
#define __RENAME(x) __asm__(#x)
77

88
void fortify_panic(const char *name) __noreturn __cold;
@@ -50,8 +50,17 @@ extern char *__underlying_strncpy(char *p, const char *q, __kernel_size_t size)
5050
#define __underlying_strncpy __builtin_strncpy
5151
#endif
5252

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

@@ -63,7 +72,7 @@ char *strncpy(char * const p, const char *q, __kernel_size_t size)
6372
}
6473

6574
__FORTIFY_INLINE __diagnose_as(__builtin_strcat, 1, 2)
66-
char *strcat(char * const p, const char *q)
75+
char *strcat(char * const POS p, const char *q)
6776
{
6877
size_t p_size = __builtin_object_size(p, 1);
6978

@@ -75,7 +84,7 @@ char *strcat(char * const p, const char *q)
7584
}
7685

7786
extern __kernel_size_t __real_strnlen(const char *, __kernel_size_t) __RENAME(strnlen);
78-
__FORTIFY_INLINE __kernel_size_t strnlen(const char * const p, __kernel_size_t maxlen)
87+
__FORTIFY_INLINE __kernel_size_t strnlen(const char * const POS p, __kernel_size_t maxlen)
7988
{
8089
size_t p_size = __builtin_object_size(p, 1);
8190
size_t p_len = __compiletime_strlen(p);
@@ -112,7 +121,7 @@ __FORTIFY_INLINE __kernel_size_t strnlen(const char * const p, __kernel_size_t m
112121
__builtin_choose_expr(__is_constexpr(__builtin_strlen(p)), \
113122
__builtin_strlen(p), __fortify_strlen(p))
114123
__FORTIFY_INLINE __diagnose_as(__builtin_strlen, 1)
115-
__kernel_size_t __fortify_strlen(const char * const p)
124+
__kernel_size_t __fortify_strlen(const char * const POS p)
116125
{
117126
__kernel_size_t ret;
118127
size_t p_size = __builtin_object_size(p, 1);
@@ -128,7 +137,7 @@ __kernel_size_t __fortify_strlen(const char * const p)
128137

129138
/* defined after fortified strlen to reuse it */
130139
extern size_t __real_strlcpy(char *, const char *, size_t) __RENAME(strlcpy);
131-
__FORTIFY_INLINE size_t strlcpy(char * const p, const char * const q, size_t size)
140+
__FORTIFY_INLINE size_t strlcpy(char * const POS p, const char * const POS q, size_t size)
132141
{
133142
size_t p_size = __builtin_object_size(p, 1);
134143
size_t q_size = __builtin_object_size(q, 1);
@@ -155,7 +164,7 @@ __FORTIFY_INLINE size_t strlcpy(char * const p, const char * const q, size_t siz
155164

156165
/* defined after fortified strnlen to reuse it */
157166
extern ssize_t __real_strscpy(char *, const char *, size_t) __RENAME(strscpy);
158-
__FORTIFY_INLINE ssize_t strscpy(char * const p, const char * const q, size_t size)
167+
__FORTIFY_INLINE ssize_t strscpy(char * const POS p, const char * const POS q, size_t size)
159168
{
160169
size_t len;
161170
/* Use string size rather than possible enclosing struct size. */
@@ -202,7 +211,7 @@ __FORTIFY_INLINE ssize_t strscpy(char * const p, const char * const q, size_t si
202211

203212
/* defined after fortified strlen and strnlen to reuse them */
204213
__FORTIFY_INLINE __diagnose_as(__builtin_strncat, 1, 2, 3)
205-
char *strncat(char * const p, const char * const q, __kernel_size_t count)
214+
char *strncat(char * const POS p, const char * const POS q, __kernel_size_t count)
206215
{
207216
size_t p_len, copy_len;
208217
size_t p_size = __builtin_object_size(p, 1);
@@ -373,7 +382,7 @@ __FORTIFY_INLINE void fortify_memcpy_chk(__kernel_size_t size,
373382
memmove)
374383

375384
extern void *__real_memscan(void *, int, __kernel_size_t) __RENAME(memscan);
376-
__FORTIFY_INLINE void *memscan(void * const p, int c, __kernel_size_t size)
385+
__FORTIFY_INLINE void *memscan(void * const POS0 p, int c, __kernel_size_t size)
377386
{
378387
size_t p_size = __builtin_object_size(p, 0);
379388

@@ -385,7 +394,7 @@ __FORTIFY_INLINE void *memscan(void * const p, int c, __kernel_size_t size)
385394
}
386395

387396
__FORTIFY_INLINE __diagnose_as(__builtin_memcmp, 1, 2, 3)
388-
int memcmp(const void * const p, const void * const q, __kernel_size_t size)
397+
int memcmp(const void * const POS0 p, const void * const POS0 q, __kernel_size_t size)
389398
{
390399
size_t p_size = __builtin_object_size(p, 0);
391400
size_t q_size = __builtin_object_size(q, 0);
@@ -402,7 +411,7 @@ int memcmp(const void * const p, const void * const q, __kernel_size_t size)
402411
}
403412

404413
__FORTIFY_INLINE __diagnose_as(__builtin_memchr, 1, 2, 3)
405-
void *memchr(const void * const p, int c, __kernel_size_t size)
414+
void *memchr(const void * const POS0 p, int c, __kernel_size_t size)
406415
{
407416
size_t p_size = __builtin_object_size(p, 0);
408417

@@ -414,7 +423,7 @@ void *memchr(const void * const p, int c, __kernel_size_t size)
414423
}
415424

416425
void *__real_memchr_inv(const void *s, int c, size_t n) __RENAME(memchr_inv);
417-
__FORTIFY_INLINE void *memchr_inv(const void * const p, int c, size_t size)
426+
__FORTIFY_INLINE void *memchr_inv(const void * const POS0 p, int c, size_t size)
418427
{
419428
size_t p_size = __builtin_object_size(p, 0);
420429

@@ -426,7 +435,7 @@ __FORTIFY_INLINE void *memchr_inv(const void * const p, int c, size_t size)
426435
}
427436

428437
extern void *__real_kmemdup(const void *src, size_t len, gfp_t gfp) __RENAME(kmemdup);
429-
__FORTIFY_INLINE void *kmemdup(const void * const p, size_t size, gfp_t gfp)
438+
__FORTIFY_INLINE void *kmemdup(const void * const POS0 p, size_t size, gfp_t gfp)
430439
{
431440
size_t p_size = __builtin_object_size(p, 0);
432441

@@ -439,7 +448,7 @@ __FORTIFY_INLINE void *kmemdup(const void * const p, size_t size, gfp_t gfp)
439448

440449
/* Defined after fortified strlen to reuse it. */
441450
__FORTIFY_INLINE __diagnose_as(__builtin_strcpy, 1, 2)
442-
char *strcpy(char * const p, const char * const q)
451+
char *strcpy(char * const POS p, const char * const POS q)
443452
{
444453
size_t p_size = __builtin_object_size(p, 1);
445454
size_t q_size = __builtin_object_size(q, 1);
@@ -468,4 +477,7 @@ char *strcpy(char * const p, const char * const q)
468477
#undef __underlying_strncat
469478
#undef __underlying_strncpy
470479

480+
#undef POS
481+
#undef POS0
482+
471483
#endif /* _LINUX_FORTIFY_STRING_H_ */

0 commit comments

Comments
 (0)