ns: don't call the namespace LSM hooks for kernel-internal namespaces
Subsystems (3)
Christian Brauner
maintainercover ns: don't call the namespace LSM hooks for kernel-internal namespaces
mount_subtree() allocates a private anonymous mount namespace so that automounts during the path lookup have somewhere to go. btrfs goes through it on every mount and NFS on every NFSv4 mount and whenever it follows a referral.
Since the namespace LSM hooks went in that namespace goes through security_namespace_init() like any other. Landlock enforces LANDLOCK_PERMISSION_NAMESPACE_USE in its namespace_init hook. So a sandbox that handles that permission without allowing CLONE_NEWNS can't mount btrfs or NFSv4 anymore. mount(2) fails with EPERM even though nobody asked for a mount namespace. Zhiqi reported that and sent a fix that skips namespace_init for this namespace (cf. [1]).
But namespace_free needs to be skipped as well. Otherwise a bpf program attached to namespace_free would see these namespaces go away without ever having seen them being created. Right now that can't happen but only because security_namespace_free() skips the hook if no LSM requested a namespace blob. So mark such namespaces as kernel-internal and keep them away from both hooks. The first patch turns the anon argument of alloc_mnt_ns() into flags so we don't end up with two booleans.
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.
I tested this on v7.4-rc3 with a btrfs loop mount and an NFSv4 mount in a Landlock domain that handles LANDLOCK_PERMISSION_NAMESPACE_USE but doesn't allow CLONE_NEWNS. Both fail with EPERM without this series and work with it. unshare(CLONE_NEWNS), clone(CLONE_NEWNS), open_tree(OPEN_TREE_CLONE), fsmount() and setns() are still denied and the Landlock namespace selftests pass.
[1]: https://lore.kernel.org/[email protected]
---
Christian Brauner (2):
mount: pass flags to alloc_mnt_ns()
ns: don't call the namespace LSM hooks for kernel-internal namespaces
fs/namespace.c | 31 +++++++++++++++++++++---------- include/linux/ns/ns_common_types.h | 2 ++ include/linux/ns_common.h | 20 +++++++++++++------- kernel/nscommon.c | 9 ++++++--- 4 files changed, 42 insertions(+), 20 deletions(-)
---
base-commit: 666539eecdf4db623f5e3c3e55cf9f2dfad24aa4
change-id: 20261120-work-ns-mount_subtree-d429ad72f8ac
Christian Brauner
maintainerPATCH 1/2 mount: pass flags to alloc_mnt_ns()
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.
--- fs/namespace.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-)
fs/namespace.c +15 -9
@@ -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, unsignedstruct 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 areturn 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 adec_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);
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH 1/2] mount: pass flags to alloc_mnt_ns()
Sashiko has reviewed this patch and found no issues.
Quoted 7 lines
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) <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH 1/2] mount: pass flags to alloc_mnt_ns()
In reply to Christian BraunerQuoted 7 lines · Christian Brauner
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) <[email protected]>
Quoted 110 lines
---
fs/namespace.c | 24 +++++++++++++++---------1 file changed, 15 insertions(+), 9 deletions(-)fs/namespace.c +0
-0@@ -2353,7 +2353,12 @@ void drop_collected_paths(const struct path *paths, const struct path *prealloc) kfree(paths); }
-static structmnt_namespace*alloc_mnt_ns(structuser_namespace*, bool);fs/namespace.c +6
-0+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. */
voiddissolve_on_fput(struct vfsmount *mnt)fs/namespace.c +0
-0@@ -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;fs/namespace.c +1
-1- ns =alloc_mnt_ns (user_ns, true); + ns =alloc_mnt_ns (user_ns,MNT_NS_ANON );if (
IS_ERR(ns))
return ns;fs/namespace.c +0
-0@@ -3230,7 +3235,7 @@ static struct mnt_namespace *create_new_namespace(struct path *path, if (foreign) copy_flags |= CL_SLAVE | CL_COPY_UNBINDABLE;fs/namespace.c +1
-1- new_ns =alloc_mnt_ns (user_ns, false); + new_ns =alloc_mnt_ns (user_ns,0 );if (
IS_ERR(new_ns))
returnERR_CAST(new_ns);fs/namespace.c +0
-0@@ -4369,7 +4374,8 @@ static void free_mnt_ns(struct mnt_namespace *ns) mnt_ns_tree_remove(ns); }
-static structmnt_namespace*alloc_mnt_ns(structuser_namespace*user_ns, bool anon)
+static structmnt_namespace*alloc_mnt_ns(structuser_namespace*user_ns,fs/namespace.c +1
-0+ enum mnt_ns_flags_t flags){
structmnt_namespace*new_ns;
struct ucounts *ucounts;fs/namespace.c +0
-0@@ -4385,7 +4391,7 @@ static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool a return ERR_PTR(-ENOMEM); }fs/namespace.c +1
-1-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);fs/namespace.c +1
-1@@ -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);fs/namespace.c +0
-0@@ -4422,7 +4428,7 @@ struct mnt_namespace *copy_mnt_ns(u64 flags, struct mnt_namespace *ns,
old =ns->root;fs/namespace.c +1
-1- new_ns =alloc_mnt_ns (user_ns, false); + new_ns =alloc_mnt_ns (user_ns,0 );if (
IS_ERR(new_ns))
returnnew_ns;fs/namespace.c +0
-0@@ -4509,7 +4515,7 @@ struct dentry *mount_subtree(struct vfsmount *m, const char *name) struct path path; int err;fs/namespace.c +1
-1- ns =alloc_mnt_ns (&init_user_ns, true); + ns =alloc_mnt_ns (&init_user_ns,MNT_NS_ANON );if (
IS_ERR(ns)) {
mntput(m);
returnERR_CAST(ns);fs/namespace.c +0
-0@@ -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));fs/namespace.c +1
-1- 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))
returnPTR_ERR(ns);
mnt =real_mount(new_path.mnt);
Christian Brauner
maintainerPATCH 2/2 ns: don't call the namespace LSM hooks for kernel-internal namespaces
In reply to Christian Braunermount_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.
--- 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(-)
fs/namespace.c +7 -2
@@ -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);
include/linux/ns/ns_common_types.h +2 -0
@@ -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;
include/linux/ns_common.h +13 -7
@@ -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)))
kernel/nscommon.c +6 -3
@@ -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_opevoid __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);
Signature · 1 line
2.53.0
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 14 lines
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 beforemount_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()
Quoted 7 lines
-> mount_subtree()[ ... ]
Fixes f675d2e95569 ("lsm: add LSM blob and hooks for namespaces")Reported-by Zhiqi Zhang <[email protected]>Signed-off-by Christian Brauner (Amutable) <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Quoted 39 lines · Christian Brauner
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 beforemount_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()callssecurity_namespace_init()for it like for
any other namespace. Since commit15dd86021b57a("landlock: Enforce namespace use restrictions") Landlock enforcesLANDLOCK_PERMISSION_NAMESPACE_USEin itsnamespace_inithook. So a task
in a domain that handles that permission without allowingCLONE_NEWNScan'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. Currentlysecurity_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 tonamespace_freewould see namespaces go away that it never saw being created.The anonymous mount namespaces created by
open_tree(OPEN_TREE_CLONE)andfsmount()are handed to userspace and keep going through the hooks.
Landlock covers them on purpose.
Quoted 5 lines
Fixes f675d2e95569 ("lsm: add LSM blob and hooks for namespaces")Reported-by Zhiqi Zhang <[email protected]>Closes:
https://lore.kernel.org/[email protected]Signed-off-by Christian Brauner (Amutable) <[email protected]>
Looks good to me.
Quoted 180 lines
---
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(-)fs/namespace.c +0
-0@@ -2355,6 +2355,7 @@ void drop_collected_paths(const struct path *paths, const struct path *prealloc)
enummnt_ns_flags_t{
MNT_NS_ANON= (1 << 0),fs/namespace.c +1
-0+ MNT_NS_KERNEL = (1 << 1),};
static structmnt_namespace*alloc_mnt_ns(structuser_namespace*,fs/namespace.c +0
-0@@ -4377,10 +4378,14 @@ static void free_mnt_ns(struct mnt_namespace *ns)static struct
mnt_namespace*alloc_mnt_ns(structuser_namespace*user_ns,
enummnt_ns_flags_tflags) {fs/namespace.c +1
-0+enum ns_common_flags_t ns_flags =0 ;struct
mnt_namespace*new_ns;
struct ucounts *ucounts;
int ret;fs/namespace.c +3
-0+if (flags &MNT_NS_KERNEL ) + ns_flags |=NS_COMMON_KERNEL ; +ucounts =
inc_mnt_namespaces(user_ns);
if (!ucounts)
returnERR_PTR(-ENOSPC);fs/namespace.c +0
-0@@ -4392,7 +4397,7 @@ static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, }
if (flags &MNT_NS_ANON)fs/namespace.c +1
-1- 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) {fs/namespace.c +0
-0@@ -4515,7 +4520,7 @@ struct dentry *mount_subtree(struct vfsmount *m, const char *name) struct path path; int err;fs/namespace.c +1
-1- 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);
returnERR_CAST(ns);include/linux/ns/ns_common_types.h +1
-0@@ -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)
include/linux/ns/ns_common_types.h +0
-0@@ -113,6 +114,7 @@ struct ns_common {
refcount_t__ns_ref; /* do not use directly */ }____cacheline_aligned_in_smp;
u32ns_type;include/linux/ns/ns_common_types.h +1
-0+u32 ns_flags;struct dentry *stashed;
const structproc_ns_operations*ops;
unsigned int inum;include/linux/ns_common.h +0
-0@@ -8,9 +8,14 @@ #include <uapi/linux/sched.h> #include <uapi/linux/nsfs.h>include/linux/ns_common.h +2
-0+enum ns_common_flags_t { +/* Kernel-internal namespace. The LSM namespace hooks never see it. */
include/linux/ns_common.h +3
-0+ NS_COMMON_KERNEL = (1 << 0), +}; +bool
is_current_namespace(struct ns_common *ns);
int __ns_common_init(structns_common*ns, u32ns_type, const structproc_ns_operations*ops,include/linux/ns_common.h +1
-1- int inum, u64 ns_id); + int inum, u64 ns_id, enum ns_common_flags_tflags);
void__ns_common_free(struct ns_common *ns);
structns_common*__must_checkns_owner(struct ns_common *ns);include/linux/ns_common.h +0
-0@@ -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), \include/linux/ns_common.h +0
-1- (((__ns) == ns_init_ns(__ns)) ?
ns_init_id(__ns): 0))include/linux/ns_common.h +1
-0+ (((__ns) == ns_init_ns(__ns)) ?
ns_init_id(__ns): 0), \include/linux/ns_common.h +1
-0+ 0)include/linux/ns_common.h +5
-5-#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)
#definens_common_free(__ns)__ns_common_free(to_ns_common((__ns)))kernel/nscommon.c +0
-0@@ -55,7 +55,7 @@ static void ns_debug(struct ns_common *ns, const struct proc_ns_operations *ops) #endif
int __ns_common_init(structns_common*ns, u32ns_type, const structproc_ns_operations*ops,kernel/nscommon.c +1
-1- int inum, u64 ns_id) + int inum, u64 ns_id, enum ns_common_flags_tflags)
{
int ret = 0;kernel/nscommon.c +0
-0@@ -66,6 +66,7 @@ int __ns_common_init(struct ns_common *ns, u32
ns_type, const structproc_ns_ope
#endif
ns->ops= ops;
ns->ns_type=ns_type;kernel/nscommon.c +1
-0+ 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);kernel/nscommon.c +0
-0@@ -85,7 +86,8 @@ int __ns_common_init(struct ns_common *ns, u32
ns_type, const structproc_ns_ope
/* Allocate the id before the LSM hook so it can track the namespace by it. */
__ns_tree_gen_id(ns, ns_id);kernel/nscommon.c +2
-1- ret =security_namespace_init (ns); +if (!(flags &NS_COMMON_KERNEL )) + ret =security_namespace_init (ns);if (ret) {
if (!inum)
proc_free_inum(ns->inum);kernel/nscommon.c +0
-0@@ -106,7 +108,8 @@ int __ns_common_init(struct ns_common *ns, u32
ns_type, const structproc_ns_ope
void__ns_common_free(struct ns_common *ns)
{kernel/nscommon.c +2
-1-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);
Quoted 26 lines · Christian Brauner
mount_subtree()allocates a private anonymous mount namespace so that automounts during the path lookup have somewhere to go. btrfs goes through it on every mount and NFS on every NFSv4 mount and whenever it follows a referral.Since the namespace LSM hooks went in that namespace goes through
security_namespace_init()like any other. Landlock enforcesLANDLOCK_PERMISSION_NAMESPACE_USEin itsnamespace_inithook. So a sandbox that handles that permission without allowingCLONE_NEWNScan't mount btrfs or NFSv4 anymore. mount(2) fails with EPERM even though nobody asked for a mount namespace. Zhiqi reported that and sent a fix that skipsnamespace_initfor this namespace (cf. [1]).But
namespace_freeneeds to be skipped as well. Otherwise a bpf program attached tonamespace_freewould see these namespaces go away without ever having seen them being created. Right now that can't happen but only becausesecurity_namespace_free()skips the hook if no LSM requested a namespace blob. So mark such namespaces as kernel-internal and keep them away from both hooks. The first patch turns the anon argument ofalloc_mnt_ns()into flags so we don't end up with two booleans.The anonymous mount namespaces created by
open_tree(OPEN_TREE_CLONE)andfsmount()are handed to userspace and keep going through the hooks. Landlock covers them on purpose.
Quoted 8 lines
I tested this on v7.4-rc3 with a btrfs loop mount and an NFSv4 mount in a Landlock domain that handles
LANDLOCK_PERMISSION_NAMESPACE_USEbut doesn't allowCLONE_NEWNS. Both fail with EPERM without this series and work with it.unshare(CLONE_NEWNS),clone(CLONE_NEWNS),open_tree(OPEN_TREE_CLONE),fsmount()andsetns()are still denied and the Landlock namespace selftests pass.
Thanks, both look good to me.
Quoted 16 lines
Signed-off-by Christian Brauner (Amutable) <[email protected]>---
Christian Brauner (2):
mount: pass flags toalloc_mnt_ns()
ns: don't call the namespace LSM hooks for kernel-internal namespacesfs/namespace.c | 31 +++++++++++++++++++++---------- include/linux/ns/ns_common_types.h | 2 ++ include/linux/ns_common.h | 20 +++++++++++++------- kernel/nscommon.c | 9 ++++++--- 4 files changed, 42 insertions(+), 20 deletions(-)---
base-commit:666539eecdf4db623f5e3c3e55cf9f2dfad24aa4
change-id: 20261120-work-ns-mount_subtree-d429ad72f8ac