159bd9dedSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/fs/namespace.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * (C) Copyright Al Viro 2000, 2001 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Based on code from fs/super.c, copyright Linus Torvalds and others. 81da177e4SLinus Torvalds * Heavily rewritten. 91da177e4SLinus Torvalds */ 101da177e4SLinus Torvalds 111da177e4SLinus Torvalds #include <linux/syscalls.h> 12d10577a8SAl Viro #include <linux/export.h> 1316f7e0feSRandy Dunlap #include <linux/capability.h> 146b3286edSKirill Korotaev #include <linux/mnt_namespace.h> 15771b1371SEric W. Biederman #include <linux/user_namespace.h> 161da177e4SLinus Torvalds #include <linux/namei.h> 171da177e4SLinus Torvalds #include <linux/security.h> 185b825c3aSIngo Molnar #include <linux/cred.h> 1973cd49ecSMiklos Szeredi #include <linux/idr.h> 2057f150a5SRob Landley #include <linux/init.h> /* init_rootfs */ 21d10577a8SAl Viro #include <linux/fs_struct.h> /* get_fs_root et.al. */ 22d10577a8SAl Viro #include <linux/fsnotify.h> /* fsnotify_vfsmount_delete */ 23a07b2000SAl Viro #include <linux/file.h> 24d10577a8SAl Viro #include <linux/uaccess.h> 250bb80f24SDavid Howells #include <linux/proc_ns.h> 2620b4fb48SLinus Torvalds #include <linux/magic.h> 2757c8a661SMike Rapoport #include <linux/memblock.h> 289ea459e1SAl Viro #include <linux/task_work.h> 299164bb4aSIngo Molnar #include <linux/sched/task.h> 30e262e32dSDavid Howells #include <uapi/linux/mount.h> 319bc61ab1SDavid Howells #include <linux/fs_context.h> 32037f11b4SAl Viro #include <linux/shmem_fs.h> 339164bb4aSIngo Molnar 3407b20889SRam Pai #include "pnode.h" 35948730b0SAdrian Bunk #include "internal.h" 361da177e4SLinus Torvalds 37d2921684SEric W. Biederman /* Maximum number of mounts in a mount namespace */ 38d2921684SEric W. Biederman unsigned int sysctl_mount_max __read_mostly = 100000; 39d2921684SEric W. Biederman 400818bf27SAl Viro static unsigned int m_hash_mask __read_mostly; 410818bf27SAl Viro static unsigned int m_hash_shift __read_mostly; 420818bf27SAl Viro static unsigned int mp_hash_mask __read_mostly; 430818bf27SAl Viro static unsigned int mp_hash_shift __read_mostly; 440818bf27SAl Viro 450818bf27SAl Viro static __initdata unsigned long mhash_entries; 460818bf27SAl Viro static int __init set_mhash_entries(char *str) 470818bf27SAl Viro { 480818bf27SAl Viro if (!str) 490818bf27SAl Viro return 0; 500818bf27SAl Viro mhash_entries = simple_strtoul(str, &str, 0); 510818bf27SAl Viro return 1; 520818bf27SAl Viro } 530818bf27SAl Viro __setup("mhash_entries=", set_mhash_entries); 540818bf27SAl Viro 550818bf27SAl Viro static __initdata unsigned long mphash_entries; 560818bf27SAl Viro static int __init set_mphash_entries(char *str) 570818bf27SAl Viro { 580818bf27SAl Viro if (!str) 590818bf27SAl Viro return 0; 600818bf27SAl Viro mphash_entries = simple_strtoul(str, &str, 0); 610818bf27SAl Viro return 1; 620818bf27SAl Viro } 630818bf27SAl Viro __setup("mphash_entries=", set_mphash_entries); 6413f14b4dSEric Dumazet 65c7999c36SAl Viro static u64 event; 6673cd49ecSMiklos Szeredi static DEFINE_IDA(mnt_id_ida); 67719f5d7fSMiklos Szeredi static DEFINE_IDA(mnt_group_ida); 685addc5ddSAl Viro 6938129a13SAl Viro static struct hlist_head *mount_hashtable __read_mostly; 700818bf27SAl Viro static struct hlist_head *mountpoint_hashtable __read_mostly; 71e18b890bSChristoph Lameter static struct kmem_cache *mnt_cache __read_mostly; 7259aa0da8SAl Viro static DECLARE_RWSEM(namespace_sem); 734edbe133SAl Viro static HLIST_HEAD(unmounted); /* protected by namespace_sem */ 744edbe133SAl Viro static LIST_HEAD(ex_mountpoints); /* protected by namespace_sem */ 751da177e4SLinus Torvalds 76f87fd4c2SMiklos Szeredi /* /sys/fs */ 7700d26666SGreg Kroah-Hartman struct kobject *fs_kobj; 7800d26666SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(fs_kobj); 79f87fd4c2SMiklos Szeredi 8099b7db7bSNick Piggin /* 8199b7db7bSNick Piggin * vfsmount lock may be taken for read to prevent changes to the 8299b7db7bSNick Piggin * vfsmount hash, ie. during mountpoint lookups or walking back 8399b7db7bSNick Piggin * up the tree. 8499b7db7bSNick Piggin * 8599b7db7bSNick Piggin * It should be taken for write in all cases where the vfsmount 8699b7db7bSNick Piggin * tree or hash is modified or when a vfsmount structure is modified. 8799b7db7bSNick Piggin */ 8848a066e7SAl Viro __cacheline_aligned_in_smp DEFINE_SEQLOCK(mount_lock); 8999b7db7bSNick Piggin 9038129a13SAl Viro static inline struct hlist_head *m_hash(struct vfsmount *mnt, struct dentry *dentry) 911da177e4SLinus Torvalds { 921da177e4SLinus Torvalds unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES); 931da177e4SLinus Torvalds tmp += ((unsigned long)dentry / L1_CACHE_BYTES); 940818bf27SAl Viro tmp = tmp + (tmp >> m_hash_shift); 950818bf27SAl Viro return &mount_hashtable[tmp & m_hash_mask]; 960818bf27SAl Viro } 970818bf27SAl Viro 980818bf27SAl Viro static inline struct hlist_head *mp_hash(struct dentry *dentry) 990818bf27SAl Viro { 1000818bf27SAl Viro unsigned long tmp = ((unsigned long)dentry / L1_CACHE_BYTES); 1010818bf27SAl Viro tmp = tmp + (tmp >> mp_hash_shift); 1020818bf27SAl Viro return &mountpoint_hashtable[tmp & mp_hash_mask]; 1031da177e4SLinus Torvalds } 1041da177e4SLinus Torvalds 105b105e270SAl Viro static int mnt_alloc_id(struct mount *mnt) 10673cd49ecSMiklos Szeredi { 107169b480eSMatthew Wilcox int res = ida_alloc(&mnt_id_ida, GFP_KERNEL); 10873cd49ecSMiklos Szeredi 109169b480eSMatthew Wilcox if (res < 0) 11073cd49ecSMiklos Szeredi return res; 111169b480eSMatthew Wilcox mnt->mnt_id = res; 112169b480eSMatthew Wilcox return 0; 11373cd49ecSMiklos Szeredi } 11473cd49ecSMiklos Szeredi 115b105e270SAl Viro static void mnt_free_id(struct mount *mnt) 11673cd49ecSMiklos Szeredi { 117169b480eSMatthew Wilcox ida_free(&mnt_id_ida, mnt->mnt_id); 11873cd49ecSMiklos Szeredi } 11973cd49ecSMiklos Szeredi 120719f5d7fSMiklos Szeredi /* 121719f5d7fSMiklos Szeredi * Allocate a new peer group ID 122719f5d7fSMiklos Szeredi */ 1234b8b21f4SAl Viro static int mnt_alloc_group_id(struct mount *mnt) 124719f5d7fSMiklos Szeredi { 125169b480eSMatthew Wilcox int res = ida_alloc_min(&mnt_group_ida, 1, GFP_KERNEL); 126f21f6220SAl Viro 127169b480eSMatthew Wilcox if (res < 0) 128f21f6220SAl Viro return res; 129169b480eSMatthew Wilcox mnt->mnt_group_id = res; 130169b480eSMatthew Wilcox return 0; 131719f5d7fSMiklos Szeredi } 132719f5d7fSMiklos Szeredi 133719f5d7fSMiklos Szeredi /* 134719f5d7fSMiklos Szeredi * Release a peer group ID 135719f5d7fSMiklos Szeredi */ 1364b8b21f4SAl Viro void mnt_release_group_id(struct mount *mnt) 137719f5d7fSMiklos Szeredi { 138169b480eSMatthew Wilcox ida_free(&mnt_group_ida, mnt->mnt_group_id); 13915169fe7SAl Viro mnt->mnt_group_id = 0; 140719f5d7fSMiklos Szeredi } 141719f5d7fSMiklos Szeredi 142b3e19d92SNick Piggin /* 143b3e19d92SNick Piggin * vfsmount lock must be held for read 144b3e19d92SNick Piggin */ 14583adc753SAl Viro static inline void mnt_add_count(struct mount *mnt, int n) 146b3e19d92SNick Piggin { 147b3e19d92SNick Piggin #ifdef CONFIG_SMP 14868e8a9feSAl Viro this_cpu_add(mnt->mnt_pcp->mnt_count, n); 149b3e19d92SNick Piggin #else 150b3e19d92SNick Piggin preempt_disable(); 15168e8a9feSAl Viro mnt->mnt_count += n; 152b3e19d92SNick Piggin preempt_enable(); 153b3e19d92SNick Piggin #endif 154b3e19d92SNick Piggin } 155b3e19d92SNick Piggin 156b3e19d92SNick Piggin /* 157b3e19d92SNick Piggin * vfsmount lock must be held for write 158b3e19d92SNick Piggin */ 159edf7ddbfSEric Biggers int mnt_get_count(struct mount *mnt) 160b3e19d92SNick Piggin { 161b3e19d92SNick Piggin #ifdef CONFIG_SMP 162edf7ddbfSEric Biggers int count = 0; 163b3e19d92SNick Piggin int cpu; 164b3e19d92SNick Piggin 165b3e19d92SNick Piggin for_each_possible_cpu(cpu) { 16668e8a9feSAl Viro count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count; 167b3e19d92SNick Piggin } 168b3e19d92SNick Piggin 169b3e19d92SNick Piggin return count; 170b3e19d92SNick Piggin #else 17168e8a9feSAl Viro return mnt->mnt_count; 172b3e19d92SNick Piggin #endif 173b3e19d92SNick Piggin } 174b3e19d92SNick Piggin 175b105e270SAl Viro static struct mount *alloc_vfsmnt(const char *name) 1761da177e4SLinus Torvalds { 177c63181e6SAl Viro struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL); 178c63181e6SAl Viro if (mnt) { 17973cd49ecSMiklos Szeredi int err; 18073cd49ecSMiklos Szeredi 181c63181e6SAl Viro err = mnt_alloc_id(mnt); 18288b38782SLi Zefan if (err) 18388b38782SLi Zefan goto out_free_cache; 18488b38782SLi Zefan 18588b38782SLi Zefan if (name) { 186fcc139aeSAndrzej Hajda mnt->mnt_devname = kstrdup_const(name, GFP_KERNEL); 187c63181e6SAl Viro if (!mnt->mnt_devname) 18888b38782SLi Zefan goto out_free_id; 18973cd49ecSMiklos Szeredi } 19073cd49ecSMiklos Szeredi 191b3e19d92SNick Piggin #ifdef CONFIG_SMP 192c63181e6SAl Viro mnt->mnt_pcp = alloc_percpu(struct mnt_pcp); 193c63181e6SAl Viro if (!mnt->mnt_pcp) 194b3e19d92SNick Piggin goto out_free_devname; 195b3e19d92SNick Piggin 196c63181e6SAl Viro this_cpu_add(mnt->mnt_pcp->mnt_count, 1); 197b3e19d92SNick Piggin #else 198c63181e6SAl Viro mnt->mnt_count = 1; 199c63181e6SAl Viro mnt->mnt_writers = 0; 200b3e19d92SNick Piggin #endif 201b3e19d92SNick Piggin 20238129a13SAl Viro INIT_HLIST_NODE(&mnt->mnt_hash); 203c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_child); 204c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_mounts); 205c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_list); 206c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_expire); 207c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_share); 208c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_slave_list); 209c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_slave); 2100a5eb7c8SEric W. Biederman INIT_HLIST_NODE(&mnt->mnt_mp_list); 21199b19d16SEric W. Biederman INIT_LIST_HEAD(&mnt->mnt_umounting); 21256cbb429SAl Viro INIT_HLIST_HEAD(&mnt->mnt_stuck_children); 2131da177e4SLinus Torvalds } 214c63181e6SAl Viro return mnt; 21588b38782SLi Zefan 216d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 217d3ef3d73Snpiggin@suse.de out_free_devname: 218fcc139aeSAndrzej Hajda kfree_const(mnt->mnt_devname); 219d3ef3d73Snpiggin@suse.de #endif 22088b38782SLi Zefan out_free_id: 221c63181e6SAl Viro mnt_free_id(mnt); 22288b38782SLi Zefan out_free_cache: 223c63181e6SAl Viro kmem_cache_free(mnt_cache, mnt); 22488b38782SLi Zefan return NULL; 2251da177e4SLinus Torvalds } 2261da177e4SLinus Torvalds 2278366025eSDave Hansen /* 2288366025eSDave Hansen * Most r/o checks on a fs are for operations that take 2298366025eSDave Hansen * discrete amounts of time, like a write() or unlink(). 2308366025eSDave Hansen * We must keep track of when those operations start 2318366025eSDave Hansen * (for permission checks) and when they end, so that 2328366025eSDave Hansen * we can determine when writes are able to occur to 2338366025eSDave Hansen * a filesystem. 2348366025eSDave Hansen */ 2353d733633SDave Hansen /* 2363d733633SDave Hansen * __mnt_is_readonly: check whether a mount is read-only 2373d733633SDave Hansen * @mnt: the mount to check for its write status 2383d733633SDave Hansen * 2393d733633SDave Hansen * This shouldn't be used directly ouside of the VFS. 2403d733633SDave Hansen * It does not guarantee that the filesystem will stay 2413d733633SDave Hansen * r/w, just that it is right *now*. This can not and 2423d733633SDave Hansen * should not be used in place of IS_RDONLY(inode). 2433d733633SDave Hansen * mnt_want/drop_write() will _keep_ the filesystem 2443d733633SDave Hansen * r/w. 2453d733633SDave Hansen */ 24643f5e655SDavid Howells bool __mnt_is_readonly(struct vfsmount *mnt) 2473d733633SDave Hansen { 24843f5e655SDavid Howells return (mnt->mnt_flags & MNT_READONLY) || sb_rdonly(mnt->mnt_sb); 2493d733633SDave Hansen } 2503d733633SDave Hansen EXPORT_SYMBOL_GPL(__mnt_is_readonly); 2513d733633SDave Hansen 25283adc753SAl Viro static inline void mnt_inc_writers(struct mount *mnt) 2533d733633SDave Hansen { 254d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 25568e8a9feSAl Viro this_cpu_inc(mnt->mnt_pcp->mnt_writers); 256d3ef3d73Snpiggin@suse.de #else 25768e8a9feSAl Viro mnt->mnt_writers++; 258d3ef3d73Snpiggin@suse.de #endif 2593d733633SDave Hansen } 2603d733633SDave Hansen 26183adc753SAl Viro static inline void mnt_dec_writers(struct mount *mnt) 2623d733633SDave Hansen { 263d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 26468e8a9feSAl Viro this_cpu_dec(mnt->mnt_pcp->mnt_writers); 265d3ef3d73Snpiggin@suse.de #else 26668e8a9feSAl Viro mnt->mnt_writers--; 267d3ef3d73Snpiggin@suse.de #endif 268d3ef3d73Snpiggin@suse.de } 269d3ef3d73Snpiggin@suse.de 27083adc753SAl Viro static unsigned int mnt_get_writers(struct mount *mnt) 271d3ef3d73Snpiggin@suse.de { 272d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 273d3ef3d73Snpiggin@suse.de unsigned int count = 0; 2743d733633SDave Hansen int cpu; 2753d733633SDave Hansen 2763d733633SDave Hansen for_each_possible_cpu(cpu) { 27768e8a9feSAl Viro count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers; 2783d733633SDave Hansen } 2793d733633SDave Hansen 280d3ef3d73Snpiggin@suse.de return count; 281d3ef3d73Snpiggin@suse.de #else 282d3ef3d73Snpiggin@suse.de return mnt->mnt_writers; 283d3ef3d73Snpiggin@suse.de #endif 2843d733633SDave Hansen } 2853d733633SDave Hansen 2864ed5e82fSMiklos Szeredi static int mnt_is_readonly(struct vfsmount *mnt) 2874ed5e82fSMiklos Szeredi { 2884ed5e82fSMiklos Szeredi if (mnt->mnt_sb->s_readonly_remount) 2894ed5e82fSMiklos Szeredi return 1; 2904ed5e82fSMiklos Szeredi /* Order wrt setting s_flags/s_readonly_remount in do_remount() */ 2914ed5e82fSMiklos Szeredi smp_rmb(); 2924ed5e82fSMiklos Szeredi return __mnt_is_readonly(mnt); 2934ed5e82fSMiklos Szeredi } 2944ed5e82fSMiklos Szeredi 2953d733633SDave Hansen /* 296eb04c282SJan Kara * Most r/o & frozen checks on a fs are for operations that take discrete 297eb04c282SJan Kara * amounts of time, like a write() or unlink(). We must keep track of when 298eb04c282SJan Kara * those operations start (for permission checks) and when they end, so that we 299eb04c282SJan Kara * can determine when writes are able to occur to a filesystem. 3003d733633SDave Hansen */ 3018366025eSDave Hansen /** 302eb04c282SJan Kara * __mnt_want_write - get write access to a mount without freeze protection 30383adc753SAl Viro * @m: the mount on which to take a write 3048366025eSDave Hansen * 305eb04c282SJan Kara * This tells the low-level filesystem that a write is about to be performed to 306eb04c282SJan Kara * it, and makes sure that writes are allowed (mnt it read-write) before 307eb04c282SJan Kara * returning success. This operation does not protect against filesystem being 308eb04c282SJan Kara * frozen. When the write operation is finished, __mnt_drop_write() must be 309eb04c282SJan Kara * called. This is effectively a refcount. 3108366025eSDave Hansen */ 311eb04c282SJan Kara int __mnt_want_write(struct vfsmount *m) 3128366025eSDave Hansen { 31383adc753SAl Viro struct mount *mnt = real_mount(m); 3143d733633SDave Hansen int ret = 0; 3153d733633SDave Hansen 316d3ef3d73Snpiggin@suse.de preempt_disable(); 317c6653a83SNick Piggin mnt_inc_writers(mnt); 318d3ef3d73Snpiggin@suse.de /* 319c6653a83SNick Piggin * The store to mnt_inc_writers must be visible before we pass 320d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD loop below, so that the slowpath can see our 321d3ef3d73Snpiggin@suse.de * incremented count after it has set MNT_WRITE_HOLD. 322d3ef3d73Snpiggin@suse.de */ 323d3ef3d73Snpiggin@suse.de smp_mb(); 3246aa7de05SMark Rutland while (READ_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD) 325d3ef3d73Snpiggin@suse.de cpu_relax(); 326d3ef3d73Snpiggin@suse.de /* 327d3ef3d73Snpiggin@suse.de * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will 328d3ef3d73Snpiggin@suse.de * be set to match its requirements. So we must not load that until 329d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD is cleared. 330d3ef3d73Snpiggin@suse.de */ 331d3ef3d73Snpiggin@suse.de smp_rmb(); 3324ed5e82fSMiklos Szeredi if (mnt_is_readonly(m)) { 333c6653a83SNick Piggin mnt_dec_writers(mnt); 3343d733633SDave Hansen ret = -EROFS; 3353d733633SDave Hansen } 336d3ef3d73Snpiggin@suse.de preempt_enable(); 337eb04c282SJan Kara 338eb04c282SJan Kara return ret; 339eb04c282SJan Kara } 340eb04c282SJan Kara 341eb04c282SJan Kara /** 342eb04c282SJan Kara * mnt_want_write - get write access to a mount 343eb04c282SJan Kara * @m: the mount on which to take a write 344eb04c282SJan Kara * 345eb04c282SJan Kara * This tells the low-level filesystem that a write is about to be performed to 346eb04c282SJan Kara * it, and makes sure that writes are allowed (mount is read-write, filesystem 347eb04c282SJan Kara * is not frozen) before returning success. When the write operation is 348eb04c282SJan Kara * finished, mnt_drop_write() must be called. This is effectively a refcount. 349eb04c282SJan Kara */ 350eb04c282SJan Kara int mnt_want_write(struct vfsmount *m) 351eb04c282SJan Kara { 352eb04c282SJan Kara int ret; 353eb04c282SJan Kara 354eb04c282SJan Kara sb_start_write(m->mnt_sb); 355eb04c282SJan Kara ret = __mnt_want_write(m); 356eb04c282SJan Kara if (ret) 357eb04c282SJan Kara sb_end_write(m->mnt_sb); 3583d733633SDave Hansen return ret; 3598366025eSDave Hansen } 3608366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_want_write); 3618366025eSDave Hansen 3628366025eSDave Hansen /** 363eb04c282SJan Kara * __mnt_want_write_file - get write access to a file's mount 364eb04c282SJan Kara * @file: the file who's mount on which to take a write 365eb04c282SJan Kara * 366*14e43bf4SEric Biggers * This is like __mnt_want_write, but if the file is already open for writing it 367*14e43bf4SEric Biggers * skips incrementing mnt_writers (since the open file already has a reference) 368*14e43bf4SEric Biggers * and instead only does the check for emergency r/o remounts. This must be 369*14e43bf4SEric Biggers * paired with __mnt_drop_write_file. 370eb04c282SJan Kara */ 371eb04c282SJan Kara int __mnt_want_write_file(struct file *file) 372eb04c282SJan Kara { 373*14e43bf4SEric Biggers if (file->f_mode & FMODE_WRITER) { 374*14e43bf4SEric Biggers /* 375*14e43bf4SEric Biggers * Superblock may have become readonly while there are still 376*14e43bf4SEric Biggers * writable fd's, e.g. due to a fs error with errors=remount-ro 377*14e43bf4SEric Biggers */ 378*14e43bf4SEric Biggers if (__mnt_is_readonly(file->f_path.mnt)) 379*14e43bf4SEric Biggers return -EROFS; 380*14e43bf4SEric Biggers return 0; 381*14e43bf4SEric Biggers } 382eb04c282SJan Kara return __mnt_want_write(file->f_path.mnt); 383eb04c282SJan Kara } 384eb04c282SJan Kara 385eb04c282SJan Kara /** 3867c6893e3SMiklos Szeredi * mnt_want_write_file - get write access to a file's mount 3877c6893e3SMiklos Szeredi * @file: the file who's mount on which to take a write 3887c6893e3SMiklos Szeredi * 389*14e43bf4SEric Biggers * This is like mnt_want_write, but if the file is already open for writing it 390*14e43bf4SEric Biggers * skips incrementing mnt_writers (since the open file already has a reference) 391*14e43bf4SEric Biggers * and instead only does the freeze protection and the check for emergency r/o 392*14e43bf4SEric Biggers * remounts. This must be paired with mnt_drop_write_file. 3937c6893e3SMiklos Szeredi */ 3947c6893e3SMiklos Szeredi int mnt_want_write_file(struct file *file) 3957c6893e3SMiklos Szeredi { 3967c6893e3SMiklos Szeredi int ret; 3977c6893e3SMiklos Szeredi 3987c6893e3SMiklos Szeredi sb_start_write(file_inode(file)->i_sb); 3997c6893e3SMiklos Szeredi ret = __mnt_want_write_file(file); 4007c6893e3SMiklos Szeredi if (ret) 4017c6893e3SMiklos Szeredi sb_end_write(file_inode(file)->i_sb); 4027c6893e3SMiklos Szeredi return ret; 4037c6893e3SMiklos Szeredi } 40496029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_want_write_file); 40596029c4eSnpiggin@suse.de 40696029c4eSnpiggin@suse.de /** 407eb04c282SJan Kara * __mnt_drop_write - give up write access to a mount 4088366025eSDave Hansen * @mnt: the mount on which to give up write access 4098366025eSDave Hansen * 4108366025eSDave Hansen * Tells the low-level filesystem that we are done 4118366025eSDave Hansen * performing writes to it. Must be matched with 412eb04c282SJan Kara * __mnt_want_write() call above. 4138366025eSDave Hansen */ 414eb04c282SJan Kara void __mnt_drop_write(struct vfsmount *mnt) 4158366025eSDave Hansen { 416d3ef3d73Snpiggin@suse.de preempt_disable(); 41783adc753SAl Viro mnt_dec_writers(real_mount(mnt)); 418d3ef3d73Snpiggin@suse.de preempt_enable(); 4198366025eSDave Hansen } 420eb04c282SJan Kara 421eb04c282SJan Kara /** 422eb04c282SJan Kara * mnt_drop_write - give up write access to a mount 423eb04c282SJan Kara * @mnt: the mount on which to give up write access 424eb04c282SJan Kara * 425eb04c282SJan Kara * Tells the low-level filesystem that we are done performing writes to it and 426eb04c282SJan Kara * also allows filesystem to be frozen again. Must be matched with 427eb04c282SJan Kara * mnt_want_write() call above. 428eb04c282SJan Kara */ 429eb04c282SJan Kara void mnt_drop_write(struct vfsmount *mnt) 430eb04c282SJan Kara { 431eb04c282SJan Kara __mnt_drop_write(mnt); 432eb04c282SJan Kara sb_end_write(mnt->mnt_sb); 433eb04c282SJan Kara } 4348366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_drop_write); 4358366025eSDave Hansen 436eb04c282SJan Kara void __mnt_drop_write_file(struct file *file) 437eb04c282SJan Kara { 438*14e43bf4SEric Biggers if (!(file->f_mode & FMODE_WRITER)) 439eb04c282SJan Kara __mnt_drop_write(file->f_path.mnt); 440eb04c282SJan Kara } 441eb04c282SJan Kara 4427c6893e3SMiklos Szeredi void mnt_drop_write_file(struct file *file) 4437c6893e3SMiklos Szeredi { 444a6795a58SMiklos Szeredi __mnt_drop_write_file(file); 4457c6893e3SMiklos Szeredi sb_end_write(file_inode(file)->i_sb); 4467c6893e3SMiklos Szeredi } 4472a79f17eSAl Viro EXPORT_SYMBOL(mnt_drop_write_file); 4482a79f17eSAl Viro 44983adc753SAl Viro static int mnt_make_readonly(struct mount *mnt) 4508366025eSDave Hansen { 4513d733633SDave Hansen int ret = 0; 4523d733633SDave Hansen 453719ea2fbSAl Viro lock_mount_hash(); 45483adc753SAl Viro mnt->mnt.mnt_flags |= MNT_WRITE_HOLD; 455d3ef3d73Snpiggin@suse.de /* 456d3ef3d73Snpiggin@suse.de * After storing MNT_WRITE_HOLD, we'll read the counters. This store 457d3ef3d73Snpiggin@suse.de * should be visible before we do. 458d3ef3d73Snpiggin@suse.de */ 459d3ef3d73Snpiggin@suse.de smp_mb(); 460d3ef3d73Snpiggin@suse.de 461d3ef3d73Snpiggin@suse.de /* 462d3ef3d73Snpiggin@suse.de * With writers on hold, if this value is zero, then there are 463d3ef3d73Snpiggin@suse.de * definitely no active writers (although held writers may subsequently 464d3ef3d73Snpiggin@suse.de * increment the count, they'll have to wait, and decrement it after 465d3ef3d73Snpiggin@suse.de * seeing MNT_READONLY). 466d3ef3d73Snpiggin@suse.de * 467d3ef3d73Snpiggin@suse.de * It is OK to have counter incremented on one CPU and decremented on 468d3ef3d73Snpiggin@suse.de * another: the sum will add up correctly. The danger would be when we 469d3ef3d73Snpiggin@suse.de * sum up each counter, if we read a counter before it is incremented, 470d3ef3d73Snpiggin@suse.de * but then read another CPU's count which it has been subsequently 471d3ef3d73Snpiggin@suse.de * decremented from -- we would see more decrements than we should. 472d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD protects against this scenario, because 473d3ef3d73Snpiggin@suse.de * mnt_want_write first increments count, then smp_mb, then spins on 474d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD, so it can't be decremented by another CPU while 475d3ef3d73Snpiggin@suse.de * we're counting up here. 476d3ef3d73Snpiggin@suse.de */ 477c6653a83SNick Piggin if (mnt_get_writers(mnt) > 0) 478d3ef3d73Snpiggin@suse.de ret = -EBUSY; 479d3ef3d73Snpiggin@suse.de else 48083adc753SAl Viro mnt->mnt.mnt_flags |= MNT_READONLY; 481d3ef3d73Snpiggin@suse.de /* 482d3ef3d73Snpiggin@suse.de * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers 483d3ef3d73Snpiggin@suse.de * that become unheld will see MNT_READONLY. 484d3ef3d73Snpiggin@suse.de */ 485d3ef3d73Snpiggin@suse.de smp_wmb(); 48683adc753SAl Viro mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD; 487719ea2fbSAl Viro unlock_mount_hash(); 4883d733633SDave Hansen return ret; 4893d733633SDave Hansen } 4908366025eSDave Hansen 49143f5e655SDavid Howells static int __mnt_unmake_readonly(struct mount *mnt) 4922e4b7fcdSDave Hansen { 493719ea2fbSAl Viro lock_mount_hash(); 49483adc753SAl Viro mnt->mnt.mnt_flags &= ~MNT_READONLY; 495719ea2fbSAl Viro unlock_mount_hash(); 49643f5e655SDavid Howells return 0; 4972e4b7fcdSDave Hansen } 4982e4b7fcdSDave Hansen 4994ed5e82fSMiklos Szeredi int sb_prepare_remount_readonly(struct super_block *sb) 5004ed5e82fSMiklos Szeredi { 5014ed5e82fSMiklos Szeredi struct mount *mnt; 5024ed5e82fSMiklos Szeredi int err = 0; 5034ed5e82fSMiklos Szeredi 5048e8b8796SMiklos Szeredi /* Racy optimization. Recheck the counter under MNT_WRITE_HOLD */ 5058e8b8796SMiklos Szeredi if (atomic_long_read(&sb->s_remove_count)) 5068e8b8796SMiklos Szeredi return -EBUSY; 5078e8b8796SMiklos Szeredi 508719ea2fbSAl Viro lock_mount_hash(); 5094ed5e82fSMiklos Szeredi list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) { 5104ed5e82fSMiklos Szeredi if (!(mnt->mnt.mnt_flags & MNT_READONLY)) { 5114ed5e82fSMiklos Szeredi mnt->mnt.mnt_flags |= MNT_WRITE_HOLD; 5124ed5e82fSMiklos Szeredi smp_mb(); 5134ed5e82fSMiklos Szeredi if (mnt_get_writers(mnt) > 0) { 5144ed5e82fSMiklos Szeredi err = -EBUSY; 5154ed5e82fSMiklos Szeredi break; 5164ed5e82fSMiklos Szeredi } 5174ed5e82fSMiklos Szeredi } 5184ed5e82fSMiklos Szeredi } 5198e8b8796SMiklos Szeredi if (!err && atomic_long_read(&sb->s_remove_count)) 5208e8b8796SMiklos Szeredi err = -EBUSY; 5218e8b8796SMiklos Szeredi 5224ed5e82fSMiklos Szeredi if (!err) { 5234ed5e82fSMiklos Szeredi sb->s_readonly_remount = 1; 5244ed5e82fSMiklos Szeredi smp_wmb(); 5254ed5e82fSMiklos Szeredi } 5264ed5e82fSMiklos Szeredi list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) { 5274ed5e82fSMiklos Szeredi if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD) 5284ed5e82fSMiklos Szeredi mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD; 5294ed5e82fSMiklos Szeredi } 530719ea2fbSAl Viro unlock_mount_hash(); 5314ed5e82fSMiklos Szeredi 5324ed5e82fSMiklos Szeredi return err; 5334ed5e82fSMiklos Szeredi } 5344ed5e82fSMiklos Szeredi 535b105e270SAl Viro static void free_vfsmnt(struct mount *mnt) 5361da177e4SLinus Torvalds { 537fcc139aeSAndrzej Hajda kfree_const(mnt->mnt_devname); 538d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 53968e8a9feSAl Viro free_percpu(mnt->mnt_pcp); 540d3ef3d73Snpiggin@suse.de #endif 541b105e270SAl Viro kmem_cache_free(mnt_cache, mnt); 5421da177e4SLinus Torvalds } 5431da177e4SLinus Torvalds 5448ffcb32eSDavid Howells static void delayed_free_vfsmnt(struct rcu_head *head) 5458ffcb32eSDavid Howells { 5468ffcb32eSDavid Howells free_vfsmnt(container_of(head, struct mount, mnt_rcu)); 5478ffcb32eSDavid Howells } 5488ffcb32eSDavid Howells 54948a066e7SAl Viro /* call under rcu_read_lock */ 550294d71ffSAl Viro int __legitimize_mnt(struct vfsmount *bastard, unsigned seq) 55148a066e7SAl Viro { 55248a066e7SAl Viro struct mount *mnt; 55348a066e7SAl Viro if (read_seqretry(&mount_lock, seq)) 554294d71ffSAl Viro return 1; 55548a066e7SAl Viro if (bastard == NULL) 556294d71ffSAl Viro return 0; 55748a066e7SAl Viro mnt = real_mount(bastard); 55848a066e7SAl Viro mnt_add_count(mnt, 1); 559119e1ef8SAl Viro smp_mb(); // see mntput_no_expire() 56048a066e7SAl Viro if (likely(!read_seqretry(&mount_lock, seq))) 561294d71ffSAl Viro return 0; 56248a066e7SAl Viro if (bastard->mnt_flags & MNT_SYNC_UMOUNT) { 56348a066e7SAl Viro mnt_add_count(mnt, -1); 564294d71ffSAl Viro return 1; 56548a066e7SAl Viro } 566119e1ef8SAl Viro lock_mount_hash(); 567119e1ef8SAl Viro if (unlikely(bastard->mnt_flags & MNT_DOOMED)) { 568119e1ef8SAl Viro mnt_add_count(mnt, -1); 569119e1ef8SAl Viro unlock_mount_hash(); 570119e1ef8SAl Viro return 1; 571119e1ef8SAl Viro } 572119e1ef8SAl Viro unlock_mount_hash(); 573119e1ef8SAl Viro /* caller will mntput() */ 574294d71ffSAl Viro return -1; 575294d71ffSAl Viro } 576294d71ffSAl Viro 577294d71ffSAl Viro /* call under rcu_read_lock */ 578294d71ffSAl Viro bool legitimize_mnt(struct vfsmount *bastard, unsigned seq) 579294d71ffSAl Viro { 580294d71ffSAl Viro int res = __legitimize_mnt(bastard, seq); 581294d71ffSAl Viro if (likely(!res)) 582294d71ffSAl Viro return true; 583294d71ffSAl Viro if (unlikely(res < 0)) { 58448a066e7SAl Viro rcu_read_unlock(); 58548a066e7SAl Viro mntput(bastard); 58648a066e7SAl Viro rcu_read_lock(); 587294d71ffSAl Viro } 58848a066e7SAl Viro return false; 58948a066e7SAl Viro } 59048a066e7SAl Viro 5911da177e4SLinus Torvalds /* 592474279dcSAl Viro * find the first mount at @dentry on vfsmount @mnt. 59348a066e7SAl Viro * call under rcu_read_lock() 5941da177e4SLinus Torvalds */ 595474279dcSAl Viro struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry) 5961da177e4SLinus Torvalds { 59738129a13SAl Viro struct hlist_head *head = m_hash(mnt, dentry); 598474279dcSAl Viro struct mount *p; 5991da177e4SLinus Torvalds 60038129a13SAl Viro hlist_for_each_entry_rcu(p, head, mnt_hash) 601474279dcSAl Viro if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry) 602474279dcSAl Viro return p; 603474279dcSAl Viro return NULL; 6041da177e4SLinus Torvalds } 605474279dcSAl Viro 606474279dcSAl Viro /* 607f015f126SDavid Howells * lookup_mnt - Return the first child mount mounted at path 608f015f126SDavid Howells * 609f015f126SDavid Howells * "First" means first mounted chronologically. If you create the 610f015f126SDavid Howells * following mounts: 611f015f126SDavid Howells * 612f015f126SDavid Howells * mount /dev/sda1 /mnt 613f015f126SDavid Howells * mount /dev/sda2 /mnt 614f015f126SDavid Howells * mount /dev/sda3 /mnt 615f015f126SDavid Howells * 616f015f126SDavid Howells * Then lookup_mnt() on the base /mnt dentry in the root mount will 617f015f126SDavid Howells * return successively the root dentry and vfsmount of /dev/sda1, then 618f015f126SDavid Howells * /dev/sda2, then /dev/sda3, then NULL. 619f015f126SDavid Howells * 620f015f126SDavid Howells * lookup_mnt takes a reference to the found vfsmount. 621a05964f3SRam Pai */ 622ca71cf71SAl Viro struct vfsmount *lookup_mnt(const struct path *path) 623a05964f3SRam Pai { 624c7105365SAl Viro struct mount *child_mnt; 62548a066e7SAl Viro struct vfsmount *m; 62648a066e7SAl Viro unsigned seq; 62799b7db7bSNick Piggin 62848a066e7SAl Viro rcu_read_lock(); 62948a066e7SAl Viro do { 63048a066e7SAl Viro seq = read_seqbegin(&mount_lock); 631474279dcSAl Viro child_mnt = __lookup_mnt(path->mnt, path->dentry); 63248a066e7SAl Viro m = child_mnt ? &child_mnt->mnt : NULL; 63348a066e7SAl Viro } while (!legitimize_mnt(m, seq)); 63448a066e7SAl Viro rcu_read_unlock(); 63548a066e7SAl Viro return m; 636a05964f3SRam Pai } 637a05964f3SRam Pai 6389f6c61f9SMiklos Szeredi static inline void lock_ns_list(struct mnt_namespace *ns) 6399f6c61f9SMiklos Szeredi { 6409f6c61f9SMiklos Szeredi spin_lock(&ns->ns_lock); 6419f6c61f9SMiklos Szeredi } 6429f6c61f9SMiklos Szeredi 6439f6c61f9SMiklos Szeredi static inline void unlock_ns_list(struct mnt_namespace *ns) 6449f6c61f9SMiklos Szeredi { 6459f6c61f9SMiklos Szeredi spin_unlock(&ns->ns_lock); 6469f6c61f9SMiklos Szeredi } 6479f6c61f9SMiklos Szeredi 6489f6c61f9SMiklos Szeredi static inline bool mnt_is_cursor(struct mount *mnt) 6499f6c61f9SMiklos Szeredi { 6509f6c61f9SMiklos Szeredi return mnt->mnt.mnt_flags & MNT_CURSOR; 6519f6c61f9SMiklos Szeredi } 6529f6c61f9SMiklos Szeredi 6537af1364fSEric W. Biederman /* 6547af1364fSEric W. Biederman * __is_local_mountpoint - Test to see if dentry is a mountpoint in the 6557af1364fSEric W. Biederman * current mount namespace. 6567af1364fSEric W. Biederman * 6577af1364fSEric W. Biederman * The common case is dentries are not mountpoints at all and that 6587af1364fSEric W. Biederman * test is handled inline. For the slow case when we are actually 6597af1364fSEric W. Biederman * dealing with a mountpoint of some kind, walk through all of the 6607af1364fSEric W. Biederman * mounts in the current mount namespace and test to see if the dentry 6617af1364fSEric W. Biederman * is a mountpoint. 6627af1364fSEric W. Biederman * 6637af1364fSEric W. Biederman * The mount_hashtable is not usable in the context because we 6647af1364fSEric W. Biederman * need to identify all mounts that may be in the current mount 6657af1364fSEric W. Biederman * namespace not just a mount that happens to have some specified 6667af1364fSEric W. Biederman * parent mount. 6677af1364fSEric W. Biederman */ 6687af1364fSEric W. Biederman bool __is_local_mountpoint(struct dentry *dentry) 6697af1364fSEric W. Biederman { 6707af1364fSEric W. Biederman struct mnt_namespace *ns = current->nsproxy->mnt_ns; 6717af1364fSEric W. Biederman struct mount *mnt; 6727af1364fSEric W. Biederman bool is_covered = false; 6737af1364fSEric W. Biederman 6747af1364fSEric W. Biederman down_read(&namespace_sem); 6759f6c61f9SMiklos Szeredi lock_ns_list(ns); 6767af1364fSEric W. Biederman list_for_each_entry(mnt, &ns->list, mnt_list) { 6779f6c61f9SMiklos Szeredi if (mnt_is_cursor(mnt)) 6789f6c61f9SMiklos Szeredi continue; 6797af1364fSEric W. Biederman is_covered = (mnt->mnt_mountpoint == dentry); 6807af1364fSEric W. Biederman if (is_covered) 6817af1364fSEric W. Biederman break; 6827af1364fSEric W. Biederman } 6839f6c61f9SMiklos Szeredi unlock_ns_list(ns); 6847af1364fSEric W. Biederman up_read(&namespace_sem); 6855ad05cc8SNikolay Borisov 6867af1364fSEric W. Biederman return is_covered; 6877af1364fSEric W. Biederman } 6887af1364fSEric W. Biederman 689e2dfa935SEric W. Biederman static struct mountpoint *lookup_mountpoint(struct dentry *dentry) 69084d17192SAl Viro { 6910818bf27SAl Viro struct hlist_head *chain = mp_hash(dentry); 69284d17192SAl Viro struct mountpoint *mp; 69384d17192SAl Viro 6940818bf27SAl Viro hlist_for_each_entry(mp, chain, m_hash) { 69584d17192SAl Viro if (mp->m_dentry == dentry) { 69684d17192SAl Viro mp->m_count++; 69784d17192SAl Viro return mp; 69884d17192SAl Viro } 69984d17192SAl Viro } 700e2dfa935SEric W. Biederman return NULL; 701e2dfa935SEric W. Biederman } 702e2dfa935SEric W. Biederman 7033895dbf8SEric W. Biederman static struct mountpoint *get_mountpoint(struct dentry *dentry) 704e2dfa935SEric W. Biederman { 7053895dbf8SEric W. Biederman struct mountpoint *mp, *new = NULL; 706e2dfa935SEric W. Biederman int ret; 70784d17192SAl Viro 7083895dbf8SEric W. Biederman if (d_mountpoint(dentry)) { 7091e9c75fbSBenjamin Coddington /* might be worth a WARN_ON() */ 7101e9c75fbSBenjamin Coddington if (d_unlinked(dentry)) 7111e9c75fbSBenjamin Coddington return ERR_PTR(-ENOENT); 7123895dbf8SEric W. Biederman mountpoint: 7133895dbf8SEric W. Biederman read_seqlock_excl(&mount_lock); 7143895dbf8SEric W. Biederman mp = lookup_mountpoint(dentry); 7153895dbf8SEric W. Biederman read_sequnlock_excl(&mount_lock); 7163895dbf8SEric W. Biederman if (mp) 7173895dbf8SEric W. Biederman goto done; 71884d17192SAl Viro } 719eed81007SMiklos Szeredi 7203895dbf8SEric W. Biederman if (!new) 7213895dbf8SEric W. Biederman new = kmalloc(sizeof(struct mountpoint), GFP_KERNEL); 7223895dbf8SEric W. Biederman if (!new) 7233895dbf8SEric W. Biederman return ERR_PTR(-ENOMEM); 7243895dbf8SEric W. Biederman 7253895dbf8SEric W. Biederman 7263895dbf8SEric W. Biederman /* Exactly one processes may set d_mounted */ 7273895dbf8SEric W. Biederman ret = d_set_mounted(dentry); 7283895dbf8SEric W. Biederman 7293895dbf8SEric W. Biederman /* Someone else set d_mounted? */ 7303895dbf8SEric W. Biederman if (ret == -EBUSY) 7313895dbf8SEric W. Biederman goto mountpoint; 7323895dbf8SEric W. Biederman 7333895dbf8SEric W. Biederman /* The dentry is not available as a mountpoint? */ 7343895dbf8SEric W. Biederman mp = ERR_PTR(ret); 7353895dbf8SEric W. Biederman if (ret) 7363895dbf8SEric W. Biederman goto done; 7373895dbf8SEric W. Biederman 7383895dbf8SEric W. Biederman /* Add the new mountpoint to the hash table */ 7393895dbf8SEric W. Biederman read_seqlock_excl(&mount_lock); 7404edbe133SAl Viro new->m_dentry = dget(dentry); 7413895dbf8SEric W. Biederman new->m_count = 1; 7423895dbf8SEric W. Biederman hlist_add_head(&new->m_hash, mp_hash(dentry)); 7433895dbf8SEric W. Biederman INIT_HLIST_HEAD(&new->m_list); 7443895dbf8SEric W. Biederman read_sequnlock_excl(&mount_lock); 7453895dbf8SEric W. Biederman 7463895dbf8SEric W. Biederman mp = new; 7473895dbf8SEric W. Biederman new = NULL; 7483895dbf8SEric W. Biederman done: 7493895dbf8SEric W. Biederman kfree(new); 75084d17192SAl Viro return mp; 75184d17192SAl Viro } 75284d17192SAl Viro 7534edbe133SAl Viro /* 7544edbe133SAl Viro * vfsmount lock must be held. Additionally, the caller is responsible 7554edbe133SAl Viro * for serializing calls for given disposal list. 7564edbe133SAl Viro */ 7574edbe133SAl Viro static void __put_mountpoint(struct mountpoint *mp, struct list_head *list) 75884d17192SAl Viro { 75984d17192SAl Viro if (!--mp->m_count) { 76084d17192SAl Viro struct dentry *dentry = mp->m_dentry; 7610a5eb7c8SEric W. Biederman BUG_ON(!hlist_empty(&mp->m_list)); 76284d17192SAl Viro spin_lock(&dentry->d_lock); 76384d17192SAl Viro dentry->d_flags &= ~DCACHE_MOUNTED; 76484d17192SAl Viro spin_unlock(&dentry->d_lock); 7654edbe133SAl Viro dput_to_list(dentry, list); 7660818bf27SAl Viro hlist_del(&mp->m_hash); 76784d17192SAl Viro kfree(mp); 76884d17192SAl Viro } 76984d17192SAl Viro } 77084d17192SAl Viro 7714edbe133SAl Viro /* called with namespace_lock and vfsmount lock */ 7724edbe133SAl Viro static void put_mountpoint(struct mountpoint *mp) 7734edbe133SAl Viro { 7744edbe133SAl Viro __put_mountpoint(mp, &ex_mountpoints); 7754edbe133SAl Viro } 7764edbe133SAl Viro 777143c8c91SAl Viro static inline int check_mnt(struct mount *mnt) 7781da177e4SLinus Torvalds { 7796b3286edSKirill Korotaev return mnt->mnt_ns == current->nsproxy->mnt_ns; 7801da177e4SLinus Torvalds } 7811da177e4SLinus Torvalds 78299b7db7bSNick Piggin /* 78399b7db7bSNick Piggin * vfsmount lock must be held for write 78499b7db7bSNick Piggin */ 7856b3286edSKirill Korotaev static void touch_mnt_namespace(struct mnt_namespace *ns) 7865addc5ddSAl Viro { 7875addc5ddSAl Viro if (ns) { 7885addc5ddSAl Viro ns->event = ++event; 7895addc5ddSAl Viro wake_up_interruptible(&ns->poll); 7905addc5ddSAl Viro } 7915addc5ddSAl Viro } 7925addc5ddSAl Viro 79399b7db7bSNick Piggin /* 79499b7db7bSNick Piggin * vfsmount lock must be held for write 79599b7db7bSNick Piggin */ 7966b3286edSKirill Korotaev static void __touch_mnt_namespace(struct mnt_namespace *ns) 7975addc5ddSAl Viro { 7985addc5ddSAl Viro if (ns && ns->event != event) { 7995addc5ddSAl Viro ns->event = event; 8005addc5ddSAl Viro wake_up_interruptible(&ns->poll); 8015addc5ddSAl Viro } 8025addc5ddSAl Viro } 8035addc5ddSAl Viro 80499b7db7bSNick Piggin /* 80599b7db7bSNick Piggin * vfsmount lock must be held for write 80699b7db7bSNick Piggin */ 807e4e59906SAl Viro static struct mountpoint *unhash_mnt(struct mount *mnt) 8081da177e4SLinus Torvalds { 809e4e59906SAl Viro struct mountpoint *mp; 8100714a533SAl Viro mnt->mnt_parent = mnt; 811a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 8126b41d536SAl Viro list_del_init(&mnt->mnt_child); 81338129a13SAl Viro hlist_del_init_rcu(&mnt->mnt_hash); 8140a5eb7c8SEric W. Biederman hlist_del_init(&mnt->mnt_mp_list); 815e4e59906SAl Viro mp = mnt->mnt_mp; 81684d17192SAl Viro mnt->mnt_mp = NULL; 817e4e59906SAl Viro return mp; 8187bdb11deSEric W. Biederman } 8197bdb11deSEric W. Biederman 8207bdb11deSEric W. Biederman /* 8217bdb11deSEric W. Biederman * vfsmount lock must be held for write 8227bdb11deSEric W. Biederman */ 8236a46c573SEric W. Biederman static void umount_mnt(struct mount *mnt) 8246a46c573SEric W. Biederman { 825e4e59906SAl Viro put_mountpoint(unhash_mnt(mnt)); 8266a46c573SEric W. Biederman } 8276a46c573SEric W. Biederman 8286a46c573SEric W. Biederman /* 8296a46c573SEric W. Biederman * vfsmount lock must be held for write 8306a46c573SEric W. Biederman */ 83184d17192SAl Viro void mnt_set_mountpoint(struct mount *mnt, 83284d17192SAl Viro struct mountpoint *mp, 83344d964d6SAl Viro struct mount *child_mnt) 834b90fa9aeSRam Pai { 83584d17192SAl Viro mp->m_count++; 8363a2393d7SAl Viro mnt_add_count(mnt, 1); /* essentially, that's mntget */ 8374edbe133SAl Viro child_mnt->mnt_mountpoint = mp->m_dentry; 8383a2393d7SAl Viro child_mnt->mnt_parent = mnt; 83984d17192SAl Viro child_mnt->mnt_mp = mp; 8400a5eb7c8SEric W. Biederman hlist_add_head(&child_mnt->mnt_mp_list, &mp->m_list); 841b90fa9aeSRam Pai } 842b90fa9aeSRam Pai 8431064f874SEric W. Biederman static void __attach_mnt(struct mount *mnt, struct mount *parent) 8441064f874SEric W. Biederman { 8451064f874SEric W. Biederman hlist_add_head_rcu(&mnt->mnt_hash, 8461064f874SEric W. Biederman m_hash(&parent->mnt, mnt->mnt_mountpoint)); 8471064f874SEric W. Biederman list_add_tail(&mnt->mnt_child, &parent->mnt_mounts); 8481064f874SEric W. Biederman } 8491064f874SEric W. Biederman 85099b7db7bSNick Piggin /* 85199b7db7bSNick Piggin * vfsmount lock must be held for write 85299b7db7bSNick Piggin */ 85384d17192SAl Viro static void attach_mnt(struct mount *mnt, 85484d17192SAl Viro struct mount *parent, 85584d17192SAl Viro struct mountpoint *mp) 8561da177e4SLinus Torvalds { 85784d17192SAl Viro mnt_set_mountpoint(parent, mp, mnt); 8581064f874SEric W. Biederman __attach_mnt(mnt, parent); 859b90fa9aeSRam Pai } 860b90fa9aeSRam Pai 8611064f874SEric W. Biederman void mnt_change_mountpoint(struct mount *parent, struct mountpoint *mp, struct mount *mnt) 86212a5b529SAl Viro { 8631064f874SEric W. Biederman struct mountpoint *old_mp = mnt->mnt_mp; 8641064f874SEric W. Biederman struct mount *old_parent = mnt->mnt_parent; 8651064f874SEric W. Biederman 8661064f874SEric W. Biederman list_del_init(&mnt->mnt_child); 8671064f874SEric W. Biederman hlist_del_init(&mnt->mnt_mp_list); 8681064f874SEric W. Biederman hlist_del_init_rcu(&mnt->mnt_hash); 8691064f874SEric W. Biederman 8701064f874SEric W. Biederman attach_mnt(mnt, parent, mp); 8711064f874SEric W. Biederman 8721064f874SEric W. Biederman put_mountpoint(old_mp); 8731064f874SEric W. Biederman mnt_add_count(old_parent, -1); 87412a5b529SAl Viro } 87512a5b529SAl Viro 876b90fa9aeSRam Pai /* 87799b7db7bSNick Piggin * vfsmount lock must be held for write 878b90fa9aeSRam Pai */ 8791064f874SEric W. Biederman static void commit_tree(struct mount *mnt) 880b90fa9aeSRam Pai { 8810714a533SAl Viro struct mount *parent = mnt->mnt_parent; 88283adc753SAl Viro struct mount *m; 883b90fa9aeSRam Pai LIST_HEAD(head); 884143c8c91SAl Viro struct mnt_namespace *n = parent->mnt_ns; 885b90fa9aeSRam Pai 8860714a533SAl Viro BUG_ON(parent == mnt); 887b90fa9aeSRam Pai 8881a4eeaf2SAl Viro list_add_tail(&head, &mnt->mnt_list); 889f7a99c5bSAl Viro list_for_each_entry(m, &head, mnt_list) 890143c8c91SAl Viro m->mnt_ns = n; 891f03c6599SAl Viro 892b90fa9aeSRam Pai list_splice(&head, n->list.prev); 893b90fa9aeSRam Pai 894d2921684SEric W. Biederman n->mounts += n->pending_mounts; 895d2921684SEric W. Biederman n->pending_mounts = 0; 896d2921684SEric W. Biederman 8971064f874SEric W. Biederman __attach_mnt(mnt, parent); 8986b3286edSKirill Korotaev touch_mnt_namespace(n); 8991da177e4SLinus Torvalds } 9001da177e4SLinus Torvalds 901909b0a88SAl Viro static struct mount *next_mnt(struct mount *p, struct mount *root) 9021da177e4SLinus Torvalds { 9036b41d536SAl Viro struct list_head *next = p->mnt_mounts.next; 9046b41d536SAl Viro if (next == &p->mnt_mounts) { 9051da177e4SLinus Torvalds while (1) { 906909b0a88SAl Viro if (p == root) 9071da177e4SLinus Torvalds return NULL; 9086b41d536SAl Viro next = p->mnt_child.next; 9096b41d536SAl Viro if (next != &p->mnt_parent->mnt_mounts) 9101da177e4SLinus Torvalds break; 9110714a533SAl Viro p = p->mnt_parent; 9121da177e4SLinus Torvalds } 9131da177e4SLinus Torvalds } 9146b41d536SAl Viro return list_entry(next, struct mount, mnt_child); 9151da177e4SLinus Torvalds } 9161da177e4SLinus Torvalds 917315fc83eSAl Viro static struct mount *skip_mnt_tree(struct mount *p) 9189676f0c6SRam Pai { 9196b41d536SAl Viro struct list_head *prev = p->mnt_mounts.prev; 9206b41d536SAl Viro while (prev != &p->mnt_mounts) { 9216b41d536SAl Viro p = list_entry(prev, struct mount, mnt_child); 9226b41d536SAl Viro prev = p->mnt_mounts.prev; 9239676f0c6SRam Pai } 9249676f0c6SRam Pai return p; 9259676f0c6SRam Pai } 9269676f0c6SRam Pai 9278f291889SAl Viro /** 9288f291889SAl Viro * vfs_create_mount - Create a mount for a configured superblock 9298f291889SAl Viro * @fc: The configuration context with the superblock attached 9308f291889SAl Viro * 9318f291889SAl Viro * Create a mount to an already configured superblock. If necessary, the 9328f291889SAl Viro * caller should invoke vfs_get_tree() before calling this. 9338f291889SAl Viro * 9348f291889SAl Viro * Note that this does not attach the mount to anything. 9358f291889SAl Viro */ 9368f291889SAl Viro struct vfsmount *vfs_create_mount(struct fs_context *fc) 9379d412a43SAl Viro { 938b105e270SAl Viro struct mount *mnt; 9399d412a43SAl Viro 9408f291889SAl Viro if (!fc->root) 9418f291889SAl Viro return ERR_PTR(-EINVAL); 9429d412a43SAl Viro 9438f291889SAl Viro mnt = alloc_vfsmnt(fc->source ?: "none"); 9449d412a43SAl Viro if (!mnt) 9459d412a43SAl Viro return ERR_PTR(-ENOMEM); 9469d412a43SAl Viro 9478f291889SAl Viro if (fc->sb_flags & SB_KERNMOUNT) 948b105e270SAl Viro mnt->mnt.mnt_flags = MNT_INTERNAL; 9499d412a43SAl Viro 9508f291889SAl Viro atomic_inc(&fc->root->d_sb->s_active); 9518f291889SAl Viro mnt->mnt.mnt_sb = fc->root->d_sb; 9528f291889SAl Viro mnt->mnt.mnt_root = dget(fc->root); 953a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 9540714a533SAl Viro mnt->mnt_parent = mnt; 9558f291889SAl Viro 956719ea2fbSAl Viro lock_mount_hash(); 9578f291889SAl Viro list_add_tail(&mnt->mnt_instance, &mnt->mnt.mnt_sb->s_mounts); 958719ea2fbSAl Viro unlock_mount_hash(); 959b105e270SAl Viro return &mnt->mnt; 9609d412a43SAl Viro } 9618f291889SAl Viro EXPORT_SYMBOL(vfs_create_mount); 9628f291889SAl Viro 9638f291889SAl Viro struct vfsmount *fc_mount(struct fs_context *fc) 9648f291889SAl Viro { 9658f291889SAl Viro int err = vfs_get_tree(fc); 9668f291889SAl Viro if (!err) { 9678f291889SAl Viro up_write(&fc->root->d_sb->s_umount); 9688f291889SAl Viro return vfs_create_mount(fc); 9698f291889SAl Viro } 9708f291889SAl Viro return ERR_PTR(err); 9718f291889SAl Viro } 9728f291889SAl Viro EXPORT_SYMBOL(fc_mount); 9738f291889SAl Viro 9749bc61ab1SDavid Howells struct vfsmount *vfs_kern_mount(struct file_system_type *type, 9759bc61ab1SDavid Howells int flags, const char *name, 9769bc61ab1SDavid Howells void *data) 9771da177e4SLinus Torvalds { 9789bc61ab1SDavid Howells struct fs_context *fc; 9798f291889SAl Viro struct vfsmount *mnt; 9809bc61ab1SDavid Howells int ret = 0; 9819d412a43SAl Viro 9829d412a43SAl Viro if (!type) 9833e1aeb00SDavid Howells return ERR_PTR(-EINVAL); 9849d412a43SAl Viro 9859bc61ab1SDavid Howells fc = fs_context_for_mount(type, flags); 9869bc61ab1SDavid Howells if (IS_ERR(fc)) 9879bc61ab1SDavid Howells return ERR_CAST(fc); 9889bc61ab1SDavid Howells 9893e1aeb00SDavid Howells if (name) 9903e1aeb00SDavid Howells ret = vfs_parse_fs_string(fc, "source", 9913e1aeb00SDavid Howells name, strlen(name)); 9929bc61ab1SDavid Howells if (!ret) 9939bc61ab1SDavid Howells ret = parse_monolithic_mount_data(fc, data); 9949bc61ab1SDavid Howells if (!ret) 9958f291889SAl Viro mnt = fc_mount(fc); 9968f291889SAl Viro else 9978f291889SAl Viro mnt = ERR_PTR(ret); 9989d412a43SAl Viro 9999bc61ab1SDavid Howells put_fs_context(fc); 10008f291889SAl Viro return mnt; 10019d412a43SAl Viro } 10029d412a43SAl Viro EXPORT_SYMBOL_GPL(vfs_kern_mount); 10039d412a43SAl Viro 100493faccbbSEric W. Biederman struct vfsmount * 100593faccbbSEric W. Biederman vfs_submount(const struct dentry *mountpoint, struct file_system_type *type, 100693faccbbSEric W. Biederman const char *name, void *data) 100793faccbbSEric W. Biederman { 100893faccbbSEric W. Biederman /* Until it is worked out how to pass the user namespace 100993faccbbSEric W. Biederman * through from the parent mount to the submount don't support 101093faccbbSEric W. Biederman * unprivileged mounts with submounts. 101193faccbbSEric W. Biederman */ 101293faccbbSEric W. Biederman if (mountpoint->d_sb->s_user_ns != &init_user_ns) 101393faccbbSEric W. Biederman return ERR_PTR(-EPERM); 101493faccbbSEric W. Biederman 1015e462ec50SDavid Howells return vfs_kern_mount(type, SB_SUBMOUNT, name, data); 101693faccbbSEric W. Biederman } 101793faccbbSEric W. Biederman EXPORT_SYMBOL_GPL(vfs_submount); 101893faccbbSEric W. Biederman 101987129cc0SAl Viro static struct mount *clone_mnt(struct mount *old, struct dentry *root, 102036341f64SRam Pai int flag) 10211da177e4SLinus Torvalds { 102287129cc0SAl Viro struct super_block *sb = old->mnt.mnt_sb; 1023be34d1a3SDavid Howells struct mount *mnt; 1024be34d1a3SDavid Howells int err; 10251da177e4SLinus Torvalds 1026be34d1a3SDavid Howells mnt = alloc_vfsmnt(old->mnt_devname); 1027be34d1a3SDavid Howells if (!mnt) 1028be34d1a3SDavid Howells return ERR_PTR(-ENOMEM); 1029be34d1a3SDavid Howells 10307a472ef4SEric W. Biederman if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE)) 103115169fe7SAl Viro mnt->mnt_group_id = 0; /* not a peer of original */ 1032719f5d7fSMiklos Szeredi else 103315169fe7SAl Viro mnt->mnt_group_id = old->mnt_group_id; 1034719f5d7fSMiklos Szeredi 103515169fe7SAl Viro if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) { 1036be34d1a3SDavid Howells err = mnt_alloc_group_id(mnt); 1037719f5d7fSMiklos Szeredi if (err) 1038719f5d7fSMiklos Szeredi goto out_free; 1039719f5d7fSMiklos Szeredi } 1040719f5d7fSMiklos Szeredi 104116a34adbSAl Viro mnt->mnt.mnt_flags = old->mnt.mnt_flags; 104216a34adbSAl Viro mnt->mnt.mnt_flags &= ~(MNT_WRITE_HOLD|MNT_MARKED|MNT_INTERNAL); 10435ff9d8a6SEric W. Biederman 10441da177e4SLinus Torvalds atomic_inc(&sb->s_active); 1045b105e270SAl Viro mnt->mnt.mnt_sb = sb; 1046b105e270SAl Viro mnt->mnt.mnt_root = dget(root); 1047a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 10480714a533SAl Viro mnt->mnt_parent = mnt; 1049719ea2fbSAl Viro lock_mount_hash(); 105039f7c4dbSMiklos Szeredi list_add_tail(&mnt->mnt_instance, &sb->s_mounts); 1051719ea2fbSAl Viro unlock_mount_hash(); 1052b90fa9aeSRam Pai 10537a472ef4SEric W. Biederman if ((flag & CL_SLAVE) || 10547a472ef4SEric W. Biederman ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) { 10556776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave_list); 105632301920SAl Viro mnt->mnt_master = old; 1057fc7be130SAl Viro CLEAR_MNT_SHARED(mnt); 10588aec0809SAl Viro } else if (!(flag & CL_PRIVATE)) { 1059fc7be130SAl Viro if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old)) 10606776db3dSAl Viro list_add(&mnt->mnt_share, &old->mnt_share); 1061d10e8defSAl Viro if (IS_MNT_SLAVE(old)) 10626776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave); 1063d10e8defSAl Viro mnt->mnt_master = old->mnt_master; 10645235d448SAl Viro } else { 10655235d448SAl Viro CLEAR_MNT_SHARED(mnt); 10665afe0022SRam Pai } 1067b90fa9aeSRam Pai if (flag & CL_MAKE_SHARED) 10680f0afb1dSAl Viro set_mnt_shared(mnt); 10691da177e4SLinus Torvalds 10701da177e4SLinus Torvalds /* stick the duplicate mount on the same expiry list 10711da177e4SLinus Torvalds * as the original if that was on one */ 107236341f64SRam Pai if (flag & CL_EXPIRE) { 10736776db3dSAl Viro if (!list_empty(&old->mnt_expire)) 10746776db3dSAl Viro list_add(&mnt->mnt_expire, &old->mnt_expire); 10751da177e4SLinus Torvalds } 1076be34d1a3SDavid Howells 1077cb338d06SAl Viro return mnt; 1078719f5d7fSMiklos Szeredi 1079719f5d7fSMiklos Szeredi out_free: 10808ffcb32eSDavid Howells mnt_free_id(mnt); 1081719f5d7fSMiklos Szeredi free_vfsmnt(mnt); 1082be34d1a3SDavid Howells return ERR_PTR(err); 10831da177e4SLinus Torvalds } 10841da177e4SLinus Torvalds 10859ea459e1SAl Viro static void cleanup_mnt(struct mount *mnt) 10869ea459e1SAl Viro { 108756cbb429SAl Viro struct hlist_node *p; 108856cbb429SAl Viro struct mount *m; 10899ea459e1SAl Viro /* 109056cbb429SAl Viro * The warning here probably indicates that somebody messed 109156cbb429SAl Viro * up a mnt_want/drop_write() pair. If this happens, the 109256cbb429SAl Viro * filesystem was probably unable to make r/w->r/o transitions. 10939ea459e1SAl Viro * The locking used to deal with mnt_count decrement provides barriers, 10949ea459e1SAl Viro * so mnt_get_writers() below is safe. 10959ea459e1SAl Viro */ 10969ea459e1SAl Viro WARN_ON(mnt_get_writers(mnt)); 10979ea459e1SAl Viro if (unlikely(mnt->mnt_pins.first)) 10989ea459e1SAl Viro mnt_pin_kill(mnt); 109956cbb429SAl Viro hlist_for_each_entry_safe(m, p, &mnt->mnt_stuck_children, mnt_umount) { 110056cbb429SAl Viro hlist_del(&m->mnt_umount); 110156cbb429SAl Viro mntput(&m->mnt); 110256cbb429SAl Viro } 11039ea459e1SAl Viro fsnotify_vfsmount_delete(&mnt->mnt); 11049ea459e1SAl Viro dput(mnt->mnt.mnt_root); 11059ea459e1SAl Viro deactivate_super(mnt->mnt.mnt_sb); 11069ea459e1SAl Viro mnt_free_id(mnt); 11079ea459e1SAl Viro call_rcu(&mnt->mnt_rcu, delayed_free_vfsmnt); 11089ea459e1SAl Viro } 11099ea459e1SAl Viro 11109ea459e1SAl Viro static void __cleanup_mnt(struct rcu_head *head) 11119ea459e1SAl Viro { 11129ea459e1SAl Viro cleanup_mnt(container_of(head, struct mount, mnt_rcu)); 11139ea459e1SAl Viro } 11149ea459e1SAl Viro 11159ea459e1SAl Viro static LLIST_HEAD(delayed_mntput_list); 11169ea459e1SAl Viro static void delayed_mntput(struct work_struct *unused) 11179ea459e1SAl Viro { 11189ea459e1SAl Viro struct llist_node *node = llist_del_all(&delayed_mntput_list); 111929785735SByungchul Park struct mount *m, *t; 11209ea459e1SAl Viro 112129785735SByungchul Park llist_for_each_entry_safe(m, t, node, mnt_llist) 112229785735SByungchul Park cleanup_mnt(m); 11239ea459e1SAl Viro } 11249ea459e1SAl Viro static DECLARE_DELAYED_WORK(delayed_mntput_work, delayed_mntput); 11259ea459e1SAl Viro 1126900148dcSAl Viro static void mntput_no_expire(struct mount *mnt) 11277b7b1aceSAl Viro { 11284edbe133SAl Viro LIST_HEAD(list); 1129edf7ddbfSEric Biggers int count; 11304edbe133SAl Viro 113148a066e7SAl Viro rcu_read_lock(); 11329ea0a46cSAl Viro if (likely(READ_ONCE(mnt->mnt_ns))) { 11339ea0a46cSAl Viro /* 11349ea0a46cSAl Viro * Since we don't do lock_mount_hash() here, 11359ea0a46cSAl Viro * ->mnt_ns can change under us. However, if it's 11369ea0a46cSAl Viro * non-NULL, then there's a reference that won't 11379ea0a46cSAl Viro * be dropped until after an RCU delay done after 11389ea0a46cSAl Viro * turning ->mnt_ns NULL. So if we observe it 11399ea0a46cSAl Viro * non-NULL under rcu_read_lock(), the reference 11409ea0a46cSAl Viro * we are dropping is not the final one. 11419ea0a46cSAl Viro */ 1142aa9c0e07SAl Viro mnt_add_count(mnt, -1); 114348a066e7SAl Viro rcu_read_unlock(); 114499b7db7bSNick Piggin return; 1145b3e19d92SNick Piggin } 1146719ea2fbSAl Viro lock_mount_hash(); 1147119e1ef8SAl Viro /* 1148119e1ef8SAl Viro * make sure that if __legitimize_mnt() has not seen us grab 1149119e1ef8SAl Viro * mount_lock, we'll see their refcount increment here. 1150119e1ef8SAl Viro */ 1151119e1ef8SAl Viro smp_mb(); 11529ea0a46cSAl Viro mnt_add_count(mnt, -1); 1153edf7ddbfSEric Biggers count = mnt_get_count(mnt); 1154edf7ddbfSEric Biggers if (count != 0) { 1155edf7ddbfSEric Biggers WARN_ON(count < 0); 115648a066e7SAl Viro rcu_read_unlock(); 1157719ea2fbSAl Viro unlock_mount_hash(); 115899b7db7bSNick Piggin return; 115999b7db7bSNick Piggin } 116048a066e7SAl Viro if (unlikely(mnt->mnt.mnt_flags & MNT_DOOMED)) { 116148a066e7SAl Viro rcu_read_unlock(); 116248a066e7SAl Viro unlock_mount_hash(); 116348a066e7SAl Viro return; 116448a066e7SAl Viro } 116548a066e7SAl Viro mnt->mnt.mnt_flags |= MNT_DOOMED; 116648a066e7SAl Viro rcu_read_unlock(); 1167962830dfSAndi Kleen 116839f7c4dbSMiklos Szeredi list_del(&mnt->mnt_instance); 1169ce07d891SEric W. Biederman 1170ce07d891SEric W. Biederman if (unlikely(!list_empty(&mnt->mnt_mounts))) { 1171ce07d891SEric W. Biederman struct mount *p, *tmp; 1172ce07d891SEric W. Biederman list_for_each_entry_safe(p, tmp, &mnt->mnt_mounts, mnt_child) { 11734edbe133SAl Viro __put_mountpoint(unhash_mnt(p), &list); 117456cbb429SAl Viro hlist_add_head(&p->mnt_umount, &mnt->mnt_stuck_children); 1175ce07d891SEric W. Biederman } 1176ce07d891SEric W. Biederman } 1177719ea2fbSAl Viro unlock_mount_hash(); 11784edbe133SAl Viro shrink_dentry_list(&list); 1179649a795aSAl Viro 11809ea459e1SAl Viro if (likely(!(mnt->mnt.mnt_flags & MNT_INTERNAL))) { 11819ea459e1SAl Viro struct task_struct *task = current; 11829ea459e1SAl Viro if (likely(!(task->flags & PF_KTHREAD))) { 11839ea459e1SAl Viro init_task_work(&mnt->mnt_rcu, __cleanup_mnt); 118491989c70SJens Axboe if (!task_work_add(task, &mnt->mnt_rcu, TWA_RESUME)) 11859ea459e1SAl Viro return; 11869ea459e1SAl Viro } 11879ea459e1SAl Viro if (llist_add(&mnt->mnt_llist, &delayed_mntput_list)) 11889ea459e1SAl Viro schedule_delayed_work(&delayed_mntput_work, 1); 11899ea459e1SAl Viro return; 11909ea459e1SAl Viro } 11919ea459e1SAl Viro cleanup_mnt(mnt); 1192b3e19d92SNick Piggin } 1193b3e19d92SNick Piggin 1194b3e19d92SNick Piggin void mntput(struct vfsmount *mnt) 1195b3e19d92SNick Piggin { 1196b3e19d92SNick Piggin if (mnt) { 1197863d684fSAl Viro struct mount *m = real_mount(mnt); 1198b3e19d92SNick Piggin /* avoid cacheline pingpong, hope gcc doesn't get "smart" */ 1199863d684fSAl Viro if (unlikely(m->mnt_expiry_mark)) 1200863d684fSAl Viro m->mnt_expiry_mark = 0; 1201863d684fSAl Viro mntput_no_expire(m); 1202b3e19d92SNick Piggin } 1203b3e19d92SNick Piggin } 1204b3e19d92SNick Piggin EXPORT_SYMBOL(mntput); 1205b3e19d92SNick Piggin 1206b3e19d92SNick Piggin struct vfsmount *mntget(struct vfsmount *mnt) 1207b3e19d92SNick Piggin { 1208b3e19d92SNick Piggin if (mnt) 120983adc753SAl Viro mnt_add_count(real_mount(mnt), 1); 1210b3e19d92SNick Piggin return mnt; 1211b3e19d92SNick Piggin } 1212b3e19d92SNick Piggin EXPORT_SYMBOL(mntget); 1213b3e19d92SNick Piggin 1214c6609c0aSIan Kent /* path_is_mountpoint() - Check if path is a mount in the current 1215c6609c0aSIan Kent * namespace. 1216c6609c0aSIan Kent * 1217c6609c0aSIan Kent * d_mountpoint() can only be used reliably to establish if a dentry is 1218c6609c0aSIan Kent * not mounted in any namespace and that common case is handled inline. 1219c6609c0aSIan Kent * d_mountpoint() isn't aware of the possibility there may be multiple 1220c6609c0aSIan Kent * mounts using a given dentry in a different namespace. This function 1221c6609c0aSIan Kent * checks if the passed in path is a mountpoint rather than the dentry 1222c6609c0aSIan Kent * alone. 1223c6609c0aSIan Kent */ 1224c6609c0aSIan Kent bool path_is_mountpoint(const struct path *path) 1225c6609c0aSIan Kent { 1226c6609c0aSIan Kent unsigned seq; 1227c6609c0aSIan Kent bool res; 1228c6609c0aSIan Kent 1229c6609c0aSIan Kent if (!d_mountpoint(path->dentry)) 1230c6609c0aSIan Kent return false; 1231c6609c0aSIan Kent 1232c6609c0aSIan Kent rcu_read_lock(); 1233c6609c0aSIan Kent do { 1234c6609c0aSIan Kent seq = read_seqbegin(&mount_lock); 1235c6609c0aSIan Kent res = __path_is_mountpoint(path); 1236c6609c0aSIan Kent } while (read_seqretry(&mount_lock, seq)); 1237c6609c0aSIan Kent rcu_read_unlock(); 1238c6609c0aSIan Kent 1239c6609c0aSIan Kent return res; 1240c6609c0aSIan Kent } 1241c6609c0aSIan Kent EXPORT_SYMBOL(path_is_mountpoint); 1242c6609c0aSIan Kent 1243ca71cf71SAl Viro struct vfsmount *mnt_clone_internal(const struct path *path) 12447b7b1aceSAl Viro { 12453064c356SAl Viro struct mount *p; 12463064c356SAl Viro p = clone_mnt(real_mount(path->mnt), path->dentry, CL_PRIVATE); 12473064c356SAl Viro if (IS_ERR(p)) 12483064c356SAl Viro return ERR_CAST(p); 12493064c356SAl Viro p->mnt.mnt_flags |= MNT_INTERNAL; 12503064c356SAl Viro return &p->mnt; 12517b7b1aceSAl Viro } 12521da177e4SLinus Torvalds 1253a1a2c409SMiklos Szeredi #ifdef CONFIG_PROC_FS 12549f6c61f9SMiklos Szeredi static struct mount *mnt_list_next(struct mnt_namespace *ns, 12559f6c61f9SMiklos Szeredi struct list_head *p) 12569f6c61f9SMiklos Szeredi { 12579f6c61f9SMiklos Szeredi struct mount *mnt, *ret = NULL; 12589f6c61f9SMiklos Szeredi 12599f6c61f9SMiklos Szeredi lock_ns_list(ns); 12609f6c61f9SMiklos Szeredi list_for_each_continue(p, &ns->list) { 12619f6c61f9SMiklos Szeredi mnt = list_entry(p, typeof(*mnt), mnt_list); 12629f6c61f9SMiklos Szeredi if (!mnt_is_cursor(mnt)) { 12639f6c61f9SMiklos Szeredi ret = mnt; 12649f6c61f9SMiklos Szeredi break; 12659f6c61f9SMiklos Szeredi } 12669f6c61f9SMiklos Szeredi } 12679f6c61f9SMiklos Szeredi unlock_ns_list(ns); 12689f6c61f9SMiklos Szeredi 12699f6c61f9SMiklos Szeredi return ret; 12709f6c61f9SMiklos Szeredi } 12719f6c61f9SMiklos Szeredi 12720226f492SAl Viro /* iterator; we want it to have access to namespace_sem, thus here... */ 12731da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos) 12741da177e4SLinus Torvalds { 1275ede1bf0dSYann Droneaud struct proc_mounts *p = m->private; 12769f6c61f9SMiklos Szeredi struct list_head *prev; 12771da177e4SLinus Torvalds 1278390c6843SRam Pai down_read(&namespace_sem); 12799f6c61f9SMiklos Szeredi if (!*pos) { 12809f6c61f9SMiklos Szeredi prev = &p->ns->list; 12819f6c61f9SMiklos Szeredi } else { 12829f6c61f9SMiklos Szeredi prev = &p->cursor.mnt_list; 12839f6c61f9SMiklos Szeredi 12849f6c61f9SMiklos Szeredi /* Read after we'd reached the end? */ 12859f6c61f9SMiklos Szeredi if (list_empty(prev)) 12869f6c61f9SMiklos Szeredi return NULL; 1287c7999c36SAl Viro } 1288c7999c36SAl Viro 12899f6c61f9SMiklos Szeredi return mnt_list_next(p->ns, prev); 12901da177e4SLinus Torvalds } 12911da177e4SLinus Torvalds 12921da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos) 12931da177e4SLinus Torvalds { 1294ede1bf0dSYann Droneaud struct proc_mounts *p = m->private; 12959f6c61f9SMiklos Szeredi struct mount *mnt = v; 1296b0765fb8SPavel Emelianov 12979f6c61f9SMiklos Szeredi ++*pos; 12989f6c61f9SMiklos Szeredi return mnt_list_next(p->ns, &mnt->mnt_list); 12991da177e4SLinus Torvalds } 13001da177e4SLinus Torvalds 13011da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v) 13021da177e4SLinus Torvalds { 13039f6c61f9SMiklos Szeredi struct proc_mounts *p = m->private; 13049f6c61f9SMiklos Szeredi struct mount *mnt = v; 13059f6c61f9SMiklos Szeredi 13069f6c61f9SMiklos Szeredi lock_ns_list(p->ns); 13079f6c61f9SMiklos Szeredi if (mnt) 13089f6c61f9SMiklos Szeredi list_move_tail(&p->cursor.mnt_list, &mnt->mnt_list); 13099f6c61f9SMiklos Szeredi else 13109f6c61f9SMiklos Szeredi list_del_init(&p->cursor.mnt_list); 13119f6c61f9SMiklos Szeredi unlock_ns_list(p->ns); 1312390c6843SRam Pai up_read(&namespace_sem); 13131da177e4SLinus Torvalds } 13141da177e4SLinus Torvalds 13150226f492SAl Viro static int m_show(struct seq_file *m, void *v) 13169f5596afSAl Viro { 1317ede1bf0dSYann Droneaud struct proc_mounts *p = m->private; 13189f6c61f9SMiklos Szeredi struct mount *r = v; 13190226f492SAl Viro return p->show(m, &r->mnt); 13201da177e4SLinus Torvalds } 13211da177e4SLinus Torvalds 1322a1a2c409SMiklos Szeredi const struct seq_operations mounts_op = { 13231da177e4SLinus Torvalds .start = m_start, 13241da177e4SLinus Torvalds .next = m_next, 13251da177e4SLinus Torvalds .stop = m_stop, 13260226f492SAl Viro .show = m_show, 1327b4629fe2SChuck Lever }; 13289f6c61f9SMiklos Szeredi 13299f6c61f9SMiklos Szeredi void mnt_cursor_del(struct mnt_namespace *ns, struct mount *cursor) 13309f6c61f9SMiklos Szeredi { 13319f6c61f9SMiklos Szeredi down_read(&namespace_sem); 13329f6c61f9SMiklos Szeredi lock_ns_list(ns); 13339f6c61f9SMiklos Szeredi list_del(&cursor->mnt_list); 13349f6c61f9SMiklos Szeredi unlock_ns_list(ns); 13359f6c61f9SMiklos Szeredi up_read(&namespace_sem); 13369f6c61f9SMiklos Szeredi } 1337a1a2c409SMiklos Szeredi #endif /* CONFIG_PROC_FS */ 1338b4629fe2SChuck Lever 13391da177e4SLinus Torvalds /** 13401da177e4SLinus Torvalds * may_umount_tree - check if a mount tree is busy 13411da177e4SLinus Torvalds * @mnt: root of mount tree 13421da177e4SLinus Torvalds * 13431da177e4SLinus Torvalds * This is called to check if a tree of mounts has any 13441da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts that are 13451da177e4SLinus Torvalds * busy. 13461da177e4SLinus Torvalds */ 1347909b0a88SAl Viro int may_umount_tree(struct vfsmount *m) 13481da177e4SLinus Torvalds { 1349909b0a88SAl Viro struct mount *mnt = real_mount(m); 135036341f64SRam Pai int actual_refs = 0; 135136341f64SRam Pai int minimum_refs = 0; 1352315fc83eSAl Viro struct mount *p; 1353909b0a88SAl Viro BUG_ON(!m); 13541da177e4SLinus Torvalds 1355b3e19d92SNick Piggin /* write lock needed for mnt_get_count */ 1356719ea2fbSAl Viro lock_mount_hash(); 1357909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 135883adc753SAl Viro actual_refs += mnt_get_count(p); 13591da177e4SLinus Torvalds minimum_refs += 2; 13601da177e4SLinus Torvalds } 1361719ea2fbSAl Viro unlock_mount_hash(); 13621da177e4SLinus Torvalds 13631da177e4SLinus Torvalds if (actual_refs > minimum_refs) 13641da177e4SLinus Torvalds return 0; 1365e3474a8eSIan Kent 1366e3474a8eSIan Kent return 1; 13671da177e4SLinus Torvalds } 13681da177e4SLinus Torvalds 13691da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount_tree); 13701da177e4SLinus Torvalds 13711da177e4SLinus Torvalds /** 13721da177e4SLinus Torvalds * may_umount - check if a mount point is busy 13731da177e4SLinus Torvalds * @mnt: root of mount 13741da177e4SLinus Torvalds * 13751da177e4SLinus Torvalds * This is called to check if a mount point has any 13761da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts. If the 13771da177e4SLinus Torvalds * mount has sub mounts this will return busy 13781da177e4SLinus Torvalds * regardless of whether the sub mounts are busy. 13791da177e4SLinus Torvalds * 13801da177e4SLinus Torvalds * Doesn't take quota and stuff into account. IOW, in some cases it will 13811da177e4SLinus Torvalds * give false negatives. The main reason why it's here is that we need 13821da177e4SLinus Torvalds * a non-destructive way to look for easily umountable filesystems. 13831da177e4SLinus Torvalds */ 13841da177e4SLinus Torvalds int may_umount(struct vfsmount *mnt) 13851da177e4SLinus Torvalds { 1386e3474a8eSIan Kent int ret = 1; 13878ad08d8aSAl Viro down_read(&namespace_sem); 1388719ea2fbSAl Viro lock_mount_hash(); 13891ab59738SAl Viro if (propagate_mount_busy(real_mount(mnt), 2)) 1390e3474a8eSIan Kent ret = 0; 1391719ea2fbSAl Viro unlock_mount_hash(); 13928ad08d8aSAl Viro up_read(&namespace_sem); 1393a05964f3SRam Pai return ret; 13941da177e4SLinus Torvalds } 13951da177e4SLinus Torvalds 13961da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount); 13971da177e4SLinus Torvalds 139897216be0SAl Viro static void namespace_unlock(void) 13991da177e4SLinus Torvalds { 1400a3b3c562SEric W. Biederman struct hlist_head head; 140156cbb429SAl Viro struct hlist_node *p; 140256cbb429SAl Viro struct mount *m; 14034edbe133SAl Viro LIST_HEAD(list); 140497216be0SAl Viro 1405a3b3c562SEric W. Biederman hlist_move_list(&unmounted, &head); 14064edbe133SAl Viro list_splice_init(&ex_mountpoints, &list); 1407a3b3c562SEric W. Biederman 140897216be0SAl Viro up_write(&namespace_sem); 1409a3b3c562SEric W. Biederman 14104edbe133SAl Viro shrink_dentry_list(&list); 14114edbe133SAl Viro 1412a3b3c562SEric W. Biederman if (likely(hlist_empty(&head))) 141397216be0SAl Viro return; 141497216be0SAl Viro 141522cb7405SNeilBrown synchronize_rcu_expedited(); 141648a066e7SAl Viro 141756cbb429SAl Viro hlist_for_each_entry_safe(m, p, &head, mnt_umount) { 141856cbb429SAl Viro hlist_del(&m->mnt_umount); 141956cbb429SAl Viro mntput(&m->mnt); 142056cbb429SAl Viro } 142170fbcdf4SRam Pai } 142270fbcdf4SRam Pai 142397216be0SAl Viro static inline void namespace_lock(void) 1424e3197d83SAl Viro { 142597216be0SAl Viro down_write(&namespace_sem); 1426e3197d83SAl Viro } 1427e3197d83SAl Viro 1428e819f152SEric W. Biederman enum umount_tree_flags { 1429e819f152SEric W. Biederman UMOUNT_SYNC = 1, 1430e819f152SEric W. Biederman UMOUNT_PROPAGATE = 2, 1431e0c9c0afSEric W. Biederman UMOUNT_CONNECTED = 4, 1432e819f152SEric W. Biederman }; 1433f2d0a123SEric W. Biederman 1434f2d0a123SEric W. Biederman static bool disconnect_mount(struct mount *mnt, enum umount_tree_flags how) 1435f2d0a123SEric W. Biederman { 1436f2d0a123SEric W. Biederman /* Leaving mounts connected is only valid for lazy umounts */ 1437f2d0a123SEric W. Biederman if (how & UMOUNT_SYNC) 1438f2d0a123SEric W. Biederman return true; 1439f2d0a123SEric W. Biederman 1440f2d0a123SEric W. Biederman /* A mount without a parent has nothing to be connected to */ 1441f2d0a123SEric W. Biederman if (!mnt_has_parent(mnt)) 1442f2d0a123SEric W. Biederman return true; 1443f2d0a123SEric W. Biederman 1444f2d0a123SEric W. Biederman /* Because the reference counting rules change when mounts are 1445f2d0a123SEric W. Biederman * unmounted and connected, umounted mounts may not be 1446f2d0a123SEric W. Biederman * connected to mounted mounts. 1447f2d0a123SEric W. Biederman */ 1448f2d0a123SEric W. Biederman if (!(mnt->mnt_parent->mnt.mnt_flags & MNT_UMOUNT)) 1449f2d0a123SEric W. Biederman return true; 1450f2d0a123SEric W. Biederman 1451f2d0a123SEric W. Biederman /* Has it been requested that the mount remain connected? */ 1452f2d0a123SEric W. Biederman if (how & UMOUNT_CONNECTED) 1453f2d0a123SEric W. Biederman return false; 1454f2d0a123SEric W. Biederman 1455f2d0a123SEric W. Biederman /* Is the mount locked such that it needs to remain connected? */ 1456f2d0a123SEric W. Biederman if (IS_MNT_LOCKED(mnt)) 1457f2d0a123SEric W. Biederman return false; 1458f2d0a123SEric W. Biederman 1459f2d0a123SEric W. Biederman /* By default disconnect the mount */ 1460f2d0a123SEric W. Biederman return true; 1461f2d0a123SEric W. Biederman } 1462f2d0a123SEric W. Biederman 146399b7db7bSNick Piggin /* 146448a066e7SAl Viro * mount_lock must be held 146599b7db7bSNick Piggin * namespace_sem must be held for write 146699b7db7bSNick Piggin */ 1467e819f152SEric W. Biederman static void umount_tree(struct mount *mnt, enum umount_tree_flags how) 146870fbcdf4SRam Pai { 1469c003b26fSEric W. Biederman LIST_HEAD(tmp_list); 1470315fc83eSAl Viro struct mount *p; 147170fbcdf4SRam Pai 14725d88457eSEric W. Biederman if (how & UMOUNT_PROPAGATE) 14735d88457eSEric W. Biederman propagate_mount_unlock(mnt); 14745d88457eSEric W. Biederman 1475c003b26fSEric W. Biederman /* Gather the mounts to umount */ 1476590ce4bcSEric W. Biederman for (p = mnt; p; p = next_mnt(p, mnt)) { 1477590ce4bcSEric W. Biederman p->mnt.mnt_flags |= MNT_UMOUNT; 1478c003b26fSEric W. Biederman list_move(&p->mnt_list, &tmp_list); 1479590ce4bcSEric W. Biederman } 1480c003b26fSEric W. Biederman 1481411a938bSEric W. Biederman /* Hide the mounts from mnt_mounts */ 1482c003b26fSEric W. Biederman list_for_each_entry(p, &tmp_list, mnt_list) { 1483c003b26fSEric W. Biederman list_del_init(&p->mnt_child); 148438129a13SAl Viro } 148570fbcdf4SRam Pai 1486c003b26fSEric W. Biederman /* Add propogated mounts to the tmp_list */ 1487e819f152SEric W. Biederman if (how & UMOUNT_PROPAGATE) 14887b8a53fdSAl Viro propagate_umount(&tmp_list); 1489a05964f3SRam Pai 1490c003b26fSEric W. Biederman while (!list_empty(&tmp_list)) { 1491d2921684SEric W. Biederman struct mnt_namespace *ns; 1492ce07d891SEric W. Biederman bool disconnect; 1493c003b26fSEric W. Biederman p = list_first_entry(&tmp_list, struct mount, mnt_list); 14946776db3dSAl Viro list_del_init(&p->mnt_expire); 14951a4eeaf2SAl Viro list_del_init(&p->mnt_list); 1496d2921684SEric W. Biederman ns = p->mnt_ns; 1497d2921684SEric W. Biederman if (ns) { 1498d2921684SEric W. Biederman ns->mounts--; 1499d2921684SEric W. Biederman __touch_mnt_namespace(ns); 1500d2921684SEric W. Biederman } 150163d37a84SAl Viro p->mnt_ns = NULL; 1502e819f152SEric W. Biederman if (how & UMOUNT_SYNC) 150348a066e7SAl Viro p->mnt.mnt_flags |= MNT_SYNC_UMOUNT; 150487b95ce0SAl Viro 1505f2d0a123SEric W. Biederman disconnect = disconnect_mount(p, how); 1506676da58dSAl Viro if (mnt_has_parent(p)) { 150781b6b061SAl Viro mnt_add_count(p->mnt_parent, -1); 1508ce07d891SEric W. Biederman if (!disconnect) { 1509ce07d891SEric W. Biederman /* Don't forget about p */ 1510ce07d891SEric W. Biederman list_add_tail(&p->mnt_child, &p->mnt_parent->mnt_mounts); 1511ce07d891SEric W. Biederman } else { 15126a46c573SEric W. Biederman umount_mnt(p); 15137c4b93d8SAl Viro } 1514ce07d891SEric W. Biederman } 15150f0afb1dSAl Viro change_mnt_propagation(p, MS_PRIVATE); 151619a1c409SAl Viro if (disconnect) 151719a1c409SAl Viro hlist_add_head(&p->mnt_umount, &unmounted); 151838129a13SAl Viro } 15191da177e4SLinus Torvalds } 15201da177e4SLinus Torvalds 1521b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt); 1522c35038beSAl Viro 15238d0347f6SDavid Howells static int do_umount_root(struct super_block *sb) 15248d0347f6SDavid Howells { 15258d0347f6SDavid Howells int ret = 0; 15268d0347f6SDavid Howells 15278d0347f6SDavid Howells down_write(&sb->s_umount); 15288d0347f6SDavid Howells if (!sb_rdonly(sb)) { 15298d0347f6SDavid Howells struct fs_context *fc; 15308d0347f6SDavid Howells 15318d0347f6SDavid Howells fc = fs_context_for_reconfigure(sb->s_root, SB_RDONLY, 15328d0347f6SDavid Howells SB_RDONLY); 15338d0347f6SDavid Howells if (IS_ERR(fc)) { 15348d0347f6SDavid Howells ret = PTR_ERR(fc); 15358d0347f6SDavid Howells } else { 15368d0347f6SDavid Howells ret = parse_monolithic_mount_data(fc, NULL); 15378d0347f6SDavid Howells if (!ret) 15388d0347f6SDavid Howells ret = reconfigure_super(fc); 15398d0347f6SDavid Howells put_fs_context(fc); 15408d0347f6SDavid Howells } 15418d0347f6SDavid Howells } 15428d0347f6SDavid Howells up_write(&sb->s_umount); 15438d0347f6SDavid Howells return ret; 15448d0347f6SDavid Howells } 15458d0347f6SDavid Howells 15461ab59738SAl Viro static int do_umount(struct mount *mnt, int flags) 15471da177e4SLinus Torvalds { 15481ab59738SAl Viro struct super_block *sb = mnt->mnt.mnt_sb; 15491da177e4SLinus Torvalds int retval; 15501da177e4SLinus Torvalds 15511ab59738SAl Viro retval = security_sb_umount(&mnt->mnt, flags); 15521da177e4SLinus Torvalds if (retval) 15531da177e4SLinus Torvalds return retval; 15541da177e4SLinus Torvalds 15551da177e4SLinus Torvalds /* 15561da177e4SLinus Torvalds * Allow userspace to request a mountpoint be expired rather than 15571da177e4SLinus Torvalds * unmounting unconditionally. Unmount only happens if: 15581da177e4SLinus Torvalds * (1) the mark is already set (the mark is cleared by mntput()) 15591da177e4SLinus Torvalds * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount] 15601da177e4SLinus Torvalds */ 15611da177e4SLinus Torvalds if (flags & MNT_EXPIRE) { 15621ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt || 15631da177e4SLinus Torvalds flags & (MNT_FORCE | MNT_DETACH)) 15641da177e4SLinus Torvalds return -EINVAL; 15651da177e4SLinus Torvalds 1566b3e19d92SNick Piggin /* 1567b3e19d92SNick Piggin * probably don't strictly need the lock here if we examined 1568b3e19d92SNick Piggin * all race cases, but it's a slowpath. 1569b3e19d92SNick Piggin */ 1570719ea2fbSAl Viro lock_mount_hash(); 157183adc753SAl Viro if (mnt_get_count(mnt) != 2) { 1572719ea2fbSAl Viro unlock_mount_hash(); 15731da177e4SLinus Torvalds return -EBUSY; 1574b3e19d92SNick Piggin } 1575719ea2fbSAl Viro unlock_mount_hash(); 15761da177e4SLinus Torvalds 1577863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1)) 15781da177e4SLinus Torvalds return -EAGAIN; 15791da177e4SLinus Torvalds } 15801da177e4SLinus Torvalds 15811da177e4SLinus Torvalds /* 15821da177e4SLinus Torvalds * If we may have to abort operations to get out of this 15831da177e4SLinus Torvalds * mount, and they will themselves hold resources we must 15841da177e4SLinus Torvalds * allow the fs to do things. In the Unix tradition of 15851da177e4SLinus Torvalds * 'Gee thats tricky lets do it in userspace' the umount_begin 15861da177e4SLinus Torvalds * might fail to complete on the first run through as other tasks 15871da177e4SLinus Torvalds * must return, and the like. Thats for the mount program to worry 15881da177e4SLinus Torvalds * about for the moment. 15891da177e4SLinus Torvalds */ 15901da177e4SLinus Torvalds 159142faad99SAl Viro if (flags & MNT_FORCE && sb->s_op->umount_begin) { 159242faad99SAl Viro sb->s_op->umount_begin(sb); 159342faad99SAl Viro } 15941da177e4SLinus Torvalds 15951da177e4SLinus Torvalds /* 15961da177e4SLinus Torvalds * No sense to grab the lock for this test, but test itself looks 15971da177e4SLinus Torvalds * somewhat bogus. Suggestions for better replacement? 15981da177e4SLinus Torvalds * Ho-hum... In principle, we might treat that as umount + switch 15991da177e4SLinus Torvalds * to rootfs. GC would eventually take care of the old vfsmount. 16001da177e4SLinus Torvalds * Actually it makes sense, especially if rootfs would contain a 16011da177e4SLinus Torvalds * /reboot - static binary that would close all descriptors and 16021da177e4SLinus Torvalds * call reboot(9). Then init(8) could umount root and exec /reboot. 16031da177e4SLinus Torvalds */ 16041ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) { 16051da177e4SLinus Torvalds /* 16061da177e4SLinus Torvalds * Special case for "unmounting" root ... 16071da177e4SLinus Torvalds * we just try to remount it readonly. 16081da177e4SLinus Torvalds */ 1609bc6155d1SEric W. Biederman if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN)) 1610a1480dccSAndy Lutomirski return -EPERM; 16118d0347f6SDavid Howells return do_umount_root(sb); 16121da177e4SLinus Torvalds } 16131da177e4SLinus Torvalds 161497216be0SAl Viro namespace_lock(); 1615719ea2fbSAl Viro lock_mount_hash(); 16161da177e4SLinus Torvalds 161725d202edSEric W. Biederman /* Recheck MNT_LOCKED with the locks held */ 161825d202edSEric W. Biederman retval = -EINVAL; 161925d202edSEric W. Biederman if (mnt->mnt.mnt_flags & MNT_LOCKED) 162025d202edSEric W. Biederman goto out; 162125d202edSEric W. Biederman 162225d202edSEric W. Biederman event++; 162348a066e7SAl Viro if (flags & MNT_DETACH) { 162448a066e7SAl Viro if (!list_empty(&mnt->mnt_list)) 1625e819f152SEric W. Biederman umount_tree(mnt, UMOUNT_PROPAGATE); 162648a066e7SAl Viro retval = 0; 162748a066e7SAl Viro } else { 1628b54b9be7SAl Viro shrink_submounts(mnt); 16291da177e4SLinus Torvalds retval = -EBUSY; 163048a066e7SAl Viro if (!propagate_mount_busy(mnt, 2)) { 16311a4eeaf2SAl Viro if (!list_empty(&mnt->mnt_list)) 1632e819f152SEric W. Biederman umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC); 16331da177e4SLinus Torvalds retval = 0; 16341da177e4SLinus Torvalds } 163548a066e7SAl Viro } 163625d202edSEric W. Biederman out: 1637719ea2fbSAl Viro unlock_mount_hash(); 1638e3197d83SAl Viro namespace_unlock(); 16391da177e4SLinus Torvalds return retval; 16401da177e4SLinus Torvalds } 16411da177e4SLinus Torvalds 16421da177e4SLinus Torvalds /* 164380b5dce8SEric W. Biederman * __detach_mounts - lazily unmount all mounts on the specified dentry 164480b5dce8SEric W. Biederman * 164580b5dce8SEric W. Biederman * During unlink, rmdir, and d_drop it is possible to loose the path 164680b5dce8SEric W. Biederman * to an existing mountpoint, and wind up leaking the mount. 164780b5dce8SEric W. Biederman * detach_mounts allows lazily unmounting those mounts instead of 164880b5dce8SEric W. Biederman * leaking them. 164980b5dce8SEric W. Biederman * 165080b5dce8SEric W. Biederman * The caller may hold dentry->d_inode->i_mutex. 165180b5dce8SEric W. Biederman */ 165280b5dce8SEric W. Biederman void __detach_mounts(struct dentry *dentry) 165380b5dce8SEric W. Biederman { 165480b5dce8SEric W. Biederman struct mountpoint *mp; 165580b5dce8SEric W. Biederman struct mount *mnt; 165680b5dce8SEric W. Biederman 165780b5dce8SEric W. Biederman namespace_lock(); 16583895dbf8SEric W. Biederman lock_mount_hash(); 165980b5dce8SEric W. Biederman mp = lookup_mountpoint(dentry); 1660adc9b5c0SAl Viro if (!mp) 166180b5dce8SEric W. Biederman goto out_unlock; 166280b5dce8SEric W. Biederman 1663e06b933eSAndrey Ulanov event++; 166480b5dce8SEric W. Biederman while (!hlist_empty(&mp->m_list)) { 166580b5dce8SEric W. Biederman mnt = hlist_entry(mp->m_list.first, struct mount, mnt_mp_list); 1666ce07d891SEric W. Biederman if (mnt->mnt.mnt_flags & MNT_UMOUNT) { 1667fe78fcc8SEric W. Biederman umount_mnt(mnt); 166856cbb429SAl Viro hlist_add_head(&mnt->mnt_umount, &unmounted); 1669ce07d891SEric W. Biederman } 1670e0c9c0afSEric W. Biederman else umount_tree(mnt, UMOUNT_CONNECTED); 167180b5dce8SEric W. Biederman } 167280b5dce8SEric W. Biederman put_mountpoint(mp); 167380b5dce8SEric W. Biederman out_unlock: 16743895dbf8SEric W. Biederman unlock_mount_hash(); 167580b5dce8SEric W. Biederman namespace_unlock(); 167680b5dce8SEric W. Biederman } 167780b5dce8SEric W. Biederman 167880b5dce8SEric W. Biederman /* 16799b40bc90SAl Viro * Is the caller allowed to modify his namespace? 16809b40bc90SAl Viro */ 16819b40bc90SAl Viro static inline bool may_mount(void) 16829b40bc90SAl Viro { 16839b40bc90SAl Viro return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN); 16849b40bc90SAl Viro } 16859b40bc90SAl Viro 1686df2474a2SJeff Layton #ifdef CONFIG_MANDATORY_FILE_LOCKING 16879e8925b6SJeff Layton static inline bool may_mandlock(void) 16889e8925b6SJeff Layton { 168995ace754SEric W. Biederman return capable(CAP_SYS_ADMIN); 16909e8925b6SJeff Layton } 1691df2474a2SJeff Layton #else 1692df2474a2SJeff Layton static inline bool may_mandlock(void) 1693df2474a2SJeff Layton { 1694df2474a2SJeff Layton pr_warn("VFS: \"mand\" mount option not supported"); 1695df2474a2SJeff Layton return false; 1696df2474a2SJeff Layton } 1697df2474a2SJeff Layton #endif 16989e8925b6SJeff Layton 169925ccd24fSChristoph Hellwig static int can_umount(const struct path *path, int flags) 17001da177e4SLinus Torvalds { 170125ccd24fSChristoph Hellwig struct mount *mnt = real_mount(path->mnt); 17021da177e4SLinus Torvalds 1703db1f05bbSMiklos Szeredi if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW)) 1704db1f05bbSMiklos Szeredi return -EINVAL; 17059b40bc90SAl Viro if (!may_mount()) 17069b40bc90SAl Viro return -EPERM; 170741525f56SChristoph Hellwig if (path->dentry != path->mnt->mnt_root) 170825ccd24fSChristoph Hellwig return -EINVAL; 1709143c8c91SAl Viro if (!check_mnt(mnt)) 171025ccd24fSChristoph Hellwig return -EINVAL; 171125d202edSEric W. Biederman if (mnt->mnt.mnt_flags & MNT_LOCKED) /* Check optimistically */ 171225ccd24fSChristoph Hellwig return -EINVAL; 1713b2f5d4dcSEric W. Biederman if (flags & MNT_FORCE && !capable(CAP_SYS_ADMIN)) 171425ccd24fSChristoph Hellwig return -EPERM; 171525ccd24fSChristoph Hellwig return 0; 171625ccd24fSChristoph Hellwig } 17171da177e4SLinus Torvalds 171825ccd24fSChristoph Hellwig int path_umount(struct path *path, int flags) 171925ccd24fSChristoph Hellwig { 172025ccd24fSChristoph Hellwig struct mount *mnt = real_mount(path->mnt); 172125ccd24fSChristoph Hellwig int ret; 172225ccd24fSChristoph Hellwig 172325ccd24fSChristoph Hellwig ret = can_umount(path, flags); 172425ccd24fSChristoph Hellwig if (!ret) 172525ccd24fSChristoph Hellwig ret = do_umount(mnt, flags); 172625ccd24fSChristoph Hellwig 1727429731b1SJan Blunck /* we mustn't call path_put() as that would clear mnt_expiry_mark */ 172841525f56SChristoph Hellwig dput(path->dentry); 1729900148dcSAl Viro mntput_no_expire(mnt); 173025ccd24fSChristoph Hellwig return ret; 17311da177e4SLinus Torvalds } 17321da177e4SLinus Torvalds 173309267defSChristoph Hellwig static int ksys_umount(char __user *name, int flags) 173441525f56SChristoph Hellwig { 173541525f56SChristoph Hellwig int lookup_flags = LOOKUP_MOUNTPOINT; 173641525f56SChristoph Hellwig struct path path; 173741525f56SChristoph Hellwig int ret; 173841525f56SChristoph Hellwig 173941525f56SChristoph Hellwig if (!(flags & UMOUNT_NOFOLLOW)) 174041525f56SChristoph Hellwig lookup_flags |= LOOKUP_FOLLOW; 174141525f56SChristoph Hellwig ret = user_path_at(AT_FDCWD, name, lookup_flags, &path); 174241525f56SChristoph Hellwig if (ret) 174341525f56SChristoph Hellwig return ret; 174441525f56SChristoph Hellwig return path_umount(&path, flags); 174541525f56SChristoph Hellwig } 174641525f56SChristoph Hellwig 17473a18ef5cSDominik Brodowski SYSCALL_DEFINE2(umount, char __user *, name, int, flags) 17483a18ef5cSDominik Brodowski { 17493a18ef5cSDominik Brodowski return ksys_umount(name, flags); 17503a18ef5cSDominik Brodowski } 17513a18ef5cSDominik Brodowski 17521da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT 17531da177e4SLinus Torvalds 17541da177e4SLinus Torvalds /* 17551da177e4SLinus Torvalds * The 2.0 compatible umount. No flags. 17561da177e4SLinus Torvalds */ 1757bdc480e3SHeiko Carstens SYSCALL_DEFINE1(oldumount, char __user *, name) 17581da177e4SLinus Torvalds { 17593a18ef5cSDominik Brodowski return ksys_umount(name, 0); 17601da177e4SLinus Torvalds } 17611da177e4SLinus Torvalds 17621da177e4SLinus Torvalds #endif 17631da177e4SLinus Torvalds 17644ce5d2b1SEric W. Biederman static bool is_mnt_ns_file(struct dentry *dentry) 17658823c079SEric W. Biederman { 17664ce5d2b1SEric W. Biederman /* Is this a proxy for a mount namespace? */ 1767e149ed2bSAl Viro return dentry->d_op == &ns_dentry_operations && 1768e149ed2bSAl Viro dentry->d_fsdata == &mntns_operations; 17694ce5d2b1SEric W. Biederman } 17704ce5d2b1SEric W. Biederman 1771213921f9SEric Biggers static struct mnt_namespace *to_mnt_ns(struct ns_common *ns) 177258be2825SAl Viro { 177358be2825SAl Viro return container_of(ns, struct mnt_namespace, ns); 177458be2825SAl Viro } 177558be2825SAl Viro 1776303cc571SChristian Brauner struct ns_common *from_mnt_ns(struct mnt_namespace *mnt) 1777303cc571SChristian Brauner { 1778303cc571SChristian Brauner return &mnt->ns; 1779303cc571SChristian Brauner } 1780303cc571SChristian Brauner 17814ce5d2b1SEric W. Biederman static bool mnt_ns_loop(struct dentry *dentry) 17824ce5d2b1SEric W. Biederman { 17834ce5d2b1SEric W. Biederman /* Could bind mounting the mount namespace inode cause a 17844ce5d2b1SEric W. Biederman * mount namespace loop? 17854ce5d2b1SEric W. Biederman */ 17864ce5d2b1SEric W. Biederman struct mnt_namespace *mnt_ns; 17874ce5d2b1SEric W. Biederman if (!is_mnt_ns_file(dentry)) 17884ce5d2b1SEric W. Biederman return false; 17894ce5d2b1SEric W. Biederman 1790f77c8014SAl Viro mnt_ns = to_mnt_ns(get_proc_ns(dentry->d_inode)); 17918823c079SEric W. Biederman return current->nsproxy->mnt_ns->seq >= mnt_ns->seq; 17928823c079SEric W. Biederman } 17938823c079SEric W. Biederman 179487129cc0SAl Viro struct mount *copy_tree(struct mount *mnt, struct dentry *dentry, 179536341f64SRam Pai int flag) 17961da177e4SLinus Torvalds { 179784d17192SAl Viro struct mount *res, *p, *q, *r, *parent; 17981da177e4SLinus Torvalds 17994ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt)) 18004ce5d2b1SEric W. Biederman return ERR_PTR(-EINVAL); 18014ce5d2b1SEric W. Biederman 18024ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry)) 1803be34d1a3SDavid Howells return ERR_PTR(-EINVAL); 18049676f0c6SRam Pai 180536341f64SRam Pai res = q = clone_mnt(mnt, dentry, flag); 1806be34d1a3SDavid Howells if (IS_ERR(q)) 1807be34d1a3SDavid Howells return q; 1808be34d1a3SDavid Howells 1809a73324daSAl Viro q->mnt_mountpoint = mnt->mnt_mountpoint; 18101da177e4SLinus Torvalds 18111da177e4SLinus Torvalds p = mnt; 18126b41d536SAl Viro list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) { 1813315fc83eSAl Viro struct mount *s; 18147ec02ef1SJan Blunck if (!is_subdir(r->mnt_mountpoint, dentry)) 18151da177e4SLinus Torvalds continue; 18161da177e4SLinus Torvalds 1817909b0a88SAl Viro for (s = r; s; s = next_mnt(s, r)) { 18184ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_UNBINDABLE) && 18194ce5d2b1SEric W. Biederman IS_MNT_UNBINDABLE(s)) { 1820df7342b2SEric W. Biederman if (s->mnt.mnt_flags & MNT_LOCKED) { 1821df7342b2SEric W. Biederman /* Both unbindable and locked. */ 1822df7342b2SEric W. Biederman q = ERR_PTR(-EPERM); 1823df7342b2SEric W. Biederman goto out; 1824df7342b2SEric W. Biederman } else { 18254ce5d2b1SEric W. Biederman s = skip_mnt_tree(s); 18264ce5d2b1SEric W. Biederman continue; 18274ce5d2b1SEric W. Biederman } 1828df7342b2SEric W. Biederman } 18294ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_MNT_NS_FILE) && 18304ce5d2b1SEric W. Biederman is_mnt_ns_file(s->mnt.mnt_root)) { 18319676f0c6SRam Pai s = skip_mnt_tree(s); 18329676f0c6SRam Pai continue; 18339676f0c6SRam Pai } 18340714a533SAl Viro while (p != s->mnt_parent) { 18350714a533SAl Viro p = p->mnt_parent; 18360714a533SAl Viro q = q->mnt_parent; 18371da177e4SLinus Torvalds } 183887129cc0SAl Viro p = s; 183984d17192SAl Viro parent = q; 184087129cc0SAl Viro q = clone_mnt(p, p->mnt.mnt_root, flag); 1841be34d1a3SDavid Howells if (IS_ERR(q)) 1842be34d1a3SDavid Howells goto out; 1843719ea2fbSAl Viro lock_mount_hash(); 18441a4eeaf2SAl Viro list_add_tail(&q->mnt_list, &res->mnt_list); 18451064f874SEric W. Biederman attach_mnt(q, parent, p->mnt_mp); 1846719ea2fbSAl Viro unlock_mount_hash(); 18471da177e4SLinus Torvalds } 18481da177e4SLinus Torvalds } 18491da177e4SLinus Torvalds return res; 1850be34d1a3SDavid Howells out: 18511da177e4SLinus Torvalds if (res) { 1852719ea2fbSAl Viro lock_mount_hash(); 1853e819f152SEric W. Biederman umount_tree(res, UMOUNT_SYNC); 1854719ea2fbSAl Viro unlock_mount_hash(); 18551da177e4SLinus Torvalds } 1856be34d1a3SDavid Howells return q; 18571da177e4SLinus Torvalds } 18581da177e4SLinus Torvalds 1859be34d1a3SDavid Howells /* Caller should check returned pointer for errors */ 1860be34d1a3SDavid Howells 1861ca71cf71SAl Viro struct vfsmount *collect_mounts(const struct path *path) 18628aec0809SAl Viro { 1863cb338d06SAl Viro struct mount *tree; 186497216be0SAl Viro namespace_lock(); 1865cd4a4017SEric W. Biederman if (!check_mnt(real_mount(path->mnt))) 1866cd4a4017SEric W. Biederman tree = ERR_PTR(-EINVAL); 1867cd4a4017SEric W. Biederman else 186887129cc0SAl Viro tree = copy_tree(real_mount(path->mnt), path->dentry, 186987129cc0SAl Viro CL_COPY_ALL | CL_PRIVATE); 1870328e6d90SAl Viro namespace_unlock(); 1871be34d1a3SDavid Howells if (IS_ERR(tree)) 187252e220d3SDan Carpenter return ERR_CAST(tree); 1873be34d1a3SDavid Howells return &tree->mnt; 18748aec0809SAl Viro } 18758aec0809SAl Viro 1876a07b2000SAl Viro static void free_mnt_ns(struct mnt_namespace *); 1877a07b2000SAl Viro static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *, bool); 1878a07b2000SAl Viro 1879a07b2000SAl Viro void dissolve_on_fput(struct vfsmount *mnt) 1880a07b2000SAl Viro { 1881a07b2000SAl Viro struct mnt_namespace *ns; 1882a07b2000SAl Viro namespace_lock(); 1883a07b2000SAl Viro lock_mount_hash(); 1884a07b2000SAl Viro ns = real_mount(mnt)->mnt_ns; 188544dfd84aSDavid Howells if (ns) { 188644dfd84aSDavid Howells if (is_anon_ns(ns)) 1887a07b2000SAl Viro umount_tree(real_mount(mnt), UMOUNT_CONNECTED); 188844dfd84aSDavid Howells else 188944dfd84aSDavid Howells ns = NULL; 189044dfd84aSDavid Howells } 1891a07b2000SAl Viro unlock_mount_hash(); 1892a07b2000SAl Viro namespace_unlock(); 189344dfd84aSDavid Howells if (ns) 1894a07b2000SAl Viro free_mnt_ns(ns); 1895a07b2000SAl Viro } 1896a07b2000SAl Viro 18978aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt) 18988aec0809SAl Viro { 189997216be0SAl Viro namespace_lock(); 1900719ea2fbSAl Viro lock_mount_hash(); 19019c8e0a1bSEric W. Biederman umount_tree(real_mount(mnt), 0); 1902719ea2fbSAl Viro unlock_mount_hash(); 19033ab6abeeSAl Viro namespace_unlock(); 19048aec0809SAl Viro } 19058aec0809SAl Viro 1906c771d683SMiklos Szeredi /** 1907c771d683SMiklos Szeredi * clone_private_mount - create a private clone of a path 1908c771d683SMiklos Szeredi * 1909c771d683SMiklos Szeredi * This creates a new vfsmount, which will be the clone of @path. The new will 1910c771d683SMiklos Szeredi * not be attached anywhere in the namespace and will be private (i.e. changes 1911c771d683SMiklos Szeredi * to the originating mount won't be propagated into this). 1912c771d683SMiklos Szeredi * 1913c771d683SMiklos Szeredi * Release with mntput(). 1914c771d683SMiklos Szeredi */ 1915ca71cf71SAl Viro struct vfsmount *clone_private_mount(const struct path *path) 1916c771d683SMiklos Szeredi { 1917c771d683SMiklos Szeredi struct mount *old_mnt = real_mount(path->mnt); 1918c771d683SMiklos Szeredi struct mount *new_mnt; 1919c771d683SMiklos Szeredi 1920c771d683SMiklos Szeredi if (IS_MNT_UNBINDABLE(old_mnt)) 1921c771d683SMiklos Szeredi return ERR_PTR(-EINVAL); 1922c771d683SMiklos Szeredi 1923c771d683SMiklos Szeredi new_mnt = clone_mnt(old_mnt, path->dentry, CL_PRIVATE); 1924c771d683SMiklos Szeredi if (IS_ERR(new_mnt)) 1925c771d683SMiklos Szeredi return ERR_CAST(new_mnt); 1926c771d683SMiklos Szeredi 1927df820f8dSMiklos Szeredi /* Longterm mount to be removed by kern_unmount*() */ 1928df820f8dSMiklos Szeredi new_mnt->mnt_ns = MNT_NS_INTERNAL; 1929df820f8dSMiklos Szeredi 1930c771d683SMiklos Szeredi return &new_mnt->mnt; 1931c771d683SMiklos Szeredi } 1932c771d683SMiklos Szeredi EXPORT_SYMBOL_GPL(clone_private_mount); 1933c771d683SMiklos Szeredi 19341f707137SAl Viro int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg, 19351f707137SAl Viro struct vfsmount *root) 19361f707137SAl Viro { 19371a4eeaf2SAl Viro struct mount *mnt; 19381f707137SAl Viro int res = f(root, arg); 19391f707137SAl Viro if (res) 19401f707137SAl Viro return res; 19411a4eeaf2SAl Viro list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) { 19421a4eeaf2SAl Viro res = f(&mnt->mnt, arg); 19431f707137SAl Viro if (res) 19441f707137SAl Viro return res; 19451f707137SAl Viro } 19461f707137SAl Viro return 0; 19471f707137SAl Viro } 19481f707137SAl Viro 19493bd045ccSAl Viro static void lock_mnt_tree(struct mount *mnt) 19503bd045ccSAl Viro { 19513bd045ccSAl Viro struct mount *p; 19523bd045ccSAl Viro 19533bd045ccSAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 19543bd045ccSAl Viro int flags = p->mnt.mnt_flags; 19553bd045ccSAl Viro /* Don't allow unprivileged users to change mount flags */ 19563bd045ccSAl Viro flags |= MNT_LOCK_ATIME; 19573bd045ccSAl Viro 19583bd045ccSAl Viro if (flags & MNT_READONLY) 19593bd045ccSAl Viro flags |= MNT_LOCK_READONLY; 19603bd045ccSAl Viro 19613bd045ccSAl Viro if (flags & MNT_NODEV) 19623bd045ccSAl Viro flags |= MNT_LOCK_NODEV; 19633bd045ccSAl Viro 19643bd045ccSAl Viro if (flags & MNT_NOSUID) 19653bd045ccSAl Viro flags |= MNT_LOCK_NOSUID; 19663bd045ccSAl Viro 19673bd045ccSAl Viro if (flags & MNT_NOEXEC) 19683bd045ccSAl Viro flags |= MNT_LOCK_NOEXEC; 19693bd045ccSAl Viro /* Don't allow unprivileged users to reveal what is under a mount */ 19703bd045ccSAl Viro if (list_empty(&p->mnt_expire)) 19713bd045ccSAl Viro flags |= MNT_LOCKED; 19723bd045ccSAl Viro p->mnt.mnt_flags = flags; 19733bd045ccSAl Viro } 19743bd045ccSAl Viro } 19753bd045ccSAl Viro 19764b8b21f4SAl Viro static void cleanup_group_ids(struct mount *mnt, struct mount *end) 1977719f5d7fSMiklos Szeredi { 1978315fc83eSAl Viro struct mount *p; 1979719f5d7fSMiklos Szeredi 1980909b0a88SAl Viro for (p = mnt; p != end; p = next_mnt(p, mnt)) { 1981fc7be130SAl Viro if (p->mnt_group_id && !IS_MNT_SHARED(p)) 19824b8b21f4SAl Viro mnt_release_group_id(p); 1983719f5d7fSMiklos Szeredi } 1984719f5d7fSMiklos Szeredi } 1985719f5d7fSMiklos Szeredi 19864b8b21f4SAl Viro static int invent_group_ids(struct mount *mnt, bool recurse) 1987719f5d7fSMiklos Szeredi { 1988315fc83eSAl Viro struct mount *p; 1989719f5d7fSMiklos Szeredi 1990909b0a88SAl Viro for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) { 1991fc7be130SAl Viro if (!p->mnt_group_id && !IS_MNT_SHARED(p)) { 19924b8b21f4SAl Viro int err = mnt_alloc_group_id(p); 1993719f5d7fSMiklos Szeredi if (err) { 19944b8b21f4SAl Viro cleanup_group_ids(mnt, p); 1995719f5d7fSMiklos Szeredi return err; 1996719f5d7fSMiklos Szeredi } 1997719f5d7fSMiklos Szeredi } 1998719f5d7fSMiklos Szeredi } 1999719f5d7fSMiklos Szeredi 2000719f5d7fSMiklos Szeredi return 0; 2001719f5d7fSMiklos Szeredi } 2002719f5d7fSMiklos Szeredi 2003d2921684SEric W. Biederman int count_mounts(struct mnt_namespace *ns, struct mount *mnt) 2004d2921684SEric W. Biederman { 2005d2921684SEric W. Biederman unsigned int max = READ_ONCE(sysctl_mount_max); 2006d2921684SEric W. Biederman unsigned int mounts = 0, old, pending, sum; 2007d2921684SEric W. Biederman struct mount *p; 2008d2921684SEric W. Biederman 2009d2921684SEric W. Biederman for (p = mnt; p; p = next_mnt(p, mnt)) 2010d2921684SEric W. Biederman mounts++; 2011d2921684SEric W. Biederman 2012d2921684SEric W. Biederman old = ns->mounts; 2013d2921684SEric W. Biederman pending = ns->pending_mounts; 2014d2921684SEric W. Biederman sum = old + pending; 2015d2921684SEric W. Biederman if ((old > sum) || 2016d2921684SEric W. Biederman (pending > sum) || 2017d2921684SEric W. Biederman (max < sum) || 2018d2921684SEric W. Biederman (mounts > (max - sum))) 2019d2921684SEric W. Biederman return -ENOSPC; 2020d2921684SEric W. Biederman 2021d2921684SEric W. Biederman ns->pending_mounts = pending + mounts; 2022d2921684SEric W. Biederman return 0; 2023d2921684SEric W. Biederman } 2024d2921684SEric W. Biederman 2025b90fa9aeSRam Pai /* 2026b90fa9aeSRam Pai * @source_mnt : mount tree to be attached 2027b90fa9aeSRam Pai * @nd : place the mount tree @source_mnt is attached 202821444403SRam Pai * @parent_nd : if non-null, detach the source_mnt from its parent and 202921444403SRam Pai * store the parent mount and mountpoint dentry. 203021444403SRam Pai * (done when source_mnt is moved) 2031b90fa9aeSRam Pai * 2032b90fa9aeSRam Pai * NOTE: in the table below explains the semantics when a source mount 2033b90fa9aeSRam Pai * of a given type is attached to a destination mount of a given type. 20349676f0c6SRam Pai * --------------------------------------------------------------------------- 2035b90fa9aeSRam Pai * | BIND MOUNT OPERATION | 20369676f0c6SRam Pai * |************************************************************************** 20379676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 20389676f0c6SRam Pai * | dest | | | | | 20399676f0c6SRam Pai * | | | | | | | 20409676f0c6SRam Pai * | v | | | | | 20419676f0c6SRam Pai * |************************************************************************** 20429676f0c6SRam Pai * | shared | shared (++) | shared (+) | shared(+++)| invalid | 20435afe0022SRam Pai * | | | | | | 20449676f0c6SRam Pai * |non-shared| shared (+) | private | slave (*) | invalid | 20459676f0c6SRam Pai * *************************************************************************** 2046b90fa9aeSRam Pai * A bind operation clones the source mount and mounts the clone on the 2047b90fa9aeSRam Pai * destination mount. 2048b90fa9aeSRam Pai * 2049b90fa9aeSRam Pai * (++) the cloned mount is propagated to all the mounts in the propagation 2050b90fa9aeSRam Pai * tree of the destination mount and the cloned mount is added to 2051b90fa9aeSRam Pai * the peer group of the source mount. 2052b90fa9aeSRam Pai * (+) the cloned mount is created under the destination mount and is marked 2053b90fa9aeSRam Pai * as shared. The cloned mount is added to the peer group of the source 2054b90fa9aeSRam Pai * mount. 20555afe0022SRam Pai * (+++) the mount is propagated to all the mounts in the propagation tree 20565afe0022SRam Pai * of the destination mount and the cloned mount is made slave 20575afe0022SRam Pai * of the same master as that of the source mount. The cloned mount 20585afe0022SRam Pai * is marked as 'shared and slave'. 20595afe0022SRam Pai * (*) the cloned mount is made a slave of the same master as that of the 20605afe0022SRam Pai * source mount. 20615afe0022SRam Pai * 20629676f0c6SRam Pai * --------------------------------------------------------------------------- 206321444403SRam Pai * | MOVE MOUNT OPERATION | 20649676f0c6SRam Pai * |************************************************************************** 20659676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 20669676f0c6SRam Pai * | dest | | | | | 20679676f0c6SRam Pai * | | | | | | | 20689676f0c6SRam Pai * | v | | | | | 20699676f0c6SRam Pai * |************************************************************************** 20709676f0c6SRam Pai * | shared | shared (+) | shared (+) | shared(+++) | invalid | 20715afe0022SRam Pai * | | | | | | 20729676f0c6SRam Pai * |non-shared| shared (+*) | private | slave (*) | unbindable | 20739676f0c6SRam Pai * *************************************************************************** 20745afe0022SRam Pai * 20755afe0022SRam Pai * (+) the mount is moved to the destination. And is then propagated to 20765afe0022SRam Pai * all the mounts in the propagation tree of the destination mount. 207721444403SRam Pai * (+*) the mount is moved to the destination. 20785afe0022SRam Pai * (+++) the mount is moved to the destination and is then propagated to 20795afe0022SRam Pai * all the mounts belonging to the destination mount's propagation tree. 20805afe0022SRam Pai * the mount is marked as 'shared and slave'. 20815afe0022SRam Pai * (*) the mount continues to be a slave at the new location. 2082b90fa9aeSRam Pai * 2083b90fa9aeSRam Pai * if the source mount is a tree, the operations explained above is 2084b90fa9aeSRam Pai * applied to each mount in the tree. 2085b90fa9aeSRam Pai * Must be called without spinlocks held, since this function can sleep 2086b90fa9aeSRam Pai * in allocations. 2087b90fa9aeSRam Pai */ 20880fb54e50SAl Viro static int attach_recursive_mnt(struct mount *source_mnt, 208984d17192SAl Viro struct mount *dest_mnt, 209084d17192SAl Viro struct mountpoint *dest_mp, 20912763d119SAl Viro bool moving) 2092b90fa9aeSRam Pai { 20933bd045ccSAl Viro struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns; 209438129a13SAl Viro HLIST_HEAD(tree_list); 2095d2921684SEric W. Biederman struct mnt_namespace *ns = dest_mnt->mnt_ns; 20961064f874SEric W. Biederman struct mountpoint *smp; 2097315fc83eSAl Viro struct mount *child, *p; 209838129a13SAl Viro struct hlist_node *n; 2099719f5d7fSMiklos Szeredi int err; 2100b90fa9aeSRam Pai 21011064f874SEric W. Biederman /* Preallocate a mountpoint in case the new mounts need 21021064f874SEric W. Biederman * to be tucked under other mounts. 21031064f874SEric W. Biederman */ 21041064f874SEric W. Biederman smp = get_mountpoint(source_mnt->mnt.mnt_root); 21051064f874SEric W. Biederman if (IS_ERR(smp)) 21061064f874SEric W. Biederman return PTR_ERR(smp); 21071064f874SEric W. Biederman 2108d2921684SEric W. Biederman /* Is there space to add these mounts to the mount namespace? */ 21092763d119SAl Viro if (!moving) { 2110d2921684SEric W. Biederman err = count_mounts(ns, source_mnt); 2111d2921684SEric W. Biederman if (err) 2112d2921684SEric W. Biederman goto out; 2113d2921684SEric W. Biederman } 2114d2921684SEric W. Biederman 2115fc7be130SAl Viro if (IS_MNT_SHARED(dest_mnt)) { 21160fb54e50SAl Viro err = invent_group_ids(source_mnt, true); 2117719f5d7fSMiklos Szeredi if (err) 2118719f5d7fSMiklos Szeredi goto out; 211984d17192SAl Viro err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list); 2120f2ebb3a9SAl Viro lock_mount_hash(); 2121719f5d7fSMiklos Szeredi if (err) 2122719f5d7fSMiklos Szeredi goto out_cleanup_ids; 2123909b0a88SAl Viro for (p = source_mnt; p; p = next_mnt(p, source_mnt)) 21240f0afb1dSAl Viro set_mnt_shared(p); 21250b1b901bSAl Viro } else { 21260b1b901bSAl Viro lock_mount_hash(); 2127b90fa9aeSRam Pai } 21282763d119SAl Viro if (moving) { 21292763d119SAl Viro unhash_mnt(source_mnt); 213084d17192SAl Viro attach_mnt(source_mnt, dest_mnt, dest_mp); 2131143c8c91SAl Viro touch_mnt_namespace(source_mnt->mnt_ns); 213221444403SRam Pai } else { 213344dfd84aSDavid Howells if (source_mnt->mnt_ns) { 213444dfd84aSDavid Howells /* move from anon - the caller will destroy */ 213544dfd84aSDavid Howells list_del_init(&source_mnt->mnt_ns->list); 213644dfd84aSDavid Howells } 213784d17192SAl Viro mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt); 21381064f874SEric W. Biederman commit_tree(source_mnt); 213921444403SRam Pai } 2140b90fa9aeSRam Pai 214138129a13SAl Viro hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) { 21421d6a32acSAl Viro struct mount *q; 214338129a13SAl Viro hlist_del_init(&child->mnt_hash); 21441064f874SEric W. Biederman q = __lookup_mnt(&child->mnt_parent->mnt, 21451d6a32acSAl Viro child->mnt_mountpoint); 21461064f874SEric W. Biederman if (q) 21471064f874SEric W. Biederman mnt_change_mountpoint(child, smp, q); 21483bd045ccSAl Viro /* Notice when we are propagating across user namespaces */ 21493bd045ccSAl Viro if (child->mnt_parent->mnt_ns->user_ns != user_ns) 21503bd045ccSAl Viro lock_mnt_tree(child); 2151d728cf79SChristian Brauner child->mnt.mnt_flags &= ~MNT_LOCKED; 21521064f874SEric W. Biederman commit_tree(child); 2153b90fa9aeSRam Pai } 21541064f874SEric W. Biederman put_mountpoint(smp); 2155719ea2fbSAl Viro unlock_mount_hash(); 215699b7db7bSNick Piggin 2157b90fa9aeSRam Pai return 0; 2158719f5d7fSMiklos Szeredi 2159719f5d7fSMiklos Szeredi out_cleanup_ids: 2160f2ebb3a9SAl Viro while (!hlist_empty(&tree_list)) { 2161f2ebb3a9SAl Viro child = hlist_entry(tree_list.first, struct mount, mnt_hash); 2162d2921684SEric W. Biederman child->mnt_parent->mnt_ns->pending_mounts = 0; 2163e819f152SEric W. Biederman umount_tree(child, UMOUNT_SYNC); 2164f2ebb3a9SAl Viro } 2165f2ebb3a9SAl Viro unlock_mount_hash(); 21660fb54e50SAl Viro cleanup_group_ids(source_mnt, NULL); 2167719f5d7fSMiklos Szeredi out: 2168d2921684SEric W. Biederman ns->pending_mounts = 0; 21691064f874SEric W. Biederman 21701064f874SEric W. Biederman read_seqlock_excl(&mount_lock); 21711064f874SEric W. Biederman put_mountpoint(smp); 21721064f874SEric W. Biederman read_sequnlock_excl(&mount_lock); 21731064f874SEric W. Biederman 2174719f5d7fSMiklos Szeredi return err; 2175b90fa9aeSRam Pai } 2176b90fa9aeSRam Pai 217784d17192SAl Viro static struct mountpoint *lock_mount(struct path *path) 2178b12cea91SAl Viro { 2179b12cea91SAl Viro struct vfsmount *mnt; 218084d17192SAl Viro struct dentry *dentry = path->dentry; 2181b12cea91SAl Viro retry: 21825955102cSAl Viro inode_lock(dentry->d_inode); 218384d17192SAl Viro if (unlikely(cant_mount(dentry))) { 21845955102cSAl Viro inode_unlock(dentry->d_inode); 218584d17192SAl Viro return ERR_PTR(-ENOENT); 2186b12cea91SAl Viro } 218797216be0SAl Viro namespace_lock(); 2188b12cea91SAl Viro mnt = lookup_mnt(path); 218984d17192SAl Viro if (likely(!mnt)) { 21903895dbf8SEric W. Biederman struct mountpoint *mp = get_mountpoint(dentry); 219184d17192SAl Viro if (IS_ERR(mp)) { 219297216be0SAl Viro namespace_unlock(); 21935955102cSAl Viro inode_unlock(dentry->d_inode); 219484d17192SAl Viro return mp; 219584d17192SAl Viro } 219684d17192SAl Viro return mp; 219784d17192SAl Viro } 219897216be0SAl Viro namespace_unlock(); 21995955102cSAl Viro inode_unlock(path->dentry->d_inode); 2200b12cea91SAl Viro path_put(path); 2201b12cea91SAl Viro path->mnt = mnt; 220284d17192SAl Viro dentry = path->dentry = dget(mnt->mnt_root); 2203b12cea91SAl Viro goto retry; 2204b12cea91SAl Viro } 2205b12cea91SAl Viro 220684d17192SAl Viro static void unlock_mount(struct mountpoint *where) 2207b12cea91SAl Viro { 220884d17192SAl Viro struct dentry *dentry = where->m_dentry; 22093895dbf8SEric W. Biederman 22103895dbf8SEric W. Biederman read_seqlock_excl(&mount_lock); 221184d17192SAl Viro put_mountpoint(where); 22123895dbf8SEric W. Biederman read_sequnlock_excl(&mount_lock); 22133895dbf8SEric W. Biederman 2214328e6d90SAl Viro namespace_unlock(); 22155955102cSAl Viro inode_unlock(dentry->d_inode); 2216b12cea91SAl Viro } 2217b12cea91SAl Viro 221884d17192SAl Viro static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp) 22191da177e4SLinus Torvalds { 2220e462ec50SDavid Howells if (mnt->mnt.mnt_sb->s_flags & SB_NOUSER) 22211da177e4SLinus Torvalds return -EINVAL; 22221da177e4SLinus Torvalds 2223e36cb0b8SDavid Howells if (d_is_dir(mp->m_dentry) != 2224e36cb0b8SDavid Howells d_is_dir(mnt->mnt.mnt_root)) 22251da177e4SLinus Torvalds return -ENOTDIR; 22261da177e4SLinus Torvalds 22272763d119SAl Viro return attach_recursive_mnt(mnt, p, mp, false); 22281da177e4SLinus Torvalds } 22291da177e4SLinus Torvalds 22301da177e4SLinus Torvalds /* 22317a2e8a8fSValerie Aurora * Sanity check the flags to change_mnt_propagation. 22327a2e8a8fSValerie Aurora */ 22337a2e8a8fSValerie Aurora 2234e462ec50SDavid Howells static int flags_to_propagation_type(int ms_flags) 22357a2e8a8fSValerie Aurora { 2236e462ec50SDavid Howells int type = ms_flags & ~(MS_REC | MS_SILENT); 22377a2e8a8fSValerie Aurora 22387a2e8a8fSValerie Aurora /* Fail if any non-propagation flags are set */ 22397a2e8a8fSValerie Aurora if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 22407a2e8a8fSValerie Aurora return 0; 22417a2e8a8fSValerie Aurora /* Only one propagation flag should be set */ 22427a2e8a8fSValerie Aurora if (!is_power_of_2(type)) 22437a2e8a8fSValerie Aurora return 0; 22447a2e8a8fSValerie Aurora return type; 22457a2e8a8fSValerie Aurora } 22467a2e8a8fSValerie Aurora 22477a2e8a8fSValerie Aurora /* 224807b20889SRam Pai * recursively change the type of the mountpoint. 224907b20889SRam Pai */ 2250e462ec50SDavid Howells static int do_change_type(struct path *path, int ms_flags) 225107b20889SRam Pai { 2252315fc83eSAl Viro struct mount *m; 22534b8b21f4SAl Viro struct mount *mnt = real_mount(path->mnt); 2254e462ec50SDavid Howells int recurse = ms_flags & MS_REC; 22557a2e8a8fSValerie Aurora int type; 2256719f5d7fSMiklos Szeredi int err = 0; 225707b20889SRam Pai 22582d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 225907b20889SRam Pai return -EINVAL; 226007b20889SRam Pai 2261e462ec50SDavid Howells type = flags_to_propagation_type(ms_flags); 22627a2e8a8fSValerie Aurora if (!type) 22637a2e8a8fSValerie Aurora return -EINVAL; 22647a2e8a8fSValerie Aurora 226597216be0SAl Viro namespace_lock(); 2266719f5d7fSMiklos Szeredi if (type == MS_SHARED) { 2267719f5d7fSMiklos Szeredi err = invent_group_ids(mnt, recurse); 2268719f5d7fSMiklos Szeredi if (err) 2269719f5d7fSMiklos Szeredi goto out_unlock; 2270719f5d7fSMiklos Szeredi } 2271719f5d7fSMiklos Szeredi 2272719ea2fbSAl Viro lock_mount_hash(); 2273909b0a88SAl Viro for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL)) 22740f0afb1dSAl Viro change_mnt_propagation(m, type); 2275719ea2fbSAl Viro unlock_mount_hash(); 2276719f5d7fSMiklos Szeredi 2277719f5d7fSMiklos Szeredi out_unlock: 227897216be0SAl Viro namespace_unlock(); 2279719f5d7fSMiklos Szeredi return err; 228007b20889SRam Pai } 228107b20889SRam Pai 22825ff9d8a6SEric W. Biederman static bool has_locked_children(struct mount *mnt, struct dentry *dentry) 22835ff9d8a6SEric W. Biederman { 22845ff9d8a6SEric W. Biederman struct mount *child; 22855ff9d8a6SEric W. Biederman list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) { 22865ff9d8a6SEric W. Biederman if (!is_subdir(child->mnt_mountpoint, dentry)) 22875ff9d8a6SEric W. Biederman continue; 22885ff9d8a6SEric W. Biederman 22895ff9d8a6SEric W. Biederman if (child->mnt.mnt_flags & MNT_LOCKED) 22905ff9d8a6SEric W. Biederman return true; 22915ff9d8a6SEric W. Biederman } 22925ff9d8a6SEric W. Biederman return false; 22935ff9d8a6SEric W. Biederman } 22945ff9d8a6SEric W. Biederman 2295a07b2000SAl Viro static struct mount *__do_loopback(struct path *old_path, int recurse) 2296a07b2000SAl Viro { 2297a07b2000SAl Viro struct mount *mnt = ERR_PTR(-EINVAL), *old = real_mount(old_path->mnt); 2298a07b2000SAl Viro 2299a07b2000SAl Viro if (IS_MNT_UNBINDABLE(old)) 2300a07b2000SAl Viro return mnt; 2301a07b2000SAl Viro 2302a07b2000SAl Viro if (!check_mnt(old) && old_path->dentry->d_op != &ns_dentry_operations) 2303a07b2000SAl Viro return mnt; 2304a07b2000SAl Viro 2305a07b2000SAl Viro if (!recurse && has_locked_children(old, old_path->dentry)) 2306a07b2000SAl Viro return mnt; 2307a07b2000SAl Viro 2308a07b2000SAl Viro if (recurse) 2309a07b2000SAl Viro mnt = copy_tree(old, old_path->dentry, CL_COPY_MNT_NS_FILE); 2310a07b2000SAl Viro else 2311a07b2000SAl Viro mnt = clone_mnt(old, old_path->dentry, 0); 2312a07b2000SAl Viro 2313a07b2000SAl Viro if (!IS_ERR(mnt)) 2314a07b2000SAl Viro mnt->mnt.mnt_flags &= ~MNT_LOCKED; 2315a07b2000SAl Viro 2316a07b2000SAl Viro return mnt; 2317a07b2000SAl Viro } 2318a07b2000SAl Viro 231907b20889SRam Pai /* 23201da177e4SLinus Torvalds * do loopback mount. 23211da177e4SLinus Torvalds */ 2322808d4e3cSAl Viro static int do_loopback(struct path *path, const char *old_name, 23232dafe1c4SEric Sandeen int recurse) 23241da177e4SLinus Torvalds { 23252d92ab3cSAl Viro struct path old_path; 2326a07b2000SAl Viro struct mount *mnt = NULL, *parent; 232784d17192SAl Viro struct mountpoint *mp; 232857eccb83SAl Viro int err; 23291da177e4SLinus Torvalds if (!old_name || !*old_name) 23301da177e4SLinus Torvalds return -EINVAL; 2331815d405cSTrond Myklebust err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path); 23321da177e4SLinus Torvalds if (err) 23331da177e4SLinus Torvalds return err; 23341da177e4SLinus Torvalds 23358823c079SEric W. Biederman err = -EINVAL; 23364ce5d2b1SEric W. Biederman if (mnt_ns_loop(old_path.dentry)) 23378823c079SEric W. Biederman goto out; 23388823c079SEric W. Biederman 233984d17192SAl Viro mp = lock_mount(path); 2340a07b2000SAl Viro if (IS_ERR(mp)) { 234184d17192SAl Viro err = PTR_ERR(mp); 23429676f0c6SRam Pai goto out; 2343a07b2000SAl Viro } 23449676f0c6SRam Pai 234584d17192SAl Viro parent = real_mount(path->mnt); 2346e149ed2bSAl Viro if (!check_mnt(parent)) 2347e149ed2bSAl Viro goto out2; 2348e149ed2bSAl Viro 2349a07b2000SAl Viro mnt = __do_loopback(&old_path, recurse); 2350be34d1a3SDavid Howells if (IS_ERR(mnt)) { 2351be34d1a3SDavid Howells err = PTR_ERR(mnt); 2352e9c5d8a5SAndrey Vagin goto out2; 2353be34d1a3SDavid Howells } 2354ccd48bc7SAl Viro 235584d17192SAl Viro err = graft_tree(mnt, parent, mp); 23561da177e4SLinus Torvalds if (err) { 2357719ea2fbSAl Viro lock_mount_hash(); 2358e819f152SEric W. Biederman umount_tree(mnt, UMOUNT_SYNC); 2359719ea2fbSAl Viro unlock_mount_hash(); 23605b83d2c5SRam Pai } 2361b12cea91SAl Viro out2: 236284d17192SAl Viro unlock_mount(mp); 2363ccd48bc7SAl Viro out: 23642d92ab3cSAl Viro path_put(&old_path); 23651da177e4SLinus Torvalds return err; 23661da177e4SLinus Torvalds } 23671da177e4SLinus Torvalds 2368a07b2000SAl Viro static struct file *open_detached_copy(struct path *path, bool recursive) 2369a07b2000SAl Viro { 2370a07b2000SAl Viro struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns; 2371a07b2000SAl Viro struct mnt_namespace *ns = alloc_mnt_ns(user_ns, true); 2372a07b2000SAl Viro struct mount *mnt, *p; 2373a07b2000SAl Viro struct file *file; 2374a07b2000SAl Viro 2375a07b2000SAl Viro if (IS_ERR(ns)) 2376a07b2000SAl Viro return ERR_CAST(ns); 2377a07b2000SAl Viro 2378a07b2000SAl Viro namespace_lock(); 2379a07b2000SAl Viro mnt = __do_loopback(path, recursive); 2380a07b2000SAl Viro if (IS_ERR(mnt)) { 2381a07b2000SAl Viro namespace_unlock(); 2382a07b2000SAl Viro free_mnt_ns(ns); 2383a07b2000SAl Viro return ERR_CAST(mnt); 2384a07b2000SAl Viro } 2385a07b2000SAl Viro 2386a07b2000SAl Viro lock_mount_hash(); 2387a07b2000SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 2388a07b2000SAl Viro p->mnt_ns = ns; 2389a07b2000SAl Viro ns->mounts++; 2390a07b2000SAl Viro } 2391a07b2000SAl Viro ns->root = mnt; 2392a07b2000SAl Viro list_add_tail(&ns->list, &mnt->mnt_list); 2393a07b2000SAl Viro mntget(&mnt->mnt); 2394a07b2000SAl Viro unlock_mount_hash(); 2395a07b2000SAl Viro namespace_unlock(); 2396a07b2000SAl Viro 2397a07b2000SAl Viro mntput(path->mnt); 2398a07b2000SAl Viro path->mnt = &mnt->mnt; 2399a07b2000SAl Viro file = dentry_open(path, O_PATH, current_cred()); 2400a07b2000SAl Viro if (IS_ERR(file)) 2401a07b2000SAl Viro dissolve_on_fput(path->mnt); 2402a07b2000SAl Viro else 2403a07b2000SAl Viro file->f_mode |= FMODE_NEED_UNMOUNT; 2404a07b2000SAl Viro return file; 2405a07b2000SAl Viro } 2406a07b2000SAl Viro 24072658ce09SBen Dooks SYSCALL_DEFINE3(open_tree, int, dfd, const char __user *, filename, unsigned, flags) 2408a07b2000SAl Viro { 2409a07b2000SAl Viro struct file *file; 2410a07b2000SAl Viro struct path path; 2411a07b2000SAl Viro int lookup_flags = LOOKUP_AUTOMOUNT | LOOKUP_FOLLOW; 2412a07b2000SAl Viro bool detached = flags & OPEN_TREE_CLONE; 2413a07b2000SAl Viro int error; 2414a07b2000SAl Viro int fd; 2415a07b2000SAl Viro 2416a07b2000SAl Viro BUILD_BUG_ON(OPEN_TREE_CLOEXEC != O_CLOEXEC); 2417a07b2000SAl Viro 2418a07b2000SAl Viro if (flags & ~(AT_EMPTY_PATH | AT_NO_AUTOMOUNT | AT_RECURSIVE | 2419a07b2000SAl Viro AT_SYMLINK_NOFOLLOW | OPEN_TREE_CLONE | 2420a07b2000SAl Viro OPEN_TREE_CLOEXEC)) 2421a07b2000SAl Viro return -EINVAL; 2422a07b2000SAl Viro 2423a07b2000SAl Viro if ((flags & (AT_RECURSIVE | OPEN_TREE_CLONE)) == AT_RECURSIVE) 2424a07b2000SAl Viro return -EINVAL; 2425a07b2000SAl Viro 2426a07b2000SAl Viro if (flags & AT_NO_AUTOMOUNT) 2427a07b2000SAl Viro lookup_flags &= ~LOOKUP_AUTOMOUNT; 2428a07b2000SAl Viro if (flags & AT_SYMLINK_NOFOLLOW) 2429a07b2000SAl Viro lookup_flags &= ~LOOKUP_FOLLOW; 2430a07b2000SAl Viro if (flags & AT_EMPTY_PATH) 2431a07b2000SAl Viro lookup_flags |= LOOKUP_EMPTY; 2432a07b2000SAl Viro 2433a07b2000SAl Viro if (detached && !may_mount()) 2434a07b2000SAl Viro return -EPERM; 2435a07b2000SAl Viro 2436a07b2000SAl Viro fd = get_unused_fd_flags(flags & O_CLOEXEC); 2437a07b2000SAl Viro if (fd < 0) 2438a07b2000SAl Viro return fd; 2439a07b2000SAl Viro 2440a07b2000SAl Viro error = user_path_at(dfd, filename, lookup_flags, &path); 2441a07b2000SAl Viro if (unlikely(error)) { 2442a07b2000SAl Viro file = ERR_PTR(error); 2443a07b2000SAl Viro } else { 2444a07b2000SAl Viro if (detached) 2445a07b2000SAl Viro file = open_detached_copy(&path, flags & AT_RECURSIVE); 2446a07b2000SAl Viro else 2447a07b2000SAl Viro file = dentry_open(&path, O_PATH, current_cred()); 2448a07b2000SAl Viro path_put(&path); 2449a07b2000SAl Viro } 2450a07b2000SAl Viro if (IS_ERR(file)) { 2451a07b2000SAl Viro put_unused_fd(fd); 2452a07b2000SAl Viro return PTR_ERR(file); 2453a07b2000SAl Viro } 2454a07b2000SAl Viro fd_install(fd, file); 2455a07b2000SAl Viro return fd; 2456a07b2000SAl Viro } 2457a07b2000SAl Viro 245843f5e655SDavid Howells /* 245943f5e655SDavid Howells * Don't allow locked mount flags to be cleared. 246043f5e655SDavid Howells * 246143f5e655SDavid Howells * No locks need to be held here while testing the various MNT_LOCK 246243f5e655SDavid Howells * flags because those flags can never be cleared once they are set. 246343f5e655SDavid Howells */ 246443f5e655SDavid Howells static bool can_change_locked_flags(struct mount *mnt, unsigned int mnt_flags) 24652e4b7fcdSDave Hansen { 246643f5e655SDavid Howells unsigned int fl = mnt->mnt.mnt_flags; 24672e4b7fcdSDave Hansen 246843f5e655SDavid Howells if ((fl & MNT_LOCK_READONLY) && 246943f5e655SDavid Howells !(mnt_flags & MNT_READONLY)) 247043f5e655SDavid Howells return false; 247143f5e655SDavid Howells 247243f5e655SDavid Howells if ((fl & MNT_LOCK_NODEV) && 247343f5e655SDavid Howells !(mnt_flags & MNT_NODEV)) 247443f5e655SDavid Howells return false; 247543f5e655SDavid Howells 247643f5e655SDavid Howells if ((fl & MNT_LOCK_NOSUID) && 247743f5e655SDavid Howells !(mnt_flags & MNT_NOSUID)) 247843f5e655SDavid Howells return false; 247943f5e655SDavid Howells 248043f5e655SDavid Howells if ((fl & MNT_LOCK_NOEXEC) && 248143f5e655SDavid Howells !(mnt_flags & MNT_NOEXEC)) 248243f5e655SDavid Howells return false; 248343f5e655SDavid Howells 248443f5e655SDavid Howells if ((fl & MNT_LOCK_ATIME) && 248543f5e655SDavid Howells ((fl & MNT_ATIME_MASK) != (mnt_flags & MNT_ATIME_MASK))) 248643f5e655SDavid Howells return false; 248743f5e655SDavid Howells 248843f5e655SDavid Howells return true; 248943f5e655SDavid Howells } 249043f5e655SDavid Howells 249143f5e655SDavid Howells static int change_mount_ro_state(struct mount *mnt, unsigned int mnt_flags) 249243f5e655SDavid Howells { 249343f5e655SDavid Howells bool readonly_request = (mnt_flags & MNT_READONLY); 249443f5e655SDavid Howells 249543f5e655SDavid Howells if (readonly_request == __mnt_is_readonly(&mnt->mnt)) 24962e4b7fcdSDave Hansen return 0; 24972e4b7fcdSDave Hansen 24982e4b7fcdSDave Hansen if (readonly_request) 249943f5e655SDavid Howells return mnt_make_readonly(mnt); 250043f5e655SDavid Howells 250143f5e655SDavid Howells return __mnt_unmake_readonly(mnt); 250243f5e655SDavid Howells } 250343f5e655SDavid Howells 250443f5e655SDavid Howells /* 250543f5e655SDavid Howells * Update the user-settable attributes on a mount. The caller must hold 250643f5e655SDavid Howells * sb->s_umount for writing. 250743f5e655SDavid Howells */ 250843f5e655SDavid Howells static void set_mount_attributes(struct mount *mnt, unsigned int mnt_flags) 250943f5e655SDavid Howells { 251043f5e655SDavid Howells lock_mount_hash(); 251143f5e655SDavid Howells mnt_flags |= mnt->mnt.mnt_flags & ~MNT_USER_SETTABLE_MASK; 251243f5e655SDavid Howells mnt->mnt.mnt_flags = mnt_flags; 251343f5e655SDavid Howells touch_mnt_namespace(mnt->mnt_ns); 251443f5e655SDavid Howells unlock_mount_hash(); 251543f5e655SDavid Howells } 251643f5e655SDavid Howells 2517f8b92ba6SDeepa Dinamani static void mnt_warn_timestamp_expiry(struct path *mountpoint, struct vfsmount *mnt) 2518f8b92ba6SDeepa Dinamani { 2519f8b92ba6SDeepa Dinamani struct super_block *sb = mnt->mnt_sb; 2520f8b92ba6SDeepa Dinamani 2521f8b92ba6SDeepa Dinamani if (!__mnt_is_readonly(mnt) && 2522f8b92ba6SDeepa Dinamani (ktime_get_real_seconds() + TIME_UPTIME_SEC_MAX > sb->s_time_max)) { 2523f8b92ba6SDeepa Dinamani char *buf = (char *)__get_free_page(GFP_KERNEL); 2524f8b92ba6SDeepa Dinamani char *mntpath = buf ? d_path(mountpoint, buf, PAGE_SIZE) : ERR_PTR(-ENOMEM); 2525f8b92ba6SDeepa Dinamani struct tm tm; 2526f8b92ba6SDeepa Dinamani 2527f8b92ba6SDeepa Dinamani time64_to_tm(sb->s_time_max, 0, &tm); 2528f8b92ba6SDeepa Dinamani 25290ecee669SEric Biggers pr_warn("%s filesystem being %s at %s supports timestamps until %04ld (0x%llx)\n", 25300ecee669SEric Biggers sb->s_type->name, 25310ecee669SEric Biggers is_mounted(mnt) ? "remounted" : "mounted", 25320ecee669SEric Biggers mntpath, 2533f8b92ba6SDeepa Dinamani tm.tm_year+1900, (unsigned long long)sb->s_time_max); 2534f8b92ba6SDeepa Dinamani 2535f8b92ba6SDeepa Dinamani free_page((unsigned long)buf); 2536f8b92ba6SDeepa Dinamani } 2537f8b92ba6SDeepa Dinamani } 2538f8b92ba6SDeepa Dinamani 253943f5e655SDavid Howells /* 254043f5e655SDavid Howells * Handle reconfiguration of the mountpoint only without alteration of the 254143f5e655SDavid Howells * superblock it refers to. This is triggered by specifying MS_REMOUNT|MS_BIND 254243f5e655SDavid Howells * to mount(2). 254343f5e655SDavid Howells */ 254443f5e655SDavid Howells static int do_reconfigure_mnt(struct path *path, unsigned int mnt_flags) 254543f5e655SDavid Howells { 254643f5e655SDavid Howells struct super_block *sb = path->mnt->mnt_sb; 254743f5e655SDavid Howells struct mount *mnt = real_mount(path->mnt); 254843f5e655SDavid Howells int ret; 254943f5e655SDavid Howells 255043f5e655SDavid Howells if (!check_mnt(mnt)) 255143f5e655SDavid Howells return -EINVAL; 255243f5e655SDavid Howells 255343f5e655SDavid Howells if (path->dentry != mnt->mnt.mnt_root) 255443f5e655SDavid Howells return -EINVAL; 255543f5e655SDavid Howells 255643f5e655SDavid Howells if (!can_change_locked_flags(mnt, mnt_flags)) 255743f5e655SDavid Howells return -EPERM; 255843f5e655SDavid Howells 255943f5e655SDavid Howells down_write(&sb->s_umount); 256043f5e655SDavid Howells ret = change_mount_ro_state(mnt, mnt_flags); 256143f5e655SDavid Howells if (ret == 0) 256243f5e655SDavid Howells set_mount_attributes(mnt, mnt_flags); 256343f5e655SDavid Howells up_write(&sb->s_umount); 2564f8b92ba6SDeepa Dinamani 2565f8b92ba6SDeepa Dinamani mnt_warn_timestamp_expiry(path, &mnt->mnt); 2566f8b92ba6SDeepa Dinamani 256743f5e655SDavid Howells return ret; 25682e4b7fcdSDave Hansen } 25692e4b7fcdSDave Hansen 25701da177e4SLinus Torvalds /* 25711da177e4SLinus Torvalds * change filesystem flags. dir should be a physical root of filesystem. 25721da177e4SLinus Torvalds * If you've mounted a non-root directory somewhere and want to do remount 25731da177e4SLinus Torvalds * on it - tough luck. 25741da177e4SLinus Torvalds */ 2575e462ec50SDavid Howells static int do_remount(struct path *path, int ms_flags, int sb_flags, 2576e462ec50SDavid Howells int mnt_flags, void *data) 25771da177e4SLinus Torvalds { 25781da177e4SLinus Torvalds int err; 25792d92ab3cSAl Viro struct super_block *sb = path->mnt->mnt_sb; 2580143c8c91SAl Viro struct mount *mnt = real_mount(path->mnt); 25818d0347f6SDavid Howells struct fs_context *fc; 25821da177e4SLinus Torvalds 2583143c8c91SAl Viro if (!check_mnt(mnt)) 25841da177e4SLinus Torvalds return -EINVAL; 25851da177e4SLinus Torvalds 25862d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 25871da177e4SLinus Torvalds return -EINVAL; 25881da177e4SLinus Torvalds 258943f5e655SDavid Howells if (!can_change_locked_flags(mnt, mnt_flags)) 259007b64558SEric W. Biederman return -EPERM; 25919566d674SEric W. Biederman 25928d0347f6SDavid Howells fc = fs_context_for_reconfigure(path->dentry, sb_flags, MS_RMT_MASK); 25938d0347f6SDavid Howells if (IS_ERR(fc)) 25948d0347f6SDavid Howells return PTR_ERR(fc); 2595ff36fe2cSEric Paris 2596b330966fSMiklos Szeredi fc->oldapi = true; 25978d0347f6SDavid Howells err = parse_monolithic_mount_data(fc, data); 25988d0347f6SDavid Howells if (!err) { 25991da177e4SLinus Torvalds down_write(&sb->s_umount); 260057eccb83SAl Viro err = -EPERM; 260143f5e655SDavid Howells if (ns_capable(sb->s_user_ns, CAP_SYS_ADMIN)) { 26028d0347f6SDavid Howells err = reconfigure_super(fc); 260343f5e655SDavid Howells if (!err) 260443f5e655SDavid Howells set_mount_attributes(mnt, mnt_flags); 26050e55a7ccSDan Williams } 26066339dab8SAl Viro up_write(&sb->s_umount); 26078d0347f6SDavid Howells } 2608f8b92ba6SDeepa Dinamani 2609f8b92ba6SDeepa Dinamani mnt_warn_timestamp_expiry(path, &mnt->mnt); 2610f8b92ba6SDeepa Dinamani 26118d0347f6SDavid Howells put_fs_context(fc); 26121da177e4SLinus Torvalds return err; 26131da177e4SLinus Torvalds } 26141da177e4SLinus Torvalds 2615cbbe362cSAl Viro static inline int tree_contains_unbindable(struct mount *mnt) 26169676f0c6SRam Pai { 2617315fc83eSAl Viro struct mount *p; 2618909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 2619fc7be130SAl Viro if (IS_MNT_UNBINDABLE(p)) 26209676f0c6SRam Pai return 1; 26219676f0c6SRam Pai } 26229676f0c6SRam Pai return 0; 26239676f0c6SRam Pai } 26249676f0c6SRam Pai 262544dfd84aSDavid Howells /* 262644dfd84aSDavid Howells * Check that there aren't references to earlier/same mount namespaces in the 262744dfd84aSDavid Howells * specified subtree. Such references can act as pins for mount namespaces 262844dfd84aSDavid Howells * that aren't checked by the mount-cycle checking code, thereby allowing 262944dfd84aSDavid Howells * cycles to be made. 263044dfd84aSDavid Howells */ 263144dfd84aSDavid Howells static bool check_for_nsfs_mounts(struct mount *subtree) 263244dfd84aSDavid Howells { 263344dfd84aSDavid Howells struct mount *p; 263444dfd84aSDavid Howells bool ret = false; 263544dfd84aSDavid Howells 263644dfd84aSDavid Howells lock_mount_hash(); 263744dfd84aSDavid Howells for (p = subtree; p; p = next_mnt(p, subtree)) 263844dfd84aSDavid Howells if (mnt_ns_loop(p->mnt.mnt_root)) 263944dfd84aSDavid Howells goto out; 264044dfd84aSDavid Howells 264144dfd84aSDavid Howells ret = true; 264244dfd84aSDavid Howells out: 264344dfd84aSDavid Howells unlock_mount_hash(); 264444dfd84aSDavid Howells return ret; 264544dfd84aSDavid Howells } 264644dfd84aSDavid Howells 26472db154b3SDavid Howells static int do_move_mount(struct path *old_path, struct path *new_path) 26481da177e4SLinus Torvalds { 264944dfd84aSDavid Howells struct mnt_namespace *ns; 2650676da58dSAl Viro struct mount *p; 26510fb54e50SAl Viro struct mount *old; 26522763d119SAl Viro struct mount *parent; 26532763d119SAl Viro struct mountpoint *mp, *old_mp; 265457eccb83SAl Viro int err; 265544dfd84aSDavid Howells bool attached; 26561da177e4SLinus Torvalds 26572db154b3SDavid Howells mp = lock_mount(new_path); 265884d17192SAl Viro if (IS_ERR(mp)) 26592db154b3SDavid Howells return PTR_ERR(mp); 2660cc53ce53SDavid Howells 26612db154b3SDavid Howells old = real_mount(old_path->mnt); 26622db154b3SDavid Howells p = real_mount(new_path->mnt); 26632763d119SAl Viro parent = old->mnt_parent; 266444dfd84aSDavid Howells attached = mnt_has_parent(old); 26652763d119SAl Viro old_mp = old->mnt_mp; 266644dfd84aSDavid Howells ns = old->mnt_ns; 2667143c8c91SAl Viro 26681da177e4SLinus Torvalds err = -EINVAL; 266944dfd84aSDavid Howells /* The mountpoint must be in our namespace. */ 267044dfd84aSDavid Howells if (!check_mnt(p)) 26712db154b3SDavid Howells goto out; 26721da177e4SLinus Torvalds 2673570d7a98SEric Biggers /* The thing moved must be mounted... */ 2674570d7a98SEric Biggers if (!is_mounted(&old->mnt)) 267544dfd84aSDavid Howells goto out; 267644dfd84aSDavid Howells 2677570d7a98SEric Biggers /* ... and either ours or the root of anon namespace */ 2678570d7a98SEric Biggers if (!(attached ? check_mnt(old) : is_anon_ns(ns))) 26792db154b3SDavid Howells goto out; 26801da177e4SLinus Torvalds 26812db154b3SDavid Howells if (old->mnt.mnt_flags & MNT_LOCKED) 26822db154b3SDavid Howells goto out; 26832db154b3SDavid Howells 26842db154b3SDavid Howells if (old_path->dentry != old_path->mnt->mnt_root) 26852db154b3SDavid Howells goto out; 26862db154b3SDavid Howells 26872db154b3SDavid Howells if (d_is_dir(new_path->dentry) != 26882db154b3SDavid Howells d_is_dir(old_path->dentry)) 26892db154b3SDavid Howells goto out; 269021444403SRam Pai /* 269121444403SRam Pai * Don't move a mount residing in a shared parent. 269221444403SRam Pai */ 26932763d119SAl Viro if (attached && IS_MNT_SHARED(parent)) 26942db154b3SDavid Howells goto out; 26959676f0c6SRam Pai /* 26969676f0c6SRam Pai * Don't move a mount tree containing unbindable mounts to a destination 26979676f0c6SRam Pai * mount which is shared. 26989676f0c6SRam Pai */ 2699fc7be130SAl Viro if (IS_MNT_SHARED(p) && tree_contains_unbindable(old)) 27002db154b3SDavid Howells goto out; 27011da177e4SLinus Torvalds err = -ELOOP; 270244dfd84aSDavid Howells if (!check_for_nsfs_mounts(old)) 270344dfd84aSDavid Howells goto out; 2704fc7be130SAl Viro for (; mnt_has_parent(p); p = p->mnt_parent) 2705676da58dSAl Viro if (p == old) 27062db154b3SDavid Howells goto out; 27071da177e4SLinus Torvalds 27082db154b3SDavid Howells err = attach_recursive_mnt(old, real_mount(new_path->mnt), mp, 27092763d119SAl Viro attached); 27104ac91378SJan Blunck if (err) 27112db154b3SDavid Howells goto out; 27121da177e4SLinus Torvalds 27131da177e4SLinus Torvalds /* if the mount is moved, it should no longer be expire 27141da177e4SLinus Torvalds * automatically */ 27156776db3dSAl Viro list_del_init(&old->mnt_expire); 27162763d119SAl Viro if (attached) 27172763d119SAl Viro put_mountpoint(old_mp); 27181da177e4SLinus Torvalds out: 27192db154b3SDavid Howells unlock_mount(mp); 272044dfd84aSDavid Howells if (!err) { 27212763d119SAl Viro if (attached) 27222763d119SAl Viro mntput_no_expire(parent); 27232763d119SAl Viro else 272444dfd84aSDavid Howells free_mnt_ns(ns); 272544dfd84aSDavid Howells } 27262db154b3SDavid Howells return err; 27272db154b3SDavid Howells } 27282db154b3SDavid Howells 27292db154b3SDavid Howells static int do_move_mount_old(struct path *path, const char *old_name) 27302db154b3SDavid Howells { 27312db154b3SDavid Howells struct path old_path; 27322db154b3SDavid Howells int err; 27332db154b3SDavid Howells 27342db154b3SDavid Howells if (!old_name || !*old_name) 27352db154b3SDavid Howells return -EINVAL; 27362db154b3SDavid Howells 27372db154b3SDavid Howells err = kern_path(old_name, LOOKUP_FOLLOW, &old_path); 27382db154b3SDavid Howells if (err) 27392db154b3SDavid Howells return err; 27402db154b3SDavid Howells 27412db154b3SDavid Howells err = do_move_mount(&old_path, path); 27422d92ab3cSAl Viro path_put(&old_path); 27431da177e4SLinus Torvalds return err; 27441da177e4SLinus Torvalds } 27451da177e4SLinus Torvalds 27469d412a43SAl Viro /* 27479d412a43SAl Viro * add a mount into a namespace's mount tree 27489d412a43SAl Viro */ 27498f11538eSAl Viro static int do_add_mount(struct mount *newmnt, struct mountpoint *mp, 27508f11538eSAl Viro struct path *path, int mnt_flags) 27519d412a43SAl Viro { 27528f11538eSAl Viro struct mount *parent = real_mount(path->mnt); 27539d412a43SAl Viro 2754f2ebb3a9SAl Viro mnt_flags &= ~MNT_INTERNAL_FLAGS; 27559d412a43SAl Viro 275684d17192SAl Viro if (unlikely(!check_mnt(parent))) { 2757156cacb1SAl Viro /* that's acceptable only for automounts done in private ns */ 2758156cacb1SAl Viro if (!(mnt_flags & MNT_SHRINKABLE)) 27598f11538eSAl Viro return -EINVAL; 2760156cacb1SAl Viro /* ... and for those we'd better have mountpoint still alive */ 276184d17192SAl Viro if (!parent->mnt_ns) 27628f11538eSAl Viro return -EINVAL; 2763156cacb1SAl Viro } 27649d412a43SAl Viro 27659d412a43SAl Viro /* Refuse the same filesystem on the same mount point */ 276695bc5f25SAl Viro if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb && 27679d412a43SAl Viro path->mnt->mnt_root == path->dentry) 27688f11538eSAl Viro return -EBUSY; 27699d412a43SAl Viro 2770e36cb0b8SDavid Howells if (d_is_symlink(newmnt->mnt.mnt_root)) 27718f11538eSAl Viro return -EINVAL; 27729d412a43SAl Viro 277395bc5f25SAl Viro newmnt->mnt.mnt_flags = mnt_flags; 27748f11538eSAl Viro return graft_tree(newmnt, parent, mp); 27759d412a43SAl Viro } 2776b1e75df4SAl Viro 2777132e4608SDavid Howells static bool mount_too_revealing(const struct super_block *sb, int *new_mnt_flags); 2778132e4608SDavid Howells 2779132e4608SDavid Howells /* 2780132e4608SDavid Howells * Create a new mount using a superblock configuration and request it 2781132e4608SDavid Howells * be added to the namespace tree. 2782132e4608SDavid Howells */ 2783132e4608SDavid Howells static int do_new_mount_fc(struct fs_context *fc, struct path *mountpoint, 2784132e4608SDavid Howells unsigned int mnt_flags) 2785132e4608SDavid Howells { 2786132e4608SDavid Howells struct vfsmount *mnt; 27878f11538eSAl Viro struct mountpoint *mp; 2788132e4608SDavid Howells struct super_block *sb = fc->root->d_sb; 2789132e4608SDavid Howells int error; 2790132e4608SDavid Howells 2791c9ce29edSAl Viro error = security_sb_kern_mount(sb); 2792c9ce29edSAl Viro if (!error && mount_too_revealing(sb, &mnt_flags)) 2793c9ce29edSAl Viro error = -EPERM; 2794c9ce29edSAl Viro 2795c9ce29edSAl Viro if (unlikely(error)) { 2796c9ce29edSAl Viro fc_drop_locked(fc); 2797c9ce29edSAl Viro return error; 2798132e4608SDavid Howells } 2799132e4608SDavid Howells 2800132e4608SDavid Howells up_write(&sb->s_umount); 2801132e4608SDavid Howells 2802132e4608SDavid Howells mnt = vfs_create_mount(fc); 2803132e4608SDavid Howells if (IS_ERR(mnt)) 2804132e4608SDavid Howells return PTR_ERR(mnt); 2805132e4608SDavid Howells 2806f8b92ba6SDeepa Dinamani mnt_warn_timestamp_expiry(mountpoint, mnt); 2807f8b92ba6SDeepa Dinamani 28088f11538eSAl Viro mp = lock_mount(mountpoint); 28098f11538eSAl Viro if (IS_ERR(mp)) { 28108f11538eSAl Viro mntput(mnt); 28118f11538eSAl Viro return PTR_ERR(mp); 28128f11538eSAl Viro } 28138f11538eSAl Viro error = do_add_mount(real_mount(mnt), mp, mountpoint, mnt_flags); 28148f11538eSAl Viro unlock_mount(mp); 28150ecee669SEric Biggers if (error < 0) 28160ecee669SEric Biggers mntput(mnt); 2817f8b92ba6SDeepa Dinamani return error; 2818f8b92ba6SDeepa Dinamani } 2819f8b92ba6SDeepa Dinamani 28201da177e4SLinus Torvalds /* 28211da177e4SLinus Torvalds * create a new mount for userspace and request it to be added into the 28221da177e4SLinus Torvalds * namespace's tree 28231da177e4SLinus Torvalds */ 2824e462ec50SDavid Howells static int do_new_mount(struct path *path, const char *fstype, int sb_flags, 2825808d4e3cSAl Viro int mnt_flags, const char *name, void *data) 28261da177e4SLinus Torvalds { 28270c55cfc4SEric W. Biederman struct file_system_type *type; 2828a0c9a8b8SAl Viro struct fs_context *fc; 2829a0c9a8b8SAl Viro const char *subtype = NULL; 2830a0c9a8b8SAl Viro int err = 0; 28311da177e4SLinus Torvalds 28320c55cfc4SEric W. Biederman if (!fstype) 28331da177e4SLinus Torvalds return -EINVAL; 28341da177e4SLinus Torvalds 28350c55cfc4SEric W. Biederman type = get_fs_type(fstype); 28360c55cfc4SEric W. Biederman if (!type) 28370c55cfc4SEric W. Biederman return -ENODEV; 28380c55cfc4SEric W. Biederman 2839a0c9a8b8SAl Viro if (type->fs_flags & FS_HAS_SUBTYPE) { 2840a0c9a8b8SAl Viro subtype = strchr(fstype, '.'); 2841a0c9a8b8SAl Viro if (subtype) { 2842a0c9a8b8SAl Viro subtype++; 2843a0c9a8b8SAl Viro if (!*subtype) { 28440c55cfc4SEric W. Biederman put_filesystem(type); 2845a0c9a8b8SAl Viro return -EINVAL; 2846a0c9a8b8SAl Viro } 2847a0c9a8b8SAl Viro } 28488654df4eSEric W. Biederman } 28498654df4eSEric W. Biederman 2850a0c9a8b8SAl Viro fc = fs_context_for_mount(type, sb_flags); 2851a0c9a8b8SAl Viro put_filesystem(type); 2852a0c9a8b8SAl Viro if (IS_ERR(fc)) 2853a0c9a8b8SAl Viro return PTR_ERR(fc); 2854a0c9a8b8SAl Viro 28553e1aeb00SDavid Howells if (subtype) 28563e1aeb00SDavid Howells err = vfs_parse_fs_string(fc, "subtype", 28573e1aeb00SDavid Howells subtype, strlen(subtype)); 28583e1aeb00SDavid Howells if (!err && name) 28593e1aeb00SDavid Howells err = vfs_parse_fs_string(fc, "source", name, strlen(name)); 2860a0c9a8b8SAl Viro if (!err) 2861a0c9a8b8SAl Viro err = parse_monolithic_mount_data(fc, data); 2862c3aabf07SAl Viro if (!err && !mount_capable(fc)) 2863c3aabf07SAl Viro err = -EPERM; 2864a0c9a8b8SAl Viro if (!err) 2865a0c9a8b8SAl Viro err = vfs_get_tree(fc); 2866132e4608SDavid Howells if (!err) 2867132e4608SDavid Howells err = do_new_mount_fc(fc, path, mnt_flags); 2868a0c9a8b8SAl Viro 2869a0c9a8b8SAl Viro put_fs_context(fc); 287015f9a3f3SAl Viro return err; 28711da177e4SLinus Torvalds } 28721da177e4SLinus Torvalds 287319a167afSAl Viro int finish_automount(struct vfsmount *m, struct path *path) 287419a167afSAl Viro { 287526df6034SAl Viro struct dentry *dentry = path->dentry; 28768f11538eSAl Viro struct mountpoint *mp; 287725e195aaSAl Viro struct mount *mnt; 287819a167afSAl Viro int err; 287925e195aaSAl Viro 288025e195aaSAl Viro if (!m) 288125e195aaSAl Viro return 0; 288225e195aaSAl Viro if (IS_ERR(m)) 288325e195aaSAl Viro return PTR_ERR(m); 288425e195aaSAl Viro 288525e195aaSAl Viro mnt = real_mount(m); 288619a167afSAl Viro /* The new mount record should have at least 2 refs to prevent it being 288719a167afSAl Viro * expired before we get a chance to add it 288819a167afSAl Viro */ 28896776db3dSAl Viro BUG_ON(mnt_get_count(mnt) < 2); 289019a167afSAl Viro 289119a167afSAl Viro if (m->mnt_sb == path->mnt->mnt_sb && 289226df6034SAl Viro m->mnt_root == dentry) { 2893b1e75df4SAl Viro err = -ELOOP; 289426df6034SAl Viro goto discard; 289519a167afSAl Viro } 289619a167afSAl Viro 289726df6034SAl Viro /* 289826df6034SAl Viro * we don't want to use lock_mount() - in this case finding something 289926df6034SAl Viro * that overmounts our mountpoint to be means "quitely drop what we've 290026df6034SAl Viro * got", not "try to mount it on top". 290126df6034SAl Viro */ 290226df6034SAl Viro inode_lock(dentry->d_inode); 290326df6034SAl Viro namespace_lock(); 290426df6034SAl Viro if (unlikely(cant_mount(dentry))) { 290526df6034SAl Viro err = -ENOENT; 290626df6034SAl Viro goto discard_locked; 290726df6034SAl Viro } 290826df6034SAl Viro rcu_read_lock(); 290926df6034SAl Viro if (unlikely(__lookup_mnt(path->mnt, dentry))) { 291026df6034SAl Viro rcu_read_unlock(); 291126df6034SAl Viro err = 0; 291226df6034SAl Viro goto discard_locked; 291326df6034SAl Viro } 291426df6034SAl Viro rcu_read_unlock(); 291526df6034SAl Viro mp = get_mountpoint(dentry); 29168f11538eSAl Viro if (IS_ERR(mp)) { 29178f11538eSAl Viro err = PTR_ERR(mp); 291826df6034SAl Viro goto discard_locked; 29198f11538eSAl Viro } 292026df6034SAl Viro 29218f11538eSAl Viro err = do_add_mount(mnt, mp, path, path->mnt->mnt_flags | MNT_SHRINKABLE); 29228f11538eSAl Viro unlock_mount(mp); 292326df6034SAl Viro if (unlikely(err)) 292426df6034SAl Viro goto discard; 292526df6034SAl Viro mntput(m); 2926b1e75df4SAl Viro return 0; 292726df6034SAl Viro 292826df6034SAl Viro discard_locked: 292926df6034SAl Viro namespace_unlock(); 293026df6034SAl Viro inode_unlock(dentry->d_inode); 293126df6034SAl Viro discard: 2932b1e75df4SAl Viro /* remove m from any expiration list it may be on */ 29336776db3dSAl Viro if (!list_empty(&mnt->mnt_expire)) { 293497216be0SAl Viro namespace_lock(); 29356776db3dSAl Viro list_del_init(&mnt->mnt_expire); 293697216be0SAl Viro namespace_unlock(); 293719a167afSAl Viro } 2938b1e75df4SAl Viro mntput(m); 2939b1e75df4SAl Viro mntput(m); 294019a167afSAl Viro return err; 294119a167afSAl Viro } 294219a167afSAl Viro 2943ea5b778aSDavid Howells /** 2944ea5b778aSDavid Howells * mnt_set_expiry - Put a mount on an expiration list 2945ea5b778aSDavid Howells * @mnt: The mount to list. 2946ea5b778aSDavid Howells * @expiry_list: The list to add the mount to. 2947ea5b778aSDavid Howells */ 2948ea5b778aSDavid Howells void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list) 2949ea5b778aSDavid Howells { 295097216be0SAl Viro namespace_lock(); 2951ea5b778aSDavid Howells 29526776db3dSAl Viro list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list); 2953ea5b778aSDavid Howells 295497216be0SAl Viro namespace_unlock(); 2955ea5b778aSDavid Howells } 2956ea5b778aSDavid Howells EXPORT_SYMBOL(mnt_set_expiry); 2957ea5b778aSDavid Howells 2958ea5b778aSDavid Howells /* 29591da177e4SLinus Torvalds * process a list of expirable mountpoints with the intent of discarding any 29601da177e4SLinus Torvalds * mountpoints that aren't in use and haven't been touched since last we came 29611da177e4SLinus Torvalds * here 29621da177e4SLinus Torvalds */ 29631da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts) 29641da177e4SLinus Torvalds { 2965761d5c38SAl Viro struct mount *mnt, *next; 29661da177e4SLinus Torvalds LIST_HEAD(graveyard); 29671da177e4SLinus Torvalds 29681da177e4SLinus Torvalds if (list_empty(mounts)) 29691da177e4SLinus Torvalds return; 29701da177e4SLinus Torvalds 297197216be0SAl Viro namespace_lock(); 2972719ea2fbSAl Viro lock_mount_hash(); 29731da177e4SLinus Torvalds 29741da177e4SLinus Torvalds /* extract from the expiration list every vfsmount that matches the 29751da177e4SLinus Torvalds * following criteria: 29761da177e4SLinus Torvalds * - only referenced by its parent vfsmount 29771da177e4SLinus Torvalds * - still marked for expiry (marked on the last call here; marks are 29781da177e4SLinus Torvalds * cleared by mntput()) 29791da177e4SLinus Torvalds */ 29806776db3dSAl Viro list_for_each_entry_safe(mnt, next, mounts, mnt_expire) { 2981863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1) || 29821ab59738SAl Viro propagate_mount_busy(mnt, 1)) 29831da177e4SLinus Torvalds continue; 29846776db3dSAl Viro list_move(&mnt->mnt_expire, &graveyard); 29851da177e4SLinus Torvalds } 2986bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 29876776db3dSAl Viro mnt = list_first_entry(&graveyard, struct mount, mnt_expire); 2988143c8c91SAl Viro touch_mnt_namespace(mnt->mnt_ns); 2989e819f152SEric W. Biederman umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC); 2990bcc5c7d2SAl Viro } 2991719ea2fbSAl Viro unlock_mount_hash(); 29923ab6abeeSAl Viro namespace_unlock(); 29931da177e4SLinus Torvalds } 29941da177e4SLinus Torvalds 29951da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry); 29961da177e4SLinus Torvalds 29971da177e4SLinus Torvalds /* 29985528f911STrond Myklebust * Ripoff of 'select_parent()' 29995528f911STrond Myklebust * 30005528f911STrond Myklebust * search the list of submounts for a given mountpoint, and move any 30015528f911STrond Myklebust * shrinkable submounts to the 'graveyard' list. 30025528f911STrond Myklebust */ 3003692afc31SAl Viro static int select_submounts(struct mount *parent, struct list_head *graveyard) 30045528f911STrond Myklebust { 3005692afc31SAl Viro struct mount *this_parent = parent; 30065528f911STrond Myklebust struct list_head *next; 30075528f911STrond Myklebust int found = 0; 30085528f911STrond Myklebust 30095528f911STrond Myklebust repeat: 30106b41d536SAl Viro next = this_parent->mnt_mounts.next; 30115528f911STrond Myklebust resume: 30126b41d536SAl Viro while (next != &this_parent->mnt_mounts) { 30135528f911STrond Myklebust struct list_head *tmp = next; 30146b41d536SAl Viro struct mount *mnt = list_entry(tmp, struct mount, mnt_child); 30155528f911STrond Myklebust 30165528f911STrond Myklebust next = tmp->next; 3017692afc31SAl Viro if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE)) 30185528f911STrond Myklebust continue; 30195528f911STrond Myklebust /* 30205528f911STrond Myklebust * Descend a level if the d_mounts list is non-empty. 30215528f911STrond Myklebust */ 30226b41d536SAl Viro if (!list_empty(&mnt->mnt_mounts)) { 30235528f911STrond Myklebust this_parent = mnt; 30245528f911STrond Myklebust goto repeat; 30255528f911STrond Myklebust } 30265528f911STrond Myklebust 30271ab59738SAl Viro if (!propagate_mount_busy(mnt, 1)) { 30286776db3dSAl Viro list_move_tail(&mnt->mnt_expire, graveyard); 30295528f911STrond Myklebust found++; 30305528f911STrond Myklebust } 30315528f911STrond Myklebust } 30325528f911STrond Myklebust /* 30335528f911STrond Myklebust * All done at this level ... ascend and resume the search 30345528f911STrond Myklebust */ 30355528f911STrond Myklebust if (this_parent != parent) { 30366b41d536SAl Viro next = this_parent->mnt_child.next; 30370714a533SAl Viro this_parent = this_parent->mnt_parent; 30385528f911STrond Myklebust goto resume; 30395528f911STrond Myklebust } 30405528f911STrond Myklebust return found; 30415528f911STrond Myklebust } 30425528f911STrond Myklebust 30435528f911STrond Myklebust /* 30445528f911STrond Myklebust * process a list of expirable mountpoints with the intent of discarding any 30455528f911STrond Myklebust * submounts of a specific parent mountpoint 304699b7db7bSNick Piggin * 304748a066e7SAl Viro * mount_lock must be held for write 30485528f911STrond Myklebust */ 3049b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt) 30505528f911STrond Myklebust { 30515528f911STrond Myklebust LIST_HEAD(graveyard); 3052761d5c38SAl Viro struct mount *m; 30535528f911STrond Myklebust 30545528f911STrond Myklebust /* extract submounts of 'mountpoint' from the expiration list */ 3055c35038beSAl Viro while (select_submounts(mnt, &graveyard)) { 3056bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 3057761d5c38SAl Viro m = list_first_entry(&graveyard, struct mount, 30586776db3dSAl Viro mnt_expire); 3059143c8c91SAl Viro touch_mnt_namespace(m->mnt_ns); 3060e819f152SEric W. Biederman umount_tree(m, UMOUNT_PROPAGATE|UMOUNT_SYNC); 3061bcc5c7d2SAl Viro } 3062bcc5c7d2SAl Viro } 30635528f911STrond Myklebust } 30645528f911STrond Myklebust 3065028abd92SChristoph Hellwig static void *copy_mount_options(const void __user * data) 30661da177e4SLinus Torvalds { 3067b40ef869SAl Viro char *copy; 3068d563d678SCatalin Marinas unsigned left, offset; 30691da177e4SLinus Torvalds 30701da177e4SLinus Torvalds if (!data) 3071b40ef869SAl Viro return NULL; 30721da177e4SLinus Torvalds 3073b40ef869SAl Viro copy = kmalloc(PAGE_SIZE, GFP_KERNEL); 3074b40ef869SAl Viro if (!copy) 3075b40ef869SAl Viro return ERR_PTR(-ENOMEM); 30761da177e4SLinus Torvalds 3077d563d678SCatalin Marinas left = copy_from_user(copy, data, PAGE_SIZE); 30781da177e4SLinus Torvalds 3079d563d678SCatalin Marinas /* 3080d563d678SCatalin Marinas * Not all architectures have an exact copy_from_user(). Resort to 3081d563d678SCatalin Marinas * byte at a time. 3082d563d678SCatalin Marinas */ 3083d563d678SCatalin Marinas offset = PAGE_SIZE - left; 3084d563d678SCatalin Marinas while (left) { 3085d563d678SCatalin Marinas char c; 3086d563d678SCatalin Marinas if (get_user(c, (const char __user *)data + offset)) 3087d563d678SCatalin Marinas break; 3088d563d678SCatalin Marinas copy[offset] = c; 3089d563d678SCatalin Marinas left--; 3090d563d678SCatalin Marinas offset++; 3091d563d678SCatalin Marinas } 3092d563d678SCatalin Marinas 3093d563d678SCatalin Marinas if (left == PAGE_SIZE) { 3094b40ef869SAl Viro kfree(copy); 3095b40ef869SAl Viro return ERR_PTR(-EFAULT); 30961da177e4SLinus Torvalds } 3097d563d678SCatalin Marinas 3098b40ef869SAl Viro return copy; 30991da177e4SLinus Torvalds } 31001da177e4SLinus Torvalds 3101028abd92SChristoph Hellwig static char *copy_mount_string(const void __user *data) 3102eca6f534SVegard Nossum { 3103fbdb4401SChandan Rajendra return data ? strndup_user(data, PATH_MAX) : NULL; 3104eca6f534SVegard Nossum } 3105eca6f534SVegard Nossum 31061da177e4SLinus Torvalds /* 31071da177e4SLinus Torvalds * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to 31081da177e4SLinus Torvalds * be given to the mount() call (ie: read-only, no-dev, no-suid etc). 31091da177e4SLinus Torvalds * 31101da177e4SLinus Torvalds * data is a (void *) that can point to any structure up to 31111da177e4SLinus Torvalds * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent 31121da177e4SLinus Torvalds * information (or be NULL). 31131da177e4SLinus Torvalds * 31141da177e4SLinus Torvalds * Pre-0.97 versions of mount() didn't have a flags word. 31151da177e4SLinus Torvalds * When the flags word was introduced its top half was required 31161da177e4SLinus Torvalds * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9. 31171da177e4SLinus Torvalds * Therefore, if this magic number is present, it carries no information 31181da177e4SLinus Torvalds * and must be discarded. 31191da177e4SLinus Torvalds */ 3120c60166f0SChristoph Hellwig int path_mount(const char *dev_name, struct path *path, 3121808d4e3cSAl Viro const char *type_page, unsigned long flags, void *data_page) 31221da177e4SLinus Torvalds { 3123e462ec50SDavid Howells unsigned int mnt_flags = 0, sb_flags; 3124a1e6aaa3SChristoph Hellwig int ret; 31251da177e4SLinus Torvalds 31261da177e4SLinus Torvalds /* Discard magic */ 31271da177e4SLinus Torvalds if ((flags & MS_MGC_MSK) == MS_MGC_VAL) 31281da177e4SLinus Torvalds flags &= ~MS_MGC_MSK; 31291da177e4SLinus Torvalds 31301da177e4SLinus Torvalds /* Basic sanity checks */ 31311da177e4SLinus Torvalds if (data_page) 31321da177e4SLinus Torvalds ((char *)data_page)[PAGE_SIZE - 1] = 0; 31331da177e4SLinus Torvalds 3134e462ec50SDavid Howells if (flags & MS_NOUSER) 3135e462ec50SDavid Howells return -EINVAL; 3136e462ec50SDavid Howells 3137a1e6aaa3SChristoph Hellwig ret = security_sb_mount(dev_name, path, type_page, flags, data_page); 3138a1e6aaa3SChristoph Hellwig if (ret) 3139a1e6aaa3SChristoph Hellwig return ret; 3140a1e6aaa3SChristoph Hellwig if (!may_mount()) 3141a1e6aaa3SChristoph Hellwig return -EPERM; 3142a1e6aaa3SChristoph Hellwig if ((flags & SB_MANDLOCK) && !may_mandlock()) 3143a1e6aaa3SChristoph Hellwig return -EPERM; 3144a27ab9f2STetsuo Handa 3145613cbe3dSAndi Kleen /* Default to relatime unless overriden */ 3146613cbe3dSAndi Kleen if (!(flags & MS_NOATIME)) 31470a1c01c9SMatthew Garrett mnt_flags |= MNT_RELATIME; 31480a1c01c9SMatthew Garrett 31491da177e4SLinus Torvalds /* Separate the per-mountpoint flags */ 31501da177e4SLinus Torvalds if (flags & MS_NOSUID) 31511da177e4SLinus Torvalds mnt_flags |= MNT_NOSUID; 31521da177e4SLinus Torvalds if (flags & MS_NODEV) 31531da177e4SLinus Torvalds mnt_flags |= MNT_NODEV; 31541da177e4SLinus Torvalds if (flags & MS_NOEXEC) 31551da177e4SLinus Torvalds mnt_flags |= MNT_NOEXEC; 3156fc33a7bbSChristoph Hellwig if (flags & MS_NOATIME) 3157fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NOATIME; 3158fc33a7bbSChristoph Hellwig if (flags & MS_NODIRATIME) 3159fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NODIRATIME; 3160d0adde57SMatthew Garrett if (flags & MS_STRICTATIME) 3161d0adde57SMatthew Garrett mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME); 3162a9e5b732SDavid Howells if (flags & MS_RDONLY) 31632e4b7fcdSDave Hansen mnt_flags |= MNT_READONLY; 3164dab741e0SMattias Nissler if (flags & MS_NOSYMFOLLOW) 3165dab741e0SMattias Nissler mnt_flags |= MNT_NOSYMFOLLOW; 3166fc33a7bbSChristoph Hellwig 3167ffbc6f0eSEric W. Biederman /* The default atime for remount is preservation */ 3168ffbc6f0eSEric W. Biederman if ((flags & MS_REMOUNT) && 3169ffbc6f0eSEric W. Biederman ((flags & (MS_NOATIME | MS_NODIRATIME | MS_RELATIME | 3170ffbc6f0eSEric W. Biederman MS_STRICTATIME)) == 0)) { 3171ffbc6f0eSEric W. Biederman mnt_flags &= ~MNT_ATIME_MASK; 3172a1e6aaa3SChristoph Hellwig mnt_flags |= path->mnt->mnt_flags & MNT_ATIME_MASK; 3173ffbc6f0eSEric W. Biederman } 3174ffbc6f0eSEric W. Biederman 3175e462ec50SDavid Howells sb_flags = flags & (SB_RDONLY | 3176e462ec50SDavid Howells SB_SYNCHRONOUS | 3177e462ec50SDavid Howells SB_MANDLOCK | 3178e462ec50SDavid Howells SB_DIRSYNC | 3179e462ec50SDavid Howells SB_SILENT | 3180917086ffSMimi Zohar SB_POSIXACL | 3181d7ee9469SMarkus Trippelsdorf SB_LAZYTIME | 3182917086ffSMimi Zohar SB_I_VERSION); 31831da177e4SLinus Torvalds 318443f5e655SDavid Howells if ((flags & (MS_REMOUNT | MS_BIND)) == (MS_REMOUNT | MS_BIND)) 3185a1e6aaa3SChristoph Hellwig return do_reconfigure_mnt(path, mnt_flags); 3186a1e6aaa3SChristoph Hellwig if (flags & MS_REMOUNT) 3187a1e6aaa3SChristoph Hellwig return do_remount(path, flags, sb_flags, mnt_flags, data_page); 3188a1e6aaa3SChristoph Hellwig if (flags & MS_BIND) 3189a1e6aaa3SChristoph Hellwig return do_loopback(path, dev_name, flags & MS_REC); 3190a1e6aaa3SChristoph Hellwig if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 3191a1e6aaa3SChristoph Hellwig return do_change_type(path, flags); 3192a1e6aaa3SChristoph Hellwig if (flags & MS_MOVE) 3193a1e6aaa3SChristoph Hellwig return do_move_mount_old(path, dev_name); 3194a1e6aaa3SChristoph Hellwig 3195a1e6aaa3SChristoph Hellwig return do_new_mount(path, type_page, sb_flags, mnt_flags, dev_name, 31961da177e4SLinus Torvalds data_page); 3197a1e6aaa3SChristoph Hellwig } 3198a1e6aaa3SChristoph Hellwig 3199a1e6aaa3SChristoph Hellwig long do_mount(const char *dev_name, const char __user *dir_name, 3200a1e6aaa3SChristoph Hellwig const char *type_page, unsigned long flags, void *data_page) 3201a1e6aaa3SChristoph Hellwig { 3202a1e6aaa3SChristoph Hellwig struct path path; 3203a1e6aaa3SChristoph Hellwig int ret; 3204a1e6aaa3SChristoph Hellwig 3205a1e6aaa3SChristoph Hellwig ret = user_path_at(AT_FDCWD, dir_name, LOOKUP_FOLLOW, &path); 3206a1e6aaa3SChristoph Hellwig if (ret) 3207a1e6aaa3SChristoph Hellwig return ret; 3208a1e6aaa3SChristoph Hellwig ret = path_mount(dev_name, &path, type_page, flags, data_page); 32092d92ab3cSAl Viro path_put(&path); 3210a1e6aaa3SChristoph Hellwig return ret; 32111da177e4SLinus Torvalds } 32121da177e4SLinus Torvalds 3213537f7ccbSEric W. Biederman static struct ucounts *inc_mnt_namespaces(struct user_namespace *ns) 3214537f7ccbSEric W. Biederman { 3215537f7ccbSEric W. Biederman return inc_ucount(ns, current_euid(), UCOUNT_MNT_NAMESPACES); 3216537f7ccbSEric W. Biederman } 3217537f7ccbSEric W. Biederman 3218537f7ccbSEric W. Biederman static void dec_mnt_namespaces(struct ucounts *ucounts) 3219537f7ccbSEric W. Biederman { 3220537f7ccbSEric W. Biederman dec_ucount(ucounts, UCOUNT_MNT_NAMESPACES); 3221537f7ccbSEric W. Biederman } 3222537f7ccbSEric W. Biederman 3223771b1371SEric W. Biederman static void free_mnt_ns(struct mnt_namespace *ns) 3224771b1371SEric W. Biederman { 322574e83122SAl Viro if (!is_anon_ns(ns)) 32266344c433SAl Viro ns_free_inum(&ns->ns); 3227537f7ccbSEric W. Biederman dec_mnt_namespaces(ns->ucounts); 3228771b1371SEric W. Biederman put_user_ns(ns->user_ns); 3229771b1371SEric W. Biederman kfree(ns); 3230771b1371SEric W. Biederman } 3231771b1371SEric W. Biederman 32328823c079SEric W. Biederman /* 32338823c079SEric W. Biederman * Assign a sequence number so we can detect when we attempt to bind 32348823c079SEric W. Biederman * mount a reference to an older mount namespace into the current 32358823c079SEric W. Biederman * mount namespace, preventing reference counting loops. A 64bit 32368823c079SEric W. Biederman * number incrementing at 10Ghz will take 12,427 years to wrap which 32378823c079SEric W. Biederman * is effectively never, so we can ignore the possibility. 32388823c079SEric W. Biederman */ 32398823c079SEric W. Biederman static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1); 32408823c079SEric W. Biederman 324174e83122SAl Viro static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool anon) 3242cf8d2c11STrond Myklebust { 3243cf8d2c11STrond Myklebust struct mnt_namespace *new_ns; 3244537f7ccbSEric W. Biederman struct ucounts *ucounts; 324598f842e6SEric W. Biederman int ret; 3246cf8d2c11STrond Myklebust 3247537f7ccbSEric W. Biederman ucounts = inc_mnt_namespaces(user_ns); 3248537f7ccbSEric W. Biederman if (!ucounts) 3249df75e774SEric W. Biederman return ERR_PTR(-ENOSPC); 3250537f7ccbSEric W. Biederman 325174e83122SAl Viro new_ns = kzalloc(sizeof(struct mnt_namespace), GFP_KERNEL); 3252537f7ccbSEric W. Biederman if (!new_ns) { 3253537f7ccbSEric W. Biederman dec_mnt_namespaces(ucounts); 3254cf8d2c11STrond Myklebust return ERR_PTR(-ENOMEM); 3255537f7ccbSEric W. Biederman } 325674e83122SAl Viro if (!anon) { 32576344c433SAl Viro ret = ns_alloc_inum(&new_ns->ns); 325898f842e6SEric W. Biederman if (ret) { 325998f842e6SEric W. Biederman kfree(new_ns); 3260537f7ccbSEric W. Biederman dec_mnt_namespaces(ucounts); 326198f842e6SEric W. Biederman return ERR_PTR(ret); 326298f842e6SEric W. Biederman } 326374e83122SAl Viro } 326433c42940SAl Viro new_ns->ns.ops = &mntns_operations; 326574e83122SAl Viro if (!anon) 32668823c079SEric W. Biederman new_ns->seq = atomic64_add_return(1, &mnt_ns_seq); 32671a7b8969SKirill Tkhai refcount_set(&new_ns->ns.count, 1); 3268cf8d2c11STrond Myklebust INIT_LIST_HEAD(&new_ns->list); 3269cf8d2c11STrond Myklebust init_waitqueue_head(&new_ns->poll); 32709f6c61f9SMiklos Szeredi spin_lock_init(&new_ns->ns_lock); 3271771b1371SEric W. Biederman new_ns->user_ns = get_user_ns(user_ns); 3272537f7ccbSEric W. Biederman new_ns->ucounts = ucounts; 3273cf8d2c11STrond Myklebust return new_ns; 3274cf8d2c11STrond Myklebust } 3275cf8d2c11STrond Myklebust 32760766f788SEmese Revfy __latent_entropy 32779559f689SAl Viro struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns, 32789559f689SAl Viro struct user_namespace *user_ns, struct fs_struct *new_fs) 32791da177e4SLinus Torvalds { 32806b3286edSKirill Korotaev struct mnt_namespace *new_ns; 32817f2da1e7SAl Viro struct vfsmount *rootmnt = NULL, *pwdmnt = NULL; 3282315fc83eSAl Viro struct mount *p, *q; 32839559f689SAl Viro struct mount *old; 3284cb338d06SAl Viro struct mount *new; 32857a472ef4SEric W. Biederman int copy_flags; 32861da177e4SLinus Torvalds 32879559f689SAl Viro BUG_ON(!ns); 32889559f689SAl Viro 32899559f689SAl Viro if (likely(!(flags & CLONE_NEWNS))) { 32909559f689SAl Viro get_mnt_ns(ns); 32919559f689SAl Viro return ns; 32929559f689SAl Viro } 32939559f689SAl Viro 32949559f689SAl Viro old = ns->root; 32959559f689SAl Viro 329674e83122SAl Viro new_ns = alloc_mnt_ns(user_ns, false); 3297cf8d2c11STrond Myklebust if (IS_ERR(new_ns)) 3298cf8d2c11STrond Myklebust return new_ns; 32991da177e4SLinus Torvalds 330097216be0SAl Viro namespace_lock(); 33011da177e4SLinus Torvalds /* First pass: copy the tree topology */ 33024ce5d2b1SEric W. Biederman copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE; 33039559f689SAl Viro if (user_ns != ns->user_ns) 33043bd045ccSAl Viro copy_flags |= CL_SHARED_TO_SLAVE; 33057a472ef4SEric W. Biederman new = copy_tree(old, old->mnt.mnt_root, copy_flags); 3306be34d1a3SDavid Howells if (IS_ERR(new)) { 3307328e6d90SAl Viro namespace_unlock(); 3308771b1371SEric W. Biederman free_mnt_ns(new_ns); 3309be34d1a3SDavid Howells return ERR_CAST(new); 33101da177e4SLinus Torvalds } 33113bd045ccSAl Viro if (user_ns != ns->user_ns) { 33123bd045ccSAl Viro lock_mount_hash(); 33133bd045ccSAl Viro lock_mnt_tree(new); 33143bd045ccSAl Viro unlock_mount_hash(); 33153bd045ccSAl Viro } 3316be08d6d2SAl Viro new_ns->root = new; 33171a4eeaf2SAl Viro list_add_tail(&new_ns->list, &new->mnt_list); 33181da177e4SLinus Torvalds 33191da177e4SLinus Torvalds /* 33201da177e4SLinus Torvalds * Second pass: switch the tsk->fs->* elements and mark new vfsmounts 33211da177e4SLinus Torvalds * as belonging to new namespace. We have already acquired a private 33221da177e4SLinus Torvalds * fs_struct, so tsk->fs->lock is not needed. 33231da177e4SLinus Torvalds */ 3324909b0a88SAl Viro p = old; 3325cb338d06SAl Viro q = new; 33261da177e4SLinus Torvalds while (p) { 3327143c8c91SAl Viro q->mnt_ns = new_ns; 3328d2921684SEric W. Biederman new_ns->mounts++; 33299559f689SAl Viro if (new_fs) { 33309559f689SAl Viro if (&p->mnt == new_fs->root.mnt) { 33319559f689SAl Viro new_fs->root.mnt = mntget(&q->mnt); 3332315fc83eSAl Viro rootmnt = &p->mnt; 33331da177e4SLinus Torvalds } 33349559f689SAl Viro if (&p->mnt == new_fs->pwd.mnt) { 33359559f689SAl Viro new_fs->pwd.mnt = mntget(&q->mnt); 3336315fc83eSAl Viro pwdmnt = &p->mnt; 33371da177e4SLinus Torvalds } 33381da177e4SLinus Torvalds } 3339909b0a88SAl Viro p = next_mnt(p, old); 3340909b0a88SAl Viro q = next_mnt(q, new); 33414ce5d2b1SEric W. Biederman if (!q) 33424ce5d2b1SEric W. Biederman break; 33434ce5d2b1SEric W. Biederman while (p->mnt.mnt_root != q->mnt.mnt_root) 33444ce5d2b1SEric W. Biederman p = next_mnt(p, old); 33451da177e4SLinus Torvalds } 3346328e6d90SAl Viro namespace_unlock(); 33471da177e4SLinus Torvalds 33481da177e4SLinus Torvalds if (rootmnt) 3349f03c6599SAl Viro mntput(rootmnt); 33501da177e4SLinus Torvalds if (pwdmnt) 3351f03c6599SAl Viro mntput(pwdmnt); 33521da177e4SLinus Torvalds 3353741a2951SJANAK DESAI return new_ns; 3354741a2951SJANAK DESAI } 3355741a2951SJANAK DESAI 335674e83122SAl Viro struct dentry *mount_subtree(struct vfsmount *m, const char *name) 3357cf8d2c11STrond Myklebust { 33581a4eeaf2SAl Viro struct mount *mnt = real_mount(m); 3359ea441d11SAl Viro struct mnt_namespace *ns; 3360d31da0f0SAl Viro struct super_block *s; 3361ea441d11SAl Viro struct path path; 3362ea441d11SAl Viro int err; 3363ea441d11SAl Viro 336474e83122SAl Viro ns = alloc_mnt_ns(&init_user_ns, true); 336574e83122SAl Viro if (IS_ERR(ns)) { 336674e83122SAl Viro mntput(m); 3367ea441d11SAl Viro return ERR_CAST(ns); 336874e83122SAl Viro } 336974e83122SAl Viro mnt->mnt_ns = ns; 337074e83122SAl Viro ns->root = mnt; 337174e83122SAl Viro ns->mounts++; 337274e83122SAl Viro list_add(&mnt->mnt_list, &ns->list); 3373ea441d11SAl Viro 337474e83122SAl Viro err = vfs_path_lookup(m->mnt_root, m, 3375ea441d11SAl Viro name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path); 3376ea441d11SAl Viro 3377ea441d11SAl Viro put_mnt_ns(ns); 3378ea441d11SAl Viro 3379ea441d11SAl Viro if (err) 3380ea441d11SAl Viro return ERR_PTR(err); 3381ea441d11SAl Viro 3382ea441d11SAl Viro /* trade a vfsmount reference for active sb one */ 3383d31da0f0SAl Viro s = path.mnt->mnt_sb; 3384d31da0f0SAl Viro atomic_inc(&s->s_active); 3385ea441d11SAl Viro mntput(path.mnt); 3386ea441d11SAl Viro /* lock the sucker */ 3387d31da0f0SAl Viro down_write(&s->s_umount); 3388ea441d11SAl Viro /* ... and return the root of (sub)tree on it */ 3389ea441d11SAl Viro return path.dentry; 3390ea441d11SAl Viro } 3391ea441d11SAl Viro EXPORT_SYMBOL(mount_subtree); 3392ea441d11SAl Viro 3393cccaa5e3SDominik Brodowski SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name, 3394cccaa5e3SDominik Brodowski char __user *, type, unsigned long, flags, void __user *, data) 33951da177e4SLinus Torvalds { 3396eca6f534SVegard Nossum int ret; 3397eca6f534SVegard Nossum char *kernel_type; 3398eca6f534SVegard Nossum char *kernel_dev; 3399b40ef869SAl Viro void *options; 34001da177e4SLinus Torvalds 3401b8850d1fSTim Gardner kernel_type = copy_mount_string(type); 3402b8850d1fSTim Gardner ret = PTR_ERR(kernel_type); 3403b8850d1fSTim Gardner if (IS_ERR(kernel_type)) 3404eca6f534SVegard Nossum goto out_type; 34051da177e4SLinus Torvalds 3406b8850d1fSTim Gardner kernel_dev = copy_mount_string(dev_name); 3407b8850d1fSTim Gardner ret = PTR_ERR(kernel_dev); 3408b8850d1fSTim Gardner if (IS_ERR(kernel_dev)) 3409eca6f534SVegard Nossum goto out_dev; 34101da177e4SLinus Torvalds 3411b40ef869SAl Viro options = copy_mount_options(data); 3412b40ef869SAl Viro ret = PTR_ERR(options); 3413b40ef869SAl Viro if (IS_ERR(options)) 3414eca6f534SVegard Nossum goto out_data; 34151da177e4SLinus Torvalds 3416b40ef869SAl Viro ret = do_mount(kernel_dev, dir_name, kernel_type, flags, options); 3417eca6f534SVegard Nossum 3418b40ef869SAl Viro kfree(options); 3419eca6f534SVegard Nossum out_data: 3420eca6f534SVegard Nossum kfree(kernel_dev); 3421eca6f534SVegard Nossum out_dev: 3422eca6f534SVegard Nossum kfree(kernel_type); 3423eca6f534SVegard Nossum out_type: 3424eca6f534SVegard Nossum return ret; 34251da177e4SLinus Torvalds } 34261da177e4SLinus Torvalds 34271da177e4SLinus Torvalds /* 342893766fbdSDavid Howells * Create a kernel mount representation for a new, prepared superblock 342993766fbdSDavid Howells * (specified by fs_fd) and attach to an open_tree-like file descriptor. 343093766fbdSDavid Howells */ 343193766fbdSDavid Howells SYSCALL_DEFINE3(fsmount, int, fs_fd, unsigned int, flags, 343293766fbdSDavid Howells unsigned int, attr_flags) 343393766fbdSDavid Howells { 343493766fbdSDavid Howells struct mnt_namespace *ns; 343593766fbdSDavid Howells struct fs_context *fc; 343693766fbdSDavid Howells struct file *file; 343793766fbdSDavid Howells struct path newmount; 343893766fbdSDavid Howells struct mount *mnt; 343993766fbdSDavid Howells struct fd f; 344093766fbdSDavid Howells unsigned int mnt_flags = 0; 344193766fbdSDavid Howells long ret; 344293766fbdSDavid Howells 344393766fbdSDavid Howells if (!may_mount()) 344493766fbdSDavid Howells return -EPERM; 344593766fbdSDavid Howells 344693766fbdSDavid Howells if ((flags & ~(FSMOUNT_CLOEXEC)) != 0) 344793766fbdSDavid Howells return -EINVAL; 344893766fbdSDavid Howells 344993766fbdSDavid Howells if (attr_flags & ~(MOUNT_ATTR_RDONLY | 345093766fbdSDavid Howells MOUNT_ATTR_NOSUID | 345193766fbdSDavid Howells MOUNT_ATTR_NODEV | 345293766fbdSDavid Howells MOUNT_ATTR_NOEXEC | 345393766fbdSDavid Howells MOUNT_ATTR__ATIME | 345493766fbdSDavid Howells MOUNT_ATTR_NODIRATIME)) 345593766fbdSDavid Howells return -EINVAL; 345693766fbdSDavid Howells 345793766fbdSDavid Howells if (attr_flags & MOUNT_ATTR_RDONLY) 345893766fbdSDavid Howells mnt_flags |= MNT_READONLY; 345993766fbdSDavid Howells if (attr_flags & MOUNT_ATTR_NOSUID) 346093766fbdSDavid Howells mnt_flags |= MNT_NOSUID; 346193766fbdSDavid Howells if (attr_flags & MOUNT_ATTR_NODEV) 346293766fbdSDavid Howells mnt_flags |= MNT_NODEV; 346393766fbdSDavid Howells if (attr_flags & MOUNT_ATTR_NOEXEC) 346493766fbdSDavid Howells mnt_flags |= MNT_NOEXEC; 346593766fbdSDavid Howells if (attr_flags & MOUNT_ATTR_NODIRATIME) 346693766fbdSDavid Howells mnt_flags |= MNT_NODIRATIME; 346793766fbdSDavid Howells 346893766fbdSDavid Howells switch (attr_flags & MOUNT_ATTR__ATIME) { 346993766fbdSDavid Howells case MOUNT_ATTR_STRICTATIME: 347093766fbdSDavid Howells break; 347193766fbdSDavid Howells case MOUNT_ATTR_NOATIME: 347293766fbdSDavid Howells mnt_flags |= MNT_NOATIME; 347393766fbdSDavid Howells break; 347493766fbdSDavid Howells case MOUNT_ATTR_RELATIME: 347593766fbdSDavid Howells mnt_flags |= MNT_RELATIME; 347693766fbdSDavid Howells break; 347793766fbdSDavid Howells default: 347893766fbdSDavid Howells return -EINVAL; 347993766fbdSDavid Howells } 348093766fbdSDavid Howells 348193766fbdSDavid Howells f = fdget(fs_fd); 348293766fbdSDavid Howells if (!f.file) 348393766fbdSDavid Howells return -EBADF; 348493766fbdSDavid Howells 348593766fbdSDavid Howells ret = -EINVAL; 348693766fbdSDavid Howells if (f.file->f_op != &fscontext_fops) 348793766fbdSDavid Howells goto err_fsfd; 348893766fbdSDavid Howells 348993766fbdSDavid Howells fc = f.file->private_data; 349093766fbdSDavid Howells 349193766fbdSDavid Howells ret = mutex_lock_interruptible(&fc->uapi_mutex); 349293766fbdSDavid Howells if (ret < 0) 349393766fbdSDavid Howells goto err_fsfd; 349493766fbdSDavid Howells 349593766fbdSDavid Howells /* There must be a valid superblock or we can't mount it */ 349693766fbdSDavid Howells ret = -EINVAL; 349793766fbdSDavid Howells if (!fc->root) 349893766fbdSDavid Howells goto err_unlock; 349993766fbdSDavid Howells 350093766fbdSDavid Howells ret = -EPERM; 350193766fbdSDavid Howells if (mount_too_revealing(fc->root->d_sb, &mnt_flags)) { 350293766fbdSDavid Howells pr_warn("VFS: Mount too revealing\n"); 350393766fbdSDavid Howells goto err_unlock; 350493766fbdSDavid Howells } 350593766fbdSDavid Howells 350693766fbdSDavid Howells ret = -EBUSY; 350793766fbdSDavid Howells if (fc->phase != FS_CONTEXT_AWAITING_MOUNT) 350893766fbdSDavid Howells goto err_unlock; 350993766fbdSDavid Howells 351093766fbdSDavid Howells ret = -EPERM; 351193766fbdSDavid Howells if ((fc->sb_flags & SB_MANDLOCK) && !may_mandlock()) 351293766fbdSDavid Howells goto err_unlock; 351393766fbdSDavid Howells 351493766fbdSDavid Howells newmount.mnt = vfs_create_mount(fc); 351593766fbdSDavid Howells if (IS_ERR(newmount.mnt)) { 351693766fbdSDavid Howells ret = PTR_ERR(newmount.mnt); 351793766fbdSDavid Howells goto err_unlock; 351893766fbdSDavid Howells } 351993766fbdSDavid Howells newmount.dentry = dget(fc->root); 352093766fbdSDavid Howells newmount.mnt->mnt_flags = mnt_flags; 352193766fbdSDavid Howells 352293766fbdSDavid Howells /* We've done the mount bit - now move the file context into more or 352393766fbdSDavid Howells * less the same state as if we'd done an fspick(). We don't want to 352493766fbdSDavid Howells * do any memory allocation or anything like that at this point as we 352593766fbdSDavid Howells * don't want to have to handle any errors incurred. 352693766fbdSDavid Howells */ 352793766fbdSDavid Howells vfs_clean_context(fc); 352893766fbdSDavid Howells 352993766fbdSDavid Howells ns = alloc_mnt_ns(current->nsproxy->mnt_ns->user_ns, true); 353093766fbdSDavid Howells if (IS_ERR(ns)) { 353193766fbdSDavid Howells ret = PTR_ERR(ns); 353293766fbdSDavid Howells goto err_path; 353393766fbdSDavid Howells } 353493766fbdSDavid Howells mnt = real_mount(newmount.mnt); 353593766fbdSDavid Howells mnt->mnt_ns = ns; 353693766fbdSDavid Howells ns->root = mnt; 353793766fbdSDavid Howells ns->mounts = 1; 353893766fbdSDavid Howells list_add(&mnt->mnt_list, &ns->list); 35391b0b9cc8SEric Biggers mntget(newmount.mnt); 354093766fbdSDavid Howells 354193766fbdSDavid Howells /* Attach to an apparent O_PATH fd with a note that we need to unmount 354293766fbdSDavid Howells * it, not just simply put it. 354393766fbdSDavid Howells */ 354493766fbdSDavid Howells file = dentry_open(&newmount, O_PATH, fc->cred); 354593766fbdSDavid Howells if (IS_ERR(file)) { 354693766fbdSDavid Howells dissolve_on_fput(newmount.mnt); 354793766fbdSDavid Howells ret = PTR_ERR(file); 354893766fbdSDavid Howells goto err_path; 354993766fbdSDavid Howells } 355093766fbdSDavid Howells file->f_mode |= FMODE_NEED_UNMOUNT; 355193766fbdSDavid Howells 355293766fbdSDavid Howells ret = get_unused_fd_flags((flags & FSMOUNT_CLOEXEC) ? O_CLOEXEC : 0); 355393766fbdSDavid Howells if (ret >= 0) 355493766fbdSDavid Howells fd_install(ret, file); 355593766fbdSDavid Howells else 355693766fbdSDavid Howells fput(file); 355793766fbdSDavid Howells 355893766fbdSDavid Howells err_path: 355993766fbdSDavid Howells path_put(&newmount); 356093766fbdSDavid Howells err_unlock: 356193766fbdSDavid Howells mutex_unlock(&fc->uapi_mutex); 356293766fbdSDavid Howells err_fsfd: 356393766fbdSDavid Howells fdput(f); 356493766fbdSDavid Howells return ret; 356593766fbdSDavid Howells } 356693766fbdSDavid Howells 356793766fbdSDavid Howells /* 356893766fbdSDavid Howells * Move a mount from one place to another. In combination with 356993766fbdSDavid Howells * fsopen()/fsmount() this is used to install a new mount and in combination 357093766fbdSDavid Howells * with open_tree(OPEN_TREE_CLONE [| AT_RECURSIVE]) it can be used to copy 357193766fbdSDavid Howells * a mount subtree. 35722db154b3SDavid Howells * 35732db154b3SDavid Howells * Note the flags value is a combination of MOVE_MOUNT_* flags. 35742db154b3SDavid Howells */ 35752db154b3SDavid Howells SYSCALL_DEFINE5(move_mount, 35762658ce09SBen Dooks int, from_dfd, const char __user *, from_pathname, 35772658ce09SBen Dooks int, to_dfd, const char __user *, to_pathname, 35782db154b3SDavid Howells unsigned int, flags) 35792db154b3SDavid Howells { 35802db154b3SDavid Howells struct path from_path, to_path; 35812db154b3SDavid Howells unsigned int lflags; 35822db154b3SDavid Howells int ret = 0; 35832db154b3SDavid Howells 35842db154b3SDavid Howells if (!may_mount()) 35852db154b3SDavid Howells return -EPERM; 35862db154b3SDavid Howells 35872db154b3SDavid Howells if (flags & ~MOVE_MOUNT__MASK) 35882db154b3SDavid Howells return -EINVAL; 35892db154b3SDavid Howells 35902db154b3SDavid Howells /* If someone gives a pathname, they aren't permitted to move 35912db154b3SDavid Howells * from an fd that requires unmount as we can't get at the flag 35922db154b3SDavid Howells * to clear it afterwards. 35932db154b3SDavid Howells */ 35942db154b3SDavid Howells lflags = 0; 35952db154b3SDavid Howells if (flags & MOVE_MOUNT_F_SYMLINKS) lflags |= LOOKUP_FOLLOW; 35962db154b3SDavid Howells if (flags & MOVE_MOUNT_F_AUTOMOUNTS) lflags |= LOOKUP_AUTOMOUNT; 35972db154b3SDavid Howells if (flags & MOVE_MOUNT_F_EMPTY_PATH) lflags |= LOOKUP_EMPTY; 35982db154b3SDavid Howells 35992db154b3SDavid Howells ret = user_path_at(from_dfd, from_pathname, lflags, &from_path); 36002db154b3SDavid Howells if (ret < 0) 36012db154b3SDavid Howells return ret; 36022db154b3SDavid Howells 36032db154b3SDavid Howells lflags = 0; 36042db154b3SDavid Howells if (flags & MOVE_MOUNT_T_SYMLINKS) lflags |= LOOKUP_FOLLOW; 36052db154b3SDavid Howells if (flags & MOVE_MOUNT_T_AUTOMOUNTS) lflags |= LOOKUP_AUTOMOUNT; 36062db154b3SDavid Howells if (flags & MOVE_MOUNT_T_EMPTY_PATH) lflags |= LOOKUP_EMPTY; 36072db154b3SDavid Howells 36082db154b3SDavid Howells ret = user_path_at(to_dfd, to_pathname, lflags, &to_path); 36092db154b3SDavid Howells if (ret < 0) 36102db154b3SDavid Howells goto out_from; 36112db154b3SDavid Howells 36122db154b3SDavid Howells ret = security_move_mount(&from_path, &to_path); 36132db154b3SDavid Howells if (ret < 0) 36142db154b3SDavid Howells goto out_to; 36152db154b3SDavid Howells 36162db154b3SDavid Howells ret = do_move_mount(&from_path, &to_path); 36172db154b3SDavid Howells 36182db154b3SDavid Howells out_to: 36192db154b3SDavid Howells path_put(&to_path); 36202db154b3SDavid Howells out_from: 36212db154b3SDavid Howells path_put(&from_path); 36222db154b3SDavid Howells return ret; 36232db154b3SDavid Howells } 36242db154b3SDavid Howells 36252db154b3SDavid Howells /* 3626afac7cbaSAl Viro * Return true if path is reachable from root 3627afac7cbaSAl Viro * 362848a066e7SAl Viro * namespace_sem or mount_lock is held 3629afac7cbaSAl Viro */ 3630643822b4SAl Viro bool is_path_reachable(struct mount *mnt, struct dentry *dentry, 3631afac7cbaSAl Viro const struct path *root) 3632afac7cbaSAl Viro { 3633643822b4SAl Viro while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) { 3634a73324daSAl Viro dentry = mnt->mnt_mountpoint; 36350714a533SAl Viro mnt = mnt->mnt_parent; 3636afac7cbaSAl Viro } 3637643822b4SAl Viro return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry); 3638afac7cbaSAl Viro } 3639afac7cbaSAl Viro 3640640eb7e7SMickaël Salaün bool path_is_under(const struct path *path1, const struct path *path2) 3641afac7cbaSAl Viro { 364225ab4c9bSYaowei Bai bool res; 364348a066e7SAl Viro read_seqlock_excl(&mount_lock); 3644643822b4SAl Viro res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2); 364548a066e7SAl Viro read_sequnlock_excl(&mount_lock); 3646afac7cbaSAl Viro return res; 3647afac7cbaSAl Viro } 3648afac7cbaSAl Viro EXPORT_SYMBOL(path_is_under); 3649afac7cbaSAl Viro 3650afac7cbaSAl Viro /* 36511da177e4SLinus Torvalds * pivot_root Semantics: 36521da177e4SLinus Torvalds * Moves the root file system of the current process to the directory put_old, 36531da177e4SLinus Torvalds * makes new_root as the new root file system of the current process, and sets 36541da177e4SLinus Torvalds * root/cwd of all processes which had them on the current root to new_root. 36551da177e4SLinus Torvalds * 36561da177e4SLinus Torvalds * Restrictions: 36571da177e4SLinus Torvalds * The new_root and put_old must be directories, and must not be on the 36581da177e4SLinus Torvalds * same file system as the current process root. The put_old must be 36591da177e4SLinus Torvalds * underneath new_root, i.e. adding a non-zero number of /.. to the string 36601da177e4SLinus Torvalds * pointed to by put_old must yield the same directory as new_root. No other 36611da177e4SLinus Torvalds * file system may be mounted on put_old. After all, new_root is a mountpoint. 36621da177e4SLinus Torvalds * 36634a0d11faSNeil Brown * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem. 36640c1bc6b8SMauro Carvalho Chehab * See Documentation/filesystems/ramfs-rootfs-initramfs.rst for alternatives 36654a0d11faSNeil Brown * in this situation. 36664a0d11faSNeil Brown * 36671da177e4SLinus Torvalds * Notes: 36681da177e4SLinus Torvalds * - we don't move root/cwd if they are not at the root (reason: if something 36691da177e4SLinus Torvalds * cared enough to change them, it's probably wrong to force them elsewhere) 36701da177e4SLinus Torvalds * - it's okay to pick a root that isn't the root of a file system, e.g. 36711da177e4SLinus Torvalds * /nfs/my_root where /nfs is the mount point. It must be a mountpoint, 36721da177e4SLinus Torvalds * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root 36731da177e4SLinus Torvalds * first. 36741da177e4SLinus Torvalds */ 36753480b257SHeiko Carstens SYSCALL_DEFINE2(pivot_root, const char __user *, new_root, 36763480b257SHeiko Carstens const char __user *, put_old) 36771da177e4SLinus Torvalds { 36782763d119SAl Viro struct path new, old, root; 36792763d119SAl Viro struct mount *new_mnt, *root_mnt, *old_mnt, *root_parent, *ex_parent; 368084d17192SAl Viro struct mountpoint *old_mp, *root_mp; 36811da177e4SLinus Torvalds int error; 36821da177e4SLinus Torvalds 36839b40bc90SAl Viro if (!may_mount()) 36841da177e4SLinus Torvalds return -EPERM; 36851da177e4SLinus Torvalds 3686ce6595a2SAl Viro error = user_path_at(AT_FDCWD, new_root, 3687ce6595a2SAl Viro LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &new); 36881da177e4SLinus Torvalds if (error) 36891da177e4SLinus Torvalds goto out0; 36901da177e4SLinus Torvalds 3691ce6595a2SAl Viro error = user_path_at(AT_FDCWD, put_old, 3692ce6595a2SAl Viro LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &old); 36931da177e4SLinus Torvalds if (error) 36941da177e4SLinus Torvalds goto out1; 36951da177e4SLinus Torvalds 36962d8f3038SAl Viro error = security_sb_pivotroot(&old, &new); 3697b12cea91SAl Viro if (error) 3698b12cea91SAl Viro goto out2; 36991da177e4SLinus Torvalds 3700f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &root); 370184d17192SAl Viro old_mp = lock_mount(&old); 370284d17192SAl Viro error = PTR_ERR(old_mp); 370384d17192SAl Viro if (IS_ERR(old_mp)) 3704b12cea91SAl Viro goto out3; 3705b12cea91SAl Viro 37061da177e4SLinus Torvalds error = -EINVAL; 3707419148daSAl Viro new_mnt = real_mount(new.mnt); 3708419148daSAl Viro root_mnt = real_mount(root.mnt); 370984d17192SAl Viro old_mnt = real_mount(old.mnt); 37102763d119SAl Viro ex_parent = new_mnt->mnt_parent; 37112763d119SAl Viro root_parent = root_mnt->mnt_parent; 371284d17192SAl Viro if (IS_MNT_SHARED(old_mnt) || 37132763d119SAl Viro IS_MNT_SHARED(ex_parent) || 37142763d119SAl Viro IS_MNT_SHARED(root_parent)) 3715b12cea91SAl Viro goto out4; 3716143c8c91SAl Viro if (!check_mnt(root_mnt) || !check_mnt(new_mnt)) 3717b12cea91SAl Viro goto out4; 37185ff9d8a6SEric W. Biederman if (new_mnt->mnt.mnt_flags & MNT_LOCKED) 37195ff9d8a6SEric W. Biederman goto out4; 37201da177e4SLinus Torvalds error = -ENOENT; 3721f3da392eSAlexey Dobriyan if (d_unlinked(new.dentry)) 3722b12cea91SAl Viro goto out4; 37231da177e4SLinus Torvalds error = -EBUSY; 372484d17192SAl Viro if (new_mnt == root_mnt || old_mnt == root_mnt) 3725b12cea91SAl Viro goto out4; /* loop, on the same file system */ 37261da177e4SLinus Torvalds error = -EINVAL; 37278c3ee42eSAl Viro if (root.mnt->mnt_root != root.dentry) 3728b12cea91SAl Viro goto out4; /* not a mountpoint */ 3729676da58dSAl Viro if (!mnt_has_parent(root_mnt)) 3730b12cea91SAl Viro goto out4; /* not attached */ 37312d8f3038SAl Viro if (new.mnt->mnt_root != new.dentry) 3732b12cea91SAl Viro goto out4; /* not a mountpoint */ 3733676da58dSAl Viro if (!mnt_has_parent(new_mnt)) 3734b12cea91SAl Viro goto out4; /* not attached */ 37354ac91378SJan Blunck /* make sure we can reach put_old from new_root */ 373684d17192SAl Viro if (!is_path_reachable(old_mnt, old.dentry, &new)) 3737b12cea91SAl Viro goto out4; 37380d082601SEric W. Biederman /* make certain new is below the root */ 37390d082601SEric W. Biederman if (!is_path_reachable(new_mnt, new.dentry, &root)) 37400d082601SEric W. Biederman goto out4; 3741719ea2fbSAl Viro lock_mount_hash(); 37422763d119SAl Viro umount_mnt(new_mnt); 37432763d119SAl Viro root_mp = unhash_mnt(root_mnt); /* we'll need its mountpoint */ 37445ff9d8a6SEric W. Biederman if (root_mnt->mnt.mnt_flags & MNT_LOCKED) { 37455ff9d8a6SEric W. Biederman new_mnt->mnt.mnt_flags |= MNT_LOCKED; 37465ff9d8a6SEric W. Biederman root_mnt->mnt.mnt_flags &= ~MNT_LOCKED; 37475ff9d8a6SEric W. Biederman } 37484ac91378SJan Blunck /* mount old root on put_old */ 374984d17192SAl Viro attach_mnt(root_mnt, old_mnt, old_mp); 37504ac91378SJan Blunck /* mount new_root on / */ 37512763d119SAl Viro attach_mnt(new_mnt, root_parent, root_mp); 37522763d119SAl Viro mnt_add_count(root_parent, -1); 37536b3286edSKirill Korotaev touch_mnt_namespace(current->nsproxy->mnt_ns); 37544fed655cSEric W. Biederman /* A moved mount should not expire automatically */ 37554fed655cSEric W. Biederman list_del_init(&new_mnt->mnt_expire); 37563895dbf8SEric W. Biederman put_mountpoint(root_mp); 3757719ea2fbSAl Viro unlock_mount_hash(); 37582d8f3038SAl Viro chroot_fs_refs(&root, &new); 37591da177e4SLinus Torvalds error = 0; 3760b12cea91SAl Viro out4: 376184d17192SAl Viro unlock_mount(old_mp); 37622763d119SAl Viro if (!error) 37632763d119SAl Viro mntput_no_expire(ex_parent); 3764b12cea91SAl Viro out3: 37658c3ee42eSAl Viro path_put(&root); 3766b12cea91SAl Viro out2: 37672d8f3038SAl Viro path_put(&old); 37681da177e4SLinus Torvalds out1: 37692d8f3038SAl Viro path_put(&new); 37701da177e4SLinus Torvalds out0: 37711da177e4SLinus Torvalds return error; 37721da177e4SLinus Torvalds } 37731da177e4SLinus Torvalds 37741da177e4SLinus Torvalds static void __init init_mount_tree(void) 37751da177e4SLinus Torvalds { 37761da177e4SLinus Torvalds struct vfsmount *mnt; 377774e83122SAl Viro struct mount *m; 37786b3286edSKirill Korotaev struct mnt_namespace *ns; 3779ac748a09SJan Blunck struct path root; 37801da177e4SLinus Torvalds 3781fd3e007fSAl Viro mnt = vfs_kern_mount(&rootfs_fs_type, 0, "rootfs", NULL); 37821da177e4SLinus Torvalds if (IS_ERR(mnt)) 37831da177e4SLinus Torvalds panic("Can't create rootfs"); 3784b3e19d92SNick Piggin 378574e83122SAl Viro ns = alloc_mnt_ns(&init_user_ns, false); 37863b22edc5STrond Myklebust if (IS_ERR(ns)) 37871da177e4SLinus Torvalds panic("Can't allocate initial namespace"); 378874e83122SAl Viro m = real_mount(mnt); 378974e83122SAl Viro m->mnt_ns = ns; 379074e83122SAl Viro ns->root = m; 379174e83122SAl Viro ns->mounts = 1; 379274e83122SAl Viro list_add(&m->mnt_list, &ns->list); 37936b3286edSKirill Korotaev init_task.nsproxy->mnt_ns = ns; 37946b3286edSKirill Korotaev get_mnt_ns(ns); 37951da177e4SLinus Torvalds 3796be08d6d2SAl Viro root.mnt = mnt; 3797be08d6d2SAl Viro root.dentry = mnt->mnt_root; 3798da362b09SEric W. Biederman mnt->mnt_flags |= MNT_LOCKED; 3799ac748a09SJan Blunck 3800ac748a09SJan Blunck set_fs_pwd(current->fs, &root); 3801ac748a09SJan Blunck set_fs_root(current->fs, &root); 38021da177e4SLinus Torvalds } 38031da177e4SLinus Torvalds 380474bf17cfSDenis Cheng void __init mnt_init(void) 38051da177e4SLinus Torvalds { 380615a67dd8SRandy Dunlap int err; 38071da177e4SLinus Torvalds 38087d6fec45SAl Viro mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount), 380920c2df83SPaul Mundt 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL); 38101da177e4SLinus Torvalds 38110818bf27SAl Viro mount_hashtable = alloc_large_system_hash("Mount-cache", 381238129a13SAl Viro sizeof(struct hlist_head), 38130818bf27SAl Viro mhash_entries, 19, 38143d375d78SPavel Tatashin HASH_ZERO, 38150818bf27SAl Viro &m_hash_shift, &m_hash_mask, 0, 0); 38160818bf27SAl Viro mountpoint_hashtable = alloc_large_system_hash("Mountpoint-cache", 38170818bf27SAl Viro sizeof(struct hlist_head), 38180818bf27SAl Viro mphash_entries, 19, 38193d375d78SPavel Tatashin HASH_ZERO, 38200818bf27SAl Viro &mp_hash_shift, &mp_hash_mask, 0, 0); 38211da177e4SLinus Torvalds 382284d17192SAl Viro if (!mount_hashtable || !mountpoint_hashtable) 38231da177e4SLinus Torvalds panic("Failed to allocate mount hash table\n"); 38241da177e4SLinus Torvalds 38254b93dc9bSTejun Heo kernfs_init(); 38264b93dc9bSTejun Heo 382715a67dd8SRandy Dunlap err = sysfs_init(); 382815a67dd8SRandy Dunlap if (err) 382915a67dd8SRandy Dunlap printk(KERN_WARNING "%s: sysfs_init error: %d\n", 38308e24eea7SHarvey Harrison __func__, err); 383100d26666SGreg Kroah-Hartman fs_kobj = kobject_create_and_add("fs", NULL); 383200d26666SGreg Kroah-Hartman if (!fs_kobj) 38338e24eea7SHarvey Harrison printk(KERN_WARNING "%s: kobj create error\n", __func__); 3834037f11b4SAl Viro shmem_init(); 38351da177e4SLinus Torvalds init_rootfs(); 38361da177e4SLinus Torvalds init_mount_tree(); 38371da177e4SLinus Torvalds } 38381da177e4SLinus Torvalds 3839616511d0STrond Myklebust void put_mnt_ns(struct mnt_namespace *ns) 38401da177e4SLinus Torvalds { 38411a7b8969SKirill Tkhai if (!refcount_dec_and_test(&ns->ns.count)) 3842616511d0STrond Myklebust return; 38437b00ed6fSAl Viro drop_collected_mounts(&ns->root->mnt); 3844771b1371SEric W. Biederman free_mnt_ns(ns); 38451da177e4SLinus Torvalds } 38469d412a43SAl Viro 3847d911b458SDavid Howells struct vfsmount *kern_mount(struct file_system_type *type) 38489d412a43SAl Viro { 3849423e0ab0STim Chen struct vfsmount *mnt; 3850d911b458SDavid Howells mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, NULL); 3851423e0ab0STim Chen if (!IS_ERR(mnt)) { 3852423e0ab0STim Chen /* 3853423e0ab0STim Chen * it is a longterm mount, don't release mnt until 3854423e0ab0STim Chen * we unmount before file sys is unregistered 3855423e0ab0STim Chen */ 3856f7a99c5bSAl Viro real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL; 3857423e0ab0STim Chen } 3858423e0ab0STim Chen return mnt; 38599d412a43SAl Viro } 3860d911b458SDavid Howells EXPORT_SYMBOL_GPL(kern_mount); 3861423e0ab0STim Chen 3862423e0ab0STim Chen void kern_unmount(struct vfsmount *mnt) 3863423e0ab0STim Chen { 3864423e0ab0STim Chen /* release long term mount so mount point can be released */ 3865423e0ab0STim Chen if (!IS_ERR_OR_NULL(mnt)) { 3866f7a99c5bSAl Viro real_mount(mnt)->mnt_ns = NULL; 386748a066e7SAl Viro synchronize_rcu(); /* yecchhh... */ 3868423e0ab0STim Chen mntput(mnt); 3869423e0ab0STim Chen } 3870423e0ab0STim Chen } 3871423e0ab0STim Chen EXPORT_SYMBOL(kern_unmount); 387202125a82SAl Viro 3873df820f8dSMiklos Szeredi void kern_unmount_array(struct vfsmount *mnt[], unsigned int num) 3874df820f8dSMiklos Szeredi { 3875df820f8dSMiklos Szeredi unsigned int i; 3876df820f8dSMiklos Szeredi 3877df820f8dSMiklos Szeredi for (i = 0; i < num; i++) 3878df820f8dSMiklos Szeredi if (mnt[i]) 3879df820f8dSMiklos Szeredi real_mount(mnt[i])->mnt_ns = NULL; 3880df820f8dSMiklos Szeredi synchronize_rcu_expedited(); 3881df820f8dSMiklos Szeredi for (i = 0; i < num; i++) 3882df820f8dSMiklos Szeredi mntput(mnt[i]); 3883df820f8dSMiklos Szeredi } 3884df820f8dSMiklos Szeredi EXPORT_SYMBOL(kern_unmount_array); 3885df820f8dSMiklos Szeredi 388602125a82SAl Viro bool our_mnt(struct vfsmount *mnt) 388702125a82SAl Viro { 3888143c8c91SAl Viro return check_mnt(real_mount(mnt)); 388902125a82SAl Viro } 38908823c079SEric W. Biederman 38913151527eSEric W. Biederman bool current_chrooted(void) 38923151527eSEric W. Biederman { 38933151527eSEric W. Biederman /* Does the current process have a non-standard root */ 38943151527eSEric W. Biederman struct path ns_root; 38953151527eSEric W. Biederman struct path fs_root; 38963151527eSEric W. Biederman bool chrooted; 38973151527eSEric W. Biederman 38983151527eSEric W. Biederman /* Find the namespace root */ 38993151527eSEric W. Biederman ns_root.mnt = ¤t->nsproxy->mnt_ns->root->mnt; 39003151527eSEric W. Biederman ns_root.dentry = ns_root.mnt->mnt_root; 39013151527eSEric W. Biederman path_get(&ns_root); 39023151527eSEric W. Biederman while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root)) 39033151527eSEric W. Biederman ; 39043151527eSEric W. Biederman 39053151527eSEric W. Biederman get_fs_root(current->fs, &fs_root); 39063151527eSEric W. Biederman 39073151527eSEric W. Biederman chrooted = !path_equal(&fs_root, &ns_root); 39083151527eSEric W. Biederman 39093151527eSEric W. Biederman path_put(&fs_root); 39103151527eSEric W. Biederman path_put(&ns_root); 39113151527eSEric W. Biederman 39123151527eSEric W. Biederman return chrooted; 39133151527eSEric W. Biederman } 39143151527eSEric W. Biederman 3915132e4608SDavid Howells static bool mnt_already_visible(struct mnt_namespace *ns, 3916132e4608SDavid Howells const struct super_block *sb, 39178654df4eSEric W. Biederman int *new_mnt_flags) 391887a8ebd6SEric W. Biederman { 39198c6cf9ccSEric W. Biederman int new_flags = *new_mnt_flags; 392087a8ebd6SEric W. Biederman struct mount *mnt; 3921e51db735SEric W. Biederman bool visible = false; 392287a8ebd6SEric W. Biederman 392344bb4385SAl Viro down_read(&namespace_sem); 39249f6c61f9SMiklos Szeredi lock_ns_list(ns); 392587a8ebd6SEric W. Biederman list_for_each_entry(mnt, &ns->list, mnt_list) { 3926e51db735SEric W. Biederman struct mount *child; 392777b1a97dSEric W. Biederman int mnt_flags; 392877b1a97dSEric W. Biederman 39299f6c61f9SMiklos Szeredi if (mnt_is_cursor(mnt)) 39309f6c61f9SMiklos Szeredi continue; 39319f6c61f9SMiklos Szeredi 3932132e4608SDavid Howells if (mnt->mnt.mnt_sb->s_type != sb->s_type) 3933e51db735SEric W. Biederman continue; 3934e51db735SEric W. Biederman 39357e96c1b0SEric W. Biederman /* This mount is not fully visible if it's root directory 39367e96c1b0SEric W. Biederman * is not the root directory of the filesystem. 39377e96c1b0SEric W. Biederman */ 39387e96c1b0SEric W. Biederman if (mnt->mnt.mnt_root != mnt->mnt.mnt_sb->s_root) 39397e96c1b0SEric W. Biederman continue; 39407e96c1b0SEric W. Biederman 3941a1935c17SEric W. Biederman /* A local view of the mount flags */ 394277b1a97dSEric W. Biederman mnt_flags = mnt->mnt.mnt_flags; 394377b1a97dSEric W. Biederman 3944695e9df0SEric W. Biederman /* Don't miss readonly hidden in the superblock flags */ 3945bc98a42cSDavid Howells if (sb_rdonly(mnt->mnt.mnt_sb)) 3946695e9df0SEric W. Biederman mnt_flags |= MNT_LOCK_READONLY; 3947695e9df0SEric W. Biederman 39488c6cf9ccSEric W. Biederman /* Verify the mount flags are equal to or more permissive 39498c6cf9ccSEric W. Biederman * than the proposed new mount. 39508c6cf9ccSEric W. Biederman */ 395177b1a97dSEric W. Biederman if ((mnt_flags & MNT_LOCK_READONLY) && 39528c6cf9ccSEric W. Biederman !(new_flags & MNT_READONLY)) 39538c6cf9ccSEric W. Biederman continue; 395477b1a97dSEric W. Biederman if ((mnt_flags & MNT_LOCK_ATIME) && 395577b1a97dSEric W. Biederman ((mnt_flags & MNT_ATIME_MASK) != (new_flags & MNT_ATIME_MASK))) 39568c6cf9ccSEric W. Biederman continue; 39578c6cf9ccSEric W. Biederman 3958ceeb0e5dSEric W. Biederman /* This mount is not fully visible if there are any 3959ceeb0e5dSEric W. Biederman * locked child mounts that cover anything except for 3960ceeb0e5dSEric W. Biederman * empty directories. 3961e51db735SEric W. Biederman */ 3962e51db735SEric W. Biederman list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) { 3963e51db735SEric W. Biederman struct inode *inode = child->mnt_mountpoint->d_inode; 3964ceeb0e5dSEric W. Biederman /* Only worry about locked mounts */ 3965d71ed6c9SEric W. Biederman if (!(child->mnt.mnt_flags & MNT_LOCKED)) 3966ceeb0e5dSEric W. Biederman continue; 39677236c85eSEric W. Biederman /* Is the directory permanetly empty? */ 39687236c85eSEric W. Biederman if (!is_empty_dir_inode(inode)) 3969e51db735SEric W. Biederman goto next; 397087a8ebd6SEric W. Biederman } 39718c6cf9ccSEric W. Biederman /* Preserve the locked attributes */ 397277b1a97dSEric W. Biederman *new_mnt_flags |= mnt_flags & (MNT_LOCK_READONLY | \ 39738c6cf9ccSEric W. Biederman MNT_LOCK_ATIME); 3974e51db735SEric W. Biederman visible = true; 3975e51db735SEric W. Biederman goto found; 3976e51db735SEric W. Biederman next: ; 397787a8ebd6SEric W. Biederman } 3978e51db735SEric W. Biederman found: 39799f6c61f9SMiklos Szeredi unlock_ns_list(ns); 398044bb4385SAl Viro up_read(&namespace_sem); 3981e51db735SEric W. Biederman return visible; 398287a8ebd6SEric W. Biederman } 398387a8ebd6SEric W. Biederman 3984132e4608SDavid Howells static bool mount_too_revealing(const struct super_block *sb, int *new_mnt_flags) 39858654df4eSEric W. Biederman { 3986a1935c17SEric W. Biederman const unsigned long required_iflags = SB_I_NOEXEC | SB_I_NODEV; 39878654df4eSEric W. Biederman struct mnt_namespace *ns = current->nsproxy->mnt_ns; 39888654df4eSEric W. Biederman unsigned long s_iflags; 39898654df4eSEric W. Biederman 39908654df4eSEric W. Biederman if (ns->user_ns == &init_user_ns) 39918654df4eSEric W. Biederman return false; 39928654df4eSEric W. Biederman 39938654df4eSEric W. Biederman /* Can this filesystem be too revealing? */ 3994132e4608SDavid Howells s_iflags = sb->s_iflags; 39958654df4eSEric W. Biederman if (!(s_iflags & SB_I_USERNS_VISIBLE)) 39968654df4eSEric W. Biederman return false; 39978654df4eSEric W. Biederman 3998a1935c17SEric W. Biederman if ((s_iflags & required_iflags) != required_iflags) { 3999a1935c17SEric W. Biederman WARN_ONCE(1, "Expected s_iflags to contain 0x%lx\n", 4000a1935c17SEric W. Biederman required_iflags); 4001a1935c17SEric W. Biederman return true; 4002a1935c17SEric W. Biederman } 4003a1935c17SEric W. Biederman 4004132e4608SDavid Howells return !mnt_already_visible(ns, sb, new_mnt_flags); 40058654df4eSEric W. Biederman } 40068654df4eSEric W. Biederman 4007380cf5baSAndy Lutomirski bool mnt_may_suid(struct vfsmount *mnt) 4008380cf5baSAndy Lutomirski { 4009380cf5baSAndy Lutomirski /* 4010380cf5baSAndy Lutomirski * Foreign mounts (accessed via fchdir or through /proc 4011380cf5baSAndy Lutomirski * symlinks) are always treated as if they are nosuid. This 4012380cf5baSAndy Lutomirski * prevents namespaces from trusting potentially unsafe 4013380cf5baSAndy Lutomirski * suid/sgid bits, file caps, or security labels that originate 4014380cf5baSAndy Lutomirski * in other namespaces. 4015380cf5baSAndy Lutomirski */ 4016380cf5baSAndy Lutomirski return !(mnt->mnt_flags & MNT_NOSUID) && check_mnt(real_mount(mnt)) && 4017380cf5baSAndy Lutomirski current_in_userns(mnt->mnt_sb->s_user_ns); 4018380cf5baSAndy Lutomirski } 4019380cf5baSAndy Lutomirski 402064964528SAl Viro static struct ns_common *mntns_get(struct task_struct *task) 40218823c079SEric W. Biederman { 402258be2825SAl Viro struct ns_common *ns = NULL; 40238823c079SEric W. Biederman struct nsproxy *nsproxy; 40248823c079SEric W. Biederman 4025728dba3aSEric W. Biederman task_lock(task); 4026728dba3aSEric W. Biederman nsproxy = task->nsproxy; 40278823c079SEric W. Biederman if (nsproxy) { 402858be2825SAl Viro ns = &nsproxy->mnt_ns->ns; 402958be2825SAl Viro get_mnt_ns(to_mnt_ns(ns)); 40308823c079SEric W. Biederman } 4031728dba3aSEric W. Biederman task_unlock(task); 40328823c079SEric W. Biederman 40338823c079SEric W. Biederman return ns; 40348823c079SEric W. Biederman } 40358823c079SEric W. Biederman 403664964528SAl Viro static void mntns_put(struct ns_common *ns) 40378823c079SEric W. Biederman { 403858be2825SAl Viro put_mnt_ns(to_mnt_ns(ns)); 40398823c079SEric W. Biederman } 40408823c079SEric W. Biederman 4041f2a8d52eSChristian Brauner static int mntns_install(struct nsset *nsset, struct ns_common *ns) 40428823c079SEric W. Biederman { 4043f2a8d52eSChristian Brauner struct nsproxy *nsproxy = nsset->nsproxy; 4044f2a8d52eSChristian Brauner struct fs_struct *fs = nsset->fs; 40454f757f3cSAl Viro struct mnt_namespace *mnt_ns = to_mnt_ns(ns), *old_mnt_ns; 4046f2a8d52eSChristian Brauner struct user_namespace *user_ns = nsset->cred->user_ns; 40478823c079SEric W. Biederman struct path root; 40484f757f3cSAl Viro int err; 40498823c079SEric W. Biederman 40500c55cfc4SEric W. Biederman if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) || 4051f2a8d52eSChristian Brauner !ns_capable(user_ns, CAP_SYS_CHROOT) || 4052f2a8d52eSChristian Brauner !ns_capable(user_ns, CAP_SYS_ADMIN)) 4053ae11e0f1SZhao Hongjiang return -EPERM; 40548823c079SEric W. Biederman 405574e83122SAl Viro if (is_anon_ns(mnt_ns)) 405674e83122SAl Viro return -EINVAL; 405774e83122SAl Viro 40588823c079SEric W. Biederman if (fs->users != 1) 40598823c079SEric W. Biederman return -EINVAL; 40608823c079SEric W. Biederman 40618823c079SEric W. Biederman get_mnt_ns(mnt_ns); 40624f757f3cSAl Viro old_mnt_ns = nsproxy->mnt_ns; 40638823c079SEric W. Biederman nsproxy->mnt_ns = mnt_ns; 40648823c079SEric W. Biederman 40658823c079SEric W. Biederman /* Find the root */ 40664f757f3cSAl Viro err = vfs_path_lookup(mnt_ns->root->mnt.mnt_root, &mnt_ns->root->mnt, 40674f757f3cSAl Viro "/", LOOKUP_DOWN, &root); 40684f757f3cSAl Viro if (err) { 40694f757f3cSAl Viro /* revert to old namespace */ 40704f757f3cSAl Viro nsproxy->mnt_ns = old_mnt_ns; 40714f757f3cSAl Viro put_mnt_ns(mnt_ns); 40724f757f3cSAl Viro return err; 40734f757f3cSAl Viro } 40748823c079SEric W. Biederman 40754068367cSAndrei Vagin put_mnt_ns(old_mnt_ns); 40764068367cSAndrei Vagin 40778823c079SEric W. Biederman /* Update the pwd and root */ 40788823c079SEric W. Biederman set_fs_pwd(fs, &root); 40798823c079SEric W. Biederman set_fs_root(fs, &root); 40808823c079SEric W. Biederman 40818823c079SEric W. Biederman path_put(&root); 40828823c079SEric W. Biederman return 0; 40838823c079SEric W. Biederman } 40848823c079SEric W. Biederman 4085bcac25a5SAndrey Vagin static struct user_namespace *mntns_owner(struct ns_common *ns) 4086bcac25a5SAndrey Vagin { 4087bcac25a5SAndrey Vagin return to_mnt_ns(ns)->user_ns; 4088bcac25a5SAndrey Vagin } 4089bcac25a5SAndrey Vagin 40908823c079SEric W. Biederman const struct proc_ns_operations mntns_operations = { 40918823c079SEric W. Biederman .name = "mnt", 40928823c079SEric W. Biederman .type = CLONE_NEWNS, 40938823c079SEric W. Biederman .get = mntns_get, 40948823c079SEric W. Biederman .put = mntns_put, 40958823c079SEric W. Biederman .install = mntns_install, 4096bcac25a5SAndrey Vagin .owner = mntns_owner, 40978823c079SEric W. Biederman }; 4098