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