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