Skip to content

Commit 36be5cb

Browse files
melverakpm00
authored andcommitted
kasan: treat meminstrinsic as builtins in uninstrumented files
Where the compiler instruments meminstrinsics by generating calls to __asan/__hwasan_ prefixed functions, let the compiler consider memintrinsics as builtin again. To do so, never override memset/memmove/memcpy if the compiler does the correct instrumentation - even on !GENERIC_ENTRY architectures. [[email protected]: powerpc: don't rename memintrinsics if compiler adds prefixes] Link: https://lore.kernel.org/all/[email protected]/ [1] Link: https://lkml.kernel.org/r/[email protected] Link: https://lkml.kernel.org/r/[email protected] Fixes: 69d4c0d ("entry, kasan, x86: Disallow overriding mem*() functions") Signed-off-by: Marco Elver <[email protected]> Reviewed-by: Andrey Konovalov <[email protected]> Tested-by: Linux Kernel Functional Testing <[email protected]> Tested-by: Naresh Kamboju <[email protected]> Acked-by: Michael Ellerman <[email protected]> (powerpc) 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: Kees Cook <[email protected]> Cc: Nathan Chancellor <[email protected]> Cc: Nick Desaulniers <[email protected]> Cc: Nicolas Schier <[email protected]> Cc: Peter Zijlstra (Intel) <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Vincenzo Frascino <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 51287dc commit 36be5cb

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

lib/Kconfig.kasan

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ menuconfig KASAN
4949

5050
if KASAN
5151

52+
config CC_HAS_KASAN_MEMINTRINSIC_PREFIX
53+
def_bool (CC_IS_CLANG && $(cc-option,-fsanitize=kernel-address -mllvm -asan-kernel-mem-intrinsic-prefix=1)) || \
54+
(CC_IS_GCC && $(cc-option,-fsanitize=kernel-address --param asan-kernel-mem-intrinsic-prefix=1))
55+
# Don't define it if we don't need it: compilation of the test uses
56+
# this variable to decide how the compiler should treat builtins.
57+
depends on !KASAN_HW_TAGS
58+
help
59+
The compiler is able to prefix memintrinsics with __asan or __hwasan.
60+
5261
choice
5362
prompt "KASAN mode"
5463
default KASAN_GENERIC

mm/kasan/shadow.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,14 @@ bool __kasan_check_write(const volatile void *p, unsigned int size)
3838
}
3939
EXPORT_SYMBOL(__kasan_check_write);
4040

41-
#ifndef CONFIG_GENERIC_ENTRY
41+
#if !defined(CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX) && !defined(CONFIG_GENERIC_ENTRY)
4242
/*
4343
* CONFIG_GENERIC_ENTRY relies on compiler emitted mem*() calls to not be
4444
* instrumented. KASAN enabled toolchains should emit __asan_mem*() functions
4545
* for the sites they want to instrument.
46+
*
47+
* If we have a compiler that can instrument meminstrinsics, never override
48+
* these, so that non-instrumented files can safely consider them as builtins.
4649
*/
4750
#undef memset
4851
void *memset(void *addr, int c, size_t len)

scripts/Makefile.kasan

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# SPDX-License-Identifier: GPL-2.0
2+
3+
ifdef CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX
4+
# Safe for compiler to generate meminstrinsic calls in uninstrumented files.
5+
CFLAGS_KASAN_NOSANITIZE :=
6+
else
7+
# Don't let compiler generate memintrinsic calls in uninstrumented files
8+
# because they are instrumented.
29
CFLAGS_KASAN_NOSANITIZE := -fno-builtin
10+
endif
11+
312
KASAN_SHADOW_OFFSET ?= $(CONFIG_KASAN_SHADOW_OFFSET)
413

514
cc-param = $(call cc-option, -mllvm -$(1), $(call cc-option, --param $(1)))

0 commit comments

Comments
 (0)