From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796441295; b=D/UgKMVA2DWN84XMzVKxkkVAwP+S+wLO5bG5+2wA2PkxQ0eCmS9ubdSFuHgnhnmBnYEJ QmJ6kKwg4HVEubquZ1GewhJfXr95AkLU7dexv2FwY8yNkZLCWeiKk0Bw56/cse2/Bfc3f w4sHzTBq1yAfJPuh4nbs4/FWQ8wt7Za5exoPdK7iGnEs3EZrXT9ErEMEcUTS6qFYah7n0 NnvYcvsAVc3tnEDM7RXsEyNarXMd8dVtoKbOu2pWqg8AwzZ7iEugpgaE1pShSCwSrUNLh 5LiKkXDg5FLzpcOqfUy4sVXoeKmFOgOsE9c7E+W6tN7DHGU1ARGU/eblPin5nxhy+Qg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796441295; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=OvG6aeuAPeCHNkeM6ObdGBtVWUBOJ1LBv1yh0GFxyPo=; b=kVPvCKvtZ9dOxlWRC9qsEWg93ByGeqXfF3q9kWBwrsmu4urKwnruu4GFRqpnlCYf2mRi NcbSg1cswZvDfbahr/Y6iLoC54fiB2k82hLTn4zZNF2LeCD2tMcqp9MJH1xAc0NRyZhln 6+mYdMw35K14HlptiCW3LUYZdlnagehy65lqPvC/KQuWK9IjfjL5AzyF01H6rgFMuN3bo LId7zea7yZp1U8wv4LiKsmqSGEWOR3rr1QMO2WfVn8MTVCo6AFWaRP2YbnCE17WXXFDBV eLGxjpa0qGaKhuAYPhnfPsB8ndeM1FZ7K4SA2sUfcX62jvNrVOpfLBaLPsfcZ8Q0Tsw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=WdNxOfWr; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with UTF8SMTPS id 9A19F606BA42; Sat, 5 Dec 2026 03:28:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=WdNxOfWr Received: by smtp.kernel.org (Postfix) with UTF8SMTPSA id B98E51F00A13; Sat, 5 Dec 2026 03:28:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796441295; bh=OvG6aeuAPeCHNkeM6ObdGBtVWUBOJ1LBv1yh0GFxyPo=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=WdNxOfWr9eOFlbp5RLf7Qc564gUvvRoy0ZC6YNz3xU/Qxmakpfe6KbSTN0jfzq+H2 MJAWt2aLYzW6EvFToqO7L1uc89moA37IGN9lj2yIu0LqpeLKKL3D8Zj/vi+nDJqX1q gUTv/O1tIClIQRDHoI8YytE8xfTZg6xRd5WHDtfeF9zDLWgmsL8EZ2gDGpYtbnw4Zv jbN3S4Y6ZwyJIH3PKqJojIEWgMj87amFpGOe8h/+qNQ7NzCcSwZCdI/D39dbGlIF2C 7ei6IppQs9cpCa0kkjxo/uBjgm6B0vWxwVv3tu+skGpWKYRq8xaS/F9wyoaTPvFbk3 diRkxqsrsE9VQ== From: Jarkko Sakkinen To: fsdevel@vger.kernel.org Cc: Jarkko Sakkinen , bpf@vger.kernel.org, containers@lists.linux.dev, linux-api@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-integrity@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, Aleksa Sarai , Alexander Mikhalitsyn , Alexander Viro , Alexei Starovoitov , Andrew Morton , Andrii Nakryiko , Andy Lutomirski , Arnd Bergmann , Baolin Wang , Ben Segall , Christian Brauner , Daniel Borkmann , David Hildenbrand , David Howells , David Woodhouse , Davidlohr Bueso , Dietmar Eggemann , Eduard Zingerman , Emil Tsalapatis , Eric Biggers , Georgia Garcia , Giuseppe Scrivano , =?UTF-8?q?G=C3=BCnther=20Noack?= , Hugh Dickins , Ihor Solodrai , Ingo Molnar , James Morris , Jan Kara , Jason Gunthorpe , Jeff Layton , Jiri Olsa , John Johansen , Jonathan Corbet , Josef Bacik , Juri Lelli , Kees Cook , K Prateek Nayak , KP Singh , Kumar Kartikeya Dwivedi , Lennart Poettering , "Liam R. Howlett" , Manfred Spraul , Martin KaFai Lau , Matt Bobrowski , Mel Gorman , Michal Hocko , =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= , Mike Rapoport , Neal Gompa , Oleg Nesterov , Paul Moore , Pavel Tikhomirov , Peter Zijlstra , Randy Dunlap , "Serge E. Hallyn" , Serge Hallyn , Shuah Khan , Song Liu , Stephen Smalley , Steven Rostedt , Valentin Schneider , Vincent Guittot , Yonghong Song , Zygmunt Krynicki Subject: [RFC PATCH v10 22/28] security: Add run LSM hooks Date: Sat, 5 Dec 2026 05:26:27 +0200 Message-ID: <20261205032633.1702772-23-jarkko@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261205032633.1702772-1-jarkko@kernel.org> References: <20261205032633.1702772-1-jarkko@kernel.org> X-Mailing-List: containers@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Security modules need visibility into run creation, teardown, namespace sharing, process entry, and run-wide signal delivery so policies can attach per-run security state and mediate operations on run file descriptors. Add lbs_runfd and run->security blob management alongside security_runfd_alloc(), security_runfd_free(), security_runfd_share(), security_runfd_clone(), and security_runfd_kill(). Signed-off-by: Jarkko Sakkinen --- include/linux/lsm_hook_defs.h | 8 ++++ include/linux/lsm_hooks.h | 3 ++ include/linux/runfd.h | 3 ++ include/linux/security.h | 37 ++++++++++++++++++ kernel/runfd.c | 43 ++++++++++++++++----- security/lsm_init.c | 6 +++ security/security.c | 72 +++++++++++++++++++++++++++++++++++ 7 files changed, 163 insertions(+), 9 deletions(-) diff --git a/include/linux/lsm_hook_defs.h b/include/linux/lsm_hook_defs.h index 005a350681c3..e2ec29671269 100644 --- a/include/linux/lsm_hook_defs.h +++ b/include/linux/lsm_hook_defs.h @@ -480,3 +480,11 @@ LSM_HOOK(int, 0, bdev_alloc_security, struct block_device *bdev) LSM_HOOK(void, LSM_RET_VOID, bdev_free_security, struct block_device *bdev) LSM_HOOK(int, 0, bdev_setintegrity, struct block_device *bdev, enum lsm_integrity_type type, const void *value, size_t size) + +#ifdef CONFIG_RUNFD +LSM_HOOK(int, 0, runfd_alloc, struct run *run, unsigned int flags) +LSM_HOOK(void, LSM_RET_VOID, runfd_free, struct run *run) +LSM_HOOK(int, 0, runfd_share, struct run *run, struct ns_common *ns) +LSM_HOOK(int, 0, runfd_clone, struct run *run) +LSM_HOOK(int, 0, runfd_kill, struct run *run, int sig) +#endif /* CONFIG_RUNFD */ diff --git a/include/linux/lsm_hooks.h b/include/linux/lsm_hooks.h index 13621e9e233e..b4134b5a6ac7 100644 --- a/include/linux/lsm_hooks.h +++ b/include/linux/lsm_hooks.h @@ -121,6 +121,9 @@ struct lsm_blob_sizes { unsigned int lbs_bpf_map; unsigned int lbs_bpf_prog; unsigned int lbs_bpf_token; +#ifdef CONFIG_RUNFD + unsigned int lbs_runfd; +#endif }; /* diff --git a/include/linux/runfd.h b/include/linux/runfd.h index 7ee0edf383b0..82c498ee65f1 100644 --- a/include/linux/runfd.h +++ b/include/linux/runfd.h @@ -64,6 +64,9 @@ enum runfd_state { struct run { refcount_t usage; int exit_code; +#ifdef CONFIG_SECURITY + void *security; +#endif struct user_namespace *user_ns; struct nsproxy *ns; struct pid *init_pid; diff --git a/include/linux/security.h b/include/linux/security.h index 9c8f8da83251..69d180c39c6a 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -84,6 +84,7 @@ struct audit_krule; struct ns_common; struct user_namespace; struct timezone; +struct run; enum lsm_event { LSM_POLICY_CHANGE, @@ -2471,4 +2472,40 @@ static inline void security_initramfs_populated(void) } #endif /* CONFIG_SECURITY */ +#ifdef CONFIG_RUNFD +#ifdef CONFIG_SECURITY +int security_runfd_alloc(struct run *run, unsigned int flags); +void security_runfd_free(struct run *run); +int security_runfd_share(struct run *run, struct ns_common *ns); +int security_runfd_clone(struct run *run); +int security_runfd_kill(struct run *run, int sig); +#else +static inline int security_runfd_alloc(struct run *run, + unsigned int flags) +{ + return 0; +} + +static inline void security_runfd_free(struct run *run) +{ +} + +static inline int security_runfd_share(struct run *run, + struct ns_common *ns) +{ + return 0; +} + +static inline int security_runfd_clone(struct run *run) +{ + return 0; +} + +static inline int security_runfd_kill(struct run *run, int sig) +{ + return 0; +} +#endif /* CONFIG_SECURITY */ +#endif /* CONFIG_RUNFD */ + #endif /* ! __LINUX_SECURITY_H */ diff --git a/kernel/runfd.c b/kernel/runfd.c index 6296b2eab12f..843f7f52fa95 100644 --- a/kernel/runfd.c +++ b/kernel/runfd.c @@ -42,15 +42,13 @@ #include #include #include +#include #include #include "exit.h" -void runfd_put(struct run *c) +static void runfd_free(struct run *c) { - if (!c || !refcount_dec_and_test(&c->usage)) - return; - if (c->ns) put_nsproxy(c->ns); if (c->init_pid) @@ -62,6 +60,15 @@ void runfd_put(struct run *c) kfree(c); } +void runfd_put(struct run *c) +{ + if (!c || !refcount_dec_and_test(&c->usage)) + return; + + security_runfd_free(c); + runfd_free(c); +} + static __poll_t runfd_poll(struct file *file, poll_table *wait) { struct run *run = file->private_data; @@ -144,6 +151,7 @@ static long runfd_share_nsfd(struct run *c, unsigned long arg) struct ns_common *old_time_children = NULL; struct ns_common *old_ns = NULL; struct ns_common *ns; + int ret; if (arg > INT_MAX) return -EBADF; @@ -174,6 +182,10 @@ static long runfd_share_nsfd(struct run *c, unsigned long arg) if (!runfd_may_use_ns(ns)) return -EPERM; + ret = security_runfd_share(c, ns); + if (ret) + return ret; + scoped_guard(spinlock, &c->lock) { if (c->state != RUNFD_INIT) return -EBUSY; @@ -322,6 +334,7 @@ int runfd_send_signal(struct file *file, int sig, bool any_perm = false; int other_err = 0; struct pid *pid; + int ret; int nr; if (info || flags || sig < 0 || !valid_signal(sig)) @@ -331,6 +344,10 @@ int runfd_send_signal(struct file *file, int sig, if (READ_ONCE(run->state) != RUNFD_RUNNING) return -ESRCH; + ret = security_runfd_kill(run, sig); + if (ret) + return ret; + scoped_guard(rcu) { read_lock(&tasklist_lock); idr_for_each_entry(&pid_ns->idr, pid, nr) { @@ -494,20 +511,20 @@ static struct run *runfd_create(unsigned int flags) user_ns = runfd_create_user_ns(); if (IS_ERR(user_ns)) { ret = PTR_ERR(user_ns); - goto err_put; + goto err_free; } c->user_ns = user_ns; ns_ref_active_get(user_ns); if (!ns_capable(user_ns, CAP_SYS_ADMIN)) { ret = -EPERM; - goto err_put; + goto err_free; } ns = create_new_namespaces(clone_flags, current, user_ns, NULL); if (IS_ERR(ns)) { ret = PTR_ERR(ns); - goto err_put; + goto err_free; } if (ns->time_ns != ns->time_ns_for_children) { @@ -518,10 +535,14 @@ static struct run *runfd_create(unsigned int flags) c->ns = ns; nsproxy_ns_active_get(ns); + ret = security_runfd_alloc(c, flags); + if (ret < 0) + goto err_free; + return c; -err_put: - runfd_put(c); +err_free: + runfd_free(c); return ERR_PTR(ret); } @@ -682,6 +703,10 @@ int runfd_prepare_fork(struct kernel_clone_args *args) if (ret) goto err_forking; + ret = security_runfd_clone(run); + if (ret) + goto err_forking; + args->run_file = no_free_ptr(file); args->run = run; args->run_bootstrapping = bootstrapping; diff --git a/security/lsm_init.c b/security/lsm_init.c index 8d5ecea99c46..ec74af5c8652 100644 --- a/security/lsm_init.c +++ b/security/lsm_init.c @@ -316,6 +316,9 @@ static void __init lsm_prepare(struct lsm_info *lsm) lsm_blob_size_update(&blobs->lbs_bpf_map, &blob_sizes.lbs_bpf_map); lsm_blob_size_update(&blobs->lbs_bpf_prog, &blob_sizes.lbs_bpf_prog); lsm_blob_size_update(&blobs->lbs_bpf_token, &blob_sizes.lbs_bpf_token); +#ifdef CONFIG_RUNFD + lsm_blob_size_update(&blobs->lbs_runfd, &blob_sizes.lbs_runfd); +#endif } /** @@ -460,6 +463,9 @@ int __init security_init(void) lsm_pr("blob(bpf_map) size %d\n", blob_sizes.lbs_bpf_map); lsm_pr("blob(bpf_prog) size %d\n", blob_sizes.lbs_bpf_prog); lsm_pr("blob(bpf_token) size %d\n", blob_sizes.lbs_bpf_token); +#ifdef CONFIG_RUNFD + lsm_pr("blob(run) size %d\n", blob_sizes.lbs_runfd); +#endif } if (blob_sizes.lbs_file) diff --git a/security/security.c b/security/security.c index c93c94103d2e..c8e47ac9ed39 100644 --- a/security/security.c +++ b/security/security.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include @@ -5804,3 +5805,74 @@ void security_initramfs_populated(void) { call_void_hook(initramfs_populated); } + +#ifdef CONFIG_RUNFD +/** + * security_runfd_alloc() - Allocate LSM blob and check run creation + * @run: the new run + * @flags: creation flags passed to runfd() + * + * Return: 0 on success, negative error code on failure. + */ +int security_runfd_alloc(struct run *run, unsigned int flags) +{ + int rc; + + rc = lsm_blob_alloc(&run->security, blob_sizes.lbs_runfd, + GFP_KERNEL); + if (unlikely(rc)) + return rc; + + rc = call_int_hook(runfd_alloc, run, flags); + if (unlikely(rc)) + security_runfd_free(run); + + return rc; +} + +/** + * security_runfd_free() - Release LSM blob from @run + * @run: the run being freed + */ +void security_runfd_free(struct run *run) +{ + call_void_hook(runfd_free, run); + kfree(run->security); + run->security = NULL; +} + +/** + * security_runfd_share() - Check permission to share @ns into @run + * @run: the target run + * @ns: the namespace being shared + * + * Return: 0 if permission is granted, negative error code on failure. + */ +int security_runfd_share(struct run *run, struct ns_common *ns) +{ + return call_int_hook(runfd_share, run, ns); +} + +/** + * security_runfd_clone() - Check permission to spawn into @run + * @run: the target run + * + * Return: 0 if permission is granted, negative error code on failure. + */ +int security_runfd_clone(struct run *run) +{ + return call_int_hook(runfd_clone, run); +} + +/** + * security_runfd_kill() - Check permission to signal @run + * @run: the target run + * @sig: signal number (or 0 for permission probe) + * + * Return: 0 if permission is granted, negative error code on failure. + */ +int security_runfd_kill(struct run *run, int sig) +{ + return call_int_hook(runfd_kill, run, sig); +} +#endif /* CONFIG_RUNFD */ -- 2.47.3