PTRACE_SET_SYSCALL_INFO: add support for seccomp syscall skipping
Subsystems (4)
v8 cover PTRACE_SET_SYSCALL_INFO: add support for seccomp syscall skipping
PTRACE_SET_SYSCALL_INFO is a generic ptrace API that complements PTRACE_GET_SYSCALL_INFO by allowing a tracer to modify details of a system call in which the tracee is currently blocked.
This patch set extends PTRACE_SET_SYSCALL_INFO with support for skipping a system call at a PTRACE_EVENT_SECCOMP stop and setting its return value. To do this, the tracer changes 'op' from PTRACE_SYSCALL_INFO_SECCOMP to PTRACE_SYSCALL_INFO_EXIT and fills in the exit union fields (rval and is_error).
Michal pointed out in the review of v7 [1] that the register state alone cannot tell whether a syscall has to be executed or skipped with a preset return value, since on some architectures the return value shares a register with the syscall number or the arguments, and that the "already dispatched" information should be architecture independent rather than an architecture-specific thread flag.
v8 does exactly that: the tracer no longer sets the syscall number to -1 and relies on syscall_get_nr() reading it back as negative. Instead, patch 1 adds a per-task flag owned by seccomp, which the tracer sets while the tracee is stopped. When the tracee resumes, seccomp skips the system call just like for SECCOMP_RET_ERRNO, a path every architecture already handles without touching the return value. This also fixes skipping on s390, where -1 is read back as syscall 65535.
On powerpc, this series depends on the fix posted separately [2]:syscall_set_return_value() currently sets TIF_SYSCALL_RET on the *caller*, i.e. on the tracer when called from PTRACE_SET_SYSCALL_INFO, and skipped system calls lose non-error return values. Both problems are regressions in v7.3-rc and already break the existing set_syscall_info selftest and seccomp_bpf user_notification_basic on ppc64le.
[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/
Testing (QEMU, v7.3-rc6, tools/testing/selftests/ptrace/set_syscall_info and tools/testing/selftests/seccomp/seccomp_bpf):
x86_64 (KVM) ppc64le (TCG) s390x (TCG) ptrace/set_syscall_info 2/2 2/2 2/2 seccomp/seccomp_bpf 111/111 111/111 110/111 (*)
Skipping getpid() at a PTRACE_EVENT_SECCOMP stop, with a filter that kills the process on unknown syscall numbers:
op=SECCOMP, nr=-1 (before) error r3 left as is SIGSYS kill op=SECCOMP->EXIT (this) -ESRCH -ESRCH -ESRCH
(*) user_notification_wait_killable_after_reply timed out under TCG
while another VM was running; it passes 5/5 both with and without this series on an idle x86_64 KVM guest.
Build-tested also with CONFIG_SECCOMP=n and CONFIG_SECCOMP_FILTER=n. Not tested: MIPS, arm64, riscv, loongarch, real hardware.
Changes in v8:
* skip the system call via a seccomp per-task flag instead of syscall_set_nr(-1), as suggested by Michal Suchánek
* new helper ptrace_set_syscall_info_seccomp_skip() instead of a bool argument to ptrace_set_syscall_info_exit() (Michal Suchánek)
* once the system call is skipped, reject further PTRACE_SYSCALL_INFO_SECCOMP updates during the same stop
* selftest: also test an error return value and the above
* dropped the Reviewed-by tags of Oleg and Dmitry, as patch 2 changed significantly
Changes in v7:
* rebased to Linux 7.3-rc1
Changes in v6:
* selftests/ptrace/set_syscall_info.c: improve messages in case of failure.
Changes in v5:
* reworded the explanation for not supporting PTRACE_SYSCALL_INFO_ENTRY yet.
* selftests/ptrace: removed a redundant check.
Changes in v4:
* Reworded the commit messages for clarity.
* Renamed the local variable child_op to op in ptrace_set_syscall_info()
* Clean up and improve the coding style of selftests/ptrace/set_syscall_info.c (suggested by Dmitry V. Levin)
Changes in v3:
* restrict the syscall skipping feature to PTRACE_SYSCALL_INFO_SECCOMP
Changes in v2:
bugfix: _NONE -> _EXIT transition was erroneously permitted
Davide Berardi (1):
seccomp: let the tracer skip a syscall independently of its number
Renzo Davoli (2):
ptrace: add PTRACE_SET_SYSCALL_INFO syscall skipping support selftests/ptrace: add a test case for PTRACE_SET_SYSCALL_INFO syscall
skipping
include/linux/seccomp.h | 33 +++ include/linux/seccomp_types.h | 4 + kernel/ptrace.c | 45 +++- kernel/seccomp.c | 11 +- .../selftests/ptrace/set_syscall_info.c | 220 +++++++++++++++++- 5 files changed, 309 insertions(+), 4 deletions(-)
base-commit: 32c52ec87d6245ee2d12ed5dd509e40062939b50
prerequisite-patch-id: abdd0fc0973bc3845ae7bfe84d855a7213ccd3d7
Signature · 1 line
2.55.0
PATCH v8 1/3 seccomp: let the tracer skip a syscall independently of its number
From: Davide Berardi <[email protected]>
At a PTRACE_EVENT_SECCOMP stop the tracer can only skip the system call by setting its number to -1, which seccomp reads back with syscall_get_nr(). This depends on how each architecture lays out the syscall number and the return value in the registers:
- on some architectures the syscall number shares a register with the return value (e.g. v0 on MIPS), so setting the return value after the number would overwrite the skip request, and each of them needs its own way to keep it (MIPS keeps a copy of the number in thread_info);
- on s390 syscall_get_nr() returns the low 16 bits of int_code, so -1 is read back as 65535: seccomp does not skip the call but runs the filters again on syscall 65535, which kills the task if the filter denies unknown syscalls.
Add a per-task flag which the tracer sets with seccomp_ptrace_skip_syscall() while the tracee is stopped at the PTRACE_EVENT_SECCOMP stop, after it has set the return value. When the tracee resumes, seccomp skips the system call like for SECCOMP_RET_ERRNO, which every architecture already handles without touching the return value.
The flag is cleared by the tracee before the stop and written by the tracer only while the tracee is stopped, so it needs no locking: the wake-up of the tracee orders it like the registers written by the tracer.
The next patch uses this in PTRACE_SET_SYSCALL_INFO.
--- include/linux/seccomp.h | 33 +++++++++++++++++++++++++++++++++ include/linux/seccomp_types.h | 4 ++++ kernel/seccomp.c | 11 ++++++++++- 3 files changed, 47 insertions(+), 1 deletion(-)
include/linux/seccomp.h +33 -0
@@ -75,6 +75,32 @@ static inline int seccomp_mode(struct seccomp *s)#ifdef CONFIG_SECCOMP_FILTER extern void seccomp_filter_release (struct task_struct *tsk);extern void get_seccomp_filter (struct task_struct *tsk); + +/** +* seccomp_ptrace_skip_syscall - make a tracee skip the current system call +* @tsk: tracee stopped at a PTRACE_EVENT_SECCOMP stop +* +* Must only be called by the tracer of @tsk while @tsk is stopped at a +* PTRACE_EVENT_SECCOMP stop, after it has set the return value of the system +* call. When the tracee resumes, the system call is skipped and the return +* value is left untouched, like with SECCOMP_RET_ERRNO. +*/ +static inline void seccomp_ptrace_skip_syscall (struct task_struct *tsk) +{ + tsk->seccomp.ptrace_skip = true; +} + +/** +* seccomp_ptrace_syscall_skipped - check for seccomp_ptrace_skip_syscall() +* @tsk: tracee stopped at a PTRACE_EVENT_SECCOMP stop +* +* Return: true if the tracer requested to skip the system call during the +* current PTRACE_EVENT_SECCOMP stop of @tsk. +*/ +static inline bool seccomp_ptrace_syscall_skipped (struct task_struct *tsk) +{ +return tsk->seccomp.ptrace_skip; +}#else /* CONFIG_SECCOMP_FILTER */ static inline void seccomp_filter_release (struct task_struct *tsk) {
@@ -84,6 +110,13 @@ static inline void get_seccomp_filter(struct task_struct *tsk) {return ; } +static inline void seccomp_ptrace_skip_syscall (struct task_struct *tsk) +{ +} +static inline bool seccomp_ptrace_syscall_skipped (struct task_struct *tsk) +{ +return false; +}#endif /* CONFIG_SECCOMP_FILTER */ #if defined(CONFIG_SECCOMP_FILTER ) && defined(CONFIG_CHECKPOINT_RESTORE )
include/linux/seccomp_types.h +4 -0
@@ -18,11 +18,15 @@ struct seccomp_filter;* * @filter must only be accessed from the context of current as there * is no read locking. +* @ptrace_skip: set by the tracer while the task is stopped at a +* PTRACE_EVENT_SECCOMP stop to request that the system call is +* skipped, after it has set the return value. */ struct seccomp {int mode;atomic_t filter_count;struct seccomp_filter *filter; +bool ptrace_skip; };#else
kernel/seccomp.c +10 -1
@@ -1306,6 +1306,7 @@ static bool __seccomp_filter(int this_syscall, const bool recheck_after_trace) }/* Allow the BPF to provide the event message */ + current->seccomp.ptrace_skip = false;ptrace_event (PTRACE_EVENT_SECCOMP , data);/* * The delivery of a fatal signal during event
@@ -1319,7 +1320,15 @@ static bool __seccomp_filter(int this_syscall, const bool recheck_after_trace)*/ if (fatal_signal_pending (current)) goto skip; -/* Check if the tracer forced the syscall to be skipped. */ +/* +* Check if the tracer forced the syscall to be skipped, either +* with seccomp_ptrace_skip_syscall(), which does not depend on +* how the architecture encodes the syscall number and the +* return value in the registers, or by setting the syscall +* number to -1. +*/ +if (current->seccomp.ptrace_skip) + goto skip; this_syscall =syscall_get_nr (current,current_pt_regs ());if (this_syscall <0 ) goto skip;
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v8 1/3] seccomp: let the tracer skip a syscall independently of its number
Sashiko has reviewed this patch and found no issues.
Quoted 18 lines
From: Davide Berardi <[email protected]>
At a
PTRACE_EVENT_SECCOMPstop the tracer can only skip the system call by setting its number to -1, which seccomp reads back withsyscall_get_nr(). This depends on how each architecture lays out the syscall number and the return value in the registers:- on some architectures the syscall number shares a register with the return value (e.g. v0 on MIPS), so setting the return value after the number would overwrite the skip request, and each of them needs its own way to keep it (MIPS keeps a copy of the number in
thread_info);- on s390
syscall_get_nr()returns the low 16 bits ofint_code, so -1 is read back as 65535: seccomp does not skip the call but runs the filters again on syscall 65535, which kills the task if the filter
[ ... ]
The next patch uses this inPTRACE_SET_SYSCALL_INFO.
Quoted 3 lines
Suggested-by Michal Suchánek <[email protected]>Assisted-by claude-opus-5-5Signed-off-by Renzo Davoli <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v8 2/3 ptrace: add PTRACE_SET_SYSCALL_INFO syscall skipping support
In reply to Renzo DavoliExtend PTRACE_SET_SYSCALL_INFO to support skipping a system call triggered via seccomp.
When a tracer retrieves a ptrace_syscall_info structure with 'op' set to PTRACE_SYSCALL_INFO_SECCOMP, it can now choose to skip the system call. To do this, the tracer changes 'op' to PTRACE_SYSCALL_INFO_EXIT and populates the exit union fields (rval and is_error) to define the return value and error status for the tracee.
The skip request is passed to seccomp with seccomp_ptrace_skip_syscall() rather than by setting the system call number to -1, so that it does not depend on how the architecture lays out the syscall number and the return value in the registers. The tracee then skips the system call the same way as for SECCOMP_RET_ERRNO.
Once the system call has been skipped, a further PTRACE_SET_SYSCALL_INFO with op == PTRACE_SYSCALL_INFO_SECCOMP during the same stop fails with -EINVAL, as the return value may already have overwritten registers holding the syscall number or arguments; op == PTRACE_SYSCALL_INFO_EXIT can still be used to change the return value.
System call suppression via PTRACE_SYSCALL_INFO_ENTRY is currently not implemented. On some architectures (e.g. MIPS), when a system call is skipped by setting the syscall number to -1 at the entry stop, the architecture entry path unconditionally overwrites the return value register with -ENOSYS, clobbering any custom return value set by the tracer at the entry stop.
[v8: skip via seccomp_ptrace_skip_syscall() instead of syscall_set_nr(-1),
separate ptrace_set_syscall_info_seccomp_skip(), reject later PTRACE_SYSCALL_INFO_SECCOMP updates during the same stop]
--- kernel/ptrace.c | 45 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-)
kernel/ptrace.c +43 -2
@@ -34,6 +34,7 @@#include <linux/sched/signal.h> #include <linux/minmax.h> #include <linux/syscall_user_dispatch.h> +#include <linux/seccomp.h> #include <asm/syscall.h> /* for syscall_get_* */
@@ -1119,12 +1120,35 @@ ptrace_set_syscall_info_exit(struct task_struct *child, struct pt_regs *regs,return 0 ; } +/* +* Skip the system call at a PTRACE_SYSCALL_INFO_SECCOMP stop and set its +* return value as specified by info->exit. +* +* The skip request is passed to seccomp out of band instead of by setting +* the system call number to -1: on some architectures the syscall number, +* or the register seccomp reads it from, shares a register with the return +* value, and on others -1 cannot be represented as a syscall number. +*/ +static int +ptrace_set_syscall_info_seccomp_skip (struct task_struct *child, +struct pt_regs *regs, +struct ptrace_syscall_info *info) +{ +int ret =ptrace_set_syscall_info_exit (child, regs, info); + +if (!ret) +seccomp_ptrace_skip_syscall (child); + +return ret; +} +static int ptrace_set_syscall_info (struct task_struct *child,unsigned long user_size,const void __user *datavp) {struct pt_regs *regs =task_pt_regs (child);struct ptrace_syscall_info info; +int op;if (user_size <sizeof (info))return -EINVAL ;
@@ -1141,8 +1165,25 @@ ptrace_set_syscall_info(struct task_struct *child, unsigned long user_size,if (info.flags || info.reserved)return -EINVAL ; -/* Changing the type of the system call stop is not supported yet. */ -if (ptrace_get_syscall_info_op (child) != info.op) + op =ptrace_get_syscall_info_op (child); + +/* +* Changing the type of the system call stop is not supported, except +* that a PTRACE_SYSCALL_INFO_SECCOMP stop can be turned into a +* PTRACE_SYSCALL_INFO_EXIT one to skip the system call and set its +* return value. Once that is done, the system call number and +* arguments cannot be changed anymore during the same stop, as the +* return value might share registers with them. +*/ +if (op ==PTRACE_SYSCALL_INFO_SECCOMP ) { +if (info.op ==PTRACE_SYSCALL_INFO_EXIT ) +return ptrace_set_syscall_info_seccomp_skip (child, regs, + &info); +if (seccomp_ptrace_syscall_skipped (child)) +return -EINVAL ; + } + +if (op != info.op)return -EINVAL ;switch (info.op) {
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v8 2/3] ptrace: add PTRACE_SET_SYSCALL_INFO syscall skipping support
Sashiko has reviewed this patch and found no issues.
Quoted 21 lines
Extend
PTRACE_SET_SYSCALL_INFOto support skipping a system call triggered via seccomp.When a tracer retrieves a
ptrace_syscall_infostructure with 'op' set toPTRACE_SYSCALL_INFO_SECCOMP, it can now choose to skip the system call. To do this, the tracer changes 'op' toPTRACE_SYSCALL_INFO_EXITand populates the exit union fields (rval andis_error) to define the return value and error status for the tracee.The skip request is passed to seccomp with
seccomp_ptrace_skip_syscall()rather than by setting the system call number to -1, so that it does not depend on how the architecture lays out the syscall number and the return value in the registers. The tracee then skips the system call the same way as forSECCOMP_RET_ERRNO.Once the system call has been skipped, a further
PTRACE_SET_SYSCALL_INFOwith op ==PTRACE_SYSCALL_INFO_SECCOMPduring the same stop fails with-EINVAL, as the return value may already have overwritten registers holding the syscall number or arguments; op ==PTRACE_SYSCALL_INFO_EXITcan still be used to change the return value.
Quoted 8 lines
System call suppression via
PTRACE_SYSCALL_INFO_ENTRYis currently not implemented. On some architectures (e.g. MIPS), when a system call is skipped by setting the syscall number to -1 at the entry stop, the architecture entry path unconditionally overwrites the return value register with-ENOSYS, clobbering any custom return value set by the tracer at the entry stop.Signed-off-by Renzo Davoli <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH v8 3/3 selftests/ptrace: add a test case for PTRACE_SET_SYSCALL_INFO syscall skipping
In reply to Renzo DavoliCheck whether PTRACE_SET_SYSCALL_INFO syscall skipping semantics implemented in the kernel matches userspace expectations.
[v8: also check an error return value, and that the syscall cannot be
changed anymore once skipped while its return value still can; checkpatch fixes]
--- .../selftests/ptrace/set_syscall_info.c | 220 +++++++++++++++++- 1 file changed, 219 insertions(+), 1 deletion(-)
tools/testing/selftests/ptrace/set_syscall_info.c +219 -1
@@ -11,9 +11,15 @@#include <err.h> #include <fcntl.h> #include <signal.h> +#include <stdlib.h> +#include <stddef.h> #include <asm/unistd.h> +#include <sys/prctl.h> #include <linux/types.h> #include <linux/ptrace.h> +#include <linux/filter.h> +#include <linux/seccomp.h> +#include <linux/prctl.h> #if defined(_MIPS_SIM) && _MIPS_SIM == _MIPS_SIM_NABI32/*
@@ -36,6 +42,7 @@ struct si_exit {static unsigned int ptrace_stop;static pid_t tracee_pid; +static pid_t tracer_pid;static int kill_tracee (pid_t pid)
@@ -64,6 +71,25 @@ sys_ptrace(int request, pid_t pid, unsigned long addr, unsigned long data) ptrace_stop, ##__VA_ARGS__); \ }while (0 ) +static int sys_seccomp(unsigned int operation, unsigned int flags, void *args) +{ +return syscall (__NR_seccomp, operation, flags, args); +} + +static struct sock_filter seccomp_filter[] = { +BPF_STMT (BPF_LD +BPF_W +BPF_ABS , offsetof(struct seccomp_data , nr)), + +BPF_JUMP (BPF_JMP +BPF_JEQ +BPF_K , __NR_restart_syscall,0 ,1 ), +BPF_STMT (BPF_RET +BPF_K ,SECCOMP_RET_ALLOW ), + +BPF_STMT (BPF_RET +BPF_K ,SECCOMP_RET_TRACE ), +}; + +static struct sock_fprog seccomp_prog = { + .filter = seccomp_filter, + .len =ARRAY_SIZE (seccomp_filter) +}; + static void check_psi_entry(struct __test_metadata *_metadata, const struct ptrace_syscall_info *info,
@@ -128,7 +154,6 @@ check_psi_exit(struct __test_metadata *_metadata,TEST (set_syscall_info) { -const pid_t tracer_pid =getpid ();const kernel_ulong_t dummy[] = { (kernel_ulong_t )0xdad0bef0bad0fed0ULL , (kernel_ulong_t )0xdad1bef1bad1fed1ULL ,
@@ -138,6 +163,7 @@ TEST(set_syscall_info) (kernel_ulong_t) 0xdad5bef5bad5fed5ULL, };int splice_in[2 ], splice_out[2 ]; + tracer_pid =getpid ();ASSERT_EQ (0 ,pipe (splice_in));ASSERT_EQ (0 ,pipe (splice_out));
@@ -516,4 +542,196 @@ TEST(set_syscall_info)ASSERT_EQ (ptrace_stop,ARRAY_SIZE (si) *2 ); } +TEST (set_syscall_info_seccomp) +{ + tracer_pid =getpid (); + tracee_pid =fork (); + + ASSERT_LE(0, tracee_pid) { +TH_LOG ("fork: %m" ); + } + +/* tracee */ +if (tracee_pid ==0 ) { + tracee_pid =getpid (); + ASSERT_EQ(0, sys_ptrace(PTRACE_TRACEME, 0, 0, 0)) { +TH_LOG ("PTRACE_TRACEME: %m" ); + } + ASSERT_EQ(0, kill(tracee_pid, SIGSTOP)) { +/* cannot happen */ +TH_LOG ("kill SIGSTOP: %m" ); + } + + ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) { +TH_LOG ("prctl: %m" ); + } +ASSERT_EQ (0 ,sys_seccomp (SECCOMP_SET_MODE_FILTER ,0 , + (void *) &seccomp_prog)) { +TH_LOG ("seccomp: %m" ); + } + +/* run getpid unmodified */ + ASSERT_EQ(tracee_pid, getpid()) { +_exit (1 ); + } + +/* run getppid instead of getpid */ + ASSERT_EQ(tracer_pid, getpid()) { +_exit (1 ); + } + +/* skip getpid and return 42 */ + ASSERT_EQ(42, getpid()) { +_exit (1 ); + } + +/* skip getpid and fail with ESRCH */ + errno =0 ; + ASSERT_EQ(-1, syscall(__NR_getpid)) { +_exit (1 ); + } + ASSERT_EQ(ESRCH, errno) { +_exit (1 ); + } +_exit (0 ); + } + +int status; + +/* tracer */ + ASSERT_LE(0, waitpid(-1, &status, 0)) { +LOG_KILL_TRACEE ("waitpid: %m" ); + } + +ASSERT_EQ (0 ,sys_ptrace (PTRACE_SETOPTIONS , tracee_pid,0 , + (PTRACE_O_TRACESECCOMP | PTRACE_O_TRACESYSGOOD))) +LOG_KILL_TRACEE ("PTRACE_SETOPTIONS: %m" ); + + ASSERT_EQ(0, sys_ptrace(PTRACE_CONT, tracee_pid, 0, 0)) { +LOG_KILL_TRACEE ("PTRACE_CONT: %m" ); + } + +static const char *const testname[] = { +"unknown error" , +"getpid seccomp unchanged" , +"getpid seccomp nr change: getppid" , +"getpid seccomp skip+set retvalue" , +"getpid seccomp skip+set error" + }; + +static const unsigned int expected_nr[] = { + __NR_getpid, + __NR_getpid, + __NR_getpid, + __NR_getpid, + __NR_exit_group + }; + +for (ptrace_stop =0 ; ; ++ptrace_stop) { + ASSERT_EQ(tracee_pid, wait(&status)) { +/* cannot happen */ +LOG_KILL_TRACEE ("wait: %m" ); + } + +if (WIFEXITED (status)) { + tracee_pid =0 ;/* the tracee is no more */ + ASSERT_EQ(0, WEXITSTATUS(status)) { +LOG_KILL_TRACEE ("unexpected exit status %u" , +WEXITSTATUS (status)); + } +break ; + } +ASSERT_FALSE (WIFSIGNALED (status )) { + tracee_pid =0 ;/* the tracee is no more */ +LOG_KILL_TRACEE ("unexpected signal %u" , +WTERMSIG (status)); + } +ASSERT_TRUE (WIFSTOPPED (status )) { +LOG_KILL_TRACEE ("unexpected wait status %#x" , status); + } + + ASSERT_EQ(status >> 8, SIGTRAP | (PTRACE_EVENT_SECCOMP << 8)) { +LOG_KILL_TRACEE ("unexpected stop, wait status %#x" , status); + } + +struct ptrace_syscall_info info = { + .op =0xff /* invalid PTRACE_SYSCALL_INFO_* op */ + }; +size_t info_size =sizeof (info); + +ASSERT_LT (0 ,sys_ptrace (PTRACE_GET_SYSCALL_INFO , tracee_pid, + info_size, (uintptr_t) &info)) { +LOG_KILL_TRACEE ("PTRACE_GET_SYSCALL_INFO: %m" ); + } + ASSERT_EQ(PTRACE_SYSCALL_INFO_SECCOMP, info.op) { +LOG_KILL_TRACEE ("entry op mismatch: %m" ); + } + ASSERT_TRUE(info.arch) { +LOG_KILL_TRACEE ("entry arch mismatch: %m" ); + } + ASSERT_TRUE(info.instruction_pointer) { +LOG_KILL_TRACEE ("entry instruction_pointer mismatch: %m" ); + } + ASSERT_TRUE(info.stack_pointer) { +LOG_KILL_TRACEE ("entry stack_pointer mismatch: %m" ); + } + + ASSERT_LT(ptrace_stop, ARRAY_SIZE(expected_nr)) { +LOG_KILL_TRACEE ("ptrace stop overflow" ); + } + ASSERT_FALSE(info.seccomp.nr == __NR_exit_group && info.seccomp.args[0] == 1) { +LOG_KILL_TRACEE ("tracee error: %s" , + testname[ptrace_stop]); + } + ASSERT_EQ(info.seccomp.nr, expected_nr[ptrace_stop]) { +LOG_KILL_TRACEE ("syscall nr mismatch" ); + } +const struct ptrace_syscall_info seccomp_info = info; + +switch (ptrace_stop) { +case 0 : +case 4 : +break ; +case 1 : + info.seccomp.nr = __NR_getppid; +break ; +case 2 : + info.op =PTRACE_SYSCALL_INFO_EXIT ; + info.exit.rval =41 ; + info.exit.is_error =0 ; +ASSERT_EQ (0 ,sys_ptrace (PTRACE_SET_SYSCALL_INFO , + tracee_pid, info_size, + (uintptr_t) &info)) { +LOG_KILL_TRACEE ("PTRACE_SET_SYSCALL_INFO: %m" ); + } +/* the syscall cannot be changed anymore once skipped */ +ASSERT_EQ (-1 ,sys_ptrace (PTRACE_SET_SYSCALL_INFO , + tracee_pid, info_size, + (uintptr_t) &seccomp_info)) { +LOG_KILL_TRACEE ("PTRACE_SET_SYSCALL_INFO after skip succeeded" ); + } + ASSERT_EQ(EINVAL, errno) { +LOG_KILL_TRACEE ("PTRACE_SET_SYSCALL_INFO after skip: %m" ); + } +/* but its return value can */ + info.exit.rval =42 ; +break ; +case 3 : + info.op =PTRACE_SYSCALL_INFO_EXIT ; + info.exit.rval = -ESRCH ; + info.exit.is_error =1 ; +break ; + } + +ASSERT_EQ (0 ,sys_ptrace (PTRACE_SET_SYSCALL_INFO , tracee_pid, + info_size, (uintptr_t) &info)) { +LOG_KILL_TRACEE ("PTRACE_SET_SYSCALL_INFO: %m" ); + } + + ASSERT_EQ(0, sys_ptrace(PTRACE_CONT, tracee_pid, 0, 0)) { +LOG_KILL_TRACEE ("PTRACE_CONT: %m" ); + } + } +} +TEST_HARNESS_MAIN
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v8 3/3] selftests/ptrace: add a test case for PTRACE_SET_SYSCALL_INFO syscall skipping
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] selftests/ptrace: tracee ASSERT_EQ() traps on write() before _exit() - [Medium]…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] selftests/ptrace: tracee ASSERT_EQ() traps on write() before _exit()
- [Medium] selftests/ptrace: set_syscall_info_seccomp() fails instead of skipping --
commit cc1ccfdbe8397159a506a6d7612652b6af1ea5d4
Author: Renzo Davoli <[email protected]>
selftests/ptrace: add a test case for PTRACE_SET_SYSCALL_INFO syscall skipping
This commit adds a seccomp test case in set_syscall_info.c to verify PTRACE_SET_SYSCALL_INFO syscall skipping semantics, including setting return values, error values, and preventing syscall changes after skipping.
Quoted 4 lines
tools/testing/selftests/ptrace/set_syscall_info.c +0
-0
[ ... ]
Quoted 21 lines
tools/testing/selftests/ptrace/set_syscall_info.c +17
-0@@ -516,4 +542,196 @@ TEST(set_syscall_info)ASSERT_EQ (ptrace_stop,ARRAY_SIZE (si) *2 ); } +TEST (set_syscall_info_seccomp) +{ + tracer_pid =getpid (); + tracee_pid =fork (); + + ASSERT_LE(0, tracee_pid) { +TH_LOG ("fork: %m" ); + } + +/* tracee */ +if (tracee_pid ==0 ) { + tracee_pid =getpid (); + ASSERT_EQ(0, sys_ptrace(PTRACE_TRACEME, 0, 0, 0)) { +TH_LOG ("PTRACE_TRACEME: %m" ); + } + ASSERT_EQ(0, kill(tracee_pid, SIGSTOP)) { +/* cannot happen */
tools/testing/selftests/ptrace/set_syscall_info.c TEST(set_syscall_info) · line 571
Quoted 10 lines
tools/testing/selftests/ptrace/set_syscall_info.c +10
-0+TH_LOG ("kill SIGSTOP: %m" ); + } + + ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) { +TH_LOG ("prctl: %m" ); + } +ASSERT_EQ (0 ,sys_seccomp (SECCOMP_SET_MODE_FILTER ,0 , + (void *) &seccomp_prog)) { +TH_LOG ("seccomp: %m" ); + }
[Severity: Medium]
Should set_syscall_info_seccomp skip the test instead of failing when sys_seccomp(SECCOMP_SET_MODE_FILTER, ...) is not supported by the kernel or architecture?
On kernels built without CONFIG_SECCOMP_FILTER (or on architectures that support PTRACE_SET_SYSCALL_INFO via CONFIG_HAVE_ARCH_TRACEHOOK but lack HAVE_ARCH_SECCOMP_FILTER), sys_seccomp() returns -1 with ENOSYS or EINVAL.
Because this ASSERT_EQ() runs inside the already-ptraced child, a failure invokes __bail() -> abort(), which raises SIGABRT in the tracee and causes the tracer to fail with "unexpected stop, wait status 0x67f" instead of skipping the test via SKIP().
Quoted 26 lines
tools/testing/selftests/ptrace/set_syscall_info.c +26
-0+ +/* run getpid unmodified */ + ASSERT_EQ(tracee_pid, getpid()) { +_exit (1 ); + } + +/* run getppid instead of getpid */ + ASSERT_EQ(tracer_pid, getpid()) { +_exit (1 ); + } + +/* skip getpid and return 42 */ + ASSERT_EQ(42, getpid()) { +_exit (1 ); + } + +/* skip getpid and fail with ESRCH */ + errno =0 ; + ASSERT_EQ(-1, syscall(__NR_getpid)) { +_exit (1 ); + } + ASSERT_EQ(ESRCH, errno) { +_exit (1 ); + } +_exit (0 ); + }
[ ... ]
tools/testing/selftests/ptrace/set_syscall_info.c TEST(set_syscall_info) · line 688
Quoted 10 lines
tools/testing/selftests/ptrace/set_syscall_info.c +10
-0+ ASSERT_LT(ptrace_stop, ARRAY_SIZE(expected_nr)) { +LOG_KILL_TRACEE ("ptrace stop overflow" ); + } + ASSERT_FALSE(info.seccomp.nr == __NR_exit_group && info.seccomp.args[0] == 1) { +LOG_KILL_TRACEE ("tracee error: %s" , + testname[ptrace_stop]); + } + ASSERT_EQ(info.seccomp.nr, expected_nr[ptrace_stop]) { +LOG_KILL_TRACEE ("syscall nr mismatch" ); + }
[Severity: Medium]
If any of the getpid() checks in the tracee fail, will the tracee ever reach _exit(1) to trigger the __NR_exit_group diagnostic check here?
In tools/testing/selftests/kselftest_harness.h, ASSERT_EQ() expands to __EXPECT(), which logs to unbuffered stderr before running the optional handler block:
#define __EXPECT(_expected, _expected_str, _seen, _seen_str, _t, _assert) do { \
...
__TH_LOG("Expected %s (%ju) %s %s (%ju)", \
_expected_str, __exp_print, #_t, \
_seen_str, __seen_print); \...
} while (0); OPTIONAL_HANDLER(_assert)
Since seccomp_filter intercepts all syscalls other than __NR_restart_syscall with SECCOMP_RET_TRACE, the fprintf(stderr, ...) inside __TH_LOG() triggers a PTRACE_EVENT_SECCOMP stop on __NR_write before the { _exit(1); } block is reached.
As a result, the tracer wakes up seeing info.seccomp.nr == __NR_write rather than __NR_exit_group, bypasses the testname[ptrace_stop] diagnostic, fails at ASSERT_EQ(info.seccomp.nr, expected_nr[ptrace_stop]) with "syscall nr mismatch", and kills the tracee via LOG_KILL_TRACEE(). Plain if (...) _exit(1); checks in the tracee (similar to TEST(set_syscall_info)) would avoid issuing extra syscalls before _exit(1).
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3