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