Skip to contentSkip to conversation

um: introduce pidfd_mmap()/pidfd_munmap() syscalls

RFC PATCH In review Cong Wang 2 participants 8 messages Jul 10 – Jul 21

Subsystems (6)
lore
Share & tools
lore

Cong Wang

author
#0 lore raw

cover um: introduce pidfd_mmap()/pidfd_munmap() syscalls

This RFC adds two syscalls, pidfd_mmap() and pidfd_munmap(), that let a supervisor install or remove a mapping in another process's address space without target-side cooperation, and converts User Mode Linux to use them.

pidfd_mmap(int pidfd, struct pidfd_mmap_args *uargs, unsigned int flags) pidfd_munmap(int pidfd, unsigned long addr, unsigned long len)

The backing fd (if any) is resolved in the *caller's* fd table, mirroring pidfd_getfd()'s cross-task install model. Both are gated by ptrace_may_access(PTRACE_MODE_ATTACH_REALCREDS); LSM/fsnotify hooks run against the caller. pidfd_mmap() takes an extensible, size-versioned argument struct (clone3/openat2 style) since an mmap-shaped call exceeds the 6-argument syscall limit.

They build on the mm-side remote-install primitives vm_mmap_remote()/ vm_munmap_remote(), posted separately as:

https://lore.kernel.org/all/[email protected]/

UML is the motivating user. Its SKAS monitor previously installed guest mappings by driving the stub to execute mmap/munmap itself: in seccomp mode it passed the physmem fd to the stub over SCM_RIGHTS, and in ptrace mode it batched STUB_SYSCALL_MMAP/MUNMAP entries for the stub to run. pidfd_mmap() lets the monitor install mappings into the stub's mm directly, so the stub never touches the fd or executes mmap/munmap. With both modes converted, the entire stub-syscall batcher and the SCM_RIGHTS fd-passing become dead code and are removed (net ~530 lines). The ptrace conversion (patch 5) is included mainly to demonstrate the primitive's reach; the security caveats pidfd_mmap() does *not* address (e.g. a guest blocking SIGALRM to dodge scheduling) are preserved in stub.c.

As this is an RFC, the UML side _intentionally_ does not handle host
portability: it assumes a host kernel that provides pidfd_mmap() and deliberately omits any detection or fallback for older hosts (which would otherwise pick the ptrace path, or fail the seccomp probe). That belongs in a non-RFC version and is left out here to keep the series focused on demonstrating the impact on UML.

---

Cong Wang (6):
pidfd: add pidfd_mmap()/pidfd_munmap() syscalls
um: acquire a stub pidfd via CLONE_PIDFD in seccomp mode
um: install guest mappings via pidfd_mmap() in seccomp mode
um: forbid mmap/munmap in the stub seccomp filter
um: install guest mappings via pidfd_mmap() in both modes selftests/pidfd: add pidfd_mmap()/pidfd_munmap() tests

 arch/um/include/shared/os.h                   |   4 -
 arch/um/include/shared/skas/mm_id.h           |   8 +-
 arch/um/include/shared/skas/stub-data.h       |  28 --
 arch/um/kernel/skas/mmu.c                     |   9 +-
 arch/um/kernel/skas/stub.c                    | 151 ++--------
 arch/um/kernel/skas/stub_exe.c                |  20 +-
 arch/um/os-Linux/skas/mem.c                   | 275 ++----------------
 arch/um/os-Linux/skas/process.c               |  92 ++----
 arch/um/os-Linux/start_up.c                   |   8 +-
 arch/x86/entry/syscalls/syscall_64.tbl        |   2 +
 include/linux/syscalls.h                      |   5 +
 include/uapi/linux/pidfd.h                    |  18 ++
 kernel/pid.c                                  | 132 +++++++++
 scripts/syscall.tbl                           |   2 +
 tools/testing/selftests/pidfd/Makefile        |   3 +-
 tools/testing/selftests/pidfd/pidfd.h         |  34 +++
 .../testing/selftests/pidfd/pidfd_mmap_test.c | 234 +++++++++++++++
 17 files changed, 520 insertions(+), 505 deletions(-)
 create mode 100644 tools/testing/selftests/pidfd/pidfd_mmap_test.c

