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