LinuxPPC-Dev Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: "Michal Suchánek" <msuchanek@suse.de>
To: Mukesh Kumar Chaurasiya <mkchauras@gmail.com>
Cc: maddy@linux.ibm.com, mpe@ellerman.id.au, npiggin@gmail.com,
	chleroy@kernel.org, sshegde@linux.ibm.com,
	mkchauras@linux.ibm.com, kees@kernel.org, mark.rutland@arm.com,
	ryan.roberts@arm.com, linuxppc-dev@lists.ozlabs.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] powerpc/syscall: Fix seccomp errno handling with GENERIC_ENTRY
Date: Mon, 29 Jun 2026 19:07:21 +0200	[thread overview]
Message-ID: <akKmSblIh_xNcHHp@kunlun.suse.cz> (raw)
In-Reply-To: <akKkdd7i6oZ2o5A5@li-1a3e774c-28e4-11b2-a85c-acc9f2883e29.ibm.com>

On Mon, Jun 29, 2026 at 10:32:47PM +0530, Mukesh Kumar Chaurasiya wrote:
> On Mon, Jun 29, 2026 at 03:31:36PM +0200, Michal Suchánek wrote:
> > 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.
> > 
> > On Wed, Jun 24, 2026 at 10:45:20PM +0530, Mukesh Kumar Chaurasiya (IBM) wrote:
> > > 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")
> > > Reported-by: Michal Suchánek <msuchanek@suse.de>
> > > Signed-off-by: Mukesh Kumar Chaurasiya (IBM) <mkchauras@gmail.com>
> > > ---
> > >  arch/powerpc/kernel/syscall.c | 4 ++++
> > >  1 file changed, 4 insertions(+)
> > > 
> > > diff --git a/arch/powerpc/kernel/syscall.c b/arch/powerpc/kernel/syscall.c
> > > index a9da2af6efa8..5b58c8d396c8 100644
> > > --- a/arch/powerpc/kernel/syscall.c
> > > +++ b/arch/powerpc/kernel/syscall.c
> > > @@ -22,6 +22,10 @@ notrace long system_call_exception(struct pt_regs *regs, unsigned long r0)
> > 	unsigned long r0_initial = 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)
> > 				&& (r0_initial != -1L))
> > > +		return regs->gpr[3];
> > > +
> > >  	if (unlikely(r0 >= NR_syscalls)) {
> > >  		if (unlikely(trap_is_unsupported_scv(regs))) {
> > >  			/* Unsupported scv vector */
> > 
> > Thanks
> > 
> > Michal
> 
> What do you think about this diff?
> This seems much cleaner.
> 
> diff --git a/arch/powerpc/kernel/syscall.c b/arch/powerpc/kernel/syscall.c
> index a9da2af6efa8..a6c89052e8c5 100644
> --- a/arch/powerpc/kernel/syscall.c
> +++ b/arch/powerpc/kernel/syscall.c
> @@ -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);
>  

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


  reply	other threads:[~2026-06-29 17:07 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-24 17:15 [PATCH] powerpc/syscall: Fix seccomp errno handling with GENERIC_ENTRY Mukesh Kumar Chaurasiya (IBM)
2026-06-24 17:44 ` Michal Suchánek
2026-06-26  5:54 ` Michal Suchánek
2026-06-29  4:50   ` Mukesh Kumar Chaurasiya
2026-06-26  7:50 ` Kees Cook
2026-06-26 14:30   ` Christophe Leroy (CS GROUP)
2026-06-26 14:31 ` Christophe Leroy (CS GROUP)
2026-06-29  4:54   ` Mukesh Kumar Chaurasiya
2026-06-29 13:31 ` Michal Suchánek
2026-06-29 17:02   ` Mukesh Kumar Chaurasiya
2026-06-29 17:07     ` Michal Suchánek [this message]
2026-06-29 17:14       ` Mukesh Kumar Chaurasiya

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=akKmSblIh_xNcHHp@kunlun.suse.cz \
    --to=msuchanek@suse.de \
    --cc=chleroy@kernel.org \
    --cc=kees@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=maddy@linux.ibm.com \
    --cc=mark.rutland@arm.com \
    --cc=mkchauras@gmail.com \
    --cc=mkchauras@linux.ibm.com \
    --cc=mpe@ellerman.id.au \
    --cc=npiggin@gmail.com \
    --cc=ryan.roberts@arm.com \
    --cc=sshegde@linux.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox