blob: 21fd423b19cf41e1344a6ed9c066482ee2b7809c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/namespace.c
3 *
4 * (C) Copyright Al Viro 2000, 2001
5 * Released under GPL v2.
6 *
7 * Based on code from fs/super.c, copyright Linus Torvalds and others.
8 * Heavily rewritten.
9 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/syscalls.h>
Al Virod10577a2011-12-07 13:06:11 -050012#include <linux/export.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080013#include <linux/capability.h>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080014#include <linux/mnt_namespace.h>
Eric W. Biederman771b1372012-07-26 21:08:32 -070015#include <linux/user_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/namei.h>
17#include <linux/security.h>
Miklos Szeredi73cd49e2008-03-26 22:11:34 +010018#include <linux/idr.h>
Rob Landley57f150a2013-09-11 14:26:10 -070019#include <linux/init.h> /* init_rootfs */
Al Virod10577a2011-12-07 13:06:11 -050020#include <linux/fs_struct.h> /* get_fs_root et.al. */
21#include <linux/fsnotify.h> /* fsnotify_vfsmount_delete */
22#include <linux/uaccess.h>
Jaegeuk Kimd1fc63d2017-09-12 17:56:31 -070023#include <linux/file.h>
David Howells0bb80f22013-04-12 01:50:06 +010024#include <linux/proc_ns.h>
Linus Torvalds20b4fb42013-05-01 17:51:54 -070025#include <linux/magic.h>
Al Viro0818bf22014-02-28 13:46:44 -050026#include <linux/bootmem.h>
Al Viro9ea459e2014-08-08 13:08:20 -040027#include <linux/task_work.h>
Ram Pai07b20882005-11-07 17:19:07 -050028#include "pnode.h"
Adrian Bunk948730b2007-07-15 23:41:25 -070029#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Eric W. Biedermand2921682016-09-28 00:27:17 -050031/* Maximum number of mounts in a mount namespace */
32unsigned int sysctl_mount_max __read_mostly = 100000;
33
Al Viro0818bf22014-02-28 13:46:44 -050034static unsigned int m_hash_mask __read_mostly;
35static unsigned int m_hash_shift __read_mostly;
36static unsigned int mp_hash_mask __read_mostly;
37static unsigned int mp_hash_shift __read_mostly;
38
39static __initdata unsigned long mhash_entries;
40static int __init set_mhash_entries(char *str)
41{
42 if (!str)
43 return 0;
44 mhash_entries = simple_strtoul(str, &str, 0);
45 return 1;
46}
47__setup("mhash_entries=", set_mhash_entries);
48
49static __initdata unsigned long mphash_entries;
50static int __init set_mphash_entries(char *str)
51{
52 if (!str)
53 return 0;
54 mphash_entries = simple_strtoul(str, &str, 0);
55 return 1;
56}
57__setup("mphash_entries=", set_mphash_entries);
Eric Dumazet13f14b42008-02-06 01:37:57 -080058
Al Viroc7999c32014-02-27 14:40:10 -050059static u64 event;
Miklos Szeredi73cd49e2008-03-26 22:11:34 +010060static DEFINE_IDA(mnt_id_ida);
Miklos Szeredi719f5d72008-03-27 13:06:23 +010061static DEFINE_IDA(mnt_group_ida);
Nick Piggin99b7db72010-08-18 04:37:39 +100062static DEFINE_SPINLOCK(mnt_id_lock);
Al Virof21f6222009-06-24 03:12:00 -040063static int mnt_id_start = 0;
64static int mnt_group_start = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Al Viro38129a12014-03-20 21:10:51 -040066static struct hlist_head *mount_hashtable __read_mostly;
Al Viro0818bf22014-02-28 13:46:44 -050067static struct hlist_head *mountpoint_hashtable __read_mostly;
Christoph Lametere18b8902006-12-06 20:33:20 -080068static struct kmem_cache *mnt_cache __read_mostly;
Al Viro59aa0da2013-09-16 21:34:53 -040069static DECLARE_RWSEM(namespace_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Miklos Szeredif87fd4c2006-01-16 22:14:23 -080071/* /sys/fs */
Greg Kroah-Hartman00d26662007-10-29 14:17:23 -060072struct kobject *fs_kobj;
73EXPORT_SYMBOL_GPL(fs_kobj);
Miklos Szeredif87fd4c2006-01-16 22:14:23 -080074
Nick Piggin99b7db72010-08-18 04:37:39 +100075/*
76 * vfsmount lock may be taken for read to prevent changes to the
77 * vfsmount hash, ie. during mountpoint lookups or walking back
78 * up the tree.
79 *
80 * It should be taken for write in all cases where the vfsmount
81 * tree or hash is modified or when a vfsmount structure is modified.
82 */
Al Viro48a066e2013-09-29 22:06:07 -040083__cacheline_aligned_in_smp DEFINE_SEQLOCK(mount_lock);
Nick Piggin99b7db72010-08-18 04:37:39 +100084
Al Viro38129a12014-03-20 21:10:51 -040085static inline struct hlist_head *m_hash(struct vfsmount *mnt, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Ram Paib58fed82005-11-07 17:16:09 -050087 unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
88 tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
Al Viro0818bf22014-02-28 13:46:44 -050089 tmp = tmp + (tmp >> m_hash_shift);
90 return &mount_hashtable[tmp & m_hash_mask];
91}
92
93static inline struct hlist_head *mp_hash(struct dentry *dentry)
94{
95 unsigned long tmp = ((unsigned long)dentry / L1_CACHE_BYTES);
96 tmp = tmp + (tmp >> mp_hash_shift);
97 return &mountpoint_hashtable[tmp & mp_hash_mask];
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
Nick Piggin99b7db72010-08-18 04:37:39 +1000100/*
101 * allocation is serialized by namespace_sem, but we need the spinlock to
102 * serialize with freeing.
103 */
Al Virob105e272011-11-24 20:38:33 -0500104static int mnt_alloc_id(struct mount *mnt)
Miklos Szeredi73cd49e2008-03-26 22:11:34 +0100105{
106 int res;
107
108retry:
109 ida_pre_get(&mnt_id_ida, GFP_KERNEL);
Nick Piggin99b7db72010-08-18 04:37:39 +1000110 spin_lock(&mnt_id_lock);
Al Viro15169fe2011-11-25 00:50:41 -0500111 res = ida_get_new_above(&mnt_id_ida, mnt_id_start, &mnt->mnt_id);
Al Virof21f6222009-06-24 03:12:00 -0400112 if (!res)
Al Viro15169fe2011-11-25 00:50:41 -0500113 mnt_id_start = mnt->mnt_id + 1;
Nick Piggin99b7db72010-08-18 04:37:39 +1000114 spin_unlock(&mnt_id_lock);
Miklos Szeredi73cd49e2008-03-26 22:11:34 +0100115 if (res == -EAGAIN)
116 goto retry;
117
118 return res;
119}
120
Al Virob105e272011-11-24 20:38:33 -0500121static void mnt_free_id(struct mount *mnt)
Miklos Szeredi73cd49e2008-03-26 22:11:34 +0100122{
Al Viro15169fe2011-11-25 00:50:41 -0500123 int id = mnt->mnt_id;
Nick Piggin99b7db72010-08-18 04:37:39 +1000124 spin_lock(&mnt_id_lock);
Al Virof21f6222009-06-24 03:12:00 -0400125 ida_remove(&mnt_id_ida, id);
126 if (mnt_id_start > id)
127 mnt_id_start = id;
Nick Piggin99b7db72010-08-18 04:37:39 +1000128 spin_unlock(&mnt_id_lock);
Miklos Szeredi73cd49e2008-03-26 22:11:34 +0100129}
130
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100131/*
132 * Allocate a new peer group ID
133 *
134 * mnt_group_ida is protected by namespace_sem
135 */
Al Viro4b8b21f2011-11-24 19:54:23 -0500136static int mnt_alloc_group_id(struct mount *mnt)
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100137{
Al Virof21f6222009-06-24 03:12:00 -0400138 int res;
139
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100140 if (!ida_pre_get(&mnt_group_ida, GFP_KERNEL))
141 return -ENOMEM;
142
Al Virof21f6222009-06-24 03:12:00 -0400143 res = ida_get_new_above(&mnt_group_ida,
144 mnt_group_start,
Al Viro15169fe2011-11-25 00:50:41 -0500145 &mnt->mnt_group_id);
Al Virof21f6222009-06-24 03:12:00 -0400146 if (!res)
Al Viro15169fe2011-11-25 00:50:41 -0500147 mnt_group_start = mnt->mnt_group_id + 1;
Al Virof21f6222009-06-24 03:12:00 -0400148
149 return res;
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100150}
151
152/*
153 * Release a peer group ID
154 */
Al Viro4b8b21f2011-11-24 19:54:23 -0500155void mnt_release_group_id(struct mount *mnt)
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100156{
Al Viro15169fe2011-11-25 00:50:41 -0500157 int id = mnt->mnt_group_id;
Al Virof21f6222009-06-24 03:12:00 -0400158 ida_remove(&mnt_group_ida, id);
159 if (mnt_group_start > id)
160 mnt_group_start = id;
Al Viro15169fe2011-11-25 00:50:41 -0500161 mnt->mnt_group_id = 0;
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100162}
163
Nick Pigginb3e19d92011-01-07 17:50:11 +1100164/*
165 * vfsmount lock must be held for read
166 */
Al Viro83adc752011-11-24 22:37:54 -0500167static inline void mnt_add_count(struct mount *mnt, int n)
Nick Pigginb3e19d92011-01-07 17:50:11 +1100168{
169#ifdef CONFIG_SMP
Al Viro68e8a9f2011-11-24 22:53:09 -0500170 this_cpu_add(mnt->mnt_pcp->mnt_count, n);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100171#else
172 preempt_disable();
Al Viro68e8a9f2011-11-24 22:53:09 -0500173 mnt->mnt_count += n;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100174 preempt_enable();
175#endif
176}
177
Nick Pigginb3e19d92011-01-07 17:50:11 +1100178/*
179 * vfsmount lock must be held for write
180 */
Al Viro83adc752011-11-24 22:37:54 -0500181unsigned int mnt_get_count(struct mount *mnt)
Nick Pigginb3e19d92011-01-07 17:50:11 +1100182{
183#ifdef CONFIG_SMP
Al Virof03c6592011-01-14 22:30:21 -0500184 unsigned int count = 0;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100185 int cpu;
186
187 for_each_possible_cpu(cpu) {
Al Viro68e8a9f2011-11-24 22:53:09 -0500188 count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100189 }
190
191 return count;
192#else
Al Viro68e8a9f2011-11-24 22:53:09 -0500193 return mnt->mnt_count;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100194#endif
195}
196
Al Viro87b95ce2015-01-10 19:01:08 -0500197static void drop_mountpoint(struct fs_pin *p)
198{
199 struct mount *m = container_of(p, struct mount, mnt_umount);
200 dput(m->mnt_ex_mountpoint);
201 pin_remove(p);
202 mntput(&m->mnt);
203}
204
Al Virob105e272011-11-24 20:38:33 -0500205static struct mount *alloc_vfsmnt(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Al Viroc63181e2011-11-25 02:35:16 -0500207 struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
208 if (mnt) {
Miklos Szeredi73cd49e2008-03-26 22:11:34 +0100209 int err;
210
Al Viroc63181e2011-11-25 02:35:16 -0500211 err = mnt_alloc_id(mnt);
Li Zefan88b38782008-07-21 18:06:36 +0800212 if (err)
213 goto out_free_cache;
214
215 if (name) {
Andrzej Hajdafcc139a2015-02-13 14:36:41 -0800216 mnt->mnt_devname = kstrdup_const(name, GFP_KERNEL);
Al Viroc63181e2011-11-25 02:35:16 -0500217 if (!mnt->mnt_devname)
Li Zefan88b38782008-07-21 18:06:36 +0800218 goto out_free_id;
Miklos Szeredi73cd49e2008-03-26 22:11:34 +0100219 }
220
Nick Pigginb3e19d92011-01-07 17:50:11 +1100221#ifdef CONFIG_SMP
Al Viroc63181e2011-11-25 02:35:16 -0500222 mnt->mnt_pcp = alloc_percpu(struct mnt_pcp);
223 if (!mnt->mnt_pcp)
Nick Pigginb3e19d92011-01-07 17:50:11 +1100224 goto out_free_devname;
225
Al Viroc63181e2011-11-25 02:35:16 -0500226 this_cpu_add(mnt->mnt_pcp->mnt_count, 1);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100227#else
Al Viroc63181e2011-11-25 02:35:16 -0500228 mnt->mnt_count = 1;
229 mnt->mnt_writers = 0;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100230#endif
Daniel Rosenberg707ae9d2017-09-06 13:06:43 -0700231 mnt->mnt.data = NULL;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100232
Al Viro38129a12014-03-20 21:10:51 -0400233 INIT_HLIST_NODE(&mnt->mnt_hash);
Al Viroc63181e2011-11-25 02:35:16 -0500234 INIT_LIST_HEAD(&mnt->mnt_child);
235 INIT_LIST_HEAD(&mnt->mnt_mounts);
236 INIT_LIST_HEAD(&mnt->mnt_list);
237 INIT_LIST_HEAD(&mnt->mnt_expire);
238 INIT_LIST_HEAD(&mnt->mnt_share);
239 INIT_LIST_HEAD(&mnt->mnt_slave_list);
240 INIT_LIST_HEAD(&mnt->mnt_slave);
Eric W. Biederman0a5eb7c2013-09-22 19:37:01 -0700241 INIT_HLIST_NODE(&mnt->mnt_mp_list);
Eric W. Biedermanbb4fbf02016-10-24 16:16:13 -0500242 INIT_LIST_HEAD(&mnt->mnt_umounting);
Andreas Gruenbacher2504c5d2009-12-17 21:24:27 -0500243#ifdef CONFIG_FSNOTIFY
244 INIT_HLIST_HEAD(&mnt->mnt_fsnotify_marks);
245#endif
Al Viro87b95ce2015-01-10 19:01:08 -0500246 init_fs_pin(&mnt->mnt_umount, drop_mountpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
Al Viroc63181e2011-11-25 02:35:16 -0500248 return mnt;
Li Zefan88b38782008-07-21 18:06:36 +0800249
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000250#ifdef CONFIG_SMP
251out_free_devname:
Andrzej Hajdafcc139a2015-02-13 14:36:41 -0800252 kfree_const(mnt->mnt_devname);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000253#endif
Li Zefan88b38782008-07-21 18:06:36 +0800254out_free_id:
Al Viroc63181e2011-11-25 02:35:16 -0500255 mnt_free_id(mnt);
Li Zefan88b38782008-07-21 18:06:36 +0800256out_free_cache:
Al Viroc63181e2011-11-25 02:35:16 -0500257 kmem_cache_free(mnt_cache, mnt);
Li Zefan88b38782008-07-21 18:06:36 +0800258 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
Dave Hansen83660252008-02-15 14:37:30 -0800261/*
262 * Most r/o checks on a fs are for operations that take
263 * discrete amounts of time, like a write() or unlink().
264 * We must keep track of when those operations start
265 * (for permission checks) and when they end, so that
266 * we can determine when writes are able to occur to
267 * a filesystem.
268 */
Dave Hansen3d733632008-02-15 14:37:59 -0800269/*
270 * __mnt_is_readonly: check whether a mount is read-only
271 * @mnt: the mount to check for its write status
272 *
273 * This shouldn't be used directly ouside of the VFS.
274 * It does not guarantee that the filesystem will stay
275 * r/w, just that it is right *now*. This can not and
276 * should not be used in place of IS_RDONLY(inode).
277 * mnt_want/drop_write() will _keep_ the filesystem
278 * r/w.
279 */
280int __mnt_is_readonly(struct vfsmount *mnt)
281{
Dave Hansen2e4b7fc2008-02-15 14:38:00 -0800282 if (mnt->mnt_flags & MNT_READONLY)
283 return 1;
284 if (mnt->mnt_sb->s_flags & MS_RDONLY)
285 return 1;
286 return 0;
Dave Hansen3d733632008-02-15 14:37:59 -0800287}
288EXPORT_SYMBOL_GPL(__mnt_is_readonly);
289
Al Viro83adc752011-11-24 22:37:54 -0500290static inline void mnt_inc_writers(struct mount *mnt)
Dave Hansen3d733632008-02-15 14:37:59 -0800291{
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000292#ifdef CONFIG_SMP
Al Viro68e8a9f2011-11-24 22:53:09 -0500293 this_cpu_inc(mnt->mnt_pcp->mnt_writers);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000294#else
Al Viro68e8a9f2011-11-24 22:53:09 -0500295 mnt->mnt_writers++;
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000296#endif
Dave Hansen3d733632008-02-15 14:37:59 -0800297}
Dave Hansen3d733632008-02-15 14:37:59 -0800298
Al Viro83adc752011-11-24 22:37:54 -0500299static inline void mnt_dec_writers(struct mount *mnt)
Dave Hansen3d733632008-02-15 14:37:59 -0800300{
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000301#ifdef CONFIG_SMP
Al Viro68e8a9f2011-11-24 22:53:09 -0500302 this_cpu_dec(mnt->mnt_pcp->mnt_writers);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000303#else
Al Viro68e8a9f2011-11-24 22:53:09 -0500304 mnt->mnt_writers--;
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000305#endif
306}
307
Al Viro83adc752011-11-24 22:37:54 -0500308static unsigned int mnt_get_writers(struct mount *mnt)
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000309{
310#ifdef CONFIG_SMP
311 unsigned int count = 0;
Dave Hansen3d733632008-02-15 14:37:59 -0800312 int cpu;
Dave Hansen3d733632008-02-15 14:37:59 -0800313
314 for_each_possible_cpu(cpu) {
Al Viro68e8a9f2011-11-24 22:53:09 -0500315 count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers;
Dave Hansen3d733632008-02-15 14:37:59 -0800316 }
Dave Hansen3d733632008-02-15 14:37:59 -0800317
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000318 return count;
319#else
320 return mnt->mnt_writers;
321#endif
Dave Hansen3d733632008-02-15 14:37:59 -0800322}
323
Miklos Szeredi4ed5e822011-11-21 12:11:31 +0100324static int mnt_is_readonly(struct vfsmount *mnt)
325{
326 if (mnt->mnt_sb->s_readonly_remount)
327 return 1;
328 /* Order wrt setting s_flags/s_readonly_remount in do_remount() */
329 smp_rmb();
330 return __mnt_is_readonly(mnt);
331}
332
Dave Hansen3d733632008-02-15 14:37:59 -0800333/*
Jan Karaeb04c282012-06-12 16:20:35 +0200334 * Most r/o & frozen checks on a fs are for operations that take discrete
335 * amounts of time, like a write() or unlink(). We must keep track of when
336 * those operations start (for permission checks) and when they end, so that we
337 * can determine when writes are able to occur to a filesystem.
Dave Hansen3d733632008-02-15 14:37:59 -0800338 */
Dave Hansen83660252008-02-15 14:37:30 -0800339/**
Jan Karaeb04c282012-06-12 16:20:35 +0200340 * __mnt_want_write - get write access to a mount without freeze protection
Al Viro83adc752011-11-24 22:37:54 -0500341 * @m: the mount on which to take a write
Dave Hansen83660252008-02-15 14:37:30 -0800342 *
Jan Karaeb04c282012-06-12 16:20:35 +0200343 * This tells the low-level filesystem that a write is about to be performed to
344 * it, and makes sure that writes are allowed (mnt it read-write) before
345 * returning success. This operation does not protect against filesystem being
346 * frozen. When the write operation is finished, __mnt_drop_write() must be
347 * called. This is effectively a refcount.
Dave Hansen83660252008-02-15 14:37:30 -0800348 */
Jan Karaeb04c282012-06-12 16:20:35 +0200349int __mnt_want_write(struct vfsmount *m)
Dave Hansen83660252008-02-15 14:37:30 -0800350{
Al Viro83adc752011-11-24 22:37:54 -0500351 struct mount *mnt = real_mount(m);
Dave Hansen3d733632008-02-15 14:37:59 -0800352 int ret = 0;
Dave Hansen3d733632008-02-15 14:37:59 -0800353
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000354 preempt_disable();
Nick Pigginc6653a82011-01-07 17:50:10 +1100355 mnt_inc_writers(mnt);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000356 /*
Nick Pigginc6653a82011-01-07 17:50:10 +1100357 * The store to mnt_inc_writers must be visible before we pass
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000358 * MNT_WRITE_HOLD loop below, so that the slowpath can see our
359 * incremented count after it has set MNT_WRITE_HOLD.
360 */
361 smp_mb();
Miao Xie1e755292012-11-16 17:23:50 +0800362 while (ACCESS_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD)
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000363 cpu_relax();
364 /*
365 * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
366 * be set to match its requirements. So we must not load that until
367 * MNT_WRITE_HOLD is cleared.
368 */
369 smp_rmb();
Miklos Szeredi4ed5e822011-11-21 12:11:31 +0100370 if (mnt_is_readonly(m)) {
Nick Pigginc6653a82011-01-07 17:50:10 +1100371 mnt_dec_writers(mnt);
Dave Hansen3d733632008-02-15 14:37:59 -0800372 ret = -EROFS;
Dave Hansen3d733632008-02-15 14:37:59 -0800373 }
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000374 preempt_enable();
Jan Karaeb04c282012-06-12 16:20:35 +0200375
376 return ret;
377}
378
379/**
380 * mnt_want_write - get write access to a mount
381 * @m: the mount on which to take a write
382 *
383 * This tells the low-level filesystem that a write is about to be performed to
384 * it, and makes sure that writes are allowed (mount is read-write, filesystem
385 * is not frozen) before returning success. When the write operation is
386 * finished, mnt_drop_write() must be called. This is effectively a refcount.
387 */
388int mnt_want_write(struct vfsmount *m)
389{
390 int ret;
391
392 sb_start_write(m->mnt_sb);
393 ret = __mnt_want_write(m);
394 if (ret)
395 sb_end_write(m->mnt_sb);
Dave Hansen3d733632008-02-15 14:37:59 -0800396 return ret;
Dave Hansen83660252008-02-15 14:37:30 -0800397}
398EXPORT_SYMBOL_GPL(mnt_want_write);
399
400/**
npiggin@suse.de96029c42009-04-26 20:25:55 +1000401 * mnt_clone_write - get write access to a mount
402 * @mnt: the mount on which to take a write
403 *
404 * This is effectively like mnt_want_write, except
405 * it must only be used to take an extra write reference
406 * on a mountpoint that we already know has a write reference
407 * on it. This allows some optimisation.
408 *
409 * After finished, mnt_drop_write must be called as usual to
410 * drop the reference.
411 */
412int mnt_clone_write(struct vfsmount *mnt)
413{
414 /* superblock may be r/o */
415 if (__mnt_is_readonly(mnt))
416 return -EROFS;
417 preempt_disable();
Al Viro83adc752011-11-24 22:37:54 -0500418 mnt_inc_writers(real_mount(mnt));
npiggin@suse.de96029c42009-04-26 20:25:55 +1000419 preempt_enable();
420 return 0;
421}
422EXPORT_SYMBOL_GPL(mnt_clone_write);
423
424/**
Jan Karaeb04c282012-06-12 16:20:35 +0200425 * __mnt_want_write_file - get write access to a file's mount
426 * @file: the file who's mount on which to take a write
427 *
428 * This is like __mnt_want_write, but it takes a file and can
429 * do some optimisations if the file is open for write already
430 */
431int __mnt_want_write_file(struct file *file)
432{
Al Viro83f936c2014-03-14 12:02:47 -0400433 if (!(file->f_mode & FMODE_WRITER))
Jan Karaeb04c282012-06-12 16:20:35 +0200434 return __mnt_want_write(file->f_path.mnt);
435 else
436 return mnt_clone_write(file->f_path.mnt);
437}
438
439/**
npiggin@suse.de96029c42009-04-26 20:25:55 +1000440 * mnt_want_write_file - get write access to a file's mount
441 * @file: the file who's mount on which to take a write
442 *
443 * This is like mnt_want_write, but it takes a file and can
444 * do some optimisations if the file is open for write already
445 */
446int mnt_want_write_file(struct file *file)
447{
Jan Karaeb04c282012-06-12 16:20:35 +0200448 int ret;
449
450 sb_start_write(file->f_path.mnt->mnt_sb);
451 ret = __mnt_want_write_file(file);
452 if (ret)
453 sb_end_write(file->f_path.mnt->mnt_sb);
454 return ret;
npiggin@suse.de96029c42009-04-26 20:25:55 +1000455}
456EXPORT_SYMBOL_GPL(mnt_want_write_file);
457
458/**
Jan Karaeb04c282012-06-12 16:20:35 +0200459 * __mnt_drop_write - give up write access to a mount
Dave Hansen83660252008-02-15 14:37:30 -0800460 * @mnt: the mount on which to give up write access
461 *
462 * Tells the low-level filesystem that we are done
463 * performing writes to it. Must be matched with
Jan Karaeb04c282012-06-12 16:20:35 +0200464 * __mnt_want_write() call above.
Dave Hansen83660252008-02-15 14:37:30 -0800465 */
Jan Karaeb04c282012-06-12 16:20:35 +0200466void __mnt_drop_write(struct vfsmount *mnt)
Dave Hansen83660252008-02-15 14:37:30 -0800467{
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000468 preempt_disable();
Al Viro83adc752011-11-24 22:37:54 -0500469 mnt_dec_writers(real_mount(mnt));
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000470 preempt_enable();
Dave Hansen83660252008-02-15 14:37:30 -0800471}
Jan Karaeb04c282012-06-12 16:20:35 +0200472
473/**
474 * mnt_drop_write - give up write access to a mount
475 * @mnt: the mount on which to give up write access
476 *
477 * Tells the low-level filesystem that we are done performing writes to it and
478 * also allows filesystem to be frozen again. Must be matched with
479 * mnt_want_write() call above.
480 */
481void mnt_drop_write(struct vfsmount *mnt)
482{
483 __mnt_drop_write(mnt);
484 sb_end_write(mnt->mnt_sb);
485}
Dave Hansen83660252008-02-15 14:37:30 -0800486EXPORT_SYMBOL_GPL(mnt_drop_write);
487
Jan Karaeb04c282012-06-12 16:20:35 +0200488void __mnt_drop_write_file(struct file *file)
489{
490 __mnt_drop_write(file->f_path.mnt);
491}
492
Al Viro2a79f172011-12-09 08:06:57 -0500493void mnt_drop_write_file(struct file *file)
494{
495 mnt_drop_write(file->f_path.mnt);
496}
497EXPORT_SYMBOL(mnt_drop_write_file);
498
Al Viro83adc752011-11-24 22:37:54 -0500499static int mnt_make_readonly(struct mount *mnt)
Dave Hansen83660252008-02-15 14:37:30 -0800500{
Dave Hansen3d733632008-02-15 14:37:59 -0800501 int ret = 0;
502
Al Viro719ea2f2013-09-29 11:24:49 -0400503 lock_mount_hash();
Al Viro83adc752011-11-24 22:37:54 -0500504 mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000505 /*
506 * After storing MNT_WRITE_HOLD, we'll read the counters. This store
507 * should be visible before we do.
508 */
509 smp_mb();
510
511 /*
512 * With writers on hold, if this value is zero, then there are
513 * definitely no active writers (although held writers may subsequently
514 * increment the count, they'll have to wait, and decrement it after
515 * seeing MNT_READONLY).
516 *
517 * It is OK to have counter incremented on one CPU and decremented on
518 * another: the sum will add up correctly. The danger would be when we
519 * sum up each counter, if we read a counter before it is incremented,
520 * but then read another CPU's count which it has been subsequently
521 * decremented from -- we would see more decrements than we should.
522 * MNT_WRITE_HOLD protects against this scenario, because
523 * mnt_want_write first increments count, then smp_mb, then spins on
524 * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
525 * we're counting up here.
526 */
Nick Pigginc6653a82011-01-07 17:50:10 +1100527 if (mnt_get_writers(mnt) > 0)
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000528 ret = -EBUSY;
529 else
Al Viro83adc752011-11-24 22:37:54 -0500530 mnt->mnt.mnt_flags |= MNT_READONLY;
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000531 /*
532 * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
533 * that become unheld will see MNT_READONLY.
534 */
535 smp_wmb();
Al Viro83adc752011-11-24 22:37:54 -0500536 mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
Al Viro719ea2f2013-09-29 11:24:49 -0400537 unlock_mount_hash();
Dave Hansen3d733632008-02-15 14:37:59 -0800538 return ret;
Dave Hansen83660252008-02-15 14:37:30 -0800539}
Dave Hansen83660252008-02-15 14:37:30 -0800540
Al Viro83adc752011-11-24 22:37:54 -0500541static void __mnt_unmake_readonly(struct mount *mnt)
Dave Hansen2e4b7fc2008-02-15 14:38:00 -0800542{
Al Viro719ea2f2013-09-29 11:24:49 -0400543 lock_mount_hash();
Al Viro83adc752011-11-24 22:37:54 -0500544 mnt->mnt.mnt_flags &= ~MNT_READONLY;
Al Viro719ea2f2013-09-29 11:24:49 -0400545 unlock_mount_hash();
Dave Hansen2e4b7fc2008-02-15 14:38:00 -0800546}
547
Miklos Szeredi4ed5e822011-11-21 12:11:31 +0100548int sb_prepare_remount_readonly(struct super_block *sb)
549{
550 struct mount *mnt;
551 int err = 0;
552
Miklos Szeredi8e8b8792011-11-21 12:11:33 +0100553 /* Racy optimization. Recheck the counter under MNT_WRITE_HOLD */
554 if (atomic_long_read(&sb->s_remove_count))
555 return -EBUSY;
556
Al Viro719ea2f2013-09-29 11:24:49 -0400557 lock_mount_hash();
Miklos Szeredi4ed5e822011-11-21 12:11:31 +0100558 list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
559 if (!(mnt->mnt.mnt_flags & MNT_READONLY)) {
560 mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
561 smp_mb();
562 if (mnt_get_writers(mnt) > 0) {
563 err = -EBUSY;
564 break;
565 }
566 }
567 }
Miklos Szeredi8e8b8792011-11-21 12:11:33 +0100568 if (!err && atomic_long_read(&sb->s_remove_count))
569 err = -EBUSY;
570
Miklos Szeredi4ed5e822011-11-21 12:11:31 +0100571 if (!err) {
572 sb->s_readonly_remount = 1;
573 smp_wmb();
574 }
575 list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
576 if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD)
577 mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
578 }
Al Viro719ea2f2013-09-29 11:24:49 -0400579 unlock_mount_hash();
Miklos Szeredi4ed5e822011-11-21 12:11:31 +0100580
581 return err;
582}
583
Al Virob105e272011-11-24 20:38:33 -0500584static void free_vfsmnt(struct mount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Daniel Rosenberg1aaf05b2016-10-26 15:29:51 -0700586 kfree(mnt->mnt.data);
Andrzej Hajdafcc139a2015-02-13 14:36:41 -0800587 kfree_const(mnt->mnt_devname);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000588#ifdef CONFIG_SMP
Al Viro68e8a9f2011-11-24 22:53:09 -0500589 free_percpu(mnt->mnt_pcp);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000590#endif
Al Virob105e272011-11-24 20:38:33 -0500591 kmem_cache_free(mnt_cache, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
David Howells8ffcb322014-01-24 12:17:54 +0000594static void delayed_free_vfsmnt(struct rcu_head *head)
595{
596 free_vfsmnt(container_of(head, struct mount, mnt_rcu));
597}
598
Al Viro48a066e2013-09-29 22:06:07 -0400599/* call under rcu_read_lock */
Al Viro294d71f2015-05-08 11:43:53 -0400600int __legitimize_mnt(struct vfsmount *bastard, unsigned seq)
Al Viro48a066e2013-09-29 22:06:07 -0400601{
602 struct mount *mnt;
603 if (read_seqretry(&mount_lock, seq))
Al Viro294d71f2015-05-08 11:43:53 -0400604 return 1;
Al Viro48a066e2013-09-29 22:06:07 -0400605 if (bastard == NULL)
Al Viro294d71f2015-05-08 11:43:53 -0400606 return 0;
Al Viro48a066e2013-09-29 22:06:07 -0400607 mnt = real_mount(bastard);
608 mnt_add_count(mnt, 1);
Al Viroe31578c2018-08-09 17:51:32 -0400609 smp_mb(); // see mntput_no_expire()
Al Viro48a066e2013-09-29 22:06:07 -0400610 if (likely(!read_seqretry(&mount_lock, seq)))
Al Viro294d71f2015-05-08 11:43:53 -0400611 return 0;
Al Viro48a066e2013-09-29 22:06:07 -0400612 if (bastard->mnt_flags & MNT_SYNC_UMOUNT) {
613 mnt_add_count(mnt, -1);
Al Viro294d71f2015-05-08 11:43:53 -0400614 return 1;
Al Viro48a066e2013-09-29 22:06:07 -0400615 }
Al Viroe31578c2018-08-09 17:51:32 -0400616 lock_mount_hash();
617 if (unlikely(bastard->mnt_flags & MNT_DOOMED)) {
618 mnt_add_count(mnt, -1);
619 unlock_mount_hash();
620 return 1;
621 }
622 unlock_mount_hash();
623 /* caller will mntput() */
Al Viro294d71f2015-05-08 11:43:53 -0400624 return -1;
625}
626
627/* call under rcu_read_lock */
628bool legitimize_mnt(struct vfsmount *bastard, unsigned seq)
629{
630 int res = __legitimize_mnt(bastard, seq);
631 if (likely(!res))
632 return true;
633 if (unlikely(res < 0)) {
634 rcu_read_unlock();
635 mntput(bastard);
636 rcu_read_lock();
637 }
Al Viro48a066e2013-09-29 22:06:07 -0400638 return false;
639}
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641/*
Al Viro474279d2013-10-01 16:11:26 -0400642 * find the first mount at @dentry on vfsmount @mnt.
Al Viro48a066e2013-09-29 22:06:07 -0400643 * call under rcu_read_lock()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 */
Al Viro474279d2013-10-01 16:11:26 -0400645struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
Al Viro38129a12014-03-20 21:10:51 -0400647 struct hlist_head *head = m_hash(mnt, dentry);
Al Viro474279d2013-10-01 16:11:26 -0400648 struct mount *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Al Viro38129a12014-03-20 21:10:51 -0400650 hlist_for_each_entry_rcu(p, head, mnt_hash)
Al Viro474279d2013-10-01 16:11:26 -0400651 if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry)
652 return p;
653 return NULL;
654}
655
656/*
David Howellsf015f1262012-06-25 12:55:28 +0100657 * lookup_mnt - Return the first child mount mounted at path
658 *
659 * "First" means first mounted chronologically. If you create the
660 * following mounts:
661 *
662 * mount /dev/sda1 /mnt
663 * mount /dev/sda2 /mnt
664 * mount /dev/sda3 /mnt
665 *
666 * Then lookup_mnt() on the base /mnt dentry in the root mount will
667 * return successively the root dentry and vfsmount of /dev/sda1, then
668 * /dev/sda2, then /dev/sda3, then NULL.
669 *
670 * lookup_mnt takes a reference to the found vfsmount.
Ram Paia05964f2005-11-07 17:20:17 -0500671 */
Al Viro1c755af2009-04-18 14:06:57 -0400672struct vfsmount *lookup_mnt(struct path *path)
Ram Paia05964f2005-11-07 17:20:17 -0500673{
Al Viroc7105362011-11-24 18:22:03 -0500674 struct mount *child_mnt;
Al Viro48a066e2013-09-29 22:06:07 -0400675 struct vfsmount *m;
676 unsigned seq;
Nick Piggin99b7db72010-08-18 04:37:39 +1000677
Al Viro48a066e2013-09-29 22:06:07 -0400678 rcu_read_lock();
679 do {
680 seq = read_seqbegin(&mount_lock);
681 child_mnt = __lookup_mnt(path->mnt, path->dentry);
682 m = child_mnt ? &child_mnt->mnt : NULL;
683 } while (!legitimize_mnt(m, seq));
684 rcu_read_unlock();
685 return m;
Ram Paia05964f2005-11-07 17:20:17 -0500686}
687
Eric W. Biederman7af13642013-10-04 19:15:13 -0700688/*
689 * __is_local_mountpoint - Test to see if dentry is a mountpoint in the
690 * current mount namespace.
691 *
692 * The common case is dentries are not mountpoints at all and that
693 * test is handled inline. For the slow case when we are actually
694 * dealing with a mountpoint of some kind, walk through all of the
695 * mounts in the current mount namespace and test to see if the dentry
696 * is a mountpoint.
697 *
698 * The mount_hashtable is not usable in the context because we
699 * need to identify all mounts that may be in the current mount
700 * namespace not just a mount that happens to have some specified
701 * parent mount.
702 */
703bool __is_local_mountpoint(struct dentry *dentry)
704{
705 struct mnt_namespace *ns = current->nsproxy->mnt_ns;
706 struct mount *mnt;
707 bool is_covered = false;
708
709 if (!d_mountpoint(dentry))
710 goto out;
711
712 down_read(&namespace_sem);
713 list_for_each_entry(mnt, &ns->list, mnt_list) {
714 is_covered = (mnt->mnt_mountpoint == dentry);
715 if (is_covered)
716 break;
717 }
718 up_read(&namespace_sem);
719out:
720 return is_covered;
721}
722
Eric W. Biedermane2dfa932014-02-24 17:32:34 -0800723static struct mountpoint *lookup_mountpoint(struct dentry *dentry)
Al Viro84d17192013-03-15 10:53:28 -0400724{
Al Viro0818bf22014-02-28 13:46:44 -0500725 struct hlist_head *chain = mp_hash(dentry);
Al Viro84d17192013-03-15 10:53:28 -0400726 struct mountpoint *mp;
727
Al Viro0818bf22014-02-28 13:46:44 -0500728 hlist_for_each_entry(mp, chain, m_hash) {
Al Viro84d17192013-03-15 10:53:28 -0400729 if (mp->m_dentry == dentry) {
730 /* might be worth a WARN_ON() */
731 if (d_unlinked(dentry))
732 return ERR_PTR(-ENOENT);
733 mp->m_count++;
734 return mp;
735 }
736 }
Eric W. Biedermane2dfa932014-02-24 17:32:34 -0800737 return NULL;
738}
739
Eric W. Biederman1a62a0f2017-01-03 14:18:43 +1300740static struct mountpoint *get_mountpoint(struct dentry *dentry)
Eric W. Biedermane2dfa932014-02-24 17:32:34 -0800741{
Eric W. Biederman1a62a0f2017-01-03 14:18:43 +1300742 struct mountpoint *mp, *new = NULL;
Eric W. Biedermane2dfa932014-02-24 17:32:34 -0800743 int ret;
Al Viro84d17192013-03-15 10:53:28 -0400744
Eric W. Biederman1a62a0f2017-01-03 14:18:43 +1300745 if (d_mountpoint(dentry)) {
746mountpoint:
747 read_seqlock_excl(&mount_lock);
748 mp = lookup_mountpoint(dentry);
749 read_sequnlock_excl(&mount_lock);
750 if (mp)
751 goto done;
Al Viro84d17192013-03-15 10:53:28 -0400752 }
Miklos Szeredieed81002013-09-05 14:39:11 +0200753
Eric W. Biederman1a62a0f2017-01-03 14:18:43 +1300754 if (!new)
755 new = kmalloc(sizeof(struct mountpoint), GFP_KERNEL);
756 if (!new)
757 return ERR_PTR(-ENOMEM);
758
759
760 /* Exactly one processes may set d_mounted */
761 ret = d_set_mounted(dentry);
762
763 /* Someone else set d_mounted? */
764 if (ret == -EBUSY)
765 goto mountpoint;
766
767 /* The dentry is not available as a mountpoint? */
768 mp = ERR_PTR(ret);
769 if (ret)
770 goto done;
771
772 /* Add the new mountpoint to the hash table */
773 read_seqlock_excl(&mount_lock);
774 new->m_dentry = dentry;
775 new->m_count = 1;
776 hlist_add_head(&new->m_hash, mp_hash(dentry));
777 INIT_HLIST_HEAD(&new->m_list);
778 read_sequnlock_excl(&mount_lock);
779
780 mp = new;
781 new = NULL;
782done:
783 kfree(new);
Al Viro84d17192013-03-15 10:53:28 -0400784 return mp;
785}
786
787static void put_mountpoint(struct mountpoint *mp)
788{
789 if (!--mp->m_count) {
790 struct dentry *dentry = mp->m_dentry;
Eric W. Biederman0a5eb7c2013-09-22 19:37:01 -0700791 BUG_ON(!hlist_empty(&mp->m_list));
Al Viro84d17192013-03-15 10:53:28 -0400792 spin_lock(&dentry->d_lock);
793 dentry->d_flags &= ~DCACHE_MOUNTED;
794 spin_unlock(&dentry->d_lock);
Al Viro0818bf22014-02-28 13:46:44 -0500795 hlist_del(&mp->m_hash);
Al Viro84d17192013-03-15 10:53:28 -0400796 kfree(mp);
797 }
798}
799
Al Viro143c8c92011-11-25 00:46:35 -0500800static inline int check_mnt(struct mount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800802 return mnt->mnt_ns == current->nsproxy->mnt_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
804
Nick Piggin99b7db72010-08-18 04:37:39 +1000805/*
806 * vfsmount lock must be held for write
807 */
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800808static void touch_mnt_namespace(struct mnt_namespace *ns)
Al Viro5addc5d2005-11-07 17:15:49 -0500809{
810 if (ns) {
811 ns->event = ++event;
812 wake_up_interruptible(&ns->poll);
813 }
814}
815
Nick Piggin99b7db72010-08-18 04:37:39 +1000816/*
817 * vfsmount lock must be held for write
818 */
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800819static void __touch_mnt_namespace(struct mnt_namespace *ns)
Al Viro5addc5d2005-11-07 17:15:49 -0500820{
821 if (ns && ns->event != event) {
822 ns->event = event;
823 wake_up_interruptible(&ns->poll);
824 }
825}
826
Nick Piggin99b7db72010-08-18 04:37:39 +1000827/*
828 * vfsmount lock must be held for write
829 */
Eric W. Biederman7bdb11d2014-12-29 13:03:41 -0600830static void unhash_mnt(struct mount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
Al Viro0714a532011-11-24 22:19:58 -0500832 mnt->mnt_parent = mnt;
Al Viroa73324d2011-11-24 22:25:07 -0500833 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
Al Viro6b41d532011-11-24 23:24:33 -0500834 list_del_init(&mnt->mnt_child);
Al Viro38129a12014-03-20 21:10:51 -0400835 hlist_del_init_rcu(&mnt->mnt_hash);
Eric W. Biederman0a5eb7c2013-09-22 19:37:01 -0700836 hlist_del_init(&mnt->mnt_mp_list);
Al Viro84d17192013-03-15 10:53:28 -0400837 put_mountpoint(mnt->mnt_mp);
838 mnt->mnt_mp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
Nick Piggin99b7db72010-08-18 04:37:39 +1000841/*
842 * vfsmount lock must be held for write
843 */
Eric W. Biederman7bdb11d2014-12-29 13:03:41 -0600844static void detach_mnt(struct mount *mnt, struct path *old_path)
845{
846 old_path->dentry = mnt->mnt_mountpoint;
847 old_path->mnt = &mnt->mnt_parent->mnt;
848 unhash_mnt(mnt);
849}
850
851/*
852 * vfsmount lock must be held for write
853 */
Eric W. Biederman6a46c572015-01-15 22:58:33 -0600854static void umount_mnt(struct mount *mnt)
855{
856 /* old mountpoint will be dropped when we can do that */
857 mnt->mnt_ex_mountpoint = mnt->mnt_mountpoint;
858 unhash_mnt(mnt);
859}
860
861/*
862 * vfsmount lock must be held for write
863 */
Al Viro84d17192013-03-15 10:53:28 -0400864void mnt_set_mountpoint(struct mount *mnt,
865 struct mountpoint *mp,
Al Viro44d964d62011-11-24 21:28:22 -0500866 struct mount *child_mnt)
Ram Paib90fa9a2005-11-07 17:19:50 -0500867{
Al Viro84d17192013-03-15 10:53:28 -0400868 mp->m_count++;
Al Viro3a2393d2011-11-25 03:19:09 -0500869 mnt_add_count(mnt, 1); /* essentially, that's mntget */
Al Viro84d17192013-03-15 10:53:28 -0400870 child_mnt->mnt_mountpoint = dget(mp->m_dentry);
Al Viro3a2393d2011-11-25 03:19:09 -0500871 child_mnt->mnt_parent = mnt;
Al Viro84d17192013-03-15 10:53:28 -0400872 child_mnt->mnt_mp = mp;
Eric W. Biederman0a5eb7c2013-09-22 19:37:01 -0700873 hlist_add_head(&child_mnt->mnt_mp_list, &mp->m_list);
Ram Paib90fa9a2005-11-07 17:19:50 -0500874}
875
Eric W. Biederman808e83e2017-01-20 18:28:35 +1300876static void __attach_mnt(struct mount *mnt, struct mount *parent)
877{
878 hlist_add_head_rcu(&mnt->mnt_hash,
879 m_hash(&parent->mnt, mnt->mnt_mountpoint));
880 list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
881}
882
Nick Piggin99b7db72010-08-18 04:37:39 +1000883/*
884 * vfsmount lock must be held for write
885 */
Al Viro84d17192013-03-15 10:53:28 -0400886static void attach_mnt(struct mount *mnt,
887 struct mount *parent,
888 struct mountpoint *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
Al Viro84d17192013-03-15 10:53:28 -0400890 mnt_set_mountpoint(parent, mp, mnt);
Eric W. Biederman808e83e2017-01-20 18:28:35 +1300891 __attach_mnt(mnt, parent);
Ram Paib90fa9a2005-11-07 17:19:50 -0500892}
893
Eric W. Biederman808e83e2017-01-20 18:28:35 +1300894void mnt_change_mountpoint(struct mount *parent, struct mountpoint *mp, struct mount *mnt)
Al Viro12a5b522014-08-10 03:44:55 -0400895{
Eric W. Biederman808e83e2017-01-20 18:28:35 +1300896 struct mountpoint *old_mp = mnt->mnt_mp;
897 struct dentry *old_mountpoint = mnt->mnt_mountpoint;
898 struct mount *old_parent = mnt->mnt_parent;
899
900 list_del_init(&mnt->mnt_child);
901 hlist_del_init(&mnt->mnt_mp_list);
902 hlist_del_init_rcu(&mnt->mnt_hash);
903
904 attach_mnt(mnt, parent, mp);
905
906 put_mountpoint(old_mp);
907
908 /*
909 * Safely avoid even the suggestion this code might sleep or
910 * lock the mount hash by taking advantage of the knowledge that
911 * mnt_change_mountpoint will not release the final reference
912 * to a mountpoint.
913 *
914 * During mounting, the mount passed in as the parent mount will
915 * continue to use the old mountpoint and during unmounting, the
916 * old mountpoint will continue to exist until namespace_unlock,
917 * which happens well after mnt_change_mountpoint.
918 */
919 spin_lock(&old_mountpoint->d_lock);
920 old_mountpoint->d_lockref.count--;
921 spin_unlock(&old_mountpoint->d_lock);
922
923 mnt_add_count(old_parent, -1);
Al Viro12a5b522014-08-10 03:44:55 -0400924}
925
Ram Paib90fa9a2005-11-07 17:19:50 -0500926/*
Nick Piggin99b7db72010-08-18 04:37:39 +1000927 * vfsmount lock must be held for write
Ram Paib90fa9a2005-11-07 17:19:50 -0500928 */
Eric W. Biederman808e83e2017-01-20 18:28:35 +1300929static void commit_tree(struct mount *mnt)
Ram Paib90fa9a2005-11-07 17:19:50 -0500930{
Al Viro0714a532011-11-24 22:19:58 -0500931 struct mount *parent = mnt->mnt_parent;
Al Viro83adc752011-11-24 22:37:54 -0500932 struct mount *m;
Ram Paib90fa9a2005-11-07 17:19:50 -0500933 LIST_HEAD(head);
Al Viro143c8c92011-11-25 00:46:35 -0500934 struct mnt_namespace *n = parent->mnt_ns;
Ram Paib90fa9a2005-11-07 17:19:50 -0500935
Al Viro0714a532011-11-24 22:19:58 -0500936 BUG_ON(parent == mnt);
Ram Paib90fa9a2005-11-07 17:19:50 -0500937
Al Viro1a4eeaf2011-11-25 02:19:55 -0500938 list_add_tail(&head, &mnt->mnt_list);
Al Virof7a99c52012-06-09 00:59:08 -0400939 list_for_each_entry(m, &head, mnt_list)
Al Viro143c8c92011-11-25 00:46:35 -0500940 m->mnt_ns = n;
Al Virof03c6592011-01-14 22:30:21 -0500941
Ram Paib90fa9a2005-11-07 17:19:50 -0500942 list_splice(&head, n->list.prev);
943
Eric W. Biedermand2921682016-09-28 00:27:17 -0500944 n->mounts += n->pending_mounts;
945 n->pending_mounts = 0;
946
Eric W. Biederman808e83e2017-01-20 18:28:35 +1300947 __attach_mnt(mnt, parent);
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800948 touch_mnt_namespace(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
950
Al Viro909b0a82011-11-25 03:06:56 -0500951static struct mount *next_mnt(struct mount *p, struct mount *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
Al Viro6b41d532011-11-24 23:24:33 -0500953 struct list_head *next = p->mnt_mounts.next;
954 if (next == &p->mnt_mounts) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 while (1) {
Al Viro909b0a82011-11-25 03:06:56 -0500956 if (p == root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 return NULL;
Al Viro6b41d532011-11-24 23:24:33 -0500958 next = p->mnt_child.next;
959 if (next != &p->mnt_parent->mnt_mounts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 break;
Al Viro0714a532011-11-24 22:19:58 -0500961 p = p->mnt_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 }
963 }
Al Viro6b41d532011-11-24 23:24:33 -0500964 return list_entry(next, struct mount, mnt_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965}
966
Al Viro315fc832011-11-24 18:57:30 -0500967static struct mount *skip_mnt_tree(struct mount *p)
Ram Pai9676f0c2005-11-07 17:21:20 -0500968{
Al Viro6b41d532011-11-24 23:24:33 -0500969 struct list_head *prev = p->mnt_mounts.prev;
970 while (prev != &p->mnt_mounts) {
971 p = list_entry(prev, struct mount, mnt_child);
972 prev = p->mnt_mounts.prev;
Ram Pai9676f0c2005-11-07 17:21:20 -0500973 }
974 return p;
975}
976
Al Viro9d412a42011-03-17 22:08:28 -0400977struct vfsmount *
978vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
979{
Al Virob105e272011-11-24 20:38:33 -0500980 struct mount *mnt;
Al Viro9d412a42011-03-17 22:08:28 -0400981 struct dentry *root;
982
983 if (!type)
984 return ERR_PTR(-ENODEV);
985
986 mnt = alloc_vfsmnt(name);
987 if (!mnt)
988 return ERR_PTR(-ENOMEM);
989
Daniel Rosenberg1aaf05b2016-10-26 15:29:51 -0700990 if (type->alloc_mnt_data) {
991 mnt->mnt.data = type->alloc_mnt_data();
992 if (!mnt->mnt.data) {
993 mnt_free_id(mnt);
994 free_vfsmnt(mnt);
995 return ERR_PTR(-ENOMEM);
996 }
997 }
Al Viro9d412a42011-03-17 22:08:28 -0400998 if (flags & MS_KERNMOUNT)
Al Virob105e272011-11-24 20:38:33 -0500999 mnt->mnt.mnt_flags = MNT_INTERNAL;
Al Viro9d412a42011-03-17 22:08:28 -04001000
Daniel Rosenberg29f888e2016-10-26 15:58:22 -07001001 root = mount_fs(type, flags, name, &mnt->mnt, data);
Al Viro9d412a42011-03-17 22:08:28 -04001002 if (IS_ERR(root)) {
David Howells8ffcb322014-01-24 12:17:54 +00001003 mnt_free_id(mnt);
Al Viro9d412a42011-03-17 22:08:28 -04001004 free_vfsmnt(mnt);
1005 return ERR_CAST(root);
1006 }
1007
Al Virob105e272011-11-24 20:38:33 -05001008 mnt->mnt.mnt_root = root;
1009 mnt->mnt.mnt_sb = root->d_sb;
Al Viroa73324d2011-11-24 22:25:07 -05001010 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
Al Viro0714a532011-11-24 22:19:58 -05001011 mnt->mnt_parent = mnt;
Al Viro719ea2f2013-09-29 11:24:49 -04001012 lock_mount_hash();
Miklos Szeredi39f7c4d2011-11-21 12:11:30 +01001013 list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts);
Al Viro719ea2f2013-09-29 11:24:49 -04001014 unlock_mount_hash();
Al Virob105e272011-11-24 20:38:33 -05001015 return &mnt->mnt;
Al Viro9d412a42011-03-17 22:08:28 -04001016}
1017EXPORT_SYMBOL_GPL(vfs_kern_mount);
1018
Eric W. Biedermand3381fa2017-02-01 06:06:16 +13001019struct vfsmount *
1020vfs_submount(const struct dentry *mountpoint, struct file_system_type *type,
1021 const char *name, void *data)
1022{
1023 /* Until it is worked out how to pass the user namespace
1024 * through from the parent mount to the submount don't support
1025 * unprivileged mounts with submounts.
1026 */
1027 if (mountpoint->d_sb->s_user_ns != &init_user_ns)
1028 return ERR_PTR(-EPERM);
1029
1030 return vfs_kern_mount(type, MS_SUBMOUNT, name, data);
1031}
1032EXPORT_SYMBOL_GPL(vfs_submount);
1033
Al Viro87129cc2011-11-24 21:24:27 -05001034static struct mount *clone_mnt(struct mount *old, struct dentry *root,
Ram Pai36341f62005-11-07 17:17:22 -05001035 int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
Al Viro87129cc2011-11-24 21:24:27 -05001037 struct super_block *sb = old->mnt.mnt_sb;
David Howellsbe34d1a2012-06-25 12:55:18 +01001038 struct mount *mnt;
1039 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
David Howellsbe34d1a2012-06-25 12:55:18 +01001041 mnt = alloc_vfsmnt(old->mnt_devname);
1042 if (!mnt)
1043 return ERR_PTR(-ENOMEM);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001044
Daniel Rosenberg1aaf05b2016-10-26 15:29:51 -07001045 if (sb->s_op->clone_mnt_data) {
1046 mnt->mnt.data = sb->s_op->clone_mnt_data(old->mnt.data);
1047 if (!mnt->mnt.data) {
1048 err = -ENOMEM;
1049 goto out_free;
1050 }
1051 }
1052
Eric W. Biederman7a472ef2012-07-31 13:13:04 -07001053 if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE))
David Howellsbe34d1a2012-06-25 12:55:18 +01001054 mnt->mnt_group_id = 0; /* not a peer of original */
1055 else
1056 mnt->mnt_group_id = old->mnt_group_id;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001057
David Howellsbe34d1a2012-06-25 12:55:18 +01001058 if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
1059 err = mnt_alloc_group_id(mnt);
1060 if (err)
1061 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 }
David Howellsbe34d1a2012-06-25 12:55:18 +01001063
Al Viro3f68e22e2018-04-19 22:03:08 -04001064 mnt->mnt.mnt_flags = old->mnt.mnt_flags;
1065 mnt->mnt.mnt_flags &= ~(MNT_WRITE_HOLD|MNT_MARKED|MNT_INTERNAL);
Eric W. Biederman132c94e2013-03-22 04:08:05 -07001066 /* Don't allow unprivileged users to change mount flags */
Eric W. Biederman9566d672014-07-28 17:26:07 -07001067 if (flag & CL_UNPRIVILEGED) {
1068 mnt->mnt.mnt_flags |= MNT_LOCK_ATIME;
1069
1070 if (mnt->mnt.mnt_flags & MNT_READONLY)
1071 mnt->mnt.mnt_flags |= MNT_LOCK_READONLY;
1072
1073 if (mnt->mnt.mnt_flags & MNT_NODEV)
1074 mnt->mnt.mnt_flags |= MNT_LOCK_NODEV;
1075
1076 if (mnt->mnt.mnt_flags & MNT_NOSUID)
1077 mnt->mnt.mnt_flags |= MNT_LOCK_NOSUID;
1078
1079 if (mnt->mnt.mnt_flags & MNT_NOEXEC)
1080 mnt->mnt.mnt_flags |= MNT_LOCK_NOEXEC;
1081 }
Eric W. Biederman132c94e2013-03-22 04:08:05 -07001082
Eric W. Biederman5ff9d8a2013-03-29 21:04:39 -07001083 /* Don't allow unprivileged users to reveal what is under a mount */
Eric W. Biederman381cacb2014-10-07 17:11:46 -07001084 if ((flag & CL_UNPRIVILEGED) &&
1085 (!(flag & CL_EXPIRE) || list_empty(&old->mnt_expire)))
Eric W. Biederman5ff9d8a2013-03-29 21:04:39 -07001086 mnt->mnt.mnt_flags |= MNT_LOCKED;
1087
David Howellsbe34d1a2012-06-25 12:55:18 +01001088 atomic_inc(&sb->s_active);
1089 mnt->mnt.mnt_sb = sb;
1090 mnt->mnt.mnt_root = dget(root);
1091 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
1092 mnt->mnt_parent = mnt;
Al Viro719ea2f2013-09-29 11:24:49 -04001093 lock_mount_hash();
David Howellsbe34d1a2012-06-25 12:55:18 +01001094 list_add_tail(&mnt->mnt_instance, &sb->s_mounts);
Al Viro719ea2f2013-09-29 11:24:49 -04001095 unlock_mount_hash();
David Howellsbe34d1a2012-06-25 12:55:18 +01001096
Eric W. Biederman7a472ef2012-07-31 13:13:04 -07001097 if ((flag & CL_SLAVE) ||
1098 ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) {
David Howellsbe34d1a2012-06-25 12:55:18 +01001099 list_add(&mnt->mnt_slave, &old->mnt_slave_list);
1100 mnt->mnt_master = old;
1101 CLEAR_MNT_SHARED(mnt);
1102 } else if (!(flag & CL_PRIVATE)) {
1103 if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
1104 list_add(&mnt->mnt_share, &old->mnt_share);
1105 if (IS_MNT_SLAVE(old))
1106 list_add(&mnt->mnt_slave, &old->mnt_slave);
1107 mnt->mnt_master = old->mnt_master;
1108 }
1109 if (flag & CL_MAKE_SHARED)
1110 set_mnt_shared(mnt);
1111
1112 /* stick the duplicate mount on the same expiry list
1113 * as the original if that was on one */
1114 if (flag & CL_EXPIRE) {
1115 if (!list_empty(&old->mnt_expire))
1116 list_add(&mnt->mnt_expire, &old->mnt_expire);
1117 }
1118
Al Virocb338d02011-11-24 20:55:08 -05001119 return mnt;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001120
1121 out_free:
David Howells8ffcb322014-01-24 12:17:54 +00001122 mnt_free_id(mnt);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001123 free_vfsmnt(mnt);
David Howellsbe34d1a2012-06-25 12:55:18 +01001124 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125}
1126
Al Viro9ea459e2014-08-08 13:08:20 -04001127static void cleanup_mnt(struct mount *mnt)
1128{
1129 /*
1130 * This probably indicates that somebody messed
1131 * up a mnt_want/drop_write() pair. If this
1132 * happens, the filesystem was probably unable
1133 * to make r/w->r/o transitions.
1134 */
1135 /*
1136 * The locking used to deal with mnt_count decrement provides barriers,
1137 * so mnt_get_writers() below is safe.
1138 */
1139 WARN_ON(mnt_get_writers(mnt));
1140 if (unlikely(mnt->mnt_pins.first))
1141 mnt_pin_kill(mnt);
1142 fsnotify_vfsmount_delete(&mnt->mnt);
1143 dput(mnt->mnt.mnt_root);
1144 deactivate_super(mnt->mnt.mnt_sb);
1145 mnt_free_id(mnt);
1146 call_rcu(&mnt->mnt_rcu, delayed_free_vfsmnt);
1147}
1148
1149static void __cleanup_mnt(struct rcu_head *head)
1150{
1151 cleanup_mnt(container_of(head, struct mount, mnt_rcu));
1152}
1153
1154static LLIST_HEAD(delayed_mntput_list);
1155static void delayed_mntput(struct work_struct *unused)
1156{
1157 struct llist_node *node = llist_del_all(&delayed_mntput_list);
1158 struct llist_node *next;
1159
1160 for (; node; node = next) {
1161 next = llist_next(node);
1162 cleanup_mnt(llist_entry(node, struct mount, mnt_llist));
1163 }
1164}
1165static DECLARE_DELAYED_WORK(delayed_mntput_work, delayed_mntput);
1166
Jaegeuk Kimd1fc63d2017-09-12 17:56:31 -07001167void flush_delayed_mntput_wait(void)
1168{
1169 delayed_mntput(NULL);
1170 flush_delayed_work(&delayed_mntput_work);
1171}
1172
Al Viro900148d2011-11-25 00:33:11 -05001173static void mntput_no_expire(struct mount *mnt)
Al Viro7b7b1ac2005-11-07 17:13:39 -05001174{
Al Viro48a066e2013-09-29 22:06:07 -04001175 rcu_read_lock();
Al Viro87a2d842018-08-09 17:21:17 -04001176 if (likely(READ_ONCE(mnt->mnt_ns))) {
1177 /*
1178 * Since we don't do lock_mount_hash() here,
1179 * ->mnt_ns can change under us. However, if it's
1180 * non-NULL, then there's a reference that won't
1181 * be dropped until after an RCU delay done after
1182 * turning ->mnt_ns NULL. So if we observe it
1183 * non-NULL under rcu_read_lock(), the reference
1184 * we are dropping is not the final one.
1185 */
1186 mnt_add_count(mnt, -1);
Al Viro48a066e2013-09-29 22:06:07 -04001187 rcu_read_unlock();
Al Virof03c6592011-01-14 22:30:21 -05001188 return;
Nick Pigginb3e19d92011-01-07 17:50:11 +11001189 }
Al Viro719ea2f2013-09-29 11:24:49 -04001190 lock_mount_hash();
Al Viroe31578c2018-08-09 17:51:32 -04001191 /*
1192 * make sure that if __legitimize_mnt() has not seen us grab
1193 * mount_lock, we'll see their refcount increment here.
1194 */
1195 smp_mb();
Al Viro87a2d842018-08-09 17:21:17 -04001196 mnt_add_count(mnt, -1);
Nick Pigginb3e19d92011-01-07 17:50:11 +11001197 if (mnt_get_count(mnt)) {
Al Viro48a066e2013-09-29 22:06:07 -04001198 rcu_read_unlock();
Al Viro719ea2f2013-09-29 11:24:49 -04001199 unlock_mount_hash();
Nick Pigginb3e19d92011-01-07 17:50:11 +11001200 return;
1201 }
Al Viro48a066e2013-09-29 22:06:07 -04001202 if (unlikely(mnt->mnt.mnt_flags & MNT_DOOMED)) {
1203 rcu_read_unlock();
1204 unlock_mount_hash();
1205 return;
1206 }
1207 mnt->mnt.mnt_flags |= MNT_DOOMED;
1208 rcu_read_unlock();
Andi Kleen962830d2012-05-08 13:32:02 +09301209
Miklos Szeredi39f7c4d2011-11-21 12:11:30 +01001210 list_del(&mnt->mnt_instance);
Eric W. Biedermance07d892014-12-23 21:37:03 -06001211
1212 if (unlikely(!list_empty(&mnt->mnt_mounts))) {
1213 struct mount *p, *tmp;
1214 list_for_each_entry_safe(p, tmp, &mnt->mnt_mounts, mnt_child) {
1215 umount_mnt(p);
1216 }
1217 }
Al Viro719ea2f2013-09-29 11:24:49 -04001218 unlock_mount_hash();
Al Viro649a7952013-09-28 12:41:25 -04001219
Al Viro9ea459e2014-08-08 13:08:20 -04001220 if (likely(!(mnt->mnt.mnt_flags & MNT_INTERNAL))) {
1221 struct task_struct *task = current;
1222 if (likely(!(task->flags & PF_KTHREAD))) {
1223 init_task_work(&mnt->mnt_rcu, __cleanup_mnt);
1224 if (!task_work_add(task, &mnt->mnt_rcu, true))
1225 return;
1226 }
1227 if (llist_add(&mnt->mnt_llist, &delayed_mntput_list))
1228 schedule_delayed_work(&delayed_mntput_work, 1);
1229 return;
1230 }
1231 cleanup_mnt(mnt);
Al Viro7b7b1ac2005-11-07 17:13:39 -05001232}
Nick Pigginb3e19d92011-01-07 17:50:11 +11001233
1234void mntput(struct vfsmount *mnt)
1235{
1236 if (mnt) {
Al Viro863d6842011-11-25 00:57:42 -05001237 struct mount *m = real_mount(mnt);
Nick Pigginb3e19d92011-01-07 17:50:11 +11001238 /* avoid cacheline pingpong, hope gcc doesn't get "smart" */
Al Viro863d6842011-11-25 00:57:42 -05001239 if (unlikely(m->mnt_expiry_mark))
1240 m->mnt_expiry_mark = 0;
1241 mntput_no_expire(m);
Nick Pigginb3e19d92011-01-07 17:50:11 +11001242 }
1243}
1244EXPORT_SYMBOL(mntput);
1245
1246struct vfsmount *mntget(struct vfsmount *mnt)
1247{
1248 if (mnt)
Al Viro83adc752011-11-24 22:37:54 -05001249 mnt_add_count(real_mount(mnt), 1);
Nick Pigginb3e19d92011-01-07 17:50:11 +11001250 return mnt;
1251}
1252EXPORT_SYMBOL(mntget);
1253
Al Viro3064c352014-08-07 09:12:31 -04001254struct vfsmount *mnt_clone_internal(struct path *path)
Al Viro7b7b1ac2005-11-07 17:13:39 -05001255{
Al Viro3064c352014-08-07 09:12:31 -04001256 struct mount *p;
1257 p = clone_mnt(real_mount(path->mnt), path->dentry, CL_PRIVATE);
1258 if (IS_ERR(p))
1259 return ERR_CAST(p);
1260 p->mnt.mnt_flags |= MNT_INTERNAL;
1261 return &p->mnt;
Al Viro7b7b1ac2005-11-07 17:13:39 -05001262}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Miklos Szeredib3b304a2008-02-08 04:21:35 -08001264static inline void mangle(struct seq_file *m, const char *s)
1265{
1266 seq_escape(m, s, " \t\n\\");
1267}
1268
1269/*
1270 * Simple .show_options callback for filesystems which don't want to
1271 * implement more complex mount option showing.
1272 *
1273 * See also save_mount_options().
1274 */
Al Viro34c80b12011-12-08 21:32:45 -05001275int generic_show_options(struct seq_file *m, struct dentry *root)
Miklos Szeredib3b304a2008-02-08 04:21:35 -08001276{
Al Viro2a32ceb2009-05-08 16:05:57 -04001277 const char *options;
1278
1279 rcu_read_lock();
Al Viro34c80b12011-12-08 21:32:45 -05001280 options = rcu_dereference(root->d_sb->s_options);
Miklos Szeredib3b304a2008-02-08 04:21:35 -08001281
1282 if (options != NULL && options[0]) {
1283 seq_putc(m, ',');
1284 mangle(m, options);
1285 }
Al Viro2a32ceb2009-05-08 16:05:57 -04001286 rcu_read_unlock();
Miklos Szeredib3b304a2008-02-08 04:21:35 -08001287
1288 return 0;
1289}
1290EXPORT_SYMBOL(generic_show_options);
1291
1292/*
1293 * If filesystem uses generic_show_options(), this function should be
1294 * called from the fill_super() callback.
1295 *
1296 * The .remount_fs callback usually needs to be handled in a special
1297 * way, to make sure, that previous options are not overwritten if the
1298 * remount fails.
1299 *
1300 * Also note, that if the filesystem's .remount_fs function doesn't
1301 * reset all options to their default value, but changes only newly
1302 * given options, then the displayed options will not reflect reality
1303 * any more.
1304 */
1305void save_mount_options(struct super_block *sb, char *options)
1306{
Al Viro2a32ceb2009-05-08 16:05:57 -04001307 BUG_ON(sb->s_options);
1308 rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL));
Miklos Szeredib3b304a2008-02-08 04:21:35 -08001309}
1310EXPORT_SYMBOL(save_mount_options);
1311
Al Viro2a32ceb2009-05-08 16:05:57 -04001312void replace_mount_options(struct super_block *sb, char *options)
1313{
1314 char *old = sb->s_options;
1315 rcu_assign_pointer(sb->s_options, options);
1316 if (old) {
1317 synchronize_rcu();
1318 kfree(old);
1319 }
1320}
1321EXPORT_SYMBOL(replace_mount_options);
1322
Miklos Szeredia1a2c402008-03-27 13:06:24 +01001323#ifdef CONFIG_PROC_FS
Al Viro0226f492011-12-06 12:21:54 -05001324/* iterator; we want it to have access to namespace_sem, thus here... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325static void *m_start(struct seq_file *m, loff_t *pos)
1326{
Yann Droneaudede1bf02015-06-30 14:57:30 -07001327 struct proc_mounts *p = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Ram Pai390c6842005-11-07 17:17:51 -05001329 down_read(&namespace_sem);
Al Viroc7999c32014-02-27 14:40:10 -05001330 if (p->cached_event == p->ns->event) {
1331 void *v = p->cached_mount;
1332 if (*pos == p->cached_index)
1333 return v;
1334 if (*pos == p->cached_index + 1) {
1335 v = seq_list_next(v, &p->ns->list, &p->cached_index);
1336 return p->cached_mount = v;
1337 }
1338 }
1339
1340 p->cached_event = p->ns->event;
1341 p->cached_mount = seq_list_start(&p->ns->list, *pos);
1342 p->cached_index = *pos;
1343 return p->cached_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344}
1345
1346static void *m_next(struct seq_file *m, void *v, loff_t *pos)
1347{
Yann Droneaudede1bf02015-06-30 14:57:30 -07001348 struct proc_mounts *p = m->private;
Pavel Emelianovb0765fb2007-07-15 23:39:55 -07001349
Al Viroc7999c32014-02-27 14:40:10 -05001350 p->cached_mount = seq_list_next(v, &p->ns->list, pos);
1351 p->cached_index = *pos;
1352 return p->cached_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353}
1354
1355static void m_stop(struct seq_file *m, void *v)
1356{
Ram Pai390c6842005-11-07 17:17:51 -05001357 up_read(&namespace_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358}
1359
Al Viro0226f492011-12-06 12:21:54 -05001360static int m_show(struct seq_file *m, void *v)
Al Viro9f5596a2010-02-05 00:40:25 -05001361{
Yann Droneaudede1bf02015-06-30 14:57:30 -07001362 struct proc_mounts *p = m->private;
Al Viro1a4eeaf2011-11-25 02:19:55 -05001363 struct mount *r = list_entry(v, struct mount, mnt_list);
Al Viro0226f492011-12-06 12:21:54 -05001364 return p->show(m, &r->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365}
1366
Miklos Szeredia1a2c402008-03-27 13:06:24 +01001367const struct seq_operations mounts_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 .start = m_start,
1369 .next = m_next,
1370 .stop = m_stop,
Al Viro0226f492011-12-06 12:21:54 -05001371 .show = m_show,
Chuck Leverb4629fe2006-03-20 13:44:12 -05001372};
Miklos Szeredia1a2c402008-03-27 13:06:24 +01001373#endif /* CONFIG_PROC_FS */
Chuck Leverb4629fe2006-03-20 13:44:12 -05001374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375/**
1376 * may_umount_tree - check if a mount tree is busy
1377 * @mnt: root of mount tree
1378 *
1379 * This is called to check if a tree of mounts has any
1380 * open files, pwds, chroots or sub mounts that are
1381 * busy.
1382 */
Al Viro909b0a82011-11-25 03:06:56 -05001383int may_umount_tree(struct vfsmount *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
Al Viro909b0a82011-11-25 03:06:56 -05001385 struct mount *mnt = real_mount(m);
Ram Pai36341f62005-11-07 17:17:22 -05001386 int actual_refs = 0;
1387 int minimum_refs = 0;
Al Viro315fc832011-11-24 18:57:30 -05001388 struct mount *p;
Al Viro909b0a82011-11-25 03:06:56 -05001389 BUG_ON(!m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Nick Pigginb3e19d92011-01-07 17:50:11 +11001391 /* write lock needed for mnt_get_count */
Al Viro719ea2f2013-09-29 11:24:49 -04001392 lock_mount_hash();
Al Viro909b0a82011-11-25 03:06:56 -05001393 for (p = mnt; p; p = next_mnt(p, mnt)) {
Al Viro83adc752011-11-24 22:37:54 -05001394 actual_refs += mnt_get_count(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 minimum_refs += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
Al Viro719ea2f2013-09-29 11:24:49 -04001397 unlock_mount_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 if (actual_refs > minimum_refs)
Ian Kente3474a82006-03-27 01:14:51 -08001400 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Ian Kente3474a82006-03-27 01:14:51 -08001402 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403}
1404
1405EXPORT_SYMBOL(may_umount_tree);
1406
1407/**
1408 * may_umount - check if a mount point is busy
1409 * @mnt: root of mount
1410 *
1411 * This is called to check if a mount point has any
1412 * open files, pwds, chroots or sub mounts. If the
1413 * mount has sub mounts this will return busy
1414 * regardless of whether the sub mounts are busy.
1415 *
1416 * Doesn't take quota and stuff into account. IOW, in some cases it will
1417 * give false negatives. The main reason why it's here is that we need
1418 * a non-destructive way to look for easily umountable filesystems.
1419 */
1420int may_umount(struct vfsmount *mnt)
1421{
Ian Kente3474a82006-03-27 01:14:51 -08001422 int ret = 1;
Al Viro8ad08d82010-01-16 12:56:08 -05001423 down_read(&namespace_sem);
Al Viro719ea2f2013-09-29 11:24:49 -04001424 lock_mount_hash();
Al Viro1ab59732011-11-24 21:35:16 -05001425 if (propagate_mount_busy(real_mount(mnt), 2))
Ian Kente3474a82006-03-27 01:14:51 -08001426 ret = 0;
Al Viro719ea2f2013-09-29 11:24:49 -04001427 unlock_mount_hash();
Al Viro8ad08d82010-01-16 12:56:08 -05001428 up_read(&namespace_sem);
Ram Paia05964f2005-11-07 17:20:17 -05001429 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430}
1431
1432EXPORT_SYMBOL(may_umount);
1433
Al Viro38129a12014-03-20 21:10:51 -04001434static HLIST_HEAD(unmounted); /* protected by namespace_sem */
Al Viroe3197d82013-03-16 14:35:16 -04001435
Al Viro97216be2013-03-16 15:12:40 -04001436static void namespace_unlock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
Eric W. Biedermana3b3c562015-04-02 20:33:53 -05001438 struct hlist_head head;
Al Viro97216be2013-03-16 15:12:40 -04001439
Eric W. Biedermana3b3c562015-04-02 20:33:53 -05001440 hlist_move_list(&unmounted, &head);
Al Viro97216be2013-03-16 15:12:40 -04001441
Al Viro97216be2013-03-16 15:12:40 -04001442 up_write(&namespace_sem);
1443
Eric W. Biedermana3b3c562015-04-02 20:33:53 -05001444 if (likely(hlist_empty(&head)))
1445 return;
1446
Al Viro48a066e2013-09-29 22:06:07 -04001447 synchronize_rcu();
1448
Al Viro87b95ce2015-01-10 19:01:08 -05001449 group_pin_kill(&head);
Ram Pai70fbcdf2005-11-07 17:17:04 -05001450}
1451
Al Viro97216be2013-03-16 15:12:40 -04001452static inline void namespace_lock(void)
Al Viroe3197d82013-03-16 14:35:16 -04001453{
Al Viro97216be2013-03-16 15:12:40 -04001454 down_write(&namespace_sem);
Al Viroe3197d82013-03-16 14:35:16 -04001455}
1456
Eric W. Biedermane819f152014-12-24 07:20:01 -06001457enum umount_tree_flags {
1458 UMOUNT_SYNC = 1,
1459 UMOUNT_PROPAGATE = 2,
Eric W. Biedermane0c9c0a2015-04-01 18:30:06 -05001460 UMOUNT_CONNECTED = 4,
Eric W. Biedermane819f152014-12-24 07:20:01 -06001461};
Eric W. Biedermanf2d0a122015-07-17 14:15:30 -05001462
1463static bool disconnect_mount(struct mount *mnt, enum umount_tree_flags how)
1464{
1465 /* Leaving mounts connected is only valid for lazy umounts */
1466 if (how & UMOUNT_SYNC)
1467 return true;
1468
1469 /* A mount without a parent has nothing to be connected to */
1470 if (!mnt_has_parent(mnt))
1471 return true;
1472
1473 /* Because the reference counting rules change when mounts are
1474 * unmounted and connected, umounted mounts may not be
1475 * connected to mounted mounts.
1476 */
1477 if (!(mnt->mnt_parent->mnt.mnt_flags & MNT_UMOUNT))
1478 return true;
1479
1480 /* Has it been requested that the mount remain connected? */
1481 if (how & UMOUNT_CONNECTED)
1482 return false;
1483
1484 /* Is the mount locked such that it needs to remain connected? */
1485 if (IS_MNT_LOCKED(mnt))
1486 return false;
1487
1488 /* By default disconnect the mount */
1489 return true;
1490}
1491
Nick Piggin99b7db72010-08-18 04:37:39 +10001492/*
Al Viro48a066e2013-09-29 22:06:07 -04001493 * mount_lock must be held
Nick Piggin99b7db72010-08-18 04:37:39 +10001494 * namespace_sem must be held for write
1495 */
Eric W. Biedermane819f152014-12-24 07:20:01 -06001496static void umount_tree(struct mount *mnt, enum umount_tree_flags how)
Ram Pai70fbcdf2005-11-07 17:17:04 -05001497{
Eric W. Biedermanc003b262014-12-18 13:10:48 -06001498 LIST_HEAD(tmp_list);
Al Viro315fc832011-11-24 18:57:30 -05001499 struct mount *p;
Ram Pai70fbcdf2005-11-07 17:17:04 -05001500
Eric W. Biederman5d884572015-01-03 05:39:35 -06001501 if (how & UMOUNT_PROPAGATE)
1502 propagate_mount_unlock(mnt);
1503
Eric W. Biedermanc003b262014-12-18 13:10:48 -06001504 /* Gather the mounts to umount */
Eric W. Biederman590ce4b2014-12-22 18:30:08 -06001505 for (p = mnt; p; p = next_mnt(p, mnt)) {
1506 p->mnt.mnt_flags |= MNT_UMOUNT;
Eric W. Biedermanc003b262014-12-18 13:10:48 -06001507 list_move(&p->mnt_list, &tmp_list);
Eric W. Biederman590ce4b2014-12-22 18:30:08 -06001508 }
Eric W. Biedermanc003b262014-12-18 13:10:48 -06001509
Eric W. Biederman411a9382014-12-22 19:12:07 -06001510 /* Hide the mounts from mnt_mounts */
Eric W. Biedermanc003b262014-12-18 13:10:48 -06001511 list_for_each_entry(p, &tmp_list, mnt_list) {
Eric W. Biedermanc003b262014-12-18 13:10:48 -06001512 list_del_init(&p->mnt_child);
Al Viro38129a12014-03-20 21:10:51 -04001513 }
Ram Pai70fbcdf2005-11-07 17:17:04 -05001514
Eric W. Biedermanc003b262014-12-18 13:10:48 -06001515 /* Add propogated mounts to the tmp_list */
Eric W. Biedermane819f152014-12-24 07:20:01 -06001516 if (how & UMOUNT_PROPAGATE)
Al Viro7b8a53f2011-01-15 20:08:44 -05001517 propagate_umount(&tmp_list);
Ram Paia05964f2005-11-07 17:20:17 -05001518
Eric W. Biedermanc003b262014-12-18 13:10:48 -06001519 while (!list_empty(&tmp_list)) {
Eric W. Biedermand2921682016-09-28 00:27:17 -05001520 struct mnt_namespace *ns;
Eric W. Biedermance07d892014-12-23 21:37:03 -06001521 bool disconnect;
Eric W. Biedermanc003b262014-12-18 13:10:48 -06001522 p = list_first_entry(&tmp_list, struct mount, mnt_list);
Al Viro6776db3d2011-11-25 00:22:05 -05001523 list_del_init(&p->mnt_expire);
Al Viro1a4eeaf2011-11-25 02:19:55 -05001524 list_del_init(&p->mnt_list);
Eric W. Biedermand2921682016-09-28 00:27:17 -05001525 ns = p->mnt_ns;
1526 if (ns) {
1527 ns->mounts--;
1528 __touch_mnt_namespace(ns);
1529 }
Al Viro143c8c92011-11-25 00:46:35 -05001530 p->mnt_ns = NULL;
Eric W. Biedermane819f152014-12-24 07:20:01 -06001531 if (how & UMOUNT_SYNC)
Al Viro48a066e2013-09-29 22:06:07 -04001532 p->mnt.mnt_flags |= MNT_SYNC_UMOUNT;
Al Viro87b95ce2015-01-10 19:01:08 -05001533
Eric W. Biedermanf2d0a122015-07-17 14:15:30 -05001534 disconnect = disconnect_mount(p, how);
Eric W. Biedermance07d892014-12-23 21:37:03 -06001535
1536 pin_insert_group(&p->mnt_umount, &p->mnt_parent->mnt,
1537 disconnect ? &unmounted : NULL);
Al Viro676da582011-11-24 21:47:05 -05001538 if (mnt_has_parent(p)) {
Al Viro81b6b062014-08-30 18:32:05 -04001539 mnt_add_count(p->mnt_parent, -1);
Eric W. Biedermance07d892014-12-23 21:37:03 -06001540 if (!disconnect) {
1541 /* Don't forget about p */
1542 list_add_tail(&p->mnt_child, &p->mnt_parent->mnt_mounts);
1543 } else {
1544 umount_mnt(p);
1545 }
Al Viro7c4b93d2008-03-21 23:59:49 -04001546 }
Al Viro0f0afb12011-11-24 20:43:10 -05001547 change_mnt_propagation(p, MS_PRIVATE);
Al Viro38129a12014-03-20 21:10:51 -04001548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549}
1550
Al Virob54b9be2013-03-16 14:39:34 -04001551static void shrink_submounts(struct mount *mnt);
Al Viroc35038b2008-03-22 00:46:23 -04001552
Al Viro1ab59732011-11-24 21:35:16 -05001553static int do_umount(struct mount *mnt, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
Al Viro1ab59732011-11-24 21:35:16 -05001555 struct super_block *sb = mnt->mnt.mnt_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 int retval;
1557
Al Viro1ab59732011-11-24 21:35:16 -05001558 retval = security_sb_umount(&mnt->mnt, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 if (retval)
1560 return retval;
1561
1562 /*
1563 * Allow userspace to request a mountpoint be expired rather than
1564 * unmounting unconditionally. Unmount only happens if:
1565 * (1) the mark is already set (the mark is cleared by mntput())
1566 * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
1567 */
1568 if (flags & MNT_EXPIRE) {
Al Viro1ab59732011-11-24 21:35:16 -05001569 if (&mnt->mnt == current->fs->root.mnt ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 flags & (MNT_FORCE | MNT_DETACH))
1571 return -EINVAL;
1572
Nick Pigginb3e19d92011-01-07 17:50:11 +11001573 /*
1574 * probably don't strictly need the lock here if we examined
1575 * all race cases, but it's a slowpath.
1576 */
Al Viro719ea2f2013-09-29 11:24:49 -04001577 lock_mount_hash();
Al Viro83adc752011-11-24 22:37:54 -05001578 if (mnt_get_count(mnt) != 2) {
Al Viro719ea2f2013-09-29 11:24:49 -04001579 unlock_mount_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 return -EBUSY;
Nick Pigginb3e19d92011-01-07 17:50:11 +11001581 }
Al Viro719ea2f2013-09-29 11:24:49 -04001582 unlock_mount_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Al Viro863d6842011-11-25 00:57:42 -05001584 if (!xchg(&mnt->mnt_expiry_mark, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 return -EAGAIN;
1586 }
1587
1588 /*
1589 * If we may have to abort operations to get out of this
1590 * mount, and they will themselves hold resources we must
1591 * allow the fs to do things. In the Unix tradition of
1592 * 'Gee thats tricky lets do it in userspace' the umount_begin
1593 * might fail to complete on the first run through as other tasks
1594 * must return, and the like. Thats for the mount program to worry
1595 * about for the moment.
1596 */
1597
Al Viro42faad92008-04-24 07:21:56 -04001598 if (flags & MNT_FORCE && sb->s_op->umount_begin) {
Al Viro42faad92008-04-24 07:21:56 -04001599 sb->s_op->umount_begin(sb);
Al Viro42faad92008-04-24 07:21:56 -04001600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
1602 /*
1603 * No sense to grab the lock for this test, but test itself looks
1604 * somewhat bogus. Suggestions for better replacement?
1605 * Ho-hum... In principle, we might treat that as umount + switch
1606 * to rootfs. GC would eventually take care of the old vfsmount.
1607 * Actually it makes sense, especially if rootfs would contain a
1608 * /reboot - static binary that would close all descriptors and
1609 * call reboot(9). Then init(8) could umount root and exec /reboot.
1610 */
Al Viro1ab59732011-11-24 21:35:16 -05001611 if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 /*
1613 * Special case for "unmounting" root ...
1614 * we just try to remount it readonly.
1615 */
Andy Lutomirskia1480dc2014-10-08 12:32:47 -07001616 if (!capable(CAP_SYS_ADMIN))
1617 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 down_write(&sb->s_umount);
Al Viro4aa98cf2009-05-08 13:36:58 -04001619 if (!(sb->s_flags & MS_RDONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 up_write(&sb->s_umount);
1622 return retval;
1623 }
1624
Al Viro97216be2013-03-16 15:12:40 -04001625 namespace_lock();
Al Viro719ea2f2013-09-29 11:24:49 -04001626 lock_mount_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Eric W. Biedermanfa14e9b2018-10-22 10:21:38 -05001628 /* Recheck MNT_LOCKED with the locks held */
1629 retval = -EINVAL;
1630 if (mnt->mnt.mnt_flags & MNT_LOCKED)
1631 goto out;
1632
1633 event++;
Al Viro48a066e2013-09-29 22:06:07 -04001634 if (flags & MNT_DETACH) {
Al Viro1a4eeaf2011-11-25 02:19:55 -05001635 if (!list_empty(&mnt->mnt_list))
Eric W. Biedermane819f152014-12-24 07:20:01 -06001636 umount_tree(mnt, UMOUNT_PROPAGATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 retval = 0;
Al Viro48a066e2013-09-29 22:06:07 -04001638 } else {
1639 shrink_submounts(mnt);
1640 retval = -EBUSY;
1641 if (!propagate_mount_busy(mnt, 2)) {
1642 if (!list_empty(&mnt->mnt_list))
Eric W. Biedermane819f152014-12-24 07:20:01 -06001643 umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
Al Viro48a066e2013-09-29 22:06:07 -04001644 retval = 0;
1645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 }
Eric W. Biedermanfa14e9b2018-10-22 10:21:38 -05001647out:
Al Viro719ea2f2013-09-29 11:24:49 -04001648 unlock_mount_hash();
Al Viroe3197d82013-03-16 14:35:16 -04001649 namespace_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 return retval;
1651}
1652
Eric W. Biederman80b5dce2013-10-03 01:31:18 -07001653/*
1654 * __detach_mounts - lazily unmount all mounts on the specified dentry
1655 *
1656 * During unlink, rmdir, and d_drop it is possible to loose the path
1657 * to an existing mountpoint, and wind up leaking the mount.
1658 * detach_mounts allows lazily unmounting those mounts instead of
1659 * leaking them.
1660 *
1661 * The caller may hold dentry->d_inode->i_mutex.
1662 */
1663void __detach_mounts(struct dentry *dentry)
1664{
1665 struct mountpoint *mp;
1666 struct mount *mnt;
1667
1668 namespace_lock();
Eric W. Biederman1a62a0f2017-01-03 14:18:43 +13001669 lock_mount_hash();
Eric W. Biederman80b5dce2013-10-03 01:31:18 -07001670 mp = lookup_mountpoint(dentry);
Eric W. Biedermanf53e5792015-01-19 11:48:45 -06001671 if (IS_ERR_OR_NULL(mp))
Eric W. Biederman80b5dce2013-10-03 01:31:18 -07001672 goto out_unlock;
1673
Andrey Ulanove06b9332016-04-15 14:24:41 -07001674 event++;
Eric W. Biederman80b5dce2013-10-03 01:31:18 -07001675 while (!hlist_empty(&mp->m_list)) {
1676 mnt = hlist_entry(mp->m_list.first, struct mount, mnt_mp_list);
Eric W. Biedermance07d892014-12-23 21:37:03 -06001677 if (mnt->mnt.mnt_flags & MNT_UMOUNT) {
Eric W. Biedermanfe78fcc2015-07-17 14:54:27 -05001678 hlist_add_head(&mnt->mnt_umount.s_list, &unmounted);
1679 umount_mnt(mnt);
Eric W. Biedermance07d892014-12-23 21:37:03 -06001680 }
Eric W. Biedermane0c9c0a2015-04-01 18:30:06 -05001681 else umount_tree(mnt, UMOUNT_CONNECTED);
Eric W. Biederman80b5dce2013-10-03 01:31:18 -07001682 }
Eric W. Biederman80b5dce2013-10-03 01:31:18 -07001683 put_mountpoint(mp);
1684out_unlock:
Eric W. Biederman1a62a0f2017-01-03 14:18:43 +13001685 unlock_mount_hash();
Eric W. Biederman80b5dce2013-10-03 01:31:18 -07001686 namespace_unlock();
1687}
1688
Al Viro9b40bc92013-02-22 22:45:42 -05001689/*
1690 * Is the caller allowed to modify his namespace?
1691 */
1692static inline bool may_mount(void)
1693{
1694 return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN);
1695}
1696
Jeff Laytonc5e63102019-08-15 15:21:17 -04001697#ifdef CONFIG_MANDATORY_FILE_LOCKING
Jeff Layton0febcf92021-08-20 09:29:50 -04001698static bool may_mandlock(void)
Jeff Layton9e8925b2015-11-16 09:49:34 -05001699{
Jeff Layton0febcf92021-08-20 09:29:50 -04001700 pr_warn_once("======================================================\n"
1701 "WARNING: the mand mount option is being deprecated and\n"
1702 " will be removed in v5.15!\n"
1703 "======================================================\n");
Eric W. Biederman95ace752015-11-11 17:22:33 -06001704 return capable(CAP_SYS_ADMIN);
Jeff Layton9e8925b2015-11-16 09:49:34 -05001705}
Jeff Laytonc5e63102019-08-15 15:21:17 -04001706#else
1707static inline bool may_mandlock(void)
1708{
1709 pr_warn("VFS: \"mand\" mount option not supported");
1710 return false;
1711}
1712#endif
Jeff Layton9e8925b2015-11-16 09:49:34 -05001713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714/*
1715 * Now umount can handle mount points as well as block devices.
1716 * This is important for filesystems which use unnamed block devices.
1717 *
1718 * We now support a flag for forced unmount like the other 'big iron'
1719 * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
1720 */
1721
Heiko Carstensbdc480e2009-01-14 14:14:12 +01001722SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723{
Al Viro2d8f3032008-07-22 09:59:21 -04001724 struct path path;
Al Viro900148d2011-11-25 00:33:11 -05001725 struct mount *mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 int retval;
Miklos Szeredidb1f05b2010-02-10 12:15:53 +01001727 int lookup_flags = 0;
Jaegeuk Kimd1fc63d2017-09-12 17:56:31 -07001728 bool user_request = !(current->flags & PF_KTHREAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Miklos Szeredidb1f05b2010-02-10 12:15:53 +01001730 if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
1731 return -EINVAL;
1732
Al Viro9b40bc92013-02-22 22:45:42 -05001733 if (!may_mount())
1734 return -EPERM;
1735
Miklos Szeredidb1f05b2010-02-10 12:15:53 +01001736 if (!(flags & UMOUNT_NOFOLLOW))
1737 lookup_flags |= LOOKUP_FOLLOW;
1738
Al Viro197df042013-09-08 14:03:27 -04001739 retval = user_path_mountpoint_at(AT_FDCWD, name, lookup_flags, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 if (retval)
1741 goto out;
Al Viro900148d2011-11-25 00:33:11 -05001742 mnt = real_mount(path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 retval = -EINVAL;
Al Viro2d8f3032008-07-22 09:59:21 -04001744 if (path.dentry != path.mnt->mnt_root)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 goto dput_and_out;
Al Viro143c8c92011-11-25 00:46:35 -05001746 if (!check_mnt(mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 goto dput_and_out;
Eric W. Biedermanfa14e9b2018-10-22 10:21:38 -05001748 if (mnt->mnt.mnt_flags & MNT_LOCKED) /* Check optimistically */
Eric W. Biederman5ff9d8a2013-03-29 21:04:39 -07001749 goto dput_and_out;
Eric W. Biedermanb2f5d4d2014-10-04 14:44:03 -07001750 retval = -EPERM;
1751 if (flags & MNT_FORCE && !capable(CAP_SYS_ADMIN))
1752 goto dput_and_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Jaegeuk Kimd1fc63d2017-09-12 17:56:31 -07001754 /* flush delayed_fput to put mnt_count */
1755 if (user_request)
1756 flush_delayed_fput_wait();
1757
Al Viro900148d2011-11-25 00:33:11 -05001758 retval = do_umount(mnt, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759dput_and_out:
Jan Blunck429731b2008-02-14 19:34:31 -08001760 /* we mustn't call path_put() as that would clear mnt_expiry_mark */
Al Viro2d8f3032008-07-22 09:59:21 -04001761 dput(path.dentry);
Sayali Lokhandefb132ce2020-01-09 14:45:19 +05301762 if (user_request && (!retval || (flags & MNT_FORCE))) {
1763 /* filesystem needs to handle unclosed namespaces */
1764 if (mnt->mnt.mnt_sb->s_op->umount_end)
1765 mnt->mnt.mnt_sb->s_op->umount_end(mnt->mnt.mnt_sb,
1766 flags);
1767 }
Al Viro900148d2011-11-25 00:33:11 -05001768 mntput_no_expire(mnt);
Jaegeuk Kimd1fc63d2017-09-12 17:56:31 -07001769
1770 if (!user_request)
1771 goto out;
1772
1773 if (!retval) {
1774 /*
1775 * If the last delayed_fput() is called during do_umount()
1776 * and makes mnt_count zero, we need to guarantee to register
1777 * delayed_mntput by waiting for delayed_fput work again.
1778 */
1779 flush_delayed_fput_wait();
1780
1781 /* flush delayed_mntput_work to put sb->s_active */
1782 flush_delayed_mntput_wait();
1783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784out:
1785 return retval;
1786}
1787
1788#ifdef __ARCH_WANT_SYS_OLDUMOUNT
1789
1790/*
Ram Paib58fed82005-11-07 17:16:09 -05001791 * The 2.0 compatible umount. No flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 */
Heiko Carstensbdc480e2009-01-14 14:14:12 +01001793SYSCALL_DEFINE1(oldumount, char __user *, name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794{
Ram Paib58fed82005-11-07 17:16:09 -05001795 return sys_umount(name, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796}
1797
1798#endif
1799
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07001800static bool is_mnt_ns_file(struct dentry *dentry)
Eric W. Biederman8823c072010-03-07 18:49:36 -08001801{
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07001802 /* Is this a proxy for a mount namespace? */
Al Viroe149ed22014-11-01 10:57:28 -04001803 return dentry->d_op == &ns_dentry_operations &&
1804 dentry->d_fsdata == &mntns_operations;
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07001805}
1806
Al Viro58be28252014-11-01 00:00:23 -04001807struct mnt_namespace *to_mnt_ns(struct ns_common *ns)
1808{
1809 return container_of(ns, struct mnt_namespace, ns);
1810}
1811
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07001812static bool mnt_ns_loop(struct dentry *dentry)
1813{
1814 /* Could bind mounting the mount namespace inode cause a
1815 * mount namespace loop?
1816 */
1817 struct mnt_namespace *mnt_ns;
1818 if (!is_mnt_ns_file(dentry))
1819 return false;
1820
Al Virof77c8012014-11-01 03:13:17 -04001821 mnt_ns = to_mnt_ns(get_proc_ns(dentry->d_inode));
Eric W. Biederman8823c072010-03-07 18:49:36 -08001822 return current->nsproxy->mnt_ns->seq >= mnt_ns->seq;
1823}
1824
Al Viro87129cc2011-11-24 21:24:27 -05001825struct mount *copy_tree(struct mount *mnt, struct dentry *dentry,
Ram Pai36341f62005-11-07 17:17:22 -05001826 int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827{
Al Viro84d17192013-03-15 10:53:28 -04001828 struct mount *res, *p, *q, *r, *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07001830 if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt))
1831 return ERR_PTR(-EINVAL);
1832
1833 if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry))
David Howellsbe34d1a2012-06-25 12:55:18 +01001834 return ERR_PTR(-EINVAL);
Ram Pai9676f0c2005-11-07 17:21:20 -05001835
Ram Pai36341f62005-11-07 17:17:22 -05001836 res = q = clone_mnt(mnt, dentry, flag);
David Howellsbe34d1a2012-06-25 12:55:18 +01001837 if (IS_ERR(q))
1838 return q;
1839
Al Viroa73324d2011-11-24 22:25:07 -05001840 q->mnt_mountpoint = mnt->mnt_mountpoint;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
1842 p = mnt;
Al Viro6b41d532011-11-24 23:24:33 -05001843 list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
Al Viro315fc832011-11-24 18:57:30 -05001844 struct mount *s;
Jan Blunck7ec02ef2008-04-29 00:59:40 -07001845 if (!is_subdir(r->mnt_mountpoint, dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 continue;
1847
Al Viro909b0a82011-11-25 03:06:56 -05001848 for (s = r; s; s = next_mnt(s, r)) {
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07001849 if (!(flag & CL_COPY_UNBINDABLE) &&
1850 IS_MNT_UNBINDABLE(s)) {
Eric W. Biedermand7565182018-10-25 09:04:18 -05001851 if (s->mnt.mnt_flags & MNT_LOCKED) {
1852 /* Both unbindable and locked. */
1853 q = ERR_PTR(-EPERM);
1854 goto out;
1855 } else {
1856 s = skip_mnt_tree(s);
1857 continue;
1858 }
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07001859 }
1860 if (!(flag & CL_COPY_MNT_NS_FILE) &&
1861 is_mnt_ns_file(s->mnt.mnt_root)) {
Ram Pai9676f0c2005-11-07 17:21:20 -05001862 s = skip_mnt_tree(s);
1863 continue;
1864 }
Al Viro0714a532011-11-24 22:19:58 -05001865 while (p != s->mnt_parent) {
1866 p = p->mnt_parent;
1867 q = q->mnt_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 }
Al Viro87129cc2011-11-24 21:24:27 -05001869 p = s;
Al Viro84d17192013-03-15 10:53:28 -04001870 parent = q;
Al Viro87129cc2011-11-24 21:24:27 -05001871 q = clone_mnt(p, p->mnt.mnt_root, flag);
David Howellsbe34d1a2012-06-25 12:55:18 +01001872 if (IS_ERR(q))
1873 goto out;
Al Viro719ea2f2013-09-29 11:24:49 -04001874 lock_mount_hash();
Al Viro1a4eeaf2011-11-25 02:19:55 -05001875 list_add_tail(&q->mnt_list, &res->mnt_list);
Eric W. Biederman808e83e2017-01-20 18:28:35 +13001876 attach_mnt(q, parent, p->mnt_mp);
Al Viro719ea2f2013-09-29 11:24:49 -04001877 unlock_mount_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 }
1879 }
1880 return res;
David Howellsbe34d1a2012-06-25 12:55:18 +01001881out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 if (res) {
Al Viro719ea2f2013-09-29 11:24:49 -04001883 lock_mount_hash();
Eric W. Biedermane819f152014-12-24 07:20:01 -06001884 umount_tree(res, UMOUNT_SYNC);
Al Viro719ea2f2013-09-29 11:24:49 -04001885 unlock_mount_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 }
David Howellsbe34d1a2012-06-25 12:55:18 +01001887 return q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888}
1889
David Howellsbe34d1a2012-06-25 12:55:18 +01001890/* Caller should check returned pointer for errors */
1891
Al Viro589ff872009-04-18 03:28:19 -04001892struct vfsmount *collect_mounts(struct path *path)
Al Viro8aec0802007-06-07 12:20:32 -04001893{
Al Virocb338d02011-11-24 20:55:08 -05001894 struct mount *tree;
Al Viro97216be2013-03-16 15:12:40 -04001895 namespace_lock();
Eric W. Biedermancd4a4012015-01-07 14:28:26 -06001896 if (!check_mnt(real_mount(path->mnt)))
1897 tree = ERR_PTR(-EINVAL);
1898 else
1899 tree = copy_tree(real_mount(path->mnt), path->dentry,
1900 CL_COPY_ALL | CL_PRIVATE);
Al Viro328e6d92013-03-16 14:49:45 -04001901 namespace_unlock();
David Howellsbe34d1a2012-06-25 12:55:18 +01001902 if (IS_ERR(tree))
Dan Carpenter52e220d2013-08-14 12:44:39 +03001903 return ERR_CAST(tree);
David Howellsbe34d1a2012-06-25 12:55:18 +01001904 return &tree->mnt;
Al Viro8aec0802007-06-07 12:20:32 -04001905}
1906
1907void drop_collected_mounts(struct vfsmount *mnt)
1908{
Al Viro97216be2013-03-16 15:12:40 -04001909 namespace_lock();
Al Viro719ea2f2013-09-29 11:24:49 -04001910 lock_mount_hash();
Eric W. Biederman03004f42018-10-25 12:05:11 -05001911 umount_tree(real_mount(mnt), 0);
Al Viro719ea2f2013-09-29 11:24:49 -04001912 unlock_mount_hash();
Al Viro3ab6abe2013-03-16 14:42:19 -04001913 namespace_unlock();
Al Viro8aec0802007-06-07 12:20:32 -04001914}
1915
Miklos Szeredie3eee872021-08-09 10:19:47 +02001916static bool has_locked_children(struct mount *mnt, struct dentry *dentry)
1917{
1918 struct mount *child;
1919
1920 list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
1921 if (!is_subdir(child->mnt_mountpoint, dentry))
1922 continue;
1923
1924 if (child->mnt.mnt_flags & MNT_LOCKED)
1925 return true;
1926 }
1927 return false;
1928}
1929
Miklos Szeredic771d682014-10-24 00:14:36 +02001930/**
1931 * clone_private_mount - create a private clone of a path
1932 *
1933 * This creates a new vfsmount, which will be the clone of @path. The new will
1934 * not be attached anywhere in the namespace and will be private (i.e. changes
1935 * to the originating mount won't be propagated into this).
1936 *
1937 * Release with mntput().
1938 */
1939struct vfsmount *clone_private_mount(struct path *path)
1940{
1941 struct mount *old_mnt = real_mount(path->mnt);
1942 struct mount *new_mnt;
1943
Miklos Szeredic771d682014-10-24 00:14:36 +02001944 down_read(&namespace_sem);
Miklos Szeredie3eee872021-08-09 10:19:47 +02001945 if (IS_MNT_UNBINDABLE(old_mnt))
1946 goto invalid;
1947
1948 if (!check_mnt(old_mnt))
1949 goto invalid;
1950
1951 if (has_locked_children(old_mnt, path->dentry))
1952 goto invalid;
1953
Miklos Szeredic771d682014-10-24 00:14:36 +02001954 new_mnt = clone_mnt(old_mnt, path->dentry, CL_PRIVATE);
1955 up_read(&namespace_sem);
Miklos Szeredie3eee872021-08-09 10:19:47 +02001956
Miklos Szeredic771d682014-10-24 00:14:36 +02001957 if (IS_ERR(new_mnt))
1958 return ERR_CAST(new_mnt);
1959
1960 return &new_mnt->mnt;
Miklos Szeredie3eee872021-08-09 10:19:47 +02001961
1962invalid:
1963 up_read(&namespace_sem);
1964 return ERR_PTR(-EINVAL);
Miklos Szeredic771d682014-10-24 00:14:36 +02001965}
1966EXPORT_SYMBOL_GPL(clone_private_mount);
1967
Al Viro1f707132010-01-30 22:51:25 -05001968int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
1969 struct vfsmount *root)
1970{
Al Viro1a4eeaf2011-11-25 02:19:55 -05001971 struct mount *mnt;
Al Viro1f707132010-01-30 22:51:25 -05001972 int res = f(root, arg);
1973 if (res)
1974 return res;
Al Viro1a4eeaf2011-11-25 02:19:55 -05001975 list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) {
1976 res = f(&mnt->mnt, arg);
Al Viro1f707132010-01-30 22:51:25 -05001977 if (res)
1978 return res;
1979 }
1980 return 0;
1981}
1982
Al Viro4b8b21f2011-11-24 19:54:23 -05001983static void cleanup_group_ids(struct mount *mnt, struct mount *end)
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001984{
Al Viro315fc832011-11-24 18:57:30 -05001985 struct mount *p;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001986
Al Viro909b0a82011-11-25 03:06:56 -05001987 for (p = mnt; p != end; p = next_mnt(p, mnt)) {
Al Virofc7be132011-11-25 01:05:37 -05001988 if (p->mnt_group_id && !IS_MNT_SHARED(p))
Al Viro4b8b21f2011-11-24 19:54:23 -05001989 mnt_release_group_id(p);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001990 }
1991}
1992
Al Viro4b8b21f2011-11-24 19:54:23 -05001993static int invent_group_ids(struct mount *mnt, bool recurse)
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001994{
Al Viro315fc832011-11-24 18:57:30 -05001995 struct mount *p;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001996
Al Viro909b0a82011-11-25 03:06:56 -05001997 for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
Al Virofc7be132011-11-25 01:05:37 -05001998 if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
Al Viro4b8b21f2011-11-24 19:54:23 -05001999 int err = mnt_alloc_group_id(p);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002000 if (err) {
Al Viro4b8b21f2011-11-24 19:54:23 -05002001 cleanup_group_ids(mnt, p);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002002 return err;
2003 }
2004 }
2005 }
2006
2007 return 0;
2008}
2009
Eric W. Biedermand2921682016-09-28 00:27:17 -05002010int count_mounts(struct mnt_namespace *ns, struct mount *mnt)
2011{
2012 unsigned int max = READ_ONCE(sysctl_mount_max);
2013 unsigned int mounts = 0, old, pending, sum;
2014 struct mount *p;
2015
2016 for (p = mnt; p; p = next_mnt(p, mnt))
2017 mounts++;
2018
2019 old = ns->mounts;
2020 pending = ns->pending_mounts;
2021 sum = old + pending;
2022 if ((old > sum) ||
2023 (pending > sum) ||
2024 (max < sum) ||
2025 (mounts > (max - sum)))
2026 return -ENOSPC;
2027
2028 ns->pending_mounts = pending + mounts;
2029 return 0;
2030}
2031
Ram Paib90fa9a2005-11-07 17:19:50 -05002032/*
2033 * @source_mnt : mount tree to be attached
Ram Pai21444402005-11-07 17:20:03 -05002034 * @nd : place the mount tree @source_mnt is attached
2035 * @parent_nd : if non-null, detach the source_mnt from its parent and
2036 * store the parent mount and mountpoint dentry.
2037 * (done when source_mnt is moved)
Ram Paib90fa9a2005-11-07 17:19:50 -05002038 *
2039 * NOTE: in the table below explains the semantics when a source mount
2040 * of a given type is attached to a destination mount of a given type.
Ram Pai9676f0c2005-11-07 17:21:20 -05002041 * ---------------------------------------------------------------------------
2042 * | BIND MOUNT OPERATION |
2043 * |**************************************************************************
2044 * | source-->| shared | private | slave | unbindable |
2045 * | dest | | | | |
2046 * | | | | | | |
2047 * | v | | | | |
2048 * |**************************************************************************
2049 * | shared | shared (++) | shared (+) | shared(+++)| invalid |
2050 * | | | | | |
2051 * |non-shared| shared (+) | private | slave (*) | invalid |
2052 * ***************************************************************************
Ram Paib90fa9a2005-11-07 17:19:50 -05002053 * A bind operation clones the source mount and mounts the clone on the
2054 * destination mount.
2055 *
2056 * (++) the cloned mount is propagated to all the mounts in the propagation
2057 * tree of the destination mount and the cloned mount is added to
2058 * the peer group of the source mount.
2059 * (+) the cloned mount is created under the destination mount and is marked
2060 * as shared. The cloned mount is added to the peer group of the source
2061 * mount.
Ram Pai5afe0022005-11-07 17:21:01 -05002062 * (+++) the mount is propagated to all the mounts in the propagation tree
2063 * of the destination mount and the cloned mount is made slave
2064 * of the same master as that of the source mount. The cloned mount
2065 * is marked as 'shared and slave'.
2066 * (*) the cloned mount is made a slave of the same master as that of the
2067 * source mount.
2068 *
Ram Pai9676f0c2005-11-07 17:21:20 -05002069 * ---------------------------------------------------------------------------
2070 * | MOVE MOUNT OPERATION |
2071 * |**************************************************************************
2072 * | source-->| shared | private | slave | unbindable |
2073 * | dest | | | | |
2074 * | | | | | | |
2075 * | v | | | | |
2076 * |**************************************************************************
2077 * | shared | shared (+) | shared (+) | shared(+++) | invalid |
2078 * | | | | | |
2079 * |non-shared| shared (+*) | private | slave (*) | unbindable |
2080 * ***************************************************************************
Ram Pai5afe0022005-11-07 17:21:01 -05002081 *
2082 * (+) the mount is moved to the destination. And is then propagated to
2083 * all the mounts in the propagation tree of the destination mount.
Ram Pai21444402005-11-07 17:20:03 -05002084 * (+*) the mount is moved to the destination.
Ram Pai5afe0022005-11-07 17:21:01 -05002085 * (+++) the mount is moved to the destination and is then propagated to
2086 * all the mounts belonging to the destination mount's propagation tree.
2087 * the mount is marked as 'shared and slave'.
2088 * (*) the mount continues to be a slave at the new location.
Ram Paib90fa9a2005-11-07 17:19:50 -05002089 *
2090 * if the source mount is a tree, the operations explained above is
2091 * applied to each mount in the tree.
2092 * Must be called without spinlocks held, since this function can sleep
2093 * in allocations.
2094 */
Al Viro0fb54e52011-11-24 19:59:16 -05002095static int attach_recursive_mnt(struct mount *source_mnt,
Al Viro84d17192013-03-15 10:53:28 -04002096 struct mount *dest_mnt,
2097 struct mountpoint *dest_mp,
2098 struct path *parent_path)
Ram Paib90fa9a2005-11-07 17:19:50 -05002099{
Al Viro38129a12014-03-20 21:10:51 -04002100 HLIST_HEAD(tree_list);
Eric W. Biedermand2921682016-09-28 00:27:17 -05002101 struct mnt_namespace *ns = dest_mnt->mnt_ns;
Eric W. Biederman808e83e2017-01-20 18:28:35 +13002102 struct mountpoint *smp;
Al Viro315fc832011-11-24 18:57:30 -05002103 struct mount *child, *p;
Al Viro38129a12014-03-20 21:10:51 -04002104 struct hlist_node *n;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002105 int err;
Ram Paib90fa9a2005-11-07 17:19:50 -05002106
Eric W. Biederman808e83e2017-01-20 18:28:35 +13002107 /* Preallocate a mountpoint in case the new mounts need
2108 * to be tucked under other mounts.
2109 */
2110 smp = get_mountpoint(source_mnt->mnt.mnt_root);
2111 if (IS_ERR(smp))
2112 return PTR_ERR(smp);
2113
Eric W. Biedermand2921682016-09-28 00:27:17 -05002114 /* Is there space to add these mounts to the mount namespace? */
2115 if (!parent_path) {
2116 err = count_mounts(ns, source_mnt);
2117 if (err)
2118 goto out;
2119 }
2120
Al Virofc7be132011-11-25 01:05:37 -05002121 if (IS_MNT_SHARED(dest_mnt)) {
Al Viro0fb54e52011-11-24 19:59:16 -05002122 err = invent_group_ids(source_mnt, true);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002123 if (err)
2124 goto out;
Al Viro0b1b9012014-03-21 10:14:08 -04002125 err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list);
Al Virof2ebb3a2014-02-27 09:35:45 -05002126 lock_mount_hash();
Al Viro0b1b9012014-03-21 10:14:08 -04002127 if (err)
2128 goto out_cleanup_ids;
Al Viro909b0a82011-11-25 03:06:56 -05002129 for (p = source_mnt; p; p = next_mnt(p, source_mnt))
Al Viro0f0afb12011-11-24 20:43:10 -05002130 set_mnt_shared(p);
Al Viro0b1b9012014-03-21 10:14:08 -04002131 } else {
2132 lock_mount_hash();
Ram Paib90fa9a2005-11-07 17:19:50 -05002133 }
Al Viro1a390682008-03-21 20:48:19 -04002134 if (parent_path) {
Al Viro0fb54e52011-11-24 19:59:16 -05002135 detach_mnt(source_mnt, parent_path);
Al Viro84d17192013-03-15 10:53:28 -04002136 attach_mnt(source_mnt, dest_mnt, dest_mp);
Al Viro143c8c92011-11-25 00:46:35 -05002137 touch_mnt_namespace(source_mnt->mnt_ns);
Ram Pai21444402005-11-07 17:20:03 -05002138 } else {
Al Viro84d17192013-03-15 10:53:28 -04002139 mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt);
Eric W. Biederman808e83e2017-01-20 18:28:35 +13002140 commit_tree(source_mnt);
Ram Pai21444402005-11-07 17:20:03 -05002141 }
Ram Paib90fa9a2005-11-07 17:19:50 -05002142
Al Viro38129a12014-03-20 21:10:51 -04002143 hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) {
Al Viro1d6a32a2014-03-20 20:34:43 -04002144 struct mount *q;
Al Viro38129a12014-03-20 21:10:51 -04002145 hlist_del_init(&child->mnt_hash);
Eric W. Biederman808e83e2017-01-20 18:28:35 +13002146 q = __lookup_mnt(&child->mnt_parent->mnt,
2147 child->mnt_mountpoint);
2148 if (q)
2149 mnt_change_mountpoint(child, smp, q);
2150 commit_tree(child);
Ram Paib90fa9a2005-11-07 17:19:50 -05002151 }
Eric W. Biederman808e83e2017-01-20 18:28:35 +13002152 put_mountpoint(smp);
Al Viro719ea2f2013-09-29 11:24:49 -04002153 unlock_mount_hash();
Nick Piggin99b7db72010-08-18 04:37:39 +10002154
Ram Paib90fa9a2005-11-07 17:19:50 -05002155 return 0;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002156
2157 out_cleanup_ids:
Al Virof2ebb3a2014-02-27 09:35:45 -05002158 while (!hlist_empty(&tree_list)) {
2159 child = hlist_entry(tree_list.first, struct mount, mnt_hash);
Eric W. Biedermand2921682016-09-28 00:27:17 -05002160 child->mnt_parent->mnt_ns->pending_mounts = 0;
Eric W. Biedermane819f152014-12-24 07:20:01 -06002161 umount_tree(child, UMOUNT_SYNC);
Al Virof2ebb3a2014-02-27 09:35:45 -05002162 }
2163 unlock_mount_hash();
Al Viro0b1b9012014-03-21 10:14:08 -04002164 cleanup_group_ids(source_mnt, NULL);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002165 out:
Eric W. Biedermand2921682016-09-28 00:27:17 -05002166 ns->pending_mounts = 0;
Eric W. Biederman808e83e2017-01-20 18:28:35 +13002167
2168 read_seqlock_excl(&mount_lock);
2169 put_mountpoint(smp);
2170 read_sequnlock_excl(&mount_lock);
2171
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002172 return err;
Ram Paib90fa9a2005-11-07 17:19:50 -05002173}
2174
Al Viro84d17192013-03-15 10:53:28 -04002175static struct mountpoint *lock_mount(struct path *path)
Al Virob12cea92011-03-18 08:55:38 -04002176{
2177 struct vfsmount *mnt;
Al Viro84d17192013-03-15 10:53:28 -04002178 struct dentry *dentry = path->dentry;
Al Virob12cea92011-03-18 08:55:38 -04002179retry:
Al Viro59551022016-01-22 15:40:57 -05002180 inode_lock(dentry->d_inode);
Al Viro84d17192013-03-15 10:53:28 -04002181 if (unlikely(cant_mount(dentry))) {
Al Viro59551022016-01-22 15:40:57 -05002182 inode_unlock(dentry->d_inode);
Al Viro84d17192013-03-15 10:53:28 -04002183 return ERR_PTR(-ENOENT);
Al Virob12cea92011-03-18 08:55:38 -04002184 }
Al Viro97216be2013-03-16 15:12:40 -04002185 namespace_lock();
Al Virob12cea92011-03-18 08:55:38 -04002186 mnt = lookup_mnt(path);
Al Viro84d17192013-03-15 10:53:28 -04002187 if (likely(!mnt)) {
Eric W. Biederman1a62a0f2017-01-03 14:18:43 +13002188 struct mountpoint *mp = get_mountpoint(dentry);
Al Viro84d17192013-03-15 10:53:28 -04002189 if (IS_ERR(mp)) {
Al Viro97216be2013-03-16 15:12:40 -04002190 namespace_unlock();
Al Viro59551022016-01-22 15:40:57 -05002191 inode_unlock(dentry->d_inode);
Al Viro84d17192013-03-15 10:53:28 -04002192 return mp;
2193 }
2194 return mp;
2195 }
Al Viro97216be2013-03-16 15:12:40 -04002196 namespace_unlock();
Al Viro59551022016-01-22 15:40:57 -05002197 inode_unlock(path->dentry->d_inode);
Al Virob12cea92011-03-18 08:55:38 -04002198 path_put(path);
2199 path->mnt = mnt;
Al Viro84d17192013-03-15 10:53:28 -04002200 dentry = path->dentry = dget(mnt->mnt_root);
Al Virob12cea92011-03-18 08:55:38 -04002201 goto retry;
2202}
2203
Al Viro84d17192013-03-15 10:53:28 -04002204static void unlock_mount(struct mountpoint *where)
Al Virob12cea92011-03-18 08:55:38 -04002205{
Al Viro84d17192013-03-15 10:53:28 -04002206 struct dentry *dentry = where->m_dentry;
Eric W. Biederman1a62a0f2017-01-03 14:18:43 +13002207
2208 read_seqlock_excl(&mount_lock);
Al Viro84d17192013-03-15 10:53:28 -04002209 put_mountpoint(where);
Eric W. Biederman1a62a0f2017-01-03 14:18:43 +13002210 read_sequnlock_excl(&mount_lock);
2211
Al Viro328e6d92013-03-16 14:49:45 -04002212 namespace_unlock();
Al Viro59551022016-01-22 15:40:57 -05002213 inode_unlock(dentry->d_inode);
Al Virob12cea92011-03-18 08:55:38 -04002214}
2215
Al Viro84d17192013-03-15 10:53:28 -04002216static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217{
Al Viro95bc5f22011-11-25 00:30:56 -05002218 if (mnt->mnt.mnt_sb->s_flags & MS_NOUSER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 return -EINVAL;
2220
David Howellse36cb0b2015-01-29 12:02:35 +00002221 if (d_is_dir(mp->m_dentry) !=
2222 d_is_dir(mnt->mnt.mnt_root))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 return -ENOTDIR;
2224
Al Viro84d17192013-03-15 10:53:28 -04002225 return attach_recursive_mnt(mnt, p, mp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226}
2227
2228/*
Valerie Aurora7a2e8a82010-08-26 11:07:22 -07002229 * Sanity check the flags to change_mnt_propagation.
2230 */
2231
2232static int flags_to_propagation_type(int flags)
2233{
Roman Borisov7c6e9842011-05-25 16:26:48 -07002234 int type = flags & ~(MS_REC | MS_SILENT);
Valerie Aurora7a2e8a82010-08-26 11:07:22 -07002235
2236 /* Fail if any non-propagation flags are set */
2237 if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
2238 return 0;
2239 /* Only one propagation flag should be set */
2240 if (!is_power_of_2(type))
2241 return 0;
2242 return type;
2243}
2244
2245/*
Ram Pai07b20882005-11-07 17:19:07 -05002246 * recursively change the type of the mountpoint.
2247 */
Al Viro0a0d8a42008-08-02 00:55:27 -04002248static int do_change_type(struct path *path, int flag)
Ram Pai07b20882005-11-07 17:19:07 -05002249{
Al Viro315fc832011-11-24 18:57:30 -05002250 struct mount *m;
Al Viro4b8b21f2011-11-24 19:54:23 -05002251 struct mount *mnt = real_mount(path->mnt);
Ram Pai07b20882005-11-07 17:19:07 -05002252 int recurse = flag & MS_REC;
Valerie Aurora7a2e8a82010-08-26 11:07:22 -07002253 int type;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002254 int err = 0;
Ram Pai07b20882005-11-07 17:19:07 -05002255
Al Viro2d92ab32008-08-02 00:51:11 -04002256 if (path->dentry != path->mnt->mnt_root)
Ram Pai07b20882005-11-07 17:19:07 -05002257 return -EINVAL;
2258
Valerie Aurora7a2e8a82010-08-26 11:07:22 -07002259 type = flags_to_propagation_type(flag);
2260 if (!type)
2261 return -EINVAL;
2262
Al Viro97216be2013-03-16 15:12:40 -04002263 namespace_lock();
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002264 if (type == MS_SHARED) {
2265 err = invent_group_ids(mnt, recurse);
2266 if (err)
2267 goto out_unlock;
2268 }
2269
Al Viro719ea2f2013-09-29 11:24:49 -04002270 lock_mount_hash();
Al Viro909b0a82011-11-25 03:06:56 -05002271 for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
Al Viro0f0afb12011-11-24 20:43:10 -05002272 change_mnt_propagation(m, type);
Al Viro719ea2f2013-09-29 11:24:49 -04002273 unlock_mount_hash();
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002274
2275 out_unlock:
Al Viro97216be2013-03-16 15:12:40 -04002276 namespace_unlock();
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002277 return err;
Ram Pai07b20882005-11-07 17:19:07 -05002278}
2279
2280/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 * do loopback mount.
2282 */
Al Viro808d4e32012-10-11 11:42:01 -04002283static int do_loopback(struct path *path, const char *old_name,
Eric Sandeen2dafe1c2008-02-08 04:22:12 -08002284 int recurse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285{
Al Viro2d92ab32008-08-02 00:51:11 -04002286 struct path old_path;
Al Viro84d17192013-03-15 10:53:28 -04002287 struct mount *mnt = NULL, *old, *parent;
2288 struct mountpoint *mp;
Al Viro57eccb82013-02-22 22:49:10 -05002289 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 if (!old_name || !*old_name)
2291 return -EINVAL;
Trond Myklebust815d4052011-09-26 20:36:09 -04002292 err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 if (err)
2294 return err;
2295
Eric W. Biederman8823c072010-03-07 18:49:36 -08002296 err = -EINVAL;
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07002297 if (mnt_ns_loop(old_path.dentry))
Eric W. Biederman8823c072010-03-07 18:49:36 -08002298 goto out;
2299
Al Viro84d17192013-03-15 10:53:28 -04002300 mp = lock_mount(path);
2301 err = PTR_ERR(mp);
2302 if (IS_ERR(mp))
Jan Blunck4ac91372008-02-14 19:34:32 -08002303 goto out;
Ram Pai9676f0c2005-11-07 17:21:20 -05002304
Al Viro87129cc2011-11-24 21:24:27 -05002305 old = real_mount(old_path.mnt);
Al Viro84d17192013-03-15 10:53:28 -04002306 parent = real_mount(path->mnt);
Al Viro87129cc2011-11-24 21:24:27 -05002307
Al Virob12cea92011-03-18 08:55:38 -04002308 err = -EINVAL;
Al Virofc7be132011-11-25 01:05:37 -05002309 if (IS_MNT_UNBINDABLE(old))
Al Virob12cea92011-03-18 08:55:38 -04002310 goto out2;
2311
Al Viroe149ed22014-11-01 10:57:28 -04002312 if (!check_mnt(parent))
2313 goto out2;
2314
2315 if (!check_mnt(old) && old_path.dentry->d_op != &ns_dentry_operations)
Al Virob12cea92011-03-18 08:55:38 -04002316 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
Eric W. Biederman5ff9d8a2013-03-29 21:04:39 -07002318 if (!recurse && has_locked_children(old, old_path.dentry))
2319 goto out2;
2320
Al Viroccd48bc2005-11-07 17:15:04 -05002321 if (recurse)
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07002322 mnt = copy_tree(old, old_path.dentry, CL_COPY_MNT_NS_FILE);
Al Viroccd48bc2005-11-07 17:15:04 -05002323 else
Al Viro87129cc2011-11-24 21:24:27 -05002324 mnt = clone_mnt(old, old_path.dentry, 0);
Al Viroccd48bc2005-11-07 17:15:04 -05002325
David Howellsbe34d1a2012-06-25 12:55:18 +01002326 if (IS_ERR(mnt)) {
2327 err = PTR_ERR(mnt);
Andrey Vagine9c5d8a2013-04-09 17:33:29 +04002328 goto out2;
David Howellsbe34d1a2012-06-25 12:55:18 +01002329 }
Al Viroccd48bc2005-11-07 17:15:04 -05002330
Eric W. Biederman5ff9d8a2013-03-29 21:04:39 -07002331 mnt->mnt.mnt_flags &= ~MNT_LOCKED;
2332
Al Viro84d17192013-03-15 10:53:28 -04002333 err = graft_tree(mnt, parent, mp);
Al Viroccd48bc2005-11-07 17:15:04 -05002334 if (err) {
Al Viro719ea2f2013-09-29 11:24:49 -04002335 lock_mount_hash();
Eric W. Biedermane819f152014-12-24 07:20:01 -06002336 umount_tree(mnt, UMOUNT_SYNC);
Al Viro719ea2f2013-09-29 11:24:49 -04002337 unlock_mount_hash();
Ram Pai5b83d2c5c2005-11-07 17:16:29 -05002338 }
Al Virob12cea92011-03-18 08:55:38 -04002339out2:
Al Viro84d17192013-03-15 10:53:28 -04002340 unlock_mount(mp);
Al Viroccd48bc2005-11-07 17:15:04 -05002341out:
Al Viro2d92ab32008-08-02 00:51:11 -04002342 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 return err;
2344}
2345
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08002346static int change_mount_flags(struct vfsmount *mnt, int ms_flags)
2347{
2348 int error = 0;
2349 int readonly_request = 0;
2350
2351 if (ms_flags & MS_RDONLY)
2352 readonly_request = 1;
2353 if (readonly_request == __mnt_is_readonly(mnt))
2354 return 0;
2355
2356 if (readonly_request)
Al Viro83adc752011-11-24 22:37:54 -05002357 error = mnt_make_readonly(real_mount(mnt));
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08002358 else
Al Viro83adc752011-11-24 22:37:54 -05002359 __mnt_unmake_readonly(real_mount(mnt));
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08002360 return error;
2361}
2362
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363/*
2364 * change filesystem flags. dir should be a physical root of filesystem.
2365 * If you've mounted a non-root directory somewhere and want to do remount
2366 * on it - tough luck.
2367 */
Al Viro0a0d8a42008-08-02 00:55:27 -04002368static int do_remount(struct path *path, int flags, int mnt_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 void *data)
2370{
2371 int err;
Al Viro2d92ab32008-08-02 00:51:11 -04002372 struct super_block *sb = path->mnt->mnt_sb;
Al Viro143c8c92011-11-25 00:46:35 -05002373 struct mount *mnt = real_mount(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
Al Viro143c8c92011-11-25 00:46:35 -05002375 if (!check_mnt(mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 return -EINVAL;
2377
Al Viro2d92ab32008-08-02 00:51:11 -04002378 if (path->dentry != path->mnt->mnt_root)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 return -EINVAL;
2380
Eric W. Biederman07b64552014-07-28 17:10:56 -07002381 /* Don't allow changing of locked mnt flags.
2382 *
2383 * No locks need to be held here while testing the various
2384 * MNT_LOCK flags because those flags can never be cleared
2385 * once they are set.
2386 */
2387 if ((mnt->mnt.mnt_flags & MNT_LOCK_READONLY) &&
2388 !(mnt_flags & MNT_READONLY)) {
2389 return -EPERM;
2390 }
Eric W. Biederman9566d672014-07-28 17:26:07 -07002391 if ((mnt->mnt.mnt_flags & MNT_LOCK_NODEV) &&
2392 !(mnt_flags & MNT_NODEV)) {
Eric W. Biederman67690f92016-05-18 13:50:06 -05002393 return -EPERM;
Eric W. Biederman9566d672014-07-28 17:26:07 -07002394 }
2395 if ((mnt->mnt.mnt_flags & MNT_LOCK_NOSUID) &&
2396 !(mnt_flags & MNT_NOSUID)) {
2397 return -EPERM;
2398 }
2399 if ((mnt->mnt.mnt_flags & MNT_LOCK_NOEXEC) &&
2400 !(mnt_flags & MNT_NOEXEC)) {
2401 return -EPERM;
2402 }
2403 if ((mnt->mnt.mnt_flags & MNT_LOCK_ATIME) &&
2404 ((mnt->mnt.mnt_flags & MNT_ATIME_MASK) != (mnt_flags & MNT_ATIME_MASK))) {
2405 return -EPERM;
2406 }
2407
Eric Parisff36fe22011-03-03 16:09:14 -05002408 err = security_sb_remount(sb, data);
2409 if (err)
2410 return err;
2411
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 down_write(&sb->s_umount);
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08002413 if (flags & MS_BIND)
Al Viro2d92ab32008-08-02 00:51:11 -04002414 err = change_mount_flags(path->mnt, flags);
Al Viro57eccb82013-02-22 22:49:10 -05002415 else if (!capable(CAP_SYS_ADMIN))
2416 err = -EPERM;
Daniel Rosenberg1aaf05b2016-10-26 15:29:51 -07002417 else {
Daniel Rosenberg29f888e2016-10-26 15:58:22 -07002418 err = do_remount_sb2(path->mnt, sb, flags, data, 0);
Daniel Rosenberg1aaf05b2016-10-26 15:29:51 -07002419 namespace_lock();
2420 lock_mount_hash();
2421 propagate_remount(mnt);
2422 unlock_mount_hash();
2423 namespace_unlock();
2424 }
Al Viro7b43a792010-01-16 13:01:26 -05002425 if (!err) {
Al Viro719ea2f2013-09-29 11:24:49 -04002426 lock_mount_hash();
Eric W. Biedermana6138db2014-07-28 16:26:53 -07002427 mnt_flags |= mnt->mnt.mnt_flags & ~MNT_USER_SETTABLE_MASK;
Al Viro143c8c92011-11-25 00:46:35 -05002428 mnt->mnt.mnt_flags = mnt_flags;
Al Viro143c8c92011-11-25 00:46:35 -05002429 touch_mnt_namespace(mnt->mnt_ns);
Al Viro719ea2f2013-09-29 11:24:49 -04002430 unlock_mount_hash();
Dan Williams0e55a7c2008-09-26 19:01:20 -07002431 }
Al Viro6339dab2013-09-16 22:41:01 -04002432 up_write(&sb->s_umount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 return err;
2434}
2435
Al Virocbbe3622011-11-24 20:01:19 -05002436static inline int tree_contains_unbindable(struct mount *mnt)
Ram Pai9676f0c2005-11-07 17:21:20 -05002437{
Al Viro315fc832011-11-24 18:57:30 -05002438 struct mount *p;
Al Viro909b0a82011-11-25 03:06:56 -05002439 for (p = mnt; p; p = next_mnt(p, mnt)) {
Al Virofc7be132011-11-25 01:05:37 -05002440 if (IS_MNT_UNBINDABLE(p))
Ram Pai9676f0c2005-11-07 17:21:20 -05002441 return 1;
2442 }
2443 return 0;
2444}
2445
Al Viro808d4e32012-10-11 11:42:01 -04002446static int do_move_mount(struct path *path, const char *old_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447{
Al Viro2d92ab32008-08-02 00:51:11 -04002448 struct path old_path, parent_path;
Al Viro676da582011-11-24 21:47:05 -05002449 struct mount *p;
Al Viro0fb54e52011-11-24 19:59:16 -05002450 struct mount *old;
Al Viro84d17192013-03-15 10:53:28 -04002451 struct mountpoint *mp;
Al Viro57eccb82013-02-22 22:49:10 -05002452 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 if (!old_name || !*old_name)
2454 return -EINVAL;
Al Viro2d92ab32008-08-02 00:51:11 -04002455 err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 if (err)
2457 return err;
2458
Al Viro84d17192013-03-15 10:53:28 -04002459 mp = lock_mount(path);
2460 err = PTR_ERR(mp);
2461 if (IS_ERR(mp))
David Howellscc53ce52011-01-14 18:45:26 +00002462 goto out;
2463
Al Viro143c8c92011-11-25 00:46:35 -05002464 old = real_mount(old_path.mnt);
Al Virofc7be132011-11-25 01:05:37 -05002465 p = real_mount(path->mnt);
Al Viro143c8c92011-11-25 00:46:35 -05002466
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 err = -EINVAL;
Al Virofc7be132011-11-25 01:05:37 -05002468 if (!check_mnt(p) || !check_mnt(old))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 goto out1;
2470
Eric W. Biederman5ff9d8a2013-03-29 21:04:39 -07002471 if (old->mnt.mnt_flags & MNT_LOCKED)
2472 goto out1;
2473
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 err = -EINVAL;
Al Viro2d92ab32008-08-02 00:51:11 -04002475 if (old_path.dentry != old_path.mnt->mnt_root)
Ram Pai21444402005-11-07 17:20:03 -05002476 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
Al Viro676da582011-11-24 21:47:05 -05002478 if (!mnt_has_parent(old))
Ram Pai21444402005-11-07 17:20:03 -05002479 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
David Howellse36cb0b2015-01-29 12:02:35 +00002481 if (d_is_dir(path->dentry) !=
2482 d_is_dir(old_path.dentry))
Ram Pai21444402005-11-07 17:20:03 -05002483 goto out1;
2484 /*
2485 * Don't move a mount residing in a shared parent.
2486 */
Al Virofc7be132011-11-25 01:05:37 -05002487 if (IS_MNT_SHARED(old->mnt_parent))
Ram Pai21444402005-11-07 17:20:03 -05002488 goto out1;
Ram Pai9676f0c2005-11-07 17:21:20 -05002489 /*
2490 * Don't move a mount tree containing unbindable mounts to a destination
2491 * mount which is shared.
2492 */
Al Virofc7be132011-11-25 01:05:37 -05002493 if (IS_MNT_SHARED(p) && tree_contains_unbindable(old))
Ram Pai9676f0c2005-11-07 17:21:20 -05002494 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 err = -ELOOP;
Al Virofc7be132011-11-25 01:05:37 -05002496 for (; mnt_has_parent(p); p = p->mnt_parent)
Al Viro676da582011-11-24 21:47:05 -05002497 if (p == old)
Ram Pai21444402005-11-07 17:20:03 -05002498 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
Al Viro84d17192013-03-15 10:53:28 -04002500 err = attach_recursive_mnt(old, real_mount(path->mnt), mp, &parent_path);
Jan Blunck4ac91372008-02-14 19:34:32 -08002501 if (err)
Ram Pai21444402005-11-07 17:20:03 -05002502 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503
2504 /* if the mount is moved, it should no longer be expire
2505 * automatically */
Al Viro6776db3d2011-11-25 00:22:05 -05002506 list_del_init(&old->mnt_expire);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507out1:
Al Viro84d17192013-03-15 10:53:28 -04002508 unlock_mount(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 if (!err)
Al Viro1a390682008-03-21 20:48:19 -04002511 path_put(&parent_path);
Al Viro2d92ab32008-08-02 00:51:11 -04002512 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 return err;
2514}
2515
Al Viro9d412a42011-03-17 22:08:28 -04002516static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
2517{
2518 int err;
2519 const char *subtype = strchr(fstype, '.');
2520 if (subtype) {
2521 subtype++;
2522 err = -EINVAL;
2523 if (!subtype[0])
2524 goto err;
2525 } else
2526 subtype = "";
2527
2528 mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
2529 err = -ENOMEM;
2530 if (!mnt->mnt_sb->s_subtype)
2531 goto err;
2532 return mnt;
2533
2534 err:
2535 mntput(mnt);
2536 return ERR_PTR(err);
2537}
2538
Al Viro9d412a42011-03-17 22:08:28 -04002539/*
2540 * add a mount into a namespace's mount tree
2541 */
Al Viro95bc5f22011-11-25 00:30:56 -05002542static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags)
Al Viro9d412a42011-03-17 22:08:28 -04002543{
Al Viro84d17192013-03-15 10:53:28 -04002544 struct mountpoint *mp;
2545 struct mount *parent;
Al Viro9d412a42011-03-17 22:08:28 -04002546 int err;
2547
Al Virof2ebb3a2014-02-27 09:35:45 -05002548 mnt_flags &= ~MNT_INTERNAL_FLAGS;
Al Viro9d412a42011-03-17 22:08:28 -04002549
Al Viro84d17192013-03-15 10:53:28 -04002550 mp = lock_mount(path);
2551 if (IS_ERR(mp))
2552 return PTR_ERR(mp);
Al Viro9d412a42011-03-17 22:08:28 -04002553
Al Viro84d17192013-03-15 10:53:28 -04002554 parent = real_mount(path->mnt);
Al Viro9d412a42011-03-17 22:08:28 -04002555 err = -EINVAL;
Al Viro84d17192013-03-15 10:53:28 -04002556 if (unlikely(!check_mnt(parent))) {
Al Viro156cacb2012-09-21 08:19:02 -04002557 /* that's acceptable only for automounts done in private ns */
2558 if (!(mnt_flags & MNT_SHRINKABLE))
2559 goto unlock;
2560 /* ... and for those we'd better have mountpoint still alive */
Al Viro84d17192013-03-15 10:53:28 -04002561 if (!parent->mnt_ns)
Al Viro156cacb2012-09-21 08:19:02 -04002562 goto unlock;
2563 }
Al Viro9d412a42011-03-17 22:08:28 -04002564
2565 /* Refuse the same filesystem on the same mount point */
2566 err = -EBUSY;
Al Viro95bc5f22011-11-25 00:30:56 -05002567 if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb &&
Al Viro9d412a42011-03-17 22:08:28 -04002568 path->mnt->mnt_root == path->dentry)
2569 goto unlock;
2570
2571 err = -EINVAL;
David Howellse36cb0b2015-01-29 12:02:35 +00002572 if (d_is_symlink(newmnt->mnt.mnt_root))
Al Viro9d412a42011-03-17 22:08:28 -04002573 goto unlock;
2574
Al Viro95bc5f22011-11-25 00:30:56 -05002575 newmnt->mnt.mnt_flags = mnt_flags;
Al Viro84d17192013-03-15 10:53:28 -04002576 err = graft_tree(newmnt, parent, mp);
Al Viro9d412a42011-03-17 22:08:28 -04002577
2578unlock:
Al Viro84d17192013-03-15 10:53:28 -04002579 unlock_mount(mp);
Al Viro9d412a42011-03-17 22:08:28 -04002580 return err;
2581}
Al Virob1e75df2011-01-17 01:47:59 -05002582
Eric W. Biederman8654df42016-06-09 16:06:06 -05002583static bool mount_too_revealing(struct vfsmount *mnt, int *new_mnt_flags);
Eric W. Biederman1b852bc2015-05-08 23:22:29 -05002584
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585/*
2586 * create a new mount for userspace and request it to be added into the
2587 * namespace's tree
2588 */
Eric W. Biederman0c55cfc2012-07-26 21:42:03 -07002589static int do_new_mount(struct path *path, const char *fstype, int flags,
Al Viro808d4e32012-10-11 11:42:01 -04002590 int mnt_flags, const char *name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591{
Eric W. Biederman0c55cfc2012-07-26 21:42:03 -07002592 struct file_system_type *type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 struct vfsmount *mnt;
Al Viro15f9a3f2011-01-17 01:41:58 -05002594 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
Eric W. Biederman0c55cfc2012-07-26 21:42:03 -07002596 if (!fstype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 return -EINVAL;
2598
Eric W. Biederman0c55cfc2012-07-26 21:42:03 -07002599 type = get_fs_type(fstype);
2600 if (!type)
2601 return -ENODEV;
2602
Eric W. Biederman0c55cfc2012-07-26 21:42:03 -07002603 mnt = vfs_kern_mount(type, flags, name, data);
2604 if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
2605 !mnt->mnt_sb->s_subtype)
2606 mnt = fs_set_subtype(mnt, fstype);
2607
2608 put_filesystem(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 if (IS_ERR(mnt))
2610 return PTR_ERR(mnt);
2611
Eric W. Biederman8654df42016-06-09 16:06:06 -05002612 if (mount_too_revealing(mnt, &mnt_flags)) {
2613 mntput(mnt);
2614 return -EPERM;
2615 }
2616
Al Viro95bc5f22011-11-25 00:30:56 -05002617 err = do_add_mount(real_mount(mnt), path, mnt_flags);
Al Viro15f9a3f2011-01-17 01:41:58 -05002618 if (err)
2619 mntput(mnt);
2620 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621}
2622
Al Viro19a167a2011-01-17 01:35:23 -05002623int finish_automount(struct vfsmount *m, struct path *path)
2624{
Al Viro6776db3d2011-11-25 00:22:05 -05002625 struct mount *mnt = real_mount(m);
Al Viro19a167a2011-01-17 01:35:23 -05002626 int err;
2627 /* The new mount record should have at least 2 refs to prevent it being
2628 * expired before we get a chance to add it
2629 */
Al Viro6776db3d2011-11-25 00:22:05 -05002630 BUG_ON(mnt_get_count(mnt) < 2);
Al Viro19a167a2011-01-17 01:35:23 -05002631
2632 if (m->mnt_sb == path->mnt->mnt_sb &&
2633 m->mnt_root == path->dentry) {
Al Virob1e75df2011-01-17 01:47:59 -05002634 err = -ELOOP;
2635 goto fail;
Al Viro19a167a2011-01-17 01:35:23 -05002636 }
2637
Al Viro95bc5f22011-11-25 00:30:56 -05002638 err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE);
Al Virob1e75df2011-01-17 01:47:59 -05002639 if (!err)
2640 return 0;
2641fail:
2642 /* remove m from any expiration list it may be on */
Al Viro6776db3d2011-11-25 00:22:05 -05002643 if (!list_empty(&mnt->mnt_expire)) {
Al Viro97216be2013-03-16 15:12:40 -04002644 namespace_lock();
Al Viro6776db3d2011-11-25 00:22:05 -05002645 list_del_init(&mnt->mnt_expire);
Al Viro97216be2013-03-16 15:12:40 -04002646 namespace_unlock();
Al Viro19a167a2011-01-17 01:35:23 -05002647 }
Al Virob1e75df2011-01-17 01:47:59 -05002648 mntput(m);
2649 mntput(m);
Al Viro19a167a2011-01-17 01:35:23 -05002650 return err;
2651}
2652
David Howellsea5b7782011-01-14 19:10:03 +00002653/**
2654 * mnt_set_expiry - Put a mount on an expiration list
2655 * @mnt: The mount to list.
2656 * @expiry_list: The list to add the mount to.
2657 */
2658void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
2659{
Al Viro97216be2013-03-16 15:12:40 -04002660 namespace_lock();
David Howellsea5b7782011-01-14 19:10:03 +00002661
Al Viro6776db3d2011-11-25 00:22:05 -05002662 list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list);
David Howellsea5b7782011-01-14 19:10:03 +00002663
Al Viro97216be2013-03-16 15:12:40 -04002664 namespace_unlock();
David Howellsea5b7782011-01-14 19:10:03 +00002665}
2666EXPORT_SYMBOL(mnt_set_expiry);
2667
2668/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 * process a list of expirable mountpoints with the intent of discarding any
2670 * mountpoints that aren't in use and haven't been touched since last we came
2671 * here
2672 */
2673void mark_mounts_for_expiry(struct list_head *mounts)
2674{
Al Viro761d5c32011-11-24 21:07:43 -05002675 struct mount *mnt, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 LIST_HEAD(graveyard);
2677
2678 if (list_empty(mounts))
2679 return;
2680
Al Viro97216be2013-03-16 15:12:40 -04002681 namespace_lock();
Al Viro719ea2f2013-09-29 11:24:49 -04002682 lock_mount_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683
2684 /* extract from the expiration list every vfsmount that matches the
2685 * following criteria:
2686 * - only referenced by its parent vfsmount
2687 * - still marked for expiry (marked on the last call here; marks are
2688 * cleared by mntput())
2689 */
Al Viro6776db3d2011-11-25 00:22:05 -05002690 list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
Al Viro863d6842011-11-25 00:57:42 -05002691 if (!xchg(&mnt->mnt_expiry_mark, 1) ||
Al Viro1ab59732011-11-24 21:35:16 -05002692 propagate_mount_busy(mnt, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 continue;
Al Viro6776db3d2011-11-25 00:22:05 -05002694 list_move(&mnt->mnt_expire, &graveyard);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 }
Al Virobcc5c7d2008-03-22 00:21:53 -04002696 while (!list_empty(&graveyard)) {
Al Viro6776db3d2011-11-25 00:22:05 -05002697 mnt = list_first_entry(&graveyard, struct mount, mnt_expire);
Al Viro143c8c92011-11-25 00:46:35 -05002698 touch_mnt_namespace(mnt->mnt_ns);
Eric W. Biedermane819f152014-12-24 07:20:01 -06002699 umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
Al Virobcc5c7d2008-03-22 00:21:53 -04002700 }
Al Viro719ea2f2013-09-29 11:24:49 -04002701 unlock_mount_hash();
Al Viro3ab6abe2013-03-16 14:42:19 -04002702 namespace_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703}
2704
2705EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
2706
2707/*
Trond Myklebust5528f9112006-06-09 09:34:17 -04002708 * Ripoff of 'select_parent()'
2709 *
2710 * search the list of submounts for a given mountpoint, and move any
2711 * shrinkable submounts to the 'graveyard' list.
2712 */
Al Viro692afc32011-11-24 21:15:14 -05002713static int select_submounts(struct mount *parent, struct list_head *graveyard)
Trond Myklebust5528f9112006-06-09 09:34:17 -04002714{
Al Viro692afc32011-11-24 21:15:14 -05002715 struct mount *this_parent = parent;
Trond Myklebust5528f9112006-06-09 09:34:17 -04002716 struct list_head *next;
2717 int found = 0;
2718
2719repeat:
Al Viro6b41d532011-11-24 23:24:33 -05002720 next = this_parent->mnt_mounts.next;
Trond Myklebust5528f9112006-06-09 09:34:17 -04002721resume:
Al Viro6b41d532011-11-24 23:24:33 -05002722 while (next != &this_parent->mnt_mounts) {
Trond Myklebust5528f9112006-06-09 09:34:17 -04002723 struct list_head *tmp = next;
Al Viro6b41d532011-11-24 23:24:33 -05002724 struct mount *mnt = list_entry(tmp, struct mount, mnt_child);
Trond Myklebust5528f9112006-06-09 09:34:17 -04002725
2726 next = tmp->next;
Al Viro692afc32011-11-24 21:15:14 -05002727 if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE))
Trond Myklebust5528f9112006-06-09 09:34:17 -04002728 continue;
2729 /*
2730 * Descend a level if the d_mounts list is non-empty.
2731 */
Al Viro6b41d532011-11-24 23:24:33 -05002732 if (!list_empty(&mnt->mnt_mounts)) {
Trond Myklebust5528f9112006-06-09 09:34:17 -04002733 this_parent = mnt;
2734 goto repeat;
2735 }
2736
Al Viro1ab59732011-11-24 21:35:16 -05002737 if (!propagate_mount_busy(mnt, 1)) {
Al Viro6776db3d2011-11-25 00:22:05 -05002738 list_move_tail(&mnt->mnt_expire, graveyard);
Trond Myklebust5528f9112006-06-09 09:34:17 -04002739 found++;
2740 }
2741 }
2742 /*
2743 * All done at this level ... ascend and resume the search
2744 */
2745 if (this_parent != parent) {
Al Viro6b41d532011-11-24 23:24:33 -05002746 next = this_parent->mnt_child.next;
Al Viro0714a532011-11-24 22:19:58 -05002747 this_parent = this_parent->mnt_parent;
Trond Myklebust5528f9112006-06-09 09:34:17 -04002748 goto resume;
2749 }
2750 return found;
2751}
2752
2753/*
2754 * process a list of expirable mountpoints with the intent of discarding any
2755 * submounts of a specific parent mountpoint
Nick Piggin99b7db72010-08-18 04:37:39 +10002756 *
Al Viro48a066e2013-09-29 22:06:07 -04002757 * mount_lock must be held for write
Trond Myklebust5528f9112006-06-09 09:34:17 -04002758 */
Al Virob54b9be2013-03-16 14:39:34 -04002759static void shrink_submounts(struct mount *mnt)
Trond Myklebust5528f9112006-06-09 09:34:17 -04002760{
2761 LIST_HEAD(graveyard);
Al Viro761d5c32011-11-24 21:07:43 -05002762 struct mount *m;
Trond Myklebust5528f9112006-06-09 09:34:17 -04002763
Trond Myklebust5528f9112006-06-09 09:34:17 -04002764 /* extract submounts of 'mountpoint' from the expiration list */
Al Viroc35038b2008-03-22 00:46:23 -04002765 while (select_submounts(mnt, &graveyard)) {
Al Virobcc5c7d2008-03-22 00:21:53 -04002766 while (!list_empty(&graveyard)) {
Al Viro761d5c32011-11-24 21:07:43 -05002767 m = list_first_entry(&graveyard, struct mount,
Al Viro6776db3d2011-11-25 00:22:05 -05002768 mnt_expire);
Al Viro143c8c92011-11-25 00:46:35 -05002769 touch_mnt_namespace(m->mnt_ns);
Eric W. Biedermane819f152014-12-24 07:20:01 -06002770 umount_tree(m, UMOUNT_PROPAGATE|UMOUNT_SYNC);
Al Virobcc5c7d2008-03-22 00:21:53 -04002771 }
2772 }
Trond Myklebust5528f9112006-06-09 09:34:17 -04002773}
2774
Trond Myklebust5528f9112006-06-09 09:34:17 -04002775/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 * Some copy_from_user() implementations do not return the exact number of
2777 * bytes remaining to copy on a fault. But copy_mount_options() requires that.
2778 * Note that this function differs from copy_from_user() in that it will oops
2779 * on bad values of `to', rather than returning a short copy.
2780 */
Ram Paib58fed82005-11-07 17:16:09 -05002781static long exact_copy_from_user(void *to, const void __user * from,
2782 unsigned long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783{
2784 char *t = to;
2785 const char __user *f = from;
2786 char c;
2787
2788 if (!access_ok(VERIFY_READ, from, n))
2789 return n;
2790
2791 while (n) {
2792 if (__get_user(c, f)) {
2793 memset(t, 0, n);
2794 break;
2795 }
2796 *t++ = c;
2797 f++;
2798 n--;
2799 }
2800 return n;
2801}
2802
Al Virob40ef862015-12-14 18:44:44 -05002803void *copy_mount_options(const void __user * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804{
2805 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 unsigned long size;
Al Virob40ef862015-12-14 18:44:44 -05002807 char *copy;
Ram Paib58fed82005-11-07 17:16:09 -05002808
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 if (!data)
Al Virob40ef862015-12-14 18:44:44 -05002810 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
Al Virob40ef862015-12-14 18:44:44 -05002812 copy = kmalloc(PAGE_SIZE, GFP_KERNEL);
2813 if (!copy)
2814 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815
2816 /* We only care that *some* data at the address the user
2817 * gave us is valid. Just in case, we'll zero
2818 * the remainder of the page.
2819 */
2820 /* copy_from_user cannot cross TASK_SIZE ! */
2821 size = TASK_SIZE - (unsigned long)data;
2822 if (size > PAGE_SIZE)
2823 size = PAGE_SIZE;
2824
Al Virob40ef862015-12-14 18:44:44 -05002825 i = size - exact_copy_from_user(copy, data, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 if (!i) {
Al Virob40ef862015-12-14 18:44:44 -05002827 kfree(copy);
2828 return ERR_PTR(-EFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 }
2830 if (i != PAGE_SIZE)
Al Virob40ef862015-12-14 18:44:44 -05002831 memset(copy + i, 0, PAGE_SIZE - i);
2832 return copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833}
2834
Tim Gardnerb8850d12014-08-28 11:26:03 -06002835char *copy_mount_string(const void __user *data)
Vegard Nossumeca6f532009-09-18 13:05:45 -07002836{
Tim Gardnerb8850d12014-08-28 11:26:03 -06002837 return data ? strndup_user(data, PAGE_SIZE) : NULL;
Vegard Nossumeca6f532009-09-18 13:05:45 -07002838}
2839
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840/*
2841 * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
2842 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
2843 *
2844 * data is a (void *) that can point to any structure up to
2845 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
2846 * information (or be NULL).
2847 *
2848 * Pre-0.97 versions of mount() didn't have a flags word.
2849 * When the flags word was introduced its top half was required
2850 * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
2851 * Therefore, if this magic number is present, it carries no information
2852 * and must be discarded.
2853 */
Seunghun Lee5e6123f2014-09-14 22:15:10 +09002854long do_mount(const char *dev_name, const char __user *dir_name,
Al Viro808d4e32012-10-11 11:42:01 -04002855 const char *type_page, unsigned long flags, void *data_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856{
Al Viro2d92ab32008-08-02 00:51:11 -04002857 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 int retval = 0;
2859 int mnt_flags = 0;
2860
2861 /* Discard magic */
2862 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
2863 flags &= ~MS_MGC_MSK;
2864
2865 /* Basic sanity checks */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 if (data_page)
2867 ((char *)data_page)[PAGE_SIZE - 1] = 0;
2868
Tetsuo Handaa27ab9f22009-10-04 21:49:49 +09002869 /* ... and get the mountpoint */
Seunghun Lee5e6123f2014-09-14 22:15:10 +09002870 retval = user_path(dir_name, &path);
Tetsuo Handaa27ab9f22009-10-04 21:49:49 +09002871 if (retval)
2872 return retval;
2873
2874 retval = security_sb_mount(dev_name, &path,
2875 type_page, flags, data_page);
Al Viro0d5cadb2013-05-04 14:40:51 -04002876 if (!retval && !may_mount())
2877 retval = -EPERM;
Jeff Layton9e8925b2015-11-16 09:49:34 -05002878 if (!retval && (flags & MS_MANDLOCK) && !may_mandlock())
2879 retval = -EPERM;
Tetsuo Handaa27ab9f22009-10-04 21:49:49 +09002880 if (retval)
2881 goto dput_out;
2882
Andi Kleen613cbe32009-04-19 18:40:43 +02002883 /* Default to relatime unless overriden */
2884 if (!(flags & MS_NOATIME))
2885 mnt_flags |= MNT_RELATIME;
Matthew Garrett0a1c01c2009-03-26 17:53:14 +00002886
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 /* Separate the per-mountpoint flags */
2888 if (flags & MS_NOSUID)
2889 mnt_flags |= MNT_NOSUID;
2890 if (flags & MS_NODEV)
2891 mnt_flags |= MNT_NODEV;
2892 if (flags & MS_NOEXEC)
2893 mnt_flags |= MNT_NOEXEC;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08002894 if (flags & MS_NOATIME)
2895 mnt_flags |= MNT_NOATIME;
2896 if (flags & MS_NODIRATIME)
2897 mnt_flags |= MNT_NODIRATIME;
Matthew Garrettd0adde52009-03-26 17:49:56 +00002898 if (flags & MS_STRICTATIME)
2899 mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08002900 if (flags & MS_RDONLY)
2901 mnt_flags |= MNT_READONLY;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08002902
Eric W. Biedermanffbc6f02014-07-28 17:36:04 -07002903 /* The default atime for remount is preservation */
2904 if ((flags & MS_REMOUNT) &&
2905 ((flags & (MS_NOATIME | MS_NODIRATIME | MS_RELATIME |
2906 MS_STRICTATIME)) == 0)) {
2907 mnt_flags &= ~MNT_ATIME_MASK;
2908 mnt_flags |= path.mnt->mnt_flags & MNT_ATIME_MASK;
2909 }
2910
Al Viro7a4dec52010-08-09 12:05:43 -04002911 flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN |
Matthew Garrettd0adde52009-03-26 17:49:56 +00002912 MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT |
Eric W. Biedermand3381fa2017-02-01 06:06:16 +13002913 MS_STRICTATIME | MS_NOREMOTELOCK | MS_SUBMOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 if (flags & MS_REMOUNT)
Al Viro2d92ab32008-08-02 00:51:11 -04002916 retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 data_page);
2918 else if (flags & MS_BIND)
Al Viro2d92ab32008-08-02 00:51:11 -04002919 retval = do_loopback(&path, dev_name, flags & MS_REC);
Ram Pai9676f0c2005-11-07 17:21:20 -05002920 else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
Al Viro2d92ab32008-08-02 00:51:11 -04002921 retval = do_change_type(&path, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 else if (flags & MS_MOVE)
Al Viro2d92ab32008-08-02 00:51:11 -04002923 retval = do_move_mount(&path, dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 else
Al Viro2d92ab32008-08-02 00:51:11 -04002925 retval = do_new_mount(&path, type_page, flags, mnt_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 dev_name, data_page);
2927dput_out:
Al Viro2d92ab32008-08-02 00:51:11 -04002928 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 return retval;
2930}
2931
Eric W. Biederman537f7cc2016-08-08 14:37:37 -05002932static struct ucounts *inc_mnt_namespaces(struct user_namespace *ns)
2933{
2934 return inc_ucount(ns, current_euid(), UCOUNT_MNT_NAMESPACES);
2935}
2936
2937static void dec_mnt_namespaces(struct ucounts *ucounts)
2938{
2939 dec_ucount(ucounts, UCOUNT_MNT_NAMESPACES);
2940}
2941
Eric W. Biederman771b1372012-07-26 21:08:32 -07002942static void free_mnt_ns(struct mnt_namespace *ns)
2943{
Al Viro6344c432014-11-01 00:45:45 -04002944 ns_free_inum(&ns->ns);
Eric W. Biederman537f7cc2016-08-08 14:37:37 -05002945 dec_mnt_namespaces(ns->ucounts);
Eric W. Biederman771b1372012-07-26 21:08:32 -07002946 put_user_ns(ns->user_ns);
2947 kfree(ns);
2948}
2949
Eric W. Biederman8823c072010-03-07 18:49:36 -08002950/*
2951 * Assign a sequence number so we can detect when we attempt to bind
2952 * mount a reference to an older mount namespace into the current
2953 * mount namespace, preventing reference counting loops. A 64bit
2954 * number incrementing at 10Ghz will take 12,427 years to wrap which
2955 * is effectively never, so we can ignore the possibility.
2956 */
2957static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1);
2958
Eric W. Biederman771b1372012-07-26 21:08:32 -07002959static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns)
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002960{
2961 struct mnt_namespace *new_ns;
Eric W. Biederman537f7cc2016-08-08 14:37:37 -05002962 struct ucounts *ucounts;
Eric W. Biederman98f842e2011-06-15 10:21:48 -07002963 int ret;
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002964
Eric W. Biederman537f7cc2016-08-08 14:37:37 -05002965 ucounts = inc_mnt_namespaces(user_ns);
2966 if (!ucounts)
Eric W. Biedermandf75e772016-09-22 13:08:36 -05002967 return ERR_PTR(-ENOSPC);
Eric W. Biederman537f7cc2016-08-08 14:37:37 -05002968
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002969 new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
Eric W. Biederman537f7cc2016-08-08 14:37:37 -05002970 if (!new_ns) {
2971 dec_mnt_namespaces(ucounts);
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002972 return ERR_PTR(-ENOMEM);
Eric W. Biederman537f7cc2016-08-08 14:37:37 -05002973 }
Al Viro6344c432014-11-01 00:45:45 -04002974 ret = ns_alloc_inum(&new_ns->ns);
Eric W. Biederman98f842e2011-06-15 10:21:48 -07002975 if (ret) {
2976 kfree(new_ns);
Eric W. Biederman537f7cc2016-08-08 14:37:37 -05002977 dec_mnt_namespaces(ucounts);
Eric W. Biederman98f842e2011-06-15 10:21:48 -07002978 return ERR_PTR(ret);
2979 }
Al Viro33c42942014-11-01 02:32:53 -04002980 new_ns->ns.ops = &mntns_operations;
Eric W. Biederman8823c072010-03-07 18:49:36 -08002981 new_ns->seq = atomic64_add_return(1, &mnt_ns_seq);
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002982 atomic_set(&new_ns->count, 1);
2983 new_ns->root = NULL;
2984 INIT_LIST_HEAD(&new_ns->list);
2985 init_waitqueue_head(&new_ns->poll);
2986 new_ns->event = 0;
Eric W. Biederman771b1372012-07-26 21:08:32 -07002987 new_ns->user_ns = get_user_ns(user_ns);
Eric W. Biederman537f7cc2016-08-08 14:37:37 -05002988 new_ns->ucounts = ucounts;
Eric W. Biedermand2921682016-09-28 00:27:17 -05002989 new_ns->mounts = 0;
2990 new_ns->pending_mounts = 0;
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04002991 return new_ns;
2992}
2993
Emese Revfy0766f782016-06-20 20:42:34 +02002994__latent_entropy
Al Viro9559f682013-09-28 20:47:57 -04002995struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
2996 struct user_namespace *user_ns, struct fs_struct *new_fs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997{
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08002998 struct mnt_namespace *new_ns;
Al Viro7f2da1e2008-05-10 20:44:54 -04002999 struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
Al Viro315fc832011-11-24 18:57:30 -05003000 struct mount *p, *q;
Al Viro9559f682013-09-28 20:47:57 -04003001 struct mount *old;
Al Virocb338d02011-11-24 20:55:08 -05003002 struct mount *new;
Eric W. Biederman7a472ef2012-07-31 13:13:04 -07003003 int copy_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
Al Viro9559f682013-09-28 20:47:57 -04003005 BUG_ON(!ns);
3006
3007 if (likely(!(flags & CLONE_NEWNS))) {
3008 get_mnt_ns(ns);
3009 return ns;
3010 }
3011
3012 old = ns->root;
3013
Eric W. Biederman771b1372012-07-26 21:08:32 -07003014 new_ns = alloc_mnt_ns(user_ns);
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04003015 if (IS_ERR(new_ns))
3016 return new_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017
Al Viro97216be2013-03-16 15:12:40 -04003018 namespace_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 /* First pass: copy the tree topology */
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07003020 copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE;
Al Viro9559f682013-09-28 20:47:57 -04003021 if (user_ns != ns->user_ns)
Eric W. Biederman132c94e2013-03-22 04:08:05 -07003022 copy_flags |= CL_SHARED_TO_SLAVE | CL_UNPRIVILEGED;
Eric W. Biederman7a472ef2012-07-31 13:13:04 -07003023 new = copy_tree(old, old->mnt.mnt_root, copy_flags);
David Howellsbe34d1a2012-06-25 12:55:18 +01003024 if (IS_ERR(new)) {
Al Viro328e6d92013-03-16 14:49:45 -04003025 namespace_unlock();
Eric W. Biederman771b1372012-07-26 21:08:32 -07003026 free_mnt_ns(new_ns);
David Howellsbe34d1a2012-06-25 12:55:18 +01003027 return ERR_CAST(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 }
Al Virobe08d6d2011-12-06 13:32:36 -05003029 new_ns->root = new;
Al Viro1a4eeaf2011-11-25 02:19:55 -05003030 list_add_tail(&new_ns->list, &new->mnt_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
3032 /*
3033 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
3034 * as belonging to new namespace. We have already acquired a private
3035 * fs_struct, so tsk->fs->lock is not needed.
3036 */
Al Viro909b0a82011-11-25 03:06:56 -05003037 p = old;
Al Virocb338d02011-11-24 20:55:08 -05003038 q = new;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 while (p) {
Al Viro143c8c92011-11-25 00:46:35 -05003040 q->mnt_ns = new_ns;
Eric W. Biedermand2921682016-09-28 00:27:17 -05003041 new_ns->mounts++;
Al Viro9559f682013-09-28 20:47:57 -04003042 if (new_fs) {
3043 if (&p->mnt == new_fs->root.mnt) {
3044 new_fs->root.mnt = mntget(&q->mnt);
Al Viro315fc832011-11-24 18:57:30 -05003045 rootmnt = &p->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 }
Al Viro9559f682013-09-28 20:47:57 -04003047 if (&p->mnt == new_fs->pwd.mnt) {
3048 new_fs->pwd.mnt = mntget(&q->mnt);
Al Viro315fc832011-11-24 18:57:30 -05003049 pwdmnt = &p->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 }
Al Viro909b0a82011-11-25 03:06:56 -05003052 p = next_mnt(p, old);
3053 q = next_mnt(q, new);
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07003054 if (!q)
3055 break;
3056 while (p->mnt.mnt_root != q->mnt.mnt_root)
3057 p = next_mnt(p, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 }
Al Viro328e6d92013-03-16 14:49:45 -04003059 namespace_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 if (rootmnt)
Al Virof03c6592011-01-14 22:30:21 -05003062 mntput(rootmnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 if (pwdmnt)
Al Virof03c6592011-01-14 22:30:21 -05003064 mntput(pwdmnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065
JANAK DESAI741a2952006-02-07 12:59:00 -08003066 return new_ns;
3067}
3068
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04003069/**
3070 * create_mnt_ns - creates a private namespace and adds a root filesystem
3071 * @mnt: pointer to the new root filesystem mountpoint
3072 */
Al Viro1a4eeaf2011-11-25 02:19:55 -05003073static struct mnt_namespace *create_mnt_ns(struct vfsmount *m)
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04003074{
Eric W. Biederman771b1372012-07-26 21:08:32 -07003075 struct mnt_namespace *new_ns = alloc_mnt_ns(&init_user_ns);
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04003076 if (!IS_ERR(new_ns)) {
Al Viro1a4eeaf2011-11-25 02:19:55 -05003077 struct mount *mnt = real_mount(m);
3078 mnt->mnt_ns = new_ns;
Al Virobe08d6d2011-12-06 13:32:36 -05003079 new_ns->root = mnt;
Eric W. Biedermand2921682016-09-28 00:27:17 -05003080 new_ns->mounts++;
Al Virob1983cd2013-05-04 15:18:53 -04003081 list_add(&mnt->mnt_list, &new_ns->list);
Al Viroc1334492011-11-16 16:12:14 -05003082 } else {
Al Viro1a4eeaf2011-11-25 02:19:55 -05003083 mntput(m);
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04003084 }
3085 return new_ns;
3086}
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04003087
Al Viroea441d12011-11-16 21:43:59 -05003088struct dentry *mount_subtree(struct vfsmount *mnt, const char *name)
3089{
3090 struct mnt_namespace *ns;
Al Virod31da0f2011-11-22 12:31:21 -05003091 struct super_block *s;
Al Viroea441d12011-11-16 21:43:59 -05003092 struct path path;
3093 int err;
3094
3095 ns = create_mnt_ns(mnt);
3096 if (IS_ERR(ns))
3097 return ERR_CAST(ns);
3098
3099 err = vfs_path_lookup(mnt->mnt_root, mnt,
3100 name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
3101
3102 put_mnt_ns(ns);
3103
3104 if (err)
3105 return ERR_PTR(err);
3106
3107 /* trade a vfsmount reference for active sb one */
Al Virod31da0f2011-11-22 12:31:21 -05003108 s = path.mnt->mnt_sb;
3109 atomic_inc(&s->s_active);
Al Viroea441d12011-11-16 21:43:59 -05003110 mntput(path.mnt);
3111 /* lock the sucker */
Al Virod31da0f2011-11-22 12:31:21 -05003112 down_write(&s->s_umount);
Al Viroea441d12011-11-16 21:43:59 -05003113 /* ... and return the root of (sub)tree on it */
3114 return path.dentry;
3115}
3116EXPORT_SYMBOL(mount_subtree);
3117
Heiko Carstensbdc480e2009-01-14 14:14:12 +01003118SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
3119 char __user *, type, unsigned long, flags, void __user *, data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120{
Vegard Nossumeca6f532009-09-18 13:05:45 -07003121 int ret;
3122 char *kernel_type;
Vegard Nossumeca6f532009-09-18 13:05:45 -07003123 char *kernel_dev;
Al Virob40ef862015-12-14 18:44:44 -05003124 void *options;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125
Tim Gardnerb8850d12014-08-28 11:26:03 -06003126 kernel_type = copy_mount_string(type);
3127 ret = PTR_ERR(kernel_type);
3128 if (IS_ERR(kernel_type))
Vegard Nossumeca6f532009-09-18 13:05:45 -07003129 goto out_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130
Tim Gardnerb8850d12014-08-28 11:26:03 -06003131 kernel_dev = copy_mount_string(dev_name);
3132 ret = PTR_ERR(kernel_dev);
3133 if (IS_ERR(kernel_dev))
Vegard Nossumeca6f532009-09-18 13:05:45 -07003134 goto out_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135
Al Virob40ef862015-12-14 18:44:44 -05003136 options = copy_mount_options(data);
3137 ret = PTR_ERR(options);
3138 if (IS_ERR(options))
Vegard Nossumeca6f532009-09-18 13:05:45 -07003139 goto out_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140
Al Virob40ef862015-12-14 18:44:44 -05003141 ret = do_mount(kernel_dev, dir_name, kernel_type, flags, options);
Vegard Nossumeca6f532009-09-18 13:05:45 -07003142
Al Virob40ef862015-12-14 18:44:44 -05003143 kfree(options);
Vegard Nossumeca6f532009-09-18 13:05:45 -07003144out_data:
3145 kfree(kernel_dev);
3146out_dev:
Vegard Nossumeca6f532009-09-18 13:05:45 -07003147 kfree(kernel_type);
3148out_type:
3149 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150}
3151
3152/*
Al Viroafac7cb2011-11-23 19:34:49 -05003153 * Return true if path is reachable from root
3154 *
Al Viro48a066e2013-09-29 22:06:07 -04003155 * namespace_sem or mount_lock is held
Al Viroafac7cb2011-11-23 19:34:49 -05003156 */
Al Viro643822b2011-11-24 22:00:28 -05003157bool is_path_reachable(struct mount *mnt, struct dentry *dentry,
Al Viroafac7cb2011-11-23 19:34:49 -05003158 const struct path *root)
3159{
Al Viro643822b2011-11-24 22:00:28 -05003160 while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) {
Al Viroa73324d2011-11-24 22:25:07 -05003161 dentry = mnt->mnt_mountpoint;
Al Viro0714a532011-11-24 22:19:58 -05003162 mnt = mnt->mnt_parent;
Al Viroafac7cb2011-11-23 19:34:49 -05003163 }
Al Viro643822b2011-11-24 22:00:28 -05003164 return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry);
Al Viroafac7cb2011-11-23 19:34:49 -05003165}
3166
Yaowei Bai25ab4c92015-11-17 14:40:10 +08003167bool path_is_under(struct path *path1, struct path *path2)
Al Viroafac7cb2011-11-23 19:34:49 -05003168{
Yaowei Bai25ab4c92015-11-17 14:40:10 +08003169 bool res;
Al Viro48a066e2013-09-29 22:06:07 -04003170 read_seqlock_excl(&mount_lock);
Al Viro643822b2011-11-24 22:00:28 -05003171 res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2);
Al Viro48a066e2013-09-29 22:06:07 -04003172 read_sequnlock_excl(&mount_lock);
Al Viroafac7cb2011-11-23 19:34:49 -05003173 return res;
3174}
3175EXPORT_SYMBOL(path_is_under);
3176
3177/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 * pivot_root Semantics:
3179 * Moves the root file system of the current process to the directory put_old,
3180 * makes new_root as the new root file system of the current process, and sets
3181 * root/cwd of all processes which had them on the current root to new_root.
3182 *
3183 * Restrictions:
3184 * The new_root and put_old must be directories, and must not be on the
3185 * same file system as the current process root. The put_old must be
3186 * underneath new_root, i.e. adding a non-zero number of /.. to the string
3187 * pointed to by put_old must yield the same directory as new_root. No other
3188 * file system may be mounted on put_old. After all, new_root is a mountpoint.
3189 *
Neil Brown4a0d11f2006-01-08 01:03:18 -08003190 * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
3191 * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
3192 * in this situation.
3193 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 * Notes:
3195 * - we don't move root/cwd if they are not at the root (reason: if something
3196 * cared enough to change them, it's probably wrong to force them elsewhere)
3197 * - it's okay to pick a root that isn't the root of a file system, e.g.
3198 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
3199 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
3200 * first.
3201 */
Heiko Carstens3480b252009-01-14 14:14:16 +01003202SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
3203 const char __user *, put_old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204{
Al Viro2d8f3032008-07-22 09:59:21 -04003205 struct path new, old, parent_path, root_parent, root;
Al Viro84d17192013-03-15 10:53:28 -04003206 struct mount *new_mnt, *root_mnt, *old_mnt;
3207 struct mountpoint *old_mp, *root_mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 int error;
3209
Al Viro9b40bc92013-02-22 22:45:42 -05003210 if (!may_mount())
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 return -EPERM;
3212
Al Viro2d8f3032008-07-22 09:59:21 -04003213 error = user_path_dir(new_root, &new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 if (error)
3215 goto out0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216
Al Viro2d8f3032008-07-22 09:59:21 -04003217 error = user_path_dir(put_old, &old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 if (error)
3219 goto out1;
3220
Al Viro2d8f3032008-07-22 09:59:21 -04003221 error = security_sb_pivotroot(&old, &new);
Al Virob12cea92011-03-18 08:55:38 -04003222 if (error)
3223 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02003225 get_fs_root(current->fs, &root);
Al Viro84d17192013-03-15 10:53:28 -04003226 old_mp = lock_mount(&old);
3227 error = PTR_ERR(old_mp);
3228 if (IS_ERR(old_mp))
Al Virob12cea92011-03-18 08:55:38 -04003229 goto out3;
3230
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 error = -EINVAL;
Al Viro419148d2011-11-24 19:41:16 -05003232 new_mnt = real_mount(new.mnt);
3233 root_mnt = real_mount(root.mnt);
Al Viro84d17192013-03-15 10:53:28 -04003234 old_mnt = real_mount(old.mnt);
3235 if (IS_MNT_SHARED(old_mnt) ||
Al Virofc7be132011-11-25 01:05:37 -05003236 IS_MNT_SHARED(new_mnt->mnt_parent) ||
3237 IS_MNT_SHARED(root_mnt->mnt_parent))
Al Virob12cea92011-03-18 08:55:38 -04003238 goto out4;
Al Viro143c8c92011-11-25 00:46:35 -05003239 if (!check_mnt(root_mnt) || !check_mnt(new_mnt))
Al Virob12cea92011-03-18 08:55:38 -04003240 goto out4;
Eric W. Biederman5ff9d8a2013-03-29 21:04:39 -07003241 if (new_mnt->mnt.mnt_flags & MNT_LOCKED)
3242 goto out4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 error = -ENOENT;
Alexey Dobriyanf3da3922009-05-04 03:32:03 +04003244 if (d_unlinked(new.dentry))
Al Virob12cea92011-03-18 08:55:38 -04003245 goto out4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 error = -EBUSY;
Al Viro84d17192013-03-15 10:53:28 -04003247 if (new_mnt == root_mnt || old_mnt == root_mnt)
Al Virob12cea92011-03-18 08:55:38 -04003248 goto out4; /* loop, on the same file system */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 error = -EINVAL;
Al Viro8c3ee422008-03-22 18:00:39 -04003250 if (root.mnt->mnt_root != root.dentry)
Al Virob12cea92011-03-18 08:55:38 -04003251 goto out4; /* not a mountpoint */
Al Viro676da582011-11-24 21:47:05 -05003252 if (!mnt_has_parent(root_mnt))
Al Virob12cea92011-03-18 08:55:38 -04003253 goto out4; /* not attached */
Al Viro84d17192013-03-15 10:53:28 -04003254 root_mp = root_mnt->mnt_mp;
Al Viro2d8f3032008-07-22 09:59:21 -04003255 if (new.mnt->mnt_root != new.dentry)
Al Virob12cea92011-03-18 08:55:38 -04003256 goto out4; /* not a mountpoint */
Al Viro676da582011-11-24 21:47:05 -05003257 if (!mnt_has_parent(new_mnt))
Al Virob12cea92011-03-18 08:55:38 -04003258 goto out4; /* not attached */
Jan Blunck4ac91372008-02-14 19:34:32 -08003259 /* make sure we can reach put_old from new_root */
Al Viro84d17192013-03-15 10:53:28 -04003260 if (!is_path_reachable(old_mnt, old.dentry, &new))
Al Virob12cea92011-03-18 08:55:38 -04003261 goto out4;
Eric W. Biederman0d082602014-10-08 10:42:27 -07003262 /* make certain new is below the root */
3263 if (!is_path_reachable(new_mnt, new.dentry, &root))
3264 goto out4;
Al Viro719ea2f2013-09-29 11:24:49 -04003265 lock_mount_hash();
Piotr Krysiuk91e99792020-04-27 11:34:12 +01003266 root_mp->m_count++; /* pin it so it won't go away */
Al Viro419148d2011-11-24 19:41:16 -05003267 detach_mnt(new_mnt, &parent_path);
3268 detach_mnt(root_mnt, &root_parent);
Eric W. Biederman5ff9d8a2013-03-29 21:04:39 -07003269 if (root_mnt->mnt.mnt_flags & MNT_LOCKED) {
3270 new_mnt->mnt.mnt_flags |= MNT_LOCKED;
3271 root_mnt->mnt.mnt_flags &= ~MNT_LOCKED;
3272 }
Jan Blunck4ac91372008-02-14 19:34:32 -08003273 /* mount old root on put_old */
Al Viro84d17192013-03-15 10:53:28 -04003274 attach_mnt(root_mnt, old_mnt, old_mp);
Jan Blunck4ac91372008-02-14 19:34:32 -08003275 /* mount new_root on / */
Al Viro84d17192013-03-15 10:53:28 -04003276 attach_mnt(new_mnt, real_mount(root_parent.mnt), root_mp);
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08003277 touch_mnt_namespace(current->nsproxy->mnt_ns);
Eric W. Biederman4fed6552014-10-08 10:42:57 -07003278 /* A moved mount should not expire automatically */
3279 list_del_init(&new_mnt->mnt_expire);
Eric W. Biederman1a62a0f2017-01-03 14:18:43 +13003280 put_mountpoint(root_mp);
Al Viro719ea2f2013-09-29 11:24:49 -04003281 unlock_mount_hash();
Al Viro2d8f3032008-07-22 09:59:21 -04003282 chroot_fs_refs(&root, &new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 error = 0;
Al Virob12cea92011-03-18 08:55:38 -04003284out4:
Al Viro84d17192013-03-15 10:53:28 -04003285 unlock_mount(old_mp);
Al Virob12cea92011-03-18 08:55:38 -04003286 if (!error) {
3287 path_put(&root_parent);
3288 path_put(&parent_path);
3289 }
3290out3:
Al Viro8c3ee422008-03-22 18:00:39 -04003291 path_put(&root);
Al Virob12cea92011-03-18 08:55:38 -04003292out2:
Al Viro2d8f3032008-07-22 09:59:21 -04003293 path_put(&old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294out1:
Al Viro2d8f3032008-07-22 09:59:21 -04003295 path_put(&new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296out0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298}
3299
3300static void __init init_mount_tree(void)
3301{
3302 struct vfsmount *mnt;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08003303 struct mnt_namespace *ns;
Jan Blunckac748a02008-02-14 19:34:39 -08003304 struct path root;
Eric W. Biederman0c55cfc2012-07-26 21:42:03 -07003305 struct file_system_type *type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306
Eric W. Biederman0c55cfc2012-07-26 21:42:03 -07003307 type = get_fs_type("rootfs");
3308 if (!type)
3309 panic("Can't find rootfs type");
3310 mnt = vfs_kern_mount(type, 0, "rootfs", NULL);
3311 put_filesystem(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 if (IS_ERR(mnt))
3313 panic("Can't create rootfs");
Nick Pigginb3e19d92011-01-07 17:50:11 +11003314
Trond Myklebust3b22edc2009-06-23 17:29:49 -04003315 ns = create_mnt_ns(mnt);
3316 if (IS_ERR(ns))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 panic("Can't allocate initial namespace");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08003319 init_task.nsproxy->mnt_ns = ns;
3320 get_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321
Al Virobe08d6d2011-12-06 13:32:36 -05003322 root.mnt = mnt;
3323 root.dentry = mnt->mnt_root;
Eric W. Biedermanda362b02014-10-07 12:19:53 -07003324 mnt->mnt_flags |= MNT_LOCKED;
Jan Blunckac748a02008-02-14 19:34:39 -08003325
3326 set_fs_pwd(current->fs, &root);
3327 set_fs_root(current->fs, &root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328}
3329
Denis Cheng74bf17c2007-10-16 23:26:30 -07003330void __init mnt_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331{
Eric Dumazet13f14b42008-02-06 01:37:57 -08003332 unsigned u;
Randy Dunlap15a67dd2006-09-29 01:58:57 -07003333 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334
Al Viro7d6fec42011-11-23 12:14:10 -05003335 mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
Paul Mundt20c2df82007-07-20 10:11:58 +09003336 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337
Al Viro0818bf22014-02-28 13:46:44 -05003338 mount_hashtable = alloc_large_system_hash("Mount-cache",
Al Viro38129a12014-03-20 21:10:51 -04003339 sizeof(struct hlist_head),
Al Viro0818bf22014-02-28 13:46:44 -05003340 mhash_entries, 19,
3341 0,
3342 &m_hash_shift, &m_hash_mask, 0, 0);
3343 mountpoint_hashtable = alloc_large_system_hash("Mountpoint-cache",
3344 sizeof(struct hlist_head),
3345 mphash_entries, 19,
3346 0,
3347 &mp_hash_shift, &mp_hash_mask, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348
Al Viro84d17192013-03-15 10:53:28 -04003349 if (!mount_hashtable || !mountpoint_hashtable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 panic("Failed to allocate mount hash table\n");
3351
Al Viro0818bf22014-02-28 13:46:44 -05003352 for (u = 0; u <= m_hash_mask; u++)
Al Viro38129a12014-03-20 21:10:51 -04003353 INIT_HLIST_HEAD(&mount_hashtable[u]);
Al Viro0818bf22014-02-28 13:46:44 -05003354 for (u = 0; u <= mp_hash_mask; u++)
3355 INIT_HLIST_HEAD(&mountpoint_hashtable[u]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356
Tejun Heo4b93dc92013-11-28 14:54:43 -05003357 kernfs_init();
3358
Randy Dunlap15a67dd2006-09-29 01:58:57 -07003359 err = sysfs_init();
3360 if (err)
3361 printk(KERN_WARNING "%s: sysfs_init error: %d\n",
Harvey Harrison8e24eea2008-04-30 00:55:09 -07003362 __func__, err);
Greg Kroah-Hartman00d26662007-10-29 14:17:23 -06003363 fs_kobj = kobject_create_and_add("fs", NULL);
3364 if (!fs_kobj)
Harvey Harrison8e24eea2008-04-30 00:55:09 -07003365 printk(KERN_WARNING "%s: kobj create error\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 init_rootfs();
3367 init_mount_tree();
3368}
3369
Trond Myklebust616511d2009-06-22 15:09:13 -04003370void put_mnt_ns(struct mnt_namespace *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371{
Al Virod498b252010-02-05 02:21:06 -05003372 if (!atomic_dec_and_test(&ns->count))
Trond Myklebust616511d2009-06-22 15:09:13 -04003373 return;
Al Viro7b00ed62013-09-16 21:19:20 -04003374 drop_collected_mounts(&ns->root->mnt);
Eric W. Biederman771b1372012-07-26 21:08:32 -07003375 free_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376}
Al Viro9d412a42011-03-17 22:08:28 -04003377
3378struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
3379{
Tim Chen423e0ab2011-07-19 09:32:38 -07003380 struct vfsmount *mnt;
3381 mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
3382 if (!IS_ERR(mnt)) {
3383 /*
3384 * it is a longterm mount, don't release mnt until
3385 * we unmount before file sys is unregistered
3386 */
Al Virof7a99c52012-06-09 00:59:08 -04003387 real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL;
Tim Chen423e0ab2011-07-19 09:32:38 -07003388 }
3389 return mnt;
Al Viro9d412a42011-03-17 22:08:28 -04003390}
3391EXPORT_SYMBOL_GPL(kern_mount_data);
Tim Chen423e0ab2011-07-19 09:32:38 -07003392
3393void kern_unmount(struct vfsmount *mnt)
3394{
3395 /* release long term mount so mount point can be released */
3396 if (!IS_ERR_OR_NULL(mnt)) {
Al Virof7a99c52012-06-09 00:59:08 -04003397 real_mount(mnt)->mnt_ns = NULL;
Al Viro48a066e2013-09-29 22:06:07 -04003398 synchronize_rcu(); /* yecchhh... */
Tim Chen423e0ab2011-07-19 09:32:38 -07003399 mntput(mnt);
3400 }
3401}
3402EXPORT_SYMBOL(kern_unmount);
Al Viro02125a82011-12-05 08:43:34 -05003403
3404bool our_mnt(struct vfsmount *mnt)
3405{
Al Viro143c8c92011-11-25 00:46:35 -05003406 return check_mnt(real_mount(mnt));
Al Viro02125a82011-12-05 08:43:34 -05003407}
Eric W. Biederman8823c072010-03-07 18:49:36 -08003408
Eric W. Biederman31515272013-03-15 01:45:51 -07003409bool current_chrooted(void)
3410{
3411 /* Does the current process have a non-standard root */
3412 struct path ns_root;
3413 struct path fs_root;
3414 bool chrooted;
3415
3416 /* Find the namespace root */
3417 ns_root.mnt = &current->nsproxy->mnt_ns->root->mnt;
3418 ns_root.dentry = ns_root.mnt->mnt_root;
3419 path_get(&ns_root);
3420 while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root))
3421 ;
3422
3423 get_fs_root(current->fs, &fs_root);
3424
3425 chrooted = !path_equal(&fs_root, &ns_root);
3426
3427 path_put(&fs_root);
3428 path_put(&ns_root);
3429
3430 return chrooted;
3431}
3432
Eric W. Biederman8654df42016-06-09 16:06:06 -05003433static bool mnt_already_visible(struct mnt_namespace *ns, struct vfsmount *new,
3434 int *new_mnt_flags)
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -07003435{
Eric W. Biederman8c6cf9c2015-05-08 23:49:47 -05003436 int new_flags = *new_mnt_flags;
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -07003437 struct mount *mnt;
Eric W. Biedermane51db732013-03-30 19:57:41 -07003438 bool visible = false;
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -07003439
Al Viro44bb4382013-09-16 21:37:36 -04003440 down_read(&namespace_sem);
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -07003441 list_for_each_entry(mnt, &ns->list, mnt_list) {
Eric W. Biedermane51db732013-03-30 19:57:41 -07003442 struct mount *child;
Eric W. Biederman77b1a972015-06-04 09:43:11 -05003443 int mnt_flags;
3444
Eric W. Biederman8654df42016-06-09 16:06:06 -05003445 if (mnt->mnt.mnt_sb->s_type != new->mnt_sb->s_type)
Eric W. Biedermane51db732013-03-30 19:57:41 -07003446 continue;
3447
Eric W. Biederman7e96c1b2015-05-08 16:36:50 -05003448 /* This mount is not fully visible if it's root directory
3449 * is not the root directory of the filesystem.
3450 */
3451 if (mnt->mnt.mnt_root != mnt->mnt.mnt_sb->s_root)
3452 continue;
3453
Eric W. Biedermana1935c12016-06-15 06:59:49 -05003454 /* A local view of the mount flags */
Eric W. Biederman77b1a972015-06-04 09:43:11 -05003455 mnt_flags = mnt->mnt.mnt_flags;
Eric W. Biederman77b1a972015-06-04 09:43:11 -05003456
Eric W. Biederman695e9df2016-06-10 12:21:40 -05003457 /* Don't miss readonly hidden in the superblock flags */
3458 if (mnt->mnt.mnt_sb->s_flags & MS_RDONLY)
3459 mnt_flags |= MNT_LOCK_READONLY;
3460
Eric W. Biederman8c6cf9c2015-05-08 23:49:47 -05003461 /* Verify the mount flags are equal to or more permissive
3462 * than the proposed new mount.
3463 */
Eric W. Biederman77b1a972015-06-04 09:43:11 -05003464 if ((mnt_flags & MNT_LOCK_READONLY) &&
Eric W. Biederman8c6cf9c2015-05-08 23:49:47 -05003465 !(new_flags & MNT_READONLY))
3466 continue;
Eric W. Biederman77b1a972015-06-04 09:43:11 -05003467 if ((mnt_flags & MNT_LOCK_ATIME) &&
3468 ((mnt_flags & MNT_ATIME_MASK) != (new_flags & MNT_ATIME_MASK)))
Eric W. Biederman8c6cf9c2015-05-08 23:49:47 -05003469 continue;
3470
Eric W. Biedermanceeb0e52015-01-07 08:10:09 -06003471 /* This mount is not fully visible if there are any
3472 * locked child mounts that cover anything except for
3473 * empty directories.
Eric W. Biedermane51db732013-03-30 19:57:41 -07003474 */
3475 list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
3476 struct inode *inode = child->mnt_mountpoint->d_inode;
Eric W. Biedermanceeb0e52015-01-07 08:10:09 -06003477 /* Only worry about locked mounts */
Eric W. Biedermand71ed6c2016-05-27 14:50:05 -05003478 if (!(child->mnt.mnt_flags & MNT_LOCKED))
Eric W. Biedermanceeb0e52015-01-07 08:10:09 -06003479 continue;
Eric W. Biederman7236c852015-05-13 20:51:09 -05003480 /* Is the directory permanetly empty? */
3481 if (!is_empty_dir_inode(inode))
Eric W. Biedermane51db732013-03-30 19:57:41 -07003482 goto next;
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -07003483 }
Eric W. Biederman8c6cf9c2015-05-08 23:49:47 -05003484 /* Preserve the locked attributes */
Eric W. Biederman77b1a972015-06-04 09:43:11 -05003485 *new_mnt_flags |= mnt_flags & (MNT_LOCK_READONLY | \
Eric W. Biederman77b1a972015-06-04 09:43:11 -05003486 MNT_LOCK_ATIME);
Eric W. Biedermane51db732013-03-30 19:57:41 -07003487 visible = true;
3488 goto found;
3489 next: ;
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -07003490 }
Eric W. Biedermane51db732013-03-30 19:57:41 -07003491found:
Al Viro44bb4382013-09-16 21:37:36 -04003492 up_read(&namespace_sem);
Eric W. Biedermane51db732013-03-30 19:57:41 -07003493 return visible;
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -07003494}
3495
Eric W. Biederman8654df42016-06-09 16:06:06 -05003496static bool mount_too_revealing(struct vfsmount *mnt, int *new_mnt_flags)
3497{
Eric W. Biedermana1935c12016-06-15 06:59:49 -05003498 const unsigned long required_iflags = SB_I_NOEXEC | SB_I_NODEV;
Eric W. Biederman8654df42016-06-09 16:06:06 -05003499 struct mnt_namespace *ns = current->nsproxy->mnt_ns;
3500 unsigned long s_iflags;
3501
3502 if (ns->user_ns == &init_user_ns)
3503 return false;
3504
3505 /* Can this filesystem be too revealing? */
3506 s_iflags = mnt->mnt_sb->s_iflags;
3507 if (!(s_iflags & SB_I_USERNS_VISIBLE))
3508 return false;
3509
Eric W. Biedermana1935c12016-06-15 06:59:49 -05003510 if ((s_iflags & required_iflags) != required_iflags) {
3511 WARN_ONCE(1, "Expected s_iflags to contain 0x%lx\n",
3512 required_iflags);
3513 return true;
3514 }
3515
Eric W. Biederman8654df42016-06-09 16:06:06 -05003516 return !mnt_already_visible(ns, mnt, new_mnt_flags);
3517}
3518
Andy Lutomirski380cf5b2016-06-23 16:41:05 -05003519bool mnt_may_suid(struct vfsmount *mnt)
3520{
3521 /*
3522 * Foreign mounts (accessed via fchdir or through /proc
3523 * symlinks) are always treated as if they are nosuid. This
3524 * prevents namespaces from trusting potentially unsafe
3525 * suid/sgid bits, file caps, or security labels that originate
3526 * in other namespaces.
3527 */
3528 return !(mnt->mnt_flags & MNT_NOSUID) && check_mnt(real_mount(mnt)) &&
3529 current_in_userns(mnt->mnt_sb->s_user_ns);
3530}
3531
Al Viro64964522014-11-01 00:37:32 -04003532static struct ns_common *mntns_get(struct task_struct *task)
Eric W. Biederman8823c072010-03-07 18:49:36 -08003533{
Al Viro58be28252014-11-01 00:00:23 -04003534 struct ns_common *ns = NULL;
Eric W. Biederman8823c072010-03-07 18:49:36 -08003535 struct nsproxy *nsproxy;
3536
Eric W. Biederman728dba32014-02-03 19:13:49 -08003537 task_lock(task);
3538 nsproxy = task->nsproxy;
Eric W. Biederman8823c072010-03-07 18:49:36 -08003539 if (nsproxy) {
Al Viro58be28252014-11-01 00:00:23 -04003540 ns = &nsproxy->mnt_ns->ns;
3541 get_mnt_ns(to_mnt_ns(ns));
Eric W. Biederman8823c072010-03-07 18:49:36 -08003542 }
Eric W. Biederman728dba32014-02-03 19:13:49 -08003543 task_unlock(task);
Eric W. Biederman8823c072010-03-07 18:49:36 -08003544
3545 return ns;
3546}
3547
Al Viro64964522014-11-01 00:37:32 -04003548static void mntns_put(struct ns_common *ns)
Eric W. Biederman8823c072010-03-07 18:49:36 -08003549{
Al Viro58be28252014-11-01 00:00:23 -04003550 put_mnt_ns(to_mnt_ns(ns));
Eric W. Biederman8823c072010-03-07 18:49:36 -08003551}
3552
Al Viro64964522014-11-01 00:37:32 -04003553static int mntns_install(struct nsproxy *nsproxy, struct ns_common *ns)
Eric W. Biederman8823c072010-03-07 18:49:36 -08003554{
3555 struct fs_struct *fs = current->fs;
Al Viro58be28252014-11-01 00:00:23 -04003556 struct mnt_namespace *mnt_ns = to_mnt_ns(ns);
Eric W. Biederman8823c072010-03-07 18:49:36 -08003557 struct path root;
3558
Eric W. Biederman0c55cfc2012-07-26 21:42:03 -07003559 if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) ||
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -07003560 !ns_capable(current_user_ns(), CAP_SYS_CHROOT) ||
3561 !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
Zhao Hongjiangae11e0f2012-09-13 16:38:03 +08003562 return -EPERM;
Eric W. Biederman8823c072010-03-07 18:49:36 -08003563
3564 if (fs->users != 1)
3565 return -EINVAL;
3566
3567 get_mnt_ns(mnt_ns);
3568 put_mnt_ns(nsproxy->mnt_ns);
3569 nsproxy->mnt_ns = mnt_ns;
3570
3571 /* Find the root */
3572 root.mnt = &mnt_ns->root->mnt;
3573 root.dentry = mnt_ns->root->mnt.mnt_root;
3574 path_get(&root);
3575 while(d_mountpoint(root.dentry) && follow_down_one(&root))
3576 ;
3577
3578 /* Update the pwd and root */
3579 set_fs_pwd(fs, &root);
3580 set_fs_root(fs, &root);
3581
3582 path_put(&root);
3583 return 0;
3584}
3585
Andrey Vaginbcac25a2016-09-06 00:47:13 -07003586static struct user_namespace *mntns_owner(struct ns_common *ns)
3587{
3588 return to_mnt_ns(ns)->user_ns;
3589}
3590
Eric W. Biederman8823c072010-03-07 18:49:36 -08003591const struct proc_ns_operations mntns_operations = {
3592 .name = "mnt",
3593 .type = CLONE_NEWNS,
3594 .get = mntns_get,
3595 .put = mntns_put,
3596 .install = mntns_install,
Andrey Vaginbcac25a2016-09-06 00:47:13 -07003597 .owner = mntns_owner,
Eric W. Biederman8823c072010-03-07 18:49:36 -08003598};