[RFC PATCH v9 23/29] security: Add nsexec LSM hooks

Jarkko Sakkinen jarkko at kernel.org
Tue Dec 1 03:09:55 UTC 2026


Security modules need visibility into nsexec creation, teardown,
namespace sharing, process entry, and nsexec-wide signal delivery so
policies can attach per-nsexec security state and mediate operations
on nsexec file descriptors.

Add lbs_nsexec and nsexec->security blob management alongside
security_nsexec_alloc(), security_nsexec_free(),
security_nsexec_share(), security_nsexec_clone(), and
security_nsexec_kill().

Signed-off-by: Jarkko Sakkinen <jarkko at kernel.org>
---
 include/linux/lsm_hook_defs.h |  8 ++++
 include/linux/lsm_hooks.h     |  3 ++
 include/linux/nsexec.h        |  3 ++
 include/linux/security.h      | 37 ++++++++++++++++++
 kernel/nsexec.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..de263f05798f 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_NSEXEC
+LSM_HOOK(int, 0, nsexec_alloc, struct nsexec *nsexec, unsigned int flags)
+LSM_HOOK(void, LSM_RET_VOID, nsexec_free, struct nsexec *nsexec)
+LSM_HOOK(int, 0, nsexec_share, struct nsexec *nsexec, struct ns_common *ns)
+LSM_HOOK(int, 0, nsexec_clone, struct nsexec *nsexec)
+LSM_HOOK(int, 0, nsexec_kill, struct nsexec *nsexec, int sig)
+#endif /* CONFIG_NSEXEC */
diff --git a/include/linux/lsm_hooks.h b/include/linux/lsm_hooks.h
index 13621e9e233e..b32d540504fc 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_NSEXEC
+	unsigned int lbs_nsexec;
+#endif
 };
 
 /*
diff --git a/include/linux/nsexec.h b/include/linux/nsexec.h
index 5e1d40e51205..750199922a7d 100644
--- a/include/linux/nsexec.h
+++ b/include/linux/nsexec.h
@@ -64,6 +64,9 @@ enum nsexec_state {
 struct nsexec {
 	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..2c613978e584 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 nsexec;
 
 enum lsm_event {
 	LSM_POLICY_CHANGE,
@@ -2471,4 +2472,40 @@ static inline void security_initramfs_populated(void)
 }
 #endif /* CONFIG_SECURITY */
 
+#ifdef CONFIG_NSEXEC
+#ifdef CONFIG_SECURITY
+int security_nsexec_alloc(struct nsexec *nsexec, unsigned int flags);
+void security_nsexec_free(struct nsexec *nsexec);
+int security_nsexec_share(struct nsexec *nsexec, struct ns_common *ns);
+int security_nsexec_clone(struct nsexec *nsexec);
+int security_nsexec_kill(struct nsexec *nsexec, int sig);
+#else
+static inline int security_nsexec_alloc(struct nsexec *nsexec,
+					 unsigned int flags)
+{
+	return 0;
+}
+
+static inline void security_nsexec_free(struct nsexec *nsexec)
+{
+}
+
+static inline int security_nsexec_share(struct nsexec *nsexec,
+					 struct ns_common *ns)
+{
+	return 0;
+}
+
+static inline int security_nsexec_clone(struct nsexec *nsexec)
+{
+	return 0;
+}
+
+static inline int security_nsexec_kill(struct nsexec *nsexec, int sig)
+{
+	return 0;
+}
+#endif /* CONFIG_SECURITY */
+#endif /* CONFIG_NSEXEC */
+
 #endif /* ! __LINUX_SECURITY_H */
diff --git a/kernel/nsexec.c b/kernel/nsexec.c
index 761f994977c1..4ba295e86ccd 100644
--- a/kernel/nsexec.c
+++ b/kernel/nsexec.c
@@ -41,15 +41,13 @@
 #include <linux/uts_namespace.h>
 #include <net/net_namespace.h>
 #include <linux/idr.h>
+#include <linux/security.h>
 #include <uapi/linux/nsexec.h>
 
 #include "exit.h"
 
-void nsexec_put(struct nsexec *c)
+static void nsexec_free(struct nsexec *c)
 {
-	if (!c || !refcount_dec_and_test(&c->usage))
-		return;
-
 	if (c->ns)
 		put_nsproxy(c->ns);
 	if (c->init_pid)
@@ -61,6 +59,15 @@ void nsexec_put(struct nsexec *c)
 	kfree(c);
 }
 
