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