• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  linux/fs/namespace.c
3  *
4  * (C) Copyright Al Viro 2000, 2001
5  *	Released under GPL v2.
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/idr.h>
19 #include <linux/init.h>		/* init_rootfs */
20 #include <linux/fs_struct.h>	/* get_fs_root et.al. */
21 #include <linux/fsnotify.h>	/* fsnotify_vfsmount_delete */
22 #include <linux/uaccess.h>
23 #include <linux/proc_ns.h>
24 #include <linux/magic.h>
25 #include <linux/bootmem.h>
26 #include <linux/task_work.h>
27 #include "pnode.h"
28 #include "internal.h"
29 
30 static unsigned int m_hash_mask __read_mostly;
31 static unsigned int m_hash_shift __read_mostly;
32 static unsigned int mp_hash_mask __read_mostly;
33 static unsigned int mp_hash_shift __read_mostly;
34 
35 static __initdata unsigned long mhash_entries;
set_mhash_entries(char * str)36 static int __init set_mhash_entries(char *str)
37 {
38 	if (!str)
39 		return 0;
40 	mhash_entries = simple_strtoul(str, &str, 0);
41 	return 1;
42 }
43 __setup("mhash_entries=", set_mhash_entries);
44 
45 static __initdata unsigned long mphash_entries;
set_mphash_entries(char * str)46 static int __init set_mphash_entries(char *str)
47 {
48 	if (!str)
49 		return 0;
50 	mphash_entries = simple_strtoul(str, &str, 0);
51 	return 1;
52 }
53 __setup("mphash_entries=", set_mphash_entries);
54 
55 static u64 event;
56 static DEFINE_IDA(mnt_id_ida);
57 static DEFINE_IDA(mnt_group_ida);
58 static DEFINE_SPINLOCK(mnt_id_lock);
59 static int mnt_id_start = 0;
60 static int mnt_group_start = 1;
61 
62 static struct hlist_head *mount_hashtable __read_mostly;
63 static struct hlist_head *mountpoint_hashtable __read_mostly;
64 static struct kmem_cache *mnt_cache __read_mostly;
65 static DECLARE_RWSEM(namespace_sem);
66 
67 /* /sys/fs */
68 struct kobject *fs_kobj;
69 EXPORT_SYMBOL_GPL(fs_kobj);
70 
71 /*
72  * vfsmount lock may be taken for read to prevent changes to the
73  * vfsmount hash, ie. during mountpoint lookups or walking back
74  * up the tree.
75  *
76  * It should be taken for write in all cases where the vfsmount
77  * tree or hash is modified or when a vfsmount structure is modified.
78  */
79 __cacheline_aligned_in_smp DEFINE_SEQLOCK(mount_lock);
80 
m_hash(struct vfsmount * mnt,struct dentry * dentry)81 static inline struct hlist_head *m_hash(struct vfsmount *mnt, struct dentry *dentry)
82 {
83 	unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
84 	tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
85 	tmp = tmp + (tmp >> m_hash_shift);
86 	return &mount_hashtable[tmp & m_hash_mask];
87 }
88 
mp_hash(struct dentry * dentry)89 static inline struct hlist_head *mp_hash(struct dentry *dentry)
90 {
91 	unsigned long tmp = ((unsigned long)dentry / L1_CACHE_BYTES);
92 	tmp = tmp + (tmp >> mp_hash_shift);
93 	return &mountpoint_hashtable[tmp & mp_hash_mask];
94 }
95 
96 /*
97  * allocation is serialized by namespace_sem, but we need the spinlock to
98  * serialize with freeing.
99  */
mnt_alloc_id(struct mount * mnt)100 static int mnt_alloc_id(struct mount *mnt)
101 {
102 	int res;
103 
104 retry:
105 	ida_pre_get(&mnt_id_ida, GFP_KERNEL);
106 	spin_lock(&mnt_id_lock);
107 	res = ida_get_new_above(&mnt_id_ida, mnt_id_start, &mnt->mnt_id);
108 	if (!res)
109 		mnt_id_start = mnt->mnt_id + 1;
110 	spin_unlock(&mnt_id_lock);
111 	if (res == -EAGAIN)
112 		goto retry;
113 
114 	return res;
115 }
116 
mnt_free_id(struct mount * mnt)117 static void mnt_free_id(struct mount *mnt)
118 {
119 	int id = mnt->mnt_id;
120 	spin_lock(&mnt_id_lock);
121 	ida_remove(&mnt_id_ida, id);
122 	if (mnt_id_start > id)
123 		mnt_id_start = id;
124 	spin_unlock(&mnt_id_lock);
125 }
126 
127 /*
128  * Allocate a new peer group ID
129  *
130  * mnt_group_ida is protected by namespace_sem
131  */
mnt_alloc_group_id(struct mount * mnt)132 static int mnt_alloc_group_id(struct mount *mnt)
133 {
134 	int res;
135 
136 	if (!ida_pre_get(&mnt_group_ida, GFP_KERNEL))
137 		return -ENOMEM;
138 
139 	res = ida_get_new_above(&mnt_group_ida,
140 				mnt_group_start,
141 				&mnt->mnt_group_id);
142 	if (!res)
143 		mnt_group_start = mnt->mnt_group_id + 1;
144 
145 	return res;
146 }
147 
148 /*
149  * Release a peer group ID
150  */
mnt_release_group_id(struct mount * mnt)151 void mnt_release_group_id(struct mount *mnt)
152 {
153 	int id = mnt->mnt_group_id;
154 	ida_remove(&mnt_group_ida, id);
155 	if (mnt_group_start > id)
156 		mnt_group_start = id;
157 	mnt->mnt_group_id = 0;
158 }
159 
160 /*
161  * vfsmount lock must be held for read
162  */
mnt_add_count(struct mount * mnt,int n)163 static inline void mnt_add_count(struct mount *mnt, int n)
164 {
165 #ifdef CONFIG_SMP
166 	this_cpu_add(mnt->mnt_pcp->mnt_count, n);
167 #else
168 	preempt_disable();
169 	mnt->mnt_count += n;
170 	preempt_enable();
171 #endif
172 }
173 
174 /*
175  * vfsmount lock must be held for write
176  */
mnt_get_count(struct mount * mnt)177 unsigned int mnt_get_count(struct mount *mnt)
178 {
179 #ifdef CONFIG_SMP
180 	unsigned int count = 0;
181 	int cpu;
182 
183 	for_each_possible_cpu(cpu) {
184 		count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
185 	}
186 
187 	return count;
188 #else
189 	return mnt->mnt_count;
190 #endif
191 }
192 
alloc_vfsmnt(const char * name)193 static struct mount *alloc_vfsmnt(const char *name)
194 {
195 	struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
196 	if (mnt) {
197 		int err;
198 
199 		err = mnt_alloc_id(mnt);
200 		if (err)
201 			goto out_free_cache;
202 
203 		if (name) {
204 			mnt->mnt_devname = kstrdup(name, GFP_KERNEL);
205 			if (!mnt->mnt_devname)
206 				goto out_free_id;
207 		}
208 
209 #ifdef CONFIG_SMP
210 		mnt->mnt_pcp = alloc_percpu(struct mnt_pcp);
211 		if (!mnt->mnt_pcp)
212 			goto out_free_devname;
213 
214 		this_cpu_add(mnt->mnt_pcp->mnt_count, 1);
215 #else
216 		mnt->mnt_count = 1;
217 		mnt->mnt_writers = 0;
218 #endif
219 		mnt->mnt.data = NULL;
220 
221 		INIT_HLIST_NODE(&mnt->mnt_hash);
222 		INIT_LIST_HEAD(&mnt->mnt_child);
223 		INIT_LIST_HEAD(&mnt->mnt_mounts);
224 		INIT_LIST_HEAD(&mnt->mnt_list);
225 		INIT_LIST_HEAD(&mnt->mnt_expire);
226 		INIT_LIST_HEAD(&mnt->mnt_share);
227 		INIT_LIST_HEAD(&mnt->mnt_slave_list);
228 		INIT_LIST_HEAD(&mnt->mnt_slave);
229 		INIT_HLIST_NODE(&mnt->mnt_mp_list);
230 #ifdef CONFIG_FSNOTIFY
231 		INIT_HLIST_HEAD(&mnt->mnt_fsnotify_marks);
232 #endif
233 	}
234 	return mnt;
235 
236 #ifdef CONFIG_SMP
237 out_free_devname:
238 	kfree(mnt->mnt_devname);
239 #endif
240 out_free_id:
241 	mnt_free_id(mnt);
242 out_free_cache:
243 	kmem_cache_free(mnt_cache, mnt);
244 	return NULL;
245 }
246 
247 /*
248  * Most r/o checks on a fs are for operations that take
249  * discrete amounts of time, like a write() or unlink().
250  * We must keep track of when those operations start
251  * (for permission checks) and when they end, so that
252  * we can determine when writes are able to occur to
253  * a filesystem.
254  */
255 /*
256  * __mnt_is_readonly: check whether a mount is read-only
257  * @mnt: the mount to check for its write status
258  *
259  * This shouldn't be used directly ouside of the VFS.
260  * It does not guarantee that the filesystem will stay
261  * r/w, just that it is right *now*.  This can not and
262  * should not be used in place of IS_RDONLY(inode).
263  * mnt_want/drop_write() will _keep_ the filesystem
264  * r/w.
265  */
__mnt_is_readonly(struct vfsmount * mnt)266 int __mnt_is_readonly(struct vfsmount *mnt)
267 {
268 	if (mnt->mnt_flags & MNT_READONLY)
269 		return 1;
270 	if (mnt->mnt_sb->s_flags & MS_RDONLY)
271 		return 1;
272 	return 0;
273 }
274 EXPORT_SYMBOL_GPL(__mnt_is_readonly);
275 
mnt_inc_writers(struct mount * mnt)276 static inline void mnt_inc_writers(struct mount *mnt)
277 {
278 #ifdef CONFIG_SMP
279 	this_cpu_inc(mnt->mnt_pcp->mnt_writers);
280 #else
281 	mnt->mnt_writers++;
282 #endif
283 }
284 
mnt_dec_writers(struct mount * mnt)285 static inline void mnt_dec_writers(struct mount *mnt)
286 {
287 #ifdef CONFIG_SMP
288 	this_cpu_dec(mnt->mnt_pcp->mnt_writers);
289 #else
290 	mnt->mnt_writers--;
291 #endif
292 }
293 
mnt_get_writers(struct mount * mnt)294 static unsigned int mnt_get_writers(struct mount *mnt)
295 {
296 #ifdef CONFIG_SMP
297 	unsigned int count = 0;
298 	int cpu;
299 
300 	for_each_possible_cpu(cpu) {
301 		count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers;
302 	}
303 
304 	return count;
305 #else
306 	return mnt->mnt_writers;
307 #endif
308 }
309 
mnt_is_readonly(struct vfsmount * mnt)310 static int mnt_is_readonly(struct vfsmount *mnt)
311 {
312 	if (mnt->mnt_sb->s_readonly_remount)
313 		return 1;
314 	/* Order wrt setting s_flags/s_readonly_remount in do_remount() */
315 	smp_rmb();
316 	return __mnt_is_readonly(mnt);
317 }
318 
319 /*
320  * Most r/o & frozen checks on a fs are for operations that take discrete
321  * amounts of time, like a write() or unlink().  We must keep track of when
322  * those operations start (for permission checks) and when they end, so that we
323  * can determine when writes are able to occur to a filesystem.
324  */
325 /**
326  * __mnt_want_write - get write access to a mount without freeze protection
327  * @m: the mount on which to take a write
328  *
329  * This tells the low-level filesystem that a write is about to be performed to
330  * it, and makes sure that writes are allowed (mnt it read-write) before
331  * returning success. This operation does not protect against filesystem being
332  * frozen. When the write operation is finished, __mnt_drop_write() must be
333  * called. This is effectively a refcount.
334  */
__mnt_want_write(struct vfsmount * m)335 int __mnt_want_write(struct vfsmount *m)
336 {
337 	struct mount *mnt = real_mount(m);
338 	int ret = 0;
339 
340 	preempt_disable();
341 	mnt_inc_writers(mnt);
342 	/*
343 	 * The store to mnt_inc_writers must be visible before we pass
344 	 * MNT_WRITE_HOLD loop below, so that the slowpath can see our
345 	 * incremented count after it has set MNT_WRITE_HOLD.
346 	 */
347 	smp_mb();
348 	while (ACCESS_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD)
349 		cpu_relax();
350 	/*
351 	 * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
352 	 * be set to match its requirements. So we must not load that until
353 	 * MNT_WRITE_HOLD is cleared.
354 	 */
355 	smp_rmb();
356 	if (mnt_is_readonly(m)) {
357 		mnt_dec_writers(mnt);
358 		ret = -EROFS;
359 	}
360 	preempt_enable();
361 
362 	return ret;
363 }
364 
365 /**
366  * mnt_want_write - get write access to a mount
367  * @m: the mount on which to take a write
368  *
369  * This tells the low-level filesystem that a write is about to be performed to
370  * it, and makes sure that writes are allowed (mount is read-write, filesystem
371  * is not frozen) before returning success.  When the write operation is
372  * finished, mnt_drop_write() must be called.  This is effectively a refcount.
373  */
mnt_want_write(struct vfsmount * m)374 int mnt_want_write(struct vfsmount *m)
375 {
376 	int ret;
377 
378 	sb_start_write(m->mnt_sb);
379 	ret = __mnt_want_write(m);
380 	if (ret)
381 		sb_end_write(m->mnt_sb);
382 	return ret;
383 }
384 EXPORT_SYMBOL_GPL(mnt_want_write);
385 
386 /**
387  * mnt_clone_write - get write access to a mount
388  * @mnt: the mount on which to take a write
389  *
390  * This is effectively like mnt_want_write, except
391  * it must only be used to take an extra write reference
392  * on a mountpoint that we already know has a write reference
393  * on it. This allows some optimisation.
394  *
395  * After finished, mnt_drop_write must be called as usual to
396  * drop the reference.
397  */
mnt_clone_write(struct vfsmount * mnt)398 int mnt_clone_write(struct vfsmount *mnt)
399 {
400 	/* superblock may be r/o */
401 	if (__mnt_is_readonly(mnt))
402 		return -EROFS;
403 	preempt_disable();
404 	mnt_inc_writers(real_mount(mnt));
405 	preempt_enable();
406 	return 0;
407 }
408 EXPORT_SYMBOL_GPL(mnt_clone_write);
409 
410 /**
411  * __mnt_want_write_file - get write access to a file's mount
412  * @file: the file who's mount on which to take a write
413  *
414  * This is like __mnt_want_write, but it takes a file and can
415  * do some optimisations if the file is open for write already
416  */
__mnt_want_write_file(struct file * file)417 int __mnt_want_write_file(struct file *file)
418 {
419 	if (!(file->f_mode & FMODE_WRITER))
420 		return __mnt_want_write(file->f_path.mnt);
421 	else
422 		return mnt_clone_write(file->f_path.mnt);
423 }
424 
425 /**
426  * mnt_want_write_file - get write access to a file's mount
427  * @file: the file who's mount on which to take a write
428  *
429  * This is like mnt_want_write, but it takes a file and can
430  * do some optimisations if the file is open for write already
431  */
mnt_want_write_file(struct file * file)432 int mnt_want_write_file(struct file *file)
433 {
434 	int ret;
435 
436 	sb_start_write(file->f_path.mnt->mnt_sb);
437 	ret = __mnt_want_write_file(file);
438 	if (ret)
439 		sb_end_write(file->f_path.mnt->mnt_sb);
440 	return ret;
441 }
442 EXPORT_SYMBOL_GPL(mnt_want_write_file);
443 
444 /**
445  * __mnt_drop_write - give up write access to a mount
446  * @mnt: the mount on which to give up write access
447  *
448  * Tells the low-level filesystem that we are done
449  * performing writes to it.  Must be matched with
450  * __mnt_want_write() call above.
451  */
__mnt_drop_write(struct vfsmount * mnt)452 void __mnt_drop_write(struct vfsmount *mnt)
453 {
454 	preempt_disable();
455 	mnt_dec_writers(real_mount(mnt));
456 	preempt_enable();
457 }
458 
459 /**
460  * mnt_drop_write - give up write access to a mount
461  * @mnt: the mount on which to give up write access
462  *
463  * Tells the low-level filesystem that we are done performing writes to it and
464  * also allows filesystem to be frozen again.  Must be matched with
465  * mnt_want_write() call above.
466  */
mnt_drop_write(struct vfsmount * mnt)467 void mnt_drop_write(struct vfsmount *mnt)
468 {
469 	__mnt_drop_write(mnt);
470 	sb_end_write(mnt->mnt_sb);
471 }
472 EXPORT_SYMBOL_GPL(mnt_drop_write);
473 
__mnt_drop_write_file(struct file * file)474 void __mnt_drop_write_file(struct file *file)
475 {
476 	__mnt_drop_write(file->f_path.mnt);
477 }
478 
mnt_drop_write_file(struct file * file)479 void mnt_drop_write_file(struct file *file)
480 {
481 	mnt_drop_write(file->f_path.mnt);
482 }
483 EXPORT_SYMBOL(mnt_drop_write_file);
484 
mnt_make_readonly(struct mount * mnt)485 static int mnt_make_readonly(struct mount *mnt)
486 {
487 	int ret = 0;
488 
489 	lock_mount_hash();
490 	mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
491 	/*
492 	 * After storing MNT_WRITE_HOLD, we'll read the counters. This store
493 	 * should be visible before we do.
494 	 */
495 	smp_mb();
496 
497 	/*
498 	 * With writers on hold, if this value is zero, then there are
499 	 * definitely no active writers (although held writers may subsequently
500 	 * increment the count, they'll have to wait, and decrement it after
501 	 * seeing MNT_READONLY).
502 	 *
503 	 * It is OK to have counter incremented on one CPU and decremented on
504 	 * another: the sum will add up correctly. The danger would be when we
505 	 * sum up each counter, if we read a counter before it is incremented,
506 	 * but then read another CPU's count which it has been subsequently
507 	 * decremented from -- we would see more decrements than we should.
508 	 * MNT_WRITE_HOLD protects against this scenario, because
509 	 * mnt_want_write first increments count, then smp_mb, then spins on
510 	 * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
511 	 * we're counting up here.
512 	 */
513 	if (mnt_get_writers(mnt) > 0)
514 		ret = -EBUSY;
515 	else
516 		mnt->mnt.mnt_flags |= MNT_READONLY;
517 	/*
518 	 * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
519 	 * that become unheld will see MNT_READONLY.
520 	 */
521 	smp_wmb();
522 	mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
523 	unlock_mount_hash();
524 	return ret;
525 }
526 
__mnt_unmake_readonly(struct mount * mnt)527 static void __mnt_unmake_readonly(struct mount *mnt)
528 {
529 	lock_mount_hash();
530 	mnt->mnt.mnt_flags &= ~MNT_READONLY;
531 	unlock_mount_hash();
532 }
533 
sb_prepare_remount_readonly(struct super_block * sb)534 int sb_prepare_remount_readonly(struct super_block *sb)
535 {
536 	struct mount *mnt;
537 	int err = 0;
538 
539 	/* Racy optimization.  Recheck the counter under MNT_WRITE_HOLD */
540 	if (atomic_long_read(&sb->s_remove_count))
541 		return -EBUSY;
542 
543 	lock_mount_hash();
544 	list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
545 		if (!(mnt->mnt.mnt_flags & MNT_READONLY)) {
546 			mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
547 			smp_mb();
548 			if (mnt_get_writers(mnt) > 0) {
549 				err = -EBUSY;
550 				break;
551 			}
552 		}
553 	}
554 	if (!err && atomic_long_read(&sb->s_remove_count))
555 		err = -EBUSY;
556 
557 	if (!err) {
558 		sb->s_readonly_remount = 1;
559 		smp_wmb();
560 	}
561 	list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
562 		if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD)
563 			mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
564 	}
565 	unlock_mount_hash();
566 
567 	return err;
568 }
569 
free_vfsmnt(struct mount * mnt)570 static void free_vfsmnt(struct mount *mnt)
571 {
572 	kfree(mnt->mnt.data);
573 	kfree(mnt->mnt_devname);
574 #ifdef CONFIG_SMP
575 	free_percpu(mnt->mnt_pcp);
576 #endif
577 	kmem_cache_free(mnt_cache, mnt);
578 }
579 
delayed_free_vfsmnt(struct rcu_head * head)580 static void delayed_free_vfsmnt(struct rcu_head *head)
581 {
582 	free_vfsmnt(container_of(head, struct mount, mnt_rcu));
583 }
584 
585 /* call under rcu_read_lock */
legitimize_mnt(struct vfsmount * bastard,unsigned seq)586 bool legitimize_mnt(struct vfsmount *bastard, unsigned seq)
587 {
588 	struct mount *mnt;
589 	if (read_seqretry(&mount_lock, seq))
590 		return false;
591 	if (bastard == NULL)
592 		return true;
593 	mnt = real_mount(bastard);
594 	mnt_add_count(mnt, 1);
595 	if (likely(!read_seqretry(&mount_lock, seq)))
596 		return true;
597 	if (bastard->mnt_flags & MNT_SYNC_UMOUNT) {
598 		mnt_add_count(mnt, -1);
599 		return false;
600 	}
601 	rcu_read_unlock();
602 	mntput(bastard);
603 	rcu_read_lock();
604 	return false;
605 }
606 
607 /*
608  * find the first mount at @dentry on vfsmount @mnt.
609  * call under rcu_read_lock()
610  */
__lookup_mnt(struct vfsmount * mnt,struct dentry * dentry)611 struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
612 {
613 	struct hlist_head *head = m_hash(mnt, dentry);
614 	struct mount *p;
615 
616 	hlist_for_each_entry_rcu(p, head, mnt_hash)
617 		if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry)
618 			return p;
619 	return NULL;
620 }
621 
622 /*
623  * find the last mount at @dentry on vfsmount @mnt.
624  * mount_lock must be held.
625  */
__lookup_mnt_last(struct vfsmount * mnt,struct dentry * dentry)626 struct mount *__lookup_mnt_last(struct vfsmount *mnt, struct dentry *dentry)
627 {
628 	struct mount *p, *res;
629 	res = p = __lookup_mnt(mnt, dentry);
630 	if (!p)
631 		goto out;
632 	hlist_for_each_entry_continue(p, mnt_hash) {
633 		if (&p->mnt_parent->mnt != mnt || p->mnt_mountpoint != dentry)
634 			break;
635 		res = p;
636 	}
637 out:
638 	return res;
639 }
640 
641 /*
642  * lookup_mnt - Return the first child mount mounted at path
643  *
644  * "First" means first mounted chronologically.  If you create the
645  * following mounts:
646  *
647  * mount /dev/sda1 /mnt
648  * mount /dev/sda2 /mnt
649  * mount /dev/sda3 /mnt
650  *
651  * Then lookup_mnt() on the base /mnt dentry in the root mount will
652  * return successively the root dentry and vfsmount of /dev/sda1, then
653  * /dev/sda2, then /dev/sda3, then NULL.
654  *
655  * lookup_mnt takes a reference to the found vfsmount.
656  */
lookup_mnt(struct path * path)657 struct vfsmount *lookup_mnt(struct path *path)
658 {
659 	struct mount *child_mnt;
660 	struct vfsmount *m;
661 	unsigned seq;
662 
663 	rcu_read_lock();
664 	do {
665 		seq = read_seqbegin(&mount_lock);
666 		child_mnt = __lookup_mnt(path->mnt, path->dentry);
667 		m = child_mnt ? &child_mnt->mnt : NULL;
668 	} while (!legitimize_mnt(m, seq));
669 	rcu_read_unlock();
670 	return m;
671 }
672 
673 /*
674  * __is_local_mountpoint - Test to see if dentry is a mountpoint in the
675  *                         current mount namespace.
676  *
677  * The common case is dentries are not mountpoints at all and that
678  * test is handled inline.  For the slow case when we are actually
679  * dealing with a mountpoint of some kind, walk through all of the
680  * mounts in the current mount namespace and test to see if the dentry
681  * is a mountpoint.
682  *
683  * The mount_hashtable is not usable in the context because we
684  * need to identify all mounts that may be in the current mount
685  * namespace not just a mount that happens to have some specified
686  * parent mount.
687  */
__is_local_mountpoint(struct dentry * dentry)688 bool __is_local_mountpoint(struct dentry *dentry)
689 {
690 	struct mnt_namespace *ns = current->nsproxy->mnt_ns;
691 	struct mount *mnt;
692 	bool is_covered = false;
693 
694 	if (!d_mountpoint(dentry))
695 		goto out;
696 
697 	down_read(&namespace_sem);
698 	list_for_each_entry(mnt, &ns->list, mnt_list) {
699 		is_covered = (mnt->mnt_mountpoint == dentry);
700 		if (is_covered)
701 			break;
702 	}
703 	up_read(&namespace_sem);
704 out:
705 	return is_covered;
706 }
707 
lookup_mountpoint(struct dentry * dentry)708 static struct mountpoint *lookup_mountpoint(struct dentry *dentry)
709 {
710 	struct hlist_head *chain = mp_hash(dentry);
711 	struct mountpoint *mp;
712 
713 	hlist_for_each_entry(mp, chain, m_hash) {
714 		if (mp->m_dentry == dentry) {
715 			/* might be worth a WARN_ON() */
716 			if (d_unlinked(dentry))
717 				return ERR_PTR(-ENOENT);
718 			mp->m_count++;
719 			return mp;
720 		}
721 	}
722 	return NULL;
723 }
724 
new_mountpoint(struct dentry * dentry)725 static struct mountpoint *new_mountpoint(struct dentry *dentry)
726 {
727 	struct hlist_head *chain = mp_hash(dentry);
728 	struct mountpoint *mp;
729 	int ret;
730 
731 	mp = kmalloc(sizeof(struct mountpoint), GFP_KERNEL);
732 	if (!mp)
733 		return ERR_PTR(-ENOMEM);
734 
735 	ret = d_set_mounted(dentry);
736 	if (ret) {
737 		kfree(mp);
738 		return ERR_PTR(ret);
739 	}
740 
741 	mp->m_dentry = dentry;
742 	mp->m_count = 1;
743 	hlist_add_head(&mp->m_hash, chain);
744 	INIT_HLIST_HEAD(&mp->m_list);
745 	return mp;
746 }
747 
put_mountpoint(struct mountpoint * mp)748 static void put_mountpoint(struct mountpoint *mp)
749 {
750 	if (!--mp->m_count) {
751 		struct dentry *dentry = mp->m_dentry;
752 		BUG_ON(!hlist_empty(&mp->m_list));
753 		spin_lock(&dentry->d_lock);
754 		dentry->d_flags &= ~DCACHE_MOUNTED;
755 		spin_unlock(&dentry->d_lock);
756 		hlist_del(&mp->m_hash);
757 		kfree(mp);
758 	}
759 }
760 
check_mnt(struct mount * mnt)761 static inline int check_mnt(struct mount *mnt)
762 {
763 	return mnt->mnt_ns == current->nsproxy->mnt_ns;
764 }
765 
766 /*
767  * vfsmount lock must be held for write
768  */
touch_mnt_namespace(struct mnt_namespace * ns)769 static void touch_mnt_namespace(struct mnt_namespace *ns)
770 {
771 	if (ns) {
772 		ns->event = ++event;
773 		wake_up_interruptible(&ns->poll);
774 	}
775 }
776 
777 /*
778  * vfsmount lock must be held for write
779  */
__touch_mnt_namespace(struct mnt_namespace * ns)780 static void __touch_mnt_namespace(struct mnt_namespace *ns)
781 {
782 	if (ns && ns->event != event) {
783 		ns->event = event;
784 		wake_up_interruptible(&ns->poll);
785 	}
786 }
787 
788 /*
789  * vfsmount lock must be held for write
790  */
detach_mnt(struct mount * mnt,struct path * old_path)791 static void detach_mnt(struct mount *mnt, struct path *old_path)
792 {
793 	old_path->dentry = mnt->mnt_mountpoint;
794 	old_path->mnt = &mnt->mnt_parent->mnt;
795 	mnt->mnt_parent = mnt;
796 	mnt->mnt_mountpoint = mnt->mnt.mnt_root;
797 	list_del_init(&mnt->mnt_child);
798 	hlist_del_init_rcu(&mnt->mnt_hash);
799 	hlist_del_init(&mnt->mnt_mp_list);
800 	put_mountpoint(mnt->mnt_mp);
801 	mnt->mnt_mp = NULL;
802 }
803 
804 /*
805  * vfsmount lock must be held for write
806  */
mnt_set_mountpoint(struct mount * mnt,struct mountpoint * mp,struct mount * child_mnt)807 void mnt_set_mountpoint(struct mount *mnt,
808 			struct mountpoint *mp,
809 			struct mount *child_mnt)
810 {
811 	mp->m_count++;
812 	mnt_add_count(mnt, 1);	/* essentially, that's mntget */
813 	child_mnt->mnt_mountpoint = dget(mp->m_dentry);
814 	child_mnt->mnt_parent = mnt;
815 	child_mnt->mnt_mp = mp;
816 	hlist_add_head(&child_mnt->mnt_mp_list, &mp->m_list);
817 }
818 
819 /*
820  * vfsmount lock must be held for write
821  */
attach_mnt(struct mount * mnt,struct mount * parent,struct mountpoint * mp)822 static void attach_mnt(struct mount *mnt,
823 			struct mount *parent,
824 			struct mountpoint *mp)
825 {
826 	mnt_set_mountpoint(parent, mp, mnt);
827 	hlist_add_head_rcu(&mnt->mnt_hash, m_hash(&parent->mnt, mp->m_dentry));
828 	list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
829 }
830 
attach_shadowed(struct mount * mnt,struct mount * parent,struct mount * shadows)831 static void attach_shadowed(struct mount *mnt,
832 			struct mount *parent,
833 			struct mount *shadows)
834 {
835 	if (shadows) {
836 		hlist_add_behind_rcu(&mnt->mnt_hash, &shadows->mnt_hash);
837 		list_add(&mnt->mnt_child, &shadows->mnt_child);
838 	} else {
839 		hlist_add_head_rcu(&mnt->mnt_hash,
840 				m_hash(&parent->mnt, mnt->mnt_mountpoint));
841 		list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
842 	}
843 }
844 
845 /*
846  * vfsmount lock must be held for write
847  */
commit_tree(struct mount * mnt,struct mount * shadows)848 static void commit_tree(struct mount *mnt, struct mount *shadows)
849 {
850 	struct mount *parent = mnt->mnt_parent;
851 	struct mount *m;
852 	LIST_HEAD(head);
853 	struct mnt_namespace *n = parent->mnt_ns;
854 
855 	BUG_ON(parent == mnt);
856 
857 	list_add_tail(&head, &mnt->mnt_list);
858 	list_for_each_entry(m, &head, mnt_list)
859 		m->mnt_ns = n;
860 
861 	list_splice(&head, n->list.prev);
862 
863 	attach_shadowed(mnt, parent, shadows);
864 	touch_mnt_namespace(n);
865 }
866 
next_mnt(struct mount * p,struct mount * root)867 static struct mount *next_mnt(struct mount *p, struct mount *root)
868 {
869 	struct list_head *next = p->mnt_mounts.next;
870 	if (next == &p->mnt_mounts) {
871 		while (1) {
872 			if (p == root)
873 				return NULL;
874 			next = p->mnt_child.next;
875 			if (next != &p->mnt_parent->mnt_mounts)
876 				break;
877 			p = p->mnt_parent;
878 		}
879 	}
880 	return list_entry(next, struct mount, mnt_child);
881 }
882 
skip_mnt_tree(struct mount * p)883 static struct mount *skip_mnt_tree(struct mount *p)
884 {
885 	struct list_head *prev = p->mnt_mounts.prev;
886 	while (prev != &p->mnt_mounts) {
887 		p = list_entry(prev, struct mount, mnt_child);
888 		prev = p->mnt_mounts.prev;
889 	}
890 	return p;
891 }
892 
893 struct vfsmount *
vfs_kern_mount(struct file_system_type * type,int flags,const char * name,void * data)894 vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
895 {
896 	struct mount *mnt;
897 	struct dentry *root;
898 
899 	if (!type)
900 		return ERR_PTR(-ENODEV);
901 
902 	mnt = alloc_vfsmnt(name);
903 	if (!mnt)
904 		return ERR_PTR(-ENOMEM);
905 
906 	if (type->alloc_mnt_data) {
907 		mnt->mnt.data = type->alloc_mnt_data();
908 		if (!mnt->mnt.data) {
909 			mnt_free_id(mnt);
910 			free_vfsmnt(mnt);
911 			return ERR_PTR(-ENOMEM);
912 		}
913 	}
914 	if (flags & MS_KERNMOUNT)
915 		mnt->mnt.mnt_flags = MNT_INTERNAL;
916 
917 	root = mount_fs(type, flags, name, &mnt->mnt, data);
918 	if (IS_ERR(root)) {
919 		mnt_free_id(mnt);
920 		free_vfsmnt(mnt);
921 		return ERR_CAST(root);
922 	}
923 
924 	mnt->mnt.mnt_root = root;
925 	mnt->mnt.mnt_sb = root->d_sb;
926 	mnt->mnt_mountpoint = mnt->mnt.mnt_root;
927 	mnt->mnt_parent = mnt;
928 	lock_mount_hash();
929 	list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts);
930 	unlock_mount_hash();
931 	return &mnt->mnt;
932 }
933 EXPORT_SYMBOL_GPL(vfs_kern_mount);
934 
clone_mnt(struct mount * old,struct dentry * root,int flag)935 static struct mount *clone_mnt(struct mount *old, struct dentry *root,
936 					int flag)
937 {
938 	struct super_block *sb = old->mnt.mnt_sb;
939 	struct mount *mnt;
940 	int err;
941 
942 	mnt = alloc_vfsmnt(old->mnt_devname);
943 	if (!mnt)
944 		return ERR_PTR(-ENOMEM);
945 
946 	if (sb->s_op->clone_mnt_data) {
947 		mnt->mnt.data = sb->s_op->clone_mnt_data(old->mnt.data);
948 		if (!mnt->mnt.data) {
949 			err = -ENOMEM;
950 			goto out_free;
951 		}
952 	}
953 
954 	if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE))
955 		mnt->mnt_group_id = 0; /* not a peer of original */
956 	else
957 		mnt->mnt_group_id = old->mnt_group_id;
958 
959 	if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
960 		err = mnt_alloc_group_id(mnt);
961 		if (err)
962 			goto out_free;
963 	}
964 
965 	mnt->mnt.mnt_flags = old->mnt.mnt_flags & ~(MNT_WRITE_HOLD|MNT_MARKED);
966 	/* Don't allow unprivileged users to change mount flags */
967 	if (flag & CL_UNPRIVILEGED) {
968 		mnt->mnt.mnt_flags |= MNT_LOCK_ATIME;
969 
970 		if (mnt->mnt.mnt_flags & MNT_READONLY)
971 			mnt->mnt.mnt_flags |= MNT_LOCK_READONLY;
972 
973 		if (mnt->mnt.mnt_flags & MNT_NODEV)
974 			mnt->mnt.mnt_flags |= MNT_LOCK_NODEV;
975 
976 		if (mnt->mnt.mnt_flags & MNT_NOSUID)
977 			mnt->mnt.mnt_flags |= MNT_LOCK_NOSUID;
978 
979 		if (mnt->mnt.mnt_flags & MNT_NOEXEC)
980 			mnt->mnt.mnt_flags |= MNT_LOCK_NOEXEC;
981 	}
982 
983 	/* Don't allow unprivileged users to reveal what is under a mount */
984 	if ((flag & CL_UNPRIVILEGED) && list_empty(&old->mnt_expire))
985 		mnt->mnt.mnt_flags |= MNT_LOCKED;
986 
987 	atomic_inc(&sb->s_active);
988 	mnt->mnt.mnt_sb = sb;
989 	mnt->mnt.mnt_root = dget(root);
990 	mnt->mnt_mountpoint = mnt->mnt.mnt_root;
991 	mnt->mnt_parent = mnt;
992 	lock_mount_hash();
993 	list_add_tail(&mnt->mnt_instance, &sb->s_mounts);
994 	unlock_mount_hash();
995 
996 	if ((flag & CL_SLAVE) ||
997 	    ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) {
998 		list_add(&mnt->mnt_slave, &old->mnt_slave_list);
999 		mnt->mnt_master = old;
1000 		CLEAR_MNT_SHARED(mnt);
1001 	} else if (!(flag & CL_PRIVATE)) {
1002 		if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
1003 			list_add(&mnt->mnt_share, &old->mnt_share);
1004 		if (IS_MNT_SLAVE(old))
1005 			list_add(&mnt->mnt_slave, &old->mnt_slave);
1006 		mnt->mnt_master = old->mnt_master;
1007 	}
1008 	if (flag & CL_MAKE_SHARED)
1009 		set_mnt_shared(mnt);
1010 
1011 	/* stick the duplicate mount on the same expiry list
1012 	 * as the original if that was on one */
1013 	if (flag & CL_EXPIRE) {
1014 		if (!list_empty(&old->mnt_expire))
1015 			list_add(&mnt->mnt_expire, &old->mnt_expire);
1016 	}
1017 
1018 	return mnt;
1019 
1020  out_free:
1021 	mnt_free_id(mnt);
1022 	free_vfsmnt(mnt);
1023 	return ERR_PTR(err);
1024 }
1025 
cleanup_mnt(struct mount * mnt)1026 static void cleanup_mnt(struct mount *mnt)
1027 {
1028 	/*
1029 	 * This probably indicates that somebody messed
1030 	 * up a mnt_want/drop_write() pair.  If this
1031 	 * happens, the filesystem was probably unable
1032 	 * to make r/w->r/o transitions.
1033 	 */
1034 	/*
1035 	 * The locking used to deal with mnt_count decrement provides barriers,
1036 	 * so mnt_get_writers() below is safe.
1037 	 */
1038 	WARN_ON(mnt_get_writers(mnt));
1039 	if (unlikely(mnt->mnt_pins.first))
1040 		mnt_pin_kill(mnt);
1041 	fsnotify_vfsmount_delete(&mnt->mnt);
1042 	dput(mnt->mnt.mnt_root);
1043 	deactivate_super(mnt->mnt.mnt_sb);
1044 	mnt_free_id(mnt);
1045 	call_rcu(&mnt->mnt_rcu, delayed_free_vfsmnt);
1046 }
1047 
__cleanup_mnt(struct rcu_head * head)1048 static void __cleanup_mnt(struct rcu_head *head)
1049 {
1050 	cleanup_mnt(container_of(head, struct mount, mnt_rcu));
1051 }
1052 
1053 static LLIST_HEAD(delayed_mntput_list);
delayed_mntput(struct work_struct * unused)1054 static void delayed_mntput(struct work_struct *unused)
1055 {
1056 	struct llist_node *node = llist_del_all(&delayed_mntput_list);
1057 	struct llist_node *next;
1058 
1059 	for (; node; node = next) {
1060 		next = llist_next(node);
1061 		cleanup_mnt(llist_entry(node, struct mount, mnt_llist));
1062 	}
1063 }
1064 static DECLARE_DELAYED_WORK(delayed_mntput_work, delayed_mntput);
1065 
mntput_no_expire(struct mount * mnt)1066 static void mntput_no_expire(struct mount *mnt)
1067 {
1068 	rcu_read_lock();
1069 	mnt_add_count(mnt, -1);
1070 	if (likely(mnt->mnt_ns)) { /* shouldn't be the last one */
1071 		rcu_read_unlock();
1072 		return;
1073 	}
1074 	lock_mount_hash();
1075 	if (mnt_get_count(mnt)) {
1076 		rcu_read_unlock();
1077 		unlock_mount_hash();
1078 		return;
1079 	}
1080 	if (unlikely(mnt->mnt.mnt_flags & MNT_DOOMED)) {
1081 		rcu_read_unlock();
1082 		unlock_mount_hash();
1083 		return;
1084 	}
1085 	mnt->mnt.mnt_flags |= MNT_DOOMED;
1086 	rcu_read_unlock();
1087 
1088 	list_del(&mnt->mnt_instance);
1089 	unlock_mount_hash();
1090 
1091 	if (likely(!(mnt->mnt.mnt_flags & MNT_INTERNAL))) {
1092 		struct task_struct *task = current;
1093 		if (likely(!(task->flags & PF_KTHREAD))) {
1094 			init_task_work(&mnt->mnt_rcu, __cleanup_mnt);
1095 			if (!task_work_add(task, &mnt->mnt_rcu, true))
1096 				return;
1097 		}
1098 		if (llist_add(&mnt->mnt_llist, &delayed_mntput_list))
1099 			schedule_delayed_work(&delayed_mntput_work, 1);
1100 		return;
1101 	}
1102 	cleanup_mnt(mnt);
1103 }
1104 
mntput(struct vfsmount * mnt)1105 void mntput(struct vfsmount *mnt)
1106 {
1107 	if (mnt) {
1108 		struct mount *m = real_mount(mnt);
1109 		/* avoid cacheline pingpong, hope gcc doesn't get "smart" */
1110 		if (unlikely(m->mnt_expiry_mark))
1111 			m->mnt_expiry_mark = 0;
1112 		mntput_no_expire(m);
1113 	}
1114 }
1115 EXPORT_SYMBOL(mntput);
1116 
mntget(struct vfsmount * mnt)1117 struct vfsmount *mntget(struct vfsmount *mnt)
1118 {
1119 	if (mnt)
1120 		mnt_add_count(real_mount(mnt), 1);
1121 	return mnt;
1122 }
1123 EXPORT_SYMBOL(mntget);
1124 
mnt_clone_internal(struct path * path)1125 struct vfsmount *mnt_clone_internal(struct path *path)
1126 {
1127 	struct mount *p;
1128 	p = clone_mnt(real_mount(path->mnt), path->dentry, CL_PRIVATE);
1129 	if (IS_ERR(p))
1130 		return ERR_CAST(p);
1131 	p->mnt.mnt_flags |= MNT_INTERNAL;
1132 	return &p->mnt;
1133 }
1134 
mangle(struct seq_file * m,const char * s)1135 static inline void mangle(struct seq_file *m, const char *s)
1136 {
1137 	seq_escape(m, s, " \t\n\\");
1138 }
1139 
1140 /*
1141  * Simple .show_options callback for filesystems which don't want to
1142  * implement more complex mount option showing.
1143  *
1144  * See also save_mount_options().
1145  */
generic_show_options(struct seq_file * m,struct dentry * root)1146 int generic_show_options(struct seq_file *m, struct dentry *root)
1147 {
1148 	const char *options;
1149 
1150 	rcu_read_lock();
1151 	options = rcu_dereference(root->d_sb->s_options);
1152 
1153 	if (options != NULL && options[0]) {
1154 		seq_putc(m, ',');
1155 		mangle(m, options);
1156 	}
1157 	rcu_read_unlock();
1158 
1159 	return 0;
1160 }
1161 EXPORT_SYMBOL(generic_show_options);
1162 
1163 /*
1164  * If filesystem uses generic_show_options(), this function should be
1165  * called from the fill_super() callback.
1166  *
1167  * The .remount_fs callback usually needs to be handled in a special
1168  * way, to make sure, that previous options are not overwritten if the
1169  * remount fails.
1170  *
1171  * Also note, that if the filesystem's .remount_fs function doesn't
1172  * reset all options to their default value, but changes only newly
1173  * given options, then the displayed options will not reflect reality
1174  * any more.
1175  */
save_mount_options(struct super_block * sb,char * options)1176 void save_mount_options(struct super_block *sb, char *options)
1177 {
1178 	BUG_ON(sb->s_options);
1179 	rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL));
1180 }
1181 EXPORT_SYMBOL(save_mount_options);
1182 
replace_mount_options(struct super_block * sb,char * options)1183 void replace_mount_options(struct super_block *sb, char *options)
1184 {
1185 	char *old = sb->s_options;
1186 	rcu_assign_pointer(sb->s_options, options);
1187 	if (old) {
1188 		synchronize_rcu();
1189 		kfree(old);
1190 	}
1191 }
1192 EXPORT_SYMBOL(replace_mount_options);
1193 
1194 #ifdef CONFIG_PROC_FS
1195 /* iterator; we want it to have access to namespace_sem, thus here... */
m_start(struct seq_file * m,loff_t * pos)1196 static void *m_start(struct seq_file *m, loff_t *pos)
1197 {
1198 	struct proc_mounts *p = proc_mounts(m);
1199 
1200 	down_read(&namespace_sem);
1201 	if (p->cached_event == p->ns->event) {
1202 		void *v = p->cached_mount;
1203 		if (*pos == p->cached_index)
1204 			return v;
1205 		if (*pos == p->cached_index + 1) {
1206 			v = seq_list_next(v, &p->ns->list, &p->cached_index);
1207 			return p->cached_mount = v;
1208 		}
1209 	}
1210 
1211 	p->cached_event = p->ns->event;
1212 	p->cached_mount = seq_list_start(&p->ns->list, *pos);
1213 	p->cached_index = *pos;
1214 	return p->cached_mount;
1215 }
1216 
m_next(struct seq_file * m,void * v,loff_t * pos)1217 static void *m_next(struct seq_file *m, void *v, loff_t *pos)
1218 {
1219 	struct proc_mounts *p = proc_mounts(m);
1220 
1221 	p->cached_mount = seq_list_next(v, &p->ns->list, pos);
1222 	p->cached_index = *pos;
1223 	return p->cached_mount;
1224 }
1225 
m_stop(struct seq_file * m,void * v)1226 static void m_stop(struct seq_file *m, void *v)
1227 {
1228 	up_read(&namespace_sem);
1229 }
1230 
m_show(struct seq_file * m,void * v)1231 static int m_show(struct seq_file *m, void *v)
1232 {
1233 	struct proc_mounts *p = proc_mounts(m);
1234 	struct mount *r = list_entry(v, struct mount, mnt_list);
1235 	return p->show(m, &r->mnt);
1236 }
1237 
1238 const struct seq_operations mounts_op = {
1239 	.start	= m_start,
1240 	.next	= m_next,
1241 	.stop	= m_stop,
1242 	.show	= m_show,
1243 };
1244 #endif  /* CONFIG_PROC_FS */
1245 
1246 /**
1247  * may_umount_tree - check if a mount tree is busy
1248  * @mnt: root of mount tree
1249  *
1250  * This is called to check if a tree of mounts has any
1251  * open files, pwds, chroots or sub mounts that are
1252  * busy.
1253  */
may_umount_tree(struct vfsmount * m)1254 int may_umount_tree(struct vfsmount *m)
1255 {
1256 	struct mount *mnt = real_mount(m);
1257 	int actual_refs = 0;
1258 	int minimum_refs = 0;
1259 	struct mount *p;
1260 	BUG_ON(!m);
1261 
1262 	/* write lock needed for mnt_get_count */
1263 	lock_mount_hash();
1264 	for (p = mnt; p; p = next_mnt(p, mnt)) {
1265 		actual_refs += mnt_get_count(p);
1266 		minimum_refs += 2;
1267 	}
1268 	unlock_mount_hash();
1269 
1270 	if (actual_refs > minimum_refs)
1271 		return 0;
1272 
1273 	return 1;
1274 }
1275 
1276 EXPORT_SYMBOL(may_umount_tree);
1277 
1278 /**
1279  * may_umount - check if a mount point is busy
1280  * @mnt: root of mount
1281  *
1282  * This is called to check if a mount point has any
1283  * open files, pwds, chroots or sub mounts. If the
1284  * mount has sub mounts this will return busy
1285  * regardless of whether the sub mounts are busy.
1286  *
1287  * Doesn't take quota and stuff into account. IOW, in some cases it will
1288  * give false negatives. The main reason why it's here is that we need
1289  * a non-destructive way to look for easily umountable filesystems.
1290  */
may_umount(struct vfsmount * mnt)1291 int may_umount(struct vfsmount *mnt)
1292 {
1293 	int ret = 1;
1294 	down_read(&namespace_sem);
1295 	lock_mount_hash();
1296 	if (propagate_mount_busy(real_mount(mnt), 2))
1297 		ret = 0;
1298 	unlock_mount_hash();
1299 	up_read(&namespace_sem);
1300 	return ret;
1301 }
1302 
1303 EXPORT_SYMBOL(may_umount);
1304 
1305 static HLIST_HEAD(unmounted);	/* protected by namespace_sem */
1306 
namespace_unlock(void)1307 static void namespace_unlock(void)
1308 {
1309 	struct mount *mnt;
1310 	struct hlist_head head = unmounted;
1311 
1312 	if (likely(hlist_empty(&head))) {
1313 		up_write(&namespace_sem);
1314 		return;
1315 	}
1316 
1317 	head.first->pprev = &head.first;
1318 	INIT_HLIST_HEAD(&unmounted);
1319 
1320 	/* undo decrements we'd done in umount_tree() */
1321 	hlist_for_each_entry(mnt, &head, mnt_hash)
1322 		if (mnt->mnt_ex_mountpoint.mnt)
1323 			mntget(mnt->mnt_ex_mountpoint.mnt);
1324 
1325 	up_write(&namespace_sem);
1326 
1327 	synchronize_rcu();
1328 
1329 	while (!hlist_empty(&head)) {
1330 		mnt = hlist_entry(head.first, struct mount, mnt_hash);
1331 		hlist_del_init(&mnt->mnt_hash);
1332 		if (mnt->mnt_ex_mountpoint.mnt)
1333 			path_put(&mnt->mnt_ex_mountpoint);
1334 		mntput(&mnt->mnt);
1335 	}
1336 }
1337 
namespace_lock(void)1338 static inline void namespace_lock(void)
1339 {
1340 	down_write(&namespace_sem);
1341 }
1342 
1343 enum umount_tree_flags {
1344 	UMOUNT_SYNC = 1,
1345 	UMOUNT_PROPAGATE = 2,
1346 };
1347 /*
1348  * mount_lock must be held
1349  * namespace_sem must be held for write
1350  */
umount_tree(struct mount * mnt,enum umount_tree_flags how)1351 static void umount_tree(struct mount *mnt, enum umount_tree_flags how)
1352 {
1353 	HLIST_HEAD(tmp_list);
1354 	struct mount *p;
1355 	struct mount *last = NULL;
1356 
1357 	for (p = mnt; p; p = next_mnt(p, mnt)) {
1358 		hlist_del_init_rcu(&p->mnt_hash);
1359 		hlist_add_head(&p->mnt_hash, &tmp_list);
1360 	}
1361 
1362 	hlist_for_each_entry(p, &tmp_list, mnt_hash)
1363 		list_del_init(&p->mnt_child);
1364 
1365 	if (how & UMOUNT_PROPAGATE)
1366 		propagate_umount(&tmp_list);
1367 
1368 	hlist_for_each_entry(p, &tmp_list, mnt_hash) {
1369 		list_del_init(&p->mnt_expire);
1370 		list_del_init(&p->mnt_list);
1371 		__touch_mnt_namespace(p->mnt_ns);
1372 		p->mnt_ns = NULL;
1373 		if (how & UMOUNT_SYNC)
1374 			p->mnt.mnt_flags |= MNT_SYNC_UMOUNT;
1375 		if (mnt_has_parent(p)) {
1376 			hlist_del_init(&p->mnt_mp_list);
1377 			put_mountpoint(p->mnt_mp);
1378 			mnt_add_count(p->mnt_parent, -1);
1379 			/* move the reference to mountpoint into ->mnt_ex_mountpoint */
1380 			p->mnt_ex_mountpoint.dentry = p->mnt_mountpoint;
1381 			p->mnt_ex_mountpoint.mnt = &p->mnt_parent->mnt;
1382 			p->mnt_mountpoint = p->mnt.mnt_root;
1383 			p->mnt_parent = p;
1384 			p->mnt_mp = NULL;
1385 		}
1386 		change_mnt_propagation(p, MS_PRIVATE);
1387 		last = p;
1388 	}
1389 	if (last) {
1390 		last->mnt_hash.next = unmounted.first;
1391 		if (unmounted.first)
1392 			unmounted.first->pprev = &last->mnt_hash.next;
1393 		unmounted.first = tmp_list.first;
1394 		unmounted.first->pprev = &unmounted.first;
1395 	}
1396 }
1397 
1398 static void shrink_submounts(struct mount *mnt);
1399 
do_umount(struct mount * mnt,int flags)1400 static int do_umount(struct mount *mnt, int flags)
1401 {
1402 	struct super_block *sb = mnt->mnt.mnt_sb;
1403 	int retval;
1404 
1405 	retval = security_sb_umount(&mnt->mnt, flags);
1406 	if (retval)
1407 		return retval;
1408 
1409 	/*
1410 	 * Allow userspace to request a mountpoint be expired rather than
1411 	 * unmounting unconditionally. Unmount only happens if:
1412 	 *  (1) the mark is already set (the mark is cleared by mntput())
1413 	 *  (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
1414 	 */
1415 	if (flags & MNT_EXPIRE) {
1416 		if (&mnt->mnt == current->fs->root.mnt ||
1417 		    flags & (MNT_FORCE | MNT_DETACH))
1418 			return -EINVAL;
1419 
1420 		/*
1421 		 * probably don't strictly need the lock here if we examined
1422 		 * all race cases, but it's a slowpath.
1423 		 */
1424 		lock_mount_hash();
1425 		if (mnt_get_count(mnt) != 2) {
1426 			unlock_mount_hash();
1427 			return -EBUSY;
1428 		}
1429 		unlock_mount_hash();
1430 
1431 		if (!xchg(&mnt->mnt_expiry_mark, 1))
1432 			return -EAGAIN;
1433 	}
1434 
1435 	/*
1436 	 * If we may have to abort operations to get out of this
1437 	 * mount, and they will themselves hold resources we must
1438 	 * allow the fs to do things. In the Unix tradition of
1439 	 * 'Gee thats tricky lets do it in userspace' the umount_begin
1440 	 * might fail to complete on the first run through as other tasks
1441 	 * must return, and the like. Thats for the mount program to worry
1442 	 * about for the moment.
1443 	 */
1444 
1445 	if (flags & MNT_FORCE && sb->s_op->umount_begin) {
1446 		sb->s_op->umount_begin(sb);
1447 	}
1448 
1449 	/*
1450 	 * No sense to grab the lock for this test, but test itself looks
1451 	 * somewhat bogus. Suggestions for better replacement?
1452 	 * Ho-hum... In principle, we might treat that as umount + switch
1453 	 * to rootfs. GC would eventually take care of the old vfsmount.
1454 	 * Actually it makes sense, especially if rootfs would contain a
1455 	 * /reboot - static binary that would close all descriptors and
1456 	 * call reboot(9). Then init(8) could umount root and exec /reboot.
1457 	 */
1458 	if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
1459 		/*
1460 		 * Special case for "unmounting" root ...
1461 		 * we just try to remount it readonly.
1462 		 */
1463 		if (!capable(CAP_SYS_ADMIN))
1464 			return -EPERM;
1465 		down_write(&sb->s_umount);
1466 		if (!(sb->s_flags & MS_RDONLY))
1467 			retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
1468 		up_write(&sb->s_umount);
1469 		return retval;
1470 	}
1471 
1472 	namespace_lock();
1473 	lock_mount_hash();
1474 	event++;
1475 
1476 	if (flags & MNT_DETACH) {
1477 		if (!list_empty(&mnt->mnt_list))
1478 			umount_tree(mnt, UMOUNT_PROPAGATE);
1479 		retval = 0;
1480 	} else {
1481 		shrink_submounts(mnt);
1482 		retval = -EBUSY;
1483 		if (!propagate_mount_busy(mnt, 2)) {
1484 			if (!list_empty(&mnt->mnt_list))
1485 				umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
1486 			retval = 0;
1487 		}
1488 	}
1489 	unlock_mount_hash();
1490 	namespace_unlock();
1491 	return retval;
1492 }
1493 
1494 /*
1495  * __detach_mounts - lazily unmount all mounts on the specified dentry
1496  *
1497  * During unlink, rmdir, and d_drop it is possible to loose the path
1498  * to an existing mountpoint, and wind up leaking the mount.
1499  * detach_mounts allows lazily unmounting those mounts instead of
1500  * leaking them.
1501  *
1502  * The caller may hold dentry->d_inode->i_mutex.
1503  */
__detach_mounts(struct dentry * dentry)1504 void __detach_mounts(struct dentry *dentry)
1505 {
1506 	struct mountpoint *mp;
1507 	struct mount *mnt;
1508 
1509 	namespace_lock();
1510 	mp = lookup_mountpoint(dentry);
1511 	if (IS_ERR_OR_NULL(mp))
1512 		goto out_unlock;
1513 
1514 	lock_mount_hash();
1515 	event++;
1516 	while (!hlist_empty(&mp->m_list)) {
1517 		mnt = hlist_entry(mp->m_list.first, struct mount, mnt_mp_list);
1518 		umount_tree(mnt, 0);
1519 	}
1520 	unlock_mount_hash();
1521 	put_mountpoint(mp);
1522 out_unlock:
1523 	namespace_unlock();
1524 }
1525 
1526 /*
1527  * Is the caller allowed to modify his namespace?
1528  */
may_mount(void)1529 static inline bool may_mount(void)
1530 {
1531 	return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN);
1532 }
1533 
1534 /*
1535  * Now umount can handle mount points as well as block devices.
1536  * This is important for filesystems which use unnamed block devices.
1537  *
1538  * We now support a flag for forced unmount like the other 'big iron'
1539  * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
1540  */
1541 
SYSCALL_DEFINE2(umount,char __user *,name,int,flags)1542 SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
1543 {
1544 	struct path path;
1545 	struct mount *mnt;
1546 	int retval;
1547 	int lookup_flags = 0;
1548 
1549 	if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
1550 		return -EINVAL;
1551 
1552 	if (!may_mount())
1553 		return -EPERM;
1554 
1555 	if (!(flags & UMOUNT_NOFOLLOW))
1556 		lookup_flags |= LOOKUP_FOLLOW;
1557 
1558 	retval = user_path_mountpoint_at(AT_FDCWD, name, lookup_flags, &path);
1559 	if (retval)
1560 		goto out;
1561 	mnt = real_mount(path.mnt);
1562 	retval = -EINVAL;
1563 	if (path.dentry != path.mnt->mnt_root)
1564 		goto dput_and_out;
1565 	if (!check_mnt(mnt))
1566 		goto dput_and_out;
1567 	if (mnt->mnt.mnt_flags & MNT_LOCKED)
1568 		goto dput_and_out;
1569 	retval = -EPERM;
1570 	if (flags & MNT_FORCE && !capable(CAP_SYS_ADMIN))
1571 		goto dput_and_out;
1572 
1573 	retval = do_umount(mnt, flags);
1574 dput_and_out:
1575 	/* we mustn't call path_put() as that would clear mnt_expiry_mark */
1576 	dput(path.dentry);
1577 	mntput_no_expire(mnt);
1578 out:
1579 	return retval;
1580 }
1581 
1582 #ifdef __ARCH_WANT_SYS_OLDUMOUNT
1583 
1584 /*
1585  *	The 2.0 compatible umount. No flags.
1586  */
SYSCALL_DEFINE1(oldumount,char __user *,name)1587 SYSCALL_DEFINE1(oldumount, char __user *, name)
1588 {
1589 	return sys_umount(name, 0);
1590 }
1591 
1592 #endif
1593 
is_mnt_ns_file(struct dentry * dentry)1594 static bool is_mnt_ns_file(struct dentry *dentry)
1595 {
1596 	/* Is this a proxy for a mount namespace? */
1597 	struct inode *inode = dentry->d_inode;
1598 	struct proc_ns *ei;
1599 
1600 	if (!proc_ns_inode(inode))
1601 		return false;
1602 
1603 	ei = get_proc_ns(inode);
1604 	if (ei->ns_ops != &mntns_operations)
1605 		return false;
1606 
1607 	return true;
1608 }
1609 
mnt_ns_loop(struct dentry * dentry)1610 static bool mnt_ns_loop(struct dentry *dentry)
1611 {
1612 	/* Could bind mounting the mount namespace inode cause a
1613 	 * mount namespace loop?
1614 	 */
1615 	struct mnt_namespace *mnt_ns;
1616 	if (!is_mnt_ns_file(dentry))
1617 		return false;
1618 
1619 	mnt_ns = get_proc_ns(dentry->d_inode)->ns;
1620 	return current->nsproxy->mnt_ns->seq >= mnt_ns->seq;
1621 }
1622 
copy_tree(struct mount * mnt,struct dentry * dentry,int flag)1623 struct mount *copy_tree(struct mount *mnt, struct dentry *dentry,
1624 					int flag)
1625 {
1626 	struct mount *res, *p, *q, *r, *parent;
1627 
1628 	if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt))
1629 		return ERR_PTR(-EINVAL);
1630 
1631 	if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry))
1632 		return ERR_PTR(-EINVAL);
1633 
1634 	res = q = clone_mnt(mnt, dentry, flag);
1635 	if (IS_ERR(q))
1636 		return q;
1637 
1638 	q->mnt.mnt_flags &= ~MNT_LOCKED;
1639 	q->mnt_mountpoint = mnt->mnt_mountpoint;
1640 
1641 	p = mnt;
1642 	list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
1643 		struct mount *s;
1644 		if (!is_subdir(r->mnt_mountpoint, dentry))
1645 			continue;
1646 
1647 		for (s = r; s; s = next_mnt(s, r)) {
1648 			struct mount *t = NULL;
1649 			if (!(flag & CL_COPY_UNBINDABLE) &&
1650 			    IS_MNT_UNBINDABLE(s)) {
1651 				s = skip_mnt_tree(s);
1652 				continue;
1653 			}
1654 			if (!(flag & CL_COPY_MNT_NS_FILE) &&
1655 			    is_mnt_ns_file(s->mnt.mnt_root)) {
1656 				s = skip_mnt_tree(s);
1657 				continue;
1658 			}
1659 			while (p != s->mnt_parent) {
1660 				p = p->mnt_parent;
1661 				q = q->mnt_parent;
1662 			}
1663 			p = s;
1664 			parent = q;
1665 			q = clone_mnt(p, p->mnt.mnt_root, flag);
1666 			if (IS_ERR(q))
1667 				goto out;
1668 			lock_mount_hash();
1669 			list_add_tail(&q->mnt_list, &res->mnt_list);
1670 			mnt_set_mountpoint(parent, p->mnt_mp, q);
1671 			if (!list_empty(&parent->mnt_mounts)) {
1672 				t = list_last_entry(&parent->mnt_mounts,
1673 					struct mount, mnt_child);
1674 				if (t->mnt_mp != p->mnt_mp)
1675 					t = NULL;
1676 			}
1677 			attach_shadowed(q, parent, t);
1678 			unlock_mount_hash();
1679 		}
1680 	}
1681 	return res;
1682 out:
1683 	if (res) {
1684 		lock_mount_hash();
1685 		umount_tree(res, UMOUNT_SYNC);
1686 		unlock_mount_hash();
1687 	}
1688 	return q;
1689 }
1690 
1691 /* Caller should check returned pointer for errors */
1692 
collect_mounts(struct path * path)1693 struct vfsmount *collect_mounts(struct path *path)
1694 {
1695 	struct mount *tree;
1696 	namespace_lock();
1697 	if (!check_mnt(real_mount(path->mnt)))
1698 		tree = ERR_PTR(-EINVAL);
1699 	else
1700 		tree = copy_tree(real_mount(path->mnt), path->dentry,
1701 				 CL_COPY_ALL | CL_PRIVATE);
1702 	namespace_unlock();
1703 	if (IS_ERR(tree))
1704 		return ERR_CAST(tree);
1705 	return &tree->mnt;
1706 }
1707 
drop_collected_mounts(struct vfsmount * mnt)1708 void drop_collected_mounts(struct vfsmount *mnt)
1709 {
1710 	namespace_lock();
1711 	lock_mount_hash();
1712 	umount_tree(real_mount(mnt), UMOUNT_SYNC);
1713 	unlock_mount_hash();
1714 	namespace_unlock();
1715 }
1716 
1717 /**
1718  * clone_private_mount - create a private clone of a path
1719  *
1720  * This creates a new vfsmount, which will be the clone of @path.  The new will
1721  * not be attached anywhere in the namespace and will be private (i.e. changes
1722  * to the originating mount won't be propagated into this).
1723  *
1724  * Release with mntput().
1725  */
clone_private_mount(struct path * path)1726 struct vfsmount *clone_private_mount(struct path *path)
1727 {
1728 	struct mount *old_mnt = real_mount(path->mnt);
1729 	struct mount *new_mnt;
1730 
1731 	if (IS_MNT_UNBINDABLE(old_mnt))
1732 		return ERR_PTR(-EINVAL);
1733 
1734 	down_read(&namespace_sem);
1735 	new_mnt = clone_mnt(old_mnt, path->dentry, CL_PRIVATE);
1736 	up_read(&namespace_sem);
1737 	if (IS_ERR(new_mnt))
1738 		return ERR_CAST(new_mnt);
1739 
1740 	return &new_mnt->mnt;
1741 }
1742 EXPORT_SYMBOL_GPL(clone_private_mount);
1743 
iterate_mounts(int (* f)(struct vfsmount *,void *),void * arg,struct vfsmount * root)1744 int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
1745 		   struct vfsmount *root)
1746 {
1747 	struct mount *mnt;
1748 	int res = f(root, arg);
1749 	if (res)
1750 		return res;
1751 	list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) {
1752 		res = f(&mnt->mnt, arg);
1753 		if (res)
1754 			return res;
1755 	}
1756 	return 0;
1757 }
1758 
cleanup_group_ids(struct mount * mnt,struct mount * end)1759 static void cleanup_group_ids(struct mount *mnt, struct mount *end)
1760 {
1761 	struct mount *p;
1762 
1763 	for (p = mnt; p != end; p = next_mnt(p, mnt)) {
1764 		if (p->mnt_group_id && !IS_MNT_SHARED(p))
1765 			mnt_release_group_id(p);
1766 	}
1767 }
1768 
invent_group_ids(struct mount * mnt,bool recurse)1769 static int invent_group_ids(struct mount *mnt, bool recurse)
1770 {
1771 	struct mount *p;
1772 
1773 	for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
1774 		if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
1775 			int err = mnt_alloc_group_id(p);
1776 			if (err) {
1777 				cleanup_group_ids(mnt, p);
1778 				return err;
1779 			}
1780 		}
1781 	}
1782 
1783 	return 0;
1784 }
1785 
1786 /*
1787  *  @source_mnt : mount tree to be attached
1788  *  @nd         : place the mount tree @source_mnt is attached
1789  *  @parent_nd  : if non-null, detach the source_mnt from its parent and
1790  *  		   store the parent mount and mountpoint dentry.
1791  *  		   (done when source_mnt is moved)
1792  *
1793  *  NOTE: in the table below explains the semantics when a source mount
1794  *  of a given type is attached to a destination mount of a given type.
1795  * ---------------------------------------------------------------------------
1796  * |         BIND MOUNT OPERATION                                            |
1797  * |**************************************************************************
1798  * | source-->| shared        |       private  |       slave    | unbindable |
1799  * | dest     |               |                |                |            |
1800  * |   |      |               |                |                |            |
1801  * |   v      |               |                |                |            |
1802  * |**************************************************************************
1803  * |  shared  | shared (++)   |     shared (+) |     shared(+++)|  invalid   |
1804  * |          |               |                |                |            |
1805  * |non-shared| shared (+)    |      private   |      slave (*) |  invalid   |
1806  * ***************************************************************************
1807  * A bind operation clones the source mount and mounts the clone on the
1808  * destination mount.
1809  *
1810  * (++)  the cloned mount is propagated to all the mounts in the propagation
1811  * 	 tree of the destination mount and the cloned mount is added to
1812  * 	 the peer group of the source mount.
1813  * (+)   the cloned mount is created under the destination mount and is marked
1814  *       as shared. The cloned mount is added to the peer group of the source
1815  *       mount.
1816  * (+++) the mount is propagated to all the mounts in the propagation tree
1817  *       of the destination mount and the cloned mount is made slave
1818  *       of the same master as that of the source mount. The cloned mount
1819  *       is marked as 'shared and slave'.
1820  * (*)   the cloned mount is made a slave of the same master as that of the
1821  * 	 source mount.
1822  *
1823  * ---------------------------------------------------------------------------
1824  * |         		MOVE MOUNT OPERATION                                 |
1825  * |**************************************************************************
1826  * | source-->| shared        |       private  |       slave    | unbindable |
1827  * | dest     |               |                |                |            |
1828  * |   |      |               |                |                |            |
1829  * |   v      |               |                |                |            |
1830  * |**************************************************************************
1831  * |  shared  | shared (+)    |     shared (+) |    shared(+++) |  invalid   |
1832  * |          |               |                |                |            |
1833  * |non-shared| shared (+*)   |      private   |    slave (*)   | unbindable |
1834  * ***************************************************************************
1835  *
1836  * (+)  the mount is moved to the destination. And is then propagated to
1837  * 	all the mounts in the propagation tree of the destination mount.
1838  * (+*)  the mount is moved to the destination.
1839  * (+++)  the mount is moved to the destination and is then propagated to
1840  * 	all the mounts belonging to the destination mount's propagation tree.
1841  * 	the mount is marked as 'shared and slave'.
1842  * (*)	the mount continues to be a slave at the new location.
1843  *
1844  * if the source mount is a tree, the operations explained above is
1845  * applied to each mount in the tree.
1846  * Must be called without spinlocks held, since this function can sleep
1847  * in allocations.
1848  */
attach_recursive_mnt(struct mount * source_mnt,struct mount * dest_mnt,struct mountpoint * dest_mp,struct path * parent_path)1849 static int attach_recursive_mnt(struct mount *source_mnt,
1850 			struct mount *dest_mnt,
1851 			struct mountpoint *dest_mp,
1852 			struct path *parent_path)
1853 {
1854 	HLIST_HEAD(tree_list);
1855 	struct mount *child, *p;
1856 	struct hlist_node *n;
1857 	int err;
1858 
1859 	if (IS_MNT_SHARED(dest_mnt)) {
1860 		err = invent_group_ids(source_mnt, true);
1861 		if (err)
1862 			goto out;
1863 		err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list);
1864 		lock_mount_hash();
1865 		if (err)
1866 			goto out_cleanup_ids;
1867 		for (p = source_mnt; p; p = next_mnt(p, source_mnt))
1868 			set_mnt_shared(p);
1869 	} else {
1870 		lock_mount_hash();
1871 	}
1872 	if (parent_path) {
1873 		detach_mnt(source_mnt, parent_path);
1874 		attach_mnt(source_mnt, dest_mnt, dest_mp);
1875 		touch_mnt_namespace(source_mnt->mnt_ns);
1876 	} else {
1877 		mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt);
1878 		commit_tree(source_mnt, NULL);
1879 	}
1880 
1881 	hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) {
1882 		struct mount *q;
1883 		hlist_del_init(&child->mnt_hash);
1884 		q = __lookup_mnt_last(&child->mnt_parent->mnt,
1885 				      child->mnt_mountpoint);
1886 		commit_tree(child, q);
1887 	}
1888 	unlock_mount_hash();
1889 
1890 	return 0;
1891 
1892  out_cleanup_ids:
1893 	while (!hlist_empty(&tree_list)) {
1894 		child = hlist_entry(tree_list.first, struct mount, mnt_hash);
1895 		umount_tree(child, UMOUNT_SYNC);
1896 	}
1897 	unlock_mount_hash();
1898 	cleanup_group_ids(source_mnt, NULL);
1899  out:
1900 	return err;
1901 }
1902 
lock_mount(struct path * path)1903 static struct mountpoint *lock_mount(struct path *path)
1904 {
1905 	struct vfsmount *mnt;
1906 	struct dentry *dentry = path->dentry;
1907 retry:
1908 	mutex_lock(&dentry->d_inode->i_mutex);
1909 	if (unlikely(cant_mount(dentry))) {
1910 		mutex_unlock(&dentry->d_inode->i_mutex);
1911 		return ERR_PTR(-ENOENT);
1912 	}
1913 	namespace_lock();
1914 	mnt = lookup_mnt(path);
1915 	if (likely(!mnt)) {
1916 		struct mountpoint *mp = lookup_mountpoint(dentry);
1917 		if (!mp)
1918 			mp = new_mountpoint(dentry);
1919 		if (IS_ERR(mp)) {
1920 			namespace_unlock();
1921 			mutex_unlock(&dentry->d_inode->i_mutex);
1922 			return mp;
1923 		}
1924 		return mp;
1925 	}
1926 	namespace_unlock();
1927 	mutex_unlock(&path->dentry->d_inode->i_mutex);
1928 	path_put(path);
1929 	path->mnt = mnt;
1930 	dentry = path->dentry = dget(mnt->mnt_root);
1931 	goto retry;
1932 }
1933 
unlock_mount(struct mountpoint * where)1934 static void unlock_mount(struct mountpoint *where)
1935 {
1936 	struct dentry *dentry = where->m_dentry;
1937 	put_mountpoint(where);
1938 	namespace_unlock();
1939 	mutex_unlock(&dentry->d_inode->i_mutex);
1940 }
1941 
graft_tree(struct mount * mnt,struct mount * p,struct mountpoint * mp)1942 static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp)
1943 {
1944 	if (mnt->mnt.mnt_sb->s_flags & MS_NOUSER)
1945 		return -EINVAL;
1946 
1947 	if (S_ISDIR(mp->m_dentry->d_inode->i_mode) !=
1948 	      S_ISDIR(mnt->mnt.mnt_root->d_inode->i_mode))
1949 		return -ENOTDIR;
1950 
1951 	return attach_recursive_mnt(mnt, p, mp, NULL);
1952 }
1953 
1954 /*
1955  * Sanity check the flags to change_mnt_propagation.
1956  */
1957 
flags_to_propagation_type(int flags)1958 static int flags_to_propagation_type(int flags)
1959 {
1960 	int type = flags & ~(MS_REC | MS_SILENT);
1961 
1962 	/* Fail if any non-propagation flags are set */
1963 	if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
1964 		return 0;
1965 	/* Only one propagation flag should be set */
1966 	if (!is_power_of_2(type))
1967 		return 0;
1968 	return type;
1969 }
1970 
1971 /*
1972  * recursively change the type of the mountpoint.
1973  */
do_change_type(struct path * path,int flag)1974 static int do_change_type(struct path *path, int flag)
1975 {
1976 	struct mount *m;
1977 	struct mount *mnt = real_mount(path->mnt);
1978 	int recurse = flag & MS_REC;
1979 	int type;
1980 	int err = 0;
1981 
1982 	if (path->dentry != path->mnt->mnt_root)
1983 		return -EINVAL;
1984 
1985 	type = flags_to_propagation_type(flag);
1986 	if (!type)
1987 		return -EINVAL;
1988 
1989 	namespace_lock();
1990 	if (type == MS_SHARED) {
1991 		err = invent_group_ids(mnt, recurse);
1992 		if (err)
1993 			goto out_unlock;
1994 	}
1995 
1996 	lock_mount_hash();
1997 	for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
1998 		change_mnt_propagation(m, type);
1999 	unlock_mount_hash();
2000 
2001  out_unlock:
2002 	namespace_unlock();
2003 	return err;
2004 }
2005 
has_locked_children(struct mount * mnt,struct dentry * dentry)2006 static bool has_locked_children(struct mount *mnt, struct dentry *dentry)
2007 {
2008 	struct mount *child;
2009 	list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
2010 		if (!is_subdir(child->mnt_mountpoint, dentry))
2011 			continue;
2012 
2013 		if (child->mnt.mnt_flags & MNT_LOCKED)
2014 			return true;
2015 	}
2016 	return false;
2017 }
2018 
2019 /*
2020  * do loopback mount.
2021  */
do_loopback(struct path * path,const char * old_name,int recurse)2022 static int do_loopback(struct path *path, const char *old_name,
2023 				int recurse)
2024 {
2025 	struct path old_path;
2026 	struct mount *mnt = NULL, *old, *parent;
2027 	struct mountpoint *mp;
2028 	int err;
2029 	if (!old_name || !*old_name)
2030 		return -EINVAL;
2031 	err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
2032 	if (err)
2033 		return err;
2034 
2035 	err = -EINVAL;
2036 	if (mnt_ns_loop(old_path.dentry))
2037 		goto out;
2038 
2039 	mp = lock_mount(path);
2040 	err = PTR_ERR(mp);
2041 	if (IS_ERR(mp))
2042 		goto out;
2043 
2044 	old = real_mount(old_path.mnt);
2045 	parent = real_mount(path->mnt);
2046 
2047 	err = -EINVAL;
2048 	if (IS_MNT_UNBINDABLE(old))
2049 		goto out2;
2050 
2051 	if (!check_mnt(parent) || !check_mnt(old))
2052 		goto out2;
2053 
2054 	if (!recurse && has_locked_children(old, old_path.dentry))
2055 		goto out2;
2056 
2057 	if (recurse)
2058 		mnt = copy_tree(old, old_path.dentry, CL_COPY_MNT_NS_FILE);
2059 	else
2060 		mnt = clone_mnt(old, old_path.dentry, 0);
2061 
2062 	if (IS_ERR(mnt)) {
2063 		err = PTR_ERR(mnt);
2064 		goto out2;
2065 	}
2066 
2067 	mnt->mnt.mnt_flags &= ~MNT_LOCKED;
2068 
2069 	err = graft_tree(mnt, parent, mp);
2070 	if (err) {
2071 		lock_mount_hash();
2072 		umount_tree(mnt, UMOUNT_SYNC);
2073 		unlock_mount_hash();
2074 	}
2075 out2:
2076 	unlock_mount(mp);
2077 out:
2078 	path_put(&old_path);
2079 	return err;
2080 }
2081 
change_mount_flags(struct vfsmount * mnt,int ms_flags)2082 static int change_mount_flags(struct vfsmount *mnt, int ms_flags)
2083 {
2084 	int error = 0;
2085 	int readonly_request = 0;
2086 
2087 	if (ms_flags & MS_RDONLY)
2088 		readonly_request = 1;
2089 	if (readonly_request == __mnt_is_readonly(mnt))
2090 		return 0;
2091 
2092 	if (readonly_request)
2093 		error = mnt_make_readonly(real_mount(mnt));
2094 	else
2095 		__mnt_unmake_readonly(real_mount(mnt));
2096 	return error;
2097 }
2098 
2099 /*
2100  * change filesystem flags. dir should be a physical root of filesystem.
2101  * If you've mounted a non-root directory somewhere and want to do remount
2102  * on it - tough luck.
2103  */
do_remount(struct path * path,int flags,int mnt_flags,void * data)2104 static int do_remount(struct path *path, int flags, int mnt_flags,
2105 		      void *data)
2106 {
2107 	int err;
2108 	struct super_block *sb = path->mnt->mnt_sb;
2109 	struct mount *mnt = real_mount(path->mnt);
2110 
2111 	if (!check_mnt(mnt))
2112 		return -EINVAL;
2113 
2114 	if (path->dentry != path->mnt->mnt_root)
2115 		return -EINVAL;
2116 
2117 	/* Don't allow changing of locked mnt flags.
2118 	 *
2119 	 * No locks need to be held here while testing the various
2120 	 * MNT_LOCK flags because those flags can never be cleared
2121 	 * once they are set.
2122 	 */
2123 	if ((mnt->mnt.mnt_flags & MNT_LOCK_READONLY) &&
2124 	    !(mnt_flags & MNT_READONLY)) {
2125 		return -EPERM;
2126 	}
2127 	if ((mnt->mnt.mnt_flags & MNT_LOCK_NODEV) &&
2128 	    !(mnt_flags & MNT_NODEV)) {
2129 		/* Was the nodev implicitly added in mount? */
2130 		if ((mnt->mnt_ns->user_ns != &init_user_ns) &&
2131 		    !(sb->s_type->fs_flags & FS_USERNS_DEV_MOUNT)) {
2132 			mnt_flags |= MNT_NODEV;
2133 		} else {
2134 			return -EPERM;
2135 		}
2136 	}
2137 	if ((mnt->mnt.mnt_flags & MNT_LOCK_NOSUID) &&
2138 	    !(mnt_flags & MNT_NOSUID)) {
2139 		return -EPERM;
2140 	}
2141 	if ((mnt->mnt.mnt_flags & MNT_LOCK_NOEXEC) &&
2142 	    !(mnt_flags & MNT_NOEXEC)) {
2143 		return -EPERM;
2144 	}
2145 	if ((mnt->mnt.mnt_flags & MNT_LOCK_ATIME) &&
2146 	    ((mnt->mnt.mnt_flags & MNT_ATIME_MASK) != (mnt_flags & MNT_ATIME_MASK))) {
2147 		return -EPERM;
2148 	}
2149 
2150 	err = security_sb_remount(sb, data);
2151 	if (err)
2152 		return err;
2153 
2154 	down_write(&sb->s_umount);
2155 	if (flags & MS_BIND)
2156 		err = change_mount_flags(path->mnt, flags);
2157 	else if (!capable(CAP_SYS_ADMIN))
2158 		err = -EPERM;
2159 	else {
2160 		err = do_remount_sb2(path->mnt, sb, flags, data, 0);
2161 		namespace_lock();
2162 		lock_mount_hash();
2163 		propagate_remount(mnt);
2164 		unlock_mount_hash();
2165 		namespace_unlock();
2166 	}
2167 	if (!err) {
2168 		lock_mount_hash();
2169 		mnt_flags |= mnt->mnt.mnt_flags & ~MNT_USER_SETTABLE_MASK;
2170 		mnt->mnt.mnt_flags = mnt_flags;
2171 		touch_mnt_namespace(mnt->mnt_ns);
2172 		unlock_mount_hash();
2173 	}
2174 	up_write(&sb->s_umount);
2175 	return err;
2176 }
2177 
tree_contains_unbindable(struct mount * mnt)2178 static inline int tree_contains_unbindable(struct mount *mnt)
2179 {
2180 	struct mount *p;
2181 	for (p = mnt; p; p = next_mnt(p, mnt)) {
2182 		if (IS_MNT_UNBINDABLE(p))
2183 			return 1;
2184 	}
2185 	return 0;
2186 }
2187 
do_move_mount(struct path * path,const char * old_name)2188 static int do_move_mount(struct path *path, const char *old_name)
2189 {
2190 	struct path old_path, parent_path;
2191 	struct mount *p;
2192 	struct mount *old;
2193 	struct mountpoint *mp;
2194 	int err;
2195 	if (!old_name || !*old_name)
2196 		return -EINVAL;
2197 	err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
2198 	if (err)
2199 		return err;
2200 
2201 	mp = lock_mount(path);
2202 	err = PTR_ERR(mp);
2203 	if (IS_ERR(mp))
2204 		goto out;
2205 
2206 	old = real_mount(old_path.mnt);
2207 	p = real_mount(path->mnt);
2208 
2209 	err = -EINVAL;
2210 	if (!check_mnt(p) || !check_mnt(old))
2211 		goto out1;
2212 
2213 	if (old->mnt.mnt_flags & MNT_LOCKED)
2214 		goto out1;
2215 
2216 	err = -EINVAL;
2217 	if (old_path.dentry != old_path.mnt->mnt_root)
2218 		goto out1;
2219 
2220 	if (!mnt_has_parent(old))
2221 		goto out1;
2222 
2223 	if (S_ISDIR(path->dentry->d_inode->i_mode) !=
2224 	      S_ISDIR(old_path.dentry->d_inode->i_mode))
2225 		goto out1;
2226 	/*
2227 	 * Don't move a mount residing in a shared parent.
2228 	 */
2229 	if (IS_MNT_SHARED(old->mnt_parent))
2230 		goto out1;
2231 	/*
2232 	 * Don't move a mount tree containing unbindable mounts to a destination
2233 	 * mount which is shared.
2234 	 */
2235 	if (IS_MNT_SHARED(p) && tree_contains_unbindable(old))
2236 		goto out1;
2237 	err = -ELOOP;
2238 	for (; mnt_has_parent(p); p = p->mnt_parent)
2239 		if (p == old)
2240 			goto out1;
2241 
2242 	err = attach_recursive_mnt(old, real_mount(path->mnt), mp, &parent_path);
2243 	if (err)
2244 		goto out1;
2245 
2246 	/* if the mount is moved, it should no longer be expire
2247 	 * automatically */
2248 	list_del_init(&old->mnt_expire);
2249 out1:
2250 	unlock_mount(mp);
2251 out:
2252 	if (!err)
2253 		path_put(&parent_path);
2254 	path_put(&old_path);
2255 	return err;
2256 }
2257 
fs_set_subtype(struct vfsmount * mnt,const char * fstype)2258 static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
2259 {
2260 	int err;
2261 	const char *subtype = strchr(fstype, '.');
2262 	if (subtype) {
2263 		subtype++;
2264 		err = -EINVAL;
2265 		if (!subtype[0])
2266 			goto err;
2267 	} else
2268 		subtype = "";
2269 
2270 	mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
2271 	err = -ENOMEM;
2272 	if (!mnt->mnt_sb->s_subtype)
2273 		goto err;
2274 	return mnt;
2275 
2276  err:
2277 	mntput(mnt);
2278 	return ERR_PTR(err);
2279 }
2280 
2281 /*
2282  * add a mount into a namespace's mount tree
2283  */
do_add_mount(struct mount * newmnt,struct path * path,int mnt_flags)2284 static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags)
2285 {
2286 	struct mountpoint *mp;
2287 	struct mount *parent;
2288 	int err;
2289 
2290 	mnt_flags &= ~MNT_INTERNAL_FLAGS;
2291 
2292 	mp = lock_mount(path);
2293 	if (IS_ERR(mp))
2294 		return PTR_ERR(mp);
2295 
2296 	parent = real_mount(path->mnt);
2297 	err = -EINVAL;
2298 	if (unlikely(!check_mnt(parent))) {
2299 		/* that's acceptable only for automounts done in private ns */
2300 		if (!(mnt_flags & MNT_SHRINKABLE))
2301 			goto unlock;
2302 		/* ... and for those we'd better have mountpoint still alive */
2303 		if (!parent->mnt_ns)
2304 			goto unlock;
2305 	}
2306 
2307 	/* Refuse the same filesystem on the same mount point */
2308 	err = -EBUSY;
2309 	if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb &&
2310 	    path->mnt->mnt_root == path->dentry)
2311 		goto unlock;
2312 
2313 	err = -EINVAL;
2314 	if (S_ISLNK(newmnt->mnt.mnt_root->d_inode->i_mode))
2315 		goto unlock;
2316 
2317 	newmnt->mnt.mnt_flags = mnt_flags;
2318 	err = graft_tree(newmnt, parent, mp);
2319 
2320 unlock:
2321 	unlock_mount(mp);
2322 	return err;
2323 }
2324 
2325 static bool fs_fully_visible(struct file_system_type *fs_type, int *new_mnt_flags);
2326 
2327 /*
2328  * create a new mount for userspace and request it to be added into the
2329  * namespace's tree
2330  */
do_new_mount(struct path * path,const char * fstype,int flags,int mnt_flags,const char * name,void * data)2331 static int do_new_mount(struct path *path, const char *fstype, int flags,
2332 			int mnt_flags, const char *name, void *data)
2333 {
2334 	struct file_system_type *type;
2335 	struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns;
2336 	struct vfsmount *mnt;
2337 	int err;
2338 
2339 	if (!fstype)
2340 		return -EINVAL;
2341 
2342 	type = get_fs_type(fstype);
2343 	if (!type)
2344 		return -ENODEV;
2345 
2346 	if (user_ns != &init_user_ns) {
2347 		if (!(type->fs_flags & FS_USERNS_MOUNT)) {
2348 			put_filesystem(type);
2349 			return -EPERM;
2350 		}
2351 		/* Only in special cases allow devices from mounts
2352 		 * created outside the initial user namespace.
2353 		 */
2354 		if (!(type->fs_flags & FS_USERNS_DEV_MOUNT)) {
2355 			flags |= MS_NODEV;
2356 			mnt_flags |= MNT_NODEV | MNT_LOCK_NODEV;
2357 		}
2358 		if (type->fs_flags & FS_USERNS_VISIBLE) {
2359 			if (!fs_fully_visible(type, &mnt_flags)) {
2360 				put_filesystem(type);
2361 				return -EPERM;
2362 			}
2363 		}
2364 	}
2365 
2366 	mnt = vfs_kern_mount(type, flags, name, data);
2367 	if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
2368 	    !mnt->mnt_sb->s_subtype)
2369 		mnt = fs_set_subtype(mnt, fstype);
2370 
2371 	put_filesystem(type);
2372 	if (IS_ERR(mnt))
2373 		return PTR_ERR(mnt);
2374 
2375 	err = do_add_mount(real_mount(mnt), path, mnt_flags);
2376 	if (err)
2377 		mntput(mnt);
2378 	return err;
2379 }
2380 
finish_automount(struct vfsmount * m,struct path * path)2381 int finish_automount(struct vfsmount *m, struct path *path)
2382 {
2383 	struct mount *mnt = real_mount(m);
2384 	int err;
2385 	/* The new mount record should have at least 2 refs to prevent it being
2386 	 * expired before we get a chance to add it
2387 	 */
2388 	BUG_ON(mnt_get_count(mnt) < 2);
2389 
2390 	if (m->mnt_sb == path->mnt->mnt_sb &&
2391 	    m->mnt_root == path->dentry) {
2392 		err = -ELOOP;
2393 		goto fail;
2394 	}
2395 
2396 	err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE);
2397 	if (!err)
2398 		return 0;
2399 fail:
2400 	/* remove m from any expiration list it may be on */
2401 	if (!list_empty(&mnt->mnt_expire)) {
2402 		namespace_lock();
2403 		list_del_init(&mnt->mnt_expire);
2404 		namespace_unlock();
2405 	}
2406 	mntput(m);
2407 	mntput(m);
2408 	return err;
2409 }
2410 
2411 /**
2412  * mnt_set_expiry - Put a mount on an expiration list
2413  * @mnt: The mount to list.
2414  * @expiry_list: The list to add the mount to.
2415  */
mnt_set_expiry(struct vfsmount * mnt,struct list_head * expiry_list)2416 void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
2417 {
2418 	namespace_lock();
2419 
2420 	list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list);
2421 
2422 	namespace_unlock();
2423 }
2424 EXPORT_SYMBOL(mnt_set_expiry);
2425 
2426 /*
2427  * process a list of expirable mountpoints with the intent of discarding any
2428  * mountpoints that aren't in use and haven't been touched since last we came
2429  * here
2430  */
mark_mounts_for_expiry(struct list_head * mounts)2431 void mark_mounts_for_expiry(struct list_head *mounts)
2432 {
2433 	struct mount *mnt, *next;
2434 	LIST_HEAD(graveyard);
2435 
2436 	if (list_empty(mounts))
2437 		return;
2438 
2439 	namespace_lock();
2440 	lock_mount_hash();
2441 
2442 	/* extract from the expiration list every vfsmount that matches the
2443 	 * following criteria:
2444 	 * - only referenced by its parent vfsmount
2445 	 * - still marked for expiry (marked on the last call here; marks are
2446 	 *   cleared by mntput())
2447 	 */
2448 	list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
2449 		if (!xchg(&mnt->mnt_expiry_mark, 1) ||
2450 			propagate_mount_busy(mnt, 1))
2451 			continue;
2452 		list_move(&mnt->mnt_expire, &graveyard);
2453 	}
2454 	while (!list_empty(&graveyard)) {
2455 		mnt = list_first_entry(&graveyard, struct mount, mnt_expire);
2456 		touch_mnt_namespace(mnt->mnt_ns);
2457 		umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
2458 	}
2459 	unlock_mount_hash();
2460 	namespace_unlock();
2461 }
2462 
2463 EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
2464 
2465 /*
2466  * Ripoff of 'select_parent()'
2467  *
2468  * search the list of submounts for a given mountpoint, and move any
2469  * shrinkable submounts to the 'graveyard' list.
2470  */
select_submounts(struct mount * parent,struct list_head * graveyard)2471 static int select_submounts(struct mount *parent, struct list_head *graveyard)
2472 {
2473 	struct mount *this_parent = parent;
2474 	struct list_head *next;
2475 	int found = 0;
2476 
2477 repeat:
2478 	next = this_parent->mnt_mounts.next;
2479 resume:
2480 	while (next != &this_parent->mnt_mounts) {
2481 		struct list_head *tmp = next;
2482 		struct mount *mnt = list_entry(tmp, struct mount, mnt_child);
2483 
2484 		next = tmp->next;
2485 		if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE))
2486 			continue;
2487 		/*
2488 		 * Descend a level if the d_mounts list is non-empty.
2489 		 */
2490 		if (!list_empty(&mnt->mnt_mounts)) {
2491 			this_parent = mnt;
2492 			goto repeat;
2493 		}
2494 
2495 		if (!propagate_mount_busy(mnt, 1)) {
2496 			list_move_tail(&mnt->mnt_expire, graveyard);
2497 			found++;
2498 		}
2499 	}
2500 	/*
2501 	 * All done at this level ... ascend and resume the search
2502 	 */
2503 	if (this_parent != parent) {
2504 		next = this_parent->mnt_child.next;
2505 		this_parent = this_parent->mnt_parent;
2506 		goto resume;
2507 	}
2508 	return found;
2509 }
2510 
2511 /*
2512  * process a list of expirable mountpoints with the intent of discarding any
2513  * submounts of a specific parent mountpoint
2514  *
2515  * mount_lock must be held for write
2516  */
shrink_submounts(struct mount * mnt)2517 static void shrink_submounts(struct mount *mnt)
2518 {
2519 	LIST_HEAD(graveyard);
2520 	struct mount *m;
2521 
2522 	/* extract submounts of 'mountpoint' from the expiration list */
2523 	while (select_submounts(mnt, &graveyard)) {
2524 		while (!list_empty(&graveyard)) {
2525 			m = list_first_entry(&graveyard, struct mount,
2526 						mnt_expire);
2527 			touch_mnt_namespace(m->mnt_ns);
2528 			umount_tree(m, UMOUNT_PROPAGATE|UMOUNT_SYNC);
2529 		}
2530 	}
2531 }
2532 
2533 /*
2534  * Some copy_from_user() implementations do not return the exact number of
2535  * bytes remaining to copy on a fault.  But copy_mount_options() requires that.
2536  * Note that this function differs from copy_from_user() in that it will oops
2537  * on bad values of `to', rather than returning a short copy.
2538  */
exact_copy_from_user(void * to,const void __user * from,unsigned long n)2539 static long exact_copy_from_user(void *to, const void __user * from,
2540 				 unsigned long n)
2541 {
2542 	char *t = to;
2543 	const char __user *f = from;
2544 	char c;
2545 
2546 	if (!access_ok(VERIFY_READ, from, n))
2547 		return n;
2548 
2549 	while (n) {
2550 		if (__get_user(c, f)) {
2551 			memset(t, 0, n);
2552 			break;
2553 		}
2554 		*t++ = c;
2555 		f++;
2556 		n--;
2557 	}
2558 	return n;
2559 }
2560 
copy_mount_options(const void __user * data,unsigned long * where)2561 int copy_mount_options(const void __user * data, unsigned long *where)
2562 {
2563 	int i;
2564 	unsigned long page;
2565 	unsigned long size;
2566 
2567 	*where = 0;
2568 	if (!data)
2569 		return 0;
2570 
2571 	if (!(page = __get_free_page(GFP_KERNEL)))
2572 		return -ENOMEM;
2573 
2574 	/* We only care that *some* data at the address the user
2575 	 * gave us is valid.  Just in case, we'll zero
2576 	 * the remainder of the page.
2577 	 */
2578 	/* copy_from_user cannot cross TASK_SIZE ! */
2579 	size = TASK_SIZE - (unsigned long)data;
2580 	if (size > PAGE_SIZE)
2581 		size = PAGE_SIZE;
2582 
2583 	i = size - exact_copy_from_user((void *)page, data, size);
2584 	if (!i) {
2585 		free_page(page);
2586 		return -EFAULT;
2587 	}
2588 	if (i != PAGE_SIZE)
2589 		memset((char *)page + i, 0, PAGE_SIZE - i);
2590 	*where = page;
2591 	return 0;
2592 }
2593 
copy_mount_string(const void __user * data)2594 char *copy_mount_string(const void __user *data)
2595 {
2596 	return data ? strndup_user(data, PAGE_SIZE) : NULL;
2597 }
2598 
2599 /*
2600  * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
2601  * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
2602  *
2603  * data is a (void *) that can point to any structure up to
2604  * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
2605  * information (or be NULL).
2606  *
2607  * Pre-0.97 versions of mount() didn't have a flags word.
2608  * When the flags word was introduced its top half was required
2609  * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
2610  * Therefore, if this magic number is present, it carries no information
2611  * and must be discarded.
2612  */
do_mount(const char * dev_name,const char __user * dir_name,const char * type_page,unsigned long flags,void * data_page)2613 long do_mount(const char *dev_name, const char __user *dir_name,
2614 		const char *type_page, unsigned long flags, void *data_page)
2615 {
2616 	struct path path;
2617 	int retval = 0;
2618 	int mnt_flags = 0;
2619 
2620 	/* Discard magic */
2621 	if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
2622 		flags &= ~MS_MGC_MSK;
2623 
2624 	/* Basic sanity checks */
2625 	if (data_page)
2626 		((char *)data_page)[PAGE_SIZE - 1] = 0;
2627 
2628 	/* ... and get the mountpoint */
2629 	retval = user_path(dir_name, &path);
2630 	if (retval)
2631 		return retval;
2632 
2633 	retval = security_sb_mount(dev_name, &path,
2634 				   type_page, flags, data_page);
2635 	if (!retval && !may_mount())
2636 		retval = -EPERM;
2637 	if (retval)
2638 		goto dput_out;
2639 
2640 	/* Default to relatime unless overriden */
2641 	if (!(flags & MS_NOATIME))
2642 		mnt_flags |= MNT_RELATIME;
2643 
2644 	/* Separate the per-mountpoint flags */
2645 	if (flags & MS_NOSUID)
2646 		mnt_flags |= MNT_NOSUID;
2647 	if (flags & MS_NODEV)
2648 		mnt_flags |= MNT_NODEV;
2649 	if (flags & MS_NOEXEC)
2650 		mnt_flags |= MNT_NOEXEC;
2651 	if (flags & MS_NOATIME)
2652 		mnt_flags |= MNT_NOATIME;
2653 	if (flags & MS_NODIRATIME)
2654 		mnt_flags |= MNT_NODIRATIME;
2655 	if (flags & MS_STRICTATIME)
2656 		mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
2657 	if (flags & MS_RDONLY)
2658 		mnt_flags |= MNT_READONLY;
2659 
2660 	/* The default atime for remount is preservation */
2661 	if ((flags & MS_REMOUNT) &&
2662 	    ((flags & (MS_NOATIME | MS_NODIRATIME | MS_RELATIME |
2663 		       MS_STRICTATIME)) == 0)) {
2664 		mnt_flags &= ~MNT_ATIME_MASK;
2665 		mnt_flags |= path.mnt->mnt_flags & MNT_ATIME_MASK;
2666 	}
2667 
2668 	flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN |
2669 		   MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT |
2670 		   MS_STRICTATIME);
2671 
2672 	if (flags & MS_REMOUNT)
2673 		retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags,
2674 				    data_page);
2675 	else if (flags & MS_BIND)
2676 		retval = do_loopback(&path, dev_name, flags & MS_REC);
2677 	else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
2678 		retval = do_change_type(&path, flags);
2679 	else if (flags & MS_MOVE)
2680 		retval = do_move_mount(&path, dev_name);
2681 	else
2682 		retval = do_new_mount(&path, type_page, flags, mnt_flags,
2683 				      dev_name, data_page);
2684 dput_out:
2685 	path_put(&path);
2686 	return retval;
2687 }
2688 
free_mnt_ns(struct mnt_namespace * ns)2689 static void free_mnt_ns(struct mnt_namespace *ns)
2690 {
2691 	proc_free_inum(ns->proc_inum);
2692 	put_user_ns(ns->user_ns);
2693 	kfree(ns);
2694 }
2695 
2696 /*
2697  * Assign a sequence number so we can detect when we attempt to bind
2698  * mount a reference to an older mount namespace into the current
2699  * mount namespace, preventing reference counting loops.  A 64bit
2700  * number incrementing at 10Ghz will take 12,427 years to wrap which
2701  * is effectively never, so we can ignore the possibility.
2702  */
2703 static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1);
2704 
alloc_mnt_ns(struct user_namespace * user_ns)2705 static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns)
2706 {
2707 	struct mnt_namespace *new_ns;
2708 	int ret;
2709 
2710 	new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
2711 	if (!new_ns)
2712 		return ERR_PTR(-ENOMEM);
2713 	ret = proc_alloc_inum(&new_ns->proc_inum);
2714 	if (ret) {
2715 		kfree(new_ns);
2716 		return ERR_PTR(ret);
2717 	}
2718 	new_ns->seq = atomic64_add_return(1, &mnt_ns_seq);
2719 	atomic_set(&new_ns->count, 1);
2720 	new_ns->root = NULL;
2721 	INIT_LIST_HEAD(&new_ns->list);
2722 	init_waitqueue_head(&new_ns->poll);
2723 	new_ns->event = 0;
2724 	new_ns->user_ns = get_user_ns(user_ns);
2725 	return new_ns;
2726 }
2727 
copy_mnt_ns(unsigned long flags,struct mnt_namespace * ns,struct user_namespace * user_ns,struct fs_struct * new_fs)2728 struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
2729 		struct user_namespace *user_ns, struct fs_struct *new_fs)
2730 {
2731 	struct mnt_namespace *new_ns;
2732 	struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
2733 	struct mount *p, *q;
2734 	struct mount *old;
2735 	struct mount *new;
2736 	int copy_flags;
2737 
2738 	BUG_ON(!ns);
2739 
2740 	if (likely(!(flags & CLONE_NEWNS))) {
2741 		get_mnt_ns(ns);
2742 		return ns;
2743 	}
2744 
2745 	old = ns->root;
2746 
2747 	new_ns = alloc_mnt_ns(user_ns);
2748 	if (IS_ERR(new_ns))
2749 		return new_ns;
2750 
2751 	namespace_lock();
2752 	/* First pass: copy the tree topology */
2753 	copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE;
2754 	if (user_ns != ns->user_ns)
2755 		copy_flags |= CL_SHARED_TO_SLAVE | CL_UNPRIVILEGED;
2756 	new = copy_tree(old, old->mnt.mnt_root, copy_flags);
2757 	if (IS_ERR(new)) {
2758 		namespace_unlock();
2759 		free_mnt_ns(new_ns);
2760 		return ERR_CAST(new);
2761 	}
2762 	new_ns->root = new;
2763 	list_add_tail(&new_ns->list, &new->mnt_list);
2764 
2765 	/*
2766 	 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
2767 	 * as belonging to new namespace.  We have already acquired a private
2768 	 * fs_struct, so tsk->fs->lock is not needed.
2769 	 */
2770 	p = old;
2771 	q = new;
2772 	while (p) {
2773 		q->mnt_ns = new_ns;
2774 		if (new_fs) {
2775 			if (&p->mnt == new_fs->root.mnt) {
2776 				new_fs->root.mnt = mntget(&q->mnt);
2777 				rootmnt = &p->mnt;
2778 			}
2779 			if (&p->mnt == new_fs->pwd.mnt) {
2780 				new_fs->pwd.mnt = mntget(&q->mnt);
2781 				pwdmnt = &p->mnt;
2782 			}
2783 		}
2784 		p = next_mnt(p, old);
2785 		q = next_mnt(q, new);
2786 		if (!q)
2787 			break;
2788 		while (p->mnt.mnt_root != q->mnt.mnt_root)
2789 			p = next_mnt(p, old);
2790 	}
2791 	namespace_unlock();
2792 
2793 	if (rootmnt)
2794 		mntput(rootmnt);
2795 	if (pwdmnt)
2796 		mntput(pwdmnt);
2797 
2798 	return new_ns;
2799 }
2800 
2801 /**
2802  * create_mnt_ns - creates a private namespace and adds a root filesystem
2803  * @mnt: pointer to the new root filesystem mountpoint
2804  */
create_mnt_ns(struct vfsmount * m)2805 static struct mnt_namespace *create_mnt_ns(struct vfsmount *m)
2806 {
2807 	struct mnt_namespace *new_ns = alloc_mnt_ns(&init_user_ns);
2808 	if (!IS_ERR(new_ns)) {
2809 		struct mount *mnt = real_mount(m);
2810 		mnt->mnt_ns = new_ns;
2811 		new_ns->root = mnt;
2812 		list_add(&mnt->mnt_list, &new_ns->list);
2813 	} else {
2814 		mntput(m);
2815 	}
2816 	return new_ns;
2817 }
2818 
mount_subtree(struct vfsmount * mnt,const char * name)2819 struct dentry *mount_subtree(struct vfsmount *mnt, const char *name)
2820 {
2821 	struct mnt_namespace *ns;
2822 	struct super_block *s;
2823 	struct path path;
2824 	int err;
2825 
2826 	ns = create_mnt_ns(mnt);
2827 	if (IS_ERR(ns))
2828 		return ERR_CAST(ns);
2829 
2830 	err = vfs_path_lookup(mnt->mnt_root, mnt,
2831 			name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
2832 
2833 	put_mnt_ns(ns);
2834 
2835 	if (err)
2836 		return ERR_PTR(err);
2837 
2838 	/* trade a vfsmount reference for active sb one */
2839 	s = path.mnt->mnt_sb;
2840 	atomic_inc(&s->s_active);
2841 	mntput(path.mnt);
2842 	/* lock the sucker */
2843 	down_write(&s->s_umount);
2844 	/* ... and return the root of (sub)tree on it */
2845 	return path.dentry;
2846 }
2847 EXPORT_SYMBOL(mount_subtree);
2848 
SYSCALL_DEFINE5(mount,char __user *,dev_name,char __user *,dir_name,char __user *,type,unsigned long,flags,void __user *,data)2849 SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
2850 		char __user *, type, unsigned long, flags, void __user *, data)
2851 {
2852 	int ret;
2853 	char *kernel_type;
2854 	char *kernel_dev;
2855 	unsigned long data_page;
2856 
2857 	kernel_type = copy_mount_string(type);
2858 	ret = PTR_ERR(kernel_type);
2859 	if (IS_ERR(kernel_type))
2860 		goto out_type;
2861 
2862 	kernel_dev = copy_mount_string(dev_name);
2863 	ret = PTR_ERR(kernel_dev);
2864 	if (IS_ERR(kernel_dev))
2865 		goto out_dev;
2866 
2867 	ret = copy_mount_options(data, &data_page);
2868 	if (ret < 0)
2869 		goto out_data;
2870 
2871 	ret = do_mount(kernel_dev, dir_name, kernel_type, flags,
2872 		(void *) data_page);
2873 
2874 	free_page(data_page);
2875 out_data:
2876 	kfree(kernel_dev);
2877 out_dev:
2878 	kfree(kernel_type);
2879 out_type:
2880 	return ret;
2881 }
2882 
2883 /*
2884  * Return true if path is reachable from root
2885  *
2886  * namespace_sem or mount_lock is held
2887  */
is_path_reachable(struct mount * mnt,struct dentry * dentry,const struct path * root)2888 bool is_path_reachable(struct mount *mnt, struct dentry *dentry,
2889 			 const struct path *root)
2890 {
2891 	while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) {
2892 		dentry = mnt->mnt_mountpoint;
2893 		mnt = mnt->mnt_parent;
2894 	}
2895 	return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry);
2896 }
2897 
path_is_under(struct path * path1,struct path * path2)2898 int path_is_under(struct path *path1, struct path *path2)
2899 {
2900 	int res;
2901 	read_seqlock_excl(&mount_lock);
2902 	res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2);
2903 	read_sequnlock_excl(&mount_lock);
2904 	return res;
2905 }
2906 EXPORT_SYMBOL(path_is_under);
2907 
2908 /*
2909  * pivot_root Semantics:
2910  * Moves the root file system of the current process to the directory put_old,
2911  * makes new_root as the new root file system of the current process, and sets
2912  * root/cwd of all processes which had them on the current root to new_root.
2913  *
2914  * Restrictions:
2915  * The new_root and put_old must be directories, and  must not be on the
2916  * same file  system as the current process root. The put_old  must  be
2917  * underneath new_root,  i.e. adding a non-zero number of /.. to the string
2918  * pointed to by put_old must yield the same directory as new_root. No other
2919  * file system may be mounted on put_old. After all, new_root is a mountpoint.
2920  *
2921  * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
2922  * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
2923  * in this situation.
2924  *
2925  * Notes:
2926  *  - we don't move root/cwd if they are not at the root (reason: if something
2927  *    cared enough to change them, it's probably wrong to force them elsewhere)
2928  *  - it's okay to pick a root that isn't the root of a file system, e.g.
2929  *    /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
2930  *    though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
2931  *    first.
2932  */
SYSCALL_DEFINE2(pivot_root,const char __user *,new_root,const char __user *,put_old)2933 SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
2934 		const char __user *, put_old)
2935 {
2936 	struct path new, old, parent_path, root_parent, root;
2937 	struct mount *new_mnt, *root_mnt, *old_mnt;
2938 	struct mountpoint *old_mp, *root_mp;
2939 	int error;
2940 
2941 	if (!may_mount())
2942 		return -EPERM;
2943 
2944 	error = user_path_dir(new_root, &new);
2945 	if (error)
2946 		goto out0;
2947 
2948 	error = user_path_dir(put_old, &old);
2949 	if (error)
2950 		goto out1;
2951 
2952 	error = security_sb_pivotroot(&old, &new);
2953 	if (error)
2954 		goto out2;
2955 
2956 	get_fs_root(current->fs, &root);
2957 	old_mp = lock_mount(&old);
2958 	error = PTR_ERR(old_mp);
2959 	if (IS_ERR(old_mp))
2960 		goto out3;
2961 
2962 	error = -EINVAL;
2963 	new_mnt = real_mount(new.mnt);
2964 	root_mnt = real_mount(root.mnt);
2965 	old_mnt = real_mount(old.mnt);
2966 	if (IS_MNT_SHARED(old_mnt) ||
2967 		IS_MNT_SHARED(new_mnt->mnt_parent) ||
2968 		IS_MNT_SHARED(root_mnt->mnt_parent))
2969 		goto out4;
2970 	if (!check_mnt(root_mnt) || !check_mnt(new_mnt))
2971 		goto out4;
2972 	if (new_mnt->mnt.mnt_flags & MNT_LOCKED)
2973 		goto out4;
2974 	error = -ENOENT;
2975 	if (d_unlinked(new.dentry))
2976 		goto out4;
2977 	error = -EBUSY;
2978 	if (new_mnt == root_mnt || old_mnt == root_mnt)
2979 		goto out4; /* loop, on the same file system  */
2980 	error = -EINVAL;
2981 	if (root.mnt->mnt_root != root.dentry)
2982 		goto out4; /* not a mountpoint */
2983 	if (!mnt_has_parent(root_mnt))
2984 		goto out4; /* not attached */
2985 	root_mp = root_mnt->mnt_mp;
2986 	if (new.mnt->mnt_root != new.dentry)
2987 		goto out4; /* not a mountpoint */
2988 	if (!mnt_has_parent(new_mnt))
2989 		goto out4; /* not attached */
2990 	/* make sure we can reach put_old from new_root */
2991 	if (!is_path_reachable(old_mnt, old.dentry, &new))
2992 		goto out4;
2993 	/* make certain new is below the root */
2994 	if (!is_path_reachable(new_mnt, new.dentry, &root))
2995 		goto out4;
2996 	root_mp->m_count++; /* pin it so it won't go away */
2997 	lock_mount_hash();
2998 	detach_mnt(new_mnt, &parent_path);
2999 	detach_mnt(root_mnt, &root_parent);
3000 	if (root_mnt->mnt.mnt_flags & MNT_LOCKED) {
3001 		new_mnt->mnt.mnt_flags |= MNT_LOCKED;
3002 		root_mnt->mnt.mnt_flags &= ~MNT_LOCKED;
3003 	}
3004 	/* mount old root on put_old */
3005 	attach_mnt(root_mnt, old_mnt, old_mp);
3006 	/* mount new_root on / */
3007 	attach_mnt(new_mnt, real_mount(root_parent.mnt), root_mp);
3008 	touch_mnt_namespace(current->nsproxy->mnt_ns);
3009 	unlock_mount_hash();
3010 	chroot_fs_refs(&root, &new);
3011 	put_mountpoint(root_mp);
3012 	error = 0;
3013 out4:
3014 	unlock_mount(old_mp);
3015 	if (!error) {
3016 		path_put(&root_parent);
3017 		path_put(&parent_path);
3018 	}
3019 out3:
3020 	path_put(&root);
3021 out2:
3022 	path_put(&old);
3023 out1:
3024 	path_put(&new);
3025 out0:
3026 	return error;
3027 }
3028 
init_mount_tree(void)3029 static void __init init_mount_tree(void)
3030 {
3031 	struct vfsmount *mnt;
3032 	struct mnt_namespace *ns;
3033 	struct path root;
3034 	struct file_system_type *type;
3035 
3036 	type = get_fs_type("rootfs");
3037 	if (!type)
3038 		panic("Can't find rootfs type");
3039 	mnt = vfs_kern_mount(type, 0, "rootfs", NULL);
3040 	put_filesystem(type);
3041 	if (IS_ERR(mnt))
3042 		panic("Can't create rootfs");
3043 
3044 	ns = create_mnt_ns(mnt);
3045 	if (IS_ERR(ns))
3046 		panic("Can't allocate initial namespace");
3047 
3048 	init_task.nsproxy->mnt_ns = ns;
3049 	get_mnt_ns(ns);
3050 
3051 	root.mnt = mnt;
3052 	root.dentry = mnt->mnt_root;
3053 
3054 	set_fs_pwd(current->fs, &root);
3055 	set_fs_root(current->fs, &root);
3056 }
3057 
mnt_init(void)3058 void __init mnt_init(void)
3059 {
3060 	unsigned u;
3061 	int err;
3062 
3063 	mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
3064 			0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
3065 
3066 	mount_hashtable = alloc_large_system_hash("Mount-cache",
3067 				sizeof(struct hlist_head),
3068 				mhash_entries, 19,
3069 				0,
3070 				&m_hash_shift, &m_hash_mask, 0, 0);
3071 	mountpoint_hashtable = alloc_large_system_hash("Mountpoint-cache",
3072 				sizeof(struct hlist_head),
3073 				mphash_entries, 19,
3074 				0,
3075 				&mp_hash_shift, &mp_hash_mask, 0, 0);
3076 
3077 	if (!mount_hashtable || !mountpoint_hashtable)
3078 		panic("Failed to allocate mount hash table\n");
3079 
3080 	for (u = 0; u <= m_hash_mask; u++)
3081 		INIT_HLIST_HEAD(&mount_hashtable[u]);
3082 	for (u = 0; u <= mp_hash_mask; u++)
3083 		INIT_HLIST_HEAD(&mountpoint_hashtable[u]);
3084 
3085 	kernfs_init();
3086 
3087 	err = sysfs_init();
3088 	if (err)
3089 		printk(KERN_WARNING "%s: sysfs_init error: %d\n",
3090 			__func__, err);
3091 	fs_kobj = kobject_create_and_add("fs", NULL);
3092 	if (!fs_kobj)
3093 		printk(KERN_WARNING "%s: kobj create error\n", __func__);
3094 	init_rootfs();
3095 	init_mount_tree();
3096 }
3097 
put_mnt_ns(struct mnt_namespace * ns)3098 void put_mnt_ns(struct mnt_namespace *ns)
3099 {
3100 	if (!atomic_dec_and_test(&ns->count))
3101 		return;
3102 	drop_collected_mounts(&ns->root->mnt);
3103 	free_mnt_ns(ns);
3104 }
3105 
kern_mount_data(struct file_system_type * type,void * data)3106 struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
3107 {
3108 	struct vfsmount *mnt;
3109 	mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
3110 	if (!IS_ERR(mnt)) {
3111 		/*
3112 		 * it is a longterm mount, don't release mnt until
3113 		 * we unmount before file sys is unregistered
3114 		*/
3115 		real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL;
3116 	}
3117 	return mnt;
3118 }
3119 EXPORT_SYMBOL_GPL(kern_mount_data);
3120 
kern_unmount(struct vfsmount * mnt)3121 void kern_unmount(struct vfsmount *mnt)
3122 {
3123 	/* release long term mount so mount point can be released */
3124 	if (!IS_ERR_OR_NULL(mnt)) {
3125 		real_mount(mnt)->mnt_ns = NULL;
3126 		synchronize_rcu();	/* yecchhh... */
3127 		mntput(mnt);
3128 	}
3129 }
3130 EXPORT_SYMBOL(kern_unmount);
3131 
our_mnt(struct vfsmount * mnt)3132 bool our_mnt(struct vfsmount *mnt)
3133 {
3134 	return check_mnt(real_mount(mnt));
3135 }
3136 
current_chrooted(void)3137 bool current_chrooted(void)
3138 {
3139 	/* Does the current process have a non-standard root */
3140 	struct path ns_root;
3141 	struct path fs_root;
3142 	bool chrooted;
3143 
3144 	/* Find the namespace root */
3145 	ns_root.mnt = &current->nsproxy->mnt_ns->root->mnt;
3146 	ns_root.dentry = ns_root.mnt->mnt_root;
3147 	path_get(&ns_root);
3148 	while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root))
3149 		;
3150 
3151 	get_fs_root(current->fs, &fs_root);
3152 
3153 	chrooted = !path_equal(&fs_root, &ns_root);
3154 
3155 	path_put(&fs_root);
3156 	path_put(&ns_root);
3157 
3158 	return chrooted;
3159 }
3160 
fs_fully_visible(struct file_system_type * type,int * new_mnt_flags)3161 static bool fs_fully_visible(struct file_system_type *type, int *new_mnt_flags)
3162 {
3163 	struct mnt_namespace *ns = current->nsproxy->mnt_ns;
3164 	int new_flags = *new_mnt_flags;
3165 	struct mount *mnt;
3166 	bool visible = false;
3167 
3168 	if (unlikely(!ns))
3169 		return false;
3170 
3171 	down_read(&namespace_sem);
3172 	list_for_each_entry(mnt, &ns->list, mnt_list) {
3173 		struct mount *child;
3174 		if (mnt->mnt.mnt_sb->s_type != type)
3175 			continue;
3176 
3177 		/* This mount is not fully visible if it's root directory
3178 		 * is not the root directory of the filesystem.
3179 		 */
3180 		if (mnt->mnt.mnt_root != mnt->mnt.mnt_sb->s_root)
3181 			continue;
3182 
3183 		/* Verify the mount flags are equal to or more permissive
3184 		 * than the proposed new mount.
3185 		 */
3186 		if ((mnt->mnt.mnt_flags & MNT_LOCK_READONLY) &&
3187 		    !(new_flags & MNT_READONLY))
3188 			continue;
3189 		if ((mnt->mnt.mnt_flags & MNT_LOCK_NODEV) &&
3190 		    !(new_flags & MNT_NODEV))
3191 			continue;
3192 		if ((mnt->mnt.mnt_flags & MNT_LOCK_ATIME) &&
3193 		    ((mnt->mnt.mnt_flags & MNT_ATIME_MASK) != (new_flags & MNT_ATIME_MASK)))
3194 			continue;
3195 
3196 		/* This mount is not fully visible if there are any
3197 		 * locked child mounts that cover anything except for
3198 		 * empty directories.
3199 		 */
3200 		list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
3201 			struct inode *inode = child->mnt_mountpoint->d_inode;
3202 			/* Only worry about locked mounts */
3203 			if (!(child->mnt.mnt_flags & MNT_LOCKED))
3204 				continue;
3205 			if (!S_ISDIR(inode->i_mode))
3206 				goto next;
3207 			if (inode->i_nlink > 2)
3208 				goto next;
3209 		}
3210 		/* Preserve the locked attributes */
3211 		*new_mnt_flags |= mnt->mnt.mnt_flags & (MNT_LOCK_READONLY | \
3212 							MNT_LOCK_NODEV    | \
3213 							MNT_LOCK_ATIME);
3214 		visible = true;
3215 		goto found;
3216 	next:	;
3217 	}
3218 found:
3219 	up_read(&namespace_sem);
3220 	return visible;
3221 }
3222 
mntns_get(struct task_struct * task)3223 static void *mntns_get(struct task_struct *task)
3224 {
3225 	struct mnt_namespace *ns = NULL;
3226 	struct nsproxy *nsproxy;
3227 
3228 	task_lock(task);
3229 	nsproxy = task->nsproxy;
3230 	if (nsproxy) {
3231 		ns = nsproxy->mnt_ns;
3232 		get_mnt_ns(ns);
3233 	}
3234 	task_unlock(task);
3235 
3236 	return ns;
3237 }
3238 
mntns_put(void * ns)3239 static void mntns_put(void *ns)
3240 {
3241 	put_mnt_ns(ns);
3242 }
3243 
mntns_install(struct nsproxy * nsproxy,void * ns)3244 static int mntns_install(struct nsproxy *nsproxy, void *ns)
3245 {
3246 	struct fs_struct *fs = current->fs;
3247 	struct mnt_namespace *mnt_ns = ns;
3248 	struct path root;
3249 
3250 	if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) ||
3251 	    !ns_capable(current_user_ns(), CAP_SYS_CHROOT) ||
3252 	    !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
3253 		return -EPERM;
3254 
3255 	if (fs->users != 1)
3256 		return -EINVAL;
3257 
3258 	get_mnt_ns(mnt_ns);
3259 	put_mnt_ns(nsproxy->mnt_ns);
3260 	nsproxy->mnt_ns = mnt_ns;
3261 
3262 	/* Find the root */
3263 	root.mnt    = &mnt_ns->root->mnt;
3264 	root.dentry = mnt_ns->root->mnt.mnt_root;
3265 	path_get(&root);
3266 	while(d_mountpoint(root.dentry) && follow_down_one(&root))
3267 		;
3268 
3269 	/* Update the pwd and root */
3270 	set_fs_pwd(fs, &root);
3271 	set_fs_root(fs, &root);
3272 
3273 	path_put(&root);
3274 	return 0;
3275 }
3276 
mntns_inum(void * ns)3277 static unsigned int mntns_inum(void *ns)
3278 {
3279 	struct mnt_namespace *mnt_ns = ns;
3280 	return mnt_ns->proc_inum;
3281 }
3282 
3283 const struct proc_ns_operations mntns_operations = {
3284 	.name		= "mnt",
3285 	.type		= CLONE_NEWNS,
3286 	.get		= mntns_get,
3287 	.put		= mntns_put,
3288 	.install	= mntns_install,
3289 	.inum		= mntns_inum,
3290 };
3291