Re: [RFC PATCH v1 1/1] arm64: Forget syscall if different from execve*()

Francis Laniel
2022-05-10T14:42:48Z

Hi.

Le mardi 10 mai 2022, 11:59:48 BST Will Deacon a écrit :
> On Mon, May 09, 2022 at 04:19:57PM +0100, Francis Laniel wrote:
> > This patch enables exeve*() to be traced by syscalls:sys_exit_execve
> > tracepoint.
> > Previously, calling forget_syscall() would set syscall to -1, which
> > impedes
> > this tracepoint to prints its information.
> > So, this patch makes call to forget_syscall() conditional by only calling
> > it when syscall number is not execve() or execveat().
> >
> > Signed-off-by: Francis Laniel <flaniel@linux.microsoft.com>
> > ---
> >
> > arch/arm64/include/asm/processor.h | 8 +++++++-
> > 1 file changed, 7 insertions(+), 1 deletion(-)
> >
> > diff --git a/arch/arm64/include/asm/processor.h
> > b/arch/arm64/include/asm/processor.h index 73e38d9a540c..e12ceb363d6a
> > 100644
> > --- a/arch/arm64/include/asm/processor.h
> > +++ b/arch/arm64/include/asm/processor.h
> > @@ -34,6 +34,8 @@
> >
> > #include <vdso/processor.h>
> >
> > +#include <asm-generic/unistd.h>
> > +
> >
> > #include <asm/alternative.h>
> > #include <asm/cpufeature.h>
> > #include <asm/hw_breakpoint.h>
> >
> > @@ -250,8 +252,12 @@ void tls_preserve_current_state(void);
> >
> > static inline void start_thread_common(struct pt_regs *regs, unsigned
> > long pc) {
> >
> > + s32 previous_syscall = regs->syscallno;
> >
> > memset(regs, 0, sizeof(*regs));
> >
> > - forget_syscall(regs);
> > + if (previous_syscall == __NR_execve || previous_syscall ==
> > __NR_execveat)
> > + regs->syscallno = previous_syscall;
> > + else
> > + forget_syscall(regs);
>
> Hmm, this really looks like a bodge and it doesn't handle the compat case
> either.
>
> How do other architectures handle this?

My understanding of others architectures is quite limited, but here are my
findings and understanding of some of them:
* arm (32 bits) EABI: start_thread() sets r7 to previous r7 for ELF FDPIC and
to 0 for other binfmts [1].
* arm (32 bits) OABI: syscall number is set to -1 if
ptrace_report_syscall_entry() failed [2].
* mips: start_thread() does not modify current_thread_info->syscall which is
taken directly from v0 [3, 4].
* riscv: start_thread() does not modify a7 [5].
* x86_64: start_thread_common() does not touch orig_ax which seems to contain
the syscall number [6].

> Will

Best regards.
---
[1] https://elixir.bootlin.com/linux/v5.18-rc6/source/arch/arm/include/asm/
processor.h#L52
[2] https://elixir.bootlin.com/linux/v5.18-rc6/source/arch/arm/kernel/
ptrace.c#L847
[3] https://elixir.bootlin.com/linux/v5.18-rc6/source/arch/mips/kernel/
process.c#L52
[4] https://elixir.bootlin.com/linux/v5.18-rc6/source/arch/mips/kernel/
scall64-n64.S#L85
[5] https://elixir.bootlin.com/linux/v5.18-rc6/source/arch/riscv/kernel/
process.c#L87
[6] https://elixir.bootlin.com/linux/v5.18-rc6/source/arch/x86/kernel/
process_64.c#L505