All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jarkko Sakkinen <jarkko@kernel.org>
To: linux-fsdevel@vger.kernel.org
Cc: Jarkko Sakkinen <jarkko@kernel.org>,
	bpf@vger.kernel.org, containers@lists.linux.dev,
	linux-api@vger.kernel.org, linux-arch@vger.kernel.org,
	linux-doc@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 <aleksa@amutable.com>,
	Alexander Mikhalitsyn <alex@amutable.com>,
	Alexander Viro <viro@zeniv.linux.org.uk>,
	Andy Lutomirski <luto@amacapital.net>,
	Arnd Bergmann <arnd@arndb.de>,
	Christian Brauner <brauner@kernel.org>,
	David Howells <dhowells@redhat.com>,
	Eric Biggers <ebiggers@kernel.org>,
	Giuseppe Scrivano <giuseppe@scrivano.org>,
	Hugh Dickins <hughd@google.com>, Jan Kara <jack@suse.cz>,
	Jeff Layton <jlayton@kernel.org>,
	Jonathan Corbet <corbet@lwn.net>,
	Josef Bacik <josef@toxicpanda.com>, Kees Cook <kees@kernel.org>,
	KP Singh <kpsingh@kernel.org>,
	Lennart Poettering <lennart@poettering.net>,
	Neal Gompa <neal@gompa.dev>, Oleg Nesterov <oleg@redhat.com>,
	Paul Moore <paul@paul-moore.com>,
	Pavel Tikhomirov <ptikhomirov@virtuozzo.com>,
	Serge Hallyn <sergeh@kernel.org>,
	Shuah Khan <skhan@linuxfoundation.org>,
	Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
Subject: [RFC PATCH v7 21/26] security: Add userland LSM hooks
Date: Sun, 22 Nov 2026 21:35:34 +0200	[thread overview]
Message-ID: <20261122193539.3334213-22-jarkko@kernel.org> (raw)
In-Reply-To: <20261122193539.3334213-1-jarkko@kernel.org>

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

Add lbs_userland and userland->security blob management alongside
security_userland_alloc(), security_userland_free(),
security_userland_share(), security_userland_clone(), and
security_userland_kill().

Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
 include/linux/lsm_hook_defs.h |  8 ++++
 include/linux/lsm_hooks.h     |  3 ++
 include/linux/security.h      | 37 ++++++++++++++++++
 include/linux/userland.h      |  3 ++
 kernel/userland.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 87d9d06710cb..be858bfdc534 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_USERLAND
+LSM_HOOK(int, 0, userland_alloc, struct userland *userland, unsigned int flags)
+LSM_HOOK(void, LSM_RET_VOID, userland_free, struct userland *userland)
+LSM_HOOK(int, 0, userland_share, struct userland *userland, struct ns_common *ns)
+LSM_HOOK(int, 0, userland_clone, struct userland *userland)
+LSM_HOOK(int, 0, userland_kill, struct userland *userland, int sig)
+#endif /* CONFIG_USERLAND */
diff --git a/include/linux/lsm_hooks.h b/include/linux/lsm_hooks.h
index 13621e9e233e..7c18370ac427 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_USERLAND
+	unsigned int lbs_userland;
+#endif
 };
 
 /*
diff --git a/include/linux/security.h b/include/linux/security.h
index 40831fab5470..2638ee1aa397 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 userland;
 
 enum lsm_event {
 	LSM_POLICY_CHANGE,
@@ -2469,4 +2470,40 @@ static inline void security_initramfs_populated(void)
 }
 #endif /* CONFIG_SECURITY */
 
+#ifdef CONFIG_USERLAND
+#ifdef CONFIG_SECURITY
+int security_userland_alloc(struct userland *userland, unsigned int flags);
+void security_userland_free(struct userland *userland);
+int security_userland_share(struct userland *userland, struct ns_common *ns);
+int security_userland_clone(struct userland *userland);
+int security_userland_kill(struct userland *userland, int sig);
+#else
+static inline int security_userland_alloc(struct userland *userland,
+					  unsigned int flags)
+{
+	return 0;
+}
+
+static inline void security_userland_free(struct userland *userland)
+{
+}
+
+static inline int security_userland_share(struct userland *userland,
+					  struct ns_common *ns)
+{
+	return 0;
+}
+
+static inline int security_userland_clone(struct userland *userland)
+{
+	return 0;
+}
+
+static inline int security_userland_kill(struct userland *userland, int sig)
+{
+	return 0;
+}
+#endif /* CONFIG_SECURITY */
+#endif /* CONFIG_USERLAND */
+
 #endif /* ! __LINUX_SECURITY_H */
