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