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