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