diff --git a/include/linux/userland.h b/include/linux/userland.h
index a9b99642c46e..eecee199fad7 100644
--- a/include/linux/userland.h
+++ b/include/linux/userland.h
@@ -61,6 +61,9 @@ enum userland_state {
 struct userland {
 	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/kernel/userland.c b/kernel/userland.c
index 2375442733b6..0a6c6179f629 100644
--- a/kernel/userland.c
+++ b/kernel/userland.c
@@ -40,15 +40,13 @@
 #include <linux/uts_namespace.h>
 #include <net/net_namespace.h>
 #include <linux/idr.h>
+#include <linux/security.h>
 #include <uapi/linux/userland.h>
 
 #include "exit.h"
 
-void userland_put(struct userland *c)
+static void userland_free(struct userland *c)
 {
-	if (!c || !refcount_dec_and_test(&c->usage))
-		return;
-
 	if (c->ns)
 		put_nsproxy(c->ns);
 	if (c->init_pid)
@@ -60,6 +58,15 @@ void userland_put(struct userland *c)
 	kfree(c);
 }
 
+void userland_put(struct userland *c)
+{
+	if (!c || !refcount_dec_and_test(&c->usage))
+		return;
+
+	security_userland_free(c);
+	userland_free(c);
+}
+
 static __poll_t userland_poll(struct file *file, poll_table *wait)
 {
 	struct userland *userland = file->private_data;
@@ -136,6 +143,7 @@ static long userland_share_nsfd(struct userland *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;
@@ -167,6 +175,10 @@ static long userland_share_nsfd(struct userland *c, unsigned long arg)
 	    !ns_capable(ns->ops->owner(ns), CAP_SYS_ADMIN))
 		return -EPERM;
 
+	ret = security_userland_share(c, ns);
+	if (ret)
+		return ret;
+
 	scoped_guard(spinlock, &c->lock) {
 		if (c->state != USERLAND_INIT)
 			return -EBUSY;
@@ -315,6 +327,7 @@ int userland_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))
@@ -324,6 +337,10 @@ int userland_send_signal(struct file *file, int sig,
 	if (READ_ONCE(userland->state) != USERLAND_RUNNING)
 		return -ESRCH;
 
+	ret = security_userland_kill(userland, sig);
+	if (ret)
+		return ret;
+
 	scoped_guard(rcu) {
 		read_lock(&tasklist_lock);
 		idr_for_each_entry(&pid_ns->idr, pid, nr) {
@@ -489,20 +506,20 @@ static struct userland *userland_create(unsigned int flags)
 	user_ns = userland_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) {
@@ -513,10 +530,14 @@ static struct userland *userland_create(unsigned int flags)
 	c->ns = ns;
 	nsproxy_ns_active_get(ns);
 
+	ret = security_userland_alloc(c, flags);
+	if (ret < 0)
+		goto err_free;
+
 	return c;
 
-err_put:
-	userland_put(c);
+err_free:
+	userland_free(c);
 	return ERR_PTR(ret);
 }
 
@@ -610,6 +631,10 @@ int userland_prepare_fork(struct kernel_clone_args *args)
 	if (!pidns_is_ancestor(pid_ns, task_active_pid_ns(current)))
 		return -EXDEV;
 
+	ret = security_userland_clone(userland);
+	if (ret)
+		return ret;
+
 	ret = mnt_ns_get_root(userland->ns->mnt_ns, &root);
 	if (ret)
 		return ret;
diff --git a/security/lsm_init.c b/security/lsm_init.c
index 8d5ecea99c46..b1af1d922c58 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_USERLAND
+	lsm_blob_size_update(&blobs->lbs_userland, &blob_sizes.lbs_userland);
+#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_USERLAND
+		lsm_pr("blob(userland) size %d\n", blob_sizes.lbs_userland);
+#endif
 	}
 
 	if (blob_sizes.lbs_file)
diff --git a/security/security.c b/security/security.c
index 4727b8347fc4..8c3a5df21938 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/userland.h>
 #include <net/flow.h>
 #include <net/sock.h>
 
@@ -5795,3 +5796,74 @@ void security_initramfs_populated(void)
 {
 	call_void_hook(initramfs_populated);
 }
+
+#ifdef CONFIG_USERLAND
+/**
+ * security_userland_alloc() - Allocate LSM blob and check userland creation
+ * @userland: the new userland
+ * @flags: creation flags passed to userland_open()
+ *
+ * Return: 0 on success, negative error code on failure.
+ */
+int security_userland_alloc(struct userland *userland, unsigned int flags)
+{
+	int rc;
+
+	rc = lsm_blob_alloc(&userland->security, blob_sizes.lbs_userland,
+			    GFP_KERNEL);
+	if (unlikely(rc))
+		return rc;
+
+	rc = call_int_hook(userland_alloc, userland, flags);
+	if (unlikely(rc))
+		security_userland_free(userland);
+
+	return rc;
+}
+
+/**
+ * security_userland_free() - Release LSM blob from @userland
+ * @userland: the userland being freed
+ */
+void security_userland_free(struct userland *userland)
+{
+	call_void_hook(userland_free, userland);
+	kfree(userland->security);
+	userland->security = NULL;
+}
+
+/**
+ * security_userland_share() - Check permission to share @ns into @userland
+ * @userland: the target userland
+ * @ns: the namespace being shared
+ *
+ * Return: 0 if permission is granted, negative error code on failure.
+ */
+int security_userland_share(struct userland *userland, struct ns_common *ns)
+{
+	return call_int_hook(userland_share, userland, ns);
+}
+
+/**
+ * security_userland_clone() - Check permission to spawn into @userland
+ * @userland: the target userland
+ *
+ * Return: 0 if permission is granted, negative error code on failure.
+ */
+int security_userland_clone(struct userland *userland)
+{
+	return call_int_hook(userland_clone, userland);
+}
+
+/**
+ * security_userland_kill() - Check permission to signal @userland
+ * @userland: the target userland
+ * @sig: signal number (or 0 for permission probe)
+ *
+ * Return: 0 if permission is granted, negative error code on failure.
+ */
+int security_userland_kill(struct userland *userland, int sig)
+{
+	return call_int_hook(userland_kill, userland, sig);
+}
+#endif /* CONFIG_USERLAND */
-- 
2.47.3


  parent reply	other threads:[~2026-11-22 19:37 UTC|newest]

Thread overview: 75+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-22 19:35 [RFC PATCH v7 00/26] userland: Userland containers Jarkko Sakkinen
2026-11-22 19:35 ` [RFC PATCH v7 01/26] userns: Factor out idmap validation and commit helpers Jarkko Sakkinen
2026-11-22 19:35 ` [RFC PATCH v7 02/26] userns: Add userns_set_idmap() system call Jarkko Sakkinen
2026-11-22 19:35 ` [RFC PATCH v7 03/26] arch: Wire up " Jarkko Sakkinen
2026-11-22 19:35 ` [RFC PATCH v7 04/26] mount: Add mnt_ns_get_root() Jarkko Sakkinen
2026-11-22 19:35 ` [RFC PATCH v7 05/26] mount: Enforce mount_too_revealing() for foreign mntns Jarkko Sakkinen
2026-11-22 19:35 ` [RFC PATCH v7 06/26] mount: Support MOVE_MOUNT_T_MNTNS in move_mount() Jarkko Sakkinen
2026-11-22 20:02   ` sashiko-bot
2026-11-22 19:35 ` [RFC PATCH v7 07/26] mount: Support MOVE_MOUNT_CLONE with MOVE_MOUNT_T_MNTNS Jarkko Sakkinen
2026-11-22 20:28   ` bot+bpf-ci
2026-11-23 14:05     ` Jarkko Sakkinen
2026-11-22 19:35 ` [RFC PATCH v7 08/26] userns: Factor out install_user_ns_cred() Jarkko Sakkinen
2026-11-22 19:35 ` [RFC PATCH v7 09/26] nsproxy: Expose create_new_namespaces() Jarkko Sakkinen
2026-11-22 19:35 ` [RFC PATCH v7 10/26] userland: Add core infrastructure Jarkko Sakkinen
2026-11-22 20:28   ` bot+bpf-ci
2026-11-22 19:35 ` [RFC PATCH v7 11/26] userland: Track init lifecycle via copy_process() and pid_namespace Jarkko Sakkinen
2026-11-22 20:09   ` bot+bpf-ci
2026-11-22 19:35 ` [RFC PATCH v7 12/26] userland: Add userland_open() system call Jarkko Sakkinen
2026-11-22 19:35 ` [RFC PATCH v7 13/26] userland: Add USERLAND_GET_NSFD ioctl Jarkko Sakkinen
2026-11-22 19:35 ` [RFC PATCH v7 14/26] userland: Add USERLAND_SHARE_NSFD ioctl Jarkko Sakkinen
2026-11-22 19:35 ` [RFC PATCH v7 15/26] exit: Support P_USERLAND in waitid() Jarkko Sakkinen
2026-11-22 19:35 ` [RFC PATCH v7 16/26] signal: Support userland fds in pidfd_send_signal() Jarkko Sakkinen
2026-11-22 19:35 ` [RFC PATCH v7 17/26] fork: Support CLONE_INTO_USERLAND in clone3() Jarkko Sakkinen
2026-11-22 20:01   ` sashiko-bot
2026-11-22 20:28   ` bot+bpf-ci
2026-11-22 19:35 ` [RFC PATCH v7 18/26] fs: Support FSCONFIG_SET_USERLAND in fsconfig() Jarkko Sakkinen
2026-11-22 19:58   ` sashiko-bot
2026-11-22 20:28   ` bot+bpf-ci
2026-11-22 19:35 ` [RFC PATCH v7 19/26] proc: Implement ->set_userland() fs_context operation Jarkko Sakkinen
2026-11-22 19:35 ` [RFC PATCH v7 20/26] ipc/mqueue: " Jarkko Sakkinen
2026-11-22 19:35 ` Jarkko Sakkinen [this message]
2026-11-22 19:35 ` [RFC PATCH v7 22/26] bpf: Register userland LSM hooks Jarkko Sakkinen
2026-11-22 19:35 ` [RFC PATCH v7 23/26] arch: Wire up userland_open() system call Jarkko Sakkinen
2026-11-22 19:35 ` [RFC PATCH v7 24/26] docs: Document userland uAPI Jarkko Sakkinen
2026-11-22 20:09   ` bot+bpf-ci
2026-11-22 19:35 ` [RFC PATCH v7 25/26] selftests/userland: Add userland test suite Jarkko Sakkinen
2026-11-22 19:52   ` sashiko-bot
2026-11-22 20:28   ` bot+bpf-ci
2026-11-22 19:35 ` [RFC PATCH v7 26/26] MAINTAINERS: Add entry for USERLAND Jarkko Sakkinen
2026-11-22 20:02 ` [RFC PATCH v7 00/26] userland: Userland containers sashiko-bot
2026-11-23  0:10 ` Jarkko Sakkinen
2026-11-23  0:13   ` Jarkko Sakkinen
2026-11-23  0:22     ` Jarkko Sakkinen
2026-11-23  0:26       ` Jarkko Sakkinen
2026-11-23  1:08         ` Jarkko Sakkinen
2026-11-23  0:21 ` Alexei Starovoitov
2026-11-23  0:59   ` Jarkko Sakkinen
2026-11-23  1:17     ` Jarkko Sakkinen
2026-11-23 13:25       ` Jarkko Sakkinen
2026-11-23 16:20         ` Jarkko Sakkinen
2026-11-27 21:41     ` Askar Safin
2026-11-27 22:04       ` Jarkko Sakkinen
2026-11-27 22:08         ` Jarkko Sakkinen
2026-11-27 22:43           ` Jarkko Sakkinen
2026-11-27 23:15             ` Jarkko Sakkinen
2026-11-28  1:50               ` Askar Safin
2026-11-28  9:53                 ` Jarkko Sakkinen
2026-11-28  9:58                   ` Jarkko Sakkinen
2026-11-28 10:36                   ` Jarkko Sakkinen
2026-11-28 11:26                     ` Jarkko Sakkinen
2026-11-30  3:22                   ` Askar Safin
2026-11-24  1:19 ` Jarkko Sakkinen
2026-11-24  1:28   ` Jarkko Sakkinen
2026-11-24  3:37     ` Jarkko Sakkinen
2026-11-24  3:58       ` Jarkko Sakkinen
2026-11-24  8:19         ` Jarkko Sakkinen
2026-11-24  8:58           ` Jarkko Sakkinen
2026-11-24 18:35             ` Jarkko Sakkinen
2026-11-24 22:03 ` Jarkko Sakkinen
2026-11-25 16:24   ` Jarkko Sakkinen
2026-11-28 17:55   ` Jarkko Sakkinen
2026-11-28 19:44     ` Jarkko Sakkinen
2026-11-28 20:38       ` Jarkko Sakkinen
2026-11-28  0:55 ` Askar Safin
2026-11-28  1:08   ` Jarkko Sakkinen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261122193539.3334213-22-jarkko@kernel.org \
    --to=jarkko@kernel.org \
    --cc=aleksa@amutable.com \
    --cc=alex@amutable.com \
    --cc=arnd@arndb.de \
    --cc=bpf@vger.kernel.org \
    --cc=brauner@kernel.org \
    --cc=containers@lists.linux.dev \
    --cc=corbet@lwn.net \
    --cc=dhowells@redhat.com \
    --cc=ebiggers@kernel.org \
    --cc=giuseppe@scrivano.org \
    --cc=hughd@google.com \
    --cc=jack@suse.cz \
    --cc=jlayton@kernel.org \
    --cc=josef@toxicpanda.com \
    --cc=kees@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=lennart@poettering.net \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=luto@amacapital.net \
    --cc=neal@gompa.dev \
    --cc=oleg@redhat.com \
    --cc=paul@paul-moore.com \
    --cc=ptikhomirov@virtuozzo.com \
    --cc=sergeh@kernel.org \
    --cc=skhan@linuxfoundation.org \
    --cc=viro@zeniv.linux.org.uk \
    --cc=zygmunt.krynicki@canonical.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.