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