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