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