1.. SPDX-License-Identifier: GPL-2.0 2 3.. include:: ../disclaimer-zh_TW.rst 4 5:Original: Documentation/dev-tools/kasan.rst 6:Translator: ��������� Wan Jiabing <wanjiabing@vivo.com> 7 8���������������������(KASAN) 9===================== 10 11������ 12---- 13 14Kernel Address SANitizer(KASAN)��������������������������������������������������������������� 15������������������������������������������������������������ 16 17KASAN���������������: 18 191. ������KASAN 202. ���������������������KASAN 213. ���������������������KASAN 22 23���CONFIG_KASAN_GENERIC���������������KASAN������������������������������������������������ 24������ASan������������������������CPU������������������������������������������������������������������ 25 26���������������������KASAN���SW_TAGS KASAN���������CONFIG_KASAN_SW_TAGS��������� 27���������������������������������������������������������HWASan������������������������arm64��������� 28��������������������������������������������������������������������������������������������� 29 30���������������������KASAN���HW_TAGS KASAN������CONFIG_KASAN_HW_TAGS������������ 31������������������������������������������������������������������������������������������MTE��������������� 32������������arm64 CPU������������������������������������������������������������������������������������ 33 34������������KASAN������������������������������������������������������������Kconfig������������������ 35 36������������������������������������������������������������������������������������������������������������ 37������������������������������������������������������ 38 39������ 40---- 41 42������������ 43~~~~~~~~ 44 45���x86_64���arm���arm64���powerpc���riscv���s390���xtensa���loongarch���������������KASAN��� 46������������������KASAN������������arm64������������ 47 48��������� 49~~~~~~ 50 51������KASAN������������������������������������������������������������������������������������������������ 52��������������������������������������������������������������������������������������������������������������� 53��������������������������������������������������� 54 55������KASAN������GCC 8.3.0���������������������������������������������������Clang��������� 56 57���������������������KASAN������GCC 11+���������������������������Clang��������� 58 59���������������������KASAN������GCC 10+���Clang 12+��� 60 61������������ 62~~~~~~~~ 63 64������KASAN������������������slab���page_alloc���vmap���vmalloc������������������������ 65������������������ 66 67���������������������KASAN������slab���page_alloc���vmalloc������������������ 68 69���������������������KASAN������slab���page_alloc������������������vmalloc��������� 70 71������slab���������������KASAN���������������SLUB���SLAB������������������������������������ 72KASAN���������SLUB��� 73 74������ 75---- 76 77���������KASAN������������������������������������:: 78 79 CONFIG_KASAN=y 80 81��������� ``CONFIG_KASAN_GENERIC`` (������������KASAN������)��� ``CONFIG_KASAN_SW_TAGS`` 82(���������������������������KASAN������)������ ``CONFIG_KASAN_HW_TAGS`` (������������������������ 83���KASAN������)��������������������� 84 85��������������������������������� ``CONFIG_KASAN_OUTLINE`` ��� ``CONFIG_KASAN_INLINE`` 86���������������������outline���inline������������������������������������������������������������������ 87������������2������ 88 89������������������slab���������alloc���free������������������������������������������ 90``CONFIG_STACKTRACE`` ������������������������������������������������������������������������ 91��������� ``CONFIG_PAGE_OWNER`` ��������� ``page_owner=on`` ��������������� 92 93������������ 94~~~~~~~~ 95 96KASAN������������ ``panic_on_warn`` ������������������������������������������������KASAN 97������������������������������������������������ 98 99������������������KASAN��������������������������������������������������������������� 100``kasan_multi_shot``���KASAN��������������������������������������������������������������� 101���KASAN��������� ``panic_on_warn``��� 102 103������������������ ``panic_on_warn`` ��� ``kasan.fault=`` boot��������������� 104��������������������������������� 105 106- ``kasan.fault=report`` ��� ``=panic`` ���������������������KASAN report��� 107 ��������������������������������� ``report`` ������������ ``kasan_multi_shot`` ��� 108 ������������������������������ 109 110������������������������������KASAN��������������������������������������������������������������������� 111������������������ 112 113- ``kasan.stacktrace=off`` ��� ``=on`` ��������������������������������������� 114 ������������������������ ``on`` ������ 115 116- ``kasan.stack_ring_size=<number of entries>`` ��������������������� 117 ������������������ ``32768`` ������ 118 119���������������������KASAN������������������������������������������������������������������������������ 120������������������������������������������������KASAN��������������������� 121 122- ``kasan=off`` ��� ``=on`` ������KASAN��������������������������� ``on`` ������ 123 124- ``kasan.mode=sync``, ``=async`` or ``=asymm`` ������KASAN������ 125 ������������������������������������������������������������������ ``������`` ������ 126 ��������������������������������������������������������������������������������� 127 ��������������������������������������������������������������������������������������������������������� 128 ���������������arm64������������TFSR_EL1������������������������������������������������������\ 129 ��������������������������������������������� 130 ������������������������������������������������������������������������������ 131 132- ``kasan.vmalloc=off`` or ``=on`` ���������������vmalloc��������������������������� ``on`` ������ 133 134������������ 135~~~~~~~~ 136 137���������KASAN������������������:: 138 139 ================================================================== 140 BUG: KASAN: slab-out-of-bounds in kmalloc_oob_right+0xa8/0xbc [kasan_test] 141 Write of size 1 at addr ffff8801f44ec37b by task insmod/2760 142 143 CPU: 1 PID: 2760 Comm: insmod Not tainted 4.19.0-rc3+ #698 144 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.10.2-1 04/01/2014 145 Call Trace: 146 dump_stack+0x94/0xd8 147 print_address_description+0x73/0x280 148 kasan_report+0x144/0x187 149 __asan_report_store1_noabort+0x17/0x20 150 kmalloc_oob_right+0xa8/0xbc [kasan_test] 151 kmalloc_tests_init+0x16/0x700 [kasan_test] 152 do_one_initcall+0xa5/0x3ae 153 do_init_module+0x1b6/0x547 154 load_module+0x75df/0x8070 155 __do_sys_init_module+0x1c6/0x200 156 __x64_sys_init_module+0x6e/0xb0 157 do_syscall_64+0x9f/0x2c0 158 entry_SYSCALL_64_after_hwframe+0x44/0xa9 159 RIP: 0033:0x7f96443109da 160 RSP: 002b:00007ffcf0b51b08 EFLAGS: 00000202 ORIG_RAX: 00000000000000af 161 RAX: ffffffffffffffda RBX: 000055dc3ee521a0 RCX: 00007f96443109da 162 RDX: 00007f96445cff88 RSI: 0000000000057a50 RDI: 00007f9644992000 163 RBP: 000055dc3ee510b0 R08: 0000000000000003 R09: 0000000000000000 164 R10: 00007f964430cd0a R11: 0000000000000202 R12: 00007f96445cff88 165 R13: 000055dc3ee51090 R14: 0000000000000000 R15: 0000000000000000 166 167 Allocated by task 2760: 168 save_stack+0x43/0xd0 169 kasan_kmalloc+0xa7/0xd0 170 kmem_cache_alloc_trace+0xe1/0x1b0 171 kmalloc_oob_right+0x56/0xbc [kasan_test] 172 kmalloc_tests_init+0x16/0x700 [kasan_test] 173 do_one_initcall+0xa5/0x3ae 174 do_init_module+0x1b6/0x547 175 load_module+0x75df/0x8070 176 __do_sys_init_module+0x1c6/0x200 177 __x64_sys_init_module+0x6e/0xb0 178 do_syscall_64+0x9f/0x2c0 179 entry_SYSCALL_64_after_hwframe+0x44/0xa9 180 181 Freed by task 815: 182 save_stack+0x43/0xd0 183 __kasan_slab_free+0x135/0x190 184 kasan_slab_free+0xe/0x10 185 kfree+0x93/0x1a0 186 umh_complete+0x6a/0xa0 187 call_usermodehelper_exec_async+0x4c3/0x640 188 ret_from_fork+0x35/0x40 189 190 The buggy address belongs to the object at ffff8801f44ec300 191 which belongs to the cache kmalloc-128 of size 128 192 The buggy address is located 123 bytes inside of 193 128-byte region [ffff8801f44ec300, ffff8801f44ec380) 194 The buggy address belongs to the page: 195 page:ffffea0007d13b00 count:1 mapcount:0 mapping:ffff8801f7001640 index:0x0 196 flags: 0x200000000000100(slab) 197 raw: 0200000000000100 ffffea0007d11dc0 0000001a0000001a ffff8801f7001640 198 raw: 0000000000000000 0000000080150015 00000001ffffffff 0000000000000000 199 page dumped because: kasan: bad access detected 200 201 Memory state around the buggy address: 202 ffff8801f44ec200: fc fc fc fc fc fc fc fc fb fb fb fb fb fb fb fb 203 ffff8801f44ec280: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc 204 >ffff8801f44ec300: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 03 205 ^ 206 ffff8801f44ec380: fc fc fc fc fc fc fc fc fb fb fb fb fb fb fb fb 207 ffff8801f44ec400: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc 208 ================================================================== 209 210������������������������������������������������������������������������������������������������������������������ 211���������������������������������������������������������������������������slab������������������������������ 212������������������������������������������������������������������������������������������������������������ 213slab��������������������������������������������������������� 214 215������������������������������������������������������������������������KASAN��������������������������������� 216���������������������KASAN������������8���16��������������������������������������������������������������� 217��������������������������������������������������������������������� 218 219������������KASAN���������������������������������8��������������������������������������������������������������� 220���������8������������������������������������������������������������������������Redzone���������������KASAN 221���������������������������������������:00���������������������������������8���������������������������������N 222(1 <= N <= 7)���������N���������������������������(8 - N)��������������������������������������������� 223������������������8���������KASAN���������������������������������������������������������������������redzones 224������������������������������ mm/kasan/kasan.h������ 225 226������������������������������������������������ ``03`` ������������������������������������������������ 227 228���������������������KASAN������������������������������������������������������������������������ 229(������ `������������`_ ������)��� 230 231������������KASAN������������������ ``slab-out-of-bounds`` ��� ``use-after-free`` ��� 232������������������:KASAN��������������������������������������������������������������������������������������� 233������������������������ 234 235������KASAN������������������������������������������������������������������������������������������������������ 236������������������������������������������������������������������ call_rcu() ��������������������������� 237 238������������ 239-------- 240 241������KASAN 242~~~~~~~~~ 243 244������KASAN������������������������������������������������������������������������������������������������������ 245������������������������������������������������������ 246 247������KASAN���1/8������������������������������������������16TB���������x86_64������128TB��������������� 248������������������������������������������������������������������������������������������ 249 250������������������������������������������������������:: 251 252 static inline void *kasan_mem_to_shadow(const void *addr) 253 { 254 return (void *)((unsigned long)addr >> KASAN_SHADOW_SCALE_SHIFT) 255 + KASAN_SHADOW_OFFSET; 256 } 257 258��������� ``KASAN_SHADOW_SCALE_SHIFT = 3`` ��� 259 260���������������������������������������������������������������������������������1���2���4���8���16��������������� 261������������������( ``__asan_load*(addr)`` , ``__asan_store*(addr)``)��������������������� 262��������������������������������������������������������������� 263 264������inline��������������������������������������������������������������������������������������������������� 265���������������������������������������outline���������������������������������x1.1-x2������������������ 266 267������KASAN��������������������������������������������������������������������� 268��������� mm/kasan/quarantine.c ��������������������� 269 270���������������������KASAN������ 271~~~~~~~~~~~~~~~~~~~~~~~ 272 273���������������������KASAN������������������������������������������������������������������������arm64��������������� 274 275���������������������KASAN������arm64 CPU���������������������(TBI)��������������������������������������� 276������������������������������������������������������������������16���������������������������������������(��������� 277���������������������1/16���������������������)��� 278 279������������������������������������������������KASAN������������������������������������������������������������ 280��������������������������������������������������������������� 281 282���������������������KASAN��������������������������������������������������������������������������������������� 283������������������������������������������������������������������������������������������������������������������ 284���KASAN������������������������ 285 286���������������������KASAN���������������������������outline���������������������������������������inline��� 287���������������������������������������������outline������������������������������������������������������������ 288���������������inline��������������������������� ``brk`` ��������������������������� ``brk`` ������������ 289������������������������ 290 291���������������������KASAN������0xFF������������������������������������������������������0xFF������������ 292���������������������������������0xFE��������������������������������������������������� 293 294 295���������������������KASAN������ 296~~~~~~~~~~~~~~~~~~~~~~~ 297 298���������������������KASAN������������������������������������������������������������������������������������ 299��������������������������������������� 300 301���������������������KASAN���������������arm64���������������������������ARMv8.5������������������������ 302���arm64������������������(MTE)���������������������(TBI)��� 303 304���������arm64��������������������������������������������������������������������������������������������������� 305��������������������������������������������������������������������������������������������������������������������� 306������������������������������������������������������������������������ 307 308���������������������KASAN������0xFF������������������������������������������������������0xFF��������������� 309������������������������������0xFE��������������������������������������������������� 310 311���������������������MTE���ARMv8.5������������������������������������������������KASAN������������������������ 312������KASAN������������������������������ 313 314������������������CONFIG_KASAN_HW_TAGS���������������������������������TBI������������������ 315``kasan.mode=off`` ������������������MTE������������TBI������ 316 317���������������������KASAN���������������������������������������������MTE��������������������������� 318 319������������ 320-------- 321 322���������������������������������KASAN��������� 323 324��������������������������������������������������������������������������������������������������������������������� 325���������������������������������������������������������������������������������KASAN��������������������������� 326������������������������������ 327 328������������ 329~~~~~~~~ 330 331������������������������������������������������������������������������������������������������������������������ 332��������������������������������������� ������ ������vmalloc���vmemmap������ ������ ��������������������������� 333������������������������������������������������������������������������������������������ 334 335������������������������������������������������������������������������������������������������������������������ 336������������������������������KASAN��������������������������������������������������������������������������� 337��������������������������������������� 338 339������������������ ``VMAP_STACK`` ���������������������������������vmalloc��������������������������� 340��������������������������������������������������������������������������������������������������� 341 342CONFIG_KASAN_VMALLOC 343~~~~~~~~~~~~~~~~~~~~ 344 345������ ``CONFIG_KASAN_VMALLOC`` ���KASAN���������������������������������������������vmalloc 346������������������������arm64���x86���riscv���s390���powerpc��������������� 347 348������������������vmalloc���vmap������������������������������������������������������ 349 350vmalloc������������������������������������������������������������������������������������������������������ 351��������������������������������������������������������������������������������������������������������������� 352������������������������ ``KASAN_GRANULE_SIZE * PAGE_SIZE`` ��������� 353 354���������KASAN���������������������������������������vmalloc��������������������������������������������� 355���������������������������������������������������������������������������vmalloc��������������� 356 357KASAN���������vmap��������������������������������������������������� 358 359������������������������������������KASAN������������vmalloc��������������������������������������������� 360������������������������������������������������������������������������������arch������������ 361 362������������x86��������� ``VMAP_STACK`` ��������������������������������������������������������������������� 363 364��������������� 365---------- 366 367������������ 368~~~~~~~~ 369 370������KASAN������������������������������������������������������������������������������������������������ 371��������������������������������� 372 373���������������������������������������������������������������������������KASAN��������������������������������� 374��������������������������������������������������������������������������������������� 375 376������������KASAN��������������������������������������������������������� ``KASAN_SANITIZE`` ������ 377������������������Makefile���: 378 379- ������������������(���������main.o):: 380 381 KASAN_SANITIZE_main.o := n 382 383- ������������������������������������:: 384 385 KASAN_SANITIZE := n 386 387������������KASAN���������������������������������������������������������������KASAN��������� 388``__no_sanitize_address`` ������������������������ ``noinstr`` ��� 389 390������������������������������������������������������������������������������KASAN���������������KASAN������ 391��������������������������������������������������������������������������������������������������������������������� 392������������������������������������������������������������ 393 394������������KASAN������������������������������������������������������������KASAN������������������ 395``kasan_disable_current()``/``kasan_enable_current()`` ������������������������������ 396������������������������������������������������������������������ 397 398���������������������KASAN������������������������������������������ ``kasan_reset_tag()`` ��� 399``page_kasan_tag_reset()`` ��������������������� ``page_kasan_tag_reset()`` 400������������������������������������ ``page_kasan_tag`` / ``page_kasan_tag_set`` ��� 401������������������KASAN��������� 402 403������ 404~~~~ 405 406���������KASAN������������������KASAN��������������������������������������������������������������� 407������������������������: 408 4091. ���KUnit������������������������������������ ``CONFIG_KASAN_KUNIT_TEST`` ��������� 410������������������������������������������������������������������������������������������������������ 411 4122. ���KUnit��������������������������� ``CONFIG_KASAN_MODULE_TEST`` ������������������������������ 413���������������������������������������������������������������������������������KASAN������������������������ 414 415������������������������������KUnit���������KASAN������������������������KASAN��������������������������������� 416��������������������� 417 418���������������:: 419 420 ok 28 - kmalloc_double_kzfree 421 422��������� ``kmalloc`` ������������������������������:: 423 424 # kmalloc_large_oob_right: ASSERTION FAILED at mm/kasan/kasan_test.c:245 425 Expected ptr is not null, but is 426 not ok 5 - kmalloc_large_oob_right 427 428���������������KASAN������������������������������:: 429 430 # kmalloc_double_kzfree: EXPECTATION FAILED at mm/kasan/kasan_test.c:709 431 KASAN failure expected in "kfree_sensitive(ptr)", but none occurred 432 not ok 28 - kmalloc_double_kzfree 433 434 435������������������KASAN������������������������������:: 436 437 ok 1 - kasan 438 439���������������������������������������:: 440 441 not ok 1 - kasan 442 443������������������������������KUnit���������KASAN��������� 444 4451. ��������������� 446 447 ������ ``CONFIG_KUNIT`` ������KASAN-KUnit������������������������������������������������������ 448 ``insmod`` ��� ``modprobe`` ������ ``kasan_test.ko`` ������������ 449 4502. ������ 451 452 ������������ ``CONFIG_KUNIT`` ������������������KASAN-KUnit������������������������������ 453 ��������������������������������������������������������� 454 4553. ������kunit_tool 456 457 ������������ ``CONFIG_KUNIT`` ��� ``CONFIG_KASAN_KUNIT_TEST`` ������������������ 458 ``kunit_tool`` ���������������������������KUnit������������������������������������������ 459 ���KASAN��������������� ``kunit_tool`` ������������������������������ 460 `KUnit������ <https://www.kernel.org/doc/html/latest/dev-tools/kunit/index.html>`_ ��� 461 462.. _KUnit: https://www.kernel.org/doc/html/latest/dev-tools/kunit/index.html 463 464