[PATCH 2/2] ns: don't call the namespace LSM hooks for kernel-internal namespaces
Alexander Mikhalitsyn
alex at amutable.com
Fri Nov 20 14:03:07 UTC 2026
On Fri, 2026-11-20 at 13:29 +0100, Christian Brauner wrote:
> mount_subtree() allocates a private anonymous mount namespace so that
> automounts triggered while it looks up the path have somewhere to go.
> That namespace never becomes anyone's namespace and it's gone again
> before mount_subtree() returns. btrfs goes through it on every mount:
>
> btrfs_get_tree()
> -> btrfs_get_tree_subvol()
> -> mount_subvol()
> -> mount_subtree()
>
> and NFS on every NFSv4 mount and whenever it follows a referral:
>
> nfs4_try_get_tree() / nfs4_get_referral_tree()
> -> do_nfs4_mount()
> -> mount_subtree()
>
> But __ns_common_init() calls security_namespace_init() for it like
> for
> any other namespace. Since commit 15dd86021b57a ("landlock: Enforce
> namespace use restrictions") Landlock enforces
> LANDLOCK_PERMISSION_NAMESPACE_USE in its namespace_init hook. So a
> task
> in a domain that handles that permission without allowing CLONE_NEWNS
> can't mount btrfs or NFSv4 anymore. mount(2) fails with EPERM even
> though the caller never asked for a mount namespace.
>
> Mark such namespaces as kernel-internal and don't call the namespace
> LSM hooks for them at all. That includes namespace_free. Currently
> security_namespace_free() only calls the hook if an LSM requested a
> namespace blob and none does. But we shouldn't rely on that.
> Otherwise
> a bpf program attached to namespace_free would see namespaces go away
> that it never saw being created.
>
> The anonymous mount namespaces created by open_tree(OPEN_TREE_CLONE)
> and fsmount() are handed to userspace and keep going through the
> hooks.
> Landlock covers them on purpose.
>
> Fixes: f675d2e95569 ("lsm: add LSM blob and hooks for namespaces")
> Reported-by: Zhiqi Zhang <zhiqizhangg at gmail.com>
> Closes:
> https://lore.kernel.org/20261114141323.19029-1-zhiqizhangg@gmail.com
> Signed-off-by: Christian Brauner (Amutable) <brauner at kernel.org>
Looks good to me.
Reviewed-by: Alexander Mikhalitsyn <alex at amutable.com>
> ---
> fs/namespace.c | 9 +++++++--
> include/linux/ns/ns_common_types.h | 2 ++
> include/linux/ns_common.h | 20 +++++++++++++-------
> kernel/nscommon.c | 9 ++++++---
> 4 files changed, 28 insertions(+), 12 deletions(-)
>
> diff --git a/fs/namespace.c b/fs/namespace.c
> index 756f9011cbf1d..82738b818c1d0 100644
> --- a/fs/namespace.c
> +++ b/fs/namespace.c
> @@ -2355,6 +2355,7 @@ void drop_collected_paths(const struct path
> *paths, const struct path *prealloc)
>
> enum mnt_ns_flags_t {
> MNT_NS_ANON = (1 << 0),
> + MNT_NS_KERNEL = (1 << 1),
> };
>
> static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *,
> @@ -4377,10 +4378,14 @@ static void free_mnt_ns(struct mnt_namespace
> *ns)
> static struct mnt_namespace *alloc_mnt_ns(struct user_namespace
> *user_ns,
> enum mnt_ns_flags_t flags)
> {
> + enum ns_common_flags_t ns_flags = 0;
> struct mnt_namespace *new_ns;
> struct ucounts *ucounts;
> int ret;
>
> + if (flags & MNT_NS_KERNEL)
> + ns_flags |= NS_COMMON_KERNEL;
> +
> ucounts = inc_mnt_namespaces(user_ns);
> if (!ucounts)
> return ERR_PTR(-ENOSPC);
> @@ -4392,7 +4397,7 @@ static struct mnt_namespace
> *alloc_mnt_ns(struct user_namespace *user_ns,
> }
>
> if (flags & MNT_NS_ANON)
> - ret = ns_common_init_inum(new_ns, MNT_NS_ANON_INO);
> + ret = ns_common_init_inum(new_ns, MNT_NS_ANON_INO,
> ns_flags);
> else
> ret = ns_common_init(new_ns);
> if (ret) {
> @@ -4515,7 +4520,7 @@ struct dentry *mount_subtree(struct vfsmount
> *m, const char *name)
> struct path path;
> int err;
>
> - ns = alloc_mnt_ns(&init_user_ns, MNT_NS_ANON);
> + ns = alloc_mnt_ns(&init_user_ns, MNT_NS_ANON |
> MNT_NS_KERNEL);
> if (IS_ERR(ns)) {
> mntput(m);
> return ERR_CAST(ns);
> diff --git a/include/linux/ns/ns_common_types.h
> b/include/linux/ns/ns_common_types.h
> index 47a5cfe2326a3..5f8b0930ae065 100644
> --- a/include/linux/ns/ns_common_types.h
> +++ b/include/linux/ns/ns_common_types.h
> @@ -102,6 +102,7 @@ extern const struct proc_ns_operations
> utsns_operations;
> * __ns_ref_active = 1 and remain active forever.
> *
> * @ns_type: type of namespace (e.g., CLONE_NEWNET)
> + * @ns_flags: NS_COMMON_* flags
> * @stashed: cached dentry to be used by the vfs
> * @ops: namespace operations
> * @inum: namespace inode number (quickly recycled for non-initial
> namespaces)
> @@ -113,6 +114,7 @@ struct ns_common {
> refcount_t __ns_ref; /* do not use directly */
> } ____cacheline_aligned_in_smp;
> u32 ns_type;
> + u32 ns_flags;
> struct dentry *stashed;
> const struct proc_ns_operations *ops;
> unsigned int inum;
> diff --git a/include/linux/ns_common.h b/include/linux/ns_common.h
> index e8b2bf0da5ede..524b77904d3b4 100644
> --- a/include/linux/ns_common.h
> +++ b/include/linux/ns_common.h
> @@ -8,9 +8,14 @@
> #include <uapi/linux/sched.h>
> #include <uapi/linux/nsfs.h>
>
> +enum ns_common_flags_t {
> + /* Kernel-internal namespace. The LSM namespace hooks never
> see it. */
> + NS_COMMON_KERNEL = (1 << 0),
> +};
> +
> bool is_current_namespace(struct ns_common *ns);
> int __ns_common_init(struct ns_common *ns, u32 ns_type, const struct
> proc_ns_operations *ops,
> - int inum, u64 ns_id);
> + int inum, u64 ns_id, enum ns_common_flags_t
> flags);
> void __ns_common_free(struct ns_common *ns);
> struct ns_common *__must_check ns_owner(struct ns_common *ns);
>
> @@ -47,13 +52,14 @@ static __always_inline bool is_ns_init_id(const
> struct ns_common *ns)
>
> ns_common_type(__ns), \
>
> to_ns_operations(__ns), \
> (((__ns) == ns_init_ns(__ns)) ?
> ns_init_inum(__ns) : 0), \
> - (((__ns) == ns_init_ns(__ns)) ?
> ns_init_id(__ns) : 0))
> + (((__ns) == ns_init_ns(__ns)) ?
> ns_init_id(__ns) : 0), \
> + 0)
>
> -#define ns_common_init_inum(__ns, __inum) \
> - __ns_common_init(to_ns_common(__ns), \
> - ns_common_type(__ns), \
> - to_ns_operations(__ns), \
> - __inum, 0)
> +#define ns_common_init_inum(__ns, __inum, __flags) \
> + __ns_common_init(to_ns_common(__ns), \
> + ns_common_type(__ns), \
> + to_ns_operations(__ns), \
> + __inum, 0, __flags)
>
> #define ns_common_free(__ns) __ns_common_free(to_ns_common((__ns)))
>
> diff --git a/kernel/nscommon.c b/kernel/nscommon.c
> index 4438c6b663f91..20fc0d0e98212 100644
> --- a/kernel/nscommon.c
> +++ b/kernel/nscommon.c
> @@ -55,7 +55,7 @@ static void ns_debug(struct ns_common *ns, const
> struct proc_ns_operations *ops)
> #endif
>
> int __ns_common_init(struct ns_common *ns, u32 ns_type, const struct
> proc_ns_operations *ops,
> - int inum, u64 ns_id)
> + int inum, u64 ns_id, enum ns_common_flags_t
> flags)
> {
> int ret = 0;
>
> @@ -66,6 +66,7 @@ int __ns_common_init(struct ns_common *ns, u32
> ns_type, const struct proc_ns_ope
> #endif
> ns->ops = ops;
> ns->ns_type = ns_type;
> + ns->ns_flags = flags;
> ns_tree_node_init(&ns->ns_tree_node);
> ns_tree_node_init(&ns->ns_unified_node);
> ns_tree_node_init(&ns->ns_owner_node);
> @@ -85,7 +86,8 @@ int __ns_common_init(struct ns_common *ns, u32
> ns_type, const struct proc_ns_ope
> /* Allocate the id before the LSM hook so it can track the
> namespace by it. */
> __ns_tree_gen_id(ns, ns_id);
>
> - ret = security_namespace_init(ns);
> + if (!(flags & NS_COMMON_KERNEL))
> + ret = security_namespace_init(ns);
> if (ret) {
> if (!inum)
> proc_free_inum(ns->inum);
> @@ -106,7 +108,8 @@ int __ns_common_init(struct ns_common *ns, u32
> ns_type, const struct proc_ns_ope
>
> void __ns_common_free(struct ns_common *ns)
> {
> - security_namespace_free(ns);
> + if (!(ns->ns_flags & NS_COMMON_KERNEL))
> + security_namespace_free(ns);
>
> if (ns->inum > MNT_NS_INO_SPECIAL_MAX)
> proc_free_inum(ns->inum);
More information about the Linux-security-module-archive
mailing list