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