From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8559C606A338 for ; Tue, 1 Dec 2026 03:12:51 +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 kanga.kvack.org (Postfix) id 9B0926B05DE; Mon, 30 Nov 2026 22:12:11 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 939746B05DF; Mon, 30 Nov 2026 22:12:11 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 801806B05E0; Mon, 30 Nov 2026 22:12:11 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 413776B05DE for ; Mon, 30 Nov 2026 22:12:11 -0500 (EST) Received: from smtpin02.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay10.hostedemail.com (Postfix) with ESMTP id A4336C0D6F for ; Tue, 1 Dec 2026 03:12:09 +0000 (UTC) X-FDA: 85493940378.02.8196A3F Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by imf25.hostedemail.com (Postfix) with ESMTP id E2205A000A for ; Tue, 1 Dec 2026 03:12:07 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=c4Ur7f5h; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf25.hostedemail.com: domain of jarkko@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=jarkko@kernel.org ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796094728; b=ROUemZGh6XKa0y7SsLaAOQEEWwgkK52cfJMuUvnPi5yrgEt0QVtj2kzNnH/NDFF20KVDec Yo2SFSsPQJGIWzVAljYs3W6Lds4FWnFazYb3Y2fagz00IOwxbDH/WVbhwTOUPwnf8YVHff O0YlqDyjxqlPO9fbfL8IDh/n67KUy+M= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796094728; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=dT2F4h2Aw9AC/6/9g//hpPvOSipXMPMuXeqbLHCi7mk=; b=gKB+O9oKUS9pzyKXrb6RqVhvmaNd8ywzxF9WiO7KkPxc+BXgVdxrd6Bx2aZ4FefaZodrPO QFohq5WOBBhdM5VwDH+vCdbq9yD6awAWV8eNM1yNsVv0ucTFlnsfP2EulyaDXAOGf3Rftu bTKbA+RJoxp6CMC/yA5JVE8GOYHuF5o= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=c4Ur7f5h; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf25.hostedemail.com: domain of jarkko@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=jarkko@kernel.org Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with UTF8SMTP id 1DF8C4060A; Tue, 1 Dec 2026 03:12:07 +0000 (UTC) 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-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Stat-Signature: y9yz15oty8s3jxhn4xs9c3erriu8dpuf X-Rspamd-Server: rspam03 X-Rspam-User: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Rspamd-Queue-Id: E2205A000A X-HE-Tag: 1796094727-729123 X-HE-Meta: U2FsdGVkX19q7zAUOD7LPnnoIWq9KaZv55ktOYPZkKkPsQcnuOiMN94OTNGG9ciQuY/JlSEjFZeBF81OZnSGjJuRR3e0pb8rTm6PasUfniDI7rsqHHQqTc56evLlFtkHYLdBg2jsnpnro+NpVpm5+qUuZSEQU6pVOCreI0CqXCs/RvqB/8TKUi5+QPN7ii6d+J+MzsWEnY0ZkpkCJ8iHTZ4H6ctWajSdy9kDONSh8EkuqwVOkd6AHuNAGy650TAOrvBBO4WMtl6lbdFBdomT9qiFMfkK1hzPakFk2YBBBVq6AJRDuYaOg0tMCjSOITqKXjMzcc6/N63rBgbU+OtBhN1QQiNNV7ene2xKVaOO6fdQdRyY9U8Qz9FA1DAwy3qDuJ2lni+SAQJP35xWFq8u7ZryHBolGXjSTwdJGQSjxs/C3h45IvfS/B5g0fPEJCLqQa9x+O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ner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 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