+void nsexec_put(struct nsexec *c)
+{
+	if (!c || !refcount_dec_and_test(&c->usage))
+		return;
+
+	security_nsexec_free(c);
+	nsexec_free(c);
+}
+
 static __poll_t nsexec_poll(struct file *file, poll_table *wait)
 {
 	struct nsexec *nsexec = file->private_data;
@@ -143,6 +150,7 @@ static long nsexec_share_nsfd(struct nsexec *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;
@@ -173,6 +181,10 @@ static long nsexec_share_nsfd(struct nsexec *c, unsigned long arg)
 	if (!nsexec_may_use_ns(ns))
 		return -EPERM;
 
+	ret = security_nsexec_share(c, ns);
+	if (ret)
+		return ret;
+
 	scoped_guard(spinlock, &c->lock) {
 		if (c->state != NSEXEC_INIT)
 			return -EBUSY;
@@ -321,6 +333,7 @@ int nsexec_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))
@@ -330,6 +343,10 @@ int nsexec_send_signal(struct file *file, int sig,
 	if (READ_ONCE(nsexec->state) != NSEXEC_RUNNING)
 		return -ESRCH;
 
+	ret = security_nsexec_kill(nsexec, sig);
+	if (ret)
+		return ret;
+
 	scoped_guard(rcu) {
 		read_lock(&tasklist_lock);
 		idr_for_each_entry(&pid_ns->idr, pid, nr) {
@@ -493,20 +510,20 @@ static struct nsexec *nsexec_create(unsigned int flags)
 	user_ns = nsexec_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) {
@@ -517,10 +534,14 @@ static struct nsexec *nsexec_create(unsigned int flags)
 	c->ns = ns;
 	nsproxy_ns_active_get(ns);
 
+	ret = security_nsexec_alloc(c, flags);
+	if (ret < 0)
+		goto err_free;
+
 	return c;
 
-err_put:
-	nsexec_put(c);
+err_free:
+	nsexec_free(c);
 	return ERR_PTR(ret);
 }
 
@@ -681,6 +702,10 @@ int nsexec_prepare_fork(struct kernel_clone_args *args)
 	if (ret)
 		goto err_forking;
 
+	ret = security_nsexec_clone(nsexec);
+	if (ret)
+		goto err_forking;
+
 	args->nsexec_file = no_free_ptr(file);
 	args->nsexec = nsexec;
 	args->nsexec_bootstrapping = bootstrapping;
diff --git a/security/lsm_init.c b/security/lsm_init.c
index 8d5ecea99c46..374a09c124a7 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_NSEXEC
+	lsm_blob_size_update(&blobs->lbs_nsexec, &blob_sizes.lbs_nsexec);
+#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_NSEXEC
+		lsm_pr("blob(nsexec) size %d\n", blob_sizes.lbs_nsexec);
+#endif
 	}
 
 	if (blob_sizes.lbs_file)
diff --git a/security/security.c b/security/security.c
index b4d60dcf4880..5d16e248ce37 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/nsexec.h>
 #include <net/flow.h>
 #include <net/sock.h>
 
@@ -5796,3 +5797,74 @@ void security_initramfs_populated(void)
 {
 	call_void_hook(initramfs_populated);
 }
+
+#ifdef CONFIG_NSEXEC
+/**
+ * security_nsexec_alloc() - Allocate LSM blob and check nsexec creation
+ * @nsexec: the new nsexec
+ * @flags: creation flags passed to nsexec_open()
+ *
+ * Return: 0 on success, negative error code on failure.
+ */
+int security_nsexec_alloc(struct nsexec *nsexec, unsigned int flags)
+{
+	int rc;
+
+	rc = lsm_blob_alloc(&nsexec->security, blob_sizes.lbs_nsexec,
+			    GFP_KERNEL);
+	if (unlikely(rc))
+		return rc;
+
+	rc = call_int_hook(nsexec_alloc, nsexec, flags);
+	if (unlikely(rc))
+		security_nsexec_free(nsexec);
+
+	return rc;
+}
+
+/**
+ * security_nsexec_free() - Release LSM blob from @nsexec
+ * @nsexec: the nsexec being freed
+ */
+void security_nsexec_free(struct nsexec *nsexec)
+{
+	call_void_hook(nsexec_free, nsexec);
+	kfree(nsexec->security);
+	nsexec->security = NULL;
+}
+
+/**
+ * security_nsexec_share() - Check permission to share @ns into @nsexec
+ * @nsexec: the target nsexec
+ * @ns: the namespace being shared
+ *
+ * Return: 0 if permission is granted, negative error code on failure.
+ */
+int security_nsexec_share(struct nsexec *nsexec, struct ns_common *ns)
+{
+	return call_int_hook(nsexec_share, nsexec, ns);
+}
+
+/**
+ * security_nsexec_clone() - Check permission to spawn into @nsexec
+ * @nsexec: the target nsexec
+ *
+ * Return: 0 if permission is granted, negative error code on failure.
+ */
+int security_nsexec_clone(struct nsexec *nsexec)
+{
+	return call_int_hook(nsexec_clone, nsexec);
+}
+
+/**
+ * security_nsexec_kill() - Check permission to signal @nsexec
+ * @nsexec: the target nsexec
+ * @sig: signal number (or 0 for permission probe)
+ *
+ * Return: 0 if permission is granted, negative error code on failure.
+ */
+int security_nsexec_kill(struct nsexec *nsexec, int sig)
+{
+	return call_int_hook(nsexec_kill, nsexec, sig);
+}
+#endif /* CONFIG_NSEXEC */
-- 
2.47.3




More information about the Linux-security-module-archive mailing list