mirror of
https://github.com/torvalds/linux.git
synced 2026-11-20 13:03:48 +00:00
Currently the namespace id is allocated only after __ns_common_init()
has called the namespace_init LSM hook. Most namespace types allocate it
in ns_tree_add() right before the namespace is added to the namespace
trees. So the namespace_init hook always sees an id of zero.
The namespace id is the only identifier of a namespace that is never
recycled. A bpf LSM wants to use it to track a namespace from
namespace_init until namespace_free, e.g., by using it as the key into a
map that associates data with the namespace. The inode number doesn't
work for that as it is quickly recycled.
So allocate the id in __ns_common_init() before calling the
namespace_init hook. Every namespace then has its id by the time it is
added to the namespace trees. The initial namespaces still get their
fixed ids, either statically from NS_COMMON_INIT() or for init_net from
ns_common_init(). So ns_tree_add() doesn't need to allocate an id
anymore. Ids are still handed out in creation order so mnt_ns_loop()
keeps working. Mount and ipc namespaces already allocated their id right
after ns_common_init() anyway.
If creating the namespace fails after the id has been allocated the id
is simply never used.
Landlock logs the namespace id when it denies the use of a namespace.
For a denied namespace creation that used to be zero. Now it's the id
that was allocated for the new namespace. That namespace never becomes
visible to userspace but it's the same id that other LSMs see in
namespace_init. Update the Landlock documentation and selftests
accordingly. The selftests can't know the id of a namespace whose
creation was denied so they only check that it isn't zero.
While at it, drop ns_tree_gen_id() as it has no users left.
Reproducer, 100 user and uts namespaces:
bpftrace -e 'fentry:bpf_lsm_namespace_init {
@[args.ns->ns_id ? "set" : "zero"] = count(); }' &
for i in $(seq 100); do unshare -Uu true; done
before: @[zero]: 200
after: @[set]: 200
Fixes: f675d2e955 ("lsm: add LSM blob and hooks for namespaces")
Cc: [email protected] # mainline only
Link: https://patch.msgid.link/20261029-work-security-namespace_free-v2-4-65591dbba7b3@kernel.org
Reviewed-by: Mickaël Salaün <[email protected]> # Landlock changes
Signed-off-by: Christian Brauner (Amutable) <[email protected]>
336 lines
9.3 KiB
C
336 lines
9.3 KiB
C
// SPDX-License-Identifier: GPL-2.0-only
|
|
/* Copyright (c) 2025 Christian Brauner <[email protected]> */
|
|
|
|
#include <linux/ns_common.h>
|
|
#include <linux/nstree.h>
|
|
#include <linux/proc_ns.h>
|
|
#include <linux/security.h>
|
|
#include <linux/user_namespace.h>
|
|
#include <linux/vfsdebug.h>
|
|
|
|
#ifdef CONFIG_DEBUG_VFS
|
|
static void ns_debug(struct ns_common *ns, const struct proc_ns_operations *ops)
|
|
{
|
|
switch (ns->ns_type) {
|
|
#ifdef CONFIG_CGROUPS
|
|
case CLONE_NEWCGROUP:
|
|
VFS_WARN_ON_ONCE(ops != &cgroupns_operations);
|
|
break;
|
|
#endif
|
|
#ifdef CONFIG_IPC_NS
|
|
case CLONE_NEWIPC:
|
|
VFS_WARN_ON_ONCE(ops != &ipcns_operations);
|
|
break;
|
|
#endif
|
|
case CLONE_NEWNS:
|
|
VFS_WARN_ON_ONCE(ops != &mntns_operations);
|
|
break;
|
|
#ifdef CONFIG_NET_NS
|
|
case CLONE_NEWNET:
|
|
VFS_WARN_ON_ONCE(ops != &netns_operations);
|
|
break;
|
|
#endif
|
|
#ifdef CONFIG_PID_NS
|
|
case CLONE_NEWPID:
|
|
VFS_WARN_ON_ONCE(ops != &pidns_operations);
|
|
break;
|
|
#endif
|
|
#ifdef CONFIG_TIME_NS
|
|
case CLONE_NEWTIME:
|
|
VFS_WARN_ON_ONCE(ops != &timens_operations);
|
|
break;
|
|
#endif
|
|
#ifdef CONFIG_USER_NS
|
|
case CLONE_NEWUSER:
|
|
VFS_WARN_ON_ONCE(ops != &userns_operations);
|
|
break;
|
|
#endif
|
|
#ifdef CONFIG_UTS_NS
|
|
case CLONE_NEWUTS:
|
|
VFS_WARN_ON_ONCE(ops != &utsns_operations);
|
|
break;
|
|
#endif
|
|
}
|
|
}
|
|
#endif
|
|
|
|
int __ns_common_init(struct ns_common *ns, u32 ns_type, const struct proc_ns_operations *ops,
|
|
int inum, u64 ns_id)
|
|
{
|
|
int ret = 0;
|
|
|
|
refcount_set(&ns->__ns_ref, 1);
|
|
ns->stashed = NULL;
|
|
#ifdef CONFIG_SECURITY
|
|
ns->ns_security = NULL;
|
|
#endif
|
|
ns->ops = ops;
|
|
ns->ns_type = ns_type;
|
|
ns_tree_node_init(&ns->ns_tree_node);
|
|
ns_tree_node_init(&ns->ns_unified_node);
|
|
ns_tree_node_init(&ns->ns_owner_node);
|
|
ns_tree_root_init(&ns->ns_owner_root);
|
|
|
|
#ifdef CONFIG_DEBUG_VFS
|
|
ns_debug(ns, ops);
|
|
#endif
|
|
|
|
if (inum)
|
|
ns->inum = inum;
|
|
else
|
|
ret = proc_alloc_inum(&ns->inum);
|
|
if (ret)
|
|
return ret;
|
|
|
|
/* 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 (ret) {
|
|
if (!inum)
|
|
proc_free_inum(ns->inum);
|
|
return ret;
|
|
}
|
|
|
|
/*
|
|
* Tree ref starts at 0. It's incremented when namespace enters
|
|
* active use (installed in nsproxy) and decremented when all
|
|
* active uses are gone. Initial namespaces are always active.
|
|
*/
|
|
if (is_ns_init_inum(ns))
|
|
atomic_set(&ns->__ns_ref_active, 1);
|
|
else
|
|
atomic_set(&ns->__ns_ref_active, 0);
|
|
return 0;
|
|
}
|
|
|
|
void __ns_common_free(struct ns_common *ns)
|
|
{
|
|
security_namespace_free(ns);
|
|
|
|
if (ns->inum > MNT_NS_INO_SPECIAL_MAX)
|
|
proc_free_inum(ns->inum);
|
|
}
|
|
|
|
struct ns_common *__must_check ns_owner(struct ns_common *ns)
|
|
{
|
|
struct user_namespace *owner;
|
|
|
|
if (unlikely(!ns->ops))
|
|
return NULL;
|
|
VFS_WARN_ON_ONCE(!ns->ops->owner);
|
|
owner = ns->ops->owner(ns);
|
|
VFS_WARN_ON_ONCE(!owner && ns != to_ns_common(&init_user_ns));
|
|
if (!owner)
|
|
return NULL;
|
|
/* Skip init_user_ns as it's always active */
|
|
if (owner == &init_user_ns)
|
|
return NULL;
|
|
return to_ns_common(owner);
|
|
}
|
|
|
|
/*
|
|
* The active reference count works by having each namespace that gets
|
|
* created take a single active reference on its owning user namespace.
|
|
* That single reference is only released once the child namespace's
|
|
* active count itself goes down.
|
|
*
|
|
* A regular namespace tree might look as follow:
|
|
* Legend:
|
|
* + : adding active reference
|
|
* - : dropping active reference
|
|
* x : always active (initial namespace)
|
|
*
|
|
*
|
|
* net_ns pid_ns
|
|
* \ /
|
|
* + +
|
|
* user_ns1 (2)
|
|
* |
|
|
* ipc_ns | uts_ns
|
|
* \ | /
|
|
* + + +
|
|
* user_ns2 (3)
|
|
* |
|
|
* cgroup_ns | mnt_ns
|
|
* \ | /
|
|
* x x x
|
|
* init_user_ns (1)
|
|
*
|
|
* If both net_ns and pid_ns put their last active reference on
|
|
* themselves it will cascade to user_ns1 dropping its own active
|
|
* reference and dropping one active reference on user_ns2:
|
|
*
|
|
* net_ns pid_ns
|
|
* \ /
|
|
* - -
|
|
* user_ns1 (0)
|
|
* |
|
|
* ipc_ns | uts_ns
|
|
* \ | /
|
|
* + - +
|
|
* user_ns2 (2)
|
|
* |
|
|
* cgroup_ns | mnt_ns
|
|
* \ | /
|
|
* x x x
|
|
* init_user_ns (1)
|
|
*
|
|
* The iteration stops once we reach a namespace that still has active
|
|
* references.
|
|
*/
|
|
void __ns_ref_active_put(struct ns_common *ns)
|
|
{
|
|
/* Initial namespaces are always active. */
|
|
if (is_ns_init_id(ns))
|
|
return;
|
|
|
|
if (!atomic_dec_and_test(&ns->__ns_ref_active)) {
|
|
VFS_WARN_ON_ONCE(__ns_ref_active_read(ns) < 0);
|
|
return;
|
|
}
|
|
|
|
VFS_WARN_ON_ONCE(is_ns_init_id(ns));
|
|
VFS_WARN_ON_ONCE(!__ns_ref_read(ns));
|
|
|
|
for (;;) {
|
|
ns = ns_owner(ns);
|
|
if (!ns)
|
|
return;
|
|
VFS_WARN_ON_ONCE(is_ns_init_id(ns));
|
|
if (!atomic_dec_and_test(&ns->__ns_ref_active)) {
|
|
VFS_WARN_ON_ONCE(__ns_ref_active_read(ns) < 0);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
/*
|
|
* The active reference count works by having each namespace that gets
|
|
* created take a single active reference on its owning user namespace.
|
|
* That single reference is only released once the child namespace's
|
|
* active count itself goes down. This makes it possible to efficiently
|
|
* resurrect a namespace tree:
|
|
*
|
|
* A regular namespace tree might look as follow:
|
|
* Legend:
|
|
* + : adding active reference
|
|
* - : dropping active reference
|
|
* x : always active (initial namespace)
|
|
*
|
|
*
|
|
* net_ns pid_ns
|
|
* \ /
|
|
* + +
|
|
* user_ns1 (2)
|
|
* |
|
|
* ipc_ns | uts_ns
|
|
* \ | /
|
|
* + + +
|
|
* user_ns2 (3)
|
|
* |
|
|
* cgroup_ns | mnt_ns
|
|
* \ | /
|
|
* x x x
|
|
* init_user_ns (1)
|
|
*
|
|
* If both net_ns and pid_ns put their last active reference on
|
|
* themselves it will cascade to user_ns1 dropping its own active
|
|
* reference and dropping one active reference on user_ns2:
|
|
*
|
|
* net_ns pid_ns
|
|
* \ /
|
|
* - -
|
|
* user_ns1 (0)
|
|
* |
|
|
* ipc_ns | uts_ns
|
|
* \ | /
|
|
* + - +
|
|
* user_ns2 (2)
|
|
* |
|
|
* cgroup_ns | mnt_ns
|
|
* \ | /
|
|
* x x x
|
|
* init_user_ns (1)
|
|
*
|
|
* Assume the whole tree is dead but all namespaces are still active:
|
|
*
|
|
* net_ns pid_ns
|
|
* \ /
|
|
* - -
|
|
* user_ns1 (0)
|
|
* |
|
|
* ipc_ns | uts_ns
|
|
* \ | /
|
|
* - - -
|
|
* user_ns2 (0)
|
|
* |
|
|
* cgroup_ns | mnt_ns
|
|
* \ | /
|
|
* x x x
|
|
* init_user_ns (1)
|
|
*
|
|
* Now assume the net_ns gets resurrected (.e.g., via the SIOCGSKNS ioctl()):
|
|
*
|
|
* net_ns pid_ns
|
|
* \ /
|
|
* + -
|
|
* user_ns1 (0)
|
|
* |
|
|
* ipc_ns | uts_ns
|
|
* \ | /
|
|
* - + -
|
|
* user_ns2 (0)
|
|
* |
|
|
* cgroup_ns | mnt_ns
|
|
* \ | /
|
|
* x x x
|
|
* init_user_ns (1)
|
|
*
|
|
* If net_ns had a zero reference count and we bumped it we also need to
|
|
* take another reference on its owning user namespace. Similarly, if
|
|
* pid_ns had a zero reference count it also needs to take another
|
|
* reference on its owning user namespace. So both net_ns and pid_ns
|
|
* will each have their own reference on the owning user namespace.
|
|
*
|
|
* If the owning user namespace user_ns1 had a zero reference count then
|
|
* it also needs to take another reference on its owning user namespace
|
|
* and so on.
|
|
*/
|
|
void __ns_ref_active_get(struct ns_common *ns)
|
|
{
|
|
int prev;
|
|
|
|
/* Initial namespaces are always active. */
|
|
if (is_ns_init_id(ns))
|
|
return;
|
|
|
|
/* If we didn't resurrect the namespace we're done. */
|
|
prev = atomic_fetch_add(1, &ns->__ns_ref_active);
|
|
VFS_WARN_ON_ONCE(prev < 0);
|
|
if (likely(prev))
|
|
return;
|
|
|
|
/*
|
|
* We did resurrect it. Walk the ownership hierarchy upwards
|
|
* until we found an owning user namespace that is active.
|
|
*/
|
|
for (;;) {
|
|
ns = ns_owner(ns);
|
|
if (!ns)
|
|
return;
|
|
|
|
VFS_WARN_ON_ONCE(is_ns_init_id(ns));
|
|
prev = atomic_fetch_add(1, &ns->__ns_ref_active);
|
|
VFS_WARN_ON_ONCE(prev < 0);
|
|
if (likely(prev))
|
|
return;
|
|
}
|
|
}
|
|
|
|
bool may_see_all_namespaces(void)
|
|
{
|
|
return (task_active_pid_ns(current) == &init_pid_ns) &&
|
|
ns_capable_noaudit(init_pid_ns.user_ns, CAP_SYS_ADMIN);
|
|
}
|