stable.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] riscv/entry: get correct syscall number from syscall_get_nr()
@ 2024-10-16 17:49 Celeste Liu via B4 Relay
  2024-10-16 17:51 ` kernel test robot
  2024-10-21 14:00 ` Björn Töpel
  0 siblings, 2 replies; 18+ messages in thread
From: Celeste Liu via B4 Relay @ 2024-10-16 17:49 UTC (permalink / raw)
  To: Paul Walmsley, Palmer Dabbelt, Albert Ou, Björn Töpel,
	Celeste Liu
  Cc: Palmer Dabbelt, Alexandre Ghiti, Dmitry V. Levin,
	Andrea Bolognani, Felix Yan, Ruizhe Pan, Shiqi Zhang, Guo Ren,
	Yao Zi, Han Gao, linux-riscv, linux-kernel, stable, Celeste Liu

From: Celeste Liu <CoelacanthusHex@gmail.com>

The return value of syscall_enter_from_user_mode() is always -1 when the
syscall was filtered. We can't know whether syscall_nr is -1 when we get -1
from syscall_enter_from_user_mode(). And the old syscall variable is
unusable because syscall_enter_from_user_mode() may change a7 register.
So get correct syscall number from syscall_get_nr().

So syscall number part of return value of syscall_enter_from_user_mode()
is completely useless. We can remove it from API and require caller to
get syscall number from syscall_get_nr(). But this change affect more
architectures and will block more time. So we split it into another
patchset to avoid block this fix. (Other architectures can works
without this change but riscv need it, see Link: tag below)

Fixes: 61119394631f ("riscv: entry: always initialize regs->a0 to -ENOSYS")
Reported-by: Andrea Bolognani <abologna@redhat.com>
Closes: https://github.com/strace/strace/issues/315
Link: https://lore.kernel.org/all/59505464-c84a-403d-972f-d4b2055eeaac@gmail.com/
Signed-off-by: Celeste Liu <CoelacanthusHex@gmail.com>
---
 arch/riscv/kernel/traps.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c
index 51ebfd23e0076447518081d137102a9a11ff2e45..3125fab8ee4af468ace9f692dd34e1797555cce3 100644
--- a/arch/riscv/kernel/traps.c
+++ b/arch/riscv/kernel/traps.c
@@ -316,18 +316,25 @@ void do_trap_ecall_u(struct pt_regs *regs)
 {
 	if (user_mode(regs)) {
 		long syscall = regs->a7;
+		long res;
 
 		regs->epc += 4;
 		regs->orig_a0 = regs->a0;
-		regs->a0 = -ENOSYS;
 
 		riscv_v_vstate_discard(regs);
 
-		syscall = syscall_enter_from_user_mode(regs, syscall);
+		res = syscall_enter_from_user_mode(regs, syscall);
+		/*
+		 * Call syscall_get_nr() again because syscall_enter_from_user_mode()
+		 * may change a7 register.
+		 */
+		syscall = syscall_get_nr(current, regs);
 
 		add_random_kstack_offset();
 
-		if (syscall >= 0 && syscall < NR_syscalls)
+		if (syscall < 0 || syscall >= NR_syscalls)
+			regs->a0 = -ENOSYS;
+		else if (res != -1)
 			syscall_handler(regs, syscall);
 
 		/*

---
base-commit: 2f87d0916ce0d2925cedbc9e8f5d6291ba2ac7b2
change-id: 20241016-fix-riscv-syscall-nr-917b566f97f3

Best regards,
-- 
Celeste Liu <CoelacanthusHex@gmail.com>



^ permalink raw reply related	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2024-11-30 22:40 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-16 17:49 [PATCH] riscv/entry: get correct syscall number from syscall_get_nr() Celeste Liu via B4 Relay
2024-10-16 17:51 ` kernel test robot
2024-10-21 14:00 ` Björn Töpel
2024-10-21 15:23   ` Celeste Liu
2024-10-21 16:46     ` Björn Töpel
2024-10-25 13:12       ` Thomas Gleixner
2024-10-25 14:30         ` Björn Töpel
2024-10-26 20:21           ` Thomas Gleixner
2024-10-27 15:29             ` Celeste Liu
2024-10-27 15:56               ` Thomas Gleixner
2024-10-27 17:01                 ` Celeste Liu
2024-10-27 21:52                   ` Thomas Gleixner
2024-10-28  0:17                     ` Ron Economos
2024-10-28 16:25                       ` Celeste Liu
2024-10-28 19:33                         ` Björn Töpel
2024-11-30 22:39                           ` Celeste Liu
2024-10-28  9:45             ` Björn Töpel
2024-11-15 21:49               ` Aurelien Jarno

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).