• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
36 #include "pnode.h"
37 #include "internal.h"
38 #include <trace/hooks/blk.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 __read_mostly;
44 static unsigned int m_hash_shift __read_mostly;
45 static unsigned int mp_hash_mask __read_mostly;
46 static unsigned int mp_hash_shift __read_mostly;
47 
48 static __initdata unsigned long mhash_entries;
set_mhash_entries(char * str)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;
set_mphash_entries(char * str)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_IDA(mnt_id_ida);
70 static DEFINE_IDA(mnt_group_ida);
71 
72 static struct hlist_head *mount_hashtable __read_mostly;
73 static struct hlist_head *mountpoint_hashtable __read_mostly;
74 static struct kmem_cache *mnt_cache __read_mostly;
75 static DECLARE_RWSEM(namespace_sem);
76 static HLIST_HEAD(unmounted);	/* protected by namespace_sem */
77 static LIST_HEAD(ex_mountpoints); /* protected by namespace_sem */
78 
79 struct mount_kattr {
80 	unsigned int attr_set;
81 	unsigned int attr_clr;
82 	unsigned int propagation;
83 	unsigned int lookup_flags;
84 	bool recurse;
85 	struct user_namespace *mnt_userns;
86 	struct mnt_idmap *mnt_idmap;
87 };
88 
89 /* /sys/fs */
90 struct kobject *fs_kobj;
91 EXPORT_SYMBOL_GPL(fs_kobj);
92 
93 /*
94  * vfsmount lock may be taken for read to prevent changes to the
95  * vfsmount hash, ie. during mountpoint lookups or walking back
96  * up the tree.
97  *
98  * It should be taken for write in all cases where the vfsmount
99  * tree or hash is modified or when a vfsmount structure is modified.
100  */
101 __cacheline_aligned_in_smp DEFINE_SEQLOCK(mount_lock);
102 
lock_mount_hash(void)103 static inline void lock_mount_hash(void)
104 {
105 	write_seqlock(&mount_lock);
106 }
107 
unlock_mount_hash(void)108 static inline void unlock_mount_hash(void)
109 {
110 	write_sequnlock(&mount_lock);
111 }
112 
m_hash(struct vfsmount * mnt,struct dentry * dentry)113 static inline struct hlist_head *m_hash(struct vfsmount *mnt, struct dentry *dentry)
114 {
115 	unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
116 	tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
117 	tmp = tmp + (tmp >> m_hash_shift);
118 	return &mount_hashtable[tmp & m_hash_mask];
119 }
120 
mp_hash(struct dentry * dentry)121 static inline struct hlist_head *mp_hash(struct dentry *dentry)
122 {
123 	unsigned long tmp = ((unsigned long)dentry / L1_CACHE_BYTES);
124 	tmp = tmp + (tmp >> mp_hash_shift);
125 	return &mountpoint_hashtable[tmp & mp_hash_mask];
126 }
127 
mnt_alloc_id(struct mount * mnt)128 static int mnt_alloc_id(struct mount *mnt)
129 {
130 	int res = ida_alloc(&mnt_id_ida, GFP_KERNEL);
131 
132 	if (res < 0)
133 		return res;
134 	mnt->mnt_id = res;
135 	return 0;
136 }
137 
mnt_free_id(struct mount * mnt)138 static void mnt_free_id(struct mount *mnt)
139 {
140 	ida_free(&mnt_id_ida, mnt->mnt_id);
141 }
142 
143 /*
144  * Allocate a new peer group ID
145  */
mnt_alloc_group_id(struct mount * mnt)146 static int mnt_alloc_group_id(struct mount *mnt)
147 {
148 	int res = ida_alloc_min(&mnt_group_ida, 1, GFP_KERNEL);
149 
150 	if (res < 0)
151 		return res;
152 	mnt->mnt_group_id = res;
153 	return 0;
154 }
155 
156 /*
157  * Release a peer group ID
158  */
mnt_release_group_id(struct mount * mnt)159 void mnt_release_group_id(struct mount *mnt)
160 {
161 	ida_free(&mnt_group_ida, mnt->mnt_group_id);
162 	mnt->mnt_group_id = 0;
163 }
164 
165 /*
166  * vfsmount lock must be held for read
167  */
mnt_add_count(struct mount * mnt,int n)168 static inline void mnt_add_count(struct mount *mnt, int n)
169 {
170 #ifdef CONFIG_SMP
171 	this_cpu_add(mnt->mnt_pcp->mnt_count, n);
172 #else
173 	preempt_disable();
174 	mnt->mnt_count += n;
175 	preempt_enable();
176 #endif
177 }
178 
179 /*
180  * vfsmount lock must be held for write
181  */
mnt_get_count(struct mount * mnt)182 int mnt_get_count(struct mount *mnt)
183 {
184 #ifdef CONFIG_SMP
185 	int count = 0;
186 	int cpu;
187 
188 	for_each_possible_cpu(cpu) {
189 		count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
190 	}
191 
192 	return count;
193 #else
194 	return mnt->mnt_count;
195 #endif
196 }
197 
alloc_vfsmnt(const char * name)198 static struct mount *alloc_vfsmnt(const char *name)
199 {
200 	struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
201 	if (mnt) {
202 		int err;
203 
204 		err = mnt_alloc_id(mnt);
205 		if (err)
206 			goto out_free_cache;
207 
208 		if (name) {
209 			mnt->mnt_devname = kstrdup_const(name,
210 							 GFP_KERNEL_ACCOUNT);
211 			if (!mnt->mnt_devname)
212 				goto out_free_id;
213 		}
214 
215 #ifdef CONFIG_SMP
216 		mnt->mnt_pcp = alloc_percpu(struct mnt_pcp);
217 		if (!mnt->mnt_pcp)
218 			goto out_free_devname;
219 
220 		this_cpu_add(mnt->mnt_pcp->mnt_count, 1);
221 #else
222 		mnt->mnt_count = 1;
223 		mnt->mnt_writers = 0;
224 #endif
225 
226 		INIT_HLIST_NODE(&mnt->mnt_hash);
227 		INIT_LIST_HEAD(&mnt->mnt_child);
228 		INIT_LIST_HEAD(&mnt->mnt_mounts);
229 		INIT_LIST_HEAD(&mnt->mnt_list);
230 		INIT_LIST_HEAD(&mnt->mnt_expire);
231 		INIT_LIST_HEAD(&mnt->mnt_share);
232 		INIT_LIST_HEAD(&mnt->mnt_slave_list);
233 		INIT_LIST_HEAD(&mnt->mnt_slave);
234 		INIT_HLIST_NODE(&mnt->mnt_mp_list);
235 		INIT_LIST_HEAD(&mnt->mnt_umounting);
236 		INIT_HLIST_HEAD(&mnt->mnt_stuck_children);
237 		mnt->mnt.mnt_idmap = &nop_mnt_idmap;
238 	}
239 	return mnt;
240 
241 #ifdef CONFIG_SMP
242 out_free_devname:
243 	kfree_const(mnt->mnt_devname);
244 #endif
245 out_free_id:
246 	mnt_free_id(mnt);
247 out_free_cache:
248 	kmem_cache_free(mnt_cache, mnt);
249 	return NULL;
250 }
251 
252 /*
253  * Most r/o checks on a fs are for operations that take
254  * discrete amounts of time, like a write() or unlink().
255  * We must keep track of when those operations start
256  * (for permission checks) and when they end, so that
257  * we can determine when writes are able to occur to
258  * a filesystem.
259  */
260 /*
261  * __mnt_is_readonly: check whether a mount is read-only
262  * @mnt: the mount to check for its write status
263  *
264  * This shouldn't be used directly ouside of the VFS.
265  * It does not guarantee that the filesystem will stay
266  * r/w, just that it is right *now*.  This can not and
267  * should not be used in place of IS_RDONLY(inode).
268  * mnt_want/drop_write() will _keep_ the filesystem
269  * r/w.
270  */
__mnt_is_readonly(struct vfsmount * mnt)271 bool __mnt_is_readonly(struct vfsmount *mnt)
272 {
273 	return (mnt->mnt_flags & MNT_READONLY) || sb_rdonly(mnt->mnt_sb);
274 }
275 EXPORT_SYMBOL_GPL(__mnt_is_readonly);
276 
mnt_inc_writers(struct mount * mnt)277 static inline void mnt_inc_writers(struct mount *mnt)
278 {
279 #ifdef CONFIG_SMP
280 	this_cpu_inc(mnt->mnt_pcp->mnt_writers);
281 #else
282 	mnt->mnt_writers++;
283 #endif
284 }
285 
mnt_dec_writers(struct mount * mnt)286 static inline void mnt_dec_writers(struct mount *mnt)
287 {
288 #ifdef CONFIG_SMP
289 	this_cpu_dec(mnt->mnt_pcp->mnt_writers);
290 #else
291 	mnt->mnt_writers--;
292 #endif
293 }
294 
mnt_get_writers(struct mount * mnt)295 static unsigned int mnt_get_writers(struct mount *mnt)
296 {
297 #ifdef CONFIG_SMP
298 	unsigned int count = 0;
299 	int cpu;
300 
301 	for_each_possible_cpu(cpu) {
302 		count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers;
303 	}
304 
305 	return count;
306 #else
307 	return mnt->mnt_writers;
308 #endif
309 }
310 
mnt_is_readonly(struct vfsmount * mnt)311 static int mnt_is_readonly(struct vfsmount *mnt)
312 {
313 	if (READ_ONCE(mnt->mnt_sb->s_readonly_remount))
314 		return 1;
315 	/*
316 	 * The barrier pairs with the barrier in sb_start_ro_state_change()
317 	 * making sure if we don't see s_readonly_remount set yet, we also will
318 	 * not see any superblock / mount flag changes done by remount.
319 	 * It also pairs with the barrier in sb_end_ro_state_change()
320 	 * assuring that if we see s_readonly_remount already cleared, we will
321 	 * see the values of superblock / mount flags updated by remount.
322 	 */
323 	smp_rmb();
324 	return __mnt_is_readonly(mnt);
325 }
326 
327 /*
328  * Most r/o & frozen checks on a fs are for operations that take discrete
329  * amounts of time, like a write() or unlink().  We must keep track of when
330  * those operations start (for permission checks) and when they end, so that we
331  * can determine when writes are able to occur to a filesystem.
332  */
333 /**
334  * __mnt_want_write - get write access to a mount without freeze protection
335  * @m: the mount on which to take a write
336  *
337  * This tells the low-level filesystem that a write is about to be performed to
338  * it, and makes sure that writes are allowed (mnt it read-write) before
339  * returning success. This operation does not protect against filesystem being
340  * frozen. When the write operation is finished, __mnt_drop_write() must be
341  * called. This is effectively a refcount.
342  */
__mnt_want_write(struct vfsmount * m)343 int __mnt_want_write(struct vfsmount *m)
344 {
345 	struct mount *mnt = real_mount(m);
346 	int ret = 0;
347 
348 	preempt_disable();
349 	mnt_inc_writers(mnt);
350 	/*
351 	 * The store to mnt_inc_writers must be visible before we pass
352 	 * MNT_WRITE_HOLD loop below, so that the slowpath can see our
353 	 * incremented count after it has set MNT_WRITE_HOLD.
354 	 */
355 	smp_mb();
356 	might_lock(&mount_lock.lock);
357 	while (READ_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD) {
358 		if (!IS_ENABLED(CONFIG_PREEMPT_RT)) {
359 			cpu_relax();
360 		} else {
361 			/*
362 			 * This prevents priority inversion, if the task
363 			 * setting MNT_WRITE_HOLD got preempted on a remote
364 			 * CPU, and it prevents life lock if the task setting
365 			 * MNT_WRITE_HOLD has a lower priority and is bound to
366 			 * the same CPU as the task that is spinning here.
367 			 */
368 			preempt_enable();
369 			lock_mount_hash();
370 			unlock_mount_hash();
371 			preempt_disable();
372 		}
373 	}
374 	/*
375 	 * The barrier pairs with the barrier sb_start_ro_state_change() making
376 	 * sure that if we see MNT_WRITE_HOLD cleared, we will also see
377 	 * s_readonly_remount set (or even SB_RDONLY / MNT_READONLY flags) in
378 	 * mnt_is_readonly() and bail in case we are racing with remount
379 	 * read-only.
380 	 */
381 	smp_rmb();
382 	if (mnt_is_readonly(m)) {
383 		mnt_dec_writers(mnt);
384 		ret = -EROFS;
385 	}
386 	preempt_enable();
387 
388 	return ret;
389 }
390 
391 /**
392  * mnt_want_write - get write access to a mount
393  * @m: the mount on which to take a write
394  *
395  * This tells the low-level filesystem that a write is about to be performed to
396  * it, and makes sure that writes are allowed (mount is read-write, filesystem
397  * is not frozen) before returning success.  When the write operation is
398  * finished, mnt_drop_write() must be called.  This is effectively a refcount.
399  */
mnt_want_write(struct vfsmount * m)400 int mnt_want_write(struct vfsmount *m)
401 {
402 	int ret;
403 
404 	sb_start_write(m->mnt_sb);
405 	ret = __mnt_want_write(m);
406 	if (ret)
407 		sb_end_write(m->mnt_sb);
408 	return ret;
409 }
410 EXPORT_SYMBOL_GPL(mnt_want_write);
411 
412 /**
413  * __mnt_want_write_file - get write access to a file's mount
414  * @file: the file who's mount on which to take a write
415  *
416  * This is like __mnt_want_write, but if the file is already open for writing it
417  * skips incrementing mnt_writers (since the open file already has a reference)
418  * and instead only does the check for emergency r/o remounts.  This must be
419  * paired with __mnt_drop_write_file.
420  */
__mnt_want_write_file(struct file * file)421 int __mnt_want_write_file(struct file *file)
422 {
423 	if (file->f_mode & FMODE_WRITER) {
424 		/*
425 		 * Superblock may have become readonly while there are still
426 		 * writable fd's, e.g. due to a fs error with errors=remount-ro
427 		 */
428 		if (__mnt_is_readonly(file->f_path.mnt))
429 			return -EROFS;
430 		return 0;
431 	}
432 	return __mnt_want_write(file->f_path.mnt);
433 }
434 
435 /**
436  * mnt_want_write_file - get write access to a file's mount
437  * @file: the file who's mount on which to take a write
438  *
439  * This is like mnt_want_write, but if the file is already open for writing it
440  * skips incrementing mnt_writers (since the open file already has a reference)
441  * and instead only does the freeze protection and the check for emergency r/o
442  * remounts.  This must be paired with mnt_drop_write_file.
443  */
mnt_want_write_file(struct file * file)444 int mnt_want_write_file(struct file *file)
445 {
446 	int ret;
447 
448 	sb_start_write(file_inode(file)->i_sb);
449 	ret = __mnt_want_write_file(file);
450 	if (ret)
451 		sb_end_write(file_inode(file)->i_sb);
452 	return ret;
453 }
454 EXPORT_SYMBOL_NS_GPL(mnt_want_write_file, ANDROID_GKI_VFS_EXPORT_ONLY);
455 
456 /**
457  * __mnt_drop_write - give up write access to a mount
458  * @mnt: the mount on which to give up write access
459  *
460  * Tells the low-level filesystem that we are done
461  * performing writes to it.  Must be matched with
462  * __mnt_want_write() call above.
463  */
__mnt_drop_write(struct vfsmount * mnt)464 void __mnt_drop_write(struct vfsmount *mnt)
465 {
466 	preempt_disable();
467 	mnt_dec_writers(real_mount(mnt));
468 	preempt_enable();
469 }
470 
471 /**
472  * mnt_drop_write - give up write access to a mount
473  * @mnt: the mount on which to give up write access
474  *
475  * Tells the low-level filesystem that we are done performing writes to it and
476  * also allows filesystem to be frozen again.  Must be matched with
477  * mnt_want_write() call above.
478  */
mnt_drop_write(struct vfsmount * mnt)479 void mnt_drop_write(struct vfsmount *mnt)
480 {
481 	__mnt_drop_write(mnt);
482 	sb_end_write(mnt->mnt_sb);
483 }
484 EXPORT_SYMBOL_GPL(mnt_drop_write);
485 
__mnt_drop_write_file(struct file * file)486 void __mnt_drop_write_file(struct file *file)
487 {
488 	if (!(file->f_mode & FMODE_WRITER))
489 		__mnt_drop_write(file->f_path.mnt);
490 }
491 
mnt_drop_write_file(struct file * file)492 void mnt_drop_write_file(struct file *file)
493 {
494 	__mnt_drop_write_file(file);
495 	sb_end_write(file_inode(file)->i_sb);
496 }
497 EXPORT_SYMBOL_NS(mnt_drop_write_file, ANDROID_GKI_VFS_EXPORT_ONLY);
498 
499 /**
500  * mnt_hold_writers - prevent write access to the given mount
501  * @mnt: mnt to prevent write access to
502  *
503  * Prevents write access to @mnt if there are no active writers for @mnt.
504  * This function needs to be called and return successfully before changing
505  * properties of @mnt that need to remain stable for callers with write access
506  * to @mnt.
507  *
508  * After this functions has been called successfully callers must pair it with
509  * a call to mnt_unhold_writers() in order to stop preventing write access to
510  * @mnt.
511  *
512  * Context: This function expects lock_mount_hash() to be held serializing
513  *          setting MNT_WRITE_HOLD.
514  * Return: On success 0 is returned.
515  *	   On error, -EBUSY is returned.
516  */
mnt_hold_writers(struct mount * mnt)517 static inline int mnt_hold_writers(struct mount *mnt)
518 {
519 	mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
520 	/*
521 	 * After storing MNT_WRITE_HOLD, we'll read the counters. This store
522 	 * should be visible before we do.
523 	 */
524 	smp_mb();
525 
526 	/*
527 	 * With writers on hold, if this value is zero, then there are
528 	 * definitely no active writers (although held writers may subsequently
529 	 * increment the count, they'll have to wait, and decrement it after
530 	 * seeing MNT_READONLY).
531 	 *
532 	 * It is OK to have counter incremented on one CPU and decremented on
533 	 * another: the sum will add up correctly. The danger would be when we
534 	 * sum up each counter, if we read a counter before it is incremented,
535 	 * but then read another CPU's count which it has been subsequently
536 	 * decremented from -- we would see more decrements than we should.
537 	 * MNT_WRITE_HOLD protects against this scenario, because
538 	 * mnt_want_write first increments count, then smp_mb, then spins on
539 	 * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
540 	 * we're counting up here.
541 	 */
542 	if (mnt_get_writers(mnt) > 0)
543 		return -EBUSY;
544 
545 	return 0;
546 }
547 
548 /**
549  * mnt_unhold_writers - stop preventing write access to the given mount
550  * @mnt: mnt to stop preventing write access to
551  *
552  * Stop preventing write access to @mnt allowing callers to gain write access
553  * to @mnt again.
554  *
555  * This function can only be called after a successful call to
556  * mnt_hold_writers().
557  *
558  * Context: This function expects lock_mount_hash() to be held.
559  */
mnt_unhold_writers(struct mount * mnt)560 static inline void mnt_unhold_writers(struct mount *mnt)
561 {
562 	/*
563 	 * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
564 	 * that become unheld will see MNT_READONLY.
565 	 */
566 	smp_wmb();
567 	mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
568 }
569 
mnt_make_readonly(struct mount * mnt)570 static int mnt_make_readonly(struct mount *mnt)
571 {
572 	int ret;
573 
574 	ret = mnt_hold_writers(mnt);
575 	if (!ret)
576 		mnt->mnt.mnt_flags |= MNT_READONLY;
577 	mnt_unhold_writers(mnt);
578 	return ret;
579 }
580 
sb_prepare_remount_readonly(struct super_block * sb)581 int sb_prepare_remount_readonly(struct super_block *sb)
582 {
583 	struct mount *mnt;
584 	int err = 0;
585 
586 	/* Racy optimization.  Recheck the counter under MNT_WRITE_HOLD */
587 	if (atomic_long_read(&sb->s_remove_count))
588 		return -EBUSY;
589 
590 	lock_mount_hash();
591 	list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
592 		if (!(mnt->mnt.mnt_flags & MNT_READONLY)) {
593 			err = mnt_hold_writers(mnt);
594 			if (err)
595 				break;
596 		}
597 	}
598 	if (!err && atomic_long_read(&sb->s_remove_count))
599 		err = -EBUSY;
600 
601 	if (!err)
602 		sb_start_ro_state_change(sb);
603 	list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
604 		if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD)
605 			mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
606 	}
607 	unlock_mount_hash();
608 
609 	return err;
610 }
611 
free_vfsmnt(struct mount * mnt)612 static void free_vfsmnt(struct mount *mnt)
613 {
614 	mnt_idmap_put(mnt_idmap(&mnt->mnt));
615 	kfree_const(mnt->mnt_devname);
616 #ifdef CONFIG_SMP
617 	free_percpu(mnt->mnt_pcp);
618 #endif
619 	kmem_cache_free(mnt_cache, mnt);
620 }
621 
delayed_free_vfsmnt(struct rcu_head * head)622 static void delayed_free_vfsmnt(struct rcu_head *head)
623 {
624 	free_vfsmnt(container_of(head, struct mount, mnt_rcu));
625 }
626 
627 /* call under rcu_read_lock */
__legitimize_mnt(struct vfsmount * bastard,unsigned seq)628 int __legitimize_mnt(struct vfsmount *bastard, unsigned seq)
629 {
630 	struct mount *mnt;
631 	if (read_seqretry(&mount_lock, seq))
632 		return 1;
633 	if (bastard == NULL)
634 		return 0;
635 	mnt = real_mount(bastard);
636 	mnt_add_count(mnt, 1);
637 	smp_mb();			// see mntput_no_expire()
638 	if (likely(!read_seqretry(&mount_lock, seq)))
639 		return 0;
640 	if (bastard->mnt_flags & MNT_SYNC_UMOUNT) {
641 		mnt_add_count(mnt, -1);
642 		return 1;
643 	}
644 	lock_mount_hash();
645 	if (unlikely(bastard->mnt_flags & MNT_DOOMED)) {
646 		mnt_add_count(mnt, -1);
647 		unlock_mount_hash();
648 		return 1;
649 	}
650 	unlock_mount_hash();
651 	/* caller will mntput() */
652 	return -1;
653 }
654 
655 /* call under rcu_read_lock */
legitimize_mnt(struct vfsmount * bastard,unsigned seq)656 static bool legitimize_mnt(struct vfsmount *bastard, unsigned seq)
657 {
658 	int res = __legitimize_mnt(bastard, seq);
659 	if (likely(!res))
660 		return true;
661 	if (unlikely(res < 0)) {
662 		rcu_read_unlock();
663 		mntput(bastard);
664 		rcu_read_lock();
665 	}
666 	return false;
667 }
668 
669 /**
670  * __lookup_mnt - find first child mount
671  * @mnt:	parent mount
672  * @dentry:	mountpoint
673  *
674  * If @mnt has a child mount @c mounted @dentry find and return it.
675  *
676  * Note that the child mount @c need not be unique. There are cases
677  * where shadow mounts are created. For example, during mount
678  * propagation when a source mount @mnt whose root got overmounted by a
679  * mount @o after path lookup but before @namespace_sem could be
680  * acquired gets copied and propagated. So @mnt gets copied including
681  * @o. When @mnt is propagated to a destination mount @d that already
682  * has another mount @n mounted at the same mountpoint then the source
683  * mount @mnt will be tucked beneath @n, i.e., @n will be mounted on
684  * @mnt and @mnt mounted on @d. Now both @n and @o are mounted at @mnt
685  * on @dentry.
686  *
687  * Return: The first child of @mnt mounted @dentry or NULL.
688  */
__lookup_mnt(struct vfsmount * mnt,struct dentry * dentry)689 struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
690 {
691 	struct hlist_head *head = m_hash(mnt, dentry);
692 	struct mount *p;
693 
694 	hlist_for_each_entry_rcu(p, head, mnt_hash)
695 		if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry)
696 			return p;
697 	return NULL;
698 }
699 
700 /*
701  * lookup_mnt - Return the first child mount mounted at path
702  *
703  * "First" means first mounted chronologically.  If you create the
704  * following mounts:
705  *
706  * mount /dev/sda1 /mnt
707  * mount /dev/sda2 /mnt
708  * mount /dev/sda3 /mnt
709  *
710  * Then lookup_mnt() on the base /mnt dentry in the root mount will
711  * return successively the root dentry and vfsmount of /dev/sda1, then
712  * /dev/sda2, then /dev/sda3, then NULL.
713  *
714  * lookup_mnt takes a reference to the found vfsmount.
715  */
lookup_mnt(const struct path * path)716 struct vfsmount *lookup_mnt(const struct path *path)
717 {
718 	struct mount *child_mnt;
719 	struct vfsmount *m;
720 	unsigned seq;
721 
722 	rcu_read_lock();
723 	do {
724 		seq = read_seqbegin(&mount_lock);
725 		child_mnt = __lookup_mnt(path->mnt, path->dentry);
726 		m = child_mnt ? &child_mnt->mnt : NULL;
727 	} while (!legitimize_mnt(m, seq));
728 	rcu_read_unlock();
729 	return m;
730 }
731 
lock_ns_list(struct mnt_namespace * ns)732 static inline void lock_ns_list(struct mnt_namespace *ns)
733 {
734 	spin_lock(&ns->ns_lock);
735 }
736 
unlock_ns_list(struct mnt_namespace * ns)737 static inline void unlock_ns_list(struct mnt_namespace *ns)
738 {
739 	spin_unlock(&ns->ns_lock);
740 }
741 
mnt_is_cursor(struct mount * mnt)742 static inline bool mnt_is_cursor(struct mount *mnt)
743 {
744 	return mnt->mnt.mnt_flags & MNT_CURSOR;
745 }
746 
747 /*
748  * __is_local_mountpoint - Test to see if dentry is a mountpoint in the
749  *                         current mount namespace.
750  *
751  * The common case is dentries are not mountpoints at all and that
752  * test is handled inline.  For the slow case when we are actually
753  * dealing with a mountpoint of some kind, walk through all of the
754  * mounts in the current mount namespace and test to see if the dentry
755  * is a mountpoint.
756  *
757  * The mount_hashtable is not usable in the context because we
758  * need to identify all mounts that may be in the current mount
759  * namespace not just a mount that happens to have some specified
760  * parent mount.
761  */
__is_local_mountpoint(struct dentry * dentry)762 bool __is_local_mountpoint(struct dentry *dentry)
763 {
764 	struct mnt_namespace *ns = current->nsproxy->mnt_ns;
765 	struct mount *mnt;
766 	bool is_covered = false;
767 
768 	down_read(&namespace_sem);
769 	lock_ns_list(ns);
770 	list_for_each_entry(mnt, &ns->list, mnt_list) {
771 		if (mnt_is_cursor(mnt))
772 			continue;
773 		is_covered = (mnt->mnt_mountpoint == dentry);
774 		if (is_covered)
775 			break;
776 	}
777 	unlock_ns_list(ns);
778 	up_read(&namespace_sem);
779 
780 	return is_covered;
781 }
782 
lookup_mountpoint(struct dentry * dentry)783 static struct mountpoint *lookup_mountpoint(struct dentry *dentry)
784 {
785 	struct hlist_head *chain = mp_hash(dentry);
786 	struct mountpoint *mp;
787 
788 	hlist_for_each_entry(mp, chain, m_hash) {
789 		if (mp->m_dentry == dentry) {
790 			mp->m_count++;
791 			return mp;
792 		}
793 	}
794 	return NULL;
795 }
796 
get_mountpoint(struct dentry * dentry)797 static struct mountpoint *get_mountpoint(struct dentry *dentry)
798 {
799 	struct mountpoint *mp, *new = NULL;
800 	int ret;
801 
802 	if (d_mountpoint(dentry)) {
803 		/* might be worth a WARN_ON() */
804 		if (d_unlinked(dentry))
805 			return ERR_PTR(-ENOENT);
806 mountpoint:
807 		read_seqlock_excl(&mount_lock);
808 		mp = lookup_mountpoint(dentry);
809 		read_sequnlock_excl(&mount_lock);
810 		if (mp)
811 			goto done;
812 	}
813 
814 	if (!new)
815 		new = kmalloc(sizeof(struct mountpoint), GFP_KERNEL);
816 	if (!new)
817 		return ERR_PTR(-ENOMEM);
818 
819 
820 	/* Exactly one processes may set d_mounted */
821 	ret = d_set_mounted(dentry);
822 
823 	/* Someone else set d_mounted? */
824 	if (ret == -EBUSY)
825 		goto mountpoint;
826 
827 	/* The dentry is not available as a mountpoint? */
828 	mp = ERR_PTR(ret);
829 	if (ret)
830 		goto done;
831 
832 	/* Add the new mountpoint to the hash table */
833 	read_seqlock_excl(&mount_lock);
834 	new->m_dentry = dget(dentry);
835 	new->m_count = 1;
836 	hlist_add_head(&new->m_hash, mp_hash(dentry));
837 	INIT_HLIST_HEAD(&new->m_list);
838 	read_sequnlock_excl(&mount_lock);
839 
840 	mp = new;
841 	new = NULL;
842 done:
843 	kfree(new);
844 	return mp;
845 }
846 
847 /*
848  * vfsmount lock must be held.  Additionally, the caller is responsible
849  * for serializing calls for given disposal list.
850  */
__put_mountpoint(struct mountpoint * mp,struct list_head * list)851 static void __put_mountpoint(struct mountpoint *mp, struct list_head *list)
852 {
853 	if (!--mp->m_count) {
854 		struct dentry *dentry = mp->m_dentry;
855 		BUG_ON(!hlist_empty(&mp->m_list));
856 		spin_lock(&dentry->d_lock);
857 		dentry->d_flags &= ~DCACHE_MOUNTED;
858 		spin_unlock(&dentry->d_lock);
859 		dput_to_list(dentry, list);
860 		hlist_del(&mp->m_hash);
861 		kfree(mp);
862 	}
863 }
864 
865 /* called with namespace_lock and vfsmount lock */
put_mountpoint(struct mountpoint * mp)866 static void put_mountpoint(struct mountpoint *mp)
867 {
868 	__put_mountpoint(mp, &ex_mountpoints);
869 }
870 
check_mnt(struct mount * mnt)871 static inline int check_mnt(struct mount *mnt)
872 {
873 	return mnt->mnt_ns == current->nsproxy->mnt_ns;
874 }
875 
876 /*
877  * vfsmount lock must be held for write
878  */
touch_mnt_namespace(struct mnt_namespace * ns)879 static void touch_mnt_namespace(struct mnt_namespace *ns)
880 {
881 	if (ns) {
882 		ns->event = ++event;
883 		wake_up_interruptible(&ns->poll);
884 	}
885 }
886 
887 /*
888  * vfsmount lock must be held for write
889  */
__touch_mnt_namespace(struct mnt_namespace * ns)890 static void __touch_mnt_namespace(struct mnt_namespace *ns)
891 {
892 	if (ns && ns->event != event) {
893 		ns->event = event;
894 		wake_up_interruptible(&ns->poll);
895 	}
896 }
897 
898 /*
899  * vfsmount lock must be held for write
900  */
unhash_mnt(struct mount * mnt)901 static struct mountpoint *unhash_mnt(struct mount *mnt)
902 {
903 	struct mountpoint *mp;
904 	mnt->mnt_parent = mnt;
905 	mnt->mnt_mountpoint = mnt->mnt.mnt_root;
906 	list_del_init(&mnt->mnt_child);
907 	hlist_del_init_rcu(&mnt->mnt_hash);
908 	hlist_del_init(&mnt->mnt_mp_list);
909 	mp = mnt->mnt_mp;
910 	mnt->mnt_mp = NULL;
911 	return mp;
912 }
913 
914 /*
915  * vfsmount lock must be held for write
916  */
umount_mnt(struct mount * mnt)917 static void umount_mnt(struct mount *mnt)
918 {
919 	put_mountpoint(unhash_mnt(mnt));
920 }
921 
922 /*
923  * vfsmount lock must be held for write
924  */
mnt_set_mountpoint(struct mount * mnt,struct mountpoint * mp,struct mount * child_mnt)925 void mnt_set_mountpoint(struct mount *mnt,
926 			struct mountpoint *mp,
927 			struct mount *child_mnt)
928 {
929 	mp->m_count++;
930 	mnt_add_count(mnt, 1);	/* essentially, that's mntget */
931 	child_mnt->mnt_mountpoint = mp->m_dentry;
932 	child_mnt->mnt_parent = mnt;
933 	child_mnt->mnt_mp = mp;
934 	hlist_add_head(&child_mnt->mnt_mp_list, &mp->m_list);
935 }
936 
937 /**
938  * mnt_set_mountpoint_beneath - mount a mount beneath another one
939  *
940  * @new_parent: the source mount
941  * @top_mnt:    the mount beneath which @new_parent is mounted
942  * @new_mp:     the new mountpoint of @top_mnt on @new_parent
943  *
944  * Remove @top_mnt from its current mountpoint @top_mnt->mnt_mp and
945  * parent @top_mnt->mnt_parent and mount it on top of @new_parent at
946  * @new_mp. And mount @new_parent on the old parent and old
947  * mountpoint of @top_mnt.
948  *
949  * Context: This function expects namespace_lock() and lock_mount_hash()
950  *          to have been acquired in that order.
951  */
mnt_set_mountpoint_beneath(struct mount * new_parent,struct mount * top_mnt,struct mountpoint * new_mp)952 static void mnt_set_mountpoint_beneath(struct mount *new_parent,
953 				       struct mount *top_mnt,
954 				       struct mountpoint *new_mp)
955 {
956 	struct mount *old_top_parent = top_mnt->mnt_parent;
957 	struct mountpoint *old_top_mp = top_mnt->mnt_mp;
958 
959 	mnt_set_mountpoint(old_top_parent, old_top_mp, new_parent);
960 	mnt_change_mountpoint(new_parent, new_mp, top_mnt);
961 }
962 
963 
__attach_mnt(struct mount * mnt,struct mount * parent)964 static void __attach_mnt(struct mount *mnt, struct mount *parent)
965 {
966 	hlist_add_head_rcu(&mnt->mnt_hash,
967 			   m_hash(&parent->mnt, mnt->mnt_mountpoint));
968 	list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
969 }
970 
971 /**
972  * attach_mnt - mount a mount, attach to @mount_hashtable and parent's
973  *              list of child mounts
974  * @parent:  the parent
975  * @mnt:     the new mount
976  * @mp:      the new mountpoint
977  * @beneath: whether to mount @mnt beneath or on top of @parent
978  *
979  * If @beneath is false, mount @mnt at @mp on @parent. Then attach @mnt
980  * to @parent's child mount list and to @mount_hashtable.
981  *
982  * If @beneath is true, remove @mnt from its current parent and
983  * mountpoint and mount it on @mp on @parent, and mount @parent on the
984  * old parent and old mountpoint of @mnt. Finally, attach @parent to
985  * @mnt_hashtable and @parent->mnt_parent->mnt_mounts.
986  *
987  * Note, when __attach_mnt() is called @mnt->mnt_parent already points
988  * to the correct parent.
989  *
990  * Context: This function expects namespace_lock() and lock_mount_hash()
991  *          to have been acquired in that order.
992  */
attach_mnt(struct mount * mnt,struct mount * parent,struct mountpoint * mp,bool beneath)993 static void attach_mnt(struct mount *mnt, struct mount *parent,
994 		       struct mountpoint *mp, bool beneath)
995 {
996 	if (beneath)
997 		mnt_set_mountpoint_beneath(mnt, parent, mp);
998 	else
999 		mnt_set_mountpoint(parent, mp, mnt);
1000 	/*
1001 	 * Note, @mnt->mnt_parent has to be used. If @mnt was mounted
1002 	 * beneath @parent then @mnt will need to be attached to
1003 	 * @parent's old parent, not @parent. IOW, @mnt->mnt_parent
1004 	 * isn't the same mount as @parent.
1005 	 */
1006 	__attach_mnt(mnt, mnt->mnt_parent);
1007 }
1008 
mnt_change_mountpoint(struct mount * parent,struct mountpoint * mp,struct mount * mnt)1009 void mnt_change_mountpoint(struct mount *parent, struct mountpoint *mp, struct mount *mnt)
1010 {
1011 	struct mountpoint *old_mp = mnt->mnt_mp;
1012 	struct mount *old_parent = mnt->mnt_parent;
1013 
1014 	list_del_init(&mnt->mnt_child);
1015 	hlist_del_init(&mnt->mnt_mp_list);
1016 	hlist_del_init_rcu(&mnt->mnt_hash);
1017 
1018 	attach_mnt(mnt, parent, mp, false);
1019 
1020 	put_mountpoint(old_mp);
1021 	mnt_add_count(old_parent, -1);
1022 }
1023 
1024 /*
1025  * vfsmount lock must be held for write
1026  */
commit_tree(struct mount * mnt)1027 static void commit_tree(struct mount *mnt)
1028 {
1029 	struct mount *parent = mnt->mnt_parent;
1030 	struct mount *m;
1031 	LIST_HEAD(head);
1032 	struct mnt_namespace *n = parent->mnt_ns;
1033 
1034 	BUG_ON(parent == mnt);
1035 
1036 	list_add_tail(&head, &mnt->mnt_list);
1037 	list_for_each_entry(m, &head, mnt_list)
1038 		m->mnt_ns = n;
1039 
1040 	list_splice(&head, n->list.prev);
1041 
1042 	n->mounts += n->pending_mounts;
1043 	n->pending_mounts = 0;
1044 
1045 	__attach_mnt(mnt, parent);
1046 	touch_mnt_namespace(n);
1047 }
1048 
next_mnt(struct mount * p,struct mount * root)1049 static struct mount *next_mnt(struct mount *p, struct mount *root)
1050 {
1051 	struct list_head *next = p->mnt_mounts.next;
1052 	if (next == &p->mnt_mounts) {
1053 		while (1) {
1054 			if (p == root)
1055 				return NULL;
1056 			next = p->mnt_child.next;
1057 			if (next != &p->mnt_parent->mnt_mounts)
1058 				break;
1059 			p = p->mnt_parent;
1060 		}
1061 	}
1062 	return list_entry(next, struct mount, mnt_child);
1063 }
1064 
skip_mnt_tree(struct mount * p)1065 static struct mount *skip_mnt_tree(struct mount *p)
1066 {
1067 	struct list_head *prev = p->mnt_mounts.prev;
1068 	while (prev != &p->mnt_mounts) {
1069 		p = list_entry(prev, struct mount, mnt_child);
1070 		prev = p->mnt_mounts.prev;
1071 	}
1072 	return p;
1073 }
1074 
1075 /**
1076  * vfs_create_mount - Create a mount for a configured superblock
1077  * @fc: The configuration context with the superblock attached
1078  *
1079  * Create a mount to an already configured superblock.  If necessary, the
1080  * caller should invoke vfs_get_tree() before calling this.
1081  *
1082  * Note that this does not attach the mount to anything.
1083  */
vfs_create_mount(struct fs_context * fc)1084 struct vfsmount *vfs_create_mount(struct fs_context *fc)
1085 {
1086 	struct mount *mnt;
1087 
1088 	if (!fc->root)
1089 		return ERR_PTR(-EINVAL);
1090 
1091 	mnt = alloc_vfsmnt(fc->source ?: "none");
1092 	if (!mnt)
1093 		return ERR_PTR(-ENOMEM);
1094 
1095 	if (fc->sb_flags & SB_KERNMOUNT)
1096 		mnt->mnt.mnt_flags = MNT_INTERNAL;
1097 
1098 	atomic_inc(&fc->root->d_sb->s_active);
1099 	mnt->mnt.mnt_sb		= fc->root->d_sb;
1100 	mnt->mnt.mnt_root	= dget(fc->root);
1101 	mnt->mnt_mountpoint	= mnt->mnt.mnt_root;
1102 	mnt->mnt_parent		= mnt;
1103 
1104 	lock_mount_hash();
1105 	list_add_tail(&mnt->mnt_instance, &mnt->mnt.mnt_sb->s_mounts);
1106 	unlock_mount_hash();
1107 	return &mnt->mnt;
1108 }
1109 EXPORT_SYMBOL(vfs_create_mount);
1110 
fc_mount(struct fs_context * fc)1111 struct vfsmount *fc_mount(struct fs_context *fc)
1112 {
1113 	int err = vfs_get_tree(fc);
1114 	if (!err) {
1115 		up_write(&fc->root->d_sb->s_umount);
1116 		return vfs_create_mount(fc);
1117 	}
1118 	return ERR_PTR(err);
1119 }
1120 EXPORT_SYMBOL(fc_mount);
1121 
vfs_kern_mount(struct file_system_type * type,int flags,const char * name,void * data)1122 struct vfsmount *vfs_kern_mount(struct file_system_type *type,
1123 				int flags, const char *name,
1124 				void *data)
1125 {
1126 	struct fs_context *fc;
1127 	struct vfsmount *mnt;
1128 	int ret = 0;
1129 
1130 	if (!type)
1131 		return ERR_PTR(-EINVAL);
1132 
1133 	fc = fs_context_for_mount(type, flags);
1134 	if (IS_ERR(fc))
1135 		return ERR_CAST(fc);
1136 
1137 	if (name)
1138 		ret = vfs_parse_fs_string(fc, "source",
1139 					  name, strlen(name));
1140 	if (!ret)
1141 		ret = parse_monolithic_mount_data(fc, data);
1142 	if (!ret)
1143 		mnt = fc_mount(fc);
1144 	else
1145 		mnt = ERR_PTR(ret);
1146 
1147 	put_fs_context(fc);
1148 	return mnt;
1149 }
1150 EXPORT_SYMBOL_GPL(vfs_kern_mount);
1151 
1152 struct vfsmount *
vfs_submount(const struct dentry * mountpoint,struct file_system_type * type,const char * name,void * data)1153 vfs_submount(const struct dentry *mountpoint, struct file_system_type *type,
1154 	     const char *name, void *data)
1155 {
1156 	/* Until it is worked out how to pass the user namespace
1157 	 * through from the parent mount to the submount don't support
1158 	 * unprivileged mounts with submounts.
1159 	 */
1160 	if (mountpoint->d_sb->s_user_ns != &init_user_ns)
1161 		return ERR_PTR(-EPERM);
1162 
1163 	return vfs_kern_mount(type, SB_SUBMOUNT, name, data);
1164 }
1165 EXPORT_SYMBOL_GPL(vfs_submount);
1166 
clone_mnt(struct mount * old,struct dentry * root,int flag)1167 static struct mount *clone_mnt(struct mount *old, struct dentry *root,
1168 					int flag)
1169 {
1170 	struct super_block *sb = old->mnt.mnt_sb;
1171 	struct mount *mnt;
1172 	int err;
1173 
1174 	mnt = alloc_vfsmnt(old->mnt_devname);
1175 	if (!mnt)
1176 		return ERR_PTR(-ENOMEM);
1177 
1178 	if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE))
1179 		mnt->mnt_group_id = 0; /* not a peer of original */
1180 	else
1181 		mnt->mnt_group_id = old->mnt_group_id;
1182 
1183 	if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
1184 		err = mnt_alloc_group_id(mnt);
1185 		if (err)
1186 			goto out_free;
1187 	}
1188 
1189 	mnt->mnt.mnt_flags = old->mnt.mnt_flags;
1190 	mnt->mnt.mnt_flags &= ~(MNT_WRITE_HOLD|MNT_MARKED|MNT_INTERNAL);
1191 
1192 	atomic_inc(&sb->s_active);
1193 	mnt->mnt.mnt_idmap = mnt_idmap_get(mnt_idmap(&old->mnt));
1194 
1195 	mnt->mnt.mnt_sb = sb;
1196 	mnt->mnt.mnt_root = dget(root);
1197 	mnt->mnt_mountpoint = mnt->mnt.mnt_root;
1198 	mnt->mnt_parent = mnt;
1199 	lock_mount_hash();
1200 	list_add_tail(&mnt->mnt_instance, &sb->s_mounts);
1201 	unlock_mount_hash();
1202 
1203 	if ((flag & CL_SLAVE) ||
1204 	    ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) {
1205 		list_add(&mnt->mnt_slave, &old->mnt_slave_list);
1206 		mnt->mnt_master = old;
1207 		CLEAR_MNT_SHARED(mnt);
1208 	} else if (!(flag & CL_PRIVATE)) {
1209 		if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
1210 			list_add(&mnt->mnt_share, &old->mnt_share);
1211 		if (IS_MNT_SLAVE(old))
1212 			list_add(&mnt->mnt_slave, &old->mnt_slave);
1213 		mnt->mnt_master = old->mnt_master;
1214 	} else {
1215 		CLEAR_MNT_SHARED(mnt);
1216 	}
1217 	if (flag & CL_MAKE_SHARED)
1218 		set_mnt_shared(mnt);
1219 
1220 	/* stick the duplicate mount on the same expiry list
1221 	 * as the original if that was on one */
1222 	if (flag & CL_EXPIRE) {
1223 		if (!list_empty(&old->mnt_expire))
1224 			list_add(&mnt->mnt_expire, &old->mnt_expire);
1225 	}
1226 
1227 	return mnt;
1228 
1229  out_free:
1230 	mnt_free_id(mnt);
1231 	free_vfsmnt(mnt);
1232 	return ERR_PTR(err);
1233 }
1234 
cleanup_mnt(struct mount * mnt)1235 static void cleanup_mnt(struct mount *mnt)
1236 {
1237 	struct hlist_node *p;
1238 	struct mount *m;
1239 	/*
1240 	 * The warning here probably indicates that somebody messed
1241 	 * up a mnt_want/drop_write() pair.  If this happens, the
1242 	 * filesystem was probably unable to make r/w->r/o transitions.
1243 	 * The locking used to deal with mnt_count decrement provides barriers,
1244 	 * so mnt_get_writers() below is safe.
1245 	 */
1246 	WARN_ON(mnt_get_writers(mnt));
1247 	if (unlikely(mnt->mnt_pins.first))
1248 		mnt_pin_kill(mnt);
1249 	hlist_for_each_entry_safe(m, p, &mnt->mnt_stuck_children, mnt_umount) {
1250 		hlist_del(&m->mnt_umount);
1251 		mntput(&m->mnt);
1252 	}
1253 	fsnotify_vfsmount_delete(&mnt->mnt);
1254 	dput(mnt->mnt.mnt_root);
1255 	deactivate_super(mnt->mnt.mnt_sb);
1256 	mnt_free_id(mnt);
1257 	call_rcu(&mnt->mnt_rcu, delayed_free_vfsmnt);
1258 }
1259 
__cleanup_mnt(struct rcu_head * head)1260 static void __cleanup_mnt(struct rcu_head *head)
1261 {
1262 	cleanup_mnt(container_of(head, struct mount, mnt_rcu));
1263 }
1264 
1265 static LLIST_HEAD(delayed_mntput_list);
delayed_mntput(struct work_struct * unused)1266 static void delayed_mntput(struct work_struct *unused)
1267 {
1268 	struct llist_node *node = llist_del_all(&delayed_mntput_list);
1269 	struct mount *m, *t;
1270 
1271 	llist_for_each_entry_safe(m, t, node, mnt_llist)
1272 		cleanup_mnt(m);
1273 }
1274 static DECLARE_DELAYED_WORK(delayed_mntput_work, delayed_mntput);
1275 
mntput_no_expire(struct mount * mnt)1276 static void mntput_no_expire(struct mount *mnt)
1277 {
1278 	LIST_HEAD(list);
1279 	int count;
1280 
1281 	rcu_read_lock();
1282 	if (likely(READ_ONCE(mnt->mnt_ns))) {
1283 		/*
1284 		 * Since we don't do lock_mount_hash() here,
1285 		 * ->mnt_ns can change under us.  However, if it's
1286 		 * non-NULL, then there's a reference that won't
1287 		 * be dropped until after an RCU delay done after
1288 		 * turning ->mnt_ns NULL.  So if we observe it
1289 		 * non-NULL under rcu_read_lock(), the reference
1290 		 * we are dropping is not the final one.
1291 		 */
1292 		mnt_add_count(mnt, -1);
1293 		rcu_read_unlock();
1294 		return;
1295 	}
1296 	lock_mount_hash();
1297 	/*
1298 	 * make sure that if __legitimize_mnt() has not seen us grab
1299 	 * mount_lock, we'll see their refcount increment here.
1300 	 */
1301 	smp_mb();
1302 	mnt_add_count(mnt, -1);
1303 	count = mnt_get_count(mnt);
1304 	if (count != 0) {
1305 		WARN_ON(count < 0);
1306 		rcu_read_unlock();
1307 		unlock_mount_hash();
1308 		return;
1309 	}
1310 	if (unlikely(mnt->mnt.mnt_flags & MNT_DOOMED)) {
1311 		rcu_read_unlock();
1312 		unlock_mount_hash();
1313 		return;
1314 	}
1315 	mnt->mnt.mnt_flags |= MNT_DOOMED;
1316 	rcu_read_unlock();
1317 
1318 	list_del(&mnt->mnt_instance);
1319 
1320 	if (unlikely(!list_empty(&mnt->mnt_mounts))) {
1321 		struct mount *p, *tmp;
1322 		list_for_each_entry_safe(p, tmp, &mnt->mnt_mounts,  mnt_child) {
1323 			__put_mountpoint(unhash_mnt(p), &list);
1324 			hlist_add_head(&p->mnt_umount, &mnt->mnt_stuck_children);
1325 		}
1326 	}
1327 	unlock_mount_hash();
1328 	shrink_dentry_list(&list);
1329 
1330 	if (likely(!(mnt->mnt.mnt_flags & MNT_INTERNAL))) {
1331 		struct task_struct *task = current;
1332 		if (likely(!(task->flags & PF_KTHREAD))) {
1333 			init_task_work(&mnt->mnt_rcu, __cleanup_mnt);
1334 			if (!task_work_add(task, &mnt->mnt_rcu, TWA_RESUME))
1335 				return;
1336 		}
1337 		if (llist_add(&mnt->mnt_llist, &delayed_mntput_list))
1338 			schedule_delayed_work(&delayed_mntput_work, 1);
1339 		return;
1340 	}
1341 	cleanup_mnt(mnt);
1342 }
1343 
mntput(struct vfsmount * mnt)1344 void mntput(struct vfsmount *mnt)
1345 {
1346 	if (mnt) {
1347 		struct mount *m = real_mount(mnt);
1348 		/* avoid cacheline pingpong, hope gcc doesn't get "smart" */
1349 		if (unlikely(m->mnt_expiry_mark))
1350 			m->mnt_expiry_mark = 0;
1351 		mntput_no_expire(m);
1352 	}
1353 }
1354 EXPORT_SYMBOL(mntput);
1355 
mntget(struct vfsmount * mnt)1356 struct vfsmount *mntget(struct vfsmount *mnt)
1357 {
1358 	if (mnt)
1359 		mnt_add_count(real_mount(mnt), 1);
1360 	return mnt;
1361 }
1362 EXPORT_SYMBOL_NS_GPL(mntget, ANDROID_GKI_VFS_EXPORT_ONLY);
1363 
1364 /*
1365  * Make a mount point inaccessible to new lookups.
1366  * Because there may still be current users, the caller MUST WAIT
1367  * for an RCU grace period before destroying the mount point.
1368  */
mnt_make_shortterm(struct vfsmount * mnt)1369 void mnt_make_shortterm(struct vfsmount *mnt)
1370 {
1371 	if (mnt)
1372 		real_mount(mnt)->mnt_ns = NULL;
1373 }
1374 
1375 /**
1376  * path_is_mountpoint() - Check if path is a mount in the current namespace.
1377  * @path: path to check
1378  *
1379  *  d_mountpoint() can only be used reliably to establish if a dentry is
1380  *  not mounted in any namespace and that common case is handled inline.
1381  *  d_mountpoint() isn't aware of the possibility there may be multiple
1382  *  mounts using a given dentry in a different namespace. This function
1383  *  checks if the passed in path is a mountpoint rather than the dentry
1384  *  alone.
1385  */
path_is_mountpoint(const struct path * path)1386 bool path_is_mountpoint(const struct path *path)
1387 {
1388 	unsigned seq;
1389 	bool res;
1390 
1391 	if (!d_mountpoint(path->dentry))
1392 		return false;
1393 
1394 	rcu_read_lock();
1395 	do {
1396 		seq = read_seqbegin(&mount_lock);
1397 		res = __path_is_mountpoint(path);
1398 	} while (read_seqretry(&mount_lock, seq));
1399 	rcu_read_unlock();
1400 
1401 	return res;
1402 }
1403 EXPORT_SYMBOL(path_is_mountpoint);
1404 
mnt_clone_internal(const struct path * path)1405 struct vfsmount *mnt_clone_internal(const struct path *path)
1406 {
1407 	struct mount *p;
1408 	p = clone_mnt(real_mount(path->mnt), path->dentry, CL_PRIVATE);
1409 	if (IS_ERR(p))
1410 		return ERR_CAST(p);
1411 	p->mnt.mnt_flags |= MNT_INTERNAL;
1412 	return &p->mnt;
1413 }
1414 
1415 #ifdef CONFIG_PROC_FS
mnt_list_next(struct mnt_namespace * ns,struct list_head * p)1416 static struct mount *mnt_list_next(struct mnt_namespace *ns,
1417 				   struct list_head *p)
1418 {
1419 	struct mount *mnt, *ret = NULL;
1420 
1421 	lock_ns_list(ns);
1422 	list_for_each_continue(p, &ns->list) {
1423 		mnt = list_entry(p, typeof(*mnt), mnt_list);
1424 		if (!mnt_is_cursor(mnt)) {
1425 			ret = mnt;
1426 			break;
1427 		}
1428 	}
1429 	unlock_ns_list(ns);
1430 
1431 	return ret;
1432 }
1433 
1434 /* iterator; we want it to have access to namespace_sem, thus here... */
m_start(struct seq_file * m,loff_t * pos)1435 static void *m_start(struct seq_file *m, loff_t *pos)
1436 {
1437 	struct proc_mounts *p = m->private;
1438 	struct list_head *prev;
1439 
1440 	down_read(&namespace_sem);
1441 	if (!*pos) {
1442 		prev = &p->ns->list;
1443 	} else {
1444 		prev = &p->cursor.mnt_list;
1445 
1446 		/* Read after we'd reached the end? */
1447 		if (list_empty(prev))
1448 			return NULL;
1449 	}
1450 
1451 	return mnt_list_next(p->ns, prev);
1452 }
1453 
m_next(struct seq_file * m,void * v,loff_t * pos)1454 static void *m_next(struct seq_file *m, void *v, loff_t *pos)
1455 {
1456 	struct proc_mounts *p = m->private;
1457 	struct mount *mnt = v;
1458 
1459 	++*pos;
1460 	return mnt_list_next(p->ns, &mnt->mnt_list);
1461 }
1462 
m_stop(struct seq_file * m,void * v)1463 static void m_stop(struct seq_file *m, void *v)
1464 {
1465 	struct proc_mounts *p = m->private;
1466 	struct mount *mnt = v;
1467 
1468 	lock_ns_list(p->ns);
1469 	if (mnt)
1470 		list_move_tail(&p->cursor.mnt_list, &mnt->mnt_list);
1471 	else
1472 		list_del_init(&p->cursor.mnt_list);
1473 	unlock_ns_list(p->ns);
1474 	up_read(&namespace_sem);
1475 }
1476 
m_show(struct seq_file * m,void * v)1477 static int m_show(struct seq_file *m, void *v)
1478 {
1479 	struct proc_mounts *p = m->private;
1480 	struct mount *r = v;
1481 	return p->show(m, &r->mnt);
1482 }
1483 
1484 const struct seq_operations mounts_op = {
1485 	.start	= m_start,
1486 	.next	= m_next,
1487 	.stop	= m_stop,
1488 	.show	= m_show,
1489 };
1490 
mnt_cursor_del(struct mnt_namespace * ns,struct mount * cursor)1491 void mnt_cursor_del(struct mnt_namespace *ns, struct mount *cursor)
1492 {
1493 	down_read(&namespace_sem);
1494 	lock_ns_list(ns);
1495 	list_del(&cursor->mnt_list);
1496 	unlock_ns_list(ns);
1497 	up_read(&namespace_sem);
1498 }
1499 #endif  /* CONFIG_PROC_FS */
1500 
1501 /**
1502  * may_umount_tree - check if a mount tree is busy
1503  * @m: root of mount tree
1504  *
1505  * This is called to check if a tree of mounts has any
1506  * open files, pwds, chroots or sub mounts that are
1507  * busy.
1508  */
may_umount_tree(struct vfsmount * m)1509 int may_umount_tree(struct vfsmount *m)
1510 {
1511 	struct mount *mnt = real_mount(m);
1512 	int actual_refs = 0;
1513 	int minimum_refs = 0;
1514 	struct mount *p;
1515 	BUG_ON(!m);
1516 
1517 	/* write lock needed for mnt_get_count */
1518 	lock_mount_hash();
1519 	for (p = mnt; p; p = next_mnt(p, mnt)) {
1520 		actual_refs += mnt_get_count(p);
1521 		minimum_refs += 2;
1522 	}
1523 	unlock_mount_hash();
1524 
1525 	if (actual_refs > minimum_refs)
1526 		return 0;
1527 
1528 	return 1;
1529 }
1530 
1531 EXPORT_SYMBOL(may_umount_tree);
1532 
1533 /**
1534  * may_umount - check if a mount point is busy
1535  * @mnt: root of mount
1536  *
1537  * This is called to check if a mount point has any
1538  * open files, pwds, chroots or sub mounts. If the
1539  * mount has sub mounts this will return busy
1540  * regardless of whether the sub mounts are busy.
1541  *
1542  * Doesn't take quota and stuff into account. IOW, in some cases it will
1543  * give false negatives. The main reason why it's here is that we need
1544  * a non-destructive way to look for easily umountable filesystems.
1545  */
may_umount(struct vfsmount * mnt)1546 int may_umount(struct vfsmount *mnt)
1547 {
1548 	int ret = 1;
1549 	down_read(&namespace_sem);
1550 	lock_mount_hash();
1551 	if (propagate_mount_busy(real_mount(mnt), 2))
1552 		ret = 0;
1553 	unlock_mount_hash();
1554 	up_read(&namespace_sem);
1555 	return ret;
1556 }
1557 
1558 EXPORT_SYMBOL(may_umount);
1559 
namespace_unlock(void)1560 static void namespace_unlock(void)
1561 {
1562 	struct hlist_head head;
1563 	struct hlist_node *p;
1564 	struct mount *m;
1565 	LIST_HEAD(list);
1566 
1567 	hlist_move_list(&unmounted, &head);
1568 	list_splice_init(&ex_mountpoints, &list);
1569 
1570 	up_write(&namespace_sem);
1571 
1572 	shrink_dentry_list(&list);
1573 
1574 	if (likely(hlist_empty(&head)))
1575 		return;
1576 
1577 	synchronize_rcu_expedited();
1578 
1579 	hlist_for_each_entry_safe(m, p, &head, mnt_umount) {
1580 		hlist_del(&m->mnt_umount);
1581 		mntput(&m->mnt);
1582 	}
1583 }
1584 
namespace_lock(void)1585 static inline void namespace_lock(void)
1586 {
1587 	down_write(&namespace_sem);
1588 }
1589 
1590 enum umount_tree_flags {
1591 	UMOUNT_SYNC = 1,
1592 	UMOUNT_PROPAGATE = 2,
1593 	UMOUNT_CONNECTED = 4,
1594 };
1595 
disconnect_mount(struct mount * mnt,enum umount_tree_flags how)1596 static bool disconnect_mount(struct mount *mnt, enum umount_tree_flags how)
1597 {
1598 	/* Leaving mounts connected is only valid for lazy umounts */
1599 	if (how & UMOUNT_SYNC)
1600 		return true;
1601 
1602 	/* A mount without a parent has nothing to be connected to */
1603 	if (!mnt_has_parent(mnt))
1604 		return true;
1605 
1606 	/* Because the reference counting rules change when mounts are
1607 	 * unmounted and connected, umounted mounts may not be
1608 	 * connected to mounted mounts.
1609 	 */
1610 	if (!(mnt->mnt_parent->mnt.mnt_flags & MNT_UMOUNT))
1611 		return true;
1612 
1613 	/* Has it been requested that the mount remain connected? */
1614 	if (how & UMOUNT_CONNECTED)
1615 		return false;
1616 
1617 	/* Is the mount locked such that it needs to remain connected? */
1618 	if (IS_MNT_LOCKED(mnt))
1619 		return false;
1620 
1621 	/* By default disconnect the mount */
1622 	return true;
1623 }
1624 
1625 /*
1626  * mount_lock must be held
1627  * namespace_sem must be held for write
1628  */
umount_tree(struct mount * mnt,enum umount_tree_flags how)1629 static void umount_tree(struct mount *mnt, enum umount_tree_flags how)
1630 {
1631 	LIST_HEAD(tmp_list);
1632 	struct mount *p;
1633 
1634 	if (how & UMOUNT_PROPAGATE)
1635 		propagate_mount_unlock(mnt);
1636 
1637 	/* Gather the mounts to umount */
1638 	for (p = mnt; p; p = next_mnt(p, mnt)) {
1639 		p->mnt.mnt_flags |= MNT_UMOUNT;
1640 		list_move(&p->mnt_list, &tmp_list);
1641 	}
1642 
1643 	/* Hide the mounts from mnt_mounts */
1644 	list_for_each_entry(p, &tmp_list, mnt_list) {
1645 		list_del_init(&p->mnt_child);
1646 	}
1647 
1648 	/* Add propogated mounts to the tmp_list */
1649 	if (how & UMOUNT_PROPAGATE)
1650 		propagate_umount(&tmp_list);
1651 
1652 	while (!list_empty(&tmp_list)) {
1653 		struct mnt_namespace *ns;
1654 		bool disconnect;
1655 		p = list_first_entry(&tmp_list, struct mount, mnt_list);
1656 		list_del_init(&p->mnt_expire);
1657 		list_del_init(&p->mnt_list);
1658 		ns = p->mnt_ns;
1659 		if (ns) {
1660 			ns->mounts--;
1661 			__touch_mnt_namespace(ns);
1662 		}
1663 		p->mnt_ns = NULL;
1664 		if (how & UMOUNT_SYNC)
1665 			p->mnt.mnt_flags |= MNT_SYNC_UMOUNT;
1666 
1667 		disconnect = disconnect_mount(p, how);
1668 		if (mnt_has_parent(p)) {
1669 			mnt_add_count(p->mnt_parent, -1);
1670 			if (!disconnect) {
1671 				/* Don't forget about p */
1672 				list_add_tail(&p->mnt_child, &p->mnt_parent->mnt_mounts);
1673 			} else {
1674 				umount_mnt(p);
1675 			}
1676 		}
1677 		change_mnt_propagation(p, MS_PRIVATE);
1678 		if (disconnect)
1679 			hlist_add_head(&p->mnt_umount, &unmounted);
1680 	}
1681 }
1682 
1683 static void shrink_submounts(struct mount *mnt);
1684 
do_umount_root(struct super_block * sb)1685 static int do_umount_root(struct super_block *sb)
1686 {
1687 	int ret = 0;
1688 
1689 	down_write(&sb->s_umount);
1690 	if (!sb_rdonly(sb)) {
1691 		struct fs_context *fc;
1692 
1693 		fc = fs_context_for_reconfigure(sb->s_root, SB_RDONLY,
1694 						SB_RDONLY);
1695 		if (IS_ERR(fc)) {
1696 			ret = PTR_ERR(fc);
1697 		} else {
1698 			ret = parse_monolithic_mount_data(fc, NULL);
1699 			if (!ret)
1700 				ret = reconfigure_super(fc);
1701 			put_fs_context(fc);
1702 		}
1703 	}
1704 	up_write(&sb->s_umount);
1705 	return ret;
1706 }
1707 
do_umount(struct mount * mnt,int flags)1708 static int do_umount(struct mount *mnt, int flags)
1709 {
1710 	struct super_block *sb = mnt->mnt.mnt_sb;
1711 	int retval;
1712 
1713 	retval = security_sb_umount(&mnt->mnt, flags);
1714 	if (retval)
1715 		return retval;
1716 
1717 	/*
1718 	 * Allow userspace to request a mountpoint be expired rather than
1719 	 * unmounting unconditionally. Unmount only happens if:
1720 	 *  (1) the mark is already set (the mark is cleared by mntput())
1721 	 *  (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
1722 	 */
1723 	if (flags & MNT_EXPIRE) {
1724 		if (&mnt->mnt == current->fs->root.mnt ||
1725 		    flags & (MNT_FORCE | MNT_DETACH))
1726 			return -EINVAL;
1727 
1728 		/*
1729 		 * probably don't strictly need the lock here if we examined
1730 		 * all race cases, but it's a slowpath.
1731 		 */
1732 		lock_mount_hash();
1733 		if (mnt_get_count(mnt) != 2) {
1734 			unlock_mount_hash();
1735 			return -EBUSY;
1736 		}
1737 		unlock_mount_hash();
1738 
1739 		if (!xchg(&mnt->mnt_expiry_mark, 1))
1740 			return -EAGAIN;
1741 	}
1742 
1743 	/*
1744 	 * If we may have to abort operations to get out of this
1745 	 * mount, and they will themselves hold resources we must
1746 	 * allow the fs to do things. In the Unix tradition of
1747 	 * 'Gee thats tricky lets do it in userspace' the umount_begin
1748 	 * might fail to complete on the first run through as other tasks
1749 	 * must return, and the like. Thats for the mount program to worry
1750 	 * about for the moment.
1751 	 */
1752 
1753 	if (flags & MNT_FORCE && sb->s_op->umount_begin) {
1754 		sb->s_op->umount_begin(sb);
1755 	}
1756 
1757 	/*
1758 	 * No sense to grab the lock for this test, but test itself looks
1759 	 * somewhat bogus. Suggestions for better replacement?
1760 	 * Ho-hum... In principle, we might treat that as umount + switch
1761 	 * to rootfs. GC would eventually take care of the old vfsmount.
1762 	 * Actually it makes sense, especially if rootfs would contain a
1763 	 * /reboot - static binary that would close all descriptors and
1764 	 * call reboot(9). Then init(8) could umount root and exec /reboot.
1765 	 */
1766 	if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
1767 		/*
1768 		 * Special case for "unmounting" root ...
1769 		 * we just try to remount it readonly.
1770 		 */
1771 		if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN))
1772 			return -EPERM;
1773 		return do_umount_root(sb);
1774 	}
1775 
1776 	namespace_lock();
1777 	lock_mount_hash();
1778 
1779 	/* Recheck MNT_LOCKED with the locks held */
1780 	retval = -EINVAL;
1781 	if (mnt->mnt.mnt_flags & MNT_LOCKED)
1782 		goto out;
1783 
1784 	event++;
1785 	if (flags & MNT_DETACH) {
1786 		if (!list_empty(&mnt->mnt_list))
1787 			umount_tree(mnt, UMOUNT_PROPAGATE);
1788 		retval = 0;
1789 	} else {
1790 		shrink_submounts(mnt);
1791 		retval = -EBUSY;
1792 		if (!propagate_mount_busy(mnt, 2)) {
1793 			if (!list_empty(&mnt->mnt_list))
1794 				umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
1795 			retval = 0;
1796 		}
1797 	}
1798 out:
1799 	unlock_mount_hash();
1800 	namespace_unlock();
1801 	return retval;
1802 }
1803 
1804 /*
1805  * __detach_mounts - lazily unmount all mounts on the specified dentry
1806  *
1807  * During unlink, rmdir, and d_drop it is possible to loose the path
1808  * to an existing mountpoint, and wind up leaking the mount.
1809  * detach_mounts allows lazily unmounting those mounts instead of
1810  * leaking them.
1811  *
1812  * The caller may hold dentry->d_inode->i_mutex.
1813  */
__detach_mounts(struct dentry * dentry)1814 void __detach_mounts(struct dentry *dentry)
1815 {
1816 	struct mountpoint *mp;
1817 	struct mount *mnt;
1818 
1819 	namespace_lock();
1820 	lock_mount_hash();
1821 	mp = lookup_mountpoint(dentry);
1822 	if (!mp)
1823 		goto out_unlock;
1824 
1825 	event++;
1826 	while (!hlist_empty(&mp->m_list)) {
1827 		mnt = hlist_entry(mp->m_list.first, struct mount, mnt_mp_list);
1828 		if (mnt->mnt.mnt_flags & MNT_UMOUNT) {
1829 			umount_mnt(mnt);
1830 			hlist_add_head(&mnt->mnt_umount, &unmounted);
1831 		}
1832 		else umount_tree(mnt, UMOUNT_CONNECTED);
1833 	}
1834 	put_mountpoint(mp);
1835 out_unlock:
1836 	unlock_mount_hash();
1837 	namespace_unlock();
1838 }
1839 
1840 /*
1841  * Is the caller allowed to modify his namespace?
1842  */
may_mount(void)1843 bool may_mount(void)
1844 {
1845 	return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN);
1846 }
1847 
1848 /**
1849  * path_mounted - check whether path is mounted
1850  * @path: path to check
1851  *
1852  * Determine whether @path refers to the root of a mount.
1853  *
1854  * Return: true if @path is the root of a mount, false if not.
1855  */
path_mounted(const struct path * path)1856 static inline bool path_mounted(const struct path *path)
1857 {
1858 	return path->mnt->mnt_root == path->dentry;
1859 }
1860 
warn_mandlock(void)1861 static void warn_mandlock(void)
1862 {
1863 	pr_warn_once("=======================================================\n"
1864 		     "WARNING: The mand mount option has been deprecated and\n"
1865 		     "         and is ignored by this kernel. Remove the mand\n"
1866 		     "         option from the mount to silence this warning.\n"
1867 		     "=======================================================\n");
1868 }
1869 
can_umount(const struct path * path,int flags)1870 static int can_umount(const struct path *path, int flags)
1871 {
1872 	struct mount *mnt = real_mount(path->mnt);
1873 
1874 	if (!may_mount())
1875 		return -EPERM;
1876 	if (!path_mounted(path))
1877 		return -EINVAL;
1878 	if (!check_mnt(mnt))
1879 		return -EINVAL;
1880 	if (mnt->mnt.mnt_flags & MNT_LOCKED) /* Check optimistically */
1881 		return -EINVAL;
1882 	if (flags & MNT_FORCE && !capable(CAP_SYS_ADMIN))
1883 		return -EPERM;
1884 	return 0;
1885 }
1886 
1887 // caller is responsible for flags being sane
path_umount(struct path * path,int flags)1888 int path_umount(struct path *path, int flags)
1889 {
1890 	struct mount *mnt = real_mount(path->mnt);
1891 	int ret;
1892 
1893 	ret = can_umount(path, flags);
1894 	if (!ret)
1895 		ret = do_umount(mnt, flags);
1896 
1897 	/* we mustn't call path_put() as that would clear mnt_expiry_mark */
1898 	dput(path->dentry);
1899 	mntput_no_expire(mnt);
1900 	return ret;
1901 }
1902 
ksys_umount(char __user * name,int flags)1903 static int ksys_umount(char __user *name, int flags)
1904 {
1905 	int lookup_flags = LOOKUP_MOUNTPOINT;
1906 	struct path path;
1907 	int ret;
1908 
1909 	// basic validity checks done first
1910 	if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
1911 		return -EINVAL;
1912 
1913 	if (!(flags & UMOUNT_NOFOLLOW))
1914 		lookup_flags |= LOOKUP_FOLLOW;
1915 	ret = user_path_at(AT_FDCWD, name, lookup_flags, &path);
1916 	if (ret)
1917 		return ret;
1918 	return path_umount(&path, flags);
1919 }
1920 
SYSCALL_DEFINE2(umount,char __user *,name,int,flags)1921 SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
1922 {
1923 	return ksys_umount(name, flags);
1924 }
1925 
1926 #ifdef __ARCH_WANT_SYS_OLDUMOUNT
1927 
1928 /*
1929  *	The 2.0 compatible umount. No flags.
1930  */
SYSCALL_DEFINE1(oldumount,char __user *,name)1931 SYSCALL_DEFINE1(oldumount, char __user *, name)
1932 {
1933 	return ksys_umount(name, 0);
1934 }
1935 
1936 #endif
1937 
is_mnt_ns_file(struct dentry * dentry)1938 static bool is_mnt_ns_file(struct dentry *dentry)
1939 {
1940 	/* Is this a proxy for a mount namespace? */
1941 	return dentry->d_op == &ns_dentry_operations &&
1942 	       dentry->d_fsdata == &mntns_operations;
1943 }
1944 
to_mnt_ns(struct ns_common * ns)1945 static struct mnt_namespace *to_mnt_ns(struct ns_common *ns)
1946 {
1947 	return container_of(ns, struct mnt_namespace, ns);
1948 }
1949 
from_mnt_ns(struct mnt_namespace * mnt)1950 struct ns_common *from_mnt_ns(struct mnt_namespace *mnt)
1951 {
1952 	return &mnt->ns;
1953 }
1954 
mnt_ns_loop(struct dentry * dentry)1955 static bool mnt_ns_loop(struct dentry *dentry)
1956 {
1957 	/* Could bind mounting the mount namespace inode cause a
1958 	 * mount namespace loop?
1959 	 */
1960 	struct mnt_namespace *mnt_ns;
1961 	if (!is_mnt_ns_file(dentry))
1962 		return false;
1963 
1964 	mnt_ns = to_mnt_ns(get_proc_ns(dentry->d_inode));
1965 	return current->nsproxy->mnt_ns->seq >= mnt_ns->seq;
1966 }
1967 
copy_tree(struct mount * mnt,struct dentry * dentry,int flag)1968 struct mount *copy_tree(struct mount *mnt, struct dentry *dentry,
1969 					int flag)
1970 {
1971 	struct mount *res, *p, *q, *r, *parent;
1972 
1973 	if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt))
1974 		return ERR_PTR(-EINVAL);
1975 
1976 	if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry))
1977 		return ERR_PTR(-EINVAL);
1978 
1979 	res = q = clone_mnt(mnt, dentry, flag);
1980 	if (IS_ERR(q))
1981 		return q;
1982 
1983 	q->mnt_mountpoint = mnt->mnt_mountpoint;
1984 
1985 	p = mnt;
1986 	list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
1987 		struct mount *s;
1988 		if (!is_subdir(r->mnt_mountpoint, dentry))
1989 			continue;
1990 
1991 		for (s = r; s; s = next_mnt(s, r)) {
1992 			if (!(flag & CL_COPY_UNBINDABLE) &&
1993 			    IS_MNT_UNBINDABLE(s)) {
1994 				if (s->mnt.mnt_flags & MNT_LOCKED) {
1995 					/* Both unbindable and locked. */
1996 					q = ERR_PTR(-EPERM);
1997 					goto out;
1998 				} else {
1999 					s = skip_mnt_tree(s);
2000 					continue;
2001 				}
2002 			}
2003 			if (!(flag & CL_COPY_MNT_NS_FILE) &&
2004 			    is_mnt_ns_file(s->mnt.mnt_root)) {
2005 				s = skip_mnt_tree(s);
2006 				continue;
2007 			}
2008 			while (p != s->mnt_parent) {
2009 				p = p->mnt_parent;
2010 				q = q->mnt_parent;
2011 			}
2012 			p = s;
2013 			parent = q;
2014 			q = clone_mnt(p, p->mnt.mnt_root, flag);
2015 			if (IS_ERR(q))
2016 				goto out;
2017 			lock_mount_hash();
2018 			list_add_tail(&q->mnt_list, &res->mnt_list);
2019 			attach_mnt(q, parent, p->mnt_mp, false);
2020 			unlock_mount_hash();
2021 		}
2022 	}
2023 	return res;
2024 out:
2025 	if (res) {
2026 		lock_mount_hash();
2027 		umount_tree(res, UMOUNT_SYNC);
2028 		unlock_mount_hash();
2029 	}
2030 	return q;
2031 }
2032 
2033 /* Caller should check returned pointer for errors */
2034 
collect_mounts(const struct path * path)2035 struct vfsmount *collect_mounts(const struct path *path)
2036 {
2037 	struct mount *tree;
2038 	namespace_lock();
2039 	if (!check_mnt(real_mount(path->mnt)))
2040 		tree = ERR_PTR(-EINVAL);
2041 	else
2042 		tree = copy_tree(real_mount(path->mnt), path->dentry,
2043 				 CL_COPY_ALL | CL_PRIVATE);
2044 	namespace_unlock();
2045 	if (IS_ERR(tree))
2046 		return ERR_CAST(tree);
2047 	return &tree->mnt;
2048 }
2049 
2050 static void free_mnt_ns(struct mnt_namespace *);
2051 static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *, bool);
2052 
dissolve_on_fput(struct vfsmount * mnt)2053 void dissolve_on_fput(struct vfsmount *mnt)
2054 {
2055 	struct mnt_namespace *ns;
2056 	namespace_lock();
2057 	lock_mount_hash();
2058 	ns = real_mount(mnt)->mnt_ns;
2059 	if (ns) {
2060 		if (is_anon_ns(ns))
2061 			umount_tree(real_mount(mnt), UMOUNT_CONNECTED);
2062 		else
2063 			ns = NULL;
2064 	}
2065 	unlock_mount_hash();
2066 	namespace_unlock();
2067 	if (ns)
2068 		free_mnt_ns(ns);
2069 }
2070 
drop_collected_mounts(struct vfsmount * mnt)2071 void drop_collected_mounts(struct vfsmount *mnt)
2072 {
2073 	namespace_lock();
2074 	lock_mount_hash();
2075 	umount_tree(real_mount(mnt), 0);
2076 	unlock_mount_hash();
2077 	namespace_unlock();
2078 }
2079 
has_locked_children(struct mount * mnt,struct dentry * dentry)2080 static bool has_locked_children(struct mount *mnt, struct dentry *dentry)
2081 {
2082 	struct mount *child;
2083 
2084 	list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
2085 		if (!is_subdir(child->mnt_mountpoint, dentry))
2086 			continue;
2087 
2088 		if (child->mnt.mnt_flags & MNT_LOCKED)
2089 			return true;
2090 	}
2091 	return false;
2092 }
2093 
2094 /**
2095  * clone_private_mount - create a private clone of a path
2096  * @path: path to clone
2097  *
2098  * This creates a new vfsmount, which will be the clone of @path.  The new mount
2099  * will not be attached anywhere in the namespace and will be private (i.e.
2100  * changes to the originating mount won't be propagated into this).
2101  *
2102  * Release with mntput().
2103  */
clone_private_mount(const struct path * path)2104 struct vfsmount *clone_private_mount(const struct path *path)
2105 {
2106 	struct mount *old_mnt = real_mount(path->mnt);
2107 	struct mount *new_mnt;
2108 
2109 	down_read(&namespace_sem);
2110 	if (IS_MNT_UNBINDABLE(old_mnt))
2111 		goto invalid;
2112 
2113 	if (!check_mnt(old_mnt))
2114 		goto invalid;
2115 
2116 	if (has_locked_children(old_mnt, path->dentry))
2117 		goto invalid;
2118 
2119 	new_mnt = clone_mnt(old_mnt, path->dentry, CL_PRIVATE);
2120 	up_read(&namespace_sem);
2121 
2122 	if (IS_ERR(new_mnt))
2123 		return ERR_CAST(new_mnt);
2124 
2125 	/* Longterm mount to be removed by kern_unmount*() */
2126 	new_mnt->mnt_ns = MNT_NS_INTERNAL;
2127 
2128 	return &new_mnt->mnt;
2129 
2130 invalid:
2131 	up_read(&namespace_sem);
2132 	return ERR_PTR(-EINVAL);
2133 }
2134 EXPORT_SYMBOL_GPL(clone_private_mount);
2135 
iterate_mounts(int (* f)(struct vfsmount *,void *),void * arg,struct vfsmount * root)2136 int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
2137 		   struct vfsmount *root)
2138 {
2139 	struct mount *mnt;
2140 	int res = f(root, arg);
2141 	if (res)
2142 		return res;
2143 	list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) {
2144 		res = f(&mnt->mnt, arg);
2145 		if (res)
2146 			return res;
2147 	}
2148 	return 0;
2149 }
2150 
lock_mnt_tree(struct mount * mnt)2151 static void lock_mnt_tree(struct mount *mnt)
2152 {
2153 	struct mount *p;
2154 
2155 	for (p = mnt; p; p = next_mnt(p, mnt)) {
2156 		int flags = p->mnt.mnt_flags;
2157 		/* Don't allow unprivileged users to change mount flags */
2158 		flags |= MNT_LOCK_ATIME;
2159 
2160 		if (flags & MNT_READONLY)
2161 			flags |= MNT_LOCK_READONLY;
2162 
2163 		if (flags & MNT_NODEV)
2164 			flags |= MNT_LOCK_NODEV;
2165 
2166 		if (flags & MNT_NOSUID)
2167 			flags |= MNT_LOCK_NOSUID;
2168 
2169 		if (flags & MNT_NOEXEC)
2170 			flags |= MNT_LOCK_NOEXEC;
2171 		/* Don't allow unprivileged users to reveal what is under a mount */
2172 		if (list_empty(&p->mnt_expire))
2173 			flags |= MNT_LOCKED;
2174 		p->mnt.mnt_flags = flags;
2175 	}
2176 }
2177 
cleanup_group_ids(struct mount * mnt,struct mount * end)2178 static void cleanup_group_ids(struct mount *mnt, struct mount *end)
2179 {
2180 	struct mount *p;
2181 
2182 	for (p = mnt; p != end; p = next_mnt(p, mnt)) {
2183 		if (p->mnt_group_id && !IS_MNT_SHARED(p))
2184 			mnt_release_group_id(p);
2185 	}
2186 }
2187 
invent_group_ids(struct mount * mnt,bool recurse)2188 static int invent_group_ids(struct mount *mnt, bool recurse)
2189 {
2190 	struct mount *p;
2191 
2192 	for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
2193 		if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
2194 			int err = mnt_alloc_group_id(p);
2195 			if (err) {
2196 				cleanup_group_ids(mnt, p);
2197 				return err;
2198 			}
2199 		}
2200 	}
2201 
2202 	return 0;
2203 }
2204 
count_mounts(struct mnt_namespace * ns,struct mount * mnt)2205 int count_mounts(struct mnt_namespace *ns, struct mount *mnt)
2206 {
2207 	unsigned int max = READ_ONCE(sysctl_mount_max);
2208 	unsigned int mounts = 0;
2209 	struct mount *p;
2210 
2211 	if (ns->mounts >= max)
2212 		return -ENOSPC;
2213 	max -= ns->mounts;
2214 	if (ns->pending_mounts >= max)
2215 		return -ENOSPC;
2216 	max -= ns->pending_mounts;
2217 
2218 	for (p = mnt; p; p = next_mnt(p, mnt))
2219 		mounts++;
2220 
2221 	if (mounts > max)
2222 		return -ENOSPC;
2223 
2224 	ns->pending_mounts += mounts;
2225 	return 0;
2226 }
2227 
2228 enum mnt_tree_flags_t {
2229 	MNT_TREE_MOVE = BIT(0),
2230 	MNT_TREE_BENEATH = BIT(1),
2231 };
2232 
2233 /**
2234  * attach_recursive_mnt - attach a source mount tree
2235  * @source_mnt: mount tree to be attached
2236  * @top_mnt:    mount that @source_mnt will be mounted on or mounted beneath
2237  * @dest_mp:    the mountpoint @source_mnt will be mounted at
2238  * @flags:      modify how @source_mnt is supposed to be attached
2239  *
2240  *  NOTE: in the table below explains the semantics when a source mount
2241  *  of a given type is attached to a destination mount of a given type.
2242  * ---------------------------------------------------------------------------
2243  * |         BIND MOUNT OPERATION                                            |
2244  * |**************************************************************************
2245  * | source-->| shared        |       private  |       slave    | unbindable |
2246  * | dest     |               |                |                |            |
2247  * |   |      |               |                |                |            |
2248  * |   v      |               |                |                |            |
2249  * |**************************************************************************
2250  * |  shared  | shared (++)   |     shared (+) |     shared(+++)|  invalid   |
2251  * |          |               |                |                |            |
2252  * |non-shared| shared (+)    |      private   |      slave (*) |  invalid   |
2253  * ***************************************************************************
2254  * A bind operation clones the source mount and mounts the clone on the
2255  * destination mount.
2256  *
2257  * (++)  the cloned mount is propagated to all the mounts in the propagation
2258  * 	 tree of the destination mount and the cloned mount is added to
2259  * 	 the peer group of the source mount.
2260  * (+)   the cloned mount is created under the destination mount and is marked
2261  *       as shared. The cloned mount is added to the peer group of the source
2262  *       mount.
2263  * (+++) the mount is propagated to all the mounts in the propagation tree
2264  *       of the destination mount and the cloned mount is made slave
2265  *       of the same master as that of the source mount. The cloned mount
2266  *       is marked as 'shared and slave'.
2267  * (*)   the cloned mount is made a slave of the same master as that of the
2268  * 	 source mount.
2269  *
2270  * ---------------------------------------------------------------------------
2271  * |         		MOVE MOUNT OPERATION                                 |
2272  * |**************************************************************************
2273  * | source-->| shared        |       private  |       slave    | unbindable |
2274  * | dest     |               |                |                |            |
2275  * |   |      |               |                |                |            |
2276  * |   v      |               |                |                |            |
2277  * |**************************************************************************
2278  * |  shared  | shared (+)    |     shared (+) |    shared(+++) |  invalid   |
2279  * |          |               |                |                |            |
2280  * |non-shared| shared (+*)   |      private   |    slave (*)   | unbindable |
2281  * ***************************************************************************
2282  *
2283  * (+)  the mount is moved to the destination. And is then propagated to
2284  * 	all the mounts in the propagation tree of the destination mount.
2285  * (+*)  the mount is moved to the destination.
2286  * (+++)  the mount is moved to the destination and is then propagated to
2287  * 	all the mounts belonging to the destination mount's propagation tree.
2288  * 	the mount is marked as 'shared and slave'.
2289  * (*)	the mount continues to be a slave at the new location.
2290  *
2291  * if the source mount is a tree, the operations explained above is
2292  * applied to each mount in the tree.
2293  * Must be called without spinlocks held, since this function can sleep
2294  * in allocations.
2295  *
2296  * Context: The function expects namespace_lock() to be held.
2297  * Return: If @source_mnt was successfully attached 0 is returned.
2298  *         Otherwise a negative error code is returned.
2299  */
attach_recursive_mnt(struct mount * source_mnt,struct mount * top_mnt,struct mountpoint * dest_mp,enum mnt_tree_flags_t flags)2300 static int attach_recursive_mnt(struct mount *source_mnt,
2301 				struct mount *top_mnt,
2302 				struct mountpoint *dest_mp,
2303 				enum mnt_tree_flags_t flags)
2304 {
2305 	struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns;
2306 	HLIST_HEAD(tree_list);
2307 	struct mnt_namespace *ns = top_mnt->mnt_ns;
2308 	struct mountpoint *smp;
2309 	struct mount *child, *dest_mnt, *p;
2310 	struct hlist_node *n;
2311 	int err = 0;
2312 	bool moving = flags & MNT_TREE_MOVE, beneath = flags & MNT_TREE_BENEATH;
2313 
2314 	/*
2315 	 * Preallocate a mountpoint in case the new mounts need to be
2316 	 * mounted beneath mounts on the same mountpoint.
2317 	 */
2318 	smp = get_mountpoint(source_mnt->mnt.mnt_root);
2319 	if (IS_ERR(smp))
2320 		return PTR_ERR(smp);
2321 
2322 	/* Is there space to add these mounts to the mount namespace? */
2323 	if (!moving) {
2324 		err = count_mounts(ns, source_mnt);
2325 		if (err)
2326 			goto out;
2327 	}
2328 
2329 	if (beneath)
2330 		dest_mnt = top_mnt->mnt_parent;
2331 	else
2332 		dest_mnt = top_mnt;
2333 
2334 	if (IS_MNT_SHARED(dest_mnt)) {
2335 		err = invent_group_ids(source_mnt, true);
2336 		if (err)
2337 			goto out;
2338 		err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list);
2339 	}
2340 	lock_mount_hash();
2341 	if (err)
2342 		goto out_cleanup_ids;
2343 
2344 	if (IS_MNT_SHARED(dest_mnt)) {
2345 		for (p = source_mnt; p; p = next_mnt(p, source_mnt))
2346 			set_mnt_shared(p);
2347 	}
2348 
2349 	if (moving) {
2350 		if (beneath)
2351 			dest_mp = smp;
2352 		unhash_mnt(source_mnt);
2353 		attach_mnt(source_mnt, top_mnt, dest_mp, beneath);
2354 		touch_mnt_namespace(source_mnt->mnt_ns);
2355 	} else {
2356 		if (source_mnt->mnt_ns) {
2357 			/* move from anon - the caller will destroy */
2358 			list_del_init(&source_mnt->mnt_ns->list);
2359 		}
2360 		if (beneath)
2361 			mnt_set_mountpoint_beneath(source_mnt, top_mnt, smp);
2362 		else
2363 			mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt);
2364 		commit_tree(source_mnt);
2365 	}
2366 
2367 	hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) {
2368 		struct mount *q;
2369 		hlist_del_init(&child->mnt_hash);
2370 		q = __lookup_mnt(&child->mnt_parent->mnt,
2371 				 child->mnt_mountpoint);
2372 		if (q)
2373 			mnt_change_mountpoint(child, smp, q);
2374 		/* Notice when we are propagating across user namespaces */
2375 		if (child->mnt_parent->mnt_ns->user_ns != user_ns)
2376 			lock_mnt_tree(child);
2377 		child->mnt.mnt_flags &= ~MNT_LOCKED;
2378 		commit_tree(child);
2379 	}
2380 	put_mountpoint(smp);
2381 	unlock_mount_hash();
2382 
2383 	return 0;
2384 
2385  out_cleanup_ids:
2386 	while (!hlist_empty(&tree_list)) {
2387 		child = hlist_entry(tree_list.first, struct mount, mnt_hash);
2388 		child->mnt_parent->mnt_ns->pending_mounts = 0;
2389 		umount_tree(child, UMOUNT_SYNC);
2390 	}
2391 	unlock_mount_hash();
2392 	cleanup_group_ids(source_mnt, NULL);
2393  out:
2394 	ns->pending_mounts = 0;
2395 
2396 	read_seqlock_excl(&mount_lock);
2397 	put_mountpoint(smp);
2398 	read_sequnlock_excl(&mount_lock);
2399 
2400 	return err;
2401 }
2402 
2403 /**
2404  * do_lock_mount - lock mount and mountpoint
2405  * @path:    target path
2406  * @beneath: whether the intention is to mount beneath @path
2407  *
2408  * Follow the mount stack on @path until the top mount @mnt is found. If
2409  * the initial @path->{mnt,dentry} is a mountpoint lookup the first
2410  * mount stacked on top of it. Then simply follow @{mnt,mnt->mnt_root}
2411  * until nothing is stacked on top of it anymore.
2412  *
2413  * Acquire the inode_lock() on the top mount's ->mnt_root to protect
2414  * against concurrent removal of the new mountpoint from another mount
2415  * namespace.
2416  *
2417  * If @beneath is requested, acquire inode_lock() on @mnt's mountpoint
2418  * @mp on @mnt->mnt_parent must be acquired. This protects against a
2419  * concurrent unlink of @mp->mnt_dentry from another mount namespace
2420  * where @mnt doesn't have a child mount mounted @mp. A concurrent
2421  * removal of @mnt->mnt_root doesn't matter as nothing will be mounted
2422  * on top of it for @beneath.
2423  *
2424  * In addition, @beneath needs to make sure that @mnt hasn't been
2425  * unmounted or moved from its current mountpoint in between dropping
2426  * @mount_lock and acquiring @namespace_sem. For the !@beneath case @mnt
2427  * being unmounted would be detected later by e.g., calling
2428  * check_mnt(mnt) in the function it's called from. For the @beneath
2429  * case however, it's useful to detect it directly in do_lock_mount().
2430  * If @mnt hasn't been unmounted then @mnt->mnt_mountpoint still points
2431  * to @mnt->mnt_mp->m_dentry. But if @mnt has been unmounted it will
2432  * point to @mnt->mnt_root and @mnt->mnt_mp will be NULL.
2433  *
2434  * Return: Either the target mountpoint on the top mount or the top
2435  *         mount's mountpoint.
2436  */
do_lock_mount(struct path * path,bool beneath)2437 static struct mountpoint *do_lock_mount(struct path *path, bool beneath)
2438 {
2439 	struct vfsmount *mnt = path->mnt;
2440 	struct dentry *dentry;
2441 	struct mountpoint *mp = ERR_PTR(-ENOENT);
2442 
2443 	for (;;) {
2444 		struct mount *m;
2445 
2446 		if (beneath) {
2447 			m = real_mount(mnt);
2448 			read_seqlock_excl(&mount_lock);
2449 			dentry = dget(m->mnt_mountpoint);
2450 			read_sequnlock_excl(&mount_lock);
2451 		} else {
2452 			dentry = path->dentry;
2453 		}
2454 
2455 		inode_lock(dentry->d_inode);
2456 		if (unlikely(cant_mount(dentry))) {
2457 			inode_unlock(dentry->d_inode);
2458 			goto out;
2459 		}
2460 
2461 		namespace_lock();
2462 
2463 		if (beneath && (!is_mounted(mnt) || m->mnt_mountpoint != dentry)) {
2464 			namespace_unlock();
2465 			inode_unlock(dentry->d_inode);
2466 			goto out;
2467 		}
2468 
2469 		mnt = lookup_mnt(path);
2470 		if (likely(!mnt))
2471 			break;
2472 
2473 		namespace_unlock();
2474 		inode_unlock(dentry->d_inode);
2475 		if (beneath)
2476 			dput(dentry);
2477 		path_put(path);
2478 		path->mnt = mnt;
2479 		path->dentry = dget(mnt->mnt_root);
2480 	}
2481 
2482 	mp = get_mountpoint(dentry);
2483 	if (IS_ERR(mp)) {
2484 		namespace_unlock();
2485 		inode_unlock(dentry->d_inode);
2486 	}
2487 
2488 out:
2489 	if (beneath)
2490 		dput(dentry);
2491 
2492 	return mp;
2493 }
2494 
lock_mount(struct path * path)2495 static inline struct mountpoint *lock_mount(struct path *path)
2496 {
2497 	return do_lock_mount(path, false);
2498 }
2499 
unlock_mount(struct mountpoint * where)2500 static void unlock_mount(struct mountpoint *where)
2501 {
2502 	struct dentry *dentry = where->m_dentry;
2503 
2504 	read_seqlock_excl(&mount_lock);
2505 	put_mountpoint(where);
2506 	read_sequnlock_excl(&mount_lock);
2507 
2508 	namespace_unlock();
2509 	inode_unlock(dentry->d_inode);
2510 }
2511 
graft_tree(struct mount * mnt,struct mount * p,struct mountpoint * mp)2512 static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp)
2513 {
2514 	if (mnt->mnt.mnt_sb->s_flags & SB_NOUSER)
2515 		return -EINVAL;
2516 
2517 	if (d_is_dir(mp->m_dentry) !=
2518 	      d_is_dir(mnt->mnt.mnt_root))
2519 		return -ENOTDIR;
2520 
2521 	return attach_recursive_mnt(mnt, p, mp, 0);
2522 }
2523 
2524 /*
2525  * Sanity check the flags to change_mnt_propagation.
2526  */
2527 
flags_to_propagation_type(int ms_flags)2528 static int flags_to_propagation_type(int ms_flags)
2529 {
2530 	int type = ms_flags & ~(MS_REC | MS_SILENT);
2531 
2532 	/* Fail if any non-propagation flags are set */
2533 	if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
2534 		return 0;
2535 	/* Only one propagation flag should be set */
2536 	if (!is_power_of_2(type))
2537 		return 0;
2538 	return type;
2539 }
2540 
2541 /*
2542  * recursively change the type of the mountpoint.
2543  */
do_change_type(struct path * path,int ms_flags)2544 static int do_change_type(struct path *path, int ms_flags)
2545 {
2546 	struct mount *m;
2547 	struct mount *mnt = real_mount(path->mnt);
2548 	int recurse = ms_flags & MS_REC;
2549 	int type;
2550 	int err = 0;
2551 
2552 	if (!path_mounted(path))
2553 		return -EINVAL;
2554 
2555 	type = flags_to_propagation_type(ms_flags);
2556 	if (!type)
2557 		return -EINVAL;
2558 
2559 	namespace_lock();
2560 	if (type == MS_SHARED) {
2561 		err = invent_group_ids(mnt, recurse);
2562 		if (err)
2563 			goto out_unlock;
2564 	}
2565 
2566 	lock_mount_hash();
2567 	for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
2568 		change_mnt_propagation(m, type);
2569 	unlock_mount_hash();
2570 
2571  out_unlock:
2572 	namespace_unlock();
2573 	return err;
2574 }
2575 
__do_loopback(struct path * old_path,int recurse)2576 static struct mount *__do_loopback(struct path *old_path, int recurse)
2577 {
2578 	struct mount *mnt = ERR_PTR(-EINVAL), *old = real_mount(old_path->mnt);
2579 
2580 	if (IS_MNT_UNBINDABLE(old))
2581 		return mnt;
2582 
2583 	if (!check_mnt(old) && old_path->dentry->d_op != &ns_dentry_operations)
2584 		return mnt;
2585 
2586 	if (!recurse && has_locked_children(old, old_path->dentry))
2587 		return mnt;
2588 
2589 	if (recurse)
2590 		mnt = copy_tree(old, old_path->dentry, CL_COPY_MNT_NS_FILE);
2591 	else
2592 		mnt = clone_mnt(old, old_path->dentry, 0);
2593 
2594 	if (!IS_ERR(mnt))
2595 		mnt->mnt.mnt_flags &= ~MNT_LOCKED;
2596 
2597 	return mnt;
2598 }
2599 
2600 /*
2601  * do loopback mount.
2602  */
do_loopback(struct path * path,const char * old_name,int recurse)2603 static int do_loopback(struct path *path, const char *old_name,
2604 				int recurse)
2605 {
2606 	struct path old_path;
2607 	struct mount *mnt = NULL, *parent;
2608 	struct mountpoint *mp;
2609 	int err;
2610 	if (!old_name || !*old_name)
2611 		return -EINVAL;
2612 	err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
2613 	if (err)
2614 		return err;
2615 
2616 	err = -EINVAL;
2617 	if (mnt_ns_loop(old_path.dentry))
2618 		goto out;
2619 
2620 	mp = lock_mount(path);
2621 	if (IS_ERR(mp)) {
2622 		err = PTR_ERR(mp);
2623 		goto out;
2624 	}
2625 
2626 	parent = real_mount(path->mnt);
2627 	if (!check_mnt(parent))
2628 		goto out2;
2629 
2630 	mnt = __do_loopback(&old_path, recurse);
2631 	if (IS_ERR(mnt)) {
2632 		err = PTR_ERR(mnt);
2633 		goto out2;
2634 	}
2635 
2636 	err = graft_tree(mnt, parent, mp);
2637 	if (err) {
2638 		lock_mount_hash();
2639 		umount_tree(mnt, UMOUNT_SYNC);
2640 		unlock_mount_hash();
2641 	}
2642 out2:
2643 	unlock_mount(mp);
2644 out:
2645 	path_put(&old_path);
2646 	return err;
2647 }
2648 
open_detached_copy(struct path * path,bool recursive)2649 static struct file *open_detached_copy(struct path *path, bool recursive)
2650 {
2651 	struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns;
2652 	struct mnt_namespace *ns = alloc_mnt_ns(user_ns, true);
2653 	struct mount *mnt, *p;
2654 	struct file *file;
2655 
2656 	if (IS_ERR(ns))
2657 		return ERR_CAST(ns);
2658 
2659 	namespace_lock();
2660 	mnt = __do_loopback(path, recursive);
2661 	if (IS_ERR(mnt)) {
2662 		namespace_unlock();
2663 		free_mnt_ns(ns);
2664 		return ERR_CAST(mnt);
2665 	}
2666 
2667 	lock_mount_hash();
2668 	for (p = mnt; p; p = next_mnt(p, mnt)) {
2669 		p->mnt_ns = ns;
2670 		ns->mounts++;
2671 	}
2672 	ns->root = mnt;
2673 	list_add_tail(&ns->list, &mnt->mnt_list);
2674 	mntget(&mnt->mnt);
2675 	unlock_mount_hash();
2676 	namespace_unlock();
2677 
2678 	mntput(path->mnt);
2679 	path->mnt = &mnt->mnt;
2680 	file = dentry_open(path, O_PATH, current_cred());
2681 	if (IS_ERR(file))
2682 		dissolve_on_fput(path->mnt);
2683 	else
2684 		file->f_mode |= FMODE_NEED_UNMOUNT;
2685 	return file;
2686 }
2687 
SYSCALL_DEFINE3(open_tree,int,dfd,const char __user *,filename,unsigned,flags)2688 SYSCALL_DEFINE3(open_tree, int, dfd, const char __user *, filename, unsigned, flags)
2689 {
2690 	struct file *file;
2691 	struct path path;
2692 	int lookup_flags = LOOKUP_AUTOMOUNT | LOOKUP_FOLLOW;
2693 	bool detached = flags & OPEN_TREE_CLONE;
2694 	int error;
2695 	int fd;
2696 
2697 	BUILD_BUG_ON(OPEN_TREE_CLOEXEC != O_CLOEXEC);
2698 
2699 	if (flags & ~(AT_EMPTY_PATH | AT_NO_AUTOMOUNT | AT_RECURSIVE |
2700 		      AT_SYMLINK_NOFOLLOW | OPEN_TREE_CLONE |
2701 		      OPEN_TREE_CLOEXEC))
2702 		return -EINVAL;
2703 
2704 	if ((flags & (AT_RECURSIVE | OPEN_TREE_CLONE)) == AT_RECURSIVE)
2705 		return -EINVAL;
2706 
2707 	if (flags & AT_NO_AUTOMOUNT)
2708 		lookup_flags &= ~LOOKUP_AUTOMOUNT;
2709 	if (flags & AT_SYMLINK_NOFOLLOW)
2710 		lookup_flags &= ~LOOKUP_FOLLOW;
2711 	if (flags & AT_EMPTY_PATH)
2712 		lookup_flags |= LOOKUP_EMPTY;
2713 
2714 	if (detached && !may_mount())
2715 		return -EPERM;
2716 
2717 	fd = get_unused_fd_flags(flags & O_CLOEXEC);
2718 	if (fd < 0)
2719 		return fd;
2720 
2721 	error = user_path_at(dfd, filename, lookup_flags, &path);
2722 	if (unlikely(error)) {
2723 		file = ERR_PTR(error);
2724 	} else {
2725 		if (detached)
2726 			file = open_detached_copy(&path, flags & AT_RECURSIVE);
2727 		else
2728 			file = dentry_open(&path, O_PATH, current_cred());
2729 		path_put(&path);
2730 	}
2731 	if (IS_ERR(file)) {
2732 		put_unused_fd(fd);
2733 		return PTR_ERR(file);
2734 	}
2735 	fd_install(fd, file);
2736 	return fd;
2737 }
2738 
2739 /*
2740  * Don't allow locked mount flags to be cleared.
2741  *
2742  * No locks need to be held here while testing the various MNT_LOCK
2743  * flags because those flags can never be cleared once they are set.
2744  */
can_change_locked_flags(struct mount * mnt,unsigned int mnt_flags)2745 static bool can_change_locked_flags(struct mount *mnt, unsigned int mnt_flags)
2746 {
2747 	unsigned int fl = mnt->mnt.mnt_flags;
2748 
2749 	if ((fl & MNT_LOCK_READONLY) &&
2750 	    !(mnt_flags & MNT_READONLY))
2751 		return false;
2752 
2753 	if ((fl & MNT_LOCK_NODEV) &&
2754 	    !(mnt_flags & MNT_NODEV))
2755 		return false;
2756 
2757 	if ((fl & MNT_LOCK_NOSUID) &&
2758 	    !(mnt_flags & MNT_NOSUID))
2759 		return false;
2760 
2761 	if ((fl & MNT_LOCK_NOEXEC) &&
2762 	    !(mnt_flags & MNT_NOEXEC))
2763 		return false;
2764 
2765 	if ((fl & MNT_LOCK_ATIME) &&
2766 	    ((fl & MNT_ATIME_MASK) != (mnt_flags & MNT_ATIME_MASK)))
2767 		return false;
2768 
2769 	return true;
2770 }
2771 
change_mount_ro_state(struct mount * mnt,unsigned int mnt_flags)2772 static int change_mount_ro_state(struct mount *mnt, unsigned int mnt_flags)
2773 {
2774 	bool readonly_request = (mnt_flags & MNT_READONLY);
2775 
2776 	if (readonly_request == __mnt_is_readonly(&mnt->mnt))
2777 		return 0;
2778 
2779 	if (readonly_request)
2780 		return mnt_make_readonly(mnt);
2781 
2782 	mnt->mnt.mnt_flags &= ~MNT_READONLY;
2783 	return 0;
2784 }
2785 
set_mount_attributes(struct mount * mnt,unsigned int mnt_flags)2786 static void set_mount_attributes(struct mount *mnt, unsigned int mnt_flags)
2787 {
2788 	mnt_flags |= mnt->mnt.mnt_flags & ~MNT_USER_SETTABLE_MASK;
2789 	mnt->mnt.mnt_flags = mnt_flags;
2790 	touch_mnt_namespace(mnt->mnt_ns);
2791 }
2792 
mnt_warn_timestamp_expiry(struct path * mountpoint,struct vfsmount * mnt)2793 static void mnt_warn_timestamp_expiry(struct path *mountpoint, struct vfsmount *mnt)
2794 {
2795 	struct super_block *sb = mnt->mnt_sb;
2796 
2797 	if (!__mnt_is_readonly(mnt) &&
2798 	   (!(sb->s_iflags & SB_I_TS_EXPIRY_WARNED)) &&
2799 	   (ktime_get_real_seconds() + TIME_UPTIME_SEC_MAX > sb->s_time_max)) {
2800 		char *buf = (char *)__get_free_page(GFP_KERNEL);
2801 		char *mntpath = buf ? d_path(mountpoint, buf, PAGE_SIZE) : ERR_PTR(-ENOMEM);
2802 
2803 		pr_warn("%s filesystem being %s at %s supports timestamps until %ptTd (0x%llx)\n",
2804 			sb->s_type->name,
2805 			is_mounted(mnt) ? "remounted" : "mounted",
2806 			mntpath, &sb->s_time_max,
2807 			(unsigned long long)sb->s_time_max);
2808 
2809 		free_page((unsigned long)buf);
2810 		sb->s_iflags |= SB_I_TS_EXPIRY_WARNED;
2811 	}
2812 }
2813 
2814 /*
2815  * Handle reconfiguration of the mountpoint only without alteration of the
2816  * superblock it refers to.  This is triggered by specifying MS_REMOUNT|MS_BIND
2817  * to mount(2).
2818  */
do_reconfigure_mnt(struct path * path,unsigned int mnt_flags)2819 static int do_reconfigure_mnt(struct path *path, unsigned int mnt_flags)
2820 {
2821 	struct super_block *sb = path->mnt->mnt_sb;
2822 	struct mount *mnt = real_mount(path->mnt);
2823 	int ret;
2824 
2825 	if (!check_mnt(mnt))
2826 		return -EINVAL;
2827 
2828 	if (!path_mounted(path))
2829 		return -EINVAL;
2830 
2831 	if (!can_change_locked_flags(mnt, mnt_flags))
2832 		return -EPERM;
2833 
2834 	/*
2835 	 * We're only checking whether the superblock is read-only not
2836 	 * changing it, so only take down_read(&sb->s_umount).
2837 	 */
2838 	down_read(&sb->s_umount);
2839 	lock_mount_hash();
2840 	ret = change_mount_ro_state(mnt, mnt_flags);
2841 	if (ret == 0)
2842 		set_mount_attributes(mnt, mnt_flags);
2843 	unlock_mount_hash();
2844 	up_read(&sb->s_umount);
2845 
2846 	mnt_warn_timestamp_expiry(path, &mnt->mnt);
2847 
2848 	return ret;
2849 }
2850 
2851 /*
2852  * change filesystem flags. dir should be a physical root of filesystem.
2853  * If you've mounted a non-root directory somewhere and want to do remount
2854  * on it - tough luck.
2855  */
do_remount(struct path * path,int ms_flags,int sb_flags,int mnt_flags,void * data)2856 static int do_remount(struct path *path, int ms_flags, int sb_flags,
2857 		      int mnt_flags, void *data)
2858 {
2859 	int err;
2860 	struct super_block *sb = path->mnt->mnt_sb;
2861 	struct mount *mnt = real_mount(path->mnt);
2862 	struct fs_context *fc;
2863 
2864 	if (!check_mnt(mnt))
2865 		return -EINVAL;
2866 
2867 	if (!path_mounted(path))
2868 		return -EINVAL;
2869 
2870 	if (!can_change_locked_flags(mnt, mnt_flags))
2871 		return -EPERM;
2872 
2873 	fc = fs_context_for_reconfigure(path->dentry, sb_flags, MS_RMT_MASK);
2874 	if (IS_ERR(fc))
2875 		return PTR_ERR(fc);
2876 
2877 	/*
2878 	 * Indicate to the filesystem that the remount request is coming
2879 	 * from the legacy mount system call.
2880 	 */
2881 	fc->oldapi = true;
2882 
2883 	err = parse_monolithic_mount_data(fc, data);
2884 	if (!err) {
2885 		down_write(&sb->s_umount);
2886 		err = -EPERM;
2887 		if (ns_capable(sb->s_user_ns, CAP_SYS_ADMIN)) {
2888 			err = reconfigure_super(fc);
2889 			if (!err) {
2890 				lock_mount_hash();
2891 				set_mount_attributes(mnt, mnt_flags);
2892 				unlock_mount_hash();
2893 			}
2894 		}
2895 		up_write(&sb->s_umount);
2896 	}
2897 
2898 	mnt_warn_timestamp_expiry(path, &mnt->mnt);
2899 
2900 	put_fs_context(fc);
2901 	return err;
2902 }
2903 
tree_contains_unbindable(struct mount * mnt)2904 static inline int tree_contains_unbindable(struct mount *mnt)
2905 {
2906 	struct mount *p;
2907 	for (p = mnt; p; p = next_mnt(p, mnt)) {
2908 		if (IS_MNT_UNBINDABLE(p))
2909 			return 1;
2910 	}
2911 	return 0;
2912 }
2913 
2914 /*
2915  * Check that there aren't references to earlier/same mount namespaces in the
2916  * specified subtree.  Such references can act as pins for mount namespaces
2917  * that aren't checked by the mount-cycle checking code, thereby allowing
2918  * cycles to be made.
2919  */
check_for_nsfs_mounts(struct mount * subtree)2920 static bool check_for_nsfs_mounts(struct mount *subtree)
2921 {
2922 	struct mount *p;
2923 	bool ret = false;
2924 
2925 	lock_mount_hash();
2926 	for (p = subtree; p; p = next_mnt(p, subtree))
2927 		if (mnt_ns_loop(p->mnt.mnt_root))
2928 			goto out;
2929 
2930 	ret = true;
2931 out:
2932 	unlock_mount_hash();
2933 	return ret;
2934 }
2935 
do_set_group(struct path * from_path,struct path * to_path)2936 static int do_set_group(struct path *from_path, struct path *to_path)
2937 {
2938 	struct mount *from, *to;
2939 	int err;
2940 
2941 	from = real_mount(from_path->mnt);
2942 	to = real_mount(to_path->mnt);
2943 
2944 	namespace_lock();
2945 
2946 	err = -EINVAL;
2947 	/* To and From must be mounted */
2948 	if (!is_mounted(&from->mnt))
2949 		goto out;
2950 	if (!is_mounted(&to->mnt))
2951 		goto out;
2952 
2953 	err = -EPERM;
2954 	/* We should be allowed to modify mount namespaces of both mounts */
2955 	if (!ns_capable(from->mnt_ns->user_ns, CAP_SYS_ADMIN))
2956 		goto out;
2957 	if (!ns_capable(to->mnt_ns->user_ns, CAP_SYS_ADMIN))
2958 		goto out;
2959 
2960 	err = -EINVAL;
2961 	/* To and From paths should be mount roots */
2962 	if (!path_mounted(from_path))
2963 		goto out;
2964 	if (!path_mounted(to_path))
2965 		goto out;
2966 
2967 	/* Setting sharing groups is only allowed across same superblock */
2968 	if (from->mnt.mnt_sb != to->mnt.mnt_sb)
2969 		goto out;
2970 
2971 	/* From mount root should be wider than To mount root */
2972 	if (!is_subdir(to->mnt.mnt_root, from->mnt.mnt_root))
2973 		goto out;
2974 
2975 	/* From mount should not have locked children in place of To's root */
2976 	if (has_locked_children(from, to->mnt.mnt_root))
2977 		goto out;
2978 
2979 	/* Setting sharing groups is only allowed on private mounts */
2980 	if (IS_MNT_SHARED(to) || IS_MNT_SLAVE(to))
2981 		goto out;
2982 
2983 	/* From should not be private */
2984 	if (!IS_MNT_SHARED(from) && !IS_MNT_SLAVE(from))
2985 		goto out;
2986 
2987 	if (IS_MNT_SLAVE(from)) {
2988 		struct mount *m = from->mnt_master;
2989 
2990 		list_add(&to->mnt_slave, &m->mnt_slave_list);
2991 		to->mnt_master = m;
2992 	}
2993 
2994 	if (IS_MNT_SHARED(from)) {
2995 		to->mnt_group_id = from->mnt_group_id;
2996 		list_add(&to->mnt_share, &from->mnt_share);
2997 		lock_mount_hash();
2998 		set_mnt_shared(to);
2999 		unlock_mount_hash();
3000 	}
3001 
3002 	err = 0;
3003 out:
3004 	namespace_unlock();
3005 	return err;
3006 }
3007 
3008 /**
3009  * path_overmounted - check if path is overmounted
3010  * @path: path to check
3011  *
3012  * Check if path is overmounted, i.e., if there's a mount on top of
3013  * @path->mnt with @path->dentry as mountpoint.
3014  *
3015  * Context: This function expects namespace_lock() to be held.
3016  * Return: If path is overmounted true is returned, false if not.
3017  */
path_overmounted(const struct path * path)3018 static inline bool path_overmounted(const struct path *path)
3019 {
3020 	rcu_read_lock();
3021 	if (unlikely(__lookup_mnt(path->mnt, path->dentry))) {
3022 		rcu_read_unlock();
3023 		return true;
3024 	}
3025 	rcu_read_unlock();
3026 	return false;
3027 }
3028 
3029 /**
3030  * can_move_mount_beneath - check that we can mount beneath the top mount
3031  * @from: mount to mount beneath
3032  * @to:   mount under which to mount
3033  *
3034  * - Make sure that @to->dentry is actually the root of a mount under
3035  *   which we can mount another mount.
3036  * - Make sure that nothing can be mounted beneath the caller's current
3037  *   root or the rootfs of the namespace.
3038  * - Make sure that the caller can unmount the topmost mount ensuring
3039  *   that the caller could reveal the underlying mountpoint.
3040  * - Ensure that nothing has been mounted on top of @from before we
3041  *   grabbed @namespace_sem to avoid creating pointless shadow mounts.
3042  * - Prevent mounting beneath a mount if the propagation relationship
3043  *   between the source mount, parent mount, and top mount would lead to
3044  *   nonsensical mount trees.
3045  *
3046  * Context: This function expects namespace_lock() to be held.
3047  * Return: On success 0, and on error a negative error code is returned.
3048  */
can_move_mount_beneath(const struct path * from,const struct path * to,const struct mountpoint * mp)3049 static int can_move_mount_beneath(const struct path *from,
3050 				  const struct path *to,
3051 				  const struct mountpoint *mp)
3052 {
3053 	struct mount *mnt_from = real_mount(from->mnt),
3054 		     *mnt_to = real_mount(to->mnt),
3055 		     *parent_mnt_to = mnt_to->mnt_parent;
3056 
3057 	if (!mnt_has_parent(mnt_to))
3058 		return -EINVAL;
3059 
3060 	if (!path_mounted(to))
3061 		return -EINVAL;
3062 
3063 	if (IS_MNT_LOCKED(mnt_to))
3064 		return -EINVAL;
3065 
3066 	/* Avoid creating shadow mounts during mount propagation. */
3067 	if (path_overmounted(from))
3068 		return -EINVAL;
3069 
3070 	/*
3071 	 * Mounting beneath the rootfs only makes sense when the
3072 	 * semantics of pivot_root(".", ".") are used.
3073 	 */
3074 	if (&mnt_to->mnt == current->fs->root.mnt)
3075 		return -EINVAL;
3076 	if (parent_mnt_to == current->nsproxy->mnt_ns->root)
3077 		return -EINVAL;
3078 
3079 	for (struct mount *p = mnt_from; mnt_has_parent(p); p = p->mnt_parent)
3080 		if (p == mnt_to)
3081 			return -EINVAL;
3082 
3083 	/*
3084 	 * If the parent mount propagates to the child mount this would
3085 	 * mean mounting @mnt_from on @mnt_to->mnt_parent and then
3086 	 * propagating a copy @c of @mnt_from on top of @mnt_to. This
3087 	 * defeats the whole purpose of mounting beneath another mount.
3088 	 */
3089 	if (propagation_would_overmount(parent_mnt_to, mnt_to, mp))
3090 		return -EINVAL;
3091 
3092 	/*
3093 	 * If @mnt_to->mnt_parent propagates to @mnt_from this would
3094 	 * mean propagating a copy @c of @mnt_from on top of @mnt_from.
3095 	 * Afterwards @mnt_from would be mounted on top of
3096 	 * @mnt_to->mnt_parent and @mnt_to would be unmounted from
3097 	 * @mnt->mnt_parent and remounted on @mnt_from. But since @c is
3098 	 * already mounted on @mnt_from, @mnt_to would ultimately be
3099 	 * remounted on top of @c. Afterwards, @mnt_from would be
3100 	 * covered by a copy @c of @mnt_from and @c would be covered by
3101 	 * @mnt_from itself. This defeats the whole purpose of mounting
3102 	 * @mnt_from beneath @mnt_to.
3103 	 */
3104 	if (propagation_would_overmount(parent_mnt_to, mnt_from, mp))
3105 		return -EINVAL;
3106 
3107 	return 0;
3108 }
3109 
do_move_mount(struct path * old_path,struct path * new_path,bool beneath)3110 static int do_move_mount(struct path *old_path, struct path *new_path,
3111 			 bool beneath)
3112 {
3113 	struct mnt_namespace *ns;
3114 	struct mount *p;
3115 	struct mount *old;
3116 	struct mount *parent;
3117 	struct mountpoint *mp, *old_mp;
3118 	int err;
3119 	bool attached;
3120 	enum mnt_tree_flags_t flags = 0;
3121 
3122 	mp = do_lock_mount(new_path, beneath);
3123 	if (IS_ERR(mp))
3124 		return PTR_ERR(mp);
3125 
3126 	old = real_mount(old_path->mnt);
3127 	p = real_mount(new_path->mnt);
3128 	parent = old->mnt_parent;
3129 	attached = mnt_has_parent(old);
3130 	if (attached)
3131 		flags |= MNT_TREE_MOVE;
3132 	old_mp = old->mnt_mp;
3133 	ns = old->mnt_ns;
3134 
3135 	err = -EINVAL;
3136 	/* The mountpoint must be in our namespace. */
3137 	if (!check_mnt(p))
3138 		goto out;
3139 
3140 	/* The thing moved must be mounted... */
3141 	if (!is_mounted(&old->mnt))
3142 		goto out;
3143 
3144 	/* ... and either ours or the root of anon namespace */
3145 	if (!(attached ? check_mnt(old) : is_anon_ns(ns)))
3146 		goto out;
3147 
3148 	if (old->mnt.mnt_flags & MNT_LOCKED)
3149 		goto out;
3150 
3151 	if (!path_mounted(old_path))
3152 		goto out;
3153 
3154 	if (d_is_dir(new_path->dentry) !=
3155 	    d_is_dir(old_path->dentry))
3156 		goto out;
3157 	/*
3158 	 * Don't move a mount residing in a shared parent.
3159 	 */
3160 	if (attached && IS_MNT_SHARED(parent))
3161 		goto out;
3162 
3163 	if (beneath) {
3164 		err = can_move_mount_beneath(old_path, new_path, mp);
3165 		if (err)
3166 			goto out;
3167 
3168 		err = -EINVAL;
3169 		p = p->mnt_parent;
3170 		flags |= MNT_TREE_BENEATH;
3171 	}
3172 
3173 	/*
3174 	 * Don't move a mount tree containing unbindable mounts to a destination
3175 	 * mount which is shared.
3176 	 */
3177 	if (IS_MNT_SHARED(p) && tree_contains_unbindable(old))
3178 		goto out;
3179 	err = -ELOOP;
3180 	if (!check_for_nsfs_mounts(old))
3181 		goto out;
3182 	for (; mnt_has_parent(p); p = p->mnt_parent)
3183 		if (p == old)
3184 			goto out;
3185 
3186 	err = attach_recursive_mnt(old, real_mount(new_path->mnt), mp, flags);
3187 	if (err)
3188 		goto out;
3189 
3190 	/* if the mount is moved, it should no longer be expire
3191 	 * automatically */
3192 	list_del_init(&old->mnt_expire);
3193 	if (attached)
3194 		put_mountpoint(old_mp);
3195 out:
3196 	unlock_mount(mp);
3197 	if (!err) {
3198 		if (attached)
3199 			mntput_no_expire(parent);
3200 		else
3201 			free_mnt_ns(ns);
3202 	}
3203 	return err;
3204 }
3205 
do_move_mount_old(struct path * path,const char * old_name)3206 static int do_move_mount_old(struct path *path, const char *old_name)
3207 {
3208 	struct path old_path;
3209 	int err;
3210 
3211 	if (!old_name || !*old_name)
3212 		return -EINVAL;
3213 
3214 	err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
3215 	if (err)
3216 		return err;
3217 
3218 	err = do_move_mount(&old_path, path, false);
3219 	path_put(&old_path);
3220 	return err;
3221 }
3222 
3223 /*
3224  * add a mount into a namespace's mount tree
3225  */
do_add_mount(struct mount * newmnt,struct mountpoint * mp,const struct path * path,int mnt_flags)3226 static int do_add_mount(struct mount *newmnt, struct mountpoint *mp,
3227 			const struct path *path, int mnt_flags)
3228 {
3229 	struct mount *parent = real_mount(path->mnt);
3230 
3231 	mnt_flags &= ~MNT_INTERNAL_FLAGS;
3232 
3233 	if (unlikely(!check_mnt(parent))) {
3234 		/* that's acceptable only for automounts done in private ns */
3235 		if (!(mnt_flags & MNT_SHRINKABLE))
3236 			return -EINVAL;
3237 		/* ... and for those we'd better have mountpoint still alive */
3238 		if (!parent->mnt_ns)
3239 			return -EINVAL;
3240 	}
3241 
3242 	/* Refuse the same filesystem on the same mount point */
3243 	if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb && path_mounted(path))
3244 		return -EBUSY;
3245 
3246 	if (d_is_symlink(newmnt->mnt.mnt_root))
3247 		return -EINVAL;
3248 
3249 	newmnt->mnt.mnt_flags = mnt_flags;
3250 	return graft_tree(newmnt, parent, mp);
3251 }
3252 
3253 static bool mount_too_revealing(const struct super_block *sb, int *new_mnt_flags);
3254 
3255 /*
3256  * Create a new mount using a superblock configuration and request it
3257  * be added to the namespace tree.
3258  */
do_new_mount_fc(struct fs_context * fc,struct path * mountpoint,unsigned int mnt_flags)3259 static int do_new_mount_fc(struct fs_context *fc, struct path *mountpoint,
3260 			   unsigned int mnt_flags)
3261 {
3262 	struct vfsmount *mnt;
3263 	struct mountpoint *mp;
3264 	struct super_block *sb = fc->root->d_sb;
3265 	int error;
3266 
3267 	error = security_sb_kern_mount(sb);
3268 	if (!error && mount_too_revealing(sb, &mnt_flags))
3269 		error = -EPERM;
3270 
3271 	if (unlikely(error)) {
3272 		fc_drop_locked(fc);
3273 		return error;
3274 	}
3275 
3276 	up_write(&sb->s_umount);
3277 
3278 	mnt = vfs_create_mount(fc);
3279 	if (IS_ERR(mnt))
3280 		return PTR_ERR(mnt);
3281 
3282 	mnt_warn_timestamp_expiry(mountpoint, mnt);
3283 
3284 	mp = lock_mount(mountpoint);
3285 	if (IS_ERR(mp)) {
3286 		mntput(mnt);
3287 		return PTR_ERR(mp);
3288 	}
3289 	error = do_add_mount(real_mount(mnt), mp, mountpoint, mnt_flags);
3290 	unlock_mount(mp);
3291 	if (error < 0)
3292 		mntput(mnt);
3293 	else
3294 		trace_android_vh_do_new_mount_fc(mountpoint, mnt);
3295 	return error;
3296 }
3297 
3298 /*
3299  * create a new mount for userspace and request it to be added into the
3300  * namespace's tree
3301  */
do_new_mount(struct path * path,const char * fstype,int sb_flags,int mnt_flags,const char * name,void * data)3302 static int do_new_mount(struct path *path, const char *fstype, int sb_flags,
3303 			int mnt_flags, const char *name, void *data)
3304 {
3305 	struct file_system_type *type;
3306 	struct fs_context *fc;
3307 	const char *subtype = NULL;
3308 	int err = 0;
3309 
3310 	if (!fstype)
3311 		return -EINVAL;
3312 
3313 	type = get_fs_type(fstype);
3314 	if (!type)
3315 		return -ENODEV;
3316 
3317 	if (type->fs_flags & FS_HAS_SUBTYPE) {
3318 		subtype = strchr(fstype, '.');
3319 		if (subtype) {
3320 			subtype++;
3321 			if (!*subtype) {
3322 				put_filesystem(type);
3323 				return -EINVAL;
3324 			}
3325 		}
3326 	}
3327 
3328 	fc = fs_context_for_mount(type, sb_flags);
3329 	put_filesystem(type);
3330 	if (IS_ERR(fc))
3331 		return PTR_ERR(fc);
3332 
3333 	/*
3334 	 * Indicate to the filesystem that the mount request is coming
3335 	 * from the legacy mount system call.
3336 	 */
3337 	fc->oldapi = true;
3338 
3339 	if (subtype)
3340 		err = vfs_parse_fs_string(fc, "subtype",
3341 					  subtype, strlen(subtype));
3342 	if (!err && name)
3343 		err = vfs_parse_fs_string(fc, "source", name, strlen(name));
3344 	if (!err)
3345 		err = parse_monolithic_mount_data(fc, data);
3346 	if (!err && !mount_capable(fc))
3347 		err = -EPERM;
3348 	if (!err)
3349 		err = vfs_get_tree(fc);
3350 	if (!err)
3351 		err = do_new_mount_fc(fc, path, mnt_flags);
3352 
3353 	put_fs_context(fc);
3354 	return err;
3355 }
3356 
finish_automount(struct vfsmount * m,const struct path * path)3357 int finish_automount(struct vfsmount *m, const struct path *path)
3358 {
3359 	struct dentry *dentry = path->dentry;
3360 	struct mountpoint *mp;
3361 	struct mount *mnt;
3362 	int err;
3363 
3364 	if (!m)
3365 		return 0;
3366 	if (IS_ERR(m))
3367 		return PTR_ERR(m);
3368 
3369 	mnt = real_mount(m);
3370 	/* The new mount record should have at least 2 refs to prevent it being
3371 	 * expired before we get a chance to add it
3372 	 */
3373 	BUG_ON(mnt_get_count(mnt) < 2);
3374 
3375 	if (m->mnt_sb == path->mnt->mnt_sb &&
3376 	    m->mnt_root == dentry) {
3377 		err = -ELOOP;
3378 		goto discard;
3379 	}
3380 
3381 	/*
3382 	 * we don't want to use lock_mount() - in this case finding something
3383 	 * that overmounts our mountpoint to be means "quitely drop what we've
3384 	 * got", not "try to mount it on top".
3385 	 */
3386 	inode_lock(dentry->d_inode);
3387 	namespace_lock();
3388 	if (unlikely(cant_mount(dentry))) {
3389 		err = -ENOENT;
3390 		goto discard_locked;
3391 	}
3392 	if (path_overmounted(path)) {
3393 		err = 0;
3394 		goto discard_locked;
3395 	}
3396 	mp = get_mountpoint(dentry);
3397 	if (IS_ERR(mp)) {
3398 		err = PTR_ERR(mp);
3399 		goto discard_locked;
3400 	}
3401 
3402 	err = do_add_mount(mnt, mp, path, path->mnt->mnt_flags | MNT_SHRINKABLE);
3403 	unlock_mount(mp);
3404 	if (unlikely(err))
3405 		goto discard;
3406 	mntput(m);
3407 	return 0;
3408 
3409 discard_locked:
3410 	namespace_unlock();
3411 	inode_unlock(dentry->d_inode);
3412 discard:
3413 	/* remove m from any expiration list it may be on */
3414 	if (!list_empty(&mnt->mnt_expire)) {
3415 		namespace_lock();
3416 		list_del_init(&mnt->mnt_expire);
3417 		namespace_unlock();
3418 	}
3419 	mntput(m);
3420 	mntput(m);
3421 	return err;
3422 }
3423 
3424 /**
3425  * mnt_set_expiry - Put a mount on an expiration list
3426  * @mnt: The mount to list.
3427  * @expiry_list: The list to add the mount to.
3428  */
mnt_set_expiry(struct vfsmount * mnt,struct list_head * expiry_list)3429 void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
3430 {
3431 	namespace_lock();
3432 
3433 	list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list);
3434 
3435 	namespace_unlock();
3436 }
3437 EXPORT_SYMBOL(mnt_set_expiry);
3438 
3439 /*
3440  * process a list of expirable mountpoints with the intent of discarding any
3441  * mountpoints that aren't in use and haven't been touched since last we came
3442  * here
3443  */
mark_mounts_for_expiry(struct list_head * mounts)3444 void mark_mounts_for_expiry(struct list_head *mounts)
3445 {
3446 	struct mount *mnt, *next;
3447 	LIST_HEAD(graveyard);
3448 
3449 	if (list_empty(mounts))
3450 		return;
3451 
3452 	namespace_lock();
3453 	lock_mount_hash();
3454 
3455 	/* extract from the expiration list every vfsmount that matches the
3456 	 * following criteria:
3457 	 * - only referenced by its parent vfsmount
3458 	 * - still marked for expiry (marked on the last call here; marks are
3459 	 *   cleared by mntput())
3460 	 */
3461 	list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
3462 		if (!xchg(&mnt->mnt_expiry_mark, 1) ||
3463 			propagate_mount_busy(mnt, 1))
3464 			continue;
3465 		list_move(&mnt->mnt_expire, &graveyard);
3466 	}
3467 	while (!list_empty(&graveyard)) {
3468 		mnt = list_first_entry(&graveyard, struct mount, mnt_expire);
3469 		touch_mnt_namespace(mnt->mnt_ns);
3470 		umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
3471 	}
3472 	unlock_mount_hash();
3473 	namespace_unlock();
3474 }
3475 
3476 EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
3477 
3478 /*
3479  * Ripoff of 'select_parent()'
3480  *
3481  * search the list of submounts for a given mountpoint, and move any
3482  * shrinkable submounts to the 'graveyard' list.
3483  */
select_submounts(struct mount * parent,struct list_head * graveyard)3484 static int select_submounts(struct mount *parent, struct list_head *graveyard)
3485 {
3486 	struct mount *this_parent = parent;
3487 	struct list_head *next;
3488 	int found = 0;
3489 
3490 repeat:
3491 	next = this_parent->mnt_mounts.next;
3492 resume:
3493 	while (next != &this_parent->mnt_mounts) {
3494 		struct list_head *tmp = next;
3495 		struct mount *mnt = list_entry(tmp, struct mount, mnt_child);
3496 
3497 		next = tmp->next;
3498 		if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE))
3499 			continue;
3500 		/*
3501 		 * Descend a level if the d_mounts list is non-empty.
3502 		 */
3503 		if (!list_empty(&mnt->mnt_mounts)) {
3504 			this_parent = mnt;
3505 			goto repeat;
3506 		}
3507 
3508 		if (!propagate_mount_busy(mnt, 1)) {
3509 			list_move_tail(&mnt->mnt_expire, graveyard);
3510 			found++;
3511 		}
3512 	}
3513 	/*
3514 	 * All done at this level ... ascend and resume the search
3515 	 */
3516 	if (this_parent != parent) {
3517 		next = this_parent->mnt_child.next;
3518 		this_parent = this_parent->mnt_parent;
3519 		goto resume;
3520 	}
3521 	return found;
3522 }
3523 
3524 /*
3525  * process a list of expirable mountpoints with the intent of discarding any
3526  * submounts of a specific parent mountpoint
3527  *
3528  * mount_lock must be held for write
3529  */
shrink_submounts(struct mount * mnt)3530 static void shrink_submounts(struct mount *mnt)
3531 {
3532 	LIST_HEAD(graveyard);
3533 	struct mount *m;
3534 
3535 	/* extract submounts of 'mountpoint' from the expiration list */
3536 	while (select_submounts(mnt, &graveyard)) {
3537 		while (!list_empty(&graveyard)) {
3538 			m = list_first_entry(&graveyard, struct mount,
3539 						mnt_expire);
3540 			touch_mnt_namespace(m->mnt_ns);
3541 			umount_tree(m, UMOUNT_PROPAGATE|UMOUNT_SYNC);
3542 		}
3543 	}
3544 }
3545 
copy_mount_options(const void __user * data)3546 static void *copy_mount_options(const void __user * data)
3547 {
3548 	char *copy;
3549 	unsigned left, offset;
3550 
3551 	if (!data)
3552 		return NULL;
3553 
3554 	copy = kmalloc(PAGE_SIZE, GFP_KERNEL);
3555 	if (!copy)
3556 		return ERR_PTR(-ENOMEM);
3557 
3558 	left = copy_from_user(copy, data, PAGE_SIZE);
3559 
3560 	/*
3561 	 * Not all architectures have an exact copy_from_user(). Resort to
3562 	 * byte at a time.
3563 	 */
3564 	offset = PAGE_SIZE - left;
3565 	while (left) {
3566 		char c;
3567 		if (get_user(c, (const char __user *)data + offset))
3568 			break;
3569 		copy[offset] = c;
3570 		left--;
3571 		offset++;
3572 	}
3573 
3574 	if (left == PAGE_SIZE) {
3575 		kfree(copy);
3576 		return ERR_PTR(-EFAULT);
3577 	}
3578 
3579 	return copy;
3580 }
3581 
copy_mount_string(const void __user * data)3582 static char *copy_mount_string(const void __user *data)
3583 {
3584 	return data ? strndup_user(data, PATH_MAX) : NULL;
3585 }
3586 
3587 /*
3588  * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
3589  * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
3590  *
3591  * data is a (void *) that can point to any structure up to
3592  * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
3593  * information (or be NULL).
3594  *
3595  * Pre-0.97 versions of mount() didn't have a flags word.
3596  * When the flags word was introduced its top half was required
3597  * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
3598  * Therefore, if this magic number is present, it carries no information
3599  * and must be discarded.
3600  */
path_mount(const char * dev_name,struct path * path,const char * type_page,unsigned long flags,void * data_page)3601 int path_mount(const char *dev_name, struct path *path,
3602 		const char *type_page, unsigned long flags, void *data_page)
3603 {
3604 	unsigned int mnt_flags = 0, sb_flags;
3605 	int ret;
3606 
3607 	/* Discard magic */
3608 	if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
3609 		flags &= ~MS_MGC_MSK;
3610 
3611 	/* Basic sanity checks */
3612 	if (data_page)
3613 		((char *)data_page)[PAGE_SIZE - 1] = 0;
3614 
3615 	if (flags & MS_NOUSER)
3616 		return -EINVAL;
3617 
3618 	ret = security_sb_mount(dev_name, path, type_page, flags, data_page);
3619 	if (ret)
3620 		return ret;
3621 	if (!may_mount())
3622 		return -EPERM;
3623 	if (flags & SB_MANDLOCK)
3624 		warn_mandlock();
3625 
3626 	/* Default to relatime unless overriden */
3627 	if (!(flags & MS_NOATIME))
3628 		mnt_flags |= MNT_RELATIME;
3629 
3630 	/* Separate the per-mountpoint flags */
3631 	if (flags & MS_NOSUID)
3632 		mnt_flags |= MNT_NOSUID;
3633 	if (flags & MS_NODEV)
3634 		mnt_flags |= MNT_NODEV;
3635 	if (flags & MS_NOEXEC)
3636 		mnt_flags |= MNT_NOEXEC;
3637 	if (flags & MS_NOATIME)
3638 		mnt_flags |= MNT_NOATIME;
3639 	if (flags & MS_NODIRATIME)
3640 		mnt_flags |= MNT_NODIRATIME;
3641 	if (flags & MS_STRICTATIME)
3642 		mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
3643 	if (flags & MS_RDONLY)
3644 		mnt_flags |= MNT_READONLY;
3645 	if (flags & MS_NOSYMFOLLOW)
3646 		mnt_flags |= MNT_NOSYMFOLLOW;
3647 
3648 	/* The default atime for remount is preservation */
3649 	if ((flags & MS_REMOUNT) &&
3650 	    ((flags & (MS_NOATIME | MS_NODIRATIME | MS_RELATIME |
3651 		       MS_STRICTATIME)) == 0)) {
3652 		mnt_flags &= ~MNT_ATIME_MASK;
3653 		mnt_flags |= path->mnt->mnt_flags & MNT_ATIME_MASK;
3654 	}
3655 
3656 	sb_flags = flags & (SB_RDONLY |
3657 			    SB_SYNCHRONOUS |
3658 			    SB_MANDLOCK |
3659 			    SB_DIRSYNC |
3660 			    SB_SILENT |
3661 			    SB_POSIXACL |
3662 			    SB_LAZYTIME |
3663 			    SB_I_VERSION);
3664 
3665 	if ((flags & (MS_REMOUNT | MS_BIND)) == (MS_REMOUNT | MS_BIND))
3666 		return do_reconfigure_mnt(path, mnt_flags);
3667 	if (flags & MS_REMOUNT)
3668 		return do_remount(path, flags, sb_flags, mnt_flags, data_page);
3669 	if (flags & MS_BIND)
3670 		return do_loopback(path, dev_name, flags & MS_REC);
3671 	if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
3672 		return do_change_type(path, flags);
3673 	if (flags & MS_MOVE)
3674 		return do_move_mount_old(path, dev_name);
3675 
3676 	return do_new_mount(path, type_page, sb_flags, mnt_flags, dev_name,
3677 			    data_page);
3678 }
3679 
do_mount(const char * dev_name,const char __user * dir_name,const char * type_page,unsigned long flags,void * data_page)3680 long do_mount(const char *dev_name, const char __user *dir_name,
3681 		const char *type_page, unsigned long flags, void *data_page)
3682 {
3683 	struct path path;
3684 	int ret;
3685 
3686 	ret = user_path_at(AT_FDCWD, dir_name, LOOKUP_FOLLOW, &path);
3687 	if (ret)
3688 		return ret;
3689 	ret = path_mount(dev_name, &path, type_page, flags, data_page);
3690 	path_put(&path);
3691 	return ret;
3692 }
3693 
inc_mnt_namespaces(struct user_namespace * ns)3694 static struct ucounts *inc_mnt_namespaces(struct user_namespace *ns)
3695 {
3696 	return inc_ucount(ns, current_euid(), UCOUNT_MNT_NAMESPACES);
3697 }
3698 
dec_mnt_namespaces(struct ucounts * ucounts)3699 static void dec_mnt_namespaces(struct ucounts *ucounts)
3700 {
3701 	dec_ucount(ucounts, UCOUNT_MNT_NAMESPACES);
3702 }
3703 
free_mnt_ns(struct mnt_namespace * ns)3704 static void free_mnt_ns(struct mnt_namespace *ns)
3705 {
3706 	if (!is_anon_ns(ns))
3707 		ns_free_inum(&ns->ns);
3708 	dec_mnt_namespaces(ns->ucounts);
3709 	put_user_ns(ns->user_ns);
3710 	kfree(ns);
3711 }
3712 
3713 /*
3714  * Assign a sequence number so we can detect when we attempt to bind
3715  * mount a reference to an older mount namespace into the current
3716  * mount namespace, preventing reference counting loops.  A 64bit
3717  * number incrementing at 10Ghz will take 12,427 years to wrap which
3718  * is effectively never, so we can ignore the possibility.
3719  */
3720 static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1);
3721 
alloc_mnt_ns(struct user_namespace * user_ns,bool anon)3722 static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool anon)
3723 {
3724 	struct mnt_namespace *new_ns;
3725 	struct ucounts *ucounts;
3726 	int ret;
3727 
3728 	ucounts = inc_mnt_namespaces(user_ns);
3729 	if (!ucounts)
3730 		return ERR_PTR(-ENOSPC);
3731 
3732 	new_ns = kzalloc(sizeof(struct mnt_namespace), GFP_KERNEL_ACCOUNT);
3733 	if (!new_ns) {
3734 		dec_mnt_namespaces(ucounts);
3735 		return ERR_PTR(-ENOMEM);
3736 	}
3737 	if (!anon) {
3738 		ret = ns_alloc_inum(&new_ns->ns);
3739 		if (ret) {
3740 			kfree(new_ns);
3741 			dec_mnt_namespaces(ucounts);
3742 			return ERR_PTR(ret);
3743 		}
3744 	}
3745 	new_ns->ns.ops = &mntns_operations;
3746 	if (!anon)
3747 		new_ns->seq = atomic64_add_return(1, &mnt_ns_seq);
3748 	refcount_set(&new_ns->ns.count, 1);
3749 	INIT_LIST_HEAD(&new_ns->list);
3750 	init_waitqueue_head(&new_ns->poll);
3751 	spin_lock_init(&new_ns->ns_lock);
3752 	new_ns->user_ns = get_user_ns(user_ns);
3753 	new_ns->ucounts = ucounts;
3754 	return new_ns;
3755 }
3756 
3757 __latent_entropy
copy_mnt_ns(unsigned long flags,struct mnt_namespace * ns,struct user_namespace * user_ns,struct fs_struct * new_fs)3758 struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
3759 		struct user_namespace *user_ns, struct fs_struct *new_fs)
3760 {
3761 	struct mnt_namespace *new_ns;
3762 	struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
3763 	struct mount *p, *q;
3764 	struct mount *old;
3765 	struct mount *new;
3766 	int copy_flags;
3767 
3768 	BUG_ON(!ns);
3769 
3770 	if (likely(!(flags & CLONE_NEWNS))) {
3771 		get_mnt_ns(ns);
3772 		return ns;
3773 	}
3774 
3775 	old = ns->root;
3776 
3777 	new_ns = alloc_mnt_ns(user_ns, false);
3778 	if (IS_ERR(new_ns))
3779 		return new_ns;
3780 
3781 	namespace_lock();
3782 	/* First pass: copy the tree topology */
3783 	copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE;
3784 	if (user_ns != ns->user_ns)
3785 		copy_flags |= CL_SHARED_TO_SLAVE;
3786 	new = copy_tree(old, old->mnt.mnt_root, copy_flags);
3787 	if (IS_ERR(new)) {
3788 		namespace_unlock();
3789 		free_mnt_ns(new_ns);
3790 		return ERR_CAST(new);
3791 	}
3792 	if (user_ns != ns->user_ns) {
3793 		lock_mount_hash();
3794 		lock_mnt_tree(new);
3795 		unlock_mount_hash();
3796 	}
3797 	new_ns->root = new;
3798 	list_add_tail(&new_ns->list, &new->mnt_list);
3799 
3800 	/*
3801 	 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
3802 	 * as belonging to new namespace.  We have already acquired a private
3803 	 * fs_struct, so tsk->fs->lock is not needed.
3804 	 */
3805 	p = old;
3806 	q = new;
3807 	while (p) {
3808 		q->mnt_ns = new_ns;
3809 		new_ns->mounts++;
3810 		if (new_fs) {
3811 			if (&p->mnt == new_fs->root.mnt) {
3812 				new_fs->root.mnt = mntget(&q->mnt);
3813 				rootmnt = &p->mnt;
3814 			}
3815 			if (&p->mnt == new_fs->pwd.mnt) {
3816 				new_fs->pwd.mnt = mntget(&q->mnt);
3817 				pwdmnt = &p->mnt;
3818 			}
3819 		}
3820 		p = next_mnt(p, old);
3821 		q = next_mnt(q, new);
3822 		if (!q)
3823 			break;
3824 		// an mntns binding we'd skipped?
3825 		while (p->mnt.mnt_root != q->mnt.mnt_root)
3826 			p = next_mnt(skip_mnt_tree(p), old);
3827 	}
3828 	namespace_unlock();
3829 
3830 	if (rootmnt)
3831 		mntput(rootmnt);
3832 	if (pwdmnt)
3833 		mntput(pwdmnt);
3834 
3835 	return new_ns;
3836 }
3837 
mount_subtree(struct vfsmount * m,const char * name)3838 struct dentry *mount_subtree(struct vfsmount *m, const char *name)
3839 {
3840 	struct mount *mnt = real_mount(m);
3841 	struct mnt_namespace *ns;
3842 	struct super_block *s;
3843 	struct path path;
3844 	int err;
3845 
3846 	ns = alloc_mnt_ns(&init_user_ns, true);
3847 	if (IS_ERR(ns)) {
3848 		mntput(m);
3849 		return ERR_CAST(ns);
3850 	}
3851 	mnt->mnt_ns = ns;
3852 	ns->root = mnt;
3853 	ns->mounts++;
3854 	list_add(&mnt->mnt_list, &ns->list);
3855 
3856 	err = vfs_path_lookup(m->mnt_root, m,
3857 			name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
3858 
3859 	put_mnt_ns(ns);
3860 
3861 	if (err)
3862 		return ERR_PTR(err);
3863 
3864 	/* trade a vfsmount reference for active sb one */
3865 	s = path.mnt->mnt_sb;
3866 	atomic_inc(&s->s_active);
3867 	mntput(path.mnt);
3868 	/* lock the sucker */
3869 	down_write(&s->s_umount);
3870 	/* ... and return the root of (sub)tree on it */
3871 	return path.dentry;
3872 }
3873 EXPORT_SYMBOL(mount_subtree);
3874 
SYSCALL_DEFINE5(mount,char __user *,dev_name,char __user *,dir_name,char __user *,type,unsigned long,flags,void __user *,data)3875 SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
3876 		char __user *, type, unsigned long, flags, void __user *, data)
3877 {
3878 	int ret;
3879 	char *kernel_type;
3880 	char *kernel_dev;
3881 	void *options;
3882 
3883 	kernel_type = copy_mount_string(type);
3884 	ret = PTR_ERR(kernel_type);
3885 	if (IS_ERR(kernel_type))
3886 		goto out_type;
3887 
3888 	kernel_dev = copy_mount_string(dev_name);
3889 	ret = PTR_ERR(kernel_dev);
3890 	if (IS_ERR(kernel_dev))
3891 		goto out_dev;
3892 
3893 	options = copy_mount_options(data);
3894 	ret = PTR_ERR(options);
3895 	if (IS_ERR(options))
3896 		goto out_data;
3897 
3898 	ret = do_mount(kernel_dev, dir_name, kernel_type, flags, options);
3899 
3900 	kfree(options);
3901 out_data:
3902 	kfree(kernel_dev);
3903 out_dev:
3904 	kfree(kernel_type);
3905 out_type:
3906 	return ret;
3907 }
3908 
3909 #define FSMOUNT_VALID_FLAGS                                                    \
3910 	(MOUNT_ATTR_RDONLY | MOUNT_ATTR_NOSUID | MOUNT_ATTR_NODEV |            \
3911 	 MOUNT_ATTR_NOEXEC | MOUNT_ATTR__ATIME | MOUNT_ATTR_NODIRATIME |       \
3912 	 MOUNT_ATTR_NOSYMFOLLOW)
3913 
3914 #define MOUNT_SETATTR_VALID_FLAGS (FSMOUNT_VALID_FLAGS | MOUNT_ATTR_IDMAP)
3915 
3916 #define MOUNT_SETATTR_PROPAGATION_FLAGS \
3917 	(MS_UNBINDABLE | MS_PRIVATE | MS_SLAVE | MS_SHARED)
3918 
attr_flags_to_mnt_flags(u64 attr_flags)3919 static unsigned int attr_flags_to_mnt_flags(u64 attr_flags)
3920 {
3921 	unsigned int mnt_flags = 0;
3922 
3923 	if (attr_flags & MOUNT_ATTR_RDONLY)
3924 		mnt_flags |= MNT_READONLY;
3925 	if (attr_flags & MOUNT_ATTR_NOSUID)
3926 		mnt_flags |= MNT_NOSUID;
3927 	if (attr_flags & MOUNT_ATTR_NODEV)
3928 		mnt_flags |= MNT_NODEV;
3929 	if (attr_flags & MOUNT_ATTR_NOEXEC)
3930 		mnt_flags |= MNT_NOEXEC;
3931 	if (attr_flags & MOUNT_ATTR_NODIRATIME)
3932 		mnt_flags |= MNT_NODIRATIME;
3933 	if (attr_flags & MOUNT_ATTR_NOSYMFOLLOW)
3934 		mnt_flags |= MNT_NOSYMFOLLOW;
3935 
3936 	return mnt_flags;
3937 }
3938 
3939 /*
3940  * Create a kernel mount representation for a new, prepared superblock
3941  * (specified by fs_fd) and attach to an open_tree-like file descriptor.
3942  */
SYSCALL_DEFINE3(fsmount,int,fs_fd,unsigned int,flags,unsigned int,attr_flags)3943 SYSCALL_DEFINE3(fsmount, int, fs_fd, unsigned int, flags,
3944 		unsigned int, attr_flags)
3945 {
3946 	struct mnt_namespace *ns;
3947 	struct fs_context *fc;
3948 	struct file *file;
3949 	struct path newmount;
3950 	struct mount *mnt;
3951 	struct fd f;
3952 	unsigned int mnt_flags = 0;
3953 	long ret;
3954 
3955 	if (!may_mount())
3956 		return -EPERM;
3957 
3958 	if ((flags & ~(FSMOUNT_CLOEXEC)) != 0)
3959 		return -EINVAL;
3960 
3961 	if (attr_flags & ~FSMOUNT_VALID_FLAGS)
3962 		return -EINVAL;
3963 
3964 	mnt_flags = attr_flags_to_mnt_flags(attr_flags);
3965 
3966 	switch (attr_flags & MOUNT_ATTR__ATIME) {
3967 	case MOUNT_ATTR_STRICTATIME:
3968 		break;
3969 	case MOUNT_ATTR_NOATIME:
3970 		mnt_flags |= MNT_NOATIME;
3971 		break;
3972 	case MOUNT_ATTR_RELATIME:
3973 		mnt_flags |= MNT_RELATIME;
3974 		break;
3975 	default:
3976 		return -EINVAL;
3977 	}
3978 
3979 	f = fdget(fs_fd);
3980 	if (!f.file)
3981 		return -EBADF;
3982 
3983 	ret = -EINVAL;
3984 	if (f.file->f_op != &fscontext_fops)
3985 		goto err_fsfd;
3986 
3987 	fc = f.file->private_data;
3988 
3989 	ret = mutex_lock_interruptible(&fc->uapi_mutex);
3990 	if (ret < 0)
3991 		goto err_fsfd;
3992 
3993 	/* There must be a valid superblock or we can't mount it */
3994 	ret = -EINVAL;
3995 	if (!fc->root)
3996 		goto err_unlock;
3997 
3998 	ret = -EPERM;
3999 	if (mount_too_revealing(fc->root->d_sb, &mnt_flags)) {
4000 		pr_warn("VFS: Mount too revealing\n");
4001 		goto err_unlock;
4002 	}
4003 
4004 	ret = -EBUSY;
4005 	if (fc->phase != FS_CONTEXT_AWAITING_MOUNT)
4006 		goto err_unlock;
4007 
4008 	if (fc->sb_flags & SB_MANDLOCK)
4009 		warn_mandlock();
4010 
4011 	newmount.mnt = vfs_create_mount(fc);
4012 	if (IS_ERR(newmount.mnt)) {
4013 		ret = PTR_ERR(newmount.mnt);
4014 		goto err_unlock;
4015 	}
4016 	newmount.dentry = dget(fc->root);
4017 	newmount.mnt->mnt_flags = mnt_flags;
4018 
4019 	/* We've done the mount bit - now move the file context into more or
4020 	 * less the same state as if we'd done an fspick().  We don't want to
4021 	 * do any memory allocation or anything like that at this point as we
4022 	 * don't want to have to handle any errors incurred.
4023 	 */
4024 	vfs_clean_context(fc);
4025 
4026 	ns = alloc_mnt_ns(current->nsproxy->mnt_ns->user_ns, true);
4027 	if (IS_ERR(ns)) {
4028 		ret = PTR_ERR(ns);
4029 		goto err_path;
4030 	}
4031 	mnt = real_mount(newmount.mnt);
4032 	mnt->mnt_ns = ns;
4033 	ns->root = mnt;
4034 	ns->mounts = 1;
4035 	list_add(&mnt->mnt_list, &ns->list);
4036 	mntget(newmount.mnt);
4037 
4038 	/* Attach to an apparent O_PATH fd with a note that we need to unmount
4039 	 * it, not just simply put it.
4040 	 */
4041 	file = dentry_open(&newmount, O_PATH, fc->cred);
4042 	if (IS_ERR(file)) {
4043 		dissolve_on_fput(newmount.mnt);
4044 		ret = PTR_ERR(file);
4045 		goto err_path;
4046 	}
4047 	file->f_mode |= FMODE_NEED_UNMOUNT;
4048 
4049 	ret = get_unused_fd_flags((flags & FSMOUNT_CLOEXEC) ? O_CLOEXEC : 0);
4050 	if (ret >= 0)
4051 		fd_install(ret, file);
4052 	else
4053 		fput(file);
4054 
4055 err_path:
4056 	path_put(&newmount);
4057 err_unlock:
4058 	mutex_unlock(&fc->uapi_mutex);
4059 err_fsfd:
4060 	fdput(f);
4061 	return ret;
4062 }
4063 
4064 /*
4065  * Move a mount from one place to another.  In combination with
4066  * fsopen()/fsmount() this is used to install a new mount and in combination
4067  * with open_tree(OPEN_TREE_CLONE [| AT_RECURSIVE]) it can be used to copy
4068  * a mount subtree.
4069  *
4070  * Note the flags value is a combination of MOVE_MOUNT_* flags.
4071  */
SYSCALL_DEFINE5(move_mount,int,from_dfd,const char __user *,from_pathname,int,to_dfd,const char __user *,to_pathname,unsigned int,flags)4072 SYSCALL_DEFINE5(move_mount,
4073 		int, from_dfd, const char __user *, from_pathname,
4074 		int, to_dfd, const char __user *, to_pathname,
4075 		unsigned int, flags)
4076 {
4077 	struct path from_path, to_path;
4078 	unsigned int lflags;
4079 	int ret = 0;
4080 
4081 	if (!may_mount())
4082 		return -EPERM;
4083 
4084 	if (flags & ~MOVE_MOUNT__MASK)
4085 		return -EINVAL;
4086 
4087 	if ((flags & (MOVE_MOUNT_BENEATH | MOVE_MOUNT_SET_GROUP)) ==
4088 	    (MOVE_MOUNT_BENEATH | MOVE_MOUNT_SET_GROUP))
4089 		return -EINVAL;
4090 
4091 	/* If someone gives a pathname, they aren't permitted to move
4092 	 * from an fd that requires unmount as we can't get at the flag
4093 	 * to clear it afterwards.
4094 	 */
4095 	lflags = 0;
4096 	if (flags & MOVE_MOUNT_F_SYMLINKS)	lflags |= LOOKUP_FOLLOW;
4097 	if (flags & MOVE_MOUNT_F_AUTOMOUNTS)	lflags |= LOOKUP_AUTOMOUNT;
4098 	if (flags & MOVE_MOUNT_F_EMPTY_PATH)	lflags |= LOOKUP_EMPTY;
4099 
4100 	ret = user_path_at(from_dfd, from_pathname, lflags, &from_path);
4101 	if (ret < 0)
4102 		return ret;
4103 
4104 	lflags = 0;
4105 	if (flags & MOVE_MOUNT_T_SYMLINKS)	lflags |= LOOKUP_FOLLOW;
4106 	if (flags & MOVE_MOUNT_T_AUTOMOUNTS)	lflags |= LOOKUP_AUTOMOUNT;
4107 	if (flags & MOVE_MOUNT_T_EMPTY_PATH)	lflags |= LOOKUP_EMPTY;
4108 
4109 	ret = user_path_at(to_dfd, to_pathname, lflags, &to_path);
4110 	if (ret < 0)
4111 		goto out_from;
4112 
4113 	ret = security_move_mount(&from_path, &to_path);
4114 	if (ret < 0)
4115 		goto out_to;
4116 
4117 	if (flags & MOVE_MOUNT_SET_GROUP)
4118 		ret = do_set_group(&from_path, &to_path);
4119 	else
4120 		ret = do_move_mount(&from_path, &to_path,
4121 				    (flags & MOVE_MOUNT_BENEATH));
4122 
4123 out_to:
4124 	path_put(&to_path);
4125 out_from:
4126 	path_put(&from_path);
4127 	return ret;
4128 }
4129 
4130 /*
4131  * Return true if path is reachable from root
4132  *
4133  * namespace_sem or mount_lock is held
4134  */
is_path_reachable(struct mount * mnt,struct dentry * dentry,const struct path * root)4135 bool is_path_reachable(struct mount *mnt, struct dentry *dentry,
4136 			 const struct path *root)
4137 {
4138 	while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) {
4139 		dentry = mnt->mnt_mountpoint;
4140 		mnt = mnt->mnt_parent;
4141 	}
4142 	return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry);
4143 }
4144 
path_is_under(const struct path * path1,const struct path * path2)4145 bool path_is_under(const struct path *path1, const struct path *path2)
4146 {
4147 	bool res;
4148 	read_seqlock_excl(&mount_lock);
4149 	res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2);
4150 	read_sequnlock_excl(&mount_lock);
4151 	return res;
4152 }
4153 EXPORT_SYMBOL(path_is_under);
4154 
4155 /*
4156  * pivot_root Semantics:
4157  * Moves the root file system of the current process to the directory put_old,
4158  * makes new_root as the new root file system of the current process, and sets
4159  * root/cwd of all processes which had them on the current root to new_root.
4160  *
4161  * Restrictions:
4162  * The new_root and put_old must be directories, and  must not be on the
4163  * same file  system as the current process root. The put_old  must  be
4164  * underneath new_root,  i.e. adding a non-zero number of /.. to the string
4165  * pointed to by put_old must yield the same directory as new_root. No other
4166  * file system may be mounted on put_old. After all, new_root is a mountpoint.
4167  *
4168  * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
4169  * See Documentation/filesystems/ramfs-rootfs-initramfs.rst for alternatives
4170  * in this situation.
4171  *
4172  * Notes:
4173  *  - we don't move root/cwd if they are not at the root (reason: if something
4174  *    cared enough to change them, it's probably wrong to force them elsewhere)
4175  *  - it's okay to pick a root that isn't the root of a file system, e.g.
4176  *    /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
4177  *    though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
4178  *    first.
4179  */
SYSCALL_DEFINE2(pivot_root,const char __user *,new_root,const char __user *,put_old)4180 SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
4181 		const char __user *, put_old)
4182 {
4183 	struct path new, old, root;
4184 	struct mount *new_mnt, *root_mnt, *old_mnt, *root_parent, *ex_parent;
4185 	struct mountpoint *old_mp, *root_mp;
4186 	int error;
4187 
4188 	if (!may_mount())
4189 		return -EPERM;
4190 
4191 	error = user_path_at(AT_FDCWD, new_root,
4192 			     LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &new);
4193 	if (error)
4194 		goto out0;
4195 
4196 	error = user_path_at(AT_FDCWD, put_old,
4197 			     LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &old);
4198 	if (error)
4199 		goto out1;
4200 
4201 	error = security_sb_pivotroot(&old, &new);
4202 	if (error)
4203 		goto out2;
4204 
4205 	get_fs_root(current->fs, &root);
4206 	old_mp = lock_mount(&old);
4207 	error = PTR_ERR(old_mp);
4208 	if (IS_ERR(old_mp))
4209 		goto out3;
4210 
4211 	error = -EINVAL;
4212 	new_mnt = real_mount(new.mnt);
4213 	root_mnt = real_mount(root.mnt);
4214 	old_mnt = real_mount(old.mnt);
4215 	ex_parent = new_mnt->mnt_parent;
4216 	root_parent = root_mnt->mnt_parent;
4217 	if (IS_MNT_SHARED(old_mnt) ||
4218 		IS_MNT_SHARED(ex_parent) ||
4219 		IS_MNT_SHARED(root_parent))
4220 		goto out4;
4221 	if (!check_mnt(root_mnt) || !check_mnt(new_mnt))
4222 		goto out4;
4223 	if (new_mnt->mnt.mnt_flags & MNT_LOCKED)
4224 		goto out4;
4225 	error = -ENOENT;
4226 	if (d_unlinked(new.dentry))
4227 		goto out4;
4228 	error = -EBUSY;
4229 	if (new_mnt == root_mnt || old_mnt == root_mnt)
4230 		goto out4; /* loop, on the same file system  */
4231 	error = -EINVAL;
4232 	if (!path_mounted(&root))
4233 		goto out4; /* not a mountpoint */
4234 	if (!mnt_has_parent(root_mnt))
4235 		goto out4; /* not attached */
4236 	if (!path_mounted(&new))
4237 		goto out4; /* not a mountpoint */
4238 	if (!mnt_has_parent(new_mnt))
4239 		goto out4; /* not attached */
4240 	/* make sure we can reach put_old from new_root */
4241 	if (!is_path_reachable(old_mnt, old.dentry, &new))
4242 		goto out4;
4243 	/* make certain new is below the root */
4244 	if (!is_path_reachable(new_mnt, new.dentry, &root))
4245 		goto out4;
4246 	lock_mount_hash();
4247 	umount_mnt(new_mnt);
4248 	root_mp = unhash_mnt(root_mnt);  /* we'll need its mountpoint */
4249 	if (root_mnt->mnt.mnt_flags & MNT_LOCKED) {
4250 		new_mnt->mnt.mnt_flags |= MNT_LOCKED;
4251 		root_mnt->mnt.mnt_flags &= ~MNT_LOCKED;
4252 	}
4253 	/* mount old root on put_old */
4254 	attach_mnt(root_mnt, old_mnt, old_mp, false);
4255 	/* mount new_root on / */
4256 	attach_mnt(new_mnt, root_parent, root_mp, false);
4257 	mnt_add_count(root_parent, -1);
4258 	touch_mnt_namespace(current->nsproxy->mnt_ns);
4259 	/* A moved mount should not expire automatically */
4260 	list_del_init(&new_mnt->mnt_expire);
4261 	put_mountpoint(root_mp);
4262 	unlock_mount_hash();
4263 	chroot_fs_refs(&root, &new);
4264 	error = 0;
4265 out4:
4266 	unlock_mount(old_mp);
4267 	if (!error)
4268 		mntput_no_expire(ex_parent);
4269 out3:
4270 	path_put(&root);
4271 out2:
4272 	path_put(&old);
4273 out1:
4274 	path_put(&new);
4275 out0:
4276 	return error;
4277 }
4278 
recalc_flags(struct mount_kattr * kattr,struct mount * mnt)4279 static unsigned int recalc_flags(struct mount_kattr *kattr, struct mount *mnt)
4280 {
4281 	unsigned int flags = mnt->mnt.mnt_flags;
4282 
4283 	/*  flags to clear */
4284 	flags &= ~kattr->attr_clr;
4285 	/* flags to raise */
4286 	flags |= kattr->attr_set;
4287 
4288 	return flags;
4289 }
4290 
can_idmap_mount(const struct mount_kattr * kattr,struct mount * mnt)4291 static int can_idmap_mount(const struct mount_kattr *kattr, struct mount *mnt)
4292 {
4293 	struct vfsmount *m = &mnt->mnt;
4294 	struct user_namespace *fs_userns = m->mnt_sb->s_user_ns;
4295 
4296 	if (!kattr->mnt_idmap)
4297 		return 0;
4298 
4299 	/*
4300 	 * Creating an idmapped mount with the filesystem wide idmapping
4301 	 * doesn't make sense so block that. We don't allow mushy semantics.
4302 	 */
4303 	if (!check_fsmapping(kattr->mnt_idmap, m->mnt_sb))
4304 		return -EINVAL;
4305 
4306 	/*
4307 	 * Once a mount has been idmapped we don't allow it to change its
4308 	 * mapping. It makes things simpler and callers can just create
4309 	 * another bind-mount they can idmap if they want to.
4310 	 */
4311 	if (is_idmapped_mnt(m))
4312 		return -EPERM;
4313 
4314 	/* The underlying filesystem doesn't support idmapped mounts yet. */
4315 	if (!(m->mnt_sb->s_type->fs_flags & FS_ALLOW_IDMAP))
4316 		return -EINVAL;
4317 
4318 	/* We're not controlling the superblock. */
4319 	if (!ns_capable(fs_userns, CAP_SYS_ADMIN))
4320 		return -EPERM;
4321 
4322 	/* Mount has already been visible in the filesystem hierarchy. */
4323 	if (!is_anon_ns(mnt->mnt_ns))
4324 		return -EINVAL;
4325 
4326 	return 0;
4327 }
4328 
4329 /**
4330  * mnt_allow_writers() - check whether the attribute change allows writers
4331  * @kattr: the new mount attributes
4332  * @mnt: the mount to which @kattr will be applied
4333  *
4334  * Check whether thew new mount attributes in @kattr allow concurrent writers.
4335  *
4336  * Return: true if writers need to be held, false if not
4337  */
mnt_allow_writers(const struct mount_kattr * kattr,const struct mount * mnt)4338 static inline bool mnt_allow_writers(const struct mount_kattr *kattr,
4339 				     const struct mount *mnt)
4340 {
4341 	return (!(kattr->attr_set & MNT_READONLY) ||
4342 		(mnt->mnt.mnt_flags & MNT_READONLY)) &&
4343 	       !kattr->mnt_idmap;
4344 }
4345 
mount_setattr_prepare(struct mount_kattr * kattr,struct mount * mnt)4346 static int mount_setattr_prepare(struct mount_kattr *kattr, struct mount *mnt)
4347 {
4348 	struct mount *m;
4349 	int err;
4350 
4351 	for (m = mnt; m; m = next_mnt(m, mnt)) {
4352 		if (!can_change_locked_flags(m, recalc_flags(kattr, m))) {
4353 			err = -EPERM;
4354 			break;
4355 		}
4356 
4357 		err = can_idmap_mount(kattr, m);
4358 		if (err)
4359 			break;
4360 
4361 		if (!mnt_allow_writers(kattr, m)) {
4362 			err = mnt_hold_writers(m);
4363 			if (err)
4364 				break;
4365 		}
4366 
4367 		if (!kattr->recurse)
4368 			return 0;
4369 	}
4370 
4371 	if (err) {
4372 		struct mount *p;
4373 
4374 		/*
4375 		 * If we had to call mnt_hold_writers() MNT_WRITE_HOLD will
4376 		 * be set in @mnt_flags. The loop unsets MNT_WRITE_HOLD for all
4377 		 * mounts and needs to take care to include the first mount.
4378 		 */
4379 		for (p = mnt; p; p = next_mnt(p, mnt)) {
4380 			/* If we had to hold writers unblock them. */
4381 			if (p->mnt.mnt_flags & MNT_WRITE_HOLD)
4382 				mnt_unhold_writers(p);
4383 
4384 			/*
4385 			 * We're done once the first mount we changed got
4386 			 * MNT_WRITE_HOLD unset.
4387 			 */
4388 			if (p == m)
4389 				break;
4390 		}
4391 	}
4392 	return err;
4393 }
4394 
do_idmap_mount(const struct mount_kattr * kattr,struct mount * mnt)4395 static void do_idmap_mount(const struct mount_kattr *kattr, struct mount *mnt)
4396 {
4397 	if (!kattr->mnt_idmap)
4398 		return;
4399 
4400 	/*
4401 	 * Pairs with smp_load_acquire() in mnt_idmap().
4402 	 *
4403 	 * Since we only allow a mount to change the idmapping once and
4404 	 * verified this in can_idmap_mount() we know that the mount has
4405 	 * @nop_mnt_idmap attached to it. So there's no need to drop any
4406 	 * references.
4407 	 */
4408 	smp_store_release(&mnt->mnt.mnt_idmap, mnt_idmap_get(kattr->mnt_idmap));
4409 }
4410 
mount_setattr_commit(struct mount_kattr * kattr,struct mount * mnt)4411 static void mount_setattr_commit(struct mount_kattr *kattr, struct mount *mnt)
4412 {
4413 	struct mount *m;
4414 
4415 	for (m = mnt; m; m = next_mnt(m, mnt)) {
4416 		unsigned int flags;
4417 
4418 		do_idmap_mount(kattr, m);
4419 		flags = recalc_flags(kattr, m);
4420 		WRITE_ONCE(m->mnt.mnt_flags, flags);
4421 
4422 		/* If we had to hold writers unblock them. */
4423 		if (m->mnt.mnt_flags & MNT_WRITE_HOLD)
4424 			mnt_unhold_writers(m);
4425 
4426 		if (kattr->propagation)
4427 			change_mnt_propagation(m, kattr->propagation);
4428 		if (!kattr->recurse)
4429 			break;
4430 	}
4431 	touch_mnt_namespace(mnt->mnt_ns);
4432 }
4433 
do_mount_setattr(struct path * path,struct mount_kattr * kattr)4434 static int do_mount_setattr(struct path *path, struct mount_kattr *kattr)
4435 {
4436 	struct mount *mnt = real_mount(path->mnt);
4437 	int err = 0;
4438 
4439 	if (!path_mounted(path))
4440 		return -EINVAL;
4441 
4442 	if (kattr->mnt_userns) {
4443 		struct mnt_idmap *mnt_idmap;
4444 
4445 		mnt_idmap = alloc_mnt_idmap(kattr->mnt_userns);
4446 		if (IS_ERR(mnt_idmap))
4447 			return PTR_ERR(mnt_idmap);
4448 		kattr->mnt_idmap = mnt_idmap;
4449 	}
4450 
4451 	if (kattr->propagation) {
4452 		/*
4453 		 * Only take namespace_lock() if we're actually changing
4454 		 * propagation.
4455 		 */
4456 		namespace_lock();
4457 		if (kattr->propagation == MS_SHARED) {
4458 			err = invent_group_ids(mnt, kattr->recurse);
4459 			if (err) {
4460 				namespace_unlock();
4461 				return err;
4462 			}
4463 		}
4464 	}
4465 
4466 	err = -EINVAL;
4467 	lock_mount_hash();
4468 
4469 	/* Ensure that this isn't anything purely vfs internal. */
4470 	if (!is_mounted(&mnt->mnt))
4471 		goto out;
4472 
4473 	/*
4474 	 * If this is an attached mount make sure it's located in the callers
4475 	 * mount namespace. If it's not don't let the caller interact with it.
4476 	 *
4477 	 * If this mount doesn't have a parent it's most often simply a
4478 	 * detached mount with an anonymous mount namespace. IOW, something
4479 	 * that's simply not attached yet. But there are apparently also users
4480 	 * that do change mount properties on the rootfs itself. That obviously
4481 	 * neither has a parent nor is it a detached mount so we cannot
4482 	 * unconditionally check for detached mounts.
4483 	 */
4484 	if ((mnt_has_parent(mnt) || !is_anon_ns(mnt->mnt_ns)) && !check_mnt(mnt))
4485 		goto out;
4486 
4487 	/*
4488 	 * First, we get the mount tree in a shape where we can change mount
4489 	 * properties without failure. If we succeeded to do so we commit all
4490 	 * changes and if we failed we clean up.
4491 	 */
4492 	err = mount_setattr_prepare(kattr, mnt);
4493 	if (!err)
4494 		mount_setattr_commit(kattr, mnt);
4495 
4496 out:
4497 	unlock_mount_hash();
4498 
4499 	if (kattr->propagation) {
4500 		if (err)
4501 			cleanup_group_ids(mnt, NULL);
4502 		namespace_unlock();
4503 	}
4504 
4505 	return err;
4506 }
4507 
build_mount_idmapped(const struct mount_attr * attr,size_t usize,struct mount_kattr * kattr,unsigned int flags)4508 static int build_mount_idmapped(const struct mount_attr *attr, size_t usize,
4509 				struct mount_kattr *kattr, unsigned int flags)
4510 {
4511 	int err = 0;
4512 	struct ns_common *ns;
4513 	struct user_namespace *mnt_userns;
4514 	struct fd f;
4515 
4516 	if (!((attr->attr_set | attr->attr_clr) & MOUNT_ATTR_IDMAP))
4517 		return 0;
4518 
4519 	/*
4520 	 * We currently do not support clearing an idmapped mount. If this ever
4521 	 * is a use-case we can revisit this but for now let's keep it simple
4522 	 * and not allow it.
4523 	 */
4524 	if (attr->attr_clr & MOUNT_ATTR_IDMAP)
4525 		return -EINVAL;
4526 
4527 	if (attr->userns_fd > INT_MAX)
4528 		return -EINVAL;
4529 
4530 	f = fdget(attr->userns_fd);
4531 	if (!f.file)
4532 		return -EBADF;
4533 
4534 	if (!proc_ns_file(f.file)) {
4535 		err = -EINVAL;
4536 		goto out_fput;
4537 	}
4538 
4539 	ns = get_proc_ns(file_inode(f.file));
4540 	if (ns->ops->type != CLONE_NEWUSER) {
4541 		err = -EINVAL;
4542 		goto out_fput;
4543 	}
4544 
4545 	/*
4546 	 * The initial idmapping cannot be used to create an idmapped
4547 	 * mount. We use the initial idmapping as an indicator of a mount
4548 	 * that is not idmapped. It can simply be passed into helpers that
4549 	 * are aware of idmapped mounts as a convenient shortcut. A user
4550 	 * can just create a dedicated identity mapping to achieve the same
4551 	 * result.
4552 	 */
4553 	mnt_userns = container_of(ns, struct user_namespace, ns);
4554 	if (mnt_userns == &init_user_ns) {
4555 		err = -EPERM;
4556 		goto out_fput;
4557 	}
4558 
4559 	/* We're not controlling the target namespace. */
4560 	if (!ns_capable(mnt_userns, CAP_SYS_ADMIN)) {
4561 		err = -EPERM;
4562 		goto out_fput;
4563 	}
4564 
4565 	kattr->mnt_userns = get_user_ns(mnt_userns);
4566 
4567 out_fput:
4568 	fdput(f);
4569 	return err;
4570 }
4571 
build_mount_kattr(const struct mount_attr * attr,size_t usize,struct mount_kattr * kattr,unsigned int flags)4572 static int build_mount_kattr(const struct mount_attr *attr, size_t usize,
4573 			     struct mount_kattr *kattr, unsigned int flags)
4574 {
4575 	unsigned int lookup_flags = LOOKUP_AUTOMOUNT | LOOKUP_FOLLOW;
4576 
4577 	if (flags & AT_NO_AUTOMOUNT)
4578 		lookup_flags &= ~LOOKUP_AUTOMOUNT;
4579 	if (flags & AT_SYMLINK_NOFOLLOW)
4580 		lookup_flags &= ~LOOKUP_FOLLOW;
4581 	if (flags & AT_EMPTY_PATH)
4582 		lookup_flags |= LOOKUP_EMPTY;
4583 
4584 	*kattr = (struct mount_kattr) {
4585 		.lookup_flags	= lookup_flags,
4586 		.recurse	= !!(flags & AT_RECURSIVE),
4587 	};
4588 
4589 	if (attr->propagation & ~MOUNT_SETATTR_PROPAGATION_FLAGS)
4590 		return -EINVAL;
4591 	if (hweight32(attr->propagation & MOUNT_SETATTR_PROPAGATION_FLAGS) > 1)
4592 		return -EINVAL;
4593 	kattr->propagation = attr->propagation;
4594 
4595 	if ((attr->attr_set | attr->attr_clr) & ~MOUNT_SETATTR_VALID_FLAGS)
4596 		return -EINVAL;
4597 
4598 	kattr->attr_set = attr_flags_to_mnt_flags(attr->attr_set);
4599 	kattr->attr_clr = attr_flags_to_mnt_flags(attr->attr_clr);
4600 
4601 	/*
4602 	 * Since the MOUNT_ATTR_<atime> values are an enum, not a bitmap,
4603 	 * users wanting to transition to a different atime setting cannot
4604 	 * simply specify the atime setting in @attr_set, but must also
4605 	 * specify MOUNT_ATTR__ATIME in the @attr_clr field.
4606 	 * So ensure that MOUNT_ATTR__ATIME can't be partially set in
4607 	 * @attr_clr and that @attr_set can't have any atime bits set if
4608 	 * MOUNT_ATTR__ATIME isn't set in @attr_clr.
4609 	 */
4610 	if (attr->attr_clr & MOUNT_ATTR__ATIME) {
4611 		if ((attr->attr_clr & MOUNT_ATTR__ATIME) != MOUNT_ATTR__ATIME)
4612 			return -EINVAL;
4613 
4614 		/*
4615 		 * Clear all previous time settings as they are mutually
4616 		 * exclusive.
4617 		 */
4618 		kattr->attr_clr |= MNT_RELATIME | MNT_NOATIME;
4619 		switch (attr->attr_set & MOUNT_ATTR__ATIME) {
4620 		case MOUNT_ATTR_RELATIME:
4621 			kattr->attr_set |= MNT_RELATIME;
4622 			break;
4623 		case MOUNT_ATTR_NOATIME:
4624 			kattr->attr_set |= MNT_NOATIME;
4625 			break;
4626 		case MOUNT_ATTR_STRICTATIME:
4627 			break;
4628 		default:
4629 			return -EINVAL;
4630 		}
4631 	} else {
4632 		if (attr->attr_set & MOUNT_ATTR__ATIME)
4633 			return -EINVAL;
4634 	}
4635 
4636 	return build_mount_idmapped(attr, usize, kattr, flags);
4637 }
4638 
finish_mount_kattr(struct mount_kattr * kattr)4639 static void finish_mount_kattr(struct mount_kattr *kattr)
4640 {
4641 	put_user_ns(kattr->mnt_userns);
4642 	kattr->mnt_userns = NULL;
4643 
4644 	if (kattr->mnt_idmap)
4645 		mnt_idmap_put(kattr->mnt_idmap);
4646 }
4647 
SYSCALL_DEFINE5(mount_setattr,int,dfd,const char __user *,path,unsigned int,flags,struct mount_attr __user *,uattr,size_t,usize)4648 SYSCALL_DEFINE5(mount_setattr, int, dfd, const char __user *, path,
4649 		unsigned int, flags, struct mount_attr __user *, uattr,
4650 		size_t, usize)
4651 {
4652 	int err;
4653 	struct path target;
4654 	struct mount_attr attr;
4655 	struct mount_kattr kattr;
4656 
4657 	BUILD_BUG_ON(sizeof(struct mount_attr) != MOUNT_ATTR_SIZE_VER0);
4658 
4659 	if (flags & ~(AT_EMPTY_PATH |
4660 		      AT_RECURSIVE |
4661 		      AT_SYMLINK_NOFOLLOW |
4662 		      AT_NO_AUTOMOUNT))
4663 		return -EINVAL;
4664 
4665 	if (unlikely(usize > PAGE_SIZE))
4666 		return -E2BIG;
4667 	if (unlikely(usize < MOUNT_ATTR_SIZE_VER0))
4668 		return -EINVAL;
4669 
4670 	if (!may_mount())
4671 		return -EPERM;
4672 
4673 	err = copy_struct_from_user(&attr, sizeof(attr), uattr, usize);
4674 	if (err)
4675 		return err;
4676 
4677 	/* Don't bother walking through the mounts if this is a nop. */
4678 	if (attr.attr_set == 0 &&
4679 	    attr.attr_clr == 0 &&
4680 	    attr.propagation == 0)
4681 		return 0;
4682 
4683 	err = build_mount_kattr(&attr, usize, &kattr, flags);
4684 	if (err)
4685 		return err;
4686 
4687 	err = user_path_at(dfd, path, kattr.lookup_flags, &target);
4688 	if (!err) {
4689 		err = do_mount_setattr(&target, &kattr);
4690 		path_put(&target);
4691 	}
4692 	finish_mount_kattr(&kattr);
4693 	return err;
4694 }
4695 
init_mount_tree(void)4696 static void __init init_mount_tree(void)
4697 {
4698 	struct vfsmount *mnt;
4699 	struct mount *m;
4700 	struct mnt_namespace *ns;
4701 	struct path root;
4702 
4703 	mnt = vfs_kern_mount(&rootfs_fs_type, 0, "rootfs", NULL);
4704 	if (IS_ERR(mnt))
4705 		panic("Can't create rootfs");
4706 
4707 	ns = alloc_mnt_ns(&init_user_ns, false);
4708 	if (IS_ERR(ns))
4709 		panic("Can't allocate initial namespace");
4710 	m = real_mount(mnt);
4711 	m->mnt_ns = ns;
4712 	ns->root = m;
4713 	ns->mounts = 1;
4714 	list_add(&m->mnt_list, &ns->list);
4715 	init_task.nsproxy->mnt_ns = ns;
4716 	get_mnt_ns(ns);
4717 
4718 	root.mnt = mnt;
4719 	root.dentry = mnt->mnt_root;
4720 	mnt->mnt_flags |= MNT_LOCKED;
4721 
4722 	set_fs_pwd(current->fs, &root);
4723 	set_fs_root(current->fs, &root);
4724 }
4725 
mnt_init(void)4726 void __init mnt_init(void)
4727 {
4728 	int err;
4729 
4730 	mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
4731 			0, SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, NULL);
4732 
4733 	mount_hashtable = alloc_large_system_hash("Mount-cache",
4734 				sizeof(struct hlist_head),
4735 				mhash_entries, 19,
4736 				HASH_ZERO,
4737 				&m_hash_shift, &m_hash_mask, 0, 0);
4738 	mountpoint_hashtable = alloc_large_system_hash("Mountpoint-cache",
4739 				sizeof(struct hlist_head),
4740 				mphash_entries, 19,
4741 				HASH_ZERO,
4742 				&mp_hash_shift, &mp_hash_mask, 0, 0);
4743 
4744 	if (!mount_hashtable || !mountpoint_hashtable)
4745 		panic("Failed to allocate mount hash table\n");
4746 
4747 	kernfs_init();
4748 
4749 	err = sysfs_init();
4750 	if (err)
4751 		printk(KERN_WARNING "%s: sysfs_init error: %d\n",
4752 			__func__, err);
4753 	fs_kobj = kobject_create_and_add("fs", NULL);
4754 	if (!fs_kobj)
4755 		printk(KERN_WARNING "%s: kobj create error\n", __func__);
4756 	shmem_init();
4757 	init_rootfs();
4758 	init_mount_tree();
4759 }
4760 
put_mnt_ns(struct mnt_namespace * ns)4761 void put_mnt_ns(struct mnt_namespace *ns)
4762 {
4763 	if (!refcount_dec_and_test(&ns->ns.count))
4764 		return;
4765 	drop_collected_mounts(&ns->root->mnt);
4766 	free_mnt_ns(ns);
4767 }
4768 
kern_mount(struct file_system_type * type)4769 struct vfsmount *kern_mount(struct file_system_type *type)
4770 {
4771 	struct vfsmount *mnt;
4772 	mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, NULL);
4773 	if (!IS_ERR(mnt)) {
4774 		/*
4775 		 * it is a longterm mount, don't release mnt until
4776 		 * we unmount before file sys is unregistered
4777 		*/
4778 		real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL;
4779 	}
4780 	return mnt;
4781 }
4782 EXPORT_SYMBOL_GPL(kern_mount);
4783 
kern_unmount(struct vfsmount * mnt)4784 void kern_unmount(struct vfsmount *mnt)
4785 {
4786 	/* release long term mount so mount point can be released */
4787 	if (!IS_ERR(mnt)) {
4788 		mnt_make_shortterm(mnt);
4789 		synchronize_rcu();	/* yecchhh... */
4790 		mntput(mnt);
4791 	}
4792 }
4793 EXPORT_SYMBOL(kern_unmount);
4794 
kern_unmount_array(struct vfsmount * mnt[],unsigned int num)4795 void kern_unmount_array(struct vfsmount *mnt[], unsigned int num)
4796 {
4797 	unsigned int i;
4798 
4799 	for (i = 0; i < num; i++)
4800 		mnt_make_shortterm(mnt[i]);
4801 	synchronize_rcu_expedited();
4802 	for (i = 0; i < num; i++)
4803 		mntput(mnt[i]);
4804 }
4805 EXPORT_SYMBOL(kern_unmount_array);
4806 
our_mnt(struct vfsmount * mnt)4807 bool our_mnt(struct vfsmount *mnt)
4808 {
4809 	return check_mnt(real_mount(mnt));
4810 }
4811 
current_chrooted(void)4812 bool current_chrooted(void)
4813 {
4814 	/* Does the current process have a non-standard root */
4815 	struct path ns_root;
4816 	struct path fs_root;
4817 	bool chrooted;
4818 
4819 	/* Find the namespace root */
4820 	ns_root.mnt = &current->nsproxy->mnt_ns->root->mnt;
4821 	ns_root.dentry = ns_root.mnt->mnt_root;
4822 	path_get(&ns_root);
4823 	while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root))
4824 		;
4825 
4826 	get_fs_root(current->fs, &fs_root);
4827 
4828 	chrooted = !path_equal(&fs_root, &ns_root);
4829 
4830 	path_put(&fs_root);
4831 	path_put(&ns_root);
4832 
4833 	return chrooted;
4834 }
4835 
mnt_already_visible(struct mnt_namespace * ns,const struct super_block * sb,int * new_mnt_flags)4836 static bool mnt_already_visible(struct mnt_namespace *ns,
4837 				const struct super_block *sb,
4838 				int *new_mnt_flags)
4839 {
4840 	int new_flags = *new_mnt_flags;
4841 	struct mount *mnt;
4842 	bool visible = false;
4843 
4844 	down_read(&namespace_sem);
4845 	lock_ns_list(ns);
4846 	list_for_each_entry(mnt, &ns->list, mnt_list) {
4847 		struct mount *child;
4848 		int mnt_flags;
4849 
4850 		if (mnt_is_cursor(mnt))
4851 			continue;
4852 
4853 		if (mnt->mnt.mnt_sb->s_type != sb->s_type)
4854 			continue;
4855 
4856 		/* This mount is not fully visible if it's root directory
4857 		 * is not the root directory of the filesystem.
4858 		 */
4859 		if (mnt->mnt.mnt_root != mnt->mnt.mnt_sb->s_root)
4860 			continue;
4861 
4862 		/* A local view of the mount flags */
4863 		mnt_flags = mnt->mnt.mnt_flags;
4864 
4865 		/* Don't miss readonly hidden in the superblock flags */
4866 		if (sb_rdonly(mnt->mnt.mnt_sb))
4867 			mnt_flags |= MNT_LOCK_READONLY;
4868 
4869 		/* Verify the mount flags are equal to or more permissive
4870 		 * than the proposed new mount.
4871 		 */
4872 		if ((mnt_flags & MNT_LOCK_READONLY) &&
4873 		    !(new_flags & MNT_READONLY))
4874 			continue;
4875 		if ((mnt_flags & MNT_LOCK_ATIME) &&
4876 		    ((mnt_flags & MNT_ATIME_MASK) != (new_flags & MNT_ATIME_MASK)))
4877 			continue;
4878 
4879 		/* This mount is not fully visible if there are any
4880 		 * locked child mounts that cover anything except for
4881 		 * empty directories.
4882 		 */
4883 		list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
4884 			struct inode *inode = child->mnt_mountpoint->d_inode;
4885 			/* Only worry about locked mounts */
4886 			if (!(child->mnt.mnt_flags & MNT_LOCKED))
4887 				continue;
4888 			/* Is the directory permanetly empty? */
4889 			if (!is_empty_dir_inode(inode))
4890 				goto next;
4891 		}
4892 		/* Preserve the locked attributes */
4893 		*new_mnt_flags |= mnt_flags & (MNT_LOCK_READONLY | \
4894 					       MNT_LOCK_ATIME);
4895 		visible = true;
4896 		goto found;
4897 	next:	;
4898 	}
4899 found:
4900 	unlock_ns_list(ns);
4901 	up_read(&namespace_sem);
4902 	return visible;
4903 }
4904 
mount_too_revealing(const struct super_block * sb,int * new_mnt_flags)4905 static bool mount_too_revealing(const struct super_block *sb, int *new_mnt_flags)
4906 {
4907 	const unsigned long required_iflags = SB_I_NOEXEC | SB_I_NODEV;
4908 	struct mnt_namespace *ns = current->nsproxy->mnt_ns;
4909 	unsigned long s_iflags;
4910 
4911 	if (ns->user_ns == &init_user_ns)
4912 		return false;
4913 
4914 	/* Can this filesystem be too revealing? */
4915 	s_iflags = sb->s_iflags;
4916 	if (!(s_iflags & SB_I_USERNS_VISIBLE))
4917 		return false;
4918 
4919 	if ((s_iflags & required_iflags) != required_iflags) {
4920 		WARN_ONCE(1, "Expected s_iflags to contain 0x%lx\n",
4921 			  required_iflags);
4922 		return true;
4923 	}
4924 
4925 	return !mnt_already_visible(ns, sb, new_mnt_flags);
4926 }
4927 
mnt_may_suid(struct vfsmount * mnt)4928 bool mnt_may_suid(struct vfsmount *mnt)
4929 {
4930 	/*
4931 	 * Foreign mounts (accessed via fchdir or through /proc
4932 	 * symlinks) are always treated as if they are nosuid.  This
4933 	 * prevents namespaces from trusting potentially unsafe
4934 	 * suid/sgid bits, file caps, or security labels that originate
4935 	 * in other namespaces.
4936 	 */
4937 	return !(mnt->mnt_flags & MNT_NOSUID) && check_mnt(real_mount(mnt)) &&
4938 	       current_in_userns(mnt->mnt_sb->s_user_ns);
4939 }
4940 
mntns_get(struct task_struct * task)4941 static struct ns_common *mntns_get(struct task_struct *task)
4942 {
4943 	struct ns_common *ns = NULL;
4944 	struct nsproxy *nsproxy;
4945 
4946 	task_lock(task);
4947 	nsproxy = task->nsproxy;
4948 	if (nsproxy) {
4949 		ns = &nsproxy->mnt_ns->ns;
4950 		get_mnt_ns(to_mnt_ns(ns));
4951 	}
4952 	task_unlock(task);
4953 
4954 	return ns;
4955 }
4956 
mntns_put(struct ns_common * ns)4957 static void mntns_put(struct ns_common *ns)
4958 {
4959 	put_mnt_ns(to_mnt_ns(ns));
4960 }
4961 
mntns_install(struct nsset * nsset,struct ns_common * ns)4962 static int mntns_install(struct nsset *nsset, struct ns_common *ns)
4963 {
4964 	struct nsproxy *nsproxy = nsset->nsproxy;
4965 	struct fs_struct *fs = nsset->fs;
4966 	struct mnt_namespace *mnt_ns = to_mnt_ns(ns), *old_mnt_ns;
4967 	struct user_namespace *user_ns = nsset->cred->user_ns;
4968 	struct path root;
4969 	int err;
4970 
4971 	if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) ||
4972 	    !ns_capable(user_ns, CAP_SYS_CHROOT) ||
4973 	    !ns_capable(user_ns, CAP_SYS_ADMIN))
4974 		return -EPERM;
4975 
4976 	if (is_anon_ns(mnt_ns))
4977 		return -EINVAL;
4978 
4979 	if (fs->users != 1)
4980 		return -EINVAL;
4981 
4982 	get_mnt_ns(mnt_ns);
4983 	old_mnt_ns = nsproxy->mnt_ns;
4984 	nsproxy->mnt_ns = mnt_ns;
4985 
4986 	/* Find the root */
4987 	err = vfs_path_lookup(mnt_ns->root->mnt.mnt_root, &mnt_ns->root->mnt,
4988 				"/", LOOKUP_DOWN, &root);
4989 	if (err) {
4990 		/* revert to old namespace */
4991 		nsproxy->mnt_ns = old_mnt_ns;
4992 		put_mnt_ns(mnt_ns);
4993 		return err;
4994 	}
4995 
4996 	put_mnt_ns(old_mnt_ns);
4997 
4998 	/* Update the pwd and root */
4999 	set_fs_pwd(fs, &root);
5000 	set_fs_root(fs, &root);
5001 
5002 	path_put(&root);
5003 	return 0;
5004 }
5005 
mntns_owner(struct ns_common * ns)5006 static struct user_namespace *mntns_owner(struct ns_common *ns)
5007 {
5008 	return to_mnt_ns(ns)->user_ns;
5009 }
5010 
5011 const struct proc_ns_operations mntns_operations = {
5012 	.name		= "mnt",
5013 	.type		= CLONE_NEWNS,
5014 	.get		= mntns_get,
5015 	.put		= mntns_put,
5016 	.install	= mntns_install,
5017 	.owner		= mntns_owner,
5018 };
5019 
5020 #ifdef CONFIG_SYSCTL
5021 static struct ctl_table fs_namespace_sysctls[] = {
5022 	{
5023 		.procname	= "mount-max",
5024 		.data		= &sysctl_mount_max,
5025 		.maxlen		= sizeof(unsigned int),
5026 		.mode		= 0644,
5027 		.proc_handler	= proc_dointvec_minmax,
5028 		.extra1		= SYSCTL_ONE,
5029 	},
5030 	{ }
5031 };
5032 
init_fs_namespace_sysctls(void)5033 static int __init init_fs_namespace_sysctls(void)
5034 {
5035 	register_sysctl_init("fs", fs_namespace_sysctls);
5036 	return 0;
5037 }
5038 fs_initcall(init_fs_namespace_sysctls);
5039 
5040 #endif /* CONFIG_SYSCTL */
5041