xref: /OK3568_Linux_fs/kernel/lib/Kconfig.kasan (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun# SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun# This config refers to the generic KASAN mode.
3*4882a593Smuzhiyunconfig HAVE_ARCH_KASAN
4*4882a593Smuzhiyun	bool
5*4882a593Smuzhiyun
6*4882a593Smuzhiyunconfig HAVE_ARCH_KASAN_SW_TAGS
7*4882a593Smuzhiyun	bool
8*4882a593Smuzhiyun
9*4882a593Smuzhiyunconfig HAVE_ARCH_KASAN_HW_TAGS
10*4882a593Smuzhiyun	bool
11*4882a593Smuzhiyun
12*4882a593Smuzhiyunconfig HAVE_ARCH_KASAN_VMALLOC
13*4882a593Smuzhiyun	bool
14*4882a593Smuzhiyun
15*4882a593Smuzhiyunconfig CC_HAS_KASAN_GENERIC
16*4882a593Smuzhiyun	def_bool $(cc-option, -fsanitize=kernel-address)
17*4882a593Smuzhiyun
18*4882a593Smuzhiyunconfig CC_HAS_KASAN_SW_TAGS
19*4882a593Smuzhiyun	def_bool $(cc-option, -fsanitize=kernel-hwaddress)
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun# This option is only required for software KASAN modes.
22*4882a593Smuzhiyun# Old GCC versions don't have proper support for no_sanitize_address.
23*4882a593Smuzhiyun# See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89124 for details.
24*4882a593Smuzhiyunconfig CC_HAS_WORKING_NOSANITIZE_ADDRESS
25*4882a593Smuzhiyun	def_bool !CC_IS_GCC || GCC_VERSION >= 80300
26*4882a593Smuzhiyun
27*4882a593Smuzhiyunmenuconfig KASAN
28*4882a593Smuzhiyun	bool "KASAN: runtime memory debugger"
29*4882a593Smuzhiyun	depends on (((HAVE_ARCH_KASAN && CC_HAS_KASAN_GENERIC) || \
30*4882a593Smuzhiyun		     (HAVE_ARCH_KASAN_SW_TAGS && CC_HAS_KASAN_SW_TAGS)) && \
31*4882a593Smuzhiyun		    CC_HAS_WORKING_NOSANITIZE_ADDRESS) || \
32*4882a593Smuzhiyun		   HAVE_ARCH_KASAN_HW_TAGS
33*4882a593Smuzhiyun	depends on (SLUB && SYSFS) || (SLAB && !DEBUG_SLAB)
34*4882a593Smuzhiyun	select STACKDEPOT
35*4882a593Smuzhiyun	help
36*4882a593Smuzhiyun	  Enables KASAN (KernelAddressSANitizer) - runtime memory debugger,
37*4882a593Smuzhiyun	  designed to find out-of-bounds accesses and use-after-free bugs.
38*4882a593Smuzhiyun	  See Documentation/dev-tools/kasan.rst for details.
39*4882a593Smuzhiyun
40*4882a593Smuzhiyunif KASAN
41*4882a593Smuzhiyun
42*4882a593Smuzhiyunchoice
43*4882a593Smuzhiyun	prompt "KASAN mode"
44*4882a593Smuzhiyun	default KASAN_GENERIC
45*4882a593Smuzhiyun	help
46*4882a593Smuzhiyun	  KASAN has three modes:
47*4882a593Smuzhiyun	  1. generic KASAN (similar to userspace ASan,
48*4882a593Smuzhiyun	     x86_64/arm64/xtensa, enabled with CONFIG_KASAN_GENERIC),
49*4882a593Smuzhiyun	  2. software tag-based KASAN (arm64 only, based on software
50*4882a593Smuzhiyun	     memory tagging (similar to userspace HWASan), enabled with
51*4882a593Smuzhiyun	     CONFIG_KASAN_SW_TAGS), and
52*4882a593Smuzhiyun	  3. hardware tag-based KASAN (arm64 only, based on hardware
53*4882a593Smuzhiyun	     memory tagging, enabled with CONFIG_KASAN_HW_TAGS).
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun	  All KASAN modes are strictly debugging features.
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun	  For better error reports enable CONFIG_STACKTRACE.
58*4882a593Smuzhiyun
59*4882a593Smuzhiyunconfig KASAN_GENERIC
60*4882a593Smuzhiyun	bool "Generic mode"
61*4882a593Smuzhiyun	depends on HAVE_ARCH_KASAN && CC_HAS_KASAN_GENERIC
62*4882a593Smuzhiyun	depends on CC_HAS_WORKING_NOSANITIZE_ADDRESS
63*4882a593Smuzhiyun	select SLUB_DEBUG if SLUB
64*4882a593Smuzhiyun	select CONSTRUCTORS
65*4882a593Smuzhiyun	help
66*4882a593Smuzhiyun	  Enables generic KASAN mode.
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun	  This mode is supported in both GCC and Clang. With GCC it requires
69*4882a593Smuzhiyun	  version 8.3.0 or later. Any supported Clang version is compatible,
70*4882a593Smuzhiyun	  but detection of out-of-bounds accesses for global variables is
71*4882a593Smuzhiyun	  supported only since Clang 11.
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun	  This mode consumes about 1/8th of available memory at kernel start
74*4882a593Smuzhiyun	  and introduces an overhead of ~x1.5 for the rest of the allocations.
75*4882a593Smuzhiyun	  The performance slowdown is ~x3.
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun	  Currently CONFIG_KASAN_GENERIC doesn't work with CONFIG_DEBUG_SLAB
78*4882a593Smuzhiyun	  (the resulting kernel does not boot).
79*4882a593Smuzhiyun
80*4882a593Smuzhiyunconfig KASAN_SW_TAGS
81*4882a593Smuzhiyun	bool "Software tag-based mode"
82*4882a593Smuzhiyun	depends on HAVE_ARCH_KASAN_SW_TAGS && CC_HAS_KASAN_SW_TAGS
83*4882a593Smuzhiyun	depends on CC_HAS_WORKING_NOSANITIZE_ADDRESS
84*4882a593Smuzhiyun	select SLUB_DEBUG if SLUB
85*4882a593Smuzhiyun	select CONSTRUCTORS
86*4882a593Smuzhiyun	help
87*4882a593Smuzhiyun	  Enables software tag-based KASAN mode.
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun	  This mode require software memory tagging support in the form of
90*4882a593Smuzhiyun	  HWASan-like compiler instrumentation.
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun	  Currently this mode is only implemented for arm64 CPUs and relies on
93*4882a593Smuzhiyun	  Top Byte Ignore. This mode requires Clang.
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun	  This mode consumes about 1/16th of available memory at kernel start
96*4882a593Smuzhiyun	  and introduces an overhead of ~20% for the rest of the allocations.
97*4882a593Smuzhiyun	  This mode may potentially introduce problems relating to pointer
98*4882a593Smuzhiyun	  casting and comparison, as it embeds tags into the top byte of each
99*4882a593Smuzhiyun	  pointer.
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun	  Currently CONFIG_KASAN_SW_TAGS doesn't work with CONFIG_DEBUG_SLAB
102*4882a593Smuzhiyun	  (the resulting kernel does not boot).
103*4882a593Smuzhiyun
104*4882a593Smuzhiyunconfig KASAN_HW_TAGS
105*4882a593Smuzhiyun	bool "Hardware tag-based mode"
106*4882a593Smuzhiyun	depends on HAVE_ARCH_KASAN_HW_TAGS
107*4882a593Smuzhiyun	depends on SLUB
108*4882a593Smuzhiyun	help
109*4882a593Smuzhiyun	  Enables hardware tag-based KASAN mode.
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun	  This mode requires hardware memory tagging support, and can be used
112*4882a593Smuzhiyun	  by any architecture that provides it.
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun	  Currently this mode is only implemented for arm64 CPUs starting from
115*4882a593Smuzhiyun	  ARMv8.5 and relies on Memory Tagging Extension and Top Byte Ignore.
116*4882a593Smuzhiyun
117*4882a593Smuzhiyunendchoice
118*4882a593Smuzhiyun
119*4882a593Smuzhiyunchoice
120*4882a593Smuzhiyun	prompt "Instrumentation type"
121*4882a593Smuzhiyun	depends on KASAN_GENERIC || KASAN_SW_TAGS
122*4882a593Smuzhiyun	default KASAN_OUTLINE
123*4882a593Smuzhiyun
124*4882a593Smuzhiyunconfig KASAN_OUTLINE
125*4882a593Smuzhiyun	bool "Outline instrumentation"
126*4882a593Smuzhiyun	help
127*4882a593Smuzhiyun	  Before every memory access compiler insert function call
128*4882a593Smuzhiyun	  __asan_load*/__asan_store*. These functions performs check
129*4882a593Smuzhiyun	  of shadow memory. This is slower than inline instrumentation,
130*4882a593Smuzhiyun	  however it doesn't bloat size of kernel's .text section so
131*4882a593Smuzhiyun	  much as inline does.
132*4882a593Smuzhiyun
133*4882a593Smuzhiyunconfig KASAN_INLINE
134*4882a593Smuzhiyun	bool "Inline instrumentation"
135*4882a593Smuzhiyun	help
136*4882a593Smuzhiyun	  Compiler directly inserts code checking shadow memory before
137*4882a593Smuzhiyun	  memory accesses. This is faster than outline (in some workloads
138*4882a593Smuzhiyun	  it gives about x2 boost over outline instrumentation), but
139*4882a593Smuzhiyun	  make kernel's .text size much bigger.
140*4882a593Smuzhiyun
141*4882a593Smuzhiyunendchoice
142*4882a593Smuzhiyun
143*4882a593Smuzhiyunconfig KASAN_STACK
144*4882a593Smuzhiyun	bool "Enable stack instrumentation (unsafe)" if CC_IS_CLANG && !COMPILE_TEST
145*4882a593Smuzhiyun	depends on KASAN_GENERIC || KASAN_SW_TAGS
146*4882a593Smuzhiyun	default y if CC_IS_GCC
147*4882a593Smuzhiyun	help
148*4882a593Smuzhiyun	  The LLVM stack address sanitizer has a know problem that
149*4882a593Smuzhiyun	  causes excessive stack usage in a lot of functions, see
150*4882a593Smuzhiyun	  https://bugs.llvm.org/show_bug.cgi?id=38809
151*4882a593Smuzhiyun	  Disabling asan-stack makes it safe to run kernels build
152*4882a593Smuzhiyun	  with clang-8 with KASAN enabled, though it loses some of
153*4882a593Smuzhiyun	  the functionality.
154*4882a593Smuzhiyun	  This feature is always disabled when compile-testing with clang
155*4882a593Smuzhiyun	  to avoid cluttering the output in stack overflow warnings,
156*4882a593Smuzhiyun	  but clang users can still enable it for builds without
157*4882a593Smuzhiyun	  CONFIG_COMPILE_TEST.	On gcc it is assumed to always be safe
158*4882a593Smuzhiyun	  to use and enabled by default.
159*4882a593Smuzhiyun
160*4882a593Smuzhiyunconfig KASAN_S390_4_LEVEL_PAGING
161*4882a593Smuzhiyun	bool "KASan: use 4-level paging"
162*4882a593Smuzhiyun	depends on S390
163*4882a593Smuzhiyun	help
164*4882a593Smuzhiyun	  Compiling the kernel with KASan disables automatic 3-level vs
165*4882a593Smuzhiyun	  4-level paging selection. 3-level paging is used by default (up
166*4882a593Smuzhiyun	  to 3TB of RAM with KASan enabled). This options allows to force
167*4882a593Smuzhiyun	  4-level paging instead.
168*4882a593Smuzhiyun
169*4882a593Smuzhiyunconfig KASAN_SW_TAGS_IDENTIFY
170*4882a593Smuzhiyun	bool "Enable memory corruption identification"
171*4882a593Smuzhiyun	depends on KASAN_SW_TAGS
172*4882a593Smuzhiyun	help
173*4882a593Smuzhiyun	  This option enables best-effort identification of bug type
174*4882a593Smuzhiyun	  (use-after-free or out-of-bounds) at the cost of increased
175*4882a593Smuzhiyun	  memory consumption.
176*4882a593Smuzhiyun
177*4882a593Smuzhiyunconfig KASAN_VMALLOC
178*4882a593Smuzhiyun	bool "Back mappings in vmalloc space with real shadow memory"
179*4882a593Smuzhiyun	depends on KASAN_GENERIC && HAVE_ARCH_KASAN_VMALLOC
180*4882a593Smuzhiyun	help
181*4882a593Smuzhiyun	  By default, the shadow region for vmalloc space is the read-only
182*4882a593Smuzhiyun	  zero page. This means that KASAN cannot detect errors involving
183*4882a593Smuzhiyun	  vmalloc space.
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun	  Enabling this option will hook in to vmap/vmalloc and back those
186*4882a593Smuzhiyun	  mappings with real shadow memory allocated on demand. This allows
187*4882a593Smuzhiyun	  for KASAN to detect more sorts of errors (and to support vmapped
188*4882a593Smuzhiyun	  stacks), but at the cost of higher memory usage.
189*4882a593Smuzhiyun
190*4882a593Smuzhiyunconfig KASAN_KUNIT_TEST
191*4882a593Smuzhiyun	tristate "KUnit-compatible tests of KASAN bug detection capabilities" if !KUNIT_ALL_TESTS
192*4882a593Smuzhiyun	depends on KASAN && KUNIT
193*4882a593Smuzhiyun	default KUNIT_ALL_TESTS
194*4882a593Smuzhiyun	help
195*4882a593Smuzhiyun	  This is a KUnit test suite doing various nasty things like
196*4882a593Smuzhiyun	  out of bounds and use after free accesses. It is useful for testing
197*4882a593Smuzhiyun	  kernel debugging features like KASAN.
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun	  For more information on KUnit and unit tests in general, please refer
200*4882a593Smuzhiyun	  to the KUnit documentation in Documentation/dev-tools/kunit.
201*4882a593Smuzhiyun
202*4882a593Smuzhiyunconfig KASAN_MODULE_TEST
203*4882a593Smuzhiyun	tristate "KUnit-incompatible tests of KASAN bug detection capabilities"
204*4882a593Smuzhiyun	depends on m && KASAN && !KASAN_HW_TAGS
205*4882a593Smuzhiyun	help
206*4882a593Smuzhiyun	  This is a part of the KASAN test suite that is incompatible with
207*4882a593Smuzhiyun	  KUnit. Currently includes tests that do bad copy_from/to_user
208*4882a593Smuzhiyun	  accesses.
209*4882a593Smuzhiyun
210*4882a593Smuzhiyunendif # KASAN
211