diff mbox series

riscv: entry: always initialize regs->a0 to -ENOSYS

Message ID 20240627103205.27914-2-CoelacanthusHex@gmail.com (mailing list archive)
State Superseded
Headers show
Series riscv: entry: always initialize regs->a0 to -ENOSYS | expand

Checks

Context Check Description
conchuod/vmtest-for-next-PR success PR summary
conchuod/patch-1-test-1 success .github/scripts/patches/tests/build_rv32_defconfig.sh
conchuod/patch-1-test-2 success .github/scripts/patches/tests/build_rv64_clang_allmodconfig.sh
conchuod/patch-1-test-3 success .github/scripts/patches/tests/build_rv64_gcc_allmodconfig.sh
conchuod/patch-1-test-4 success .github/scripts/patches/tests/build_rv64_nommu_k210_defconfig.sh
conchuod/patch-1-test-5 success .github/scripts/patches/tests/build_rv64_nommu_virt_defconfig.sh
conchuod/patch-1-test-6 warning .github/scripts/patches/tests/checkpatch.sh
conchuod/patch-1-test-7 success .github/scripts/patches/tests/dtb_warn_rv64.sh
conchuod/patch-1-test-8 success .github/scripts/patches/tests/header_inline.sh
conchuod/patch-1-test-9 success .github/scripts/patches/tests/kdoc.sh
conchuod/patch-1-test-10 success .github/scripts/patches/tests/module_param.sh
conchuod/patch-1-test-11 success .github/scripts/patches/tests/verify_fixes.sh
conchuod/patch-1-test-12 success .github/scripts/patches/tests/verify_signedoff.sh

Commit Message

Celeste Liu June 27, 2024, 10:32 a.m. UTC
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")
Cc: stable@vger.kernel.org
Signed-off-by: Celeste Liu <CoelacanthusHex@gmail.com>
---
 arch/riscv/kernel/traps.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Björn Töpel June 27, 2024, 2:08 p.m. UTC | #1
Celeste Liu <coelacanthushex@gmail.com> writes:

> 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")
> Cc: stable@vger.kernel.org
> Signed-off-by: Celeste Liu <CoelacanthusHex@gmail.com>

Reported-by: "Dmitry V. Levin" <ldv@strace.io>
Reviewed-by: Björn Töpel <bjorn@rivosinc.com>
Celeste Liu June 27, 2024, 2:29 p.m. UTC | #2
On 2024-06-27 22:08, Björn Töpel wrote:

> Celeste Liu <coelacanthushex@gmail.com> writes:
> 
>> 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")
>> Cc: stable@vger.kernel.org
>> Signed-off-by: Celeste Liu <CoelacanthusHex@gmail.com>
> 
> Reported-by: "Dmitry V. Levin" <ldv@strace.io>
> Reviewed-by: Björn Töpel <bjorn@rivosinc.com>

Patch v2 has been sent.
Björn Töpel June 27, 2024, 3:48 p.m. UTC | #3
On Thu, 27 Jun 2024 at 16:29, Celeste Liu <coelacanthushex@gmail.com> wrote:
>
> On 2024-06-27 22:08, Björn Töpel wrote:
>
> > Celeste Liu <coelacanthushex@gmail.com> writes:
> >
> >> 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")
> >> Cc: stable@vger.kernel.org
> >> Signed-off-by: Celeste Liu <CoelacanthusHex@gmail.com>
> >
> > Reported-by: "Dmitry V. Levin" <ldv@strace.io>
> > Reviewed-by: Björn Töpel <bjorn@rivosinc.com>
>
> Patch v2 has been sent.

For future patches; You don't need to respin a patch to add tags. The
tooling will pick up the tags automatically.
diff mbox series

Patch

diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c
index 05a16b1f0aee..51ebfd23e007 100644
--- a/arch/riscv/kernel/traps.c
+++ b/arch/riscv/kernel/traps.c
@@ -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).