12757aafaSJonathan CorbetThe Kernel Address Sanitizer (KASAN) 22757aafaSJonathan Corbet==================================== 32757aafaSJonathan Corbet 42757aafaSJonathan CorbetOverview 52757aafaSJonathan Corbet-------- 62757aafaSJonathan Corbet 7c2ec0c8fSAndrey KonovalovKernel Address Sanitizer (KASAN) is a dynamic memory safety error detector 8c2ec0c8fSAndrey Konovalovdesigned to find out-of-bounds and use-after-free bugs. 92757aafaSJonathan Corbet 10c2ec0c8fSAndrey KonovalovKASAN has three modes: 112757aafaSJonathan Corbet 12c2ec0c8fSAndrey Konovalov1. Generic KASAN 13c2ec0c8fSAndrey Konovalov2. Software Tag-Based KASAN 14c2ec0c8fSAndrey Konovalov3. Hardware Tag-Based KASAN 153cbc37dcSAndrey Konovalov 16c2ec0c8fSAndrey KonovalovGeneric KASAN, enabled with CONFIG_KASAN_GENERIC, is the mode intended for 17c2ec0c8fSAndrey Konovalovdebugging, similar to userspace ASan. This mode is supported on many CPU 18c2ec0c8fSAndrey Konovalovarchitectures, but it has significant performance and memory overheads. 192757aafaSJonathan Corbet 20c2ec0c8fSAndrey KonovalovSoftware Tag-Based KASAN or SW_TAGS KASAN, enabled with CONFIG_KASAN_SW_TAGS, 21c2ec0c8fSAndrey Konovalovcan be used for both debugging and dogfood testing, similar to userspace HWASan. 22c2ec0c8fSAndrey KonovalovThis mode is only supported for arm64, but its moderate memory overhead allows 23c2ec0c8fSAndrey Konovalovusing it for testing on memory-restricted devices with real workloads. 24b3b0e6acSAndrey Konovalov 25c2ec0c8fSAndrey KonovalovHardware Tag-Based KASAN or HW_TAGS KASAN, enabled with CONFIG_KASAN_HW_TAGS, 26c2ec0c8fSAndrey Konovalovis the mode intended to be used as an in-field memory bug detector or as a 27c2ec0c8fSAndrey Konovalovsecurity mitigation. This mode only works on arm64 CPUs that support MTE 28c2ec0c8fSAndrey Konovalov(Memory Tagging Extension), but it has low memory and performance overheads and 29c2ec0c8fSAndrey Konovalovthus can be used in production. 30b3b0e6acSAndrey Konovalov 31c2ec0c8fSAndrey KonovalovFor details about the memory and performance impact of each KASAN mode, see the 32c2ec0c8fSAndrey Konovalovdescriptions of the corresponding Kconfig options. 333cbc37dcSAndrey Konovalov 34c2ec0c8fSAndrey KonovalovThe Generic and the Software Tag-Based modes are commonly referred to as the 35c2ec0c8fSAndrey Konovalovsoftware modes. The Software Tag-Based and the Hardware Tag-Based modes are 36c2ec0c8fSAndrey Konovalovreferred to as the tag-based modes. 373cbc37dcSAndrey Konovalov 38c2ec0c8fSAndrey KonovalovSupport 39c2ec0c8fSAndrey Konovalov------- 40c2ec0c8fSAndrey Konovalov 41c2ec0c8fSAndrey KonovalovArchitectures 42c2ec0c8fSAndrey Konovalov~~~~~~~~~~~~~ 43c2ec0c8fSAndrey Konovalov 44*5aa4ac64SQing ZhangGeneric KASAN is supported on x86_64, arm, arm64, powerpc, riscv, s390, xtensa, 45*5aa4ac64SQing Zhangand loongarch, and the tag-based KASAN modes are supported only on arm64. 46c2ec0c8fSAndrey Konovalov 47c2ec0c8fSAndrey KonovalovCompilers 48c2ec0c8fSAndrey Konovalov~~~~~~~~~ 49c2ec0c8fSAndrey Konovalov 50c2ec0c8fSAndrey KonovalovSoftware KASAN modes use compile-time instrumentation to insert validity checks 51c2ec0c8fSAndrey Konovalovbefore every memory access and thus require a compiler version that provides 52c2ec0c8fSAndrey Konovalovsupport for that. The Hardware Tag-Based mode relies on hardware to perform 53c2ec0c8fSAndrey Konovalovthese checks but still requires a compiler version that supports the memory 54c2ec0c8fSAndrey Konovalovtagging instructions. 55c2ec0c8fSAndrey Konovalov 56c2ec0c8fSAndrey KonovalovGeneric KASAN requires GCC version 8.3.0 or later 57c2ec0c8fSAndrey Konovalovor any Clang version supported by the kernel. 58c2ec0c8fSAndrey Konovalov 59c2ec0c8fSAndrey KonovalovSoftware Tag-Based KASAN requires GCC 11+ 60c2ec0c8fSAndrey Konovalovor any Clang version supported by the kernel. 61c2ec0c8fSAndrey Konovalov 62c2ec0c8fSAndrey KonovalovHardware Tag-Based KASAN requires GCC 10+ or Clang 12+. 63c2ec0c8fSAndrey Konovalov 64c2ec0c8fSAndrey KonovalovMemory types 65c2ec0c8fSAndrey Konovalov~~~~~~~~~~~~ 66c2ec0c8fSAndrey Konovalov 67c2ec0c8fSAndrey KonovalovGeneric KASAN supports finding bugs in all of slab, page_alloc, vmap, vmalloc, 68c2ec0c8fSAndrey Konovalovstack, and global memory. 69c2ec0c8fSAndrey Konovalov 70c2ec0c8fSAndrey KonovalovSoftware Tag-Based KASAN supports slab, page_alloc, vmalloc, and stack memory. 71c2ec0c8fSAndrey Konovalov 72c2ec0c8fSAndrey KonovalovHardware Tag-Based KASAN supports slab, page_alloc, and non-executable vmalloc 73c2ec0c8fSAndrey Konovalovmemory. 74c2ec0c8fSAndrey Konovalov 75c2ec0c8fSAndrey KonovalovFor slab, both software KASAN modes support SLUB and SLAB allocators, while 76c2ec0c8fSAndrey KonovalovHardware Tag-Based KASAN only supports SLUB. 772757aafaSJonathan Corbet 782757aafaSJonathan CorbetUsage 792757aafaSJonathan Corbet----- 802757aafaSJonathan Corbet 8186e6f08dSAndrey KonovalovTo enable KASAN, configure the kernel with:: 822757aafaSJonathan Corbet 832757aafaSJonathan Corbet CONFIG_KASAN=y 842757aafaSJonathan Corbet 85c2ec0c8fSAndrey Konovalovand choose between ``CONFIG_KASAN_GENERIC`` (to enable Generic KASAN), 86c2ec0c8fSAndrey Konovalov``CONFIG_KASAN_SW_TAGS`` (to enable Software Tag-Based KASAN), and 87c2ec0c8fSAndrey Konovalov``CONFIG_KASAN_HW_TAGS`` (to enable Hardware Tag-Based KASAN). 882757aafaSJonathan Corbet 89c2ec0c8fSAndrey KonovalovFor the software modes, also choose between ``CONFIG_KASAN_OUTLINE`` and 9086e6f08dSAndrey Konovalov``CONFIG_KASAN_INLINE``. Outline and inline are compiler instrumentation types. 91c2ec0c8fSAndrey KonovalovThe former produces a smaller binary while the latter is up to 2 times faster. 92b3b0e6acSAndrey Konovalov 9386e6f08dSAndrey KonovalovTo include alloc and free stack traces of affected slab objects into reports, 9486e6f08dSAndrey Konovalovenable ``CONFIG_STACKTRACE``. To include alloc and free stack traces of affected 9586e6f08dSAndrey Konovalovphysical pages, enable ``CONFIG_PAGE_OWNER`` and boot with ``page_owner=on``. 960fe9a448SVlastimil Babka 97ca89f2a2SAndrey KonovalovBoot parameters 98ca89f2a2SAndrey Konovalov~~~~~~~~~~~~~~~ 99ca89f2a2SAndrey Konovalov 100ca89f2a2SAndrey KonovalovKASAN is affected by the generic ``panic_on_warn`` command line parameter. 101ca89f2a2SAndrey KonovalovWhen it is enabled, KASAN panics the kernel after printing a bug report. 102ca89f2a2SAndrey Konovalov 103ca89f2a2SAndrey KonovalovBy default, KASAN prints a bug report only for the first invalid memory access. 104ca89f2a2SAndrey KonovalovWith ``kasan_multi_shot``, KASAN prints a report on every invalid access. This 105ca89f2a2SAndrey Konovaloveffectively disables ``panic_on_warn`` for KASAN reports. 106ca89f2a2SAndrey Konovalov 107ca89f2a2SAndrey KonovalovAlternatively, independent of ``panic_on_warn``, the ``kasan.fault=`` boot 108ca89f2a2SAndrey Konovalovparameter can be used to control panic and reporting behaviour: 109ca89f2a2SAndrey Konovalov 110452c03fdSMarco Elver- ``kasan.fault=report``, ``=panic``, or ``=panic_on_write`` controls whether 111452c03fdSMarco Elver to only print a KASAN report, panic the kernel, or panic the kernel on 112452c03fdSMarco Elver invalid writes only (default: ``report``). The panic happens even if 1138c293a63SMarco Elver ``kasan_multi_shot`` is enabled. Note that when using asynchronous mode of 1148c293a63SMarco Elver Hardware Tag-Based KASAN, ``kasan.fault=panic_on_write`` always panics on 1158c293a63SMarco Elver asynchronously checked accesses (including reads). 116ca89f2a2SAndrey Konovalov 1177ebfce33SAndrey KonovalovSoftware and Hardware Tag-Based KASAN modes (see the section about various 11880b92bfeSAndrey Konovalovmodes below) support altering stack trace collection behavior: 1197ebfce33SAndrey Konovalov 1207ebfce33SAndrey Konovalov- ``kasan.stacktrace=off`` or ``=on`` disables or enables alloc and free stack 1217ebfce33SAndrey Konovalov traces collection (default: ``on``). 12280b92bfeSAndrey Konovalov- ``kasan.stack_ring_size=<number of entries>`` specifies the number of entries 12380b92bfeSAndrey Konovalov in the stack ring (default: ``32768``). 1247ebfce33SAndrey Konovalov 1257ebfce33SAndrey KonovalovHardware Tag-Based KASAN mode is intended for use in production as a security 1267ebfce33SAndrey Konovalovmitigation. Therefore, it supports additional boot parameters that allow 1277ebfce33SAndrey Konovalovdisabling KASAN altogether or controlling its features: 128ca89f2a2SAndrey Konovalov 129ca89f2a2SAndrey Konovalov- ``kasan=off`` or ``=on`` controls whether KASAN is enabled (default: ``on``). 130ca89f2a2SAndrey Konovalov 131ca89f2a2SAndrey Konovalov- ``kasan.mode=sync``, ``=async`` or ``=asymm`` controls whether KASAN 132ca89f2a2SAndrey Konovalov is configured in synchronous, asynchronous or asymmetric mode of 133ca89f2a2SAndrey Konovalov execution (default: ``sync``). 134ca89f2a2SAndrey Konovalov Synchronous mode: a bad access is detected immediately when a tag 135ca89f2a2SAndrey Konovalov check fault occurs. 136ca89f2a2SAndrey Konovalov Asynchronous mode: a bad access detection is delayed. When a tag check 137ca89f2a2SAndrey Konovalov fault occurs, the information is stored in hardware (in the TFSR_EL1 138ca89f2a2SAndrey Konovalov register for arm64). The kernel periodically checks the hardware and 139ca89f2a2SAndrey Konovalov only reports tag faults during these checks. 140ca89f2a2SAndrey Konovalov Asymmetric mode: a bad access is detected synchronously on reads and 141ca89f2a2SAndrey Konovalov asynchronously on writes. 142ca89f2a2SAndrey Konovalov 143ca89f2a2SAndrey Konovalov- ``kasan.vmalloc=off`` or ``=on`` disables or enables tagging of vmalloc 144ca89f2a2SAndrey Konovalov allocations (default: ``on``). 145ca89f2a2SAndrey Konovalov 14644383cefSAndrey Konovalov- ``kasan.page_alloc.sample=<sampling interval>`` makes KASAN tag only every 14744383cefSAndrey Konovalov Nth page_alloc allocation with the order equal or greater than 14844383cefSAndrey Konovalov ``kasan.page_alloc.sample.order``, where N is the value of the ``sample`` 14944383cefSAndrey Konovalov parameter (default: ``1``, or tag every such allocation). 15044383cefSAndrey Konovalov This parameter is intended to mitigate the performance overhead introduced 15144383cefSAndrey Konovalov by KASAN. 15244383cefSAndrey Konovalov Note that enabling this parameter makes Hardware Tag-Based KASAN skip checks 15344383cefSAndrey Konovalov of allocations chosen by sampling and thus miss bad accesses to these 15444383cefSAndrey Konovalov allocations. Use the default value for accurate bug detection. 15544383cefSAndrey Konovalov 15644383cefSAndrey Konovalov- ``kasan.page_alloc.sample.order=<minimum page order>`` specifies the minimum 15744383cefSAndrey Konovalov order of allocations that are affected by sampling (default: ``3``). 15844383cefSAndrey Konovalov Only applies when ``kasan.page_alloc.sample`` is set to a value greater 15944383cefSAndrey Konovalov than ``1``. 16044383cefSAndrey Konovalov This parameter is intended to allow sampling only large page_alloc 16144383cefSAndrey Konovalov allocations, which is the biggest source of the performance overhead. 16244383cefSAndrey Konovalov 1632757aafaSJonathan CorbetError reports 1642757aafaSJonathan Corbet~~~~~~~~~~~~~ 1652757aafaSJonathan Corbet 166836f79a2SAndrey KonovalovA typical KASAN report looks like this:: 1672757aafaSJonathan Corbet 1682757aafaSJonathan Corbet ================================================================== 169b3b0e6acSAndrey Konovalov BUG: KASAN: slab-out-of-bounds in kmalloc_oob_right+0xa8/0xbc [test_kasan] 170b3b0e6acSAndrey Konovalov Write of size 1 at addr ffff8801f44ec37b by task insmod/2760 1712757aafaSJonathan Corbet 172b3b0e6acSAndrey Konovalov CPU: 1 PID: 2760 Comm: insmod Not tainted 4.19.0-rc3+ #698 173b3b0e6acSAndrey Konovalov Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.10.2-1 04/01/2014 1742757aafaSJonathan Corbet Call Trace: 175b3b0e6acSAndrey Konovalov dump_stack+0x94/0xd8 176b3b0e6acSAndrey Konovalov print_address_description+0x73/0x280 177b3b0e6acSAndrey Konovalov kasan_report+0x144/0x187 178b3b0e6acSAndrey Konovalov __asan_report_store1_noabort+0x17/0x20 179b3b0e6acSAndrey Konovalov kmalloc_oob_right+0xa8/0xbc [test_kasan] 180b3b0e6acSAndrey Konovalov kmalloc_tests_init+0x16/0x700 [test_kasan] 181b3b0e6acSAndrey Konovalov do_one_initcall+0xa5/0x3ae 182b3b0e6acSAndrey Konovalov do_init_module+0x1b6/0x547 183b3b0e6acSAndrey Konovalov load_module+0x75df/0x8070 184b3b0e6acSAndrey Konovalov __do_sys_init_module+0x1c6/0x200 185b3b0e6acSAndrey Konovalov __x64_sys_init_module+0x6e/0xb0 186b3b0e6acSAndrey Konovalov do_syscall_64+0x9f/0x2c0 187b3b0e6acSAndrey Konovalov entry_SYSCALL_64_after_hwframe+0x44/0xa9 188b3b0e6acSAndrey Konovalov RIP: 0033:0x7f96443109da 189b3b0e6acSAndrey Konovalov RSP: 002b:00007ffcf0b51b08 EFLAGS: 00000202 ORIG_RAX: 00000000000000af 190b3b0e6acSAndrey Konovalov RAX: ffffffffffffffda RBX: 000055dc3ee521a0 RCX: 00007f96443109da 191b3b0e6acSAndrey Konovalov RDX: 00007f96445cff88 RSI: 0000000000057a50 RDI: 00007f9644992000 192b3b0e6acSAndrey Konovalov RBP: 000055dc3ee510b0 R08: 0000000000000003 R09: 0000000000000000 193b3b0e6acSAndrey Konovalov R10: 00007f964430cd0a R11: 0000000000000202 R12: 00007f96445cff88 194b3b0e6acSAndrey Konovalov R13: 000055dc3ee51090 R14: 0000000000000000 R15: 0000000000000000 195b3b0e6acSAndrey Konovalov 196b3b0e6acSAndrey Konovalov Allocated by task 2760: 197b3b0e6acSAndrey Konovalov save_stack+0x43/0xd0 198b3b0e6acSAndrey Konovalov kasan_kmalloc+0xa7/0xd0 199b3b0e6acSAndrey Konovalov kmem_cache_alloc_trace+0xe1/0x1b0 200b3b0e6acSAndrey Konovalov kmalloc_oob_right+0x56/0xbc [test_kasan] 201b3b0e6acSAndrey Konovalov kmalloc_tests_init+0x16/0x700 [test_kasan] 202b3b0e6acSAndrey Konovalov do_one_initcall+0xa5/0x3ae 203b3b0e6acSAndrey Konovalov do_init_module+0x1b6/0x547 204b3b0e6acSAndrey Konovalov load_module+0x75df/0x8070 205b3b0e6acSAndrey Konovalov __do_sys_init_module+0x1c6/0x200 206b3b0e6acSAndrey Konovalov __x64_sys_init_module+0x6e/0xb0 207b3b0e6acSAndrey Konovalov do_syscall_64+0x9f/0x2c0 208b3b0e6acSAndrey Konovalov entry_SYSCALL_64_after_hwframe+0x44/0xa9 209b3b0e6acSAndrey Konovalov 210b3b0e6acSAndrey Konovalov Freed by task 815: 211b3b0e6acSAndrey Konovalov save_stack+0x43/0xd0 212b3b0e6acSAndrey Konovalov __kasan_slab_free+0x135/0x190 213b3b0e6acSAndrey Konovalov kasan_slab_free+0xe/0x10 214b3b0e6acSAndrey Konovalov kfree+0x93/0x1a0 215b3b0e6acSAndrey Konovalov umh_complete+0x6a/0xa0 216b3b0e6acSAndrey Konovalov call_usermodehelper_exec_async+0x4c3/0x640 217b3b0e6acSAndrey Konovalov ret_from_fork+0x35/0x40 218b3b0e6acSAndrey Konovalov 219b3b0e6acSAndrey Konovalov The buggy address belongs to the object at ffff8801f44ec300 220b3b0e6acSAndrey Konovalov which belongs to the cache kmalloc-128 of size 128 221b3b0e6acSAndrey Konovalov The buggy address is located 123 bytes inside of 222b3b0e6acSAndrey Konovalov 128-byte region [ffff8801f44ec300, ffff8801f44ec380) 223b3b0e6acSAndrey Konovalov The buggy address belongs to the page: 224b3b0e6acSAndrey Konovalov page:ffffea0007d13b00 count:1 mapcount:0 mapping:ffff8801f7001640 index:0x0 225b3b0e6acSAndrey Konovalov flags: 0x200000000000100(slab) 226b3b0e6acSAndrey Konovalov raw: 0200000000000100 ffffea0007d11dc0 0000001a0000001a ffff8801f7001640 227b3b0e6acSAndrey Konovalov raw: 0000000000000000 0000000080150015 00000001ffffffff 0000000000000000 228b3b0e6acSAndrey Konovalov page dumped because: kasan: bad access detected 229b3b0e6acSAndrey Konovalov 2302757aafaSJonathan Corbet Memory state around the buggy address: 231b3b0e6acSAndrey Konovalov ffff8801f44ec200: fc fc fc fc fc fc fc fc fb fb fb fb fb fb fb fb 232b3b0e6acSAndrey Konovalov ffff8801f44ec280: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc 233b3b0e6acSAndrey Konovalov >ffff8801f44ec300: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 03 2342757aafaSJonathan Corbet ^ 235b3b0e6acSAndrey Konovalov ffff8801f44ec380: fc fc fc fc fc fc fc fc fb fb fb fb fb fb fb fb 236b3b0e6acSAndrey Konovalov ffff8801f44ec400: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc 2372757aafaSJonathan Corbet ================================================================== 2382757aafaSJonathan Corbet 239836f79a2SAndrey KonovalovThe report header summarizes what kind of bug happened and what kind of access 240836f79a2SAndrey Konovalovcaused it. It is followed by a stack trace of the bad access, a stack trace of 241836f79a2SAndrey Konovalovwhere the accessed memory was allocated (in case a slab object was accessed), 242836f79a2SAndrey Konovalovand a stack trace of where the object was freed (in case of a use-after-free 243836f79a2SAndrey Konovalovbug report). Next comes a description of the accessed slab object and the 244836f79a2SAndrey Konovalovinformation about the accessed memory page. 2452757aafaSJonathan Corbet 246836f79a2SAndrey KonovalovIn the end, the report shows the memory state around the accessed address. 247836f79a2SAndrey KonovalovInternally, KASAN tracks memory state separately for each memory granule, which 248625d8673SAndrey Konovalovis either 8 or 16 aligned bytes depending on KASAN mode. Each number in the 249625d8673SAndrey Konovalovmemory state section of the report shows the state of one of the memory 250625d8673SAndrey Konovalovgranules that surround the accessed address. 2512757aafaSJonathan Corbet 252c2ec0c8fSAndrey KonovalovFor Generic KASAN, the size of each memory granule is 8. The state of each 253625d8673SAndrey Konovalovgranule is encoded in one shadow byte. Those 8 bytes can be accessible, 254836f79a2SAndrey Konovalovpartially accessible, freed, or be a part of a redzone. KASAN uses the following 255836f79a2SAndrey Konovalovencoding for each shadow byte: 00 means that all 8 bytes of the corresponding 256625d8673SAndrey Konovalovmemory region are accessible; number N (1 <= N <= 7) means that the first N 257625d8673SAndrey Konovalovbytes are accessible, and other (8 - N) bytes are not; any negative value 258625d8673SAndrey Konovalovindicates that the entire 8-byte word is inaccessible. KASAN uses different 259625d8673SAndrey Konovalovnegative values to distinguish between different kinds of inaccessible memory 260625d8673SAndrey Konovalovlike redzones or freed memory (see mm/kasan/kasan.h). 2612757aafaSJonathan Corbet 262836f79a2SAndrey KonovalovIn the report above, the arrow points to the shadow byte ``03``, which means 263836f79a2SAndrey Konovalovthat the accessed address is partially accessible. 264836f79a2SAndrey Konovalov 265836f79a2SAndrey KonovalovFor tag-based KASAN modes, this last report section shows the memory tags around 266836f79a2SAndrey Konovalovthe accessed address (see the `Implementation details`_ section). 267836f79a2SAndrey Konovalov 268836f79a2SAndrey KonovalovNote that KASAN bug titles (like ``slab-out-of-bounds`` or ``use-after-free``) 269836f79a2SAndrey Konovalovare best-effort: KASAN prints the most probable bug type based on the limited 270836f79a2SAndrey Konovalovinformation it has. The actual type of the bug might be different. 271836f79a2SAndrey Konovalov 272836f79a2SAndrey KonovalovGeneric KASAN also reports up to two auxiliary call stack traces. These stack 273836f79a2SAndrey Konovalovtraces point to places in code that interacted with the object but that are not 274836f79a2SAndrey Konovalovdirectly present in the bad access stack trace. Currently, this includes 275836f79a2SAndrey Konovalovcall_rcu() and workqueue queuing. 276625d8673SAndrey Konovalov 2772757aafaSJonathan CorbetImplementation details 2782757aafaSJonathan Corbet---------------------- 2792757aafaSJonathan Corbet 280b3b0e6acSAndrey KonovalovGeneric KASAN 281b3b0e6acSAndrey Konovalov~~~~~~~~~~~~~ 282b3b0e6acSAndrey Konovalov 283b8191d7dSAndrey KonovalovSoftware KASAN modes use shadow memory to record whether each byte of memory is 284b8191d7dSAndrey Konovalovsafe to access and use compile-time instrumentation to insert shadow memory 285b8191d7dSAndrey Konovalovchecks before each memory access. 2862757aafaSJonathan Corbet 287b8191d7dSAndrey KonovalovGeneric KASAN dedicates 1/8th of kernel memory to its shadow memory (16TB 288b3b0e6acSAndrey Konovalovto cover 128TB on x86_64) and uses direct mapping with a scale and offset to 289b3b0e6acSAndrey Konovalovtranslate a memory address to its corresponding shadow address. 2902757aafaSJonathan Corbet 2912757aafaSJonathan CorbetHere is the function which translates an address to its corresponding shadow 2922757aafaSJonathan Corbetaddress:: 2932757aafaSJonathan Corbet 2942757aafaSJonathan Corbet static inline void *kasan_mem_to_shadow(const void *addr) 2952757aafaSJonathan Corbet { 296b8191d7dSAndrey Konovalov return (void *)((unsigned long)addr >> KASAN_SHADOW_SCALE_SHIFT) 2972757aafaSJonathan Corbet + KASAN_SHADOW_OFFSET; 2982757aafaSJonathan Corbet } 2992757aafaSJonathan Corbet 3002757aafaSJonathan Corbetwhere ``KASAN_SHADOW_SCALE_SHIFT = 3``. 3012757aafaSJonathan Corbet 302b3b0e6acSAndrey KonovalovCompile-time instrumentation is used to insert memory access checks. Compiler 303b8191d7dSAndrey Konovalovinserts function calls (``__asan_load*(addr)``, ``__asan_store*(addr)``) before 304b8191d7dSAndrey Konovaloveach memory access of size 1, 2, 4, 8, or 16. These functions check whether 305b8191d7dSAndrey Konovalovmemory accesses are valid or not by checking corresponding shadow memory. 3062757aafaSJonathan Corbet 307b8191d7dSAndrey KonovalovWith inline instrumentation, instead of making function calls, the compiler 308b8191d7dSAndrey Konovalovdirectly inserts the code to check shadow memory. This option significantly 309b8191d7dSAndrey Konovalovenlarges the kernel, but it gives an x1.1-x2 performance boost over the 310b8191d7dSAndrey Konovalovoutline-instrumented kernel. 311b3b0e6acSAndrey Konovalov 312b8191d7dSAndrey KonovalovGeneric KASAN is the only mode that delays the reuse of freed objects via 313625d8673SAndrey Konovalovquarantine (see mm/kasan/quarantine.c for implementation). 314625d8673SAndrey Konovalov 315c2ec0c8fSAndrey KonovalovSoftware Tag-Based KASAN 316b3b0e6acSAndrey Konovalov~~~~~~~~~~~~~~~~~~~~~~~~ 317b3b0e6acSAndrey Konovalov 318c2ec0c8fSAndrey KonovalovSoftware Tag-Based KASAN uses a software memory tagging approach to checking 319a6c18d4eSAndrey Konovalovaccess validity. It is currently only implemented for the arm64 architecture. 320948e3253SAndrey Konovalov 321c2ec0c8fSAndrey KonovalovSoftware Tag-Based KASAN uses the Top Byte Ignore (TBI) feature of arm64 CPUs 322a6c18d4eSAndrey Konovalovto store a pointer tag in the top byte of kernel pointers. It uses shadow memory 323a6c18d4eSAndrey Konovalovto store memory tags associated with each 16-byte memory cell (therefore, it 324a6c18d4eSAndrey Konovalovdedicates 1/16th of the kernel memory for shadow memory). 325b3b0e6acSAndrey Konovalov 326c2ec0c8fSAndrey KonovalovOn each memory allocation, Software Tag-Based KASAN generates a random tag, tags 327a6c18d4eSAndrey Konovalovthe allocated memory with this tag, and embeds the same tag into the returned 328948e3253SAndrey Konovalovpointer. 329948e3253SAndrey Konovalov 330c2ec0c8fSAndrey KonovalovSoftware Tag-Based KASAN uses compile-time instrumentation to insert checks 331a6c18d4eSAndrey Konovalovbefore each memory access. These checks make sure that the tag of the memory 332a6c18d4eSAndrey Konovalovthat is being accessed is equal to the tag of the pointer that is used to access 333c2ec0c8fSAndrey Konovalovthis memory. In case of a tag mismatch, Software Tag-Based KASAN prints a bug 334a6c18d4eSAndrey Konovalovreport. 335b3b0e6acSAndrey Konovalov 336c2ec0c8fSAndrey KonovalovSoftware Tag-Based KASAN also has two instrumentation modes (outline, which 337a6c18d4eSAndrey Konovalovemits callbacks to check memory accesses; and inline, which performs the shadow 338b3b0e6acSAndrey Konovalovmemory checks inline). With outline instrumentation mode, a bug report is 339a6c18d4eSAndrey Konovalovprinted from the function that performs the access check. With inline 340a6c18d4eSAndrey Konovalovinstrumentation, a ``brk`` instruction is emitted by the compiler, and a 341a6c18d4eSAndrey Konovalovdedicated ``brk`` handler is used to print bug reports. 342b3b0e6acSAndrey Konovalov 343c2ec0c8fSAndrey KonovalovSoftware Tag-Based KASAN uses 0xFF as a match-all pointer tag (accesses through 344a6c18d4eSAndrey Konovalovpointers with the 0xFF pointer tag are not checked). The value 0xFE is currently 345948e3253SAndrey Konovalovreserved to tag freed memory regions. 346948e3253SAndrey Konovalov 347c2ec0c8fSAndrey KonovalovHardware Tag-Based KASAN 348948e3253SAndrey Konovalov~~~~~~~~~~~~~~~~~~~~~~~~ 349948e3253SAndrey Konovalov 350c2ec0c8fSAndrey KonovalovHardware Tag-Based KASAN is similar to the software mode in concept but uses 351948e3253SAndrey Konovalovhardware memory tagging support instead of compiler instrumentation and 352948e3253SAndrey Konovalovshadow memory. 353948e3253SAndrey Konovalov 354c2ec0c8fSAndrey KonovalovHardware Tag-Based KASAN is currently only implemented for arm64 architecture 355948e3253SAndrey Konovalovand based on both arm64 Memory Tagging Extension (MTE) introduced in ARMv8.5 356bb48675eSAndrey KonovalovInstruction Set Architecture and Top Byte Ignore (TBI). 357948e3253SAndrey Konovalov 358948e3253SAndrey KonovalovSpecial arm64 instructions are used to assign memory tags for each allocation. 359948e3253SAndrey KonovalovSame tags are assigned to pointers to those allocations. On every memory 360bb48675eSAndrey Konovalovaccess, hardware makes sure that the tag of the memory that is being accessed is 361bb48675eSAndrey Konovalovequal to the tag of the pointer that is used to access this memory. In case of a 362bb48675eSAndrey Konovalovtag mismatch, a fault is generated, and a report is printed. 363948e3253SAndrey Konovalov 364c2ec0c8fSAndrey KonovalovHardware Tag-Based KASAN uses 0xFF as a match-all pointer tag (accesses through 365bb48675eSAndrey Konovalovpointers with the 0xFF pointer tag are not checked). The value 0xFE is currently 366948e3253SAndrey Konovalovreserved to tag freed memory regions. 367948e3253SAndrey Konovalov 368c2ec0c8fSAndrey KonovalovIf the hardware does not support MTE (pre ARMv8.5), Hardware Tag-Based KASAN 369bb48675eSAndrey Konovalovwill not be enabled. In this case, all KASAN boot parameters are ignored. 3704062c245SAndrey Konovalov 371bb48675eSAndrey KonovalovNote that enabling CONFIG_KASAN_HW_TAGS always results in in-kernel TBI being 372bb48675eSAndrey Konovalovenabled. Even when ``kasan.mode=off`` is provided or when the hardware does not 3734062c245SAndrey Konovalovsupport MTE (but supports TBI). 3744062c245SAndrey Konovalov 375c2ec0c8fSAndrey KonovalovHardware Tag-Based KASAN only reports the first found bug. After that, MTE tag 3767169487bSAndrey Konovalovchecking gets disabled. 3777169487bSAndrey Konovalov 37896d7d141SAndrey KonovalovShadow memory 37996d7d141SAndrey Konovalov------------- 3803c5c3cfbSDaniel Axtens 3818479d7b5SAndrey KonovalovThe contents of this section are only applicable to software KASAN modes. 3828479d7b5SAndrey Konovalov 38367ca1c0bSAndrey KonovalovThe kernel maps memory in several different parts of the address space. 38467ca1c0bSAndrey KonovalovThe range of kernel virtual addresses is large: there is not enough real 38567ca1c0bSAndrey Konovalovmemory to support a real shadow region for every address that could be 38667ca1c0bSAndrey Konovalovaccessed by the kernel. Therefore, KASAN only maps real shadow for certain 38767ca1c0bSAndrey Konovalovparts of the address space. 3883c5c3cfbSDaniel Axtens 38996d7d141SAndrey KonovalovDefault behaviour 39096d7d141SAndrey Konovalov~~~~~~~~~~~~~~~~~ 3913c5c3cfbSDaniel Axtens 3923c5c3cfbSDaniel AxtensBy default, architectures only map real memory over the shadow region 3933c5c3cfbSDaniel Axtensfor the linear mapping (and potentially other small areas). For all 3943c5c3cfbSDaniel Axtensother areas - such as vmalloc and vmemmap space - a single read-only 3953c5c3cfbSDaniel Axtenspage is mapped over the shadow area. This read-only shadow page 3963c5c3cfbSDaniel Axtensdeclares all memory accesses as permitted. 3973c5c3cfbSDaniel Axtens 3983c5c3cfbSDaniel AxtensThis presents a problem for modules: they do not live in the linear 39967ca1c0bSAndrey Konovalovmapping but in a dedicated module space. By hooking into the module 40067ca1c0bSAndrey Konovalovallocator, KASAN temporarily maps real shadow memory to cover them. 40167ca1c0bSAndrey KonovalovThis allows detection of invalid accesses to module globals, for example. 4023c5c3cfbSDaniel Axtens 4033c5c3cfbSDaniel AxtensThis also creates an incompatibility with ``VMAP_STACK``: if the stack 4043c5c3cfbSDaniel Axtenslives in vmalloc space, it will be shadowed by the read-only page, and 4053c5c3cfbSDaniel Axtensthe kernel will fault when trying to set up the shadow data for stack 4063c5c3cfbSDaniel Axtensvariables. 4073c5c3cfbSDaniel Axtens 4083c5c3cfbSDaniel AxtensCONFIG_KASAN_VMALLOC 4093c5c3cfbSDaniel Axtens~~~~~~~~~~~~~~~~~~~~ 4103c5c3cfbSDaniel Axtens 4113c5c3cfbSDaniel AxtensWith ``CONFIG_KASAN_VMALLOC``, KASAN can cover vmalloc space at the 41267ca1c0bSAndrey Konovalovcost of greater memory usage. Currently, this is supported on x86, 4138479d7b5SAndrey Konovalovarm64, riscv, s390, and powerpc. 4143c5c3cfbSDaniel Axtens 41567ca1c0bSAndrey KonovalovThis works by hooking into vmalloc and vmap and dynamically 4163c5c3cfbSDaniel Axtensallocating real shadow memory to back the mappings. 4173c5c3cfbSDaniel Axtens 4183c5c3cfbSDaniel AxtensMost mappings in vmalloc space are small, requiring less than a full 4193c5c3cfbSDaniel Axtenspage of shadow space. Allocating a full shadow page per mapping would 4203c5c3cfbSDaniel Axtenstherefore be wasteful. Furthermore, to ensure that different mappings 4213c5c3cfbSDaniel Axtensuse different shadow pages, mappings would have to be aligned to 4221f600626SAndrey Konovalov``KASAN_GRANULE_SIZE * PAGE_SIZE``. 4233c5c3cfbSDaniel Axtens 424625d8673SAndrey KonovalovInstead, KASAN shares backing space across multiple mappings. It allocates 4253c5c3cfbSDaniel Axtensa backing page when a mapping in vmalloc space uses a particular page 4263c5c3cfbSDaniel Axtensof the shadow region. This page can be shared by other vmalloc 4273c5c3cfbSDaniel Axtensmappings later on. 4283c5c3cfbSDaniel Axtens 429625d8673SAndrey KonovalovKASAN hooks into the vmap infrastructure to lazily clean up unused shadow 4303c5c3cfbSDaniel Axtensmemory. 4313c5c3cfbSDaniel Axtens 432625d8673SAndrey KonovalovTo avoid the difficulties around swapping mappings around, KASAN expects 4333c5c3cfbSDaniel Axtensthat the part of the shadow region that covers the vmalloc space will 43467ca1c0bSAndrey Konovalovnot be covered by the early shadow page but will be left unmapped. 43567ca1c0bSAndrey KonovalovThis will require changes in arch-specific code. 4363c5c3cfbSDaniel Axtens 43767ca1c0bSAndrey KonovalovThis allows ``VMAP_STACK`` support on x86 and can simplify support of 4383c5c3cfbSDaniel Axtensarchitectures that do not have a fixed module region. 4399ab5be97SPatricia Alfonso 44096d7d141SAndrey KonovalovFor developers 44196d7d141SAndrey Konovalov-------------- 44296d7d141SAndrey Konovalov 44396d7d141SAndrey KonovalovIgnoring accesses 44496d7d141SAndrey Konovalov~~~~~~~~~~~~~~~~~ 44596d7d141SAndrey Konovalov 44696d7d141SAndrey KonovalovSoftware KASAN modes use compiler instrumentation to insert validity checks. 447fe547fcaSAndrey KonovalovSuch instrumentation might be incompatible with some parts of the kernel, and 448fe547fcaSAndrey Konovalovtherefore needs to be disabled. 449fe547fcaSAndrey Konovalov 450fe547fcaSAndrey KonovalovOther parts of the kernel might access metadata for allocated objects. 451fe547fcaSAndrey KonovalovNormally, KASAN detects and reports such accesses, but in some cases (e.g., 452fe547fcaSAndrey Konovalovin memory allocators), these accesses are valid. 453fe547fcaSAndrey Konovalov 454fe547fcaSAndrey KonovalovFor software KASAN modes, to disable instrumentation for a specific file or 455fe547fcaSAndrey Konovalovdirectory, add a ``KASAN_SANITIZE`` annotation to the respective kernel 45696d7d141SAndrey KonovalovMakefile: 45796d7d141SAndrey Konovalov 458fe547fcaSAndrey Konovalov- For a single file (e.g., main.o):: 45996d7d141SAndrey Konovalov 46096d7d141SAndrey Konovalov KASAN_SANITIZE_main.o := n 46196d7d141SAndrey Konovalov 46296d7d141SAndrey Konovalov- For all files in one directory:: 46396d7d141SAndrey Konovalov 46496d7d141SAndrey Konovalov KASAN_SANITIZE := n 46596d7d141SAndrey Konovalov 466fe547fcaSAndrey KonovalovFor software KASAN modes, to disable instrumentation on a per-function basis, 467fe547fcaSAndrey Konovalovuse the KASAN-specific ``__no_sanitize_address`` function attribute or the 468fe547fcaSAndrey Konovalovgeneric ``noinstr`` one. 469fe547fcaSAndrey Konovalov 470fe547fcaSAndrey KonovalovNote that disabling compiler instrumentation (either on a per-file or a 471fe547fcaSAndrey Konovalovper-function basis) makes KASAN ignore the accesses that happen directly in 472fe547fcaSAndrey Konovalovthat code for software KASAN modes. It does not help when the accesses happen 473c2ec0c8fSAndrey Konovalovindirectly (through calls to instrumented functions) or with Hardware 474c2ec0c8fSAndrey KonovalovTag-Based KASAN, which does not use compiler instrumentation. 475fe547fcaSAndrey Konovalov 476fe547fcaSAndrey KonovalovFor software KASAN modes, to disable KASAN reports in a part of the kernel code 477fe547fcaSAndrey Konovalovfor the current task, annotate this part of the code with a 478fe547fcaSAndrey Konovalov``kasan_disable_current()``/``kasan_enable_current()`` section. This also 479fe547fcaSAndrey Konovalovdisables the reports for indirect accesses that happen through function calls. 480fe547fcaSAndrey Konovalov 481c2ec0c8fSAndrey KonovalovFor tag-based KASAN modes, to disable access checking, use 482c2ec0c8fSAndrey Konovalov``kasan_reset_tag()`` or ``page_kasan_tag_reset()``. Note that temporarily 483c2ec0c8fSAndrey Konovalovdisabling access checking via ``page_kasan_tag_reset()`` requires saving and 484c2ec0c8fSAndrey Konovalovrestoring the per-page KASAN tag via ``page_kasan_tag``/``page_kasan_tag_set``. 48596d7d141SAndrey Konovalov 48696d7d141SAndrey KonovalovTests 48796d7d141SAndrey Konovalov~~~~~ 4889ab5be97SPatricia Alfonso 489fc23c074SAndrey KonovalovThere are KASAN tests that allow verifying that KASAN works and can detect 490fc23c074SAndrey Konovalovcertain types of memory corruptions. The tests consist of two parts: 4919ab5be97SPatricia Alfonso 492625d8673SAndrey Konovalov1. Tests that are integrated with the KUnit Test Framework. Enabled with 493625d8673SAndrey Konovalov``CONFIG_KASAN_KUNIT_TEST``. These tests can be run and partially verified 494fc23c074SAndrey Konovalovautomatically in a few different ways; see the instructions below. 4959ab5be97SPatricia Alfonso 496625d8673SAndrey Konovalov2. Tests that are currently incompatible with KUnit. Enabled with 4975d92bdffSAndrey Konovalov``CONFIG_KASAN_MODULE_TEST`` and can only be run as a module. These tests can 498fc23c074SAndrey Konovalovonly be verified manually by loading the kernel module and inspecting the 499625d8673SAndrey Konovalovkernel log for KASAN reports. 500625d8673SAndrey Konovalov 501fc23c074SAndrey KonovalovEach KUnit-compatible KASAN test prints one of multiple KASAN reports if an 502fc23c074SAndrey Konovaloverror is detected. Then the test prints its number and status. 503625d8673SAndrey Konovalov 504625d8673SAndrey KonovalovWhen a test passes:: 5059ab5be97SPatricia Alfonso 5069ab5be97SPatricia Alfonso ok 28 - kmalloc_double_kzfree 50732519c03SMauro Carvalho Chehab 508625d8673SAndrey KonovalovWhen a test fails due to a failed ``kmalloc``:: 5099ab5be97SPatricia Alfonso 5109ab5be97SPatricia Alfonso # kmalloc_large_oob_right: ASSERTION FAILED at lib/test_kasan.c:163 5119ab5be97SPatricia Alfonso Expected ptr is not null, but is 5129ab5be97SPatricia Alfonso not ok 4 - kmalloc_large_oob_right 51332519c03SMauro Carvalho Chehab 514625d8673SAndrey KonovalovWhen a test fails due to a missing KASAN report:: 5159ab5be97SPatricia Alfonso 5163ff16d30SDavid Gow # kmalloc_double_kzfree: EXPECTATION FAILED at lib/test_kasan.c:974 5173ff16d30SDavid Gow KASAN failure expected in "kfree_sensitive(ptr)", but none occurred 5183ff16d30SDavid Gow not ok 44 - kmalloc_double_kzfree 5193ff16d30SDavid Gow 5209ab5be97SPatricia Alfonso 521625d8673SAndrey KonovalovAt the end the cumulative status of all KASAN tests is printed. On success:: 5229ab5be97SPatricia Alfonso 5239ab5be97SPatricia Alfonso ok 1 - kasan 5249ab5be97SPatricia Alfonso 525625d8673SAndrey KonovalovOr, if one of the tests failed:: 5269ab5be97SPatricia Alfonso 5279ab5be97SPatricia Alfonso not ok 1 - kasan 5289ab5be97SPatricia Alfonso 529625d8673SAndrey KonovalovThere are a few ways to run KUnit-compatible KASAN tests. 530625d8673SAndrey Konovalov 531625d8673SAndrey Konovalov1. Loadable module 5329ab5be97SPatricia Alfonso 533fc23c074SAndrey Konovalov With ``CONFIG_KUNIT`` enabled, KASAN-KUnit tests can be built as a loadable 534fc23c074SAndrey Konovalov module and run by loading ``test_kasan.ko`` with ``insmod`` or ``modprobe``. 5359ab5be97SPatricia Alfonso 536625d8673SAndrey Konovalov2. Built-In 5379ab5be97SPatricia Alfonso 538fc23c074SAndrey Konovalov With ``CONFIG_KUNIT`` built-in, KASAN-KUnit tests can be built-in as well. 539fc23c074SAndrey Konovalov In this case, the tests will run at boot as a late-init call. 5409ab5be97SPatricia Alfonso 541625d8673SAndrey Konovalov3. Using kunit_tool 5429ab5be97SPatricia Alfonso 543fc23c074SAndrey Konovalov With ``CONFIG_KUNIT`` and ``CONFIG_KASAN_KUNIT_TEST`` built-in, it is also 544fc23c074SAndrey Konovalov possible to use ``kunit_tool`` to see the results of KUnit tests in a more 545fc23c074SAndrey Konovalov readable way. This will not print the KASAN reports of the tests that passed. 546fc23c074SAndrey Konovalov See `KUnit documentation <https://www.kernel.org/doc/html/latest/dev-tools/kunit/index.html>`_ 547625d8673SAndrey Konovalov for more up-to-date information on ``kunit_tool``. 5489ab5be97SPatricia Alfonso 5499ab5be97SPatricia Alfonso.. _KUnit: https://www.kernel.org/doc/html/latest/dev-tools/kunit/index.html 550