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