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