11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/fs/namespace.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * (C) Copyright Al Viro 2000, 2001 51da177e4SLinus Torvalds * Released under GPL v2. 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Based on code from fs/super.c, copyright Linus Torvalds and others. 81da177e4SLinus Torvalds * Heavily rewritten. 91da177e4SLinus Torvalds */ 101da177e4SLinus Torvalds 111da177e4SLinus Torvalds #include <linux/syscalls.h> 12d10577a8SAl Viro #include <linux/export.h> 1316f7e0feSRandy Dunlap #include <linux/capability.h> 146b3286edSKirill Korotaev #include <linux/mnt_namespace.h> 15771b1371SEric W. Biederman #include <linux/user_namespace.h> 161da177e4SLinus Torvalds #include <linux/namei.h> 171da177e4SLinus Torvalds #include <linux/security.h> 185b825c3aSIngo Molnar #include <linux/cred.h> 1973cd49ecSMiklos Szeredi #include <linux/idr.h> 2057f150a5SRob Landley #include <linux/init.h> /* init_rootfs */ 21d10577a8SAl Viro #include <linux/fs_struct.h> /* get_fs_root et.al. */ 22d10577a8SAl Viro #include <linux/fsnotify.h> /* fsnotify_vfsmount_delete */ 23a07b2000SAl Viro #include <linux/file.h> 24d10577a8SAl Viro #include <linux/uaccess.h> 250bb80f24SDavid Howells #include <linux/proc_ns.h> 2620b4fb48SLinus Torvalds #include <linux/magic.h> 2757c8a661SMike Rapoport #include <linux/memblock.h> 289ea459e1SAl Viro #include <linux/task_work.h> 299164bb4aSIngo Molnar #include <linux/sched/task.h> 30e262e32dSDavid Howells #include <uapi/linux/mount.h> 319bc61ab1SDavid Howells #include <linux/fs_context.h> 329164bb4aSIngo Molnar 3307b20889SRam Pai #include "pnode.h" 34948730b0SAdrian Bunk #include "internal.h" 351da177e4SLinus Torvalds 36d2921684SEric W. Biederman /* Maximum number of mounts in a mount namespace */ 37d2921684SEric W. Biederman unsigned int sysctl_mount_max __read_mostly = 100000; 38d2921684SEric W. Biederman 390818bf27SAl Viro static unsigned int m_hash_mask __read_mostly; 400818bf27SAl Viro static unsigned int m_hash_shift __read_mostly; 410818bf27SAl Viro static unsigned int mp_hash_mask __read_mostly; 420818bf27SAl Viro static unsigned int mp_hash_shift __read_mostly; 430818bf27SAl Viro 440818bf27SAl Viro static __initdata unsigned long mhash_entries; 450818bf27SAl Viro static int __init set_mhash_entries(char *str) 460818bf27SAl Viro { 470818bf27SAl Viro if (!str) 480818bf27SAl Viro return 0; 490818bf27SAl Viro mhash_entries = simple_strtoul(str, &str, 0); 500818bf27SAl Viro return 1; 510818bf27SAl Viro } 520818bf27SAl Viro __setup("mhash_entries=", set_mhash_entries); 530818bf27SAl Viro 540818bf27SAl Viro static __initdata unsigned long mphash_entries; 550818bf27SAl Viro static int __init set_mphash_entries(char *str) 560818bf27SAl Viro { 570818bf27SAl Viro if (!str) 580818bf27SAl Viro return 0; 590818bf27SAl Viro mphash_entries = simple_strtoul(str, &str, 0); 600818bf27SAl Viro return 1; 610818bf27SAl Viro } 620818bf27SAl Viro __setup("mphash_entries=", set_mphash_entries); 6313f14b4dSEric Dumazet 64c7999c36SAl Viro static u64 event; 6573cd49ecSMiklos Szeredi static DEFINE_IDA(mnt_id_ida); 66719f5d7fSMiklos Szeredi static DEFINE_IDA(mnt_group_ida); 675addc5ddSAl Viro 6838129a13SAl Viro static struct hlist_head *mount_hashtable __read_mostly; 690818bf27SAl Viro static struct hlist_head *mountpoint_hashtable __read_mostly; 70e18b890bSChristoph Lameter static struct kmem_cache *mnt_cache __read_mostly; 7159aa0da8SAl Viro static DECLARE_RWSEM(namespace_sem); 721da177e4SLinus Torvalds 73f87fd4c2SMiklos Szeredi /* /sys/fs */ 7400d26666SGreg Kroah-Hartman struct kobject *fs_kobj; 7500d26666SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(fs_kobj); 76f87fd4c2SMiklos Szeredi 7799b7db7bSNick Piggin /* 7899b7db7bSNick Piggin * vfsmount lock may be taken for read to prevent changes to the 7999b7db7bSNick Piggin * vfsmount hash, ie. during mountpoint lookups or walking back 8099b7db7bSNick Piggin * up the tree. 8199b7db7bSNick Piggin * 8299b7db7bSNick Piggin * It should be taken for write in all cases where the vfsmount 8399b7db7bSNick Piggin * tree or hash is modified or when a vfsmount structure is modified. 8499b7db7bSNick Piggin */ 8548a066e7SAl Viro __cacheline_aligned_in_smp DEFINE_SEQLOCK(mount_lock); 8699b7db7bSNick Piggin 8738129a13SAl Viro static inline struct hlist_head *m_hash(struct vfsmount *mnt, struct dentry *dentry) 881da177e4SLinus Torvalds { 891da177e4SLinus Torvalds unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES); 901da177e4SLinus Torvalds tmp += ((unsigned long)dentry / L1_CACHE_BYTES); 910818bf27SAl Viro tmp = tmp + (tmp >> m_hash_shift); 920818bf27SAl Viro return &mount_hashtable[tmp & m_hash_mask]; 930818bf27SAl Viro } 940818bf27SAl Viro 950818bf27SAl Viro static inline struct hlist_head *mp_hash(struct dentry *dentry) 960818bf27SAl Viro { 970818bf27SAl Viro unsigned long tmp = ((unsigned long)dentry / L1_CACHE_BYTES); 980818bf27SAl Viro tmp = tmp + (tmp >> mp_hash_shift); 990818bf27SAl Viro return &mountpoint_hashtable[tmp & mp_hash_mask]; 1001da177e4SLinus Torvalds } 1011da177e4SLinus Torvalds 102b105e270SAl Viro static int mnt_alloc_id(struct mount *mnt) 10373cd49ecSMiklos Szeredi { 104169b480eSMatthew Wilcox int res = ida_alloc(&mnt_id_ida, GFP_KERNEL); 10573cd49ecSMiklos Szeredi 106169b480eSMatthew Wilcox if (res < 0) 10773cd49ecSMiklos Szeredi return res; 108169b480eSMatthew Wilcox mnt->mnt_id = res; 109169b480eSMatthew Wilcox return 0; 11073cd49ecSMiklos Szeredi } 11173cd49ecSMiklos Szeredi 112b105e270SAl Viro static void mnt_free_id(struct mount *mnt) 11373cd49ecSMiklos Szeredi { 114169b480eSMatthew Wilcox ida_free(&mnt_id_ida, mnt->mnt_id); 11573cd49ecSMiklos Szeredi } 11673cd49ecSMiklos Szeredi 117719f5d7fSMiklos Szeredi /* 118719f5d7fSMiklos Szeredi * Allocate a new peer group ID 119719f5d7fSMiklos Szeredi */ 1204b8b21f4SAl Viro static int mnt_alloc_group_id(struct mount *mnt) 121719f5d7fSMiklos Szeredi { 122169b480eSMatthew Wilcox int res = ida_alloc_min(&mnt_group_ida, 1, GFP_KERNEL); 123f21f6220SAl Viro 124169b480eSMatthew Wilcox if (res < 0) 125f21f6220SAl Viro return res; 126169b480eSMatthew Wilcox mnt->mnt_group_id = res; 127169b480eSMatthew Wilcox return 0; 128719f5d7fSMiklos Szeredi } 129719f5d7fSMiklos Szeredi 130719f5d7fSMiklos Szeredi /* 131719f5d7fSMiklos Szeredi * Release a peer group ID 132719f5d7fSMiklos Szeredi */ 1334b8b21f4SAl Viro void mnt_release_group_id(struct mount *mnt) 134719f5d7fSMiklos Szeredi { 135169b480eSMatthew Wilcox ida_free(&mnt_group_ida, mnt->mnt_group_id); 13615169fe7SAl Viro mnt->mnt_group_id = 0; 137719f5d7fSMiklos Szeredi } 138719f5d7fSMiklos Szeredi 139b3e19d92SNick Piggin /* 140b3e19d92SNick Piggin * vfsmount lock must be held for read 141b3e19d92SNick Piggin */ 14283adc753SAl Viro static inline void mnt_add_count(struct mount *mnt, int n) 143b3e19d92SNick Piggin { 144b3e19d92SNick Piggin #ifdef CONFIG_SMP 14568e8a9feSAl Viro this_cpu_add(mnt->mnt_pcp->mnt_count, n); 146b3e19d92SNick Piggin #else 147b3e19d92SNick Piggin preempt_disable(); 14868e8a9feSAl Viro mnt->mnt_count += n; 149b3e19d92SNick Piggin preempt_enable(); 150b3e19d92SNick Piggin #endif 151b3e19d92SNick Piggin } 152b3e19d92SNick Piggin 153b3e19d92SNick Piggin /* 154b3e19d92SNick Piggin * vfsmount lock must be held for write 155b3e19d92SNick Piggin */ 15683adc753SAl Viro unsigned int mnt_get_count(struct mount *mnt) 157b3e19d92SNick Piggin { 158b3e19d92SNick Piggin #ifdef CONFIG_SMP 159f03c6599SAl Viro unsigned int count = 0; 160b3e19d92SNick Piggin int cpu; 161b3e19d92SNick Piggin 162b3e19d92SNick Piggin for_each_possible_cpu(cpu) { 16368e8a9feSAl Viro count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count; 164b3e19d92SNick Piggin } 165b3e19d92SNick Piggin 166b3e19d92SNick Piggin return count; 167b3e19d92SNick Piggin #else 16868e8a9feSAl Viro return mnt->mnt_count; 169b3e19d92SNick Piggin #endif 170b3e19d92SNick Piggin } 171b3e19d92SNick Piggin 17287b95ce0SAl Viro static void drop_mountpoint(struct fs_pin *p) 17387b95ce0SAl Viro { 17487b95ce0SAl Viro struct mount *m = container_of(p, struct mount, mnt_umount); 17587b95ce0SAl Viro dput(m->mnt_ex_mountpoint); 17687b95ce0SAl Viro pin_remove(p); 17787b95ce0SAl Viro mntput(&m->mnt); 17887b95ce0SAl Viro } 17987b95ce0SAl Viro 180b105e270SAl Viro static struct mount *alloc_vfsmnt(const char *name) 1811da177e4SLinus Torvalds { 182c63181e6SAl Viro struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL); 183c63181e6SAl Viro if (mnt) { 18473cd49ecSMiklos Szeredi int err; 18573cd49ecSMiklos Szeredi 186c63181e6SAl Viro err = mnt_alloc_id(mnt); 18788b38782SLi Zefan if (err) 18888b38782SLi Zefan goto out_free_cache; 18988b38782SLi Zefan 19088b38782SLi Zefan if (name) { 191fcc139aeSAndrzej Hajda mnt->mnt_devname = kstrdup_const(name, GFP_KERNEL); 192c63181e6SAl Viro if (!mnt->mnt_devname) 19388b38782SLi Zefan goto out_free_id; 19473cd49ecSMiklos Szeredi } 19573cd49ecSMiklos Szeredi 196b3e19d92SNick Piggin #ifdef CONFIG_SMP 197c63181e6SAl Viro mnt->mnt_pcp = alloc_percpu(struct mnt_pcp); 198c63181e6SAl Viro if (!mnt->mnt_pcp) 199b3e19d92SNick Piggin goto out_free_devname; 200b3e19d92SNick Piggin 201c63181e6SAl Viro this_cpu_add(mnt->mnt_pcp->mnt_count, 1); 202b3e19d92SNick Piggin #else 203c63181e6SAl Viro mnt->mnt_count = 1; 204c63181e6SAl Viro mnt->mnt_writers = 0; 205b3e19d92SNick Piggin #endif 206b3e19d92SNick Piggin 20738129a13SAl Viro INIT_HLIST_NODE(&mnt->mnt_hash); 208c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_child); 209c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_mounts); 210c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_list); 211c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_expire); 212c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_share); 213c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_slave_list); 214c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_slave); 2150a5eb7c8SEric W. Biederman INIT_HLIST_NODE(&mnt->mnt_mp_list); 21699b19d16SEric W. Biederman INIT_LIST_HEAD(&mnt->mnt_umounting); 21787b95ce0SAl Viro init_fs_pin(&mnt->mnt_umount, drop_mountpoint); 2181da177e4SLinus Torvalds } 219c63181e6SAl Viro return mnt; 22088b38782SLi Zefan 221d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 222d3ef3d73Snpiggin@suse.de out_free_devname: 223fcc139aeSAndrzej Hajda kfree_const(mnt->mnt_devname); 224d3ef3d73Snpiggin@suse.de #endif 22588b38782SLi Zefan out_free_id: 226c63181e6SAl Viro mnt_free_id(mnt); 22788b38782SLi Zefan out_free_cache: 228c63181e6SAl Viro kmem_cache_free(mnt_cache, mnt); 22988b38782SLi Zefan return NULL; 2301da177e4SLinus Torvalds } 2311da177e4SLinus Torvalds 2328366025eSDave Hansen /* 2338366025eSDave Hansen * Most r/o checks on a fs are for operations that take 2348366025eSDave Hansen * discrete amounts of time, like a write() or unlink(). 2358366025eSDave Hansen * We must keep track of when those operations start 2368366025eSDave Hansen * (for permission checks) and when they end, so that 2378366025eSDave Hansen * we can determine when writes are able to occur to 2388366025eSDave Hansen * a filesystem. 2398366025eSDave Hansen */ 2403d733633SDave Hansen /* 2413d733633SDave Hansen * __mnt_is_readonly: check whether a mount is read-only 2423d733633SDave Hansen * @mnt: the mount to check for its write status 2433d733633SDave Hansen * 2443d733633SDave Hansen * This shouldn't be used directly ouside of the VFS. 2453d733633SDave Hansen * It does not guarantee that the filesystem will stay 2463d733633SDave Hansen * r/w, just that it is right *now*. This can not and 2473d733633SDave Hansen * should not be used in place of IS_RDONLY(inode). 2483d733633SDave Hansen * mnt_want/drop_write() will _keep_ the filesystem 2493d733633SDave Hansen * r/w. 2503d733633SDave Hansen */ 25143f5e655SDavid Howells bool __mnt_is_readonly(struct vfsmount *mnt) 2523d733633SDave Hansen { 25343f5e655SDavid Howells return (mnt->mnt_flags & MNT_READONLY) || sb_rdonly(mnt->mnt_sb); 2543d733633SDave Hansen } 2553d733633SDave Hansen EXPORT_SYMBOL_GPL(__mnt_is_readonly); 2563d733633SDave Hansen 25783adc753SAl Viro static inline void mnt_inc_writers(struct mount *mnt) 2583d733633SDave Hansen { 259d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 26068e8a9feSAl Viro this_cpu_inc(mnt->mnt_pcp->mnt_writers); 261d3ef3d73Snpiggin@suse.de #else 26268e8a9feSAl Viro mnt->mnt_writers++; 263d3ef3d73Snpiggin@suse.de #endif 2643d733633SDave Hansen } 2653d733633SDave Hansen 26683adc753SAl Viro static inline void mnt_dec_writers(struct mount *mnt) 2673d733633SDave Hansen { 268d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 26968e8a9feSAl Viro this_cpu_dec(mnt->mnt_pcp->mnt_writers); 270d3ef3d73Snpiggin@suse.de #else 27168e8a9feSAl Viro mnt->mnt_writers--; 272d3ef3d73Snpiggin@suse.de #endif 273d3ef3d73Snpiggin@suse.de } 274d3ef3d73Snpiggin@suse.de 27583adc753SAl Viro static unsigned int mnt_get_writers(struct mount *mnt) 276d3ef3d73Snpiggin@suse.de { 277d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 278d3ef3d73Snpiggin@suse.de unsigned int count = 0; 2793d733633SDave Hansen int cpu; 2803d733633SDave Hansen 2813d733633SDave Hansen for_each_possible_cpu(cpu) { 28268e8a9feSAl Viro count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers; 2833d733633SDave Hansen } 2843d733633SDave Hansen 285d3ef3d73Snpiggin@suse.de return count; 286d3ef3d73Snpiggin@suse.de #else 287d3ef3d73Snpiggin@suse.de return mnt->mnt_writers; 288d3ef3d73Snpiggin@suse.de #endif 2893d733633SDave Hansen } 2903d733633SDave Hansen 2914ed5e82fSMiklos Szeredi static int mnt_is_readonly(struct vfsmount *mnt) 2924ed5e82fSMiklos Szeredi { 2934ed5e82fSMiklos Szeredi if (mnt->mnt_sb->s_readonly_remount) 2944ed5e82fSMiklos Szeredi return 1; 2954ed5e82fSMiklos Szeredi /* Order wrt setting s_flags/s_readonly_remount in do_remount() */ 2964ed5e82fSMiklos Szeredi smp_rmb(); 2974ed5e82fSMiklos Szeredi return __mnt_is_readonly(mnt); 2984ed5e82fSMiklos Szeredi } 2994ed5e82fSMiklos Szeredi 3003d733633SDave Hansen /* 301eb04c282SJan Kara * Most r/o & frozen checks on a fs are for operations that take discrete 302eb04c282SJan Kara * amounts of time, like a write() or unlink(). We must keep track of when 303eb04c282SJan Kara * those operations start (for permission checks) and when they end, so that we 304eb04c282SJan Kara * can determine when writes are able to occur to a filesystem. 3053d733633SDave Hansen */ 3068366025eSDave Hansen /** 307eb04c282SJan Kara * __mnt_want_write - get write access to a mount without freeze protection 30883adc753SAl Viro * @m: the mount on which to take a write 3098366025eSDave Hansen * 310eb04c282SJan Kara * This tells the low-level filesystem that a write is about to be performed to 311eb04c282SJan Kara * it, and makes sure that writes are allowed (mnt it read-write) before 312eb04c282SJan Kara * returning success. This operation does not protect against filesystem being 313eb04c282SJan Kara * frozen. When the write operation is finished, __mnt_drop_write() must be 314eb04c282SJan Kara * called. This is effectively a refcount. 3158366025eSDave Hansen */ 316eb04c282SJan Kara int __mnt_want_write(struct vfsmount *m) 3178366025eSDave Hansen { 31883adc753SAl Viro struct mount *mnt = real_mount(m); 3193d733633SDave Hansen int ret = 0; 3203d733633SDave Hansen 321d3ef3d73Snpiggin@suse.de preempt_disable(); 322c6653a83SNick Piggin mnt_inc_writers(mnt); 323d3ef3d73Snpiggin@suse.de /* 324c6653a83SNick Piggin * The store to mnt_inc_writers must be visible before we pass 325d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD loop below, so that the slowpath can see our 326d3ef3d73Snpiggin@suse.de * incremented count after it has set MNT_WRITE_HOLD. 327d3ef3d73Snpiggin@suse.de */ 328d3ef3d73Snpiggin@suse.de smp_mb(); 3296aa7de05SMark Rutland while (READ_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD) 330d3ef3d73Snpiggin@suse.de cpu_relax(); 331d3ef3d73Snpiggin@suse.de /* 332d3ef3d73Snpiggin@suse.de * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will 333d3ef3d73Snpiggin@suse.de * be set to match its requirements. So we must not load that until 334d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD is cleared. 335d3ef3d73Snpiggin@suse.de */ 336d3ef3d73Snpiggin@suse.de smp_rmb(); 3374ed5e82fSMiklos Szeredi if (mnt_is_readonly(m)) { 338c6653a83SNick Piggin mnt_dec_writers(mnt); 3393d733633SDave Hansen ret = -EROFS; 3403d733633SDave Hansen } 341d3ef3d73Snpiggin@suse.de preempt_enable(); 342eb04c282SJan Kara 343eb04c282SJan Kara return ret; 344eb04c282SJan Kara } 345eb04c282SJan Kara 346eb04c282SJan Kara /** 347eb04c282SJan Kara * mnt_want_write - get write access to a mount 348eb04c282SJan Kara * @m: the mount on which to take a write 349eb04c282SJan Kara * 350eb04c282SJan Kara * This tells the low-level filesystem that a write is about to be performed to 351eb04c282SJan Kara * it, and makes sure that writes are allowed (mount is read-write, filesystem 352eb04c282SJan Kara * is not frozen) before returning success. When the write operation is 353eb04c282SJan Kara * finished, mnt_drop_write() must be called. This is effectively a refcount. 354eb04c282SJan Kara */ 355eb04c282SJan Kara int mnt_want_write(struct vfsmount *m) 356eb04c282SJan Kara { 357eb04c282SJan Kara int ret; 358eb04c282SJan Kara 359eb04c282SJan Kara sb_start_write(m->mnt_sb); 360eb04c282SJan Kara ret = __mnt_want_write(m); 361eb04c282SJan Kara if (ret) 362eb04c282SJan Kara sb_end_write(m->mnt_sb); 3633d733633SDave Hansen return ret; 3648366025eSDave Hansen } 3658366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_want_write); 3668366025eSDave Hansen 3678366025eSDave Hansen /** 36896029c4eSnpiggin@suse.de * mnt_clone_write - get write access to a mount 36996029c4eSnpiggin@suse.de * @mnt: the mount on which to take a write 37096029c4eSnpiggin@suse.de * 37196029c4eSnpiggin@suse.de * This is effectively like mnt_want_write, except 37296029c4eSnpiggin@suse.de * it must only be used to take an extra write reference 37396029c4eSnpiggin@suse.de * on a mountpoint that we already know has a write reference 37496029c4eSnpiggin@suse.de * on it. This allows some optimisation. 37596029c4eSnpiggin@suse.de * 37696029c4eSnpiggin@suse.de * After finished, mnt_drop_write must be called as usual to 37796029c4eSnpiggin@suse.de * drop the reference. 37896029c4eSnpiggin@suse.de */ 37996029c4eSnpiggin@suse.de int mnt_clone_write(struct vfsmount *mnt) 38096029c4eSnpiggin@suse.de { 38196029c4eSnpiggin@suse.de /* superblock may be r/o */ 38296029c4eSnpiggin@suse.de if (__mnt_is_readonly(mnt)) 38396029c4eSnpiggin@suse.de return -EROFS; 38496029c4eSnpiggin@suse.de preempt_disable(); 38583adc753SAl Viro mnt_inc_writers(real_mount(mnt)); 38696029c4eSnpiggin@suse.de preempt_enable(); 38796029c4eSnpiggin@suse.de return 0; 38896029c4eSnpiggin@suse.de } 38996029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_clone_write); 39096029c4eSnpiggin@suse.de 39196029c4eSnpiggin@suse.de /** 392eb04c282SJan Kara * __mnt_want_write_file - get write access to a file's mount 393eb04c282SJan Kara * @file: the file who's mount on which to take a write 394eb04c282SJan Kara * 395eb04c282SJan Kara * This is like __mnt_want_write, but it takes a file and can 396eb04c282SJan Kara * do some optimisations if the file is open for write already 397eb04c282SJan Kara */ 398eb04c282SJan Kara int __mnt_want_write_file(struct file *file) 399eb04c282SJan Kara { 40083f936c7SAl Viro if (!(file->f_mode & FMODE_WRITER)) 401eb04c282SJan Kara return __mnt_want_write(file->f_path.mnt); 402eb04c282SJan Kara else 403eb04c282SJan Kara return mnt_clone_write(file->f_path.mnt); 404eb04c282SJan Kara } 405eb04c282SJan Kara 406eb04c282SJan Kara /** 4077c6893e3SMiklos Szeredi * mnt_want_write_file - get write access to a file's mount 4087c6893e3SMiklos Szeredi * @file: the file who's mount on which to take a write 4097c6893e3SMiklos Szeredi * 4107c6893e3SMiklos Szeredi * This is like mnt_want_write, but it takes a file and can 4117c6893e3SMiklos Szeredi * do some optimisations if the file is open for write already 4127c6893e3SMiklos Szeredi */ 4137c6893e3SMiklos Szeredi int mnt_want_write_file(struct file *file) 4147c6893e3SMiklos Szeredi { 4157c6893e3SMiklos Szeredi int ret; 4167c6893e3SMiklos Szeredi 4177c6893e3SMiklos Szeredi sb_start_write(file_inode(file)->i_sb); 4187c6893e3SMiklos Szeredi ret = __mnt_want_write_file(file); 4197c6893e3SMiklos Szeredi if (ret) 4207c6893e3SMiklos Szeredi sb_end_write(file_inode(file)->i_sb); 4217c6893e3SMiklos Szeredi return ret; 4227c6893e3SMiklos Szeredi } 42396029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_want_write_file); 42496029c4eSnpiggin@suse.de 42596029c4eSnpiggin@suse.de /** 426eb04c282SJan Kara * __mnt_drop_write - give up write access to a mount 4278366025eSDave Hansen * @mnt: the mount on which to give up write access 4288366025eSDave Hansen * 4298366025eSDave Hansen * Tells the low-level filesystem that we are done 4308366025eSDave Hansen * performing writes to it. Must be matched with 431eb04c282SJan Kara * __mnt_want_write() call above. 4328366025eSDave Hansen */ 433eb04c282SJan Kara void __mnt_drop_write(struct vfsmount *mnt) 4348366025eSDave Hansen { 435d3ef3d73Snpiggin@suse.de preempt_disable(); 43683adc753SAl Viro mnt_dec_writers(real_mount(mnt)); 437d3ef3d73Snpiggin@suse.de preempt_enable(); 4388366025eSDave Hansen } 439eb04c282SJan Kara 440eb04c282SJan Kara /** 441eb04c282SJan Kara * mnt_drop_write - give up write access to a mount 442eb04c282SJan Kara * @mnt: the mount on which to give up write access 443eb04c282SJan Kara * 444eb04c282SJan Kara * Tells the low-level filesystem that we are done performing writes to it and 445eb04c282SJan Kara * also allows filesystem to be frozen again. Must be matched with 446eb04c282SJan Kara * mnt_want_write() call above. 447eb04c282SJan Kara */ 448eb04c282SJan Kara void mnt_drop_write(struct vfsmount *mnt) 449eb04c282SJan Kara { 450eb04c282SJan Kara __mnt_drop_write(mnt); 451eb04c282SJan Kara sb_end_write(mnt->mnt_sb); 452eb04c282SJan Kara } 4538366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_drop_write); 4548366025eSDave Hansen 455eb04c282SJan Kara void __mnt_drop_write_file(struct file *file) 456eb04c282SJan Kara { 457eb04c282SJan Kara __mnt_drop_write(file->f_path.mnt); 458eb04c282SJan Kara } 459eb04c282SJan Kara 4607c6893e3SMiklos Szeredi void mnt_drop_write_file(struct file *file) 4617c6893e3SMiklos Szeredi { 462a6795a58SMiklos Szeredi __mnt_drop_write_file(file); 4637c6893e3SMiklos Szeredi sb_end_write(file_inode(file)->i_sb); 4647c6893e3SMiklos Szeredi } 4652a79f17eSAl Viro EXPORT_SYMBOL(mnt_drop_write_file); 4662a79f17eSAl Viro 46783adc753SAl Viro static int mnt_make_readonly(struct mount *mnt) 4688366025eSDave Hansen { 4693d733633SDave Hansen int ret = 0; 4703d733633SDave Hansen 471719ea2fbSAl Viro lock_mount_hash(); 47283adc753SAl Viro mnt->mnt.mnt_flags |= MNT_WRITE_HOLD; 473d3ef3d73Snpiggin@suse.de /* 474d3ef3d73Snpiggin@suse.de * After storing MNT_WRITE_HOLD, we'll read the counters. This store 475d3ef3d73Snpiggin@suse.de * should be visible before we do. 476d3ef3d73Snpiggin@suse.de */ 477d3ef3d73Snpiggin@suse.de smp_mb(); 478d3ef3d73Snpiggin@suse.de 479d3ef3d73Snpiggin@suse.de /* 480d3ef3d73Snpiggin@suse.de * With writers on hold, if this value is zero, then there are 481d3ef3d73Snpiggin@suse.de * definitely no active writers (although held writers may subsequently 482d3ef3d73Snpiggin@suse.de * increment the count, they'll have to wait, and decrement it after 483d3ef3d73Snpiggin@suse.de * seeing MNT_READONLY). 484d3ef3d73Snpiggin@suse.de * 485d3ef3d73Snpiggin@suse.de * It is OK to have counter incremented on one CPU and decremented on 486d3ef3d73Snpiggin@suse.de * another: the sum will add up correctly. The danger would be when we 487d3ef3d73Snpiggin@suse.de * sum up each counter, if we read a counter before it is incremented, 488d3ef3d73Snpiggin@suse.de * but then read another CPU's count which it has been subsequently 489d3ef3d73Snpiggin@suse.de * decremented from -- we would see more decrements than we should. 490d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD protects against this scenario, because 491d3ef3d73Snpiggin@suse.de * mnt_want_write first increments count, then smp_mb, then spins on 492d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD, so it can't be decremented by another CPU while 493d3ef3d73Snpiggin@suse.de * we're counting up here. 494d3ef3d73Snpiggin@suse.de */ 495c6653a83SNick Piggin if (mnt_get_writers(mnt) > 0) 496d3ef3d73Snpiggin@suse.de ret = -EBUSY; 497d3ef3d73Snpiggin@suse.de else 49883adc753SAl Viro mnt->mnt.mnt_flags |= MNT_READONLY; 499d3ef3d73Snpiggin@suse.de /* 500d3ef3d73Snpiggin@suse.de * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers 501d3ef3d73Snpiggin@suse.de * that become unheld will see MNT_READONLY. 502d3ef3d73Snpiggin@suse.de */ 503d3ef3d73Snpiggin@suse.de smp_wmb(); 50483adc753SAl Viro mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD; 505719ea2fbSAl Viro unlock_mount_hash(); 5063d733633SDave Hansen return ret; 5073d733633SDave Hansen } 5088366025eSDave Hansen 50943f5e655SDavid Howells static int __mnt_unmake_readonly(struct mount *mnt) 5102e4b7fcdSDave Hansen { 511719ea2fbSAl Viro lock_mount_hash(); 51283adc753SAl Viro mnt->mnt.mnt_flags &= ~MNT_READONLY; 513719ea2fbSAl Viro unlock_mount_hash(); 51443f5e655SDavid Howells return 0; 5152e4b7fcdSDave Hansen } 5162e4b7fcdSDave Hansen 5174ed5e82fSMiklos Szeredi int sb_prepare_remount_readonly(struct super_block *sb) 5184ed5e82fSMiklos Szeredi { 5194ed5e82fSMiklos Szeredi struct mount *mnt; 5204ed5e82fSMiklos Szeredi int err = 0; 5214ed5e82fSMiklos Szeredi 5228e8b8796SMiklos Szeredi /* Racy optimization. Recheck the counter under MNT_WRITE_HOLD */ 5238e8b8796SMiklos Szeredi if (atomic_long_read(&sb->s_remove_count)) 5248e8b8796SMiklos Szeredi return -EBUSY; 5258e8b8796SMiklos Szeredi 526719ea2fbSAl Viro lock_mount_hash(); 5274ed5e82fSMiklos Szeredi list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) { 5284ed5e82fSMiklos Szeredi if (!(mnt->mnt.mnt_flags & MNT_READONLY)) { 5294ed5e82fSMiklos Szeredi mnt->mnt.mnt_flags |= MNT_WRITE_HOLD; 5304ed5e82fSMiklos Szeredi smp_mb(); 5314ed5e82fSMiklos Szeredi if (mnt_get_writers(mnt) > 0) { 5324ed5e82fSMiklos Szeredi err = -EBUSY; 5334ed5e82fSMiklos Szeredi break; 5344ed5e82fSMiklos Szeredi } 5354ed5e82fSMiklos Szeredi } 5364ed5e82fSMiklos Szeredi } 5378e8b8796SMiklos Szeredi if (!err && atomic_long_read(&sb->s_remove_count)) 5388e8b8796SMiklos Szeredi err = -EBUSY; 5398e8b8796SMiklos Szeredi 5404ed5e82fSMiklos Szeredi if (!err) { 5414ed5e82fSMiklos Szeredi sb->s_readonly_remount = 1; 5424ed5e82fSMiklos Szeredi smp_wmb(); 5434ed5e82fSMiklos Szeredi } 5444ed5e82fSMiklos Szeredi list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) { 5454ed5e82fSMiklos Szeredi if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD) 5464ed5e82fSMiklos Szeredi mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD; 5474ed5e82fSMiklos Szeredi } 548719ea2fbSAl Viro unlock_mount_hash(); 5494ed5e82fSMiklos Szeredi 5504ed5e82fSMiklos Szeredi return err; 5514ed5e82fSMiklos Szeredi } 5524ed5e82fSMiklos Szeredi 553b105e270SAl Viro static void free_vfsmnt(struct mount *mnt) 5541da177e4SLinus Torvalds { 555fcc139aeSAndrzej Hajda kfree_const(mnt->mnt_devname); 556d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 55768e8a9feSAl Viro free_percpu(mnt->mnt_pcp); 558d3ef3d73Snpiggin@suse.de #endif 559b105e270SAl Viro kmem_cache_free(mnt_cache, mnt); 5601da177e4SLinus Torvalds } 5611da177e4SLinus Torvalds 5628ffcb32eSDavid Howells static void delayed_free_vfsmnt(struct rcu_head *head) 5638ffcb32eSDavid Howells { 5648ffcb32eSDavid Howells free_vfsmnt(container_of(head, struct mount, mnt_rcu)); 5658ffcb32eSDavid Howells } 5668ffcb32eSDavid Howells 56748a066e7SAl Viro /* call under rcu_read_lock */ 568294d71ffSAl Viro int __legitimize_mnt(struct vfsmount *bastard, unsigned seq) 56948a066e7SAl Viro { 57048a066e7SAl Viro struct mount *mnt; 57148a066e7SAl Viro if (read_seqretry(&mount_lock, seq)) 572294d71ffSAl Viro return 1; 57348a066e7SAl Viro if (bastard == NULL) 574294d71ffSAl Viro return 0; 57548a066e7SAl Viro mnt = real_mount(bastard); 57648a066e7SAl Viro mnt_add_count(mnt, 1); 577119e1ef8SAl Viro smp_mb(); // see mntput_no_expire() 57848a066e7SAl Viro if (likely(!read_seqretry(&mount_lock, seq))) 579294d71ffSAl Viro return 0; 58048a066e7SAl Viro if (bastard->mnt_flags & MNT_SYNC_UMOUNT) { 58148a066e7SAl Viro mnt_add_count(mnt, -1); 582294d71ffSAl Viro return 1; 58348a066e7SAl Viro } 584119e1ef8SAl Viro lock_mount_hash(); 585119e1ef8SAl Viro if (unlikely(bastard->mnt_flags & MNT_DOOMED)) { 586119e1ef8SAl Viro mnt_add_count(mnt, -1); 587119e1ef8SAl Viro unlock_mount_hash(); 588119e1ef8SAl Viro return 1; 589119e1ef8SAl Viro } 590119e1ef8SAl Viro unlock_mount_hash(); 591119e1ef8SAl Viro /* caller will mntput() */ 592294d71ffSAl Viro return -1; 593294d71ffSAl Viro } 594294d71ffSAl Viro 595294d71ffSAl Viro /* call under rcu_read_lock */ 596294d71ffSAl Viro bool legitimize_mnt(struct vfsmount *bastard, unsigned seq) 597294d71ffSAl Viro { 598294d71ffSAl Viro int res = __legitimize_mnt(bastard, seq); 599294d71ffSAl Viro if (likely(!res)) 600294d71ffSAl Viro return true; 601294d71ffSAl Viro if (unlikely(res < 0)) { 60248a066e7SAl Viro rcu_read_unlock(); 60348a066e7SAl Viro mntput(bastard); 60448a066e7SAl Viro rcu_read_lock(); 605294d71ffSAl Viro } 60648a066e7SAl Viro return false; 60748a066e7SAl Viro } 60848a066e7SAl Viro 6091da177e4SLinus Torvalds /* 610474279dcSAl Viro * find the first mount at @dentry on vfsmount @mnt. 61148a066e7SAl Viro * call under rcu_read_lock() 6121da177e4SLinus Torvalds */ 613474279dcSAl Viro struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry) 6141da177e4SLinus Torvalds { 61538129a13SAl Viro struct hlist_head *head = m_hash(mnt, dentry); 616474279dcSAl Viro struct mount *p; 6171da177e4SLinus Torvalds 61838129a13SAl Viro hlist_for_each_entry_rcu(p, head, mnt_hash) 619474279dcSAl Viro if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry) 620474279dcSAl Viro return p; 621474279dcSAl Viro return NULL; 6221da177e4SLinus Torvalds } 623474279dcSAl Viro 624474279dcSAl Viro /* 625f015f126SDavid Howells * lookup_mnt - Return the first child mount mounted at path 626f015f126SDavid Howells * 627f015f126SDavid Howells * "First" means first mounted chronologically. If you create the 628f015f126SDavid Howells * following mounts: 629f015f126SDavid Howells * 630f015f126SDavid Howells * mount /dev/sda1 /mnt 631f015f126SDavid Howells * mount /dev/sda2 /mnt 632f015f126SDavid Howells * mount /dev/sda3 /mnt 633f015f126SDavid Howells * 634f015f126SDavid Howells * Then lookup_mnt() on the base /mnt dentry in the root mount will 635f015f126SDavid Howells * return successively the root dentry and vfsmount of /dev/sda1, then 636f015f126SDavid Howells * /dev/sda2, then /dev/sda3, then NULL. 637f015f126SDavid Howells * 638f015f126SDavid Howells * lookup_mnt takes a reference to the found vfsmount. 639a05964f3SRam Pai */ 640ca71cf71SAl Viro struct vfsmount *lookup_mnt(const struct path *path) 641a05964f3SRam Pai { 642c7105365SAl Viro struct mount *child_mnt; 64348a066e7SAl Viro struct vfsmount *m; 64448a066e7SAl Viro unsigned seq; 64599b7db7bSNick Piggin 64648a066e7SAl Viro rcu_read_lock(); 64748a066e7SAl Viro do { 64848a066e7SAl Viro seq = read_seqbegin(&mount_lock); 649474279dcSAl Viro child_mnt = __lookup_mnt(path->mnt, path->dentry); 65048a066e7SAl Viro m = child_mnt ? &child_mnt->mnt : NULL; 65148a066e7SAl Viro } while (!legitimize_mnt(m, seq)); 65248a066e7SAl Viro rcu_read_unlock(); 65348a066e7SAl Viro return m; 654a05964f3SRam Pai } 655a05964f3SRam Pai 6567af1364fSEric W. Biederman /* 6577af1364fSEric W. Biederman * __is_local_mountpoint - Test to see if dentry is a mountpoint in the 6587af1364fSEric W. Biederman * current mount namespace. 6597af1364fSEric W. Biederman * 6607af1364fSEric W. Biederman * The common case is dentries are not mountpoints at all and that 6617af1364fSEric W. Biederman * test is handled inline. For the slow case when we are actually 6627af1364fSEric W. Biederman * dealing with a mountpoint of some kind, walk through all of the 6637af1364fSEric W. Biederman * mounts in the current mount namespace and test to see if the dentry 6647af1364fSEric W. Biederman * is a mountpoint. 6657af1364fSEric W. Biederman * 6667af1364fSEric W. Biederman * The mount_hashtable is not usable in the context because we 6677af1364fSEric W. Biederman * need to identify all mounts that may be in the current mount 6687af1364fSEric W. Biederman * namespace not just a mount that happens to have some specified 6697af1364fSEric W. Biederman * parent mount. 6707af1364fSEric W. Biederman */ 6717af1364fSEric W. Biederman bool __is_local_mountpoint(struct dentry *dentry) 6727af1364fSEric W. Biederman { 6737af1364fSEric W. Biederman struct mnt_namespace *ns = current->nsproxy->mnt_ns; 6747af1364fSEric W. Biederman struct mount *mnt; 6757af1364fSEric W. Biederman bool is_covered = false; 6767af1364fSEric W. Biederman 6777af1364fSEric W. Biederman if (!d_mountpoint(dentry)) 6787af1364fSEric W. Biederman goto out; 6797af1364fSEric W. Biederman 6807af1364fSEric W. Biederman down_read(&namespace_sem); 6817af1364fSEric W. Biederman list_for_each_entry(mnt, &ns->list, mnt_list) { 6827af1364fSEric W. Biederman is_covered = (mnt->mnt_mountpoint == dentry); 6837af1364fSEric W. Biederman if (is_covered) 6847af1364fSEric W. Biederman break; 6857af1364fSEric W. Biederman } 6867af1364fSEric W. Biederman up_read(&namespace_sem); 6877af1364fSEric W. Biederman out: 6887af1364fSEric W. Biederman return is_covered; 6897af1364fSEric W. Biederman } 6907af1364fSEric W. Biederman 691e2dfa935SEric W. Biederman static struct mountpoint *lookup_mountpoint(struct dentry *dentry) 69284d17192SAl Viro { 6930818bf27SAl Viro struct hlist_head *chain = mp_hash(dentry); 69484d17192SAl Viro struct mountpoint *mp; 69584d17192SAl Viro 6960818bf27SAl Viro hlist_for_each_entry(mp, chain, m_hash) { 69784d17192SAl Viro if (mp->m_dentry == dentry) { 69884d17192SAl Viro mp->m_count++; 69984d17192SAl Viro return mp; 70084d17192SAl Viro } 70184d17192SAl Viro } 702e2dfa935SEric W. Biederman return NULL; 703e2dfa935SEric W. Biederman } 704e2dfa935SEric W. Biederman 7053895dbf8SEric W. Biederman static struct mountpoint *get_mountpoint(struct dentry *dentry) 706e2dfa935SEric W. Biederman { 7073895dbf8SEric W. Biederman struct mountpoint *mp, *new = NULL; 708e2dfa935SEric W. Biederman int ret; 70984d17192SAl Viro 7103895dbf8SEric W. Biederman if (d_mountpoint(dentry)) { 7111e9c75fbSBenjamin Coddington /* might be worth a WARN_ON() */ 7121e9c75fbSBenjamin Coddington if (d_unlinked(dentry)) 7131e9c75fbSBenjamin Coddington return ERR_PTR(-ENOENT); 7143895dbf8SEric W. Biederman mountpoint: 7153895dbf8SEric W. Biederman read_seqlock_excl(&mount_lock); 7163895dbf8SEric W. Biederman mp = lookup_mountpoint(dentry); 7173895dbf8SEric W. Biederman read_sequnlock_excl(&mount_lock); 7183895dbf8SEric W. Biederman if (mp) 7193895dbf8SEric W. Biederman goto done; 72084d17192SAl Viro } 721eed81007SMiklos Szeredi 7223895dbf8SEric W. Biederman if (!new) 7233895dbf8SEric W. Biederman new = kmalloc(sizeof(struct mountpoint), GFP_KERNEL); 7243895dbf8SEric W. Biederman if (!new) 7253895dbf8SEric W. Biederman return ERR_PTR(-ENOMEM); 7263895dbf8SEric W. Biederman 7273895dbf8SEric W. Biederman 7283895dbf8SEric W. Biederman /* Exactly one processes may set d_mounted */ 7293895dbf8SEric W. Biederman ret = d_set_mounted(dentry); 7303895dbf8SEric W. Biederman 7313895dbf8SEric W. Biederman /* Someone else set d_mounted? */ 7323895dbf8SEric W. Biederman if (ret == -EBUSY) 7333895dbf8SEric W. Biederman goto mountpoint; 7343895dbf8SEric W. Biederman 7353895dbf8SEric W. Biederman /* The dentry is not available as a mountpoint? */ 7363895dbf8SEric W. Biederman mp = ERR_PTR(ret); 7373895dbf8SEric W. Biederman if (ret) 7383895dbf8SEric W. Biederman goto done; 7393895dbf8SEric W. Biederman 7403895dbf8SEric W. Biederman /* Add the new mountpoint to the hash table */ 7413895dbf8SEric W. Biederman read_seqlock_excl(&mount_lock); 7423895dbf8SEric W. Biederman new->m_dentry = dentry; 7433895dbf8SEric W. Biederman new->m_count = 1; 7443895dbf8SEric W. Biederman hlist_add_head(&new->m_hash, mp_hash(dentry)); 7453895dbf8SEric W. Biederman INIT_HLIST_HEAD(&new->m_list); 7463895dbf8SEric W. Biederman read_sequnlock_excl(&mount_lock); 7473895dbf8SEric W. Biederman 7483895dbf8SEric W. Biederman mp = new; 7493895dbf8SEric W. Biederman new = NULL; 7503895dbf8SEric W. Biederman done: 7513895dbf8SEric W. Biederman kfree(new); 75284d17192SAl Viro return mp; 75384d17192SAl Viro } 75484d17192SAl Viro 75584d17192SAl Viro static void put_mountpoint(struct mountpoint *mp) 75684d17192SAl Viro { 75784d17192SAl Viro if (!--mp->m_count) { 75884d17192SAl Viro struct dentry *dentry = mp->m_dentry; 7590a5eb7c8SEric W. Biederman BUG_ON(!hlist_empty(&mp->m_list)); 76084d17192SAl Viro spin_lock(&dentry->d_lock); 76184d17192SAl Viro dentry->d_flags &= ~DCACHE_MOUNTED; 76284d17192SAl Viro spin_unlock(&dentry->d_lock); 7630818bf27SAl Viro hlist_del(&mp->m_hash); 76484d17192SAl Viro kfree(mp); 76584d17192SAl Viro } 76684d17192SAl Viro } 76784d17192SAl Viro 768143c8c91SAl Viro static inline int check_mnt(struct mount *mnt) 7691da177e4SLinus Torvalds { 7706b3286edSKirill Korotaev return mnt->mnt_ns == current->nsproxy->mnt_ns; 7711da177e4SLinus Torvalds } 7721da177e4SLinus Torvalds 77399b7db7bSNick Piggin /* 77499b7db7bSNick Piggin * vfsmount lock must be held for write 77599b7db7bSNick Piggin */ 7766b3286edSKirill Korotaev static void touch_mnt_namespace(struct mnt_namespace *ns) 7775addc5ddSAl Viro { 7785addc5ddSAl Viro if (ns) { 7795addc5ddSAl Viro ns->event = ++event; 7805addc5ddSAl Viro wake_up_interruptible(&ns->poll); 7815addc5ddSAl Viro } 7825addc5ddSAl Viro } 7835addc5ddSAl Viro 78499b7db7bSNick Piggin /* 78599b7db7bSNick Piggin * vfsmount lock must be held for write 78699b7db7bSNick Piggin */ 7876b3286edSKirill Korotaev static void __touch_mnt_namespace(struct mnt_namespace *ns) 7885addc5ddSAl Viro { 7895addc5ddSAl Viro if (ns && ns->event != event) { 7905addc5ddSAl Viro ns->event = event; 7915addc5ddSAl Viro wake_up_interruptible(&ns->poll); 7925addc5ddSAl Viro } 7935addc5ddSAl Viro } 7945addc5ddSAl Viro 79599b7db7bSNick Piggin /* 79699b7db7bSNick Piggin * vfsmount lock must be held for write 79799b7db7bSNick Piggin */ 7987bdb11deSEric W. Biederman static void unhash_mnt(struct mount *mnt) 7991da177e4SLinus Torvalds { 8000714a533SAl Viro mnt->mnt_parent = mnt; 801a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 8026b41d536SAl Viro list_del_init(&mnt->mnt_child); 80338129a13SAl Viro hlist_del_init_rcu(&mnt->mnt_hash); 8040a5eb7c8SEric W. Biederman hlist_del_init(&mnt->mnt_mp_list); 80584d17192SAl Viro put_mountpoint(mnt->mnt_mp); 80684d17192SAl Viro mnt->mnt_mp = NULL; 8071da177e4SLinus Torvalds } 8081da177e4SLinus Torvalds 80999b7db7bSNick Piggin /* 81099b7db7bSNick Piggin * vfsmount lock must be held for write 81199b7db7bSNick Piggin */ 8127bdb11deSEric W. Biederman static void detach_mnt(struct mount *mnt, struct path *old_path) 8137bdb11deSEric W. Biederman { 8147bdb11deSEric W. Biederman old_path->dentry = mnt->mnt_mountpoint; 8157bdb11deSEric W. Biederman old_path->mnt = &mnt->mnt_parent->mnt; 8167bdb11deSEric W. Biederman unhash_mnt(mnt); 8177bdb11deSEric W. Biederman } 8187bdb11deSEric W. Biederman 8197bdb11deSEric W. Biederman /* 8207bdb11deSEric W. Biederman * vfsmount lock must be held for write 8217bdb11deSEric W. Biederman */ 8226a46c573SEric W. Biederman static void umount_mnt(struct mount *mnt) 8236a46c573SEric W. Biederman { 8246a46c573SEric W. Biederman /* old mountpoint will be dropped when we can do that */ 8256a46c573SEric W. Biederman mnt->mnt_ex_mountpoint = mnt->mnt_mountpoint; 8266a46c573SEric W. Biederman unhash_mnt(mnt); 8276a46c573SEric W. Biederman } 8286a46c573SEric W. Biederman 8296a46c573SEric W. Biederman /* 8306a46c573SEric W. Biederman * vfsmount lock must be held for write 8316a46c573SEric W. Biederman */ 83284d17192SAl Viro void mnt_set_mountpoint(struct mount *mnt, 83384d17192SAl Viro struct mountpoint *mp, 83444d964d6SAl Viro struct mount *child_mnt) 835b90fa9aeSRam Pai { 83684d17192SAl Viro mp->m_count++; 8373a2393d7SAl Viro mnt_add_count(mnt, 1); /* essentially, that's mntget */ 83884d17192SAl Viro child_mnt->mnt_mountpoint = dget(mp->m_dentry); 8393a2393d7SAl Viro child_mnt->mnt_parent = mnt; 84084d17192SAl Viro child_mnt->mnt_mp = mp; 8410a5eb7c8SEric W. Biederman hlist_add_head(&child_mnt->mnt_mp_list, &mp->m_list); 842b90fa9aeSRam Pai } 843b90fa9aeSRam Pai 8441064f874SEric W. Biederman static void __attach_mnt(struct mount *mnt, struct mount *parent) 8451064f874SEric W. Biederman { 8461064f874SEric W. Biederman hlist_add_head_rcu(&mnt->mnt_hash, 8471064f874SEric W. Biederman m_hash(&parent->mnt, mnt->mnt_mountpoint)); 8481064f874SEric W. Biederman list_add_tail(&mnt->mnt_child, &parent->mnt_mounts); 8491064f874SEric W. Biederman } 8501064f874SEric W. Biederman 85199b7db7bSNick Piggin /* 85299b7db7bSNick Piggin * vfsmount lock must be held for write 85399b7db7bSNick Piggin */ 85484d17192SAl Viro static void attach_mnt(struct mount *mnt, 85584d17192SAl Viro struct mount *parent, 85684d17192SAl Viro struct mountpoint *mp) 8571da177e4SLinus Torvalds { 85884d17192SAl Viro mnt_set_mountpoint(parent, mp, mnt); 8591064f874SEric W. Biederman __attach_mnt(mnt, parent); 860b90fa9aeSRam Pai } 861b90fa9aeSRam Pai 8621064f874SEric W. Biederman void mnt_change_mountpoint(struct mount *parent, struct mountpoint *mp, struct mount *mnt) 86312a5b529SAl Viro { 8641064f874SEric W. Biederman struct mountpoint *old_mp = mnt->mnt_mp; 8651064f874SEric W. Biederman struct dentry *old_mountpoint = mnt->mnt_mountpoint; 8661064f874SEric W. Biederman struct mount *old_parent = mnt->mnt_parent; 8671064f874SEric W. Biederman 8681064f874SEric W. Biederman list_del_init(&mnt->mnt_child); 8691064f874SEric W. Biederman hlist_del_init(&mnt->mnt_mp_list); 8701064f874SEric W. Biederman hlist_del_init_rcu(&mnt->mnt_hash); 8711064f874SEric W. Biederman 8721064f874SEric W. Biederman attach_mnt(mnt, parent, mp); 8731064f874SEric W. Biederman 8741064f874SEric W. Biederman put_mountpoint(old_mp); 8751064f874SEric W. Biederman 8761064f874SEric W. Biederman /* 8771064f874SEric W. Biederman * Safely avoid even the suggestion this code might sleep or 8781064f874SEric W. Biederman * lock the mount hash by taking advantage of the knowledge that 8791064f874SEric W. Biederman * mnt_change_mountpoint will not release the final reference 8801064f874SEric W. Biederman * to a mountpoint. 8811064f874SEric W. Biederman * 8821064f874SEric W. Biederman * During mounting, the mount passed in as the parent mount will 8831064f874SEric W. Biederman * continue to use the old mountpoint and during unmounting, the 8841064f874SEric W. Biederman * old mountpoint will continue to exist until namespace_unlock, 8851064f874SEric W. Biederman * which happens well after mnt_change_mountpoint. 8861064f874SEric W. Biederman */ 8871064f874SEric W. Biederman spin_lock(&old_mountpoint->d_lock); 8881064f874SEric W. Biederman old_mountpoint->d_lockref.count--; 8891064f874SEric W. Biederman spin_unlock(&old_mountpoint->d_lock); 8901064f874SEric W. Biederman 8911064f874SEric W. Biederman mnt_add_count(old_parent, -1); 89212a5b529SAl Viro } 89312a5b529SAl Viro 894b90fa9aeSRam Pai /* 89599b7db7bSNick Piggin * vfsmount lock must be held for write 896b90fa9aeSRam Pai */ 8971064f874SEric W. Biederman static void commit_tree(struct mount *mnt) 898b90fa9aeSRam Pai { 8990714a533SAl Viro struct mount *parent = mnt->mnt_parent; 90083adc753SAl Viro struct mount *m; 901b90fa9aeSRam Pai LIST_HEAD(head); 902143c8c91SAl Viro struct mnt_namespace *n = parent->mnt_ns; 903b90fa9aeSRam Pai 9040714a533SAl Viro BUG_ON(parent == mnt); 905b90fa9aeSRam Pai 9061a4eeaf2SAl Viro list_add_tail(&head, &mnt->mnt_list); 907f7a99c5bSAl Viro list_for_each_entry(m, &head, mnt_list) 908143c8c91SAl Viro m->mnt_ns = n; 909f03c6599SAl Viro 910b90fa9aeSRam Pai list_splice(&head, n->list.prev); 911b90fa9aeSRam Pai 912d2921684SEric W. Biederman n->mounts += n->pending_mounts; 913d2921684SEric W. Biederman n->pending_mounts = 0; 914d2921684SEric W. Biederman 9151064f874SEric W. Biederman __attach_mnt(mnt, parent); 9166b3286edSKirill Korotaev touch_mnt_namespace(n); 9171da177e4SLinus Torvalds } 9181da177e4SLinus Torvalds 919909b0a88SAl Viro static struct mount *next_mnt(struct mount *p, struct mount *root) 9201da177e4SLinus Torvalds { 9216b41d536SAl Viro struct list_head *next = p->mnt_mounts.next; 9226b41d536SAl Viro if (next == &p->mnt_mounts) { 9231da177e4SLinus Torvalds while (1) { 924909b0a88SAl Viro if (p == root) 9251da177e4SLinus Torvalds return NULL; 9266b41d536SAl Viro next = p->mnt_child.next; 9276b41d536SAl Viro if (next != &p->mnt_parent->mnt_mounts) 9281da177e4SLinus Torvalds break; 9290714a533SAl Viro p = p->mnt_parent; 9301da177e4SLinus Torvalds } 9311da177e4SLinus Torvalds } 9326b41d536SAl Viro return list_entry(next, struct mount, mnt_child); 9331da177e4SLinus Torvalds } 9341da177e4SLinus Torvalds 935315fc83eSAl Viro static struct mount *skip_mnt_tree(struct mount *p) 9369676f0c6SRam Pai { 9376b41d536SAl Viro struct list_head *prev = p->mnt_mounts.prev; 9386b41d536SAl Viro while (prev != &p->mnt_mounts) { 9396b41d536SAl Viro p = list_entry(prev, struct mount, mnt_child); 9406b41d536SAl Viro prev = p->mnt_mounts.prev; 9419676f0c6SRam Pai } 9429676f0c6SRam Pai return p; 9439676f0c6SRam Pai } 9449676f0c6SRam Pai 9458f291889SAl Viro /** 9468f291889SAl Viro * vfs_create_mount - Create a mount for a configured superblock 9478f291889SAl Viro * @fc: The configuration context with the superblock attached 9488f291889SAl Viro * 9498f291889SAl Viro * Create a mount to an already configured superblock. If necessary, the 9508f291889SAl Viro * caller should invoke vfs_get_tree() before calling this. 9518f291889SAl Viro * 9528f291889SAl Viro * Note that this does not attach the mount to anything. 9538f291889SAl Viro */ 9548f291889SAl Viro struct vfsmount *vfs_create_mount(struct fs_context *fc) 9559d412a43SAl Viro { 956b105e270SAl Viro struct mount *mnt; 9579d412a43SAl Viro 9588f291889SAl Viro if (!fc->root) 9598f291889SAl Viro return ERR_PTR(-EINVAL); 9609d412a43SAl Viro 9618f291889SAl Viro mnt = alloc_vfsmnt(fc->source ?: "none"); 9629d412a43SAl Viro if (!mnt) 9639d412a43SAl Viro return ERR_PTR(-ENOMEM); 9649d412a43SAl Viro 9658f291889SAl Viro if (fc->sb_flags & SB_KERNMOUNT) 966b105e270SAl Viro mnt->mnt.mnt_flags = MNT_INTERNAL; 9679d412a43SAl Viro 9688f291889SAl Viro atomic_inc(&fc->root->d_sb->s_active); 9698f291889SAl Viro mnt->mnt.mnt_sb = fc->root->d_sb; 9708f291889SAl Viro mnt->mnt.mnt_root = dget(fc->root); 971a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 9720714a533SAl Viro mnt->mnt_parent = mnt; 9738f291889SAl Viro 974719ea2fbSAl Viro lock_mount_hash(); 9758f291889SAl Viro list_add_tail(&mnt->mnt_instance, &mnt->mnt.mnt_sb->s_mounts); 976719ea2fbSAl Viro unlock_mount_hash(); 977b105e270SAl Viro return &mnt->mnt; 9789d412a43SAl Viro } 9798f291889SAl Viro EXPORT_SYMBOL(vfs_create_mount); 9808f291889SAl Viro 9818f291889SAl Viro struct vfsmount *fc_mount(struct fs_context *fc) 9828f291889SAl Viro { 9838f291889SAl Viro int err = vfs_get_tree(fc); 9848f291889SAl Viro if (!err) { 9858f291889SAl Viro up_write(&fc->root->d_sb->s_umount); 9868f291889SAl Viro return vfs_create_mount(fc); 9878f291889SAl Viro } 9888f291889SAl Viro return ERR_PTR(err); 9898f291889SAl Viro } 9908f291889SAl Viro EXPORT_SYMBOL(fc_mount); 9918f291889SAl Viro 9929bc61ab1SDavid Howells struct vfsmount *vfs_kern_mount(struct file_system_type *type, 9939bc61ab1SDavid Howells int flags, const char *name, 9949bc61ab1SDavid Howells void *data) 9951da177e4SLinus Torvalds { 9969bc61ab1SDavid Howells struct fs_context *fc; 9978f291889SAl Viro struct vfsmount *mnt; 9989bc61ab1SDavid Howells int ret = 0; 9999d412a43SAl Viro 10009d412a43SAl Viro if (!type) 10013e1aeb00SDavid Howells return ERR_PTR(-EINVAL); 10029d412a43SAl Viro 10039bc61ab1SDavid Howells fc = fs_context_for_mount(type, flags); 10049bc61ab1SDavid Howells if (IS_ERR(fc)) 10059bc61ab1SDavid Howells return ERR_CAST(fc); 10069bc61ab1SDavid Howells 10073e1aeb00SDavid Howells if (name) 10083e1aeb00SDavid Howells ret = vfs_parse_fs_string(fc, "source", 10093e1aeb00SDavid Howells name, strlen(name)); 10109bc61ab1SDavid Howells if (!ret) 10119bc61ab1SDavid Howells ret = parse_monolithic_mount_data(fc, data); 10129bc61ab1SDavid Howells if (!ret) 10138f291889SAl Viro mnt = fc_mount(fc); 10148f291889SAl Viro else 10158f291889SAl Viro mnt = ERR_PTR(ret); 10169d412a43SAl Viro 10179bc61ab1SDavid Howells put_fs_context(fc); 10188f291889SAl Viro return mnt; 10199d412a43SAl Viro } 10209d412a43SAl Viro EXPORT_SYMBOL_GPL(vfs_kern_mount); 10219d412a43SAl Viro 102293faccbbSEric W. Biederman struct vfsmount * 102393faccbbSEric W. Biederman vfs_submount(const struct dentry *mountpoint, struct file_system_type *type, 102493faccbbSEric W. Biederman const char *name, void *data) 102593faccbbSEric W. Biederman { 102693faccbbSEric W. Biederman /* Until it is worked out how to pass the user namespace 102793faccbbSEric W. Biederman * through from the parent mount to the submount don't support 102893faccbbSEric W. Biederman * unprivileged mounts with submounts. 102993faccbbSEric W. Biederman */ 103093faccbbSEric W. Biederman if (mountpoint->d_sb->s_user_ns != &init_user_ns) 103193faccbbSEric W. Biederman return ERR_PTR(-EPERM); 103293faccbbSEric W. Biederman 1033e462ec50SDavid Howells return vfs_kern_mount(type, SB_SUBMOUNT, name, data); 103493faccbbSEric W. Biederman } 103593faccbbSEric W. Biederman EXPORT_SYMBOL_GPL(vfs_submount); 103693faccbbSEric W. Biederman 103787129cc0SAl Viro static struct mount *clone_mnt(struct mount *old, struct dentry *root, 103836341f64SRam Pai int flag) 10391da177e4SLinus Torvalds { 104087129cc0SAl Viro struct super_block *sb = old->mnt.mnt_sb; 1041be34d1a3SDavid Howells struct mount *mnt; 1042be34d1a3SDavid Howells int err; 10431da177e4SLinus Torvalds 1044be34d1a3SDavid Howells mnt = alloc_vfsmnt(old->mnt_devname); 1045be34d1a3SDavid Howells if (!mnt) 1046be34d1a3SDavid Howells return ERR_PTR(-ENOMEM); 1047be34d1a3SDavid Howells 10487a472ef4SEric W. Biederman if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE)) 104915169fe7SAl Viro mnt->mnt_group_id = 0; /* not a peer of original */ 1050719f5d7fSMiklos Szeredi else 105115169fe7SAl Viro mnt->mnt_group_id = old->mnt_group_id; 1052719f5d7fSMiklos Szeredi 105315169fe7SAl Viro if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) { 1054be34d1a3SDavid Howells err = mnt_alloc_group_id(mnt); 1055719f5d7fSMiklos Szeredi if (err) 1056719f5d7fSMiklos Szeredi goto out_free; 1057719f5d7fSMiklos Szeredi } 1058719f5d7fSMiklos Szeredi 105916a34adbSAl Viro mnt->mnt.mnt_flags = old->mnt.mnt_flags; 106016a34adbSAl Viro mnt->mnt.mnt_flags &= ~(MNT_WRITE_HOLD|MNT_MARKED|MNT_INTERNAL); 10615ff9d8a6SEric W. Biederman 10621da177e4SLinus Torvalds atomic_inc(&sb->s_active); 1063b105e270SAl Viro mnt->mnt.mnt_sb = sb; 1064b105e270SAl Viro mnt->mnt.mnt_root = dget(root); 1065a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 10660714a533SAl Viro mnt->mnt_parent = mnt; 1067719ea2fbSAl Viro lock_mount_hash(); 106839f7c4dbSMiklos Szeredi list_add_tail(&mnt->mnt_instance, &sb->s_mounts); 1069719ea2fbSAl Viro unlock_mount_hash(); 1070b90fa9aeSRam Pai 10717a472ef4SEric W. Biederman if ((flag & CL_SLAVE) || 10727a472ef4SEric W. Biederman ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) { 10736776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave_list); 107432301920SAl Viro mnt->mnt_master = old; 1075fc7be130SAl Viro CLEAR_MNT_SHARED(mnt); 10768aec0809SAl Viro } else if (!(flag & CL_PRIVATE)) { 1077fc7be130SAl Viro if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old)) 10786776db3dSAl Viro list_add(&mnt->mnt_share, &old->mnt_share); 1079d10e8defSAl Viro if (IS_MNT_SLAVE(old)) 10806776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave); 1081d10e8defSAl Viro mnt->mnt_master = old->mnt_master; 10825235d448SAl Viro } else { 10835235d448SAl Viro CLEAR_MNT_SHARED(mnt); 10845afe0022SRam Pai } 1085b90fa9aeSRam Pai if (flag & CL_MAKE_SHARED) 10860f0afb1dSAl Viro set_mnt_shared(mnt); 10871da177e4SLinus Torvalds 10881da177e4SLinus Torvalds /* stick the duplicate mount on the same expiry list 10891da177e4SLinus Torvalds * as the original if that was on one */ 109036341f64SRam Pai if (flag & CL_EXPIRE) { 10916776db3dSAl Viro if (!list_empty(&old->mnt_expire)) 10926776db3dSAl Viro list_add(&mnt->mnt_expire, &old->mnt_expire); 10931da177e4SLinus Torvalds } 1094be34d1a3SDavid Howells 1095cb338d06SAl Viro return mnt; 1096719f5d7fSMiklos Szeredi 1097719f5d7fSMiklos Szeredi out_free: 10988ffcb32eSDavid Howells mnt_free_id(mnt); 1099719f5d7fSMiklos Szeredi free_vfsmnt(mnt); 1100be34d1a3SDavid Howells return ERR_PTR(err); 11011da177e4SLinus Torvalds } 11021da177e4SLinus Torvalds 11039ea459e1SAl Viro static void cleanup_mnt(struct mount *mnt) 11049ea459e1SAl Viro { 11059ea459e1SAl Viro /* 11069ea459e1SAl Viro * This probably indicates that somebody messed 11079ea459e1SAl Viro * up a mnt_want/drop_write() pair. If this 11089ea459e1SAl Viro * happens, the filesystem was probably unable 11099ea459e1SAl Viro * to make r/w->r/o transitions. 11109ea459e1SAl Viro */ 11119ea459e1SAl Viro /* 11129ea459e1SAl Viro * The locking used to deal with mnt_count decrement provides barriers, 11139ea459e1SAl Viro * so mnt_get_writers() below is safe. 11149ea459e1SAl Viro */ 11159ea459e1SAl Viro WARN_ON(mnt_get_writers(mnt)); 11169ea459e1SAl Viro if (unlikely(mnt->mnt_pins.first)) 11179ea459e1SAl Viro mnt_pin_kill(mnt); 11189ea459e1SAl Viro fsnotify_vfsmount_delete(&mnt->mnt); 11199ea459e1SAl Viro dput(mnt->mnt.mnt_root); 11209ea459e1SAl Viro deactivate_super(mnt->mnt.mnt_sb); 11219ea459e1SAl Viro mnt_free_id(mnt); 11229ea459e1SAl Viro call_rcu(&mnt->mnt_rcu, delayed_free_vfsmnt); 11239ea459e1SAl Viro } 11249ea459e1SAl Viro 11259ea459e1SAl Viro static void __cleanup_mnt(struct rcu_head *head) 11269ea459e1SAl Viro { 11279ea459e1SAl Viro cleanup_mnt(container_of(head, struct mount, mnt_rcu)); 11289ea459e1SAl Viro } 11299ea459e1SAl Viro 11309ea459e1SAl Viro static LLIST_HEAD(delayed_mntput_list); 11319ea459e1SAl Viro static void delayed_mntput(struct work_struct *unused) 11329ea459e1SAl Viro { 11339ea459e1SAl Viro struct llist_node *node = llist_del_all(&delayed_mntput_list); 113429785735SByungchul Park struct mount *m, *t; 11359ea459e1SAl Viro 113629785735SByungchul Park llist_for_each_entry_safe(m, t, node, mnt_llist) 113729785735SByungchul Park cleanup_mnt(m); 11389ea459e1SAl Viro } 11399ea459e1SAl Viro static DECLARE_DELAYED_WORK(delayed_mntput_work, delayed_mntput); 11409ea459e1SAl Viro 1141900148dcSAl Viro static void mntput_no_expire(struct mount *mnt) 11427b7b1aceSAl Viro { 114348a066e7SAl Viro rcu_read_lock(); 11449ea0a46cSAl Viro if (likely(READ_ONCE(mnt->mnt_ns))) { 11459ea0a46cSAl Viro /* 11469ea0a46cSAl Viro * Since we don't do lock_mount_hash() here, 11479ea0a46cSAl Viro * ->mnt_ns can change under us. However, if it's 11489ea0a46cSAl Viro * non-NULL, then there's a reference that won't 11499ea0a46cSAl Viro * be dropped until after an RCU delay done after 11509ea0a46cSAl Viro * turning ->mnt_ns NULL. So if we observe it 11519ea0a46cSAl Viro * non-NULL under rcu_read_lock(), the reference 11529ea0a46cSAl Viro * we are dropping is not the final one. 11539ea0a46cSAl Viro */ 1154aa9c0e07SAl Viro mnt_add_count(mnt, -1); 115548a066e7SAl Viro rcu_read_unlock(); 115699b7db7bSNick Piggin return; 1157b3e19d92SNick Piggin } 1158719ea2fbSAl Viro lock_mount_hash(); 1159119e1ef8SAl Viro /* 1160119e1ef8SAl Viro * make sure that if __legitimize_mnt() has not seen us grab 1161119e1ef8SAl Viro * mount_lock, we'll see their refcount increment here. 1162119e1ef8SAl Viro */ 1163119e1ef8SAl Viro smp_mb(); 11649ea0a46cSAl Viro mnt_add_count(mnt, -1); 1165b3e19d92SNick Piggin if (mnt_get_count(mnt)) { 116648a066e7SAl Viro rcu_read_unlock(); 1167719ea2fbSAl Viro unlock_mount_hash(); 116899b7db7bSNick Piggin return; 116999b7db7bSNick Piggin } 117048a066e7SAl Viro if (unlikely(mnt->mnt.mnt_flags & MNT_DOOMED)) { 117148a066e7SAl Viro rcu_read_unlock(); 117248a066e7SAl Viro unlock_mount_hash(); 117348a066e7SAl Viro return; 117448a066e7SAl Viro } 117548a066e7SAl Viro mnt->mnt.mnt_flags |= MNT_DOOMED; 117648a066e7SAl Viro rcu_read_unlock(); 1177962830dfSAndi Kleen 117839f7c4dbSMiklos Szeredi list_del(&mnt->mnt_instance); 1179ce07d891SEric W. Biederman 1180ce07d891SEric W. Biederman if (unlikely(!list_empty(&mnt->mnt_mounts))) { 1181ce07d891SEric W. Biederman struct mount *p, *tmp; 1182ce07d891SEric W. Biederman list_for_each_entry_safe(p, tmp, &mnt->mnt_mounts, mnt_child) { 1183ce07d891SEric W. Biederman umount_mnt(p); 1184ce07d891SEric W. Biederman } 1185ce07d891SEric W. Biederman } 1186719ea2fbSAl Viro unlock_mount_hash(); 1187649a795aSAl Viro 11889ea459e1SAl Viro if (likely(!(mnt->mnt.mnt_flags & MNT_INTERNAL))) { 11899ea459e1SAl Viro struct task_struct *task = current; 11909ea459e1SAl Viro if (likely(!(task->flags & PF_KTHREAD))) { 11919ea459e1SAl Viro init_task_work(&mnt->mnt_rcu, __cleanup_mnt); 11929ea459e1SAl Viro if (!task_work_add(task, &mnt->mnt_rcu, true)) 11939ea459e1SAl Viro return; 11949ea459e1SAl Viro } 11959ea459e1SAl Viro if (llist_add(&mnt->mnt_llist, &delayed_mntput_list)) 11969ea459e1SAl Viro schedule_delayed_work(&delayed_mntput_work, 1); 11979ea459e1SAl Viro return; 11989ea459e1SAl Viro } 11999ea459e1SAl Viro cleanup_mnt(mnt); 1200b3e19d92SNick Piggin } 1201b3e19d92SNick Piggin 1202b3e19d92SNick Piggin void mntput(struct vfsmount *mnt) 1203b3e19d92SNick Piggin { 1204b3e19d92SNick Piggin if (mnt) { 1205863d684fSAl Viro struct mount *m = real_mount(mnt); 1206b3e19d92SNick Piggin /* avoid cacheline pingpong, hope gcc doesn't get "smart" */ 1207863d684fSAl Viro if (unlikely(m->mnt_expiry_mark)) 1208863d684fSAl Viro m->mnt_expiry_mark = 0; 1209863d684fSAl Viro mntput_no_expire(m); 1210b3e19d92SNick Piggin } 1211b3e19d92SNick Piggin } 1212b3e19d92SNick Piggin EXPORT_SYMBOL(mntput); 1213b3e19d92SNick Piggin 1214b3e19d92SNick Piggin struct vfsmount *mntget(struct vfsmount *mnt) 1215b3e19d92SNick Piggin { 1216b3e19d92SNick Piggin if (mnt) 121783adc753SAl Viro mnt_add_count(real_mount(mnt), 1); 1218b3e19d92SNick Piggin return mnt; 1219b3e19d92SNick Piggin } 1220b3e19d92SNick Piggin EXPORT_SYMBOL(mntget); 1221b3e19d92SNick Piggin 1222c6609c0aSIan Kent /* path_is_mountpoint() - Check if path is a mount in the current 1223c6609c0aSIan Kent * namespace. 1224c6609c0aSIan Kent * 1225c6609c0aSIan Kent * d_mountpoint() can only be used reliably to establish if a dentry is 1226c6609c0aSIan Kent * not mounted in any namespace and that common case is handled inline. 1227c6609c0aSIan Kent * d_mountpoint() isn't aware of the possibility there may be multiple 1228c6609c0aSIan Kent * mounts using a given dentry in a different namespace. This function 1229c6609c0aSIan Kent * checks if the passed in path is a mountpoint rather than the dentry 1230c6609c0aSIan Kent * alone. 1231c6609c0aSIan Kent */ 1232c6609c0aSIan Kent bool path_is_mountpoint(const struct path *path) 1233c6609c0aSIan Kent { 1234c6609c0aSIan Kent unsigned seq; 1235c6609c0aSIan Kent bool res; 1236c6609c0aSIan Kent 1237c6609c0aSIan Kent if (!d_mountpoint(path->dentry)) 1238c6609c0aSIan Kent return false; 1239c6609c0aSIan Kent 1240c6609c0aSIan Kent rcu_read_lock(); 1241c6609c0aSIan Kent do { 1242c6609c0aSIan Kent seq = read_seqbegin(&mount_lock); 1243c6609c0aSIan Kent res = __path_is_mountpoint(path); 1244c6609c0aSIan Kent } while (read_seqretry(&mount_lock, seq)); 1245c6609c0aSIan Kent rcu_read_unlock(); 1246c6609c0aSIan Kent 1247c6609c0aSIan Kent return res; 1248c6609c0aSIan Kent } 1249c6609c0aSIan Kent EXPORT_SYMBOL(path_is_mountpoint); 1250c6609c0aSIan Kent 1251ca71cf71SAl Viro struct vfsmount *mnt_clone_internal(const struct path *path) 12527b7b1aceSAl Viro { 12533064c356SAl Viro struct mount *p; 12543064c356SAl Viro p = clone_mnt(real_mount(path->mnt), path->dentry, CL_PRIVATE); 12553064c356SAl Viro if (IS_ERR(p)) 12563064c356SAl Viro return ERR_CAST(p); 12573064c356SAl Viro p->mnt.mnt_flags |= MNT_INTERNAL; 12583064c356SAl Viro return &p->mnt; 12597b7b1aceSAl Viro } 12601da177e4SLinus Torvalds 1261a1a2c409SMiklos Szeredi #ifdef CONFIG_PROC_FS 12620226f492SAl Viro /* iterator; we want it to have access to namespace_sem, thus here... */ 12631da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos) 12641da177e4SLinus Torvalds { 1265ede1bf0dSYann Droneaud struct proc_mounts *p = m->private; 12661da177e4SLinus Torvalds 1267390c6843SRam Pai down_read(&namespace_sem); 1268c7999c36SAl Viro if (p->cached_event == p->ns->event) { 1269c7999c36SAl Viro void *v = p->cached_mount; 1270c7999c36SAl Viro if (*pos == p->cached_index) 1271c7999c36SAl Viro return v; 1272c7999c36SAl Viro if (*pos == p->cached_index + 1) { 1273c7999c36SAl Viro v = seq_list_next(v, &p->ns->list, &p->cached_index); 1274c7999c36SAl Viro return p->cached_mount = v; 1275c7999c36SAl Viro } 1276c7999c36SAl Viro } 1277c7999c36SAl Viro 1278c7999c36SAl Viro p->cached_event = p->ns->event; 1279c7999c36SAl Viro p->cached_mount = seq_list_start(&p->ns->list, *pos); 1280c7999c36SAl Viro p->cached_index = *pos; 1281c7999c36SAl Viro return p->cached_mount; 12821da177e4SLinus Torvalds } 12831da177e4SLinus Torvalds 12841da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos) 12851da177e4SLinus Torvalds { 1286ede1bf0dSYann Droneaud struct proc_mounts *p = m->private; 1287b0765fb8SPavel Emelianov 1288c7999c36SAl Viro p->cached_mount = seq_list_next(v, &p->ns->list, pos); 1289c7999c36SAl Viro p->cached_index = *pos; 1290c7999c36SAl Viro return p->cached_mount; 12911da177e4SLinus Torvalds } 12921da177e4SLinus Torvalds 12931da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v) 12941da177e4SLinus Torvalds { 1295390c6843SRam Pai up_read(&namespace_sem); 12961da177e4SLinus Torvalds } 12971da177e4SLinus Torvalds 12980226f492SAl Viro static int m_show(struct seq_file *m, void *v) 12999f5596afSAl Viro { 1300ede1bf0dSYann Droneaud struct proc_mounts *p = m->private; 13011a4eeaf2SAl Viro struct mount *r = list_entry(v, struct mount, mnt_list); 13020226f492SAl Viro return p->show(m, &r->mnt); 13031da177e4SLinus Torvalds } 13041da177e4SLinus Torvalds 1305a1a2c409SMiklos Szeredi const struct seq_operations mounts_op = { 13061da177e4SLinus Torvalds .start = m_start, 13071da177e4SLinus Torvalds .next = m_next, 13081da177e4SLinus Torvalds .stop = m_stop, 13090226f492SAl Viro .show = m_show, 1310b4629fe2SChuck Lever }; 1311a1a2c409SMiklos Szeredi #endif /* CONFIG_PROC_FS */ 1312b4629fe2SChuck Lever 13131da177e4SLinus Torvalds /** 13141da177e4SLinus Torvalds * may_umount_tree - check if a mount tree is busy 13151da177e4SLinus Torvalds * @mnt: root of mount tree 13161da177e4SLinus Torvalds * 13171da177e4SLinus Torvalds * This is called to check if a tree of mounts has any 13181da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts that are 13191da177e4SLinus Torvalds * busy. 13201da177e4SLinus Torvalds */ 1321909b0a88SAl Viro int may_umount_tree(struct vfsmount *m) 13221da177e4SLinus Torvalds { 1323909b0a88SAl Viro struct mount *mnt = real_mount(m); 132436341f64SRam Pai int actual_refs = 0; 132536341f64SRam Pai int minimum_refs = 0; 1326315fc83eSAl Viro struct mount *p; 1327909b0a88SAl Viro BUG_ON(!m); 13281da177e4SLinus Torvalds 1329b3e19d92SNick Piggin /* write lock needed for mnt_get_count */ 1330719ea2fbSAl Viro lock_mount_hash(); 1331909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 133283adc753SAl Viro actual_refs += mnt_get_count(p); 13331da177e4SLinus Torvalds minimum_refs += 2; 13341da177e4SLinus Torvalds } 1335719ea2fbSAl Viro unlock_mount_hash(); 13361da177e4SLinus Torvalds 13371da177e4SLinus Torvalds if (actual_refs > minimum_refs) 13381da177e4SLinus Torvalds return 0; 1339e3474a8eSIan Kent 1340e3474a8eSIan Kent return 1; 13411da177e4SLinus Torvalds } 13421da177e4SLinus Torvalds 13431da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount_tree); 13441da177e4SLinus Torvalds 13451da177e4SLinus Torvalds /** 13461da177e4SLinus Torvalds * may_umount - check if a mount point is busy 13471da177e4SLinus Torvalds * @mnt: root of mount 13481da177e4SLinus Torvalds * 13491da177e4SLinus Torvalds * This is called to check if a mount point has any 13501da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts. If the 13511da177e4SLinus Torvalds * mount has sub mounts this will return busy 13521da177e4SLinus Torvalds * regardless of whether the sub mounts are busy. 13531da177e4SLinus Torvalds * 13541da177e4SLinus Torvalds * Doesn't take quota and stuff into account. IOW, in some cases it will 13551da177e4SLinus Torvalds * give false negatives. The main reason why it's here is that we need 13561da177e4SLinus Torvalds * a non-destructive way to look for easily umountable filesystems. 13571da177e4SLinus Torvalds */ 13581da177e4SLinus Torvalds int may_umount(struct vfsmount *mnt) 13591da177e4SLinus Torvalds { 1360e3474a8eSIan Kent int ret = 1; 13618ad08d8aSAl Viro down_read(&namespace_sem); 1362719ea2fbSAl Viro lock_mount_hash(); 13631ab59738SAl Viro if (propagate_mount_busy(real_mount(mnt), 2)) 1364e3474a8eSIan Kent ret = 0; 1365719ea2fbSAl Viro unlock_mount_hash(); 13668ad08d8aSAl Viro up_read(&namespace_sem); 1367a05964f3SRam Pai return ret; 13681da177e4SLinus Torvalds } 13691da177e4SLinus Torvalds 13701da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount); 13711da177e4SLinus Torvalds 137238129a13SAl Viro static HLIST_HEAD(unmounted); /* protected by namespace_sem */ 1373e3197d83SAl Viro 137497216be0SAl Viro static void namespace_unlock(void) 13751da177e4SLinus Torvalds { 1376a3b3c562SEric W. Biederman struct hlist_head head; 137797216be0SAl Viro 1378a3b3c562SEric W. Biederman hlist_move_list(&unmounted, &head); 1379a3b3c562SEric W. Biederman 138097216be0SAl Viro up_write(&namespace_sem); 1381a3b3c562SEric W. Biederman 1382a3b3c562SEric W. Biederman if (likely(hlist_empty(&head))) 138397216be0SAl Viro return; 138497216be0SAl Viro 138522cb7405SNeilBrown synchronize_rcu_expedited(); 138648a066e7SAl Viro 138787b95ce0SAl Viro group_pin_kill(&head); 138870fbcdf4SRam Pai } 138970fbcdf4SRam Pai 139097216be0SAl Viro static inline void namespace_lock(void) 1391e3197d83SAl Viro { 139297216be0SAl Viro down_write(&namespace_sem); 1393e3197d83SAl Viro } 1394e3197d83SAl Viro 1395e819f152SEric W. Biederman enum umount_tree_flags { 1396e819f152SEric W. Biederman UMOUNT_SYNC = 1, 1397e819f152SEric W. Biederman UMOUNT_PROPAGATE = 2, 1398e0c9c0afSEric W. Biederman UMOUNT_CONNECTED = 4, 1399e819f152SEric W. Biederman }; 1400f2d0a123SEric W. Biederman 1401f2d0a123SEric W. Biederman static bool disconnect_mount(struct mount *mnt, enum umount_tree_flags how) 1402f2d0a123SEric W. Biederman { 1403f2d0a123SEric W. Biederman /* Leaving mounts connected is only valid for lazy umounts */ 1404f2d0a123SEric W. Biederman if (how & UMOUNT_SYNC) 1405f2d0a123SEric W. Biederman return true; 1406f2d0a123SEric W. Biederman 1407f2d0a123SEric W. Biederman /* A mount without a parent has nothing to be connected to */ 1408f2d0a123SEric W. Biederman if (!mnt_has_parent(mnt)) 1409f2d0a123SEric W. Biederman return true; 1410f2d0a123SEric W. Biederman 1411f2d0a123SEric W. Biederman /* Because the reference counting rules change when mounts are 1412f2d0a123SEric W. Biederman * unmounted and connected, umounted mounts may not be 1413f2d0a123SEric W. Biederman * connected to mounted mounts. 1414f2d0a123SEric W. Biederman */ 1415f2d0a123SEric W. Biederman if (!(mnt->mnt_parent->mnt.mnt_flags & MNT_UMOUNT)) 1416f2d0a123SEric W. Biederman return true; 1417f2d0a123SEric W. Biederman 1418f2d0a123SEric W. Biederman /* Has it been requested that the mount remain connected? */ 1419f2d0a123SEric W. Biederman if (how & UMOUNT_CONNECTED) 1420f2d0a123SEric W. Biederman return false; 1421f2d0a123SEric W. Biederman 1422f2d0a123SEric W. Biederman /* Is the mount locked such that it needs to remain connected? */ 1423f2d0a123SEric W. Biederman if (IS_MNT_LOCKED(mnt)) 1424f2d0a123SEric W. Biederman return false; 1425f2d0a123SEric W. Biederman 1426f2d0a123SEric W. Biederman /* By default disconnect the mount */ 1427f2d0a123SEric W. Biederman return true; 1428f2d0a123SEric W. Biederman } 1429f2d0a123SEric W. Biederman 143099b7db7bSNick Piggin /* 143148a066e7SAl Viro * mount_lock must be held 143299b7db7bSNick Piggin * namespace_sem must be held for write 143399b7db7bSNick Piggin */ 1434e819f152SEric W. Biederman static void umount_tree(struct mount *mnt, enum umount_tree_flags how) 143570fbcdf4SRam Pai { 1436c003b26fSEric W. Biederman LIST_HEAD(tmp_list); 1437315fc83eSAl Viro struct mount *p; 143870fbcdf4SRam Pai 14395d88457eSEric W. Biederman if (how & UMOUNT_PROPAGATE) 14405d88457eSEric W. Biederman propagate_mount_unlock(mnt); 14415d88457eSEric W. Biederman 1442c003b26fSEric W. Biederman /* Gather the mounts to umount */ 1443590ce4bcSEric W. Biederman for (p = mnt; p; p = next_mnt(p, mnt)) { 1444590ce4bcSEric W. Biederman p->mnt.mnt_flags |= MNT_UMOUNT; 1445c003b26fSEric W. Biederman list_move(&p->mnt_list, &tmp_list); 1446590ce4bcSEric W. Biederman } 1447c003b26fSEric W. Biederman 1448411a938bSEric W. Biederman /* Hide the mounts from mnt_mounts */ 1449c003b26fSEric W. Biederman list_for_each_entry(p, &tmp_list, mnt_list) { 1450c003b26fSEric W. Biederman list_del_init(&p->mnt_child); 145138129a13SAl Viro } 145270fbcdf4SRam Pai 1453c003b26fSEric W. Biederman /* Add propogated mounts to the tmp_list */ 1454e819f152SEric W. Biederman if (how & UMOUNT_PROPAGATE) 14557b8a53fdSAl Viro propagate_umount(&tmp_list); 1456a05964f3SRam Pai 1457c003b26fSEric W. Biederman while (!list_empty(&tmp_list)) { 1458d2921684SEric W. Biederman struct mnt_namespace *ns; 1459ce07d891SEric W. Biederman bool disconnect; 1460c003b26fSEric W. Biederman p = list_first_entry(&tmp_list, struct mount, mnt_list); 14616776db3dSAl Viro list_del_init(&p->mnt_expire); 14621a4eeaf2SAl Viro list_del_init(&p->mnt_list); 1463d2921684SEric W. Biederman ns = p->mnt_ns; 1464d2921684SEric W. Biederman if (ns) { 1465d2921684SEric W. Biederman ns->mounts--; 1466d2921684SEric W. Biederman __touch_mnt_namespace(ns); 1467d2921684SEric W. Biederman } 146863d37a84SAl Viro p->mnt_ns = NULL; 1469e819f152SEric W. Biederman if (how & UMOUNT_SYNC) 147048a066e7SAl Viro p->mnt.mnt_flags |= MNT_SYNC_UMOUNT; 147187b95ce0SAl Viro 1472f2d0a123SEric W. Biederman disconnect = disconnect_mount(p, how); 1473ce07d891SEric W. Biederman 1474ce07d891SEric W. Biederman pin_insert_group(&p->mnt_umount, &p->mnt_parent->mnt, 1475ce07d891SEric W. Biederman disconnect ? &unmounted : NULL); 1476676da58dSAl Viro if (mnt_has_parent(p)) { 147781b6b061SAl Viro mnt_add_count(p->mnt_parent, -1); 1478ce07d891SEric W. Biederman if (!disconnect) { 1479ce07d891SEric W. Biederman /* Don't forget about p */ 1480ce07d891SEric W. Biederman list_add_tail(&p->mnt_child, &p->mnt_parent->mnt_mounts); 1481ce07d891SEric W. Biederman } else { 14826a46c573SEric W. Biederman umount_mnt(p); 14837c4b93d8SAl Viro } 1484ce07d891SEric W. Biederman } 14850f0afb1dSAl Viro change_mnt_propagation(p, MS_PRIVATE); 148638129a13SAl Viro } 14871da177e4SLinus Torvalds } 14881da177e4SLinus Torvalds 1489b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt); 1490c35038beSAl Viro 14918d0347f6SDavid Howells static int do_umount_root(struct super_block *sb) 14928d0347f6SDavid Howells { 14938d0347f6SDavid Howells int ret = 0; 14948d0347f6SDavid Howells 14958d0347f6SDavid Howells down_write(&sb->s_umount); 14968d0347f6SDavid Howells if (!sb_rdonly(sb)) { 14978d0347f6SDavid Howells struct fs_context *fc; 14988d0347f6SDavid Howells 14998d0347f6SDavid Howells fc = fs_context_for_reconfigure(sb->s_root, SB_RDONLY, 15008d0347f6SDavid Howells SB_RDONLY); 15018d0347f6SDavid Howells if (IS_ERR(fc)) { 15028d0347f6SDavid Howells ret = PTR_ERR(fc); 15038d0347f6SDavid Howells } else { 15048d0347f6SDavid Howells ret = parse_monolithic_mount_data(fc, NULL); 15058d0347f6SDavid Howells if (!ret) 15068d0347f6SDavid Howells ret = reconfigure_super(fc); 15078d0347f6SDavid Howells put_fs_context(fc); 15088d0347f6SDavid Howells } 15098d0347f6SDavid Howells } 15108d0347f6SDavid Howells up_write(&sb->s_umount); 15118d0347f6SDavid Howells return ret; 15128d0347f6SDavid Howells } 15138d0347f6SDavid Howells 15141ab59738SAl Viro static int do_umount(struct mount *mnt, int flags) 15151da177e4SLinus Torvalds { 15161ab59738SAl Viro struct super_block *sb = mnt->mnt.mnt_sb; 15171da177e4SLinus Torvalds int retval; 15181da177e4SLinus Torvalds 15191ab59738SAl Viro retval = security_sb_umount(&mnt->mnt, flags); 15201da177e4SLinus Torvalds if (retval) 15211da177e4SLinus Torvalds return retval; 15221da177e4SLinus Torvalds 15231da177e4SLinus Torvalds /* 15241da177e4SLinus Torvalds * Allow userspace to request a mountpoint be expired rather than 15251da177e4SLinus Torvalds * unmounting unconditionally. Unmount only happens if: 15261da177e4SLinus Torvalds * (1) the mark is already set (the mark is cleared by mntput()) 15271da177e4SLinus Torvalds * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount] 15281da177e4SLinus Torvalds */ 15291da177e4SLinus Torvalds if (flags & MNT_EXPIRE) { 15301ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt || 15311da177e4SLinus Torvalds flags & (MNT_FORCE | MNT_DETACH)) 15321da177e4SLinus Torvalds return -EINVAL; 15331da177e4SLinus Torvalds 1534b3e19d92SNick Piggin /* 1535b3e19d92SNick Piggin * probably don't strictly need the lock here if we examined 1536b3e19d92SNick Piggin * all race cases, but it's a slowpath. 1537b3e19d92SNick Piggin */ 1538719ea2fbSAl Viro lock_mount_hash(); 153983adc753SAl Viro if (mnt_get_count(mnt) != 2) { 1540719ea2fbSAl Viro unlock_mount_hash(); 15411da177e4SLinus Torvalds return -EBUSY; 1542b3e19d92SNick Piggin } 1543719ea2fbSAl Viro unlock_mount_hash(); 15441da177e4SLinus Torvalds 1545863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1)) 15461da177e4SLinus Torvalds return -EAGAIN; 15471da177e4SLinus Torvalds } 15481da177e4SLinus Torvalds 15491da177e4SLinus Torvalds /* 15501da177e4SLinus Torvalds * If we may have to abort operations to get out of this 15511da177e4SLinus Torvalds * mount, and they will themselves hold resources we must 15521da177e4SLinus Torvalds * allow the fs to do things. In the Unix tradition of 15531da177e4SLinus Torvalds * 'Gee thats tricky lets do it in userspace' the umount_begin 15541da177e4SLinus Torvalds * might fail to complete on the first run through as other tasks 15551da177e4SLinus Torvalds * must return, and the like. Thats for the mount program to worry 15561da177e4SLinus Torvalds * about for the moment. 15571da177e4SLinus Torvalds */ 15581da177e4SLinus Torvalds 155942faad99SAl Viro if (flags & MNT_FORCE && sb->s_op->umount_begin) { 156042faad99SAl Viro sb->s_op->umount_begin(sb); 156142faad99SAl Viro } 15621da177e4SLinus Torvalds 15631da177e4SLinus Torvalds /* 15641da177e4SLinus Torvalds * No sense to grab the lock for this test, but test itself looks 15651da177e4SLinus Torvalds * somewhat bogus. Suggestions for better replacement? 15661da177e4SLinus Torvalds * Ho-hum... In principle, we might treat that as umount + switch 15671da177e4SLinus Torvalds * to rootfs. GC would eventually take care of the old vfsmount. 15681da177e4SLinus Torvalds * Actually it makes sense, especially if rootfs would contain a 15691da177e4SLinus Torvalds * /reboot - static binary that would close all descriptors and 15701da177e4SLinus Torvalds * call reboot(9). Then init(8) could umount root and exec /reboot. 15711da177e4SLinus Torvalds */ 15721ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) { 15731da177e4SLinus Torvalds /* 15741da177e4SLinus Torvalds * Special case for "unmounting" root ... 15751da177e4SLinus Torvalds * we just try to remount it readonly. 15761da177e4SLinus Torvalds */ 1577bc6155d1SEric W. Biederman if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN)) 1578a1480dccSAndy Lutomirski return -EPERM; 15798d0347f6SDavid Howells return do_umount_root(sb); 15801da177e4SLinus Torvalds } 15811da177e4SLinus Torvalds 158297216be0SAl Viro namespace_lock(); 1583719ea2fbSAl Viro lock_mount_hash(); 15841da177e4SLinus Torvalds 158525d202edSEric W. Biederman /* Recheck MNT_LOCKED with the locks held */ 158625d202edSEric W. Biederman retval = -EINVAL; 158725d202edSEric W. Biederman if (mnt->mnt.mnt_flags & MNT_LOCKED) 158825d202edSEric W. Biederman goto out; 158925d202edSEric W. Biederman 159025d202edSEric W. Biederman event++; 159148a066e7SAl Viro if (flags & MNT_DETACH) { 159248a066e7SAl Viro if (!list_empty(&mnt->mnt_list)) 1593e819f152SEric W. Biederman umount_tree(mnt, UMOUNT_PROPAGATE); 159448a066e7SAl Viro retval = 0; 159548a066e7SAl Viro } else { 1596b54b9be7SAl Viro shrink_submounts(mnt); 15971da177e4SLinus Torvalds retval = -EBUSY; 159848a066e7SAl Viro if (!propagate_mount_busy(mnt, 2)) { 15991a4eeaf2SAl Viro if (!list_empty(&mnt->mnt_list)) 1600e819f152SEric W. Biederman umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC); 16011da177e4SLinus Torvalds retval = 0; 16021da177e4SLinus Torvalds } 160348a066e7SAl Viro } 160425d202edSEric W. Biederman out: 1605719ea2fbSAl Viro unlock_mount_hash(); 1606e3197d83SAl Viro namespace_unlock(); 16071da177e4SLinus Torvalds return retval; 16081da177e4SLinus Torvalds } 16091da177e4SLinus Torvalds 16101da177e4SLinus Torvalds /* 161180b5dce8SEric W. Biederman * __detach_mounts - lazily unmount all mounts on the specified dentry 161280b5dce8SEric W. Biederman * 161380b5dce8SEric W. Biederman * During unlink, rmdir, and d_drop it is possible to loose the path 161480b5dce8SEric W. Biederman * to an existing mountpoint, and wind up leaking the mount. 161580b5dce8SEric W. Biederman * detach_mounts allows lazily unmounting those mounts instead of 161680b5dce8SEric W. Biederman * leaking them. 161780b5dce8SEric W. Biederman * 161880b5dce8SEric W. Biederman * The caller may hold dentry->d_inode->i_mutex. 161980b5dce8SEric W. Biederman */ 162080b5dce8SEric W. Biederman void __detach_mounts(struct dentry *dentry) 162180b5dce8SEric W. Biederman { 162280b5dce8SEric W. Biederman struct mountpoint *mp; 162380b5dce8SEric W. Biederman struct mount *mnt; 162480b5dce8SEric W. Biederman 162580b5dce8SEric W. Biederman namespace_lock(); 16263895dbf8SEric W. Biederman lock_mount_hash(); 162780b5dce8SEric W. Biederman mp = lookup_mountpoint(dentry); 1628f53e5797SEric W. Biederman if (IS_ERR_OR_NULL(mp)) 162980b5dce8SEric W. Biederman goto out_unlock; 163080b5dce8SEric W. Biederman 1631e06b933eSAndrey Ulanov event++; 163280b5dce8SEric W. Biederman while (!hlist_empty(&mp->m_list)) { 163380b5dce8SEric W. Biederman mnt = hlist_entry(mp->m_list.first, struct mount, mnt_mp_list); 1634ce07d891SEric W. Biederman if (mnt->mnt.mnt_flags & MNT_UMOUNT) { 1635fe78fcc8SEric W. Biederman hlist_add_head(&mnt->mnt_umount.s_list, &unmounted); 1636fe78fcc8SEric W. Biederman umount_mnt(mnt); 1637ce07d891SEric W. Biederman } 1638e0c9c0afSEric W. Biederman else umount_tree(mnt, UMOUNT_CONNECTED); 163980b5dce8SEric W. Biederman } 164080b5dce8SEric W. Biederman put_mountpoint(mp); 164180b5dce8SEric W. Biederman out_unlock: 16423895dbf8SEric W. Biederman unlock_mount_hash(); 164380b5dce8SEric W. Biederman namespace_unlock(); 164480b5dce8SEric W. Biederman } 164580b5dce8SEric W. Biederman 164680b5dce8SEric W. Biederman /* 16479b40bc90SAl Viro * Is the caller allowed to modify his namespace? 16489b40bc90SAl Viro */ 16499b40bc90SAl Viro static inline bool may_mount(void) 16509b40bc90SAl Viro { 16519b40bc90SAl Viro return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN); 16529b40bc90SAl Viro } 16539b40bc90SAl Viro 16549e8925b6SJeff Layton static inline bool may_mandlock(void) 16559e8925b6SJeff Layton { 16569e8925b6SJeff Layton #ifndef CONFIG_MANDATORY_FILE_LOCKING 16579e8925b6SJeff Layton return false; 16589e8925b6SJeff Layton #endif 165995ace754SEric W. Biederman return capable(CAP_SYS_ADMIN); 16609e8925b6SJeff Layton } 16619e8925b6SJeff Layton 16629b40bc90SAl Viro /* 16631da177e4SLinus Torvalds * Now umount can handle mount points as well as block devices. 16641da177e4SLinus Torvalds * This is important for filesystems which use unnamed block devices. 16651da177e4SLinus Torvalds * 16661da177e4SLinus Torvalds * We now support a flag for forced unmount like the other 'big iron' 16671da177e4SLinus Torvalds * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD 16681da177e4SLinus Torvalds */ 16691da177e4SLinus Torvalds 16703a18ef5cSDominik Brodowski int ksys_umount(char __user *name, int flags) 16711da177e4SLinus Torvalds { 16722d8f3038SAl Viro struct path path; 1673900148dcSAl Viro struct mount *mnt; 16741da177e4SLinus Torvalds int retval; 1675db1f05bbSMiklos Szeredi int lookup_flags = 0; 16761da177e4SLinus Torvalds 1677db1f05bbSMiklos Szeredi if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW)) 1678db1f05bbSMiklos Szeredi return -EINVAL; 1679db1f05bbSMiklos Szeredi 16809b40bc90SAl Viro if (!may_mount()) 16819b40bc90SAl Viro return -EPERM; 16829b40bc90SAl Viro 1683db1f05bbSMiklos Szeredi if (!(flags & UMOUNT_NOFOLLOW)) 1684db1f05bbSMiklos Szeredi lookup_flags |= LOOKUP_FOLLOW; 1685db1f05bbSMiklos Szeredi 168657d46577SRichard Guy Briggs lookup_flags |= LOOKUP_NO_EVAL; 168757d46577SRichard Guy Briggs 1688197df04cSAl Viro retval = user_path_mountpoint_at(AT_FDCWD, name, lookup_flags, &path); 16891da177e4SLinus Torvalds if (retval) 16901da177e4SLinus Torvalds goto out; 1691900148dcSAl Viro mnt = real_mount(path.mnt); 16921da177e4SLinus Torvalds retval = -EINVAL; 16932d8f3038SAl Viro if (path.dentry != path.mnt->mnt_root) 16941da177e4SLinus Torvalds goto dput_and_out; 1695143c8c91SAl Viro if (!check_mnt(mnt)) 16961da177e4SLinus Torvalds goto dput_and_out; 169725d202edSEric W. Biederman if (mnt->mnt.mnt_flags & MNT_LOCKED) /* Check optimistically */ 16985ff9d8a6SEric W. Biederman goto dput_and_out; 1699b2f5d4dcSEric W. Biederman retval = -EPERM; 1700b2f5d4dcSEric W. Biederman if (flags & MNT_FORCE && !capable(CAP_SYS_ADMIN)) 1701b2f5d4dcSEric W. Biederman goto dput_and_out; 17021da177e4SLinus Torvalds 1703900148dcSAl Viro retval = do_umount(mnt, flags); 17041da177e4SLinus Torvalds dput_and_out: 1705429731b1SJan Blunck /* we mustn't call path_put() as that would clear mnt_expiry_mark */ 17062d8f3038SAl Viro dput(path.dentry); 1707900148dcSAl Viro mntput_no_expire(mnt); 17081da177e4SLinus Torvalds out: 17091da177e4SLinus Torvalds return retval; 17101da177e4SLinus Torvalds } 17111da177e4SLinus Torvalds 17123a18ef5cSDominik Brodowski SYSCALL_DEFINE2(umount, char __user *, name, int, flags) 17133a18ef5cSDominik Brodowski { 17143a18ef5cSDominik Brodowski return ksys_umount(name, flags); 17153a18ef5cSDominik Brodowski } 17163a18ef5cSDominik Brodowski 17171da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT 17181da177e4SLinus Torvalds 17191da177e4SLinus Torvalds /* 17201da177e4SLinus Torvalds * The 2.0 compatible umount. No flags. 17211da177e4SLinus Torvalds */ 1722bdc480e3SHeiko Carstens SYSCALL_DEFINE1(oldumount, char __user *, name) 17231da177e4SLinus Torvalds { 17243a18ef5cSDominik Brodowski return ksys_umount(name, 0); 17251da177e4SLinus Torvalds } 17261da177e4SLinus Torvalds 17271da177e4SLinus Torvalds #endif 17281da177e4SLinus Torvalds 17294ce5d2b1SEric W. Biederman static bool is_mnt_ns_file(struct dentry *dentry) 17308823c079SEric W. Biederman { 17314ce5d2b1SEric W. Biederman /* Is this a proxy for a mount namespace? */ 1732e149ed2bSAl Viro return dentry->d_op == &ns_dentry_operations && 1733e149ed2bSAl Viro dentry->d_fsdata == &mntns_operations; 17344ce5d2b1SEric W. Biederman } 17354ce5d2b1SEric W. Biederman 173658be2825SAl Viro struct mnt_namespace *to_mnt_ns(struct ns_common *ns) 173758be2825SAl Viro { 173858be2825SAl Viro return container_of(ns, struct mnt_namespace, ns); 173958be2825SAl Viro } 174058be2825SAl Viro 17414ce5d2b1SEric W. Biederman static bool mnt_ns_loop(struct dentry *dentry) 17424ce5d2b1SEric W. Biederman { 17434ce5d2b1SEric W. Biederman /* Could bind mounting the mount namespace inode cause a 17444ce5d2b1SEric W. Biederman * mount namespace loop? 17454ce5d2b1SEric W. Biederman */ 17464ce5d2b1SEric W. Biederman struct mnt_namespace *mnt_ns; 17474ce5d2b1SEric W. Biederman if (!is_mnt_ns_file(dentry)) 17484ce5d2b1SEric W. Biederman return false; 17494ce5d2b1SEric W. Biederman 1750f77c8014SAl Viro mnt_ns = to_mnt_ns(get_proc_ns(dentry->d_inode)); 17518823c079SEric W. Biederman return current->nsproxy->mnt_ns->seq >= mnt_ns->seq; 17528823c079SEric W. Biederman } 17538823c079SEric W. Biederman 175487129cc0SAl Viro struct mount *copy_tree(struct mount *mnt, struct dentry *dentry, 175536341f64SRam Pai int flag) 17561da177e4SLinus Torvalds { 175784d17192SAl Viro struct mount *res, *p, *q, *r, *parent; 17581da177e4SLinus Torvalds 17594ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt)) 17604ce5d2b1SEric W. Biederman return ERR_PTR(-EINVAL); 17614ce5d2b1SEric W. Biederman 17624ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry)) 1763be34d1a3SDavid Howells return ERR_PTR(-EINVAL); 17649676f0c6SRam Pai 176536341f64SRam Pai res = q = clone_mnt(mnt, dentry, flag); 1766be34d1a3SDavid Howells if (IS_ERR(q)) 1767be34d1a3SDavid Howells return q; 1768be34d1a3SDavid Howells 1769a73324daSAl Viro q->mnt_mountpoint = mnt->mnt_mountpoint; 17701da177e4SLinus Torvalds 17711da177e4SLinus Torvalds p = mnt; 17726b41d536SAl Viro list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) { 1773315fc83eSAl Viro struct mount *s; 17747ec02ef1SJan Blunck if (!is_subdir(r->mnt_mountpoint, dentry)) 17751da177e4SLinus Torvalds continue; 17761da177e4SLinus Torvalds 1777909b0a88SAl Viro for (s = r; s; s = next_mnt(s, r)) { 17784ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_UNBINDABLE) && 17794ce5d2b1SEric W. Biederman IS_MNT_UNBINDABLE(s)) { 1780df7342b2SEric W. Biederman if (s->mnt.mnt_flags & MNT_LOCKED) { 1781df7342b2SEric W. Biederman /* Both unbindable and locked. */ 1782df7342b2SEric W. Biederman q = ERR_PTR(-EPERM); 1783df7342b2SEric W. Biederman goto out; 1784df7342b2SEric W. Biederman } else { 17854ce5d2b1SEric W. Biederman s = skip_mnt_tree(s); 17864ce5d2b1SEric W. Biederman continue; 17874ce5d2b1SEric W. Biederman } 1788df7342b2SEric W. Biederman } 17894ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_MNT_NS_FILE) && 17904ce5d2b1SEric W. Biederman is_mnt_ns_file(s->mnt.mnt_root)) { 17919676f0c6SRam Pai s = skip_mnt_tree(s); 17929676f0c6SRam Pai continue; 17939676f0c6SRam Pai } 17940714a533SAl Viro while (p != s->mnt_parent) { 17950714a533SAl Viro p = p->mnt_parent; 17960714a533SAl Viro q = q->mnt_parent; 17971da177e4SLinus Torvalds } 179887129cc0SAl Viro p = s; 179984d17192SAl Viro parent = q; 180087129cc0SAl Viro q = clone_mnt(p, p->mnt.mnt_root, flag); 1801be34d1a3SDavid Howells if (IS_ERR(q)) 1802be34d1a3SDavid Howells goto out; 1803719ea2fbSAl Viro lock_mount_hash(); 18041a4eeaf2SAl Viro list_add_tail(&q->mnt_list, &res->mnt_list); 18051064f874SEric W. Biederman attach_mnt(q, parent, p->mnt_mp); 1806719ea2fbSAl Viro unlock_mount_hash(); 18071da177e4SLinus Torvalds } 18081da177e4SLinus Torvalds } 18091da177e4SLinus Torvalds return res; 1810be34d1a3SDavid Howells out: 18111da177e4SLinus Torvalds if (res) { 1812719ea2fbSAl Viro lock_mount_hash(); 1813e819f152SEric W. Biederman umount_tree(res, UMOUNT_SYNC); 1814719ea2fbSAl Viro unlock_mount_hash(); 18151da177e4SLinus Torvalds } 1816be34d1a3SDavid Howells return q; 18171da177e4SLinus Torvalds } 18181da177e4SLinus Torvalds 1819be34d1a3SDavid Howells /* Caller should check returned pointer for errors */ 1820be34d1a3SDavid Howells 1821ca71cf71SAl Viro struct vfsmount *collect_mounts(const struct path *path) 18228aec0809SAl Viro { 1823cb338d06SAl Viro struct mount *tree; 182497216be0SAl Viro namespace_lock(); 1825cd4a4017SEric W. Biederman if (!check_mnt(real_mount(path->mnt))) 1826cd4a4017SEric W. Biederman tree = ERR_PTR(-EINVAL); 1827cd4a4017SEric W. Biederman else 182887129cc0SAl Viro tree = copy_tree(real_mount(path->mnt), path->dentry, 182987129cc0SAl Viro CL_COPY_ALL | CL_PRIVATE); 1830328e6d90SAl Viro namespace_unlock(); 1831be34d1a3SDavid Howells if (IS_ERR(tree)) 183252e220d3SDan Carpenter return ERR_CAST(tree); 1833be34d1a3SDavid Howells return &tree->mnt; 18348aec0809SAl Viro } 18358aec0809SAl Viro 1836a07b2000SAl Viro static void free_mnt_ns(struct mnt_namespace *); 1837a07b2000SAl Viro static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *, bool); 1838a07b2000SAl Viro 1839a07b2000SAl Viro void dissolve_on_fput(struct vfsmount *mnt) 1840a07b2000SAl Viro { 1841a07b2000SAl Viro struct mnt_namespace *ns; 1842a07b2000SAl Viro namespace_lock(); 1843a07b2000SAl Viro lock_mount_hash(); 1844a07b2000SAl Viro ns = real_mount(mnt)->mnt_ns; 184544dfd84aSDavid Howells if (ns) { 184644dfd84aSDavid Howells if (is_anon_ns(ns)) 1847a07b2000SAl Viro umount_tree(real_mount(mnt), UMOUNT_CONNECTED); 184844dfd84aSDavid Howells else 184944dfd84aSDavid Howells ns = NULL; 185044dfd84aSDavid Howells } 1851a07b2000SAl Viro unlock_mount_hash(); 1852a07b2000SAl Viro namespace_unlock(); 185344dfd84aSDavid Howells if (ns) 1854a07b2000SAl Viro free_mnt_ns(ns); 1855a07b2000SAl Viro } 1856a07b2000SAl Viro 18578aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt) 18588aec0809SAl Viro { 185997216be0SAl Viro namespace_lock(); 1860719ea2fbSAl Viro lock_mount_hash(); 18619c8e0a1bSEric W. Biederman umount_tree(real_mount(mnt), 0); 1862719ea2fbSAl Viro unlock_mount_hash(); 18633ab6abeeSAl Viro namespace_unlock(); 18648aec0809SAl Viro } 18658aec0809SAl Viro 1866c771d683SMiklos Szeredi /** 1867c771d683SMiklos Szeredi * clone_private_mount - create a private clone of a path 1868c771d683SMiklos Szeredi * 1869c771d683SMiklos Szeredi * This creates a new vfsmount, which will be the clone of @path. The new will 1870c771d683SMiklos Szeredi * not be attached anywhere in the namespace and will be private (i.e. changes 1871c771d683SMiklos Szeredi * to the originating mount won't be propagated into this). 1872c771d683SMiklos Szeredi * 1873c771d683SMiklos Szeredi * Release with mntput(). 1874c771d683SMiklos Szeredi */ 1875ca71cf71SAl Viro struct vfsmount *clone_private_mount(const struct path *path) 1876c771d683SMiklos Szeredi { 1877c771d683SMiklos Szeredi struct mount *old_mnt = real_mount(path->mnt); 1878c771d683SMiklos Szeredi struct mount *new_mnt; 1879c771d683SMiklos Szeredi 1880c771d683SMiklos Szeredi if (IS_MNT_UNBINDABLE(old_mnt)) 1881c771d683SMiklos Szeredi return ERR_PTR(-EINVAL); 1882c771d683SMiklos Szeredi 1883c771d683SMiklos Szeredi new_mnt = clone_mnt(old_mnt, path->dentry, CL_PRIVATE); 1884c771d683SMiklos Szeredi if (IS_ERR(new_mnt)) 1885c771d683SMiklos Szeredi return ERR_CAST(new_mnt); 1886c771d683SMiklos Szeredi 1887c771d683SMiklos Szeredi return &new_mnt->mnt; 1888c771d683SMiklos Szeredi } 1889c771d683SMiklos Szeredi EXPORT_SYMBOL_GPL(clone_private_mount); 1890c771d683SMiklos Szeredi 18911f707137SAl Viro int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg, 18921f707137SAl Viro struct vfsmount *root) 18931f707137SAl Viro { 18941a4eeaf2SAl Viro struct mount *mnt; 18951f707137SAl Viro int res = f(root, arg); 18961f707137SAl Viro if (res) 18971f707137SAl Viro return res; 18981a4eeaf2SAl Viro list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) { 18991a4eeaf2SAl Viro res = f(&mnt->mnt, arg); 19001f707137SAl Viro if (res) 19011f707137SAl Viro return res; 19021f707137SAl Viro } 19031f707137SAl Viro return 0; 19041f707137SAl Viro } 19051f707137SAl Viro 19063bd045ccSAl Viro static void lock_mnt_tree(struct mount *mnt) 19073bd045ccSAl Viro { 19083bd045ccSAl Viro struct mount *p; 19093bd045ccSAl Viro 19103bd045ccSAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 19113bd045ccSAl Viro int flags = p->mnt.mnt_flags; 19123bd045ccSAl Viro /* Don't allow unprivileged users to change mount flags */ 19133bd045ccSAl Viro flags |= MNT_LOCK_ATIME; 19143bd045ccSAl Viro 19153bd045ccSAl Viro if (flags & MNT_READONLY) 19163bd045ccSAl Viro flags |= MNT_LOCK_READONLY; 19173bd045ccSAl Viro 19183bd045ccSAl Viro if (flags & MNT_NODEV) 19193bd045ccSAl Viro flags |= MNT_LOCK_NODEV; 19203bd045ccSAl Viro 19213bd045ccSAl Viro if (flags & MNT_NOSUID) 19223bd045ccSAl Viro flags |= MNT_LOCK_NOSUID; 19233bd045ccSAl Viro 19243bd045ccSAl Viro if (flags & MNT_NOEXEC) 19253bd045ccSAl Viro flags |= MNT_LOCK_NOEXEC; 19263bd045ccSAl Viro /* Don't allow unprivileged users to reveal what is under a mount */ 19273bd045ccSAl Viro if (list_empty(&p->mnt_expire)) 19283bd045ccSAl Viro flags |= MNT_LOCKED; 19293bd045ccSAl Viro p->mnt.mnt_flags = flags; 19303bd045ccSAl Viro } 19313bd045ccSAl Viro } 19323bd045ccSAl Viro 19334b8b21f4SAl Viro static void cleanup_group_ids(struct mount *mnt, struct mount *end) 1934719f5d7fSMiklos Szeredi { 1935315fc83eSAl Viro struct mount *p; 1936719f5d7fSMiklos Szeredi 1937909b0a88SAl Viro for (p = mnt; p != end; p = next_mnt(p, mnt)) { 1938fc7be130SAl Viro if (p->mnt_group_id && !IS_MNT_SHARED(p)) 19394b8b21f4SAl Viro mnt_release_group_id(p); 1940719f5d7fSMiklos Szeredi } 1941719f5d7fSMiklos Szeredi } 1942719f5d7fSMiklos Szeredi 19434b8b21f4SAl Viro static int invent_group_ids(struct mount *mnt, bool recurse) 1944719f5d7fSMiklos Szeredi { 1945315fc83eSAl Viro struct mount *p; 1946719f5d7fSMiklos Szeredi 1947909b0a88SAl Viro for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) { 1948fc7be130SAl Viro if (!p->mnt_group_id && !IS_MNT_SHARED(p)) { 19494b8b21f4SAl Viro int err = mnt_alloc_group_id(p); 1950719f5d7fSMiklos Szeredi if (err) { 19514b8b21f4SAl Viro cleanup_group_ids(mnt, p); 1952719f5d7fSMiklos Szeredi return err; 1953719f5d7fSMiklos Szeredi } 1954719f5d7fSMiklos Szeredi } 1955719f5d7fSMiklos Szeredi } 1956719f5d7fSMiklos Szeredi 1957719f5d7fSMiklos Szeredi return 0; 1958719f5d7fSMiklos Szeredi } 1959719f5d7fSMiklos Szeredi 1960d2921684SEric W. Biederman int count_mounts(struct mnt_namespace *ns, struct mount *mnt) 1961d2921684SEric W. Biederman { 1962d2921684SEric W. Biederman unsigned int max = READ_ONCE(sysctl_mount_max); 1963d2921684SEric W. Biederman unsigned int mounts = 0, old, pending, sum; 1964d2921684SEric W. Biederman struct mount *p; 1965d2921684SEric W. Biederman 1966d2921684SEric W. Biederman for (p = mnt; p; p = next_mnt(p, mnt)) 1967d2921684SEric W. Biederman mounts++; 1968d2921684SEric W. Biederman 1969d2921684SEric W. Biederman old = ns->mounts; 1970d2921684SEric W. Biederman pending = ns->pending_mounts; 1971d2921684SEric W. Biederman sum = old + pending; 1972d2921684SEric W. Biederman if ((old > sum) || 1973d2921684SEric W. Biederman (pending > sum) || 1974d2921684SEric W. Biederman (max < sum) || 1975d2921684SEric W. Biederman (mounts > (max - sum))) 1976d2921684SEric W. Biederman return -ENOSPC; 1977d2921684SEric W. Biederman 1978d2921684SEric W. Biederman ns->pending_mounts = pending + mounts; 1979d2921684SEric W. Biederman return 0; 1980d2921684SEric W. Biederman } 1981d2921684SEric W. Biederman 1982b90fa9aeSRam Pai /* 1983b90fa9aeSRam Pai * @source_mnt : mount tree to be attached 1984b90fa9aeSRam Pai * @nd : place the mount tree @source_mnt is attached 198521444403SRam Pai * @parent_nd : if non-null, detach the source_mnt from its parent and 198621444403SRam Pai * store the parent mount and mountpoint dentry. 198721444403SRam Pai * (done when source_mnt is moved) 1988b90fa9aeSRam Pai * 1989b90fa9aeSRam Pai * NOTE: in the table below explains the semantics when a source mount 1990b90fa9aeSRam Pai * of a given type is attached to a destination mount of a given type. 19919676f0c6SRam Pai * --------------------------------------------------------------------------- 1992b90fa9aeSRam Pai * | BIND MOUNT OPERATION | 19939676f0c6SRam Pai * |************************************************************************** 19949676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 19959676f0c6SRam Pai * | dest | | | | | 19969676f0c6SRam Pai * | | | | | | | 19979676f0c6SRam Pai * | v | | | | | 19989676f0c6SRam Pai * |************************************************************************** 19999676f0c6SRam Pai * | shared | shared (++) | shared (+) | shared(+++)| invalid | 20005afe0022SRam Pai * | | | | | | 20019676f0c6SRam Pai * |non-shared| shared (+) | private | slave (*) | invalid | 20029676f0c6SRam Pai * *************************************************************************** 2003b90fa9aeSRam Pai * A bind operation clones the source mount and mounts the clone on the 2004b90fa9aeSRam Pai * destination mount. 2005b90fa9aeSRam Pai * 2006b90fa9aeSRam Pai * (++) the cloned mount is propagated to all the mounts in the propagation 2007b90fa9aeSRam Pai * tree of the destination mount and the cloned mount is added to 2008b90fa9aeSRam Pai * the peer group of the source mount. 2009b90fa9aeSRam Pai * (+) the cloned mount is created under the destination mount and is marked 2010b90fa9aeSRam Pai * as shared. The cloned mount is added to the peer group of the source 2011b90fa9aeSRam Pai * mount. 20125afe0022SRam Pai * (+++) the mount is propagated to all the mounts in the propagation tree 20135afe0022SRam Pai * of the destination mount and the cloned mount is made slave 20145afe0022SRam Pai * of the same master as that of the source mount. The cloned mount 20155afe0022SRam Pai * is marked as 'shared and slave'. 20165afe0022SRam Pai * (*) the cloned mount is made a slave of the same master as that of the 20175afe0022SRam Pai * source mount. 20185afe0022SRam Pai * 20199676f0c6SRam Pai * --------------------------------------------------------------------------- 202021444403SRam Pai * | MOVE MOUNT OPERATION | 20219676f0c6SRam Pai * |************************************************************************** 20229676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 20239676f0c6SRam Pai * | dest | | | | | 20249676f0c6SRam Pai * | | | | | | | 20259676f0c6SRam Pai * | v | | | | | 20269676f0c6SRam Pai * |************************************************************************** 20279676f0c6SRam Pai * | shared | shared (+) | shared (+) | shared(+++) | invalid | 20285afe0022SRam Pai * | | | | | | 20299676f0c6SRam Pai * |non-shared| shared (+*) | private | slave (*) | unbindable | 20309676f0c6SRam Pai * *************************************************************************** 20315afe0022SRam Pai * 20325afe0022SRam Pai * (+) the mount is moved to the destination. And is then propagated to 20335afe0022SRam Pai * all the mounts in the propagation tree of the destination mount. 203421444403SRam Pai * (+*) the mount is moved to the destination. 20355afe0022SRam Pai * (+++) the mount is moved to the destination and is then propagated to 20365afe0022SRam Pai * all the mounts belonging to the destination mount's propagation tree. 20375afe0022SRam Pai * the mount is marked as 'shared and slave'. 20385afe0022SRam Pai * (*) the mount continues to be a slave at the new location. 2039b90fa9aeSRam Pai * 2040b90fa9aeSRam Pai * if the source mount is a tree, the operations explained above is 2041b90fa9aeSRam Pai * applied to each mount in the tree. 2042b90fa9aeSRam Pai * Must be called without spinlocks held, since this function can sleep 2043b90fa9aeSRam Pai * in allocations. 2044b90fa9aeSRam Pai */ 20450fb54e50SAl Viro static int attach_recursive_mnt(struct mount *source_mnt, 204684d17192SAl Viro struct mount *dest_mnt, 204784d17192SAl Viro struct mountpoint *dest_mp, 204884d17192SAl Viro struct path *parent_path) 2049b90fa9aeSRam Pai { 20503bd045ccSAl Viro struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns; 205138129a13SAl Viro HLIST_HEAD(tree_list); 2052d2921684SEric W. Biederman struct mnt_namespace *ns = dest_mnt->mnt_ns; 20531064f874SEric W. Biederman struct mountpoint *smp; 2054315fc83eSAl Viro struct mount *child, *p; 205538129a13SAl Viro struct hlist_node *n; 2056719f5d7fSMiklos Szeredi int err; 2057b90fa9aeSRam Pai 20581064f874SEric W. Biederman /* Preallocate a mountpoint in case the new mounts need 20591064f874SEric W. Biederman * to be tucked under other mounts. 20601064f874SEric W. Biederman */ 20611064f874SEric W. Biederman smp = get_mountpoint(source_mnt->mnt.mnt_root); 20621064f874SEric W. Biederman if (IS_ERR(smp)) 20631064f874SEric W. Biederman return PTR_ERR(smp); 20641064f874SEric W. Biederman 2065d2921684SEric W. Biederman /* Is there space to add these mounts to the mount namespace? */ 2066d2921684SEric W. Biederman if (!parent_path) { 2067d2921684SEric W. Biederman err = count_mounts(ns, source_mnt); 2068d2921684SEric W. Biederman if (err) 2069d2921684SEric W. Biederman goto out; 2070d2921684SEric W. Biederman } 2071d2921684SEric W. Biederman 2072fc7be130SAl Viro if (IS_MNT_SHARED(dest_mnt)) { 20730fb54e50SAl Viro err = invent_group_ids(source_mnt, true); 2074719f5d7fSMiklos Szeredi if (err) 2075719f5d7fSMiklos Szeredi goto out; 207684d17192SAl Viro err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list); 2077f2ebb3a9SAl Viro lock_mount_hash(); 2078719f5d7fSMiklos Szeredi if (err) 2079719f5d7fSMiklos Szeredi goto out_cleanup_ids; 2080909b0a88SAl Viro for (p = source_mnt; p; p = next_mnt(p, source_mnt)) 20810f0afb1dSAl Viro set_mnt_shared(p); 20820b1b901bSAl Viro } else { 20830b1b901bSAl Viro lock_mount_hash(); 2084b90fa9aeSRam Pai } 20851a390689SAl Viro if (parent_path) { 20860fb54e50SAl Viro detach_mnt(source_mnt, parent_path); 208784d17192SAl Viro attach_mnt(source_mnt, dest_mnt, dest_mp); 2088143c8c91SAl Viro touch_mnt_namespace(source_mnt->mnt_ns); 208921444403SRam Pai } else { 209044dfd84aSDavid Howells if (source_mnt->mnt_ns) { 209144dfd84aSDavid Howells /* move from anon - the caller will destroy */ 209244dfd84aSDavid Howells list_del_init(&source_mnt->mnt_ns->list); 209344dfd84aSDavid Howells } 209484d17192SAl Viro mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt); 20951064f874SEric W. Biederman commit_tree(source_mnt); 209621444403SRam Pai } 2097b90fa9aeSRam Pai 209838129a13SAl Viro hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) { 20991d6a32acSAl Viro struct mount *q; 210038129a13SAl Viro hlist_del_init(&child->mnt_hash); 21011064f874SEric W. Biederman q = __lookup_mnt(&child->mnt_parent->mnt, 21021d6a32acSAl Viro child->mnt_mountpoint); 21031064f874SEric W. Biederman if (q) 21041064f874SEric W. Biederman mnt_change_mountpoint(child, smp, q); 21053bd045ccSAl Viro /* Notice when we are propagating across user namespaces */ 21063bd045ccSAl Viro if (child->mnt_parent->mnt_ns->user_ns != user_ns) 21073bd045ccSAl Viro lock_mnt_tree(child); 21081064f874SEric W. Biederman commit_tree(child); 2109b90fa9aeSRam Pai } 21101064f874SEric W. Biederman put_mountpoint(smp); 2111719ea2fbSAl Viro unlock_mount_hash(); 211299b7db7bSNick Piggin 2113b90fa9aeSRam Pai return 0; 2114719f5d7fSMiklos Szeredi 2115719f5d7fSMiklos Szeredi out_cleanup_ids: 2116f2ebb3a9SAl Viro while (!hlist_empty(&tree_list)) { 2117f2ebb3a9SAl Viro child = hlist_entry(tree_list.first, struct mount, mnt_hash); 2118d2921684SEric W. Biederman child->mnt_parent->mnt_ns->pending_mounts = 0; 2119e819f152SEric W. Biederman umount_tree(child, UMOUNT_SYNC); 2120f2ebb3a9SAl Viro } 2121f2ebb3a9SAl Viro unlock_mount_hash(); 21220fb54e50SAl Viro cleanup_group_ids(source_mnt, NULL); 2123719f5d7fSMiklos Szeredi out: 2124d2921684SEric W. Biederman ns->pending_mounts = 0; 21251064f874SEric W. Biederman 21261064f874SEric W. Biederman read_seqlock_excl(&mount_lock); 21271064f874SEric W. Biederman put_mountpoint(smp); 21281064f874SEric W. Biederman read_sequnlock_excl(&mount_lock); 21291064f874SEric W. Biederman 2130719f5d7fSMiklos Szeredi return err; 2131b90fa9aeSRam Pai } 2132b90fa9aeSRam Pai 213384d17192SAl Viro static struct mountpoint *lock_mount(struct path *path) 2134b12cea91SAl Viro { 2135b12cea91SAl Viro struct vfsmount *mnt; 213684d17192SAl Viro struct dentry *dentry = path->dentry; 2137b12cea91SAl Viro retry: 21385955102cSAl Viro inode_lock(dentry->d_inode); 213984d17192SAl Viro if (unlikely(cant_mount(dentry))) { 21405955102cSAl Viro inode_unlock(dentry->d_inode); 214184d17192SAl Viro return ERR_PTR(-ENOENT); 2142b12cea91SAl Viro } 214397216be0SAl Viro namespace_lock(); 2144b12cea91SAl Viro mnt = lookup_mnt(path); 214584d17192SAl Viro if (likely(!mnt)) { 21463895dbf8SEric W. Biederman struct mountpoint *mp = get_mountpoint(dentry); 214784d17192SAl Viro if (IS_ERR(mp)) { 214897216be0SAl Viro namespace_unlock(); 21495955102cSAl Viro inode_unlock(dentry->d_inode); 215084d17192SAl Viro return mp; 215184d17192SAl Viro } 215284d17192SAl Viro return mp; 215384d17192SAl Viro } 215497216be0SAl Viro namespace_unlock(); 21555955102cSAl Viro inode_unlock(path->dentry->d_inode); 2156b12cea91SAl Viro path_put(path); 2157b12cea91SAl Viro path->mnt = mnt; 215884d17192SAl Viro dentry = path->dentry = dget(mnt->mnt_root); 2159b12cea91SAl Viro goto retry; 2160b12cea91SAl Viro } 2161b12cea91SAl Viro 216284d17192SAl Viro static void unlock_mount(struct mountpoint *where) 2163b12cea91SAl Viro { 216484d17192SAl Viro struct dentry *dentry = where->m_dentry; 21653895dbf8SEric W. Biederman 21663895dbf8SEric W. Biederman read_seqlock_excl(&mount_lock); 216784d17192SAl Viro put_mountpoint(where); 21683895dbf8SEric W. Biederman read_sequnlock_excl(&mount_lock); 21693895dbf8SEric W. Biederman 2170328e6d90SAl Viro namespace_unlock(); 21715955102cSAl Viro inode_unlock(dentry->d_inode); 2172b12cea91SAl Viro } 2173b12cea91SAl Viro 217484d17192SAl Viro static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp) 21751da177e4SLinus Torvalds { 2176e462ec50SDavid Howells if (mnt->mnt.mnt_sb->s_flags & SB_NOUSER) 21771da177e4SLinus Torvalds return -EINVAL; 21781da177e4SLinus Torvalds 2179e36cb0b8SDavid Howells if (d_is_dir(mp->m_dentry) != 2180e36cb0b8SDavid Howells d_is_dir(mnt->mnt.mnt_root)) 21811da177e4SLinus Torvalds return -ENOTDIR; 21821da177e4SLinus Torvalds 218384d17192SAl Viro return attach_recursive_mnt(mnt, p, mp, NULL); 21841da177e4SLinus Torvalds } 21851da177e4SLinus Torvalds 21861da177e4SLinus Torvalds /* 21877a2e8a8fSValerie Aurora * Sanity check the flags to change_mnt_propagation. 21887a2e8a8fSValerie Aurora */ 21897a2e8a8fSValerie Aurora 2190e462ec50SDavid Howells static int flags_to_propagation_type(int ms_flags) 21917a2e8a8fSValerie Aurora { 2192e462ec50SDavid Howells int type = ms_flags & ~(MS_REC | MS_SILENT); 21937a2e8a8fSValerie Aurora 21947a2e8a8fSValerie Aurora /* Fail if any non-propagation flags are set */ 21957a2e8a8fSValerie Aurora if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 21967a2e8a8fSValerie Aurora return 0; 21977a2e8a8fSValerie Aurora /* Only one propagation flag should be set */ 21987a2e8a8fSValerie Aurora if (!is_power_of_2(type)) 21997a2e8a8fSValerie Aurora return 0; 22007a2e8a8fSValerie Aurora return type; 22017a2e8a8fSValerie Aurora } 22027a2e8a8fSValerie Aurora 22037a2e8a8fSValerie Aurora /* 220407b20889SRam Pai * recursively change the type of the mountpoint. 220507b20889SRam Pai */ 2206e462ec50SDavid Howells static int do_change_type(struct path *path, int ms_flags) 220707b20889SRam Pai { 2208315fc83eSAl Viro struct mount *m; 22094b8b21f4SAl Viro struct mount *mnt = real_mount(path->mnt); 2210e462ec50SDavid Howells int recurse = ms_flags & MS_REC; 22117a2e8a8fSValerie Aurora int type; 2212719f5d7fSMiklos Szeredi int err = 0; 221307b20889SRam Pai 22142d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 221507b20889SRam Pai return -EINVAL; 221607b20889SRam Pai 2217e462ec50SDavid Howells type = flags_to_propagation_type(ms_flags); 22187a2e8a8fSValerie Aurora if (!type) 22197a2e8a8fSValerie Aurora return -EINVAL; 22207a2e8a8fSValerie Aurora 222197216be0SAl Viro namespace_lock(); 2222719f5d7fSMiklos Szeredi if (type == MS_SHARED) { 2223719f5d7fSMiklos Szeredi err = invent_group_ids(mnt, recurse); 2224719f5d7fSMiklos Szeredi if (err) 2225719f5d7fSMiklos Szeredi goto out_unlock; 2226719f5d7fSMiklos Szeredi } 2227719f5d7fSMiklos Szeredi 2228719ea2fbSAl Viro lock_mount_hash(); 2229909b0a88SAl Viro for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL)) 22300f0afb1dSAl Viro change_mnt_propagation(m, type); 2231719ea2fbSAl Viro unlock_mount_hash(); 2232719f5d7fSMiklos Szeredi 2233719f5d7fSMiklos Szeredi out_unlock: 223497216be0SAl Viro namespace_unlock(); 2235719f5d7fSMiklos Szeredi return err; 223607b20889SRam Pai } 223707b20889SRam Pai 22385ff9d8a6SEric W. Biederman static bool has_locked_children(struct mount *mnt, struct dentry *dentry) 22395ff9d8a6SEric W. Biederman { 22405ff9d8a6SEric W. Biederman struct mount *child; 22415ff9d8a6SEric W. Biederman list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) { 22425ff9d8a6SEric W. Biederman if (!is_subdir(child->mnt_mountpoint, dentry)) 22435ff9d8a6SEric W. Biederman continue; 22445ff9d8a6SEric W. Biederman 22455ff9d8a6SEric W. Biederman if (child->mnt.mnt_flags & MNT_LOCKED) 22465ff9d8a6SEric W. Biederman return true; 22475ff9d8a6SEric W. Biederman } 22485ff9d8a6SEric W. Biederman return false; 22495ff9d8a6SEric W. Biederman } 22505ff9d8a6SEric W. Biederman 2251a07b2000SAl Viro static struct mount *__do_loopback(struct path *old_path, int recurse) 2252a07b2000SAl Viro { 2253a07b2000SAl Viro struct mount *mnt = ERR_PTR(-EINVAL), *old = real_mount(old_path->mnt); 2254a07b2000SAl Viro 2255a07b2000SAl Viro if (IS_MNT_UNBINDABLE(old)) 2256a07b2000SAl Viro return mnt; 2257a07b2000SAl Viro 2258a07b2000SAl Viro if (!check_mnt(old) && old_path->dentry->d_op != &ns_dentry_operations) 2259a07b2000SAl Viro return mnt; 2260a07b2000SAl Viro 2261a07b2000SAl Viro if (!recurse && has_locked_children(old, old_path->dentry)) 2262a07b2000SAl Viro return mnt; 2263a07b2000SAl Viro 2264a07b2000SAl Viro if (recurse) 2265a07b2000SAl Viro mnt = copy_tree(old, old_path->dentry, CL_COPY_MNT_NS_FILE); 2266a07b2000SAl Viro else 2267a07b2000SAl Viro mnt = clone_mnt(old, old_path->dentry, 0); 2268a07b2000SAl Viro 2269a07b2000SAl Viro if (!IS_ERR(mnt)) 2270a07b2000SAl Viro mnt->mnt.mnt_flags &= ~MNT_LOCKED; 2271a07b2000SAl Viro 2272a07b2000SAl Viro return mnt; 2273a07b2000SAl Viro } 2274a07b2000SAl Viro 227507b20889SRam Pai /* 22761da177e4SLinus Torvalds * do loopback mount. 22771da177e4SLinus Torvalds */ 2278808d4e3cSAl Viro static int do_loopback(struct path *path, const char *old_name, 22792dafe1c4SEric Sandeen int recurse) 22801da177e4SLinus Torvalds { 22812d92ab3cSAl Viro struct path old_path; 2282a07b2000SAl Viro struct mount *mnt = NULL, *parent; 228384d17192SAl Viro struct mountpoint *mp; 228457eccb83SAl Viro int err; 22851da177e4SLinus Torvalds if (!old_name || !*old_name) 22861da177e4SLinus Torvalds return -EINVAL; 2287815d405cSTrond Myklebust err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path); 22881da177e4SLinus Torvalds if (err) 22891da177e4SLinus Torvalds return err; 22901da177e4SLinus Torvalds 22918823c079SEric W. Biederman err = -EINVAL; 22924ce5d2b1SEric W. Biederman if (mnt_ns_loop(old_path.dentry)) 22938823c079SEric W. Biederman goto out; 22948823c079SEric W. Biederman 229584d17192SAl Viro mp = lock_mount(path); 2296a07b2000SAl Viro if (IS_ERR(mp)) { 229784d17192SAl Viro err = PTR_ERR(mp); 22989676f0c6SRam Pai goto out; 2299a07b2000SAl Viro } 23009676f0c6SRam Pai 230184d17192SAl Viro parent = real_mount(path->mnt); 2302e149ed2bSAl Viro if (!check_mnt(parent)) 2303e149ed2bSAl Viro goto out2; 2304e149ed2bSAl Viro 2305a07b2000SAl Viro mnt = __do_loopback(&old_path, recurse); 2306be34d1a3SDavid Howells if (IS_ERR(mnt)) { 2307be34d1a3SDavid Howells err = PTR_ERR(mnt); 2308e9c5d8a5SAndrey Vagin goto out2; 2309be34d1a3SDavid Howells } 2310ccd48bc7SAl Viro 231184d17192SAl Viro err = graft_tree(mnt, parent, mp); 23121da177e4SLinus Torvalds if (err) { 2313719ea2fbSAl Viro lock_mount_hash(); 2314e819f152SEric W. Biederman umount_tree(mnt, UMOUNT_SYNC); 2315719ea2fbSAl Viro unlock_mount_hash(); 23165b83d2c5SRam Pai } 2317b12cea91SAl Viro out2: 231884d17192SAl Viro unlock_mount(mp); 2319ccd48bc7SAl Viro out: 23202d92ab3cSAl Viro path_put(&old_path); 23211da177e4SLinus Torvalds return err; 23221da177e4SLinus Torvalds } 23231da177e4SLinus Torvalds 2324a07b2000SAl Viro static struct file *open_detached_copy(struct path *path, bool recursive) 2325a07b2000SAl Viro { 2326a07b2000SAl Viro struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns; 2327a07b2000SAl Viro struct mnt_namespace *ns = alloc_mnt_ns(user_ns, true); 2328a07b2000SAl Viro struct mount *mnt, *p; 2329a07b2000SAl Viro struct file *file; 2330a07b2000SAl Viro 2331a07b2000SAl Viro if (IS_ERR(ns)) 2332a07b2000SAl Viro return ERR_CAST(ns); 2333a07b2000SAl Viro 2334a07b2000SAl Viro namespace_lock(); 2335a07b2000SAl Viro mnt = __do_loopback(path, recursive); 2336a07b2000SAl Viro if (IS_ERR(mnt)) { 2337a07b2000SAl Viro namespace_unlock(); 2338a07b2000SAl Viro free_mnt_ns(ns); 2339a07b2000SAl Viro return ERR_CAST(mnt); 2340a07b2000SAl Viro } 2341a07b2000SAl Viro 2342a07b2000SAl Viro lock_mount_hash(); 2343a07b2000SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 2344a07b2000SAl Viro p->mnt_ns = ns; 2345a07b2000SAl Viro ns->mounts++; 2346a07b2000SAl Viro } 2347a07b2000SAl Viro ns->root = mnt; 2348a07b2000SAl Viro list_add_tail(&ns->list, &mnt->mnt_list); 2349a07b2000SAl Viro mntget(&mnt->mnt); 2350a07b2000SAl Viro unlock_mount_hash(); 2351a07b2000SAl Viro namespace_unlock(); 2352a07b2000SAl Viro 2353a07b2000SAl Viro mntput(path->mnt); 2354a07b2000SAl Viro path->mnt = &mnt->mnt; 2355a07b2000SAl Viro file = dentry_open(path, O_PATH, current_cred()); 2356a07b2000SAl Viro if (IS_ERR(file)) 2357a07b2000SAl Viro dissolve_on_fput(path->mnt); 2358a07b2000SAl Viro else 2359a07b2000SAl Viro file->f_mode |= FMODE_NEED_UNMOUNT; 2360a07b2000SAl Viro return file; 2361a07b2000SAl Viro } 2362a07b2000SAl Viro 2363a07b2000SAl Viro SYSCALL_DEFINE3(open_tree, int, dfd, const char *, filename, unsigned, flags) 2364a07b2000SAl Viro { 2365a07b2000SAl Viro struct file *file; 2366a07b2000SAl Viro struct path path; 2367a07b2000SAl Viro int lookup_flags = LOOKUP_AUTOMOUNT | LOOKUP_FOLLOW; 2368a07b2000SAl Viro bool detached = flags & OPEN_TREE_CLONE; 2369a07b2000SAl Viro int error; 2370a07b2000SAl Viro int fd; 2371a07b2000SAl Viro 2372a07b2000SAl Viro BUILD_BUG_ON(OPEN_TREE_CLOEXEC != O_CLOEXEC); 2373a07b2000SAl Viro 2374a07b2000SAl Viro if (flags & ~(AT_EMPTY_PATH | AT_NO_AUTOMOUNT | AT_RECURSIVE | 2375a07b2000SAl Viro AT_SYMLINK_NOFOLLOW | OPEN_TREE_CLONE | 2376a07b2000SAl Viro OPEN_TREE_CLOEXEC)) 2377a07b2000SAl Viro return -EINVAL; 2378a07b2000SAl Viro 2379a07b2000SAl Viro if ((flags & (AT_RECURSIVE | OPEN_TREE_CLONE)) == AT_RECURSIVE) 2380a07b2000SAl Viro return -EINVAL; 2381a07b2000SAl Viro 2382a07b2000SAl Viro if (flags & AT_NO_AUTOMOUNT) 2383a07b2000SAl Viro lookup_flags &= ~LOOKUP_AUTOMOUNT; 2384a07b2000SAl Viro if (flags & AT_SYMLINK_NOFOLLOW) 2385a07b2000SAl Viro lookup_flags &= ~LOOKUP_FOLLOW; 2386a07b2000SAl Viro if (flags & AT_EMPTY_PATH) 2387a07b2000SAl Viro lookup_flags |= LOOKUP_EMPTY; 2388a07b2000SAl Viro 2389a07b2000SAl Viro if (detached && !may_mount()) 2390a07b2000SAl Viro return -EPERM; 2391a07b2000SAl Viro 2392a07b2000SAl Viro fd = get_unused_fd_flags(flags & O_CLOEXEC); 2393a07b2000SAl Viro if (fd < 0) 2394a07b2000SAl Viro return fd; 2395a07b2000SAl Viro 2396a07b2000SAl Viro error = user_path_at(dfd, filename, lookup_flags, &path); 2397a07b2000SAl Viro if (unlikely(error)) { 2398a07b2000SAl Viro file = ERR_PTR(error); 2399a07b2000SAl Viro } else { 2400a07b2000SAl Viro if (detached) 2401a07b2000SAl Viro file = open_detached_copy(&path, flags & AT_RECURSIVE); 2402a07b2000SAl Viro else 2403a07b2000SAl Viro file = dentry_open(&path, O_PATH, current_cred()); 2404a07b2000SAl Viro path_put(&path); 2405a07b2000SAl Viro } 2406a07b2000SAl Viro if (IS_ERR(file)) { 2407a07b2000SAl Viro put_unused_fd(fd); 2408a07b2000SAl Viro return PTR_ERR(file); 2409a07b2000SAl Viro } 2410a07b2000SAl Viro fd_install(fd, file); 2411a07b2000SAl Viro return fd; 2412a07b2000SAl Viro } 2413a07b2000SAl Viro 241443f5e655SDavid Howells /* 241543f5e655SDavid Howells * Don't allow locked mount flags to be cleared. 241643f5e655SDavid Howells * 241743f5e655SDavid Howells * No locks need to be held here while testing the various MNT_LOCK 241843f5e655SDavid Howells * flags because those flags can never be cleared once they are set. 241943f5e655SDavid Howells */ 242043f5e655SDavid Howells static bool can_change_locked_flags(struct mount *mnt, unsigned int mnt_flags) 24212e4b7fcdSDave Hansen { 242243f5e655SDavid Howells unsigned int fl = mnt->mnt.mnt_flags; 24232e4b7fcdSDave Hansen 242443f5e655SDavid Howells if ((fl & MNT_LOCK_READONLY) && 242543f5e655SDavid Howells !(mnt_flags & MNT_READONLY)) 242643f5e655SDavid Howells return false; 242743f5e655SDavid Howells 242843f5e655SDavid Howells if ((fl & MNT_LOCK_NODEV) && 242943f5e655SDavid Howells !(mnt_flags & MNT_NODEV)) 243043f5e655SDavid Howells return false; 243143f5e655SDavid Howells 243243f5e655SDavid Howells if ((fl & MNT_LOCK_NOSUID) && 243343f5e655SDavid Howells !(mnt_flags & MNT_NOSUID)) 243443f5e655SDavid Howells return false; 243543f5e655SDavid Howells 243643f5e655SDavid Howells if ((fl & MNT_LOCK_NOEXEC) && 243743f5e655SDavid Howells !(mnt_flags & MNT_NOEXEC)) 243843f5e655SDavid Howells return false; 243943f5e655SDavid Howells 244043f5e655SDavid Howells if ((fl & MNT_LOCK_ATIME) && 244143f5e655SDavid Howells ((fl & MNT_ATIME_MASK) != (mnt_flags & MNT_ATIME_MASK))) 244243f5e655SDavid Howells return false; 244343f5e655SDavid Howells 244443f5e655SDavid Howells return true; 244543f5e655SDavid Howells } 244643f5e655SDavid Howells 244743f5e655SDavid Howells static int change_mount_ro_state(struct mount *mnt, unsigned int mnt_flags) 244843f5e655SDavid Howells { 244943f5e655SDavid Howells bool readonly_request = (mnt_flags & MNT_READONLY); 245043f5e655SDavid Howells 245143f5e655SDavid Howells if (readonly_request == __mnt_is_readonly(&mnt->mnt)) 24522e4b7fcdSDave Hansen return 0; 24532e4b7fcdSDave Hansen 24542e4b7fcdSDave Hansen if (readonly_request) 245543f5e655SDavid Howells return mnt_make_readonly(mnt); 245643f5e655SDavid Howells 245743f5e655SDavid Howells return __mnt_unmake_readonly(mnt); 245843f5e655SDavid Howells } 245943f5e655SDavid Howells 246043f5e655SDavid Howells /* 246143f5e655SDavid Howells * Update the user-settable attributes on a mount. The caller must hold 246243f5e655SDavid Howells * sb->s_umount for writing. 246343f5e655SDavid Howells */ 246443f5e655SDavid Howells static void set_mount_attributes(struct mount *mnt, unsigned int mnt_flags) 246543f5e655SDavid Howells { 246643f5e655SDavid Howells lock_mount_hash(); 246743f5e655SDavid Howells mnt_flags |= mnt->mnt.mnt_flags & ~MNT_USER_SETTABLE_MASK; 246843f5e655SDavid Howells mnt->mnt.mnt_flags = mnt_flags; 246943f5e655SDavid Howells touch_mnt_namespace(mnt->mnt_ns); 247043f5e655SDavid Howells unlock_mount_hash(); 247143f5e655SDavid Howells } 247243f5e655SDavid Howells 247343f5e655SDavid Howells /* 247443f5e655SDavid Howells * Handle reconfiguration of the mountpoint only without alteration of the 247543f5e655SDavid Howells * superblock it refers to. This is triggered by specifying MS_REMOUNT|MS_BIND 247643f5e655SDavid Howells * to mount(2). 247743f5e655SDavid Howells */ 247843f5e655SDavid Howells static int do_reconfigure_mnt(struct path *path, unsigned int mnt_flags) 247943f5e655SDavid Howells { 248043f5e655SDavid Howells struct super_block *sb = path->mnt->mnt_sb; 248143f5e655SDavid Howells struct mount *mnt = real_mount(path->mnt); 248243f5e655SDavid Howells int ret; 248343f5e655SDavid Howells 248443f5e655SDavid Howells if (!check_mnt(mnt)) 248543f5e655SDavid Howells return -EINVAL; 248643f5e655SDavid Howells 248743f5e655SDavid Howells if (path->dentry != mnt->mnt.mnt_root) 248843f5e655SDavid Howells return -EINVAL; 248943f5e655SDavid Howells 249043f5e655SDavid Howells if (!can_change_locked_flags(mnt, mnt_flags)) 249143f5e655SDavid Howells return -EPERM; 249243f5e655SDavid Howells 249343f5e655SDavid Howells down_write(&sb->s_umount); 249443f5e655SDavid Howells ret = change_mount_ro_state(mnt, mnt_flags); 249543f5e655SDavid Howells if (ret == 0) 249643f5e655SDavid Howells set_mount_attributes(mnt, mnt_flags); 249743f5e655SDavid Howells up_write(&sb->s_umount); 249843f5e655SDavid Howells return ret; 24992e4b7fcdSDave Hansen } 25002e4b7fcdSDave Hansen 25011da177e4SLinus Torvalds /* 25021da177e4SLinus Torvalds * change filesystem flags. dir should be a physical root of filesystem. 25031da177e4SLinus Torvalds * If you've mounted a non-root directory somewhere and want to do remount 25041da177e4SLinus Torvalds * on it - tough luck. 25051da177e4SLinus Torvalds */ 2506e462ec50SDavid Howells static int do_remount(struct path *path, int ms_flags, int sb_flags, 2507e462ec50SDavid Howells int mnt_flags, void *data) 25081da177e4SLinus Torvalds { 25091da177e4SLinus Torvalds int err; 25102d92ab3cSAl Viro struct super_block *sb = path->mnt->mnt_sb; 2511143c8c91SAl Viro struct mount *mnt = real_mount(path->mnt); 25128d0347f6SDavid Howells struct fs_context *fc; 25131da177e4SLinus Torvalds 2514143c8c91SAl Viro if (!check_mnt(mnt)) 25151da177e4SLinus Torvalds return -EINVAL; 25161da177e4SLinus Torvalds 25172d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 25181da177e4SLinus Torvalds return -EINVAL; 25191da177e4SLinus Torvalds 252043f5e655SDavid Howells if (!can_change_locked_flags(mnt, mnt_flags)) 252107b64558SEric W. Biederman return -EPERM; 25229566d674SEric W. Biederman 25238d0347f6SDavid Howells fc = fs_context_for_reconfigure(path->dentry, sb_flags, MS_RMT_MASK); 25248d0347f6SDavid Howells if (IS_ERR(fc)) 25258d0347f6SDavid Howells return PTR_ERR(fc); 2526ff36fe2cSEric Paris 25278d0347f6SDavid Howells err = parse_monolithic_mount_data(fc, data); 25288d0347f6SDavid Howells if (!err) { 25291da177e4SLinus Torvalds down_write(&sb->s_umount); 253057eccb83SAl Viro err = -EPERM; 253143f5e655SDavid Howells if (ns_capable(sb->s_user_ns, CAP_SYS_ADMIN)) { 25328d0347f6SDavid Howells err = reconfigure_super(fc); 253343f5e655SDavid Howells if (!err) 253443f5e655SDavid Howells set_mount_attributes(mnt, mnt_flags); 25350e55a7ccSDan Williams } 25366339dab8SAl Viro up_write(&sb->s_umount); 25378d0347f6SDavid Howells } 25388d0347f6SDavid Howells put_fs_context(fc); 25391da177e4SLinus Torvalds return err; 25401da177e4SLinus Torvalds } 25411da177e4SLinus Torvalds 2542cbbe362cSAl Viro static inline int tree_contains_unbindable(struct mount *mnt) 25439676f0c6SRam Pai { 2544315fc83eSAl Viro struct mount *p; 2545909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 2546fc7be130SAl Viro if (IS_MNT_UNBINDABLE(p)) 25479676f0c6SRam Pai return 1; 25489676f0c6SRam Pai } 25499676f0c6SRam Pai return 0; 25509676f0c6SRam Pai } 25519676f0c6SRam Pai 255244dfd84aSDavid Howells /* 255344dfd84aSDavid Howells * Check that there aren't references to earlier/same mount namespaces in the 255444dfd84aSDavid Howells * specified subtree. Such references can act as pins for mount namespaces 255544dfd84aSDavid Howells * that aren't checked by the mount-cycle checking code, thereby allowing 255644dfd84aSDavid Howells * cycles to be made. 255744dfd84aSDavid Howells */ 255844dfd84aSDavid Howells static bool check_for_nsfs_mounts(struct mount *subtree) 255944dfd84aSDavid Howells { 256044dfd84aSDavid Howells struct mount *p; 256144dfd84aSDavid Howells bool ret = false; 256244dfd84aSDavid Howells 256344dfd84aSDavid Howells lock_mount_hash(); 256444dfd84aSDavid Howells for (p = subtree; p; p = next_mnt(p, subtree)) 256544dfd84aSDavid Howells if (mnt_ns_loop(p->mnt.mnt_root)) 256644dfd84aSDavid Howells goto out; 256744dfd84aSDavid Howells 256844dfd84aSDavid Howells ret = true; 256944dfd84aSDavid Howells out: 257044dfd84aSDavid Howells unlock_mount_hash(); 257144dfd84aSDavid Howells return ret; 257244dfd84aSDavid Howells } 257344dfd84aSDavid Howells 25742db154b3SDavid Howells static int do_move_mount(struct path *old_path, struct path *new_path) 25751da177e4SLinus Torvalds { 25762db154b3SDavid Howells struct path parent_path = {.mnt = NULL, .dentry = NULL}; 257744dfd84aSDavid Howells struct mnt_namespace *ns; 2578676da58dSAl Viro struct mount *p; 25790fb54e50SAl Viro struct mount *old; 258084d17192SAl Viro struct mountpoint *mp; 258157eccb83SAl Viro int err; 258244dfd84aSDavid Howells bool attached; 25831da177e4SLinus Torvalds 25842db154b3SDavid Howells mp = lock_mount(new_path); 258584d17192SAl Viro if (IS_ERR(mp)) 25862db154b3SDavid Howells return PTR_ERR(mp); 2587cc53ce53SDavid Howells 25882db154b3SDavid Howells old = real_mount(old_path->mnt); 25892db154b3SDavid Howells p = real_mount(new_path->mnt); 259044dfd84aSDavid Howells attached = mnt_has_parent(old); 259144dfd84aSDavid Howells ns = old->mnt_ns; 2592143c8c91SAl Viro 25931da177e4SLinus Torvalds err = -EINVAL; 259444dfd84aSDavid Howells /* The mountpoint must be in our namespace. */ 259544dfd84aSDavid Howells if (!check_mnt(p)) 25962db154b3SDavid Howells goto out; 25971da177e4SLinus Torvalds 259844dfd84aSDavid Howells /* The thing moved should be either ours or completely unattached. */ 259944dfd84aSDavid Howells if (attached && !check_mnt(old)) 260044dfd84aSDavid Howells goto out; 260144dfd84aSDavid Howells 260205883eeeSAl Viro if (!attached && !(ns && is_anon_ns(ns))) 26032db154b3SDavid Howells goto out; 26041da177e4SLinus Torvalds 26052db154b3SDavid Howells if (old->mnt.mnt_flags & MNT_LOCKED) 26062db154b3SDavid Howells goto out; 26072db154b3SDavid Howells 26082db154b3SDavid Howells if (old_path->dentry != old_path->mnt->mnt_root) 26092db154b3SDavid Howells goto out; 26102db154b3SDavid Howells 26112db154b3SDavid Howells if (d_is_dir(new_path->dentry) != 26122db154b3SDavid Howells d_is_dir(old_path->dentry)) 26132db154b3SDavid Howells goto out; 261421444403SRam Pai /* 261521444403SRam Pai * Don't move a mount residing in a shared parent. 261621444403SRam Pai */ 261744dfd84aSDavid Howells if (attached && IS_MNT_SHARED(old->mnt_parent)) 26182db154b3SDavid Howells goto out; 26199676f0c6SRam Pai /* 26209676f0c6SRam Pai * Don't move a mount tree containing unbindable mounts to a destination 26219676f0c6SRam Pai * mount which is shared. 26229676f0c6SRam Pai */ 2623fc7be130SAl Viro if (IS_MNT_SHARED(p) && tree_contains_unbindable(old)) 26242db154b3SDavid Howells goto out; 26251da177e4SLinus Torvalds err = -ELOOP; 262644dfd84aSDavid Howells if (!check_for_nsfs_mounts(old)) 262744dfd84aSDavid Howells goto out; 2628fc7be130SAl Viro for (; mnt_has_parent(p); p = p->mnt_parent) 2629676da58dSAl Viro if (p == old) 26302db154b3SDavid Howells goto out; 26311da177e4SLinus Torvalds 26322db154b3SDavid Howells err = attach_recursive_mnt(old, real_mount(new_path->mnt), mp, 263344dfd84aSDavid Howells attached ? &parent_path : NULL); 26344ac91378SJan Blunck if (err) 26352db154b3SDavid Howells goto out; 26361da177e4SLinus Torvalds 26371da177e4SLinus Torvalds /* if the mount is moved, it should no longer be expire 26381da177e4SLinus Torvalds * automatically */ 26396776db3dSAl Viro list_del_init(&old->mnt_expire); 26401da177e4SLinus Torvalds out: 26412db154b3SDavid Howells unlock_mount(mp); 264244dfd84aSDavid Howells if (!err) { 26431a390689SAl Viro path_put(&parent_path); 264444dfd84aSDavid Howells if (!attached) 264544dfd84aSDavid Howells free_mnt_ns(ns); 264644dfd84aSDavid Howells } 26472db154b3SDavid Howells return err; 26482db154b3SDavid Howells } 26492db154b3SDavid Howells 26502db154b3SDavid Howells static int do_move_mount_old(struct path *path, const char *old_name) 26512db154b3SDavid Howells { 26522db154b3SDavid Howells struct path old_path; 26532db154b3SDavid Howells int err; 26542db154b3SDavid Howells 26552db154b3SDavid Howells if (!old_name || !*old_name) 26562db154b3SDavid Howells return -EINVAL; 26572db154b3SDavid Howells 26582db154b3SDavid Howells err = kern_path(old_name, LOOKUP_FOLLOW, &old_path); 26592db154b3SDavid Howells if (err) 26602db154b3SDavid Howells return err; 26612db154b3SDavid Howells 26622db154b3SDavid Howells err = do_move_mount(&old_path, path); 26632d92ab3cSAl Viro path_put(&old_path); 26641da177e4SLinus Torvalds return err; 26651da177e4SLinus Torvalds } 26661da177e4SLinus Torvalds 26679d412a43SAl Viro /* 26689d412a43SAl Viro * add a mount into a namespace's mount tree 26699d412a43SAl Viro */ 267095bc5f25SAl Viro static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags) 26719d412a43SAl Viro { 267284d17192SAl Viro struct mountpoint *mp; 267384d17192SAl Viro struct mount *parent; 26749d412a43SAl Viro int err; 26759d412a43SAl Viro 2676f2ebb3a9SAl Viro mnt_flags &= ~MNT_INTERNAL_FLAGS; 26779d412a43SAl Viro 267884d17192SAl Viro mp = lock_mount(path); 267984d17192SAl Viro if (IS_ERR(mp)) 268084d17192SAl Viro return PTR_ERR(mp); 26819d412a43SAl Viro 268284d17192SAl Viro parent = real_mount(path->mnt); 26839d412a43SAl Viro err = -EINVAL; 268484d17192SAl Viro if (unlikely(!check_mnt(parent))) { 2685156cacb1SAl Viro /* that's acceptable only for automounts done in private ns */ 2686156cacb1SAl Viro if (!(mnt_flags & MNT_SHRINKABLE)) 26879d412a43SAl Viro goto unlock; 2688156cacb1SAl Viro /* ... and for those we'd better have mountpoint still alive */ 268984d17192SAl Viro if (!parent->mnt_ns) 2690156cacb1SAl Viro goto unlock; 2691156cacb1SAl Viro } 26929d412a43SAl Viro 26939d412a43SAl Viro /* Refuse the same filesystem on the same mount point */ 26949d412a43SAl Viro err = -EBUSY; 269595bc5f25SAl Viro if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb && 26969d412a43SAl Viro path->mnt->mnt_root == path->dentry) 26979d412a43SAl Viro goto unlock; 26989d412a43SAl Viro 26999d412a43SAl Viro err = -EINVAL; 2700e36cb0b8SDavid Howells if (d_is_symlink(newmnt->mnt.mnt_root)) 27019d412a43SAl Viro goto unlock; 27029d412a43SAl Viro 270395bc5f25SAl Viro newmnt->mnt.mnt_flags = mnt_flags; 270484d17192SAl Viro err = graft_tree(newmnt, parent, mp); 27059d412a43SAl Viro 27069d412a43SAl Viro unlock: 270784d17192SAl Viro unlock_mount(mp); 27089d412a43SAl Viro return err; 27099d412a43SAl Viro } 2710b1e75df4SAl Viro 2711132e4608SDavid Howells static bool mount_too_revealing(const struct super_block *sb, int *new_mnt_flags); 2712132e4608SDavid Howells 2713132e4608SDavid Howells /* 2714132e4608SDavid Howells * Create a new mount using a superblock configuration and request it 2715132e4608SDavid Howells * be added to the namespace tree. 2716132e4608SDavid Howells */ 2717132e4608SDavid Howells static int do_new_mount_fc(struct fs_context *fc, struct path *mountpoint, 2718132e4608SDavid Howells unsigned int mnt_flags) 2719132e4608SDavid Howells { 2720132e4608SDavid Howells struct vfsmount *mnt; 2721132e4608SDavid Howells struct super_block *sb = fc->root->d_sb; 2722132e4608SDavid Howells int error; 2723132e4608SDavid Howells 2724c9ce29edSAl Viro error = security_sb_kern_mount(sb); 2725c9ce29edSAl Viro if (!error && mount_too_revealing(sb, &mnt_flags)) 2726c9ce29edSAl Viro error = -EPERM; 2727c9ce29edSAl Viro 2728c9ce29edSAl Viro if (unlikely(error)) { 2729c9ce29edSAl Viro fc_drop_locked(fc); 2730c9ce29edSAl Viro return error; 2731132e4608SDavid Howells } 2732132e4608SDavid Howells 2733132e4608SDavid Howells up_write(&sb->s_umount); 2734132e4608SDavid Howells 2735132e4608SDavid Howells mnt = vfs_create_mount(fc); 2736132e4608SDavid Howells if (IS_ERR(mnt)) 2737132e4608SDavid Howells return PTR_ERR(mnt); 2738132e4608SDavid Howells 2739132e4608SDavid Howells error = do_add_mount(real_mount(mnt), mountpoint, mnt_flags); 2740132e4608SDavid Howells if (error < 0) 2741132e4608SDavid Howells mntput(mnt); 2742132e4608SDavid Howells return error; 2743132e4608SDavid Howells } 27441b852bceSEric W. Biederman 27451da177e4SLinus Torvalds /* 27461da177e4SLinus Torvalds * create a new mount for userspace and request it to be added into the 27471da177e4SLinus Torvalds * namespace's tree 27481da177e4SLinus Torvalds */ 2749e462ec50SDavid Howells static int do_new_mount(struct path *path, const char *fstype, int sb_flags, 2750808d4e3cSAl Viro int mnt_flags, const char *name, void *data) 27511da177e4SLinus Torvalds { 27520c55cfc4SEric W. Biederman struct file_system_type *type; 2753a0c9a8b8SAl Viro struct fs_context *fc; 2754a0c9a8b8SAl Viro const char *subtype = NULL; 2755a0c9a8b8SAl Viro int err = 0; 27561da177e4SLinus Torvalds 27570c55cfc4SEric W. Biederman if (!fstype) 27581da177e4SLinus Torvalds return -EINVAL; 27591da177e4SLinus Torvalds 27600c55cfc4SEric W. Biederman type = get_fs_type(fstype); 27610c55cfc4SEric W. Biederman if (!type) 27620c55cfc4SEric W. Biederman return -ENODEV; 27630c55cfc4SEric W. Biederman 2764a0c9a8b8SAl Viro if (type->fs_flags & FS_HAS_SUBTYPE) { 2765a0c9a8b8SAl Viro subtype = strchr(fstype, '.'); 2766a0c9a8b8SAl Viro if (subtype) { 2767a0c9a8b8SAl Viro subtype++; 2768a0c9a8b8SAl Viro if (!*subtype) { 27690c55cfc4SEric W. Biederman put_filesystem(type); 2770a0c9a8b8SAl Viro return -EINVAL; 2771a0c9a8b8SAl Viro } 2772a0c9a8b8SAl Viro } else { 2773a0c9a8b8SAl Viro subtype = ""; 2774a0c9a8b8SAl Viro } 27758654df4eSEric W. Biederman } 27768654df4eSEric W. Biederman 2777a0c9a8b8SAl Viro fc = fs_context_for_mount(type, sb_flags); 2778a0c9a8b8SAl Viro put_filesystem(type); 2779a0c9a8b8SAl Viro if (IS_ERR(fc)) 2780a0c9a8b8SAl Viro return PTR_ERR(fc); 2781a0c9a8b8SAl Viro 27823e1aeb00SDavid Howells if (subtype) 27833e1aeb00SDavid Howells err = vfs_parse_fs_string(fc, "subtype", 27843e1aeb00SDavid Howells subtype, strlen(subtype)); 27853e1aeb00SDavid Howells if (!err && name) 27863e1aeb00SDavid Howells err = vfs_parse_fs_string(fc, "source", name, strlen(name)); 2787a0c9a8b8SAl Viro if (!err) 2788a0c9a8b8SAl Viro err = parse_monolithic_mount_data(fc, data); 2789c3aabf07SAl Viro if (!err && !mount_capable(fc)) 2790c3aabf07SAl Viro err = -EPERM; 2791a0c9a8b8SAl Viro if (!err) 2792a0c9a8b8SAl Viro err = vfs_get_tree(fc); 2793132e4608SDavid Howells if (!err) 2794132e4608SDavid Howells err = do_new_mount_fc(fc, path, mnt_flags); 2795a0c9a8b8SAl Viro 2796a0c9a8b8SAl Viro put_fs_context(fc); 279715f9a3f3SAl Viro return err; 27981da177e4SLinus Torvalds } 27991da177e4SLinus Torvalds 280019a167afSAl Viro int finish_automount(struct vfsmount *m, struct path *path) 280119a167afSAl Viro { 28026776db3dSAl Viro struct mount *mnt = real_mount(m); 280319a167afSAl Viro int err; 280419a167afSAl Viro /* The new mount record should have at least 2 refs to prevent it being 280519a167afSAl Viro * expired before we get a chance to add it 280619a167afSAl Viro */ 28076776db3dSAl Viro BUG_ON(mnt_get_count(mnt) < 2); 280819a167afSAl Viro 280919a167afSAl Viro if (m->mnt_sb == path->mnt->mnt_sb && 281019a167afSAl Viro m->mnt_root == path->dentry) { 2811b1e75df4SAl Viro err = -ELOOP; 2812b1e75df4SAl Viro goto fail; 281319a167afSAl Viro } 281419a167afSAl Viro 281595bc5f25SAl Viro err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE); 2816b1e75df4SAl Viro if (!err) 2817b1e75df4SAl Viro return 0; 2818b1e75df4SAl Viro fail: 2819b1e75df4SAl Viro /* remove m from any expiration list it may be on */ 28206776db3dSAl Viro if (!list_empty(&mnt->mnt_expire)) { 282197216be0SAl Viro namespace_lock(); 28226776db3dSAl Viro list_del_init(&mnt->mnt_expire); 282397216be0SAl Viro namespace_unlock(); 282419a167afSAl Viro } 2825b1e75df4SAl Viro mntput(m); 2826b1e75df4SAl Viro mntput(m); 282719a167afSAl Viro return err; 282819a167afSAl Viro } 282919a167afSAl Viro 2830ea5b778aSDavid Howells /** 2831ea5b778aSDavid Howells * mnt_set_expiry - Put a mount on an expiration list 2832ea5b778aSDavid Howells * @mnt: The mount to list. 2833ea5b778aSDavid Howells * @expiry_list: The list to add the mount to. 2834ea5b778aSDavid Howells */ 2835ea5b778aSDavid Howells void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list) 2836ea5b778aSDavid Howells { 283797216be0SAl Viro namespace_lock(); 2838ea5b778aSDavid Howells 28396776db3dSAl Viro list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list); 2840ea5b778aSDavid Howells 284197216be0SAl Viro namespace_unlock(); 2842ea5b778aSDavid Howells } 2843ea5b778aSDavid Howells EXPORT_SYMBOL(mnt_set_expiry); 2844ea5b778aSDavid Howells 2845ea5b778aSDavid Howells /* 28461da177e4SLinus Torvalds * process a list of expirable mountpoints with the intent of discarding any 28471da177e4SLinus Torvalds * mountpoints that aren't in use and haven't been touched since last we came 28481da177e4SLinus Torvalds * here 28491da177e4SLinus Torvalds */ 28501da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts) 28511da177e4SLinus Torvalds { 2852761d5c38SAl Viro struct mount *mnt, *next; 28531da177e4SLinus Torvalds LIST_HEAD(graveyard); 28541da177e4SLinus Torvalds 28551da177e4SLinus Torvalds if (list_empty(mounts)) 28561da177e4SLinus Torvalds return; 28571da177e4SLinus Torvalds 285897216be0SAl Viro namespace_lock(); 2859719ea2fbSAl Viro lock_mount_hash(); 28601da177e4SLinus Torvalds 28611da177e4SLinus Torvalds /* extract from the expiration list every vfsmount that matches the 28621da177e4SLinus Torvalds * following criteria: 28631da177e4SLinus Torvalds * - only referenced by its parent vfsmount 28641da177e4SLinus Torvalds * - still marked for expiry (marked on the last call here; marks are 28651da177e4SLinus Torvalds * cleared by mntput()) 28661da177e4SLinus Torvalds */ 28676776db3dSAl Viro list_for_each_entry_safe(mnt, next, mounts, mnt_expire) { 2868863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1) || 28691ab59738SAl Viro propagate_mount_busy(mnt, 1)) 28701da177e4SLinus Torvalds continue; 28716776db3dSAl Viro list_move(&mnt->mnt_expire, &graveyard); 28721da177e4SLinus Torvalds } 2873bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 28746776db3dSAl Viro mnt = list_first_entry(&graveyard, struct mount, mnt_expire); 2875143c8c91SAl Viro touch_mnt_namespace(mnt->mnt_ns); 2876e819f152SEric W. Biederman umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC); 2877bcc5c7d2SAl Viro } 2878719ea2fbSAl Viro unlock_mount_hash(); 28793ab6abeeSAl Viro namespace_unlock(); 28801da177e4SLinus Torvalds } 28811da177e4SLinus Torvalds 28821da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry); 28831da177e4SLinus Torvalds 28841da177e4SLinus Torvalds /* 28855528f911STrond Myklebust * Ripoff of 'select_parent()' 28865528f911STrond Myklebust * 28875528f911STrond Myklebust * search the list of submounts for a given mountpoint, and move any 28885528f911STrond Myklebust * shrinkable submounts to the 'graveyard' list. 28895528f911STrond Myklebust */ 2890692afc31SAl Viro static int select_submounts(struct mount *parent, struct list_head *graveyard) 28915528f911STrond Myklebust { 2892692afc31SAl Viro struct mount *this_parent = parent; 28935528f911STrond Myklebust struct list_head *next; 28945528f911STrond Myklebust int found = 0; 28955528f911STrond Myklebust 28965528f911STrond Myklebust repeat: 28976b41d536SAl Viro next = this_parent->mnt_mounts.next; 28985528f911STrond Myklebust resume: 28996b41d536SAl Viro while (next != &this_parent->mnt_mounts) { 29005528f911STrond Myklebust struct list_head *tmp = next; 29016b41d536SAl Viro struct mount *mnt = list_entry(tmp, struct mount, mnt_child); 29025528f911STrond Myklebust 29035528f911STrond Myklebust next = tmp->next; 2904692afc31SAl Viro if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE)) 29055528f911STrond Myklebust continue; 29065528f911STrond Myklebust /* 29075528f911STrond Myklebust * Descend a level if the d_mounts list is non-empty. 29085528f911STrond Myklebust */ 29096b41d536SAl Viro if (!list_empty(&mnt->mnt_mounts)) { 29105528f911STrond Myklebust this_parent = mnt; 29115528f911STrond Myklebust goto repeat; 29125528f911STrond Myklebust } 29135528f911STrond Myklebust 29141ab59738SAl Viro if (!propagate_mount_busy(mnt, 1)) { 29156776db3dSAl Viro list_move_tail(&mnt->mnt_expire, graveyard); 29165528f911STrond Myklebust found++; 29175528f911STrond Myklebust } 29185528f911STrond Myklebust } 29195528f911STrond Myklebust /* 29205528f911STrond Myklebust * All done at this level ... ascend and resume the search 29215528f911STrond Myklebust */ 29225528f911STrond Myklebust if (this_parent != parent) { 29236b41d536SAl Viro next = this_parent->mnt_child.next; 29240714a533SAl Viro this_parent = this_parent->mnt_parent; 29255528f911STrond Myklebust goto resume; 29265528f911STrond Myklebust } 29275528f911STrond Myklebust return found; 29285528f911STrond Myklebust } 29295528f911STrond Myklebust 29305528f911STrond Myklebust /* 29315528f911STrond Myklebust * process a list of expirable mountpoints with the intent of discarding any 29325528f911STrond Myklebust * submounts of a specific parent mountpoint 293399b7db7bSNick Piggin * 293448a066e7SAl Viro * mount_lock must be held for write 29355528f911STrond Myklebust */ 2936b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt) 29375528f911STrond Myklebust { 29385528f911STrond Myklebust LIST_HEAD(graveyard); 2939761d5c38SAl Viro struct mount *m; 29405528f911STrond Myklebust 29415528f911STrond Myklebust /* extract submounts of 'mountpoint' from the expiration list */ 2942c35038beSAl Viro while (select_submounts(mnt, &graveyard)) { 2943bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 2944761d5c38SAl Viro m = list_first_entry(&graveyard, struct mount, 29456776db3dSAl Viro mnt_expire); 2946143c8c91SAl Viro touch_mnt_namespace(m->mnt_ns); 2947e819f152SEric W. Biederman umount_tree(m, UMOUNT_PROPAGATE|UMOUNT_SYNC); 2948bcc5c7d2SAl Viro } 2949bcc5c7d2SAl Viro } 29505528f911STrond Myklebust } 29515528f911STrond Myklebust 29525528f911STrond Myklebust /* 29531da177e4SLinus Torvalds * Some copy_from_user() implementations do not return the exact number of 29541da177e4SLinus Torvalds * bytes remaining to copy on a fault. But copy_mount_options() requires that. 29551da177e4SLinus Torvalds * Note that this function differs from copy_from_user() in that it will oops 29561da177e4SLinus Torvalds * on bad values of `to', rather than returning a short copy. 29571da177e4SLinus Torvalds */ 2958b58fed8bSRam Pai static long exact_copy_from_user(void *to, const void __user * from, 2959b58fed8bSRam Pai unsigned long n) 29601da177e4SLinus Torvalds { 29611da177e4SLinus Torvalds char *t = to; 29621da177e4SLinus Torvalds const char __user *f = from; 29631da177e4SLinus Torvalds char c; 29641da177e4SLinus Torvalds 296596d4f267SLinus Torvalds if (!access_ok(from, n)) 29661da177e4SLinus Torvalds return n; 29671da177e4SLinus Torvalds 29681da177e4SLinus Torvalds while (n) { 29691da177e4SLinus Torvalds if (__get_user(c, f)) { 29701da177e4SLinus Torvalds memset(t, 0, n); 29711da177e4SLinus Torvalds break; 29721da177e4SLinus Torvalds } 29731da177e4SLinus Torvalds *t++ = c; 29741da177e4SLinus Torvalds f++; 29751da177e4SLinus Torvalds n--; 29761da177e4SLinus Torvalds } 29771da177e4SLinus Torvalds return n; 29781da177e4SLinus Torvalds } 29791da177e4SLinus Torvalds 2980b40ef869SAl Viro void *copy_mount_options(const void __user * data) 29811da177e4SLinus Torvalds { 29821da177e4SLinus Torvalds int i; 29831da177e4SLinus Torvalds unsigned long size; 2984b40ef869SAl Viro char *copy; 29851da177e4SLinus Torvalds 29861da177e4SLinus Torvalds if (!data) 2987b40ef869SAl Viro return NULL; 29881da177e4SLinus Torvalds 2989b40ef869SAl Viro copy = kmalloc(PAGE_SIZE, GFP_KERNEL); 2990b40ef869SAl Viro if (!copy) 2991b40ef869SAl Viro return ERR_PTR(-ENOMEM); 29921da177e4SLinus Torvalds 29931da177e4SLinus Torvalds /* We only care that *some* data at the address the user 29941da177e4SLinus Torvalds * gave us is valid. Just in case, we'll zero 29951da177e4SLinus Torvalds * the remainder of the page. 29961da177e4SLinus Torvalds */ 29971da177e4SLinus Torvalds /* copy_from_user cannot cross TASK_SIZE ! */ 29981da177e4SLinus Torvalds size = TASK_SIZE - (unsigned long)data; 29991da177e4SLinus Torvalds if (size > PAGE_SIZE) 30001da177e4SLinus Torvalds size = PAGE_SIZE; 30011da177e4SLinus Torvalds 3002b40ef869SAl Viro i = size - exact_copy_from_user(copy, data, size); 30031da177e4SLinus Torvalds if (!i) { 3004b40ef869SAl Viro kfree(copy); 3005b40ef869SAl Viro return ERR_PTR(-EFAULT); 30061da177e4SLinus Torvalds } 30071da177e4SLinus Torvalds if (i != PAGE_SIZE) 3008b40ef869SAl Viro memset(copy + i, 0, PAGE_SIZE - i); 3009b40ef869SAl Viro return copy; 30101da177e4SLinus Torvalds } 30111da177e4SLinus Torvalds 3012b8850d1fSTim Gardner char *copy_mount_string(const void __user *data) 3013eca6f534SVegard Nossum { 3014fbdb4401SChandan Rajendra return data ? strndup_user(data, PATH_MAX) : NULL; 3015eca6f534SVegard Nossum } 3016eca6f534SVegard Nossum 30171da177e4SLinus Torvalds /* 30181da177e4SLinus Torvalds * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to 30191da177e4SLinus Torvalds * be given to the mount() call (ie: read-only, no-dev, no-suid etc). 30201da177e4SLinus Torvalds * 30211da177e4SLinus Torvalds * data is a (void *) that can point to any structure up to 30221da177e4SLinus Torvalds * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent 30231da177e4SLinus Torvalds * information (or be NULL). 30241da177e4SLinus Torvalds * 30251da177e4SLinus Torvalds * Pre-0.97 versions of mount() didn't have a flags word. 30261da177e4SLinus Torvalds * When the flags word was introduced its top half was required 30271da177e4SLinus Torvalds * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9. 30281da177e4SLinus Torvalds * Therefore, if this magic number is present, it carries no information 30291da177e4SLinus Torvalds * and must be discarded. 30301da177e4SLinus Torvalds */ 30315e6123f3SSeunghun Lee long do_mount(const char *dev_name, const char __user *dir_name, 3032808d4e3cSAl Viro const char *type_page, unsigned long flags, void *data_page) 30331da177e4SLinus Torvalds { 30342d92ab3cSAl Viro struct path path; 3035e462ec50SDavid Howells unsigned int mnt_flags = 0, sb_flags; 30361da177e4SLinus Torvalds int retval = 0; 30371da177e4SLinus Torvalds 30381da177e4SLinus Torvalds /* Discard magic */ 30391da177e4SLinus Torvalds if ((flags & MS_MGC_MSK) == MS_MGC_VAL) 30401da177e4SLinus Torvalds flags &= ~MS_MGC_MSK; 30411da177e4SLinus Torvalds 30421da177e4SLinus Torvalds /* Basic sanity checks */ 30431da177e4SLinus Torvalds if (data_page) 30441da177e4SLinus Torvalds ((char *)data_page)[PAGE_SIZE - 1] = 0; 30451da177e4SLinus Torvalds 3046e462ec50SDavid Howells if (flags & MS_NOUSER) 3047e462ec50SDavid Howells return -EINVAL; 3048e462ec50SDavid Howells 3049a27ab9f2STetsuo Handa /* ... and get the mountpoint */ 30505e6123f3SSeunghun Lee retval = user_path(dir_name, &path); 3051a27ab9f2STetsuo Handa if (retval) 3052a27ab9f2STetsuo Handa return retval; 3053a27ab9f2STetsuo Handa 3054a27ab9f2STetsuo Handa retval = security_sb_mount(dev_name, &path, 3055a27ab9f2STetsuo Handa type_page, flags, data_page); 30560d5cadb8SAl Viro if (!retval && !may_mount()) 30570d5cadb8SAl Viro retval = -EPERM; 3058e462ec50SDavid Howells if (!retval && (flags & SB_MANDLOCK) && !may_mandlock()) 30599e8925b6SJeff Layton retval = -EPERM; 3060a27ab9f2STetsuo Handa if (retval) 3061a27ab9f2STetsuo Handa goto dput_out; 3062a27ab9f2STetsuo Handa 3063613cbe3dSAndi Kleen /* Default to relatime unless overriden */ 3064613cbe3dSAndi Kleen if (!(flags & MS_NOATIME)) 30650a1c01c9SMatthew Garrett mnt_flags |= MNT_RELATIME; 30660a1c01c9SMatthew Garrett 30671da177e4SLinus Torvalds /* Separate the per-mountpoint flags */ 30681da177e4SLinus Torvalds if (flags & MS_NOSUID) 30691da177e4SLinus Torvalds mnt_flags |= MNT_NOSUID; 30701da177e4SLinus Torvalds if (flags & MS_NODEV) 30711da177e4SLinus Torvalds mnt_flags |= MNT_NODEV; 30721da177e4SLinus Torvalds if (flags & MS_NOEXEC) 30731da177e4SLinus Torvalds mnt_flags |= MNT_NOEXEC; 3074fc33a7bbSChristoph Hellwig if (flags & MS_NOATIME) 3075fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NOATIME; 3076fc33a7bbSChristoph Hellwig if (flags & MS_NODIRATIME) 3077fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NODIRATIME; 3078d0adde57SMatthew Garrett if (flags & MS_STRICTATIME) 3079d0adde57SMatthew Garrett mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME); 3080a9e5b732SDavid Howells if (flags & MS_RDONLY) 30812e4b7fcdSDave Hansen mnt_flags |= MNT_READONLY; 3082fc33a7bbSChristoph Hellwig 3083ffbc6f0eSEric W. Biederman /* The default atime for remount is preservation */ 3084ffbc6f0eSEric W. Biederman if ((flags & MS_REMOUNT) && 3085ffbc6f0eSEric W. Biederman ((flags & (MS_NOATIME | MS_NODIRATIME | MS_RELATIME | 3086ffbc6f0eSEric W. Biederman MS_STRICTATIME)) == 0)) { 3087ffbc6f0eSEric W. Biederman mnt_flags &= ~MNT_ATIME_MASK; 3088ffbc6f0eSEric W. Biederman mnt_flags |= path.mnt->mnt_flags & MNT_ATIME_MASK; 3089ffbc6f0eSEric W. Biederman } 3090ffbc6f0eSEric W. Biederman 3091e462ec50SDavid Howells sb_flags = flags & (SB_RDONLY | 3092e462ec50SDavid Howells SB_SYNCHRONOUS | 3093e462ec50SDavid Howells SB_MANDLOCK | 3094e462ec50SDavid Howells SB_DIRSYNC | 3095e462ec50SDavid Howells SB_SILENT | 3096917086ffSMimi Zohar SB_POSIXACL | 3097d7ee9469SMarkus Trippelsdorf SB_LAZYTIME | 3098917086ffSMimi Zohar SB_I_VERSION); 30991da177e4SLinus Torvalds 310043f5e655SDavid Howells if ((flags & (MS_REMOUNT | MS_BIND)) == (MS_REMOUNT | MS_BIND)) 310143f5e655SDavid Howells retval = do_reconfigure_mnt(&path, mnt_flags); 310243f5e655SDavid Howells else if (flags & MS_REMOUNT) 3103e462ec50SDavid Howells retval = do_remount(&path, flags, sb_flags, mnt_flags, 31041da177e4SLinus Torvalds data_page); 31051da177e4SLinus Torvalds else if (flags & MS_BIND) 31062d92ab3cSAl Viro retval = do_loopback(&path, dev_name, flags & MS_REC); 31079676f0c6SRam Pai else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 31082d92ab3cSAl Viro retval = do_change_type(&path, flags); 31091da177e4SLinus Torvalds else if (flags & MS_MOVE) 31102db154b3SDavid Howells retval = do_move_mount_old(&path, dev_name); 31111da177e4SLinus Torvalds else 3112e462ec50SDavid Howells retval = do_new_mount(&path, type_page, sb_flags, mnt_flags, 31131da177e4SLinus Torvalds dev_name, data_page); 31141da177e4SLinus Torvalds dput_out: 31152d92ab3cSAl Viro path_put(&path); 31161da177e4SLinus Torvalds return retval; 31171da177e4SLinus Torvalds } 31181da177e4SLinus Torvalds 3119537f7ccbSEric W. Biederman static struct ucounts *inc_mnt_namespaces(struct user_namespace *ns) 3120537f7ccbSEric W. Biederman { 3121537f7ccbSEric W. Biederman return inc_ucount(ns, current_euid(), UCOUNT_MNT_NAMESPACES); 3122537f7ccbSEric W. Biederman } 3123537f7ccbSEric W. Biederman 3124537f7ccbSEric W. Biederman static void dec_mnt_namespaces(struct ucounts *ucounts) 3125537f7ccbSEric W. Biederman { 3126537f7ccbSEric W. Biederman dec_ucount(ucounts, UCOUNT_MNT_NAMESPACES); 3127537f7ccbSEric W. Biederman } 3128537f7ccbSEric W. Biederman 3129771b1371SEric W. Biederman static void free_mnt_ns(struct mnt_namespace *ns) 3130771b1371SEric W. Biederman { 313174e83122SAl Viro if (!is_anon_ns(ns)) 31326344c433SAl Viro ns_free_inum(&ns->ns); 3133537f7ccbSEric W. Biederman dec_mnt_namespaces(ns->ucounts); 3134771b1371SEric W. Biederman put_user_ns(ns->user_ns); 3135771b1371SEric W. Biederman kfree(ns); 3136771b1371SEric W. Biederman } 3137771b1371SEric W. Biederman 31388823c079SEric W. Biederman /* 31398823c079SEric W. Biederman * Assign a sequence number so we can detect when we attempt to bind 31408823c079SEric W. Biederman * mount a reference to an older mount namespace into the current 31418823c079SEric W. Biederman * mount namespace, preventing reference counting loops. A 64bit 31428823c079SEric W. Biederman * number incrementing at 10Ghz will take 12,427 years to wrap which 31438823c079SEric W. Biederman * is effectively never, so we can ignore the possibility. 31448823c079SEric W. Biederman */ 31458823c079SEric W. Biederman static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1); 31468823c079SEric W. Biederman 314774e83122SAl Viro static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool anon) 3148cf8d2c11STrond Myklebust { 3149cf8d2c11STrond Myklebust struct mnt_namespace *new_ns; 3150537f7ccbSEric W. Biederman struct ucounts *ucounts; 315198f842e6SEric W. Biederman int ret; 3152cf8d2c11STrond Myklebust 3153537f7ccbSEric W. Biederman ucounts = inc_mnt_namespaces(user_ns); 3154537f7ccbSEric W. Biederman if (!ucounts) 3155df75e774SEric W. Biederman return ERR_PTR(-ENOSPC); 3156537f7ccbSEric W. Biederman 315774e83122SAl Viro new_ns = kzalloc(sizeof(struct mnt_namespace), GFP_KERNEL); 3158537f7ccbSEric W. Biederman if (!new_ns) { 3159537f7ccbSEric W. Biederman dec_mnt_namespaces(ucounts); 3160cf8d2c11STrond Myklebust return ERR_PTR(-ENOMEM); 3161537f7ccbSEric W. Biederman } 316274e83122SAl Viro if (!anon) { 31636344c433SAl Viro ret = ns_alloc_inum(&new_ns->ns); 316498f842e6SEric W. Biederman if (ret) { 316598f842e6SEric W. Biederman kfree(new_ns); 3166537f7ccbSEric W. Biederman dec_mnt_namespaces(ucounts); 316798f842e6SEric W. Biederman return ERR_PTR(ret); 316898f842e6SEric W. Biederman } 316974e83122SAl Viro } 317033c42940SAl Viro new_ns->ns.ops = &mntns_operations; 317174e83122SAl Viro if (!anon) 31728823c079SEric W. Biederman new_ns->seq = atomic64_add_return(1, &mnt_ns_seq); 3173cf8d2c11STrond Myklebust atomic_set(&new_ns->count, 1); 3174cf8d2c11STrond Myklebust INIT_LIST_HEAD(&new_ns->list); 3175cf8d2c11STrond Myklebust init_waitqueue_head(&new_ns->poll); 3176771b1371SEric W. Biederman new_ns->user_ns = get_user_ns(user_ns); 3177537f7ccbSEric W. Biederman new_ns->ucounts = ucounts; 3178cf8d2c11STrond Myklebust return new_ns; 3179cf8d2c11STrond Myklebust } 3180cf8d2c11STrond Myklebust 31810766f788SEmese Revfy __latent_entropy 31829559f689SAl Viro struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns, 31839559f689SAl Viro struct user_namespace *user_ns, struct fs_struct *new_fs) 31841da177e4SLinus Torvalds { 31856b3286edSKirill Korotaev struct mnt_namespace *new_ns; 31867f2da1e7SAl Viro struct vfsmount *rootmnt = NULL, *pwdmnt = NULL; 3187315fc83eSAl Viro struct mount *p, *q; 31889559f689SAl Viro struct mount *old; 3189cb338d06SAl Viro struct mount *new; 31907a472ef4SEric W. Biederman int copy_flags; 31911da177e4SLinus Torvalds 31929559f689SAl Viro BUG_ON(!ns); 31939559f689SAl Viro 31949559f689SAl Viro if (likely(!(flags & CLONE_NEWNS))) { 31959559f689SAl Viro get_mnt_ns(ns); 31969559f689SAl Viro return ns; 31979559f689SAl Viro } 31989559f689SAl Viro 31999559f689SAl Viro old = ns->root; 32009559f689SAl Viro 320174e83122SAl Viro new_ns = alloc_mnt_ns(user_ns, false); 3202cf8d2c11STrond Myklebust if (IS_ERR(new_ns)) 3203cf8d2c11STrond Myklebust return new_ns; 32041da177e4SLinus Torvalds 320597216be0SAl Viro namespace_lock(); 32061da177e4SLinus Torvalds /* First pass: copy the tree topology */ 32074ce5d2b1SEric W. Biederman copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE; 32089559f689SAl Viro if (user_ns != ns->user_ns) 32093bd045ccSAl Viro copy_flags |= CL_SHARED_TO_SLAVE; 32107a472ef4SEric W. Biederman new = copy_tree(old, old->mnt.mnt_root, copy_flags); 3211be34d1a3SDavid Howells if (IS_ERR(new)) { 3212328e6d90SAl Viro namespace_unlock(); 3213771b1371SEric W. Biederman free_mnt_ns(new_ns); 3214be34d1a3SDavid Howells return ERR_CAST(new); 32151da177e4SLinus Torvalds } 32163bd045ccSAl Viro if (user_ns != ns->user_ns) { 32173bd045ccSAl Viro lock_mount_hash(); 32183bd045ccSAl Viro lock_mnt_tree(new); 32193bd045ccSAl Viro unlock_mount_hash(); 32203bd045ccSAl Viro } 3221be08d6d2SAl Viro new_ns->root = new; 32221a4eeaf2SAl Viro list_add_tail(&new_ns->list, &new->mnt_list); 32231da177e4SLinus Torvalds 32241da177e4SLinus Torvalds /* 32251da177e4SLinus Torvalds * Second pass: switch the tsk->fs->* elements and mark new vfsmounts 32261da177e4SLinus Torvalds * as belonging to new namespace. We have already acquired a private 32271da177e4SLinus Torvalds * fs_struct, so tsk->fs->lock is not needed. 32281da177e4SLinus Torvalds */ 3229909b0a88SAl Viro p = old; 3230cb338d06SAl Viro q = new; 32311da177e4SLinus Torvalds while (p) { 3232143c8c91SAl Viro q->mnt_ns = new_ns; 3233d2921684SEric W. Biederman new_ns->mounts++; 32349559f689SAl Viro if (new_fs) { 32359559f689SAl Viro if (&p->mnt == new_fs->root.mnt) { 32369559f689SAl Viro new_fs->root.mnt = mntget(&q->mnt); 3237315fc83eSAl Viro rootmnt = &p->mnt; 32381da177e4SLinus Torvalds } 32399559f689SAl Viro if (&p->mnt == new_fs->pwd.mnt) { 32409559f689SAl Viro new_fs->pwd.mnt = mntget(&q->mnt); 3241315fc83eSAl Viro pwdmnt = &p->mnt; 32421da177e4SLinus Torvalds } 32431da177e4SLinus Torvalds } 3244909b0a88SAl Viro p = next_mnt(p, old); 3245909b0a88SAl Viro q = next_mnt(q, new); 32464ce5d2b1SEric W. Biederman if (!q) 32474ce5d2b1SEric W. Biederman break; 32484ce5d2b1SEric W. Biederman while (p->mnt.mnt_root != q->mnt.mnt_root) 32494ce5d2b1SEric W. Biederman p = next_mnt(p, old); 32501da177e4SLinus Torvalds } 3251328e6d90SAl Viro namespace_unlock(); 32521da177e4SLinus Torvalds 32531da177e4SLinus Torvalds if (rootmnt) 3254f03c6599SAl Viro mntput(rootmnt); 32551da177e4SLinus Torvalds if (pwdmnt) 3256f03c6599SAl Viro mntput(pwdmnt); 32571da177e4SLinus Torvalds 3258741a2951SJANAK DESAI return new_ns; 3259741a2951SJANAK DESAI } 3260741a2951SJANAK DESAI 326174e83122SAl Viro struct dentry *mount_subtree(struct vfsmount *m, const char *name) 3262cf8d2c11STrond Myklebust { 32631a4eeaf2SAl Viro struct mount *mnt = real_mount(m); 3264ea441d11SAl Viro struct mnt_namespace *ns; 3265d31da0f0SAl Viro struct super_block *s; 3266ea441d11SAl Viro struct path path; 3267ea441d11SAl Viro int err; 3268ea441d11SAl Viro 326974e83122SAl Viro ns = alloc_mnt_ns(&init_user_ns, true); 327074e83122SAl Viro if (IS_ERR(ns)) { 327174e83122SAl Viro mntput(m); 3272ea441d11SAl Viro return ERR_CAST(ns); 327374e83122SAl Viro } 327474e83122SAl Viro mnt->mnt_ns = ns; 327574e83122SAl Viro ns->root = mnt; 327674e83122SAl Viro ns->mounts++; 327774e83122SAl Viro list_add(&mnt->mnt_list, &ns->list); 3278ea441d11SAl Viro 327974e83122SAl Viro err = vfs_path_lookup(m->mnt_root, m, 3280ea441d11SAl Viro name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path); 3281ea441d11SAl Viro 3282ea441d11SAl Viro put_mnt_ns(ns); 3283ea441d11SAl Viro 3284ea441d11SAl Viro if (err) 3285ea441d11SAl Viro return ERR_PTR(err); 3286ea441d11SAl Viro 3287ea441d11SAl Viro /* trade a vfsmount reference for active sb one */ 3288d31da0f0SAl Viro s = path.mnt->mnt_sb; 3289d31da0f0SAl Viro atomic_inc(&s->s_active); 3290ea441d11SAl Viro mntput(path.mnt); 3291ea441d11SAl Viro /* lock the sucker */ 3292d31da0f0SAl Viro down_write(&s->s_umount); 3293ea441d11SAl Viro /* ... and return the root of (sub)tree on it */ 3294ea441d11SAl Viro return path.dentry; 3295ea441d11SAl Viro } 3296ea441d11SAl Viro EXPORT_SYMBOL(mount_subtree); 3297ea441d11SAl Viro 3298312db1aaSDominik Brodowski int ksys_mount(char __user *dev_name, char __user *dir_name, char __user *type, 3299312db1aaSDominik Brodowski unsigned long flags, void __user *data) 33001da177e4SLinus Torvalds { 3301eca6f534SVegard Nossum int ret; 3302eca6f534SVegard Nossum char *kernel_type; 3303eca6f534SVegard Nossum char *kernel_dev; 3304b40ef869SAl Viro void *options; 33051da177e4SLinus Torvalds 3306b8850d1fSTim Gardner kernel_type = copy_mount_string(type); 3307b8850d1fSTim Gardner ret = PTR_ERR(kernel_type); 3308b8850d1fSTim Gardner if (IS_ERR(kernel_type)) 3309eca6f534SVegard Nossum goto out_type; 33101da177e4SLinus Torvalds 3311b8850d1fSTim Gardner kernel_dev = copy_mount_string(dev_name); 3312b8850d1fSTim Gardner ret = PTR_ERR(kernel_dev); 3313b8850d1fSTim Gardner if (IS_ERR(kernel_dev)) 3314eca6f534SVegard Nossum goto out_dev; 33151da177e4SLinus Torvalds 3316b40ef869SAl Viro options = copy_mount_options(data); 3317b40ef869SAl Viro ret = PTR_ERR(options); 3318b40ef869SAl Viro if (IS_ERR(options)) 3319eca6f534SVegard Nossum goto out_data; 33201da177e4SLinus Torvalds 3321b40ef869SAl Viro ret = do_mount(kernel_dev, dir_name, kernel_type, flags, options); 3322eca6f534SVegard Nossum 3323b40ef869SAl Viro kfree(options); 3324eca6f534SVegard Nossum out_data: 3325eca6f534SVegard Nossum kfree(kernel_dev); 3326eca6f534SVegard Nossum out_dev: 3327eca6f534SVegard Nossum kfree(kernel_type); 3328eca6f534SVegard Nossum out_type: 3329eca6f534SVegard Nossum return ret; 33301da177e4SLinus Torvalds } 33311da177e4SLinus Torvalds 3332312db1aaSDominik Brodowski SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name, 3333312db1aaSDominik Brodowski char __user *, type, unsigned long, flags, void __user *, data) 3334312db1aaSDominik Brodowski { 3335312db1aaSDominik Brodowski return ksys_mount(dev_name, dir_name, type, flags, data); 3336312db1aaSDominik Brodowski } 3337312db1aaSDominik Brodowski 33381da177e4SLinus Torvalds /* 333993766fbdSDavid Howells * Create a kernel mount representation for a new, prepared superblock 334093766fbdSDavid Howells * (specified by fs_fd) and attach to an open_tree-like file descriptor. 334193766fbdSDavid Howells */ 334293766fbdSDavid Howells SYSCALL_DEFINE3(fsmount, int, fs_fd, unsigned int, flags, 334393766fbdSDavid Howells unsigned int, attr_flags) 334493766fbdSDavid Howells { 334593766fbdSDavid Howells struct mnt_namespace *ns; 334693766fbdSDavid Howells struct fs_context *fc; 334793766fbdSDavid Howells struct file *file; 334893766fbdSDavid Howells struct path newmount; 334993766fbdSDavid Howells struct mount *mnt; 335093766fbdSDavid Howells struct fd f; 335193766fbdSDavid Howells unsigned int mnt_flags = 0; 335293766fbdSDavid Howells long ret; 335393766fbdSDavid Howells 335493766fbdSDavid Howells if (!may_mount()) 335593766fbdSDavid Howells return -EPERM; 335693766fbdSDavid Howells 335793766fbdSDavid Howells if ((flags & ~(FSMOUNT_CLOEXEC)) != 0) 335893766fbdSDavid Howells return -EINVAL; 335993766fbdSDavid Howells 336093766fbdSDavid Howells if (attr_flags & ~(MOUNT_ATTR_RDONLY | 336193766fbdSDavid Howells MOUNT_ATTR_NOSUID | 336293766fbdSDavid Howells MOUNT_ATTR_NODEV | 336393766fbdSDavid Howells MOUNT_ATTR_NOEXEC | 336493766fbdSDavid Howells MOUNT_ATTR__ATIME | 336593766fbdSDavid Howells MOUNT_ATTR_NODIRATIME)) 336693766fbdSDavid Howells return -EINVAL; 336793766fbdSDavid Howells 336893766fbdSDavid Howells if (attr_flags & MOUNT_ATTR_RDONLY) 336993766fbdSDavid Howells mnt_flags |= MNT_READONLY; 337093766fbdSDavid Howells if (attr_flags & MOUNT_ATTR_NOSUID) 337193766fbdSDavid Howells mnt_flags |= MNT_NOSUID; 337293766fbdSDavid Howells if (attr_flags & MOUNT_ATTR_NODEV) 337393766fbdSDavid Howells mnt_flags |= MNT_NODEV; 337493766fbdSDavid Howells if (attr_flags & MOUNT_ATTR_NOEXEC) 337593766fbdSDavid Howells mnt_flags |= MNT_NOEXEC; 337693766fbdSDavid Howells if (attr_flags & MOUNT_ATTR_NODIRATIME) 337793766fbdSDavid Howells mnt_flags |= MNT_NODIRATIME; 337893766fbdSDavid Howells 337993766fbdSDavid Howells switch (attr_flags & MOUNT_ATTR__ATIME) { 338093766fbdSDavid Howells case MOUNT_ATTR_STRICTATIME: 338193766fbdSDavid Howells break; 338293766fbdSDavid Howells case MOUNT_ATTR_NOATIME: 338393766fbdSDavid Howells mnt_flags |= MNT_NOATIME; 338493766fbdSDavid Howells break; 338593766fbdSDavid Howells case MOUNT_ATTR_RELATIME: 338693766fbdSDavid Howells mnt_flags |= MNT_RELATIME; 338793766fbdSDavid Howells break; 338893766fbdSDavid Howells default: 338993766fbdSDavid Howells return -EINVAL; 339093766fbdSDavid Howells } 339193766fbdSDavid Howells 339293766fbdSDavid Howells f = fdget(fs_fd); 339393766fbdSDavid Howells if (!f.file) 339493766fbdSDavid Howells return -EBADF; 339593766fbdSDavid Howells 339693766fbdSDavid Howells ret = -EINVAL; 339793766fbdSDavid Howells if (f.file->f_op != &fscontext_fops) 339893766fbdSDavid Howells goto err_fsfd; 339993766fbdSDavid Howells 340093766fbdSDavid Howells fc = f.file->private_data; 340193766fbdSDavid Howells 340293766fbdSDavid Howells ret = mutex_lock_interruptible(&fc->uapi_mutex); 340393766fbdSDavid Howells if (ret < 0) 340493766fbdSDavid Howells goto err_fsfd; 340593766fbdSDavid Howells 340693766fbdSDavid Howells /* There must be a valid superblock or we can't mount it */ 340793766fbdSDavid Howells ret = -EINVAL; 340893766fbdSDavid Howells if (!fc->root) 340993766fbdSDavid Howells goto err_unlock; 341093766fbdSDavid Howells 341193766fbdSDavid Howells ret = -EPERM; 341293766fbdSDavid Howells if (mount_too_revealing(fc->root->d_sb, &mnt_flags)) { 341393766fbdSDavid Howells pr_warn("VFS: Mount too revealing\n"); 341493766fbdSDavid Howells goto err_unlock; 341593766fbdSDavid Howells } 341693766fbdSDavid Howells 341793766fbdSDavid Howells ret = -EBUSY; 341893766fbdSDavid Howells if (fc->phase != FS_CONTEXT_AWAITING_MOUNT) 341993766fbdSDavid Howells goto err_unlock; 342093766fbdSDavid Howells 342193766fbdSDavid Howells ret = -EPERM; 342293766fbdSDavid Howells if ((fc->sb_flags & SB_MANDLOCK) && !may_mandlock()) 342393766fbdSDavid Howells goto err_unlock; 342493766fbdSDavid Howells 342593766fbdSDavid Howells newmount.mnt = vfs_create_mount(fc); 342693766fbdSDavid Howells if (IS_ERR(newmount.mnt)) { 342793766fbdSDavid Howells ret = PTR_ERR(newmount.mnt); 342893766fbdSDavid Howells goto err_unlock; 342993766fbdSDavid Howells } 343093766fbdSDavid Howells newmount.dentry = dget(fc->root); 343193766fbdSDavid Howells newmount.mnt->mnt_flags = mnt_flags; 343293766fbdSDavid Howells 343393766fbdSDavid Howells /* We've done the mount bit - now move the file context into more or 343493766fbdSDavid Howells * less the same state as if we'd done an fspick(). We don't want to 343593766fbdSDavid Howells * do any memory allocation or anything like that at this point as we 343693766fbdSDavid Howells * don't want to have to handle any errors incurred. 343793766fbdSDavid Howells */ 343893766fbdSDavid Howells vfs_clean_context(fc); 343993766fbdSDavid Howells 344093766fbdSDavid Howells ns = alloc_mnt_ns(current->nsproxy->mnt_ns->user_ns, true); 344193766fbdSDavid Howells if (IS_ERR(ns)) { 344293766fbdSDavid Howells ret = PTR_ERR(ns); 344393766fbdSDavid Howells goto err_path; 344493766fbdSDavid Howells } 344593766fbdSDavid Howells mnt = real_mount(newmount.mnt); 344693766fbdSDavid Howells mnt->mnt_ns = ns; 344793766fbdSDavid Howells ns->root = mnt; 344893766fbdSDavid Howells ns->mounts = 1; 344993766fbdSDavid Howells list_add(&mnt->mnt_list, &ns->list); 345093766fbdSDavid Howells 345193766fbdSDavid Howells /* Attach to an apparent O_PATH fd with a note that we need to unmount 345293766fbdSDavid Howells * it, not just simply put it. 345393766fbdSDavid Howells */ 345493766fbdSDavid Howells file = dentry_open(&newmount, O_PATH, fc->cred); 345593766fbdSDavid Howells if (IS_ERR(file)) { 345693766fbdSDavid Howells dissolve_on_fput(newmount.mnt); 345793766fbdSDavid Howells ret = PTR_ERR(file); 345893766fbdSDavid Howells goto err_path; 345993766fbdSDavid Howells } 346093766fbdSDavid Howells file->f_mode |= FMODE_NEED_UNMOUNT; 346193766fbdSDavid Howells 346293766fbdSDavid Howells ret = get_unused_fd_flags((flags & FSMOUNT_CLOEXEC) ? O_CLOEXEC : 0); 346393766fbdSDavid Howells if (ret >= 0) 346493766fbdSDavid Howells fd_install(ret, file); 346593766fbdSDavid Howells else 346693766fbdSDavid Howells fput(file); 346793766fbdSDavid Howells 346893766fbdSDavid Howells err_path: 346993766fbdSDavid Howells path_put(&newmount); 347093766fbdSDavid Howells err_unlock: 347193766fbdSDavid Howells mutex_unlock(&fc->uapi_mutex); 347293766fbdSDavid Howells err_fsfd: 347393766fbdSDavid Howells fdput(f); 347493766fbdSDavid Howells return ret; 347593766fbdSDavid Howells } 347693766fbdSDavid Howells 347793766fbdSDavid Howells /* 347893766fbdSDavid Howells * Move a mount from one place to another. In combination with 347993766fbdSDavid Howells * fsopen()/fsmount() this is used to install a new mount and in combination 348093766fbdSDavid Howells * with open_tree(OPEN_TREE_CLONE [| AT_RECURSIVE]) it can be used to copy 348193766fbdSDavid Howells * a mount subtree. 34822db154b3SDavid Howells * 34832db154b3SDavid Howells * Note the flags value is a combination of MOVE_MOUNT_* flags. 34842db154b3SDavid Howells */ 34852db154b3SDavid Howells SYSCALL_DEFINE5(move_mount, 34862db154b3SDavid Howells int, from_dfd, const char *, from_pathname, 34872db154b3SDavid Howells int, to_dfd, const char *, to_pathname, 34882db154b3SDavid Howells unsigned int, flags) 34892db154b3SDavid Howells { 34902db154b3SDavid Howells struct path from_path, to_path; 34912db154b3SDavid Howells unsigned int lflags; 34922db154b3SDavid Howells int ret = 0; 34932db154b3SDavid Howells 34942db154b3SDavid Howells if (!may_mount()) 34952db154b3SDavid Howells return -EPERM; 34962db154b3SDavid Howells 34972db154b3SDavid Howells if (flags & ~MOVE_MOUNT__MASK) 34982db154b3SDavid Howells return -EINVAL; 34992db154b3SDavid Howells 35002db154b3SDavid Howells /* If someone gives a pathname, they aren't permitted to move 35012db154b3SDavid Howells * from an fd that requires unmount as we can't get at the flag 35022db154b3SDavid Howells * to clear it afterwards. 35032db154b3SDavid Howells */ 35042db154b3SDavid Howells lflags = 0; 35052db154b3SDavid Howells if (flags & MOVE_MOUNT_F_SYMLINKS) lflags |= LOOKUP_FOLLOW; 35062db154b3SDavid Howells if (flags & MOVE_MOUNT_F_AUTOMOUNTS) lflags |= LOOKUP_AUTOMOUNT; 35072db154b3SDavid Howells if (flags & MOVE_MOUNT_F_EMPTY_PATH) lflags |= LOOKUP_EMPTY; 35082db154b3SDavid Howells 35092db154b3SDavid Howells ret = user_path_at(from_dfd, from_pathname, lflags, &from_path); 35102db154b3SDavid Howells if (ret < 0) 35112db154b3SDavid Howells return ret; 35122db154b3SDavid Howells 35132db154b3SDavid Howells lflags = 0; 35142db154b3SDavid Howells if (flags & MOVE_MOUNT_T_SYMLINKS) lflags |= LOOKUP_FOLLOW; 35152db154b3SDavid Howells if (flags & MOVE_MOUNT_T_AUTOMOUNTS) lflags |= LOOKUP_AUTOMOUNT; 35162db154b3SDavid Howells if (flags & MOVE_MOUNT_T_EMPTY_PATH) lflags |= LOOKUP_EMPTY; 35172db154b3SDavid Howells 35182db154b3SDavid Howells ret = user_path_at(to_dfd, to_pathname, lflags, &to_path); 35192db154b3SDavid Howells if (ret < 0) 35202db154b3SDavid Howells goto out_from; 35212db154b3SDavid Howells 35222db154b3SDavid Howells ret = security_move_mount(&from_path, &to_path); 35232db154b3SDavid Howells if (ret < 0) 35242db154b3SDavid Howells goto out_to; 35252db154b3SDavid Howells 35262db154b3SDavid Howells ret = do_move_mount(&from_path, &to_path); 35272db154b3SDavid Howells 35282db154b3SDavid Howells out_to: 35292db154b3SDavid Howells path_put(&to_path); 35302db154b3SDavid Howells out_from: 35312db154b3SDavid Howells path_put(&from_path); 35322db154b3SDavid Howells return ret; 35332db154b3SDavid Howells } 35342db154b3SDavid Howells 35352db154b3SDavid Howells /* 3536afac7cbaSAl Viro * Return true if path is reachable from root 3537afac7cbaSAl Viro * 353848a066e7SAl Viro * namespace_sem or mount_lock is held 3539afac7cbaSAl Viro */ 3540643822b4SAl Viro bool is_path_reachable(struct mount *mnt, struct dentry *dentry, 3541afac7cbaSAl Viro const struct path *root) 3542afac7cbaSAl Viro { 3543643822b4SAl Viro while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) { 3544a73324daSAl Viro dentry = mnt->mnt_mountpoint; 35450714a533SAl Viro mnt = mnt->mnt_parent; 3546afac7cbaSAl Viro } 3547643822b4SAl Viro return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry); 3548afac7cbaSAl Viro } 3549afac7cbaSAl Viro 3550640eb7e7SMickaël Salaün bool path_is_under(const struct path *path1, const struct path *path2) 3551afac7cbaSAl Viro { 355225ab4c9bSYaowei Bai bool res; 355348a066e7SAl Viro read_seqlock_excl(&mount_lock); 3554643822b4SAl Viro res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2); 355548a066e7SAl Viro read_sequnlock_excl(&mount_lock); 3556afac7cbaSAl Viro return res; 3557afac7cbaSAl Viro } 3558afac7cbaSAl Viro EXPORT_SYMBOL(path_is_under); 3559afac7cbaSAl Viro 3560afac7cbaSAl Viro /* 35611da177e4SLinus Torvalds * pivot_root Semantics: 35621da177e4SLinus Torvalds * Moves the root file system of the current process to the directory put_old, 35631da177e4SLinus Torvalds * makes new_root as the new root file system of the current process, and sets 35641da177e4SLinus Torvalds * root/cwd of all processes which had them on the current root to new_root. 35651da177e4SLinus Torvalds * 35661da177e4SLinus Torvalds * Restrictions: 35671da177e4SLinus Torvalds * The new_root and put_old must be directories, and must not be on the 35681da177e4SLinus Torvalds * same file system as the current process root. The put_old must be 35691da177e4SLinus Torvalds * underneath new_root, i.e. adding a non-zero number of /.. to the string 35701da177e4SLinus Torvalds * pointed to by put_old must yield the same directory as new_root. No other 35711da177e4SLinus Torvalds * file system may be mounted on put_old. After all, new_root is a mountpoint. 35721da177e4SLinus Torvalds * 35734a0d11faSNeil Brown * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem. 35744a0d11faSNeil Brown * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives 35754a0d11faSNeil Brown * in this situation. 35764a0d11faSNeil Brown * 35771da177e4SLinus Torvalds * Notes: 35781da177e4SLinus Torvalds * - we don't move root/cwd if they are not at the root (reason: if something 35791da177e4SLinus Torvalds * cared enough to change them, it's probably wrong to force them elsewhere) 35801da177e4SLinus Torvalds * - it's okay to pick a root that isn't the root of a file system, e.g. 35811da177e4SLinus Torvalds * /nfs/my_root where /nfs is the mount point. It must be a mountpoint, 35821da177e4SLinus Torvalds * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root 35831da177e4SLinus Torvalds * first. 35841da177e4SLinus Torvalds */ 35853480b257SHeiko Carstens SYSCALL_DEFINE2(pivot_root, const char __user *, new_root, 35863480b257SHeiko Carstens const char __user *, put_old) 35871da177e4SLinus Torvalds { 35882d8f3038SAl Viro struct path new, old, parent_path, root_parent, root; 358984d17192SAl Viro struct mount *new_mnt, *root_mnt, *old_mnt; 359084d17192SAl Viro struct mountpoint *old_mp, *root_mp; 35911da177e4SLinus Torvalds int error; 35921da177e4SLinus Torvalds 35939b40bc90SAl Viro if (!may_mount()) 35941da177e4SLinus Torvalds return -EPERM; 35951da177e4SLinus Torvalds 35962d8f3038SAl Viro error = user_path_dir(new_root, &new); 35971da177e4SLinus Torvalds if (error) 35981da177e4SLinus Torvalds goto out0; 35991da177e4SLinus Torvalds 36002d8f3038SAl Viro error = user_path_dir(put_old, &old); 36011da177e4SLinus Torvalds if (error) 36021da177e4SLinus Torvalds goto out1; 36031da177e4SLinus Torvalds 36042d8f3038SAl Viro error = security_sb_pivotroot(&old, &new); 3605b12cea91SAl Viro if (error) 3606b12cea91SAl Viro goto out2; 36071da177e4SLinus Torvalds 3608f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &root); 360984d17192SAl Viro old_mp = lock_mount(&old); 361084d17192SAl Viro error = PTR_ERR(old_mp); 361184d17192SAl Viro if (IS_ERR(old_mp)) 3612b12cea91SAl Viro goto out3; 3613b12cea91SAl Viro 36141da177e4SLinus Torvalds error = -EINVAL; 3615419148daSAl Viro new_mnt = real_mount(new.mnt); 3616419148daSAl Viro root_mnt = real_mount(root.mnt); 361784d17192SAl Viro old_mnt = real_mount(old.mnt); 361884d17192SAl Viro if (IS_MNT_SHARED(old_mnt) || 3619fc7be130SAl Viro IS_MNT_SHARED(new_mnt->mnt_parent) || 3620fc7be130SAl Viro IS_MNT_SHARED(root_mnt->mnt_parent)) 3621b12cea91SAl Viro goto out4; 3622143c8c91SAl Viro if (!check_mnt(root_mnt) || !check_mnt(new_mnt)) 3623b12cea91SAl Viro goto out4; 36245ff9d8a6SEric W. Biederman if (new_mnt->mnt.mnt_flags & MNT_LOCKED) 36255ff9d8a6SEric W. Biederman goto out4; 36261da177e4SLinus Torvalds error = -ENOENT; 3627f3da392eSAlexey Dobriyan if (d_unlinked(new.dentry)) 3628b12cea91SAl Viro goto out4; 36291da177e4SLinus Torvalds error = -EBUSY; 363084d17192SAl Viro if (new_mnt == root_mnt || old_mnt == root_mnt) 3631b12cea91SAl Viro goto out4; /* loop, on the same file system */ 36321da177e4SLinus Torvalds error = -EINVAL; 36338c3ee42eSAl Viro if (root.mnt->mnt_root != root.dentry) 3634b12cea91SAl Viro goto out4; /* not a mountpoint */ 3635676da58dSAl Viro if (!mnt_has_parent(root_mnt)) 3636b12cea91SAl Viro goto out4; /* not attached */ 363784d17192SAl Viro root_mp = root_mnt->mnt_mp; 36382d8f3038SAl Viro if (new.mnt->mnt_root != new.dentry) 3639b12cea91SAl Viro goto out4; /* not a mountpoint */ 3640676da58dSAl Viro if (!mnt_has_parent(new_mnt)) 3641b12cea91SAl Viro goto out4; /* not attached */ 36424ac91378SJan Blunck /* make sure we can reach put_old from new_root */ 364384d17192SAl Viro if (!is_path_reachable(old_mnt, old.dentry, &new)) 3644b12cea91SAl Viro goto out4; 36450d082601SEric W. Biederman /* make certain new is below the root */ 36460d082601SEric W. Biederman if (!is_path_reachable(new_mnt, new.dentry, &root)) 36470d082601SEric W. Biederman goto out4; 364884d17192SAl Viro root_mp->m_count++; /* pin it so it won't go away */ 3649719ea2fbSAl Viro lock_mount_hash(); 3650419148daSAl Viro detach_mnt(new_mnt, &parent_path); 3651419148daSAl Viro detach_mnt(root_mnt, &root_parent); 36525ff9d8a6SEric W. Biederman if (root_mnt->mnt.mnt_flags & MNT_LOCKED) { 36535ff9d8a6SEric W. Biederman new_mnt->mnt.mnt_flags |= MNT_LOCKED; 36545ff9d8a6SEric W. Biederman root_mnt->mnt.mnt_flags &= ~MNT_LOCKED; 36555ff9d8a6SEric W. Biederman } 36564ac91378SJan Blunck /* mount old root on put_old */ 365784d17192SAl Viro attach_mnt(root_mnt, old_mnt, old_mp); 36584ac91378SJan Blunck /* mount new_root on / */ 365984d17192SAl Viro attach_mnt(new_mnt, real_mount(root_parent.mnt), root_mp); 36606b3286edSKirill Korotaev touch_mnt_namespace(current->nsproxy->mnt_ns); 36614fed655cSEric W. Biederman /* A moved mount should not expire automatically */ 36624fed655cSEric W. Biederman list_del_init(&new_mnt->mnt_expire); 36633895dbf8SEric W. Biederman put_mountpoint(root_mp); 3664719ea2fbSAl Viro unlock_mount_hash(); 36652d8f3038SAl Viro chroot_fs_refs(&root, &new); 36661da177e4SLinus Torvalds error = 0; 3667b12cea91SAl Viro out4: 366884d17192SAl Viro unlock_mount(old_mp); 3669b12cea91SAl Viro if (!error) { 36701a390689SAl Viro path_put(&root_parent); 36711a390689SAl Viro path_put(&parent_path); 3672b12cea91SAl Viro } 3673b12cea91SAl Viro out3: 36748c3ee42eSAl Viro path_put(&root); 3675b12cea91SAl Viro out2: 36762d8f3038SAl Viro path_put(&old); 36771da177e4SLinus Torvalds out1: 36782d8f3038SAl Viro path_put(&new); 36791da177e4SLinus Torvalds out0: 36801da177e4SLinus Torvalds return error; 36811da177e4SLinus Torvalds } 36821da177e4SLinus Torvalds 36831da177e4SLinus Torvalds static void __init init_mount_tree(void) 36841da177e4SLinus Torvalds { 36851da177e4SLinus Torvalds struct vfsmount *mnt; 368674e83122SAl Viro struct mount *m; 36876b3286edSKirill Korotaev struct mnt_namespace *ns; 3688ac748a09SJan Blunck struct path root; 36891da177e4SLinus Torvalds 3690fd3e007fSAl Viro mnt = vfs_kern_mount(&rootfs_fs_type, 0, "rootfs", NULL); 36911da177e4SLinus Torvalds if (IS_ERR(mnt)) 36921da177e4SLinus Torvalds panic("Can't create rootfs"); 3693b3e19d92SNick Piggin 369474e83122SAl Viro ns = alloc_mnt_ns(&init_user_ns, false); 36953b22edc5STrond Myklebust if (IS_ERR(ns)) 36961da177e4SLinus Torvalds panic("Can't allocate initial namespace"); 369774e83122SAl Viro m = real_mount(mnt); 369874e83122SAl Viro m->mnt_ns = ns; 369974e83122SAl Viro ns->root = m; 370074e83122SAl Viro ns->mounts = 1; 370174e83122SAl Viro list_add(&m->mnt_list, &ns->list); 37026b3286edSKirill Korotaev init_task.nsproxy->mnt_ns = ns; 37036b3286edSKirill Korotaev get_mnt_ns(ns); 37041da177e4SLinus Torvalds 3705be08d6d2SAl Viro root.mnt = mnt; 3706be08d6d2SAl Viro root.dentry = mnt->mnt_root; 3707da362b09SEric W. Biederman mnt->mnt_flags |= MNT_LOCKED; 3708ac748a09SJan Blunck 3709ac748a09SJan Blunck set_fs_pwd(current->fs, &root); 3710ac748a09SJan Blunck set_fs_root(current->fs, &root); 37111da177e4SLinus Torvalds } 37121da177e4SLinus Torvalds 371374bf17cfSDenis Cheng void __init mnt_init(void) 37141da177e4SLinus Torvalds { 371515a67dd8SRandy Dunlap int err; 37161da177e4SLinus Torvalds 37177d6fec45SAl Viro mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount), 371820c2df83SPaul Mundt 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL); 37191da177e4SLinus Torvalds 37200818bf27SAl Viro mount_hashtable = alloc_large_system_hash("Mount-cache", 372138129a13SAl Viro sizeof(struct hlist_head), 37220818bf27SAl Viro mhash_entries, 19, 37233d375d78SPavel Tatashin HASH_ZERO, 37240818bf27SAl Viro &m_hash_shift, &m_hash_mask, 0, 0); 37250818bf27SAl Viro mountpoint_hashtable = alloc_large_system_hash("Mountpoint-cache", 37260818bf27SAl Viro sizeof(struct hlist_head), 37270818bf27SAl Viro mphash_entries, 19, 37283d375d78SPavel Tatashin HASH_ZERO, 37290818bf27SAl Viro &mp_hash_shift, &mp_hash_mask, 0, 0); 37301da177e4SLinus Torvalds 373184d17192SAl Viro if (!mount_hashtable || !mountpoint_hashtable) 37321da177e4SLinus Torvalds panic("Failed to allocate mount hash table\n"); 37331da177e4SLinus Torvalds 37344b93dc9bSTejun Heo kernfs_init(); 37354b93dc9bSTejun Heo 373615a67dd8SRandy Dunlap err = sysfs_init(); 373715a67dd8SRandy Dunlap if (err) 373815a67dd8SRandy Dunlap printk(KERN_WARNING "%s: sysfs_init error: %d\n", 37398e24eea7SHarvey Harrison __func__, err); 374000d26666SGreg Kroah-Hartman fs_kobj = kobject_create_and_add("fs", NULL); 374100d26666SGreg Kroah-Hartman if (!fs_kobj) 37428e24eea7SHarvey Harrison printk(KERN_WARNING "%s: kobj create error\n", __func__); 37431da177e4SLinus Torvalds init_rootfs(); 37441da177e4SLinus Torvalds init_mount_tree(); 37451da177e4SLinus Torvalds } 37461da177e4SLinus Torvalds 3747616511d0STrond Myklebust void put_mnt_ns(struct mnt_namespace *ns) 37481da177e4SLinus Torvalds { 3749d498b25aSAl Viro if (!atomic_dec_and_test(&ns->count)) 3750616511d0STrond Myklebust return; 37517b00ed6fSAl Viro drop_collected_mounts(&ns->root->mnt); 3752771b1371SEric W. Biederman free_mnt_ns(ns); 37531da177e4SLinus Torvalds } 37549d412a43SAl Viro 3755d911b458SDavid Howells struct vfsmount *kern_mount(struct file_system_type *type) 37569d412a43SAl Viro { 3757423e0ab0STim Chen struct vfsmount *mnt; 3758d911b458SDavid Howells mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, NULL); 3759423e0ab0STim Chen if (!IS_ERR(mnt)) { 3760423e0ab0STim Chen /* 3761423e0ab0STim Chen * it is a longterm mount, don't release mnt until 3762423e0ab0STim Chen * we unmount before file sys is unregistered 3763423e0ab0STim Chen */ 3764f7a99c5bSAl Viro real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL; 3765423e0ab0STim Chen } 3766423e0ab0STim Chen return mnt; 37679d412a43SAl Viro } 3768d911b458SDavid Howells EXPORT_SYMBOL_GPL(kern_mount); 3769423e0ab0STim Chen 3770423e0ab0STim Chen void kern_unmount(struct vfsmount *mnt) 3771423e0ab0STim Chen { 3772423e0ab0STim Chen /* release long term mount so mount point can be released */ 3773423e0ab0STim Chen if (!IS_ERR_OR_NULL(mnt)) { 3774f7a99c5bSAl Viro real_mount(mnt)->mnt_ns = NULL; 377548a066e7SAl Viro synchronize_rcu(); /* yecchhh... */ 3776423e0ab0STim Chen mntput(mnt); 3777423e0ab0STim Chen } 3778423e0ab0STim Chen } 3779423e0ab0STim Chen EXPORT_SYMBOL(kern_unmount); 378002125a82SAl Viro 378102125a82SAl Viro bool our_mnt(struct vfsmount *mnt) 378202125a82SAl Viro { 3783143c8c91SAl Viro return check_mnt(real_mount(mnt)); 378402125a82SAl Viro } 37858823c079SEric W. Biederman 37863151527eSEric W. Biederman bool current_chrooted(void) 37873151527eSEric W. Biederman { 37883151527eSEric W. Biederman /* Does the current process have a non-standard root */ 37893151527eSEric W. Biederman struct path ns_root; 37903151527eSEric W. Biederman struct path fs_root; 37913151527eSEric W. Biederman bool chrooted; 37923151527eSEric W. Biederman 37933151527eSEric W. Biederman /* Find the namespace root */ 37943151527eSEric W. Biederman ns_root.mnt = ¤t->nsproxy->mnt_ns->root->mnt; 37953151527eSEric W. Biederman ns_root.dentry = ns_root.mnt->mnt_root; 37963151527eSEric W. Biederman path_get(&ns_root); 37973151527eSEric W. Biederman while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root)) 37983151527eSEric W. Biederman ; 37993151527eSEric W. Biederman 38003151527eSEric W. Biederman get_fs_root(current->fs, &fs_root); 38013151527eSEric W. Biederman 38023151527eSEric W. Biederman chrooted = !path_equal(&fs_root, &ns_root); 38033151527eSEric W. Biederman 38043151527eSEric W. Biederman path_put(&fs_root); 38053151527eSEric W. Biederman path_put(&ns_root); 38063151527eSEric W. Biederman 38073151527eSEric W. Biederman return chrooted; 38083151527eSEric W. Biederman } 38093151527eSEric W. Biederman 3810132e4608SDavid Howells static bool mnt_already_visible(struct mnt_namespace *ns, 3811132e4608SDavid Howells const struct super_block *sb, 38128654df4eSEric W. Biederman int *new_mnt_flags) 381387a8ebd6SEric W. Biederman { 38148c6cf9ccSEric W. Biederman int new_flags = *new_mnt_flags; 381587a8ebd6SEric W. Biederman struct mount *mnt; 3816e51db735SEric W. Biederman bool visible = false; 381787a8ebd6SEric W. Biederman 381844bb4385SAl Viro down_read(&namespace_sem); 381987a8ebd6SEric W. Biederman list_for_each_entry(mnt, &ns->list, mnt_list) { 3820e51db735SEric W. Biederman struct mount *child; 382177b1a97dSEric W. Biederman int mnt_flags; 382277b1a97dSEric W. Biederman 3823132e4608SDavid Howells if (mnt->mnt.mnt_sb->s_type != sb->s_type) 3824e51db735SEric W. Biederman continue; 3825e51db735SEric W. Biederman 38267e96c1b0SEric W. Biederman /* This mount is not fully visible if it's root directory 38277e96c1b0SEric W. Biederman * is not the root directory of the filesystem. 38287e96c1b0SEric W. Biederman */ 38297e96c1b0SEric W. Biederman if (mnt->mnt.mnt_root != mnt->mnt.mnt_sb->s_root) 38307e96c1b0SEric W. Biederman continue; 38317e96c1b0SEric W. Biederman 3832a1935c17SEric W. Biederman /* A local view of the mount flags */ 383377b1a97dSEric W. Biederman mnt_flags = mnt->mnt.mnt_flags; 383477b1a97dSEric W. Biederman 3835695e9df0SEric W. Biederman /* Don't miss readonly hidden in the superblock flags */ 3836bc98a42cSDavid Howells if (sb_rdonly(mnt->mnt.mnt_sb)) 3837695e9df0SEric W. Biederman mnt_flags |= MNT_LOCK_READONLY; 3838695e9df0SEric W. Biederman 38398c6cf9ccSEric W. Biederman /* Verify the mount flags are equal to or more permissive 38408c6cf9ccSEric W. Biederman * than the proposed new mount. 38418c6cf9ccSEric W. Biederman */ 384277b1a97dSEric W. Biederman if ((mnt_flags & MNT_LOCK_READONLY) && 38438c6cf9ccSEric W. Biederman !(new_flags & MNT_READONLY)) 38448c6cf9ccSEric W. Biederman continue; 384577b1a97dSEric W. Biederman if ((mnt_flags & MNT_LOCK_ATIME) && 384677b1a97dSEric W. Biederman ((mnt_flags & MNT_ATIME_MASK) != (new_flags & MNT_ATIME_MASK))) 38478c6cf9ccSEric W. Biederman continue; 38488c6cf9ccSEric W. Biederman 3849ceeb0e5dSEric W. Biederman /* This mount is not fully visible if there are any 3850ceeb0e5dSEric W. Biederman * locked child mounts that cover anything except for 3851ceeb0e5dSEric W. Biederman * empty directories. 3852e51db735SEric W. Biederman */ 3853e51db735SEric W. Biederman list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) { 3854e51db735SEric W. Biederman struct inode *inode = child->mnt_mountpoint->d_inode; 3855ceeb0e5dSEric W. Biederman /* Only worry about locked mounts */ 3856d71ed6c9SEric W. Biederman if (!(child->mnt.mnt_flags & MNT_LOCKED)) 3857ceeb0e5dSEric W. Biederman continue; 38587236c85eSEric W. Biederman /* Is the directory permanetly empty? */ 38597236c85eSEric W. Biederman if (!is_empty_dir_inode(inode)) 3860e51db735SEric W. Biederman goto next; 386187a8ebd6SEric W. Biederman } 38628c6cf9ccSEric W. Biederman /* Preserve the locked attributes */ 386377b1a97dSEric W. Biederman *new_mnt_flags |= mnt_flags & (MNT_LOCK_READONLY | \ 38648c6cf9ccSEric W. Biederman MNT_LOCK_ATIME); 3865e51db735SEric W. Biederman visible = true; 3866e51db735SEric W. Biederman goto found; 3867e51db735SEric W. Biederman next: ; 386887a8ebd6SEric W. Biederman } 3869e51db735SEric W. Biederman found: 387044bb4385SAl Viro up_read(&namespace_sem); 3871e51db735SEric W. Biederman return visible; 387287a8ebd6SEric W. Biederman } 387387a8ebd6SEric W. Biederman 3874132e4608SDavid Howells static bool mount_too_revealing(const struct super_block *sb, int *new_mnt_flags) 38758654df4eSEric W. Biederman { 3876a1935c17SEric W. Biederman const unsigned long required_iflags = SB_I_NOEXEC | SB_I_NODEV; 38778654df4eSEric W. Biederman struct mnt_namespace *ns = current->nsproxy->mnt_ns; 38788654df4eSEric W. Biederman unsigned long s_iflags; 38798654df4eSEric W. Biederman 38808654df4eSEric W. Biederman if (ns->user_ns == &init_user_ns) 38818654df4eSEric W. Biederman return false; 38828654df4eSEric W. Biederman 38838654df4eSEric W. Biederman /* Can this filesystem be too revealing? */ 3884132e4608SDavid Howells s_iflags = sb->s_iflags; 38858654df4eSEric W. Biederman if (!(s_iflags & SB_I_USERNS_VISIBLE)) 38868654df4eSEric W. Biederman return false; 38878654df4eSEric W. Biederman 3888a1935c17SEric W. Biederman if ((s_iflags & required_iflags) != required_iflags) { 3889a1935c17SEric W. Biederman WARN_ONCE(1, "Expected s_iflags to contain 0x%lx\n", 3890a1935c17SEric W. Biederman required_iflags); 3891a1935c17SEric W. Biederman return true; 3892a1935c17SEric W. Biederman } 3893a1935c17SEric W. Biederman 3894132e4608SDavid Howells return !mnt_already_visible(ns, sb, new_mnt_flags); 38958654df4eSEric W. Biederman } 38968654df4eSEric W. Biederman 3897380cf5baSAndy Lutomirski bool mnt_may_suid(struct vfsmount *mnt) 3898380cf5baSAndy Lutomirski { 3899380cf5baSAndy Lutomirski /* 3900380cf5baSAndy Lutomirski * Foreign mounts (accessed via fchdir or through /proc 3901380cf5baSAndy Lutomirski * symlinks) are always treated as if they are nosuid. This 3902380cf5baSAndy Lutomirski * prevents namespaces from trusting potentially unsafe 3903380cf5baSAndy Lutomirski * suid/sgid bits, file caps, or security labels that originate 3904380cf5baSAndy Lutomirski * in other namespaces. 3905380cf5baSAndy Lutomirski */ 3906380cf5baSAndy Lutomirski return !(mnt->mnt_flags & MNT_NOSUID) && check_mnt(real_mount(mnt)) && 3907380cf5baSAndy Lutomirski current_in_userns(mnt->mnt_sb->s_user_ns); 3908380cf5baSAndy Lutomirski } 3909380cf5baSAndy Lutomirski 391064964528SAl Viro static struct ns_common *mntns_get(struct task_struct *task) 39118823c079SEric W. Biederman { 391258be2825SAl Viro struct ns_common *ns = NULL; 39138823c079SEric W. Biederman struct nsproxy *nsproxy; 39148823c079SEric W. Biederman 3915728dba3aSEric W. Biederman task_lock(task); 3916728dba3aSEric W. Biederman nsproxy = task->nsproxy; 39178823c079SEric W. Biederman if (nsproxy) { 391858be2825SAl Viro ns = &nsproxy->mnt_ns->ns; 391958be2825SAl Viro get_mnt_ns(to_mnt_ns(ns)); 39208823c079SEric W. Biederman } 3921728dba3aSEric W. Biederman task_unlock(task); 39228823c079SEric W. Biederman 39238823c079SEric W. Biederman return ns; 39248823c079SEric W. Biederman } 39258823c079SEric W. Biederman 392664964528SAl Viro static void mntns_put(struct ns_common *ns) 39278823c079SEric W. Biederman { 392858be2825SAl Viro put_mnt_ns(to_mnt_ns(ns)); 39298823c079SEric W. Biederman } 39308823c079SEric W. Biederman 393164964528SAl Viro static int mntns_install(struct nsproxy *nsproxy, struct ns_common *ns) 39328823c079SEric W. Biederman { 39338823c079SEric W. Biederman struct fs_struct *fs = current->fs; 39344f757f3cSAl Viro struct mnt_namespace *mnt_ns = to_mnt_ns(ns), *old_mnt_ns; 39358823c079SEric W. Biederman struct path root; 39364f757f3cSAl Viro int err; 39378823c079SEric W. Biederman 39380c55cfc4SEric W. Biederman if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) || 3939c7b96acfSEric W. Biederman !ns_capable(current_user_ns(), CAP_SYS_CHROOT) || 3940c7b96acfSEric W. Biederman !ns_capable(current_user_ns(), CAP_SYS_ADMIN)) 3941ae11e0f1SZhao Hongjiang return -EPERM; 39428823c079SEric W. Biederman 394374e83122SAl Viro if (is_anon_ns(mnt_ns)) 394474e83122SAl Viro return -EINVAL; 394574e83122SAl Viro 39468823c079SEric W. Biederman if (fs->users != 1) 39478823c079SEric W. Biederman return -EINVAL; 39488823c079SEric W. Biederman 39498823c079SEric W. Biederman get_mnt_ns(mnt_ns); 39504f757f3cSAl Viro old_mnt_ns = nsproxy->mnt_ns; 39518823c079SEric W. Biederman nsproxy->mnt_ns = mnt_ns; 39528823c079SEric W. Biederman 39538823c079SEric W. Biederman /* Find the root */ 39544f757f3cSAl Viro err = vfs_path_lookup(mnt_ns->root->mnt.mnt_root, &mnt_ns->root->mnt, 39554f757f3cSAl Viro "/", LOOKUP_DOWN, &root); 39564f757f3cSAl Viro if (err) { 39574f757f3cSAl Viro /* revert to old namespace */ 39584f757f3cSAl Viro nsproxy->mnt_ns = old_mnt_ns; 39594f757f3cSAl Viro put_mnt_ns(mnt_ns); 39604f757f3cSAl Viro return err; 39614f757f3cSAl Viro } 39628823c079SEric W. Biederman 39634068367cSAndrei Vagin put_mnt_ns(old_mnt_ns); 39644068367cSAndrei Vagin 39658823c079SEric W. Biederman /* Update the pwd and root */ 39668823c079SEric W. Biederman set_fs_pwd(fs, &root); 39678823c079SEric W. Biederman set_fs_root(fs, &root); 39688823c079SEric W. Biederman 39698823c079SEric W. Biederman path_put(&root); 39708823c079SEric W. Biederman return 0; 39718823c079SEric W. Biederman } 39728823c079SEric W. Biederman 3973bcac25a5SAndrey Vagin static struct user_namespace *mntns_owner(struct ns_common *ns) 3974bcac25a5SAndrey Vagin { 3975bcac25a5SAndrey Vagin return to_mnt_ns(ns)->user_ns; 3976bcac25a5SAndrey Vagin } 3977bcac25a5SAndrey Vagin 39788823c079SEric W. Biederman const struct proc_ns_operations mntns_operations = { 39798823c079SEric W. Biederman .name = "mnt", 39808823c079SEric W. Biederman .type = CLONE_NEWNS, 39818823c079SEric W. Biederman .get = mntns_get, 39828823c079SEric W. Biederman .put = mntns_put, 39838823c079SEric W. Biederman .install = mntns_install, 3984bcac25a5SAndrey Vagin .owner = mntns_owner, 39858823c079SEric W. Biederman }; 3986