1 /* SPDX-License-Identifier: GPL-2.0-only */ 1 2 #ifndef _LINUX_RANDOMIZE_KSTACK_H 3 #define _LINUX_RANDOMIZE_KSTACK_H 4 5 #ifdef CONFIG_RANDOMIZE_KSTACK_OFFSET 6 #include <linux/kernel.h> 7 #include <linux/jump_label.h> 8 #include <linux/percpu-defs.h> 9 10 DECLARE_STATIC_KEY_MAYBE(CONFIG_RANDOMIZE_KSTA 11 randomize_kstack_offs 12 DECLARE_PER_CPU(u32, kstack_offset); 13 14 /* 15 * Do not use this anywhere else in the kernel 16 * it provides an arch-agnostic way to grow th 17 * alignment. Also, since this use is being ex 18 * 10 bits, stack-clash style attacks are unli 19 * "VLAs" in Documentation/process/deprecated. 20 * 21 * The normal __builtin_alloca() is initialize 22 * only with Clang and not GCC). Initializing 23 * entry is expensive, and generating an impli 24 * problematic (such as in noinstr functions). 25 * supports it (which it should if it initiali 26 * "uninitialized" variant of the builtin. 27 */ 28 #if __has_builtin(__builtin_alloca_uninitializ 29 #define __kstack_alloca __builtin_alloca_unini 30 #else 31 #define __kstack_alloca __builtin_alloca 32 #endif 33 34 /* 35 * Use, at most, 6 bits of entropy (on 64-bit; 36 * to keep the "VLA" from being unbounded (see 37 * the bottom 4 bits (on 64-bit systems, 2 for 38 * alignment will always be at least word size 39 * code gen better when it is applying the act 40 * the final offset. The resulting randomness 41 * constraining usable stack space. 42 */ 43 #ifdef CONFIG_64BIT 44 #define KSTACK_OFFSET_MAX(x) ((x) & 0b11111 45 #else 46 #define KSTACK_OFFSET_MAX(x) ((x) & 0b11111 47 #endif 48 49 /** 50 * add_random_kstack_offset - Increase stack u 51 * chosen random of 52 * 53 * This should be used in the syscall entry pa 54 * preempt are disabled, and after user regist 55 * the stack. For testing the resulting entrop 56 * tools/testing/selftests/lkdtm/stack-entropy 57 */ 58 #define add_random_kstack_offset() do { 59 if (static_branch_maybe(CONFIG_RANDOMI 60 &randomize_kst 61 u32 offset = raw_cpu_read(ksta 62 u8 *ptr = __kstack_alloca(KSTA 63 /* Keep allocation even after 64 asm volatile("" :: "r"(ptr) : 65 } 66 } while (0) 67 68 /** 69 * choose_random_kstack_offset - Choose the ra 70 * add_random_ks 71 * 72 * This should only be used during syscall exi 73 * preempt are disabled. This position in the 74 * frustrate attacks from userspace attempting 75 * - Maximize the timing uncertainty visible f 76 * offset is chosen at syscall entry, usersp 77 * over the timing between choosing offsets. 78 * kernel mode?" tends to be more difficult 79 * will we be in user mode?" 80 * - Reduce the lifetime of the new offset sit 81 * kernel mode execution. Exposure of "threa 82 * (e.g. current, percpu, etc) tends to be e 83 * location memory exposure. 84 */ 85 #define choose_random_kstack_offset(rand) do { 86 if (static_branch_maybe(CONFIG_RANDOMI 87 &randomize_kst 88 u32 offset = raw_cpu_read(ksta 89 offset = ror32(offset, 5) ^ (r 90 raw_cpu_write(kstack_offset, o 91 } 92 } while (0) 93 #else /* CONFIG_RANDOMIZE_KSTACK_OFFSET */ 94 #define add_random_kstack_offset() 95 #define choose_random_kstack_offset(rand) 96 #endif /* CONFIG_RANDOMIZE_KSTACK_OFFSET */ 97 98 #endif 99
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.