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 const 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), 3172 (flags & OPEN_TREE_DROP_MNTNS_MOUNTS) ? 0 : CL_COPY_MNT_NS_FILE); 3173 if (IS_ERR(mnt)) { 3174 emptied_ns = ns; 3175 return ERR_CAST(mnt); 3176 } 3177 3178 for (p = mnt; p; p = next_mnt(p, mnt)) { 3179 mnt_add_to_ns(ns, p); 3180 ns->nr_mounts++; 3181 } 3182 ns->root = mnt; 3183 return ns; 3184 } 3185 3186 static struct file *open_detached_copy(struct path *path, unsigned int flags) 3187 { 3188 struct mnt_namespace *ns = get_detached_copy(path, flags); 3189 struct file *file; 3190 3191 if (IS_ERR(ns)) 3192 return ERR_CAST(ns); 3193 3194 mntput(path->mnt); 3195 path->mnt = mntget(&ns->root->mnt); 3196 file = dentry_open(path, O_PATH, current_cred()); 3197 if (IS_ERR(file)) 3198 dissolve_on_fput(no_free_ptr(path->mnt)); 3199 else 3200 file->f_mode |= FMODE_NEED_UNMOUNT; 3201 return file; 3202 } 3203 3204 enum mount_copy_flags_t { 3205 MOUNT_COPY_RECURSIVE = (1 << 0), 3206 MOUNT_COPY_NEW = (1 << 1), 3207 }; 3208 3209 static struct mnt_namespace *create_new_namespace(struct path *path, 3210 enum mount_copy_flags_t flags) 3211 { 3212 struct mnt_namespace *ns = current->nsproxy->mnt_ns; 3213 struct user_namespace *user_ns = current_user_ns(); 3214 struct mnt_namespace *new_ns; 3215 struct mount *new_ns_root, *old_ns_root; 3216 struct path to_path; 3217 struct mount *mnt; 3218 unsigned int copy_flags = 0; 3219 bool locked = false, recurse = flags & MOUNT_COPY_RECURSIVE; 3220 bool foreign = user_ns != ns->user_ns; 3221 3222 if (unlikely(!d_can_lookup(path->dentry))) 3223 return ERR_PTR(-ENOTDIR); 3224 3225 /* 3226 * Without privileges over the mount namespace the copy is made from 3227 * nothing mounted below @path may be left out. It would reveal what 3228 * it covers. That's what unshare() gives such a caller as well. 3229 */ 3230 if (foreign) 3231 copy_flags |= CL_SLAVE | CL_COPY_UNBINDABLE; 3232 3233 new_ns = alloc_mnt_ns(user_ns, false); 3234 if (IS_ERR(new_ns)) 3235 return ERR_CAST(new_ns); 3236 3237 old_ns_root = ns->root; 3238 to_path.mnt = &old_ns_root->mnt; 3239 to_path.dentry = old_ns_root->mnt.mnt_root; 3240 3241 VFS_WARN_ON_ONCE(old_ns_root->mnt.mnt_sb->s_type != &nullfs_fs_type); 3242 3243 LOCK_MOUNT_EXACT_COPY(mp, &to_path, copy_flags); 3244 if (IS_ERR(mp.parent)) { 3245 free_mnt_ns(new_ns); 3246 return ERR_CAST(mp.parent); 3247 } 3248 new_ns_root = mp.parent; 3249 3250 /* 3251 * If the real rootfs had a locked mount on top of it somewhere 3252 * in the stack, lock the new mount tree as well so it can't be 3253 * exposed. 3254 */ 3255 mnt = old_ns_root; 3256 while (mnt->overmount) { 3257 mnt = mnt->overmount; 3258 if (mnt->mnt.mnt_flags & MNT_LOCKED) 3259 locked = true; 3260 } 3261 3262 /* 3263 * We don't emulate unshare()ing a mount namespace. We stick to 3264 * the restrictions of creating detached bind-mounts. It has a 3265 * lot saner and simpler semantics. A caller without privileges 3266 * over the mount namespace can't leave out any child though. 3267 */ 3268 if (flags & MOUNT_COPY_NEW) 3269 mnt = clone_mnt(real_mount(path->mnt), path->dentry, copy_flags); 3270 else if (foreign && !recurse && 3271 __has_children(real_mount(path->mnt), path->dentry)) 3272 mnt = ERR_PTR(-EINVAL); 3273 else 3274 mnt = __do_loopback(path, recurse, copy_flags); 3275 scoped_guard(mount_writer) { 3276 if (IS_ERR(mnt)) { 3277 emptied_ns = new_ns; 3278 umount_tree(new_ns_root, 0); 3279 return ERR_CAST(mnt); 3280 } 3281 3282 if (locked) 3283 mnt->mnt.mnt_flags |= MNT_LOCKED; 3284 /* 3285 * now mount the detached tree on top of the copy 3286 * of the real rootfs we created. 3287 */ 3288 attach_mnt(mnt, new_ns_root, mp.mp); 3289 if (foreign) 3290 lock_mnt_tree(new_ns_root); 3291 } 3292 3293 for (mnt = new_ns_root; mnt; mnt = next_mnt(mnt, new_ns_root)) { 3294 mnt_add_to_ns(new_ns, mnt); 3295 new_ns->nr_mounts++; 3296 } 3297 3298 new_ns->root = new_ns_root; 3299 ns_tree_add_raw(new_ns); 3300 return new_ns; 3301 } 3302 3303 static struct file *open_new_namespace(struct path *path, 3304 enum mount_copy_flags_t flags) 3305 { 3306 struct mnt_namespace *new_ns; 3307 3308 new_ns = create_new_namespace(path, flags); 3309 if (IS_ERR(new_ns)) 3310 return ERR_CAST(new_ns); 3311 return open_namespace_file(to_ns_common(new_ns)); 3312 } 3313 3314 static struct file *vfs_open_tree(int dfd, const char __user *filename, unsigned int flags) 3315 { 3316 int ret; 3317 struct path path __free(path_put) = {}; 3318 int lookup_flags = LOOKUP_AUTOMOUNT | LOOKUP_FOLLOW; 3319 3320 BUILD_BUG_ON(OPEN_TREE_CLOEXEC != O_CLOEXEC); 3321 3322 if (flags & ~(AT_EMPTY_PATH | AT_NO_AUTOMOUNT | AT_RECURSIVE | 3323 AT_SYMLINK_NOFOLLOW | OPEN_TREE_CLONE | 3324 OPEN_TREE_CLOEXEC | OPEN_TREE_NAMESPACE | 3325 OPEN_TREE_DROP_MNTNS_MOUNTS)) 3326 return ERR_PTR(-EINVAL); 3327 3328 /* 3329 * Only meaningful when a tree is copied recursively: a non-recursive 3330 * clone has no submounts to drop. OPEN_TREE_NAMESPACE never copies 3331 * pinned mount namespaces, so there the flag is a no-op. 3332 */ 3333 if ((flags & OPEN_TREE_DROP_MNTNS_MOUNTS) && 3334 (!(flags & AT_RECURSIVE) || 3335 !(flags & (OPEN_TREE_CLONE | OPEN_TREE_NAMESPACE)))) 3336 return ERR_PTR(-EINVAL); 3337 3338 if ((flags & (AT_RECURSIVE | OPEN_TREE_CLONE | OPEN_TREE_NAMESPACE)) == 3339 AT_RECURSIVE) 3340 return ERR_PTR(-EINVAL); 3341 3342 if (hweight32(flags & (OPEN_TREE_CLONE | OPEN_TREE_NAMESPACE)) > 1) 3343 return ERR_PTR(-EINVAL); 3344 3345 if (flags & AT_NO_AUTOMOUNT) 3346 lookup_flags &= ~LOOKUP_AUTOMOUNT; 3347 if (flags & AT_SYMLINK_NOFOLLOW) 3348 lookup_flags &= ~LOOKUP_FOLLOW; 3349 3350 /* 3351 * If we create a new mount namespace with the cloned mount tree we 3352 * just care about being privileged over our current user namespace. 3353 * The new mount namespace will be owned by it. 3354 */ 3355 if ((flags & OPEN_TREE_NAMESPACE) && 3356 !ns_capable(current_user_ns(), CAP_SYS_ADMIN)) 3357 return ERR_PTR(-EPERM); 3358 3359 if ((flags & OPEN_TREE_CLONE) && !may_mount()) 3360 return ERR_PTR(-EPERM); 3361 3362 CLASS(filename_uflags, name)(filename, flags); 3363 ret = filename_lookup(dfd, name, lookup_flags, &path, NULL); 3364 if (unlikely(ret)) 3365 return ERR_PTR(ret); 3366 3367 if (flags & OPEN_TREE_NAMESPACE) 3368 return open_new_namespace(&path, (flags & AT_RECURSIVE) ? MOUNT_COPY_RECURSIVE : 0); 3369 3370 if (flags & OPEN_TREE_CLONE) 3371 return open_detached_copy(&path, flags); 3372 3373 return dentry_open(&path, O_PATH, current_cred()); 3374 } 3375 3376 SYSCALL_DEFINE3(open_tree, int, dfd, const char __user *, filename, unsigned, flags) 3377 { 3378 return FD_ADD(flags, vfs_open_tree(dfd, filename, flags)); 3379 } 3380 3381 /* 3382 * Don't allow locked mount flags to be cleared. 3383 * 3384 * No locks need to be held here while testing the various MNT_LOCK 3385 * flags because those flags can never be cleared once they are set. 3386 */ 3387 static bool can_change_locked_flags(struct mount *mnt, unsigned int mnt_flags) 3388 { 3389 unsigned int fl = mnt->mnt.mnt_flags; 3390 3391 if ((fl & MNT_LOCK_READONLY) && 3392 !(mnt_flags & MNT_READONLY)) 3393 return false; 3394 3395 if ((fl & MNT_LOCK_NODEV) && 3396 !(mnt_flags & MNT_NODEV)) 3397 return false; 3398 3399 if ((fl & MNT_LOCK_NOSUID) && 3400 !(mnt_flags & MNT_NOSUID)) 3401 return false; 3402 3403 if ((fl & MNT_LOCK_NOEXEC) && 3404 !(mnt_flags & MNT_NOEXEC)) 3405 return false; 3406 3407 if ((fl & MNT_LOCK_ATIME) && 3408 ((fl & MNT_ATIME_MASK) != (mnt_flags & MNT_ATIME_MASK))) 3409 return false; 3410 3411 return true; 3412 } 3413 3414 static int change_mount_ro_state(struct mount *mnt, unsigned int mnt_flags) 3415 { 3416 bool readonly_request = (mnt_flags & MNT_READONLY); 3417 3418 if (readonly_request == __mnt_is_readonly(&mnt->mnt)) 3419 return 0; 3420 3421 if (readonly_request) 3422 return mnt_make_readonly(mnt); 3423 3424 mnt->mnt.mnt_flags &= ~MNT_READONLY; 3425 return 0; 3426 } 3427 3428 static void set_mount_attributes(struct mount *mnt, unsigned int mnt_flags) 3429 { 3430 mnt_flags |= mnt->mnt.mnt_flags & ~MNT_USER_SETTABLE_MASK; 3431 mnt->mnt.mnt_flags = mnt_flags; 3432 touch_mnt_namespace(mnt->mnt_ns); 3433 } 3434 3435 static void mnt_warn_timestamp_expiry(const struct path *mountpoint, 3436 struct vfsmount *mnt) 3437 { 3438 struct super_block *sb = mnt->mnt_sb; 3439 3440 if (!__mnt_is_readonly(mnt) && 3441 (!(sb->s_iflags & SB_I_TS_EXPIRY_WARNED)) && 3442 (ktime_get_real_seconds() + TIME_UPTIME_SEC_MAX > sb->s_time_max)) { 3443 char *buf, *mntpath; 3444 3445 buf = __getname(); 3446 if (buf) 3447 mntpath = d_path(mountpoint, buf, PATH_MAX); 3448 else 3449 mntpath = ERR_PTR(-ENOMEM); 3450 if (IS_ERR(mntpath)) 3451 mntpath = "(unknown)"; 3452 3453 pr_warn("%s filesystem being %s at %s supports timestamps until %ptTd (0x%llx)\n", 3454 sb->s_type->name, 3455 is_mounted(mnt) ? "remounted" : "mounted", 3456 mntpath, &sb->s_time_max, 3457 (unsigned long long)sb->s_time_max); 3458 3459 sb->s_iflags |= SB_I_TS_EXPIRY_WARNED; 3460 __putname(buf); 3461 } 3462 } 3463 3464 /* 3465 * Handle reconfiguration of the mountpoint only without alteration of the 3466 * superblock it refers to. This is triggered by specifying MS_REMOUNT|MS_BIND 3467 * to mount(2). 3468 */ 3469 static int do_reconfigure_mnt(const struct path *path, unsigned int mnt_flags) 3470 { 3471 struct super_block *sb = path->mnt->mnt_sb; 3472 struct mount *mnt = real_mount(path->mnt); 3473 int ret; 3474 3475 if (!check_mnt(mnt)) 3476 return -EINVAL; 3477 3478 if (!path_mounted(path)) 3479 return -EINVAL; 3480 3481 if (!can_change_locked_flags(mnt, mnt_flags)) 3482 return -EPERM; 3483 3484 /* 3485 * We're only checking whether the superblock is read-only not 3486 * changing it, so only take down_read(&sb->s_umount). 3487 */ 3488 down_read(&sb->s_umount); 3489 lock_mount_hash(); 3490 ret = change_mount_ro_state(mnt, mnt_flags); 3491 if (ret == 0) 3492 set_mount_attributes(mnt, mnt_flags); 3493 unlock_mount_hash(); 3494 up_read(&sb->s_umount); 3495 3496 mnt_warn_timestamp_expiry(path, &mnt->mnt); 3497 3498 return ret; 3499 } 3500 3501 /* 3502 * change filesystem flags. dir should be a physical root of filesystem. 3503 * If you've mounted a non-root directory somewhere and want to do remount 3504 * on it - tough luck. 3505 */ 3506 static int do_remount(const struct path *path, int sb_flags, 3507 int mnt_flags, void *data) 3508 { 3509 int err; 3510 struct super_block *sb = path->mnt->mnt_sb; 3511 struct mount *mnt = real_mount(path->mnt); 3512 struct fs_context *fc; 3513 3514 if (!check_mnt(mnt)) 3515 return -EINVAL; 3516 3517 if (!path_mounted(path)) 3518 return -EINVAL; 3519 3520 if (!can_change_locked_flags(mnt, mnt_flags)) 3521 return -EPERM; 3522 3523 fc = fs_context_for_reconfigure(path->dentry, sb_flags, MS_RMT_MASK); 3524 if (IS_ERR(fc)) 3525 return PTR_ERR(fc); 3526 3527 /* 3528 * Indicate to the filesystem that the remount request is coming 3529 * from the legacy mount system call. 3530 */ 3531 fc->oldapi = true; 3532 3533 err = parse_monolithic_mount_data(fc, data); 3534 if (!err) { 3535 down_write(&sb->s_umount); 3536 err = -EPERM; 3537 if (ns_capable(sb->s_user_ns, CAP_SYS_ADMIN)) { 3538 err = reconfigure_super(fc); 3539 if (!err) { 3540 lock_mount_hash(); 3541 set_mount_attributes(mnt, mnt_flags); 3542 unlock_mount_hash(); 3543 } 3544 } 3545 up_write(&sb->s_umount); 3546 } 3547 3548 mnt_warn_timestamp_expiry(path, &mnt->mnt); 3549 3550 put_fs_context(fc); 3551 return err; 3552 } 3553 3554 static inline int tree_contains_unbindable(struct mount *mnt) 3555 { 3556 struct mount *p; 3557 for (p = mnt; p; p = next_mnt(p, mnt)) { 3558 if (IS_MNT_UNBINDABLE(p)) 3559 return 1; 3560 } 3561 return 0; 3562 } 3563 3564 static int do_set_group(const struct path *from_path, const struct path *to_path) 3565 { 3566 struct mount *from = real_mount(from_path->mnt); 3567 struct mount *to = real_mount(to_path->mnt); 3568 int err; 3569 3570 guard(namespace_excl)(); 3571 3572 err = may_change_propagation(from); 3573 if (err) 3574 return err; 3575 err = may_change_propagation(to); 3576 if (err) 3577 return err; 3578 3579 /* To and From paths should be mount roots */ 3580 if (!path_mounted(from_path)) 3581 return -EINVAL; 3582 if (!path_mounted(to_path)) 3583 return -EINVAL; 3584 3585 /* Setting sharing groups is only allowed across same superblock */ 3586 if (from->mnt.mnt_sb != to->mnt.mnt_sb) 3587 return -EINVAL; 3588 3589 /* From mount root should be wider than To mount root */ 3590 if (!is_subdir(to->mnt.mnt_root, from->mnt.mnt_root)) 3591 return -EINVAL; 3592 3593 /* From mount should not have locked children in place of To's root */ 3594 if (has_locked_children(from, to->mnt.mnt_root)) 3595 return -EINVAL; 3596 3597 /* Setting sharing groups is only allowed on private mounts */ 3598 if (IS_MNT_SHARED(to) || IS_MNT_SLAVE(to) || IS_MNT_UNBINDABLE(to)) 3599 return -EINVAL; 3600 3601 /* From should not be private */ 3602 if (!IS_MNT_SHARED(from) && !IS_MNT_SLAVE(from)) 3603 return -EINVAL; 3604 3605 if (IS_MNT_SLAVE(from)) { 3606 hlist_add_behind(&to->mnt_slave, &from->mnt_slave); 3607 to->mnt_master = from->mnt_master; 3608 } 3609 3610 if (IS_MNT_SHARED(from)) { 3611 to->mnt_group_id = from->mnt_group_id; 3612 list_add(&to->mnt_share, &from->mnt_share); 3613 set_mnt_shared(to); 3614 } 3615 3616 guard(mount_locked_reader)(); 3617 touch_mnt_namespace(to->mnt_ns); 3618 3619 return 0; 3620 } 3621 3622 /** 3623 * path_overmounted - check if path is overmounted 3624 * @path: path to check 3625 * 3626 * Check if path is overmounted, i.e., if there's a mount on top of 3627 * @path->mnt with @path->dentry as mountpoint. 3628 * 3629 * Context: namespace_sem must be held at least shared. 3630 * MUST NOT be called under lock_mount_hash() (there one should just 3631 * call __lookup_mnt() and check if it returns NULL). 3632 * Return: If path is overmounted true is returned, false if not. 3633 */ 3634 static inline bool path_overmounted(const struct path *path) 3635 { 3636 unsigned seq = read_seqbegin(&mount_lock); 3637 bool no_child; 3638 3639 rcu_read_lock(); 3640 no_child = !__lookup_mnt(path->mnt, path->dentry); 3641 rcu_read_unlock(); 3642 if (need_seqretry(&mount_lock, seq)) { 3643 read_seqlock_excl(&mount_lock); 3644 no_child = !__lookup_mnt(path->mnt, path->dentry); 3645 read_sequnlock_excl(&mount_lock); 3646 } 3647 return unlikely(!no_child); 3648 } 3649 3650 /* 3651 * Check if there is a possibly empty chain of descent from p1 to p2. 3652 * Locks: namespace_sem (shared) or mount_lock (read_seqlock_excl). 3653 */ 3654 static bool mount_is_ancestor(const struct mount *p1, const struct mount *p2) 3655 { 3656 while (p2 != p1 && mnt_has_parent(p2)) 3657 p2 = p2->mnt_parent; 3658 return p2 == p1; 3659 } 3660 3661 /** 3662 * can_move_mount_beneath - check that we can mount beneath the top mount 3663 * @mnt_from: mount we are trying to move 3664 * @mnt_to: mount under which to mount 3665 * @mp: mountpoint of @mnt_to 3666 * 3667 * - Make sure that @mnt_from can take over the lock of @mnt_to. A 3668 * mount of a mount namespace file is left out of a copy of the tree 3669 * and can't. 3670 * - Ensure that nothing has been mounted on top of @mnt_from before we 3671 * grabbed @namespace_sem to avoid creating pointless shadow mounts. 3672 * - Prevent mounting beneath a mount if the propagation relationship 3673 * between the source mount, parent mount, and top mount would lead to 3674 * nonsensical mount trees. 3675 * 3676 * Context: This function expects namespace_lock() to be held. 3677 * Return: On success 0, and on error a negative error code is returned. 3678 */ 3679 static int can_move_mount_beneath(const struct mount *mnt_from, 3680 const struct mount *mnt_to, 3681 struct pinned_mountpoint *mp) 3682 { 3683 struct mount *parent_mnt_to = mnt_to->mnt_parent; 3684 3685 /* 3686 * @mnt_from takes over the lock of @mnt_to. copy_tree() leaves a 3687 * mount of a mount namespace file out of every copy made without 3688 * CL_COPY_MNT_NS_FILE, together with what is mounted on it, so it 3689 * can't keep anything hidden. 3690 */ 3691 if (IS_MNT_LOCKED(mnt_to) && is_mnt_ns_file(mnt_from->mnt.mnt_root)) 3692 return -EINVAL; 3693 3694 /* Avoid creating shadow mounts during mount propagation. */ 3695 if (mnt_from->overmount) 3696 return -EINVAL; 3697 3698 if (mount_is_ancestor(mnt_to, mnt_from)) 3699 return -EINVAL; 3700 3701 /* 3702 * If the parent mount propagates to the child mount this would 3703 * mean mounting @mnt_from on @mnt_to->mnt_parent and then 3704 * propagating a copy @c of @mnt_from on top of @mnt_to. This 3705 * defeats the whole purpose of mounting beneath another mount. 3706 */ 3707 if (propagation_would_overmount(parent_mnt_to, mnt_to, mp->mp)) 3708 return -EINVAL; 3709 3710 /* 3711 * If @mnt_to->mnt_parent propagates to @mnt_from this would 3712 * mean propagating a copy @c of @mnt_from on top of @mnt_from. 3713 * Afterwards @mnt_from would be mounted on top of 3714 * @mnt_to->mnt_parent and @mnt_to would be unmounted from 3715 * @mnt->mnt_parent and remounted on @mnt_from. But since @c is 3716 * already mounted on @mnt_from, @mnt_to would ultimately be 3717 * remounted on top of @c. Afterwards, @mnt_from would be 3718 * covered by a copy @c of @mnt_from and @c would be covered by 3719 * @mnt_from itself. This defeats the whole purpose of mounting 3720 * @mnt_from beneath @mnt_to. 3721 */ 3722 if (check_mnt(mnt_from) && 3723 propagation_would_overmount(parent_mnt_to, mnt_from, mp->mp)) 3724 return -EINVAL; 3725 3726 return 0; 3727 } 3728 3729 /* may_use_mount() - check if a mount tree can be used 3730 * @mnt: vfsmount to be used 3731 * 3732 * This helper checks if the caller may use the mount tree starting 3733 * from @path->mnt. The caller may use the mount tree under the 3734 * following circumstances: 3735 * 3736 * (1) The caller is located in the mount namespace of the mount tree. 3737 * This also implies that the mount does not belong to an anonymous 3738 * mount namespace. 3739 * (2) The caller is trying to use a mount tree that belongs to an 3740 * anonymous mount namespace. 3741 * 3742 * For that to be safe, this helper enforces that the origin mount 3743 * namespace the anonymous mount namespace was created from is the 3744 * same as the caller's mount namespace by comparing the sequence 3745 * numbers. 3746 * 3747 * The ownership of a non-anonymous mount namespace such as the 3748 * caller's cannot change. 3749 * => We know that the caller's mount namespace is stable. 3750 * 3751 * If the origin sequence number of the anonymous mount namespace is 3752 * the same as the sequence number of the caller's mount namespace. 3753 * => The owning namespaces are the same. 3754 * 3755 * ==> The earlier capability check on the owning namespace of the 3756 * caller's mount namespace ensures that the caller has the 3757 * ability to use the mount tree. 3758 * 3759 * Returns true if the mount tree can be used, false otherwise. 3760 */ 3761 static inline bool may_use_mount(struct mount *mnt) 3762 { 3763 if (check_mnt(mnt)) 3764 return true; 3765 3766 /* 3767 * Make sure that noone unmounted the target path or somehow 3768 * managed to get their hands on something purely kernel 3769 * internal. 3770 */ 3771 if (!is_mounted(&mnt->mnt)) 3772 return false; 3773 3774 return check_anonymous_mnt(mnt); 3775 } 3776 3777 static int do_move_mount(const struct path *old_path, 3778 const struct path *new_path, 3779 enum mnt_tree_flags_t flags) 3780 { 3781 struct mount *old = real_mount(old_path->mnt); 3782 int err; 3783 bool beneath = flags & MNT_TREE_BENEATH; 3784 3785 if (!path_mounted(old_path)) 3786 return -EINVAL; 3787 3788 if (d_is_dir(new_path->dentry) != d_is_dir(old_path->dentry)) 3789 return -EINVAL; 3790 3791 LOCK_MOUNT_MAYBE_BENEATH(mp, new_path, beneath); 3792 if (IS_ERR(mp.parent)) 3793 return PTR_ERR(mp.parent); 3794 3795 if (check_mnt(old)) { 3796 /* if the source is in our namespace... */ 3797 /* ... it should be detachable from parent */ 3798 if (!mnt_has_parent(old) || IS_MNT_LOCKED(old)) 3799 return -EINVAL; 3800 /* ... which should not be shared */ 3801 if (IS_MNT_SHARED(old->mnt_parent)) 3802 return -EINVAL; 3803 /* ... and the target should be in our namespace */ 3804 if (!check_mnt(mp.parent)) 3805 return -EINVAL; 3806 } else { 3807 /* 3808 * otherwise the source must be the root of some anon namespace. 3809 */ 3810 if (!anon_ns_root(old)) 3811 return -EINVAL; 3812 /* 3813 * Bail out early if the target is within the same namespace - 3814 * subsequent checks would've rejected that, but they lose 3815 * some corner cases if we check it early. 3816 */ 3817 if (old->mnt_ns == mp.parent->mnt_ns) 3818 return -EINVAL; 3819 /* 3820 * Target should be either in our namespace or in an acceptable 3821 * anon namespace, sensu check_anonymous_mnt(). 3822 */ 3823 if (!may_use_mount(mp.parent)) 3824 return -EINVAL; 3825 } 3826 3827 if (beneath) { 3828 struct mount *over = real_mount(new_path->mnt); 3829 3830 if (mp.parent != over->mnt_parent) 3831 over = mp.parent->overmount; 3832 err = can_move_mount_beneath(old, over, &mp); 3833 if (err) 3834 return err; 3835 } 3836 3837 /* 3838 * Don't move a mount tree containing unbindable mounts to a destination 3839 * mount which is shared. 3840 */ 3841 if (IS_MNT_SHARED(mp.parent) && tree_contains_unbindable(old)) 3842 return -EINVAL; 3843 if (!check_for_nsfs_mounts(old)) 3844 return -ELOOP; 3845 if (mount_is_ancestor(old, mp.parent)) 3846 return -ELOOP; 3847 3848 return attach_recursive_mnt(old, &mp); 3849 } 3850 3851 static int do_move_mount_old(const struct path *path, const char *old_name) 3852 { 3853 struct path old_path __free(path_put) = {}; 3854 int err; 3855 3856 if (!old_name || !*old_name) 3857 return -EINVAL; 3858 3859 err = kern_path(old_name, LOOKUP_FOLLOW, &old_path); 3860 if (err) 3861 return err; 3862 3863 return do_move_mount(&old_path, path, 0); 3864 } 3865 3866 /* 3867 * add a mount into a namespace's mount tree 3868 */ 3869 static int do_add_mount(struct mount *newmnt, const struct pinned_mountpoint *mp, 3870 int mnt_flags) 3871 { 3872 struct mount *parent = mp->parent; 3873 3874 if (IS_ERR(parent)) 3875 return PTR_ERR(parent); 3876 3877 mnt_flags &= ~MNT_INTERNAL_FLAGS; 3878 3879 if (unlikely(!check_mnt(parent))) { 3880 /* that's acceptable only for automounts done in private ns */ 3881 if (!(mnt_flags & MNT_SHRINKABLE)) 3882 return -EINVAL; 3883 /* ... and for those we'd better have mountpoint still alive */ 3884 if (!is_mounted(&parent->mnt)) 3885 return -EINVAL; 3886 } 3887 3888 /* Refuse the same filesystem on the same mount point */ 3889 if (parent->mnt.mnt_sb == newmnt->mnt.mnt_sb && 3890 parent->mnt.mnt_root == mp->mp->m_dentry) 3891 return -EBUSY; 3892 3893 if (d_is_symlink(newmnt->mnt.mnt_root)) 3894 return -EINVAL; 3895 3896 newmnt->mnt.mnt_flags = mnt_flags; 3897 return graft_tree(newmnt, mp); 3898 } 3899 3900 static bool mount_too_revealing(const struct super_block *sb, int *new_mnt_flags); 3901 3902 /* 3903 * Create a new mount using a superblock configuration and request it 3904 * be added to the namespace tree. 3905 */ 3906 static int do_new_mount_fc(struct fs_context *fc, const struct path *mountpoint, 3907 unsigned int mnt_flags) 3908 { 3909 struct super_block *sb; 3910 struct vfsmount *mnt __free(mntput) = fc_mount(fc); 3911 int error; 3912 3913 if (IS_ERR(mnt)) 3914 return PTR_ERR(mnt); 3915 3916 sb = fc->root->d_sb; 3917 error = security_sb_kern_mount(sb); 3918 if (unlikely(error)) 3919 return error; 3920 3921 if (unlikely(mount_too_revealing(sb, &mnt_flags))) { 3922 errorfcp(fc, "VFS", "Mount too revealing"); 3923 return -EPERM; 3924 } 3925 3926 mnt_warn_timestamp_expiry(mountpoint, mnt); 3927 3928 LOCK_MOUNT(mp, mountpoint); 3929 error = do_add_mount(real_mount(mnt), &mp, mnt_flags); 3930 if (!error) 3931 retain_and_null_ptr(mnt); // consumed on success 3932 return error; 3933 } 3934 3935 /* 3936 * create a new mount for userspace and request it to be added into the 3937 * namespace's tree 3938 */ 3939 static int do_new_mount(const struct path *path, const char *fstype, 3940 int sb_flags, int mnt_flags, 3941 const char *name, void *data) 3942 { 3943 struct file_system_type *type; 3944 struct fs_context *fc; 3945 const char *subtype = NULL; 3946 int err = 0; 3947 3948 if (!fstype) 3949 return -EINVAL; 3950 3951 type = get_fs_type(fstype); 3952 if (!type) 3953 return -ENODEV; 3954 3955 if (type->fs_flags & FS_HAS_SUBTYPE) { 3956 subtype = strchr(fstype, '.'); 3957 if (subtype) { 3958 subtype++; 3959 if (!*subtype) { 3960 put_filesystem(type); 3961 return -EINVAL; 3962 } 3963 } 3964 } 3965 3966 fc = fs_context_for_mount(type, sb_flags); 3967 put_filesystem(type); 3968 if (IS_ERR(fc)) 3969 return PTR_ERR(fc); 3970 3971 /* 3972 * Indicate to the filesystem that the mount request is coming 3973 * from the legacy mount system call. 3974 */ 3975 fc->oldapi = true; 3976 3977 if (subtype) 3978 err = vfs_parse_fs_string(fc, "subtype", subtype); 3979 if (!err && name) 3980 err = vfs_parse_fs_string(fc, "source", name); 3981 if (!err) 3982 err = parse_monolithic_mount_data(fc, data); 3983 if (!err && !mount_capable(fc)) 3984 err = -EPERM; 3985 if (!err) 3986 err = do_new_mount_fc(fc, path, mnt_flags); 3987 3988 put_fs_context(fc); 3989 return err; 3990 } 3991 3992 static void lock_mount_exact(const struct path *path, 3993 struct pinned_mountpoint *mp, bool copy_mount, 3994 unsigned int copy_flags) 3995 { 3996 struct dentry *dentry = path->dentry; 3997 int err; 3998 3999 /* Assert that inode_lock() locked the correct inode. */ 4000 VFS_WARN_ON_ONCE(copy_mount && !path_mounted(path)); 4001 4002 inode_lock(dentry->d_inode); 4003 namespace_lock(); 4004 if (unlikely(cant_mount(dentry))) 4005 err = -ENOENT; 4006 else if (!copy_mount && path_overmounted(path)) 4007 err = -EBUSY; 4008 else 4009 err = get_mountpoint(dentry, mp); 4010 if (unlikely(err)) { 4011 namespace_unlock(); 4012 inode_unlock(dentry->d_inode); 4013 mp->parent = ERR_PTR(err); 4014 return; 4015 } 4016 4017 if (copy_mount) 4018 mp->parent = clone_mnt(real_mount(path->mnt), dentry, copy_flags); 4019 else 4020 mp->parent = real_mount(path->mnt); 4021 if (unlikely(IS_ERR(mp->parent))) 4022 __unlock_mount(mp); 4023 } 4024 4025 int finish_automount(struct vfsmount *__m, const struct path *path) 4026 { 4027 struct vfsmount *m __free(mntput) = __m; 4028 struct mount *mnt; 4029 int err; 4030 4031 if (!m) 4032 return 0; 4033 if (IS_ERR(m)) 4034 return PTR_ERR(m); 4035 4036 mnt = real_mount(m); 4037 4038 if (m->mnt_root == path->dentry) 4039 return -ELOOP; 4040 4041 /* 4042 * we don't want to use LOCK_MOUNT() - in this case finding something 4043 * that overmounts our mountpoint to be means "quitely drop what we've 4044 * got", not "try to mount it on top". 4045 */ 4046 LOCK_MOUNT_EXACT(mp, path); 4047 if (mp.parent == ERR_PTR(-EBUSY)) 4048 return 0; 4049 4050 err = do_add_mount(mnt, &mp, path->mnt->mnt_flags | MNT_SHRINKABLE); 4051 if (likely(!err)) 4052 retain_and_null_ptr(m); 4053 return err; 4054 } 4055 4056 /** 4057 * mnt_set_expiry - Put a mount on an expiration list 4058 * @mnt: The mount to list. 4059 * @expiry_list: The list to add the mount to. 4060 */ 4061 void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list) 4062 { 4063 guard(mount_locked_reader)(); 4064 list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list); 4065 } 4066 EXPORT_SYMBOL(mnt_set_expiry); 4067 4068 /* 4069 * process a list of expirable mountpoints with the intent of discarding any 4070 * mountpoints that aren't in use and haven't been touched since last we came 4071 * here 4072 */ 4073 void mark_mounts_for_expiry(struct list_head *mounts) 4074 { 4075 struct mount *mnt, *next; 4076 LIST_HEAD(graveyard); 4077 4078 if (list_empty(mounts)) 4079 return; 4080 4081 guard(namespace_excl)(); 4082 guard(mount_writer)(); 4083 4084 /* extract from the expiration list every vfsmount that matches the 4085 * following criteria: 4086 * - already mounted 4087 * - only referenced by its parent vfsmount 4088 * - still marked for expiry (marked on the last call here; marks are 4089 * cleared by mntput()) 4090 */ 4091 list_for_each_entry_safe(mnt, next, mounts, mnt_expire) { 4092 if (!is_mounted(&mnt->mnt)) 4093 continue; 4094 /* lock_mnt_tree() leaves expirable mounts alone */ 4095 VFS_WARN_ON_ONCE(IS_MNT_LOCKED(mnt)); 4096 if (!xchg(&mnt->mnt_expiry_mark, 1) || 4097 propagate_mount_busy(mnt, 1)) 4098 continue; 4099 list_move(&mnt->mnt_expire, &graveyard); 4100 } 4101 while (!list_empty(&graveyard)) { 4102 mnt = list_first_entry(&graveyard, struct mount, mnt_expire); 4103 /* an earlier umount_tree() may have moved a busy mount here */ 4104 if (propagate_mount_busy(mnt, 1)) { 4105 list_move(&mnt->mnt_expire, mounts); 4106 continue; 4107 } 4108 touch_mnt_namespace(mnt->mnt_ns); 4109 umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC); 4110 } 4111 } 4112 4113 EXPORT_SYMBOL_GPL(mark_mounts_for_expiry); 4114 4115 /* 4116 * Unmount @mnt if it's a shrinkable mount without children that nobody uses. 4117 * 4118 * mount_lock must be held for write 4119 */ 4120 static bool shrink_submount(struct mount *mnt) 4121 { 4122 /* not the kernel's to remove either */ 4123 if (IS_MNT_LOCKED(mnt) || propagate_mount_busy(mnt, 1)) 4124 return false; 4125 touch_mnt_namespace(mnt->mnt_ns); 4126 umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC); 4127 return true; 4128 } 4129 4130 /* 4131 * Ripoff of 'select_parent()' 4132 * 4133 * unmount the shrinkable submounts of @parent that aren't busy, children 4134 * before their parent, and say whether anything went 4135 * 4136 * The cursor into the children of @this_parent survives the umount of a 4137 * child mount without child mounts. The mounts that get umounted together with 4138 * it are located under receiving mounts of @this_parent and never under 4139 * @this_parent itself. The one exception is @this_parent getting unmounted 4140 * then the walk starts over. 4141 */ 4142 static bool __shrink_submounts(struct mount *parent) 4143 { 4144 struct mount *this_parent = parent; 4145 struct list_head *next; 4146 bool shrunk = false; 4147 4148 repeat: 4149 next = this_parent->mnt_mounts.next; 4150 resume: 4151 while (next != &this_parent->mnt_mounts) { 4152 struct list_head *tmp = next; 4153 struct mount *mnt = list_entry(tmp, struct mount, mnt_child); 4154 4155 next = tmp->next; 4156 if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE)) 4157 continue; 4158 /* 4159 * Descend a level if the d_mounts list is non-empty. 4160 */ 4161 if (!list_empty(&mnt->mnt_mounts)) { 4162 this_parent = mnt; 4163 goto repeat; 4164 } 4165 if (!shrink_submount(mnt)) 4166 continue; 4167 shrunk = true; 4168 if (unlikely(this_parent->mnt.mnt_flags & MNT_UMOUNT)) 4169 return true; 4170 } 4171 /* 4172 * All done at this level ... ascend and resume the search 4173 */ 4174 if (this_parent != parent) { 4175 struct mount *mnt = this_parent; 4176 4177 next = mnt->mnt_child.next; 4178 this_parent = mnt->mnt_parent; 4179 /* its children are gone, maybe it can go as well */ 4180 if (shrink_submount(mnt)) { 4181 shrunk = true; 4182 if (unlikely(this_parent->mnt.mnt_flags & MNT_UMOUNT)) 4183 return true; 4184 } 4185 goto resume; 4186 } 4187 return shrunk; 4188 } 4189 4190 /* 4191 * unmount the shrinkable submounts of @mnt that aren't busy 4192 * 4193 * The busy check and the umount of a mount are adjacent. An umount can 4194 * still empty or move a mount in a part of the tree that was walked 4195 * already, so walk again until nothing goes. 4196 * 4197 * mount_lock must be held for write 4198 */ 4199 static void shrink_submounts(struct mount *mnt) 4200 { 4201 for (;;) { 4202 if (!__shrink_submounts(mnt)) 4203 break; 4204 } 4205 } 4206 4207 static void *copy_mount_options(const void __user * data) 4208 { 4209 char *copy; 4210 unsigned left, offset; 4211 4212 if (!data) 4213 return NULL; 4214 4215 copy = kmalloc(PAGE_SIZE, GFP_KERNEL); 4216 if (!copy) 4217 return ERR_PTR(-ENOMEM); 4218 4219 left = copy_from_user(copy, data, PAGE_SIZE); 4220 4221 /* 4222 * Not all architectures have an exact copy_from_user(). Resort to 4223 * byte at a time. 4224 */ 4225 offset = PAGE_SIZE - left; 4226 while (left) { 4227 char c; 4228 if (get_user(c, (const char __user *)data + offset)) 4229 break; 4230 copy[offset] = c; 4231 left--; 4232 offset++; 4233 } 4234 4235 if (left == PAGE_SIZE) { 4236 kfree(copy); 4237 return ERR_PTR(-EFAULT); 4238 } 4239 4240 return copy; 4241 } 4242 4243 static char *copy_mount_string(const void __user *data) 4244 { 4245 return data ? strndup_user(data, PATH_MAX) : NULL; 4246 } 4247 4248 /* 4249 * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to 4250 * be given to the mount() call (ie: read-only, no-dev, no-suid etc). 4251 * 4252 * data is a (void *) that can point to any structure up to 4253 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent 4254 * information (or be NULL). 4255 * 4256 * Pre-0.97 versions of mount() didn't have a flags word. 4257 * When the flags word was introduced its top half was required 4258 * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9. 4259 * Therefore, if this magic number is present, it carries no information 4260 * and must be discarded. 4261 */ 4262 int path_mount(const char *dev_name, const struct path *path, 4263 const char *type_page, unsigned long flags, void *data_page) 4264 { 4265 unsigned int mnt_flags = 0, sb_flags; 4266 int ret; 4267 4268 /* Discard magic */ 4269 if ((flags & MS_MGC_MSK) == MS_MGC_VAL) 4270 flags &= ~MS_MGC_MSK; 4271 4272 /* Basic sanity checks */ 4273 if (data_page) 4274 ((char *)data_page)[PAGE_SIZE - 1] = 0; 4275 4276 if (flags & MS_NOUSER) 4277 return -EINVAL; 4278 4279 ret = security_sb_mount(dev_name, path, type_page, flags, data_page); 4280 if (ret) 4281 return ret; 4282 if (!may_mount()) 4283 return -EPERM; 4284 if (flags & SB_MANDLOCK) 4285 warn_mandlock(); 4286 4287 /* Default to relatime unless overridden */ 4288 if (!(flags & MS_NOATIME)) 4289 mnt_flags |= MNT_RELATIME; 4290 4291 /* Separate the per-mountpoint flags */ 4292 if (flags & MS_NOSUID) 4293 mnt_flags |= MNT_NOSUID; 4294 if (flags & MS_NODEV) 4295 mnt_flags |= MNT_NODEV; 4296 if (flags & MS_NOEXEC) 4297 mnt_flags |= MNT_NOEXEC; 4298 if (flags & MS_NOATIME) 4299 mnt_flags |= MNT_NOATIME; 4300 if (flags & MS_NODIRATIME) 4301 mnt_flags |= MNT_NODIRATIME; 4302 if (flags & MS_STRICTATIME) 4303 mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME); 4304 if (flags & MS_RDONLY) 4305 mnt_flags |= MNT_READONLY; 4306 if (flags & MS_NOSYMFOLLOW) 4307 mnt_flags |= MNT_NOSYMFOLLOW; 4308 4309 /* The default atime for remount is preservation */ 4310 if ((flags & MS_REMOUNT) && 4311 ((flags & (MS_NOATIME | MS_NODIRATIME | MS_RELATIME | 4312 MS_STRICTATIME)) == 0)) { 4313 mnt_flags &= ~MNT_ATIME_MASK; 4314 mnt_flags |= path->mnt->mnt_flags & MNT_ATIME_MASK; 4315 } 4316 4317 sb_flags = flags & (SB_RDONLY | 4318 SB_SYNCHRONOUS | 4319 SB_MANDLOCK | 4320 SB_DIRSYNC | 4321 SB_SILENT | 4322 SB_POSIXACL | 4323 SB_LAZYTIME | 4324 SB_I_VERSION); 4325 4326 if ((flags & (MS_REMOUNT | MS_BIND)) == (MS_REMOUNT | MS_BIND)) 4327 return do_reconfigure_mnt(path, mnt_flags); 4328 if (flags & MS_REMOUNT) 4329 return do_remount(path, sb_flags, mnt_flags, data_page); 4330 if (flags & MS_BIND) 4331 return do_loopback(path, dev_name, flags & MS_REC); 4332 if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 4333 return do_change_type(path, flags); 4334 if (flags & MS_MOVE) 4335 return do_move_mount_old(path, dev_name); 4336 4337 return do_new_mount(path, type_page, sb_flags, mnt_flags, dev_name, 4338 data_page); 4339 } 4340 4341 int do_mount(const char *dev_name, const char __user *dir_name, 4342 const char *type_page, unsigned long flags, void *data_page) 4343 { 4344 struct path path __free(path_put) = {}; 4345 int ret; 4346 4347 ret = user_path_at(AT_FDCWD, dir_name, LOOKUP_FOLLOW, &path); 4348 if (ret) 4349 return ret; 4350 return path_mount(dev_name, &path, type_page, flags, data_page); 4351 } 4352 4353 static struct ucounts *inc_mnt_namespaces(struct user_namespace *ns) 4354 { 4355 return inc_ucount(ns, current_euid(), UCOUNT_MNT_NAMESPACES); 4356 } 4357 4358 static void dec_mnt_namespaces(struct ucounts *ucounts) 4359 { 4360 dec_ucount(ucounts, UCOUNT_MNT_NAMESPACES); 4361 } 4362 4363 static void free_mnt_ns(struct mnt_namespace *ns) 4364 { 4365 ns_common_free(ns); 4366 dec_mnt_namespaces(ns->ucounts); 4367 /* the last active reference is gone, no mark can show up anymore */ 4368 fsnotify_mntns_delete(ns); 4369 mnt_ns_tree_remove(ns); 4370 } 4371 4372 static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool anon) 4373 { 4374 struct mnt_namespace *new_ns; 4375 struct ucounts *ucounts; 4376 int ret; 4377 4378 ucounts = inc_mnt_namespaces(user_ns); 4379 if (!ucounts) 4380 return ERR_PTR(-ENOSPC); 4381 4382 new_ns = kzalloc_obj(struct mnt_namespace, GFP_KERNEL_ACCOUNT); 4383 if (!new_ns) { 4384 dec_mnt_namespaces(ucounts); 4385 return ERR_PTR(-ENOMEM); 4386 } 4387 4388 if (anon) 4389 ret = ns_common_init_inum(new_ns, MNT_NS_ANON_INO); 4390 else 4391 ret = ns_common_init(new_ns); 4392 if (ret) { 4393 kfree(new_ns); 4394 dec_mnt_namespaces(ucounts); 4395 return ERR_PTR(ret); 4396 } 4397 ns_tree_gen_id(new_ns); 4398 4399 new_ns->is_anon = anon; 4400 refcount_set(&new_ns->passive, 1); 4401 new_ns->mounts = RB_ROOT; 4402 init_waitqueue_head(&new_ns->poll); 4403 new_ns->user_ns = get_user_ns(user_ns); 4404 new_ns->ucounts = ucounts; 4405 return new_ns; 4406 } 4407 4408 __latent_entropy 4409 struct mnt_namespace *copy_mnt_ns(u64 flags, struct mnt_namespace *ns, 4410 struct user_namespace *user_ns, struct fs_struct *new_fs) 4411 { 4412 struct mnt_namespace *new_ns; 4413 struct path old_root __free(path_put) = {}; 4414 struct path old_pwd __free(path_put) = {}; 4415 struct mount *p, *q; 4416 struct mount *old; 4417 struct mount *new; 4418 int copy_flags; 4419 4420 if (likely(!(flags & CLONE_NEWNS))) { 4421 get_mnt_ns(ns); 4422 return ns; 4423 } 4424 4425 old = ns->root; 4426 4427 new_ns = alloc_mnt_ns(user_ns, false); 4428 if (IS_ERR(new_ns)) 4429 return new_ns; 4430 4431 guard(namespace_excl)(); 4432 4433 if (flags & CLONE_EMPTY_MNTNS) 4434 copy_flags = 0; 4435 else 4436 copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE; 4437 if (user_ns != ns->user_ns) 4438 copy_flags |= CL_SLAVE; 4439 4440 if (flags & CLONE_EMPTY_MNTNS) 4441 new = clone_mnt(old, old->mnt.mnt_root, copy_flags); 4442 else 4443 new = copy_tree(old, old->mnt.mnt_root, copy_flags); 4444 if (IS_ERR(new)) { 4445 emptied_ns = new_ns; 4446 return ERR_CAST(new); 4447 } 4448 if (user_ns != ns->user_ns) { 4449 guard(mount_writer)(); 4450 lock_mnt_tree(new); 4451 } 4452 new_ns->root = new; 4453 4454 if (flags & CLONE_EMPTY_MNTNS) { 4455 /* 4456 * Empty mount namespace: only the root mount exists. 4457 * Reset root and pwd to the cloned mount's root dentry. 4458 */ 4459 if (new_fs) { 4460 old_root = new_fs->root; 4461 old_pwd = new_fs->pwd; 4462 4463 new_fs->root.mnt = mntget(&new->mnt); 4464 new_fs->root.dentry = dget(new->mnt.mnt_root); 4465 4466 new_fs->pwd.mnt = mntget(&new->mnt); 4467 new_fs->pwd.dentry = dget(new->mnt.mnt_root); 4468 } 4469 mnt_add_to_ns(new_ns, new); 4470 new_ns->nr_mounts++; 4471 } else { 4472 /* 4473 * Full copy: walk old and new trees in parallel, switching 4474 * the tsk->fs->* elements and marking new vfsmounts as 4475 * belonging to new namespace. We have already acquired a 4476 * private fs_struct, so tsk->fs->lock is not needed. 4477 */ 4478 p = old; 4479 q = new; 4480 while (p) { 4481 mnt_add_to_ns(new_ns, q); 4482 new_ns->nr_mounts++; 4483 if (new_fs) { 4484 if (&p->mnt == new_fs->root.mnt) { 4485 old_root.mnt = new_fs->root.mnt; 4486 new_fs->root.mnt = mntget(&q->mnt); 4487 } 4488 if (&p->mnt == new_fs->pwd.mnt) { 4489 old_pwd.mnt = new_fs->pwd.mnt; 4490 new_fs->pwd.mnt = mntget(&q->mnt); 4491 } 4492 } 4493 p = next_mnt(p, old); 4494 q = next_mnt(q, new); 4495 if (!q) 4496 break; 4497 // an mntns binding we'd skipped? 4498 while (p->mnt.mnt_root != q->mnt.mnt_root) 4499 p = next_mnt(skip_mnt_tree(p), old); 4500 } 4501 } 4502 ns_tree_add_raw(new_ns); 4503 return new_ns; 4504 } 4505 4506 struct dentry *mount_subtree(struct vfsmount *m, const char *name) 4507 { 4508 struct mount *mnt = real_mount(m); 4509 struct mnt_namespace *ns; 4510 struct super_block *s; 4511 struct path path; 4512 int err; 4513 4514 ns = alloc_mnt_ns(&init_user_ns, true); 4515 if (IS_ERR(ns)) { 4516 mntput(m); 4517 return ERR_CAST(ns); 4518 } 4519 ns->root = mnt; 4520 ns->nr_mounts++; 4521 mnt_add_to_ns(ns, mnt); 4522 4523 err = vfs_path_lookup(m->mnt_root, m, 4524 name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path); 4525 4526 put_mnt_ns(ns); 4527 4528 if (err) 4529 return ERR_PTR(err); 4530 4531 /* trade a vfsmount reference for active sb one */ 4532 s = path.mnt->mnt_sb; 4533 atomic_inc(&s->s_active); 4534 mntput(path.mnt); 4535 /* lock the sucker */ 4536 down_write(&s->s_umount); 4537 /* ... and return the root of (sub)tree on it */ 4538 return path.dentry; 4539 } 4540 EXPORT_SYMBOL(mount_subtree); 4541 4542 SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name, 4543 char __user *, type, unsigned long, flags, void __user *, data) 4544 { 4545 int ret; 4546 char *kernel_type; 4547 char *kernel_dev; 4548 void *options; 4549 4550 kernel_type = copy_mount_string(type); 4551 ret = PTR_ERR(kernel_type); 4552 if (IS_ERR(kernel_type)) 4553 goto out_type; 4554 4555 kernel_dev = copy_mount_string(dev_name); 4556 ret = PTR_ERR(kernel_dev); 4557 if (IS_ERR(kernel_dev)) 4558 goto out_dev; 4559 4560 options = copy_mount_options(data); 4561 ret = PTR_ERR(options); 4562 if (IS_ERR(options)) 4563 goto out_data; 4564 4565 ret = do_mount(kernel_dev, dir_name, kernel_type, flags, options); 4566 4567 kfree(options); 4568 out_data: 4569 kfree(kernel_dev); 4570 out_dev: 4571 kfree(kernel_type); 4572 out_type: 4573 return ret; 4574 } 4575 4576 #define FSMOUNT_VALID_FLAGS \ 4577 (MOUNT_ATTR_RDONLY | MOUNT_ATTR_NOSUID | MOUNT_ATTR_NODEV | \ 4578 MOUNT_ATTR_NOEXEC | MOUNT_ATTR__ATIME | MOUNT_ATTR_NODIRATIME | \ 4579 MOUNT_ATTR_NOSYMFOLLOW) 4580 4581 #define MOUNT_SETATTR_VALID_FLAGS (FSMOUNT_VALID_FLAGS | MOUNT_ATTR_IDMAP) 4582 4583 #define MOUNT_SETATTR_PROPAGATION_FLAGS \ 4584 (MS_UNBINDABLE | MS_PRIVATE | MS_SLAVE | MS_SHARED) 4585 4586 static unsigned int attr_flags_to_mnt_flags(u64 attr_flags) 4587 { 4588 unsigned int mnt_flags = 0; 4589 4590 if (attr_flags & MOUNT_ATTR_RDONLY) 4591 mnt_flags |= MNT_READONLY; 4592 if (attr_flags & MOUNT_ATTR_NOSUID) 4593 mnt_flags |= MNT_NOSUID; 4594 if (attr_flags & MOUNT_ATTR_NODEV) 4595 mnt_flags |= MNT_NODEV; 4596 if (attr_flags & MOUNT_ATTR_NOEXEC) 4597 mnt_flags |= MNT_NOEXEC; 4598 if (attr_flags & MOUNT_ATTR_NODIRATIME) 4599 mnt_flags |= MNT_NODIRATIME; 4600 if (attr_flags & MOUNT_ATTR_NOSYMFOLLOW) 4601 mnt_flags |= MNT_NOSYMFOLLOW; 4602 4603 return mnt_flags; 4604 } 4605 4606 /* 4607 * Create a kernel mount representation for a new, prepared superblock 4608 * (specified by fs_fd) and attach to an open_tree-like file descriptor. 4609 */ 4610 SYSCALL_DEFINE3(fsmount, int, fs_fd, unsigned int, flags, 4611 unsigned int, attr_flags) 4612 { 4613 struct path new_path __free(path_put) = {}; 4614 struct mnt_namespace *ns; 4615 struct fs_context *fc; 4616 struct vfsmount *new_mnt; 4617 struct mount *mnt; 4618 unsigned int mnt_flags = 0; 4619 long ret; 4620 4621 if ((flags & ~(FSMOUNT_CLOEXEC | FSMOUNT_NAMESPACE)) != 0) 4622 return -EINVAL; 4623 4624 if ((flags & FSMOUNT_NAMESPACE) && 4625 !ns_capable(current_user_ns(), CAP_SYS_ADMIN)) 4626 return -EPERM; 4627 4628 if (!(flags & FSMOUNT_NAMESPACE) && !may_mount()) 4629 return -EPERM; 4630 4631 if (attr_flags & ~FSMOUNT_VALID_FLAGS) 4632 return -EINVAL; 4633 4634 mnt_flags = attr_flags_to_mnt_flags(attr_flags); 4635 4636 switch (attr_flags & MOUNT_ATTR__ATIME) { 4637 case MOUNT_ATTR_STRICTATIME: 4638 break; 4639 case MOUNT_ATTR_NOATIME: 4640 mnt_flags |= MNT_NOATIME; 4641 break; 4642 case MOUNT_ATTR_RELATIME: 4643 mnt_flags |= MNT_RELATIME; 4644 break; 4645 default: 4646 return -EINVAL; 4647 } 4648 4649 CLASS(fd, f)(fs_fd); 4650 if (fd_empty(f)) 4651 return -EBADF; 4652 4653 if (fd_file(f)->f_op != &fscontext_fops) 4654 return -EINVAL; 4655 4656 fc = fd_file(f)->private_data; 4657 4658 ACQUIRE(mutex_intr, uapi_mutex)(&fc->uapi_mutex); 4659 ret = ACQUIRE_ERR(mutex_intr, &uapi_mutex); 4660 if (ret) 4661 return ret; 4662 4663 /* There must be a valid superblock or we can't mount it */ 4664 ret = -EINVAL; 4665 if (!fc->root) 4666 return ret; 4667 4668 ret = -EPERM; 4669 if (mount_too_revealing(fc->root->d_sb, &mnt_flags)) { 4670 errorfcp(fc, "VFS", "Mount too revealing"); 4671 return ret; 4672 } 4673 4674 ret = -EBUSY; 4675 if (fc->phase != FS_CONTEXT_AWAITING_MOUNT) 4676 return ret; 4677 4678 if (fc->sb_flags & SB_MANDLOCK) 4679 warn_mandlock(); 4680 4681 new_mnt = vfs_create_mount(fc); 4682 if (IS_ERR(new_mnt)) 4683 return PTR_ERR(new_mnt); 4684 if (new_mnt->mnt_sb->s_flags & SB_NOUSER) { 4685 mntput(new_mnt); 4686 return -EINVAL; 4687 } 4688 new_mnt->mnt_flags = mnt_flags; 4689 4690 new_path.dentry = dget(fc->root); 4691 new_path.mnt = new_mnt; 4692 4693 /* We've done the mount bit - now move the file context into more or 4694 * less the same state as if we'd done an fspick(). We don't want to 4695 * do any memory allocation or anything like that at this point as we 4696 * don't want to have to handle any errors incurred. 4697 */ 4698 vfs_clean_context(fc); 4699 4700 if (flags & FSMOUNT_NAMESPACE) 4701 return FD_ADD((flags & FSMOUNT_CLOEXEC) ? O_CLOEXEC : 0, 4702 open_new_namespace(&new_path, MOUNT_COPY_NEW)); 4703 4704 ns = alloc_mnt_ns(current->nsproxy->mnt_ns->user_ns, true); 4705 if (IS_ERR(ns)) 4706 return PTR_ERR(ns); 4707 mnt = real_mount(new_path.mnt); 4708 ns->root = mnt; 4709 ns->nr_mounts = 1; 4710 mnt_add_to_ns(ns, mnt); 4711 mntget(new_path.mnt); 4712 4713 FD_PREPARE(fdf, (flags & FSMOUNT_CLOEXEC) ? O_CLOEXEC : 0, 4714 dentry_open(&new_path, O_PATH, fc->cred)); 4715 if (fdf->fd < 0) { 4716 dissolve_on_fput(no_free_ptr(new_path.mnt)); 4717 return fdf->fd; 4718 } 4719 4720 /* 4721 * Attach to an apparent O_PATH fd with a note that we 4722 * need to unmount it, not just simply put it. 4723 */ 4724 fdf->file->f_mode |= FMODE_NEED_UNMOUNT; 4725 return fd_publish(fdf); 4726 } 4727 4728 static inline int vfs_move_mount(const struct path *from_path, 4729 const struct path *to_path, 4730 enum mnt_tree_flags_t mflags) 4731 { 4732 int ret; 4733 4734 ret = security_move_mount(from_path, to_path); 4735 if (ret) 4736 return ret; 4737 4738 if (mflags & MNT_TREE_PROPAGATION) 4739 return do_set_group(from_path, to_path); 4740 4741 return do_move_mount(from_path, to_path, mflags); 4742 } 4743 4744 /* 4745 * Move a mount from one place to another. In combination with 4746 * fsopen()/fsmount() this is used to install a new mount and in combination 4747 * with open_tree(OPEN_TREE_CLONE [| AT_RECURSIVE]) it can be used to copy 4748 * a mount subtree. 4749 * 4750 * Note the flags value is a combination of MOVE_MOUNT_* flags. 4751 */ 4752 SYSCALL_DEFINE5(move_mount, 4753 int, from_dfd, const char __user *, from_pathname, 4754 int, to_dfd, const char __user *, to_pathname, 4755 unsigned int, flags) 4756 { 4757 struct path to_path __free(path_put) = {}; 4758 struct path from_path __free(path_put) = {}; 4759 unsigned int lflags, uflags; 4760 enum mnt_tree_flags_t mflags = 0; 4761 int ret = 0; 4762 4763 if (!may_mount()) 4764 return -EPERM; 4765 4766 if (flags & ~MOVE_MOUNT__MASK) 4767 return -EINVAL; 4768 4769 if ((flags & (MOVE_MOUNT_BENEATH | MOVE_MOUNT_SET_GROUP)) == 4770 (MOVE_MOUNT_BENEATH | MOVE_MOUNT_SET_GROUP)) 4771 return -EINVAL; 4772 4773 if (flags & MOVE_MOUNT_SET_GROUP) mflags |= MNT_TREE_PROPAGATION; 4774 if (flags & MOVE_MOUNT_BENEATH) mflags |= MNT_TREE_BENEATH; 4775 4776 uflags = 0; 4777 if (flags & MOVE_MOUNT_T_EMPTY_PATH) 4778 uflags = AT_EMPTY_PATH; 4779 4780 CLASS(filename_maybe_null,to_name)(to_pathname, uflags); 4781 if (!to_name && to_dfd >= 0) { 4782 CLASS(fd_raw, f_to)(to_dfd); 4783 if (fd_empty(f_to)) 4784 return -EBADF; 4785 4786 to_path = fd_file(f_to)->f_path; 4787 path_get(&to_path); 4788 } else { 4789 lflags = 0; 4790 if (flags & MOVE_MOUNT_T_SYMLINKS) 4791 lflags |= LOOKUP_FOLLOW; 4792 if (flags & MOVE_MOUNT_T_AUTOMOUNTS) 4793 lflags |= LOOKUP_AUTOMOUNT; 4794 ret = filename_lookup(to_dfd, to_name, lflags, &to_path, NULL); 4795 if (ret) 4796 return ret; 4797 } 4798 4799 uflags = 0; 4800 if (flags & MOVE_MOUNT_F_EMPTY_PATH) 4801 uflags = AT_EMPTY_PATH; 4802 4803 CLASS(filename_maybe_null,from_name)(from_pathname, uflags); 4804 if (!from_name && from_dfd >= 0) { 4805 CLASS(fd_raw, f_from)(from_dfd); 4806 if (fd_empty(f_from)) 4807 return -EBADF; 4808 4809 return vfs_move_mount(&fd_file(f_from)->f_path, &to_path, mflags); 4810 } 4811 4812 lflags = 0; 4813 if (flags & MOVE_MOUNT_F_SYMLINKS) 4814 lflags |= LOOKUP_FOLLOW; 4815 if (flags & MOVE_MOUNT_F_AUTOMOUNTS) 4816 lflags |= LOOKUP_AUTOMOUNT; 4817 ret = filename_lookup(from_dfd, from_name, lflags, &from_path, NULL); 4818 if (ret) 4819 return ret; 4820 4821 return vfs_move_mount(&from_path, &to_path, mflags); 4822 } 4823 4824 /* 4825 * Return true if path is reachable from root 4826 * 4827 * locks: mount_locked_reader || namespace_shared && is_mounted(mnt) 4828 */ 4829 bool is_path_reachable(struct mount *mnt, struct dentry *dentry, 4830 const struct path *root) 4831 { 4832 while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) { 4833 dentry = mnt->mnt_mountpoint; 4834 mnt = mnt->mnt_parent; 4835 } 4836 return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry); 4837 } 4838 4839 bool path_is_under(const struct path *path1, const struct path *path2) 4840 { 4841 guard(mount_locked_reader)(); 4842 return is_path_reachable(real_mount(path1->mnt), path1->dentry, path2); 4843 } 4844 EXPORT_SYMBOL(path_is_under); 4845 4846 int path_pivot_root(struct path *new, struct path *old) 4847 { 4848 struct path root __free(path_put) = {}; 4849 struct mount *new_mnt, *root_mnt, *old_mnt, *root_parent, *ex_parent; 4850 int error; 4851 4852 if (!may_mount()) 4853 return -EPERM; 4854 4855 error = security_sb_pivotroot(old, new); 4856 if (error) 4857 return error; 4858 4859 get_fs_root(current->fs, &root); 4860 4861 LOCK_MOUNT(old_mp, old); 4862 old_mnt = old_mp.parent; 4863 if (IS_ERR(old_mnt)) 4864 return PTR_ERR(old_mnt); 4865 4866 new_mnt = real_mount(new->mnt); 4867 root_mnt = real_mount(root.mnt); 4868 /* only a mounted mount has a parent that namespace_sem pins */ 4869 if (!check_mnt(root_mnt) || !check_mnt(new_mnt)) 4870 return -EINVAL; 4871 ex_parent = new_mnt->mnt_parent; 4872 root_parent = root_mnt->mnt_parent; 4873 if (IS_MNT_SHARED(old_mnt) || 4874 IS_MNT_SHARED(ex_parent) || 4875 IS_MNT_SHARED(root_parent)) 4876 return -EINVAL; 4877 if (new_mnt->mnt.mnt_flags & MNT_LOCKED) 4878 return -EINVAL; 4879 if (d_unlinked(new->dentry)) 4880 return -ENOENT; 4881 if (new_mnt == root_mnt || old_mnt == root_mnt) 4882 return -EBUSY; /* loop, on the same file system */ 4883 if (!path_mounted(&root)) 4884 return -EINVAL; /* not a mountpoint */ 4885 if (!mnt_has_parent(root_mnt)) 4886 return -EINVAL; /* absolute root */ 4887 if (!path_mounted(new)) 4888 return -EINVAL; /* not a mountpoint */ 4889 if (!mnt_has_parent(new_mnt)) 4890 return -EINVAL; /* absolute root */ 4891 /* make sure we can reach put_old from new_root */ 4892 if (!is_path_reachable(old_mnt, old_mp.mp->m_dentry, new)) 4893 return -EINVAL; 4894 /* make certain new is below the root */ 4895 if (!is_path_reachable(new_mnt, new->dentry, &root)) 4896 return -EINVAL; 4897 lock_mount_hash(); 4898 umount_mnt(new_mnt); 4899 if (root_mnt->mnt.mnt_flags & MNT_LOCKED) { 4900 new_mnt->mnt.mnt_flags |= MNT_LOCKED; 4901 root_mnt->mnt.mnt_flags &= ~MNT_LOCKED; 4902 } 4903 /* mount new_root on / */ 4904 attach_mnt(new_mnt, root_parent, root_mnt->mnt_mp); 4905 umount_mnt(root_mnt); 4906 /* mount old root on put_old */ 4907 attach_mnt(root_mnt, old_mnt, old_mp.mp); 4908 touch_mnt_namespace(current->nsproxy->mnt_ns); 4909 /* A moved mount should not expire automatically */ 4910 list_del_init(&new_mnt->mnt_expire); 4911 unlock_mount_hash(); 4912 mnt_notify_add(root_mnt); 4913 mnt_notify_add(new_mnt); 4914 chroot_fs_refs(&root, new); 4915 return 0; 4916 } 4917 4918 /* 4919 * pivot_root Semantics: 4920 * Moves the root file system of the current process to the directory put_old, 4921 * makes new_root as the new root file system of the current process, and sets 4922 * root/cwd of all processes which had them on the current root to new_root. 4923 * 4924 * Restrictions: 4925 * The new_root and put_old must be directories, and must not be on the 4926 * same file system as the current process root. The put_old must be 4927 * underneath new_root, i.e. adding a non-zero number of /.. to the string 4928 * pointed to by put_old must yield the same directory as new_root. No other 4929 * file system may be mounted on put_old. After all, new_root is a mountpoint. 4930 * 4931 * The immutable nullfs filesystem is mounted as the true root of the VFS 4932 * hierarchy. The mutable rootfs (tmpfs/ramfs) is layered on top of this, 4933 * allowing pivot_root() to work normally from initramfs. 4934 * 4935 * Notes: 4936 * - we don't move root/cwd if they are not at the root (reason: if something 4937 * cared enough to change them, it's probably wrong to force them elsewhere) 4938 * - it's okay to pick a root that isn't the root of a file system, e.g. 4939 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint, 4940 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root 4941 * first. 4942 */ 4943 SYSCALL_DEFINE2(pivot_root, const char __user *, new_root, 4944 const char __user *, put_old) 4945 { 4946 struct path new __free(path_put) = {}; 4947 struct path old __free(path_put) = {}; 4948 int error; 4949 4950 error = user_path_at(AT_FDCWD, new_root, 4951 LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &new); 4952 if (error) 4953 return error; 4954 4955 error = user_path_at(AT_FDCWD, put_old, 4956 LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &old); 4957 if (error) 4958 return error; 4959 4960 return path_pivot_root(&new, &old); 4961 } 4962 4963 static unsigned int recalc_flags(struct mount_kattr *kattr, struct mount *mnt) 4964 { 4965 unsigned int flags = mnt->mnt.mnt_flags; 4966 4967 /* flags to clear */ 4968 flags &= ~kattr->attr_clr; 4969 /* flags to raise */ 4970 flags |= kattr->attr_set; 4971 4972 return flags; 4973 } 4974 4975 static int can_idmap_mount(const struct mount_kattr *kattr, struct mount *mnt) 4976 { 4977 struct vfsmount *m = &mnt->mnt; 4978 struct user_namespace *fs_userns = m->mnt_sb->s_user_ns; 4979 4980 if (!kattr->mnt_idmap) 4981 return 0; 4982 4983 /* 4984 * Creating an idmapped mount with the filesystem wide idmapping 4985 * doesn't make sense so block that. We don't allow mushy semantics. 4986 */ 4987 if (kattr->mnt_userns == m->mnt_sb->s_user_ns) 4988 return -EINVAL; 4989 4990 /* 4991 * We only allow an mount to change it's idmapping if it has 4992 * never been accessible to userspace. 4993 */ 4994 if (!(kattr->kflags & MOUNT_KATTR_IDMAP_REPLACE) && is_idmapped_mnt(m)) 4995 return -EPERM; 4996 4997 /* The underlying filesystem doesn't support idmapped mounts yet. */ 4998 if (!(m->mnt_sb->s_type->fs_flags & FS_ALLOW_IDMAP)) 4999 return -EINVAL; 5000 5001 /* The filesystem has turned off idmapped mounts. */ 5002 if (m->mnt_sb->s_iflags & SB_I_NOIDMAP) 5003 return -EINVAL; 5004 5005 /* We're not controlling the superblock. */ 5006 if (!ns_capable(fs_userns, CAP_SYS_ADMIN)) 5007 return -EPERM; 5008 5009 /* Mount has already been visible in the filesystem hierarchy. */ 5010 if (!is_anon_ns(mnt->mnt_ns)) 5011 return -EINVAL; 5012 5013 return 0; 5014 } 5015 5016 /** 5017 * mnt_allow_writers() - check whether the attribute change allows writers 5018 * @kattr: the new mount attributes 5019 * @mnt: the mount to which @kattr will be applied 5020 * 5021 * Check whether thew new mount attributes in @kattr allow concurrent writers. 5022 * 5023 * Return: true if writers need to be held, false if not 5024 */ 5025 static inline bool mnt_allow_writers(const struct mount_kattr *kattr, 5026 const struct mount *mnt) 5027 { 5028 return (!(kattr->attr_set & MNT_READONLY) || 5029 (mnt->mnt.mnt_flags & MNT_READONLY)) && 5030 !kattr->mnt_idmap; 5031 } 5032 5033 static int mount_setattr_prepare(struct mount_kattr *kattr, struct mount *mnt) 5034 { 5035 struct mount *m; 5036 int err; 5037 5038 for (m = mnt; m; m = next_mnt(m, mnt)) { 5039 if (!can_change_locked_flags(m, recalc_flags(kattr, m))) { 5040 err = -EPERM; 5041 break; 5042 } 5043 5044 err = can_idmap_mount(kattr, m); 5045 if (err) 5046 break; 5047 5048 if (!mnt_allow_writers(kattr, m)) { 5049 err = mnt_hold_writers(m); 5050 if (err) { 5051 m = next_mnt(m, mnt); 5052 break; 5053 } 5054 } 5055 5056 if (!(kattr->kflags & MOUNT_KATTR_RECURSE)) 5057 return 0; 5058 } 5059 5060 if (err) { 5061 /* undo all mnt_hold_writers() we'd done */ 5062 for (struct mount *p = mnt; p != m; p = next_mnt(p, mnt)) 5063 mnt_unhold_writers(p); 5064 } 5065 return err; 5066 } 5067 5068 static void do_idmap_mount(const struct mount_kattr *kattr, struct mount *mnt) 5069 { 5070 const struct mnt_idmap *old_idmap; 5071 5072 if (!kattr->mnt_idmap) 5073 return; 5074 5075 old_idmap = mnt_idmap(&mnt->mnt); 5076 5077 /* Pairs with smp_load_acquire() in mnt_idmap(). */ 5078 smp_store_release(&mnt->mnt.mnt_idmap, mnt_idmap_get(kattr->mnt_idmap)); 5079 mnt_idmap_put(old_idmap); 5080 } 5081 5082 static void mount_setattr_commit(struct mount_kattr *kattr, struct mount *mnt) 5083 { 5084 struct mount *m; 5085 5086 for (m = mnt; m; m = next_mnt(m, mnt)) { 5087 unsigned int flags; 5088 5089 do_idmap_mount(kattr, m); 5090 flags = recalc_flags(kattr, m); 5091 WRITE_ONCE(m->mnt.mnt_flags, flags); 5092 5093 /* If we had to hold writers unblock them. */ 5094 mnt_unhold_writers(m); 5095 5096 if (kattr->propagation) 5097 change_mnt_propagation(m, kattr->propagation); 5098 if (!(kattr->kflags & MOUNT_KATTR_RECURSE)) 5099 break; 5100 } 5101 touch_mnt_namespace(mnt->mnt_ns); 5102 } 5103 5104 static int do_mount_setattr(const struct path *path, struct mount_kattr *kattr) 5105 { 5106 struct mount *mnt = real_mount(path->mnt); 5107 int err = 0; 5108 5109 if (!path_mounted(path)) 5110 return -EINVAL; 5111 5112 if (kattr->mnt_userns) { 5113 const struct mnt_idmap *mnt_idmap; 5114 5115 mnt_idmap = alloc_mnt_idmap(kattr->mnt_userns); 5116 if (IS_ERR(mnt_idmap)) 5117 return PTR_ERR(mnt_idmap); 5118 kattr->mnt_idmap = mnt_idmap; 5119 } 5120 5121 if (kattr->propagation) { 5122 /* 5123 * Only take namespace_lock() if we're actually changing 5124 * propagation. 5125 */ 5126 namespace_lock(); 5127 /* invent_group_ids() walks the tree, only walk a mounted one */ 5128 if (!anon_ns_root(mnt) && !check_mnt(mnt)) { 5129 namespace_unlock(); 5130 return -EINVAL; 5131 } 5132 if (kattr->propagation == MS_SHARED) { 5133 err = invent_group_ids(mnt, kattr->kflags & MOUNT_KATTR_RECURSE); 5134 if (err) { 5135 namespace_unlock(); 5136 return err; 5137 } 5138 } 5139 } 5140 5141 err = -EINVAL; 5142 lock_mount_hash(); 5143 5144 /* Checked under namespace_sem already if the propagation changes. */ 5145 if (!anon_ns_root(mnt) && !check_mnt(mnt)) 5146 goto out; 5147 5148 /* 5149 * First, we get the mount tree in a shape where we can change mount 5150 * properties without failure. If we succeeded to do so we commit all 5151 * changes and if we failed we clean up. 5152 */ 5153 err = mount_setattr_prepare(kattr, mnt); 5154 if (!err) 5155 mount_setattr_commit(kattr, mnt); 5156 5157 out: 5158 unlock_mount_hash(); 5159 5160 if (kattr->propagation) { 5161 if (err) 5162 cleanup_group_ids(mnt, NULL); 5163 namespace_unlock(); 5164 } 5165 5166 return err; 5167 } 5168 5169 static int build_mount_idmapped(const struct mount_attr *attr, size_t usize, 5170 struct mount_kattr *kattr) 5171 { 5172 struct ns_common *ns; 5173 struct user_namespace *mnt_userns; 5174 5175 if (!((attr->attr_set | attr->attr_clr) & MOUNT_ATTR_IDMAP)) 5176 return 0; 5177 5178 if (attr->attr_clr & MOUNT_ATTR_IDMAP) { 5179 /* 5180 * We can only remove an idmapping if it's never been 5181 * exposed to userspace. 5182 */ 5183 if (!(kattr->kflags & MOUNT_KATTR_IDMAP_REPLACE)) 5184 return -EINVAL; 5185 5186 /* 5187 * Removal of idmappings is equivalent to setting 5188 * nop_mnt_idmap. 5189 */ 5190 if (!(attr->attr_set & MOUNT_ATTR_IDMAP)) { 5191 kattr->mnt_idmap = &nop_mnt_idmap; 5192 return 0; 5193 } 5194 } 5195 5196 if (attr->userns_fd > INT_MAX) 5197 return -EINVAL; 5198 5199 CLASS(fd, f)(attr->userns_fd); 5200 if (fd_empty(f)) 5201 return -EBADF; 5202 5203 if (!proc_ns_file(fd_file(f))) 5204 return -EINVAL; 5205 5206 ns = get_proc_ns(file_inode(fd_file(f))); 5207 if (ns->ns_type != CLONE_NEWUSER) 5208 return -EINVAL; 5209 5210 /* 5211 * The initial idmapping cannot be used to create an idmapped 5212 * mount. We use the initial idmapping as an indicator of a mount 5213 * that is not idmapped. It can simply be passed into helpers that 5214 * are aware of idmapped mounts as a convenient shortcut. A user 5215 * can just create a dedicated identity mapping to achieve the same 5216 * result. 5217 */ 5218 mnt_userns = container_of(ns, struct user_namespace, ns); 5219 if (mnt_userns == &init_user_ns) 5220 return -EPERM; 5221 5222 /* We're not controlling the target namespace. */ 5223 if (!ns_capable(mnt_userns, CAP_SYS_ADMIN)) 5224 return -EPERM; 5225 5226 kattr->mnt_userns = get_user_ns(mnt_userns); 5227 return 0; 5228 } 5229 5230 static int build_mount_kattr(const struct mount_attr *attr, size_t usize, 5231 struct mount_kattr *kattr) 5232 { 5233 if (attr->propagation & ~MOUNT_SETATTR_PROPAGATION_FLAGS) 5234 return -EINVAL; 5235 if (hweight32(attr->propagation & MOUNT_SETATTR_PROPAGATION_FLAGS) > 1) 5236 return -EINVAL; 5237 kattr->propagation = attr->propagation; 5238 5239 if ((attr->attr_set | attr->attr_clr) & ~MOUNT_SETATTR_VALID_FLAGS) 5240 return -EINVAL; 5241 5242 kattr->attr_set = attr_flags_to_mnt_flags(attr->attr_set); 5243 kattr->attr_clr = attr_flags_to_mnt_flags(attr->attr_clr); 5244 5245 /* 5246 * Since the MOUNT_ATTR_<atime> values are an enum, not a bitmap, 5247 * users wanting to transition to a different atime setting cannot 5248 * simply specify the atime setting in @attr_set, but must also 5249 * specify MOUNT_ATTR__ATIME in the @attr_clr field. 5250 * So ensure that MOUNT_ATTR__ATIME can't be partially set in 5251 * @attr_clr and that @attr_set can't have any atime bits set if 5252 * MOUNT_ATTR__ATIME isn't set in @attr_clr. 5253 */ 5254 if (attr->attr_clr & MOUNT_ATTR__ATIME) { 5255 if ((attr->attr_clr & MOUNT_ATTR__ATIME) != MOUNT_ATTR__ATIME) 5256 return -EINVAL; 5257 5258 /* 5259 * Clear all previous time settings as they are mutually 5260 * exclusive. 5261 */ 5262 kattr->attr_clr |= MNT_RELATIME | MNT_NOATIME; 5263 switch (attr->attr_set & MOUNT_ATTR__ATIME) { 5264 case MOUNT_ATTR_RELATIME: 5265 kattr->attr_set |= MNT_RELATIME; 5266 break; 5267 case MOUNT_ATTR_NOATIME: 5268 kattr->attr_set |= MNT_NOATIME; 5269 break; 5270 case MOUNT_ATTR_STRICTATIME: 5271 break; 5272 default: 5273 return -EINVAL; 5274 } 5275 } else { 5276 if (attr->attr_set & MOUNT_ATTR__ATIME) 5277 return -EINVAL; 5278 } 5279 5280 return build_mount_idmapped(attr, usize, kattr); 5281 } 5282 5283 static void finish_mount_kattr(struct mount_kattr *kattr) 5284 { 5285 if (kattr->mnt_userns) { 5286 put_user_ns(kattr->mnt_userns); 5287 kattr->mnt_userns = NULL; 5288 } 5289 5290 if (kattr->mnt_idmap) 5291 mnt_idmap_put(kattr->mnt_idmap); 5292 } 5293 5294 static int wants_mount_setattr(struct mount_attr __user *uattr, size_t usize, 5295 struct mount_kattr *kattr) 5296 { 5297 int ret; 5298 struct mount_attr attr; 5299 5300 BUILD_BUG_ON(sizeof(struct mount_attr) != MOUNT_ATTR_SIZE_VER0); 5301 5302 if (unlikely(usize > PAGE_SIZE)) 5303 return -E2BIG; 5304 if (unlikely(usize < MOUNT_ATTR_SIZE_VER0)) 5305 return -EINVAL; 5306 5307 if (!may_mount()) 5308 return -EPERM; 5309 5310 ret = copy_struct_from_user(&attr, sizeof(attr), uattr, usize); 5311 if (ret) 5312 return ret; 5313 5314 /* Don't bother walking through the mounts if this is a nop. */ 5315 if (attr.attr_set == 0 && 5316 attr.attr_clr == 0 && 5317 attr.propagation == 0) 5318 return 0; /* Tell caller to not bother. */ 5319 5320 ret = build_mount_kattr(&attr, usize, kattr); 5321 if (ret < 0) 5322 return ret; 5323 5324 return 1; 5325 } 5326 5327 SYSCALL_DEFINE5(mount_setattr, int, dfd, const char __user *, path, 5328 unsigned int, flags, struct mount_attr __user *, uattr, 5329 size_t, usize) 5330 { 5331 int err; 5332 struct path target; 5333 struct mount_kattr kattr; 5334 unsigned int lookup_flags = LOOKUP_AUTOMOUNT | LOOKUP_FOLLOW; 5335 5336 if (flags & ~(AT_EMPTY_PATH | 5337 AT_RECURSIVE | 5338 AT_SYMLINK_NOFOLLOW | 5339 AT_NO_AUTOMOUNT)) 5340 return -EINVAL; 5341 5342 if (flags & AT_NO_AUTOMOUNT) 5343 lookup_flags &= ~LOOKUP_AUTOMOUNT; 5344 if (flags & AT_SYMLINK_NOFOLLOW) 5345 lookup_flags &= ~LOOKUP_FOLLOW; 5346 5347 kattr = (struct mount_kattr) { 5348 .lookup_flags = lookup_flags, 5349 }; 5350 5351 if (flags & AT_RECURSIVE) 5352 kattr.kflags |= MOUNT_KATTR_RECURSE; 5353 5354 err = wants_mount_setattr(uattr, usize, &kattr); 5355 if (err <= 0) 5356 return err; 5357 5358 CLASS(filename_uflags, name)(path, flags); 5359 err = filename_lookup(dfd, name, kattr.lookup_flags, &target, NULL); 5360 if (!err) { 5361 err = do_mount_setattr(&target, &kattr); 5362 path_put(&target); 5363 } 5364 finish_mount_kattr(&kattr); 5365 return err; 5366 } 5367 5368 SYSCALL_DEFINE5(open_tree_attr, int, dfd, const char __user *, filename, 5369 unsigned, flags, struct mount_attr __user *, uattr, 5370 size_t, usize) 5371 { 5372 if (!uattr && usize) 5373 return -EINVAL; 5374 5375 FD_PREPARE(fdf, flags, vfs_open_tree(dfd, filename, flags)); 5376 if (fdf->fd < 0) 5377 return fdf->fd; 5378 5379 if (uattr) { 5380 struct mount_kattr kattr = {}; 5381 struct file *file = fdf->file; 5382 int ret; 5383 5384 if (flags & OPEN_TREE_CLONE) 5385 kattr.kflags = MOUNT_KATTR_IDMAP_REPLACE; 5386 if (flags & AT_RECURSIVE) 5387 kattr.kflags |= MOUNT_KATTR_RECURSE; 5388 5389 ret = wants_mount_setattr(uattr, usize, &kattr); 5390 if (ret > 0) { 5391 ret = do_mount_setattr(&file->f_path, &kattr); 5392 finish_mount_kattr(&kattr); 5393 } 5394 if (ret) 5395 return ret; 5396 } 5397 5398 return fd_publish(fdf); 5399 } 5400 5401 int show_path(struct seq_file *m, struct dentry *root) 5402 { 5403 if (root->d_sb->s_op->show_path) 5404 return root->d_sb->s_op->show_path(m, root); 5405 5406 seq_dentry(m, root, " \t\n\\"); 5407 return 0; 5408 } 5409 5410 static struct vfsmount *lookup_mnt_in_ns(u64 id, struct mnt_namespace *ns) 5411 { 5412 struct mount *mnt = mnt_find_id_at(ns, id); 5413 5414 if (!mnt || mnt->mnt_id_unique != id) 5415 return NULL; 5416 5417 return &mnt->mnt; 5418 } 5419 5420 struct kstatmount { 5421 struct statmount __user *buf; 5422 size_t bufsize; 5423 struct vfsmount *mnt; 5424 const struct mnt_idmap *idmap; 5425 u64 mask; 5426 struct path root; 5427 struct seq_file seq; 5428 5429 /* Must be last --ends in a flexible-array member. */ 5430 struct statmount sm; 5431 }; 5432 5433 static u64 mnt_to_attr_flags(struct vfsmount *mnt) 5434 { 5435 unsigned int mnt_flags = READ_ONCE(mnt->mnt_flags); 5436 u64 attr_flags = 0; 5437 5438 if (mnt_flags & MNT_READONLY) 5439 attr_flags |= MOUNT_ATTR_RDONLY; 5440 if (mnt_flags & MNT_NOSUID) 5441 attr_flags |= MOUNT_ATTR_NOSUID; 5442 if (mnt_flags & MNT_NODEV) 5443 attr_flags |= MOUNT_ATTR_NODEV; 5444 if (mnt_flags & MNT_NOEXEC) 5445 attr_flags |= MOUNT_ATTR_NOEXEC; 5446 if (mnt_flags & MNT_NODIRATIME) 5447 attr_flags |= MOUNT_ATTR_NODIRATIME; 5448 if (mnt_flags & MNT_NOSYMFOLLOW) 5449 attr_flags |= MOUNT_ATTR_NOSYMFOLLOW; 5450 5451 if (mnt_flags & MNT_NOATIME) 5452 attr_flags |= MOUNT_ATTR_NOATIME; 5453 else if (mnt_flags & MNT_RELATIME) 5454 attr_flags |= MOUNT_ATTR_RELATIME; 5455 else 5456 attr_flags |= MOUNT_ATTR_STRICTATIME; 5457 5458 if (is_idmapped_mnt(mnt)) 5459 attr_flags |= MOUNT_ATTR_IDMAP; 5460 5461 return attr_flags; 5462 } 5463 5464 static u64 mnt_to_propagation_flags(struct mount *m) 5465 { 5466 u64 propagation = 0; 5467 5468 if (IS_MNT_SHARED(m)) 5469 propagation |= MS_SHARED; 5470 if (IS_MNT_SLAVE(m)) 5471 propagation |= MS_SLAVE; 5472 if (IS_MNT_UNBINDABLE(m)) 5473 propagation |= MS_UNBINDABLE; 5474 if (!propagation) 5475 propagation |= MS_PRIVATE; 5476 5477 return propagation; 5478 } 5479 5480 u64 vfsmount_to_propagation_flags(struct vfsmount *mnt) 5481 { 5482 return mnt_to_propagation_flags(real_mount(mnt)); 5483 } 5484 EXPORT_SYMBOL_GPL(vfsmount_to_propagation_flags); 5485 5486 static void statmount_sb_basic(struct kstatmount *s) 5487 { 5488 struct super_block *sb = s->mnt->mnt_sb; 5489 5490 s->sm.mask |= STATMOUNT_SB_BASIC; 5491 s->sm.sb_dev_major = MAJOR(sb->s_dev); 5492 s->sm.sb_dev_minor = MINOR(sb->s_dev); 5493 s->sm.sb_magic = sb->s_magic; 5494 s->sm.sb_flags = sb->s_flags & (SB_RDONLY|SB_SYNCHRONOUS|SB_DIRSYNC|SB_LAZYTIME); 5495 } 5496 5497 static void statmount_mnt_parent(struct kstatmount *s, const struct mount *m) 5498 { 5499 s->sm.mnt_parent_id = m->mnt_parent->mnt_id_unique; 5500 s->sm.mnt_parent_id_old = m->mnt_parent->mnt_id; 5501 } 5502 5503 static void statmount_mnt_basic(struct kstatmount *s) 5504 { 5505 struct mount *m = real_mount(s->mnt); 5506 5507 s->sm.mask |= STATMOUNT_MNT_BASIC; 5508 s->sm.mnt_id = m->mnt_id_unique; 5509 s->sm.mnt_id_old = m->mnt_id; 5510 /* An unmounted mount is cut loose from its parent under mount_lock alone. */ 5511 if (likely(is_mounted(s->mnt))) 5512 statmount_mnt_parent(s, m); 5513 else 5514 scoped_guard(mount_locked_reader) 5515 statmount_mnt_parent(s, m); 5516 s->sm.mnt_attr = mnt_to_attr_flags(&m->mnt); 5517 s->sm.mnt_propagation = mnt_to_propagation_flags(m); 5518 s->sm.mnt_peer_group = m->mnt_group_id; 5519 s->sm.mnt_master = IS_MNT_SLAVE(m) ? m->mnt_master->mnt_group_id : 0; 5520 } 5521 5522 static void statmount_propagate_from(struct kstatmount *s) 5523 { 5524 struct mount *m = real_mount(s->mnt); 5525 5526 s->sm.mask |= STATMOUNT_PROPAGATE_FROM; 5527 if (IS_MNT_SLAVE(m)) 5528 s->sm.propagate_from = get_dominating_id(m, ¤t->fs->root); 5529 } 5530 5531 static int statmount_mnt_root(struct kstatmount *s, struct seq_file *seq) 5532 { 5533 int ret; 5534 size_t start = seq->count; 5535 5536 ret = show_path(seq, s->mnt->mnt_root); 5537 if (ret) 5538 return ret; 5539 5540 if (unlikely(seq_has_overflowed(seq))) 5541 return -EAGAIN; 5542 5543 /* 5544 * Unescape the result. It would be better if supplied string was not 5545 * escaped in the first place, but that's a pretty invasive change. 5546 */ 5547 seq->buf[seq->count] = '\0'; 5548 seq->count = start; 5549 seq_commit(seq, string_unescape_inplace(seq->buf + start, UNESCAPE_OCTAL)); 5550 return 0; 5551 } 5552 5553 static int statmount_mnt_point(struct kstatmount *s, struct seq_file *seq) 5554 { 5555 struct vfsmount *mnt = s->mnt; 5556 struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt }; 5557 int err; 5558 5559 err = seq_path_root(seq, &mnt_path, &s->root, ""); 5560 return err == SEQ_SKIP ? 0 : err; 5561 } 5562 5563 static int statmount_fs_type(struct kstatmount *s, struct seq_file *seq) 5564 { 5565 struct super_block *sb = s->mnt->mnt_sb; 5566 5567 seq_puts(seq, sb->s_type->name); 5568 return 0; 5569 } 5570 5571 static void statmount_fs_subtype(struct kstatmount *s, struct seq_file *seq) 5572 { 5573 struct super_block *sb = s->mnt->mnt_sb; 5574 5575 if (sb->s_subtype) 5576 seq_puts(seq, sb->s_subtype); 5577 } 5578 5579 static int statmount_sb_source(struct kstatmount *s, struct seq_file *seq) 5580 { 5581 struct super_block *sb = s->mnt->mnt_sb; 5582 struct mount *r = real_mount(s->mnt); 5583 5584 if (sb->s_op->show_devname) { 5585 size_t start = seq->count; 5586 int ret; 5587 5588 ret = sb->s_op->show_devname(seq, s->mnt->mnt_root); 5589 if (ret) 5590 return ret; 5591 5592 if (unlikely(seq_has_overflowed(seq))) 5593 return -EAGAIN; 5594 5595 /* Unescape the result */ 5596 seq->buf[seq->count] = '\0'; 5597 seq->count = start; 5598 seq_commit(seq, string_unescape_inplace(seq->buf + start, UNESCAPE_OCTAL)); 5599 } else { 5600 seq_puts(seq, r->mnt_devname); 5601 } 5602 return 0; 5603 } 5604 5605 static void statmount_mnt_ns_id(struct kstatmount *s, struct mnt_namespace *ns) 5606 { 5607 s->sm.mask |= STATMOUNT_MNT_NS_ID; 5608 s->sm.mnt_ns_id = ns->ns.ns_id; 5609 } 5610 5611 static int statmount_mnt_opts(struct kstatmount *s, struct seq_file *seq) 5612 { 5613 struct vfsmount *mnt = s->mnt; 5614 struct super_block *sb = mnt->mnt_sb; 5615 size_t start = seq->count; 5616 int err; 5617 5618 err = security_sb_show_options(seq, sb); 5619 if (err) 5620 return err; 5621 5622 if (sb->s_op->show_options) { 5623 err = sb->s_op->show_options(seq, mnt->mnt_root); 5624 if (err) 5625 return err; 5626 } 5627 5628 if (unlikely(seq_has_overflowed(seq))) 5629 return -EAGAIN; 5630 5631 if (seq->count == start) 5632 return 0; 5633 5634 /* skip leading comma */ 5635 memmove(seq->buf + start, seq->buf + start + 1, 5636 seq->count - start - 1); 5637 seq->count--; 5638 5639 return 0; 5640 } 5641 5642 static inline int statmount_opt_process(struct seq_file *seq, size_t start) 5643 { 5644 char *buf_end, *opt_end, *src, *dst; 5645 int count = 0; 5646 5647 if (unlikely(seq_has_overflowed(seq))) 5648 return -EAGAIN; 5649 5650 buf_end = seq->buf + seq->count; 5651 dst = seq->buf + start; 5652 src = dst + 1; /* skip initial comma */ 5653 5654 if (src >= buf_end) { 5655 seq->count = start; 5656 return 0; 5657 } 5658 5659 *buf_end = '\0'; 5660 for (; src < buf_end; src = opt_end + 1) { 5661 opt_end = strchrnul(src, ','); 5662 *opt_end = '\0'; 5663 dst += string_unescape(src, dst, 0, UNESCAPE_OCTAL) + 1; 5664 if (WARN_ON_ONCE(++count == INT_MAX)) 5665 return -EOVERFLOW; 5666 } 5667 seq->count = dst - 1 - seq->buf; 5668 return count; 5669 } 5670 5671 static int statmount_opt_array(struct kstatmount *s, struct seq_file *seq) 5672 { 5673 struct vfsmount *mnt = s->mnt; 5674 struct super_block *sb = mnt->mnt_sb; 5675 size_t start = seq->count; 5676 int err; 5677 5678 if (!sb->s_op->show_options) 5679 return 0; 5680 5681 err = sb->s_op->show_options(seq, mnt->mnt_root); 5682 if (err) 5683 return err; 5684 5685 err = statmount_opt_process(seq, start); 5686 if (err < 0) 5687 return err; 5688 5689 s->sm.opt_num = err; 5690 return 0; 5691 } 5692 5693 static int statmount_opt_sec_array(struct kstatmount *s, struct seq_file *seq) 5694 { 5695 struct vfsmount *mnt = s->mnt; 5696 struct super_block *sb = mnt->mnt_sb; 5697 size_t start = seq->count; 5698 int err; 5699 5700 err = security_sb_show_options(seq, sb); 5701 if (err) 5702 return err; 5703 5704 err = statmount_opt_process(seq, start); 5705 if (err < 0) 5706 return err; 5707 5708 s->sm.opt_sec_num = err; 5709 return 0; 5710 } 5711 5712 static inline int statmount_mnt_uidmap(struct kstatmount *s, struct seq_file *seq) 5713 { 5714 int ret; 5715 5716 ret = statmount_mnt_idmap(s->idmap, seq, true); 5717 if (ret < 0) 5718 return ret; 5719 5720 s->sm.mnt_uidmap_num = ret; 5721 /* 5722 * Always raise STATMOUNT_MNT_UIDMAP even if there are no valid 5723 * mappings. This allows userspace to distinguish between a 5724 * non-idmapped mount and an idmapped mount where none of the 5725 * individual mappings are valid in the caller's idmapping. 5726 */ 5727 if (is_valid_mnt_idmap(s->idmap)) 5728 s->sm.mask |= STATMOUNT_MNT_UIDMAP; 5729 return 0; 5730 } 5731 5732 static inline int statmount_mnt_gidmap(struct kstatmount *s, struct seq_file *seq) 5733 { 5734 int ret; 5735 5736 ret = statmount_mnt_idmap(s->idmap, seq, false); 5737 if (ret < 0) 5738 return ret; 5739 5740 s->sm.mnt_gidmap_num = ret; 5741 /* 5742 * Always raise STATMOUNT_MNT_GIDMAP even if there are no valid 5743 * mappings. This allows userspace to distinguish between a 5744 * non-idmapped mount and an idmapped mount where none of the 5745 * individual mappings are valid in the caller's idmapping. 5746 */ 5747 if (is_valid_mnt_idmap(s->idmap)) 5748 s->sm.mask |= STATMOUNT_MNT_GIDMAP; 5749 return 0; 5750 } 5751 5752 static int statmount_string(struct kstatmount *s, u64 flag) 5753 { 5754 int ret = 0; 5755 size_t kbufsize; 5756 struct seq_file *seq = &s->seq; 5757 struct statmount *sm = &s->sm; 5758 u32 start, *offp; 5759 5760 /* Reserve an empty string at the beginning for any unset offsets */ 5761 if (!seq->count) 5762 seq_putc(seq, 0); 5763 5764 start = seq->count; 5765 5766 switch (flag) { 5767 case STATMOUNT_FS_TYPE: 5768 offp = &sm->fs_type; 5769 ret = statmount_fs_type(s, seq); 5770 break; 5771 case STATMOUNT_MNT_ROOT: 5772 offp = &sm->mnt_root; 5773 ret = statmount_mnt_root(s, seq); 5774 break; 5775 case STATMOUNT_MNT_POINT: 5776 offp = &sm->mnt_point; 5777 ret = statmount_mnt_point(s, seq); 5778 break; 5779 case STATMOUNT_MNT_OPTS: 5780 offp = &sm->mnt_opts; 5781 ret = statmount_mnt_opts(s, seq); 5782 break; 5783 case STATMOUNT_OPT_ARRAY: 5784 offp = &sm->opt_array; 5785 ret = statmount_opt_array(s, seq); 5786 break; 5787 case STATMOUNT_OPT_SEC_ARRAY: 5788 offp = &sm->opt_sec_array; 5789 ret = statmount_opt_sec_array(s, seq); 5790 break; 5791 case STATMOUNT_FS_SUBTYPE: 5792 offp = &sm->fs_subtype; 5793 statmount_fs_subtype(s, seq); 5794 break; 5795 case STATMOUNT_SB_SOURCE: 5796 offp = &sm->sb_source; 5797 ret = statmount_sb_source(s, seq); 5798 break; 5799 case STATMOUNT_MNT_UIDMAP: 5800 offp = &sm->mnt_uidmap; 5801 ret = statmount_mnt_uidmap(s, seq); 5802 break; 5803 case STATMOUNT_MNT_GIDMAP: 5804 offp = &sm->mnt_gidmap; 5805 ret = statmount_mnt_gidmap(s, seq); 5806 break; 5807 default: 5808 WARN_ON_ONCE(true); 5809 return -EINVAL; 5810 } 5811 5812 /* 5813 * If nothing was emitted, return to avoid setting the flag 5814 * and terminating the buffer. 5815 */ 5816 if (seq->count == start) 5817 return ret; 5818 if (unlikely(check_add_overflow(sizeof(*sm), seq->count, &kbufsize))) 5819 return -EOVERFLOW; 5820 if (kbufsize >= s->bufsize) 5821 return -EOVERFLOW; 5822 5823 /* signal a retry */ 5824 if (unlikely(seq_has_overflowed(seq))) 5825 return -EAGAIN; 5826 5827 if (ret) 5828 return ret; 5829 5830 seq->buf[seq->count++] = '\0'; 5831 sm->mask |= flag; 5832 *offp = start; 5833 return 0; 5834 } 5835 5836 static int copy_statmount_to_user(struct kstatmount *s) 5837 { 5838 struct statmount *sm = &s->sm; 5839 struct seq_file *seq = &s->seq; 5840 char __user *str = ((char __user *)s->buf) + sizeof(*sm); 5841 size_t copysize = min_t(size_t, s->bufsize, sizeof(*sm)); 5842 5843 if (seq->count && copy_to_user(str, seq->buf, seq->count)) 5844 return -EFAULT; 5845 5846 /* Return the number of bytes copied to the buffer */ 5847 sm->size = copysize + seq->count; 5848 if (copy_to_user(s->buf, sm, copysize)) 5849 return -EFAULT; 5850 5851 return 0; 5852 } 5853 5854 static struct mount *listmnt_next(struct mount *curr, bool reverse) 5855 { 5856 struct rb_node *node; 5857 5858 if (reverse) 5859 node = rb_prev(&curr->mnt_node); 5860 else 5861 node = rb_next(&curr->mnt_node); 5862 5863 return node_to_mount(node); 5864 } 5865 5866 static int grab_requested_root(struct mnt_namespace *ns, struct path *root) 5867 { 5868 struct mount *first, *child; 5869 5870 rwsem_assert_held(&namespace_sem); 5871 5872 /* We're looking at our own ns, just use get_fs_root. */ 5873 if (ns == current->nsproxy->mnt_ns) { 5874 get_fs_root(current->fs, root); 5875 return 0; 5876 } 5877 5878 /* 5879 * We have to find the first mount in our ns and use that, however it 5880 * may not exist, so handle that properly. 5881 */ 5882 if (mnt_ns_empty(ns)) 5883 return -ENOENT; 5884 5885 first = ns->root; 5886 for (child = node_to_mount(ns->mnt_first_node); child; 5887 child = listmnt_next(child, false)) { 5888 if (child != first && child->mnt_parent == first) 5889 break; 5890 } 5891 if (!child) 5892 return -ENOENT; 5893 5894 root->mnt = mntget(&child->mnt); 5895 root->dentry = dget(root->mnt->mnt_root); 5896 return 0; 5897 } 5898 5899 /* This must be updated whenever a new flag is added */ 5900 #define STATMOUNT_SUPPORTED (STATMOUNT_SB_BASIC | \ 5901 STATMOUNT_MNT_BASIC | \ 5902 STATMOUNT_PROPAGATE_FROM | \ 5903 STATMOUNT_MNT_ROOT | \ 5904 STATMOUNT_MNT_POINT | \ 5905 STATMOUNT_FS_TYPE | \ 5906 STATMOUNT_MNT_NS_ID | \ 5907 STATMOUNT_MNT_OPTS | \ 5908 STATMOUNT_FS_SUBTYPE | \ 5909 STATMOUNT_SB_SOURCE | \ 5910 STATMOUNT_OPT_ARRAY | \ 5911 STATMOUNT_OPT_SEC_ARRAY | \ 5912 STATMOUNT_SUPPORTED_MASK | \ 5913 STATMOUNT_MNT_UIDMAP | \ 5914 STATMOUNT_MNT_GIDMAP) 5915 5916 /* locks: namespace_shared */ 5917 static int do_statmount(struct kstatmount *s, u64 mnt_id, u64 mnt_ns_id, 5918 struct file *mnt_file, struct mnt_namespace *ns) 5919 { 5920 int err; 5921 5922 if (mnt_file) { 5923 WARN_ON_ONCE(ns != NULL); 5924 5925 s->mnt = mnt_file->f_path.mnt; 5926 ns = real_mount(s->mnt)->mnt_ns; 5927 if (IS_ERR(ns)) 5928 return PTR_ERR(ns); 5929 if (!ns) 5930 /* 5931 * We can't set mount point and mnt_ns_id since we don't have a 5932 * ns for the mount. This can happen if the mount is unmounted 5933 * with MNT_DETACH. 5934 */ 5935 s->mask &= ~(STATMOUNT_MNT_POINT | STATMOUNT_MNT_NS_ID); 5936 } else { 5937 /* Has the namespace already been emptied? */ 5938 if (mnt_ns_id && mnt_ns_empty(ns)) 5939 return -ENOENT; 5940 5941 s->mnt = lookup_mnt_in_ns(mnt_id, ns); 5942 if (!s->mnt) 5943 return -ENOENT; 5944 } 5945 5946 if (ns) { 5947 err = grab_requested_root(ns, &s->root); 5948 if (err) 5949 return err; 5950 5951 if (!mnt_file) { 5952 struct mount *m; 5953 /* 5954 * Don't trigger audit denials. We just want to determine what 5955 * mounts to show users. 5956 */ 5957 m = real_mount(s->mnt); 5958 if (!is_path_reachable(m, m->mnt.mnt_root, &s->root) && 5959 !ns_capable_noaudit(ns->user_ns, CAP_SYS_ADMIN)) 5960 return -EPERM; 5961 } 5962 } 5963 5964 err = security_sb_statfs(s->mnt->mnt_root); 5965 if (err) 5966 return err; 5967 5968 /* 5969 * Note that mount properties in mnt->mnt_flags, mnt->mnt_idmap 5970 * can change concurrently as we only hold the read-side of the 5971 * namespace semaphore and mount properties may change with only 5972 * the mount lock held. 5973 * 5974 * We could sample the mount lock sequence counter to detect 5975 * those changes and retry. But it's not worth it. Worst that 5976 * happens is that the mnt->mnt_idmap pointer is already changed 5977 * while mnt->mnt_flags isn't or vica versa. So what. 5978 * 5979 * Both mnt->mnt_flags and mnt->mnt_idmap are set and retrieved 5980 * via READ_ONCE()/WRITE_ONCE() and guard against theoretical 5981 * torn read/write. That's all we care about right now. 5982 */ 5983 s->idmap = mnt_idmap(s->mnt); 5984 if (s->mask & STATMOUNT_MNT_BASIC) 5985 statmount_mnt_basic(s); 5986 5987 if (s->mask & STATMOUNT_SB_BASIC) 5988 statmount_sb_basic(s); 5989 5990 if (s->mask & STATMOUNT_PROPAGATE_FROM) 5991 statmount_propagate_from(s); 5992 5993 if (s->mask & STATMOUNT_FS_TYPE) 5994 err = statmount_string(s, STATMOUNT_FS_TYPE); 5995 5996 if (!err && s->mask & STATMOUNT_MNT_ROOT) 5997 err = statmount_string(s, STATMOUNT_MNT_ROOT); 5998 5999 if (!err && s->mask & STATMOUNT_MNT_POINT) 6000 err = statmount_string(s, STATMOUNT_MNT_POINT); 6001 6002 if (!err && s->mask & STATMOUNT_MNT_OPTS) 6003 err = statmount_string(s, STATMOUNT_MNT_OPTS); 6004 6005 if (!err && s->mask & STATMOUNT_OPT_ARRAY) 6006 err = statmount_string(s, STATMOUNT_OPT_ARRAY); 6007 6008 if (!err && s->mask & STATMOUNT_OPT_SEC_ARRAY) 6009 err = statmount_string(s, STATMOUNT_OPT_SEC_ARRAY); 6010 6011 if (!err && s->mask & STATMOUNT_FS_SUBTYPE) 6012 err = statmount_string(s, STATMOUNT_FS_SUBTYPE); 6013 6014 if (!err && s->mask & STATMOUNT_SB_SOURCE) 6015 err = statmount_string(s, STATMOUNT_SB_SOURCE); 6016 6017 if (!err && s->mask & STATMOUNT_MNT_UIDMAP) 6018 err = statmount_string(s, STATMOUNT_MNT_UIDMAP); 6019 6020 if (!err && s->mask & STATMOUNT_MNT_GIDMAP) 6021 err = statmount_string(s, STATMOUNT_MNT_GIDMAP); 6022 6023 if (!err && s->mask & STATMOUNT_MNT_NS_ID) 6024 statmount_mnt_ns_id(s, ns); 6025 6026 if (!err && s->mask & STATMOUNT_SUPPORTED_MASK) { 6027 s->sm.mask |= STATMOUNT_SUPPORTED_MASK; 6028 s->sm.supported_mask = STATMOUNT_SUPPORTED; 6029 } 6030 6031 if (err) 6032 return err; 6033 6034 /* Are there bits in the return mask not present in STATMOUNT_SUPPORTED? */ 6035 WARN_ON_ONCE(~STATMOUNT_SUPPORTED & s->sm.mask); 6036 6037 return 0; 6038 } 6039 6040 static inline bool retry_statmount(const long ret, size_t *seq_size) 6041 { 6042 if (likely(ret != -EAGAIN)) 6043 return false; 6044 if (unlikely(check_mul_overflow(*seq_size, 2, seq_size))) 6045 return false; 6046 if (unlikely(*seq_size > MAX_RW_COUNT)) 6047 return false; 6048 return true; 6049 } 6050 6051 #define STATMOUNT_STRING_REQ (STATMOUNT_MNT_ROOT | STATMOUNT_MNT_POINT | \ 6052 STATMOUNT_FS_TYPE | STATMOUNT_MNT_OPTS | \ 6053 STATMOUNT_FS_SUBTYPE | STATMOUNT_SB_SOURCE | \ 6054 STATMOUNT_OPT_ARRAY | STATMOUNT_OPT_SEC_ARRAY | \ 6055 STATMOUNT_MNT_UIDMAP | STATMOUNT_MNT_GIDMAP) 6056 6057 static int prepare_kstatmount(struct kstatmount *ks, struct mnt_id_req *kreq, 6058 struct statmount __user *buf, size_t bufsize, 6059 size_t seq_size) 6060 { 6061 if (!access_ok(buf, bufsize)) 6062 return -EFAULT; 6063 6064 memset(ks, 0, sizeof(*ks)); 6065 ks->mask = kreq->param; 6066 ks->buf = buf; 6067 ks->bufsize = bufsize; 6068 6069 if (ks->mask & STATMOUNT_STRING_REQ) { 6070 if (bufsize == sizeof(ks->sm)) 6071 return -EOVERFLOW; 6072 6073 ks->seq.buf = kvmalloc(seq_size, GFP_KERNEL_ACCOUNT); 6074 if (!ks->seq.buf) 6075 return -ENOMEM; 6076 6077 ks->seq.size = seq_size; 6078 } 6079 6080 return 0; 6081 } 6082 6083 static int copy_mnt_id_req(const struct mnt_id_req __user *req, 6084 struct mnt_id_req *kreq, unsigned int flags) 6085 { 6086 int ret; 6087 size_t usize; 6088 6089 BUILD_BUG_ON(sizeof(struct mnt_id_req) != MNT_ID_REQ_SIZE_VER1); 6090 6091 ret = get_user(usize, &req->size); 6092 if (ret) 6093 return -EFAULT; 6094 if (unlikely(usize > PAGE_SIZE)) 6095 return -E2BIG; 6096 if (unlikely(usize < MNT_ID_REQ_SIZE_VER0)) 6097 return -EINVAL; 6098 memset(kreq, 0, sizeof(*kreq)); 6099 ret = copy_struct_from_user(kreq, sizeof(*kreq), req, usize); 6100 if (ret) 6101 return ret; 6102 6103 if (flags & STATMOUNT_BY_FD) { 6104 if (kreq->mnt_id || kreq->mnt_ns_id) 6105 return -EINVAL; 6106 } else { 6107 if (kreq->mnt_ns_fd != 0 && kreq->mnt_ns_id) 6108 return -EINVAL; 6109 /* The first valid unique mount id is MNT_UNIQUE_ID_OFFSET + 1. */ 6110 if (kreq->mnt_id <= MNT_UNIQUE_ID_OFFSET) 6111 return -EINVAL; 6112 } 6113 return 0; 6114 } 6115 6116 /* 6117 * If the user requested a specific mount namespace id, look that up and return 6118 * that, or if not simply grab a passive reference on our mount namespace and 6119 * return that. 6120 */ 6121 static struct mnt_namespace *grab_requested_mnt_ns(const struct mnt_id_req *kreq) 6122 { 6123 struct mnt_namespace *mnt_ns; 6124 6125 if (kreq->mnt_ns_id) { 6126 mnt_ns = lookup_mnt_ns(kreq->mnt_ns_id); 6127 if (!mnt_ns) 6128 return ERR_PTR(-ENOENT); 6129 } else if (kreq->mnt_ns_fd) { 6130 struct ns_common *ns; 6131 6132 CLASS(fd, f)(kreq->mnt_ns_fd); 6133 if (fd_empty(f)) 6134 return ERR_PTR(-EBADF); 6135 6136 if (!proc_ns_file(fd_file(f))) 6137 return ERR_PTR(-EINVAL); 6138 6139 ns = get_proc_ns(file_inode(fd_file(f))); 6140 if (ns->ns_type != CLONE_NEWNS) 6141 return ERR_PTR(-EINVAL); 6142 6143 mnt_ns = to_mnt_ns(ns); 6144 refcount_inc(&mnt_ns->passive); 6145 } else { 6146 mnt_ns = current->nsproxy->mnt_ns; 6147 refcount_inc(&mnt_ns->passive); 6148 } 6149 6150 return mnt_ns; 6151 } 6152 6153 SYSCALL_DEFINE4(statmount, const struct mnt_id_req __user *, req, 6154 struct statmount __user *, buf, size_t, bufsize, 6155 unsigned int, flags) 6156 { 6157 struct mnt_namespace *ns __free(mnt_ns_release) = NULL; 6158 struct kstatmount *ks __free(kfree) = NULL; 6159 struct file *mnt_file __free(fput) = NULL; 6160 struct mnt_id_req kreq; 6161 /* We currently support retrieval of 3 strings. */ 6162 size_t seq_size = 3 * PATH_MAX; 6163 int ret; 6164 6165 if (flags & ~STATMOUNT_BY_FD) 6166 return -EINVAL; 6167 6168 ret = copy_mnt_id_req(req, &kreq, flags); 6169 if (ret) 6170 return ret; 6171 6172 if (flags & STATMOUNT_BY_FD) { 6173 mnt_file = fget_raw(kreq.mnt_fd); 6174 if (!mnt_file) 6175 return -EBADF; 6176 /* do_statmount sets ns in case of STATMOUNT_BY_FD */ 6177 } else { 6178 ns = grab_requested_mnt_ns(&kreq); 6179 if (IS_ERR(ns)) 6180 return PTR_ERR(ns); 6181 6182 if (kreq.mnt_ns_id && (ns != current->nsproxy->mnt_ns) && 6183 !ns_capable_noaudit(ns->user_ns, CAP_SYS_ADMIN)) 6184 return -EPERM; 6185 } 6186 6187 ks = kmalloc_obj(*ks, GFP_KERNEL_ACCOUNT); 6188 if (!ks) 6189 return -ENOMEM; 6190 6191 retry: 6192 ret = prepare_kstatmount(ks, &kreq, buf, bufsize, seq_size); 6193 if (ret) 6194 return ret; 6195 6196 scoped_guard(namespace_shared) 6197 ret = do_statmount(ks, kreq.mnt_id, kreq.mnt_ns_id, mnt_file, ns); 6198 6199 if (!ret) 6200 ret = copy_statmount_to_user(ks); 6201 kvfree(ks->seq.buf); 6202 path_put(&ks->root); 6203 if (retry_statmount(ret, &seq_size)) 6204 goto retry; 6205 return ret; 6206 } 6207 6208 struct klistmount { 6209 u64 last_mnt_id; 6210 u64 mnt_parent_id; 6211 u64 *kmnt_ids; 6212 u32 nr_mnt_ids; 6213 struct mnt_namespace *ns; 6214 struct path root; 6215 }; 6216 6217 /* locks: namespace_shared */ 6218 static ssize_t do_listmount(struct klistmount *kls, bool reverse) 6219 { 6220 struct mnt_namespace *ns = kls->ns; 6221 u64 mnt_parent_id = kls->mnt_parent_id; 6222 u64 last_mnt_id = kls->last_mnt_id; 6223 u64 *mnt_ids = kls->kmnt_ids; 6224 size_t nr_mnt_ids = kls->nr_mnt_ids; 6225 struct path orig; 6226 struct mount *r, *first; 6227 ssize_t ret; 6228 6229 rwsem_assert_held(&namespace_sem); 6230 6231 ret = grab_requested_root(ns, &kls->root); 6232 if (ret) 6233 return ret; 6234 6235 if (mnt_parent_id == LSMT_ROOT) { 6236 orig = kls->root; 6237 } else { 6238 orig.mnt = lookup_mnt_in_ns(mnt_parent_id, ns); 6239 if (!orig.mnt) 6240 return -ENOENT; 6241 orig.dentry = orig.mnt->mnt_root; 6242 } 6243 6244 /* 6245 * Don't trigger audit denials. We just want to determine what 6246 * mounts to show users. 6247 */ 6248 if (!is_path_reachable(real_mount(orig.mnt), orig.dentry, &kls->root) && 6249 !ns_capable_noaudit(ns->user_ns, CAP_SYS_ADMIN)) 6250 return -EPERM; 6251 6252 ret = security_sb_statfs(orig.dentry); 6253 if (ret) 6254 return ret; 6255 6256 if (!last_mnt_id) { 6257 if (reverse) 6258 first = node_to_mount(ns->mnt_last_node); 6259 else 6260 first = node_to_mount(ns->mnt_first_node); 6261 } else { 6262 if (reverse) 6263 first = mnt_find_id_at_reverse(ns, last_mnt_id - 1); 6264 else 6265 first = mnt_find_id_at(ns, last_mnt_id + 1); 6266 } 6267 6268 for (ret = 0, r = first; r && nr_mnt_ids; r = listmnt_next(r, reverse)) { 6269 if (r->mnt_id_unique == mnt_parent_id) 6270 continue; 6271 if (!is_path_reachable(r, r->mnt.mnt_root, &orig)) 6272 continue; 6273 *mnt_ids = r->mnt_id_unique; 6274 mnt_ids++; 6275 nr_mnt_ids--; 6276 ret++; 6277 } 6278 return ret; 6279 } 6280 6281 static void __free_klistmount_free(const struct klistmount *kls) 6282 { 6283 path_put(&kls->root); 6284 kvfree(kls->kmnt_ids); 6285 mnt_ns_release(kls->ns); 6286 } 6287 6288 static inline int prepare_klistmount(struct klistmount *kls, struct mnt_id_req *kreq, 6289 size_t nr_mnt_ids) 6290 { 6291 u64 last_mnt_id = kreq->param; 6292 struct mnt_namespace *ns; 6293 6294 /* The first valid unique mount id is MNT_UNIQUE_ID_OFFSET + 1. */ 6295 if (last_mnt_id != 0 && last_mnt_id <= MNT_UNIQUE_ID_OFFSET) 6296 return -EINVAL; 6297 6298 kls->last_mnt_id = last_mnt_id; 6299 6300 kls->nr_mnt_ids = nr_mnt_ids; 6301 kls->kmnt_ids = kvmalloc_array(nr_mnt_ids, sizeof(*kls->kmnt_ids), 6302 GFP_KERNEL_ACCOUNT); 6303 if (!kls->kmnt_ids) 6304 return -ENOMEM; 6305 6306 ns = grab_requested_mnt_ns(kreq); 6307 if (IS_ERR(ns)) 6308 return PTR_ERR(ns); 6309 kls->ns = ns; 6310 6311 kls->mnt_parent_id = kreq->mnt_id; 6312 return 0; 6313 } 6314 6315 SYSCALL_DEFINE4(listmount, const struct mnt_id_req __user *, req, 6316 u64 __user *, mnt_ids, size_t, nr_mnt_ids, unsigned int, flags) 6317 { 6318 struct klistmount kls __free(klistmount_free) = {}; 6319 const size_t maxcount = 1000000; 6320 struct mnt_id_req kreq; 6321 ssize_t ret; 6322 6323 if (flags & ~LISTMOUNT_REVERSE) 6324 return -EINVAL; 6325 6326 /* 6327 * If the mount namespace really has more than 1 million mounts the 6328 * caller must iterate over the mount namespace (and reconsider their 6329 * system design...). 6330 */ 6331 if (unlikely(nr_mnt_ids > maxcount)) 6332 return -EOVERFLOW; 6333 6334 if (!access_ok(mnt_ids, nr_mnt_ids * sizeof(*mnt_ids))) 6335 return -EFAULT; 6336 6337 ret = copy_mnt_id_req(req, &kreq, 0); 6338 if (ret) 6339 return ret; 6340 6341 ret = prepare_klistmount(&kls, &kreq, nr_mnt_ids); 6342 if (ret) 6343 return ret; 6344 6345 if (kreq.mnt_ns_id && (kls.ns != current->nsproxy->mnt_ns) && 6346 !ns_capable_noaudit(kls.ns->user_ns, CAP_SYS_ADMIN)) 6347 return -ENOENT; 6348 6349 /* 6350 * We only need to guard against mount topology changes as 6351 * listmount() doesn't care about any mount properties. 6352 */ 6353 scoped_guard(namespace_shared) 6354 ret = do_listmount(&kls, (flags & LISTMOUNT_REVERSE)); 6355 if (ret <= 0) 6356 return ret; 6357 6358 if (copy_to_user(mnt_ids, kls.kmnt_ids, ret * sizeof(*mnt_ids))) 6359 return -EFAULT; 6360 6361 return ret; 6362 } 6363 6364 struct mnt_namespace init_mnt_ns = { 6365 .ns = NS_COMMON_INIT(init_mnt_ns), 6366 .user_ns = &init_user_ns, 6367 .passive = REFCOUNT_INIT(1), 6368 .mounts = RB_ROOT, 6369 .poll = __WAIT_QUEUE_HEAD_INITIALIZER(init_mnt_ns.poll), 6370 }; 6371 6372 static void __init mount_rootfs_on_nullfs(struct vfsmount *mnt, 6373 struct vfsmount *nullfs_mnt) 6374 { 6375 struct path root = { 6376 .mnt = nullfs_mnt, 6377 .dentry = nullfs_mnt->mnt_root, 6378 }; 6379 6380 LOCK_MOUNT_EXACT(mp, &root); 6381 if (unlikely(IS_ERR(mp.parent))) 6382 panic("VFS: Failed to mount rootfs on nullfs"); 6383 scoped_guard(mount_writer) 6384 attach_mnt(real_mount(mnt), mp.parent, mp.mp); 6385 } 6386 6387 static struct vfsmount *__init knullfs_file_mount(void) 6388 { 6389 struct dentry *file; 6390 struct mount *mnt; 6391 6392 file = nullfs_new_file(knullfs->mnt_sb); 6393 if (IS_ERR(file)) 6394 return ERR_CAST(file); 6395 mnt = clone_mnt(real_mount(knullfs), file, CL_PRIVATE); 6396 dput(file); 6397 if (IS_ERR(mnt)) 6398 return ERR_CAST(mnt); 6399 mnt->mnt_ns = MNT_NS_INTERNAL; 6400 mnt->mnt.mnt_flags |= MNT_INTERNAL; 6401 mnt->mnt.mnt_flags &= ~MNT_READONLY; 6402 dont_mount(mnt->mnt.mnt_root); 6403 return &mnt->mnt; 6404 } 6405 6406 static void __init init_mount_tree(void) 6407 { 6408 struct vfsmount *mnt, *nullfs_mnt; 6409 struct mount *mnt_root; 6410 struct path root; 6411 6412 /* 6413 * We create three mounts: 6414 * 6415 * (1) nullfs with mount id 1 6416 * (2) mutable rootfs with mount id 2 6417 * (3) private nullfs for kthreads (SB_KERNMOUNT), kept in knullfs 6418 * (4) a second mount of (3) rooted on a regular file, kept in 6419 * knullfs_file 6420 * 6421 * with (2) mounted on top of (1). The init_task's root and pwd 6422 * are pointed at (3) so all kthreads start isolated in nullfs. 6423 * A lookup at the cover an unmounted mount left behind finds (3) 6424 * or (4), see __lookup_mnt(). 6425 */ 6426 nullfs_mnt = vfs_kern_mount(&nullfs_fs_type, 0, "nullfs", NULL); 6427 if (IS_ERR(nullfs_mnt)) 6428 panic("VFS: Failed to create nullfs"); 6429 6430 mnt = vfs_kern_mount(&rootfs_fs_type, 0, "rootfs", initramfs_options); 6431 if (IS_ERR(mnt)) 6432 panic("Can't create rootfs"); 6433 6434 VFS_WARN_ON_ONCE(real_mount(nullfs_mnt)->mnt_id != 1); 6435 VFS_WARN_ON_ONCE(real_mount(mnt)->mnt_id != 2); 6436 6437 /* The namespace root is the nullfs mnt. */ 6438 mnt_root = real_mount(nullfs_mnt); 6439 init_mnt_ns.root = mnt_root; 6440 6441 mount_rootfs_on_nullfs(mnt, nullfs_mnt); 6442 6443 pr_info("VFS: Finished mounting rootfs on nullfs\n"); 6444 6445 /* 6446 * We've dropped all locks here but that's fine. Not just are we 6447 * the only task that's running, there's no other mount 6448 * namespace in existence and the initial mount namespace is 6449 * completely empty until we add the mounts we just created. 6450 */ 6451 for (struct mount *p = mnt_root; p; p = next_mnt(p, mnt_root)) { 6452 mnt_add_to_ns(&init_mnt_ns, p); 6453 init_mnt_ns.nr_mounts++; 6454 } 6455 6456 knullfs = kern_mount(&nullfs_fs_type); 6457 if (IS_ERR(knullfs)) 6458 panic("VFS: Failed to create private nullfs instance"); 6459 /* nothing is ever mounted on the root of a kernel thread */ 6460 dont_mount(knullfs->mnt_root); 6461 /* and nothing is ever written through it */ 6462 knullfs->mnt_flags |= MNT_READONLY; 6463 knullfs_file = knullfs_file_mount(); 6464 if (IS_ERR(knullfs_file)) 6465 panic("VFS: Failed to create the nullfs file stand-in"); 6466 root.mnt = knullfs; 6467 root.dentry = knullfs->mnt_root; 6468 6469 init_task.nsproxy->mnt_ns = &init_mnt_ns; 6470 get_mnt_ns(&init_mnt_ns); 6471 set_fs_pwd(current->fs, &root); 6472 set_fs_root(current->fs, &root); 6473 6474 ns_tree_add(&init_mnt_ns); 6475 } 6476 6477 void __init mnt_init(void) 6478 { 6479 int err; 6480 6481 mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount), 6482 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, NULL); 6483 6484 mount_hashtable = alloc_large_system_hash("Mount-cache", 6485 sizeof(struct hlist_head), 6486 mhash_entries, 19, 6487 HASH_ZERO, 6488 &m_hash_shift, &m_hash_mask, 0, 0); 6489 mountpoint_hashtable = alloc_large_system_hash("Mountpoint-cache", 6490 sizeof(struct hlist_head), 6491 mphash_entries, 19, 6492 HASH_ZERO, 6493 &mp_hash_shift, &mp_hash_mask, 0, 0); 6494 6495 super_dev_init(); 6496 6497 kernfs_init(); 6498 6499 err = sysfs_init(); 6500 if (err) 6501 printk(KERN_WARNING "%s: sysfs_init error: %d\n", 6502 __func__, err); 6503 fs_kobj = kobject_create_and_add("fs", NULL); 6504 if (!fs_kobj) 6505 printk(KERN_WARNING "%s: kobj create error\n", __func__); 6506 shmem_init(); 6507 init_rootfs(); 6508 init_mount_tree(); 6509 failfs_init(); 6510 } 6511 6512 void put_mnt_ns(struct mnt_namespace *ns) 6513 { 6514 if (!ns_ref_put(ns)) 6515 return; 6516 guard(namespace_excl)(); 6517 emptied_ns = ns; 6518 guard(mount_writer)(); 6519 umount_tree(ns->root, 0); 6520 } 6521 6522 struct vfsmount *kern_mount(struct file_system_type *type) 6523 { 6524 struct vfsmount *mnt; 6525 mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, NULL); 6526 if (!IS_ERR(mnt)) { 6527 /* 6528 * it is a longterm mount, don't release mnt until 6529 * we unmount before file sys is unregistered 6530 */ 6531 real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL; 6532 } 6533 return mnt; 6534 } 6535 EXPORT_SYMBOL_GPL(kern_mount); 6536 6537 void kern_unmount(struct vfsmount *mnt) 6538 { 6539 /* release long term mount so mount point can be released */ 6540 if (!IS_ERR(mnt)) { 6541 mnt_make_shortterm(mnt); 6542 synchronize_rcu(); /* yecchhh... */ 6543 mntput(mnt); 6544 } 6545 } 6546 EXPORT_SYMBOL(kern_unmount); 6547 6548 void kern_unmount_array(struct vfsmount *mnt[], unsigned int num) 6549 { 6550 unsigned int i; 6551 6552 for (i = 0; i < num; i++) 6553 mnt_make_shortterm(mnt[i]); 6554 synchronize_rcu_expedited(); 6555 for (i = 0; i < num; i++) 6556 mntput(mnt[i]); 6557 } 6558 EXPORT_SYMBOL(kern_unmount_array); 6559 6560 bool our_mnt(struct vfsmount *mnt) 6561 { 6562 return check_mnt(real_mount(mnt)); 6563 } 6564 6565 bool current_chrooted(void) 6566 { 6567 /* Does the current process have a non-standard root */ 6568 struct path fs_root __free(path_put) = {}; 6569 struct mount *root; 6570 6571 get_fs_root(current->fs, &fs_root); 6572 6573 /* Find the namespace root */ 6574 6575 guard(mount_locked_reader)(); 6576 6577 root = topmost_overmount(current->nsproxy->mnt_ns->root); 6578 6579 return fs_root.mnt != &root->mnt || !path_mounted(&fs_root); 6580 } 6581 6582 static bool mnt_already_visible(struct mnt_namespace *ns, 6583 const struct super_block *sb, 6584 int *new_mnt_flags) 6585 { 6586 int new_flags = *new_mnt_flags; 6587 struct mount *mnt; 6588 6589 /* Don't acquire namespace semaphore without a good reason. */ 6590 if (hlist_empty(&ns->mnt_visible_mounts)) 6591 return false; 6592 6593 guard(namespace_shared)(); 6594 hlist_for_each_entry(mnt, &ns->mnt_visible_mounts, mnt_ns_visible) { 6595 const struct super_block *sb_visible = mnt->mnt.mnt_sb; 6596 struct mount *child; 6597 int mnt_flags; 6598 6599 if (sb_visible->s_type != sb->s_type) 6600 continue; 6601 6602 /* 6603 * Restricted variants are not compatible with anything, even 6604 * other restricted variants. 6605 */ 6606 if (sb_visible->s_iflags & SB_I_RESTRICTED_VARIANT) 6607 continue; 6608 6609 /* A local view of the mount flags */ 6610 mnt_flags = mnt->mnt.mnt_flags; 6611 6612 /* Don't miss readonly hidden in the superblock flags */ 6613 if (sb_rdonly(mnt->mnt.mnt_sb)) 6614 mnt_flags |= MNT_LOCK_READONLY; 6615 6616 /* Verify the mount flags are equal to or more permissive 6617 * than the proposed new mount. 6618 */ 6619 if ((mnt_flags & MNT_LOCK_READONLY) && 6620 !(new_flags & MNT_READONLY)) 6621 continue; 6622 if ((mnt_flags & MNT_LOCK_ATIME) && 6623 ((mnt_flags & MNT_ATIME_MASK) != (new_flags & MNT_ATIME_MASK))) 6624 continue; 6625 6626 /* This mount is not fully visible if there are any 6627 * locked child mounts that cover anything except for 6628 * empty directories. 6629 */ 6630 list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) { 6631 struct inode *inode = child->mnt_mountpoint->d_inode; 6632 /* Only worry about locked mounts */ 6633 if (!(child->mnt.mnt_flags & MNT_LOCKED)) 6634 continue; 6635 /* Is the directory permanently empty? */ 6636 if (!is_empty_dir_inode(inode)) 6637 goto next; 6638 } 6639 /* Preserve the locked attributes */ 6640 *new_mnt_flags |= mnt_flags & (MNT_LOCK_READONLY | \ 6641 MNT_LOCK_ATIME); 6642 return true; 6643 next: ; 6644 } 6645 return false; 6646 } 6647 6648 static bool mount_too_revealing(const struct super_block *sb, int *new_mnt_flags) 6649 { 6650 const unsigned long required_iflags = SB_I_NOEXEC | SB_I_NODEV; 6651 struct mnt_namespace *ns = current->nsproxy->mnt_ns; 6652 unsigned long s_iflags; 6653 6654 if (ns->user_ns == &init_user_ns) 6655 return false; 6656 6657 /* Can this filesystem be too revealing? */ 6658 if (!(sb->s_type->fs_flags & FS_USERNS_MOUNT_RESTRICTED)) 6659 return false; 6660 6661 s_iflags = sb->s_iflags; 6662 if ((s_iflags & required_iflags) != required_iflags) { 6663 WARN_ONCE(1, "Expected s_iflags to contain 0x%lx\n", 6664 required_iflags); 6665 return true; 6666 } 6667 6668 /* 6669 * Restricted variants don't need an already visible mount because they 6670 * don't expose the full filesystem view. 6671 */ 6672 if (s_iflags & SB_I_RESTRICTED_VARIANT) 6673 return false; 6674 6675 return !mnt_already_visible(ns, sb, new_mnt_flags); 6676 } 6677 6678 bool mnt_may_suid(struct vfsmount *mnt) 6679 { 6680 /* 6681 * Foreign mounts (accessed via fchdir or through /proc 6682 * symlinks) are always treated as if they are nosuid. This 6683 * prevents namespaces from trusting potentially unsafe 6684 * suid/sgid bits, file caps, or security labels that originate 6685 * in other namespaces. 6686 */ 6687 return !(mnt->mnt_flags & MNT_NOSUID) && check_mnt(real_mount(mnt)) && 6688 current_in_userns(mnt->mnt_sb->s_user_ns); 6689 } 6690 6691 static struct ns_common *mntns_get(struct task_struct *task) 6692 { 6693 struct ns_common *ns = NULL; 6694 struct nsproxy *nsproxy; 6695 6696 task_lock(task); 6697 nsproxy = task->nsproxy; 6698 if (nsproxy) { 6699 ns = &nsproxy->mnt_ns->ns; 6700 get_mnt_ns(to_mnt_ns(ns)); 6701 } 6702 task_unlock(task); 6703 6704 return ns; 6705 } 6706 6707 static void mntns_put(struct ns_common *ns) 6708 { 6709 put_mnt_ns(to_mnt_ns(ns)); 6710 } 6711 6712 static int mntns_install(struct nsset *nsset, struct ns_common *ns) 6713 { 6714 struct nsproxy *nsproxy = nsset->nsproxy; 6715 struct fs_struct *fs = nsset->fs; 6716 struct mnt_namespace *mnt_ns = to_mnt_ns(ns), *old_mnt_ns; 6717 struct user_namespace *user_ns = nsset->cred->user_ns; 6718 struct path root; 6719 int err; 6720 6721 if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) || 6722 !ns_capable(user_ns, CAP_SYS_CHROOT) || 6723 !ns_capable(user_ns, CAP_SYS_ADMIN)) 6724 return -EPERM; 6725 6726 if (is_anon_ns(mnt_ns)) 6727 return -EINVAL; 6728 6729 if (fs->users != 1) 6730 return -EINVAL; 6731 6732 get_mnt_ns(mnt_ns); 6733 old_mnt_ns = nsproxy->mnt_ns; 6734 nsproxy->mnt_ns = mnt_ns; 6735 6736 /* Find the root */ 6737 err = vfs_path_lookup(mnt_ns->root->mnt.mnt_root, &mnt_ns->root->mnt, 6738 "/", LOOKUP_DOWN, &root); 6739 if (err) { 6740 /* revert to old namespace */ 6741 nsproxy->mnt_ns = old_mnt_ns; 6742 put_mnt_ns(mnt_ns); 6743 return err; 6744 } 6745 6746 put_mnt_ns(old_mnt_ns); 6747 6748 /* Update the pwd and root */ 6749 set_fs_pwd(fs, &root); 6750 set_fs_root(fs, &root); 6751 6752 path_put(&root); 6753 return 0; 6754 } 6755 6756 static struct user_namespace *mntns_owner(struct ns_common *ns) 6757 { 6758 return to_mnt_ns(ns)->user_ns; 6759 } 6760 6761 const struct proc_ns_operations mntns_operations = { 6762 .name = "mnt", 6763 .get = mntns_get, 6764 .put = mntns_put, 6765 .install = mntns_install, 6766 .owner = mntns_owner, 6767 }; 6768 6769 #ifdef CONFIG_SYSCTL 6770 static const struct ctl_table fs_namespace_sysctls[] = { 6771 { 6772 .procname = "mount-max", 6773 .data = &sysctl_mount_max, 6774 .maxlen = sizeof(unsigned int), 6775 .mode = 0644, 6776 .proc_handler = proc_dointvec_minmax, 6777 .extra1 = SYSCTL_ONE, 6778 }, 6779 }; 6780 6781 static int __init init_fs_namespace_sysctls(void) 6782 { 6783 register_sysctl_init("fs", fs_namespace_sysctls); 6784 return 0; 6785 } 6786 fs_initcall(init_fs_namespace_sysctls); 6787 6788 #endif /* CONFIG_SYSCTL */ 6789