seccomp: SECCOMP_IOCTL_NOTIF_PIN_ARGS for race-free unotify
Subsystems (13)
cover seccomp: SECCOMP_IOCTL_NOTIF_PIN_ARGS for race-free unotify
From: Cong Wang <[email protected]>
This RFC introduces SECCOMP_IOCTL_NOTIF_PIN_ARGS, a new ioctl on the seccomp user-notification listener that lets an unprivileged supervisor atomically snapshot pointer-arg payloads from a trapped task and bind those snapshots to the task's resumed syscall body. It closes the documented TOCTOU race that today makes content-aware policy on SECCOMP_USER_NOTIF_FLAG_CONTINUE unsafe for unprivileged supervisors.
Posting as RFC because the UAPI shape, the consumption-hook placement, and the v1 vs v2 cut are all design choices that benefit from review before a non-RFC submission.
## Motivation
seccomp_unotify(2) lets a supervisor inspect a trapped task's syscall arguments and either deny, allow, or CONTINUE the syscall. CONTINUE re-runs the syscall body in the trapped task, which re-fetches every pointer argument from user memory. A sibling thread or CLONE_VM peer in the trapped task's address space can mutate that memory between the supervisor's process_vm_readv() and the kernel's re-read, turning any policy that examined the argument into a check on already-stale bytes. The seccomp_unotify(2) man page documents this race explicitly.
There is no race-free workaround for unprivileged supervisors today:
- ptrace and /proc/pid/mem are not available to them;
- process_vm_readv into a userspace buffer doesn't help, because
the kernel will re-read user memory regardless on CONTINUE;
- SECCOMP_IOCTL_NOTIF_ADDFD only solves the fd-substitution case, not the content-of-pointer-arg case.
The result is that unprivileged seccomp supervisors -- which are the target audience of seccomp_unotify(2) in the first place -- cannot implement content-aware allow policies. They can only deny or unconditionally allow. Anything that depends on the actual contents of a path, sockaddr, argv, or write buffer is unsafe.
## Concrete user: Sandlock
Sandlock <https://github.com/multikernel/sandlock> is a process-based unprivileged sandbox for AI agents, built on seccomp_unotify(2). At sandbox setup the agent process installs a seccomp filter with SECCOMP_FILTER_FLAG_NEW_LISTENER and hands the listener fd to a Sandlock supervisor; the supervisor then drives each filtered syscall via SECCOMP_IOCTL_NOTIF_RECV and replies with either an injected errno or SECCOMP_USER_NOTIF_FLAG_CONTINUE. This is what lets Sandlock confine an AI-agent process (coding agents, tool-using agents) to the filesystem and network surface its operator authorized, without root, a container runtime, or virtualization.
AI-agent workloads routinely fork helpers, exec compilers and language runtimes, and follow user- or model-controlled paths, so every content-aware policy decision the supervisor makes depends on the contents of syscall pointer arguments -- "allow open(path) iff path is below /allowed/", "allow bind(addr) iff addr->sun_path is in the permitlist", "allow execve(filename, argv) iff argv[0] is one of N approved binaries". Each of these is implemented as a CONTINUE reply, and CONTINUE re-fetches the pointer args from the trapped task's user memory -- which is where the documented TOCTOU race bites.
Today Sandlock has to either run privileged (defeating the unprivileged-sandbox model) or accept the TOCTOU race (defeating the guarantee against an adversarial agent that spawns a sibling thread to mutate the buffer between the supervisor's inspection and the kernel's re-read on CONTINUE). PIN_ARGS gives it the missing primitive without either compromise.
This is a general primitive, not a Sandlock-specific feature, but having a concrete user with a real and growing threat model (LLM agents executing untrusted or partially-trusted code in-process) should help anchor the design discussion.
## Approach
PIN_ARGS is a per-notification ioctl. The supervisor describes which register slots to snapshot and what shape each one is. The kernel walks the trapped task's mm under the existing remote-mm primitives (access_remote_vm, copy_remote_vm_str), copies the bytes into kernel- owned buffers, and stamps the snapshot onto the trapped task. On SECCOMP_USER_NOTIF_FLAG_CONTINUE_PINNED, the kernel's syscall fetch points consume from the kernel buffer instead of re-reading user memory.
Three v1 shapes:
- SECCOMP_PIN_FIXED (sockaddr, single-buffer read/write)
- SECCOMP_PIN_CSTRING (paths)
- SECCOMP_PIN_CSTRING_ARRAY (argv, envp)
Each per-arg copy is bounded by max_bytes; total cumulative bytes per request are bounded at a hardcoded 1 MiB. Allocations use GFP_KERNEL_ACCOUNT so the trapped task's memcg pays the cost.
The pin is one-shot: cleared on the trapped task's next return-to- userspace via task_work, with fallback paths for task exit, listener release, and explicit discard (CONTINUE without CONTINUE_PINNED). The syscall number is captured at pin time and verified at consumption, so a signal-handler-issued syscall during -ERESTART* resolution will not consume the pin.
Pin orchestration uses a three-phase lock dance: validate the notif and snapshot register args under filter->notify_lock, walk the trapped task's mm without locks, then re-validate and attach the snapshot. The walker uses primitives the kernel already uses for arg fetch (copy_remote_vm_str, getname_kernel, copy_string_kernel, iov_iter_kvec), so consumption sites are minimally invasive.
## Why copy and not page-pinning
Page-level FOLL_PIN doesn't solve content TOCTOU: the trapped task (or its CLONE_VM peer) is the owner of the mm and can write through the same mapping. There is no kernel primitive for "freeze the contents of these user pages." Copying at decision time is the only way to guarantee the bytes the supervisor inspected equal the bytes the kernel acts on.
The kernel already does this copy in syscall bodies today -- getname(), copy_strings(), move_addr_to_kernel(), copy_from_iter() for ITER_UBUF. PIN_ARGS shifts when that copy happens (at supervisor decision time) and re-points the syscall fetch points at the snapshot. Net new copies per syscall: zero.
## Why unprivileged
PIN_ARGS is gated by listener-fd possession, which is itself a capability scoped by file-descriptor ownership and SCM_RIGHTS passing. The supervisor already has equivalent remote-mm read access via process_vm_readv() (subject to the same ptrace_may_access checks). NO_NEW_PRIVS, required for unprivileged seccomp filter installation, blocks the obvious execve escalation. The DoS surface is bounded by the 1 MiB per-request cap, the one-shot lifetime, and at-most-one-pin-per-trapped-task, with memcg accounting on top.
Requiring CAP_SYS_PTRACE would render PIN_ARGS useless for its only real audience; privileged supervisors already have ptrace and /proc/pid/mem.
## What's NOT covered in v1
- Vector I/O (readv/writev) -- needs per-iovec pin descriptors, intentional v2.
- Nested-pointer payloads (sendmsg msghdr, futex_waitv) -- same.
- Per-iter consumption hooks beyond getname_flags, move_addr_to_kernel, copy_strings, and import_ubuf. Other syscall fetch sites that re-read user memory still race; v1 covers the four most common cases (path, sockaddr, argv/envp, single-buffer read/write) which together cover the bulk of practical unprivileged-sandbox policies.
## Patches
[PATCH 1/3] seccomp: kernel implementation
(UAPI, walker, orchestrator, four consumption hooks, one-shot lifecycle)
[PATCH 2/3] selftests/seccomp: end-to-end coverage
(10 cases across all three shapes + lifecycle)
[PATCH 3/3] Documentation: seccomp_filter.rst
("Pinned arguments" section)
## Testing
The selftest binary covers all three v1 shapes against real syscalls (bind, openat, execve, write), plus negative paths (CONTINUE without PINNED, double pin, mismatched flags) and the lifecycle (post- syscall clear, SIGKILL teardown). All ten cases pass on x86_64.
Cong Wang (3):
seccomp: add SECCOMP_IOCTL_NOTIF_PIN_ARGS to close the unotify TOCTOU
race
selftests/seccomp: add seccomp_pin_args end-to-end coverage
Documentation: seccomp: document SECCOMP_IOCTL_NOTIF_PIN_ARGS
.../userspace-api/seccomp_filter.rst | 76 ++ MAINTAINERS | 2 + fs/exec.c | 63 ++ fs/namei.c | 19 + fs/read_write.c | 8 +- include/linux/mm.h | 2 +- include/linux/seccomp.h | 35 + include/linux/seccomp_types.h | 33 + include/uapi/linux/seccomp.h | 73 ++ kernel/Makefile | 1 + kernel/exit.c | 1 + kernel/fork.c | 5 + kernel/seccomp.c | 189 +++- kernel/seccomp_pin.c | 453 +++++++++ kernel/seccomp_pin.h | 109 +++ lib/iov_iter.c | 22 + mm/memory.c | 4 +- mm/nommu.c | 4 +- net/socket.c | 16 + tools/testing/selftests/seccomp/.gitignore | 1 + tools/testing/selftests/seccomp/Makefile | 2 +- .../selftests/seccomp/seccomp_pin_args.c | 857 ++++++++++++++++++ 22 files changed, 1961 insertions(+), 14 deletions(-) create mode 100644 kernel/seccomp_pin.c create mode 100644 kernel/seccomp_pin.h create mode 100644 tools/testing/selftests/seccomp/seccomp_pin_args.c
--
2.43.0
RFC PATCH 1/3 seccomp: add SECCOMP_IOCTL_NOTIF_PIN_ARGS to close the unotify TOCTOU race
From: Cong Wang <[email protected]>
seccomp_unotify(2) leaves a documented TOCTOU window for unprivileged
supervisors: a sibling thread or CLONE_VM peer can mutate pointer-arg buffers between the supervisor's process_vm_readv() and the kernel's re-read on SECCOMP_USER_NOTIF_FLAG_CONTINUE. ptrace()/proc/pid/mem are not available to unprivileged supervisors, so today there is no race-free path for argument-content policy on CONTINUE.
This patch adds SECCOMP_IOCTL_NOTIF_PIN_ARGS, which atomically copies designated pointer-arg payloads from the trapped task's address space into kernel-owned buffers and binds those buffers to the task's next syscall execution. On SECCOMP_USER_NOTIF_FLAG_CONTINUE_PINNED, the syscall-body fetch points consume from the kernel buffer instead of re-reading user memory; mutations after PIN_ARGS returns have no effect.
Three v1 shapes are supported: a fixed-size copy (sockaddr, single- buffer write/read content), a NUL-bounded C string (paths), and a NULL-terminated array of C strings (argv/envp). Each per-arg descriptor caps copy size; total cumulative bytes per request are bounded at a hardcoded 1 MiB. Pinned-buffer allocations are tagged GFP_KERNEL_ACCOUNT so the trapped task's memcg pays the cost.
Pin orchestration uses a three-phase lock dance: validate the notif and snapshot register args under the filter notify lock, walk the trapped task's mm without locks, then re-validate and attach the snapshot. The pin is one-shot: a task_work clears it on the next return-to-userspace after the resumed syscall body completes, with fallback paths for task exit, listener release, and explicit discard (CONTINUE without CONTINUE_PINNED). The syscall number is captured at pin time and verified at consumption so a signal-handler-issued syscall during -ERESTART* resolution will not consume the pin.
--- MAINTAINERS | 2 + fs/exec.c | 63 +++++ fs/namei.c | 19 ++ fs/read_write.c | 8 +- include/linux/mm.h | 2 +- include/linux/seccomp.h | 35 +++ include/linux/seccomp_types.h | 33 +++ include/uapi/linux/seccomp.h | 73 ++++++ kernel/Makefile | 1 + kernel/exit.c | 1 + kernel/fork.c | 5 + kernel/seccomp.c | 189 +++++++++++++- kernel/seccomp_pin.c | 453 ++++++++++++++++++++++++++++++++++ kernel/seccomp_pin.h | 109 ++++++++ lib/iov_iter.c | 22 ++ mm/memory.c | 4 +- mm/nommu.c | 4 +- net/socket.c | 16 ++ 18 files changed, 1026 insertions(+), 13 deletions(-) create mode 100644 kernel/seccomp_pin.c create mode 100644 kernel/seccomp_pin.h
MAINTAINERS +2 -0
@@ -24086,6 +24086,8 @@ F: Documentation/userspace-api/seccomp_filter.rst F: include/linux/seccomp.h F: include/uapi/linux/seccomp.h F: kernel/seccomp.c +F: kernel/seccomp_pin.c +F: kernel/seccomp_pin.h F: tools/testing/selftests/kselftest_harness.h F: tools/testing/selftests/kselftest_harness/ F: tools/testing/selftests/seccomp/*
fs/exec.c +63 -0
81 lines not on this page · raw message
fs/namei.c +19 -0
@@ -30,6 +30,7 @@#include <linux/syscalls.h> #include <linux/mount.h> #include <linux/audit.h> +#include <linux/seccomp.h> #include <linux/capability.h> #include <linux/file.h> #include <linux/fcntl.h>
@@ -222,6 +223,24 @@ do_getname(const char __user *filename, int flags, bool incomplete)struct filename *getname_flags (const char __user *filename,int flags) { +const struct seccomp_pinned_arg *pin; + +/* +* If a seccomp supervisor pinned this path via PIN_ARGS and sent +* CONTINUE_PINNED, build the struct filename from the kernel-side +* snapshot instead of re-reading user memory. The pinned buffer +* is NUL-terminated by copy_remote_vm_str() in the walker, so +* getname_kernel() can consume it directly. +* +* The empty-path-with-LOOKUP_EMPTY policy is handled here because +* getname_kernel() does not reject empty strings. +*/ + pin =seccomp_pin_lookup_current ((u64 )(uintptr_t )filename); +if (pin && pin->kind ==SECCOMP_PIN_CSTRING ) { +if (pin->size <=1 && !(flags &LOOKUP_EMPTY )) +return ERR_PTR (-ENOENT ); +return getname_kernel (pin->data); + }return do_getname (filename, flags, false); }
fs/read_write.c +6 -2
@@ -488,7 +488,9 @@ static ssize_t new_sync_read(struct file *filp, char __user *buf, size_t len, loinit_sync_kiocb (&kiocb, filp); kiocb.ki_pos = (ppos ? *ppos :0 ); -iov_iter_ubuf (&iter,ITER_DEST , buf, len); + ret =import_ubuf (ITER_DEST , buf, len, &iter); +if (unlikely (ret)) +return ret; ret = filp->f_op->read_iter (&kiocb, &iter);BUG_ON (ret == -EIOCBQUEUED );
@@ -590,7 +592,9 @@ static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_tinit_sync_kiocb (&kiocb, filp); kiocb.ki_pos = (ppos ? *ppos :0 ); -iov_iter_ubuf (&iter,ITER_SOURCE , (void __user *)buf, len); + ret =import_ubuf (ITER_SOURCE , (void __user *)buf, len, &iter); +if (unlikely (ret)) +return ret; ret = filp->f_op->write_iter (&kiocb, &iter);BUG_ON (ret == -EIOCBQUEUED );
include/linux/mm.h +1 -1
@@ -3187,7 +3187,7 @@ extern int access_process_vm(struct task_struct *tsk, unsigned long addr,extern int access_remote_vm (struct mm_struct *mm,unsigned long addr,void *buf,int len,unsigned int gup_flags); -#ifdef CONFIG_BPF_SYSCALL +#if defined(CONFIG_BPF_SYSCALL ) || defined(CONFIG_SECCOMP_FILTER )extern int copy_remote_vm_str (struct task_struct *tsk,unsigned long addr,void *buf,int len,unsigned int gup_flags);#endif
include/linux/seccomp.h +35 -0
@@ -75,6 +75,35 @@ 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); +extern void seccomp_clear_pinned_args (struct task_struct *tsk); + +/** +* seccomp_pin_lookup_current - find a live PIN_ARGS snapshot for current(). +* @user_addr: the userspace address the syscall body is about to read. +* +* Called from syscall fetch points (getname_flags, copy_strings, +* move_addr_to_kernel, import_ubuf). Returns a pinned-arg entry whose +* @data / @size the caller may consume in place of re-reading user +* memory, or NULL if there is no live snapshot, the current syscall +* does not match the one captured at pin time, or no entry matches +* @user_addr. +* +* Safe to call lockless: current owns its seccomp.pinned_args field +* once the PIN_ARGS orchestrator has installed it via WRITE_ONCE. +*/ +const struct seccomp_pinned_arg *seccomp_pin_lookup_current (u64 user_addr); + +/** +* seccomp_pin_kvec_for - return a stable kvec for the given pin entry. +* @pin: a pin returned by seccomp_pin_lookup_current(); must belong +* to the current task. +* +* The returned pointer references kvec storage that outlives the pin +* (freed at syscall exit), suitable for iov_iter_kvec() callers whose +* iov_iter consumes after the wrapping function returns. +*/ +struct kvec ; +const struct kvec *seccomp_pin_kvec_for (const struct seccomp_pinned_arg *pin);#else /* CONFIG_SECCOMP_FILTER */ static inline void seccomp_filter_release (struct task_struct *tsk) {
@@ -84,6 +113,12 @@ static inline void get_seccomp_filter(struct task_struct *tsk) {return ; } +static inline void seccomp_clear_pinned_args (struct task_struct *tsk) { } +static inline const struct seccomp_pinned_arg * +seccomp_pin_lookup_current (u64 user_addr) {return NULL ; } +struct kvec ; +static inline const struct kvec * +seccomp_pin_kvec_for (const struct seccomp_pinned_arg *pin) {return NULL ; }#endif /* CONFIG_SECCOMP_FILTER */ #if defined(CONFIG_SECCOMP_FILTER ) && defined(CONFIG_CHECKPOINT_RESTORE )
include/linux/seccomp_types.h +33 -0
@@ -7,6 +7,34 @@#ifdef CONFIG_SECCOMP struct seccomp_filter ; +struct seccomp_pinned_args ; + +#define SECCOMP_PIN_MAX_ARGS 6 + +/** +* struct seccomp_pinned_arg - one kernel-owned snapshot of a user-pointer arg. +* @user_addr: the original userspace address (key for lookup at consumption). +* @size: bytes actually populated in @data. +* @arg_idx: syscall register slot 0..5. +* @kind: one of SECCOMP_PIN_*. +* @data: kvmalloc'd buffer holding the snapshotted bytes. +* +* Consumption sites (getname_flags, copy_strings, move_addr_to_kernel, +* import_ubuf) inspect @data and @size after a successful +* seccomp_pin_lookup_current(). For sites that need a stable kvec +* pointer outliving the call (import_ubuf -> vfs_write iter), +* seccomp_pin_kvec_for() returns a kvec stored alongside the pin +* with matching lifetime. +*/ +struct seccomp_pinned_arg { +u64 user_addr; +u32 size; +u8 arg_idx; +u8 kind; +u16 _pad; +void *data; +}; +/** * struct seccomp - the state of a seccomp'ed process *
@@ -18,11 +46,16 @@ struct seccomp_filter;* * @filter must only be accessed from the context of current as there * is no read locking. +* @pinned_args: NULL except during a PIN_ARGS window. Owned by the trapped +* task itself; populated by SECCOMP_IOCTL_NOTIF_PIN_ARGS, consumed +* on CONTINUE_PINNED, freed at syscall exit, listener release, or +* task exit. See kernel/seccomp_pin.c. */ struct seccomp {int mode;atomic_t filter_count;struct seccomp_filter *filter; +struct seccomp_pinned_args *pinned_args; };#else
include/uapi/linux/seccomp.h +73 -0
77 lines not on this page · raw message
kernel/Makefile +1 -0
@@ -106,6 +106,7 @@ obj-$(CONFIG_LOCKUP_DETECTOR) += watchdog.o obj-$(CONFIG_HARDLOCKUP_DETECTOR_BUDDY) += watchdog_buddy.o obj-$(CONFIG_HARDLOCKUP_DETECTOR_PERF) += watchdog_perf.o obj-$(CONFIG_SECCOMP) += seccomp.o +obj-$(CONFIG_SECCOMP_FILTER) += seccomp_pin.o obj-$(CONFIG_RELAY) += relay.o obj-$(CONFIG_SYSCTL) += utsname_sysctl.o obj-$(CONFIG_TASK_DELAY_ACCT) += delayacct.o
kernel/exit.c +1 -0
@@ -917,6 +917,7 @@ void __noreturn do_exit(long code)exit_signals (tsk);/* sets PF_EXITING */ seccomp_filter_release (tsk); +seccomp_clear_pinned_args (tsk);acct_update_integrals (tsk); group_dead =atomic_dec_and_test (&tsk->signal->live);
kernel/fork.c +5 -0
@@ -1763,6 +1763,11 @@ static void copy_seccomp(struct task_struct *p)/* Ref-count the new filter user, and assign it. */ get_seccomp_filter (current); p->seccomp = current->seccomp; +/* +* pinned_args is a per-trapped-task transient that belongs to the +* outstanding notification on the parent (if any). Don't inherit it. +*/ + p->seccomp.pinned_args =NULL ;/* * Explicitly enable no_new_privs here in case it got set
kernel/seccomp.c +183 -6
265 lines not on this page · raw message
kernel/seccomp_pin.c new file +453 -0
453 lines not on this page · raw message
kernel/seccomp_pin.h new file +109 -0
109 lines not on this page · raw message
lib/iov_iter.c +22 -0
@@ -9,6 +9,7 @@#include <linux/vmalloc.h> #include <linux/splice.h> #include <linux/compat.h> +#include <linux/seccomp.h> #include <linux/scatterlist.h> #include <linux/instrumented.h> #include <linux/iov_iter.h>
@@ -1444,8 +1445,29 @@ EXPORT_SYMBOL(import_iovec);int import_ubuf (int rw,void __user *buf,size_t len,struct iov_iter *i) { +const struct seccomp_pinned_arg *pin; +const struct kvec *kvec; +if (len >MAX_RW_COUNT ) len =MAX_RW_COUNT ; + +/* +* Pinned by a seccomp PIN_ARGS supervisor on this task? Build the +* iov_iter over the kernel snapshot rather than re-reading user +* memory. The kvec storage is owned by current->seccomp.pinned_args +* and lives until syscall exit, so it outlasts @i's consumption. +*/ + pin =seccomp_pin_lookup_current ((u64 )(uintptr_t )buf); +if (pin && pin->kind ==SECCOMP_PIN_FIXED ) { + kvec =seccomp_pin_kvec_for (pin); +if (kvec) { +size_t n =min_t (size_t, len, pin->size); + +iov_iter_kvec (i, rw, kvec,1 , n); +return 0 ; + } + } +if (unlikely (!access_ok (buf, len)))return -EFAULT ;
mm/memory.c +2 -2
@@ -7168,7 +7168,7 @@ int access_process_vm(struct task_struct *tsk, unsigned long addr, }EXPORT_SYMBOL_GPL (access_process_vm); -#ifdef CONFIG_BPF_SYSCALL +#if defined(CONFIG_BPF_SYSCALL ) || defined(CONFIG_SECCOMP_FILTER )/* * Copy a string from another process's address space as given in mm. * If there is any error return -EFAULT.
@@ -7286,7 +7286,7 @@ int copy_remote_vm_str(struct task_struct *tsk, unsigned long addr, return ret; }EXPORT_SYMBOL_GPL (copy_remote_vm_str); -#endif /* CONFIG_BPF_SYSCALL */ +#endif /* CONFIG_BPF_SYSCALL || CONFIG_SECCOMP_FILTER */ /* * Print the name of a VMA.
mm/nommu.c +2 -2
@@ -1711,7 +1711,7 @@ int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, in }EXPORT_SYMBOL_GPL (access_process_vm); -#ifdef CONFIG_BPF_SYSCALL +#if defined(CONFIG_BPF_SYSCALL ) || defined(CONFIG_SECCOMP_FILTER )/* * Copy a string from another process's address space as given in mm. * If there is any error return -EFAULT.
@@ -1788,7 +1788,7 @@ int copy_remote_vm_str(struct task_struct *tsk, unsigned long addr, return ret; }EXPORT_SYMBOL_GPL (copy_remote_vm_str); -#endif /* CONFIG_BPF_SYSCALL */ +#endif /* CONFIG_BPF_SYSCALL || CONFIG_SECCOMP_FILTER */ /** * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
net/socket.c +16 -0
@@ -82,6 +82,7 @@#include <linux/compat.h> #include <linux/kmod.h> #include <linux/audit.h> +#include <linux/seccomp.h> #include <linux/wireless.h> #include <linux/nsproxy.h> #include <linux/magic.h>
@@ -248,10 +249,25 @@ static const struct net_proto_family __rcu *net_families[NPROTO] __read_mostly;int move_addr_to_kernel (void __user *uaddr,int ulen,struct sockaddr_storage *kaddr) { +const struct seccomp_pinned_arg *pin; +if (ulen <0 || ulen >sizeof (struct sockaddr_storage ))return -EINVAL ;if (ulen ==0 )return 0 ; + +/* If a seccomp supervisor pinned this sockaddr via PIN_ARGS and +* sent CONTINUE_PINNED, consume from the kernel snapshot instead +* of re-reading user memory. Closes the unotify TOCTOU. +*/ + pin =seccomp_pin_lookup_current ((u64 )(uintptr_t )uaddr); +if (pin) { +size_t n =min_t (size_t, (size_t )ulen, pin->size); + +memcpy (kaddr, pin->data, n); +return audit_sockaddr (ulen, kaddr); + } +if (copy_from_user (kaddr, uaddr, ulen))return -EFAULT ;return audit_sockaddr (ulen, kaddr);
Signature · 1 line
2.43.0
Christian Brauner
maintainerRe: [RFC PATCH 1/3] seccomp: add SECCOMP_IOCTL_NOTIF_PIN_ARGS to close the unotify TOCTOU race
fs/namei.c do_getname(const char __user *filename, int flags, bool incomplete) · line 243
Quoted 12 lines · Cong Wang
fs/namei.c +0
-0@@ -222,6 +223,24 @@ do_getname(const char __user *filename, int flags, bool incomplete)[ ... skip 15 lines ... ]
fs/namei.c +6
-0+ pin =seccomp_pin_lookup_current ((u64 )(uintptr_t )filename); +if (pin && pin->kind ==SECCOMP_PIN_CSTRING ) { +if (pin->size <=1 && !(flags &LOOKUP_EMPTY )) +return ERR_PTR (-ENOENT ); +return getname_kernel (pin->data); + }
Sorry, no. That's just not acceptable at all. We're not spraying "continue from snapshotted state" across the vfs and the kernel in general. This is just screaming for security issues. Anything that wants to do something remotely like this needs to come as generic abstraction where the syscall layer itself doesn't have to care at all about this. There are just so many corners where you run into issues with this.
Signature · 1 line
Christian Brauner <[email protected]>
Re: [RFC PATCH 1/3] seccomp: add SECCOMP_IOCTL_NOTIF_PIN_ARGS to close the unotify TOCTOU race
Quoted 13 lines · Christian Brauner
Quoted 11 lines · Cong Wang
fs/namei.c +0
-0@@ -222,6 +223,24 @@ do_getname(const char __user *filename, int flags, bool incomplete)[ ... skip 15 lines ... ]
fs/namei.c +5
-0+ pin =seccomp_pin_lookup_current ((u64 )(uintptr_t )filename); +if (pin && pin->kind ==SECCOMP_PIN_CSTRING ) { +if (pin->size <=1 && !(flags &LOOKUP_EMPTY )) +return ERR_PTR (-ENOENT ); +return getname_kernel (pin->data);
Quoted 8 lines
Quoted 1 line
+ }
Sorry, no. That's just not acceptable at all. We're not spraying "continue from snapshotted state" across the vfs and the kernel in general. This is just screaming for security issues. Anything that wants to do something remotely like this needs to come as generic abstraction where the syscall layer itself doesn't have to care at all about this. There are just so many corners where you run into issues with this.
You're right. Having every fetch site consult a per-task pin pointer is exactly the kind of cross-cutting awareness that doesn't scale.
How about the following direction instead?
Reshape the mechanism as a PTRACE_SYSCALL-style redirect, applied at the notification reply path. The supervisor describes:
struct seccomp_notif_inject {
__u64 id;
__u64 nr;
__u64 args[6];
__u64 buf; /* __user, kernel-input bytes */
__u32 buf_size;
__u32 args_in_buf_mask; /* bit i: args[i] is offset into buf */
};NOTIF_SEND with a new FLAG_INJECTED applies the redirect. The trapped task's nr/args registers are set via syscall_set_nr() and syscall_set_arguments() (the same primitives ptrace uses for syscall substitution today), and any arg flagged in args_in_buf_mask is satisfied from a kernel-side buffer rather than from the trapped task's mm. fs/, net/, mm/, lib/ get zero changes. The whole feature lives in a new kernel/seccomp_inject.c plus a small dispatcher in kernel/seccomp.c.
This is intentionally a strict subset of what ptrace can already do via PTRACE_POKEDATA + PTRACE_SETREGSET. This does not add a kernel capability; it provides a listener-fd-gated, syscall-whitelisted, narrower interface to that capability for unprivileged seccomp_unotify supervisors, where ptrace's privilege model and per-syscall overhead are not viable. SECCOMP_IOCTL_NOTIF_ADDFD set the precedent for this kind of narrow listener-fd interface to a ptrace-overlapping capability.
Thanks,
Cong
RFC PATCH 2/3 selftests/seccomp: add seccomp_pin_args end-to-end coverage
In reply to Cong WangFrom: Cong Wang <[email protected]>
Add a standalone selftest binary for SECCOMP_IOCTL_NOTIF_PIN_ARGS exercising all three v1 shapes (fixed/cstring/cstring-array) on real syscalls (bind, openat, execve, write), plus negative paths (CONTINUE without PINNED, double pin, mismatched flags) and the single-shot lifecycle (post-syscall clear, SIGKILL teardown).
The tests use MAP_SHARED to mirror the documented CLONE_VM peer
attack: the supervisor pins the trapped child's pointer arg, the parent mutates the underlying bytes, and the test verifies the kernel acted on the pinned snapshot rather than the mutation.
Lives in its own file rather than seccomp_bpf.c since the feature is unrelated to the BPF filter machinery.
--- tools/testing/selftests/seccomp/.gitignore | 1 + tools/testing/selftests/seccomp/Makefile | 2 +- .../selftests/seccomp/seccomp_pin_args.c | 857 ++++++++++++++++++ 3 files changed, 859 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/seccomp/seccomp_pin_args.c
tools/testing/selftests/seccomp/.gitignore +1 -0
@@ -1,3 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only seccomp_bpf seccomp_benchmark +seccomp_pin_args
tools/testing/selftests/seccomp/Makefile +1 -1
@@ -3,5 +3,5 @@ CFLAGS += -Wl,-no-as-needed -Wall $(KHDR_INCLUDES) LDFLAGS += -lpthread LDLIBS += -lcap -TEST_GEN_PROGS := seccomp_bpf seccomp_benchmark +TEST_GEN_PROGS := seccomp_bpf seccomp_benchmark seccomp_pin_args include ../lib.mk
tools/testing/selftests/seccomp/seccomp_pin_args.c new file +857 -0
857 lines not on this page · raw message
Signature · 1 line
2.43.0
RFC PATCH 3/3 Documentation: seccomp: document SECCOMP_IOCTL_NOTIF_PIN_ARGS
In reply to Cong WangFrom: Cong Wang <[email protected]>
Add a "Pinned arguments" section to the userspace API doc covering the motivation (closing the documented TOCTOU window for unprivileged supervisors), the pin/consume flow via SECCOMP_IOCTL_NOTIF_PIN_ARGS and SECCOMP_USER_NOTIF_FLAG_CONTINUE_PINNED, the three v1 shapes with their per-shape semantics, the single-shot lifecycle, the syscall_nr mismatch check, and the explicitly-not-covered cases left for follow-ups (vector I/O, nested-pointer payloads).
--- .../userspace-api/seccomp_filter.rst | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+)
Documentation/userspace-api/seccomp_filter.rst +76 -0
@@ -289,6 +289,82 @@ above in this document: all arguments being read from the tracee's memory should be read into the tracer's memory before any policy decisions are made. This allows for an atomic decision on syscall arguments. +Pinned arguments +---------------- + +For unprivileged supervisors, ``ptrace()``/``/proc/pid/mem`` are not +available, and reading the tracee's memory via ``process_vm_readv()`` +remains racy: a sibling thread or ``CLONE_VM`` peer can mutate the +buffer between supervisor read and the kernel's re-read on +``SECCOMP_USER_NOTIF_FLAG_CONTINUE``. ``SECCOMP_IOCTL_NOTIF_PIN_ARGS`` +closes that race by atomically copying designated pointer-arg payloads +from the tracee's address space into kernel-owned buffers, and binding +those buffers to the tracee's next-syscall execution. + +The supervisor receives a notification as today, then issues +``ioctl(SECCOMP_IOCTL_NOTIF_PIN_ARGS, &payload)`` with a +``struct seccomp_notif_pin_args`` describing which pointer-args to +snapshot. Each per-arg descriptor names a syscall register slot +(``arg_idx``, 0..5), one of three shapes (``SECCOMP_PIN_FIXED``, +``SECCOMP_PIN_CSTRING``, ``SECCOMP_PIN_CSTRING_ARRAY``), and a +``max_bytes`` cap. The kernel walks the trapped task's mm, copies +the bytes into kernel buffers, and writes them back to a supervisor- +provided byte buffer (``buf`` / ``buf_size``) plus per-arg metadata +(``actual_size``, ``buf_offset``, ``truncated``). + +To consume the snapshot on syscall re-execution, the supervisor sends +``ioctl(SECCOMP_IOCTL_NOTIF_SEND)`` with both +``SECCOMP_USER_NOTIF_FLAG_CONTINUE`` and +``SECCOMP_USER_NOTIF_FLAG_CONTINUE_PINNED`` set. The kernel's syscall +fetch points (``getname_flags``, ``copy_strings``, +``move_addr_to_kernel``, ``import_ubuf``) check +``current->seccomp.pinned_args`` and consume from the kernel buffer +instead of re-reading user memory; mutations to the original buffer +after ``PIN_ARGS`` returns have no effect. + +The pin is single-shot: it is cleared automatically when the trapped +task next returns to user mode after the resumed syscall body +completes, when the task exits, when the listener fd is closed, or +when the supervisor sends ``CONTINUE`` without ``CONTINUE_PINNED`` +(an explicit "I changed my mind" path). Subsequent traps require a +fresh ``PIN_ARGS`` for the new notification id. + +Per-shape semantics: + +* ``SECCOMP_PIN_FIXED`` copies exactly ``max_bytes`` from + ``args[arg_idx]``. Suitable for ``struct sockaddr`` (``bind``, + ``connect``, ``sendto``) and for ``write(fd, buf, count)`` (the + supervisor sets ``max_bytes = count`` from + ``seccomp_data.args[2]``). + +* ``SECCOMP_PIN_CSTRING`` walks to the trailing NUL, capped at + ``max_bytes``. The pinned buffer is always NUL-terminated; if the + cap was hit before the source NUL, ``truncated`` carries + ``SECCOMP_PIN_TRUNCATED_BYTES``. Suitable for paths + (``open``/``openat``/``execve`` filename, etc.). + +* ``SECCOMP_PIN_CSTRING_ARRAY`` walks a NULL-terminated pointer table + at ``args[arg_idx]`` and copies each non-NULL string. Suitable for + ``execve``'s argv and envp. Bounded by both ``max_bytes`` and + ``max_entries``. Result is packed as + ``[u32 count][u32 offsets[count]][u8 strings[]]``. + +The total cumulative ``max_bytes`` across all per-arg descriptors and +the supervisor-provided ``buf_size`` are each bounded at 1 MiB; this +is a hard-coded defensive ceiling, not a tunable. + +The kernel records the syscall number at pin time and verifies a +match at consumption: a signal handler running on the trapped task +during ``-ERESTART*`` resolution that issues an unrelated syscall +will not consume the pin. + +Cumulative scope of v1: ``SECCOMP_PIN_FIXED`` covers sockaddr and +single-buffer write content; ``SECCOMP_PIN_CSTRING`` covers paths; +``SECCOMP_PIN_CSTRING_ARRAY`` covers argv and envp. Vector I/O +(``readv``/``writev``) and nested-pointer payloads +(``sendmsg``/``recvmsg`` ``msghdr``, ``futex_waitv``) are not covered +in v1. + Sysctls =======
Signature · 1 line
2.43.0