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