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