* [PATCH 1/2] mount: pass flags to alloc_mnt_ns()
2026-11-20 12:29 [PATCH 0/2] ns: don't call the namespace LSM hooks for kernel-internal namespaces Christian Brauner
@ 2026-11-20 12:29 ` Christian Brauner
2026-11-20 12:32 ` sashiko-bot
2026-11-20 12:29 ` [PATCH 2/2] ns: don't call the namespace LSM hooks for kernel-internal namespaces Christian Brauner
1 sibling, 1 reply; 5+ messages in thread
From: Christian Brauner @ 2026-11-20 12:29 UTC (permalink / raw)
To: linux-fsdevel
Cc: linux-security-module, Alexander Viro, Jan Kara, bpf, Zhiqi Zhang,
Christian Brauner (Amutable)
Turn the anon argument of alloc_mnt_ns() into a flags argument. The
next patch needs to pass another property of the new mount namespace
and two booleans in a row would make the callers unreadable.
No functional changes.
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
fs/namespace.c | 24 +++++++++++++++---------
1 file changed, 15 insertions(+), 9 deletions(-)
diff --git a/fs/namespace.c b/fs/namespace.c
index aecd05ac01705..756f9011cbf1d 100644
--- a/fs/namespace.c
+++ b/fs/namespace.c
@@ -2353,7 +2353,12 @@ void drop_collected_paths(const struct path *paths, const struct path *prealloc)
kfree(paths);
}
-static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *, bool);
+enum mnt_ns_flags_t {
+ MNT_NS_ANON = (1 << 0),
+};
+
+static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *,
+ enum mnt_ns_flags_t);
/* Consumes the caller's reference to @mnt. */
void dissolve_on_fput(struct vfsmount *mnt)
@@ -3149,7 +3154,7 @@ static struct mnt_namespace *get_detached_copy(const struct path *path, unsigned
struct user_namespace *user_ns = mnt_ns->user_ns;
struct mount *mnt, *p;
- ns = alloc_mnt_ns(user_ns, true);
+ ns = alloc_mnt_ns(user_ns, MNT_NS_ANON);
if (IS_ERR(ns))
return ns;
@@ -3230,7 +3235,7 @@ static struct mnt_namespace *create_new_namespace(struct path *path,
if (foreign)
copy_flags |= CL_SLAVE | CL_COPY_UNBINDABLE;
- new_ns = alloc_mnt_ns(user_ns, false);
+ new_ns = alloc_mnt_ns(user_ns, 0);
if (IS_ERR(new_ns))
return ERR_CAST(new_ns);
@@ -4369,7 +4374,8 @@ static void free_mnt_ns(struct mnt_namespace *ns)
mnt_ns_tree_remove(ns);
}
-static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool anon)
+static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns,
+ enum mnt_ns_flags_t flags)
{
struct mnt_namespace *new_ns;
struct ucounts *ucounts;
@@ -4385,7 +4391,7 @@ static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool a
return ERR_PTR(-ENOMEM);
}
- if (anon)
+ if (flags & MNT_NS_ANON)
ret = ns_common_init_inum(new_ns, MNT_NS_ANON_INO);
else
ret = ns_common_init(new_ns);
@@ -4394,7 +4400,7 @@ static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool a
dec_mnt_namespaces(ucounts);
return ERR_PTR(ret);
}
- new_ns->is_anon = anon;
+ new_ns->is_anon = flags & MNT_NS_ANON;
refcount_set(&new_ns->passive, 1);
new_ns->mounts = RB_ROOT;
init_waitqueue_head(&new_ns->poll);
@@ -4422,7 +4428,7 @@ struct mnt_namespace *copy_mnt_ns(u64 flags, struct mnt_namespace *ns,
old = ns->root;
- new_ns = alloc_mnt_ns(user_ns, false);
+ new_ns = alloc_mnt_ns(user_ns, 0);
if (IS_ERR(new_ns))
return new_ns;
@@ -4509,7 +4515,7 @@ struct dentry *mount_subtree(struct vfsmount *m, const char *name)
struct path path;
int err;
- ns = alloc_mnt_ns(&init_user_ns, true);
+ ns = alloc_mnt_ns(&init_user_ns, MNT_NS_ANON);
if (IS_ERR(ns)) {
mntput(m);
return ERR_CAST(ns);
@@ -4699,7 +4705,7 @@ SYSCALL_DEFINE3(fsmount, int, fs_fd, unsigned int, flags,
return FD_ADD((flags & FSMOUNT_CLOEXEC) ? O_CLOEXEC : 0,
open_new_namespace(&new_path, MOUNT_COPY_NEW));
- ns = alloc_mnt_ns(current->nsproxy->mnt_ns->user_ns, true);
+ ns = alloc_mnt_ns(current->nsproxy->mnt_ns->user_ns, MNT_NS_ANON);
if (IS_ERR(ns))
return PTR_ERR(ns);
mnt = real_mount(new_path.mnt);
--
2.53.0
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH 2/2] ns: don't call the namespace LSM hooks for kernel-internal namespaces
2026-11-20 12:29 [PATCH 0/2] ns: don't call the namespace LSM hooks for kernel-internal namespaces Christian Brauner
2026-11-20 12:29 ` [PATCH 1/2] mount: pass flags to alloc_mnt_ns() Christian Brauner
@ 2026-11-20 12:29 ` Christian Brauner
2026-11-20 12:33 ` sashiko-bot
1 sibling, 1 reply; 5+ messages in thread
From: Christian Brauner @ 2026-11-20 12:29 UTC (permalink / raw)
To: linux-fsdevel
Cc: linux-security-module, Alexander Viro, Jan Kara, bpf, Zhiqi Zhang,
Christian Brauner (Amutable)
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@gmail.com>
Closes: https://lore.kernel.org/20261114141323.19029-1-zhiqizhangg@gmail.com
Signed-off-by: Christian Brauner (Amutable) <brauner@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/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);
--
2.53.0
^ permalink raw reply related [flat|nested] 5+ messages in thread