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