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