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