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