hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/Documentation/dev-tools/kasan.rst
....@@ -4,25 +4,25 @@
44 Overview
55 --------
66
7
-KernelAddressSANitizer (KASAN) is a dynamic memory error detector designed to
8
-find out-of-bound and use-after-free bugs. KASAN has two modes: generic KASAN
9
-(similar to userspace ASan) and software tag-based KASAN (similar to userspace
10
-HWASan).
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:
119
12
-KASAN uses compile-time instrumentation to insert validity checks before every
13
-memory access, and therefore requires a compiler version that supports that.
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).
13
+
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.
1417
1518 Generic KASAN is supported in both GCC and Clang. With GCC it requires version
16
-4.9.2 or later for basic support and version 5.0 or later for detection of
17
-out-of-bounds accesses for stack and global variables and for inline
18
-instrumentation mode (see the Usage section). With Clang it requires version
19
-7.0.0 or later and it doesn't support detection of out-of-bounds accesses for
20
-global variables yet.
19
+8.3.0 or later. Any supported Clang version is compatible, but detection of
20
+out-of-bounds accesses for global variables is only supported since Clang 11.
2121
22
-Tag-based KASAN is only supported in Clang and requires version 7.0.0 or later.
22
+Tag-based KASAN is only supported in Clang.
2323
24
-Currently generic KASAN is supported for the x86_64, arm64, xtensa and s390
25
-architectures, and tag-based KASAN is supported only for arm64.
24
+Currently generic KASAN is supported for the x86_64, arm64, xtensa, s390 and
25
+and riscv architectures, and tag-based KASAN modes are supported only for arm64.
2626
2727 Usage
2828 -----
....@@ -31,26 +31,21 @@
3131
3232 CONFIG_KASAN = y
3333
34
-and choose between CONFIG_KASAN_GENERIC (to enable generic KASAN) and
35
-CONFIG_KASAN_SW_TAGS (to enable software tag-based KASAN).
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).
3637
37
-You also need to choose between CONFIG_KASAN_OUTLINE and CONFIG_KASAN_INLINE.
38
-Outline and inline are compiler instrumentation types. The former produces
39
-smaller binary while the latter is 1.1 - 2 times faster.
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.
4041
41
-Both KASAN modes work with both SLUB and SLAB memory allocators.
42
-For better bug detection and nicer reporting, enable CONFIG_STACKTRACE.
42
+Both software KASAN modes work with both SLUB and SLAB memory allocators,
43
+while the hardware tag-based KASAN currently only support SLUB.
4344
44
-To disable instrumentation for specific files or directories, add a line
45
-similar to the following to the respective kernel Makefile:
45
+For better error reports that include stack traces, enable CONFIG_STACKTRACE.
4646
47
-- For a single file (e.g. main.o)::
48
-
49
- KASAN_SANITIZE_main.o := n
50
-
51
-- For all files in one directory::
52
-
53
- KASAN_SANITIZE := n
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.
5449
5550 Error reports
5651 ~~~~~~~~~~~~~
....@@ -136,22 +131,68 @@
136131 the accessed slab object and information about the accessed memory page.
137132
138133 In the last section the report shows memory state around the accessed address.
139
-Reading this part requires some understanding of how KASAN works.
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.
140138
141
-The state of each 8 aligned bytes of memory is encoded in one shadow byte.
142
-Those 8 bytes can be accessible, partially accessible, freed or be a redzone.
143
-We use the following encoding for each shadow byte: 0 means that all 8 bytes
144
-of the corresponding memory region are accessible; number N (1 <= N <= 7) means
145
-that the first N bytes are accessible, and other (8 - N) bytes are not;
146
-any negative value indicates that the entire 8-byte word is inaccessible.
147
-We use different negative values to distinguish between different kinds of
148
-inaccessible memory like redzones or freed memory (see mm/kasan/kasan.h).
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).
149148
150149 In the report above the arrows point to the shadow byte 03, which means that
151
-the accessed address is partially accessible.
150
+the accessed address is partially accessible. For tag-based KASAN modes this
151
+last report section shows the memory tags around the accessed address
152
+(see the `Implementation details`_ section).
152153
153
-For tag-based KASAN this last report section shows the memory tags around the
154
-accessed address (see Implementation details section).
154
+Boot parameters
155
+~~~~~~~~~~~~~~~
156
+
157
+Hardware tag-based KASAN mode (see the section about various modes below) is
158
+intended for use in production as a security mitigation. Therefore, it supports
159
+boot parameters that allow to disable KASAN competely or otherwise control
160
+particular KASAN features.
161
+
162
+- ``kasan=off`` or ``=on`` controls whether KASAN is enabled (default: ``on``).
163
+
164
+- ``kasan.mode=sync`` or ``=async`` controls whether KASAN is configured in
165
+ synchronous or asynchronous mode of execution (default: ``sync``).
166
+ Synchronous mode: a bad access is detected immediately when a tag
167
+ check fault occurs.
168
+ Asynchronous mode: a bad access detection is delayed. When a tag check
169
+ fault occurs, the information is stored in hardware (in the TFSR_EL1
170
+ register for arm64). The kernel periodically checks the hardware and
171
+ only reports tag faults during these checks.
172
+
173
+- ``kasan.stacktrace=off`` or ``=on`` disables or enables alloc and free stack
174
+ traces collection (default: ``on``).
175
+
176
+- ``kasan.fault=report`` or ``=panic`` controls whether to only print a KASAN
177
+ report or also panic the kernel (default: ``report``). Note, that tag
178
+ checking gets disabled after the first reported bug.
179
+
180
+For developers
181
+~~~~~~~~~~~~~~
182
+
183
+Software KASAN modes use compiler instrumentation to insert validity checks.
184
+Such instrumentation might be incompatible with some part of the kernel, and
185
+therefore needs to be disabled. To disable instrumentation for specific files
186
+or directories, add a line similar to the following to the respective kernel
187
+Makefile:
188
+
189
+- For a single file (e.g. main.o)::
190
+
191
+ KASAN_SANITIZE_main.o := n
192
+
193
+- For all files in one directory::
194
+
195
+ KASAN_SANITIZE := n
155196
156197
157198 Implementation details
....@@ -160,10 +201,10 @@
160201 Generic KASAN
161202 ~~~~~~~~~~~~~
162203
163
-From a high level, our approach to memory error detection is similar to that
164
-of kmemcheck: use shadow memory to record whether each byte of memory is safe
165
-to access, and use compile-time instrumentation to insert checks of shadow
166
-memory on each memory access.
204
+From a high level perspective, KASAN's approach to memory error detection is
205
+similar to that of kmemcheck: use shadow memory to record whether each byte of
206
+memory is safe to access, and use compile-time instrumentation to insert checks
207
+of shadow memory on each memory access.
167208
168209 Generic KASAN dedicates 1/8th of kernel memory to its shadow memory (e.g. 16TB
169210 to cover 128TB on x86_64) and uses direct mapping with a scale and offset to
....@@ -190,20 +231,34 @@
190231 This option significantly enlarges kernel but it gives x1.1-x2 performance
191232 boost over outline instrumented kernel.
192233
234
+Generic KASAN also reports the last 2 call stacks to creation of work that
235
+potentially has access to an object. Call stacks for the following are shown:
236
+call_rcu() and workqueue queuing.
237
+
238
+Generic KASAN is the only mode that delays the reuse of freed object via
239
+quarantine (see mm/kasan/quarantine.c for implementation).
240
+
193241 Software tag-based KASAN
194242 ~~~~~~~~~~~~~~~~~~~~~~~~
195243
196
-Tag-based KASAN uses the Top Byte Ignore (TBI) feature of modern arm64 CPUs to
197
-store a pointer tag in the top byte of kernel pointers. Like generic KASAN it
198
-uses shadow memory to store memory tags associated with each 16-byte memory
244
+Software tag-based KASAN requires software memory tagging support in the form
245
+of HWASan-like compiler instrumentation (see HWASan documentation for details).
246
+
247
+Software tag-based KASAN is currently only implemented for arm64 architecture.
248
+
249
+Software tag-based KASAN uses the Top Byte Ignore (TBI) feature of arm64 CPUs
250
+to store a pointer tag in the top byte of kernel pointers. Like generic KASAN
251
+it uses shadow memory to store memory tags associated with each 16-byte memory
199252 cell (therefore it dedicates 1/16th of the kernel memory for shadow memory).
200253
201
-On each memory allocation tag-based KASAN generates a random tag, tags the
202
-allocated memory with this tag, and embeds this tag into the returned pointer.
254
+On each memory allocation software tag-based KASAN generates a random tag, tags
255
+the allocated memory with this tag, and embeds this tag into the returned
256
+pointer.
257
+
203258 Software tag-based KASAN uses compile-time instrumentation to insert checks
204259 before each memory access. These checks make sure that tag of the memory that
205260 is being accessed is equal to tag of the pointer that is used to access this
206
-memory. In case of a tag mismatch tag-based KASAN prints a bug report.
261
+memory. In case of a tag mismatch software tag-based KASAN prints a bug report.
207262
208263 Software tag-based KASAN also has two instrumentation modes (outline, that
209264 emits callbacks to check memory accesses; and inline, that performs the shadow
....@@ -212,6 +267,177 @@
212267 instrumentation a brk instruction is emitted by the compiler, and a dedicated
213268 brk handler is used to print bug reports.
214269
215
-A potential expansion of this mode is a hardware tag-based mode, which would
216
-use hardware memory tagging support instead of compiler instrumentation and
217
-manual shadow memory manipulation.
270
+Software tag-based KASAN uses 0xFF as a match-all pointer tag (accesses through
271
+pointers with 0xFF pointer tag aren't checked). The value 0xFE is currently
272
+reserved to tag freed memory regions.
273
+
274
+Software tag-based KASAN currently only supports tagging of
275
+kmem_cache_alloc/kmalloc and page_alloc memory.
276
+
277
+Hardware tag-based KASAN
278
+~~~~~~~~~~~~~~~~~~~~~~~~
279
+
280
+Hardware tag-based KASAN is similar to the software mode in concept, but uses
281
+hardware memory tagging support instead of compiler instrumentation and
282
+shadow memory.
283
+
284
+Hardware tag-based KASAN is currently only implemented for arm64 architecture
285
+and based on both arm64 Memory Tagging Extension (MTE) introduced in ARMv8.5
286
+Instruction Set Architecture, and Top Byte Ignore (TBI).
287
+
288
+Special arm64 instructions are used to assign memory tags for each allocation.
289
+Same tags are assigned to pointers to those allocations. On every memory
290
+access, hardware makes sure that tag of the memory that is being accessed is
291
+equal to tag of the pointer that is used to access this memory. In case of a
292
+tag mismatch a fault is generated and a report is printed.
293
+
294
+Hardware tag-based KASAN uses 0xFF as a match-all pointer tag (accesses through
295
+pointers with 0xFF pointer tag aren't checked). The value 0xFE is currently
296
+reserved to tag freed memory regions.
297
+
298
+Hardware tag-based KASAN currently only supports tagging of
299
+kmem_cache_alloc/kmalloc and page_alloc memory.
300
+
301
+If the hardware doesn't support MTE (pre ARMv8.5), hardware tag-based KASAN
302
+won't be enabled. In this case all boot parameters are ignored.
303
+
304
+Note, that enabling CONFIG_KASAN_HW_TAGS always results in in-kernel TBI being
305
+enabled. Even when kasan.mode=off is provided, or when the hardware doesn't
306
+support MTE (but supports TBI).
307
+
308
+Hardware tag-based KASAN only reports the first found bug. After that MTE tag
309
+checking gets disabled.
310
+
311
+What memory accesses are sanitised by KASAN?
312
+--------------------------------------------
313
+
314
+The kernel maps memory in a number of different parts of the address
315
+space. This poses something of a problem for KASAN, which requires
316
+that all addresses accessed by instrumented code have a valid shadow
317
+region.
318
+
319
+The range of kernel virtual addresses is large: there is not enough
320
+real memory to support a real shadow region for every address that
321
+could be accessed by the kernel.
322
+
323
+By default
324
+~~~~~~~~~~
325
+
326
+By default, architectures only map real memory over the shadow region
327
+for the linear mapping (and potentially other small areas). For all
328
+other areas - such as vmalloc and vmemmap space - a single read-only
329
+page is mapped over the shadow area. This read-only shadow page
330
+declares all memory accesses as permitted.
331
+
332
+This presents a problem for modules: they do not live in the linear
333
+mapping, but in a dedicated module space. By hooking in to the module
334
+allocator, KASAN can temporarily map real shadow memory to cover
335
+them. This allows detection of invalid accesses to module globals, for
336
+example.
337
+
338
+This also creates an incompatibility with ``VMAP_STACK``: if the stack
339
+lives in vmalloc space, it will be shadowed by the read-only page, and
340
+the kernel will fault when trying to set up the shadow data for stack
341
+variables.
342
+
343
+CONFIG_KASAN_VMALLOC
344
+~~~~~~~~~~~~~~~~~~~~
345
+
346
+With ``CONFIG_KASAN_VMALLOC``, KASAN can cover vmalloc space at the
347
+cost of greater memory usage. Currently this is only supported on x86.
348
+
349
+This works by hooking into vmalloc and vmap, and dynamically
350
+allocating real shadow memory to back the mappings.
351
+
352
+Most mappings in vmalloc space are small, requiring less than a full
353
+page of shadow space. Allocating a full shadow page per mapping would
354
+therefore be wasteful. Furthermore, to ensure that different mappings
355
+use different shadow pages, mappings would have to be aligned to
356
+``KASAN_GRANULE_SIZE * PAGE_SIZE``.
357
+
358
+Instead, KASAN shares backing space across multiple mappings. It allocates
359
+a backing page when a mapping in vmalloc space uses a particular page
360
+of the shadow region. This page can be shared by other vmalloc
361
+mappings later on.
362
+
363
+KASAN hooks into the vmap infrastructure to lazily clean up unused shadow
364
+memory.
365
+
366
+To avoid the difficulties around swapping mappings around, KASAN expects
367
+that the part of the shadow region that covers the vmalloc space will
368
+not be covered by the early shadow page, but will be left
369
+unmapped. This will require changes in arch-specific code.
370
+
371
+This allows ``VMAP_STACK`` support on x86, and can simplify support of
372
+architectures that do not have a fixed module region.
373
+
374
+CONFIG_KASAN_KUNIT_TEST and CONFIG_KASAN_MODULE_TEST
375
+----------------------------------------------------
376
+
377
+KASAN tests consist of two parts:
378
+
379
+1. Tests that are integrated with the KUnit Test Framework. Enabled with
380
+``CONFIG_KASAN_KUNIT_TEST``. These tests can be run and partially verified
381
+automatically in a few different ways, see the instructions below.
382
+
383
+2. Tests that are currently incompatible with KUnit. Enabled with
384
+``CONFIG_KASAN_MODULE_TEST`` and can only be run as a module. These tests can
385
+only be verified manually, by loading the kernel module and inspecting the
386
+kernel log for KASAN reports.
387
+
388
+Each KUnit-compatible KASAN test prints a KASAN report if an error is detected.
389
+Then the test prints its number and status.
390
+
391
+When a test passes::
392
+
393
+ ok 28 - kmalloc_double_kzfree
394
+
395
+When a test fails due to a failed ``kmalloc``::
396
+
397
+ # kmalloc_large_oob_right: ASSERTION FAILED at lib/test_kasan.c:163
398
+ Expected ptr is not null, but is
399
+ not ok 4 - kmalloc_large_oob_right
400
+
401
+When a test fails due to a missing KASAN report::
402
+
403
+ # kmalloc_double_kzfree: EXPECTATION FAILED at lib/test_kasan.c:629
404
+ Expected kasan_data->report_expected == kasan_data->report_found, but
405
+ kasan_data->report_expected == 1
406
+ kasan_data->report_found == 0
407
+ not ok 28 - kmalloc_double_kzfree
408
+
409
+At the end the cumulative status of all KASAN tests is printed. On success::
410
+
411
+ ok 1 - kasan
412
+
413
+Or, if one of the tests failed::
414
+
415
+ not ok 1 - kasan
416
+
417
+
418
+There are a few ways to run KUnit-compatible KASAN tests.
419
+
420
+1. Loadable module
421
+~~~~~~~~~~~~~~~~~~
422
+
423
+With ``CONFIG_KUNIT`` enabled, ``CONFIG_KASAN_KUNIT_TEST`` can be built as
424
+a loadable module and run on any architecture that supports KASAN by loading
425
+the module with insmod or modprobe. The module is called ``test_kasan``.
426
+
427
+2. Built-In
428
+~~~~~~~~~~~
429
+
430
+With ``CONFIG_KUNIT`` built-in, ``CONFIG_KASAN_KUNIT_TEST`` can be built-in
431
+on any architecure that supports KASAN. These and any other KUnit tests enabled
432
+will run and print the results at boot as a late-init call.
433
+
434
+3. Using kunit_tool
435
+~~~~~~~~~~~~~~~~~~~
436
+
437
+With ``CONFIG_KUNIT`` and ``CONFIG_KASAN_KUNIT_TEST`` built-in, it's also
438
+possible use ``kunit_tool`` to see the results of these and other KUnit tests
439
+in a more readable way. This will not print the KASAN reports of the tests that
440
+passed. Use `KUnit documentation <https://www.kernel.org/doc/html/latest/dev-tools/kunit/index.html>`_
441
+for more up-to-date information on ``kunit_tool``.
442
+
443
+.. _KUnit: https://www.kernel.org/doc/html/latest/dev-tools/kunit/index.html