]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - fs/namespace.c
vfs: Introduce fs_context, switch vfs_kern_mount() to it.
[mirror_ubuntu-focal-kernel.git] / fs / namespace.c
CommitLineData
1da177e4
LT
1/*
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
1da177e4 11#include <linux/syscalls.h>
d10577a8 12#include <linux/export.h>
16f7e0fe 13#include <linux/capability.h>
6b3286ed 14#include <linux/mnt_namespace.h>
771b1371 15#include <linux/user_namespace.h>
1da177e4
LT
16#include <linux/namei.h>
17#include <linux/security.h>
5b825c3a 18#include <linux/cred.h>
73cd49ec 19#include <linux/idr.h>
57f150a5 20#include <linux/init.h> /* init_rootfs */
d10577a8
AV
21#include <linux/fs_struct.h> /* get_fs_root et.al. */
22#include <linux/fsnotify.h> /* fsnotify_vfsmount_delete */
23#include <linux/uaccess.h>
0bb80f24 24#include <linux/proc_ns.h>
20b4fb48 25#include <linux/magic.h>
57c8a661 26#include <linux/memblock.h>
9ea459e1 27#include <linux/task_work.h>
9164bb4a 28#include <linux/sched/task.h>
e262e32d 29#include <uapi/linux/mount.h>
9bc61ab1 30#include <linux/fs_context.h>
9164bb4a 31
07b20889 32#include "pnode.h"
948730b0 33#include "internal.h"
1da177e4 34
d2921684
EB
35/* Maximum number of mounts in a mount namespace */
36unsigned int sysctl_mount_max __read_mostly = 100000;
37
0818bf27
AV
38static unsigned int m_hash_mask __read_mostly;
39static unsigned int m_hash_shift __read_mostly;
40static unsigned int mp_hash_mask __read_mostly;
41static unsigned int mp_hash_shift __read_mostly;
42
43static __initdata unsigned long mhash_entries;
44static int __init set_mhash_entries(char *str)
45{
46 if (!str)
47 return 0;
48 mhash_entries = simple_strtoul(str, &str, 0);
49 return 1;
50}
51__setup("mhash_entries=", set_mhash_entries);
52
53static __initdata unsigned long mphash_entries;
54static int __init set_mphash_entries(char *str)
55{
56 if (!str)
57 return 0;
58 mphash_entries = simple_strtoul(str, &str, 0);
59 return 1;
60}
61__setup("mphash_entries=", set_mphash_entries);
13f14b4d 62
c7999c36 63static u64 event;
73cd49ec 64static DEFINE_IDA(mnt_id_ida);
719f5d7f 65static DEFINE_IDA(mnt_group_ida);
1da177e4 66
38129a13 67static struct hlist_head *mount_hashtable __read_mostly;
0818bf27 68static struct hlist_head *mountpoint_hashtable __read_mostly;
e18b890b 69static struct kmem_cache *mnt_cache __read_mostly;
59aa0da8 70static DECLARE_RWSEM(namespace_sem);
1da177e4 71
f87fd4c2 72/* /sys/fs */
00d26666
GKH
73struct kobject *fs_kobj;
74EXPORT_SYMBOL_GPL(fs_kobj);
f87fd4c2 75
99b7db7b
NP
76/*
77 * vfsmount lock may be taken for read to prevent changes to the
78 * vfsmount hash, ie. during mountpoint lookups or walking back
79 * up the tree.
80 *
81 * It should be taken for write in all cases where the vfsmount
82 * tree or hash is modified or when a vfsmount structure is modified.
83 */
48a066e7 84__cacheline_aligned_in_smp DEFINE_SEQLOCK(mount_lock);
99b7db7b 85
38129a13 86static inline struct hlist_head *m_hash(struct vfsmount *mnt, struct dentry *dentry)
1da177e4 87{
b58fed8b
RP
88 unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
89 tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
0818bf27
AV
90 tmp = tmp + (tmp >> m_hash_shift);
91 return &mount_hashtable[tmp & m_hash_mask];
92}
93
94static inline struct hlist_head *mp_hash(struct dentry *dentry)
95{
96 unsigned long tmp = ((unsigned long)dentry / L1_CACHE_BYTES);
97 tmp = tmp + (tmp >> mp_hash_shift);
98 return &mountpoint_hashtable[tmp & mp_hash_mask];
1da177e4
LT
99}
100
b105e270 101static int mnt_alloc_id(struct mount *mnt)
73cd49ec 102{
169b480e
MW
103 int res = ida_alloc(&mnt_id_ida, GFP_KERNEL);
104
105 if (res < 0)
106 return res;
107 mnt->mnt_id = res;
108 return 0;
73cd49ec
MS
109}
110
b105e270 111static void mnt_free_id(struct mount *mnt)
73cd49ec 112{
169b480e 113 ida_free(&mnt_id_ida, mnt->mnt_id);
73cd49ec
MS
114}
115
719f5d7f
MS
116/*
117 * Allocate a new peer group ID
719f5d7f 118 */
4b8b21f4 119static int mnt_alloc_group_id(struct mount *mnt)
719f5d7f 120{
169b480e 121 int res = ida_alloc_min(&mnt_group_ida, 1, GFP_KERNEL);
f21f6220 122
169b480e
MW
123 if (res < 0)
124 return res;
125 mnt->mnt_group_id = res;
126 return 0;
719f5d7f
MS
127}
128
129/*
130 * Release a peer group ID
131 */
4b8b21f4 132void mnt_release_group_id(struct mount *mnt)
719f5d7f 133{
169b480e 134 ida_free(&mnt_group_ida, mnt->mnt_group_id);
15169fe7 135 mnt->mnt_group_id = 0;
719f5d7f
MS
136}
137
b3e19d92
NP
138/*
139 * vfsmount lock must be held for read
140 */
83adc753 141static inline void mnt_add_count(struct mount *mnt, int n)
b3e19d92
NP
142{
143#ifdef CONFIG_SMP
68e8a9fe 144 this_cpu_add(mnt->mnt_pcp->mnt_count, n);
b3e19d92
NP
145#else
146 preempt_disable();
68e8a9fe 147 mnt->mnt_count += n;
b3e19d92
NP
148 preempt_enable();
149#endif
150}
151
b3e19d92
NP
152/*
153 * vfsmount lock must be held for write
154 */
83adc753 155unsigned int mnt_get_count(struct mount *mnt)
b3e19d92
NP
156{
157#ifdef CONFIG_SMP
f03c6599 158 unsigned int count = 0;
b3e19d92
NP
159 int cpu;
160
161 for_each_possible_cpu(cpu) {
68e8a9fe 162 count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
b3e19d92
NP
163 }
164
165 return count;
166#else
68e8a9fe 167 return mnt->mnt_count;
b3e19d92
NP
168#endif
169}
170
87b95ce0
AV
171static void drop_mountpoint(struct fs_pin *p)
172{
173 struct mount *m = container_of(p, struct mount, mnt_umount);
174 dput(m->mnt_ex_mountpoint);
175 pin_remove(p);
176 mntput(&m->mnt);
177}
178
b105e270 179static struct mount *alloc_vfsmnt(const char *name)
1da177e4 180{
c63181e6
AV
181 struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
182 if (mnt) {
73cd49ec
MS
183 int err;
184
c63181e6 185 err = mnt_alloc_id(mnt);
88b38782
LZ
186 if (err)
187 goto out_free_cache;
188
189 if (name) {
fcc139ae 190 mnt->mnt_devname = kstrdup_const(name, GFP_KERNEL);
c63181e6 191 if (!mnt->mnt_devname)
88b38782 192 goto out_free_id;
73cd49ec
MS
193 }
194
b3e19d92 195#ifdef CONFIG_SMP
c63181e6
AV
196 mnt->mnt_pcp = alloc_percpu(struct mnt_pcp);
197 if (!mnt->mnt_pcp)
b3e19d92
NP
198 goto out_free_devname;
199
c63181e6 200 this_cpu_add(mnt->mnt_pcp->mnt_count, 1);
b3e19d92 201#else
c63181e6
AV
202 mnt->mnt_count = 1;
203 mnt->mnt_writers = 0;
b3e19d92
NP
204#endif
205
38129a13 206 INIT_HLIST_NODE(&mnt->mnt_hash);
c63181e6
AV
207 INIT_LIST_HEAD(&mnt->mnt_child);
208 INIT_LIST_HEAD(&mnt->mnt_mounts);
209 INIT_LIST_HEAD(&mnt->mnt_list);
210 INIT_LIST_HEAD(&mnt->mnt_expire);
211 INIT_LIST_HEAD(&mnt->mnt_share);
212 INIT_LIST_HEAD(&mnt->mnt_slave_list);
213 INIT_LIST_HEAD(&mnt->mnt_slave);
0a5eb7c8 214 INIT_HLIST_NODE(&mnt->mnt_mp_list);
99b19d16 215 INIT_LIST_HEAD(&mnt->mnt_umounting);
87b95ce0 216 init_fs_pin(&mnt->mnt_umount, drop_mountpoint);
1da177e4 217 }
c63181e6 218 return mnt;
88b38782 219
d3ef3d73 220#ifdef CONFIG_SMP
221out_free_devname:
fcc139ae 222 kfree_const(mnt->mnt_devname);
d3ef3d73 223#endif
88b38782 224out_free_id:
c63181e6 225 mnt_free_id(mnt);
88b38782 226out_free_cache:
c63181e6 227 kmem_cache_free(mnt_cache, mnt);
88b38782 228 return NULL;
1da177e4
LT
229}
230
3d733633
DH
231/*
232 * Most r/o checks on a fs are for operations that take
233 * discrete amounts of time, like a write() or unlink().
234 * We must keep track of when those operations start
235 * (for permission checks) and when they end, so that
236 * we can determine when writes are able to occur to
237 * a filesystem.
238 */
239/*
240 * __mnt_is_readonly: check whether a mount is read-only
241 * @mnt: the mount to check for its write status
242 *
243 * This shouldn't be used directly ouside of the VFS.
244 * It does not guarantee that the filesystem will stay
245 * r/w, just that it is right *now*. This can not and
246 * should not be used in place of IS_RDONLY(inode).
247 * mnt_want/drop_write() will _keep_ the filesystem
248 * r/w.
249 */
43f5e655 250bool __mnt_is_readonly(struct vfsmount *mnt)
3d733633 251{
43f5e655 252 return (mnt->mnt_flags & MNT_READONLY) || sb_rdonly(mnt->mnt_sb);
3d733633
DH
253}
254EXPORT_SYMBOL_GPL(__mnt_is_readonly);
255
83adc753 256static inline void mnt_inc_writers(struct mount *mnt)
d3ef3d73 257{
258#ifdef CONFIG_SMP
68e8a9fe 259 this_cpu_inc(mnt->mnt_pcp->mnt_writers);
d3ef3d73 260#else
68e8a9fe 261 mnt->mnt_writers++;
d3ef3d73 262#endif
263}
3d733633 264
83adc753 265static inline void mnt_dec_writers(struct mount *mnt)
3d733633 266{
d3ef3d73 267#ifdef CONFIG_SMP
68e8a9fe 268 this_cpu_dec(mnt->mnt_pcp->mnt_writers);
d3ef3d73 269#else
68e8a9fe 270 mnt->mnt_writers--;
d3ef3d73 271#endif
3d733633 272}
3d733633 273
83adc753 274static unsigned int mnt_get_writers(struct mount *mnt)
3d733633 275{
d3ef3d73 276#ifdef CONFIG_SMP
277 unsigned int count = 0;
3d733633 278 int cpu;
3d733633
DH
279
280 for_each_possible_cpu(cpu) {
68e8a9fe 281 count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers;
3d733633 282 }
3d733633 283
d3ef3d73 284 return count;
285#else
286 return mnt->mnt_writers;
287#endif
3d733633
DH
288}
289
4ed5e82f
MS
290static int mnt_is_readonly(struct vfsmount *mnt)
291{
292 if (mnt->mnt_sb->s_readonly_remount)
293 return 1;
294 /* Order wrt setting s_flags/s_readonly_remount in do_remount() */
295 smp_rmb();
296 return __mnt_is_readonly(mnt);
297}
298
8366025e 299/*
eb04c282
JK
300 * Most r/o & frozen checks on a fs are for operations that take discrete
301 * amounts of time, like a write() or unlink(). We must keep track of when
302 * those operations start (for permission checks) and when they end, so that we
303 * can determine when writes are able to occur to a filesystem.
8366025e
DH
304 */
305/**
eb04c282 306 * __mnt_want_write - get write access to a mount without freeze protection
83adc753 307 * @m: the mount on which to take a write
8366025e 308 *
eb04c282
JK
309 * This tells the low-level filesystem that a write is about to be performed to
310 * it, and makes sure that writes are allowed (mnt it read-write) before
311 * returning success. This operation does not protect against filesystem being
312 * frozen. When the write operation is finished, __mnt_drop_write() must be
313 * called. This is effectively a refcount.
8366025e 314 */
eb04c282 315int __mnt_want_write(struct vfsmount *m)
8366025e 316{
83adc753 317 struct mount *mnt = real_mount(m);
3d733633 318 int ret = 0;
3d733633 319
d3ef3d73 320 preempt_disable();
c6653a83 321 mnt_inc_writers(mnt);
d3ef3d73 322 /*
c6653a83 323 * The store to mnt_inc_writers must be visible before we pass
d3ef3d73 324 * MNT_WRITE_HOLD loop below, so that the slowpath can see our
325 * incremented count after it has set MNT_WRITE_HOLD.
326 */
327 smp_mb();
6aa7de05 328 while (READ_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD)
d3ef3d73 329 cpu_relax();
330 /*
331 * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
332 * be set to match its requirements. So we must not load that until
333 * MNT_WRITE_HOLD is cleared.
334 */
335 smp_rmb();
4ed5e82f 336 if (mnt_is_readonly(m)) {
c6653a83 337 mnt_dec_writers(mnt);
3d733633 338 ret = -EROFS;
3d733633 339 }
d3ef3d73 340 preempt_enable();
eb04c282
JK
341
342 return ret;
343}
344
345/**
346 * mnt_want_write - get write access to a mount
347 * @m: the mount on which to take a write
348 *
349 * This tells the low-level filesystem that a write is about to be performed to
350 * it, and makes sure that writes are allowed (mount is read-write, filesystem
351 * is not frozen) before returning success. When the write operation is
352 * finished, mnt_drop_write() must be called. This is effectively a refcount.
353 */
354int mnt_want_write(struct vfsmount *m)
355{
356 int ret;
357
358 sb_start_write(m->mnt_sb);
359 ret = __mnt_want_write(m);
360 if (ret)
361 sb_end_write(m->mnt_sb);
3d733633 362 return ret;
8366025e
DH
363}
364EXPORT_SYMBOL_GPL(mnt_want_write);
365
96029c4e 366/**
367 * mnt_clone_write - get write access to a mount
368 * @mnt: the mount on which to take a write
369 *
370 * This is effectively like mnt_want_write, except
371 * it must only be used to take an extra write reference
372 * on a mountpoint that we already know has a write reference
373 * on it. This allows some optimisation.
374 *
375 * After finished, mnt_drop_write must be called as usual to
376 * drop the reference.
377 */
378int mnt_clone_write(struct vfsmount *mnt)
379{
380 /* superblock may be r/o */
381 if (__mnt_is_readonly(mnt))
382 return -EROFS;
383 preempt_disable();
83adc753 384 mnt_inc_writers(real_mount(mnt));
96029c4e 385 preempt_enable();
386 return 0;
387}
388EXPORT_SYMBOL_GPL(mnt_clone_write);
389
390/**
eb04c282 391 * __mnt_want_write_file - get write access to a file's mount
96029c4e 392 * @file: the file who's mount on which to take a write
393 *
eb04c282 394 * This is like __mnt_want_write, but it takes a file and can
96029c4e 395 * do some optimisations if the file is open for write already
396 */
eb04c282 397int __mnt_want_write_file(struct file *file)
96029c4e 398{
83f936c7 399 if (!(file->f_mode & FMODE_WRITER))
eb04c282 400 return __mnt_want_write(file->f_path.mnt);
96029c4e 401 else
402 return mnt_clone_write(file->f_path.mnt);
403}
eb04c282 404
7c6893e3
MS
405/**
406 * mnt_want_write_file - get write access to a file's mount
407 * @file: the file who's mount on which to take a write
408 *
409 * This is like mnt_want_write, but it takes a file and can
410 * do some optimisations if the file is open for write already
7c6893e3
MS
411 */
412int mnt_want_write_file(struct file *file)
413{
414 int ret;
415
a6795a58 416 sb_start_write(file_inode(file)->i_sb);
eb04c282
JK
417 ret = __mnt_want_write_file(file);
418 if (ret)
a6795a58 419 sb_end_write(file_inode(file)->i_sb);
7c6893e3
MS
420 return ret;
421}
96029c4e 422EXPORT_SYMBOL_GPL(mnt_want_write_file);
423
8366025e 424/**
eb04c282 425 * __mnt_drop_write - give up write access to a mount
8366025e
DH
426 * @mnt: the mount on which to give up write access
427 *
428 * Tells the low-level filesystem that we are done
429 * performing writes to it. Must be matched with
eb04c282 430 * __mnt_want_write() call above.
8366025e 431 */
eb04c282 432void __mnt_drop_write(struct vfsmount *mnt)
8366025e 433{
d3ef3d73 434 preempt_disable();
83adc753 435 mnt_dec_writers(real_mount(mnt));
d3ef3d73 436 preempt_enable();
8366025e 437}
eb04c282
JK
438
439/**
440 * mnt_drop_write - give up write access to a mount
441 * @mnt: the mount on which to give up write access
442 *
443 * Tells the low-level filesystem that we are done performing writes to it and
444 * also allows filesystem to be frozen again. Must be matched with
445 * mnt_want_write() call above.
446 */
447void mnt_drop_write(struct vfsmount *mnt)
448{
449 __mnt_drop_write(mnt);
450 sb_end_write(mnt->mnt_sb);
451}
8366025e
DH
452EXPORT_SYMBOL_GPL(mnt_drop_write);
453
eb04c282
JK
454void __mnt_drop_write_file(struct file *file)
455{
456 __mnt_drop_write(file->f_path.mnt);
457}
458
7c6893e3
MS
459void mnt_drop_write_file(struct file *file)
460{
a6795a58 461 __mnt_drop_write_file(file);
7c6893e3
MS
462 sb_end_write(file_inode(file)->i_sb);
463}
2a79f17e
AV
464EXPORT_SYMBOL(mnt_drop_write_file);
465
83adc753 466static int mnt_make_readonly(struct mount *mnt)
8366025e 467{
3d733633
DH
468 int ret = 0;
469
719ea2fb 470 lock_mount_hash();
83adc753 471 mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
3d733633 472 /*
d3ef3d73 473 * After storing MNT_WRITE_HOLD, we'll read the counters. This store
474 * should be visible before we do.
3d733633 475 */
d3ef3d73 476 smp_mb();
477
3d733633 478 /*
d3ef3d73 479 * With writers on hold, if this value is zero, then there are
480 * definitely no active writers (although held writers may subsequently
481 * increment the count, they'll have to wait, and decrement it after
482 * seeing MNT_READONLY).
483 *
484 * It is OK to have counter incremented on one CPU and decremented on
485 * another: the sum will add up correctly. The danger would be when we
486 * sum up each counter, if we read a counter before it is incremented,
487 * but then read another CPU's count which it has been subsequently
488 * decremented from -- we would see more decrements than we should.
489 * MNT_WRITE_HOLD protects against this scenario, because
490 * mnt_want_write first increments count, then smp_mb, then spins on
491 * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
492 * we're counting up here.
3d733633 493 */
c6653a83 494 if (mnt_get_writers(mnt) > 0)
d3ef3d73 495 ret = -EBUSY;
496 else
83adc753 497 mnt->mnt.mnt_flags |= MNT_READONLY;
d3ef3d73 498 /*
499 * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
500 * that become unheld will see MNT_READONLY.
501 */
502 smp_wmb();
83adc753 503 mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
719ea2fb 504 unlock_mount_hash();
3d733633 505 return ret;
8366025e 506}
8366025e 507
43f5e655 508static int __mnt_unmake_readonly(struct mount *mnt)
2e4b7fcd 509{
719ea2fb 510 lock_mount_hash();
83adc753 511 mnt->mnt.mnt_flags &= ~MNT_READONLY;
719ea2fb 512 unlock_mount_hash();
43f5e655 513 return 0;
2e4b7fcd
DH
514}
515
4ed5e82f
MS
516int sb_prepare_remount_readonly(struct super_block *sb)
517{
518 struct mount *mnt;
519 int err = 0;
520
8e8b8796
MS
521 /* Racy optimization. Recheck the counter under MNT_WRITE_HOLD */
522 if (atomic_long_read(&sb->s_remove_count))
523 return -EBUSY;
524
719ea2fb 525 lock_mount_hash();
4ed5e82f
MS
526 list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
527 if (!(mnt->mnt.mnt_flags & MNT_READONLY)) {
528 mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
529 smp_mb();
530 if (mnt_get_writers(mnt) > 0) {
531 err = -EBUSY;
532 break;
533 }
534 }
535 }
8e8b8796
MS
536 if (!err && atomic_long_read(&sb->s_remove_count))
537 err = -EBUSY;
538
4ed5e82f
MS
539 if (!err) {
540 sb->s_readonly_remount = 1;
541 smp_wmb();
542 }
543 list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
544 if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD)
545 mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
546 }
719ea2fb 547 unlock_mount_hash();
4ed5e82f
MS
548
549 return err;
550}
551
b105e270 552static void free_vfsmnt(struct mount *mnt)
1da177e4 553{
fcc139ae 554 kfree_const(mnt->mnt_devname);
d3ef3d73 555#ifdef CONFIG_SMP
68e8a9fe 556 free_percpu(mnt->mnt_pcp);
d3ef3d73 557#endif
b105e270 558 kmem_cache_free(mnt_cache, mnt);
1da177e4
LT
559}
560
8ffcb32e
DH
561static void delayed_free_vfsmnt(struct rcu_head *head)
562{
563 free_vfsmnt(container_of(head, struct mount, mnt_rcu));
564}
565
48a066e7 566/* call under rcu_read_lock */
294d71ff 567int __legitimize_mnt(struct vfsmount *bastard, unsigned seq)
48a066e7
AV
568{
569 struct mount *mnt;
570 if (read_seqretry(&mount_lock, seq))
294d71ff 571 return 1;
48a066e7 572 if (bastard == NULL)
294d71ff 573 return 0;
48a066e7
AV
574 mnt = real_mount(bastard);
575 mnt_add_count(mnt, 1);
119e1ef8 576 smp_mb(); // see mntput_no_expire()
48a066e7 577 if (likely(!read_seqretry(&mount_lock, seq)))
294d71ff 578 return 0;
48a066e7
AV
579 if (bastard->mnt_flags & MNT_SYNC_UMOUNT) {
580 mnt_add_count(mnt, -1);
294d71ff
AV
581 return 1;
582 }
119e1ef8
AV
583 lock_mount_hash();
584 if (unlikely(bastard->mnt_flags & MNT_DOOMED)) {
585 mnt_add_count(mnt, -1);
586 unlock_mount_hash();
587 return 1;
588 }
589 unlock_mount_hash();
590 /* caller will mntput() */
294d71ff
AV
591 return -1;
592}
593
594/* call under rcu_read_lock */
595bool legitimize_mnt(struct vfsmount *bastard, unsigned seq)
596{
597 int res = __legitimize_mnt(bastard, seq);
598 if (likely(!res))
599 return true;
600 if (unlikely(res < 0)) {
601 rcu_read_unlock();
602 mntput(bastard);
603 rcu_read_lock();
48a066e7 604 }
48a066e7
AV
605 return false;
606}
607
1da177e4 608/*
474279dc 609 * find the first mount at @dentry on vfsmount @mnt.
48a066e7 610 * call under rcu_read_lock()
1da177e4 611 */
474279dc 612struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
1da177e4 613{
38129a13 614 struct hlist_head *head = m_hash(mnt, dentry);
474279dc
AV
615 struct mount *p;
616
38129a13 617 hlist_for_each_entry_rcu(p, head, mnt_hash)
474279dc
AV
618 if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry)
619 return p;
620 return NULL;
621}
622
a05964f3 623/*
f015f126
DH
624 * lookup_mnt - Return the first child mount mounted at path
625 *
626 * "First" means first mounted chronologically. If you create the
627 * following mounts:
628 *
629 * mount /dev/sda1 /mnt
630 * mount /dev/sda2 /mnt
631 * mount /dev/sda3 /mnt
632 *
633 * Then lookup_mnt() on the base /mnt dentry in the root mount will
634 * return successively the root dentry and vfsmount of /dev/sda1, then
635 * /dev/sda2, then /dev/sda3, then NULL.
636 *
637 * lookup_mnt takes a reference to the found vfsmount.
a05964f3 638 */
ca71cf71 639struct vfsmount *lookup_mnt(const struct path *path)
a05964f3 640{
c7105365 641 struct mount *child_mnt;
48a066e7
AV
642 struct vfsmount *m;
643 unsigned seq;
99b7db7b 644
48a066e7
AV
645 rcu_read_lock();
646 do {
647 seq = read_seqbegin(&mount_lock);
648 child_mnt = __lookup_mnt(path->mnt, path->dentry);
649 m = child_mnt ? &child_mnt->mnt : NULL;
650 } while (!legitimize_mnt(m, seq));
651 rcu_read_unlock();
652 return m;
a05964f3
RP
653}
654
7af1364f
EB
655/*
656 * __is_local_mountpoint - Test to see if dentry is a mountpoint in the
657 * current mount namespace.
658 *
659 * The common case is dentries are not mountpoints at all and that
660 * test is handled inline. For the slow case when we are actually
661 * dealing with a mountpoint of some kind, walk through all of the
662 * mounts in the current mount namespace and test to see if the dentry
663 * is a mountpoint.
664 *
665 * The mount_hashtable is not usable in the context because we
666 * need to identify all mounts that may be in the current mount
667 * namespace not just a mount that happens to have some specified
668 * parent mount.
669 */
670bool __is_local_mountpoint(struct dentry *dentry)
671{
672 struct mnt_namespace *ns = current->nsproxy->mnt_ns;
673 struct mount *mnt;
674 bool is_covered = false;
675
676 if (!d_mountpoint(dentry))
677 goto out;
678
679 down_read(&namespace_sem);
680 list_for_each_entry(mnt, &ns->list, mnt_list) {
681 is_covered = (mnt->mnt_mountpoint == dentry);
682 if (is_covered)
683 break;
684 }
685 up_read(&namespace_sem);
686out:
687 return is_covered;
688}
689
e2dfa935 690static struct mountpoint *lookup_mountpoint(struct dentry *dentry)
84d17192 691{
0818bf27 692 struct hlist_head *chain = mp_hash(dentry);
84d17192
AV
693 struct mountpoint *mp;
694
0818bf27 695 hlist_for_each_entry(mp, chain, m_hash) {
84d17192 696 if (mp->m_dentry == dentry) {
84d17192
AV
697 mp->m_count++;
698 return mp;
699 }
700 }
e2dfa935
EB
701 return NULL;
702}
703
3895dbf8 704static struct mountpoint *get_mountpoint(struct dentry *dentry)
e2dfa935 705{
3895dbf8 706 struct mountpoint *mp, *new = NULL;
e2dfa935 707 int ret;
84d17192 708
3895dbf8 709 if (d_mountpoint(dentry)) {
1e9c75fb
BC
710 /* might be worth a WARN_ON() */
711 if (d_unlinked(dentry))
712 return ERR_PTR(-ENOENT);
3895dbf8
EB
713mountpoint:
714 read_seqlock_excl(&mount_lock);
715 mp = lookup_mountpoint(dentry);
716 read_sequnlock_excl(&mount_lock);
717 if (mp)
718 goto done;
719 }
720
721 if (!new)
722 new = kmalloc(sizeof(struct mountpoint), GFP_KERNEL);
723 if (!new)
84d17192
AV
724 return ERR_PTR(-ENOMEM);
725
3895dbf8
EB
726
727 /* Exactly one processes may set d_mounted */
eed81007 728 ret = d_set_mounted(dentry);
eed81007 729
3895dbf8
EB
730 /* Someone else set d_mounted? */
731 if (ret == -EBUSY)
732 goto mountpoint;
733
734 /* The dentry is not available as a mountpoint? */
735 mp = ERR_PTR(ret);
736 if (ret)
737 goto done;
738
739 /* Add the new mountpoint to the hash table */
740 read_seqlock_excl(&mount_lock);
741 new->m_dentry = dentry;
742 new->m_count = 1;
743 hlist_add_head(&new->m_hash, mp_hash(dentry));
744 INIT_HLIST_HEAD(&new->m_list);
745 read_sequnlock_excl(&mount_lock);
746
747 mp = new;
748 new = NULL;
749done:
750 kfree(new);
84d17192
AV
751 return mp;
752}
753
754static void put_mountpoint(struct mountpoint *mp)
755{
756 if (!--mp->m_count) {
757 struct dentry *dentry = mp->m_dentry;
0a5eb7c8 758 BUG_ON(!hlist_empty(&mp->m_list));
84d17192
AV
759 spin_lock(&dentry->d_lock);
760 dentry->d_flags &= ~DCACHE_MOUNTED;
761 spin_unlock(&dentry->d_lock);
0818bf27 762 hlist_del(&mp->m_hash);
84d17192
AV
763 kfree(mp);
764 }
765}
766
143c8c91 767static inline int check_mnt(struct mount *mnt)
1da177e4 768{
6b3286ed 769 return mnt->mnt_ns == current->nsproxy->mnt_ns;
1da177e4
LT
770}
771
99b7db7b
NP
772/*
773 * vfsmount lock must be held for write
774 */
6b3286ed 775static void touch_mnt_namespace(struct mnt_namespace *ns)
5addc5dd
AV
776{
777 if (ns) {
778 ns->event = ++event;
779 wake_up_interruptible(&ns->poll);
780 }
781}
782
99b7db7b
NP
783/*
784 * vfsmount lock must be held for write
785 */
6b3286ed 786static void __touch_mnt_namespace(struct mnt_namespace *ns)
5addc5dd
AV
787{
788 if (ns && ns->event != event) {
789 ns->event = event;
790 wake_up_interruptible(&ns->poll);
791 }
792}
793
99b7db7b
NP
794/*
795 * vfsmount lock must be held for write
796 */
7bdb11de 797static void unhash_mnt(struct mount *mnt)
419148da 798{
0714a533 799 mnt->mnt_parent = mnt;
a73324da 800 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
6b41d536 801 list_del_init(&mnt->mnt_child);
38129a13 802 hlist_del_init_rcu(&mnt->mnt_hash);
0a5eb7c8 803 hlist_del_init(&mnt->mnt_mp_list);
84d17192
AV
804 put_mountpoint(mnt->mnt_mp);
805 mnt->mnt_mp = NULL;
1da177e4
LT
806}
807
7bdb11de
EB
808/*
809 * vfsmount lock must be held for write
810 */
811static void detach_mnt(struct mount *mnt, struct path *old_path)
812{
813 old_path->dentry = mnt->mnt_mountpoint;
814 old_path->mnt = &mnt->mnt_parent->mnt;
815 unhash_mnt(mnt);
816}
817
6a46c573
EB
818/*
819 * vfsmount lock must be held for write
820 */
821static void umount_mnt(struct mount *mnt)
822{
823 /* old mountpoint will be dropped when we can do that */
824 mnt->mnt_ex_mountpoint = mnt->mnt_mountpoint;
825 unhash_mnt(mnt);
826}
827
99b7db7b
NP
828/*
829 * vfsmount lock must be held for write
830 */
84d17192
AV
831void mnt_set_mountpoint(struct mount *mnt,
832 struct mountpoint *mp,
44d964d6 833 struct mount *child_mnt)
b90fa9ae 834{
84d17192 835 mp->m_count++;
3a2393d7 836 mnt_add_count(mnt, 1); /* essentially, that's mntget */
84d17192 837 child_mnt->mnt_mountpoint = dget(mp->m_dentry);
3a2393d7 838 child_mnt->mnt_parent = mnt;
84d17192 839 child_mnt->mnt_mp = mp;
0a5eb7c8 840 hlist_add_head(&child_mnt->mnt_mp_list, &mp->m_list);
b90fa9ae
RP
841}
842
1064f874
EB
843static void __attach_mnt(struct mount *mnt, struct mount *parent)
844{
845 hlist_add_head_rcu(&mnt->mnt_hash,
846 m_hash(&parent->mnt, mnt->mnt_mountpoint));
847 list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
848}
849
99b7db7b
NP
850/*
851 * vfsmount lock must be held for write
852 */
84d17192
AV
853static void attach_mnt(struct mount *mnt,
854 struct mount *parent,
855 struct mountpoint *mp)
1da177e4 856{
84d17192 857 mnt_set_mountpoint(parent, mp, mnt);
1064f874 858 __attach_mnt(mnt, parent);
b90fa9ae
RP
859}
860
1064f874 861void mnt_change_mountpoint(struct mount *parent, struct mountpoint *mp, struct mount *mnt)
12a5b529 862{
1064f874
EB
863 struct mountpoint *old_mp = mnt->mnt_mp;
864 struct dentry *old_mountpoint = mnt->mnt_mountpoint;
865 struct mount *old_parent = mnt->mnt_parent;
866
867 list_del_init(&mnt->mnt_child);
868 hlist_del_init(&mnt->mnt_mp_list);
869 hlist_del_init_rcu(&mnt->mnt_hash);
870
871 attach_mnt(mnt, parent, mp);
872
873 put_mountpoint(old_mp);
874
875 /*
876 * Safely avoid even the suggestion this code might sleep or
877 * lock the mount hash by taking advantage of the knowledge that
878 * mnt_change_mountpoint will not release the final reference
879 * to a mountpoint.
880 *
881 * During mounting, the mount passed in as the parent mount will
882 * continue to use the old mountpoint and during unmounting, the
883 * old mountpoint will continue to exist until namespace_unlock,
884 * which happens well after mnt_change_mountpoint.
885 */
886 spin_lock(&old_mountpoint->d_lock);
887 old_mountpoint->d_lockref.count--;
888 spin_unlock(&old_mountpoint->d_lock);
889
890 mnt_add_count(old_parent, -1);
12a5b529
AV
891}
892
b90fa9ae 893/*
99b7db7b 894 * vfsmount lock must be held for write
b90fa9ae 895 */
1064f874 896static void commit_tree(struct mount *mnt)
b90fa9ae 897{
0714a533 898 struct mount *parent = mnt->mnt_parent;
83adc753 899 struct mount *m;
b90fa9ae 900 LIST_HEAD(head);
143c8c91 901 struct mnt_namespace *n = parent->mnt_ns;
b90fa9ae 902
0714a533 903 BUG_ON(parent == mnt);
b90fa9ae 904
1a4eeaf2 905 list_add_tail(&head, &mnt->mnt_list);
f7a99c5b 906 list_for_each_entry(m, &head, mnt_list)
143c8c91 907 m->mnt_ns = n;
f03c6599 908
b90fa9ae
RP
909 list_splice(&head, n->list.prev);
910
d2921684
EB
911 n->mounts += n->pending_mounts;
912 n->pending_mounts = 0;
913
1064f874 914 __attach_mnt(mnt, parent);
6b3286ed 915 touch_mnt_namespace(n);
1da177e4
LT
916}
917
909b0a88 918static struct mount *next_mnt(struct mount *p, struct mount *root)
1da177e4 919{
6b41d536
AV
920 struct list_head *next = p->mnt_mounts.next;
921 if (next == &p->mnt_mounts) {
1da177e4 922 while (1) {
909b0a88 923 if (p == root)
1da177e4 924 return NULL;
6b41d536
AV
925 next = p->mnt_child.next;
926 if (next != &p->mnt_parent->mnt_mounts)
1da177e4 927 break;
0714a533 928 p = p->mnt_parent;
1da177e4
LT
929 }
930 }
6b41d536 931 return list_entry(next, struct mount, mnt_child);
1da177e4
LT
932}
933
315fc83e 934static struct mount *skip_mnt_tree(struct mount *p)
9676f0c6 935{
6b41d536
AV
936 struct list_head *prev = p->mnt_mounts.prev;
937 while (prev != &p->mnt_mounts) {
938 p = list_entry(prev, struct mount, mnt_child);
939 prev = p->mnt_mounts.prev;
9676f0c6
RP
940 }
941 return p;
942}
943
9bc61ab1
DH
944struct vfsmount *vfs_kern_mount(struct file_system_type *type,
945 int flags, const char *name,
946 void *data)
9d412a43 947{
9bc61ab1 948 struct fs_context *fc;
b105e270 949 struct mount *mnt;
9bc61ab1 950 int ret = 0;
9d412a43
AV
951
952 if (!type)
953 return ERR_PTR(-ENODEV);
954
9bc61ab1
DH
955 fc = fs_context_for_mount(type, flags);
956 if (IS_ERR(fc))
957 return ERR_CAST(fc);
958
959 if (name) {
960 fc->source = kstrdup(name, GFP_KERNEL);
961 if (!fc->source)
962 ret = -ENOMEM;
963 }
964 if (!ret)
965 ret = parse_monolithic_mount_data(fc, data);
966 if (!ret)
967 ret = vfs_get_tree(fc);
968 if (ret) {
969 put_fs_context(fc);
970 return ERR_PTR(ret);
971 }
972 up_write(&fc->root->d_sb->s_umount);
9d412a43 973 mnt = alloc_vfsmnt(name);
9bc61ab1
DH
974 if (!mnt) {
975 put_fs_context(fc);
9d412a43 976 return ERR_PTR(-ENOMEM);
9bc61ab1 977 }
9d412a43 978
e462ec50 979 if (flags & SB_KERNMOUNT)
b105e270 980 mnt->mnt.mnt_flags = MNT_INTERNAL;
9d412a43 981
9bc61ab1
DH
982 atomic_inc(&fc->root->d_sb->s_active);
983 mnt->mnt.mnt_root = dget(fc->root);
984 mnt->mnt.mnt_sb = fc->root->d_sb;
a73324da 985 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
0714a533 986 mnt->mnt_parent = mnt;
719ea2fb 987 lock_mount_hash();
9bc61ab1 988 list_add_tail(&mnt->mnt_instance, &fc->root->d_sb->s_mounts);
719ea2fb 989 unlock_mount_hash();
9bc61ab1 990 put_fs_context(fc);
b105e270 991 return &mnt->mnt;
9d412a43
AV
992}
993EXPORT_SYMBOL_GPL(vfs_kern_mount);
994
93faccbb
EB
995struct vfsmount *
996vfs_submount(const struct dentry *mountpoint, struct file_system_type *type,
997 const char *name, void *data)
998{
999 /* Until it is worked out how to pass the user namespace
1000 * through from the parent mount to the submount don't support
1001 * unprivileged mounts with submounts.
1002 */
1003 if (mountpoint->d_sb->s_user_ns != &init_user_ns)
1004 return ERR_PTR(-EPERM);
1005
e462ec50 1006 return vfs_kern_mount(type, SB_SUBMOUNT, name, data);
93faccbb
EB
1007}
1008EXPORT_SYMBOL_GPL(vfs_submount);
1009
87129cc0 1010static struct mount *clone_mnt(struct mount *old, struct dentry *root,
36341f64 1011 int flag)
1da177e4 1012{
87129cc0 1013 struct super_block *sb = old->mnt.mnt_sb;
be34d1a3
DH
1014 struct mount *mnt;
1015 int err;
1da177e4 1016
be34d1a3
DH
1017 mnt = alloc_vfsmnt(old->mnt_devname);
1018 if (!mnt)
1019 return ERR_PTR(-ENOMEM);
719f5d7f 1020
7a472ef4 1021 if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE))
be34d1a3
DH
1022 mnt->mnt_group_id = 0; /* not a peer of original */
1023 else
1024 mnt->mnt_group_id = old->mnt_group_id;
b90fa9ae 1025
be34d1a3
DH
1026 if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
1027 err = mnt_alloc_group_id(mnt);
1028 if (err)
1029 goto out_free;
1da177e4 1030 }
be34d1a3 1031
16a34adb
AV
1032 mnt->mnt.mnt_flags = old->mnt.mnt_flags;
1033 mnt->mnt.mnt_flags &= ~(MNT_WRITE_HOLD|MNT_MARKED|MNT_INTERNAL);
5ff9d8a6 1034
be34d1a3
DH
1035 atomic_inc(&sb->s_active);
1036 mnt->mnt.mnt_sb = sb;
1037 mnt->mnt.mnt_root = dget(root);
1038 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
1039 mnt->mnt_parent = mnt;
719ea2fb 1040 lock_mount_hash();
be34d1a3 1041 list_add_tail(&mnt->mnt_instance, &sb->s_mounts);
719ea2fb 1042 unlock_mount_hash();
be34d1a3 1043
7a472ef4
EB
1044 if ((flag & CL_SLAVE) ||
1045 ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) {
be34d1a3
DH
1046 list_add(&mnt->mnt_slave, &old->mnt_slave_list);
1047 mnt->mnt_master = old;
1048 CLEAR_MNT_SHARED(mnt);
1049 } else if (!(flag & CL_PRIVATE)) {
1050 if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
1051 list_add(&mnt->mnt_share, &old->mnt_share);
1052 if (IS_MNT_SLAVE(old))
1053 list_add(&mnt->mnt_slave, &old->mnt_slave);
1054 mnt->mnt_master = old->mnt_master;
5235d448
AV
1055 } else {
1056 CLEAR_MNT_SHARED(mnt);
be34d1a3
DH
1057 }
1058 if (flag & CL_MAKE_SHARED)
1059 set_mnt_shared(mnt);
1060
1061 /* stick the duplicate mount on the same expiry list
1062 * as the original if that was on one */
1063 if (flag & CL_EXPIRE) {
1064 if (!list_empty(&old->mnt_expire))
1065 list_add(&mnt->mnt_expire, &old->mnt_expire);
1066 }
1067
cb338d06 1068 return mnt;
719f5d7f
MS
1069
1070 out_free:
8ffcb32e 1071 mnt_free_id(mnt);
719f5d7f 1072 free_vfsmnt(mnt);
be34d1a3 1073 return ERR_PTR(err);
1da177e4
LT
1074}
1075
9ea459e1
AV
1076static void cleanup_mnt(struct mount *mnt)
1077{
1078 /*
1079 * This probably indicates that somebody messed
1080 * up a mnt_want/drop_write() pair. If this
1081 * happens, the filesystem was probably unable
1082 * to make r/w->r/o transitions.
1083 */
1084 /*
1085 * The locking used to deal with mnt_count decrement provides barriers,
1086 * so mnt_get_writers() below is safe.
1087 */
1088 WARN_ON(mnt_get_writers(mnt));
1089 if (unlikely(mnt->mnt_pins.first))
1090 mnt_pin_kill(mnt);
1091 fsnotify_vfsmount_delete(&mnt->mnt);
1092 dput(mnt->mnt.mnt_root);
1093 deactivate_super(mnt->mnt.mnt_sb);
1094 mnt_free_id(mnt);
1095 call_rcu(&mnt->mnt_rcu, delayed_free_vfsmnt);
1096}
1097
1098static void __cleanup_mnt(struct rcu_head *head)
1099{
1100 cleanup_mnt(container_of(head, struct mount, mnt_rcu));
1101}
1102
1103static LLIST_HEAD(delayed_mntput_list);
1104static void delayed_mntput(struct work_struct *unused)
1105{
1106 struct llist_node *node = llist_del_all(&delayed_mntput_list);
29785735 1107 struct mount *m, *t;
9ea459e1 1108
29785735
BP
1109 llist_for_each_entry_safe(m, t, node, mnt_llist)
1110 cleanup_mnt(m);
9ea459e1
AV
1111}
1112static DECLARE_DELAYED_WORK(delayed_mntput_work, delayed_mntput);
1113
900148dc 1114static void mntput_no_expire(struct mount *mnt)
b3e19d92 1115{
48a066e7 1116 rcu_read_lock();
9ea0a46c
AV
1117 if (likely(READ_ONCE(mnt->mnt_ns))) {
1118 /*
1119 * Since we don't do lock_mount_hash() here,
1120 * ->mnt_ns can change under us. However, if it's
1121 * non-NULL, then there's a reference that won't
1122 * be dropped until after an RCU delay done after
1123 * turning ->mnt_ns NULL. So if we observe it
1124 * non-NULL under rcu_read_lock(), the reference
1125 * we are dropping is not the final one.
1126 */
1127 mnt_add_count(mnt, -1);
48a066e7 1128 rcu_read_unlock();
f03c6599 1129 return;
b3e19d92 1130 }
719ea2fb 1131 lock_mount_hash();
119e1ef8
AV
1132 /*
1133 * make sure that if __legitimize_mnt() has not seen us grab
1134 * mount_lock, we'll see their refcount increment here.
1135 */
1136 smp_mb();
9ea0a46c 1137 mnt_add_count(mnt, -1);
b3e19d92 1138 if (mnt_get_count(mnt)) {
48a066e7 1139 rcu_read_unlock();
719ea2fb 1140 unlock_mount_hash();
99b7db7b
NP
1141 return;
1142 }
48a066e7
AV
1143 if (unlikely(mnt->mnt.mnt_flags & MNT_DOOMED)) {
1144 rcu_read_unlock();
1145 unlock_mount_hash();
1146 return;
1147 }
1148 mnt->mnt.mnt_flags |= MNT_DOOMED;
1149 rcu_read_unlock();
962830df 1150
39f7c4db 1151 list_del(&mnt->mnt_instance);
ce07d891
EB
1152
1153 if (unlikely(!list_empty(&mnt->mnt_mounts))) {
1154 struct mount *p, *tmp;
1155 list_for_each_entry_safe(p, tmp, &mnt->mnt_mounts, mnt_child) {
1156 umount_mnt(p);
1157 }
1158 }
719ea2fb 1159 unlock_mount_hash();
649a795a 1160
9ea459e1
AV
1161 if (likely(!(mnt->mnt.mnt_flags & MNT_INTERNAL))) {
1162 struct task_struct *task = current;
1163 if (likely(!(task->flags & PF_KTHREAD))) {
1164 init_task_work(&mnt->mnt_rcu, __cleanup_mnt);
1165 if (!task_work_add(task, &mnt->mnt_rcu, true))
1166 return;
1167 }
1168 if (llist_add(&mnt->mnt_llist, &delayed_mntput_list))
1169 schedule_delayed_work(&delayed_mntput_work, 1);
1170 return;
1171 }
1172 cleanup_mnt(mnt);
b3e19d92 1173}
b3e19d92
NP
1174
1175void mntput(struct vfsmount *mnt)
1176{
1177 if (mnt) {
863d684f 1178 struct mount *m = real_mount(mnt);
b3e19d92 1179 /* avoid cacheline pingpong, hope gcc doesn't get "smart" */
863d684f
AV
1180 if (unlikely(m->mnt_expiry_mark))
1181 m->mnt_expiry_mark = 0;
1182 mntput_no_expire(m);
b3e19d92
NP
1183 }
1184}
1185EXPORT_SYMBOL(mntput);
1186
1187struct vfsmount *mntget(struct vfsmount *mnt)
1188{
1189 if (mnt)
83adc753 1190 mnt_add_count(real_mount(mnt), 1);
b3e19d92
NP
1191 return mnt;
1192}
1193EXPORT_SYMBOL(mntget);
1194
c6609c0a
IK
1195/* path_is_mountpoint() - Check if path is a mount in the current
1196 * namespace.
1197 *
1198 * d_mountpoint() can only be used reliably to establish if a dentry is
1199 * not mounted in any namespace and that common case is handled inline.
1200 * d_mountpoint() isn't aware of the possibility there may be multiple
1201 * mounts using a given dentry in a different namespace. This function
1202 * checks if the passed in path is a mountpoint rather than the dentry
1203 * alone.
1204 */
1205bool path_is_mountpoint(const struct path *path)
1206{
1207 unsigned seq;
1208 bool res;
1209
1210 if (!d_mountpoint(path->dentry))
1211 return false;
1212
1213 rcu_read_lock();
1214 do {
1215 seq = read_seqbegin(&mount_lock);
1216 res = __path_is_mountpoint(path);
1217 } while (read_seqretry(&mount_lock, seq));
1218 rcu_read_unlock();
1219
1220 return res;
1221}
1222EXPORT_SYMBOL(path_is_mountpoint);
1223
ca71cf71 1224struct vfsmount *mnt_clone_internal(const struct path *path)
7b7b1ace 1225{
3064c356
AV
1226 struct mount *p;
1227 p = clone_mnt(real_mount(path->mnt), path->dentry, CL_PRIVATE);
1228 if (IS_ERR(p))
1229 return ERR_CAST(p);
1230 p->mnt.mnt_flags |= MNT_INTERNAL;
1231 return &p->mnt;
7b7b1ace 1232}
1da177e4 1233
a1a2c409 1234#ifdef CONFIG_PROC_FS
0226f492 1235/* iterator; we want it to have access to namespace_sem, thus here... */
1da177e4
LT
1236static void *m_start(struct seq_file *m, loff_t *pos)
1237{
ede1bf0d 1238 struct proc_mounts *p = m->private;
1da177e4 1239
390c6843 1240 down_read(&namespace_sem);
c7999c36
AV
1241 if (p->cached_event == p->ns->event) {
1242 void *v = p->cached_mount;
1243 if (*pos == p->cached_index)
1244 return v;
1245 if (*pos == p->cached_index + 1) {
1246 v = seq_list_next(v, &p->ns->list, &p->cached_index);
1247 return p->cached_mount = v;
1248 }
1249 }
1250
1251 p->cached_event = p->ns->event;
1252 p->cached_mount = seq_list_start(&p->ns->list, *pos);
1253 p->cached_index = *pos;
1254 return p->cached_mount;
1da177e4
LT
1255}
1256
1257static void *m_next(struct seq_file *m, void *v, loff_t *pos)
1258{
ede1bf0d 1259 struct proc_mounts *p = m->private;
b0765fb8 1260
c7999c36
AV
1261 p->cached_mount = seq_list_next(v, &p->ns->list, pos);
1262 p->cached_index = *pos;
1263 return p->cached_mount;
1da177e4
LT
1264}
1265
1266static void m_stop(struct seq_file *m, void *v)
1267{
390c6843 1268 up_read(&namespace_sem);
1da177e4
LT
1269}
1270
0226f492 1271static int m_show(struct seq_file *m, void *v)
2d4d4864 1272{
ede1bf0d 1273 struct proc_mounts *p = m->private;
1a4eeaf2 1274 struct mount *r = list_entry(v, struct mount, mnt_list);
0226f492 1275 return p->show(m, &r->mnt);
1da177e4
LT
1276}
1277
a1a2c409 1278const struct seq_operations mounts_op = {
1da177e4
LT
1279 .start = m_start,
1280 .next = m_next,
1281 .stop = m_stop,
0226f492 1282 .show = m_show,
b4629fe2 1283};
a1a2c409 1284#endif /* CONFIG_PROC_FS */
b4629fe2 1285
1da177e4
LT
1286/**
1287 * may_umount_tree - check if a mount tree is busy
1288 * @mnt: root of mount tree
1289 *
1290 * This is called to check if a tree of mounts has any
1291 * open files, pwds, chroots or sub mounts that are
1292 * busy.
1293 */
909b0a88 1294int may_umount_tree(struct vfsmount *m)
1da177e4 1295{
909b0a88 1296 struct mount *mnt = real_mount(m);
36341f64
RP
1297 int actual_refs = 0;
1298 int minimum_refs = 0;
315fc83e 1299 struct mount *p;
909b0a88 1300 BUG_ON(!m);
1da177e4 1301
b3e19d92 1302 /* write lock needed for mnt_get_count */
719ea2fb 1303 lock_mount_hash();
909b0a88 1304 for (p = mnt; p; p = next_mnt(p, mnt)) {
83adc753 1305 actual_refs += mnt_get_count(p);
1da177e4 1306 minimum_refs += 2;
1da177e4 1307 }
719ea2fb 1308 unlock_mount_hash();
1da177e4
LT
1309
1310 if (actual_refs > minimum_refs)
e3474a8e 1311 return 0;
1da177e4 1312
e3474a8e 1313 return 1;
1da177e4
LT
1314}
1315
1316EXPORT_SYMBOL(may_umount_tree);
1317
1318/**
1319 * may_umount - check if a mount point is busy
1320 * @mnt: root of mount
1321 *
1322 * This is called to check if a mount point has any
1323 * open files, pwds, chroots or sub mounts. If the
1324 * mount has sub mounts this will return busy
1325 * regardless of whether the sub mounts are busy.
1326 *
1327 * Doesn't take quota and stuff into account. IOW, in some cases it will
1328 * give false negatives. The main reason why it's here is that we need
1329 * a non-destructive way to look for easily umountable filesystems.
1330 */
1331int may_umount(struct vfsmount *mnt)
1332{
e3474a8e 1333 int ret = 1;
8ad08d8a 1334 down_read(&namespace_sem);
719ea2fb 1335 lock_mount_hash();
1ab59738 1336 if (propagate_mount_busy(real_mount(mnt), 2))
e3474a8e 1337 ret = 0;
719ea2fb 1338 unlock_mount_hash();
8ad08d8a 1339 up_read(&namespace_sem);
a05964f3 1340 return ret;
1da177e4
LT
1341}
1342
1343EXPORT_SYMBOL(may_umount);
1344
38129a13 1345static HLIST_HEAD(unmounted); /* protected by namespace_sem */
e3197d83 1346
97216be0 1347static void namespace_unlock(void)
70fbcdf4 1348{
a3b3c562 1349 struct hlist_head head;
97216be0 1350
a3b3c562 1351 hlist_move_list(&unmounted, &head);
97216be0 1352
97216be0
AV
1353 up_write(&namespace_sem);
1354
a3b3c562
EB
1355 if (likely(hlist_empty(&head)))
1356 return;
1357
22cb7405 1358 synchronize_rcu_expedited();
48a066e7 1359
87b95ce0 1360 group_pin_kill(&head);
70fbcdf4
RP
1361}
1362
97216be0 1363static inline void namespace_lock(void)
e3197d83 1364{
97216be0 1365 down_write(&namespace_sem);
e3197d83
AV
1366}
1367
e819f152
EB
1368enum umount_tree_flags {
1369 UMOUNT_SYNC = 1,
1370 UMOUNT_PROPAGATE = 2,
e0c9c0af 1371 UMOUNT_CONNECTED = 4,
e819f152 1372};
f2d0a123
EB
1373
1374static bool disconnect_mount(struct mount *mnt, enum umount_tree_flags how)
1375{
1376 /* Leaving mounts connected is only valid for lazy umounts */
1377 if (how & UMOUNT_SYNC)
1378 return true;
1379
1380 /* A mount without a parent has nothing to be connected to */
1381 if (!mnt_has_parent(mnt))
1382 return true;
1383
1384 /* Because the reference counting rules change when mounts are
1385 * unmounted and connected, umounted mounts may not be
1386 * connected to mounted mounts.
1387 */
1388 if (!(mnt->mnt_parent->mnt.mnt_flags & MNT_UMOUNT))
1389 return true;
1390
1391 /* Has it been requested that the mount remain connected? */
1392 if (how & UMOUNT_CONNECTED)
1393 return false;
1394
1395 /* Is the mount locked such that it needs to remain connected? */
1396 if (IS_MNT_LOCKED(mnt))
1397 return false;
1398
1399 /* By default disconnect the mount */
1400 return true;
1401}
1402
99b7db7b 1403/*
48a066e7 1404 * mount_lock must be held
99b7db7b
NP
1405 * namespace_sem must be held for write
1406 */
e819f152 1407static void umount_tree(struct mount *mnt, enum umount_tree_flags how)
1da177e4 1408{
c003b26f 1409 LIST_HEAD(tmp_list);
315fc83e 1410 struct mount *p;
1da177e4 1411
5d88457e
EB
1412 if (how & UMOUNT_PROPAGATE)
1413 propagate_mount_unlock(mnt);
1414
c003b26f 1415 /* Gather the mounts to umount */
590ce4bc
EB
1416 for (p = mnt; p; p = next_mnt(p, mnt)) {
1417 p->mnt.mnt_flags |= MNT_UMOUNT;
c003b26f 1418 list_move(&p->mnt_list, &tmp_list);
590ce4bc 1419 }
1da177e4 1420
411a938b 1421 /* Hide the mounts from mnt_mounts */
c003b26f 1422 list_for_each_entry(p, &tmp_list, mnt_list) {
88b368f2 1423 list_del_init(&p->mnt_child);
c003b26f 1424 }
88b368f2 1425
c003b26f 1426 /* Add propogated mounts to the tmp_list */
e819f152 1427 if (how & UMOUNT_PROPAGATE)
7b8a53fd 1428 propagate_umount(&tmp_list);
a05964f3 1429
c003b26f 1430 while (!list_empty(&tmp_list)) {
d2921684 1431 struct mnt_namespace *ns;
ce07d891 1432 bool disconnect;
c003b26f 1433 p = list_first_entry(&tmp_list, struct mount, mnt_list);
6776db3d 1434 list_del_init(&p->mnt_expire);
1a4eeaf2 1435 list_del_init(&p->mnt_list);
d2921684
EB
1436 ns = p->mnt_ns;
1437 if (ns) {
1438 ns->mounts--;
1439 __touch_mnt_namespace(ns);
1440 }
143c8c91 1441 p->mnt_ns = NULL;
e819f152 1442 if (how & UMOUNT_SYNC)
48a066e7 1443 p->mnt.mnt_flags |= MNT_SYNC_UMOUNT;
87b95ce0 1444
f2d0a123 1445 disconnect = disconnect_mount(p, how);
ce07d891
EB
1446
1447 pin_insert_group(&p->mnt_umount, &p->mnt_parent->mnt,
1448 disconnect ? &unmounted : NULL);
676da58d 1449 if (mnt_has_parent(p)) {
81b6b061 1450 mnt_add_count(p->mnt_parent, -1);
ce07d891
EB
1451 if (!disconnect) {
1452 /* Don't forget about p */
1453 list_add_tail(&p->mnt_child, &p->mnt_parent->mnt_mounts);
1454 } else {
1455 umount_mnt(p);
1456 }
7c4b93d8 1457 }
0f0afb1d 1458 change_mnt_propagation(p, MS_PRIVATE);
1da177e4
LT
1459 }
1460}
1461
b54b9be7 1462static void shrink_submounts(struct mount *mnt);
c35038be 1463
1ab59738 1464static int do_umount(struct mount *mnt, int flags)
1da177e4 1465{
1ab59738 1466 struct super_block *sb = mnt->mnt.mnt_sb;
1da177e4
LT
1467 int retval;
1468
1ab59738 1469 retval = security_sb_umount(&mnt->mnt, flags);
1da177e4
LT
1470 if (retval)
1471 return retval;
1472
1473 /*
1474 * Allow userspace to request a mountpoint be expired rather than
1475 * unmounting unconditionally. Unmount only happens if:
1476 * (1) the mark is already set (the mark is cleared by mntput())
1477 * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
1478 */
1479 if (flags & MNT_EXPIRE) {
1ab59738 1480 if (&mnt->mnt == current->fs->root.mnt ||
1da177e4
LT
1481 flags & (MNT_FORCE | MNT_DETACH))
1482 return -EINVAL;
1483
b3e19d92
NP
1484 /*
1485 * probably don't strictly need the lock here if we examined
1486 * all race cases, but it's a slowpath.
1487 */
719ea2fb 1488 lock_mount_hash();
83adc753 1489 if (mnt_get_count(mnt) != 2) {
719ea2fb 1490 unlock_mount_hash();
1da177e4 1491 return -EBUSY;
b3e19d92 1492 }
719ea2fb 1493 unlock_mount_hash();
1da177e4 1494
863d684f 1495 if (!xchg(&mnt->mnt_expiry_mark, 1))
1da177e4
LT
1496 return -EAGAIN;
1497 }
1498
1499 /*
1500 * If we may have to abort operations to get out of this
1501 * mount, and they will themselves hold resources we must
1502 * allow the fs to do things. In the Unix tradition of
1503 * 'Gee thats tricky lets do it in userspace' the umount_begin
1504 * might fail to complete on the first run through as other tasks
1505 * must return, and the like. Thats for the mount program to worry
1506 * about for the moment.
1507 */
1508
42faad99 1509 if (flags & MNT_FORCE && sb->s_op->umount_begin) {
42faad99 1510 sb->s_op->umount_begin(sb);
42faad99 1511 }
1da177e4
LT
1512
1513 /*
1514 * No sense to grab the lock for this test, but test itself looks
1515 * somewhat bogus. Suggestions for better replacement?
1516 * Ho-hum... In principle, we might treat that as umount + switch
1517 * to rootfs. GC would eventually take care of the old vfsmount.
1518 * Actually it makes sense, especially if rootfs would contain a
1519 * /reboot - static binary that would close all descriptors and
1520 * call reboot(9). Then init(8) could umount root and exec /reboot.
1521 */
1ab59738 1522 if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
1da177e4
LT
1523 /*
1524 * Special case for "unmounting" root ...
1525 * we just try to remount it readonly.
1526 */
bc6155d1 1527 if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN))
a1480dcc 1528 return -EPERM;
1da177e4 1529 down_write(&sb->s_umount);
bc98a42c 1530 if (!sb_rdonly(sb))
e462ec50 1531 retval = do_remount_sb(sb, SB_RDONLY, NULL, 0);
1da177e4
LT
1532 up_write(&sb->s_umount);
1533 return retval;
1534 }
1535
97216be0 1536 namespace_lock();
719ea2fb 1537 lock_mount_hash();
1da177e4 1538
25d202ed
EB
1539 /* Recheck MNT_LOCKED with the locks held */
1540 retval = -EINVAL;
1541 if (mnt->mnt.mnt_flags & MNT_LOCKED)
1542 goto out;
1543
1544 event++;
48a066e7 1545 if (flags & MNT_DETACH) {
1a4eeaf2 1546 if (!list_empty(&mnt->mnt_list))
e819f152 1547 umount_tree(mnt, UMOUNT_PROPAGATE);
1da177e4 1548 retval = 0;
48a066e7
AV
1549 } else {
1550 shrink_submounts(mnt);
1551 retval = -EBUSY;
1552 if (!propagate_mount_busy(mnt, 2)) {
1553 if (!list_empty(&mnt->mnt_list))
e819f152 1554 umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
48a066e7
AV
1555 retval = 0;
1556 }
1da177e4 1557 }
25d202ed 1558out:
719ea2fb 1559 unlock_mount_hash();
e3197d83 1560 namespace_unlock();
1da177e4
LT
1561 return retval;
1562}
1563
80b5dce8
EB
1564/*
1565 * __detach_mounts - lazily unmount all mounts on the specified dentry
1566 *
1567 * During unlink, rmdir, and d_drop it is possible to loose the path
1568 * to an existing mountpoint, and wind up leaking the mount.
1569 * detach_mounts allows lazily unmounting those mounts instead of
1570 * leaking them.
1571 *
1572 * The caller may hold dentry->d_inode->i_mutex.
1573 */
1574void __detach_mounts(struct dentry *dentry)
1575{
1576 struct mountpoint *mp;
1577 struct mount *mnt;
1578
1579 namespace_lock();
3895dbf8 1580 lock_mount_hash();
80b5dce8 1581 mp = lookup_mountpoint(dentry);
f53e5797 1582 if (IS_ERR_OR_NULL(mp))
80b5dce8
EB
1583 goto out_unlock;
1584
e06b933e 1585 event++;
80b5dce8
EB
1586 while (!hlist_empty(&mp->m_list)) {
1587 mnt = hlist_entry(mp->m_list.first, struct mount, mnt_mp_list);
ce07d891 1588 if (mnt->mnt.mnt_flags & MNT_UMOUNT) {
fe78fcc8
EB
1589 hlist_add_head(&mnt->mnt_umount.s_list, &unmounted);
1590 umount_mnt(mnt);
ce07d891 1591 }
e0c9c0af 1592 else umount_tree(mnt, UMOUNT_CONNECTED);
80b5dce8 1593 }
80b5dce8
EB
1594 put_mountpoint(mp);
1595out_unlock:
3895dbf8 1596 unlock_mount_hash();
80b5dce8
EB
1597 namespace_unlock();
1598}
1599
dd111b31 1600/*
9b40bc90
AV
1601 * Is the caller allowed to modify his namespace?
1602 */
1603static inline bool may_mount(void)
1604{
1605 return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN);
1606}
1607
9e8925b6
JL
1608static inline bool may_mandlock(void)
1609{
1610#ifndef CONFIG_MANDATORY_FILE_LOCKING
1611 return false;
1612#endif
95ace754 1613 return capable(CAP_SYS_ADMIN);
9e8925b6
JL
1614}
1615
1da177e4
LT
1616/*
1617 * Now umount can handle mount points as well as block devices.
1618 * This is important for filesystems which use unnamed block devices.
1619 *
1620 * We now support a flag for forced unmount like the other 'big iron'
1621 * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
1622 */
1623
3a18ef5c 1624int ksys_umount(char __user *name, int flags)
1da177e4 1625{
2d8f3038 1626 struct path path;
900148dc 1627 struct mount *mnt;
1da177e4 1628 int retval;
db1f05bb 1629 int lookup_flags = 0;
1da177e4 1630
db1f05bb
MS
1631 if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
1632 return -EINVAL;
1633
9b40bc90
AV
1634 if (!may_mount())
1635 return -EPERM;
1636
db1f05bb
MS
1637 if (!(flags & UMOUNT_NOFOLLOW))
1638 lookup_flags |= LOOKUP_FOLLOW;
1639
197df04c 1640 retval = user_path_mountpoint_at(AT_FDCWD, name, lookup_flags, &path);
1da177e4
LT
1641 if (retval)
1642 goto out;
900148dc 1643 mnt = real_mount(path.mnt);
1da177e4 1644 retval = -EINVAL;
2d8f3038 1645 if (path.dentry != path.mnt->mnt_root)
1da177e4 1646 goto dput_and_out;
143c8c91 1647 if (!check_mnt(mnt))
1da177e4 1648 goto dput_and_out;
25d202ed 1649 if (mnt->mnt.mnt_flags & MNT_LOCKED) /* Check optimistically */
5ff9d8a6 1650 goto dput_and_out;
b2f5d4dc
EB
1651 retval = -EPERM;
1652 if (flags & MNT_FORCE && !capable(CAP_SYS_ADMIN))
1653 goto dput_and_out;
1da177e4 1654
900148dc 1655 retval = do_umount(mnt, flags);
1da177e4 1656dput_and_out:
429731b1 1657 /* we mustn't call path_put() as that would clear mnt_expiry_mark */
2d8f3038 1658 dput(path.dentry);
900148dc 1659 mntput_no_expire(mnt);
1da177e4
LT
1660out:
1661 return retval;
1662}
1663
3a18ef5c
DB
1664SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
1665{
1666 return ksys_umount(name, flags);
1667}
1668
1da177e4
LT
1669#ifdef __ARCH_WANT_SYS_OLDUMOUNT
1670
1671/*
b58fed8b 1672 * The 2.0 compatible umount. No flags.
1da177e4 1673 */
bdc480e3 1674SYSCALL_DEFINE1(oldumount, char __user *, name)
1da177e4 1675{
3a18ef5c 1676 return ksys_umount(name, 0);
1da177e4
LT
1677}
1678
1679#endif
1680
4ce5d2b1 1681static bool is_mnt_ns_file(struct dentry *dentry)
8823c079 1682{
4ce5d2b1 1683 /* Is this a proxy for a mount namespace? */
e149ed2b
AV
1684 return dentry->d_op == &ns_dentry_operations &&
1685 dentry->d_fsdata == &mntns_operations;
4ce5d2b1
EB
1686}
1687
58be2825
AV
1688struct mnt_namespace *to_mnt_ns(struct ns_common *ns)
1689{
1690 return container_of(ns, struct mnt_namespace, ns);
1691}
1692
4ce5d2b1
EB
1693static bool mnt_ns_loop(struct dentry *dentry)
1694{
1695 /* Could bind mounting the mount namespace inode cause a
1696 * mount namespace loop?
1697 */
1698 struct mnt_namespace *mnt_ns;
1699 if (!is_mnt_ns_file(dentry))
1700 return false;
1701
f77c8014 1702 mnt_ns = to_mnt_ns(get_proc_ns(dentry->d_inode));
8823c079
EB
1703 return current->nsproxy->mnt_ns->seq >= mnt_ns->seq;
1704}
1705
87129cc0 1706struct mount *copy_tree(struct mount *mnt, struct dentry *dentry,
36341f64 1707 int flag)
1da177e4 1708{
84d17192 1709 struct mount *res, *p, *q, *r, *parent;
1da177e4 1710
4ce5d2b1
EB
1711 if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt))
1712 return ERR_PTR(-EINVAL);
1713
1714 if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry))
be34d1a3 1715 return ERR_PTR(-EINVAL);
9676f0c6 1716
36341f64 1717 res = q = clone_mnt(mnt, dentry, flag);
be34d1a3
DH
1718 if (IS_ERR(q))
1719 return q;
1720
a73324da 1721 q->mnt_mountpoint = mnt->mnt_mountpoint;
1da177e4
LT
1722
1723 p = mnt;
6b41d536 1724 list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
315fc83e 1725 struct mount *s;
7ec02ef1 1726 if (!is_subdir(r->mnt_mountpoint, dentry))
1da177e4
LT
1727 continue;
1728
909b0a88 1729 for (s = r; s; s = next_mnt(s, r)) {
4ce5d2b1
EB
1730 if (!(flag & CL_COPY_UNBINDABLE) &&
1731 IS_MNT_UNBINDABLE(s)) {
df7342b2
EB
1732 if (s->mnt.mnt_flags & MNT_LOCKED) {
1733 /* Both unbindable and locked. */
1734 q = ERR_PTR(-EPERM);
1735 goto out;
1736 } else {
1737 s = skip_mnt_tree(s);
1738 continue;
1739 }
4ce5d2b1
EB
1740 }
1741 if (!(flag & CL_COPY_MNT_NS_FILE) &&
1742 is_mnt_ns_file(s->mnt.mnt_root)) {
9676f0c6
RP
1743 s = skip_mnt_tree(s);
1744 continue;
1745 }
0714a533
AV
1746 while (p != s->mnt_parent) {
1747 p = p->mnt_parent;
1748 q = q->mnt_parent;
1da177e4 1749 }
87129cc0 1750 p = s;
84d17192 1751 parent = q;
87129cc0 1752 q = clone_mnt(p, p->mnt.mnt_root, flag);
be34d1a3
DH
1753 if (IS_ERR(q))
1754 goto out;
719ea2fb 1755 lock_mount_hash();
1a4eeaf2 1756 list_add_tail(&q->mnt_list, &res->mnt_list);
1064f874 1757 attach_mnt(q, parent, p->mnt_mp);
719ea2fb 1758 unlock_mount_hash();
1da177e4
LT
1759 }
1760 }
1761 return res;
be34d1a3 1762out:
1da177e4 1763 if (res) {
719ea2fb 1764 lock_mount_hash();
e819f152 1765 umount_tree(res, UMOUNT_SYNC);
719ea2fb 1766 unlock_mount_hash();
1da177e4 1767 }
be34d1a3 1768 return q;
1da177e4
LT
1769}
1770
be34d1a3
DH
1771/* Caller should check returned pointer for errors */
1772
ca71cf71 1773struct vfsmount *collect_mounts(const struct path *path)
8aec0809 1774{
cb338d06 1775 struct mount *tree;
97216be0 1776 namespace_lock();
cd4a4017
EB
1777 if (!check_mnt(real_mount(path->mnt)))
1778 tree = ERR_PTR(-EINVAL);
1779 else
1780 tree = copy_tree(real_mount(path->mnt), path->dentry,
1781 CL_COPY_ALL | CL_PRIVATE);
328e6d90 1782 namespace_unlock();
be34d1a3 1783 if (IS_ERR(tree))
52e220d3 1784 return ERR_CAST(tree);
be34d1a3 1785 return &tree->mnt;
8aec0809
AV
1786}
1787
1788void drop_collected_mounts(struct vfsmount *mnt)
1789{
97216be0 1790 namespace_lock();
719ea2fb 1791 lock_mount_hash();
9c8e0a1b 1792 umount_tree(real_mount(mnt), 0);
719ea2fb 1793 unlock_mount_hash();
3ab6abee 1794 namespace_unlock();
8aec0809
AV
1795}
1796
c771d683
MS
1797/**
1798 * clone_private_mount - create a private clone of a path
1799 *
1800 * This creates a new vfsmount, which will be the clone of @path. The new will
1801 * not be attached anywhere in the namespace and will be private (i.e. changes
1802 * to the originating mount won't be propagated into this).
1803 *
1804 * Release with mntput().
1805 */
ca71cf71 1806struct vfsmount *clone_private_mount(const struct path *path)
c771d683
MS
1807{
1808 struct mount *old_mnt = real_mount(path->mnt);
1809 struct mount *new_mnt;
1810
1811 if (IS_MNT_UNBINDABLE(old_mnt))
1812 return ERR_PTR(-EINVAL);
1813
c771d683 1814 new_mnt = clone_mnt(old_mnt, path->dentry, CL_PRIVATE);
c771d683
MS
1815 if (IS_ERR(new_mnt))
1816 return ERR_CAST(new_mnt);
1817
1818 return &new_mnt->mnt;
1819}
1820EXPORT_SYMBOL_GPL(clone_private_mount);
1821
1f707137
AV
1822int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
1823 struct vfsmount *root)
1824{
1a4eeaf2 1825 struct mount *mnt;
1f707137
AV
1826 int res = f(root, arg);
1827 if (res)
1828 return res;
1a4eeaf2
AV
1829 list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) {
1830 res = f(&mnt->mnt, arg);
1f707137
AV
1831 if (res)
1832 return res;
1833 }
1834 return 0;
1835}
1836
3bd045cc
AV
1837static void lock_mnt_tree(struct mount *mnt)
1838{
1839 struct mount *p;
1840
1841 for (p = mnt; p; p = next_mnt(p, mnt)) {
1842 int flags = p->mnt.mnt_flags;
1843 /* Don't allow unprivileged users to change mount flags */
1844 flags |= MNT_LOCK_ATIME;
1845
1846 if (flags & MNT_READONLY)
1847 flags |= MNT_LOCK_READONLY;
1848
1849 if (flags & MNT_NODEV)
1850 flags |= MNT_LOCK_NODEV;
1851
1852 if (flags & MNT_NOSUID)
1853 flags |= MNT_LOCK_NOSUID;
1854
1855 if (flags & MNT_NOEXEC)
1856 flags |= MNT_LOCK_NOEXEC;
1857 /* Don't allow unprivileged users to reveal what is under a mount */
1858 if (list_empty(&p->mnt_expire))
1859 flags |= MNT_LOCKED;
1860 p->mnt.mnt_flags = flags;
1861 }
1862}
1863
4b8b21f4 1864static void cleanup_group_ids(struct mount *mnt, struct mount *end)
719f5d7f 1865{
315fc83e 1866 struct mount *p;
719f5d7f 1867
909b0a88 1868 for (p = mnt; p != end; p = next_mnt(p, mnt)) {
fc7be130 1869 if (p->mnt_group_id && !IS_MNT_SHARED(p))
4b8b21f4 1870 mnt_release_group_id(p);
719f5d7f
MS
1871 }
1872}
1873
4b8b21f4 1874static int invent_group_ids(struct mount *mnt, bool recurse)
719f5d7f 1875{
315fc83e 1876 struct mount *p;
719f5d7f 1877
909b0a88 1878 for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
fc7be130 1879 if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
4b8b21f4 1880 int err = mnt_alloc_group_id(p);
719f5d7f 1881 if (err) {
4b8b21f4 1882 cleanup_group_ids(mnt, p);
719f5d7f
MS
1883 return err;
1884 }
1885 }
1886 }
1887
1888 return 0;
1889}
1890
d2921684
EB
1891int count_mounts(struct mnt_namespace *ns, struct mount *mnt)
1892{
1893 unsigned int max = READ_ONCE(sysctl_mount_max);
1894 unsigned int mounts = 0, old, pending, sum;
1895 struct mount *p;
1896
1897 for (p = mnt; p; p = next_mnt(p, mnt))
1898 mounts++;
1899
1900 old = ns->mounts;
1901 pending = ns->pending_mounts;
1902 sum = old + pending;
1903 if ((old > sum) ||
1904 (pending > sum) ||
1905 (max < sum) ||
1906 (mounts > (max - sum)))
1907 return -ENOSPC;
1908
1909 ns->pending_mounts = pending + mounts;
1910 return 0;
1911}
1912
b90fa9ae
RP
1913/*
1914 * @source_mnt : mount tree to be attached
21444403
RP
1915 * @nd : place the mount tree @source_mnt is attached
1916 * @parent_nd : if non-null, detach the source_mnt from its parent and
1917 * store the parent mount and mountpoint dentry.
1918 * (done when source_mnt is moved)
b90fa9ae
RP
1919 *
1920 * NOTE: in the table below explains the semantics when a source mount
1921 * of a given type is attached to a destination mount of a given type.
9676f0c6
RP
1922 * ---------------------------------------------------------------------------
1923 * | BIND MOUNT OPERATION |
1924 * |**************************************************************************
1925 * | source-->| shared | private | slave | unbindable |
1926 * | dest | | | | |
1927 * | | | | | | |
1928 * | v | | | | |
1929 * |**************************************************************************
1930 * | shared | shared (++) | shared (+) | shared(+++)| invalid |
1931 * | | | | | |
1932 * |non-shared| shared (+) | private | slave (*) | invalid |
1933 * ***************************************************************************
b90fa9ae
RP
1934 * A bind operation clones the source mount and mounts the clone on the
1935 * destination mount.
1936 *
1937 * (++) the cloned mount is propagated to all the mounts in the propagation
1938 * tree of the destination mount and the cloned mount is added to
1939 * the peer group of the source mount.
1940 * (+) the cloned mount is created under the destination mount and is marked
1941 * as shared. The cloned mount is added to the peer group of the source
1942 * mount.
5afe0022
RP
1943 * (+++) the mount is propagated to all the mounts in the propagation tree
1944 * of the destination mount and the cloned mount is made slave
1945 * of the same master as that of the source mount. The cloned mount
1946 * is marked as 'shared and slave'.
1947 * (*) the cloned mount is made a slave of the same master as that of the
1948 * source mount.
1949 *
9676f0c6
RP
1950 * ---------------------------------------------------------------------------
1951 * | MOVE MOUNT OPERATION |
1952 * |**************************************************************************
1953 * | source-->| shared | private | slave | unbindable |
1954 * | dest | | | | |
1955 * | | | | | | |
1956 * | v | | | | |
1957 * |**************************************************************************
1958 * | shared | shared (+) | shared (+) | shared(+++) | invalid |
1959 * | | | | | |
1960 * |non-shared| shared (+*) | private | slave (*) | unbindable |
1961 * ***************************************************************************
5afe0022
RP
1962 *
1963 * (+) the mount is moved to the destination. And is then propagated to
1964 * all the mounts in the propagation tree of the destination mount.
21444403 1965 * (+*) the mount is moved to the destination.
5afe0022
RP
1966 * (+++) the mount is moved to the destination and is then propagated to
1967 * all the mounts belonging to the destination mount's propagation tree.
1968 * the mount is marked as 'shared and slave'.
1969 * (*) the mount continues to be a slave at the new location.
b90fa9ae
RP
1970 *
1971 * if the source mount is a tree, the operations explained above is
1972 * applied to each mount in the tree.
1973 * Must be called without spinlocks held, since this function can sleep
1974 * in allocations.
1975 */
0fb54e50 1976static int attach_recursive_mnt(struct mount *source_mnt,
84d17192
AV
1977 struct mount *dest_mnt,
1978 struct mountpoint *dest_mp,
1979 struct path *parent_path)
b90fa9ae 1980{
3bd045cc 1981 struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns;
38129a13 1982 HLIST_HEAD(tree_list);
d2921684 1983 struct mnt_namespace *ns = dest_mnt->mnt_ns;
1064f874 1984 struct mountpoint *smp;
315fc83e 1985 struct mount *child, *p;
38129a13 1986 struct hlist_node *n;
719f5d7f 1987 int err;
b90fa9ae 1988
1064f874
EB
1989 /* Preallocate a mountpoint in case the new mounts need
1990 * to be tucked under other mounts.
1991 */
1992 smp = get_mountpoint(source_mnt->mnt.mnt_root);
1993 if (IS_ERR(smp))
1994 return PTR_ERR(smp);
1995
d2921684
EB
1996 /* Is there space to add these mounts to the mount namespace? */
1997 if (!parent_path) {
1998 err = count_mounts(ns, source_mnt);
1999 if (err)
2000 goto out;
2001 }
2002
fc7be130 2003 if (IS_MNT_SHARED(dest_mnt)) {
0fb54e50 2004 err = invent_group_ids(source_mnt, true);
719f5d7f
MS
2005 if (err)
2006 goto out;
0b1b901b 2007 err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list);
f2ebb3a9 2008 lock_mount_hash();
0b1b901b
AV
2009 if (err)
2010 goto out_cleanup_ids;
909b0a88 2011 for (p = source_mnt; p; p = next_mnt(p, source_mnt))
0f0afb1d 2012 set_mnt_shared(p);
0b1b901b
AV
2013 } else {
2014 lock_mount_hash();
b90fa9ae 2015 }
1a390689 2016 if (parent_path) {
0fb54e50 2017 detach_mnt(source_mnt, parent_path);
84d17192 2018 attach_mnt(source_mnt, dest_mnt, dest_mp);
143c8c91 2019 touch_mnt_namespace(source_mnt->mnt_ns);
21444403 2020 } else {
84d17192 2021 mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt);
1064f874 2022 commit_tree(source_mnt);
21444403 2023 }
b90fa9ae 2024
38129a13 2025 hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) {
1d6a32ac 2026 struct mount *q;
38129a13 2027 hlist_del_init(&child->mnt_hash);
1064f874
EB
2028 q = __lookup_mnt(&child->mnt_parent->mnt,
2029 child->mnt_mountpoint);
2030 if (q)
2031 mnt_change_mountpoint(child, smp, q);
3bd045cc
AV
2032 /* Notice when we are propagating across user namespaces */
2033 if (child->mnt_parent->mnt_ns->user_ns != user_ns)
2034 lock_mnt_tree(child);
1064f874 2035 commit_tree(child);
b90fa9ae 2036 }
1064f874 2037 put_mountpoint(smp);
719ea2fb 2038 unlock_mount_hash();
99b7db7b 2039
b90fa9ae 2040 return 0;
719f5d7f
MS
2041
2042 out_cleanup_ids:
f2ebb3a9
AV
2043 while (!hlist_empty(&tree_list)) {
2044 child = hlist_entry(tree_list.first, struct mount, mnt_hash);
d2921684 2045 child->mnt_parent->mnt_ns->pending_mounts = 0;
e819f152 2046 umount_tree(child, UMOUNT_SYNC);
f2ebb3a9
AV
2047 }
2048 unlock_mount_hash();
0b1b901b 2049 cleanup_group_ids(source_mnt, NULL);
719f5d7f 2050 out:
d2921684 2051 ns->pending_mounts = 0;
1064f874
EB
2052
2053 read_seqlock_excl(&mount_lock);
2054 put_mountpoint(smp);
2055 read_sequnlock_excl(&mount_lock);
2056
719f5d7f 2057 return err;
b90fa9ae
RP
2058}
2059
84d17192 2060static struct mountpoint *lock_mount(struct path *path)
b12cea91
AV
2061{
2062 struct vfsmount *mnt;
84d17192 2063 struct dentry *dentry = path->dentry;
b12cea91 2064retry:
5955102c 2065 inode_lock(dentry->d_inode);
84d17192 2066 if (unlikely(cant_mount(dentry))) {
5955102c 2067 inode_unlock(dentry->d_inode);
84d17192 2068 return ERR_PTR(-ENOENT);
b12cea91 2069 }
97216be0 2070 namespace_lock();
b12cea91 2071 mnt = lookup_mnt(path);
84d17192 2072 if (likely(!mnt)) {
3895dbf8 2073 struct mountpoint *mp = get_mountpoint(dentry);
84d17192 2074 if (IS_ERR(mp)) {
97216be0 2075 namespace_unlock();
5955102c 2076 inode_unlock(dentry->d_inode);
84d17192
AV
2077 return mp;
2078 }
2079 return mp;
2080 }
97216be0 2081 namespace_unlock();
5955102c 2082 inode_unlock(path->dentry->d_inode);
b12cea91
AV
2083 path_put(path);
2084 path->mnt = mnt;
84d17192 2085 dentry = path->dentry = dget(mnt->mnt_root);
b12cea91
AV
2086 goto retry;
2087}
2088
84d17192 2089static void unlock_mount(struct mountpoint *where)
b12cea91 2090{
84d17192 2091 struct dentry *dentry = where->m_dentry;
3895dbf8
EB
2092
2093 read_seqlock_excl(&mount_lock);
84d17192 2094 put_mountpoint(where);
3895dbf8
EB
2095 read_sequnlock_excl(&mount_lock);
2096
328e6d90 2097 namespace_unlock();
5955102c 2098 inode_unlock(dentry->d_inode);
b12cea91
AV
2099}
2100
84d17192 2101static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp)
1da177e4 2102{
e462ec50 2103 if (mnt->mnt.mnt_sb->s_flags & SB_NOUSER)
1da177e4
LT
2104 return -EINVAL;
2105
e36cb0b8
DH
2106 if (d_is_dir(mp->m_dentry) !=
2107 d_is_dir(mnt->mnt.mnt_root))
1da177e4
LT
2108 return -ENOTDIR;
2109
84d17192 2110 return attach_recursive_mnt(mnt, p, mp, NULL);
1da177e4
LT
2111}
2112
7a2e8a8f
VA
2113/*
2114 * Sanity check the flags to change_mnt_propagation.
2115 */
2116
e462ec50 2117static int flags_to_propagation_type(int ms_flags)
7a2e8a8f 2118{
e462ec50 2119 int type = ms_flags & ~(MS_REC | MS_SILENT);
7a2e8a8f
VA
2120
2121 /* Fail if any non-propagation flags are set */
2122 if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
2123 return 0;
2124 /* Only one propagation flag should be set */
2125 if (!is_power_of_2(type))
2126 return 0;
2127 return type;
2128}
2129
07b20889
RP
2130/*
2131 * recursively change the type of the mountpoint.
2132 */
e462ec50 2133static int do_change_type(struct path *path, int ms_flags)
07b20889 2134{
315fc83e 2135 struct mount *m;
4b8b21f4 2136 struct mount *mnt = real_mount(path->mnt);
e462ec50 2137 int recurse = ms_flags & MS_REC;
7a2e8a8f 2138 int type;
719f5d7f 2139 int err = 0;
07b20889 2140
2d92ab3c 2141 if (path->dentry != path->mnt->mnt_root)
07b20889
RP
2142 return -EINVAL;
2143
e462ec50 2144 type = flags_to_propagation_type(ms_flags);
7a2e8a8f
VA
2145 if (!type)
2146 return -EINVAL;
2147
97216be0 2148 namespace_lock();
719f5d7f
MS
2149 if (type == MS_SHARED) {
2150 err = invent_group_ids(mnt, recurse);
2151 if (err)
2152 goto out_unlock;
2153 }
2154
719ea2fb 2155 lock_mount_hash();
909b0a88 2156 for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
0f0afb1d 2157 change_mnt_propagation(m, type);
719ea2fb 2158 unlock_mount_hash();
719f5d7f
MS
2159
2160 out_unlock:
97216be0 2161 namespace_unlock();
719f5d7f 2162 return err;
07b20889
RP
2163}
2164
5ff9d8a6
EB
2165static bool has_locked_children(struct mount *mnt, struct dentry *dentry)
2166{
2167 struct mount *child;
2168 list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
2169 if (!is_subdir(child->mnt_mountpoint, dentry))
2170 continue;
2171
2172 if (child->mnt.mnt_flags & MNT_LOCKED)
2173 return true;
2174 }
2175 return false;
2176}
2177
1da177e4
LT
2178/*
2179 * do loopback mount.
2180 */
808d4e3c 2181static int do_loopback(struct path *path, const char *old_name,
2dafe1c4 2182 int recurse)
1da177e4 2183{
2d92ab3c 2184 struct path old_path;
84d17192
AV
2185 struct mount *mnt = NULL, *old, *parent;
2186 struct mountpoint *mp;
57eccb83 2187 int err;
1da177e4
LT
2188 if (!old_name || !*old_name)
2189 return -EINVAL;
815d405c 2190 err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
1da177e4
LT
2191 if (err)
2192 return err;
2193
8823c079 2194 err = -EINVAL;
4ce5d2b1 2195 if (mnt_ns_loop(old_path.dentry))
dd111b31 2196 goto out;
8823c079 2197
84d17192
AV
2198 mp = lock_mount(path);
2199 err = PTR_ERR(mp);
2200 if (IS_ERR(mp))
b12cea91
AV
2201 goto out;
2202
87129cc0 2203 old = real_mount(old_path.mnt);
84d17192 2204 parent = real_mount(path->mnt);
87129cc0 2205
1da177e4 2206 err = -EINVAL;
fc7be130 2207 if (IS_MNT_UNBINDABLE(old))
b12cea91 2208 goto out2;
9676f0c6 2209
e149ed2b
AV
2210 if (!check_mnt(parent))
2211 goto out2;
2212
2213 if (!check_mnt(old) && old_path.dentry->d_op != &ns_dentry_operations)
b12cea91 2214 goto out2;
1da177e4 2215
5ff9d8a6
EB
2216 if (!recurse && has_locked_children(old, old_path.dentry))
2217 goto out2;
2218
ccd48bc7 2219 if (recurse)
4ce5d2b1 2220 mnt = copy_tree(old, old_path.dentry, CL_COPY_MNT_NS_FILE);
ccd48bc7 2221 else
87129cc0 2222 mnt = clone_mnt(old, old_path.dentry, 0);
ccd48bc7 2223
be34d1a3
DH
2224 if (IS_ERR(mnt)) {
2225 err = PTR_ERR(mnt);
e9c5d8a5 2226 goto out2;
be34d1a3 2227 }
ccd48bc7 2228
5ff9d8a6
EB
2229 mnt->mnt.mnt_flags &= ~MNT_LOCKED;
2230
84d17192 2231 err = graft_tree(mnt, parent, mp);
ccd48bc7 2232 if (err) {
719ea2fb 2233 lock_mount_hash();
e819f152 2234 umount_tree(mnt, UMOUNT_SYNC);
719ea2fb 2235 unlock_mount_hash();
5b83d2c5 2236 }
b12cea91 2237out2:
84d17192 2238 unlock_mount(mp);
ccd48bc7 2239out:
2d92ab3c 2240 path_put(&old_path);
1da177e4
LT
2241 return err;
2242}
2243
43f5e655
DH
2244/*
2245 * Don't allow locked mount flags to be cleared.
2246 *
2247 * No locks need to be held here while testing the various MNT_LOCK
2248 * flags because those flags can never be cleared once they are set.
2249 */
2250static bool can_change_locked_flags(struct mount *mnt, unsigned int mnt_flags)
2e4b7fcd 2251{
43f5e655
DH
2252 unsigned int fl = mnt->mnt.mnt_flags;
2253
2254 if ((fl & MNT_LOCK_READONLY) &&
2255 !(mnt_flags & MNT_READONLY))
2256 return false;
2257
2258 if ((fl & MNT_LOCK_NODEV) &&
2259 !(mnt_flags & MNT_NODEV))
2260 return false;
2261
2262 if ((fl & MNT_LOCK_NOSUID) &&
2263 !(mnt_flags & MNT_NOSUID))
2264 return false;
2265
2266 if ((fl & MNT_LOCK_NOEXEC) &&
2267 !(mnt_flags & MNT_NOEXEC))
2268 return false;
2269
2270 if ((fl & MNT_LOCK_ATIME) &&
2271 ((fl & MNT_ATIME_MASK) != (mnt_flags & MNT_ATIME_MASK)))
2272 return false;
2e4b7fcd 2273
43f5e655
DH
2274 return true;
2275}
2276
2277static int change_mount_ro_state(struct mount *mnt, unsigned int mnt_flags)
2e4b7fcd 2278{
43f5e655 2279 bool readonly_request = (mnt_flags & MNT_READONLY);
2e4b7fcd 2280
43f5e655 2281 if (readonly_request == __mnt_is_readonly(&mnt->mnt))
2e4b7fcd
DH
2282 return 0;
2283
2284 if (readonly_request)
43f5e655
DH
2285 return mnt_make_readonly(mnt);
2286
2287 return __mnt_unmake_readonly(mnt);
2288}
2289
2290/*
2291 * Update the user-settable attributes on a mount. The caller must hold
2292 * sb->s_umount for writing.
2293 */
2294static void set_mount_attributes(struct mount *mnt, unsigned int mnt_flags)
2295{
2296 lock_mount_hash();
2297 mnt_flags |= mnt->mnt.mnt_flags & ~MNT_USER_SETTABLE_MASK;
2298 mnt->mnt.mnt_flags = mnt_flags;
2299 touch_mnt_namespace(mnt->mnt_ns);
2300 unlock_mount_hash();
2301}
2302
2303/*
2304 * Handle reconfiguration of the mountpoint only without alteration of the
2305 * superblock it refers to. This is triggered by specifying MS_REMOUNT|MS_BIND
2306 * to mount(2).
2307 */
2308static int do_reconfigure_mnt(struct path *path, unsigned int mnt_flags)
2309{
2310 struct super_block *sb = path->mnt->mnt_sb;
2311 struct mount *mnt = real_mount(path->mnt);
2312 int ret;
2313
2314 if (!check_mnt(mnt))
2315 return -EINVAL;
2316
2317 if (path->dentry != mnt->mnt.mnt_root)
2318 return -EINVAL;
2319
2320 if (!can_change_locked_flags(mnt, mnt_flags))
2321 return -EPERM;
2322
2323 down_write(&sb->s_umount);
2324 ret = change_mount_ro_state(mnt, mnt_flags);
2325 if (ret == 0)
2326 set_mount_attributes(mnt, mnt_flags);
2327 up_write(&sb->s_umount);
2328 return ret;
2e4b7fcd
DH
2329}
2330
1da177e4
LT
2331/*
2332 * change filesystem flags. dir should be a physical root of filesystem.
2333 * If you've mounted a non-root directory somewhere and want to do remount
2334 * on it - tough luck.
2335 */
e462ec50
DH
2336static int do_remount(struct path *path, int ms_flags, int sb_flags,
2337 int mnt_flags, void *data)
1da177e4
LT
2338{
2339 int err;
2d92ab3c 2340 struct super_block *sb = path->mnt->mnt_sb;
143c8c91 2341 struct mount *mnt = real_mount(path->mnt);
204cc0cc 2342 void *sec_opts = NULL;
1da177e4 2343
143c8c91 2344 if (!check_mnt(mnt))
1da177e4
LT
2345 return -EINVAL;
2346
2d92ab3c 2347 if (path->dentry != path->mnt->mnt_root)
1da177e4
LT
2348 return -EINVAL;
2349
43f5e655 2350 if (!can_change_locked_flags(mnt, mnt_flags))
9566d674 2351 return -EPERM;
9566d674 2352
c039bc3c 2353 if (data && !(sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)) {
204cc0cc 2354 err = security_sb_eat_lsm_opts(data, &sec_opts);
c039bc3c
AV
2355 if (err)
2356 return err;
2357 }
204cc0cc
AV
2358 err = security_sb_remount(sb, sec_opts);
2359 security_free_mnt_opts(&sec_opts);
ff36fe2c
EP
2360 if (err)
2361 return err;
2362
1da177e4 2363 down_write(&sb->s_umount);
43f5e655
DH
2364 err = -EPERM;
2365 if (ns_capable(sb->s_user_ns, CAP_SYS_ADMIN)) {
e462ec50 2366 err = do_remount_sb(sb, sb_flags, data, 0);
43f5e655
DH
2367 if (!err)
2368 set_mount_attributes(mnt, mnt_flags);
0e55a7cc 2369 }
6339dab8 2370 up_write(&sb->s_umount);
1da177e4
LT
2371 return err;
2372}
2373
cbbe362c 2374static inline int tree_contains_unbindable(struct mount *mnt)
9676f0c6 2375{
315fc83e 2376 struct mount *p;
909b0a88 2377 for (p = mnt; p; p = next_mnt(p, mnt)) {
fc7be130 2378 if (IS_MNT_UNBINDABLE(p))
9676f0c6
RP
2379 return 1;
2380 }
2381 return 0;
2382}
2383
808d4e3c 2384static int do_move_mount(struct path *path, const char *old_name)
1da177e4 2385{
2d92ab3c 2386 struct path old_path, parent_path;
676da58d 2387 struct mount *p;
0fb54e50 2388 struct mount *old;
84d17192 2389 struct mountpoint *mp;
57eccb83 2390 int err;
1da177e4
LT
2391 if (!old_name || !*old_name)
2392 return -EINVAL;
2d92ab3c 2393 err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
1da177e4
LT
2394 if (err)
2395 return err;
2396
84d17192
AV
2397 mp = lock_mount(path);
2398 err = PTR_ERR(mp);
2399 if (IS_ERR(mp))
cc53ce53
DH
2400 goto out;
2401
143c8c91 2402 old = real_mount(old_path.mnt);
fc7be130 2403 p = real_mount(path->mnt);
143c8c91 2404
1da177e4 2405 err = -EINVAL;
fc7be130 2406 if (!check_mnt(p) || !check_mnt(old))
1da177e4
LT
2407 goto out1;
2408
5ff9d8a6
EB
2409 if (old->mnt.mnt_flags & MNT_LOCKED)
2410 goto out1;
2411
1da177e4 2412 err = -EINVAL;
2d92ab3c 2413 if (old_path.dentry != old_path.mnt->mnt_root)
21444403 2414 goto out1;
1da177e4 2415
676da58d 2416 if (!mnt_has_parent(old))
21444403 2417 goto out1;
1da177e4 2418
e36cb0b8
DH
2419 if (d_is_dir(path->dentry) !=
2420 d_is_dir(old_path.dentry))
21444403
RP
2421 goto out1;
2422 /*
2423 * Don't move a mount residing in a shared parent.
2424 */
fc7be130 2425 if (IS_MNT_SHARED(old->mnt_parent))
21444403 2426 goto out1;
9676f0c6
RP
2427 /*
2428 * Don't move a mount tree containing unbindable mounts to a destination
2429 * mount which is shared.
2430 */
fc7be130 2431 if (IS_MNT_SHARED(p) && tree_contains_unbindable(old))
9676f0c6 2432 goto out1;
1da177e4 2433 err = -ELOOP;
fc7be130 2434 for (; mnt_has_parent(p); p = p->mnt_parent)
676da58d 2435 if (p == old)
21444403 2436 goto out1;
1da177e4 2437
84d17192 2438 err = attach_recursive_mnt(old, real_mount(path->mnt), mp, &parent_path);
4ac91378 2439 if (err)
21444403 2440 goto out1;
1da177e4
LT
2441
2442 /* if the mount is moved, it should no longer be expire
2443 * automatically */
6776db3d 2444 list_del_init(&old->mnt_expire);
1da177e4 2445out1:
84d17192 2446 unlock_mount(mp);
1da177e4 2447out:
1da177e4 2448 if (!err)
1a390689 2449 path_put(&parent_path);
2d92ab3c 2450 path_put(&old_path);
1da177e4
LT
2451 return err;
2452}
2453
9d412a43
AV
2454static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
2455{
2456 int err;
2457 const char *subtype = strchr(fstype, '.');
2458 if (subtype) {
2459 subtype++;
2460 err = -EINVAL;
2461 if (!subtype[0])
2462 goto err;
2463 } else
2464 subtype = "";
2465
2466 mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
2467 err = -ENOMEM;
2468 if (!mnt->mnt_sb->s_subtype)
2469 goto err;
2470 return mnt;
2471
2472 err:
2473 mntput(mnt);
2474 return ERR_PTR(err);
2475}
2476
9d412a43
AV
2477/*
2478 * add a mount into a namespace's mount tree
2479 */
95bc5f25 2480static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags)
9d412a43 2481{
84d17192
AV
2482 struct mountpoint *mp;
2483 struct mount *parent;
9d412a43
AV
2484 int err;
2485
f2ebb3a9 2486 mnt_flags &= ~MNT_INTERNAL_FLAGS;
9d412a43 2487
84d17192
AV
2488 mp = lock_mount(path);
2489 if (IS_ERR(mp))
2490 return PTR_ERR(mp);
9d412a43 2491
84d17192 2492 parent = real_mount(path->mnt);
9d412a43 2493 err = -EINVAL;
84d17192 2494 if (unlikely(!check_mnt(parent))) {
156cacb1
AV
2495 /* that's acceptable only for automounts done in private ns */
2496 if (!(mnt_flags & MNT_SHRINKABLE))
2497 goto unlock;
2498 /* ... and for those we'd better have mountpoint still alive */
84d17192 2499 if (!parent->mnt_ns)
156cacb1
AV
2500 goto unlock;
2501 }
9d412a43
AV
2502
2503 /* Refuse the same filesystem on the same mount point */
2504 err = -EBUSY;
95bc5f25 2505 if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb &&
9d412a43
AV
2506 path->mnt->mnt_root == path->dentry)
2507 goto unlock;
2508
2509 err = -EINVAL;
e36cb0b8 2510 if (d_is_symlink(newmnt->mnt.mnt_root))
9d412a43
AV
2511 goto unlock;
2512
95bc5f25 2513 newmnt->mnt.mnt_flags = mnt_flags;
84d17192 2514 err = graft_tree(newmnt, parent, mp);
9d412a43
AV
2515
2516unlock:
84d17192 2517 unlock_mount(mp);
9d412a43
AV
2518 return err;
2519}
b1e75df4 2520
8654df4e 2521static bool mount_too_revealing(struct vfsmount *mnt, int *new_mnt_flags);
1b852bce 2522
1da177e4
LT
2523/*
2524 * create a new mount for userspace and request it to be added into the
2525 * namespace's tree
2526 */
e462ec50 2527static int do_new_mount(struct path *path, const char *fstype, int sb_flags,
808d4e3c 2528 int mnt_flags, const char *name, void *data)
1da177e4 2529{
0c55cfc4 2530 struct file_system_type *type;
1da177e4 2531 struct vfsmount *mnt;
15f9a3f3 2532 int err;
1da177e4 2533
0c55cfc4 2534 if (!fstype)
1da177e4
LT
2535 return -EINVAL;
2536
0c55cfc4
EB
2537 type = get_fs_type(fstype);
2538 if (!type)
2539 return -ENODEV;
2540
e462ec50 2541 mnt = vfs_kern_mount(type, sb_flags, name, data);
0c55cfc4
EB
2542 if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
2543 !mnt->mnt_sb->s_subtype)
2544 mnt = fs_set_subtype(mnt, fstype);
2545
2546 put_filesystem(type);
1da177e4
LT
2547 if (IS_ERR(mnt))
2548 return PTR_ERR(mnt);
2549
8654df4e
EB
2550 if (mount_too_revealing(mnt, &mnt_flags)) {
2551 mntput(mnt);
2552 return -EPERM;
2553 }
2554
95bc5f25 2555 err = do_add_mount(real_mount(mnt), path, mnt_flags);
15f9a3f3
AV
2556 if (err)
2557 mntput(mnt);
2558 return err;
1da177e4
LT
2559}
2560
19a167af
AV
2561int finish_automount(struct vfsmount *m, struct path *path)
2562{
6776db3d 2563 struct mount *mnt = real_mount(m);
19a167af
AV
2564 int err;
2565 /* The new mount record should have at least 2 refs to prevent it being
2566 * expired before we get a chance to add it
2567 */
6776db3d 2568 BUG_ON(mnt_get_count(mnt) < 2);
19a167af
AV
2569
2570 if (m->mnt_sb == path->mnt->mnt_sb &&
2571 m->mnt_root == path->dentry) {
b1e75df4
AV
2572 err = -ELOOP;
2573 goto fail;
19a167af
AV
2574 }
2575
95bc5f25 2576 err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE);
b1e75df4
AV
2577 if (!err)
2578 return 0;
2579fail:
2580 /* remove m from any expiration list it may be on */
6776db3d 2581 if (!list_empty(&mnt->mnt_expire)) {
97216be0 2582 namespace_lock();
6776db3d 2583 list_del_init(&mnt->mnt_expire);
97216be0 2584 namespace_unlock();
19a167af 2585 }
b1e75df4
AV
2586 mntput(m);
2587 mntput(m);
19a167af
AV
2588 return err;
2589}
2590
ea5b778a
DH
2591/**
2592 * mnt_set_expiry - Put a mount on an expiration list
2593 * @mnt: The mount to list.
2594 * @expiry_list: The list to add the mount to.
2595 */
2596void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
2597{
97216be0 2598 namespace_lock();
ea5b778a 2599
6776db3d 2600 list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list);
ea5b778a 2601
97216be0 2602 namespace_unlock();
ea5b778a
DH
2603}
2604EXPORT_SYMBOL(mnt_set_expiry);
2605
1da177e4
LT
2606/*
2607 * process a list of expirable mountpoints with the intent of discarding any
2608 * mountpoints that aren't in use and haven't been touched since last we came
2609 * here
2610 */
2611void mark_mounts_for_expiry(struct list_head *mounts)
2612{
761d5c38 2613 struct mount *mnt, *next;
1da177e4
LT
2614 LIST_HEAD(graveyard);
2615
2616 if (list_empty(mounts))
2617 return;
2618
97216be0 2619 namespace_lock();
719ea2fb 2620 lock_mount_hash();
1da177e4
LT
2621
2622 /* extract from the expiration list every vfsmount that matches the
2623 * following criteria:
2624 * - only referenced by its parent vfsmount
2625 * - still marked for expiry (marked on the last call here; marks are
2626 * cleared by mntput())
2627 */
6776db3d 2628 list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
863d684f 2629 if (!xchg(&mnt->mnt_expiry_mark, 1) ||
1ab59738 2630 propagate_mount_busy(mnt, 1))
1da177e4 2631 continue;
6776db3d 2632 list_move(&mnt->mnt_expire, &graveyard);
1da177e4 2633 }
bcc5c7d2 2634 while (!list_empty(&graveyard)) {
6776db3d 2635 mnt = list_first_entry(&graveyard, struct mount, mnt_expire);
143c8c91 2636 touch_mnt_namespace(mnt->mnt_ns);
e819f152 2637 umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
bcc5c7d2 2638 }
719ea2fb 2639 unlock_mount_hash();
3ab6abee 2640 namespace_unlock();
5528f911
TM
2641}
2642
2643EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
2644
2645/*
2646 * Ripoff of 'select_parent()'
2647 *
2648 * search the list of submounts for a given mountpoint, and move any
2649 * shrinkable submounts to the 'graveyard' list.
2650 */
692afc31 2651static int select_submounts(struct mount *parent, struct list_head *graveyard)
5528f911 2652{
692afc31 2653 struct mount *this_parent = parent;
5528f911
TM
2654 struct list_head *next;
2655 int found = 0;
2656
2657repeat:
6b41d536 2658 next = this_parent->mnt_mounts.next;
5528f911 2659resume:
6b41d536 2660 while (next != &this_parent->mnt_mounts) {
5528f911 2661 struct list_head *tmp = next;
6b41d536 2662 struct mount *mnt = list_entry(tmp, struct mount, mnt_child);
5528f911
TM
2663
2664 next = tmp->next;
692afc31 2665 if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE))
1da177e4 2666 continue;
5528f911
TM
2667 /*
2668 * Descend a level if the d_mounts list is non-empty.
2669 */
6b41d536 2670 if (!list_empty(&mnt->mnt_mounts)) {
5528f911
TM
2671 this_parent = mnt;
2672 goto repeat;
2673 }
1da177e4 2674
1ab59738 2675 if (!propagate_mount_busy(mnt, 1)) {
6776db3d 2676 list_move_tail(&mnt->mnt_expire, graveyard);
5528f911
TM
2677 found++;
2678 }
1da177e4 2679 }
5528f911
TM
2680 /*
2681 * All done at this level ... ascend and resume the search
2682 */
2683 if (this_parent != parent) {
6b41d536 2684 next = this_parent->mnt_child.next;
0714a533 2685 this_parent = this_parent->mnt_parent;
5528f911
TM
2686 goto resume;
2687 }
2688 return found;
2689}
2690
2691/*
2692 * process a list of expirable mountpoints with the intent of discarding any
2693 * submounts of a specific parent mountpoint
99b7db7b 2694 *
48a066e7 2695 * mount_lock must be held for write
5528f911 2696 */
b54b9be7 2697static void shrink_submounts(struct mount *mnt)
5528f911
TM
2698{
2699 LIST_HEAD(graveyard);
761d5c38 2700 struct mount *m;
5528f911 2701
5528f911 2702 /* extract submounts of 'mountpoint' from the expiration list */
c35038be 2703 while (select_submounts(mnt, &graveyard)) {
bcc5c7d2 2704 while (!list_empty(&graveyard)) {
761d5c38 2705 m = list_first_entry(&graveyard, struct mount,
6776db3d 2706 mnt_expire);
143c8c91 2707 touch_mnt_namespace(m->mnt_ns);
e819f152 2708 umount_tree(m, UMOUNT_PROPAGATE|UMOUNT_SYNC);
bcc5c7d2
AV
2709 }
2710 }
1da177e4
LT
2711}
2712
1da177e4
LT
2713/*
2714 * Some copy_from_user() implementations do not return the exact number of
2715 * bytes remaining to copy on a fault. But copy_mount_options() requires that.
2716 * Note that this function differs from copy_from_user() in that it will oops
2717 * on bad values of `to', rather than returning a short copy.
2718 */
b58fed8b
RP
2719static long exact_copy_from_user(void *to, const void __user * from,
2720 unsigned long n)
1da177e4
LT
2721{
2722 char *t = to;
2723 const char __user *f = from;
2724 char c;
2725
96d4f267 2726 if (!access_ok(from, n))
1da177e4
LT
2727 return n;
2728
9da3f2b7 2729 current->kernel_uaccess_faults_ok++;
1da177e4
LT
2730 while (n) {
2731 if (__get_user(c, f)) {
2732 memset(t, 0, n);
2733 break;
2734 }
2735 *t++ = c;
2736 f++;
2737 n--;
2738 }
9da3f2b7 2739 current->kernel_uaccess_faults_ok--;
1da177e4
LT
2740 return n;
2741}
2742
b40ef869 2743void *copy_mount_options(const void __user * data)
1da177e4
LT
2744{
2745 int i;
1da177e4 2746 unsigned long size;
b40ef869 2747 char *copy;
b58fed8b 2748
1da177e4 2749 if (!data)
b40ef869 2750 return NULL;
1da177e4 2751
b40ef869
AV
2752 copy = kmalloc(PAGE_SIZE, GFP_KERNEL);
2753 if (!copy)
2754 return ERR_PTR(-ENOMEM);
1da177e4
LT
2755
2756 /* We only care that *some* data at the address the user
2757 * gave us is valid. Just in case, we'll zero
2758 * the remainder of the page.
2759 */
2760 /* copy_from_user cannot cross TASK_SIZE ! */
2761 size = TASK_SIZE - (unsigned long)data;
2762 if (size > PAGE_SIZE)
2763 size = PAGE_SIZE;
2764
b40ef869 2765 i = size - exact_copy_from_user(copy, data, size);
1da177e4 2766 if (!i) {
b40ef869
AV
2767 kfree(copy);
2768 return ERR_PTR(-EFAULT);
1da177e4
LT
2769 }
2770 if (i != PAGE_SIZE)
b40ef869
AV
2771 memset(copy + i, 0, PAGE_SIZE - i);
2772 return copy;
1da177e4
LT
2773}
2774
b8850d1f 2775char *copy_mount_string(const void __user *data)
eca6f534 2776{
b8850d1f 2777 return data ? strndup_user(data, PAGE_SIZE) : NULL;
eca6f534
VN
2778}
2779
1da177e4
LT
2780/*
2781 * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
2782 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
2783 *
2784 * data is a (void *) that can point to any structure up to
2785 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
2786 * information (or be NULL).
2787 *
2788 * Pre-0.97 versions of mount() didn't have a flags word.
2789 * When the flags word was introduced its top half was required
2790 * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
2791 * Therefore, if this magic number is present, it carries no information
2792 * and must be discarded.
2793 */
5e6123f3 2794long do_mount(const char *dev_name, const char __user *dir_name,
808d4e3c 2795 const char *type_page, unsigned long flags, void *data_page)
1da177e4 2796{
2d92ab3c 2797 struct path path;
e462ec50 2798 unsigned int mnt_flags = 0, sb_flags;
1da177e4 2799 int retval = 0;
1da177e4
LT
2800
2801 /* Discard magic */
2802 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
2803 flags &= ~MS_MGC_MSK;
2804
2805 /* Basic sanity checks */
1da177e4
LT
2806 if (data_page)
2807 ((char *)data_page)[PAGE_SIZE - 1] = 0;
2808
e462ec50
DH
2809 if (flags & MS_NOUSER)
2810 return -EINVAL;
2811
a27ab9f2 2812 /* ... and get the mountpoint */
5e6123f3 2813 retval = user_path(dir_name, &path);
a27ab9f2
TH
2814 if (retval)
2815 return retval;
2816
2817 retval = security_sb_mount(dev_name, &path,
2818 type_page, flags, data_page);
0d5cadb8
AV
2819 if (!retval && !may_mount())
2820 retval = -EPERM;
e462ec50 2821 if (!retval && (flags & SB_MANDLOCK) && !may_mandlock())
9e8925b6 2822 retval = -EPERM;
a27ab9f2
TH
2823 if (retval)
2824 goto dput_out;
2825
613cbe3d
AK
2826 /* Default to relatime unless overriden */
2827 if (!(flags & MS_NOATIME))
2828 mnt_flags |= MNT_RELATIME;
0a1c01c9 2829
1da177e4
LT
2830 /* Separate the per-mountpoint flags */
2831 if (flags & MS_NOSUID)
2832 mnt_flags |= MNT_NOSUID;
2833 if (flags & MS_NODEV)
2834 mnt_flags |= MNT_NODEV;
2835 if (flags & MS_NOEXEC)
2836 mnt_flags |= MNT_NOEXEC;
fc33a7bb
CH
2837 if (flags & MS_NOATIME)
2838 mnt_flags |= MNT_NOATIME;
2839 if (flags & MS_NODIRATIME)
2840 mnt_flags |= MNT_NODIRATIME;
d0adde57
MG
2841 if (flags & MS_STRICTATIME)
2842 mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
a9e5b732 2843 if (flags & MS_RDONLY)
2e4b7fcd 2844 mnt_flags |= MNT_READONLY;
fc33a7bb 2845
ffbc6f0e
EB
2846 /* The default atime for remount is preservation */
2847 if ((flags & MS_REMOUNT) &&
2848 ((flags & (MS_NOATIME | MS_NODIRATIME | MS_RELATIME |
2849 MS_STRICTATIME)) == 0)) {
2850 mnt_flags &= ~MNT_ATIME_MASK;
2851 mnt_flags |= path.mnt->mnt_flags & MNT_ATIME_MASK;
2852 }
2853
e462ec50
DH
2854 sb_flags = flags & (SB_RDONLY |
2855 SB_SYNCHRONOUS |
2856 SB_MANDLOCK |
2857 SB_DIRSYNC |
2858 SB_SILENT |
917086ff 2859 SB_POSIXACL |
d7ee9469 2860 SB_LAZYTIME |
917086ff 2861 SB_I_VERSION);
1da177e4 2862
43f5e655
DH
2863 if ((flags & (MS_REMOUNT | MS_BIND)) == (MS_REMOUNT | MS_BIND))
2864 retval = do_reconfigure_mnt(&path, mnt_flags);
2865 else if (flags & MS_REMOUNT)
e462ec50 2866 retval = do_remount(&path, flags, sb_flags, mnt_flags,
1da177e4
LT
2867 data_page);
2868 else if (flags & MS_BIND)
2d92ab3c 2869 retval = do_loopback(&path, dev_name, flags & MS_REC);
9676f0c6 2870 else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
2d92ab3c 2871 retval = do_change_type(&path, flags);
1da177e4 2872 else if (flags & MS_MOVE)
2d92ab3c 2873 retval = do_move_mount(&path, dev_name);
1da177e4 2874 else
e462ec50 2875 retval = do_new_mount(&path, type_page, sb_flags, mnt_flags,
1da177e4
LT
2876 dev_name, data_page);
2877dput_out:
2d92ab3c 2878 path_put(&path);
1da177e4
LT
2879 return retval;
2880}
2881
537f7ccb
EB
2882static struct ucounts *inc_mnt_namespaces(struct user_namespace *ns)
2883{
2884 return inc_ucount(ns, current_euid(), UCOUNT_MNT_NAMESPACES);
2885}
2886
2887static void dec_mnt_namespaces(struct ucounts *ucounts)
2888{
2889 dec_ucount(ucounts, UCOUNT_MNT_NAMESPACES);
2890}
2891
771b1371
EB
2892static void free_mnt_ns(struct mnt_namespace *ns)
2893{
74e83122
AV
2894 if (!is_anon_ns(ns))
2895 ns_free_inum(&ns->ns);
537f7ccb 2896 dec_mnt_namespaces(ns->ucounts);
771b1371
EB
2897 put_user_ns(ns->user_ns);
2898 kfree(ns);
2899}
2900
8823c079
EB
2901/*
2902 * Assign a sequence number so we can detect when we attempt to bind
2903 * mount a reference to an older mount namespace into the current
2904 * mount namespace, preventing reference counting loops. A 64bit
2905 * number incrementing at 10Ghz will take 12,427 years to wrap which
2906 * is effectively never, so we can ignore the possibility.
2907 */
2908static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1);
2909
74e83122 2910static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool anon)
cf8d2c11
TM
2911{
2912 struct mnt_namespace *new_ns;
537f7ccb 2913 struct ucounts *ucounts;
98f842e6 2914 int ret;
cf8d2c11 2915
537f7ccb
EB
2916 ucounts = inc_mnt_namespaces(user_ns);
2917 if (!ucounts)
df75e774 2918 return ERR_PTR(-ENOSPC);
537f7ccb 2919
74e83122 2920 new_ns = kzalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
537f7ccb
EB
2921 if (!new_ns) {
2922 dec_mnt_namespaces(ucounts);
cf8d2c11 2923 return ERR_PTR(-ENOMEM);
537f7ccb 2924 }
74e83122
AV
2925 if (!anon) {
2926 ret = ns_alloc_inum(&new_ns->ns);
2927 if (ret) {
2928 kfree(new_ns);
2929 dec_mnt_namespaces(ucounts);
2930 return ERR_PTR(ret);
2931 }
98f842e6 2932 }
33c42940 2933 new_ns->ns.ops = &mntns_operations;
74e83122
AV
2934 if (!anon)
2935 new_ns->seq = atomic64_add_return(1, &mnt_ns_seq);
cf8d2c11 2936 atomic_set(&new_ns->count, 1);
cf8d2c11
TM
2937 INIT_LIST_HEAD(&new_ns->list);
2938 init_waitqueue_head(&new_ns->poll);
771b1371 2939 new_ns->user_ns = get_user_ns(user_ns);
537f7ccb 2940 new_ns->ucounts = ucounts;
cf8d2c11
TM
2941 return new_ns;
2942}
2943
0766f788 2944__latent_entropy
9559f689
AV
2945struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
2946 struct user_namespace *user_ns, struct fs_struct *new_fs)
1da177e4 2947{
6b3286ed 2948 struct mnt_namespace *new_ns;
7f2da1e7 2949 struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
315fc83e 2950 struct mount *p, *q;
9559f689 2951 struct mount *old;
cb338d06 2952 struct mount *new;
7a472ef4 2953 int copy_flags;
1da177e4 2954
9559f689
AV
2955 BUG_ON(!ns);
2956
2957 if (likely(!(flags & CLONE_NEWNS))) {
2958 get_mnt_ns(ns);
2959 return ns;
2960 }
2961
2962 old = ns->root;
2963
74e83122 2964 new_ns = alloc_mnt_ns(user_ns, false);
cf8d2c11
TM
2965 if (IS_ERR(new_ns))
2966 return new_ns;
1da177e4 2967
97216be0 2968 namespace_lock();
1da177e4 2969 /* First pass: copy the tree topology */
4ce5d2b1 2970 copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE;
9559f689 2971 if (user_ns != ns->user_ns)
3bd045cc 2972 copy_flags |= CL_SHARED_TO_SLAVE;
7a472ef4 2973 new = copy_tree(old, old->mnt.mnt_root, copy_flags);
be34d1a3 2974 if (IS_ERR(new)) {
328e6d90 2975 namespace_unlock();
771b1371 2976 free_mnt_ns(new_ns);
be34d1a3 2977 return ERR_CAST(new);
1da177e4 2978 }
3bd045cc
AV
2979 if (user_ns != ns->user_ns) {
2980 lock_mount_hash();
2981 lock_mnt_tree(new);
2982 unlock_mount_hash();
2983 }
be08d6d2 2984 new_ns->root = new;
1a4eeaf2 2985 list_add_tail(&new_ns->list, &new->mnt_list);
1da177e4
LT
2986
2987 /*
2988 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
2989 * as belonging to new namespace. We have already acquired a private
2990 * fs_struct, so tsk->fs->lock is not needed.
2991 */
909b0a88 2992 p = old;
cb338d06 2993 q = new;
1da177e4 2994 while (p) {
143c8c91 2995 q->mnt_ns = new_ns;
d2921684 2996 new_ns->mounts++;
9559f689
AV
2997 if (new_fs) {
2998 if (&p->mnt == new_fs->root.mnt) {
2999 new_fs->root.mnt = mntget(&q->mnt);
315fc83e 3000 rootmnt = &p->mnt;
1da177e4 3001 }
9559f689
AV
3002 if (&p->mnt == new_fs->pwd.mnt) {
3003 new_fs->pwd.mnt = mntget(&q->mnt);
315fc83e 3004 pwdmnt = &p->mnt;
1da177e4 3005 }
1da177e4 3006 }
909b0a88
AV
3007 p = next_mnt(p, old);
3008 q = next_mnt(q, new);
4ce5d2b1
EB
3009 if (!q)
3010 break;
3011 while (p->mnt.mnt_root != q->mnt.mnt_root)
3012 p = next_mnt(p, old);
1da177e4 3013 }
328e6d90 3014 namespace_unlock();
1da177e4 3015
1da177e4 3016 if (rootmnt)
f03c6599 3017 mntput(rootmnt);
1da177e4 3018 if (pwdmnt)
f03c6599 3019 mntput(pwdmnt);
1da177e4 3020
741a2951 3021 return new_ns;
1da177e4
LT
3022}
3023
74e83122 3024struct dentry *mount_subtree(struct vfsmount *m, const char *name)
ea441d11 3025{
74e83122 3026 struct mount *mnt = real_mount(m);
ea441d11 3027 struct mnt_namespace *ns;
d31da0f0 3028 struct super_block *s;
ea441d11
AV
3029 struct path path;
3030 int err;
3031
74e83122
AV
3032 ns = alloc_mnt_ns(&init_user_ns, true);
3033 if (IS_ERR(ns)) {
3034 mntput(m);
ea441d11 3035 return ERR_CAST(ns);
74e83122
AV
3036 }
3037 mnt->mnt_ns = ns;
3038 ns->root = mnt;
3039 ns->mounts++;
3040 list_add(&mnt->mnt_list, &ns->list);
ea441d11 3041
74e83122 3042 err = vfs_path_lookup(m->mnt_root, m,
ea441d11
AV
3043 name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
3044
3045 put_mnt_ns(ns);
3046
3047 if (err)
3048 return ERR_PTR(err);
3049
3050 /* trade a vfsmount reference for active sb one */
d31da0f0
AV
3051 s = path.mnt->mnt_sb;
3052 atomic_inc(&s->s_active);
ea441d11
AV
3053 mntput(path.mnt);
3054 /* lock the sucker */
d31da0f0 3055 down_write(&s->s_umount);
ea441d11
AV
3056 /* ... and return the root of (sub)tree on it */
3057 return path.dentry;
3058}
3059EXPORT_SYMBOL(mount_subtree);
3060
312db1aa
DB
3061int ksys_mount(char __user *dev_name, char __user *dir_name, char __user *type,
3062 unsigned long flags, void __user *data)
1da177e4 3063{
eca6f534
VN
3064 int ret;
3065 char *kernel_type;
eca6f534 3066 char *kernel_dev;
b40ef869 3067 void *options;
1da177e4 3068
b8850d1f
TG
3069 kernel_type = copy_mount_string(type);
3070 ret = PTR_ERR(kernel_type);
3071 if (IS_ERR(kernel_type))
eca6f534 3072 goto out_type;
1da177e4 3073
b8850d1f
TG
3074 kernel_dev = copy_mount_string(dev_name);
3075 ret = PTR_ERR(kernel_dev);
3076 if (IS_ERR(kernel_dev))
eca6f534 3077 goto out_dev;
1da177e4 3078
b40ef869
AV
3079 options = copy_mount_options(data);
3080 ret = PTR_ERR(options);
3081 if (IS_ERR(options))
eca6f534 3082 goto out_data;
1da177e4 3083
b40ef869 3084 ret = do_mount(kernel_dev, dir_name, kernel_type, flags, options);
1da177e4 3085
b40ef869 3086 kfree(options);
eca6f534
VN
3087out_data:
3088 kfree(kernel_dev);
3089out_dev:
eca6f534
VN
3090 kfree(kernel_type);
3091out_type:
3092 return ret;
1da177e4
LT
3093}
3094
312db1aa
DB
3095SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
3096 char __user *, type, unsigned long, flags, void __user *, data)
3097{
3098 return ksys_mount(dev_name, dir_name, type, flags, data);
3099}
3100
afac7cba
AV
3101/*
3102 * Return true if path is reachable from root
3103 *
48a066e7 3104 * namespace_sem or mount_lock is held
afac7cba 3105 */
643822b4 3106bool is_path_reachable(struct mount *mnt, struct dentry *dentry,
afac7cba
AV
3107 const struct path *root)
3108{
643822b4 3109 while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) {
a73324da 3110 dentry = mnt->mnt_mountpoint;
0714a533 3111 mnt = mnt->mnt_parent;
afac7cba 3112 }
643822b4 3113 return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry);
afac7cba
AV
3114}
3115
640eb7e7 3116bool path_is_under(const struct path *path1, const struct path *path2)
afac7cba 3117{
25ab4c9b 3118 bool res;
48a066e7 3119 read_seqlock_excl(&mount_lock);
643822b4 3120 res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2);
48a066e7 3121 read_sequnlock_excl(&mount_lock);
afac7cba
AV
3122 return res;
3123}
3124EXPORT_SYMBOL(path_is_under);
3125
1da177e4
LT
3126/*
3127 * pivot_root Semantics:
3128 * Moves the root file system of the current process to the directory put_old,
3129 * makes new_root as the new root file system of the current process, and sets
3130 * root/cwd of all processes which had them on the current root to new_root.
3131 *
3132 * Restrictions:
3133 * The new_root and put_old must be directories, and must not be on the
3134 * same file system as the current process root. The put_old must be
3135 * underneath new_root, i.e. adding a non-zero number of /.. to the string
3136 * pointed to by put_old must yield the same directory as new_root. No other
3137 * file system may be mounted on put_old. After all, new_root is a mountpoint.
3138 *
4a0d11fa
NB
3139 * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
3140 * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
3141 * in this situation.
3142 *
1da177e4
LT
3143 * Notes:
3144 * - we don't move root/cwd if they are not at the root (reason: if something
3145 * cared enough to change them, it's probably wrong to force them elsewhere)
3146 * - it's okay to pick a root that isn't the root of a file system, e.g.
3147 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
3148 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
3149 * first.
3150 */
3480b257
HC
3151SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
3152 const char __user *, put_old)
1da177e4 3153{
2d8f3038 3154 struct path new, old, parent_path, root_parent, root;
84d17192
AV
3155 struct mount *new_mnt, *root_mnt, *old_mnt;
3156 struct mountpoint *old_mp, *root_mp;
1da177e4
LT
3157 int error;
3158
9b40bc90 3159 if (!may_mount())
1da177e4
LT
3160 return -EPERM;
3161
2d8f3038 3162 error = user_path_dir(new_root, &new);
1da177e4
LT
3163 if (error)
3164 goto out0;
1da177e4 3165
2d8f3038 3166 error = user_path_dir(put_old, &old);
1da177e4
LT
3167 if (error)
3168 goto out1;
3169
2d8f3038 3170 error = security_sb_pivotroot(&old, &new);
b12cea91
AV
3171 if (error)
3172 goto out2;
1da177e4 3173
f7ad3c6b 3174 get_fs_root(current->fs, &root);
84d17192
AV
3175 old_mp = lock_mount(&old);
3176 error = PTR_ERR(old_mp);
3177 if (IS_ERR(old_mp))
b12cea91
AV
3178 goto out3;
3179
1da177e4 3180 error = -EINVAL;
419148da
AV
3181 new_mnt = real_mount(new.mnt);
3182 root_mnt = real_mount(root.mnt);
84d17192
AV
3183 old_mnt = real_mount(old.mnt);
3184 if (IS_MNT_SHARED(old_mnt) ||
fc7be130
AV
3185 IS_MNT_SHARED(new_mnt->mnt_parent) ||
3186 IS_MNT_SHARED(root_mnt->mnt_parent))
b12cea91 3187 goto out4;
143c8c91 3188 if (!check_mnt(root_mnt) || !check_mnt(new_mnt))
b12cea91 3189 goto out4;
5ff9d8a6
EB
3190 if (new_mnt->mnt.mnt_flags & MNT_LOCKED)
3191 goto out4;
1da177e4 3192 error = -ENOENT;
f3da392e 3193 if (d_unlinked(new.dentry))
b12cea91 3194 goto out4;
1da177e4 3195 error = -EBUSY;
84d17192 3196 if (new_mnt == root_mnt || old_mnt == root_mnt)
b12cea91 3197 goto out4; /* loop, on the same file system */
1da177e4 3198 error = -EINVAL;
8c3ee42e 3199 if (root.mnt->mnt_root != root.dentry)
b12cea91 3200 goto out4; /* not a mountpoint */
676da58d 3201 if (!mnt_has_parent(root_mnt))
b12cea91 3202 goto out4; /* not attached */
84d17192 3203 root_mp = root_mnt->mnt_mp;
2d8f3038 3204 if (new.mnt->mnt_root != new.dentry)
b12cea91 3205 goto out4; /* not a mountpoint */
676da58d 3206 if (!mnt_has_parent(new_mnt))
b12cea91 3207 goto out4; /* not attached */
4ac91378 3208 /* make sure we can reach put_old from new_root */
84d17192 3209 if (!is_path_reachable(old_mnt, old.dentry, &new))
b12cea91 3210 goto out4;
0d082601
EB
3211 /* make certain new is below the root */
3212 if (!is_path_reachable(new_mnt, new.dentry, &root))
3213 goto out4;
84d17192 3214 root_mp->m_count++; /* pin it so it won't go away */
719ea2fb 3215 lock_mount_hash();
419148da
AV
3216 detach_mnt(new_mnt, &parent_path);
3217 detach_mnt(root_mnt, &root_parent);
5ff9d8a6
EB
3218 if (root_mnt->mnt.mnt_flags & MNT_LOCKED) {
3219 new_mnt->mnt.mnt_flags |= MNT_LOCKED;
3220 root_mnt->mnt.mnt_flags &= ~MNT_LOCKED;
3221 }
4ac91378 3222 /* mount old root on put_old */
84d17192 3223 attach_mnt(root_mnt, old_mnt, old_mp);
4ac91378 3224 /* mount new_root on / */
84d17192 3225 attach_mnt(new_mnt, real_mount(root_parent.mnt), root_mp);
6b3286ed 3226 touch_mnt_namespace(current->nsproxy->mnt_ns);
4fed655c
EB
3227 /* A moved mount should not expire automatically */
3228 list_del_init(&new_mnt->mnt_expire);
3895dbf8 3229 put_mountpoint(root_mp);
719ea2fb 3230 unlock_mount_hash();
2d8f3038 3231 chroot_fs_refs(&root, &new);
1da177e4 3232 error = 0;
b12cea91 3233out4:
84d17192 3234 unlock_mount(old_mp);
b12cea91
AV
3235 if (!error) {
3236 path_put(&root_parent);
3237 path_put(&parent_path);
3238 }
3239out3:
8c3ee42e 3240 path_put(&root);
b12cea91 3241out2:
2d8f3038 3242 path_put(&old);
1da177e4 3243out1:
2d8f3038 3244 path_put(&new);
1da177e4 3245out0:
1da177e4 3246 return error;
1da177e4
LT
3247}
3248
3249static void __init init_mount_tree(void)
3250{
3251 struct vfsmount *mnt;
74e83122 3252 struct mount *m;
6b3286ed 3253 struct mnt_namespace *ns;
ac748a09 3254 struct path root;
0c55cfc4 3255 struct file_system_type *type;
1da177e4 3256
0c55cfc4
EB
3257 type = get_fs_type("rootfs");
3258 if (!type)
3259 panic("Can't find rootfs type");
3260 mnt = vfs_kern_mount(type, 0, "rootfs", NULL);
3261 put_filesystem(type);
1da177e4
LT
3262 if (IS_ERR(mnt))
3263 panic("Can't create rootfs");
b3e19d92 3264
74e83122 3265 ns = alloc_mnt_ns(&init_user_ns, false);
3b22edc5 3266 if (IS_ERR(ns))
1da177e4 3267 panic("Can't allocate initial namespace");
74e83122
AV
3268 m = real_mount(mnt);
3269 m->mnt_ns = ns;
3270 ns->root = m;
3271 ns->mounts = 1;
3272 list_add(&m->mnt_list, &ns->list);
6b3286ed
KK
3273 init_task.nsproxy->mnt_ns = ns;
3274 get_mnt_ns(ns);
3275
be08d6d2
AV
3276 root.mnt = mnt;
3277 root.dentry = mnt->mnt_root;
da362b09 3278 mnt->mnt_flags |= MNT_LOCKED;
ac748a09
JB
3279
3280 set_fs_pwd(current->fs, &root);
3281 set_fs_root(current->fs, &root);
1da177e4
LT
3282}
3283
74bf17cf 3284void __init mnt_init(void)
1da177e4 3285{
15a67dd8 3286 int err;
1da177e4 3287
7d6fec45 3288 mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
20c2df83 3289 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
1da177e4 3290
0818bf27 3291 mount_hashtable = alloc_large_system_hash("Mount-cache",
38129a13 3292 sizeof(struct hlist_head),
0818bf27 3293 mhash_entries, 19,
3d375d78 3294 HASH_ZERO,
0818bf27
AV
3295 &m_hash_shift, &m_hash_mask, 0, 0);
3296 mountpoint_hashtable = alloc_large_system_hash("Mountpoint-cache",
3297 sizeof(struct hlist_head),
3298 mphash_entries, 19,
3d375d78 3299 HASH_ZERO,
0818bf27 3300 &mp_hash_shift, &mp_hash_mask, 0, 0);
1da177e4 3301
84d17192 3302 if (!mount_hashtable || !mountpoint_hashtable)
1da177e4
LT
3303 panic("Failed to allocate mount hash table\n");
3304
4b93dc9b
TH
3305 kernfs_init();
3306
15a67dd8
RD
3307 err = sysfs_init();
3308 if (err)
3309 printk(KERN_WARNING "%s: sysfs_init error: %d\n",
8e24eea7 3310 __func__, err);
00d26666
GKH
3311 fs_kobj = kobject_create_and_add("fs", NULL);
3312 if (!fs_kobj)
8e24eea7 3313 printk(KERN_WARNING "%s: kobj create error\n", __func__);
1da177e4
LT
3314 init_rootfs();
3315 init_mount_tree();
3316}
3317
616511d0 3318void put_mnt_ns(struct mnt_namespace *ns)
1da177e4 3319{
d498b25a 3320 if (!atomic_dec_and_test(&ns->count))
616511d0 3321 return;
7b00ed6f 3322 drop_collected_mounts(&ns->root->mnt);
771b1371 3323 free_mnt_ns(ns);
1da177e4 3324}
9d412a43
AV
3325
3326struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
3327{
423e0ab0 3328 struct vfsmount *mnt;
e462ec50 3329 mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, data);
423e0ab0
TC
3330 if (!IS_ERR(mnt)) {
3331 /*
3332 * it is a longterm mount, don't release mnt until
3333 * we unmount before file sys is unregistered
3334 */
f7a99c5b 3335 real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL;
423e0ab0
TC
3336 }
3337 return mnt;
9d412a43
AV
3338}
3339EXPORT_SYMBOL_GPL(kern_mount_data);
423e0ab0
TC
3340
3341void kern_unmount(struct vfsmount *mnt)
3342{
3343 /* release long term mount so mount point can be released */
3344 if (!IS_ERR_OR_NULL(mnt)) {
f7a99c5b 3345 real_mount(mnt)->mnt_ns = NULL;
48a066e7 3346 synchronize_rcu(); /* yecchhh... */
423e0ab0
TC
3347 mntput(mnt);
3348 }
3349}
3350EXPORT_SYMBOL(kern_unmount);
02125a82
AV
3351
3352bool our_mnt(struct vfsmount *mnt)
3353{
143c8c91 3354 return check_mnt(real_mount(mnt));
02125a82 3355}
8823c079 3356
3151527e
EB
3357bool current_chrooted(void)
3358{
3359 /* Does the current process have a non-standard root */
3360 struct path ns_root;
3361 struct path fs_root;
3362 bool chrooted;
3363
3364 /* Find the namespace root */
3365 ns_root.mnt = &current->nsproxy->mnt_ns->root->mnt;
3366 ns_root.dentry = ns_root.mnt->mnt_root;
3367 path_get(&ns_root);
3368 while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root))
3369 ;
3370
3371 get_fs_root(current->fs, &fs_root);
3372
3373 chrooted = !path_equal(&fs_root, &ns_root);
3374
3375 path_put(&fs_root);
3376 path_put(&ns_root);
3377
3378 return chrooted;
3379}
3380
8654df4e
EB
3381static bool mnt_already_visible(struct mnt_namespace *ns, struct vfsmount *new,
3382 int *new_mnt_flags)
87a8ebd6 3383{
8c6cf9cc 3384 int new_flags = *new_mnt_flags;
87a8ebd6 3385 struct mount *mnt;
e51db735 3386 bool visible = false;
87a8ebd6 3387
44bb4385 3388 down_read(&namespace_sem);
87a8ebd6 3389 list_for_each_entry(mnt, &ns->list, mnt_list) {
e51db735 3390 struct mount *child;
77b1a97d
EB
3391 int mnt_flags;
3392
8654df4e 3393 if (mnt->mnt.mnt_sb->s_type != new->mnt_sb->s_type)
e51db735
EB
3394 continue;
3395
7e96c1b0
EB
3396 /* This mount is not fully visible if it's root directory
3397 * is not the root directory of the filesystem.
3398 */
3399 if (mnt->mnt.mnt_root != mnt->mnt.mnt_sb->s_root)
3400 continue;
3401
a1935c17 3402 /* A local view of the mount flags */
77b1a97d 3403 mnt_flags = mnt->mnt.mnt_flags;
77b1a97d 3404
695e9df0 3405 /* Don't miss readonly hidden in the superblock flags */
bc98a42c 3406 if (sb_rdonly(mnt->mnt.mnt_sb))
695e9df0
EB
3407 mnt_flags |= MNT_LOCK_READONLY;
3408
8c6cf9cc
EB
3409 /* Verify the mount flags are equal to or more permissive
3410 * than the proposed new mount.
3411 */
77b1a97d 3412 if ((mnt_flags & MNT_LOCK_READONLY) &&
8c6cf9cc
EB
3413 !(new_flags & MNT_READONLY))
3414 continue;
77b1a97d
EB
3415 if ((mnt_flags & MNT_LOCK_ATIME) &&
3416 ((mnt_flags & MNT_ATIME_MASK) != (new_flags & MNT_ATIME_MASK)))
8c6cf9cc
EB
3417 continue;
3418
ceeb0e5d
EB
3419 /* This mount is not fully visible if there are any
3420 * locked child mounts that cover anything except for
3421 * empty directories.
e51db735
EB
3422 */
3423 list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
3424 struct inode *inode = child->mnt_mountpoint->d_inode;
ceeb0e5d 3425 /* Only worry about locked mounts */
d71ed6c9 3426 if (!(child->mnt.mnt_flags & MNT_LOCKED))
ceeb0e5d 3427 continue;
7236c85e
EB
3428 /* Is the directory permanetly empty? */
3429 if (!is_empty_dir_inode(inode))
e51db735 3430 goto next;
87a8ebd6 3431 }
8c6cf9cc 3432 /* Preserve the locked attributes */
77b1a97d 3433 *new_mnt_flags |= mnt_flags & (MNT_LOCK_READONLY | \
77b1a97d 3434 MNT_LOCK_ATIME);
e51db735
EB
3435 visible = true;
3436 goto found;
3437 next: ;
87a8ebd6 3438 }
e51db735 3439found:
44bb4385 3440 up_read(&namespace_sem);
e51db735 3441 return visible;
87a8ebd6
EB
3442}
3443
8654df4e
EB
3444static bool mount_too_revealing(struct vfsmount *mnt, int *new_mnt_flags)
3445{
a1935c17 3446 const unsigned long required_iflags = SB_I_NOEXEC | SB_I_NODEV;
8654df4e
EB
3447 struct mnt_namespace *ns = current->nsproxy->mnt_ns;
3448 unsigned long s_iflags;
3449
3450 if (ns->user_ns == &init_user_ns)
3451 return false;
3452
3453 /* Can this filesystem be too revealing? */
3454 s_iflags = mnt->mnt_sb->s_iflags;
3455 if (!(s_iflags & SB_I_USERNS_VISIBLE))
3456 return false;
3457
a1935c17
EB
3458 if ((s_iflags & required_iflags) != required_iflags) {
3459 WARN_ONCE(1, "Expected s_iflags to contain 0x%lx\n",
3460 required_iflags);
3461 return true;
3462 }
3463
8654df4e
EB
3464 return !mnt_already_visible(ns, mnt, new_mnt_flags);
3465}
3466
380cf5ba
AL
3467bool mnt_may_suid(struct vfsmount *mnt)
3468{
3469 /*
3470 * Foreign mounts (accessed via fchdir or through /proc
3471 * symlinks) are always treated as if they are nosuid. This
3472 * prevents namespaces from trusting potentially unsafe
3473 * suid/sgid bits, file caps, or security labels that originate
3474 * in other namespaces.
3475 */
3476 return !(mnt->mnt_flags & MNT_NOSUID) && check_mnt(real_mount(mnt)) &&
3477 current_in_userns(mnt->mnt_sb->s_user_ns);
3478}
3479
64964528 3480static struct ns_common *mntns_get(struct task_struct *task)
8823c079 3481{
58be2825 3482 struct ns_common *ns = NULL;
8823c079
EB
3483 struct nsproxy *nsproxy;
3484
728dba3a
EB
3485 task_lock(task);
3486 nsproxy = task->nsproxy;
8823c079 3487 if (nsproxy) {
58be2825
AV
3488 ns = &nsproxy->mnt_ns->ns;
3489 get_mnt_ns(to_mnt_ns(ns));
8823c079 3490 }
728dba3a 3491 task_unlock(task);
8823c079
EB
3492
3493 return ns;
3494}
3495
64964528 3496static void mntns_put(struct ns_common *ns)
8823c079 3497{
58be2825 3498 put_mnt_ns(to_mnt_ns(ns));
8823c079
EB
3499}
3500
64964528 3501static int mntns_install(struct nsproxy *nsproxy, struct ns_common *ns)
8823c079
EB
3502{
3503 struct fs_struct *fs = current->fs;
4f757f3c 3504 struct mnt_namespace *mnt_ns = to_mnt_ns(ns), *old_mnt_ns;
8823c079 3505 struct path root;
4f757f3c 3506 int err;
8823c079 3507
0c55cfc4 3508 if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) ||
c7b96acf
EB
3509 !ns_capable(current_user_ns(), CAP_SYS_CHROOT) ||
3510 !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
ae11e0f1 3511 return -EPERM;
8823c079 3512
74e83122
AV
3513 if (is_anon_ns(mnt_ns))
3514 return -EINVAL;
3515
8823c079
EB
3516 if (fs->users != 1)
3517 return -EINVAL;
3518
3519 get_mnt_ns(mnt_ns);
4f757f3c 3520 old_mnt_ns = nsproxy->mnt_ns;
8823c079
EB
3521 nsproxy->mnt_ns = mnt_ns;
3522
3523 /* Find the root */
4f757f3c
AV
3524 err = vfs_path_lookup(mnt_ns->root->mnt.mnt_root, &mnt_ns->root->mnt,
3525 "/", LOOKUP_DOWN, &root);
3526 if (err) {
3527 /* revert to old namespace */
3528 nsproxy->mnt_ns = old_mnt_ns;
3529 put_mnt_ns(mnt_ns);
3530 return err;
3531 }
8823c079 3532
4068367c
AV
3533 put_mnt_ns(old_mnt_ns);
3534
8823c079
EB
3535 /* Update the pwd and root */
3536 set_fs_pwd(fs, &root);
3537 set_fs_root(fs, &root);
3538
3539 path_put(&root);
3540 return 0;
3541}
3542
bcac25a5
AV
3543static struct user_namespace *mntns_owner(struct ns_common *ns)
3544{
3545 return to_mnt_ns(ns)->user_ns;
3546}
3547
8823c079
EB
3548const struct proc_ns_operations mntns_operations = {
3549 .name = "mnt",
3550 .type = CLONE_NEWNS,
3551 .get = mntns_get,
3552 .put = mntns_put,
3553 .install = mntns_install,
bcac25a5 3554 .owner = mntns_owner,
8823c079 3555};