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