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