Jonathan Corbet | 2757aaf | 2016-08-07 15:31:03 -0600 | [diff] [blame] | 1 | The Kernel Address Sanitizer (KASAN) |
| 2 | ==================================== |
| 3 | |
| 4 | Overview |
| 5 | -------- |
| 6 | |
Andrey Konovalov | 625d867 | 2020-12-22 12:03:35 -0800 | [diff] [blame] | 7 | KernelAddressSANitizer (KASAN) is a dynamic memory safety error detector |
| 8 | designed to find out-of-bound and use-after-free bugs. KASAN has three modes: |
Jonathan Corbet | 2757aaf | 2016-08-07 15:31:03 -0600 | [diff] [blame] | 9 | |
Andrey Konovalov | 948e325 | 2020-12-22 12:02:24 -0800 | [diff] [blame] | 10 | 1. generic KASAN (similar to userspace ASan), |
| 11 | 2. software tag-based KASAN (similar to userspace HWASan), |
| 12 | 3. hardware tag-based KASAN (based on hardware memory tagging). |
Jonathan Corbet | 2757aaf | 2016-08-07 15:31:03 -0600 | [diff] [blame] | 13 | |
Andrey Konovalov | 948e325 | 2020-12-22 12:02:24 -0800 | [diff] [blame] | 14 | Software KASAN modes (1 and 2) use compile-time instrumentation to insert |
| 15 | validity checks before every memory access, and therefore require a compiler |
| 16 | version that supports that. |
Jonathan Corbet | 2757aaf | 2016-08-07 15:31:03 -0600 | [diff] [blame] | 17 | |
Andrey Konovalov | b3b0e6a | 2018-12-28 00:31:10 -0800 | [diff] [blame] | 18 | Generic KASAN is supported in both GCC and Clang. With GCC it requires version |
Marco Elver | 527f675 | 2020-10-13 16:47:51 -0700 | [diff] [blame] | 19 | 8.3.0 or later. Any supported Clang version is compatible, but detection of |
Marco Elver | ac4766b | 2020-08-06 23:24:31 -0700 | [diff] [blame] | 20 | out-of-bounds accesses for global variables is only supported since Clang 11. |
Andrey Konovalov | b3b0e6a | 2018-12-28 00:31:10 -0800 | [diff] [blame] | 21 | |
Marco Elver | 527f675 | 2020-10-13 16:47:51 -0700 | [diff] [blame] | 22 | Tag-based KASAN is only supported in Clang. |
Andrey Konovalov | b3b0e6a | 2018-12-28 00:31:10 -0800 | [diff] [blame] | 23 | |
Linus Walleij | 4210157 | 2020-10-25 23:56:18 +0100 | [diff] [blame] | 24 | Currently generic KASAN is supported for the x86_64, arm, arm64, xtensa, s390 |
Andrey Konovalov | 948e325 | 2020-12-22 12:02:24 -0800 | [diff] [blame] | 25 | and riscv architectures, and tag-based KASAN modes are supported only for arm64. |
Jonathan Corbet | 2757aaf | 2016-08-07 15:31:03 -0600 | [diff] [blame] | 26 | |
| 27 | Usage |
| 28 | ----- |
| 29 | |
| 30 | To enable KASAN configure kernel with:: |
| 31 | |
| 32 | CONFIG_KASAN = y |
| 33 | |
Andrey Konovalov | 948e325 | 2020-12-22 12:02:24 -0800 | [diff] [blame] | 34 | and choose between CONFIG_KASAN_GENERIC (to enable generic KASAN), |
| 35 | CONFIG_KASAN_SW_TAGS (to enable software tag-based KASAN), and |
| 36 | CONFIG_KASAN_HW_TAGS (to enable hardware tag-based KASAN). |
Jonathan Corbet | 2757aaf | 2016-08-07 15:31:03 -0600 | [diff] [blame] | 37 | |
Andrey Konovalov | 948e325 | 2020-12-22 12:02:24 -0800 | [diff] [blame] | 38 | For software modes, you also need to choose between CONFIG_KASAN_OUTLINE and |
| 39 | CONFIG_KASAN_INLINE. Outline and inline are compiler instrumentation types. |
| 40 | The former produces smaller binary while the latter is 1.1 - 2 times faster. |
Andrey Konovalov | b3b0e6a | 2018-12-28 00:31:10 -0800 | [diff] [blame] | 41 | |
Andrey Konovalov | 948e325 | 2020-12-22 12:02:24 -0800 | [diff] [blame] | 42 | Both software KASAN modes work with both SLUB and SLAB memory allocators, |
Andrey Konovalov | 625d867 | 2020-12-22 12:03:35 -0800 | [diff] [blame] | 43 | while the hardware tag-based KASAN currently only support SLUB. |
| 44 | |
| 45 | For better error reports that include stack traces, enable CONFIG_STACKTRACE. |
Jonathan Corbet | 2757aaf | 2016-08-07 15:31:03 -0600 | [diff] [blame] | 46 | |
Vlastimil Babka | 0fe9a44 | 2019-10-14 14:11:44 -0700 | [diff] [blame] | 47 | To augment reports with last allocation and freeing stack of the physical page, |
| 48 | it is recommended to enable also CONFIG_PAGE_OWNER and boot with page_owner=on. |
| 49 | |
Jonathan Corbet | 2757aaf | 2016-08-07 15:31:03 -0600 | [diff] [blame] | 50 | Error reports |
| 51 | ~~~~~~~~~~~~~ |
| 52 | |
Andrey Konovalov | b3b0e6a | 2018-12-28 00:31:10 -0800 | [diff] [blame] | 53 | A typical out-of-bounds access generic KASAN report looks like this:: |
Jonathan Corbet | 2757aaf | 2016-08-07 15:31:03 -0600 | [diff] [blame] | 54 | |
| 55 | ================================================================== |
Andrey Konovalov | b3b0e6a | 2018-12-28 00:31:10 -0800 | [diff] [blame] | 56 | BUG: KASAN: slab-out-of-bounds in kmalloc_oob_right+0xa8/0xbc [test_kasan] |
| 57 | Write of size 1 at addr ffff8801f44ec37b by task insmod/2760 |
Jonathan Corbet | 2757aaf | 2016-08-07 15:31:03 -0600 | [diff] [blame] | 58 | |
Andrey Konovalov | b3b0e6a | 2018-12-28 00:31:10 -0800 | [diff] [blame] | 59 | CPU: 1 PID: 2760 Comm: insmod Not tainted 4.19.0-rc3+ #698 |
| 60 | Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.10.2-1 04/01/2014 |
Jonathan Corbet | 2757aaf | 2016-08-07 15:31:03 -0600 | [diff] [blame] | 61 | Call Trace: |
Andrey Konovalov | b3b0e6a | 2018-12-28 00:31:10 -0800 | [diff] [blame] | 62 | dump_stack+0x94/0xd8 |
| 63 | print_address_description+0x73/0x280 |
| 64 | kasan_report+0x144/0x187 |
| 65 | __asan_report_store1_noabort+0x17/0x20 |
| 66 | kmalloc_oob_right+0xa8/0xbc [test_kasan] |
| 67 | kmalloc_tests_init+0x16/0x700 [test_kasan] |
| 68 | do_one_initcall+0xa5/0x3ae |
| 69 | do_init_module+0x1b6/0x547 |
| 70 | load_module+0x75df/0x8070 |
| 71 | __do_sys_init_module+0x1c6/0x200 |
| 72 | __x64_sys_init_module+0x6e/0xb0 |
| 73 | do_syscall_64+0x9f/0x2c0 |
| 74 | entry_SYSCALL_64_after_hwframe+0x44/0xa9 |
| 75 | RIP: 0033:0x7f96443109da |
| 76 | RSP: 002b:00007ffcf0b51b08 EFLAGS: 00000202 ORIG_RAX: 00000000000000af |
| 77 | RAX: ffffffffffffffda RBX: 000055dc3ee521a0 RCX: 00007f96443109da |
| 78 | RDX: 00007f96445cff88 RSI: 0000000000057a50 RDI: 00007f9644992000 |
| 79 | RBP: 000055dc3ee510b0 R08: 0000000000000003 R09: 0000000000000000 |
| 80 | R10: 00007f964430cd0a R11: 0000000000000202 R12: 00007f96445cff88 |
| 81 | R13: 000055dc3ee51090 R14: 0000000000000000 R15: 0000000000000000 |
| 82 | |
| 83 | Allocated by task 2760: |
| 84 | save_stack+0x43/0xd0 |
| 85 | kasan_kmalloc+0xa7/0xd0 |
| 86 | kmem_cache_alloc_trace+0xe1/0x1b0 |
| 87 | kmalloc_oob_right+0x56/0xbc [test_kasan] |
| 88 | kmalloc_tests_init+0x16/0x700 [test_kasan] |
| 89 | do_one_initcall+0xa5/0x3ae |
| 90 | do_init_module+0x1b6/0x547 |
| 91 | load_module+0x75df/0x8070 |
| 92 | __do_sys_init_module+0x1c6/0x200 |
| 93 | __x64_sys_init_module+0x6e/0xb0 |
| 94 | do_syscall_64+0x9f/0x2c0 |
| 95 | entry_SYSCALL_64_after_hwframe+0x44/0xa9 |
| 96 | |
| 97 | Freed by task 815: |
| 98 | save_stack+0x43/0xd0 |
| 99 | __kasan_slab_free+0x135/0x190 |
| 100 | kasan_slab_free+0xe/0x10 |
| 101 | kfree+0x93/0x1a0 |
| 102 | umh_complete+0x6a/0xa0 |
| 103 | call_usermodehelper_exec_async+0x4c3/0x640 |
| 104 | ret_from_fork+0x35/0x40 |
| 105 | |
| 106 | The buggy address belongs to the object at ffff8801f44ec300 |
| 107 | which belongs to the cache kmalloc-128 of size 128 |
| 108 | The buggy address is located 123 bytes inside of |
| 109 | 128-byte region [ffff8801f44ec300, ffff8801f44ec380) |
| 110 | The buggy address belongs to the page: |
| 111 | page:ffffea0007d13b00 count:1 mapcount:0 mapping:ffff8801f7001640 index:0x0 |
| 112 | flags: 0x200000000000100(slab) |
| 113 | raw: 0200000000000100 ffffea0007d11dc0 0000001a0000001a ffff8801f7001640 |
| 114 | raw: 0000000000000000 0000000080150015 00000001ffffffff 0000000000000000 |
| 115 | page dumped because: kasan: bad access detected |
| 116 | |
Jonathan Corbet | 2757aaf | 2016-08-07 15:31:03 -0600 | [diff] [blame] | 117 | Memory state around the buggy address: |
Andrey Konovalov | b3b0e6a | 2018-12-28 00:31:10 -0800 | [diff] [blame] | 118 | ffff8801f44ec200: fc fc fc fc fc fc fc fc fb fb fb fb fb fb fb fb |
| 119 | ffff8801f44ec280: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc |
| 120 | >ffff8801f44ec300: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 03 |
| 121 | ^ |
| 122 | ffff8801f44ec380: fc fc fc fc fc fc fc fc fb fb fb fb fb fb fb fb |
| 123 | ffff8801f44ec400: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc |
Jonathan Corbet | 2757aaf | 2016-08-07 15:31:03 -0600 | [diff] [blame] | 124 | ================================================================== |
| 125 | |
Andrey Konovalov | b3b0e6a | 2018-12-28 00:31:10 -0800 | [diff] [blame] | 126 | The header of the report provides a short summary of what kind of bug happened |
| 127 | and what kind of access caused it. It's followed by a stack trace of the bad |
| 128 | access, a stack trace of where the accessed memory was allocated (in case bad |
| 129 | access happens on a slab object), and a stack trace of where the object was |
| 130 | freed (in case of a use-after-free bug report). Next comes a description of |
| 131 | the accessed slab object and information about the accessed memory page. |
Jonathan Corbet | 2757aaf | 2016-08-07 15:31:03 -0600 | [diff] [blame] | 132 | |
| 133 | In the last section the report shows memory state around the accessed address. |
Andrey Konovalov | 625d867 | 2020-12-22 12:03:35 -0800 | [diff] [blame] | 134 | Internally KASAN tracks memory state separately for each memory granule, which |
| 135 | is either 8 or 16 aligned bytes depending on KASAN mode. Each number in the |
| 136 | memory state section of the report shows the state of one of the memory |
| 137 | granules that surround the accessed address. |
Jonathan Corbet | 2757aaf | 2016-08-07 15:31:03 -0600 | [diff] [blame] | 138 | |
Andrey Konovalov | 625d867 | 2020-12-22 12:03:35 -0800 | [diff] [blame] | 139 | For generic KASAN the size of each memory granule is 8. The state of each |
| 140 | granule is encoded in one shadow byte. Those 8 bytes can be accessible, |
| 141 | partially accessible, freed or be a part of a redzone. KASAN uses the following |
| 142 | encoding for each shadow byte: 0 means that all 8 bytes of the corresponding |
| 143 | memory region are accessible; number N (1 <= N <= 7) means that the first N |
| 144 | bytes are accessible, and other (8 - N) bytes are not; any negative value |
| 145 | indicates that the entire 8-byte word is inaccessible. KASAN uses different |
| 146 | negative values to distinguish between different kinds of inaccessible memory |
| 147 | like redzones or freed memory (see mm/kasan/kasan.h). |
Jonathan Corbet | 2757aaf | 2016-08-07 15:31:03 -0600 | [diff] [blame] | 148 | |
| 149 | In the report above the arrows point to the shadow byte 03, which means that |
| 150 | the accessed address is partially accessible. |
| 151 | |
Andrey Konovalov | b3b0e6a | 2018-12-28 00:31:10 -0800 | [diff] [blame] | 152 | For tag-based KASAN this last report section shows the memory tags around the |
Andrey Konovalov | 625d867 | 2020-12-22 12:03:35 -0800 | [diff] [blame] | 153 | accessed address (see `Implementation details`_ section). |
| 154 | |
| 155 | Boot parameters |
| 156 | ~~~~~~~~~~~~~~~ |
| 157 | |
| 158 | Hardware tag-based KASAN mode (see the section about different mode below) is |
| 159 | intended for use in production as a security mitigation. Therefore it supports |
| 160 | boot parameters that allow to disable KASAN competely or otherwise control |
| 161 | particular KASAN features. |
| 162 | |
Andrey Konovalov | 76bc99e | 2021-01-23 21:01:34 -0800 | [diff] [blame] | 163 | - ``kasan=off`` or ``=on`` controls whether KASAN is enabled (default: ``on``). |
Andrey Konovalov | 625d867 | 2020-12-22 12:03:35 -0800 | [diff] [blame] | 164 | |
Andrey Konovalov | 76bc99e | 2021-01-23 21:01:34 -0800 | [diff] [blame] | 165 | - ``kasan.stacktrace=off`` or ``=on`` disables or enables alloc and free stack |
Andrey Konovalov | 1cc4cdb | 2021-02-09 13:42:03 -0800 | [diff] [blame] | 166 | traces collection (default: ``on``). |
Andrey Konovalov | 625d867 | 2020-12-22 12:03:35 -0800 | [diff] [blame] | 167 | |
Andrey Konovalov | 76bc99e | 2021-01-23 21:01:34 -0800 | [diff] [blame] | 168 | - ``kasan.fault=report`` or ``=panic`` controls whether to only print a KASAN |
| 169 | report or also panic the kernel (default: ``report``). |
Andrey Konovalov | 625d867 | 2020-12-22 12:03:35 -0800 | [diff] [blame] | 170 | |
| 171 | For developers |
| 172 | ~~~~~~~~~~~~~~ |
| 173 | |
| 174 | Software KASAN modes use compiler instrumentation to insert validity checks. |
| 175 | Such instrumentation might be incompatible with some part of the kernel, and |
| 176 | therefore needs to be disabled. To disable instrumentation for specific files |
| 177 | or directories, add a line similar to the following to the respective kernel |
| 178 | Makefile: |
| 179 | |
| 180 | - For a single file (e.g. main.o):: |
| 181 | |
| 182 | KASAN_SANITIZE_main.o := n |
| 183 | |
| 184 | - For all files in one directory:: |
| 185 | |
| 186 | KASAN_SANITIZE := n |
Andrey Konovalov | b3b0e6a | 2018-12-28 00:31:10 -0800 | [diff] [blame] | 187 | |
Jonathan Corbet | 2757aaf | 2016-08-07 15:31:03 -0600 | [diff] [blame] | 188 | |
| 189 | Implementation details |
| 190 | ---------------------- |
| 191 | |
Andrey Konovalov | b3b0e6a | 2018-12-28 00:31:10 -0800 | [diff] [blame] | 192 | Generic KASAN |
| 193 | ~~~~~~~~~~~~~ |
| 194 | |
Andrey Konovalov | 625d867 | 2020-12-22 12:03:35 -0800 | [diff] [blame] | 195 | From a high level perspective, KASAN's approach to memory error detection is |
| 196 | similar to that of kmemcheck: use shadow memory to record whether each byte of |
| 197 | memory is safe to access, and use compile-time instrumentation to insert checks |
| 198 | of shadow memory on each memory access. |
Jonathan Corbet | 2757aaf | 2016-08-07 15:31:03 -0600 | [diff] [blame] | 199 | |
Andrey Konovalov | b3b0e6a | 2018-12-28 00:31:10 -0800 | [diff] [blame] | 200 | Generic KASAN dedicates 1/8th of kernel memory to its shadow memory (e.g. 16TB |
| 201 | to cover 128TB on x86_64) and uses direct mapping with a scale and offset to |
| 202 | translate a memory address to its corresponding shadow address. |
Jonathan Corbet | 2757aaf | 2016-08-07 15:31:03 -0600 | [diff] [blame] | 203 | |
| 204 | Here is the function which translates an address to its corresponding shadow |
| 205 | address:: |
| 206 | |
| 207 | static inline void *kasan_mem_to_shadow(const void *addr) |
| 208 | { |
| 209 | return ((unsigned long)addr >> KASAN_SHADOW_SCALE_SHIFT) |
| 210 | + KASAN_SHADOW_OFFSET; |
| 211 | } |
| 212 | |
| 213 | where ``KASAN_SHADOW_SCALE_SHIFT = 3``. |
| 214 | |
Andrey Konovalov | b3b0e6a | 2018-12-28 00:31:10 -0800 | [diff] [blame] | 215 | Compile-time instrumentation is used to insert memory access checks. Compiler |
| 216 | inserts function calls (__asan_load*(addr), __asan_store*(addr)) before each |
| 217 | memory access of size 1, 2, 4, 8 or 16. These functions check whether memory |
| 218 | access is valid or not by checking corresponding shadow memory. |
Jonathan Corbet | 2757aaf | 2016-08-07 15:31:03 -0600 | [diff] [blame] | 219 | |
| 220 | GCC 5.0 has possibility to perform inline instrumentation. Instead of making |
| 221 | function calls GCC directly inserts the code to check the shadow memory. |
| 222 | This option significantly enlarges kernel but it gives x1.1-x2 performance |
| 223 | boost over outline instrumented kernel. |
Andrey Konovalov | b3b0e6a | 2018-12-28 00:31:10 -0800 | [diff] [blame] | 224 | |
Walter Wu | 4784be2 | 2020-12-14 19:09:21 -0800 | [diff] [blame] | 225 | Generic KASAN also reports the last 2 call stacks to creation of work that |
| 226 | potentially has access to an object. Call stacks for the following are shown: |
| 227 | call_rcu() and workqueue queuing. |
Walter Wu | 9793b62 | 2020-08-06 23:24:46 -0700 | [diff] [blame] | 228 | |
Andrey Konovalov | 625d867 | 2020-12-22 12:03:35 -0800 | [diff] [blame] | 229 | Generic KASAN is the only mode that delays the reuse of freed object via |
| 230 | quarantine (see mm/kasan/quarantine.c for implementation). |
| 231 | |
Andrey Konovalov | b3b0e6a | 2018-12-28 00:31:10 -0800 | [diff] [blame] | 232 | Software tag-based KASAN |
| 233 | ~~~~~~~~~~~~~~~~~~~~~~~~ |
| 234 | |
Andrey Konovalov | 948e325 | 2020-12-22 12:02:24 -0800 | [diff] [blame] | 235 | Software tag-based KASAN requires software memory tagging support in the form |
| 236 | of HWASan-like compiler instrumentation (see HWASan documentation for details). |
| 237 | |
| 238 | Software tag-based KASAN is currently only implemented for arm64 architecture. |
| 239 | |
| 240 | Software tag-based KASAN uses the Top Byte Ignore (TBI) feature of arm64 CPUs |
| 241 | to store a pointer tag in the top byte of kernel pointers. Like generic KASAN |
| 242 | it uses shadow memory to store memory tags associated with each 16-byte memory |
Andrey Konovalov | b3b0e6a | 2018-12-28 00:31:10 -0800 | [diff] [blame] | 243 | cell (therefore it dedicates 1/16th of the kernel memory for shadow memory). |
| 244 | |
Andrey Konovalov | 948e325 | 2020-12-22 12:02:24 -0800 | [diff] [blame] | 245 | On each memory allocation software tag-based KASAN generates a random tag, tags |
| 246 | the allocated memory with this tag, and embeds this tag into the returned |
| 247 | pointer. |
| 248 | |
Andrey Konovalov | b3b0e6a | 2018-12-28 00:31:10 -0800 | [diff] [blame] | 249 | Software tag-based KASAN uses compile-time instrumentation to insert checks |
| 250 | before each memory access. These checks make sure that tag of the memory that |
| 251 | is being accessed is equal to tag of the pointer that is used to access this |
Andrey Konovalov | 948e325 | 2020-12-22 12:02:24 -0800 | [diff] [blame] | 252 | memory. In case of a tag mismatch software tag-based KASAN prints a bug report. |
Andrey Konovalov | b3b0e6a | 2018-12-28 00:31:10 -0800 | [diff] [blame] | 253 | |
| 254 | Software tag-based KASAN also has two instrumentation modes (outline, that |
| 255 | emits callbacks to check memory accesses; and inline, that performs the shadow |
| 256 | memory checks inline). With outline instrumentation mode, a bug report is |
| 257 | simply printed from the function that performs the access check. With inline |
| 258 | instrumentation a brk instruction is emitted by the compiler, and a dedicated |
| 259 | brk handler is used to print bug reports. |
| 260 | |
Andrey Konovalov | 948e325 | 2020-12-22 12:02:24 -0800 | [diff] [blame] | 261 | Software tag-based KASAN uses 0xFF as a match-all pointer tag (accesses through |
| 262 | pointers with 0xFF pointer tag aren't checked). The value 0xFE is currently |
| 263 | reserved to tag freed memory regions. |
| 264 | |
| 265 | Software tag-based KASAN currently only supports tagging of |
| 266 | kmem_cache_alloc/kmalloc and page_alloc memory. |
| 267 | |
| 268 | Hardware tag-based KASAN |
| 269 | ~~~~~~~~~~~~~~~~~~~~~~~~ |
| 270 | |
| 271 | Hardware tag-based KASAN is similar to the software mode in concept, but uses |
| 272 | hardware memory tagging support instead of compiler instrumentation and |
| 273 | shadow memory. |
| 274 | |
| 275 | Hardware tag-based KASAN is currently only implemented for arm64 architecture |
| 276 | and based on both arm64 Memory Tagging Extension (MTE) introduced in ARMv8.5 |
| 277 | Instruction Set Architecture, and Top Byte Ignore (TBI). |
| 278 | |
| 279 | Special arm64 instructions are used to assign memory tags for each allocation. |
| 280 | Same tags are assigned to pointers to those allocations. On every memory |
| 281 | access, hardware makes sure that tag of the memory that is being accessed is |
| 282 | equal to tag of the pointer that is used to access this memory. In case of a |
| 283 | tag mismatch a fault is generated and a report is printed. |
| 284 | |
| 285 | Hardware tag-based KASAN uses 0xFF as a match-all pointer tag (accesses through |
| 286 | pointers with 0xFF pointer tag aren't checked). The value 0xFE is currently |
| 287 | reserved to tag freed memory regions. |
| 288 | |
| 289 | Hardware tag-based KASAN currently only supports tagging of |
| 290 | kmem_cache_alloc/kmalloc and page_alloc memory. |
Daniel Axtens | 3c5c3cf | 2019-11-30 17:54:50 -0800 | [diff] [blame] | 291 | |
| 292 | What memory accesses are sanitised by KASAN? |
| 293 | -------------------------------------------- |
| 294 | |
| 295 | The kernel maps memory in a number of different parts of the address |
| 296 | space. This poses something of a problem for KASAN, which requires |
| 297 | that all addresses accessed by instrumented code have a valid shadow |
| 298 | region. |
| 299 | |
| 300 | The range of kernel virtual addresses is large: there is not enough |
| 301 | real memory to support a real shadow region for every address that |
| 302 | could be accessed by the kernel. |
| 303 | |
| 304 | By default |
| 305 | ~~~~~~~~~~ |
| 306 | |
| 307 | By default, architectures only map real memory over the shadow region |
| 308 | for the linear mapping (and potentially other small areas). For all |
| 309 | other areas - such as vmalloc and vmemmap space - a single read-only |
| 310 | page is mapped over the shadow area. This read-only shadow page |
| 311 | declares all memory accesses as permitted. |
| 312 | |
| 313 | This presents a problem for modules: they do not live in the linear |
| 314 | mapping, but in a dedicated module space. By hooking in to the module |
| 315 | allocator, KASAN can temporarily map real shadow memory to cover |
| 316 | them. This allows detection of invalid accesses to module globals, for |
| 317 | example. |
| 318 | |
| 319 | This also creates an incompatibility with ``VMAP_STACK``: if the stack |
| 320 | lives in vmalloc space, it will be shadowed by the read-only page, and |
| 321 | the kernel will fault when trying to set up the shadow data for stack |
| 322 | variables. |
| 323 | |
| 324 | CONFIG_KASAN_VMALLOC |
| 325 | ~~~~~~~~~~~~~~~~~~~~ |
| 326 | |
| 327 | With ``CONFIG_KASAN_VMALLOC``, KASAN can cover vmalloc space at the |
| 328 | cost of greater memory usage. Currently this is only supported on x86. |
| 329 | |
| 330 | This works by hooking into vmalloc and vmap, and dynamically |
| 331 | allocating real shadow memory to back the mappings. |
| 332 | |
| 333 | Most mappings in vmalloc space are small, requiring less than a full |
| 334 | page of shadow space. Allocating a full shadow page per mapping would |
| 335 | therefore be wasteful. Furthermore, to ensure that different mappings |
| 336 | use different shadow pages, mappings would have to be aligned to |
Andrey Konovalov | 1f60062 | 2020-12-22 12:00:24 -0800 | [diff] [blame] | 337 | ``KASAN_GRANULE_SIZE * PAGE_SIZE``. |
Daniel Axtens | 3c5c3cf | 2019-11-30 17:54:50 -0800 | [diff] [blame] | 338 | |
Andrey Konovalov | 625d867 | 2020-12-22 12:03:35 -0800 | [diff] [blame] | 339 | Instead, KASAN shares backing space across multiple mappings. It allocates |
Daniel Axtens | 3c5c3cf | 2019-11-30 17:54:50 -0800 | [diff] [blame] | 340 | a backing page when a mapping in vmalloc space uses a particular page |
| 341 | of the shadow region. This page can be shared by other vmalloc |
| 342 | mappings later on. |
| 343 | |
Andrey Konovalov | 625d867 | 2020-12-22 12:03:35 -0800 | [diff] [blame] | 344 | KASAN hooks into the vmap infrastructure to lazily clean up unused shadow |
Daniel Axtens | 3c5c3cf | 2019-11-30 17:54:50 -0800 | [diff] [blame] | 345 | memory. |
| 346 | |
Andrey Konovalov | 625d867 | 2020-12-22 12:03:35 -0800 | [diff] [blame] | 347 | To avoid the difficulties around swapping mappings around, KASAN expects |
Daniel Axtens | 3c5c3cf | 2019-11-30 17:54:50 -0800 | [diff] [blame] | 348 | that the part of the shadow region that covers the vmalloc space will |
| 349 | not be covered by the early shadow page, but will be left |
| 350 | unmapped. This will require changes in arch-specific code. |
| 351 | |
| 352 | This allows ``VMAP_STACK`` support on x86, and can simplify support of |
| 353 | architectures that do not have a fixed module region. |
Patricia Alfonso | 9ab5be9 | 2020-10-13 16:55:09 -0700 | [diff] [blame] | 354 | |
| 355 | CONFIG_KASAN_KUNIT_TEST & CONFIG_TEST_KASAN_MODULE |
| 356 | -------------------------------------------------- |
| 357 | |
Andrey Konovalov | 625d867 | 2020-12-22 12:03:35 -0800 | [diff] [blame] | 358 | KASAN tests consist on two parts: |
Patricia Alfonso | 9ab5be9 | 2020-10-13 16:55:09 -0700 | [diff] [blame] | 359 | |
Andrey Konovalov | 625d867 | 2020-12-22 12:03:35 -0800 | [diff] [blame] | 360 | 1. Tests that are integrated with the KUnit Test Framework. Enabled with |
| 361 | ``CONFIG_KASAN_KUNIT_TEST``. These tests can be run and partially verified |
| 362 | automatically in a few different ways, see the instructions below. |
Patricia Alfonso | 9ab5be9 | 2020-10-13 16:55:09 -0700 | [diff] [blame] | 363 | |
Andrey Konovalov | 625d867 | 2020-12-22 12:03:35 -0800 | [diff] [blame] | 364 | 2. Tests that are currently incompatible with KUnit. Enabled with |
| 365 | ``CONFIG_TEST_KASAN_MODULE`` and can only be run as a module. These tests can |
| 366 | only be verified manually, by loading the kernel module and inspecting the |
| 367 | kernel log for KASAN reports. |
| 368 | |
| 369 | Each KUnit-compatible KASAN test prints a KASAN report if an error is detected. |
| 370 | Then the test prints its number and status. |
| 371 | |
| 372 | When a test passes:: |
Patricia Alfonso | 9ab5be9 | 2020-10-13 16:55:09 -0700 | [diff] [blame] | 373 | |
| 374 | ok 28 - kmalloc_double_kzfree |
Mauro Carvalho Chehab | 32519c0 | 2020-10-27 10:51:09 +0100 | [diff] [blame] | 375 | |
Andrey Konovalov | 625d867 | 2020-12-22 12:03:35 -0800 | [diff] [blame] | 376 | When a test fails due to a failed ``kmalloc``:: |
Patricia Alfonso | 9ab5be9 | 2020-10-13 16:55:09 -0700 | [diff] [blame] | 377 | |
| 378 | # kmalloc_large_oob_right: ASSERTION FAILED at lib/test_kasan.c:163 |
| 379 | Expected ptr is not null, but is |
| 380 | not ok 4 - kmalloc_large_oob_right |
Mauro Carvalho Chehab | 32519c0 | 2020-10-27 10:51:09 +0100 | [diff] [blame] | 381 | |
Andrey Konovalov | 625d867 | 2020-12-22 12:03:35 -0800 | [diff] [blame] | 382 | When a test fails due to a missing KASAN report:: |
Patricia Alfonso | 9ab5be9 | 2020-10-13 16:55:09 -0700 | [diff] [blame] | 383 | |
| 384 | # kmalloc_double_kzfree: EXPECTATION FAILED at lib/test_kasan.c:629 |
| 385 | Expected kasan_data->report_expected == kasan_data->report_found, but |
| 386 | kasan_data->report_expected == 1 |
| 387 | kasan_data->report_found == 0 |
| 388 | not ok 28 - kmalloc_double_kzfree |
| 389 | |
Andrey Konovalov | 625d867 | 2020-12-22 12:03:35 -0800 | [diff] [blame] | 390 | At the end the cumulative status of all KASAN tests is printed. On success:: |
Patricia Alfonso | 9ab5be9 | 2020-10-13 16:55:09 -0700 | [diff] [blame] | 391 | |
| 392 | ok 1 - kasan |
| 393 | |
Andrey Konovalov | 625d867 | 2020-12-22 12:03:35 -0800 | [diff] [blame] | 394 | Or, if one of the tests failed:: |
Patricia Alfonso | 9ab5be9 | 2020-10-13 16:55:09 -0700 | [diff] [blame] | 395 | |
| 396 | not ok 1 - kasan |
| 397 | |
Andrey Konovalov | 625d867 | 2020-12-22 12:03:35 -0800 | [diff] [blame] | 398 | |
| 399 | There are a few ways to run KUnit-compatible KASAN tests. |
| 400 | |
| 401 | 1. Loadable module |
| 402 | ~~~~~~~~~~~~~~~~~~ |
Patricia Alfonso | 9ab5be9 | 2020-10-13 16:55:09 -0700 | [diff] [blame] | 403 | |
| 404 | With ``CONFIG_KUNIT`` enabled, ``CONFIG_KASAN_KUNIT_TEST`` can be built as |
Andrey Konovalov | 625d867 | 2020-12-22 12:03:35 -0800 | [diff] [blame] | 405 | a loadable module and run on any architecture that supports KASAN by loading |
| 406 | the module with insmod or modprobe. The module is called ``test_kasan``. |
Patricia Alfonso | 9ab5be9 | 2020-10-13 16:55:09 -0700 | [diff] [blame] | 407 | |
Andrey Konovalov | 625d867 | 2020-12-22 12:03:35 -0800 | [diff] [blame] | 408 | 2. Built-In |
| 409 | ~~~~~~~~~~~ |
Patricia Alfonso | 9ab5be9 | 2020-10-13 16:55:09 -0700 | [diff] [blame] | 410 | |
| 411 | With ``CONFIG_KUNIT`` built-in, ``CONFIG_KASAN_KUNIT_TEST`` can be built-in |
Andrey Konovalov | 625d867 | 2020-12-22 12:03:35 -0800 | [diff] [blame] | 412 | on any architecure that supports KASAN. These and any other KUnit tests enabled |
| 413 | will run and print the results at boot as a late-init call. |
Patricia Alfonso | 9ab5be9 | 2020-10-13 16:55:09 -0700 | [diff] [blame] | 414 | |
Andrey Konovalov | 625d867 | 2020-12-22 12:03:35 -0800 | [diff] [blame] | 415 | 3. Using kunit_tool |
| 416 | ~~~~~~~~~~~~~~~~~~~ |
Patricia Alfonso | 9ab5be9 | 2020-10-13 16:55:09 -0700 | [diff] [blame] | 417 | |
Andrey Konovalov | 625d867 | 2020-12-22 12:03:35 -0800 | [diff] [blame] | 418 | With ``CONFIG_KUNIT`` and ``CONFIG_KASAN_KUNIT_TEST`` built-in, it's also |
| 419 | possible use ``kunit_tool`` to see the results of these and other KUnit tests |
| 420 | in a more readable way. This will not print the KASAN reports of the tests that |
| 421 | passed. Use `KUnit documentation <https://www.kernel.org/doc/html/latest/dev-tools/kunit/index.html>`_ |
| 422 | for more up-to-date information on ``kunit_tool``. |
Patricia Alfonso | 9ab5be9 | 2020-10-13 16:55:09 -0700 | [diff] [blame] | 423 | |
| 424 | .. _KUnit: https://www.kernel.org/doc/html/latest/dev-tools/kunit/index.html |