Skip to contentSkip to conversation

powerpc/syscall: Fix seccomp errno handling with GENERIC_ENTRY

PATCH Superseded Mukesh Kumar Chaurasiya (IBM) 4 participants 12 messages Jun 24 – Jun 29 PowerPC

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 powerpc/syscall: Fix seccomp errno handling with GENERIC_ENTRY

After enabling GENERIC_ENTRY on PowerPC, seccomp filters using SCMP_ACT_ERRNO without an explicit errnoRet value return ENOSYS (Function not implemented) instead of the expected EPERM (Operation not permitted).

The issue occurs in system_call_exception() when syscall_enter_from_user_mode() returns -1 to indicate the syscall should be skipped (e.g., blocked by seccomp). The current code treats this -1 as a syscall number and compares it against NR_syscalls. Since -1 (when cast to unsigned long) is greater than NR_syscalls, the code incorrectly returns -ENOSYS, overwriting the errno that seccomp already set via syscall_set_return_value().

The generic entry code in syscall_trace_enter() calls __secure_computing(), which sets the appropriate errno in regs->gpr[3] and returns -1 to signal that the syscall should be skipped. However, the PowerPC syscall handler was not checking for this -1 return value before validating the syscall number.

Fix this by explicitly checking if syscall_enter_from_user_mode() returns -1 and returning the value already set in regs->gpr[3] (the errno from seccomp) before performing the syscall number validation.

This aligns PowerPC's behavior with other architectures using GENERIC_ENTRY and restores correct seccomp errno handling.

Fixes bee25f97ad24 ("powerpc: Enable GENERIC_ENTRY feature")
---
 arch/powerpc/kernel/syscall.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/powerpc/kernel/syscall.c +4 -0
index a9da2af6efa8..5b58c8d396c8 100644
@@ -22,6 +22,10 @@ notrace long system_call_exception(struct pt_regs *regs, unsigned long r0)
 	add_random_kstack_offset();
 	r0 = syscall_enter_from_user_mode(regs, r0);
 
