linux-stable/arch/riscv/kernel
Stefan O'Rear 00effef72c 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:36:04 +02:00
..
compat_vdso riscv: Handle zicsr/zifencei issue between gcc and binutils 2023-08-16 07:39:38 -07:00
pi riscv: Fix an off-by-one in get_early_cmdline() 2024-01-31 16:18:48 -08:00
probes riscv: kprobes: allow writing to x0 2023-11-28 17:20:13 +00:00
vdso RISC-V: hwprobe: Fix vDSO SIGSEGV 2023-11-28 17:19:49 +00:00
.gitignore
Makefile riscv: add CALLER_ADDRx support 2024-03-06 14:48:41 +00:00
acpi.c RISC-V: ACPI: Fix acpi_os_ioremap to return iomem address 2023-08-02 13:49:43 -07:00
alternative.c Merge patch series "Add non-coherent DMA support for AX45MP" 2023-09-08 11:24:34 -07:00
asm-offsets.c riscv: VMAP_STACK overflow detection thread-safe 2023-11-28 17:19:47 +00:00
cacheinfo.c RISC-V Patches for the 6.4 Merge Window, Part 1 2023-04-28 16:55:39 -07:00
cfi.c riscv: Add CFI error handling 2023-08-23 14:16:39 -07:00
compat_signal.c riscv: compat: signal: Add rt_frame implementation 2022-05-17 16:37:21 -07:00
compat_syscall_table.c riscv: Implement syscall wrappers 2023-08-23 14:16:36 -07:00
copy-unaligned.S RISC-V: Probe for unaligned access speed 2023-09-01 09:06:25 -07:00
copy-unaligned.h RISC-V: Probe for unaligned access speed 2023-09-01 09:06:25 -07:00
cpu-hotplug.c riscv: Switch to hotplug core state synchronization 2023-05-15 13:44:59 +02:00
cpu.c RISC-V: Don't fail in riscv_of_parent_hartid() for disabled HARTs 2023-11-20 11:59:37 +01:00
cpu_ops.c RISC-V: Align SBI probe implementation with spec 2023-04-29 13:04:50 -07: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: Ignore V from the riscv,isa DT property on older T-Head CPUs 2024-03-06 14:48:38 +00:00
crash_core.c kexec: drop dependency on ARCH_SUPPORTS_KEXEC from CRASH_DUMP 2023-12-20 17:02:03 +01: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 riscv: Prepare EFI header for relocatable kernels 2023-04-19 07:46:28 -07:00
efi.c efi: Discover BTI support in runtime services regions 2023-02-04 09:19:02 +01:00
elf_kexec.c riscv: kexec: Align the kexeced kernel entry 2023-09-12 13:58:39 -07:00
entry.S riscv: put interrupt entries into .irqentry.text 2023-11-28 17:20:13 +00:00
fpu.S
ftrace.c RISC-V: Don't check text_mutex during stop_machine 2023-03-09 14:58:51 -08:00
head.S riscv: Make XIP bootable again 2024-02-05 20:14:31 +00:00
head.h riscv: entry: Convert to generic entry 2023-03-23 08:47:00 -07:00
hibernate-asm.S riscv: hibernation: Remove duplicate call of suspend_restore_csrs 2023-06-19 09:27:57 -07:00
hibernate.c riscv: hibernate: remove WARN_ON in save_processor_state 2023-06-23 10:06:22 -07:00
image-vars.h riscv: libstub: Implement KASLR by using generic functions 2023-09-05 19:49:31 -07:00
irq.c RISC-V: Fix wrong use of CONFIG_HAVE_SOFTIRQ_ON_OWN_STACK 2023-10-12 11:34:16 -07:00
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 RISC-V: rename parse_asm.h to insn.h 2022-12-29 06:59:47 -08:00
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
mcount-dyn.S riscv: entry: Consolidate general regs saving/restoring 2023-03-23 08:47:03 -07:00
mcount.S riscv: Add ftrace_stub_graph 2023-08-23 14:16:38 -07:00
module-sections.c
module.c riscv: Fix module_alloc() that did not reset the linear mapping permissions 2024-01-25 15:35:50 -08:00
patch.c riscv: Disable preemption when using patch_map() 2024-04-10 16:36:01 +02: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:36:04 +02:00
ptrace.c RISC-V: Add ptrace support for vectors 2023-09-01 13:05:38 -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:48:41 +00:00
riscv_ksyms.c RISC-V: add infrastructure to allow different str* implementations 2023-01-31 11:43:23 -08:00
sbi-ipi.c RISC-V: Allow marking IPIs as suitable for remote FENCEs 2023-04-08 11:26:24 +01:00
sbi.c riscv: Make __flush_tlb_range() loop over pte instead of flushing the whole tlb 2024-02-16 19:10:52 +01:00
setup.c riscv: kdump: fix crashkernel reserving problem on RISC-V 2023-10-12 11:34:09 -07:00
signal.c riscv: signal: fix sigaltstack frame size checking 2023-10-12 05:38:40 -07:00
smp.c riscv: Fix CPU feature detection with SMP disabled 2023-08-08 15:28:25 -07:00
smpboot.c RISC-V: alternative: Remove feature_probe_func 2023-09-01 09:06:26 -07:00
soc.c
stacktrace.c riscv: Use READ_ONCE_NOCHECK in imprecise unwinding stack mode 2023-03-09 14:50:35 -08:00
suspend.c RISC-V: Change suspend_save_csrs and suspend_restore_csrs to public function 2023-04-29 11:25:10 -07:00
suspend_entry.S riscv: Add types to indirectly called assembly functions 2023-08-23 14:16:37 -07:00
sys_riscv.c riscv: Implement syscall wrappers 2023-08-23 14:16:36 -07:00
syscall_table.c riscv: Implement syscall wrappers 2023-08-23 14:16:36 -07:00
time.c RISC-V: time.c: Add ACPI support for time_init() 2023-06-01 08:45:13 -07:00
traps.c riscv: VMAP_STACK overflow detection thread-safe 2023-11-28 17:19:47 +00:00
traps_misaligned.c riscv: fix misaligned access handling of C.SWSP and C.SDSP 2023-12-13 18:45:19 +01:00
vdso.c riscv: vdso: include vdso/vsyscall.h for vdso_data 2023-07-04 07:54:41 -07:00
vector.c riscv: vector: clear V-reg in the first-use trap 2023-07-01 07:38:21 -07:00
vmlinux-xip.lds.S riscv: Check if the code to patch lies in the exit section 2024-01-25 15:35:50 -08:00
vmlinux.lds.S riscv: Check if the code to patch lies in the exit section 2024-01-25 15:35:50 -08:00