WSL2-Linux-Kernel/arch
Marco Elver 8550c9b846 stack: Constrain and fix stack offset randomization with Clang builds
[ Upstream commit efa90c11f6 ]

All supported versions of Clang perform auto-init of __builtin_alloca()
when stack auto-init is on (CONFIG_INIT_STACK_ALL_{ZERO,PATTERN}).

add_random_kstack_offset() uses __builtin_alloca() to add a stack
offset. This means, when CONFIG_INIT_STACK_ALL_{ZERO,PATTERN} is
enabled, add_random_kstack_offset() will auto-init that unused portion
of the stack used to add an offset.

There are several problems with this:

	1. These offsets can be as large as 1023 bytes. Performing
	   memset() on them isn't exactly cheap, and this is done on
	   every syscall entry.

	2. Architectures adding add_random_kstack_offset() to syscall
	   entry implemented in C require them to be 'noinstr' (e.g. see
	   x86 and s390). The potential problem here is that a call to
	   memset may occur, which is not noinstr.

A x86_64 defconfig kernel with Clang 11 and CONFIG_VMLINUX_VALIDATION shows:

 | vmlinux.o: warning: objtool: do_syscall_64()+0x9d: call to memset() leaves .noinstr.text section
 | vmlinux.o: warning: objtool: do_int80_syscall_32()+0xab: call to memset() leaves .noinstr.text section
 | vmlinux.o: warning: objtool: __do_fast_syscall_32()+0xe2: call to memset() leaves .noinstr.text section
 | vmlinux.o: warning: objtool: fixup_bad_iret()+0x2f: call to memset() leaves .noinstr.text section

Clang 14 (unreleased) will introduce a way to skip alloca initialization
via __builtin_alloca_uninitialized() (https://reviews.llvm.org/D115440).

Constrain RANDOMIZE_KSTACK_OFFSET to only be enabled if no stack
auto-init is enabled, the compiler is GCC, or Clang is version 14+. Use
__builtin_alloca_uninitialized() if the compiler provides it, as is done
by Clang 14.

Link: https://lkml.kernel.org/r/YbHTKUjEejZCLyhX@elver.google.com
Fixes: 39218ff4c6 ("stack: Optionally randomize kernel stack offset each syscall")
Signed-off-by: Marco Elver <elver@google.com>
Reviewed-by: Nathan Chancellor <nathan@kernel.org>
Acked-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Kees Cook <keescook@chromium.org>
Link: https://lore.kernel.org/r/20220131090521.1947110-2-elver@google.com
Signed-off-by: Sasha Levin <sashal@kernel.org>
2022-04-08 14:23:06 +02:00
..
alpha alpha: enable GENERIC_PCI_IOMAP unconditionally 2021-09-19 10:37:00 -07:00
arc signal: Replace force_sigsegv(SIGSEGV) with force_fatal_sig(SIGSEGV) 2021-11-25 09:49:06 +01:00
arm ARM: dts: exynos: add missing HDMI supplies on SMDK5420 2022-04-08 14:23:00 +02:00
arm64 arm64: dts: ti: k3-am64: Fix gic-v3 compatible regs 2022-04-08 14:22:58 +02:00
csky uaccess: fix integer overflow on access_ok() 2022-03-28 09:58:45 +02:00
h8300 Merge branch 'akpm' (patches from Andrew) 2021-09-08 12:55:35 -07:00
hexagon uaccess: fix integer overflow on access_ok() 2022-03-28 09:58:45 +02:00
ia64 PCI/sysfs: Find shadow ROM before static attribute initialization 2022-02-01 17:27:05 +01:00
m68k m68k: fix access_ok for coldfire 2022-03-28 09:58:46 +02:00
microblaze uaccess: fix integer overflow on access_ok() 2022-03-28 09:58:45 +02:00
mips DEC: Limit PMAX memory probing to R3k systems 2022-04-08 14:23:02 +02:00
nds32 nds32: fix access_ok() checks in get/put_user 2022-03-28 09:58:46 +02:00
nios2 nios2: Make NIOS2_DTB_SOURCE_BOOL depend on !COMPILE_TEST 2021-10-27 09:29:07 -05:00
openrisc openrisc: Add clone3 ABI wrapper 2022-01-27 11:04:10 +01:00
parisc parisc/unaligned: Fix ldw() and stw() unalignment handlers 2022-03-02 11:47:49 +01:00
powerpc powerpc/kvm: Fix kvm_use_magic_page 2022-04-08 14:22:57 +02:00
riscv riscv: Increase stack size under KASAN 2022-04-08 14:22:55 +02:00
s390 s390/extable: fix exception table sorting 2022-03-08 19:12:48 +01:00
sh Documentation, arch: Remove leftovers from CIFS_WEAK_PW_HASH 2022-01-27 11:05:21 +01:00
sparc signal: Replace force_fatal_sig with force_exit_sig when in doubt 2021-11-25 09:49:07 +01:00
um block: drop unused includes in <linux/genhd.h> 2022-03-16 14:23:46 +01:00
x86 ACPI / x86: Work around broken XSDT on Advantech DAC-BJ01 board 2022-03-28 09:58:44 +02:00
xtensa xtensa: fix xtensa_wsr always writing 0 2022-04-08 14:23:03 +02:00
.gitignore
Kconfig stack: Constrain and fix stack offset randomization with Clang builds 2022-04-08 14:23:06 +02:00