Re: [PATCH v3 3/6] syscall.h: introduce syscall_set_nr()

Dmitry V. Levin
2025-01-28T16:32:51Z

On Tue, Jan 28, 2025 at 04:13:52PM +0100, Christophe Leroy wrote:
> Le 28/01/2025 à 10:16, Dmitry V. Levin a écrit :
> > Similar to syscall_set_arguments() that complements
> > syscall_get_arguments(), introduce syscall_set_nr()
> > that complements syscall_get_nr().
> >
> > syscall_set_nr() is going to be needed along with
> > syscall_set_arguments() on all HAVE_ARCH_TRACEHOOK
> > architectures to implement PTRACE_SET_SYSCALL_INFO API.
> >
> > Signed-off-by: Dmitry V. Levin <ldv@strace.io>
> > Tested-by: Charlie Jenkins <charlie@rivosinc.com>
> > Reviewed-by: Charlie Jenkins <charlie@rivosinc.com>
> > ---
> > arch/arc/include/asm/syscall.h | 11 +++++++++++
> > arch/arm/include/asm/syscall.h | 24 ++++++++++++++++++++++++
> > arch/arm64/include/asm/syscall.h | 16 ++++++++++++++++
> > arch/hexagon/include/asm/syscall.h | 7 +++++++
> > arch/loongarch/include/asm/syscall.h | 7 +++++++
> > arch/m68k/include/asm/syscall.h | 7 +++++++
> > arch/microblaze/include/asm/syscall.h | 7 +++++++
> > arch/mips/include/asm/syscall.h | 14 ++++++++++++++
> > arch/nios2/include/asm/syscall.h | 5 +++++
> > arch/openrisc/include/asm/syscall.h | 6 ++++++
> > arch/parisc/include/asm/syscall.h | 7 +++++++
> > arch/powerpc/include/asm/syscall.h | 10 ++++++++++
> > arch/riscv/include/asm/syscall.h | 7 +++++++
> > arch/s390/include/asm/syscall.h | 12 ++++++++++++
> > arch/sh/include/asm/syscall_32.h | 12 ++++++++++++
> > arch/sparc/include/asm/syscall.h | 12 ++++++++++++
> > arch/um/include/asm/syscall-generic.h | 5 +++++
> > arch/x86/include/asm/syscall.h | 7 +++++++
> > arch/xtensa/include/asm/syscall.h | 7 +++++++
> > include/asm-generic/syscall.h | 14 ++++++++++++++
> > 20 files changed, 197 insertions(+)
> >
>
> > diff --git a/arch/arm64/include/asm/syscall.h b/arch/arm64/include/asm/syscall.h
> > index 76020b66286b..712daa90e643 100644
> > --- a/arch/arm64/include/asm/syscall.h
> > +++ b/arch/arm64/include/asm/syscall.h
> > @@ -61,6 +61,22 @@ static inline void syscall_set_return_value(struct task_struct *task,
> > regs->regs[0] = val;
> > }
> >
> > +static inline void syscall_set_nr(struct task_struct *task,
> > + struct pt_regs *regs,
> > + int nr)
> > +{
> > + regs->syscallno = nr;
> > + if (nr == -1) {
> > + /*
> > + * When the syscall number is set to -1, the syscall will be
> > + * skipped. In this case the syscall return value has to be
> > + * set explicitly, otherwise the first syscall argument is
> > + * returned as the syscall return value.
> > + */
> > + syscall_set_return_value(task, regs, -ENOSYS, 0);
> > + }
> > +}
> > +
> > #define SYSCALL_MAX_ARGS 6
> >
> > static inline void syscall_get_arguments(struct task_struct *task,
>
> > diff --git a/arch/powerpc/include/asm/syscall.h b/arch/powerpc/include/asm/syscall.h
> > index 521f279e6b33..7505dcfed247 100644
> > --- a/arch/powerpc/include/asm/syscall.h
> > +++ b/arch/powerpc/include/asm/syscall.h
> > @@ -39,6 +39,16 @@ static inline int syscall_get_nr(struct task_struct *task, struct pt_regs *regs)
> > return -1;
> > }
> >
> > +static inline void syscall_set_nr(struct task_struct *task, struct pt_regs *regs, int nr)
> > +{
> > + /*
> > + * Unlike syscall_get_nr(), syscall_set_nr() can be called only when
> > + * the target task is stopped for tracing on entering syscall, so
> > + * there is no need to have the same check syscall_get_nr() has.
> > + */
> > + regs->gpr[0] = nr;
>
> Doesn't the same as for ARM64 apply here as well ?

I carefully checked all affected architectures and added that
syscall_set_return_value() call only where I think it's needed.

On powerpc it's not needed with the current implementation: their
do_seccomp() sets -ENOSYS before __secure_computing() invocation, and
their do_syscall_trace_enter() sets -ENOSYS in case of an invalid syscall
number.