Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

arm64: kasan: Switch to using KASAN_SHADOW_OFFSET

KASAN_SHADOW_OFFSET is a constant that is supplied to gcc as a command
line argument and affects the codegen of the inline address sanetiser.

Essentially, for an example memory access:
*ptr1 = val;
The compiler will insert logic similar to the below:
shadowValue = *(ptr1 >> KASAN_SHADOW_SCALE_SHIFT + KASAN_SHADOW_OFFSET)
if (somethingWrong(shadowValue))
flagAnError();

This code sequence is inserted into many places, thus
KASAN_SHADOW_OFFSET is essentially baked into many places in the kernel
text.

If we want to run a single kernel binary with multiple address spaces,
then we need to do this with KASAN_SHADOW_OFFSET fixed.

Thankfully, due to the way the KASAN_SHADOW_OFFSET is used to provide
shadow addresses we know that the end of the shadow region is constant
w.r.t. VA space size:
KASAN_SHADOW_END = ~0 >> KASAN_SHADOW_SCALE_SHIFT + KASAN_SHADOW_OFFSET

This means that if we increase the size of the VA space, the start of
the KASAN region expands into lower addresses whilst the end of the
KASAN region is fixed.

Currently the arm64 code computes KASAN_SHADOW_OFFSET at build time via
build scripts with the VA size used as a parameter. (There are build
time checks in the C code too to ensure that expected values are being
derived). It is sufficient, and indeed is a simplification, to remove
the build scripts (and build time checks) entirely and instead provide
KASAN_SHADOW_OFFSET values.

This patch removes the logic to compute the KASAN_SHADOW_OFFSET in the
arm64 Makefile, and instead we adopt the approach used by x86 to supply
offset values in kConfig. To help debug/develop future VA space changes,
the Makefile logic has been preserved in a script file in the arm64
Documentation folder.

Reviewed-by: Catalin Marinas <catalin.marinas@arm.com>
Signed-off-by: Steve Capper <steve.capper@arm.com>
Signed-off-by: Will Deacon <will@kernel.org>

authored by

Steve Capper and committed by
Will Deacon
6bd1d0be 14c127c9

+51 -18
+27
Documentation/arm64/kasan-offsets.sh
··· 1 + #!/bin/sh 2 + 3 + # Print out the KASAN_SHADOW_OFFSETS required to place the KASAN SHADOW 4 + # start address at the mid-point of the kernel VA space 5 + 6 + print_kasan_offset () { 7 + printf "%02d\t" $1 8 + printf "0x%08x00000000\n" $(( (0xffffffff & (-1 << ($1 - 1 - 32))) \ 9 + + (1 << ($1 - 32 - $2)) \ 10 + - (1 << (64 - 32 - $2)) )) 11 + } 12 + 13 + echo KASAN_SHADOW_SCALE_SHIFT = 3 14 + printf "VABITS\tKASAN_SHADOW_OFFSET\n" 15 + print_kasan_offset 48 3 16 + print_kasan_offset 47 3 17 + print_kasan_offset 42 3 18 + print_kasan_offset 39 3 19 + print_kasan_offset 36 3 20 + echo 21 + echo KASAN_SHADOW_SCALE_SHIFT = 4 22 + printf "VABITS\tKASAN_SHADOW_OFFSET\n" 23 + print_kasan_offset 48 4 24 + print_kasan_offset 47 4 25 + print_kasan_offset 42 4 26 + print_kasan_offset 39 4 27 + print_kasan_offset 36 4
+15
arch/arm64/Kconfig
··· 297 297 config ARCH_PROC_KCORE_TEXT 298 298 def_bool y 299 299 300 + config KASAN_SHADOW_OFFSET 301 + hex 302 + depends on KASAN 303 + default 0xdfffa00000000000 if (ARM64_VA_BITS_48 || ARM64_USER_VA_BITS_52) && !KASAN_SW_TAGS 304 + default 0xdfffd00000000000 if ARM64_VA_BITS_47 && !KASAN_SW_TAGS 305 + default 0xdffffe8000000000 if ARM64_VA_BITS_42 && !KASAN_SW_TAGS 306 + default 0xdfffffd000000000 if ARM64_VA_BITS_39 && !KASAN_SW_TAGS 307 + default 0xdffffffa00000000 if ARM64_VA_BITS_36 && !KASAN_SW_TAGS 308 + default 0xefff900000000000 if (ARM64_VA_BITS_48 || ARM64_USER_VA_BITS_52) && KASAN_SW_TAGS 309 + default 0xefffc80000000000 if ARM64_VA_BITS_47 && KASAN_SW_TAGS 310 + default 0xeffffe4000000000 if ARM64_VA_BITS_42 && KASAN_SW_TAGS 311 + default 0xefffffc800000000 if ARM64_VA_BITS_39 && KASAN_SW_TAGS 312 + default 0xeffffff900000000 if ARM64_VA_BITS_36 && KASAN_SW_TAGS 313 + default 0xffffffffffffffff 314 + 300 315 source "arch/arm64/Kconfig.platforms" 301 316 302 317 menu "Kernel Features"
-8
arch/arm64/Makefile
··· 126 126 KBUILD_CPPFLAGS += -DKASAN_SHADOW_SCALE_SHIFT=$(KASAN_SHADOW_SCALE_SHIFT) 127 127 KBUILD_AFLAGS += -DKASAN_SHADOW_SCALE_SHIFT=$(KASAN_SHADOW_SCALE_SHIFT) 128 128 129 - # KASAN_SHADOW_OFFSET = VA_START + (1 << (VA_BITS - KASAN_SHADOW_SCALE_SHIFT)) 130 - # - (1 << (64 - KASAN_SHADOW_SCALE_SHIFT)) 131 - # in 32-bit arithmetic 132 - KASAN_SHADOW_OFFSET := $(shell printf "0x%08x00000000\n" $$(( \ 133 - (0xffffffff & (-1 << ($(CONFIG_ARM64_VA_BITS) - 1 - 32))) \ 134 - + (1 << ($(CONFIG_ARM64_VA_BITS) - 32 - $(KASAN_SHADOW_SCALE_SHIFT))) \ 135 - - (1 << (64 - 32 - $(KASAN_SHADOW_SCALE_SHIFT))) )) ) 136 - 137 129 export TEXT_OFFSET GZFLAGS 138 130 139 131 core-y += arch/arm64/kernel/ arch/arm64/mm/
+4 -7
arch/arm64/include/asm/kasan.h
··· 18 18 * KASAN_SHADOW_START: beginning of the kernel virtual addresses. 19 19 * KASAN_SHADOW_END: KASAN_SHADOW_START + 1/N of kernel virtual addresses, 20 20 * where N = (1 << KASAN_SHADOW_SCALE_SHIFT). 21 - */ 22 - #define KASAN_SHADOW_START (VA_START) 23 - #define KASAN_SHADOW_END (KASAN_SHADOW_START + KASAN_SHADOW_SIZE) 24 - 25 - /* 21 + * 22 + * KASAN_SHADOW_OFFSET: 26 23 * This value is used to map an address to the corresponding shadow 27 24 * address by the following formula: 28 25 * shadow_addr = (address >> KASAN_SHADOW_SCALE_SHIFT) + KASAN_SHADOW_OFFSET ··· 30 33 * KASAN_SHADOW_OFFSET = KASAN_SHADOW_END - 31 34 * (1ULL << (64 - KASAN_SHADOW_SCALE_SHIFT)) 32 35 */ 33 - #define KASAN_SHADOW_OFFSET (KASAN_SHADOW_END - (1ULL << \ 34 - (64 - KASAN_SHADOW_SCALE_SHIFT))) 36 + #define _KASAN_SHADOW_START(va) (KASAN_SHADOW_END - (1UL << ((va) - KASAN_SHADOW_SCALE_SHIFT))) 37 + #define KASAN_SHADOW_START _KASAN_SHADOW_START(VA_BITS) 35 38 36 39 void kasan_init(void); 37 40 void kasan_copy_shadow(pgd_t *pgdir);
+5 -3
arch/arm64/include/asm/memory.h
··· 42 42 #define PAGE_OFFSET (UL(0xffffffffffffffff) - \ 43 43 (UL(1) << VA_BITS) + 1) 44 44 #define KIMAGE_VADDR (MODULES_END) 45 - #define BPF_JIT_REGION_START (VA_START + KASAN_SHADOW_SIZE) 45 + #define BPF_JIT_REGION_START (KASAN_SHADOW_END) 46 46 #define BPF_JIT_REGION_SIZE (SZ_128M) 47 47 #define BPF_JIT_REGION_END (BPF_JIT_REGION_START + BPF_JIT_REGION_SIZE) 48 48 #define MODULES_END (MODULES_VADDR + MODULES_VSIZE) ··· 68 68 * significantly, so double the (minimum) stack size when they are in use. 69 69 */ 70 70 #ifdef CONFIG_KASAN 71 - #define KASAN_SHADOW_SIZE (UL(1) << (VA_BITS - KASAN_SHADOW_SCALE_SHIFT)) 71 + #define KASAN_SHADOW_OFFSET _AC(CONFIG_KASAN_SHADOW_OFFSET, UL) 72 + #define KASAN_SHADOW_END ((UL(1) << (64 - KASAN_SHADOW_SCALE_SHIFT)) \ 73 + + KASAN_SHADOW_OFFSET) 72 74 #define KASAN_THREAD_SHIFT 1 73 75 #else 74 - #define KASAN_SHADOW_SIZE (0) 75 76 #define KASAN_THREAD_SHIFT 0 77 + #define KASAN_SHADOW_END (VA_START) 76 78 #endif 77 79 78 80 #define MIN_THREAD_SHIFT (14 + KASAN_THREAD_SHIFT)