base-commit: 69288464b0af40a988958c0e1b29ff15ccd2acb7

Signature · 1 line
2.43.0
author
#1 lore raw

RFC PATCH 1/6 pidfd: add pidfd_mmap()/pidfd_munmap() syscalls

From: Cong Wang <[email protected]>

A supervisor that may PTRACE_ATTACH a target installs or removes a mapping in the target's address space directly, without target-side cooperation. The backing fd is resolved in the *caller's* fd table, mirroring pidfd_getfd()'s cross-task install model.

pidfd_mmap() takes an extensible struct pidfd_mmap_args (clone3/openat2 style, versioned by a leading size field) since an mmap-shaped call exceeds the 6-argument syscall limit. pidfd_munmap() is a flat 3-arg call. Both are gated by ptrace_may_access(PTRACE_MODE_ATTACH_REALCREDS) and wired for x86_64 and the asm-generic table.

Assisted-by Claude:claude-opus-4.8
---
 arch/x86/entry/syscalls/syscall_64.tbl |   2 +
 include/linux/syscalls.h               |   5 +
 include/uapi/linux/pidfd.h             |  18 ++++
 kernel/pid.c                           | 132 +++++++++++++++++++++++++
 scripts/syscall.tbl                    |   2 +
 5 files changed, 159 insertions(+)
arch/x86/entry/syscalls/syscall_64.tbl +2 -0
index 524155d655da..12e3cbeb0d2c 100644
@@ -396,6 +396,8 @@
 469	common	file_setattr		sys_file_setattr
 470	common	listns			sys_listns
 471	common	rseq_slice_yield	sys_rseq_slice_yield
+472	common	pidfd_mmap		sys_pidfd_mmap
+473	common	pidfd_munmap		sys_pidfd_munmap
 
 #
 # Due to a historical design error, certain syscalls are numbered differently
include/linux/syscalls.h +5 -0
index 874d9067a43b..301a2d80823b 100644
@@ -78,6 +78,7 @@ struct statmount;
 struct mnt_id_req;
 struct ns_id_req;
 struct xattr_args;
+struct pidfd_mmap_args;
 struct file_attr;
 
 #include <linux/types.h>
@@ -986,6 +987,10 @@ asmlinkage long sys_pidfd_send_signal(int pidfd, int sig,
 				       siginfo_t __user *info,
 				       unsigned int flags);
 asmlinkage long sys_pidfd_getfd(int pidfd, int fd, unsigned int flags);
