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> 289caccd41SChristian Brauner #include <linux/proc_fs.h> 299ea459e1SAl Viro #include <linux/task_work.h> 309164bb4aSIngo Molnar #include <linux/sched/task.h> 31e262e32dSDavid Howells #include <uapi/linux/mount.h> 329bc61ab1SDavid Howells #include <linux/fs_context.h> 33037f11b4SAl Viro #include <linux/shmem_fs.h> 349164bb4aSIngo Molnar 3507b20889SRam Pai #include "pnode.h" 36948730b0SAdrian Bunk #include "internal.h" 371da177e4SLinus Torvalds 38d2921684SEric W. Biederman /* Maximum number of mounts in a mount namespace */ 39d2921684SEric W. Biederman unsigned int sysctl_mount_max __read_mostly = 100000; 40d2921684SEric W. Biederman 410818bf27SAl Viro static unsigned int m_hash_mask __read_mostly; 420818bf27SAl Viro static unsigned int m_hash_shift __read_mostly; 430818bf27SAl Viro static unsigned int mp_hash_mask __read_mostly; 440818bf27SAl Viro static unsigned int mp_hash_shift __read_mostly; 450818bf27SAl Viro 460818bf27SAl Viro static __initdata unsigned long mhash_entries; 470818bf27SAl Viro static int __init set_mhash_entries(char *str) 480818bf27SAl Viro { 490818bf27SAl Viro if (!str) 500818bf27SAl Viro return 0; 510818bf27SAl Viro mhash_entries = simple_strtoul(str, &str, 0); 520818bf27SAl Viro return 1; 530818bf27SAl Viro } 540818bf27SAl Viro __setup("mhash_entries=", set_mhash_entries); 550818bf27SAl Viro 560818bf27SAl Viro static __initdata unsigned long mphash_entries; 570818bf27SAl Viro static int __init set_mphash_entries(char *str) 580818bf27SAl Viro { 590818bf27SAl Viro if (!str) 600818bf27SAl Viro return 0; 610818bf27SAl Viro mphash_entries = simple_strtoul(str, &str, 0); 620818bf27SAl Viro return 1; 630818bf27SAl Viro } 640818bf27SAl Viro __setup("mphash_entries=", set_mphash_entries); 6513f14b4dSEric Dumazet 66c7999c36SAl Viro static u64 event; 6773cd49ecSMiklos Szeredi static DEFINE_IDA(mnt_id_ida); 68719f5d7fSMiklos Szeredi static DEFINE_IDA(mnt_group_ida); 695addc5ddSAl Viro 7038129a13SAl Viro static struct hlist_head *mount_hashtable __read_mostly; 710818bf27SAl Viro static struct hlist_head *mountpoint_hashtable __read_mostly; 72e18b890bSChristoph Lameter static struct kmem_cache *mnt_cache __read_mostly; 7359aa0da8SAl Viro static DECLARE_RWSEM(namespace_sem); 744edbe133SAl Viro static HLIST_HEAD(unmounted); /* protected by namespace_sem */ 754edbe133SAl Viro static LIST_HEAD(ex_mountpoints); /* protected by namespace_sem */ 761da177e4SLinus Torvalds 772a186721SChristian Brauner struct mount_kattr { 782a186721SChristian Brauner unsigned int attr_set; 792a186721SChristian Brauner unsigned int attr_clr; 802a186721SChristian Brauner unsigned int propagation; 812a186721SChristian Brauner unsigned int lookup_flags; 822a186721SChristian Brauner bool recurse; 839caccd41SChristian Brauner struct user_namespace *mnt_userns; 842a186721SChristian Brauner }; 852a186721SChristian Brauner 86f87fd4c2SMiklos Szeredi /* /sys/fs */ 8700d26666SGreg Kroah-Hartman struct kobject *fs_kobj; 8800d26666SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(fs_kobj); 89f87fd4c2SMiklos Szeredi 9099b7db7bSNick Piggin /* 9199b7db7bSNick Piggin * vfsmount lock may be taken for read to prevent changes to the 9299b7db7bSNick Piggin * vfsmount hash, ie. during mountpoint lookups or walking back 9399b7db7bSNick Piggin * up the tree. 9499b7db7bSNick Piggin * 9599b7db7bSNick Piggin * It should be taken for write in all cases where the vfsmount 9699b7db7bSNick Piggin * tree or hash is modified or when a vfsmount structure is modified. 9799b7db7bSNick Piggin */ 9848a066e7SAl Viro __cacheline_aligned_in_smp DEFINE_SEQLOCK(mount_lock); 9999b7db7bSNick Piggin 100d033cb67SChristian Brauner static inline void lock_mount_hash(void) 101d033cb67SChristian Brauner { 102d033cb67SChristian Brauner write_seqlock(&mount_lock); 103d033cb67SChristian Brauner } 104d033cb67SChristian Brauner 105d033cb67SChristian Brauner static inline void unlock_mount_hash(void) 106d033cb67SChristian Brauner { 107d033cb67SChristian Brauner write_sequnlock(&mount_lock); 108d033cb67SChristian Brauner } 109d033cb67SChristian Brauner 11038129a13SAl Viro static inline struct hlist_head *m_hash(struct vfsmount *mnt, struct dentry *dentry) 1111da177e4SLinus Torvalds { 1121da177e4SLinus Torvalds unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES); 1131da177e4SLinus Torvalds tmp += ((unsigned long)dentry / L1_CACHE_BYTES); 1140818bf27SAl Viro tmp = tmp + (tmp >> m_hash_shift); 1150818bf27SAl Viro return &mount_hashtable[tmp & m_hash_mask]; 1160818bf27SAl Viro } 1170818bf27SAl Viro 1180818bf27SAl Viro static inline struct hlist_head *mp_hash(struct dentry *dentry) 1190818bf27SAl Viro { 1200818bf27SAl Viro unsigned long tmp = ((unsigned long)dentry / L1_CACHE_BYTES); 1210818bf27SAl Viro tmp = tmp + (tmp >> mp_hash_shift); 1220818bf27SAl Viro return &mountpoint_hashtable[tmp & mp_hash_mask]; 1231da177e4SLinus Torvalds } 1241da177e4SLinus Torvalds 125b105e270SAl Viro static int mnt_alloc_id(struct mount *mnt) 12673cd49ecSMiklos Szeredi { 127169b480eSMatthew Wilcox int res = ida_alloc(&mnt_id_ida, GFP_KERNEL); 12873cd49ecSMiklos Szeredi 129169b480eSMatthew Wilcox if (res < 0) 13073cd49ecSMiklos Szeredi return res; 131169b480eSMatthew Wilcox mnt->mnt_id = res; 132169b480eSMatthew Wilcox return 0; 13373cd49ecSMiklos Szeredi } 13473cd49ecSMiklos Szeredi 135b105e270SAl Viro static void mnt_free_id(struct mount *mnt) 13673cd49ecSMiklos Szeredi { 137169b480eSMatthew Wilcox ida_free(&mnt_id_ida, mnt->mnt_id); 13873cd49ecSMiklos Szeredi } 13973cd49ecSMiklos Szeredi 140719f5d7fSMiklos Szeredi /* 141719f5d7fSMiklos Szeredi * Allocate a new peer group ID 142719f5d7fSMiklos Szeredi */ 1434b8b21f4SAl Viro static int mnt_alloc_group_id(struct mount *mnt) 144719f5d7fSMiklos Szeredi { 145169b480eSMatthew Wilcox int res = ida_alloc_min(&mnt_group_ida, 1, GFP_KERNEL); 146f21f6220SAl Viro 147169b480eSMatthew Wilcox if (res < 0) 148f21f6220SAl Viro return res; 149169b480eSMatthew Wilcox mnt->mnt_group_id = res; 150169b480eSMatthew Wilcox return 0; 151719f5d7fSMiklos Szeredi } 152719f5d7fSMiklos Szeredi 153719f5d7fSMiklos Szeredi /* 154719f5d7fSMiklos Szeredi * Release a peer group ID 155719f5d7fSMiklos Szeredi */ 1564b8b21f4SAl Viro void mnt_release_group_id(struct mount *mnt) 157719f5d7fSMiklos Szeredi { 158169b480eSMatthew Wilcox ida_free(&mnt_group_ida, mnt->mnt_group_id); 15915169fe7SAl Viro mnt->mnt_group_id = 0; 160719f5d7fSMiklos Szeredi } 161719f5d7fSMiklos Szeredi 162b3e19d92SNick Piggin /* 163b3e19d92SNick Piggin * vfsmount lock must be held for read 164b3e19d92SNick Piggin */ 16583adc753SAl Viro static inline void mnt_add_count(struct mount *mnt, int n) 166b3e19d92SNick Piggin { 167b3e19d92SNick Piggin #ifdef CONFIG_SMP 16868e8a9feSAl Viro this_cpu_add(mnt->mnt_pcp->mnt_count, n); 169b3e19d92SNick Piggin #else 170b3e19d92SNick Piggin preempt_disable(); 17168e8a9feSAl Viro mnt->mnt_count += n; 172b3e19d92SNick Piggin preempt_enable(); 173b3e19d92SNick Piggin #endif 174b3e19d92SNick Piggin } 175b3e19d92SNick Piggin 176b3e19d92SNick Piggin /* 177b3e19d92SNick Piggin * vfsmount lock must be held for write 178b3e19d92SNick Piggin */ 179edf7ddbfSEric Biggers int mnt_get_count(struct mount *mnt) 180b3e19d92SNick Piggin { 181b3e19d92SNick Piggin #ifdef CONFIG_SMP 182edf7ddbfSEric Biggers int count = 0; 183b3e19d92SNick Piggin int cpu; 184b3e19d92SNick Piggin 185b3e19d92SNick Piggin for_each_possible_cpu(cpu) { 18668e8a9feSAl Viro count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count; 187b3e19d92SNick Piggin } 188b3e19d92SNick Piggin 189b3e19d92SNick Piggin return count; 190b3e19d92SNick Piggin #else 19168e8a9feSAl Viro return mnt->mnt_count; 192b3e19d92SNick Piggin #endif 193b3e19d92SNick Piggin } 194b3e19d92SNick Piggin 195b105e270SAl Viro static struct mount *alloc_vfsmnt(const char *name) 1961da177e4SLinus Torvalds { 197c63181e6SAl Viro struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL); 198c63181e6SAl Viro if (mnt) { 19973cd49ecSMiklos Szeredi int err; 20073cd49ecSMiklos Szeredi 201c63181e6SAl Viro err = mnt_alloc_id(mnt); 20288b38782SLi Zefan if (err) 20388b38782SLi Zefan goto out_free_cache; 20488b38782SLi Zefan 20588b38782SLi Zefan if (name) { 20679f6540bSVasily Averin mnt->mnt_devname = kstrdup_const(name, 20779f6540bSVasily Averin GFP_KERNEL_ACCOUNT); 208c63181e6SAl Viro if (!mnt->mnt_devname) 20988b38782SLi Zefan goto out_free_id; 21073cd49ecSMiklos Szeredi } 21173cd49ecSMiklos Szeredi 212b3e19d92SNick Piggin #ifdef CONFIG_SMP 213c63181e6SAl Viro mnt->mnt_pcp = alloc_percpu(struct mnt_pcp); 214c63181e6SAl Viro if (!mnt->mnt_pcp) 215b3e19d92SNick Piggin goto out_free_devname; 216b3e19d92SNick Piggin 217c63181e6SAl Viro this_cpu_add(mnt->mnt_pcp->mnt_count, 1); 218b3e19d92SNick Piggin #else 219c63181e6SAl Viro mnt->mnt_count = 1; 220c63181e6SAl Viro mnt->mnt_writers = 0; 221b3e19d92SNick Piggin #endif 222b3e19d92SNick Piggin 22338129a13SAl Viro INIT_HLIST_NODE(&mnt->mnt_hash); 224c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_child); 225c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_mounts); 226c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_list); 227c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_expire); 228c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_share); 229c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_slave_list); 230c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_slave); 2310a5eb7c8SEric W. Biederman INIT_HLIST_NODE(&mnt->mnt_mp_list); 23299b19d16SEric W. Biederman INIT_LIST_HEAD(&mnt->mnt_umounting); 23356cbb429SAl Viro INIT_HLIST_HEAD(&mnt->mnt_stuck_children); 234a6435940SChristian Brauner mnt->mnt.mnt_userns = &init_user_ns; 2351da177e4SLinus Torvalds } 236c63181e6SAl Viro return mnt; 23788b38782SLi Zefan 238d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 239d3ef3d73Snpiggin@suse.de out_free_devname: 240fcc139aeSAndrzej Hajda kfree_const(mnt->mnt_devname); 241d3ef3d73Snpiggin@suse.de #endif 24288b38782SLi Zefan out_free_id: 243c63181e6SAl Viro mnt_free_id(mnt); 24488b38782SLi Zefan out_free_cache: 245c63181e6SAl Viro kmem_cache_free(mnt_cache, mnt); 24688b38782SLi Zefan return NULL; 2471da177e4SLinus Torvalds } 2481da177e4SLinus Torvalds 2498366025eSDave Hansen /* 2508366025eSDave Hansen * Most r/o checks on a fs are for operations that take 2518366025eSDave Hansen * discrete amounts of time, like a write() or unlink(). 2528366025eSDave Hansen * We must keep track of when those operations start 2538366025eSDave Hansen * (for permission checks) and when they end, so that 2548366025eSDave Hansen * we can determine when writes are able to occur to 2558366025eSDave Hansen * a filesystem. 2568366025eSDave Hansen */ 2573d733633SDave Hansen /* 2583d733633SDave Hansen * __mnt_is_readonly: check whether a mount is read-only 2593d733633SDave Hansen * @mnt: the mount to check for its write status 2603d733633SDave Hansen * 2613d733633SDave Hansen * This shouldn't be used directly ouside of the VFS. 2623d733633SDave Hansen * It does not guarantee that the filesystem will stay 2633d733633SDave Hansen * r/w, just that it is right *now*. This can not and 2643d733633SDave Hansen * should not be used in place of IS_RDONLY(inode). 2653d733633SDave Hansen * mnt_want/drop_write() will _keep_ the filesystem 2663d733633SDave Hansen * r/w. 2673d733633SDave Hansen */ 26843f5e655SDavid Howells bool __mnt_is_readonly(struct vfsmount *mnt) 2693d733633SDave Hansen { 27043f5e655SDavid Howells return (mnt->mnt_flags & MNT_READONLY) || sb_rdonly(mnt->mnt_sb); 2713d733633SDave Hansen } 2723d733633SDave Hansen EXPORT_SYMBOL_GPL(__mnt_is_readonly); 2733d733633SDave Hansen 27483adc753SAl Viro static inline void mnt_inc_writers(struct mount *mnt) 2753d733633SDave Hansen { 276d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 27768e8a9feSAl Viro this_cpu_inc(mnt->mnt_pcp->mnt_writers); 278d3ef3d73Snpiggin@suse.de #else 27968e8a9feSAl Viro mnt->mnt_writers++; 280d3ef3d73Snpiggin@suse.de #endif 2813d733633SDave Hansen } 2823d733633SDave Hansen 28383adc753SAl Viro static inline void mnt_dec_writers(struct mount *mnt) 2843d733633SDave Hansen { 285d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 28668e8a9feSAl Viro this_cpu_dec(mnt->mnt_pcp->mnt_writers); 287d3ef3d73Snpiggin@suse.de #else 28868e8a9feSAl Viro mnt->mnt_writers--; 289d3ef3d73Snpiggin@suse.de #endif 290d3ef3d73Snpiggin@suse.de } 291d3ef3d73Snpiggin@suse.de 29283adc753SAl Viro static unsigned int mnt_get_writers(struct mount *mnt) 293d3ef3d73Snpiggin@suse.de { 294d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 295d3ef3d73Snpiggin@suse.de unsigned int count = 0; 2963d733633SDave Hansen int cpu; 2973d733633SDave Hansen 2983d733633SDave Hansen for_each_possible_cpu(cpu) { 29968e8a9feSAl Viro count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers; 3003d733633SDave Hansen } 3013d733633SDave Hansen 302d3ef3d73Snpiggin@suse.de return count; 303d3ef3d73Snpiggin@suse.de #else 304d3ef3d73Snpiggin@suse.de return mnt->mnt_writers; 305d3ef3d73Snpiggin@suse.de #endif 3063d733633SDave Hansen } 3073d733633SDave Hansen 3084ed5e82fSMiklos Szeredi static int mnt_is_readonly(struct vfsmount *mnt) 3094ed5e82fSMiklos Szeredi { 3104ed5e82fSMiklos Szeredi if (mnt->mnt_sb->s_readonly_remount) 3114ed5e82fSMiklos Szeredi return 1; 3124ed5e82fSMiklos Szeredi /* Order wrt setting s_flags/s_readonly_remount in do_remount() */ 3134ed5e82fSMiklos Szeredi smp_rmb(); 3144ed5e82fSMiklos Szeredi return __mnt_is_readonly(mnt); 3154ed5e82fSMiklos Szeredi } 3164ed5e82fSMiklos Szeredi 3173d733633SDave Hansen /* 318eb04c282SJan Kara * Most r/o & frozen checks on a fs are for operations that take discrete 319eb04c282SJan Kara * amounts of time, like a write() or unlink(). We must keep track of when 320eb04c282SJan Kara * those operations start (for permission checks) and when they end, so that we 321eb04c282SJan Kara * can determine when writes are able to occur to a filesystem. 3223d733633SDave Hansen */ 3238366025eSDave Hansen /** 324eb04c282SJan Kara * __mnt_want_write - get write access to a mount without freeze protection 32583adc753SAl Viro * @m: the mount on which to take a write 3268366025eSDave Hansen * 327eb04c282SJan Kara * This tells the low-level filesystem that a write is about to be performed to 328eb04c282SJan Kara * it, and makes sure that writes are allowed (mnt it read-write) before 329eb04c282SJan Kara * returning success. This operation does not protect against filesystem being 330eb04c282SJan Kara * frozen. When the write operation is finished, __mnt_drop_write() must be 331eb04c282SJan Kara * called. This is effectively a refcount. 3328366025eSDave Hansen */ 333eb04c282SJan Kara int __mnt_want_write(struct vfsmount *m) 3348366025eSDave Hansen { 33583adc753SAl Viro struct mount *mnt = real_mount(m); 3363d733633SDave Hansen int ret = 0; 3373d733633SDave Hansen 338d3ef3d73Snpiggin@suse.de preempt_disable(); 339c6653a83SNick Piggin mnt_inc_writers(mnt); 340d3ef3d73Snpiggin@suse.de /* 341c6653a83SNick Piggin * The store to mnt_inc_writers must be visible before we pass 342d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD loop below, so that the slowpath can see our 343d3ef3d73Snpiggin@suse.de * incremented count after it has set MNT_WRITE_HOLD. 344d3ef3d73Snpiggin@suse.de */ 345d3ef3d73Snpiggin@suse.de smp_mb(); 3466aa7de05SMark Rutland while (READ_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD) 347d3ef3d73Snpiggin@suse.de cpu_relax(); 348d3ef3d73Snpiggin@suse.de /* 349d3ef3d73Snpiggin@suse.de * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will 350d3ef3d73Snpiggin@suse.de * be set to match its requirements. So we must not load that until 351d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD is cleared. 352d3ef3d73Snpiggin@suse.de */ 353d3ef3d73Snpiggin@suse.de smp_rmb(); 3544ed5e82fSMiklos Szeredi if (mnt_is_readonly(m)) { 355c6653a83SNick Piggin mnt_dec_writers(mnt); 3563d733633SDave Hansen ret = -EROFS; 3573d733633SDave Hansen } 358d3ef3d73Snpiggin@suse.de preempt_enable(); 359eb04c282SJan Kara 360eb04c282SJan Kara return ret; 361eb04c282SJan Kara } 362eb04c282SJan Kara 363eb04c282SJan Kara /** 364eb04c282SJan Kara * mnt_want_write - get write access to a mount 365eb04c282SJan Kara * @m: the mount on which to take a write 366eb04c282SJan Kara * 367eb04c282SJan Kara * This tells the low-level filesystem that a write is about to be performed to 368eb04c282SJan Kara * it, and makes sure that writes are allowed (mount is read-write, filesystem 369eb04c282SJan Kara * is not frozen) before returning success. When the write operation is 370eb04c282SJan Kara * finished, mnt_drop_write() must be called. This is effectively a refcount. 371eb04c282SJan Kara */ 372eb04c282SJan Kara int mnt_want_write(struct vfsmount *m) 373eb04c282SJan Kara { 374eb04c282SJan Kara int ret; 375eb04c282SJan Kara 376eb04c282SJan Kara sb_start_write(m->mnt_sb); 377eb04c282SJan Kara ret = __mnt_want_write(m); 378eb04c282SJan Kara if (ret) 379eb04c282SJan Kara sb_end_write(m->mnt_sb); 3803d733633SDave Hansen return ret; 3818366025eSDave Hansen } 3828366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_want_write); 3838366025eSDave Hansen 3848366025eSDave Hansen /** 385eb04c282SJan Kara * __mnt_want_write_file - get write access to a file's mount 386eb04c282SJan Kara * @file: the file who's mount on which to take a write 387eb04c282SJan Kara * 38814e43bf4SEric Biggers * This is like __mnt_want_write, but if the file is already open for writing it 38914e43bf4SEric Biggers * skips incrementing mnt_writers (since the open file already has a reference) 39014e43bf4SEric Biggers * and instead only does the check for emergency r/o remounts. This must be 39114e43bf4SEric Biggers * paired with __mnt_drop_write_file. 392eb04c282SJan Kara */ 393eb04c282SJan Kara int __mnt_want_write_file(struct file *file) 394eb04c282SJan Kara { 39514e43bf4SEric Biggers if (file->f_mode & FMODE_WRITER) { 39614e43bf4SEric Biggers /* 39714e43bf4SEric Biggers * Superblock may have become readonly while there are still 39814e43bf4SEric Biggers * writable fd's, e.g. due to a fs error with errors=remount-ro 39914e43bf4SEric Biggers */ 40014e43bf4SEric Biggers if (__mnt_is_readonly(file->f_path.mnt)) 40114e43bf4SEric Biggers return -EROFS; 40214e43bf4SEric Biggers return 0; 40314e43bf4SEric Biggers } 404eb04c282SJan Kara return __mnt_want_write(file->f_path.mnt); 405eb04c282SJan Kara } 406eb04c282SJan Kara 407eb04c282SJan Kara /** 4087c6893e3SMiklos Szeredi * mnt_want_write_file - get write access to a file's mount 4097c6893e3SMiklos Szeredi * @file: the file who's mount on which to take a write 4107c6893e3SMiklos Szeredi * 41114e43bf4SEric Biggers * This is like mnt_want_write, but if the file is already open for writing it 41214e43bf4SEric Biggers * skips incrementing mnt_writers (since the open file already has a reference) 41314e43bf4SEric Biggers * and instead only does the freeze protection and the check for emergency r/o 41414e43bf4SEric Biggers * remounts. This must be paired with mnt_drop_write_file. 4157c6893e3SMiklos Szeredi */ 4167c6893e3SMiklos Szeredi int mnt_want_write_file(struct file *file) 4177c6893e3SMiklos Szeredi { 4187c6893e3SMiklos Szeredi int ret; 4197c6893e3SMiklos Szeredi 4207c6893e3SMiklos Szeredi sb_start_write(file_inode(file)->i_sb); 4217c6893e3SMiklos Szeredi ret = __mnt_want_write_file(file); 4227c6893e3SMiklos Szeredi if (ret) 4237c6893e3SMiklos Szeredi sb_end_write(file_inode(file)->i_sb); 4247c6893e3SMiklos Szeredi return ret; 4257c6893e3SMiklos Szeredi } 42696029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_want_write_file); 42796029c4eSnpiggin@suse.de 42896029c4eSnpiggin@suse.de /** 429eb04c282SJan Kara * __mnt_drop_write - give up write access to a mount 4308366025eSDave Hansen * @mnt: the mount on which to give up write access 4318366025eSDave Hansen * 4328366025eSDave Hansen * Tells the low-level filesystem that we are done 4338366025eSDave Hansen * performing writes to it. Must be matched with 434eb04c282SJan Kara * __mnt_want_write() call above. 4358366025eSDave Hansen */ 436eb04c282SJan Kara void __mnt_drop_write(struct vfsmount *mnt) 4378366025eSDave Hansen { 438d3ef3d73Snpiggin@suse.de preempt_disable(); 43983adc753SAl Viro mnt_dec_writers(real_mount(mnt)); 440d3ef3d73Snpiggin@suse.de preempt_enable(); 4418366025eSDave Hansen } 442eb04c282SJan Kara 443eb04c282SJan Kara /** 444eb04c282SJan Kara * mnt_drop_write - give up write access to a mount 445eb04c282SJan Kara * @mnt: the mount on which to give up write access 446eb04c282SJan Kara * 447eb04c282SJan Kara * Tells the low-level filesystem that we are done performing writes to it and 448eb04c282SJan Kara * also allows filesystem to be frozen again. Must be matched with 449eb04c282SJan Kara * mnt_want_write() call above. 450eb04c282SJan Kara */ 451eb04c282SJan Kara void mnt_drop_write(struct vfsmount *mnt) 452eb04c282SJan Kara { 453eb04c282SJan Kara __mnt_drop_write(mnt); 454eb04c282SJan Kara sb_end_write(mnt->mnt_sb); 455eb04c282SJan Kara } 4568366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_drop_write); 4578366025eSDave Hansen 458eb04c282SJan Kara void __mnt_drop_write_file(struct file *file) 459eb04c282SJan Kara { 46014e43bf4SEric Biggers if (!(file->f_mode & FMODE_WRITER)) 461eb04c282SJan Kara __mnt_drop_write(file->f_path.mnt); 462eb04c282SJan Kara } 463eb04c282SJan Kara 4647c6893e3SMiklos Szeredi void mnt_drop_write_file(struct file *file) 4657c6893e3SMiklos Szeredi { 466a6795a58SMiklos Szeredi __mnt_drop_write_file(file); 4677c6893e3SMiklos Szeredi sb_end_write(file_inode(file)->i_sb); 4687c6893e3SMiklos Szeredi } 4692a79f17eSAl Viro EXPORT_SYMBOL(mnt_drop_write_file); 4702a79f17eSAl Viro 471fbdc2f6cSChristian Brauner static inline int mnt_hold_writers(struct mount *mnt) 4728366025eSDave Hansen { 47383adc753SAl Viro mnt->mnt.mnt_flags |= MNT_WRITE_HOLD; 474d3ef3d73Snpiggin@suse.de /* 475d3ef3d73Snpiggin@suse.de * After storing MNT_WRITE_HOLD, we'll read the counters. This store 476d3ef3d73Snpiggin@suse.de * should be visible before we do. 477d3ef3d73Snpiggin@suse.de */ 478d3ef3d73Snpiggin@suse.de smp_mb(); 479d3ef3d73Snpiggin@suse.de 480d3ef3d73Snpiggin@suse.de /* 481d3ef3d73Snpiggin@suse.de * With writers on hold, if this value is zero, then there are 482d3ef3d73Snpiggin@suse.de * definitely no active writers (although held writers may subsequently 483d3ef3d73Snpiggin@suse.de * increment the count, they'll have to wait, and decrement it after 484d3ef3d73Snpiggin@suse.de * seeing MNT_READONLY). 485d3ef3d73Snpiggin@suse.de * 486d3ef3d73Snpiggin@suse.de * It is OK to have counter incremented on one CPU and decremented on 487d3ef3d73Snpiggin@suse.de * another: the sum will add up correctly. The danger would be when we 488d3ef3d73Snpiggin@suse.de * sum up each counter, if we read a counter before it is incremented, 489d3ef3d73Snpiggin@suse.de * but then read another CPU's count which it has been subsequently 490d3ef3d73Snpiggin@suse.de * decremented from -- we would see more decrements than we should. 491d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD protects against this scenario, because 492d3ef3d73Snpiggin@suse.de * mnt_want_write first increments count, then smp_mb, then spins on 493d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD, so it can't be decremented by another CPU while 494d3ef3d73Snpiggin@suse.de * we're counting up here. 495d3ef3d73Snpiggin@suse.de */ 496c6653a83SNick Piggin if (mnt_get_writers(mnt) > 0) 497fbdc2f6cSChristian Brauner return -EBUSY; 498fbdc2f6cSChristian Brauner 499fbdc2f6cSChristian Brauner return 0; 500fbdc2f6cSChristian Brauner } 501fbdc2f6cSChristian Brauner 502fbdc2f6cSChristian Brauner static inline void mnt_unhold_writers(struct mount *mnt) 503fbdc2f6cSChristian Brauner { 504d3ef3d73Snpiggin@suse.de /* 505d3ef3d73Snpiggin@suse.de * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers 506d3ef3d73Snpiggin@suse.de * that become unheld will see MNT_READONLY. 507d3ef3d73Snpiggin@suse.de */ 508d3ef3d73Snpiggin@suse.de smp_wmb(); 50983adc753SAl Viro mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD; 510fbdc2f6cSChristian Brauner } 511fbdc2f6cSChristian Brauner 512fbdc2f6cSChristian Brauner static int mnt_make_readonly(struct mount *mnt) 513fbdc2f6cSChristian Brauner { 514fbdc2f6cSChristian Brauner int ret; 515fbdc2f6cSChristian Brauner 516fbdc2f6cSChristian Brauner ret = mnt_hold_writers(mnt); 517fbdc2f6cSChristian Brauner if (!ret) 518fbdc2f6cSChristian Brauner mnt->mnt.mnt_flags |= MNT_READONLY; 519fbdc2f6cSChristian Brauner mnt_unhold_writers(mnt); 5203d733633SDave Hansen return ret; 5213d733633SDave Hansen } 5228366025eSDave Hansen 5234ed5e82fSMiklos Szeredi int sb_prepare_remount_readonly(struct super_block *sb) 5244ed5e82fSMiklos Szeredi { 5254ed5e82fSMiklos Szeredi struct mount *mnt; 5264ed5e82fSMiklos Szeredi int err = 0; 5274ed5e82fSMiklos Szeredi 5288e8b8796SMiklos Szeredi /* Racy optimization. Recheck the counter under MNT_WRITE_HOLD */ 5298e8b8796SMiklos Szeredi if (atomic_long_read(&sb->s_remove_count)) 5308e8b8796SMiklos Szeredi return -EBUSY; 5318e8b8796SMiklos Szeredi 532719ea2fbSAl Viro lock_mount_hash(); 5334ed5e82fSMiklos Szeredi list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) { 5344ed5e82fSMiklos Szeredi if (!(mnt->mnt.mnt_flags & MNT_READONLY)) { 5354ed5e82fSMiklos Szeredi mnt->mnt.mnt_flags |= MNT_WRITE_HOLD; 5364ed5e82fSMiklos Szeredi smp_mb(); 5374ed5e82fSMiklos Szeredi if (mnt_get_writers(mnt) > 0) { 5384ed5e82fSMiklos Szeredi err = -EBUSY; 5394ed5e82fSMiklos Szeredi break; 5404ed5e82fSMiklos Szeredi } 5414ed5e82fSMiklos Szeredi } 5424ed5e82fSMiklos Szeredi } 5438e8b8796SMiklos Szeredi if (!err && atomic_long_read(&sb->s_remove_count)) 5448e8b8796SMiklos Szeredi err = -EBUSY; 5458e8b8796SMiklos Szeredi 5464ed5e82fSMiklos Szeredi if (!err) { 5474ed5e82fSMiklos Szeredi sb->s_readonly_remount = 1; 5484ed5e82fSMiklos Szeredi smp_wmb(); 5494ed5e82fSMiklos Szeredi } 5504ed5e82fSMiklos Szeredi list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) { 5514ed5e82fSMiklos Szeredi if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD) 5524ed5e82fSMiklos Szeredi mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD; 5534ed5e82fSMiklos Szeredi } 554719ea2fbSAl Viro unlock_mount_hash(); 5554ed5e82fSMiklos Szeredi 5564ed5e82fSMiklos Szeredi return err; 5574ed5e82fSMiklos Szeredi } 5584ed5e82fSMiklos Szeredi 559b105e270SAl Viro static void free_vfsmnt(struct mount *mnt) 5601da177e4SLinus Torvalds { 561a6435940SChristian Brauner struct user_namespace *mnt_userns; 562a6435940SChristian Brauner 563a6435940SChristian Brauner mnt_userns = mnt_user_ns(&mnt->mnt); 564a6435940SChristian Brauner if (mnt_userns != &init_user_ns) 565a6435940SChristian Brauner put_user_ns(mnt_userns); 566fcc139aeSAndrzej Hajda kfree_const(mnt->mnt_devname); 567d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 56868e8a9feSAl Viro free_percpu(mnt->mnt_pcp); 569d3ef3d73Snpiggin@suse.de #endif 570b105e270SAl Viro kmem_cache_free(mnt_cache, mnt); 5711da177e4SLinus Torvalds } 5721da177e4SLinus Torvalds 5738ffcb32eSDavid Howells static void delayed_free_vfsmnt(struct rcu_head *head) 5748ffcb32eSDavid Howells { 5758ffcb32eSDavid Howells free_vfsmnt(container_of(head, struct mount, mnt_rcu)); 5768ffcb32eSDavid Howells } 5778ffcb32eSDavid Howells 57848a066e7SAl Viro /* call under rcu_read_lock */ 579294d71ffSAl Viro int __legitimize_mnt(struct vfsmount *bastard, unsigned seq) 58048a066e7SAl Viro { 58148a066e7SAl Viro struct mount *mnt; 58248a066e7SAl Viro if (read_seqretry(&mount_lock, seq)) 583294d71ffSAl Viro return 1; 58448a066e7SAl Viro if (bastard == NULL) 585294d71ffSAl Viro return 0; 58648a066e7SAl Viro mnt = real_mount(bastard); 58748a066e7SAl Viro mnt_add_count(mnt, 1); 588119e1ef8SAl Viro smp_mb(); // see mntput_no_expire() 58948a066e7SAl Viro if (likely(!read_seqretry(&mount_lock, seq))) 590294d71ffSAl Viro return 0; 59148a066e7SAl Viro if (bastard->mnt_flags & MNT_SYNC_UMOUNT) { 59248a066e7SAl Viro mnt_add_count(mnt, -1); 593294d71ffSAl Viro return 1; 59448a066e7SAl Viro } 595119e1ef8SAl Viro lock_mount_hash(); 596119e1ef8SAl Viro if (unlikely(bastard->mnt_flags & MNT_DOOMED)) { 597119e1ef8SAl Viro mnt_add_count(mnt, -1); 598119e1ef8SAl Viro unlock_mount_hash(); 599119e1ef8SAl Viro return 1; 600119e1ef8SAl Viro } 601119e1ef8SAl Viro unlock_mount_hash(); 602119e1ef8SAl Viro /* caller will mntput() */ 603294d71ffSAl Viro return -1; 604294d71ffSAl Viro } 605294d71ffSAl Viro 606294d71ffSAl Viro /* call under rcu_read_lock */ 607294d71ffSAl Viro bool legitimize_mnt(struct vfsmount *bastard, unsigned seq) 608294d71ffSAl Viro { 609294d71ffSAl Viro int res = __legitimize_mnt(bastard, seq); 610294d71ffSAl Viro if (likely(!res)) 611294d71ffSAl Viro return true; 612294d71ffSAl Viro if (unlikely(res < 0)) { 61348a066e7SAl Viro rcu_read_unlock(); 61448a066e7SAl Viro mntput(bastard); 61548a066e7SAl Viro rcu_read_lock(); 616294d71ffSAl Viro } 61748a066e7SAl Viro return false; 61848a066e7SAl Viro } 61948a066e7SAl Viro 6201da177e4SLinus Torvalds /* 621474279dcSAl Viro * find the first mount at @dentry on vfsmount @mnt. 62248a066e7SAl Viro * call under rcu_read_lock() 6231da177e4SLinus Torvalds */ 624474279dcSAl Viro struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry) 6251da177e4SLinus Torvalds { 62638129a13SAl Viro struct hlist_head *head = m_hash(mnt, dentry); 627474279dcSAl Viro struct mount *p; 6281da177e4SLinus Torvalds 62938129a13SAl Viro hlist_for_each_entry_rcu(p, head, mnt_hash) 630474279dcSAl Viro if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry) 631474279dcSAl Viro return p; 632474279dcSAl Viro return NULL; 6331da177e4SLinus Torvalds } 634474279dcSAl Viro 635474279dcSAl Viro /* 636f015f126SDavid Howells * lookup_mnt - Return the first child mount mounted at path 637f015f126SDavid Howells * 638f015f126SDavid Howells * "First" means first mounted chronologically. If you create the 639f015f126SDavid Howells * following mounts: 640f015f126SDavid Howells * 641f015f126SDavid Howells * mount /dev/sda1 /mnt 642f015f126SDavid Howells * mount /dev/sda2 /mnt 643f015f126SDavid Howells * mount /dev/sda3 /mnt 644f015f126SDavid Howells * 645f015f126SDavid Howells * Then lookup_mnt() on the base /mnt dentry in the root mount will 646f015f126SDavid Howells * return successively the root dentry and vfsmount of /dev/sda1, then 647f015f126SDavid Howells * /dev/sda2, then /dev/sda3, then NULL. 648f015f126SDavid Howells * 649f015f126SDavid Howells * lookup_mnt takes a reference to the found vfsmount. 650a05964f3SRam Pai */ 651ca71cf71SAl Viro struct vfsmount *lookup_mnt(const struct path *path) 652a05964f3SRam Pai { 653c7105365SAl Viro struct mount *child_mnt; 65448a066e7SAl Viro struct vfsmount *m; 65548a066e7SAl Viro unsigned seq; 65699b7db7bSNick Piggin 65748a066e7SAl Viro rcu_read_lock(); 65848a066e7SAl Viro do { 65948a066e7SAl Viro seq = read_seqbegin(&mount_lock); 660474279dcSAl Viro child_mnt = __lookup_mnt(path->mnt, path->dentry); 66148a066e7SAl Viro m = child_mnt ? &child_mnt->mnt : NULL; 66248a066e7SAl Viro } while (!legitimize_mnt(m, seq)); 66348a066e7SAl Viro rcu_read_unlock(); 66448a066e7SAl Viro return m; 665a05964f3SRam Pai } 666a05964f3SRam Pai 6679f6c61f9SMiklos Szeredi static inline void lock_ns_list(struct mnt_namespace *ns) 6689f6c61f9SMiklos Szeredi { 6699f6c61f9SMiklos Szeredi spin_lock(&ns->ns_lock); 6709f6c61f9SMiklos Szeredi } 6719f6c61f9SMiklos Szeredi 6729f6c61f9SMiklos Szeredi static inline void unlock_ns_list(struct mnt_namespace *ns) 6739f6c61f9SMiklos Szeredi { 6749f6c61f9SMiklos Szeredi spin_unlock(&ns->ns_lock); 6759f6c61f9SMiklos Szeredi } 6769f6c61f9SMiklos Szeredi 6779f6c61f9SMiklos Szeredi static inline bool mnt_is_cursor(struct mount *mnt) 6789f6c61f9SMiklos Szeredi { 6799f6c61f9SMiklos Szeredi return mnt->mnt.mnt_flags & MNT_CURSOR; 6809f6c61f9SMiklos Szeredi } 6819f6c61f9SMiklos Szeredi 6827af1364fSEric W. Biederman /* 6837af1364fSEric W. Biederman * __is_local_mountpoint - Test to see if dentry is a mountpoint in the 6847af1364fSEric W. Biederman * current mount namespace. 6857af1364fSEric W. Biederman * 6867af1364fSEric W. Biederman * The common case is dentries are not mountpoints at all and that 6877af1364fSEric W. Biederman * test is handled inline. For the slow case when we are actually 6887af1364fSEric W. Biederman * dealing with a mountpoint of some kind, walk through all of the 6897af1364fSEric W. Biederman * mounts in the current mount namespace and test to see if the dentry 6907af1364fSEric W. Biederman * is a mountpoint. 6917af1364fSEric W. Biederman * 6927af1364fSEric W. Biederman * The mount_hashtable is not usable in the context because we 6937af1364fSEric W. Biederman * need to identify all mounts that may be in the current mount 6947af1364fSEric W. Biederman * namespace not just a mount that happens to have some specified 6957af1364fSEric W. Biederman * parent mount. 6967af1364fSEric W. Biederman */ 6977af1364fSEric W. Biederman bool __is_local_mountpoint(struct dentry *dentry) 6987af1364fSEric W. Biederman { 6997af1364fSEric W. Biederman struct mnt_namespace *ns = current->nsproxy->mnt_ns; 7007af1364fSEric W. Biederman struct mount *mnt; 7017af1364fSEric W. Biederman bool is_covered = false; 7027af1364fSEric W. Biederman 7037af1364fSEric W. Biederman down_read(&namespace_sem); 7049f6c61f9SMiklos Szeredi lock_ns_list(ns); 7057af1364fSEric W. Biederman list_for_each_entry(mnt, &ns->list, mnt_list) { 7069f6c61f9SMiklos Szeredi if (mnt_is_cursor(mnt)) 7079f6c61f9SMiklos Szeredi continue; 7087af1364fSEric W. Biederman is_covered = (mnt->mnt_mountpoint == dentry); 7097af1364fSEric W. Biederman if (is_covered) 7107af1364fSEric W. Biederman break; 7117af1364fSEric W. Biederman } 7129f6c61f9SMiklos Szeredi unlock_ns_list(ns); 7137af1364fSEric W. Biederman up_read(&namespace_sem); 7145ad05cc8SNikolay Borisov 7157af1364fSEric W. Biederman return is_covered; 7167af1364fSEric W. Biederman } 7177af1364fSEric W. Biederman 718e2dfa935SEric W. Biederman static struct mountpoint *lookup_mountpoint(struct dentry *dentry) 71984d17192SAl Viro { 7200818bf27SAl Viro struct hlist_head *chain = mp_hash(dentry); 72184d17192SAl Viro struct mountpoint *mp; 72284d17192SAl Viro 7230818bf27SAl Viro hlist_for_each_entry(mp, chain, m_hash) { 72484d17192SAl Viro if (mp->m_dentry == dentry) { 72584d17192SAl Viro mp->m_count++; 72684d17192SAl Viro return mp; 72784d17192SAl Viro } 72884d17192SAl Viro } 729e2dfa935SEric W. Biederman return NULL; 730e2dfa935SEric W. Biederman } 731e2dfa935SEric W. Biederman 7323895dbf8SEric W. Biederman static struct mountpoint *get_mountpoint(struct dentry *dentry) 733e2dfa935SEric W. Biederman { 7343895dbf8SEric W. Biederman struct mountpoint *mp, *new = NULL; 735e2dfa935SEric W. Biederman int ret; 73684d17192SAl Viro 7373895dbf8SEric W. Biederman if (d_mountpoint(dentry)) { 7381e9c75fbSBenjamin Coddington /* might be worth a WARN_ON() */ 7391e9c75fbSBenjamin Coddington if (d_unlinked(dentry)) 7401e9c75fbSBenjamin Coddington return ERR_PTR(-ENOENT); 7413895dbf8SEric W. Biederman mountpoint: 7423895dbf8SEric W. Biederman read_seqlock_excl(&mount_lock); 7433895dbf8SEric W. Biederman mp = lookup_mountpoint(dentry); 7443895dbf8SEric W. Biederman read_sequnlock_excl(&mount_lock); 7453895dbf8SEric W. Biederman if (mp) 7463895dbf8SEric W. Biederman goto done; 74784d17192SAl Viro } 748eed81007SMiklos Szeredi 7493895dbf8SEric W. Biederman if (!new) 7503895dbf8SEric W. Biederman new = kmalloc(sizeof(struct mountpoint), GFP_KERNEL); 7513895dbf8SEric W. Biederman if (!new) 7523895dbf8SEric W. Biederman return ERR_PTR(-ENOMEM); 7533895dbf8SEric W. Biederman 7543895dbf8SEric W. Biederman 7553895dbf8SEric W. Biederman /* Exactly one processes may set d_mounted */ 7563895dbf8SEric W. Biederman ret = d_set_mounted(dentry); 7573895dbf8SEric W. Biederman 7583895dbf8SEric W. Biederman /* Someone else set d_mounted? */ 7593895dbf8SEric W. Biederman if (ret == -EBUSY) 7603895dbf8SEric W. Biederman goto mountpoint; 7613895dbf8SEric W. Biederman 7623895dbf8SEric W. Biederman /* The dentry is not available as a mountpoint? */ 7633895dbf8SEric W. Biederman mp = ERR_PTR(ret); 7643895dbf8SEric W. Biederman if (ret) 7653895dbf8SEric W. Biederman goto done; 7663895dbf8SEric W. Biederman 7673895dbf8SEric W. Biederman /* Add the new mountpoint to the hash table */ 7683895dbf8SEric W. Biederman read_seqlock_excl(&mount_lock); 7694edbe133SAl Viro new->m_dentry = dget(dentry); 7703895dbf8SEric W. Biederman new->m_count = 1; 7713895dbf8SEric W. Biederman hlist_add_head(&new->m_hash, mp_hash(dentry)); 7723895dbf8SEric W. Biederman INIT_HLIST_HEAD(&new->m_list); 7733895dbf8SEric W. Biederman read_sequnlock_excl(&mount_lock); 7743895dbf8SEric W. Biederman 7753895dbf8SEric W. Biederman mp = new; 7763895dbf8SEric W. Biederman new = NULL; 7773895dbf8SEric W. Biederman done: 7783895dbf8SEric W. Biederman kfree(new); 77984d17192SAl Viro return mp; 78084d17192SAl Viro } 78184d17192SAl Viro 7824edbe133SAl Viro /* 7834edbe133SAl Viro * vfsmount lock must be held. Additionally, the caller is responsible 7844edbe133SAl Viro * for serializing calls for given disposal list. 7854edbe133SAl Viro */ 7864edbe133SAl Viro static void __put_mountpoint(struct mountpoint *mp, struct list_head *list) 78784d17192SAl Viro { 78884d17192SAl Viro if (!--mp->m_count) { 78984d17192SAl Viro struct dentry *dentry = mp->m_dentry; 7900a5eb7c8SEric W. Biederman BUG_ON(!hlist_empty(&mp->m_list)); 79184d17192SAl Viro spin_lock(&dentry->d_lock); 79284d17192SAl Viro dentry->d_flags &= ~DCACHE_MOUNTED; 79384d17192SAl Viro spin_unlock(&dentry->d_lock); 7944edbe133SAl Viro dput_to_list(dentry, list); 7950818bf27SAl Viro hlist_del(&mp->m_hash); 79684d17192SAl Viro kfree(mp); 79784d17192SAl Viro } 79884d17192SAl Viro } 79984d17192SAl Viro 8004edbe133SAl Viro /* called with namespace_lock and vfsmount lock */ 8014edbe133SAl Viro static void put_mountpoint(struct mountpoint *mp) 8024edbe133SAl Viro { 8034edbe133SAl Viro __put_mountpoint(mp, &ex_mountpoints); 8044edbe133SAl Viro } 8054edbe133SAl Viro 806143c8c91SAl Viro static inline int check_mnt(struct mount *mnt) 8071da177e4SLinus Torvalds { 8086b3286edSKirill Korotaev return mnt->mnt_ns == current->nsproxy->mnt_ns; 8091da177e4SLinus Torvalds } 8101da177e4SLinus Torvalds 81199b7db7bSNick Piggin /* 81299b7db7bSNick Piggin * vfsmount lock must be held for write 81399b7db7bSNick Piggin */ 8146b3286edSKirill Korotaev static void touch_mnt_namespace(struct mnt_namespace *ns) 8155addc5ddSAl Viro { 8165addc5ddSAl Viro if (ns) { 8175addc5ddSAl Viro ns->event = ++event; 8185addc5ddSAl Viro wake_up_interruptible(&ns->poll); 8195addc5ddSAl Viro } 8205addc5ddSAl Viro } 8215addc5ddSAl Viro 82299b7db7bSNick Piggin /* 82399b7db7bSNick Piggin * vfsmount lock must be held for write 82499b7db7bSNick Piggin */ 8256b3286edSKirill Korotaev static void __touch_mnt_namespace(struct mnt_namespace *ns) 8265addc5ddSAl Viro { 8275addc5ddSAl Viro if (ns && ns->event != event) { 8285addc5ddSAl Viro ns->event = event; 8295addc5ddSAl Viro wake_up_interruptible(&ns->poll); 8305addc5ddSAl Viro } 8315addc5ddSAl Viro } 8325addc5ddSAl Viro 83399b7db7bSNick Piggin /* 83499b7db7bSNick Piggin * vfsmount lock must be held for write 83599b7db7bSNick Piggin */ 836e4e59906SAl Viro static struct mountpoint *unhash_mnt(struct mount *mnt) 8371da177e4SLinus Torvalds { 838e4e59906SAl Viro struct mountpoint *mp; 8390714a533SAl Viro mnt->mnt_parent = mnt; 840a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 8416b41d536SAl Viro list_del_init(&mnt->mnt_child); 84238129a13SAl Viro hlist_del_init_rcu(&mnt->mnt_hash); 8430a5eb7c8SEric W. Biederman hlist_del_init(&mnt->mnt_mp_list); 844e4e59906SAl Viro mp = mnt->mnt_mp; 84584d17192SAl Viro mnt->mnt_mp = NULL; 846e4e59906SAl Viro return mp; 8477bdb11deSEric W. Biederman } 8487bdb11deSEric W. Biederman 8497bdb11deSEric W. Biederman /* 8507bdb11deSEric W. Biederman * vfsmount lock must be held for write 8517bdb11deSEric W. Biederman */ 8526a46c573SEric W. Biederman static void umount_mnt(struct mount *mnt) 8536a46c573SEric W. Biederman { 854e4e59906SAl Viro put_mountpoint(unhash_mnt(mnt)); 8556a46c573SEric W. Biederman } 8566a46c573SEric W. Biederman 8576a46c573SEric W. Biederman /* 8586a46c573SEric W. Biederman * vfsmount lock must be held for write 8596a46c573SEric W. Biederman */ 86084d17192SAl Viro void mnt_set_mountpoint(struct mount *mnt, 86184d17192SAl Viro struct mountpoint *mp, 86244d964d6SAl Viro struct mount *child_mnt) 863b90fa9aeSRam Pai { 86484d17192SAl Viro mp->m_count++; 8653a2393d7SAl Viro mnt_add_count(mnt, 1); /* essentially, that's mntget */ 8664edbe133SAl Viro child_mnt->mnt_mountpoint = mp->m_dentry; 8673a2393d7SAl Viro child_mnt->mnt_parent = mnt; 86884d17192SAl Viro child_mnt->mnt_mp = mp; 8690a5eb7c8SEric W. Biederman hlist_add_head(&child_mnt->mnt_mp_list, &mp->m_list); 870b90fa9aeSRam Pai } 871b90fa9aeSRam Pai 8721064f874SEric W. Biederman static void __attach_mnt(struct mount *mnt, struct mount *parent) 8731064f874SEric W. Biederman { 8741064f874SEric W. Biederman hlist_add_head_rcu(&mnt->mnt_hash, 8751064f874SEric W. Biederman m_hash(&parent->mnt, mnt->mnt_mountpoint)); 8761064f874SEric W. Biederman list_add_tail(&mnt->mnt_child, &parent->mnt_mounts); 8771064f874SEric W. Biederman } 8781064f874SEric W. Biederman 87999b7db7bSNick Piggin /* 88099b7db7bSNick Piggin * vfsmount lock must be held for write 88199b7db7bSNick Piggin */ 88284d17192SAl Viro static void attach_mnt(struct mount *mnt, 88384d17192SAl Viro struct mount *parent, 88484d17192SAl Viro struct mountpoint *mp) 8851da177e4SLinus Torvalds { 88684d17192SAl Viro mnt_set_mountpoint(parent, mp, mnt); 8871064f874SEric W. Biederman __attach_mnt(mnt, parent); 888b90fa9aeSRam Pai } 889b90fa9aeSRam Pai 8901064f874SEric W. Biederman void mnt_change_mountpoint(struct mount *parent, struct mountpoint *mp, struct mount *mnt) 89112a5b529SAl Viro { 8921064f874SEric W. Biederman struct mountpoint *old_mp = mnt->mnt_mp; 8931064f874SEric W. Biederman struct mount *old_parent = mnt->mnt_parent; 8941064f874SEric W. Biederman 8951064f874SEric W. Biederman list_del_init(&mnt->mnt_child); 8961064f874SEric W. Biederman hlist_del_init(&mnt->mnt_mp_list); 8971064f874SEric W. Biederman hlist_del_init_rcu(&mnt->mnt_hash); 8981064f874SEric W. Biederman 8991064f874SEric W. Biederman attach_mnt(mnt, parent, mp); 9001064f874SEric W. Biederman 9011064f874SEric W. Biederman put_mountpoint(old_mp); 9021064f874SEric W. Biederman mnt_add_count(old_parent, -1); 90312a5b529SAl Viro } 90412a5b529SAl Viro 905b90fa9aeSRam Pai /* 90699b7db7bSNick Piggin * vfsmount lock must be held for write 907b90fa9aeSRam Pai */ 9081064f874SEric W. Biederman static void commit_tree(struct mount *mnt) 909b90fa9aeSRam Pai { 9100714a533SAl Viro struct mount *parent = mnt->mnt_parent; 91183adc753SAl Viro struct mount *m; 912b90fa9aeSRam Pai LIST_HEAD(head); 913143c8c91SAl Viro struct mnt_namespace *n = parent->mnt_ns; 914b90fa9aeSRam Pai 9150714a533SAl Viro BUG_ON(parent == mnt); 916b90fa9aeSRam Pai 9171a4eeaf2SAl Viro list_add_tail(&head, &mnt->mnt_list); 918f7a99c5bSAl Viro list_for_each_entry(m, &head, mnt_list) 919143c8c91SAl Viro m->mnt_ns = n; 920f03c6599SAl Viro 921b90fa9aeSRam Pai list_splice(&head, n->list.prev); 922b90fa9aeSRam Pai 923d2921684SEric W. Biederman n->mounts += n->pending_mounts; 924d2921684SEric W. Biederman n->pending_mounts = 0; 925d2921684SEric W. Biederman 9261064f874SEric W. Biederman __attach_mnt(mnt, parent); 9276b3286edSKirill Korotaev touch_mnt_namespace(n); 9281da177e4SLinus Torvalds } 9291da177e4SLinus Torvalds 930909b0a88SAl Viro static struct mount *next_mnt(struct mount *p, struct mount *root) 9311da177e4SLinus Torvalds { 9326b41d536SAl Viro struct list_head *next = p->mnt_mounts.next; 9336b41d536SAl Viro if (next == &p->mnt_mounts) { 9341da177e4SLinus Torvalds while (1) { 935909b0a88SAl Viro if (p == root) 9361da177e4SLinus Torvalds return NULL; 9376b41d536SAl Viro next = p->mnt_child.next; 9386b41d536SAl Viro if (next != &p->mnt_parent->mnt_mounts) 9391da177e4SLinus Torvalds break; 9400714a533SAl Viro p = p->mnt_parent; 9411da177e4SLinus Torvalds } 9421da177e4SLinus Torvalds } 9436b41d536SAl Viro return list_entry(next, struct mount, mnt_child); 9441da177e4SLinus Torvalds } 9451da177e4SLinus Torvalds 946315fc83eSAl Viro static struct mount *skip_mnt_tree(struct mount *p) 9479676f0c6SRam Pai { 9486b41d536SAl Viro struct list_head *prev = p->mnt_mounts.prev; 9496b41d536SAl Viro while (prev != &p->mnt_mounts) { 9506b41d536SAl Viro p = list_entry(prev, struct mount, mnt_child); 9516b41d536SAl Viro prev = p->mnt_mounts.prev; 9529676f0c6SRam Pai } 9539676f0c6SRam Pai return p; 9549676f0c6SRam Pai } 9559676f0c6SRam Pai 9568f291889SAl Viro /** 9578f291889SAl Viro * vfs_create_mount - Create a mount for a configured superblock 9588f291889SAl Viro * @fc: The configuration context with the superblock attached 9598f291889SAl Viro * 9608f291889SAl Viro * Create a mount to an already configured superblock. If necessary, the 9618f291889SAl Viro * caller should invoke vfs_get_tree() before calling this. 9628f291889SAl Viro * 9638f291889SAl Viro * Note that this does not attach the mount to anything. 9648f291889SAl Viro */ 9658f291889SAl Viro struct vfsmount *vfs_create_mount(struct fs_context *fc) 9669d412a43SAl Viro { 967b105e270SAl Viro struct mount *mnt; 9689d412a43SAl Viro 9698f291889SAl Viro if (!fc->root) 9708f291889SAl Viro return ERR_PTR(-EINVAL); 9719d412a43SAl Viro 9728f291889SAl Viro mnt = alloc_vfsmnt(fc->source ?: "none"); 9739d412a43SAl Viro if (!mnt) 9749d412a43SAl Viro return ERR_PTR(-ENOMEM); 9759d412a43SAl Viro 9768f291889SAl Viro if (fc->sb_flags & SB_KERNMOUNT) 977b105e270SAl Viro mnt->mnt.mnt_flags = MNT_INTERNAL; 9789d412a43SAl Viro 9798f291889SAl Viro atomic_inc(&fc->root->d_sb->s_active); 9808f291889SAl Viro mnt->mnt.mnt_sb = fc->root->d_sb; 9818f291889SAl Viro mnt->mnt.mnt_root = dget(fc->root); 982a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 9830714a533SAl Viro mnt->mnt_parent = mnt; 9848f291889SAl Viro 985719ea2fbSAl Viro lock_mount_hash(); 9868f291889SAl Viro list_add_tail(&mnt->mnt_instance, &mnt->mnt.mnt_sb->s_mounts); 987719ea2fbSAl Viro unlock_mount_hash(); 988b105e270SAl Viro return &mnt->mnt; 9899d412a43SAl Viro } 9908f291889SAl Viro EXPORT_SYMBOL(vfs_create_mount); 9918f291889SAl Viro 9928f291889SAl Viro struct vfsmount *fc_mount(struct fs_context *fc) 9938f291889SAl Viro { 9948f291889SAl Viro int err = vfs_get_tree(fc); 9958f291889SAl Viro if (!err) { 9968f291889SAl Viro up_write(&fc->root->d_sb->s_umount); 9978f291889SAl Viro return vfs_create_mount(fc); 9988f291889SAl Viro } 9998f291889SAl Viro return ERR_PTR(err); 10008f291889SAl Viro } 10018f291889SAl Viro EXPORT_SYMBOL(fc_mount); 10028f291889SAl Viro 10039bc61ab1SDavid Howells struct vfsmount *vfs_kern_mount(struct file_system_type *type, 10049bc61ab1SDavid Howells int flags, const char *name, 10059bc61ab1SDavid Howells void *data) 10061da177e4SLinus Torvalds { 10079bc61ab1SDavid Howells struct fs_context *fc; 10088f291889SAl Viro struct vfsmount *mnt; 10099bc61ab1SDavid Howells int ret = 0; 10109d412a43SAl Viro 10119d412a43SAl Viro if (!type) 10123e1aeb00SDavid Howells return ERR_PTR(-EINVAL); 10139d412a43SAl Viro 10149bc61ab1SDavid Howells fc = fs_context_for_mount(type, flags); 10159bc61ab1SDavid Howells if (IS_ERR(fc)) 10169bc61ab1SDavid Howells return ERR_CAST(fc); 10179bc61ab1SDavid Howells 10183e1aeb00SDavid Howells if (name) 10193e1aeb00SDavid Howells ret = vfs_parse_fs_string(fc, "source", 10203e1aeb00SDavid Howells name, strlen(name)); 10219bc61ab1SDavid Howells if (!ret) 10229bc61ab1SDavid Howells ret = parse_monolithic_mount_data(fc, data); 10239bc61ab1SDavid Howells if (!ret) 10248f291889SAl Viro mnt = fc_mount(fc); 10258f291889SAl Viro else 10268f291889SAl Viro mnt = ERR_PTR(ret); 10279d412a43SAl Viro 10289bc61ab1SDavid Howells put_fs_context(fc); 10298f291889SAl Viro return mnt; 10309d412a43SAl Viro } 10319d412a43SAl Viro EXPORT_SYMBOL_GPL(vfs_kern_mount); 10329d412a43SAl Viro 103393faccbbSEric W. Biederman struct vfsmount * 103493faccbbSEric W. Biederman vfs_submount(const struct dentry *mountpoint, struct file_system_type *type, 103593faccbbSEric W. Biederman const char *name, void *data) 103693faccbbSEric W. Biederman { 103793faccbbSEric W. Biederman /* Until it is worked out how to pass the user namespace 103893faccbbSEric W. Biederman * through from the parent mount to the submount don't support 103993faccbbSEric W. Biederman * unprivileged mounts with submounts. 104093faccbbSEric W. Biederman */ 104193faccbbSEric W. Biederman if (mountpoint->d_sb->s_user_ns != &init_user_ns) 104293faccbbSEric W. Biederman return ERR_PTR(-EPERM); 104393faccbbSEric W. Biederman 1044e462ec50SDavid Howells return vfs_kern_mount(type, SB_SUBMOUNT, name, data); 104593faccbbSEric W. Biederman } 104693faccbbSEric W. Biederman EXPORT_SYMBOL_GPL(vfs_submount); 104793faccbbSEric W. Biederman 104887129cc0SAl Viro static struct mount *clone_mnt(struct mount *old, struct dentry *root, 104936341f64SRam Pai int flag) 10501da177e4SLinus Torvalds { 105187129cc0SAl Viro struct super_block *sb = old->mnt.mnt_sb; 1052be34d1a3SDavid Howells struct mount *mnt; 1053be34d1a3SDavid Howells int err; 10541da177e4SLinus Torvalds 1055be34d1a3SDavid Howells mnt = alloc_vfsmnt(old->mnt_devname); 1056be34d1a3SDavid Howells if (!mnt) 1057be34d1a3SDavid Howells return ERR_PTR(-ENOMEM); 1058be34d1a3SDavid Howells 10597a472ef4SEric W. Biederman if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE)) 106015169fe7SAl Viro mnt->mnt_group_id = 0; /* not a peer of original */ 1061719f5d7fSMiklos Szeredi else 106215169fe7SAl Viro mnt->mnt_group_id = old->mnt_group_id; 1063719f5d7fSMiklos Szeredi 106415169fe7SAl Viro if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) { 1065be34d1a3SDavid Howells err = mnt_alloc_group_id(mnt); 1066719f5d7fSMiklos Szeredi if (err) 1067719f5d7fSMiklos Szeredi goto out_free; 1068719f5d7fSMiklos Szeredi } 1069719f5d7fSMiklos Szeredi 107016a34adbSAl Viro mnt->mnt.mnt_flags = old->mnt.mnt_flags; 107116a34adbSAl Viro mnt->mnt.mnt_flags &= ~(MNT_WRITE_HOLD|MNT_MARKED|MNT_INTERNAL); 10725ff9d8a6SEric W. Biederman 10731da177e4SLinus Torvalds atomic_inc(&sb->s_active); 1074a6435940SChristian Brauner mnt->mnt.mnt_userns = mnt_user_ns(&old->mnt); 1075a6435940SChristian Brauner if (mnt->mnt.mnt_userns != &init_user_ns) 1076a6435940SChristian Brauner mnt->mnt.mnt_userns = get_user_ns(mnt->mnt.mnt_userns); 1077b105e270SAl Viro mnt->mnt.mnt_sb = sb; 1078b105e270SAl Viro mnt->mnt.mnt_root = dget(root); 1079a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 10800714a533SAl Viro mnt->mnt_parent = mnt; 1081719ea2fbSAl Viro lock_mount_hash(); 108239f7c4dbSMiklos Szeredi list_add_tail(&mnt->mnt_instance, &sb->s_mounts); 1083719ea2fbSAl Viro unlock_mount_hash(); 1084b90fa9aeSRam Pai 10857a472ef4SEric W. Biederman if ((flag & CL_SLAVE) || 10867a472ef4SEric W. Biederman ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) { 10876776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave_list); 108832301920SAl Viro mnt->mnt_master = old; 1089fc7be130SAl Viro CLEAR_MNT_SHARED(mnt); 10908aec0809SAl Viro } else if (!(flag & CL_PRIVATE)) { 1091fc7be130SAl Viro if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old)) 10926776db3dSAl Viro list_add(&mnt->mnt_share, &old->mnt_share); 1093d10e8defSAl Viro if (IS_MNT_SLAVE(old)) 10946776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave); 1095d10e8defSAl Viro mnt->mnt_master = old->mnt_master; 10965235d448SAl Viro } else { 10975235d448SAl Viro CLEAR_MNT_SHARED(mnt); 10985afe0022SRam Pai } 1099b90fa9aeSRam Pai if (flag & CL_MAKE_SHARED) 11000f0afb1dSAl Viro set_mnt_shared(mnt); 11011da177e4SLinus Torvalds 11021da177e4SLinus Torvalds /* stick the duplicate mount on the same expiry list 11031da177e4SLinus Torvalds * as the original if that was on one */ 110436341f64SRam Pai if (flag & CL_EXPIRE) { 11056776db3dSAl Viro if (!list_empty(&old->mnt_expire)) 11066776db3dSAl Viro list_add(&mnt->mnt_expire, &old->mnt_expire); 11071da177e4SLinus Torvalds } 1108be34d1a3SDavid Howells 1109cb338d06SAl Viro return mnt; 1110719f5d7fSMiklos Szeredi 1111719f5d7fSMiklos Szeredi out_free: 11128ffcb32eSDavid Howells mnt_free_id(mnt); 1113719f5d7fSMiklos Szeredi free_vfsmnt(mnt); 1114be34d1a3SDavid Howells return ERR_PTR(err); 11151da177e4SLinus Torvalds } 11161da177e4SLinus Torvalds 11179ea459e1SAl Viro static void cleanup_mnt(struct mount *mnt) 11189ea459e1SAl Viro { 111956cbb429SAl Viro struct hlist_node *p; 112056cbb429SAl Viro struct mount *m; 11219ea459e1SAl Viro /* 112256cbb429SAl Viro * The warning here probably indicates that somebody messed 112356cbb429SAl Viro * up a mnt_want/drop_write() pair. If this happens, the 112456cbb429SAl Viro * filesystem was probably unable to make r/w->r/o transitions. 11259ea459e1SAl Viro * The locking used to deal with mnt_count decrement provides barriers, 11269ea459e1SAl Viro * so mnt_get_writers() below is safe. 11279ea459e1SAl Viro */ 11289ea459e1SAl Viro WARN_ON(mnt_get_writers(mnt)); 11299ea459e1SAl Viro if (unlikely(mnt->mnt_pins.first)) 11309ea459e1SAl Viro mnt_pin_kill(mnt); 113156cbb429SAl Viro hlist_for_each_entry_safe(m, p, &mnt->mnt_stuck_children, mnt_umount) { 113256cbb429SAl Viro hlist_del(&m->mnt_umount); 113356cbb429SAl Viro mntput(&m->mnt); 113456cbb429SAl Viro } 11359ea459e1SAl Viro fsnotify_vfsmount_delete(&mnt->mnt); 11369ea459e1SAl Viro dput(mnt->mnt.mnt_root); 11379ea459e1SAl Viro deactivate_super(mnt->mnt.mnt_sb); 11389ea459e1SAl Viro mnt_free_id(mnt); 11399ea459e1SAl Viro call_rcu(&mnt->mnt_rcu, delayed_free_vfsmnt); 11409ea459e1SAl Viro } 11419ea459e1SAl Viro 11429ea459e1SAl Viro static void __cleanup_mnt(struct rcu_head *head) 11439ea459e1SAl Viro { 11449ea459e1SAl Viro cleanup_mnt(container_of(head, struct mount, mnt_rcu)); 11459ea459e1SAl Viro } 11469ea459e1SAl Viro 11479ea459e1SAl Viro static LLIST_HEAD(delayed_mntput_list); 11489ea459e1SAl Viro static void delayed_mntput(struct work_struct *unused) 11499ea459e1SAl Viro { 11509ea459e1SAl Viro struct llist_node *node = llist_del_all(&delayed_mntput_list); 115129785735SByungchul Park struct mount *m, *t; 11529ea459e1SAl Viro 115329785735SByungchul Park llist_for_each_entry_safe(m, t, node, mnt_llist) 115429785735SByungchul Park cleanup_mnt(m); 11559ea459e1SAl Viro } 11569ea459e1SAl Viro static DECLARE_DELAYED_WORK(delayed_mntput_work, delayed_mntput); 11579ea459e1SAl Viro 1158900148dcSAl Viro static void mntput_no_expire(struct mount *mnt) 11597b7b1aceSAl Viro { 11604edbe133SAl Viro LIST_HEAD(list); 1161edf7ddbfSEric Biggers int count; 11624edbe133SAl Viro 116348a066e7SAl Viro rcu_read_lock(); 11649ea0a46cSAl Viro if (likely(READ_ONCE(mnt->mnt_ns))) { 11659ea0a46cSAl Viro /* 11669ea0a46cSAl Viro * Since we don't do lock_mount_hash() here, 11679ea0a46cSAl Viro * ->mnt_ns can change under us. However, if it's 11689ea0a46cSAl Viro * non-NULL, then there's a reference that won't 11699ea0a46cSAl Viro * be dropped until after an RCU delay done after 11709ea0a46cSAl Viro * turning ->mnt_ns NULL. So if we observe it 11719ea0a46cSAl Viro * non-NULL under rcu_read_lock(), the reference 11729ea0a46cSAl Viro * we are dropping is not the final one. 11739ea0a46cSAl Viro */ 1174aa9c0e07SAl Viro mnt_add_count(mnt, -1); 117548a066e7SAl Viro rcu_read_unlock(); 117699b7db7bSNick Piggin return; 1177b3e19d92SNick Piggin } 1178719ea2fbSAl Viro lock_mount_hash(); 1179119e1ef8SAl Viro /* 1180119e1ef8SAl Viro * make sure that if __legitimize_mnt() has not seen us grab 1181119e1ef8SAl Viro * mount_lock, we'll see their refcount increment here. 1182119e1ef8SAl Viro */ 1183119e1ef8SAl Viro smp_mb(); 11849ea0a46cSAl Viro mnt_add_count(mnt, -1); 1185edf7ddbfSEric Biggers count = mnt_get_count(mnt); 1186edf7ddbfSEric Biggers if (count != 0) { 1187edf7ddbfSEric Biggers WARN_ON(count < 0); 118848a066e7SAl Viro rcu_read_unlock(); 1189719ea2fbSAl Viro unlock_mount_hash(); 119099b7db7bSNick Piggin return; 119199b7db7bSNick Piggin } 119248a066e7SAl Viro if (unlikely(mnt->mnt.mnt_flags & MNT_DOOMED)) { 119348a066e7SAl Viro rcu_read_unlock(); 119448a066e7SAl Viro unlock_mount_hash(); 119548a066e7SAl Viro return; 119648a066e7SAl Viro } 119748a066e7SAl Viro mnt->mnt.mnt_flags |= MNT_DOOMED; 119848a066e7SAl Viro rcu_read_unlock(); 1199962830dfSAndi Kleen 120039f7c4dbSMiklos Szeredi list_del(&mnt->mnt_instance); 1201ce07d891SEric W. Biederman 1202ce07d891SEric W. Biederman if (unlikely(!list_empty(&mnt->mnt_mounts))) { 1203ce07d891SEric W. Biederman struct mount *p, *tmp; 1204ce07d891SEric W. Biederman list_for_each_entry_safe(p, tmp, &mnt->mnt_mounts, mnt_child) { 12054edbe133SAl Viro __put_mountpoint(unhash_mnt(p), &list); 120656cbb429SAl Viro hlist_add_head(&p->mnt_umount, &mnt->mnt_stuck_children); 1207ce07d891SEric W. Biederman } 1208ce07d891SEric W. Biederman } 1209719ea2fbSAl Viro unlock_mount_hash(); 12104edbe133SAl Viro shrink_dentry_list(&list); 1211649a795aSAl Viro 12129ea459e1SAl Viro if (likely(!(mnt->mnt.mnt_flags & MNT_INTERNAL))) { 12139ea459e1SAl Viro struct task_struct *task = current; 12149ea459e1SAl Viro if (likely(!(task->flags & PF_KTHREAD))) { 12159ea459e1SAl Viro init_task_work(&mnt->mnt_rcu, __cleanup_mnt); 121691989c70SJens Axboe if (!task_work_add(task, &mnt->mnt_rcu, TWA_RESUME)) 12179ea459e1SAl Viro return; 12189ea459e1SAl Viro } 12199ea459e1SAl Viro if (llist_add(&mnt->mnt_llist, &delayed_mntput_list)) 12209ea459e1SAl Viro schedule_delayed_work(&delayed_mntput_work, 1); 12219ea459e1SAl Viro return; 12229ea459e1SAl Viro } 12239ea459e1SAl Viro cleanup_mnt(mnt); 1224b3e19d92SNick Piggin } 1225b3e19d92SNick Piggin 1226b3e19d92SNick Piggin void mntput(struct vfsmount *mnt) 1227b3e19d92SNick Piggin { 1228b3e19d92SNick Piggin if (mnt) { 1229863d684fSAl Viro struct mount *m = real_mount(mnt); 1230b3e19d92SNick Piggin /* avoid cacheline pingpong, hope gcc doesn't get "smart" */ 1231863d684fSAl Viro if (unlikely(m->mnt_expiry_mark)) 1232863d684fSAl Viro m->mnt_expiry_mark = 0; 1233863d684fSAl Viro mntput_no_expire(m); 1234b3e19d92SNick Piggin } 1235b3e19d92SNick Piggin } 1236b3e19d92SNick Piggin EXPORT_SYMBOL(mntput); 1237b3e19d92SNick Piggin 1238b3e19d92SNick Piggin struct vfsmount *mntget(struct vfsmount *mnt) 1239b3e19d92SNick Piggin { 1240b3e19d92SNick Piggin if (mnt) 124183adc753SAl Viro mnt_add_count(real_mount(mnt), 1); 1242b3e19d92SNick Piggin return mnt; 1243b3e19d92SNick Piggin } 1244b3e19d92SNick Piggin EXPORT_SYMBOL(mntget); 1245b3e19d92SNick Piggin 12461f287bc4SRandy Dunlap /** 12471f287bc4SRandy Dunlap * path_is_mountpoint() - Check if path is a mount in the current namespace. 12481f287bc4SRandy Dunlap * @path: path to check 1249c6609c0aSIan Kent * 1250c6609c0aSIan Kent * d_mountpoint() can only be used reliably to establish if a dentry is 1251c6609c0aSIan Kent * not mounted in any namespace and that common case is handled inline. 1252c6609c0aSIan Kent * d_mountpoint() isn't aware of the possibility there may be multiple 1253c6609c0aSIan Kent * mounts using a given dentry in a different namespace. This function 1254c6609c0aSIan Kent * checks if the passed in path is a mountpoint rather than the dentry 1255c6609c0aSIan Kent * alone. 1256c6609c0aSIan Kent */ 1257c6609c0aSIan Kent bool path_is_mountpoint(const struct path *path) 1258c6609c0aSIan Kent { 1259c6609c0aSIan Kent unsigned seq; 1260c6609c0aSIan Kent bool res; 1261c6609c0aSIan Kent 1262c6609c0aSIan Kent if (!d_mountpoint(path->dentry)) 1263c6609c0aSIan Kent return false; 1264c6609c0aSIan Kent 1265c6609c0aSIan Kent rcu_read_lock(); 1266c6609c0aSIan Kent do { 1267c6609c0aSIan Kent seq = read_seqbegin(&mount_lock); 1268c6609c0aSIan Kent res = __path_is_mountpoint(path); 1269c6609c0aSIan Kent } while (read_seqretry(&mount_lock, seq)); 1270c6609c0aSIan Kent rcu_read_unlock(); 1271c6609c0aSIan Kent 1272c6609c0aSIan Kent return res; 1273c6609c0aSIan Kent } 1274c6609c0aSIan Kent EXPORT_SYMBOL(path_is_mountpoint); 1275c6609c0aSIan Kent 1276ca71cf71SAl Viro struct vfsmount *mnt_clone_internal(const struct path *path) 12777b7b1aceSAl Viro { 12783064c356SAl Viro struct mount *p; 12793064c356SAl Viro p = clone_mnt(real_mount(path->mnt), path->dentry, CL_PRIVATE); 12803064c356SAl Viro if (IS_ERR(p)) 12813064c356SAl Viro return ERR_CAST(p); 12823064c356SAl Viro p->mnt.mnt_flags |= MNT_INTERNAL; 12833064c356SAl Viro return &p->mnt; 12847b7b1aceSAl Viro } 12851da177e4SLinus Torvalds 1286a1a2c409SMiklos Szeredi #ifdef CONFIG_PROC_FS 12879f6c61f9SMiklos Szeredi static struct mount *mnt_list_next(struct mnt_namespace *ns, 12889f6c61f9SMiklos Szeredi struct list_head *p) 12899f6c61f9SMiklos Szeredi { 12909f6c61f9SMiklos Szeredi struct mount *mnt, *ret = NULL; 12919f6c61f9SMiklos Szeredi 12929f6c61f9SMiklos Szeredi lock_ns_list(ns); 12939f6c61f9SMiklos Szeredi list_for_each_continue(p, &ns->list) { 12949f6c61f9SMiklos Szeredi mnt = list_entry(p, typeof(*mnt), mnt_list); 12959f6c61f9SMiklos Szeredi if (!mnt_is_cursor(mnt)) { 12969f6c61f9SMiklos Szeredi ret = mnt; 12979f6c61f9SMiklos Szeredi break; 12989f6c61f9SMiklos Szeredi } 12999f6c61f9SMiklos Szeredi } 13009f6c61f9SMiklos Szeredi unlock_ns_list(ns); 13019f6c61f9SMiklos Szeredi 13029f6c61f9SMiklos Szeredi return ret; 13039f6c61f9SMiklos Szeredi } 13049f6c61f9SMiklos Szeredi 13050226f492SAl Viro /* iterator; we want it to have access to namespace_sem, thus here... */ 13061da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos) 13071da177e4SLinus Torvalds { 1308ede1bf0dSYann Droneaud struct proc_mounts *p = m->private; 13099f6c61f9SMiklos Szeredi struct list_head *prev; 13101da177e4SLinus Torvalds 1311390c6843SRam Pai down_read(&namespace_sem); 13129f6c61f9SMiklos Szeredi if (!*pos) { 13139f6c61f9SMiklos Szeredi prev = &p->ns->list; 13149f6c61f9SMiklos Szeredi } else { 13159f6c61f9SMiklos Szeredi prev = &p->cursor.mnt_list; 13169f6c61f9SMiklos Szeredi 13179f6c61f9SMiklos Szeredi /* Read after we'd reached the end? */ 13189f6c61f9SMiklos Szeredi if (list_empty(prev)) 13199f6c61f9SMiklos Szeredi return NULL; 1320c7999c36SAl Viro } 1321c7999c36SAl Viro 13229f6c61f9SMiklos Szeredi return mnt_list_next(p->ns, prev); 13231da177e4SLinus Torvalds } 13241da177e4SLinus Torvalds 13251da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos) 13261da177e4SLinus Torvalds { 1327ede1bf0dSYann Droneaud struct proc_mounts *p = m->private; 13289f6c61f9SMiklos Szeredi struct mount *mnt = v; 1329b0765fb8SPavel Emelianov 13309f6c61f9SMiklos Szeredi ++*pos; 13319f6c61f9SMiklos Szeredi return mnt_list_next(p->ns, &mnt->mnt_list); 13321da177e4SLinus Torvalds } 13331da177e4SLinus Torvalds 13341da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v) 13351da177e4SLinus Torvalds { 13369f6c61f9SMiklos Szeredi struct proc_mounts *p = m->private; 13379f6c61f9SMiklos Szeredi struct mount *mnt = v; 13389f6c61f9SMiklos Szeredi 13399f6c61f9SMiklos Szeredi lock_ns_list(p->ns); 13409f6c61f9SMiklos Szeredi if (mnt) 13419f6c61f9SMiklos Szeredi list_move_tail(&p->cursor.mnt_list, &mnt->mnt_list); 13429f6c61f9SMiklos Szeredi else 13439f6c61f9SMiklos Szeredi list_del_init(&p->cursor.mnt_list); 13449f6c61f9SMiklos Szeredi unlock_ns_list(p->ns); 1345390c6843SRam Pai up_read(&namespace_sem); 13461da177e4SLinus Torvalds } 13471da177e4SLinus Torvalds 13480226f492SAl Viro static int m_show(struct seq_file *m, void *v) 13499f5596afSAl Viro { 1350ede1bf0dSYann Droneaud struct proc_mounts *p = m->private; 13519f6c61f9SMiklos Szeredi struct mount *r = v; 13520226f492SAl Viro return p->show(m, &r->mnt); 13531da177e4SLinus Torvalds } 13541da177e4SLinus Torvalds 1355a1a2c409SMiklos Szeredi const struct seq_operations mounts_op = { 13561da177e4SLinus Torvalds .start = m_start, 13571da177e4SLinus Torvalds .next = m_next, 13581da177e4SLinus Torvalds .stop = m_stop, 13590226f492SAl Viro .show = m_show, 1360b4629fe2SChuck Lever }; 13619f6c61f9SMiklos Szeredi 13629f6c61f9SMiklos Szeredi void mnt_cursor_del(struct mnt_namespace *ns, struct mount *cursor) 13639f6c61f9SMiklos Szeredi { 13649f6c61f9SMiklos Szeredi down_read(&namespace_sem); 13659f6c61f9SMiklos Szeredi lock_ns_list(ns); 13669f6c61f9SMiklos Szeredi list_del(&cursor->mnt_list); 13679f6c61f9SMiklos Szeredi unlock_ns_list(ns); 13689f6c61f9SMiklos Szeredi up_read(&namespace_sem); 13699f6c61f9SMiklos Szeredi } 1370a1a2c409SMiklos Szeredi #endif /* CONFIG_PROC_FS */ 1371b4629fe2SChuck Lever 13721da177e4SLinus Torvalds /** 13731da177e4SLinus Torvalds * may_umount_tree - check if a mount tree is busy 13741f287bc4SRandy Dunlap * @m: root of mount tree 13751da177e4SLinus Torvalds * 13761da177e4SLinus Torvalds * This is called to check if a tree of mounts has any 13771da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts that are 13781da177e4SLinus Torvalds * busy. 13791da177e4SLinus Torvalds */ 1380909b0a88SAl Viro int may_umount_tree(struct vfsmount *m) 13811da177e4SLinus Torvalds { 1382909b0a88SAl Viro struct mount *mnt = real_mount(m); 138336341f64SRam Pai int actual_refs = 0; 138436341f64SRam Pai int minimum_refs = 0; 1385315fc83eSAl Viro struct mount *p; 1386909b0a88SAl Viro BUG_ON(!m); 13871da177e4SLinus Torvalds 1388b3e19d92SNick Piggin /* write lock needed for mnt_get_count */ 1389719ea2fbSAl Viro lock_mount_hash(); 1390909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 139183adc753SAl Viro actual_refs += mnt_get_count(p); 13921da177e4SLinus Torvalds minimum_refs += 2; 13931da177e4SLinus Torvalds } 1394719ea2fbSAl Viro unlock_mount_hash(); 13951da177e4SLinus Torvalds 13961da177e4SLinus Torvalds if (actual_refs > minimum_refs) 13971da177e4SLinus Torvalds return 0; 1398e3474a8eSIan Kent 1399e3474a8eSIan Kent return 1; 14001da177e4SLinus Torvalds } 14011da177e4SLinus Torvalds 14021da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount_tree); 14031da177e4SLinus Torvalds 14041da177e4SLinus Torvalds /** 14051da177e4SLinus Torvalds * may_umount - check if a mount point is busy 14061da177e4SLinus Torvalds * @mnt: root of mount 14071da177e4SLinus Torvalds * 14081da177e4SLinus Torvalds * This is called to check if a mount point has any 14091da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts. If the 14101da177e4SLinus Torvalds * mount has sub mounts this will return busy 14111da177e4SLinus Torvalds * regardless of whether the sub mounts are busy. 14121da177e4SLinus Torvalds * 14131da177e4SLinus Torvalds * Doesn't take quota and stuff into account. IOW, in some cases it will 14141da177e4SLinus Torvalds * give false negatives. The main reason why it's here is that we need 14151da177e4SLinus Torvalds * a non-destructive way to look for easily umountable filesystems. 14161da177e4SLinus Torvalds */ 14171da177e4SLinus Torvalds int may_umount(struct vfsmount *mnt) 14181da177e4SLinus Torvalds { 1419e3474a8eSIan Kent int ret = 1; 14208ad08d8aSAl Viro down_read(&namespace_sem); 1421719ea2fbSAl Viro lock_mount_hash(); 14221ab59738SAl Viro if (propagate_mount_busy(real_mount(mnt), 2)) 1423e3474a8eSIan Kent ret = 0; 1424719ea2fbSAl Viro unlock_mount_hash(); 14258ad08d8aSAl Viro up_read(&namespace_sem); 1426a05964f3SRam Pai return ret; 14271da177e4SLinus Torvalds } 14281da177e4SLinus Torvalds 14291da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount); 14301da177e4SLinus Torvalds 143197216be0SAl Viro static void namespace_unlock(void) 14321da177e4SLinus Torvalds { 1433a3b3c562SEric W. Biederman struct hlist_head head; 143456cbb429SAl Viro struct hlist_node *p; 143556cbb429SAl Viro struct mount *m; 14364edbe133SAl Viro LIST_HEAD(list); 143797216be0SAl Viro 1438a3b3c562SEric W. Biederman hlist_move_list(&unmounted, &head); 14394edbe133SAl Viro list_splice_init(&ex_mountpoints, &list); 1440a3b3c562SEric W. Biederman 144197216be0SAl Viro up_write(&namespace_sem); 1442a3b3c562SEric W. Biederman 14434edbe133SAl Viro shrink_dentry_list(&list); 14444edbe133SAl Viro 1445a3b3c562SEric W. Biederman if (likely(hlist_empty(&head))) 144697216be0SAl Viro return; 144797216be0SAl Viro 144822cb7405SNeilBrown synchronize_rcu_expedited(); 144948a066e7SAl Viro 145056cbb429SAl Viro hlist_for_each_entry_safe(m, p, &head, mnt_umount) { 145156cbb429SAl Viro hlist_del(&m->mnt_umount); 145256cbb429SAl Viro mntput(&m->mnt); 145356cbb429SAl Viro } 145470fbcdf4SRam Pai } 145570fbcdf4SRam Pai 145697216be0SAl Viro static inline void namespace_lock(void) 1457e3197d83SAl Viro { 145897216be0SAl Viro down_write(&namespace_sem); 1459e3197d83SAl Viro } 1460e3197d83SAl Viro 1461e819f152SEric W. Biederman enum umount_tree_flags { 1462e819f152SEric W. Biederman UMOUNT_SYNC = 1, 1463e819f152SEric W. Biederman UMOUNT_PROPAGATE = 2, 1464e0c9c0afSEric W. Biederman UMOUNT_CONNECTED = 4, 1465e819f152SEric W. Biederman }; 1466f2d0a123SEric W. Biederman 1467f2d0a123SEric W. Biederman static bool disconnect_mount(struct mount *mnt, enum umount_tree_flags how) 1468f2d0a123SEric W. Biederman { 1469f2d0a123SEric W. Biederman /* Leaving mounts connected is only valid for lazy umounts */ 1470f2d0a123SEric W. Biederman if (how & UMOUNT_SYNC) 1471f2d0a123SEric W. Biederman return true; 1472f2d0a123SEric W. Biederman 1473f2d0a123SEric W. Biederman /* A mount without a parent has nothing to be connected to */ 1474f2d0a123SEric W. Biederman if (!mnt_has_parent(mnt)) 1475f2d0a123SEric W. Biederman return true; 1476f2d0a123SEric W. Biederman 1477f2d0a123SEric W. Biederman /* Because the reference counting rules change when mounts are 1478f2d0a123SEric W. Biederman * unmounted and connected, umounted mounts may not be 1479f2d0a123SEric W. Biederman * connected to mounted mounts. 1480f2d0a123SEric W. Biederman */ 1481f2d0a123SEric W. Biederman if (!(mnt->mnt_parent->mnt.mnt_flags & MNT_UMOUNT)) 1482f2d0a123SEric W. Biederman return true; 1483f2d0a123SEric W. Biederman 1484f2d0a123SEric W. Biederman /* Has it been requested that the mount remain connected? */ 1485f2d0a123SEric W. Biederman if (how & UMOUNT_CONNECTED) 1486f2d0a123SEric W. Biederman return false; 1487f2d0a123SEric W. Biederman 1488f2d0a123SEric W. Biederman /* Is the mount locked such that it needs to remain connected? */ 1489f2d0a123SEric W. Biederman if (IS_MNT_LOCKED(mnt)) 1490f2d0a123SEric W. Biederman return false; 1491f2d0a123SEric W. Biederman 1492f2d0a123SEric W. Biederman /* By default disconnect the mount */ 1493f2d0a123SEric W. Biederman return true; 1494f2d0a123SEric W. Biederman } 1495f2d0a123SEric W. Biederman 149699b7db7bSNick Piggin /* 149748a066e7SAl Viro * mount_lock must be held 149899b7db7bSNick Piggin * namespace_sem must be held for write 149999b7db7bSNick Piggin */ 1500e819f152SEric W. Biederman static void umount_tree(struct mount *mnt, enum umount_tree_flags how) 150170fbcdf4SRam Pai { 1502c003b26fSEric W. Biederman LIST_HEAD(tmp_list); 1503315fc83eSAl Viro struct mount *p; 150470fbcdf4SRam Pai 15055d88457eSEric W. Biederman if (how & UMOUNT_PROPAGATE) 15065d88457eSEric W. Biederman propagate_mount_unlock(mnt); 15075d88457eSEric W. Biederman 1508c003b26fSEric W. Biederman /* Gather the mounts to umount */ 1509590ce4bcSEric W. Biederman for (p = mnt; p; p = next_mnt(p, mnt)) { 1510590ce4bcSEric W. Biederman p->mnt.mnt_flags |= MNT_UMOUNT; 1511c003b26fSEric W. Biederman list_move(&p->mnt_list, &tmp_list); 1512590ce4bcSEric W. Biederman } 1513c003b26fSEric W. Biederman 1514411a938bSEric W. Biederman /* Hide the mounts from mnt_mounts */ 1515c003b26fSEric W. Biederman list_for_each_entry(p, &tmp_list, mnt_list) { 1516c003b26fSEric W. Biederman list_del_init(&p->mnt_child); 151738129a13SAl Viro } 151870fbcdf4SRam Pai 1519c003b26fSEric W. Biederman /* Add propogated mounts to the tmp_list */ 1520e819f152SEric W. Biederman if (how & UMOUNT_PROPAGATE) 15217b8a53fdSAl Viro propagate_umount(&tmp_list); 1522a05964f3SRam Pai 1523c003b26fSEric W. Biederman while (!list_empty(&tmp_list)) { 1524d2921684SEric W. Biederman struct mnt_namespace *ns; 1525ce07d891SEric W. Biederman bool disconnect; 1526c003b26fSEric W. Biederman p = list_first_entry(&tmp_list, struct mount, mnt_list); 15276776db3dSAl Viro list_del_init(&p->mnt_expire); 15281a4eeaf2SAl Viro list_del_init(&p->mnt_list); 1529d2921684SEric W. Biederman ns = p->mnt_ns; 1530d2921684SEric W. Biederman if (ns) { 1531d2921684SEric W. Biederman ns->mounts--; 1532d2921684SEric W. Biederman __touch_mnt_namespace(ns); 1533d2921684SEric W. Biederman } 153463d37a84SAl Viro p->mnt_ns = NULL; 1535e819f152SEric W. Biederman if (how & UMOUNT_SYNC) 153648a066e7SAl Viro p->mnt.mnt_flags |= MNT_SYNC_UMOUNT; 153787b95ce0SAl Viro 1538f2d0a123SEric W. Biederman disconnect = disconnect_mount(p, how); 1539676da58dSAl Viro if (mnt_has_parent(p)) { 154081b6b061SAl Viro mnt_add_count(p->mnt_parent, -1); 1541ce07d891SEric W. Biederman if (!disconnect) { 1542ce07d891SEric W. Biederman /* Don't forget about p */ 1543ce07d891SEric W. Biederman list_add_tail(&p->mnt_child, &p->mnt_parent->mnt_mounts); 1544ce07d891SEric W. Biederman } else { 15456a46c573SEric W. Biederman umount_mnt(p); 15467c4b93d8SAl Viro } 1547ce07d891SEric W. Biederman } 15480f0afb1dSAl Viro change_mnt_propagation(p, MS_PRIVATE); 154919a1c409SAl Viro if (disconnect) 155019a1c409SAl Viro hlist_add_head(&p->mnt_umount, &unmounted); 155138129a13SAl Viro } 15521da177e4SLinus Torvalds } 15531da177e4SLinus Torvalds 1554b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt); 1555c35038beSAl Viro 15568d0347f6SDavid Howells static int do_umount_root(struct super_block *sb) 15578d0347f6SDavid Howells { 15588d0347f6SDavid Howells int ret = 0; 15598d0347f6SDavid Howells 15608d0347f6SDavid Howells down_write(&sb->s_umount); 15618d0347f6SDavid Howells if (!sb_rdonly(sb)) { 15628d0347f6SDavid Howells struct fs_context *fc; 15638d0347f6SDavid Howells 15648d0347f6SDavid Howells fc = fs_context_for_reconfigure(sb->s_root, SB_RDONLY, 15658d0347f6SDavid Howells SB_RDONLY); 15668d0347f6SDavid Howells if (IS_ERR(fc)) { 15678d0347f6SDavid Howells ret = PTR_ERR(fc); 15688d0347f6SDavid Howells } else { 15698d0347f6SDavid Howells ret = parse_monolithic_mount_data(fc, NULL); 15708d0347f6SDavid Howells if (!ret) 15718d0347f6SDavid Howells ret = reconfigure_super(fc); 15728d0347f6SDavid Howells put_fs_context(fc); 15738d0347f6SDavid Howells } 15748d0347f6SDavid Howells } 15758d0347f6SDavid Howells up_write(&sb->s_umount); 15768d0347f6SDavid Howells return ret; 15778d0347f6SDavid Howells } 15788d0347f6SDavid Howells 15791ab59738SAl Viro static int do_umount(struct mount *mnt, int flags) 15801da177e4SLinus Torvalds { 15811ab59738SAl Viro struct super_block *sb = mnt->mnt.mnt_sb; 15821da177e4SLinus Torvalds int retval; 15831da177e4SLinus Torvalds 15841ab59738SAl Viro retval = security_sb_umount(&mnt->mnt, flags); 15851da177e4SLinus Torvalds if (retval) 15861da177e4SLinus Torvalds return retval; 15871da177e4SLinus Torvalds 15881da177e4SLinus Torvalds /* 15891da177e4SLinus Torvalds * Allow userspace to request a mountpoint be expired rather than 15901da177e4SLinus Torvalds * unmounting unconditionally. Unmount only happens if: 15911da177e4SLinus Torvalds * (1) the mark is already set (the mark is cleared by mntput()) 15921da177e4SLinus Torvalds * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount] 15931da177e4SLinus Torvalds */ 15941da177e4SLinus Torvalds if (flags & MNT_EXPIRE) { 15951ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt || 15961da177e4SLinus Torvalds flags & (MNT_FORCE | MNT_DETACH)) 15971da177e4SLinus Torvalds return -EINVAL; 15981da177e4SLinus Torvalds 1599b3e19d92SNick Piggin /* 1600b3e19d92SNick Piggin * probably don't strictly need the lock here if we examined 1601b3e19d92SNick Piggin * all race cases, but it's a slowpath. 1602b3e19d92SNick Piggin */ 1603719ea2fbSAl Viro lock_mount_hash(); 160483adc753SAl Viro if (mnt_get_count(mnt) != 2) { 1605719ea2fbSAl Viro unlock_mount_hash(); 16061da177e4SLinus Torvalds return -EBUSY; 1607b3e19d92SNick Piggin } 1608719ea2fbSAl Viro unlock_mount_hash(); 16091da177e4SLinus Torvalds 1610863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1)) 16111da177e4SLinus Torvalds return -EAGAIN; 16121da177e4SLinus Torvalds } 16131da177e4SLinus Torvalds 16141da177e4SLinus Torvalds /* 16151da177e4SLinus Torvalds * If we may have to abort operations to get out of this 16161da177e4SLinus Torvalds * mount, and they will themselves hold resources we must 16171da177e4SLinus Torvalds * allow the fs to do things. In the Unix tradition of 16181da177e4SLinus Torvalds * 'Gee thats tricky lets do it in userspace' the umount_begin 16191da177e4SLinus Torvalds * might fail to complete on the first run through as other tasks 16201da177e4SLinus Torvalds * must return, and the like. Thats for the mount program to worry 16211da177e4SLinus Torvalds * about for the moment. 16221da177e4SLinus Torvalds */ 16231da177e4SLinus Torvalds 162442faad99SAl Viro if (flags & MNT_FORCE && sb->s_op->umount_begin) { 162542faad99SAl Viro sb->s_op->umount_begin(sb); 162642faad99SAl Viro } 16271da177e4SLinus Torvalds 16281da177e4SLinus Torvalds /* 16291da177e4SLinus Torvalds * No sense to grab the lock for this test, but test itself looks 16301da177e4SLinus Torvalds * somewhat bogus. Suggestions for better replacement? 16311da177e4SLinus Torvalds * Ho-hum... In principle, we might treat that as umount + switch 16321da177e4SLinus Torvalds * to rootfs. GC would eventually take care of the old vfsmount. 16331da177e4SLinus Torvalds * Actually it makes sense, especially if rootfs would contain a 16341da177e4SLinus Torvalds * /reboot - static binary that would close all descriptors and 16351da177e4SLinus Torvalds * call reboot(9). Then init(8) could umount root and exec /reboot. 16361da177e4SLinus Torvalds */ 16371ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) { 16381da177e4SLinus Torvalds /* 16391da177e4SLinus Torvalds * Special case for "unmounting" root ... 16401da177e4SLinus Torvalds * we just try to remount it readonly. 16411da177e4SLinus Torvalds */ 1642bc6155d1SEric W. Biederman if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN)) 1643a1480dccSAndy Lutomirski return -EPERM; 16448d0347f6SDavid Howells return do_umount_root(sb); 16451da177e4SLinus Torvalds } 16461da177e4SLinus Torvalds 164797216be0SAl Viro namespace_lock(); 1648719ea2fbSAl Viro lock_mount_hash(); 16491da177e4SLinus Torvalds 165025d202edSEric W. Biederman /* Recheck MNT_LOCKED with the locks held */ 165125d202edSEric W. Biederman retval = -EINVAL; 165225d202edSEric W. Biederman if (mnt->mnt.mnt_flags & MNT_LOCKED) 165325d202edSEric W. Biederman goto out; 165425d202edSEric W. Biederman 165525d202edSEric W. Biederman event++; 165648a066e7SAl Viro if (flags & MNT_DETACH) { 165748a066e7SAl Viro if (!list_empty(&mnt->mnt_list)) 1658e819f152SEric W. Biederman umount_tree(mnt, UMOUNT_PROPAGATE); 165948a066e7SAl Viro retval = 0; 166048a066e7SAl Viro } else { 1661b54b9be7SAl Viro shrink_submounts(mnt); 16621da177e4SLinus Torvalds retval = -EBUSY; 166348a066e7SAl Viro if (!propagate_mount_busy(mnt, 2)) { 16641a4eeaf2SAl Viro if (!list_empty(&mnt->mnt_list)) 1665e819f152SEric W. Biederman umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC); 16661da177e4SLinus Torvalds retval = 0; 16671da177e4SLinus Torvalds } 166848a066e7SAl Viro } 166925d202edSEric W. Biederman out: 1670719ea2fbSAl Viro unlock_mount_hash(); 1671e3197d83SAl Viro namespace_unlock(); 16721da177e4SLinus Torvalds return retval; 16731da177e4SLinus Torvalds } 16741da177e4SLinus Torvalds 16751da177e4SLinus Torvalds /* 167680b5dce8SEric W. Biederman * __detach_mounts - lazily unmount all mounts on the specified dentry 167780b5dce8SEric W. Biederman * 167880b5dce8SEric W. Biederman * During unlink, rmdir, and d_drop it is possible to loose the path 167980b5dce8SEric W. Biederman * to an existing mountpoint, and wind up leaking the mount. 168080b5dce8SEric W. Biederman * detach_mounts allows lazily unmounting those mounts instead of 168180b5dce8SEric W. Biederman * leaking them. 168280b5dce8SEric W. Biederman * 168380b5dce8SEric W. Biederman * The caller may hold dentry->d_inode->i_mutex. 168480b5dce8SEric W. Biederman */ 168580b5dce8SEric W. Biederman void __detach_mounts(struct dentry *dentry) 168680b5dce8SEric W. Biederman { 168780b5dce8SEric W. Biederman struct mountpoint *mp; 168880b5dce8SEric W. Biederman struct mount *mnt; 168980b5dce8SEric W. Biederman 169080b5dce8SEric W. Biederman namespace_lock(); 16913895dbf8SEric W. Biederman lock_mount_hash(); 169280b5dce8SEric W. Biederman mp = lookup_mountpoint(dentry); 1693adc9b5c0SAl Viro if (!mp) 169480b5dce8SEric W. Biederman goto out_unlock; 169580b5dce8SEric W. Biederman 1696e06b933eSAndrey Ulanov event++; 169780b5dce8SEric W. Biederman while (!hlist_empty(&mp->m_list)) { 169880b5dce8SEric W. Biederman mnt = hlist_entry(mp->m_list.first, struct mount, mnt_mp_list); 1699ce07d891SEric W. Biederman if (mnt->mnt.mnt_flags & MNT_UMOUNT) { 1700fe78fcc8SEric W. Biederman umount_mnt(mnt); 170156cbb429SAl Viro hlist_add_head(&mnt->mnt_umount, &unmounted); 1702ce07d891SEric W. Biederman } 1703e0c9c0afSEric W. Biederman else umount_tree(mnt, UMOUNT_CONNECTED); 170480b5dce8SEric W. Biederman } 170580b5dce8SEric W. Biederman put_mountpoint(mp); 170680b5dce8SEric W. Biederman out_unlock: 17073895dbf8SEric W. Biederman unlock_mount_hash(); 170880b5dce8SEric W. Biederman namespace_unlock(); 170980b5dce8SEric W. Biederman } 171080b5dce8SEric W. Biederman 171180b5dce8SEric W. Biederman /* 17129b40bc90SAl Viro * Is the caller allowed to modify his namespace? 17139b40bc90SAl Viro */ 17149b40bc90SAl Viro static inline bool may_mount(void) 17159b40bc90SAl Viro { 17169b40bc90SAl Viro return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN); 17179b40bc90SAl Viro } 17189b40bc90SAl Viro 1719df2474a2SJeff Layton #ifdef CONFIG_MANDATORY_FILE_LOCKING 1720fdd92b64SJeff Layton static bool may_mandlock(void) 17219e8925b6SJeff Layton { 1722fdd92b64SJeff Layton pr_warn_once("======================================================\n" 1723fdd92b64SJeff Layton "WARNING: the mand mount option is being deprecated and\n" 1724fdd92b64SJeff Layton " will be removed in v5.15!\n" 1725fdd92b64SJeff Layton "======================================================\n"); 172695ace754SEric W. Biederman return capable(CAP_SYS_ADMIN); 17279e8925b6SJeff Layton } 1728df2474a2SJeff Layton #else 1729df2474a2SJeff Layton static inline bool may_mandlock(void) 1730df2474a2SJeff Layton { 1731df2474a2SJeff Layton pr_warn("VFS: \"mand\" mount option not supported"); 1732df2474a2SJeff Layton return false; 1733df2474a2SJeff Layton } 1734df2474a2SJeff Layton #endif 17359e8925b6SJeff Layton 173625ccd24fSChristoph Hellwig static int can_umount(const struct path *path, int flags) 17371da177e4SLinus Torvalds { 173825ccd24fSChristoph Hellwig struct mount *mnt = real_mount(path->mnt); 17391da177e4SLinus Torvalds 17409b40bc90SAl Viro if (!may_mount()) 17419b40bc90SAl Viro return -EPERM; 174241525f56SChristoph Hellwig if (path->dentry != path->mnt->mnt_root) 174325ccd24fSChristoph Hellwig return -EINVAL; 1744143c8c91SAl Viro if (!check_mnt(mnt)) 174525ccd24fSChristoph Hellwig return -EINVAL; 174625d202edSEric W. Biederman if (mnt->mnt.mnt_flags & MNT_LOCKED) /* Check optimistically */ 174725ccd24fSChristoph Hellwig return -EINVAL; 1748b2f5d4dcSEric W. Biederman if (flags & MNT_FORCE && !capable(CAP_SYS_ADMIN)) 174925ccd24fSChristoph Hellwig return -EPERM; 175025ccd24fSChristoph Hellwig return 0; 175125ccd24fSChristoph Hellwig } 17521da177e4SLinus Torvalds 1753a0a6df9aSAl Viro // caller is responsible for flags being sane 175425ccd24fSChristoph Hellwig int path_umount(struct path *path, int flags) 175525ccd24fSChristoph Hellwig { 175625ccd24fSChristoph Hellwig struct mount *mnt = real_mount(path->mnt); 175725ccd24fSChristoph Hellwig int ret; 175825ccd24fSChristoph Hellwig 175925ccd24fSChristoph Hellwig ret = can_umount(path, flags); 176025ccd24fSChristoph Hellwig if (!ret) 176125ccd24fSChristoph Hellwig ret = do_umount(mnt, flags); 176225ccd24fSChristoph Hellwig 1763429731b1SJan Blunck /* we mustn't call path_put() as that would clear mnt_expiry_mark */ 176441525f56SChristoph Hellwig dput(path->dentry); 1765900148dcSAl Viro mntput_no_expire(mnt); 176625ccd24fSChristoph Hellwig return ret; 17671da177e4SLinus Torvalds } 17681da177e4SLinus Torvalds 176909267defSChristoph Hellwig static int ksys_umount(char __user *name, int flags) 177041525f56SChristoph Hellwig { 177141525f56SChristoph Hellwig int lookup_flags = LOOKUP_MOUNTPOINT; 177241525f56SChristoph Hellwig struct path path; 177341525f56SChristoph Hellwig int ret; 177441525f56SChristoph Hellwig 1775a0a6df9aSAl Viro // basic validity checks done first 1776a0a6df9aSAl Viro if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW)) 1777a0a6df9aSAl Viro return -EINVAL; 1778a0a6df9aSAl Viro 177941525f56SChristoph Hellwig if (!(flags & UMOUNT_NOFOLLOW)) 178041525f56SChristoph Hellwig lookup_flags |= LOOKUP_FOLLOW; 178141525f56SChristoph Hellwig ret = user_path_at(AT_FDCWD, name, lookup_flags, &path); 178241525f56SChristoph Hellwig if (ret) 178341525f56SChristoph Hellwig return ret; 178441525f56SChristoph Hellwig return path_umount(&path, flags); 178541525f56SChristoph Hellwig } 178641525f56SChristoph Hellwig 17873a18ef5cSDominik Brodowski SYSCALL_DEFINE2(umount, char __user *, name, int, flags) 17883a18ef5cSDominik Brodowski { 17893a18ef5cSDominik Brodowski return ksys_umount(name, flags); 17903a18ef5cSDominik Brodowski } 17913a18ef5cSDominik Brodowski 17921da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT 17931da177e4SLinus Torvalds 17941da177e4SLinus Torvalds /* 17951da177e4SLinus Torvalds * The 2.0 compatible umount. No flags. 17961da177e4SLinus Torvalds */ 1797bdc480e3SHeiko Carstens SYSCALL_DEFINE1(oldumount, char __user *, name) 17981da177e4SLinus Torvalds { 17993a18ef5cSDominik Brodowski return ksys_umount(name, 0); 18001da177e4SLinus Torvalds } 18011da177e4SLinus Torvalds 18021da177e4SLinus Torvalds #endif 18031da177e4SLinus Torvalds 18044ce5d2b1SEric W. Biederman static bool is_mnt_ns_file(struct dentry *dentry) 18058823c079SEric W. Biederman { 18064ce5d2b1SEric W. Biederman /* Is this a proxy for a mount namespace? */ 1807e149ed2bSAl Viro return dentry->d_op == &ns_dentry_operations && 1808e149ed2bSAl Viro dentry->d_fsdata == &mntns_operations; 18094ce5d2b1SEric W. Biederman } 18104ce5d2b1SEric W. Biederman 1811213921f9SEric Biggers static struct mnt_namespace *to_mnt_ns(struct ns_common *ns) 181258be2825SAl Viro { 181358be2825SAl Viro return container_of(ns, struct mnt_namespace, ns); 181458be2825SAl Viro } 181558be2825SAl Viro 1816303cc571SChristian Brauner struct ns_common *from_mnt_ns(struct mnt_namespace *mnt) 1817303cc571SChristian Brauner { 1818303cc571SChristian Brauner return &mnt->ns; 1819303cc571SChristian Brauner } 1820303cc571SChristian Brauner 18214ce5d2b1SEric W. Biederman static bool mnt_ns_loop(struct dentry *dentry) 18224ce5d2b1SEric W. Biederman { 18234ce5d2b1SEric W. Biederman /* Could bind mounting the mount namespace inode cause a 18244ce5d2b1SEric W. Biederman * mount namespace loop? 18254ce5d2b1SEric W. Biederman */ 18264ce5d2b1SEric W. Biederman struct mnt_namespace *mnt_ns; 18274ce5d2b1SEric W. Biederman if (!is_mnt_ns_file(dentry)) 18284ce5d2b1SEric W. Biederman return false; 18294ce5d2b1SEric W. Biederman 1830f77c8014SAl Viro mnt_ns = to_mnt_ns(get_proc_ns(dentry->d_inode)); 18318823c079SEric W. Biederman return current->nsproxy->mnt_ns->seq >= mnt_ns->seq; 18328823c079SEric W. Biederman } 18338823c079SEric W. Biederman 183487129cc0SAl Viro struct mount *copy_tree(struct mount *mnt, struct dentry *dentry, 183536341f64SRam Pai int flag) 18361da177e4SLinus Torvalds { 183784d17192SAl Viro struct mount *res, *p, *q, *r, *parent; 18381da177e4SLinus Torvalds 18394ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt)) 18404ce5d2b1SEric W. Biederman return ERR_PTR(-EINVAL); 18414ce5d2b1SEric W. Biederman 18424ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry)) 1843be34d1a3SDavid Howells return ERR_PTR(-EINVAL); 18449676f0c6SRam Pai 184536341f64SRam Pai res = q = clone_mnt(mnt, dentry, flag); 1846be34d1a3SDavid Howells if (IS_ERR(q)) 1847be34d1a3SDavid Howells return q; 1848be34d1a3SDavid Howells 1849a73324daSAl Viro q->mnt_mountpoint = mnt->mnt_mountpoint; 18501da177e4SLinus Torvalds 18511da177e4SLinus Torvalds p = mnt; 18526b41d536SAl Viro list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) { 1853315fc83eSAl Viro struct mount *s; 18547ec02ef1SJan Blunck if (!is_subdir(r->mnt_mountpoint, dentry)) 18551da177e4SLinus Torvalds continue; 18561da177e4SLinus Torvalds 1857909b0a88SAl Viro for (s = r; s; s = next_mnt(s, r)) { 18584ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_UNBINDABLE) && 18594ce5d2b1SEric W. Biederman IS_MNT_UNBINDABLE(s)) { 1860df7342b2SEric W. Biederman if (s->mnt.mnt_flags & MNT_LOCKED) { 1861df7342b2SEric W. Biederman /* Both unbindable and locked. */ 1862df7342b2SEric W. Biederman q = ERR_PTR(-EPERM); 1863df7342b2SEric W. Biederman goto out; 1864df7342b2SEric W. Biederman } else { 18654ce5d2b1SEric W. Biederman s = skip_mnt_tree(s); 18664ce5d2b1SEric W. Biederman continue; 18674ce5d2b1SEric W. Biederman } 1868df7342b2SEric W. Biederman } 18694ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_MNT_NS_FILE) && 18704ce5d2b1SEric W. Biederman is_mnt_ns_file(s->mnt.mnt_root)) { 18719676f0c6SRam Pai s = skip_mnt_tree(s); 18729676f0c6SRam Pai continue; 18739676f0c6SRam Pai } 18740714a533SAl Viro while (p != s->mnt_parent) { 18750714a533SAl Viro p = p->mnt_parent; 18760714a533SAl Viro q = q->mnt_parent; 18771da177e4SLinus Torvalds } 187887129cc0SAl Viro p = s; 187984d17192SAl Viro parent = q; 188087129cc0SAl Viro q = clone_mnt(p, p->mnt.mnt_root, flag); 1881be34d1a3SDavid Howells if (IS_ERR(q)) 1882be34d1a3SDavid Howells goto out; 1883719ea2fbSAl Viro lock_mount_hash(); 18841a4eeaf2SAl Viro list_add_tail(&q->mnt_list, &res->mnt_list); 18851064f874SEric W. Biederman attach_mnt(q, parent, p->mnt_mp); 1886719ea2fbSAl Viro unlock_mount_hash(); 18871da177e4SLinus Torvalds } 18881da177e4SLinus Torvalds } 18891da177e4SLinus Torvalds return res; 1890be34d1a3SDavid Howells out: 18911da177e4SLinus Torvalds if (res) { 1892719ea2fbSAl Viro lock_mount_hash(); 1893e819f152SEric W. Biederman umount_tree(res, UMOUNT_SYNC); 1894719ea2fbSAl Viro unlock_mount_hash(); 18951da177e4SLinus Torvalds } 1896be34d1a3SDavid Howells return q; 18971da177e4SLinus Torvalds } 18981da177e4SLinus Torvalds 1899be34d1a3SDavid Howells /* Caller should check returned pointer for errors */ 1900be34d1a3SDavid Howells 1901ca71cf71SAl Viro struct vfsmount *collect_mounts(const struct path *path) 19028aec0809SAl Viro { 1903cb338d06SAl Viro struct mount *tree; 190497216be0SAl Viro namespace_lock(); 1905cd4a4017SEric W. Biederman if (!check_mnt(real_mount(path->mnt))) 1906cd4a4017SEric W. Biederman tree = ERR_PTR(-EINVAL); 1907cd4a4017SEric W. Biederman else 190887129cc0SAl Viro tree = copy_tree(real_mount(path->mnt), path->dentry, 190987129cc0SAl Viro CL_COPY_ALL | CL_PRIVATE); 1910328e6d90SAl Viro namespace_unlock(); 1911be34d1a3SDavid Howells if (IS_ERR(tree)) 191252e220d3SDan Carpenter return ERR_CAST(tree); 1913be34d1a3SDavid Howells return &tree->mnt; 19148aec0809SAl Viro } 19158aec0809SAl Viro 1916a07b2000SAl Viro static void free_mnt_ns(struct mnt_namespace *); 1917a07b2000SAl Viro static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *, bool); 1918a07b2000SAl Viro 1919a07b2000SAl Viro void dissolve_on_fput(struct vfsmount *mnt) 1920a07b2000SAl Viro { 1921a07b2000SAl Viro struct mnt_namespace *ns; 1922a07b2000SAl Viro namespace_lock(); 1923a07b2000SAl Viro lock_mount_hash(); 1924a07b2000SAl Viro ns = real_mount(mnt)->mnt_ns; 192544dfd84aSDavid Howells if (ns) { 192644dfd84aSDavid Howells if (is_anon_ns(ns)) 1927a07b2000SAl Viro umount_tree(real_mount(mnt), UMOUNT_CONNECTED); 192844dfd84aSDavid Howells else 192944dfd84aSDavid Howells ns = NULL; 193044dfd84aSDavid Howells } 1931a07b2000SAl Viro unlock_mount_hash(); 1932a07b2000SAl Viro namespace_unlock(); 193344dfd84aSDavid Howells if (ns) 1934a07b2000SAl Viro free_mnt_ns(ns); 1935a07b2000SAl Viro } 1936a07b2000SAl Viro 19378aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt) 19388aec0809SAl Viro { 193997216be0SAl Viro namespace_lock(); 1940719ea2fbSAl Viro lock_mount_hash(); 19419c8e0a1bSEric W. Biederman umount_tree(real_mount(mnt), 0); 1942719ea2fbSAl Viro unlock_mount_hash(); 19433ab6abeeSAl Viro namespace_unlock(); 19448aec0809SAl Viro } 19458aec0809SAl Viro 1946427215d8SMiklos Szeredi static bool has_locked_children(struct mount *mnt, struct dentry *dentry) 1947427215d8SMiklos Szeredi { 1948427215d8SMiklos Szeredi struct mount *child; 1949427215d8SMiklos Szeredi 1950427215d8SMiklos Szeredi list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) { 1951427215d8SMiklos Szeredi if (!is_subdir(child->mnt_mountpoint, dentry)) 1952427215d8SMiklos Szeredi continue; 1953427215d8SMiklos Szeredi 1954427215d8SMiklos Szeredi if (child->mnt.mnt_flags & MNT_LOCKED) 1955427215d8SMiklos Szeredi return true; 1956427215d8SMiklos Szeredi } 1957427215d8SMiklos Szeredi return false; 1958427215d8SMiklos Szeredi } 1959427215d8SMiklos Szeredi 1960c771d683SMiklos Szeredi /** 1961c771d683SMiklos Szeredi * clone_private_mount - create a private clone of a path 19621f287bc4SRandy Dunlap * @path: path to clone 1963c771d683SMiklos Szeredi * 19641f287bc4SRandy Dunlap * This creates a new vfsmount, which will be the clone of @path. The new mount 19651f287bc4SRandy Dunlap * will not be attached anywhere in the namespace and will be private (i.e. 19661f287bc4SRandy Dunlap * changes to the originating mount won't be propagated into this). 1967c771d683SMiklos Szeredi * 1968c771d683SMiklos Szeredi * Release with mntput(). 1969c771d683SMiklos Szeredi */ 1970ca71cf71SAl Viro struct vfsmount *clone_private_mount(const struct path *path) 1971c771d683SMiklos Szeredi { 1972c771d683SMiklos Szeredi struct mount *old_mnt = real_mount(path->mnt); 1973c771d683SMiklos Szeredi struct mount *new_mnt; 1974c771d683SMiklos Szeredi 1975427215d8SMiklos Szeredi down_read(&namespace_sem); 1976c771d683SMiklos Szeredi if (IS_MNT_UNBINDABLE(old_mnt)) 1977427215d8SMiklos Szeredi goto invalid; 1978427215d8SMiklos Szeredi 1979427215d8SMiklos Szeredi if (!check_mnt(old_mnt)) 1980427215d8SMiklos Szeredi goto invalid; 1981427215d8SMiklos Szeredi 1982427215d8SMiklos Szeredi if (has_locked_children(old_mnt, path->dentry)) 1983427215d8SMiklos Szeredi goto invalid; 1984c771d683SMiklos Szeredi 1985c771d683SMiklos Szeredi new_mnt = clone_mnt(old_mnt, path->dentry, CL_PRIVATE); 1986427215d8SMiklos Szeredi up_read(&namespace_sem); 1987427215d8SMiklos Szeredi 1988c771d683SMiklos Szeredi if (IS_ERR(new_mnt)) 1989c771d683SMiklos Szeredi return ERR_CAST(new_mnt); 1990c771d683SMiklos Szeredi 1991df820f8dSMiklos Szeredi /* Longterm mount to be removed by kern_unmount*() */ 1992df820f8dSMiklos Szeredi new_mnt->mnt_ns = MNT_NS_INTERNAL; 1993df820f8dSMiklos Szeredi 1994c771d683SMiklos Szeredi return &new_mnt->mnt; 1995427215d8SMiklos Szeredi 1996427215d8SMiklos Szeredi invalid: 1997427215d8SMiklos Szeredi up_read(&namespace_sem); 1998427215d8SMiklos Szeredi return ERR_PTR(-EINVAL); 1999c771d683SMiklos Szeredi } 2000c771d683SMiklos Szeredi EXPORT_SYMBOL_GPL(clone_private_mount); 2001c771d683SMiklos Szeredi 20021f707137SAl Viro int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg, 20031f707137SAl Viro struct vfsmount *root) 20041f707137SAl Viro { 20051a4eeaf2SAl Viro struct mount *mnt; 20061f707137SAl Viro int res = f(root, arg); 20071f707137SAl Viro if (res) 20081f707137SAl Viro return res; 20091a4eeaf2SAl Viro list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) { 20101a4eeaf2SAl Viro res = f(&mnt->mnt, arg); 20111f707137SAl Viro if (res) 20121f707137SAl Viro return res; 20131f707137SAl Viro } 20141f707137SAl Viro return 0; 20151f707137SAl Viro } 20161f707137SAl Viro 20173bd045ccSAl Viro static void lock_mnt_tree(struct mount *mnt) 20183bd045ccSAl Viro { 20193bd045ccSAl Viro struct mount *p; 20203bd045ccSAl Viro 20213bd045ccSAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 20223bd045ccSAl Viro int flags = p->mnt.mnt_flags; 20233bd045ccSAl Viro /* Don't allow unprivileged users to change mount flags */ 20243bd045ccSAl Viro flags |= MNT_LOCK_ATIME; 20253bd045ccSAl Viro 20263bd045ccSAl Viro if (flags & MNT_READONLY) 20273bd045ccSAl Viro flags |= MNT_LOCK_READONLY; 20283bd045ccSAl Viro 20293bd045ccSAl Viro if (flags & MNT_NODEV) 20303bd045ccSAl Viro flags |= MNT_LOCK_NODEV; 20313bd045ccSAl Viro 20323bd045ccSAl Viro if (flags & MNT_NOSUID) 20333bd045ccSAl Viro flags |= MNT_LOCK_NOSUID; 20343bd045ccSAl Viro 20353bd045ccSAl Viro if (flags & MNT_NOEXEC) 20363bd045ccSAl Viro flags |= MNT_LOCK_NOEXEC; 20373bd045ccSAl Viro /* Don't allow unprivileged users to reveal what is under a mount */ 20383bd045ccSAl Viro if (list_empty(&p->mnt_expire)) 20393bd045ccSAl Viro flags |= MNT_LOCKED; 20403bd045ccSAl Viro p->mnt.mnt_flags = flags; 20413bd045ccSAl Viro } 20423bd045ccSAl Viro } 20433bd045ccSAl Viro 20444b8b21f4SAl Viro static void cleanup_group_ids(struct mount *mnt, struct mount *end) 2045719f5d7fSMiklos Szeredi { 2046315fc83eSAl Viro struct mount *p; 2047719f5d7fSMiklos Szeredi 2048909b0a88SAl Viro for (p = mnt; p != end; p = next_mnt(p, mnt)) { 2049fc7be130SAl Viro if (p->mnt_group_id && !IS_MNT_SHARED(p)) 20504b8b21f4SAl Viro mnt_release_group_id(p); 2051719f5d7fSMiklos Szeredi } 2052719f5d7fSMiklos Szeredi } 2053719f5d7fSMiklos Szeredi 20544b8b21f4SAl Viro static int invent_group_ids(struct mount *mnt, bool recurse) 2055719f5d7fSMiklos Szeredi { 2056315fc83eSAl Viro struct mount *p; 2057719f5d7fSMiklos Szeredi 2058909b0a88SAl Viro for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) { 2059fc7be130SAl Viro if (!p->mnt_group_id && !IS_MNT_SHARED(p)) { 20604b8b21f4SAl Viro int err = mnt_alloc_group_id(p); 2061719f5d7fSMiklos Szeredi if (err) { 20624b8b21f4SAl Viro cleanup_group_ids(mnt, p); 2063719f5d7fSMiklos Szeredi return err; 2064719f5d7fSMiklos Szeredi } 2065719f5d7fSMiklos Szeredi } 2066719f5d7fSMiklos Szeredi } 2067719f5d7fSMiklos Szeredi 2068719f5d7fSMiklos Szeredi return 0; 2069719f5d7fSMiklos Szeredi } 2070719f5d7fSMiklos Szeredi 2071d2921684SEric W. Biederman int count_mounts(struct mnt_namespace *ns, struct mount *mnt) 2072d2921684SEric W. Biederman { 2073d2921684SEric W. Biederman unsigned int max = READ_ONCE(sysctl_mount_max); 2074d2921684SEric W. Biederman unsigned int mounts = 0, old, pending, sum; 2075d2921684SEric W. Biederman struct mount *p; 2076d2921684SEric W. Biederman 2077d2921684SEric W. Biederman for (p = mnt; p; p = next_mnt(p, mnt)) 2078d2921684SEric W. Biederman mounts++; 2079d2921684SEric W. Biederman 2080d2921684SEric W. Biederman old = ns->mounts; 2081d2921684SEric W. Biederman pending = ns->pending_mounts; 2082d2921684SEric W. Biederman sum = old + pending; 2083d2921684SEric W. Biederman if ((old > sum) || 2084d2921684SEric W. Biederman (pending > sum) || 2085d2921684SEric W. Biederman (max < sum) || 2086d2921684SEric W. Biederman (mounts > (max - sum))) 2087d2921684SEric W. Biederman return -ENOSPC; 2088d2921684SEric W. Biederman 2089d2921684SEric W. Biederman ns->pending_mounts = pending + mounts; 2090d2921684SEric W. Biederman return 0; 2091d2921684SEric W. Biederman } 2092d2921684SEric W. Biederman 2093b90fa9aeSRam Pai /* 2094b90fa9aeSRam Pai * @source_mnt : mount tree to be attached 2095b90fa9aeSRam Pai * @nd : place the mount tree @source_mnt is attached 209621444403SRam Pai * @parent_nd : if non-null, detach the source_mnt from its parent and 209721444403SRam Pai * store the parent mount and mountpoint dentry. 209821444403SRam Pai * (done when source_mnt is moved) 2099b90fa9aeSRam Pai * 2100b90fa9aeSRam Pai * NOTE: in the table below explains the semantics when a source mount 2101b90fa9aeSRam Pai * of a given type is attached to a destination mount of a given type. 21029676f0c6SRam Pai * --------------------------------------------------------------------------- 2103b90fa9aeSRam Pai * | BIND MOUNT OPERATION | 21049676f0c6SRam Pai * |************************************************************************** 21059676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 21069676f0c6SRam Pai * | dest | | | | | 21079676f0c6SRam Pai * | | | | | | | 21089676f0c6SRam Pai * | v | | | | | 21099676f0c6SRam Pai * |************************************************************************** 21109676f0c6SRam Pai * | shared | shared (++) | shared (+) | shared(+++)| invalid | 21115afe0022SRam Pai * | | | | | | 21129676f0c6SRam Pai * |non-shared| shared (+) | private | slave (*) | invalid | 21139676f0c6SRam Pai * *************************************************************************** 2114b90fa9aeSRam Pai * A bind operation clones the source mount and mounts the clone on the 2115b90fa9aeSRam Pai * destination mount. 2116b90fa9aeSRam Pai * 2117b90fa9aeSRam Pai * (++) the cloned mount is propagated to all the mounts in the propagation 2118b90fa9aeSRam Pai * tree of the destination mount and the cloned mount is added to 2119b90fa9aeSRam Pai * the peer group of the source mount. 2120b90fa9aeSRam Pai * (+) the cloned mount is created under the destination mount and is marked 2121b90fa9aeSRam Pai * as shared. The cloned mount is added to the peer group of the source 2122b90fa9aeSRam Pai * mount. 21235afe0022SRam Pai * (+++) the mount is propagated to all the mounts in the propagation tree 21245afe0022SRam Pai * of the destination mount and the cloned mount is made slave 21255afe0022SRam Pai * of the same master as that of the source mount. The cloned mount 21265afe0022SRam Pai * is marked as 'shared and slave'. 21275afe0022SRam Pai * (*) the cloned mount is made a slave of the same master as that of the 21285afe0022SRam Pai * source mount. 21295afe0022SRam Pai * 21309676f0c6SRam Pai * --------------------------------------------------------------------------- 213121444403SRam Pai * | MOVE MOUNT OPERATION | 21329676f0c6SRam Pai * |************************************************************************** 21339676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 21349676f0c6SRam Pai * | dest | | | | | 21359676f0c6SRam Pai * | | | | | | | 21369676f0c6SRam Pai * | v | | | | | 21379676f0c6SRam Pai * |************************************************************************** 21389676f0c6SRam Pai * | shared | shared (+) | shared (+) | shared(+++) | invalid | 21395afe0022SRam Pai * | | | | | | 21409676f0c6SRam Pai * |non-shared| shared (+*) | private | slave (*) | unbindable | 21419676f0c6SRam Pai * *************************************************************************** 21425afe0022SRam Pai * 21435afe0022SRam Pai * (+) the mount is moved to the destination. And is then propagated to 21445afe0022SRam Pai * all the mounts in the propagation tree of the destination mount. 214521444403SRam Pai * (+*) the mount is moved to the destination. 21465afe0022SRam Pai * (+++) the mount is moved to the destination and is then propagated to 21475afe0022SRam Pai * all the mounts belonging to the destination mount's propagation tree. 21485afe0022SRam Pai * the mount is marked as 'shared and slave'. 21495afe0022SRam Pai * (*) the mount continues to be a slave at the new location. 2150b90fa9aeSRam Pai * 2151b90fa9aeSRam Pai * if the source mount is a tree, the operations explained above is 2152b90fa9aeSRam Pai * applied to each mount in the tree. 2153b90fa9aeSRam Pai * Must be called without spinlocks held, since this function can sleep 2154b90fa9aeSRam Pai * in allocations. 2155b90fa9aeSRam Pai */ 21560fb54e50SAl Viro static int attach_recursive_mnt(struct mount *source_mnt, 215784d17192SAl Viro struct mount *dest_mnt, 215884d17192SAl Viro struct mountpoint *dest_mp, 21592763d119SAl Viro bool moving) 2160b90fa9aeSRam Pai { 21613bd045ccSAl Viro struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns; 216238129a13SAl Viro HLIST_HEAD(tree_list); 2163d2921684SEric W. Biederman struct mnt_namespace *ns = dest_mnt->mnt_ns; 21641064f874SEric W. Biederman struct mountpoint *smp; 2165315fc83eSAl Viro struct mount *child, *p; 216638129a13SAl Viro struct hlist_node *n; 2167719f5d7fSMiklos Szeredi int err; 2168b90fa9aeSRam Pai 21691064f874SEric W. Biederman /* Preallocate a mountpoint in case the new mounts need 21701064f874SEric W. Biederman * to be tucked under other mounts. 21711064f874SEric W. Biederman */ 21721064f874SEric W. Biederman smp = get_mountpoint(source_mnt->mnt.mnt_root); 21731064f874SEric W. Biederman if (IS_ERR(smp)) 21741064f874SEric W. Biederman return PTR_ERR(smp); 21751064f874SEric W. Biederman 2176d2921684SEric W. Biederman /* Is there space to add these mounts to the mount namespace? */ 21772763d119SAl Viro if (!moving) { 2178d2921684SEric W. Biederman err = count_mounts(ns, source_mnt); 2179d2921684SEric W. Biederman if (err) 2180d2921684SEric W. Biederman goto out; 2181d2921684SEric W. Biederman } 2182d2921684SEric W. Biederman 2183fc7be130SAl Viro if (IS_MNT_SHARED(dest_mnt)) { 21840fb54e50SAl Viro err = invent_group_ids(source_mnt, true); 2185719f5d7fSMiklos Szeredi if (err) 2186719f5d7fSMiklos Szeredi goto out; 218784d17192SAl Viro err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list); 2188f2ebb3a9SAl Viro lock_mount_hash(); 2189719f5d7fSMiklos Szeredi if (err) 2190719f5d7fSMiklos Szeredi goto out_cleanup_ids; 2191909b0a88SAl Viro for (p = source_mnt; p; p = next_mnt(p, source_mnt)) 21920f0afb1dSAl Viro set_mnt_shared(p); 21930b1b901bSAl Viro } else { 21940b1b901bSAl Viro lock_mount_hash(); 2195b90fa9aeSRam Pai } 21962763d119SAl Viro if (moving) { 21972763d119SAl Viro unhash_mnt(source_mnt); 219884d17192SAl Viro attach_mnt(source_mnt, dest_mnt, dest_mp); 2199143c8c91SAl Viro touch_mnt_namespace(source_mnt->mnt_ns); 220021444403SRam Pai } else { 220144dfd84aSDavid Howells if (source_mnt->mnt_ns) { 220244dfd84aSDavid Howells /* move from anon - the caller will destroy */ 220344dfd84aSDavid Howells list_del_init(&source_mnt->mnt_ns->list); 220444dfd84aSDavid Howells } 220584d17192SAl Viro mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt); 22061064f874SEric W. Biederman commit_tree(source_mnt); 220721444403SRam Pai } 2208b90fa9aeSRam Pai 220938129a13SAl Viro hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) { 22101d6a32acSAl Viro struct mount *q; 221138129a13SAl Viro hlist_del_init(&child->mnt_hash); 22121064f874SEric W. Biederman q = __lookup_mnt(&child->mnt_parent->mnt, 22131d6a32acSAl Viro child->mnt_mountpoint); 22141064f874SEric W. Biederman if (q) 22151064f874SEric W. Biederman mnt_change_mountpoint(child, smp, q); 22163bd045ccSAl Viro /* Notice when we are propagating across user namespaces */ 22173bd045ccSAl Viro if (child->mnt_parent->mnt_ns->user_ns != user_ns) 22183bd045ccSAl Viro lock_mnt_tree(child); 2219d728cf79SChristian Brauner child->mnt.mnt_flags &= ~MNT_LOCKED; 22201064f874SEric W. Biederman commit_tree(child); 2221b90fa9aeSRam Pai } 22221064f874SEric W. Biederman put_mountpoint(smp); 2223719ea2fbSAl Viro unlock_mount_hash(); 222499b7db7bSNick Piggin 2225b90fa9aeSRam Pai return 0; 2226719f5d7fSMiklos Szeredi 2227719f5d7fSMiklos Szeredi out_cleanup_ids: 2228f2ebb3a9SAl Viro while (!hlist_empty(&tree_list)) { 2229f2ebb3a9SAl Viro child = hlist_entry(tree_list.first, struct mount, mnt_hash); 2230d2921684SEric W. Biederman child->mnt_parent->mnt_ns->pending_mounts = 0; 2231e819f152SEric W. Biederman umount_tree(child, UMOUNT_SYNC); 2232f2ebb3a9SAl Viro } 2233f2ebb3a9SAl Viro unlock_mount_hash(); 22340fb54e50SAl Viro cleanup_group_ids(source_mnt, NULL); 2235719f5d7fSMiklos Szeredi out: 2236d2921684SEric W. Biederman ns->pending_mounts = 0; 22371064f874SEric W. Biederman 22381064f874SEric W. Biederman read_seqlock_excl(&mount_lock); 22391064f874SEric W. Biederman put_mountpoint(smp); 22401064f874SEric W. Biederman read_sequnlock_excl(&mount_lock); 22411064f874SEric W. Biederman 2242719f5d7fSMiklos Szeredi return err; 2243b90fa9aeSRam Pai } 2244b90fa9aeSRam Pai 224584d17192SAl Viro static struct mountpoint *lock_mount(struct path *path) 2246b12cea91SAl Viro { 2247b12cea91SAl Viro struct vfsmount *mnt; 224884d17192SAl Viro struct dentry *dentry = path->dentry; 2249b12cea91SAl Viro retry: 22505955102cSAl Viro inode_lock(dentry->d_inode); 225184d17192SAl Viro if (unlikely(cant_mount(dentry))) { 22525955102cSAl Viro inode_unlock(dentry->d_inode); 225384d17192SAl Viro return ERR_PTR(-ENOENT); 2254b12cea91SAl Viro } 225597216be0SAl Viro namespace_lock(); 2256b12cea91SAl Viro mnt = lookup_mnt(path); 225784d17192SAl Viro if (likely(!mnt)) { 22583895dbf8SEric W. Biederman struct mountpoint *mp = get_mountpoint(dentry); 225984d17192SAl Viro if (IS_ERR(mp)) { 226097216be0SAl Viro namespace_unlock(); 22615955102cSAl Viro inode_unlock(dentry->d_inode); 226284d17192SAl Viro return mp; 226384d17192SAl Viro } 226484d17192SAl Viro return mp; 226584d17192SAl Viro } 226697216be0SAl Viro namespace_unlock(); 22675955102cSAl Viro inode_unlock(path->dentry->d_inode); 2268b12cea91SAl Viro path_put(path); 2269b12cea91SAl Viro path->mnt = mnt; 227084d17192SAl Viro dentry = path->dentry = dget(mnt->mnt_root); 2271b12cea91SAl Viro goto retry; 2272b12cea91SAl Viro } 2273b12cea91SAl Viro 227484d17192SAl Viro static void unlock_mount(struct mountpoint *where) 2275b12cea91SAl Viro { 227684d17192SAl Viro struct dentry *dentry = where->m_dentry; 22773895dbf8SEric W. Biederman 22783895dbf8SEric W. Biederman read_seqlock_excl(&mount_lock); 227984d17192SAl Viro put_mountpoint(where); 22803895dbf8SEric W. Biederman read_sequnlock_excl(&mount_lock); 22813895dbf8SEric W. Biederman 2282328e6d90SAl Viro namespace_unlock(); 22835955102cSAl Viro inode_unlock(dentry->d_inode); 2284b12cea91SAl Viro } 2285b12cea91SAl Viro 228684d17192SAl Viro static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp) 22871da177e4SLinus Torvalds { 2288e462ec50SDavid Howells if (mnt->mnt.mnt_sb->s_flags & SB_NOUSER) 22891da177e4SLinus Torvalds return -EINVAL; 22901da177e4SLinus Torvalds 2291e36cb0b8SDavid Howells if (d_is_dir(mp->m_dentry) != 2292e36cb0b8SDavid Howells d_is_dir(mnt->mnt.mnt_root)) 22931da177e4SLinus Torvalds return -ENOTDIR; 22941da177e4SLinus Torvalds 22952763d119SAl Viro return attach_recursive_mnt(mnt, p, mp, false); 22961da177e4SLinus Torvalds } 22971da177e4SLinus Torvalds 22981da177e4SLinus Torvalds /* 22997a2e8a8fSValerie Aurora * Sanity check the flags to change_mnt_propagation. 23007a2e8a8fSValerie Aurora */ 23017a2e8a8fSValerie Aurora 2302e462ec50SDavid Howells static int flags_to_propagation_type(int ms_flags) 23037a2e8a8fSValerie Aurora { 2304e462ec50SDavid Howells int type = ms_flags & ~(MS_REC | MS_SILENT); 23057a2e8a8fSValerie Aurora 23067a2e8a8fSValerie Aurora /* Fail if any non-propagation flags are set */ 23077a2e8a8fSValerie Aurora if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 23087a2e8a8fSValerie Aurora return 0; 23097a2e8a8fSValerie Aurora /* Only one propagation flag should be set */ 23107a2e8a8fSValerie Aurora if (!is_power_of_2(type)) 23117a2e8a8fSValerie Aurora return 0; 23127a2e8a8fSValerie Aurora return type; 23137a2e8a8fSValerie Aurora } 23147a2e8a8fSValerie Aurora 23157a2e8a8fSValerie Aurora /* 231607b20889SRam Pai * recursively change the type of the mountpoint. 231707b20889SRam Pai */ 2318e462ec50SDavid Howells static int do_change_type(struct path *path, int ms_flags) 231907b20889SRam Pai { 2320315fc83eSAl Viro struct mount *m; 23214b8b21f4SAl Viro struct mount *mnt = real_mount(path->mnt); 2322e462ec50SDavid Howells int recurse = ms_flags & MS_REC; 23237a2e8a8fSValerie Aurora int type; 2324719f5d7fSMiklos Szeredi int err = 0; 232507b20889SRam Pai 23262d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 232707b20889SRam Pai return -EINVAL; 232807b20889SRam Pai 2329e462ec50SDavid Howells type = flags_to_propagation_type(ms_flags); 23307a2e8a8fSValerie Aurora if (!type) 23317a2e8a8fSValerie Aurora return -EINVAL; 23327a2e8a8fSValerie Aurora 233397216be0SAl Viro namespace_lock(); 2334719f5d7fSMiklos Szeredi if (type == MS_SHARED) { 2335719f5d7fSMiklos Szeredi err = invent_group_ids(mnt, recurse); 2336719f5d7fSMiklos Szeredi if (err) 2337719f5d7fSMiklos Szeredi goto out_unlock; 2338719f5d7fSMiklos Szeredi } 2339719f5d7fSMiklos Szeredi 2340719ea2fbSAl Viro lock_mount_hash(); 2341909b0a88SAl Viro for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL)) 23420f0afb1dSAl Viro change_mnt_propagation(m, type); 2343719ea2fbSAl Viro unlock_mount_hash(); 2344719f5d7fSMiklos Szeredi 2345719f5d7fSMiklos Szeredi out_unlock: 234697216be0SAl Viro namespace_unlock(); 2347719f5d7fSMiklos Szeredi return err; 234807b20889SRam Pai } 234907b20889SRam Pai 2350a07b2000SAl Viro static struct mount *__do_loopback(struct path *old_path, int recurse) 2351a07b2000SAl Viro { 2352a07b2000SAl Viro struct mount *mnt = ERR_PTR(-EINVAL), *old = real_mount(old_path->mnt); 2353a07b2000SAl Viro 2354a07b2000SAl Viro if (IS_MNT_UNBINDABLE(old)) 2355a07b2000SAl Viro return mnt; 2356a07b2000SAl Viro 2357a07b2000SAl Viro if (!check_mnt(old) && old_path->dentry->d_op != &ns_dentry_operations) 2358a07b2000SAl Viro return mnt; 2359a07b2000SAl Viro 2360a07b2000SAl Viro if (!recurse && has_locked_children(old, old_path->dentry)) 2361a07b2000SAl Viro return mnt; 2362a07b2000SAl Viro 2363a07b2000SAl Viro if (recurse) 2364a07b2000SAl Viro mnt = copy_tree(old, old_path->dentry, CL_COPY_MNT_NS_FILE); 2365a07b2000SAl Viro else 2366a07b2000SAl Viro mnt = clone_mnt(old, old_path->dentry, 0); 2367a07b2000SAl Viro 2368a07b2000SAl Viro if (!IS_ERR(mnt)) 2369a07b2000SAl Viro mnt->mnt.mnt_flags &= ~MNT_LOCKED; 2370a07b2000SAl Viro 2371a07b2000SAl Viro return mnt; 2372a07b2000SAl Viro } 2373a07b2000SAl Viro 237407b20889SRam Pai /* 23751da177e4SLinus Torvalds * do loopback mount. 23761da177e4SLinus Torvalds */ 2377808d4e3cSAl Viro static int do_loopback(struct path *path, const char *old_name, 23782dafe1c4SEric Sandeen int recurse) 23791da177e4SLinus Torvalds { 23802d92ab3cSAl Viro struct path old_path; 2381a07b2000SAl Viro struct mount *mnt = NULL, *parent; 238284d17192SAl Viro struct mountpoint *mp; 238357eccb83SAl Viro int err; 23841da177e4SLinus Torvalds if (!old_name || !*old_name) 23851da177e4SLinus Torvalds return -EINVAL; 2386815d405cSTrond Myklebust err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path); 23871da177e4SLinus Torvalds if (err) 23881da177e4SLinus Torvalds return err; 23891da177e4SLinus Torvalds 23908823c079SEric W. Biederman err = -EINVAL; 23914ce5d2b1SEric W. Biederman if (mnt_ns_loop(old_path.dentry)) 23928823c079SEric W. Biederman goto out; 23938823c079SEric W. Biederman 239484d17192SAl Viro mp = lock_mount(path); 2395a07b2000SAl Viro if (IS_ERR(mp)) { 239684d17192SAl Viro err = PTR_ERR(mp); 23979676f0c6SRam Pai goto out; 2398a07b2000SAl Viro } 23999676f0c6SRam Pai 240084d17192SAl Viro parent = real_mount(path->mnt); 2401e149ed2bSAl Viro if (!check_mnt(parent)) 2402e149ed2bSAl Viro goto out2; 2403e149ed2bSAl Viro 2404a07b2000SAl Viro mnt = __do_loopback(&old_path, recurse); 2405be34d1a3SDavid Howells if (IS_ERR(mnt)) { 2406be34d1a3SDavid Howells err = PTR_ERR(mnt); 2407e9c5d8a5SAndrey Vagin goto out2; 2408be34d1a3SDavid Howells } 2409ccd48bc7SAl Viro 241084d17192SAl Viro err = graft_tree(mnt, parent, mp); 24111da177e4SLinus Torvalds if (err) { 2412719ea2fbSAl Viro lock_mount_hash(); 2413e819f152SEric W. Biederman umount_tree(mnt, UMOUNT_SYNC); 2414719ea2fbSAl Viro unlock_mount_hash(); 24155b83d2c5SRam Pai } 2416b12cea91SAl Viro out2: 241784d17192SAl Viro unlock_mount(mp); 2418ccd48bc7SAl Viro out: 24192d92ab3cSAl Viro path_put(&old_path); 24201da177e4SLinus Torvalds return err; 24211da177e4SLinus Torvalds } 24221da177e4SLinus Torvalds 2423a07b2000SAl Viro static struct file *open_detached_copy(struct path *path, bool recursive) 2424a07b2000SAl Viro { 2425a07b2000SAl Viro struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns; 2426a07b2000SAl Viro struct mnt_namespace *ns = alloc_mnt_ns(user_ns, true); 2427a07b2000SAl Viro struct mount *mnt, *p; 2428a07b2000SAl Viro struct file *file; 2429a07b2000SAl Viro 2430a07b2000SAl Viro if (IS_ERR(ns)) 2431a07b2000SAl Viro return ERR_CAST(ns); 2432a07b2000SAl Viro 2433a07b2000SAl Viro namespace_lock(); 2434a07b2000SAl Viro mnt = __do_loopback(path, recursive); 2435a07b2000SAl Viro if (IS_ERR(mnt)) { 2436a07b2000SAl Viro namespace_unlock(); 2437a07b2000SAl Viro free_mnt_ns(ns); 2438a07b2000SAl Viro return ERR_CAST(mnt); 2439a07b2000SAl Viro } 2440a07b2000SAl Viro 2441a07b2000SAl Viro lock_mount_hash(); 2442a07b2000SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 2443a07b2000SAl Viro p->mnt_ns = ns; 2444a07b2000SAl Viro ns->mounts++; 2445a07b2000SAl Viro } 2446a07b2000SAl Viro ns->root = mnt; 2447a07b2000SAl Viro list_add_tail(&ns->list, &mnt->mnt_list); 2448a07b2000SAl Viro mntget(&mnt->mnt); 2449a07b2000SAl Viro unlock_mount_hash(); 2450a07b2000SAl Viro namespace_unlock(); 2451a07b2000SAl Viro 2452a07b2000SAl Viro mntput(path->mnt); 2453a07b2000SAl Viro path->mnt = &mnt->mnt; 2454a07b2000SAl Viro file = dentry_open(path, O_PATH, current_cred()); 2455a07b2000SAl Viro if (IS_ERR(file)) 2456a07b2000SAl Viro dissolve_on_fput(path->mnt); 2457a07b2000SAl Viro else 2458a07b2000SAl Viro file->f_mode |= FMODE_NEED_UNMOUNT; 2459a07b2000SAl Viro return file; 2460a07b2000SAl Viro } 2461a07b2000SAl Viro 24622658ce09SBen Dooks SYSCALL_DEFINE3(open_tree, int, dfd, const char __user *, filename, unsigned, flags) 2463a07b2000SAl Viro { 2464a07b2000SAl Viro struct file *file; 2465a07b2000SAl Viro struct path path; 2466a07b2000SAl Viro int lookup_flags = LOOKUP_AUTOMOUNT | LOOKUP_FOLLOW; 2467a07b2000SAl Viro bool detached = flags & OPEN_TREE_CLONE; 2468a07b2000SAl Viro int error; 2469a07b2000SAl Viro int fd; 2470a07b2000SAl Viro 2471a07b2000SAl Viro BUILD_BUG_ON(OPEN_TREE_CLOEXEC != O_CLOEXEC); 2472a07b2000SAl Viro 2473a07b2000SAl Viro if (flags & ~(AT_EMPTY_PATH | AT_NO_AUTOMOUNT | AT_RECURSIVE | 2474a07b2000SAl Viro AT_SYMLINK_NOFOLLOW | OPEN_TREE_CLONE | 2475a07b2000SAl Viro OPEN_TREE_CLOEXEC)) 2476a07b2000SAl Viro return -EINVAL; 2477a07b2000SAl Viro 2478a07b2000SAl Viro if ((flags & (AT_RECURSIVE | OPEN_TREE_CLONE)) == AT_RECURSIVE) 2479a07b2000SAl Viro return -EINVAL; 2480a07b2000SAl Viro 2481a07b2000SAl Viro if (flags & AT_NO_AUTOMOUNT) 2482a07b2000SAl Viro lookup_flags &= ~LOOKUP_AUTOMOUNT; 2483a07b2000SAl Viro if (flags & AT_SYMLINK_NOFOLLOW) 2484a07b2000SAl Viro lookup_flags &= ~LOOKUP_FOLLOW; 2485a07b2000SAl Viro if (flags & AT_EMPTY_PATH) 2486a07b2000SAl Viro lookup_flags |= LOOKUP_EMPTY; 2487a07b2000SAl Viro 2488a07b2000SAl Viro if (detached && !may_mount()) 2489a07b2000SAl Viro return -EPERM; 2490a07b2000SAl Viro 2491a07b2000SAl Viro fd = get_unused_fd_flags(flags & O_CLOEXEC); 2492a07b2000SAl Viro if (fd < 0) 2493a07b2000SAl Viro return fd; 2494a07b2000SAl Viro 2495a07b2000SAl Viro error = user_path_at(dfd, filename, lookup_flags, &path); 2496a07b2000SAl Viro if (unlikely(error)) { 2497a07b2000SAl Viro file = ERR_PTR(error); 2498a07b2000SAl Viro } else { 2499a07b2000SAl Viro if (detached) 2500a07b2000SAl Viro file = open_detached_copy(&path, flags & AT_RECURSIVE); 2501a07b2000SAl Viro else 2502a07b2000SAl Viro file = dentry_open(&path, O_PATH, current_cred()); 2503a07b2000SAl Viro path_put(&path); 2504a07b2000SAl Viro } 2505a07b2000SAl Viro if (IS_ERR(file)) { 2506a07b2000SAl Viro put_unused_fd(fd); 2507a07b2000SAl Viro return PTR_ERR(file); 2508a07b2000SAl Viro } 2509a07b2000SAl Viro fd_install(fd, file); 2510a07b2000SAl Viro return fd; 2511a07b2000SAl Viro } 2512a07b2000SAl Viro 251343f5e655SDavid Howells /* 251443f5e655SDavid Howells * Don't allow locked mount flags to be cleared. 251543f5e655SDavid Howells * 251643f5e655SDavid Howells * No locks need to be held here while testing the various MNT_LOCK 251743f5e655SDavid Howells * flags because those flags can never be cleared once they are set. 251843f5e655SDavid Howells */ 251943f5e655SDavid Howells static bool can_change_locked_flags(struct mount *mnt, unsigned int mnt_flags) 25202e4b7fcdSDave Hansen { 252143f5e655SDavid Howells unsigned int fl = mnt->mnt.mnt_flags; 25222e4b7fcdSDave Hansen 252343f5e655SDavid Howells if ((fl & MNT_LOCK_READONLY) && 252443f5e655SDavid Howells !(mnt_flags & MNT_READONLY)) 252543f5e655SDavid Howells return false; 252643f5e655SDavid Howells 252743f5e655SDavid Howells if ((fl & MNT_LOCK_NODEV) && 252843f5e655SDavid Howells !(mnt_flags & MNT_NODEV)) 252943f5e655SDavid Howells return false; 253043f5e655SDavid Howells 253143f5e655SDavid Howells if ((fl & MNT_LOCK_NOSUID) && 253243f5e655SDavid Howells !(mnt_flags & MNT_NOSUID)) 253343f5e655SDavid Howells return false; 253443f5e655SDavid Howells 253543f5e655SDavid Howells if ((fl & MNT_LOCK_NOEXEC) && 253643f5e655SDavid Howells !(mnt_flags & MNT_NOEXEC)) 253743f5e655SDavid Howells return false; 253843f5e655SDavid Howells 253943f5e655SDavid Howells if ((fl & MNT_LOCK_ATIME) && 254043f5e655SDavid Howells ((fl & MNT_ATIME_MASK) != (mnt_flags & MNT_ATIME_MASK))) 254143f5e655SDavid Howells return false; 254243f5e655SDavid Howells 254343f5e655SDavid Howells return true; 254443f5e655SDavid Howells } 254543f5e655SDavid Howells 254643f5e655SDavid Howells static int change_mount_ro_state(struct mount *mnt, unsigned int mnt_flags) 254743f5e655SDavid Howells { 254843f5e655SDavid Howells bool readonly_request = (mnt_flags & MNT_READONLY); 254943f5e655SDavid Howells 255043f5e655SDavid Howells if (readonly_request == __mnt_is_readonly(&mnt->mnt)) 25512e4b7fcdSDave Hansen return 0; 25522e4b7fcdSDave Hansen 25532e4b7fcdSDave Hansen if (readonly_request) 255443f5e655SDavid Howells return mnt_make_readonly(mnt); 255543f5e655SDavid Howells 255668847c94SChristian Brauner mnt->mnt.mnt_flags &= ~MNT_READONLY; 255768847c94SChristian Brauner return 0; 255843f5e655SDavid Howells } 255943f5e655SDavid Howells 256043f5e655SDavid Howells static void set_mount_attributes(struct mount *mnt, unsigned int mnt_flags) 256143f5e655SDavid Howells { 256243f5e655SDavid Howells mnt_flags |= mnt->mnt.mnt_flags & ~MNT_USER_SETTABLE_MASK; 256343f5e655SDavid Howells mnt->mnt.mnt_flags = mnt_flags; 256443f5e655SDavid Howells touch_mnt_namespace(mnt->mnt_ns); 256543f5e655SDavid Howells } 256643f5e655SDavid Howells 2567f8b92ba6SDeepa Dinamani static void mnt_warn_timestamp_expiry(struct path *mountpoint, struct vfsmount *mnt) 2568f8b92ba6SDeepa Dinamani { 2569f8b92ba6SDeepa Dinamani struct super_block *sb = mnt->mnt_sb; 2570f8b92ba6SDeepa Dinamani 2571f8b92ba6SDeepa Dinamani if (!__mnt_is_readonly(mnt) && 2572f8b92ba6SDeepa Dinamani (ktime_get_real_seconds() + TIME_UPTIME_SEC_MAX > sb->s_time_max)) { 2573f8b92ba6SDeepa Dinamani char *buf = (char *)__get_free_page(GFP_KERNEL); 2574f8b92ba6SDeepa Dinamani char *mntpath = buf ? d_path(mountpoint, buf, PAGE_SIZE) : ERR_PTR(-ENOMEM); 2575f8b92ba6SDeepa Dinamani struct tm tm; 2576f8b92ba6SDeepa Dinamani 2577f8b92ba6SDeepa Dinamani time64_to_tm(sb->s_time_max, 0, &tm); 2578f8b92ba6SDeepa Dinamani 25790ecee669SEric Biggers pr_warn("%s filesystem being %s at %s supports timestamps until %04ld (0x%llx)\n", 25800ecee669SEric Biggers sb->s_type->name, 25810ecee669SEric Biggers is_mounted(mnt) ? "remounted" : "mounted", 25820ecee669SEric Biggers mntpath, 2583f8b92ba6SDeepa Dinamani tm.tm_year+1900, (unsigned long long)sb->s_time_max); 2584f8b92ba6SDeepa Dinamani 2585f8b92ba6SDeepa Dinamani free_page((unsigned long)buf); 2586f8b92ba6SDeepa Dinamani } 2587f8b92ba6SDeepa Dinamani } 2588f8b92ba6SDeepa Dinamani 258943f5e655SDavid Howells /* 259043f5e655SDavid Howells * Handle reconfiguration of the mountpoint only without alteration of the 259143f5e655SDavid Howells * superblock it refers to. This is triggered by specifying MS_REMOUNT|MS_BIND 259243f5e655SDavid Howells * to mount(2). 259343f5e655SDavid Howells */ 259443f5e655SDavid Howells static int do_reconfigure_mnt(struct path *path, unsigned int mnt_flags) 259543f5e655SDavid Howells { 259643f5e655SDavid Howells struct super_block *sb = path->mnt->mnt_sb; 259743f5e655SDavid Howells struct mount *mnt = real_mount(path->mnt); 259843f5e655SDavid Howells int ret; 259943f5e655SDavid Howells 260043f5e655SDavid Howells if (!check_mnt(mnt)) 260143f5e655SDavid Howells return -EINVAL; 260243f5e655SDavid Howells 260343f5e655SDavid Howells if (path->dentry != mnt->mnt.mnt_root) 260443f5e655SDavid Howells return -EINVAL; 260543f5e655SDavid Howells 260643f5e655SDavid Howells if (!can_change_locked_flags(mnt, mnt_flags)) 260743f5e655SDavid Howells return -EPERM; 260843f5e655SDavid Howells 2609e58ace1aSChristian Brauner /* 2610e58ace1aSChristian Brauner * We're only checking whether the superblock is read-only not 2611e58ace1aSChristian Brauner * changing it, so only take down_read(&sb->s_umount). 2612e58ace1aSChristian Brauner */ 2613e58ace1aSChristian Brauner down_read(&sb->s_umount); 261468847c94SChristian Brauner lock_mount_hash(); 261543f5e655SDavid Howells ret = change_mount_ro_state(mnt, mnt_flags); 261643f5e655SDavid Howells if (ret == 0) 261743f5e655SDavid Howells set_mount_attributes(mnt, mnt_flags); 261868847c94SChristian Brauner unlock_mount_hash(); 2619e58ace1aSChristian Brauner up_read(&sb->s_umount); 2620f8b92ba6SDeepa Dinamani 2621f8b92ba6SDeepa Dinamani mnt_warn_timestamp_expiry(path, &mnt->mnt); 2622f8b92ba6SDeepa Dinamani 262343f5e655SDavid Howells return ret; 26242e4b7fcdSDave Hansen } 26252e4b7fcdSDave Hansen 26261da177e4SLinus Torvalds /* 26271da177e4SLinus Torvalds * change filesystem flags. dir should be a physical root of filesystem. 26281da177e4SLinus Torvalds * If you've mounted a non-root directory somewhere and want to do remount 26291da177e4SLinus Torvalds * on it - tough luck. 26301da177e4SLinus Torvalds */ 2631e462ec50SDavid Howells static int do_remount(struct path *path, int ms_flags, int sb_flags, 2632e462ec50SDavid Howells int mnt_flags, void *data) 26331da177e4SLinus Torvalds { 26341da177e4SLinus Torvalds int err; 26352d92ab3cSAl Viro struct super_block *sb = path->mnt->mnt_sb; 2636143c8c91SAl Viro struct mount *mnt = real_mount(path->mnt); 26378d0347f6SDavid Howells struct fs_context *fc; 26381da177e4SLinus Torvalds 2639143c8c91SAl Viro if (!check_mnt(mnt)) 26401da177e4SLinus Torvalds return -EINVAL; 26411da177e4SLinus Torvalds 26422d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 26431da177e4SLinus Torvalds return -EINVAL; 26441da177e4SLinus Torvalds 264543f5e655SDavid Howells if (!can_change_locked_flags(mnt, mnt_flags)) 264607b64558SEric W. Biederman return -EPERM; 26479566d674SEric W. Biederman 26488d0347f6SDavid Howells fc = fs_context_for_reconfigure(path->dentry, sb_flags, MS_RMT_MASK); 26498d0347f6SDavid Howells if (IS_ERR(fc)) 26508d0347f6SDavid Howells return PTR_ERR(fc); 2651ff36fe2cSEric Paris 2652b330966fSMiklos Szeredi fc->oldapi = true; 26538d0347f6SDavid Howells err = parse_monolithic_mount_data(fc, data); 26548d0347f6SDavid Howells if (!err) { 26551da177e4SLinus Torvalds down_write(&sb->s_umount); 265657eccb83SAl Viro err = -EPERM; 265743f5e655SDavid Howells if (ns_capable(sb->s_user_ns, CAP_SYS_ADMIN)) { 26588d0347f6SDavid Howells err = reconfigure_super(fc); 265968847c94SChristian Brauner if (!err) { 266068847c94SChristian Brauner lock_mount_hash(); 266143f5e655SDavid Howells set_mount_attributes(mnt, mnt_flags); 266268847c94SChristian Brauner unlock_mount_hash(); 266368847c94SChristian Brauner } 26640e55a7ccSDan Williams } 26656339dab8SAl Viro up_write(&sb->s_umount); 26668d0347f6SDavid Howells } 2667f8b92ba6SDeepa Dinamani 2668f8b92ba6SDeepa Dinamani mnt_warn_timestamp_expiry(path, &mnt->mnt); 2669f8b92ba6SDeepa Dinamani 26708d0347f6SDavid Howells put_fs_context(fc); 26711da177e4SLinus Torvalds return err; 26721da177e4SLinus Torvalds } 26731da177e4SLinus Torvalds 2674cbbe362cSAl Viro static inline int tree_contains_unbindable(struct mount *mnt) 26759676f0c6SRam Pai { 2676315fc83eSAl Viro struct mount *p; 2677909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 2678fc7be130SAl Viro if (IS_MNT_UNBINDABLE(p)) 26799676f0c6SRam Pai return 1; 26809676f0c6SRam Pai } 26819676f0c6SRam Pai return 0; 26829676f0c6SRam Pai } 26839676f0c6SRam Pai 268444dfd84aSDavid Howells /* 268544dfd84aSDavid Howells * Check that there aren't references to earlier/same mount namespaces in the 268644dfd84aSDavid Howells * specified subtree. Such references can act as pins for mount namespaces 268744dfd84aSDavid Howells * that aren't checked by the mount-cycle checking code, thereby allowing 268844dfd84aSDavid Howells * cycles to be made. 268944dfd84aSDavid Howells */ 269044dfd84aSDavid Howells static bool check_for_nsfs_mounts(struct mount *subtree) 269144dfd84aSDavid Howells { 269244dfd84aSDavid Howells struct mount *p; 269344dfd84aSDavid Howells bool ret = false; 269444dfd84aSDavid Howells 269544dfd84aSDavid Howells lock_mount_hash(); 269644dfd84aSDavid Howells for (p = subtree; p; p = next_mnt(p, subtree)) 269744dfd84aSDavid Howells if (mnt_ns_loop(p->mnt.mnt_root)) 269844dfd84aSDavid Howells goto out; 269944dfd84aSDavid Howells 270044dfd84aSDavid Howells ret = true; 270144dfd84aSDavid Howells out: 270244dfd84aSDavid Howells unlock_mount_hash(); 270344dfd84aSDavid Howells return ret; 270444dfd84aSDavid Howells } 270544dfd84aSDavid Howells 27062db154b3SDavid Howells static int do_move_mount(struct path *old_path, struct path *new_path) 27071da177e4SLinus Torvalds { 270844dfd84aSDavid Howells struct mnt_namespace *ns; 2709676da58dSAl Viro struct mount *p; 27100fb54e50SAl Viro struct mount *old; 27112763d119SAl Viro struct mount *parent; 27122763d119SAl Viro struct mountpoint *mp, *old_mp; 271357eccb83SAl Viro int err; 271444dfd84aSDavid Howells bool attached; 27151da177e4SLinus Torvalds 27162db154b3SDavid Howells mp = lock_mount(new_path); 271784d17192SAl Viro if (IS_ERR(mp)) 27182db154b3SDavid Howells return PTR_ERR(mp); 2719cc53ce53SDavid Howells 27202db154b3SDavid Howells old = real_mount(old_path->mnt); 27212db154b3SDavid Howells p = real_mount(new_path->mnt); 27222763d119SAl Viro parent = old->mnt_parent; 272344dfd84aSDavid Howells attached = mnt_has_parent(old); 27242763d119SAl Viro old_mp = old->mnt_mp; 272544dfd84aSDavid Howells ns = old->mnt_ns; 2726143c8c91SAl Viro 27271da177e4SLinus Torvalds err = -EINVAL; 272844dfd84aSDavid Howells /* The mountpoint must be in our namespace. */ 272944dfd84aSDavid Howells if (!check_mnt(p)) 27302db154b3SDavid Howells goto out; 27311da177e4SLinus Torvalds 2732570d7a98SEric Biggers /* The thing moved must be mounted... */ 2733570d7a98SEric Biggers if (!is_mounted(&old->mnt)) 273444dfd84aSDavid Howells goto out; 273544dfd84aSDavid Howells 2736570d7a98SEric Biggers /* ... and either ours or the root of anon namespace */ 2737570d7a98SEric Biggers if (!(attached ? check_mnt(old) : is_anon_ns(ns))) 27382db154b3SDavid Howells goto out; 27391da177e4SLinus Torvalds 27402db154b3SDavid Howells if (old->mnt.mnt_flags & MNT_LOCKED) 27412db154b3SDavid Howells goto out; 27422db154b3SDavid Howells 27432db154b3SDavid Howells if (old_path->dentry != old_path->mnt->mnt_root) 27442db154b3SDavid Howells goto out; 27452db154b3SDavid Howells 27462db154b3SDavid Howells if (d_is_dir(new_path->dentry) != 27472db154b3SDavid Howells d_is_dir(old_path->dentry)) 27482db154b3SDavid Howells goto out; 274921444403SRam Pai /* 275021444403SRam Pai * Don't move a mount residing in a shared parent. 275121444403SRam Pai */ 27522763d119SAl Viro if (attached && IS_MNT_SHARED(parent)) 27532db154b3SDavid Howells goto out; 27549676f0c6SRam Pai /* 27559676f0c6SRam Pai * Don't move a mount tree containing unbindable mounts to a destination 27569676f0c6SRam Pai * mount which is shared. 27579676f0c6SRam Pai */ 2758fc7be130SAl Viro if (IS_MNT_SHARED(p) && tree_contains_unbindable(old)) 27592db154b3SDavid Howells goto out; 27601da177e4SLinus Torvalds err = -ELOOP; 276144dfd84aSDavid Howells if (!check_for_nsfs_mounts(old)) 276244dfd84aSDavid Howells goto out; 2763fc7be130SAl Viro for (; mnt_has_parent(p); p = p->mnt_parent) 2764676da58dSAl Viro if (p == old) 27652db154b3SDavid Howells goto out; 27661da177e4SLinus Torvalds 27672db154b3SDavid Howells err = attach_recursive_mnt(old, real_mount(new_path->mnt), mp, 27682763d119SAl Viro attached); 27694ac91378SJan Blunck if (err) 27702db154b3SDavid Howells goto out; 27711da177e4SLinus Torvalds 27721da177e4SLinus Torvalds /* if the mount is moved, it should no longer be expire 27731da177e4SLinus Torvalds * automatically */ 27746776db3dSAl Viro list_del_init(&old->mnt_expire); 27752763d119SAl Viro if (attached) 27762763d119SAl Viro put_mountpoint(old_mp); 27771da177e4SLinus Torvalds out: 27782db154b3SDavid Howells unlock_mount(mp); 277944dfd84aSDavid Howells if (!err) { 27802763d119SAl Viro if (attached) 27812763d119SAl Viro mntput_no_expire(parent); 27822763d119SAl Viro else 278344dfd84aSDavid Howells free_mnt_ns(ns); 278444dfd84aSDavid Howells } 27852db154b3SDavid Howells return err; 27862db154b3SDavid Howells } 27872db154b3SDavid Howells 27882db154b3SDavid Howells static int do_move_mount_old(struct path *path, const char *old_name) 27892db154b3SDavid Howells { 27902db154b3SDavid Howells struct path old_path; 27912db154b3SDavid Howells int err; 27922db154b3SDavid Howells 27932db154b3SDavid Howells if (!old_name || !*old_name) 27942db154b3SDavid Howells return -EINVAL; 27952db154b3SDavid Howells 27962db154b3SDavid Howells err = kern_path(old_name, LOOKUP_FOLLOW, &old_path); 27972db154b3SDavid Howells if (err) 27982db154b3SDavid Howells return err; 27992db154b3SDavid Howells 28002db154b3SDavid Howells err = do_move_mount(&old_path, path); 28012d92ab3cSAl Viro path_put(&old_path); 28021da177e4SLinus Torvalds return err; 28031da177e4SLinus Torvalds } 28041da177e4SLinus Torvalds 28059d412a43SAl Viro /* 28069d412a43SAl Viro * add a mount into a namespace's mount tree 28079d412a43SAl Viro */ 28088f11538eSAl Viro static int do_add_mount(struct mount *newmnt, struct mountpoint *mp, 28098f11538eSAl Viro struct path *path, int mnt_flags) 28109d412a43SAl Viro { 28118f11538eSAl Viro struct mount *parent = real_mount(path->mnt); 28129d412a43SAl Viro 2813f2ebb3a9SAl Viro mnt_flags &= ~MNT_INTERNAL_FLAGS; 28149d412a43SAl Viro 281584d17192SAl Viro if (unlikely(!check_mnt(parent))) { 2816156cacb1SAl Viro /* that's acceptable only for automounts done in private ns */ 2817156cacb1SAl Viro if (!(mnt_flags & MNT_SHRINKABLE)) 28188f11538eSAl Viro return -EINVAL; 2819156cacb1SAl Viro /* ... and for those we'd better have mountpoint still alive */ 282084d17192SAl Viro if (!parent->mnt_ns) 28218f11538eSAl Viro return -EINVAL; 2822156cacb1SAl Viro } 28239d412a43SAl Viro 28249d412a43SAl Viro /* Refuse the same filesystem on the same mount point */ 282595bc5f25SAl Viro if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb && 28269d412a43SAl Viro path->mnt->mnt_root == path->dentry) 28278f11538eSAl Viro return -EBUSY; 28289d412a43SAl Viro 2829e36cb0b8SDavid Howells if (d_is_symlink(newmnt->mnt.mnt_root)) 28308f11538eSAl Viro return -EINVAL; 28319d412a43SAl Viro 283295bc5f25SAl Viro newmnt->mnt.mnt_flags = mnt_flags; 28338f11538eSAl Viro return graft_tree(newmnt, parent, mp); 28349d412a43SAl Viro } 2835b1e75df4SAl Viro 2836132e4608SDavid Howells static bool mount_too_revealing(const struct super_block *sb, int *new_mnt_flags); 2837132e4608SDavid Howells 2838132e4608SDavid Howells /* 2839132e4608SDavid Howells * Create a new mount using a superblock configuration and request it 2840132e4608SDavid Howells * be added to the namespace tree. 2841132e4608SDavid Howells */ 2842132e4608SDavid Howells static int do_new_mount_fc(struct fs_context *fc, struct path *mountpoint, 2843132e4608SDavid Howells unsigned int mnt_flags) 2844132e4608SDavid Howells { 2845132e4608SDavid Howells struct vfsmount *mnt; 28468f11538eSAl Viro struct mountpoint *mp; 2847132e4608SDavid Howells struct super_block *sb = fc->root->d_sb; 2848132e4608SDavid Howells int error; 2849132e4608SDavid Howells 2850c9ce29edSAl Viro error = security_sb_kern_mount(sb); 2851c9ce29edSAl Viro if (!error && mount_too_revealing(sb, &mnt_flags)) 2852c9ce29edSAl Viro error = -EPERM; 2853c9ce29edSAl Viro 2854c9ce29edSAl Viro if (unlikely(error)) { 2855c9ce29edSAl Viro fc_drop_locked(fc); 2856c9ce29edSAl Viro return error; 2857132e4608SDavid Howells } 2858132e4608SDavid Howells 2859132e4608SDavid Howells up_write(&sb->s_umount); 2860132e4608SDavid Howells 2861132e4608SDavid Howells mnt = vfs_create_mount(fc); 2862132e4608SDavid Howells if (IS_ERR(mnt)) 2863132e4608SDavid Howells return PTR_ERR(mnt); 2864132e4608SDavid Howells 2865f8b92ba6SDeepa Dinamani mnt_warn_timestamp_expiry(mountpoint, mnt); 2866f8b92ba6SDeepa Dinamani 28678f11538eSAl Viro mp = lock_mount(mountpoint); 28688f11538eSAl Viro if (IS_ERR(mp)) { 28698f11538eSAl Viro mntput(mnt); 28708f11538eSAl Viro return PTR_ERR(mp); 28718f11538eSAl Viro } 28728f11538eSAl Viro error = do_add_mount(real_mount(mnt), mp, mountpoint, mnt_flags); 28738f11538eSAl Viro unlock_mount(mp); 28740ecee669SEric Biggers if (error < 0) 28750ecee669SEric Biggers mntput(mnt); 2876f8b92ba6SDeepa Dinamani return error; 2877f8b92ba6SDeepa Dinamani } 2878f8b92ba6SDeepa Dinamani 28791da177e4SLinus Torvalds /* 28801da177e4SLinus Torvalds * create a new mount for userspace and request it to be added into the 28811da177e4SLinus Torvalds * namespace's tree 28821da177e4SLinus Torvalds */ 2883e462ec50SDavid Howells static int do_new_mount(struct path *path, const char *fstype, int sb_flags, 2884808d4e3cSAl Viro int mnt_flags, const char *name, void *data) 28851da177e4SLinus Torvalds { 28860c55cfc4SEric W. Biederman struct file_system_type *type; 2887a0c9a8b8SAl Viro struct fs_context *fc; 2888a0c9a8b8SAl Viro const char *subtype = NULL; 2889a0c9a8b8SAl Viro int err = 0; 28901da177e4SLinus Torvalds 28910c55cfc4SEric W. Biederman if (!fstype) 28921da177e4SLinus Torvalds return -EINVAL; 28931da177e4SLinus Torvalds 28940c55cfc4SEric W. Biederman type = get_fs_type(fstype); 28950c55cfc4SEric W. Biederman if (!type) 28960c55cfc4SEric W. Biederman return -ENODEV; 28970c55cfc4SEric W. Biederman 2898a0c9a8b8SAl Viro if (type->fs_flags & FS_HAS_SUBTYPE) { 2899a0c9a8b8SAl Viro subtype = strchr(fstype, '.'); 2900a0c9a8b8SAl Viro if (subtype) { 2901a0c9a8b8SAl Viro subtype++; 2902a0c9a8b8SAl Viro if (!*subtype) { 29030c55cfc4SEric W. Biederman put_filesystem(type); 2904a0c9a8b8SAl Viro return -EINVAL; 2905a0c9a8b8SAl Viro } 2906a0c9a8b8SAl Viro } 29078654df4eSEric W. Biederman } 29088654df4eSEric W. Biederman 2909a0c9a8b8SAl Viro fc = fs_context_for_mount(type, sb_flags); 2910a0c9a8b8SAl Viro put_filesystem(type); 2911a0c9a8b8SAl Viro if (IS_ERR(fc)) 2912a0c9a8b8SAl Viro return PTR_ERR(fc); 2913a0c9a8b8SAl Viro 29143e1aeb00SDavid Howells if (subtype) 29153e1aeb00SDavid Howells err = vfs_parse_fs_string(fc, "subtype", 29163e1aeb00SDavid Howells subtype, strlen(subtype)); 29173e1aeb00SDavid Howells if (!err && name) 29183e1aeb00SDavid Howells err = vfs_parse_fs_string(fc, "source", name, strlen(name)); 2919a0c9a8b8SAl Viro if (!err) 2920a0c9a8b8SAl Viro err = parse_monolithic_mount_data(fc, data); 2921c3aabf07SAl Viro if (!err && !mount_capable(fc)) 2922c3aabf07SAl Viro err = -EPERM; 2923a0c9a8b8SAl Viro if (!err) 2924a0c9a8b8SAl Viro err = vfs_get_tree(fc); 2925132e4608SDavid Howells if (!err) 2926132e4608SDavid Howells err = do_new_mount_fc(fc, path, mnt_flags); 2927a0c9a8b8SAl Viro 2928a0c9a8b8SAl Viro put_fs_context(fc); 292915f9a3f3SAl Viro return err; 29301da177e4SLinus Torvalds } 29311da177e4SLinus Torvalds 293219a167afSAl Viro int finish_automount(struct vfsmount *m, struct path *path) 293319a167afSAl Viro { 293426df6034SAl Viro struct dentry *dentry = path->dentry; 29358f11538eSAl Viro struct mountpoint *mp; 293625e195aaSAl Viro struct mount *mnt; 293719a167afSAl Viro int err; 293825e195aaSAl Viro 293925e195aaSAl Viro if (!m) 294025e195aaSAl Viro return 0; 294125e195aaSAl Viro if (IS_ERR(m)) 294225e195aaSAl Viro return PTR_ERR(m); 294325e195aaSAl Viro 294425e195aaSAl Viro mnt = real_mount(m); 294519a167afSAl Viro /* The new mount record should have at least 2 refs to prevent it being 294619a167afSAl Viro * expired before we get a chance to add it 294719a167afSAl Viro */ 29486776db3dSAl Viro BUG_ON(mnt_get_count(mnt) < 2); 294919a167afSAl Viro 295019a167afSAl Viro if (m->mnt_sb == path->mnt->mnt_sb && 295126df6034SAl Viro m->mnt_root == dentry) { 2952b1e75df4SAl Viro err = -ELOOP; 295326df6034SAl Viro goto discard; 295419a167afSAl Viro } 295519a167afSAl Viro 295626df6034SAl Viro /* 295726df6034SAl Viro * we don't want to use lock_mount() - in this case finding something 295826df6034SAl Viro * that overmounts our mountpoint to be means "quitely drop what we've 295926df6034SAl Viro * got", not "try to mount it on top". 296026df6034SAl Viro */ 296126df6034SAl Viro inode_lock(dentry->d_inode); 296226df6034SAl Viro namespace_lock(); 296326df6034SAl Viro if (unlikely(cant_mount(dentry))) { 296426df6034SAl Viro err = -ENOENT; 296526df6034SAl Viro goto discard_locked; 296626df6034SAl Viro } 296726df6034SAl Viro rcu_read_lock(); 296826df6034SAl Viro if (unlikely(__lookup_mnt(path->mnt, dentry))) { 296926df6034SAl Viro rcu_read_unlock(); 297026df6034SAl Viro err = 0; 297126df6034SAl Viro goto discard_locked; 297226df6034SAl Viro } 297326df6034SAl Viro rcu_read_unlock(); 297426df6034SAl Viro mp = get_mountpoint(dentry); 29758f11538eSAl Viro if (IS_ERR(mp)) { 29768f11538eSAl Viro err = PTR_ERR(mp); 297726df6034SAl Viro goto discard_locked; 29788f11538eSAl Viro } 297926df6034SAl Viro 29808f11538eSAl Viro err = do_add_mount(mnt, mp, path, path->mnt->mnt_flags | MNT_SHRINKABLE); 29818f11538eSAl Viro unlock_mount(mp); 298226df6034SAl Viro if (unlikely(err)) 298326df6034SAl Viro goto discard; 298426df6034SAl Viro mntput(m); 2985b1e75df4SAl Viro return 0; 298626df6034SAl Viro 298726df6034SAl Viro discard_locked: 298826df6034SAl Viro namespace_unlock(); 298926df6034SAl Viro inode_unlock(dentry->d_inode); 299026df6034SAl Viro discard: 2991b1e75df4SAl Viro /* remove m from any expiration list it may be on */ 29926776db3dSAl Viro if (!list_empty(&mnt->mnt_expire)) { 299397216be0SAl Viro namespace_lock(); 29946776db3dSAl Viro list_del_init(&mnt->mnt_expire); 299597216be0SAl Viro namespace_unlock(); 299619a167afSAl Viro } 2997b1e75df4SAl Viro mntput(m); 2998b1e75df4SAl Viro mntput(m); 299919a167afSAl Viro return err; 300019a167afSAl Viro } 300119a167afSAl Viro 3002ea5b778aSDavid Howells /** 3003ea5b778aSDavid Howells * mnt_set_expiry - Put a mount on an expiration list 3004ea5b778aSDavid Howells * @mnt: The mount to list. 3005ea5b778aSDavid Howells * @expiry_list: The list to add the mount to. 3006ea5b778aSDavid Howells */ 3007ea5b778aSDavid Howells void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list) 3008ea5b778aSDavid Howells { 300997216be0SAl Viro namespace_lock(); 3010ea5b778aSDavid Howells 30116776db3dSAl Viro list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list); 3012ea5b778aSDavid Howells 301397216be0SAl Viro namespace_unlock(); 3014ea5b778aSDavid Howells } 3015ea5b778aSDavid Howells EXPORT_SYMBOL(mnt_set_expiry); 3016ea5b778aSDavid Howells 3017ea5b778aSDavid Howells /* 30181da177e4SLinus Torvalds * process a list of expirable mountpoints with the intent of discarding any 30191da177e4SLinus Torvalds * mountpoints that aren't in use and haven't been touched since last we came 30201da177e4SLinus Torvalds * here 30211da177e4SLinus Torvalds */ 30221da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts) 30231da177e4SLinus Torvalds { 3024761d5c38SAl Viro struct mount *mnt, *next; 30251da177e4SLinus Torvalds LIST_HEAD(graveyard); 30261da177e4SLinus Torvalds 30271da177e4SLinus Torvalds if (list_empty(mounts)) 30281da177e4SLinus Torvalds return; 30291da177e4SLinus Torvalds 303097216be0SAl Viro namespace_lock(); 3031719ea2fbSAl Viro lock_mount_hash(); 30321da177e4SLinus Torvalds 30331da177e4SLinus Torvalds /* extract from the expiration list every vfsmount that matches the 30341da177e4SLinus Torvalds * following criteria: 30351da177e4SLinus Torvalds * - only referenced by its parent vfsmount 30361da177e4SLinus Torvalds * - still marked for expiry (marked on the last call here; marks are 30371da177e4SLinus Torvalds * cleared by mntput()) 30381da177e4SLinus Torvalds */ 30396776db3dSAl Viro list_for_each_entry_safe(mnt, next, mounts, mnt_expire) { 3040863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1) || 30411ab59738SAl Viro propagate_mount_busy(mnt, 1)) 30421da177e4SLinus Torvalds continue; 30436776db3dSAl Viro list_move(&mnt->mnt_expire, &graveyard); 30441da177e4SLinus Torvalds } 3045bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 30466776db3dSAl Viro mnt = list_first_entry(&graveyard, struct mount, mnt_expire); 3047143c8c91SAl Viro touch_mnt_namespace(mnt->mnt_ns); 3048e819f152SEric W. Biederman umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC); 3049bcc5c7d2SAl Viro } 3050719ea2fbSAl Viro unlock_mount_hash(); 30513ab6abeeSAl Viro namespace_unlock(); 30521da177e4SLinus Torvalds } 30531da177e4SLinus Torvalds 30541da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry); 30551da177e4SLinus Torvalds 30561da177e4SLinus Torvalds /* 30575528f911STrond Myklebust * Ripoff of 'select_parent()' 30585528f911STrond Myklebust * 30595528f911STrond Myklebust * search the list of submounts for a given mountpoint, and move any 30605528f911STrond Myklebust * shrinkable submounts to the 'graveyard' list. 30615528f911STrond Myklebust */ 3062692afc31SAl Viro static int select_submounts(struct mount *parent, struct list_head *graveyard) 30635528f911STrond Myklebust { 3064692afc31SAl Viro struct mount *this_parent = parent; 30655528f911STrond Myklebust struct list_head *next; 30665528f911STrond Myklebust int found = 0; 30675528f911STrond Myklebust 30685528f911STrond Myklebust repeat: 30696b41d536SAl Viro next = this_parent->mnt_mounts.next; 30705528f911STrond Myklebust resume: 30716b41d536SAl Viro while (next != &this_parent->mnt_mounts) { 30725528f911STrond Myklebust struct list_head *tmp = next; 30736b41d536SAl Viro struct mount *mnt = list_entry(tmp, struct mount, mnt_child); 30745528f911STrond Myklebust 30755528f911STrond Myklebust next = tmp->next; 3076692afc31SAl Viro if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE)) 30775528f911STrond Myklebust continue; 30785528f911STrond Myklebust /* 30795528f911STrond Myklebust * Descend a level if the d_mounts list is non-empty. 30805528f911STrond Myklebust */ 30816b41d536SAl Viro if (!list_empty(&mnt->mnt_mounts)) { 30825528f911STrond Myklebust this_parent = mnt; 30835528f911STrond Myklebust goto repeat; 30845528f911STrond Myklebust } 30855528f911STrond Myklebust 30861ab59738SAl Viro if (!propagate_mount_busy(mnt, 1)) { 30876776db3dSAl Viro list_move_tail(&mnt->mnt_expire, graveyard); 30885528f911STrond Myklebust found++; 30895528f911STrond Myklebust } 30905528f911STrond Myklebust } 30915528f911STrond Myklebust /* 30925528f911STrond Myklebust * All done at this level ... ascend and resume the search 30935528f911STrond Myklebust */ 30945528f911STrond Myklebust if (this_parent != parent) { 30956b41d536SAl Viro next = this_parent->mnt_child.next; 30960714a533SAl Viro this_parent = this_parent->mnt_parent; 30975528f911STrond Myklebust goto resume; 30985528f911STrond Myklebust } 30995528f911STrond Myklebust return found; 31005528f911STrond Myklebust } 31015528f911STrond Myklebust 31025528f911STrond Myklebust /* 31035528f911STrond Myklebust * process a list of expirable mountpoints with the intent of discarding any 31045528f911STrond Myklebust * submounts of a specific parent mountpoint 310599b7db7bSNick Piggin * 310648a066e7SAl Viro * mount_lock must be held for write 31075528f911STrond Myklebust */ 3108b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt) 31095528f911STrond Myklebust { 31105528f911STrond Myklebust LIST_HEAD(graveyard); 3111761d5c38SAl Viro struct mount *m; 31125528f911STrond Myklebust 31135528f911STrond Myklebust /* extract submounts of 'mountpoint' from the expiration list */ 3114c35038beSAl Viro while (select_submounts(mnt, &graveyard)) { 3115bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 3116761d5c38SAl Viro m = list_first_entry(&graveyard, struct mount, 31176776db3dSAl Viro mnt_expire); 3118143c8c91SAl Viro touch_mnt_namespace(m->mnt_ns); 3119e819f152SEric W. Biederman umount_tree(m, UMOUNT_PROPAGATE|UMOUNT_SYNC); 3120bcc5c7d2SAl Viro } 3121bcc5c7d2SAl Viro } 31225528f911STrond Myklebust } 31235528f911STrond Myklebust 3124028abd92SChristoph Hellwig static void *copy_mount_options(const void __user * data) 31251da177e4SLinus Torvalds { 3126b40ef869SAl Viro char *copy; 3127d563d678SCatalin Marinas unsigned left, offset; 31281da177e4SLinus Torvalds 31291da177e4SLinus Torvalds if (!data) 3130b40ef869SAl Viro return NULL; 31311da177e4SLinus Torvalds 3132b40ef869SAl Viro copy = kmalloc(PAGE_SIZE, GFP_KERNEL); 3133b40ef869SAl Viro if (!copy) 3134b40ef869SAl Viro return ERR_PTR(-ENOMEM); 31351da177e4SLinus Torvalds 3136d563d678SCatalin Marinas left = copy_from_user(copy, data, PAGE_SIZE); 31371da177e4SLinus Torvalds 3138d563d678SCatalin Marinas /* 3139d563d678SCatalin Marinas * Not all architectures have an exact copy_from_user(). Resort to 3140d563d678SCatalin Marinas * byte at a time. 3141d563d678SCatalin Marinas */ 3142d563d678SCatalin Marinas offset = PAGE_SIZE - left; 3143d563d678SCatalin Marinas while (left) { 3144d563d678SCatalin Marinas char c; 3145d563d678SCatalin Marinas if (get_user(c, (const char __user *)data + offset)) 3146d563d678SCatalin Marinas break; 3147d563d678SCatalin Marinas copy[offset] = c; 3148d563d678SCatalin Marinas left--; 3149d563d678SCatalin Marinas offset++; 3150d563d678SCatalin Marinas } 3151d563d678SCatalin Marinas 3152d563d678SCatalin Marinas if (left == PAGE_SIZE) { 3153b40ef869SAl Viro kfree(copy); 3154b40ef869SAl Viro return ERR_PTR(-EFAULT); 31551da177e4SLinus Torvalds } 3156d563d678SCatalin Marinas 3157b40ef869SAl Viro return copy; 31581da177e4SLinus Torvalds } 31591da177e4SLinus Torvalds 3160028abd92SChristoph Hellwig static char *copy_mount_string(const void __user *data) 3161eca6f534SVegard Nossum { 3162fbdb4401SChandan Rajendra return data ? strndup_user(data, PATH_MAX) : NULL; 3163eca6f534SVegard Nossum } 3164eca6f534SVegard Nossum 31651da177e4SLinus Torvalds /* 31661da177e4SLinus Torvalds * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to 31671da177e4SLinus Torvalds * be given to the mount() call (ie: read-only, no-dev, no-suid etc). 31681da177e4SLinus Torvalds * 31691da177e4SLinus Torvalds * data is a (void *) that can point to any structure up to 31701da177e4SLinus Torvalds * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent 31711da177e4SLinus Torvalds * information (or be NULL). 31721da177e4SLinus Torvalds * 31731da177e4SLinus Torvalds * Pre-0.97 versions of mount() didn't have a flags word. 31741da177e4SLinus Torvalds * When the flags word was introduced its top half was required 31751da177e4SLinus Torvalds * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9. 31761da177e4SLinus Torvalds * Therefore, if this magic number is present, it carries no information 31771da177e4SLinus Torvalds * and must be discarded. 31781da177e4SLinus Torvalds */ 3179c60166f0SChristoph Hellwig int path_mount(const char *dev_name, struct path *path, 3180808d4e3cSAl Viro const char *type_page, unsigned long flags, void *data_page) 31811da177e4SLinus Torvalds { 3182e462ec50SDavid Howells unsigned int mnt_flags = 0, sb_flags; 3183a1e6aaa3SChristoph Hellwig int ret; 31841da177e4SLinus Torvalds 31851da177e4SLinus Torvalds /* Discard magic */ 31861da177e4SLinus Torvalds if ((flags & MS_MGC_MSK) == MS_MGC_VAL) 31871da177e4SLinus Torvalds flags &= ~MS_MGC_MSK; 31881da177e4SLinus Torvalds 31891da177e4SLinus Torvalds /* Basic sanity checks */ 31901da177e4SLinus Torvalds if (data_page) 31911da177e4SLinus Torvalds ((char *)data_page)[PAGE_SIZE - 1] = 0; 31921da177e4SLinus Torvalds 3193e462ec50SDavid Howells if (flags & MS_NOUSER) 3194e462ec50SDavid Howells return -EINVAL; 3195e462ec50SDavid Howells 3196a1e6aaa3SChristoph Hellwig ret = security_sb_mount(dev_name, path, type_page, flags, data_page); 3197a1e6aaa3SChristoph Hellwig if (ret) 3198a1e6aaa3SChristoph Hellwig return ret; 3199a1e6aaa3SChristoph Hellwig if (!may_mount()) 3200a1e6aaa3SChristoph Hellwig return -EPERM; 3201a1e6aaa3SChristoph Hellwig if ((flags & SB_MANDLOCK) && !may_mandlock()) 3202a1e6aaa3SChristoph Hellwig return -EPERM; 3203a27ab9f2STetsuo Handa 3204613cbe3dSAndi Kleen /* Default to relatime unless overriden */ 3205613cbe3dSAndi Kleen if (!(flags & MS_NOATIME)) 32060a1c01c9SMatthew Garrett mnt_flags |= MNT_RELATIME; 32070a1c01c9SMatthew Garrett 32081da177e4SLinus Torvalds /* Separate the per-mountpoint flags */ 32091da177e4SLinus Torvalds if (flags & MS_NOSUID) 32101da177e4SLinus Torvalds mnt_flags |= MNT_NOSUID; 32111da177e4SLinus Torvalds if (flags & MS_NODEV) 32121da177e4SLinus Torvalds mnt_flags |= MNT_NODEV; 32131da177e4SLinus Torvalds if (flags & MS_NOEXEC) 32141da177e4SLinus Torvalds mnt_flags |= MNT_NOEXEC; 3215fc33a7bbSChristoph Hellwig if (flags & MS_NOATIME) 3216fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NOATIME; 3217fc33a7bbSChristoph Hellwig if (flags & MS_NODIRATIME) 3218fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NODIRATIME; 3219d0adde57SMatthew Garrett if (flags & MS_STRICTATIME) 3220d0adde57SMatthew Garrett mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME); 3221a9e5b732SDavid Howells if (flags & MS_RDONLY) 32222e4b7fcdSDave Hansen mnt_flags |= MNT_READONLY; 3223dab741e0SMattias Nissler if (flags & MS_NOSYMFOLLOW) 3224dab741e0SMattias Nissler mnt_flags |= MNT_NOSYMFOLLOW; 3225fc33a7bbSChristoph Hellwig 3226ffbc6f0eSEric W. Biederman /* The default atime for remount is preservation */ 3227ffbc6f0eSEric W. Biederman if ((flags & MS_REMOUNT) && 3228ffbc6f0eSEric W. Biederman ((flags & (MS_NOATIME | MS_NODIRATIME | MS_RELATIME | 3229ffbc6f0eSEric W. Biederman MS_STRICTATIME)) == 0)) { 3230ffbc6f0eSEric W. Biederman mnt_flags &= ~MNT_ATIME_MASK; 3231a1e6aaa3SChristoph Hellwig mnt_flags |= path->mnt->mnt_flags & MNT_ATIME_MASK; 3232ffbc6f0eSEric W. Biederman } 3233ffbc6f0eSEric W. Biederman 3234e462ec50SDavid Howells sb_flags = flags & (SB_RDONLY | 3235e462ec50SDavid Howells SB_SYNCHRONOUS | 3236e462ec50SDavid Howells SB_MANDLOCK | 3237e462ec50SDavid Howells SB_DIRSYNC | 3238e462ec50SDavid Howells SB_SILENT | 3239917086ffSMimi Zohar SB_POSIXACL | 3240d7ee9469SMarkus Trippelsdorf SB_LAZYTIME | 3241917086ffSMimi Zohar SB_I_VERSION); 32421da177e4SLinus Torvalds 324343f5e655SDavid Howells if ((flags & (MS_REMOUNT | MS_BIND)) == (MS_REMOUNT | MS_BIND)) 3244a1e6aaa3SChristoph Hellwig return do_reconfigure_mnt(path, mnt_flags); 3245a1e6aaa3SChristoph Hellwig if (flags & MS_REMOUNT) 3246a1e6aaa3SChristoph Hellwig return do_remount(path, flags, sb_flags, mnt_flags, data_page); 3247a1e6aaa3SChristoph Hellwig if (flags & MS_BIND) 3248a1e6aaa3SChristoph Hellwig return do_loopback(path, dev_name, flags & MS_REC); 3249a1e6aaa3SChristoph Hellwig if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 3250a1e6aaa3SChristoph Hellwig return do_change_type(path, flags); 3251a1e6aaa3SChristoph Hellwig if (flags & MS_MOVE) 3252a1e6aaa3SChristoph Hellwig return do_move_mount_old(path, dev_name); 3253a1e6aaa3SChristoph Hellwig 3254a1e6aaa3SChristoph Hellwig return do_new_mount(path, type_page, sb_flags, mnt_flags, dev_name, 32551da177e4SLinus Torvalds data_page); 3256a1e6aaa3SChristoph Hellwig } 3257a1e6aaa3SChristoph Hellwig 3258a1e6aaa3SChristoph Hellwig long do_mount(const char *dev_name, const char __user *dir_name, 3259a1e6aaa3SChristoph Hellwig const char *type_page, unsigned long flags, void *data_page) 3260a1e6aaa3SChristoph Hellwig { 3261a1e6aaa3SChristoph Hellwig struct path path; 3262a1e6aaa3SChristoph Hellwig int ret; 3263a1e6aaa3SChristoph Hellwig 3264a1e6aaa3SChristoph Hellwig ret = user_path_at(AT_FDCWD, dir_name, LOOKUP_FOLLOW, &path); 3265a1e6aaa3SChristoph Hellwig if (ret) 3266a1e6aaa3SChristoph Hellwig return ret; 3267a1e6aaa3SChristoph Hellwig ret = path_mount(dev_name, &path, type_page, flags, data_page); 32682d92ab3cSAl Viro path_put(&path); 3269a1e6aaa3SChristoph Hellwig return ret; 32701da177e4SLinus Torvalds } 32711da177e4SLinus Torvalds 3272537f7ccbSEric W. Biederman static struct ucounts *inc_mnt_namespaces(struct user_namespace *ns) 3273537f7ccbSEric W. Biederman { 3274537f7ccbSEric W. Biederman return inc_ucount(ns, current_euid(), UCOUNT_MNT_NAMESPACES); 3275537f7ccbSEric W. Biederman } 3276537f7ccbSEric W. Biederman 3277537f7ccbSEric W. Biederman static void dec_mnt_namespaces(struct ucounts *ucounts) 3278537f7ccbSEric W. Biederman { 3279537f7ccbSEric W. Biederman dec_ucount(ucounts, UCOUNT_MNT_NAMESPACES); 3280537f7ccbSEric W. Biederman } 3281537f7ccbSEric W. Biederman 3282771b1371SEric W. Biederman static void free_mnt_ns(struct mnt_namespace *ns) 3283771b1371SEric W. Biederman { 328474e83122SAl Viro if (!is_anon_ns(ns)) 32856344c433SAl Viro ns_free_inum(&ns->ns); 3286537f7ccbSEric W. Biederman dec_mnt_namespaces(ns->ucounts); 3287771b1371SEric W. Biederman put_user_ns(ns->user_ns); 3288771b1371SEric W. Biederman kfree(ns); 3289771b1371SEric W. Biederman } 3290771b1371SEric W. Biederman 32918823c079SEric W. Biederman /* 32928823c079SEric W. Biederman * Assign a sequence number so we can detect when we attempt to bind 32938823c079SEric W. Biederman * mount a reference to an older mount namespace into the current 32948823c079SEric W. Biederman * mount namespace, preventing reference counting loops. A 64bit 32958823c079SEric W. Biederman * number incrementing at 10Ghz will take 12,427 years to wrap which 32968823c079SEric W. Biederman * is effectively never, so we can ignore the possibility. 32978823c079SEric W. Biederman */ 32988823c079SEric W. Biederman static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1); 32998823c079SEric W. Biederman 330074e83122SAl Viro static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool anon) 3301cf8d2c11STrond Myklebust { 3302cf8d2c11STrond Myklebust struct mnt_namespace *new_ns; 3303537f7ccbSEric W. Biederman struct ucounts *ucounts; 330498f842e6SEric W. Biederman int ret; 3305cf8d2c11STrond Myklebust 3306537f7ccbSEric W. Biederman ucounts = inc_mnt_namespaces(user_ns); 3307537f7ccbSEric W. Biederman if (!ucounts) 3308df75e774SEric W. Biederman return ERR_PTR(-ENOSPC); 3309537f7ccbSEric W. Biederman 3310*30acd0bdSVasily Averin new_ns = kzalloc(sizeof(struct mnt_namespace), GFP_KERNEL_ACCOUNT); 3311537f7ccbSEric W. Biederman if (!new_ns) { 3312537f7ccbSEric W. Biederman dec_mnt_namespaces(ucounts); 3313cf8d2c11STrond Myklebust return ERR_PTR(-ENOMEM); 3314537f7ccbSEric W. Biederman } 331574e83122SAl Viro if (!anon) { 33166344c433SAl Viro ret = ns_alloc_inum(&new_ns->ns); 331798f842e6SEric W. Biederman if (ret) { 331898f842e6SEric W. Biederman kfree(new_ns); 3319537f7ccbSEric W. Biederman dec_mnt_namespaces(ucounts); 332098f842e6SEric W. Biederman return ERR_PTR(ret); 332198f842e6SEric W. Biederman } 332274e83122SAl Viro } 332333c42940SAl Viro new_ns->ns.ops = &mntns_operations; 332474e83122SAl Viro if (!anon) 33258823c079SEric W. Biederman new_ns->seq = atomic64_add_return(1, &mnt_ns_seq); 33261a7b8969SKirill Tkhai refcount_set(&new_ns->ns.count, 1); 3327cf8d2c11STrond Myklebust INIT_LIST_HEAD(&new_ns->list); 3328cf8d2c11STrond Myklebust init_waitqueue_head(&new_ns->poll); 33299f6c61f9SMiklos Szeredi spin_lock_init(&new_ns->ns_lock); 3330771b1371SEric W. Biederman new_ns->user_ns = get_user_ns(user_ns); 3331537f7ccbSEric W. Biederman new_ns->ucounts = ucounts; 3332cf8d2c11STrond Myklebust return new_ns; 3333cf8d2c11STrond Myklebust } 3334cf8d2c11STrond Myklebust 33350766f788SEmese Revfy __latent_entropy 33369559f689SAl Viro struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns, 33379559f689SAl Viro struct user_namespace *user_ns, struct fs_struct *new_fs) 33381da177e4SLinus Torvalds { 33396b3286edSKirill Korotaev struct mnt_namespace *new_ns; 33407f2da1e7SAl Viro struct vfsmount *rootmnt = NULL, *pwdmnt = NULL; 3341315fc83eSAl Viro struct mount *p, *q; 33429559f689SAl Viro struct mount *old; 3343cb338d06SAl Viro struct mount *new; 33447a472ef4SEric W. Biederman int copy_flags; 33451da177e4SLinus Torvalds 33469559f689SAl Viro BUG_ON(!ns); 33479559f689SAl Viro 33489559f689SAl Viro if (likely(!(flags & CLONE_NEWNS))) { 33499559f689SAl Viro get_mnt_ns(ns); 33509559f689SAl Viro return ns; 33519559f689SAl Viro } 33529559f689SAl Viro 33539559f689SAl Viro old = ns->root; 33549559f689SAl Viro 335574e83122SAl Viro new_ns = alloc_mnt_ns(user_ns, false); 3356cf8d2c11STrond Myklebust if (IS_ERR(new_ns)) 3357cf8d2c11STrond Myklebust return new_ns; 33581da177e4SLinus Torvalds 335997216be0SAl Viro namespace_lock(); 33601da177e4SLinus Torvalds /* First pass: copy the tree topology */ 33614ce5d2b1SEric W. Biederman copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE; 33629559f689SAl Viro if (user_ns != ns->user_ns) 33633bd045ccSAl Viro copy_flags |= CL_SHARED_TO_SLAVE; 33647a472ef4SEric W. Biederman new = copy_tree(old, old->mnt.mnt_root, copy_flags); 3365be34d1a3SDavid Howells if (IS_ERR(new)) { 3366328e6d90SAl Viro namespace_unlock(); 3367771b1371SEric W. Biederman free_mnt_ns(new_ns); 3368be34d1a3SDavid Howells return ERR_CAST(new); 33691da177e4SLinus Torvalds } 33703bd045ccSAl Viro if (user_ns != ns->user_ns) { 33713bd045ccSAl Viro lock_mount_hash(); 33723bd045ccSAl Viro lock_mnt_tree(new); 33733bd045ccSAl Viro unlock_mount_hash(); 33743bd045ccSAl Viro } 3375be08d6d2SAl Viro new_ns->root = new; 33761a4eeaf2SAl Viro list_add_tail(&new_ns->list, &new->mnt_list); 33771da177e4SLinus Torvalds 33781da177e4SLinus Torvalds /* 33791da177e4SLinus Torvalds * Second pass: switch the tsk->fs->* elements and mark new vfsmounts 33801da177e4SLinus Torvalds * as belonging to new namespace. We have already acquired a private 33811da177e4SLinus Torvalds * fs_struct, so tsk->fs->lock is not needed. 33821da177e4SLinus Torvalds */ 3383909b0a88SAl Viro p = old; 3384cb338d06SAl Viro q = new; 33851da177e4SLinus Torvalds while (p) { 3386143c8c91SAl Viro q->mnt_ns = new_ns; 3387d2921684SEric W. Biederman new_ns->mounts++; 33889559f689SAl Viro if (new_fs) { 33899559f689SAl Viro if (&p->mnt == new_fs->root.mnt) { 33909559f689SAl Viro new_fs->root.mnt = mntget(&q->mnt); 3391315fc83eSAl Viro rootmnt = &p->mnt; 33921da177e4SLinus Torvalds } 33939559f689SAl Viro if (&p->mnt == new_fs->pwd.mnt) { 33949559f689SAl Viro new_fs->pwd.mnt = mntget(&q->mnt); 3395315fc83eSAl Viro pwdmnt = &p->mnt; 33961da177e4SLinus Torvalds } 33971da177e4SLinus Torvalds } 3398909b0a88SAl Viro p = next_mnt(p, old); 3399909b0a88SAl Viro q = next_mnt(q, new); 34004ce5d2b1SEric W. Biederman if (!q) 34014ce5d2b1SEric W. Biederman break; 34024ce5d2b1SEric W. Biederman while (p->mnt.mnt_root != q->mnt.mnt_root) 34034ce5d2b1SEric W. Biederman p = next_mnt(p, old); 34041da177e4SLinus Torvalds } 3405328e6d90SAl Viro namespace_unlock(); 34061da177e4SLinus Torvalds 34071da177e4SLinus Torvalds if (rootmnt) 3408f03c6599SAl Viro mntput(rootmnt); 34091da177e4SLinus Torvalds if (pwdmnt) 3410f03c6599SAl Viro mntput(pwdmnt); 34111da177e4SLinus Torvalds 3412741a2951SJANAK DESAI return new_ns; 3413741a2951SJANAK DESAI } 3414741a2951SJANAK DESAI 341574e83122SAl Viro struct dentry *mount_subtree(struct vfsmount *m, const char *name) 3416cf8d2c11STrond Myklebust { 34171a4eeaf2SAl Viro struct mount *mnt = real_mount(m); 3418ea441d11SAl Viro struct mnt_namespace *ns; 3419d31da0f0SAl Viro struct super_block *s; 3420ea441d11SAl Viro struct path path; 3421ea441d11SAl Viro int err; 3422ea441d11SAl Viro 342374e83122SAl Viro ns = alloc_mnt_ns(&init_user_ns, true); 342474e83122SAl Viro if (IS_ERR(ns)) { 342574e83122SAl Viro mntput(m); 3426ea441d11SAl Viro return ERR_CAST(ns); 342774e83122SAl Viro } 342874e83122SAl Viro mnt->mnt_ns = ns; 342974e83122SAl Viro ns->root = mnt; 343074e83122SAl Viro ns->mounts++; 343174e83122SAl Viro list_add(&mnt->mnt_list, &ns->list); 3432ea441d11SAl Viro 343374e83122SAl Viro err = vfs_path_lookup(m->mnt_root, m, 3434ea441d11SAl Viro name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path); 3435ea441d11SAl Viro 3436ea441d11SAl Viro put_mnt_ns(ns); 3437ea441d11SAl Viro 3438ea441d11SAl Viro if (err) 3439ea441d11SAl Viro return ERR_PTR(err); 3440ea441d11SAl Viro 3441ea441d11SAl Viro /* trade a vfsmount reference for active sb one */ 3442d31da0f0SAl Viro s = path.mnt->mnt_sb; 3443d31da0f0SAl Viro atomic_inc(&s->s_active); 3444ea441d11SAl Viro mntput(path.mnt); 3445ea441d11SAl Viro /* lock the sucker */ 3446d31da0f0SAl Viro down_write(&s->s_umount); 3447ea441d11SAl Viro /* ... and return the root of (sub)tree on it */ 3448ea441d11SAl Viro return path.dentry; 3449ea441d11SAl Viro } 3450ea441d11SAl Viro EXPORT_SYMBOL(mount_subtree); 3451ea441d11SAl Viro 3452cccaa5e3SDominik Brodowski SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name, 3453cccaa5e3SDominik Brodowski char __user *, type, unsigned long, flags, void __user *, data) 34541da177e4SLinus Torvalds { 3455eca6f534SVegard Nossum int ret; 3456eca6f534SVegard Nossum char *kernel_type; 3457eca6f534SVegard Nossum char *kernel_dev; 3458b40ef869SAl Viro void *options; 34591da177e4SLinus Torvalds 3460b8850d1fSTim Gardner kernel_type = copy_mount_string(type); 3461b8850d1fSTim Gardner ret = PTR_ERR(kernel_type); 3462b8850d1fSTim Gardner if (IS_ERR(kernel_type)) 3463eca6f534SVegard Nossum goto out_type; 34641da177e4SLinus Torvalds 3465b8850d1fSTim Gardner kernel_dev = copy_mount_string(dev_name); 3466b8850d1fSTim Gardner ret = PTR_ERR(kernel_dev); 3467b8850d1fSTim Gardner if (IS_ERR(kernel_dev)) 3468eca6f534SVegard Nossum goto out_dev; 34691da177e4SLinus Torvalds 3470b40ef869SAl Viro options = copy_mount_options(data); 3471b40ef869SAl Viro ret = PTR_ERR(options); 3472b40ef869SAl Viro if (IS_ERR(options)) 3473eca6f534SVegard Nossum goto out_data; 34741da177e4SLinus Torvalds 3475b40ef869SAl Viro ret = do_mount(kernel_dev, dir_name, kernel_type, flags, options); 3476eca6f534SVegard Nossum 3477b40ef869SAl Viro kfree(options); 3478eca6f534SVegard Nossum out_data: 3479eca6f534SVegard Nossum kfree(kernel_dev); 3480eca6f534SVegard Nossum out_dev: 3481eca6f534SVegard Nossum kfree(kernel_type); 3482eca6f534SVegard Nossum out_type: 3483eca6f534SVegard Nossum return ret; 34841da177e4SLinus Torvalds } 34851da177e4SLinus Torvalds 34865b490500SChristian Brauner #define FSMOUNT_VALID_FLAGS \ 34875b490500SChristian Brauner (MOUNT_ATTR_RDONLY | MOUNT_ATTR_NOSUID | MOUNT_ATTR_NODEV | \ 3488dd8b477fSChristian Brauner MOUNT_ATTR_NOEXEC | MOUNT_ATTR__ATIME | MOUNT_ATTR_NODIRATIME | \ 3489dd8b477fSChristian Brauner MOUNT_ATTR_NOSYMFOLLOW) 34905b490500SChristian Brauner 34919caccd41SChristian Brauner #define MOUNT_SETATTR_VALID_FLAGS (FSMOUNT_VALID_FLAGS | MOUNT_ATTR_IDMAP) 34922a186721SChristian Brauner 34932a186721SChristian Brauner #define MOUNT_SETATTR_PROPAGATION_FLAGS \ 34942a186721SChristian Brauner (MS_UNBINDABLE | MS_PRIVATE | MS_SLAVE | MS_SHARED) 34952a186721SChristian Brauner 34965b490500SChristian Brauner static unsigned int attr_flags_to_mnt_flags(u64 attr_flags) 34975b490500SChristian Brauner { 34985b490500SChristian Brauner unsigned int mnt_flags = 0; 34995b490500SChristian Brauner 35005b490500SChristian Brauner if (attr_flags & MOUNT_ATTR_RDONLY) 35015b490500SChristian Brauner mnt_flags |= MNT_READONLY; 35025b490500SChristian Brauner if (attr_flags & MOUNT_ATTR_NOSUID) 35035b490500SChristian Brauner mnt_flags |= MNT_NOSUID; 35045b490500SChristian Brauner if (attr_flags & MOUNT_ATTR_NODEV) 35055b490500SChristian Brauner mnt_flags |= MNT_NODEV; 35065b490500SChristian Brauner if (attr_flags & MOUNT_ATTR_NOEXEC) 35075b490500SChristian Brauner mnt_flags |= MNT_NOEXEC; 35085b490500SChristian Brauner if (attr_flags & MOUNT_ATTR_NODIRATIME) 35095b490500SChristian Brauner mnt_flags |= MNT_NODIRATIME; 3510dd8b477fSChristian Brauner if (attr_flags & MOUNT_ATTR_NOSYMFOLLOW) 3511dd8b477fSChristian Brauner mnt_flags |= MNT_NOSYMFOLLOW; 35125b490500SChristian Brauner 35135b490500SChristian Brauner return mnt_flags; 35145b490500SChristian Brauner } 35155b490500SChristian Brauner 35161da177e4SLinus Torvalds /* 351793766fbdSDavid Howells * Create a kernel mount representation for a new, prepared superblock 351893766fbdSDavid Howells * (specified by fs_fd) and attach to an open_tree-like file descriptor. 351993766fbdSDavid Howells */ 352093766fbdSDavid Howells SYSCALL_DEFINE3(fsmount, int, fs_fd, unsigned int, flags, 352193766fbdSDavid Howells unsigned int, attr_flags) 352293766fbdSDavid Howells { 352393766fbdSDavid Howells struct mnt_namespace *ns; 352493766fbdSDavid Howells struct fs_context *fc; 352593766fbdSDavid Howells struct file *file; 352693766fbdSDavid Howells struct path newmount; 352793766fbdSDavid Howells struct mount *mnt; 352893766fbdSDavid Howells struct fd f; 352993766fbdSDavid Howells unsigned int mnt_flags = 0; 353093766fbdSDavid Howells long ret; 353193766fbdSDavid Howells 353293766fbdSDavid Howells if (!may_mount()) 353393766fbdSDavid Howells return -EPERM; 353493766fbdSDavid Howells 353593766fbdSDavid Howells if ((flags & ~(FSMOUNT_CLOEXEC)) != 0) 353693766fbdSDavid Howells return -EINVAL; 353793766fbdSDavid Howells 35385b490500SChristian Brauner if (attr_flags & ~FSMOUNT_VALID_FLAGS) 353993766fbdSDavid Howells return -EINVAL; 354093766fbdSDavid Howells 35415b490500SChristian Brauner mnt_flags = attr_flags_to_mnt_flags(attr_flags); 354293766fbdSDavid Howells 354393766fbdSDavid Howells switch (attr_flags & MOUNT_ATTR__ATIME) { 354493766fbdSDavid Howells case MOUNT_ATTR_STRICTATIME: 354593766fbdSDavid Howells break; 354693766fbdSDavid Howells case MOUNT_ATTR_NOATIME: 354793766fbdSDavid Howells mnt_flags |= MNT_NOATIME; 354893766fbdSDavid Howells break; 354993766fbdSDavid Howells case MOUNT_ATTR_RELATIME: 355093766fbdSDavid Howells mnt_flags |= MNT_RELATIME; 355193766fbdSDavid Howells break; 355293766fbdSDavid Howells default: 355393766fbdSDavid Howells return -EINVAL; 355493766fbdSDavid Howells } 355593766fbdSDavid Howells 355693766fbdSDavid Howells f = fdget(fs_fd); 355793766fbdSDavid Howells if (!f.file) 355893766fbdSDavid Howells return -EBADF; 355993766fbdSDavid Howells 356093766fbdSDavid Howells ret = -EINVAL; 356193766fbdSDavid Howells if (f.file->f_op != &fscontext_fops) 356293766fbdSDavid Howells goto err_fsfd; 356393766fbdSDavid Howells 356493766fbdSDavid Howells fc = f.file->private_data; 356593766fbdSDavid Howells 356693766fbdSDavid Howells ret = mutex_lock_interruptible(&fc->uapi_mutex); 356793766fbdSDavid Howells if (ret < 0) 356893766fbdSDavid Howells goto err_fsfd; 356993766fbdSDavid Howells 357093766fbdSDavid Howells /* There must be a valid superblock or we can't mount it */ 357193766fbdSDavid Howells ret = -EINVAL; 357293766fbdSDavid Howells if (!fc->root) 357393766fbdSDavid Howells goto err_unlock; 357493766fbdSDavid Howells 357593766fbdSDavid Howells ret = -EPERM; 357693766fbdSDavid Howells if (mount_too_revealing(fc->root->d_sb, &mnt_flags)) { 357793766fbdSDavid Howells pr_warn("VFS: Mount too revealing\n"); 357893766fbdSDavid Howells goto err_unlock; 357993766fbdSDavid Howells } 358093766fbdSDavid Howells 358193766fbdSDavid Howells ret = -EBUSY; 358293766fbdSDavid Howells if (fc->phase != FS_CONTEXT_AWAITING_MOUNT) 358393766fbdSDavid Howells goto err_unlock; 358493766fbdSDavid Howells 358593766fbdSDavid Howells ret = -EPERM; 358693766fbdSDavid Howells if ((fc->sb_flags & SB_MANDLOCK) && !may_mandlock()) 358793766fbdSDavid Howells goto err_unlock; 358893766fbdSDavid Howells 358993766fbdSDavid Howells newmount.mnt = vfs_create_mount(fc); 359093766fbdSDavid Howells if (IS_ERR(newmount.mnt)) { 359193766fbdSDavid Howells ret = PTR_ERR(newmount.mnt); 359293766fbdSDavid Howells goto err_unlock; 359393766fbdSDavid Howells } 359493766fbdSDavid Howells newmount.dentry = dget(fc->root); 359593766fbdSDavid Howells newmount.mnt->mnt_flags = mnt_flags; 359693766fbdSDavid Howells 359793766fbdSDavid Howells /* We've done the mount bit - now move the file context into more or 359893766fbdSDavid Howells * less the same state as if we'd done an fspick(). We don't want to 359993766fbdSDavid Howells * do any memory allocation or anything like that at this point as we 360093766fbdSDavid Howells * don't want to have to handle any errors incurred. 360193766fbdSDavid Howells */ 360293766fbdSDavid Howells vfs_clean_context(fc); 360393766fbdSDavid Howells 360493766fbdSDavid Howells ns = alloc_mnt_ns(current->nsproxy->mnt_ns->user_ns, true); 360593766fbdSDavid Howells if (IS_ERR(ns)) { 360693766fbdSDavid Howells ret = PTR_ERR(ns); 360793766fbdSDavid Howells goto err_path; 360893766fbdSDavid Howells } 360993766fbdSDavid Howells mnt = real_mount(newmount.mnt); 361093766fbdSDavid Howells mnt->mnt_ns = ns; 361193766fbdSDavid Howells ns->root = mnt; 361293766fbdSDavid Howells ns->mounts = 1; 361393766fbdSDavid Howells list_add(&mnt->mnt_list, &ns->list); 36141b0b9cc8SEric Biggers mntget(newmount.mnt); 361593766fbdSDavid Howells 361693766fbdSDavid Howells /* Attach to an apparent O_PATH fd with a note that we need to unmount 361793766fbdSDavid Howells * it, not just simply put it. 361893766fbdSDavid Howells */ 361993766fbdSDavid Howells file = dentry_open(&newmount, O_PATH, fc->cred); 362093766fbdSDavid Howells if (IS_ERR(file)) { 362193766fbdSDavid Howells dissolve_on_fput(newmount.mnt); 362293766fbdSDavid Howells ret = PTR_ERR(file); 362393766fbdSDavid Howells goto err_path; 362493766fbdSDavid Howells } 362593766fbdSDavid Howells file->f_mode |= FMODE_NEED_UNMOUNT; 362693766fbdSDavid Howells 362793766fbdSDavid Howells ret = get_unused_fd_flags((flags & FSMOUNT_CLOEXEC) ? O_CLOEXEC : 0); 362893766fbdSDavid Howells if (ret >= 0) 362993766fbdSDavid Howells fd_install(ret, file); 363093766fbdSDavid Howells else 363193766fbdSDavid Howells fput(file); 363293766fbdSDavid Howells 363393766fbdSDavid Howells err_path: 363493766fbdSDavid Howells path_put(&newmount); 363593766fbdSDavid Howells err_unlock: 363693766fbdSDavid Howells mutex_unlock(&fc->uapi_mutex); 363793766fbdSDavid Howells err_fsfd: 363893766fbdSDavid Howells fdput(f); 363993766fbdSDavid Howells return ret; 364093766fbdSDavid Howells } 364193766fbdSDavid Howells 364293766fbdSDavid Howells /* 364393766fbdSDavid Howells * Move a mount from one place to another. In combination with 364493766fbdSDavid Howells * fsopen()/fsmount() this is used to install a new mount and in combination 364593766fbdSDavid Howells * with open_tree(OPEN_TREE_CLONE [| AT_RECURSIVE]) it can be used to copy 364693766fbdSDavid Howells * a mount subtree. 36472db154b3SDavid Howells * 36482db154b3SDavid Howells * Note the flags value is a combination of MOVE_MOUNT_* flags. 36492db154b3SDavid Howells */ 36502db154b3SDavid Howells SYSCALL_DEFINE5(move_mount, 36512658ce09SBen Dooks int, from_dfd, const char __user *, from_pathname, 36522658ce09SBen Dooks int, to_dfd, const char __user *, to_pathname, 36532db154b3SDavid Howells unsigned int, flags) 36542db154b3SDavid Howells { 36552db154b3SDavid Howells struct path from_path, to_path; 36562db154b3SDavid Howells unsigned int lflags; 36572db154b3SDavid Howells int ret = 0; 36582db154b3SDavid Howells 36592db154b3SDavid Howells if (!may_mount()) 36602db154b3SDavid Howells return -EPERM; 36612db154b3SDavid Howells 36622db154b3SDavid Howells if (flags & ~MOVE_MOUNT__MASK) 36632db154b3SDavid Howells return -EINVAL; 36642db154b3SDavid Howells 36652db154b3SDavid Howells /* If someone gives a pathname, they aren't permitted to move 36662db154b3SDavid Howells * from an fd that requires unmount as we can't get at the flag 36672db154b3SDavid Howells * to clear it afterwards. 36682db154b3SDavid Howells */ 36692db154b3SDavid Howells lflags = 0; 36702db154b3SDavid Howells if (flags & MOVE_MOUNT_F_SYMLINKS) lflags |= LOOKUP_FOLLOW; 36712db154b3SDavid Howells if (flags & MOVE_MOUNT_F_AUTOMOUNTS) lflags |= LOOKUP_AUTOMOUNT; 36722db154b3SDavid Howells if (flags & MOVE_MOUNT_F_EMPTY_PATH) lflags |= LOOKUP_EMPTY; 36732db154b3SDavid Howells 36742db154b3SDavid Howells ret = user_path_at(from_dfd, from_pathname, lflags, &from_path); 36752db154b3SDavid Howells if (ret < 0) 36762db154b3SDavid Howells return ret; 36772db154b3SDavid Howells 36782db154b3SDavid Howells lflags = 0; 36792db154b3SDavid Howells if (flags & MOVE_MOUNT_T_SYMLINKS) lflags |= LOOKUP_FOLLOW; 36802db154b3SDavid Howells if (flags & MOVE_MOUNT_T_AUTOMOUNTS) lflags |= LOOKUP_AUTOMOUNT; 36812db154b3SDavid Howells if (flags & MOVE_MOUNT_T_EMPTY_PATH) lflags |= LOOKUP_EMPTY; 36822db154b3SDavid Howells 36832db154b3SDavid Howells ret = user_path_at(to_dfd, to_pathname, lflags, &to_path); 36842db154b3SDavid Howells if (ret < 0) 36852db154b3SDavid Howells goto out_from; 36862db154b3SDavid Howells 36872db154b3SDavid Howells ret = security_move_mount(&from_path, &to_path); 36882db154b3SDavid Howells if (ret < 0) 36892db154b3SDavid Howells goto out_to; 36902db154b3SDavid Howells 36912db154b3SDavid Howells ret = do_move_mount(&from_path, &to_path); 36922db154b3SDavid Howells 36932db154b3SDavid Howells out_to: 36942db154b3SDavid Howells path_put(&to_path); 36952db154b3SDavid Howells out_from: 36962db154b3SDavid Howells path_put(&from_path); 36972db154b3SDavid Howells return ret; 36982db154b3SDavid Howells } 36992db154b3SDavid Howells 37002db154b3SDavid Howells /* 3701afac7cbaSAl Viro * Return true if path is reachable from root 3702afac7cbaSAl Viro * 370348a066e7SAl Viro * namespace_sem or mount_lock is held 3704afac7cbaSAl Viro */ 3705643822b4SAl Viro bool is_path_reachable(struct mount *mnt, struct dentry *dentry, 3706afac7cbaSAl Viro const struct path *root) 3707afac7cbaSAl Viro { 3708643822b4SAl Viro while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) { 3709a73324daSAl Viro dentry = mnt->mnt_mountpoint; 37100714a533SAl Viro mnt = mnt->mnt_parent; 3711afac7cbaSAl Viro } 3712643822b4SAl Viro return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry); 3713afac7cbaSAl Viro } 3714afac7cbaSAl Viro 3715640eb7e7SMickaël Salaün bool path_is_under(const struct path *path1, const struct path *path2) 3716afac7cbaSAl Viro { 371725ab4c9bSYaowei Bai bool res; 371848a066e7SAl Viro read_seqlock_excl(&mount_lock); 3719643822b4SAl Viro res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2); 372048a066e7SAl Viro read_sequnlock_excl(&mount_lock); 3721afac7cbaSAl Viro return res; 3722afac7cbaSAl Viro } 3723afac7cbaSAl Viro EXPORT_SYMBOL(path_is_under); 3724afac7cbaSAl Viro 3725afac7cbaSAl Viro /* 37261da177e4SLinus Torvalds * pivot_root Semantics: 37271da177e4SLinus Torvalds * Moves the root file system of the current process to the directory put_old, 37281da177e4SLinus Torvalds * makes new_root as the new root file system of the current process, and sets 37291da177e4SLinus Torvalds * root/cwd of all processes which had them on the current root to new_root. 37301da177e4SLinus Torvalds * 37311da177e4SLinus Torvalds * Restrictions: 37321da177e4SLinus Torvalds * The new_root and put_old must be directories, and must not be on the 37331da177e4SLinus Torvalds * same file system as the current process root. The put_old must be 37341da177e4SLinus Torvalds * underneath new_root, i.e. adding a non-zero number of /.. to the string 37351da177e4SLinus Torvalds * pointed to by put_old must yield the same directory as new_root. No other 37361da177e4SLinus Torvalds * file system may be mounted on put_old. After all, new_root is a mountpoint. 37371da177e4SLinus Torvalds * 37384a0d11faSNeil Brown * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem. 37390c1bc6b8SMauro Carvalho Chehab * See Documentation/filesystems/ramfs-rootfs-initramfs.rst for alternatives 37404a0d11faSNeil Brown * in this situation. 37414a0d11faSNeil Brown * 37421da177e4SLinus Torvalds * Notes: 37431da177e4SLinus Torvalds * - we don't move root/cwd if they are not at the root (reason: if something 37441da177e4SLinus Torvalds * cared enough to change them, it's probably wrong to force them elsewhere) 37451da177e4SLinus Torvalds * - it's okay to pick a root that isn't the root of a file system, e.g. 37461da177e4SLinus Torvalds * /nfs/my_root where /nfs is the mount point. It must be a mountpoint, 37471da177e4SLinus Torvalds * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root 37481da177e4SLinus Torvalds * first. 37491da177e4SLinus Torvalds */ 37503480b257SHeiko Carstens SYSCALL_DEFINE2(pivot_root, const char __user *, new_root, 37513480b257SHeiko Carstens const char __user *, put_old) 37521da177e4SLinus Torvalds { 37532763d119SAl Viro struct path new, old, root; 37542763d119SAl Viro struct mount *new_mnt, *root_mnt, *old_mnt, *root_parent, *ex_parent; 375584d17192SAl Viro struct mountpoint *old_mp, *root_mp; 37561da177e4SLinus Torvalds int error; 37571da177e4SLinus Torvalds 37589b40bc90SAl Viro if (!may_mount()) 37591da177e4SLinus Torvalds return -EPERM; 37601da177e4SLinus Torvalds 3761ce6595a2SAl Viro error = user_path_at(AT_FDCWD, new_root, 3762ce6595a2SAl Viro LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &new); 37631da177e4SLinus Torvalds if (error) 37641da177e4SLinus Torvalds goto out0; 37651da177e4SLinus Torvalds 3766ce6595a2SAl Viro error = user_path_at(AT_FDCWD, put_old, 3767ce6595a2SAl Viro LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &old); 37681da177e4SLinus Torvalds if (error) 37691da177e4SLinus Torvalds goto out1; 37701da177e4SLinus Torvalds 37712d8f3038SAl Viro error = security_sb_pivotroot(&old, &new); 3772b12cea91SAl Viro if (error) 3773b12cea91SAl Viro goto out2; 37741da177e4SLinus Torvalds 3775f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &root); 377684d17192SAl Viro old_mp = lock_mount(&old); 377784d17192SAl Viro error = PTR_ERR(old_mp); 377884d17192SAl Viro if (IS_ERR(old_mp)) 3779b12cea91SAl Viro goto out3; 3780b12cea91SAl Viro 37811da177e4SLinus Torvalds error = -EINVAL; 3782419148daSAl Viro new_mnt = real_mount(new.mnt); 3783419148daSAl Viro root_mnt = real_mount(root.mnt); 378484d17192SAl Viro old_mnt = real_mount(old.mnt); 37852763d119SAl Viro ex_parent = new_mnt->mnt_parent; 37862763d119SAl Viro root_parent = root_mnt->mnt_parent; 378784d17192SAl Viro if (IS_MNT_SHARED(old_mnt) || 37882763d119SAl Viro IS_MNT_SHARED(ex_parent) || 37892763d119SAl Viro IS_MNT_SHARED(root_parent)) 3790b12cea91SAl Viro goto out4; 3791143c8c91SAl Viro if (!check_mnt(root_mnt) || !check_mnt(new_mnt)) 3792b12cea91SAl Viro goto out4; 37935ff9d8a6SEric W. Biederman if (new_mnt->mnt.mnt_flags & MNT_LOCKED) 37945ff9d8a6SEric W. Biederman goto out4; 37951da177e4SLinus Torvalds error = -ENOENT; 3796f3da392eSAlexey Dobriyan if (d_unlinked(new.dentry)) 3797b12cea91SAl Viro goto out4; 37981da177e4SLinus Torvalds error = -EBUSY; 379984d17192SAl Viro if (new_mnt == root_mnt || old_mnt == root_mnt) 3800b12cea91SAl Viro goto out4; /* loop, on the same file system */ 38011da177e4SLinus Torvalds error = -EINVAL; 38028c3ee42eSAl Viro if (root.mnt->mnt_root != root.dentry) 3803b12cea91SAl Viro goto out4; /* not a mountpoint */ 3804676da58dSAl Viro if (!mnt_has_parent(root_mnt)) 3805b12cea91SAl Viro goto out4; /* not attached */ 38062d8f3038SAl Viro if (new.mnt->mnt_root != new.dentry) 3807b12cea91SAl Viro goto out4; /* not a mountpoint */ 3808676da58dSAl Viro if (!mnt_has_parent(new_mnt)) 3809b12cea91SAl Viro goto out4; /* not attached */ 38104ac91378SJan Blunck /* make sure we can reach put_old from new_root */ 381184d17192SAl Viro if (!is_path_reachable(old_mnt, old.dentry, &new)) 3812b12cea91SAl Viro goto out4; 38130d082601SEric W. Biederman /* make certain new is below the root */ 38140d082601SEric W. Biederman if (!is_path_reachable(new_mnt, new.dentry, &root)) 38150d082601SEric W. Biederman goto out4; 3816719ea2fbSAl Viro lock_mount_hash(); 38172763d119SAl Viro umount_mnt(new_mnt); 38182763d119SAl Viro root_mp = unhash_mnt(root_mnt); /* we'll need its mountpoint */ 38195ff9d8a6SEric W. Biederman if (root_mnt->mnt.mnt_flags & MNT_LOCKED) { 38205ff9d8a6SEric W. Biederman new_mnt->mnt.mnt_flags |= MNT_LOCKED; 38215ff9d8a6SEric W. Biederman root_mnt->mnt.mnt_flags &= ~MNT_LOCKED; 38225ff9d8a6SEric W. Biederman } 38234ac91378SJan Blunck /* mount old root on put_old */ 382484d17192SAl Viro attach_mnt(root_mnt, old_mnt, old_mp); 38254ac91378SJan Blunck /* mount new_root on / */ 38262763d119SAl Viro attach_mnt(new_mnt, root_parent, root_mp); 38272763d119SAl Viro mnt_add_count(root_parent, -1); 38286b3286edSKirill Korotaev touch_mnt_namespace(current->nsproxy->mnt_ns); 38294fed655cSEric W. Biederman /* A moved mount should not expire automatically */ 38304fed655cSEric W. Biederman list_del_init(&new_mnt->mnt_expire); 38313895dbf8SEric W. Biederman put_mountpoint(root_mp); 3832719ea2fbSAl Viro unlock_mount_hash(); 38332d8f3038SAl Viro chroot_fs_refs(&root, &new); 38341da177e4SLinus Torvalds error = 0; 3835b12cea91SAl Viro out4: 383684d17192SAl Viro unlock_mount(old_mp); 38372763d119SAl Viro if (!error) 38382763d119SAl Viro mntput_no_expire(ex_parent); 3839b12cea91SAl Viro out3: 38408c3ee42eSAl Viro path_put(&root); 3841b12cea91SAl Viro out2: 38422d8f3038SAl Viro path_put(&old); 38431da177e4SLinus Torvalds out1: 38442d8f3038SAl Viro path_put(&new); 38451da177e4SLinus Torvalds out0: 38461da177e4SLinus Torvalds return error; 38471da177e4SLinus Torvalds } 38481da177e4SLinus Torvalds 38492a186721SChristian Brauner static unsigned int recalc_flags(struct mount_kattr *kattr, struct mount *mnt) 38502a186721SChristian Brauner { 38512a186721SChristian Brauner unsigned int flags = mnt->mnt.mnt_flags; 38522a186721SChristian Brauner 38532a186721SChristian Brauner /* flags to clear */ 38542a186721SChristian Brauner flags &= ~kattr->attr_clr; 38552a186721SChristian Brauner /* flags to raise */ 38562a186721SChristian Brauner flags |= kattr->attr_set; 38572a186721SChristian Brauner 38582a186721SChristian Brauner return flags; 38592a186721SChristian Brauner } 38602a186721SChristian Brauner 38619caccd41SChristian Brauner static int can_idmap_mount(const struct mount_kattr *kattr, struct mount *mnt) 38629caccd41SChristian Brauner { 38639caccd41SChristian Brauner struct vfsmount *m = &mnt->mnt; 38649caccd41SChristian Brauner 38659caccd41SChristian Brauner if (!kattr->mnt_userns) 38669caccd41SChristian Brauner return 0; 38679caccd41SChristian Brauner 38689caccd41SChristian Brauner /* 38699caccd41SChristian Brauner * Once a mount has been idmapped we don't allow it to change its 38709caccd41SChristian Brauner * mapping. It makes things simpler and callers can just create 38719caccd41SChristian Brauner * another bind-mount they can idmap if they want to. 38729caccd41SChristian Brauner */ 38739caccd41SChristian Brauner if (mnt_user_ns(m) != &init_user_ns) 38749caccd41SChristian Brauner return -EPERM; 38759caccd41SChristian Brauner 38769caccd41SChristian Brauner /* The underlying filesystem doesn't support idmapped mounts yet. */ 38779caccd41SChristian Brauner if (!(m->mnt_sb->s_type->fs_flags & FS_ALLOW_IDMAP)) 38789caccd41SChristian Brauner return -EINVAL; 38799caccd41SChristian Brauner 38802ca4dcc4SChristian Brauner /* Don't yet support filesystem mountable in user namespaces. */ 38812ca4dcc4SChristian Brauner if (m->mnt_sb->s_user_ns != &init_user_ns) 38822ca4dcc4SChristian Brauner return -EINVAL; 38832ca4dcc4SChristian Brauner 38849caccd41SChristian Brauner /* We're not controlling the superblock. */ 38852ca4dcc4SChristian Brauner if (!capable(CAP_SYS_ADMIN)) 38869caccd41SChristian Brauner return -EPERM; 38879caccd41SChristian Brauner 38889caccd41SChristian Brauner /* Mount has already been visible in the filesystem hierarchy. */ 38899caccd41SChristian Brauner if (!is_anon_ns(mnt->mnt_ns)) 38909caccd41SChristian Brauner return -EINVAL; 38919caccd41SChristian Brauner 38929caccd41SChristian Brauner return 0; 38939caccd41SChristian Brauner } 38949caccd41SChristian Brauner 38952a186721SChristian Brauner static struct mount *mount_setattr_prepare(struct mount_kattr *kattr, 38962a186721SChristian Brauner struct mount *mnt, int *err) 38972a186721SChristian Brauner { 38982a186721SChristian Brauner struct mount *m = mnt, *last = NULL; 38992a186721SChristian Brauner 39002a186721SChristian Brauner if (!is_mounted(&m->mnt)) { 39012a186721SChristian Brauner *err = -EINVAL; 39022a186721SChristian Brauner goto out; 39032a186721SChristian Brauner } 39042a186721SChristian Brauner 39052a186721SChristian Brauner if (!(mnt_has_parent(m) ? check_mnt(m) : is_anon_ns(m->mnt_ns))) { 39062a186721SChristian Brauner *err = -EINVAL; 39072a186721SChristian Brauner goto out; 39082a186721SChristian Brauner } 39092a186721SChristian Brauner 39102a186721SChristian Brauner do { 39112a186721SChristian Brauner unsigned int flags; 39122a186721SChristian Brauner 39132a186721SChristian Brauner flags = recalc_flags(kattr, m); 39142a186721SChristian Brauner if (!can_change_locked_flags(m, flags)) { 39152a186721SChristian Brauner *err = -EPERM; 39162a186721SChristian Brauner goto out; 39172a186721SChristian Brauner } 39182a186721SChristian Brauner 39199caccd41SChristian Brauner *err = can_idmap_mount(kattr, m); 39209caccd41SChristian Brauner if (*err) 39219caccd41SChristian Brauner goto out; 39229caccd41SChristian Brauner 39232a186721SChristian Brauner last = m; 39242a186721SChristian Brauner 39252a186721SChristian Brauner if ((kattr->attr_set & MNT_READONLY) && 39262a186721SChristian Brauner !(m->mnt.mnt_flags & MNT_READONLY)) { 39272a186721SChristian Brauner *err = mnt_hold_writers(m); 39282a186721SChristian Brauner if (*err) 39292a186721SChristian Brauner goto out; 39302a186721SChristian Brauner } 39312a186721SChristian Brauner } while (kattr->recurse && (m = next_mnt(m, mnt))); 39322a186721SChristian Brauner 39332a186721SChristian Brauner out: 39342a186721SChristian Brauner return last; 39352a186721SChristian Brauner } 39362a186721SChristian Brauner 39379caccd41SChristian Brauner static void do_idmap_mount(const struct mount_kattr *kattr, struct mount *mnt) 39389caccd41SChristian Brauner { 39399caccd41SChristian Brauner struct user_namespace *mnt_userns; 39409caccd41SChristian Brauner 39419caccd41SChristian Brauner if (!kattr->mnt_userns) 39429caccd41SChristian Brauner return; 39439caccd41SChristian Brauner 39449caccd41SChristian Brauner mnt_userns = get_user_ns(kattr->mnt_userns); 39459caccd41SChristian Brauner /* Pairs with smp_load_acquire() in mnt_user_ns(). */ 39469caccd41SChristian Brauner smp_store_release(&mnt->mnt.mnt_userns, mnt_userns); 39479caccd41SChristian Brauner } 39489caccd41SChristian Brauner 39492a186721SChristian Brauner static void mount_setattr_commit(struct mount_kattr *kattr, 39502a186721SChristian Brauner struct mount *mnt, struct mount *last, 39512a186721SChristian Brauner int err) 39522a186721SChristian Brauner { 39532a186721SChristian Brauner struct mount *m = mnt; 39542a186721SChristian Brauner 39552a186721SChristian Brauner do { 39562a186721SChristian Brauner if (!err) { 39572a186721SChristian Brauner unsigned int flags; 39582a186721SChristian Brauner 39599caccd41SChristian Brauner do_idmap_mount(kattr, m); 39602a186721SChristian Brauner flags = recalc_flags(kattr, m); 39612a186721SChristian Brauner WRITE_ONCE(m->mnt.mnt_flags, flags); 39622a186721SChristian Brauner } 39632a186721SChristian Brauner 39642a186721SChristian Brauner /* 39652a186721SChristian Brauner * We either set MNT_READONLY above so make it visible 39662a186721SChristian Brauner * before ~MNT_WRITE_HOLD or we failed to recursively 39672a186721SChristian Brauner * apply mount options. 39682a186721SChristian Brauner */ 39692a186721SChristian Brauner if ((kattr->attr_set & MNT_READONLY) && 39702a186721SChristian Brauner (m->mnt.mnt_flags & MNT_WRITE_HOLD)) 39712a186721SChristian Brauner mnt_unhold_writers(m); 39722a186721SChristian Brauner 39732a186721SChristian Brauner if (!err && kattr->propagation) 39742a186721SChristian Brauner change_mnt_propagation(m, kattr->propagation); 39752a186721SChristian Brauner 39762a186721SChristian Brauner /* 39772a186721SChristian Brauner * On failure, only cleanup until we found the first mount 39782a186721SChristian Brauner * we failed to handle. 39792a186721SChristian Brauner */ 39802a186721SChristian Brauner if (err && m == last) 39812a186721SChristian Brauner break; 39822a186721SChristian Brauner } while (kattr->recurse && (m = next_mnt(m, mnt))); 39832a186721SChristian Brauner 39842a186721SChristian Brauner if (!err) 39852a186721SChristian Brauner touch_mnt_namespace(mnt->mnt_ns); 39862a186721SChristian Brauner } 39872a186721SChristian Brauner 39882a186721SChristian Brauner static int do_mount_setattr(struct path *path, struct mount_kattr *kattr) 39892a186721SChristian Brauner { 39902a186721SChristian Brauner struct mount *mnt = real_mount(path->mnt), *last = NULL; 39912a186721SChristian Brauner int err = 0; 39922a186721SChristian Brauner 39932a186721SChristian Brauner if (path->dentry != mnt->mnt.mnt_root) 39942a186721SChristian Brauner return -EINVAL; 39952a186721SChristian Brauner 39962a186721SChristian Brauner if (kattr->propagation) { 39972a186721SChristian Brauner /* 39982a186721SChristian Brauner * Only take namespace_lock() if we're actually changing 39992a186721SChristian Brauner * propagation. 40002a186721SChristian Brauner */ 40012a186721SChristian Brauner namespace_lock(); 40022a186721SChristian Brauner if (kattr->propagation == MS_SHARED) { 40032a186721SChristian Brauner err = invent_group_ids(mnt, kattr->recurse); 40042a186721SChristian Brauner if (err) { 40052a186721SChristian Brauner namespace_unlock(); 40062a186721SChristian Brauner return err; 40072a186721SChristian Brauner } 40082a186721SChristian Brauner } 40092a186721SChristian Brauner } 40102a186721SChristian Brauner 40112a186721SChristian Brauner lock_mount_hash(); 40122a186721SChristian Brauner 40132a186721SChristian Brauner /* 40142a186721SChristian Brauner * Get the mount tree in a shape where we can change mount 40152a186721SChristian Brauner * properties without failure. 40162a186721SChristian Brauner */ 40172a186721SChristian Brauner last = mount_setattr_prepare(kattr, mnt, &err); 40182a186721SChristian Brauner if (last) /* Commit all changes or revert to the old state. */ 40192a186721SChristian Brauner mount_setattr_commit(kattr, mnt, last, err); 40202a186721SChristian Brauner 40212a186721SChristian Brauner unlock_mount_hash(); 40222a186721SChristian Brauner 40232a186721SChristian Brauner if (kattr->propagation) { 40242a186721SChristian Brauner namespace_unlock(); 40252a186721SChristian Brauner if (err) 40262a186721SChristian Brauner cleanup_group_ids(mnt, NULL); 40272a186721SChristian Brauner } 40282a186721SChristian Brauner 40292a186721SChristian Brauner return err; 40302a186721SChristian Brauner } 40312a186721SChristian Brauner 40329caccd41SChristian Brauner static int build_mount_idmapped(const struct mount_attr *attr, size_t usize, 40339caccd41SChristian Brauner struct mount_kattr *kattr, unsigned int flags) 40349caccd41SChristian Brauner { 40359caccd41SChristian Brauner int err = 0; 40369caccd41SChristian Brauner struct ns_common *ns; 40379caccd41SChristian Brauner struct user_namespace *mnt_userns; 40389caccd41SChristian Brauner struct file *file; 40399caccd41SChristian Brauner 40409caccd41SChristian Brauner if (!((attr->attr_set | attr->attr_clr) & MOUNT_ATTR_IDMAP)) 40419caccd41SChristian Brauner return 0; 40429caccd41SChristian Brauner 40439caccd41SChristian Brauner /* 40449caccd41SChristian Brauner * We currently do not support clearing an idmapped mount. If this ever 40459caccd41SChristian Brauner * is a use-case we can revisit this but for now let's keep it simple 40469caccd41SChristian Brauner * and not allow it. 40479caccd41SChristian Brauner */ 40489caccd41SChristian Brauner if (attr->attr_clr & MOUNT_ATTR_IDMAP) 40499caccd41SChristian Brauner return -EINVAL; 40509caccd41SChristian Brauner 40519caccd41SChristian Brauner if (attr->userns_fd > INT_MAX) 40529caccd41SChristian Brauner return -EINVAL; 40539caccd41SChristian Brauner 40549caccd41SChristian Brauner file = fget(attr->userns_fd); 40559caccd41SChristian Brauner if (!file) 40569caccd41SChristian Brauner return -EBADF; 40579caccd41SChristian Brauner 40589caccd41SChristian Brauner if (!proc_ns_file(file)) { 40599caccd41SChristian Brauner err = -EINVAL; 40609caccd41SChristian Brauner goto out_fput; 40619caccd41SChristian Brauner } 40629caccd41SChristian Brauner 40639caccd41SChristian Brauner ns = get_proc_ns(file_inode(file)); 40649caccd41SChristian Brauner if (ns->ops->type != CLONE_NEWUSER) { 40659caccd41SChristian Brauner err = -EINVAL; 40669caccd41SChristian Brauner goto out_fput; 40679caccd41SChristian Brauner } 40689caccd41SChristian Brauner 40699caccd41SChristian Brauner /* 40709caccd41SChristian Brauner * The init_user_ns is used to indicate that a vfsmount is not idmapped. 40719caccd41SChristian Brauner * This is simpler than just having to treat NULL as unmapped. Users 40729caccd41SChristian Brauner * wanting to idmap a mount to init_user_ns can just use a namespace 40739caccd41SChristian Brauner * with an identity mapping. 40749caccd41SChristian Brauner */ 40759caccd41SChristian Brauner mnt_userns = container_of(ns, struct user_namespace, ns); 40769caccd41SChristian Brauner if (mnt_userns == &init_user_ns) { 40779caccd41SChristian Brauner err = -EPERM; 40789caccd41SChristian Brauner goto out_fput; 40799caccd41SChristian Brauner } 40809caccd41SChristian Brauner kattr->mnt_userns = get_user_ns(mnt_userns); 40819caccd41SChristian Brauner 40829caccd41SChristian Brauner out_fput: 40839caccd41SChristian Brauner fput(file); 40849caccd41SChristian Brauner return err; 40859caccd41SChristian Brauner } 40869caccd41SChristian Brauner 40879caccd41SChristian Brauner static int build_mount_kattr(const struct mount_attr *attr, size_t usize, 40882a186721SChristian Brauner struct mount_kattr *kattr, unsigned int flags) 40892a186721SChristian Brauner { 40902a186721SChristian Brauner unsigned int lookup_flags = LOOKUP_AUTOMOUNT | LOOKUP_FOLLOW; 40912a186721SChristian Brauner 40922a186721SChristian Brauner if (flags & AT_NO_AUTOMOUNT) 40932a186721SChristian Brauner lookup_flags &= ~LOOKUP_AUTOMOUNT; 40942a186721SChristian Brauner if (flags & AT_SYMLINK_NOFOLLOW) 40952a186721SChristian Brauner lookup_flags &= ~LOOKUP_FOLLOW; 40962a186721SChristian Brauner if (flags & AT_EMPTY_PATH) 40972a186721SChristian Brauner lookup_flags |= LOOKUP_EMPTY; 40982a186721SChristian Brauner 40992a186721SChristian Brauner *kattr = (struct mount_kattr) { 41002a186721SChristian Brauner .lookup_flags = lookup_flags, 41012a186721SChristian Brauner .recurse = !!(flags & AT_RECURSIVE), 41022a186721SChristian Brauner }; 41032a186721SChristian Brauner 41042a186721SChristian Brauner if (attr->propagation & ~MOUNT_SETATTR_PROPAGATION_FLAGS) 41052a186721SChristian Brauner return -EINVAL; 41062a186721SChristian Brauner if (hweight32(attr->propagation & MOUNT_SETATTR_PROPAGATION_FLAGS) > 1) 41072a186721SChristian Brauner return -EINVAL; 41082a186721SChristian Brauner kattr->propagation = attr->propagation; 41092a186721SChristian Brauner 41102a186721SChristian Brauner if ((attr->attr_set | attr->attr_clr) & ~MOUNT_SETATTR_VALID_FLAGS) 41112a186721SChristian Brauner return -EINVAL; 41122a186721SChristian Brauner 41132a186721SChristian Brauner kattr->attr_set = attr_flags_to_mnt_flags(attr->attr_set); 41142a186721SChristian Brauner kattr->attr_clr = attr_flags_to_mnt_flags(attr->attr_clr); 41152a186721SChristian Brauner 41162a186721SChristian Brauner /* 41172a186721SChristian Brauner * Since the MOUNT_ATTR_<atime> values are an enum, not a bitmap, 41182a186721SChristian Brauner * users wanting to transition to a different atime setting cannot 41192a186721SChristian Brauner * simply specify the atime setting in @attr_set, but must also 41202a186721SChristian Brauner * specify MOUNT_ATTR__ATIME in the @attr_clr field. 41212a186721SChristian Brauner * So ensure that MOUNT_ATTR__ATIME can't be partially set in 41222a186721SChristian Brauner * @attr_clr and that @attr_set can't have any atime bits set if 41232a186721SChristian Brauner * MOUNT_ATTR__ATIME isn't set in @attr_clr. 41242a186721SChristian Brauner */ 41252a186721SChristian Brauner if (attr->attr_clr & MOUNT_ATTR__ATIME) { 41262a186721SChristian Brauner if ((attr->attr_clr & MOUNT_ATTR__ATIME) != MOUNT_ATTR__ATIME) 41272a186721SChristian Brauner return -EINVAL; 41282a186721SChristian Brauner 41292a186721SChristian Brauner /* 41302a186721SChristian Brauner * Clear all previous time settings as they are mutually 41312a186721SChristian Brauner * exclusive. 41322a186721SChristian Brauner */ 41332a186721SChristian Brauner kattr->attr_clr |= MNT_RELATIME | MNT_NOATIME; 41342a186721SChristian Brauner switch (attr->attr_set & MOUNT_ATTR__ATIME) { 41352a186721SChristian Brauner case MOUNT_ATTR_RELATIME: 41362a186721SChristian Brauner kattr->attr_set |= MNT_RELATIME; 41372a186721SChristian Brauner break; 41382a186721SChristian Brauner case MOUNT_ATTR_NOATIME: 41392a186721SChristian Brauner kattr->attr_set |= MNT_NOATIME; 41402a186721SChristian Brauner break; 41412a186721SChristian Brauner case MOUNT_ATTR_STRICTATIME: 41422a186721SChristian Brauner break; 41432a186721SChristian Brauner default: 41442a186721SChristian Brauner return -EINVAL; 41452a186721SChristian Brauner } 41462a186721SChristian Brauner } else { 41472a186721SChristian Brauner if (attr->attr_set & MOUNT_ATTR__ATIME) 41482a186721SChristian Brauner return -EINVAL; 41492a186721SChristian Brauner } 41502a186721SChristian Brauner 41519caccd41SChristian Brauner return build_mount_idmapped(attr, usize, kattr, flags); 41529caccd41SChristian Brauner } 41539caccd41SChristian Brauner 41549caccd41SChristian Brauner static void finish_mount_kattr(struct mount_kattr *kattr) 41559caccd41SChristian Brauner { 41569caccd41SChristian Brauner put_user_ns(kattr->mnt_userns); 41579caccd41SChristian Brauner kattr->mnt_userns = NULL; 41582a186721SChristian Brauner } 41592a186721SChristian Brauner 41602a186721SChristian Brauner SYSCALL_DEFINE5(mount_setattr, int, dfd, const char __user *, path, 41612a186721SChristian Brauner unsigned int, flags, struct mount_attr __user *, uattr, 41622a186721SChristian Brauner size_t, usize) 41632a186721SChristian Brauner { 41642a186721SChristian Brauner int err; 41652a186721SChristian Brauner struct path target; 41662a186721SChristian Brauner struct mount_attr attr; 41672a186721SChristian Brauner struct mount_kattr kattr; 41682a186721SChristian Brauner 41692a186721SChristian Brauner BUILD_BUG_ON(sizeof(struct mount_attr) != MOUNT_ATTR_SIZE_VER0); 41702a186721SChristian Brauner 41712a186721SChristian Brauner if (flags & ~(AT_EMPTY_PATH | 41722a186721SChristian Brauner AT_RECURSIVE | 41732a186721SChristian Brauner AT_SYMLINK_NOFOLLOW | 41742a186721SChristian Brauner AT_NO_AUTOMOUNT)) 41752a186721SChristian Brauner return -EINVAL; 41762a186721SChristian Brauner 41772a186721SChristian Brauner if (unlikely(usize > PAGE_SIZE)) 41782a186721SChristian Brauner return -E2BIG; 41792a186721SChristian Brauner if (unlikely(usize < MOUNT_ATTR_SIZE_VER0)) 41802a186721SChristian Brauner return -EINVAL; 41812a186721SChristian Brauner 41822a186721SChristian Brauner if (!may_mount()) 41832a186721SChristian Brauner return -EPERM; 41842a186721SChristian Brauner 41852a186721SChristian Brauner err = copy_struct_from_user(&attr, sizeof(attr), uattr, usize); 41862a186721SChristian Brauner if (err) 41872a186721SChristian Brauner return err; 41882a186721SChristian Brauner 41892a186721SChristian Brauner /* Don't bother walking through the mounts if this is a nop. */ 41902a186721SChristian Brauner if (attr.attr_set == 0 && 41912a186721SChristian Brauner attr.attr_clr == 0 && 41922a186721SChristian Brauner attr.propagation == 0) 41932a186721SChristian Brauner return 0; 41942a186721SChristian Brauner 41959caccd41SChristian Brauner err = build_mount_kattr(&attr, usize, &kattr, flags); 41962a186721SChristian Brauner if (err) 41972a186721SChristian Brauner return err; 41982a186721SChristian Brauner 41992a186721SChristian Brauner err = user_path_at(dfd, path, kattr.lookup_flags, &target); 42002a186721SChristian Brauner if (err) 42012a186721SChristian Brauner return err; 42022a186721SChristian Brauner 42032a186721SChristian Brauner err = do_mount_setattr(&target, &kattr); 42049caccd41SChristian Brauner finish_mount_kattr(&kattr); 42052a186721SChristian Brauner path_put(&target); 42062a186721SChristian Brauner return err; 42072a186721SChristian Brauner } 42082a186721SChristian Brauner 42091da177e4SLinus Torvalds static void __init init_mount_tree(void) 42101da177e4SLinus Torvalds { 42111da177e4SLinus Torvalds struct vfsmount *mnt; 421274e83122SAl Viro struct mount *m; 42136b3286edSKirill Korotaev struct mnt_namespace *ns; 4214ac748a09SJan Blunck struct path root; 42151da177e4SLinus Torvalds 4216fd3e007fSAl Viro mnt = vfs_kern_mount(&rootfs_fs_type, 0, "rootfs", NULL); 42171da177e4SLinus Torvalds if (IS_ERR(mnt)) 42181da177e4SLinus Torvalds panic("Can't create rootfs"); 4219b3e19d92SNick Piggin 422074e83122SAl Viro ns = alloc_mnt_ns(&init_user_ns, false); 42213b22edc5STrond Myklebust if (IS_ERR(ns)) 42221da177e4SLinus Torvalds panic("Can't allocate initial namespace"); 422374e83122SAl Viro m = real_mount(mnt); 422474e83122SAl Viro m->mnt_ns = ns; 422574e83122SAl Viro ns->root = m; 422674e83122SAl Viro ns->mounts = 1; 422774e83122SAl Viro list_add(&m->mnt_list, &ns->list); 42286b3286edSKirill Korotaev init_task.nsproxy->mnt_ns = ns; 42296b3286edSKirill Korotaev get_mnt_ns(ns); 42301da177e4SLinus Torvalds 4231be08d6d2SAl Viro root.mnt = mnt; 4232be08d6d2SAl Viro root.dentry = mnt->mnt_root; 4233da362b09SEric W. Biederman mnt->mnt_flags |= MNT_LOCKED; 4234ac748a09SJan Blunck 4235ac748a09SJan Blunck set_fs_pwd(current->fs, &root); 4236ac748a09SJan Blunck set_fs_root(current->fs, &root); 42371da177e4SLinus Torvalds } 42381da177e4SLinus Torvalds 423974bf17cfSDenis Cheng void __init mnt_init(void) 42401da177e4SLinus Torvalds { 424115a67dd8SRandy Dunlap int err; 42421da177e4SLinus Torvalds 42437d6fec45SAl Viro mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount), 424479f6540bSVasily Averin 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, NULL); 42451da177e4SLinus Torvalds 42460818bf27SAl Viro mount_hashtable = alloc_large_system_hash("Mount-cache", 424738129a13SAl Viro sizeof(struct hlist_head), 42480818bf27SAl Viro mhash_entries, 19, 42493d375d78SPavel Tatashin HASH_ZERO, 42500818bf27SAl Viro &m_hash_shift, &m_hash_mask, 0, 0); 42510818bf27SAl Viro mountpoint_hashtable = alloc_large_system_hash("Mountpoint-cache", 42520818bf27SAl Viro sizeof(struct hlist_head), 42530818bf27SAl Viro mphash_entries, 19, 42543d375d78SPavel Tatashin HASH_ZERO, 42550818bf27SAl Viro &mp_hash_shift, &mp_hash_mask, 0, 0); 42561da177e4SLinus Torvalds 425784d17192SAl Viro if (!mount_hashtable || !mountpoint_hashtable) 42581da177e4SLinus Torvalds panic("Failed to allocate mount hash table\n"); 42591da177e4SLinus Torvalds 42604b93dc9bSTejun Heo kernfs_init(); 42614b93dc9bSTejun Heo 426215a67dd8SRandy Dunlap err = sysfs_init(); 426315a67dd8SRandy Dunlap if (err) 426415a67dd8SRandy Dunlap printk(KERN_WARNING "%s: sysfs_init error: %d\n", 42658e24eea7SHarvey Harrison __func__, err); 426600d26666SGreg Kroah-Hartman fs_kobj = kobject_create_and_add("fs", NULL); 426700d26666SGreg Kroah-Hartman if (!fs_kobj) 42688e24eea7SHarvey Harrison printk(KERN_WARNING "%s: kobj create error\n", __func__); 4269037f11b4SAl Viro shmem_init(); 42701da177e4SLinus Torvalds init_rootfs(); 42711da177e4SLinus Torvalds init_mount_tree(); 42721da177e4SLinus Torvalds } 42731da177e4SLinus Torvalds 4274616511d0STrond Myklebust void put_mnt_ns(struct mnt_namespace *ns) 42751da177e4SLinus Torvalds { 42761a7b8969SKirill Tkhai if (!refcount_dec_and_test(&ns->ns.count)) 4277616511d0STrond Myklebust return; 42787b00ed6fSAl Viro drop_collected_mounts(&ns->root->mnt); 4279771b1371SEric W. Biederman free_mnt_ns(ns); 42801da177e4SLinus Torvalds } 42819d412a43SAl Viro 4282d911b458SDavid Howells struct vfsmount *kern_mount(struct file_system_type *type) 42839d412a43SAl Viro { 4284423e0ab0STim Chen struct vfsmount *mnt; 4285d911b458SDavid Howells mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, NULL); 4286423e0ab0STim Chen if (!IS_ERR(mnt)) { 4287423e0ab0STim Chen /* 4288423e0ab0STim Chen * it is a longterm mount, don't release mnt until 4289423e0ab0STim Chen * we unmount before file sys is unregistered 4290423e0ab0STim Chen */ 4291f7a99c5bSAl Viro real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL; 4292423e0ab0STim Chen } 4293423e0ab0STim Chen return mnt; 42949d412a43SAl Viro } 4295d911b458SDavid Howells EXPORT_SYMBOL_GPL(kern_mount); 4296423e0ab0STim Chen 4297423e0ab0STim Chen void kern_unmount(struct vfsmount *mnt) 4298423e0ab0STim Chen { 4299423e0ab0STim Chen /* release long term mount so mount point can be released */ 4300423e0ab0STim Chen if (!IS_ERR_OR_NULL(mnt)) { 4301f7a99c5bSAl Viro real_mount(mnt)->mnt_ns = NULL; 430248a066e7SAl Viro synchronize_rcu(); /* yecchhh... */ 4303423e0ab0STim Chen mntput(mnt); 4304423e0ab0STim Chen } 4305423e0ab0STim Chen } 4306423e0ab0STim Chen EXPORT_SYMBOL(kern_unmount); 430702125a82SAl Viro 4308df820f8dSMiklos Szeredi void kern_unmount_array(struct vfsmount *mnt[], unsigned int num) 4309df820f8dSMiklos Szeredi { 4310df820f8dSMiklos Szeredi unsigned int i; 4311df820f8dSMiklos Szeredi 4312df820f8dSMiklos Szeredi for (i = 0; i < num; i++) 4313df820f8dSMiklos Szeredi if (mnt[i]) 4314df820f8dSMiklos Szeredi real_mount(mnt[i])->mnt_ns = NULL; 4315df820f8dSMiklos Szeredi synchronize_rcu_expedited(); 4316df820f8dSMiklos Szeredi for (i = 0; i < num; i++) 4317df820f8dSMiklos Szeredi mntput(mnt[i]); 4318df820f8dSMiklos Szeredi } 4319df820f8dSMiklos Szeredi EXPORT_SYMBOL(kern_unmount_array); 4320df820f8dSMiklos Szeredi 432102125a82SAl Viro bool our_mnt(struct vfsmount *mnt) 432202125a82SAl Viro { 4323143c8c91SAl Viro return check_mnt(real_mount(mnt)); 432402125a82SAl Viro } 43258823c079SEric W. Biederman 43263151527eSEric W. Biederman bool current_chrooted(void) 43273151527eSEric W. Biederman { 43283151527eSEric W. Biederman /* Does the current process have a non-standard root */ 43293151527eSEric W. Biederman struct path ns_root; 43303151527eSEric W. Biederman struct path fs_root; 43313151527eSEric W. Biederman bool chrooted; 43323151527eSEric W. Biederman 43333151527eSEric W. Biederman /* Find the namespace root */ 43343151527eSEric W. Biederman ns_root.mnt = ¤t->nsproxy->mnt_ns->root->mnt; 43353151527eSEric W. Biederman ns_root.dentry = ns_root.mnt->mnt_root; 43363151527eSEric W. Biederman path_get(&ns_root); 43373151527eSEric W. Biederman while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root)) 43383151527eSEric W. Biederman ; 43393151527eSEric W. Biederman 43403151527eSEric W. Biederman get_fs_root(current->fs, &fs_root); 43413151527eSEric W. Biederman 43423151527eSEric W. Biederman chrooted = !path_equal(&fs_root, &ns_root); 43433151527eSEric W. Biederman 43443151527eSEric W. Biederman path_put(&fs_root); 43453151527eSEric W. Biederman path_put(&ns_root); 43463151527eSEric W. Biederman 43473151527eSEric W. Biederman return chrooted; 43483151527eSEric W. Biederman } 43493151527eSEric W. Biederman 4350132e4608SDavid Howells static bool mnt_already_visible(struct mnt_namespace *ns, 4351132e4608SDavid Howells const struct super_block *sb, 43528654df4eSEric W. Biederman int *new_mnt_flags) 435387a8ebd6SEric W. Biederman { 43548c6cf9ccSEric W. Biederman int new_flags = *new_mnt_flags; 435587a8ebd6SEric W. Biederman struct mount *mnt; 4356e51db735SEric W. Biederman bool visible = false; 435787a8ebd6SEric W. Biederman 435844bb4385SAl Viro down_read(&namespace_sem); 43599f6c61f9SMiklos Szeredi lock_ns_list(ns); 436087a8ebd6SEric W. Biederman list_for_each_entry(mnt, &ns->list, mnt_list) { 4361e51db735SEric W. Biederman struct mount *child; 436277b1a97dSEric W. Biederman int mnt_flags; 436377b1a97dSEric W. Biederman 43649f6c61f9SMiklos Szeredi if (mnt_is_cursor(mnt)) 43659f6c61f9SMiklos Szeredi continue; 43669f6c61f9SMiklos Szeredi 4367132e4608SDavid Howells if (mnt->mnt.mnt_sb->s_type != sb->s_type) 4368e51db735SEric W. Biederman continue; 4369e51db735SEric W. Biederman 43707e96c1b0SEric W. Biederman /* This mount is not fully visible if it's root directory 43717e96c1b0SEric W. Biederman * is not the root directory of the filesystem. 43727e96c1b0SEric W. Biederman */ 43737e96c1b0SEric W. Biederman if (mnt->mnt.mnt_root != mnt->mnt.mnt_sb->s_root) 43747e96c1b0SEric W. Biederman continue; 43757e96c1b0SEric W. Biederman 4376a1935c17SEric W. Biederman /* A local view of the mount flags */ 437777b1a97dSEric W. Biederman mnt_flags = mnt->mnt.mnt_flags; 437877b1a97dSEric W. Biederman 4379695e9df0SEric W. Biederman /* Don't miss readonly hidden in the superblock flags */ 4380bc98a42cSDavid Howells if (sb_rdonly(mnt->mnt.mnt_sb)) 4381695e9df0SEric W. Biederman mnt_flags |= MNT_LOCK_READONLY; 4382695e9df0SEric W. Biederman 43838c6cf9ccSEric W. Biederman /* Verify the mount flags are equal to or more permissive 43848c6cf9ccSEric W. Biederman * than the proposed new mount. 43858c6cf9ccSEric W. Biederman */ 438677b1a97dSEric W. Biederman if ((mnt_flags & MNT_LOCK_READONLY) && 43878c6cf9ccSEric W. Biederman !(new_flags & MNT_READONLY)) 43888c6cf9ccSEric W. Biederman continue; 438977b1a97dSEric W. Biederman if ((mnt_flags & MNT_LOCK_ATIME) && 439077b1a97dSEric W. Biederman ((mnt_flags & MNT_ATIME_MASK) != (new_flags & MNT_ATIME_MASK))) 43918c6cf9ccSEric W. Biederman continue; 43928c6cf9ccSEric W. Biederman 4393ceeb0e5dSEric W. Biederman /* This mount is not fully visible if there are any 4394ceeb0e5dSEric W. Biederman * locked child mounts that cover anything except for 4395ceeb0e5dSEric W. Biederman * empty directories. 4396e51db735SEric W. Biederman */ 4397e51db735SEric W. Biederman list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) { 4398e51db735SEric W. Biederman struct inode *inode = child->mnt_mountpoint->d_inode; 4399ceeb0e5dSEric W. Biederman /* Only worry about locked mounts */ 4400d71ed6c9SEric W. Biederman if (!(child->mnt.mnt_flags & MNT_LOCKED)) 4401ceeb0e5dSEric W. Biederman continue; 44027236c85eSEric W. Biederman /* Is the directory permanetly empty? */ 44037236c85eSEric W. Biederman if (!is_empty_dir_inode(inode)) 4404e51db735SEric W. Biederman goto next; 440587a8ebd6SEric W. Biederman } 44068c6cf9ccSEric W. Biederman /* Preserve the locked attributes */ 440777b1a97dSEric W. Biederman *new_mnt_flags |= mnt_flags & (MNT_LOCK_READONLY | \ 44088c6cf9ccSEric W. Biederman MNT_LOCK_ATIME); 4409e51db735SEric W. Biederman visible = true; 4410e51db735SEric W. Biederman goto found; 4411e51db735SEric W. Biederman next: ; 441287a8ebd6SEric W. Biederman } 4413e51db735SEric W. Biederman found: 44149f6c61f9SMiklos Szeredi unlock_ns_list(ns); 441544bb4385SAl Viro up_read(&namespace_sem); 4416e51db735SEric W. Biederman return visible; 441787a8ebd6SEric W. Biederman } 441887a8ebd6SEric W. Biederman 4419132e4608SDavid Howells static bool mount_too_revealing(const struct super_block *sb, int *new_mnt_flags) 44208654df4eSEric W. Biederman { 4421a1935c17SEric W. Biederman const unsigned long required_iflags = SB_I_NOEXEC | SB_I_NODEV; 44228654df4eSEric W. Biederman struct mnt_namespace *ns = current->nsproxy->mnt_ns; 44238654df4eSEric W. Biederman unsigned long s_iflags; 44248654df4eSEric W. Biederman 44258654df4eSEric W. Biederman if (ns->user_ns == &init_user_ns) 44268654df4eSEric W. Biederman return false; 44278654df4eSEric W. Biederman 44288654df4eSEric W. Biederman /* Can this filesystem be too revealing? */ 4429132e4608SDavid Howells s_iflags = sb->s_iflags; 44308654df4eSEric W. Biederman if (!(s_iflags & SB_I_USERNS_VISIBLE)) 44318654df4eSEric W. Biederman return false; 44328654df4eSEric W. Biederman 4433a1935c17SEric W. Biederman if ((s_iflags & required_iflags) != required_iflags) { 4434a1935c17SEric W. Biederman WARN_ONCE(1, "Expected s_iflags to contain 0x%lx\n", 4435a1935c17SEric W. Biederman required_iflags); 4436a1935c17SEric W. Biederman return true; 4437a1935c17SEric W. Biederman } 4438a1935c17SEric W. Biederman 4439132e4608SDavid Howells return !mnt_already_visible(ns, sb, new_mnt_flags); 44408654df4eSEric W. Biederman } 44418654df4eSEric W. Biederman 4442380cf5baSAndy Lutomirski bool mnt_may_suid(struct vfsmount *mnt) 4443380cf5baSAndy Lutomirski { 4444380cf5baSAndy Lutomirski /* 4445380cf5baSAndy Lutomirski * Foreign mounts (accessed via fchdir or through /proc 4446380cf5baSAndy Lutomirski * symlinks) are always treated as if they are nosuid. This 4447380cf5baSAndy Lutomirski * prevents namespaces from trusting potentially unsafe 4448380cf5baSAndy Lutomirski * suid/sgid bits, file caps, or security labels that originate 4449380cf5baSAndy Lutomirski * in other namespaces. 4450380cf5baSAndy Lutomirski */ 4451380cf5baSAndy Lutomirski return !(mnt->mnt_flags & MNT_NOSUID) && check_mnt(real_mount(mnt)) && 4452380cf5baSAndy Lutomirski current_in_userns(mnt->mnt_sb->s_user_ns); 4453380cf5baSAndy Lutomirski } 4454380cf5baSAndy Lutomirski 445564964528SAl Viro static struct ns_common *mntns_get(struct task_struct *task) 44568823c079SEric W. Biederman { 445758be2825SAl Viro struct ns_common *ns = NULL; 44588823c079SEric W. Biederman struct nsproxy *nsproxy; 44598823c079SEric W. Biederman 4460728dba3aSEric W. Biederman task_lock(task); 4461728dba3aSEric W. Biederman nsproxy = task->nsproxy; 44628823c079SEric W. Biederman if (nsproxy) { 446358be2825SAl Viro ns = &nsproxy->mnt_ns->ns; 446458be2825SAl Viro get_mnt_ns(to_mnt_ns(ns)); 44658823c079SEric W. Biederman } 4466728dba3aSEric W. Biederman task_unlock(task); 44678823c079SEric W. Biederman 44688823c079SEric W. Biederman return ns; 44698823c079SEric W. Biederman } 44708823c079SEric W. Biederman 447164964528SAl Viro static void mntns_put(struct ns_common *ns) 44728823c079SEric W. Biederman { 447358be2825SAl Viro put_mnt_ns(to_mnt_ns(ns)); 44748823c079SEric W. Biederman } 44758823c079SEric W. Biederman 4476f2a8d52eSChristian Brauner static int mntns_install(struct nsset *nsset, struct ns_common *ns) 44778823c079SEric W. Biederman { 4478f2a8d52eSChristian Brauner struct nsproxy *nsproxy = nsset->nsproxy; 4479f2a8d52eSChristian Brauner struct fs_struct *fs = nsset->fs; 44804f757f3cSAl Viro struct mnt_namespace *mnt_ns = to_mnt_ns(ns), *old_mnt_ns; 4481f2a8d52eSChristian Brauner struct user_namespace *user_ns = nsset->cred->user_ns; 44828823c079SEric W. Biederman struct path root; 44834f757f3cSAl Viro int err; 44848823c079SEric W. Biederman 44850c55cfc4SEric W. Biederman if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) || 4486f2a8d52eSChristian Brauner !ns_capable(user_ns, CAP_SYS_CHROOT) || 4487f2a8d52eSChristian Brauner !ns_capable(user_ns, CAP_SYS_ADMIN)) 4488ae11e0f1SZhao Hongjiang return -EPERM; 44898823c079SEric W. Biederman 449074e83122SAl Viro if (is_anon_ns(mnt_ns)) 449174e83122SAl Viro return -EINVAL; 449274e83122SAl Viro 44938823c079SEric W. Biederman if (fs->users != 1) 44948823c079SEric W. Biederman return -EINVAL; 44958823c079SEric W. Biederman 44968823c079SEric W. Biederman get_mnt_ns(mnt_ns); 44974f757f3cSAl Viro old_mnt_ns = nsproxy->mnt_ns; 44988823c079SEric W. Biederman nsproxy->mnt_ns = mnt_ns; 44998823c079SEric W. Biederman 45008823c079SEric W. Biederman /* Find the root */ 45014f757f3cSAl Viro err = vfs_path_lookup(mnt_ns->root->mnt.mnt_root, &mnt_ns->root->mnt, 45024f757f3cSAl Viro "/", LOOKUP_DOWN, &root); 45034f757f3cSAl Viro if (err) { 45044f757f3cSAl Viro /* revert to old namespace */ 45054f757f3cSAl Viro nsproxy->mnt_ns = old_mnt_ns; 45064f757f3cSAl Viro put_mnt_ns(mnt_ns); 45074f757f3cSAl Viro return err; 45084f757f3cSAl Viro } 45098823c079SEric W. Biederman 45104068367cSAndrei Vagin put_mnt_ns(old_mnt_ns); 45114068367cSAndrei Vagin 45128823c079SEric W. Biederman /* Update the pwd and root */ 45138823c079SEric W. Biederman set_fs_pwd(fs, &root); 45148823c079SEric W. Biederman set_fs_root(fs, &root); 45158823c079SEric W. Biederman 45168823c079SEric W. Biederman path_put(&root); 45178823c079SEric W. Biederman return 0; 45188823c079SEric W. Biederman } 45198823c079SEric W. Biederman 4520bcac25a5SAndrey Vagin static struct user_namespace *mntns_owner(struct ns_common *ns) 4521bcac25a5SAndrey Vagin { 4522bcac25a5SAndrey Vagin return to_mnt_ns(ns)->user_ns; 4523bcac25a5SAndrey Vagin } 4524bcac25a5SAndrey Vagin 45258823c079SEric W. Biederman const struct proc_ns_operations mntns_operations = { 45268823c079SEric W. Biederman .name = "mnt", 45278823c079SEric W. Biederman .type = CLONE_NEWNS, 45288823c079SEric W. Biederman .get = mntns_get, 45298823c079SEric W. Biederman .put = mntns_put, 45308823c079SEric W. Biederman .install = mntns_install, 4531bcac25a5SAndrey Vagin .owner = mntns_owner, 45328823c079SEric W. Biederman }; 4533