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 29 62 Software Tag-Based KASAN requires GCC 11+ !! 30 To enable KASAN configure kernel with:: 63 or any Clang version supported by the kernel. << 64 31 65 Hardware Tag-Based KASAN requires GCC 10+ or C !! 32 CONFIG_KASAN = y 66 33 67 Memory types !! 34 and choose between CONFIG_KASAN_GENERIC (to enable generic KASAN) and 68 ~~~~~~~~~~~~ !! 35 CONFIG_KASAN_SW_TAGS (to enable software tag-based KASAN). 69 36 70 Generic KASAN supports finding bugs in all of !! 37 You also need to choose between CONFIG_KASAN_OUTLINE and CONFIG_KASAN_INLINE. 71 stack, and global memory. !! 38 Outline and inline are compiler instrumentation types. The former produces >> 39 smaller binary while the latter is 1.1 - 2 times faster. 72 40 73 Software Tag-Based KASAN supports slab, page_a !! 41 Both KASAN modes work with both SLUB and SLAB memory allocators. >> 42 For better bug detection and nicer reporting, enable CONFIG_STACKTRACE. 74 43 75 Hardware Tag-Based KASAN supports slab, page_a !! 44 To augment reports with last allocation and freeing stack of the physical page, 76 memory. !! 45 it is recommended to enable also CONFIG_PAGE_OWNER and boot with page_owner=on. 77 46 78 For slab, both software KASAN modes support SL !! 47 To disable instrumentation for specific files or directories, add a line 79 Hardware Tag-Based KASAN only supports SLUB. !! 48 similar to the following to the respective kernel Makefile: 80 49 81 Usage !! 50 - For a single file (e.g. main.o):: 82 ----- << 83 51 84 To enable KASAN, configure the kernel with:: !! 52 KASAN_SANITIZE_main.o := n 85 53 86 CONFIG_KASAN=y !! 54 - For all files in one directory:: 87 55 88 and choose between ``CONFIG_KASAN_GENERIC`` (t !! 56 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 57 166 Error reports 58 Error reports 167 ~~~~~~~~~~~~~ 59 ~~~~~~~~~~~~~ 168 60 169 A typical KASAN report looks like this:: !! 61 A typical out-of-bounds access generic KASAN report looks like this:: 170 62 171 ========================================== 63 ================================================================== 172 BUG: KASAN: slab-out-of-bounds in kmalloc_ !! 64 BUG: KASAN: slab-out-of-bounds in kmalloc_oob_right+0xa8/0xbc [test_kasan] 173 Write of size 1 at addr ffff8801f44ec37b b 65 Write of size 1 at addr ffff8801f44ec37b by task insmod/2760 174 66 175 CPU: 1 PID: 2760 Comm: insmod Not tainted 67 CPU: 1 PID: 2760 Comm: insmod Not tainted 4.19.0-rc3+ #698 176 Hardware name: QEMU Standard PC (i440FX + 68 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.10.2-1 04/01/2014 177 Call Trace: 69 Call Trace: 178 dump_stack+0x94/0xd8 70 dump_stack+0x94/0xd8 179 print_address_description+0x73/0x280 71 print_address_description+0x73/0x280 180 kasan_report+0x144/0x187 72 kasan_report+0x144/0x187 181 __asan_report_store1_noabort+0x17/0x20 73 __asan_report_store1_noabort+0x17/0x20 182 kmalloc_oob_right+0xa8/0xbc [kasan_test] !! 74 kmalloc_oob_right+0xa8/0xbc [test_kasan] 183 kmalloc_tests_init+0x16/0x700 [kasan_test !! 75 kmalloc_tests_init+0x16/0x700 [test_kasan] 184 do_one_initcall+0xa5/0x3ae 76 do_one_initcall+0xa5/0x3ae 185 do_init_module+0x1b6/0x547 77 do_init_module+0x1b6/0x547 186 load_module+0x75df/0x8070 78 load_module+0x75df/0x8070 187 __do_sys_init_module+0x1c6/0x200 79 __do_sys_init_module+0x1c6/0x200 188 __x64_sys_init_module+0x6e/0xb0 80 __x64_sys_init_module+0x6e/0xb0 189 do_syscall_64+0x9f/0x2c0 81 do_syscall_64+0x9f/0x2c0 190 entry_SYSCALL_64_after_hwframe+0x44/0xa9 82 entry_SYSCALL_64_after_hwframe+0x44/0xa9 191 RIP: 0033:0x7f96443109da 83 RIP: 0033:0x7f96443109da 192 RSP: 002b:00007ffcf0b51b08 EFLAGS: 0000020 84 RSP: 002b:00007ffcf0b51b08 EFLAGS: 00000202 ORIG_RAX: 00000000000000af 193 RAX: ffffffffffffffda RBX: 000055dc3ee521a 85 RAX: ffffffffffffffda RBX: 000055dc3ee521a0 RCX: 00007f96443109da 194 RDX: 00007f96445cff88 RSI: 0000000000057a5 86 RDX: 00007f96445cff88 RSI: 0000000000057a50 RDI: 00007f9644992000 195 RBP: 000055dc3ee510b0 R08: 000000000000000 87 RBP: 000055dc3ee510b0 R08: 0000000000000003 R09: 0000000000000000 196 R10: 00007f964430cd0a R11: 000000000000020 88 R10: 00007f964430cd0a R11: 0000000000000202 R12: 00007f96445cff88 197 R13: 000055dc3ee51090 R14: 000000000000000 89 R13: 000055dc3ee51090 R14: 0000000000000000 R15: 0000000000000000 198 90 199 Allocated by task 2760: 91 Allocated by task 2760: 200 save_stack+0x43/0xd0 92 save_stack+0x43/0xd0 201 kasan_kmalloc+0xa7/0xd0 93 kasan_kmalloc+0xa7/0xd0 202 kmem_cache_alloc_trace+0xe1/0x1b0 94 kmem_cache_alloc_trace+0xe1/0x1b0 203 kmalloc_oob_right+0x56/0xbc [kasan_test] !! 95 kmalloc_oob_right+0x56/0xbc [test_kasan] 204 kmalloc_tests_init+0x16/0x700 [kasan_test !! 96 kmalloc_tests_init+0x16/0x700 [test_kasan] 205 do_one_initcall+0xa5/0x3ae 97 do_one_initcall+0xa5/0x3ae 206 do_init_module+0x1b6/0x547 98 do_init_module+0x1b6/0x547 207 load_module+0x75df/0x8070 99 load_module+0x75df/0x8070 208 __do_sys_init_module+0x1c6/0x200 100 __do_sys_init_module+0x1c6/0x200 209 __x64_sys_init_module+0x6e/0xb0 101 __x64_sys_init_module+0x6e/0xb0 210 do_syscall_64+0x9f/0x2c0 102 do_syscall_64+0x9f/0x2c0 211 entry_SYSCALL_64_after_hwframe+0x44/0xa9 103 entry_SYSCALL_64_after_hwframe+0x44/0xa9 212 104 213 Freed by task 815: 105 Freed by task 815: 214 save_stack+0x43/0xd0 106 save_stack+0x43/0xd0 215 __kasan_slab_free+0x135/0x190 107 __kasan_slab_free+0x135/0x190 216 kasan_slab_free+0xe/0x10 108 kasan_slab_free+0xe/0x10 217 kfree+0x93/0x1a0 109 kfree+0x93/0x1a0 218 umh_complete+0x6a/0xa0 110 umh_complete+0x6a/0xa0 219 call_usermodehelper_exec_async+0x4c3/0x64 111 call_usermodehelper_exec_async+0x4c3/0x640 220 ret_from_fork+0x35/0x40 112 ret_from_fork+0x35/0x40 221 113 222 The buggy address belongs to the object at 114 The buggy address belongs to the object at ffff8801f44ec300 223 which belongs to the cache kmalloc-128 of 115 which belongs to the cache kmalloc-128 of size 128 224 The buggy address is located 123 bytes ins 116 The buggy address is located 123 bytes inside of 225 128-byte region [ffff8801f44ec300, ffff88 117 128-byte region [ffff8801f44ec300, ffff8801f44ec380) 226 The buggy address belongs to the page: 118 The buggy address belongs to the page: 227 page:ffffea0007d13b00 count:1 mapcount:0 m 119 page:ffffea0007d13b00 count:1 mapcount:0 mapping:ffff8801f7001640 index:0x0 228 flags: 0x200000000000100(slab) 120 flags: 0x200000000000100(slab) 229 raw: 0200000000000100 ffffea0007d11dc0 000 121 raw: 0200000000000100 ffffea0007d11dc0 0000001a0000001a ffff8801f7001640 230 raw: 0000000000000000 0000000080150015 000 122 raw: 0000000000000000 0000000080150015 00000001ffffffff 0000000000000000 231 page dumped because: kasan: bad access det 123 page dumped because: kasan: bad access detected 232 124 233 Memory state around the buggy address: 125 Memory state around the buggy address: 234 ffff8801f44ec200: fc fc fc fc fc fc fc fc 126 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 127 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 128 >ffff8801f44ec300: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 03 237 129 ^ 238 ffff8801f44ec380: fc fc fc fc fc fc fc fc 130 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 131 ffff8801f44ec400: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc 240 ========================================== 132 ================================================================== 241 133 242 The report header summarizes what kind of bug !! 134 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 !! 135 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 !! 136 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 !! 137 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 !! 138 freed (in case of a use-after-free bug report). Next comes a description of 247 information about the accessed memory page. !! 139 the accessed slab object and information about the accessed memory page. 248 !! 140 249 In the end, the report shows the memory state !! 141 In the last section the report shows memory state around the accessed address. 250 Internally, KASAN tracks memory state separate !! 142 Reading this part requires some understanding of how KASAN works. 251 is either 8 or 16 aligned bytes depending on K !! 143 252 memory state section of the report shows the s !! 144 The state of each 8 aligned bytes of memory is encoded in one shadow byte. 253 granules that surround the accessed address. !! 145 Those 8 bytes can be accessible, partially accessible, freed or be a redzone. 254 !! 146 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 !! 147 of the corresponding memory region are accessible; number N (1 <= N <= 7) means 256 granule is encoded in one shadow byte. Those 8 !! 148 that the first N bytes are accessible, and other (8 - N) bytes are not; 257 partially accessible, freed, or be a part of a !! 149 any negative value indicates that the entire 8-byte word is inaccessible. 258 encoding for each shadow byte: 00 means that a !! 150 We use different negative values to distinguish between different kinds of 259 memory region are accessible; number N (1 <= N !! 151 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 152 290 Here is the report with CONFIG_KASAN_EXTRA_INF !! 153 In the report above the arrows point to the shadow byte 03, which means that 291 different parts are shown):: !! 154 the accessed address is partially accessible. >> 155 >> 156 For tag-based KASAN this last report section shows the memory tags around the >> 157 accessed address (see Implementation details section). 292 158 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 159 301 Implementation details 160 Implementation details 302 ---------------------- 161 ---------------------- 303 162 304 Generic KASAN 163 Generic KASAN 305 ~~~~~~~~~~~~~ 164 ~~~~~~~~~~~~~ 306 165 307 Software KASAN modes use shadow memory to reco !! 166 From a high level, our approach to memory error detection is similar to that 308 safe to access and use compile-time instrument !! 167 of kmemcheck: use shadow memory to record whether each byte of memory is safe 309 checks before each memory access. !! 168 to access, and use compile-time instrumentation to insert checks of shadow >> 169 memory on each memory access. 310 170 311 Generic KASAN dedicates 1/8th of kernel memory !! 171 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 172 to cover 128TB on x86_64) and uses direct mapping with a scale and offset to 313 translate a memory address to its correspondin 173 translate a memory address to its corresponding shadow address. 314 174 315 Here is the function which translates an addre 175 Here is the function which translates an address to its corresponding shadow 316 address:: 176 address:: 317 177 318 static inline void *kasan_mem_to_shadow(co 178 static inline void *kasan_mem_to_shadow(const void *addr) 319 { 179 { 320 return (void *)((unsigned long)addr >> !! 180 return ((unsigned long)addr >> KASAN_SHADOW_SCALE_SHIFT) 321 + KASAN_SHADOW_OFFSET; 181 + KASAN_SHADOW_OFFSET; 322 } 182 } 323 183 324 where ``KASAN_SHADOW_SCALE_SHIFT = 3``. 184 where ``KASAN_SHADOW_SCALE_SHIFT = 3``. 325 185 326 Compile-time instrumentation is used to insert 186 Compile-time instrumentation is used to insert memory access checks. Compiler 327 inserts function calls (``__asan_load*(addr)`` !! 187 inserts function calls (__asan_load*(addr), __asan_store*(addr)) before each 328 each memory access of size 1, 2, 4, 8, or 16. !! 188 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 !! 189 access is valid or not by checking corresponding shadow memory. 330 !! 190 331 With inline instrumentation, instead of making !! 191 GCC 5.0 has possibility to perform inline instrumentation. Instead of making 332 directly inserts the code to check shadow memo !! 192 function calls GCC directly inserts the code to check the shadow memory. 333 enlarges the kernel, but it gives an x1.1-x2 p !! 193 This option significantly enlarges kernel but it gives x1.1-x2 performance 334 outline-instrumented kernel. !! 194 boost over outline instrumented kernel. 335 195 336 Generic KASAN is the only mode that delays the !! 196 Software tag-based KASAN 337 quarantine (see mm/kasan/quarantine.c for impl << 338 << 339 Software Tag-Based KASAN << 340 ~~~~~~~~~~~~~~~~~~~~~~~~ 197 ~~~~~~~~~~~~~~~~~~~~~~~~ 341 198 342 Software Tag-Based KASAN uses a software memor !! 199 Tag-based KASAN uses the Top Byte Ignore (TBI) feature of modern arm64 CPUs to 343 access validity. It is currently only implemen !! 200 store a pointer tag in the top byte of kernel pointers. Like generic KASAN it 344 !! 201 uses shadow memory to store memory tags associated with each 16-byte memory 345 Software Tag-Based KASAN uses the Top Byte Ign !! 202 cell (therefore it dedicates 1/16th of the kernel memory for shadow memory). 346 to store a pointer tag in the top byte of kern !! 203 347 to store memory tags associated with each 16-b !! 204 On each memory allocation tag-based KASAN generates a random tag, tags the 348 dedicates 1/16th of the kernel memory for shad !! 205 allocated memory with this tag, and embeds this tag into the returned pointer. 349 !! 206 Software tag-based KASAN uses compile-time instrumentation to insert checks 350 On each memory allocation, Software Tag-Based !! 207 before each memory access. These checks make sure that tag of the memory that 351 the allocated memory with this tag, and embeds !! 208 is being accessed is equal to tag of the pointer that is used to access this 352 pointer. !! 209 memory. In case of a tag mismatch tag-based KASAN prints a bug report. 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 210 360 Software Tag-Based KASAN also has two instrume !! 211 Software tag-based KASAN also has two instrumentation modes (outline, that 361 emits callbacks to check memory accesses; and !! 212 emits callbacks to check memory accesses; and inline, that performs the shadow 362 memory checks inline). With outline instrument 213 memory checks inline). With outline instrumentation mode, a bug report is 363 printed from the function that performs the ac !! 214 simply printed from the function that performs the access check. With inline 364 instrumentation, a ``brk`` instruction is emit !! 215 instrumentation a brk instruction is emitted by the compiler, and a dedicated 365 dedicated ``brk`` handler is used to print bug !! 216 brk handler is used to print bug reports. 366 !! 217 367 Software Tag-Based KASAN uses 0xFF as a match- !! 218 A potential expansion of this mode is a hardware tag-based mode, which would 368 pointers with the 0xFF pointer tag are not che !! 219 use hardware memory tagging support instead of compiler instrumentation and 369 reserved to tag freed memory regions. !! 220 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.