+asmlinkage long sys_pidfd_mmap(int pidfd, struct pidfd_mmap_args __user *uargs,
+			       unsigned int flags);
+asmlinkage long sys_pidfd_munmap(int pidfd, unsigned long addr,
+				 unsigned long len);
 asmlinkage long sys_landlock_create_ruleset(const struct landlock_ruleset_attr __user *attr,
 		size_t size, __u32 flags);
 asmlinkage long sys_landlock_add_rule(int ruleset_fd, enum landlock_rule_type rule_type,
include/uapi/linux/pidfd.h +18 -0
index 0919246a1611..d68a11d844e3 100644
@@ -107,6 +107,24 @@ struct pidfd_info {
 	__u64 supported_mask;	/* Mask flags that this kernel supports */
 };
 
+/*
+ * Argument block for pidfd_mmap(). Extensible: @size is sizeof(struct) as
+ * known to userspace; the kernel zero-extends older/smaller structs and
+ * rejects unknown non-zero trailing bytes (see copy_struct_from_user()).
+ */
+struct pidfd_mmap_args {
+	__u64 size;	/* sizeof(struct pidfd_mmap_args) */
+	__u64 addr;	/* MAP_FIXED target address, or 0 to let the kernel choose */
+	__u64 len;	/* length in bytes */
+	__u64 prot;	/* PROT_* protection bits */
+	__u64 flags;	/* MAP_* flags */
+	__u64 pgoff;	/* page offset into @fd */
+	__s32 fd;	/* backing fd in the caller's fd table, or -1 (MAP_ANONYMOUS) */
+	__u32 __spare;	/* must be zero */
+};
+
+#define PIDFD_MMAP_ARGS_SIZE_VER0	56 /* sizeof first published struct */
+
 #define PIDFS_IOCTL_MAGIC 0xFF
 
 #define PIDFD_GET_CGROUP_NAMESPACE            _IO(PIDFS_IOCTL_MAGIC, 1)
kernel/pid.c +132 -0

141 lines not on this page · raw message

scripts/syscall.tbl +2 -0
index 7a42b32b6577..f4fa7826232b 100644
@@ -412,3 +412,5 @@
 469	common	file_setattr			sys_file_setattr
 470	common	listns				sys_listns
 471	common	rseq_slice_yield		sys_rseq_slice_yield
+472	common	pidfd_mmap			sys_pidfd_mmap
+473	common	pidfd_munmap			sys_pidfd_munmap
Signature · 1 line
2.43.0
author
#2 lore raw

RFC PATCH 2/6 um: acquire a stub pidfd via CLONE_PIDFD in seccomp mode

In reply to Cong Wang

From: Cong Wang <[email protected]>

In SECCOMP mode the monitor will install guest mappings into the stub's address space directly via pidfd_mmap(), so it needs a pidfd to the stub. Acquire one atomically at clone() time via CLONE_PIDFD (returned through the legacy-clone parent_tid argument), store it in mm_id->stub_pidfd, and close it on teardown. The ptrace (SKAS0) mode does not use pidfd_mmap and leaves stub_pidfd as -1.

Assisted-by Claude:claude-opus-4.8
---
 arch/um/include/shared/skas/mm_id.h |  1 +
 arch/um/kernel/skas/mmu.c           |  6 ++++++
 arch/um/os-Linux/skas/process.c     | 24 ++++++++++++++++++++++--
 3 files changed, 29 insertions(+), 2 deletions(-)
arch/um/include/shared/skas/mm_id.h +1 -0
index 18c0621430d2..cec97189f12b 100644
@@ -16,6 +16,7 @@ struct mm_id {
 	int syscall_data_len;
 
 	/* Only used with SECCOMP mode */
+	int stub_pidfd;		/* pidfd to the stub, or -1 */
 	int sock;
 	int syscall_fd_num;
 	int syscall_fd_map[STUB_MAX_FDS];
arch/um/kernel/skas/mmu.c +6 -0
index b5017096028b..441dcf94ec9c 100644
@@ -54,6 +54,7 @@ int init_new_context(struct task_struct *task, struct mm_struct *mm)
 		goto out;
 
 	new_id->stack = stack;
+	new_id->stub_pidfd = -1;
 	new_id->syscall_data_len = 0;
 	new_id->syscall_fd_num = 0;
 
@@ -103,6 +104,11 @@ void destroy_context(struct mm_struct *mm)
 		mmu->id.pid = -1;
 	}
 
+	if (mmu->id.stub_pidfd >= 0) {
+		os_close_file(mmu->id.stub_pidfd);
+		mmu->id.stub_pidfd = -1;
+	}
+
 	if (using_seccomp && mmu->id.sock)
 		os_close_file(mmu->id.sock);
 
arch/um/os-Linux/skas/process.c +22 -2
index d6c22f8aa06d..3dd97ca7999a 100644
@@ -34,6 +34,10 @@
 #include <asm-generic/rwonce.h>
 #include "../internal.h"
 
+#ifndef CLONE_PIDFD
+#define CLONE_PIDFD 0x00001000
+#endif
+
 int is_skas_winch(int pid, int fd, void *data)
 {
 	return pid == getpgrp();
@@ -448,6 +452,9 @@ int start_userspace(struct mm_id *mm_id)
 	void *stack;
 	unsigned long sp;
 	int status, n, err;
+	int stub_pidfd = -1;
+
+	mm_id->stub_pidfd = -1;
 
 	/* setup a temporary stack page */
 	stack = mmap(NULL, UM_KERN_PAGE_SIZE,
@@ -474,15 +481,25 @@ int start_userspace(struct mm_id *mm_id)
 	if (using_seccomp)
 		proc_data->futex = FUTEX_IN_CHILD;
 
+	/*
+	 * In SECCOMP mode, acquire a pidfd to the stub via CLONE_PIDFD (it is
+	 * returned through the legacy-clone parent_tid argument). The monitor
+	 * installs guest mappings into the stub's mm directly via pidfd_mmap(),
+	 * so the stub itself never needs the mmap capability. The ptrace mode
+	 * does not use it and drives the stub directly.
+	 */
 	mm_id->pid = clone(userspace_tramp, (void *) sp,
-		    CLONE_VFORK | CLONE_VM | SIGCHLD,
-		    (void *)&tramp_data);
+		    CLONE_VFORK | CLONE_VM | (using_seccomp ? CLONE_PIDFD : 0) |
+		    SIGCHLD,
+		    (void *)&tramp_data, &stub_pidfd);
 	if (mm_id->pid < 0) {
 		err = -errno;
 		printk(UM_KERN_ERR "%s : clone failed, errno = %d\n",
 		       __func__, errno);
 		goto out_close;
 	}
+	if (using_seccomp)
+		mm_id->stub_pidfd = stub_pidfd;
 
 	if (using_seccomp) {
 		wait_stub_done_seccomp(mm_id, 1, 1);
@@ -534,8 +551,11 @@ int start_userspace(struct mm_id *mm_id)
 out_close:
 	close(tramp_data.sockpair[0]);
 	close(tramp_data.sockpair[1]);
+	if (stub_pidfd >= 0)
+		close(stub_pidfd);
 
 	mm_id->pid = -1;
+	mm_id->stub_pidfd = -1;
 
 	return err;
 }
Signature · 1 line
2.43.0
author
#3 lore raw

RFC PATCH 3/6 um: install guest mappings via pidfd_mmap() in seccomp mode

In reply to Cong Wang

From: Cong Wang <[email protected]>

In SECCOMP mode, map()/unmap() now install into the stub's address space directly from the monitor via pidfd_mmap()/pidfd_munmap(), resolving the physmem fd in the monitor's own fd table. The stub neither receives the fd nor executes mmap/munmap itself.

The ptrace (SKAS0) mode is unchanged: it keeps batching STUB_SYSCALL_MMAP /MUNMAP for the stub to execute, which works on any host kernel and needs no new syscall. Only the seccomp path, where the stub is sandboxed, is converted.

Assisted-by Claude:claude-opus-4.8
---
 arch/um/os-Linux/skas/mem.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
arch/um/os-Linux/skas/mem.c +24 -0
index 8b9921ac3ef8..f751a1ea1c4c 100644
@@ -9,6 +9,8 @@
 #include <errno.h>
 #include <string.h>
 #include <sys/mman.h>
+#include <sys/syscall.h>
+#include <linux/pidfd.h>
 #include <init.h>
 #include <as-layout.h>
 #include <mm_id.h>
@@ -236,6 +238,21 @@ int map(struct mm_id *mm_idp, unsigned long virt, unsigned long len, int prot,
 {
 	struct stub_syscall *sc;
 
+	if (using_seccomp) {
+		struct pidfd_mmap_args args = {
+			.size	= sizeof(args),
+			.addr	= virt,
+			.len	= len,
+			.prot	= prot,
+			.flags	= MAP_SHARED | MAP_FIXED,
+			.pgoff	= offset >> UM_KERN_PAGE_SHIFT,
+			.fd	= phys_fd,
+		};
+		long res = syscall(__NR_pidfd_mmap, mm_idp->stub_pidfd, &args, 0);
+
+		return res < 0 ? -errno : 0;
+	}
+
 	/* Compress with previous syscall if that is possible */
 	sc = syscall_stub_get_previous(mm_idp, STUB_SYSCALL_MMAP, virt);
 	if (sc && sc->mem.prot == prot &&
@@ -268,6 +285,13 @@ int unmap(struct mm_id *mm_idp, unsigned long addr, unsigned long len)
 {
 	struct stub_syscall *sc;
 
+	if (using_seccomp) {
+		long res = syscall(__NR_pidfd_munmap, mm_idp->stub_pidfd,
+				   addr, len);
+
+		return res < 0 ? -errno : 0;
+	}
+
 	/* Compress with previous syscall if that is possible */
 	sc = syscall_stub_get_previous(mm_idp, STUB_SYSCALL_MUNMAP, addr);
 	if (sc) {
Signature · 1 line
2.43.0
author
#4 lore raw

RFC PATCH 4/6 um: forbid mmap/munmap in the stub seccomp filter

In reply to Cong Wang

From: Cong Wang <[email protected]>

In seccomp mode the monitor now installs all guest mappings via pidfd_mmap(), so the stub has no legitimate need to mmap/munmap after its filter is in place (the pre-filter bootstrap mmaps are unaffected). Drop the STUB_MMAP_NR and __NR_munmap allowlist arms and fix the jump offsets; both now hit SECCOMP_RET_KILL_PROCESS. A hijacked stub can no longer map arbitrary physmem. The filter exists only in seccomp mode, so ptrace (SKAS0) is untouched.

Assisted-by Claude:claude-opus-4.8
---
 arch/um/kernel/skas/stub_exe.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
arch/um/kernel/skas/stub_exe.c +11 -9
index cbafaa684e66..a88274449168 100644
@@ -165,16 +165,18 @@ noinline static void real_init(void)
 			BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
 				 offsetof(struct seccomp_data, nr)),
 
-			/* [10-16] Check against permitted syscalls */
+			/*
+			 * [10-14] Check against permitted syscalls. mmap and
+			 * munmap are deliberately absent: in seccomp mode the
+			 * monitor installs all guest mappings via pidfd_mmap(),
+			 * so a stub that attempts mmap/munmap hits the KILL at
+			 * [15].
+			 */
 			BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, __NR_futex,
-				 7, 0),
-			BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K,__NR_recvmsg,
-				 6, 0),
-			BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K,__NR_close,
 				 5, 0),
-			BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, STUB_MMAP_NR,
+			BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, __NR_recvmsg,
 				 4, 0),
-			BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, __NR_munmap,
+			BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, __NR_close,
 				 3, 0),
 #ifdef __i386__
 			BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, __NR_set_thread_area,
@@ -186,10 +188,10 @@ noinline static void real_init(void)
 			BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, __NR_rt_sigreturn,
 				 1, 0),
 
