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