Lines Matching +full:in +full:- +full:memory

5 --------
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:
11 2. software tag-based KASAN (similar to userspace HWASan),
12 3. hardware tag-based KASAN (based on hardware memory tagging).
14 Generic KASAN is mainly used for debugging due to a large memory overhead.
15 Software tag-based KASAN can be used for dogfood testing as it has a lower
16 memory overhead that allows using it with real workloads. Hardware tag-based
17 KASAN comes with low memory and performance overheads and, therefore, can be
18 used in production. Either as an in-field memory bug detector or as a security
21 Software KASAN modes (#1 and #2) use compile-time instrumentation to insert
22 validity checks before every memory access and, therefore, require a compiler
25 Generic KASAN is supported in GCC and Clang. With GCC, it requires version
27 out-of-bounds accesses for global variables is only supported since Clang 11.
29 Software tag-based KASAN mode is only supported in Clang.
32 still requires a compiler version that supports memory tagging instructions.
33 This mode is supported in GCC 10+ and Clang 11+.
35 Both software KASAN modes work with SLUB and SLAB memory allocators,
36 while the hardware tag-based KASAN currently only supports SLUB.
39 and riscv architectures, and tag-based KASAN modes are supported only for arm64.
42 -----
49 ``CONFIG_KASAN_SW_TAGS`` (to enable software tag-based KASAN), and
50 ``CONFIG_KASAN_HW_TAGS`` (to enable hardware tag-based KASAN).
54 The former produces a smaller binary while the latter is 1.1-2 times faster.
66 BUG: KASAN: slab-out-of-bounds in kmalloc_oob_right+0xa8/0xbc [test_kasan]
69 CPU: 1 PID: 2760 Comm: insmod Not tainted 4.19.0-rc3+ #698
70 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.10.2-1 04/01/2014
117 which belongs to the cache kmalloc-128 of size 128
119 128-byte region [ffff8801f44ec300, ffff8801f44ec380)
127 Memory state around the buggy address:
138 where the accessed memory was allocated (in case a slab object was accessed),
139 and a stack trace of where the object was freed (in case of a use-after-free
141 information about the accessed memory page.
143 In the end, the report shows the memory state around the accessed address.
144 Internally, KASAN tracks memory state separately for each memory granule, which
145 is either 8 or 16 aligned bytes depending on KASAN mode. Each number in the
146 memory state section of the report shows the state of one of the memory
149 For generic KASAN, the size of each memory granule is 8. The state of each
150 granule is encoded in one shadow byte. Those 8 bytes can be accessible,
153 memory region are accessible; number N (1 <= N <= 7) means that the first N
154 bytes are accessible, and other (8 - N) bytes are not; any negative value
155 indicates that the entire 8-byte word is inaccessible. KASAN uses different
156 negative values to distinguish between different kinds of inaccessible memory
157 like redzones or freed memory (see mm/kasan/kasan.h).
159 In the report above, the arrow points to the shadow byte ``03``, which means
162 For tag-based KASAN modes, this last report section shows the memory tags around
165 Note that KASAN bug titles (like ``slab-out-of-bounds`` or ``use-after-free``)
166 are best-effort: KASAN prints the most probable bug type based on the limited
170 traces point to places in code that interacted with the object but that are not
171 directly present in the bad access stack trace. Currently, this includes
180 By default, KASAN prints a bug report only for the first invalid memory access.
187 - ``kasan.fault=report`` or ``=panic`` controls whether to only print a KASAN
191 Hardware tag-based KASAN mode (see the section about various modes below) is
192 intended for use in production as a security mitigation. Therefore, it supports
195 - ``kasan=off`` or ``=on`` controls whether KASAN is enabled (default: ``on``).
197 - ``kasan.mode=sync`` or ``=async`` controls whether KASAN is configured in
202 fault occurs, the information is stored in hardware (in the TFSR_EL1
206 - ``kasan.stacktrace=off`` or ``=on`` disables or enables alloc and free stack
210 ----------------------
215 Software KASAN modes use shadow memory to record whether each byte of memory is
216 safe to access and use compile-time instrumentation to insert shadow memory
217 checks before each memory access.
219 Generic KASAN dedicates 1/8th of kernel memory to its shadow memory (16TB
221 translate a memory address to its corresponding shadow address.
234 Compile-time instrumentation is used to insert memory access checks. Compiler
236 each memory access of size 1, 2, 4, 8, or 16. These functions check whether
237 memory accesses are valid or not by checking corresponding shadow memory.
240 directly inserts the code to check shadow memory. This option significantly
241 enlarges the kernel, but it gives an x1.1-x2 performance boost over the
242 outline-instrumented kernel.
247 Software tag-based KASAN
250 Software tag-based KASAN uses a software memory tagging approach to checking
253 Software tag-based KASAN uses the Top Byte Ignore (TBI) feature of arm64 CPUs
254 to store a pointer tag in the top byte of kernel pointers. It uses shadow memory
255 to store memory tags associated with each 16-byte memory cell (therefore, it
256 dedicates 1/16th of the kernel memory for shadow memory).
258 On each memory allocation, software tag-based KASAN generates a random tag, tags
259 the allocated memory with this tag, and embeds the same tag into the returned
262 Software tag-based KASAN uses compile-time instrumentation to insert checks
263 before each memory access. These checks make sure that the tag of the memory
265 this memory. In case of a tag mismatch, software tag-based KASAN prints a bug
268 Software tag-based KASAN also has two instrumentation modes (outline, which
269 emits callbacks to check memory accesses; and inline, which performs the shadow
270 memory checks inline). With outline instrumentation mode, a bug report is
275 Software tag-based KASAN uses 0xFF as a match-all pointer tag (accesses through
277 reserved to tag freed memory regions.
279 Software tag-based KASAN currently only supports tagging of slab and page_alloc
280 memory.
282 Hardware tag-based KASAN
285 Hardware tag-based KASAN is similar to the software mode in concept but uses
286 hardware memory tagging support instead of compiler instrumentation and
287 shadow memory.
289 Hardware tag-based KASAN is currently only implemented for arm64 architecture
290 and based on both arm64 Memory Tagging Extension (MTE) introduced in ARMv8.5
293 Special arm64 instructions are used to assign memory tags for each allocation.
294 Same tags are assigned to pointers to those allocations. On every memory
295 access, hardware makes sure that the tag of the memory that is being accessed is
296 equal to the tag of the pointer that is used to access this memory. In case of a
299 Hardware tag-based KASAN uses 0xFF as a match-all pointer tag (accesses through
301 reserved to tag freed memory regions.
303 Hardware tag-based KASAN currently only supports tagging of slab and page_alloc
304 memory.
306 If the hardware does not support MTE (pre ARMv8.5), hardware tag-based KASAN
307 will not be enabled. In this case, all KASAN boot parameters are ignored.
309 Note that enabling CONFIG_KASAN_HW_TAGS always results in in-kernel TBI being
313 Hardware tag-based KASAN only reports the first found bug. After that, MTE tag
316 Shadow memory
317 -------------
319 The kernel maps memory in several different parts of the address space.
321 memory to support a real shadow region for every address that could be
328 By default, architectures only map real memory over the shadow region
330 other areas - such as vmalloc and vmemmap space - a single read-only
331 page is mapped over the shadow area. This read-only shadow page
332 declares all memory accesses as permitted.
334 This presents a problem for modules: they do not live in the linear
335 mapping but in a dedicated module space. By hooking into the module
336 allocator, KASAN temporarily maps real shadow memory to cover them.
340 lives in vmalloc space, it will be shadowed by the read-only page, and
348 cost of greater memory usage. Currently, this is supported on x86,
352 allocating real shadow memory to back the mappings.
354 Most mappings in vmalloc space are small, requiring less than a full
361 a backing page when a mapping in vmalloc space uses a particular page
366 memory.
371 This will require changes in arch-specific code.
377 --------------
387 Normally, KASAN detects and reports such accesses, but in some cases (e.g.,
388 in memory allocators), these accesses are valid.
394 - For a single file (e.g., main.o)::
398 - For all files in one directory::
402 For software KASAN modes, to disable instrumentation on a per-function basis,
403 use the KASAN-specific ``__no_sanitize_address`` function attribute or the
406 Note that disabling compiler instrumentation (either on a per-file or a
407 per-function basis) makes KASAN ignore the accesses that happen directly in
410 tag-based mode that does not use compiler instrumentation.
412 For software KASAN modes, to disable KASAN reports in a part of the kernel code
417 For tag-based KASAN modes (include the hardware one), to disable access
420 saving and restoring the per-page KASAN tag via
427 certain types of memory corruptions. The tests consist of two parts:
431 automatically in a few different ways; see the instructions below.
438 Each KUnit-compatible KASAN test prints one of multiple KASAN reports if an
443 ok 28 - kmalloc_double_kzfree
449 not ok 4 - kmalloc_large_oob_right
454 KASAN failure expected in "kfree_sensitive(ptr)", but none occurred
455 not ok 44 - kmalloc_double_kzfree
460 ok 1 - kasan
464 not ok 1 - kasan
466 There are a few ways to run KUnit-compatible KASAN tests.
470 With ``CONFIG_KUNIT`` enabled, KASAN-KUnit tests can be built as a loadable
473 2. Built-In
475 With ``CONFIG_KUNIT`` built-in, KASAN-KUnit tests can be built-in as well.
476 In this case, the tests will run at boot as a late-init call.
480 With ``CONFIG_KUNIT`` and ``CONFIG_KASAN_KUNIT_TEST`` built-in, it is also
481 possible to use ``kunit_tool`` to see the results of KUnit tests in a more
483 See `KUnit documentation <https://www.kernel.org/doc/html/latest/dev-tools/kunit/index.html>`_
484 for more up-to-date information on ``kunit_tool``.
486 .. _KUnit: https://www.kernel.org/doc/html/latest/dev-tools/kunit/index.html