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