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.\" 282df97575SZhenlei Huang.Dd October 13, 2023 2938da497aSMark Johnston.Dt KASAN 9 3038da497aSMark Johnston.Os 3138da497aSMark Johnston.Sh NAME 32420d30f5SMark Johnston.Nm KASAN 33420d30f5SMark Johnston.Nd Kernel Address SANitizer 3438da497aSMark Johnston.Sh SYNOPSIS 35420d30f5SMark JohnstonThe 36420d30f5SMark Johnston.Pa GENERIC-KASAN 37420d30f5SMark Johnstonkernel configuration can be used to compile a KASAN-enabled kernel using 38420d30f5SMark Johnston.Pa GENERIC 39420d30f5SMark Johnstonas a base configuration. 4093d8b4daSMark JohnstonAlternately, to compile KASAN into the kernel, place the following line in your 41420d30f5SMark Johnstonkernel configuration file: 4238da497aSMark Johnston.Bd -ragged -offset indent 4338da497aSMark Johnston.Cd "options KASAN" 4438da497aSMark Johnston.Ed 4538da497aSMark Johnston.Pp 46e0cc5660SMark Johnston.In sys/asan.h 4738da497aSMark Johnston.Ft void 4838da497aSMark Johnston.Fn kasan_mark "const void *addr" "size_t size" "size_t redzsize" "uint8_t code" 4938da497aSMark Johnston.Sh DESCRIPTION 5038da497aSMark Johnston.Nm 5138da497aSMark Johnstonis a subsystem which leverages compiler instrumentation to detect invalid 5238da497aSMark Johnstonmemory accesses in the kernel. 5340924005SMark JohnstonCurrently it is implemented on the amd64 and arm64 platforms. 5438da497aSMark Johnston.Pp 5538da497aSMark JohnstonWhen 5638da497aSMark Johnston.Nm 5738da497aSMark Johnstonis compiled into the kernel, the compiler is configured to emit function 5838da497aSMark Johnstoncalls upon every memory access. 5938da497aSMark JohnstonThe functions are implemented by 6038da497aSMark Johnston.Nm 6138da497aSMark Johnstonand permit run-time detection of several types of bugs including 6238da497aSMark Johnstonuse-after-frees, double frees and frees of invalid pointers, and out-of-bounds 6338da497aSMark Johnstonaccesses. 6438da497aSMark JohnstonThese protections apply to memory allocated by 6538da497aSMark Johnston.Xr uma 9 , 6638da497aSMark Johnston.Xr malloc 9 6738da497aSMark Johnstonand related functions, and 6838da497aSMark Johnston.Fn kmem_malloc 6938da497aSMark Johnstonand related functions, 7038da497aSMark Johnstonas well as global variables and kernel stacks. 7138da497aSMark Johnston.Nm 7238da497aSMark Johnstonis conservative and will not detect all instances of these types of bugs. 7338da497aSMark JohnstonMemory accesses through the kernel map are sanitized, but accesses via the 7438da497aSMark Johnstondirect map are not. 7538da497aSMark JohnstonWhen 7638da497aSMark Johnston.Nm 7738da497aSMark Johnstonis configured, the kernel aims to minimize its use of the direct map. 7838da497aSMark Johnston.Sh IMPLEMENTATION NOTES 7938da497aSMark Johnston.Nm 8038da497aSMark Johnstonis implemented using compiler instrumentation and a kernel runtime. 8138da497aSMark JohnstonWhen a 8238da497aSMark Johnstonkernel is built with the KASAN option enabled, the compiler inserts function calls 8338da497aSMark Johnstonbefore most memory accesses in the generated code. 8438da497aSMark JohnstonThe runtime implements the corresponding functions, which decide whether a 8538da497aSMark Johnstongiven access is valid. 8638da497aSMark JohnstonIf not, the runtime prints a warning or panics the kernel, depending on the 8738da497aSMark Johnstonvalue of the 8838da497aSMark Johnston.Sy debug.kasan.panic_on_violation 8938da497aSMark Johnstonsysctl/tunable. 9038da497aSMark Johnston.Pp 9138da497aSMark JohnstonThe 9238da497aSMark Johnston.Nm 932df97575SZhenlei Huangruntime in a KASAN-configured kernel can be disabled by 942df97575SZhenlei Huangsetting the loader tunable 952df97575SZhenlei Huang.Sy debug.kasan.disable=1 . 962df97575SZhenlei Huang.Pp 972df97575SZhenlei HuangThe 982df97575SZhenlei Huang.Nm 9938da497aSMark Johnstonruntime works by maintaining a shadow map for the kernel map. 10038da497aSMark JohnstonThere exists a linear mapping between addresses in the kernel map and addresses 10138da497aSMark Johnstonin the shadow map. 10238da497aSMark JohnstonThe shadow map is used to store information about the current state of 10338da497aSMark Johnstonallocations from the kernel map. 10438da497aSMark JohnstonFor example, when a buffer is returned by 10538da497aSMark Johnston.Xr malloc 9 , 10638da497aSMark Johnstonthe corresponding region of the shadow map is marked to indicate that the 10738da497aSMark Johnstonbuffer is valid. 10838da497aSMark JohnstonWhen it is freed, the shadow map is updated to mark the buffer as invalid. 10938da497aSMark JohnstonAccesses to the buffer are intercepted by the 11038da497aSMark Johnston.Nm 11138da497aSMark Johnstonruntime and validated using the contents of the shadow map. 11238da497aSMark Johnston.Pp 11338da497aSMark JohnstonUpon booting, all kernel memory is marked as valid. 11438da497aSMark JohnstonKernel allocators must mark cached but free buffers as invalid, and must mark 11538da497aSMark Johnstonthem valid before freeing the kernel virtual address range. 11638da497aSMark JohnstonThis slightly reduces the effectiveness of 11738da497aSMark Johnston.Nm 11838da497aSMark Johnstonbut simplifies its maintenance and integration into the kernel. 11938da497aSMark Johnston.Pp 12038da497aSMark JohnstonUpdates to the shadow map are performed by calling 12138da497aSMark Johnston.Fn kasan_mark . 12238da497aSMark JohnstonParameter 12338da497aSMark Johnston.Fa addr 12438da497aSMark Johnstonis the address of the buffer whose shadow is to be updated, 12538da497aSMark Johnston.Fa size 12638da497aSMark Johnstonis the usable size of the buffer, and 12738da497aSMark Johnston.Fa redzsize 12838da497aSMark Johnstonis the full size of the buffer allocated from lower layers of the system. 12938da497aSMark Johnston.Fa redzsize 13038da497aSMark Johnstonmust be greater than or equal to 13138da497aSMark Johnston.Fa size . 13238da497aSMark JohnstonIn some cases kernel allocators will return a buffer larger than that requested 13338da497aSMark Johnstonby the consumer; the unused space at the end is referred to as a red zone and is 13438da497aSMark Johnstonalways marked as invalid. 13538da497aSMark Johnston.Fa code 13638da497aSMark Johnstonallows the caller to specify an identifier used when marking a buffer as invalid. 13738da497aSMark JohnstonThe identifier is included in any reports generated by 13838da497aSMark Johnston.Nm 13938da497aSMark Johnstonand helps identify the source of the invalid access. 14038da497aSMark JohnstonFor instance, when an item is freed to a 14138da497aSMark Johnston.Xr uma 9 14238da497aSMark Johnstonzone, the item is marked with 14338da497aSMark Johnston.Dv KASAN_UMA_FREED . 14438da497aSMark JohnstonSee 14538da497aSMark Johnston.In sys/asan.h 14638da497aSMark Johnstonfor the available identifiers. 14738da497aSMark JohnstonIf the entire buffer is to be marked valid, i.e., 14838da497aSMark Johnston.Fa size 14938da497aSMark Johnstonand 15038da497aSMark Johnston.Fa redzsize 15138da497aSMark Johnstonare equal, 15238da497aSMark Johnston.Fa code 15338da497aSMark Johnstonshould be 0. 15438da497aSMark Johnston.Sh SEE ALSO 155420d30f5SMark Johnston.Xr build 7 , 156ce260994SMark Johnston.Xr KMSAN 9 , 15738da497aSMark Johnston.Xr malloc 9 , 15838da497aSMark Johnston.Xr memguard 9 , 15938da497aSMark Johnston.Xr redzone 9 , 16038da497aSMark Johnston.Xr uma 9 16138da497aSMark Johnston.Sh HISTORY 16238da497aSMark Johnston.Nm 163420d30f5SMark Johnstonwas ported from 164420d30f5SMark Johnston.Nx 165420d30f5SMark Johnstonand first appeared in 166*ba719a0fSTom Hukins.Fx 13.1 . 16738da497aSMark Johnston.Sh BUGS 16838da497aSMark JohnstonAccesses to kernel memory outside of the kernel map are ignored by the 16938da497aSMark Johnston.Nm 17038da497aSMark Johnstonruntime. 17138da497aSMark JohnstonWhen 17238da497aSMark Johnston.Nm 17338da497aSMark Johnstonis configured, the kernel memory allocators are configured to use the kernel 17438da497aSMark Johnstonmap, but some uses of the direct map remain. 17540924005SMark JohnstonFor example, on amd64 and arm64, accesses to page table pages are not tracked. 17638da497aSMark Johnston.Pp 17738da497aSMark JohnstonSome kernel memory allocators explicitly permit accesses after an object has 17838da497aSMark Johnstonbeen freed. 17938da497aSMark JohnstonThese cannot be sanitized by 18038da497aSMark Johnston.Nm . 18138da497aSMark JohnstonFor example, memory from all 18238da497aSMark Johnston.Xr uma 9 18338da497aSMark Johnstonzones initialized with the 18438da497aSMark Johnston.Dv UMA_ZONE_NOFREE 18538da497aSMark Johnstonflag are not sanitized. 186