+	/* Seccomp or ptrace may have set return value, skip syscall */
+	if (unlikely(r0 == -1L))
+		return regs->gpr[3];
+
 	if (unlikely(r0 >= NR_syscalls)) {
 		if (unlikely(trap_is_unsupported_scv(regs))) {
 			/* Unsupported scv vector */
Signature · 1 line
2.54.0
Tested-by on v1 1/1
#1 lore raw
Quoted 22 lines · Mukesh Kumar Chaurasiya (IBM)

After enabling GENERIC_ENTRY on PowerPC, seccomp filters using SCMP_ACT_ERRNO without an explicit errnoRet value return ENOSYS (Function not implemented) instead of the expected EPERM (Operation not permitted).

The issue occurs in system_call_exception() when syscall_enter_from_user_mode() returns -1 to indicate the syscall should be skipped (e.g., blocked by seccomp). The current code treats this -1 as a syscall number and compares it against NR_syscalls. Since -1 (when cast to unsigned long) is greater than NR_syscalls, the code incorrectly returns -ENOSYS, overwriting the errno that seccomp already set via syscall_set_return_value().

The generic entry code in syscall_trace_enter() calls __secure_computing(), which sets the appropriate errno in regs->gpr[3] and returns -1 to signal that the syscall should be skipped. However, the PowerPC syscall handler was not checking for this -1 return value before validating the syscall number.

Fix this by explicitly checking if syscall_enter_from_user_mode() returns -1 and returning the value already set in regs->gpr[3] (the errno from seccomp) before performing the syscall number validation.

Quoted 5 lines

This aligns PowerPC's behavior with other architectures using GENERIC_ENTRY and restores correct seccomp errno handling.

Fixes bee25f97ad24 ("powerpc: Enable GENERIC_ENTRY feature")

Thanks

Michal

Quoted 23 lines
---
 arch/powerpc/kernel/syscall.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/powerpc/kernel/syscall.c +4 -0
index a9da2af6efa8..5b58c8d396c8 100644
@@ -22,6 +22,10 @@ notrace long system_call_exception(struct pt_regs *regs, unsigned long r0)
 	add_random_kstack_offset();
 	r0 = syscall_enter_from_user_mode(regs, r0);
 
+	/* Seccomp or ptrace may have set return value, skip syscall */
+	if (unlikely(r0 == -1L))
+		return regs->gpr[3];
+
 	if (unlikely(r0 >= NR_syscalls)) {
 		if (unlikely(trap_is_unsupported_scv(regs))) {
 			/* Unsupported scv vector */
Signature · 1 line
2.54.0
#2 lore raw
In reply to Mukesh Kumar Chaurasiya (IBM)

Hello,

Quoted 37 lines · Mukesh Kumar Chaurasiya (IBM)

After enabling GENERIC_ENTRY on PowerPC, seccomp filters using SCMP_ACT_ERRNO without an explicit errnoRet value return ENOSYS (Function not implemented) instead of the expected EPERM (Operation not permitted).

The issue occurs in system_call_exception() when syscall_enter_from_user_mode() returns -1 to indicate the syscall should be skipped (e.g., blocked by seccomp). The current code treats this -1 as a syscall number and compares it against NR_syscalls. Since -1 (when cast to unsigned long) is greater than NR_syscalls, the code incorrectly returns -ENOSYS, overwriting the errno that seccomp already set via syscall_set_return_value().

The generic entry code in syscall_trace_enter() calls __secure_computing(), which sets the appropriate errno in regs->gpr[3] and returns -1 to signal that the syscall should be skipped. However, the PowerPC syscall handler was not checking for this -1 return value before validating the syscall number.

Fix this by explicitly checking if syscall_enter_from_user_mode() returns -1 and returning the value already set in regs->gpr[3] (the errno from seccomp) before performing the syscall number validation.

This aligns PowerPC's behavior with other architectures using GENERIC_ENTRY and restores correct seccomp errno handling.

Fixes bee25f97ad24 ("powerpc: Enable GENERIC_ENTRY feature")
---
 arch/powerpc/kernel/syscall.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/powerpc/kernel/syscall.c +0 -0
index a9da2af6efa8..5b58c8d396c8 100644

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

Quoted 6 lines
arch/powerpc/kernel/syscall.c +3 -0
@@ -22,6 +22,10 @@ notrace long system_call_exception(struct pt_regs *regs, unsigned long r0)
 	add_random_kstack_offset();
 	r0 = syscall_enter_from_user_mode(regs, r0);
 
+	/* Seccomp or ptrace may have set return value, skip syscall */
+	if (unlikely(r0 == -1L))
+		return regs->gpr[3];

this should use syscall_get_error instead of accessing the register value directly. The error is represented differently in the scv and non-scv case, and for non-scv case this will return wrong value.

Thanks

Michal

Quoted 7 lines
arch/powerpc/kernel/syscall.c +1 -0
+
 	if (unlikely(r0 >= NR_syscalls)) {
 		if (unlikely(trap_is_unsupported_scv(regs))) {
 			/* Unsupported scv vector */
Signature · 1 line
2.54.0
#3 lore raw
Quoted 51 lines · Michal Suchánek

Hello,

Quoted 37 lines · Mukesh Kumar Chaurasiya (IBM)

After enabling GENERIC_ENTRY on PowerPC, seccomp filters using SCMP_ACT_ERRNO without an explicit errnoRet value return ENOSYS (Function not implemented) instead of the expected EPERM (Operation not permitted).

The issue occurs in system_call_exception() when syscall_enter_from_user_mode() returns -1 to indicate the syscall should be skipped (e.g., blocked by seccomp). The current code treats this -1 as a syscall number and compares it against NR_syscalls. Since -1 (when cast to unsigned long) is greater than NR_syscalls, the code incorrectly returns -ENOSYS, overwriting the errno that seccomp already set via syscall_set_return_value().

The generic entry code in syscall_trace_enter() calls __secure_computing(), which sets the appropriate errno in regs->gpr[3] and returns -1 to signal that the syscall should be skipped. However, the PowerPC syscall handler was not checking for this -1 return value before validating the syscall number.

Fix this by explicitly checking if syscall_enter_from_user_mode() returns -1 and returning the value already set in regs->gpr[3] (the errno from seccomp) before performing the syscall number validation.

This aligns PowerPC's behavior with other architectures using GENERIC_ENTRY and restores correct seccomp errno handling.

Fixes bee25f97ad24 ("powerpc: Enable GENERIC_ENTRY feature")
---
 arch/powerpc/kernel/syscall.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/powerpc/kernel/syscall.c +0 -0
index a9da2af6efa8..5b58c8d396c8 100644

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

Quoted 6 lines
arch/powerpc/kernel/syscall.c +3 -0
@@ -22,6 +22,10 @@ notrace long system_call_exception(struct pt_regs *regs, unsigned long r0)
 	add_random_kstack_offset();
 	r0 = syscall_enter_from_user_mode(regs, r0);
 
+	/* Seccomp or ptrace may have set return value, skip syscall */
+	if (unlikely(r0 == -1L))
+		return regs->gpr[3];

this should use syscall_get_error instead of accessing the register value directly. The error is represented differently in the scv and non-scv case, and for non-scv case this will return wrong value.

Quoted 4 lines

Thanks

Michal

Yeah, Thanks.

Will send out a new version.

Regards,
Mukesh

Quoted 7 lines
Quoted 7 lines
arch/powerpc/kernel/syscall.c +1 -0
+
 	if (unlikely(r0 >= NR_syscalls)) {
 		if (unlikely(trap_is_unsupported_scv(regs))) {
 			/* Unsupported scv vector */
Signature · 1 line
2.54.0
#4 lore raw
In reply to Mukesh Kumar Chaurasiya (IBM)
Quoted 4 lines · Mukesh Kumar Chaurasiya (IBM)

After enabling GENERIC_ENTRY on PowerPC, seccomp filters using SCMP_ACT_ERRNO without an explicit errnoRet value return ENOSYS (Function not implemented) instead of the expected EPERM (Operation not permitted).

Was this noticed by the seccomp_bpf kselftest? (If not, I'd love a regression test added for this case...)

-Kees

Signature · 1 line
Kees Cook
#5 lore raw

Le 26/06/2026 à 09:50, Kees Cook a écrit :

Quoted 9 lines · Mukesh Kumar Chaurasiya (IBM)
Quoted 4 lines

After enabling GENERIC_ENTRY on PowerPC, seccomp filters using SCMP_ACT_ERRNO without an explicit errnoRet value return ENOSYS (Function not implemented) instead of the expected EPERM (Operation not permitted).

Was this noticed by the seccomp_bpf kselftest? (If not, I'd love a regression test added for this case...)

Was reported here:

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

#6 lore raw
In reply to Mukesh Kumar Chaurasiya (IBM)

Le 24/06/2026 à 19:15, Mukesh Kumar Chaurasiya (IBM) a écrit :

Quoted 22 lines

After enabling GENERIC_ENTRY on PowerPC, seccomp filters using SCMP_ACT_ERRNO without an explicit errnoRet value return ENOSYS (Function not implemented) instead of the expected EPERM (Operation not permitted).

The issue occurs in system_call_exception() when syscall_enter_from_user_mode() returns -1 to indicate the syscall should be skipped (e.g., blocked by seccomp). The current code treats this -1 as a syscall number and compares it against NR_syscalls. Since -1 (when cast to unsigned long) is greater than NR_syscalls, the code incorrectly returns -ENOSYS, overwriting the errno that seccomp already set via syscall_set_return_value().

The generic entry code in syscall_trace_enter() calls __secure_computing(), which sets the appropriate errno in regs->gpr[3] and returns -1 to signal that the syscall should be skipped. However, the PowerPC syscall handler was not checking for this -1 return value before validating the syscall number.

Fix this by explicitly checking if syscall_enter_from_user_mode() returns -1 and returning the value already set in regs->gpr[3] (the errno from seccomp) before performing the syscall number validation.

Quoted 5 lines

This aligns PowerPC's behavior with other architectures using GENERIC_ENTRY and restores correct seccomp errno handling.

Fixes bee25f97ad24 ("powerpc: Enable GENERIC_ENTRY feature")
Quoted 10 lines
---
  arch/powerpc/kernel/syscall.c | 4 ++++
  1 file changed, 4 insertions(+)
arch/powerpc/kernel/syscall.c +0 -0
index a9da2af6efa8..5b58c8d396c8 100644

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

Quoted 5 lines
arch/powerpc/kernel/syscall.c +2 -0
@@ -22,6 +22,10 @@ notrace long system_call_exception(struct pt_regs *regs, unsigned long r0)
  	add_random_kstack_offset();
  	r0 = syscall_enter_from_user_mode(regs, r0);
 
+	/* Seccomp or ptrace may have set return value, skip syscall */
+	if (unlikely(r0 == -1L))

Is it really needed to add the L after 1 ?

Quoted 5 lines
arch/powerpc/kernel/syscall.c +2 -0
+		return regs->gpr[3];
+
  	if (unlikely(r0 >= NR_syscalls)) {
  		if (unlikely(trap_is_unsupported_scv(regs))) {
  			/* Unsupported scv vector */
#7 lore raw
Quoted 38 lines · Christophe Leroy (CS GROUP)

Le 24/06/2026 à 19:15, Mukesh Kumar Chaurasiya (IBM) a écrit :

Quoted 22 lines

After enabling GENERIC_ENTRY on PowerPC, seccomp filters using SCMP_ACT_ERRNO without an explicit errnoRet value return ENOSYS (Function not implemented) instead of the expected EPERM (Operation not permitted).

The issue occurs in system_call_exception() when syscall_enter_from_user_mode() returns -1 to indicate the syscall should be skipped (e.g., blocked by seccomp). The current code treats this -1 as a syscall number and compares it against NR_syscalls. Since -1 (when cast to unsigned long) is greater than NR_syscalls, the code incorrectly returns -ENOSYS, overwriting the errno that seccomp already set via syscall_set_return_value().

The generic entry code in syscall_trace_enter() calls __secure_computing(), which sets the appropriate errno in regs->gpr[3] and returns -1 to signal that the syscall should be skipped. However, the PowerPC syscall handler was not checking for this -1 return value before validating the syscall number.

Fix this by explicitly checking if syscall_enter_from_user_mode() returns -1 and returning the value already set in regs->gpr[3] (the errno from seccomp) before performing the syscall number validation.

Quoted 5 lines

This aligns PowerPC's behavior with other architectures using GENERIC_ENTRY and restores correct seccomp errno handling.

Fixes bee25f97ad24 ("powerpc: Enable GENERIC_ENTRY feature")
Quoted 5 lines
---
  arch/powerpc/kernel/syscall.c | 4 ++++
  1 file changed, 4 insertions(+)
Quoted 12 lines

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

Quoted 9 lines
arch/powerpc/kernel/syscall.c +2 -0
index a9da2af6efa8..5b58c8d396c8 100644
@@ -22,6 +22,10 @@ notrace long system_call_exception(struct pt_regs *regs, unsigned long r0)
  	add_random_kstack_offset();
  	r0 = syscall_enter_from_user_mode(regs, r0);
+	/* Seccomp or ptrace may have set return value, skip syscall */
+	if (unlikely(r0 == -1L))

Is it really needed to add the L after 1 ?

I guess it's not needed, but syscall_trace_enter is returning -1L, and i wanted to keep it consistent to that.

Regards,
Mukesh

Quoted 6 lines
Quoted 5 lines
arch/powerpc/kernel/syscall.c +2 -0
+		return regs->gpr[3];
+
  	if (unlikely(r0 >= NR_syscalls)) {
  		if (unlikely(trap_is_unsupported_scv(regs))) {
  			/* Unsupported scv vector */
#8 lore raw
In reply to Mukesh Kumar Chaurasiya (IBM)

Hello,

there is yet another bug identified.

When the initial syscall number is -1 the new condition bypasses setting the ENOSYS below in if (unlikely(r0 >= NR_syscalls)) and returns 0.

perl -MPOSIX -e '$!=0; my $r = syscall(-1, 0); print "ret=$r errno=".($!+0)." ($!)\n"'

Normally the result is

ret=-1 errno=38 (Function not implemented)

but with this patch the result is

ret=0 errno=0 ()

fixup below.

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

Quoted 37 lines · Mukesh Kumar Chaurasiya (IBM)

After enabling GENERIC_ENTRY on PowerPC, seccomp filters using SCMP_ACT_ERRNO without an explicit errnoRet value return ENOSYS (Function not implemented) instead of the expected EPERM (Operation not permitted).

The issue occurs in system_call_exception() when syscall_enter_from_user_mode() returns -1 to indicate the syscall should be skipped (e.g., blocked by seccomp). The current code treats this -1 as a syscall number and compares it against NR_syscalls. Since -1 (when cast to unsigned long) is greater than NR_syscalls, the code incorrectly returns -ENOSYS, overwriting the errno that seccomp already set via syscall_set_return_value().

The generic entry code in syscall_trace_enter() calls __secure_computing(), which sets the appropriate errno in regs->gpr[3] and returns -1 to signal that the syscall should be skipped. However, the PowerPC syscall handler was not checking for this -1 return value before validating the syscall number.

Fix this by explicitly checking if syscall_enter_from_user_mode() returns -1 and returning the value already set in regs->gpr[3] (the errno from seccomp) before performing the syscall number validation.

This aligns PowerPC's behavior with other architectures using GENERIC_ENTRY and restores correct seccomp errno handling.

Fixes bee25f97ad24 ("powerpc: Enable GENERIC_ENTRY feature")
---
 arch/powerpc/kernel/syscall.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/powerpc/kernel/syscall.c +0 -0
index a9da2af6efa8..5b58c8d396c8 100644
@@ -22,6 +22,10 @@ notrace long system_call_exception(struct pt_regs *regs, unsigned long r0)
	unsigned long r0_initial = r0;

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

Quoted 5 lines
arch/powerpc/kernel/syscall.c +2 -0
 	add_random_kstack_offset();
 	r0 = syscall_enter_from_user_mode(regs, r0);
 
+	/* Seccomp or ptrace may have set return value, skip syscall */
+	if (unlikely(r0 == -1L)
				&& (r0_initial != -1L))

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

Quoted 5 lines
arch/powerpc/kernel/syscall.c +2 -0
+		return regs->gpr[3];
+
 	if (unlikely(r0 >= NR_syscalls)) {
 		if (unlikely(trap_is_unsupported_scv(regs))) {
 			/* Unsupported scv vector */

Thanks

Michal

#9 lore raw
Quoted 72 lines · Michal Suchánek

Hello,

there is yet another bug identified.

When the initial syscall number is -1 the new condition bypasses setting the ENOSYS below in if (unlikely(r0 >= NR_syscalls)) and returns 0.

perl -MPOSIX -e '$!=0; my $r = syscall(-1, 0); print "ret=$r errno=".($!+0)." ($!)\n"'

Normally the result is

ret=-1 errno=38 (Function not implemented)

but with this patch the result is

ret=0 errno=0 ()

fixup below.

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

Quoted 37 lines · Mukesh Kumar Chaurasiya (IBM)

After enabling GENERIC_ENTRY on PowerPC, seccomp filters using SCMP_ACT_ERRNO without an explicit errnoRet value return ENOSYS (Function not implemented) instead of the expected EPERM (Operation not permitted).

The issue occurs in system_call_exception() when syscall_enter_from_user_mode() returns -1 to indicate the syscall should be skipped (e.g., blocked by seccomp). The current code treats this -1 as a syscall number and compares it against NR_syscalls. Since -1 (when cast to unsigned long) is greater than NR_syscalls, the code incorrectly returns -ENOSYS, overwriting the errno that seccomp already set via syscall_set_return_value().

The generic entry code in syscall_trace_enter() calls __secure_computing(), which sets the appropriate errno in regs->gpr[3] and returns -1 to signal that the syscall should be skipped. However, the PowerPC syscall handler was not checking for this -1 return value before validating the syscall number.

Fix this by explicitly checking if syscall_enter_from_user_mode() returns -1 and returning the value already set in regs->gpr[3] (the errno from seccomp) before performing the syscall number validation.

This aligns PowerPC's behavior with other architectures using GENERIC_ENTRY and restores correct seccomp errno handling.

Fixes bee25f97ad24 ("powerpc: Enable GENERIC_ENTRY feature")
---
 arch/powerpc/kernel/syscall.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/powerpc/kernel/syscall.c +0 -0
index a9da2af6efa8..5b58c8d396c8 100644
@@ -22,6 +22,10 @@ notrace long system_call_exception(struct pt_regs *regs, unsigned long r0)
	unsigned long r0_initial = r0;

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

Quoted 5 lines
arch/powerpc/kernel/syscall.c +2 -0
 	add_random_kstack_offset();
 	r0 = syscall_enter_from_user_mode(regs, r0);
 
+	/* Seccomp or ptrace may have set return value, skip syscall */
+	if (unlikely(r0 == -1L)
				&& (r0_initial != -1L))

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

Quoted 5 lines
arch/powerpc/kernel/syscall.c +2 -0
+		return regs->gpr[3];
+
 	if (unlikely(r0 >= NR_syscalls)) {
 		if (unlikely(trap_is_unsupported_scv(regs))) {
 			/* Unsupported scv vector */

Thanks

Quoted 1 line

Michal

What do you think about this diff?
This seems much cleaner.

arch/powerpc/kernel/syscall.c +5 -2
index a9da2af6efa8..a6c89052e8c5 100644
@@ -20,8 +20,6 @@ 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(r0 >= NR_syscalls)) {
                if (unlikely(trap_is_unsupported_scv(regs))) {
                        /* Unsupported scv vector */
@@ -30,6 +28,11 @@ notrace long system_call_exception(struct pt_regs *regs, unsigned long r0)
                }
                return -ENOSYS;
        }
+       r0 = syscall_enter_from_user_mode(regs, r0);
+
+       /* Seccomp or ptrace may have set return value, skip syscall */
+       if (unlikely(r0 == -1L))
+               return syscall_get_error(current, regs);
 
        /* May be faster to do array_index_nospec? */
        barrier_nospec();

Regards,
Mukesh

#10 lore raw
Quoted 92 lines · Mukesh Kumar Chaurasiya
Quoted 72 lines · Michal Suchánek

Hello,

there is yet another bug identified.

When the initial syscall number is -1 the new condition bypasses setting the ENOSYS below in if (unlikely(r0 >= NR_syscalls)) and returns 0.

perl -MPOSIX -e '$!=0; my $r = syscall(-1, 0); print "ret=$r errno=".($!+0)." ($!)\n"'

Normally the result is

ret=-1 errno=38 (Function not implemented)

but with this patch the result is

ret=0 errno=0 ()

fixup below.

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

Quoted 37 lines · Mukesh Kumar Chaurasiya (IBM)

After enabling GENERIC_ENTRY on PowerPC, seccomp filters using SCMP_ACT_ERRNO without an explicit errnoRet value return ENOSYS (Function not implemented) instead of the expected EPERM (Operation not permitted).

The issue occurs in system_call_exception() when syscall_enter_from_user_mode() returns -1 to indicate the syscall should be skipped (e.g., blocked by seccomp). The current code treats this -1 as a syscall number and compares it against NR_syscalls. Since -1 (when cast to unsigned long) is greater than NR_syscalls, the code incorrectly returns -ENOSYS, overwriting the errno that seccomp already set via syscall_set_return_value().

The generic entry code in syscall_trace_enter() calls __secure_computing(), which sets the appropriate errno in regs->gpr[3] and returns -1 to signal that the syscall should be skipped. However, the PowerPC syscall handler was not checking for this -1 return value before validating the syscall number.

Fix this by explicitly checking if syscall_enter_from_user_mode() returns -1 and returning the value already set in regs->gpr[3] (the errno from seccomp) before performing the syscall number validation.

This aligns PowerPC's behavior with other architectures using GENERIC_ENTRY and restores correct seccomp errno handling.

Fixes bee25f97ad24 ("powerpc: Enable GENERIC_ENTRY feature")
---
 arch/powerpc/kernel/syscall.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/powerpc/kernel/syscall.c +0 -0
index a9da2af6efa8..5b58c8d396c8 100644
@@ -22,6 +22,10 @@ notrace long system_call_exception(struct pt_regs *regs, unsigned long r0)
	unsigned long r0_initial = r0;

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

Quoted 5 lines
arch/powerpc/kernel/syscall.c +2 -0
 	add_random_kstack_offset();
 	r0 = syscall_enter_from_user_mode(regs, r0);
 
+	/* Seccomp or ptrace may have set return value, skip syscall */
+	if (unlikely(r0 == -1L)
				&& (r0_initial != -1L))

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

Quoted 5 lines
arch/powerpc/kernel/syscall.c +2 -0
+		return regs->gpr[3];
+
 	if (unlikely(r0 >= NR_syscalls)) {
 		if (unlikely(trap_is_unsupported_scv(regs))) {
 			/* Unsupported scv vector */

Thanks

Quoted 1 line

Michal

What do you think about this diff?
This seems much cleaner.

arch/powerpc/kernel/syscall.c +0 -2
index a9da2af6efa8..a6c89052e8c5 100644
@@ -20,8 +20,6 @@ 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(r0 >= NR_syscalls)) {
                if (unlikely(trap_is_unsupported_scv(regs))) {
                        /* Unsupported scv vector */

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

Quoted 9 lines
arch/powerpc/kernel/syscall.c +5 -0
@@ -30,6 +28,11 @@ notrace long system_call_exception(struct pt_regs *regs, unsigned long r0)
                }
                return -ENOSYS;
        }
+       r0 = syscall_enter_from_user_mode(regs, r0);
+
+       /* Seccomp or ptrace may have set return value, skip syscall */
+       if (unlikely(r0 == -1L))
+               return syscall_get_error(current, regs);
 

This will skip the check for NR_syscalls for whatever is returned from syscall_enter_from_user_mode other than -1. To me it is not clear if invalid syscall can be generated by one of the modifications done in syscall_enter_from_user_mode.

Thanks

Michal

#11 lore raw
Quoted 108 lines · Michal Suchánek
Quoted 92 lines · Mukesh Kumar Chaurasiya
Quoted 72 lines · Michal Suchánek

Hello,

there is yet another bug identified.

When the initial syscall number is -1 the new condition bypasses setting the ENOSYS below in if (unlikely(r0 >= NR_syscalls)) and returns 0.

perl -MPOSIX -e '$!=0; my $r = syscall(-1, 0); print "ret=$r errno=".($!+0)." ($!)\n"'

Normally the result is

ret=-1 errno=38 (Function not implemented)

but with this patch the result is

ret=0 errno=0 ()

fixup below.

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

Quoted 37 lines · Mukesh Kumar Chaurasiya (IBM)

After enabling GENERIC_ENTRY on PowerPC, seccomp filters using SCMP_ACT_ERRNO without an explicit errnoRet value return ENOSYS (Function not implemented) instead of the expected EPERM (Operation not permitted).

The issue occurs in system_call_exception() when syscall_enter_from_user_mode() returns -1 to indicate the syscall should be skipped (e.g., blocked by seccomp). The current code treats this -1 as a syscall number and compares it against NR_syscalls. Since -1 (when cast to unsigned long) is greater than NR_syscalls, the code incorrectly returns -ENOSYS, overwriting the errno that seccomp already set via syscall_set_return_value().

The generic entry code in syscall_trace_enter() calls __secure_computing(), which sets the appropriate errno in regs->gpr[3] and returns -1 to signal that the syscall should be skipped. However, the PowerPC syscall handler was not checking for this -1 return value before validating the syscall number.

Fix this by explicitly checking if syscall_enter_from_user_mode() returns -1 and returning the value already set in regs->gpr[3] (the errno from seccomp) before performing the syscall number validation.

This aligns PowerPC's behavior with other architectures using GENERIC_ENTRY and restores correct seccomp errno handling.

Fixes bee25f97ad24 ("powerpc: Enable GENERIC_ENTRY feature")
---
 arch/powerpc/kernel/syscall.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/powerpc/kernel/syscall.c +0 -0
index a9da2af6efa8..5b58c8d396c8 100644
@@ -22,6 +22,10 @@ notrace long system_call_exception(struct pt_regs *regs, unsigned long r0)
	unsigned long r0_initial = r0;

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

Quoted 5 lines
arch/powerpc/kernel/syscall.c +2 -0
 	add_random_kstack_offset();
 	r0 = syscall_enter_from_user_mode(regs, r0);
 
+	/* Seccomp or ptrace may have set return value, skip syscall */
+	if (unlikely(r0 == -1L)
				&& (r0_initial != -1L))

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

Quoted 5 lines
arch/powerpc/kernel/syscall.c +2 -0
+		return regs->gpr[3];
+
 	if (unlikely(r0 >= NR_syscalls)) {
 		if (unlikely(trap_is_unsupported_scv(regs))) {
 			/* Unsupported scv vector */

Thanks

Quoted 1 line

Michal

What do you think about this diff?
This seems much cleaner.

arch/powerpc/kernel/syscall.c +0 -2
index a9da2af6efa8..a6c89052e8c5 100644
@@ -20,8 +20,6 @@ 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(r0 >= NR_syscalls)) {
                if (unlikely(trap_is_unsupported_scv(regs))) {
                        /* Unsupported scv vector */

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

Quoted 9 lines
arch/powerpc/kernel/syscall.c +5 -0
@@ -30,6 +28,11 @@ notrace long system_call_exception(struct pt_regs *regs, unsigned long r0)
                }
                return -ENOSYS;
        }
+       r0 = syscall_enter_from_user_mode(regs, r0);
+
+       /* Seccomp or ptrace may have set return value, skip syscall */
+       if (unlikely(r0 == -1L))
+               return syscall_get_error(current, regs);
 

This will skip the check for NR_syscalls for whatever is returned from syscall_enter_from_user_mode other than -1. To me it is not clear if invalid syscall can be generated by one of the modifications done in syscall_enter_from_user_mode.

Quoted 3 lines

Thanks

Michal

The possible return values for syscall_enter_from_user_mode are either gpr[0] or -1. If we have an invalid syscall it'll be handled by the r0 >= NR_syscalls check. I think functionally there should be no issues.

Regards,
Mukesh