Skip to content

Commit 2e57773

Browse files
xairyakpm00
authored andcommitted
kasan, fortify: properly rename memintrinsics
After commit 69d4c0d ("entry, kasan, x86: Disallow overriding mem*() functions") and the follow-up fixes, with CONFIG_FORTIFY_SOURCE enabled, even though the compiler instruments meminstrinsics by generating calls to __asan/__hwasan_ prefixed functions, FORTIFY_SOURCE still uses uninstrumented memset/memmove/memcpy as the underlying functions. As a result, KASAN cannot detect bad accesses in memset/memmove/memcpy. This also makes KASAN tests corrupt kernel memory and cause crashes. To fix this, use __asan_/__hwasan_memset/memmove/memcpy as the underlying functions whenever appropriate. Do this only for the instrumented code (as indicated by __SANITIZE_ADDRESS__). Link: https://lkml.kernel.org/r/[email protected] Fixes: 69d4c0d ("entry, kasan, x86: Disallow overriding mem*() functions") Fixes: 51287dc ("kasan: emit different calls for instrumentable memintrinsics") Fixes: 36be5cb ("kasan: treat meminstrinsic as builtins in uninstrumented files") Signed-off-by: Andrey Konovalov <[email protected]> Reported-by: Erhard Furtner <[email protected]> Reported-by: Nico Pache <[email protected]> Closes: https://lore.kernel.org/all/[email protected]/ Reviewed-by: Marco Elver <[email protected]> Tested-by: Nico Pache <[email protected]> Acked-by: Nico Pache <[email protected]> Cc: Alexander Potapenko <[email protected]> Cc: Andrey Ryabinin <[email protected]> Cc: Daniel Axtens <[email protected]> Cc: Dmitry Vyukov <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent a38568a commit 2e57773

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

include/linux/fortify-string.h

+18-4
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,30 @@ void __write_overflow_field(size_t avail, size_t wanted) __compiletime_warning("
7575
__ret; \
7676
})
7777

78-
#if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
78+
#if defined(__SANITIZE_ADDRESS__)
79+
80+
#if !defined(CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX) && !defined(CONFIG_GENERIC_ENTRY)
81+
extern void *__underlying_memset(void *p, int c, __kernel_size_t size) __RENAME(memset);
82+
extern void *__underlying_memmove(void *p, const void *q, __kernel_size_t size) __RENAME(memmove);
83+
extern void *__underlying_memcpy(void *p, const void *q, __kernel_size_t size) __RENAME(memcpy);
84+
#elif defined(CONFIG_KASAN_GENERIC)
85+
extern void *__underlying_memset(void *p, int c, __kernel_size_t size) __RENAME(__asan_memset);
86+
extern void *__underlying_memmove(void *p, const void *q, __kernel_size_t size) __RENAME(__asan_memmove);
87+
extern void *__underlying_memcpy(void *p, const void *q, __kernel_size_t size) __RENAME(__asan_memcpy);
88+
#else /* CONFIG_KASAN_SW_TAGS */
89+
extern void *__underlying_memset(void *p, int c, __kernel_size_t size) __RENAME(__hwasan_memset);
90+
extern void *__underlying_memmove(void *p, const void *q, __kernel_size_t size) __RENAME(__hwasan_memmove);
91+
extern void *__underlying_memcpy(void *p, const void *q, __kernel_size_t size) __RENAME(__hwasan_memcpy);
92+
#endif
93+
7994
extern void *__underlying_memchr(const void *p, int c, __kernel_size_t size) __RENAME(memchr);
8095
extern int __underlying_memcmp(const void *p, const void *q, __kernel_size_t size) __RENAME(memcmp);
81-
extern void *__underlying_memcpy(void *p, const void *q, __kernel_size_t size) __RENAME(memcpy);
82-
extern void *__underlying_memmove(void *p, const void *q, __kernel_size_t size) __RENAME(memmove);
83-
extern void *__underlying_memset(void *p, int c, __kernel_size_t size) __RENAME(memset);
8496
extern char *__underlying_strcat(char *p, const char *q) __RENAME(strcat);
8597
extern char *__underlying_strcpy(char *p, const char *q) __RENAME(strcpy);
8698
extern __kernel_size_t __underlying_strlen(const char *p) __RENAME(strlen);
8799
extern char *__underlying_strncat(char *p, const char *q, __kernel_size_t count) __RENAME(strncat);
88100
extern char *__underlying_strncpy(char *p, const char *q, __kernel_size_t size) __RENAME(strncpy);
101+
89102
#else
90103

91104
#if defined(__SANITIZE_MEMORY__)
@@ -110,6 +123,7 @@ extern char *__underlying_strncpy(char *p, const char *q, __kernel_size_t size)
110123
#define __underlying_strlen __builtin_strlen
111124
#define __underlying_strncat __builtin_strncat
112125
#define __underlying_strncpy __builtin_strncpy
126+
113127
#endif
114128

115129
/**

0 commit comments

Comments
 (0)