Skip to contentSkip to conversation

entry: Untangle the return value of syscall_enter_from_user_mode from syscall NR

RFC In review Michal Suchánek 6 participants 27 messages Jul 1 – Jul 6

Subsystems (8)
Documentation x86 PowerPC RISC-V +4 more subsystems
lore
Share & tools
lore
#0 lore raw

RFC 1/1 entry: Untangle the return value of syscall_enter_from_user_mode from syscall NR

The return value of syscall_enter_from_user_mode is used both for the adjusted syscall number and the indicator that a syscall should be skipped.

As seccomp can be invoked on any syscall, including invalid ones this somewhat undermines seccomp.

While the seccomp variants that terminate the process do not need to care about this for the filter that sets the syscall return value this disctinction is required.

Pass the syscall number as a pointer to the inline entry functions, and use the return value exclusively for the indication that the syscall is already handled.

This should avoid the need for the s390 PIF_SYSCALL_RET_SET which is the workaround for exactly this deficiency.

If this is desirable the patch could be split into some series that adjusts the code flow where needed so that the final change is mostly mechanical.

There is also another way to handle this problem.

With x86 using bit 30 to denote compatibility syscall it sounds like declaring syscall number a 30bit quantity would work.

Then bit 31 could be used to denote an invalid syscall that can never be executed, and the -1 returned from syscall_enter_from_user_mode would then be inherently invalid.

That is so long as no architectures use syscall numbers outside of this range so far, and the limitation is considered fine.

---
 Documentation/core-api/entry.rst | 12 ++++++----
 arch/loongarch/kernel/syscall.c  |  6 ++---
 arch/powerpc/kernel/syscall.c    |  3 ++-
 arch/riscv/kernel/traps.c        |  6 ++---
 arch/s390/kernel/syscall.c       |  6 ++---
 arch/x86/entry/syscall_32.c      | 39 +++++++++++++++-----------------
 arch/x86/entry/syscall_64.c      | 19 ++++++++--------
 include/linux/entry-common.h     | 38 ++++++++++++++-----------------
 8 files changed, 63 insertions(+), 66 deletions(-)
Documentation/core-api/entry.rst +7 -5
index 71d8eedc0549..b0bfae31fe7c 100644
@@ -68,12 +68,14 @@ invoked from low-level assembly code looks like this:
   noinstr void syscall(struct pt_regs *regs, int nr)
   {
 	arch_syscall_enter(regs);
-	nr = syscall_enter_from_user_mode(regs, nr);
 
-	instrumentation_begin();
-	if (!invoke_syscall(regs, nr) && nr != -1)
-	 	result_reg(regs) = __sys_ni_syscall(regs);
-	instrumentation_end();
+	/* Skip syscall when -1 is returned */
+	if (!syscall_enter_from_user_mode(regs, &nr)) {
+		instrumentation_begin();
+		if (!invoke_syscall(regs, nr) && nr != -1)
+			result_reg(regs) = __sys_ni_syscall(regs);
+		instrumentation_end();
+	}
 
 	syscall_exit_to_user_mode(regs);
   }
