Skip to content

Commit 92df138

Browse files
committed
fortify: Use __diagnose_as() for better diagnostic coverage
In preparation for using Clang's __pass_object_size, add __diagnose_as() attributes to mark the functions as being the same as the indicated builtins. When __daignose_as() is available, Clang will have a more complete ability to apply its own diagnostic analysis to callers of these functions, as if they were the builtins themselves. Without __diagnose_as, Clang's compile time diagnostic messages won't be as precise as they could be, but at least users of older toolchains will still benefit from having fortified routines. Signed-off-by: Kees Cook <[email protected]> Reviewed-by: Nick Desaulniers <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 0a2b782 commit 92df138

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

include/linux/fortify-string.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ extern char *__underlying_strncpy(char *p, const char *q, __kernel_size_t size)
5050
#define __underlying_strncpy __builtin_strncpy
5151
#endif
5252

53-
__FORTIFY_INLINE char *strncpy(char * const p, const char *q, __kernel_size_t size)
53+
__FORTIFY_INLINE __diagnose_as(__builtin_strncpy, 1, 2, 3)
54+
char *strncpy(char * const p, const char *q, __kernel_size_t size)
5455
{
5556
size_t p_size = __builtin_object_size(p, 1);
5657

@@ -61,7 +62,8 @@ __FORTIFY_INLINE char *strncpy(char * const p, const char *q, __kernel_size_t si
6162
return __underlying_strncpy(p, q, size);
6263
}
6364

64-
__FORTIFY_INLINE char *strcat(char * const p, const char *q)
65+
__FORTIFY_INLINE __diagnose_as(__builtin_strcat, 1, 2)
66+
char *strcat(char * const p, const char *q)
6567
{
6668
size_t p_size = __builtin_object_size(p, 1);
6769

@@ -94,7 +96,8 @@ __FORTIFY_INLINE __kernel_size_t strnlen(const char * const p, __kernel_size_t m
9496
}
9597

9698
/* defined after fortified strnlen to reuse it. */
97-
__FORTIFY_INLINE __kernel_size_t strlen(const char * const p)
99+
__FORTIFY_INLINE __diagnose_as(__builtin_strlen, 1)
100+
__kernel_size_t strlen(const char * const p)
98101
{
99102
__kernel_size_t ret;
100103
size_t p_size = __builtin_object_size(p, 1);
@@ -183,7 +186,8 @@ __FORTIFY_INLINE ssize_t strscpy(char * const p, const char * const q, size_t si
183186
}
184187

185188
/* defined after fortified strlen and strnlen to reuse them */
186-
__FORTIFY_INLINE char *strncat(char * const p, const char * const q, __kernel_size_t count)
189+
__FORTIFY_INLINE __diagnose_as(__builtin_strncat, 1, 2, 3)
190+
char *strncat(char * const p, const char * const q, __kernel_size_t count)
187191
{
188192
size_t p_len, copy_len;
189193
size_t p_size = __builtin_object_size(p, 1);
@@ -365,7 +369,8 @@ __FORTIFY_INLINE void *memscan(void * const p, int c, __kernel_size_t size)
365369
return __real_memscan(p, c, size);
366370
}
367371

368-
__FORTIFY_INLINE int memcmp(const void * const p, const void * const q, __kernel_size_t size)
372+
__FORTIFY_INLINE __diagnose_as(__builtin_memcmp, 1, 2, 3)
373+
int memcmp(const void * const p, const void * const q, __kernel_size_t size)
369374
{
370375
size_t p_size = __builtin_object_size(p, 0);
371376
size_t q_size = __builtin_object_size(q, 0);
@@ -381,7 +386,8 @@ __FORTIFY_INLINE int memcmp(const void * const p, const void * const q, __kernel
381386
return __underlying_memcmp(p, q, size);
382387
}
383388

384-
__FORTIFY_INLINE void *memchr(const void * const p, int c, __kernel_size_t size)
389+
__FORTIFY_INLINE __diagnose_as(__builtin_memchr, 1, 2, 3)
390+
void *memchr(const void * const p, int c, __kernel_size_t size)
385391
{
386392
size_t p_size = __builtin_object_size(p, 0);
387393

@@ -417,7 +423,8 @@ __FORTIFY_INLINE void *kmemdup(const void * const p, size_t size, gfp_t gfp)
417423
}
418424

419425
/* Defined after fortified strlen to reuse it. */
420-
__FORTIFY_INLINE char *strcpy(char * const p, const char * const q)
426+
__FORTIFY_INLINE __diagnose_as(__builtin_strcpy, 1, 2)
427+
char *strcpy(char * const p, const char * const q)
421428
{
422429
size_t p_size = __builtin_object_size(p, 1);
423430
size_t q_size = __builtin_object_size(q, 1);

0 commit comments

Comments
 (0)