PTRACE_SET_SYSCALL_INFO: add support for seccomp syscall skipping
Subsystems (2)
v4 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.
The API is designed to let tracers inspect and modify system call information in a simple, architecture-agnostic manner.
The current implementation only supports modifying the subset of system call information needed by strace: the system call number, arguments, and return value.
This patch set extends PTRACE_SET_SYSCALL_INFO with support for skipping a system call.
When a seccomp filter returns SECCOMP_RET_TRACE, the tracer receives, via PTRACE_GET_SYSCALL_INFO, a struct ptrace_syscall_info with op == PTRACE_SYSCALL_INFO_SECCOMP.
The tracer can skip the system call by setting the system call number to -1. However, the current PTRACE_SET_SYSCALL_INFO interface does not provide a way to specify the return value or error code that should be reported to the tracee after skipping the call.
This patchset extends 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.
System call suppression for PTRACE_SYSCALL_INFO_ENTRY is currently omitted because its implementation is architecture-dependent. On some architectures, the system call number and return value share the same register, making it difficult to suppress a system call without altering the return value. A portable implementation would require an audit of all supported architectures.
This patchset is a new version of the proposed patchset entitled:ptrace_set_syscall_info: add support for seccomp syscall skipping and instruction pointer modification
The patchset has been split in two:
syscall skipping(this)
instruction pointer modification (it will be updated soon)
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
Changes since the previous patchset v2:
* bugfix: skip_syscall init value
* fix comments
Changes in (previous patchset) v2:
* use PTRACE_SYSCALL_INFO_EXIT instead of a new tag
* fixed most of the comments from sashiko.dev
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
kernel/ptrace.c | 27 ++- .../selftests/ptrace/set_syscall_info.c | 175 +++++++++++++++++- 2 files changed, 196 insertions(+), 6 deletions(-)
Signature · 1 line
2.53.0
PATCH v4 1/2 ptrace: add PTRACE_SET_SYSCALL_INFO syscall skipping support
Extend 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.
System call suppression for PTRACE_SYSCALL_INFO_ENTRY is currently omitted because its implementation is architecture-dependent. On some architectures, the system call number and return value share the same register, making it difficult to suppress a system call without altering the return value. A portable implementation would require an audit of all supported architectures.
--- kernel/ptrace.c | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-)
kernel/ptrace.c +22 -5
@@ -1099,7 +1099,7 @@ ptrace_set_syscall_info_seccomp(struct task_struct *child, struct pt_regs *regs,static int ptrace_set_syscall_info_exit (struct task_struct *child,struct pt_regs *regs, -struct ptrace_syscall_info *info) +struct ptrace_syscall_info *info,bool skip_syscall) {long rval = info->exit.rval;
@@ -1111,6 +1111,9 @@ ptrace_set_syscall_info_exit(struct task_struct *child, struct pt_regs *regs,if (rval != info->exit.rval)return -ERANGE ; +if (skip_syscall) +syscall_set_nr (child, regs,-1 ); +if (info->exit.is_error)syscall_set_return_value (child, regs, rval,0 );else
@@ -1125,6 +1128,8 @@ ptrace_set_syscall_info(struct task_struct *child, unsigned long user_size, {struct pt_regs *regs =task_pt_regs (child);struct ptrace_syscall_info info; +int op; +bool skip_syscall = false;if (user_size <sizeof (info))return -EINVAL ;
@@ -1141,15 +1146,27 @@ 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) -return -EINVAL ; +/* +* Changing the type of the system call stop is not allowed, with the +* following exception: +* PTRACE_SYSCALL_INFO_SECCOMP can be changed to PTRACE_SYSCALL_INFO_EXIT +* to skip the system call +*/ + + op =ptrace_get_syscall_info_op (child); +if (op != info.op) { +if (info.op ==PTRACE_SYSCALL_INFO_EXIT && + op ==PTRACE_SYSCALL_INFO_SECCOMP ) + skip_syscall = true; +else +return -EINVAL ; + }switch (info.op) {case PTRACE_SYSCALL_INFO_ENTRY :return ptrace_set_syscall_info_entry (child, regs, &info);case PTRACE_SYSCALL_INFO_EXIT : -return ptrace_set_syscall_info_exit (child, regs, &info); +return ptrace_set_syscall_info_exit (child, regs, &info, skip_syscall);case PTRACE_SYSCALL_INFO_SECCOMP :return ptrace_set_syscall_info_seccomp (child, regs, &info);default :
Signature · 1 line
2.53.0
Re: [PATCH v4 1/2] ptrace: add PTRACE_SET_SYSCALL_INFO syscall skipping support
Quoted 9 lines · Renzo Davoli
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.
Quoted 5 lines
System call suppression for
PTRACE_SYSCALL_INFO_ENTRYis currently omitted because its implementation is architecture-dependent. On some architectures, the system call number and return value share the same register, making it difficult to suppress a system call without altering the return value. A portable implementation would require an audit of all supported architectures.
I suggest the following wording for the explanation why PTRACE_SYSCALL_INFO_ENTRY is not supported yet:
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 before the tracer can set a custom return value at the exit stop. A portable implementation of entry-based syscall suppression would require an audit of all supported architectures to ensure the tracer-provided return value is preserved.
Signature · 1 line
ldv
Oleg Nesterov
maintainerRe: [PATCH v4 1/2] ptrace: add PTRACE_SET_SYSCALL_INFO syscall skipping support
Quoted 12 lines · Dmitry V. Levin
Quoted 5 lines · Renzo Davoli
System call suppression for
PTRACE_SYSCALL_INFO_ENTRYis currently omitted because its implementation is architecture-dependent. On some architectures, the system call number and return value share the same register, making it difficult to suppress a system call without altering the return value. A portable implementation would require an audit of all supported architectures.I suggest the following wording for the explanation why
PTRACE_SYSCALL_INFO_ENTRYis not supported yet:
Quoted 6 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-ENOSYSbefore the tracer can set a custom return value at the exit stop.
Thanks! but looks a bit misleading or (quite possibly) I am confused...
At least for MIPS, I think it should something like
when a system call is skipped by setting the syscall number to -1
...
architecture entry path unconditionally overwrites the return value register with -ENOSYS
...
_after_ (or even if ) the tracer has already set a custom return value
No?
Oleg.
Re: [PATCH v4 1/2] ptrace: add PTRACE_SET_SYSCALL_INFO syscall skipping support
Quoted 28 lines · Oleg Nesterov
Quoted 12 lines · Dmitry V. Levin
Quoted 5 lines · Renzo Davoli
System call suppression for
PTRACE_SYSCALL_INFO_ENTRYis currently omitted because its implementation is architecture-dependent. On some architectures, the system call number and return value share the same register, making it difficult to suppress a system call without altering the return value. A portable implementation would require an audit of all supported architectures.I suggest the following wording for the explanation why
PTRACE_SYSCALL_INFO_ENTRYis not supported yet:Quoted 6 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-ENOSYSbefore the tracer can set a custom return value at the exit stop.Thanks! but looks a bit misleading or (quite possibly) I am confused...
At least for MIPS, I think it should something like
when a system call is skipped by setting the syscall number to -1
...
Quoted 8 lines
architecture entry path unconditionally overwrites the return value register with
-ENOSYS...
_after_ (or even if ) the tracer has already set a custom return value
No?
Sure, thanks for spotting. Consider the following edition:
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.
Signature · 1 line
ldv
Oleg Nesterov
maintainerRe: [PATCH v4 1/2] ptrace: add PTRACE_SET_SYSCALL_INFO syscall skipping support
Quoted 7 lines · Dmitry V. Levin
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.
Thanks! LGTM.
Oleg.
PATCH v4 2/2 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.
--- .../selftests/ptrace/set_syscall_info.c | 175 +++++++++++++++++- 1 file changed, 174 insertions(+), 1 deletion(-)
tools/testing/selftests/ptrace/set_syscall_info.c +174 -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,151 @@ 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()) { +TH_LOG ("getpid seccomp unchanged: %m" ); + } + +/* run getppid instead of getpid */ + ASSERT_EQ(tracer_pid, getpid()) { +TH_LOG ("getpid seccomp nr changes: %m" ); + } + +/* skip getpid and return 42 */ + ASSERT_EQ(42, getpid()) { +TH_LOG ("getpid skip set return value changes: %m" ); + } +_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" ); + } + +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, 4) { +LOG_KILL_TRACEE ("ptrace stop overflow" ); + } + +const unsigned int expected_nr[] = { + __NR_getpid, + __NR_getpid, + __NR_getpid, + __NR_exit_group + }; + + ASSERT_LT(ptrace_stop, ARRAY_SIZE(expected_nr)) { +LOG_KILL_TRACEE ("ptrace stop overflow" ); + } + ASSERT_EQ(info.seccomp.nr, expected_nr[ptrace_stop]) { +LOG_KILL_TRACEE ("syscall nr mismatch" ); + } +switch (ptrace_stop) { +case 0 : +case 3 : +break ; +case 1 : + info.seccomp.nr = __NR_getppid; +break ; +case 2 : + info.op =PTRACE_SYSCALL_INFO_EXIT ; + info.exit.rval =42 ; + info.exit.is_error =0 ; +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.53.0
Re: [PATCH v4 2/2] selftests/ptrace: add a test case for PTRACE_SET_SYSCALL_INFO syscall skipping
Quoted 2 lines · Renzo Davoli
Check whether
PTRACE_SET_SYSCALL_INFOsyscall skipping semantics implemented in the kernel matches userspace expectations.
[...]
tools/testing/selftests/ptrace/set_syscall_info.c TEST(set_syscall_info) · line 653
Quoted 3 lines
tools/testing/selftests/ptrace/set_syscall_info.c +3
-0+ ASSERT_LT(ptrace_stop, 4) { +LOG_KILL_TRACEE ("ptrace stop overflow" ); + }
This assertion is redundant given that ...
tools/testing/selftests/ptrace/set_syscall_info.c TEST(set_syscall_info) · line 664
Quoted 10 lines
tools/testing/selftests/ptrace/set_syscall_info.c +10
-0+const unsigned int expected_nr[] = { + __NR_getpid, + __NR_getpid, + __NR_getpid, + __NR_exit_group + }; + + ASSERT_LT(ptrace_stop, ARRAY_SIZE(expected_nr)) { +LOG_KILL_TRACEE ("ptrace stop overflow" ); + }
... there is a more reliable assertion here instead.
tools/testing/selftests/ptrace/set_syscall_info.c TEST(set_syscall_info) · line 671
Quoted 7 lines
tools/testing/selftests/ptrace/set_syscall_info.c +7
-0+ ASSERT_EQ(info.seccomp.nr, expected_nr[ptrace_stop]) { +LOG_KILL_TRACEE ("syscall nr mismatch" ); + } +switch (ptrace_stop) { +case 0 : +case 3 : +break ;
These two cases don't really do anything, feel free to drop them if you like, it's a matter of style preferences.
Signature · 1 line
ldv