linux-stable/arch/riscv/kernel
Stefan O'Rear f6583444d7 riscv: process: Fix kernel gp leakage
commit d14fa1fcf6 upstream.

childregs represents the registers which are active for the new thread
in user context. For a kernel thread, childregs->gp is never used since
the kernel gp is not touched by switch_to. For a user mode helper, the
gp value can be observed in user space after execve or possibly by other
means.

[From the email thread]

The /* Kernel thread */ comment is somewhat inaccurate in that it is also used
for user_mode_helper threads, which exec a user process, e.g. /sbin/init or
when /proc/sys/kernel/core_pattern is a pipe. Such threads do not have
PF_KTHREAD set and are valid targets for ptrace etc. even before they exec.

childregs is the *user* context during syscall execution and it is observable
from userspace in at least five ways:

1. kernel_execve does not currently clear integer registers, so the starting
   register state for PID 1 and other user processes started by the kernel has
   sp = user stack, gp = kernel __global_pointer$, all other integer registers
   zeroed by the memset in the patch comment.

   This is a bug in its own right, but I'm unwilling to bet that it is the only
   way to exploit the issue addressed by this patch.

2. ptrace(PTRACE_GETREGSET): you can PTRACE_ATTACH to a user_mode_helper thread
   before it execs, but ptrace requires SIGSTOP to be delivered which can only
   happen at user/kernel boundaries.

3. /proc/*/task/*/syscall: this is perfectly happy to read pt_regs for
   user_mode_helpers before the exec completes, but gp is not one of the
   registers it returns.

4. PERF_SAMPLE_REGS_USER: LOCKDOWN_PERF normally prevents access to kernel
   addresses via PERF_SAMPLE_REGS_INTR, but due to this bug kernel addresses
   are also exposed via PERF_SAMPLE_REGS_USER which is permitted under
   LOCKDOWN_PERF. I have not attempted to write exploit code.

5. Much of the tracing infrastructure allows access to user registers. I have
   not attempted to determine which forms of tracing allow access to user
   registers without already allowing access to kernel registers.