-			/* [17] Not one of the permitted syscalls */
+			/* [15] Not one of the permitted syscalls */
 			BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_KILL_PROCESS),
 
-			/* [18] Permitted call for the stub */
+			/* [16] Permitted call for the stub */
 			BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW),
 		};
 		struct sock_fprog prog = {
Signature · 1 line
2.43.0
author
#5 lore raw

RFC PATCH 5/6 um: install guest mappings via pidfd_mmap() in both modes

In reply to Cong Wang

From: Cong Wang <[email protected]>

Route both seccomp and ptrace (SKAS0) map()/unmap() through pidfd_mmap()/pidfd_munmap(): the monitor resolves the physmem fd in its own fd table and installs the mapping into the stub's mm directly, so the stub never executes mmap/munmap itself in either mode.

Neither mode drives the stub to execute syscalls anymore, so the whole stub-syscall batcher and the seccomp-only SCM_RIGHTS fd-passing become dead code and are removed. The seccomp model's inherent risks are not addressed by pidfd_mmap() (a malicious guest can still block SIGALRM to dodge scheduling), so that note is preserved in stub.c.

This is an RFC demonstration of pidfd_mmap()'s reach; it assumes a host kernel that provides the syscall and does not add a fallback for older hosts intentionally.

Assisted-by Claude:claude-opus-4.8
---
 arch/um/include/shared/os.h             |   4 -
 arch/um/include/shared/skas/mm_id.h     |   7 -
 arch/um/include/shared/skas/stub-data.h |  28 ---
 arch/um/kernel/skas/mmu.c               |   5 -
 arch/um/kernel/skas/stub.c              | 151 ++----------
 arch/um/kernel/skas/stub_exe.c          |  16 +-
 arch/um/os-Linux/skas/mem.c             | 295 ++----------------------
 arch/um/os-Linux/skas/process.c         |  86 +------
 arch/um/os-Linux/start_up.c             |   8 +-
 9 files changed, 67 insertions(+), 533 deletions(-)
arch/um/include/shared/os.h +0 -4
index b26e94292fc1..c6917583ca64 100644
@@ -280,10 +280,6 @@ extern long long os_persistent_clock_emulation(void);
 extern long long os_nsecs(void);
 
 /* skas/mem.c */
-int syscall_stub_flush(struct mm_id *mm_idp);
-struct stub_syscall *syscall_stub_alloc(struct mm_id *mm_idp);
-void syscall_stub_dump_error(struct mm_id *mm_idp);
-
 int map(struct mm_id *mm_idp, unsigned long virt,
 	unsigned long len, int prot, int phys_fd,
 	unsigned long long offset);
arch/um/include/shared/skas/mm_id.h +0 -7
index cec97189f12b..0f74e1728a1e 100644
@@ -8,18 +8,11 @@
 
 #include <linux/compiler_types.h>
 
-#define STUB_MAX_FDS 4
-
 struct mm_id {
 	int pid;
 	unsigned long stack;
-	int syscall_data_len;
 
-	/* Only used with SECCOMP mode */
 	int stub_pidfd;		/* pidfd to the stub, or -1 */
-	int sock;
-	int syscall_fd_num;
-	int syscall_fd_map[STUB_MAX_FDS];
 };
 
 struct mutex *__get_turnstile(struct mm_id *mm_id);
arch/um/include/shared/skas/stub-data.h +0 -28
index 27db38e95df9..eaa208bcdaff 100644
@@ -31,36 +31,8 @@ struct stub_init_data {
 	unsigned long signal_restorer;
 };
 
-#define STUB_NEXT_SYSCALL(s) \
-	((struct stub_syscall *) (((unsigned long) s) + (s)->cmd_len))
-
-enum stub_syscall_type {
-	STUB_SYSCALL_UNSET = 0,
-	STUB_SYSCALL_MMAP,
-	STUB_SYSCALL_MUNMAP,
-};
-
-struct stub_syscall {
-	struct {
-		unsigned long addr;
-		unsigned long length;
-		unsigned long offset;
-		int fd;
-		int prot;
-	} mem;
-
-	enum stub_syscall_type syscall;
-};
-
 struct stub_data {
-	long err;
-
-	int syscall_data_len;
-	/* 128 leaves enough room for additional fields in the struct */
-	struct stub_syscall syscall_data[(UM_KERN_PAGE_SIZE - 128) / sizeof(struct stub_syscall)] __aligned(16);
-
 	/* data shared with signal handler (only used in seccomp mode) */
-	short restart_wait;
 	unsigned int futex;
 	int signal;
 	unsigned short si_offset;
arch/um/kernel/skas/mmu.c +0 -5
index 441dcf94ec9c..b2c9cf023789 100644
@@ -55,8 +55,6 @@ int init_new_context(struct task_struct *task, struct mm_struct *mm)
 
 	new_id->stack = stack;
 	new_id->stub_pidfd = -1;
-	new_id->syscall_data_len = 0;
-	new_id->syscall_fd_num = 0;
 
 	scoped_guard(spinlock_irqsave, &mm_list_lock) {
 		/* Insert into list, used for lookups when the child dies */
@@ -109,9 +107,6 @@ void destroy_context(struct mm_struct *mm)
 		mmu->id.stub_pidfd = -1;
 	}
 
-	if (using_seccomp && mmu->id.sock)
-		os_close_file(mmu->id.sock);
-
 	free_pages(mmu->id.stack, ilog2(STUB_DATA_PAGES));
 }
 
arch/um/kernel/skas/stub.c +22 -129

181 lines not on this page · raw message

arch/um/kernel/skas/stub_exe.c +7 -9
index a88274449168..af1d011be0a4 100644
@@ -166,15 +166,13 @@ noinline static void real_init(void)
 				 offsetof(struct seccomp_data, nr)),
 
 			/*
-			 * [10-14] Check against permitted syscalls. mmap and
-			 * munmap are deliberately absent: in seccomp mode the
-			 * monitor installs all guest mappings via pidfd_mmap(),
-			 * so a stub that attempts mmap/munmap hits the KILL at
-			 * [15].
+			 * [10-13] Check against permitted syscalls. mmap, munmap
+			 * and recvmsg are deliberately absent: in seccomp mode
+			 * the monitor installs all guest mappings via
+			 * pidfd_mmap() and no longer passes fds to the stub, so a
+			 * stub that attempts mmap/munmap hits the KILL at [14].
 			 */
 			BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, __NR_futex,
