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=1796094727; b=h1aZ9IfyIZqRF/dYMOH8wjeht/cwNAaissjniXRIo1riWefG9SixrqxwgNORrsJBhjq3 KC94ZYUyaCP4MPkvl/BpQDwTjZRmuz0pZn4tavnhCoH+OWqQkYiSbBnlLBMj3KHAmeHWL mFxC12T8W9Y+80AXFcoarxu/96EMrRmUWDiaYi1/toQcGrdNB/fmHrzlH92V2OIGVib8b faBOGJtAYvwwt6dz+GfEwiMNzg5lBgWT+2V9bIui9zN9EKkwyd5My4h484LBX1H9HV1ms yagoNvTNi0R4t64Mg7bpwDgHoV9XCnx4McYVkvQQDO6zG5BN0IXk2shJDwI/hgF89OA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796094727; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=dT2F4h2Aw9AC/6/9g//hpPvOSipXMPMuXeqbLHCi7mk=; b=cb54UFtTRbdXfrNHwwE9m2BL9mqh5aXOFmhwWtpsnOMPKRpMlzRGd5nIPvRq3GMxZhhD m/dFXIRI3pEQ2OIb8LHMHzhsYJp2k3+Jg1he54a8/DfKBF3MRTi2R18BeQ1MLBDjM6bhr rCGb623ZF8r3yvLBuwe4MUaOX0PlprHYMRxvO049kNGIxWssshnEoATzTvuOV2TeaYqoI 3cyP0JhSxueRRw8IugpWXs+LASqEhCBKnG43805eZ8rAhUZwx8GU5yqjsk1xrrrm8Xnfq x+53ElXeKuWw8Oa7nxFQdHTp8GlfD8DLFUCJbbXwdWB3acGJQX0n/moKjLa1H49Ch/Q== 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=c4Ur7f5h; 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 2F24A606A680; Tue, 1 Dec 2026 03:12:07 +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=c4Ur7f5h Received: by smtp.kernel.org (Postfix) with UTF8SMTPSA id 1E3831F00898; Tue, 1 Dec 2026 03:12:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796094726; bh=dT2F4h2Aw9AC/6/9g//hpPvOSipXMPMuXeqbLHCi7mk=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=c4Ur7f5hClPlmFCtipV+vBrTx87L4nzL2vz68orKgWD8IS/ZLErHFWVnVDv28Z31u UcJrK2qluadaEFrb+O6lLcYqHsEGEWVskJ7QdxOF19mOXVfGdUXhXcCgaJpJsT4jDd 3/qMsm662n1r14v2kALK4YV7F8hsHnja8NWPFQ73RtWM69GTHTWSqZHv+EMmq+kMHd Y+h82NSM535tWPE/iLTkh1OP2wma3lxpiIsvoT/Utl7V9ptPxuu+bUxKqLG4LuLHGs XKnmAz/e2iLwfqmJxuY/cUOLCTkcldNPctV2PldPVVJw8BW4UKKv0Qdll+6doLGVtN kiuDoIOCG6Tlg== 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 v9 23/29] security: Add nsexec LSM hooks Date: Tue, 1 Dec 2026 05:09:55 +0200 Message-ID: <20261201031002.593830-24-jarkko@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261201031002.593830-1-jarkko@kernel.org> References: <20261201031002.593830-1-jarkko@kernel.org> X-Mailing-List: bpf@vger.kernel.org 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 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 --- 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 #include #include +#include #include #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 #include #include +#include #include #include @@ -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