]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
riscv: entry: always initialize regs->a0 to -ENOSYS
authorCeleste Liu <coelacanthushex@gmail.com>
Thu, 27 Jun 2024 14:23:39 +0000 (22:23 +0800)
committerPalmer Dabbelt <palmer@rivosinc.com>
Wed, 14 Aug 2024 20:12:22 +0000 (13:12 -0700)
Otherwise when the tracer changes syscall number to -1, the kernel fails
to initialize a0 with -ENOSYS and subsequently fails to return the error
code of the failed syscall to userspace. For example, it will break
strace syscall tampering.

Fixes: 52449c17bdd1 ("riscv: entry: set a0 = -ENOSYS only when syscall != -1")
Reported-by: "Dmitry V. Levin" <ldv@strace.io>
Reviewed-by: Björn Töpel <bjorn@rivosinc.com>
Cc: stable@vger.kernel.org
Signed-off-by: Celeste Liu <CoelacanthusHex@gmail.com>
Link: https://lore.kernel.org/r/20240627142338.5114-2-CoelacanthusHex@gmail.com
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
arch/riscv/kernel/traps.c

index 05a16b1f0aee858f3abf7a28c647ad1146410da0..51ebfd23e0076447518081d137102a9a11ff2e45 100644 (file)
@@ -319,6 +319,7 @@ void do_trap_ecall_u(struct pt_regs *regs)
 
                regs->epc += 4;
                regs->orig_a0 = regs->a0;
+               regs->a0 = -ENOSYS;
 
                riscv_v_vstate_discard(regs);
 
@@ -328,8 +329,7 @@ void do_trap_ecall_u(struct pt_regs *regs)
 
                if (syscall >= 0 && syscall < NR_syscalls)
                        syscall_handler(regs, syscall);
-               else if (syscall != -1)
-                       regs->a0 = -ENOSYS;
+
                /*
                 * Ultimately, this value will get limited by KSTACK_OFFSET_MAX(),
                 * so the maximum stack offset is 1k bytes (10 bits).