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