-				 5, 0),
-			BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, __NR_recvmsg,
 				 4, 0),
 			BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, __NR_close,
 				 3, 0),
@@ -188,10 +186,10 @@ noinline static void real_init(void)
 			BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, __NR_rt_sigreturn,
 				 1, 0),
 
-			/* [15] Not one of the permitted syscalls */
+			/* [14] Not one of the permitted syscalls */
 			BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_KILL_PROCESS),
 
-			/* [16] Permitted call for the stub */
+			/* [15] Permitted call for the stub */
 			BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW),
 		};
 		struct sock_fprog prog = {
arch/um/os-Linux/skas/mem.c +21 -274

308 lines not on this page · raw message

arch/um/os-Linux/skas/process.c +12 -74

129 lines not on this page · raw message

arch/um/os-Linux/start_up.c +5 -3
index 054ac03bbf5e..518107622a2d 100644
@@ -311,9 +311,11 @@ static bool __init init_seccomp(void)
 				      PROT_READ | PROT_WRITE,
 				      MAP_SHARED | MAP_ANON, 0, 0);
 
-	/* Use the syscall data area as stack, we just need something */
-	sp = (unsigned long)&seccomp_test_stub_data->syscall_data +
-	     sizeof(seccomp_test_stub_data->syscall_data) -
+	/*
+	 * We just need some valid stack; use the first page of the shared
+	 * stub_data region (sigstack occupies the following page).
+	 */
+	sp = (unsigned long)&seccomp_test_stub_data->sigstack[0] -
 	     sizeof(void *);
 	pid = clone(seccomp_helper, (void *)sp, CLONE_VFORK | CLONE_VM, NULL);
 
