xref: /linux/include/linux/randomize_kstack.h (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
139218ff4SKees Cook /* SPDX-License-Identifier: GPL-2.0-only */
239218ff4SKees Cook #ifndef _LINUX_RANDOMIZE_KSTACK_H
339218ff4SKees Cook #define _LINUX_RANDOMIZE_KSTACK_H
439218ff4SKees Cook 
58cb37a59SMarco Elver #ifdef CONFIG_RANDOMIZE_KSTACK_OFFSET
639218ff4SKees Cook #include <linux/kernel.h>
739218ff4SKees Cook #include <linux/jump_label.h>
839218ff4SKees Cook #include <linux/percpu-defs.h>
939218ff4SKees Cook 
1039218ff4SKees Cook DECLARE_STATIC_KEY_MAYBE(CONFIG_RANDOMIZE_KSTACK_OFFSET_DEFAULT,
1139218ff4SKees Cook 			 randomize_kstack_offset);
1239218ff4SKees Cook DECLARE_PER_CPU(u32, kstack_offset);
1339218ff4SKees Cook 
1439218ff4SKees Cook /*
1539218ff4SKees Cook  * Do not use this anywhere else in the kernel. This is used here because
1639218ff4SKees Cook  * it provides an arch-agnostic way to grow the stack with correct
1739218ff4SKees Cook  * alignment. Also, since this use is being explicitly masked to a max of
1839218ff4SKees Cook  * 10 bits, stack-clash style attacks are unlikely. For more details see
1939218ff4SKees Cook  * "VLAs" in Documentation/process/deprecated.rst
20efa90c11SMarco Elver  *
21efa90c11SMarco Elver  * The normal __builtin_alloca() is initialized with INIT_STACK_ALL (currently
22efa90c11SMarco Elver  * only with Clang and not GCC). Initializing the unused area on each syscall
23efa90c11SMarco Elver  * entry is expensive, and generating an implicit call to memset() may also be
24efa90c11SMarco Elver  * problematic (such as in noinstr functions). Therefore, if the compiler
25efa90c11SMarco Elver  * supports it (which it should if it initializes allocas), always use the
26efa90c11SMarco Elver  * "uninitialized" variant of the builtin.
2739218ff4SKees Cook  */
28efa90c11SMarco Elver #if __has_builtin(__builtin_alloca_uninitialized)
29efa90c11SMarco Elver #define __kstack_alloca __builtin_alloca_uninitialized
30efa90c11SMarco Elver #else
31efa90c11SMarco Elver #define __kstack_alloca __builtin_alloca
32efa90c11SMarco Elver #endif
33efa90c11SMarco Elver 
3439218ff4SKees Cook /*
35*872bb37fSKees Cook  * Use, at most, 6 bits of entropy (on 64-bit; 8 on 32-bit). This cap is
36*872bb37fSKees Cook  * to keep the "VLA" from being unbounded (see above). Additionally clear
37*872bb37fSKees Cook  * the bottom 4 bits (on 64-bit systems, 2 for 32-bit), since stack
38*872bb37fSKees Cook  * alignment will always be at least word size. This makes the compiler
39*872bb37fSKees Cook  * code gen better when it is applying the actual per-arch alignment to
40*872bb37fSKees Cook  * the final offset. The resulting randomness is reasonable without overly
41*872bb37fSKees Cook  * constraining usable stack space.
4239218ff4SKees Cook  */
43*872bb37fSKees Cook #ifdef CONFIG_64BIT
44*872bb37fSKees Cook #define KSTACK_OFFSET_MAX(x)	((x) & 0b1111110000)
45*872bb37fSKees Cook #else
46*872bb37fSKees Cook #define KSTACK_OFFSET_MAX(x)	((x) & 0b1111111100)
47*872bb37fSKees Cook #endif
4839218ff4SKees Cook 
491ff29758SKees Cook /**
501ff29758SKees Cook  * add_random_kstack_offset - Increase stack utilization by previously
511ff29758SKees Cook  *			      chosen random offset
521ff29758SKees Cook  *
531ff29758SKees Cook  * This should be used in the syscall entry path when interrupts and
5439218ff4SKees Cook  * preempt are disabled, and after user registers have been stored to
551ff29758SKees Cook  * the stack. For testing the resulting entropy, please see:
561ff29758SKees Cook  * tools/testing/selftests/lkdtm/stack-entropy.sh
5739218ff4SKees Cook  */
5839218ff4SKees Cook #define add_random_kstack_offset() do {					\
5939218ff4SKees Cook 	if (static_branch_maybe(CONFIG_RANDOMIZE_KSTACK_OFFSET_DEFAULT,	\
6039218ff4SKees Cook 				&randomize_kstack_offset)) {		\
6139218ff4SKees Cook 		u32 offset = raw_cpu_read(kstack_offset);		\
62efa90c11SMarco Elver 		u8 *ptr = __kstack_alloca(KSTACK_OFFSET_MAX(offset));	\
6339218ff4SKees Cook 		/* Keep allocation even after "ptr" loses scope. */	\
642515dd6cSNick Desaulniers 		asm volatile("" :: "r"(ptr) : "memory");		\
6539218ff4SKees Cook 	}								\
6639218ff4SKees Cook } while (0)
6739218ff4SKees Cook 
681ff29758SKees Cook /**
691ff29758SKees Cook  * choose_random_kstack_offset - Choose the random offset for the next
701ff29758SKees Cook  *				 add_random_kstack_offset()
711ff29758SKees Cook  *
721ff29758SKees Cook  * This should only be used during syscall exit when interrupts and
731ff29758SKees Cook  * preempt are disabled. This position in the syscall flow is done to
741ff29758SKees Cook  * frustrate attacks from userspace attempting to learn the next offset:
751ff29758SKees Cook  * - Maximize the timing uncertainty visible from userspace: if the
761ff29758SKees Cook  *   offset is chosen at syscall entry, userspace has much more control
771ff29758SKees Cook  *   over the timing between choosing offsets. "How long will we be in
781ff29758SKees Cook  *   kernel mode?" tends to be more difficult to predict than "how long
791ff29758SKees Cook  *   will we be in user mode?"
801ff29758SKees Cook  * - Reduce the lifetime of the new offset sitting in memory during
811ff29758SKees Cook  *   kernel mode execution. Exposure of "thread-local" memory content
821ff29758SKees Cook  *   (e.g. current, percpu, etc) tends to be easier than arbitrary
831ff29758SKees Cook  *   location memory exposure.
841ff29758SKees Cook  */
8539218ff4SKees Cook #define choose_random_kstack_offset(rand) do {				\
8639218ff4SKees Cook 	if (static_branch_maybe(CONFIG_RANDOMIZE_KSTACK_OFFSET_DEFAULT,	\
8739218ff4SKees Cook 				&randomize_kstack_offset)) {		\
8839218ff4SKees Cook 		u32 offset = raw_cpu_read(kstack_offset);		\
899c573cd3SKees Cook 		offset = ror32(offset, 5) ^ (rand);			\
9039218ff4SKees Cook 		raw_cpu_write(kstack_offset, offset);			\
9139218ff4SKees Cook 	}								\
9239218ff4SKees Cook } while (0)
938cb37a59SMarco Elver #else /* CONFIG_RANDOMIZE_KSTACK_OFFSET */
948cb37a59SMarco Elver #define add_random_kstack_offset()		do { } while (0)
958cb37a59SMarco Elver #define choose_random_kstack_offset(rand)	do { } while (0)
968cb37a59SMarco Elver #endif /* CONFIG_RANDOMIZE_KSTACK_OFFSET */
9739218ff4SKees Cook 
9839218ff4SKees Cook #endif
99