summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrey Konovalov <andreyknvl@google.com>2018-12-28 03:29:53 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2018-12-28 15:11:43 -0500
commit2bd926b439b4cb6b9ed240a9781cd01958b53d85 (patch)
treebfdd9f2a3f0111b3a3510032aabdc6f90ce73ed4
parentb938fcf42739de8270e6ea41593722929c8a7dd0 (diff)
kasan: add CONFIG_KASAN_GENERIC and CONFIG_KASAN_SW_TAGS
This commit splits the current CONFIG_KASAN config option into two: 1. CONFIG_KASAN_GENERIC, that enables the generic KASAN mode (the one that exists now); 2. CONFIG_KASAN_SW_TAGS, that enables the software tag-based KASAN mode. The name CONFIG_KASAN_SW_TAGS is chosen as in the future we will have another hardware tag-based KASAN mode, that will rely on hardware memory tagging support in arm64. With CONFIG_KASAN_SW_TAGS enabled, compiler options are changed to instrument kernel files with -fsantize=kernel-hwaddress (except the ones for which KASAN_SANITIZE := n is set). Both CONFIG_KASAN_GENERIC and CONFIG_KASAN_SW_TAGS support both CONFIG_KASAN_INLINE and CONFIG_KASAN_OUTLINE instrumentation modes. This commit also adds empty placeholder (for now) implementation of tag-based KASAN specific hooks inserted by the compiler and adjusts common hooks implementation. While this commit adds the CONFIG_KASAN_SW_TAGS config option, this option is not selectable, as it depends on HAVE_ARCH_KASAN_SW_TAGS, which we will enable once all the infrastracture code has been added. Link: http://lkml.kernel.org/r/b2550106eb8a68b10fefbabce820910b115aa853.1544099024.git.andreyknvl@google.com Signed-off-by: Andrey Konovalov <andreyknvl@google.com> Reviewed-by: Andrey Ryabinin <aryabinin@virtuozzo.com> Reviewed-by: Dmitry Vyukov <dvyukov@google.com> Cc: Christoph Lameter <cl@linux.com> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Will Deacon <will.deacon@arm.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rw-r--r--include/linux/compiler-clang.h6
-rw-r--r--include/linux/compiler-gcc.h6
-rw-r--r--include/linux/compiler_attributes.h13
-rw-r--r--include/linux/kasan.h16
-rw-r--r--lib/Kconfig.kasan98
-rw-r--r--mm/kasan/Makefile6
-rw-r--r--mm/kasan/generic.c2
-rw-r--r--mm/kasan/kasan.h3
-rw-r--r--mm/kasan/tags.c75
-rw-r--r--mm/slub.c2
-rw-r--r--scripts/Makefile.kasan53
11 files changed, 214 insertions, 66 deletions
diff --git a/include/linux/compiler-clang.h b/include/linux/compiler-clang.h
index 3e7dafb3ea80..39f668d5066b 100644
--- a/include/linux/compiler-clang.h
+++ b/include/linux/compiler-clang.h
@@ -16,9 +16,13 @@
16/* all clang versions usable with the kernel support KASAN ABI version 5 */ 16/* all clang versions usable with the kernel support KASAN ABI version 5 */
17#define KASAN_ABI_VERSION 5 17#define KASAN_ABI_VERSION 5
18 18
19#if __has_feature(address_sanitizer) || __has_feature(hwaddress_sanitizer)
19/* emulate gcc's __SANITIZE_ADDRESS__ flag */ 20/* emulate gcc's __SANITIZE_ADDRESS__ flag */
20#if __has_feature(address_sanitizer)
21#define __SANITIZE_ADDRESS__ 21#define __SANITIZE_ADDRESS__
22#define __no_sanitize_address \
23 __attribute__((no_sanitize("address", "hwaddress")))
24#else
25#define __no_sanitize_address
22#endif 26#endif
23 27
24/* 28/*
diff --git a/include/linux/compiler-gcc.h b/include/linux/compiler-gcc.h
index 2010493e1040..5776da43da97 100644
--- a/include/linux/compiler-gcc.h
+++ b/include/linux/compiler-gcc.h
@@ -143,6 +143,12 @@
143#define KASAN_ABI_VERSION 3 143#define KASAN_ABI_VERSION 3
144#endif 144#endif
145 145
146#if __has_attribute(__no_sanitize_address__)
147#define __no_sanitize_address __attribute__((no_sanitize_address))
148#else
149#define __no_sanitize_address
150#endif
151
146#if GCC_VERSION >= 50100 152#if GCC_VERSION >= 50100
147#define COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW 1 153#define COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW 1
148#endif 154#endif
diff --git a/include/linux/compiler_attributes.h b/include/linux/compiler_attributes.h
index fe07b680dd4a..19f32b0c29af 100644
--- a/include/linux/compiler_attributes.h
+++ b/include/linux/compiler_attributes.h
@@ -200,19 +200,6 @@
200#define __noreturn __attribute__((__noreturn__)) 200#define __noreturn __attribute__((__noreturn__))
201 201
202/* 202/*
203 * Optional: only supported since gcc >= 4.8
204 * Optional: not supported by icc
205 *
206 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-no_005fsanitize_005faddress-function-attribute
207 * clang: https://clang.llvm.org/docs/AttributeReference.html#no-sanitize-address-no-address-safety-analysis
208 */
209#if __has_attribute(__no_sanitize_address__)
210# define __no_sanitize_address __attribute__((__no_sanitize_address__))
211#else
212# define __no_sanitize_address
213#endif
214
215/*
216 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-packed-type-attribute 203 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-packed-type-attribute
217 * clang: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-packed-variable-attribute 204 * clang: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-packed-variable-attribute
218 */ 205 */
diff --git a/include/linux/kasan.h b/include/linux/kasan.h
index 52c86a568a4e..b66fdf5ea7ab 100644
--- a/include/linux/kasan.h
+++ b/include/linux/kasan.h
@@ -45,8 +45,6 @@ void kasan_free_pages(struct page *page, unsigned int order);
45 45
46void kasan_cache_create(struct kmem_cache *cache, unsigned int *size, 46void kasan_cache_create(struct kmem_cache *cache, unsigned int *size,
47 slab_flags_t *flags); 47 slab_flags_t *flags);
48void kasan_cache_shrink(struct kmem_cache *cache);
49void kasan_cache_shutdown(struct kmem_cache *cache);
50 48
51void kasan_poison_slab(struct page *page); 49void kasan_poison_slab(struct page *page);
52void kasan_unpoison_object_data(struct kmem_cache *cache, void *object); 50void kasan_unpoison_object_data(struct kmem_cache *cache, void *object);
@@ -97,8 +95,6 @@ static inline void kasan_free_pages(struct page *page, unsigned int order) {}
97static inline void kasan_cache_create(struct kmem_cache *cache, 95static inline void kasan_cache_create(struct kmem_cache *cache,
98 unsigned int *size, 96 unsigned int *size,
99 slab_flags_t *flags) {} 97 slab_flags_t *flags) {}
100static inline void kasan_cache_shrink(struct kmem_cache *cache) {}
101static inline void kasan_cache_shutdown(struct kmem_cache *cache) {}
102 98
103static inline void kasan_poison_slab(struct page *page) {} 99static inline void kasan_poison_slab(struct page *page) {}
104static inline void kasan_unpoison_object_data(struct kmem_cache *cache, 100static inline void kasan_unpoison_object_data(struct kmem_cache *cache,
@@ -155,4 +151,16 @@ static inline size_t kasan_metadata_size(struct kmem_cache *cache) { return 0; }
155 151
156#endif /* CONFIG_KASAN */ 152#endif /* CONFIG_KASAN */
157 153
154#ifdef CONFIG_KASAN_GENERIC
155
156void kasan_cache_shrink(struct kmem_cache *cache);
157void kasan_cache_shutdown(struct kmem_cache *cache);
158
159#else /* CONFIG_KASAN_GENERIC */
160
161static inline void kasan_cache_shrink(struct kmem_cache *cache) {}
162static inline void kasan_cache_shutdown(struct kmem_cache *cache) {}
163
164#endif /* CONFIG_KASAN_GENERIC */
165
158#endif /* LINUX_KASAN_H */ 166#endif /* LINUX_KASAN_H */
diff --git a/lib/Kconfig.kasan b/lib/Kconfig.kasan
index d0bad1bd9a2b..d8c474b6691e 100644
--- a/lib/Kconfig.kasan
+++ b/lib/Kconfig.kasan
@@ -1,36 +1,92 @@
1# This config refers to the generic KASAN mode.
1config HAVE_ARCH_KASAN 2config HAVE_ARCH_KASAN
2 bool 3 bool
3 4
4if HAVE_ARCH_KASAN 5config HAVE_ARCH_KASAN_SW_TAGS
6 bool
7
8config CC_HAS_KASAN_GENERIC
9 def_bool $(cc-option, -fsanitize=kernel-address)
10
11config CC_HAS_KASAN_SW_TAGS
12 def_bool $(cc-option, -fsanitize=kernel-hwaddress)
5 13
6config KASAN 14config KASAN
7 bool "KASan: runtime memory debugger" 15 bool "KASAN: runtime memory debugger"
16 depends on (HAVE_ARCH_KASAN && CC_HAS_KASAN_GENERIC) || \
17 (HAVE_ARCH_KASAN_SW_TAGS && CC_HAS_KASAN_SW_TAGS)
18 depends on (SLUB && SYSFS) || (SLAB && !DEBUG_SLAB)
19 help
20 Enables KASAN (KernelAddressSANitizer) - runtime memory debugger,
21 designed to find out-of-bounds accesses and use-after-free bugs.
22 See Documentation/dev-tools/kasan.rst for details.
23
24choice
25 prompt "KASAN mode"
26 depends on KASAN
27 default KASAN_GENERIC
28 help
29 KASAN has two modes: generic KASAN (similar to userspace ASan,
30 x86_64/arm64/xtensa, enabled with CONFIG_KASAN_GENERIC) and
31 software tag-based KASAN (a version based on software memory
32 tagging, arm64 only, similar to userspace HWASan, enabled with
33 CONFIG_KASAN_SW_TAGS).
34 Both generic and tag-based KASAN are strictly debugging features.
35
36config KASAN_GENERIC
37 bool "Generic mode"
38 depends on HAVE_ARCH_KASAN && CC_HAS_KASAN_GENERIC
8 depends on (SLUB && SYSFS) || (SLAB && !DEBUG_SLAB) 39 depends on (SLUB && SYSFS) || (SLAB && !DEBUG_SLAB)
9 select SLUB_DEBUG if SLUB 40 select SLUB_DEBUG if SLUB
10 select CONSTRUCTORS 41 select CONSTRUCTORS
11 select STACKDEPOT 42 select STACKDEPOT
12 help 43 help
13 Enables kernel address sanitizer - runtime memory debugger, 44 Enables generic KASAN mode.
14 designed to find out-of-bounds accesses and use-after-free bugs. 45 Supported in both GCC and Clang. With GCC it requires version 4.9.2
15 This is strictly a debugging feature and it requires a gcc version 46 or later for basic support and version 5.0 or later for detection of
16 of 4.9.2 or later. Detection of out of bounds accesses to stack or 47 out-of-bounds accesses for stack and global variables and for inline
17 global variables requires gcc 5.0 or later. 48 instrumentation mode (CONFIG_KASAN_INLINE). With Clang it requires
18 This feature consumes about 1/8 of available memory and brings about 49 version 3.7.0 or later and it doesn't support detection of
19 ~x3 performance slowdown. 50 out-of-bounds accesses for global variables yet.
51 This mode consumes about 1/8th of available memory at kernel start
52 and introduces an overhead of ~x1.5 for the rest of the allocations.
53 The performance slowdown is ~x3.
20 For better error detection enable CONFIG_STACKTRACE. 54 For better error detection enable CONFIG_STACKTRACE.
21 Currently CONFIG_KASAN doesn't work with CONFIG_DEBUG_SLAB 55 Currently CONFIG_KASAN_GENERIC doesn't work with CONFIG_DEBUG_SLAB
22 (the resulting kernel does not boot). 56 (the resulting kernel does not boot).
23 57
58config KASAN_SW_TAGS
59 bool "Software tag-based mode"
60 depends on HAVE_ARCH_KASAN_SW_TAGS && CC_HAS_KASAN_SW_TAGS
61 depends on (SLUB && SYSFS) || (SLAB && !DEBUG_SLAB)
62 select SLUB_DEBUG if SLUB
63 select CONSTRUCTORS
64 select STACKDEPOT
65 help
66 Enables software tag-based KASAN mode.
67 This mode requires Top Byte Ignore support by the CPU and therefore
68 is only supported for arm64.
69 This mode requires Clang version 7.0.0 or later.
70 This mode consumes about 1/16th of available memory at kernel start
71 and introduces an overhead of ~20% for the rest of the allocations.
72 This mode may potentially introduce problems relating to pointer
73 casting and comparison, as it embeds tags into the top byte of each
74 pointer.
75 For better error detection enable CONFIG_STACKTRACE.
76 Currently CONFIG_KASAN_SW_TAGS doesn't work with CONFIG_DEBUG_SLAB
77 (the resulting kernel does not boot).
78
79endchoice
80
24config KASAN_EXTRA 81config KASAN_EXTRA
25 bool "KAsan: extra checks" 82 bool "KASAN: extra checks"
26 depends on KASAN && DEBUG_KERNEL && !COMPILE_TEST 83 depends on KASAN_GENERIC && DEBUG_KERNEL && !COMPILE_TEST
27 help 84 help
28 This enables further checks in the kernel address sanitizer, for now 85 This enables further checks in generic KASAN, for now it only
29 it only includes the address-use-after-scope check that can lead 86 includes the address-use-after-scope check that can lead to
30 to excessive kernel stack usage, frame size warnings and longer 87 excessive kernel stack usage, frame size warnings and longer
31 compile time. 88 compile time.
32 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81715 has more 89 See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81715
33
34 90
35choice 91choice
36 prompt "Instrumentation type" 92 prompt "Instrumentation type"
@@ -53,7 +109,7 @@ config KASAN_INLINE
53 memory accesses. This is faster than outline (in some workloads 109 memory accesses. This is faster than outline (in some workloads
54 it gives about x2 boost over outline instrumentation), but 110 it gives about x2 boost over outline instrumentation), but
55 make kernel's .text size much bigger. 111 make kernel's .text size much bigger.
56 This requires a gcc version of 5.0 or later. 112 For CONFIG_KASAN_GENERIC this requires GCC 5.0 or later.
57 113
58endchoice 114endchoice
59 115
@@ -67,11 +123,9 @@ config KASAN_S390_4_LEVEL_PAGING
67 4-level paging instead. 123 4-level paging instead.
68 124
69config TEST_KASAN 125config TEST_KASAN
70 tristate "Module for testing kasan for bug detection" 126 tristate "Module for testing KASAN for bug detection"
71 depends on m && KASAN 127 depends on m && KASAN
72 help 128 help
73 This is a test module doing various nasty things like 129 This is a test module doing various nasty things like
74 out of bounds accesses, use after free. It is useful for testing 130 out of bounds accesses, use after free. It is useful for testing
75 kernel debugging features like kernel address sanitizer. 131 kernel debugging features like KASAN.
76
77endif
diff --git a/mm/kasan/Makefile b/mm/kasan/Makefile
index d643530b24aa..68ba1822f003 100644
--- a/mm/kasan/Makefile
+++ b/mm/kasan/Makefile
@@ -2,6 +2,7 @@
2KASAN_SANITIZE := n 2KASAN_SANITIZE := n
3UBSAN_SANITIZE_common.o := n 3UBSAN_SANITIZE_common.o := n
4UBSAN_SANITIZE_generic.o := n 4UBSAN_SANITIZE_generic.o := n
5UBSAN_SANITIZE_tags.o := n
5KCOV_INSTRUMENT := n 6KCOV_INSTRUMENT := n
6 7
7CFLAGS_REMOVE_generic.o = -pg 8CFLAGS_REMOVE_generic.o = -pg
@@ -10,5 +11,8 @@ CFLAGS_REMOVE_generic.o = -pg
10 11
11CFLAGS_common.o := $(call cc-option, -fno-conserve-stack -fno-stack-protector) 12CFLAGS_common.o := $(call cc-option, -fno-conserve-stack -fno-stack-protector)
12CFLAGS_generic.o := $(call cc-option, -fno-conserve-stack -fno-stack-protector) 13CFLAGS_generic.o := $(call cc-option, -fno-conserve-stack -fno-stack-protector)
14CFLAGS_tags.o := $(call cc-option, -fno-conserve-stack -fno-stack-protector)
13 15
14obj-y := common.o generic.o report.o init.o quarantine.o 16obj-$(CONFIG_KASAN) := common.o init.o report.o
17obj-$(CONFIG_KASAN_GENERIC) += generic.o quarantine.o
18obj-$(CONFIG_KASAN_SW_TAGS) += tags.o
diff --git a/mm/kasan/generic.c b/mm/kasan/generic.c
index 44ec228de0a2..b8de6d33c55c 100644
--- a/mm/kasan/generic.c
+++ b/mm/kasan/generic.c
@@ -1,5 +1,5 @@
1/* 1/*
2 * This file contains core KASAN code. 2 * This file contains core generic KASAN code.
3 * 3 *
4 * Copyright (c) 2014 Samsung Electronics Co., Ltd. 4 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
5 * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com> 5 * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com>
diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h
index 659463800f10..19b950eaccff 100644
--- a/mm/kasan/kasan.h
+++ b/mm/kasan/kasan.h
@@ -114,7 +114,8 @@ void kasan_report(unsigned long addr, size_t size,
114 bool is_write, unsigned long ip); 114 bool is_write, unsigned long ip);
115void kasan_report_invalid_free(void *object, unsigned long ip); 115void kasan_report_invalid_free(void *object, unsigned long ip);
116 116
117#if defined(CONFIG_SLAB) || defined(CONFIG_SLUB) 117#if defined(CONFIG_KASAN_GENERIC) && \
118 (defined(CONFIG_SLAB) || defined(CONFIG_SLUB))
118void quarantine_put(struct kasan_free_meta *info, struct kmem_cache *cache); 119void quarantine_put(struct kasan_free_meta *info, struct kmem_cache *cache);
119void quarantine_reduce(void); 120void quarantine_reduce(void);
120void quarantine_remove_cache(struct kmem_cache *cache); 121void quarantine_remove_cache(struct kmem_cache *cache);
diff --git a/mm/kasan/tags.c b/mm/kasan/tags.c
new file mode 100644
index 000000000000..04194923c543
--- /dev/null
+++ b/mm/kasan/tags.c
@@ -0,0 +1,75 @@
1/*
2 * This file contains core tag-based KASAN code.
3 *
4 * Copyright (c) 2018 Google, Inc.
5 * Author: Andrey Konovalov <andreyknvl@google.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 */
12
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14#define DISABLE_BRANCH_PROFILING
15
16#include <linux/export.h>
17#include <linux/interrupt.h>
18#include <linux/init.h>
19#include <linux/kasan.h>
20#include <linux/kernel.h>
21#include <linux/kmemleak.h>
22#include <linux/linkage.h>
23#include <linux/memblock.h>
24#include <linux/memory.h>
25#include <linux/mm.h>
26#include <linux/module.h>
27#include <linux/printk.h>
28#include <linux/random.h>
29#include <linux/sched.h>
30#include <linux/sched/task_stack.h>
31#include <linux/slab.h>
32#include <linux/stacktrace.h>
33#include <linux/string.h>
34#include <linux/types.h>
35#include <linux/vmalloc.h>
36#include <linux/bug.h>
37
38#include "kasan.h"
39#include "../slab.h"
40
41void check_memory_region(unsigned long addr, size_t size, bool write,
42 unsigned long ret_ip)
43{
44}
45
46#define DEFINE_HWASAN_LOAD_STORE(size) \
47 void __hwasan_load##size##_noabort(unsigned long addr) \
48 { \
49 } \
50 EXPORT_SYMBOL(__hwasan_load##size##_noabort); \
51 void __hwasan_store##size##_noabort(unsigned long addr) \
52 { \
53 } \
54 EXPORT_SYMBOL(__hwasan_store##size##_noabort)
55
56DEFINE_HWASAN_LOAD_STORE(1);
57DEFINE_HWASAN_LOAD_STORE(2);
58DEFINE_HWASAN_LOAD_STORE(4);
59DEFINE_HWASAN_LOAD_STORE(8);
60DEFINE_HWASAN_LOAD_STORE(16);
61
62void __hwasan_loadN_noabort(unsigned long addr, unsigned long size)
63{
64}
65EXPORT_SYMBOL(__hwasan_loadN_noabort);
66
67void __hwasan_storeN_noabort(unsigned long addr, unsigned long size)
68{
69}
70EXPORT_SYMBOL(__hwasan_storeN_noabort);
71
72void __hwasan_tag_memory(unsigned long addr, u8 tag, unsigned long size)
73{
74}
75EXPORT_SYMBOL(__hwasan_tag_memory);
diff --git a/mm/slub.c b/mm/slub.c
index 8561a32910dd..e739d46600b9 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -2992,7 +2992,7 @@ static __always_inline void slab_free(struct kmem_cache *s, struct page *page,
2992 do_slab_free(s, page, head, tail, cnt, addr); 2992 do_slab_free(s, page, head, tail, cnt, addr);
2993} 2993}
2994 2994
2995#ifdef CONFIG_KASAN 2995#ifdef CONFIG_KASAN_GENERIC
2996void ___cache_free(struct kmem_cache *cache, void *x, unsigned long addr) 2996void ___cache_free(struct kmem_cache *cache, void *x, unsigned long addr)
2997{ 2997{
2998 do_slab_free(cache, virt_to_head_page(x), x, NULL, 1, addr); 2998 do_slab_free(cache, virt_to_head_page(x), x, NULL, 1, addr);
diff --git a/scripts/Makefile.kasan b/scripts/Makefile.kasan
index 69552a39951d..25c259df8ffa 100644
--- a/scripts/Makefile.kasan
+++ b/scripts/Makefile.kasan
@@ -1,5 +1,6 @@
1# SPDX-License-Identifier: GPL-2.0 1# SPDX-License-Identifier: GPL-2.0
2ifdef CONFIG_KASAN 2ifdef CONFIG_KASAN_GENERIC
3
3ifdef CONFIG_KASAN_INLINE 4ifdef CONFIG_KASAN_INLINE
4 call_threshold := 10000 5 call_threshold := 10000
5else 6else
@@ -12,36 +13,44 @@ CFLAGS_KASAN_MINIMAL := -fsanitize=kernel-address
12 13
13cc-param = $(call cc-option, -mllvm -$(1), $(call cc-option, --param $(1))) 14cc-param = $(call cc-option, -mllvm -$(1), $(call cc-option, --param $(1)))
14 15
15ifeq ($(call cc-option, $(CFLAGS_KASAN_MINIMAL) -Werror),) 16# -fasan-shadow-offset fails without -fsanitize
16 ifneq ($(CONFIG_COMPILE_TEST),y) 17CFLAGS_KASAN_SHADOW := $(call cc-option, -fsanitize=kernel-address \
17 $(warning Cannot use CONFIG_KASAN: \
18 -fsanitize=kernel-address is not supported by compiler)
19 endif
20else
21 # -fasan-shadow-offset fails without -fsanitize
22 CFLAGS_KASAN_SHADOW := $(call cc-option, -fsanitize=kernel-address \
23 -fasan-shadow-offset=$(KASAN_SHADOW_OFFSET), \ 18 -fasan-shadow-offset=$(KASAN_SHADOW_OFFSET), \
24 $(call cc-option, -fsanitize=kernel-address \ 19 $(call cc-option, -fsanitize=kernel-address \
25 -mllvm -asan-mapping-offset=$(KASAN_SHADOW_OFFSET))) 20 -mllvm -asan-mapping-offset=$(KASAN_SHADOW_OFFSET)))
26 21
27 ifeq ($(strip $(CFLAGS_KASAN_SHADOW)),) 22ifeq ($(strip $(CFLAGS_KASAN_SHADOW)),)
28 CFLAGS_KASAN := $(CFLAGS_KASAN_MINIMAL) 23 CFLAGS_KASAN := $(CFLAGS_KASAN_MINIMAL)
29 else 24else
30 # Now add all the compiler specific options that are valid standalone 25 # Now add all the compiler specific options that are valid standalone
31 CFLAGS_KASAN := $(CFLAGS_KASAN_SHADOW) \ 26 CFLAGS_KASAN := $(CFLAGS_KASAN_SHADOW) \
32 $(call cc-param,asan-globals=1) \ 27 $(call cc-param,asan-globals=1) \
33 $(call cc-param,asan-instrumentation-with-call-threshold=$(call_threshold)) \ 28 $(call cc-param,asan-instrumentation-with-call-threshold=$(call_threshold)) \
34 $(call cc-param,asan-stack=1) \ 29 $(call cc-param,asan-stack=1) \
35 $(call cc-param,asan-use-after-scope=1) \ 30 $(call cc-param,asan-use-after-scope=1) \
36 $(call cc-param,asan-instrument-allocas=1) 31 $(call cc-param,asan-instrument-allocas=1)
37 endif
38
39endif 32endif
40 33
41ifdef CONFIG_KASAN_EXTRA 34ifdef CONFIG_KASAN_EXTRA
42CFLAGS_KASAN += $(call cc-option, -fsanitize-address-use-after-scope) 35CFLAGS_KASAN += $(call cc-option, -fsanitize-address-use-after-scope)
43endif 36endif
44 37
45CFLAGS_KASAN_NOSANITIZE := -fno-builtin 38endif # CONFIG_KASAN_GENERIC
46 39
40ifdef CONFIG_KASAN_SW_TAGS
41
42ifdef CONFIG_KASAN_INLINE
43 instrumentation_flags := -mllvm -hwasan-mapping-offset=$(KASAN_SHADOW_OFFSET)
44else
45 instrumentation_flags := -mllvm -hwasan-instrument-with-calls=1
46endif
47
48CFLAGS_KASAN := -fsanitize=kernel-hwaddress \
49 -mllvm -hwasan-instrument-stack=0 \
50 $(instrumentation_flags)
51
52endif # CONFIG_KASAN_SW_TAGS
53
54ifdef CONFIG_KASAN
55CFLAGS_KASAN_NOSANITIZE := -fno-builtin
47endif 56endif