at commit a291a5cf

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 #include <linux/pidfs.h>
36 #include <linux/nstree.h>
37
38 #include "pnode.h"
39 #include "internal.h"
40
41 /* Maximum number of mounts in a mount namespace */
42 static unsigned int sysctl_mount_max __read_mostly = 100000;
43
44 static unsigned int m_hash_mask __ro_after_init;
45 static unsigned int m_hash_shift __ro_after_init;
46 static unsigned int mp_hash_mask __ro_after_init;
47 static unsigned int mp_hash_shift __ro_after_init;
48
49 static __initdata unsigned long mhash_entries;
50 static int __init set_mhash_entries(char *str)
51 {
52 return kstrtoul(str, 0, &mhash_entries) == 0;
53 }
54 __setup("mhash_entries=", set_mhash_entries);
55
56 static __initdata unsigned long mphash_entries;
57 static int __init set_mphash_entries(char *str)
58 {
59 return kstrtoul(str, 0, &mphash_entries) == 0;
60 }
61 __setup("mphash_entries=", set_mphash_entries);
62
63 static char * __initdata initramfs_options;
64 static int __init initramfs_options_setup(char *str)
65 {
66 initramfs_options = str;
67 return 1;
68 }
69
70 __setup("initramfs_options=", initramfs_options_setup);
71
72 static u64 event;
73 static DEFINE_XARRAY_FLAGS(mnt_id_xa, XA_FLAGS_ALLOC);
74 static DEFINE_IDA(mnt_group_ida);
75
76 /* Don't allow confusion with old 32bit mount ID */
77 #define MNT_UNIQUE_ID_OFFSET (1ULL << 31)
78 static u64 mnt_id_ctr = MNT_UNIQUE_ID_OFFSET;
79
80 static struct hlist_head *mount_hashtable __ro_after_init;
81 static struct hlist_head *mountpoint_hashtable __ro_after_init;
82 static struct kmem_cache *mnt_cache __ro_after_init;
83 struct vfsmount *knullfs __ro_after_init; /* private nullfs instance */
84 static struct vfsmount *knullfs_file __ro_after_init; /* its regular file */
85 static DECLARE_RWSEM(namespace_sem);
86 static HLIST_HEAD(unmounted); /* protected by namespace_sem */
87 static LIST_HEAD(ex_mountpoints); /* protected by namespace_sem */
88 static struct mnt_namespace *emptied_ns; /* protected by namespace_sem */
89
90 static inline void namespace_lock(void);
91 static void namespace_unlock(void);
92 DEFINE_LOCK_GUARD_0(namespace_excl, namespace_lock(), namespace_unlock())
93 DEFINE_LOCK_GUARD_0(namespace_shared, down_read(&namespace_sem),
94 up_read(&namespace_sem))
95
96 DEFINE_FREE(mntput, struct vfsmount *, if (!IS_ERR(_T)) mntput(_T))
97
98 #ifdef CONFIG_FSNOTIFY
99 LIST_HEAD(notify_list); /* protected by namespace_sem */
100 #endif
101
102 enum mount_kattr_flags_t {
103 MOUNT_KATTR_RECURSE = (1 << 0),
104 MOUNT_KATTR_IDMAP_REPLACE = (1 << 1),
105 };
106
107 struct mount_kattr {
108 unsigned int attr_set;
109 unsigned int attr_clr;
110 unsigned int propagation;
111 unsigned int lookup_flags;
112 enum mount_kattr_flags_t kflags;
113 struct user_namespace *mnt_userns;
114 struct mnt_idmap *mnt_idmap;
115 };
116
117 /* /sys/fs */
118 struct kobject *fs_kobj __ro_after_init;
119 EXPORT_SYMBOL_GPL(fs_kobj);
120
121 /*
122 * vfsmount lock may be taken for read to prevent changes to the
123 * vfsmount hash, ie. during mountpoint lookups or walking back
124 * up the tree.
125 *
126 * It should be taken for write in all cases where the vfsmount
127 * tree or hash is modified or when a vfsmount structure is modified.
128 */
129 __cacheline_aligned_in_smp DEFINE_SEQLOCK(mount_lock);
130
131 static void mnt_ns_release(struct mnt_namespace *ns)
132 {
133 /* keep alive for {list,stat}mount() */
134 if (ns && refcount_dec_and_test(&ns->passive)) {
135 put_user_ns(ns->user_ns);
136 kfree(ns);
137 }
138 }
139 DEFINE_FREE(mnt_ns_release, struct mnt_namespace *,
140 if (!IS_ERR(_T)) mnt_ns_release(_T))
141
142 static void mnt_ns_release_rcu(struct rcu_head *rcu)
143 {
144 mnt_ns_release(container_of(rcu, struct mnt_namespace, ns.ns_rcu));
145 }
146
147 static void mnt_ns_tree_remove(struct mnt_namespace *ns)
148 {
149 /* remove from global mount namespace list */
150 if (ns_tree_active(ns))
151 ns_tree_remove(ns);
152
153 call_rcu(&ns->ns.ns_rcu, mnt_ns_release_rcu);
154 }
155
156 /*
157 * Lookup a mount namespace by id and take a passive reference count. Taking a
158 * passive reference means the mount namespace can be emptied if e.g., the last
159 * task holding an active reference exits. To access the mounts of the
160 * namespace the @namespace_sem must first be acquired. If the namespace has
161 * already shut down before acquiring @namespace_sem, {list,stat}mount() will
162 * see that the mount rbtree of the namespace is empty.
163 *
164 * Note the lookup is lockless protected by a sequence counter. We only
165 * need to guard against false negatives as false positives aren't
166 * possible. So if we didn't find a mount namespace and the sequence
167 * counter has changed we need to retry. If the sequence counter is
168 * still the same we know the search actually failed.
169 */
170 static struct mnt_namespace *lookup_mnt_ns(u64 mnt_ns_id)
171 {
172 struct mnt_namespace *mnt_ns;
173 struct ns_common *ns;
174
175 guard(rcu)();
176 ns = ns_tree_lookup_rcu(mnt_ns_id, CLONE_NEWNS);
177 if (!ns)
178 return NULL;
179
180 /*
181 * The last reference count is put with RCU delay so we can
182 * unconditonally acquire a reference here.
183 */
184 mnt_ns = container_of(ns, struct mnt_namespace, ns);
185 refcount_inc(&mnt_ns->passive);
186 return mnt_ns;
187 }
188
189 static inline void lock_mount_hash(void)
190 {
191 write_seqlock(&mount_lock);
192 }
193
194 static inline void unlock_mount_hash(void)
195 {
196 write_sequnlock(&mount_lock);
197 }
198
199 static inline struct hlist_head *m_hash(struct vfsmount *mnt, struct dentry *dentry)
200 {
201 unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
202 tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
203 tmp = tmp + (tmp >> m_hash_shift);
204 return &mount_hashtable[tmp & m_hash_mask];
205 }
206
207 static inline struct hlist_head *mp_hash(struct dentry *dentry)
208 {
209 unsigned long tmp = ((unsigned long)dentry / L1_CACHE_BYTES);
210 tmp = tmp + (tmp >> mp_hash_shift);
211 return &mountpoint_hashtable[tmp & mp_hash_mask];
212 }
213
214 /*
215 * What an unmounted mount leaves behind at its unmounted parent instead of
216 * staying attached to it. A lookup on the parent at the mountpoint finds
217 * a stand-in for as long as the cover is there. The parent frees it.
218 */
219 struct mnt_cover {
220 struct hlist_node node; /* parent->mnt_covers, RCU */
221 struct hlist_node pin; /* mp->m_covers, keeps the mountpoint */
222 struct dentry *dentry;
223 struct mountpoint *mp;
224 struct rcu_head rcu;
225 };
226
227 static int mnt_alloc_id(struct mount *mnt)
228 {
229 int res;
230
231 xa_lock(&mnt_id_xa);
232 res = __xa_alloc(&mnt_id_xa, &mnt->mnt_id, mnt, xa_limit_31b, GFP_KERNEL);
233 if (!res)
234 mnt->mnt_id_unique = ++mnt_id_ctr;
235 xa_unlock(&mnt_id_xa);
236 return res;
237 }
238
239 static void mnt_free_id(struct mount *mnt)
240 {
241 xa_erase(&mnt_id_xa, mnt->mnt_id);
242 }
243
244 /*
245 * Allocate a new peer group ID
246 */
247 static int mnt_alloc_group_id(struct mount *mnt)
248 {
249 int res = ida_alloc_min(&mnt_group_ida, 1, GFP_KERNEL);
250
251 if (res < 0)
252 return res;
253 mnt->mnt_group_id = res;
254 return 0;
255 }
256
257 /*
258 * Release a peer group ID
259 */
260 void mnt_release_group_id(struct mount *mnt)
261 {
262 ida_free(&mnt_group_ida, mnt->mnt_group_id);
263 mnt->mnt_group_id = 0;
264 }
265
266 static inline void mnt_inc_count(struct mount *mnt)
267 {
268 #ifdef CONFIG_SMP
269 this_cpu_inc(mnt->mnt_pcp->mnt_gets);
270 #else
271 preempt_disable();
272 mnt->mnt_count++;
273 preempt_enable();
274 #endif
275 }
276
277 static inline void mnt_dec_count(struct mount *mnt)
278 {
279 #ifdef CONFIG_SMP
280 this_cpu_inc(mnt->mnt_pcp->mnt_puts);
281 #else
282 preempt_disable();
283 mnt->mnt_count--;
284 preempt_enable();
285 #endif
286 }
287
288 /*
289 * vfsmount lock must be held for write
290 */
291 int mnt_get_count(struct mount *mnt)
292 {
293 #ifdef CONFIG_SMP
294 unsigned int gets = 0, puts = 0;
295 int cpu;
296
297 /* puts first, so a put counted here has its get counted below */
298 for_each_possible_cpu(cpu)
299 puts += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_puts;
300 /*
301 * Pairs with the smp_wmb() in mntput_no_expire() and with the
302 * smp_mb() in __legitimize_mnt().
303 */
304 smp_mb();
305 for_each_possible_cpu(cpu)
306 gets += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_gets;
307
308 return gets - puts;
309 #else
310 return mnt->mnt_count;
311 #endif
312 }
313
314 static struct mount *alloc_vfsmnt(const char *name)
315 {
316 struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
317 if (mnt) {
318 int err;
319
320 mnt->mnt_cover = kzalloc_obj(struct mnt_cover, GFP_KERNEL_ACCOUNT);
321 if (!mnt->mnt_cover)
322 goto out_free_cache;
323
324 err = mnt_alloc_id(mnt);
325 if (err)
326 goto out_free_cache;
327
328 if (name)
329 mnt->mnt_devname = kstrdup_const(name,
330 GFP_KERNEL_ACCOUNT);
331 else
332 mnt->mnt_devname = "none";
333 if (!mnt->mnt_devname)
334 goto out_free_id;
335
336 #ifdef CONFIG_SMP
337 mnt->mnt_pcp = alloc_percpu(struct mnt_pcp);
338 if (!mnt->mnt_pcp)
339 goto out_free_devname;
340
341 this_cpu_inc(mnt->mnt_pcp->mnt_gets);
342 #else
343 mnt->mnt_count = 1;
344 mnt->mnt_writers = 0;
345 #endif
346
347 INIT_HLIST_NODE(&mnt->mnt_hash);
348 INIT_LIST_HEAD(&mnt->mnt_child);
349 INIT_LIST_HEAD(&mnt->mnt_mounts);
350 INIT_LIST_HEAD(&mnt->mnt_list);
351 INIT_LIST_HEAD(&mnt->mnt_expire);
352 INIT_LIST_HEAD(&mnt->mnt_share);
353 INIT_HLIST_HEAD(&mnt->mnt_slave_list);
354 INIT_HLIST_NODE(&mnt->mnt_slave);
355 INIT_HLIST_NODE(&mnt->mnt_mp_list);
356 INIT_HLIST_HEAD(&mnt->mnt_covers);
357 INIT_HLIST_NODE(&mnt->mnt_ns_visible);
358 #ifdef CONFIG_FSNOTIFY
359 INIT_LIST_HEAD(&mnt->to_notify);
360 #endif
361 RB_CLEAR_NODE(&mnt->mnt_node);
362 mnt->mnt.mnt_idmap = &nop_mnt_idmap;
363 }
364 return mnt;
365
366 #ifdef CONFIG_SMP
367 out_free_devname:
368 kfree_const(mnt->mnt_devname);
369 #endif
370 out_free_id:
371 mnt_free_id(mnt);
372 out_free_cache:
373 kfree(mnt->mnt_cover);
374 kmem_cache_free(mnt_cache, mnt);
375 return NULL;
376 }
377
378 /*
379 * Most r/o checks on a fs are for operations that take
380 * discrete amounts of time, like a write() or unlink().
381 * We must keep track of when those operations start
382 * (for permission checks) and when they end, so that
383 * we can determine when writes are able to occur to
384 * a filesystem.
385 */
386 /*
387 * __mnt_is_readonly: check whether a mount is read-only
388 * @mnt: the mount to check for its write status
389 *
390 * This shouldn't be used directly ouside of the VFS.
391 * It does not guarantee that the filesystem will stay
392 * r/w, just that it is right *now*. This can not and
393 * should not be used in place of IS_RDONLY(inode).
394 * mnt_want/drop_write() will _keep_ the filesystem
395 * r/w.
396 */
397 bool __mnt_is_readonly(const struct vfsmount *mnt)
398 {
399 return (mnt->mnt_flags & MNT_READONLY) || sb_rdonly(mnt->mnt_sb);
400 }
401 EXPORT_SYMBOL_GPL(__mnt_is_readonly);
402
403 static inline void mnt_inc_writers(struct mount *mnt)
404 {
405 #ifdef CONFIG_SMP
406 this_cpu_inc(mnt->mnt_pcp->mnt_writers);
407 #else
408 mnt->mnt_writers++;
409 #endif
410 }
411
412 static inline void mnt_dec_writers(struct mount *mnt)
413 {
414 #ifdef CONFIG_SMP
415 this_cpu_dec(mnt->mnt_pcp->mnt_writers);
416 #else
417 mnt->mnt_writers--;
418 #endif
419 }
420
421 static unsigned int mnt_get_writers(struct mount *mnt)
422 {
423 #ifdef CONFIG_SMP
424 unsigned int count = 0;
425 int cpu;
426
427 for_each_possible_cpu(cpu) {
428 count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers;
429 }
430
431 return count;
432 #else
433 return mnt->mnt_writers;
434 #endif
435 }
436
437 static int mnt_is_readonly(const struct vfsmount *mnt)
438 {
439 if (READ_ONCE(mnt->mnt_sb->s_readonly_remount))
440 return 1;
441 /*
442 * The barrier pairs with the barrier in sb_start_ro_state_change()
443 * making sure if we don't see s_readonly_remount set yet, we also will
444 * not see any superblock / mount flag changes done by remount.
445 * It also pairs with the barrier in sb_end_ro_state_change()
446 * assuring that if we see s_readonly_remount already cleared, we will
447 * see the values of superblock / mount flags updated by remount.
448 */
449 smp_rmb();
450 return __mnt_is_readonly(mnt);
451 }
452
453 /*
454 * Most r/o & frozen checks on a fs are for operations that take discrete
455 * amounts of time, like a write() or unlink(). We must keep track of when
456 * those operations start (for permission checks) and when they end, so that we
457 * can determine when writes are able to occur to a filesystem.
458 */
459 /**
460 * mnt_get_write_access - get write access to a mount without freeze protection
461 * @m: the mount on which to take a write
462 *
463 * This tells the low-level filesystem that a write is about to be performed to
464 * it, and makes sure that writes are allowed (mnt it read-write) before
465 * returning success. This operation does not protect against filesystem being
466 * frozen. When the write operation is finished, mnt_put_write_access() must be
467 * called. This is effectively a refcount.
468 */
469 int mnt_get_write_access(struct vfsmount *m)
470 {
471 struct mount *mnt = real_mount(m);
472 int ret = 0;
473
474 preempt_disable();
475 mnt_inc_writers(mnt);
476 /*
477 * The store to mnt_inc_writers must be visible before we pass
478 * WRITE_HOLD loop below, so that the slowpath can see our
479 * incremented count after it has set WRITE_HOLD.
480 */
481 smp_mb();
482 might_lock(&mount_lock.lock);
483 while (__test_write_hold(READ_ONCE(mnt->mnt_pprev_for_sb))) {
484 if (!IS_ENABLED(CONFIG_PREEMPT_RT)) {
485 cpu_relax();
486 } else {
487 /*
488 * This prevents priority inversion, if the task
489 * setting WRITE_HOLD got preempted on a remote
490 * CPU, and it prevents life lock if the task setting
491 * WRITE_HOLD has a lower priority and is bound to
492 * the same CPU as the task that is spinning here.
493 */
494 preempt_enable();
495 read_seqlock_excl(&mount_lock);
496 read_sequnlock_excl(&mount_lock);
497 preempt_disable();
498 }
499 }
500 /*
501 * The barrier pairs with the barrier sb_start_ro_state_change() making
502 * sure that if we see WRITE_HOLD cleared, we will also see
503 * s_readonly_remount set (or even SB_RDONLY / MNT_READONLY flags) in
504 * mnt_is_readonly() and bail in case we are racing with remount
505 * read-only.
506 */
507 smp_rmb();
508 if (mnt_is_readonly(m)) {
509 mnt_dec_writers(mnt);
510 ret = -EROFS;
511 }
512 preempt_enable();
513
514 return ret;
515 }
516 EXPORT_SYMBOL_GPL(mnt_get_write_access);
517
518 /**
519 * mnt_want_write - get write access to a mount
520 * @m: the mount on which to take a write
521 *
522 * This tells the low-level filesystem that a write is about to be performed to
523 * it, and makes sure that writes are allowed (mount is read-write, filesystem
524 * is not frozen) before returning success. When the write operation is
525 * finished, mnt_drop_write() must be called. This is effectively a refcount.
526 */
527 int mnt_want_write(struct vfsmount *m)
528 {
529 int ret;
530
531 sb_start_write(m->mnt_sb);
532 ret = mnt_get_write_access(m);
533 if (ret)
534 sb_end_write(m->mnt_sb);
535 return ret;
536 }
537 EXPORT_SYMBOL_GPL(mnt_want_write);
538
539 /**
540 * mnt_get_write_access_file - get write access to a file's mount
541 * @file: the file who's mount on which to take a write
542 *
543 * This is like mnt_get_write_access, but if @file is already open for write it
544 * skips incrementing mnt_writers (since the open file already has a reference)
545 * and instead only does the check for emergency r/o remounts. This must be
546 * paired with mnt_put_write_access_file.
547 */
548 int mnt_get_write_access_file(struct file *file)
549 {
550 if (file->f_mode & FMODE_WRITER) {
551 /*
552 * Superblock may have become readonly while there are still
553 * writable fd's, e.g. due to a fs error with errors=remount-ro
554 */
555 if (__mnt_is_readonly(file->f_path.mnt))
556 return -EROFS;
557 return 0;
558 }
559 return mnt_get_write_access(file->f_path.mnt);
560 }
561
562 /**
563 * mnt_want_write_file - get write access to a file's mount
564 * @file: the file who's mount on which to take a write
565 *
566 * This is like mnt_want_write, but if the file is already open for writing it
567 * skips incrementing mnt_writers (since the open file already has a reference)
568 * and instead only does the freeze protection and the check for emergency r/o
569 * remounts. This must be paired with mnt_drop_write_file.
570 */
571 int mnt_want_write_file(struct file *file)
572 {
573 int ret;
574
575 sb_start_write(file_inode(file)->i_sb);
576 ret = mnt_get_write_access_file(file);
577 if (ret)
578 sb_end_write(file_inode(file)->i_sb);
579 return ret;
580 }
581 EXPORT_SYMBOL_GPL(mnt_want_write_file);
582
583 /**
584 * mnt_put_write_access - give up write access to a mount
585 * @mnt: the mount on which to give up write access
586 *
587 * Tells the low-level filesystem that we are done
588 * performing writes to it. Must be matched with
589 * mnt_get_write_access() call above.
590 */
591 void mnt_put_write_access(struct vfsmount *mnt)
592 {
593 preempt_disable();
594 mnt_dec_writers(real_mount(mnt));
595 preempt_enable();
596 }
597 EXPORT_SYMBOL_GPL(mnt_put_write_access);
598
599 /**
600 * mnt_drop_write - give up write access to a mount
601 * @mnt: the mount on which to give up write access
602 *
603 * Tells the low-level filesystem that we are done performing writes to it and
604 * also allows filesystem to be frozen again. Must be matched with
605 * mnt_want_write() call above.
606 */
607 void mnt_drop_write(struct vfsmount *mnt)
608 {
609 mnt_put_write_access(mnt);
610 sb_end_write(mnt->mnt_sb);
611 }
612 EXPORT_SYMBOL_GPL(mnt_drop_write);
613
614 void mnt_put_write_access_file(struct file *file)
615 {
616 if (!(file->f_mode & FMODE_WRITER))
617 mnt_put_write_access(file->f_path.mnt);
618 }
619
620 void mnt_drop_write_file(struct file *file)
621 {
622 mnt_put_write_access_file(file);
623 sb_end_write(file_inode(file)->i_sb);
624 }
625 EXPORT_SYMBOL(mnt_drop_write_file);
626
627 /**
628 * mnt_hold_writers - prevent write access to the given mount
629 * @mnt: mnt to prevent write access to
630 *
631 * Prevents write access to @mnt if there are no active writers for @mnt.
632 * This function needs to be called and return successfully before changing
633 * properties of @mnt that need to remain stable for callers with write access
634 * to @mnt.
635 *
636 * After this functions has been called successfully callers must pair it with
637 * a call to mnt_unhold_writers() in order to stop preventing write access to
638 * @mnt.
639 *
640 * Context: This function expects to be in mount_locked_reader scope serializing
641 * setting WRITE_HOLD.
642 * Return: On success 0 is returned.
643 * On error, -EBUSY is returned.
644 */
645 static inline int mnt_hold_writers(struct mount *mnt)
646 {
647 set_write_hold(mnt);
648 /*
649 * After storing WRITE_HOLD, we'll read the counters. This store
650 * should be visible before we do.
651 */
652 smp_mb();
653
654 /*
655 * With writers on hold, if this value is zero, then there are
656 * definitely no active writers (although held writers may subsequently
657 * increment the count, they'll have to wait, and decrement it after
658 * seeing MNT_READONLY).
659 *
660 * It is OK to have counter incremented on one CPU and decremented on
661 * another: the sum will add up correctly. The danger would be when we
662 * sum up each counter, if we read a counter before it is incremented,
663 * but then read another CPU's count which it has been subsequently
664 * decremented from -- we would see more decrements than we should.
665 * WRITE_HOLD protects against this scenario, because
666 * mnt_want_write first increments count, then smp_mb, then spins on
667 * WRITE_HOLD, so it can't be decremented by another CPU while
668 * we're counting up here.
669 */
670 if (mnt_get_writers(mnt) > 0)
671 return -EBUSY;
672
673 return 0;
674 }
675
676 /**
677 * mnt_unhold_writers - stop preventing write access to the given mount
678 * @mnt: mnt to stop preventing write access to
679 *
680 * Stop preventing write access to @mnt allowing callers to gain write access
681 * to @mnt again.
682 *
683 * This function can only be called after a call to mnt_hold_writers().
684 *
685 * Context: This function expects to be in the same mount_locked_reader scope
686 * as the matching mnt_hold_writers().
687 */
688 static inline void mnt_unhold_writers(struct mount *mnt)
689 {
690 if (!test_write_hold(mnt))
691 return;
692 /*
693 * MNT_READONLY must become visible before ~WRITE_HOLD, so writers
694 * that become unheld will see MNT_READONLY.
695 */
696 smp_wmb();
697 clear_write_hold(mnt);
698 }
699
700 static inline void mnt_del_instance(struct mount *m)
701 {
702 struct mount **p = m->mnt_pprev_for_sb;
703 struct mount *next = m->mnt_next_for_sb;
704
705 if (next)
706 next->mnt_pprev_for_sb = p;
707 *p = next;
708 }
709
710 static inline void mnt_add_instance(struct mount *m, struct super_block *s)
711 {
712 struct mount *first = s->s_mounts;
713
714 if (first)
715 first->mnt_pprev_for_sb = &m->mnt_next_for_sb;
716 m->mnt_next_for_sb = first;
717 m->mnt_pprev_for_sb = &s->s_mounts;
718 s->s_mounts = m;
719 }
720
721 static int mnt_make_readonly(struct mount *mnt)
722 {
723 int ret;
724
725 ret = mnt_hold_writers(mnt);
726 if (!ret)
727 mnt->mnt.mnt_flags |= MNT_READONLY;
728 mnt_unhold_writers(mnt);
729 return ret;
730 }
731
732 int sb_prepare_remount_readonly(struct super_block *sb)
733 {
734 int err = 0;
735
736 /* Racy optimization. Recheck the counter under WRITE_HOLD */
737 if (atomic_long_read(&sb->s_remove_count))
738 return -EBUSY;
739
740 guard(mount_locked_reader)();
741
742 for (struct mount *m = sb->s_mounts; m; m = m->mnt_next_for_sb) {
743 if (!(m->mnt.mnt_flags & MNT_READONLY)) {
744 err = mnt_hold_writers(m);
745 if (err)
746 break;
747 }
748 }
749 if (!err && atomic_long_read(&sb->s_remove_count))
750 err = -EBUSY;
751
752 if (!err)
753 sb_start_ro_state_change(sb);
754 for (struct mount *m = sb->s_mounts; m; m = m->mnt_next_for_sb) {
755 if (test_write_hold(m))
756 clear_write_hold(m);
757 }
758
759 return err;
760 }
761
762 static void free_vfsmnt(struct mount *mnt)
763 {
764 mnt_idmap_put(mnt_idmap(&mnt->mnt));
765 /* NULL if it left it behind */
766 kfree(mnt->mnt_cover);
767 kfree_const(mnt->mnt_devname);
768 #ifdef CONFIG_SMP
769 free_percpu(mnt->mnt_pcp);
770 #endif
771 kmem_cache_free(mnt_cache, mnt);
772 }
773
774 static void delayed_free_vfsmnt(struct rcu_head *head)
775 {
776 free_vfsmnt(container_of(head, struct mount, mnt_rcu));
777 }
778
779 /* call under rcu_read_lock */
780 int __legitimize_mnt(struct vfsmount *bastard, unsigned seq)
781 {
782 struct mount *mnt;
783 if (read_seqretry(&mount_lock, seq))
784 return 1;
785 if (bastard == NULL)
786 return 0;
787 mnt = real_mount(bastard);
788 mnt_inc_count(mnt);
789 smp_mb(); /* pairs with the smp_mb() in mnt_get_count() */
790 if (likely(!read_seqretry(&mount_lock, seq)))
791 return 0;
792 lock_mount_hash();
793 if (unlikely(bastard->mnt_flags & (MNT_SYNC_UMOUNT | MNT_DOOMED))) {
794 mnt_dec_count(mnt);
795 unlock_mount_hash();
796 return 1;
797 }
798 unlock_mount_hash();
799 /* caller will mntput() */
800 return -1;
801 }
802
803 /* call under rcu_read_lock */
804 static bool legitimize_mnt(struct vfsmount *bastard, unsigned seq)
805 {
806 int res = __legitimize_mnt(bastard, seq);
807 if (likely(!res))
808 return true;
809 if (unlikely(res < 0)) {
810 rcu_read_unlock();
811 mntput(bastard);
812 rcu_read_lock();
813 }
814 return false;
815 }
816
817 /**
818 * __lookup_mnt - mount hash lookup
819 * @mnt: parent mount
820 * @dentry: dentry of mountpoint
821 *
822 * If @mnt has a child mount @c mounted on @dentry find and return it.
823 * If @mnt is unmounted and a child that was unmounted with it left its
824 * cover behind at @dentry, return the stand-in for it instead: knullfs
825 * for a directory, its regular file for anything else.
826 * Caller must either hold the spinlock component of @mount_lock or
827 * hold rcu_read_lock(), sample the seqcount component before the call
828 * and recheck it afterwards.
829 *
830 * Return: The child of @mnt mounted on @dentry, a stand-in or %NULL.
831 */
832 struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
833 {
834 struct hlist_head *head = m_hash(mnt, dentry);
835 struct mnt_cover *cover;
836 struct mount *p;
837
838 hlist_for_each_entry_rcu(p, head, mnt_hash)
839 if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry)
840 return p;
841 /* an unmounted mount keeps the covers its unmounted children left */
842 /* a lockless caller rechecks mount_lock after a miss, a stale flag is harmless */
843 if (unlikely(data_race(mnt->mnt_flags) & MNT_UMOUNT)) {
844 hlist_for_each_entry_rcu(cover, &real_mount(mnt)->mnt_covers, node)
845 if (cover->dentry == dentry)
846 return real_mount(d_is_dir(dentry) ? knullfs : knullfs_file);
847 }
848 return NULL;
849 }
850
851 /**
852 * lookup_mnt - Return the child mount mounted at given location
853 * @path: location in the namespace
854 *
855 * Acquires and returns a new reference to mount at given location
856 * or %NULL if nothing is mounted there.
857 */
858 struct vfsmount *lookup_mnt(const struct path *path)
859 {
860 struct mount *child_mnt;
861 struct vfsmount *m;
862 unsigned seq;
863
864 rcu_read_lock();
865 do {
866 seq = read_seqbegin(&mount_lock);
867 child_mnt = __lookup_mnt(path->mnt, path->dentry);
868 m = child_mnt ? &child_mnt->mnt : NULL;
869 } while (!legitimize_mnt(m, seq));
870 rcu_read_unlock();
871 return m;
872 }
873
874 /*
875 * __is_local_mountpoint - Test to see if dentry is a mountpoint in the
876 * current mount namespace.
877 *
878 * The common case is dentries are not mountpoints at all and that
879 * test is handled inline. For the slow case when we are actually
880 * dealing with a mountpoint of some kind, walk through all of the
881 * mounts in the current mount namespace and test to see if the dentry
882 * is a mountpoint.
883 *
884 * The mount_hashtable is not usable in the context because we
885 * need to identify all mounts that may be in the current mount
886 * namespace not just a mount that happens to have some specified
887 * parent mount.
888 */
889 bool __is_local_mountpoint(const struct dentry *dentry)
890 {
891 struct mnt_namespace *ns = current->nsproxy->mnt_ns;
892 struct mount *mnt, *n;
893
894 guard(namespace_shared)();
895
896 rbtree_postorder_for_each_entry_safe(mnt, n, &ns->mounts, mnt_node)
897 if (mnt->mnt_mountpoint == dentry)
898 return true;
899
900 return false;
901 }
902
903 struct pinned_mountpoint {
904 struct hlist_node node;
905 struct mountpoint *mp;
906 struct mount *parent;
907 };
908
909 static bool lookup_mountpoint(struct dentry *dentry, struct pinned_mountpoint *m)
910 {
911 struct hlist_head *chain = mp_hash(dentry);
912 struct mountpoint *mp;
913
914 hlist_for_each_entry(mp, chain, m_hash) {
915 if (mp->m_dentry == dentry) {
916 hlist_add_head(&m->node, &mp->m_list);
917 m->mp = mp;
918 return true;
919 }
920 }
921 return false;
922 }
923
924 static int get_mountpoint(struct dentry *dentry, struct pinned_mountpoint *m)
925 {
926 struct mountpoint *mp __free(kfree) = NULL;
927 bool found;
928 int ret;
929
930 if (d_mountpoint(dentry)) {
931 /* might be worth a WARN_ON() */
932 if (d_unlinked(dentry))
933 return -ENOENT;
934 mountpoint:
935 read_seqlock_excl(&mount_lock);
936 found = lookup_mountpoint(dentry, m);
937 read_sequnlock_excl(&mount_lock);
938 if (found)
939 return 0;
940 }
941
942 if (!mp)
943 mp = kmalloc_obj(struct mountpoint);
944 if (!mp)
945 return -ENOMEM;
946
947 /* Exactly one processes may set d_mounted */
948 ret = d_set_mounted(dentry);
949
950 /* Someone else set d_mounted? */
951 if (ret == -EBUSY)
952 goto mountpoint;
953
954 /* The dentry is not available as a mountpoint? */
955 if (ret)
956 return ret;
957
958 /* Add the new mountpoint to the hash table */
959 read_seqlock_excl(&mount_lock);
960 mp->m_dentry = dget(dentry);
961 hlist_add_head(&mp->m_hash, mp_hash(dentry));
962 INIT_HLIST_HEAD(&mp->m_list);
963 INIT_HLIST_HEAD(&mp->m_covers);
964 hlist_add_head(&m->node, &mp->m_list);
965 m->mp = no_free_ptr(mp);
966 read_sequnlock_excl(&mount_lock);
967 return 0;
968 }
969
970 /*
971 * vfsmount lock must be held. Additionally, the caller is responsible
972 * for serializing calls for given disposal list.
973 */
974 static void maybe_free_mountpoint(struct mountpoint *mp, struct list_head *list)
975 {
976 if (hlist_empty(&mp->m_list) && hlist_empty(&mp->m_covers)) {
977 struct dentry *dentry = mp->m_dentry;
978 spin_lock(&dentry->d_lock);
979 dentry->d_flags &= ~DCACHE_MOUNTED;
980 spin_unlock(&dentry->d_lock);
981 dput_to_list(dentry, list);
982 hlist_del(&mp->m_hash);
983 kfree(mp);
984 }
985 }
986
987 /*
988 * locks: mount_lock [read_seqlock_excl], namespace_sem [excl]
989 */
990 static void unpin_mountpoint(struct pinned_mountpoint *m)
991 {
992 if (m->mp) {
993 hlist_del(&m->node);
994 maybe_free_mountpoint(m->mp, &ex_mountpoints);
995 }
996 }
997
998 static inline int check_mnt(const struct mount *mnt)
999 {
1000 return mnt->mnt_ns == current->nsproxy->mnt_ns;
1001 }
1002
1003 static inline bool check_anonymous_mnt(struct mount *mnt)
1004 {
1005 u64 seq;
1006
1007 if (!is_anon_ns(mnt->mnt_ns))
1008 return false;
1009
1010 seq = mnt->mnt_ns->seq_origin;
1011 return !seq || (seq == current->nsproxy->mnt_ns->ns.ns_id);
1012 }
1013
1014 /*
1015 * vfsmount lock must be held for write
1016 */
1017 static void touch_mnt_namespace(struct mnt_namespace *ns)
1018 {
1019 if (ns) {
1020 ns->event = ++event;
1021 wake_up_interruptible(&ns->poll);
1022 }
1023 }
1024
1025 /*
1026 * vfsmount lock must be held for write
1027 */
1028 static void __touch_mnt_namespace(struct mnt_namespace *ns)
1029 {
1030 if (ns && ns->event != event) {
1031 ns->event = event;
1032 wake_up_interruptible(&ns->poll);
1033 }
1034 }
1035
1036 /*
1037 * locks: mount_lock[write_seqlock]
1038 */
1039 static void __umount_mnt(struct mount *mnt, struct list_head *shrink_list)
1040 {
1041 struct mountpoint *mp;
1042 struct mount *parent = mnt->mnt_parent;
1043 if (unlikely(parent->overmount == mnt))
1044 parent->overmount = NULL;
1045 mnt->mnt_parent = mnt;
1046 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
1047 list_del_init(&mnt->mnt_child);
1048 hlist_del_init_rcu(&mnt->mnt_hash);
1049 hlist_del_init(&mnt->mnt_mp_list);
1050 mp = mnt->mnt_mp;
1051 mnt->mnt_mp = NULL;
1052 maybe_free_mountpoint(mp, shrink_list);
1053 }
1054
1055 /*
1056 * locks: mount_lock[write_seqlock], namespace_sem[excl] (for ex_mountpoints)
1057 */
1058 static void umount_mnt(struct mount *mnt)
1059 {
1060 __umount_mnt(mnt, &ex_mountpoints);
1061 }
1062
1063 /*
1064 * @mnt is unmounted together with its parent and would have stayed attached
1065 * to it. Leave its cover behind before it is detached so that a lookup on the
1066 * parent at the mountpoint keeps finding a mount instead of what @mnt covered.
1067 *
1068 * locks: mount_lock[write_seqlock]
1069 */
1070 static void leave_cover(struct mount *mnt)
1071 {
1072 struct mnt_cover *cover = mnt->mnt_cover;
1073
1074 mnt->mnt_cover = NULL;
1075 cover->dentry = mnt->mnt_mountpoint;
1076 cover->mp = mnt->mnt_mp;
1077 /* keeps the mountpoint once @mnt has let go of it */
1078 hlist_add_head(&cover->pin, &cover->mp->m_covers);
1079 hlist_add_head_rcu(&cover->node, &mnt->mnt_parent->mnt_covers);
1080 }
1081
1082 /*
1083 * locks: mount_lock[write_seqlock]
1084 */
1085 static void drop_cover(struct mnt_cover *cover, struct list_head *shrink_list)
1086 {
1087 hlist_del_rcu(&cover->node);
1088 hlist_del(&cover->pin);
1089 maybe_free_mountpoint(cover->mp, shrink_list);
1090 kfree_rcu(cover, rcu);
1091 }
1092
1093 /*
1094 * vfsmount lock must be held for write
1095 */
1096 void mnt_set_mountpoint(struct mount *mnt,
1097 struct mountpoint *mp,
1098 struct mount *child_mnt)
1099 {
1100 child_mnt->mnt_mountpoint = mp->m_dentry;
1101 child_mnt->mnt_parent = mnt;
1102 child_mnt->mnt_mp = mp;
1103 hlist_add_head(&child_mnt->mnt_mp_list, &mp->m_list);
1104 }
1105
1106 static void make_visible(struct mount *mnt)
1107 {
1108 struct mount *parent = mnt->mnt_parent;
1109 if (unlikely(mnt->mnt_mountpoint == parent->mnt.mnt_root))
1110 parent->overmount = mnt;
1111 hlist_add_head_rcu(&mnt->mnt_hash,
1112 m_hash(&parent->mnt, mnt->mnt_mountpoint));
1113 list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
1114 }
1115
1116 /**
1117 * attach_mnt - mount a mount, attach to @mount_hashtable and parent's
1118 * list of child mounts
1119 * @parent: the parent
1120 * @mnt: the new mount
1121 * @mp: the new mountpoint
1122 *
1123 * Mount @mnt at @mp on @parent. Then attach @mnt
1124 * to @parent's child mount list and to @mount_hashtable.
1125 *
1126 * Note, when make_visible() is called @mnt->mnt_parent already points
1127 * to the correct parent.
1128 *
1129 * Context: This function expects namespace_lock() and lock_mount_hash()
1130 * to have been acquired in that order.
1131 */
1132 static void attach_mnt(struct mount *mnt, struct mount *parent,
1133 struct mountpoint *mp)
1134 {
1135 mnt_set_mountpoint(parent, mp, mnt);
1136 make_visible(mnt);
1137 }
1138
1139 void mnt_change_mountpoint(struct mount *parent, struct mountpoint *mp, struct mount *mnt)
1140 {
1141 struct mountpoint *old_mp = mnt->mnt_mp;
1142 struct mount *old_parent = mnt->mnt_parent;
1143
1144 if (old_parent->overmount == mnt)
1145 old_parent->overmount = NULL;
1146 list_del_init(&mnt->mnt_child);
1147 hlist_del_init(&mnt->mnt_mp_list);
1148 hlist_del_init_rcu(&mnt->mnt_hash);
1149
1150 attach_mnt(mnt, parent, mp);
1151
1152 maybe_free_mountpoint(old_mp, &ex_mountpoints);
1153 }
1154
1155 static inline struct mount *node_to_mount(struct rb_node *node)
1156 {
1157 return node ? rb_entry(node, struct mount, mnt_node) : NULL;
1158 }
1159
1160 static void mnt_add_to_ns(struct mnt_namespace *ns, struct mount *mnt)
1161 {
1162 struct rb_node **link = &ns->mounts.rb_node;
1163 struct rb_node *parent = NULL;
1164 bool mnt_first_node = true, mnt_last_node = true;
1165
1166 WARN_ON(mnt_ns_attached(mnt));
1167 WRITE_ONCE(mnt->mnt_ns, ns);
1168 while (*link) {
1169 parent = *link;
1170 if (mnt->mnt_id_unique < node_to_mount(parent)->mnt_id_unique) {
1171 link = &parent->rb_left;
1172 mnt_last_node = false;
1173 } else {
1174 link = &parent->rb_right;
1175 mnt_first_node = false;
1176 }
1177 }
1178
1179 if (mnt_last_node)
1180 ns->mnt_last_node = &mnt->mnt_node;
1181 if (mnt_first_node)
1182 ns->mnt_first_node = &mnt->mnt_node;
1183 rb_link_node(&mnt->mnt_node, parent, link);
1184 rb_insert_color(&mnt->mnt_node, &ns->mounts);
1185
1186 if ((mnt->mnt.mnt_sb->s_type->fs_flags & FS_USERNS_MOUNT_RESTRICTED) &&
1187 mnt->mnt.mnt_root == mnt->mnt.mnt_sb->s_root)
1188 hlist_add_head(&mnt->mnt_ns_visible, &ns->mnt_visible_mounts);
1189
1190 mnt_notify_add(mnt);
1191 }
1192
1193 static struct mount *next_mnt(struct mount *p, struct mount *root)
1194 {
1195 struct list_head *next = p->mnt_mounts.next;
1196 if (next == &p->mnt_mounts) {
1197 while (1) {
1198 if (p == root)
1199 return NULL;
1200 next = p->mnt_child.next;
1201 if (next != &p->mnt_parent->mnt_mounts)
1202 break;
1203 p = p->mnt_parent;
1204 }
1205 }
1206 return list_entry(next, struct mount, mnt_child);
1207 }
1208
1209 static struct mount *skip_mnt_tree(struct mount *p)
1210 {
1211 struct list_head *prev = p->mnt_mounts.prev;
1212 while (prev != &p->mnt_mounts) {
1213 p = list_entry(prev, struct mount, mnt_child);
1214 prev = p->mnt_mounts.prev;
1215 }
1216 return p;
1217 }
1218
1219 /*
1220 * vfsmount lock must be held for write
1221 */
1222 static void commit_tree(struct mount *mnt)
1223 {
1224 struct mnt_namespace *n = mnt->mnt_parent->mnt_ns;
1225
1226 if (!mnt_ns_attached(mnt)) {
1227 for (struct mount *m = mnt; m; m = next_mnt(m, mnt))
1228 mnt_add_to_ns(n, m);
1229 n->nr_mounts += n->pending_mounts;
1230 n->pending_mounts = 0;
1231 }
1232
1233 make_visible(mnt);
1234 touch_mnt_namespace(n);
1235 }
1236
1237 static void setup_mnt(struct mount *m, struct dentry *root)
1238 {
1239 struct super_block *s = root->d_sb;
1240
1241 atomic_inc(&s->s_active);
1242 m->mnt.mnt_sb = s;
1243 m->mnt.mnt_root = dget(root);
1244 m->mnt_mountpoint = m->mnt.mnt_root;
1245 m->mnt_parent = m;
1246
1247 guard(mount_locked_reader)();
1248 mnt_add_instance(m, s);
1249 }
1250
1251 /**
1252 * vfs_create_mount - Create a mount for a configured superblock
1253 * @fc: The configuration context with the superblock attached
1254 *
1255 * Create a mount to an already configured superblock. If necessary, the
1256 * caller should invoke vfs_get_tree() before calling this.
1257 *
1258 * Note that this does not attach the mount to anything.
1259 */
1260 struct vfsmount *vfs_create_mount(struct fs_context *fc)
1261 {
1262 struct mount *mnt;
1263
1264 if (!fc->root)
1265 return ERR_PTR(-EINVAL);
1266
1267 mnt = alloc_vfsmnt(fc->source);
1268 if (!mnt)
1269 return ERR_PTR(-ENOMEM);
1270
1271 if (fc->sb_flags & SB_KERNMOUNT)
1272 mnt->mnt.mnt_flags = MNT_INTERNAL;
1273
1274 setup_mnt(mnt, fc->root);
1275
1276 return &mnt->mnt;
1277 }
1278 EXPORT_SYMBOL(vfs_create_mount);
1279
1280 struct vfsmount *fc_mount(struct fs_context *fc)
1281 {
1282 int err = vfs_get_tree(fc);
1283 if (!err) {
1284 up_write(&fc->root->d_sb->s_umount);
1285 return vfs_create_mount(fc);
1286 }
1287 return ERR_PTR(err);
1288 }
1289 EXPORT_SYMBOL(fc_mount);
1290
1291 struct vfsmount *fc_mount_longterm(struct fs_context *fc)
1292 {
1293 struct vfsmount *mnt = fc_mount(fc);
1294 if (!IS_ERR(mnt))
1295 real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL;
1296 return mnt;
1297 }
1298 EXPORT_SYMBOL(fc_mount_longterm);
1299
1300 struct vfsmount *vfs_kern_mount(struct file_system_type *type,
1301 int flags, const char *name,
1302 void *data)
1303 {
1304 struct fs_context *fc;
1305 struct vfsmount *mnt;
1306 int ret = 0;
1307
1308 if (!type)
1309 return ERR_PTR(-EINVAL);
1310
1311 fc = fs_context_for_mount(type, flags);
1312 if (IS_ERR(fc))
1313 return ERR_CAST(fc);
1314
1315 if (name)
1316 ret = vfs_parse_fs_string(fc, "source", name);
1317 if (!ret)
1318 ret = parse_monolithic_mount_data(fc, data);
1319 if (!ret)
1320 mnt = fc_mount(fc);
1321 else
1322 mnt = ERR_PTR(ret);
1323
1324 put_fs_context(fc);
1325 return mnt;
1326 }
1327 EXPORT_SYMBOL_GPL(vfs_kern_mount);
1328
1329 static struct mount *clone_mnt(struct mount *old, struct dentry *root,
1330 int flag)
1331 {
1332 struct mount *mnt;
1333 int err;
1334
1335 mnt = alloc_vfsmnt(old->mnt_devname);
1336 if (!mnt)
1337 return ERR_PTR(-ENOMEM);
1338
1339 mnt->mnt.mnt_flags = READ_ONCE(old->mnt.mnt_flags) &
1340 ~MNT_INTERNAL_FLAGS;
1341 mnt->mnt_t_flags = old->mnt_t_flags & T_UNBINDABLE;
1342
1343 if (flag & (CL_SLAVE | CL_PRIVATE))
1344 mnt->mnt_group_id = 0; /* not a peer of original */
1345 else
1346 mnt->mnt_group_id = old->mnt_group_id;
1347
1348 if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
1349 err = mnt_alloc_group_id(mnt);
1350 if (err)
1351 goto out_free;
1352 }
1353
1354 if (mnt->mnt_group_id)
1355 set_mnt_shared(mnt);
1356
1357 mnt->mnt.mnt_idmap = mnt_idmap_get(mnt_idmap(&old->mnt));
1358
1359 setup_mnt(mnt, root);
1360
1361 if (flag & CL_PRIVATE) // we are done with it
1362 return mnt;
1363
1364 if (peers(mnt, old))
1365 list_add(&mnt->mnt_share, &old->mnt_share);
1366
1367 if ((flag & CL_SLAVE) && old->mnt_group_id) {
1368 hlist_add_head(&mnt->mnt_slave, &old->mnt_slave_list);
1369 mnt->mnt_master = old;
1370 } else if (IS_MNT_SLAVE(old)) {
1371 hlist_add_behind(&mnt->mnt_slave, &old->mnt_slave);
1372 mnt->mnt_master = old->mnt_master;
1373 }
1374 return mnt;
1375
1376 out_free:
1377 mnt_free_id(mnt);
1378 free_vfsmnt(mnt);
1379 return ERR_PTR(err);
1380 }
1381
1382 static void cleanup_mnt(struct mount *mnt)
1383 {
1384 /*
1385 * The warning here probably indicates that somebody messed
1386 * up a mnt_want/drop_write() pair. If this happens, the
1387 * filesystem was probably unable to make r/w->r/o transitions.
1388 * The locking used to deal with mnt_count decrement provides barriers,
1389 * so mnt_get_writers() below is safe.
1390 */
1391 WARN_ON(mnt_get_writers(mnt));
1392 if (unlikely(mnt->mnt_pins.first))
1393 mnt_pin_kill(mnt);
1394 fsnotify_vfsmount_delete(&mnt->mnt);
1395 dput(mnt->mnt.mnt_root);
1396 deactivate_super(mnt->mnt.mnt_sb);
1397 mnt_free_id(mnt);
1398 call_rcu(&mnt->mnt_rcu, delayed_free_vfsmnt);
1399 }
1400
1401 static void __cleanup_mnt(struct rcu_head *head)
1402 {
1403 cleanup_mnt(container_of(head, struct mount, mnt_rcu));
1404 }
1405
1406 static LLIST_HEAD(delayed_mntput_list);
1407 static void delayed_mntput(struct work_struct *unused)
1408 {
1409 struct llist_node *node = llist_del_all(&delayed_mntput_list);
1410 struct mount *m, *t;
1411
1412 llist_for_each_entry_safe(m, t, node, mnt_llist)
1413 cleanup_mnt(m);
1414 }
1415 static DECLARE_DELAYED_WORK(delayed_mntput_work, delayed_mntput);
1416
1417 static void noinline mntput_no_expire_slowpath(struct mount *mnt)
1418 {
1419 struct mnt_cover *cover;
1420 struct hlist_node *n;
1421 LIST_HEAD(list);
1422 int count;
1423
1424 VFS_BUG_ON(mnt->mnt_ns);
1425 lock_mount_hash();
1426 mnt_dec_count(mnt);
1427 count = mnt_get_count(mnt);
1428 if (count != 0) {
1429 WARN_ON(count < 0);
1430 rcu_read_unlock();
1431 unlock_mount_hash();
1432 return;
1433 }
1434 if (unlikely(mnt->mnt.mnt_flags & MNT_DOOMED)) {
1435 rcu_read_unlock();
1436 unlock_mount_hash();
1437 return;
1438 }
1439 mnt->mnt.mnt_flags |= MNT_DOOMED;
1440 rcu_read_unlock();
1441
1442 mnt_del_instance(mnt);
1443 if (unlikely(!list_empty(&mnt->mnt_expire)))
1444 list_del(&mnt->mnt_expire);
1445
1446 /* nothing stays attached to an unmounted mount */
1447 VFS_WARN_ON_ONCE(!list_empty(&mnt->mnt_mounts));
1448 hlist_for_each_entry_safe(cover, n, &mnt->mnt_covers, node)
1449 drop_cover(cover, &list);
1450 unlock_mount_hash();
1451 shrink_dentry_list(&list);
1452
1453 if (likely(!(mnt->mnt.mnt_flags & MNT_INTERNAL))) {
1454 struct task_struct *task = current;
1455 if (likely(!(task->flags & PF_KTHREAD))) {
1456 init_task_work(&mnt->mnt_rcu, __cleanup_mnt);
1457 if (!task_work_add(task, &mnt->mnt_rcu, TWA_RESUME))
1458 return;
1459 }
1460 if (llist_add(&mnt->mnt_llist, &delayed_mntput_list))
1461 schedule_delayed_work(&delayed_mntput_work, 1);
1462 return;
1463 }
1464 cleanup_mnt(mnt);
1465 }
1466
1467 static void mntput_no_expire(struct mount *mnt)
1468 {
1469 rcu_read_lock();
1470 if (likely(READ_ONCE(mnt->mnt_ns))) {
1471 /*
1472 * Since we don't do lock_mount_hash() here,
1473 * ->mnt_ns can change under us. However, if it's
1474 * non-NULL, then there's a reference that won't
1475 * be dropped until after an RCU delay done after
1476 * turning ->mnt_ns NULL. So if we observe it
1477 * non-NULL under rcu_read_lock(), the reference
1478 * we are dropping is not the final one.
1479 */
1480 smp_wmb(); /* pairs with the smp_mb() in mnt_get_count() */
1481 mnt_dec_count(mnt);
1482 rcu_read_unlock();
1483 return;
1484 }
1485 mntput_no_expire_slowpath(mnt);
1486 }
1487
1488 void mntput(struct vfsmount *mnt)
1489 {
1490 if (mnt) {
1491 struct mount *m = real_mount(mnt);
1492 /* avoid cacheline pingpong */
1493 if (unlikely(m->mnt_expiry_mark))
1494 WRITE_ONCE(m->mnt_expiry_mark, 0);
1495 mntput_no_expire(m);
1496 }
1497 }
1498 EXPORT_SYMBOL(mntput);
1499
1500 struct vfsmount *mntget(struct vfsmount *mnt)
1501 {
1502 if (mnt)
1503 mnt_inc_count(real_mount(mnt));
1504 return mnt;
1505 }
1506 EXPORT_SYMBOL(mntget);
1507
1508 /*
1509 * Make a mount point inaccessible to new lookups.
1510 * Because there may still be current users, the caller MUST WAIT
1511 * for an RCU grace period before destroying the mount point.
1512 */
1513 void mnt_make_shortterm(struct vfsmount *mnt)
1514 {
1515 if (mnt)
1516 WRITE_ONCE(real_mount(mnt)->mnt_ns, NULL);
1517 }
1518
1519 /**
1520 * path_is_mountpoint() - Check if path is a mount in the current namespace.
1521 * @path: path to check
1522 *
1523 * d_mountpoint() can only be used reliably to establish if a dentry is
1524 * not mounted in any namespace and that common case is handled inline.
1525 * d_mountpoint() isn't aware of the possibility there may be multiple
1526 * mounts using a given dentry in a different namespace. This function
1527 * checks if the passed in path is a mountpoint rather than the dentry
1528 * alone.
1529 */
1530 bool path_is_mountpoint(const struct path *path)
1531 {
1532 unsigned seq;
1533 bool res;
1534
1535 if (!d_mountpoint(path->dentry))
1536 return false;
1537
1538 rcu_read_lock();
1539 do {
1540 seq = read_seqbegin(&mount_lock);
1541 res = __path_is_mountpoint(path);
1542 } while (read_seqretry(&mount_lock, seq));
1543 rcu_read_unlock();
1544
1545 return res;
1546 }
1547 EXPORT_SYMBOL(path_is_mountpoint);
1548
1549 struct vfsmount *mnt_clone_internal(const struct path *path)
1550 {
1551 struct mount *p;
1552 p = clone_mnt(real_mount(path->mnt), path->dentry, CL_PRIVATE);
1553 if (IS_ERR(p))
1554 return ERR_CAST(p);
1555 p->mnt.mnt_flags |= MNT_INTERNAL;
1556 return &p->mnt;
1557 }
1558
1559 /*
1560 * Returns the mount which either has the specified mnt_id, or has the next
1561 * smallest id afer the specified one.
1562 */
1563 static struct mount *mnt_find_id_at(struct mnt_namespace *ns, u64 mnt_id)
1564 {
1565 struct rb_node *node = ns->mounts.rb_node;
1566 struct mount *ret = NULL;
1567
1568 while (node) {
1569 struct mount *m = node_to_mount(node);
1570
1571 if (mnt_id <= m->mnt_id_unique) {
1572 ret = node_to_mount(node);
1573 if (mnt_id == m->mnt_id_unique)
1574 break;
1575 node = node->rb_left;
1576 } else {
1577 node = node->rb_right;
1578 }
1579 }
1580 return ret;
1581 }
1582
1583 /*
1584 * Returns the mount which either has the specified mnt_id, or has the next
1585 * greater id before the specified one.
1586 */
1587 static struct mount *mnt_find_id_at_reverse(struct mnt_namespace *ns, u64 mnt_id)
1588 {
1589 struct rb_node *node = ns->mounts.rb_node;
1590 struct mount *ret = NULL;
1591
1592 while (node) {
1593 struct mount *m = node_to_mount(node);
1594
1595 if (mnt_id >= m->mnt_id_unique) {
1596 ret = node_to_mount(node);
1597 if (mnt_id == m->mnt_id_unique)
1598 break;
1599 node = node->rb_right;
1600 } else {
1601 node = node->rb_left;
1602 }
1603 }
1604 return ret;
1605 }
1606
1607 #ifdef CONFIG_PROC_FS
1608
1609 /* iterator; we want it to have access to namespace_sem, thus here... */
1610 static void *m_start(struct seq_file *m, loff_t *pos)
1611 {
1612 struct proc_mounts *p = m->private;
1613 struct mount *mnt;
1614
1615 down_read(&namespace_sem);
1616
1617 mnt = mnt_find_id_at(p->ns, *pos);
1618 if (mnt)
1619 *pos = mnt->mnt_id_unique;
1620 return mnt;
1621 }
1622
1623 static void *m_next(struct seq_file *m, void *v, loff_t *pos)
1624 {
1625 struct mount *mnt = v;
1626 struct rb_node *node = rb_next(&mnt->mnt_node);
1627
1628 if (node) {
1629 struct mount *next = node_to_mount(node);
1630 *pos = next->mnt_id_unique;
1631 return next;
1632 }
1633
1634 /*
1635 * No more mounts. Set pos past current mount's ID so that if
1636 * iteration restarts, mnt_find_id_at() returns NULL.
1637 */
1638 *pos = mnt->mnt_id_unique + 1;
1639 return NULL;
1640 }
1641
1642 static void m_stop(struct seq_file *m, void *v)
1643 {
1644 up_read(&namespace_sem);
1645 }
1646
1647 static int m_show(struct seq_file *m, void *v)
1648 {
1649 struct proc_mounts *p = m->private;
1650 struct mount *r = v;
1651 return p->show(m, &r->mnt);
1652 }
1653
1654 const struct seq_operations mounts_op = {
1655 .start = m_start,
1656 .next = m_next,
1657 .stop = m_stop,
1658 .show = m_show,
1659 };
1660
1661 #endif /* CONFIG_PROC_FS */
1662
1663 /**
1664 * may_umount_tree - check if a mount tree is busy
1665 * @m: root of mount tree
1666 *
1667 * This is called to check if a tree of mounts has any
1668 * open files, pwds, chroots or sub mounts that are
1669 * busy.
1670 */
1671 int may_umount_tree(struct vfsmount *m)
1672 {
1673 struct mount *mnt = real_mount(m);
1674 bool busy = false;
1675
1676 /* write lock needed for mnt_get_count */
1677 lock_mount_hash();
1678 for (struct mount *p = mnt; p; p = next_mnt(p, mnt)) {
1679 if (mnt_get_count(p) > (p == mnt ? 2 : 1)) {
1680 busy = true;
1681 break;
1682 }
1683 }
1684 unlock_mount_hash();
1685
1686 return !busy;
1687 }
1688
1689 EXPORT_SYMBOL(may_umount_tree);
1690
1691 /**
1692 * may_umount - check if a mount point is busy
1693 * @mnt: root of mount
1694 *
1695 * This is called to check if a mount point has any
1696 * open files, pwds, chroots or sub mounts. If the
1697 * mount has sub mounts this will return busy
1698 * regardless of whether the sub mounts are busy.
1699 *
1700 * Doesn't take quota and stuff into account. IOW, in some cases it will
1701 * give false negatives. The main reason why it's here is that we need
1702 * a non-destructive way to look for easily umountable filesystems.
1703 */
1704 int may_umount(struct vfsmount *mnt)
1705 {
1706 int ret = 1;
1707 down_read(&namespace_sem);
1708 lock_mount_hash();
1709 if (propagate_mount_busy(real_mount(mnt), 2))
1710 ret = 0;
1711 unlock_mount_hash();
1712 up_read(&namespace_sem);
1713 return ret;
1714 }
1715
1716 EXPORT_SYMBOL(may_umount);
1717
1718 #ifdef CONFIG_FSNOTIFY
1719 static void mnt_notify(struct mount *p)
1720 {
1721 if (!p->prev_ns && p->mnt_ns) {
1722 fsnotify_mnt_attach(p->mnt_ns, &p->mnt);
1723 } else if (p->prev_ns && !p->mnt_ns) {
1724 fsnotify_mnt_detach(p->prev_ns, &p->mnt);
1725 } else if (p->prev_ns == p->mnt_ns) {
1726 fsnotify_mnt_move(p->mnt_ns, &p->mnt);
1727 } else {
1728 fsnotify_mnt_detach(p->prev_ns, &p->mnt);
1729 fsnotify_mnt_attach(p->mnt_ns, &p->mnt);
1730 }
1731 p->prev_ns = p->mnt_ns;
1732 }
1733
1734 static void notify_mnt_list(void)
1735 {
1736 struct mount *m, *tmp;
1737 /*
1738 * Notify about mounts that were added/reparented/detached/remain
1739 * connected after unmount.
1740 */
1741 list_for_each_entry_safe(m, tmp, &notify_list, to_notify) {
1742 mnt_notify(m);
1743 list_del_init(&m->to_notify);
1744 }
1745 }
1746
1747 static bool need_notify_mnt_list(void)
1748 {
1749 return !list_empty(&notify_list);
1750 }
1751 #else
1752 static void notify_mnt_list(void)
1753 {
1754 }
1755
1756 static bool need_notify_mnt_list(void)
1757 {
1758 return false;
1759 }
1760 #endif
1761
1762 static void free_mnt_ns(struct mnt_namespace *);
1763 static void namespace_unlock(void)
1764 {
1765 struct hlist_head head;
1766 struct hlist_node *p;
1767 struct mount *m;
1768 struct mnt_namespace *ns = emptied_ns;
1769 LIST_HEAD(list);
1770
1771 hlist_move_list(&unmounted, &head);
1772 list_splice_init(&ex_mountpoints, &list);
1773 emptied_ns = NULL;
1774
1775 if (need_notify_mnt_list()) {
1776 /*
1777 * No point blocking out concurrent readers while notifications
1778 * are sent. This will also allow statmount()/listmount() to run
1779 * concurrently.
1780 */
1781 downgrade_write(&namespace_sem);
1782 notify_mnt_list();
1783 up_read(&namespace_sem);
1784 } else {
1785 up_write(&namespace_sem);
1786 }
1787 if (unlikely(ns)) {
1788 /* Make sure we notice when we leak mounts. */
1789 VFS_WARN_ON_ONCE(!mnt_ns_empty(ns));
1790 free_mnt_ns(ns);
1791 }
1792
1793 shrink_dentry_list(&list);
1794
1795 if (likely(hlist_empty(&head)))
1796 return;
1797
1798 synchronize_rcu_expedited();
1799
1800 hlist_for_each_entry_safe(m, p, &head, mnt_umount) {
1801 hlist_del(&m->mnt_umount);
1802 mntput(&m->mnt);
1803 }
1804 }
1805
1806 static inline void namespace_lock(void)
1807 {
1808 down_write(&namespace_sem);
1809 }
1810
1811 enum umount_tree_flags {
1812 UMOUNT_SYNC = 1,
1813 UMOUNT_PROPAGATE = 2,
1814 UMOUNT_COVER = 4,
1815 };
1816
1817 /* Do we need to leave the mountpoint on the parent covered? */
1818 static bool needs_cover(struct mount *mnt, enum umount_tree_flags how)
1819 {
1820 if (how & UMOUNT_SYNC)
1821 return false;
1822 if (!(mnt->mnt_parent->mnt.mnt_flags & MNT_UMOUNT))
1823 return false;
1824 return (how & UMOUNT_COVER) || IS_MNT_LOCKED(mnt);
1825 }
1826
1827 /*
1828 * mount_lock must be held
1829 * namespace_sem must be held for write
1830 */
1831 static void umount_tree(struct mount *mnt, enum umount_tree_flags how)
1832 {
1833 LIST_HEAD(tmp_list);
1834 struct mount *p;
1835
1836 if (how & UMOUNT_PROPAGATE)
1837 propagate_mount_unlock(mnt);
1838
1839 /* Gather the mounts to umount */
1840 for (p = mnt; p; p = next_mnt(p, mnt)) {
1841 /* A mount is unmounted once. */
1842 VFS_WARN_ON_ONCE(p->mnt.mnt_flags & MNT_UMOUNT);
1843 p->mnt.mnt_flags |= MNT_UMOUNT;
1844 if (mnt_ns_attached(p))
1845 move_from_ns(p);
1846 list_add_tail(&p->mnt_list, &tmp_list);
1847 }
1848
1849 /* Hide the mounts from mnt_mounts */
1850 list_for_each_entry(p, &tmp_list, mnt_list) {
1851 list_del_init(&p->mnt_child);
1852 }
1853
1854 /* Add propagated mounts to the tmp_list */
1855 if (how & UMOUNT_PROPAGATE)
1856 propagate_umount(&tmp_list);
1857
1858 bulk_make_private(&tmp_list);
1859
1860 while (!list_empty(&tmp_list)) {
1861 struct mnt_namespace *ns;
1862 p = list_first_entry(&tmp_list, struct mount, mnt_list);
1863 list_del_init(&p->mnt_expire);
1864 list_del_init(&p->mnt_list);
1865 ns = p->mnt_ns;
1866 if (ns) {
1867 ns->nr_mounts--;
1868 __touch_mnt_namespace(ns);
1869 }
1870 WRITE_ONCE(p->mnt_ns, NULL);
1871 if (how & UMOUNT_SYNC)
1872 p->mnt.mnt_flags |= MNT_SYNC_UMOUNT;
1873
1874 if (mnt_has_parent(p)) {
1875 if (needs_cover(p, how))
1876 leave_cover(p);
1877 umount_mnt(p);
1878 }
1879 hlist_add_head(&p->mnt_umount, &unmounted);
1880
1881 /*
1882 * At this point p->mnt_ns is NULL, notification will be queued
1883 * only if
1884 *
1885 * - p->prev_ns is non-NULL *and*
1886 * - p->prev_ns->n_fsnotify_marks is non-NULL
1887 *
1888 * This will preclude queuing the mount if this is a cleanup
1889 * after a failed copy_tree() or destruction of an anonymous
1890 * namespace, etc.
1891 */
1892 mnt_notify_add(p);
1893 }
1894 }
1895
1896 static void shrink_submounts(struct mount *mnt);
1897
1898 static int do_umount_root(struct super_block *sb)
1899 {
1900 int ret = 0;
1901
1902 down_write(&sb->s_umount);
1903 if (!sb_rdonly(sb)) {
1904 struct fs_context *fc;
1905
1906 fc = fs_context_for_reconfigure(sb->s_root, SB_RDONLY,
1907 SB_RDONLY);
1908 if (IS_ERR(fc)) {
1909 ret = PTR_ERR(fc);
1910 } else {
1911 ret = parse_monolithic_mount_data(fc, NULL);
1912 if (!ret)
1913 ret = reconfigure_super(fc);
1914 put_fs_context(fc);
1915 }
1916 }
1917 up_write(&sb->s_umount);
1918 return ret;
1919 }
1920
1921 static int do_umount(struct mount *mnt, int flags)
1922 {
1923 struct super_block *sb = mnt->mnt.mnt_sb;
1924 int retval;
1925
1926 retval = security_sb_umount(&mnt->mnt, flags);
1927 if (retval)
1928 return retval;
1929
1930 /*
1931 * Allow userspace to request a mountpoint be expired rather than
1932 * unmounting unconditionally. Unmount only happens if:
1933 * (1) the mark is already set (the mark is cleared by mntput())
1934 * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
1935 */
1936 if (flags & MNT_EXPIRE) {
1937 if (&mnt->mnt == current->fs->root.mnt ||
1938 flags & (MNT_FORCE | MNT_DETACH))
1939 return -EINVAL;
1940
1941 /*
1942 * probably don't strictly need the lock here if we examined
1943 * all race cases, but it's a slowpath.
1944 */
1945 lock_mount_hash();
1946 if (!list_empty(&mnt->mnt_mounts) || mnt_get_count(mnt) != 2) {
1947 unlock_mount_hash();
1948 return -EBUSY;
1949 }
1950 unlock_mount_hash();
1951
1952 if (!xchg(&mnt->mnt_expiry_mark, 1))
1953 return -EAGAIN;
1954 }
1955
1956 /*
1957 * If we may have to abort operations to get out of this
1958 * mount, and they will themselves hold resources we must
1959 * allow the fs to do things. In the Unix tradition of
1960 * 'Gee thats tricky lets do it in userspace' the umount_begin
1961 * might fail to complete on the first run through as other tasks
1962 * must return, and the like. Thats for the mount program to worry
1963 * about for the moment.
1964 */
1965
1966 if (flags & MNT_FORCE && sb->s_op->umount_begin) {
1967 sb->s_op->umount_begin(sb);
1968 }
1969
1970 /*
1971 * No sense to grab the lock for this test, but test itself looks
1972 * somewhat bogus. Suggestions for better replacement?
1973 * Ho-hum... In principle, we might treat that as umount + switch
1974 * to rootfs. GC would eventually take care of the old vfsmount.
1975 * Actually it makes sense, especially if rootfs would contain a
1976 * /reboot - static binary that would close all descriptors and
1977 * call reboot(9). Then init(8) could umount root and exec /reboot.
1978 */
1979 if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
1980 /*
1981 * Special case for "unmounting" root ...
1982 * we just try to remount it readonly.
1983 */
1984 if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN))
1985 return -EPERM;
1986 return do_umount_root(sb);
1987 }
1988
1989 namespace_lock();
1990 lock_mount_hash();
1991
1992 /* Repeat the earlier racy checks, now that we are holding the locks */
1993 retval = -EINVAL;
1994 if (!check_mnt(mnt))
1995 goto out;
1996
1997 if (mnt->mnt.mnt_flags & MNT_LOCKED)
1998 goto out;
1999
2000 if (!mnt_has_parent(mnt)) /* not the absolute root */
2001 goto out;
2002
2003 event++;
2004 if (flags & MNT_DETACH) {
2005 umount_tree(mnt, UMOUNT_PROPAGATE);
2006 retval = 0;
2007 } else {
2008 shrink_submounts(mnt);
2009 retval = -EBUSY;
2010 if (!propagate_mount_busy(mnt, 2)) {
2011 umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
2012 retval = 0;
2013 }
2014 }
2015 out:
2016 unlock_mount_hash();
2017 namespace_unlock();
2018 return retval;
2019 }
2020
2021 /*
2022 * __detach_mounts - lazily unmount all mounts on the specified dentry
2023 *
2024 * During unlink, rmdir, and d_drop it is possible to loose the path
2025 * to an existing mountpoint, and wind up leaking the mount.
2026 * detach_mounts allows lazily unmounting those mounts instead of
2027 * leaking them.
2028 *
2029 * The dentry is unhashed before the mounts go so that no lookup finds
2030 * what they covered. The caller removes it for good afterwards.
2031 *
2032 * The caller may hold dentry->d_inode->i_rwsem.
2033 */
2034 void __detach_mounts(struct dentry *dentry)
2035 {
2036 struct pinned_mountpoint mp = {};
2037 struct mnt_cover *cover;
2038 struct hlist_node *n;
2039 struct mount *mnt;
2040
2041 guard(namespace_excl)();
2042 guard(mount_writer)();
2043
2044 if (!lookup_mountpoint(dentry, &mp))
2045 return;
2046
2047 /* the name goes first, what covered it goes second */
2048 d_drop(dentry);
2049 event++;
2050 while (mp.node.next) {
2051 mnt = hlist_entry(mp.node.next, struct mount, mnt_mp_list);
2052 umount_tree(mnt, UMOUNT_COVER);
2053 }
2054 /* the dentry goes away, so do the covers left behind on it */
2055 hlist_for_each_entry_safe(cover, n, &mp.mp->m_covers, pin)
2056 drop_cover(cover, &ex_mountpoints);
2057 unpin_mountpoint(&mp);
2058 }
2059
2060 /*
2061 * Is the caller allowed to modify his namespace?
2062 */
2063 bool may_mount(void)
2064 {
2065 return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN);
2066 }
2067
2068 static void warn_mandlock(void)
2069 {
2070 pr_warn_once("=======================================================\n"
2071 "WARNING: The mand mount option has been deprecated and\n"
2072 " and is ignored by this kernel. Remove the mand\n"
2073 " option from the mount to silence this warning.\n"
2074 "=======================================================\n");
2075 }
2076
2077 static int can_umount(const struct path *path, int flags)
2078 {
2079 struct mount *mnt = real_mount(path->mnt);
2080 struct super_block *sb = path->dentry->d_sb;
2081
2082 if (!may_mount())
2083 return -EPERM;
2084 if (!path_mounted(path))
2085 return -EINVAL;
2086 if (!check_mnt(mnt))
2087 return -EINVAL;
2088 if (mnt->mnt.mnt_flags & MNT_LOCKED) /* Check optimistically */
2089 return -EINVAL;
2090 if (flags & MNT_FORCE && !ns_capable(sb->s_user_ns, CAP_SYS_ADMIN))
2091 return -EPERM;
2092 return 0;
2093 }
2094
2095 // caller is responsible for flags being sane
2096 int path_umount(const struct path *path, int flags)
2097 {
2098 struct mount *mnt = real_mount(path->mnt);
2099 int ret;
2100
2101 ret = can_umount(path, flags);
2102 if (!ret)
2103 ret = do_umount(mnt, flags);
2104
2105 /* we mustn't call path_put() as that would clear mnt_expiry_mark */
2106 dput(path->dentry);
2107 mntput_no_expire(mnt);
2108 return ret;
2109 }
2110
2111 static int ksys_umount(char __user *name, int flags)
2112 {
2113 int lookup_flags = LOOKUP_MOUNTPOINT;
2114 struct path path;
2115 int ret;
2116
2117 // basic validity checks done first
2118 if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
2119 return -EINVAL;
2120
2121 if (!(flags & UMOUNT_NOFOLLOW))
2122 lookup_flags |= LOOKUP_FOLLOW;
2123 ret = user_path_at(AT_FDCWD, name, lookup_flags, &path);
2124 if (ret)
2125 return ret;
2126 return path_umount(&path, flags);
2127 }
2128
2129 SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
2130 {
2131 return ksys_umount(name, flags);
2132 }
2133
2134 #ifdef __ARCH_WANT_SYS_OLDUMOUNT
2135
2136 /*
2137 * The 2.0 compatible umount. No flags.
2138 */
2139 SYSCALL_DEFINE1(oldumount, char __user *, name)
2140 {
2141 return ksys_umount(name, 0);
2142 }
2143
2144 #endif
2145
2146 static bool is_mnt_ns_file(struct dentry *dentry)
2147 {
2148 struct ns_common *ns;
2149
2150 /* Is this a proxy for a mount namespace? */
2151 if (dentry->d_op != &ns_dentry_operations)
2152 return false;
2153
2154 ns = d_inode(dentry)->i_private;
2155
2156 return ns->ops == &mntns_operations;
2157 }
2158
2159 struct ns_common *from_mnt_ns(struct mnt_namespace *mnt)
2160 {
2161 return &mnt->ns;
2162 }
2163
2164 struct mnt_namespace *get_sequential_mnt_ns(struct mnt_namespace *mntns, bool previous)
2165 {
2166 struct ns_common *ns;
2167
2168 guard(rcu)();
2169
2170 for (;;) {
2171 ns = ns_tree_adjoined_rcu(mntns, previous);
2172 if (IS_ERR(ns))
2173 return ERR_CAST(ns);
2174
2175 mntns = to_mnt_ns(ns);
2176
2177 /*
2178 * The last passive reference count is put with RCU
2179 * delay so accessing the mount namespace is not just
2180 * safe but all relevant members are still valid.
2181 */
2182 if (!ns_capable_noaudit(mntns->user_ns, CAP_SYS_ADMIN))
2183 continue;
2184
2185 /*
2186 * We need an active reference count as we're persisting
2187 * the mount namespace and it might already be on its
2188 * deathbed.
2189 */
2190 if (!ns_ref_get(mntns))
2191 continue;
2192
2193 return mntns;
2194 }
2195 }
2196
2197 struct mnt_namespace *mnt_ns_from_dentry(struct dentry *dentry)
2198 {
2199 if (!is_mnt_ns_file(dentry))
2200 return NULL;
2201
2202 return to_mnt_ns(get_proc_ns(dentry->d_inode));
2203 }
2204
2205 static bool mnt_ns_loop(struct dentry *dentry)
2206 {
2207 /* Could bind mounting the mount namespace inode cause a
2208 * mount namespace loop?
2209 */
2210 struct mnt_namespace *mnt_ns = mnt_ns_from_dentry(dentry);
2211
2212 if (!mnt_ns)
2213 return false;
2214
2215 return current->nsproxy->mnt_ns->ns.ns_id >= mnt_ns->ns.ns_id;
2216 }
2217
2218 struct mount *copy_tree(struct mount *src_root, struct dentry *dentry,
2219 int flag)
2220 {
2221 struct mount *res, *src_parent, *src_root_child, *src_mnt,
2222 *dst_parent, *dst_mnt;
2223
2224 if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(src_root))
2225 return ERR_PTR(-EINVAL);
2226
2227 if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry))
2228 return ERR_PTR(-EINVAL);
2229
2230 res = dst_mnt = clone_mnt(src_root, dentry, flag);
2231 if (IS_ERR(dst_mnt))
2232 return dst_mnt;
2233
2234 src_parent = src_root;
2235
2236 list_for_each_entry(src_root_child, &src_root->mnt_mounts, mnt_child) {
2237 if (!is_subdir(src_root_child->mnt_mountpoint, dentry))
2238 continue;
2239
2240 for (src_mnt = src_root_child; src_mnt;
2241 src_mnt = next_mnt(src_mnt, src_root_child)) {
2242 if (!(flag & CL_COPY_UNBINDABLE) &&
2243 IS_MNT_UNBINDABLE(src_mnt)) {
2244 if (src_mnt->mnt.mnt_flags & MNT_LOCKED) {
2245 /* Both unbindable and locked. */
2246 dst_mnt = ERR_PTR(-EPERM);
2247 goto out;
2248 } else {
2249 src_mnt = skip_mnt_tree(src_mnt);
2250 continue;
2251 }
2252 }
2253 if (!(flag & CL_COPY_MNT_NS_FILE) &&
2254 is_mnt_ns_file(src_mnt->mnt.mnt_root)) {
2255 src_mnt = skip_mnt_tree(src_mnt);
2256 continue;
2257 }
2258 while (src_parent != src_mnt->mnt_parent) {
2259 src_parent = src_parent->mnt_parent;
2260 dst_mnt = dst_mnt->mnt_parent;
2261 }
2262
2263 src_parent = src_mnt;
2264 dst_parent = dst_mnt;
2265 dst_mnt = clone_mnt(src_mnt, src_mnt->mnt.mnt_root, flag);
2266 if (IS_ERR(dst_mnt))
2267 goto out;
2268 lock_mount_hash();
2269 if (src_mnt->mnt.mnt_flags & MNT_LOCKED)
2270 dst_mnt->mnt.mnt_flags |= MNT_LOCKED;
2271 if (unlikely(flag & CL_EXPIRE)) {
2272 /* stick the duplicate mount on the same expiry
2273 * list as the original if that was on one */
2274 if (!list_empty(&src_mnt->mnt_expire))
2275 list_add(&dst_mnt->mnt_expire,
2276 &src_mnt->mnt_expire);
2277 }
2278 attach_mnt(dst_mnt, dst_parent, src_parent->mnt_mp);
2279 unlock_mount_hash();
2280 }
2281 }
2282 return res;
2283
2284 out:
2285 if (res) {
2286 lock_mount_hash();
2287 umount_tree(res, UMOUNT_SYNC);
2288 unlock_mount_hash();
2289 }
2290 return dst_mnt;
2291 }
2292
2293 static inline bool extend_array(struct path **res, struct path **to_free,
2294 unsigned n, unsigned *count, unsigned new_count)
2295 {
2296 struct path *p;
2297
2298 if (likely(n < *count))
2299 return true;
2300 p = kmalloc_objs(struct path, new_count);
2301 if (p && *count)
2302 memcpy(p, *res, *count * sizeof(struct path));
2303 *count = new_count;
2304 kfree(*to_free);
2305 *to_free = *res = p;
2306 return p;
2307 }
2308
2309 const struct path *collect_paths(const struct path *path,
2310 struct path *prealloc, unsigned count)
2311 {
2312 struct mount *root = real_mount(path->mnt);
2313 struct mount *child;
2314 struct path *res = prealloc, *to_free = NULL;
2315 unsigned n = 0;
2316
2317 guard(namespace_shared)();
2318
2319 if (!check_mnt(root))
2320 return ERR_PTR(-EINVAL);
2321 if (!extend_array(&res, &to_free, 0, &count, 32))
2322 return ERR_PTR(-ENOMEM);
2323 res[n++] = *path;
2324 list_for_each_entry(child, &root->mnt_mounts, mnt_child) {
2325 if (!is_subdir(child->mnt_mountpoint, path->dentry))
2326 continue;
2327 for (struct mount *m = child; m; m = next_mnt(m, child)) {
2328 if (!extend_array(&res, &to_free, n, &count, 2 * count))
2329 return ERR_PTR(-ENOMEM);
2330 res[n].mnt = &m->mnt;
2331 res[n].dentry = m->mnt.mnt_root;
2332 n++;
2333 }
2334 }
2335 if (!extend_array(&res, &to_free, n, &count, count + 1))
2336 return ERR_PTR(-ENOMEM);
2337 memset(res + n, 0, (count - n) * sizeof(struct path));
2338 for (struct path *p = res; p->mnt; p++)
2339 path_get(p);
2340 return res;
2341 }
2342
2343 void drop_collected_paths(const struct path *paths, const struct path *prealloc)
2344 {
2345 for (const struct path *p = paths; p->mnt; p++)
2346 path_put(p);
2347 if (paths != prealloc)
2348 kfree(paths);
2349 }
2350
2351 static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *, bool);
2352
2353 /* Consumes the caller's reference to @mnt. */
2354 void dissolve_on_fput(struct vfsmount *mnt)
2355 {
2356 struct vfsmount *p __free(mntput) = mnt;
2357 struct mount *m = real_mount(p);
2358
2359 /*
2360 * m used to be the root of anon namespace; if it still is one,
2361 * we need to dissolve the mount tree and free that namespace.
2362 * Let's try to avoid taking namespace_sem if we can determine
2363 * that there's nothing to do without it - rcu_read_lock() is
2364 * enough to make anon_ns_root() memory-safe and once m has
2365 * left its namespace, it's no longer our concern, since it will
2366 * never become a root of anon ns again.
2367 */
2368
2369 scoped_guard(rcu) {
2370 if (!anon_ns_root(m))
2371 return;
2372 }
2373
2374 scoped_guard(namespace_excl) {
2375 if (!anon_ns_root(m))
2376 return;
2377
2378 emptied_ns = m->mnt_ns;
2379 lock_mount_hash();
2380 umount_tree(m, UMOUNT_COVER);
2381 unlock_mount_hash();
2382 mntput(no_free_ptr(p));
2383 }
2384 }
2385
2386 /* locks: namespace_shared && pinned(mnt) || mount_locked_reader */
2387 bool has_locked_children(struct mount *mnt, struct dentry *dentry)
2388 {
2389 struct mount *child;
2390
2391 list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
2392 if (!is_subdir(child->mnt_mountpoint, dentry))
2393 continue;
2394
2395 if (child->mnt.mnt_flags & MNT_LOCKED)
2396 return true;
2397 }
2398 return false;
2399 }
2400
2401 /* locks: namespace_shared && pinned(mnt) || mount_locked_reader */
2402 static bool __has_children(struct mount *mnt, struct dentry *dentry)
2403 {
2404 struct mount *child;
2405
2406 list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
2407 if (is_subdir(child->mnt_mountpoint, dentry))
2408 return true;
2409 }
2410 return false;
2411 }
2412
2413 /*
2414 * Check that there aren't references to earlier/same mount namespaces in the
2415 * specified subtree. Such references can act as pins for mount namespaces
2416 * that aren't checked by the mount-cycle checking code, thereby allowing
2417 * cycles to be made.
2418 *
2419 * locks: mount_locked_reader || namespace_shared && pinned(subtree)
2420 */
2421 static bool check_for_nsfs_mounts(struct mount *subtree)
2422 {
2423 for (struct mount *p = subtree; p; p = next_mnt(p, subtree))
2424 if (mnt_ns_loop(p->mnt.mnt_root))
2425 return false;
2426 return true;
2427 }
2428
2429 /**
2430 * clone_private_mount - create a private clone of a path
2431 * @path: path to clone
2432 *
2433 * This creates a new vfsmount, which will be the clone of @path. The new mount
2434 * will not be attached anywhere in the namespace and will be private (i.e.
2435 * changes to the originating mount won't be propagated into this).
2436 *
2437 * This assumes caller has called or done the equivalent of may_mount().
2438 *
2439 * Release with mntput().
2440 */
2441 struct vfsmount *clone_private_mount(const struct path *path)
2442 {
2443 struct mount *old_mnt = real_mount(path->mnt);
2444 struct mount *new_mnt;
2445
2446 guard(namespace_shared)();
2447
2448 if (IS_MNT_UNBINDABLE(old_mnt))
2449 return ERR_PTR(-EINVAL);
2450
2451 /*
2452 * Make sure the source mount is acceptable.
2453 * Anything mounted in our mount namespace is allowed.
2454 * Otherwise, it must be the root of an anonymous mount
2455 * namespace, and we need to make sure no namespace
2456 * loops get created.
2457 */
2458 if (!check_mnt(old_mnt)) {
2459 if (!anon_ns_root(old_mnt))
2460 return ERR_PTR(-EINVAL);
2461
2462 if (!check_for_nsfs_mounts(old_mnt))
2463 return ERR_PTR(-EINVAL);
2464 }
2465
2466 if (!ns_capable(old_mnt->mnt_ns->user_ns, CAP_SYS_ADMIN))
2467 return ERR_PTR(-EPERM);
2468
2469 if (has_locked_children(old_mnt, path->dentry))
2470 return ERR_PTR(-EINVAL);
2471
2472 new_mnt = clone_mnt(old_mnt, path->dentry, CL_PRIVATE);
2473 if (IS_ERR(new_mnt))
2474 return ERR_PTR(-EINVAL);
2475
2476 /* Longterm mount to be removed by kern_unmount*() */
2477 new_mnt->mnt_ns = MNT_NS_INTERNAL;
2478 return &new_mnt->mnt;
2479 }
2480 EXPORT_SYMBOL_GPL(clone_private_mount);
2481
2482 static void lock_mnt_tree(struct mount *mnt)
2483 {
2484 struct mount *p;
2485
2486 for (p = mnt; p; p = next_mnt(p, mnt)) {
2487 int flags = p->mnt.mnt_flags;
2488 /* Don't allow unprivileged users to change mount flags */
2489 flags |= MNT_LOCK_ATIME;
2490
2491 if (flags & MNT_READONLY)
2492 flags |= MNT_LOCK_READONLY;
2493
2494 if (flags & MNT_NODEV)
2495 flags |= MNT_LOCK_NODEV;
2496
2497 if (flags & MNT_NOSUID)
2498 flags |= MNT_LOCK_NOSUID;
2499
2500 if (flags & MNT_NOEXEC)
2501 flags |= MNT_LOCK_NOEXEC;
2502 /* Don't allow unprivileged users to reveal what is under a mount */
2503 if (list_empty(&p->mnt_expire) && p != mnt)
2504 flags |= MNT_LOCKED;
2505 p->mnt.mnt_flags = flags;
2506 }
2507 }
2508
2509 static void cleanup_group_ids(struct mount *mnt, struct mount *end)
2510 {
2511 struct mount *p;
2512
2513 for (p = mnt; p != end; p = next_mnt(p, mnt)) {
2514 if (p->mnt_group_id && !IS_MNT_SHARED(p))
2515 mnt_release_group_id(p);
2516 }
2517 }
2518
2519 static int invent_group_ids(struct mount *mnt, bool recurse)
2520 {
2521 struct mount *p;
2522
2523 for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
2524 if (!p->mnt_group_id) {
2525 int err = mnt_alloc_group_id(p);
2526 if (err) {
2527 cleanup_group_ids(mnt, p);
2528 return err;
2529 }
2530 }
2531 }
2532
2533 return 0;
2534 }
2535
2536 int count_mounts(struct mnt_namespace *ns, struct mount *mnt)
2537 {
2538 unsigned int max = READ_ONCE(sysctl_mount_max);
2539 unsigned int mounts = 0;
2540 struct mount *p;
2541
2542 if (ns->nr_mounts >= max)
2543 return -ENOSPC;
2544 max -= ns->nr_mounts;
2545 if (ns->pending_mounts >= max)
2546 return -ENOSPC;
2547 max -= ns->pending_mounts;
2548
2549 for (p = mnt; p; p = next_mnt(p, mnt))
2550 mounts++;
2551
2552 if (mounts > max)
2553 return -ENOSPC;
2554
2555 ns->pending_mounts += mounts;
2556 return 0;
2557 }
2558
2559 enum mnt_tree_flags_t {
2560 MNT_TREE_BENEATH = BIT(0),
2561 MNT_TREE_PROPAGATION = BIT(1),
2562 };
2563
2564 /**
2565 * attach_recursive_mnt - attach a source mount tree
2566 * @source_mnt: mount tree to be attached
2567 * @dest: the context for mounting at the place where the tree should go
2568 *
2569 * NOTE: in the table below explains the semantics when a source mount
2570 * of a given type is attached to a destination mount of a given type.
2571 * ---------------------------------------------------------------------------
2572 * | BIND MOUNT OPERATION |
2573 * |**************************************************************************
2574 * | source-->| shared | private | slave | unbindable |
2575 * | dest | | | | |
2576 * | | | | | | |
2577 * | v | | | | |
2578 * |**************************************************************************
2579 * | shared | shared (++) | shared (+) | shared(+++)| invalid |
2580 * | | | | | |
2581 * |non-shared| shared (+) | private | slave (*) | invalid |
2582 * ***************************************************************************
2583 * A bind operation clones the source mount and mounts the clone on the
2584 * destination mount.
2585 *
2586 * (++) the cloned mount is propagated to all the mounts in the propagation
2587 * tree of the destination mount and the cloned mount is added to
2588 * the peer group of the source mount.
2589 * (+) the cloned mount is created under the destination mount and is marked
2590 * as shared. The cloned mount is added to the peer group of the source
2591 * mount.
2592 * (+++) the mount is propagated to all the mounts in the propagation tree
2593 * of the destination mount and the cloned mount is made slave
2594 * of the same master as that of the source mount. The cloned mount
2595 * is marked as 'shared and slave'.
2596 * (*) the cloned mount is made a slave of the same master as that of the
2597 * source mount.
2598 *
2599 * ---------------------------------------------------------------------------
2600 * | MOVE MOUNT OPERATION |
2601 * |**************************************************************************
2602 * | source-->| shared | private | slave | unbindable |
2603 * | dest | | | | |
2604 * | | | | | | |
2605 * | v | | | | |
2606 * |**************************************************************************
2607 * | shared | shared (+) | shared (+) | shared(+++) | invalid |
2608 * | | | | | |
2609 * |non-shared| shared (+*) | private | slave (*) | unbindable |
2610 * ***************************************************************************
2611 *
2612 * (+) the mount is moved to the destination. And is then propagated to
2613 * all the mounts in the propagation tree of the destination mount.
2614 * (+*) the mount is moved to the destination.
2615 * (+++) the mount is moved to the destination and is then propagated to
2616 * all the mounts belonging to the destination mount's propagation tree.
2617 * the mount is marked as 'shared and slave'.
2618 * (*) the mount continues to be a slave at the new location.
2619 *
2620 * if the source mount is a tree, the operations explained above is
2621 * applied to each mount in the tree.
2622 * Must be called without spinlocks held, since this function can sleep
2623 * in allocations.
2624 *
2625 * Context: The function expects namespace_lock() to be held.
2626 * Return: If @source_mnt was successfully attached 0 is returned.
2627 * Otherwise a negative error code is returned.
2628 */
2629 static int attach_recursive_mnt(struct mount *source_mnt,
2630 const struct pinned_mountpoint *dest)
2631 {
2632 struct mount *dest_mnt = dest->parent;
2633 struct mountpoint *dest_mp = dest->mp;
2634 HLIST_HEAD(tree_list);
2635 struct mnt_namespace *ns = dest_mnt->mnt_ns;
2636 struct user_namespace *user_ns = ns->user_ns;
2637 struct pinned_mountpoint root = {};
2638 struct mountpoint *shorter = NULL;
2639 struct mount *child, *p;
2640 struct mount *top;
2641 struct hlist_node *n;
2642 int err = 0;
2643 bool moving = mnt_has_parent(source_mnt);
2644
2645 /*
2646 * A caller in an unprivileged mount namespaces may trigger an
2647 * automount and propagate locked mounts into privileged mount
2648 * namespaces. Take ownership from the target mount namespace.
2649 * It's equivalent for everything but the automount case.
2650 *
2651 * Detached trees in anonymous mount namespaces by be handed
2652 * over via SCM_RIGHTS or inherited in other ways on purpose
2653 * the attaching task's mount namespace is authoritative, not
2654 * the creator of the detached tree.
2655 */
2656 if (is_anon_ns(ns))
2657 user_ns = current->nsproxy->mnt_ns->user_ns;
2658 else
2659 user_ns = ns->user_ns;
2660
2661 /*
2662 * Preallocate a mountpoint in case the new mounts need to be
2663 * mounted beneath mounts on the same mountpoint.
2664 */
2665 for (top = source_mnt; ; top = top->overmount) {
2666 if (!shorter && is_mnt_ns_file(top->mnt.mnt_root))
2667 shorter = top->mnt_mp;
2668 if (likely(!top->overmount))
2669 break;
2670 }
2671 err = get_mountpoint(top->mnt.mnt_root, &root);
2672 if (err)
2673 return err;
2674
2675 /* Is there space to add these mounts to the mount namespace? */
2676 if (!moving) {
2677 err = count_mounts(ns, source_mnt);
2678 if (err)
2679 goto out;
2680 }
2681
2682 if (IS_MNT_SHARED(dest_mnt)) {
2683 err = invent_group_ids(source_mnt, true);
2684 if (err)
2685 goto out;
2686 err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list);
2687 }
2688 lock_mount_hash();
2689 if (err)
2690 goto out_cleanup_ids;
2691
2692 if (IS_MNT_SHARED(dest_mnt)) {
2693 for (p = source_mnt; p; p = next_mnt(p, source_mnt))
2694 set_mnt_shared(p);
2695 }
2696
2697 if (moving) {
2698 umount_mnt(source_mnt);
2699 mnt_notify_add(source_mnt);
2700 /* if the mount is moved, it should no longer be expired
2701 * automatically */
2702 list_del_init(&source_mnt->mnt_expire);
2703 } else {
2704 if (source_mnt->mnt_ns) {
2705 /* move from anon - the caller will destroy */
2706 emptied_ns = source_mnt->mnt_ns;
2707 for (p = source_mnt; p; p = next_mnt(p, source_mnt))
2708 move_from_ns(p);
2709 }
2710 }
2711
2712 mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt);
2713 /*
2714 * Now the original copy is in the same state as the secondaries -
2715 * its root attached to mountpoint, but not hashed and all mounts
2716 * in it are either in our namespace or in no namespace at all.
2717 * Add the original to the list of copies and deal with the
2718 * rest of work for all of them uniformly.
2719 */
2720 hlist_add_head(&source_mnt->mnt_hash, &tree_list);
2721
2722 hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) {
2723 struct mount *q;
2724 hlist_del_init(&child->mnt_hash);
2725 /* Notice when we are propagating across user namespaces */
2726 if (child->mnt_parent->mnt_ns->user_ns != user_ns)
2727 lock_mnt_tree(child);
2728 q = __lookup_mnt(&child->mnt_parent->mnt,
2729 child->mnt_mountpoint);
2730 commit_tree(child);
2731 if (q) {
2732 struct mount *r = topmost_overmount(child);
2733 struct mountpoint *mp = root.mp;
2734
2735 if (unlikely(shorter) && child != source_mnt)
2736 mp = shorter;
2737 /*
2738 * If @q was locked it was meant to hide
2739 * whatever was under it. Let @child take over
2740 * that job and lock it. If @child is the mount
2741 * the caller placed we can then unlock @q:
2742 * nothing another namespace does removes it
2743 * again. A propagated copy goes away when the
2744 * mounter of the original unmounts it, so @q
2745 * keeps its lock.
2746 */
2747 if (IS_MNT_LOCKED(q)) {
2748 child->mnt.mnt_flags |= MNT_LOCKED;
2749 if (child == source_mnt)
2750 q->mnt.mnt_flags &= ~MNT_LOCKED;
2751 }
2752 mnt_change_mountpoint(r, mp, q);
2753 }
2754 }
2755 unpin_mountpoint(&root);
2756 unlock_mount_hash();
2757
2758 return 0;
2759
2760 out_cleanup_ids:
2761 while (!hlist_empty(&tree_list)) {
2762 child = hlist_entry(tree_list.first, struct mount, mnt_hash);
2763 child->mnt_parent->mnt_ns->pending_mounts = 0;
2764 umount_tree(child, UMOUNT_SYNC);
2765 }
2766 unlock_mount_hash();
2767 cleanup_group_ids(source_mnt, NULL);
2768 out:
2769 ns->pending_mounts = 0;
2770
2771 read_seqlock_excl(&mount_lock);
2772 unpin_mountpoint(&root);
2773 read_sequnlock_excl(&mount_lock);
2774
2775 return err;
2776 }
2777
2778 static inline struct mount *where_to_mount(const struct path *path,
2779 struct dentry **dentry,
2780 bool beneath)
2781 {
2782 struct mount *m;
2783
2784 if (unlikely(beneath)) {
2785 m = topmost_overmount(real_mount(path->mnt));
2786 *dentry = m->mnt_mountpoint;
2787 return m->mnt_parent;
2788 }
2789 m = __lookup_mnt(path->mnt, path->dentry);
2790 if (unlikely(m)) {
2791 m = topmost_overmount(m);
2792 *dentry = m->mnt.mnt_root;
2793 return m;
2794 }
2795 *dentry = path->dentry;
2796 return real_mount(path->mnt);
2797 }
2798
2799 /**
2800 * do_lock_mount - acquire environment for mounting
2801 * @path: target path
2802 * @res: context to set up
2803 * @beneath: whether the intention is to mount beneath @path
2804 *
2805 * To mount something at given location, we need
2806 * namespace_sem locked exclusive
2807 * inode of dentry we are mounting on locked exclusive
2808 * struct mountpoint for that dentry
2809 * struct mount we are mounting on
2810 *
2811 * Results are stored in caller-supplied context (pinned_mountpoint);
2812 * on success we have res->parent and res->mp pointing to parent and
2813 * mountpoint respectively and res->node inserted into the ->m_list
2814 * of the mountpoint, making sure the mountpoint won't disappear.
2815 * On failure we have res->parent set to ERR_PTR(-E...), res->mp
2816 * left NULL, res->node - empty.
2817 * In case of success do_lock_mount returns with locks acquired (in
2818 * proper order - inode lock nests outside of namespace_sem).
2819 *
2820 * Request to mount on overmounted location is treated as "mount on
2821 * top of whatever's overmounting it"; request to mount beneath
2822 * a location - "mount immediately beneath the topmost mount at that
2823 * place".
2824 *
2825 * In all cases the location must not have been unmounted and the
2826 * chosen mountpoint must be allowed to be mounted on. For "beneath"
2827 * case we also require the location to be at the root of a mount
2828 * that has something mounted on top of it (i.e. has an overmount).
2829 */
2830 static void do_lock_mount(const struct path *path,
2831 struct pinned_mountpoint *res,
2832 bool beneath)
2833 {
2834 int err;
2835
2836 if (unlikely(beneath) && !path_mounted(path)) {
2837 res->parent = ERR_PTR(-EINVAL);
2838 return;
2839 }
2840
2841 do {
2842 struct dentry *dentry, *d;
2843 struct mount *m, *n;
2844
2845 scoped_guard(mount_locked_reader) {
2846 m = where_to_mount(path, &dentry, beneath);
2847 /* sticky, so it takes no locks to refuse it */
2848 if (unlikely(cant_mount(dentry))) {
2849 res->parent = ERR_PTR(-ENOENT);
2850 return;
2851 }
2852 if (&m->mnt != path->mnt) {
2853 mntget(&m->mnt);
2854 dget(dentry);
2855 }
2856 }
2857
2858 inode_lock(dentry->d_inode);
2859 namespace_lock();
2860
2861 // check if the chain of mounts (if any) has changed.
2862 scoped_guard(mount_locked_reader)
2863 n = where_to_mount(path, &d, beneath);
2864
2865 if (unlikely(n != m || dentry != d))
2866 err = -EAGAIN; // something moved, retry
2867 else if (unlikely(cant_mount(dentry) || !is_mounted(path->mnt)))
2868 err = -ENOENT; // not to be mounted on
2869 else if (beneath && &m->mnt == path->mnt && !m->overmount)
2870 err = -EINVAL;
2871 else
2872 err = get_mountpoint(dentry, res);
2873
2874 if (unlikely(err)) {
2875 res->parent = ERR_PTR(err);
2876 namespace_unlock();
2877 inode_unlock(dentry->d_inode);
2878 } else {
2879 res->parent = m;
2880 }
2881 /*
2882 * Drop the temporary references. This is subtle - on success
2883 * we are doing that under namespace_sem, which would normally
2884 * be forbidden. However, in that case we are guaranteed that
2885 * refcounts won't reach zero, since we know that path->mnt
2886 * is mounted and thus all mounts reachable from it are pinned
2887 * and stable, along with their mountpoints and roots.
2888 */
2889 if (&m->mnt != path->mnt) {
2890 dput(dentry);
2891 mntput(&m->mnt);
2892 }
2893 } while (err == -EAGAIN);
2894 }
2895
2896 static void __unlock_mount(struct pinned_mountpoint *m)
2897 {
2898 inode_unlock(m->mp->m_dentry->d_inode);
2899 read_seqlock_excl(&mount_lock);
2900 unpin_mountpoint(m);
2901 read_sequnlock_excl(&mount_lock);
2902 namespace_unlock();
2903 }
2904
2905 static inline void unlock_mount(struct pinned_mountpoint *m)
2906 {
2907 if (!IS_ERR(m->parent))
2908 __unlock_mount(m);
2909 }
2910
2911 static void lock_mount_exact(const struct path *path,
2912 struct pinned_mountpoint *mp, bool copy_mount,
2913 unsigned int copy_flags);
2914
2915 #define LOCK_MOUNT_MAYBE_BENEATH(mp, path, beneath) \
2916 struct pinned_mountpoint mp __cleanup(unlock_mount) = {}; \
2917 do_lock_mount((path), &mp, (beneath))
2918 #define LOCK_MOUNT(mp, path) LOCK_MOUNT_MAYBE_BENEATH(mp, (path), false)
2919 #define LOCK_MOUNT_EXACT(mp, path) \
2920 struct pinned_mountpoint mp __cleanup(unlock_mount) = {}; \
2921 lock_mount_exact((path), &mp, false, 0)
2922 #define LOCK_MOUNT_EXACT_COPY(mp, path, copy_flags) \
2923 struct pinned_mountpoint mp __cleanup(unlock_mount) = {}; \
2924 lock_mount_exact((path), &mp, true, (copy_flags))
2925
2926 static int graft_tree(struct mount *mnt, const struct pinned_mountpoint *mp)
2927 {
2928 if (mnt->mnt.mnt_sb->s_flags & SB_NOUSER)
2929 return -EINVAL;
2930
2931 if (d_is_dir(mp->mp->m_dentry) !=
2932 d_is_dir(mnt->mnt.mnt_root))
2933 return -ENOTDIR;
2934
2935 return attach_recursive_mnt(mnt, mp);
2936 }
2937
2938 static int may_change_propagation(const struct mount *m)
2939 {
2940 struct mnt_namespace *ns = m->mnt_ns;
2941
2942 // it must be mounted in some namespace
2943 if (IS_ERR_OR_NULL(ns)) // is_mounted()
2944 return -EINVAL;
2945 // and the caller must be admin in userns of that namespace
2946 if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN))
2947 return -EPERM;
2948 return 0;
2949 }
2950
2951 /*
2952 * Sanity check the flags to change_mnt_propagation.
2953 */
2954
2955 static int flags_to_propagation_type(int ms_flags)
2956 {
2957 int type = ms_flags & ~(MS_REC | MS_SILENT);
2958
2959 /* Fail if any non-propagation flags are set */
2960 if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
2961 return 0;
2962 /* Only one propagation flag should be set */
2963 if (!is_power_of_2(type))
2964 return 0;
2965 return type;
2966 }
2967
2968 /*
2969 * recursively change the type of the mountpoint.
2970 */
2971 static int do_change_type(const struct path *path, int ms_flags)
2972 {
2973 struct mount *m;
2974 struct mount *mnt = real_mount(path->mnt);
2975 int recurse = ms_flags & MS_REC;
2976 int type;
2977 int err;
2978
2979 if (!path_mounted(path))
2980 return -EINVAL;
2981
2982 type = flags_to_propagation_type(ms_flags);
2983 if (!type)
2984 return -EINVAL;
2985
2986 guard(namespace_excl)();
2987
2988 err = may_change_propagation(mnt);
2989 if (err)
2990 return err;
2991
2992 if (type == MS_SHARED) {
2993 err = invent_group_ids(mnt, recurse);
2994 if (err)
2995 return err;
2996 }
2997
2998 for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
2999 change_mnt_propagation(m, type);
3000
3001 guard(mount_locked_reader)();
3002 touch_mnt_namespace(mnt->mnt_ns);
3003
3004 return 0;
3005 }
3006
3007 /* may_copy_tree() - check if a mount tree can be copied
3008 * @path: path to the mount tree to be copied
3009 *
3010 * This helper checks if the caller may copy the mount tree starting
3011 * from @path->mnt. The caller may copy the mount tree under the
3012 * following circumstances:
3013 *
3014 * (1) The caller is located in the mount namespace of the mount tree.
3015 * This also implies that the mount does not belong to an anonymous
3016 * mount namespace.
3017 * (2) The caller tries to copy an nfs mount referring to a mount
3018 * namespace, i.e., the caller is trying to copy a mount namespace
3019 * entry from nsfs.
3020 * (3) The caller tries to copy a pidfs mount referring to a pidfd.
3021 * (4) The caller is trying to copy a mount tree that belongs to an
3022 * anonymous mount namespace.
3023 *
3024 * For that to be safe, this helper enforces that the origin mount
3025 * namespace the anonymous mount namespace was created from is the
3026 * same as the caller's mount namespace by comparing the sequence
3027 * numbers.
3028 *
3029 * This is not strictly necessary. The current semantics of the new
3030 * mount api enforce that the caller must be located in the same
3031 * mount namespace as the mount tree it interacts with. Using the
3032 * origin sequence number preserves these semantics even for
3033 * anonymous mount namespaces. However, one could envision extending
3034 * the api to directly operate across mount namespace if needed.
3035 *
3036 * The ownership of a non-anonymous mount namespace such as the
3037 * caller's cannot change.
3038 * => We know that the caller's mount namespace is stable.
3039 *
3040 * If the origin sequence number of the anonymous mount namespace is
3041 * the same as the sequence number of the caller's mount namespace.
3042 * => The owning namespaces are the same.
3043 *
3044 * ==> The earlier capability check on the owning namespace of the
3045 * caller's mount namespace ensures that the caller has the
3046 * ability to copy the mount tree.
3047 *
3048 * Returns true if the mount tree can be copied, false otherwise.
3049 */
3050 static inline bool may_copy_tree(const struct path *path)
3051 {
3052 struct mount *mnt = real_mount(path->mnt);
3053 const struct dentry_operations *d_op;
3054
3055 if (check_mnt(mnt))
3056 return true;
3057
3058 d_op = path->dentry->d_op;
3059 if (d_op == &ns_dentry_operations)
3060 return true;
3061
3062 if (d_op == &pidfs_dentry_operations)
3063 return true;
3064
3065 if (!is_mounted(path->mnt))
3066 return false;
3067
3068 return check_anonymous_mnt(mnt);
3069 }
3070
3071 static struct mount *__do_loopback(const struct path *old_path,
3072 bool recurse, unsigned int copy_flags)
3073 {
3074 struct mount *old = real_mount(old_path->mnt);
3075
3076 if (IS_MNT_UNBINDABLE(old))
3077 return ERR_PTR(-EINVAL);
3078
3079 if (!may_copy_tree(old_path))
3080 return ERR_PTR(-EINVAL);
3081
3082 /* a pseudo dentry is freed without an RCU delay, no walk may find it */
3083 if (old_path->dentry->d_flags & DCACHE_NORCU)
3084 return ERR_PTR(-EINVAL);
3085
3086 if (recurse && !old->mnt_ns)
3087 return ERR_PTR(-EINVAL);
3088
3089 if (!recurse && has_locked_children(old, old_path->dentry))
3090 return ERR_PTR(-EINVAL);
3091
3092 if (recurse)
3093 return copy_tree(old, old_path->dentry, copy_flags);
3094
3095 return clone_mnt(old, old_path->dentry, copy_flags);
3096 }
3097
3098 /*
3099 * do loopback mount.
3100 */
3101 static int do_loopback(const struct path *path, const char *old_name,
3102 int recurse)
3103 {
3104 struct path old_path __free(path_put) = {};
3105 struct mount *mnt = NULL;
3106 int err;
3107
3108 if (!old_name || !*old_name)
3109 return -EINVAL;
3110 err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
3111 if (err)
3112 return err;
3113
3114 if (mnt_ns_loop(old_path.dentry))
3115 return -EINVAL;
3116
3117 LOCK_MOUNT(mp, path);
3118 if (IS_ERR(mp.parent))
3119 return PTR_ERR(mp.parent);
3120
3121 if (!check_mnt(mp.parent))
3122 return -EINVAL;
3123
3124 mnt = __do_loopback(&old_path, recurse, CL_COPY_MNT_NS_FILE);
3125 if (IS_ERR(mnt))
3126 return PTR_ERR(mnt);
3127
3128 /* the copy may carry mount namespace files from below the source */
3129 if (recurse && !check_for_nsfs_mounts(mnt))
3130 err = -EINVAL;
3131 else
3132 err = graft_tree(mnt, &mp);
3133 if (err) {
3134 lock_mount_hash();
3135 umount_tree(mnt, UMOUNT_SYNC);
3136 unlock_mount_hash();
3137 }
3138 return err;
3139 }
3140
3141 static struct mnt_namespace *get_detached_copy(const struct path *path, unsigned int flags)
3142 {
3143 struct mnt_namespace *ns, *mnt_ns = current->nsproxy->mnt_ns, *src_mnt_ns;
3144 struct user_namespace *user_ns = mnt_ns->user_ns;
3145 struct mount *mnt, *p;
3146
3147 ns = alloc_mnt_ns(user_ns, true);
3148 if (IS_ERR(ns))
3149 return ns;
3150
3151 guard(namespace_excl)();
3152
3153 /*
3154 * Record the sequence number of the source mount namespace.
3155 * This needs to hold namespace_sem to ensure that the mount
3156 * doesn't get attached.
3157 */
3158 if (is_mounted(path->mnt)) {
3159 src_mnt_ns = real_mount(path->mnt)->mnt_ns;
3160 if (is_anon_ns(src_mnt_ns))
3161 ns->seq_origin = src_mnt_ns->seq_origin;
3162 else
3163 ns->seq_origin = src_mnt_ns->ns.ns_id;
3164 }
3165
3166 mnt = __do_loopback(path, (flags & AT_RECURSIVE), CL_COPY_MNT_NS_FILE);
3167 if (IS_ERR(mnt)) {
3168 emptied_ns = ns;
3169 return ERR_CAST(mnt);
3170 }
3171
3172 for (p = mnt; p; p = next_mnt(p, mnt)) {
3173 mnt_add_to_ns(ns, p);
3174 ns->nr_mounts++;
3175 }
3176 ns->root = mnt;
3177 return ns;
3178 }
3179
3180 static struct file *open_detached_copy(struct path *path, unsigned int flags)
3181 {
3182 struct mnt_namespace *ns = get_detached_copy(path, flags);
3183 struct file *file;
3184
3185 if (IS_ERR(ns))
3186 return ERR_CAST(ns);
3187
3188 mntput(path->mnt);
3189 path->mnt = mntget(&ns->root->mnt);
3190 file = dentry_open(path, O_PATH, current_cred());
3191 if (IS_ERR(file))
3192 dissolve_on_fput(no_free_ptr(path->mnt));
3193 else
3194 file->f_mode |= FMODE_NEED_UNMOUNT;
3195 return file;
3196 }
3197
3198 enum mount_copy_flags_t {
3199 MOUNT_COPY_RECURSIVE = (1 << 0),
3200 MOUNT_COPY_NEW = (1 << 1),
3201 };
3202
3203 static struct mnt_namespace *create_new_namespace(struct path *path,
3204 enum mount_copy_flags_t flags)
3205 {
3206 struct mnt_namespace *ns = current->nsproxy->mnt_ns;
3207 struct user_namespace *user_ns = current_user_ns();
3208 struct mnt_namespace *new_ns;
3209 struct mount *new_ns_root, *old_ns_root;
3210 struct path to_path;
3211 struct mount *mnt;
3212 unsigned int copy_flags = 0;
3213 bool locked = false, recurse = flags & MOUNT_COPY_RECURSIVE;
3214 bool foreign = user_ns != ns->user_ns;
3215
3216 if (unlikely(!d_can_lookup(path->dentry)))
3217 return ERR_PTR(-ENOTDIR);
3218
3219 /*
3220 * Without privileges over the mount namespace the copy is made from
3221 * nothing mounted below @path may be left out. It would reveal what
3222 * it covers. That's what unshare() gives such a caller as well.
3223 */
3224 if (foreign)
3225 copy_flags |= CL_SLAVE | CL_COPY_UNBINDABLE;
3226
3227 new_ns = alloc_mnt_ns(user_ns, false);
3228 if (IS_ERR(new_ns))
3229 return ERR_CAST(new_ns);
3230
3231 old_ns_root = ns->root;
3232 to_path.mnt = &old_ns_root->mnt;
3233 to_path.dentry = old_ns_root->mnt.mnt_root;
3234
3235 VFS_WARN_ON_ONCE(old_ns_root->mnt.mnt_sb->s_type != &nullfs_fs_type);
3236
3237 LOCK_MOUNT_EXACT_COPY(mp, &to_path, copy_flags);
3238 if (IS_ERR(mp.parent)) {
3239 free_mnt_ns(new_ns);
3240 return ERR_CAST(mp.parent);
3241 }
3242 new_ns_root = mp.parent;
3243
3244 /*
3245 * If the real rootfs had a locked mount on top of it somewhere
3246 * in the stack, lock the new mount tree as well so it can't be
3247 * exposed.
3248 */
3249 mnt = old_ns_root;
3250 while (mnt->overmount) {
3251 mnt = mnt->overmount;
3252 if (mnt->mnt.mnt_flags & MNT_LOCKED)
3253 locked = true;
3254 }
3255
3256 /*
3257 * We don't emulate unshare()ing a mount namespace. We stick to
3258 * the restrictions of creating detached bind-mounts. It has a
3259 * lot saner and simpler semantics. A caller without privileges
3260 * over the mount namespace can't leave out any child though.
3261 */
3262 if (flags & MOUNT_COPY_NEW)
3263 mnt = clone_mnt(real_mount(path->mnt), path->dentry, copy_flags);
3264 else if (foreign && !recurse &&
3265 __has_children(real_mount(path->mnt), path->dentry))
3266 mnt = ERR_PTR(-EINVAL);
3267 else
3268 mnt = __do_loopback(path, recurse, copy_flags);
3269 scoped_guard(mount_writer) {
3270 if (IS_ERR(mnt)) {
3271 emptied_ns = new_ns;
3272 umount_tree(new_ns_root, 0);
3273 return ERR_CAST(mnt);
3274 }
3275
3276 if (locked)
3277 mnt->mnt.mnt_flags |= MNT_LOCKED;
3278 /*
3279 * now mount the detached tree on top of the copy
3280 * of the real rootfs we created.
3281 */
3282 attach_mnt(mnt, new_ns_root, mp.mp);
3283 if (foreign)
3284 lock_mnt_tree(new_ns_root);
3285 }
3286
3287 for (mnt = new_ns_root; mnt; mnt = next_mnt(mnt, new_ns_root)) {
3288 mnt_add_to_ns(new_ns, mnt);
3289 new_ns->nr_mounts++;
3290 }
3291
3292 new_ns->root = new_ns_root;
3293 ns_tree_add_raw(new_ns);
3294 return new_ns;
3295 }
3296
3297 static struct file *open_new_namespace(struct path *path,
3298 enum mount_copy_flags_t flags)
3299 {
3300 struct mnt_namespace *new_ns;
3301
3302 new_ns = create_new_namespace(path, flags);
3303 if (IS_ERR(new_ns))
3304 return ERR_CAST(new_ns);
3305 return open_namespace_file(to_ns_common(new_ns));
3306 }
3307
3308 static struct file *vfs_open_tree(int dfd, const char __user *filename, unsigned int flags)
3309 {
3310 int ret;
3311 struct path path __free(path_put) = {};
3312 int lookup_flags = LOOKUP_AUTOMOUNT | LOOKUP_FOLLOW;
3313
3314 BUILD_BUG_ON(OPEN_TREE_CLOEXEC != O_CLOEXEC);
3315
3316 if (flags & ~(AT_EMPTY_PATH | AT_NO_AUTOMOUNT | AT_RECURSIVE |
3317 AT_SYMLINK_NOFOLLOW | OPEN_TREE_CLONE |
3318 OPEN_TREE_CLOEXEC | OPEN_TREE_NAMESPACE))
3319 return ERR_PTR(-EINVAL);
3320
3321 if ((flags & (AT_RECURSIVE | OPEN_TREE_CLONE | OPEN_TREE_NAMESPACE)) ==
3322 AT_RECURSIVE)
3323 return ERR_PTR(-EINVAL);
3324
3325 if (hweight32(flags & (OPEN_TREE_CLONE | OPEN_TREE_NAMESPACE)) > 1)
3326 return ERR_PTR(-EINVAL);
3327
3328 if (flags & AT_NO_AUTOMOUNT)
3329 lookup_flags &= ~LOOKUP_AUTOMOUNT;
3330 if (flags & AT_SYMLINK_NOFOLLOW)
3331 lookup_flags &= ~LOOKUP_FOLLOW;
3332
3333 /*
3334 * If we create a new mount namespace with the cloned mount tree we
3335 * just care about being privileged over our current user namespace.
3336 * The new mount namespace will be owned by it.
3337 */
3338 if ((flags & OPEN_TREE_NAMESPACE) &&
3339 !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
3340 return ERR_PTR(-EPERM);
3341
3342 if ((flags & OPEN_TREE_CLONE) && !may_mount())
3343 return ERR_PTR(-EPERM);
3344
3345 CLASS(filename_uflags, name)(filename, flags);
3346 ret = filename_lookup(dfd, name, lookup_flags, &path, NULL);
3347 if (unlikely(ret))
3348 return ERR_PTR(ret);
3349
3350 if (flags & OPEN_TREE_NAMESPACE)
3351 return open_new_namespace(&path, (flags & AT_RECURSIVE) ? MOUNT_COPY_RECURSIVE : 0);
3352
3353 if (flags & OPEN_TREE_CLONE)
3354 return open_detached_copy(&path, flags);
3355
3356 return dentry_open(&path, O_PATH, current_cred());
3357 }
3358
3359 SYSCALL_DEFINE3(open_tree, int, dfd, const char __user *, filename, unsigned, flags)
3360 {
3361 return FD_ADD(flags, vfs_open_tree(dfd, filename, flags));
3362 }
3363
3364 /*
3365 * Don't allow locked mount flags to be cleared.
3366 *
3367 * No locks need to be held here while testing the various MNT_LOCK
3368 * flags because those flags can never be cleared once they are set.
3369 */
3370 static bool can_change_locked_flags(struct mount *mnt, unsigned int mnt_flags)
3371 {
3372 unsigned int fl = mnt->mnt.mnt_flags;
3373
3374 if ((fl & MNT_LOCK_READONLY) &&
3375 !(mnt_flags & MNT_READONLY))
3376 return false;
3377
3378 if ((fl & MNT_LOCK_NODEV) &&
3379 !(mnt_flags & MNT_NODEV))
3380 return false;
3381
3382 if ((fl & MNT_LOCK_NOSUID) &&
3383 !(mnt_flags & MNT_NOSUID))
3384 return false;
3385
3386 if ((fl & MNT_LOCK_NOEXEC) &&
3387 !(mnt_flags & MNT_NOEXEC))
3388 return false;
3389
3390 if ((fl & MNT_LOCK_ATIME) &&
3391 ((fl & MNT_ATIME_MASK) != (mnt_flags & MNT_ATIME_MASK)))
3392 return false;
3393
3394 return true;
3395 }
3396
3397 static int change_mount_ro_state(struct mount *mnt, unsigned int mnt_flags)
3398 {
3399 bool readonly_request = (mnt_flags & MNT_READONLY);
3400
3401 if (readonly_request == __mnt_is_readonly(&mnt->mnt))
3402 return 0;
3403
3404 if (readonly_request)
3405 return mnt_make_readonly(mnt);
3406
3407 mnt->mnt.mnt_flags &= ~MNT_READONLY;
3408 return 0;
3409 }
3410
3411 static void set_mount_attributes(struct mount *mnt, unsigned int mnt_flags)
3412 {
3413 mnt_flags |= mnt->mnt.mnt_flags & ~MNT_USER_SETTABLE_MASK;
3414 mnt->mnt.mnt_flags = mnt_flags;
3415 touch_mnt_namespace(mnt->mnt_ns);
3416 }
3417
3418 static void mnt_warn_timestamp_expiry(const struct path *mountpoint,
3419 struct vfsmount *mnt)
3420 {
3421 struct super_block *sb = mnt->mnt_sb;
3422
3423 if (!__mnt_is_readonly(mnt) &&
3424 (!(sb->s_iflags & SB_I_TS_EXPIRY_WARNED)) &&
3425 (ktime_get_real_seconds() + TIME_UPTIME_SEC_MAX > sb->s_time_max)) {
3426 char *buf, *mntpath;
3427
3428 buf = __getname();
3429 if (buf)
3430 mntpath = d_path(mountpoint, buf, PATH_MAX);
3431 else
3432 mntpath = ERR_PTR(-ENOMEM);
3433 if (IS_ERR(mntpath))
3434 mntpath = "(unknown)";
3435
3436 pr_warn("%s filesystem being %s at %s supports timestamps until %ptTd (0x%llx)\n",
3437 sb->s_type->name,
3438 is_mounted(mnt) ? "remounted" : "mounted",
3439 mntpath, &sb->s_time_max,
3440 (unsigned long long)sb->s_time_max);
3441
3442 sb->s_iflags |= SB_I_TS_EXPIRY_WARNED;
3443 __putname(buf);
3444 }
3445 }
3446
3447 /*
3448 * Handle reconfiguration of the mountpoint only without alteration of the
3449 * superblock it refers to. This is triggered by specifying MS_REMOUNT|MS_BIND
3450 * to mount(2).
3451 */
3452 static int do_reconfigure_mnt(const struct path *path, unsigned int mnt_flags)
3453 {
3454 struct super_block *sb = path->mnt->mnt_sb;
3455 struct mount *mnt = real_mount(path->mnt);
3456 int ret;
3457
3458 if (!check_mnt(mnt))
3459 return -EINVAL;
3460
3461 if (!path_mounted(path))
3462 return -EINVAL;
3463
3464 if (!can_change_locked_flags(mnt, mnt_flags))
3465 return -EPERM;
3466
3467 /*
3468 * We're only checking whether the superblock is read-only not
3469 * changing it, so only take down_read(&sb->s_umount).
3470 */
3471 down_read(&sb->s_umount);
3472 lock_mount_hash();
3473 ret = change_mount_ro_state(mnt, mnt_flags);
3474 if (ret == 0)
3475 set_mount_attributes(mnt, mnt_flags);
3476 unlock_mount_hash();
3477 up_read(&sb->s_umount);
3478
3479 mnt_warn_timestamp_expiry(path, &mnt->mnt);
3480
3481 return ret;
3482 }
3483
3484 /*
3485 * change filesystem flags. dir should be a physical root of filesystem.
3486 * If you've mounted a non-root directory somewhere and want to do remount
3487 * on it - tough luck.
3488 */
3489 static int do_remount(const struct path *path, int sb_flags,
3490 int mnt_flags, void *data)
3491 {
3492 int err;
3493 struct super_block *sb = path->mnt->mnt_sb;
3494 struct mount *mnt = real_mount(path->mnt);
3495 struct fs_context *fc;
3496
3497 if (!check_mnt(mnt))
3498 return -EINVAL;
3499
3500 if (!path_mounted(path))
3501 return -EINVAL;
3502
3503 if (!can_change_locked_flags(mnt, mnt_flags))
3504 return -EPERM;
3505
3506 fc = fs_context_for_reconfigure(path->dentry, sb_flags, MS_RMT_MASK);
3507 if (IS_ERR(fc))
3508 return PTR_ERR(fc);
3509
3510 /*
3511 * Indicate to the filesystem that the remount request is coming
3512 * from the legacy mount system call.
3513 */
3514 fc->oldapi = true;
3515
3516 err = parse_monolithic_mount_data(fc, data);
3517 if (!err) {
3518 down_write(&sb->s_umount);
3519 err = -EPERM;
3520 if (ns_capable(sb->s_user_ns, CAP_SYS_ADMIN)) {
3521 err = reconfigure_super(fc);
3522 if (!err) {
3523 lock_mount_hash();
3524 set_mount_attributes(mnt, mnt_flags);
3525 unlock_mount_hash();
3526 }
3527 }
3528 up_write(&sb->s_umount);
3529 }
3530
3531 mnt_warn_timestamp_expiry(path, &mnt->mnt);
3532
3533 put_fs_context(fc);
3534 return err;
3535 }
3536
3537 static inline int tree_contains_unbindable(struct mount *mnt)
3538 {
3539 struct mount *p;
3540 for (p = mnt; p; p = next_mnt(p, mnt)) {
3541 if (IS_MNT_UNBINDABLE(p))
3542 return 1;
3543 }
3544 return 0;
3545 }
3546
3547 static int do_set_group(const struct path *from_path, const struct path *to_path)
3548 {
3549 struct mount *from = real_mount(from_path->mnt);
3550 struct mount *to = real_mount(to_path->mnt);
3551 int err;
3552
3553 guard(namespace_excl)();
3554
3555 err = may_change_propagation(from);
3556 if (err)
3557 return err;
3558 err = may_change_propagation(to);
3559 if (err)
3560 return err;
3561
3562 /* To and From paths should be mount roots */
3563 if (!path_mounted(from_path))
3564 return -EINVAL;
3565 if (!path_mounted(to_path))
3566 return -EINVAL;
3567
3568 /* Setting sharing groups is only allowed across same superblock */
3569 if (from->mnt.mnt_sb != to->mnt.mnt_sb)
3570 return -EINVAL;
3571
3572 /* From mount root should be wider than To mount root */
3573 if (!is_subdir(to->mnt.mnt_root, from->mnt.mnt_root))
3574 return -EINVAL;
3575
3576 /* From mount should not have locked children in place of To's root */
3577 if (has_locked_children(from, to->mnt.mnt_root))
3578 return -EINVAL;
3579
3580 /* Setting sharing groups is only allowed on private mounts */
3581 if (IS_MNT_SHARED(to) || IS_MNT_SLAVE(to) || IS_MNT_UNBINDABLE(to))
3582 return -EINVAL;
3583
3584 /* From should not be private */
3585 if (!IS_MNT_SHARED(from) && !IS_MNT_SLAVE(from))
3586 return -EINVAL;
3587
3588 if (IS_MNT_SLAVE(from)) {
3589 hlist_add_behind(&to->mnt_slave, &from->mnt_slave);
3590 to->mnt_master = from->mnt_master;
3591 }
3592
3593 if (IS_MNT_SHARED(from)) {
3594 to->mnt_group_id = from->mnt_group_id;
3595 list_add(&to->mnt_share, &from->mnt_share);
3596 set_mnt_shared(to);
3597 }
3598
3599 guard(mount_locked_reader)();
3600 touch_mnt_namespace(to->mnt_ns);
3601
3602 return 0;
3603 }
3604
3605 /**
3606 * path_overmounted - check if path is overmounted
3607 * @path: path to check
3608 *
3609 * Check if path is overmounted, i.e., if there's a mount on top of
3610 * @path->mnt with @path->dentry as mountpoint.
3611 *
3612 * Context: namespace_sem must be held at least shared.
3613 * MUST NOT be called under lock_mount_hash() (there one should just
3614 * call __lookup_mnt() and check if it returns NULL).
3615 * Return: If path is overmounted true is returned, false if not.
3616 */
3617 static inline bool path_overmounted(const struct path *path)
3618 {
3619 unsigned seq = read_seqbegin(&mount_lock);
3620 bool no_child;
3621
3622 rcu_read_lock();
3623 no_child = !__lookup_mnt(path->mnt, path->dentry);
3624 rcu_read_unlock();
3625 if (need_seqretry(&mount_lock, seq)) {
3626 read_seqlock_excl(&mount_lock);
3627 no_child = !__lookup_mnt(path->mnt, path->dentry);
3628 read_sequnlock_excl(&mount_lock);
3629 }
3630 return unlikely(!no_child);
3631 }
3632
3633 /*
3634 * Check if there is a possibly empty chain of descent from p1 to p2.
3635 * Locks: namespace_sem (shared) or mount_lock (read_seqlock_excl).
3636 */
3637 static bool mount_is_ancestor(const struct mount *p1, const struct mount *p2)
3638 {
3639 while (p2 != p1 && mnt_has_parent(p2))
3640 p2 = p2->mnt_parent;
3641 return p2 == p1;
3642 }
3643
3644 /**
3645 * can_move_mount_beneath - check that we can mount beneath the top mount
3646 * @mnt_from: mount we are trying to move
3647 * @mnt_to: mount under which to mount
3648 * @mp: mountpoint of @mnt_to
3649 *
3650 * - Make sure that the caller can unmount the topmost mount ensuring
3651 * that the caller could reveal the underlying mountpoint.
3652 * - Ensure that nothing has been mounted on top of @mnt_from before we
3653 * grabbed @namespace_sem to avoid creating pointless shadow mounts.
3654 * - Prevent mounting beneath a mount if the propagation relationship
3655 * between the source mount, parent mount, and top mount would lead to
3656 * nonsensical mount trees.
3657 *
3658 * Context: This function expects namespace_lock() to be held.
3659 * Return: On success 0, and on error a negative error code is returned.
3660 */
3661 static int can_move_mount_beneath(const struct mount *mnt_from,
3662 const struct mount *mnt_to,
3663 struct pinned_mountpoint *mp)
3664 {
3665 struct mount *parent_mnt_to = mnt_to->mnt_parent;
3666
3667 /* Avoid creating shadow mounts during mount propagation. */
3668 if (mnt_from->overmount)
3669 return -EINVAL;
3670
3671 if (mount_is_ancestor(mnt_to, mnt_from))
3672 return -EINVAL;
3673
3674 /*
3675 * If the parent mount propagates to the child mount this would
3676 * mean mounting @mnt_from on @mnt_to->mnt_parent and then
3677 * propagating a copy @c of @mnt_from on top of @mnt_to. This
3678 * defeats the whole purpose of mounting beneath another mount.
3679 */
3680 if (propagation_would_overmount(parent_mnt_to, mnt_to, mp->mp))
3681 return -EINVAL;
3682
3683 /*
3684 * If @mnt_to->mnt_parent propagates to @mnt_from this would
3685 * mean propagating a copy @c of @mnt_from on top of @mnt_from.
3686 * Afterwards @mnt_from would be mounted on top of
3687 * @mnt_to->mnt_parent and @mnt_to would be unmounted from
3688 * @mnt->mnt_parent and remounted on @mnt_from. But since @c is
3689 * already mounted on @mnt_from, @mnt_to would ultimately be
3690 * remounted on top of @c. Afterwards, @mnt_from would be
3691 * covered by a copy @c of @mnt_from and @c would be covered by
3692 * @mnt_from itself. This defeats the whole purpose of mounting
3693 * @mnt_from beneath @mnt_to.
3694 */
3695 if (check_mnt(mnt_from) &&
3696 propagation_would_overmount(parent_mnt_to, mnt_from, mp->mp))
3697 return -EINVAL;
3698
3699 return 0;
3700 }
3701
3702 /* may_use_mount() - check if a mount tree can be used
3703 * @mnt: vfsmount to be used
3704 *
3705 * This helper checks if the caller may use the mount tree starting
3706 * from @path->mnt. The caller may use the mount tree under the
3707 * following circumstances:
3708 *
3709 * (1) The caller is located in the mount namespace of the mount tree.
3710 * This also implies that the mount does not belong to an anonymous
3711 * mount namespace.
3712 * (2) The caller is trying to use a mount tree that belongs to an
3713 * anonymous mount namespace.
3714 *
3715 * For that to be safe, this helper enforces that the origin mount
3716 * namespace the anonymous mount namespace was created from is the
3717 * same as the caller's mount namespace by comparing the sequence
3718 * numbers.
3719 *
3720 * The ownership of a non-anonymous mount namespace such as the
3721 * caller's cannot change.
3722 * => We know that the caller's mount namespace is stable.
3723 *
3724 * If the origin sequence number of the anonymous mount namespace is
3725 * the same as the sequence number of the caller's mount namespace.
3726 * => The owning namespaces are the same.
3727 *
3728 * ==> The earlier capability check on the owning namespace of the
3729 * caller's mount namespace ensures that the caller has the
3730 * ability to use the mount tree.
3731 *
3732 * Returns true if the mount tree can be used, false otherwise.
3733 */
3734 static inline bool may_use_mount(struct mount *mnt)
3735 {
3736 if (check_mnt(mnt))
3737 return true;
3738
3739 /*
3740 * Make sure that noone unmounted the target path or somehow
3741 * managed to get their hands on something purely kernel
3742 * internal.
3743 */
3744 if (!is_mounted(&mnt->mnt))
3745 return false;
3746
3747 return check_anonymous_mnt(mnt);
3748 }
3749
3750 static int do_move_mount(const struct path *old_path,
3751 const struct path *new_path,
3752 enum mnt_tree_flags_t flags)
3753 {
3754 struct mount *old = real_mount(old_path->mnt);
3755 int err;
3756 bool beneath = flags & MNT_TREE_BENEATH;
3757
3758 if (!path_mounted(old_path))
3759 return -EINVAL;
3760
3761 if (d_is_dir(new_path->dentry) != d_is_dir(old_path->dentry))
3762 return -EINVAL;
3763
3764 LOCK_MOUNT_MAYBE_BENEATH(mp, new_path, beneath);
3765 if (IS_ERR(mp.parent))
3766 return PTR_ERR(mp.parent);
3767
3768 if (check_mnt(old)) {
3769 /* if the source is in our namespace... */
3770 /* ... it should be detachable from parent */
3771 if (!mnt_has_parent(old) || IS_MNT_LOCKED(old))
3772 return -EINVAL;
3773 /* ... which should not be shared */
3774 if (IS_MNT_SHARED(old->mnt_parent))
3775 return -EINVAL;
3776 /* ... and the target should be in our namespace */
3777 if (!check_mnt(mp.parent))
3778 return -EINVAL;
3779 } else {
3780 /*
3781 * otherwise the source must be the root of some anon namespace.
3782 */
3783 if (!anon_ns_root(old))
3784 return -EINVAL;
3785 /*
3786 * Bail out early if the target is within the same namespace -
3787 * subsequent checks would've rejected that, but they lose
3788 * some corner cases if we check it early.
3789 */
3790 if (old->mnt_ns == mp.parent->mnt_ns)
3791 return -EINVAL;
3792 /*
3793 * Target should be either in our namespace or in an acceptable
3794 * anon namespace, sensu check_anonymous_mnt().
3795 */
3796 if (!may_use_mount(mp.parent))
3797 return -EINVAL;
3798 }
3799
3800 if (beneath) {
3801 struct mount *over = real_mount(new_path->mnt);
3802
3803 if (mp.parent != over->mnt_parent)
3804 over = mp.parent->overmount;
3805 err = can_move_mount_beneath(old, over, &mp);
3806 if (err)
3807 return err;
3808 }
3809
3810 /*
3811 * Don't move a mount tree containing unbindable mounts to a destination
3812 * mount which is shared.
3813 */
3814 if (IS_MNT_SHARED(mp.parent) && tree_contains_unbindable(old))
3815 return -EINVAL;
3816 if (!check_for_nsfs_mounts(old))
3817 return -ELOOP;
3818 if (mount_is_ancestor(old, mp.parent))
3819 return -ELOOP;
3820
3821 return attach_recursive_mnt(old, &mp);
3822 }
3823
3824 static int do_move_mount_old(const struct path *path, const char *old_name)
3825 {
3826 struct path old_path __free(path_put) = {};
3827 int err;
3828
3829 if (!old_name || !*old_name)
3830 return -EINVAL;
3831
3832 err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
3833 if (err)
3834 return err;
3835
3836 return do_move_mount(&old_path, path, 0);
3837 }
3838
3839 /*
3840 * add a mount into a namespace's mount tree
3841 */
3842 static int do_add_mount(struct mount *newmnt, const struct pinned_mountpoint *mp,
3843 int mnt_flags)
3844 {
3845 struct mount *parent = mp->parent;
3846
3847 if (IS_ERR(parent))
3848 return PTR_ERR(parent);
3849
3850 mnt_flags &= ~MNT_INTERNAL_FLAGS;
3851
3852 if (unlikely(!check_mnt(parent))) {
3853 /* that's acceptable only for automounts done in private ns */
3854 if (!(mnt_flags & MNT_SHRINKABLE))
3855 return -EINVAL;
3856 /* ... and for those we'd better have mountpoint still alive */
3857 if (!is_mounted(&parent->mnt))
3858 return -EINVAL;
3859 }
3860
3861 /* Refuse the same filesystem on the same mount point */
3862 if (parent->mnt.mnt_sb == newmnt->mnt.mnt_sb &&
3863 parent->mnt.mnt_root == mp->mp->m_dentry)
3864 return -EBUSY;
3865
3866 if (d_is_symlink(newmnt->mnt.mnt_root))
3867 return -EINVAL;
3868
3869 newmnt->mnt.mnt_flags = mnt_flags;
3870 return graft_tree(newmnt, mp);
3871 }
3872
3873 static bool mount_too_revealing(const struct super_block *sb, int *new_mnt_flags);
3874
3875 /*
3876 * Create a new mount using a superblock configuration and request it
3877 * be added to the namespace tree.
3878 */
3879 static int do_new_mount_fc(struct fs_context *fc, const struct path *mountpoint,
3880 unsigned int mnt_flags)
3881 {
3882 struct super_block *sb;
3883 struct vfsmount *mnt __free(mntput) = fc_mount(fc);
3884 int error;
3885
3886 if (IS_ERR(mnt))
3887 return PTR_ERR(mnt);
3888
3889 sb = fc->root->d_sb;
3890 error = security_sb_kern_mount(sb);
3891 if (unlikely(error))
3892 return error;
3893
3894 if (unlikely(mount_too_revealing(sb, &mnt_flags))) {
3895 errorfcp(fc, "VFS", "Mount too revealing");
3896 return -EPERM;
3897 }
3898
3899 mnt_warn_timestamp_expiry(mountpoint, mnt);
3900
3901 LOCK_MOUNT(mp, mountpoint);
3902 error = do_add_mount(real_mount(mnt), &mp, mnt_flags);
3903 if (!error)
3904 retain_and_null_ptr(mnt); // consumed on success
3905 return error;
3906 }
3907
3908 /*
3909 * create a new mount for userspace and request it to be added into the
3910 * namespace's tree
3911 */
3912 static int do_new_mount(const struct path *path, const char *fstype,
3913 int sb_flags, int mnt_flags,
3914 const char *name, void *data)
3915 {
3916 struct file_system_type *type;
3917 struct fs_context *fc;
3918 const char *subtype = NULL;
3919 int err = 0;
3920
3921 if (!fstype)
3922 return -EINVAL;
3923
3924 type = get_fs_type(fstype);
3925 if (!type)
3926 return -ENODEV;
3927
3928 if (type->fs_flags & FS_HAS_SUBTYPE) {
3929 subtype = strchr(fstype, '.');
3930 if (subtype) {
3931 subtype++;
3932 if (!*subtype) {
3933 put_filesystem(type);
3934 return -EINVAL;
3935 }
3936 }
3937 }
3938
3939 fc = fs_context_for_mount(type, sb_flags);
3940 put_filesystem(type);
3941 if (IS_ERR(fc))
3942 return PTR_ERR(fc);
3943
3944 /*
3945 * Indicate to the filesystem that the mount request is coming
3946 * from the legacy mount system call.
3947 */
3948 fc->oldapi = true;
3949
3950 if (subtype)
3951 err = vfs_parse_fs_string(fc, "subtype", subtype);
3952 if (!err && name)
3953 err = vfs_parse_fs_string(fc, "source", name);
3954 if (!err)
3955 err = parse_monolithic_mount_data(fc, data);
3956 if (!err && !mount_capable(fc))
3957 err = -EPERM;
3958 if (!err)
3959 err = do_new_mount_fc(fc, path, mnt_flags);
3960
3961 put_fs_context(fc);
3962 return err;
3963 }
3964
3965 static void lock_mount_exact(const struct path *path,
3966 struct pinned_mountpoint *mp, bool copy_mount,
3967 unsigned int copy_flags)
3968 {
3969 struct dentry *dentry = path->dentry;
3970 int err;
3971
3972 /* Assert that inode_lock() locked the correct inode. */
3973 VFS_WARN_ON_ONCE(copy_mount && !path_mounted(path));
3974
3975 inode_lock(dentry->d_inode);
3976 namespace_lock();
3977 if (unlikely(cant_mount(dentry)))
3978 err = -ENOENT;
3979 else if (!copy_mount && path_overmounted(path))
3980 err = -EBUSY;
3981 else
3982 err = get_mountpoint(dentry, mp);
3983 if (unlikely(err)) {
3984 namespace_unlock();
3985 inode_unlock(dentry->d_inode);
3986 mp->parent = ERR_PTR(err);
3987 return;
3988 }
3989
3990 if (copy_mount)
3991 mp->parent = clone_mnt(real_mount(path->mnt), dentry, copy_flags);
3992 else
3993 mp->parent = real_mount(path->mnt);
3994 if (unlikely(IS_ERR(mp->parent)))
3995 __unlock_mount(mp);
3996 }
3997
3998 int finish_automount(struct vfsmount *__m, const struct path *path)
3999 {
4000 struct vfsmount *m __free(mntput) = __m;
4001 struct mount *mnt;
4002 int err;
4003
4004 if (!m)
4005 return 0;
4006 if (IS_ERR(m))
4007 return PTR_ERR(m);
4008
4009 mnt = real_mount(m);
4010
4011 if (m->mnt_root == path->dentry)
4012 return -ELOOP;
4013
4014 /*
4015 * we don't want to use LOCK_MOUNT() - in this case finding something
4016 * that overmounts our mountpoint to be means "quitely drop what we've
4017 * got", not "try to mount it on top".
4018 */
4019 LOCK_MOUNT_EXACT(mp, path);
4020 if (mp.parent == ERR_PTR(-EBUSY))
4021 return 0;
4022
4023 err = do_add_mount(mnt, &mp, path->mnt->mnt_flags | MNT_SHRINKABLE);
4024 if (likely(!err))
4025 retain_and_null_ptr(m);
4026 return err;
4027 }
4028
4029 /**
4030 * mnt_set_expiry - Put a mount on an expiration list
4031 * @mnt: The mount to list.
4032 * @expiry_list: The list to add the mount to.
4033 */
4034 void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
4035 {
4036 guard(mount_locked_reader)();
4037 list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list);
4038 }
4039 EXPORT_SYMBOL(mnt_set_expiry);
4040
4041 /*
4042 * process a list of expirable mountpoints with the intent of discarding any
4043 * mountpoints that aren't in use and haven't been touched since last we came
4044 * here
4045 */
4046 void mark_mounts_for_expiry(struct list_head *mounts)
4047 {
4048 struct mount *mnt, *next;
4049 LIST_HEAD(graveyard);
4050
4051 if (list_empty(mounts))
4052 return;
4053
4054 guard(namespace_excl)();
4055 guard(mount_writer)();
4056
4057 /* extract from the expiration list every vfsmount that matches the
4058 * following criteria:
4059 * - already mounted
4060 * - only referenced by its parent vfsmount
4061 * - still marked for expiry (marked on the last call here; marks are
4062 * cleared by mntput())
4063 */
4064 list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
4065 if (!is_mounted(&mnt->mnt))
4066 continue;
4067 /* lock_mnt_tree() leaves expirable mounts alone */
4068 VFS_WARN_ON_ONCE(IS_MNT_LOCKED(mnt));
4069 if (!xchg(&mnt->mnt_expiry_mark, 1) ||
4070 propagate_mount_busy(mnt, 1))
4071 continue;
4072 list_move(&mnt->mnt_expire, &graveyard);
4073 }
4074 while (!list_empty(&graveyard)) {
4075 mnt = list_first_entry(&graveyard, struct mount, mnt_expire);
4076 /* an earlier umount_tree() may have moved a busy mount here */
4077 if (propagate_mount_busy(mnt, 1)) {
4078 list_move(&mnt->mnt_expire, mounts);
4079 continue;
4080 }
4081 touch_mnt_namespace(mnt->mnt_ns);
4082 umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
4083 }
4084 }
4085
4086 EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
4087
4088 /*
4089 * Unmount @mnt if it's a shrinkable mount without children that nobody uses.
4090 *
4091 * mount_lock must be held for write
4092 */
4093 static bool shrink_submount(struct mount *mnt)
4094 {
4095 /* not the kernel's to remove either */
4096 if (IS_MNT_LOCKED(mnt) || propagate_mount_busy(mnt, 1))
4097 return false;
4098 touch_mnt_namespace(mnt->mnt_ns);
4099 umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
4100 return true;
4101 }
4102
4103 /*
4104 * Ripoff of 'select_parent()'
4105 *
4106 * unmount the shrinkable submounts of @parent that aren't busy, children
4107 * before their parent, and say whether anything went
4108 *
4109 * The cursor into the children of @this_parent survives the umount of a
4110 * child mount without child mounts. The mounts that get umounted together with
4111 * it are located under receiving mounts of @this_parent and never under
4112 * @this_parent itself. The one exception is @this_parent getting unmounted
4113 * then the walk starts over.
4114 */
4115 static bool __shrink_submounts(struct mount *parent)
4116 {
4117 struct mount *this_parent = parent;
4118 struct list_head *next;
4119 bool shrunk = false;
4120
4121 repeat:
4122 next = this_parent->mnt_mounts.next;
4123 resume:
4124 while (next != &this_parent->mnt_mounts) {
4125 struct list_head *tmp = next;
4126 struct mount *mnt = list_entry(tmp, struct mount, mnt_child);
4127
4128 next = tmp->next;
4129 if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE))
4130 continue;
4131 /*
4132 * Descend a level if the d_mounts list is non-empty.
4133 */
4134 if (!list_empty(&mnt->mnt_mounts)) {
4135 this_parent = mnt;
4136 goto repeat;
4137 }
4138 if (!shrink_submount(mnt))
4139 continue;
4140 shrunk = true;
4141 if (unlikely(this_parent->mnt.mnt_flags & MNT_UMOUNT))
4142 return true;
4143 }
4144 /*
4145 * All done at this level ... ascend and resume the search
4146 */
4147 if (this_parent != parent) {
4148 struct mount *mnt = this_parent;
4149
4150 next = mnt->mnt_child.next;
4151 this_parent = mnt->mnt_parent;
4152 /* its children are gone, maybe it can go as well */
4153 if (shrink_submount(mnt)) {
4154 shrunk = true;
4155 if (unlikely(this_parent->mnt.mnt_flags & MNT_UMOUNT))
4156 return true;
4157 }
4158 goto resume;
4159 }
4160 return shrunk;
4161 }
4162
4163 /*
4164 * unmount the shrinkable submounts of @mnt that aren't busy
4165 *
4166 * The busy check and the umount of a mount are adjacent. An umount can
4167 * still empty or move a mount in a part of the tree that was walked
4168 * already, so walk again until nothing goes.
4169 *
4170 * mount_lock must be held for write
4171 */
4172 static void shrink_submounts(struct mount *mnt)
4173 {
4174 for (;;) {
4175 if (!__shrink_submounts(mnt))
4176 break;
4177 }
4178 }
4179
4180 static void *copy_mount_options(const void __user * data)
4181 {
4182 char *copy;
4183 unsigned left, offset;
4184
4185 if (!data)
4186 return NULL;
4187
4188 copy = kmalloc(PAGE_SIZE, GFP_KERNEL);
4189 if (!copy)
4190 return ERR_PTR(-ENOMEM);
4191
4192 left = copy_from_user(copy, data, PAGE_SIZE);
4193
4194 /*
4195 * Not all architectures have an exact copy_from_user(). Resort to
4196 * byte at a time.
4197 */
4198 offset = PAGE_SIZE - left;
4199 while (left) {
4200 char c;
4201 if (get_user(c, (const char __user *)data + offset))
4202 break;
4203 copy[offset] = c;
4204 left--;
4205 offset++;
4206 }
4207
4208 if (left == PAGE_SIZE) {
4209 kfree(copy);
4210 return ERR_PTR(-EFAULT);
4211 }
4212
4213 return copy;
4214 }
4215
4216 static char *copy_mount_string(const void __user *data)
4217 {
4218 return data ? strndup_user(data, PATH_MAX) : NULL;
4219 }
4220
4221 /*
4222 * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
4223 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
4224 *
4225 * data is a (void *) that can point to any structure up to
4226 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
4227 * information (or be NULL).
4228 *
4229 * Pre-0.97 versions of mount() didn't have a flags word.
4230 * When the flags word was introduced its top half was required
4231 * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
4232 * Therefore, if this magic number is present, it carries no information
4233 * and must be discarded.
4234 */
4235 int path_mount(const char *dev_name, const struct path *path,
4236 const char *type_page, unsigned long flags, void *data_page)
4237 {
4238 unsigned int mnt_flags = 0, sb_flags;
4239 int ret;
4240
4241 /* Discard magic */
4242 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
4243 flags &= ~MS_MGC_MSK;
4244
4245 /* Basic sanity checks */
4246 if (data_page)
4247 ((char *)data_page)[PAGE_SIZE - 1] = 0;
4248
4249 if (flags & MS_NOUSER)
4250 return -EINVAL;
4251
4252 ret = security_sb_mount(dev_name, path, type_page, flags, data_page);
4253 if (ret)
4254 return ret;
4255 if (!may_mount())
4256 return -EPERM;
4257 if (flags & SB_MANDLOCK)
4258 warn_mandlock();
4259
4260 /* Default to relatime unless overriden */
4261 if (!(flags & MS_NOATIME))
4262 mnt_flags |= MNT_RELATIME;
4263
4264 /* Separate the per-mountpoint flags */
4265 if (flags & MS_NOSUID)
4266 mnt_flags |= MNT_NOSUID;
4267 if (flags & MS_NODEV)
4268 mnt_flags |= MNT_NODEV;
4269 if (flags & MS_NOEXEC)
4270 mnt_flags |= MNT_NOEXEC;
4271 if (flags & MS_NOATIME)
4272 mnt_flags |= MNT_NOATIME;
4273 if (flags & MS_NODIRATIME)
4274 mnt_flags |= MNT_NODIRATIME;
4275 if (flags & MS_STRICTATIME)
4276 mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
4277 if (flags & MS_RDONLY)
4278 mnt_flags |= MNT_READONLY;
4279 if (flags & MS_NOSYMFOLLOW)
4280 mnt_flags |= MNT_NOSYMFOLLOW;
4281
4282 /* The default atime for remount is preservation */
4283 if ((flags & MS_REMOUNT) &&
4284 ((flags & (MS_NOATIME | MS_NODIRATIME | MS_RELATIME |
4285 MS_STRICTATIME)) == 0)) {
4286 mnt_flags &= ~MNT_ATIME_MASK;
4287 mnt_flags |= path->mnt->mnt_flags & MNT_ATIME_MASK;
4288 }
4289
4290 sb_flags = flags & (SB_RDONLY |
4291 SB_SYNCHRONOUS |
4292 SB_MANDLOCK |
4293 SB_DIRSYNC |
4294 SB_SILENT |
4295 SB_POSIXACL |
4296 SB_LAZYTIME |
4297 SB_I_VERSION);
4298
4299 if ((flags & (MS_REMOUNT | MS_BIND)) == (MS_REMOUNT | MS_BIND))
4300 return do_reconfigure_mnt(path, mnt_flags);
4301 if (flags & MS_REMOUNT)
4302 return do_remount(path, sb_flags, mnt_flags, data_page);
4303 if (flags & MS_BIND)
4304 return do_loopback(path, dev_name, flags & MS_REC);
4305 if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
4306 return do_change_type(path, flags);
4307 if (flags & MS_MOVE)
4308 return do_move_mount_old(path, dev_name);
4309
4310 return do_new_mount(path, type_page, sb_flags, mnt_flags, dev_name,
4311 data_page);
4312 }
4313
4314 int do_mount(const char *dev_name, const char __user *dir_name,
4315 const char *type_page, unsigned long flags, void *data_page)
4316 {
4317 struct path path __free(path_put) = {};
4318 int ret;
4319
4320 ret = user_path_at(AT_FDCWD, dir_name, LOOKUP_FOLLOW, &path);
4321 if (ret)
4322 return ret;
4323 return path_mount(dev_name, &path, type_page, flags, data_page);
4324 }
4325
4326 static struct ucounts *inc_mnt_namespaces(struct user_namespace *ns)
4327 {
4328 return inc_ucount(ns, current_euid(), UCOUNT_MNT_NAMESPACES);
4329 }
4330
4331 static void dec_mnt_namespaces(struct ucounts *ucounts)
4332 {
4333 dec_ucount(ucounts, UCOUNT_MNT_NAMESPACES);
4334 }
4335
4336 static void free_mnt_ns(struct mnt_namespace *ns)
4337 {
4338 if (!is_anon_ns(ns))
4339 ns_common_free(ns);
4340 dec_mnt_namespaces(ns->ucounts);
4341 /* the last active reference is gone, no mark can show up anymore */
4342 fsnotify_mntns_delete(ns);
4343 mnt_ns_tree_remove(ns);
4344 }
4345
4346 static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool anon)
4347 {
4348 struct mnt_namespace *new_ns;
4349 struct ucounts *ucounts;
4350 int ret;
4351
4352 ucounts = inc_mnt_namespaces(user_ns);
4353 if (!ucounts)
4354 return ERR_PTR(-ENOSPC);
4355
4356 new_ns = kzalloc_obj(struct mnt_namespace, GFP_KERNEL_ACCOUNT);
4357 if (!new_ns) {
4358 dec_mnt_namespaces(ucounts);
4359 return ERR_PTR(-ENOMEM);
4360 }
4361
4362 if (anon)
4363 ret = ns_common_init_inum(new_ns, MNT_NS_ANON_INO);
4364 else
4365 ret = ns_common_init(new_ns);
4366 if (ret) {
4367 kfree(new_ns);
4368 dec_mnt_namespaces(ucounts);
4369 return ERR_PTR(ret);
4370 }
4371 ns_tree_gen_id(new_ns);
4372
4373 new_ns->is_anon = anon;
4374 refcount_set(&new_ns->passive, 1);
4375 new_ns->mounts = RB_ROOT;
4376 init_waitqueue_head(&new_ns->poll);
4377 new_ns->user_ns = get_user_ns(user_ns);
4378 new_ns->ucounts = ucounts;
4379 return new_ns;
4380 }
4381
4382 __latent_entropy
4383 struct mnt_namespace *copy_mnt_ns(u64 flags, struct mnt_namespace *ns,
4384 struct user_namespace *user_ns, struct fs_struct *new_fs)
4385 {
4386 struct mnt_namespace *new_ns;
4387 struct path old_root __free(path_put) = {};
4388 struct path old_pwd __free(path_put) = {};
4389 struct mount *p, *q;
4390 struct mount *old;
4391 struct mount *new;
4392 int copy_flags;
4393
4394 BUG_ON(!ns);
4395
4396 if (likely(!(flags & CLONE_NEWNS))) {
4397 get_mnt_ns(ns);
4398 return ns;
4399 }
4400
4401 old = ns->root;
4402
4403 new_ns = alloc_mnt_ns(user_ns, false);
4404 if (IS_ERR(new_ns))
4405 return new_ns;
4406
4407 guard(namespace_excl)();
4408
4409 if (flags & CLONE_EMPTY_MNTNS)
4410 copy_flags = 0;
4411 else
4412 copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE;
4413 if (user_ns != ns->user_ns)
4414 copy_flags |= CL_SLAVE;
4415
4416 if (flags & CLONE_EMPTY_MNTNS)
4417 new = clone_mnt(old, old->mnt.mnt_root, copy_flags);
4418 else
4419 new = copy_tree(old, old->mnt.mnt_root, copy_flags);
4420 if (IS_ERR(new)) {
4421 emptied_ns = new_ns;
4422 return ERR_CAST(new);
4423 }
4424 if (user_ns != ns->user_ns) {
4425 guard(mount_writer)();
4426 lock_mnt_tree(new);
4427 }
4428 new_ns->root = new;
4429
4430 if (flags & CLONE_EMPTY_MNTNS) {
4431 /*
4432 * Empty mount namespace: only the root mount exists.
4433 * Reset root and pwd to the cloned mount's root dentry.
4434 */
4435 if (new_fs) {
4436 old_root = new_fs->root;
4437 old_pwd = new_fs->pwd;
4438
4439 new_fs->root.mnt = mntget(&new->mnt);
4440 new_fs->root.dentry = dget(new->mnt.mnt_root);
4441
4442 new_fs->pwd.mnt = mntget(&new->mnt);
4443 new_fs->pwd.dentry = dget(new->mnt.mnt_root);
4444 }
4445 mnt_add_to_ns(new_ns, new);
4446 new_ns->nr_mounts++;
4447 } else {
4448 /*
4449 * Full copy: walk old and new trees in parallel, switching
4450 * the tsk->fs->* elements and marking new vfsmounts as
4451 * belonging to new namespace. We have already acquired a
4452 * private fs_struct, so tsk->fs->lock is not needed.
4453 */
4454 p = old;
4455 q = new;
4456 while (p) {
4457 mnt_add_to_ns(new_ns, q);
4458 new_ns->nr_mounts++;
4459 if (new_fs) {
4460 if (&p->mnt == new_fs->root.mnt) {
4461 old_root.mnt = new_fs->root.mnt;
4462 new_fs->root.mnt = mntget(&q->mnt);
4463 }
4464 if (&p->mnt == new_fs->pwd.mnt) {
4465 old_pwd.mnt = new_fs->pwd.mnt;
4466 new_fs->pwd.mnt = mntget(&q->mnt);
4467 }
4468 }
4469 p = next_mnt(p, old);
4470 q = next_mnt(q, new);
4471 if (!q)
4472 break;
4473 // an mntns binding we'd skipped?
4474 while (p->mnt.mnt_root != q->mnt.mnt_root)
4475 p = next_mnt(skip_mnt_tree(p), old);
4476 }
4477 }
4478 ns_tree_add_raw(new_ns);
4479 return new_ns;
4480 }
4481
4482 struct dentry *mount_subtree(struct vfsmount *m, const char *name)
4483 {
4484 struct mount *mnt = real_mount(m);
4485 struct mnt_namespace *ns;
4486 struct super_block *s;
4487 struct path path;
4488 int err;
4489
4490 ns = alloc_mnt_ns(&init_user_ns, true);
4491 if (IS_ERR(ns)) {
4492 mntput(m);
4493 return ERR_CAST(ns);
4494 }
4495 ns->root = mnt;
4496 ns->nr_mounts++;
4497 mnt_add_to_ns(ns, mnt);
4498
4499 err = vfs_path_lookup(m->mnt_root, m,
4500 name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
4501
4502 put_mnt_ns(ns);
4503
4504 if (err)
4505 return ERR_PTR(err);
4506
4507 /* trade a vfsmount reference for active sb one */
4508 s = path.mnt->mnt_sb;
4509 atomic_inc(&s->s_active);
4510 mntput(path.mnt);
4511 /* lock the sucker */
4512 down_write(&s->s_umount);
4513 /* ... and return the root of (sub)tree on it */
4514 return path.dentry;
4515 }
4516 EXPORT_SYMBOL(mount_subtree);
4517
4518 SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
4519 char __user *, type, unsigned long, flags, void __user *, data)
4520 {
4521 int ret;
4522 char *kernel_type;
4523 char *kernel_dev;
4524 void *options;
4525
4526 kernel_type = copy_mount_string(type);
4527 ret = PTR_ERR(kernel_type);
4528 if (IS_ERR(kernel_type))
4529 goto out_type;
4530
4531 kernel_dev = copy_mount_string(dev_name);
4532 ret = PTR_ERR(kernel_dev);
4533 if (IS_ERR(kernel_dev))
4534 goto out_dev;
4535
4536 options = copy_mount_options(data);
4537 ret = PTR_ERR(options);
4538 if (IS_ERR(options))
4539 goto out_data;
4540
4541 ret = do_mount(kernel_dev, dir_name, kernel_type, flags, options);
4542
4543 kfree(options);
4544 out_data:
4545 kfree(kernel_dev);
4546 out_dev:
4547 kfree(kernel_type);
4548 out_type:
4549 return ret;
4550 }
4551
4552 #define FSMOUNT_VALID_FLAGS \
4553 (MOUNT_ATTR_RDONLY | MOUNT_ATTR_NOSUID | MOUNT_ATTR_NODEV | \
4554 MOUNT_ATTR_NOEXEC | MOUNT_ATTR__ATIME | MOUNT_ATTR_NODIRATIME | \
4555 MOUNT_ATTR_NOSYMFOLLOW)
4556
4557 #define MOUNT_SETATTR_VALID_FLAGS (FSMOUNT_VALID_FLAGS | MOUNT_ATTR_IDMAP)
4558
4559 #define MOUNT_SETATTR_PROPAGATION_FLAGS \
4560 (MS_UNBINDABLE | MS_PRIVATE | MS_SLAVE | MS_SHARED)
4561
4562 static unsigned int attr_flags_to_mnt_flags(u64 attr_flags)
4563 {
4564 unsigned int mnt_flags = 0;
4565
4566 if (attr_flags & MOUNT_ATTR_RDONLY)
4567 mnt_flags |= MNT_READONLY;
4568 if (attr_flags & MOUNT_ATTR_NOSUID)
4569 mnt_flags |= MNT_NOSUID;
4570 if (attr_flags & MOUNT_ATTR_NODEV)
4571 mnt_flags |= MNT_NODEV;
4572 if (attr_flags & MOUNT_ATTR_NOEXEC)
4573 mnt_flags |= MNT_NOEXEC;
4574 if (attr_flags & MOUNT_ATTR_NODIRATIME)
4575 mnt_flags |= MNT_NODIRATIME;
4576 if (attr_flags & MOUNT_ATTR_NOSYMFOLLOW)
4577 mnt_flags |= MNT_NOSYMFOLLOW;
4578
4579 return mnt_flags;
4580 }
4581
4582 /*
4583 * Create a kernel mount representation for a new, prepared superblock
4584 * (specified by fs_fd) and attach to an open_tree-like file descriptor.
4585 */
4586 SYSCALL_DEFINE3(fsmount, int, fs_fd, unsigned int, flags,
4587 unsigned int, attr_flags)
4588 {
4589 struct path new_path __free(path_put) = {};
4590 struct mnt_namespace *ns;
4591 struct fs_context *fc;
4592 struct vfsmount *new_mnt;
4593 struct mount *mnt;
4594 unsigned int mnt_flags = 0;
4595 long ret;
4596
4597 if ((flags & ~(FSMOUNT_CLOEXEC | FSMOUNT_NAMESPACE)) != 0)
4598 return -EINVAL;
4599
4600 if ((flags & FSMOUNT_NAMESPACE) &&
4601 !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
4602 return -EPERM;
4603
4604 if (!(flags & FSMOUNT_NAMESPACE) && !may_mount())
4605 return -EPERM;
4606
4607 if (attr_flags & ~FSMOUNT_VALID_FLAGS)
4608 return -EINVAL;
4609
4610 mnt_flags = attr_flags_to_mnt_flags(attr_flags);
4611
4612 switch (attr_flags & MOUNT_ATTR__ATIME) {
4613 case MOUNT_ATTR_STRICTATIME:
4614 break;
4615 case MOUNT_ATTR_NOATIME:
4616 mnt_flags |= MNT_NOATIME;
4617 break;
4618 case MOUNT_ATTR_RELATIME:
4619 mnt_flags |= MNT_RELATIME;
4620 break;
4621 default:
4622 return -EINVAL;
4623 }
4624
4625 CLASS(fd, f)(fs_fd);
4626 if (fd_empty(f))
4627 return -EBADF;
4628
4629 if (fd_file(f)->f_op != &fscontext_fops)
4630 return -EINVAL;
4631
4632 fc = fd_file(f)->private_data;
4633
4634 ACQUIRE(mutex_intr, uapi_mutex)(&fc->uapi_mutex);
4635 ret = ACQUIRE_ERR(mutex_intr, &uapi_mutex);
4636 if (ret)
4637 return ret;
4638
4639 /* There must be a valid superblock or we can't mount it */
4640 ret = -EINVAL;
4641 if (!fc->root)
4642 return ret;
4643
4644 ret = -EPERM;
4645 if (mount_too_revealing(fc->root->d_sb, &mnt_flags)) {
4646 errorfcp(fc, "VFS", "Mount too revealing");
4647 return ret;
4648 }
4649
4650 ret = -EBUSY;
4651 if (fc->phase != FS_CONTEXT_AWAITING_MOUNT)
4652 return ret;
4653
4654 if (fc->sb_flags & SB_MANDLOCK)
4655 warn_mandlock();
4656
4657 new_mnt = vfs_create_mount(fc);
4658 if (IS_ERR(new_mnt))
4659 return PTR_ERR(new_mnt);
4660 if (new_mnt->mnt_sb->s_flags & SB_NOUSER) {
4661 mntput(new_mnt);
4662 return -EINVAL;
4663 }
4664 new_mnt->mnt_flags = mnt_flags;
4665
4666 new_path.dentry = dget(fc->root);
4667 new_path.mnt = new_mnt;
4668
4669 /* We've done the mount bit - now move the file context into more or
4670 * less the same state as if we'd done an fspick(). We don't want to
4671 * do any memory allocation or anything like that at this point as we
4672 * don't want to have to handle any errors incurred.
4673 */
4674 vfs_clean_context(fc);
4675
4676 if (flags & FSMOUNT_NAMESPACE)
4677 return FD_ADD((flags & FSMOUNT_CLOEXEC) ? O_CLOEXEC : 0,
4678 open_new_namespace(&new_path, MOUNT_COPY_NEW));
4679
4680 ns = alloc_mnt_ns(current->nsproxy->mnt_ns->user_ns, true);
4681 if (IS_ERR(ns))
4682 return PTR_ERR(ns);
4683 mnt = real_mount(new_path.mnt);
4684 ns->root = mnt;
4685 ns->nr_mounts = 1;
4686 mnt_add_to_ns(ns, mnt);
4687 mntget(new_path.mnt);
4688
4689 FD_PREPARE(fdf, (flags & FSMOUNT_CLOEXEC) ? O_CLOEXEC : 0,
4690 dentry_open(&new_path, O_PATH, fc->cred));
4691 if (fdf.err) {
4692 dissolve_on_fput(no_free_ptr(new_path.mnt));
4693 return fdf.err;
4694 }
4695
4696 /*
4697 * Attach to an apparent O_PATH fd with a note that we
4698 * need to unmount it, not just simply put it.
4699 */
4700 fd_prepare_file(fdf)->f_mode |= FMODE_NEED_UNMOUNT;
4701 return fd_publish(fdf);
4702 }
4703
4704 static inline int vfs_move_mount(const struct path *from_path,
4705 const struct path *to_path,
4706 enum mnt_tree_flags_t mflags)
4707 {
4708 int ret;
4709
4710 ret = security_move_mount(from_path, to_path);
4711 if (ret)
4712 return ret;
4713
4714 if (mflags & MNT_TREE_PROPAGATION)
4715 return do_set_group(from_path, to_path);
4716
4717 return do_move_mount(from_path, to_path, mflags);
4718 }
4719
4720 /*
4721 * Move a mount from one place to another. In combination with
4722 * fsopen()/fsmount() this is used to install a new mount and in combination
4723 * with open_tree(OPEN_TREE_CLONE [| AT_RECURSIVE]) it can be used to copy
4724 * a mount subtree.
4725 *
4726 * Note the flags value is a combination of MOVE_MOUNT_* flags.
4727 */
4728 SYSCALL_DEFINE5(move_mount,
4729 int, from_dfd, const char __user *, from_pathname,
4730 int, to_dfd, const char __user *, to_pathname,
4731 unsigned int, flags)
4732 {
4733 struct path to_path __free(path_put) = {};
4734 struct path from_path __free(path_put) = {};
4735 unsigned int lflags, uflags;
4736 enum mnt_tree_flags_t mflags = 0;
4737 int ret = 0;
4738
4739 if (!may_mount())
4740 return -EPERM;
4741
4742 if (flags & ~MOVE_MOUNT__MASK)
4743 return -EINVAL;
4744
4745 if ((flags & (MOVE_MOUNT_BENEATH | MOVE_MOUNT_SET_GROUP)) ==
4746 (MOVE_MOUNT_BENEATH | MOVE_MOUNT_SET_GROUP))
4747 return -EINVAL;
4748
4749 if (flags & MOVE_MOUNT_SET_GROUP) mflags |= MNT_TREE_PROPAGATION;
4750 if (flags & MOVE_MOUNT_BENEATH) mflags |= MNT_TREE_BENEATH;
4751
4752 uflags = 0;
4753 if (flags & MOVE_MOUNT_T_EMPTY_PATH)
4754 uflags = AT_EMPTY_PATH;
4755
4756 CLASS(filename_maybe_null,to_name)(to_pathname, uflags);
4757 if (!to_name && to_dfd >= 0) {
4758 CLASS(fd_raw, f_to)(to_dfd);
4759 if (fd_empty(f_to))
4760 return -EBADF;
4761
4762 to_path = fd_file(f_to)->f_path;
4763 path_get(&to_path);
4764 } else {
4765 lflags = 0;
4766 if (flags & MOVE_MOUNT_T_SYMLINKS)
4767 lflags |= LOOKUP_FOLLOW;
4768 if (flags & MOVE_MOUNT_T_AUTOMOUNTS)
4769 lflags |= LOOKUP_AUTOMOUNT;
4770 ret = filename_lookup(to_dfd, to_name, lflags, &to_path, NULL);
4771 if (ret)
4772 return ret;
4773 }
4774
4775 uflags = 0;
4776 if (flags & MOVE_MOUNT_F_EMPTY_PATH)
4777 uflags = AT_EMPTY_PATH;
4778
4779 CLASS(filename_maybe_null,from_name)(from_pathname, uflags);
4780 if (!from_name && from_dfd >= 0) {
4781 CLASS(fd_raw, f_from)(from_dfd);
4782 if (fd_empty(f_from))
4783 return -EBADF;
4784
4785 return vfs_move_mount(&fd_file(f_from)->f_path, &to_path, mflags);
4786 }
4787
4788 lflags = 0;
4789 if (flags & MOVE_MOUNT_F_SYMLINKS)
4790 lflags |= LOOKUP_FOLLOW;
4791 if (flags & MOVE_MOUNT_F_AUTOMOUNTS)
4792 lflags |= LOOKUP_AUTOMOUNT;
4793 ret = filename_lookup(from_dfd, from_name, lflags, &from_path, NULL);
4794 if (ret)
4795 return ret;
4796
4797 return vfs_move_mount(&from_path, &to_path, mflags);
4798 }
4799
4800 /*
4801 * Return true if path is reachable from root
4802 *
4803 * locks: mount_locked_reader || namespace_shared && is_mounted(mnt)
4804 */
4805 bool is_path_reachable(struct mount *mnt, struct dentry *dentry,
4806 const struct path *root)
4807 {
4808 while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) {
4809 dentry = mnt->mnt_mountpoint;
4810 mnt = mnt->mnt_parent;
4811 }
4812 return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry);
4813 }
4814
4815 bool path_is_under(const struct path *path1, const struct path *path2)
4816 {
4817 guard(mount_locked_reader)();
4818 return is_path_reachable(real_mount(path1->mnt), path1->dentry, path2);
4819 }
4820 EXPORT_SYMBOL(path_is_under);
4821
4822 int path_pivot_root(struct path *new, struct path *old)
4823 {
4824 struct path root __free(path_put) = {};
4825 struct mount *new_mnt, *root_mnt, *old_mnt, *root_parent, *ex_parent;
4826 int error;
4827
4828 if (!may_mount())
4829 return -EPERM;
4830
4831 error = security_sb_pivotroot(old, new);
4832 if (error)
4833 return error;
4834
4835 get_fs_root(current->fs, &root);
4836
4837 LOCK_MOUNT(old_mp, old);
4838 old_mnt = old_mp.parent;
4839 if (IS_ERR(old_mnt))
4840 return PTR_ERR(old_mnt);
4841
4842 new_mnt = real_mount(new->mnt);
4843 root_mnt = real_mount(root.mnt);
4844 /* only a mounted mount has a parent that namespace_sem pins */
4845 if (!check_mnt(root_mnt) || !check_mnt(new_mnt))
4846 return -EINVAL;
4847 ex_parent = new_mnt->mnt_parent;
4848 root_parent = root_mnt->mnt_parent;
4849 if (IS_MNT_SHARED(old_mnt) ||
4850 IS_MNT_SHARED(ex_parent) ||
4851 IS_MNT_SHARED(root_parent))
4852 return -EINVAL;
4853 if (new_mnt->mnt.mnt_flags & MNT_LOCKED)
4854 return -EINVAL;
4855 if (d_unlinked(new->dentry))
4856 return -ENOENT;
4857 if (new_mnt == root_mnt || old_mnt == root_mnt)
4858 return -EBUSY; /* loop, on the same file system */
4859 if (!path_mounted(&root))
4860 return -EINVAL; /* not a mountpoint */
4861 if (!mnt_has_parent(root_mnt))
4862 return -EINVAL; /* absolute root */
4863 if (!path_mounted(new))
4864 return -EINVAL; /* not a mountpoint */
4865 if (!mnt_has_parent(new_mnt))
4866 return -EINVAL; /* absolute root */
4867 /* make sure we can reach put_old from new_root */
4868 if (!is_path_reachable(old_mnt, old_mp.mp->m_dentry, new))
4869 return -EINVAL;
4870 /* make certain new is below the root */
4871 if (!is_path_reachable(new_mnt, new->dentry, &root))
4872 return -EINVAL;
4873 lock_mount_hash();
4874 umount_mnt(new_mnt);
4875 if (root_mnt->mnt.mnt_flags & MNT_LOCKED) {
4876 new_mnt->mnt.mnt_flags |= MNT_LOCKED;
4877 root_mnt->mnt.mnt_flags &= ~MNT_LOCKED;
4878 }
4879 /* mount new_root on / */
4880 attach_mnt(new_mnt, root_parent, root_mnt->mnt_mp);
4881 umount_mnt(root_mnt);
4882 /* mount old root on put_old */
4883 attach_mnt(root_mnt, old_mnt, old_mp.mp);
4884 touch_mnt_namespace(current->nsproxy->mnt_ns);
4885 /* A moved mount should not expire automatically */
4886 list_del_init(&new_mnt->mnt_expire);
4887 unlock_mount_hash();
4888 mnt_notify_add(root_mnt);
4889 mnt_notify_add(new_mnt);
4890 chroot_fs_refs(&root, new);
4891 return 0;
4892 }
4893
4894 /*
4895 * pivot_root Semantics:
4896 * Moves the root file system of the current process to the directory put_old,
4897 * makes new_root as the new root file system of the current process, and sets
4898 * root/cwd of all processes which had them on the current root to new_root.
4899 *
4900 * Restrictions:
4901 * The new_root and put_old must be directories, and must not be on the
4902 * same file system as the current process root. The put_old must be
4903 * underneath new_root, i.e. adding a non-zero number of /.. to the string
4904 * pointed to by put_old must yield the same directory as new_root. No other
4905 * file system may be mounted on put_old. After all, new_root is a mountpoint.
4906 *
4907 * The immutable nullfs filesystem is mounted as the true root of the VFS
4908 * hierarchy. The mutable rootfs (tmpfs/ramfs) is layered on top of this,
4909 * allowing pivot_root() to work normally from initramfs.
4910 *
4911 * Notes:
4912 * - we don't move root/cwd if they are not at the root (reason: if something
4913 * cared enough to change them, it's probably wrong to force them elsewhere)
4914 * - it's okay to pick a root that isn't the root of a file system, e.g.
4915 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
4916 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
4917 * first.
4918 */
4919 SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
4920 const char __user *, put_old)
4921 {
4922 struct path new __free(path_put) = {};
4923 struct path old __free(path_put) = {};
4924 int error;
4925
4926 error = user_path_at(AT_FDCWD, new_root,
4927 LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &new);
4928 if (error)
4929 return error;
4930
4931 error = user_path_at(AT_FDCWD, put_old,
4932 LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &old);
4933 if (error)
4934 return error;
4935
4936 return path_pivot_root(&new, &old);
4937 }
4938
4939 static unsigned int recalc_flags(struct mount_kattr *kattr, struct mount *mnt)
4940 {
4941 unsigned int flags = mnt->mnt.mnt_flags;
4942
4943 /* flags to clear */
4944 flags &= ~kattr->attr_clr;
4945 /* flags to raise */
4946 flags |= kattr->attr_set;
4947
4948 return flags;
4949 }
4950
4951 static int can_idmap_mount(const struct mount_kattr *kattr, struct mount *mnt)
4952 {
4953 struct vfsmount *m = &mnt->mnt;
4954 struct user_namespace *fs_userns = m->mnt_sb->s_user_ns;
4955
4956 if (!kattr->mnt_idmap)
4957 return 0;
4958
4959 /*
4960 * Creating an idmapped mount with the filesystem wide idmapping
4961 * doesn't make sense so block that. We don't allow mushy semantics.
4962 */
4963 if (kattr->mnt_userns == m->mnt_sb->s_user_ns)
4964 return -EINVAL;
4965
4966 /*
4967 * We only allow an mount to change it's idmapping if it has
4968 * never been accessible to userspace.
4969 */
4970 if (!(kattr->kflags & MOUNT_KATTR_IDMAP_REPLACE) && is_idmapped_mnt(m))
4971 return -EPERM;
4972
4973 /* The underlying filesystem doesn't support idmapped mounts yet. */
4974 if (!(m->mnt_sb->s_type->fs_flags & FS_ALLOW_IDMAP))
4975 return -EINVAL;
4976
4977 /* The filesystem has turned off idmapped mounts. */
4978 if (m->mnt_sb->s_iflags & SB_I_NOIDMAP)
4979 return -EINVAL;
4980
4981 /* We're not controlling the superblock. */
4982 if (!ns_capable(fs_userns, CAP_SYS_ADMIN))
4983 return -EPERM;
4984
4985 /* Mount has already been visible in the filesystem hierarchy. */
4986 if (!is_anon_ns(mnt->mnt_ns))
4987 return -EINVAL;
4988
4989 return 0;
4990 }
4991
4992 /**
4993 * mnt_allow_writers() - check whether the attribute change allows writers
4994 * @kattr: the new mount attributes
4995 * @mnt: the mount to which @kattr will be applied
4996 *
4997 * Check whether thew new mount attributes in @kattr allow concurrent writers.
4998 *
4999 * Return: true if writers need to be held, false if not
5000 */
5001 static inline bool mnt_allow_writers(const struct mount_kattr *kattr,
5002 const struct mount *mnt)
5003 {
5004 return (!(kattr->attr_set & MNT_READONLY) ||
5005 (mnt->mnt.mnt_flags & MNT_READONLY)) &&
5006 !kattr->mnt_idmap;
5007 }
5008
5009 static int mount_setattr_prepare(struct mount_kattr *kattr, struct mount *mnt)
5010 {
5011 struct mount *m;
5012 int err;
5013
5014 for (m = mnt; m; m = next_mnt(m, mnt)) {
5015 if (!can_change_locked_flags(m, recalc_flags(kattr, m))) {
5016 err = -EPERM;
5017 break;
5018 }
5019
5020 err = can_idmap_mount(kattr, m);
5021 if (err)
5022 break;
5023
5024 if (!mnt_allow_writers(kattr, m)) {
5025 err = mnt_hold_writers(m);
5026 if (err) {
5027 m = next_mnt(m, mnt);
5028 break;
5029 }
5030 }
5031
5032 if (!(kattr->kflags & MOUNT_KATTR_RECURSE))
5033 return 0;
5034 }
5035
5036 if (err) {
5037 /* undo all mnt_hold_writers() we'd done */
5038 for (struct mount *p = mnt; p != m; p = next_mnt(p, mnt))
5039 mnt_unhold_writers(p);
5040 }
5041 return err;
5042 }
5043
5044 static void do_idmap_mount(const struct mount_kattr *kattr, struct mount *mnt)
5045 {
5046 struct mnt_idmap *old_idmap;
5047
5048 if (!kattr->mnt_idmap)
5049 return;
5050
5051 old_idmap = mnt_idmap(&mnt->mnt);
5052
5053 /* Pairs with smp_load_acquire() in mnt_idmap(). */
5054 smp_store_release(&mnt->mnt.mnt_idmap, mnt_idmap_get(kattr->mnt_idmap));
5055 mnt_idmap_put(old_idmap);
5056 }
5057
5058 static void mount_setattr_commit(struct mount_kattr *kattr, struct mount *mnt)
5059 {
5060 struct mount *m;
5061
5062 for (m = mnt; m; m = next_mnt(m, mnt)) {
5063 unsigned int flags;
5064
5065 do_idmap_mount(kattr, m);
5066 flags = recalc_flags(kattr, m);
5067 WRITE_ONCE(m->mnt.mnt_flags, flags);
5068
5069 /* If we had to hold writers unblock them. */
5070 mnt_unhold_writers(m);
5071
5072 if (kattr->propagation)
5073 change_mnt_propagation(m, kattr->propagation);
5074 if (!(kattr->kflags & MOUNT_KATTR_RECURSE))
5075 break;
5076 }
5077 touch_mnt_namespace(mnt->mnt_ns);
5078 }
5079
5080 static int do_mount_setattr(const struct path *path, struct mount_kattr *kattr)
5081 {
5082 struct mount *mnt = real_mount(path->mnt);
5083 int err = 0;
5084
5085 if (!path_mounted(path))
5086 return -EINVAL;
5087
5088 if (kattr->mnt_userns) {
5089 struct mnt_idmap *mnt_idmap;
5090
5091 mnt_idmap = alloc_mnt_idmap(kattr->mnt_userns);
5092 if (IS_ERR(mnt_idmap))
5093 return PTR_ERR(mnt_idmap);
5094 kattr->mnt_idmap = mnt_idmap;
5095 }
5096
5097 if (kattr->propagation) {
5098 /*
5099 * Only take namespace_lock() if we're actually changing
5100 * propagation.
5101 */
5102 namespace_lock();
5103 /* invent_group_ids() walks the tree, only walk a mounted one */
5104 if (!anon_ns_root(mnt) && !check_mnt(mnt)) {
5105 namespace_unlock();
5106 return -EINVAL;
5107 }
5108 if (kattr->propagation == MS_SHARED) {
5109 err = invent_group_ids(mnt, kattr->kflags & MOUNT_KATTR_RECURSE);
5110 if (err) {
5111 namespace_unlock();
5112 return err;
5113 }
5114 }
5115 }
5116
5117 err = -EINVAL;
5118 lock_mount_hash();
5119
5120 /* Checked under namespace_sem already if the propagation changes. */
5121 if (!anon_ns_root(mnt) && !check_mnt(mnt))
5122 goto out;
5123
5124 /*
5125 * First, we get the mount tree in a shape where we can change mount
5126 * properties without failure. If we succeeded to do so we commit all
5127 * changes and if we failed we clean up.
5128 */
5129 err = mount_setattr_prepare(kattr, mnt);
5130 if (!err)
5131 mount_setattr_commit(kattr, mnt);
5132
5133 out:
5134 unlock_mount_hash();
5135
5136 if (kattr->propagation) {
5137 if (err)
5138 cleanup_group_ids(mnt, NULL);
5139 namespace_unlock();
5140 }
5141
5142 return err;
5143 }
5144
5145 static int build_mount_idmapped(const struct mount_attr *attr, size_t usize,
5146 struct mount_kattr *kattr)
5147 {
5148 struct ns_common *ns;
5149 struct user_namespace *mnt_userns;
5150
5151 if (!((attr->attr_set | attr->attr_clr) & MOUNT_ATTR_IDMAP))
5152 return 0;
5153
5154 if (attr->attr_clr & MOUNT_ATTR_IDMAP) {
5155 /*
5156 * We can only remove an idmapping if it's never been
5157 * exposed to userspace.
5158 */
5159 if (!(kattr->kflags & MOUNT_KATTR_IDMAP_REPLACE))
5160 return -EINVAL;
5161
5162 /*
5163 * Removal of idmappings is equivalent to setting
5164 * nop_mnt_idmap.
5165 */
5166 if (!(attr->attr_set & MOUNT_ATTR_IDMAP)) {
5167 kattr->mnt_idmap = &nop_mnt_idmap;
5168 return 0;
5169 }
5170 }
5171
5172 if (attr->userns_fd > INT_MAX)
5173 return -EINVAL;
5174
5175 CLASS(fd, f)(attr->userns_fd);
5176 if (fd_empty(f))
5177 return -EBADF;
5178
5179 if (!proc_ns_file(fd_file(f)))
5180 return -EINVAL;
5181
5182 ns = get_proc_ns(file_inode(fd_file(f)));
5183 if (ns->ns_type != CLONE_NEWUSER)
5184 return -EINVAL;
5185
5186 /*
5187 * The initial idmapping cannot be used to create an idmapped
5188 * mount. We use the initial idmapping as an indicator of a mount
5189 * that is not idmapped. It can simply be passed into helpers that
5190 * are aware of idmapped mounts as a convenient shortcut. A user
5191 * can just create a dedicated identity mapping to achieve the same
5192 * result.
5193 */
5194 mnt_userns = container_of(ns, struct user_namespace, ns);
5195 if (mnt_userns == &init_user_ns)
5196 return -EPERM;
5197
5198 /* We're not controlling the target namespace. */
5199 if (!ns_capable(mnt_userns, CAP_SYS_ADMIN))
5200 return -EPERM;
5201
5202 kattr->mnt_userns = get_user_ns(mnt_userns);
5203 return 0;
5204 }
5205
5206 static int build_mount_kattr(const struct mount_attr *attr, size_t usize,
5207 struct mount_kattr *kattr)
5208 {
5209 if (attr->propagation & ~MOUNT_SETATTR_PROPAGATION_FLAGS)
5210 return -EINVAL;
5211 if (hweight32(attr->propagation & MOUNT_SETATTR_PROPAGATION_FLAGS) > 1)
5212 return -EINVAL;
5213 kattr->propagation = attr->propagation;
5214
5215 if ((attr->attr_set | attr->attr_clr) & ~MOUNT_SETATTR_VALID_FLAGS)
5216 return -EINVAL;
5217
5218 kattr->attr_set = attr_flags_to_mnt_flags(attr->attr_set);
5219 kattr->attr_clr = attr_flags_to_mnt_flags(attr->attr_clr);
5220
5221 /*
5222 * Since the MOUNT_ATTR_<atime> values are an enum, not a bitmap,
5223 * users wanting to transition to a different atime setting cannot
5224 * simply specify the atime setting in @attr_set, but must also
5225 * specify MOUNT_ATTR__ATIME in the @attr_clr field.
5226 * So ensure that MOUNT_ATTR__ATIME can't be partially set in
5227 * @attr_clr and that @attr_set can't have any atime bits set if
5228 * MOUNT_ATTR__ATIME isn't set in @attr_clr.
5229 */
5230 if (attr->attr_clr & MOUNT_ATTR__ATIME) {
5231 if ((attr->attr_clr & MOUNT_ATTR__ATIME) != MOUNT_ATTR__ATIME)
5232 return -EINVAL;
5233
5234 /*
5235 * Clear all previous time settings as they are mutually
5236 * exclusive.
5237 */
5238 kattr->attr_clr |= MNT_RELATIME | MNT_NOATIME;
5239 switch (attr->attr_set & MOUNT_ATTR__ATIME) {
5240 case MOUNT_ATTR_RELATIME:
5241 kattr->attr_set |= MNT_RELATIME;
5242 break;
5243 case MOUNT_ATTR_NOATIME:
5244 kattr->attr_set |= MNT_NOATIME;
5245 break;
5246 case MOUNT_ATTR_STRICTATIME:
5247 break;
5248 default:
5249 return -EINVAL;
5250 }
5251 } else {
5252 if (attr->attr_set & MOUNT_ATTR__ATIME)
5253 return -EINVAL;
5254 }
5255
5256 return build_mount_idmapped(attr, usize, kattr);
5257 }
5258
5259 static void finish_mount_kattr(struct mount_kattr *kattr)
5260 {
5261 if (kattr->mnt_userns) {
5262 put_user_ns(kattr->mnt_userns);
5263 kattr->mnt_userns = NULL;
5264 }
5265
5266 if (kattr->mnt_idmap)
5267 mnt_idmap_put(kattr->mnt_idmap);
5268 }
5269
5270 static int wants_mount_setattr(struct mount_attr __user *uattr, size_t usize,
5271 struct mount_kattr *kattr)
5272 {
5273 int ret;
5274 struct mount_attr attr;
5275
5276 BUILD_BUG_ON(sizeof(struct mount_attr) != MOUNT_ATTR_SIZE_VER0);
5277
5278 if (unlikely(usize > PAGE_SIZE))
5279 return -E2BIG;
5280 if (unlikely(usize < MOUNT_ATTR_SIZE_VER0))
5281 return -EINVAL;
5282
5283 if (!may_mount())
5284 return -EPERM;
5285
5286 ret = copy_struct_from_user(&attr, sizeof(attr), uattr, usize);
5287 if (ret)
5288 return ret;
5289
5290 /* Don't bother walking through the mounts if this is a nop. */
5291 if (attr.attr_set == 0 &&
5292 attr.attr_clr == 0 &&
5293 attr.propagation == 0)
5294 return 0; /* Tell caller to not bother. */
5295
5296 ret = build_mount_kattr(&attr, usize, kattr);
5297 if (ret < 0)
5298 return ret;
5299
5300 return 1;
5301 }
5302
5303 SYSCALL_DEFINE5(mount_setattr, int, dfd, const char __user *, path,
5304 unsigned int, flags, struct mount_attr __user *, uattr,
5305 size_t, usize)
5306 {
5307 int err;
5308 struct path target;
5309 struct mount_kattr kattr;
5310 unsigned int lookup_flags = LOOKUP_AUTOMOUNT | LOOKUP_FOLLOW;
5311
5312 if (flags & ~(AT_EMPTY_PATH |
5313 AT_RECURSIVE |
5314 AT_SYMLINK_NOFOLLOW |
5315 AT_NO_AUTOMOUNT))
5316 return -EINVAL;
5317
5318 if (flags & AT_NO_AUTOMOUNT)
5319 lookup_flags &= ~LOOKUP_AUTOMOUNT;
5320 if (flags & AT_SYMLINK_NOFOLLOW)
5321 lookup_flags &= ~LOOKUP_FOLLOW;
5322
5323 kattr = (struct mount_kattr) {
5324 .lookup_flags = lookup_flags,
5325 };
5326
5327 if (flags & AT_RECURSIVE)
5328 kattr.kflags |= MOUNT_KATTR_RECURSE;
5329
5330 err = wants_mount_setattr(uattr, usize, &kattr);
5331 if (err <= 0)
5332 return err;
5333
5334 CLASS(filename_uflags, name)(path, flags);
5335 err = filename_lookup(dfd, name, kattr.lookup_flags, &target, NULL);
5336 if (!err) {
5337 err = do_mount_setattr(&target, &kattr);
5338 path_put(&target);
5339 }
5340 finish_mount_kattr(&kattr);
5341 return err;
5342 }
5343
5344 SYSCALL_DEFINE5(open_tree_attr, int, dfd, const char __user *, filename,
5345 unsigned, flags, struct mount_attr __user *, uattr,
5346 size_t, usize)
5347 {
5348 if (!uattr && usize)
5349 return -EINVAL;
5350
5351 FD_PREPARE(fdf, flags, vfs_open_tree(dfd, filename, flags));
5352 if (fdf.err)
5353 return fdf.err;
5354
5355 if (uattr) {
5356 struct mount_kattr kattr = {};
5357 struct file *file = fd_prepare_file(fdf);
5358 int ret;
5359
5360 if (flags & OPEN_TREE_CLONE)
5361 kattr.kflags = MOUNT_KATTR_IDMAP_REPLACE;
5362 if (flags & AT_RECURSIVE)
5363 kattr.kflags |= MOUNT_KATTR_RECURSE;
5364
5365 ret = wants_mount_setattr(uattr, usize, &kattr);
5366 if (ret > 0) {
5367 ret = do_mount_setattr(&file->f_path, &kattr);
5368 finish_mount_kattr(&kattr);
5369 }
5370 if (ret)
5371 return ret;
5372 }
5373
5374 return fd_publish(fdf);
5375 }
5376
5377 int show_path(struct seq_file *m, struct dentry *root)
5378 {
5379 if (root->d_sb->s_op->show_path)
5380 return root->d_sb->s_op->show_path(m, root);
5381
5382 seq_dentry(m, root, " \t\n\\");
5383 return 0;
5384 }
5385
5386 static struct vfsmount *lookup_mnt_in_ns(u64 id, struct mnt_namespace *ns)
5387 {
5388 struct mount *mnt = mnt_find_id_at(ns, id);
5389
5390 if (!mnt || mnt->mnt_id_unique != id)
5391 return NULL;
5392
5393 return &mnt->mnt;
5394 }
5395
5396 struct kstatmount {
5397 struct statmount __user *buf;
5398 size_t bufsize;
5399 struct vfsmount *mnt;
5400 struct mnt_idmap *idmap;
5401 u64 mask;
5402 struct path root;
5403 struct seq_file seq;
5404
5405 /* Must be last --ends in a flexible-array member. */
5406 struct statmount sm;
5407 };
5408
5409 static u64 mnt_to_attr_flags(struct vfsmount *mnt)
5410 {
5411 unsigned int mnt_flags = READ_ONCE(mnt->mnt_flags);
5412 u64 attr_flags = 0;
5413
5414 if (mnt_flags & MNT_READONLY)
5415 attr_flags |= MOUNT_ATTR_RDONLY;
5416 if (mnt_flags & MNT_NOSUID)
5417 attr_flags |= MOUNT_ATTR_NOSUID;
5418 if (mnt_flags & MNT_NODEV)
5419 attr_flags |= MOUNT_ATTR_NODEV;
5420 if (mnt_flags & MNT_NOEXEC)
5421 attr_flags |= MOUNT_ATTR_NOEXEC;
5422 if (mnt_flags & MNT_NODIRATIME)
5423 attr_flags |= MOUNT_ATTR_NODIRATIME;
5424 if (mnt_flags & MNT_NOSYMFOLLOW)
5425 attr_flags |= MOUNT_ATTR_NOSYMFOLLOW;
5426
5427 if (mnt_flags & MNT_NOATIME)
5428 attr_flags |= MOUNT_ATTR_NOATIME;
5429 else if (mnt_flags & MNT_RELATIME)
5430 attr_flags |= MOUNT_ATTR_RELATIME;
5431 else
5432 attr_flags |= MOUNT_ATTR_STRICTATIME;
5433
5434 if (is_idmapped_mnt(mnt))
5435 attr_flags |= MOUNT_ATTR_IDMAP;
5436
5437 return attr_flags;
5438 }
5439
5440 static u64 mnt_to_propagation_flags(struct mount *m)
5441 {
5442 u64 propagation = 0;
5443
5444 if (IS_MNT_SHARED(m))
5445 propagation |= MS_SHARED;
5446 if (IS_MNT_SLAVE(m))
5447 propagation |= MS_SLAVE;
5448 if (IS_MNT_UNBINDABLE(m))
5449 propagation |= MS_UNBINDABLE;
5450 if (!propagation)
5451 propagation |= MS_PRIVATE;
5452
5453 return propagation;
5454 }
5455
5456 u64 vfsmount_to_propagation_flags(struct vfsmount *mnt)
5457 {
5458 return mnt_to_propagation_flags(real_mount(mnt));
5459 }
5460 EXPORT_SYMBOL_GPL(vfsmount_to_propagation_flags);
5461
5462 static void statmount_sb_basic(struct kstatmount *s)
5463 {
5464 struct super_block *sb = s->mnt->mnt_sb;
5465
5466 s->sm.mask |= STATMOUNT_SB_BASIC;
5467 s->sm.sb_dev_major = MAJOR(sb->s_dev);
5468 s->sm.sb_dev_minor = MINOR(sb->s_dev);
5469 s->sm.sb_magic = sb->s_magic;
5470 s->sm.sb_flags = sb->s_flags & (SB_RDONLY|SB_SYNCHRONOUS|SB_DIRSYNC|SB_LAZYTIME);
5471 }
5472
5473 static void statmount_mnt_parent(struct kstatmount *s, const struct mount *m)
5474 {
5475 s->sm.mnt_parent_id = m->mnt_parent->mnt_id_unique;
5476 s->sm.mnt_parent_id_old = m->mnt_parent->mnt_id;
5477 }
5478
5479 static void statmount_mnt_basic(struct kstatmount *s)
5480 {
5481 struct mount *m = real_mount(s->mnt);
5482
5483 s->sm.mask |= STATMOUNT_MNT_BASIC;
5484 s->sm.mnt_id = m->mnt_id_unique;
5485 s->sm.mnt_id_old = m->mnt_id;
5486 /* An unmounted mount is cut loose from its parent under mount_lock alone. */
5487 if (likely(is_mounted(s->mnt)))
5488 statmount_mnt_parent(s, m);
5489 else
5490 scoped_guard(mount_locked_reader)
5491 statmount_mnt_parent(s, m);
5492 s->sm.mnt_attr = mnt_to_attr_flags(&m->mnt);
5493 s->sm.mnt_propagation = mnt_to_propagation_flags(m);
5494 s->sm.mnt_peer_group = m->mnt_group_id;
5495 s->sm.mnt_master = IS_MNT_SLAVE(m) ? m->mnt_master->mnt_group_id : 0;
5496 }
5497
5498 static void statmount_propagate_from(struct kstatmount *s)
5499 {
5500 struct mount *m = real_mount(s->mnt);
5501
5502 s->sm.mask |= STATMOUNT_PROPAGATE_FROM;
5503 if (IS_MNT_SLAVE(m))
5504 s->sm.propagate_from = get_dominating_id(m, &current->fs->root);
5505 }
5506
5507 static int statmount_mnt_root(struct kstatmount *s, struct seq_file *seq)
5508 {
5509 int ret;
5510 size_t start = seq->count;
5511
5512 ret = show_path(seq, s->mnt->mnt_root);
5513 if (ret)
5514 return ret;
5515
5516 if (unlikely(seq_has_overflowed(seq)))
5517 return -EAGAIN;
5518
5519 /*
5520 * Unescape the result. It would be better if supplied string was not
5521 * escaped in the first place, but that's a pretty invasive change.
5522 */
5523 seq->buf[seq->count] = '\0';
5524 seq->count = start;
5525 seq_commit(seq, string_unescape_inplace(seq->buf + start, UNESCAPE_OCTAL));
5526 return 0;
5527 }
5528
5529 static int statmount_mnt_point(struct kstatmount *s, struct seq_file *seq)
5530 {
5531 struct vfsmount *mnt = s->mnt;
5532 struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
5533 int err;
5534
5535 err = seq_path_root(seq, &mnt_path, &s->root, "");
5536 return err == SEQ_SKIP ? 0 : err;
5537 }
5538
5539 static int statmount_fs_type(struct kstatmount *s, struct seq_file *seq)
5540 {
5541 struct super_block *sb = s->mnt->mnt_sb;
5542
5543 seq_puts(seq, sb->s_type->name);
5544 return 0;
5545 }
5546
5547 static void statmount_fs_subtype(struct kstatmount *s, struct seq_file *seq)
5548 {
5549 struct super_block *sb = s->mnt->mnt_sb;
5550
5551 if (sb->s_subtype)
5552 seq_puts(seq, sb->s_subtype);
5553 }
5554
5555 static int statmount_sb_source(struct kstatmount *s, struct seq_file *seq)
5556 {
5557 struct super_block *sb = s->mnt->mnt_sb;
5558 struct mount *r = real_mount(s->mnt);
5559
5560 if (sb->s_op->show_devname) {
5561 size_t start = seq->count;
5562 int ret;
5563
5564 ret = sb->s_op->show_devname(seq, s->mnt->mnt_root);
5565 if (ret)
5566 return ret;
5567
5568 if (unlikely(seq_has_overflowed(seq)))
5569 return -EAGAIN;
5570
5571 /* Unescape the result */
5572 seq->buf[seq->count] = '\0';
5573 seq->count = start;
5574 seq_commit(seq, string_unescape_inplace(seq->buf + start, UNESCAPE_OCTAL));
5575 } else {
5576 seq_puts(seq, r->mnt_devname);
5577 }
5578 return 0;
5579 }
5580
5581 static void statmount_mnt_ns_id(struct kstatmount *s, struct mnt_namespace *ns)
5582 {
5583 s->sm.mask |= STATMOUNT_MNT_NS_ID;
5584 s->sm.mnt_ns_id = ns->ns.ns_id;
5585 }
5586
5587 static int statmount_mnt_opts(struct kstatmount *s, struct seq_file *seq)
5588 {
5589 struct vfsmount *mnt = s->mnt;
5590 struct super_block *sb = mnt->mnt_sb;
5591 size_t start = seq->count;
5592 int err;
5593
5594 err = security_sb_show_options(seq, sb);
5595 if (err)
5596 return err;
5597
5598 if (sb->s_op->show_options) {
5599 err = sb->s_op->show_options(seq, mnt->mnt_root);
5600 if (err)
5601 return err;
5602 }
5603
5604 if (unlikely(seq_has_overflowed(seq)))
5605 return -EAGAIN;
5606
5607 if (seq->count == start)
5608 return 0;
5609
5610 /* skip leading comma */
5611 memmove(seq->buf + start, seq->buf + start + 1,
5612 seq->count - start - 1);
5613 seq->count--;
5614
5615 return 0;
5616 }
5617
5618 static inline int statmount_opt_process(struct seq_file *seq, size_t start)
5619 {
5620 char *buf_end, *opt_end, *src, *dst;
5621 int count = 0;
5622
5623 if (unlikely(seq_has_overflowed(seq)))
5624 return -EAGAIN;
5625
5626 buf_end = seq->buf + seq->count;
5627 dst = seq->buf + start;
5628 src = dst + 1; /* skip initial comma */
5629
5630 if (src >= buf_end) {
5631 seq->count = start;
5632 return 0;
5633 }
5634
5635 *buf_end = '\0';
5636 for (; src < buf_end; src = opt_end + 1) {
5637 opt_end = strchrnul(src, ',');
5638 *opt_end = '\0';
5639 dst += string_unescape(src, dst, 0, UNESCAPE_OCTAL) + 1;
5640 if (WARN_ON_ONCE(++count == INT_MAX))
5641 return -EOVERFLOW;
5642 }
5643 seq->count = dst - 1 - seq->buf;
5644 return count;
5645 }
5646
5647 static int statmount_opt_array(struct kstatmount *s, struct seq_file *seq)
5648 {
5649 struct vfsmount *mnt = s->mnt;
5650 struct super_block *sb = mnt->mnt_sb;
5651 size_t start = seq->count;
5652 int err;
5653
5654 if (!sb->s_op->show_options)
5655 return 0;
5656
5657 err = sb->s_op->show_options(seq, mnt->mnt_root);
5658 if (err)
5659 return err;
5660
5661 err = statmount_opt_process(seq, start);
5662 if (err < 0)
5663 return err;
5664
5665 s->sm.opt_num = err;
5666 return 0;
5667 }
5668
5669 static int statmount_opt_sec_array(struct kstatmount *s, struct seq_file *seq)
5670 {
5671 struct vfsmount *mnt = s->mnt;
5672 struct super_block *sb = mnt->mnt_sb;
5673 size_t start = seq->count;
5674 int err;
5675
5676 err = security_sb_show_options(seq, sb);
5677 if (err)
5678 return err;
5679
5680 err = statmount_opt_process(seq, start);
5681 if (err < 0)
5682 return err;
5683
5684 s->sm.opt_sec_num = err;
5685 return 0;
5686 }
5687
5688 static inline int statmount_mnt_uidmap(struct kstatmount *s, struct seq_file *seq)
5689 {
5690 int ret;
5691
5692 ret = statmount_mnt_idmap(s->idmap, seq, true);
5693 if (ret < 0)
5694 return ret;
5695
5696 s->sm.mnt_uidmap_num = ret;
5697 /*
5698 * Always raise STATMOUNT_MNT_UIDMAP even if there are no valid
5699 * mappings. This allows userspace to distinguish between a
5700 * non-idmapped mount and an idmapped mount where none of the
5701 * individual mappings are valid in the caller's idmapping.
5702 */
5703 if (is_valid_mnt_idmap(s->idmap))
5704 s->sm.mask |= STATMOUNT_MNT_UIDMAP;
5705 return 0;
5706 }
5707
5708 static inline int statmount_mnt_gidmap(struct kstatmount *s, struct seq_file *seq)
5709 {
5710 int ret;
5711
5712 ret = statmount_mnt_idmap(s->idmap, seq, false);
5713 if (ret < 0)
5714 return ret;
5715
5716 s->sm.mnt_gidmap_num = ret;
5717 /*
5718 * Always raise STATMOUNT_MNT_GIDMAP even if there are no valid
5719 * mappings. This allows userspace to distinguish between a
5720 * non-idmapped mount and an idmapped mount where none of the
5721 * individual mappings are valid in the caller's idmapping.
5722 */
5723 if (is_valid_mnt_idmap(s->idmap))
5724 s->sm.mask |= STATMOUNT_MNT_GIDMAP;
5725 return 0;
5726 }
5727
5728 static int statmount_string(struct kstatmount *s, u64 flag)
5729 {
5730 int ret = 0;
5731 size_t kbufsize;
5732 struct seq_file *seq = &s->seq;
5733 struct statmount *sm = &s->sm;
5734 u32 start, *offp;
5735
5736 /* Reserve an empty string at the beginning for any unset offsets */
5737 if (!seq->count)
5738 seq_putc(seq, 0);
5739
5740 start = seq->count;
5741
5742 switch (flag) {
5743 case STATMOUNT_FS_TYPE:
5744 offp = &sm->fs_type;
5745 ret = statmount_fs_type(s, seq);
5746 break;
5747 case STATMOUNT_MNT_ROOT:
5748 offp = &sm->mnt_root;
5749 ret = statmount_mnt_root(s, seq);
5750 break;
5751 case STATMOUNT_MNT_POINT:
5752 offp = &sm->mnt_point;
5753 ret = statmount_mnt_point(s, seq);
5754 break;
5755 case STATMOUNT_MNT_OPTS:
5756 offp = &sm->mnt_opts;
5757 ret = statmount_mnt_opts(s, seq);
5758 break;
5759 case STATMOUNT_OPT_ARRAY:
5760 offp = &sm->opt_array;
5761 ret = statmount_opt_array(s, seq);
5762 break;
5763 case STATMOUNT_OPT_SEC_ARRAY:
5764 offp = &sm->opt_sec_array;
5765 ret = statmount_opt_sec_array(s, seq);
5766 break;
5767 case STATMOUNT_FS_SUBTYPE:
5768 offp = &sm->fs_subtype;
5769 statmount_fs_subtype(s, seq);
5770 break;
5771 case STATMOUNT_SB_SOURCE:
5772 offp = &sm->sb_source;
5773 ret = statmount_sb_source(s, seq);
5774 break;
5775 case STATMOUNT_MNT_UIDMAP:
5776 offp = &sm->mnt_uidmap;
5777 ret = statmount_mnt_uidmap(s, seq);
5778 break;
5779 case STATMOUNT_MNT_GIDMAP:
5780 offp = &sm->mnt_gidmap;
5781 ret = statmount_mnt_gidmap(s, seq);
5782 break;
5783 default:
5784 WARN_ON_ONCE(true);
5785 return -EINVAL;
5786 }
5787
5788 /*
5789 * If nothing was emitted, return to avoid setting the flag
5790 * and terminating the buffer.
5791 */
5792 if (seq->count == start)
5793 return ret;
5794 if (unlikely(check_add_overflow(sizeof(*sm), seq->count, &kbufsize)))
5795 return -EOVERFLOW;
5796 if (kbufsize >= s->bufsize)
5797 return -EOVERFLOW;
5798
5799 /* signal a retry */
5800 if (unlikely(seq_has_overflowed(seq)))
5801 return -EAGAIN;
5802
5803 if (ret)
5804 return ret;
5805
5806 seq->buf[seq->count++] = '\0';
5807 sm->mask |= flag;
5808 *offp = start;
5809 return 0;
5810 }
5811
5812 static int copy_statmount_to_user(struct kstatmount *s)
5813 {
5814 struct statmount *sm = &s->sm;
5815 struct seq_file *seq = &s->seq;
5816 char __user *str = ((char __user *)s->buf) + sizeof(*sm);
5817 size_t copysize = min_t(size_t, s->bufsize, sizeof(*sm));
5818
5819 if (seq->count && copy_to_user(str, seq->buf, seq->count))
5820 return -EFAULT;
5821
5822 /* Return the number of bytes copied to the buffer */
5823 sm->size = copysize + seq->count;
5824 if (copy_to_user(s->buf, sm, copysize))
5825 return -EFAULT;
5826
5827 return 0;
5828 }
5829
5830 static struct mount *listmnt_next(struct mount *curr, bool reverse)
5831 {
5832 struct rb_node *node;
5833
5834 if (reverse)
5835 node = rb_prev(&curr->mnt_node);
5836 else
5837 node = rb_next(&curr->mnt_node);
5838
5839 return node_to_mount(node);
5840 }
5841
5842 static int grab_requested_root(struct mnt_namespace *ns, struct path *root)
5843 {
5844 struct mount *first, *child;
5845
5846 rwsem_assert_held(&namespace_sem);
5847
5848 /* We're looking at our own ns, just use get_fs_root. */
5849 if (ns == current->nsproxy->mnt_ns) {
5850 get_fs_root(current->fs, root);
5851 return 0;
5852 }
5853
5854 /*
5855 * We have to find the first mount in our ns and use that, however it
5856 * may not exist, so handle that properly.
5857 */
5858 if (mnt_ns_empty(ns))
5859 return -ENOENT;
5860
5861 first = ns->root;
5862 for (child = node_to_mount(ns->mnt_first_node); child;
5863 child = listmnt_next(child, false)) {
5864 if (child != first && child->mnt_parent == first)
5865 break;
5866 }
5867 if (!child)
5868 return -ENOENT;
5869
5870 root->mnt = mntget(&child->mnt);
5871 root->dentry = dget(root->mnt->mnt_root);
5872 return 0;
5873 }
5874
5875 /* This must be updated whenever a new flag is added */
5876 #define STATMOUNT_SUPPORTED (STATMOUNT_SB_BASIC | \
5877 STATMOUNT_MNT_BASIC | \
5878 STATMOUNT_PROPAGATE_FROM | \
5879 STATMOUNT_MNT_ROOT | \
5880 STATMOUNT_MNT_POINT | \
5881 STATMOUNT_FS_TYPE | \
5882 STATMOUNT_MNT_NS_ID | \
5883 STATMOUNT_MNT_OPTS | \
5884 STATMOUNT_FS_SUBTYPE | \
5885 STATMOUNT_SB_SOURCE | \
5886 STATMOUNT_OPT_ARRAY | \
5887 STATMOUNT_OPT_SEC_ARRAY | \
5888 STATMOUNT_SUPPORTED_MASK | \
5889 STATMOUNT_MNT_UIDMAP | \
5890 STATMOUNT_MNT_GIDMAP)
5891
5892 /* locks: namespace_shared */
5893 static int do_statmount(struct kstatmount *s, u64 mnt_id, u64 mnt_ns_id,
5894 struct file *mnt_file, struct mnt_namespace *ns)
5895 {
5896 int err;
5897
5898 if (mnt_file) {
5899 WARN_ON_ONCE(ns != NULL);
5900
5901 s->mnt = mnt_file->f_path.mnt;
5902 ns = real_mount(s->mnt)->mnt_ns;
5903 if (IS_ERR(ns))
5904 return PTR_ERR(ns);
5905 if (!ns)
5906 /*
5907 * We can't set mount point and mnt_ns_id since we don't have a
5908 * ns for the mount. This can happen if the mount is unmounted
5909 * with MNT_DETACH.
5910 */
5911 s->mask &= ~(STATMOUNT_MNT_POINT | STATMOUNT_MNT_NS_ID);
5912 } else {
5913 /* Has the namespace already been emptied? */
5914 if (mnt_ns_id && mnt_ns_empty(ns))
5915 return -ENOENT;
5916
5917 s->mnt = lookup_mnt_in_ns(mnt_id, ns);
5918 if (!s->mnt)
5919 return -ENOENT;
5920 }
5921
5922 if (ns) {
5923 err = grab_requested_root(ns, &s->root);
5924 if (err)
5925 return err;
5926
5927 if (!mnt_file) {
5928 struct mount *m;
5929 /*
5930 * Don't trigger audit denials. We just want to determine what
5931 * mounts to show users.
5932 */
5933 m = real_mount(s->mnt);
5934 if (!is_path_reachable(m, m->mnt.mnt_root, &s->root) &&
5935 !ns_capable_noaudit(ns->user_ns, CAP_SYS_ADMIN))
5936 return -EPERM;
5937 }
5938 }
5939
5940 err = security_sb_statfs(s->mnt->mnt_root);
5941 if (err)
5942 return err;
5943
5944 /*
5945 * Note that mount properties in mnt->mnt_flags, mnt->mnt_idmap
5946 * can change concurrently as we only hold the read-side of the
5947 * namespace semaphore and mount properties may change with only
5948 * the mount lock held.
5949 *
5950 * We could sample the mount lock sequence counter to detect
5951 * those changes and retry. But it's not worth it. Worst that
5952 * happens is that the mnt->mnt_idmap pointer is already changed
5953 * while mnt->mnt_flags isn't or vica versa. So what.
5954 *
5955 * Both mnt->mnt_flags and mnt->mnt_idmap are set and retrieved
5956 * via READ_ONCE()/WRITE_ONCE() and guard against theoretical
5957 * torn read/write. That's all we care about right now.
5958 */
5959 s->idmap = mnt_idmap(s->mnt);
5960 if (s->mask & STATMOUNT_MNT_BASIC)
5961 statmount_mnt_basic(s);
5962
5963 if (s->mask & STATMOUNT_SB_BASIC)
5964 statmount_sb_basic(s);
5965
5966 if (s->mask & STATMOUNT_PROPAGATE_FROM)
5967 statmount_propagate_from(s);
5968
5969 if (s->mask & STATMOUNT_FS_TYPE)
5970 err = statmount_string(s, STATMOUNT_FS_TYPE);
5971
5972 if (!err && s->mask & STATMOUNT_MNT_ROOT)
5973 err = statmount_string(s, STATMOUNT_MNT_ROOT);
5974
5975 if (!err && s->mask & STATMOUNT_MNT_POINT)
5976 err = statmount_string(s, STATMOUNT_MNT_POINT);
5977
5978 if (!err && s->mask & STATMOUNT_MNT_OPTS)
5979 err = statmount_string(s, STATMOUNT_MNT_OPTS);
5980
5981 if (!err && s->mask & STATMOUNT_OPT_ARRAY)
5982 err = statmount_string(s, STATMOUNT_OPT_ARRAY);
5983
5984 if (!err && s->mask & STATMOUNT_OPT_SEC_ARRAY)
5985 err = statmount_string(s, STATMOUNT_OPT_SEC_ARRAY);
5986
5987 if (!err && s->mask & STATMOUNT_FS_SUBTYPE)
5988 err = statmount_string(s, STATMOUNT_FS_SUBTYPE);
5989
5990 if (!err && s->mask & STATMOUNT_SB_SOURCE)
5991 err = statmount_string(s, STATMOUNT_SB_SOURCE);
5992
5993 if (!err && s->mask & STATMOUNT_MNT_UIDMAP)
5994 err = statmount_string(s, STATMOUNT_MNT_UIDMAP);
5995
5996 if (!err && s->mask & STATMOUNT_MNT_GIDMAP)
5997 err = statmount_string(s, STATMOUNT_MNT_GIDMAP);
5998
5999 if (!err && s->mask & STATMOUNT_MNT_NS_ID)
6000 statmount_mnt_ns_id(s, ns);
6001
6002 if (!err && s->mask & STATMOUNT_SUPPORTED_MASK) {
6003 s->sm.mask |= STATMOUNT_SUPPORTED_MASK;
6004 s->sm.supported_mask = STATMOUNT_SUPPORTED;
6005 }
6006
6007 if (err)
6008 return err;
6009
6010 /* Are there bits in the return mask not present in STATMOUNT_SUPPORTED? */
6011 WARN_ON_ONCE(~STATMOUNT_SUPPORTED & s->sm.mask);
6012
6013 return 0;
6014 }
6015
6016 static inline bool retry_statmount(const long ret, size_t *seq_size)
6017 {
6018 if (likely(ret != -EAGAIN))
6019 return false;
6020 if (unlikely(check_mul_overflow(*seq_size, 2, seq_size)))
6021 return false;
6022 if (unlikely(*seq_size > MAX_RW_COUNT))
6023 return false;
6024 return true;
6025 }
6026
6027 #define STATMOUNT_STRING_REQ (STATMOUNT_MNT_ROOT | STATMOUNT_MNT_POINT | \
6028 STATMOUNT_FS_TYPE | STATMOUNT_MNT_OPTS | \
6029 STATMOUNT_FS_SUBTYPE | STATMOUNT_SB_SOURCE | \
6030 STATMOUNT_OPT_ARRAY | STATMOUNT_OPT_SEC_ARRAY | \
6031 STATMOUNT_MNT_UIDMAP | STATMOUNT_MNT_GIDMAP)
6032
6033 static int prepare_kstatmount(struct kstatmount *ks, struct mnt_id_req *kreq,
6034 struct statmount __user *buf, size_t bufsize,
6035 size_t seq_size)
6036 {
6037 if (!access_ok(buf, bufsize))
6038 return -EFAULT;
6039
6040 memset(ks, 0, sizeof(*ks));
6041 ks->mask = kreq->param;
6042 ks->buf = buf;
6043 ks->bufsize = bufsize;
6044
6045 if (ks->mask & STATMOUNT_STRING_REQ) {
6046 if (bufsize == sizeof(ks->sm))
6047 return -EOVERFLOW;
6048
6049 ks->seq.buf = kvmalloc(seq_size, GFP_KERNEL_ACCOUNT);
6050 if (!ks->seq.buf)
6051 return -ENOMEM;
6052
6053 ks->seq.size = seq_size;
6054 }
6055
6056 return 0;
6057 }
6058
6059 static int copy_mnt_id_req(const struct mnt_id_req __user *req,
6060 struct mnt_id_req *kreq, unsigned int flags)
6061 {
6062 int ret;
6063 size_t usize;
6064
6065 BUILD_BUG_ON(sizeof(struct mnt_id_req) != MNT_ID_REQ_SIZE_VER1);
6066
6067 ret = get_user(usize, &req->size);
6068 if (ret)
6069 return -EFAULT;
6070 if (unlikely(usize > PAGE_SIZE))
6071 return -E2BIG;
6072 if (unlikely(usize < MNT_ID_REQ_SIZE_VER0))
6073 return -EINVAL;
6074 memset(kreq, 0, sizeof(*kreq));
6075 ret = copy_struct_from_user(kreq, sizeof(*kreq), req, usize);
6076 if (ret)
6077 return ret;
6078
6079 if (flags & STATMOUNT_BY_FD) {
6080 if (kreq->mnt_id || kreq->mnt_ns_id)
6081 return -EINVAL;
6082 } else {
6083 if (kreq->mnt_ns_fd != 0 && kreq->mnt_ns_id)
6084 return -EINVAL;
6085 /* The first valid unique mount id is MNT_UNIQUE_ID_OFFSET + 1. */
6086 if (kreq->mnt_id <= MNT_UNIQUE_ID_OFFSET)
6087 return -EINVAL;
6088 }
6089 return 0;
6090 }
6091
6092 /*
6093 * If the user requested a specific mount namespace id, look that up and return
6094 * that, or if not simply grab a passive reference on our mount namespace and
6095 * return that.
6096 */
6097 static struct mnt_namespace *grab_requested_mnt_ns(const struct mnt_id_req *kreq)
6098 {
6099 struct mnt_namespace *mnt_ns;
6100
6101 if (kreq->mnt_ns_id) {
6102 mnt_ns = lookup_mnt_ns(kreq->mnt_ns_id);
6103 if (!mnt_ns)
6104 return ERR_PTR(-ENOENT);
6105 } else if (kreq->mnt_ns_fd) {
6106 struct ns_common *ns;
6107
6108 CLASS(fd, f)(kreq->mnt_ns_fd);
6109 if (fd_empty(f))
6110 return ERR_PTR(-EBADF);
6111
6112 if (!proc_ns_file(fd_file(f)))
6113 return ERR_PTR(-EINVAL);
6114
6115 ns = get_proc_ns(file_inode(fd_file(f)));
6116 if (ns->ns_type != CLONE_NEWNS)
6117 return ERR_PTR(-EINVAL);
6118
6119 mnt_ns = to_mnt_ns(ns);
6120 refcount_inc(&mnt_ns->passive);
6121 } else {
6122 mnt_ns = current->nsproxy->mnt_ns;
6123 refcount_inc(&mnt_ns->passive);
6124 }
6125
6126 return mnt_ns;
6127 }
6128
6129 SYSCALL_DEFINE4(statmount, const struct mnt_id_req __user *, req,
6130 struct statmount __user *, buf, size_t, bufsize,
6131 unsigned int, flags)
6132 {
6133 struct mnt_namespace *ns __free(mnt_ns_release) = NULL;
6134 struct kstatmount *ks __free(kfree) = NULL;
6135 struct file *mnt_file __free(fput) = NULL;
6136 struct mnt_id_req kreq;
6137 /* We currently support retrieval of 3 strings. */
6138 size_t seq_size = 3 * PATH_MAX;
6139 int ret;
6140
6141 if (flags & ~STATMOUNT_BY_FD)
6142 return -EINVAL;
6143
6144 ret = copy_mnt_id_req(req, &kreq, flags);
6145 if (ret)
6146 return ret;
6147
6148 if (flags & STATMOUNT_BY_FD) {
6149 mnt_file = fget_raw(kreq.mnt_fd);
6150 if (!mnt_file)
6151 return -EBADF;
6152 /* do_statmount sets ns in case of STATMOUNT_BY_FD */
6153 } else {
6154 ns = grab_requested_mnt_ns(&kreq);
6155 if (IS_ERR(ns))
6156 return PTR_ERR(ns);
6157
6158 if (kreq.mnt_ns_id && (ns != current->nsproxy->mnt_ns) &&
6159 !ns_capable_noaudit(ns->user_ns, CAP_SYS_ADMIN))
6160 return -EPERM;
6161 }
6162
6163 ks = kmalloc_obj(*ks, GFP_KERNEL_ACCOUNT);
6164 if (!ks)
6165 return -ENOMEM;
6166
6167 retry:
6168 ret = prepare_kstatmount(ks, &kreq, buf, bufsize, seq_size);
6169 if (ret)
6170 return ret;
6171
6172 scoped_guard(namespace_shared)
6173 ret = do_statmount(ks, kreq.mnt_id, kreq.mnt_ns_id, mnt_file, ns);
6174
6175 if (!ret)
6176 ret = copy_statmount_to_user(ks);
6177 kvfree(ks->seq.buf);
6178 path_put(&ks->root);
6179 if (retry_statmount(ret, &seq_size))
6180 goto retry;
6181 return ret;
6182 }
6183
6184 struct klistmount {
6185 u64 last_mnt_id;
6186 u64 mnt_parent_id;
6187 u64 *kmnt_ids;
6188 u32 nr_mnt_ids;
6189 struct mnt_namespace *ns;
6190 struct path root;
6191 };
6192
6193 /* locks: namespace_shared */
6194 static ssize_t do_listmount(struct klistmount *kls, bool reverse)
6195 {
6196 struct mnt_namespace *ns = kls->ns;
6197 u64 mnt_parent_id = kls->mnt_parent_id;
6198 u64 last_mnt_id = kls->last_mnt_id;
6199 u64 *mnt_ids = kls->kmnt_ids;
6200 size_t nr_mnt_ids = kls->nr_mnt_ids;
6201 struct path orig;
6202 struct mount *r, *first;
6203 ssize_t ret;
6204
6205 rwsem_assert_held(&namespace_sem);
6206
6207 ret = grab_requested_root(ns, &kls->root);
6208 if (ret)
6209 return ret;
6210
6211 if (mnt_parent_id == LSMT_ROOT) {
6212 orig = kls->root;
6213 } else {
6214 orig.mnt = lookup_mnt_in_ns(mnt_parent_id, ns);
6215 if (!orig.mnt)
6216 return -ENOENT;
6217 orig.dentry = orig.mnt->mnt_root;
6218 }
6219
6220 /*
6221 * Don't trigger audit denials. We just want to determine what
6222 * mounts to show users.
6223 */
6224 if (!is_path_reachable(real_mount(orig.mnt), orig.dentry, &kls->root) &&
6225 !ns_capable_noaudit(ns->user_ns, CAP_SYS_ADMIN))
6226 return -EPERM;
6227
6228 ret = security_sb_statfs(orig.dentry);
6229 if (ret)
6230 return ret;
6231
6232 if (!last_mnt_id) {
6233 if (reverse)
6234 first = node_to_mount(ns->mnt_last_node);
6235 else
6236 first = node_to_mount(ns->mnt_first_node);
6237 } else {
6238 if (reverse)
6239 first = mnt_find_id_at_reverse(ns, last_mnt_id - 1);
6240 else
6241 first = mnt_find_id_at(ns, last_mnt_id + 1);
6242 }
6243
6244 for (ret = 0, r = first; r && nr_mnt_ids; r = listmnt_next(r, reverse)) {
6245 if (r->mnt_id_unique == mnt_parent_id)
6246 continue;
6247 if (!is_path_reachable(r, r->mnt.mnt_root, &orig))
6248 continue;
6249 *mnt_ids = r->mnt_id_unique;
6250 mnt_ids++;
6251 nr_mnt_ids--;
6252 ret++;
6253 }
6254 return ret;
6255 }
6256
6257 static void __free_klistmount_free(const struct klistmount *kls)
6258 {
6259 path_put(&kls->root);
6260 kvfree(kls->kmnt_ids);
6261 mnt_ns_release(kls->ns);
6262 }
6263
6264 static inline int prepare_klistmount(struct klistmount *kls, struct mnt_id_req *kreq,
6265 size_t nr_mnt_ids)
6266 {
6267 u64 last_mnt_id = kreq->param;
6268 struct mnt_namespace *ns;
6269
6270 /* The first valid unique mount id is MNT_UNIQUE_ID_OFFSET + 1. */
6271 if (last_mnt_id != 0 && last_mnt_id <= MNT_UNIQUE_ID_OFFSET)
6272 return -EINVAL;
6273
6274 kls->last_mnt_id = last_mnt_id;
6275
6276 kls->nr_mnt_ids = nr_mnt_ids;
6277 kls->kmnt_ids = kvmalloc_array(nr_mnt_ids, sizeof(*kls->kmnt_ids),
6278 GFP_KERNEL_ACCOUNT);
6279 if (!kls->kmnt_ids)
6280 return -ENOMEM;
6281
6282 ns = grab_requested_mnt_ns(kreq);
6283 if (IS_ERR(ns))
6284 return PTR_ERR(ns);
6285 kls->ns = ns;
6286
6287 kls->mnt_parent_id = kreq->mnt_id;
6288 return 0;
6289 }
6290
6291 SYSCALL_DEFINE4(listmount, const struct mnt_id_req __user *, req,
6292 u64 __user *, mnt_ids, size_t, nr_mnt_ids, unsigned int, flags)
6293 {
6294 struct klistmount kls __free(klistmount_free) = {};
6295 const size_t maxcount = 1000000;
6296 struct mnt_id_req kreq;
6297 ssize_t ret;
6298
6299 if (flags & ~LISTMOUNT_REVERSE)
6300 return -EINVAL;
6301
6302 /*
6303 * If the mount namespace really has more than 1 million mounts the
6304 * caller must iterate over the mount namespace (and reconsider their
6305 * system design...).
6306 */
6307 if (unlikely(nr_mnt_ids > maxcount))
6308 return -EOVERFLOW;
6309
6310 if (!access_ok(mnt_ids, nr_mnt_ids * sizeof(*mnt_ids)))
6311 return -EFAULT;
6312
6313 ret = copy_mnt_id_req(req, &kreq, 0);
6314 if (ret)
6315 return ret;
6316
6317 ret = prepare_klistmount(&kls, &kreq, nr_mnt_ids);
6318 if (ret)
6319 return ret;
6320
6321 if (kreq.mnt_ns_id && (kls.ns != current->nsproxy->mnt_ns) &&
6322 !ns_capable_noaudit(kls.ns->user_ns, CAP_SYS_ADMIN))
6323 return -ENOENT;
6324
6325 /*
6326 * We only need to guard against mount topology changes as
6327 * listmount() doesn't care about any mount properties.
6328 */
6329 scoped_guard(namespace_shared)
6330 ret = do_listmount(&kls, (flags & LISTMOUNT_REVERSE));
6331 if (ret <= 0)
6332 return ret;
6333
6334 if (copy_to_user(mnt_ids, kls.kmnt_ids, ret * sizeof(*mnt_ids)))
6335 return -EFAULT;
6336
6337 return ret;
6338 }
6339
6340 struct mnt_namespace init_mnt_ns = {
6341 .ns = NS_COMMON_INIT(init_mnt_ns),
6342 .user_ns = &init_user_ns,
6343 .passive = REFCOUNT_INIT(1),
6344 .mounts = RB_ROOT,
6345 .poll = __WAIT_QUEUE_HEAD_INITIALIZER(init_mnt_ns.poll),
6346 };
6347
6348 static void __init mount_rootfs_on_nullfs(struct vfsmount *mnt,
6349 struct vfsmount *nullfs_mnt)
6350 {
6351 struct path root = {
6352 .mnt = nullfs_mnt,
6353 .dentry = nullfs_mnt->mnt_root,
6354 };
6355
6356 LOCK_MOUNT_EXACT(mp, &root);
6357 if (unlikely(IS_ERR(mp.parent)))
6358 panic("VFS: Failed to mount rootfs on nullfs");
6359 scoped_guard(mount_writer)
6360 attach_mnt(real_mount(mnt), mp.parent, mp.mp);
6361 }
6362
6363 static struct vfsmount *__init knullfs_file_mount(void)
6364 {
6365 struct dentry *file;
6366 struct mount *mnt;
6367
6368 file = nullfs_new_file(knullfs->mnt_sb);
6369 if (IS_ERR(file))
6370 return ERR_CAST(file);
6371 mnt = clone_mnt(real_mount(knullfs), file, CL_PRIVATE);
6372 dput(file);
6373 if (IS_ERR(mnt))
6374 return ERR_CAST(mnt);
6375 mnt->mnt_ns = MNT_NS_INTERNAL;
6376 mnt->mnt.mnt_flags |= MNT_INTERNAL;
6377 mnt->mnt.mnt_flags &= ~MNT_READONLY;
6378 dont_mount(mnt->mnt.mnt_root);
6379 return &mnt->mnt;
6380 }
6381
6382 static void __init init_mount_tree(void)
6383 {
6384 struct vfsmount *mnt, *nullfs_mnt;
6385 struct mount *mnt_root;
6386 struct path root;
6387
6388 /*
6389 * We create three mounts:
6390 *
6391 * (1) nullfs with mount id 1
6392 * (2) mutable rootfs with mount id 2
6393 * (3) private nullfs for kthreads (SB_KERNMOUNT), kept in knullfs
6394 * (4) a second mount of (3) rooted on a regular file, kept in
6395 * knullfs_file
6396 *
6397 * with (2) mounted on top of (1). The init_task's root and pwd
6398 * are pointed at (3) so all kthreads start isolated in nullfs.
6399 * A lookup at the cover an unmounted mount left behind finds (3)
6400 * or (4), see __lookup_mnt().
6401 */
6402 nullfs_mnt = vfs_kern_mount(&nullfs_fs_type, 0, "nullfs", NULL);
6403 if (IS_ERR(nullfs_mnt))
6404 panic("VFS: Failed to create nullfs");
6405
6406 mnt = vfs_kern_mount(&rootfs_fs_type, 0, "rootfs", initramfs_options);
6407 if (IS_ERR(mnt))
6408 panic("Can't create rootfs");
6409
6410 VFS_WARN_ON_ONCE(real_mount(nullfs_mnt)->mnt_id != 1);
6411 VFS_WARN_ON_ONCE(real_mount(mnt)->mnt_id != 2);
6412
6413 /* The namespace root is the nullfs mnt. */
6414 mnt_root = real_mount(nullfs_mnt);
6415 init_mnt_ns.root = mnt_root;
6416
6417 mount_rootfs_on_nullfs(mnt, nullfs_mnt);
6418
6419 pr_info("VFS: Finished mounting rootfs on nullfs\n");
6420
6421 /*
6422 * We've dropped all locks here but that's fine. Not just are we
6423 * the only task that's running, there's no other mount
6424 * namespace in existence and the initial mount namespace is
6425 * completely empty until we add the mounts we just created.
6426 */
6427 for (struct mount *p = mnt_root; p; p = next_mnt(p, mnt_root)) {
6428 mnt_add_to_ns(&init_mnt_ns, p);
6429 init_mnt_ns.nr_mounts++;
6430 }
6431
6432 knullfs = kern_mount(&nullfs_fs_type);
6433 if (IS_ERR(knullfs))
6434 panic("VFS: Failed to create private nullfs instance");
6435 /* nothing is ever mounted on the root of a kernel thread */
6436 dont_mount(knullfs->mnt_root);
6437 /* and nothing is ever written through it */
6438 knullfs->mnt_flags |= MNT_READONLY;
6439 knullfs_file = knullfs_file_mount();
6440 if (IS_ERR(knullfs_file))
6441 panic("VFS: Failed to create the nullfs file stand-in");
6442 root.mnt = knullfs;
6443 root.dentry = knullfs->mnt_root;
6444
6445 init_task.nsproxy->mnt_ns = &init_mnt_ns;
6446 get_mnt_ns(&init_mnt_ns);
6447 set_fs_pwd(current->fs, &root);
6448 set_fs_root(current->fs, &root);
6449
6450 ns_tree_add(&init_mnt_ns);
6451 }
6452
6453 void __init mnt_init(void)
6454 {
6455 int err;
6456
6457 mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
6458 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, NULL);
6459
6460 mount_hashtable = alloc_large_system_hash("Mount-cache",
6461 sizeof(struct hlist_head),
6462 mhash_entries, 19,
6463 HASH_ZERO,
6464 &m_hash_shift, &m_hash_mask, 0, 0);
6465 mountpoint_hashtable = alloc_large_system_hash("Mountpoint-cache",
6466 sizeof(struct hlist_head),
6467 mphash_entries, 19,
6468 HASH_ZERO,
6469 &mp_hash_shift, &mp_hash_mask, 0, 0);
6470
6471 super_dev_init();
6472
6473 kernfs_init();
6474
6475 err = sysfs_init();
6476 if (err)
6477 printk(KERN_WARNING "%s: sysfs_init error: %d\n",
6478 __func__, err);
6479 fs_kobj = kobject_create_and_add("fs", NULL);
6480 if (!fs_kobj)
6481 printk(KERN_WARNING "%s: kobj create error\n", __func__);
6482 shmem_init();
6483 init_rootfs();
6484 init_mount_tree();
6485 failfs_init();
6486 }
6487
6488 void put_mnt_ns(struct mnt_namespace *ns)
6489 {
6490 if (!ns_ref_put(ns))
6491 return;
6492 guard(namespace_excl)();
6493 emptied_ns = ns;
6494 guard(mount_writer)();
6495 umount_tree(ns->root, 0);
6496 }
6497
6498 struct vfsmount *kern_mount(struct file_system_type *type)
6499 {
6500 struct vfsmount *mnt;
6501 mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, NULL);
6502 if (!IS_ERR(mnt)) {
6503 /*
6504 * it is a longterm mount, don't release mnt until
6505 * we unmount before file sys is unregistered
6506 */
6507 real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL;
6508 }
6509 return mnt;
6510 }
6511 EXPORT_SYMBOL_GPL(kern_mount);
6512
6513 void kern_unmount(struct vfsmount *mnt)
6514 {
6515 /* release long term mount so mount point can be released */
6516 if (!IS_ERR(mnt)) {
6517 mnt_make_shortterm(mnt);
6518 synchronize_rcu(); /* yecchhh... */
6519 mntput(mnt);
6520 }
6521 }
6522 EXPORT_SYMBOL(kern_unmount);
6523
6524 void kern_unmount_array(struct vfsmount *mnt[], unsigned int num)
6525 {
6526 unsigned int i;
6527
6528 for (i = 0; i < num; i++)
6529 mnt_make_shortterm(mnt[i]);
6530 synchronize_rcu_expedited();
6531 for (i = 0; i < num; i++)
6532 mntput(mnt[i]);
6533 }
6534 EXPORT_SYMBOL(kern_unmount_array);
6535
6536 bool our_mnt(struct vfsmount *mnt)
6537 {
6538 return check_mnt(real_mount(mnt));
6539 }
6540
6541 bool current_chrooted(void)
6542 {
6543 /* Does the current process have a non-standard root */
6544 struct path fs_root __free(path_put) = {};
6545 struct mount *root;
6546
6547 get_fs_root(current->fs, &fs_root);
6548
6549 /* Find the namespace root */
6550
6551 guard(mount_locked_reader)();
6552
6553 root = topmost_overmount(current->nsproxy->mnt_ns->root);
6554
6555 return fs_root.mnt != &root->mnt || !path_mounted(&fs_root);
6556 }
6557
6558 static bool mnt_already_visible(struct mnt_namespace *ns,
6559 const struct super_block *sb,
6560 int *new_mnt_flags)
6561 {
6562 int new_flags = *new_mnt_flags;
6563 struct mount *mnt;
6564
6565 /* Don't acquire namespace semaphore without a good reason. */
6566 if (hlist_empty(&ns->mnt_visible_mounts))
6567 return false;
6568
6569 guard(namespace_shared)();
6570 hlist_for_each_entry(mnt, &ns->mnt_visible_mounts, mnt_ns_visible) {
6571 const struct super_block *sb_visible = mnt->mnt.mnt_sb;
6572 struct mount *child;
6573 int mnt_flags;
6574
6575 if (sb_visible->s_type != sb->s_type)
6576 continue;
6577
6578 /*
6579 * Restricted variants are not compatible with anything, even
6580 * other restricted variants.
6581 */
6582 if (sb_visible->s_iflags & SB_I_RESTRICTED_VARIANT)
6583 continue;
6584
6585 /* A local view of the mount flags */
6586 mnt_flags = mnt->mnt.mnt_flags;
6587
6588 /* Don't miss readonly hidden in the superblock flags */
6589 if (sb_rdonly(mnt->mnt.mnt_sb))
6590 mnt_flags |= MNT_LOCK_READONLY;
6591
6592 /* Verify the mount flags are equal to or more permissive
6593 * than the proposed new mount.
6594 */
6595 if ((mnt_flags & MNT_LOCK_READONLY) &&
6596 !(new_flags & MNT_READONLY))
6597 continue;
6598 if ((mnt_flags & MNT_LOCK_ATIME) &&
6599 ((mnt_flags & MNT_ATIME_MASK) != (new_flags & MNT_ATIME_MASK)))
6600 continue;
6601
6602 /* This mount is not fully visible if there are any
6603 * locked child mounts that cover anything except for
6604 * empty directories.
6605 */
6606 list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
6607 struct inode *inode = child->mnt_mountpoint->d_inode;
6608 /* Only worry about locked mounts */
6609 if (!(child->mnt.mnt_flags & MNT_LOCKED))
6610 continue;
6611 /* Is the directory permanently empty? */
6612 if (!is_empty_dir_inode(inode))
6613 goto next;
6614 }
6615 /* Preserve the locked attributes */
6616 *new_mnt_flags |= mnt_flags & (MNT_LOCK_READONLY | \
6617 MNT_LOCK_ATIME);
6618 return true;
6619 next: ;
6620 }
6621 return false;
6622 }
6623
6624 static bool mount_too_revealing(const struct super_block *sb, int *new_mnt_flags)
6625 {
6626 const unsigned long required_iflags = SB_I_NOEXEC | SB_I_NODEV;
6627 struct mnt_namespace *ns = current->nsproxy->mnt_ns;
6628 unsigned long s_iflags;
6629
6630 if (ns->user_ns == &init_user_ns)
6631 return false;
6632
6633 /* Can this filesystem be too revealing? */
6634 if (!(sb->s_type->fs_flags & FS_USERNS_MOUNT_RESTRICTED))
6635 return false;
6636
6637 s_iflags = sb->s_iflags;
6638 if ((s_iflags & required_iflags) != required_iflags) {
6639 WARN_ONCE(1, "Expected s_iflags to contain 0x%lx\n",
6640 required_iflags);
6641 return true;
6642 }
6643
6644 /*
6645 * Restricted variants don't need an already visible mount because they
6646 * don't expose the full filesystem view.
6647 */
6648 if (s_iflags & SB_I_RESTRICTED_VARIANT)
6649 return false;
6650
6651 return !mnt_already_visible(ns, sb, new_mnt_flags);
6652 }
6653
6654 bool mnt_may_suid(struct vfsmount *mnt)
6655 {
6656 /*
6657 * Foreign mounts (accessed via fchdir or through /proc
6658 * symlinks) are always treated as if they are nosuid. This
6659 * prevents namespaces from trusting potentially unsafe
6660 * suid/sgid bits, file caps, or security labels that originate
6661 * in other namespaces.
6662 */
6663 return !(mnt->mnt_flags & MNT_NOSUID) && check_mnt(real_mount(mnt)) &&
6664 current_in_userns(mnt->mnt_sb->s_user_ns);
6665 }
6666
6667 static struct ns_common *mntns_get(struct task_struct *task)
6668 {
6669 struct ns_common *ns = NULL;
6670 struct nsproxy *nsproxy;
6671
6672 task_lock(task);
6673 nsproxy = task->nsproxy;
6674 if (nsproxy) {
6675 ns = &nsproxy->mnt_ns->ns;
6676 get_mnt_ns(to_mnt_ns(ns));
6677 }
6678 task_unlock(task);
6679
6680 return ns;
6681 }
6682
6683 static void mntns_put(struct ns_common *ns)
6684 {
6685 put_mnt_ns(to_mnt_ns(ns));
6686 }
6687
6688 static int mntns_install(struct nsset *nsset, struct ns_common *ns)
6689 {
6690 struct nsproxy *nsproxy = nsset->nsproxy;
6691 struct fs_struct *fs = nsset->fs;
6692 struct mnt_namespace *mnt_ns = to_mnt_ns(ns), *old_mnt_ns;
6693 struct user_namespace *user_ns = nsset->cred->user_ns;
6694 struct path root;
6695 int err;
6696
6697 if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) ||
6698 !ns_capable(user_ns, CAP_SYS_CHROOT) ||
6699 !ns_capable(user_ns, CAP_SYS_ADMIN))
6700 return -EPERM;
6701
6702 if (is_anon_ns(mnt_ns))
6703 return -EINVAL;
6704
6705 if (fs->users != 1)
6706 return -EINVAL;
6707
6708 get_mnt_ns(mnt_ns);
6709 old_mnt_ns = nsproxy->mnt_ns;
6710 nsproxy->mnt_ns = mnt_ns;
6711
6712 /* Find the root */
6713 err = vfs_path_lookup(mnt_ns->root->mnt.mnt_root, &mnt_ns->root->mnt,
6714 "/", LOOKUP_DOWN, &root);
6715 if (err) {
6716 /* revert to old namespace */
6717 nsproxy->mnt_ns = old_mnt_ns;
6718 put_mnt_ns(mnt_ns);
6719 return err;
6720 }
6721
6722 put_mnt_ns(old_mnt_ns);
6723
6724 /* Update the pwd and root */
6725 set_fs_pwd(fs, &root);
6726 set_fs_root(fs, &root);
6727
6728 path_put(&root);
6729 return 0;
6730 }
6731
6732 static struct user_namespace *mntns_owner(struct ns_common *ns)
6733 {
6734 return to_mnt_ns(ns)->user_ns;
6735 }
6736
6737 const struct proc_ns_operations mntns_operations = {
6738 .name = "mnt",
6739 .get = mntns_get,
6740 .put = mntns_put,
6741 .install = mntns_install,
6742 .owner = mntns_owner,
6743 };
6744
6745 #ifdef CONFIG_SYSCTL
6746 static const struct ctl_table fs_namespace_sysctls[] = {
6747 {
6748 .procname = "mount-max",
6749 .data = &sysctl_mount_max,
6750 .maxlen = sizeof(unsigned int),
6751 .mode = 0644,
6752 .proc_handler = proc_dointvec_minmax,
6753 .extra1 = SYSCTL_ONE,
6754 },
6755 };
6756
6757 static int __init init_fs_namespace_sysctls(void)
6758 {
6759 register_sysctl_init("fs", fs_namespace_sysctls);
6760 return 0;
6761 }
6762 fs_initcall(init_fs_namespace_sysctls);
6763
6764 #endif /* CONFIG_SYSCTL */