GNU Linux-libre 5.15.54-gnu
[releases.git] / include / linux / randomize_kstack.h
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 #ifndef _LINUX_RANDOMIZE_KSTACK_H
3 #define _LINUX_RANDOMIZE_KSTACK_H
4
5 #include <linux/kernel.h>
6 #include <linux/jump_label.h>
7 #include <linux/percpu-defs.h>
8
9 DECLARE_STATIC_KEY_MAYBE(CONFIG_RANDOMIZE_KSTACK_OFFSET_DEFAULT,
10                          randomize_kstack_offset);
11 DECLARE_PER_CPU(u32, kstack_offset);
12
13 /*
14  * Do not use this anywhere else in the kernel. This is used here because
15  * it provides an arch-agnostic way to grow the stack with correct
16  * alignment. Also, since this use is being explicitly masked to a max of
17  * 10 bits, stack-clash style attacks are unlikely. For more details see
18  * "VLAs" in Documentation/process/deprecated.rst
19  *
20  * The normal __builtin_alloca() is initialized with INIT_STACK_ALL (currently
21  * only with Clang and not GCC). Initializing the unused area on each syscall
22  * entry is expensive, and generating an implicit call to memset() may also be
23  * problematic (such as in noinstr functions). Therefore, if the compiler
24  * supports it (which it should if it initializes allocas), always use the
25  * "uninitialized" variant of the builtin.
26  */
27 #if __has_builtin(__builtin_alloca_uninitialized)
28 #define __kstack_alloca __builtin_alloca_uninitialized
29 #else
30 #define __kstack_alloca __builtin_alloca
31 #endif
32
33 /*
34  * Use, at most, 10 bits of entropy. We explicitly cap this to keep the
35  * "VLA" from being unbounded (see above). 10 bits leaves enough room for
36  * per-arch offset masks to reduce entropy (by removing higher bits, since
37  * high entropy may overly constrain usable stack space), and for
38  * compiler/arch-specific stack alignment to remove the lower bits.
39  */
40 #define KSTACK_OFFSET_MAX(x)    ((x) & 0x3FF)
41
42 /*
43  * These macros must be used during syscall entry when interrupts and
44  * preempt are disabled, and after user registers have been stored to
45  * the stack.
46  */
47 #define add_random_kstack_offset() do {                                 \
48         if (static_branch_maybe(CONFIG_RANDOMIZE_KSTACK_OFFSET_DEFAULT, \
49                                 &randomize_kstack_offset)) {            \
50                 u32 offset = raw_cpu_read(kstack_offset);               \
51                 u8 *ptr = __kstack_alloca(KSTACK_OFFSET_MAX(offset));   \
52                 /* Keep allocation even after "ptr" loses scope. */     \
53                 asm volatile("" :: "r"(ptr) : "memory");                \
54         }                                                               \
55 } while (0)
56
57 #define choose_random_kstack_offset(rand) do {                          \
58         if (static_branch_maybe(CONFIG_RANDOMIZE_KSTACK_OFFSET_DEFAULT, \
59                                 &randomize_kstack_offset)) {            \
60                 u32 offset = raw_cpu_read(kstack_offset);               \
61                 offset ^= (rand);                                       \
62                 raw_cpu_write(kstack_offset, offset);                   \
63         }                                                               \
64 } while (0)
65
66 #endif