Fixes: 7db91e57a0 ("RISC-V: Task implementation")
Cc: stable@vger.kernel.org
Signed-off-by: Stefan O'Rear <sorear@fastmail.com>
Reviewed-by: Alexandre Ghiti <alexghiti@rivosinc.com>
Link: https://lore.kernel.org/r/20240327061258.2370291-1-sorear@fastmail.com
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2024-04-10 16:28:33 +02:00
..
compat_vdso riscv: Handle zicsr/zifencei issue between gcc and binutils 2023-08-30 16:11:08 +02:00
probes riscv: kprobes: allow writing to x0 2023-11-28 17:07:20 +00:00
vdso riscv: vdso: fix section overlapping under some conditions 2022-11-29 18:45:53 -08:00
.gitignore
alternative.c riscv: make patch-function pointer more generic in cpu_manufacturer_info struct 2022-06-16 15:47:40 -07:00
asm-offsets.c RISC-V: Add arch functions for non-retentive suspend entry/exit 2022-03-10 09:29:31 -08:00
cacheinfo.c drivers: base: cacheinfo: Get rid of DEFINE_SMP_CALL_CACHE_FUNCTION() 2021-09-01 10:29:10 +02:00
compat_signal.c riscv: compat: signal: Add rt_frame implementation 2022-05-17 16:37:21 -07:00
compat_syscall_table.c riscv: compat: syscall: Add compat_sys_call_table implementation 2022-04-26 13:36:25 -07:00
cpu-hotplug.c riscv: cpu-hotplug: clear cpu from numa map when teardown 2022-02-10 09:16:50 -08:00
cpu.c RISC-V: Don't fail in riscv_of_parent_hartid() for disabled HARTs 2023-11-20 11:52:17 +01:00
cpu_ops.c RISC-V: Align SBI probe implementation with spec 2023-05-11 23:03:04 +09:00
cpu_ops_sbi.c riscv: cpu_ops_sbi: Add 64bit hartid support on RV64 2022-07-19 16:38:58 -07:00
cpu_ops_spinwait.c RISC-V: cpu_ops_spinwait.c should include head.h 2022-08-11 13:24:16 -07:00
cpufeature.c RISC-V: take text_mutex during alternative patching 2023-05-17 11:53:41 +02:00
crash_dump.c vmcore: convert copy_oldmem_page() to take an iov_iter 2022-04-29 14:37:59 -07:00
crash_save_regs.S RISC-V: Fixup get incorrect user mode PC for kernel mode regs 2022-08-11 08:54:40 -07:00
efi-header.S
efi.c riscv: read-only pages should not be writable 2022-05-28 11:39:31 +02:00
elf_kexec.c riscv: kexec: Align the kexeced kernel entry 2023-09-23 11:11:09 +02:00
entry.S RISC-V: Fix unannoted hardirqs-on in return to userspace slow-path 2022-12-31 13:32:37 +01:00
fpu.S
ftrace.c RISC-V: Don't check text_mutex during stop_machine 2023-03-17 08:50:29 +01:00
head.h RISC-V: Move spinwait booting method to its own config 2022-01-20 09:27:16 -08:00
head.S riscv: fix -Wundef warning for CONFIG_RISCV_BOOT_SPINWAIT 2023-02-01 08:34:39 +01:00
image-vars.h efi/libstub: use EFI provided memcpy/memset routines 2022-09-17 15:13:21 +02:00
irq.c
jump_label.c jump_label: make initial NOP patching the special case 2022-06-24 09:48:55 +02:00
kexec_relocate.S riscv: Don't use va_pa_offset on kdump 2022-01-09 10:38:06 -08:00
kgdb.c
machine_kexec.c riscv: kexec: Fixup crash_smp_send_stop without multi cores 2022-11-29 21:50:59 -08:00
machine_kexec_file.c RISC-V: Add kexec_file support 2022-05-19 12:14:18 -07:00
Makefile riscv: add CALLER_ADDRx support 2024-03-06 14:45:11 +00:00
mcount-dyn.S riscv: ftrace: Reduce the detour code size to half 2023-03-10 09:34:30 +01:00
mcount.S riscv: Workaround mcount name prior to clang-13 2021-04-26 08:25:01 -07:00
module-sections.c
module.c riscv: Fix module_alloc() that did not reset the linear mapping permissions 2024-01-25 15:27:45 -08:00
patch.c riscv: Check if the code to patch lies in the exit section 2024-01-25 15:27:45 -08:00
perf_callchain.c riscv: Fix fill_callchain return value 2022-03-30 23:01:42 -07:00
perf_regs.c
process.c riscv: process: Fix kernel gp leakage 2024-04-10 16:28:33 +02:00
ptrace.c riscv: compat: ptrace: Add compat_arch_ptrace implement 2022-05-17 16:37:22 -07:00
reset.c riscv: Use do_kernel_power_off() 2022-05-19 19:30:30 +02:00
return_address.c riscv: add CALLER_ADDRx support 2024-03-06 14:45:11 +00:00
riscv_ksyms.c riscv: provide memmove implementation 2020-12-10 17:27:54 -08:00
sbi.c RISC-V: Align SBI probe implementation with spec 2023-05-11 23:03:04 +09:00
setup.c riscv: Do not set initial_boot_params to the linear address of the dtb 2023-05-01 08:26:28 +09:00
signal.c riscv: add icache flush for nommu sigreturn trampoline 2023-04-20 12:35:12 +02:00
smp.c RISC-V: drop error print from riscv_hartid_to_cpuid() 2023-11-28 17:07:23 +00:00
smpboot.c riscv: Move call to init_cpu_topology() to later initialization stage 2023-02-01 08:34:48 +01:00
soc.c riscv: Fix builtin DTB handling 2021-01-07 19:00:50 -08:00
stacktrace.c riscv: Use READ_ONCE_NOCHECK in imprecise unwinding stack mode 2023-03-17 08:50:29 +01:00
suspend.c RISC-V: Add arch functions for non-retentive suspend entry/exit 2022-03-10 09:29:31 -08:00
suspend_entry.S RISC-V: Split out the XIP fixups into their own file 2022-05-25 14:43:33 -07:00
sys_riscv.c riscv: Allow PROT_WRITE-only mmap() 2022-09-22 09:44:59 -07:00
syscall_table.c riscv/vdso: Refactor asm/vdso.h 2021-10-02 13:42:23 -07:00
time.c RISC-V: time: initialize hrtimer based broadcast clock event device 2023-03-10 09:33:03 +01:00
trace_irq.c riscv: fix oops caused by irqsoff latency tracer 2022-02-24 20:30:30 -08:00
trace_irq.h riscv: fix oops caused by irqsoff latency tracer 2022-02-24 20:30:30 -08:00
traps.c riscv: Avoid enabling interrupts in die() 2023-03-10 09:34:29 +01:00
traps_misaligned.c riscv: fix misaligned access handling of C.SWSP and C.SDSP 2023-12-13 18:39:16 +01:00
vdso.c RISC-V Patches for the 6.1 Merge Window, Part 2 2022-10-14 11:21:11 -07:00
vmlinux-xip.lds.S riscv: Check if the code to patch lies in the exit section 2024-01-25 15:27:45 -08:00
vmlinux.lds.S riscv: Check if the code to patch lies in the exit section 2024-01-25 15:27:45 -08:00