xref: /linux/fs/namespace.c (revision b7ce6fa90fd9554482847b19756a06232c1dc78c)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  linux/fs/namespace.c
4  *
5  * (C) Copyright Al Viro 2000, 2001
6  *
7  * Based on code from fs/super.c, copyright Linus Torvalds and others.
8  * Heavily rewritten.
9  */
10 
11 #include <linux/syscalls.h>
12 #include <linux/export.h>
13 #include <linux/capability.h>
14 #include <linux/mnt_namespace.h>
15 #include <linux/user_namespace.h>
16 #include <linux/namei.h>
17 #include <linux/security.h>
18 #include <linux/cred.h>
19 #include <linux/idr.h>
20 #include <linux/init.h>		/* init_rootfs */
21 #include <linux/fs_struct.h>	/* get_fs_root et.al. */
22 #include <linux/fsnotify.h>	/* fsnotify_vfsmount_delete */
23 #include <linux/file.h>
24 #include <linux/uaccess.h>
25 #include <linux/proc_ns.h>
26 #include <linux/magic.h>
27 #include <linux/memblock.h>
28 #include <linux/proc_fs.h>
29 #include <linux/task_work.h>
30 #include <linux/sched/task.h>
31 #include <uapi/linux/mount.h>
32 #include <linux/fs_context.h>
33 #include <linux/shmem_fs.h>
34 #include <linux/mnt_idmapping.h>
35 #include <linux/pidfs.h>
36 
37 #include "pnode.h"
38 #include "internal.h"
39 
40 /* Maximum number of mounts in a mount namespace */
41 static unsigned int sysctl_mount_max __read_mostly = 100000;
42 
43 static unsigned int m_hash_mask __ro_after_init;
44 static unsigned int m_hash_shift __ro_after_init;
45 static unsigned int mp_hash_mask __ro_after_init;
46 static unsigned int mp_hash_shift __ro_after_init;
47 
48 static __initdata unsigned long mhash_entries;
49 static int __init set_mhash_entries(char *str)
50 {
51 	if (!str)
52 		return 0;
53 	mhash_entries = simple_strtoul(str, &str, 0);
54 	return 1;
55 }
56 __setup("mhash_entries=", set_mhash_entries);
57 
58 static __initdata unsigned long mphash_entries;
59 static int __init set_mphash_entries(char *str)
60 {
61 	if (!str)
62 		return 0;
63 	mphash_entries = simple_strtoul(str, &str, 0);
64 	return 1;
65 }
66 __setup("mphash_entries=", set_mphash_entries);
67 
68 static char * __initdata initramfs_options;
69 static int __init initramfs_options_setup(char *str)
70 {
71 	initramfs_options = str;
72 	return 1;
73 }
74 
75 __setup("initramfs_options=", initramfs_options_setup);
76 
77 static u64 event;
78 static DEFINE_XARRAY_FLAGS(mnt_id_xa, XA_FLAGS_ALLOC);
79 static DEFINE_IDA(mnt_group_ida);
80 
81 /* Don't allow confusion with old 32bit mount ID */
82 #define MNT_UNIQUE_ID_OFFSET (1ULL << 31)
83 static u64 mnt_id_ctr = MNT_UNIQUE_ID_OFFSET;
84 
85 static struct hlist_head *mount_hashtable __ro_after_init;
86 static struct hlist_head *mountpoint_hashtable __ro_after_init;
87 static struct kmem_cache *mnt_cache __ro_after_init;
88 static DECLARE_RWSEM(namespace_sem);
89 static HLIST_HEAD(unmounted);	/* protected by namespace_sem */
90 static LIST_HEAD(ex_mountpoints); /* protected by namespace_sem */
91 static struct mnt_namespace *emptied_ns; /* protected by namespace_sem */
92 static DEFINE_SEQLOCK(mnt_ns_tree_lock);
93 
94 #ifdef CONFIG_FSNOTIFY
95 LIST_HEAD(notify_list); /* protected by namespace_sem */
96 #endif
97 static struct rb_root mnt_ns_tree = RB_ROOT; /* protected by mnt_ns_tree_lock */
98 static LIST_HEAD(mnt_ns_list); /* protected by mnt_ns_tree_lock */
99 
100 enum mount_kattr_flags_t {
101 	MOUNT_KATTR_RECURSE		= (1 << 0),
102 	MOUNT_KATTR_IDMAP_REPLACE	= (1 << 1),
103 };
104 
105 struct mount_kattr {
106 	unsigned int attr_set;
107 	unsigned int attr_clr;
108 	unsigned int propagation;
109 	unsigned int lookup_flags;
110 	enum mount_kattr_flags_t kflags;
111 	struct user_namespace *mnt_userns;
112 	struct mnt_idmap *mnt_idmap;
113 };
114 
115 /* /sys/fs */
116 struct kobject *fs_kobj __ro_after_init;
117 EXPORT_SYMBOL_GPL(fs_kobj);
118 
119 /*
120  * vfsmount lock may be taken for read to prevent changes to the
121  * vfsmount hash, ie. during mountpoint lookups or walking back
122  * up the tree.
123  *
124  * It should be taken for write in all cases where the vfsmount
125  * tree or hash is modified or when a vfsmount structure is modified.
126  */
127 __cacheline_aligned_in_smp DEFINE_SEQLOCK(mount_lock);
128 
129 static inline struct mnt_namespace *node_to_mnt_ns(const struct rb_node *node)
130 {
131 	if (!node)
132 		return NULL;
133 	return rb_entry(node, struct mnt_namespace, mnt_ns_tree_node);
134 }
135 
136 static int mnt_ns_cmp(struct rb_node *a, const struct rb_node *b)
137 {
138 	struct mnt_namespace *ns_a = node_to_mnt_ns(a);
139 	struct mnt_namespace *ns_b = node_to_mnt_ns(b);
140 	u64 seq_a = ns_a->seq;
141 	u64 seq_b = ns_b->seq;
142 
143 	if (seq_a < seq_b)
144 		return -1;
145 	if (seq_a > seq_b)
146 		return 1;
147 	return 0;
148 }
149 
150 static inline void mnt_ns_tree_write_lock(void)
151 {
152 	write_seqlock(&mnt_ns_tree_lock);
153 }
154 
155 static inline void mnt_ns_tree_write_unlock(void)
156 {
157 	write_sequnlock(&mnt_ns_tree_lock);
158 }
159 
160 static void mnt_ns_tree_add(struct mnt_namespace *ns)
161 {
162 	struct rb_node *node, *prev;
163 
164 	mnt_ns_tree_write_lock();
165 	node = rb_find_add_rcu(&ns->mnt_ns_tree_node, &mnt_ns_tree, mnt_ns_cmp);
166 	/*
167 	 * If there's no previous entry simply add it after the
168 	 * head and if there is add it after the previous entry.
169 	 */
170 	prev = rb_prev(&ns->mnt_ns_tree_node);
171 	if (!prev)
172 		list_add_rcu(&ns->mnt_ns_list, &mnt_ns_list);
173 	else
174 		list_add_rcu(&ns->mnt_ns_list, &node_to_mnt_ns(prev)->mnt_ns_list);
175 	mnt_ns_tree_write_unlock();
176 
177 	WARN_ON_ONCE(node);
178 }
179 
180 static void mnt_ns_release(struct mnt_namespace *ns)
181 {
182 	/* keep alive for {list,stat}mount() */
183 	if (refcount_dec_and_test(&ns->passive)) {
184 		fsnotify_mntns_delete(ns);
185 		put_user_ns(ns->user_ns);
186 		kfree(ns);
187 	}
188 }
189 DEFINE_FREE(mnt_ns_release, struct mnt_namespace *, if (_T) mnt_ns_release(_T))
190 
191 static void mnt_ns_release_rcu(struct rcu_head *rcu)
192 {
193 	mnt_ns_release(container_of(rcu, struct mnt_namespace, mnt_ns_rcu));
194 }
195 
196 static void mnt_ns_tree_remove(struct mnt_namespace *ns)
197 {
198 	/* remove from global mount namespace list */
199 	if (!is_anon_ns(ns)) {
200 		mnt_ns_tree_write_lock();
201 		rb_erase(&ns->mnt_ns_tree_node, &mnt_ns_tree);
202 		list_bidir_del_rcu(&ns->mnt_ns_list);
203 		mnt_ns_tree_write_unlock();
204 	}
205 
206 	call_rcu(&ns->mnt_ns_rcu, mnt_ns_release_rcu);
207 }
208 
209 static int mnt_ns_find(const void *key, const struct rb_node *node)
210 {
211 	const u64 mnt_ns_id = *(u64 *)key;
212 	const struct mnt_namespace *ns = node_to_mnt_ns(node);
213 
214 	if (mnt_ns_id < ns->seq)
215 		return -1;
216 	if (mnt_ns_id > ns->seq)
217 		return 1;
218 	return 0;
219 }
220 
221 /*
222  * Lookup a mount namespace by id and take a passive reference count. Taking a
223  * passive reference means the mount namespace can be emptied if e.g., the last
224  * task holding an active reference exits. To access the mounts of the
225  * namespace the @namespace_sem must first be acquired. If the namespace has
226  * already shut down before acquiring @namespace_sem, {list,stat}mount() will
227  * see that the mount rbtree of the namespace is empty.
228  *
229  * Note the lookup is lockless protected by a sequence counter. We only
230  * need to guard against false negatives as false positives aren't
231  * possible. So if we didn't find a mount namespace and the sequence
232  * counter has changed we need to retry. If the sequence counter is
233  * still the same we know the search actually failed.
234  */
235 static struct mnt_namespace *lookup_mnt_ns(u64 mnt_ns_id)
236 {
237 	struct mnt_namespace *ns;
238 	struct rb_node *node;
239 	unsigned int seq;
240 
241 	guard(rcu)();
242 	do {
243 		seq = read_seqbegin(&mnt_ns_tree_lock);
244 		node = rb_find_rcu(&mnt_ns_id, &mnt_ns_tree, mnt_ns_find);
245 		if (node)
246 			break;
247 	} while (read_seqretry(&mnt_ns_tree_lock, seq));
248 
249 	if (!node)
250 		return NULL;
251 
252 	/*
253 	 * The last reference count is put with RCU delay so we can
254 	 * unconditonally acquire a reference here.
255 	 */
256 	ns = node_to_mnt_ns(node);
257 	refcount_inc(&ns->passive);
258 	return ns;
259 }
260 
261 static inline void lock_mount_hash(void)
262 {
263 	write_seqlock(&mount_lock);
264 }
265 
266 static inline void unlock_mount_hash(void)
267 {
268 	write_sequnlock(&mount_lock);
269 }
270 
271 static inline struct hlist_head *m_hash(struct vfsmount *mnt, struct dentry *dentry)
272 {
273 	unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
274 	tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
275 	tmp = tmp + (tmp >> m_hash_shift);
276 	return &mount_hashtable[tmp & m_hash_mask];
277 }
278 
279 static inline struct hlist_head *mp_hash(struct dentry *dentry)
280 {
281 	unsigned long tmp = ((unsigned long)dentry / L1_CACHE_BYTES);
282 	tmp = tmp + (tmp >> mp_hash_shift);
283 	return &mountpoint_hashtable[tmp & mp_hash_mask];
284 }
285 
286 static int mnt_alloc_id(struct mount *mnt)
287 {
288 	int res;
289 
290 	xa_lock(&mnt_id_xa);
291 	res = __xa_alloc(&mnt_id_xa, &mnt->mnt_id, mnt, XA_LIMIT(1, INT_MAX), GFP_KERNEL);
292 	if (!res)
293 		mnt->mnt_id_unique = ++mnt_id_ctr;
294 	xa_unlock(&mnt_id_xa);
295 	return res;
296 }
297 
298 static void mnt_free_id(struct mount *mnt)
299 {
300 	xa_erase(&mnt_id_xa, mnt->mnt_id);
301 }
302 
303 /*
304  * Allocate a new peer group ID
305  */
306 static int mnt_alloc_group_id(struct mount *mnt)
307 {
308 	int res = ida_alloc_min(&mnt_group_ida, 1, GFP_KERNEL);
309 
310 	if (res < 0)
311 		return res;
312 	mnt->mnt_group_id = res;
313 	return 0;
314 }
315 
316 /*
317  * Release a peer group ID
318  */
319 void mnt_release_group_id(struct mount *mnt)
320 {
321 	ida_free(&mnt_group_ida, mnt->mnt_group_id);
322 	mnt->mnt_group_id = 0;
323 }
324 
325 /*
326  * vfsmount lock must be held for read
327  */
328 static inline void mnt_add_count(struct mount *mnt, int n)
329 {
330 #ifdef CONFIG_SMP
331 	this_cpu_add(mnt->mnt_pcp->mnt_count, n);
332 #else
333 	preempt_disable();
334 	mnt->mnt_count += n;
335 	preempt_enable();
336 #endif
337 }
338 
339 /*
340  * vfsmount lock must be held for write
341  */
342 int mnt_get_count(struct mount *mnt)
343 {
344 #ifdef CONFIG_SMP
345 	int count = 0;
346 	int cpu;
347 
348 	for_each_possible_cpu(cpu) {
349 		count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
350 	}
351 
352 	return count;
353 #else
354 	return mnt->mnt_count;
355 #endif
356 }
357 
358 static struct mount *alloc_vfsmnt(const char *name)
359 {
360 	struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
361 	if (mnt) {
362 		int err;
363 
364 		err = mnt_alloc_id(mnt);
365 		if (err)
366 			goto out_free_cache;
367 
368 		if (name)
369 			mnt->mnt_devname = kstrdup_const(name,
370 							 GFP_KERNEL_ACCOUNT);
371 		else
372 			mnt->mnt_devname = "none";
373 		if (!mnt->mnt_devname)
374 			goto out_free_id;
375 
376 #ifdef CONFIG_SMP
377 		mnt->mnt_pcp = alloc_percpu(struct mnt_pcp);
378 		if (!mnt->mnt_pcp)
379 			goto out_free_devname;
380 
381 		this_cpu_add(mnt->mnt_pcp->mnt_count, 1);
382 #else
383 		mnt->mnt_count = 1;
384 		mnt->mnt_writers = 0;
385 #endif
386 
387 		INIT_HLIST_NODE(&mnt->mnt_hash);
388 		INIT_LIST_HEAD(&mnt->mnt_child);
389 		INIT_LIST_HEAD(&mnt->mnt_mounts);
390 		INIT_LIST_HEAD(&mnt->mnt_list);
391 		INIT_LIST_HEAD(&mnt->mnt_expire);
392 		INIT_LIST_HEAD(&mnt->mnt_share);
393 		INIT_HLIST_HEAD(&mnt->mnt_slave_list);
394 		INIT_HLIST_NODE(&mnt->mnt_slave);
395 		INIT_HLIST_NODE(&mnt->mnt_mp_list);
396 		INIT_HLIST_HEAD(&mnt->mnt_stuck_children);
397 		RB_CLEAR_NODE(&mnt->mnt_node);
398 		mnt->mnt.mnt_idmap = &nop_mnt_idmap;
399 	}
400 	return mnt;
401 
402 #ifdef CONFIG_SMP
403 out_free_devname:
404 	kfree_const(mnt->mnt_devname);
405 #endif
406 out_free_id:
407 	mnt_free_id(mnt);
408 out_free_cache:
409 	kmem_cache_free(mnt_cache, mnt);
410 	return NULL;
411 }
412 
413 /*
414  * Most r/o checks on a fs are for operations that take
415  * discrete amounts of time, like a write() or unlink().
416  * We must keep track of when those operations start
417  * (for permission checks) and when they end, so that
418  * we can determine when writes are able to occur to
419  * a filesystem.
420  */
421 /*
422  * __mnt_is_readonly: check whether a mount is read-only
423  * @mnt: the mount to check for its write status
424  *
425  * This shouldn't be used directly ouside of the VFS.
426  * It does not guarantee that the filesystem will stay
427  * r/w, just that it is right *now*.  This can not and
428  * should not be used in place of IS_RDONLY(inode).
429  * mnt_want/drop_write() will _keep_ the filesystem
430  * r/w.
431  */
432 bool __mnt_is_readonly(struct vfsmount *mnt)
433 {
434 	return (mnt->mnt_flags & MNT_READONLY) || sb_rdonly(mnt->mnt_sb);
435 }
436 EXPORT_SYMBOL_GPL(__mnt_is_readonly);
437 
438 static inline void mnt_inc_writers(struct mount *mnt)
439 {
440 #ifdef CONFIG_SMP
441 	this_cpu_inc(mnt->mnt_pcp->mnt_writers);
442 #else
443 	mnt->mnt_writers++;
444 #endif
445 }
446 
447 static inline void mnt_dec_writers(struct mount *mnt)
448 {
449 #ifdef CONFIG_SMP
450 	this_cpu_dec(mnt->mnt_pcp->mnt_writers);
451 #else
452 	mnt->mnt_writers--;
453 #endif
454 }
455 
456 static unsigned int mnt_get_writers(struct mount *mnt)
457 {
458 #ifdef CONFIG_SMP
459 	unsigned int count = 0;
460 	int cpu;
461 
462 	for_each_possible_cpu(cpu) {
463 		count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers;
464 	}
465 
466 	return count;
467 #else
468 	return mnt->mnt_writers;
469 #endif
470 }
471 
472 static int mnt_is_readonly(struct vfsmount *mnt)
473 {
474 	if (READ_ONCE(mnt->mnt_sb->s_readonly_remount))
475 		return 1;
476 	/*
477 	 * The barrier pairs with the barrier in sb_start_ro_state_change()
478 	 * making sure if we don't see s_readonly_remount set yet, we also will
479 	 * not see any superblock / mount flag changes done by remount.
480 	 * It also pairs with the barrier in sb_end_ro_state_change()
481 	 * assuring that if we see s_readonly_remount already cleared, we will
482 	 * see the values of superblock / mount flags updated by remount.
483 	 */
484 	smp_rmb();
485 	return __mnt_is_readonly(mnt);
486 }
487 
488 /*
489  * Most r/o & frozen checks on a fs are for operations that take discrete
490  * amounts of time, like a write() or unlink().  We must keep track of when
491  * those operations start (for permission checks) and when they end, so that we
492  * can determine when writes are able to occur to a filesystem.
493  */
494 /**
495  * mnt_get_write_access - get write access to a mount without freeze protection
496  * @m: the mount on which to take a write
497  *
498  * This tells the low-level filesystem that a write is about to be performed to
499  * it, and makes sure that writes are allowed (mnt it read-write) before
500  * returning success. This operation does not protect against filesystem being
501  * frozen. When the write operation is finished, mnt_put_write_access() must be
502  * called. This is effectively a refcount.
503  */
504 int mnt_get_write_access(struct vfsmount *m)
505 {
506 	struct mount *mnt = real_mount(m);
507 	int ret = 0;
508 
509 	preempt_disable();
510 	mnt_inc_writers(mnt);
511 	/*
512 	 * The store to mnt_inc_writers must be visible before we pass
513 	 * MNT_WRITE_HOLD loop below, so that the slowpath can see our
514 	 * incremented count after it has set MNT_WRITE_HOLD.
515 	 */
516 	smp_mb();
517 	might_lock(&mount_lock.lock);
518 	while (READ_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD) {
519 		if (!IS_ENABLED(CONFIG_PREEMPT_RT)) {
520 			cpu_relax();
521 		} else {
522 			/*
523 			 * This prevents priority inversion, if the task
524 			 * setting MNT_WRITE_HOLD got preempted on a remote
525 			 * CPU, and it prevents life lock if the task setting
526 			 * MNT_WRITE_HOLD has a lower priority and is bound to
527 			 * the same CPU as the task that is spinning here.
528 			 */
529 			preempt_enable();
530 			lock_mount_hash();
531 			unlock_mount_hash();
532 			preempt_disable();
533 		}
534 	}
535 	/*
536 	 * The barrier pairs with the barrier sb_start_ro_state_change() making
537 	 * sure that if we see MNT_WRITE_HOLD cleared, we will also see
538 	 * s_readonly_remount set (or even SB_RDONLY / MNT_READONLY flags) in
539 	 * mnt_is_readonly() and bail in case we are racing with remount
540 	 * read-only.
541 	 */
542 	smp_rmb();
543 	if (mnt_is_readonly(m)) {
544 		mnt_dec_writers(mnt);
545 		ret = -EROFS;
546 	}
547 	preempt_enable();
548 
549 	return ret;
550 }
551 EXPORT_SYMBOL_GPL(mnt_get_write_access);
552 
553 /**
554  * mnt_want_write - get write access to a mount
555  * @m: the mount on which to take a write
556  *
557  * This tells the low-level filesystem that a write is about to be performed to
558  * it, and makes sure that writes are allowed (mount is read-write, filesystem
559  * is not frozen) before returning success.  When the write operation is
560  * finished, mnt_drop_write() must be called.  This is effectively a refcount.
561  */
562 int mnt_want_write(struct vfsmount *m)
563 {
564 	int ret;
565 
566 	sb_start_write(m->mnt_sb);
567 	ret = mnt_get_write_access(m);
568 	if (ret)
569 		sb_end_write(m->mnt_sb);
570 	return ret;
571 }
572 EXPORT_SYMBOL_GPL(mnt_want_write);
573 
574 /**
575  * mnt_get_write_access_file - get write access to a file's mount
576  * @file: the file who's mount on which to take a write
577  *
578  * This is like mnt_get_write_access, but if @file is already open for write it
579  * skips incrementing mnt_writers (since the open file already has a reference)
580  * and instead only does the check for emergency r/o remounts.  This must be
581  * paired with mnt_put_write_access_file.
582  */
583 int mnt_get_write_access_file(struct file *file)
584 {
585 	if (file->f_mode & FMODE_WRITER) {
586 		/*
587 		 * Superblock may have become readonly while there are still
588 		 * writable fd's, e.g. due to a fs error with errors=remount-ro
589 		 */
590 		if (__mnt_is_readonly(file->f_path.mnt))
591 			return -EROFS;
592 		return 0;
593 	}
594 	return mnt_get_write_access(file->f_path.mnt);
595 }
596 
597 /**
598  * mnt_want_write_file - get write access to a file's mount
599  * @file: the file who's mount on which to take a write
600  *
601  * This is like mnt_want_write, but if the file is already open for writing it
602  * skips incrementing mnt_writers (since the open file already has a reference)
603  * and instead only does the freeze protection and the check for emergency r/o
604  * remounts.  This must be paired with mnt_drop_write_file.
605  */
606 int mnt_want_write_file(struct file *file)
607 {
608 	int ret;
609 
610 	sb_start_write(file_inode(file)->i_sb);
611 	ret = mnt_get_write_access_file(file);
612 	if (ret)
613 		sb_end_write(file_inode(file)->i_sb);
614 	return ret;
615 }
616 EXPORT_SYMBOL_GPL(mnt_want_write_file);
617 
618 /**
619  * mnt_put_write_access - give up write access to a mount
620  * @mnt: the mount on which to give up write access
621  *
622  * Tells the low-level filesystem that we are done
623  * performing writes to it.  Must be matched with
624  * mnt_get_write_access() call above.
625  */
626 void mnt_put_write_access(struct vfsmount *mnt)
627 {
628 	preempt_disable();
629 	mnt_dec_writers(real_mount(mnt));
630 	preempt_enable();
631 }
632 EXPORT_SYMBOL_GPL(mnt_put_write_access);
633 
634 /**
635  * mnt_drop_write - give up write access to a mount
636  * @mnt: the mount on which to give up write access
637  *
638  * Tells the low-level filesystem that we are done performing writes to it and
639  * also allows filesystem to be frozen again.  Must be matched with
640  * mnt_want_write() call above.
641  */
642 void mnt_drop_write(struct vfsmount *mnt)
643 {
644 	mnt_put_write_access(mnt);
645 	sb_end_write(mnt->mnt_sb);
646 }
647 EXPORT_SYMBOL_GPL(mnt_drop_write);
648 
649 void mnt_put_write_access_file(struct file *file)
650 {
651 	if (!(file->f_mode & FMODE_WRITER))
652 		mnt_put_write_access(file->f_path.mnt);
653 }
654 
655 void mnt_drop_write_file(struct file *file)
656 {
657 	mnt_put_write_access_file(file);
658 	sb_end_write(file_inode(file)->i_sb);
659 }
660 EXPORT_SYMBOL(mnt_drop_write_file);
661 
662 /**
663  * mnt_hold_writers - prevent write access to the given mount
664  * @mnt: mnt to prevent write access to
665  *
666  * Prevents write access to @mnt if there are no active writers for @mnt.
667  * This function needs to be called and return successfully before changing
668  * properties of @mnt that need to remain stable for callers with write access
669  * to @mnt.
670  *
671  * After this functions has been called successfully callers must pair it with
672  * a call to mnt_unhold_writers() in order to stop preventing write access to
673  * @mnt.
674  *
675  * Context: This function expects lock_mount_hash() to be held serializing
676  *          setting MNT_WRITE_HOLD.
677  * Return: On success 0 is returned.
678  *	   On error, -EBUSY is returned.
679  */
680 static inline int mnt_hold_writers(struct mount *mnt)
681 {
682 	mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
683 	/*
684 	 * After storing MNT_WRITE_HOLD, we'll read the counters. This store
685 	 * should be visible before we do.
686 	 */
687 	smp_mb();
688 
689 	/*
690 	 * With writers on hold, if this value is zero, then there are
691 	 * definitely no active writers (although held writers may subsequently
692 	 * increment the count, they'll have to wait, and decrement it after
693 	 * seeing MNT_READONLY).
694 	 *
695 	 * It is OK to have counter incremented on one CPU and decremented on
696 	 * another: the sum will add up correctly. The danger would be when we
697 	 * sum up each counter, if we read a counter before it is incremented,
698 	 * but then read another CPU's count which it has been subsequently
699 	 * decremented from -- we would see more decrements than we should.
700 	 * MNT_WRITE_HOLD protects against this scenario, because
701 	 * mnt_want_write first increments count, then smp_mb, then spins on
702 	 * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
703 	 * we're counting up here.
704 	 */
705 	if (mnt_get_writers(mnt) > 0)
706 		return -EBUSY;
707 
708 	return 0;
709 }
710 
711 /**
712  * mnt_unhold_writers - stop preventing write access to the given mount
713  * @mnt: mnt to stop preventing write access to
714  *
715  * Stop preventing write access to @mnt allowing callers to gain write access
716  * to @mnt again.
717  *
718  * This function can only be called after a successful call to
719  * mnt_hold_writers().
720  *
721  * Context: This function expects lock_mount_hash() to be held.
722  */
723 static inline void mnt_unhold_writers(struct mount *mnt)
724 {
725 	/*
726 	 * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
727 	 * that become unheld will see MNT_READONLY.
728 	 */
729 	smp_wmb();
730 	mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
731 }
732 
733 static int mnt_make_readonly(struct mount *mnt)
734 {
735 	int ret;
736 
737 	ret = mnt_hold_writers(mnt);
738 	if (!ret)
739 		mnt->mnt.mnt_flags |= MNT_READONLY;
740 	mnt_unhold_writers(mnt);
741 	return ret;
742 }
743 
744 int sb_prepare_remount_readonly(struct super_block *sb)
745 {
746 	struct mount *mnt;
747 	int err = 0;
748 
749 	/* Racy optimization.  Recheck the counter under MNT_WRITE_HOLD */
750 	if (atomic_long_read(&sb->s_remove_count))
751 		return -EBUSY;
752 
753 	lock_mount_hash();
754 	list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
755 		if (!(mnt->mnt.mnt_flags & MNT_READONLY)) {
756 			err = mnt_hold_writers(mnt);
757 			if (err)
758 				break;
759 		}
760 	}
761 	if (!err && atomic_long_read(&sb->s_remove_count))
762 		err = -EBUSY;
763 
764 	if (!err)
765 		sb_start_ro_state_change(sb);
766 	list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
767 		if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD)
768 			mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
769 	}
770 	unlock_mount_hash();
771 
772 	return err;
773 }
774 
775 static void free_vfsmnt(struct mount *mnt)
776 {
777 	mnt_idmap_put(mnt_idmap(&mnt->mnt));
778 	kfree_const(mnt->mnt_devname);
779 #ifdef CONFIG_SMP
780 	free_percpu(mnt->mnt_pcp);
781 #endif
782 	kmem_cache_free(mnt_cache, mnt);
783 }
784 
785 static void delayed_free_vfsmnt(struct rcu_head *head)
786 {
787 	free_vfsmnt(container_of(head, struct mount, mnt_rcu));
788 }
789 
790 /* call under rcu_read_lock */
791 int __legitimize_mnt(struct vfsmount *bastard, unsigned seq)
792 {
793 	struct mount *mnt;
794 	if (read_seqretry(&mount_lock, seq))
795 		return 1;
796 	if (bastard == NULL)
797 		return 0;
798 	mnt = real_mount(bastard);
799 	mnt_add_count(mnt, 1);
800 	smp_mb();		// see mntput_no_expire() and do_umount()
801 	if (likely(!read_seqretry(&mount_lock, seq)))
802 		return 0;
803 	lock_mount_hash();
804 	if (unlikely(bastard->mnt_flags & (MNT_SYNC_UMOUNT | MNT_DOOMED))) {
805 		mnt_add_count(mnt, -1);
806 		unlock_mount_hash();
807 		return 1;
808 	}
809 	unlock_mount_hash();
810 	/* caller will mntput() */
811 	return -1;
812 }
813 
814 /* call under rcu_read_lock */
815 static bool legitimize_mnt(struct vfsmount *bastard, unsigned seq)
816 {
817 	int res = __legitimize_mnt(bastard, seq);
818 	if (likely(!res))
819 		return true;
820 	if (unlikely(res < 0)) {
821 		rcu_read_unlock();
822 		mntput(bastard);
823 		rcu_read_lock();
824 	}
825 	return false;
826 }
827 
828 /**
829  * __lookup_mnt - find first child mount
830  * @mnt:	parent mount
831  * @dentry:	mountpoint
832  *
833  * If @mnt has a child mount @c mounted @dentry find and return it.
834  *
835  * Note that the child mount @c need not be unique. There are cases
836  * where shadow mounts are created. For example, during mount
837  * propagation when a source mount @mnt whose root got overmounted by a
838  * mount @o after path lookup but before @namespace_sem could be
839  * acquired gets copied and propagated. So @mnt gets copied including
840  * @o. When @mnt is propagated to a destination mount @d that already
841  * has another mount @n mounted at the same mountpoint then the source
842  * mount @mnt will be tucked beneath @n, i.e., @n will be mounted on
843  * @mnt and @mnt mounted on @d. Now both @n and @o are mounted at @mnt
844  * on @dentry.
845  *
846  * Return: The first child of @mnt mounted @dentry or NULL.
847  */
848 struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
849 {
850 	struct hlist_head *head = m_hash(mnt, dentry);
851 	struct mount *p;
852 
853 	hlist_for_each_entry_rcu(p, head, mnt_hash)
854 		if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry)
855 			return p;
856 	return NULL;
857 }
858 
859 /*
860  * lookup_mnt - Return the first child mount mounted at path
861  *
862  * "First" means first mounted chronologically.  If you create the
863  * following mounts:
864  *
865  * mount /dev/sda1 /mnt
866  * mount /dev/sda2 /mnt
867  * mount /dev/sda3 /mnt
868  *
869  * Then lookup_mnt() on the base /mnt dentry in the root mount will
870  * return successively the root dentry and vfsmount of /dev/sda1, then
871  * /dev/sda2, then /dev/sda3, then NULL.
872  *
873  * lookup_mnt takes a reference to the found vfsmount.
874  */
875 struct vfsmount *lookup_mnt(const struct path *path)
876 {
877 	struct mount *child_mnt;
878 	struct vfsmount *m;
879 	unsigned seq;
880 
881 	rcu_read_lock();
882 	do {
883 		seq = read_seqbegin(&mount_lock);
884 		child_mnt = __lookup_mnt(path->mnt, path->dentry);
885 		m = child_mnt ? &child_mnt->mnt : NULL;
886 	} while (!legitimize_mnt(m, seq));
887 	rcu_read_unlock();
888 	return m;
889 }
890 
891 /*
892  * __is_local_mountpoint - Test to see if dentry is a mountpoint in the
893  *                         current mount namespace.
894  *
895  * The common case is dentries are not mountpoints at all and that
896  * test is handled inline.  For the slow case when we are actually
897  * dealing with a mountpoint of some kind, walk through all of the
898  * mounts in the current mount namespace and test to see if the dentry
899  * is a mountpoint.
900  *
901  * The mount_hashtable is not usable in the context because we
902  * need to identify all mounts that may be in the current mount
903  * namespace not just a mount that happens to have some specified
904  * parent mount.
905  */
906 bool __is_local_mountpoint(const struct dentry *dentry)
907 {
908 	struct mnt_namespace *ns = current->nsproxy->mnt_ns;
909 	struct mount *mnt, *n;
910 	bool is_covered = false;
911 
912 	down_read(&namespace_sem);
913 	rbtree_postorder_for_each_entry_safe(mnt, n, &ns->mounts, mnt_node) {
914 		is_covered = (mnt->mnt_mountpoint == dentry);
915 		if (is_covered)
916 			break;
917 	}
918 	up_read(&namespace_sem);
919 
920 	return is_covered;
921 }
922 
923 struct pinned_mountpoint {
924 	struct hlist_node node;
925 	struct mountpoint *mp;
926 };
927 
928 static bool lookup_mountpoint(struct dentry *dentry, struct pinned_mountpoint *m)
929 {
930 	struct hlist_head *chain = mp_hash(dentry);
931 	struct mountpoint *mp;
932 
933 	hlist_for_each_entry(mp, chain, m_hash) {
934 		if (mp->m_dentry == dentry) {
935 			hlist_add_head(&m->node, &mp->m_list);
936 			m->mp = mp;
937 			return true;
938 		}
939 	}
940 	return false;
941 }
942 
943 static int get_mountpoint(struct dentry *dentry, struct pinned_mountpoint *m)
944 {
945 	struct mountpoint *mp __free(kfree) = NULL;
946 	bool found;
947 	int ret;
948 
949 	if (d_mountpoint(dentry)) {
950 		/* might be worth a WARN_ON() */
951 		if (d_unlinked(dentry))
952 			return -ENOENT;
953 mountpoint:
954 		read_seqlock_excl(&mount_lock);
955 		found = lookup_mountpoint(dentry, m);
956 		read_sequnlock_excl(&mount_lock);
957 		if (found)
958 			return 0;
959 	}
960 
961 	if (!mp)
962 		mp = kmalloc(sizeof(struct mountpoint), GFP_KERNEL);
963 	if (!mp)
964 		return -ENOMEM;
965 
966 	/* Exactly one processes may set d_mounted */
967 	ret = d_set_mounted(dentry);
968 
969 	/* Someone else set d_mounted? */
970 	if (ret == -EBUSY)
971 		goto mountpoint;
972 
973 	/* The dentry is not available as a mountpoint? */
974 	if (ret)
975 		return ret;
976 
977 	/* Add the new mountpoint to the hash table */
978 	read_seqlock_excl(&mount_lock);
979 	mp->m_dentry = dget(dentry);
980 	hlist_add_head(&mp->m_hash, mp_hash(dentry));
981 	INIT_HLIST_HEAD(&mp->m_list);
982 	hlist_add_head(&m->node, &mp->m_list);
983 	m->mp = no_free_ptr(mp);
984 	read_sequnlock_excl(&mount_lock);
985 	return 0;
986 }
987 
988 /*
989  * vfsmount lock must be held.  Additionally, the caller is responsible
990  * for serializing calls for given disposal list.
991  */
992 static void maybe_free_mountpoint(struct mountpoint *mp, struct list_head *list)
993 {
994 	if (hlist_empty(&mp->m_list)) {
995 		struct dentry *dentry = mp->m_dentry;
996 		spin_lock(&dentry->d_lock);
997 		dentry->d_flags &= ~DCACHE_MOUNTED;
998 		spin_unlock(&dentry->d_lock);
999 		dput_to_list(dentry, list);
1000 		hlist_del(&mp->m_hash);
1001 		kfree(mp);
1002 	}
1003 }
1004 
1005 /*
1006  * locks: mount_lock [read_seqlock_excl], namespace_sem [excl]
1007  */
1008 static void unpin_mountpoint(struct pinned_mountpoint *m)
1009 {
1010 	if (m->mp) {
1011 		hlist_del(&m->node);
1012 		maybe_free_mountpoint(m->mp, &ex_mountpoints);
1013 	}
1014 }
1015 
1016 static inline int check_mnt(struct mount *mnt)
1017 {
1018 	return mnt->mnt_ns == current->nsproxy->mnt_ns;
1019 }
1020 
1021 static inline bool check_anonymous_mnt(struct mount *mnt)
1022 {
1023 	u64 seq;
1024 
1025 	if (!is_anon_ns(mnt->mnt_ns))
1026 		return false;
1027 
1028 	seq = mnt->mnt_ns->seq_origin;
1029 	return !seq || (seq == current->nsproxy->mnt_ns->seq);
1030 }
1031 
1032 /*
1033  * vfsmount lock must be held for write
1034  */
1035 static void touch_mnt_namespace(struct mnt_namespace *ns)
1036 {
1037 	if (ns) {
1038 		ns->event = ++event;
1039 		wake_up_interruptible(&ns->poll);
1040 	}
1041 }
1042 
1043 /*
1044  * vfsmount lock must be held for write
1045  */
1046 static void __touch_mnt_namespace(struct mnt_namespace *ns)
1047 {
1048 	if (ns && ns->event != event) {
1049 		ns->event = event;
1050 		wake_up_interruptible(&ns->poll);
1051 	}
1052 }
1053 
1054 /*
1055  * locks: mount_lock[write_seqlock]
1056  */
1057 static void __umount_mnt(struct mount *mnt, struct list_head *shrink_list)
1058 {
1059 	struct mountpoint *mp;
1060 	struct mount *parent = mnt->mnt_parent;
1061 	if (unlikely(parent->overmount == mnt))
1062 		parent->overmount = NULL;
1063 	mnt->mnt_parent = mnt;
1064 	mnt->mnt_mountpoint = mnt->mnt.mnt_root;
1065 	list_del_init(&mnt->mnt_child);
1066 	hlist_del_init_rcu(&mnt->mnt_hash);
1067 	hlist_del_init(&mnt->mnt_mp_list);
1068 	mp = mnt->mnt_mp;
1069 	mnt->mnt_mp = NULL;
1070 	maybe_free_mountpoint(mp, shrink_list);
1071 }
1072 
1073 /*
1074  * locks: mount_lock[write_seqlock], namespace_sem[excl] (for ex_mountpoints)
1075  */
1076 static void umount_mnt(struct mount *mnt)
1077 {
1078 	__umount_mnt(mnt, &ex_mountpoints);
1079 }
1080 
1081 /*
1082  * vfsmount lock must be held for write
1083  */
1084 void mnt_set_mountpoint(struct mount *mnt,
1085 			struct mountpoint *mp,
1086 			struct mount *child_mnt)
1087 {
1088 	child_mnt->mnt_mountpoint = mp->m_dentry;
1089 	child_mnt->mnt_parent = mnt;
1090 	child_mnt->mnt_mp = mp;
1091 	hlist_add_head(&child_mnt->mnt_mp_list, &mp->m_list);
1092 }
1093 
1094 static void make_visible(struct mount *mnt)
1095 {
1096 	struct mount *parent = mnt->mnt_parent;
1097 	if (unlikely(mnt->mnt_mountpoint == parent->mnt.mnt_root))
1098 		parent->overmount = mnt;
1099 	hlist_add_head_rcu(&mnt->mnt_hash,
1100 			   m_hash(&parent->mnt, mnt->mnt_mountpoint));
1101 	list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
1102 }
1103 
1104 /**
1105  * attach_mnt - mount a mount, attach to @mount_hashtable and parent's
1106  *              list of child mounts
1107  * @parent:  the parent
1108  * @mnt:     the new mount
1109  * @mp:      the new mountpoint
1110  *
1111  * Mount @mnt at @mp on @parent. Then attach @mnt
1112  * to @parent's child mount list and to @mount_hashtable.
1113  *
1114  * Note, when make_visible() is called @mnt->mnt_parent already points
1115  * to the correct parent.
1116  *
1117  * Context: This function expects namespace_lock() and lock_mount_hash()
1118  *          to have been acquired in that order.
1119  */
1120 static void attach_mnt(struct mount *mnt, struct mount *parent,
1121 		       struct mountpoint *mp)
1122 {
1123 	mnt_set_mountpoint(parent, mp, mnt);
1124 	make_visible(mnt);
1125 }
1126 
1127 void mnt_change_mountpoint(struct mount *parent, struct mountpoint *mp, struct mount *mnt)
1128 {
1129 	struct mountpoint *old_mp = mnt->mnt_mp;
1130 
1131 	list_del_init(&mnt->mnt_child);
1132 	hlist_del_init(&mnt->mnt_mp_list);
1133 	hlist_del_init_rcu(&mnt->mnt_hash);
1134 
1135 	attach_mnt(mnt, parent, mp);
1136 
1137 	maybe_free_mountpoint(old_mp, &ex_mountpoints);
1138 }
1139 
1140 static inline struct mount *node_to_mount(struct rb_node *node)
1141 {
1142 	return node ? rb_entry(node, struct mount, mnt_node) : NULL;
1143 }
1144 
1145 static void mnt_add_to_ns(struct mnt_namespace *ns, struct mount *mnt)
1146 {
1147 	struct rb_node **link = &ns->mounts.rb_node;
1148 	struct rb_node *parent = NULL;
1149 	bool mnt_first_node = true, mnt_last_node = true;
1150 
1151 	WARN_ON(mnt_ns_attached(mnt));
1152 	mnt->mnt_ns = ns;
1153 	while (*link) {
1154 		parent = *link;
1155 		if (mnt->mnt_id_unique < node_to_mount(parent)->mnt_id_unique) {
1156 			link = &parent->rb_left;
1157 			mnt_last_node = false;
1158 		} else {
1159 			link = &parent->rb_right;
1160 			mnt_first_node = false;
1161 		}
1162 	}
1163 
1164 	if (mnt_last_node)
1165 		ns->mnt_last_node = &mnt->mnt_node;
1166 	if (mnt_first_node)
1167 		ns->mnt_first_node = &mnt->mnt_node;
1168 	rb_link_node(&mnt->mnt_node, parent, link);
1169 	rb_insert_color(&mnt->mnt_node, &ns->mounts);
1170 
1171 	mnt_notify_add(mnt);
1172 }
1173 
1174 static struct mount *next_mnt(struct mount *p, struct mount *root)
1175 {
1176 	struct list_head *next = p->mnt_mounts.next;
1177 	if (next == &p->mnt_mounts) {
1178 		while (1) {
1179 			if (p == root)
1180 				return NULL;
1181 			next = p->mnt_child.next;
1182 			if (next != &p->mnt_parent->mnt_mounts)
1183 				break;
1184 			p = p->mnt_parent;
1185 		}
1186 	}
1187 	return list_entry(next, struct mount, mnt_child);
1188 }
1189 
1190 static struct mount *skip_mnt_tree(struct mount *p)
1191 {
1192 	struct list_head *prev = p->mnt_mounts.prev;
1193 	while (prev != &p->mnt_mounts) {
1194 		p = list_entry(prev, struct mount, mnt_child);
1195 		prev = p->mnt_mounts.prev;
1196 	}
1197 	return p;
1198 }
1199 
1200 /*
1201  * vfsmount lock must be held for write
1202  */
1203 static void commit_tree(struct mount *mnt)
1204 {
1205 	struct mnt_namespace *n = mnt->mnt_parent->mnt_ns;
1206 
1207 	if (!mnt_ns_attached(mnt)) {
1208 		for (struct mount *m = mnt; m; m = next_mnt(m, mnt))
1209 			mnt_add_to_ns(n, m);
1210 		n->nr_mounts += n->pending_mounts;
1211 		n->pending_mounts = 0;
1212 	}
1213 
1214 	make_visible(mnt);
1215 	touch_mnt_namespace(n);
1216 }
1217 
1218 /**
1219  * vfs_create_mount - Create a mount for a configured superblock
1220  * @fc: The configuration context with the superblock attached
1221  *
1222  * Create a mount to an already configured superblock.  If necessary, the
1223  * caller should invoke vfs_get_tree() before calling this.
1224  *
1225  * Note that this does not attach the mount to anything.
1226  */
1227 struct vfsmount *vfs_create_mount(struct fs_context *fc)
1228 {
1229 	struct mount *mnt;
1230 
1231 	if (!fc->root)
1232 		return ERR_PTR(-EINVAL);
1233 
1234 	mnt = alloc_vfsmnt(fc->source);
1235 	if (!mnt)
1236 		return ERR_PTR(-ENOMEM);
1237 
1238 	if (fc->sb_flags & SB_KERNMOUNT)
1239 		mnt->mnt.mnt_flags = MNT_INTERNAL;
1240 
1241 	atomic_inc(&fc->root->d_sb->s_active);
1242 	mnt->mnt.mnt_sb		= fc->root->d_sb;
1243 	mnt->mnt.mnt_root	= dget(fc->root);
1244 	mnt->mnt_mountpoint	= mnt->mnt.mnt_root;
1245 	mnt->mnt_parent		= mnt;
1246 
1247 	lock_mount_hash();
1248 	list_add_tail(&mnt->mnt_instance, &mnt->mnt.mnt_sb->s_mounts);
1249 	unlock_mount_hash();
1250 	return &mnt->mnt;
1251 }
1252 EXPORT_SYMBOL(vfs_create_mount);
1253 
1254 struct vfsmount *fc_mount(struct fs_context *fc)
1255 {
1256 	int err = vfs_get_tree(fc);
1257 	if (!err) {
1258 		up_write(&fc->root->d_sb->s_umount);
1259 		return vfs_create_mount(fc);
1260 	}
1261 	return ERR_PTR(err);
1262 }
1263 EXPORT_SYMBOL(fc_mount);
1264 
1265 struct vfsmount *fc_mount_longterm(struct fs_context *fc)
1266 {
1267 	struct vfsmount *mnt = fc_mount(fc);
1268 	if (!IS_ERR(mnt))
1269 		real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL;
1270 	return mnt;
1271 }
1272 EXPORT_SYMBOL(fc_mount_longterm);
1273 
1274 struct vfsmount *vfs_kern_mount(struct file_system_type *type,
1275 				int flags, const char *name,
1276 				void *data)
1277 {
1278 	struct fs_context *fc;
1279 	struct vfsmount *mnt;
1280 	int ret = 0;
1281 
1282 	if (!type)
1283 		return ERR_PTR(-EINVAL);
1284 
1285 	fc = fs_context_for_mount(type, flags);
1286 	if (IS_ERR(fc))
1287 		return ERR_CAST(fc);
1288 
1289 	if (name)
1290 		ret = vfs_parse_fs_string(fc, "source",
1291 					  name, strlen(name));
1292 	if (!ret)
1293 		ret = parse_monolithic_mount_data(fc, data);
1294 	if (!ret)
1295 		mnt = fc_mount(fc);
1296 	else
1297 		mnt = ERR_PTR(ret);
1298 
1299 	put_fs_context(fc);
1300 	return mnt;
1301 }
1302 EXPORT_SYMBOL_GPL(vfs_kern_mount);
1303 
1304 static struct mount *clone_mnt(struct mount *old, struct dentry *root,
1305 					int flag)
1306 {
1307 	struct super_block *sb = old->mnt.mnt_sb;
1308 	struct mount *mnt;
1309 	int err;
1310 
1311 	mnt = alloc_vfsmnt(old->mnt_devname);
1312 	if (!mnt)
1313 		return ERR_PTR(-ENOMEM);
1314 
1315 	mnt->mnt.mnt_flags = READ_ONCE(old->mnt.mnt_flags) &
1316 			     ~MNT_INTERNAL_FLAGS;
1317 
1318 	if (flag & (CL_SLAVE | CL_PRIVATE))
1319 		mnt->mnt_group_id = 0; /* not a peer of original */
1320 	else
1321 		mnt->mnt_group_id = old->mnt_group_id;
1322 
1323 	if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
1324 		err = mnt_alloc_group_id(mnt);
1325 		if (err)
1326 			goto out_free;
1327 	}
1328 
1329 	if (mnt->mnt_group_id)
1330 		set_mnt_shared(mnt);
1331 
1332 	atomic_inc(&sb->s_active);
1333 	mnt->mnt.mnt_idmap = mnt_idmap_get(mnt_idmap(&old->mnt));
1334 
1335 	mnt->mnt.mnt_sb = sb;
1336 	mnt->mnt.mnt_root = dget(root);
1337 	mnt->mnt_mountpoint = mnt->mnt.mnt_root;
1338 	mnt->mnt_parent = mnt;
1339 	lock_mount_hash();
1340 	list_add_tail(&mnt->mnt_instance, &sb->s_mounts);
1341 	unlock_mount_hash();
1342 
1343 	if (flag & CL_PRIVATE)	// we are done with it
1344 		return mnt;
1345 
1346 	if (peers(mnt, old))
1347 		list_add(&mnt->mnt_share, &old->mnt_share);
1348 
1349 	if ((flag & CL_SLAVE) && old->mnt_group_id) {
1350 		hlist_add_head(&mnt->mnt_slave, &old->mnt_slave_list);
1351 		mnt->mnt_master = old;
1352 	} else if (IS_MNT_SLAVE(old)) {
1353 		hlist_add_behind(&mnt->mnt_slave, &old->mnt_slave);
1354 		mnt->mnt_master = old->mnt_master;
1355 	}
1356 	return mnt;
1357 
1358  out_free:
1359 	mnt_free_id(mnt);
1360 	free_vfsmnt(mnt);
1361 	return ERR_PTR(err);
1362 }
1363 
1364 static void cleanup_mnt(struct mount *mnt)
1365 {
1366 	struct hlist_node *p;
1367 	struct mount *m;
1368 	/*
1369 	 * The warning here probably indicates that somebody messed
1370 	 * up a mnt_want/drop_write() pair.  If this happens, the
1371 	 * filesystem was probably unable to make r/w->r/o transitions.
1372 	 * The locking used to deal with mnt_count decrement provides barriers,
1373 	 * so mnt_get_writers() below is safe.
1374 	 */
1375 	WARN_ON(mnt_get_writers(mnt));
1376 	if (unlikely(mnt->mnt_pins.first))
1377 		mnt_pin_kill(mnt);
1378 	hlist_for_each_entry_safe(m, p, &mnt->mnt_stuck_children, mnt_umount) {
1379 		hlist_del(&m->mnt_umount);
1380 		mntput(&m->mnt);
1381 	}
1382 	fsnotify_vfsmount_delete(&mnt->mnt);
1383 	dput(mnt->mnt.mnt_root);
1384 	deactivate_super(mnt->mnt.mnt_sb);
1385 	mnt_free_id(mnt);
1386 	call_rcu(&mnt->mnt_rcu, delayed_free_vfsmnt);
1387 }
1388 
1389 static void __cleanup_mnt(struct rcu_head *head)
1390 {
1391 	cleanup_mnt(container_of(head, struct mount, mnt_rcu));
1392 }
1393 
1394 static LLIST_HEAD(delayed_mntput_list);
1395 static void delayed_mntput(struct work_struct *unused)
1396 {
1397 	struct llist_node *node = llist_del_all(&delayed_mntput_list);
1398 	struct mount *m, *t;
1399 
1400 	llist_for_each_entry_safe(m, t, node, mnt_llist)
1401 		cleanup_mnt(m);
1402 }
1403 static DECLARE_DELAYED_WORK(delayed_mntput_work, delayed_mntput);
1404 
1405 static void mntput_no_expire(struct mount *mnt)
1406 {
1407 	LIST_HEAD(list);
1408 	int count;
1409 
1410 	rcu_read_lock();
1411 	if (likely(READ_ONCE(mnt->mnt_ns))) {
1412 		/*
1413 		 * Since we don't do lock_mount_hash() here,
1414 		 * ->mnt_ns can change under us.  However, if it's
1415 		 * non-NULL, then there's a reference that won't
1416 		 * be dropped until after an RCU delay done after
1417 		 * turning ->mnt_ns NULL.  So if we observe it
1418 		 * non-NULL under rcu_read_lock(), the reference
1419 		 * we are dropping is not the final one.
1420 		 */
1421 		mnt_add_count(mnt, -1);
1422 		rcu_read_unlock();
1423 		return;
1424 	}
1425 	lock_mount_hash();
1426 	/*
1427 	 * make sure that if __legitimize_mnt() has not seen us grab
1428 	 * mount_lock, we'll see their refcount increment here.
1429 	 */
1430 	smp_mb();
1431 	mnt_add_count(mnt, -1);
1432 	count = mnt_get_count(mnt);
1433 	if (count != 0) {
1434 		WARN_ON(count < 0);
1435 		rcu_read_unlock();
1436 		unlock_mount_hash();
1437 		return;
1438 	}
1439 	if (unlikely(mnt->mnt.mnt_flags & MNT_DOOMED)) {
1440 		rcu_read_unlock();
1441 		unlock_mount_hash();
1442 		return;
1443 	}
1444 	mnt->mnt.mnt_flags |= MNT_DOOMED;
1445 	rcu_read_unlock();
1446 
1447 	list_del(&mnt->mnt_instance);
1448 	if (unlikely(!list_empty(&mnt->mnt_expire)))
1449 		list_del(&mnt->mnt_expire);
1450 
1451 	if (unlikely(!list_empty(&mnt->mnt_mounts))) {
1452 		struct mount *p, *tmp;
1453 		list_for_each_entry_safe(p, tmp, &mnt->mnt_mounts,  mnt_child) {
1454 			__umount_mnt(p, &list);
1455 			hlist_add_head(&p->mnt_umount, &mnt->mnt_stuck_children);
1456 		}
1457 	}
1458 	unlock_mount_hash();
1459 	shrink_dentry_list(&list);
1460 
1461 	if (likely(!(mnt->mnt.mnt_flags & MNT_INTERNAL))) {
1462 		struct task_struct *task = current;
1463 		if (likely(!(task->flags & PF_KTHREAD))) {
1464 			init_task_work(&mnt->mnt_rcu, __cleanup_mnt);
1465 			if (!task_work_add(task, &mnt->mnt_rcu, TWA_RESUME))
1466 				return;
1467 		}
1468 		if (llist_add(&mnt->mnt_llist, &delayed_mntput_list))
1469 			schedule_delayed_work(&delayed_mntput_work, 1);
1470 		return;
1471 	}
1472 	cleanup_mnt(mnt);
1473 }
1474 
1475 void mntput(struct vfsmount *mnt)
1476 {
1477 	if (mnt) {
1478 		struct mount *m = real_mount(mnt);
1479 		/* avoid cacheline pingpong */
1480 		if (unlikely(m->mnt_expiry_mark))
1481 			WRITE_ONCE(m->mnt_expiry_mark, 0);
1482 		mntput_no_expire(m);
1483 	}
1484 }
1485 EXPORT_SYMBOL(mntput);
1486 
1487 struct vfsmount *mntget(struct vfsmount *mnt)
1488 {
1489 	if (mnt)
1490 		mnt_add_count(real_mount(mnt), 1);
1491 	return mnt;
1492 }
1493 EXPORT_SYMBOL(mntget);
1494 
1495 /*
1496  * Make a mount point inaccessible to new lookups.
1497  * Because there may still be current users, the caller MUST WAIT
1498  * for an RCU grace period before destroying the mount point.
1499  */
1500 void mnt_make_shortterm(struct vfsmount *mnt)
1501 {
1502 	if (mnt)
1503 		real_mount(mnt)->mnt_ns = NULL;
1504 }
1505 
1506 /**
1507  * path_is_mountpoint() - Check if path is a mount in the current namespace.
1508  * @path: path to check
1509  *
1510  *  d_mountpoint() can only be used reliably to establish if a dentry is
1511  *  not mounted in any namespace and that common case is handled inline.
1512  *  d_mountpoint() isn't aware of the possibility there may be multiple
1513  *  mounts using a given dentry in a different namespace. This function
1514  *  checks if the passed in path is a mountpoint rather than the dentry
1515  *  alone.
1516  */
1517 bool path_is_mountpoint(const struct path *path)
1518 {
1519 	unsigned seq;
1520 	bool res;
1521 
1522 	if (!d_mountpoint(path->dentry))
1523 		return false;
1524 
1525 	rcu_read_lock();
1526 	do {
1527 		seq = read_seqbegin(&mount_lock);
1528 		res = __path_is_mountpoint(path);
1529 	} while (read_seqretry(&mount_lock, seq));
1530 	rcu_read_unlock();
1531 
1532 	return res;
1533 }
1534 EXPORT_SYMBOL(path_is_mountpoint);
1535 
1536 struct vfsmount *mnt_clone_internal(const struct path *path)
1537 {
1538 	struct mount *p;
1539 	p = clone_mnt(real_mount(path->mnt), path->dentry, CL_PRIVATE);
1540 	if (IS_ERR(p))
1541 		return ERR_CAST(p);
1542 	p->mnt.mnt_flags |= MNT_INTERNAL;
1543 	return &p->mnt;
1544 }
1545 
1546 /*
1547  * Returns the mount which either has the specified mnt_id, or has the next
1548  * smallest id afer the specified one.
1549  */
1550 static struct mount *mnt_find_id_at(struct mnt_namespace *ns, u64 mnt_id)
1551 {
1552 	struct rb_node *node = ns->mounts.rb_node;
1553 	struct mount *ret = NULL;
1554 
1555 	while (node) {
1556 		struct mount *m = node_to_mount(node);
1557 
1558 		if (mnt_id <= m->mnt_id_unique) {
1559 			ret = node_to_mount(node);
1560 			if (mnt_id == m->mnt_id_unique)
1561 				break;
1562 			node = node->rb_left;
1563 		} else {
1564 			node = node->rb_right;
1565 		}
1566 	}
1567 	return ret;
1568 }
1569 
1570 /*
1571  * Returns the mount which either has the specified mnt_id, or has the next
1572  * greater id before the specified one.
1573  */
1574 static struct mount *mnt_find_id_at_reverse(struct mnt_namespace *ns, u64 mnt_id)
1575 {
1576 	struct rb_node *node = ns->mounts.rb_node;
1577 	struct mount *ret = NULL;
1578 
1579 	while (node) {
1580 		struct mount *m = node_to_mount(node);
1581 
1582 		if (mnt_id >= m->mnt_id_unique) {
1583 			ret = node_to_mount(node);
1584 			if (mnt_id == m->mnt_id_unique)
1585 				break;
1586 			node = node->rb_right;
1587 		} else {
1588 			node = node->rb_left;
1589 		}
1590 	}
1591 	return ret;
1592 }
1593 
1594 #ifdef CONFIG_PROC_FS
1595 
1596 /* iterator; we want it to have access to namespace_sem, thus here... */
1597 static void *m_start(struct seq_file *m, loff_t *pos)
1598 {
1599 	struct proc_mounts *p = m->private;
1600 
1601 	down_read(&namespace_sem);
1602 
1603 	return mnt_find_id_at(p->ns, *pos);
1604 }
1605 
1606 static void *m_next(struct seq_file *m, void *v, loff_t *pos)
1607 {
1608 	struct mount *next = NULL, *mnt = v;
1609 	struct rb_node *node = rb_next(&mnt->mnt_node);
1610 
1611 	++*pos;
1612 	if (node) {
1613 		next = node_to_mount(node);
1614 		*pos = next->mnt_id_unique;
1615 	}
1616 	return next;
1617 }
1618 
1619 static void m_stop(struct seq_file *m, void *v)
1620 {
1621 	up_read(&namespace_sem);
1622 }
1623 
1624 static int m_show(struct seq_file *m, void *v)
1625 {
1626 	struct proc_mounts *p = m->private;
1627 	struct mount *r = v;
1628 	return p->show(m, &r->mnt);
1629 }
1630 
1631 const struct seq_operations mounts_op = {
1632 	.start	= m_start,
1633 	.next	= m_next,
1634 	.stop	= m_stop,
1635 	.show	= m_show,
1636 };
1637 
1638 #endif  /* CONFIG_PROC_FS */
1639 
1640 /**
1641  * may_umount_tree - check if a mount tree is busy
1642  * @m: root of mount tree
1643  *
1644  * This is called to check if a tree of mounts has any
1645  * open files, pwds, chroots or sub mounts that are
1646  * busy.
1647  */
1648 int may_umount_tree(struct vfsmount *m)
1649 {
1650 	struct mount *mnt = real_mount(m);
1651 	bool busy = false;
1652 
1653 	/* write lock needed for mnt_get_count */
1654 	lock_mount_hash();
1655 	for (struct mount *p = mnt; p; p = next_mnt(p, mnt)) {
1656 		if (mnt_get_count(p) > (p == mnt ? 2 : 1)) {
1657 			busy = true;
1658 			break;
1659 		}
1660 	}
1661 	unlock_mount_hash();
1662 
1663 	return !busy;
1664 }
1665 
1666 EXPORT_SYMBOL(may_umount_tree);
1667 
1668 /**
1669  * may_umount - check if a mount point is busy
1670  * @mnt: root of mount
1671  *
1672  * This is called to check if a mount point has any
1673  * open files, pwds, chroots or sub mounts. If the
1674  * mount has sub mounts this will return busy
1675  * regardless of whether the sub mounts are busy.
1676  *
1677  * Doesn't take quota and stuff into account. IOW, in some cases it will
1678  * give false negatives. The main reason why it's here is that we need
1679  * a non-destructive way to look for easily umountable filesystems.
1680  */
1681 int may_umount(struct vfsmount *mnt)
1682 {
1683 	int ret = 1;
1684 	down_read(&namespace_sem);
1685 	lock_mount_hash();
1686 	if (propagate_mount_busy(real_mount(mnt), 2))
1687 		ret = 0;
1688 	unlock_mount_hash();
1689 	up_read(&namespace_sem);
1690 	return ret;
1691 }
1692 
1693 EXPORT_SYMBOL(may_umount);
1694 
1695 #ifdef CONFIG_FSNOTIFY
1696 static void mnt_notify(struct mount *p)
1697 {
1698 	if (!p->prev_ns && p->mnt_ns) {
1699 		fsnotify_mnt_attach(p->mnt_ns, &p->mnt);
1700 	} else if (p->prev_ns && !p->mnt_ns) {
1701 		fsnotify_mnt_detach(p->prev_ns, &p->mnt);
1702 	} else if (p->prev_ns == p->mnt_ns) {
1703 		fsnotify_mnt_move(p->mnt_ns, &p->mnt);
1704 	} else {
1705 		fsnotify_mnt_detach(p->prev_ns, &p->mnt);
1706 		fsnotify_mnt_attach(p->mnt_ns, &p->mnt);
1707 	}
1708 	p->prev_ns = p->mnt_ns;
1709 }
1710 
1711 static void notify_mnt_list(void)
1712 {
1713 	struct mount *m, *tmp;
1714 	/*
1715 	 * Notify about mounts that were added/reparented/detached/remain
1716 	 * connected after unmount.
1717 	 */
1718 	list_for_each_entry_safe(m, tmp, &notify_list, to_notify) {
1719 		mnt_notify(m);
1720 		list_del_init(&m->to_notify);
1721 	}
1722 }
1723 
1724 static bool need_notify_mnt_list(void)
1725 {
1726 	return !list_empty(&notify_list);
1727 }
1728 #else
1729 static void notify_mnt_list(void)
1730 {
1731 }
1732 
1733 static bool need_notify_mnt_list(void)
1734 {
1735 	return false;
1736 }
1737 #endif
1738 
1739 static void free_mnt_ns(struct mnt_namespace *);
1740 static void namespace_unlock(void)
1741 {
1742 	struct hlist_head head;
1743 	struct hlist_node *p;
1744 	struct mount *m;
1745 	struct mnt_namespace *ns = emptied_ns;
1746 	LIST_HEAD(list);
1747 
1748 	hlist_move_list(&unmounted, &head);
1749 	list_splice_init(&ex_mountpoints, &list);
1750 	emptied_ns = NULL;
1751 
1752 	if (need_notify_mnt_list()) {
1753 		/*
1754 		 * No point blocking out concurrent readers while notifications
1755 		 * are sent. This will also allow statmount()/listmount() to run
1756 		 * concurrently.
1757 		 */
1758 		downgrade_write(&namespace_sem);
1759 		notify_mnt_list();
1760 		up_read(&namespace_sem);
1761 	} else {
1762 		up_write(&namespace_sem);
1763 	}
1764 	if (unlikely(ns)) {
1765 		/* Make sure we notice when we leak mounts. */
1766 		VFS_WARN_ON_ONCE(!mnt_ns_empty(ns));
1767 		free_mnt_ns(ns);
1768 	}
1769 
1770 	shrink_dentry_list(&list);
1771 
1772 	if (likely(hlist_empty(&head)))
1773 		return;
1774 
1775 	synchronize_rcu_expedited();
1776 
1777 	hlist_for_each_entry_safe(m, p, &head, mnt_umount) {
1778 		hlist_del(&m->mnt_umount);
1779 		mntput(&m->mnt);
1780 	}
1781 }
1782 
1783 static inline void namespace_lock(void)
1784 {
1785 	down_write(&namespace_sem);
1786 }
1787 
1788 DEFINE_GUARD(namespace_lock, struct rw_semaphore *, namespace_lock(), namespace_unlock())
1789 
1790 enum umount_tree_flags {
1791 	UMOUNT_SYNC = 1,
1792 	UMOUNT_PROPAGATE = 2,
1793 	UMOUNT_CONNECTED = 4,
1794 };
1795 
1796 static bool disconnect_mount(struct mount *mnt, enum umount_tree_flags how)
1797 {
1798 	/* Leaving mounts connected is only valid for lazy umounts */
1799 	if (how & UMOUNT_SYNC)
1800 		return true;
1801 
1802 	/* A mount without a parent has nothing to be connected to */
1803 	if (!mnt_has_parent(mnt))
1804 		return true;
1805 
1806 	/* Because the reference counting rules change when mounts are
1807 	 * unmounted and connected, umounted mounts may not be
1808 	 * connected to mounted mounts.
1809 	 */
1810 	if (!(mnt->mnt_parent->mnt.mnt_flags & MNT_UMOUNT))
1811 		return true;
1812 
1813 	/* Has it been requested that the mount remain connected? */
1814 	if (how & UMOUNT_CONNECTED)
1815 		return false;
1816 
1817 	/* Is the mount locked such that it needs to remain connected? */
1818 	if (IS_MNT_LOCKED(mnt))
1819 		return false;
1820 
1821 	/* By default disconnect the mount */
1822 	return true;
1823 }
1824 
1825 /*
1826  * mount_lock must be held
1827  * namespace_sem must be held for write
1828  */
1829 static void umount_tree(struct mount *mnt, enum umount_tree_flags how)
1830 {
1831 	LIST_HEAD(tmp_list);
1832 	struct mount *p;
1833 
1834 	if (how & UMOUNT_PROPAGATE)
1835 		propagate_mount_unlock(mnt);
1836 
1837 	/* Gather the mounts to umount */
1838 	for (p = mnt; p; p = next_mnt(p, mnt)) {
1839 		p->mnt.mnt_flags |= MNT_UMOUNT;
1840 		if (mnt_ns_attached(p))
1841 			move_from_ns(p);
1842 		list_add_tail(&p->mnt_list, &tmp_list);
1843 	}
1844 
1845 	/* Hide the mounts from mnt_mounts */
1846 	list_for_each_entry(p, &tmp_list, mnt_list) {
1847 		list_del_init(&p->mnt_child);
1848 	}
1849 
1850 	/* Add propagated mounts to the tmp_list */
1851 	if (how & UMOUNT_PROPAGATE)
1852 		propagate_umount(&tmp_list);
1853 
1854 	while (!list_empty(&tmp_list)) {
1855 		struct mnt_namespace *ns;
1856 		bool disconnect;
1857 		p = list_first_entry(&tmp_list, struct mount, mnt_list);
1858 		list_del_init(&p->mnt_expire);
1859 		list_del_init(&p->mnt_list);
1860 		ns = p->mnt_ns;
1861 		if (ns) {
1862 			ns->nr_mounts--;
1863 			__touch_mnt_namespace(ns);
1864 		}
1865 		p->mnt_ns = NULL;
1866 		if (how & UMOUNT_SYNC)
1867 			p->mnt.mnt_flags |= MNT_SYNC_UMOUNT;
1868 
1869 		disconnect = disconnect_mount(p, how);
1870 		if (mnt_has_parent(p)) {
1871 			if (!disconnect) {
1872 				/* Don't forget about p */
1873 				list_add_tail(&p->mnt_child, &p->mnt_parent->mnt_mounts);
1874 			} else {
1875 				umount_mnt(p);
1876 			}
1877 		}
1878 		change_mnt_propagation(p, MS_PRIVATE);
1879 		if (disconnect)
1880 			hlist_add_head(&p->mnt_umount, &unmounted);
1881 
1882 		/*
1883 		 * At this point p->mnt_ns is NULL, notification will be queued
1884 		 * only if
1885 		 *
1886 		 *  - p->prev_ns is non-NULL *and*
1887 		 *  - p->prev_ns->n_fsnotify_marks is non-NULL
1888 		 *
1889 		 * This will preclude queuing the mount if this is a cleanup
1890 		 * after a failed copy_tree() or destruction of an anonymous
1891 		 * namespace, etc.
1892 		 */
1893 		mnt_notify_add(p);
1894 	}
1895 }
1896 
1897 static void shrink_submounts(struct mount *mnt);
1898 
1899 static int do_umount_root(struct super_block *sb)
1900 {
1901 	int ret = 0;
1902 
1903 	down_write(&sb->s_umount);
1904 	if (!sb_rdonly(sb)) {
1905 		struct fs_context *fc;
1906 
1907 		fc = fs_context_for_reconfigure(sb->s_root, SB_RDONLY,
1908 						SB_RDONLY);
1909 		if (IS_ERR(fc)) {
1910 			ret = PTR_ERR(fc);
1911 		} else {
1912 			ret = parse_monolithic_mount_data(fc, NULL);
1913 			if (!ret)
1914 				ret = reconfigure_super(fc);
1915 			put_fs_context(fc);
1916 		}
1917 	}
1918 	up_write(&sb->s_umount);
1919 	return ret;
1920 }
1921 
1922 static int do_umount(struct mount *mnt, int flags)
1923 {
1924 	struct super_block *sb = mnt->mnt.mnt_sb;
1925 	int retval;
1926 
1927 	retval = security_sb_umount(&mnt->mnt, flags);
1928 	if (retval)
1929 		return retval;
1930 
1931 	/*
1932 	 * Allow userspace to request a mountpoint be expired rather than
1933 	 * unmounting unconditionally. Unmount only happens if:
1934 	 *  (1) the mark is already set (the mark is cleared by mntput())
1935 	 *  (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
1936 	 */
1937 	if (flags & MNT_EXPIRE) {
1938 		if (&mnt->mnt == current->fs->root.mnt ||
1939 		    flags & (MNT_FORCE | MNT_DETACH))
1940 			return -EINVAL;
1941 
1942 		/*
1943 		 * probably don't strictly need the lock here if we examined
1944 		 * all race cases, but it's a slowpath.
1945 		 */
1946 		lock_mount_hash();
1947 		if (!list_empty(&mnt->mnt_mounts) || mnt_get_count(mnt) != 2) {
1948 			unlock_mount_hash();
1949 			return -EBUSY;
1950 		}
1951 		unlock_mount_hash();
1952 
1953 		if (!xchg(&mnt->mnt_expiry_mark, 1))
1954 			return -EAGAIN;
1955 	}
1956 
1957 	/*
1958 	 * If we may have to abort operations to get out of this
1959 	 * mount, and they will themselves hold resources we must
1960 	 * allow the fs to do things. In the Unix tradition of
1961 	 * 'Gee thats tricky lets do it in userspace' the umount_begin
1962 	 * might fail to complete on the first run through as other tasks
1963 	 * must return, and the like. Thats for the mount program to worry
1964 	 * about for the moment.
1965 	 */
1966 
1967 	if (flags & MNT_FORCE && sb->s_op->umount_begin) {
1968 		sb->s_op->umount_begin(sb);
1969 	}
1970 
1971 	/*
1972 	 * No sense to grab the lock for this test, but test itself looks
1973 	 * somewhat bogus. Suggestions for better replacement?
1974 	 * Ho-hum... In principle, we might treat that as umount + switch
1975 	 * to rootfs. GC would eventually take care of the old vfsmount.
1976 	 * Actually it makes sense, especially if rootfs would contain a
1977 	 * /reboot - static binary that would close all descriptors and
1978 	 * call reboot(9). Then init(8) could umount root and exec /reboot.
1979 	 */
1980 	if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
1981 		/*
1982 		 * Special case for "unmounting" root ...
1983 		 * we just try to remount it readonly.
1984 		 */
1985 		if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN))
1986 			return -EPERM;
1987 		return do_umount_root(sb);
1988 	}
1989 
1990 	namespace_lock();
1991 	lock_mount_hash();
1992 
1993 	/* Repeat the earlier racy checks, now that we are holding the locks */
1994 	retval = -EINVAL;
1995 	if (!check_mnt(mnt))
1996 		goto out;
1997 
1998 	if (mnt->mnt.mnt_flags & MNT_LOCKED)
1999 		goto out;
2000 
2001 	if (!mnt_has_parent(mnt)) /* not the absolute root */
2002 		goto out;
2003 
2004 	event++;
2005 	if (flags & MNT_DETACH) {
2006 		umount_tree(mnt, UMOUNT_PROPAGATE);
2007 		retval = 0;
2008 	} else {
2009 		smp_mb(); // paired with __legitimize_mnt()
2010 		shrink_submounts(mnt);
2011 		retval = -EBUSY;
2012 		if (!propagate_mount_busy(mnt, 2)) {
2013 			umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
2014 			retval = 0;
2015 		}
2016 	}
2017 out:
2018 	unlock_mount_hash();
2019 	namespace_unlock();
2020 	return retval;
2021 }
2022 
2023 /*
2024  * __detach_mounts - lazily unmount all mounts on the specified dentry
2025  *
2026  * During unlink, rmdir, and d_drop it is possible to loose the path
2027  * to an existing mountpoint, and wind up leaking the mount.
2028  * detach_mounts allows lazily unmounting those mounts instead of
2029  * leaking them.
2030  *
2031  * The caller may hold dentry->d_inode->i_rwsem.
2032  */
2033 void __detach_mounts(struct dentry *dentry)
2034 {
2035 	struct pinned_mountpoint mp = {};
2036 	struct mount *mnt;
2037 
2038 	namespace_lock();
2039 	lock_mount_hash();
2040 	if (!lookup_mountpoint(dentry, &mp))
2041 		goto out_unlock;
2042 
2043 	event++;
2044 	while (mp.node.next) {
2045 		mnt = hlist_entry(mp.node.next, struct mount, mnt_mp_list);
2046 		if (mnt->mnt.mnt_flags & MNT_UMOUNT) {
2047 			umount_mnt(mnt);
2048 			hlist_add_head(&mnt->mnt_umount, &unmounted);
2049 		}
2050 		else umount_tree(mnt, UMOUNT_CONNECTED);
2051 	}
2052 	unpin_mountpoint(&mp);
2053 out_unlock:
2054 	unlock_mount_hash();
2055 	namespace_unlock();
2056 }
2057 
2058 /*
2059  * Is the caller allowed to modify his namespace?
2060  */
2061 bool may_mount(void)
2062 {
2063 	return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN);
2064 }
2065 
2066 static void warn_mandlock(void)
2067 {
2068 	pr_warn_once("=======================================================\n"
2069 		     "WARNING: The mand mount option has been deprecated and\n"
2070 		     "         and is ignored by this kernel. Remove the mand\n"
2071 		     "         option from the mount to silence this warning.\n"
2072 		     "=======================================================\n");
2073 }
2074 
2075 static int can_umount(const struct path *path, int flags)
2076 {
2077 	struct mount *mnt = real_mount(path->mnt);
2078 	struct super_block *sb = path->dentry->d_sb;
2079 
2080 	if (!may_mount())
2081 		return -EPERM;
2082 	if (!path_mounted(path))
2083 		return -EINVAL;
2084 	if (!check_mnt(mnt))
2085 		return -EINVAL;
2086 	if (mnt->mnt.mnt_flags & MNT_LOCKED) /* Check optimistically */
2087 		return -EINVAL;
2088 	if (flags & MNT_FORCE && !ns_capable(sb->s_user_ns, CAP_SYS_ADMIN))
2089 		return -EPERM;
2090 	return 0;
2091 }
2092 
2093 // caller is responsible for flags being sane
2094 int path_umount(struct path *path, int flags)
2095 {
2096 	struct mount *mnt = real_mount(path->mnt);
2097 	int ret;
2098 
2099 	ret = can_umount(path, flags);
2100 	if (!ret)
2101 		ret = do_umount(mnt, flags);
2102 
2103 	/* we mustn't call path_put() as that would clear mnt_expiry_mark */
2104 	dput(path->dentry);
2105 	mntput_no_expire(mnt);
2106 	return ret;
2107 }
2108 
2109 static int ksys_umount(char __user *name, int flags)
2110 {
2111 	int lookup_flags = LOOKUP_MOUNTPOINT;
2112 	struct path path;
2113 	int ret;
2114 
2115 	// basic validity checks done first
2116 	if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
2117 		return -EINVAL;
2118 
2119 	if (!(flags & UMOUNT_NOFOLLOW))
2120 		lookup_flags |= LOOKUP_FOLLOW;
2121 	ret = user_path_at(AT_FDCWD, name, lookup_flags, &path);
2122 	if (ret)
2123 		return ret;
2124 	return path_umount(&path, flags);
2125 }
2126 
2127 SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
2128 {
2129 	return ksys_umount(name, flags);
2130 }
2131 
2132 #ifdef __ARCH_WANT_SYS_OLDUMOUNT
2133 
2134 /*
2135  *	The 2.0 compatible umount. No flags.
2136  */
2137 SYSCALL_DEFINE1(oldumount, char __user *, name)
2138 {
2139 	return ksys_umount(name, 0);
2140 }
2141 
2142 #endif
2143 
2144 static bool is_mnt_ns_file(struct dentry *dentry)
2145 {
2146 	struct ns_common *ns;
2147 
2148 	/* Is this a proxy for a mount namespace? */
2149 	if (dentry->d_op != &ns_dentry_operations)
2150 		return false;
2151 
2152 	ns = d_inode(dentry)->i_private;
2153 
2154 	return ns->ops == &mntns_operations;
2155 }
2156 
2157 struct ns_common *from_mnt_ns(struct mnt_namespace *mnt)
2158 {
2159 	return &mnt->ns;
2160 }
2161 
2162 struct mnt_namespace *get_sequential_mnt_ns(struct mnt_namespace *mntns, bool previous)
2163 {
2164 	guard(rcu)();
2165 
2166 	for (;;) {
2167 		struct list_head *list;
2168 
2169 		if (previous)
2170 			list = rcu_dereference(list_bidir_prev_rcu(&mntns->mnt_ns_list));
2171 		else
2172 			list = rcu_dereference(list_next_rcu(&mntns->mnt_ns_list));
2173 		if (list_is_head(list, &mnt_ns_list))
2174 			return ERR_PTR(-ENOENT);
2175 
2176 		mntns = list_entry_rcu(list, struct mnt_namespace, mnt_ns_list);
2177 
2178 		/*
2179 		 * The last passive reference count is put with RCU
2180 		 * delay so accessing the mount namespace is not just
2181 		 * safe but all relevant members are still valid.
2182 		 */
2183 		if (!ns_capable_noaudit(mntns->user_ns, CAP_SYS_ADMIN))
2184 			continue;
2185 
2186 		/*
2187 		 * We need an active reference count as we're persisting
2188 		 * the mount namespace and it might already be on its
2189 		 * deathbed.
2190 		 */
2191 		if (!refcount_inc_not_zero(&mntns->ns.count))
2192 			continue;
2193 
2194 		return mntns;
2195 	}
2196 }
2197 
2198 struct mnt_namespace *mnt_ns_from_dentry(struct dentry *dentry)
2199 {
2200 	if (!is_mnt_ns_file(dentry))
2201 		return NULL;
2202 
2203 	return to_mnt_ns(get_proc_ns(dentry->d_inode));
2204 }
2205 
2206 static bool mnt_ns_loop(struct dentry *dentry)
2207 {
2208 	/* Could bind mounting the mount namespace inode cause a
2209 	 * mount namespace loop?
2210 	 */
2211 	struct mnt_namespace *mnt_ns = mnt_ns_from_dentry(dentry);
2212 
2213 	if (!mnt_ns)
2214 		return false;
2215 
2216 	return current->nsproxy->mnt_ns->seq >= mnt_ns->seq;
2217 }
2218 
2219 struct mount *copy_tree(struct mount *src_root, struct dentry *dentry,
2220 					int flag)
2221 {
2222 	struct mount *res, *src_parent, *src_root_child, *src_mnt,
2223 		*dst_parent, *dst_mnt;
2224 
2225 	if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(src_root))
2226 		return ERR_PTR(-EINVAL);
2227 
2228 	if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry))
2229 		return ERR_PTR(-EINVAL);
2230 
2231 	res = dst_mnt = clone_mnt(src_root, dentry, flag);
2232 	if (IS_ERR(dst_mnt))
2233 		return dst_mnt;
2234 
2235 	src_parent = src_root;
2236 
2237 	list_for_each_entry(src_root_child, &src_root->mnt_mounts, mnt_child) {
2238 		if (!is_subdir(src_root_child->mnt_mountpoint, dentry))
2239 			continue;
2240 
2241 		for (src_mnt = src_root_child; src_mnt;
2242 		    src_mnt = next_mnt(src_mnt, src_root_child)) {
2243 			if (!(flag & CL_COPY_UNBINDABLE) &&
2244 			    IS_MNT_UNBINDABLE(src_mnt)) {
2245 				if (src_mnt->mnt.mnt_flags & MNT_LOCKED) {
2246 					/* Both unbindable and locked. */
2247 					dst_mnt = ERR_PTR(-EPERM);
2248 					goto out;
2249 				} else {
2250 					src_mnt = skip_mnt_tree(src_mnt);
2251 					continue;
2252 				}
2253 			}
2254 			if (!(flag & CL_COPY_MNT_NS_FILE) &&
2255 			    is_mnt_ns_file(src_mnt->mnt.mnt_root)) {
2256 				src_mnt = skip_mnt_tree(src_mnt);
2257 				continue;
2258 			}
2259 			while (src_parent != src_mnt->mnt_parent) {
2260 				src_parent = src_parent->mnt_parent;
2261 				dst_mnt = dst_mnt->mnt_parent;
2262 			}
2263 
2264 			src_parent = src_mnt;
2265 			dst_parent = dst_mnt;
2266 			dst_mnt = clone_mnt(src_mnt, src_mnt->mnt.mnt_root, flag);
2267 			if (IS_ERR(dst_mnt))
2268 				goto out;
2269 			lock_mount_hash();
2270 			if (src_mnt->mnt.mnt_flags & MNT_LOCKED)
2271 				dst_mnt->mnt.mnt_flags |= MNT_LOCKED;
2272 			if (unlikely(flag & CL_EXPIRE)) {
2273 				/* stick the duplicate mount on the same expiry
2274 				 * list as the original if that was on one */
2275 				if (!list_empty(&src_mnt->mnt_expire))
2276 					list_add(&dst_mnt->mnt_expire,
2277 						 &src_mnt->mnt_expire);
2278 			}
2279 			attach_mnt(dst_mnt, dst_parent, src_parent->mnt_mp);
2280 			unlock_mount_hash();
2281 		}
2282 	}
2283 	return res;
2284 
2285 out:
2286 	if (res) {
2287 		lock_mount_hash();
2288 		umount_tree(res, UMOUNT_SYNC);
2289 		unlock_mount_hash();
2290 	}
2291 	return dst_mnt;
2292 }
2293 
2294 static inline bool extend_array(struct path **res, struct path **to_free,
2295 				unsigned n, unsigned *count, unsigned new_count)
2296 {
2297 	struct path *p;
2298 
2299 	if (likely(n < *count))
2300 		return true;
2301 	p = kmalloc_array(new_count, sizeof(struct path), GFP_KERNEL);
2302 	if (p && *count)
2303 		memcpy(p, *res, *count * sizeof(struct path));
2304 	*count = new_count;
2305 	kfree(*to_free);
2306 	*to_free = *res = p;
2307 	return p;
2308 }
2309 
2310 struct path *collect_paths(const struct path *path,
2311 			      struct path *prealloc, unsigned count)
2312 {
2313 	struct mount *root = real_mount(path->mnt);
2314 	struct mount *child;
2315 	struct path *res = prealloc, *to_free = NULL;
2316 	unsigned n = 0;
2317 
2318 	guard(rwsem_read)(&namespace_sem);
2319 
2320 	if (!check_mnt(root))
2321 		return ERR_PTR(-EINVAL);
2322 	if (!extend_array(&res, &to_free, 0, &count, 32))
2323 		return ERR_PTR(-ENOMEM);
2324 	res[n++] = *path;
2325 	list_for_each_entry(child, &root->mnt_mounts, mnt_child) {
2326 		if (!is_subdir(child->mnt_mountpoint, path->dentry))
2327 			continue;
2328 		for (struct mount *m = child; m; m = next_mnt(m, child)) {
2329 			if (!extend_array(&res, &to_free, n, &count, 2 * count))
2330 				return ERR_PTR(-ENOMEM);
2331 			res[n].mnt = &m->mnt;
2332 			res[n].dentry = m->mnt.mnt_root;
2333 			n++;
2334 		}
2335 	}
2336 	if (!extend_array(&res, &to_free, n, &count, count + 1))
2337 		return ERR_PTR(-ENOMEM);
2338 	memset(res + n, 0, (count - n) * sizeof(struct path));
2339 	for (struct path *p = res; p->mnt; p++)
2340 		path_get(p);
2341 	return res;
2342 }
2343 
2344 void drop_collected_paths(struct path *paths, struct path *prealloc)
2345 {
2346 	for (struct path *p = paths; p->mnt; p++)
2347 		path_put(p);
2348 	if (paths != prealloc)
2349 		kfree(paths);
2350 }
2351 
2352 static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *, bool);
2353 
2354 void dissolve_on_fput(struct vfsmount *mnt)
2355 {
2356 	struct mount *m = real_mount(mnt);
2357 
2358 	/*
2359 	 * m used to be the root of anon namespace; if it still is one,
2360 	 * we need to dissolve the mount tree and free that namespace.
2361 	 * Let's try to avoid taking namespace_sem if we can determine
2362 	 * that there's nothing to do without it - rcu_read_lock() is
2363 	 * enough to make anon_ns_root() memory-safe and once m has
2364 	 * left its namespace, it's no longer our concern, since it will
2365 	 * never become a root of anon ns again.
2366 	 */
2367 
2368 	scoped_guard(rcu) {
2369 		if (!anon_ns_root(m))
2370 			return;
2371 	}
2372 
2373 	scoped_guard(namespace_lock, &namespace_sem) {
2374 		if (!anon_ns_root(m))
2375 			return;
2376 
2377 		emptied_ns = m->mnt_ns;
2378 		lock_mount_hash();
2379 		umount_tree(m, UMOUNT_CONNECTED);
2380 		unlock_mount_hash();
2381 	}
2382 }
2383 
2384 static bool __has_locked_children(struct mount *mnt, struct dentry *dentry)
2385 {
2386 	struct mount *child;
2387 
2388 	list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
2389 		if (!is_subdir(child->mnt_mountpoint, dentry))
2390 			continue;
2391 
2392 		if (child->mnt.mnt_flags & MNT_LOCKED)
2393 			return true;
2394 	}
2395 	return false;
2396 }
2397 
2398 bool has_locked_children(struct mount *mnt, struct dentry *dentry)
2399 {
2400 	bool res;
2401 
2402 	read_seqlock_excl(&mount_lock);
2403 	res = __has_locked_children(mnt, dentry);
2404 	read_sequnlock_excl(&mount_lock);
2405 	return res;
2406 }
2407 
2408 /*
2409  * Check that there aren't references to earlier/same mount namespaces in the
2410  * specified subtree.  Such references can act as pins for mount namespaces
2411  * that aren't checked by the mount-cycle checking code, thereby allowing
2412  * cycles to be made.
2413  */
2414 static bool check_for_nsfs_mounts(struct mount *subtree)
2415 {
2416 	struct mount *p;
2417 	bool ret = false;
2418 
2419 	lock_mount_hash();
2420 	for (p = subtree; p; p = next_mnt(p, subtree))
2421 		if (mnt_ns_loop(p->mnt.mnt_root))
2422 			goto out;
2423 
2424 	ret = true;
2425 out:
2426 	unlock_mount_hash();
2427 	return ret;
2428 }
2429 
2430 /**
2431  * clone_private_mount - create a private clone of a path
2432  * @path: path to clone
2433  *
2434  * This creates a new vfsmount, which will be the clone of @path.  The new mount
2435  * will not be attached anywhere in the namespace and will be private (i.e.
2436  * changes to the originating mount won't be propagated into this).
2437  *
2438  * This assumes caller has called or done the equivalent of may_mount().
2439  *
2440  * Release with mntput().
2441  */
2442 struct vfsmount *clone_private_mount(const struct path *path)
2443 {
2444 	struct mount *old_mnt = real_mount(path->mnt);
2445 	struct mount *new_mnt;
2446 
2447 	guard(rwsem_read)(&namespace_sem);
2448 
2449 	if (IS_MNT_UNBINDABLE(old_mnt))
2450 		return ERR_PTR(-EINVAL);
2451 
2452 	/*
2453 	 * Make sure the source mount is acceptable.
2454 	 * Anything mounted in our mount namespace is allowed.
2455 	 * Otherwise, it must be the root of an anonymous mount
2456 	 * namespace, and we need to make sure no namespace
2457 	 * loops get created.
2458 	 */
2459 	if (!check_mnt(old_mnt)) {
2460 		if (!anon_ns_root(old_mnt))
2461 			return ERR_PTR(-EINVAL);
2462 
2463 		if (!check_for_nsfs_mounts(old_mnt))
2464 			return ERR_PTR(-EINVAL);
2465 	}
2466 
2467 	if (!ns_capable(old_mnt->mnt_ns->user_ns, CAP_SYS_ADMIN))
2468 		return ERR_PTR(-EPERM);
2469 
2470 	if (__has_locked_children(old_mnt, path->dentry))
2471 		return ERR_PTR(-EINVAL);
2472 
2473 	new_mnt = clone_mnt(old_mnt, path->dentry, CL_PRIVATE);
2474 	if (IS_ERR(new_mnt))
2475 		return ERR_PTR(-EINVAL);
2476 
2477 	/* Longterm mount to be removed by kern_unmount*() */
2478 	new_mnt->mnt_ns = MNT_NS_INTERNAL;
2479 	return &new_mnt->mnt;
2480 }
2481 EXPORT_SYMBOL_GPL(clone_private_mount);
2482 
2483 static void lock_mnt_tree(struct mount *mnt)
2484 {
2485 	struct mount *p;
2486 
2487 	for (p = mnt; p; p = next_mnt(p, mnt)) {
2488 		int flags = p->mnt.mnt_flags;
2489 		/* Don't allow unprivileged users to change mount flags */
2490 		flags |= MNT_LOCK_ATIME;
2491 
2492 		if (flags & MNT_READONLY)
2493 			flags |= MNT_LOCK_READONLY;
2494 
2495 		if (flags & MNT_NODEV)
2496 			flags |= MNT_LOCK_NODEV;
2497 
2498 		if (flags & MNT_NOSUID)
2499 			flags |= MNT_LOCK_NOSUID;
2500 
2501 		if (flags & MNT_NOEXEC)
2502 			flags |= MNT_LOCK_NOEXEC;
2503 		/* Don't allow unprivileged users to reveal what is under a mount */
2504 		if (list_empty(&p->mnt_expire) && p != mnt)
2505 			flags |= MNT_LOCKED;
2506 		p->mnt.mnt_flags = flags;
2507 	}
2508 }
2509 
2510 static void cleanup_group_ids(struct mount *mnt, struct mount *end)
2511 {
2512 	struct mount *p;
2513 
2514 	for (p = mnt; p != end; p = next_mnt(p, mnt)) {
2515 		if (p->mnt_group_id && !IS_MNT_SHARED(p))
2516 			mnt_release_group_id(p);
2517 	}
2518 }
2519 
2520 static int invent_group_ids(struct mount *mnt, bool recurse)
2521 {
2522 	struct mount *p;
2523 
2524 	for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
2525 		if (!p->mnt_group_id) {
2526 			int err = mnt_alloc_group_id(p);
2527 			if (err) {
2528 				cleanup_group_ids(mnt, p);
2529 				return err;
2530 			}
2531 		}
2532 	}
2533 
2534 	return 0;
2535 }
2536 
2537 int count_mounts(struct mnt_namespace *ns, struct mount *mnt)
2538 {
2539 	unsigned int max = READ_ONCE(sysctl_mount_max);
2540 	unsigned int mounts = 0;
2541 	struct mount *p;
2542 
2543 	if (ns->nr_mounts >= max)
2544 		return -ENOSPC;
2545 	max -= ns->nr_mounts;
2546 	if (ns->pending_mounts >= max)
2547 		return -ENOSPC;
2548 	max -= ns->pending_mounts;
2549 
2550 	for (p = mnt; p; p = next_mnt(p, mnt))
2551 		mounts++;
2552 
2553 	if (mounts > max)
2554 		return -ENOSPC;
2555 
2556 	ns->pending_mounts += mounts;
2557 	return 0;
2558 }
2559 
2560 enum mnt_tree_flags_t {
2561 	MNT_TREE_BENEATH = BIT(0),
2562 	MNT_TREE_PROPAGATION = BIT(1),
2563 };
2564 
2565 /**
2566  * attach_recursive_mnt - attach a source mount tree
2567  * @source_mnt: mount tree to be attached
2568  * @dest_mnt:   mount that @source_mnt will be mounted on
2569  * @dest_mp:    the mountpoint @source_mnt will be mounted at
2570  *
2571  *  NOTE: in the table below explains the semantics when a source mount
2572  *  of a given type is attached to a destination mount of a given type.
2573  * ---------------------------------------------------------------------------
2574  * |         BIND MOUNT OPERATION                                            |
2575  * |**************************************************************************
2576  * | source-->| shared        |       private  |       slave    | unbindable |
2577  * | dest     |               |                |                |            |
2578  * |   |      |               |                |                |            |
2579  * |   v      |               |                |                |            |
2580  * |**************************************************************************
2581  * |  shared  | shared (++)   |     shared (+) |     shared(+++)|  invalid   |
2582  * |          |               |                |                |            |
2583  * |non-shared| shared (+)    |      private   |      slave (*) |  invalid   |
2584  * ***************************************************************************
2585  * A bind operation clones the source mount and mounts the clone on the
2586  * destination mount.
2587  *
2588  * (++)  the cloned mount is propagated to all the mounts in the propagation
2589  * 	 tree of the destination mount and the cloned mount is added to
2590  * 	 the peer group of the source mount.
2591  * (+)   the cloned mount is created under the destination mount and is marked
2592  *       as shared. The cloned mount is added to the peer group of the source
2593  *       mount.
2594  * (+++) the mount is propagated to all the mounts in the propagation tree
2595  *       of the destination mount and the cloned mount is made slave
2596  *       of the same master as that of the source mount. The cloned mount
2597  *       is marked as 'shared and slave'.
2598  * (*)   the cloned mount is made a slave of the same master as that of the
2599  * 	 source mount.
2600  *
2601  * ---------------------------------------------------------------------------
2602  * |         		MOVE MOUNT OPERATION                                 |
2603  * |**************************************************************************
2604  * | source-->| shared        |       private  |       slave    | unbindable |
2605  * | dest     |               |                |                |            |
2606  * |   |      |               |                |                |            |
2607  * |   v      |               |                |                |            |
2608  * |**************************************************************************
2609  * |  shared  | shared (+)    |     shared (+) |    shared(+++) |  invalid   |
2610  * |          |               |                |                |            |
2611  * |non-shared| shared (+*)   |      private   |    slave (*)   | unbindable |
2612  * ***************************************************************************
2613  *
2614  * (+)  the mount is moved to the destination. And is then propagated to
2615  * 	all the mounts in the propagation tree of the destination mount.
2616  * (+*)  the mount is moved to the destination.
2617  * (+++)  the mount is moved to the destination and is then propagated to
2618  * 	all the mounts belonging to the destination mount's propagation tree.
2619  * 	the mount is marked as 'shared and slave'.
2620  * (*)	the mount continues to be a slave at the new location.
2621  *
2622  * if the source mount is a tree, the operations explained above is
2623  * applied to each mount in the tree.
2624  * Must be called without spinlocks held, since this function can sleep
2625  * in allocations.
2626  *
2627  * Context: The function expects namespace_lock() to be held.
2628  * Return: If @source_mnt was successfully attached 0 is returned.
2629  *         Otherwise a negative error code is returned.
2630  */
2631 static int attach_recursive_mnt(struct mount *source_mnt,
2632 				struct mount *dest_mnt,
2633 				struct mountpoint *dest_mp)
2634 {
2635 	struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns;
2636 	HLIST_HEAD(tree_list);
2637 	struct mnt_namespace *ns = dest_mnt->mnt_ns;
2638 	struct pinned_mountpoint root = {};
2639 	struct mountpoint *shorter = NULL;
2640 	struct mount *child, *p;
2641 	struct mount *top;
2642 	struct hlist_node *n;
2643 	int err = 0;
2644 	bool moving = mnt_has_parent(source_mnt);
2645 
2646 	/*
2647 	 * Preallocate a mountpoint in case the new mounts need to be
2648 	 * mounted beneath mounts on the same mountpoint.
2649 	 */
2650 	for (top = source_mnt; unlikely(top->overmount); top = top->overmount) {
2651 		if (!shorter && is_mnt_ns_file(top->mnt.mnt_root))
2652 			shorter = top->mnt_mp;
2653 	}
2654 	err = get_mountpoint(top->mnt.mnt_root, &root);
2655 	if (err)
2656 		return err;
2657 
2658 	/* Is there space to add these mounts to the mount namespace? */
2659 	if (!moving) {
2660 		err = count_mounts(ns, source_mnt);
2661 		if (err)
2662 			goto out;
2663 	}
2664 
2665 	if (IS_MNT_SHARED(dest_mnt)) {
2666 		err = invent_group_ids(source_mnt, true);
2667 		if (err)
2668 			goto out;
2669 		err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list);
2670 	}
2671 	lock_mount_hash();
2672 	if (err)
2673 		goto out_cleanup_ids;
2674 
2675 	if (IS_MNT_SHARED(dest_mnt)) {
2676 		for (p = source_mnt; p; p = next_mnt(p, source_mnt))
2677 			set_mnt_shared(p);
2678 	}
2679 
2680 	if (moving) {
2681 		umount_mnt(source_mnt);
2682 		mnt_notify_add(source_mnt);
2683 		/* if the mount is moved, it should no longer be expired
2684 		 * automatically */
2685 		list_del_init(&source_mnt->mnt_expire);
2686 	} else {
2687 		if (source_mnt->mnt_ns) {
2688 			/* move from anon - the caller will destroy */
2689 			emptied_ns = source_mnt->mnt_ns;
2690 			for (p = source_mnt; p; p = next_mnt(p, source_mnt))
2691 				move_from_ns(p);
2692 		}
2693 	}
2694 
2695 	mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt);
2696 	/*
2697 	 * Now the original copy is in the same state as the secondaries -
2698 	 * its root attached to mountpoint, but not hashed and all mounts
2699 	 * in it are either in our namespace or in no namespace at all.
2700 	 * Add the original to the list of copies and deal with the
2701 	 * rest of work for all of them uniformly.
2702 	 */
2703 	hlist_add_head(&source_mnt->mnt_hash, &tree_list);
2704 
2705 	hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) {
2706 		struct mount *q;
2707 		hlist_del_init(&child->mnt_hash);
2708 		/* Notice when we are propagating across user namespaces */
2709 		if (child->mnt_parent->mnt_ns->user_ns != user_ns)
2710 			lock_mnt_tree(child);
2711 		q = __lookup_mnt(&child->mnt_parent->mnt,
2712 				 child->mnt_mountpoint);
2713 		commit_tree(child);
2714 		if (q) {
2715 			struct mountpoint *mp = root.mp;
2716 			struct mount *r = child;
2717 			while (unlikely(r->overmount))
2718 				r = r->overmount;
2719 			if (unlikely(shorter) && child != source_mnt)
2720 				mp = shorter;
2721 			mnt_change_mountpoint(r, mp, q);
2722 		}
2723 	}
2724 	unpin_mountpoint(&root);
2725 	unlock_mount_hash();
2726 
2727 	return 0;
2728 
2729  out_cleanup_ids:
2730 	while (!hlist_empty(&tree_list)) {
2731 		child = hlist_entry(tree_list.first, struct mount, mnt_hash);
2732 		child->mnt_parent->mnt_ns->pending_mounts = 0;
2733 		umount_tree(child, UMOUNT_SYNC);
2734 	}
2735 	unlock_mount_hash();
2736 	cleanup_group_ids(source_mnt, NULL);
2737  out:
2738 	ns->pending_mounts = 0;
2739 
2740 	read_seqlock_excl(&mount_lock);
2741 	unpin_mountpoint(&root);
2742 	read_sequnlock_excl(&mount_lock);
2743 
2744 	return err;
2745 }
2746 
2747 /**
2748  * do_lock_mount - lock mount and mountpoint
2749  * @path:    target path
2750  * @beneath: whether the intention is to mount beneath @path
2751  *
2752  * Follow the mount stack on @path until the top mount @mnt is found. If
2753  * the initial @path->{mnt,dentry} is a mountpoint lookup the first
2754  * mount stacked on top of it. Then simply follow @{mnt,mnt->mnt_root}
2755  * until nothing is stacked on top of it anymore.
2756  *
2757  * Acquire the inode_lock() on the top mount's ->mnt_root to protect
2758  * against concurrent removal of the new mountpoint from another mount
2759  * namespace.
2760  *
2761  * If @beneath is requested, acquire inode_lock() on @mnt's mountpoint
2762  * @mp on @mnt->mnt_parent must be acquired. This protects against a
2763  * concurrent unlink of @mp->mnt_dentry from another mount namespace
2764  * where @mnt doesn't have a child mount mounted @mp. A concurrent
2765  * removal of @mnt->mnt_root doesn't matter as nothing will be mounted
2766  * on top of it for @beneath.
2767  *
2768  * In addition, @beneath needs to make sure that @mnt hasn't been
2769  * unmounted or moved from its current mountpoint in between dropping
2770  * @mount_lock and acquiring @namespace_sem. For the !@beneath case @mnt
2771  * being unmounted would be detected later by e.g., calling
2772  * check_mnt(mnt) in the function it's called from. For the @beneath
2773  * case however, it's useful to detect it directly in do_lock_mount().
2774  * If @mnt hasn't been unmounted then @mnt->mnt_mountpoint still points
2775  * to @mnt->mnt_mp->m_dentry. But if @mnt has been unmounted it will
2776  * point to @mnt->mnt_root and @mnt->mnt_mp will be NULL.
2777  *
2778  * Return: Either the target mountpoint on the top mount or the top
2779  *         mount's mountpoint.
2780  */
2781 static int do_lock_mount(struct path *path, struct pinned_mountpoint *pinned, bool beneath)
2782 {
2783 	struct vfsmount *mnt = path->mnt;
2784 	struct dentry *dentry;
2785 	struct path under = {};
2786 	int err = -ENOENT;
2787 
2788 	for (;;) {
2789 		struct mount *m = real_mount(mnt);
2790 
2791 		if (beneath) {
2792 			path_put(&under);
2793 			read_seqlock_excl(&mount_lock);
2794 			under.mnt = mntget(&m->mnt_parent->mnt);
2795 			under.dentry = dget(m->mnt_mountpoint);
2796 			read_sequnlock_excl(&mount_lock);
2797 			dentry = under.dentry;
2798 		} else {
2799 			dentry = path->dentry;
2800 		}
2801 
2802 		inode_lock(dentry->d_inode);
2803 		namespace_lock();
2804 
2805 		if (unlikely(cant_mount(dentry) || !is_mounted(mnt)))
2806 			break;		// not to be mounted on
2807 
2808 		if (beneath && unlikely(m->mnt_mountpoint != dentry ||
2809 				        &m->mnt_parent->mnt != under.mnt)) {
2810 			namespace_unlock();
2811 			inode_unlock(dentry->d_inode);
2812 			continue;	// got moved
2813 		}
2814 
2815 		mnt = lookup_mnt(path);
2816 		if (unlikely(mnt)) {
2817 			namespace_unlock();
2818 			inode_unlock(dentry->d_inode);
2819 			path_put(path);
2820 			path->mnt = mnt;
2821 			path->dentry = dget(mnt->mnt_root);
2822 			continue;	// got overmounted
2823 		}
2824 		err = get_mountpoint(dentry, pinned);
2825 		if (err)
2826 			break;
2827 		if (beneath) {
2828 			/*
2829 			 * @under duplicates the references that will stay
2830 			 * at least until namespace_unlock(), so the path_put()
2831 			 * below is safe (and OK to do under namespace_lock -
2832 			 * we are not dropping the final references here).
2833 			 */
2834 			path_put(&under);
2835 		}
2836 		return 0;
2837 	}
2838 	namespace_unlock();
2839 	inode_unlock(dentry->d_inode);
2840 	if (beneath)
2841 		path_put(&under);
2842 	return err;
2843 }
2844 
2845 static inline int lock_mount(struct path *path, struct pinned_mountpoint *m)
2846 {
2847 	return do_lock_mount(path, m, false);
2848 }
2849 
2850 static void unlock_mount(struct pinned_mountpoint *m)
2851 {
2852 	inode_unlock(m->mp->m_dentry->d_inode);
2853 	read_seqlock_excl(&mount_lock);
2854 	unpin_mountpoint(m);
2855 	read_sequnlock_excl(&mount_lock);
2856 	namespace_unlock();
2857 }
2858 
2859 static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp)
2860 {
2861 	if (mnt->mnt.mnt_sb->s_flags & SB_NOUSER)
2862 		return -EINVAL;
2863 
2864 	if (d_is_dir(mp->m_dentry) !=
2865 	      d_is_dir(mnt->mnt.mnt_root))
2866 		return -ENOTDIR;
2867 
2868 	return attach_recursive_mnt(mnt, p, mp);
2869 }
2870 
2871 static int may_change_propagation(const struct mount *m)
2872 {
2873         struct mnt_namespace *ns = m->mnt_ns;
2874 
2875 	 // it must be mounted in some namespace
2876 	 if (IS_ERR_OR_NULL(ns))         // is_mounted()
2877 		 return -EINVAL;
2878 	 // and the caller must be admin in userns of that namespace
2879 	 if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN))
2880 		 return -EPERM;
2881 	 return 0;
2882 }
2883 
2884 /*
2885  * Sanity check the flags to change_mnt_propagation.
2886  */
2887 
2888 static int flags_to_propagation_type(int ms_flags)
2889 {
2890 	int type = ms_flags & ~(MS_REC | MS_SILENT);
2891 
2892 	/* Fail if any non-propagation flags are set */
2893 	if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
2894 		return 0;
2895 	/* Only one propagation flag should be set */
2896 	if (!is_power_of_2(type))
2897 		return 0;
2898 	return type;
2899 }
2900 
2901 /*
2902  * recursively change the type of the mountpoint.
2903  */
2904 static int do_change_type(struct path *path, int ms_flags)
2905 {
2906 	struct mount *m;
2907 	struct mount *mnt = real_mount(path->mnt);
2908 	int recurse = ms_flags & MS_REC;
2909 	int type;
2910 	int err = 0;
2911 
2912 	if (!path_mounted(path))
2913 		return -EINVAL;
2914 
2915 	type = flags_to_propagation_type(ms_flags);
2916 	if (!type)
2917 		return -EINVAL;
2918 
2919 	namespace_lock();
2920 	err = may_change_propagation(mnt);
2921 	if (err)
2922 		goto out_unlock;
2923 
2924 	if (type == MS_SHARED) {
2925 		err = invent_group_ids(mnt, recurse);
2926 		if (err)
2927 			goto out_unlock;
2928 	}
2929 
2930 	for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
2931 		change_mnt_propagation(m, type);
2932 
2933  out_unlock:
2934 	namespace_unlock();
2935 	return err;
2936 }
2937 
2938 /* may_copy_tree() - check if a mount tree can be copied
2939  * @path: path to the mount tree to be copied
2940  *
2941  * This helper checks if the caller may copy the mount tree starting
2942  * from @path->mnt. The caller may copy the mount tree under the
2943  * following circumstances:
2944  *
2945  * (1) The caller is located in the mount namespace of the mount tree.
2946  *     This also implies that the mount does not belong to an anonymous
2947  *     mount namespace.
2948  * (2) The caller tries to copy an nfs mount referring to a mount
2949  *     namespace, i.e., the caller is trying to copy a mount namespace
2950  *     entry from nsfs.
2951  * (3) The caller tries to copy a pidfs mount referring to a pidfd.
2952  * (4) The caller is trying to copy a mount tree that belongs to an
2953  *     anonymous mount namespace.
2954  *
2955  *     For that to be safe, this helper enforces that the origin mount
2956  *     namespace the anonymous mount namespace was created from is the
2957  *     same as the caller's mount namespace by comparing the sequence
2958  *     numbers.
2959  *
2960  *     This is not strictly necessary. The current semantics of the new
2961  *     mount api enforce that the caller must be located in the same
2962  *     mount namespace as the mount tree it interacts with. Using the
2963  *     origin sequence number preserves these semantics even for
2964  *     anonymous mount namespaces. However, one could envision extending
2965  *     the api to directly operate across mount namespace if needed.
2966  *
2967  *     The ownership of a non-anonymous mount namespace such as the
2968  *     caller's cannot change.
2969  *     => We know that the caller's mount namespace is stable.
2970  *
2971  *     If the origin sequence number of the anonymous mount namespace is
2972  *     the same as the sequence number of the caller's mount namespace.
2973  *     => The owning namespaces are the same.
2974  *
2975  *     ==> The earlier capability check on the owning namespace of the
2976  *         caller's mount namespace ensures that the caller has the
2977  *         ability to copy the mount tree.
2978  *
2979  * Returns true if the mount tree can be copied, false otherwise.
2980  */
2981 static inline bool may_copy_tree(struct path *path)
2982 {
2983 	struct mount *mnt = real_mount(path->mnt);
2984 	const struct dentry_operations *d_op;
2985 
2986 	if (check_mnt(mnt))
2987 		return true;
2988 
2989 	d_op = path->dentry->d_op;
2990 	if (d_op == &ns_dentry_operations)
2991 		return true;
2992 
2993 	if (d_op == &pidfs_dentry_operations)
2994 		return true;
2995 
2996 	if (!is_mounted(path->mnt))
2997 		return false;
2998 
2999 	return check_anonymous_mnt(mnt);
3000 }
3001 
3002 
3003 static struct mount *__do_loopback(struct path *old_path, int recurse)
3004 {
3005 	struct mount *old = real_mount(old_path->mnt);
3006 
3007 	if (IS_MNT_UNBINDABLE(old))
3008 		return ERR_PTR(-EINVAL);
3009 
3010 	if (!may_copy_tree(old_path))
3011 		return ERR_PTR(-EINVAL);
3012 
3013 	if (!recurse && __has_locked_children(old, old_path->dentry))
3014 		return ERR_PTR(-EINVAL);
3015 
3016 	if (recurse)
3017 		return copy_tree(old, old_path->dentry, CL_COPY_MNT_NS_FILE);
3018 	else
3019 		return clone_mnt(old, old_path->dentry, 0);
3020 }
3021 
3022 /*
3023  * do loopback mount.
3024  */
3025 static int do_loopback(struct path *path, const char *old_name,
3026 				int recurse)
3027 {
3028 	struct path old_path;
3029 	struct mount *mnt = NULL, *parent;
3030 	struct pinned_mountpoint mp = {};
3031 	int err;
3032 	if (!old_name || !*old_name)
3033 		return -EINVAL;
3034 	err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
3035 	if (err)
3036 		return err;
3037 
3038 	err = -EINVAL;
3039 	if (mnt_ns_loop(old_path.dentry))
3040 		goto out;
3041 
3042 	err = lock_mount(path, &mp);
3043 	if (err)
3044 		goto out;
3045 
3046 	parent = real_mount(path->mnt);
3047 	if (!check_mnt(parent))
3048 		goto out2;
3049 
3050 	mnt = __do_loopback(&old_path, recurse);
3051 	if (IS_ERR(mnt)) {
3052 		err = PTR_ERR(mnt);
3053 		goto out2;
3054 	}
3055 
3056 	err = graft_tree(mnt, parent, mp.mp);
3057 	if (err) {
3058 		lock_mount_hash();
3059 		umount_tree(mnt, UMOUNT_SYNC);
3060 		unlock_mount_hash();
3061 	}
3062 out2:
3063 	unlock_mount(&mp);
3064 out:
3065 	path_put(&old_path);
3066 	return err;
3067 }
3068 
3069 static struct file *open_detached_copy(struct path *path, bool recursive)
3070 {
3071 	struct mnt_namespace *ns, *mnt_ns = current->nsproxy->mnt_ns, *src_mnt_ns;
3072 	struct user_namespace *user_ns = mnt_ns->user_ns;
3073 	struct mount *mnt, *p;
3074 	struct file *file;
3075 
3076 	ns = alloc_mnt_ns(user_ns, true);
3077 	if (IS_ERR(ns))
3078 		return ERR_CAST(ns);
3079 
3080 	namespace_lock();
3081 
3082 	/*
3083 	 * Record the sequence number of the source mount namespace.
3084 	 * This needs to hold namespace_sem to ensure that the mount
3085 	 * doesn't get attached.
3086 	 */
3087 	if (is_mounted(path->mnt)) {
3088 		src_mnt_ns = real_mount(path->mnt)->mnt_ns;
3089 		if (is_anon_ns(src_mnt_ns))
3090 			ns->seq_origin = src_mnt_ns->seq_origin;
3091 		else
3092 			ns->seq_origin = src_mnt_ns->seq;
3093 	}
3094 
3095 	mnt = __do_loopback(path, recursive);
3096 	if (IS_ERR(mnt)) {
3097 		namespace_unlock();
3098 		free_mnt_ns(ns);
3099 		return ERR_CAST(mnt);
3100 	}
3101 
3102 	lock_mount_hash();
3103 	for (p = mnt; p; p = next_mnt(p, mnt)) {
3104 		mnt_add_to_ns(ns, p);
3105 		ns->nr_mounts++;
3106 	}
3107 	ns->root = mnt;
3108 	mntget(&mnt->mnt);
3109 	unlock_mount_hash();
3110 	namespace_unlock();
3111 
3112 	mntput(path->mnt);
3113 	path->mnt = &mnt->mnt;
3114 	file = dentry_open(path, O_PATH, current_cred());
3115 	if (IS_ERR(file))
3116 		dissolve_on_fput(path->mnt);
3117 	else
3118 		file->f_mode |= FMODE_NEED_UNMOUNT;
3119 	return file;
3120 }
3121 
3122 static struct file *vfs_open_tree(int dfd, const char __user *filename, unsigned int flags)
3123 {
3124 	int ret;
3125 	struct path path __free(path_put) = {};
3126 	int lookup_flags = LOOKUP_AUTOMOUNT | LOOKUP_FOLLOW;
3127 	bool detached = flags & OPEN_TREE_CLONE;
3128 
3129 	BUILD_BUG_ON(OPEN_TREE_CLOEXEC != O_CLOEXEC);
3130 
3131 	if (flags & ~(AT_EMPTY_PATH | AT_NO_AUTOMOUNT | AT_RECURSIVE |
3132 		      AT_SYMLINK_NOFOLLOW | OPEN_TREE_CLONE |
3133 		      OPEN_TREE_CLOEXEC))
3134 		return ERR_PTR(-EINVAL);
3135 
3136 	if ((flags & (AT_RECURSIVE | OPEN_TREE_CLONE)) == AT_RECURSIVE)
3137 		return ERR_PTR(-EINVAL);
3138 
3139 	if (flags & AT_NO_AUTOMOUNT)
3140 		lookup_flags &= ~LOOKUP_AUTOMOUNT;
3141 	if (flags & AT_SYMLINK_NOFOLLOW)
3142 		lookup_flags &= ~LOOKUP_FOLLOW;
3143 	if (flags & AT_EMPTY_PATH)
3144 		lookup_flags |= LOOKUP_EMPTY;
3145 
3146 	if (detached && !may_mount())
3147 		return ERR_PTR(-EPERM);
3148 
3149 	ret = user_path_at(dfd, filename, lookup_flags, &path);
3150 	if (unlikely(ret))
3151 		return ERR_PTR(ret);
3152 
3153 	if (detached)
3154 		return open_detached_copy(&path, flags & AT_RECURSIVE);
3155 
3156 	return dentry_open(&path, O_PATH, current_cred());
3157 }
3158 
3159 SYSCALL_DEFINE3(open_tree, int, dfd, const char __user *, filename, unsigned, flags)
3160 {
3161 	int fd;
3162 	struct file *file __free(fput) = NULL;
3163 
3164 	file = vfs_open_tree(dfd, filename, flags);
3165 	if (IS_ERR(file))
3166 		return PTR_ERR(file);
3167 
3168 	fd = get_unused_fd_flags(flags & O_CLOEXEC);
3169 	if (fd < 0)
3170 		return fd;
3171 
3172 	fd_install(fd, no_free_ptr(file));
3173 	return fd;
3174 }
3175 
3176 /*
3177  * Don't allow locked mount flags to be cleared.
3178  *
3179  * No locks need to be held here while testing the various MNT_LOCK
3180  * flags because those flags can never be cleared once they are set.
3181  */
3182 static bool can_change_locked_flags(struct mount *mnt, unsigned int mnt_flags)
3183 {
3184 	unsigned int fl = mnt->mnt.mnt_flags;
3185 
3186 	if ((fl & MNT_LOCK_READONLY) &&
3187 	    !(mnt_flags & MNT_READONLY))
3188 		return false;
3189 
3190 	if ((fl & MNT_LOCK_NODEV) &&
3191 	    !(mnt_flags & MNT_NODEV))
3192 		return false;
3193 
3194 	if ((fl & MNT_LOCK_NOSUID) &&
3195 	    !(mnt_flags & MNT_NOSUID))
3196 		return false;
3197 
3198 	if ((fl & MNT_LOCK_NOEXEC) &&
3199 	    !(mnt_flags & MNT_NOEXEC))
3200 		return false;
3201 
3202 	if ((fl & MNT_LOCK_ATIME) &&
3203 	    ((fl & MNT_ATIME_MASK) != (mnt_flags & MNT_ATIME_MASK)))
3204 		return false;
3205 
3206 	return true;
3207 }
3208 
3209 static int change_mount_ro_state(struct mount *mnt, unsigned int mnt_flags)
3210 {
3211 	bool readonly_request = (mnt_flags & MNT_READONLY);
3212 
3213 	if (readonly_request == __mnt_is_readonly(&mnt->mnt))
3214 		return 0;
3215 
3216 	if (readonly_request)
3217 		return mnt_make_readonly(mnt);
3218 
3219 	mnt->mnt.mnt_flags &= ~MNT_READONLY;
3220 	return 0;
3221 }
3222 
3223 static void set_mount_attributes(struct mount *mnt, unsigned int mnt_flags)
3224 {
3225 	mnt_flags |= mnt->mnt.mnt_flags & ~MNT_USER_SETTABLE_MASK;
3226 	mnt->mnt.mnt_flags = mnt_flags;
3227 	touch_mnt_namespace(mnt->mnt_ns);
3228 }
3229 
3230 static void mnt_warn_timestamp_expiry(struct path *mountpoint, struct vfsmount *mnt)
3231 {
3232 	struct super_block *sb = mnt->mnt_sb;
3233 
3234 	if (!__mnt_is_readonly(mnt) &&
3235 	   (!(sb->s_iflags & SB_I_TS_EXPIRY_WARNED)) &&
3236 	   (ktime_get_real_seconds() + TIME_UPTIME_SEC_MAX > sb->s_time_max)) {
3237 		char *buf, *mntpath;
3238 
3239 		buf = (char *)__get_free_page(GFP_KERNEL);
3240 		if (buf)
3241 			mntpath = d_path(mountpoint, buf, PAGE_SIZE);
3242 		else
3243 			mntpath = ERR_PTR(-ENOMEM);
3244 		if (IS_ERR(mntpath))
3245 			mntpath = "(unknown)";
3246 
3247 		pr_warn("%s filesystem being %s at %s supports timestamps until %ptTd (0x%llx)\n",
3248 			sb->s_type->name,
3249 			is_mounted(mnt) ? "remounted" : "mounted",
3250 			mntpath, &sb->s_time_max,
3251 			(unsigned long long)sb->s_time_max);
3252 
3253 		sb->s_iflags |= SB_I_TS_EXPIRY_WARNED;
3254 		if (buf)
3255 			free_page((unsigned long)buf);
3256 	}
3257 }
3258 
3259 /*
3260  * Handle reconfiguration of the mountpoint only without alteration of the
3261  * superblock it refers to.  This is triggered by specifying MS_REMOUNT|MS_BIND
3262  * to mount(2).
3263  */
3264 static int do_reconfigure_mnt(struct path *path, unsigned int mnt_flags)
3265 {
3266 	struct super_block *sb = path->mnt->mnt_sb;
3267 	struct mount *mnt = real_mount(path->mnt);
3268 	int ret;
3269 
3270 	if (!check_mnt(mnt))
3271 		return -EINVAL;
3272 
3273 	if (!path_mounted(path))
3274 		return -EINVAL;
3275 
3276 	if (!can_change_locked_flags(mnt, mnt_flags))
3277 		return -EPERM;
3278 
3279 	/*
3280 	 * We're only checking whether the superblock is read-only not
3281 	 * changing it, so only take down_read(&sb->s_umount).
3282 	 */
3283 	down_read(&sb->s_umount);
3284 	lock_mount_hash();
3285 	ret = change_mount_ro_state(mnt, mnt_flags);
3286 	if (ret == 0)
3287 		set_mount_attributes(mnt, mnt_flags);
3288 	unlock_mount_hash();
3289 	up_read(&sb->s_umount);
3290 
3291 	mnt_warn_timestamp_expiry(path, &mnt->mnt);
3292 
3293 	return ret;
3294 }
3295 
3296 /*
3297  * change filesystem flags. dir should be a physical root of filesystem.
3298  * If you've mounted a non-root directory somewhere and want to do remount
3299  * on it - tough luck.
3300  */
3301 static int do_remount(struct path *path, int ms_flags, int sb_flags,
3302 		      int mnt_flags, void *data)
3303 {
3304 	int err;
3305 	struct super_block *sb = path->mnt->mnt_sb;
3306 	struct mount *mnt = real_mount(path->mnt);
3307 	struct fs_context *fc;
3308 
3309 	if (!check_mnt(mnt))
3310 		return -EINVAL;
3311 
3312 	if (!path_mounted(path))
3313 		return -EINVAL;
3314 
3315 	if (!can_change_locked_flags(mnt, mnt_flags))
3316 		return -EPERM;
3317 
3318 	fc = fs_context_for_reconfigure(path->dentry, sb_flags, MS_RMT_MASK);
3319 	if (IS_ERR(fc))
3320 		return PTR_ERR(fc);
3321 
3322 	/*
3323 	 * Indicate to the filesystem that the remount request is coming
3324 	 * from the legacy mount system call.
3325 	 */
3326 	fc->oldapi = true;
3327 
3328 	err = parse_monolithic_mount_data(fc, data);
3329 	if (!err) {
3330 		down_write(&sb->s_umount);
3331 		err = -EPERM;
3332 		if (ns_capable(sb->s_user_ns, CAP_SYS_ADMIN)) {
3333 			err = reconfigure_super(fc);
3334 			if (!err) {
3335 				lock_mount_hash();
3336 				set_mount_attributes(mnt, mnt_flags);
3337 				unlock_mount_hash();
3338 			}
3339 		}
3340 		up_write(&sb->s_umount);
3341 	}
3342 
3343 	mnt_warn_timestamp_expiry(path, &mnt->mnt);
3344 
3345 	put_fs_context(fc);
3346 	return err;
3347 }
3348 
3349 static inline int tree_contains_unbindable(struct mount *mnt)
3350 {
3351 	struct mount *p;
3352 	for (p = mnt; p; p = next_mnt(p, mnt)) {
3353 		if (IS_MNT_UNBINDABLE(p))
3354 			return 1;
3355 	}
3356 	return 0;
3357 }
3358 
3359 static int do_set_group(struct path *from_path, struct path *to_path)
3360 {
3361 	struct mount *from, *to;
3362 	int err;
3363 
3364 	from = real_mount(from_path->mnt);
3365 	to = real_mount(to_path->mnt);
3366 
3367 	namespace_lock();
3368 
3369 	err = may_change_propagation(from);
3370 	if (err)
3371 		goto out;
3372 	err = may_change_propagation(to);
3373 	if (err)
3374 		goto out;
3375 
3376 	err = -EINVAL;
3377 	/* To and From paths should be mount roots */
3378 	if (!path_mounted(from_path))
3379 		goto out;
3380 	if (!path_mounted(to_path))
3381 		goto out;
3382 
3383 	/* Setting sharing groups is only allowed across same superblock */
3384 	if (from->mnt.mnt_sb != to->mnt.mnt_sb)
3385 		goto out;
3386 
3387 	/* From mount root should be wider than To mount root */
3388 	if (!is_subdir(to->mnt.mnt_root, from->mnt.mnt_root))
3389 		goto out;
3390 
3391 	/* From mount should not have locked children in place of To's root */
3392 	if (__has_locked_children(from, to->mnt.mnt_root))
3393 		goto out;
3394 
3395 	/* Setting sharing groups is only allowed on private mounts */
3396 	if (IS_MNT_SHARED(to) || IS_MNT_SLAVE(to))
3397 		goto out;
3398 
3399 	/* From should not be private */
3400 	if (!IS_MNT_SHARED(from) && !IS_MNT_SLAVE(from))
3401 		goto out;
3402 
3403 	if (IS_MNT_SLAVE(from)) {
3404 		hlist_add_behind(&to->mnt_slave, &from->mnt_slave);
3405 		to->mnt_master = from->mnt_master;
3406 	}
3407 
3408 	if (IS_MNT_SHARED(from)) {
3409 		to->mnt_group_id = from->mnt_group_id;
3410 		list_add(&to->mnt_share, &from->mnt_share);
3411 		set_mnt_shared(to);
3412 	}
3413 
3414 	err = 0;
3415 out:
3416 	namespace_unlock();
3417 	return err;
3418 }
3419 
3420 /**
3421  * path_overmounted - check if path is overmounted
3422  * @path: path to check
3423  *
3424  * Check if path is overmounted, i.e., if there's a mount on top of
3425  * @path->mnt with @path->dentry as mountpoint.
3426  *
3427  * Context: namespace_sem must be held at least shared.
3428  * MUST NOT be called under lock_mount_hash() (there one should just
3429  * call __lookup_mnt() and check if it returns NULL).
3430  * Return: If path is overmounted true is returned, false if not.
3431  */
3432 static inline bool path_overmounted(const struct path *path)
3433 {
3434 	unsigned seq = read_seqbegin(&mount_lock);
3435 	bool no_child;
3436 
3437 	rcu_read_lock();
3438 	no_child = !__lookup_mnt(path->mnt, path->dentry);
3439 	rcu_read_unlock();
3440 	if (need_seqretry(&mount_lock, seq)) {
3441 		read_seqlock_excl(&mount_lock);
3442 		no_child = !__lookup_mnt(path->mnt, path->dentry);
3443 		read_sequnlock_excl(&mount_lock);
3444 	}
3445 	return unlikely(!no_child);
3446 }
3447 
3448 /*
3449  * Check if there is a possibly empty chain of descent from p1 to p2.
3450  * Locks: namespace_sem (shared) or mount_lock (read_seqlock_excl).
3451  */
3452 static bool mount_is_ancestor(const struct mount *p1, const struct mount *p2)
3453 {
3454 	while (p2 != p1 && mnt_has_parent(p2))
3455 		p2 = p2->mnt_parent;
3456 	return p2 == p1;
3457 }
3458 
3459 /**
3460  * can_move_mount_beneath - check that we can mount beneath the top mount
3461  * @from: mount to mount beneath
3462  * @to:   mount under which to mount
3463  * @mp:   mountpoint of @to
3464  *
3465  * - Make sure that @to->dentry is actually the root of a mount under
3466  *   which we can mount another mount.
3467  * - Make sure that nothing can be mounted beneath the caller's current
3468  *   root or the rootfs of the namespace.
3469  * - Make sure that the caller can unmount the topmost mount ensuring
3470  *   that the caller could reveal the underlying mountpoint.
3471  * - Ensure that nothing has been mounted on top of @from before we
3472  *   grabbed @namespace_sem to avoid creating pointless shadow mounts.
3473  * - Prevent mounting beneath a mount if the propagation relationship
3474  *   between the source mount, parent mount, and top mount would lead to
3475  *   nonsensical mount trees.
3476  *
3477  * Context: This function expects namespace_lock() to be held.
3478  * Return: On success 0, and on error a negative error code is returned.
3479  */
3480 static int can_move_mount_beneath(const struct path *from,
3481 				  const struct path *to,
3482 				  const struct mountpoint *mp)
3483 {
3484 	struct mount *mnt_from = real_mount(from->mnt),
3485 		     *mnt_to = real_mount(to->mnt),
3486 		     *parent_mnt_to = mnt_to->mnt_parent;
3487 
3488 	if (!mnt_has_parent(mnt_to))
3489 		return -EINVAL;
3490 
3491 	if (!path_mounted(to))
3492 		return -EINVAL;
3493 
3494 	if (IS_MNT_LOCKED(mnt_to))
3495 		return -EINVAL;
3496 
3497 	/* Avoid creating shadow mounts during mount propagation. */
3498 	if (path_overmounted(from))
3499 		return -EINVAL;
3500 
3501 	/*
3502 	 * Mounting beneath the rootfs only makes sense when the
3503 	 * semantics of pivot_root(".", ".") are used.
3504 	 */
3505 	if (&mnt_to->mnt == current->fs->root.mnt)
3506 		return -EINVAL;
3507 	if (parent_mnt_to == current->nsproxy->mnt_ns->root)
3508 		return -EINVAL;
3509 
3510 	if (mount_is_ancestor(mnt_to, mnt_from))
3511 		return -EINVAL;
3512 
3513 	/*
3514 	 * If the parent mount propagates to the child mount this would
3515 	 * mean mounting @mnt_from on @mnt_to->mnt_parent and then
3516 	 * propagating a copy @c of @mnt_from on top of @mnt_to. This
3517 	 * defeats the whole purpose of mounting beneath another mount.
3518 	 */
3519 	if (propagation_would_overmount(parent_mnt_to, mnt_to, mp))
3520 		return -EINVAL;
3521 
3522 	/*
3523 	 * If @mnt_to->mnt_parent propagates to @mnt_from this would
3524 	 * mean propagating a copy @c of @mnt_from on top of @mnt_from.
3525 	 * Afterwards @mnt_from would be mounted on top of
3526 	 * @mnt_to->mnt_parent and @mnt_to would be unmounted from
3527 	 * @mnt->mnt_parent and remounted on @mnt_from. But since @c is
3528 	 * already mounted on @mnt_from, @mnt_to would ultimately be
3529 	 * remounted on top of @c. Afterwards, @mnt_from would be
3530 	 * covered by a copy @c of @mnt_from and @c would be covered by
3531 	 * @mnt_from itself. This defeats the whole purpose of mounting
3532 	 * @mnt_from beneath @mnt_to.
3533 	 */
3534 	if (check_mnt(mnt_from) &&
3535 	    propagation_would_overmount(parent_mnt_to, mnt_from, mp))
3536 		return -EINVAL;
3537 
3538 	return 0;
3539 }
3540 
3541 /* may_use_mount() - check if a mount tree can be used
3542  * @mnt: vfsmount to be used
3543  *
3544  * This helper checks if the caller may use the mount tree starting
3545  * from @path->mnt. The caller may use the mount tree under the
3546  * following circumstances:
3547  *
3548  * (1) The caller is located in the mount namespace of the mount tree.
3549  *     This also implies that the mount does not belong to an anonymous
3550  *     mount namespace.
3551  * (2) The caller is trying to use a mount tree that belongs to an
3552  *     anonymous mount namespace.
3553  *
3554  *     For that to be safe, this helper enforces that the origin mount
3555  *     namespace the anonymous mount namespace was created from is the
3556  *     same as the caller's mount namespace by comparing the sequence
3557  *     numbers.
3558  *
3559  *     The ownership of a non-anonymous mount namespace such as the
3560  *     caller's cannot change.
3561  *     => We know that the caller's mount namespace is stable.
3562  *
3563  *     If the origin sequence number of the anonymous mount namespace is
3564  *     the same as the sequence number of the caller's mount namespace.
3565  *     => The owning namespaces are the same.
3566  *
3567  *     ==> The earlier capability check on the owning namespace of the
3568  *         caller's mount namespace ensures that the caller has the
3569  *         ability to use the mount tree.
3570  *
3571  * Returns true if the mount tree can be used, false otherwise.
3572  */
3573 static inline bool may_use_mount(struct mount *mnt)
3574 {
3575 	if (check_mnt(mnt))
3576 		return true;
3577 
3578 	/*
3579 	 * Make sure that noone unmounted the target path or somehow
3580 	 * managed to get their hands on something purely kernel
3581 	 * internal.
3582 	 */
3583 	if (!is_mounted(&mnt->mnt))
3584 		return false;
3585 
3586 	return check_anonymous_mnt(mnt);
3587 }
3588 
3589 static int do_move_mount(struct path *old_path,
3590 			 struct path *new_path, enum mnt_tree_flags_t flags)
3591 {
3592 	struct mnt_namespace *ns;
3593 	struct mount *p;
3594 	struct mount *old;
3595 	struct mount *parent;
3596 	struct pinned_mountpoint mp;
3597 	int err;
3598 	bool beneath = flags & MNT_TREE_BENEATH;
3599 
3600 	err = do_lock_mount(new_path, &mp, beneath);
3601 	if (err)
3602 		return err;
3603 
3604 	old = real_mount(old_path->mnt);
3605 	p = real_mount(new_path->mnt);
3606 	parent = old->mnt_parent;
3607 	ns = old->mnt_ns;
3608 
3609 	err = -EINVAL;
3610 
3611 	if (check_mnt(old)) {
3612 		/* if the source is in our namespace... */
3613 		/* ... it should be detachable from parent */
3614 		if (!mnt_has_parent(old) || IS_MNT_LOCKED(old))
3615 			goto out;
3616 		/* ... and the target should be in our namespace */
3617 		if (!check_mnt(p))
3618 			goto out;
3619 		/* parent of the source should not be shared */
3620 		if (IS_MNT_SHARED(parent))
3621 			goto out;
3622 	} else {
3623 		/*
3624 		 * otherwise the source must be the root of some anon namespace.
3625 		 */
3626 		if (!anon_ns_root(old))
3627 			goto out;
3628 		/*
3629 		 * Bail out early if the target is within the same namespace -
3630 		 * subsequent checks would've rejected that, but they lose
3631 		 * some corner cases if we check it early.
3632 		 */
3633 		if (ns == p->mnt_ns)
3634 			goto out;
3635 		/*
3636 		 * Target should be either in our namespace or in an acceptable
3637 		 * anon namespace, sensu check_anonymous_mnt().
3638 		 */
3639 		if (!may_use_mount(p))
3640 			goto out;
3641 	}
3642 
3643 	if (!path_mounted(old_path))
3644 		goto out;
3645 
3646 	if (d_is_dir(new_path->dentry) !=
3647 	    d_is_dir(old_path->dentry))
3648 		goto out;
3649 
3650 	if (beneath) {
3651 		err = can_move_mount_beneath(old_path, new_path, mp.mp);
3652 		if (err)
3653 			goto out;
3654 
3655 		err = -EINVAL;
3656 		p = p->mnt_parent;
3657 	}
3658 
3659 	/*
3660 	 * Don't move a mount tree containing unbindable mounts to a destination
3661 	 * mount which is shared.
3662 	 */
3663 	if (IS_MNT_SHARED(p) && tree_contains_unbindable(old))
3664 		goto out;
3665 	err = -ELOOP;
3666 	if (!check_for_nsfs_mounts(old))
3667 		goto out;
3668 	if (mount_is_ancestor(old, p))
3669 		goto out;
3670 
3671 	err = attach_recursive_mnt(old, p, mp.mp);
3672 out:
3673 	unlock_mount(&mp);
3674 	return err;
3675 }
3676 
3677 static int do_move_mount_old(struct path *path, const char *old_name)
3678 {
3679 	struct path old_path;
3680 	int err;
3681 
3682 	if (!old_name || !*old_name)
3683 		return -EINVAL;
3684 
3685 	err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
3686 	if (err)
3687 		return err;
3688 
3689 	err = do_move_mount(&old_path, path, 0);
3690 	path_put(&old_path);
3691 	return err;
3692 }
3693 
3694 /*
3695  * add a mount into a namespace's mount tree
3696  */
3697 static int do_add_mount(struct mount *newmnt, struct mountpoint *mp,
3698 			const struct path *path, int mnt_flags)
3699 {
3700 	struct mount *parent = real_mount(path->mnt);
3701 
3702 	mnt_flags &= ~MNT_INTERNAL_FLAGS;
3703 
3704 	if (unlikely(!check_mnt(parent))) {
3705 		/* that's acceptable only for automounts done in private ns */
3706 		if (!(mnt_flags & MNT_SHRINKABLE))
3707 			return -EINVAL;
3708 		/* ... and for those we'd better have mountpoint still alive */
3709 		if (!parent->mnt_ns)
3710 			return -EINVAL;
3711 	}
3712 
3713 	/* Refuse the same filesystem on the same mount point */
3714 	if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb && path_mounted(path))
3715 		return -EBUSY;
3716 
3717 	if (d_is_symlink(newmnt->mnt.mnt_root))
3718 		return -EINVAL;
3719 
3720 	newmnt->mnt.mnt_flags = mnt_flags;
3721 	return graft_tree(newmnt, parent, mp);
3722 }
3723 
3724 static bool mount_too_revealing(const struct super_block *sb, int *new_mnt_flags);
3725 
3726 /*
3727  * Create a new mount using a superblock configuration and request it
3728  * be added to the namespace tree.
3729  */
3730 static int do_new_mount_fc(struct fs_context *fc, struct path *mountpoint,
3731 			   unsigned int mnt_flags)
3732 {
3733 	struct vfsmount *mnt;
3734 	struct pinned_mountpoint mp = {};
3735 	struct super_block *sb = fc->root->d_sb;
3736 	int error;
3737 
3738 	error = security_sb_kern_mount(sb);
3739 	if (!error && mount_too_revealing(sb, &mnt_flags))
3740 		error = -EPERM;
3741 
3742 	if (unlikely(error)) {
3743 		fc_drop_locked(fc);
3744 		return error;
3745 	}
3746 
3747 	up_write(&sb->s_umount);
3748 
3749 	mnt = vfs_create_mount(fc);
3750 	if (IS_ERR(mnt))
3751 		return PTR_ERR(mnt);
3752 
3753 	mnt_warn_timestamp_expiry(mountpoint, mnt);
3754 
3755 	error = lock_mount(mountpoint, &mp);
3756 	if (!error) {
3757 		error = do_add_mount(real_mount(mnt), mp.mp,
3758 				     mountpoint, mnt_flags);
3759 		unlock_mount(&mp);
3760 	}
3761 	if (error < 0)
3762 		mntput(mnt);
3763 	return error;
3764 }
3765 
3766 /*
3767  * create a new mount for userspace and request it to be added into the
3768  * namespace's tree
3769  */
3770 static int do_new_mount(struct path *path, const char *fstype, int sb_flags,
3771 			int mnt_flags, const char *name, void *data)
3772 {
3773 	struct file_system_type *type;
3774 	struct fs_context *fc;
3775 	const char *subtype = NULL;
3776 	int err = 0;
3777 
3778 	if (!fstype)
3779 		return -EINVAL;
3780 
3781 	type = get_fs_type(fstype);
3782 	if (!type)
3783 		return -ENODEV;
3784 
3785 	if (type->fs_flags & FS_HAS_SUBTYPE) {
3786 		subtype = strchr(fstype, '.');
3787 		if (subtype) {
3788 			subtype++;
3789 			if (!*subtype) {
3790 				put_filesystem(type);
3791 				return -EINVAL;
3792 			}
3793 		}
3794 	}
3795 
3796 	fc = fs_context_for_mount(type, sb_flags);
3797 	put_filesystem(type);
3798 	if (IS_ERR(fc))
3799 		return PTR_ERR(fc);
3800 
3801 	/*
3802 	 * Indicate to the filesystem that the mount request is coming
3803 	 * from the legacy mount system call.
3804 	 */
3805 	fc->oldapi = true;
3806 
3807 	if (subtype)
3808 		err = vfs_parse_fs_string(fc, "subtype",
3809 					  subtype, strlen(subtype));
3810 	if (!err && name)
3811 		err = vfs_parse_fs_string(fc, "source", name, strlen(name));
3812 	if (!err)
3813 		err = parse_monolithic_mount_data(fc, data);
3814 	if (!err && !mount_capable(fc))
3815 		err = -EPERM;
3816 	if (!err)
3817 		err = vfs_get_tree(fc);
3818 	if (!err)
3819 		err = do_new_mount_fc(fc, path, mnt_flags);
3820 
3821 	put_fs_context(fc);
3822 	return err;
3823 }
3824 
3825 int finish_automount(struct vfsmount *m, const struct path *path)
3826 {
3827 	struct dentry *dentry = path->dentry;
3828 	struct pinned_mountpoint mp = {};
3829 	struct mount *mnt;
3830 	int err;
3831 
3832 	if (!m)
3833 		return 0;
3834 	if (IS_ERR(m))
3835 		return PTR_ERR(m);
3836 
3837 	mnt = real_mount(m);
3838 
3839 	if (m->mnt_sb == path->mnt->mnt_sb &&
3840 	    m->mnt_root == dentry) {
3841 		err = -ELOOP;
3842 		goto discard;
3843 	}
3844 
3845 	/*
3846 	 * we don't want to use lock_mount() - in this case finding something
3847 	 * that overmounts our mountpoint to be means "quitely drop what we've
3848 	 * got", not "try to mount it on top".
3849 	 */
3850 	inode_lock(dentry->d_inode);
3851 	namespace_lock();
3852 	if (unlikely(cant_mount(dentry))) {
3853 		err = -ENOENT;
3854 		goto discard_locked;
3855 	}
3856 	if (path_overmounted(path)) {
3857 		err = 0;
3858 		goto discard_locked;
3859 	}
3860 	err = get_mountpoint(dentry, &mp);
3861 	if (err)
3862 		goto discard_locked;
3863 
3864 	err = do_add_mount(mnt, mp.mp, path,
3865 			   path->mnt->mnt_flags | MNT_SHRINKABLE);
3866 	unlock_mount(&mp);
3867 	if (unlikely(err))
3868 		goto discard;
3869 	return 0;
3870 
3871 discard_locked:
3872 	namespace_unlock();
3873 	inode_unlock(dentry->d_inode);
3874 discard:
3875 	mntput(m);
3876 	return err;
3877 }
3878 
3879 /**
3880  * mnt_set_expiry - Put a mount on an expiration list
3881  * @mnt: The mount to list.
3882  * @expiry_list: The list to add the mount to.
3883  */
3884 void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
3885 {
3886 	read_seqlock_excl(&mount_lock);
3887 	list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list);
3888 	read_sequnlock_excl(&mount_lock);
3889 }
3890 EXPORT_SYMBOL(mnt_set_expiry);
3891 
3892 /*
3893  * process a list of expirable mountpoints with the intent of discarding any
3894  * mountpoints that aren't in use and haven't been touched since last we came
3895  * here
3896  */
3897 void mark_mounts_for_expiry(struct list_head *mounts)
3898 {
3899 	struct mount *mnt, *next;
3900 	LIST_HEAD(graveyard);
3901 
3902 	if (list_empty(mounts))
3903 		return;
3904 
3905 	namespace_lock();
3906 	lock_mount_hash();
3907 
3908 	/* extract from the expiration list every vfsmount that matches the
3909 	 * following criteria:
3910 	 * - already mounted
3911 	 * - only referenced by its parent vfsmount
3912 	 * - still marked for expiry (marked on the last call here; marks are
3913 	 *   cleared by mntput())
3914 	 */
3915 	list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
3916 		if (!is_mounted(&mnt->mnt))
3917 			continue;
3918 		if (!xchg(&mnt->mnt_expiry_mark, 1) ||
3919 			propagate_mount_busy(mnt, 1))
3920 			continue;
3921 		list_move(&mnt->mnt_expire, &graveyard);
3922 	}
3923 	while (!list_empty(&graveyard)) {
3924 		mnt = list_first_entry(&graveyard, struct mount, mnt_expire);
3925 		touch_mnt_namespace(mnt->mnt_ns);
3926 		umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
3927 	}
3928 	unlock_mount_hash();
3929 	namespace_unlock();
3930 }
3931 
3932 EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
3933 
3934 /*
3935  * Ripoff of 'select_parent()'
3936  *
3937  * search the list of submounts for a given mountpoint, and move any
3938  * shrinkable submounts to the 'graveyard' list.
3939  */
3940 static int select_submounts(struct mount *parent, struct list_head *graveyard)
3941 {
3942 	struct mount *this_parent = parent;
3943 	struct list_head *next;
3944 	int found = 0;
3945 
3946 repeat:
3947 	next = this_parent->mnt_mounts.next;
3948 resume:
3949 	while (next != &this_parent->mnt_mounts) {
3950 		struct list_head *tmp = next;
3951 		struct mount *mnt = list_entry(tmp, struct mount, mnt_child);
3952 
3953 		next = tmp->next;
3954 		if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE))
3955 			continue;
3956 		/*
3957 		 * Descend a level if the d_mounts list is non-empty.
3958 		 */
3959 		if (!list_empty(&mnt->mnt_mounts)) {
3960 			this_parent = mnt;
3961 			goto repeat;
3962 		}
3963 
3964 		if (!propagate_mount_busy(mnt, 1)) {
3965 			list_move_tail(&mnt->mnt_expire, graveyard);
3966 			found++;
3967 		}
3968 	}
3969 	/*
3970 	 * All done at this level ... ascend and resume the search
3971 	 */
3972 	if (this_parent != parent) {
3973 		next = this_parent->mnt_child.next;
3974 		this_parent = this_parent->mnt_parent;
3975 		goto resume;
3976 	}
3977 	return found;
3978 }
3979 
3980 /*
3981  * process a list of expirable mountpoints with the intent of discarding any
3982  * submounts of a specific parent mountpoint
3983  *
3984  * mount_lock must be held for write
3985  */
3986 static void shrink_submounts(struct mount *mnt)
3987 {
3988 	LIST_HEAD(graveyard);
3989 	struct mount *m;
3990 
3991 	/* extract submounts of 'mountpoint' from the expiration list */
3992 	while (select_submounts(mnt, &graveyard)) {
3993 		while (!list_empty(&graveyard)) {
3994 			m = list_first_entry(&graveyard, struct mount,
3995 						mnt_expire);
3996 			touch_mnt_namespace(m->mnt_ns);
3997 			umount_tree(m, UMOUNT_PROPAGATE|UMOUNT_SYNC);
3998 		}
3999 	}
4000 }
4001 
4002 static void *copy_mount_options(const void __user * data)
4003 {
4004 	char *copy;
4005 	unsigned left, offset;
4006 
4007 	if (!data)
4008 		return NULL;
4009 
4010 	copy = kmalloc(PAGE_SIZE, GFP_KERNEL);
4011 	if (!copy)
4012 		return ERR_PTR(-ENOMEM);
4013 
4014 	left = copy_from_user(copy, data, PAGE_SIZE);
4015 
4016 	/*
4017 	 * Not all architectures have an exact copy_from_user(). Resort to
4018 	 * byte at a time.
4019 	 */
4020 	offset = PAGE_SIZE - left;
4021 	while (left) {
4022 		char c;
4023 		if (get_user(c, (const char __user *)data + offset))
4024 			break;
4025 		copy[offset] = c;
4026 		left--;
4027 		offset++;
4028 	}
4029 
4030 	if (left == PAGE_SIZE) {
4031 		kfree(copy);
4032 		return ERR_PTR(-EFAULT);
4033 	}
4034 
4035 	return copy;
4036 }
4037 
4038 static char *copy_mount_string(const void __user *data)
4039 {
4040 	return data ? strndup_user(data, PATH_MAX) : NULL;
4041 }
4042 
4043 /*
4044  * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
4045  * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
4046  *
4047  * data is a (void *) that can point to any structure up to
4048  * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
4049  * information (or be NULL).
4050  *
4051  * Pre-0.97 versions of mount() didn't have a flags word.
4052  * When the flags word was introduced its top half was required
4053  * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
4054  * Therefore, if this magic number is present, it carries no information
4055  * and must be discarded.
4056  */
4057 int path_mount(const char *dev_name, struct path *path,
4058 		const char *type_page, unsigned long flags, void *data_page)
4059 {
4060 	unsigned int mnt_flags = 0, sb_flags;
4061 	int ret;
4062 
4063 	/* Discard magic */
4064 	if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
4065 		flags &= ~MS_MGC_MSK;
4066 
4067 	/* Basic sanity checks */
4068 	if (data_page)
4069 		((char *)data_page)[PAGE_SIZE - 1] = 0;
4070 
4071 	if (flags & MS_NOUSER)
4072 		return -EINVAL;
4073 
4074 	ret = security_sb_mount(dev_name, path, type_page, flags, data_page);
4075 	if (ret)
4076 		return ret;
4077 	if (!may_mount())
4078 		return -EPERM;
4079 	if (flags & SB_MANDLOCK)
4080 		warn_mandlock();
4081 
4082 	/* Default to relatime unless overriden */
4083 	if (!(flags & MS_NOATIME))
4084 		mnt_flags |= MNT_RELATIME;
4085 
4086 	/* Separate the per-mountpoint flags */
4087 	if (flags & MS_NOSUID)
4088 		mnt_flags |= MNT_NOSUID;
4089 	if (flags & MS_NODEV)
4090 		mnt_flags |= MNT_NODEV;
4091 	if (flags & MS_NOEXEC)
4092 		mnt_flags |= MNT_NOEXEC;
4093 	if (flags & MS_NOATIME)
4094 		mnt_flags |= MNT_NOATIME;
4095 	if (flags & MS_NODIRATIME)
4096 		mnt_flags |= MNT_NODIRATIME;
4097 	if (flags & MS_STRICTATIME)
4098 		mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
4099 	if (flags & MS_RDONLY)
4100 		mnt_flags |= MNT_READONLY;
4101 	if (flags & MS_NOSYMFOLLOW)
4102 		mnt_flags |= MNT_NOSYMFOLLOW;
4103 
4104 	/* The default atime for remount is preservation */
4105 	if ((flags & MS_REMOUNT) &&
4106 	    ((flags & (MS_NOATIME | MS_NODIRATIME | MS_RELATIME |
4107 		       MS_STRICTATIME)) == 0)) {
4108 		mnt_flags &= ~MNT_ATIME_MASK;
4109 		mnt_flags |= path->mnt->mnt_flags & MNT_ATIME_MASK;
4110 	}
4111 
4112 	sb_flags = flags & (SB_RDONLY |
4113 			    SB_SYNCHRONOUS |
4114 			    SB_MANDLOCK |
4115 			    SB_DIRSYNC |
4116 			    SB_SILENT |
4117 			    SB_POSIXACL |
4118 			    SB_LAZYTIME |
4119 			    SB_I_VERSION);
4120 
4121 	if ((flags & (MS_REMOUNT | MS_BIND)) == (MS_REMOUNT | MS_BIND))
4122 		return do_reconfigure_mnt(path, mnt_flags);
4123 	if (flags & MS_REMOUNT)
4124 		return do_remount(path, flags, sb_flags, mnt_flags, data_page);
4125 	if (flags & MS_BIND)
4126 		return do_loopback(path, dev_name, flags & MS_REC);
4127 	if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
4128 		return do_change_type(path, flags);
4129 	if (flags & MS_MOVE)
4130 		return do_move_mount_old(path, dev_name);
4131 
4132 	return do_new_mount(path, type_page, sb_flags, mnt_flags, dev_name,
4133 			    data_page);
4134 }
4135 
4136 int do_mount(const char *dev_name, const char __user *dir_name,
4137 		const char *type_page, unsigned long flags, void *data_page)
4138 {
4139 	struct path path;
4140 	int ret;
4141 
4142 	ret = user_path_at(AT_FDCWD, dir_name, LOOKUP_FOLLOW, &path);
4143 	if (ret)
4144 		return ret;
4145 	ret = path_mount(dev_name, &path, type_page, flags, data_page);
4146 	path_put(&path);
4147 	return ret;
4148 }
4149 
4150 static struct ucounts *inc_mnt_namespaces(struct user_namespace *ns)
4151 {
4152 	return inc_ucount(ns, current_euid(), UCOUNT_MNT_NAMESPACES);
4153 }
4154 
4155 static void dec_mnt_namespaces(struct ucounts *ucounts)
4156 {
4157 	dec_ucount(ucounts, UCOUNT_MNT_NAMESPACES);
4158 }
4159 
4160 static void free_mnt_ns(struct mnt_namespace *ns)
4161 {
4162 	if (!is_anon_ns(ns))
4163 		ns_free_inum(&ns->ns);
4164 	dec_mnt_namespaces(ns->ucounts);
4165 	mnt_ns_tree_remove(ns);
4166 }
4167 
4168 /*
4169  * Assign a sequence number so we can detect when we attempt to bind
4170  * mount a reference to an older mount namespace into the current
4171  * mount namespace, preventing reference counting loops.  A 64bit
4172  * number incrementing at 10Ghz will take 12,427 years to wrap which
4173  * is effectively never, so we can ignore the possibility.
4174  */
4175 static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1);
4176 
4177 static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool anon)
4178 {
4179 	struct mnt_namespace *new_ns;
4180 	struct ucounts *ucounts;
4181 	int ret;
4182 
4183 	ucounts = inc_mnt_namespaces(user_ns);
4184 	if (!ucounts)
4185 		return ERR_PTR(-ENOSPC);
4186 
4187 	new_ns = kzalloc(sizeof(struct mnt_namespace), GFP_KERNEL_ACCOUNT);
4188 	if (!new_ns) {
4189 		dec_mnt_namespaces(ucounts);
4190 		return ERR_PTR(-ENOMEM);
4191 	}
4192 	if (!anon) {
4193 		ret = ns_alloc_inum(&new_ns->ns);
4194 		if (ret) {
4195 			kfree(new_ns);
4196 			dec_mnt_namespaces(ucounts);
4197 			return ERR_PTR(ret);
4198 		}
4199 	}
4200 	new_ns->ns.ops = &mntns_operations;
4201 	if (!anon)
4202 		new_ns->seq = atomic64_inc_return(&mnt_ns_seq);
4203 	refcount_set(&new_ns->ns.count, 1);
4204 	refcount_set(&new_ns->passive, 1);
4205 	new_ns->mounts = RB_ROOT;
4206 	INIT_LIST_HEAD(&new_ns->mnt_ns_list);
4207 	RB_CLEAR_NODE(&new_ns->mnt_ns_tree_node);
4208 	init_waitqueue_head(&new_ns->poll);
4209 	new_ns->user_ns = get_user_ns(user_ns);
4210 	new_ns->ucounts = ucounts;
4211 	return new_ns;
4212 }
4213 
4214 __latent_entropy
4215 struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
4216 		struct user_namespace *user_ns, struct fs_struct *new_fs)
4217 {
4218 	struct mnt_namespace *new_ns;
4219 	struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
4220 	struct mount *p, *q;
4221 	struct mount *old;
4222 	struct mount *new;
4223 	int copy_flags;
4224 
4225 	BUG_ON(!ns);
4226 
4227 	if (likely(!(flags & CLONE_NEWNS))) {
4228 		get_mnt_ns(ns);
4229 		return ns;
4230 	}
4231 
4232 	old = ns->root;
4233 
4234 	new_ns = alloc_mnt_ns(user_ns, false);
4235 	if (IS_ERR(new_ns))
4236 		return new_ns;
4237 
4238 	namespace_lock();
4239 	/* First pass: copy the tree topology */
4240 	copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE;
4241 	if (user_ns != ns->user_ns)
4242 		copy_flags |= CL_SLAVE;
4243 	new = copy_tree(old, old->mnt.mnt_root, copy_flags);
4244 	if (IS_ERR(new)) {
4245 		namespace_unlock();
4246 		ns_free_inum(&new_ns->ns);
4247 		dec_mnt_namespaces(new_ns->ucounts);
4248 		mnt_ns_release(new_ns);
4249 		return ERR_CAST(new);
4250 	}
4251 	if (user_ns != ns->user_ns) {
4252 		lock_mount_hash();
4253 		lock_mnt_tree(new);
4254 		unlock_mount_hash();
4255 	}
4256 	new_ns->root = new;
4257 
4258 	/*
4259 	 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
4260 	 * as belonging to new namespace.  We have already acquired a private
4261 	 * fs_struct, so tsk->fs->lock is not needed.
4262 	 */
4263 	p = old;
4264 	q = new;
4265 	while (p) {
4266 		mnt_add_to_ns(new_ns, q);
4267 		new_ns->nr_mounts++;
4268 		if (new_fs) {
4269 			if (&p->mnt == new_fs->root.mnt) {
4270 				new_fs->root.mnt = mntget(&q->mnt);
4271 				rootmnt = &p->mnt;
4272 			}
4273 			if (&p->mnt == new_fs->pwd.mnt) {
4274 				new_fs->pwd.mnt = mntget(&q->mnt);
4275 				pwdmnt = &p->mnt;
4276 			}
4277 		}
4278 		p = next_mnt(p, old);
4279 		q = next_mnt(q, new);
4280 		if (!q)
4281 			break;
4282 		// an mntns binding we'd skipped?
4283 		while (p->mnt.mnt_root != q->mnt.mnt_root)
4284 			p = next_mnt(skip_mnt_tree(p), old);
4285 	}
4286 	namespace_unlock();
4287 
4288 	if (rootmnt)
4289 		mntput(rootmnt);
4290 	if (pwdmnt)
4291 		mntput(pwdmnt);
4292 
4293 	mnt_ns_tree_add(new_ns);
4294 	return new_ns;
4295 }
4296 
4297 struct dentry *mount_subtree(struct vfsmount *m, const char *name)
4298 {
4299 	struct mount *mnt = real_mount(m);
4300 	struct mnt_namespace *ns;
4301 	struct super_block *s;
4302 	struct path path;
4303 	int err;
4304 
4305 	ns = alloc_mnt_ns(&init_user_ns, true);
4306 	if (IS_ERR(ns)) {
4307 		mntput(m);
4308 		return ERR_CAST(ns);
4309 	}
4310 	ns->root = mnt;
4311 	ns->nr_mounts++;
4312 	mnt_add_to_ns(ns, mnt);
4313 
4314 	err = vfs_path_lookup(m->mnt_root, m,
4315 			name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
4316 
4317 	put_mnt_ns(ns);
4318 
4319 	if (err)
4320 		return ERR_PTR(err);
4321 
4322 	/* trade a vfsmount reference for active sb one */
4323 	s = path.mnt->mnt_sb;
4324 	atomic_inc(&s->s_active);
4325 	mntput(path.mnt);
4326 	/* lock the sucker */
4327 	down_write(&s->s_umount);
4328 	/* ... and return the root of (sub)tree on it */
4329 	return path.dentry;
4330 }
4331 EXPORT_SYMBOL(mount_subtree);
4332 
4333 SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
4334 		char __user *, type, unsigned long, flags, void __user *, data)
4335 {
4336 	int ret;
4337 	char *kernel_type;
4338 	char *kernel_dev;
4339 	void *options;
4340 
4341 	kernel_type = copy_mount_string(type);
4342 	ret = PTR_ERR(kernel_type);
4343 	if (IS_ERR(kernel_type))
4344 		goto out_type;
4345 
4346 	kernel_dev = copy_mount_string(dev_name);
4347 	ret = PTR_ERR(kernel_dev);
4348 	if (IS_ERR(kernel_dev))
4349 		goto out_dev;
4350 
4351 	options = copy_mount_options(data);
4352 	ret = PTR_ERR(options);
4353 	if (IS_ERR(options))
4354 		goto out_data;
4355 
4356 	ret = do_mount(kernel_dev, dir_name, kernel_type, flags, options);
4357 
4358 	kfree(options);
4359 out_data:
4360 	kfree(kernel_dev);
4361 out_dev:
4362 	kfree(kernel_type);
4363 out_type:
4364 	return ret;
4365 }
4366 
4367 #define FSMOUNT_VALID_FLAGS                                                    \
4368 	(MOUNT_ATTR_RDONLY | MOUNT_ATTR_NOSUID | MOUNT_ATTR_NODEV |            \
4369 	 MOUNT_ATTR_NOEXEC | MOUNT_ATTR__ATIME | MOUNT_ATTR_NODIRATIME |       \
4370 	 MOUNT_ATTR_NOSYMFOLLOW)
4371 
4372 #define MOUNT_SETATTR_VALID_FLAGS (FSMOUNT_VALID_FLAGS | MOUNT_ATTR_IDMAP)
4373 
4374 #define MOUNT_SETATTR_PROPAGATION_FLAGS \
4375 	(MS_UNBINDABLE | MS_PRIVATE | MS_SLAVE | MS_SHARED)
4376 
4377 static unsigned int attr_flags_to_mnt_flags(u64 attr_flags)
4378 {
4379 	unsigned int mnt_flags = 0;
4380 
4381 	if (attr_flags & MOUNT_ATTR_RDONLY)
4382 		mnt_flags |= MNT_READONLY;
4383 	if (attr_flags & MOUNT_ATTR_NOSUID)
4384 		mnt_flags |= MNT_NOSUID;
4385 	if (attr_flags & MOUNT_ATTR_NODEV)
4386 		mnt_flags |= MNT_NODEV;
4387 	if (attr_flags & MOUNT_ATTR_NOEXEC)
4388 		mnt_flags |= MNT_NOEXEC;
4389 	if (attr_flags & MOUNT_ATTR_NODIRATIME)
4390 		mnt_flags |= MNT_NODIRATIME;
4391 	if (attr_flags & MOUNT_ATTR_NOSYMFOLLOW)
4392 		mnt_flags |= MNT_NOSYMFOLLOW;
4393 
4394 	return mnt_flags;
4395 }
4396 
4397 /*
4398  * Create a kernel mount representation for a new, prepared superblock
4399  * (specified by fs_fd) and attach to an open_tree-like file descriptor.
4400  */
4401 SYSCALL_DEFINE3(fsmount, int, fs_fd, unsigned int, flags,
4402 		unsigned int, attr_flags)
4403 {
4404 	struct mnt_namespace *ns;
4405 	struct fs_context *fc;
4406 	struct file *file;
4407 	struct path newmount;
4408 	struct mount *mnt;
4409 	unsigned int mnt_flags = 0;
4410 	long ret;
4411 
4412 	if (!may_mount())
4413 		return -EPERM;
4414 
4415 	if ((flags & ~(FSMOUNT_CLOEXEC)) != 0)
4416 		return -EINVAL;
4417 
4418 	if (attr_flags & ~FSMOUNT_VALID_FLAGS)
4419 		return -EINVAL;
4420 
4421 	mnt_flags = attr_flags_to_mnt_flags(attr_flags);
4422 
4423 	switch (attr_flags & MOUNT_ATTR__ATIME) {
4424 	case MOUNT_ATTR_STRICTATIME:
4425 		break;
4426 	case MOUNT_ATTR_NOATIME:
4427 		mnt_flags |= MNT_NOATIME;
4428 		break;
4429 	case MOUNT_ATTR_RELATIME:
4430 		mnt_flags |= MNT_RELATIME;
4431 		break;
4432 	default:
4433 		return -EINVAL;
4434 	}
4435 
4436 	CLASS(fd, f)(fs_fd);
4437 	if (fd_empty(f))
4438 		return -EBADF;
4439 
4440 	if (fd_file(f)->f_op != &fscontext_fops)
4441 		return -EINVAL;
4442 
4443 	fc = fd_file(f)->private_data;
4444 
4445 	ret = mutex_lock_interruptible(&fc->uapi_mutex);
4446 	if (ret < 0)
4447 		return ret;
4448 
4449 	/* There must be a valid superblock or we can't mount it */
4450 	ret = -EINVAL;
4451 	if (!fc->root)
4452 		goto err_unlock;
4453 
4454 	ret = -EPERM;
4455 	if (mount_too_revealing(fc->root->d_sb, &mnt_flags)) {
4456 		pr_warn("VFS: Mount too revealing\n");
4457 		goto err_unlock;
4458 	}
4459 
4460 	ret = -EBUSY;
4461 	if (fc->phase != FS_CONTEXT_AWAITING_MOUNT)
4462 		goto err_unlock;
4463 
4464 	if (fc->sb_flags & SB_MANDLOCK)
4465 		warn_mandlock();
4466 
4467 	newmount.mnt = vfs_create_mount(fc);
4468 	if (IS_ERR(newmount.mnt)) {
4469 		ret = PTR_ERR(newmount.mnt);
4470 		goto err_unlock;
4471 	}
4472 	newmount.dentry = dget(fc->root);
4473 	newmount.mnt->mnt_flags = mnt_flags;
4474 
4475 	/* We've done the mount bit - now move the file context into more or
4476 	 * less the same state as if we'd done an fspick().  We don't want to
4477 	 * do any memory allocation or anything like that at this point as we
4478 	 * don't want to have to handle any errors incurred.
4479 	 */
4480 	vfs_clean_context(fc);
4481 
4482 	ns = alloc_mnt_ns(current->nsproxy->mnt_ns->user_ns, true);
4483 	if (IS_ERR(ns)) {
4484 		ret = PTR_ERR(ns);
4485 		goto err_path;
4486 	}
4487 	mnt = real_mount(newmount.mnt);
4488 	ns->root = mnt;
4489 	ns->nr_mounts = 1;
4490 	mnt_add_to_ns(ns, mnt);
4491 	mntget(newmount.mnt);
4492 
4493 	/* Attach to an apparent O_PATH fd with a note that we need to unmount
4494 	 * it, not just simply put it.
4495 	 */
4496 	file = dentry_open(&newmount, O_PATH, fc->cred);
4497 	if (IS_ERR(file)) {
4498 		dissolve_on_fput(newmount.mnt);
4499 		ret = PTR_ERR(file);
4500 		goto err_path;
4501 	}
4502 	file->f_mode |= FMODE_NEED_UNMOUNT;
4503 
4504 	ret = get_unused_fd_flags((flags & FSMOUNT_CLOEXEC) ? O_CLOEXEC : 0);
4505 	if (ret >= 0)
4506 		fd_install(ret, file);
4507 	else
4508 		fput(file);
4509 
4510 err_path:
4511 	path_put(&newmount);
4512 err_unlock:
4513 	mutex_unlock(&fc->uapi_mutex);
4514 	return ret;
4515 }
4516 
4517 static inline int vfs_move_mount(struct path *from_path, struct path *to_path,
4518 				 enum mnt_tree_flags_t mflags)
4519 {
4520 	int ret;
4521 
4522 	ret = security_move_mount(from_path, to_path);
4523 	if (ret)
4524 		return ret;
4525 
4526 	if (mflags & MNT_TREE_PROPAGATION)
4527 		return do_set_group(from_path, to_path);
4528 
4529 	return do_move_mount(from_path, to_path, mflags);
4530 }
4531 
4532 /*
4533  * Move a mount from one place to another.  In combination with
4534  * fsopen()/fsmount() this is used to install a new mount and in combination
4535  * with open_tree(OPEN_TREE_CLONE [| AT_RECURSIVE]) it can be used to copy
4536  * a mount subtree.
4537  *
4538  * Note the flags value is a combination of MOVE_MOUNT_* flags.
4539  */
4540 SYSCALL_DEFINE5(move_mount,
4541 		int, from_dfd, const char __user *, from_pathname,
4542 		int, to_dfd, const char __user *, to_pathname,
4543 		unsigned int, flags)
4544 {
4545 	struct path to_path __free(path_put) = {};
4546 	struct path from_path __free(path_put) = {};
4547 	struct filename *to_name __free(putname) = NULL;
4548 	struct filename *from_name __free(putname) = NULL;
4549 	unsigned int lflags, uflags;
4550 	enum mnt_tree_flags_t mflags = 0;
4551 	int ret = 0;
4552 
4553 	if (!may_mount())
4554 		return -EPERM;
4555 
4556 	if (flags & ~MOVE_MOUNT__MASK)
4557 		return -EINVAL;
4558 
4559 	if ((flags & (MOVE_MOUNT_BENEATH | MOVE_MOUNT_SET_GROUP)) ==
4560 	    (MOVE_MOUNT_BENEATH | MOVE_MOUNT_SET_GROUP))
4561 		return -EINVAL;
4562 
4563 	if (flags & MOVE_MOUNT_SET_GROUP)	mflags |= MNT_TREE_PROPAGATION;
4564 	if (flags & MOVE_MOUNT_BENEATH)		mflags |= MNT_TREE_BENEATH;
4565 
4566 	uflags = 0;
4567 	if (flags & MOVE_MOUNT_T_EMPTY_PATH)
4568 		uflags = AT_EMPTY_PATH;
4569 
4570 	to_name = getname_maybe_null(to_pathname, uflags);
4571 	if (IS_ERR(to_name))
4572 		return PTR_ERR(to_name);
4573 
4574 	if (!to_name && to_dfd >= 0) {
4575 		CLASS(fd_raw, f_to)(to_dfd);
4576 		if (fd_empty(f_to))
4577 			return -EBADF;
4578 
4579 		to_path = fd_file(f_to)->f_path;
4580 		path_get(&to_path);
4581 	} else {
4582 		lflags = 0;
4583 		if (flags & MOVE_MOUNT_T_SYMLINKS)
4584 			lflags |= LOOKUP_FOLLOW;
4585 		if (flags & MOVE_MOUNT_T_AUTOMOUNTS)
4586 			lflags |= LOOKUP_AUTOMOUNT;
4587 		ret = filename_lookup(to_dfd, to_name, lflags, &to_path, NULL);
4588 		if (ret)
4589 			return ret;
4590 	}
4591 
4592 	uflags = 0;
4593 	if (flags & MOVE_MOUNT_F_EMPTY_PATH)
4594 		uflags = AT_EMPTY_PATH;
4595 
4596 	from_name = getname_maybe_null(from_pathname, uflags);
4597 	if (IS_ERR(from_name))
4598 		return PTR_ERR(from_name);
4599 
4600 	if (!from_name && from_dfd >= 0) {
4601 		CLASS(fd_raw, f_from)(from_dfd);
4602 		if (fd_empty(f_from))
4603 			return -EBADF;
4604 
4605 		return vfs_move_mount(&fd_file(f_from)->f_path, &to_path, mflags);
4606 	}
4607 
4608 	lflags = 0;
4609 	if (flags & MOVE_MOUNT_F_SYMLINKS)
4610 		lflags |= LOOKUP_FOLLOW;
4611 	if (flags & MOVE_MOUNT_F_AUTOMOUNTS)
4612 		lflags |= LOOKUP_AUTOMOUNT;
4613 	ret = filename_lookup(from_dfd, from_name, lflags, &from_path, NULL);
4614 	if (ret)
4615 		return ret;
4616 
4617 	return vfs_move_mount(&from_path, &to_path, mflags);
4618 }
4619 
4620 /*
4621  * Return true if path is reachable from root
4622  *
4623  * namespace_sem or mount_lock is held
4624  */
4625 bool is_path_reachable(struct mount *mnt, struct dentry *dentry,
4626 			 const struct path *root)
4627 {
4628 	while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) {
4629 		dentry = mnt->mnt_mountpoint;
4630 		mnt = mnt->mnt_parent;
4631 	}
4632 	return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry);
4633 }
4634 
4635 bool path_is_under(const struct path *path1, const struct path *path2)
4636 {
4637 	bool res;
4638 	read_seqlock_excl(&mount_lock);
4639 	res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2);
4640 	read_sequnlock_excl(&mount_lock);
4641 	return res;
4642 }
4643 EXPORT_SYMBOL(path_is_under);
4644 
4645 /*
4646  * pivot_root Semantics:
4647  * Moves the root file system of the current process to the directory put_old,
4648  * makes new_root as the new root file system of the current process, and sets
4649  * root/cwd of all processes which had them on the current root to new_root.
4650  *
4651  * Restrictions:
4652  * The new_root and put_old must be directories, and  must not be on the
4653  * same file  system as the current process root. The put_old  must  be
4654  * underneath new_root,  i.e. adding a non-zero number of /.. to the string
4655  * pointed to by put_old must yield the same directory as new_root. No other
4656  * file system may be mounted on put_old. After all, new_root is a mountpoint.
4657  *
4658  * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
4659  * See Documentation/filesystems/ramfs-rootfs-initramfs.rst for alternatives
4660  * in this situation.
4661  *
4662  * Notes:
4663  *  - we don't move root/cwd if they are not at the root (reason: if something
4664  *    cared enough to change them, it's probably wrong to force them elsewhere)
4665  *  - it's okay to pick a root that isn't the root of a file system, e.g.
4666  *    /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
4667  *    though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
4668  *    first.
4669  */
4670 SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
4671 		const char __user *, put_old)
4672 {
4673 	struct path new, old, root;
4674 	struct mount *new_mnt, *root_mnt, *old_mnt, *root_parent, *ex_parent;
4675 	struct pinned_mountpoint old_mp = {};
4676 	int error;
4677 
4678 	if (!may_mount())
4679 		return -EPERM;
4680 
4681 	error = user_path_at(AT_FDCWD, new_root,
4682 			     LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &new);
4683 	if (error)
4684 		goto out0;
4685 
4686 	error = user_path_at(AT_FDCWD, put_old,
4687 			     LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &old);
4688 	if (error)
4689 		goto out1;
4690 
4691 	error = security_sb_pivotroot(&old, &new);
4692 	if (error)
4693 		goto out2;
4694 
4695 	get_fs_root(current->fs, &root);
4696 	error = lock_mount(&old, &old_mp);
4697 	if (error)
4698 		goto out3;
4699 
4700 	error = -EINVAL;
4701 	new_mnt = real_mount(new.mnt);
4702 	root_mnt = real_mount(root.mnt);
4703 	old_mnt = real_mount(old.mnt);
4704 	ex_parent = new_mnt->mnt_parent;
4705 	root_parent = root_mnt->mnt_parent;
4706 	if (IS_MNT_SHARED(old_mnt) ||
4707 		IS_MNT_SHARED(ex_parent) ||
4708 		IS_MNT_SHARED(root_parent))
4709 		goto out4;
4710 	if (!check_mnt(root_mnt) || !check_mnt(new_mnt))
4711 		goto out4;
4712 	if (new_mnt->mnt.mnt_flags & MNT_LOCKED)
4713 		goto out4;
4714 	error = -ENOENT;
4715 	if (d_unlinked(new.dentry))
4716 		goto out4;
4717 	error = -EBUSY;
4718 	if (new_mnt == root_mnt || old_mnt == root_mnt)
4719 		goto out4; /* loop, on the same file system  */
4720 	error = -EINVAL;
4721 	if (!path_mounted(&root))
4722 		goto out4; /* not a mountpoint */
4723 	if (!mnt_has_parent(root_mnt))
4724 		goto out4; /* absolute root */
4725 	if (!path_mounted(&new))
4726 		goto out4; /* not a mountpoint */
4727 	if (!mnt_has_parent(new_mnt))
4728 		goto out4; /* absolute root */
4729 	/* make sure we can reach put_old from new_root */
4730 	if (!is_path_reachable(old_mnt, old.dentry, &new))
4731 		goto out4;
4732 	/* make certain new is below the root */
4733 	if (!is_path_reachable(new_mnt, new.dentry, &root))
4734 		goto out4;
4735 	lock_mount_hash();
4736 	umount_mnt(new_mnt);
4737 	if (root_mnt->mnt.mnt_flags & MNT_LOCKED) {
4738 		new_mnt->mnt.mnt_flags |= MNT_LOCKED;
4739 		root_mnt->mnt.mnt_flags &= ~MNT_LOCKED;
4740 	}
4741 	/* mount new_root on / */
4742 	attach_mnt(new_mnt, root_parent, root_mnt->mnt_mp);
4743 	umount_mnt(root_mnt);
4744 	/* mount old root on put_old */
4745 	attach_mnt(root_mnt, old_mnt, old_mp.mp);
4746 	touch_mnt_namespace(current->nsproxy->mnt_ns);
4747 	/* A moved mount should not expire automatically */
4748 	list_del_init(&new_mnt->mnt_expire);
4749 	unlock_mount_hash();
4750 	mnt_notify_add(root_mnt);
4751 	mnt_notify_add(new_mnt);
4752 	chroot_fs_refs(&root, &new);
4753 	error = 0;
4754 out4:
4755 	unlock_mount(&old_mp);
4756 out3:
4757 	path_put(&root);
4758 out2:
4759 	path_put(&old);
4760 out1:
4761 	path_put(&new);
4762 out0:
4763 	return error;
4764 }
4765 
4766 static unsigned int recalc_flags(struct mount_kattr *kattr, struct mount *mnt)
4767 {
4768 	unsigned int flags = mnt->mnt.mnt_flags;
4769 
4770 	/*  flags to clear */
4771 	flags &= ~kattr->attr_clr;
4772 	/* flags to raise */
4773 	flags |= kattr->attr_set;
4774 
4775 	return flags;
4776 }
4777 
4778 static int can_idmap_mount(const struct mount_kattr *kattr, struct mount *mnt)
4779 {
4780 	struct vfsmount *m = &mnt->mnt;
4781 	struct user_namespace *fs_userns = m->mnt_sb->s_user_ns;
4782 
4783 	if (!kattr->mnt_idmap)
4784 		return 0;
4785 
4786 	/*
4787 	 * Creating an idmapped mount with the filesystem wide idmapping
4788 	 * doesn't make sense so block that. We don't allow mushy semantics.
4789 	 */
4790 	if (kattr->mnt_userns == m->mnt_sb->s_user_ns)
4791 		return -EINVAL;
4792 
4793 	/*
4794 	 * We only allow an mount to change it's idmapping if it has
4795 	 * never been accessible to userspace.
4796 	 */
4797 	if (!(kattr->kflags & MOUNT_KATTR_IDMAP_REPLACE) && is_idmapped_mnt(m))
4798 		return -EPERM;
4799 
4800 	/* The underlying filesystem doesn't support idmapped mounts yet. */
4801 	if (!(m->mnt_sb->s_type->fs_flags & FS_ALLOW_IDMAP))
4802 		return -EINVAL;
4803 
4804 	/* The filesystem has turned off idmapped mounts. */
4805 	if (m->mnt_sb->s_iflags & SB_I_NOIDMAP)
4806 		return -EINVAL;
4807 
4808 	/* We're not controlling the superblock. */
4809 	if (!ns_capable(fs_userns, CAP_SYS_ADMIN))
4810 		return -EPERM;
4811 
4812 	/* Mount has already been visible in the filesystem hierarchy. */
4813 	if (!is_anon_ns(mnt->mnt_ns))
4814 		return -EINVAL;
4815 
4816 	return 0;
4817 }
4818 
4819 /**
4820  * mnt_allow_writers() - check whether the attribute change allows writers
4821  * @kattr: the new mount attributes
4822  * @mnt: the mount to which @kattr will be applied
4823  *
4824  * Check whether thew new mount attributes in @kattr allow concurrent writers.
4825  *
4826  * Return: true if writers need to be held, false if not
4827  */
4828 static inline bool mnt_allow_writers(const struct mount_kattr *kattr,
4829 				     const struct mount *mnt)
4830 {
4831 	return (!(kattr->attr_set & MNT_READONLY) ||
4832 		(mnt->mnt.mnt_flags & MNT_READONLY)) &&
4833 	       !kattr->mnt_idmap;
4834 }
4835 
4836 static int mount_setattr_prepare(struct mount_kattr *kattr, struct mount *mnt)
4837 {
4838 	struct mount *m;
4839 	int err;
4840 
4841 	for (m = mnt; m; m = next_mnt(m, mnt)) {
4842 		if (!can_change_locked_flags(m, recalc_flags(kattr, m))) {
4843 			err = -EPERM;
4844 			break;
4845 		}
4846 
4847 		err = can_idmap_mount(kattr, m);
4848 		if (err)
4849 			break;
4850 
4851 		if (!mnt_allow_writers(kattr, m)) {
4852 			err = mnt_hold_writers(m);
4853 			if (err)
4854 				break;
4855 		}
4856 
4857 		if (!(kattr->kflags & MOUNT_KATTR_RECURSE))
4858 			return 0;
4859 	}
4860 
4861 	if (err) {
4862 		struct mount *p;
4863 
4864 		/*
4865 		 * If we had to call mnt_hold_writers() MNT_WRITE_HOLD will
4866 		 * be set in @mnt_flags. The loop unsets MNT_WRITE_HOLD for all
4867 		 * mounts and needs to take care to include the first mount.
4868 		 */
4869 		for (p = mnt; p; p = next_mnt(p, mnt)) {
4870 			/* If we had to hold writers unblock them. */
4871 			if (p->mnt.mnt_flags & MNT_WRITE_HOLD)
4872 				mnt_unhold_writers(p);
4873 
4874 			/*
4875 			 * We're done once the first mount we changed got
4876 			 * MNT_WRITE_HOLD unset.
4877 			 */
4878 			if (p == m)
4879 				break;
4880 		}
4881 	}
4882 	return err;
4883 }
4884 
4885 static void do_idmap_mount(const struct mount_kattr *kattr, struct mount *mnt)
4886 {
4887 	struct mnt_idmap *old_idmap;
4888 
4889 	if (!kattr->mnt_idmap)
4890 		return;
4891 
4892 	old_idmap = mnt_idmap(&mnt->mnt);
4893 
4894 	/* Pairs with smp_load_acquire() in mnt_idmap(). */
4895 	smp_store_release(&mnt->mnt.mnt_idmap, mnt_idmap_get(kattr->mnt_idmap));
4896 	mnt_idmap_put(old_idmap);
4897 }
4898 
4899 static void mount_setattr_commit(struct mount_kattr *kattr, struct mount *mnt)
4900 {
4901 	struct mount *m;
4902 
4903 	for (m = mnt; m; m = next_mnt(m, mnt)) {
4904 		unsigned int flags;
4905 
4906 		do_idmap_mount(kattr, m);
4907 		flags = recalc_flags(kattr, m);
4908 		WRITE_ONCE(m->mnt.mnt_flags, flags);
4909 
4910 		/* If we had to hold writers unblock them. */
4911 		if (m->mnt.mnt_flags & MNT_WRITE_HOLD)
4912 			mnt_unhold_writers(m);
4913 
4914 		if (kattr->propagation)
4915 			change_mnt_propagation(m, kattr->propagation);
4916 		if (!(kattr->kflags & MOUNT_KATTR_RECURSE))
4917 			break;
4918 	}
4919 	touch_mnt_namespace(mnt->mnt_ns);
4920 }
4921 
4922 static int do_mount_setattr(struct path *path, struct mount_kattr *kattr)
4923 {
4924 	struct mount *mnt = real_mount(path->mnt);
4925 	int err = 0;
4926 
4927 	if (!path_mounted(path))
4928 		return -EINVAL;
4929 
4930 	if (kattr->mnt_userns) {
4931 		struct mnt_idmap *mnt_idmap;
4932 
4933 		mnt_idmap = alloc_mnt_idmap(kattr->mnt_userns);
4934 		if (IS_ERR(mnt_idmap))
4935 			return PTR_ERR(mnt_idmap);
4936 		kattr->mnt_idmap = mnt_idmap;
4937 	}
4938 
4939 	if (kattr->propagation) {
4940 		/*
4941 		 * Only take namespace_lock() if we're actually changing
4942 		 * propagation.
4943 		 */
4944 		namespace_lock();
4945 		if (kattr->propagation == MS_SHARED) {
4946 			err = invent_group_ids(mnt, kattr->kflags & MOUNT_KATTR_RECURSE);
4947 			if (err) {
4948 				namespace_unlock();
4949 				return err;
4950 			}
4951 		}
4952 	}
4953 
4954 	err = -EINVAL;
4955 	lock_mount_hash();
4956 
4957 	if (!anon_ns_root(mnt) && !check_mnt(mnt))
4958 		goto out;
4959 
4960 	/*
4961 	 * First, we get the mount tree in a shape where we can change mount
4962 	 * properties without failure. If we succeeded to do so we commit all
4963 	 * changes and if we failed we clean up.
4964 	 */
4965 	err = mount_setattr_prepare(kattr, mnt);
4966 	if (!err)
4967 		mount_setattr_commit(kattr, mnt);
4968 
4969 out:
4970 	unlock_mount_hash();
4971 
4972 	if (kattr->propagation) {
4973 		if (err)
4974 			cleanup_group_ids(mnt, NULL);
4975 		namespace_unlock();
4976 	}
4977 
4978 	return err;
4979 }
4980 
4981 static int build_mount_idmapped(const struct mount_attr *attr, size_t usize,
4982 				struct mount_kattr *kattr)
4983 {
4984 	struct ns_common *ns;
4985 	struct user_namespace *mnt_userns;
4986 
4987 	if (!((attr->attr_set | attr->attr_clr) & MOUNT_ATTR_IDMAP))
4988 		return 0;
4989 
4990 	if (attr->attr_clr & MOUNT_ATTR_IDMAP) {
4991 		/*
4992 		 * We can only remove an idmapping if it's never been
4993 		 * exposed to userspace.
4994 		 */
4995 		if (!(kattr->kflags & MOUNT_KATTR_IDMAP_REPLACE))
4996 			return -EINVAL;
4997 
4998 		/*
4999 		 * Removal of idmappings is equivalent to setting
5000 		 * nop_mnt_idmap.
5001 		 */
5002 		if (!(attr->attr_set & MOUNT_ATTR_IDMAP)) {
5003 			kattr->mnt_idmap = &nop_mnt_idmap;
5004 			return 0;
5005 		}
5006 	}
5007 
5008 	if (attr->userns_fd > INT_MAX)
5009 		return -EINVAL;
5010 
5011 	CLASS(fd, f)(attr->userns_fd);
5012 	if (fd_empty(f))
5013 		return -EBADF;
5014 
5015 	if (!proc_ns_file(fd_file(f)))
5016 		return -EINVAL;
5017 
5018 	ns = get_proc_ns(file_inode(fd_file(f)));
5019 	if (ns->ops->type != CLONE_NEWUSER)
5020 		return -EINVAL;
5021 
5022 	/*
5023 	 * The initial idmapping cannot be used to create an idmapped
5024 	 * mount. We use the initial idmapping as an indicator of a mount
5025 	 * that is not idmapped. It can simply be passed into helpers that
5026 	 * are aware of idmapped mounts as a convenient shortcut. A user
5027 	 * can just create a dedicated identity mapping to achieve the same
5028 	 * result.
5029 	 */
5030 	mnt_userns = container_of(ns, struct user_namespace, ns);
5031 	if (mnt_userns == &init_user_ns)
5032 		return -EPERM;
5033 
5034 	/* We're not controlling the target namespace. */
5035 	if (!ns_capable(mnt_userns, CAP_SYS_ADMIN))
5036 		return -EPERM;
5037 
5038 	kattr->mnt_userns = get_user_ns(mnt_userns);
5039 	return 0;
5040 }
5041 
5042 static int build_mount_kattr(const struct mount_attr *attr, size_t usize,
5043 			     struct mount_kattr *kattr)
5044 {
5045 	if (attr->propagation & ~MOUNT_SETATTR_PROPAGATION_FLAGS)
5046 		return -EINVAL;
5047 	if (hweight32(attr->propagation & MOUNT_SETATTR_PROPAGATION_FLAGS) > 1)
5048 		return -EINVAL;
5049 	kattr->propagation = attr->propagation;
5050 
5051 	if ((attr->attr_set | attr->attr_clr) & ~MOUNT_SETATTR_VALID_FLAGS)
5052 		return -EINVAL;
5053 
5054 	kattr->attr_set = attr_flags_to_mnt_flags(attr->attr_set);
5055 	kattr->attr_clr = attr_flags_to_mnt_flags(attr->attr_clr);
5056 
5057 	/*
5058 	 * Since the MOUNT_ATTR_<atime> values are an enum, not a bitmap,
5059 	 * users wanting to transition to a different atime setting cannot
5060 	 * simply specify the atime setting in @attr_set, but must also
5061 	 * specify MOUNT_ATTR__ATIME in the @attr_clr field.
5062 	 * So ensure that MOUNT_ATTR__ATIME can't be partially set in
5063 	 * @attr_clr and that @attr_set can't have any atime bits set if
5064 	 * MOUNT_ATTR__ATIME isn't set in @attr_clr.
5065 	 */
5066 	if (attr->attr_clr & MOUNT_ATTR__ATIME) {
5067 		if ((attr->attr_clr & MOUNT_ATTR__ATIME) != MOUNT_ATTR__ATIME)
5068 			return -EINVAL;
5069 
5070 		/*
5071 		 * Clear all previous time settings as they are mutually
5072 		 * exclusive.
5073 		 */
5074 		kattr->attr_clr |= MNT_RELATIME | MNT_NOATIME;
5075 		switch (attr->attr_set & MOUNT_ATTR__ATIME) {
5076 		case MOUNT_ATTR_RELATIME:
5077 			kattr->attr_set |= MNT_RELATIME;
5078 			break;
5079 		case MOUNT_ATTR_NOATIME:
5080 			kattr->attr_set |= MNT_NOATIME;
5081 			break;
5082 		case MOUNT_ATTR_STRICTATIME:
5083 			break;
5084 		default:
5085 			return -EINVAL;
5086 		}
5087 	} else {
5088 		if (attr->attr_set & MOUNT_ATTR__ATIME)
5089 			return -EINVAL;
5090 	}
5091 
5092 	return build_mount_idmapped(attr, usize, kattr);
5093 }
5094 
5095 static void finish_mount_kattr(struct mount_kattr *kattr)
5096 {
5097 	if (kattr->mnt_userns) {
5098 		put_user_ns(kattr->mnt_userns);
5099 		kattr->mnt_userns = NULL;
5100 	}
5101 
5102 	if (kattr->mnt_idmap)
5103 		mnt_idmap_put(kattr->mnt_idmap);
5104 }
5105 
5106 static int wants_mount_setattr(struct mount_attr __user *uattr, size_t usize,
5107 			       struct mount_kattr *kattr)
5108 {
5109 	int ret;
5110 	struct mount_attr attr;
5111 
5112 	BUILD_BUG_ON(sizeof(struct mount_attr) != MOUNT_ATTR_SIZE_VER0);
5113 
5114 	if (unlikely(usize > PAGE_SIZE))
5115 		return -E2BIG;
5116 	if (unlikely(usize < MOUNT_ATTR_SIZE_VER0))
5117 		return -EINVAL;
5118 
5119 	if (!may_mount())
5120 		return -EPERM;
5121 
5122 	ret = copy_struct_from_user(&attr, sizeof(attr), uattr, usize);
5123 	if (ret)
5124 		return ret;
5125 
5126 	/* Don't bother walking through the mounts if this is a nop. */
5127 	if (attr.attr_set == 0 &&
5128 	    attr.attr_clr == 0 &&
5129 	    attr.propagation == 0)
5130 		return 0; /* Tell caller to not bother. */
5131 
5132 	ret = build_mount_kattr(&attr, usize, kattr);
5133 	if (ret < 0)
5134 		return ret;
5135 
5136 	return 1;
5137 }
5138 
5139 SYSCALL_DEFINE5(mount_setattr, int, dfd, const char __user *, path,
5140 		unsigned int, flags, struct mount_attr __user *, uattr,
5141 		size_t, usize)
5142 {
5143 	int err;
5144 	struct path target;
5145 	struct mount_kattr kattr;
5146 	unsigned int lookup_flags = LOOKUP_AUTOMOUNT | LOOKUP_FOLLOW;
5147 
5148 	if (flags & ~(AT_EMPTY_PATH |
5149 		      AT_RECURSIVE |
5150 		      AT_SYMLINK_NOFOLLOW |
5151 		      AT_NO_AUTOMOUNT))
5152 		return -EINVAL;
5153 
5154 	if (flags & AT_NO_AUTOMOUNT)
5155 		lookup_flags &= ~LOOKUP_AUTOMOUNT;
5156 	if (flags & AT_SYMLINK_NOFOLLOW)
5157 		lookup_flags &= ~LOOKUP_FOLLOW;
5158 	if (flags & AT_EMPTY_PATH)
5159 		lookup_flags |= LOOKUP_EMPTY;
5160 
5161 	kattr = (struct mount_kattr) {
5162 		.lookup_flags	= lookup_flags,
5163 	};
5164 
5165 	if (flags & AT_RECURSIVE)
5166 		kattr.kflags |= MOUNT_KATTR_RECURSE;
5167 
5168 	err = wants_mount_setattr(uattr, usize, &kattr);
5169 	if (err <= 0)
5170 		return err;
5171 
5172 	err = user_path_at(dfd, path, kattr.lookup_flags, &target);
5173 	if (!err) {
5174 		err = do_mount_setattr(&target, &kattr);
5175 		path_put(&target);
5176 	}
5177 	finish_mount_kattr(&kattr);
5178 	return err;
5179 }
5180 
5181 SYSCALL_DEFINE5(open_tree_attr, int, dfd, const char __user *, filename,
5182 		unsigned, flags, struct mount_attr __user *, uattr,
5183 		size_t, usize)
5184 {
5185 	struct file __free(fput) *file = NULL;
5186 	int fd;
5187 
5188 	if (!uattr && usize)
5189 		return -EINVAL;
5190 
5191 	file = vfs_open_tree(dfd, filename, flags);
5192 	if (IS_ERR(file))
5193 		return PTR_ERR(file);
5194 
5195 	if (uattr) {
5196 		int ret;
5197 		struct mount_kattr kattr = {};
5198 
5199 		if (flags & OPEN_TREE_CLONE)
5200 			kattr.kflags = MOUNT_KATTR_IDMAP_REPLACE;
5201 		if (flags & AT_RECURSIVE)
5202 			kattr.kflags |= MOUNT_KATTR_RECURSE;
5203 
5204 		ret = wants_mount_setattr(uattr, usize, &kattr);
5205 		if (ret > 0) {
5206 			ret = do_mount_setattr(&file->f_path, &kattr);
5207 			finish_mount_kattr(&kattr);
5208 		}
5209 		if (ret)
5210 			return ret;
5211 	}
5212 
5213 	fd = get_unused_fd_flags(flags & O_CLOEXEC);
5214 	if (fd < 0)
5215 		return fd;
5216 
5217 	fd_install(fd, no_free_ptr(file));
5218 	return fd;
5219 }
5220 
5221 int show_path(struct seq_file *m, struct dentry *root)
5222 {
5223 	if (root->d_sb->s_op->show_path)
5224 		return root->d_sb->s_op->show_path(m, root);
5225 
5226 	seq_dentry(m, root, " \t\n\\");
5227 	return 0;
5228 }
5229 
5230 static struct vfsmount *lookup_mnt_in_ns(u64 id, struct mnt_namespace *ns)
5231 {
5232 	struct mount *mnt = mnt_find_id_at(ns, id);
5233 
5234 	if (!mnt || mnt->mnt_id_unique != id)
5235 		return NULL;
5236 
5237 	return &mnt->mnt;
5238 }
5239 
5240 struct kstatmount {
5241 	struct statmount __user *buf;
5242 	size_t bufsize;
5243 	struct vfsmount *mnt;
5244 	struct mnt_idmap *idmap;
5245 	u64 mask;
5246 	struct path root;
5247 	struct seq_file seq;
5248 
5249 	/* Must be last --ends in a flexible-array member. */
5250 	struct statmount sm;
5251 };
5252 
5253 static u64 mnt_to_attr_flags(struct vfsmount *mnt)
5254 {
5255 	unsigned int mnt_flags = READ_ONCE(mnt->mnt_flags);
5256 	u64 attr_flags = 0;
5257 
5258 	if (mnt_flags & MNT_READONLY)
5259 		attr_flags |= MOUNT_ATTR_RDONLY;
5260 	if (mnt_flags & MNT_NOSUID)
5261 		attr_flags |= MOUNT_ATTR_NOSUID;
5262 	if (mnt_flags & MNT_NODEV)
5263 		attr_flags |= MOUNT_ATTR_NODEV;
5264 	if (mnt_flags & MNT_NOEXEC)
5265 		attr_flags |= MOUNT_ATTR_NOEXEC;
5266 	if (mnt_flags & MNT_NODIRATIME)
5267 		attr_flags |= MOUNT_ATTR_NODIRATIME;
5268 	if (mnt_flags & MNT_NOSYMFOLLOW)
5269 		attr_flags |= MOUNT_ATTR_NOSYMFOLLOW;
5270 
5271 	if (mnt_flags & MNT_NOATIME)
5272 		attr_flags |= MOUNT_ATTR_NOATIME;
5273 	else if (mnt_flags & MNT_RELATIME)
5274 		attr_flags |= MOUNT_ATTR_RELATIME;
5275 	else
5276 		attr_flags |= MOUNT_ATTR_STRICTATIME;
5277 
5278 	if (is_idmapped_mnt(mnt))
5279 		attr_flags |= MOUNT_ATTR_IDMAP;
5280 
5281 	return attr_flags;
5282 }
5283 
5284 static u64 mnt_to_propagation_flags(struct mount *m)
5285 {
5286 	u64 propagation = 0;
5287 
5288 	if (IS_MNT_SHARED(m))
5289 		propagation |= MS_SHARED;
5290 	if (IS_MNT_SLAVE(m))
5291 		propagation |= MS_SLAVE;
5292 	if (IS_MNT_UNBINDABLE(m))
5293 		propagation |= MS_UNBINDABLE;
5294 	if (!propagation)
5295 		propagation |= MS_PRIVATE;
5296 
5297 	return propagation;
5298 }
5299 
5300 static void statmount_sb_basic(struct kstatmount *s)
5301 {
5302 	struct super_block *sb = s->mnt->mnt_sb;
5303 
5304 	s->sm.mask |= STATMOUNT_SB_BASIC;
5305 	s->sm.sb_dev_major = MAJOR(sb->s_dev);
5306 	s->sm.sb_dev_minor = MINOR(sb->s_dev);
5307 	s->sm.sb_magic = sb->s_magic;
5308 	s->sm.sb_flags = sb->s_flags & (SB_RDONLY|SB_SYNCHRONOUS|SB_DIRSYNC|SB_LAZYTIME);
5309 }
5310 
5311 static void statmount_mnt_basic(struct kstatmount *s)
5312 {
5313 	struct mount *m = real_mount(s->mnt);
5314 
5315 	s->sm.mask |= STATMOUNT_MNT_BASIC;
5316 	s->sm.mnt_id = m->mnt_id_unique;
5317 	s->sm.mnt_parent_id = m->mnt_parent->mnt_id_unique;
5318 	s->sm.mnt_id_old = m->mnt_id;
5319 	s->sm.mnt_parent_id_old = m->mnt_parent->mnt_id;
5320 	s->sm.mnt_attr = mnt_to_attr_flags(&m->mnt);
5321 	s->sm.mnt_propagation = mnt_to_propagation_flags(m);
5322 	s->sm.mnt_peer_group = m->mnt_group_id;
5323 	s->sm.mnt_master = IS_MNT_SLAVE(m) ? m->mnt_master->mnt_group_id : 0;
5324 }
5325 
5326 static void statmount_propagate_from(struct kstatmount *s)
5327 {
5328 	struct mount *m = real_mount(s->mnt);
5329 
5330 	s->sm.mask |= STATMOUNT_PROPAGATE_FROM;
5331 	if (IS_MNT_SLAVE(m))
5332 		s->sm.propagate_from = get_dominating_id(m, &current->fs->root);
5333 }
5334 
5335 static int statmount_mnt_root(struct kstatmount *s, struct seq_file *seq)
5336 {
5337 	int ret;
5338 	size_t start = seq->count;
5339 
5340 	ret = show_path(seq, s->mnt->mnt_root);
5341 	if (ret)
5342 		return ret;
5343 
5344 	if (unlikely(seq_has_overflowed(seq)))
5345 		return -EAGAIN;
5346 
5347 	/*
5348          * Unescape the result. It would be better if supplied string was not
5349          * escaped in the first place, but that's a pretty invasive change.
5350          */
5351 	seq->buf[seq->count] = '\0';
5352 	seq->count = start;
5353 	seq_commit(seq, string_unescape_inplace(seq->buf + start, UNESCAPE_OCTAL));
5354 	return 0;
5355 }
5356 
5357 static int statmount_mnt_point(struct kstatmount *s, struct seq_file *seq)
5358 {
5359 	struct vfsmount *mnt = s->mnt;
5360 	struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
5361 	int err;
5362 
5363 	err = seq_path_root(seq, &mnt_path, &s->root, "");
5364 	return err == SEQ_SKIP ? 0 : err;
5365 }
5366 
5367 static int statmount_fs_type(struct kstatmount *s, struct seq_file *seq)
5368 {
5369 	struct super_block *sb = s->mnt->mnt_sb;
5370 
5371 	seq_puts(seq, sb->s_type->name);
5372 	return 0;
5373 }
5374 
5375 static void statmount_fs_subtype(struct kstatmount *s, struct seq_file *seq)
5376 {
5377 	struct super_block *sb = s->mnt->mnt_sb;
5378 
5379 	if (sb->s_subtype)
5380 		seq_puts(seq, sb->s_subtype);
5381 }
5382 
5383 static int statmount_sb_source(struct kstatmount *s, struct seq_file *seq)
5384 {
5385 	struct super_block *sb = s->mnt->mnt_sb;
5386 	struct mount *r = real_mount(s->mnt);
5387 
5388 	if (sb->s_op->show_devname) {
5389 		size_t start = seq->count;
5390 		int ret;
5391 
5392 		ret = sb->s_op->show_devname(seq, s->mnt->mnt_root);
5393 		if (ret)
5394 			return ret;
5395 
5396 		if (unlikely(seq_has_overflowed(seq)))
5397 			return -EAGAIN;
5398 
5399 		/* Unescape the result */
5400 		seq->buf[seq->count] = '\0';
5401 		seq->count = start;
5402 		seq_commit(seq, string_unescape_inplace(seq->buf + start, UNESCAPE_OCTAL));
5403 	} else {
5404 		seq_puts(seq, r->mnt_devname);
5405 	}
5406 	return 0;
5407 }
5408 
5409 static void statmount_mnt_ns_id(struct kstatmount *s, struct mnt_namespace *ns)
5410 {
5411 	s->sm.mask |= STATMOUNT_MNT_NS_ID;
5412 	s->sm.mnt_ns_id = ns->seq;
5413 }
5414 
5415 static int statmount_mnt_opts(struct kstatmount *s, struct seq_file *seq)
5416 {
5417 	struct vfsmount *mnt = s->mnt;
5418 	struct super_block *sb = mnt->mnt_sb;
5419 	size_t start = seq->count;
5420 	int err;
5421 
5422 	err = security_sb_show_options(seq, sb);
5423 	if (err)
5424 		return err;
5425 
5426 	if (sb->s_op->show_options) {
5427 		err = sb->s_op->show_options(seq, mnt->mnt_root);
5428 		if (err)
5429 			return err;
5430 	}
5431 
5432 	if (unlikely(seq_has_overflowed(seq)))
5433 		return -EAGAIN;
5434 
5435 	if (seq->count == start)
5436 		return 0;
5437 
5438 	/* skip leading comma */
5439 	memmove(seq->buf + start, seq->buf + start + 1,
5440 		seq->count - start - 1);
5441 	seq->count--;
5442 
5443 	return 0;
5444 }
5445 
5446 static inline int statmount_opt_process(struct seq_file *seq, size_t start)
5447 {
5448 	char *buf_end, *opt_end, *src, *dst;
5449 	int count = 0;
5450 
5451 	if (unlikely(seq_has_overflowed(seq)))
5452 		return -EAGAIN;
5453 
5454 	buf_end = seq->buf + seq->count;
5455 	dst = seq->buf + start;
5456 	src = dst + 1;	/* skip initial comma */
5457 
5458 	if (src >= buf_end) {
5459 		seq->count = start;
5460 		return 0;
5461 	}
5462 
5463 	*buf_end = '\0';
5464 	for (; src < buf_end; src = opt_end + 1) {
5465 		opt_end = strchrnul(src, ',');
5466 		*opt_end = '\0';
5467 		dst += string_unescape(src, dst, 0, UNESCAPE_OCTAL) + 1;
5468 		if (WARN_ON_ONCE(++count == INT_MAX))
5469 			return -EOVERFLOW;
5470 	}
5471 	seq->count = dst - 1 - seq->buf;
5472 	return count;
5473 }
5474 
5475 static int statmount_opt_array(struct kstatmount *s, struct seq_file *seq)
5476 {
5477 	struct vfsmount *mnt = s->mnt;
5478 	struct super_block *sb = mnt->mnt_sb;
5479 	size_t start = seq->count;
5480 	int err;
5481 
5482 	if (!sb->s_op->show_options)
5483 		return 0;
5484 
5485 	err = sb->s_op->show_options(seq, mnt->mnt_root);
5486 	if (err)
5487 		return err;
5488 
5489 	err = statmount_opt_process(seq, start);
5490 	if (err < 0)
5491 		return err;
5492 
5493 	s->sm.opt_num = err;
5494 	return 0;
5495 }
5496 
5497 static int statmount_opt_sec_array(struct kstatmount *s, struct seq_file *seq)
5498 {
5499 	struct vfsmount *mnt = s->mnt;
5500 	struct super_block *sb = mnt->mnt_sb;
5501 	size_t start = seq->count;
5502 	int err;
5503 
5504 	err = security_sb_show_options(seq, sb);
5505 	if (err)
5506 		return err;
5507 
5508 	err = statmount_opt_process(seq, start);
5509 	if (err < 0)
5510 		return err;
5511 
5512 	s->sm.opt_sec_num = err;
5513 	return 0;
5514 }
5515 
5516 static inline int statmount_mnt_uidmap(struct kstatmount *s, struct seq_file *seq)
5517 {
5518 	int ret;
5519 
5520 	ret = statmount_mnt_idmap(s->idmap, seq, true);
5521 	if (ret < 0)
5522 		return ret;
5523 
5524 	s->sm.mnt_uidmap_num = ret;
5525 	/*
5526 	 * Always raise STATMOUNT_MNT_UIDMAP even if there are no valid
5527 	 * mappings. This allows userspace to distinguish between a
5528 	 * non-idmapped mount and an idmapped mount where none of the
5529 	 * individual mappings are valid in the caller's idmapping.
5530 	 */
5531 	if (is_valid_mnt_idmap(s->idmap))
5532 		s->sm.mask |= STATMOUNT_MNT_UIDMAP;
5533 	return 0;
5534 }
5535 
5536 static inline int statmount_mnt_gidmap(struct kstatmount *s, struct seq_file *seq)
5537 {
5538 	int ret;
5539 
5540 	ret = statmount_mnt_idmap(s->idmap, seq, false);
5541 	if (ret < 0)
5542 		return ret;
5543 
5544 	s->sm.mnt_gidmap_num = ret;
5545 	/*
5546 	 * Always raise STATMOUNT_MNT_GIDMAP even if there are no valid
5547 	 * mappings. This allows userspace to distinguish between a
5548 	 * non-idmapped mount and an idmapped mount where none of the
5549 	 * individual mappings are valid in the caller's idmapping.
5550 	 */
5551 	if (is_valid_mnt_idmap(s->idmap))
5552 		s->sm.mask |= STATMOUNT_MNT_GIDMAP;
5553 	return 0;
5554 }
5555 
5556 static int statmount_string(struct kstatmount *s, u64 flag)
5557 {
5558 	int ret = 0;
5559 	size_t kbufsize;
5560 	struct seq_file *seq = &s->seq;
5561 	struct statmount *sm = &s->sm;
5562 	u32 start, *offp;
5563 
5564 	/* Reserve an empty string at the beginning for any unset offsets */
5565 	if (!seq->count)
5566 		seq_putc(seq, 0);
5567 
5568 	start = seq->count;
5569 
5570 	switch (flag) {
5571 	case STATMOUNT_FS_TYPE:
5572 		offp = &sm->fs_type;
5573 		ret = statmount_fs_type(s, seq);
5574 		break;
5575 	case STATMOUNT_MNT_ROOT:
5576 		offp = &sm->mnt_root;
5577 		ret = statmount_mnt_root(s, seq);
5578 		break;
5579 	case STATMOUNT_MNT_POINT:
5580 		offp = &sm->mnt_point;
5581 		ret = statmount_mnt_point(s, seq);
5582 		break;
5583 	case STATMOUNT_MNT_OPTS:
5584 		offp = &sm->mnt_opts;
5585 		ret = statmount_mnt_opts(s, seq);
5586 		break;
5587 	case STATMOUNT_OPT_ARRAY:
5588 		offp = &sm->opt_array;
5589 		ret = statmount_opt_array(s, seq);
5590 		break;
5591 	case STATMOUNT_OPT_SEC_ARRAY:
5592 		offp = &sm->opt_sec_array;
5593 		ret = statmount_opt_sec_array(s, seq);
5594 		break;
5595 	case STATMOUNT_FS_SUBTYPE:
5596 		offp = &sm->fs_subtype;
5597 		statmount_fs_subtype(s, seq);
5598 		break;
5599 	case STATMOUNT_SB_SOURCE:
5600 		offp = &sm->sb_source;
5601 		ret = statmount_sb_source(s, seq);
5602 		break;
5603 	case STATMOUNT_MNT_UIDMAP:
5604 		sm->mnt_uidmap = start;
5605 		ret = statmount_mnt_uidmap(s, seq);
5606 		break;
5607 	case STATMOUNT_MNT_GIDMAP:
5608 		sm->mnt_gidmap = start;
5609 		ret = statmount_mnt_gidmap(s, seq);
5610 		break;
5611 	default:
5612 		WARN_ON_ONCE(true);
5613 		return -EINVAL;
5614 	}
5615 
5616 	/*
5617 	 * If nothing was emitted, return to avoid setting the flag
5618 	 * and terminating the buffer.
5619 	 */
5620 	if (seq->count == start)
5621 		return ret;
5622 	if (unlikely(check_add_overflow(sizeof(*sm), seq->count, &kbufsize)))
5623 		return -EOVERFLOW;
5624 	if (kbufsize >= s->bufsize)
5625 		return -EOVERFLOW;
5626 
5627 	/* signal a retry */
5628 	if (unlikely(seq_has_overflowed(seq)))
5629 		return -EAGAIN;
5630 
5631 	if (ret)
5632 		return ret;
5633 
5634 	seq->buf[seq->count++] = '\0';
5635 	sm->mask |= flag;
5636 	*offp = start;
5637 	return 0;
5638 }
5639 
5640 static int copy_statmount_to_user(struct kstatmount *s)
5641 {
5642 	struct statmount *sm = &s->sm;
5643 	struct seq_file *seq = &s->seq;
5644 	char __user *str = ((char __user *)s->buf) + sizeof(*sm);
5645 	size_t copysize = min_t(size_t, s->bufsize, sizeof(*sm));
5646 
5647 	if (seq->count && copy_to_user(str, seq->buf, seq->count))
5648 		return -EFAULT;
5649 
5650 	/* Return the number of bytes copied to the buffer */
5651 	sm->size = copysize + seq->count;
5652 	if (copy_to_user(s->buf, sm, copysize))
5653 		return -EFAULT;
5654 
5655 	return 0;
5656 }
5657 
5658 static struct mount *listmnt_next(struct mount *curr, bool reverse)
5659 {
5660 	struct rb_node *node;
5661 
5662 	if (reverse)
5663 		node = rb_prev(&curr->mnt_node);
5664 	else
5665 		node = rb_next(&curr->mnt_node);
5666 
5667 	return node_to_mount(node);
5668 }
5669 
5670 static int grab_requested_root(struct mnt_namespace *ns, struct path *root)
5671 {
5672 	struct mount *first, *child;
5673 
5674 	rwsem_assert_held(&namespace_sem);
5675 
5676 	/* We're looking at our own ns, just use get_fs_root. */
5677 	if (ns == current->nsproxy->mnt_ns) {
5678 		get_fs_root(current->fs, root);
5679 		return 0;
5680 	}
5681 
5682 	/*
5683 	 * We have to find the first mount in our ns and use that, however it
5684 	 * may not exist, so handle that properly.
5685 	 */
5686 	if (mnt_ns_empty(ns))
5687 		return -ENOENT;
5688 
5689 	first = child = ns->root;
5690 	for (;;) {
5691 		child = listmnt_next(child, false);
5692 		if (!child)
5693 			return -ENOENT;
5694 		if (child->mnt_parent == first)
5695 			break;
5696 	}
5697 
5698 	root->mnt = mntget(&child->mnt);
5699 	root->dentry = dget(root->mnt->mnt_root);
5700 	return 0;
5701 }
5702 
5703 /* This must be updated whenever a new flag is added */
5704 #define STATMOUNT_SUPPORTED (STATMOUNT_SB_BASIC | \
5705 			     STATMOUNT_MNT_BASIC | \
5706 			     STATMOUNT_PROPAGATE_FROM | \
5707 			     STATMOUNT_MNT_ROOT | \
5708 			     STATMOUNT_MNT_POINT | \
5709 			     STATMOUNT_FS_TYPE | \
5710 			     STATMOUNT_MNT_NS_ID | \
5711 			     STATMOUNT_MNT_OPTS | \
5712 			     STATMOUNT_FS_SUBTYPE | \
5713 			     STATMOUNT_SB_SOURCE | \
5714 			     STATMOUNT_OPT_ARRAY | \
5715 			     STATMOUNT_OPT_SEC_ARRAY | \
5716 			     STATMOUNT_SUPPORTED_MASK | \
5717 			     STATMOUNT_MNT_UIDMAP | \
5718 			     STATMOUNT_MNT_GIDMAP)
5719 
5720 static int do_statmount(struct kstatmount *s, u64 mnt_id, u64 mnt_ns_id,
5721 			struct mnt_namespace *ns)
5722 {
5723 	struct mount *m;
5724 	int err;
5725 
5726 	/* Has the namespace already been emptied? */
5727 	if (mnt_ns_id && mnt_ns_empty(ns))
5728 		return -ENOENT;
5729 
5730 	s->mnt = lookup_mnt_in_ns(mnt_id, ns);
5731 	if (!s->mnt)
5732 		return -ENOENT;
5733 
5734 	err = grab_requested_root(ns, &s->root);
5735 	if (err)
5736 		return err;
5737 
5738 	/*
5739 	 * Don't trigger audit denials. We just want to determine what
5740 	 * mounts to show users.
5741 	 */
5742 	m = real_mount(s->mnt);
5743 	if (!is_path_reachable(m, m->mnt.mnt_root, &s->root) &&
5744 	    !ns_capable_noaudit(ns->user_ns, CAP_SYS_ADMIN))
5745 		return -EPERM;
5746 
5747 	err = security_sb_statfs(s->mnt->mnt_root);
5748 	if (err)
5749 		return err;
5750 
5751 	/*
5752 	 * Note that mount properties in mnt->mnt_flags, mnt->mnt_idmap
5753 	 * can change concurrently as we only hold the read-side of the
5754 	 * namespace semaphore and mount properties may change with only
5755 	 * the mount lock held.
5756 	 *
5757 	 * We could sample the mount lock sequence counter to detect
5758 	 * those changes and retry. But it's not worth it. Worst that
5759 	 * happens is that the mnt->mnt_idmap pointer is already changed
5760 	 * while mnt->mnt_flags isn't or vica versa. So what.
5761 	 *
5762 	 * Both mnt->mnt_flags and mnt->mnt_idmap are set and retrieved
5763 	 * via READ_ONCE()/WRITE_ONCE() and guard against theoretical
5764 	 * torn read/write. That's all we care about right now.
5765 	 */
5766 	s->idmap = mnt_idmap(s->mnt);
5767 	if (s->mask & STATMOUNT_MNT_BASIC)
5768 		statmount_mnt_basic(s);
5769 
5770 	if (s->mask & STATMOUNT_SB_BASIC)
5771 		statmount_sb_basic(s);
5772 
5773 	if (s->mask & STATMOUNT_PROPAGATE_FROM)
5774 		statmount_propagate_from(s);
5775 
5776 	if (s->mask & STATMOUNT_FS_TYPE)
5777 		err = statmount_string(s, STATMOUNT_FS_TYPE);
5778 
5779 	if (!err && s->mask & STATMOUNT_MNT_ROOT)
5780 		err = statmount_string(s, STATMOUNT_MNT_ROOT);
5781 
5782 	if (!err && s->mask & STATMOUNT_MNT_POINT)
5783 		err = statmount_string(s, STATMOUNT_MNT_POINT);
5784 
5785 	if (!err && s->mask & STATMOUNT_MNT_OPTS)
5786 		err = statmount_string(s, STATMOUNT_MNT_OPTS);
5787 
5788 	if (!err && s->mask & STATMOUNT_OPT_ARRAY)
5789 		err = statmount_string(s, STATMOUNT_OPT_ARRAY);
5790 
5791 	if (!err && s->mask & STATMOUNT_OPT_SEC_ARRAY)
5792 		err = statmount_string(s, STATMOUNT_OPT_SEC_ARRAY);
5793 
5794 	if (!err && s->mask & STATMOUNT_FS_SUBTYPE)
5795 		err = statmount_string(s, STATMOUNT_FS_SUBTYPE);
5796 
5797 	if (!err && s->mask & STATMOUNT_SB_SOURCE)
5798 		err = statmount_string(s, STATMOUNT_SB_SOURCE);
5799 
5800 	if (!err && s->mask & STATMOUNT_MNT_UIDMAP)
5801 		err = statmount_string(s, STATMOUNT_MNT_UIDMAP);
5802 
5803 	if (!err && s->mask & STATMOUNT_MNT_GIDMAP)
5804 		err = statmount_string(s, STATMOUNT_MNT_GIDMAP);
5805 
5806 	if (!err && s->mask & STATMOUNT_MNT_NS_ID)
5807 		statmount_mnt_ns_id(s, ns);
5808 
5809 	if (!err && s->mask & STATMOUNT_SUPPORTED_MASK) {
5810 		s->sm.mask |= STATMOUNT_SUPPORTED_MASK;
5811 		s->sm.supported_mask = STATMOUNT_SUPPORTED;
5812 	}
5813 
5814 	if (err)
5815 		return err;
5816 
5817 	/* Are there bits in the return mask not present in STATMOUNT_SUPPORTED? */
5818 	WARN_ON_ONCE(~STATMOUNT_SUPPORTED & s->sm.mask);
5819 
5820 	return 0;
5821 }
5822 
5823 static inline bool retry_statmount(const long ret, size_t *seq_size)
5824 {
5825 	if (likely(ret != -EAGAIN))
5826 		return false;
5827 	if (unlikely(check_mul_overflow(*seq_size, 2, seq_size)))
5828 		return false;
5829 	if (unlikely(*seq_size > MAX_RW_COUNT))
5830 		return false;
5831 	return true;
5832 }
5833 
5834 #define STATMOUNT_STRING_REQ (STATMOUNT_MNT_ROOT | STATMOUNT_MNT_POINT | \
5835 			      STATMOUNT_FS_TYPE | STATMOUNT_MNT_OPTS | \
5836 			      STATMOUNT_FS_SUBTYPE | STATMOUNT_SB_SOURCE | \
5837 			      STATMOUNT_OPT_ARRAY | STATMOUNT_OPT_SEC_ARRAY | \
5838 			      STATMOUNT_MNT_UIDMAP | STATMOUNT_MNT_GIDMAP)
5839 
5840 static int prepare_kstatmount(struct kstatmount *ks, struct mnt_id_req *kreq,
5841 			      struct statmount __user *buf, size_t bufsize,
5842 			      size_t seq_size)
5843 {
5844 	if (!access_ok(buf, bufsize))
5845 		return -EFAULT;
5846 
5847 	memset(ks, 0, sizeof(*ks));
5848 	ks->mask = kreq->param;
5849 	ks->buf = buf;
5850 	ks->bufsize = bufsize;
5851 
5852 	if (ks->mask & STATMOUNT_STRING_REQ) {
5853 		if (bufsize == sizeof(ks->sm))
5854 			return -EOVERFLOW;
5855 
5856 		ks->seq.buf = kvmalloc(seq_size, GFP_KERNEL_ACCOUNT);
5857 		if (!ks->seq.buf)
5858 			return -ENOMEM;
5859 
5860 		ks->seq.size = seq_size;
5861 	}
5862 
5863 	return 0;
5864 }
5865 
5866 static int copy_mnt_id_req(const struct mnt_id_req __user *req,
5867 			   struct mnt_id_req *kreq)
5868 {
5869 	int ret;
5870 	size_t usize;
5871 
5872 	BUILD_BUG_ON(sizeof(struct mnt_id_req) != MNT_ID_REQ_SIZE_VER1);
5873 
5874 	ret = get_user(usize, &req->size);
5875 	if (ret)
5876 		return -EFAULT;
5877 	if (unlikely(usize > PAGE_SIZE))
5878 		return -E2BIG;
5879 	if (unlikely(usize < MNT_ID_REQ_SIZE_VER0))
5880 		return -EINVAL;
5881 	memset(kreq, 0, sizeof(*kreq));
5882 	ret = copy_struct_from_user(kreq, sizeof(*kreq), req, usize);
5883 	if (ret)
5884 		return ret;
5885 	if (kreq->spare != 0)
5886 		return -EINVAL;
5887 	/* The first valid unique mount id is MNT_UNIQUE_ID_OFFSET + 1. */
5888 	if (kreq->mnt_id <= MNT_UNIQUE_ID_OFFSET)
5889 		return -EINVAL;
5890 	return 0;
5891 }
5892 
5893 /*
5894  * If the user requested a specific mount namespace id, look that up and return
5895  * that, or if not simply grab a passive reference on our mount namespace and
5896  * return that.
5897  */
5898 static struct mnt_namespace *grab_requested_mnt_ns(const struct mnt_id_req *kreq)
5899 {
5900 	struct mnt_namespace *mnt_ns;
5901 
5902 	if (kreq->mnt_ns_id && kreq->spare)
5903 		return ERR_PTR(-EINVAL);
5904 
5905 	if (kreq->mnt_ns_id)
5906 		return lookup_mnt_ns(kreq->mnt_ns_id);
5907 
5908 	if (kreq->spare) {
5909 		struct ns_common *ns;
5910 
5911 		CLASS(fd, f)(kreq->spare);
5912 		if (fd_empty(f))
5913 			return ERR_PTR(-EBADF);
5914 
5915 		if (!proc_ns_file(fd_file(f)))
5916 			return ERR_PTR(-EINVAL);
5917 
5918 		ns = get_proc_ns(file_inode(fd_file(f)));
5919 		if (ns->ops->type != CLONE_NEWNS)
5920 			return ERR_PTR(-EINVAL);
5921 
5922 		mnt_ns = to_mnt_ns(ns);
5923 	} else {
5924 		mnt_ns = current->nsproxy->mnt_ns;
5925 	}
5926 
5927 	refcount_inc(&mnt_ns->passive);
5928 	return mnt_ns;
5929 }
5930 
5931 SYSCALL_DEFINE4(statmount, const struct mnt_id_req __user *, req,
5932 		struct statmount __user *, buf, size_t, bufsize,
5933 		unsigned int, flags)
5934 {
5935 	struct mnt_namespace *ns __free(mnt_ns_release) = NULL;
5936 	struct kstatmount *ks __free(kfree) = NULL;
5937 	struct mnt_id_req kreq;
5938 	/* We currently support retrieval of 3 strings. */
5939 	size_t seq_size = 3 * PATH_MAX;
5940 	int ret;
5941 
5942 	if (flags)
5943 		return -EINVAL;
5944 
5945 	ret = copy_mnt_id_req(req, &kreq);
5946 	if (ret)
5947 		return ret;
5948 
5949 	ns = grab_requested_mnt_ns(&kreq);
5950 	if (!ns)
5951 		return -ENOENT;
5952 
5953 	if (kreq.mnt_ns_id && (ns != current->nsproxy->mnt_ns) &&
5954 	    !ns_capable_noaudit(ns->user_ns, CAP_SYS_ADMIN))
5955 		return -ENOENT;
5956 
5957 	ks = kmalloc(sizeof(*ks), GFP_KERNEL_ACCOUNT);
5958 	if (!ks)
5959 		return -ENOMEM;
5960 
5961 retry:
5962 	ret = prepare_kstatmount(ks, &kreq, buf, bufsize, seq_size);
5963 	if (ret)
5964 		return ret;
5965 
5966 	scoped_guard(rwsem_read, &namespace_sem)
5967 		ret = do_statmount(ks, kreq.mnt_id, kreq.mnt_ns_id, ns);
5968 
5969 	if (!ret)
5970 		ret = copy_statmount_to_user(ks);
5971 	kvfree(ks->seq.buf);
5972 	path_put(&ks->root);
5973 	if (retry_statmount(ret, &seq_size))
5974 		goto retry;
5975 	return ret;
5976 }
5977 
5978 struct klistmount {
5979 	u64 last_mnt_id;
5980 	u64 mnt_parent_id;
5981 	u64 *kmnt_ids;
5982 	u32 nr_mnt_ids;
5983 	struct mnt_namespace *ns;
5984 	struct path root;
5985 };
5986 
5987 static ssize_t do_listmount(struct klistmount *kls, bool reverse)
5988 {
5989 	struct mnt_namespace *ns = kls->ns;
5990 	u64 mnt_parent_id = kls->mnt_parent_id;
5991 	u64 last_mnt_id = kls->last_mnt_id;
5992 	u64 *mnt_ids = kls->kmnt_ids;
5993 	size_t nr_mnt_ids = kls->nr_mnt_ids;
5994 	struct path orig;
5995 	struct mount *r, *first;
5996 	ssize_t ret;
5997 
5998 	rwsem_assert_held(&namespace_sem);
5999 
6000 	ret = grab_requested_root(ns, &kls->root);
6001 	if (ret)
6002 		return ret;
6003 
6004 	if (mnt_parent_id == LSMT_ROOT) {
6005 		orig = kls->root;
6006 	} else {
6007 		orig.mnt = lookup_mnt_in_ns(mnt_parent_id, ns);
6008 		if (!orig.mnt)
6009 			return -ENOENT;
6010 		orig.dentry = orig.mnt->mnt_root;
6011 	}
6012 
6013 	/*
6014 	 * Don't trigger audit denials. We just want to determine what
6015 	 * mounts to show users.
6016 	 */
6017 	if (!is_path_reachable(real_mount(orig.mnt), orig.dentry, &kls->root) &&
6018 	    !ns_capable_noaudit(ns->user_ns, CAP_SYS_ADMIN))
6019 		return -EPERM;
6020 
6021 	ret = security_sb_statfs(orig.dentry);
6022 	if (ret)
6023 		return ret;
6024 
6025 	if (!last_mnt_id) {
6026 		if (reverse)
6027 			first = node_to_mount(ns->mnt_last_node);
6028 		else
6029 			first = node_to_mount(ns->mnt_first_node);
6030 	} else {
6031 		if (reverse)
6032 			first = mnt_find_id_at_reverse(ns, last_mnt_id - 1);
6033 		else
6034 			first = mnt_find_id_at(ns, last_mnt_id + 1);
6035 	}
6036 
6037 	for (ret = 0, r = first; r && nr_mnt_ids; r = listmnt_next(r, reverse)) {
6038 		if (r->mnt_id_unique == mnt_parent_id)
6039 			continue;
6040 		if (!is_path_reachable(r, r->mnt.mnt_root, &orig))
6041 			continue;
6042 		*mnt_ids = r->mnt_id_unique;
6043 		mnt_ids++;
6044 		nr_mnt_ids--;
6045 		ret++;
6046 	}
6047 	return ret;
6048 }
6049 
6050 static void __free_klistmount_free(const struct klistmount *kls)
6051 {
6052 	path_put(&kls->root);
6053 	kvfree(kls->kmnt_ids);
6054 	mnt_ns_release(kls->ns);
6055 }
6056 
6057 static inline int prepare_klistmount(struct klistmount *kls, struct mnt_id_req *kreq,
6058 				     size_t nr_mnt_ids)
6059 {
6060 
6061 	u64 last_mnt_id = kreq->param;
6062 
6063 	/* The first valid unique mount id is MNT_UNIQUE_ID_OFFSET + 1. */
6064 	if (last_mnt_id != 0 && last_mnt_id <= MNT_UNIQUE_ID_OFFSET)
6065 		return -EINVAL;
6066 
6067 	kls->last_mnt_id = last_mnt_id;
6068 
6069 	kls->nr_mnt_ids = nr_mnt_ids;
6070 	kls->kmnt_ids = kvmalloc_array(nr_mnt_ids, sizeof(*kls->kmnt_ids),
6071 				       GFP_KERNEL_ACCOUNT);
6072 	if (!kls->kmnt_ids)
6073 		return -ENOMEM;
6074 
6075 	kls->ns = grab_requested_mnt_ns(kreq);
6076 	if (!kls->ns)
6077 		return -ENOENT;
6078 
6079 	kls->mnt_parent_id = kreq->mnt_id;
6080 	return 0;
6081 }
6082 
6083 SYSCALL_DEFINE4(listmount, const struct mnt_id_req __user *, req,
6084 		u64 __user *, mnt_ids, size_t, nr_mnt_ids, unsigned int, flags)
6085 {
6086 	struct klistmount kls __free(klistmount_free) = {};
6087 	const size_t maxcount = 1000000;
6088 	struct mnt_id_req kreq;
6089 	ssize_t ret;
6090 
6091 	if (flags & ~LISTMOUNT_REVERSE)
6092 		return -EINVAL;
6093 
6094 	/*
6095 	 * If the mount namespace really has more than 1 million mounts the
6096 	 * caller must iterate over the mount namespace (and reconsider their
6097 	 * system design...).
6098 	 */
6099 	if (unlikely(nr_mnt_ids > maxcount))
6100 		return -EOVERFLOW;
6101 
6102 	if (!access_ok(mnt_ids, nr_mnt_ids * sizeof(*mnt_ids)))
6103 		return -EFAULT;
6104 
6105 	ret = copy_mnt_id_req(req, &kreq);
6106 	if (ret)
6107 		return ret;
6108 
6109 	ret = prepare_klistmount(&kls, &kreq, nr_mnt_ids);
6110 	if (ret)
6111 		return ret;
6112 
6113 	if (kreq.mnt_ns_id && (kls.ns != current->nsproxy->mnt_ns) &&
6114 	    !ns_capable_noaudit(kls.ns->user_ns, CAP_SYS_ADMIN))
6115 		return -ENOENT;
6116 
6117 	/*
6118 	 * We only need to guard against mount topology changes as
6119 	 * listmount() doesn't care about any mount properties.
6120 	 */
6121 	scoped_guard(rwsem_read, &namespace_sem)
6122 		ret = do_listmount(&kls, (flags & LISTMOUNT_REVERSE));
6123 	if (ret <= 0)
6124 		return ret;
6125 
6126 	if (copy_to_user(mnt_ids, kls.kmnt_ids, ret * sizeof(*mnt_ids)))
6127 		return -EFAULT;
6128 
6129 	return ret;
6130 }
6131 
6132 static void __init init_mount_tree(void)
6133 {
6134 	struct vfsmount *mnt;
6135 	struct mount *m;
6136 	struct mnt_namespace *ns;
6137 	struct path root;
6138 
6139 	mnt = vfs_kern_mount(&rootfs_fs_type, 0, "rootfs", initramfs_options);
6140 	if (IS_ERR(mnt))
6141 		panic("Can't create rootfs");
6142 
6143 	ns = alloc_mnt_ns(&init_user_ns, true);
6144 	if (IS_ERR(ns))
6145 		panic("Can't allocate initial namespace");
6146 	ns->seq = atomic64_inc_return(&mnt_ns_seq);
6147 	ns->ns.inum = PROC_MNT_INIT_INO;
6148 	m = real_mount(mnt);
6149 	ns->root = m;
6150 	ns->nr_mounts = 1;
6151 	mnt_add_to_ns(ns, m);
6152 	init_task.nsproxy->mnt_ns = ns;
6153 	get_mnt_ns(ns);
6154 
6155 	root.mnt = mnt;
6156 	root.dentry = mnt->mnt_root;
6157 
6158 	set_fs_pwd(current->fs, &root);
6159 	set_fs_root(current->fs, &root);
6160 
6161 	mnt_ns_tree_add(ns);
6162 }
6163 
6164 void __init mnt_init(void)
6165 {
6166 	int err;
6167 
6168 	mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
6169 			0, SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, NULL);
6170 
6171 	mount_hashtable = alloc_large_system_hash("Mount-cache",
6172 				sizeof(struct hlist_head),
6173 				mhash_entries, 19,
6174 				HASH_ZERO,
6175 				&m_hash_shift, &m_hash_mask, 0, 0);
6176 	mountpoint_hashtable = alloc_large_system_hash("Mountpoint-cache",
6177 				sizeof(struct hlist_head),
6178 				mphash_entries, 19,
6179 				HASH_ZERO,
6180 				&mp_hash_shift, &mp_hash_mask, 0, 0);
6181 
6182 	if (!mount_hashtable || !mountpoint_hashtable)
6183 		panic("Failed to allocate mount hash table\n");
6184 
6185 	kernfs_init();
6186 
6187 	err = sysfs_init();
6188 	if (err)
6189 		printk(KERN_WARNING "%s: sysfs_init error: %d\n",
6190 			__func__, err);
6191 	fs_kobj = kobject_create_and_add("fs", NULL);
6192 	if (!fs_kobj)
6193 		printk(KERN_WARNING "%s: kobj create error\n", __func__);
6194 	shmem_init();
6195 	init_rootfs();
6196 	init_mount_tree();
6197 }
6198 
6199 void put_mnt_ns(struct mnt_namespace *ns)
6200 {
6201 	if (!refcount_dec_and_test(&ns->ns.count))
6202 		return;
6203 	namespace_lock();
6204 	emptied_ns = ns;
6205 	lock_mount_hash();
6206 	umount_tree(ns->root, 0);
6207 	unlock_mount_hash();
6208 	namespace_unlock();
6209 }
6210 
6211 struct vfsmount *kern_mount(struct file_system_type *type)
6212 {
6213 	struct vfsmount *mnt;
6214 	mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, NULL);
6215 	if (!IS_ERR(mnt)) {
6216 		/*
6217 		 * it is a longterm mount, don't release mnt until
6218 		 * we unmount before file sys is unregistered
6219 		*/
6220 		real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL;
6221 	}
6222 	return mnt;
6223 }
6224 EXPORT_SYMBOL_GPL(kern_mount);
6225 
6226 void kern_unmount(struct vfsmount *mnt)
6227 {
6228 	/* release long term mount so mount point can be released */
6229 	if (!IS_ERR(mnt)) {
6230 		mnt_make_shortterm(mnt);
6231 		synchronize_rcu();	/* yecchhh... */
6232 		mntput(mnt);
6233 	}
6234 }
6235 EXPORT_SYMBOL(kern_unmount);
6236 
6237 void kern_unmount_array(struct vfsmount *mnt[], unsigned int num)
6238 {
6239 	unsigned int i;
6240 
6241 	for (i = 0; i < num; i++)
6242 		mnt_make_shortterm(mnt[i]);
6243 	synchronize_rcu_expedited();
6244 	for (i = 0; i < num; i++)
6245 		mntput(mnt[i]);
6246 }
6247 EXPORT_SYMBOL(kern_unmount_array);
6248 
6249 bool our_mnt(struct vfsmount *mnt)
6250 {
6251 	return check_mnt(real_mount(mnt));
6252 }
6253 
6254 bool current_chrooted(void)
6255 {
6256 	/* Does the current process have a non-standard root */
6257 	struct path ns_root;
6258 	struct path fs_root;
6259 	bool chrooted;
6260 
6261 	/* Find the namespace root */
6262 	ns_root.mnt = &current->nsproxy->mnt_ns->root->mnt;
6263 	ns_root.dentry = ns_root.mnt->mnt_root;
6264 	path_get(&ns_root);
6265 	while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root))
6266 		;
6267 
6268 	get_fs_root(current->fs, &fs_root);
6269 
6270 	chrooted = !path_equal(&fs_root, &ns_root);
6271 
6272 	path_put(&fs_root);
6273 	path_put(&ns_root);
6274 
6275 	return chrooted;
6276 }
6277 
6278 static bool mnt_already_visible(struct mnt_namespace *ns,
6279 				const struct super_block *sb,
6280 				int *new_mnt_flags)
6281 {
6282 	int new_flags = *new_mnt_flags;
6283 	struct mount *mnt, *n;
6284 	bool visible = false;
6285 
6286 	down_read(&namespace_sem);
6287 	rbtree_postorder_for_each_entry_safe(mnt, n, &ns->mounts, mnt_node) {
6288 		struct mount *child;
6289 		int mnt_flags;
6290 
6291 		if (mnt->mnt.mnt_sb->s_type != sb->s_type)
6292 			continue;
6293 
6294 		/* This mount is not fully visible if it's root directory
6295 		 * is not the root directory of the filesystem.
6296 		 */
6297 		if (mnt->mnt.mnt_root != mnt->mnt.mnt_sb->s_root)
6298 			continue;
6299 
6300 		/* A local view of the mount flags */
6301 		mnt_flags = mnt->mnt.mnt_flags;
6302 
6303 		/* Don't miss readonly hidden in the superblock flags */
6304 		if (sb_rdonly(mnt->mnt.mnt_sb))
6305 			mnt_flags |= MNT_LOCK_READONLY;
6306 
6307 		/* Verify the mount flags are equal to or more permissive
6308 		 * than the proposed new mount.
6309 		 */
6310 		if ((mnt_flags & MNT_LOCK_READONLY) &&
6311 		    !(new_flags & MNT_READONLY))
6312 			continue;
6313 		if ((mnt_flags & MNT_LOCK_ATIME) &&
6314 		    ((mnt_flags & MNT_ATIME_MASK) != (new_flags & MNT_ATIME_MASK)))
6315 			continue;
6316 
6317 		/* This mount is not fully visible if there are any
6318 		 * locked child mounts that cover anything except for
6319 		 * empty directories.
6320 		 */
6321 		list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
6322 			struct inode *inode = child->mnt_mountpoint->d_inode;
6323 			/* Only worry about locked mounts */
6324 			if (!(child->mnt.mnt_flags & MNT_LOCKED))
6325 				continue;
6326 			/* Is the directory permanently empty? */
6327 			if (!is_empty_dir_inode(inode))
6328 				goto next;
6329 		}
6330 		/* Preserve the locked attributes */
6331 		*new_mnt_flags |= mnt_flags & (MNT_LOCK_READONLY | \
6332 					       MNT_LOCK_ATIME);
6333 		visible = true;
6334 		goto found;
6335 	next:	;
6336 	}
6337 found:
6338 	up_read(&namespace_sem);
6339 	return visible;
6340 }
6341 
6342 static bool mount_too_revealing(const struct super_block *sb, int *new_mnt_flags)
6343 {
6344 	const unsigned long required_iflags = SB_I_NOEXEC | SB_I_NODEV;
6345 	struct mnt_namespace *ns = current->nsproxy->mnt_ns;
6346 	unsigned long s_iflags;
6347 
6348 	if (ns->user_ns == &init_user_ns)
6349 		return false;
6350 
6351 	/* Can this filesystem be too revealing? */
6352 	s_iflags = sb->s_iflags;
6353 	if (!(s_iflags & SB_I_USERNS_VISIBLE))
6354 		return false;
6355 
6356 	if ((s_iflags & required_iflags) != required_iflags) {
6357 		WARN_ONCE(1, "Expected s_iflags to contain 0x%lx\n",
6358 			  required_iflags);
6359 		return true;
6360 	}
6361 
6362 	return !mnt_already_visible(ns, sb, new_mnt_flags);
6363 }
6364 
6365 bool mnt_may_suid(struct vfsmount *mnt)
6366 {
6367 	/*
6368 	 * Foreign mounts (accessed via fchdir or through /proc
6369 	 * symlinks) are always treated as if they are nosuid.  This
6370 	 * prevents namespaces from trusting potentially unsafe
6371 	 * suid/sgid bits, file caps, or security labels that originate
6372 	 * in other namespaces.
6373 	 */
6374 	return !(mnt->mnt_flags & MNT_NOSUID) && check_mnt(real_mount(mnt)) &&
6375 	       current_in_userns(mnt->mnt_sb->s_user_ns);
6376 }
6377 
6378 static struct ns_common *mntns_get(struct task_struct *task)
6379 {
6380 	struct ns_common *ns = NULL;
6381 	struct nsproxy *nsproxy;
6382 
6383 	task_lock(task);
6384 	nsproxy = task->nsproxy;
6385 	if (nsproxy) {
6386 		ns = &nsproxy->mnt_ns->ns;
6387 		get_mnt_ns(to_mnt_ns(ns));
6388 	}
6389 	task_unlock(task);
6390 
6391 	return ns;
6392 }
6393 
6394 static void mntns_put(struct ns_common *ns)
6395 {
6396 	put_mnt_ns(to_mnt_ns(ns));
6397 }
6398 
6399 static int mntns_install(struct nsset *nsset, struct ns_common *ns)
6400 {
6401 	struct nsproxy *nsproxy = nsset->nsproxy;
6402 	struct fs_struct *fs = nsset->fs;
6403 	struct mnt_namespace *mnt_ns = to_mnt_ns(ns), *old_mnt_ns;
6404 	struct user_namespace *user_ns = nsset->cred->user_ns;
6405 	struct path root;
6406 	int err;
6407 
6408 	if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) ||
6409 	    !ns_capable(user_ns, CAP_SYS_CHROOT) ||
6410 	    !ns_capable(user_ns, CAP_SYS_ADMIN))
6411 		return -EPERM;
6412 
6413 	if (is_anon_ns(mnt_ns))
6414 		return -EINVAL;
6415 
6416 	if (fs->users != 1)
6417 		return -EINVAL;
6418 
6419 	get_mnt_ns(mnt_ns);
6420 	old_mnt_ns = nsproxy->mnt_ns;
6421 	nsproxy->mnt_ns = mnt_ns;
6422 
6423 	/* Find the root */
6424 	err = vfs_path_lookup(mnt_ns->root->mnt.mnt_root, &mnt_ns->root->mnt,
6425 				"/", LOOKUP_DOWN, &root);
6426 	if (err) {
6427 		/* revert to old namespace */
6428 		nsproxy->mnt_ns = old_mnt_ns;
6429 		put_mnt_ns(mnt_ns);
6430 		return err;
6431 	}
6432 
6433 	put_mnt_ns(old_mnt_ns);
6434 
6435 	/* Update the pwd and root */
6436 	set_fs_pwd(fs, &root);
6437 	set_fs_root(fs, &root);
6438 
6439 	path_put(&root);
6440 	return 0;
6441 }
6442 
6443 static struct user_namespace *mntns_owner(struct ns_common *ns)
6444 {
6445 	return to_mnt_ns(ns)->user_ns;
6446 }
6447 
6448 const struct proc_ns_operations mntns_operations = {
6449 	.name		= "mnt",
6450 	.type		= CLONE_NEWNS,
6451 	.get		= mntns_get,
6452 	.put		= mntns_put,
6453 	.install	= mntns_install,
6454 	.owner		= mntns_owner,
6455 };
6456 
6457 #ifdef CONFIG_SYSCTL
6458 static const struct ctl_table fs_namespace_sysctls[] = {
6459 	{
6460 		.procname	= "mount-max",
6461 		.data		= &sysctl_mount_max,
6462 		.maxlen		= sizeof(unsigned int),
6463 		.mode		= 0644,
6464 		.proc_handler	= proc_dointvec_minmax,
6465 		.extra1		= SYSCTL_ONE,
6466 	},
6467 };
6468 
6469 static int __init init_fs_namespace_sysctls(void)
6470 {
6471 	register_sysctl_init("fs", fs_namespace_sysctls);
6472 	return 0;
6473 }
6474 fs_initcall(init_fs_namespace_sysctls);
6475 
6476 #endif /* CONFIG_SYSCTL */
6477