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