Signature · 1 line
2.43.0
author
#6 lore raw

RFC PATCH 6/6 selftests/pidfd: add pidfd_mmap()/pidfd_munmap() tests

In reply to Cong Wang

From: Cong Wang <[email protected]>

Exercise the new syscalls against a forked target reached by pidfd:

- install_and_read: map a parent-owned memfd into the target, verify the target's new mapping shows the memfd contents (read via process_vm_readv), that MAP_SHARED writes propagate, and that pidfd_munmap() removes it. This also covers the key semantic that the backing fd is resolved in the *caller's* fd table (the target never sees the memfd).
- anonymous: MAP_ANONYMOUS install + write-back round-trip.
- bad_args: __spare != 0, undersized @size, non-zero reserved flags, and a bad backing fd are all rejected.
- no_ptrace_access: a non-dumpable target (no ptrace access) yields -EPERM, matching the ptrace_may_access() gate.

The suite skips cleanly (TAP "SKIP") when the kernel lacks the syscall. The pidfd_mmap ABI definitions are kept in pidfd.h alongside the existing local uapi copies (struct pidfd_info, etc.).

Assisted-by Claude:claude-opus-4.8
---
 tools/testing/selftests/pidfd/Makefile        |   3 +-
 tools/testing/selftests/pidfd/pidfd.h         |  34 +++
 .../testing/selftests/pidfd/pidfd_mmap_test.c | 234 ++++++++++++++++++
 3 files changed, 270 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/pidfd/pidfd_mmap_test.c
