mirror of
https://github.com/linux-msm/laptops-kernel.git
synced 2026-08-13 14:19:53 -07:00
Merge tag 'namespace-6.18-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs
Pull namespace updates from Christian Brauner: "This contains a larger set of changes around the generic namespace infrastructure of the kernel. Each specific namespace type (net, cgroup, mnt, ...) embedds a struct ns_common which carries the reference count of the namespace and so on. We open-coded and cargo-culted so many quirks for each namespace type that it just wasn't scalable anymore. So given there's a bunch of new changes coming in that area I've started cleaning all of this up. The core change is to make it possible to correctly initialize every namespace uniformly and derive the correct initialization settings from the type of the namespace such as namespace operations, namespace type and so on. This leaves the new ns_common_init() function with a single parameter which is the specific namespace type which derives the correct parameters statically. This also means the compiler will yell as soon as someone does something remotely fishy. The ns_common_init() addition also allows us to remove ns_alloc_inum() and drops any special-casing of the initial network namespace in the network namespace initialization code that Linus complained about. Another part is reworking the reference counting. The reference counting was open-coded and copy-pasted for each namespace type even though they all followed the same rules. This also removes all open accesses to the reference count and makes it private and only uses a very small set of dedicated helpers to manipulate them just like we do for e.g., files. In addition this generalizes the mount namespace iteration infrastructure introduced a few cycles ago. As reminder, the vfs makes it possible to iterate sequentially and bidirectionally through all mount namespaces on the system or all mount namespaces that the caller holds privilege over. This allow userspace to iterate over all mounts in all mount namespaces using the listmount() and statmount() system call. Each mount namespace has a unique identifier for the lifetime of the systems that is exposed to userspace. The network namespace also has a unique identifier working exactly the same way. This extends the concept to all other namespace types. The new nstree type makes it possible to lookup namespaces purely by their identifier and to walk the namespace list sequentially and bidirectionally for all namespace types, allowing userspace to iterate through all namespaces. Looking up namespaces in the namespace tree works completely locklessly. This also means we can move the mount namespace onto the generic infrastructure and remove a bunch of code and members from struct mnt_namespace itself. There's a bunch of stuff coming on top of this in the future but for now this uses the generic namespace tree to extend a concept introduced first for pidfs a few cycles ago. For a while now we have supported pidfs file handles for pidfds. This has proven to be very useful. This extends the concept to cover namespaces as well. It is possible to encode and decode namespace file handles using the common name_to_handle_at() and open_by_handle_at() apis. As with pidfs file handles, namespace file handles are exhaustive, meaning it is not required to actually hold a reference to nsfs in able to decode aka open_by_handle_at() a namespace file handle. Instead the FD_NSFS_ROOT constant can be passed which will let the kernel grab a reference to the root of nsfs internally and thus decode the file handle. Namespaces file descriptors can already be derived from pidfds which means they aren't subject to overmount protection bugs. IOW, it's irrelevant if the caller would not have access to an appropriate /proc/<pid>/ns/ directory as they could always just derive the namespace based on a pidfd already. It has the same advantage as pidfds. It's possible to reliably and for the lifetime of the system refer to a namespace without pinning any resources and to compare them trivially. Permission checking is kept simple. If the caller is located in the namespace the file handle refers to they are able to open it otherwise they must hold privilege over the owning namespace of the relevant namespace. The namespace file handle layout is exposed as uapi and has a stable and extensible format. For now it simply contains the namespace identifier, the namespace type, and the inode number. The stable format means that userspace may construct its own namespace file handles without going through name_to_handle_at() as they are already allowed for pidfs and cgroup file handles" * tag 'namespace-6.18-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs: (65 commits) ns: drop assert ns: move ns type into struct ns_common nstree: make struct ns_tree private ns: add ns_debug() ns: simplify ns_common_init() further cgroup: add missing ns_common include ns: use inode initializer for initial namespaces selftests/namespaces: verify initial namespace inode numbers ns: rename to __ns_ref nsfs: port to ns_ref_*() helpers net: port to ns_ref_*() helpers uts: port to ns_ref_*() helpers ipv4: use check_net() net: use check_net() net-sysfs: use check_net() user: port to ns_ref_*() helpers time: port to ns_ref_*() helpers pid: port to ns_ref_*() helpers ipc: port to ns_ref_*() helpers cgroup: port to ns_ref_*() helpers ...
This commit is contained in:
@@ -58,16 +58,14 @@ new_segment:
|
||||
int blk_get_meta_cap(struct block_device *bdev, unsigned int cmd,
|
||||
struct logical_block_metadata_cap __user *argp)
|
||||
{
|
||||
struct blk_integrity *bi = blk_get_integrity(bdev->bd_disk);
|
||||
struct blk_integrity *bi;
|
||||
struct logical_block_metadata_cap meta_cap = {};
|
||||
size_t usize = _IOC_SIZE(cmd);
|
||||
|
||||
if (_IOC_DIR(cmd) != _IOC_DIR(FS_IOC_GETLBMD_CAP) ||
|
||||
_IOC_TYPE(cmd) != _IOC_TYPE(FS_IOC_GETLBMD_CAP) ||
|
||||
_IOC_NR(cmd) != _IOC_NR(FS_IOC_GETLBMD_CAP) ||
|
||||
_IOC_SIZE(cmd) < LBMD_SIZE_VER0)
|
||||
if (!extensible_ioctl_valid(cmd, FS_IOC_GETLBMD_CAP, LBMD_SIZE_VER0))
|
||||
return -ENOIOCTLCMD;
|
||||
|
||||
bi = blk_get_integrity(bdev->bd_disk);
|
||||
if (!bi)
|
||||
goto out;
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include <linux/personality.h>
|
||||
#include <linux/uaccess.h>
|
||||
#include <linux/compat.h>
|
||||
#include <linux/nsfs.h>
|
||||
#include "internal.h"
|
||||
#include "mount.h"
|
||||
|
||||
@@ -189,6 +190,11 @@ static int get_path_anchor(int fd, struct path *root)
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (fd == FD_NSFS_ROOT) {
|
||||
nsfs_get_root(root);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return -EBADF;
|
||||
}
|
||||
|
||||
|
||||
@@ -355,3 +355,4 @@ int anon_inode_getattr(struct mnt_idmap *idmap, const struct path *path,
|
||||
int anon_inode_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
|
||||
struct iattr *attr);
|
||||
void pidfs_get_root(struct path *path);
|
||||
void nsfs_get_root(struct path *path);
|
||||
|
||||
+3
-9
@@ -17,11 +17,7 @@ struct mnt_namespace {
|
||||
};
|
||||
struct user_namespace *user_ns;
|
||||
struct ucounts *ucounts;
|
||||
u64 seq; /* Sequence number to prevent loops */
|
||||
union {
|
||||
wait_queue_head_t poll;
|
||||
struct rcu_head mnt_ns_rcu;
|
||||
};
|
||||
wait_queue_head_t poll;
|
||||
u64 seq_origin; /* Sequence number of origin mount namespace */
|
||||
u64 event;
|
||||
#ifdef CONFIG_FSNOTIFY
|
||||
@@ -30,8 +26,6 @@ struct mnt_namespace {
|
||||
#endif
|
||||
unsigned int nr_mounts; /* # of mounts in the namespace */
|
||||
unsigned int pending_mounts;
|
||||
struct rb_node mnt_ns_tree_node; /* node in the mnt_ns_tree */
|
||||
struct list_head mnt_ns_list; /* entry in the sequential list of mounts namespace */
|
||||
refcount_t passive; /* number references not pinning @mounts */
|
||||
} __randomize_layout;
|
||||
|
||||
@@ -149,7 +143,7 @@ static inline void detach_mounts(struct dentry *dentry)
|
||||
|
||||
static inline void get_mnt_ns(struct mnt_namespace *ns)
|
||||
{
|
||||
refcount_inc(&ns->ns.count);
|
||||
ns_ref_inc(ns);
|
||||
}
|
||||
|
||||
extern seqlock_t mount_lock;
|
||||
@@ -173,7 +167,7 @@ static inline bool is_local_mountpoint(const struct dentry *dentry)
|
||||
|
||||
static inline bool is_anon_ns(struct mnt_namespace *ns)
|
||||
{
|
||||
return ns->seq == 0;
|
||||
return ns->ns.ns_id == 0;
|
||||
}
|
||||
|
||||
static inline bool anon_ns_root(const struct mount *m)
|
||||
|
||||
+60
-136
@@ -33,6 +33,7 @@
|
||||
#include <linux/shmem_fs.h>
|
||||
#include <linux/mnt_idmapping.h>
|
||||
#include <linux/pidfs.h>
|
||||
#include <linux/nstree.h>
|
||||
|
||||
#include "pnode.h"
|
||||
#include "internal.h"
|
||||
@@ -89,13 +90,10 @@ static DECLARE_RWSEM(namespace_sem);
|
||||
static HLIST_HEAD(unmounted); /* protected by namespace_sem */
|
||||
static LIST_HEAD(ex_mountpoints); /* protected by namespace_sem */
|
||||
static struct mnt_namespace *emptied_ns; /* protected by namespace_sem */
|
||||
static DEFINE_SEQLOCK(mnt_ns_tree_lock);
|
||||
|
||||
#ifdef CONFIG_FSNOTIFY
|
||||
LIST_HEAD(notify_list); /* protected by namespace_sem */
|
||||
#endif
|
||||
static struct rb_root mnt_ns_tree = RB_ROOT; /* protected by mnt_ns_tree_lock */
|
||||
static LIST_HEAD(mnt_ns_list); /* protected by mnt_ns_tree_lock */
|
||||
|
||||
enum mount_kattr_flags_t {
|
||||
MOUNT_KATTR_RECURSE = (1 << 0),
|
||||
@@ -128,53 +126,12 @@ __cacheline_aligned_in_smp DEFINE_SEQLOCK(mount_lock);
|
||||
|
||||
static inline struct mnt_namespace *node_to_mnt_ns(const struct rb_node *node)
|
||||
{
|
||||
struct ns_common *ns;
|
||||
|
||||
if (!node)
|
||||
return NULL;
|
||||
return rb_entry(node, struct mnt_namespace, mnt_ns_tree_node);
|
||||
}
|
||||
|
||||
static int mnt_ns_cmp(struct rb_node *a, const struct rb_node *b)
|
||||
{
|
||||
struct mnt_namespace *ns_a = node_to_mnt_ns(a);
|
||||
struct mnt_namespace *ns_b = node_to_mnt_ns(b);
|
||||
u64 seq_a = ns_a->seq;
|
||||
u64 seq_b = ns_b->seq;
|
||||
|
||||
if (seq_a < seq_b)
|
||||
return -1;
|
||||
if (seq_a > seq_b)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline void mnt_ns_tree_write_lock(void)
|
||||
{
|
||||
write_seqlock(&mnt_ns_tree_lock);
|
||||
}
|
||||
|
||||
static inline void mnt_ns_tree_write_unlock(void)
|
||||
{
|
||||
write_sequnlock(&mnt_ns_tree_lock);
|
||||
}
|
||||
|
||||
static void mnt_ns_tree_add(struct mnt_namespace *ns)
|
||||
{
|
||||
struct rb_node *node, *prev;
|
||||
|
||||
mnt_ns_tree_write_lock();
|
||||
node = rb_find_add_rcu(&ns->mnt_ns_tree_node, &mnt_ns_tree, mnt_ns_cmp);
|
||||
/*
|
||||
* If there's no previous entry simply add it after the
|
||||
* head and if there is add it after the previous entry.
|
||||
*/
|
||||
prev = rb_prev(&ns->mnt_ns_tree_node);
|
||||
if (!prev)
|
||||
list_add_rcu(&ns->mnt_ns_list, &mnt_ns_list);
|
||||
else
|
||||
list_add_rcu(&ns->mnt_ns_list, &node_to_mnt_ns(prev)->mnt_ns_list);
|
||||
mnt_ns_tree_write_unlock();
|
||||
|
||||
WARN_ON_ONCE(node);
|
||||
ns = rb_entry(node, struct ns_common, ns_tree_node);
|
||||
return container_of(ns, struct mnt_namespace, ns);
|
||||
}
|
||||
|
||||
static void mnt_ns_release(struct mnt_namespace *ns)
|
||||
@@ -190,32 +147,16 @@ DEFINE_FREE(mnt_ns_release, struct mnt_namespace *, if (_T) mnt_ns_release(_T))
|
||||
|
||||
static void mnt_ns_release_rcu(struct rcu_head *rcu)
|
||||
{
|
||||
mnt_ns_release(container_of(rcu, struct mnt_namespace, mnt_ns_rcu));
|
||||
mnt_ns_release(container_of(rcu, struct mnt_namespace, ns.ns_rcu));
|
||||
}
|
||||
|
||||
static void mnt_ns_tree_remove(struct mnt_namespace *ns)
|
||||
{
|
||||
/* remove from global mount namespace list */
|
||||
if (!is_anon_ns(ns)) {
|
||||
mnt_ns_tree_write_lock();
|
||||
rb_erase(&ns->mnt_ns_tree_node, &mnt_ns_tree);
|
||||
list_bidir_del_rcu(&ns->mnt_ns_list);
|
||||
mnt_ns_tree_write_unlock();
|
||||
}
|
||||
if (ns_tree_active(ns))
|
||||
ns_tree_remove(ns);
|
||||
|
||||
call_rcu(&ns->mnt_ns_rcu, mnt_ns_release_rcu);
|
||||
}
|
||||
|
||||
static int mnt_ns_find(const void *key, const struct rb_node *node)
|
||||
{
|
||||
const u64 mnt_ns_id = *(u64 *)key;
|
||||
const struct mnt_namespace *ns = node_to_mnt_ns(node);
|
||||
|
||||
if (mnt_ns_id < ns->seq)
|
||||
return -1;
|
||||
if (mnt_ns_id > ns->seq)
|
||||
return 1;
|
||||
return 0;
|
||||
call_rcu(&ns->ns.ns_rcu, mnt_ns_release_rcu);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -234,28 +175,21 @@ static int mnt_ns_find(const void *key, const struct rb_node *node)
|
||||
*/
|
||||
static struct mnt_namespace *lookup_mnt_ns(u64 mnt_ns_id)
|
||||
{
|
||||
struct mnt_namespace *ns;
|
||||
struct rb_node *node;
|
||||
unsigned int seq;
|
||||
struct mnt_namespace *mnt_ns;
|
||||
struct ns_common *ns;
|
||||
|
||||
guard(rcu)();
|
||||
do {
|
||||
seq = read_seqbegin(&mnt_ns_tree_lock);
|
||||
node = rb_find_rcu(&mnt_ns_id, &mnt_ns_tree, mnt_ns_find);
|
||||
if (node)
|
||||
break;
|
||||
} while (read_seqretry(&mnt_ns_tree_lock, seq));
|
||||
|
||||
if (!node)
|
||||
ns = ns_tree_lookup_rcu(mnt_ns_id, CLONE_NEWNS);
|
||||
if (!ns)
|
||||
return NULL;
|
||||
|
||||
/*
|
||||
* The last reference count is put with RCU delay so we can
|
||||
* unconditonally acquire a reference here.
|
||||
*/
|
||||
ns = node_to_mnt_ns(node);
|
||||
refcount_inc(&ns->passive);
|
||||
return ns;
|
||||
mnt_ns = container_of(ns, struct mnt_namespace, ns);
|
||||
refcount_inc(&mnt_ns->passive);
|
||||
return mnt_ns;
|
||||
}
|
||||
|
||||
static inline void lock_mount_hash(void)
|
||||
@@ -1026,7 +960,7 @@ static inline bool check_anonymous_mnt(struct mount *mnt)
|
||||
return false;
|
||||
|
||||
seq = mnt->mnt_ns->seq_origin;
|
||||
return !seq || (seq == current->nsproxy->mnt_ns->seq);
|
||||
return !seq || (seq == current->nsproxy->mnt_ns->ns.ns_id);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -2161,19 +2095,16 @@ struct ns_common *from_mnt_ns(struct mnt_namespace *mnt)
|
||||
|
||||
struct mnt_namespace *get_sequential_mnt_ns(struct mnt_namespace *mntns, bool previous)
|
||||
{
|
||||
struct ns_common *ns;
|
||||
|
||||
guard(rcu)();
|
||||
|
||||
for (;;) {
|
||||
struct list_head *list;
|
||||
ns = ns_tree_adjoined_rcu(mntns, previous);
|
||||
if (IS_ERR(ns))
|
||||
return ERR_CAST(ns);
|
||||
|
||||
if (previous)
|
||||
list = rcu_dereference(list_bidir_prev_rcu(&mntns->mnt_ns_list));
|
||||
else
|
||||
list = rcu_dereference(list_next_rcu(&mntns->mnt_ns_list));
|
||||
if (list_is_head(list, &mnt_ns_list))
|
||||
return ERR_PTR(-ENOENT);
|
||||
|
||||
mntns = list_entry_rcu(list, struct mnt_namespace, mnt_ns_list);
|
||||
mntns = to_mnt_ns(ns);
|
||||
|
||||
/*
|
||||
* The last passive reference count is put with RCU
|
||||
@@ -2188,7 +2119,7 @@ struct mnt_namespace *get_sequential_mnt_ns(struct mnt_namespace *mntns, bool pr
|
||||
* the mount namespace and it might already be on its
|
||||
* deathbed.
|
||||
*/
|
||||
if (!refcount_inc_not_zero(&mntns->ns.count))
|
||||
if (!ns_ref_get(mntns))
|
||||
continue;
|
||||
|
||||
return mntns;
|
||||
@@ -2213,7 +2144,7 @@ static bool mnt_ns_loop(struct dentry *dentry)
|
||||
if (!mnt_ns)
|
||||
return false;
|
||||
|
||||
return current->nsproxy->mnt_ns->seq >= mnt_ns->seq;
|
||||
return current->nsproxy->mnt_ns->ns.ns_id >= mnt_ns->ns.ns_id;
|
||||
}
|
||||
|
||||
struct mount *copy_tree(struct mount *src_root, struct dentry *dentry,
|
||||
@@ -3089,7 +3020,7 @@ static struct file *open_detached_copy(struct path *path, bool recursive)
|
||||
if (is_anon_ns(src_mnt_ns))
|
||||
ns->seq_origin = src_mnt_ns->seq_origin;
|
||||
else
|
||||
ns->seq_origin = src_mnt_ns->seq;
|
||||
ns->seq_origin = src_mnt_ns->ns.ns_id;
|
||||
}
|
||||
|
||||
mnt = __do_loopback(path, recursive);
|
||||
@@ -4162,20 +4093,11 @@ static void dec_mnt_namespaces(struct ucounts *ucounts)
|
||||
static void free_mnt_ns(struct mnt_namespace *ns)
|
||||
{
|
||||
if (!is_anon_ns(ns))
|
||||
ns_free_inum(&ns->ns);
|
||||
ns_common_free(ns);
|
||||
dec_mnt_namespaces(ns->ucounts);
|
||||
mnt_ns_tree_remove(ns);
|
||||
}
|
||||
|
||||
/*
|
||||
* Assign a sequence number so we can detect when we attempt to bind
|
||||
* mount a reference to an older mount namespace into the current
|
||||
* mount namespace, preventing reference counting loops. A 64bit
|
||||
* number incrementing at 10Ghz will take 12,427 years to wrap which
|
||||
* is effectively never, so we can ignore the possibility.
|
||||
*/
|
||||
static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1);
|
||||
|
||||
static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool anon)
|
||||
{
|
||||
struct mnt_namespace *new_ns;
|
||||
@@ -4191,22 +4113,20 @@ static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool a
|
||||
dec_mnt_namespaces(ucounts);
|
||||
return ERR_PTR(-ENOMEM);
|
||||
}
|
||||
if (!anon) {
|
||||
ret = ns_alloc_inum(&new_ns->ns);
|
||||
if (ret) {
|
||||
kfree(new_ns);
|
||||
dec_mnt_namespaces(ucounts);
|
||||
return ERR_PTR(ret);
|
||||
}
|
||||
|
||||
if (anon)
|
||||
ret = ns_common_init_inum(new_ns, MNT_NS_ANON_INO);
|
||||
else
|
||||
ret = ns_common_init(new_ns);
|
||||
if (ret) {
|
||||
kfree(new_ns);
|
||||
dec_mnt_namespaces(ucounts);
|
||||
return ERR_PTR(ret);
|
||||
}
|
||||
new_ns->ns.ops = &mntns_operations;
|
||||
if (!anon)
|
||||
new_ns->seq = atomic64_inc_return(&mnt_ns_seq);
|
||||
refcount_set(&new_ns->ns.count, 1);
|
||||
ns_tree_gen_id(&new_ns->ns);
|
||||
refcount_set(&new_ns->passive, 1);
|
||||
new_ns->mounts = RB_ROOT;
|
||||
INIT_LIST_HEAD(&new_ns->mnt_ns_list);
|
||||
RB_CLEAR_NODE(&new_ns->mnt_ns_tree_node);
|
||||
init_waitqueue_head(&new_ns->poll);
|
||||
new_ns->user_ns = get_user_ns(user_ns);
|
||||
new_ns->ucounts = ucounts;
|
||||
@@ -4245,7 +4165,7 @@ struct mnt_namespace *copy_mnt_ns(u64 flags, struct mnt_namespace *ns,
|
||||
new = copy_tree(old, old->mnt.mnt_root, copy_flags);
|
||||
if (IS_ERR(new)) {
|
||||
namespace_unlock();
|
||||
ns_free_inum(&new_ns->ns);
|
||||
ns_common_free(ns);
|
||||
dec_mnt_namespaces(new_ns->ucounts);
|
||||
mnt_ns_release(new_ns);
|
||||
return ERR_CAST(new);
|
||||
@@ -4292,7 +4212,7 @@ struct mnt_namespace *copy_mnt_ns(u64 flags, struct mnt_namespace *ns,
|
||||
if (pwdmnt)
|
||||
mntput(pwdmnt);
|
||||
|
||||
mnt_ns_tree_add(new_ns);
|
||||
ns_tree_add_raw(new_ns);
|
||||
return new_ns;
|
||||
}
|
||||
|
||||
@@ -5018,7 +4938,7 @@ static int build_mount_idmapped(const struct mount_attr *attr, size_t usize,
|
||||
return -EINVAL;
|
||||
|
||||
ns = get_proc_ns(file_inode(fd_file(f)));
|
||||
if (ns->ops->type != CLONE_NEWUSER)
|
||||
if (ns->ns_type != CLONE_NEWUSER)
|
||||
return -EINVAL;
|
||||
|
||||
/*
|
||||
@@ -5411,7 +5331,7 @@ static int statmount_sb_source(struct kstatmount *s, struct seq_file *seq)
|
||||
static void statmount_mnt_ns_id(struct kstatmount *s, struct mnt_namespace *ns)
|
||||
{
|
||||
s->sm.mask |= STATMOUNT_MNT_NS_ID;
|
||||
s->sm.mnt_ns_id = ns->seq;
|
||||
s->sm.mnt_ns_id = ns->ns.ns_id;
|
||||
}
|
||||
|
||||
static int statmount_mnt_opts(struct kstatmount *s, struct seq_file *seq)
|
||||
@@ -5918,7 +5838,7 @@ static struct mnt_namespace *grab_requested_mnt_ns(const struct mnt_id_req *kreq
|
||||
return ERR_PTR(-EINVAL);
|
||||
|
||||
ns = get_proc_ns(file_inode(fd_file(f)));
|
||||
if (ns->ops->type != CLONE_NEWNS)
|
||||
if (ns->ns_type != CLONE_NEWNS)
|
||||
return ERR_PTR(-EINVAL);
|
||||
|
||||
mnt_ns = to_mnt_ns(ns);
|
||||
@@ -6131,28 +6051,33 @@ SYSCALL_DEFINE4(listmount, const struct mnt_id_req __user *, req,
|
||||
return ret;
|
||||
}
|
||||
|
||||
struct mnt_namespace init_mnt_ns = {
|
||||
.ns.inum = ns_init_inum(&init_mnt_ns),
|
||||
.ns.ops = &mntns_operations,
|
||||
.user_ns = &init_user_ns,
|
||||
.ns.__ns_ref = REFCOUNT_INIT(1),
|
||||
.ns.ns_type = ns_common_type(&init_mnt_ns),
|
||||
.passive = REFCOUNT_INIT(1),
|
||||
.mounts = RB_ROOT,
|
||||
.poll = __WAIT_QUEUE_HEAD_INITIALIZER(init_mnt_ns.poll),
|
||||
};
|
||||
|
||||
static void __init init_mount_tree(void)
|
||||
{
|
||||
struct vfsmount *mnt;
|
||||
struct mount *m;
|
||||
struct mnt_namespace *ns;
|
||||
struct path root;
|
||||
|
||||
mnt = vfs_kern_mount(&rootfs_fs_type, 0, "rootfs", initramfs_options);
|
||||
if (IS_ERR(mnt))
|
||||
panic("Can't create rootfs");
|
||||
|
||||
ns = alloc_mnt_ns(&init_user_ns, true);
|
||||
if (IS_ERR(ns))
|
||||
panic("Can't allocate initial namespace");
|
||||
ns->seq = atomic64_inc_return(&mnt_ns_seq);
|
||||
ns->ns.inum = PROC_MNT_INIT_INO;
|
||||
m = real_mount(mnt);
|
||||
ns->root = m;
|
||||
ns->nr_mounts = 1;
|
||||
mnt_add_to_ns(ns, m);
|
||||
init_task.nsproxy->mnt_ns = ns;
|
||||
get_mnt_ns(ns);
|
||||
init_mnt_ns.root = m;
|
||||
init_mnt_ns.nr_mounts = 1;
|
||||
mnt_add_to_ns(&init_mnt_ns, m);
|
||||
init_task.nsproxy->mnt_ns = &init_mnt_ns;
|
||||
get_mnt_ns(&init_mnt_ns);
|
||||
|
||||
root.mnt = mnt;
|
||||
root.dentry = mnt->mnt_root;
|
||||
@@ -6160,7 +6085,7 @@ static void __init init_mount_tree(void)
|
||||
set_fs_pwd(current->fs, &root);
|
||||
set_fs_root(current->fs, &root);
|
||||
|
||||
mnt_ns_tree_add(ns);
|
||||
ns_tree_add(&init_mnt_ns);
|
||||
}
|
||||
|
||||
void __init mnt_init(void)
|
||||
@@ -6200,7 +6125,7 @@ void __init mnt_init(void)
|
||||
|
||||
void put_mnt_ns(struct mnt_namespace *ns)
|
||||
{
|
||||
if (!refcount_dec_and_test(&ns->ns.count))
|
||||
if (!ns_ref_put(ns))
|
||||
return;
|
||||
namespace_lock();
|
||||
emptied_ns = ns;
|
||||
@@ -6449,7 +6374,6 @@ static struct user_namespace *mntns_owner(struct ns_common *ns)
|
||||
|
||||
const struct proc_ns_operations mntns_operations = {
|
||||
.name = "mnt",
|
||||
.type = CLONE_NEWNS,
|
||||
.get = mntns_get,
|
||||
.put = mntns_put,
|
||||
.install = mntns_install,
|
||||
|
||||
@@ -13,12 +13,26 @@
|
||||
#include <linux/nsfs.h>
|
||||
#include <linux/uaccess.h>
|
||||
#include <linux/mnt_namespace.h>
|
||||
#include <linux/ipc_namespace.h>
|
||||
#include <linux/time_namespace.h>
|
||||
#include <linux/utsname.h>
|
||||
#include <linux/exportfs.h>
|
||||
#include <linux/nstree.h>
|
||||
#include <net/net_namespace.h>
|
||||
|
||||
#include "mount.h"
|
||||
#include "internal.h"
|
||||
|
||||
static struct vfsmount *nsfs_mnt;
|
||||
|
||||
static struct path nsfs_root_path = {};
|
||||
|
||||
void nsfs_get_root(struct path *path)
|
||||
{
|
||||
*path = nsfs_root_path;
|
||||
path_get(path);
|
||||
}
|
||||
|
||||
static long ns_ioctl(struct file *filp, unsigned int ioctl,
|
||||
unsigned long arg);
|
||||
static const struct file_operations ns_file_operations = {
|
||||
@@ -139,7 +153,7 @@ static int copy_ns_info_to_user(const struct mnt_namespace *mnt_ns,
|
||||
* the size value will be set to the size the kernel knows about.
|
||||
*/
|
||||
kinfo->size = min(usize, sizeof(*kinfo));
|
||||
kinfo->mnt_ns_id = mnt_ns->seq;
|
||||
kinfo->mnt_ns_id = mnt_ns->ns.ns_id;
|
||||
kinfo->nr_mounts = READ_ONCE(mnt_ns->nr_mounts);
|
||||
/* Subtract the root mount of the mount namespace. */
|
||||
if (kinfo->nr_mounts)
|
||||
@@ -163,15 +177,18 @@ static bool nsfs_ioctl_valid(unsigned int cmd)
|
||||
case NS_GET_TGID_FROM_PIDNS:
|
||||
case NS_GET_PID_IN_PIDNS:
|
||||
case NS_GET_TGID_IN_PIDNS:
|
||||
return (_IOC_TYPE(cmd) == _IOC_TYPE(cmd));
|
||||
case NS_GET_ID:
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Extensible ioctls require some extra handling. */
|
||||
switch (_IOC_NR(cmd)) {
|
||||
case _IOC_NR(NS_MNT_GET_INFO):
|
||||
return extensible_ioctl_valid(cmd, NS_MNT_GET_INFO, MNT_NS_INFO_SIZE_VER0);
|
||||
case _IOC_NR(NS_MNT_GET_NEXT):
|
||||
return extensible_ioctl_valid(cmd, NS_MNT_GET_NEXT, MNT_NS_INFO_SIZE_VER0);
|
||||
case _IOC_NR(NS_MNT_GET_PREV):
|
||||
return (_IOC_TYPE(cmd) == _IOC_TYPE(cmd));
|
||||
return extensible_ioctl_valid(cmd, NS_MNT_GET_PREV, MNT_NS_INFO_SIZE_VER0);
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -202,26 +219,14 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl,
|
||||
return -EINVAL;
|
||||
return open_related_ns(ns, ns->ops->get_parent);
|
||||
case NS_GET_NSTYPE:
|
||||
return ns->ops->type;
|
||||
return ns->ns_type;
|
||||
case NS_GET_OWNER_UID:
|
||||
if (ns->ops->type != CLONE_NEWUSER)
|
||||
if (ns->ns_type != CLONE_NEWUSER)
|
||||
return -EINVAL;
|
||||
user_ns = container_of(ns, struct user_namespace, ns);
|
||||
argp = (uid_t __user *) arg;
|
||||
uid = from_kuid_munged(current_user_ns(), user_ns->owner);
|
||||
return put_user(uid, argp);
|
||||
case NS_GET_MNTNS_ID: {
|
||||
__u64 __user *idp;
|
||||
__u64 id;
|
||||
|
||||
if (ns->ops->type != CLONE_NEWNS)
|
||||
return -EINVAL;
|
||||
|
||||
mnt_ns = container_of(ns, struct mnt_namespace, ns);
|
||||
idp = (__u64 __user *)arg;
|
||||
id = mnt_ns->seq;
|
||||
return put_user(id, idp);
|
||||
}
|
||||
case NS_GET_PID_FROM_PIDNS:
|
||||
fallthrough;
|
||||
case NS_GET_TGID_FROM_PIDNS:
|
||||
@@ -229,7 +234,7 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl,
|
||||
case NS_GET_PID_IN_PIDNS:
|
||||
fallthrough;
|
||||
case NS_GET_TGID_IN_PIDNS: {
|
||||
if (ns->ops->type != CLONE_NEWPID)
|
||||
if (ns->ns_type != CLONE_NEWPID)
|
||||
return -EINVAL;
|
||||
|
||||
ret = -ESRCH;
|
||||
@@ -267,6 +272,18 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl,
|
||||
ret = -ESRCH;
|
||||
return ret;
|
||||
}
|
||||
case NS_GET_MNTNS_ID:
|
||||
if (ns->ns_type != CLONE_NEWNS)
|
||||
return -EINVAL;
|
||||
fallthrough;
|
||||
case NS_GET_ID: {
|
||||
__u64 __user *idp;
|
||||
__u64 id;
|
||||
|
||||
idp = (__u64 __user *)arg;
|
||||
id = ns->ns_id;
|
||||
return put_user(id, idp);
|
||||
}
|
||||
}
|
||||
|
||||
/* extensible ioctls */
|
||||
@@ -276,7 +293,7 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl,
|
||||
struct mnt_ns_info __user *uinfo = (struct mnt_ns_info __user *)arg;
|
||||
size_t usize = _IOC_SIZE(ioctl);
|
||||
|
||||
if (ns->ops->type != CLONE_NEWNS)
|
||||
if (ns->ns_type != CLONE_NEWNS)
|
||||
return -EINVAL;
|
||||
|
||||
if (!uinfo)
|
||||
@@ -297,7 +314,7 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl,
|
||||
struct file *f __free(fput) = NULL;
|
||||
size_t usize = _IOC_SIZE(ioctl);
|
||||
|
||||
if (ns->ops->type != CLONE_NEWNS)
|
||||
if (ns->ns_type != CLONE_NEWNS)
|
||||
return -EINVAL;
|
||||
|
||||
if (usize < MNT_NS_INFO_SIZE_VER0)
|
||||
@@ -415,12 +432,164 @@ static const struct stashed_operations nsfs_stashed_ops = {
|
||||
.put_data = nsfs_put_data,
|
||||
};
|
||||
|
||||
#define NSFS_FID_SIZE_U32_VER0 (NSFS_FILE_HANDLE_SIZE_VER0 / sizeof(u32))
|
||||
#define NSFS_FID_SIZE_U32_LATEST (NSFS_FILE_HANDLE_SIZE_LATEST / sizeof(u32))
|
||||
|
||||
static int nsfs_encode_fh(struct inode *inode, u32 *fh, int *max_len,
|
||||
struct inode *parent)
|
||||
{
|
||||
struct nsfs_file_handle *fid = (struct nsfs_file_handle *)fh;
|
||||
struct ns_common *ns = inode->i_private;
|
||||
int len = *max_len;
|
||||
|
||||
if (parent)
|
||||
return FILEID_INVALID;
|
||||
|
||||
if (len < NSFS_FID_SIZE_U32_VER0) {
|
||||
*max_len = NSFS_FID_SIZE_U32_LATEST;
|
||||
return FILEID_INVALID;
|
||||
} else if (len > NSFS_FID_SIZE_U32_LATEST) {
|
||||
*max_len = NSFS_FID_SIZE_U32_LATEST;
|
||||
}
|
||||
|
||||
fid->ns_id = ns->ns_id;
|
||||
fid->ns_type = ns->ns_type;
|
||||
fid->ns_inum = inode->i_ino;
|
||||
return FILEID_NSFS;
|
||||
}
|
||||
|
||||
static struct dentry *nsfs_fh_to_dentry(struct super_block *sb, struct fid *fh,
|
||||
int fh_len, int fh_type)
|
||||
{
|
||||
struct path path __free(path_put) = {};
|
||||
struct nsfs_file_handle *fid = (struct nsfs_file_handle *)fh;
|
||||
struct user_namespace *owning_ns = NULL;
|
||||
struct ns_common *ns;
|
||||
int ret;
|
||||
|
||||
if (fh_len < NSFS_FID_SIZE_U32_VER0)
|
||||
return NULL;
|
||||
|
||||
/* Check that any trailing bytes are zero. */
|
||||
if ((fh_len > NSFS_FID_SIZE_U32_LATEST) &&
|
||||
memchr_inv((void *)fid + NSFS_FID_SIZE_U32_LATEST, 0,
|
||||
fh_len - NSFS_FID_SIZE_U32_LATEST))
|
||||
return NULL;
|
||||
|
||||
switch (fh_type) {
|
||||
case FILEID_NSFS:
|
||||
break;
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
scoped_guard(rcu) {
|
||||
ns = ns_tree_lookup_rcu(fid->ns_id, fid->ns_type);
|
||||
if (!ns)
|
||||
return NULL;
|
||||
|
||||
VFS_WARN_ON_ONCE(ns->ns_id != fid->ns_id);
|
||||
VFS_WARN_ON_ONCE(ns->ns_type != fid->ns_type);
|
||||
VFS_WARN_ON_ONCE(ns->inum != fid->ns_inum);
|
||||
|
||||
if (!__ns_ref_get(ns))
|
||||
return NULL;
|
||||
}
|
||||
|
||||
switch (ns->ns_type) {
|
||||
#ifdef CONFIG_CGROUPS
|
||||
case CLONE_NEWCGROUP:
|
||||
if (!current_in_namespace(to_cg_ns(ns)))
|
||||
owning_ns = to_cg_ns(ns)->user_ns;
|
||||
break;
|
||||
#endif
|
||||
#ifdef CONFIG_IPC_NS
|
||||
case CLONE_NEWIPC:
|
||||
if (!current_in_namespace(to_ipc_ns(ns)))
|
||||
owning_ns = to_ipc_ns(ns)->user_ns;
|
||||
break;
|
||||
#endif
|
||||
case CLONE_NEWNS:
|
||||
if (!current_in_namespace(to_mnt_ns(ns)))
|
||||
owning_ns = to_mnt_ns(ns)->user_ns;
|
||||
break;
|
||||
#ifdef CONFIG_NET_NS
|
||||
case CLONE_NEWNET:
|
||||
if (!current_in_namespace(to_net_ns(ns)))
|
||||
owning_ns = to_net_ns(ns)->user_ns;
|
||||
break;
|
||||
#endif
|
||||
#ifdef CONFIG_PID_NS
|
||||
case CLONE_NEWPID:
|
||||
if (!current_in_namespace(to_pid_ns(ns))) {
|
||||
owning_ns = to_pid_ns(ns)->user_ns;
|
||||
} else if (!READ_ONCE(to_pid_ns(ns)->child_reaper)) {
|
||||
ns->ops->put(ns);
|
||||
return ERR_PTR(-EPERM);
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
#ifdef CONFIG_TIME_NS
|
||||
case CLONE_NEWTIME:
|
||||
if (!current_in_namespace(to_time_ns(ns)))
|
||||
owning_ns = to_time_ns(ns)->user_ns;
|
||||
break;
|
||||
#endif
|
||||
#ifdef CONFIG_USER_NS
|
||||
case CLONE_NEWUSER:
|
||||
if (!current_in_namespace(to_user_ns(ns)))
|
||||
owning_ns = to_user_ns(ns);
|
||||
break;
|
||||
#endif
|
||||
#ifdef CONFIG_UTS_NS
|
||||
case CLONE_NEWUTS:
|
||||
if (!current_in_namespace(to_uts_ns(ns)))
|
||||
owning_ns = to_uts_ns(ns)->user_ns;
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
return ERR_PTR(-EOPNOTSUPP);
|
||||
}
|
||||
|
||||
if (owning_ns && !ns_capable(owning_ns, CAP_SYS_ADMIN)) {
|
||||
ns->ops->put(ns);
|
||||
return ERR_PTR(-EPERM);
|
||||
}
|
||||
|
||||
/* path_from_stashed() unconditionally consumes the reference. */
|
||||
ret = path_from_stashed(&ns->stashed, nsfs_mnt, ns, &path);
|
||||
if (ret)
|
||||
return ERR_PTR(ret);
|
||||
|
||||
return no_free_ptr(path.dentry);
|
||||
}
|
||||
|
||||
static int nsfs_export_permission(struct handle_to_path_ctx *ctx,
|
||||
unsigned int oflags)
|
||||
{
|
||||
/* nsfs_fh_to_dentry() performs all permission checks. */
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct file *nsfs_export_open(struct path *path, unsigned int oflags)
|
||||
{
|
||||
return file_open_root(path, "", oflags, 0);
|
||||
}
|
||||
|
||||
static const struct export_operations nsfs_export_operations = {
|
||||
.encode_fh = nsfs_encode_fh,
|
||||
.fh_to_dentry = nsfs_fh_to_dentry,
|
||||
.open = nsfs_export_open,
|
||||
.permission = nsfs_export_permission,
|
||||
};
|
||||
|
||||
static int nsfs_init_fs_context(struct fs_context *fc)
|
||||
{
|
||||
struct pseudo_fs_context *ctx = init_pseudo(fc, NSFS_MAGIC);
|
||||
if (!ctx)
|
||||
return -ENOMEM;
|
||||
ctx->ops = &nsfs_ops;
|
||||
ctx->eops = &nsfs_export_operations;
|
||||
ctx->dops = &ns_dentry_operations;
|
||||
fc->s_fs_info = (void *)&nsfs_stashed_ops;
|
||||
return 0;
|
||||
@@ -438,4 +607,6 @@ void __init nsfs_init(void)
|
||||
if (IS_ERR(nsfs_mnt))
|
||||
panic("can't set nsfs up\n");
|
||||
nsfs_mnt->mnt_sb->s_flags &= ~SB_NOUSER;
|
||||
nsfs_root_path.mnt = nsfs_mnt;
|
||||
nsfs_root_path.dentry = nsfs_mnt->mnt_root;
|
||||
}
|
||||
|
||||
+1
-1
@@ -440,7 +440,7 @@ static bool pidfs_ioctl_valid(unsigned int cmd)
|
||||
* erronously mistook the file descriptor for a pidfd.
|
||||
* This is not perfect but will catch most cases.
|
||||
*/
|
||||
return (_IOC_TYPE(cmd) == _IOC_TYPE(PIDFD_GET_INFO));
|
||||
return extensible_ioctl_valid(cmd, PIDFD_GET_INFO, PIDFD_INFO_SIZE_VER0);
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
+1
-1
@@ -143,7 +143,7 @@ static int proc_parse_pidns_param(struct fs_context *fc,
|
||||
if (!proc_ns_file(ns_filp))
|
||||
return invalfc(fc, "pidns argument is not an nsfs file");
|
||||
ns = get_proc_ns(file_inode(ns_filp));
|
||||
if (ns->ops->type != CLONE_NEWPID)
|
||||
if (ns->ns_type != CLONE_NEWPID)
|
||||
return invalfc(fc, "pidns argument is not a pidns file");
|
||||
target = container_of(ns, struct pid_namespace, ns);
|
||||
|
||||
|
||||
+1
-46
@@ -27,6 +27,7 @@
|
||||
#include <linux/kernel_stat.h>
|
||||
|
||||
#include <linux/cgroup-defs.h>
|
||||
#include <linux/cgroup_namespace.h>
|
||||
|
||||
struct kernel_clone_args;
|
||||
|
||||
@@ -783,52 +784,6 @@ static inline void cgroup_sk_free(struct sock_cgroup_data *skcd) {}
|
||||
|
||||
#endif /* CONFIG_CGROUP_DATA */
|
||||
|
||||
struct cgroup_namespace {
|
||||
struct ns_common ns;
|
||||
struct user_namespace *user_ns;
|
||||
struct ucounts *ucounts;
|
||||
struct css_set *root_cset;
|
||||
};
|
||||
|
||||
extern struct cgroup_namespace init_cgroup_ns;
|
||||
|
||||
#ifdef CONFIG_CGROUPS
|
||||
|
||||
void free_cgroup_ns(struct cgroup_namespace *ns);
|
||||
|
||||
struct cgroup_namespace *copy_cgroup_ns(u64 flags,
|
||||
struct user_namespace *user_ns,
|
||||
struct cgroup_namespace *old_ns);
|
||||
|
||||
int cgroup_path_ns(struct cgroup *cgrp, char *buf, size_t buflen,
|
||||
struct cgroup_namespace *ns);
|
||||
|
||||
static inline void get_cgroup_ns(struct cgroup_namespace *ns)
|
||||
{
|
||||
refcount_inc(&ns->ns.count);
|
||||
}
|
||||
|
||||
static inline void put_cgroup_ns(struct cgroup_namespace *ns)
|
||||
{
|
||||
if (refcount_dec_and_test(&ns->ns.count))
|
||||
free_cgroup_ns(ns);
|
||||
}
|
||||
|
||||
#else /* !CONFIG_CGROUPS */
|
||||
|
||||
static inline void free_cgroup_ns(struct cgroup_namespace *ns) { }
|
||||
static inline struct cgroup_namespace *
|
||||
copy_cgroup_ns(u64 flags, struct user_namespace *user_ns,
|
||||
struct cgroup_namespace *old_ns)
|
||||
{
|
||||
return old_ns;
|
||||
}
|
||||
|
||||
static inline void get_cgroup_ns(struct cgroup_namespace *ns) { }
|
||||
static inline void put_cgroup_ns(struct cgroup_namespace *ns) { }
|
||||
|
||||
#endif /* !CONFIG_CGROUPS */
|
||||
|
||||
#ifdef CONFIG_CGROUPS
|
||||
|
||||
void cgroup_enter_frozen(void);
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
/* SPDX-License-Identifier: GPL-2.0 */
|
||||
#ifndef _LINUX_CGROUP_NAMESPACE_H
|
||||
#define _LINUX_CGROUP_NAMESPACE_H
|
||||
|
||||
#include <linux/ns_common.h>
|
||||
|
||||
struct cgroup_namespace {
|
||||
struct ns_common ns;
|
||||
struct user_namespace *user_ns;
|
||||
struct ucounts *ucounts;
|
||||
struct css_set *root_cset;
|
||||
};
|
||||
|
||||
extern struct cgroup_namespace init_cgroup_ns;
|
||||
|
||||
#ifdef CONFIG_CGROUPS
|
||||
|
||||
static inline struct cgroup_namespace *to_cg_ns(struct ns_common *ns)
|
||||
{
|
||||
return container_of(ns, struct cgroup_namespace, ns);
|
||||
}
|
||||
|
||||
void free_cgroup_ns(struct cgroup_namespace *ns);
|
||||
|
||||
struct cgroup_namespace *copy_cgroup_ns(u64 flags,
|
||||
struct user_namespace *user_ns,
|
||||
struct cgroup_namespace *old_ns);
|
||||
|
||||
int cgroup_path_ns(struct cgroup *cgrp, char *buf, size_t buflen,
|
||||
struct cgroup_namespace *ns);
|
||||
|
||||
static inline void get_cgroup_ns(struct cgroup_namespace *ns)
|
||||
{
|
||||
ns_ref_inc(ns);
|
||||
}
|
||||
|
||||
static inline void put_cgroup_ns(struct cgroup_namespace *ns)
|
||||
{
|
||||
if (ns_ref_put(ns))
|
||||
free_cgroup_ns(ns);
|
||||
}
|
||||
|
||||
#else /* !CONFIG_CGROUPS */
|
||||
|
||||
static inline void free_cgroup_ns(struct cgroup_namespace *ns) { }
|
||||
static inline struct cgroup_namespace *
|
||||
copy_cgroup_ns(u64 flags, struct user_namespace *user_ns,
|
||||
struct cgroup_namespace *old_ns)
|
||||
{
|
||||
return old_ns;
|
||||
}
|
||||
|
||||
static inline void get_cgroup_ns(struct cgroup_namespace *ns) { }
|
||||
static inline void put_cgroup_ns(struct cgroup_namespace *ns) { }
|
||||
|
||||
#endif /* !CONFIG_CGROUPS */
|
||||
|
||||
#endif /* _LINUX_CGROUP_NAMESPACE_H */
|
||||
@@ -122,6 +122,12 @@ enum fid_type {
|
||||
FILEID_BCACHEFS_WITHOUT_PARENT = 0xb1,
|
||||
FILEID_BCACHEFS_WITH_PARENT = 0xb2,
|
||||
|
||||
/*
|
||||
*
|
||||
* 64 bit namespace identifier, 32 bit namespace type, 32 bit inode number.
|
||||
*/
|
||||
FILEID_NSFS = 0xf1,
|
||||
|
||||
/*
|
||||
* 64 bit unique kernfs id
|
||||
*/
|
||||
|
||||
@@ -4018,4 +4018,18 @@ static inline bool vfs_empty_path(int dfd, const char __user *path)
|
||||
|
||||
int generic_atomic_write_valid(struct kiocb *iocb, struct iov_iter *iter);
|
||||
|
||||
static inline bool extensible_ioctl_valid(unsigned int cmd_a,
|
||||
unsigned int cmd_b, size_t min_size)
|
||||
{
|
||||
if (_IOC_DIR(cmd_a) != _IOC_DIR(cmd_b))
|
||||
return false;
|
||||
if (_IOC_TYPE(cmd_a) != _IOC_TYPE(cmd_b))
|
||||
return false;
|
||||
if (_IOC_NR(cmd_a) != _IOC_NR(cmd_b))
|
||||
return false;
|
||||
if (_IOC_SIZE(cmd_a) < min_size)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif /* _LINUX_FS_H */
|
||||
|
||||
@@ -129,20 +129,25 @@ static inline int mq_init_ns(struct ipc_namespace *ns) { return 0; }
|
||||
#endif
|
||||
|
||||
#if defined(CONFIG_IPC_NS)
|
||||
static inline struct ipc_namespace *to_ipc_ns(struct ns_common *ns)
|
||||
{
|
||||
return container_of(ns, struct ipc_namespace, ns);
|
||||
}
|
||||
|
||||
extern struct ipc_namespace *copy_ipcs(u64 flags,
|
||||
struct user_namespace *user_ns, struct ipc_namespace *ns);
|
||||
|
||||
static inline struct ipc_namespace *get_ipc_ns(struct ipc_namespace *ns)
|
||||
{
|
||||
if (ns)
|
||||
refcount_inc(&ns->ns.count);
|
||||
ns_ref_inc(ns);
|
||||
return ns;
|
||||
}
|
||||
|
||||
static inline struct ipc_namespace *get_ipc_ns_not_zero(struct ipc_namespace *ns)
|
||||
{
|
||||
if (ns) {
|
||||
if (refcount_inc_not_zero(&ns->ns.count))
|
||||
if (ns_ref_get(ns))
|
||||
return ns;
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,8 @@ struct fs_struct;
|
||||
struct user_namespace;
|
||||
struct ns_common;
|
||||
|
||||
extern struct mnt_namespace init_mnt_ns;
|
||||
|
||||
extern struct mnt_namespace *copy_mnt_ns(u64, struct mnt_namespace *,
|
||||
struct user_namespace *, struct fs_struct *);
|
||||
extern void put_mnt_ns(struct mnt_namespace *ns);
|
||||
|
||||
+138
-1
@@ -3,14 +3,151 @@
|
||||
#define _LINUX_NS_COMMON_H
|
||||
|
||||
#include <linux/refcount.h>
|
||||
#include <linux/rbtree.h>
|
||||
#include <uapi/linux/sched.h>
|
||||
|
||||
struct proc_ns_operations;
|
||||
|
||||
struct cgroup_namespace;
|
||||
struct ipc_namespace;
|
||||
struct mnt_namespace;
|
||||
struct net;
|
||||
struct pid_namespace;
|
||||
struct time_namespace;
|
||||
struct user_namespace;
|
||||
struct uts_namespace;
|
||||
|
||||
extern struct cgroup_namespace init_cgroup_ns;
|
||||
extern struct ipc_namespace init_ipc_ns;
|
||||
extern struct mnt_namespace init_mnt_ns;
|
||||
extern struct net init_net;
|
||||
extern struct pid_namespace init_pid_ns;
|
||||
extern struct time_namespace init_time_ns;
|
||||
extern struct user_namespace init_user_ns;
|
||||
extern struct uts_namespace init_uts_ns;
|
||||
|
||||
extern const struct proc_ns_operations netns_operations;
|
||||
extern const struct proc_ns_operations utsns_operations;
|
||||
extern const struct proc_ns_operations ipcns_operations;
|
||||
extern const struct proc_ns_operations pidns_operations;
|
||||
extern const struct proc_ns_operations pidns_for_children_operations;
|
||||
extern const struct proc_ns_operations userns_operations;
|
||||
extern const struct proc_ns_operations mntns_operations;
|
||||
extern const struct proc_ns_operations cgroupns_operations;
|
||||
extern const struct proc_ns_operations timens_operations;
|
||||
extern const struct proc_ns_operations timens_for_children_operations;
|
||||
|
||||
struct ns_common {
|
||||
u32 ns_type;
|
||||
struct dentry *stashed;
|
||||
const struct proc_ns_operations *ops;
|
||||
unsigned int inum;
|
||||
refcount_t count;
|
||||
refcount_t __ns_ref; /* do not use directly */
|
||||
union {
|
||||
struct {
|
||||
u64 ns_id;
|
||||
struct rb_node ns_tree_node;
|
||||
struct list_head ns_list_node;
|
||||
};
|
||||
struct rcu_head ns_rcu;
|
||||
};
|
||||
};
|
||||
|
||||
int __ns_common_init(struct ns_common *ns, u32 ns_type, const struct proc_ns_operations *ops, int inum);
|
||||
void __ns_common_free(struct ns_common *ns);
|
||||
|
||||
#define to_ns_common(__ns) \
|
||||
_Generic((__ns), \
|
||||
struct cgroup_namespace *: &(__ns)->ns, \
|
||||
const struct cgroup_namespace *: &(__ns)->ns, \
|
||||
struct ipc_namespace *: &(__ns)->ns, \
|
||||
const struct ipc_namespace *: &(__ns)->ns, \
|
||||
struct mnt_namespace *: &(__ns)->ns, \
|
||||
const struct mnt_namespace *: &(__ns)->ns, \
|
||||
struct net *: &(__ns)->ns, \
|
||||
const struct net *: &(__ns)->ns, \
|
||||
struct pid_namespace *: &(__ns)->ns, \
|
||||
const struct pid_namespace *: &(__ns)->ns, \
|
||||
struct time_namespace *: &(__ns)->ns, \
|
||||
const struct time_namespace *: &(__ns)->ns, \
|
||||
struct user_namespace *: &(__ns)->ns, \
|
||||
const struct user_namespace *: &(__ns)->ns, \
|
||||
struct uts_namespace *: &(__ns)->ns, \
|
||||
const struct uts_namespace *: &(__ns)->ns)
|
||||
|
||||
#define ns_init_inum(__ns) \
|
||||
_Generic((__ns), \
|
||||
struct cgroup_namespace *: CGROUP_NS_INIT_INO, \
|
||||
struct ipc_namespace *: IPC_NS_INIT_INO, \
|
||||
struct mnt_namespace *: MNT_NS_INIT_INO, \
|
||||
struct net *: NET_NS_INIT_INO, \
|
||||
struct pid_namespace *: PID_NS_INIT_INO, \
|
||||
struct time_namespace *: TIME_NS_INIT_INO, \
|
||||
struct user_namespace *: USER_NS_INIT_INO, \
|
||||
struct uts_namespace *: UTS_NS_INIT_INO)
|
||||
|
||||
#define ns_init_ns(__ns) \
|
||||
_Generic((__ns), \
|
||||
struct cgroup_namespace *: &init_cgroup_ns, \
|
||||
struct ipc_namespace *: &init_ipc_ns, \
|
||||
struct mnt_namespace *: &init_mnt_ns, \
|
||||
struct net *: &init_net, \
|
||||
struct pid_namespace *: &init_pid_ns, \
|
||||
struct time_namespace *: &init_time_ns, \
|
||||
struct user_namespace *: &init_user_ns, \
|
||||
struct uts_namespace *: &init_uts_ns)
|
||||
|
||||
#define to_ns_operations(__ns) \
|
||||
_Generic((__ns), \
|
||||
struct cgroup_namespace *: (IS_ENABLED(CONFIG_CGROUPS) ? &cgroupns_operations : NULL), \
|
||||
struct ipc_namespace *: (IS_ENABLED(CONFIG_IPC_NS) ? &ipcns_operations : NULL), \
|
||||
struct mnt_namespace *: &mntns_operations, \
|
||||
struct net *: (IS_ENABLED(CONFIG_NET_NS) ? &netns_operations : NULL), \
|
||||
struct pid_namespace *: (IS_ENABLED(CONFIG_PID_NS) ? &pidns_operations : NULL), \
|
||||
struct time_namespace *: (IS_ENABLED(CONFIG_TIME_NS) ? &timens_operations : NULL), \
|
||||
struct user_namespace *: (IS_ENABLED(CONFIG_USER_NS) ? &userns_operations : NULL), \
|
||||
struct uts_namespace *: (IS_ENABLED(CONFIG_UTS_NS) ? &utsns_operations : NULL))
|
||||
|
||||
#define ns_common_type(__ns) \
|
||||
_Generic((__ns), \
|
||||
struct cgroup_namespace *: CLONE_NEWCGROUP, \
|
||||
struct ipc_namespace *: CLONE_NEWIPC, \
|
||||
struct mnt_namespace *: CLONE_NEWNS, \
|
||||
struct net *: CLONE_NEWNET, \
|
||||
struct pid_namespace *: CLONE_NEWPID, \
|
||||
struct time_namespace *: CLONE_NEWTIME, \
|
||||
struct user_namespace *: CLONE_NEWUSER, \
|
||||
struct uts_namespace *: CLONE_NEWUTS)
|
||||
|
||||
#define ns_common_init(__ns) \
|
||||
__ns_common_init(to_ns_common(__ns), \
|
||||
ns_common_type(__ns), \
|
||||
to_ns_operations(__ns), \
|
||||
(((__ns) == ns_init_ns(__ns)) ? ns_init_inum(__ns) : 0))
|
||||
|
||||
#define ns_common_init_inum(__ns, __inum) \
|
||||
__ns_common_init(to_ns_common(__ns), \
|
||||
ns_common_type(__ns), \
|
||||
to_ns_operations(__ns), \
|
||||
__inum)
|
||||
|
||||
#define ns_common_free(__ns) __ns_common_free(to_ns_common((__ns)))
|
||||
|
||||
static __always_inline __must_check bool __ns_ref_put(struct ns_common *ns)
|
||||
{
|
||||
return refcount_dec_and_test(&ns->__ns_ref);
|
||||
}
|
||||
|
||||
static __always_inline __must_check bool __ns_ref_get(struct ns_common *ns)
|
||||
{
|
||||
return refcount_inc_not_zero(&ns->__ns_ref);
|
||||
}
|
||||
|
||||
#define ns_ref_read(__ns) refcount_read(&to_ns_common((__ns))->__ns_ref)
|
||||
#define ns_ref_inc(__ns) refcount_inc(&to_ns_common((__ns))->__ns_ref)
|
||||
#define ns_ref_get(__ns) __ns_ref_get(to_ns_common((__ns)))
|
||||
#define ns_ref_put(__ns) __ns_ref_put(to_ns_common((__ns)))
|
||||
#define ns_ref_put_and_lock(__ns, __lock) \
|
||||
refcount_dec_and_lock(&to_ns_common((__ns))->__ns_ref, (__lock))
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
/* SPDX-License-Identifier: GPL-2.0 */
|
||||
/* Copyright (c) 2025 Christian Brauner <brauner@kernel.org> */
|
||||
|
||||
#ifndef _LINUX_NSFS_H
|
||||
#define _LINUX_NSFS_H
|
||||
|
||||
#include <linux/ns_common.h>
|
||||
#include <linux/cred.h>
|
||||
#include <linux/pid_namespace.h>
|
||||
|
||||
struct path;
|
||||
struct task_struct;
|
||||
struct proc_ns_operations;
|
||||
|
||||
int ns_get_path(struct path *path, struct task_struct *task,
|
||||
const struct proc_ns_operations *ns_ops);
|
||||
typedef struct ns_common *ns_get_path_helper_t(void *);
|
||||
int ns_get_path_cb(struct path *path, ns_get_path_helper_t ns_get_cb,
|
||||
void *private_data);
|
||||
|
||||
bool ns_match(const struct ns_common *ns, dev_t dev, ino_t ino);
|
||||
|
||||
int ns_get_name(char *buf, size_t size, struct task_struct *task,
|
||||
const struct proc_ns_operations *ns_ops);
|
||||
void nsfs_init(void);
|
||||
|
||||
#define __current_namespace_from_type(__ns) \
|
||||
_Generic((__ns), \
|
||||
struct cgroup_namespace *: current->nsproxy->cgroup_ns, \
|
||||
struct ipc_namespace *: current->nsproxy->ipc_ns, \
|
||||
struct net *: current->nsproxy->net_ns, \
|
||||
struct pid_namespace *: task_active_pid_ns(current), \
|
||||
struct mnt_namespace *: current->nsproxy->mnt_ns, \
|
||||
struct time_namespace *: current->nsproxy->time_ns, \
|
||||
struct user_namespace *: current_user_ns(), \
|
||||
struct uts_namespace *: current->nsproxy->uts_ns)
|
||||
|
||||
#define current_in_namespace(__ns) (__current_namespace_from_type(__ns) == __ns)
|
||||
|
||||
#endif /* _LINUX_NSFS_H */
|
||||
@@ -42,17 +42,6 @@ struct nsproxy {
|
||||
};
|
||||
extern struct nsproxy init_nsproxy;
|
||||
|
||||
#define to_ns_common(__ns) \
|
||||
_Generic((__ns), \
|
||||
struct cgroup_namespace *: &(__ns->ns), \
|
||||
struct ipc_namespace *: &(__ns->ns), \
|
||||
struct net *: &(__ns->ns), \
|
||||
struct pid_namespace *: &(__ns->ns), \
|
||||
struct mnt_namespace *: &(__ns->ns), \
|
||||
struct time_namespace *: &(__ns->ns), \
|
||||
struct user_namespace *: &(__ns->ns), \
|
||||
struct uts_namespace *: &(__ns->ns))
|
||||
|
||||
/*
|
||||
* A structure to encompass all bits needed to install
|
||||
* a partial or complete new set of namespaces.
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
/* SPDX-License-Identifier: GPL-2.0 */
|
||||
#ifndef _LINUX_NSTREE_H
|
||||
#define _LINUX_NSTREE_H
|
||||
|
||||
#include <linux/ns_common.h>
|
||||
#include <linux/nsproxy.h>
|
||||
#include <linux/rbtree.h>
|
||||
#include <linux/seqlock.h>
|
||||
#include <linux/rculist.h>
|
||||
#include <linux/cookie.h>
|
||||
|
||||
extern struct ns_tree cgroup_ns_tree;
|
||||
extern struct ns_tree ipc_ns_tree;
|
||||
extern struct ns_tree mnt_ns_tree;
|
||||
extern struct ns_tree net_ns_tree;
|
||||
extern struct ns_tree pid_ns_tree;
|
||||
extern struct ns_tree time_ns_tree;
|
||||
extern struct ns_tree user_ns_tree;
|
||||
extern struct ns_tree uts_ns_tree;
|
||||
|
||||
#define to_ns_tree(__ns) \
|
||||
_Generic((__ns), \
|
||||
struct cgroup_namespace *: &(cgroup_ns_tree), \
|
||||
struct ipc_namespace *: &(ipc_ns_tree), \
|
||||
struct net *: &(net_ns_tree), \
|
||||
struct pid_namespace *: &(pid_ns_tree), \
|
||||
struct mnt_namespace *: &(mnt_ns_tree), \
|
||||
struct time_namespace *: &(time_ns_tree), \
|
||||
struct user_namespace *: &(user_ns_tree), \
|
||||
struct uts_namespace *: &(uts_ns_tree))
|
||||
|
||||
u64 ns_tree_gen_id(struct ns_common *ns);
|
||||
void __ns_tree_add_raw(struct ns_common *ns, struct ns_tree *ns_tree);
|
||||
void __ns_tree_remove(struct ns_common *ns, struct ns_tree *ns_tree);
|
||||
struct ns_common *ns_tree_lookup_rcu(u64 ns_id, int ns_type);
|
||||
struct ns_common *__ns_tree_adjoined_rcu(struct ns_common *ns,
|
||||
struct ns_tree *ns_tree,
|
||||
bool previous);
|
||||
|
||||
static inline void __ns_tree_add(struct ns_common *ns, struct ns_tree *ns_tree)
|
||||
{
|
||||
ns_tree_gen_id(ns);
|
||||
__ns_tree_add_raw(ns, ns_tree);
|
||||
}
|
||||
|
||||
/**
|
||||
* ns_tree_add_raw - Add a namespace to a namespace
|
||||
* @ns: Namespace to add
|
||||
*
|
||||
* This function adds a namespace to the appropriate namespace tree
|
||||
* without assigning a id.
|
||||
*/
|
||||
#define ns_tree_add_raw(__ns) __ns_tree_add_raw(to_ns_common(__ns), to_ns_tree(__ns))
|
||||
|
||||
/**
|
||||
* ns_tree_add - Add a namespace to a namespace tree
|
||||
* @ns: Namespace to add
|
||||
*
|
||||
* This function assigns a new id to the namespace and adds it to the
|
||||
* appropriate namespace tree and list.
|
||||
*/
|
||||
#define ns_tree_add(__ns) __ns_tree_add(to_ns_common(__ns), to_ns_tree(__ns))
|
||||
|
||||
/**
|
||||
* ns_tree_remove - Remove a namespace from a namespace tree
|
||||
* @ns: Namespace to remove
|
||||
*
|
||||
* This function removes a namespace from the appropriate namespace
|
||||
* tree and list.
|
||||
*/
|
||||
#define ns_tree_remove(__ns) __ns_tree_remove(to_ns_common(__ns), to_ns_tree(__ns))
|
||||
|
||||
#define ns_tree_adjoined_rcu(__ns, __previous) \
|
||||
__ns_tree_adjoined_rcu(to_ns_common(__ns), to_ns_tree(__ns), __previous)
|
||||
|
||||
#define ns_tree_active(__ns) (!RB_EMPTY_NODE(&to_ns_common(__ns)->ns_tree_node))
|
||||
|
||||
#endif /* _LINUX_NSTREE_H */
|
||||
@@ -54,10 +54,15 @@ extern struct pid_namespace init_pid_ns;
|
||||
#define PIDNS_ADDING (1U << 31)
|
||||
|
||||
#ifdef CONFIG_PID_NS
|
||||
static inline struct pid_namespace *to_pid_ns(struct ns_common *ns)
|
||||
{
|
||||
return container_of(ns, struct pid_namespace, ns);
|
||||
}
|
||||
|
||||
static inline struct pid_namespace *get_pid_ns(struct pid_namespace *ns)
|
||||
{
|
||||
if (ns != &init_pid_ns)
|
||||
refcount_inc(&ns->ns.count);
|
||||
ns_ref_inc(ns);
|
||||
return ns;
|
||||
}
|
||||
|
||||
|
||||
+1
-21
@@ -5,7 +5,7 @@
|
||||
#ifndef _LINUX_PROC_NS_H
|
||||
#define _LINUX_PROC_NS_H
|
||||
|
||||
#include <linux/ns_common.h>
|
||||
#include <linux/nsfs.h>
|
||||
#include <uapi/linux/nsfs.h>
|
||||
|
||||
struct pid_namespace;
|
||||
@@ -17,7 +17,6 @@ struct inode;
|
||||
struct proc_ns_operations {
|
||||
const char *name;
|
||||
const char *real_ns_name;
|
||||
int type;
|
||||
struct ns_common *(*get)(struct task_struct *task);
|
||||
void (*put)(struct ns_common *ns);
|
||||
int (*install)(struct nsset *nsset, struct ns_common *ns);
|
||||
@@ -66,25 +65,6 @@ static inline void proc_free_inum(unsigned int inum) {}
|
||||
|
||||
#endif /* CONFIG_PROC_FS */
|
||||
|
||||
static inline int ns_alloc_inum(struct ns_common *ns)
|
||||
{
|
||||
WRITE_ONCE(ns->stashed, NULL);
|
||||
return proc_alloc_inum(&ns->inum);
|
||||
}
|
||||
|
||||
#define ns_free_inum(ns) proc_free_inum((ns)->inum)
|
||||
|
||||
#define get_proc_ns(inode) ((struct ns_common *)(inode)->i_private)
|
||||
extern int ns_get_path(struct path *path, struct task_struct *task,
|
||||
const struct proc_ns_operations *ns_ops);
|
||||
typedef struct ns_common *ns_get_path_helper_t(void *);
|
||||
extern int ns_get_path_cb(struct path *path, ns_get_path_helper_t ns_get_cb,
|
||||
void *private_data);
|
||||
|
||||
extern bool ns_match(const struct ns_common *ns, dev_t dev, ino_t ino);
|
||||
|
||||
extern int ns_get_name(char *buf, size_t size, struct task_struct *task,
|
||||
const struct proc_ns_operations *ns_ops);
|
||||
extern void nsfs_init(void);
|
||||
|
||||
#endif /* _LINUX_PROC_NS_H */
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user