xref: /freebsd/share/man/man9/kasan.9 (revision ce2609947c2d5433ed4ebb25f9a728340008ff92)
138da497aSMark Johnston.\"-
238da497aSMark Johnston.\" Copyright (c) 2021 The FreeBSD Foundation
338da497aSMark Johnston.\"
438da497aSMark Johnston.\" This documentation was written by Mark Johnston under sponsorship from
538da497aSMark Johnston.\" the FreeBSD Foundation.
638da497aSMark Johnston.\"
738da497aSMark Johnston.\" Redistribution and use in source and binary forms, with or without
838da497aSMark Johnston.\" modification, are permitted provided that the following conditions
938da497aSMark Johnston.\" are met:
1038da497aSMark Johnston.\" 1. Redistributions of source code must retain the above copyright
1138da497aSMark Johnston.\"    notice, this list of conditions and the following disclaimer.
1238da497aSMark Johnston.\" 2. Redistributions in binary form must reproduce the above copyright
1338da497aSMark Johnston.\"    notice, this list of conditions and the following disclaimer in the
1438da497aSMark Johnston.\"    documentation and/or other materials provided with the distribution.
1538da497aSMark Johnston.\"
1638da497aSMark Johnston.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1738da497aSMark Johnston.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1838da497aSMark Johnston.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1938da497aSMark Johnston.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2038da497aSMark Johnston.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2138da497aSMark Johnston.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2238da497aSMark Johnston.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2338da497aSMark Johnston.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2438da497aSMark Johnston.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2538da497aSMark Johnston.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2638da497aSMark Johnston.\" SUCH DAMAGE.
2738da497aSMark Johnston.\"
2838da497aSMark Johnston.\" $FreeBSD$
2938da497aSMark Johnston.\"
30420d30f5SMark Johnston.Dd April 29, 2021
3138da497aSMark Johnston.Dt KASAN 9
3238da497aSMark Johnston.Os
3338da497aSMark Johnston.Sh NAME
34420d30f5SMark Johnston.Nm KASAN
35420d30f5SMark Johnston.Nd Kernel Address SANitizer
3638da497aSMark Johnston.Sh SYNOPSIS
37420d30f5SMark JohnstonThe
38420d30f5SMark Johnston.Pa GENERIC-KASAN
39420d30f5SMark Johnstonkernel configuration can be used to compile a KASAN-enabled kernel using
40420d30f5SMark Johnston.Pa GENERIC
41420d30f5SMark Johnstonas a base configuration.
4293d8b4daSMark JohnstonAlternately, to compile KASAN into the kernel, place the following line in your
43420d30f5SMark Johnstonkernel configuration file:
4438da497aSMark Johnston.Bd -ragged -offset indent
4538da497aSMark Johnston.Cd "options KASAN"
4638da497aSMark Johnston.Ed
4738da497aSMark Johnston.Pp
48e0cc5660SMark Johnston.In sys/asan.h
4938da497aSMark Johnston.Ft void
5038da497aSMark Johnston.Fn kasan_mark "const void *addr" "size_t size" "size_t redzsize" "uint8_t code"
5138da497aSMark Johnston.Sh DESCRIPTION
5238da497aSMark Johnston.Nm
5338da497aSMark Johnstonis a subsystem which leverages compiler instrumentation to detect invalid
5438da497aSMark Johnstonmemory accesses in the kernel.
5538da497aSMark JohnstonCurrently it is implemented only on the amd64 platform.
5638da497aSMark Johnston.Pp
5738da497aSMark JohnstonWhen
5838da497aSMark Johnston.Nm
5938da497aSMark Johnstonis compiled into the kernel, the compiler is configured to emit function
6038da497aSMark Johnstoncalls upon every memory access.
6138da497aSMark JohnstonThe functions are implemented by
6238da497aSMark Johnston.Nm
6338da497aSMark Johnstonand permit run-time detection of several types of bugs including
6438da497aSMark Johnstonuse-after-frees, double frees and frees of invalid pointers, and out-of-bounds
6538da497aSMark Johnstonaccesses.
6638da497aSMark JohnstonThese protections apply to memory allocated by
6738da497aSMark Johnston.Xr uma 9 ,
6838da497aSMark Johnston.Xr malloc 9
6938da497aSMark Johnstonand related functions, and
7038da497aSMark Johnston.Fn kmem_malloc
7138da497aSMark Johnstonand related functions,
7238da497aSMark Johnstonas well as global variables and kernel stacks.
7338da497aSMark Johnston.Nm
7438da497aSMark Johnstonis conservative and will not detect all instances of these types of bugs.
7538da497aSMark JohnstonMemory accesses through the kernel map are sanitized, but accesses via the
7638da497aSMark Johnstondirect map are not.
7738da497aSMark JohnstonWhen
7838da497aSMark Johnston.Nm
7938da497aSMark Johnstonis configured, the kernel aims to minimize its use of the direct map.
8038da497aSMark Johnston.Sh IMPLEMENTATION NOTES
8138da497aSMark Johnston.Nm
8238da497aSMark Johnstonis implemented using compiler instrumentation and a kernel runtime.
8338da497aSMark JohnstonWhen a
8438da497aSMark Johnstonkernel is built with the KASAN option enabled, the compiler inserts function calls
8538da497aSMark Johnstonbefore most memory accesses in the generated code.
8638da497aSMark JohnstonThe runtime implements the corresponding functions, which decide whether a
8738da497aSMark Johnstongiven access is valid.
8838da497aSMark JohnstonIf not, the runtime prints a warning or panics the kernel, depending on the
8938da497aSMark Johnstonvalue of the
9038da497aSMark Johnston.Sy debug.kasan.panic_on_violation
9138da497aSMark Johnstonsysctl/tunable.
9238da497aSMark Johnston.Pp
9338da497aSMark JohnstonThe
9438da497aSMark Johnston.Nm
9538da497aSMark Johnstonruntime works by maintaining a shadow map for the kernel map.
9638da497aSMark JohnstonThere exists a linear mapping between addresses in the kernel map and addresses
9738da497aSMark Johnstonin the shadow map.
9838da497aSMark JohnstonThe shadow map is used to store information about the current state of
9938da497aSMark Johnstonallocations from the kernel map.
10038da497aSMark JohnstonFor example, when a buffer is returned by
10138da497aSMark Johnston.Xr malloc 9 ,
10238da497aSMark Johnstonthe corresponding region of the shadow map is marked to indicate that the
10338da497aSMark Johnstonbuffer is valid.
10438da497aSMark JohnstonWhen it is freed, the shadow map is updated to mark the buffer as invalid.
10538da497aSMark JohnstonAccesses to the buffer are intercepted by the
10638da497aSMark Johnston.Nm
10738da497aSMark Johnstonruntime and validated using the contents of the shadow map.
10838da497aSMark Johnston.Pp
10938da497aSMark JohnstonUpon booting, all kernel memory is marked as valid.
11038da497aSMark JohnstonKernel allocators must mark cached but free buffers as invalid, and must mark
11138da497aSMark Johnstonthem valid before freeing the kernel virtual address range.
11238da497aSMark JohnstonThis slightly reduces the effectiveness of
11338da497aSMark Johnston.Nm
11438da497aSMark Johnstonbut simplifies its maintenance and integration into the kernel.
11538da497aSMark Johnston.Pp
11638da497aSMark JohnstonUpdates to the shadow map are performed by calling
11738da497aSMark Johnston.Fn kasan_mark .
11838da497aSMark JohnstonParameter
11938da497aSMark Johnston.Fa addr
12038da497aSMark Johnstonis the address of the buffer whose shadow is to be updated,
12138da497aSMark Johnston.Fa size
12238da497aSMark Johnstonis the usable size of the buffer, and
12338da497aSMark Johnston.Fa redzsize
12438da497aSMark Johnstonis the full size of the buffer allocated from lower layers of the system.
12538da497aSMark Johnston.Fa redzsize
12638da497aSMark Johnstonmust be greater than or equal to
12738da497aSMark Johnston.Fa size .
12838da497aSMark JohnstonIn some cases kernel allocators will return a buffer larger than that requested
12938da497aSMark Johnstonby the consumer; the unused space at the end is referred to as a red zone and is
13038da497aSMark Johnstonalways marked as invalid.
13138da497aSMark Johnston.Fa code
13238da497aSMark Johnstonallows the caller to specify an identifier used when marking a buffer as invalid.
13338da497aSMark JohnstonThe identifier is included in any reports generated by
13438da497aSMark Johnston.Nm
13538da497aSMark Johnstonand helps identify the source of the invalid access.
13638da497aSMark JohnstonFor instance, when an item is freed to a
13738da497aSMark Johnston.Xr uma 9
13838da497aSMark Johnstonzone, the item is marked with
13938da497aSMark Johnston.Dv KASAN_UMA_FREED .
14038da497aSMark JohnstonSee
14138da497aSMark Johnston.In sys/asan.h
14238da497aSMark Johnstonfor the available identifiers.
14338da497aSMark JohnstonIf the entire buffer is to be marked valid, i.e.,
14438da497aSMark Johnston.Fa size
14538da497aSMark Johnstonand
14638da497aSMark Johnston.Fa redzsize
14738da497aSMark Johnstonare equal,
14838da497aSMark Johnston.Fa code
14938da497aSMark Johnstonshould be 0.
15038da497aSMark Johnston.Sh SEE ALSO
151420d30f5SMark Johnston.Xr build 7 ,
152*ce260994SMark Johnston.Xr KMSAN 9 ,
15338da497aSMark Johnston.Xr malloc 9 ,
15438da497aSMark Johnston.Xr memguard 9 ,
15538da497aSMark Johnston.Xr redzone 9 ,
15638da497aSMark Johnston.Xr uma 9
15738da497aSMark Johnston.Sh HISTORY
15838da497aSMark Johnston.Nm
159420d30f5SMark Johnstonwas ported from
160420d30f5SMark Johnston.Nx
161420d30f5SMark Johnstonand first appeared in
16238da497aSMark Johnston.Fx 14.0 .
16338da497aSMark Johnston.Sh BUGS
16438da497aSMark JohnstonAccesses to kernel memory outside of the kernel map are ignored by the
16538da497aSMark Johnston.Nm
16638da497aSMark Johnstonruntime.
16738da497aSMark JohnstonWhen
16838da497aSMark Johnston.Nm
16938da497aSMark Johnstonis configured, the kernel memory allocators are configured to use the kernel
17038da497aSMark Johnstonmap, but some uses of the direct map remain.
17138da497aSMark JohnstonFor example, on amd64, accesses to page table pages are not tracked.
17238da497aSMark Johnston.Pp
17338da497aSMark JohnstonSome kernel memory allocators explicitly permit accesses after an object has
17438da497aSMark Johnstonbeen freed.
17538da497aSMark JohnstonThese cannot be sanitized by
17638da497aSMark Johnston.Nm .
17738da497aSMark JohnstonFor example, memory from all
17838da497aSMark Johnston.Xr uma 9
17938da497aSMark Johnstonzones initialized with the
18038da497aSMark Johnston.Dv UMA_ZONE_NOFREE
18138da497aSMark Johnstonflag are not sanitized.
182