1 .. SPDX-License-Identifier: GPL-2.0 !! 1 The Kernel Address Sanitizer (KASAN) 2 .. Copyright (C) 2023, Google LLC. !! 2 ==================================== 3 << 4 Kernel Address Sanitizer (KASAN) << 5 ================================ << 6 3 7 Overview 4 Overview 8 -------- 5 -------- 9 6 10 Kernel Address Sanitizer (KASAN) is a dynamic !! 7 KernelAddressSANitizer (KASAN) is a dynamic memory error detector designed to 11 designed to find out-of-bounds and use-after-f !! 8 find out-of-bound and use-after-free bugs. KASAN has two modes: generic KASAN 12 !! 9 (similar to userspace ASan) and software tag-based KASAN (similar to userspace 13 KASAN has three modes: !! 10 HWASan). 14 !! 11 15 1. Generic KASAN !! 12 KASAN uses compile-time instrumentation to insert validity checks before every 16 2. Software Tag-Based KASAN !! 13 memory access, and therefore requires a compiler version that supports that. 17 3. Hardware Tag-Based KASAN !! 14 18 !! 15 Generic KASAN is supported in both GCC and Clang. With GCC it requires version 19 Generic KASAN, enabled with CONFIG_KASAN_GENER !! 16 4.9.2 or later for basic support and version 5.0 or later for detection of 20 debugging, similar to userspace ASan. This mod !! 17 out-of-bounds accesses for stack and global variables and for inline 21 architectures, but it has significant performa !! 18 instrumentation mode (see the Usage section). With Clang it requires version 22 !! 19 7.0.0 or later and it doesn't support detection of out-of-bounds accesses for 23 Software Tag-Based KASAN or SW_TAGS KASAN, ena !! 20 global variables yet. 24 can be used for both debugging and dogfood tes << 25 This mode is only supported for arm64, but its << 26 using it for testing on memory-restricted devi << 27 << 28 Hardware Tag-Based KASAN or HW_TAGS KASAN, ena << 29 is the mode intended to be used as an in-field << 30 security mitigation. This mode only works on a << 31 (Memory Tagging Extension), but it has low mem << 32 thus can be used in production. << 33 << 34 For details about the memory and performance i << 35 descriptions of the corresponding Kconfig opti << 36 << 37 The Generic and the Software Tag-Based modes a << 38 software modes. The Software Tag-Based and the << 39 referred to as the tag-based modes. << 40 21 41 Support !! 22 Tag-based KASAN is only supported in Clang and requires version 7.0.0 or later. 42 ------- << 43 23 44 Architectures !! 24 Currently generic KASAN is supported for the x86_64, arm64, xtensa and s390 45 ~~~~~~~~~~~~~ !! 25 architectures, and tag-based KASAN is supported only for arm64. 46 << 47 Generic KASAN is supported on x86_64, arm, arm << 48 and loongarch, and the tag-based KASAN modes a << 49 << 50 Compilers << 51 ~~~~~~~~~ << 52 << 53 Software KASAN modes use compile-time instrume << 54 before every memory access and thus require a << 55 support for that. The Hardware Tag-Based mode << 56 these checks but still requires a compiler ver << 57 tagging instructions. << 58 26 59 Generic KASAN requires GCC version 8.3.0 or la !! 27 Usage 60 or any Clang version supported by the kernel. !! 28 ----- 61 << 62 Software Tag-Based KASAN requires GCC 11+ << 63 or any Clang version supported by the kernel. << 64 29 65 Hardware Tag-Based KASAN requires GCC 10+ or C !! 30 To enable KASAN configure kernel with:: 66 31 67 Memory types !! 32 CONFIG_KASAN = y 68 ~~~~~~~~~~~~ << 69 33 70 Generic KASAN supports finding bugs in all of !! 34 and choose between CONFIG_KASAN_GENERIC (to enable generic KASAN) and 71 stack, and global memory. !! 35 CONFIG_KASAN_SW_TAGS (to enable software tag-based KASAN). 72 36 73 Software Tag-Based KASAN supports slab, page_a !! 37 You also need to choose between CONFIG_KASAN_OUTLINE and CONFIG_KASAN_INLINE. >> 38 Outline and inline are compiler instrumentation types. The former produces >> 39 smaller binary while the latter is 1.1 - 2 times faster. 74 40 75 Hardware Tag-Based KASAN supports slab, page_a !! 41 Both KASAN modes work with both SLUB and SLAB memory allocators. 76 memory. !! 42 For better bug detection and nicer reporting, enable CONFIG_STACKTRACE. 77 43 78 For slab, both software KASAN modes support SL !! 44 To disable instrumentation for specific files or directories, add a line 79 Hardware Tag-Based KASAN only supports SLUB. !! 45 similar to the following to the respective kernel Makefile: 80 46 81 Usage !! 47 - For a single file (e.g. main.o):: 82 ----- << 83 48 84 To enable KASAN, configure the kernel with:: !! 49 KASAN_SANITIZE_main.o := n 85 50 86 CONFIG_KASAN=y !! 51 - For all files in one directory:: 87 52 88 and choose between ``CONFIG_KASAN_GENERIC`` (t !! 53 KASAN_SANITIZE := n 89 ``CONFIG_KASAN_SW_TAGS`` (to enable Software T << 90 ``CONFIG_KASAN_HW_TAGS`` (to enable Hardware T << 91 << 92 For the software modes, also choose between `` << 93 ``CONFIG_KASAN_INLINE``. Outline and inline ar << 94 The former produces a smaller binary while the << 95 << 96 To include alloc and free stack traces of affe << 97 enable ``CONFIG_STACKTRACE``. To include alloc << 98 physical pages, enable ``CONFIG_PAGE_OWNER`` a << 99 << 100 Boot parameters << 101 ~~~~~~~~~~~~~~~ << 102 << 103 KASAN is affected by the generic ``panic_on_wa << 104 When it is enabled, KASAN panics the kernel af << 105 << 106 By default, KASAN prints a bug report only for << 107 With ``kasan_multi_shot``, KASAN prints a repo << 108 effectively disables ``panic_on_warn`` for KAS << 109 << 110 Alternatively, independent of ``panic_on_warn` << 111 parameter can be used to control panic and rep << 112 << 113 - ``kasan.fault=report``, ``=panic``, or ``=pa << 114 to only print a KASAN report, panic the kern << 115 invalid writes only (default: ``report``). T << 116 ``kasan_multi_shot`` is enabled. Note that w << 117 Hardware Tag-Based KASAN, ``kasan.fault=pani << 118 asynchronously checked accesses (including r << 119 << 120 Software and Hardware Tag-Based KASAN modes (s << 121 modes below) support altering stack trace coll << 122 << 123 - ``kasan.stacktrace=off`` or ``=on`` disables << 124 traces collection (default: ``on``). << 125 - ``kasan.stack_ring_size=<number of entries>` << 126 in the stack ring (default: ``32768``). << 127 << 128 Hardware Tag-Based KASAN mode is intended for << 129 mitigation. Therefore, it supports additional << 130 disabling KASAN altogether or controlling its << 131 << 132 - ``kasan=off`` or ``=on`` controls whether KA << 133 << 134 - ``kasan.mode=sync``, ``=async`` or ``=asymm` << 135 is configured in synchronous, asynchronous o << 136 execution (default: ``sync``). << 137 Synchronous mode: a bad access is detected i << 138 check fault occurs. << 139 Asynchronous mode: a bad access detection is << 140 fault occurs, the information is stored in h << 141 register for arm64). The kernel periodically << 142 only reports tag faults during these checks. << 143 Asymmetric mode: a bad access is detected sy << 144 asynchronously on writes. << 145 << 146 - ``kasan.vmalloc=off`` or ``=on`` disables or << 147 allocations (default: ``on``). << 148 << 149 - ``kasan.page_alloc.sample=<sampling interval << 150 Nth page_alloc allocation with the order equ << 151 ``kasan.page_alloc.sample.order``, where N i << 152 parameter (default: ``1``, or tag every such << 153 This parameter is intended to mitigate the p << 154 by KASAN. << 155 Note that enabling this parameter makes Hard << 156 of allocations chosen by sampling and thus m << 157 allocations. Use the default value for accur << 158 << 159 - ``kasan.page_alloc.sample.order=<minimum pag << 160 order of allocations that are affected by sa << 161 Only applies when ``kasan.page_alloc.sample` << 162 than ``1``. << 163 This parameter is intended to allow sampling << 164 allocations, which is the biggest source of << 165 54 166 Error reports 55 Error reports 167 ~~~~~~~~~~~~~ 56 ~~~~~~~~~~~~~ 168 57 169 A typical KASAN report looks like this:: !! 58 A typical out-of-bounds access generic KASAN report looks like this:: 170 59 171 ========================================== 60 ================================================================== 172 BUG: KASAN: slab-out-of-bounds in kmalloc_ !! 61 BUG: KASAN: slab-out-of-bounds in kmalloc_oob_right+0xa8/0xbc [test_kasan] 173 Write of size 1 at addr ffff8801f44ec37b b 62 Write of size 1 at addr ffff8801f44ec37b by task insmod/2760 174 63 175 CPU: 1 PID: 2760 Comm: insmod Not tainted 64 CPU: 1 PID: 2760 Comm: insmod Not tainted 4.19.0-rc3+ #698 176 Hardware name: QEMU Standard PC (i440FX + 65 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.10.2-1 04/01/2014 177 Call Trace: 66 Call Trace: 178 dump_stack+0x94/0xd8 67 dump_stack+0x94/0xd8 179 print_address_description+0x73/0x280 68 print_address_description+0x73/0x280 180 kasan_report+0x144/0x187 69 kasan_report+0x144/0x187 181 __asan_report_store1_noabort+0x17/0x20 70 __asan_report_store1_noabort+0x17/0x20 182 kmalloc_oob_right+0xa8/0xbc [kasan_test] !! 71 kmalloc_oob_right+0xa8/0xbc [test_kasan] 183 kmalloc_tests_init+0x16/0x700 [kasan_test !! 72 kmalloc_tests_init+0x16/0x700 [test_kasan] 184 do_one_initcall+0xa5/0x3ae 73 do_one_initcall+0xa5/0x3ae 185 do_init_module+0x1b6/0x547 74 do_init_module+0x1b6/0x547 186 load_module+0x75df/0x8070 75 load_module+0x75df/0x8070 187 __do_sys_init_module+0x1c6/0x200 76 __do_sys_init_module+0x1c6/0x200 188 __x64_sys_init_module+0x6e/0xb0 77 __x64_sys_init_module+0x6e/0xb0 189 do_syscall_64+0x9f/0x2c0 78 do_syscall_64+0x9f/0x2c0 190 entry_SYSCALL_64_after_hwframe+0x44/0xa9 79 entry_SYSCALL_64_after_hwframe+0x44/0xa9 191 RIP: 0033:0x7f96443109da 80 RIP: 0033:0x7f96443109da 192 RSP: 002b:00007ffcf0b51b08 EFLAGS: 0000020 81 RSP: 002b:00007ffcf0b51b08 EFLAGS: 00000202 ORIG_RAX: 00000000000000af 193 RAX: ffffffffffffffda RBX: 000055dc3ee521a 82 RAX: ffffffffffffffda RBX: 000055dc3ee521a0 RCX: 00007f96443109da 194 RDX: 00007f96445cff88 RSI: 0000000000057a5 83 RDX: 00007f96445cff88 RSI: 0000000000057a50 RDI: 00007f9644992000 195 RBP: 000055dc3ee510b0 R08: 000000000000000 84 RBP: 000055dc3ee510b0 R08: 0000000000000003 R09: 0000000000000000 196 R10: 00007f964430cd0a R11: 000000000000020 85 R10: 00007f964430cd0a R11: 0000000000000202 R12: 00007f96445cff88 197 R13: 000055dc3ee51090 R14: 000000000000000 86 R13: 000055dc3ee51090 R14: 0000000000000000 R15: 0000000000000000 198 87 199 Allocated by task 2760: 88 Allocated by task 2760: 200 save_stack+0x43/0xd0 89 save_stack+0x43/0xd0 201 kasan_kmalloc+0xa7/0xd0 90 kasan_kmalloc+0xa7/0xd0 202 kmem_cache_alloc_trace+0xe1/0x1b0 91 kmem_cache_alloc_trace+0xe1/0x1b0 203 kmalloc_oob_right+0x56/0xbc [kasan_test] !! 92 kmalloc_oob_right+0x56/0xbc [test_kasan] 204 kmalloc_tests_init+0x16/0x700 [kasan_test !! 93 kmalloc_tests_init+0x16/0x700 [test_kasan] 205 do_one_initcall+0xa5/0x3ae 94 do_one_initcall+0xa5/0x3ae 206 do_init_module+0x1b6/0x547 95 do_init_module+0x1b6/0x547 207 load_module+0x75df/0x8070 96 load_module+0x75df/0x8070 208 __do_sys_init_module+0x1c6/0x200 97 __do_sys_init_module+0x1c6/0x200 209 __x64_sys_init_module+0x6e/0xb0 98 __x64_sys_init_module+0x6e/0xb0 210 do_syscall_64+0x9f/0x2c0 99 do_syscall_64+0x9f/0x2c0 211 entry_SYSCALL_64_after_hwframe+0x44/0xa9 100 entry_SYSCALL_64_after_hwframe+0x44/0xa9 212 101 213 Freed by task 815: 102 Freed by task 815: 214 save_stack+0x43/0xd0 103 save_stack+0x43/0xd0 215 __kasan_slab_free+0x135/0x190 104 __kasan_slab_free+0x135/0x190 216 kasan_slab_free+0xe/0x10 105 kasan_slab_free+0xe/0x10 217 kfree+0x93/0x1a0 106 kfree+0x93/0x1a0 218 umh_complete+0x6a/0xa0 107 umh_complete+0x6a/0xa0 219 call_usermodehelper_exec_async+0x4c3/0x64 108 call_usermodehelper_exec_async+0x4c3/0x640 220 ret_from_fork+0x35/0x40 109 ret_from_fork+0x35/0x40 221 110 222 The buggy address belongs to the object at 111 The buggy address belongs to the object at ffff8801f44ec300 223 which belongs to the cache kmalloc-128 of 112 which belongs to the cache kmalloc-128 of size 128 224 The buggy address is located 123 bytes ins 113 The buggy address is located 123 bytes inside of 225 128-byte region [ffff8801f44ec300, ffff88 114 128-byte region [ffff8801f44ec300, ffff8801f44ec380) 226 The buggy address belongs to the page: 115 The buggy address belongs to the page: 227 page:ffffea0007d13b00 count:1 mapcount:0 m 116 page:ffffea0007d13b00 count:1 mapcount:0 mapping:ffff8801f7001640 index:0x0 228 flags: 0x200000000000100(slab) 117 flags: 0x200000000000100(slab) 229 raw: 0200000000000100 ffffea0007d11dc0 000 118 raw: 0200000000000100 ffffea0007d11dc0 0000001a0000001a ffff8801f7001640 230 raw: 0000000000000000 0000000080150015 000 119 raw: 0000000000000000 0000000080150015 00000001ffffffff 0000000000000000 231 page dumped because: kasan: bad access det 120 page dumped because: kasan: bad access detected 232 121 233 Memory state around the buggy address: 122 Memory state around the buggy address: 234 ffff8801f44ec200: fc fc fc fc fc fc fc fc 123 ffff8801f44ec200: fc fc fc fc fc fc fc fc fb fb fb fb fb fb fb fb 235 ffff8801f44ec280: fb fb fb fb fb fb fb fb 124 ffff8801f44ec280: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc 236 >ffff8801f44ec300: 00 00 00 00 00 00 00 00 125 >ffff8801f44ec300: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 03 237 126 ^ 238 ffff8801f44ec380: fc fc fc fc fc fc fc fc 127 ffff8801f44ec380: fc fc fc fc fc fc fc fc fb fb fb fb fb fb fb fb 239 ffff8801f44ec400: fb fb fb fb fb fb fb fb 128 ffff8801f44ec400: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc 240 ========================================== 129 ================================================================== 241 130 242 The report header summarizes what kind of bug !! 131 The header of the report provides a short summary of what kind of bug happened 243 caused it. It is followed by a stack trace of !! 132 and what kind of access caused it. It's followed by a stack trace of the bad 244 where the accessed memory was allocated (in ca !! 133 access, a stack trace of where the accessed memory was allocated (in case bad 245 and a stack trace of where the object was free !! 134 access happens on a slab object), and a stack trace of where the object was 246 bug report). Next comes a description of the a !! 135 freed (in case of a use-after-free bug report). Next comes a description of 247 information about the accessed memory page. !! 136 the accessed slab object and information about the accessed memory page. 248 !! 137 249 In the end, the report shows the memory state !! 138 In the last section the report shows memory state around the accessed address. 250 Internally, KASAN tracks memory state separate !! 139 Reading this part requires some understanding of how KASAN works. 251 is either 8 or 16 aligned bytes depending on K !! 140 252 memory state section of the report shows the s !! 141 The state of each 8 aligned bytes of memory is encoded in one shadow byte. 253 granules that surround the accessed address. !! 142 Those 8 bytes can be accessible, partially accessible, freed or be a redzone. 254 !! 143 We use the following encoding for each shadow byte: 0 means that all 8 bytes 255 For Generic KASAN, the size of each memory gra !! 144 of the corresponding memory region are accessible; number N (1 <= N <= 7) means 256 granule is encoded in one shadow byte. Those 8 !! 145 that the first N bytes are accessible, and other (8 - N) bytes are not; 257 partially accessible, freed, or be a part of a !! 146 any negative value indicates that the entire 8-byte word is inaccessible. 258 encoding for each shadow byte: 00 means that a !! 147 We use different negative values to distinguish between different kinds of 259 memory region are accessible; number N (1 <= N !! 148 inaccessible memory like redzones or freed memory (see mm/kasan/kasan.h). 260 bytes are accessible, and other (8 - N) bytes << 261 indicates that the entire 8-byte word is inacc << 262 negative values to distinguish between differe << 263 like redzones or freed memory (see mm/kasan/ka << 264 << 265 In the report above, the arrow points to the s << 266 that the accessed address is partially accessi << 267 << 268 For tag-based KASAN modes, this last report se << 269 the accessed address (see the `Implementation << 270 << 271 Note that KASAN bug titles (like ``slab-out-of << 272 are best-effort: KASAN prints the most probabl << 273 information it has. The actual type of the bug << 274 << 275 Generic KASAN also reports up to two auxiliary << 276 traces point to places in code that interacted << 277 directly present in the bad access stack trace << 278 call_rcu() and workqueue queuing. << 279 << 280 CONFIG_KASAN_EXTRA_INFO << 281 ~~~~~~~~~~~~~~~~~~~~~~~ << 282 << 283 Enabling CONFIG_KASAN_EXTRA_INFO allows KASAN << 284 information. The extra information currently s << 285 timestamp at allocation and free. More informa << 286 the bug and correlate the error with other sys << 287 extra memory to record more information (more << 288 CONFIG_KASAN_EXTRA_INFO). << 289 149 290 Here is the report with CONFIG_KASAN_EXTRA_INF !! 150 In the report above the arrows point to the shadow byte 03, which means that 291 different parts are shown):: !! 151 the accessed address is partially accessible. >> 152 >> 153 For tag-based KASAN this last report section shows the memory tags around the >> 154 accessed address (see Implementation details section). 292 155 293 ========================================== << 294 ... << 295 Allocated by task 134 on cpu 5 at 229.1338 << 296 ... << 297 Freed by task 136 on cpu 3 at 230.199335s: << 298 ... << 299 ========================================== << 300 156 301 Implementation details 157 Implementation details 302 ---------------------- 158 ---------------------- 303 159 304 Generic KASAN 160 Generic KASAN 305 ~~~~~~~~~~~~~ 161 ~~~~~~~~~~~~~ 306 162 307 Software KASAN modes use shadow memory to reco !! 163 From a high level, our approach to memory error detection is similar to that 308 safe to access and use compile-time instrument !! 164 of kmemcheck: use shadow memory to record whether each byte of memory is safe 309 checks before each memory access. !! 165 to access, and use compile-time instrumentation to insert checks of shadow >> 166 memory on each memory access. 310 167 311 Generic KASAN dedicates 1/8th of kernel memory !! 168 Generic KASAN dedicates 1/8th of kernel memory to its shadow memory (e.g. 16TB 312 to cover 128TB on x86_64) and uses direct mapp 169 to cover 128TB on x86_64) and uses direct mapping with a scale and offset to 313 translate a memory address to its correspondin 170 translate a memory address to its corresponding shadow address. 314 171 315 Here is the function which translates an addre 172 Here is the function which translates an address to its corresponding shadow 316 address:: 173 address:: 317 174 318 static inline void *kasan_mem_to_shadow(co 175 static inline void *kasan_mem_to_shadow(const void *addr) 319 { 176 { 320 return (void *)((unsigned long)addr >> !! 177 return ((unsigned long)addr >> KASAN_SHADOW_SCALE_SHIFT) 321 + KASAN_SHADOW_OFFSET; 178 + KASAN_SHADOW_OFFSET; 322 } 179 } 323 180 324 where ``KASAN_SHADOW_SCALE_SHIFT = 3``. 181 where ``KASAN_SHADOW_SCALE_SHIFT = 3``. 325 182 326 Compile-time instrumentation is used to insert 183 Compile-time instrumentation is used to insert memory access checks. Compiler 327 inserts function calls (``__asan_load*(addr)`` !! 184 inserts function calls (__asan_load*(addr), __asan_store*(addr)) before each 328 each memory access of size 1, 2, 4, 8, or 16. !! 185 memory access of size 1, 2, 4, 8 or 16. These functions check whether memory 329 memory accesses are valid or not by checking c !! 186 access is valid or not by checking corresponding shadow memory. 330 !! 187 331 With inline instrumentation, instead of making !! 188 GCC 5.0 has possibility to perform inline instrumentation. Instead of making 332 directly inserts the code to check shadow memo !! 189 function calls GCC directly inserts the code to check the shadow memory. 333 enlarges the kernel, but it gives an x1.1-x2 p !! 190 This option significantly enlarges kernel but it gives x1.1-x2 performance 334 outline-instrumented kernel. !! 191 boost over outline instrumented kernel. 335 << 336 Generic KASAN is the only mode that delays the << 337 quarantine (see mm/kasan/quarantine.c for impl << 338 192 339 Software Tag-Based KASAN !! 193 Software tag-based KASAN 340 ~~~~~~~~~~~~~~~~~~~~~~~~ 194 ~~~~~~~~~~~~~~~~~~~~~~~~ 341 195 342 Software Tag-Based KASAN uses a software memor !! 196 Tag-based KASAN uses the Top Byte Ignore (TBI) feature of modern arm64 CPUs to 343 access validity. It is currently only implemen !! 197 store a pointer tag in the top byte of kernel pointers. Like generic KASAN it >> 198 uses shadow memory to store memory tags associated with each 16-byte memory >> 199 cell (therefore it dedicates 1/16th of the kernel memory for shadow memory). >> 200 >> 201 On each memory allocation tag-based KASAN generates a random tag, tags the >> 202 allocated memory with this tag, and embeds this tag into the returned pointer. >> 203 Software tag-based KASAN uses compile-time instrumentation to insert checks >> 204 before each memory access. These checks make sure that tag of the memory that >> 205 is being accessed is equal to tag of the pointer that is used to access this >> 206 memory. In case of a tag mismatch tag-based KASAN prints a bug report. 344 207 345 Software Tag-Based KASAN uses the Top Byte Ign !! 208 Software tag-based KASAN also has two instrumentation modes (outline, that 346 to store a pointer tag in the top byte of kern !! 209 emits callbacks to check memory accesses; and inline, that performs the shadow 347 to store memory tags associated with each 16-b << 348 dedicates 1/16th of the kernel memory for shad << 349 << 350 On each memory allocation, Software Tag-Based << 351 the allocated memory with this tag, and embeds << 352 pointer. << 353 << 354 Software Tag-Based KASAN uses compile-time ins << 355 before each memory access. These checks make s << 356 that is being accessed is equal to the tag of << 357 this memory. In case of a tag mismatch, Softwa << 358 report. << 359 << 360 Software Tag-Based KASAN also has two instrume << 361 emits callbacks to check memory accesses; and << 362 memory checks inline). With outline instrument 210 memory checks inline). With outline instrumentation mode, a bug report is 363 printed from the function that performs the ac !! 211 simply printed from the function that performs the access check. With inline 364 instrumentation, a ``brk`` instruction is emit !! 212 instrumentation a brk instruction is emitted by the compiler, and a dedicated 365 dedicated ``brk`` handler is used to print bug !! 213 brk handler is used to print bug reports. 366 !! 214 367 Software Tag-Based KASAN uses 0xFF as a match- !! 215 A potential expansion of this mode is a hardware tag-based mode, which would 368 pointers with the 0xFF pointer tag are not che !! 216 use hardware memory tagging support instead of compiler instrumentation and 369 reserved to tag freed memory regions. !! 217 manual shadow memory manipulation. 370 << 371 Hardware Tag-Based KASAN << 372 ~~~~~~~~~~~~~~~~~~~~~~~~ << 373 << 374 Hardware Tag-Based KASAN is similar to the sof << 375 hardware memory tagging support instead of com << 376 shadow memory. << 377 << 378 Hardware Tag-Based KASAN is currently only imp << 379 and based on both arm64 Memory Tagging Extensi << 380 Instruction Set Architecture and Top Byte Igno << 381 << 382 Special arm64 instructions are used to assign << 383 Same tags are assigned to pointers to those al << 384 access, hardware makes sure that the tag of th << 385 equal to the tag of the pointer that is used t << 386 tag mismatch, a fault is generated, and a repo << 387 << 388 Hardware Tag-Based KASAN uses 0xFF as a match- << 389 pointers with the 0xFF pointer tag are not che << 390 reserved to tag freed memory regions. << 391 << 392 If the hardware does not support MTE (pre ARMv << 393 will not be enabled. In this case, all KASAN b << 394 << 395 Note that enabling CONFIG_KASAN_HW_TAGS always << 396 enabled. Even when ``kasan.mode=off`` is provi << 397 support MTE (but supports TBI). << 398 << 399 Hardware Tag-Based KASAN only reports the firs << 400 checking gets disabled. << 401 << 402 Shadow memory << 403 ------------- << 404 << 405 The contents of this section are only applicab << 406 << 407 The kernel maps memory in several different pa << 408 The range of kernel virtual addresses is large << 409 memory to support a real shadow region for eve << 410 accessed by the kernel. Therefore, KASAN only << 411 parts of the address space. << 412 << 413 Default behaviour << 414 ~~~~~~~~~~~~~~~~~ << 415 << 416 By default, architectures only map real memory << 417 for the linear mapping (and potentially other << 418 other areas - such as vmalloc and vmemmap spac << 419 page is mapped over the shadow area. This read << 420 declares all memory accesses as permitted. << 421 << 422 This presents a problem for modules: they do n << 423 mapping but in a dedicated module space. By ho << 424 allocator, KASAN temporarily maps real shadow << 425 This allows detection of invalid accesses to m << 426 << 427 This also creates an incompatibility with ``VM << 428 lives in vmalloc space, it will be shadowed by << 429 the kernel will fault when trying to set up th << 430 variables. << 431 << 432 CONFIG_KASAN_VMALLOC << 433 ~~~~~~~~~~~~~~~~~~~~ << 434 << 435 With ``CONFIG_KASAN_VMALLOC``, KASAN can cover << 436 cost of greater memory usage. Currently, this << 437 arm64, riscv, s390, and powerpc. << 438 << 439 This works by hooking into vmalloc and vmap an << 440 allocating real shadow memory to back the mapp << 441 << 442 Most mappings in vmalloc space are small, requ << 443 page of shadow space. Allocating a full shadow << 444 therefore be wasteful. Furthermore, to ensure << 445 use different shadow pages, mappings would hav << 446 ``KASAN_GRANULE_SIZE * PAGE_SIZE``. << 447 << 448 Instead, KASAN shares backing space across mul << 449 a backing page when a mapping in vmalloc space << 450 of the shadow region. This page can be shared << 451 mappings later on. << 452 << 453 KASAN hooks into the vmap infrastructure to la << 454 memory. << 455 << 456 To avoid the difficulties around swapping mapp << 457 that the part of the shadow region that covers << 458 not be covered by the early shadow page but wi << 459 This will require changes in arch-specific cod << 460 << 461 This allows ``VMAP_STACK`` support on x86 and << 462 architectures that do not have a fixed module << 463 << 464 For developers << 465 -------------- << 466 << 467 Ignoring accesses << 468 ~~~~~~~~~~~~~~~~~ << 469 << 470 Software KASAN modes use compiler instrumentat << 471 Such instrumentation might be incompatible wit << 472 therefore needs to be disabled. << 473 << 474 Other parts of the kernel might access metadat << 475 Normally, KASAN detects and reports such acces << 476 in memory allocators), these accesses are vali << 477 << 478 For software KASAN modes, to disable instrumen << 479 directory, add a ``KASAN_SANITIZE`` annotation << 480 Makefile: << 481 << 482 - For a single file (e.g., main.o):: << 483 << 484 KASAN_SANITIZE_main.o := n << 485 << 486 - For all files in one directory:: << 487 << 488 KASAN_SANITIZE := n << 489 << 490 For software KASAN modes, to disable instrumen << 491 use the KASAN-specific ``__no_sanitize_address << 492 generic ``noinstr`` one. << 493 << 494 Note that disabling compiler instrumentation ( << 495 per-function basis) makes KASAN ignore the acc << 496 that code for software KASAN modes. It does no << 497 indirectly (through calls to instrumented func << 498 Tag-Based KASAN, which does not use compiler i << 499 << 500 For software KASAN modes, to disable KASAN rep << 501 for the current task, annotate this part of th << 502 ``kasan_disable_current()``/``kasan_enable_cur << 503 disables the reports for indirect accesses tha << 504 << 505 For tag-based KASAN modes, to disable access c << 506 ``kasan_reset_tag()`` or ``page_kasan_tag_rese << 507 disabling access checking via ``page_kasan_tag << 508 restoring the per-page KASAN tag via ``page_ka << 509 << 510 Tests << 511 ~~~~~ << 512 << 513 There are KASAN tests that allow verifying tha << 514 certain types of memory corruptions. The tests << 515 << 516 1. Tests that are integrated with the KUnit Te << 517 ``CONFIG_KASAN_KUNIT_TEST``. These tests can b << 518 automatically in a few different ways; see the << 519 << 520 2. Tests that are currently incompatible with << 521 ``CONFIG_KASAN_MODULE_TEST`` and can only be r << 522 only be verified manually by loading the kerne << 523 kernel log for KASAN reports. << 524 << 525 Each KUnit-compatible KASAN test prints one of << 526 error is detected. Then the test prints its nu << 527 << 528 When a test passes:: << 529 << 530 ok 28 - kmalloc_double_kzfree << 531 << 532 When a test fails due to a failed ``kmalloc``: << 533 << 534 # kmalloc_large_oob_right: ASSERTION F << 535 Expected ptr is not null, but is << 536 not ok 5 - kmalloc_large_oob_right << 537 << 538 When a test fails due to a missing KASAN repor << 539 << 540 # kmalloc_double_kzfree: EXPECTATION F << 541 KASAN failure expected in "kfree_sensi << 542 not ok 28 - kmalloc_double_kzfree << 543 << 544 << 545 At the end the cumulative status of all KASAN << 546 << 547 ok 1 - kasan << 548 << 549 Or, if one of the tests failed:: << 550 << 551 not ok 1 - kasan << 552 << 553 There are a few ways to run KUnit-compatible K << 554 << 555 1. Loadable module << 556 << 557 With ``CONFIG_KUNIT`` enabled, KASAN-KUnit << 558 module and run by loading ``kasan_test.ko`` << 559 << 560 2. Built-In << 561 << 562 With ``CONFIG_KUNIT`` built-in, KASAN-KUnit << 563 In this case, the tests will run at boot as << 564 << 565 3. Using kunit_tool << 566 << 567 With ``CONFIG_KUNIT`` and ``CONFIG_KASAN_KU << 568 possible to use ``kunit_tool`` to see the r << 569 readable way. This will not print the KASAN << 570 See `KUnit documentation <https://www.kerne << 571 for more up-to-date information on ``kunit_ << 572 << 573 .. _KUnit: https://www.kernel.org/doc/html/lat <<
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.