[RFC PATCH v10 22/28] security: Add run LSM hooks
From: Jarkko Sakkinen
Date: Fri Dec 04 2026 - 22:29:52 EST
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 <jarkko@xxxxxxxxxx>
---
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 <linux/uts_namespace.h>
#include <net/net_namespace.h>
#include <linux/idr.h>
+#include <linux/security.h>
#include <uapi/linux/runfd.h>
#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 <linux/overflow.h>
#include <linux/perf_event.h>
#include <linux/fs.h>
+#include <linux/runfd.h>
#include <net/flow.h>
#include <net/sock.h>
@@ -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