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