tools/testing/selftests/pidfd/Makefile +2 -1
index 4211f91e9af8..f1c421bf725a 100644
@@ -4,7 +4,8 @@ CFLAGS += -g $(KHDR_INCLUDES) $(TOOLS_INCLUDES) -pthread -Wall
 TEST_GEN_PROGS := pidfd_test pidfd_fdinfo_test pidfd_open_test \
 	pidfd_poll_test pidfd_wait pidfd_getfd_test pidfd_setns_test \
 	pidfd_file_handle_test pidfd_bind_mount pidfd_info_test \
-	pidfd_xattr_test pidfd_setattr_test pidfd_autoreap_test
+	pidfd_xattr_test pidfd_setattr_test pidfd_autoreap_test \
+	pidfd_mmap_test
 
 TEST_GEN_PROGS_EXTENDED := pidfd_exec_helper
 
tools/testing/selftests/pidfd/pidfd.h +34 -0
index 5a4e78c10f43..91e65283d5fe 100644
@@ -203,6 +203,28 @@ struct pidfd_info {
 	__u64 supported_mask;
 };
 
+#ifndef __NR_pidfd_mmap
+#define __NR_pidfd_mmap 472
+#endif
+
+#ifndef __NR_pidfd_munmap
+#define __NR_pidfd_munmap 473
+#endif
+
+#ifndef PIDFD_MMAP_ARGS_SIZE_VER0
+struct pidfd_mmap_args {
+	__u64 size;
+	__u64 addr;
+	__u64 len;
+	__u64 prot;
+	__u64 flags;
+	__u64 pgoff;
+	__s32 fd;
+	__u32 __spare;
+};
+#define PIDFD_MMAP_ARGS_SIZE_VER0	56
+#endif
+
 /*
  * The kernel reserves 300 pids via RESERVED_PIDS in kernel/pid.c
  * That means, when it wraps around any pid < 300 will be skipped.
@@ -267,6 +289,18 @@ static inline int sys_pidfd_getfd(int pidfd, int fd, int flags)
 	return syscall(__NR_pidfd_getfd, pidfd, fd, flags);
 }
 
+static inline long sys_pidfd_mmap(int pidfd, struct pidfd_mmap_args *args,
+				  unsigned int flags)
+{
+	return syscall(__NR_pidfd_mmap, pidfd, args, flags);
+}
+
+static inline long sys_pidfd_munmap(int pidfd, unsigned long addr,
+				    unsigned long len)
+{
+	return syscall(__NR_pidfd_munmap, pidfd, addr, len);
+}
+
 static inline int sys_memfd_create(const char *name, unsigned int flags)
 {
 	return syscall(__NR_memfd_create, name, flags);
tools/testing/selftests/pidfd/pidfd_mmap_test.c new file +234 -0

234 lines not on this page · raw message

Signature · 1 line
2.43.0
maintainer
#7 lore raw
In reply to Cong Wang
Quoted 3 lines · Cong Wang

This RFC adds two syscalls, pidfd_mmap() and pidfd_munmap(), that let a supervisor install or remove a mapping in another process's address space without target-side cooperation, and converts User Mode Linux to use them.

Seems simple enough, but I can't really comment on it.

Quoted 6 lines

As this is an RFC, the UML side _intentionally_ does not handle host
portability: it assumes a host kernel that provides pidfd_mmap() and deliberately omits any detection or fallback for older hosts (which would otherwise pick the ptrace path, or fail the seccomp probe). That belongs in a non-RFC version and is left out here to keep the series focused on demonstrating the impact on UML.

Makes sense for the RFC, but I think the fallback to seccomp as-is might be useful still, we did see some benefit of seccomp and cannot necessarily update the host kernels _that_ quickly. Maybe do that only if forced by the user with seccomp=insecure or something, not pick it with =auto and fail with seccomp=on if it's not available? That'd be good enough for us, and not really leave a potential security trap (that we have now for people who don't read the docs ;) ).

johannes