12757aafaSJonathan CorbetThe Kernel Address Sanitizer (KASAN)
22757aafaSJonathan Corbet====================================
32757aafaSJonathan Corbet
42757aafaSJonathan CorbetOverview
52757aafaSJonathan Corbet--------
62757aafaSJonathan Corbet
7625d8673SAndrey KonovalovKernelAddressSANitizer (KASAN) is a dynamic memory safety error detector
8625d8673SAndrey Konovalovdesigned to find out-of-bound and use-after-free bugs. KASAN has three modes:
92757aafaSJonathan Corbet
10948e3253SAndrey Konovalov1. generic KASAN (similar to userspace ASan),
11948e3253SAndrey Konovalov2. software tag-based KASAN (similar to userspace HWASan),
12948e3253SAndrey Konovalov3. hardware tag-based KASAN (based on hardware memory tagging).
132757aafaSJonathan Corbet
143cbc37dcSAndrey KonovalovGeneric KASAN is mainly used for debugging due to a large memory overhead.
153cbc37dcSAndrey KonovalovSoftware tag-based KASAN can be used for dogfood testing as it has a lower
163cbc37dcSAndrey Konovalovmemory overhead that allows using it with real workloads. Hardware tag-based
173cbc37dcSAndrey KonovalovKASAN comes with low memory and performance overheads and, therefore, can be
183cbc37dcSAndrey Konovalovused in production. Either as an in-field memory bug detector or as a security
193cbc37dcSAndrey Konovalovmitigation.
203cbc37dcSAndrey Konovalov
213cbc37dcSAndrey KonovalovSoftware KASAN modes (#1 and #2) use compile-time instrumentation to insert
223cbc37dcSAndrey Konovalovvalidity checks before every memory access and, therefore, require a compiler
23948e3253SAndrey Konovalovversion that supports that.
242757aafaSJonathan Corbet
253cbc37dcSAndrey KonovalovGeneric KASAN is supported in GCC and Clang. With GCC, it requires version
26527f6750SMarco Elver8.3.0 or later. Any supported Clang version is compatible, but detection of
27ac4766beSMarco Elverout-of-bounds accesses for global variables is only supported since Clang 11.
28b3b0e6acSAndrey Konovalov
293cbc37dcSAndrey KonovalovSoftware tag-based KASAN mode is only supported in Clang.
30b3b0e6acSAndrey Konovalov
313cbc37dcSAndrey KonovalovThe hardware KASAN mode (#3) relies on hardware to perform the checks but
323cbc37dcSAndrey Konovalovstill requires a compiler version that supports memory tagging instructions.
333cbc37dcSAndrey KonovalovThis mode is supported in GCC 10+ and Clang 11+.
343cbc37dcSAndrey Konovalov
353cbc37dcSAndrey KonovalovBoth software KASAN modes work with SLUB and SLAB memory allocators,
363cbc37dcSAndrey Konovalovwhile the hardware tag-based KASAN currently only supports SLUB.
373cbc37dcSAndrey Konovalov
383cbc37dcSAndrey KonovalovCurrently, generic KASAN is supported for the x86_64, arm, arm64, xtensa, s390,
39948e3253SAndrey Konovalovand riscv architectures, and tag-based KASAN modes are supported only for arm64.
402757aafaSJonathan Corbet
412757aafaSJonathan CorbetUsage
422757aafaSJonathan Corbet-----
432757aafaSJonathan Corbet
4486e6f08dSAndrey KonovalovTo enable KASAN, configure the kernel with::
452757aafaSJonathan Corbet
462757aafaSJonathan Corbet	  CONFIG_KASAN=y
472757aafaSJonathan Corbet
4886e6f08dSAndrey Konovalovand choose between ``CONFIG_KASAN_GENERIC`` (to enable generic KASAN),
4986e6f08dSAndrey Konovalov``CONFIG_KASAN_SW_TAGS`` (to enable software tag-based KASAN), and
5086e6f08dSAndrey Konovalov``CONFIG_KASAN_HW_TAGS`` (to enable hardware tag-based KASAN).
512757aafaSJonathan Corbet
5286e6f08dSAndrey KonovalovFor software modes, also choose between ``CONFIG_KASAN_OUTLINE`` and
5386e6f08dSAndrey Konovalov``CONFIG_KASAN_INLINE``. Outline and inline are compiler instrumentation types.
5486e6f08dSAndrey KonovalovThe former produces a smaller binary while the latter is 1.1-2 times faster.
55b3b0e6acSAndrey Konovalov
5686e6f08dSAndrey KonovalovTo include alloc and free stack traces of affected slab objects into reports,
5786e6f08dSAndrey Konovalovenable ``CONFIG_STACKTRACE``. To include alloc and free stack traces of affected
5886e6f08dSAndrey Konovalovphysical pages, enable ``CONFIG_PAGE_OWNER`` and boot with ``page_owner=on``.
590fe9a448SVlastimil Babka
602757aafaSJonathan CorbetError reports
612757aafaSJonathan Corbet~~~~~~~~~~~~~
622757aafaSJonathan Corbet
63836f79a2SAndrey KonovalovA typical KASAN report looks like this::
642757aafaSJonathan Corbet
652757aafaSJonathan Corbet    ==================================================================
66b3b0e6acSAndrey Konovalov    BUG: KASAN: slab-out-of-bounds in kmalloc_oob_right+0xa8/0xbc [test_kasan]
67b3b0e6acSAndrey Konovalov    Write of size 1 at addr ffff8801f44ec37b by task insmod/2760
682757aafaSJonathan Corbet
69b3b0e6acSAndrey Konovalov    CPU: 1 PID: 2760 Comm: insmod Not tainted 4.19.0-rc3+ #698
70b3b0e6acSAndrey Konovalov    Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.10.2-1 04/01/2014
712757aafaSJonathan Corbet    Call Trace:
72b3b0e6acSAndrey Konovalov     dump_stack+0x94/0xd8
73b3b0e6acSAndrey Konovalov     print_address_description+0x73/0x280
74b3b0e6acSAndrey Konovalov     kasan_report+0x144/0x187
75b3b0e6acSAndrey Konovalov     __asan_report_store1_noabort+0x17/0x20
76b3b0e6acSAndrey Konovalov     kmalloc_oob_right+0xa8/0xbc [test_kasan]
77b3b0e6acSAndrey Konovalov     kmalloc_tests_init+0x16/0x700 [test_kasan]
78b3b0e6acSAndrey Konovalov     do_one_initcall+0xa5/0x3ae
79b3b0e6acSAndrey Konovalov     do_init_module+0x1b6/0x547
80b3b0e6acSAndrey Konovalov     load_module+0x75df/0x8070
81b3b0e6acSAndrey Konovalov     __do_sys_init_module+0x1c6/0x200
82b3b0e6acSAndrey Konovalov     __x64_sys_init_module+0x6e/0xb0
83b3b0e6acSAndrey Konovalov     do_syscall_64+0x9f/0x2c0
84b3b0e6acSAndrey Konovalov     entry_SYSCALL_64_after_hwframe+0x44/0xa9
85b3b0e6acSAndrey Konovalov    RIP: 0033:0x7f96443109da
86b3b0e6acSAndrey Konovalov    RSP: 002b:00007ffcf0b51b08 EFLAGS: 00000202 ORIG_RAX: 00000000000000af
87b3b0e6acSAndrey Konovalov    RAX: ffffffffffffffda RBX: 000055dc3ee521a0 RCX: 00007f96443109da
88b3b0e6acSAndrey Konovalov    RDX: 00007f96445cff88 RSI: 0000000000057a50 RDI: 00007f9644992000
89b3b0e6acSAndrey Konovalov    RBP: 000055dc3ee510b0 R08: 0000000000000003 R09: 0000000000000000
90b3b0e6acSAndrey Konovalov    R10: 00007f964430cd0a R11: 0000000000000202 R12: 00007f96445cff88
91b3b0e6acSAndrey Konovalov    R13: 000055dc3ee51090 R14: 0000000000000000 R15: 0000000000000000
92b3b0e6acSAndrey Konovalov
93b3b0e6acSAndrey Konovalov    Allocated by task 2760:
94b3b0e6acSAndrey Konovalov     save_stack+0x43/0xd0
95b3b0e6acSAndrey Konovalov     kasan_kmalloc+0xa7/0xd0
96b3b0e6acSAndrey Konovalov     kmem_cache_alloc_trace+0xe1/0x1b0
97b3b0e6acSAndrey Konovalov     kmalloc_oob_right+0x56/0xbc [test_kasan]
98b3b0e6acSAndrey Konovalov     kmalloc_tests_init+0x16/0x700 [test_kasan]
99b3b0e6acSAndrey Konovalov     do_one_initcall+0xa5/0x3ae
100b3b0e6acSAndrey Konovalov     do_init_module+0x1b6/0x547
101b3b0e6acSAndrey Konovalov     load_module+0x75df/0x8070
102b3b0e6acSAndrey Konovalov     __do_sys_init_module+0x1c6/0x200
103b3b0e6acSAndrey Konovalov     __x64_sys_init_module+0x6e/0xb0
104b3b0e6acSAndrey Konovalov     do_syscall_64+0x9f/0x2c0
105b3b0e6acSAndrey Konovalov     entry_SYSCALL_64_after_hwframe+0x44/0xa9
106b3b0e6acSAndrey Konovalov
107b3b0e6acSAndrey Konovalov    Freed by task 815:
108b3b0e6acSAndrey Konovalov     save_stack+0x43/0xd0
109b3b0e6acSAndrey Konovalov     __kasan_slab_free+0x135/0x190
110b3b0e6acSAndrey Konovalov     kasan_slab_free+0xe/0x10
111b3b0e6acSAndrey Konovalov     kfree+0x93/0x1a0
112b3b0e6acSAndrey Konovalov     umh_complete+0x6a/0xa0
113b3b0e6acSAndrey Konovalov     call_usermodehelper_exec_async+0x4c3/0x640
114b3b0e6acSAndrey Konovalov     ret_from_fork+0x35/0x40
115b3b0e6acSAndrey Konovalov
116b3b0e6acSAndrey Konovalov    The buggy address belongs to the object at ffff8801f44ec300
117b3b0e6acSAndrey Konovalov     which belongs to the cache kmalloc-128 of size 128
118b3b0e6acSAndrey Konovalov    The buggy address is located 123 bytes inside of
119b3b0e6acSAndrey Konovalov     128-byte region [ffff8801f44ec300, ffff8801f44ec380)
120b3b0e6acSAndrey Konovalov    The buggy address belongs to the page:
121b3b0e6acSAndrey Konovalov    page:ffffea0007d13b00 count:1 mapcount:0 mapping:ffff8801f7001640 index:0x0
122b3b0e6acSAndrey Konovalov    flags: 0x200000000000100(slab)
123b3b0e6acSAndrey Konovalov    raw: 0200000000000100 ffffea0007d11dc0 0000001a0000001a ffff8801f7001640
124b3b0e6acSAndrey Konovalov    raw: 0000000000000000 0000000080150015 00000001ffffffff 0000000000000000
125b3b0e6acSAndrey Konovalov    page dumped because: kasan: bad access detected
126b3b0e6acSAndrey Konovalov
1272757aafaSJonathan Corbet    Memory state around the buggy address:
128b3b0e6acSAndrey Konovalov     ffff8801f44ec200: fc fc fc fc fc fc fc fc fb fb fb fb fb fb fb fb
129b3b0e6acSAndrey Konovalov     ffff8801f44ec280: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
130b3b0e6acSAndrey Konovalov    >ffff8801f44ec300: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 03
1312757aafaSJonathan Corbet                                                                    ^
132b3b0e6acSAndrey Konovalov     ffff8801f44ec380: fc fc fc fc fc fc fc fc fb fb fb fb fb fb fb fb
133b3b0e6acSAndrey Konovalov     ffff8801f44ec400: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
1342757aafaSJonathan Corbet    ==================================================================
1352757aafaSJonathan Corbet
136836f79a2SAndrey KonovalovThe report header summarizes what kind of bug happened and what kind of access
137836f79a2SAndrey Konovalovcaused it. It is followed by a stack trace of the bad access, a stack trace of
138836f79a2SAndrey Konovalovwhere the accessed memory was allocated (in case a slab object was accessed),
139836f79a2SAndrey Konovalovand a stack trace of where the object was freed (in case of a use-after-free
140836f79a2SAndrey Konovalovbug report). Next comes a description of the accessed slab object and the
141836f79a2SAndrey Konovalovinformation about the accessed memory page.
1422757aafaSJonathan Corbet
143836f79a2SAndrey KonovalovIn the end, the report shows the memory state around the accessed address.
144836f79a2SAndrey KonovalovInternally, KASAN tracks memory state separately for each memory granule, which
145625d8673SAndrey Konovalovis either 8 or 16 aligned bytes depending on KASAN mode. Each number in the
146625d8673SAndrey Konovalovmemory state section of the report shows the state of one of the memory
147625d8673SAndrey Konovalovgranules that surround the accessed address.
1482757aafaSJonathan Corbet
149836f79a2SAndrey KonovalovFor generic KASAN, the size of each memory granule is 8. The state of each
150625d8673SAndrey Konovalovgranule is encoded in one shadow byte. Those 8 bytes can be accessible,
151836f79a2SAndrey Konovalovpartially accessible, freed, or be a part of a redzone. KASAN uses the following
152836f79a2SAndrey Konovalovencoding for each shadow byte: 00 means that all 8 bytes of the corresponding
153625d8673SAndrey Konovalovmemory region are accessible; number N (1 <= N <= 7) means that the first N
154625d8673SAndrey Konovalovbytes are accessible, and other (8 - N) bytes are not; any negative value
155625d8673SAndrey Konovalovindicates that the entire 8-byte word is inaccessible. KASAN uses different
156625d8673SAndrey Konovalovnegative values to distinguish between different kinds of inaccessible memory
157625d8673SAndrey Konovalovlike redzones or freed memory (see mm/kasan/kasan.h).
1582757aafaSJonathan Corbet
159836f79a2SAndrey KonovalovIn the report above, the arrow points to the shadow byte ``03``, which means
160836f79a2SAndrey Konovalovthat the accessed address is partially accessible.
161836f79a2SAndrey Konovalov
162836f79a2SAndrey KonovalovFor tag-based KASAN modes, this last report section shows the memory tags around
163836f79a2SAndrey Konovalovthe accessed address (see the `Implementation details`_ section).
164836f79a2SAndrey Konovalov
165836f79a2SAndrey KonovalovNote that KASAN bug titles (like ``slab-out-of-bounds`` or ``use-after-free``)
166836f79a2SAndrey Konovalovare best-effort: KASAN prints the most probable bug type based on the limited
167836f79a2SAndrey Konovalovinformation it has. The actual type of the bug might be different.
168836f79a2SAndrey Konovalov
169836f79a2SAndrey KonovalovGeneric KASAN also reports up to two auxiliary call stack traces. These stack
170836f79a2SAndrey Konovalovtraces point to places in code that interacted with the object but that are not
171836f79a2SAndrey Konovalovdirectly present in the bad access stack trace. Currently, this includes
172836f79a2SAndrey Konovalovcall_rcu() and workqueue queuing.
173625d8673SAndrey Konovalov
174625d8673SAndrey KonovalovBoot parameters
175625d8673SAndrey Konovalov~~~~~~~~~~~~~~~
176625d8673SAndrey Konovalov
177f3590747SAndrey KonovalovKASAN is affected by the generic ``panic_on_warn`` command line parameter.
178f3590747SAndrey KonovalovWhen it is enabled, KASAN panics the kernel after printing a bug report.
179f3590747SAndrey Konovalov
180f3590747SAndrey KonovalovBy default, KASAN prints a bug report only for the first invalid memory access.
181f3590747SAndrey KonovalovWith ``kasan_multi_shot``, KASAN prints a report on every invalid access. This
182f3590747SAndrey Konovaloveffectively disables ``panic_on_warn`` for KASAN reports.
183f3590747SAndrey Konovalov
1844062c245SAndrey KonovalovHardware tag-based KASAN mode (see the section about various modes below) is
1857169487bSAndrey Konovalovintended for use in production as a security mitigation. Therefore, it supports
186f3590747SAndrey Konovalovboot parameters that allow disabling KASAN or controlling its features.
187625d8673SAndrey Konovalov
18876bc99e8SAndrey Konovalov- ``kasan=off`` or ``=on`` controls whether KASAN is enabled (default: ``on``).
189625d8673SAndrey Konovalov
1902603f8a7SVincenzo Frascino- ``kasan.mode=sync`` or ``=async`` controls whether KASAN is configured in
1912603f8a7SVincenzo Frascino  synchronous or asynchronous mode of execution (default: ``sync``).
1922603f8a7SVincenzo Frascino  Synchronous mode: a bad access is detected immediately when a tag
1932603f8a7SVincenzo Frascino  check fault occurs.
1942603f8a7SVincenzo Frascino  Asynchronous mode: a bad access detection is delayed. When a tag check
1952603f8a7SVincenzo Frascino  fault occurs, the information is stored in hardware (in the TFSR_EL1
1962603f8a7SVincenzo Frascino  register for arm64). The kernel periodically checks the hardware and
1972603f8a7SVincenzo Frascino  only reports tag faults during these checks.
1982603f8a7SVincenzo Frascino
19976bc99e8SAndrey Konovalov- ``kasan.stacktrace=off`` or ``=on`` disables or enables alloc and free stack
2001cc4cdb5SAndrey Konovalov  traces collection (default: ``on``).
201625d8673SAndrey Konovalov
20276bc99e8SAndrey Konovalov- ``kasan.fault=report`` or ``=panic`` controls whether to only print a KASAN
203f3590747SAndrey Konovalov  report or also panic the kernel (default: ``report``). The panic happens even
204f3590747SAndrey Konovalov  if ``kasan_multi_shot`` is enabled.
205625d8673SAndrey Konovalov
2062757aafaSJonathan CorbetImplementation details
2072757aafaSJonathan Corbet----------------------
2082757aafaSJonathan Corbet
209b3b0e6acSAndrey KonovalovGeneric KASAN
210b3b0e6acSAndrey Konovalov~~~~~~~~~~~~~
211b3b0e6acSAndrey Konovalov
212b8191d7dSAndrey KonovalovSoftware KASAN modes use shadow memory to record whether each byte of memory is
213b8191d7dSAndrey Konovalovsafe to access and use compile-time instrumentation to insert shadow memory
214b8191d7dSAndrey Konovalovchecks before each memory access.
2152757aafaSJonathan Corbet
216b8191d7dSAndrey KonovalovGeneric KASAN dedicates 1/8th of kernel memory to its shadow memory (16TB
217b3b0e6acSAndrey Konovalovto cover 128TB on x86_64) and uses direct mapping with a scale and offset to
218b3b0e6acSAndrey Konovalovtranslate a memory address to its corresponding shadow address.
2192757aafaSJonathan Corbet
2202757aafaSJonathan CorbetHere is the function which translates an address to its corresponding shadow
2212757aafaSJonathan Corbetaddress::
2222757aafaSJonathan Corbet
2232757aafaSJonathan Corbet    static inline void *kasan_mem_to_shadow(const void *addr)
2242757aafaSJonathan Corbet    {
225b8191d7dSAndrey Konovalov	return (void *)((unsigned long)addr >> KASAN_SHADOW_SCALE_SHIFT)
2262757aafaSJonathan Corbet		+ KASAN_SHADOW_OFFSET;
2272757aafaSJonathan Corbet    }
2282757aafaSJonathan Corbet
2292757aafaSJonathan Corbetwhere ``KASAN_SHADOW_SCALE_SHIFT = 3``.
2302757aafaSJonathan Corbet
231b3b0e6acSAndrey KonovalovCompile-time instrumentation is used to insert memory access checks. Compiler
232b8191d7dSAndrey Konovalovinserts function calls (``__asan_load*(addr)``, ``__asan_store*(addr)``) before
233b8191d7dSAndrey Konovaloveach memory access of size 1, 2, 4, 8, or 16. These functions check whether
234b8191d7dSAndrey Konovalovmemory accesses are valid or not by checking corresponding shadow memory.
2352757aafaSJonathan Corbet
236b8191d7dSAndrey KonovalovWith inline instrumentation, instead of making function calls, the compiler
237b8191d7dSAndrey Konovalovdirectly inserts the code to check shadow memory. This option significantly
238b8191d7dSAndrey Konovalovenlarges the kernel, but it gives an x1.1-x2 performance boost over the
239b8191d7dSAndrey Konovalovoutline-instrumented kernel.
240b3b0e6acSAndrey Konovalov
241b8191d7dSAndrey KonovalovGeneric KASAN is the only mode that delays the reuse of freed objects via
242625d8673SAndrey Konovalovquarantine (see mm/kasan/quarantine.c for implementation).
243625d8673SAndrey Konovalov
244b3b0e6acSAndrey KonovalovSoftware tag-based KASAN
245b3b0e6acSAndrey Konovalov~~~~~~~~~~~~~~~~~~~~~~~~
246b3b0e6acSAndrey Konovalov
247a6c18d4eSAndrey KonovalovSoftware tag-based KASAN uses a software memory tagging approach to checking
248a6c18d4eSAndrey Konovalovaccess validity. It is currently only implemented for the arm64 architecture.
249948e3253SAndrey Konovalov
250948e3253SAndrey KonovalovSoftware tag-based KASAN uses the Top Byte Ignore (TBI) feature of arm64 CPUs
251a6c18d4eSAndrey Konovalovto store a pointer tag in the top byte of kernel pointers. It uses shadow memory
252a6c18d4eSAndrey Konovalovto store memory tags associated with each 16-byte memory cell (therefore, it
253a6c18d4eSAndrey Konovalovdedicates 1/16th of the kernel memory for shadow memory).
254b3b0e6acSAndrey Konovalov
255a6c18d4eSAndrey KonovalovOn each memory allocation, software tag-based KASAN generates a random tag, tags
256a6c18d4eSAndrey Konovalovthe allocated memory with this tag, and embeds the same tag into the returned
257948e3253SAndrey Konovalovpointer.
258948e3253SAndrey Konovalov
259b3b0e6acSAndrey KonovalovSoftware tag-based KASAN uses compile-time instrumentation to insert checks
260a6c18d4eSAndrey Konovalovbefore each memory access. These checks make sure that the tag of the memory
261a6c18d4eSAndrey Konovalovthat is being accessed is equal to the tag of the pointer that is used to access
262a6c18d4eSAndrey Konovalovthis memory. In case of a tag mismatch, software tag-based KASAN prints a bug
263a6c18d4eSAndrey Konovalovreport.
264b3b0e6acSAndrey Konovalov
265a6c18d4eSAndrey KonovalovSoftware tag-based KASAN also has two instrumentation modes (outline, which
266a6c18d4eSAndrey Konovalovemits callbacks to check memory accesses; and inline, which performs the shadow
267b3b0e6acSAndrey Konovalovmemory checks inline). With outline instrumentation mode, a bug report is
268a6c18d4eSAndrey Konovalovprinted from the function that performs the access check. With inline
269a6c18d4eSAndrey Konovalovinstrumentation, a ``brk`` instruction is emitted by the compiler, and a
270a6c18d4eSAndrey Konovalovdedicated ``brk`` handler is used to print bug reports.
271b3b0e6acSAndrey Konovalov
272948e3253SAndrey KonovalovSoftware tag-based KASAN uses 0xFF as a match-all pointer tag (accesses through
273a6c18d4eSAndrey Konovalovpointers with the 0xFF pointer tag are not checked). The value 0xFE is currently
274948e3253SAndrey Konovalovreserved to tag freed memory regions.
275948e3253SAndrey Konovalov
276a6c18d4eSAndrey KonovalovSoftware tag-based KASAN currently only supports tagging of slab and page_alloc
277a6c18d4eSAndrey Konovalovmemory.
278948e3253SAndrey Konovalov
279948e3253SAndrey KonovalovHardware tag-based KASAN
280948e3253SAndrey Konovalov~~~~~~~~~~~~~~~~~~~~~~~~
281948e3253SAndrey Konovalov
282bb48675eSAndrey KonovalovHardware tag-based KASAN is similar to the software mode in concept but uses
283948e3253SAndrey Konovalovhardware memory tagging support instead of compiler instrumentation and
284948e3253SAndrey Konovalovshadow memory.
285948e3253SAndrey Konovalov
286948e3253SAndrey KonovalovHardware tag-based KASAN is currently only implemented for arm64 architecture
287948e3253SAndrey Konovalovand based on both arm64 Memory Tagging Extension (MTE) introduced in ARMv8.5
288bb48675eSAndrey KonovalovInstruction Set Architecture and Top Byte Ignore (TBI).
289948e3253SAndrey Konovalov
290948e3253SAndrey KonovalovSpecial arm64 instructions are used to assign memory tags for each allocation.
291948e3253SAndrey KonovalovSame tags are assigned to pointers to those allocations. On every memory
292bb48675eSAndrey Konovalovaccess, hardware makes sure that the tag of the memory that is being accessed is
293bb48675eSAndrey Konovalovequal to the tag of the pointer that is used to access this memory. In case of a
294bb48675eSAndrey Konovalovtag mismatch, a fault is generated, and a report is printed.
295948e3253SAndrey Konovalov
296948e3253SAndrey KonovalovHardware tag-based KASAN uses 0xFF as a match-all pointer tag (accesses through
297bb48675eSAndrey Konovalovpointers with the 0xFF pointer tag are not checked). The value 0xFE is currently
298948e3253SAndrey Konovalovreserved to tag freed memory regions.
299948e3253SAndrey Konovalov
300bb48675eSAndrey KonovalovHardware tag-based KASAN currently only supports tagging of slab and page_alloc
301bb48675eSAndrey Konovalovmemory.
3023c5c3cfbSDaniel Axtens
303bb48675eSAndrey KonovalovIf the hardware does not support MTE (pre ARMv8.5), hardware tag-based KASAN
304bb48675eSAndrey Konovalovwill not be enabled. In this case, all KASAN boot parameters are ignored.
3054062c245SAndrey Konovalov
306bb48675eSAndrey KonovalovNote that enabling CONFIG_KASAN_HW_TAGS always results in in-kernel TBI being
307bb48675eSAndrey Konovalovenabled. Even when ``kasan.mode=off`` is provided or when the hardware does not
3084062c245SAndrey Konovalovsupport MTE (but supports TBI).
3094062c245SAndrey Konovalov
310bb48675eSAndrey KonovalovHardware tag-based KASAN only reports the first found bug. After that, MTE tag
3117169487bSAndrey Konovalovchecking gets disabled.
3127169487bSAndrey Konovalov
31396d7d141SAndrey KonovalovShadow memory
31496d7d141SAndrey Konovalov-------------
3153c5c3cfbSDaniel Axtens
31667ca1c0bSAndrey KonovalovThe kernel maps memory in several different parts of the address space.
31767ca1c0bSAndrey KonovalovThe range of kernel virtual addresses is large: there is not enough real
31867ca1c0bSAndrey Konovalovmemory to support a real shadow region for every address that could be
31967ca1c0bSAndrey Konovalovaccessed by the kernel. Therefore, KASAN only maps real shadow for certain
32067ca1c0bSAndrey Konovalovparts of the address space.
3213c5c3cfbSDaniel Axtens
32296d7d141SAndrey KonovalovDefault behaviour
32396d7d141SAndrey Konovalov~~~~~~~~~~~~~~~~~
3243c5c3cfbSDaniel Axtens
3253c5c3cfbSDaniel AxtensBy default, architectures only map real memory over the shadow region
3263c5c3cfbSDaniel Axtensfor the linear mapping (and potentially other small areas). For all
3273c5c3cfbSDaniel Axtensother areas - such as vmalloc and vmemmap space - a single read-only
3283c5c3cfbSDaniel Axtenspage is mapped over the shadow area. This read-only shadow page
3293c5c3cfbSDaniel Axtensdeclares all memory accesses as permitted.
3303c5c3cfbSDaniel Axtens
3313c5c3cfbSDaniel AxtensThis presents a problem for modules: they do not live in the linear
33267ca1c0bSAndrey Konovalovmapping but in a dedicated module space. By hooking into the module
33367ca1c0bSAndrey Konovalovallocator, KASAN temporarily maps real shadow memory to cover them.
33467ca1c0bSAndrey KonovalovThis allows detection of invalid accesses to module globals, for example.
3353c5c3cfbSDaniel Axtens
3363c5c3cfbSDaniel AxtensThis also creates an incompatibility with ``VMAP_STACK``: if the stack
3373c5c3cfbSDaniel Axtenslives in vmalloc space, it will be shadowed by the read-only page, and
3383c5c3cfbSDaniel Axtensthe kernel will fault when trying to set up the shadow data for stack
3393c5c3cfbSDaniel Axtensvariables.
3403c5c3cfbSDaniel Axtens
3413c5c3cfbSDaniel AxtensCONFIG_KASAN_VMALLOC
3423c5c3cfbSDaniel Axtens~~~~~~~~~~~~~~~~~~~~
3433c5c3cfbSDaniel Axtens
3443c5c3cfbSDaniel AxtensWith ``CONFIG_KASAN_VMALLOC``, KASAN can cover vmalloc space at the
34567ca1c0bSAndrey Konovalovcost of greater memory usage. Currently, this is supported on x86,
34667ca1c0bSAndrey Konovalovriscv, s390, and powerpc.
3473c5c3cfbSDaniel Axtens
34867ca1c0bSAndrey KonovalovThis works by hooking into vmalloc and vmap and dynamically
3493c5c3cfbSDaniel Axtensallocating real shadow memory to back the mappings.
3503c5c3cfbSDaniel Axtens
3513c5c3cfbSDaniel AxtensMost mappings in vmalloc space are small, requiring less than a full
3523c5c3cfbSDaniel Axtenspage of shadow space. Allocating a full shadow page per mapping would
3533c5c3cfbSDaniel Axtenstherefore be wasteful. Furthermore, to ensure that different mappings
3543c5c3cfbSDaniel Axtensuse different shadow pages, mappings would have to be aligned to
3551f600626SAndrey Konovalov``KASAN_GRANULE_SIZE * PAGE_SIZE``.
3563c5c3cfbSDaniel Axtens
357625d8673SAndrey KonovalovInstead, KASAN shares backing space across multiple mappings. It allocates
3583c5c3cfbSDaniel Axtensa backing page when a mapping in vmalloc space uses a particular page
3593c5c3cfbSDaniel Axtensof the shadow region. This page can be shared by other vmalloc
3603c5c3cfbSDaniel Axtensmappings later on.
3613c5c3cfbSDaniel Axtens
362625d8673SAndrey KonovalovKASAN hooks into the vmap infrastructure to lazily clean up unused shadow
3633c5c3cfbSDaniel Axtensmemory.
3643c5c3cfbSDaniel Axtens
365625d8673SAndrey KonovalovTo avoid the difficulties around swapping mappings around, KASAN expects
3663c5c3cfbSDaniel Axtensthat the part of the shadow region that covers the vmalloc space will
36767ca1c0bSAndrey Konovalovnot be covered by the early shadow page but will be left unmapped.
36867ca1c0bSAndrey KonovalovThis will require changes in arch-specific code.
3693c5c3cfbSDaniel Axtens
37067ca1c0bSAndrey KonovalovThis allows ``VMAP_STACK`` support on x86 and can simplify support of
3713c5c3cfbSDaniel Axtensarchitectures that do not have a fixed module region.
3729ab5be97SPatricia Alfonso
37396d7d141SAndrey KonovalovFor developers
37496d7d141SAndrey Konovalov--------------
37596d7d141SAndrey Konovalov
37696d7d141SAndrey KonovalovIgnoring accesses
37796d7d141SAndrey Konovalov~~~~~~~~~~~~~~~~~
37896d7d141SAndrey Konovalov
37996d7d141SAndrey KonovalovSoftware KASAN modes use compiler instrumentation to insert validity checks.
380fe547fcaSAndrey KonovalovSuch instrumentation might be incompatible with some parts of the kernel, and
381fe547fcaSAndrey Konovalovtherefore needs to be disabled.
382fe547fcaSAndrey Konovalov
383fe547fcaSAndrey KonovalovOther parts of the kernel might access metadata for allocated objects.
384fe547fcaSAndrey KonovalovNormally, KASAN detects and reports such accesses, but in some cases (e.g.,
385fe547fcaSAndrey Konovalovin memory allocators), these accesses are valid.
386fe547fcaSAndrey Konovalov
387fe547fcaSAndrey KonovalovFor software KASAN modes, to disable instrumentation for a specific file or
388fe547fcaSAndrey Konovalovdirectory, add a ``KASAN_SANITIZE`` annotation to the respective kernel
38996d7d141SAndrey KonovalovMakefile:
39096d7d141SAndrey Konovalov
391fe547fcaSAndrey Konovalov- For a single file (e.g., main.o)::
39296d7d141SAndrey Konovalov
39396d7d141SAndrey Konovalov    KASAN_SANITIZE_main.o := n
39496d7d141SAndrey Konovalov
39596d7d141SAndrey Konovalov- For all files in one directory::
39696d7d141SAndrey Konovalov
39796d7d141SAndrey Konovalov    KASAN_SANITIZE := n
39896d7d141SAndrey Konovalov
399fe547fcaSAndrey KonovalovFor software KASAN modes, to disable instrumentation on a per-function basis,
400fe547fcaSAndrey Konovalovuse the KASAN-specific ``__no_sanitize_address`` function attribute or the
401fe547fcaSAndrey Konovalovgeneric ``noinstr`` one.
402fe547fcaSAndrey Konovalov
403fe547fcaSAndrey KonovalovNote that disabling compiler instrumentation (either on a per-file or a
404fe547fcaSAndrey Konovalovper-function basis) makes KASAN ignore the accesses that happen directly in
405fe547fcaSAndrey Konovalovthat code for software KASAN modes. It does not help when the accesses happen
406fe547fcaSAndrey Konovalovindirectly (through calls to instrumented functions) or with the hardware
407fe547fcaSAndrey Konovalovtag-based mode that does not use compiler instrumentation.
408fe547fcaSAndrey Konovalov
409fe547fcaSAndrey KonovalovFor software KASAN modes, to disable KASAN reports in a part of the kernel code
410fe547fcaSAndrey Konovalovfor the current task, annotate this part of the code with a
411fe547fcaSAndrey Konovalov``kasan_disable_current()``/``kasan_enable_current()`` section. This also
412fe547fcaSAndrey Konovalovdisables the reports for indirect accesses that happen through function calls.
413fe547fcaSAndrey Konovalov
414fe547fcaSAndrey KonovalovFor tag-based KASAN modes (include the hardware one), to disable access
415fe547fcaSAndrey Konovalovchecking, use ``kasan_reset_tag()`` or ``page_kasan_tag_reset()``. Note that
416fe547fcaSAndrey Konovalovtemporarily disabling access checking via ``page_kasan_tag_reset()`` requires
417fe547fcaSAndrey Konovalovsaving and restoring the per-page KASAN tag via
418fe547fcaSAndrey Konovalov``page_kasan_tag``/``page_kasan_tag_set``.
41996d7d141SAndrey Konovalov
42096d7d141SAndrey KonovalovTests
42196d7d141SAndrey Konovalov~~~~~
4229ab5be97SPatricia Alfonso
423fc23c074SAndrey KonovalovThere are KASAN tests that allow verifying that KASAN works and can detect
424fc23c074SAndrey Konovalovcertain types of memory corruptions. The tests consist of two parts:
4259ab5be97SPatricia Alfonso
426625d8673SAndrey Konovalov1. Tests that are integrated with the KUnit Test Framework. Enabled with
427625d8673SAndrey Konovalov``CONFIG_KASAN_KUNIT_TEST``. These tests can be run and partially verified
428fc23c074SAndrey Konovalovautomatically in a few different ways; see the instructions below.
4299ab5be97SPatricia Alfonso
430625d8673SAndrey Konovalov2. Tests that are currently incompatible with KUnit. Enabled with
4315d92bdffSAndrey Konovalov``CONFIG_KASAN_MODULE_TEST`` and can only be run as a module. These tests can
432fc23c074SAndrey Konovalovonly be verified manually by loading the kernel module and inspecting the
433625d8673SAndrey Konovalovkernel log for KASAN reports.
434625d8673SAndrey Konovalov
435fc23c074SAndrey KonovalovEach KUnit-compatible KASAN test prints one of multiple KASAN reports if an
436fc23c074SAndrey Konovaloverror is detected. Then the test prints its number and status.
437625d8673SAndrey Konovalov
438625d8673SAndrey KonovalovWhen a test passes::
4399ab5be97SPatricia Alfonso
4409ab5be97SPatricia Alfonso        ok 28 - kmalloc_double_kzfree
44132519c03SMauro Carvalho Chehab
442625d8673SAndrey KonovalovWhen a test fails due to a failed ``kmalloc``::
4439ab5be97SPatricia Alfonso
4449ab5be97SPatricia Alfonso        # kmalloc_large_oob_right: ASSERTION FAILED at lib/test_kasan.c:163
4459ab5be97SPatricia Alfonso        Expected ptr is not null, but is
4469ab5be97SPatricia Alfonso        not ok 4 - kmalloc_large_oob_right
44732519c03SMauro Carvalho Chehab
448625d8673SAndrey KonovalovWhen a test fails due to a missing KASAN report::
4499ab5be97SPatricia Alfonso
450*3ff16d30SDavid Gow        # kmalloc_double_kzfree: EXPECTATION FAILED at lib/test_kasan.c:974
451*3ff16d30SDavid Gow        KASAN failure expected in "kfree_sensitive(ptr)", but none occurred
452*3ff16d30SDavid Gow        not ok 44 - kmalloc_double_kzfree
453*3ff16d30SDavid Gow
4549ab5be97SPatricia Alfonso
455625d8673SAndrey KonovalovAt the end the cumulative status of all KASAN tests is printed. On success::
4569ab5be97SPatricia Alfonso
4579ab5be97SPatricia Alfonso        ok 1 - kasan
4589ab5be97SPatricia Alfonso
459625d8673SAndrey KonovalovOr, if one of the tests failed::
4609ab5be97SPatricia Alfonso
4619ab5be97SPatricia Alfonso        not ok 1 - kasan
4629ab5be97SPatricia Alfonso
463625d8673SAndrey KonovalovThere are a few ways to run KUnit-compatible KASAN tests.
464625d8673SAndrey Konovalov
465625d8673SAndrey Konovalov1. Loadable module
4669ab5be97SPatricia Alfonso
467fc23c074SAndrey Konovalov   With ``CONFIG_KUNIT`` enabled, KASAN-KUnit tests can be built as a loadable
468fc23c074SAndrey Konovalov   module and run by loading ``test_kasan.ko`` with ``insmod`` or ``modprobe``.
4699ab5be97SPatricia Alfonso
470625d8673SAndrey Konovalov2. Built-In
4719ab5be97SPatricia Alfonso
472fc23c074SAndrey Konovalov   With ``CONFIG_KUNIT`` built-in, KASAN-KUnit tests can be built-in as well.
473fc23c074SAndrey Konovalov   In this case, the tests will run at boot as a late-init call.
4749ab5be97SPatricia Alfonso
475625d8673SAndrey Konovalov3. Using kunit_tool
4769ab5be97SPatricia Alfonso
477fc23c074SAndrey Konovalov   With ``CONFIG_KUNIT`` and ``CONFIG_KASAN_KUNIT_TEST`` built-in, it is also
478fc23c074SAndrey Konovalov   possible to use ``kunit_tool`` to see the results of KUnit tests in a more
479fc23c074SAndrey Konovalov   readable way. This will not print the KASAN reports of the tests that passed.
480fc23c074SAndrey Konovalov   See `KUnit documentation <https://www.kernel.org/doc/html/latest/dev-tools/kunit/index.html>`_
481625d8673SAndrey Konovalov   for more up-to-date information on ``kunit_tool``.
4829ab5be97SPatricia Alfonso
4839ab5be97SPatricia Alfonso.. _KUnit: https://www.kernel.org/doc/html/latest/dev-tools/kunit/index.html
484