6566 lines · 165.6 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0-only/* *  linux/fs/namespace.c * * (C) Copyright Al Viro 2000, 2001 * * Based on code from fs/super.c, copyright Linus Torvalds and others. * Heavily rewritten. */ #include <linux/syscalls.h>#include <linux/export.h>#include <linux/capability.h>#include <linux/mnt_namespace.h>#include <linux/user_namespace.h>#include <linux/namei.h>#include <linux/security.h>#include <linux/cred.h>#include <linux/idr.h>#include <linux/init.h>		/* init_rootfs */#include <linux/fs_struct.h>	/* get_fs_root et.al. */#include <linux/fsnotify.h>	/* fsnotify_vfsmount_delete */#include <linux/file.h>#include <linux/uaccess.h>#include <linux/proc_ns.h>#include <linux/magic.h>#include <linux/memblock.h>#include <linux/proc_fs.h>#include <linux/task_work.h>#include <linux/sched/task.h>#include <uapi/linux/mount.h>#include <linux/fs_context.h>#include <linux/shmem_fs.h>#include <linux/mnt_idmapping.h>#include <linux/pidfs.h>#include <linux/nstree.h> #include "pnode.h"#include "internal.h" /* Maximum number of mounts in a mount namespace */static unsigned int sysctl_mount_max __read_mostly = 100000; static unsigned int m_hash_mask __ro_after_init;static unsigned int m_hash_shift __ro_after_init;static unsigned int mp_hash_mask __ro_after_init;static unsigned int mp_hash_shift __ro_after_init; static __initdata unsigned long mhash_entries;static int __init set_mhash_entries(char *str){	return kstrtoul(str, 0, &mhash_entries) == 0;}__setup("mhash_entries=", set_mhash_entries); static __initdata unsigned long mphash_entries;static int __init set_mphash_entries(char *str){	return kstrtoul(str, 0, &mphash_entries) == 0;}__setup("mphash_entries=", set_mphash_entries); static char * __initdata initramfs_options;static int __init initramfs_options_setup(char *str){	initramfs_options = str;	return 1;} __setup("initramfs_options=", initramfs_options_setup); static u64 event;static DEFINE_XARRAY_FLAGS(mnt_id_xa, XA_FLAGS_ALLOC);static DEFINE_IDA(mnt_group_ida); /* Don't allow confusion with old 32bit mount ID */#define MNT_UNIQUE_ID_OFFSET (1ULL << 31)static u64 mnt_id_ctr = MNT_UNIQUE_ID_OFFSET; static struct hlist_head *mount_hashtable __ro_after_init;static struct hlist_head *mountpoint_hashtable __ro_after_init;static struct kmem_cache *mnt_cache __ro_after_init;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 inline void namespace_lock(void);static void namespace_unlock(void);DEFINE_LOCK_GUARD_0(namespace_excl, namespace_lock(), namespace_unlock())DEFINE_LOCK_GUARD_0(namespace_shared, down_read(&namespace_sem),				      up_read(&namespace_sem)) DEFINE_FREE(mntput, struct vfsmount *, if (!IS_ERR(_T)) mntput(_T)) #ifdef CONFIG_FSNOTIFYLIST_HEAD(notify_list); /* protected by namespace_sem */#endif enum mount_kattr_flags_t {	MOUNT_KATTR_RECURSE		= (1 << 0),	MOUNT_KATTR_IDMAP_REPLACE	= (1 << 1),}; struct mount_kattr {	unsigned int attr_set;	unsigned int attr_clr;	unsigned int propagation;	unsigned int lookup_flags;	enum mount_kattr_flags_t kflags;	struct user_namespace *mnt_userns;	struct mnt_idmap *mnt_idmap;}; /* /sys/fs */struct kobject *fs_kobj __ro_after_init;EXPORT_SYMBOL_GPL(fs_kobj); /* * vfsmount lock may be taken for read to prevent changes to the * vfsmount hash, ie. during mountpoint lookups or walking back * up the tree. * * It should be taken for write in all cases where the vfsmount * tree or hash is modified or when a vfsmount structure is modified. */__cacheline_aligned_in_smp DEFINE_SEQLOCK(mount_lock); static void mnt_ns_release(struct mnt_namespace *ns){	/* keep alive for {list,stat}mount() */	if (ns && refcount_dec_and_test(&ns->passive)) {		fsnotify_mntns_delete(ns);		put_user_ns(ns->user_ns);		kfree(ns);	}}DEFINE_FREE(mnt_ns_release, struct mnt_namespace *,	    if (!IS_ERR(_T)) mnt_ns_release(_T)) static void mnt_ns_release_rcu(struct rcu_head *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 (ns_tree_active(ns))		ns_tree_remove(ns); 	call_rcu(&ns->ns.ns_rcu, mnt_ns_release_rcu);} /* * Lookup a mount namespace by id and take a passive reference count. Taking a * passive reference means the mount namespace can be emptied if e.g., the last * task holding an active reference exits. To access the mounts of the * namespace the @namespace_sem must first be acquired. If the namespace has * already shut down before acquiring @namespace_sem, {list,stat}mount() will * see that the mount rbtree of the namespace is empty. * * Note the lookup is lockless protected by a sequence counter. We only * need to guard against false negatives as false positives aren't * possible. So if we didn't find a mount namespace and the sequence * counter has changed we need to retry. If the sequence counter is * still the same we know the search actually failed. */static struct mnt_namespace *lookup_mnt_ns(u64 mnt_ns_id){	struct mnt_namespace *mnt_ns;	struct ns_common *ns; 	guard(rcu)();	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.	 */	mnt_ns = container_of(ns, struct mnt_namespace, ns);	refcount_inc(&mnt_ns->passive);	return mnt_ns;} static inline void lock_mount_hash(void){	write_seqlock(&mount_lock);} static inline void unlock_mount_hash(void){	write_sequnlock(&mount_lock);} static inline struct hlist_head *m_hash(struct vfsmount *mnt, struct dentry *dentry){	unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);	tmp += ((unsigned long)dentry / L1_CACHE_BYTES);	tmp = tmp + (tmp >> m_hash_shift);	return &mount_hashtable[tmp & m_hash_mask];} static inline struct hlist_head *mp_hash(struct dentry *dentry){	unsigned long tmp = ((unsigned long)dentry / L1_CACHE_BYTES);	tmp = tmp + (tmp >> mp_hash_shift);	return &mountpoint_hashtable[tmp & mp_hash_mask];} static int mnt_alloc_id(struct mount *mnt){	int res; 	xa_lock(&mnt_id_xa);	res = __xa_alloc(&mnt_id_xa, &mnt->mnt_id, mnt, xa_limit_31b, GFP_KERNEL);	if (!res)		mnt->mnt_id_unique = ++mnt_id_ctr;	xa_unlock(&mnt_id_xa);	return res;} static void mnt_free_id(struct mount *mnt){	xa_erase(&mnt_id_xa, mnt->mnt_id);} /* * Allocate a new peer group ID */static int mnt_alloc_group_id(struct mount *mnt){	int res = ida_alloc_min(&mnt_group_ida, 1, GFP_KERNEL); 	if (res < 0)		return res;	mnt->mnt_group_id = res;	return 0;} /* * Release a peer group ID */void mnt_release_group_id(struct mount *mnt){	ida_free(&mnt_group_ida, mnt->mnt_group_id);	mnt->mnt_group_id = 0;} /* * vfsmount lock must be held for read */static inline void mnt_add_count(struct mount *mnt, int n){#ifdef CONFIG_SMP	this_cpu_add(mnt->mnt_pcp->mnt_count, n);#else	preempt_disable();	mnt->mnt_count += n;	preempt_enable();#endif} /* * vfsmount lock must be held for write */int mnt_get_count(struct mount *mnt){#ifdef CONFIG_SMP	int count = 0;	int cpu; 	for_each_possible_cpu(cpu) {		count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;	} 	return count;#else	return mnt->mnt_count;#endif} static struct mount *alloc_vfsmnt(const char *name){	struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);	if (mnt) {		int err; 		err = mnt_alloc_id(mnt);		if (err)			goto out_free_cache; 		if (name)			mnt->mnt_devname = kstrdup_const(name,							 GFP_KERNEL_ACCOUNT);		else			mnt->mnt_devname = "none";		if (!mnt->mnt_devname)			goto out_free_id; #ifdef CONFIG_SMP		mnt->mnt_pcp = alloc_percpu(struct mnt_pcp);		if (!mnt->mnt_pcp)			goto out_free_devname; 		this_cpu_add(mnt->mnt_pcp->mnt_count, 1);#else		mnt->mnt_count = 1;		mnt->mnt_writers = 0;#endif 		INIT_HLIST_NODE(&mnt->mnt_hash);		INIT_LIST_HEAD(&mnt->mnt_child);		INIT_LIST_HEAD(&mnt->mnt_mounts);		INIT_LIST_HEAD(&mnt->mnt_list);		INIT_LIST_HEAD(&mnt->mnt_expire);		INIT_LIST_HEAD(&mnt->mnt_share);		INIT_HLIST_HEAD(&mnt->mnt_slave_list);		INIT_HLIST_NODE(&mnt->mnt_slave);		INIT_HLIST_NODE(&mnt->mnt_mp_list);		INIT_HLIST_HEAD(&mnt->mnt_stuck_children);		INIT_HLIST_NODE(&mnt->mnt_ns_visible);		RB_CLEAR_NODE(&mnt->mnt_node);		mnt->mnt.mnt_idmap = &nop_mnt_idmap;	}	return mnt; #ifdef CONFIG_SMPout_free_devname:	kfree_const(mnt->mnt_devname);#endifout_free_id:	mnt_free_id(mnt);out_free_cache:	kmem_cache_free(mnt_cache, mnt);	return NULL;} /* * Most r/o checks on a fs are for operations that take * discrete amounts of time, like a write() or unlink(). * We must keep track of when those operations start * (for permission checks) and when they end, so that * we can determine when writes are able to occur to * a filesystem. *//* * __mnt_is_readonly: check whether a mount is read-only * @mnt: the mount to check for its write status * * This shouldn't be used directly ouside of the VFS. * It does not guarantee that the filesystem will stay * r/w, just that it is right *now*.  This can not and * should not be used in place of IS_RDONLY(inode). * mnt_want/drop_write() will _keep_ the filesystem * r/w. */bool __mnt_is_readonly(const struct vfsmount *mnt){	return (mnt->mnt_flags & MNT_READONLY) || sb_rdonly(mnt->mnt_sb);}EXPORT_SYMBOL_GPL(__mnt_is_readonly); static inline void mnt_inc_writers(struct mount *mnt){#ifdef CONFIG_SMP	this_cpu_inc(mnt->mnt_pcp->mnt_writers);#else	mnt->mnt_writers++;#endif} static inline void mnt_dec_writers(struct mount *mnt){#ifdef CONFIG_SMP	this_cpu_dec(mnt->mnt_pcp->mnt_writers);#else	mnt->mnt_writers--;#endif} static unsigned int mnt_get_writers(struct mount *mnt){#ifdef CONFIG_SMP	unsigned int count = 0;	int cpu; 	for_each_possible_cpu(cpu) {		count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers;	} 	return count;#else	return mnt->mnt_writers;#endif} static int mnt_is_readonly(const struct vfsmount *mnt){	if (READ_ONCE(mnt->mnt_sb->s_readonly_remount))		return 1;	/*	 * The barrier pairs with the barrier in sb_start_ro_state_change()	 * making sure if we don't see s_readonly_remount set yet, we also will	 * not see any superblock / mount flag changes done by remount.	 * It also pairs with the barrier in sb_end_ro_state_change()	 * assuring that if we see s_readonly_remount already cleared, we will	 * see the values of superblock / mount flags updated by remount.	 */	smp_rmb();	return __mnt_is_readonly(mnt);} /* * Most r/o & frozen checks on a fs are for operations that take discrete * amounts of time, like a write() or unlink().  We must keep track of when * those operations start (for permission checks) and when they end, so that we * can determine when writes are able to occur to a filesystem. *//** * mnt_get_write_access - get write access to a mount without freeze protection * @m: the mount on which to take a write * * This tells the low-level filesystem that a write is about to be performed to * it, and makes sure that writes are allowed (mnt it read-write) before * returning success. This operation does not protect against filesystem being * frozen. When the write operation is finished, mnt_put_write_access() must be * called. This is effectively a refcount. */int mnt_get_write_access(struct vfsmount *m){	struct mount *mnt = real_mount(m);	int ret = 0; 	preempt_disable();	mnt_inc_writers(mnt);	/*	 * The store to mnt_inc_writers must be visible before we pass	 * WRITE_HOLD loop below, so that the slowpath can see our	 * incremented count after it has set WRITE_HOLD.	 */	smp_mb();	might_lock(&mount_lock.lock);	while (__test_write_hold(READ_ONCE(mnt->mnt_pprev_for_sb))) {		if (!IS_ENABLED(CONFIG_PREEMPT_RT)) {			cpu_relax();		} else {			/*			 * This prevents priority inversion, if the task			 * setting WRITE_HOLD got preempted on a remote			 * CPU, and it prevents life lock if the task setting			 * WRITE_HOLD has a lower priority and is bound to			 * the same CPU as the task that is spinning here.			 */			preempt_enable();			read_seqlock_excl(&mount_lock);			read_sequnlock_excl(&mount_lock);			preempt_disable();		}	}	/*	 * The barrier pairs with the barrier sb_start_ro_state_change() making	 * sure that if we see WRITE_HOLD cleared, we will also see	 * s_readonly_remount set (or even SB_RDONLY / MNT_READONLY flags) in	 * mnt_is_readonly() and bail in case we are racing with remount	 * read-only.	 */	smp_rmb();	if (mnt_is_readonly(m)) {		mnt_dec_writers(mnt);		ret = -EROFS;	}	preempt_enable(); 	return ret;}EXPORT_SYMBOL_GPL(mnt_get_write_access); /** * mnt_want_write - get write access to a mount * @m: the mount on which to take a write * * This tells the low-level filesystem that a write is about to be performed to * it, and makes sure that writes are allowed (mount is read-write, filesystem * is not frozen) before returning success.  When the write operation is * finished, mnt_drop_write() must be called.  This is effectively a refcount. */int mnt_want_write(struct vfsmount *m){	int ret; 	sb_start_write(m->mnt_sb);	ret = mnt_get_write_access(m);	if (ret)		sb_end_write(m->mnt_sb);	return ret;}EXPORT_SYMBOL_GPL(mnt_want_write);

Lines 1–500 of 6566.