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