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