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 */ 23d10577a8SAl Viro #include <linux/uaccess.h> 240bb80f24SDavid Howells #include <linux/proc_ns.h> 2520b4fb48SLinus Torvalds #include <linux/magic.h> 260818bf27SAl Viro #include <linux/bootmem.h> 279ea459e1SAl Viro #include <linux/task_work.h> 289164bb4aSIngo Molnar #include <linux/sched/task.h> 299164bb4aSIngo Molnar 3007b20889SRam Pai #include "pnode.h" 31948730b0SAdrian Bunk #include "internal.h" 321da177e4SLinus Torvalds 33d2921684SEric W. Biederman /* Maximum number of mounts in a mount namespace */ 34d2921684SEric W. Biederman unsigned int sysctl_mount_max __read_mostly = 100000; 35d2921684SEric W. Biederman 360818bf27SAl Viro static unsigned int m_hash_mask __read_mostly; 370818bf27SAl Viro static unsigned int m_hash_shift __read_mostly; 380818bf27SAl Viro static unsigned int mp_hash_mask __read_mostly; 390818bf27SAl Viro static unsigned int mp_hash_shift __read_mostly; 400818bf27SAl Viro 410818bf27SAl Viro static __initdata unsigned long mhash_entries; 420818bf27SAl Viro static int __init set_mhash_entries(char *str) 430818bf27SAl Viro { 440818bf27SAl Viro if (!str) 450818bf27SAl Viro return 0; 460818bf27SAl Viro mhash_entries = simple_strtoul(str, &str, 0); 470818bf27SAl Viro return 1; 480818bf27SAl Viro } 490818bf27SAl Viro __setup("mhash_entries=", set_mhash_entries); 500818bf27SAl Viro 510818bf27SAl Viro static __initdata unsigned long mphash_entries; 520818bf27SAl Viro static int __init set_mphash_entries(char *str) 530818bf27SAl Viro { 540818bf27SAl Viro if (!str) 550818bf27SAl Viro return 0; 560818bf27SAl Viro mphash_entries = simple_strtoul(str, &str, 0); 570818bf27SAl Viro return 1; 580818bf27SAl Viro } 590818bf27SAl Viro __setup("mphash_entries=", set_mphash_entries); 6013f14b4dSEric Dumazet 61c7999c36SAl Viro static u64 event; 6273cd49ecSMiklos Szeredi static DEFINE_IDA(mnt_id_ida); 63719f5d7fSMiklos Szeredi static DEFINE_IDA(mnt_group_ida); 6499b7db7bSNick Piggin static DEFINE_SPINLOCK(mnt_id_lock); 65f21f6220SAl Viro static int mnt_id_start = 0; 66f21f6220SAl Viro static int mnt_group_start = 1; 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 { 10473cd49ecSMiklos Szeredi int res; 10573cd49ecSMiklos Szeredi 10673cd49ecSMiklos Szeredi retry: 10773cd49ecSMiklos Szeredi ida_pre_get(&mnt_id_ida, GFP_KERNEL); 10899b7db7bSNick Piggin spin_lock(&mnt_id_lock); 10915169fe7SAl Viro res = ida_get_new_above(&mnt_id_ida, mnt_id_start, &mnt->mnt_id); 110f21f6220SAl Viro if (!res) 11115169fe7SAl Viro mnt_id_start = mnt->mnt_id + 1; 11299b7db7bSNick Piggin spin_unlock(&mnt_id_lock); 11373cd49ecSMiklos Szeredi if (res == -EAGAIN) 11473cd49ecSMiklos Szeredi goto retry; 11573cd49ecSMiklos Szeredi 11673cd49ecSMiklos Szeredi return res; 11773cd49ecSMiklos Szeredi } 11873cd49ecSMiklos Szeredi 119b105e270SAl Viro static void mnt_free_id(struct mount *mnt) 12073cd49ecSMiklos Szeredi { 12115169fe7SAl Viro int id = mnt->mnt_id; 12299b7db7bSNick Piggin spin_lock(&mnt_id_lock); 123f21f6220SAl Viro ida_remove(&mnt_id_ida, id); 124f21f6220SAl Viro if (mnt_id_start > id) 125f21f6220SAl Viro mnt_id_start = id; 12699b7db7bSNick Piggin spin_unlock(&mnt_id_lock); 12773cd49ecSMiklos Szeredi } 12873cd49ecSMiklos Szeredi 129719f5d7fSMiklos Szeredi /* 130719f5d7fSMiklos Szeredi * Allocate a new peer group ID 131719f5d7fSMiklos Szeredi * 132719f5d7fSMiklos Szeredi * mnt_group_ida is protected by namespace_sem 133719f5d7fSMiklos Szeredi */ 1344b8b21f4SAl Viro static int mnt_alloc_group_id(struct mount *mnt) 135719f5d7fSMiklos Szeredi { 136f21f6220SAl Viro int res; 137f21f6220SAl Viro 138719f5d7fSMiklos Szeredi if (!ida_pre_get(&mnt_group_ida, GFP_KERNEL)) 139719f5d7fSMiklos Szeredi return -ENOMEM; 140719f5d7fSMiklos Szeredi 141f21f6220SAl Viro res = ida_get_new_above(&mnt_group_ida, 142f21f6220SAl Viro mnt_group_start, 14315169fe7SAl Viro &mnt->mnt_group_id); 144f21f6220SAl Viro if (!res) 14515169fe7SAl Viro mnt_group_start = mnt->mnt_group_id + 1; 146f21f6220SAl Viro 147f21f6220SAl Viro return res; 148719f5d7fSMiklos Szeredi } 149719f5d7fSMiklos Szeredi 150719f5d7fSMiklos Szeredi /* 151719f5d7fSMiklos Szeredi * Release a peer group ID 152719f5d7fSMiklos Szeredi */ 1534b8b21f4SAl Viro void mnt_release_group_id(struct mount *mnt) 154719f5d7fSMiklos Szeredi { 15515169fe7SAl Viro int id = mnt->mnt_group_id; 156f21f6220SAl Viro ida_remove(&mnt_group_ida, id); 157f21f6220SAl Viro if (mnt_group_start > id) 158f21f6220SAl Viro mnt_group_start = id; 15915169fe7SAl Viro mnt->mnt_group_id = 0; 160719f5d7fSMiklos Szeredi } 161719f5d7fSMiklos Szeredi 162b3e19d92SNick Piggin /* 163b3e19d92SNick Piggin * vfsmount lock must be held for read 164b3e19d92SNick Piggin */ 16583adc753SAl Viro static inline void mnt_add_count(struct mount *mnt, int n) 166b3e19d92SNick Piggin { 167b3e19d92SNick Piggin #ifdef CONFIG_SMP 16868e8a9feSAl Viro this_cpu_add(mnt->mnt_pcp->mnt_count, n); 169b3e19d92SNick Piggin #else 170b3e19d92SNick Piggin preempt_disable(); 17168e8a9feSAl Viro mnt->mnt_count += n; 172b3e19d92SNick Piggin preempt_enable(); 173b3e19d92SNick Piggin #endif 174b3e19d92SNick Piggin } 175b3e19d92SNick Piggin 176b3e19d92SNick Piggin /* 177b3e19d92SNick Piggin * vfsmount lock must be held for write 178b3e19d92SNick Piggin */ 17983adc753SAl Viro unsigned int mnt_get_count(struct mount *mnt) 180b3e19d92SNick Piggin { 181b3e19d92SNick Piggin #ifdef CONFIG_SMP 182f03c6599SAl Viro unsigned int count = 0; 183b3e19d92SNick Piggin int cpu; 184b3e19d92SNick Piggin 185b3e19d92SNick Piggin for_each_possible_cpu(cpu) { 18668e8a9feSAl Viro count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count; 187b3e19d92SNick Piggin } 188b3e19d92SNick Piggin 189b3e19d92SNick Piggin return count; 190b3e19d92SNick Piggin #else 19168e8a9feSAl Viro return mnt->mnt_count; 192b3e19d92SNick Piggin #endif 193b3e19d92SNick Piggin } 194b3e19d92SNick Piggin 19587b95ce0SAl Viro static void drop_mountpoint(struct fs_pin *p) 19687b95ce0SAl Viro { 19787b95ce0SAl Viro struct mount *m = container_of(p, struct mount, mnt_umount); 19887b95ce0SAl Viro dput(m->mnt_ex_mountpoint); 19987b95ce0SAl Viro pin_remove(p); 20087b95ce0SAl Viro mntput(&m->mnt); 20187b95ce0SAl Viro } 20287b95ce0SAl Viro 203b105e270SAl Viro static struct mount *alloc_vfsmnt(const char *name) 2041da177e4SLinus Torvalds { 205c63181e6SAl Viro struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL); 206c63181e6SAl Viro if (mnt) { 20773cd49ecSMiklos Szeredi int err; 20873cd49ecSMiklos Szeredi 209c63181e6SAl Viro err = mnt_alloc_id(mnt); 21088b38782SLi Zefan if (err) 21188b38782SLi Zefan goto out_free_cache; 21288b38782SLi Zefan 21388b38782SLi Zefan if (name) { 214fcc139aeSAndrzej Hajda mnt->mnt_devname = kstrdup_const(name, GFP_KERNEL); 215c63181e6SAl Viro if (!mnt->mnt_devname) 21688b38782SLi Zefan goto out_free_id; 21773cd49ecSMiklos Szeredi } 21873cd49ecSMiklos Szeredi 219b3e19d92SNick Piggin #ifdef CONFIG_SMP 220c63181e6SAl Viro mnt->mnt_pcp = alloc_percpu(struct mnt_pcp); 221c63181e6SAl Viro if (!mnt->mnt_pcp) 222b3e19d92SNick Piggin goto out_free_devname; 223b3e19d92SNick Piggin 224c63181e6SAl Viro this_cpu_add(mnt->mnt_pcp->mnt_count, 1); 225b3e19d92SNick Piggin #else 226c63181e6SAl Viro mnt->mnt_count = 1; 227c63181e6SAl Viro mnt->mnt_writers = 0; 228b3e19d92SNick Piggin #endif 229b3e19d92SNick Piggin 23038129a13SAl Viro INIT_HLIST_NODE(&mnt->mnt_hash); 231c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_child); 232c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_mounts); 233c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_list); 234c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_expire); 235c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_share); 236c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_slave_list); 237c63181e6SAl Viro INIT_LIST_HEAD(&mnt->mnt_slave); 2380a5eb7c8SEric W. Biederman INIT_HLIST_NODE(&mnt->mnt_mp_list); 23999b19d16SEric W. Biederman INIT_LIST_HEAD(&mnt->mnt_umounting); 24087b95ce0SAl Viro init_fs_pin(&mnt->mnt_umount, drop_mountpoint); 2411da177e4SLinus Torvalds } 242c63181e6SAl Viro return mnt; 24388b38782SLi Zefan 244d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 245d3ef3d73Snpiggin@suse.de out_free_devname: 246fcc139aeSAndrzej Hajda kfree_const(mnt->mnt_devname); 247d3ef3d73Snpiggin@suse.de #endif 24888b38782SLi Zefan out_free_id: 249c63181e6SAl Viro mnt_free_id(mnt); 25088b38782SLi Zefan out_free_cache: 251c63181e6SAl Viro kmem_cache_free(mnt_cache, mnt); 25288b38782SLi Zefan return NULL; 2531da177e4SLinus Torvalds } 2541da177e4SLinus Torvalds 2558366025eSDave Hansen /* 2568366025eSDave Hansen * Most r/o checks on a fs are for operations that take 2578366025eSDave Hansen * discrete amounts of time, like a write() or unlink(). 2588366025eSDave Hansen * We must keep track of when those operations start 2598366025eSDave Hansen * (for permission checks) and when they end, so that 2608366025eSDave Hansen * we can determine when writes are able to occur to 2618366025eSDave Hansen * a filesystem. 2628366025eSDave Hansen */ 2633d733633SDave Hansen /* 2643d733633SDave Hansen * __mnt_is_readonly: check whether a mount is read-only 2653d733633SDave Hansen * @mnt: the mount to check for its write status 2663d733633SDave Hansen * 2673d733633SDave Hansen * This shouldn't be used directly ouside of the VFS. 2683d733633SDave Hansen * It does not guarantee that the filesystem will stay 2693d733633SDave Hansen * r/w, just that it is right *now*. This can not and 2703d733633SDave Hansen * should not be used in place of IS_RDONLY(inode). 2713d733633SDave Hansen * mnt_want/drop_write() will _keep_ the filesystem 2723d733633SDave Hansen * r/w. 2733d733633SDave Hansen */ 2743d733633SDave Hansen int __mnt_is_readonly(struct vfsmount *mnt) 2753d733633SDave Hansen { 2762e4b7fcdSDave Hansen if (mnt->mnt_flags & MNT_READONLY) 2772e4b7fcdSDave Hansen return 1; 278bc98a42cSDavid Howells if (sb_rdonly(mnt->mnt_sb)) 2792e4b7fcdSDave Hansen return 1; 2802e4b7fcdSDave Hansen return 0; 2813d733633SDave Hansen } 2823d733633SDave Hansen EXPORT_SYMBOL_GPL(__mnt_is_readonly); 2833d733633SDave Hansen 28483adc753SAl Viro static inline void mnt_inc_writers(struct mount *mnt) 2853d733633SDave Hansen { 286d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 28768e8a9feSAl Viro this_cpu_inc(mnt->mnt_pcp->mnt_writers); 288d3ef3d73Snpiggin@suse.de #else 28968e8a9feSAl Viro mnt->mnt_writers++; 290d3ef3d73Snpiggin@suse.de #endif 2913d733633SDave Hansen } 2923d733633SDave Hansen 29383adc753SAl Viro static inline void mnt_dec_writers(struct mount *mnt) 2943d733633SDave Hansen { 295d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 29668e8a9feSAl Viro this_cpu_dec(mnt->mnt_pcp->mnt_writers); 297d3ef3d73Snpiggin@suse.de #else 29868e8a9feSAl Viro mnt->mnt_writers--; 299d3ef3d73Snpiggin@suse.de #endif 300d3ef3d73Snpiggin@suse.de } 301d3ef3d73Snpiggin@suse.de 30283adc753SAl Viro static unsigned int mnt_get_writers(struct mount *mnt) 303d3ef3d73Snpiggin@suse.de { 304d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 305d3ef3d73Snpiggin@suse.de unsigned int count = 0; 3063d733633SDave Hansen int cpu; 3073d733633SDave Hansen 3083d733633SDave Hansen for_each_possible_cpu(cpu) { 30968e8a9feSAl Viro count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers; 3103d733633SDave Hansen } 3113d733633SDave Hansen 312d3ef3d73Snpiggin@suse.de return count; 313d3ef3d73Snpiggin@suse.de #else 314d3ef3d73Snpiggin@suse.de return mnt->mnt_writers; 315d3ef3d73Snpiggin@suse.de #endif 3163d733633SDave Hansen } 3173d733633SDave Hansen 3184ed5e82fSMiklos Szeredi static int mnt_is_readonly(struct vfsmount *mnt) 3194ed5e82fSMiklos Szeredi { 3204ed5e82fSMiklos Szeredi if (mnt->mnt_sb->s_readonly_remount) 3214ed5e82fSMiklos Szeredi return 1; 3224ed5e82fSMiklos Szeredi /* Order wrt setting s_flags/s_readonly_remount in do_remount() */ 3234ed5e82fSMiklos Szeredi smp_rmb(); 3244ed5e82fSMiklos Szeredi return __mnt_is_readonly(mnt); 3254ed5e82fSMiklos Szeredi } 3264ed5e82fSMiklos Szeredi 3273d733633SDave Hansen /* 328eb04c282SJan Kara * Most r/o & frozen checks on a fs are for operations that take discrete 329eb04c282SJan Kara * amounts of time, like a write() or unlink(). We must keep track of when 330eb04c282SJan Kara * those operations start (for permission checks) and when they end, so that we 331eb04c282SJan Kara * can determine when writes are able to occur to a filesystem. 3323d733633SDave Hansen */ 3338366025eSDave Hansen /** 334eb04c282SJan Kara * __mnt_want_write - get write access to a mount without freeze protection 33583adc753SAl Viro * @m: the mount on which to take a write 3368366025eSDave Hansen * 337eb04c282SJan Kara * This tells the low-level filesystem that a write is about to be performed to 338eb04c282SJan Kara * it, and makes sure that writes are allowed (mnt it read-write) before 339eb04c282SJan Kara * returning success. This operation does not protect against filesystem being 340eb04c282SJan Kara * frozen. When the write operation is finished, __mnt_drop_write() must be 341eb04c282SJan Kara * called. This is effectively a refcount. 3428366025eSDave Hansen */ 343eb04c282SJan Kara int __mnt_want_write(struct vfsmount *m) 3448366025eSDave Hansen { 34583adc753SAl Viro struct mount *mnt = real_mount(m); 3463d733633SDave Hansen int ret = 0; 3473d733633SDave Hansen 348d3ef3d73Snpiggin@suse.de preempt_disable(); 349c6653a83SNick Piggin mnt_inc_writers(mnt); 350d3ef3d73Snpiggin@suse.de /* 351c6653a83SNick Piggin * The store to mnt_inc_writers must be visible before we pass 352d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD loop below, so that the slowpath can see our 353d3ef3d73Snpiggin@suse.de * incremented count after it has set MNT_WRITE_HOLD. 354d3ef3d73Snpiggin@suse.de */ 355d3ef3d73Snpiggin@suse.de smp_mb(); 3566aa7de05SMark Rutland while (READ_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD) 357d3ef3d73Snpiggin@suse.de cpu_relax(); 358d3ef3d73Snpiggin@suse.de /* 359d3ef3d73Snpiggin@suse.de * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will 360d3ef3d73Snpiggin@suse.de * be set to match its requirements. So we must not load that until 361d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD is cleared. 362d3ef3d73Snpiggin@suse.de */ 363d3ef3d73Snpiggin@suse.de smp_rmb(); 3644ed5e82fSMiklos Szeredi if (mnt_is_readonly(m)) { 365c6653a83SNick Piggin mnt_dec_writers(mnt); 3663d733633SDave Hansen ret = -EROFS; 3673d733633SDave Hansen } 368d3ef3d73Snpiggin@suse.de preempt_enable(); 369eb04c282SJan Kara 370eb04c282SJan Kara return ret; 371eb04c282SJan Kara } 372eb04c282SJan Kara 373eb04c282SJan Kara /** 374eb04c282SJan Kara * mnt_want_write - get write access to a mount 375eb04c282SJan Kara * @m: the mount on which to take a write 376eb04c282SJan Kara * 377eb04c282SJan Kara * This tells the low-level filesystem that a write is about to be performed to 378eb04c282SJan Kara * it, and makes sure that writes are allowed (mount is read-write, filesystem 379eb04c282SJan Kara * is not frozen) before returning success. When the write operation is 380eb04c282SJan Kara * finished, mnt_drop_write() must be called. This is effectively a refcount. 381eb04c282SJan Kara */ 382eb04c282SJan Kara int mnt_want_write(struct vfsmount *m) 383eb04c282SJan Kara { 384eb04c282SJan Kara int ret; 385eb04c282SJan Kara 386eb04c282SJan Kara sb_start_write(m->mnt_sb); 387eb04c282SJan Kara ret = __mnt_want_write(m); 388eb04c282SJan Kara if (ret) 389eb04c282SJan Kara sb_end_write(m->mnt_sb); 3903d733633SDave Hansen return ret; 3918366025eSDave Hansen } 3928366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_want_write); 3938366025eSDave Hansen 3948366025eSDave Hansen /** 39596029c4eSnpiggin@suse.de * mnt_clone_write - get write access to a mount 39696029c4eSnpiggin@suse.de * @mnt: the mount on which to take a write 39796029c4eSnpiggin@suse.de * 39896029c4eSnpiggin@suse.de * This is effectively like mnt_want_write, except 39996029c4eSnpiggin@suse.de * it must only be used to take an extra write reference 40096029c4eSnpiggin@suse.de * on a mountpoint that we already know has a write reference 40196029c4eSnpiggin@suse.de * on it. This allows some optimisation. 40296029c4eSnpiggin@suse.de * 40396029c4eSnpiggin@suse.de * After finished, mnt_drop_write must be called as usual to 40496029c4eSnpiggin@suse.de * drop the reference. 40596029c4eSnpiggin@suse.de */ 40696029c4eSnpiggin@suse.de int mnt_clone_write(struct vfsmount *mnt) 40796029c4eSnpiggin@suse.de { 40896029c4eSnpiggin@suse.de /* superblock may be r/o */ 40996029c4eSnpiggin@suse.de if (__mnt_is_readonly(mnt)) 41096029c4eSnpiggin@suse.de return -EROFS; 41196029c4eSnpiggin@suse.de preempt_disable(); 41283adc753SAl Viro mnt_inc_writers(real_mount(mnt)); 41396029c4eSnpiggin@suse.de preempt_enable(); 41496029c4eSnpiggin@suse.de return 0; 41596029c4eSnpiggin@suse.de } 41696029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_clone_write); 41796029c4eSnpiggin@suse.de 41896029c4eSnpiggin@suse.de /** 419eb04c282SJan Kara * __mnt_want_write_file - get write access to a file's mount 420eb04c282SJan Kara * @file: the file who's mount on which to take a write 421eb04c282SJan Kara * 422eb04c282SJan Kara * This is like __mnt_want_write, but it takes a file and can 423eb04c282SJan Kara * do some optimisations if the file is open for write already 424eb04c282SJan Kara */ 425eb04c282SJan Kara int __mnt_want_write_file(struct file *file) 426eb04c282SJan Kara { 42783f936c7SAl Viro if (!(file->f_mode & FMODE_WRITER)) 428eb04c282SJan Kara return __mnt_want_write(file->f_path.mnt); 429eb04c282SJan Kara else 430eb04c282SJan Kara return mnt_clone_write(file->f_path.mnt); 431eb04c282SJan Kara } 432eb04c282SJan Kara 433eb04c282SJan Kara /** 4347c6893e3SMiklos Szeredi * mnt_want_write_file_path - get write access to a file's mount 43596029c4eSnpiggin@suse.de * @file: the file who's mount on which to take a write 43696029c4eSnpiggin@suse.de * 43796029c4eSnpiggin@suse.de * This is like mnt_want_write, but it takes a file and can 43896029c4eSnpiggin@suse.de * do some optimisations if the file is open for write already 4397c6893e3SMiklos Szeredi * 4407c6893e3SMiklos Szeredi * Called by the vfs for cases when we have an open file at hand, but will do an 4417c6893e3SMiklos Szeredi * inode operation on it (important distinction for files opened on overlayfs, 4427c6893e3SMiklos Szeredi * since the file operations will come from the real underlying file, while 4437c6893e3SMiklos Szeredi * inode operations come from the overlay). 44496029c4eSnpiggin@suse.de */ 4457c6893e3SMiklos Szeredi int mnt_want_write_file_path(struct file *file) 44696029c4eSnpiggin@suse.de { 447eb04c282SJan Kara int ret; 448eb04c282SJan Kara 449eb04c282SJan Kara sb_start_write(file->f_path.mnt->mnt_sb); 450eb04c282SJan Kara ret = __mnt_want_write_file(file); 451eb04c282SJan Kara if (ret) 452eb04c282SJan Kara sb_end_write(file->f_path.mnt->mnt_sb); 453eb04c282SJan Kara return ret; 45496029c4eSnpiggin@suse.de } 4557c6893e3SMiklos Szeredi 4567c6893e3SMiklos Szeredi static inline int may_write_real(struct file *file) 4577c6893e3SMiklos Szeredi { 4587c6893e3SMiklos Szeredi struct dentry *dentry = file->f_path.dentry; 4597c6893e3SMiklos Szeredi struct dentry *upperdentry; 4607c6893e3SMiklos Szeredi 4617c6893e3SMiklos Szeredi /* Writable file? */ 4627c6893e3SMiklos Szeredi if (file->f_mode & FMODE_WRITER) 4637c6893e3SMiklos Szeredi return 0; 4647c6893e3SMiklos Szeredi 4657c6893e3SMiklos Szeredi /* Not overlayfs? */ 4667c6893e3SMiklos Szeredi if (likely(!(dentry->d_flags & DCACHE_OP_REAL))) 4677c6893e3SMiklos Szeredi return 0; 4687c6893e3SMiklos Szeredi 4697c6893e3SMiklos Szeredi /* File refers to upper, writable layer? */ 4707c6893e3SMiklos Szeredi upperdentry = d_real(dentry, NULL, 0, D_REAL_UPPER); 471954c736fSAmir Goldstein if (upperdentry && 472954c736fSAmir Goldstein (file_inode(file) == d_inode(upperdentry) || 473954c736fSAmir Goldstein file_inode(file) == d_inode(dentry))) 4747c6893e3SMiklos Szeredi return 0; 4757c6893e3SMiklos Szeredi 4767c6893e3SMiklos Szeredi /* Lower layer: can't write to real file, sorry... */ 4777c6893e3SMiklos Szeredi return -EPERM; 4787c6893e3SMiklos Szeredi } 4797c6893e3SMiklos Szeredi 4807c6893e3SMiklos Szeredi /** 4817c6893e3SMiklos Szeredi * mnt_want_write_file - get write access to a file's mount 4827c6893e3SMiklos Szeredi * @file: the file who's mount on which to take a write 4837c6893e3SMiklos Szeredi * 4847c6893e3SMiklos Szeredi * This is like mnt_want_write, but it takes a file and can 4857c6893e3SMiklos Szeredi * do some optimisations if the file is open for write already 4867c6893e3SMiklos Szeredi * 4877c6893e3SMiklos Szeredi * Mostly called by filesystems from their ioctl operation before performing 4887c6893e3SMiklos Szeredi * modification. On overlayfs this needs to check if the file is on a read-only 4897c6893e3SMiklos Szeredi * lower layer and deny access in that case. 4907c6893e3SMiklos Szeredi */ 4917c6893e3SMiklos Szeredi int mnt_want_write_file(struct file *file) 4927c6893e3SMiklos Szeredi { 4937c6893e3SMiklos Szeredi int ret; 4947c6893e3SMiklos Szeredi 4957c6893e3SMiklos Szeredi ret = may_write_real(file); 4967c6893e3SMiklos Szeredi if (!ret) { 4977c6893e3SMiklos Szeredi sb_start_write(file_inode(file)->i_sb); 4987c6893e3SMiklos Szeredi ret = __mnt_want_write_file(file); 4997c6893e3SMiklos Szeredi if (ret) 5007c6893e3SMiklos Szeredi sb_end_write(file_inode(file)->i_sb); 5017c6893e3SMiklos Szeredi } 5027c6893e3SMiklos Szeredi return ret; 5037c6893e3SMiklos Szeredi } 50496029c4eSnpiggin@suse.de EXPORT_SYMBOL_GPL(mnt_want_write_file); 50596029c4eSnpiggin@suse.de 50696029c4eSnpiggin@suse.de /** 507eb04c282SJan Kara * __mnt_drop_write - give up write access to a mount 5088366025eSDave Hansen * @mnt: the mount on which to give up write access 5098366025eSDave Hansen * 5108366025eSDave Hansen * Tells the low-level filesystem that we are done 5118366025eSDave Hansen * performing writes to it. Must be matched with 512eb04c282SJan Kara * __mnt_want_write() call above. 5138366025eSDave Hansen */ 514eb04c282SJan Kara void __mnt_drop_write(struct vfsmount *mnt) 5158366025eSDave Hansen { 516d3ef3d73Snpiggin@suse.de preempt_disable(); 51783adc753SAl Viro mnt_dec_writers(real_mount(mnt)); 518d3ef3d73Snpiggin@suse.de preempt_enable(); 5198366025eSDave Hansen } 520eb04c282SJan Kara 521eb04c282SJan Kara /** 522eb04c282SJan Kara * mnt_drop_write - give up write access to a mount 523eb04c282SJan Kara * @mnt: the mount on which to give up write access 524eb04c282SJan Kara * 525eb04c282SJan Kara * Tells the low-level filesystem that we are done performing writes to it and 526eb04c282SJan Kara * also allows filesystem to be frozen again. Must be matched with 527eb04c282SJan Kara * mnt_want_write() call above. 528eb04c282SJan Kara */ 529eb04c282SJan Kara void mnt_drop_write(struct vfsmount *mnt) 530eb04c282SJan Kara { 531eb04c282SJan Kara __mnt_drop_write(mnt); 532eb04c282SJan Kara sb_end_write(mnt->mnt_sb); 533eb04c282SJan Kara } 5348366025eSDave Hansen EXPORT_SYMBOL_GPL(mnt_drop_write); 5358366025eSDave Hansen 536eb04c282SJan Kara void __mnt_drop_write_file(struct file *file) 537eb04c282SJan Kara { 538eb04c282SJan Kara __mnt_drop_write(file->f_path.mnt); 539eb04c282SJan Kara } 540eb04c282SJan Kara 5417c6893e3SMiklos Szeredi void mnt_drop_write_file_path(struct file *file) 5422a79f17eSAl Viro { 5432a79f17eSAl Viro mnt_drop_write(file->f_path.mnt); 5442a79f17eSAl Viro } 5457c6893e3SMiklos Szeredi 5467c6893e3SMiklos Szeredi void mnt_drop_write_file(struct file *file) 5477c6893e3SMiklos Szeredi { 5487c6893e3SMiklos Szeredi __mnt_drop_write(file->f_path.mnt); 5497c6893e3SMiklos Szeredi sb_end_write(file_inode(file)->i_sb); 5507c6893e3SMiklos Szeredi } 5512a79f17eSAl Viro EXPORT_SYMBOL(mnt_drop_write_file); 5522a79f17eSAl Viro 55383adc753SAl Viro static int mnt_make_readonly(struct mount *mnt) 5548366025eSDave Hansen { 5553d733633SDave Hansen int ret = 0; 5563d733633SDave Hansen 557719ea2fbSAl Viro lock_mount_hash(); 55883adc753SAl Viro mnt->mnt.mnt_flags |= MNT_WRITE_HOLD; 559d3ef3d73Snpiggin@suse.de /* 560d3ef3d73Snpiggin@suse.de * After storing MNT_WRITE_HOLD, we'll read the counters. This store 561d3ef3d73Snpiggin@suse.de * should be visible before we do. 562d3ef3d73Snpiggin@suse.de */ 563d3ef3d73Snpiggin@suse.de smp_mb(); 564d3ef3d73Snpiggin@suse.de 565d3ef3d73Snpiggin@suse.de /* 566d3ef3d73Snpiggin@suse.de * With writers on hold, if this value is zero, then there are 567d3ef3d73Snpiggin@suse.de * definitely no active writers (although held writers may subsequently 568d3ef3d73Snpiggin@suse.de * increment the count, they'll have to wait, and decrement it after 569d3ef3d73Snpiggin@suse.de * seeing MNT_READONLY). 570d3ef3d73Snpiggin@suse.de * 571d3ef3d73Snpiggin@suse.de * It is OK to have counter incremented on one CPU and decremented on 572d3ef3d73Snpiggin@suse.de * another: the sum will add up correctly. The danger would be when we 573d3ef3d73Snpiggin@suse.de * sum up each counter, if we read a counter before it is incremented, 574d3ef3d73Snpiggin@suse.de * but then read another CPU's count which it has been subsequently 575d3ef3d73Snpiggin@suse.de * decremented from -- we would see more decrements than we should. 576d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD protects against this scenario, because 577d3ef3d73Snpiggin@suse.de * mnt_want_write first increments count, then smp_mb, then spins on 578d3ef3d73Snpiggin@suse.de * MNT_WRITE_HOLD, so it can't be decremented by another CPU while 579d3ef3d73Snpiggin@suse.de * we're counting up here. 580d3ef3d73Snpiggin@suse.de */ 581c6653a83SNick Piggin if (mnt_get_writers(mnt) > 0) 582d3ef3d73Snpiggin@suse.de ret = -EBUSY; 583d3ef3d73Snpiggin@suse.de else 58483adc753SAl Viro mnt->mnt.mnt_flags |= MNT_READONLY; 585d3ef3d73Snpiggin@suse.de /* 586d3ef3d73Snpiggin@suse.de * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers 587d3ef3d73Snpiggin@suse.de * that become unheld will see MNT_READONLY. 588d3ef3d73Snpiggin@suse.de */ 589d3ef3d73Snpiggin@suse.de smp_wmb(); 59083adc753SAl Viro mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD; 591719ea2fbSAl Viro unlock_mount_hash(); 5923d733633SDave Hansen return ret; 5933d733633SDave Hansen } 5948366025eSDave Hansen 59583adc753SAl Viro static void __mnt_unmake_readonly(struct mount *mnt) 5962e4b7fcdSDave Hansen { 597719ea2fbSAl Viro lock_mount_hash(); 59883adc753SAl Viro mnt->mnt.mnt_flags &= ~MNT_READONLY; 599719ea2fbSAl Viro unlock_mount_hash(); 6002e4b7fcdSDave Hansen } 6012e4b7fcdSDave Hansen 6024ed5e82fSMiklos Szeredi int sb_prepare_remount_readonly(struct super_block *sb) 6034ed5e82fSMiklos Szeredi { 6044ed5e82fSMiklos Szeredi struct mount *mnt; 6054ed5e82fSMiklos Szeredi int err = 0; 6064ed5e82fSMiklos Szeredi 6078e8b8796SMiklos Szeredi /* Racy optimization. Recheck the counter under MNT_WRITE_HOLD */ 6088e8b8796SMiklos Szeredi if (atomic_long_read(&sb->s_remove_count)) 6098e8b8796SMiklos Szeredi return -EBUSY; 6108e8b8796SMiklos Szeredi 611719ea2fbSAl Viro lock_mount_hash(); 6124ed5e82fSMiklos Szeredi list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) { 6134ed5e82fSMiklos Szeredi if (!(mnt->mnt.mnt_flags & MNT_READONLY)) { 6144ed5e82fSMiklos Szeredi mnt->mnt.mnt_flags |= MNT_WRITE_HOLD; 6154ed5e82fSMiklos Szeredi smp_mb(); 6164ed5e82fSMiklos Szeredi if (mnt_get_writers(mnt) > 0) { 6174ed5e82fSMiklos Szeredi err = -EBUSY; 6184ed5e82fSMiklos Szeredi break; 6194ed5e82fSMiklos Szeredi } 6204ed5e82fSMiklos Szeredi } 6214ed5e82fSMiklos Szeredi } 6228e8b8796SMiklos Szeredi if (!err && atomic_long_read(&sb->s_remove_count)) 6238e8b8796SMiklos Szeredi err = -EBUSY; 6248e8b8796SMiklos Szeredi 6254ed5e82fSMiklos Szeredi if (!err) { 6264ed5e82fSMiklos Szeredi sb->s_readonly_remount = 1; 6274ed5e82fSMiklos Szeredi smp_wmb(); 6284ed5e82fSMiklos Szeredi } 6294ed5e82fSMiklos Szeredi list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) { 6304ed5e82fSMiklos Szeredi if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD) 6314ed5e82fSMiklos Szeredi mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD; 6324ed5e82fSMiklos Szeredi } 633719ea2fbSAl Viro unlock_mount_hash(); 6344ed5e82fSMiklos Szeredi 6354ed5e82fSMiklos Szeredi return err; 6364ed5e82fSMiklos Szeredi } 6374ed5e82fSMiklos Szeredi 638b105e270SAl Viro static void free_vfsmnt(struct mount *mnt) 6391da177e4SLinus Torvalds { 640fcc139aeSAndrzej Hajda kfree_const(mnt->mnt_devname); 641d3ef3d73Snpiggin@suse.de #ifdef CONFIG_SMP 64268e8a9feSAl Viro free_percpu(mnt->mnt_pcp); 643d3ef3d73Snpiggin@suse.de #endif 644b105e270SAl Viro kmem_cache_free(mnt_cache, mnt); 6451da177e4SLinus Torvalds } 6461da177e4SLinus Torvalds 6478ffcb32eSDavid Howells static void delayed_free_vfsmnt(struct rcu_head *head) 6488ffcb32eSDavid Howells { 6498ffcb32eSDavid Howells free_vfsmnt(container_of(head, struct mount, mnt_rcu)); 6508ffcb32eSDavid Howells } 6518ffcb32eSDavid Howells 65248a066e7SAl Viro /* call under rcu_read_lock */ 653294d71ffSAl Viro int __legitimize_mnt(struct vfsmount *bastard, unsigned seq) 65448a066e7SAl Viro { 65548a066e7SAl Viro struct mount *mnt; 65648a066e7SAl Viro if (read_seqretry(&mount_lock, seq)) 657294d71ffSAl Viro return 1; 65848a066e7SAl Viro if (bastard == NULL) 659294d71ffSAl Viro return 0; 66048a066e7SAl Viro mnt = real_mount(bastard); 66148a066e7SAl Viro mnt_add_count(mnt, 1); 662119e1ef8SAl Viro smp_mb(); // see mntput_no_expire() 66348a066e7SAl Viro if (likely(!read_seqretry(&mount_lock, seq))) 664294d71ffSAl Viro return 0; 66548a066e7SAl Viro if (bastard->mnt_flags & MNT_SYNC_UMOUNT) { 66648a066e7SAl Viro mnt_add_count(mnt, -1); 667294d71ffSAl Viro return 1; 66848a066e7SAl Viro } 669119e1ef8SAl Viro lock_mount_hash(); 670119e1ef8SAl Viro if (unlikely(bastard->mnt_flags & MNT_DOOMED)) { 671119e1ef8SAl Viro mnt_add_count(mnt, -1); 672119e1ef8SAl Viro unlock_mount_hash(); 673119e1ef8SAl Viro return 1; 674119e1ef8SAl Viro } 675119e1ef8SAl Viro unlock_mount_hash(); 676119e1ef8SAl Viro /* caller will mntput() */ 677294d71ffSAl Viro return -1; 678294d71ffSAl Viro } 679294d71ffSAl Viro 680294d71ffSAl Viro /* call under rcu_read_lock */ 681294d71ffSAl Viro bool legitimize_mnt(struct vfsmount *bastard, unsigned seq) 682294d71ffSAl Viro { 683294d71ffSAl Viro int res = __legitimize_mnt(bastard, seq); 684294d71ffSAl Viro if (likely(!res)) 685294d71ffSAl Viro return true; 686294d71ffSAl Viro if (unlikely(res < 0)) { 68748a066e7SAl Viro rcu_read_unlock(); 68848a066e7SAl Viro mntput(bastard); 68948a066e7SAl Viro rcu_read_lock(); 690294d71ffSAl Viro } 69148a066e7SAl Viro return false; 69248a066e7SAl Viro } 69348a066e7SAl Viro 6941da177e4SLinus Torvalds /* 695474279dcSAl Viro * find the first mount at @dentry on vfsmount @mnt. 69648a066e7SAl Viro * call under rcu_read_lock() 6971da177e4SLinus Torvalds */ 698474279dcSAl Viro struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry) 6991da177e4SLinus Torvalds { 70038129a13SAl Viro struct hlist_head *head = m_hash(mnt, dentry); 701474279dcSAl Viro struct mount *p; 7021da177e4SLinus Torvalds 70338129a13SAl Viro hlist_for_each_entry_rcu(p, head, mnt_hash) 704474279dcSAl Viro if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry) 705474279dcSAl Viro return p; 706474279dcSAl Viro return NULL; 7071da177e4SLinus Torvalds } 708474279dcSAl Viro 709474279dcSAl Viro /* 710f015f126SDavid Howells * lookup_mnt - Return the first child mount mounted at path 711f015f126SDavid Howells * 712f015f126SDavid Howells * "First" means first mounted chronologically. If you create the 713f015f126SDavid Howells * following mounts: 714f015f126SDavid Howells * 715f015f126SDavid Howells * mount /dev/sda1 /mnt 716f015f126SDavid Howells * mount /dev/sda2 /mnt 717f015f126SDavid Howells * mount /dev/sda3 /mnt 718f015f126SDavid Howells * 719f015f126SDavid Howells * Then lookup_mnt() on the base /mnt dentry in the root mount will 720f015f126SDavid Howells * return successively the root dentry and vfsmount of /dev/sda1, then 721f015f126SDavid Howells * /dev/sda2, then /dev/sda3, then NULL. 722f015f126SDavid Howells * 723f015f126SDavid Howells * lookup_mnt takes a reference to the found vfsmount. 724a05964f3SRam Pai */ 725ca71cf71SAl Viro struct vfsmount *lookup_mnt(const struct path *path) 726a05964f3SRam Pai { 727c7105365SAl Viro struct mount *child_mnt; 72848a066e7SAl Viro struct vfsmount *m; 72948a066e7SAl Viro unsigned seq; 73099b7db7bSNick Piggin 73148a066e7SAl Viro rcu_read_lock(); 73248a066e7SAl Viro do { 73348a066e7SAl Viro seq = read_seqbegin(&mount_lock); 734474279dcSAl Viro child_mnt = __lookup_mnt(path->mnt, path->dentry); 73548a066e7SAl Viro m = child_mnt ? &child_mnt->mnt : NULL; 73648a066e7SAl Viro } while (!legitimize_mnt(m, seq)); 73748a066e7SAl Viro rcu_read_unlock(); 73848a066e7SAl Viro return m; 739a05964f3SRam Pai } 740a05964f3SRam Pai 7417af1364fSEric W. Biederman /* 7427af1364fSEric W. Biederman * __is_local_mountpoint - Test to see if dentry is a mountpoint in the 7437af1364fSEric W. Biederman * current mount namespace. 7447af1364fSEric W. Biederman * 7457af1364fSEric W. Biederman * The common case is dentries are not mountpoints at all and that 7467af1364fSEric W. Biederman * test is handled inline. For the slow case when we are actually 7477af1364fSEric W. Biederman * dealing with a mountpoint of some kind, walk through all of the 7487af1364fSEric W. Biederman * mounts in the current mount namespace and test to see if the dentry 7497af1364fSEric W. Biederman * is a mountpoint. 7507af1364fSEric W. Biederman * 7517af1364fSEric W. Biederman * The mount_hashtable is not usable in the context because we 7527af1364fSEric W. Biederman * need to identify all mounts that may be in the current mount 7537af1364fSEric W. Biederman * namespace not just a mount that happens to have some specified 7547af1364fSEric W. Biederman * parent mount. 7557af1364fSEric W. Biederman */ 7567af1364fSEric W. Biederman bool __is_local_mountpoint(struct dentry *dentry) 7577af1364fSEric W. Biederman { 7587af1364fSEric W. Biederman struct mnt_namespace *ns = current->nsproxy->mnt_ns; 7597af1364fSEric W. Biederman struct mount *mnt; 7607af1364fSEric W. Biederman bool is_covered = false; 7617af1364fSEric W. Biederman 7627af1364fSEric W. Biederman if (!d_mountpoint(dentry)) 7637af1364fSEric W. Biederman goto out; 7647af1364fSEric W. Biederman 7657af1364fSEric W. Biederman down_read(&namespace_sem); 7667af1364fSEric W. Biederman list_for_each_entry(mnt, &ns->list, mnt_list) { 7677af1364fSEric W. Biederman is_covered = (mnt->mnt_mountpoint == dentry); 7687af1364fSEric W. Biederman if (is_covered) 7697af1364fSEric W. Biederman break; 7707af1364fSEric W. Biederman } 7717af1364fSEric W. Biederman up_read(&namespace_sem); 7727af1364fSEric W. Biederman out: 7737af1364fSEric W. Biederman return is_covered; 7747af1364fSEric W. Biederman } 7757af1364fSEric W. Biederman 776e2dfa935SEric W. Biederman static struct mountpoint *lookup_mountpoint(struct dentry *dentry) 77784d17192SAl Viro { 7780818bf27SAl Viro struct hlist_head *chain = mp_hash(dentry); 77984d17192SAl Viro struct mountpoint *mp; 78084d17192SAl Viro 7810818bf27SAl Viro hlist_for_each_entry(mp, chain, m_hash) { 78284d17192SAl Viro if (mp->m_dentry == dentry) { 78384d17192SAl Viro /* might be worth a WARN_ON() */ 78484d17192SAl Viro if (d_unlinked(dentry)) 78584d17192SAl Viro return ERR_PTR(-ENOENT); 78684d17192SAl Viro mp->m_count++; 78784d17192SAl Viro return mp; 78884d17192SAl Viro } 78984d17192SAl Viro } 790e2dfa935SEric W. Biederman return NULL; 791e2dfa935SEric W. Biederman } 792e2dfa935SEric W. Biederman 7933895dbf8SEric W. Biederman static struct mountpoint *get_mountpoint(struct dentry *dentry) 794e2dfa935SEric W. Biederman { 7953895dbf8SEric W. Biederman struct mountpoint *mp, *new = NULL; 796e2dfa935SEric W. Biederman int ret; 79784d17192SAl Viro 7983895dbf8SEric W. Biederman if (d_mountpoint(dentry)) { 7993895dbf8SEric W. Biederman mountpoint: 8003895dbf8SEric W. Biederman read_seqlock_excl(&mount_lock); 8013895dbf8SEric W. Biederman mp = lookup_mountpoint(dentry); 8023895dbf8SEric W. Biederman read_sequnlock_excl(&mount_lock); 8033895dbf8SEric W. Biederman if (mp) 8043895dbf8SEric W. Biederman goto done; 80584d17192SAl Viro } 806eed81007SMiklos Szeredi 8073895dbf8SEric W. Biederman if (!new) 8083895dbf8SEric W. Biederman new = kmalloc(sizeof(struct mountpoint), GFP_KERNEL); 8093895dbf8SEric W. Biederman if (!new) 8103895dbf8SEric W. Biederman return ERR_PTR(-ENOMEM); 8113895dbf8SEric W. Biederman 8123895dbf8SEric W. Biederman 8133895dbf8SEric W. Biederman /* Exactly one processes may set d_mounted */ 8143895dbf8SEric W. Biederman ret = d_set_mounted(dentry); 8153895dbf8SEric W. Biederman 8163895dbf8SEric W. Biederman /* Someone else set d_mounted? */ 8173895dbf8SEric W. Biederman if (ret == -EBUSY) 8183895dbf8SEric W. Biederman goto mountpoint; 8193895dbf8SEric W. Biederman 8203895dbf8SEric W. Biederman /* The dentry is not available as a mountpoint? */ 8213895dbf8SEric W. Biederman mp = ERR_PTR(ret); 8223895dbf8SEric W. Biederman if (ret) 8233895dbf8SEric W. Biederman goto done; 8243895dbf8SEric W. Biederman 8253895dbf8SEric W. Biederman /* Add the new mountpoint to the hash table */ 8263895dbf8SEric W. Biederman read_seqlock_excl(&mount_lock); 8273895dbf8SEric W. Biederman new->m_dentry = dentry; 8283895dbf8SEric W. Biederman new->m_count = 1; 8293895dbf8SEric W. Biederman hlist_add_head(&new->m_hash, mp_hash(dentry)); 8303895dbf8SEric W. Biederman INIT_HLIST_HEAD(&new->m_list); 8313895dbf8SEric W. Biederman read_sequnlock_excl(&mount_lock); 8323895dbf8SEric W. Biederman 8333895dbf8SEric W. Biederman mp = new; 8343895dbf8SEric W. Biederman new = NULL; 8353895dbf8SEric W. Biederman done: 8363895dbf8SEric W. Biederman kfree(new); 83784d17192SAl Viro return mp; 83884d17192SAl Viro } 83984d17192SAl Viro 84084d17192SAl Viro static void put_mountpoint(struct mountpoint *mp) 84184d17192SAl Viro { 84284d17192SAl Viro if (!--mp->m_count) { 84384d17192SAl Viro struct dentry *dentry = mp->m_dentry; 8440a5eb7c8SEric W. Biederman BUG_ON(!hlist_empty(&mp->m_list)); 84584d17192SAl Viro spin_lock(&dentry->d_lock); 84684d17192SAl Viro dentry->d_flags &= ~DCACHE_MOUNTED; 84784d17192SAl Viro spin_unlock(&dentry->d_lock); 8480818bf27SAl Viro hlist_del(&mp->m_hash); 84984d17192SAl Viro kfree(mp); 85084d17192SAl Viro } 85184d17192SAl Viro } 85284d17192SAl Viro 853143c8c91SAl Viro static inline int check_mnt(struct mount *mnt) 8541da177e4SLinus Torvalds { 8556b3286edSKirill Korotaev return mnt->mnt_ns == current->nsproxy->mnt_ns; 8561da177e4SLinus Torvalds } 8571da177e4SLinus Torvalds 85899b7db7bSNick Piggin /* 85999b7db7bSNick Piggin * vfsmount lock must be held for write 86099b7db7bSNick Piggin */ 8616b3286edSKirill Korotaev static void touch_mnt_namespace(struct mnt_namespace *ns) 8625addc5ddSAl Viro { 8635addc5ddSAl Viro if (ns) { 8645addc5ddSAl Viro ns->event = ++event; 8655addc5ddSAl Viro wake_up_interruptible(&ns->poll); 8665addc5ddSAl Viro } 8675addc5ddSAl Viro } 8685addc5ddSAl Viro 86999b7db7bSNick Piggin /* 87099b7db7bSNick Piggin * vfsmount lock must be held for write 87199b7db7bSNick Piggin */ 8726b3286edSKirill Korotaev static void __touch_mnt_namespace(struct mnt_namespace *ns) 8735addc5ddSAl Viro { 8745addc5ddSAl Viro if (ns && ns->event != event) { 8755addc5ddSAl Viro ns->event = event; 8765addc5ddSAl Viro wake_up_interruptible(&ns->poll); 8775addc5ddSAl Viro } 8785addc5ddSAl Viro } 8795addc5ddSAl Viro 88099b7db7bSNick Piggin /* 88199b7db7bSNick Piggin * vfsmount lock must be held for write 88299b7db7bSNick Piggin */ 8837bdb11deSEric W. Biederman static void unhash_mnt(struct mount *mnt) 8841da177e4SLinus Torvalds { 8850714a533SAl Viro mnt->mnt_parent = mnt; 886a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 8876b41d536SAl Viro list_del_init(&mnt->mnt_child); 88838129a13SAl Viro hlist_del_init_rcu(&mnt->mnt_hash); 8890a5eb7c8SEric W. Biederman hlist_del_init(&mnt->mnt_mp_list); 89084d17192SAl Viro put_mountpoint(mnt->mnt_mp); 89184d17192SAl Viro mnt->mnt_mp = NULL; 8921da177e4SLinus Torvalds } 8931da177e4SLinus Torvalds 89499b7db7bSNick Piggin /* 89599b7db7bSNick Piggin * vfsmount lock must be held for write 89699b7db7bSNick Piggin */ 8977bdb11deSEric W. Biederman static void detach_mnt(struct mount *mnt, struct path *old_path) 8987bdb11deSEric W. Biederman { 8997bdb11deSEric W. Biederman old_path->dentry = mnt->mnt_mountpoint; 9007bdb11deSEric W. Biederman old_path->mnt = &mnt->mnt_parent->mnt; 9017bdb11deSEric W. Biederman unhash_mnt(mnt); 9027bdb11deSEric W. Biederman } 9037bdb11deSEric W. Biederman 9047bdb11deSEric W. Biederman /* 9057bdb11deSEric W. Biederman * vfsmount lock must be held for write 9067bdb11deSEric W. Biederman */ 9076a46c573SEric W. Biederman static void umount_mnt(struct mount *mnt) 9086a46c573SEric W. Biederman { 9096a46c573SEric W. Biederman /* old mountpoint will be dropped when we can do that */ 9106a46c573SEric W. Biederman mnt->mnt_ex_mountpoint = mnt->mnt_mountpoint; 9116a46c573SEric W. Biederman unhash_mnt(mnt); 9126a46c573SEric W. Biederman } 9136a46c573SEric W. Biederman 9146a46c573SEric W. Biederman /* 9156a46c573SEric W. Biederman * vfsmount lock must be held for write 9166a46c573SEric W. Biederman */ 91784d17192SAl Viro void mnt_set_mountpoint(struct mount *mnt, 91884d17192SAl Viro struct mountpoint *mp, 91944d964d6SAl Viro struct mount *child_mnt) 920b90fa9aeSRam Pai { 92184d17192SAl Viro mp->m_count++; 9223a2393d7SAl Viro mnt_add_count(mnt, 1); /* essentially, that's mntget */ 92384d17192SAl Viro child_mnt->mnt_mountpoint = dget(mp->m_dentry); 9243a2393d7SAl Viro child_mnt->mnt_parent = mnt; 92584d17192SAl Viro child_mnt->mnt_mp = mp; 9260a5eb7c8SEric W. Biederman hlist_add_head(&child_mnt->mnt_mp_list, &mp->m_list); 927b90fa9aeSRam Pai } 928b90fa9aeSRam Pai 9291064f874SEric W. Biederman static void __attach_mnt(struct mount *mnt, struct mount *parent) 9301064f874SEric W. Biederman { 9311064f874SEric W. Biederman hlist_add_head_rcu(&mnt->mnt_hash, 9321064f874SEric W. Biederman m_hash(&parent->mnt, mnt->mnt_mountpoint)); 9331064f874SEric W. Biederman list_add_tail(&mnt->mnt_child, &parent->mnt_mounts); 9341064f874SEric W. Biederman } 9351064f874SEric W. Biederman 93699b7db7bSNick Piggin /* 93799b7db7bSNick Piggin * vfsmount lock must be held for write 93899b7db7bSNick Piggin */ 93984d17192SAl Viro static void attach_mnt(struct mount *mnt, 94084d17192SAl Viro struct mount *parent, 94184d17192SAl Viro struct mountpoint *mp) 9421da177e4SLinus Torvalds { 94384d17192SAl Viro mnt_set_mountpoint(parent, mp, mnt); 9441064f874SEric W. Biederman __attach_mnt(mnt, parent); 945b90fa9aeSRam Pai } 946b90fa9aeSRam Pai 9471064f874SEric W. Biederman void mnt_change_mountpoint(struct mount *parent, struct mountpoint *mp, struct mount *mnt) 94812a5b529SAl Viro { 9491064f874SEric W. Biederman struct mountpoint *old_mp = mnt->mnt_mp; 9501064f874SEric W. Biederman struct dentry *old_mountpoint = mnt->mnt_mountpoint; 9511064f874SEric W. Biederman struct mount *old_parent = mnt->mnt_parent; 9521064f874SEric W. Biederman 9531064f874SEric W. Biederman list_del_init(&mnt->mnt_child); 9541064f874SEric W. Biederman hlist_del_init(&mnt->mnt_mp_list); 9551064f874SEric W. Biederman hlist_del_init_rcu(&mnt->mnt_hash); 9561064f874SEric W. Biederman 9571064f874SEric W. Biederman attach_mnt(mnt, parent, mp); 9581064f874SEric W. Biederman 9591064f874SEric W. Biederman put_mountpoint(old_mp); 9601064f874SEric W. Biederman 9611064f874SEric W. Biederman /* 9621064f874SEric W. Biederman * Safely avoid even the suggestion this code might sleep or 9631064f874SEric W. Biederman * lock the mount hash by taking advantage of the knowledge that 9641064f874SEric W. Biederman * mnt_change_mountpoint will not release the final reference 9651064f874SEric W. Biederman * to a mountpoint. 9661064f874SEric W. Biederman * 9671064f874SEric W. Biederman * During mounting, the mount passed in as the parent mount will 9681064f874SEric W. Biederman * continue to use the old mountpoint and during unmounting, the 9691064f874SEric W. Biederman * old mountpoint will continue to exist until namespace_unlock, 9701064f874SEric W. Biederman * which happens well after mnt_change_mountpoint. 9711064f874SEric W. Biederman */ 9721064f874SEric W. Biederman spin_lock(&old_mountpoint->d_lock); 9731064f874SEric W. Biederman old_mountpoint->d_lockref.count--; 9741064f874SEric W. Biederman spin_unlock(&old_mountpoint->d_lock); 9751064f874SEric W. Biederman 9761064f874SEric W. Biederman mnt_add_count(old_parent, -1); 97712a5b529SAl Viro } 97812a5b529SAl Viro 979b90fa9aeSRam Pai /* 98099b7db7bSNick Piggin * vfsmount lock must be held for write 981b90fa9aeSRam Pai */ 9821064f874SEric W. Biederman static void commit_tree(struct mount *mnt) 983b90fa9aeSRam Pai { 9840714a533SAl Viro struct mount *parent = mnt->mnt_parent; 98583adc753SAl Viro struct mount *m; 986b90fa9aeSRam Pai LIST_HEAD(head); 987143c8c91SAl Viro struct mnt_namespace *n = parent->mnt_ns; 988b90fa9aeSRam Pai 9890714a533SAl Viro BUG_ON(parent == mnt); 990b90fa9aeSRam Pai 9911a4eeaf2SAl Viro list_add_tail(&head, &mnt->mnt_list); 992f7a99c5bSAl Viro list_for_each_entry(m, &head, mnt_list) 993143c8c91SAl Viro m->mnt_ns = n; 994f03c6599SAl Viro 995b90fa9aeSRam Pai list_splice(&head, n->list.prev); 996b90fa9aeSRam Pai 997d2921684SEric W. Biederman n->mounts += n->pending_mounts; 998d2921684SEric W. Biederman n->pending_mounts = 0; 999d2921684SEric W. Biederman 10001064f874SEric W. Biederman __attach_mnt(mnt, parent); 10016b3286edSKirill Korotaev touch_mnt_namespace(n); 10021da177e4SLinus Torvalds } 10031da177e4SLinus Torvalds 1004909b0a88SAl Viro static struct mount *next_mnt(struct mount *p, struct mount *root) 10051da177e4SLinus Torvalds { 10066b41d536SAl Viro struct list_head *next = p->mnt_mounts.next; 10076b41d536SAl Viro if (next == &p->mnt_mounts) { 10081da177e4SLinus Torvalds while (1) { 1009909b0a88SAl Viro if (p == root) 10101da177e4SLinus Torvalds return NULL; 10116b41d536SAl Viro next = p->mnt_child.next; 10126b41d536SAl Viro if (next != &p->mnt_parent->mnt_mounts) 10131da177e4SLinus Torvalds break; 10140714a533SAl Viro p = p->mnt_parent; 10151da177e4SLinus Torvalds } 10161da177e4SLinus Torvalds } 10176b41d536SAl Viro return list_entry(next, struct mount, mnt_child); 10181da177e4SLinus Torvalds } 10191da177e4SLinus Torvalds 1020315fc83eSAl Viro static struct mount *skip_mnt_tree(struct mount *p) 10219676f0c6SRam Pai { 10226b41d536SAl Viro struct list_head *prev = p->mnt_mounts.prev; 10236b41d536SAl Viro while (prev != &p->mnt_mounts) { 10246b41d536SAl Viro p = list_entry(prev, struct mount, mnt_child); 10256b41d536SAl Viro prev = p->mnt_mounts.prev; 10269676f0c6SRam Pai } 10279676f0c6SRam Pai return p; 10289676f0c6SRam Pai } 10299676f0c6SRam Pai 10309d412a43SAl Viro struct vfsmount * 10319d412a43SAl Viro vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data) 10329d412a43SAl Viro { 1033b105e270SAl Viro struct mount *mnt; 10349d412a43SAl Viro struct dentry *root; 10359d412a43SAl Viro 10369d412a43SAl Viro if (!type) 10379d412a43SAl Viro return ERR_PTR(-ENODEV); 10389d412a43SAl Viro 10399d412a43SAl Viro mnt = alloc_vfsmnt(name); 10409d412a43SAl Viro if (!mnt) 10419d412a43SAl Viro return ERR_PTR(-ENOMEM); 10429d412a43SAl Viro 1043e462ec50SDavid Howells if (flags & SB_KERNMOUNT) 1044b105e270SAl Viro mnt->mnt.mnt_flags = MNT_INTERNAL; 10459d412a43SAl Viro 10469d412a43SAl Viro root = mount_fs(type, flags, name, data); 10479d412a43SAl Viro if (IS_ERR(root)) { 10488ffcb32eSDavid Howells mnt_free_id(mnt); 10499d412a43SAl Viro free_vfsmnt(mnt); 10509d412a43SAl Viro return ERR_CAST(root); 10519d412a43SAl Viro } 10529d412a43SAl Viro 1053b105e270SAl Viro mnt->mnt.mnt_root = root; 1054b105e270SAl Viro mnt->mnt.mnt_sb = root->d_sb; 1055a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 10560714a533SAl Viro mnt->mnt_parent = mnt; 1057719ea2fbSAl Viro lock_mount_hash(); 105839f7c4dbSMiklos Szeredi list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts); 1059719ea2fbSAl Viro unlock_mount_hash(); 1060b105e270SAl Viro return &mnt->mnt; 10619d412a43SAl Viro } 10629d412a43SAl Viro EXPORT_SYMBOL_GPL(vfs_kern_mount); 10639d412a43SAl Viro 106493faccbbSEric W. Biederman struct vfsmount * 106593faccbbSEric W. Biederman vfs_submount(const struct dentry *mountpoint, struct file_system_type *type, 106693faccbbSEric W. Biederman const char *name, void *data) 106793faccbbSEric W. Biederman { 106893faccbbSEric W. Biederman /* Until it is worked out how to pass the user namespace 106993faccbbSEric W. Biederman * through from the parent mount to the submount don't support 107093faccbbSEric W. Biederman * unprivileged mounts with submounts. 107193faccbbSEric W. Biederman */ 107293faccbbSEric W. Biederman if (mountpoint->d_sb->s_user_ns != &init_user_ns) 107393faccbbSEric W. Biederman return ERR_PTR(-EPERM); 107493faccbbSEric W. Biederman 1075e462ec50SDavid Howells return vfs_kern_mount(type, SB_SUBMOUNT, name, data); 107693faccbbSEric W. Biederman } 107793faccbbSEric W. Biederman EXPORT_SYMBOL_GPL(vfs_submount); 107893faccbbSEric W. Biederman 107987129cc0SAl Viro static struct mount *clone_mnt(struct mount *old, struct dentry *root, 108036341f64SRam Pai int flag) 10811da177e4SLinus Torvalds { 108287129cc0SAl Viro struct super_block *sb = old->mnt.mnt_sb; 1083be34d1a3SDavid Howells struct mount *mnt; 1084be34d1a3SDavid Howells int err; 10851da177e4SLinus Torvalds 1086be34d1a3SDavid Howells mnt = alloc_vfsmnt(old->mnt_devname); 1087be34d1a3SDavid Howells if (!mnt) 1088be34d1a3SDavid Howells return ERR_PTR(-ENOMEM); 1089be34d1a3SDavid Howells 10907a472ef4SEric W. Biederman if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE)) 109115169fe7SAl Viro mnt->mnt_group_id = 0; /* not a peer of original */ 1092719f5d7fSMiklos Szeredi else 109315169fe7SAl Viro mnt->mnt_group_id = old->mnt_group_id; 1094719f5d7fSMiklos Szeredi 109515169fe7SAl Viro if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) { 1096be34d1a3SDavid Howells err = mnt_alloc_group_id(mnt); 1097719f5d7fSMiklos Szeredi if (err) 1098719f5d7fSMiklos Szeredi goto out_free; 1099719f5d7fSMiklos Szeredi } 1100719f5d7fSMiklos Szeredi 110116a34adbSAl Viro mnt->mnt.mnt_flags = old->mnt.mnt_flags; 110216a34adbSAl Viro mnt->mnt.mnt_flags &= ~(MNT_WRITE_HOLD|MNT_MARKED|MNT_INTERNAL); 1103132c94e3SEric W. Biederman /* Don't allow unprivileged users to change mount flags */ 11049566d674SEric W. Biederman if (flag & CL_UNPRIVILEGED) { 11059566d674SEric W. Biederman mnt->mnt.mnt_flags |= MNT_LOCK_ATIME; 11069566d674SEric W. Biederman 11079566d674SEric W. Biederman if (mnt->mnt.mnt_flags & MNT_READONLY) 1108132c94e3SEric W. Biederman mnt->mnt.mnt_flags |= MNT_LOCK_READONLY; 1109132c94e3SEric W. Biederman 11109566d674SEric W. Biederman if (mnt->mnt.mnt_flags & MNT_NODEV) 11119566d674SEric W. Biederman mnt->mnt.mnt_flags |= MNT_LOCK_NODEV; 11129566d674SEric W. Biederman 11139566d674SEric W. Biederman if (mnt->mnt.mnt_flags & MNT_NOSUID) 11149566d674SEric W. Biederman mnt->mnt.mnt_flags |= MNT_LOCK_NOSUID; 11159566d674SEric W. Biederman 11169566d674SEric W. Biederman if (mnt->mnt.mnt_flags & MNT_NOEXEC) 11179566d674SEric W. Biederman mnt->mnt.mnt_flags |= MNT_LOCK_NOEXEC; 11189566d674SEric W. Biederman } 11199566d674SEric W. Biederman 11205ff9d8a6SEric W. Biederman /* Don't allow unprivileged users to reveal what is under a mount */ 1121381cacb1SEric W. Biederman if ((flag & CL_UNPRIVILEGED) && 1122381cacb1SEric W. Biederman (!(flag & CL_EXPIRE) || list_empty(&old->mnt_expire))) 11235ff9d8a6SEric W. Biederman mnt->mnt.mnt_flags |= MNT_LOCKED; 11245ff9d8a6SEric W. Biederman 11251da177e4SLinus Torvalds atomic_inc(&sb->s_active); 1126b105e270SAl Viro mnt->mnt.mnt_sb = sb; 1127b105e270SAl Viro mnt->mnt.mnt_root = dget(root); 1128a73324daSAl Viro mnt->mnt_mountpoint = mnt->mnt.mnt_root; 11290714a533SAl Viro mnt->mnt_parent = mnt; 1130719ea2fbSAl Viro lock_mount_hash(); 113139f7c4dbSMiklos Szeredi list_add_tail(&mnt->mnt_instance, &sb->s_mounts); 1132719ea2fbSAl Viro unlock_mount_hash(); 1133b90fa9aeSRam Pai 11347a472ef4SEric W. Biederman if ((flag & CL_SLAVE) || 11357a472ef4SEric W. Biederman ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) { 11366776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave_list); 113732301920SAl Viro mnt->mnt_master = old; 1138fc7be130SAl Viro CLEAR_MNT_SHARED(mnt); 11398aec0809SAl Viro } else if (!(flag & CL_PRIVATE)) { 1140fc7be130SAl Viro if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old)) 11416776db3dSAl Viro list_add(&mnt->mnt_share, &old->mnt_share); 1142d10e8defSAl Viro if (IS_MNT_SLAVE(old)) 11436776db3dSAl Viro list_add(&mnt->mnt_slave, &old->mnt_slave); 1144d10e8defSAl Viro mnt->mnt_master = old->mnt_master; 11455235d448SAl Viro } else { 11465235d448SAl Viro CLEAR_MNT_SHARED(mnt); 11475afe0022SRam Pai } 1148b90fa9aeSRam Pai if (flag & CL_MAKE_SHARED) 11490f0afb1dSAl Viro set_mnt_shared(mnt); 11501da177e4SLinus Torvalds 11511da177e4SLinus Torvalds /* stick the duplicate mount on the same expiry list 11521da177e4SLinus Torvalds * as the original if that was on one */ 115336341f64SRam Pai if (flag & CL_EXPIRE) { 11546776db3dSAl Viro if (!list_empty(&old->mnt_expire)) 11556776db3dSAl Viro list_add(&mnt->mnt_expire, &old->mnt_expire); 11561da177e4SLinus Torvalds } 1157be34d1a3SDavid Howells 1158cb338d06SAl Viro return mnt; 1159719f5d7fSMiklos Szeredi 1160719f5d7fSMiklos Szeredi out_free: 11618ffcb32eSDavid Howells mnt_free_id(mnt); 1162719f5d7fSMiklos Szeredi free_vfsmnt(mnt); 1163be34d1a3SDavid Howells return ERR_PTR(err); 11641da177e4SLinus Torvalds } 11651da177e4SLinus Torvalds 11669ea459e1SAl Viro static void cleanup_mnt(struct mount *mnt) 11679ea459e1SAl Viro { 11689ea459e1SAl Viro /* 11699ea459e1SAl Viro * This probably indicates that somebody messed 11709ea459e1SAl Viro * up a mnt_want/drop_write() pair. If this 11719ea459e1SAl Viro * happens, the filesystem was probably unable 11729ea459e1SAl Viro * to make r/w->r/o transitions. 11739ea459e1SAl Viro */ 11749ea459e1SAl Viro /* 11759ea459e1SAl Viro * The locking used to deal with mnt_count decrement provides barriers, 11769ea459e1SAl Viro * so mnt_get_writers() below is safe. 11779ea459e1SAl Viro */ 11789ea459e1SAl Viro WARN_ON(mnt_get_writers(mnt)); 11799ea459e1SAl Viro if (unlikely(mnt->mnt_pins.first)) 11809ea459e1SAl Viro mnt_pin_kill(mnt); 11819ea459e1SAl Viro fsnotify_vfsmount_delete(&mnt->mnt); 11829ea459e1SAl Viro dput(mnt->mnt.mnt_root); 11839ea459e1SAl Viro deactivate_super(mnt->mnt.mnt_sb); 11849ea459e1SAl Viro mnt_free_id(mnt); 11859ea459e1SAl Viro call_rcu(&mnt->mnt_rcu, delayed_free_vfsmnt); 11869ea459e1SAl Viro } 11879ea459e1SAl Viro 11889ea459e1SAl Viro static void __cleanup_mnt(struct rcu_head *head) 11899ea459e1SAl Viro { 11909ea459e1SAl Viro cleanup_mnt(container_of(head, struct mount, mnt_rcu)); 11919ea459e1SAl Viro } 11929ea459e1SAl Viro 11939ea459e1SAl Viro static LLIST_HEAD(delayed_mntput_list); 11949ea459e1SAl Viro static void delayed_mntput(struct work_struct *unused) 11959ea459e1SAl Viro { 11969ea459e1SAl Viro struct llist_node *node = llist_del_all(&delayed_mntput_list); 119729785735SByungchul Park struct mount *m, *t; 11989ea459e1SAl Viro 119929785735SByungchul Park llist_for_each_entry_safe(m, t, node, mnt_llist) 120029785735SByungchul Park cleanup_mnt(m); 12019ea459e1SAl Viro } 12029ea459e1SAl Viro static DECLARE_DELAYED_WORK(delayed_mntput_work, delayed_mntput); 12039ea459e1SAl Viro 1204900148dcSAl Viro static void mntput_no_expire(struct mount *mnt) 12057b7b1aceSAl Viro { 120648a066e7SAl Viro rcu_read_lock(); 12079ea0a46cSAl Viro if (likely(READ_ONCE(mnt->mnt_ns))) { 12089ea0a46cSAl Viro /* 12099ea0a46cSAl Viro * Since we don't do lock_mount_hash() here, 12109ea0a46cSAl Viro * ->mnt_ns can change under us. However, if it's 12119ea0a46cSAl Viro * non-NULL, then there's a reference that won't 12129ea0a46cSAl Viro * be dropped until after an RCU delay done after 12139ea0a46cSAl Viro * turning ->mnt_ns NULL. So if we observe it 12149ea0a46cSAl Viro * non-NULL under rcu_read_lock(), the reference 12159ea0a46cSAl Viro * we are dropping is not the final one. 12169ea0a46cSAl Viro */ 1217aa9c0e07SAl Viro mnt_add_count(mnt, -1); 121848a066e7SAl Viro rcu_read_unlock(); 121999b7db7bSNick Piggin return; 1220b3e19d92SNick Piggin } 1221719ea2fbSAl Viro lock_mount_hash(); 1222119e1ef8SAl Viro /* 1223119e1ef8SAl Viro * make sure that if __legitimize_mnt() has not seen us grab 1224119e1ef8SAl Viro * mount_lock, we'll see their refcount increment here. 1225119e1ef8SAl Viro */ 1226119e1ef8SAl Viro smp_mb(); 12279ea0a46cSAl Viro mnt_add_count(mnt, -1); 1228b3e19d92SNick Piggin if (mnt_get_count(mnt)) { 122948a066e7SAl Viro rcu_read_unlock(); 1230719ea2fbSAl Viro unlock_mount_hash(); 123199b7db7bSNick Piggin return; 123299b7db7bSNick Piggin } 123348a066e7SAl Viro if (unlikely(mnt->mnt.mnt_flags & MNT_DOOMED)) { 123448a066e7SAl Viro rcu_read_unlock(); 123548a066e7SAl Viro unlock_mount_hash(); 123648a066e7SAl Viro return; 123748a066e7SAl Viro } 123848a066e7SAl Viro mnt->mnt.mnt_flags |= MNT_DOOMED; 123948a066e7SAl Viro rcu_read_unlock(); 1240962830dfSAndi Kleen 124139f7c4dbSMiklos Szeredi list_del(&mnt->mnt_instance); 1242ce07d891SEric W. Biederman 1243ce07d891SEric W. Biederman if (unlikely(!list_empty(&mnt->mnt_mounts))) { 1244ce07d891SEric W. Biederman struct mount *p, *tmp; 1245ce07d891SEric W. Biederman list_for_each_entry_safe(p, tmp, &mnt->mnt_mounts, mnt_child) { 1246ce07d891SEric W. Biederman umount_mnt(p); 1247ce07d891SEric W. Biederman } 1248ce07d891SEric W. Biederman } 1249719ea2fbSAl Viro unlock_mount_hash(); 1250649a795aSAl Viro 12519ea459e1SAl Viro if (likely(!(mnt->mnt.mnt_flags & MNT_INTERNAL))) { 12529ea459e1SAl Viro struct task_struct *task = current; 12539ea459e1SAl Viro if (likely(!(task->flags & PF_KTHREAD))) { 12549ea459e1SAl Viro init_task_work(&mnt->mnt_rcu, __cleanup_mnt); 12559ea459e1SAl Viro if (!task_work_add(task, &mnt->mnt_rcu, true)) 12569ea459e1SAl Viro return; 12579ea459e1SAl Viro } 12589ea459e1SAl Viro if (llist_add(&mnt->mnt_llist, &delayed_mntput_list)) 12599ea459e1SAl Viro schedule_delayed_work(&delayed_mntput_work, 1); 12609ea459e1SAl Viro return; 12619ea459e1SAl Viro } 12629ea459e1SAl Viro cleanup_mnt(mnt); 1263b3e19d92SNick Piggin } 1264b3e19d92SNick Piggin 1265b3e19d92SNick Piggin void mntput(struct vfsmount *mnt) 1266b3e19d92SNick Piggin { 1267b3e19d92SNick Piggin if (mnt) { 1268863d684fSAl Viro struct mount *m = real_mount(mnt); 1269b3e19d92SNick Piggin /* avoid cacheline pingpong, hope gcc doesn't get "smart" */ 1270863d684fSAl Viro if (unlikely(m->mnt_expiry_mark)) 1271863d684fSAl Viro m->mnt_expiry_mark = 0; 1272863d684fSAl Viro mntput_no_expire(m); 1273b3e19d92SNick Piggin } 1274b3e19d92SNick Piggin } 1275b3e19d92SNick Piggin EXPORT_SYMBOL(mntput); 1276b3e19d92SNick Piggin 1277b3e19d92SNick Piggin struct vfsmount *mntget(struct vfsmount *mnt) 1278b3e19d92SNick Piggin { 1279b3e19d92SNick Piggin if (mnt) 128083adc753SAl Viro mnt_add_count(real_mount(mnt), 1); 1281b3e19d92SNick Piggin return mnt; 1282b3e19d92SNick Piggin } 1283b3e19d92SNick Piggin EXPORT_SYMBOL(mntget); 1284b3e19d92SNick Piggin 1285c6609c0aSIan Kent /* path_is_mountpoint() - Check if path is a mount in the current 1286c6609c0aSIan Kent * namespace. 1287c6609c0aSIan Kent * 1288c6609c0aSIan Kent * d_mountpoint() can only be used reliably to establish if a dentry is 1289c6609c0aSIan Kent * not mounted in any namespace and that common case is handled inline. 1290c6609c0aSIan Kent * d_mountpoint() isn't aware of the possibility there may be multiple 1291c6609c0aSIan Kent * mounts using a given dentry in a different namespace. This function 1292c6609c0aSIan Kent * checks if the passed in path is a mountpoint rather than the dentry 1293c6609c0aSIan Kent * alone. 1294c6609c0aSIan Kent */ 1295c6609c0aSIan Kent bool path_is_mountpoint(const struct path *path) 1296c6609c0aSIan Kent { 1297c6609c0aSIan Kent unsigned seq; 1298c6609c0aSIan Kent bool res; 1299c6609c0aSIan Kent 1300c6609c0aSIan Kent if (!d_mountpoint(path->dentry)) 1301c6609c0aSIan Kent return false; 1302c6609c0aSIan Kent 1303c6609c0aSIan Kent rcu_read_lock(); 1304c6609c0aSIan Kent do { 1305c6609c0aSIan Kent seq = read_seqbegin(&mount_lock); 1306c6609c0aSIan Kent res = __path_is_mountpoint(path); 1307c6609c0aSIan Kent } while (read_seqretry(&mount_lock, seq)); 1308c6609c0aSIan Kent rcu_read_unlock(); 1309c6609c0aSIan Kent 1310c6609c0aSIan Kent return res; 1311c6609c0aSIan Kent } 1312c6609c0aSIan Kent EXPORT_SYMBOL(path_is_mountpoint); 1313c6609c0aSIan Kent 1314ca71cf71SAl Viro struct vfsmount *mnt_clone_internal(const struct path *path) 13157b7b1aceSAl Viro { 13163064c356SAl Viro struct mount *p; 13173064c356SAl Viro p = clone_mnt(real_mount(path->mnt), path->dentry, CL_PRIVATE); 13183064c356SAl Viro if (IS_ERR(p)) 13193064c356SAl Viro return ERR_CAST(p); 13203064c356SAl Viro p->mnt.mnt_flags |= MNT_INTERNAL; 13213064c356SAl Viro return &p->mnt; 13227b7b1aceSAl Viro } 13231da177e4SLinus Torvalds 1324a1a2c409SMiklos Szeredi #ifdef CONFIG_PROC_FS 13250226f492SAl Viro /* iterator; we want it to have access to namespace_sem, thus here... */ 13261da177e4SLinus Torvalds static void *m_start(struct seq_file *m, loff_t *pos) 13271da177e4SLinus Torvalds { 1328ede1bf0dSYann Droneaud struct proc_mounts *p = m->private; 13291da177e4SLinus Torvalds 1330390c6843SRam Pai down_read(&namespace_sem); 1331c7999c36SAl Viro if (p->cached_event == p->ns->event) { 1332c7999c36SAl Viro void *v = p->cached_mount; 1333c7999c36SAl Viro if (*pos == p->cached_index) 1334c7999c36SAl Viro return v; 1335c7999c36SAl Viro if (*pos == p->cached_index + 1) { 1336c7999c36SAl Viro v = seq_list_next(v, &p->ns->list, &p->cached_index); 1337c7999c36SAl Viro return p->cached_mount = v; 1338c7999c36SAl Viro } 1339c7999c36SAl Viro } 1340c7999c36SAl Viro 1341c7999c36SAl Viro p->cached_event = p->ns->event; 1342c7999c36SAl Viro p->cached_mount = seq_list_start(&p->ns->list, *pos); 1343c7999c36SAl Viro p->cached_index = *pos; 1344c7999c36SAl Viro return p->cached_mount; 13451da177e4SLinus Torvalds } 13461da177e4SLinus Torvalds 13471da177e4SLinus Torvalds static void *m_next(struct seq_file *m, void *v, loff_t *pos) 13481da177e4SLinus Torvalds { 1349ede1bf0dSYann Droneaud struct proc_mounts *p = m->private; 1350b0765fb8SPavel Emelianov 1351c7999c36SAl Viro p->cached_mount = seq_list_next(v, &p->ns->list, pos); 1352c7999c36SAl Viro p->cached_index = *pos; 1353c7999c36SAl Viro return p->cached_mount; 13541da177e4SLinus Torvalds } 13551da177e4SLinus Torvalds 13561da177e4SLinus Torvalds static void m_stop(struct seq_file *m, void *v) 13571da177e4SLinus Torvalds { 1358390c6843SRam Pai up_read(&namespace_sem); 13591da177e4SLinus Torvalds } 13601da177e4SLinus Torvalds 13610226f492SAl Viro static int m_show(struct seq_file *m, void *v) 13629f5596afSAl Viro { 1363ede1bf0dSYann Droneaud struct proc_mounts *p = m->private; 13641a4eeaf2SAl Viro struct mount *r = list_entry(v, struct mount, mnt_list); 13650226f492SAl Viro return p->show(m, &r->mnt); 13661da177e4SLinus Torvalds } 13671da177e4SLinus Torvalds 1368a1a2c409SMiklos Szeredi const struct seq_operations mounts_op = { 13691da177e4SLinus Torvalds .start = m_start, 13701da177e4SLinus Torvalds .next = m_next, 13711da177e4SLinus Torvalds .stop = m_stop, 13720226f492SAl Viro .show = m_show, 1373b4629fe2SChuck Lever }; 1374a1a2c409SMiklos Szeredi #endif /* CONFIG_PROC_FS */ 1375b4629fe2SChuck Lever 13761da177e4SLinus Torvalds /** 13771da177e4SLinus Torvalds * may_umount_tree - check if a mount tree is busy 13781da177e4SLinus Torvalds * @mnt: root of mount tree 13791da177e4SLinus Torvalds * 13801da177e4SLinus Torvalds * This is called to check if a tree of mounts has any 13811da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts that are 13821da177e4SLinus Torvalds * busy. 13831da177e4SLinus Torvalds */ 1384909b0a88SAl Viro int may_umount_tree(struct vfsmount *m) 13851da177e4SLinus Torvalds { 1386909b0a88SAl Viro struct mount *mnt = real_mount(m); 138736341f64SRam Pai int actual_refs = 0; 138836341f64SRam Pai int minimum_refs = 0; 1389315fc83eSAl Viro struct mount *p; 1390909b0a88SAl Viro BUG_ON(!m); 13911da177e4SLinus Torvalds 1392b3e19d92SNick Piggin /* write lock needed for mnt_get_count */ 1393719ea2fbSAl Viro lock_mount_hash(); 1394909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 139583adc753SAl Viro actual_refs += mnt_get_count(p); 13961da177e4SLinus Torvalds minimum_refs += 2; 13971da177e4SLinus Torvalds } 1398719ea2fbSAl Viro unlock_mount_hash(); 13991da177e4SLinus Torvalds 14001da177e4SLinus Torvalds if (actual_refs > minimum_refs) 14011da177e4SLinus Torvalds return 0; 1402e3474a8eSIan Kent 1403e3474a8eSIan Kent return 1; 14041da177e4SLinus Torvalds } 14051da177e4SLinus Torvalds 14061da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount_tree); 14071da177e4SLinus Torvalds 14081da177e4SLinus Torvalds /** 14091da177e4SLinus Torvalds * may_umount - check if a mount point is busy 14101da177e4SLinus Torvalds * @mnt: root of mount 14111da177e4SLinus Torvalds * 14121da177e4SLinus Torvalds * This is called to check if a mount point has any 14131da177e4SLinus Torvalds * open files, pwds, chroots or sub mounts. If the 14141da177e4SLinus Torvalds * mount has sub mounts this will return busy 14151da177e4SLinus Torvalds * regardless of whether the sub mounts are busy. 14161da177e4SLinus Torvalds * 14171da177e4SLinus Torvalds * Doesn't take quota and stuff into account. IOW, in some cases it will 14181da177e4SLinus Torvalds * give false negatives. The main reason why it's here is that we need 14191da177e4SLinus Torvalds * a non-destructive way to look for easily umountable filesystems. 14201da177e4SLinus Torvalds */ 14211da177e4SLinus Torvalds int may_umount(struct vfsmount *mnt) 14221da177e4SLinus Torvalds { 1423e3474a8eSIan Kent int ret = 1; 14248ad08d8aSAl Viro down_read(&namespace_sem); 1425719ea2fbSAl Viro lock_mount_hash(); 14261ab59738SAl Viro if (propagate_mount_busy(real_mount(mnt), 2)) 1427e3474a8eSIan Kent ret = 0; 1428719ea2fbSAl Viro unlock_mount_hash(); 14298ad08d8aSAl Viro up_read(&namespace_sem); 1430a05964f3SRam Pai return ret; 14311da177e4SLinus Torvalds } 14321da177e4SLinus Torvalds 14331da177e4SLinus Torvalds EXPORT_SYMBOL(may_umount); 14341da177e4SLinus Torvalds 143538129a13SAl Viro static HLIST_HEAD(unmounted); /* protected by namespace_sem */ 1436e3197d83SAl Viro 143797216be0SAl Viro static void namespace_unlock(void) 14381da177e4SLinus Torvalds { 1439a3b3c562SEric W. Biederman struct hlist_head head; 144097216be0SAl Viro 1441a3b3c562SEric W. Biederman hlist_move_list(&unmounted, &head); 1442a3b3c562SEric W. Biederman 144397216be0SAl Viro up_write(&namespace_sem); 1444a3b3c562SEric W. Biederman 1445a3b3c562SEric W. Biederman if (likely(hlist_empty(&head))) 144697216be0SAl Viro return; 144797216be0SAl Viro 144848a066e7SAl Viro synchronize_rcu(); 144948a066e7SAl Viro 145087b95ce0SAl Viro group_pin_kill(&head); 145170fbcdf4SRam Pai } 145270fbcdf4SRam Pai 145397216be0SAl Viro static inline void namespace_lock(void) 1454e3197d83SAl Viro { 145597216be0SAl Viro down_write(&namespace_sem); 1456e3197d83SAl Viro } 1457e3197d83SAl Viro 1458e819f152SEric W. Biederman enum umount_tree_flags { 1459e819f152SEric W. Biederman UMOUNT_SYNC = 1, 1460e819f152SEric W. Biederman UMOUNT_PROPAGATE = 2, 1461e0c9c0afSEric W. Biederman UMOUNT_CONNECTED = 4, 1462e819f152SEric W. Biederman }; 1463f2d0a123SEric W. Biederman 1464f2d0a123SEric W. Biederman static bool disconnect_mount(struct mount *mnt, enum umount_tree_flags how) 1465f2d0a123SEric W. Biederman { 1466f2d0a123SEric W. Biederman /* Leaving mounts connected is only valid for lazy umounts */ 1467f2d0a123SEric W. Biederman if (how & UMOUNT_SYNC) 1468f2d0a123SEric W. Biederman return true; 1469f2d0a123SEric W. Biederman 1470f2d0a123SEric W. Biederman /* A mount without a parent has nothing to be connected to */ 1471f2d0a123SEric W. Biederman if (!mnt_has_parent(mnt)) 1472f2d0a123SEric W. Biederman return true; 1473f2d0a123SEric W. Biederman 1474f2d0a123SEric W. Biederman /* Because the reference counting rules change when mounts are 1475f2d0a123SEric W. Biederman * unmounted and connected, umounted mounts may not be 1476f2d0a123SEric W. Biederman * connected to mounted mounts. 1477f2d0a123SEric W. Biederman */ 1478f2d0a123SEric W. Biederman if (!(mnt->mnt_parent->mnt.mnt_flags & MNT_UMOUNT)) 1479f2d0a123SEric W. Biederman return true; 1480f2d0a123SEric W. Biederman 1481f2d0a123SEric W. Biederman /* Has it been requested that the mount remain connected? */ 1482f2d0a123SEric W. Biederman if (how & UMOUNT_CONNECTED) 1483f2d0a123SEric W. Biederman return false; 1484f2d0a123SEric W. Biederman 1485f2d0a123SEric W. Biederman /* Is the mount locked such that it needs to remain connected? */ 1486f2d0a123SEric W. Biederman if (IS_MNT_LOCKED(mnt)) 1487f2d0a123SEric W. Biederman return false; 1488f2d0a123SEric W. Biederman 1489f2d0a123SEric W. Biederman /* By default disconnect the mount */ 1490f2d0a123SEric W. Biederman return true; 1491f2d0a123SEric W. Biederman } 1492f2d0a123SEric W. Biederman 149399b7db7bSNick Piggin /* 149448a066e7SAl Viro * mount_lock must be held 149599b7db7bSNick Piggin * namespace_sem must be held for write 149699b7db7bSNick Piggin */ 1497e819f152SEric W. Biederman static void umount_tree(struct mount *mnt, enum umount_tree_flags how) 149870fbcdf4SRam Pai { 1499c003b26fSEric W. Biederman LIST_HEAD(tmp_list); 1500315fc83eSAl Viro struct mount *p; 150170fbcdf4SRam Pai 15025d88457eSEric W. Biederman if (how & UMOUNT_PROPAGATE) 15035d88457eSEric W. Biederman propagate_mount_unlock(mnt); 15045d88457eSEric W. Biederman 1505c003b26fSEric W. Biederman /* Gather the mounts to umount */ 1506590ce4bcSEric W. Biederman for (p = mnt; p; p = next_mnt(p, mnt)) { 1507590ce4bcSEric W. Biederman p->mnt.mnt_flags |= MNT_UMOUNT; 1508c003b26fSEric W. Biederman list_move(&p->mnt_list, &tmp_list); 1509590ce4bcSEric W. Biederman } 1510c003b26fSEric W. Biederman 1511411a938bSEric W. Biederman /* Hide the mounts from mnt_mounts */ 1512c003b26fSEric W. Biederman list_for_each_entry(p, &tmp_list, mnt_list) { 1513c003b26fSEric W. Biederman list_del_init(&p->mnt_child); 151438129a13SAl Viro } 151570fbcdf4SRam Pai 1516c003b26fSEric W. Biederman /* Add propogated mounts to the tmp_list */ 1517e819f152SEric W. Biederman if (how & UMOUNT_PROPAGATE) 15187b8a53fdSAl Viro propagate_umount(&tmp_list); 1519a05964f3SRam Pai 1520c003b26fSEric W. Biederman while (!list_empty(&tmp_list)) { 1521d2921684SEric W. Biederman struct mnt_namespace *ns; 1522ce07d891SEric W. Biederman bool disconnect; 1523c003b26fSEric W. Biederman p = list_first_entry(&tmp_list, struct mount, mnt_list); 15246776db3dSAl Viro list_del_init(&p->mnt_expire); 15251a4eeaf2SAl Viro list_del_init(&p->mnt_list); 1526d2921684SEric W. Biederman ns = p->mnt_ns; 1527d2921684SEric W. Biederman if (ns) { 1528d2921684SEric W. Biederman ns->mounts--; 1529d2921684SEric W. Biederman __touch_mnt_namespace(ns); 1530d2921684SEric W. Biederman } 153163d37a84SAl Viro p->mnt_ns = NULL; 1532e819f152SEric W. Biederman if (how & UMOUNT_SYNC) 153348a066e7SAl Viro p->mnt.mnt_flags |= MNT_SYNC_UMOUNT; 153487b95ce0SAl Viro 1535f2d0a123SEric W. Biederman disconnect = disconnect_mount(p, how); 1536ce07d891SEric W. Biederman 1537ce07d891SEric W. Biederman pin_insert_group(&p->mnt_umount, &p->mnt_parent->mnt, 1538ce07d891SEric W. Biederman disconnect ? &unmounted : NULL); 1539676da58dSAl Viro if (mnt_has_parent(p)) { 154081b6b061SAl Viro mnt_add_count(p->mnt_parent, -1); 1541ce07d891SEric W. Biederman if (!disconnect) { 1542ce07d891SEric W. Biederman /* Don't forget about p */ 1543ce07d891SEric W. Biederman list_add_tail(&p->mnt_child, &p->mnt_parent->mnt_mounts); 1544ce07d891SEric W. Biederman } else { 15456a46c573SEric W. Biederman umount_mnt(p); 15467c4b93d8SAl Viro } 1547ce07d891SEric W. Biederman } 15480f0afb1dSAl Viro change_mnt_propagation(p, MS_PRIVATE); 154938129a13SAl Viro } 15501da177e4SLinus Torvalds } 15511da177e4SLinus Torvalds 1552b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt); 1553c35038beSAl Viro 15541ab59738SAl Viro static int do_umount(struct mount *mnt, int flags) 15551da177e4SLinus Torvalds { 15561ab59738SAl Viro struct super_block *sb = mnt->mnt.mnt_sb; 15571da177e4SLinus Torvalds int retval; 15581da177e4SLinus Torvalds 15591ab59738SAl Viro retval = security_sb_umount(&mnt->mnt, flags); 15601da177e4SLinus Torvalds if (retval) 15611da177e4SLinus Torvalds return retval; 15621da177e4SLinus Torvalds 15631da177e4SLinus Torvalds /* 15641da177e4SLinus Torvalds * Allow userspace to request a mountpoint be expired rather than 15651da177e4SLinus Torvalds * unmounting unconditionally. Unmount only happens if: 15661da177e4SLinus Torvalds * (1) the mark is already set (the mark is cleared by mntput()) 15671da177e4SLinus Torvalds * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount] 15681da177e4SLinus Torvalds */ 15691da177e4SLinus Torvalds if (flags & MNT_EXPIRE) { 15701ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt || 15711da177e4SLinus Torvalds flags & (MNT_FORCE | MNT_DETACH)) 15721da177e4SLinus Torvalds return -EINVAL; 15731da177e4SLinus Torvalds 1574b3e19d92SNick Piggin /* 1575b3e19d92SNick Piggin * probably don't strictly need the lock here if we examined 1576b3e19d92SNick Piggin * all race cases, but it's a slowpath. 1577b3e19d92SNick Piggin */ 1578719ea2fbSAl Viro lock_mount_hash(); 157983adc753SAl Viro if (mnt_get_count(mnt) != 2) { 1580719ea2fbSAl Viro unlock_mount_hash(); 15811da177e4SLinus Torvalds return -EBUSY; 1582b3e19d92SNick Piggin } 1583719ea2fbSAl Viro unlock_mount_hash(); 15841da177e4SLinus Torvalds 1585863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1)) 15861da177e4SLinus Torvalds return -EAGAIN; 15871da177e4SLinus Torvalds } 15881da177e4SLinus Torvalds 15891da177e4SLinus Torvalds /* 15901da177e4SLinus Torvalds * If we may have to abort operations to get out of this 15911da177e4SLinus Torvalds * mount, and they will themselves hold resources we must 15921da177e4SLinus Torvalds * allow the fs to do things. In the Unix tradition of 15931da177e4SLinus Torvalds * 'Gee thats tricky lets do it in userspace' the umount_begin 15941da177e4SLinus Torvalds * might fail to complete on the first run through as other tasks 15951da177e4SLinus Torvalds * must return, and the like. Thats for the mount program to worry 15961da177e4SLinus Torvalds * about for the moment. 15971da177e4SLinus Torvalds */ 15981da177e4SLinus Torvalds 159942faad99SAl Viro if (flags & MNT_FORCE && sb->s_op->umount_begin) { 160042faad99SAl Viro sb->s_op->umount_begin(sb); 160142faad99SAl Viro } 16021da177e4SLinus Torvalds 16031da177e4SLinus Torvalds /* 16041da177e4SLinus Torvalds * No sense to grab the lock for this test, but test itself looks 16051da177e4SLinus Torvalds * somewhat bogus. Suggestions for better replacement? 16061da177e4SLinus Torvalds * Ho-hum... In principle, we might treat that as umount + switch 16071da177e4SLinus Torvalds * to rootfs. GC would eventually take care of the old vfsmount. 16081da177e4SLinus Torvalds * Actually it makes sense, especially if rootfs would contain a 16091da177e4SLinus Torvalds * /reboot - static binary that would close all descriptors and 16101da177e4SLinus Torvalds * call reboot(9). Then init(8) could umount root and exec /reboot. 16111da177e4SLinus Torvalds */ 16121ab59738SAl Viro if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) { 16131da177e4SLinus Torvalds /* 16141da177e4SLinus Torvalds * Special case for "unmounting" root ... 16151da177e4SLinus Torvalds * we just try to remount it readonly. 16161da177e4SLinus Torvalds */ 1617bc6155d1SEric W. Biederman if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN)) 1618a1480dccSAndy Lutomirski return -EPERM; 16191da177e4SLinus Torvalds down_write(&sb->s_umount); 1620bc98a42cSDavid Howells if (!sb_rdonly(sb)) 1621e462ec50SDavid Howells retval = do_remount_sb(sb, SB_RDONLY, NULL, 0); 16221da177e4SLinus Torvalds up_write(&sb->s_umount); 16231da177e4SLinus Torvalds return retval; 16241da177e4SLinus Torvalds } 16251da177e4SLinus Torvalds 162697216be0SAl Viro namespace_lock(); 1627719ea2fbSAl Viro lock_mount_hash(); 16285addc5ddSAl Viro event++; 16291da177e4SLinus Torvalds 163048a066e7SAl Viro if (flags & MNT_DETACH) { 163148a066e7SAl Viro if (!list_empty(&mnt->mnt_list)) 1632e819f152SEric W. Biederman umount_tree(mnt, UMOUNT_PROPAGATE); 163348a066e7SAl Viro retval = 0; 163448a066e7SAl Viro } else { 1635b54b9be7SAl Viro shrink_submounts(mnt); 16361da177e4SLinus Torvalds retval = -EBUSY; 163748a066e7SAl Viro if (!propagate_mount_busy(mnt, 2)) { 16381a4eeaf2SAl Viro if (!list_empty(&mnt->mnt_list)) 1639e819f152SEric W. Biederman umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC); 16401da177e4SLinus Torvalds retval = 0; 16411da177e4SLinus Torvalds } 164248a066e7SAl Viro } 1643719ea2fbSAl Viro unlock_mount_hash(); 1644e3197d83SAl Viro namespace_unlock(); 16451da177e4SLinus Torvalds return retval; 16461da177e4SLinus Torvalds } 16471da177e4SLinus Torvalds 16481da177e4SLinus Torvalds /* 164980b5dce8SEric W. Biederman * __detach_mounts - lazily unmount all mounts on the specified dentry 165080b5dce8SEric W. Biederman * 165180b5dce8SEric W. Biederman * During unlink, rmdir, and d_drop it is possible to loose the path 165280b5dce8SEric W. Biederman * to an existing mountpoint, and wind up leaking the mount. 165380b5dce8SEric W. Biederman * detach_mounts allows lazily unmounting those mounts instead of 165480b5dce8SEric W. Biederman * leaking them. 165580b5dce8SEric W. Biederman * 165680b5dce8SEric W. Biederman * The caller may hold dentry->d_inode->i_mutex. 165780b5dce8SEric W. Biederman */ 165880b5dce8SEric W. Biederman void __detach_mounts(struct dentry *dentry) 165980b5dce8SEric W. Biederman { 166080b5dce8SEric W. Biederman struct mountpoint *mp; 166180b5dce8SEric W. Biederman struct mount *mnt; 166280b5dce8SEric W. Biederman 166380b5dce8SEric W. Biederman namespace_lock(); 16643895dbf8SEric W. Biederman lock_mount_hash(); 166580b5dce8SEric W. Biederman mp = lookup_mountpoint(dentry); 1666f53e5797SEric W. Biederman if (IS_ERR_OR_NULL(mp)) 166780b5dce8SEric W. Biederman goto out_unlock; 166880b5dce8SEric W. Biederman 1669e06b933eSAndrey Ulanov event++; 167080b5dce8SEric W. Biederman while (!hlist_empty(&mp->m_list)) { 167180b5dce8SEric W. Biederman mnt = hlist_entry(mp->m_list.first, struct mount, mnt_mp_list); 1672ce07d891SEric W. Biederman if (mnt->mnt.mnt_flags & MNT_UMOUNT) { 1673fe78fcc8SEric W. Biederman hlist_add_head(&mnt->mnt_umount.s_list, &unmounted); 1674fe78fcc8SEric W. Biederman umount_mnt(mnt); 1675ce07d891SEric W. Biederman } 1676e0c9c0afSEric W. Biederman else umount_tree(mnt, UMOUNT_CONNECTED); 167780b5dce8SEric W. Biederman } 167880b5dce8SEric W. Biederman put_mountpoint(mp); 167980b5dce8SEric W. Biederman out_unlock: 16803895dbf8SEric W. Biederman unlock_mount_hash(); 168180b5dce8SEric W. Biederman namespace_unlock(); 168280b5dce8SEric W. Biederman } 168380b5dce8SEric W. Biederman 168480b5dce8SEric W. Biederman /* 16859b40bc90SAl Viro * Is the caller allowed to modify his namespace? 16869b40bc90SAl Viro */ 16879b40bc90SAl Viro static inline bool may_mount(void) 16889b40bc90SAl Viro { 16899b40bc90SAl Viro return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN); 16909b40bc90SAl Viro } 16919b40bc90SAl Viro 16929e8925b6SJeff Layton static inline bool may_mandlock(void) 16939e8925b6SJeff Layton { 16949e8925b6SJeff Layton #ifndef CONFIG_MANDATORY_FILE_LOCKING 16959e8925b6SJeff Layton return false; 16969e8925b6SJeff Layton #endif 169795ace754SEric W. Biederman return capable(CAP_SYS_ADMIN); 16989e8925b6SJeff Layton } 16999e8925b6SJeff Layton 17009b40bc90SAl Viro /* 17011da177e4SLinus Torvalds * Now umount can handle mount points as well as block devices. 17021da177e4SLinus Torvalds * This is important for filesystems which use unnamed block devices. 17031da177e4SLinus Torvalds * 17041da177e4SLinus Torvalds * We now support a flag for forced unmount like the other 'big iron' 17051da177e4SLinus Torvalds * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD 17061da177e4SLinus Torvalds */ 17071da177e4SLinus Torvalds 17083a18ef5cSDominik Brodowski int ksys_umount(char __user *name, int flags) 17091da177e4SLinus Torvalds { 17102d8f3038SAl Viro struct path path; 1711900148dcSAl Viro struct mount *mnt; 17121da177e4SLinus Torvalds int retval; 1713db1f05bbSMiklos Szeredi int lookup_flags = 0; 17141da177e4SLinus Torvalds 1715db1f05bbSMiklos Szeredi if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW)) 1716db1f05bbSMiklos Szeredi return -EINVAL; 1717db1f05bbSMiklos Szeredi 17189b40bc90SAl Viro if (!may_mount()) 17199b40bc90SAl Viro return -EPERM; 17209b40bc90SAl Viro 1721db1f05bbSMiklos Szeredi if (!(flags & UMOUNT_NOFOLLOW)) 1722db1f05bbSMiklos Szeredi lookup_flags |= LOOKUP_FOLLOW; 1723db1f05bbSMiklos Szeredi 1724197df04cSAl Viro retval = user_path_mountpoint_at(AT_FDCWD, name, lookup_flags, &path); 17251da177e4SLinus Torvalds if (retval) 17261da177e4SLinus Torvalds goto out; 1727900148dcSAl Viro mnt = real_mount(path.mnt); 17281da177e4SLinus Torvalds retval = -EINVAL; 17292d8f3038SAl Viro if (path.dentry != path.mnt->mnt_root) 17301da177e4SLinus Torvalds goto dput_and_out; 1731143c8c91SAl Viro if (!check_mnt(mnt)) 17321da177e4SLinus Torvalds goto dput_and_out; 17335ff9d8a6SEric W. Biederman if (mnt->mnt.mnt_flags & MNT_LOCKED) 17345ff9d8a6SEric W. Biederman goto dput_and_out; 1735b2f5d4dcSEric W. Biederman retval = -EPERM; 1736b2f5d4dcSEric W. Biederman if (flags & MNT_FORCE && !capable(CAP_SYS_ADMIN)) 1737b2f5d4dcSEric W. Biederman goto dput_and_out; 17381da177e4SLinus Torvalds 1739900148dcSAl Viro retval = do_umount(mnt, flags); 17401da177e4SLinus Torvalds dput_and_out: 1741429731b1SJan Blunck /* we mustn't call path_put() as that would clear mnt_expiry_mark */ 17422d8f3038SAl Viro dput(path.dentry); 1743900148dcSAl Viro mntput_no_expire(mnt); 17441da177e4SLinus Torvalds out: 17451da177e4SLinus Torvalds return retval; 17461da177e4SLinus Torvalds } 17471da177e4SLinus Torvalds 17483a18ef5cSDominik Brodowski SYSCALL_DEFINE2(umount, char __user *, name, int, flags) 17493a18ef5cSDominik Brodowski { 17503a18ef5cSDominik Brodowski return ksys_umount(name, flags); 17513a18ef5cSDominik Brodowski } 17523a18ef5cSDominik Brodowski 17531da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_OLDUMOUNT 17541da177e4SLinus Torvalds 17551da177e4SLinus Torvalds /* 17561da177e4SLinus Torvalds * The 2.0 compatible umount. No flags. 17571da177e4SLinus Torvalds */ 1758bdc480e3SHeiko Carstens SYSCALL_DEFINE1(oldumount, char __user *, name) 17591da177e4SLinus Torvalds { 17603a18ef5cSDominik Brodowski return ksys_umount(name, 0); 17611da177e4SLinus Torvalds } 17621da177e4SLinus Torvalds 17631da177e4SLinus Torvalds #endif 17641da177e4SLinus Torvalds 17654ce5d2b1SEric W. Biederman static bool is_mnt_ns_file(struct dentry *dentry) 17668823c079SEric W. Biederman { 17674ce5d2b1SEric W. Biederman /* Is this a proxy for a mount namespace? */ 1768e149ed2bSAl Viro return dentry->d_op == &ns_dentry_operations && 1769e149ed2bSAl Viro dentry->d_fsdata == &mntns_operations; 17704ce5d2b1SEric W. Biederman } 17714ce5d2b1SEric W. Biederman 177258be2825SAl Viro struct mnt_namespace *to_mnt_ns(struct ns_common *ns) 177358be2825SAl Viro { 177458be2825SAl Viro return container_of(ns, struct mnt_namespace, ns); 177558be2825SAl Viro } 177658be2825SAl Viro 17774ce5d2b1SEric W. Biederman static bool mnt_ns_loop(struct dentry *dentry) 17784ce5d2b1SEric W. Biederman { 17794ce5d2b1SEric W. Biederman /* Could bind mounting the mount namespace inode cause a 17804ce5d2b1SEric W. Biederman * mount namespace loop? 17814ce5d2b1SEric W. Biederman */ 17824ce5d2b1SEric W. Biederman struct mnt_namespace *mnt_ns; 17834ce5d2b1SEric W. Biederman if (!is_mnt_ns_file(dentry)) 17844ce5d2b1SEric W. Biederman return false; 17854ce5d2b1SEric W. Biederman 1786f77c8014SAl Viro mnt_ns = to_mnt_ns(get_proc_ns(dentry->d_inode)); 17878823c079SEric W. Biederman return current->nsproxy->mnt_ns->seq >= mnt_ns->seq; 17888823c079SEric W. Biederman } 17898823c079SEric W. Biederman 179087129cc0SAl Viro struct mount *copy_tree(struct mount *mnt, struct dentry *dentry, 179136341f64SRam Pai int flag) 17921da177e4SLinus Torvalds { 179384d17192SAl Viro struct mount *res, *p, *q, *r, *parent; 17941da177e4SLinus Torvalds 17954ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt)) 17964ce5d2b1SEric W. Biederman return ERR_PTR(-EINVAL); 17974ce5d2b1SEric W. Biederman 17984ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry)) 1799be34d1a3SDavid Howells return ERR_PTR(-EINVAL); 18009676f0c6SRam Pai 180136341f64SRam Pai res = q = clone_mnt(mnt, dentry, flag); 1802be34d1a3SDavid Howells if (IS_ERR(q)) 1803be34d1a3SDavid Howells return q; 1804be34d1a3SDavid Howells 1805a73324daSAl Viro q->mnt_mountpoint = mnt->mnt_mountpoint; 18061da177e4SLinus Torvalds 18071da177e4SLinus Torvalds p = mnt; 18086b41d536SAl Viro list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) { 1809315fc83eSAl Viro struct mount *s; 18107ec02ef1SJan Blunck if (!is_subdir(r->mnt_mountpoint, dentry)) 18111da177e4SLinus Torvalds continue; 18121da177e4SLinus Torvalds 1813909b0a88SAl Viro for (s = r; s; s = next_mnt(s, r)) { 18144ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_UNBINDABLE) && 18154ce5d2b1SEric W. Biederman IS_MNT_UNBINDABLE(s)) { 18164ce5d2b1SEric W. Biederman s = skip_mnt_tree(s); 18174ce5d2b1SEric W. Biederman continue; 18184ce5d2b1SEric W. Biederman } 18194ce5d2b1SEric W. Biederman if (!(flag & CL_COPY_MNT_NS_FILE) && 18204ce5d2b1SEric W. Biederman is_mnt_ns_file(s->mnt.mnt_root)) { 18219676f0c6SRam Pai s = skip_mnt_tree(s); 18229676f0c6SRam Pai continue; 18239676f0c6SRam Pai } 18240714a533SAl Viro while (p != s->mnt_parent) { 18250714a533SAl Viro p = p->mnt_parent; 18260714a533SAl Viro q = q->mnt_parent; 18271da177e4SLinus Torvalds } 182887129cc0SAl Viro p = s; 182984d17192SAl Viro parent = q; 183087129cc0SAl Viro q = clone_mnt(p, p->mnt.mnt_root, flag); 1831be34d1a3SDavid Howells if (IS_ERR(q)) 1832be34d1a3SDavid Howells goto out; 1833719ea2fbSAl Viro lock_mount_hash(); 18341a4eeaf2SAl Viro list_add_tail(&q->mnt_list, &res->mnt_list); 18351064f874SEric W. Biederman attach_mnt(q, parent, p->mnt_mp); 1836719ea2fbSAl Viro unlock_mount_hash(); 18371da177e4SLinus Torvalds } 18381da177e4SLinus Torvalds } 18391da177e4SLinus Torvalds return res; 1840be34d1a3SDavid Howells out: 18411da177e4SLinus Torvalds if (res) { 1842719ea2fbSAl Viro lock_mount_hash(); 1843e819f152SEric W. Biederman umount_tree(res, UMOUNT_SYNC); 1844719ea2fbSAl Viro unlock_mount_hash(); 18451da177e4SLinus Torvalds } 1846be34d1a3SDavid Howells return q; 18471da177e4SLinus Torvalds } 18481da177e4SLinus Torvalds 1849be34d1a3SDavid Howells /* Caller should check returned pointer for errors */ 1850be34d1a3SDavid Howells 1851ca71cf71SAl Viro struct vfsmount *collect_mounts(const struct path *path) 18528aec0809SAl Viro { 1853cb338d06SAl Viro struct mount *tree; 185497216be0SAl Viro namespace_lock(); 1855cd4a4017SEric W. Biederman if (!check_mnt(real_mount(path->mnt))) 1856cd4a4017SEric W. Biederman tree = ERR_PTR(-EINVAL); 1857cd4a4017SEric W. Biederman else 185887129cc0SAl Viro tree = copy_tree(real_mount(path->mnt), path->dentry, 185987129cc0SAl Viro CL_COPY_ALL | CL_PRIVATE); 1860328e6d90SAl Viro namespace_unlock(); 1861be34d1a3SDavid Howells if (IS_ERR(tree)) 186252e220d3SDan Carpenter return ERR_CAST(tree); 1863be34d1a3SDavid Howells return &tree->mnt; 18648aec0809SAl Viro } 18658aec0809SAl Viro 18668aec0809SAl Viro void drop_collected_mounts(struct vfsmount *mnt) 18678aec0809SAl Viro { 186897216be0SAl Viro namespace_lock(); 1869719ea2fbSAl Viro lock_mount_hash(); 1870e819f152SEric W. Biederman umount_tree(real_mount(mnt), UMOUNT_SYNC); 1871719ea2fbSAl Viro unlock_mount_hash(); 18723ab6abeeSAl Viro namespace_unlock(); 18738aec0809SAl Viro } 18748aec0809SAl Viro 1875c771d683SMiklos Szeredi /** 1876c771d683SMiklos Szeredi * clone_private_mount - create a private clone of a path 1877c771d683SMiklos Szeredi * 1878c771d683SMiklos Szeredi * This creates a new vfsmount, which will be the clone of @path. The new will 1879c771d683SMiklos Szeredi * not be attached anywhere in the namespace and will be private (i.e. changes 1880c771d683SMiklos Szeredi * to the originating mount won't be propagated into this). 1881c771d683SMiklos Szeredi * 1882c771d683SMiklos Szeredi * Release with mntput(). 1883c771d683SMiklos Szeredi */ 1884ca71cf71SAl Viro struct vfsmount *clone_private_mount(const struct path *path) 1885c771d683SMiklos Szeredi { 1886c771d683SMiklos Szeredi struct mount *old_mnt = real_mount(path->mnt); 1887c771d683SMiklos Szeredi struct mount *new_mnt; 1888c771d683SMiklos Szeredi 1889c771d683SMiklos Szeredi if (IS_MNT_UNBINDABLE(old_mnt)) 1890c771d683SMiklos Szeredi return ERR_PTR(-EINVAL); 1891c771d683SMiklos Szeredi 1892c771d683SMiklos Szeredi new_mnt = clone_mnt(old_mnt, path->dentry, CL_PRIVATE); 1893c771d683SMiklos Szeredi if (IS_ERR(new_mnt)) 1894c771d683SMiklos Szeredi return ERR_CAST(new_mnt); 1895c771d683SMiklos Szeredi 1896c771d683SMiklos Szeredi return &new_mnt->mnt; 1897c771d683SMiklos Szeredi } 1898c771d683SMiklos Szeredi EXPORT_SYMBOL_GPL(clone_private_mount); 1899c771d683SMiklos Szeredi 19001f707137SAl Viro int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg, 19011f707137SAl Viro struct vfsmount *root) 19021f707137SAl Viro { 19031a4eeaf2SAl Viro struct mount *mnt; 19041f707137SAl Viro int res = f(root, arg); 19051f707137SAl Viro if (res) 19061f707137SAl Viro return res; 19071a4eeaf2SAl Viro list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) { 19081a4eeaf2SAl Viro res = f(&mnt->mnt, arg); 19091f707137SAl Viro if (res) 19101f707137SAl Viro return res; 19111f707137SAl Viro } 19121f707137SAl Viro return 0; 19131f707137SAl Viro } 19141f707137SAl Viro 19154b8b21f4SAl Viro static void cleanup_group_ids(struct mount *mnt, struct mount *end) 1916719f5d7fSMiklos Szeredi { 1917315fc83eSAl Viro struct mount *p; 1918719f5d7fSMiklos Szeredi 1919909b0a88SAl Viro for (p = mnt; p != end; p = next_mnt(p, mnt)) { 1920fc7be130SAl Viro if (p->mnt_group_id && !IS_MNT_SHARED(p)) 19214b8b21f4SAl Viro mnt_release_group_id(p); 1922719f5d7fSMiklos Szeredi } 1923719f5d7fSMiklos Szeredi } 1924719f5d7fSMiklos Szeredi 19254b8b21f4SAl Viro static int invent_group_ids(struct mount *mnt, bool recurse) 1926719f5d7fSMiklos Szeredi { 1927315fc83eSAl Viro struct mount *p; 1928719f5d7fSMiklos Szeredi 1929909b0a88SAl Viro for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) { 1930fc7be130SAl Viro if (!p->mnt_group_id && !IS_MNT_SHARED(p)) { 19314b8b21f4SAl Viro int err = mnt_alloc_group_id(p); 1932719f5d7fSMiklos Szeredi if (err) { 19334b8b21f4SAl Viro cleanup_group_ids(mnt, p); 1934719f5d7fSMiklos Szeredi return err; 1935719f5d7fSMiklos Szeredi } 1936719f5d7fSMiklos Szeredi } 1937719f5d7fSMiklos Szeredi } 1938719f5d7fSMiklos Szeredi 1939719f5d7fSMiklos Szeredi return 0; 1940719f5d7fSMiklos Szeredi } 1941719f5d7fSMiklos Szeredi 1942d2921684SEric W. Biederman int count_mounts(struct mnt_namespace *ns, struct mount *mnt) 1943d2921684SEric W. Biederman { 1944d2921684SEric W. Biederman unsigned int max = READ_ONCE(sysctl_mount_max); 1945d2921684SEric W. Biederman unsigned int mounts = 0, old, pending, sum; 1946d2921684SEric W. Biederman struct mount *p; 1947d2921684SEric W. Biederman 1948d2921684SEric W. Biederman for (p = mnt; p; p = next_mnt(p, mnt)) 1949d2921684SEric W. Biederman mounts++; 1950d2921684SEric W. Biederman 1951d2921684SEric W. Biederman old = ns->mounts; 1952d2921684SEric W. Biederman pending = ns->pending_mounts; 1953d2921684SEric W. Biederman sum = old + pending; 1954d2921684SEric W. Biederman if ((old > sum) || 1955d2921684SEric W. Biederman (pending > sum) || 1956d2921684SEric W. Biederman (max < sum) || 1957d2921684SEric W. Biederman (mounts > (max - sum))) 1958d2921684SEric W. Biederman return -ENOSPC; 1959d2921684SEric W. Biederman 1960d2921684SEric W. Biederman ns->pending_mounts = pending + mounts; 1961d2921684SEric W. Biederman return 0; 1962d2921684SEric W. Biederman } 1963d2921684SEric W. Biederman 1964b90fa9aeSRam Pai /* 1965b90fa9aeSRam Pai * @source_mnt : mount tree to be attached 1966b90fa9aeSRam Pai * @nd : place the mount tree @source_mnt is attached 196721444403SRam Pai * @parent_nd : if non-null, detach the source_mnt from its parent and 196821444403SRam Pai * store the parent mount and mountpoint dentry. 196921444403SRam Pai * (done when source_mnt is moved) 1970b90fa9aeSRam Pai * 1971b90fa9aeSRam Pai * NOTE: in the table below explains the semantics when a source mount 1972b90fa9aeSRam Pai * of a given type is attached to a destination mount of a given type. 19739676f0c6SRam Pai * --------------------------------------------------------------------------- 1974b90fa9aeSRam Pai * | BIND MOUNT OPERATION | 19759676f0c6SRam Pai * |************************************************************************** 19769676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 19779676f0c6SRam Pai * | dest | | | | | 19789676f0c6SRam Pai * | | | | | | | 19799676f0c6SRam Pai * | v | | | | | 19809676f0c6SRam Pai * |************************************************************************** 19819676f0c6SRam Pai * | shared | shared (++) | shared (+) | shared(+++)| invalid | 19825afe0022SRam Pai * | | | | | | 19839676f0c6SRam Pai * |non-shared| shared (+) | private | slave (*) | invalid | 19849676f0c6SRam Pai * *************************************************************************** 1985b90fa9aeSRam Pai * A bind operation clones the source mount and mounts the clone on the 1986b90fa9aeSRam Pai * destination mount. 1987b90fa9aeSRam Pai * 1988b90fa9aeSRam Pai * (++) the cloned mount is propagated to all the mounts in the propagation 1989b90fa9aeSRam Pai * tree of the destination mount and the cloned mount is added to 1990b90fa9aeSRam Pai * the peer group of the source mount. 1991b90fa9aeSRam Pai * (+) the cloned mount is created under the destination mount and is marked 1992b90fa9aeSRam Pai * as shared. The cloned mount is added to the peer group of the source 1993b90fa9aeSRam Pai * mount. 19945afe0022SRam Pai * (+++) the mount is propagated to all the mounts in the propagation tree 19955afe0022SRam Pai * of the destination mount and the cloned mount is made slave 19965afe0022SRam Pai * of the same master as that of the source mount. The cloned mount 19975afe0022SRam Pai * is marked as 'shared and slave'. 19985afe0022SRam Pai * (*) the cloned mount is made a slave of the same master as that of the 19995afe0022SRam Pai * source mount. 20005afe0022SRam Pai * 20019676f0c6SRam Pai * --------------------------------------------------------------------------- 200221444403SRam Pai * | MOVE MOUNT OPERATION | 20039676f0c6SRam Pai * |************************************************************************** 20049676f0c6SRam Pai * | source-->| shared | private | slave | unbindable | 20059676f0c6SRam Pai * | dest | | | | | 20069676f0c6SRam Pai * | | | | | | | 20079676f0c6SRam Pai * | v | | | | | 20089676f0c6SRam Pai * |************************************************************************** 20099676f0c6SRam Pai * | shared | shared (+) | shared (+) | shared(+++) | invalid | 20105afe0022SRam Pai * | | | | | | 20119676f0c6SRam Pai * |non-shared| shared (+*) | private | slave (*) | unbindable | 20129676f0c6SRam Pai * *************************************************************************** 20135afe0022SRam Pai * 20145afe0022SRam Pai * (+) the mount is moved to the destination. And is then propagated to 20155afe0022SRam Pai * all the mounts in the propagation tree of the destination mount. 201621444403SRam Pai * (+*) the mount is moved to the destination. 20175afe0022SRam Pai * (+++) the mount is moved to the destination and is then propagated to 20185afe0022SRam Pai * all the mounts belonging to the destination mount's propagation tree. 20195afe0022SRam Pai * the mount is marked as 'shared and slave'. 20205afe0022SRam Pai * (*) the mount continues to be a slave at the new location. 2021b90fa9aeSRam Pai * 2022b90fa9aeSRam Pai * if the source mount is a tree, the operations explained above is 2023b90fa9aeSRam Pai * applied to each mount in the tree. 2024b90fa9aeSRam Pai * Must be called without spinlocks held, since this function can sleep 2025b90fa9aeSRam Pai * in allocations. 2026b90fa9aeSRam Pai */ 20270fb54e50SAl Viro static int attach_recursive_mnt(struct mount *source_mnt, 202884d17192SAl Viro struct mount *dest_mnt, 202984d17192SAl Viro struct mountpoint *dest_mp, 203084d17192SAl Viro struct path *parent_path) 2031b90fa9aeSRam Pai { 203238129a13SAl Viro HLIST_HEAD(tree_list); 2033d2921684SEric W. Biederman struct mnt_namespace *ns = dest_mnt->mnt_ns; 20341064f874SEric W. Biederman struct mountpoint *smp; 2035315fc83eSAl Viro struct mount *child, *p; 203638129a13SAl Viro struct hlist_node *n; 2037719f5d7fSMiklos Szeredi int err; 2038b90fa9aeSRam Pai 20391064f874SEric W. Biederman /* Preallocate a mountpoint in case the new mounts need 20401064f874SEric W. Biederman * to be tucked under other mounts. 20411064f874SEric W. Biederman */ 20421064f874SEric W. Biederman smp = get_mountpoint(source_mnt->mnt.mnt_root); 20431064f874SEric W. Biederman if (IS_ERR(smp)) 20441064f874SEric W. Biederman return PTR_ERR(smp); 20451064f874SEric W. Biederman 2046d2921684SEric W. Biederman /* Is there space to add these mounts to the mount namespace? */ 2047d2921684SEric W. Biederman if (!parent_path) { 2048d2921684SEric W. Biederman err = count_mounts(ns, source_mnt); 2049d2921684SEric W. Biederman if (err) 2050d2921684SEric W. Biederman goto out; 2051d2921684SEric W. Biederman } 2052d2921684SEric W. Biederman 2053fc7be130SAl Viro if (IS_MNT_SHARED(dest_mnt)) { 20540fb54e50SAl Viro err = invent_group_ids(source_mnt, true); 2055719f5d7fSMiklos Szeredi if (err) 2056719f5d7fSMiklos Szeredi goto out; 205784d17192SAl Viro err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list); 2058f2ebb3a9SAl Viro lock_mount_hash(); 2059719f5d7fSMiklos Szeredi if (err) 2060719f5d7fSMiklos Szeredi goto out_cleanup_ids; 2061909b0a88SAl Viro for (p = source_mnt; p; p = next_mnt(p, source_mnt)) 20620f0afb1dSAl Viro set_mnt_shared(p); 20630b1b901bSAl Viro } else { 20640b1b901bSAl Viro lock_mount_hash(); 2065b90fa9aeSRam Pai } 20661a390689SAl Viro if (parent_path) { 20670fb54e50SAl Viro detach_mnt(source_mnt, parent_path); 206884d17192SAl Viro attach_mnt(source_mnt, dest_mnt, dest_mp); 2069143c8c91SAl Viro touch_mnt_namespace(source_mnt->mnt_ns); 207021444403SRam Pai } else { 207184d17192SAl Viro mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt); 20721064f874SEric W. Biederman commit_tree(source_mnt); 207321444403SRam Pai } 2074b90fa9aeSRam Pai 207538129a13SAl Viro hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) { 20761d6a32acSAl Viro struct mount *q; 207738129a13SAl Viro hlist_del_init(&child->mnt_hash); 20781064f874SEric W. Biederman q = __lookup_mnt(&child->mnt_parent->mnt, 20791d6a32acSAl Viro child->mnt_mountpoint); 20801064f874SEric W. Biederman if (q) 20811064f874SEric W. Biederman mnt_change_mountpoint(child, smp, q); 20821064f874SEric W. Biederman commit_tree(child); 2083b90fa9aeSRam Pai } 20841064f874SEric W. Biederman put_mountpoint(smp); 2085719ea2fbSAl Viro unlock_mount_hash(); 208699b7db7bSNick Piggin 2087b90fa9aeSRam Pai return 0; 2088719f5d7fSMiklos Szeredi 2089719f5d7fSMiklos Szeredi out_cleanup_ids: 2090f2ebb3a9SAl Viro while (!hlist_empty(&tree_list)) { 2091f2ebb3a9SAl Viro child = hlist_entry(tree_list.first, struct mount, mnt_hash); 2092d2921684SEric W. Biederman child->mnt_parent->mnt_ns->pending_mounts = 0; 2093e819f152SEric W. Biederman umount_tree(child, UMOUNT_SYNC); 2094f2ebb3a9SAl Viro } 2095f2ebb3a9SAl Viro unlock_mount_hash(); 20960fb54e50SAl Viro cleanup_group_ids(source_mnt, NULL); 2097719f5d7fSMiklos Szeredi out: 2098d2921684SEric W. Biederman ns->pending_mounts = 0; 20991064f874SEric W. Biederman 21001064f874SEric W. Biederman read_seqlock_excl(&mount_lock); 21011064f874SEric W. Biederman put_mountpoint(smp); 21021064f874SEric W. Biederman read_sequnlock_excl(&mount_lock); 21031064f874SEric W. Biederman 2104719f5d7fSMiklos Szeredi return err; 2105b90fa9aeSRam Pai } 2106b90fa9aeSRam Pai 210784d17192SAl Viro static struct mountpoint *lock_mount(struct path *path) 2108b12cea91SAl Viro { 2109b12cea91SAl Viro struct vfsmount *mnt; 211084d17192SAl Viro struct dentry *dentry = path->dentry; 2111b12cea91SAl Viro retry: 21125955102cSAl Viro inode_lock(dentry->d_inode); 211384d17192SAl Viro if (unlikely(cant_mount(dentry))) { 21145955102cSAl Viro inode_unlock(dentry->d_inode); 211584d17192SAl Viro return ERR_PTR(-ENOENT); 2116b12cea91SAl Viro } 211797216be0SAl Viro namespace_lock(); 2118b12cea91SAl Viro mnt = lookup_mnt(path); 211984d17192SAl Viro if (likely(!mnt)) { 21203895dbf8SEric W. Biederman struct mountpoint *mp = get_mountpoint(dentry); 212184d17192SAl Viro if (IS_ERR(mp)) { 212297216be0SAl Viro namespace_unlock(); 21235955102cSAl Viro inode_unlock(dentry->d_inode); 212484d17192SAl Viro return mp; 212584d17192SAl Viro } 212684d17192SAl Viro return mp; 212784d17192SAl Viro } 212897216be0SAl Viro namespace_unlock(); 21295955102cSAl Viro inode_unlock(path->dentry->d_inode); 2130b12cea91SAl Viro path_put(path); 2131b12cea91SAl Viro path->mnt = mnt; 213284d17192SAl Viro dentry = path->dentry = dget(mnt->mnt_root); 2133b12cea91SAl Viro goto retry; 2134b12cea91SAl Viro } 2135b12cea91SAl Viro 213684d17192SAl Viro static void unlock_mount(struct mountpoint *where) 2137b12cea91SAl Viro { 213884d17192SAl Viro struct dentry *dentry = where->m_dentry; 21393895dbf8SEric W. Biederman 21403895dbf8SEric W. Biederman read_seqlock_excl(&mount_lock); 214184d17192SAl Viro put_mountpoint(where); 21423895dbf8SEric W. Biederman read_sequnlock_excl(&mount_lock); 21433895dbf8SEric W. Biederman 2144328e6d90SAl Viro namespace_unlock(); 21455955102cSAl Viro inode_unlock(dentry->d_inode); 2146b12cea91SAl Viro } 2147b12cea91SAl Viro 214884d17192SAl Viro static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp) 21491da177e4SLinus Torvalds { 2150e462ec50SDavid Howells if (mnt->mnt.mnt_sb->s_flags & SB_NOUSER) 21511da177e4SLinus Torvalds return -EINVAL; 21521da177e4SLinus Torvalds 2153e36cb0b8SDavid Howells if (d_is_dir(mp->m_dentry) != 2154e36cb0b8SDavid Howells d_is_dir(mnt->mnt.mnt_root)) 21551da177e4SLinus Torvalds return -ENOTDIR; 21561da177e4SLinus Torvalds 215784d17192SAl Viro return attach_recursive_mnt(mnt, p, mp, NULL); 21581da177e4SLinus Torvalds } 21591da177e4SLinus Torvalds 21601da177e4SLinus Torvalds /* 21617a2e8a8fSValerie Aurora * Sanity check the flags to change_mnt_propagation. 21627a2e8a8fSValerie Aurora */ 21637a2e8a8fSValerie Aurora 2164e462ec50SDavid Howells static int flags_to_propagation_type(int ms_flags) 21657a2e8a8fSValerie Aurora { 2166e462ec50SDavid Howells int type = ms_flags & ~(MS_REC | MS_SILENT); 21677a2e8a8fSValerie Aurora 21687a2e8a8fSValerie Aurora /* Fail if any non-propagation flags are set */ 21697a2e8a8fSValerie Aurora if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 21707a2e8a8fSValerie Aurora return 0; 21717a2e8a8fSValerie Aurora /* Only one propagation flag should be set */ 21727a2e8a8fSValerie Aurora if (!is_power_of_2(type)) 21737a2e8a8fSValerie Aurora return 0; 21747a2e8a8fSValerie Aurora return type; 21757a2e8a8fSValerie Aurora } 21767a2e8a8fSValerie Aurora 21777a2e8a8fSValerie Aurora /* 217807b20889SRam Pai * recursively change the type of the mountpoint. 217907b20889SRam Pai */ 2180e462ec50SDavid Howells static int do_change_type(struct path *path, int ms_flags) 218107b20889SRam Pai { 2182315fc83eSAl Viro struct mount *m; 21834b8b21f4SAl Viro struct mount *mnt = real_mount(path->mnt); 2184e462ec50SDavid Howells int recurse = ms_flags & MS_REC; 21857a2e8a8fSValerie Aurora int type; 2186719f5d7fSMiklos Szeredi int err = 0; 218707b20889SRam Pai 21882d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 218907b20889SRam Pai return -EINVAL; 219007b20889SRam Pai 2191e462ec50SDavid Howells type = flags_to_propagation_type(ms_flags); 21927a2e8a8fSValerie Aurora if (!type) 21937a2e8a8fSValerie Aurora return -EINVAL; 21947a2e8a8fSValerie Aurora 219597216be0SAl Viro namespace_lock(); 2196719f5d7fSMiklos Szeredi if (type == MS_SHARED) { 2197719f5d7fSMiklos Szeredi err = invent_group_ids(mnt, recurse); 2198719f5d7fSMiklos Szeredi if (err) 2199719f5d7fSMiklos Szeredi goto out_unlock; 2200719f5d7fSMiklos Szeredi } 2201719f5d7fSMiklos Szeredi 2202719ea2fbSAl Viro lock_mount_hash(); 2203909b0a88SAl Viro for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL)) 22040f0afb1dSAl Viro change_mnt_propagation(m, type); 2205719ea2fbSAl Viro unlock_mount_hash(); 2206719f5d7fSMiklos Szeredi 2207719f5d7fSMiklos Szeredi out_unlock: 220897216be0SAl Viro namespace_unlock(); 2209719f5d7fSMiklos Szeredi return err; 221007b20889SRam Pai } 221107b20889SRam Pai 22125ff9d8a6SEric W. Biederman static bool has_locked_children(struct mount *mnt, struct dentry *dentry) 22135ff9d8a6SEric W. Biederman { 22145ff9d8a6SEric W. Biederman struct mount *child; 22155ff9d8a6SEric W. Biederman list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) { 22165ff9d8a6SEric W. Biederman if (!is_subdir(child->mnt_mountpoint, dentry)) 22175ff9d8a6SEric W. Biederman continue; 22185ff9d8a6SEric W. Biederman 22195ff9d8a6SEric W. Biederman if (child->mnt.mnt_flags & MNT_LOCKED) 22205ff9d8a6SEric W. Biederman return true; 22215ff9d8a6SEric W. Biederman } 22225ff9d8a6SEric W. Biederman return false; 22235ff9d8a6SEric W. Biederman } 22245ff9d8a6SEric W. Biederman 222507b20889SRam Pai /* 22261da177e4SLinus Torvalds * do loopback mount. 22271da177e4SLinus Torvalds */ 2228808d4e3cSAl Viro static int do_loopback(struct path *path, const char *old_name, 22292dafe1c4SEric Sandeen int recurse) 22301da177e4SLinus Torvalds { 22312d92ab3cSAl Viro struct path old_path; 223284d17192SAl Viro struct mount *mnt = NULL, *old, *parent; 223384d17192SAl Viro struct mountpoint *mp; 223457eccb83SAl Viro int err; 22351da177e4SLinus Torvalds if (!old_name || !*old_name) 22361da177e4SLinus Torvalds return -EINVAL; 2237815d405cSTrond Myklebust err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path); 22381da177e4SLinus Torvalds if (err) 22391da177e4SLinus Torvalds return err; 22401da177e4SLinus Torvalds 22418823c079SEric W. Biederman err = -EINVAL; 22424ce5d2b1SEric W. Biederman if (mnt_ns_loop(old_path.dentry)) 22438823c079SEric W. Biederman goto out; 22448823c079SEric W. Biederman 224584d17192SAl Viro mp = lock_mount(path); 224684d17192SAl Viro err = PTR_ERR(mp); 224784d17192SAl Viro if (IS_ERR(mp)) 22489676f0c6SRam Pai goto out; 22499676f0c6SRam Pai 225087129cc0SAl Viro old = real_mount(old_path.mnt); 225184d17192SAl Viro parent = real_mount(path->mnt); 225287129cc0SAl Viro 2253b12cea91SAl Viro err = -EINVAL; 2254fc7be130SAl Viro if (IS_MNT_UNBINDABLE(old)) 2255b12cea91SAl Viro goto out2; 2256b12cea91SAl Viro 2257e149ed2bSAl Viro if (!check_mnt(parent)) 2258e149ed2bSAl Viro goto out2; 2259e149ed2bSAl Viro 2260e149ed2bSAl Viro if (!check_mnt(old) && old_path.dentry->d_op != &ns_dentry_operations) 2261b12cea91SAl Viro goto out2; 2262ccd48bc7SAl Viro 22635ff9d8a6SEric W. Biederman if (!recurse && has_locked_children(old, old_path.dentry)) 22645ff9d8a6SEric W. Biederman goto out2; 22655ff9d8a6SEric W. Biederman 22661da177e4SLinus Torvalds if (recurse) 22674ce5d2b1SEric W. Biederman mnt = copy_tree(old, old_path.dentry, CL_COPY_MNT_NS_FILE); 22681da177e4SLinus Torvalds else 226987129cc0SAl Viro mnt = clone_mnt(old, old_path.dentry, 0); 22701da177e4SLinus Torvalds 2271be34d1a3SDavid Howells if (IS_ERR(mnt)) { 2272be34d1a3SDavid Howells err = PTR_ERR(mnt); 2273e9c5d8a5SAndrey Vagin goto out2; 2274be34d1a3SDavid Howells } 2275ccd48bc7SAl Viro 22765ff9d8a6SEric W. Biederman mnt->mnt.mnt_flags &= ~MNT_LOCKED; 22775ff9d8a6SEric W. Biederman 227884d17192SAl Viro err = graft_tree(mnt, parent, mp); 22791da177e4SLinus Torvalds if (err) { 2280719ea2fbSAl Viro lock_mount_hash(); 2281e819f152SEric W. Biederman umount_tree(mnt, UMOUNT_SYNC); 2282719ea2fbSAl Viro unlock_mount_hash(); 22835b83d2c5SRam Pai } 2284b12cea91SAl Viro out2: 228584d17192SAl Viro unlock_mount(mp); 2286ccd48bc7SAl Viro out: 22872d92ab3cSAl Viro path_put(&old_path); 22881da177e4SLinus Torvalds return err; 22891da177e4SLinus Torvalds } 22901da177e4SLinus Torvalds 22912e4b7fcdSDave Hansen static int change_mount_flags(struct vfsmount *mnt, int ms_flags) 22922e4b7fcdSDave Hansen { 22932e4b7fcdSDave Hansen int error = 0; 22942e4b7fcdSDave Hansen int readonly_request = 0; 22952e4b7fcdSDave Hansen 22962e4b7fcdSDave Hansen if (ms_flags & MS_RDONLY) 22972e4b7fcdSDave Hansen readonly_request = 1; 22982e4b7fcdSDave Hansen if (readonly_request == __mnt_is_readonly(mnt)) 22992e4b7fcdSDave Hansen return 0; 23002e4b7fcdSDave Hansen 23012e4b7fcdSDave Hansen if (readonly_request) 230283adc753SAl Viro error = mnt_make_readonly(real_mount(mnt)); 23032e4b7fcdSDave Hansen else 230483adc753SAl Viro __mnt_unmake_readonly(real_mount(mnt)); 23052e4b7fcdSDave Hansen return error; 23062e4b7fcdSDave Hansen } 23072e4b7fcdSDave Hansen 23081da177e4SLinus Torvalds /* 23091da177e4SLinus Torvalds * change filesystem flags. dir should be a physical root of filesystem. 23101da177e4SLinus Torvalds * If you've mounted a non-root directory somewhere and want to do remount 23111da177e4SLinus Torvalds * on it - tough luck. 23121da177e4SLinus Torvalds */ 2313e462ec50SDavid Howells static int do_remount(struct path *path, int ms_flags, int sb_flags, 2314e462ec50SDavid Howells int mnt_flags, void *data) 23151da177e4SLinus Torvalds { 23161da177e4SLinus Torvalds int err; 23172d92ab3cSAl Viro struct super_block *sb = path->mnt->mnt_sb; 2318143c8c91SAl Viro struct mount *mnt = real_mount(path->mnt); 23191da177e4SLinus Torvalds 2320143c8c91SAl Viro if (!check_mnt(mnt)) 23211da177e4SLinus Torvalds return -EINVAL; 23221da177e4SLinus Torvalds 23232d92ab3cSAl Viro if (path->dentry != path->mnt->mnt_root) 23241da177e4SLinus Torvalds return -EINVAL; 23251da177e4SLinus Torvalds 232607b64558SEric W. Biederman /* Don't allow changing of locked mnt flags. 232707b64558SEric W. Biederman * 232807b64558SEric W. Biederman * No locks need to be held here while testing the various 232907b64558SEric W. Biederman * MNT_LOCK flags because those flags can never be cleared 233007b64558SEric W. Biederman * once they are set. 233107b64558SEric W. Biederman */ 233207b64558SEric W. Biederman if ((mnt->mnt.mnt_flags & MNT_LOCK_READONLY) && 233307b64558SEric W. Biederman !(mnt_flags & MNT_READONLY)) { 233407b64558SEric W. Biederman return -EPERM; 233507b64558SEric W. Biederman } 23369566d674SEric W. Biederman if ((mnt->mnt.mnt_flags & MNT_LOCK_NODEV) && 23379566d674SEric W. Biederman !(mnt_flags & MNT_NODEV)) { 23389566d674SEric W. Biederman return -EPERM; 23399566d674SEric W. Biederman } 23409566d674SEric W. Biederman if ((mnt->mnt.mnt_flags & MNT_LOCK_NOSUID) && 23419566d674SEric W. Biederman !(mnt_flags & MNT_NOSUID)) { 23429566d674SEric W. Biederman return -EPERM; 23439566d674SEric W. Biederman } 23449566d674SEric W. Biederman if ((mnt->mnt.mnt_flags & MNT_LOCK_NOEXEC) && 23459566d674SEric W. Biederman !(mnt_flags & MNT_NOEXEC)) { 23469566d674SEric W. Biederman return -EPERM; 23479566d674SEric W. Biederman } 23489566d674SEric W. Biederman if ((mnt->mnt.mnt_flags & MNT_LOCK_ATIME) && 23499566d674SEric W. Biederman ((mnt->mnt.mnt_flags & MNT_ATIME_MASK) != (mnt_flags & MNT_ATIME_MASK))) { 23509566d674SEric W. Biederman return -EPERM; 23519566d674SEric W. Biederman } 23529566d674SEric W. Biederman 2353ff36fe2cSEric Paris err = security_sb_remount(sb, data); 2354ff36fe2cSEric Paris if (err) 2355ff36fe2cSEric Paris return err; 2356ff36fe2cSEric Paris 23571da177e4SLinus Torvalds down_write(&sb->s_umount); 2358e462ec50SDavid Howells if (ms_flags & MS_BIND) 2359e462ec50SDavid Howells err = change_mount_flags(path->mnt, ms_flags); 2360bc6155d1SEric W. Biederman else if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN)) 236157eccb83SAl Viro err = -EPERM; 23624aa98cf7SAl Viro else 2363e462ec50SDavid Howells err = do_remount_sb(sb, sb_flags, data, 0); 23647b43a79fSAl Viro if (!err) { 2365719ea2fbSAl Viro lock_mount_hash(); 2366a6138db8SEric W. Biederman mnt_flags |= mnt->mnt.mnt_flags & ~MNT_USER_SETTABLE_MASK; 2367143c8c91SAl Viro mnt->mnt.mnt_flags = mnt_flags; 2368143c8c91SAl Viro touch_mnt_namespace(mnt->mnt_ns); 2369719ea2fbSAl Viro unlock_mount_hash(); 23700e55a7ccSDan Williams } 23716339dab8SAl Viro up_write(&sb->s_umount); 23721da177e4SLinus Torvalds return err; 23731da177e4SLinus Torvalds } 23741da177e4SLinus Torvalds 2375cbbe362cSAl Viro static inline int tree_contains_unbindable(struct mount *mnt) 23769676f0c6SRam Pai { 2377315fc83eSAl Viro struct mount *p; 2378909b0a88SAl Viro for (p = mnt; p; p = next_mnt(p, mnt)) { 2379fc7be130SAl Viro if (IS_MNT_UNBINDABLE(p)) 23809676f0c6SRam Pai return 1; 23819676f0c6SRam Pai } 23829676f0c6SRam Pai return 0; 23839676f0c6SRam Pai } 23849676f0c6SRam Pai 2385808d4e3cSAl Viro static int do_move_mount(struct path *path, const char *old_name) 23861da177e4SLinus Torvalds { 23872d92ab3cSAl Viro struct path old_path, parent_path; 2388676da58dSAl Viro struct mount *p; 23890fb54e50SAl Viro struct mount *old; 239084d17192SAl Viro struct mountpoint *mp; 239157eccb83SAl Viro int err; 23921da177e4SLinus Torvalds if (!old_name || !*old_name) 23931da177e4SLinus Torvalds return -EINVAL; 23942d92ab3cSAl Viro err = kern_path(old_name, LOOKUP_FOLLOW, &old_path); 23951da177e4SLinus Torvalds if (err) 23961da177e4SLinus Torvalds return err; 23971da177e4SLinus Torvalds 239884d17192SAl Viro mp = lock_mount(path); 239984d17192SAl Viro err = PTR_ERR(mp); 240084d17192SAl Viro if (IS_ERR(mp)) 2401cc53ce53SDavid Howells goto out; 2402cc53ce53SDavid Howells 2403143c8c91SAl Viro old = real_mount(old_path.mnt); 2404fc7be130SAl Viro p = real_mount(path->mnt); 2405143c8c91SAl Viro 24061da177e4SLinus Torvalds err = -EINVAL; 2407fc7be130SAl Viro if (!check_mnt(p) || !check_mnt(old)) 24081da177e4SLinus Torvalds goto out1; 24091da177e4SLinus Torvalds 24105ff9d8a6SEric W. Biederman if (old->mnt.mnt_flags & MNT_LOCKED) 24115ff9d8a6SEric W. Biederman goto out1; 24125ff9d8a6SEric W. Biederman 24131da177e4SLinus Torvalds err = -EINVAL; 24142d92ab3cSAl Viro if (old_path.dentry != old_path.mnt->mnt_root) 241521444403SRam Pai goto out1; 24161da177e4SLinus Torvalds 2417676da58dSAl Viro if (!mnt_has_parent(old)) 241821444403SRam Pai goto out1; 24191da177e4SLinus Torvalds 2420e36cb0b8SDavid Howells if (d_is_dir(path->dentry) != 2421e36cb0b8SDavid Howells d_is_dir(old_path.dentry)) 242221444403SRam Pai goto out1; 242321444403SRam Pai /* 242421444403SRam Pai * Don't move a mount residing in a shared parent. 242521444403SRam Pai */ 2426fc7be130SAl Viro if (IS_MNT_SHARED(old->mnt_parent)) 242721444403SRam Pai goto out1; 24289676f0c6SRam Pai /* 24299676f0c6SRam Pai * Don't move a mount tree containing unbindable mounts to a destination 24309676f0c6SRam Pai * mount which is shared. 24319676f0c6SRam Pai */ 2432fc7be130SAl Viro if (IS_MNT_SHARED(p) && tree_contains_unbindable(old)) 24339676f0c6SRam Pai goto out1; 24341da177e4SLinus Torvalds err = -ELOOP; 2435fc7be130SAl Viro for (; mnt_has_parent(p); p = p->mnt_parent) 2436676da58dSAl Viro if (p == old) 243721444403SRam Pai goto out1; 24381da177e4SLinus Torvalds 243984d17192SAl Viro err = attach_recursive_mnt(old, real_mount(path->mnt), mp, &parent_path); 24404ac91378SJan Blunck if (err) 244121444403SRam Pai goto out1; 24421da177e4SLinus Torvalds 24431da177e4SLinus Torvalds /* if the mount is moved, it should no longer be expire 24441da177e4SLinus Torvalds * automatically */ 24456776db3dSAl Viro list_del_init(&old->mnt_expire); 24461da177e4SLinus Torvalds out1: 244784d17192SAl Viro unlock_mount(mp); 24481da177e4SLinus Torvalds out: 24491da177e4SLinus Torvalds if (!err) 24501a390689SAl Viro path_put(&parent_path); 24512d92ab3cSAl Viro path_put(&old_path); 24521da177e4SLinus Torvalds return err; 24531da177e4SLinus Torvalds } 24541da177e4SLinus Torvalds 24559d412a43SAl Viro static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype) 24569d412a43SAl Viro { 24579d412a43SAl Viro int err; 24589d412a43SAl Viro const char *subtype = strchr(fstype, '.'); 24599d412a43SAl Viro if (subtype) { 24609d412a43SAl Viro subtype++; 24619d412a43SAl Viro err = -EINVAL; 24629d412a43SAl Viro if (!subtype[0]) 24639d412a43SAl Viro goto err; 24649d412a43SAl Viro } else 24659d412a43SAl Viro subtype = ""; 24669d412a43SAl Viro 24679d412a43SAl Viro mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL); 24689d412a43SAl Viro err = -ENOMEM; 24699d412a43SAl Viro if (!mnt->mnt_sb->s_subtype) 24709d412a43SAl Viro goto err; 24719d412a43SAl Viro return mnt; 24729d412a43SAl Viro 24739d412a43SAl Viro err: 24749d412a43SAl Viro mntput(mnt); 24759d412a43SAl Viro return ERR_PTR(err); 24769d412a43SAl Viro } 24779d412a43SAl Viro 24789d412a43SAl Viro /* 24799d412a43SAl Viro * add a mount into a namespace's mount tree 24809d412a43SAl Viro */ 248195bc5f25SAl Viro static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags) 24829d412a43SAl Viro { 248384d17192SAl Viro struct mountpoint *mp; 248484d17192SAl Viro struct mount *parent; 24859d412a43SAl Viro int err; 24869d412a43SAl Viro 2487f2ebb3a9SAl Viro mnt_flags &= ~MNT_INTERNAL_FLAGS; 24889d412a43SAl Viro 248984d17192SAl Viro mp = lock_mount(path); 249084d17192SAl Viro if (IS_ERR(mp)) 249184d17192SAl Viro return PTR_ERR(mp); 24929d412a43SAl Viro 249384d17192SAl Viro parent = real_mount(path->mnt); 24949d412a43SAl Viro err = -EINVAL; 249584d17192SAl Viro if (unlikely(!check_mnt(parent))) { 2496156cacb1SAl Viro /* that's acceptable only for automounts done in private ns */ 2497156cacb1SAl Viro if (!(mnt_flags & MNT_SHRINKABLE)) 24989d412a43SAl Viro goto unlock; 2499156cacb1SAl Viro /* ... and for those we'd better have mountpoint still alive */ 250084d17192SAl Viro if (!parent->mnt_ns) 2501156cacb1SAl Viro goto unlock; 2502156cacb1SAl Viro } 25039d412a43SAl Viro 25049d412a43SAl Viro /* Refuse the same filesystem on the same mount point */ 25059d412a43SAl Viro err = -EBUSY; 250695bc5f25SAl Viro if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb && 25079d412a43SAl Viro path->mnt->mnt_root == path->dentry) 25089d412a43SAl Viro goto unlock; 25099d412a43SAl Viro 25109d412a43SAl Viro err = -EINVAL; 2511e36cb0b8SDavid Howells if (d_is_symlink(newmnt->mnt.mnt_root)) 25129d412a43SAl Viro goto unlock; 25139d412a43SAl Viro 251495bc5f25SAl Viro newmnt->mnt.mnt_flags = mnt_flags; 251584d17192SAl Viro err = graft_tree(newmnt, parent, mp); 25169d412a43SAl Viro 25179d412a43SAl Viro unlock: 251884d17192SAl Viro unlock_mount(mp); 25199d412a43SAl Viro return err; 25209d412a43SAl Viro } 2521b1e75df4SAl Viro 25228654df4eSEric W. Biederman static bool mount_too_revealing(struct vfsmount *mnt, int *new_mnt_flags); 25231b852bceSEric W. Biederman 25241da177e4SLinus Torvalds /* 25251da177e4SLinus Torvalds * create a new mount for userspace and request it to be added into the 25261da177e4SLinus Torvalds * namespace's tree 25271da177e4SLinus Torvalds */ 2528e462ec50SDavid Howells static int do_new_mount(struct path *path, const char *fstype, int sb_flags, 2529808d4e3cSAl Viro int mnt_flags, const char *name, void *data) 25301da177e4SLinus Torvalds { 25310c55cfc4SEric W. Biederman struct file_system_type *type; 25321da177e4SLinus Torvalds struct vfsmount *mnt; 253315f9a3f3SAl Viro int err; 25341da177e4SLinus Torvalds 25350c55cfc4SEric W. Biederman if (!fstype) 25361da177e4SLinus Torvalds return -EINVAL; 25371da177e4SLinus Torvalds 25380c55cfc4SEric W. Biederman type = get_fs_type(fstype); 25390c55cfc4SEric W. Biederman if (!type) 25400c55cfc4SEric W. Biederman return -ENODEV; 25410c55cfc4SEric W. Biederman 2542e462ec50SDavid Howells mnt = vfs_kern_mount(type, sb_flags, name, data); 25430c55cfc4SEric W. Biederman if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) && 25440c55cfc4SEric W. Biederman !mnt->mnt_sb->s_subtype) 25450c55cfc4SEric W. Biederman mnt = fs_set_subtype(mnt, fstype); 25460c55cfc4SEric W. Biederman 25470c55cfc4SEric W. Biederman put_filesystem(type); 25481da177e4SLinus Torvalds if (IS_ERR(mnt)) 25491da177e4SLinus Torvalds return PTR_ERR(mnt); 25501da177e4SLinus Torvalds 25518654df4eSEric W. Biederman if (mount_too_revealing(mnt, &mnt_flags)) { 25528654df4eSEric W. Biederman mntput(mnt); 25538654df4eSEric W. Biederman return -EPERM; 25548654df4eSEric W. Biederman } 25558654df4eSEric W. Biederman 255695bc5f25SAl Viro err = do_add_mount(real_mount(mnt), path, mnt_flags); 255715f9a3f3SAl Viro if (err) 255815f9a3f3SAl Viro mntput(mnt); 255915f9a3f3SAl Viro return err; 25601da177e4SLinus Torvalds } 25611da177e4SLinus Torvalds 256219a167afSAl Viro int finish_automount(struct vfsmount *m, struct path *path) 256319a167afSAl Viro { 25646776db3dSAl Viro struct mount *mnt = real_mount(m); 256519a167afSAl Viro int err; 256619a167afSAl Viro /* The new mount record should have at least 2 refs to prevent it being 256719a167afSAl Viro * expired before we get a chance to add it 256819a167afSAl Viro */ 25696776db3dSAl Viro BUG_ON(mnt_get_count(mnt) < 2); 257019a167afSAl Viro 257119a167afSAl Viro if (m->mnt_sb == path->mnt->mnt_sb && 257219a167afSAl Viro m->mnt_root == path->dentry) { 2573b1e75df4SAl Viro err = -ELOOP; 2574b1e75df4SAl Viro goto fail; 257519a167afSAl Viro } 257619a167afSAl Viro 257795bc5f25SAl Viro err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE); 2578b1e75df4SAl Viro if (!err) 2579b1e75df4SAl Viro return 0; 2580b1e75df4SAl Viro fail: 2581b1e75df4SAl Viro /* remove m from any expiration list it may be on */ 25826776db3dSAl Viro if (!list_empty(&mnt->mnt_expire)) { 258397216be0SAl Viro namespace_lock(); 25846776db3dSAl Viro list_del_init(&mnt->mnt_expire); 258597216be0SAl Viro namespace_unlock(); 258619a167afSAl Viro } 2587b1e75df4SAl Viro mntput(m); 2588b1e75df4SAl Viro mntput(m); 258919a167afSAl Viro return err; 259019a167afSAl Viro } 259119a167afSAl Viro 2592ea5b778aSDavid Howells /** 2593ea5b778aSDavid Howells * mnt_set_expiry - Put a mount on an expiration list 2594ea5b778aSDavid Howells * @mnt: The mount to list. 2595ea5b778aSDavid Howells * @expiry_list: The list to add the mount to. 2596ea5b778aSDavid Howells */ 2597ea5b778aSDavid Howells void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list) 2598ea5b778aSDavid Howells { 259997216be0SAl Viro namespace_lock(); 2600ea5b778aSDavid Howells 26016776db3dSAl Viro list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list); 2602ea5b778aSDavid Howells 260397216be0SAl Viro namespace_unlock(); 2604ea5b778aSDavid Howells } 2605ea5b778aSDavid Howells EXPORT_SYMBOL(mnt_set_expiry); 2606ea5b778aSDavid Howells 2607ea5b778aSDavid Howells /* 26081da177e4SLinus Torvalds * process a list of expirable mountpoints with the intent of discarding any 26091da177e4SLinus Torvalds * mountpoints that aren't in use and haven't been touched since last we came 26101da177e4SLinus Torvalds * here 26111da177e4SLinus Torvalds */ 26121da177e4SLinus Torvalds void mark_mounts_for_expiry(struct list_head *mounts) 26131da177e4SLinus Torvalds { 2614761d5c38SAl Viro struct mount *mnt, *next; 26151da177e4SLinus Torvalds LIST_HEAD(graveyard); 26161da177e4SLinus Torvalds 26171da177e4SLinus Torvalds if (list_empty(mounts)) 26181da177e4SLinus Torvalds return; 26191da177e4SLinus Torvalds 262097216be0SAl Viro namespace_lock(); 2621719ea2fbSAl Viro lock_mount_hash(); 26221da177e4SLinus Torvalds 26231da177e4SLinus Torvalds /* extract from the expiration list every vfsmount that matches the 26241da177e4SLinus Torvalds * following criteria: 26251da177e4SLinus Torvalds * - only referenced by its parent vfsmount 26261da177e4SLinus Torvalds * - still marked for expiry (marked on the last call here; marks are 26271da177e4SLinus Torvalds * cleared by mntput()) 26281da177e4SLinus Torvalds */ 26296776db3dSAl Viro list_for_each_entry_safe(mnt, next, mounts, mnt_expire) { 2630863d684fSAl Viro if (!xchg(&mnt->mnt_expiry_mark, 1) || 26311ab59738SAl Viro propagate_mount_busy(mnt, 1)) 26321da177e4SLinus Torvalds continue; 26336776db3dSAl Viro list_move(&mnt->mnt_expire, &graveyard); 26341da177e4SLinus Torvalds } 2635bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 26366776db3dSAl Viro mnt = list_first_entry(&graveyard, struct mount, mnt_expire); 2637143c8c91SAl Viro touch_mnt_namespace(mnt->mnt_ns); 2638e819f152SEric W. Biederman umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC); 2639bcc5c7d2SAl Viro } 2640719ea2fbSAl Viro unlock_mount_hash(); 26413ab6abeeSAl Viro namespace_unlock(); 26421da177e4SLinus Torvalds } 26431da177e4SLinus Torvalds 26441da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(mark_mounts_for_expiry); 26451da177e4SLinus Torvalds 26461da177e4SLinus Torvalds /* 26475528f911STrond Myklebust * Ripoff of 'select_parent()' 26485528f911STrond Myklebust * 26495528f911STrond Myklebust * search the list of submounts for a given mountpoint, and move any 26505528f911STrond Myklebust * shrinkable submounts to the 'graveyard' list. 26515528f911STrond Myklebust */ 2652692afc31SAl Viro static int select_submounts(struct mount *parent, struct list_head *graveyard) 26535528f911STrond Myklebust { 2654692afc31SAl Viro struct mount *this_parent = parent; 26555528f911STrond Myklebust struct list_head *next; 26565528f911STrond Myklebust int found = 0; 26575528f911STrond Myklebust 26585528f911STrond Myklebust repeat: 26596b41d536SAl Viro next = this_parent->mnt_mounts.next; 26605528f911STrond Myklebust resume: 26616b41d536SAl Viro while (next != &this_parent->mnt_mounts) { 26625528f911STrond Myklebust struct list_head *tmp = next; 26636b41d536SAl Viro struct mount *mnt = list_entry(tmp, struct mount, mnt_child); 26645528f911STrond Myklebust 26655528f911STrond Myklebust next = tmp->next; 2666692afc31SAl Viro if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE)) 26675528f911STrond Myklebust continue; 26685528f911STrond Myklebust /* 26695528f911STrond Myklebust * Descend a level if the d_mounts list is non-empty. 26705528f911STrond Myklebust */ 26716b41d536SAl Viro if (!list_empty(&mnt->mnt_mounts)) { 26725528f911STrond Myklebust this_parent = mnt; 26735528f911STrond Myklebust goto repeat; 26745528f911STrond Myklebust } 26755528f911STrond Myklebust 26761ab59738SAl Viro if (!propagate_mount_busy(mnt, 1)) { 26776776db3dSAl Viro list_move_tail(&mnt->mnt_expire, graveyard); 26785528f911STrond Myklebust found++; 26795528f911STrond Myklebust } 26805528f911STrond Myklebust } 26815528f911STrond Myklebust /* 26825528f911STrond Myklebust * All done at this level ... ascend and resume the search 26835528f911STrond Myklebust */ 26845528f911STrond Myklebust if (this_parent != parent) { 26856b41d536SAl Viro next = this_parent->mnt_child.next; 26860714a533SAl Viro this_parent = this_parent->mnt_parent; 26875528f911STrond Myklebust goto resume; 26885528f911STrond Myklebust } 26895528f911STrond Myklebust return found; 26905528f911STrond Myklebust } 26915528f911STrond Myklebust 26925528f911STrond Myklebust /* 26935528f911STrond Myklebust * process a list of expirable mountpoints with the intent of discarding any 26945528f911STrond Myklebust * submounts of a specific parent mountpoint 269599b7db7bSNick Piggin * 269648a066e7SAl Viro * mount_lock must be held for write 26975528f911STrond Myklebust */ 2698b54b9be7SAl Viro static void shrink_submounts(struct mount *mnt) 26995528f911STrond Myklebust { 27005528f911STrond Myklebust LIST_HEAD(graveyard); 2701761d5c38SAl Viro struct mount *m; 27025528f911STrond Myklebust 27035528f911STrond Myklebust /* extract submounts of 'mountpoint' from the expiration list */ 2704c35038beSAl Viro while (select_submounts(mnt, &graveyard)) { 2705bcc5c7d2SAl Viro while (!list_empty(&graveyard)) { 2706761d5c38SAl Viro m = list_first_entry(&graveyard, struct mount, 27076776db3dSAl Viro mnt_expire); 2708143c8c91SAl Viro touch_mnt_namespace(m->mnt_ns); 2709e819f152SEric W. Biederman umount_tree(m, UMOUNT_PROPAGATE|UMOUNT_SYNC); 2710bcc5c7d2SAl Viro } 2711bcc5c7d2SAl Viro } 27125528f911STrond Myklebust } 27135528f911STrond Myklebust 27145528f911STrond Myklebust /* 27151da177e4SLinus Torvalds * Some copy_from_user() implementations do not return the exact number of 27161da177e4SLinus Torvalds * bytes remaining to copy on a fault. But copy_mount_options() requires that. 27171da177e4SLinus Torvalds * Note that this function differs from copy_from_user() in that it will oops 27181da177e4SLinus Torvalds * on bad values of `to', rather than returning a short copy. 27191da177e4SLinus Torvalds */ 2720b58fed8bSRam Pai static long exact_copy_from_user(void *to, const void __user * from, 2721b58fed8bSRam Pai unsigned long n) 27221da177e4SLinus Torvalds { 27231da177e4SLinus Torvalds char *t = to; 27241da177e4SLinus Torvalds const char __user *f = from; 27251da177e4SLinus Torvalds char c; 27261da177e4SLinus Torvalds 27271da177e4SLinus Torvalds if (!access_ok(VERIFY_READ, from, n)) 27281da177e4SLinus Torvalds return n; 27291da177e4SLinus Torvalds 27301da177e4SLinus Torvalds while (n) { 27311da177e4SLinus Torvalds if (__get_user(c, f)) { 27321da177e4SLinus Torvalds memset(t, 0, n); 27331da177e4SLinus Torvalds break; 27341da177e4SLinus Torvalds } 27351da177e4SLinus Torvalds *t++ = c; 27361da177e4SLinus Torvalds f++; 27371da177e4SLinus Torvalds n--; 27381da177e4SLinus Torvalds } 27391da177e4SLinus Torvalds return n; 27401da177e4SLinus Torvalds } 27411da177e4SLinus Torvalds 2742b40ef869SAl Viro void *copy_mount_options(const void __user * data) 27431da177e4SLinus Torvalds { 27441da177e4SLinus Torvalds int i; 27451da177e4SLinus Torvalds unsigned long size; 2746b40ef869SAl Viro char *copy; 27471da177e4SLinus Torvalds 27481da177e4SLinus Torvalds if (!data) 2749b40ef869SAl Viro return NULL; 27501da177e4SLinus Torvalds 2751b40ef869SAl Viro copy = kmalloc(PAGE_SIZE, GFP_KERNEL); 2752b40ef869SAl Viro if (!copy) 2753b40ef869SAl Viro return ERR_PTR(-ENOMEM); 27541da177e4SLinus Torvalds 27551da177e4SLinus Torvalds /* We only care that *some* data at the address the user 27561da177e4SLinus Torvalds * gave us is valid. Just in case, we'll zero 27571da177e4SLinus Torvalds * the remainder of the page. 27581da177e4SLinus Torvalds */ 27591da177e4SLinus Torvalds /* copy_from_user cannot cross TASK_SIZE ! */ 27601da177e4SLinus Torvalds size = TASK_SIZE - (unsigned long)data; 27611da177e4SLinus Torvalds if (size > PAGE_SIZE) 27621da177e4SLinus Torvalds size = PAGE_SIZE; 27631da177e4SLinus Torvalds 2764b40ef869SAl Viro i = size - exact_copy_from_user(copy, data, size); 27651da177e4SLinus Torvalds if (!i) { 2766b40ef869SAl Viro kfree(copy); 2767b40ef869SAl Viro return ERR_PTR(-EFAULT); 27681da177e4SLinus Torvalds } 27691da177e4SLinus Torvalds if (i != PAGE_SIZE) 2770b40ef869SAl Viro memset(copy + i, 0, PAGE_SIZE - i); 2771b40ef869SAl Viro return copy; 27721da177e4SLinus Torvalds } 27731da177e4SLinus Torvalds 2774b8850d1fSTim Gardner char *copy_mount_string(const void __user *data) 2775eca6f534SVegard Nossum { 2776b8850d1fSTim Gardner return data ? strndup_user(data, PAGE_SIZE) : NULL; 2777eca6f534SVegard Nossum } 2778eca6f534SVegard Nossum 27791da177e4SLinus Torvalds /* 27801da177e4SLinus Torvalds * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to 27811da177e4SLinus Torvalds * be given to the mount() call (ie: read-only, no-dev, no-suid etc). 27821da177e4SLinus Torvalds * 27831da177e4SLinus Torvalds * data is a (void *) that can point to any structure up to 27841da177e4SLinus Torvalds * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent 27851da177e4SLinus Torvalds * information (or be NULL). 27861da177e4SLinus Torvalds * 27871da177e4SLinus Torvalds * Pre-0.97 versions of mount() didn't have a flags word. 27881da177e4SLinus Torvalds * When the flags word was introduced its top half was required 27891da177e4SLinus Torvalds * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9. 27901da177e4SLinus Torvalds * Therefore, if this magic number is present, it carries no information 27911da177e4SLinus Torvalds * and must be discarded. 27921da177e4SLinus Torvalds */ 27935e6123f3SSeunghun Lee long do_mount(const char *dev_name, const char __user *dir_name, 2794808d4e3cSAl Viro const char *type_page, unsigned long flags, void *data_page) 27951da177e4SLinus Torvalds { 27962d92ab3cSAl Viro struct path path; 2797e462ec50SDavid Howells unsigned int mnt_flags = 0, sb_flags; 27981da177e4SLinus Torvalds int retval = 0; 27991da177e4SLinus Torvalds 28001da177e4SLinus Torvalds /* Discard magic */ 28011da177e4SLinus Torvalds if ((flags & MS_MGC_MSK) == MS_MGC_VAL) 28021da177e4SLinus Torvalds flags &= ~MS_MGC_MSK; 28031da177e4SLinus Torvalds 28041da177e4SLinus Torvalds /* Basic sanity checks */ 28051da177e4SLinus Torvalds if (data_page) 28061da177e4SLinus Torvalds ((char *)data_page)[PAGE_SIZE - 1] = 0; 28071da177e4SLinus Torvalds 2808e462ec50SDavid Howells if (flags & MS_NOUSER) 2809e462ec50SDavid Howells return -EINVAL; 2810e462ec50SDavid Howells 2811a27ab9f2STetsuo Handa /* ... and get the mountpoint */ 28125e6123f3SSeunghun Lee retval = user_path(dir_name, &path); 2813a27ab9f2STetsuo Handa if (retval) 2814a27ab9f2STetsuo Handa return retval; 2815a27ab9f2STetsuo Handa 2816a27ab9f2STetsuo Handa retval = security_sb_mount(dev_name, &path, 2817a27ab9f2STetsuo Handa type_page, flags, data_page); 28180d5cadb8SAl Viro if (!retval && !may_mount()) 28190d5cadb8SAl Viro retval = -EPERM; 2820e462ec50SDavid Howells if (!retval && (flags & SB_MANDLOCK) && !may_mandlock()) 28219e8925b6SJeff Layton retval = -EPERM; 2822a27ab9f2STetsuo Handa if (retval) 2823a27ab9f2STetsuo Handa goto dput_out; 2824a27ab9f2STetsuo Handa 2825613cbe3dSAndi Kleen /* Default to relatime unless overriden */ 2826613cbe3dSAndi Kleen if (!(flags & MS_NOATIME)) 28270a1c01c9SMatthew Garrett mnt_flags |= MNT_RELATIME; 28280a1c01c9SMatthew Garrett 28291da177e4SLinus Torvalds /* Separate the per-mountpoint flags */ 28301da177e4SLinus Torvalds if (flags & MS_NOSUID) 28311da177e4SLinus Torvalds mnt_flags |= MNT_NOSUID; 28321da177e4SLinus Torvalds if (flags & MS_NODEV) 28331da177e4SLinus Torvalds mnt_flags |= MNT_NODEV; 28341da177e4SLinus Torvalds if (flags & MS_NOEXEC) 28351da177e4SLinus Torvalds mnt_flags |= MNT_NOEXEC; 2836fc33a7bbSChristoph Hellwig if (flags & MS_NOATIME) 2837fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NOATIME; 2838fc33a7bbSChristoph Hellwig if (flags & MS_NODIRATIME) 2839fc33a7bbSChristoph Hellwig mnt_flags |= MNT_NODIRATIME; 2840d0adde57SMatthew Garrett if (flags & MS_STRICTATIME) 2841d0adde57SMatthew Garrett mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME); 2842a9e5b732SDavid Howells if (flags & MS_RDONLY) 28432e4b7fcdSDave Hansen mnt_flags |= MNT_READONLY; 2844fc33a7bbSChristoph Hellwig 2845ffbc6f0eSEric W. Biederman /* The default atime for remount is preservation */ 2846ffbc6f0eSEric W. Biederman if ((flags & MS_REMOUNT) && 2847ffbc6f0eSEric W. Biederman ((flags & (MS_NOATIME | MS_NODIRATIME | MS_RELATIME | 2848ffbc6f0eSEric W. Biederman MS_STRICTATIME)) == 0)) { 2849ffbc6f0eSEric W. Biederman mnt_flags &= ~MNT_ATIME_MASK; 2850ffbc6f0eSEric W. Biederman mnt_flags |= path.mnt->mnt_flags & MNT_ATIME_MASK; 2851ffbc6f0eSEric W. Biederman } 2852ffbc6f0eSEric W. Biederman 2853e462ec50SDavid Howells sb_flags = flags & (SB_RDONLY | 2854e462ec50SDavid Howells SB_SYNCHRONOUS | 2855e462ec50SDavid Howells SB_MANDLOCK | 2856e462ec50SDavid Howells SB_DIRSYNC | 2857e462ec50SDavid Howells SB_SILENT | 2858917086ffSMimi Zohar SB_POSIXACL | 2859d7ee9469SMarkus Trippelsdorf SB_LAZYTIME | 2860917086ffSMimi Zohar SB_I_VERSION); 28611da177e4SLinus Torvalds 28621da177e4SLinus Torvalds if (flags & MS_REMOUNT) 2863e462ec50SDavid Howells retval = do_remount(&path, flags, sb_flags, mnt_flags, 28641da177e4SLinus Torvalds data_page); 28651da177e4SLinus Torvalds else if (flags & MS_BIND) 28662d92ab3cSAl Viro retval = do_loopback(&path, dev_name, flags & MS_REC); 28679676f0c6SRam Pai else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE)) 28682d92ab3cSAl Viro retval = do_change_type(&path, flags); 28691da177e4SLinus Torvalds else if (flags & MS_MOVE) 28702d92ab3cSAl Viro retval = do_move_mount(&path, dev_name); 28711da177e4SLinus Torvalds else 2872e462ec50SDavid Howells retval = do_new_mount(&path, type_page, sb_flags, mnt_flags, 28731da177e4SLinus Torvalds dev_name, data_page); 28741da177e4SLinus Torvalds dput_out: 28752d92ab3cSAl Viro path_put(&path); 28761da177e4SLinus Torvalds return retval; 28771da177e4SLinus Torvalds } 28781da177e4SLinus Torvalds 2879537f7ccbSEric W. Biederman static struct ucounts *inc_mnt_namespaces(struct user_namespace *ns) 2880537f7ccbSEric W. Biederman { 2881537f7ccbSEric W. Biederman return inc_ucount(ns, current_euid(), UCOUNT_MNT_NAMESPACES); 2882537f7ccbSEric W. Biederman } 2883537f7ccbSEric W. Biederman 2884537f7ccbSEric W. Biederman static void dec_mnt_namespaces(struct ucounts *ucounts) 2885537f7ccbSEric W. Biederman { 2886537f7ccbSEric W. Biederman dec_ucount(ucounts, UCOUNT_MNT_NAMESPACES); 2887537f7ccbSEric W. Biederman } 2888537f7ccbSEric W. Biederman 2889771b1371SEric W. Biederman static void free_mnt_ns(struct mnt_namespace *ns) 2890771b1371SEric W. Biederman { 28916344c433SAl Viro ns_free_inum(&ns->ns); 2892537f7ccbSEric W. Biederman dec_mnt_namespaces(ns->ucounts); 2893771b1371SEric W. Biederman put_user_ns(ns->user_ns); 2894771b1371SEric W. Biederman kfree(ns); 2895771b1371SEric W. Biederman } 2896771b1371SEric W. Biederman 28978823c079SEric W. Biederman /* 28988823c079SEric W. Biederman * Assign a sequence number so we can detect when we attempt to bind 28998823c079SEric W. Biederman * mount a reference to an older mount namespace into the current 29008823c079SEric W. Biederman * mount namespace, preventing reference counting loops. A 64bit 29018823c079SEric W. Biederman * number incrementing at 10Ghz will take 12,427 years to wrap which 29028823c079SEric W. Biederman * is effectively never, so we can ignore the possibility. 29038823c079SEric W. Biederman */ 29048823c079SEric W. Biederman static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1); 29058823c079SEric W. Biederman 2906771b1371SEric W. Biederman static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns) 2907cf8d2c11STrond Myklebust { 2908cf8d2c11STrond Myklebust struct mnt_namespace *new_ns; 2909537f7ccbSEric W. Biederman struct ucounts *ucounts; 291098f842e6SEric W. Biederman int ret; 2911cf8d2c11STrond Myklebust 2912537f7ccbSEric W. Biederman ucounts = inc_mnt_namespaces(user_ns); 2913537f7ccbSEric W. Biederman if (!ucounts) 2914df75e774SEric W. Biederman return ERR_PTR(-ENOSPC); 2915537f7ccbSEric W. Biederman 2916cf8d2c11STrond Myklebust new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL); 2917537f7ccbSEric W. Biederman if (!new_ns) { 2918537f7ccbSEric W. Biederman dec_mnt_namespaces(ucounts); 2919cf8d2c11STrond Myklebust return ERR_PTR(-ENOMEM); 2920537f7ccbSEric W. Biederman } 29216344c433SAl Viro ret = ns_alloc_inum(&new_ns->ns); 292298f842e6SEric W. Biederman if (ret) { 292398f842e6SEric W. Biederman kfree(new_ns); 2924537f7ccbSEric W. Biederman dec_mnt_namespaces(ucounts); 292598f842e6SEric W. Biederman return ERR_PTR(ret); 292698f842e6SEric W. Biederman } 292733c42940SAl Viro new_ns->ns.ops = &mntns_operations; 29288823c079SEric W. Biederman new_ns->seq = atomic64_add_return(1, &mnt_ns_seq); 2929cf8d2c11STrond Myklebust atomic_set(&new_ns->count, 1); 2930cf8d2c11STrond Myklebust new_ns->root = NULL; 2931cf8d2c11STrond Myklebust INIT_LIST_HEAD(&new_ns->list); 2932cf8d2c11STrond Myklebust init_waitqueue_head(&new_ns->poll); 2933cf8d2c11STrond Myklebust new_ns->event = 0; 2934771b1371SEric W. Biederman new_ns->user_ns = get_user_ns(user_ns); 2935537f7ccbSEric W. Biederman new_ns->ucounts = ucounts; 2936d2921684SEric W. Biederman new_ns->mounts = 0; 2937d2921684SEric W. Biederman new_ns->pending_mounts = 0; 2938cf8d2c11STrond Myklebust return new_ns; 2939cf8d2c11STrond Myklebust } 2940cf8d2c11STrond Myklebust 29410766f788SEmese Revfy __latent_entropy 29429559f689SAl Viro struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns, 29439559f689SAl Viro struct user_namespace *user_ns, struct fs_struct *new_fs) 29441da177e4SLinus Torvalds { 29456b3286edSKirill Korotaev struct mnt_namespace *new_ns; 29467f2da1e7SAl Viro struct vfsmount *rootmnt = NULL, *pwdmnt = NULL; 2947315fc83eSAl Viro struct mount *p, *q; 29489559f689SAl Viro struct mount *old; 2949cb338d06SAl Viro struct mount *new; 29507a472ef4SEric W. Biederman int copy_flags; 29511da177e4SLinus Torvalds 29529559f689SAl Viro BUG_ON(!ns); 29539559f689SAl Viro 29549559f689SAl Viro if (likely(!(flags & CLONE_NEWNS))) { 29559559f689SAl Viro get_mnt_ns(ns); 29569559f689SAl Viro return ns; 29579559f689SAl Viro } 29589559f689SAl Viro 29599559f689SAl Viro old = ns->root; 29609559f689SAl Viro 2961771b1371SEric W. Biederman new_ns = alloc_mnt_ns(user_ns); 2962cf8d2c11STrond Myklebust if (IS_ERR(new_ns)) 2963cf8d2c11STrond Myklebust return new_ns; 29641da177e4SLinus Torvalds 296597216be0SAl Viro namespace_lock(); 29661da177e4SLinus Torvalds /* First pass: copy the tree topology */ 29674ce5d2b1SEric W. Biederman copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE; 29689559f689SAl Viro if (user_ns != ns->user_ns) 2969132c94e3SEric W. Biederman copy_flags |= CL_SHARED_TO_SLAVE | CL_UNPRIVILEGED; 29707a472ef4SEric W. Biederman new = copy_tree(old, old->mnt.mnt_root, copy_flags); 2971be34d1a3SDavid Howells if (IS_ERR(new)) { 2972328e6d90SAl Viro namespace_unlock(); 2973771b1371SEric W. Biederman free_mnt_ns(new_ns); 2974be34d1a3SDavid Howells return ERR_CAST(new); 29751da177e4SLinus Torvalds } 2976be08d6d2SAl Viro new_ns->root = new; 29771a4eeaf2SAl Viro list_add_tail(&new_ns->list, &new->mnt_list); 29781da177e4SLinus Torvalds 29791da177e4SLinus Torvalds /* 29801da177e4SLinus Torvalds * Second pass: switch the tsk->fs->* elements and mark new vfsmounts 29811da177e4SLinus Torvalds * as belonging to new namespace. We have already acquired a private 29821da177e4SLinus Torvalds * fs_struct, so tsk->fs->lock is not needed. 29831da177e4SLinus Torvalds */ 2984909b0a88SAl Viro p = old; 2985cb338d06SAl Viro q = new; 29861da177e4SLinus Torvalds while (p) { 2987143c8c91SAl Viro q->mnt_ns = new_ns; 2988d2921684SEric W. Biederman new_ns->mounts++; 29899559f689SAl Viro if (new_fs) { 29909559f689SAl Viro if (&p->mnt == new_fs->root.mnt) { 29919559f689SAl Viro new_fs->root.mnt = mntget(&q->mnt); 2992315fc83eSAl Viro rootmnt = &p->mnt; 29931da177e4SLinus Torvalds } 29949559f689SAl Viro if (&p->mnt == new_fs->pwd.mnt) { 29959559f689SAl Viro new_fs->pwd.mnt = mntget(&q->mnt); 2996315fc83eSAl Viro pwdmnt = &p->mnt; 29971da177e4SLinus Torvalds } 29981da177e4SLinus Torvalds } 2999909b0a88SAl Viro p = next_mnt(p, old); 3000909b0a88SAl Viro q = next_mnt(q, new); 30014ce5d2b1SEric W. Biederman if (!q) 30024ce5d2b1SEric W. Biederman break; 30034ce5d2b1SEric W. Biederman while (p->mnt.mnt_root != q->mnt.mnt_root) 30044ce5d2b1SEric W. Biederman p = next_mnt(p, old); 30051da177e4SLinus Torvalds } 3006328e6d90SAl Viro namespace_unlock(); 30071da177e4SLinus Torvalds 30081da177e4SLinus Torvalds if (rootmnt) 3009f03c6599SAl Viro mntput(rootmnt); 30101da177e4SLinus Torvalds if (pwdmnt) 3011f03c6599SAl Viro mntput(pwdmnt); 30121da177e4SLinus Torvalds 3013741a2951SJANAK DESAI return new_ns; 3014741a2951SJANAK DESAI } 3015741a2951SJANAK DESAI 3016cf8d2c11STrond Myklebust /** 3017cf8d2c11STrond Myklebust * create_mnt_ns - creates a private namespace and adds a root filesystem 3018cf8d2c11STrond Myklebust * @mnt: pointer to the new root filesystem mountpoint 3019cf8d2c11STrond Myklebust */ 30201a4eeaf2SAl Viro static struct mnt_namespace *create_mnt_ns(struct vfsmount *m) 3021cf8d2c11STrond Myklebust { 3022771b1371SEric W. Biederman struct mnt_namespace *new_ns = alloc_mnt_ns(&init_user_ns); 3023cf8d2c11STrond Myklebust if (!IS_ERR(new_ns)) { 30241a4eeaf2SAl Viro struct mount *mnt = real_mount(m); 30251a4eeaf2SAl Viro mnt->mnt_ns = new_ns; 3026be08d6d2SAl Viro new_ns->root = mnt; 3027d2921684SEric W. Biederman new_ns->mounts++; 3028b1983cd8SAl Viro list_add(&mnt->mnt_list, &new_ns->list); 3029c1334495SAl Viro } else { 30301a4eeaf2SAl Viro mntput(m); 3031cf8d2c11STrond Myklebust } 3032cf8d2c11STrond Myklebust return new_ns; 3033cf8d2c11STrond Myklebust } 3034cf8d2c11STrond Myklebust 3035ea441d11SAl Viro struct dentry *mount_subtree(struct vfsmount *mnt, const char *name) 3036ea441d11SAl Viro { 3037ea441d11SAl Viro struct mnt_namespace *ns; 3038d31da0f0SAl Viro struct super_block *s; 3039ea441d11SAl Viro struct path path; 3040ea441d11SAl Viro int err; 3041ea441d11SAl Viro 3042ea441d11SAl Viro ns = create_mnt_ns(mnt); 3043ea441d11SAl Viro if (IS_ERR(ns)) 3044ea441d11SAl Viro return ERR_CAST(ns); 3045ea441d11SAl Viro 3046ea441d11SAl Viro err = vfs_path_lookup(mnt->mnt_root, mnt, 3047ea441d11SAl Viro name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path); 3048ea441d11SAl Viro 3049ea441d11SAl Viro put_mnt_ns(ns); 3050ea441d11SAl Viro 3051ea441d11SAl Viro if (err) 3052ea441d11SAl Viro return ERR_PTR(err); 3053ea441d11SAl Viro 3054ea441d11SAl Viro /* trade a vfsmount reference for active sb one */ 3055d31da0f0SAl Viro s = path.mnt->mnt_sb; 3056d31da0f0SAl Viro atomic_inc(&s->s_active); 3057ea441d11SAl Viro mntput(path.mnt); 3058ea441d11SAl Viro /* lock the sucker */ 3059d31da0f0SAl Viro down_write(&s->s_umount); 3060ea441d11SAl Viro /* ... and return the root of (sub)tree on it */ 3061ea441d11SAl Viro return path.dentry; 3062ea441d11SAl Viro } 3063ea441d11SAl Viro EXPORT_SYMBOL(mount_subtree); 3064ea441d11SAl Viro 3065312db1aaSDominik Brodowski int ksys_mount(char __user *dev_name, char __user *dir_name, char __user *type, 3066312db1aaSDominik Brodowski unsigned long flags, void __user *data) 30671da177e4SLinus Torvalds { 3068eca6f534SVegard Nossum int ret; 3069eca6f534SVegard Nossum char *kernel_type; 3070eca6f534SVegard Nossum char *kernel_dev; 3071b40ef869SAl Viro void *options; 30721da177e4SLinus Torvalds 3073b8850d1fSTim Gardner kernel_type = copy_mount_string(type); 3074b8850d1fSTim Gardner ret = PTR_ERR(kernel_type); 3075b8850d1fSTim Gardner if (IS_ERR(kernel_type)) 3076eca6f534SVegard Nossum goto out_type; 30771da177e4SLinus Torvalds 3078b8850d1fSTim Gardner kernel_dev = copy_mount_string(dev_name); 3079b8850d1fSTim Gardner ret = PTR_ERR(kernel_dev); 3080b8850d1fSTim Gardner if (IS_ERR(kernel_dev)) 3081eca6f534SVegard Nossum goto out_dev; 30821da177e4SLinus Torvalds 3083b40ef869SAl Viro options = copy_mount_options(data); 3084b40ef869SAl Viro ret = PTR_ERR(options); 3085b40ef869SAl Viro if (IS_ERR(options)) 3086eca6f534SVegard Nossum goto out_data; 30871da177e4SLinus Torvalds 3088b40ef869SAl Viro ret = do_mount(kernel_dev, dir_name, kernel_type, flags, options); 3089eca6f534SVegard Nossum 3090b40ef869SAl Viro kfree(options); 3091eca6f534SVegard Nossum out_data: 3092eca6f534SVegard Nossum kfree(kernel_dev); 3093eca6f534SVegard Nossum out_dev: 3094eca6f534SVegard Nossum kfree(kernel_type); 3095eca6f534SVegard Nossum out_type: 3096eca6f534SVegard Nossum return ret; 30971da177e4SLinus Torvalds } 30981da177e4SLinus Torvalds 3099312db1aaSDominik Brodowski SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name, 3100312db1aaSDominik Brodowski char __user *, type, unsigned long, flags, void __user *, data) 3101312db1aaSDominik Brodowski { 3102312db1aaSDominik Brodowski return ksys_mount(dev_name, dir_name, type, flags, data); 3103312db1aaSDominik Brodowski } 3104312db1aaSDominik Brodowski 31051da177e4SLinus Torvalds /* 3106afac7cbaSAl Viro * Return true if path is reachable from root 3107afac7cbaSAl Viro * 310848a066e7SAl Viro * namespace_sem or mount_lock is held 3109afac7cbaSAl Viro */ 3110643822b4SAl Viro bool is_path_reachable(struct mount *mnt, struct dentry *dentry, 3111afac7cbaSAl Viro const struct path *root) 3112afac7cbaSAl Viro { 3113643822b4SAl Viro while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) { 3114a73324daSAl Viro dentry = mnt->mnt_mountpoint; 31150714a533SAl Viro mnt = mnt->mnt_parent; 3116afac7cbaSAl Viro } 3117643822b4SAl Viro return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry); 3118afac7cbaSAl Viro } 3119afac7cbaSAl Viro 3120640eb7e7SMickaël Salaün bool path_is_under(const struct path *path1, const struct path *path2) 3121afac7cbaSAl Viro { 312225ab4c9bSYaowei Bai bool res; 312348a066e7SAl Viro read_seqlock_excl(&mount_lock); 3124643822b4SAl Viro res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2); 312548a066e7SAl Viro read_sequnlock_excl(&mount_lock); 3126afac7cbaSAl Viro return res; 3127afac7cbaSAl Viro } 3128afac7cbaSAl Viro EXPORT_SYMBOL(path_is_under); 3129afac7cbaSAl Viro 3130afac7cbaSAl Viro /* 31311da177e4SLinus Torvalds * pivot_root Semantics: 31321da177e4SLinus Torvalds * Moves the root file system of the current process to the directory put_old, 31331da177e4SLinus Torvalds * makes new_root as the new root file system of the current process, and sets 31341da177e4SLinus Torvalds * root/cwd of all processes which had them on the current root to new_root. 31351da177e4SLinus Torvalds * 31361da177e4SLinus Torvalds * Restrictions: 31371da177e4SLinus Torvalds * The new_root and put_old must be directories, and must not be on the 31381da177e4SLinus Torvalds * same file system as the current process root. The put_old must be 31391da177e4SLinus Torvalds * underneath new_root, i.e. adding a non-zero number of /.. to the string 31401da177e4SLinus Torvalds * pointed to by put_old must yield the same directory as new_root. No other 31411da177e4SLinus Torvalds * file system may be mounted on put_old. After all, new_root is a mountpoint. 31421da177e4SLinus Torvalds * 31434a0d11faSNeil Brown * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem. 31444a0d11faSNeil Brown * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives 31454a0d11faSNeil Brown * in this situation. 31464a0d11faSNeil Brown * 31471da177e4SLinus Torvalds * Notes: 31481da177e4SLinus Torvalds * - we don't move root/cwd if they are not at the root (reason: if something 31491da177e4SLinus Torvalds * cared enough to change them, it's probably wrong to force them elsewhere) 31501da177e4SLinus Torvalds * - it's okay to pick a root that isn't the root of a file system, e.g. 31511da177e4SLinus Torvalds * /nfs/my_root where /nfs is the mount point. It must be a mountpoint, 31521da177e4SLinus Torvalds * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root 31531da177e4SLinus Torvalds * first. 31541da177e4SLinus Torvalds */ 31553480b257SHeiko Carstens SYSCALL_DEFINE2(pivot_root, const char __user *, new_root, 31563480b257SHeiko Carstens const char __user *, put_old) 31571da177e4SLinus Torvalds { 31582d8f3038SAl Viro struct path new, old, parent_path, root_parent, root; 315984d17192SAl Viro struct mount *new_mnt, *root_mnt, *old_mnt; 316084d17192SAl Viro struct mountpoint *old_mp, *root_mp; 31611da177e4SLinus Torvalds int error; 31621da177e4SLinus Torvalds 31639b40bc90SAl Viro if (!may_mount()) 31641da177e4SLinus Torvalds return -EPERM; 31651da177e4SLinus Torvalds 31662d8f3038SAl Viro error = user_path_dir(new_root, &new); 31671da177e4SLinus Torvalds if (error) 31681da177e4SLinus Torvalds goto out0; 31691da177e4SLinus Torvalds 31702d8f3038SAl Viro error = user_path_dir(put_old, &old); 31711da177e4SLinus Torvalds if (error) 31721da177e4SLinus Torvalds goto out1; 31731da177e4SLinus Torvalds 31742d8f3038SAl Viro error = security_sb_pivotroot(&old, &new); 3175b12cea91SAl Viro if (error) 3176b12cea91SAl Viro goto out2; 31771da177e4SLinus Torvalds 3178f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &root); 317984d17192SAl Viro old_mp = lock_mount(&old); 318084d17192SAl Viro error = PTR_ERR(old_mp); 318184d17192SAl Viro if (IS_ERR(old_mp)) 3182b12cea91SAl Viro goto out3; 3183b12cea91SAl Viro 31841da177e4SLinus Torvalds error = -EINVAL; 3185419148daSAl Viro new_mnt = real_mount(new.mnt); 3186419148daSAl Viro root_mnt = real_mount(root.mnt); 318784d17192SAl Viro old_mnt = real_mount(old.mnt); 318884d17192SAl Viro if (IS_MNT_SHARED(old_mnt) || 3189fc7be130SAl Viro IS_MNT_SHARED(new_mnt->mnt_parent) || 3190fc7be130SAl Viro IS_MNT_SHARED(root_mnt->mnt_parent)) 3191b12cea91SAl Viro goto out4; 3192143c8c91SAl Viro if (!check_mnt(root_mnt) || !check_mnt(new_mnt)) 3193b12cea91SAl Viro goto out4; 31945ff9d8a6SEric W. Biederman if (new_mnt->mnt.mnt_flags & MNT_LOCKED) 31955ff9d8a6SEric W. Biederman goto out4; 31961da177e4SLinus Torvalds error = -ENOENT; 3197f3da392eSAlexey Dobriyan if (d_unlinked(new.dentry)) 3198b12cea91SAl Viro goto out4; 31991da177e4SLinus Torvalds error = -EBUSY; 320084d17192SAl Viro if (new_mnt == root_mnt || old_mnt == root_mnt) 3201b12cea91SAl Viro goto out4; /* loop, on the same file system */ 32021da177e4SLinus Torvalds error = -EINVAL; 32038c3ee42eSAl Viro if (root.mnt->mnt_root != root.dentry) 3204b12cea91SAl Viro goto out4; /* not a mountpoint */ 3205676da58dSAl Viro if (!mnt_has_parent(root_mnt)) 3206b12cea91SAl Viro goto out4; /* not attached */ 320784d17192SAl Viro root_mp = root_mnt->mnt_mp; 32082d8f3038SAl Viro if (new.mnt->mnt_root != new.dentry) 3209b12cea91SAl Viro goto out4; /* not a mountpoint */ 3210676da58dSAl Viro if (!mnt_has_parent(new_mnt)) 3211b12cea91SAl Viro goto out4; /* not attached */ 32124ac91378SJan Blunck /* make sure we can reach put_old from new_root */ 321384d17192SAl Viro if (!is_path_reachable(old_mnt, old.dentry, &new)) 3214b12cea91SAl Viro goto out4; 32150d082601SEric W. Biederman /* make certain new is below the root */ 32160d082601SEric W. Biederman if (!is_path_reachable(new_mnt, new.dentry, &root)) 32170d082601SEric W. Biederman goto out4; 321884d17192SAl Viro root_mp->m_count++; /* pin it so it won't go away */ 3219719ea2fbSAl Viro lock_mount_hash(); 3220419148daSAl Viro detach_mnt(new_mnt, &parent_path); 3221419148daSAl Viro detach_mnt(root_mnt, &root_parent); 32225ff9d8a6SEric W. Biederman if (root_mnt->mnt.mnt_flags & MNT_LOCKED) { 32235ff9d8a6SEric W. Biederman new_mnt->mnt.mnt_flags |= MNT_LOCKED; 32245ff9d8a6SEric W. Biederman root_mnt->mnt.mnt_flags &= ~MNT_LOCKED; 32255ff9d8a6SEric W. Biederman } 32264ac91378SJan Blunck /* mount old root on put_old */ 322784d17192SAl Viro attach_mnt(root_mnt, old_mnt, old_mp); 32284ac91378SJan Blunck /* mount new_root on / */ 322984d17192SAl Viro attach_mnt(new_mnt, real_mount(root_parent.mnt), root_mp); 32306b3286edSKirill Korotaev touch_mnt_namespace(current->nsproxy->mnt_ns); 32314fed655cSEric W. Biederman /* A moved mount should not expire automatically */ 32324fed655cSEric W. Biederman list_del_init(&new_mnt->mnt_expire); 32333895dbf8SEric W. Biederman put_mountpoint(root_mp); 3234719ea2fbSAl Viro unlock_mount_hash(); 32352d8f3038SAl Viro chroot_fs_refs(&root, &new); 32361da177e4SLinus Torvalds error = 0; 3237b12cea91SAl Viro out4: 323884d17192SAl Viro unlock_mount(old_mp); 3239b12cea91SAl Viro if (!error) { 32401a390689SAl Viro path_put(&root_parent); 32411a390689SAl Viro path_put(&parent_path); 3242b12cea91SAl Viro } 3243b12cea91SAl Viro out3: 32448c3ee42eSAl Viro path_put(&root); 3245b12cea91SAl Viro out2: 32462d8f3038SAl Viro path_put(&old); 32471da177e4SLinus Torvalds out1: 32482d8f3038SAl Viro path_put(&new); 32491da177e4SLinus Torvalds out0: 32501da177e4SLinus Torvalds return error; 32511da177e4SLinus Torvalds } 32521da177e4SLinus Torvalds 32531da177e4SLinus Torvalds static void __init init_mount_tree(void) 32541da177e4SLinus Torvalds { 32551da177e4SLinus Torvalds struct vfsmount *mnt; 32566b3286edSKirill Korotaev struct mnt_namespace *ns; 3257ac748a09SJan Blunck struct path root; 32580c55cfc4SEric W. Biederman struct file_system_type *type; 32591da177e4SLinus Torvalds 32600c55cfc4SEric W. Biederman type = get_fs_type("rootfs"); 32610c55cfc4SEric W. Biederman if (!type) 32620c55cfc4SEric W. Biederman panic("Can't find rootfs type"); 32630c55cfc4SEric W. Biederman mnt = vfs_kern_mount(type, 0, "rootfs", NULL); 32640c55cfc4SEric W. Biederman put_filesystem(type); 32651da177e4SLinus Torvalds if (IS_ERR(mnt)) 32661da177e4SLinus Torvalds panic("Can't create rootfs"); 3267b3e19d92SNick Piggin 32683b22edc5STrond Myklebust ns = create_mnt_ns(mnt); 32693b22edc5STrond Myklebust if (IS_ERR(ns)) 32701da177e4SLinus Torvalds panic("Can't allocate initial namespace"); 32711da177e4SLinus Torvalds 32726b3286edSKirill Korotaev init_task.nsproxy->mnt_ns = ns; 32736b3286edSKirill Korotaev get_mnt_ns(ns); 32741da177e4SLinus Torvalds 3275be08d6d2SAl Viro root.mnt = mnt; 3276be08d6d2SAl Viro root.dentry = mnt->mnt_root; 3277da362b09SEric W. Biederman mnt->mnt_flags |= MNT_LOCKED; 3278ac748a09SJan Blunck 3279ac748a09SJan Blunck set_fs_pwd(current->fs, &root); 3280ac748a09SJan Blunck set_fs_root(current->fs, &root); 32811da177e4SLinus Torvalds } 32821da177e4SLinus Torvalds 328374bf17cfSDenis Cheng void __init mnt_init(void) 32841da177e4SLinus Torvalds { 328515a67dd8SRandy Dunlap int err; 32861da177e4SLinus Torvalds 32877d6fec45SAl Viro mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount), 328820c2df83SPaul Mundt 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL); 32891da177e4SLinus Torvalds 32900818bf27SAl Viro mount_hashtable = alloc_large_system_hash("Mount-cache", 329138129a13SAl Viro sizeof(struct hlist_head), 32920818bf27SAl Viro mhash_entries, 19, 32933d375d78SPavel Tatashin HASH_ZERO, 32940818bf27SAl Viro &m_hash_shift, &m_hash_mask, 0, 0); 32950818bf27SAl Viro mountpoint_hashtable = alloc_large_system_hash("Mountpoint-cache", 32960818bf27SAl Viro sizeof(struct hlist_head), 32970818bf27SAl Viro mphash_entries, 19, 32983d375d78SPavel Tatashin HASH_ZERO, 32990818bf27SAl Viro &mp_hash_shift, &mp_hash_mask, 0, 0); 33001da177e4SLinus Torvalds 330184d17192SAl Viro if (!mount_hashtable || !mountpoint_hashtable) 33021da177e4SLinus Torvalds panic("Failed to allocate mount hash table\n"); 33031da177e4SLinus Torvalds 33044b93dc9bSTejun Heo kernfs_init(); 33054b93dc9bSTejun Heo 330615a67dd8SRandy Dunlap err = sysfs_init(); 330715a67dd8SRandy Dunlap if (err) 330815a67dd8SRandy Dunlap printk(KERN_WARNING "%s: sysfs_init error: %d\n", 33098e24eea7SHarvey Harrison __func__, err); 331000d26666SGreg Kroah-Hartman fs_kobj = kobject_create_and_add("fs", NULL); 331100d26666SGreg Kroah-Hartman if (!fs_kobj) 33128e24eea7SHarvey Harrison printk(KERN_WARNING "%s: kobj create error\n", __func__); 33131da177e4SLinus Torvalds init_rootfs(); 33141da177e4SLinus Torvalds init_mount_tree(); 33151da177e4SLinus Torvalds } 33161da177e4SLinus Torvalds 3317616511d0STrond Myklebust void put_mnt_ns(struct mnt_namespace *ns) 33181da177e4SLinus Torvalds { 3319d498b25aSAl Viro if (!atomic_dec_and_test(&ns->count)) 3320616511d0STrond Myklebust return; 33217b00ed6fSAl Viro drop_collected_mounts(&ns->root->mnt); 3322771b1371SEric W. Biederman free_mnt_ns(ns); 33231da177e4SLinus Torvalds } 33249d412a43SAl Viro 33259d412a43SAl Viro struct vfsmount *kern_mount_data(struct file_system_type *type, void *data) 33269d412a43SAl Viro { 3327423e0ab0STim Chen struct vfsmount *mnt; 3328e462ec50SDavid Howells mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, data); 3329423e0ab0STim Chen if (!IS_ERR(mnt)) { 3330423e0ab0STim Chen /* 3331423e0ab0STim Chen * it is a longterm mount, don't release mnt until 3332423e0ab0STim Chen * we unmount before file sys is unregistered 3333423e0ab0STim Chen */ 3334f7a99c5bSAl Viro real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL; 3335423e0ab0STim Chen } 3336423e0ab0STim Chen return mnt; 33379d412a43SAl Viro } 33389d412a43SAl Viro EXPORT_SYMBOL_GPL(kern_mount_data); 3339423e0ab0STim Chen 3340423e0ab0STim Chen void kern_unmount(struct vfsmount *mnt) 3341423e0ab0STim Chen { 3342423e0ab0STim Chen /* release long term mount so mount point can be released */ 3343423e0ab0STim Chen if (!IS_ERR_OR_NULL(mnt)) { 3344f7a99c5bSAl Viro real_mount(mnt)->mnt_ns = NULL; 334548a066e7SAl Viro synchronize_rcu(); /* yecchhh... */ 3346423e0ab0STim Chen mntput(mnt); 3347423e0ab0STim Chen } 3348423e0ab0STim Chen } 3349423e0ab0STim Chen EXPORT_SYMBOL(kern_unmount); 335002125a82SAl Viro 335102125a82SAl Viro bool our_mnt(struct vfsmount *mnt) 335202125a82SAl Viro { 3353143c8c91SAl Viro return check_mnt(real_mount(mnt)); 335402125a82SAl Viro } 33558823c079SEric W. Biederman 33563151527eSEric W. Biederman bool current_chrooted(void) 33573151527eSEric W. Biederman { 33583151527eSEric W. Biederman /* Does the current process have a non-standard root */ 33593151527eSEric W. Biederman struct path ns_root; 33603151527eSEric W. Biederman struct path fs_root; 33613151527eSEric W. Biederman bool chrooted; 33623151527eSEric W. Biederman 33633151527eSEric W. Biederman /* Find the namespace root */ 33643151527eSEric W. Biederman ns_root.mnt = ¤t->nsproxy->mnt_ns->root->mnt; 33653151527eSEric W. Biederman ns_root.dentry = ns_root.mnt->mnt_root; 33663151527eSEric W. Biederman path_get(&ns_root); 33673151527eSEric W. Biederman while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root)) 33683151527eSEric W. Biederman ; 33693151527eSEric W. Biederman 33703151527eSEric W. Biederman get_fs_root(current->fs, &fs_root); 33713151527eSEric W. Biederman 33723151527eSEric W. Biederman chrooted = !path_equal(&fs_root, &ns_root); 33733151527eSEric W. Biederman 33743151527eSEric W. Biederman path_put(&fs_root); 33753151527eSEric W. Biederman path_put(&ns_root); 33763151527eSEric W. Biederman 33773151527eSEric W. Biederman return chrooted; 33783151527eSEric W. Biederman } 33793151527eSEric W. Biederman 33808654df4eSEric W. Biederman static bool mnt_already_visible(struct mnt_namespace *ns, struct vfsmount *new, 33818654df4eSEric W. Biederman int *new_mnt_flags) 338287a8ebd6SEric W. Biederman { 33838c6cf9ccSEric W. Biederman int new_flags = *new_mnt_flags; 338487a8ebd6SEric W. Biederman struct mount *mnt; 3385e51db735SEric W. Biederman bool visible = false; 338687a8ebd6SEric W. Biederman 338744bb4385SAl Viro down_read(&namespace_sem); 338887a8ebd6SEric W. Biederman list_for_each_entry(mnt, &ns->list, mnt_list) { 3389e51db735SEric W. Biederman struct mount *child; 339077b1a97dSEric W. Biederman int mnt_flags; 339177b1a97dSEric W. Biederman 33928654df4eSEric W. Biederman if (mnt->mnt.mnt_sb->s_type != new->mnt_sb->s_type) 3393e51db735SEric W. Biederman continue; 3394e51db735SEric W. Biederman 33957e96c1b0SEric W. Biederman /* This mount is not fully visible if it's root directory 33967e96c1b0SEric W. Biederman * is not the root directory of the filesystem. 33977e96c1b0SEric W. Biederman */ 33987e96c1b0SEric W. Biederman if (mnt->mnt.mnt_root != mnt->mnt.mnt_sb->s_root) 33997e96c1b0SEric W. Biederman continue; 34007e96c1b0SEric W. Biederman 3401a1935c17SEric W. Biederman /* A local view of the mount flags */ 340277b1a97dSEric W. Biederman mnt_flags = mnt->mnt.mnt_flags; 340377b1a97dSEric W. Biederman 3404695e9df0SEric W. Biederman /* Don't miss readonly hidden in the superblock flags */ 3405bc98a42cSDavid Howells if (sb_rdonly(mnt->mnt.mnt_sb)) 3406695e9df0SEric W. Biederman mnt_flags |= MNT_LOCK_READONLY; 3407695e9df0SEric W. Biederman 34088c6cf9ccSEric W. Biederman /* Verify the mount flags are equal to or more permissive 34098c6cf9ccSEric W. Biederman * than the proposed new mount. 34108c6cf9ccSEric W. Biederman */ 341177b1a97dSEric W. Biederman if ((mnt_flags & MNT_LOCK_READONLY) && 34128c6cf9ccSEric W. Biederman !(new_flags & MNT_READONLY)) 34138c6cf9ccSEric W. Biederman continue; 341477b1a97dSEric W. Biederman if ((mnt_flags & MNT_LOCK_ATIME) && 341577b1a97dSEric W. Biederman ((mnt_flags & MNT_ATIME_MASK) != (new_flags & MNT_ATIME_MASK))) 34168c6cf9ccSEric W. Biederman continue; 34178c6cf9ccSEric W. Biederman 3418ceeb0e5dSEric W. Biederman /* This mount is not fully visible if there are any 3419ceeb0e5dSEric W. Biederman * locked child mounts that cover anything except for 3420ceeb0e5dSEric W. Biederman * empty directories. 3421e51db735SEric W. Biederman */ 3422e51db735SEric W. Biederman list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) { 3423e51db735SEric W. Biederman struct inode *inode = child->mnt_mountpoint->d_inode; 3424ceeb0e5dSEric W. Biederman /* Only worry about locked mounts */ 3425d71ed6c9SEric W. Biederman if (!(child->mnt.mnt_flags & MNT_LOCKED)) 3426ceeb0e5dSEric W. Biederman continue; 34277236c85eSEric W. Biederman /* Is the directory permanetly empty? */ 34287236c85eSEric W. Biederman if (!is_empty_dir_inode(inode)) 3429e51db735SEric W. Biederman goto next; 343087a8ebd6SEric W. Biederman } 34318c6cf9ccSEric W. Biederman /* Preserve the locked attributes */ 343277b1a97dSEric W. Biederman *new_mnt_flags |= mnt_flags & (MNT_LOCK_READONLY | \ 34338c6cf9ccSEric W. Biederman MNT_LOCK_ATIME); 3434e51db735SEric W. Biederman visible = true; 3435e51db735SEric W. Biederman goto found; 3436e51db735SEric W. Biederman next: ; 343787a8ebd6SEric W. Biederman } 3438e51db735SEric W. Biederman found: 343944bb4385SAl Viro up_read(&namespace_sem); 3440e51db735SEric W. Biederman return visible; 344187a8ebd6SEric W. Biederman } 344287a8ebd6SEric W. Biederman 34438654df4eSEric W. Biederman static bool mount_too_revealing(struct vfsmount *mnt, int *new_mnt_flags) 34448654df4eSEric W. Biederman { 3445a1935c17SEric W. Biederman const unsigned long required_iflags = SB_I_NOEXEC | SB_I_NODEV; 34468654df4eSEric W. Biederman struct mnt_namespace *ns = current->nsproxy->mnt_ns; 34478654df4eSEric W. Biederman unsigned long s_iflags; 34488654df4eSEric W. Biederman 34498654df4eSEric W. Biederman if (ns->user_ns == &init_user_ns) 34508654df4eSEric W. Biederman return false; 34518654df4eSEric W. Biederman 34528654df4eSEric W. Biederman /* Can this filesystem be too revealing? */ 34538654df4eSEric W. Biederman s_iflags = mnt->mnt_sb->s_iflags; 34548654df4eSEric W. Biederman if (!(s_iflags & SB_I_USERNS_VISIBLE)) 34558654df4eSEric W. Biederman return false; 34568654df4eSEric W. Biederman 3457a1935c17SEric W. Biederman if ((s_iflags & required_iflags) != required_iflags) { 3458a1935c17SEric W. Biederman WARN_ONCE(1, "Expected s_iflags to contain 0x%lx\n", 3459a1935c17SEric W. Biederman required_iflags); 3460a1935c17SEric W. Biederman return true; 3461a1935c17SEric W. Biederman } 3462a1935c17SEric W. Biederman 34638654df4eSEric W. Biederman return !mnt_already_visible(ns, mnt, new_mnt_flags); 34648654df4eSEric W. Biederman } 34658654df4eSEric W. Biederman 3466380cf5baSAndy Lutomirski bool mnt_may_suid(struct vfsmount *mnt) 3467380cf5baSAndy Lutomirski { 3468380cf5baSAndy Lutomirski /* 3469380cf5baSAndy Lutomirski * Foreign mounts (accessed via fchdir or through /proc 3470380cf5baSAndy Lutomirski * symlinks) are always treated as if they are nosuid. This 3471380cf5baSAndy Lutomirski * prevents namespaces from trusting potentially unsafe 3472380cf5baSAndy Lutomirski * suid/sgid bits, file caps, or security labels that originate 3473380cf5baSAndy Lutomirski * in other namespaces. 3474380cf5baSAndy Lutomirski */ 3475380cf5baSAndy Lutomirski return !(mnt->mnt_flags & MNT_NOSUID) && check_mnt(real_mount(mnt)) && 3476380cf5baSAndy Lutomirski current_in_userns(mnt->mnt_sb->s_user_ns); 3477380cf5baSAndy Lutomirski } 3478380cf5baSAndy Lutomirski 347964964528SAl Viro static struct ns_common *mntns_get(struct task_struct *task) 34808823c079SEric W. Biederman { 348158be2825SAl Viro struct ns_common *ns = NULL; 34828823c079SEric W. Biederman struct nsproxy *nsproxy; 34838823c079SEric W. Biederman 3484728dba3aSEric W. Biederman task_lock(task); 3485728dba3aSEric W. Biederman nsproxy = task->nsproxy; 34868823c079SEric W. Biederman if (nsproxy) { 348758be2825SAl Viro ns = &nsproxy->mnt_ns->ns; 348858be2825SAl Viro get_mnt_ns(to_mnt_ns(ns)); 34898823c079SEric W. Biederman } 3490728dba3aSEric W. Biederman task_unlock(task); 34918823c079SEric W. Biederman 34928823c079SEric W. Biederman return ns; 34938823c079SEric W. Biederman } 34948823c079SEric W. Biederman 349564964528SAl Viro static void mntns_put(struct ns_common *ns) 34968823c079SEric W. Biederman { 349758be2825SAl Viro put_mnt_ns(to_mnt_ns(ns)); 34988823c079SEric W. Biederman } 34998823c079SEric W. Biederman 350064964528SAl Viro static int mntns_install(struct nsproxy *nsproxy, struct ns_common *ns) 35018823c079SEric W. Biederman { 35028823c079SEric W. Biederman struct fs_struct *fs = current->fs; 35034f757f3cSAl Viro struct mnt_namespace *mnt_ns = to_mnt_ns(ns), *old_mnt_ns; 35048823c079SEric W. Biederman struct path root; 35054f757f3cSAl Viro int err; 35068823c079SEric W. Biederman 35070c55cfc4SEric W. Biederman if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) || 3508c7b96acfSEric W. Biederman !ns_capable(current_user_ns(), CAP_SYS_CHROOT) || 3509c7b96acfSEric W. Biederman !ns_capable(current_user_ns(), CAP_SYS_ADMIN)) 3510ae11e0f1SZhao Hongjiang return -EPERM; 35118823c079SEric W. Biederman 35128823c079SEric W. Biederman if (fs->users != 1) 35138823c079SEric W. Biederman return -EINVAL; 35148823c079SEric W. Biederman 35158823c079SEric W. Biederman get_mnt_ns(mnt_ns); 35164f757f3cSAl Viro old_mnt_ns = nsproxy->mnt_ns; 35178823c079SEric W. Biederman nsproxy->mnt_ns = mnt_ns; 35188823c079SEric W. Biederman 35198823c079SEric W. Biederman /* Find the root */ 35204f757f3cSAl Viro err = vfs_path_lookup(mnt_ns->root->mnt.mnt_root, &mnt_ns->root->mnt, 35214f757f3cSAl Viro "/", LOOKUP_DOWN, &root); 35224f757f3cSAl Viro if (err) { 35234f757f3cSAl Viro /* revert to old namespace */ 35244f757f3cSAl Viro nsproxy->mnt_ns = old_mnt_ns; 35254f757f3cSAl Viro put_mnt_ns(mnt_ns); 35264f757f3cSAl Viro return err; 35274f757f3cSAl Viro } 35288823c079SEric W. Biederman 35294068367cSAndrei Vagin put_mnt_ns(old_mnt_ns); 35304068367cSAndrei Vagin 35318823c079SEric W. Biederman /* Update the pwd and root */ 35328823c079SEric W. Biederman set_fs_pwd(fs, &root); 35338823c079SEric W. Biederman set_fs_root(fs, &root); 35348823c079SEric W. Biederman 35358823c079SEric W. Biederman path_put(&root); 35368823c079SEric W. Biederman return 0; 35378823c079SEric W. Biederman } 35388823c079SEric W. Biederman 3539bcac25a5SAndrey Vagin static struct user_namespace *mntns_owner(struct ns_common *ns) 3540bcac25a5SAndrey Vagin { 3541bcac25a5SAndrey Vagin return to_mnt_ns(ns)->user_ns; 3542bcac25a5SAndrey Vagin } 3543bcac25a5SAndrey Vagin 35448823c079SEric W. Biederman const struct proc_ns_operations mntns_operations = { 35458823c079SEric W. Biederman .name = "mnt", 35468823c079SEric W. Biederman .type = CLONE_NEWNS, 35478823c079SEric W. Biederman .get = mntns_get, 35488823c079SEric W. Biederman .put = mntns_put, 35498823c079SEric W. Biederman .install = mntns_install, 3550bcac25a5SAndrey Vagin .owner = mntns_owner, 35518823c079SEric W. Biederman }; 3552