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