[PATCH 2/2] ns: don't call the namespace LSM hooks for kernel-internal namespaces

Paul Moore paul at paul-moore.com
Sat Nov 21 00:20:30 UTC 2026


On Fri, Nov 20, 2026 at 7:30 AM Christian Brauner <brauner at kernel.org> 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>
> ---
>  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/kernel/nscommon.c b/kernel/nscommon.c
> index 4438c6b663f91..20fc0d0e98212 100644
> --- a/kernel/nscommon.c
> +++ b/kernel/nscommon.c

...

> @@ -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);

We try not to make calls into LSM hooks conditional whenever possible,
preferring to leave that up to the individual LSMs.  In some cases it
isn't possible due the state used for the decision not being visible
in all of the cases where it is needed inside the LSM, but in this
case the flags/NS_COMMON_KERNEL state should already be present in the
ns_common variable passed to the LSM hook so we should be able to drop
the flags check and always call security_namespace_init(), letting the
LSM decide how to handle NS_COMMON_KERNEL namespaces.

The LSM could make a similar decision in the security_namespace_free()
hook below.

The update to Landlock's hook_namespace_init() should be as trivial as
the check here.

>         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);
>
> --
> 2.53.0

-- 
paul-moore.com



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