arch/loongarch/kernel/syscall.c +3 -3
index 94c1c3b5b0b5..fc18ac56b91c 100644
@@ -58,7 +58,7 @@ typedef long (*sys_call_fn)(unsigned long, unsigned long,
 
 void noinstr __no_stack_protector do_syscall(struct pt_regs *regs)
 {
-	unsigned long nr;
+	unsigned long nr, ret;
 	sys_call_fn syscall_fn;
 
 	nr = regs->regs[11];
@@ -70,11 +70,11 @@ void noinstr __no_stack_protector do_syscall(struct pt_regs *regs)
 	regs->orig_a0 = regs->regs[4];
 	regs->regs[4] = -ENOSYS;
 
-	nr = syscall_enter_from_user_mode(regs, nr);
+	ret = syscall_enter_from_user_mode(regs, &nr);
 
 	add_random_kstack_offset();
 
-	if (nr < NR_syscalls) {
+	if (nr < NR_syscalls && !ret) {
 		syscall_fn = sys_call_table[array_index_nospec(nr, NR_syscalls)];
 		regs->regs[4] = syscall_fn(regs->orig_a0, regs->regs[5], regs->regs[6],
 					   regs->regs[7], regs->regs[8], regs->regs[9]);
arch/powerpc/kernel/syscall.c +2 -1
index a9da2af6efa8..45d11d518c51 100644
@@ -20,7 +20,8 @@ notrace long system_call_exception(struct pt_regs *regs, unsigned long r0)
 	syscall_fn f;
 
 	add_random_kstack_offset();
-	r0 = syscall_enter_from_user_mode(regs, r0);
+	if (unlikely(syscall_enter_from_user_mode(regs, &r0)))
+		return syscall_get_error(current, regs);
 
 	if (unlikely(r0 >= NR_syscalls)) {
 		if (unlikely(trap_is_unsupported_scv(regs))) {
arch/riscv/kernel/traps.c +3 -3
index 8c62c771a656..9326a4a50696 100644
@@ -325,7 +325,7 @@ asmlinkage __visible __trap_section  __no_stack_protector
 void do_trap_ecall_u(struct pt_regs *regs)
 {
 	if (user_mode(regs)) {
-		long syscall = regs->a7;
+		long ret, syscall = regs->a7;
 
 		regs->epc += 4;
 		regs->orig_a0 = regs->a0;
@@ -333,11 +333,11 @@ void do_trap_ecall_u(struct pt_regs *regs)
 
 		riscv_v_vstate_discard(regs);
 
-		syscall = syscall_enter_from_user_mode(regs, syscall);
+		ret = syscall_enter_from_user_mode(regs, &syscall);
 
 		add_random_kstack_offset();
 
-		if (syscall >= 0 && syscall < NR_syscalls) {
+		if (syscall >= 0 && syscall < NR_syscalls && !ret) {
 			syscall = array_index_nospec(syscall, NR_syscalls);
 			syscall_handler(regs, syscall);
 		}
arch/s390/kernel/syscall.c +3 -3
index 75d5a3cab14e..9e5b873c011d 100644
@@ -95,7 +95,7 @@ SYSCALL_DEFINE0(ni_syscall)
 
 void noinstr __do_syscall(struct pt_regs *regs, int per_trap)
 {
-	unsigned long nr;
+	unsigned long nr, ret;
 
 	enter_from_user_mode(regs);
 	add_random_kstack_offset();
@@ -121,7 +121,7 @@ void noinstr __do_syscall(struct pt_regs *regs, int per_trap)
 		regs->psw.addr = current->restart_block.arch_data;
 		current->restart_block.arch_data = 1;
 	}
-	nr = syscall_enter_from_user_mode_work(regs, nr);
+	ret = syscall_enter_from_user_mode_work(regs, &nr);
 	/*
 	 * In the s390 ptrace ABI, both the syscall number and the return value
 	 * use gpr2. However, userspace puts the syscall number either in the
@@ -129,7 +129,7 @@ void noinstr __do_syscall(struct pt_regs *regs, int per_trap)
 	 * work, the ptrace code sets PIF_SYSCALL_RET_SET, which is checked here
 	 * and if set, the syscall will be skipped.
 	 */
-	if (unlikely(test_and_clear_pt_regs_flag(regs, PIF_SYSCALL_RET_SET)))
+	if (unlikely(test_and_clear_pt_regs_flag(regs, PIF_SYSCALL_RET_SET) || ret))
 		goto out;
 	regs->gprs[2] = -ENOSYS;
 	if (likely(nr < NR_syscalls)) {
arch/x86/entry/syscall_32.c +18 -21
index 31b9492fe851..525e99691b31 100644
@@ -128,7 +128,7 @@ static __always_inline bool int80_is_external(void)
  */
 __visible noinstr void do_int80_emulation(struct pt_regs *regs)
 {
-	int nr;
+	long nr;
 
 	/* Kernel does not use INT $0x80! */
 	if (unlikely(!user_mode(regs))) {
@@ -168,8 +168,7 @@ __visible noinstr void do_int80_emulation(struct pt_regs *regs)
 	nr = syscall_32_enter(regs);
 
 	local_irq_enable();
-	nr = syscall_enter_from_user_mode_work(regs, nr);
-	do_syscall_32_irqs_on(regs, nr);
+	syscall_enter_from_user_mode_work(regs, &nr);
 
 	instrumentation_end();
 	syscall_exit_to_user_mode(regs);
@@ -208,7 +207,7 @@ __visible noinstr void do_int80_emulation(struct pt_regs *regs)
  */
 DEFINE_FREDENTRY_RAW(int80_emulation)
 {
-	int nr;
+	long nr;
 
 	enter_from_user_mode(regs);
 
@@ -232,8 +231,10 @@ DEFINE_FREDENTRY_RAW(int80_emulation)
 	nr = syscall_32_enter(regs);
 
 	local_irq_enable();
-	nr = syscall_enter_from_user_mode_work(regs, nr);
-	do_syscall_32_irqs_on(regs, nr);
+	if (!syscall_enter_from_user_mode_work(regs, &nr)) {
+		nr &= GENMASK(31, 0);
+		do_syscall_32_irqs_on(regs, nr);
+	}
 
 	instrumentation_end();
 	syscall_exit_to_user_mode(regs);
@@ -245,20 +246,17 @@ DEFINE_FREDENTRY_RAW(int80_emulation)
 /* Handles int $0x80 on a 32bit kernel */
 __visible noinstr void do_int80_syscall_32(struct pt_regs *regs)
 {
-	int nr = syscall_32_enter(regs);
-
-	/*
-	 * Subtlety here: if ptrace pokes something larger than 2^31-1 into
-	 * orig_ax, the int return value truncates it. This matches
-	 * the semantics of syscall_get_nr().
-	 */
-	nr = syscall_enter_from_user_mode(regs, nr);
-	instrumentation_begin();
+	long nr = syscall_32_enter(regs);
 
 	add_random_kstack_offset();
-	do_syscall_32_irqs_on(regs, nr);
+	if (!syscall_enter_from_user_mode(regs, &nr)) {
+		instrumentation_begin();
 
-	instrumentation_end();
+		nr &= & GENMASK(31, 0);
+		do_syscall_32_irqs_on(regs, nr);
+
+		instrumentation_end();
+	}
 	syscall_exit_to_user_mode(regs);
 }
 #endif /* !CONFIG_IA32_EMULATION */
@@ -301,10 +299,9 @@ static noinstr bool __do_fast_syscall_32(struct pt_regs *regs)
 		return false;
 	}
 
-	nr = syscall_enter_from_user_mode_work(regs, nr);
-
-	/* Now this is just like a normal syscall. */
-	do_syscall_32_irqs_on(regs, nr);
+	if (!syscall_enter_from_user_mode_work(regs, &nr))
+		/* Now this is just like a normal syscall. */
+		do_syscall_32_irqs_on(regs, nr);
 
 	instrumentation_end();
 	syscall_exit_to_user_mode(regs);
arch/x86/entry/syscall_64.c +10 -9
index 71f032504e73..3400c2f43a62 100644
@@ -84,19 +84,20 @@ static __always_inline bool do_syscall_x32(struct pt_regs *regs, int nr)
 }
 
 /* Returns true to return using SYSRET, or false to use IRET */
-__visible noinstr bool do_syscall_64(struct pt_regs *regs, int nr)
+__visible noinstr bool do_syscall_64(struct pt_regs *regs, long nr)
 {
-	nr = syscall_enter_from_user_mode(regs, nr);
-
-	instrumentation_begin();
 	add_random_kstack_offset();
+	if (!syscall_enter_from_user_mode(regs, &nr)) {
 
-	if (!do_syscall_x64(regs, nr) && !do_syscall_x32(regs, nr) && nr != -1) {
-		/* Invalid system call, but still a system call. */
-		regs->ax = __x64_sys_ni_syscall(regs);
-	}
+		instrumentation_begin();
 
-	instrumentation_end();
+		if (!do_syscall_x64(regs, nr) && !do_syscall_x32(regs, nr)) {
+			/* Invalid system call, but still a system call. */
+			regs->ax = __x64_sys_ni_syscall(regs);
+		}
+
+		instrumentation_end();
+	}
 	syscall_exit_to_user_mode(regs);
 
 	/*
include/linux/entry-common.h +17 -21
index 416a3352261f..4991071d01fe 100644
@@ -69,10 +69,8 @@ static inline void syscall_enter_audit(struct pt_regs *regs, long syscall)
 	}
 }
 
-static __always_inline long syscall_trace_enter(struct pt_regs *regs, unsigned long work)
+static __always_inline long syscall_trace_enter(struct pt_regs *regs, unsigned long work, unsigned long *syscall)
 {
-	long syscall, ret = 0;
-
 	/*
 	 * Handle Syscall User Dispatch.  This must comes first, since
 	 * the ABI here can be something that doesn't make sense for
@@ -93,27 +91,25 @@ static __always_inline long syscall_trace_enter(struct pt_regs *regs, unsigned l
 
 	/* Handle ptrace */
 	if (work & (SYSCALL_WORK_SYSCALL_TRACE | SYSCALL_WORK_SYSCALL_EMU)) {
-		ret = arch_ptrace_report_syscall_entry(regs);
-		if (ret || (work & SYSCALL_WORK_SYSCALL_EMU))
+		if (arch_ptrace_report_syscall_entry(regs) || (work & SYSCALL_WORK_SYSCALL_EMU))
 			return -1L;
 	}
 
 	/* Do seccomp after ptrace, to catch any tracer changes. */
 	if (work & SYSCALL_WORK_SECCOMP) {
-		ret = __secure_computing();
-		if (ret == -1L)
-			return ret;
+		if (__secure_computing())
+			return -1L;
 	}
 
 	/* Either of the above might have changed the syscall number */
-	syscall = syscall_get_nr(current, regs);
+	*syscall = syscall_get_nr(current, regs);
 
 	if (unlikely(work & SYSCALL_WORK_SYSCALL_TRACEPOINT))
-		syscall = trace_syscall_enter(regs, syscall);
+		*syscall = trace_syscall_enter(regs, *syscall);
 
-	syscall_enter_audit(regs, syscall);
+	syscall_enter_audit(regs, *syscall);
 
-	return ret ? : syscall;
+	return 0;
 }
 
 /**
@@ -126,12 +122,12 @@ static __always_inline long syscall_trace_enter(struct pt_regs *regs, unsigned l
  * enabled after invoking enter_from_user_mode(), enabling interrupts and
  * extra architecture specific work.
  *
- * Returns: The original or a modified syscall number
+ * Returns: The original or a modified syscall number as syscall
  *
- * If the returned syscall number is -1 then the syscall should be
- * skipped. In this case the caller may invoke syscall_set_error() or
- * syscall_set_return_value() first.  If neither of those are called and -1
- * is returned, then the syscall will fail with ENOSYS.
+ * If the returned value is -1 then the syscall should be skipped. In this case
+ * the caller may invoke syscall_set_error() or syscall_set_return_value()
+ * first.  If neither of those are called and -1 is returned, then the syscall
+ * will fail with ENOSYS.
  *
  * It handles the following work items:
  *
@@ -139,14 +135,14 @@ static __always_inline long syscall_trace_enter(struct pt_regs *regs, unsigned l
  *     ptrace_report_syscall_entry(), __secure_computing(), trace_sys_enter()
  *  2) Invocation of audit_syscall_entry()
  */
-static __always_inline long syscall_enter_from_user_mode_work(struct pt_regs *regs, long syscall)
+static __always_inline long syscall_enter_from_user_mode_work(struct pt_regs *regs, long *syscall)
 {
 	unsigned long work = READ_ONCE(current_thread_info()->syscall_work);
 
 	if (work & SYSCALL_WORK_ENTER)
-		syscall = syscall_trace_enter(regs, work);
+		return syscall_trace_enter(regs, work, syscall);
 
-	return syscall;
+	return 0;
 }
 
 /**
@@ -167,7 +163,7 @@ static __always_inline long syscall_enter_from_user_mode_work(struct pt_regs *re
  * Returns: The original or a modified syscall number. See
  * syscall_enter_from_user_mode_work() for further explanation.
  */
-static __always_inline long syscall_enter_from_user_mode(struct pt_regs *regs, long syscall)
+static __always_inline long syscall_enter_from_user_mode(struct pt_regs *regs, long *syscall)
 {
 	long ret;
 
Signature · 1 line
2.51.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider: - [Critical] System call execution is entirely omitted in non-FRED `do_int80_emulation`. - [High]…

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
- [Critical] System call execution is entirely omitted in non-FRED do_int80_emulation.
- [High] Incompatible pointer types passed to generic entry APIs cause -Werror=incompatible-pointer-types build failures across multiple architectures.
- [High] Syntax error due to taking the address of an rvalue macro expansion.
- [High] add_random_kstack_offset() is called before RCU is watching, violating context tracking and noinstr bounds.
- [High] Aborted system calls fail to return the default -ENOSYS on s390 and PowerPC.
- [High] The function definition for do_syscall_64 was changed but its header declaration was not, causing a conflicting types compiler error. --

commit cc1ccd5571695deb44c86f464ec406994af66997
Author: Michal Suchánek <[email protected]>

entry: Untangle the return value of syscall_enter_from_user_mode from syscall NR

The return value of syscall_enter_from_user_mode is used both for the adjusted syscall number and the indicator that a syscall should be skipped. This patch passes the syscall number as a pointer to the inline entry functions, and uses the return value exclusively for the indication that the syscall is already handled.

arch/powerpc/kernel/syscall.c notrace long system_call_exception(struct pt_regs *regs, unsigned long r0) · line 23

Quoted 10 lines
arch/powerpc/kernel/syscall.c +1 -1
index a9da2af6efa87..45d11d518c517 100644
@@ -20,7 +20,8 @@ notrace long system_call_exception(struct pt_regs *regs, unsigned long r0)
 	syscall_fn f;
 
 	add_random_kstack_offset();
-	r0 = syscall_enter_from_user_mode(regs, r0);
+	if (unlikely(syscall_enter_from_user_mode(regs, &r0)))

[Severity: High]
Will passing an unsigned long pointer to a function expecting a long pointer cause an incompatible pointer type build failure?

Also, if a tracer aborts the syscall, will this return the unmodified register state via syscall_get_error() instead of falling through to assign -ENOSYS?

Tracer filters that abort without injecting a return value generally expect the system call to naturally return -ENOSYS.

Quoted 16 lines
arch/powerpc/kernel/syscall.c +1 -0
+		return syscall_get_error(current, regs);
 
arch/s390/kernel/syscall.c +1 -1
index 75d5a3cab14e6..9e5b873c011d7 100644
@@ -121,7 +121,7 @@ void noinstr __do_syscall(struct pt_regs *regs, int per_trap)
 		regs->psw.addr = current->restart_block.arch_data;
 		current->restart_block.arch_data = 1;
 	}
-	nr = syscall_enter_from_user_mode_work(regs, nr);
+	ret = syscall_enter_from_user_mode_work(regs, &nr);
 	/*
 	 * In the s390 ptrace ABI, both the syscall number and the return value
 	 * use gpr2. However, userspace puts the syscall number either in the

arch/s390/kernel/syscall.c void noinstr __do_syscall(struct pt_regs *regs, int per_trap) · line 134

Quoted 7 lines
arch/s390/kernel/syscall.c +1 -1
@@ -129,7 +129,7 @@ void noinstr __do_syscall(struct pt_regs *regs, int per_trap)
 	 * work, the ptrace code sets PIF_SYSCALL_RET_SET, which is checked here
 	 * and if set, the syscall will be skipped.
 	 */
-	if (unlikely(test_and_clear_pt_regs_flag(regs, PIF_SYSCALL_RET_SET)))
+	if (unlikely(test_and_clear_pt_regs_flag(regs, PIF_SYSCALL_RET_SET) || ret))
 		goto out;
 	regs->gprs[2] = -ENOSYS;

[Severity: High]
If ret is non-zero, this skips the -ENOSYS assignment and branches directly to out.

Could this leak the syscall number as the return value to userspace if a tracer aborts the syscall without injecting a return value?

arch/x86/entry/syscall_32.c __visible noinstr void do_int80_emulation(struct pt_regs *regs) · line 172

Quoted 12 lines
arch/x86/entry/syscall_32.c +1 -2
index 31b9492fe851d..525e99691b312 100644
@@ -168,8 +168,7 @@ __visible noinstr void do_int80_emulation(struct pt_regs *regs)
 	nr = syscall_32_enter(regs);
 
 	local_irq_enable();
-	nr = syscall_enter_from_user_mode_work(regs, nr);
-	do_syscall_32_irqs_on(regs, nr);
+	syscall_enter_from_user_mode_work(regs, &nr);
 

[Severity: Critical]
Is it intentional to omit the system call execution here?

It looks like do_syscall_32_irqs_on was deleted without replacement, which might cause system calls using int 0x80 to silently fail to execute on non-FRED kernels.

Quoted 2 lines
arch/x86/entry/syscall_32.c +0 -0
 	instrumentation_end();
 	syscall_exit_to_user_mode(regs);

[ ... ]

Quoted 8 lines
arch/x86/entry/syscall_32.c +0 -4
@@ -245,20 +246,17 @@ DEFINE_FREDENTRY_RAW(int80_emulation)
 /* Handles int $0x80 on a 32bit kernel */
 __visible noinstr void do_int80_syscall_32(struct pt_regs *regs)
 {
-	int nr = syscall_32_enter(regs);
-
-	/*
-	 * Subtlety here: if ptrace pokes something larger than 2^31-1 into

arch/x86/entry/syscall_32.c DEFINE_FREDENTRY_RAW(int80_emulation) · line 252

Quoted 10 lines
arch/x86/entry/syscall_32.c +2 -6
-	 * orig_ax, the int return value truncates it. This matches
-	 * the semantics of syscall_get_nr().
-	 */
-	nr = syscall_enter_from_user_mode(regs, nr);
-	instrumentation_begin();
+	long nr = syscall_32_enter(regs);
 
 	add_random_kstack_offset();
-	do_syscall_32_irqs_on(regs, nr);
+	if (!syscall_enter_from_user_mode(regs, &nr)) {

[Severity: High]
Does calling add_random_kstack_offset() before syscall_enter_from_user_mode() violate context tracking and noinstr bounds?

syscall_enter_from_user_mode() internally calls enter_from_user_mode(), which informs RCU that the CPU is back in the kernel.

Accessing RCU-protected variables before this happens could trigger PROVE_RCU warnings.

arch/x86/entry/syscall_32.c DEFINE_FREDENTRY_RAW(int80_emulation) · line 255

Quoted 4 lines
arch/x86/entry/syscall_32.c +2 -1
+		instrumentation_begin();
 
-	instrumentation_end();
+		nr &= & GENMASK(31, 0);

[Severity: High]
Does this cause a syntax error during compilation?

It appears to take the address of an rvalue macro expansion, which might result in an lvalue required as unary & operand error.

Quoted 6 lines
arch/x86/entry/syscall_32.c +4 -0
+		do_syscall_32_irqs_on(regs, nr);
+
+		instrumentation_end();
+	}
 	syscall_exit_to_user_mode(regs);
 }

[ ... ]

arch/x86/entry/syscall_32.c static noinstr bool __do_fast_syscall_32(struct pt_regs *regs) · line 302

Quoted 9 lines
arch/x86/entry/syscall_32.c +1 -4
@@ -301,10 +299,9 @@ static noinstr bool __do_fast_syscall_32(struct pt_regs *regs)
 		return false;
 	}
 
-	nr = syscall_enter_from_user_mode_work(regs, nr);
-
-	/* Now this is just like a normal syscall. */
-	do_syscall_32_irqs_on(regs, nr);
+	if (!syscall_enter_from_user_mode_work(regs, &nr))

[Severity: High]
Since nr is defined as an int earlier in __do_fast_syscall_32:

	int nr = syscall_32_enter(regs);

Could passing it to syscall_enter_from_user_mode_work, which expects a long pointer, cause an incompatible pointer types build failure?

arch/x86/entry/syscall_64.c static __always_inline bool do_syscall_x32(struct pt_regs *regs, int nr) · line 87

Quoted 13 lines
arch/x86/entry/syscall_32.c +2 -0
+		/* Now this is just like a normal syscall. */
+		do_syscall_32_irqs_on(regs, nr);
 
arch/x86/entry/syscall_64.c +1 -1
index 71f032504e731..3400c2f43a62f 100644
@@ -84,19 +84,20 @@ static __always_inline bool do_syscall_x32(struct pt_regs *regs, int nr)
 }
 
 /* Returns true to return using SYSRET, or false to use IRET */
-__visible noinstr bool do_syscall_64(struct pt_regs *regs, int nr)
+__visible noinstr bool do_syscall_64(struct pt_regs *regs, long nr)

[Severity: High]
Will changing this definition to take a long cause a conflicting types build failure, since the header declaration in arch/x86/include/asm/syscall.h still declares it with an int parameter?

arch/x86/entry/syscall_64.c static __always_inline bool do_syscall_x32(struct pt_regs *regs, int nr) · line 90

Quoted 6 lines
arch/x86/entry/syscall_64.c +1 -3
 {
-	nr = syscall_enter_from_user_mode(regs, nr);
-
-	instrumentation_begin();
 	add_random_kstack_offset();
+	if (!syscall_enter_from_user_mode(regs, &nr)) {

[Severity: High]
Could placing add_random_kstack_offset() before syscall_enter_from_user_mode() cause PROVE_RCU warnings here as well?

Quoted 17 lines
arch/x86/entry/syscall_64.c +0 -4
-	if (!do_syscall_x64(regs, nr) && !do_syscall_x32(regs, nr) && nr != -1) {
-		/* Invalid system call, but still a system call. */
-		regs->ax = __x64_sys_ni_syscall(regs);
-	}
 
include/linux/entry-common.h +1 -1
index 416a3352261fe..4991071d01fed 100644
@@ -69,10 +69,8 @@ static inline void syscall_enter_audit(struct pt_regs *regs, long syscall)
 	}
 }
 
-static __always_inline long syscall_trace_enter(struct pt_regs *regs, unsigned long work)
+static __always_inline long syscall_trace_enter(struct pt_regs *regs, unsigned long work, unsigned long *syscall)
 {

[ ... ]

include/linux/entry-common.h static __always_inline long syscall_trace_enter(struct pt_regs *regs, unsigned l · line 140

Quoted 9 lines
include/linux/entry-common.h +2 -2
@@ -139,14 +135,14 @@ static __always_inline long syscall_trace_enter(struct pt_regs *regs, unsigned l
-static __always_inline long syscall_enter_from_user_mode_work(struct pt_regs *regs, long syscall)
+static __always_inline long syscall_enter_from_user_mode_work(struct pt_regs *regs, long *syscall)
 {
 	unsigned long work = READ_ONCE(current_thread_info()->syscall_work);
 
 	if (work & SYSCALL_WORK_ENTER)
-		syscall = syscall_trace_enter(regs, work);
+		return syscall_trace_enter(regs, work, syscall);

[Severity: High]
Could passing a long pointer to syscall_trace_enter, which expects an unsigned long pointer, result in an incompatible pointer type build failure?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Michal Suchánek
Quoted 379 lines · Michal Suchánek

The return value of syscall_enter_from_user_mode is used both for the adjusted syscall number and the indicator that a syscall should be skipped.

As seccomp can be invoked on any syscall, including invalid ones this somewhat undermines seccomp.

While the seccomp variants that terminate the process do not need to care about this for the filter that sets the syscall return value this disctinction is required.

Pass the syscall number as a pointer to the inline entry functions, and use the return value exclusively for the indication that the syscall is already handled.

This should avoid the need for the s390 PIF_SYSCALL_RET_SET which is the workaround for exactly this deficiency.

If this is desirable the patch could be split into some series that adjusts the code flow where needed so that the final change is mostly mechanical.

There is also another way to handle this problem.

With x86 using bit 30 to denote compatibility syscall it sounds like declaring syscall number a 30bit quantity would work.

Then bit 31 could be used to denote an invalid syscall that can never be executed, and the -1 returned from syscall_enter_from_user_mode would then be inherently invalid.

That is so long as no architectures use syscall numbers outside of this range so far, and the limitation is considered fine.

---

Documentation/core-api/entry.rst | 12 ++++++----
arch/loongarch/kernel/syscall.c  |  6 ++---
arch/powerpc/kernel/syscall.c    |  3 ++-
arch/riscv/kernel/traps.c        |  6 ++---
arch/s390/kernel/syscall.c       |  6 ++---
arch/x86/entry/syscall_32.c      | 39 +++++++++++++++-----------------
arch/x86/entry/syscall_64.c      | 19 ++++++++--------
include/linux/entry-common.h     | 38 ++++++++++++++-----------------
8 files changed, 63 insertions(+), 66 deletions(-)
Documentation/core-api/entry.rst +7 -5
index 71d8eedc0549..b0bfae31fe7c 100644
@@ -68,12 +68,14 @@ invoked from low-level assembly code looks like this:
  noinstr void syscall(struct pt_regs *regs, int nr)
  {
 	arch_syscall_enter(regs);
-	nr = syscall_enter_from_user_mode(regs, nr);
 
-	instrumentation_begin();
-	if (!invoke_syscall(regs, nr) && nr != -1)
-	 	result_reg(regs) = __sys_ni_syscall(regs);
-	instrumentation_end();
+	/* Skip syscall when -1 is returned */
+	if (!syscall_enter_from_user_mode(regs, &nr)) {
+		instrumentation_begin();
+		if (!invoke_syscall(regs, nr) && nr != -1)
+			result_reg(regs) = __sys_ni_syscall(regs);
+		instrumentation_end();
+	}
 
 	syscall_exit_to_user_mode(regs);
  }
arch/loongarch/kernel/syscall.c +3 -3
index 94c1c3b5b0b5..fc18ac56b91c 100644
@@ -58,7 +58,7 @@ typedef long (*sys_call_fn)(unsigned long, unsigned long,
 
 void noinstr __no_stack_protector do_syscall(struct pt_regs *regs)
 {
-	unsigned long nr;
+	unsigned long nr, ret;
 	sys_call_fn syscall_fn;
 
 	nr = regs->regs[11];
@@ -70,11 +70,11 @@ void noinstr __no_stack_protector do_syscall(struct pt_regs *regs)
 	regs->orig_a0 = regs->regs[4];
 	regs->regs[4] = -ENOSYS;
 
-	nr = syscall_enter_from_user_mode(regs, nr);
+	ret = syscall_enter_from_user_mode(regs, &nr);
 
 	add_random_kstack_offset();
 
-	if (nr < NR_syscalls) {
+	if (nr < NR_syscalls && !ret) {
 		syscall_fn = sys_call_table[array_index_nospec(nr, NR_syscalls)];
 		regs->regs[4] = syscall_fn(regs->orig_a0, regs->regs[5], regs->regs[6],
 					   regs->regs[7], regs->regs[8], regs->regs[9]);
arch/powerpc/kernel/syscall.c +2 -1
index a9da2af6efa8..45d11d518c51 100644
@@ -20,7 +20,8 @@ notrace long system_call_exception(struct pt_regs *regs, unsigned long r0)
 	syscall_fn f;
 
 	add_random_kstack_offset();
-	r0 = syscall_enter_from_user_mode(regs, r0);
+	if (unlikely(syscall_enter_from_user_mode(regs, &r0)))
+		return syscall_get_error(current, regs);
 
 	if (unlikely(r0 >= NR_syscalls)) {
 		if (unlikely(trap_is_unsupported_scv(regs))) {
arch/riscv/kernel/traps.c +3 -3
index 8c62c771a656..9326a4a50696 100644
@@ -325,7 +325,7 @@ asmlinkage __visible __trap_section  __no_stack_protector
 void do_trap_ecall_u(struct pt_regs *regs)
 {
 	if (user_mode(regs)) {
-		long syscall = regs->a7;
+		long ret, syscall = regs->a7;
 
 		regs->epc += 4;
 		regs->orig_a0 = regs->a0;
@@ -333,11 +333,11 @@ void do_trap_ecall_u(struct pt_regs *regs)
 
 		riscv_v_vstate_discard(regs);
 
-		syscall = syscall_enter_from_user_mode(regs, syscall);
+		ret = syscall_enter_from_user_mode(regs, &syscall);
 
 		add_random_kstack_offset();
 
-		if (syscall >= 0 && syscall < NR_syscalls) {
+		if (syscall >= 0 && syscall < NR_syscalls && !ret) {
 			syscall = array_index_nospec(syscall, NR_syscalls);
 			syscall_handler(regs, syscall);
 		}
arch/s390/kernel/syscall.c +3 -3
index 75d5a3cab14e..9e5b873c011d 100644
@@ -95,7 +95,7 @@ SYSCALL_DEFINE0(ni_syscall)
 
 void noinstr __do_syscall(struct pt_regs *regs, int per_trap)
 {
-	unsigned long nr;
+	unsigned long nr, ret;
 
 	enter_from_user_mode(regs);
 	add_random_kstack_offset();
@@ -121,7 +121,7 @@ void noinstr __do_syscall(struct pt_regs *regs, int per_trap)
 		regs->psw.addr = current->restart_block.arch_data;
 		current->restart_block.arch_data = 1;
 	}
-	nr = syscall_enter_from_user_mode_work(regs, nr);
+	ret = syscall_enter_from_user_mode_work(regs, &nr);
 	/*
 	 * In the s390 ptrace ABI, both the syscall number and the return value
 	 * use gpr2. However, userspace puts the syscall number either in the
@@ -129,7 +129,7 @@ void noinstr __do_syscall(struct pt_regs *regs, int per_trap)
 	 * work, the ptrace code sets PIF_SYSCALL_RET_SET, which is checked here
 	 * and if set, the syscall will be skipped.
 	 */
-	if (unlikely(test_and_clear_pt_regs_flag(regs, PIF_SYSCALL_RET_SET)))
+	if (unlikely(test_and_clear_pt_regs_flag(regs, PIF_SYSCALL_RET_SET) || ret))
 		goto out;
 	regs->gprs[2] = -ENOSYS;
 	if (likely(nr < NR_syscalls)) {
arch/x86/entry/syscall_32.c +18 -21
index 31b9492fe851..525e99691b31 100644
@@ -128,7 +128,7 @@ static __always_inline bool int80_is_external(void)
 */
 __visible noinstr void do_int80_emulation(struct pt_regs *regs)
 {
-	int nr;
+	long nr;
 
 	/* Kernel does not use INT $0x80! */
 	if (unlikely(!user_mode(regs))) {
@@ -168,8 +168,7 @@ __visible noinstr void do_int80_emulation(struct pt_regs *regs)
 	nr = syscall_32_enter(regs);
 
 	local_irq_enable();
-	nr = syscall_enter_from_user_mode_work(regs, nr);
-	do_syscall_32_irqs_on(regs, nr);
+	syscall_enter_from_user_mode_work(regs, &nr);
 
 	instrumentation_end();
 	syscall_exit_to_user_mode(regs);
@@ -208,7 +207,7 @@ __visible noinstr void do_int80_emulation(struct pt_regs *regs)
 */
 DEFINE_FREDENTRY_RAW(int80_emulation)
 {
-	int nr;
+	long nr;
 
 	enter_from_user_mode(regs);
 
@@ -232,8 +231,10 @@ DEFINE_FREDENTRY_RAW(int80_emulation)
 	nr = syscall_32_enter(regs);
 
 	local_irq_enable();
-	nr = syscall_enter_from_user_mode_work(regs, nr);
-	do_syscall_32_irqs_on(regs, nr);
+	if (!syscall_enter_from_user_mode_work(regs, &nr)) {
+		nr &= GENMASK(31, 0);
+		do_syscall_32_irqs_on(regs, nr);
+	}
 
 	instrumentation_end();
 	syscall_exit_to_user_mode(regs);
@@ -245,20 +246,17 @@ DEFINE_FREDENTRY_RAW(int80_emulation)
 /* Handles int $0x80 on a 32bit kernel */
 __visible noinstr void do_int80_syscall_32(struct pt_regs *regs)
 {
-	int nr = syscall_32_enter(regs);
-
-	/*
-	 * Subtlety here: if ptrace pokes something larger than 2^31-1 into
-	 * orig_ax, the int return value truncates it. This matches
-	 * the semantics of syscall_get_nr().
-	 */
-	nr = syscall_enter_from_user_mode(regs, nr);
-	instrumentation_begin();
+	long nr = syscall_32_enter(regs);
 
 	add_random_kstack_offset();
-	do_syscall_32_irqs_on(regs, nr);
+	if (!syscall_enter_from_user_mode(regs, &nr)) {
+		instrumentation_begin();
 
-	instrumentation_end();
+		nr &= & GENMASK(31, 0);
+		do_syscall_32_irqs_on(regs, nr);
+
+		instrumentation_end();
+	}
 	syscall_exit_to_user_mode(regs);
 }
 #endif /* !CONFIG_IA32_EMULATION */
@@ -301,10 +299,9 @@ static noinstr bool __do_fast_syscall_32(struct pt_regs *regs)
 		return false;
 	}
 
-	nr = syscall_enter_from_user_mode_work(regs, nr);
-
-	/* Now this is just like a normal syscall. */
-	do_syscall_32_irqs_on(regs, nr);
+	if (!syscall_enter_from_user_mode_work(regs, &nr))
+		/* Now this is just like a normal syscall. */
+		do_syscall_32_irqs_on(regs, nr);
 
 	instrumentation_end();
 	syscall_exit_to_user_mode(regs);
arch/x86/entry/syscall_64.c +10 -9
index 71f032504e73..3400c2f43a62 100644
@@ -84,19 +84,20 @@ static __always_inline bool do_syscall_x32(struct pt_regs *regs, int nr)
 }
 
 /* Returns true to return using SYSRET, or false to use IRET */
-__visible noinstr bool do_syscall_64(struct pt_regs *regs, int nr)
+__visible noinstr bool do_syscall_64(struct pt_regs *regs, long nr)
 {
-	nr = syscall_enter_from_user_mode(regs, nr);
-
-	instrumentation_begin();
 	add_random_kstack_offset();
+	if (!syscall_enter_from_user_mode(regs, &nr)) {
 
-	if (!do_syscall_x64(regs, nr) && !do_syscall_x32(regs, nr) && nr != -1) {
-		/* Invalid system call, but still a system call. */
-		regs->ax = __x64_sys_ni_syscall(regs);
-	}
+		instrumentation_begin();
 
-	instrumentation_end();
+		if (!do_syscall_x64(regs, nr) && !do_syscall_x32(regs, nr)) {
+			/* Invalid system call, but still a system call. */
+			regs->ax = __x64_sys_ni_syscall(regs);
+		}
+
+		instrumentation_end();
+	}
 	syscall_exit_to_user_mode(regs);
 
 	/*
include/linux/entry-common.h +16 -20
index 416a3352261f..4991071d01fe 100644
@@ -69,10 +69,8 @@ static inline void syscall_enter_audit(struct pt_regs *regs, long syscall)
 	}
 }
 
-static __always_inline long syscall_trace_enter(struct pt_regs *regs, unsigned long work)
+static __always_inline long syscall_trace_enter(struct pt_regs *regs, unsigned long work, unsigned long *syscall)
 {
-	long syscall, ret = 0;
-
 	/*
 	 * Handle Syscall User Dispatch.  This must comes first, since
 	 * the ABI here can be something that doesn't make sense for
@@ -93,27 +91,25 @@ static __always_inline long syscall_trace_enter(struct pt_regs *regs, unsigned l
 
 	/* Handle ptrace */
 	if (work & (SYSCALL_WORK_SYSCALL_TRACE | SYSCALL_WORK_SYSCALL_EMU)) {
-		ret = arch_ptrace_report_syscall_entry(regs);
-		if (ret || (work & SYSCALL_WORK_SYSCALL_EMU))
+		if (arch_ptrace_report_syscall_entry(regs) || (work & SYSCALL_WORK_SYSCALL_EMU))
 			return -1L;
 	}
 
 	/* Do seccomp after ptrace, to catch any tracer changes. */
 	if (work & SYSCALL_WORK_SECCOMP) {
-		ret = __secure_computing();
-		if (ret == -1L)
-			return ret;
+		if (__secure_computing())
+			return -1L;
 	}
 
 	/* Either of the above might have changed the syscall number */
-	syscall = syscall_get_nr(current, regs);
+	*syscall = syscall_get_nr(current, regs);
 
 	if (unlikely(work & SYSCALL_WORK_SYSCALL_TRACEPOINT))
-		syscall = trace_syscall_enter(regs, syscall);
+		*syscall = trace_syscall_enter(regs, *syscall);
 
-	syscall_enter_audit(regs, syscall);
+	syscall_enter_audit(regs, *syscall);
 
-	return ret ? : syscall;
+	return 0;
 }
 
 /**
@@ -126,12 +122,12 @@ static __always_inline long syscall_trace_enter(struct pt_regs *regs, unsigned l
 * enabled after invoking enter_from_user_mode(), enabling interrupts and
 * extra architecture specific work.
 *
- * Returns: The original or a modified syscall number
+ * Returns: The original or a modified syscall number as syscall
 *
- * If the returned syscall number is -1 then the syscall should be
- * skipped. In this case the caller may invoke syscall_set_error() or
- * syscall_set_return_value() first.  If neither of those are called and -1
- * is returned, then the syscall will fail with ENOSYS.
+ * If the returned value is -1 then the syscall should be skipped. In this case
+ * the caller may invoke syscall_set_error() or syscall_set_return_value()
+ * first.  If neither of those are called and -1 is returned, then the syscall
+ * will fail with ENOSYS.
 *
 * It handles the following work items:
 *
@@ -139,14 +135,14 @@ static __always_inline long syscall_trace_enter(struct pt_regs *regs, unsigned l
 *     ptrace_report_syscall_entry(), __secure_computing(), trace_sys_enter()
 *  2) Invocation of audit_syscall_entry()
 */
-static __always_inline long syscall_enter_from_user_mode_work(struct pt_regs *regs, long syscall)
+static __always_inline long syscall_enter_from_user_mode_work(struct pt_regs *regs, long *syscall)
 {
 	unsigned long work = READ_ONCE(current_thread_info()->syscall_work);
 
 	if (work & SYSCALL_WORK_ENTER)
-		syscall = syscall_trace_enter(regs, work);
+		return syscall_trace_enter(regs, work, syscall);
 
-	return syscall;
+	return 0;
 }
 
 /**

include/linux/entry-common.h static __always_inline long syscall_enter_from_user_mode_work(struct pt_regs *re · line 169

Quoted 8 lines
include/linux/entry-common.h +1 -1
@@ -167,7 +163,7 @@ static __always_inline long syscall_enter_from_user_mode_work(struct pt_regs *re
 * Returns: The original or a modified syscall number. See
 * syscall_enter_from_user_mode_work() for further explanation.
 */
-static __always_inline long syscall_enter_from_user_mode(struct pt_regs *regs, long syscall)
+static __always_inline long syscall_enter_from_user_mode(struct pt_regs *regs, long *syscall)
 {
 	long ret;
 

Negative numbers most definitely not be assigned as valid system calls, not now, not ever.

Therein lies some serious madness.

I believe setting the syscall number to -1 to skip is an ABI already in e.g. ptrace, so I doubt we can just get rid of it anyway.

I would say as follows:

Let's formally define that:

- valid system call numbers are positive 32-bit numbers, using the appropriate ABI convention for "int".

- bits [30:n] for some value of n are reserved for architecture-specific flags/modes. MIPS uses an offset of 2000 decimal between its syscall ABIs, which would imply n ~ 11, although I personally think that is too restrictive (MIPS could in fact use such a flag to provide an escape into a larger number space if we ever need more than 2000 system calls.)

I would suggest n = 24, at least for now. It is easier to give up additional bits later than to claw them back when already used.

Thus:

1. The type for a system call is int.

2. A valid system call number is always going to be positive.

3. Bits [30:24] are available for architecture ABI use. The "architecture independent" part of the system call number is therefore 24 bits wide.

4. The exact ABI is platform-specific, obviously, but as a general guideline (especially for new platforms/ABIs) should follow the rules for a platform "int" if practical. Notably, when passing a value in a register larger than 32 bits, which side of the calling interface is responsible for sign-extending a value passed in a register. If caller side, the kernel should validate, if callee side the kernel should ignore the additional bits and do the extension.

5. A negative system call number is guaranteed to return -ENOSYS (unless intercepted by seccomp, ptrace, or another mechanism under user space control.)

6. If the platform needs to algorithmically modify the system call number due to platform-specific concerns (say, the platform uses a 16-bit special purpose register for the syscall number, or it has multiple kernel entry points with different behavior), it should if at all possible transcode the system call number as necessary to match this convention in APIs that are exposed to general kernel code.

For example, in the future I could very much see the IA32 code in the x86 kernel using bit 29 internally to indicate an ia32 system call, simplifying the is_compat implementation on x86. It should not mean that passing bit 29 to either the syscall instruction or int $0x80 will be accepted.

#3 lore raw
Quoted 32 lines · H. Peter Anvin
Quoted 31 lines · Michal Suchánek

The return value of syscall_enter_from_user_mode is used both for the adjusted syscall number and the indicator that a syscall should be skipped.

As seccomp can be invoked on any syscall, including invalid ones this somewhat undermines seccomp.

While the seccomp variants that terminate the process do not need to care about this for the filter that sets the syscall return value this disctinction is required.

Pass the syscall number as a pointer to the inline entry functions, and use the return value exclusively for the indication that the syscall is already handled.

This should avoid the need for the s390 PIF_SYSCALL_RET_SET which is the workaround for exactly this deficiency.

If this is desirable the patch could be split into some series that adjusts the code flow where needed so that the final change is mostly mechanical.

There is also another way to handle this problem.

With x86 using bit 30 to denote compatibility syscall it sounds like declaring syscall number a 30bit quantity would work.

Then bit 31 could be used to denote an invalid syscall that can never be executed, and the -1 returned from syscall_enter_from_user_mode would then be inherently invalid.

Quoted 5 lines
Quoted 3 lines

That is so long as no architectures use syscall numbers outside of this range so far, and the limitation is considered fine.

Negative numbers most definitely not be assigned as valid system calls, not now, not ever.

Negativity of a number is a matter of intepretation. Sometimes the syscall number is decleared as int, sometimes long, sometimes unsigned long.

Passing -1 to strtoul generates some bit pattern that can then be compared to another bit pattern inside a seccomp filter program, for example.

Quoted 3 lines

Therein lies some serious madness.

I believe setting the syscall number to -1 to skip is an ABI already in e.g. ptrace, so I doubt we can just get rid of it anyway.

Yes, and seccomp can set the syscall number to -1 indicating it was handled already even if the number was -1 to start with. While -1 is not a valid syscall number it can still be filtered, at least on some architectures.

Quoted 10 lines

I would say as follows:

Let's formally define that:

- valid system call numbers are positive 32-bit numbers, using the appropriate ABI convention for "int".

- bits [30:n] for some value of n are reserved for architecture-specific flags/modes. MIPS uses an offset of 2000 decimal between its syscall ABIs, which would imply n ~ 11, although I personally think that is too restrictive (MIPS could in fact use such a flag to provide an escape into a larger number space if we ever need more than 2000 system calls.)

I would suggest n = 24, at least for now. It is easier to give up additional bits later than to claw them back when already used.

Quoted 7 lines

Thus:

1. The type for a system call is int.

2. A valid system call number is always going to be positive.

3. Bits [30:24] are available for architecture ABI use. The "architecture independent" part of the system call number is therefore 24 bits wide.

Will that also work correctly with seccomp?

As I understand it the current situation is that on x86 the BPF code passed to seccomp must filter the compat syscall bit in the PBF code, and I do not see how restricting the syscall value to 24bit would happen without changing the seccomp filter API.

See eg. https://lore.kernel.org/linuxppc-dev/[email protected]/
for sample code.

Quoted 2 lines

4. The exact ABI is platform-specific, obviously, but as a general guideline (especially for new platforms/ABIs) should follow the rules for a platform "int" if practical. Notably, when passing a value in a register larger than 32 bits, which side of the calling interface is responsible for sign-extending a value passed in a register. If caller side, the kernel should validate, if callee side the kernel should ignore the additional bits and do the extension.

Do we even want to play with sign-extend?

If the syscall number is >= 1<<n after masking off flags recognized by the platfrom (if any) it's invalid.

Quoted 1 line

5. A negative system call number is guaranteed to return -ENOSYS (unless intercepted by seccomp, ptrace, or another mechanism under user space control.)

Interception by seccomp is exactly the case that's wonky.

Quoted 3 lines

6. If the platform needs to algorithmically modify the system call number due to platform-specific concerns (say, the platform uses a 16-bit special purpose register for the syscall number, or it has multiple kernel entry points with different behavior), it should if at all possible transcode the system call number as necessary to match this convention in APIs that are exposed to general kernel code.

For example, in the future I could very much see the IA32 code in the x86 kernel using bit 29 internally to indicate an ia32 system call, simplifying the is_compat implementation on x86. It should not mean that passing bit 29 to either the syscall instruction or int $0x80 will be accepted.

As I understand the code it uses bit 30 for that. Maybe I missed something?

Thanks

Michal

maintainer
#4 lore raw
In reply to H. Peter Anvin

On Wed, Jul 01 2026 at 11:29, H. Peter Anvin wrote:

Can you please trim your replies? Scrolling through hundred lines of useless quoted text is just annoying.

Quoted 8 lines · Michal Suchánek
Quoted 5 lines
include/linux/entry-common.h +1 -1
-static __always_inline long syscall_enter_from_user_mode(struct pt_regs *regs, long syscall)
+static __always_inline long syscall_enter_from_user_mode(struct pt_regs *regs, long *syscall)
{
	long ret;

1. The type for a system call is int.

That ship has sailed long ago. man syscall ...

Quoted 1 line

2. A valid system call number is always going to be positive.

That's true today.

Quoted 11 lines

3. Bits [30:24] are available for architecture ABI use. The "architecture independent" part of the system call number is therefore 24 bits wide.

4. The exact ABI is platform-specific, obviously, but as a general guideline (especially for new platforms/ABIs) should follow the rules for a platform "int" if practical. Notably, when passing a value in a register larger than 32 bits, which side of the calling interface is responsible for sign-extending a value passed in a register. If caller side, the kernel should validate, if callee side the kernel should ignore the additional bits and do the extension.

The kernel sign expands today already, i.e. for compat syscalls.

Quoted 3 lines

5. A negative system call number is guaranteed to return -ENOSYS (unless intercepted by seccomp, ptrace, or another mechanism under user space control.)

That's true today.

ASM entry:

       regs->eax = -ENOSYS;

C entry:

       nr = syscall_enter_from_user_mode(regs, nr);

       if ((unsigned)nr < SYSCALL_MAX)
       	    regs->eax = handle_syscall();
       else if (nr != -1)
       	    regs->eax = -ENOSYS;

       ....

If seccomp overwrites regs->eax and aborts any syscall (including -1) by returning -1, then the value seccomp wrote into regs->eax is preserved and returned to user space.

The same applies for syscall_user_dispatch() and ptrace...() if they decide to overwrite regs->eax _and_ abort the syscall by letting syscall_enter_from_user_mode() return -1.

trace_syscall_enter() is not any different. If the magic BPF in there rewrites the syscall number to -1 then either the original -ENOSYS or the BPF induced overwrite is returned to user space.

It's less than obvious and I have no objections to clean that up and make it more intuitive, but I still fail to see what Michal is actually trying to solve and what the magic flag is for. If s390 requires it, then that's an s390 problem, but definitely x86 does not.

Quoted 10 lines

6. If the platform needs to algorithmically modify the system call number due to platform-specific concerns (say, the platform uses a 16-bit special purpose register for the syscall number, or it has multiple kernel entry points with different behavior), it should if at all possible transcode the system call number as necessary to match this convention in APIs that are exposed to general kernel code.

For example, in the future I could very much see the IA32 code in the x86 kernel using bit 29 internally to indicate an ia32 system call, simplifying the is_compat implementation on x86.

I don't see how that makes it simpler. Those are two different entry code paths and magic bits wont make that go away.

Quoted 2 lines

It should not mean that passing bit 29 to either the syscall instruction or int $0x80 will be accepted.

Your proposal looks even more like a solution in search of a problem than the original one.

Thanks,

tglx

reviewer
#5 lore raw
Quoted 8 lines · Thomas Gleixner

If seccomp overwrites regs->eax and aborts any syscall (including -1) by returning -1, then the value seccomp wrote into regs->eax is preserved and returned to user space.

The same applies for syscall_user_dispatch() and ptrace...() if they decide to overwrite regs->eax _and_ abort the syscall by letting syscall_enter_from_user_mode() return -1.

Quoted 8 lines

trace_syscall_enter() is not any different. If the magic BPF in there rewrites the syscall number to -1 then either the original -ENOSYS or the BPF induced overwrite is returned to user space.

It's less than obvious and I have no objections to clean that up and make it more intuitive, but I still fail to see what Michal is actually trying to solve and what the magic flag is for. If s390 requires it, then that's an s390 problem, but definitely x86 does not.

The difference between x86 and s390 is that on s390, regs->gprs[2] is used for both the syscall number and the syscall return value. That was a design mistake early in the begin about 25 years ago, but it's ABI now, so it cannot be changed.

When seccomp decides to skip a syscall, it write a return value into regs->gprs[2]. When syscall_enter_from_user_mode_work() returns, it returns this number. If it's negative all is good - the 'if (likely(nr < NR_syscalls))' conditiion would just catch it and skip the syscall.

But if it's a positive number, the code cannot distinguish whether that's a return value or a syscall number.

So I introduced PIF_SYSCALL_RET_SET when converting s390 to generic entry. This flag tells the syscall code that a return value was set in ptregs and the syscall should be skipped.

I'd like to see something like the change from Michal going in - cleaned up of course. It would allow us to get rid of PIF_SYSCALL_RET_SET.

maintainer
#6 lore raw
Quoted 9 lines · Sven Schnelle

The difference between x86 and s390 is that on s390, regs->gprs[2] is used for both the syscall number and the syscall return value. That was a design mistake early in the begin about 25 years ago, but it's ABI now, so it cannot be changed.

When seccomp decides to skip a syscall, it write a return value into regs->gprs[2]. When syscall_enter_from_user_mode_work() returns, it returns this number. If it's negative all is good - the 'if (likely(nr < NR_syscalls))' conditiion would just catch it and skip the syscall.

You do have regs->orig_gpr2; I didn't fully track its usage, but can't you treat that as the syscall nr and always consider regs->gprs[2] as the return value?

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

maintainer
#7 lore raw
In reply to Sven Schnelle
Quoted 10 lines · Sven Schnelle
Quoted 4 lines · Thomas Gleixner

It's less than obvious and I have no objections to clean that up and make it more intuitive, but I still fail to see what Michal is actually trying to solve and what the magic flag is for. If s390 requires it, then that's an s390 problem, but definitely x86 does not.

The difference between x86 and s390 is that on s390, regs->gprs[2] is used for both the syscall number and the syscall return value. That was a design mistake early in the begin about 25 years ago, but it's ABI now, so it cannot be changed.

Cute.

Quoted 11 lines

When seccomp decides to skip a syscall, it write a return value into regs->gprs[2]. When syscall_enter_from_user_mode_work() returns, it returns this number. If it's negative all is good - the 'if (likely(nr < NR_syscalls))' conditiion would just catch it and skip the syscall.

But if it's a positive number, the code cannot distinguish whether that's a return value or a syscall number.

So I introduced PIF_SYSCALL_RET_SET when converting s390 to generic entry. This flag tells the syscall code that a return value was set in ptregs and the syscall should be skipped.

You also could have added a 'syscall_ret' member to pt_regs, operate on that for the return values (seccomp, syscall...) and swap it into gprs[2] right before returning to user space.

Quoted 2 lines

I'd like to see something like the change from Michal going in - cleaned up of course. It would allow us to get rid of PIF_SYSCALL_RET_SET.

I have no objections against cleaning it up and making it less convoluted.

reviewer
#8 lore raw
Quoted 22 lines · Thomas Gleixner
Quoted 10 lines · Sven Schnelle
Quoted 4 lines · Thomas Gleixner

It's less than obvious and I have no objections to clean that up and make it more intuitive, but I still fail to see what Michal is actually trying to solve and what the magic flag is for. If s390 requires it, then that's an s390 problem, but definitely x86 does not.

The difference between x86 and s390 is that on s390, regs->gprs[2] is used for both the syscall number and the syscall return value. That was a design mistake early in the begin about 25 years ago, but it's ABI now, so it cannot be changed.

Cute.

Quoted 8 lines

When seccomp decides to skip a syscall, it write a return value into regs->gprs[2]. When syscall_enter_from_user_mode_work() returns, it returns this number. If it's negative all is good - the 'if (likely(nr < NR_syscalls))' conditiion would just catch it and skip the syscall.

But if it's a positive number, the code cannot distinguish whether that's a return value or a syscall number.

Quoted 7 lines
Quoted 3 lines

So I introduced PIF_SYSCALL_RET_SET when converting s390 to generic entry. This flag tells the syscall code that a return value was set in ptregs and the syscall should be skipped.

You also could have added a 'syscall_ret' member to pt_regs, operate on that for the return values (seccomp, syscall...) and swap it into gprs[2] right before returning to user space.

That would likely also work, but I found it easier to read and understand to have an additional flag with a descriptive name than having yet another 'somehow-related-to-gpr2' member in ptregs.

maintainer
#9 lore raw
Quoted 28 lines · Sven Schnelle
Quoted 26 lines · Thomas Gleixner
Quoted 10 lines · Sven Schnelle
Quoted 4 lines · Thomas Gleixner

It's less than obvious and I have no objections to clean that up and make it more intuitive, but I still fail to see what Michal is actually trying to solve and what the magic flag is for. If s390 requires it, then that's an s390 problem, but definitely x86 does not.

The difference between x86 and s390 is that on s390, regs->gprs[2] is used for both the syscall number and the syscall return value. That was a design mistake early in the begin about 25 years ago, but it's ABI now, so it cannot be changed.

Cute.

Quoted 11 lines

When seccomp decides to skip a syscall, it write a return value into regs->gprs[2]. When syscall_enter_from_user_mode_work() returns, it returns this number. If it's negative all is good - the 'if (likely(nr < NR_syscalls))' conditiion would just catch it and skip the syscall.

But if it's a positive number, the code cannot distinguish whether that's a return value or a syscall number.

So I introduced PIF_SYSCALL_RET_SET when converting s390 to generic entry. This flag tells the syscall code that a return value was set in ptregs and the syscall should be skipped.

Quoted 7 lines
Quoted 3 lines

You also could have added a 'syscall_ret' member to pt_regs, operate on that for the return values (seccomp, syscall...) and swap it into gprs[2] right before returning to user space.

That would likely also work, but I found it easier to read and understand to have an additional flag with a descriptive name than having yet another 'somehow-related-to-gpr2' member in ptregs.

I find this very odd; I would think that having both syscall-nr and syscall-ret in separate (virtual) registers for most of the normal cycle would be most obvious and less surprising -- given that this is what all other architectures do.

Entry either grabs a copy of gpr2 and preserves it in orig_gpr2 as the syscall nr, or as Thomas suggests, you keep syscall_ret and copy that into gpr2 on return to userspace (and ptrace and signal and whatever other surface bits are affected).

Either way around you then have separate values for the entire range of at least the C part of the kernel syscall handling -- just like every other arch. How is munging things in a single value and a flag easier?

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

reviewer
#10 lore raw
Quoted 42 lines · Peter Zijlstra
Quoted 28 lines · Sven Schnelle
Quoted 26 lines · Thomas Gleixner
Quoted 10 lines · Sven Schnelle
Quoted 4 lines · Thomas Gleixner

It's less than obvious and I have no objections to clean that up and make it more intuitive, but I still fail to see what Michal is actually trying to solve and what the magic flag is for. If s390 requires it, then that's an s390 problem, but definitely x86 does not.

The difference between x86 and s390 is that on s390, regs->gprs[2] is used for both the syscall number and the syscall return value. That was a design mistake early in the begin about 25 years ago, but it's ABI now, so it cannot be changed.

Cute.

Quoted 11 lines

When seccomp decides to skip a syscall, it write a return value into regs->gprs[2]. When syscall_enter_from_user_mode_work() returns, it returns this number. If it's negative all is good - the 'if (likely(nr < NR_syscalls))' conditiion would just catch it and skip the syscall.

But if it's a positive number, the code cannot distinguish whether that's a return value or a syscall number.

So I introduced PIF_SYSCALL_RET_SET when converting s390 to generic entry. This flag tells the syscall code that a return value was set in ptregs and the syscall should be skipped.

Quoted 7 lines
Quoted 3 lines

You also could have added a 'syscall_ret' member to pt_regs, operate on that for the return values (seccomp, syscall...) and swap it into gprs[2] right before returning to user space.

That would likely also work, but I found it easier to read and understand to have an additional flag with a descriptive name than having yet another 'somehow-related-to-gpr2' member in ptregs.

I find this very odd; I would think that having both syscall-nr and syscall-ret in separate (virtual) registers for most of the normal cycle would be most obvious and less surprising -- given that this is what all other architectures do.

Quoted 8 lines

Entry either grabs a copy of gpr2 and preserves it in orig_gpr2 as the syscall nr, or as Thomas suggests, you keep syscall_ret and copy that into gpr2 on return to userspace (and ptrace and signal and whatever other surface bits are affected).

Either way around you then have separate values for the entire range of at least the C part of the kernel syscall handling -- just like every other arch. How is munging things in a single value and a flag easier?

Looks like we have different opinions on that - I find the flag way easier, and we don't need additional space for a long in ptregs and copy things around.

#11 lore raw
In reply to Peter Zijlstra
Quoted 42 lines · Peter Zijlstra
Quoted 28 lines · Sven Schnelle
Quoted 26 lines · Thomas Gleixner
Quoted 10 lines · Sven Schnelle
Quoted 4 lines · Thomas Gleixner

It's less than obvious and I have no objections to clean that up and make it more intuitive, but I still fail to see what Michal is actually trying to solve and what the magic flag is for. If s390 requires it, then that's an s390 problem, but definitely x86 does not.

The difference between x86 and s390 is that on s390, regs->gprs[2] is used for both the syscall number and the syscall return value. That was a design mistake early in the begin about 25 years ago, but it's ABI now, so it cannot be changed.

Cute.

Quoted 11 lines

When seccomp decides to skip a syscall, it write a return value into regs->gprs[2]. When syscall_enter_from_user_mode_work() returns, it returns this number. If it's negative all is good - the 'if (likely(nr < NR_syscalls))' conditiion would just catch it and skip the syscall.

But if it's a positive number, the code cannot distinguish whether that's a return value or a syscall number.

So I introduced PIF_SYSCALL_RET_SET when converting s390 to generic entry. This flag tells the syscall code that a return value was set in ptregs and the syscall should be skipped.

Quoted 7 lines
Quoted 3 lines

You also could have added a 'syscall_ret' member to pt_regs, operate on that for the return values (seccomp, syscall...) and swap it into gprs[2] right before returning to user space.

That would likely also work, but I found it easier to read and understand to have an additional flag with a descriptive name than having yet another 'somehow-related-to-gpr2' member in ptregs.

I find this very odd; I would think that having both syscall-nr and syscall-ret in separate (virtual) registers for most of the normal cycle would be most obvious and less surprising -- given that this is what all other architectures do.

Quoted 8 lines

Entry either grabs a copy of gpr2 and preserves it in orig_gpr2 as the syscall nr, or as Thomas suggests, you keep syscall_ret and copy that into gpr2 on return to userspace (and ptrace and signal and whatever other surface bits are affected).

Either way around you then have separate values for the entire range of at least the C part of the kernel syscall handling -- just like every other arch. How is munging things in a single value and a flag easier?

The same could be asked of syscall_enter_from_user_mode. I find it very odd. Why does it conflate the syscall number with its return value?

It never uses the syscall number passed in except when returning it unchanged. When it pokes the registers it reads the syscall number from them.

If the caller of syscall_enter_from_user_mode only read the syscall number from the registers when syscall_enter_from_user_mode returns and indicates the syscall should be still executed this whole shenigan would be avoided.

Thanks

Michal

reviewer
#12 lore raw
Quoted 10 lines · Michal Suchánek

The same could be asked of syscall_enter_from_user_mode. I find it very odd. Why does it conflate the syscall number with its return value?

It never uses the syscall number passed in except when returning it unchanged. When it pokes the registers it reads the syscall number from them.

If the caller of syscall_enter_from_user_mode only read the syscall number from the registers when syscall_enter_from_user_mode returns and indicates the syscall should be still executed this whole shenigan would

I agree. The fact that if (nr < NR_syscall) just works because -1 gets casted to 0xffffffff and is therefore out of bounds is very odd.

#13 lore raw
Quoted 9 lines · Sven Schnelle
Quoted 7 lines · Michal Suchánek

The same could be asked of syscall_enter_from_user_mode. I find it very odd. Why does it conflate the syscall number with its return value?

It never uses the syscall number passed in except when returning it unchanged. When it pokes the registers it reads the syscall number from them.

Quoted 7 lines
Quoted 3 lines

If the caller of syscall_enter_from_user_mode only read the syscall number from the registers when syscall_enter_from_user_mode returns and indicates the syscall should be still executed this whole shenigan would

I agree. The fact that if (nr < NR_syscall) just works because -1 gets casted to 0xffffffff and is therefore out of bounds is very odd.

Not at all strange. It is an *extremely* common construct in C, especially for range checking values into [0, n).

In addition to being idiomatic, keep in mind that this is one of the absolutely most performance critical paths in the entire kernel. One of the fundamental cornerstones behind Unix is to keep system calls cheap so that they can be simple building blocks for more complex operations. It is not the only possible design philosophy, but it is the one we chose to adopt, quite successfully.

The downside? Squeezing every possible cycle out of the system call path becomes one of the most essential tuning tasks. The good part is that keeping the system call path clean also makes it maintainable, even when there are quirks.

reviewer
#14 lore raw
Quoted 19 lines · H. Peter Anvin
Quoted 3 lines · Sven Schnelle
Quoted 1 line · Michal Suchánek

The same could be asked of syscall_enter_from_user_mode. I find it

very

Quoted 4 lines
Quoted 4 lines

odd. Why does it conflate the syscall number with its return value?

It never uses the syscall number passed in except when returning it unchanged. When it pokes the registers it reads the syscall number

from

Quoted 4 lines
Quoted 4 lines

them.

If the caller of syscall_enter_from_user_mode only read the syscall number from the registers when syscall_enter_from_user_mode returns

and

Quoted 1 line
Quoted 1 line

indicates the syscall should be still executed this whole shenigan

would

Quoted 1 line
Quoted 7 lines
Quoted 1 line

I agree. The fact that if (nr < NR_syscall) just works because -1

gets

Quoted 2 lines

casted to 0xffffffff and is therefore out of bounds is very odd.

Not at all strange. It is an *extremely* common construct in C, especially for range checking values into [0, n).

A clear indication that this is not as common as you think is that there's an extra comment in arch/x86/entry/syscall_64.c:

/*
* Convert negative numbers to very high and thus out of range
* numbers for comparisons.
*/

If everyone knows what this is the comment wouldn't be necessary. But that cast is not the thing i'm really interested in - if it stays that way, fine with me. But I would like to see the change from Michal going in which untangles the secure_computing() return value from the syscall number. Because this behaviour is very subtle and removing that would make things easier. (And also easier to read/audit, which is always importand with security related code, which seccomp/syscall clearly is).

Quoted 11 lines

In addition to being idiomatic, keep in mind that this is one of the absolutely most performance critical paths in the entire kernel. One of the fundamental cornerstones behind Unix is to keep system calls cheap so that they can be simple building blocks for more complex operations. It is not the only possible design philosophy, but it is the one we chose to adopt, quite successfully.

The downside? Squeezing every possible cycle out of the system call path becomes one of the most essential tuning tasks. The good part is that keeping the system call path clean also makes it maintainable, even when there are quirks.

I haven't measured it, but I doubt that the unsigned vs signed syscall bounds check makes a difference in real world scenarios. Even for ni_syscall cases it would be small. With the C entry code we have nowadays such optimizations should be left to the compiler. I wouldn't be surprised if the generated code is even the same.

But as written above, I don't really care about this.

#15 lore raw
In reply to Thomas Gleixner
Quoted 87 lines · Thomas Gleixner

On Wed, Jul 01 2026 at 11:29, H. Peter Anvin wrote:

Can you please trim your replies? Scrolling through hundred lines of useless quoted text is just annoying.

Quoted 8 lines · Michal Suchánek
Quoted 5 lines
include/linux/entry-common.h +1 -1
-static __always_inline long syscall_enter_from_user_mode(struct pt_regs *regs, long syscall)
+static __always_inline long syscall_enter_from_user_mode(struct pt_regs *regs, long *syscall)
{
	long ret;

1. The type for a system call is int.

That ship has sailed long ago. man syscall ...

Quoted 1 line

2. A valid system call number is always going to be positive.

That's true today.

Quoted 11 lines

3. Bits [30:24] are available for architecture ABI use. The "architecture independent" part of the system call number is therefore 24 bits wide.

4. The exact ABI is platform-specific, obviously, but as a general guideline (especially for new platforms/ABIs) should follow the rules for a platform "int" if practical. Notably, when passing a value in a register larger than 32 bits, which side of the calling interface is responsible for sign-extending a value passed in a register. If caller side, the kernel should validate, if callee side the kernel should ignore the additional bits and do the extension.

The kernel sign expands today already, i.e. for compat syscalls.

Quoted 3 lines

5. A negative system call number is guaranteed to return -ENOSYS (unless intercepted by seccomp, ptrace, or another mechanism under user space control.)

That's true today.

ASM entry:

      regs->eax = -ENOSYS;

C entry:

      nr = syscall_enter_from_user_mode(regs, nr);

      if ((unsigned)nr < SYSCALL_MAX)
      	    regs->eax = handle_syscall();
      else if (nr != -1)
      	    regs->eax = -ENOSYS;

      ....

If seccomp overwrites regs->eax and aborts any syscall (including -1) by returning -1, then the value seccomp wrote into regs->eax is preserved and returned to user space.

The same applies for syscall_user_dispatch() and ptrace...() if they decide to overwrite regs->eax _and_ abort the syscall by letting syscall_enter_from_user_mode() return -1.

trace_syscall_enter() is not any different. If the magic BPF in there rewrites the syscall number to -1 then either the original -ENOSYS or the BPF induced overwrite is returned to user space.

It's less than obvious and I have no objections to clean that up and make it more intuitive, but I still fail to see what Michal is actually trying to solve and what the magic flag is for. If s390 requires it, then that's an s390 problem, but definitely x86 does not.

Quoted 10 lines

6. If the platform needs to algorithmically modify the system call number due to platform-specific concerns (say, the platform uses a 16-bit special purpose register for the syscall number, or it has multiple kernel entry points with different behavior), it should if at all possible transcode the system call number as necessary to match this convention in APIs that are exposed to general kernel code.

For example, in the future I could very much see the IA32 code in the x86 kernel using bit 29 internally to indicate an ia32 system call, simplifying the is_compat implementation on x86.

I don't see how that makes it simpler. Those are two different entry code paths and magic bits wont make that go away.

Quoted 2 lines

It should not mean that passing bit 29 to either the syscall instruction or int $0x80 will be accepted.

Quoted 8 lines

Your proposal looks even more like a solution in search of a problem than the original one.

Thanks,

tglx

The type in syscall(3) is irrelevant. The argument passed to the kernel is treated as an int and sign-extended from 32 bits.

I'm explicitly not trying to invent things; I'm trying to document the status quo to avoid further confusion and to create mistakes.

I'm sorry I muddled the waters with what was intended to be a hypothetical example.

reviewer
#16 lore raw
In reply to Michal Suchánek
Quoted 11 lines · Michal Suchánek

The return value of syscall_enter_from_user_mode is used both for the adjusted syscall number and the indicator that a syscall should be skipped.

As seccomp can be invoked on any syscall, including invalid ones this somewhat undermines seccomp.

While the seccomp variants that terminate the process do not need to care about this for the filter that sets the syscall return value this disctinction is required.

Quoted 6 lines

Pass the syscall number as a pointer to the inline entry functions, and use the return value exclusively for the indication that the syscall is already handled.

This should avoid the need for the s390 PIF_SYSCALL_RET_SET which is the workaround for exactly this deficiency.

I'm not sure whether PIF_SYSCALL_RET_SET can be removed - the syscall return might still get set by PTRACE_SET_SYSCALL_INFO when the tracee is stopped. This might be a positive number which can't be distinguished from a syscall number. But maybe i'm missing something? It's been quite a while since I touched all that ptrace stuff.

#17 lore raw
Quoted 17 lines · Sven Schnelle
Quoted 15 lines · Michal Suchánek

The return value of syscall_enter_from_user_mode is used both for the adjusted syscall number and the indicator that a syscall should be skipped.

As seccomp can be invoked on any syscall, including invalid ones this somewhat undermines seccomp.

While the seccomp variants that terminate the process do not need to care about this for the filter that sets the syscall return value this disctinction is required.

Pass the syscall number as a pointer to the inline entry functions, and use the return value exclusively for the indication that the syscall is already handled.

Quoted 8 lines
Quoted 2 lines

This should avoid the need for the s390 PIF_SYSCALL_RET_SET which is the workaround for exactly this deficiency.

I'm not sure whether PIF_SYSCALL_RET_SET can be removed - the syscall return might still get set by PTRACE_SET_SYSCALL_INFO when the tracee is stopped. This might be a positive number which can't be distinguished from a syscall number. But maybe i'm missing something? It's been quite a while since I touched all that ptrace stuff.

When the syscall return value is set (in the registers) the return value which is also the modified syscall number is set to -1 indicating the syscall was handled. At least that's how the API is described.

So yes, if the syscall number range is restricted or the syscall number is returned through a path different from the function return value the flag should not be needed in the entry path because the case can be detected through the return value alone.

Thanks

Michal

reviewer
#18 lore raw
Quoted 27 lines · Michal Suchánek
Quoted 17 lines · Sven Schnelle
Quoted 15 lines · Michal Suchánek

The return value of syscall_enter_from_user_mode is used both for the adjusted syscall number and the indicator that a syscall should be skipped.

As seccomp can be invoked on any syscall, including invalid ones this somewhat undermines seccomp.

While the seccomp variants that terminate the process do not need to care about this for the filter that sets the syscall return value this disctinction is required.

Pass the syscall number as a pointer to the inline entry functions, and use the return value exclusively for the indication that the syscall is already handled.

Quoted 8 lines
Quoted 2 lines

This should avoid the need for the s390 PIF_SYSCALL_RET_SET which is the workaround for exactly this deficiency.

I'm not sure whether PIF_SYSCALL_RET_SET can be removed - the syscall return might still get set by PTRACE_SET_SYSCALL_INFO when the tracee is stopped. This might be a positive number which can't be distinguished from a syscall number. But maybe i'm missing something? It's been quite a while since I touched all that ptrace stuff.

Quoted 8 lines

When the syscall return value is set (in the registers) the return value which is also the modified syscall number is set to -1 indicating the syscall was handled. At least that's how the API is described.

So yes, if the syscall number range is restricted or the syscall number is returned through a path different from the function return value the flag should not be needed in the entry path because the case can be detected through the return value alone.

I'm still failing to see how this would work without an additional flag. Assume a program (the tracee) is stopped because of a syscall entry. The tracer then decides to skip the syscall and changes regs->gpr2 (which contains either the syscall number or return value) to contain 42. When the tracer than restarts the syscall, how does do_syscall() know that gpr2 is now a return value and not a syscall number?

#19 lore raw
Quoted 38 lines · Sven Schnelle
Quoted 27 lines · Michal Suchánek
Quoted 17 lines · Sven Schnelle
Quoted 15 lines · Michal Suchánek

The return value of syscall_enter_from_user_mode is used both for the adjusted syscall number and the indicator that a syscall should be skipped.

As seccomp can be invoked on any syscall, including invalid ones this somewhat undermines seccomp.

While the seccomp variants that terminate the process do not need to care about this for the filter that sets the syscall return value this disctinction is required.

Pass the syscall number as a pointer to the inline entry functions, and use the return value exclusively for the indication that the syscall is already handled.

Quoted 8 lines
Quoted 2 lines

This should avoid the need for the s390 PIF_SYSCALL_RET_SET which is the workaround for exactly this deficiency.

I'm not sure whether PIF_SYSCALL_RET_SET can be removed - the syscall return might still get set by PTRACE_SET_SYSCALL_INFO when the tracee is stopped. This might be a positive number which can't be distinguished from a syscall number. But maybe i'm missing something? It's been quite a while since I touched all that ptrace stuff.

Quoted 8 lines

When the syscall return value is set (in the registers) the return value which is also the modified syscall number is set to -1 indicating the syscall was handled. At least that's how the API is described.

So yes, if the syscall number range is restricted or the syscall number is returned through a path different from the function return value the flag should not be needed in the entry path because the case can be detected through the return value alone.

Quoted 6 lines

I'm still failing to see how this would work without an additional flag. Assume a program (the tracee) is stopped because of a syscall entry. The tracer then decides to skip the syscall and changes regs->gpr2 (which contains either the syscall number or return value) to contain 42. When the tracer than restarts the syscall, how does do_syscall() know that gpr2 is now a return value and not a syscall number?

Because then the return value from the syscall_enter_from_user_mode machinery would be -1 indicating the syscall should be skipped. That is how the return value of syscall_enter_from_user_mode is documented, I did not verify that it actually works that way for the tracing case on s390.

So long as it is clarified that -1 is not a syscall number or the syscall number is retuned elsewhere there is no doubt, the -1 indicates already handled syscall without the need for an additional flag.

Thanks

Michal

reviewer
#20 lore raw
Quoted 46 lines · Michal Suchánek
Quoted 38 lines · Sven Schnelle
Quoted 27 lines · Michal Suchánek
Quoted 17 lines · Sven Schnelle
Quoted 15 lines · Michal Suchánek

The return value of syscall_enter_from_user_mode is used both for the adjusted syscall number and the indicator that a syscall should be skipped.

As seccomp can be invoked on any syscall, including invalid ones this somewhat undermines seccomp.

While the seccomp variants that terminate the process do not need to care about this for the filter that sets the syscall return value this disctinction is required.

Pass the syscall number as a pointer to the inline entry functions, and use the return value exclusively for the indication that the syscall is already handled.

Quoted 8 lines
Quoted 2 lines

This should avoid the need for the s390 PIF_SYSCALL_RET_SET which is the workaround for exactly this deficiency.

I'm not sure whether PIF_SYSCALL_RET_SET can be removed - the syscall return might still get set by PTRACE_SET_SYSCALL_INFO when the tracee is stopped. This might be a positive number which can't be distinguished from a syscall number. But maybe i'm missing something? It's been quite a while since I touched all that ptrace stuff.

Quoted 8 lines

When the syscall return value is set (in the registers) the return value which is also the modified syscall number is set to -1 indicating the syscall was handled. At least that's how the API is described.

So yes, if the syscall number range is restricted or the syscall number is returned through a path different from the function return value the flag should not be needed in the entry path because the case can be detected through the return value alone.

Quoted 6 lines

I'm still failing to see how this would work without an additional flag. Assume a program (the tracee) is stopped because of a syscall entry. The tracer then decides to skip the syscall and changes regs->gpr2 (which contains either the syscall number or return value) to contain 42. When the tracer than restarts the syscall, how does do_syscall() know that gpr2 is now a return value and not a syscall number?

Quoted 5 lines

Because then the return value from the syscall_enter_from_user_mode machinery would be -1 indicating the syscall should be skipped. That is how the return value of syscall_enter_from_user_mode is documented, I did not verify that it actually works that way for the tracing case on s390.

I read the code and tested - I think I confused the way how syscall are intercepted by seccomp vs. ptrace. The PIF_SYSCALL_RET_SET is indeed only required to indicate syscalls skipped via seccomp and not ptrace.

maintainer
#21 lore raw
In reply to Michal Suchánek
Quoted 10 lines · Michal Suchánek

The return value of syscall_enter_from_user_mode is used both for the adjusted syscall number and the indicator that a syscall should be skipped.

As seccomp can be invoked on any syscall, including invalid ones this somewhat undermines seccomp.

While the seccomp variants that terminate the process do not need to care about this for the filter that sets the syscall return value this disctinction is required.

You completely fail to explain why and what actual problem you are trying to solve. At least I can't figure it out from the above word salad.

Quoted 10 lines

Pass the syscall number as a pointer to the inline entry functions, and use the return value exclusively for the indication that the syscall is already handled.

This should avoid the need for the s390 PIF_SYSCALL_RET_SET which is the workaround for exactly this deficiency.

If this is desirable the patch could be split into some series that adjusts the code flow where needed so that the final change is mostly mechanical.

That's not a matter of desire. That's mandatory.

Documentation/core-api/entry.rst invoked from low-level assembly code looks like this: · line 73

Quoted 6 lines
Documentation/core-api/entry.rst +2 -4
-	instrumentation_begin();
-	if (!invoke_syscall(regs, nr) && nr != -1)
-	 	result_reg(regs) = __sys_ni_syscall(regs);
-	instrumentation_end();
+	/* Skip syscall when -1 is returned */
+	if (!syscall_enter_from_user_mode(regs, &nr)) {

Seriously?

If we go and separate the syscall number from the return value, then the return value 0 means success and anything else fail. Which in other words is a boolean. So instead of tastelessly adding a completely nonsensical comment about -1 here, syscall_enter_from_user_mode() wants to have the return value type bool with a proper boolean logic: true = success, false = abort.

arch/x86/entry/syscall_32.c __visible noinstr void do_int80_emulation(struct pt_regs *regs) · line 171

Quoted 7 lines
arch/x86/entry/syscall_32.c +1 -2
@@ -168,8 +168,7 @@ __visible noinstr void do_int80_emulation(struct pt_regs *regs)
 	nr = syscall_32_enter(regs);
 
 	local_irq_enable();
-	nr = syscall_enter_from_user_mode_work(regs, nr);
-	do_syscall_32_irqs_on(regs, nr);
+	syscall_enter_from_user_mode_work(regs, &nr);

How exactly is this ever going to invoke a valid syscall?

arch/x86/entry/syscall_32.c DEFINE_FREDENTRY_RAW(int80_emulation) · line 236

Quoted 3 lines
arch/x86/entry/syscall_32.c +3 -0
+	if (!syscall_enter_from_user_mode_work(regs, &nr)) {
+		nr &= GENMASK(31, 0);
+		do_syscall_32_irqs_on(regs, nr);
  do_syscall_32_irqs_on(regs, (int)nr);

would be too simple, right?

Thanks,

tglx

#22 lore raw
Quoted 8 lines · Thomas Gleixner
Quoted 7 lines · Michal Suchánek

The return value of syscall_enter_from_user_mode is used both for the adjusted syscall number and the indicator that a syscall should be skipped.

As seccomp can be invoked on any syscall, including invalid ones this somewhat undermines seccomp.

Quoted 7 lines
Quoted 3 lines

While the seccomp variants that terminate the process do not need to care about this for the filter that sets the syscall return value this disctinction is required.

You completely fail to explain why and what actual problem you are trying to solve. At least I can't figure it out from the above word salad.

syscall_enter_from_user_mode returns the new syscall number after doing something arbitrarry with it, including running seccomp.

Wehn the syscall is already handled, eg. by seccomp filtering it returns -1 as the new syscall number. -1 is an invalid syscall number but it can still be filtered by seccomp. When the syscall number was -1 to start with it's not possible to determine if the syscall was fileterd from the return value. s390 returns the filtered state in a flag it sets on the regs structure, avoiding this problem.

However, the API should be specified in a way that does not require everyone implementing such flag.

Quoted 5 lines
Quoted 5 lines

Pass the syscall number as a pointer to the inline entry functions, and use the return value exclusively for the indication that the syscall is already handled.

Quoted 8 lines
Quoted 6 lines

This should avoid the need for the s390 PIF_SYSCALL_RET_SET which is the workaround for exactly this deficiency.

If this is desirable the patch could be split into some series that adjusts the code flow where needed so that the final change is mostly mechanical.

That's not a matter of desire. That's mandatory.

So long as it's desirable to implement an API change in this direction, it's not clear to me so far.

Quoted 9 lines

Documentation/core-api/entry.rst invoked from low-level assembly code looks like this: · line 73

Quoted 6 lines
Documentation/core-api/entry.rst +2 -4
-	instrumentation_begin();
-	if (!invoke_syscall(regs, nr) && nr != -1)
-	 	result_reg(regs) = __sys_ni_syscall(regs);
-	instrumentation_end();
+	/* Skip syscall when -1 is returned */
+	if (!syscall_enter_from_user_mode(regs, &nr)) {

Seriously?

Quoted 6 lines

If we go and separate the syscall number from the return value, then the return value 0 means success and anything else fail. Which in other words is a boolean. So instead of tastelessly adding a completely nonsensical comment about -1 here, syscall_enter_from_user_mode() wants to have the return value type bool with a proper boolean logic: true = success, false = abort.

We have that very same API down to __secure_computing() which returns boolean represented as -1 and 0 values. That does not mean it's not tasteless.

Quoted 10 lines

arch/x86/entry/syscall_32.c __visible noinstr void do_int80_emulation(struct pt_regs *regs) · line 171

Quoted 8 lines
arch/x86/entry/syscall_32.c +1 -2
@@ -168,8 +168,7 @@ __visible noinstr void do_int80_emulation(struct pt_regs *regs)
 	nr = syscall_32_enter(regs);
 
 	local_irq_enable();
-	nr = syscall_enter_from_user_mode_work(regs, nr);
-	do_syscall_32_irqs_on(regs, nr);
+	syscall_enter_from_user_mode_work(regs, &nr);

How exactly is this ever going to invoke a valid syscall?

That's one of the problems with giant all-in-one patch, things like this easily slip in. However, it is in cluded mostly for illustration, I don't expect anyone to merge this as-is.

Quoted 8 lines

arch/x86/entry/syscall_32.c DEFINE_FREDENTRY_RAW(int80_emulation) · line 236

Quoted 4 lines
arch/x86/entry/syscall_32.c +3 -0
+	if (!syscall_enter_from_user_mode_work(regs, &nr)) {
+		nr &= GENMASK(31, 0);
+		do_syscall_32_irqs_on(regs, nr);
  do_syscall_32_irqs_on(regs, (int)nr);

would be too simple, right?

Also way less explicit.

Thanks

Michal

maintainer
#23 lore raw
Quoted 17 lines · Michal Suchánek
Quoted 8 lines · Thomas Gleixner
Quoted 7 lines · Michal Suchánek

The return value of syscall_enter_from_user_mode is used both for the adjusted syscall number and the indicator that a syscall should be skipped.

As seccomp can be invoked on any syscall, including invalid ones this somewhat undermines seccomp.

Quoted 7 lines
Quoted 3 lines

While the seccomp variants that terminate the process do not need to care about this for the filter that sets the syscall return value this disctinction is required.

You completely fail to explain why and what actual problem you are trying to solve. At least I can't figure it out from the above word salad.

Quoted 6 lines

syscall_enter_from_user_mode returns the new syscall number after doing something arbitrarry with it, including running seccomp.

Wehn the syscall is already handled, eg. by seccomp filtering it returns -1 as the new syscall number. -1 is an invalid syscall number but it can still be filtered by seccomp.

Once syscall_enter_from_user_mode() returns -1 nothing can filter it anymore.

Quoted 4 lines

When the syscall number was -1 to start with it's not possible to determine if the syscall was fileterd from the return value. s390 returns the filtered state in a flag it sets on the regs structure, avoiding this problem.

What needs to determine whether the syscall was filtered or not?

Quoted 2 lines

However, the API should be specified in a way that does not require everyone implementing such flag.

Which exact problem does the flag solve?

Quoted 16 lines
Quoted 9 lines

Documentation/core-api/entry.rst invoked from low-level assembly code looks like this: · line 73

Quoted 6 lines
Documentation/core-api/entry.rst +2 -4
-	instrumentation_begin();
-	if (!invoke_syscall(regs, nr) && nr != -1)
-	 	result_reg(regs) = __sys_ni_syscall(regs);
-	instrumentation_end();
+	/* Skip syscall when -1 is returned */
+	if (!syscall_enter_from_user_mode(regs, &nr)) {

Seriously?

Quoted 6 lines

If we go and separate the syscall number from the return value, then the return value 0 means success and anything else fail. Which in other words is a boolean. So instead of tastelessly adding a completely nonsensical comment about -1 here, syscall_enter_from_user_mode() wants to have the return value type bool with a proper boolean logic: true = success, false = abort.

Quoted 3 lines

We have that very same API down to __secure_computing() which returns boolean represented as -1 and 0 values. That does not mean it's not tasteless.

Hahahahaha.

We have a lot of functions which have a boolean return value but a int/long return type for historical reasons.

We've added bool because it's not ambiguous and allows the compiler to optimize better. It also makes the code more clear. Modern code uses a non-boolean return type only when there is an actual reason for it, e.g. propagating an error code all the way back through the call chain. The historical 0=success <0 = errorcode model really want's to be restricted to such cases.

Just for the record:

https://lore.kernel.org/all/[email protected]/
https://lore.kernel.org/all/[email protected]/

So I stand with my comment that it is sloppy and tasteless to slap an argument into a pile of functions, claim separation of return value and syscall number and leave the return value in an ill-defined state.

Quoted 10 lines
Quoted 9 lines

arch/x86/entry/syscall_32.c __visible noinstr void do_int80_emulation(struct pt_regs *regs) · line 171

Quoted 7 lines
arch/x86/entry/syscall_32.c +1 -2
@@ -168,8 +168,7 @@ __visible noinstr void do_int80_emulation(struct pt_regs *regs)
 	nr = syscall_32_enter(regs);
 
 	local_irq_enable();
-	nr = syscall_enter_from_user_mode_work(regs, nr);
-	do_syscall_32_irqs_on(regs, nr);
+	syscall_enter_from_user_mode_work(regs, &nr);

How exactly is this ever going to invoke a valid syscall?

Quoted 3 lines

That's one of the problems with giant all-in-one patch, things like this easily slip in. However, it is in cluded mostly for illustration, I don't expect anyone to merge this as-is.

It's a problem with hastily cobbled together slop. Even RFC patches should at least be functional.

Quoted 9 lines
Quoted 7 lines

arch/x86/entry/syscall_32.c DEFINE_FREDENTRY_RAW(int80_emulation) · line 236

Quoted 3 lines
arch/x86/entry/syscall_32.c +3 -0
+	if (!syscall_enter_from_user_mode_work(regs, &nr)) {
+		nr &= GENMASK(31, 0);
+		do_syscall_32_irqs_on(regs, nr);
  do_syscall_32_irqs_on(regs, (int)nr);

would be too simple, right?

Also way less explicit.

Now you care about explicit, but the return value mess can be left ambiguous, right?

Aside of that, the cast is very much explicit for people who can read C.

It would be great if you could sit back and come up with a very explicit and comprehensible explanation for the problem you are trying to solve.

Thanks,

tglx

#24 lore raw
Quoted 28 lines · Thomas Gleixner
Quoted 17 lines · Michal Suchánek
Quoted 8 lines · Thomas Gleixner
Quoted 7 lines · Michal Suchánek

The return value of syscall_enter_from_user_mode is used both for the adjusted syscall number and the indicator that a syscall should be skipped.

As seccomp can be invoked on any syscall, including invalid ones this somewhat undermines seccomp.

Quoted 7 lines
Quoted 3 lines

While the seccomp variants that terminate the process do not need to care about this for the filter that sets the syscall return value this disctinction is required.

You completely fail to explain why and what actual problem you are trying to solve. At least I can't figure it out from the above word salad.

Quoted 6 lines

syscall_enter_from_user_mode returns the new syscall number after doing something arbitrarry with it, including running seccomp.

Wehn the syscall is already handled, eg. by seccomp filtering it returns -1 as the new syscall number. -1 is an invalid syscall number but it can still be filtered by seccomp.

Once syscall_enter_from_user_mode() returns -1 nothing can filter it anymore.

Quoted 6 lines
Quoted 4 lines

When the syscall number was -1 to start with it's not possible to determine if the syscall was fileterd from the return value. s390 returns the filtered state in a flag it sets on the regs structure, avoiding this problem.

What needs to determine whether the syscall was filtered or not?

The code that executes syscall_enter_from_user_mode() needs to determine that.

After syscall_enter_from_user_mode() returns the syscall needs to be executed or skipped.

'Executing' an invalid syscall boils down to setting the return value to -ENOSYS.

But if the syscall number returned is -1 was the syscall filtered and the return value set by syscall_enter_from_user_mode() or should it be set by the caller to -ENOSYS?

Quoted 5 lines
Quoted 3 lines

However, the API should be specified in a way that does not require everyone implementing such flag.

Which exact problem does the flag solve?

To be able to tell if the syscall was handled or no, the return value from syscall_enter_from_user_mode() is inconclusive.

That's what the flag is for. To be able to tell if the syscall was handled without relying on the ambiguous return value of syscall_enter_from_user_mode().

Thanks

Michal

maintainer
#25 lore raw
Quoted 14 lines · Michal Suchánek
Quoted 6 lines · Thomas Gleixner
Quoted 4 lines

When the syscall number was -1 to start with it's not possible to determine if the syscall was fileterd from the return value. s390 returns the filtered state in a flag it sets on the regs structure, avoiding this problem.

What needs to determine whether the syscall was filtered or not?

The code that executes syscall_enter_from_user_mode() needs to determine that.

After syscall_enter_from_user_mode() returns the syscall needs to be executed or skipped.

Quoted 6 lines

'Executing' an invalid syscall boils down to setting the return value to -ENOSYS.

But if the syscall number returned is -1 was the syscall filtered and the return value set by syscall_enter_from_user_mode() or should it be set by the caller to -ENOSYS?

See the explanation I gave to Peter. It's conclusive, but admittedly not intuitive.

The fact, that s390 decided to have the syscall number and the return value in the same register is a different problem, but as I pointed out to Sven it could have been solved completely without that flag.

Thanks,

tglx

maintainer
#26 lore raw
In reply to Michal Suchánek

include/linux/entry-common.h static __always_inline long syscall_trace_enter(struct pt_regs *regs, unsigned l · line 112

Quoted 10 lines · Michal Suchánek
include/linux/entry-common.h +3 -3
 	if (unlikely(work & SYSCALL_WORK_SYSCALL_TRACEPOINT))
-		syscall = trace_syscall_enter(regs, syscall);
+		*syscall = trace_syscall_enter(regs, *syscall);
 
-	syscall_enter_audit(regs, syscall);
+	syscall_enter_audit(regs, *syscall);
 
-	return ret ? : syscall;
+	return 0;

That breaks trace_syscall_enter() because the probes/BPF muck attached ot that can:

1) set the return code
2) set syscall to -1L

That works correctly today, but with your sloppy hackery the low level x86 code falls into sys_ni_syscall() which sets the return code to -ENOSYS.