Skip to content

Commit 51287dc

Browse files
melverakpm00
authored andcommitted
kasan: emit different calls for instrumentable memintrinsics
Clang 15 provides an option to prefix memcpy/memset/memmove calls with __asan_/__hwasan_ in instrumented functions: https://reviews.llvm.org/D122724 GCC will add support in future: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108777 Use it to regain KASAN instrumentation of memcpy/memset/memmove on architectures that require noinstr to be really free from instrumented mem*() functions (all GENERIC_ENTRY architectures). Link: https://lkml.kernel.org/r/[email protected] Fixes: 69d4c0d ("entry, kasan, x86: Disallow overriding mem*() functions") Signed-off-by: Marco Elver <[email protected]> Acked-by: Peter Zijlstra (Intel) <[email protected]> Reviewed-by: Andrey Konovalov <[email protected]> Tested-by: Linux Kernel Functional Testing <[email protected]> Tested-by: Naresh Kamboju <[email protected]> Cc: Alexander Potapenko <[email protected]> Cc: Andrey Ryabinin <[email protected]> Cc: Borislav Petkov (AMD) <[email protected]> Cc: Dave Hansen <[email protected]> Cc: Dmitry Vyukov <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Jakub Jelinek <[email protected]> Cc: [email protected] Cc: Kees Cook <[email protected]> Cc: Linux Kernel Functional Testing <[email protected]> Cc: Nathan Chancellor <[email protected]> # build only Cc: Nick Desaulniers <[email protected]> Cc: Nicolas Schier <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Vincenzo Frascino <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 236b925 commit 51287dc

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

mm/kasan/kasan.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,4 +666,8 @@ void __hwasan_storeN_noabort(unsigned long addr, size_t size);
666666

667667
void __hwasan_tag_memory(unsigned long addr, u8 tag, unsigned long size);
668668

669+
void *__hwasan_memset(void *addr, int c, size_t len);
670+
void *__hwasan_memmove(void *dest, const void *src, size_t len);
671+
void *__hwasan_memcpy(void *dest, const void *src, size_t len);
672+
669673
#endif /* __MM_KASAN_KASAN_H */

mm/kasan/shadow.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,17 @@ void *__asan_memcpy(void *dest, const void *src, size_t len)
107107
}
108108
EXPORT_SYMBOL(__asan_memcpy);
109109

110+
#ifdef CONFIG_KASAN_SW_TAGS
111+
void *__hwasan_memset(void *addr, int c, size_t len) __alias(__asan_memset);
112+
EXPORT_SYMBOL(__hwasan_memset);
113+
#ifdef __HAVE_ARCH_MEMMOVE
114+
void *__hwasan_memmove(void *dest, const void *src, size_t len) __alias(__asan_memmove);
115+
EXPORT_SYMBOL(__hwasan_memmove);
116+
#endif
117+
void *__hwasan_memcpy(void *dest, const void *src, size_t len) __alias(__asan_memcpy);
118+
EXPORT_SYMBOL(__hwasan_memcpy);
119+
#endif
120+
110121
void kasan_poison(const void *addr, size_t size, u8 value, bool init)
111122
{
112123
void *shadow_start, *shadow_end;

scripts/Makefile.kasan

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ endif
3838

3939
CFLAGS_KASAN += $(call cc-param,asan-stack=$(stack_enable))
4040

41+
# Instrument memcpy/memset/memmove calls by using instrumented __asan_mem*()
42+
# instead. With compilers that don't support this option, compiler-inserted
43+
# memintrinsics won't be checked by KASAN on GENERIC_ENTRY architectures.
44+
CFLAGS_KASAN += $(call cc-param,asan-kernel-mem-intrinsic-prefix=1)
45+
4146
endif # CONFIG_KASAN_GENERIC
4247

4348
ifdef CONFIG_KASAN_SW_TAGS
@@ -54,6 +59,9 @@ CFLAGS_KASAN := -fsanitize=kernel-hwaddress \
5459
$(call cc-param,hwasan-inline-all-checks=0) \
5560
$(instrumentation_flags)
5661

62+
# Instrument memcpy/memset/memmove calls by using instrumented __hwasan_mem*().
63+
CFLAGS_KASAN += $(call cc-param,hwasan-kernel-mem-intrinsic-prefix=1)
64+
5765
endif # CONFIG_KASAN_SW_TAGS
5866

5967
export CFLAGS_KASAN CFLAGS_KASAN_NOSANITIZE

0 commit comments

Comments
 (0)