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. It provides 11 designed to find out-of-bounds and use-after-f !! 8 a fast and comprehensive solution for finding use-after-free and out-of-bounds 12 !! 9 bugs. 13 KASAN has three modes: !! 10 14 !! 11 KASAN uses compile-time instrumentation for checking every memory access, 15 1. Generic KASAN !! 12 therefore you will need a GCC version 4.9.2 or later. GCC 5.0 or later is 16 2. Software Tag-Based KASAN !! 13 required for detection of out-of-bounds accesses to stack or global variables. 17 3. Hardware Tag-Based KASAN << 18 << 19 Generic KASAN, enabled with CONFIG_KASAN_GENER << 20 debugging, similar to userspace ASan. This mod << 21 architectures, but it has significant performa << 22 << 23 Software Tag-Based KASAN or SW_TAGS KASAN, ena << 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 14 41 Support !! 15 Currently KASAN is supported only for the x86_64 and arm64 architectures. 42 ------- << 43 16 44 Architectures !! 17 Usage 45 ~~~~~~~~~~~~~ !! 18 ----- 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 << 59 Generic KASAN requires GCC version 8.3.0 or la << 60 or any Clang version supported by the kernel. << 61 << 62 Software Tag-Based KASAN requires GCC 11+ << 63 or any Clang version supported by the kernel. << 64 << 65 Hardware Tag-Based KASAN requires GCC 10+ or C << 66 19 67 Memory types !! 20 To enable KASAN configure kernel with:: 68 ~~~~~~~~~~~~ << 69 21 70 Generic KASAN supports finding bugs in all of !! 22 CONFIG_KASAN = y 71 stack, and global memory. << 72 23 73 Software Tag-Based KASAN supports slab, page_a !! 24 and choose between CONFIG_KASAN_OUTLINE and CONFIG_KASAN_INLINE. Outline and >> 25 inline are compiler instrumentation types. The former produces smaller binary >> 26 the latter is 1.1 - 2 times faster. Inline instrumentation requires a GCC >> 27 version 5.0 or later. 74 28 75 Hardware Tag-Based KASAN supports slab, page_a !! 29 KASAN works with both SLUB and SLAB memory allocators. 76 memory. !! 30 For better bug detection and nicer reporting, enable CONFIG_STACKTRACE. 77 31 78 For slab, both software KASAN modes support SL !! 32 To disable instrumentation for specific files or directories, add a line 79 Hardware Tag-Based KASAN only supports SLUB. !! 33 similar to the following to the respective kernel Makefile: 80 34 81 Usage !! 35 - For a single file (e.g. main.o):: 82 ----- << 83 36 84 To enable KASAN, configure the kernel with:: !! 37 KASAN_SANITIZE_main.o := n 85 38 86 CONFIG_KASAN=y !! 39 - For all files in one directory:: 87 40 88 and choose between ``CONFIG_KASAN_GENERIC`` (t !! 41 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 42 166 Error reports 43 Error reports 167 ~~~~~~~~~~~~~ 44 ~~~~~~~~~~~~~ 168 45 169 A typical KASAN report looks like this:: !! 46 A typical out of bounds access report looks like this:: 170 47 171 ========================================== 48 ================================================================== 172 BUG: KASAN: slab-out-of-bounds in kmalloc_ !! 49 BUG: AddressSanitizer: out of bounds access in kmalloc_oob_right+0x65/0x75 [test_kasan] at addr ffff8800693bc5d3 173 Write of size 1 at addr ffff8801f44ec37b b !! 50 Write of size 1 by task modprobe/1689 174 !! 51 ============================================================================= 175 CPU: 1 PID: 2760 Comm: insmod Not tainted !! 52 BUG kmalloc-128 (Not tainted): kasan error 176 Hardware name: QEMU Standard PC (i440FX + !! 53 ----------------------------------------------------------------------------- >> 54 >> 55 Disabling lock debugging due to kernel taint >> 56 INFO: Allocated in kmalloc_oob_right+0x3d/0x75 [test_kasan] age=0 cpu=0 pid=1689 >> 57 __slab_alloc+0x4b4/0x4f0 >> 58 kmem_cache_alloc_trace+0x10b/0x190 >> 59 kmalloc_oob_right+0x3d/0x75 [test_kasan] >> 60 init_module+0x9/0x47 [test_kasan] >> 61 do_one_initcall+0x99/0x200 >> 62 load_module+0x2cb3/0x3b20 >> 63 SyS_finit_module+0x76/0x80 >> 64 system_call_fastpath+0x12/0x17 >> 65 INFO: Slab 0xffffea0001a4ef00 objects=17 used=7 fp=0xffff8800693bd728 flags=0x100000000004080 >> 66 INFO: Object 0xffff8800693bc558 @offset=1368 fp=0xffff8800693bc720 >> 67 >> 68 Bytes b4 ffff8800693bc548: 00 00 00 00 00 00 00 00 5a 5a 5a 5a 5a 5a 5a 5a ........ZZZZZZZZ >> 69 Object ffff8800693bc558: 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b kkkkkkkkkkkkkkkk >> 70 Object ffff8800693bc568: 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b kkkkkkkkkkkkkkkk >> 71 Object ffff8800693bc578: 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b kkkkkkkkkkkkkkkk >> 72 Object ffff8800693bc588: 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b kkkkkkkkkkkkkkkk >> 73 Object ffff8800693bc598: 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b kkkkkkkkkkkkkkkk >> 74 Object ffff8800693bc5a8: 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b kkkkkkkkkkkkkkkk >> 75 Object ffff8800693bc5b8: 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b kkkkkkkkkkkkkkkk >> 76 Object ffff8800693bc5c8: 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b a5 kkkkkkkkkkkkkkk. >> 77 Redzone ffff8800693bc5d8: cc cc cc cc cc cc cc cc ........ >> 78 Padding ffff8800693bc718: 5a 5a 5a 5a 5a 5a 5a 5a ZZZZZZZZ >> 79 CPU: 0 PID: 1689 Comm: modprobe Tainted: G B 3.18.0-rc1-mm1+ #98 >> 80 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.7.5-0-ge51488c-20140602_164612-nilsson.home.kraxel.org 04/01/2014 >> 81 ffff8800693bc000 0000000000000000 ffff8800693bc558 ffff88006923bb78 >> 82 ffffffff81cc68ae 00000000000000f3 ffff88006d407600 ffff88006923bba8 >> 83 ffffffff811fd848 ffff88006d407600 ffffea0001a4ef00 ffff8800693bc558 177 Call Trace: 84 Call Trace: 178 dump_stack+0x94/0xd8 !! 85 [<ffffffff81cc68ae>] dump_stack+0x46/0x58 179 print_address_description+0x73/0x280 !! 86 [<ffffffff811fd848>] print_trailer+0xf8/0x160 180 kasan_report+0x144/0x187 !! 87 [<ffffffffa00026a7>] ? kmem_cache_oob+0xc3/0xc3 [test_kasan] 181 __asan_report_store1_noabort+0x17/0x20 !! 88 [<ffffffff811ff0f5>] object_err+0x35/0x40 182 kmalloc_oob_right+0xa8/0xbc [kasan_test] !! 89 [<ffffffffa0002065>] ? kmalloc_oob_right+0x65/0x75 [test_kasan] 183 kmalloc_tests_init+0x16/0x700 [kasan_test !! 90 [<ffffffff8120b9fa>] kasan_report_error+0x38a/0x3f0 184 do_one_initcall+0xa5/0x3ae !! 91 [<ffffffff8120a79f>] ? kasan_poison_shadow+0x2f/0x40 185 do_init_module+0x1b6/0x547 !! 92 [<ffffffff8120b344>] ? kasan_unpoison_shadow+0x14/0x40 186 load_module+0x75df/0x8070 !! 93 [<ffffffff8120a79f>] ? kasan_poison_shadow+0x2f/0x40 187 __do_sys_init_module+0x1c6/0x200 !! 94 [<ffffffffa00026a7>] ? kmem_cache_oob+0xc3/0xc3 [test_kasan] 188 __x64_sys_init_module+0x6e/0xb0 !! 95 [<ffffffff8120a995>] __asan_store1+0x75/0xb0 189 do_syscall_64+0x9f/0x2c0 !! 96 [<ffffffffa0002601>] ? kmem_cache_oob+0x1d/0xc3 [test_kasan] 190 entry_SYSCALL_64_after_hwframe+0x44/0xa9 !! 97 [<ffffffffa0002065>] ? kmalloc_oob_right+0x65/0x75 [test_kasan] 191 RIP: 0033:0x7f96443109da !! 98 [<ffffffffa0002065>] kmalloc_oob_right+0x65/0x75 [test_kasan] 192 RSP: 002b:00007ffcf0b51b08 EFLAGS: 0000020 !! 99 [<ffffffffa00026b0>] init_module+0x9/0x47 [test_kasan] 193 RAX: ffffffffffffffda RBX: 000055dc3ee521a !! 100 [<ffffffff810002d9>] do_one_initcall+0x99/0x200 194 RDX: 00007f96445cff88 RSI: 0000000000057a5 !! 101 [<ffffffff811e4e5c>] ? __vunmap+0xec/0x160 195 RBP: 000055dc3ee510b0 R08: 000000000000000 !! 102 [<ffffffff81114f63>] load_module+0x2cb3/0x3b20 196 R10: 00007f964430cd0a R11: 000000000000020 !! 103 [<ffffffff8110fd70>] ? m_show+0x240/0x240 197 R13: 000055dc3ee51090 R14: 000000000000000 !! 104 [<ffffffff81115f06>] SyS_finit_module+0x76/0x80 198 !! 105 [<ffffffff81cd3129>] system_call_fastpath+0x12/0x17 199 Allocated by task 2760: << 200 save_stack+0x43/0xd0 << 201 kasan_kmalloc+0xa7/0xd0 << 202 kmem_cache_alloc_trace+0xe1/0x1b0 << 203 kmalloc_oob_right+0x56/0xbc [kasan_test] << 204 kmalloc_tests_init+0x16/0x700 [kasan_test << 205 do_one_initcall+0xa5/0x3ae << 206 do_init_module+0x1b6/0x547 << 207 load_module+0x75df/0x8070 << 208 __do_sys_init_module+0x1c6/0x200 << 209 __x64_sys_init_module+0x6e/0xb0 << 210 do_syscall_64+0x9f/0x2c0 << 211 entry_SYSCALL_64_after_hwframe+0x44/0xa9 << 212 << 213 Freed by task 815: << 214 save_stack+0x43/0xd0 << 215 __kasan_slab_free+0x135/0x190 << 216 kasan_slab_free+0xe/0x10 << 217 kfree+0x93/0x1a0 << 218 umh_complete+0x6a/0xa0 << 219 call_usermodehelper_exec_async+0x4c3/0x64 << 220 ret_from_fork+0x35/0x40 << 221 << 222 The buggy address belongs to the object at << 223 which belongs to the cache kmalloc-128 of << 224 The buggy address is located 123 bytes ins << 225 128-byte region [ffff8801f44ec300, ffff88 << 226 The buggy address belongs to the page: << 227 page:ffffea0007d13b00 count:1 mapcount:0 m << 228 flags: 0x200000000000100(slab) << 229 raw: 0200000000000100 ffffea0007d11dc0 000 << 230 raw: 0000000000000000 0000000080150015 000 << 231 page dumped because: kasan: bad access det << 232 << 233 Memory state around the buggy address: 106 Memory state around the buggy address: 234 ffff8801f44ec200: fc fc fc fc fc fc fc fc !! 107 ffff8800693bc300: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc 235 ffff8801f44ec280: fb fb fb fb fb fb fb fb !! 108 ffff8800693bc380: fc fc 00 00 00 00 00 00 00 00 00 00 00 00 00 fc 236 >ffff8801f44ec300: 00 00 00 00 00 00 00 00 !! 109 ffff8800693bc400: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc 237 !! 110 ffff8800693bc480: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc 238 ffff8801f44ec380: fc fc fc fc fc fc fc fc !! 111 ffff8800693bc500: fc fc fc fc fc fc fc fc fc fc fc 00 00 00 00 00 239 ffff8801f44ec400: fb fb fb fb fb fb fb fb !! 112 >ffff8800693bc580: 00 00 00 00 00 00 00 00 00 00 03 fc fc fc fc fc >> 113 ^ >> 114 ffff8800693bc600: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc >> 115 ffff8800693bc680: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc >> 116 ffff8800693bc700: fc fc fc fc fb fb fb fb fb fb fb fb fb fb fb fb >> 117 ffff8800693bc780: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb >> 118 ffff8800693bc800: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb 240 ========================================== 119 ================================================================== 241 120 242 The report header summarizes what kind of bug !! 121 The header of the report discribe what kind of bug happened and what kind of 243 caused it. It is followed by a stack trace of !! 122 access caused it. It's followed by the description of the accessed slub object 244 where the accessed memory was allocated (in ca !! 123 (see 'SLUB Debug output' section in Documentation/vm/slub.rst for details) and 245 and a stack trace of where the object was free !! 124 the description of the accessed memory page. 246 bug report). Next comes a description of the a !! 125 247 information about the accessed memory page. !! 126 In the last section the report shows memory state around the accessed address. 248 !! 127 Reading this part requires some understanding of how KASAN works. 249 In the end, the report shows the memory state !! 128 250 Internally, KASAN tracks memory state separate !! 129 The state of each 8 aligned bytes of memory is encoded in one shadow byte. 251 is either 8 or 16 aligned bytes depending on K !! 130 Those 8 bytes can be accessible, partially accessible, freed or be a redzone. 252 memory state section of the report shows the s !! 131 We use the following encoding for each shadow byte: 0 means that all 8 bytes 253 granules that surround the accessed address. !! 132 of the corresponding memory region are accessible; number N (1 <= N <= 7) means 254 !! 133 that the first N bytes are accessible, and other (8 - N) bytes are not; 255 For Generic KASAN, the size of each memory gra !! 134 any negative value indicates that the entire 8-byte word is inaccessible. 256 granule is encoded in one shadow byte. Those 8 !! 135 We use different negative values to distinguish between different kinds of 257 partially accessible, freed, or be a part of a !! 136 inaccessible memory like redzones or freed memory (see mm/kasan/kasan.h). 258 encoding for each shadow byte: 00 means that a << 259 memory region are accessible; number N (1 <= N << 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 137 290 Here is the report with CONFIG_KASAN_EXTRA_INF !! 138 In the report above the arrows point to the shadow byte 03, which means that 291 different parts are shown):: !! 139 the accessed address is partially accessible. 292 140 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 141 301 Implementation details 142 Implementation details 302 ---------------------- 143 ---------------------- 303 144 304 Generic KASAN !! 145 From a high level, our approach to memory error detection is similar to that 305 ~~~~~~~~~~~~~ !! 146 of kmemcheck: use shadow memory to record whether each byte of memory is safe 306 !! 147 to access, and use compile-time instrumentation to check shadow memory on each 307 Software KASAN modes use shadow memory to reco !! 148 memory access. 308 safe to access and use compile-time instrument !! 149 309 checks before each memory access. !! 150 AddressSanitizer dedicates 1/8 of kernel memory to its shadow memory 310 !! 151 (e.g. 16TB to cover 128TB on x86_64) and uses direct mapping with a scale and 311 Generic KASAN dedicates 1/8th of kernel memory !! 152 offset to translate a memory address to its corresponding shadow address. 312 to cover 128TB on x86_64) and uses direct mapp << 313 translate a memory address to its correspondin << 314 153 315 Here is the function which translates an addre 154 Here is the function which translates an address to its corresponding shadow 316 address:: 155 address:: 317 156 318 static inline void *kasan_mem_to_shadow(co 157 static inline void *kasan_mem_to_shadow(const void *addr) 319 { 158 { 320 return (void *)((unsigned long)addr >> !! 159 return ((unsigned long)addr >> KASAN_SHADOW_SCALE_SHIFT) 321 + KASAN_SHADOW_OFFSET; 160 + KASAN_SHADOW_OFFSET; 322 } 161 } 323 162 324 where ``KASAN_SHADOW_SCALE_SHIFT = 3``. 163 where ``KASAN_SHADOW_SCALE_SHIFT = 3``. 325 164 326 Compile-time instrumentation is used to insert !! 165 Compile-time instrumentation used for checking memory accesses. Compiler inserts 327 inserts function calls (``__asan_load*(addr)`` !! 166 function calls (__asan_load*(addr), __asan_store*(addr)) before each memory 328 each memory access of size 1, 2, 4, 8, or 16. !! 167 access of size 1, 2, 4, 8 or 16. These functions check whether memory access is 329 memory accesses are valid or not by checking c !! 168 valid or not by checking corresponding shadow memory. 330 !! 169 331 With inline instrumentation, instead of making !! 170 GCC 5.0 has possibility to perform inline instrumentation. Instead of making 332 directly inserts the code to check shadow memo !! 171 function calls GCC directly inserts the code to check the shadow memory. 333 enlarges the kernel, but it gives an x1.1-x2 p !! 172 This option significantly enlarges kernel but it gives x1.1-x2 performance 334 outline-instrumented kernel. !! 173 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 << 339 Software Tag-Based KASAN << 340 ~~~~~~~~~~~~~~~~~~~~~~~~ << 341 << 342 Software Tag-Based KASAN uses a software memor << 343 access validity. It is currently only implemen << 344 << 345 Software Tag-Based KASAN uses the Top Byte Ign << 346 to store a pointer tag in the top byte of kern << 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 << 363 printed from the function that performs the ac << 364 instrumentation, a ``brk`` instruction is emit << 365 dedicated ``brk`` handler is used to print bug << 366 << 367 Software Tag-Based KASAN uses 0xFF as a match- << 368 pointers with the 0xFF pointer tag are not che << 369 reserved to tag freed memory regions. << 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.