From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CC9AC433E93 for ; Sat, 4 Jul 2026 23:18:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.176 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783207141; cv=none; b=NP3G7sU/2eQWuk9UtR8ZlXoQpuK2MXfu3QRcYLxxNfKg2P9t+thECiBf0lw9JfTNgEEjMIrcEevM5wJ1OV9KN5Z6LpCGiupOKQSdIZmGBgti9UHkKjdiU+qQnbcMaEpPiGzmGbmENYAus4OjfW+QI2VL0xmGOcqHoVIsZX5WFWc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783207141; c=relaxed/simple; bh=46b13Jc5mrIImAiNNBOkNWIW/U3+WzD1irKwDr27NI4=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=ljiNMtSbtXCqLz21tzxWQM4k4wTjeM5QdN6+1Hcaj9c6lxkAFXy/WSv+ChM3kHyGn1UYciYWhX2CbM5wqFCYfrGL+oE4MY40p/CJCC6NDfyA5i+mjhuCNACNk/0GtAOrsoQQ7fxFIxbO00BUBYeyuwl5uaRpt4OVqpGYECTU1FI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=aaHZgIbN; arc=none smtp.client-ip=209.85.214.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="aaHZgIbN" Received: by mail-pl1-f176.google.com with SMTP id d9443c01a7336-2caed617615so11703255ad.3 for ; Sat, 04 Jul 2026 16:18:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1783207139; x=1783811939; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=ouPRH6bwUsvKzcuc7nCFnf16gT6SDBFOnvZAEM2+bt4=; b=aaHZgIbNJeJZI5fi1XJDJBCeR6LnnDHbnWctMj0hf95Jip5DwLfB4p2Orw/tw6tinL FXdOBHnhsDmCLMGsMRMzb9bltb7tXxFEaYIQuoU3TKn57uUFPk0XIA07DVFmhJhwRTaB xxsBqz5T5zdTFiKP5j8wus9j3wPoxDqisE+6XehUl+GJ8lhtFsK7nzhlN1qBX1eoTF1f BzEKoNuKBwsck0lzVhjemb+NhYNNqopsFLl3U+t81eZ76iULCaP3mcIlOoVELOg9jOI1 1ktnJ4WZ052dbelGOBh2rvSZ/hMTbScvw6TtGx8rwqq544eLI7HRLzRlICYGdcio2C6i 4P4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783207139; x=1783811939; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=ouPRH6bwUsvKzcuc7nCFnf16gT6SDBFOnvZAEM2+bt4=; b=lt1NBk+l3am1FGxcBkDy/HsRRp7mbZ1movwF/EmndpS6EjKFCYtNv0ZiBDhe5BIcCK IhIuOgxg814PBElEES+Q8+RBMjX5tkbWJVQpA6c2OZc0CnvCoVDUNIOndpLATSh6tzsY H70bFKGCt19MSS1BWBK3N13LN4PviBrpT3tpVI8shuMjn0xPcaFVM5WW+sE/SJcFKt1u MxEf7/RtGK4N22ErDjDSnlzvfpqlHnlEBvxWB5c1LVRbAFJpb+DExgBiu8nAlvfHMTOw 7lNLA06isNJm7dBRQ/GAq+Iyw9pRT/3U9fFjgIKuXvYtGeBJ4JJYUF6yapUiqyoV+RSO ukmg== X-Forwarded-Encrypted: i=1; AFNElJ9RGNxjFvi7HtCnJGKWBxKokHXACmTzFeVGYfacRB+vU8t7BaSEUsPJdVirHldegSzp02dUzWOAknbOL50=@vger.kernel.org X-Gm-Message-State: AOJu0YzfovMOpov2ZxeAmOSiJESju4FCqY9cx8llegt1bhw9qhSxgB0X 2dxk7WHLFG0T59i6DIeMN7dvLubdHMJpUIGaAhuitVlho/PHC47DBbIB X-Gm-Gg: AfdE7cmJCjIpG+sZfVuAFMLTFmB6JgzPRl5oXbyjPsZfP94ckFPx5HZoLrPXKCYVAyE fvpjDY4XTjL2HS54W3PjSac3ZxXZSX06Ma/XaU9SaBeBUCS/QSNU0qa3BWcnUqvQnUvnzH1kBt/ /WkDN+TUOi1GQknBo09pfwWneO345jOwF+fEFW0NHrzR9cKOruCLicER7vA8YpSevDkbR0/HQb/ D59woQsR3qc/jg64n+BJiNw7cOfs49ZEieNa1gc3rS5hzq/rCMtrZJGIvm15lsk1/ufCcCq1lo5 eu/cTIHONH27Z4S68Q6WxFVHhJGv85unUlAzC2yR+Q1EEKu/BgrrDdwMIDM4jw5qKn5kpNUOEhX Eb13dFBF0v/IYy/jVgWhqs+kJ2ou0RSHAQrPu4kzl1DvLVZZ4SvkRkoT693SKJXEYViVaxLN2fq UxvW6IMLIuKxitU3lWnNd0s6eKl2ObanS7ilY3E+W+8ObMK9q1kQ== X-Received: by 2002:a17:903:b0c:b0:2ca:6c8:abde with SMTP id d9443c01a7336-2cbb9e2f255mr42220875ad.18.1783207138946; Sat, 04 Jul 2026 16:18:58 -0700 (PDT) Received: from pop-os.. ([2601:647:6802:dbc0:55ba:96cc:892f:32d7]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2cad776589fsm27088215ad.43.2026.07.04.16.18.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 04 Jul 2026 16:18:58 -0700 (PDT) From: Cong Wang To: Andy Lutomirski Cc: Kees Cook , linux-kernel@vger.kernel.org, Will Drewry , Christian Brauner , Andrew Morton , linux-mm@kvack.org, Cong Wang Subject: [PATCH v5 0/7] seccomp: non-cooperative pinned-memfd argument redirect Date: Sat, 4 Jul 2026 16:18:24 -0700 Message-ID: <20260704231831.354543-1-xiyou.wangcong@gmail.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Cong Wang The seccomp user-notification SECCOMP_USER_NOTIF_FLAG_CONTINUE response carries an inherent TOCTOU: once the supervisor decides to let a syscall continue, the target (or a CLONE_VM peer) can rewrite the memory behind a pointer argument before the kernel reads it. This is documented in the UAPI header and is why the notifier "cannot be used to implement a security policy" today. The cooperative way around this is for the target to map a shared memfd and mseal() it during a trusted setup window, so the supervisor can hand the kernel an immutable buffer. That window does not exist for the common fork()+execve() sandbox model, where the supervisor wants to confine an uncooperative (or legacy) binary it did not write. This series lets the supervisor close the TOCTOU without any target-side cooperation: - The kernel installs a sealed, read-only, MAP_SHARED mapping of a supervisor-owned memfd directly into the trapped task's mm (SECCOMP_IOCTL_NOTIF_PIN_INSTALL). The mapping is VM_SEALED at creation, so neither the target nor a CLONE_VM peer can unmap, remap, mprotect or MAP_FIXED-stomp it. The backing memfd must be write-sealed (F_SEAL_WRITE / F_SEAL_FUTURE_WRITE), so its bytes cannot be rewritten through any other reference either; the supervisor stages the argument data through its own pre-seal mapping. - The supervisor then resumes the syscall with selected argument registers rewritten (SECCOMP_IOCTL_NOTIF_SEND_REDIRECT), the pointer ones aimed into a pin. Each pointer substitution is validated so the whole access [ptr, ptr+len) lies inside a sealed, read-only pin of the supervisor's memfd that still lives in the target's current mm; original registers are restored at syscall exit for ABI compliance. Because the data the kernel acts on lives in an immutable pin, the target can no longer win the race. execve() is handled as a first-class case: its pathname is copied from the pin before the old mm is torn down, and the register-restore is skipped once the program image has been replaced (detected via self_exec_id). A redirected syscall is re-validated against the outer filters in the target's filter chain, so an inner notifier cannot use a redirect to smuggle a syscall past a policy an outer filter enforces (e.g. redirect to a blocked unshare()); see patch 5. sandlock [1], a non-cooperative seccomp sandbox supervisor, will use this to enforce argument-level policy on uncooperative targets. [1] https://github.com/multikernel/sandlock Patch 1 adds the mm plumbing: __do_mmap(), a variant of do_mmap() that targets a caller-supplied mm (do_mmap() stays a current->mm wrapper, so no existing caller changes), and vm_mmap_remote()/vm_munmap_remote(), high-level helpers for installing and removing the sealed pin. Patch 2 adds PIN_INSTALL, patch 3 adds the __NR_seccomp_* syscall aliases, patch 4 adds SEND_REDIRECT, patch 5 adds the outer-filter re-validation, patch 6 documents the ABI, and patch 7 adds selftests. Changes since v4: - Fixed READ_IMPLIES_EXEC and VM warning in patch 1 - Fixed TRACE re-validation case in patch 5 - New patch 3: split the __NR_seccomp_* syscall aliases (arch- overridable, covering rt_sigreturn and the clone/fork family) into their own patch; x86 maps the compat (ia32) numbers. - SEND_REDIRECT now also refuses the clone/fork family (clone, clone3, fork, vfork) with -EOPNOTSUPP, not just rt_sigreturn: rewriting a task-creating syscall's registers has no use case and is unsafe. - Renamed vm_mmap_seal_remote() to vm_mmap_remote() and added the vm_munmap_remote() counterpart (patch 1). - vm_mmap_remote() now bounds the mapping within the target mm's own address space with an overflow-safe task_size check, including the caller-supplied fixed-address path. - Compat: for SECCOMP_ARCH_COMPAT targets, redirected pointer arguments are truncated to 32 bits before the pin range is validated. - Selftests: two new tests -- redirect_denied_syscalls (rt_sigreturn and the clone/fork family are all rejected with -EOPNOTSUPP) and redirect_revalidate_chain (the re-validation walks the entire outer filter stack, not just the nearest filter); plus error-path hardening so a broken kernel fails or skips rather than hanging. Changes since v3: - Split the single seccomp patch into PIN_INSTALL (patch 2) and SEND_REDIRECT (patch 4) for reviewability. - New patch 5: re-validate a redirected syscall against the outer filters in the stack, closing the bypass Andy described (an inner notifier redirecting to a syscall an outer filter blocks, e.g. unshare()). - Signals: the argument restore now runs before signal/restart processing. It is queued as task_work with TWA_RESUME -- not the TWA_SIGNAL discussed on-list, which makes signal_pending() true for the whole redirected syscall and livelocks an interruptible one. TWA_RESUME still runs the restore at the top of get_signal(), before the signal frame is built and before any -ERESTART* rewind; on a restart the syscall re-traps seccomp and the supervisor is notified again. rt_sigreturn is refused (-EOPNOTSUPP). - At most one redirect-capable notifier may exist in a filter chain (-EBUSY); ordinary notifiers are unconstrained. Syscalls with complex signal/restart behaviour (nanosleep, futex(FUTEX_WAIT), ...) are out of scope and should not have their arguments redirected. - PIN_INSTALL: target_addr == 0 lets the kernel pick a free address in the target mm (avoids a racy userspace /proc//maps scan), and a new offset field lets one memfd back several disjoint pins. - New patch 6: Documentation/userspace-api/seccomp_filter.rst. - Selftests expanded: install into a fresh post-execve mm, stateless churn, outer-filter re-validation, ABI/versioning, and a signal-ordering regression test. Changes since v2: v3 was a redesign that dropped the v2 SECCOMP_IOCTL_NOTIF_INJECT approach (an in-kernel reimplementation of a syscall whitelist) in favour of redirecting the real syscall into a sealed pin, as suggested by Andy. Changes since v1: v2 was a redesign that dropped the v1 SECCOMP_IOCTL_NOTIF_PIN_ARGS approach. All pinned-memfd and redirect selftests pass (seccomp_bpf: 119/119). Cong Wang (7): mm: add __do_mmap() and vm_mmap_remote()/vm_munmap_remote() seccomp: introduce SECCOMP_IOCTL_NOTIF_PIN_INSTALL seccomp: add __NR_seccomp_* aliases for rt_sigreturn and clone/fork seccomp: add kernel-installed pinned-memfd redirect seccomp: re-validate a redirected syscall against outer filters docs/seccomp: document pinned-memfd redirect ioctls selftests/seccomp: cover non-cooperative pinned-memfd install .../userspace-api/seccomp_filter.rst | 109 ++ arch/x86/include/asm/seccomp.h | 6 + include/asm-generic/seccomp.h | 45 + include/linux/mm.h | 5 + include/linux/seccomp.h | 12 +- include/uapi/linux/seccomp.h | 87 ++ kernel/seccomp.c | 511 ++++++- mm/internal.h | 8 + mm/mmap.c | 87 +- mm/nommu.c | 22 +- mm/util.c | 97 ++ mm/vma.c | 35 +- mm/vma.h | 6 +- tools/testing/selftests/seccomp/seccomp_bpf.c | 1251 +++++++++++++++++ 14 files changed, 2231 insertions(+), 50 deletions(-) base-commit: 87320be9f0d24fce67631b7eef919f0b79c3e45c -- 2.43.0