1*4882a593SmuzhiyunThe Kernel Address Sanitizer (KASAN) 2*4882a593Smuzhiyun==================================== 3*4882a593Smuzhiyun 4*4882a593SmuzhiyunOverview 5*4882a593Smuzhiyun-------- 6*4882a593Smuzhiyun 7*4882a593SmuzhiyunKernelAddressSANitizer (KASAN) is a dynamic memory safety error detector 8*4882a593Smuzhiyundesigned to find out-of-bound and use-after-free bugs. KASAN has three modes: 9*4882a593Smuzhiyun 10*4882a593Smuzhiyun1. generic KASAN (similar to userspace ASan), 11*4882a593Smuzhiyun2. software tag-based KASAN (similar to userspace HWASan), 12*4882a593Smuzhiyun3. hardware tag-based KASAN (based on hardware memory tagging). 13*4882a593Smuzhiyun 14*4882a593SmuzhiyunSoftware KASAN modes (1 and 2) use compile-time instrumentation to insert 15*4882a593Smuzhiyunvalidity checks before every memory access, and therefore require a compiler 16*4882a593Smuzhiyunversion that supports that. 17*4882a593Smuzhiyun 18*4882a593SmuzhiyunGeneric KASAN is supported in both GCC and Clang. With GCC it requires version 19*4882a593Smuzhiyun8.3.0 or later. Any supported Clang version is compatible, but detection of 20*4882a593Smuzhiyunout-of-bounds accesses for global variables is only supported since Clang 11. 21*4882a593Smuzhiyun 22*4882a593SmuzhiyunTag-based KASAN is only supported in Clang. 23*4882a593Smuzhiyun 24*4882a593SmuzhiyunCurrently generic KASAN is supported for the x86_64, arm64, xtensa, s390 and 25*4882a593Smuzhiyunand riscv architectures, and tag-based KASAN modes are supported only for arm64. 26*4882a593Smuzhiyun 27*4882a593SmuzhiyunUsage 28*4882a593Smuzhiyun----- 29*4882a593Smuzhiyun 30*4882a593SmuzhiyunTo enable KASAN configure kernel with:: 31*4882a593Smuzhiyun 32*4882a593Smuzhiyun CONFIG_KASAN = y 33*4882a593Smuzhiyun 34*4882a593Smuzhiyunand choose between CONFIG_KASAN_GENERIC (to enable generic KASAN), 35*4882a593SmuzhiyunCONFIG_KASAN_SW_TAGS (to enable software tag-based KASAN), and 36*4882a593SmuzhiyunCONFIG_KASAN_HW_TAGS (to enable hardware tag-based KASAN). 37*4882a593Smuzhiyun 38*4882a593SmuzhiyunFor software modes, you also need to choose between CONFIG_KASAN_OUTLINE and 39*4882a593SmuzhiyunCONFIG_KASAN_INLINE. Outline and inline are compiler instrumentation types. 40*4882a593SmuzhiyunThe former produces smaller binary while the latter is 1.1 - 2 times faster. 41*4882a593Smuzhiyun 42*4882a593SmuzhiyunBoth software KASAN modes work with both SLUB and SLAB memory allocators, 43*4882a593Smuzhiyunwhile the hardware tag-based KASAN currently only support SLUB. 44*4882a593Smuzhiyun 45*4882a593SmuzhiyunFor better error reports that include stack traces, enable CONFIG_STACKTRACE. 46*4882a593Smuzhiyun 47*4882a593SmuzhiyunTo augment reports with last allocation and freeing stack of the physical page, 48*4882a593Smuzhiyunit is recommended to enable also CONFIG_PAGE_OWNER and boot with page_owner=on. 49*4882a593Smuzhiyun 50*4882a593SmuzhiyunError reports 51*4882a593Smuzhiyun~~~~~~~~~~~~~ 52*4882a593Smuzhiyun 53*4882a593SmuzhiyunA typical out-of-bounds access generic KASAN report looks like this:: 54*4882a593Smuzhiyun 55*4882a593Smuzhiyun ================================================================== 56*4882a593Smuzhiyun BUG: KASAN: slab-out-of-bounds in kmalloc_oob_right+0xa8/0xbc [test_kasan] 57*4882a593Smuzhiyun Write of size 1 at addr ffff8801f44ec37b by task insmod/2760 58*4882a593Smuzhiyun 59*4882a593Smuzhiyun CPU: 1 PID: 2760 Comm: insmod Not tainted 4.19.0-rc3+ #698 60*4882a593Smuzhiyun Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.10.2-1 04/01/2014 61*4882a593Smuzhiyun Call Trace: 62*4882a593Smuzhiyun dump_stack+0x94/0xd8 63*4882a593Smuzhiyun print_address_description+0x73/0x280 64*4882a593Smuzhiyun kasan_report+0x144/0x187 65*4882a593Smuzhiyun __asan_report_store1_noabort+0x17/0x20 66*4882a593Smuzhiyun kmalloc_oob_right+0xa8/0xbc [test_kasan] 67*4882a593Smuzhiyun kmalloc_tests_init+0x16/0x700 [test_kasan] 68*4882a593Smuzhiyun do_one_initcall+0xa5/0x3ae 69*4882a593Smuzhiyun do_init_module+0x1b6/0x547 70*4882a593Smuzhiyun load_module+0x75df/0x8070 71*4882a593Smuzhiyun __do_sys_init_module+0x1c6/0x200 72*4882a593Smuzhiyun __x64_sys_init_module+0x6e/0xb0 73*4882a593Smuzhiyun do_syscall_64+0x9f/0x2c0 74*4882a593Smuzhiyun entry_SYSCALL_64_after_hwframe+0x44/0xa9 75*4882a593Smuzhiyun RIP: 0033:0x7f96443109da 76*4882a593Smuzhiyun RSP: 002b:00007ffcf0b51b08 EFLAGS: 00000202 ORIG_RAX: 00000000000000af 77*4882a593Smuzhiyun RAX: ffffffffffffffda RBX: 000055dc3ee521a0 RCX: 00007f96443109da 78*4882a593Smuzhiyun RDX: 00007f96445cff88 RSI: 0000000000057a50 RDI: 00007f9644992000 79*4882a593Smuzhiyun RBP: 000055dc3ee510b0 R08: 0000000000000003 R09: 0000000000000000 80*4882a593Smuzhiyun R10: 00007f964430cd0a R11: 0000000000000202 R12: 00007f96445cff88 81*4882a593Smuzhiyun R13: 000055dc3ee51090 R14: 0000000000000000 R15: 0000000000000000 82*4882a593Smuzhiyun 83*4882a593Smuzhiyun Allocated by task 2760: 84*4882a593Smuzhiyun save_stack+0x43/0xd0 85*4882a593Smuzhiyun kasan_kmalloc+0xa7/0xd0 86*4882a593Smuzhiyun kmem_cache_alloc_trace+0xe1/0x1b0 87*4882a593Smuzhiyun kmalloc_oob_right+0x56/0xbc [test_kasan] 88*4882a593Smuzhiyun kmalloc_tests_init+0x16/0x700 [test_kasan] 89*4882a593Smuzhiyun do_one_initcall+0xa5/0x3ae 90*4882a593Smuzhiyun do_init_module+0x1b6/0x547 91*4882a593Smuzhiyun load_module+0x75df/0x8070 92*4882a593Smuzhiyun __do_sys_init_module+0x1c6/0x200 93*4882a593Smuzhiyun __x64_sys_init_module+0x6e/0xb0 94*4882a593Smuzhiyun do_syscall_64+0x9f/0x2c0 95*4882a593Smuzhiyun entry_SYSCALL_64_after_hwframe+0x44/0xa9 96*4882a593Smuzhiyun 97*4882a593Smuzhiyun Freed by task 815: 98*4882a593Smuzhiyun save_stack+0x43/0xd0 99*4882a593Smuzhiyun __kasan_slab_free+0x135/0x190 100*4882a593Smuzhiyun kasan_slab_free+0xe/0x10 101*4882a593Smuzhiyun kfree+0x93/0x1a0 102*4882a593Smuzhiyun umh_complete+0x6a/0xa0 103*4882a593Smuzhiyun call_usermodehelper_exec_async+0x4c3/0x640 104*4882a593Smuzhiyun ret_from_fork+0x35/0x40 105*4882a593Smuzhiyun 106*4882a593Smuzhiyun The buggy address belongs to the object at ffff8801f44ec300 107*4882a593Smuzhiyun which belongs to the cache kmalloc-128 of size 128 108*4882a593Smuzhiyun The buggy address is located 123 bytes inside of 109*4882a593Smuzhiyun 128-byte region [ffff8801f44ec300, ffff8801f44ec380) 110*4882a593Smuzhiyun The buggy address belongs to the page: 111*4882a593Smuzhiyun page:ffffea0007d13b00 count:1 mapcount:0 mapping:ffff8801f7001640 index:0x0 112*4882a593Smuzhiyun flags: 0x200000000000100(slab) 113*4882a593Smuzhiyun raw: 0200000000000100 ffffea0007d11dc0 0000001a0000001a ffff8801f7001640 114*4882a593Smuzhiyun raw: 0000000000000000 0000000080150015 00000001ffffffff 0000000000000000 115*4882a593Smuzhiyun page dumped because: kasan: bad access detected 116*4882a593Smuzhiyun 117*4882a593Smuzhiyun Memory state around the buggy address: 118*4882a593Smuzhiyun ffff8801f44ec200: fc fc fc fc fc fc fc fc fb fb fb fb fb fb fb fb 119*4882a593Smuzhiyun ffff8801f44ec280: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc 120*4882a593Smuzhiyun >ffff8801f44ec300: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 03 121*4882a593Smuzhiyun ^ 122*4882a593Smuzhiyun ffff8801f44ec380: fc fc fc fc fc fc fc fc fb fb fb fb fb fb fb fb 123*4882a593Smuzhiyun ffff8801f44ec400: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc 124*4882a593Smuzhiyun ================================================================== 125*4882a593Smuzhiyun 126*4882a593SmuzhiyunThe header of the report provides a short summary of what kind of bug happened 127*4882a593Smuzhiyunand what kind of access caused it. It's followed by a stack trace of the bad 128*4882a593Smuzhiyunaccess, a stack trace of where the accessed memory was allocated (in case bad 129*4882a593Smuzhiyunaccess happens on a slab object), and a stack trace of where the object was 130*4882a593Smuzhiyunfreed (in case of a use-after-free bug report). Next comes a description of 131*4882a593Smuzhiyunthe accessed slab object and information about the accessed memory page. 132*4882a593Smuzhiyun 133*4882a593SmuzhiyunIn the last section the report shows memory state around the accessed address. 134*4882a593SmuzhiyunInternally KASAN tracks memory state separately for each memory granule, which 135*4882a593Smuzhiyunis either 8 or 16 aligned bytes depending on KASAN mode. Each number in the 136*4882a593Smuzhiyunmemory state section of the report shows the state of one of the memory 137*4882a593Smuzhiyungranules that surround the accessed address. 138*4882a593Smuzhiyun 139*4882a593SmuzhiyunFor generic KASAN the size of each memory granule is 8. The state of each 140*4882a593Smuzhiyungranule is encoded in one shadow byte. Those 8 bytes can be accessible, 141*4882a593Smuzhiyunpartially accessible, freed or be a part of a redzone. KASAN uses the following 142*4882a593Smuzhiyunencoding for each shadow byte: 0 means that all 8 bytes of the corresponding 143*4882a593Smuzhiyunmemory region are accessible; number N (1 <= N <= 7) means that the first N 144*4882a593Smuzhiyunbytes are accessible, and other (8 - N) bytes are not; any negative value 145*4882a593Smuzhiyunindicates that the entire 8-byte word is inaccessible. KASAN uses different 146*4882a593Smuzhiyunnegative values to distinguish between different kinds of inaccessible memory 147*4882a593Smuzhiyunlike redzones or freed memory (see mm/kasan/kasan.h). 148*4882a593Smuzhiyun 149*4882a593SmuzhiyunIn the report above the arrows point to the shadow byte 03, which means that 150*4882a593Smuzhiyunthe accessed address is partially accessible. For tag-based KASAN modes this 151*4882a593Smuzhiyunlast report section shows the memory tags around the accessed address 152*4882a593Smuzhiyun(see the `Implementation details`_ section). 153*4882a593Smuzhiyun 154*4882a593SmuzhiyunBoot parameters 155*4882a593Smuzhiyun~~~~~~~~~~~~~~~ 156*4882a593Smuzhiyun 157*4882a593SmuzhiyunHardware tag-based KASAN mode (see the section about various modes below) is 158*4882a593Smuzhiyunintended for use in production as a security mitigation. Therefore, it supports 159*4882a593Smuzhiyunboot parameters that allow to disable KASAN competely or otherwise control 160*4882a593Smuzhiyunparticular KASAN features. 161*4882a593Smuzhiyun 162*4882a593Smuzhiyun- ``kasan=off`` or ``=on`` controls whether KASAN is enabled (default: ``on``). 163*4882a593Smuzhiyun 164*4882a593Smuzhiyun- ``kasan.mode=sync`` or ``=async`` controls whether KASAN is configured in 165*4882a593Smuzhiyun synchronous or asynchronous mode of execution (default: ``sync``). 166*4882a593Smuzhiyun Synchronous mode: a bad access is detected immediately when a tag 167*4882a593Smuzhiyun check fault occurs. 168*4882a593Smuzhiyun Asynchronous mode: a bad access detection is delayed. When a tag check 169*4882a593Smuzhiyun fault occurs, the information is stored in hardware (in the TFSR_EL1 170*4882a593Smuzhiyun register for arm64). The kernel periodically checks the hardware and 171*4882a593Smuzhiyun only reports tag faults during these checks. 172*4882a593Smuzhiyun 173*4882a593Smuzhiyun- ``kasan.stacktrace=off`` or ``=on`` disables or enables alloc and free stack 174*4882a593Smuzhiyun traces collection (default: ``on``). 175*4882a593Smuzhiyun 176*4882a593Smuzhiyun- ``kasan.fault=report`` or ``=panic`` controls whether to only print a KASAN 177*4882a593Smuzhiyun report or also panic the kernel (default: ``report``). Note, that tag 178*4882a593Smuzhiyun checking gets disabled after the first reported bug. 179*4882a593Smuzhiyun 180*4882a593SmuzhiyunFor developers 181*4882a593Smuzhiyun~~~~~~~~~~~~~~ 182*4882a593Smuzhiyun 183*4882a593SmuzhiyunSoftware KASAN modes use compiler instrumentation to insert validity checks. 184*4882a593SmuzhiyunSuch instrumentation might be incompatible with some part of the kernel, and 185*4882a593Smuzhiyuntherefore needs to be disabled. To disable instrumentation for specific files 186*4882a593Smuzhiyunor directories, add a line similar to the following to the respective kernel 187*4882a593SmuzhiyunMakefile: 188*4882a593Smuzhiyun 189*4882a593Smuzhiyun- For a single file (e.g. main.o):: 190*4882a593Smuzhiyun 191*4882a593Smuzhiyun KASAN_SANITIZE_main.o := n 192*4882a593Smuzhiyun 193*4882a593Smuzhiyun- For all files in one directory:: 194*4882a593Smuzhiyun 195*4882a593Smuzhiyun KASAN_SANITIZE := n 196*4882a593Smuzhiyun 197*4882a593Smuzhiyun 198*4882a593SmuzhiyunImplementation details 199*4882a593Smuzhiyun---------------------- 200*4882a593Smuzhiyun 201*4882a593SmuzhiyunGeneric KASAN 202*4882a593Smuzhiyun~~~~~~~~~~~~~ 203*4882a593Smuzhiyun 204*4882a593SmuzhiyunFrom a high level perspective, KASAN's approach to memory error detection is 205*4882a593Smuzhiyunsimilar to that of kmemcheck: use shadow memory to record whether each byte of 206*4882a593Smuzhiyunmemory is safe to access, and use compile-time instrumentation to insert checks 207*4882a593Smuzhiyunof shadow memory on each memory access. 208*4882a593Smuzhiyun 209*4882a593SmuzhiyunGeneric KASAN dedicates 1/8th of kernel memory to its shadow memory (e.g. 16TB 210*4882a593Smuzhiyunto cover 128TB on x86_64) and uses direct mapping with a scale and offset to 211*4882a593Smuzhiyuntranslate a memory address to its corresponding shadow address. 212*4882a593Smuzhiyun 213*4882a593SmuzhiyunHere is the function which translates an address to its corresponding shadow 214*4882a593Smuzhiyunaddress:: 215*4882a593Smuzhiyun 216*4882a593Smuzhiyun static inline void *kasan_mem_to_shadow(const void *addr) 217*4882a593Smuzhiyun { 218*4882a593Smuzhiyun return ((unsigned long)addr >> KASAN_SHADOW_SCALE_SHIFT) 219*4882a593Smuzhiyun + KASAN_SHADOW_OFFSET; 220*4882a593Smuzhiyun } 221*4882a593Smuzhiyun 222*4882a593Smuzhiyunwhere ``KASAN_SHADOW_SCALE_SHIFT = 3``. 223*4882a593Smuzhiyun 224*4882a593SmuzhiyunCompile-time instrumentation is used to insert memory access checks. Compiler 225*4882a593Smuzhiyuninserts function calls (__asan_load*(addr), __asan_store*(addr)) before each 226*4882a593Smuzhiyunmemory access of size 1, 2, 4, 8 or 16. These functions check whether memory 227*4882a593Smuzhiyunaccess is valid or not by checking corresponding shadow memory. 228*4882a593Smuzhiyun 229*4882a593SmuzhiyunGCC 5.0 has possibility to perform inline instrumentation. Instead of making 230*4882a593Smuzhiyunfunction calls GCC directly inserts the code to check the shadow memory. 231*4882a593SmuzhiyunThis option significantly enlarges kernel but it gives x1.1-x2 performance 232*4882a593Smuzhiyunboost over outline instrumented kernel. 233*4882a593Smuzhiyun 234*4882a593SmuzhiyunGeneric KASAN also reports the last 2 call stacks to creation of work that 235*4882a593Smuzhiyunpotentially has access to an object. Call stacks for the following are shown: 236*4882a593Smuzhiyuncall_rcu() and workqueue queuing. 237*4882a593Smuzhiyun 238*4882a593SmuzhiyunGeneric KASAN is the only mode that delays the reuse of freed object via 239*4882a593Smuzhiyunquarantine (see mm/kasan/quarantine.c for implementation). 240*4882a593Smuzhiyun 241*4882a593SmuzhiyunSoftware tag-based KASAN 242*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~ 243*4882a593Smuzhiyun 244*4882a593SmuzhiyunSoftware tag-based KASAN requires software memory tagging support in the form 245*4882a593Smuzhiyunof HWASan-like compiler instrumentation (see HWASan documentation for details). 246*4882a593Smuzhiyun 247*4882a593SmuzhiyunSoftware tag-based KASAN is currently only implemented for arm64 architecture. 248*4882a593Smuzhiyun 249*4882a593SmuzhiyunSoftware tag-based KASAN uses the Top Byte Ignore (TBI) feature of arm64 CPUs 250*4882a593Smuzhiyunto store a pointer tag in the top byte of kernel pointers. Like generic KASAN 251*4882a593Smuzhiyunit uses shadow memory to store memory tags associated with each 16-byte memory 252*4882a593Smuzhiyuncell (therefore it dedicates 1/16th of the kernel memory for shadow memory). 253*4882a593Smuzhiyun 254*4882a593SmuzhiyunOn each memory allocation software tag-based KASAN generates a random tag, tags 255*4882a593Smuzhiyunthe allocated memory with this tag, and embeds this tag into the returned 256*4882a593Smuzhiyunpointer. 257*4882a593Smuzhiyun 258*4882a593SmuzhiyunSoftware tag-based KASAN uses compile-time instrumentation to insert checks 259*4882a593Smuzhiyunbefore each memory access. These checks make sure that tag of the memory that 260*4882a593Smuzhiyunis being accessed is equal to tag of the pointer that is used to access this 261*4882a593Smuzhiyunmemory. In case of a tag mismatch software tag-based KASAN prints a bug report. 262*4882a593Smuzhiyun 263*4882a593SmuzhiyunSoftware tag-based KASAN also has two instrumentation modes (outline, that 264*4882a593Smuzhiyunemits callbacks to check memory accesses; and inline, that performs the shadow 265*4882a593Smuzhiyunmemory checks inline). With outline instrumentation mode, a bug report is 266*4882a593Smuzhiyunsimply printed from the function that performs the access check. With inline 267*4882a593Smuzhiyuninstrumentation a brk instruction is emitted by the compiler, and a dedicated 268*4882a593Smuzhiyunbrk handler is used to print bug reports. 269*4882a593Smuzhiyun 270*4882a593SmuzhiyunSoftware tag-based KASAN uses 0xFF as a match-all pointer tag (accesses through 271*4882a593Smuzhiyunpointers with 0xFF pointer tag aren't checked). The value 0xFE is currently 272*4882a593Smuzhiyunreserved to tag freed memory regions. 273*4882a593Smuzhiyun 274*4882a593SmuzhiyunSoftware tag-based KASAN currently only supports tagging of 275*4882a593Smuzhiyunkmem_cache_alloc/kmalloc and page_alloc memory. 276*4882a593Smuzhiyun 277*4882a593SmuzhiyunHardware tag-based KASAN 278*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~ 279*4882a593Smuzhiyun 280*4882a593SmuzhiyunHardware tag-based KASAN is similar to the software mode in concept, but uses 281*4882a593Smuzhiyunhardware memory tagging support instead of compiler instrumentation and 282*4882a593Smuzhiyunshadow memory. 283*4882a593Smuzhiyun 284*4882a593SmuzhiyunHardware tag-based KASAN is currently only implemented for arm64 architecture 285*4882a593Smuzhiyunand based on both arm64 Memory Tagging Extension (MTE) introduced in ARMv8.5 286*4882a593SmuzhiyunInstruction Set Architecture, and Top Byte Ignore (TBI). 287*4882a593Smuzhiyun 288*4882a593SmuzhiyunSpecial arm64 instructions are used to assign memory tags for each allocation. 289*4882a593SmuzhiyunSame tags are assigned to pointers to those allocations. On every memory 290*4882a593Smuzhiyunaccess, hardware makes sure that tag of the memory that is being accessed is 291*4882a593Smuzhiyunequal to tag of the pointer that is used to access this memory. In case of a 292*4882a593Smuzhiyuntag mismatch a fault is generated and a report is printed. 293*4882a593Smuzhiyun 294*4882a593SmuzhiyunHardware tag-based KASAN uses 0xFF as a match-all pointer tag (accesses through 295*4882a593Smuzhiyunpointers with 0xFF pointer tag aren't checked). The value 0xFE is currently 296*4882a593Smuzhiyunreserved to tag freed memory regions. 297*4882a593Smuzhiyun 298*4882a593SmuzhiyunHardware tag-based KASAN currently only supports tagging of 299*4882a593Smuzhiyunkmem_cache_alloc/kmalloc and page_alloc memory. 300*4882a593Smuzhiyun 301*4882a593SmuzhiyunIf the hardware doesn't support MTE (pre ARMv8.5), hardware tag-based KASAN 302*4882a593Smuzhiyunwon't be enabled. In this case all boot parameters are ignored. 303*4882a593Smuzhiyun 304*4882a593SmuzhiyunNote, that enabling CONFIG_KASAN_HW_TAGS always results in in-kernel TBI being 305*4882a593Smuzhiyunenabled. Even when kasan.mode=off is provided, or when the hardware doesn't 306*4882a593Smuzhiyunsupport MTE (but supports TBI). 307*4882a593Smuzhiyun 308*4882a593SmuzhiyunHardware tag-based KASAN only reports the first found bug. After that MTE tag 309*4882a593Smuzhiyunchecking gets disabled. 310*4882a593Smuzhiyun 311*4882a593SmuzhiyunWhat memory accesses are sanitised by KASAN? 312*4882a593Smuzhiyun-------------------------------------------- 313*4882a593Smuzhiyun 314*4882a593SmuzhiyunThe kernel maps memory in a number of different parts of the address 315*4882a593Smuzhiyunspace. This poses something of a problem for KASAN, which requires 316*4882a593Smuzhiyunthat all addresses accessed by instrumented code have a valid shadow 317*4882a593Smuzhiyunregion. 318*4882a593Smuzhiyun 319*4882a593SmuzhiyunThe range of kernel virtual addresses is large: there is not enough 320*4882a593Smuzhiyunreal memory to support a real shadow region for every address that 321*4882a593Smuzhiyuncould be accessed by the kernel. 322*4882a593Smuzhiyun 323*4882a593SmuzhiyunBy default 324*4882a593Smuzhiyun~~~~~~~~~~ 325*4882a593Smuzhiyun 326*4882a593SmuzhiyunBy default, architectures only map real memory over the shadow region 327*4882a593Smuzhiyunfor the linear mapping (and potentially other small areas). For all 328*4882a593Smuzhiyunother areas - such as vmalloc and vmemmap space - a single read-only 329*4882a593Smuzhiyunpage is mapped over the shadow area. This read-only shadow page 330*4882a593Smuzhiyundeclares all memory accesses as permitted. 331*4882a593Smuzhiyun 332*4882a593SmuzhiyunThis presents a problem for modules: they do not live in the linear 333*4882a593Smuzhiyunmapping, but in a dedicated module space. By hooking in to the module 334*4882a593Smuzhiyunallocator, KASAN can temporarily map real shadow memory to cover 335*4882a593Smuzhiyunthem. This allows detection of invalid accesses to module globals, for 336*4882a593Smuzhiyunexample. 337*4882a593Smuzhiyun 338*4882a593SmuzhiyunThis also creates an incompatibility with ``VMAP_STACK``: if the stack 339*4882a593Smuzhiyunlives in vmalloc space, it will be shadowed by the read-only page, and 340*4882a593Smuzhiyunthe kernel will fault when trying to set up the shadow data for stack 341*4882a593Smuzhiyunvariables. 342*4882a593Smuzhiyun 343*4882a593SmuzhiyunCONFIG_KASAN_VMALLOC 344*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~ 345*4882a593Smuzhiyun 346*4882a593SmuzhiyunWith ``CONFIG_KASAN_VMALLOC``, KASAN can cover vmalloc space at the 347*4882a593Smuzhiyuncost of greater memory usage. Currently this is only supported on x86. 348*4882a593Smuzhiyun 349*4882a593SmuzhiyunThis works by hooking into vmalloc and vmap, and dynamically 350*4882a593Smuzhiyunallocating real shadow memory to back the mappings. 351*4882a593Smuzhiyun 352*4882a593SmuzhiyunMost mappings in vmalloc space are small, requiring less than a full 353*4882a593Smuzhiyunpage of shadow space. Allocating a full shadow page per mapping would 354*4882a593Smuzhiyuntherefore be wasteful. Furthermore, to ensure that different mappings 355*4882a593Smuzhiyunuse different shadow pages, mappings would have to be aligned to 356*4882a593Smuzhiyun``KASAN_GRANULE_SIZE * PAGE_SIZE``. 357*4882a593Smuzhiyun 358*4882a593SmuzhiyunInstead, KASAN shares backing space across multiple mappings. It allocates 359*4882a593Smuzhiyuna backing page when a mapping in vmalloc space uses a particular page 360*4882a593Smuzhiyunof the shadow region. This page can be shared by other vmalloc 361*4882a593Smuzhiyunmappings later on. 362*4882a593Smuzhiyun 363*4882a593SmuzhiyunKASAN hooks into the vmap infrastructure to lazily clean up unused shadow 364*4882a593Smuzhiyunmemory. 365*4882a593Smuzhiyun 366*4882a593SmuzhiyunTo avoid the difficulties around swapping mappings around, KASAN expects 367*4882a593Smuzhiyunthat the part of the shadow region that covers the vmalloc space will 368*4882a593Smuzhiyunnot be covered by the early shadow page, but will be left 369*4882a593Smuzhiyununmapped. This will require changes in arch-specific code. 370*4882a593Smuzhiyun 371*4882a593SmuzhiyunThis allows ``VMAP_STACK`` support on x86, and can simplify support of 372*4882a593Smuzhiyunarchitectures that do not have a fixed module region. 373*4882a593Smuzhiyun 374*4882a593SmuzhiyunCONFIG_KASAN_KUNIT_TEST and CONFIG_KASAN_MODULE_TEST 375*4882a593Smuzhiyun---------------------------------------------------- 376*4882a593Smuzhiyun 377*4882a593SmuzhiyunKASAN tests consist of two parts: 378*4882a593Smuzhiyun 379*4882a593Smuzhiyun1. Tests that are integrated with the KUnit Test Framework. Enabled with 380*4882a593Smuzhiyun``CONFIG_KASAN_KUNIT_TEST``. These tests can be run and partially verified 381*4882a593Smuzhiyunautomatically in a few different ways, see the instructions below. 382*4882a593Smuzhiyun 383*4882a593Smuzhiyun2. Tests that are currently incompatible with KUnit. Enabled with 384*4882a593Smuzhiyun``CONFIG_KASAN_MODULE_TEST`` and can only be run as a module. These tests can 385*4882a593Smuzhiyunonly be verified manually, by loading the kernel module and inspecting the 386*4882a593Smuzhiyunkernel log for KASAN reports. 387*4882a593Smuzhiyun 388*4882a593SmuzhiyunEach KUnit-compatible KASAN test prints a KASAN report if an error is detected. 389*4882a593SmuzhiyunThen the test prints its number and status. 390*4882a593Smuzhiyun 391*4882a593SmuzhiyunWhen a test passes:: 392*4882a593Smuzhiyun 393*4882a593Smuzhiyun ok 28 - kmalloc_double_kzfree 394*4882a593Smuzhiyun 395*4882a593SmuzhiyunWhen a test fails due to a failed ``kmalloc``:: 396*4882a593Smuzhiyun 397*4882a593Smuzhiyun # kmalloc_large_oob_right: ASSERTION FAILED at lib/test_kasan.c:163 398*4882a593Smuzhiyun Expected ptr is not null, but is 399*4882a593Smuzhiyun not ok 4 - kmalloc_large_oob_right 400*4882a593Smuzhiyun 401*4882a593SmuzhiyunWhen a test fails due to a missing KASAN report:: 402*4882a593Smuzhiyun 403*4882a593Smuzhiyun # kmalloc_double_kzfree: EXPECTATION FAILED at lib/test_kasan.c:629 404*4882a593Smuzhiyun Expected kasan_data->report_expected == kasan_data->report_found, but 405*4882a593Smuzhiyun kasan_data->report_expected == 1 406*4882a593Smuzhiyun kasan_data->report_found == 0 407*4882a593Smuzhiyun not ok 28 - kmalloc_double_kzfree 408*4882a593Smuzhiyun 409*4882a593SmuzhiyunAt the end the cumulative status of all KASAN tests is printed. On success:: 410*4882a593Smuzhiyun 411*4882a593Smuzhiyun ok 1 - kasan 412*4882a593Smuzhiyun 413*4882a593SmuzhiyunOr, if one of the tests failed:: 414*4882a593Smuzhiyun 415*4882a593Smuzhiyun not ok 1 - kasan 416*4882a593Smuzhiyun 417*4882a593Smuzhiyun 418*4882a593SmuzhiyunThere are a few ways to run KUnit-compatible KASAN tests. 419*4882a593Smuzhiyun 420*4882a593Smuzhiyun1. Loadable module 421*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~ 422*4882a593Smuzhiyun 423*4882a593SmuzhiyunWith ``CONFIG_KUNIT`` enabled, ``CONFIG_KASAN_KUNIT_TEST`` can be built as 424*4882a593Smuzhiyuna loadable module and run on any architecture that supports KASAN by loading 425*4882a593Smuzhiyunthe module with insmod or modprobe. The module is called ``test_kasan``. 426*4882a593Smuzhiyun 427*4882a593Smuzhiyun2. Built-In 428*4882a593Smuzhiyun~~~~~~~~~~~ 429*4882a593Smuzhiyun 430*4882a593SmuzhiyunWith ``CONFIG_KUNIT`` built-in, ``CONFIG_KASAN_KUNIT_TEST`` can be built-in 431*4882a593Smuzhiyunon any architecure that supports KASAN. These and any other KUnit tests enabled 432*4882a593Smuzhiyunwill run and print the results at boot as a late-init call. 433*4882a593Smuzhiyun 434*4882a593Smuzhiyun3. Using kunit_tool 435*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~ 436*4882a593Smuzhiyun 437*4882a593SmuzhiyunWith ``CONFIG_KUNIT`` and ``CONFIG_KASAN_KUNIT_TEST`` built-in, it's also 438*4882a593Smuzhiyunpossible use ``kunit_tool`` to see the results of these and other KUnit tests 439*4882a593Smuzhiyunin a more readable way. This will not print the KASAN reports of the tests that 440*4882a593Smuzhiyunpassed. Use `KUnit documentation <https://www.kernel.org/doc/html/latest/dev-tools/kunit/index.html>`_ 441*4882a593Smuzhiyunfor more up-to-date information on ``kunit_tool``. 442*4882a593Smuzhiyun 443*4882a593Smuzhiyun.. _KUnit: https://www.kernel.org/doc/html/latest/dev-tools/kunit/index.html 444