]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - fs/namespace.c
dpaa2-switch: Fix memory leak in dpaa2_switch_acl_entry_add() and dpaa2_switch_acl_en...
[mirror_ubuntu-jammy-kernel.git] / fs / namespace.c
CommitLineData
59bd9ded 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4
LT
2/*
3 * linux/fs/namespace.c
4 *
5 * (C) Copyright Al Viro 2000, 2001
1da177e4
LT
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 */
a07b2000 23#include <linux/file.h>
d10577a8 24#include <linux/uaccess.h>
0bb80f24 25#include <linux/proc_ns.h>
20b4fb48 26#include <linux/magic.h>
57c8a661 27#include <linux/memblock.h>
9caccd41 28#include <linux/proc_fs.h>
9ea459e1 29#include <linux/task_work.h>
9164bb4a 30#include <linux/sched/task.h>
e262e32d 31#include <uapi/linux/mount.h>
9bc61ab1 32#include <linux/fs_context.h>
037f11b4 33#include <linux/shmem_fs.h>
911a39c1 34#include <linux/mnt_idmapping.h>
9164bb4a 35
07b20889 36#include "pnode.h"
948730b0 37#include "internal.h"
1da177e4 38
d2921684
EB
39/* Maximum number of mounts in a mount namespace */
40unsigned int sysctl_mount_max __read_mostly = 100000;
41
0818bf27
AV
42static unsigned int m_hash_mask __read_mostly;
43static unsigned int m_hash_shift __read_mostly;
44static unsigned int mp_hash_mask __read_mostly;
45static unsigned int mp_hash_shift __read_mostly;
46
47static __initdata unsigned long mhash_entries;
48static int __init set_mhash_entries(char *str)
49{
50 if (!str)
51 return 0;
52 mhash_entries = simple_strtoul(str, &str, 0);
53 return 1;
54}
55__setup("mhash_entries=", set_mhash_entries);
56
57static __initdata unsigned long mphash_entries;
58static int __init set_mphash_entries(char *str)
59{
60 if (!str)
61 return 0;
62 mphash_entries = simple_strtoul(str, &str, 0);
63 return 1;
64}
65__setup("mphash_entries=", set_mphash_entries);
13f14b4d 66
c7999c36 67static u64 event;
73cd49ec 68static DEFINE_IDA(mnt_id_ida);
719f5d7f 69static DEFINE_IDA(mnt_group_ida);
1da177e4 70
38129a13 71static struct hlist_head *mount_hashtable __read_mostly;
0818bf27 72static struct hlist_head *mountpoint_hashtable __read_mostly;
e18b890b 73static struct kmem_cache *mnt_cache __read_mostly;
59aa0da8 74static DECLARE_RWSEM(namespace_sem);
4edbe133
AV
75static HLIST_HEAD(unmounted); /* protected by namespace_sem */
76static LIST_HEAD(ex_mountpoints); /* protected by namespace_sem */
1da177e4 77
2a186721
CB
78struct mount_kattr {
79 unsigned int attr_set;
80 unsigned int attr_clr;
81 unsigned int propagation;
82 unsigned int lookup_flags;
83 bool recurse;
9caccd41 84 struct user_namespace *mnt_userns;
2a186721
CB
85};
86
f87fd4c2 87/* /sys/fs */
00d26666
GKH
88struct kobject *fs_kobj;
89EXPORT_SYMBOL_GPL(fs_kobj);
f87fd4c2 90
99b7db7b
NP
91/*
92 * vfsmount lock may be taken for read to prevent changes to the
93 * vfsmount hash, ie. during mountpoint lookups or walking back
94 * up the tree.
95 *
96 * It should be taken for write in all cases where the vfsmount
97 * tree or hash is modified or when a vfsmount structure is modified.
98 */
48a066e7 99__cacheline_aligned_in_smp DEFINE_SEQLOCK(mount_lock);
99b7db7b 100
d033cb67
CB
101static inline void lock_mount_hash(void)
102{
103 write_seqlock(&mount_lock);
104}
105
106static inline void unlock_mount_hash(void)
107{
108 write_sequnlock(&mount_lock);
109}
110
38129a13 111static inline struct hlist_head *m_hash(struct vfsmount *mnt, struct dentry *dentry)
1da177e4 112{
b58fed8b
RP
113 unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
114 tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
0818bf27
AV
115 tmp = tmp + (tmp >> m_hash_shift);
116 return &mount_hashtable[tmp & m_hash_mask];
117}
118
119static inline struct hlist_head *mp_hash(struct dentry *dentry)
120{
121 unsigned long tmp = ((unsigned long)dentry / L1_CACHE_BYTES);
122 tmp = tmp + (tmp >> mp_hash_shift);
123 return &mountpoint_hashtable[tmp & mp_hash_mask];
1da177e4
LT
124}
125
b105e270 126static int mnt_alloc_id(struct mount *mnt)
73cd49ec 127{
169b480e
MW
128 int res = ida_alloc(&mnt_id_ida, GFP_KERNEL);
129
130 if (res < 0)
131 return res;
132 mnt->mnt_id = res;
133 return 0;
73cd49ec
MS
134}
135
b105e270 136static void mnt_free_id(struct mount *mnt)
73cd49ec 137{
169b480e 138 ida_free(&mnt_id_ida, mnt->mnt_id);
73cd49ec
MS
139}
140
719f5d7f
MS
141/*
142 * Allocate a new peer group ID
719f5d7f 143 */
4b8b21f4 144static int mnt_alloc_group_id(struct mount *mnt)
719f5d7f 145{
169b480e 146 int res = ida_alloc_min(&mnt_group_ida, 1, GFP_KERNEL);
f21f6220 147
169b480e
MW
148 if (res < 0)
149 return res;
150 mnt->mnt_group_id = res;
151 return 0;
719f5d7f
MS
152}
153
154/*
155 * Release a peer group ID
156 */
4b8b21f4 157void mnt_release_group_id(struct mount *mnt)
719f5d7f 158{
169b480e 159 ida_free(&mnt_group_ida, mnt->mnt_group_id);
15169fe7 160 mnt->mnt_group_id = 0;
719f5d7f
MS
161}
162
b3e19d92
NP
163/*
164 * vfsmount lock must be held for read
165 */
83adc753 166static inline void mnt_add_count(struct mount *mnt, int n)
b3e19d92
NP
167{
168#ifdef CONFIG_SMP
68e8a9fe 169 this_cpu_add(mnt->mnt_pcp->mnt_count, n);
b3e19d92
NP
170#else
171 preempt_disable();
68e8a9fe 172 mnt->mnt_count += n;
b3e19d92
NP
173 preempt_enable();
174#endif
175}
176
b3e19d92
NP
177/*
178 * vfsmount lock must be held for write
179 */
edf7ddbf 180int mnt_get_count(struct mount *mnt)
b3e19d92
NP
181{
182#ifdef CONFIG_SMP
edf7ddbf 183 int count = 0;
b3e19d92
NP
184 int cpu;
185
186 for_each_possible_cpu(cpu) {
68e8a9fe 187 count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
b3e19d92
NP
188 }
189
190 return count;
191#else
68e8a9fe 192 return mnt->mnt_count;
b3e19d92
NP
193#endif
194}
195
b105e270 196static struct mount *alloc_vfsmnt(const char *name)
1da177e4 197{
c63181e6
AV
198 struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
199 if (mnt) {
73cd49ec
MS
200 int err;
201
c63181e6 202 err = mnt_alloc_id(mnt);
88b38782
LZ
203 if (err)
204 goto out_free_cache;
205
206 if (name) {
79f6540b
VA
207 mnt->mnt_devname = kstrdup_const(name,
208 GFP_KERNEL_ACCOUNT);
c63181e6 209 if (!mnt->mnt_devname)
88b38782 210 goto out_free_id;
73cd49ec
MS
211 }
212
b3e19d92 213#ifdef CONFIG_SMP
c63181e6
AV
214 mnt->mnt_pcp = alloc_percpu(struct mnt_pcp);
215 if (!mnt->mnt_pcp)
b3e19d92
NP
216 goto out_free_devname;
217
c63181e6 218 this_cpu_add(mnt->mnt_pcp->mnt_count, 1);
b3e19d92 219#else
c63181e6
AV
220 mnt->mnt_count = 1;
221 mnt->mnt_writers = 0;
b3e19d92
NP
222#endif
223
38129a13 224 INIT_HLIST_NODE(&mnt->mnt_hash);
c63181e6
AV
225 INIT_LIST_HEAD(&mnt->mnt_child);
226 INIT_LIST_HEAD(&mnt->mnt_mounts);
227 INIT_LIST_HEAD(&mnt->mnt_list);
228 INIT_LIST_HEAD(&mnt->mnt_expire);
229 INIT_LIST_HEAD(&mnt->mnt_share);
230 INIT_LIST_HEAD(&mnt->mnt_slave_list);
231 INIT_LIST_HEAD(&mnt->mnt_slave);
0a5eb7c8 232 INIT_HLIST_NODE(&mnt->mnt_mp_list);
99b19d16 233 INIT_LIST_HEAD(&mnt->mnt_umounting);
56cbb429 234 INIT_HLIST_HEAD(&mnt->mnt_stuck_children);
a6435940 235 mnt->mnt.mnt_userns = &init_user_ns;
1da177e4 236 }
c63181e6 237 return mnt;
88b38782 238
d3ef3d73
NP
239#ifdef CONFIG_SMP
240out_free_devname:
fcc139ae 241 kfree_const(mnt->mnt_devname);
d3ef3d73 242#endif
88b38782 243out_free_id:
c63181e6 244 mnt_free_id(mnt);
88b38782 245out_free_cache:
c63181e6 246 kmem_cache_free(mnt_cache, mnt);
88b38782 247 return NULL;
1da177e4
LT
248}
249
3d733633
DH
250/*
251 * Most r/o checks on a fs are for operations that take
252 * discrete amounts of time, like a write() or unlink().
253 * We must keep track of when those operations start
254 * (for permission checks) and when they end, so that
255 * we can determine when writes are able to occur to
256 * a filesystem.
257 */
258/*
259 * __mnt_is_readonly: check whether a mount is read-only
260 * @mnt: the mount to check for its write status
261 *
262 * This shouldn't be used directly ouside of the VFS.
263 * It does not guarantee that the filesystem will stay
264 * r/w, just that it is right *now*. This can not and
265 * should not be used in place of IS_RDONLY(inode).
266 * mnt_want/drop_write() will _keep_ the filesystem
267 * r/w.
268 */
43f5e655 269bool __mnt_is_readonly(struct vfsmount *mnt)
3d733633 270{
43f5e655 271 return (mnt->mnt_flags & MNT_READONLY) || sb_rdonly(mnt->mnt_sb);
3d733633
DH
272}
273EXPORT_SYMBOL_GPL(__mnt_is_readonly);
274
83adc753 275static inline void mnt_inc_writers(struct mount *mnt)
d3ef3d73
NP
276{
277#ifdef CONFIG_SMP
68e8a9fe 278 this_cpu_inc(mnt->mnt_pcp->mnt_writers);
d3ef3d73 279#else
68e8a9fe 280 mnt->mnt_writers++;
d3ef3d73
NP
281#endif
282}
3d733633 283
83adc753 284static inline void mnt_dec_writers(struct mount *mnt)
3d733633 285{
d3ef3d73 286#ifdef CONFIG_SMP
68e8a9fe 287 this_cpu_dec(mnt->mnt_pcp->mnt_writers);
d3ef3d73 288#else
68e8a9fe 289 mnt->mnt_writers--;
d3ef3d73 290#endif
3d733633 291}
3d733633 292
83adc753 293static unsigned int mnt_get_writers(struct mount *mnt)
3d733633 294{
d3ef3d73
NP
295#ifdef CONFIG_SMP
296 unsigned int count = 0;
3d733633 297 int cpu;
3d733633
DH
298
299 for_each_possible_cpu(cpu) {
68e8a9fe 300 count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers;
3d733633 301 }
3d733633 302
d3ef3d73
NP
303 return count;
304#else
305 return mnt->mnt_writers;
306#endif
3d733633
DH
307}
308
4ed5e82f
MS
309static int mnt_is_readonly(struct vfsmount *mnt)
310{
311 if (mnt->mnt_sb->s_readonly_remount)
312 return 1;
313 /* Order wrt setting s_flags/s_readonly_remount in do_remount() */
314 smp_rmb();
315 return __mnt_is_readonly(mnt);
316}
317
8366025e 318/*
eb04c282
JK
319 * Most r/o & frozen checks on a fs are for operations that take discrete
320 * amounts of time, like a write() or unlink(). We must keep track of when
321 * those operations start (for permission checks) and when they end, so that we
322 * can determine when writes are able to occur to a filesystem.
8366025e
DH
323 */
324/**
eb04c282 325 * __mnt_want_write - get write access to a mount without freeze protection
83adc753 326 * @m: the mount on which to take a write
8366025e 327 *
eb04c282
JK
328 * This tells the low-level filesystem that a write is about to be performed to
329 * it, and makes sure that writes are allowed (mnt it read-write) before
330 * returning success. This operation does not protect against filesystem being
331 * frozen. When the write operation is finished, __mnt_drop_write() must be
332 * called. This is effectively a refcount.
8366025e 333 */
eb04c282 334int __mnt_want_write(struct vfsmount *m)
8366025e 335{
83adc753 336 struct mount *mnt = real_mount(m);
3d733633 337 int ret = 0;
3d733633 338
d3ef3d73 339 preempt_disable();
c6653a83 340 mnt_inc_writers(mnt);
d3ef3d73 341 /*
c6653a83 342 * The store to mnt_inc_writers must be visible before we pass
d3ef3d73
NP
343 * MNT_WRITE_HOLD loop below, so that the slowpath can see our
344 * incremented count after it has set MNT_WRITE_HOLD.
345 */
346 smp_mb();
6aa7de05 347 while (READ_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD)
d3ef3d73
NP
348 cpu_relax();
349 /*
350 * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
351 * be set to match its requirements. So we must not load that until
352 * MNT_WRITE_HOLD is cleared.
353 */
354 smp_rmb();
4ed5e82f 355 if (mnt_is_readonly(m)) {
c6653a83 356 mnt_dec_writers(mnt);
3d733633 357 ret = -EROFS;
3d733633 358 }
d3ef3d73 359 preempt_enable();
eb04c282
JK
360
361 return ret;
362}
363
364/**
365 * mnt_want_write - get write access to a mount
366 * @m: the mount on which to take a write
367 *
368 * This tells the low-level filesystem that a write is about to be performed to
369 * it, and makes sure that writes are allowed (mount is read-write, filesystem
370 * is not frozen) before returning success. When the write operation is
371 * finished, mnt_drop_write() must be called. This is effectively a refcount.
372 */
373int mnt_want_write(struct vfsmount *m)
374{
375 int ret;
376
377 sb_start_write(m->mnt_sb);
378 ret = __mnt_want_write(m);
379 if (ret)
380 sb_end_write(m->mnt_sb);
3d733633 381 return ret;
8366025e
DH
382}
383EXPORT_SYMBOL_GPL(mnt_want_write);
384
96029c4e 385/**
eb04c282 386 * __mnt_want_write_file - get write access to a file's mount
96029c4e
NP
387 * @file: the file who's mount on which to take a write
388 *
14e43bf4
EB
389 * This is like __mnt_want_write, but if the file is already open for writing it
390 * skips incrementing mnt_writers (since the open file already has a reference)
391 * and instead only does the check for emergency r/o remounts. This must be
392 * paired with __mnt_drop_write_file.
96029c4e 393 */
eb04c282 394int __mnt_want_write_file(struct file *file)
96029c4e 395{
14e43bf4
EB
396 if (file->f_mode & FMODE_WRITER) {
397 /*
398 * Superblock may have become readonly while there are still
399 * writable fd's, e.g. due to a fs error with errors=remount-ro
400 */
401 if (__mnt_is_readonly(file->f_path.mnt))
402 return -EROFS;
403 return 0;
404 }
405 return __mnt_want_write(file->f_path.mnt);
96029c4e 406}
eb04c282 407
7c6893e3
MS
408/**
409 * mnt_want_write_file - get write access to a file's mount
410 * @file: the file who's mount on which to take a write
411 *
14e43bf4
EB
412 * This is like mnt_want_write, but if the file is already open for writing it
413 * skips incrementing mnt_writers (since the open file already has a reference)
414 * and instead only does the freeze protection and the check for emergency r/o
415 * remounts. This must be paired with mnt_drop_write_file.
7c6893e3
MS
416 */
417int mnt_want_write_file(struct file *file)
418{
419 int ret;
420
a6795a58 421 sb_start_write(file_inode(file)->i_sb);
eb04c282
JK
422 ret = __mnt_want_write_file(file);
423 if (ret)
a6795a58 424 sb_end_write(file_inode(file)->i_sb);
7c6893e3
MS
425 return ret;
426}
96029c4e
NP
427EXPORT_SYMBOL_GPL(mnt_want_write_file);
428
8366025e 429/**
eb04c282 430 * __mnt_drop_write - give up write access to a mount
8366025e
DH
431 * @mnt: the mount on which to give up write access
432 *
433 * Tells the low-level filesystem that we are done
434 * performing writes to it. Must be matched with
eb04c282 435 * __mnt_want_write() call above.
8366025e 436 */
eb04c282 437void __mnt_drop_write(struct vfsmount *mnt)
8366025e 438{
d3ef3d73 439 preempt_disable();
83adc753 440 mnt_dec_writers(real_mount(mnt));
d3ef3d73 441 preempt_enable();
8366025e 442}
7fea21ae 443EXPORT_SYMBOL_GPL(__mnt_drop_write);
eb04c282
JK
444
445/**
446 * mnt_drop_write - give up write access to a mount
447 * @mnt: the mount on which to give up write access
448 *
449 * Tells the low-level filesystem that we are done performing writes to it and
450 * also allows filesystem to be frozen again. Must be matched with
451 * mnt_want_write() call above.
452 */
453void mnt_drop_write(struct vfsmount *mnt)
454{
455 __mnt_drop_write(mnt);
456 sb_end_write(mnt->mnt_sb);
457}
8366025e
DH
458EXPORT_SYMBOL_GPL(mnt_drop_write);
459
eb04c282
JK
460void __mnt_drop_write_file(struct file *file)
461{
14e43bf4
EB
462 if (!(file->f_mode & FMODE_WRITER))
463 __mnt_drop_write(file->f_path.mnt);
eb04c282
JK
464}
465
7c6893e3
MS
466void mnt_drop_write_file(struct file *file)
467{
a6795a58 468 __mnt_drop_write_file(file);
7c6893e3
MS
469 sb_end_write(file_inode(file)->i_sb);
470}
2a79f17e
AV
471EXPORT_SYMBOL(mnt_drop_write_file);
472
fbdc2f6c 473static inline int mnt_hold_writers(struct mount *mnt)
8366025e 474{
83adc753 475 mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
3d733633 476 /*
d3ef3d73
NP
477 * After storing MNT_WRITE_HOLD, we'll read the counters. This store
478 * should be visible before we do.
3d733633 479 */
d3ef3d73
NP
480 smp_mb();
481
3d733633 482 /*
d3ef3d73
NP
483 * With writers on hold, if this value is zero, then there are
484 * definitely no active writers (although held writers may subsequently
485 * increment the count, they'll have to wait, and decrement it after
486 * seeing MNT_READONLY).
487 *
488 * It is OK to have counter incremented on one CPU and decremented on
489 * another: the sum will add up correctly. The danger would be when we
490 * sum up each counter, if we read a counter before it is incremented,
491 * but then read another CPU's count which it has been subsequently
492 * decremented from -- we would see more decrements than we should.
493 * MNT_WRITE_HOLD protects against this scenario, because
494 * mnt_want_write first increments count, then smp_mb, then spins on
495 * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
496 * we're counting up here.
3d733633 497 */
c6653a83 498 if (mnt_get_writers(mnt) > 0)
fbdc2f6c
CB
499 return -EBUSY;
500
501 return 0;
502}
503
504static inline void mnt_unhold_writers(struct mount *mnt)
505{
d3ef3d73
NP
506 /*
507 * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
508 * that become unheld will see MNT_READONLY.
509 */
510 smp_wmb();
83adc753 511 mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
fbdc2f6c
CB
512}
513
514static int mnt_make_readonly(struct mount *mnt)
515{
516 int ret;
517
518 ret = mnt_hold_writers(mnt);
519 if (!ret)
520 mnt->mnt.mnt_flags |= MNT_READONLY;
521 mnt_unhold_writers(mnt);
3d733633 522 return ret;
8366025e 523}
8366025e 524
4ed5e82f
MS
525int sb_prepare_remount_readonly(struct super_block *sb)
526{
527 struct mount *mnt;
528 int err = 0;
529
8e8b8796
MS
530 /* Racy optimization. Recheck the counter under MNT_WRITE_HOLD */
531 if (atomic_long_read(&sb->s_remove_count))
532 return -EBUSY;
533
719ea2fb 534 lock_mount_hash();
4ed5e82f
MS
535 list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
536 if (!(mnt->mnt.mnt_flags & MNT_READONLY)) {
537 mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
538 smp_mb();
539 if (mnt_get_writers(mnt) > 0) {
540 err = -EBUSY;
541 break;
542 }
543 }
544 }
8e8b8796
MS
545 if (!err && atomic_long_read(&sb->s_remove_count))
546 err = -EBUSY;
547
4ed5e82f
MS
548 if (!err) {
549 sb->s_readonly_remount = 1;
550 smp_wmb();
551 }
552 list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
553 if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD)
554 mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
555 }
719ea2fb 556 unlock_mount_hash();
4ed5e82f
MS
557
558 return err;
559}
560
b105e270 561static void free_vfsmnt(struct mount *mnt)
1da177e4 562{
a6435940
CB
563 struct user_namespace *mnt_userns;
564
565 mnt_userns = mnt_user_ns(&mnt->mnt);
911a39c1 566 if (!initial_idmapping(mnt_userns))
a6435940 567 put_user_ns(mnt_userns);
fcc139ae 568 kfree_const(mnt->mnt_devname);
d3ef3d73 569#ifdef CONFIG_SMP
68e8a9fe 570 free_percpu(mnt->mnt_pcp);
d3ef3d73 571#endif
b105e270 572 kmem_cache_free(mnt_cache, mnt);
1da177e4
LT
573}
574
8ffcb32e
DH
575static void delayed_free_vfsmnt(struct rcu_head *head)
576{
577 free_vfsmnt(container_of(head, struct mount, mnt_rcu));
578}
579
48a066e7 580/* call under rcu_read_lock */
294d71ff 581int __legitimize_mnt(struct vfsmount *bastard, unsigned seq)
48a066e7
AV
582{
583 struct mount *mnt;
584 if (read_seqretry(&mount_lock, seq))
294d71ff 585 return 1;
48a066e7 586 if (bastard == NULL)
294d71ff 587 return 0;
48a066e7
AV
588 mnt = real_mount(bastard);
589 mnt_add_count(mnt, 1);
119e1ef8 590 smp_mb(); // see mntput_no_expire()
48a066e7 591 if (likely(!read_seqretry(&mount_lock, seq)))
294d71ff 592 return 0;
48a066e7
AV
593 if (bastard->mnt_flags & MNT_SYNC_UMOUNT) {
594 mnt_add_count(mnt, -1);
294d71ff
AV
595 return 1;
596 }
119e1ef8
AV
597 lock_mount_hash();
598 if (unlikely(bastard->mnt_flags & MNT_DOOMED)) {
599 mnt_add_count(mnt, -1);
600 unlock_mount_hash();
601 return 1;
602 }
603 unlock_mount_hash();
604 /* caller will mntput() */
294d71ff
AV
605 return -1;
606}
607
608/* call under rcu_read_lock */
609bool legitimize_mnt(struct vfsmount *bastard, unsigned seq)
610{
611 int res = __legitimize_mnt(bastard, seq);
612 if (likely(!res))
613 return true;
614 if (unlikely(res < 0)) {
615 rcu_read_unlock();
616 mntput(bastard);
617 rcu_read_lock();
48a066e7 618 }
48a066e7
AV
619 return false;
620}
621
1da177e4 622/*
474279dc 623 * find the first mount at @dentry on vfsmount @mnt.
48a066e7 624 * call under rcu_read_lock()
1da177e4 625 */
474279dc 626struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
1da177e4 627{
38129a13 628 struct hlist_head *head = m_hash(mnt, dentry);
474279dc
AV
629 struct mount *p;
630
38129a13 631 hlist_for_each_entry_rcu(p, head, mnt_hash)
474279dc
AV
632 if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry)
633 return p;
634 return NULL;
635}
636
a05964f3 637/*
f015f126
DH
638 * lookup_mnt - Return the first child mount mounted at path
639 *
640 * "First" means first mounted chronologically. If you create the
641 * following mounts:
642 *
643 * mount /dev/sda1 /mnt
644 * mount /dev/sda2 /mnt
645 * mount /dev/sda3 /mnt
646 *
647 * Then lookup_mnt() on the base /mnt dentry in the root mount will
648 * return successively the root dentry and vfsmount of /dev/sda1, then
649 * /dev/sda2, then /dev/sda3, then NULL.
650 *
651 * lookup_mnt takes a reference to the found vfsmount.
a05964f3 652 */
ca71cf71 653struct vfsmount *lookup_mnt(const struct path *path)
a05964f3 654{
c7105365 655 struct mount *child_mnt;
48a066e7
AV
656 struct vfsmount *m;
657 unsigned seq;
99b7db7b 658
48a066e7
AV
659 rcu_read_lock();
660 do {
661 seq = read_seqbegin(&mount_lock);
662 child_mnt = __lookup_mnt(path->mnt, path->dentry);
663 m = child_mnt ? &child_mnt->mnt : NULL;
664 } while (!legitimize_mnt(m, seq));
665 rcu_read_unlock();
666 return m;
a05964f3
RP
667}
668
9f6c61f9
MS
669static inline void lock_ns_list(struct mnt_namespace *ns)
670{
671 spin_lock(&ns->ns_lock);
672}
673
674static inline void unlock_ns_list(struct mnt_namespace *ns)
675{
676 spin_unlock(&ns->ns_lock);
677}
678
679static inline bool mnt_is_cursor(struct mount *mnt)
680{
681 return mnt->mnt.mnt_flags & MNT_CURSOR;
682}
683
7af1364f
EB
684/*
685 * __is_local_mountpoint - Test to see if dentry is a mountpoint in the
686 * current mount namespace.
687 *
688 * The common case is dentries are not mountpoints at all and that
689 * test is handled inline. For the slow case when we are actually
690 * dealing with a mountpoint of some kind, walk through all of the
691 * mounts in the current mount namespace and test to see if the dentry
692 * is a mountpoint.
693 *
694 * The mount_hashtable is not usable in the context because we
695 * need to identify all mounts that may be in the current mount
696 * namespace not just a mount that happens to have some specified
697 * parent mount.
698 */
699bool __is_local_mountpoint(struct dentry *dentry)
700{
701 struct mnt_namespace *ns = current->nsproxy->mnt_ns;
702 struct mount *mnt;
703 bool is_covered = false;
704
7af1364f 705 down_read(&namespace_sem);
9f6c61f9 706 lock_ns_list(ns);
7af1364f 707 list_for_each_entry(mnt, &ns->list, mnt_list) {
9f6c61f9
MS
708 if (mnt_is_cursor(mnt))
709 continue;
7af1364f
EB
710 is_covered = (mnt->mnt_mountpoint == dentry);
711 if (is_covered)
712 break;
713 }
9f6c61f9 714 unlock_ns_list(ns);
7af1364f 715 up_read(&namespace_sem);
5ad05cc8 716
7af1364f
EB
717 return is_covered;
718}
719
e2dfa935 720static struct mountpoint *lookup_mountpoint(struct dentry *dentry)
84d17192 721{
0818bf27 722 struct hlist_head *chain = mp_hash(dentry);
84d17192
AV
723 struct mountpoint *mp;
724
0818bf27 725 hlist_for_each_entry(mp, chain, m_hash) {
84d17192 726 if (mp->m_dentry == dentry) {
84d17192
AV
727 mp->m_count++;
728 return mp;
729 }
730 }
e2dfa935
EB
731 return NULL;
732}
733
3895dbf8 734static struct mountpoint *get_mountpoint(struct dentry *dentry)
e2dfa935 735{
3895dbf8 736 struct mountpoint *mp, *new = NULL;
e2dfa935 737 int ret;
84d17192 738
3895dbf8 739 if (d_mountpoint(dentry)) {
1e9c75fb
BC
740 /* might be worth a WARN_ON() */
741 if (d_unlinked(dentry))
742 return ERR_PTR(-ENOENT);
3895dbf8
EB
743mountpoint:
744 read_seqlock_excl(&mount_lock);
745 mp = lookup_mountpoint(dentry);
746 read_sequnlock_excl(&mount_lock);
747 if (mp)
748 goto done;
749 }
750
751 if (!new)
752 new = kmalloc(sizeof(struct mountpoint), GFP_KERNEL);
753 if (!new)
84d17192
AV
754 return ERR_PTR(-ENOMEM);
755
3895dbf8
EB
756
757 /* Exactly one processes may set d_mounted */
eed81007 758 ret = d_set_mounted(dentry);
eed81007 759
3895dbf8
EB
760 /* Someone else set d_mounted? */
761 if (ret == -EBUSY)
762 goto mountpoint;
763
764 /* The dentry is not available as a mountpoint? */
765 mp = ERR_PTR(ret);
766 if (ret)
767 goto done;
768
769 /* Add the new mountpoint to the hash table */
770 read_seqlock_excl(&mount_lock);
4edbe133 771 new->m_dentry = dget(dentry);
3895dbf8
EB
772 new->m_count = 1;
773 hlist_add_head(&new->m_hash, mp_hash(dentry));
774 INIT_HLIST_HEAD(&new->m_list);
775 read_sequnlock_excl(&mount_lock);
776
777 mp = new;
778 new = NULL;
779done:
780 kfree(new);
84d17192
AV
781 return mp;
782}
783
4edbe133
AV
784/*
785 * vfsmount lock must be held. Additionally, the caller is responsible
786 * for serializing calls for given disposal list.
787 */
788static void __put_mountpoint(struct mountpoint *mp, struct list_head *list)
84d17192
AV
789{
790 if (!--mp->m_count) {
791 struct dentry *dentry = mp->m_dentry;
0a5eb7c8 792 BUG_ON(!hlist_empty(&mp->m_list));
84d17192
AV
793 spin_lock(&dentry->d_lock);
794 dentry->d_flags &= ~DCACHE_MOUNTED;
795 spin_unlock(&dentry->d_lock);
4edbe133 796 dput_to_list(dentry, list);
0818bf27 797 hlist_del(&mp->m_hash);
84d17192
AV
798 kfree(mp);
799 }
800}
801
4edbe133
AV
802/* called with namespace_lock and vfsmount lock */
803static void put_mountpoint(struct mountpoint *mp)
804{
805 __put_mountpoint(mp, &ex_mountpoints);
806}
807
143c8c91 808static inline int check_mnt(struct mount *mnt)
1da177e4 809{
6b3286ed 810 return mnt->mnt_ns == current->nsproxy->mnt_ns;
1da177e4
LT
811}
812
7fea21ae
AR
813/* for aufs, CONFIG_AUFS_BR_FUSE */
814int is_current_mnt_ns(struct vfsmount *mnt)
815{
816 return check_mnt(real_mount(mnt));
817}
818EXPORT_SYMBOL_GPL(is_current_mnt_ns);
819
99b7db7b
NP
820/*
821 * vfsmount lock must be held for write
822 */
6b3286ed 823static void touch_mnt_namespace(struct mnt_namespace *ns)
5addc5dd
AV
824{
825 if (ns) {
826 ns->event = ++event;
827 wake_up_interruptible(&ns->poll);
828 }
829}
830
99b7db7b
NP
831/*
832 * vfsmount lock must be held for write
833 */
6b3286ed 834static void __touch_mnt_namespace(struct mnt_namespace *ns)
5addc5dd
AV
835{
836 if (ns && ns->event != event) {
837 ns->event = event;
838 wake_up_interruptible(&ns->poll);
839 }
840}
841
99b7db7b
NP
842/*
843 * vfsmount lock must be held for write
844 */
e4e59906 845static struct mountpoint *unhash_mnt(struct mount *mnt)
419148da 846{
e4e59906 847 struct mountpoint *mp;
0714a533 848 mnt->mnt_parent = mnt;
a73324da 849 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
6b41d536 850 list_del_init(&mnt->mnt_child);
38129a13 851 hlist_del_init_rcu(&mnt->mnt_hash);
0a5eb7c8 852 hlist_del_init(&mnt->mnt_mp_list);
e4e59906 853 mp = mnt->mnt_mp;
84d17192 854 mnt->mnt_mp = NULL;
e4e59906 855 return mp;
7bdb11de
EB
856}
857
6a46c573
EB
858/*
859 * vfsmount lock must be held for write
860 */
861static void umount_mnt(struct mount *mnt)
862{
e4e59906 863 put_mountpoint(unhash_mnt(mnt));
6a46c573
EB
864}
865
99b7db7b
NP
866/*
867 * vfsmount lock must be held for write
868 */
84d17192
AV
869void mnt_set_mountpoint(struct mount *mnt,
870 struct mountpoint *mp,
44d964d6 871 struct mount *child_mnt)
b90fa9ae 872{
84d17192 873 mp->m_count++;
3a2393d7 874 mnt_add_count(mnt, 1); /* essentially, that's mntget */
4edbe133 875 child_mnt->mnt_mountpoint = mp->m_dentry;
3a2393d7 876 child_mnt->mnt_parent = mnt;
84d17192 877 child_mnt->mnt_mp = mp;
0a5eb7c8 878 hlist_add_head(&child_mnt->mnt_mp_list, &mp->m_list);
b90fa9ae
RP
879}
880
1064f874
EB
881static void __attach_mnt(struct mount *mnt, struct mount *parent)
882{
883 hlist_add_head_rcu(&mnt->mnt_hash,
884 m_hash(&parent->mnt, mnt->mnt_mountpoint));
885 list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
886}
887
99b7db7b
NP
888/*
889 * vfsmount lock must be held for write
890 */
84d17192
AV
891static void attach_mnt(struct mount *mnt,
892 struct mount *parent,
893 struct mountpoint *mp)
1da177e4 894{
84d17192 895 mnt_set_mountpoint(parent, mp, mnt);
1064f874 896 __attach_mnt(mnt, parent);
b90fa9ae
RP
897}
898
1064f874 899void mnt_change_mountpoint(struct mount *parent, struct mountpoint *mp, struct mount *mnt)
12a5b529 900{
1064f874 901 struct mountpoint *old_mp = mnt->mnt_mp;
1064f874
EB
902 struct mount *old_parent = mnt->mnt_parent;
903
904 list_del_init(&mnt->mnt_child);
905 hlist_del_init(&mnt->mnt_mp_list);
906 hlist_del_init_rcu(&mnt->mnt_hash);
907
908 attach_mnt(mnt, parent, mp);
909
910 put_mountpoint(old_mp);
1064f874 911 mnt_add_count(old_parent, -1);
12a5b529
AV
912}
913
b90fa9ae 914/*
99b7db7b 915 * vfsmount lock must be held for write
b90fa9ae 916 */
1064f874 917static void commit_tree(struct mount *mnt)
b90fa9ae 918{
0714a533 919 struct mount *parent = mnt->mnt_parent;
83adc753 920 struct mount *m;
b90fa9ae 921 LIST_HEAD(head);
143c8c91 922 struct mnt_namespace *n = parent->mnt_ns;
b90fa9ae 923
0714a533 924 BUG_ON(parent == mnt);
b90fa9ae 925
1a4eeaf2 926 list_add_tail(&head, &mnt->mnt_list);
f7a99c5b 927 list_for_each_entry(m, &head, mnt_list)
143c8c91 928 m->mnt_ns = n;
f03c6599 929
b90fa9ae
RP
930 list_splice(&head, n->list.prev);
931
d2921684
EB
932 n->mounts += n->pending_mounts;
933 n->pending_mounts = 0;
934
1064f874 935 __attach_mnt(mnt, parent);
6b3286ed 936 touch_mnt_namespace(n);
1da177e4
LT
937}
938
909b0a88 939static struct mount *next_mnt(struct mount *p, struct mount *root)
1da177e4 940{
6b41d536
AV
941 struct list_head *next = p->mnt_mounts.next;
942 if (next == &p->mnt_mounts) {
1da177e4 943 while (1) {
909b0a88 944 if (p == root)
1da177e4 945 return NULL;
6b41d536
AV
946 next = p->mnt_child.next;
947 if (next != &p->mnt_parent->mnt_mounts)
1da177e4 948 break;
0714a533 949 p = p->mnt_parent;
1da177e4
LT
950 }
951 }
6b41d536 952 return list_entry(next, struct mount, mnt_child);
1da177e4
LT
953}
954
315fc83e 955static struct mount *skip_mnt_tree(struct mount *p)
9676f0c6 956{
6b41d536
AV
957 struct list_head *prev = p->mnt_mounts.prev;
958 while (prev != &p->mnt_mounts) {
959 p = list_entry(prev, struct mount, mnt_child);
960 prev = p->mnt_mounts.prev;
9676f0c6
RP
961 }
962 return p;
963}
964
8f291889
AV
965/**
966 * vfs_create_mount - Create a mount for a configured superblock
967 * @fc: The configuration context with the superblock attached
968 *
969 * Create a mount to an already configured superblock. If necessary, the
970 * caller should invoke vfs_get_tree() before calling this.
971 *
972 * Note that this does not attach the mount to anything.
973 */
974struct vfsmount *vfs_create_mount(struct fs_context *fc)
9d412a43 975{
b105e270 976 struct mount *mnt;
911a39c1 977 struct user_namespace *fs_userns;
9d412a43 978
8f291889
AV
979 if (!fc->root)
980 return ERR_PTR(-EINVAL);
9d412a43 981
8f291889 982 mnt = alloc_vfsmnt(fc->source ?: "none");
9d412a43
AV
983 if (!mnt)
984 return ERR_PTR(-ENOMEM);
985
8f291889 986 if (fc->sb_flags & SB_KERNMOUNT)
b105e270 987 mnt->mnt.mnt_flags = MNT_INTERNAL;
9d412a43 988
8f291889
AV
989 atomic_inc(&fc->root->d_sb->s_active);
990 mnt->mnt.mnt_sb = fc->root->d_sb;
991 mnt->mnt.mnt_root = dget(fc->root);
992 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
993 mnt->mnt_parent = mnt;
9d412a43 994
911a39c1
CB
995 fs_userns = mnt->mnt.mnt_sb->s_user_ns;
996 if (!initial_idmapping(fs_userns))
997 mnt->mnt.mnt_userns = get_user_ns(fs_userns);
998
719ea2fb 999 lock_mount_hash();
8f291889 1000 list_add_tail(&mnt->mnt_instance, &mnt->mnt.mnt_sb->s_mounts);
719ea2fb 1001 unlock_mount_hash();
b105e270 1002 return &mnt->mnt;
9d412a43 1003}
8f291889
AV
1004EXPORT_SYMBOL(vfs_create_mount);
1005
1006struct vfsmount *fc_mount(struct fs_context *fc)
1007{
1008 int err = vfs_get_tree(fc);
1009 if (!err) {
1010 up_write(&fc->root->d_sb->s_umount);
1011 return vfs_create_mount(fc);
1012 }
1013 return ERR_PTR(err);
1014}
1015EXPORT_SYMBOL(fc_mount);
1016
9bc61ab1
DH
1017struct vfsmount *vfs_kern_mount(struct file_system_type *type,
1018 int flags, const char *name,
1019 void *data)
9d412a43 1020{
9bc61ab1 1021 struct fs_context *fc;
8f291889 1022 struct vfsmount *mnt;
9bc61ab1 1023 int ret = 0;
9d412a43
AV
1024
1025 if (!type)
3e1aeb00 1026 return ERR_PTR(-EINVAL);
9d412a43 1027
9bc61ab1
DH
1028 fc = fs_context_for_mount(type, flags);
1029 if (IS_ERR(fc))
1030 return ERR_CAST(fc);
1031
3e1aeb00
DH
1032 if (name)
1033 ret = vfs_parse_fs_string(fc, "source",
1034 name, strlen(name));
9bc61ab1
DH
1035 if (!ret)
1036 ret = parse_monolithic_mount_data(fc, data);
1037 if (!ret)
8f291889
AV
1038 mnt = fc_mount(fc);
1039 else
1040 mnt = ERR_PTR(ret);
9d412a43 1041
9bc61ab1 1042 put_fs_context(fc);
8f291889 1043 return mnt;
9d412a43
AV
1044}
1045EXPORT_SYMBOL_GPL(vfs_kern_mount);
1046
93faccbb
EB
1047struct vfsmount *
1048vfs_submount(const struct dentry *mountpoint, struct file_system_type *type,
1049 const char *name, void *data)
1050{
1051 /* Until it is worked out how to pass the user namespace
1052 * through from the parent mount to the submount don't support
1053 * unprivileged mounts with submounts.
1054 */
1055 if (mountpoint->d_sb->s_user_ns != &init_user_ns)
1056 return ERR_PTR(-EPERM);
1057
e462ec50 1058 return vfs_kern_mount(type, SB_SUBMOUNT, name, data);
93faccbb
EB
1059}
1060EXPORT_SYMBOL_GPL(vfs_submount);
1061
87129cc0 1062static struct mount *clone_mnt(struct mount *old, struct dentry *root,
36341f64 1063 int flag)
1da177e4 1064{
87129cc0 1065 struct super_block *sb = old->mnt.mnt_sb;
be34d1a3
DH
1066 struct mount *mnt;
1067 int err;
1da177e4 1068
be34d1a3
DH
1069 mnt = alloc_vfsmnt(old->mnt_devname);
1070 if (!mnt)
1071 return ERR_PTR(-ENOMEM);
719f5d7f 1072
7a472ef4 1073 if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE))
be34d1a3
DH
1074 mnt->mnt_group_id = 0; /* not a peer of original */
1075 else
1076 mnt->mnt_group_id = old->mnt_group_id;
b90fa9ae 1077
be34d1a3
DH
1078 if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
1079 err = mnt_alloc_group_id(mnt);
1080 if (err)
1081 goto out_free;
1da177e4 1082 }
be34d1a3 1083
16a34adb
AV
1084 mnt->mnt.mnt_flags = old->mnt.mnt_flags;
1085 mnt->mnt.mnt_flags &= ~(MNT_WRITE_HOLD|MNT_MARKED|MNT_INTERNAL);
5ff9d8a6 1086
be34d1a3 1087 atomic_inc(&sb->s_active);
a6435940 1088 mnt->mnt.mnt_userns = mnt_user_ns(&old->mnt);
911a39c1 1089 if (!initial_idmapping(mnt->mnt.mnt_userns))
a6435940 1090 mnt->mnt.mnt_userns = get_user_ns(mnt->mnt.mnt_userns);
be34d1a3
DH
1091 mnt->mnt.mnt_sb = sb;
1092 mnt->mnt.mnt_root = dget(root);
1093 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
1094 mnt->mnt_parent = mnt;
719ea2fb 1095 lock_mount_hash();
be34d1a3 1096 list_add_tail(&mnt->mnt_instance, &sb->s_mounts);
719ea2fb 1097 unlock_mount_hash();
be34d1a3 1098
7a472ef4
EB
1099 if ((flag & CL_SLAVE) ||
1100 ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) {
be34d1a3
DH
1101 list_add(&mnt->mnt_slave, &old->mnt_slave_list);
1102 mnt->mnt_master = old;
1103 CLEAR_MNT_SHARED(mnt);
1104 } else if (!(flag & CL_PRIVATE)) {
1105 if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
1106 list_add(&mnt->mnt_share, &old->mnt_share);
1107 if (IS_MNT_SLAVE(old))
1108 list_add(&mnt->mnt_slave, &old->mnt_slave);
1109 mnt->mnt_master = old->mnt_master;
5235d448
AV
1110 } else {
1111 CLEAR_MNT_SHARED(mnt);
be34d1a3
DH
1112 }
1113 if (flag & CL_MAKE_SHARED)
1114 set_mnt_shared(mnt);
1115
1116 /* stick the duplicate mount on the same expiry list
1117 * as the original if that was on one */
1118 if (flag & CL_EXPIRE) {
1119 if (!list_empty(&old->mnt_expire))
1120 list_add(&mnt->mnt_expire, &old->mnt_expire);
1121 }
1122
cb338d06 1123 return mnt;
719f5d7f
MS
1124
1125 out_free:
8ffcb32e 1126 mnt_free_id(mnt);
719f5d7f 1127 free_vfsmnt(mnt);
be34d1a3 1128 return ERR_PTR(err);
1da177e4
LT
1129}
1130
9ea459e1
AV
1131static void cleanup_mnt(struct mount *mnt)
1132{
56cbb429
AV
1133 struct hlist_node *p;
1134 struct mount *m;
9ea459e1 1135 /*
56cbb429
AV
1136 * The warning here probably indicates that somebody messed
1137 * up a mnt_want/drop_write() pair. If this happens, the
1138 * filesystem was probably unable to make r/w->r/o transitions.
9ea459e1
AV
1139 * The locking used to deal with mnt_count decrement provides barriers,
1140 * so mnt_get_writers() below is safe.
1141 */
1142 WARN_ON(mnt_get_writers(mnt));
1143 if (unlikely(mnt->mnt_pins.first))
1144 mnt_pin_kill(mnt);
56cbb429
AV
1145 hlist_for_each_entry_safe(m, p, &mnt->mnt_stuck_children, mnt_umount) {
1146 hlist_del(&m->mnt_umount);
1147 mntput(&m->mnt);
1148 }
9ea459e1
AV
1149 fsnotify_vfsmount_delete(&mnt->mnt);
1150 dput(mnt->mnt.mnt_root);
1151 deactivate_super(mnt->mnt.mnt_sb);
1152 mnt_free_id(mnt);
1153 call_rcu(&mnt->mnt_rcu, delayed_free_vfsmnt);
1154}
1155
1156static void __cleanup_mnt(struct rcu_head *head)
1157{
1158 cleanup_mnt(container_of(head, struct mount, mnt_rcu));
1159}
1160
1161static LLIST_HEAD(delayed_mntput_list);
1162static void delayed_mntput(struct work_struct *unused)
1163{
1164 struct llist_node *node = llist_del_all(&delayed_mntput_list);
29785735 1165 struct mount *m, *t;
9ea459e1 1166
29785735
BP
1167 llist_for_each_entry_safe(m, t, node, mnt_llist)
1168 cleanup_mnt(m);
9ea459e1
AV
1169}
1170static DECLARE_DELAYED_WORK(delayed_mntput_work, delayed_mntput);
1171
900148dc 1172static void mntput_no_expire(struct mount *mnt)
b3e19d92 1173{
4edbe133 1174 LIST_HEAD(list);
edf7ddbf 1175 int count;
4edbe133 1176
48a066e7 1177 rcu_read_lock();
9ea0a46c
AV
1178 if (likely(READ_ONCE(mnt->mnt_ns))) {
1179 /*
1180 * Since we don't do lock_mount_hash() here,
1181 * ->mnt_ns can change under us. However, if it's
1182 * non-NULL, then there's a reference that won't
1183 * be dropped until after an RCU delay done after
1184 * turning ->mnt_ns NULL. So if we observe it
1185 * non-NULL under rcu_read_lock(), the reference
1186 * we are dropping is not the final one.
1187 */
1188 mnt_add_count(mnt, -1);
48a066e7 1189 rcu_read_unlock();
f03c6599 1190 return;
b3e19d92 1191 }
719ea2fb 1192 lock_mount_hash();
119e1ef8
AV
1193 /*
1194 * make sure that if __legitimize_mnt() has not seen us grab
1195 * mount_lock, we'll see their refcount increment here.
1196 */
1197 smp_mb();
9ea0a46c 1198 mnt_add_count(mnt, -1);
edf7ddbf
EB
1199 count = mnt_get_count(mnt);
1200 if (count != 0) {
1201 WARN_ON(count < 0);
48a066e7 1202 rcu_read_unlock();
719ea2fb 1203 unlock_mount_hash();
99b7db7b
NP
1204 return;
1205 }
48a066e7
AV
1206 if (unlikely(mnt->mnt.mnt_flags & MNT_DOOMED)) {
1207 rcu_read_unlock();
1208 unlock_mount_hash();
1209 return;
1210 }
1211 mnt->mnt.mnt_flags |= MNT_DOOMED;
1212 rcu_read_unlock();
962830df 1213
39f7c4db 1214 list_del(&mnt->mnt_instance);
ce07d891
EB
1215
1216 if (unlikely(!list_empty(&mnt->mnt_mounts))) {
1217 struct mount *p, *tmp;
1218 list_for_each_entry_safe(p, tmp, &mnt->mnt_mounts, mnt_child) {
4edbe133 1219 __put_mountpoint(unhash_mnt(p), &list);
56cbb429 1220 hlist_add_head(&p->mnt_umount, &mnt->mnt_stuck_children);
ce07d891
EB
1221 }
1222 }
719ea2fb 1223 unlock_mount_hash();
4edbe133 1224 shrink_dentry_list(&list);
649a795a 1225
9ea459e1
AV
1226 if (likely(!(mnt->mnt.mnt_flags & MNT_INTERNAL))) {
1227 struct task_struct *task = current;
1228 if (likely(!(task->flags & PF_KTHREAD))) {
1229 init_task_work(&mnt->mnt_rcu, __cleanup_mnt);
91989c70 1230 if (!task_work_add(task, &mnt->mnt_rcu, TWA_RESUME))
9ea459e1
AV
1231 return;
1232 }
1233 if (llist_add(&mnt->mnt_llist, &delayed_mntput_list))
1234 schedule_delayed_work(&delayed_mntput_work, 1);
1235 return;
1236 }
1237 cleanup_mnt(mnt);
b3e19d92 1238}
b3e19d92
NP
1239
1240void mntput(struct vfsmount *mnt)
1241{
1242 if (mnt) {
863d684f 1243 struct mount *m = real_mount(mnt);
b3e19d92 1244 /* avoid cacheline pingpong, hope gcc doesn't get "smart" */
863d684f
AV
1245 if (unlikely(m->mnt_expiry_mark))
1246 m->mnt_expiry_mark = 0;
1247 mntput_no_expire(m);
b3e19d92
NP
1248 }
1249}
1250EXPORT_SYMBOL(mntput);
1251
1252struct vfsmount *mntget(struct vfsmount *mnt)
1253{
1254 if (mnt)
83adc753 1255 mnt_add_count(real_mount(mnt), 1);
b3e19d92
NP
1256 return mnt;
1257}
1258EXPORT_SYMBOL(mntget);
1259
1f287bc4
RD
1260/**
1261 * path_is_mountpoint() - Check if path is a mount in the current namespace.
1262 * @path: path to check
c6609c0a
IK
1263 *
1264 * d_mountpoint() can only be used reliably to establish if a dentry is
1265 * not mounted in any namespace and that common case is handled inline.
1266 * d_mountpoint() isn't aware of the possibility there may be multiple
1267 * mounts using a given dentry in a different namespace. This function
1268 * checks if the passed in path is a mountpoint rather than the dentry
1269 * alone.
1270 */
1271bool path_is_mountpoint(const struct path *path)
1272{
1273 unsigned seq;
1274 bool res;
1275
1276 if (!d_mountpoint(path->dentry))
1277 return false;
1278
1279 rcu_read_lock();
1280 do {
1281 seq = read_seqbegin(&mount_lock);
1282 res = __path_is_mountpoint(path);
1283 } while (read_seqretry(&mount_lock, seq));
1284 rcu_read_unlock();
1285
1286 return res;
1287}
1288EXPORT_SYMBOL(path_is_mountpoint);
1289
ca71cf71 1290struct vfsmount *mnt_clone_internal(const struct path *path)
7b7b1ace 1291{
3064c356
AV
1292 struct mount *p;
1293 p = clone_mnt(real_mount(path->mnt), path->dentry, CL_PRIVATE);
1294 if (IS_ERR(p))
1295 return ERR_CAST(p);
1296 p->mnt.mnt_flags |= MNT_INTERNAL;
1297 return &p->mnt;
7b7b1ace 1298}
1da177e4 1299
a1a2c409 1300#ifdef CONFIG_PROC_FS
9f6c61f9
MS
1301static struct mount *mnt_list_next(struct mnt_namespace *ns,
1302 struct list_head *p)
1303{
1304 struct mount *mnt, *ret = NULL;
1305
1306 lock_ns_list(ns);
1307 list_for_each_continue(p, &ns->list) {
1308 mnt = list_entry(p, typeof(*mnt), mnt_list);
1309 if (!mnt_is_cursor(mnt)) {
1310 ret = mnt;
1311 break;
1312 }
1313 }
1314 unlock_ns_list(ns);
1315
1316 return ret;
1317}
1318
0226f492 1319/* iterator; we want it to have access to namespace_sem, thus here... */
1da177e4
LT
1320static void *m_start(struct seq_file *m, loff_t *pos)
1321{
ede1bf0d 1322 struct proc_mounts *p = m->private;
9f6c61f9 1323 struct list_head *prev;
1da177e4 1324
390c6843 1325 down_read(&namespace_sem);
9f6c61f9
MS
1326 if (!*pos) {
1327 prev = &p->ns->list;
1328 } else {
1329 prev = &p->cursor.mnt_list;
1330
1331 /* Read after we'd reached the end? */
1332 if (list_empty(prev))
1333 return NULL;
c7999c36
AV
1334 }
1335
9f6c61f9 1336 return mnt_list_next(p->ns, prev);
1da177e4
LT
1337}
1338
1339static void *m_next(struct seq_file *m, void *v, loff_t *pos)
1340{
ede1bf0d 1341 struct proc_mounts *p = m->private;
9f6c61f9 1342 struct mount *mnt = v;
b0765fb8 1343
9f6c61f9
MS
1344 ++*pos;
1345 return mnt_list_next(p->ns, &mnt->mnt_list);
1da177e4
LT
1346}
1347
1348static void m_stop(struct seq_file *m, void *v)
1349{
9f6c61f9
MS
1350 struct proc_mounts *p = m->private;
1351 struct mount *mnt = v;
1352
1353 lock_ns_list(p->ns);
1354 if (mnt)
1355 list_move_tail(&p->cursor.mnt_list, &mnt->mnt_list);
1356 else
1357 list_del_init(&p->cursor.mnt_list);
1358 unlock_ns_list(p->ns);
390c6843 1359 up_read(&namespace_sem);
1da177e4
LT
1360}
1361
0226f492 1362static int m_show(struct seq_file *m, void *v)
2d4d4864 1363{
ede1bf0d 1364 struct proc_mounts *p = m->private;
9f6c61f9 1365 struct mount *r = v;
0226f492 1366 return p->show(m, &r->mnt);
1da177e4
LT
1367}
1368
a1a2c409 1369const struct seq_operations mounts_op = {
1da177e4
LT
1370 .start = m_start,
1371 .next = m_next,
1372 .stop = m_stop,
0226f492 1373 .show = m_show,
b4629fe2 1374};
9f6c61f9
MS
1375
1376void mnt_cursor_del(struct mnt_namespace *ns, struct mount *cursor)
1377{
1378 down_read(&namespace_sem);
1379 lock_ns_list(ns);
1380 list_del(&cursor->mnt_list);
1381 unlock_ns_list(ns);
1382 up_read(&namespace_sem);
1383}
a1a2c409 1384#endif /* CONFIG_PROC_FS */
b4629fe2 1385
1da177e4
LT
1386/**
1387 * may_umount_tree - check if a mount tree is busy
1f287bc4 1388 * @m: root of mount tree
1da177e4
LT
1389 *
1390 * This is called to check if a tree of mounts has any
1391 * open files, pwds, chroots or sub mounts that are
1392 * busy.
1393 */
909b0a88 1394int may_umount_tree(struct vfsmount *m)
1da177e4 1395{
909b0a88 1396 struct mount *mnt = real_mount(m);
36341f64
RP
1397 int actual_refs = 0;
1398 int minimum_refs = 0;
315fc83e 1399 struct mount *p;
909b0a88 1400 BUG_ON(!m);
1da177e4 1401
b3e19d92 1402 /* write lock needed for mnt_get_count */
719ea2fb 1403 lock_mount_hash();
909b0a88 1404 for (p = mnt; p; p = next_mnt(p, mnt)) {
83adc753 1405 actual_refs += mnt_get_count(p);
1da177e4 1406 minimum_refs += 2;
1da177e4 1407 }
719ea2fb 1408 unlock_mount_hash();
1da177e4
LT
1409
1410 if (actual_refs > minimum_refs)
e3474a8e 1411 return 0;
1da177e4 1412
e3474a8e 1413 return 1;
1da177e4
LT
1414}
1415
1416EXPORT_SYMBOL(may_umount_tree);
1417
1418/**
1419 * may_umount - check if a mount point is busy
1420 * @mnt: root of mount
1421 *
1422 * This is called to check if a mount point has any
1423 * open files, pwds, chroots or sub mounts. If the
1424 * mount has sub mounts this will return busy
1425 * regardless of whether the sub mounts are busy.
1426 *
1427 * Doesn't take quota and stuff into account. IOW, in some cases it will
1428 * give false negatives. The main reason why it's here is that we need
1429 * a non-destructive way to look for easily umountable filesystems.
1430 */
1431int may_umount(struct vfsmount *mnt)
1432{
e3474a8e 1433 int ret = 1;
8ad08d8a 1434 down_read(&namespace_sem);
719ea2fb 1435 lock_mount_hash();
1ab59738 1436 if (propagate_mount_busy(real_mount(mnt), 2))
e3474a8e 1437 ret = 0;
719ea2fb 1438 unlock_mount_hash();
8ad08d8a 1439 up_read(&namespace_sem);
a05964f3 1440 return ret;
1da177e4
LT
1441}
1442
1443EXPORT_SYMBOL(may_umount);
1444
97216be0 1445static void namespace_unlock(void)
70fbcdf4 1446{
a3b3c562 1447 struct hlist_head head;
56cbb429
AV
1448 struct hlist_node *p;
1449 struct mount *m;
4edbe133 1450 LIST_HEAD(list);
97216be0 1451
a3b3c562 1452 hlist_move_list(&unmounted, &head);
4edbe133 1453 list_splice_init(&ex_mountpoints, &list);
97216be0 1454
97216be0
AV
1455 up_write(&namespace_sem);
1456
4edbe133
AV
1457 shrink_dentry_list(&list);
1458
a3b3c562
EB
1459 if (likely(hlist_empty(&head)))
1460 return;
1461
22cb7405 1462 synchronize_rcu_expedited();
48a066e7 1463
56cbb429
AV
1464 hlist_for_each_entry_safe(m, p, &head, mnt_umount) {
1465 hlist_del(&m->mnt_umount);
1466 mntput(&m->mnt);
1467 }
70fbcdf4
RP
1468}
1469
97216be0 1470static inline void namespace_lock(void)
e3197d83 1471{
97216be0 1472 down_write(&namespace_sem);
e3197d83
AV
1473}
1474
e819f152
EB
1475enum umount_tree_flags {
1476 UMOUNT_SYNC = 1,
1477 UMOUNT_PROPAGATE = 2,
e0c9c0af 1478 UMOUNT_CONNECTED = 4,
e819f152 1479};
f2d0a123
EB
1480
1481static bool disconnect_mount(struct mount *mnt, enum umount_tree_flags how)
1482{
1483 /* Leaving mounts connected is only valid for lazy umounts */
1484 if (how & UMOUNT_SYNC)
1485 return true;
1486
1487 /* A mount without a parent has nothing to be connected to */
1488 if (!mnt_has_parent(mnt))
1489 return true;
1490
1491 /* Because the reference counting rules change when mounts are
1492 * unmounted and connected, umounted mounts may not be
1493 * connected to mounted mounts.
1494 */
1495 if (!(mnt->mnt_parent->mnt.mnt_flags & MNT_UMOUNT))
1496 return true;
1497
1498 /* Has it been requested that the mount remain connected? */
1499 if (how & UMOUNT_CONNECTED)
1500 return false;
1501
1502 /* Is the mount locked such that it needs to remain connected? */
1503 if (IS_MNT_LOCKED(mnt))
1504 return false;
1505
1506 /* By default disconnect the mount */
1507 return true;
1508}
1509
99b7db7b 1510/*
48a066e7 1511 * mount_lock must be held
99b7db7b
NP
1512 * namespace_sem must be held for write
1513 */
e819f152 1514static void umount_tree(struct mount *mnt, enum umount_tree_flags how)
1da177e4 1515{
c003b26f 1516 LIST_HEAD(tmp_list);
315fc83e 1517 struct mount *p;
1da177e4 1518
5d88457e
EB
1519 if (how & UMOUNT_PROPAGATE)
1520 propagate_mount_unlock(mnt);
1521
c003b26f 1522 /* Gather the mounts to umount */
590ce4bc
EB
1523 for (p = mnt; p; p = next_mnt(p, mnt)) {
1524 p->mnt.mnt_flags |= MNT_UMOUNT;
c003b26f 1525 list_move(&p->mnt_list, &tmp_list);
590ce4bc 1526 }
1da177e4 1527
411a938b 1528 /* Hide the mounts from mnt_mounts */
c003b26f 1529 list_for_each_entry(p, &tmp_list, mnt_list) {
88b368f2 1530 list_del_init(&p->mnt_child);
c003b26f 1531 }
88b368f2 1532
c003b26f 1533 /* Add propogated mounts to the tmp_list */
e819f152 1534 if (how & UMOUNT_PROPAGATE)
7b8a53fd 1535 propagate_umount(&tmp_list);
a05964f3 1536
c003b26f 1537 while (!list_empty(&tmp_list)) {
d2921684 1538 struct mnt_namespace *ns;
ce07d891 1539 bool disconnect;
c003b26f 1540 p = list_first_entry(&tmp_list, struct mount, mnt_list);
6776db3d 1541 list_del_init(&p->mnt_expire);
1a4eeaf2 1542 list_del_init(&p->mnt_list);
d2921684
EB
1543 ns = p->mnt_ns;
1544 if (ns) {
1545 ns->mounts--;
1546 __touch_mnt_namespace(ns);
1547 }
143c8c91 1548 p->mnt_ns = NULL;
e819f152 1549 if (how & UMOUNT_SYNC)
48a066e7 1550 p->mnt.mnt_flags |= MNT_SYNC_UMOUNT;
87b95ce0 1551
f2d0a123 1552 disconnect = disconnect_mount(p, how);
676da58d 1553 if (mnt_has_parent(p)) {
81b6b061 1554 mnt_add_count(p->mnt_parent, -1);
ce07d891
EB
1555 if (!disconnect) {
1556 /* Don't forget about p */
1557 list_add_tail(&p->mnt_child, &p->mnt_parent->mnt_mounts);
1558 } else {
1559 umount_mnt(p);
1560 }
7c4b93d8 1561 }
0f0afb1d 1562 change_mnt_propagation(p, MS_PRIVATE);
19a1c409
AV
1563 if (disconnect)
1564 hlist_add_head(&p->mnt_umount, &unmounted);
1da177e4
LT
1565 }
1566}
1567
b54b9be7 1568static void shrink_submounts(struct mount *mnt);
c35038be 1569
8d0347f6
DH
1570static int do_umount_root(struct super_block *sb)
1571{
1572 int ret = 0;
1573
1574 down_write(&sb->s_umount);
1575 if (!sb_rdonly(sb)) {
1576 struct fs_context *fc;
1577
1578 fc = fs_context_for_reconfigure(sb->s_root, SB_RDONLY,
1579 SB_RDONLY);
1580 if (IS_ERR(fc)) {
1581 ret = PTR_ERR(fc);
1582 } else {
1583 ret = parse_monolithic_mount_data(fc, NULL);
1584 if (!ret)
1585 ret = reconfigure_super(fc);
1586 put_fs_context(fc);
1587 }
1588 }
1589 up_write(&sb->s_umount);
1590 return ret;
1591}
1592
1ab59738 1593static int do_umount(struct mount *mnt, int flags)
1da177e4 1594{
1ab59738 1595 struct super_block *sb = mnt->mnt.mnt_sb;
1da177e4
LT
1596 int retval;
1597
1ab59738 1598 retval = security_sb_umount(&mnt->mnt, flags);
1da177e4
LT
1599 if (retval)
1600 return retval;
1601
1602 /*
1603 * Allow userspace to request a mountpoint be expired rather than
1604 * unmounting unconditionally. Unmount only happens if:
1605 * (1) the mark is already set (the mark is cleared by mntput())
1606 * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
1607 */
1608 if (flags & MNT_EXPIRE) {
1ab59738 1609 if (&mnt->mnt == current->fs->root.mnt ||
1da177e4
LT
1610 flags & (MNT_FORCE | MNT_DETACH))
1611 return -EINVAL;
1612
b3e19d92
NP
1613 /*
1614 * probably don't strictly need the lock here if we examined
1615 * all race cases, but it's a slowpath.
1616 */
719ea2fb 1617 lock_mount_hash();
83adc753 1618 if (mnt_get_count(mnt) != 2) {
719ea2fb 1619 unlock_mount_hash();
1da177e4 1620 return -EBUSY;
b3e19d92 1621 }
719ea2fb 1622 unlock_mount_hash();
1da177e4 1623
863d684f 1624 if (!xchg(&mnt->mnt_expiry_mark, 1))
1da177e4
LT
1625 return -EAGAIN;
1626 }
1627
1628 /*
1629 * If we may have to abort operations to get out of this
1630 * mount, and they will themselves hold resources we must
1631 * allow the fs to do things. In the Unix tradition of
1632 * 'Gee thats tricky lets do it in userspace' the umount_begin
1633 * might fail to complete on the first run through as other tasks
1634 * must return, and the like. Thats for the mount program to worry
1635 * about for the moment.
1636 */
1637
42faad99 1638 if (flags & MNT_FORCE && sb->s_op->umount_begin) {
42faad99 1639 sb->s_op->umount_begin(sb);
42faad99 1640 }
1da177e4
LT
1641
1642 /*
1643 * No sense to grab the lock for this test, but test itself looks
1644 * somewhat bogus. Suggestions for better replacement?
1645 * Ho-hum... In principle, we might treat that as umount + switch
1646 * to rootfs. GC would eventually take care of the old vfsmount.
1647 * Actually it makes sense, especially if rootfs would contain a
1648 * /reboot - static binary that would close all descriptors and
1649 * call reboot(9). Then init(8) could umount root and exec /reboot.
1650 */
1ab59738 1651 if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
1da177e4
LT
1652 /*
1653 * Special case for "unmounting" root ...
1654 * we just try to remount it readonly.
1655 */
bc6155d1 1656 if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN))
a1480dcc 1657 return -EPERM;
8d0347f6 1658 return do_umount_root(sb);
1da177e4
LT
1659 }
1660
97216be0 1661 namespace_lock();
719ea2fb 1662 lock_mount_hash();
1da177e4 1663
25d202ed
EB
1664 /* Recheck MNT_LOCKED with the locks held */
1665 retval = -EINVAL;
1666 if (mnt->mnt.mnt_flags & MNT_LOCKED)
1667 goto out;
1668
1669 event++;
48a066e7 1670 if (flags & MNT_DETACH) {
1a4eeaf2 1671 if (!list_empty(&mnt->mnt_list))
e819f152 1672 umount_tree(mnt, UMOUNT_PROPAGATE);
1da177e4 1673 retval = 0;
48a066e7
AV
1674 } else {
1675 shrink_submounts(mnt);
1676 retval = -EBUSY;
1677 if (!propagate_mount_busy(mnt, 2)) {
1678 if (!list_empty(&mnt->mnt_list))
e819f152 1679 umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
48a066e7
AV
1680 retval = 0;
1681 }
1da177e4 1682 }
25d202ed 1683out:
719ea2fb 1684 unlock_mount_hash();
e3197d83 1685 namespace_unlock();
1da177e4
LT
1686 return retval;
1687}
1688
80b5dce8
EB
1689/*
1690 * __detach_mounts - lazily unmount all mounts on the specified dentry
1691 *
1692 * During unlink, rmdir, and d_drop it is possible to loose the path
1693 * to an existing mountpoint, and wind up leaking the mount.
1694 * detach_mounts allows lazily unmounting those mounts instead of
1695 * leaking them.
1696 *
1697 * The caller may hold dentry->d_inode->i_mutex.
1698 */
1699void __detach_mounts(struct dentry *dentry)
1700{
1701 struct mountpoint *mp;
1702 struct mount *mnt;
1703
1704 namespace_lock();
3895dbf8 1705 lock_mount_hash();
80b5dce8 1706 mp = lookup_mountpoint(dentry);
adc9b5c0 1707 if (!mp)
80b5dce8
EB
1708 goto out_unlock;
1709
e06b933e 1710 event++;
80b5dce8
EB
1711 while (!hlist_empty(&mp->m_list)) {
1712 mnt = hlist_entry(mp->m_list.first, struct mount, mnt_mp_list);
ce07d891 1713 if (mnt->mnt.mnt_flags & MNT_UMOUNT) {
fe78fcc8 1714 umount_mnt(mnt);
56cbb429 1715 hlist_add_head(&mnt->mnt_umount, &unmounted);
ce07d891 1716 }
e0c9c0af 1717 else umount_tree(mnt, UMOUNT_CONNECTED);
80b5dce8 1718 }
80b5dce8
EB
1719 put_mountpoint(mp);
1720out_unlock:
3895dbf8 1721 unlock_mount_hash();
80b5dce8
EB
1722 namespace_unlock();
1723}
1724
dd111b31 1725/*
9b40bc90
AV
1726 * Is the caller allowed to modify his namespace?
1727 */
1728static inline bool may_mount(void)
1729{
1730 return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN);
1731}
1732
f7e33bdb 1733static void warn_mandlock(void)
9e8925b6 1734{
f7e33bdb
JL
1735 pr_warn_once("=======================================================\n"
1736 "WARNING: The mand mount option has been deprecated and\n"
1737 " and is ignored by this kernel. Remove the mand\n"
1738 " option from the mount to silence this warning.\n"
1739 "=======================================================\n");
9e8925b6
JL
1740}
1741
25ccd24f 1742static int can_umount(const struct path *path, int flags)
1da177e4 1743{
25ccd24f 1744 struct mount *mnt = real_mount(path->mnt);
1da177e4 1745
9b40bc90
AV
1746 if (!may_mount())
1747 return -EPERM;
41525f56 1748 if (path->dentry != path->mnt->mnt_root)
25ccd24f 1749 return -EINVAL;
143c8c91 1750 if (!check_mnt(mnt))
25ccd24f 1751 return -EINVAL;
25d202ed 1752 if (mnt->mnt.mnt_flags & MNT_LOCKED) /* Check optimistically */
25ccd24f 1753 return -EINVAL;
b2f5d4dc 1754 if (flags & MNT_FORCE && !capable(CAP_SYS_ADMIN))
25ccd24f
CH
1755 return -EPERM;
1756 return 0;
1757}
1758
a0a6df9a 1759// caller is responsible for flags being sane
25ccd24f
CH
1760int path_umount(struct path *path, int flags)
1761{
1762 struct mount *mnt = real_mount(path->mnt);
1763 int ret;
1764
1765 ret = can_umount(path, flags);
1766 if (!ret)
1767 ret = do_umount(mnt, flags);
1da177e4 1768
429731b1 1769 /* we mustn't call path_put() as that would clear mnt_expiry_mark */
41525f56 1770 dput(path->dentry);
900148dc 1771 mntput_no_expire(mnt);
25ccd24f 1772 return ret;
1da177e4
LT
1773}
1774
09267def 1775static int ksys_umount(char __user *name, int flags)
41525f56
CH
1776{
1777 int lookup_flags = LOOKUP_MOUNTPOINT;
1778 struct path path;
1779 int ret;
1780
a0a6df9a
AV
1781 // basic validity checks done first
1782 if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
1783 return -EINVAL;
1784
41525f56
CH
1785 if (!(flags & UMOUNT_NOFOLLOW))
1786 lookup_flags |= LOOKUP_FOLLOW;
1787 ret = user_path_at(AT_FDCWD, name, lookup_flags, &path);
1788 if (ret)
1789 return ret;
1790 return path_umount(&path, flags);
1791}
1792
3a18ef5c
DB
1793SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
1794{
1795 return ksys_umount(name, flags);
1796}
1797
1da177e4
LT
1798#ifdef __ARCH_WANT_SYS_OLDUMOUNT
1799
1800/*
b58fed8b 1801 * The 2.0 compatible umount. No flags.
1da177e4 1802 */
bdc480e3 1803SYSCALL_DEFINE1(oldumount, char __user *, name)
1da177e4 1804{
3a18ef5c 1805 return ksys_umount(name, 0);
1da177e4
LT
1806}
1807
1808#endif
1809
4ce5d2b1 1810static bool is_mnt_ns_file(struct dentry *dentry)
8823c079 1811{
4ce5d2b1 1812 /* Is this a proxy for a mount namespace? */
e149ed2b
AV
1813 return dentry->d_op == &ns_dentry_operations &&
1814 dentry->d_fsdata == &mntns_operations;
4ce5d2b1
EB
1815}
1816
213921f9 1817static struct mnt_namespace *to_mnt_ns(struct ns_common *ns)
58be2825
AV
1818{
1819 return container_of(ns, struct mnt_namespace, ns);
1820}
1821
303cc571
CB
1822struct ns_common *from_mnt_ns(struct mnt_namespace *mnt)
1823{
1824 return &mnt->ns;
1825}
1826
4ce5d2b1
EB
1827static bool mnt_ns_loop(struct dentry *dentry)
1828{
1829 /* Could bind mounting the mount namespace inode cause a
1830 * mount namespace loop?
1831 */
1832 struct mnt_namespace *mnt_ns;
1833 if (!is_mnt_ns_file(dentry))
1834 return false;
1835
f77c8014 1836 mnt_ns = to_mnt_ns(get_proc_ns(dentry->d_inode));
8823c079
EB
1837 return current->nsproxy->mnt_ns->seq >= mnt_ns->seq;
1838}
1839
87129cc0 1840struct mount *copy_tree(struct mount *mnt, struct dentry *dentry,
36341f64 1841 int flag)
1da177e4 1842{
84d17192 1843 struct mount *res, *p, *q, *r, *parent;
1da177e4 1844
4ce5d2b1
EB
1845 if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt))
1846 return ERR_PTR(-EINVAL);
1847
1848 if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry))
be34d1a3 1849 return ERR_PTR(-EINVAL);
9676f0c6 1850
36341f64 1851 res = q = clone_mnt(mnt, dentry, flag);
be34d1a3
DH
1852 if (IS_ERR(q))
1853 return q;
1854
a73324da 1855 q->mnt_mountpoint = mnt->mnt_mountpoint;
1da177e4
LT
1856
1857 p = mnt;
6b41d536 1858 list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
315fc83e 1859 struct mount *s;
7ec02ef1 1860 if (!is_subdir(r->mnt_mountpoint, dentry))
1da177e4
LT
1861 continue;
1862
909b0a88 1863 for (s = r; s; s = next_mnt(s, r)) {
4ce5d2b1
EB
1864 if (!(flag & CL_COPY_UNBINDABLE) &&
1865 IS_MNT_UNBINDABLE(s)) {
df7342b2
EB
1866 if (s->mnt.mnt_flags & MNT_LOCKED) {
1867 /* Both unbindable and locked. */
1868 q = ERR_PTR(-EPERM);
1869 goto out;
1870 } else {
1871 s = skip_mnt_tree(s);
1872 continue;
1873 }
4ce5d2b1
EB
1874 }
1875 if (!(flag & CL_COPY_MNT_NS_FILE) &&
1876 is_mnt_ns_file(s->mnt.mnt_root)) {
9676f0c6
RP
1877 s = skip_mnt_tree(s);
1878 continue;
1879 }
0714a533
AV
1880 while (p != s->mnt_parent) {
1881 p = p->mnt_parent;
1882 q = q->mnt_parent;
1da177e4 1883 }
87129cc0 1884 p = s;
84d17192 1885 parent = q;
87129cc0 1886 q = clone_mnt(p, p->mnt.mnt_root, flag);
be34d1a3
DH
1887 if (IS_ERR(q))
1888 goto out;
719ea2fb 1889 lock_mount_hash();
1a4eeaf2 1890 list_add_tail(&q->mnt_list, &res->mnt_list);
1064f874 1891 attach_mnt(q, parent, p->mnt_mp);
719ea2fb 1892 unlock_mount_hash();
1da177e4
LT
1893 }
1894 }
1895 return res;
be34d1a3 1896out:
1da177e4 1897 if (res) {
719ea2fb 1898 lock_mount_hash();
e819f152 1899 umount_tree(res, UMOUNT_SYNC);
719ea2fb 1900 unlock_mount_hash();
1da177e4 1901 }
be34d1a3 1902 return q;
1da177e4
LT
1903}
1904
be34d1a3
DH
1905/* Caller should check returned pointer for errors */
1906
ca71cf71 1907struct vfsmount *collect_mounts(const struct path *path)
8aec0809 1908{
cb338d06 1909 struct mount *tree;
97216be0 1910 namespace_lock();
cd4a4017
EB
1911 if (!check_mnt(real_mount(path->mnt)))
1912 tree = ERR_PTR(-EINVAL);
1913 else
1914 tree = copy_tree(real_mount(path->mnt), path->dentry,
1915 CL_COPY_ALL | CL_PRIVATE);
328e6d90 1916 namespace_unlock();
be34d1a3 1917 if (IS_ERR(tree))
52e220d3 1918 return ERR_CAST(tree);
be34d1a3 1919 return &tree->mnt;
8aec0809
AV
1920}
1921
a07b2000
AV
1922static void free_mnt_ns(struct mnt_namespace *);
1923static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *, bool);
1924
1925void dissolve_on_fput(struct vfsmount *mnt)
1926{
1927 struct mnt_namespace *ns;
1928 namespace_lock();
1929 lock_mount_hash();
1930 ns = real_mount(mnt)->mnt_ns;
44dfd84a
DH
1931 if (ns) {
1932 if (is_anon_ns(ns))
1933 umount_tree(real_mount(mnt), UMOUNT_CONNECTED);
1934 else
1935 ns = NULL;
1936 }
a07b2000
AV
1937 unlock_mount_hash();
1938 namespace_unlock();
44dfd84a
DH
1939 if (ns)
1940 free_mnt_ns(ns);
a07b2000
AV
1941}
1942
8aec0809
AV
1943void drop_collected_mounts(struct vfsmount *mnt)
1944{
97216be0 1945 namespace_lock();
719ea2fb 1946 lock_mount_hash();
9c8e0a1b 1947 umount_tree(real_mount(mnt), 0);
719ea2fb 1948 unlock_mount_hash();
3ab6abee 1949 namespace_unlock();
8aec0809
AV
1950}
1951
427215d8
MS
1952static bool has_locked_children(struct mount *mnt, struct dentry *dentry)
1953{
1954 struct mount *child;
1955
1956 list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
1957 if (!is_subdir(child->mnt_mountpoint, dentry))
1958 continue;
1959
1960 if (child->mnt.mnt_flags & MNT_LOCKED)
1961 return true;
1962 }
1963 return false;
1964}
1965
c771d683
MS
1966/**
1967 * clone_private_mount - create a private clone of a path
1f287bc4 1968 * @path: path to clone
c771d683 1969 *
1f287bc4
RD
1970 * This creates a new vfsmount, which will be the clone of @path. The new mount
1971 * will not be attached anywhere in the namespace and will be private (i.e.
1972 * changes to the originating mount won't be propagated into this).
c771d683
MS
1973 *
1974 * Release with mntput().
1975 */
ca71cf71 1976struct vfsmount *clone_private_mount(const struct path *path)
c771d683
MS
1977{
1978 struct mount *old_mnt = real_mount(path->mnt);
1979 struct mount *new_mnt;
1980
427215d8 1981 down_read(&namespace_sem);
c771d683 1982 if (IS_MNT_UNBINDABLE(old_mnt))
427215d8
MS
1983 goto invalid;
1984
1985 if (!check_mnt(old_mnt))
1986 goto invalid;
1987
1988 if (has_locked_children(old_mnt, path->dentry))
1989 goto invalid;
c771d683 1990
c771d683 1991 new_mnt = clone_mnt(old_mnt, path->dentry, CL_PRIVATE);
427215d8
MS
1992 up_read(&namespace_sem);
1993
c771d683
MS
1994 if (IS_ERR(new_mnt))
1995 return ERR_CAST(new_mnt);
1996
df820f8d
MS
1997 /* Longterm mount to be removed by kern_unmount*() */
1998 new_mnt->mnt_ns = MNT_NS_INTERNAL;
1999
c771d683 2000 return &new_mnt->mnt;
427215d8
MS
2001
2002invalid:
2003 up_read(&namespace_sem);
2004 return ERR_PTR(-EINVAL);
c771d683
MS
2005}
2006EXPORT_SYMBOL_GPL(clone_private_mount);
2007
1f707137
AV
2008int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
2009 struct vfsmount *root)
2010{
1a4eeaf2 2011 struct mount *mnt;
1f707137
AV
2012 int res = f(root, arg);
2013 if (res)
2014 return res;
1a4eeaf2
AV
2015 list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) {
2016 res = f(&mnt->mnt, arg);
1f707137
AV
2017 if (res)
2018 return res;
2019 }
2020 return 0;
2021}
7fea21ae 2022EXPORT_SYMBOL_GPL(iterate_mounts);
1f707137 2023
3bd045cc
AV
2024static void lock_mnt_tree(struct mount *mnt)
2025{
2026 struct mount *p;
2027
2028 for (p = mnt; p; p = next_mnt(p, mnt)) {
2029 int flags = p->mnt.mnt_flags;
2030 /* Don't allow unprivileged users to change mount flags */
2031 flags |= MNT_LOCK_ATIME;
2032
2033 if (flags & MNT_READONLY)
2034 flags |= MNT_LOCK_READONLY;
2035
2036 if (flags & MNT_NODEV)
2037 flags |= MNT_LOCK_NODEV;
2038
2039 if (flags & MNT_NOSUID)
2040 flags |= MNT_LOCK_NOSUID;
2041
2042 if (flags & MNT_NOEXEC)
2043 flags |= MNT_LOCK_NOEXEC;
2044 /* Don't allow unprivileged users to reveal what is under a mount */
2045 if (list_empty(&p->mnt_expire))
2046 flags |= MNT_LOCKED;
2047 p->mnt.mnt_flags = flags;
2048 }
2049}
2050
4b8b21f4 2051static void cleanup_group_ids(struct mount *mnt, struct mount *end)
719f5d7f 2052{
315fc83e 2053 struct mount *p;
719f5d7f 2054
909b0a88 2055 for (p = mnt; p != end; p = next_mnt(p, mnt)) {
fc7be130 2056 if (p->mnt_group_id && !IS_MNT_SHARED(p))
4b8b21f4 2057 mnt_release_group_id(p);
719f5d7f
MS
2058 }
2059}
2060
4b8b21f4 2061static int invent_group_ids(struct mount *mnt, bool recurse)
719f5d7f 2062{
315fc83e 2063 struct mount *p;
719f5d7f 2064
909b0a88 2065 for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
fc7be130 2066 if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
4b8b21f4 2067 int err = mnt_alloc_group_id(p);
719f5d7f 2068 if (err) {
4b8b21f4 2069 cleanup_group_ids(mnt, p);
719f5d7f
MS
2070 return err;
2071 }
2072 }
2073 }
2074
2075 return 0;
2076}
2077
d2921684
EB
2078int count_mounts(struct mnt_namespace *ns, struct mount *mnt)
2079{
2080 unsigned int max = READ_ONCE(sysctl_mount_max);
2081 unsigned int mounts = 0, old, pending, sum;
2082 struct mount *p;
2083
2084 for (p = mnt; p; p = next_mnt(p, mnt))
2085 mounts++;
2086
2087 old = ns->mounts;
2088 pending = ns->pending_mounts;
2089 sum = old + pending;
2090 if ((old > sum) ||
2091 (pending > sum) ||
2092 (max < sum) ||
2093 (mounts > (max - sum)))
2094 return -ENOSPC;
2095
2096 ns->pending_mounts = pending + mounts;
2097 return 0;
2098}
2099
b90fa9ae
RP
2100/*
2101 * @source_mnt : mount tree to be attached
21444403
RP
2102 * @nd : place the mount tree @source_mnt is attached
2103 * @parent_nd : if non-null, detach the source_mnt from its parent and
2104 * store the parent mount and mountpoint dentry.
2105 * (done when source_mnt is moved)
b90fa9ae
RP
2106 *
2107 * NOTE: in the table below explains the semantics when a source mount
2108 * of a given type is attached to a destination mount of a given type.
9676f0c6
RP
2109 * ---------------------------------------------------------------------------
2110 * | BIND MOUNT OPERATION |
2111 * |**************************************************************************
2112 * | source-->| shared | private | slave | unbindable |
2113 * | dest | | | | |
2114 * | | | | | | |
2115 * | v | | | | |
2116 * |**************************************************************************
2117 * | shared | shared (++) | shared (+) | shared(+++)| invalid |
2118 * | | | | | |
2119 * |non-shared| shared (+) | private | slave (*) | invalid |
2120 * ***************************************************************************
b90fa9ae
RP
2121 * A bind operation clones the source mount and mounts the clone on the
2122 * destination mount.
2123 *
2124 * (++) the cloned mount is propagated to all the mounts in the propagation
2125 * tree of the destination mount and the cloned mount is added to
2126 * the peer group of the source mount.
2127 * (+) the cloned mount is created under the destination mount and is marked
2128 * as shared. The cloned mount is added to the peer group of the source
2129 * mount.
5afe0022
RP
2130 * (+++) the mount is propagated to all the mounts in the propagation tree
2131 * of the destination mount and the cloned mount is made slave
2132 * of the same master as that of the source mount. The cloned mount
2133 * is marked as 'shared and slave'.
2134 * (*) the cloned mount is made a slave of the same master as that of the
2135 * source mount.
2136 *
9676f0c6
RP
2137 * ---------------------------------------------------------------------------
2138 * | MOVE MOUNT OPERATION |
2139 * |**************************************************************************
2140 * | source-->| shared | private | slave | unbindable |
2141 * | dest | | | | |
2142 * | | | | | | |
2143 * | v | | | | |
2144 * |**************************************************************************
2145 * | shared | shared (+) | shared (+) | shared(+++) | invalid |
2146 * | | | | | |
2147 * |non-shared| shared (+*) | private | slave (*) | unbindable |
2148 * ***************************************************************************
5afe0022
RP
2149 *
2150 * (+) the mount is moved to the destination. And is then propagated to
2151 * all the mounts in the propagation tree of the destination mount.
21444403 2152 * (+*) the mount is moved to the destination.
5afe0022
RP
2153 * (+++) the mount is moved to the destination and is then propagated to
2154 * all the mounts belonging to the destination mount's propagation tree.
2155 * the mount is marked as 'shared and slave'.
2156 * (*) the mount continues to be a slave at the new location.
b90fa9ae
RP
2157 *
2158 * if the source mount is a tree, the operations explained above is
2159 * applied to each mount in the tree.
2160 * Must be called without spinlocks held, since this function can sleep
2161 * in allocations.
2162 */
0fb54e50 2163static int attach_recursive_mnt(struct mount *source_mnt,
84d17192
AV
2164 struct mount *dest_mnt,
2165 struct mountpoint *dest_mp,
2763d119 2166 bool moving)
b90fa9ae 2167{
3bd045cc 2168 struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns;
38129a13 2169 HLIST_HEAD(tree_list);
d2921684 2170 struct mnt_namespace *ns = dest_mnt->mnt_ns;
1064f874 2171 struct mountpoint *smp;
315fc83e 2172 struct mount *child, *p;
38129a13 2173 struct hlist_node *n;
719f5d7f 2174 int err;
b90fa9ae 2175
1064f874
EB
2176 /* Preallocate a mountpoint in case the new mounts need
2177 * to be tucked under other mounts.
2178 */
2179 smp = get_mountpoint(source_mnt->mnt.mnt_root);
2180 if (IS_ERR(smp))
2181 return PTR_ERR(smp);
2182
d2921684 2183 /* Is there space to add these mounts to the mount namespace? */
2763d119 2184 if (!moving) {
d2921684
EB
2185 err = count_mounts(ns, source_mnt);
2186 if (err)
2187 goto out;
2188 }
2189
fc7be130 2190 if (IS_MNT_SHARED(dest_mnt)) {
0fb54e50 2191 err = invent_group_ids(source_mnt, true);
719f5d7f
MS
2192 if (err)
2193 goto out;
0b1b901b 2194 err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list);
f2ebb3a9 2195 lock_mount_hash();
0b1b901b
AV
2196 if (err)
2197 goto out_cleanup_ids;
909b0a88 2198 for (p = source_mnt; p; p = next_mnt(p, source_mnt))
0f0afb1d 2199 set_mnt_shared(p);
0b1b901b
AV
2200 } else {
2201 lock_mount_hash();
b90fa9ae 2202 }
2763d119
AV
2203 if (moving) {
2204 unhash_mnt(source_mnt);
84d17192 2205 attach_mnt(source_mnt, dest_mnt, dest_mp);
143c8c91 2206 touch_mnt_namespace(source_mnt->mnt_ns);
21444403 2207 } else {
44dfd84a
DH
2208 if (source_mnt->mnt_ns) {
2209 /* move from anon - the caller will destroy */
2210 list_del_init(&source_mnt->mnt_ns->list);
2211 }
84d17192 2212 mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt);
1064f874 2213 commit_tree(source_mnt);
21444403 2214 }
b90fa9ae 2215
38129a13 2216 hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) {
1d6a32ac 2217 struct mount *q;
38129a13 2218 hlist_del_init(&child->mnt_hash);
1064f874
EB
2219 q = __lookup_mnt(&child->mnt_parent->mnt,
2220 child->mnt_mountpoint);
2221 if (q)
2222 mnt_change_mountpoint(child, smp, q);
3bd045cc
AV
2223 /* Notice when we are propagating across user namespaces */
2224 if (child->mnt_parent->mnt_ns->user_ns != user_ns)
2225 lock_mnt_tree(child);
d728cf79 2226 child->mnt.mnt_flags &= ~MNT_LOCKED;
1064f874 2227 commit_tree(child);
b90fa9ae 2228 }
1064f874 2229 put_mountpoint(smp);
719ea2fb 2230 unlock_mount_hash();
99b7db7b 2231
b90fa9ae 2232 return 0;
719f5d7f
MS
2233
2234 out_cleanup_ids:
f2ebb3a9
AV
2235 while (!hlist_empty(&tree_list)) {
2236 child = hlist_entry(tree_list.first, struct mount, mnt_hash);
d2921684 2237 child->mnt_parent->mnt_ns->pending_mounts = 0;
e819f152 2238 umount_tree(child, UMOUNT_SYNC);
f2ebb3a9
AV
2239 }
2240 unlock_mount_hash();
0b1b901b 2241 cleanup_group_ids(source_mnt, NULL);
719f5d7f 2242 out:
d2921684 2243 ns->pending_mounts = 0;
1064f874
EB
2244
2245 read_seqlock_excl(&mount_lock);
2246 put_mountpoint(smp);
2247 read_sequnlock_excl(&mount_lock);
2248
719f5d7f 2249 return err;
b90fa9ae
RP
2250}
2251
84d17192 2252static struct mountpoint *lock_mount(struct path *path)
b12cea91
AV
2253{
2254 struct vfsmount *mnt;
84d17192 2255 struct dentry *dentry = path->dentry;
b12cea91 2256retry:
5955102c 2257 inode_lock(dentry->d_inode);
84d17192 2258 if (unlikely(cant_mount(dentry))) {
5955102c 2259 inode_unlock(dentry->d_inode);
84d17192 2260 return ERR_PTR(-ENOENT);
b12cea91 2261 }
97216be0 2262 namespace_lock();
b12cea91 2263 mnt = lookup_mnt(path);
84d17192 2264 if (likely(!mnt)) {
3895dbf8 2265 struct mountpoint *mp = get_mountpoint(dentry);
84d17192 2266 if (IS_ERR(mp)) {
97216be0 2267 namespace_unlock();
5955102c 2268 inode_unlock(dentry->d_inode);
84d17192
AV
2269 return mp;
2270 }
2271 return mp;
2272 }
97216be0 2273 namespace_unlock();
5955102c 2274 inode_unlock(path->dentry->d_inode);
b12cea91
AV
2275 path_put(path);
2276 path->mnt = mnt;
84d17192 2277 dentry = path->dentry = dget(mnt->mnt_root);
b12cea91
AV
2278 goto retry;
2279}
2280
84d17192 2281static void unlock_mount(struct mountpoint *where)
b12cea91 2282{
84d17192 2283 struct dentry *dentry = where->m_dentry;
3895dbf8
EB
2284
2285 read_seqlock_excl(&mount_lock);
84d17192 2286 put_mountpoint(where);
3895dbf8
EB
2287 read_sequnlock_excl(&mount_lock);
2288
328e6d90 2289 namespace_unlock();
5955102c 2290 inode_unlock(dentry->d_inode);
b12cea91
AV
2291}
2292
84d17192 2293static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp)
1da177e4 2294{
e462ec50 2295 if (mnt->mnt.mnt_sb->s_flags & SB_NOUSER)
1da177e4
LT
2296 return -EINVAL;
2297
e36cb0b8
DH
2298 if (d_is_dir(mp->m_dentry) !=
2299 d_is_dir(mnt->mnt.mnt_root))
1da177e4
LT
2300 return -ENOTDIR;
2301
2763d119 2302 return attach_recursive_mnt(mnt, p, mp, false);
1da177e4
LT
2303}
2304
7a2e8a8f
VA
2305/*
2306 * Sanity check the flags to change_mnt_propagation.
2307 */
2308
e462ec50 2309static int flags_to_propagation_type(int ms_flags)
7a2e8a8f 2310{
e462ec50 2311 int type = ms_flags & ~(MS_REC | MS_SILENT);
7a2e8a8f
VA
2312
2313 /* Fail if any non-propagation flags are set */
2314 if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
2315 return 0;
2316 /* Only one propagation flag should be set */
2317 if (!is_power_of_2(type))
2318 return 0;
2319 return type;
2320}
2321
07b20889
RP
2322/*
2323 * recursively change the type of the mountpoint.
2324 */
e462ec50 2325static int do_change_type(struct path *path, int ms_flags)
07b20889 2326{
315fc83e 2327 struct mount *m;
4b8b21f4 2328 struct mount *mnt = real_mount(path->mnt);
e462ec50 2329 int recurse = ms_flags & MS_REC;
7a2e8a8f 2330 int type;
719f5d7f 2331 int err = 0;
07b20889 2332
2d92ab3c 2333 if (path->dentry != path->mnt->mnt_root)
07b20889
RP
2334 return -EINVAL;
2335
e462ec50 2336 type = flags_to_propagation_type(ms_flags);
7a2e8a8f
VA
2337 if (!type)
2338 return -EINVAL;
2339
97216be0 2340 namespace_lock();
719f5d7f
MS
2341 if (type == MS_SHARED) {
2342 err = invent_group_ids(mnt, recurse);
2343 if (err)
2344 goto out_unlock;
2345 }
2346
719ea2fb 2347 lock_mount_hash();
909b0a88 2348 for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
0f0afb1d 2349 change_mnt_propagation(m, type);
719ea2fb 2350 unlock_mount_hash();
719f5d7f
MS
2351
2352 out_unlock:
97216be0 2353 namespace_unlock();
719f5d7f 2354 return err;
07b20889
RP
2355}
2356
a07b2000
AV
2357static struct mount *__do_loopback(struct path *old_path, int recurse)
2358{
2359 struct mount *mnt = ERR_PTR(-EINVAL), *old = real_mount(old_path->mnt);
2360
2361 if (IS_MNT_UNBINDABLE(old))
2362 return mnt;
2363
2364 if (!check_mnt(old) && old_path->dentry->d_op != &ns_dentry_operations)
2365 return mnt;
2366
2367 if (!recurse && has_locked_children(old, old_path->dentry))
2368 return mnt;
2369
2370 if (recurse)
2371 mnt = copy_tree(old, old_path->dentry, CL_COPY_MNT_NS_FILE);
2372 else
2373 mnt = clone_mnt(old, old_path->dentry, 0);
2374
2375 if (!IS_ERR(mnt))
2376 mnt->mnt.mnt_flags &= ~MNT_LOCKED;
2377
2378 return mnt;
2379}
2380
1da177e4
LT
2381/*
2382 * do loopback mount.
2383 */
808d4e3c 2384static int do_loopback(struct path *path, const char *old_name,
2dafe1c4 2385 int recurse)
1da177e4 2386{
2d92ab3c 2387 struct path old_path;
a07b2000 2388 struct mount *mnt = NULL, *parent;
84d17192 2389 struct mountpoint *mp;
57eccb83 2390 int err;
1da177e4
LT
2391 if (!old_name || !*old_name)
2392 return -EINVAL;
815d405c 2393 err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
1da177e4
LT
2394 if (err)
2395 return err;
2396
8823c079 2397 err = -EINVAL;
4ce5d2b1 2398 if (mnt_ns_loop(old_path.dentry))
dd111b31 2399 goto out;
8823c079 2400
84d17192 2401 mp = lock_mount(path);
a07b2000
AV
2402 if (IS_ERR(mp)) {
2403 err = PTR_ERR(mp);
b12cea91 2404 goto out;
a07b2000 2405 }
b12cea91 2406
84d17192 2407 parent = real_mount(path->mnt);
e149ed2b
AV
2408 if (!check_mnt(parent))
2409 goto out2;
2410
a07b2000 2411 mnt = __do_loopback(&old_path, recurse);
be34d1a3
DH
2412 if (IS_ERR(mnt)) {
2413 err = PTR_ERR(mnt);
e9c5d8a5 2414 goto out2;
be34d1a3 2415 }
ccd48bc7 2416
84d17192 2417 err = graft_tree(mnt, parent, mp);
ccd48bc7 2418 if (err) {
719ea2fb 2419 lock_mount_hash();
e819f152 2420 umount_tree(mnt, UMOUNT_SYNC);
719ea2fb 2421 unlock_mount_hash();
5b83d2c5 2422 }
b12cea91 2423out2:
84d17192 2424 unlock_mount(mp);
ccd48bc7 2425out:
2d92ab3c 2426 path_put(&old_path);
1da177e4
LT
2427 return err;
2428}
2429
a07b2000
AV
2430static struct file *open_detached_copy(struct path *path, bool recursive)
2431{
2432 struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns;
2433 struct mnt_namespace *ns = alloc_mnt_ns(user_ns, true);
2434 struct mount *mnt, *p;
2435 struct file *file;
2436
2437 if (IS_ERR(ns))
2438 return ERR_CAST(ns);
2439
2440 namespace_lock();
2441 mnt = __do_loopback(path, recursive);
2442 if (IS_ERR(mnt)) {
2443 namespace_unlock();
2444 free_mnt_ns(ns);
2445 return ERR_CAST(mnt);
2446 }
2447
2448 lock_mount_hash();
2449 for (p = mnt; p; p = next_mnt(p, mnt)) {
2450 p->mnt_ns = ns;
2451 ns->mounts++;
2452 }
2453 ns->root = mnt;
2454 list_add_tail(&ns->list, &mnt->mnt_list);
2455 mntget(&mnt->mnt);
2456 unlock_mount_hash();
2457 namespace_unlock();
2458
2459 mntput(path->mnt);
2460 path->mnt = &mnt->mnt;
2461 file = dentry_open(path, O_PATH, current_cred());
2462 if (IS_ERR(file))
2463 dissolve_on_fput(path->mnt);
2464 else
2465 file->f_mode |= FMODE_NEED_UNMOUNT;
2466 return file;
2467}
2468
2658ce09 2469SYSCALL_DEFINE3(open_tree, int, dfd, const char __user *, filename, unsigned, flags)
a07b2000
AV
2470{
2471 struct file *file;
2472 struct path path;
2473 int lookup_flags = LOOKUP_AUTOMOUNT | LOOKUP_FOLLOW;
2474 bool detached = flags & OPEN_TREE_CLONE;
2475 int error;
2476 int fd;
2477
2478 BUILD_BUG_ON(OPEN_TREE_CLOEXEC != O_CLOEXEC);
2479
2480 if (flags & ~(AT_EMPTY_PATH | AT_NO_AUTOMOUNT | AT_RECURSIVE |
2481 AT_SYMLINK_NOFOLLOW | OPEN_TREE_CLONE |
2482 OPEN_TREE_CLOEXEC))
2483 return -EINVAL;
2484
2485 if ((flags & (AT_RECURSIVE | OPEN_TREE_CLONE)) == AT_RECURSIVE)
2486 return -EINVAL;
2487
2488 if (flags & AT_NO_AUTOMOUNT)
2489 lookup_flags &= ~LOOKUP_AUTOMOUNT;
2490 if (flags & AT_SYMLINK_NOFOLLOW)
2491 lookup_flags &= ~LOOKUP_FOLLOW;
2492 if (flags & AT_EMPTY_PATH)
2493 lookup_flags |= LOOKUP_EMPTY;
2494
2495 if (detached && !may_mount())
2496 return -EPERM;
2497
2498 fd = get_unused_fd_flags(flags & O_CLOEXEC);
2499 if (fd < 0)
2500 return fd;
2501
2502 error = user_path_at(dfd, filename, lookup_flags, &path);
2503 if (unlikely(error)) {
2504 file = ERR_PTR(error);
2505 } else {
2506 if (detached)
2507 file = open_detached_copy(&path, flags & AT_RECURSIVE);
2508 else
2509 file = dentry_open(&path, O_PATH, current_cred());
2510 path_put(&path);
2511 }
2512 if (IS_ERR(file)) {
2513 put_unused_fd(fd);
2514 return PTR_ERR(file);
2515 }
2516 fd_install(fd, file);
2517 return fd;
2518}
2519
43f5e655
DH
2520/*
2521 * Don't allow locked mount flags to be cleared.
2522 *
2523 * No locks need to be held here while testing the various MNT_LOCK
2524 * flags because those flags can never be cleared once they are set.
2525 */
2526static bool can_change_locked_flags(struct mount *mnt, unsigned int mnt_flags)
2e4b7fcd 2527{
43f5e655
DH
2528 unsigned int fl = mnt->mnt.mnt_flags;
2529
2530 if ((fl & MNT_LOCK_READONLY) &&
2531 !(mnt_flags & MNT_READONLY))
2532 return false;
2533
2534 if ((fl & MNT_LOCK_NODEV) &&
2535 !(mnt_flags & MNT_NODEV))
2536 return false;
2537
2538 if ((fl & MNT_LOCK_NOSUID) &&
2539 !(mnt_flags & MNT_NOSUID))
2540 return false;
2541
2542 if ((fl & MNT_LOCK_NOEXEC) &&
2543 !(mnt_flags & MNT_NOEXEC))
2544 return false;
2545
2546 if ((fl & MNT_LOCK_ATIME) &&
2547 ((fl & MNT_ATIME_MASK) != (mnt_flags & MNT_ATIME_MASK)))
2548 return false;
2e4b7fcd 2549
43f5e655
DH
2550 return true;
2551}
2552
2553static int change_mount_ro_state(struct mount *mnt, unsigned int mnt_flags)
2e4b7fcd 2554{
43f5e655 2555 bool readonly_request = (mnt_flags & MNT_READONLY);
2e4b7fcd 2556
43f5e655 2557 if (readonly_request == __mnt_is_readonly(&mnt->mnt))
2e4b7fcd
DH
2558 return 0;
2559
2560 if (readonly_request)
43f5e655
DH
2561 return mnt_make_readonly(mnt);
2562
68847c94
CB
2563 mnt->mnt.mnt_flags &= ~MNT_READONLY;
2564 return 0;
43f5e655
DH
2565}
2566
43f5e655
DH
2567static void set_mount_attributes(struct mount *mnt, unsigned int mnt_flags)
2568{
43f5e655
DH
2569 mnt_flags |= mnt->mnt.mnt_flags & ~MNT_USER_SETTABLE_MASK;
2570 mnt->mnt.mnt_flags = mnt_flags;
2571 touch_mnt_namespace(mnt->mnt_ns);
43f5e655
DH
2572}
2573
f8b92ba6
DD
2574static void mnt_warn_timestamp_expiry(struct path *mountpoint, struct vfsmount *mnt)
2575{
2576 struct super_block *sb = mnt->mnt_sb;
2577
2578 if (!__mnt_is_readonly(mnt) &&
2579 (ktime_get_real_seconds() + TIME_UPTIME_SEC_MAX > sb->s_time_max)) {
2580 char *buf = (char *)__get_free_page(GFP_KERNEL);
2581 char *mntpath = buf ? d_path(mountpoint, buf, PAGE_SIZE) : ERR_PTR(-ENOMEM);
2582 struct tm tm;
2583
2584 time64_to_tm(sb->s_time_max, 0, &tm);
2585
0ecee669
EB
2586 pr_warn("%s filesystem being %s at %s supports timestamps until %04ld (0x%llx)\n",
2587 sb->s_type->name,
2588 is_mounted(mnt) ? "remounted" : "mounted",
2589 mntpath,
f8b92ba6
DD
2590 tm.tm_year+1900, (unsigned long long)sb->s_time_max);
2591
2592 free_page((unsigned long)buf);
2593 }
2594}
2595
43f5e655
DH
2596/*
2597 * Handle reconfiguration of the mountpoint only without alteration of the
2598 * superblock it refers to. This is triggered by specifying MS_REMOUNT|MS_BIND
2599 * to mount(2).
2600 */
2601static int do_reconfigure_mnt(struct path *path, unsigned int mnt_flags)
2602{
2603 struct super_block *sb = path->mnt->mnt_sb;
2604 struct mount *mnt = real_mount(path->mnt);
2605 int ret;
2606
2607 if (!check_mnt(mnt))
2608 return -EINVAL;
2609
2610 if (path->dentry != mnt->mnt.mnt_root)
2611 return -EINVAL;
2612
2613 if (!can_change_locked_flags(mnt, mnt_flags))
2614 return -EPERM;
2615
e58ace1a
CB
2616 /*
2617 * We're only checking whether the superblock is read-only not
2618 * changing it, so only take down_read(&sb->s_umount).
2619 */
2620 down_read(&sb->s_umount);
68847c94 2621 lock_mount_hash();
43f5e655
DH
2622 ret = change_mount_ro_state(mnt, mnt_flags);
2623 if (ret == 0)
2624 set_mount_attributes(mnt, mnt_flags);
68847c94 2625 unlock_mount_hash();
e58ace1a 2626 up_read(&sb->s_umount);
f8b92ba6
DD
2627
2628 mnt_warn_timestamp_expiry(path, &mnt->mnt);
2629
43f5e655 2630 return ret;
2e4b7fcd
DH
2631}
2632
1da177e4
LT
2633/*
2634 * change filesystem flags. dir should be a physical root of filesystem.
2635 * If you've mounted a non-root directory somewhere and want to do remount
2636 * on it - tough luck.
2637 */
e462ec50
DH
2638static int do_remount(struct path *path, int ms_flags, int sb_flags,
2639 int mnt_flags, void *data)
1da177e4
LT
2640{
2641 int err;
2d92ab3c 2642 struct super_block *sb = path->mnt->mnt_sb;
143c8c91 2643 struct mount *mnt = real_mount(path->mnt);
8d0347f6 2644 struct fs_context *fc;
1da177e4 2645
143c8c91 2646 if (!check_mnt(mnt))
1da177e4
LT
2647 return -EINVAL;
2648
2d92ab3c 2649 if (path->dentry != path->mnt->mnt_root)
1da177e4
LT
2650 return -EINVAL;
2651
43f5e655 2652 if (!can_change_locked_flags(mnt, mnt_flags))
9566d674 2653 return -EPERM;
9566d674 2654
8d0347f6
DH
2655 fc = fs_context_for_reconfigure(path->dentry, sb_flags, MS_RMT_MASK);
2656 if (IS_ERR(fc))
2657 return PTR_ERR(fc);
ff36fe2c 2658
b330966f 2659 fc->oldapi = true;
8d0347f6
DH
2660 err = parse_monolithic_mount_data(fc, data);
2661 if (!err) {
2662 down_write(&sb->s_umount);
2663 err = -EPERM;
2664 if (ns_capable(sb->s_user_ns, CAP_SYS_ADMIN)) {
2665 err = reconfigure_super(fc);
68847c94
CB
2666 if (!err) {
2667 lock_mount_hash();
8d0347f6 2668 set_mount_attributes(mnt, mnt_flags);
68847c94
CB
2669 unlock_mount_hash();
2670 }
8d0347f6
DH
2671 }
2672 up_write(&sb->s_umount);
0e55a7cc 2673 }
f8b92ba6
DD
2674
2675 mnt_warn_timestamp_expiry(path, &mnt->mnt);
2676
8d0347f6 2677 put_fs_context(fc);
1da177e4
LT
2678 return err;
2679}
2680
cbbe362c 2681static inline int tree_contains_unbindable(struct mount *mnt)
9676f0c6 2682{
315fc83e 2683 struct mount *p;
909b0a88 2684 for (p = mnt; p; p = next_mnt(p, mnt)) {
fc7be130 2685 if (IS_MNT_UNBINDABLE(p))
9676f0c6
RP
2686 return 1;
2687 }
2688 return 0;
2689}
2690
44dfd84a
DH
2691/*
2692 * Check that there aren't references to earlier/same mount namespaces in the
2693 * specified subtree. Such references can act as pins for mount namespaces
2694 * that aren't checked by the mount-cycle checking code, thereby allowing
2695 * cycles to be made.
2696 */
2697static bool check_for_nsfs_mounts(struct mount *subtree)
2698{
2699 struct mount *p;
2700 bool ret = false;
2701
2702 lock_mount_hash();
2703 for (p = subtree; p; p = next_mnt(p, subtree))
2704 if (mnt_ns_loop(p->mnt.mnt_root))
2705 goto out;
2706
2707 ret = true;
2708out:
2709 unlock_mount_hash();
2710 return ret;
2711}
2712
9ffb14ef
PT
2713static int do_set_group(struct path *from_path, struct path *to_path)
2714{
2715 struct mount *from, *to;
2716 int err;
2717
2718 from = real_mount(from_path->mnt);
2719 to = real_mount(to_path->mnt);
2720
2721 namespace_lock();
2722
2723 err = -EINVAL;
2724 /* To and From must be mounted */
2725 if (!is_mounted(&from->mnt))
2726 goto out;
2727 if (!is_mounted(&to->mnt))
2728 goto out;
2729
2730 err = -EPERM;
2731 /* We should be allowed to modify mount namespaces of both mounts */
2732 if (!ns_capable(from->mnt_ns->user_ns, CAP_SYS_ADMIN))
2733 goto out;
2734 if (!ns_capable(to->mnt_ns->user_ns, CAP_SYS_ADMIN))
2735 goto out;
2736
2737 err = -EINVAL;
2738 /* To and From paths should be mount roots */
2739 if (from_path->dentry != from_path->mnt->mnt_root)
2740 goto out;
2741 if (to_path->dentry != to_path->mnt->mnt_root)
2742 goto out;
2743
2744 /* Setting sharing groups is only allowed across same superblock */
2745 if (from->mnt.mnt_sb != to->mnt.mnt_sb)
2746 goto out;
2747
2748 /* From mount root should be wider than To mount root */
2749 if (!is_subdir(to->mnt.mnt_root, from->mnt.mnt_root))
2750 goto out;
2751
2752 /* From mount should not have locked children in place of To's root */
2753 if (has_locked_children(from, to->mnt.mnt_root))
2754 goto out;
2755
2756 /* Setting sharing groups is only allowed on private mounts */
2757 if (IS_MNT_SHARED(to) || IS_MNT_SLAVE(to))
2758 goto out;
2759
2760 /* From should not be private */
2761 if (!IS_MNT_SHARED(from) && !IS_MNT_SLAVE(from))
2762 goto out;
2763
2764 if (IS_MNT_SLAVE(from)) {
2765 struct mount *m = from->mnt_master;
2766
2767 list_add(&to->mnt_slave, &m->mnt_slave_list);
2768 to->mnt_master = m;
2769 }
2770
2771 if (IS_MNT_SHARED(from)) {
2772 to->mnt_group_id = from->mnt_group_id;
2773 list_add(&to->mnt_share, &from->mnt_share);
2774 lock_mount_hash();
2775 set_mnt_shared(to);
2776 unlock_mount_hash();
2777 }
2778
2779 err = 0;
2780out:
2781 namespace_unlock();
2782 return err;
2783}
2784
2db154b3 2785static int do_move_mount(struct path *old_path, struct path *new_path)
1da177e4 2786{
44dfd84a 2787 struct mnt_namespace *ns;
676da58d 2788 struct mount *p;
0fb54e50 2789 struct mount *old;
2763d119
AV
2790 struct mount *parent;
2791 struct mountpoint *mp, *old_mp;
57eccb83 2792 int err;
44dfd84a 2793 bool attached;
1da177e4 2794
2db154b3 2795 mp = lock_mount(new_path);
84d17192 2796 if (IS_ERR(mp))
2db154b3 2797 return PTR_ERR(mp);
cc53ce53 2798
2db154b3
DH
2799 old = real_mount(old_path->mnt);
2800 p = real_mount(new_path->mnt);
2763d119 2801 parent = old->mnt_parent;
44dfd84a 2802 attached = mnt_has_parent(old);
2763d119 2803 old_mp = old->mnt_mp;
44dfd84a 2804 ns = old->mnt_ns;
143c8c91 2805
1da177e4 2806 err = -EINVAL;
44dfd84a
DH
2807 /* The mountpoint must be in our namespace. */
2808 if (!check_mnt(p))
2db154b3 2809 goto out;
1da177e4 2810
570d7a98
EB
2811 /* The thing moved must be mounted... */
2812 if (!is_mounted(&old->mnt))
44dfd84a
DH
2813 goto out;
2814
570d7a98
EB
2815 /* ... and either ours or the root of anon namespace */
2816 if (!(attached ? check_mnt(old) : is_anon_ns(ns)))
2db154b3 2817 goto out;
5ff9d8a6 2818
2db154b3
DH
2819 if (old->mnt.mnt_flags & MNT_LOCKED)
2820 goto out;
1da177e4 2821
2db154b3
DH
2822 if (old_path->dentry != old_path->mnt->mnt_root)
2823 goto out;
1da177e4 2824
2db154b3
DH
2825 if (d_is_dir(new_path->dentry) !=
2826 d_is_dir(old_path->dentry))
2827 goto out;
21444403
RP
2828 /*
2829 * Don't move a mount residing in a shared parent.
2830 */
2763d119 2831 if (attached && IS_MNT_SHARED(parent))
2db154b3 2832 goto out;
9676f0c6
RP
2833 /*
2834 * Don't move a mount tree containing unbindable mounts to a destination
2835 * mount which is shared.
2836 */
fc7be130 2837 if (IS_MNT_SHARED(p) && tree_contains_unbindable(old))
2db154b3 2838 goto out;
1da177e4 2839 err = -ELOOP;
44dfd84a
DH
2840 if (!check_for_nsfs_mounts(old))
2841 goto out;
fc7be130 2842 for (; mnt_has_parent(p); p = p->mnt_parent)
676da58d 2843 if (p == old)
2db154b3 2844 goto out;
1da177e4 2845
2db154b3 2846 err = attach_recursive_mnt(old, real_mount(new_path->mnt), mp,
2763d119 2847 attached);
4ac91378 2848 if (err)
2db154b3 2849 goto out;
1da177e4
LT
2850
2851 /* if the mount is moved, it should no longer be expire
2852 * automatically */
6776db3d 2853 list_del_init(&old->mnt_expire);
2763d119
AV
2854 if (attached)
2855 put_mountpoint(old_mp);
1da177e4 2856out:
2db154b3 2857 unlock_mount(mp);
44dfd84a 2858 if (!err) {
2763d119
AV
2859 if (attached)
2860 mntput_no_expire(parent);
2861 else
44dfd84a
DH
2862 free_mnt_ns(ns);
2863 }
2db154b3
DH
2864 return err;
2865}
2866
2867static int do_move_mount_old(struct path *path, const char *old_name)
2868{
2869 struct path old_path;
2870 int err;
2871
2872 if (!old_name || !*old_name)
2873 return -EINVAL;
2874
2875 err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
2876 if (err)
2877 return err;
2878
2879 err = do_move_mount(&old_path, path);
2d92ab3c 2880 path_put(&old_path);
1da177e4
LT
2881 return err;
2882}
2883
9d412a43
AV
2884/*
2885 * add a mount into a namespace's mount tree
2886 */
8f11538e
AV
2887static int do_add_mount(struct mount *newmnt, struct mountpoint *mp,
2888 struct path *path, int mnt_flags)
9d412a43 2889{
8f11538e 2890 struct mount *parent = real_mount(path->mnt);
9d412a43 2891
f2ebb3a9 2892 mnt_flags &= ~MNT_INTERNAL_FLAGS;
9d412a43 2893
84d17192 2894 if (unlikely(!check_mnt(parent))) {
156cacb1
AV
2895 /* that's acceptable only for automounts done in private ns */
2896 if (!(mnt_flags & MNT_SHRINKABLE))
8f11538e 2897 return -EINVAL;
156cacb1 2898 /* ... and for those we'd better have mountpoint still alive */
84d17192 2899 if (!parent->mnt_ns)
8f11538e 2900 return -EINVAL;
156cacb1 2901 }
9d412a43
AV
2902
2903 /* Refuse the same filesystem on the same mount point */
95bc5f25 2904 if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb &&
9d412a43 2905 path->mnt->mnt_root == path->dentry)
8f11538e 2906 return -EBUSY;
9d412a43 2907
e36cb0b8 2908 if (d_is_symlink(newmnt->mnt.mnt_root))
8f11538e 2909 return -EINVAL;
9d412a43 2910
95bc5f25 2911 newmnt->mnt.mnt_flags = mnt_flags;
8f11538e 2912 return graft_tree(newmnt, parent, mp);
9d412a43 2913}
b1e75df4 2914
132e4608
DH
2915static bool mount_too_revealing(const struct super_block *sb, int *new_mnt_flags);
2916
2917/*
2918 * Create a new mount using a superblock configuration and request it
2919 * be added to the namespace tree.
2920 */
2921static int do_new_mount_fc(struct fs_context *fc, struct path *mountpoint,
2922 unsigned int mnt_flags)
2923{
2924 struct vfsmount *mnt;
8f11538e 2925 struct mountpoint *mp;
132e4608
DH
2926 struct super_block *sb = fc->root->d_sb;
2927 int error;
2928
c9ce29ed
AV
2929 error = security_sb_kern_mount(sb);
2930 if (!error && mount_too_revealing(sb, &mnt_flags))
2931 error = -EPERM;
2932
2933 if (unlikely(error)) {
2934 fc_drop_locked(fc);
2935 return error;
132e4608
DH
2936 }
2937
2938 up_write(&sb->s_umount);
2939
2940 mnt = vfs_create_mount(fc);
2941 if (IS_ERR(mnt))
2942 return PTR_ERR(mnt);
2943
f8b92ba6
DD
2944 mnt_warn_timestamp_expiry(mountpoint, mnt);
2945
8f11538e
AV
2946 mp = lock_mount(mountpoint);
2947 if (IS_ERR(mp)) {
2948 mntput(mnt);
2949 return PTR_ERR(mp);
2950 }
2951 error = do_add_mount(real_mount(mnt), mp, mountpoint, mnt_flags);
2952 unlock_mount(mp);
0ecee669
EB
2953 if (error < 0)
2954 mntput(mnt);
132e4608
DH
2955 return error;
2956}
1b852bce 2957
1da177e4
LT
2958/*
2959 * create a new mount for userspace and request it to be added into the
2960 * namespace's tree
2961 */
e462ec50 2962static int do_new_mount(struct path *path, const char *fstype, int sb_flags,
808d4e3c 2963 int mnt_flags, const char *name, void *data)
1da177e4 2964{
0c55cfc4 2965 struct file_system_type *type;
a0c9a8b8
AV
2966 struct fs_context *fc;
2967 const char *subtype = NULL;
2968 int err = 0;
1da177e4 2969
0c55cfc4 2970 if (!fstype)
1da177e4
LT
2971 return -EINVAL;
2972
0c55cfc4
EB
2973 type = get_fs_type(fstype);
2974 if (!type)
2975 return -ENODEV;
2976
a0c9a8b8
AV
2977 if (type->fs_flags & FS_HAS_SUBTYPE) {
2978 subtype = strchr(fstype, '.');
2979 if (subtype) {
2980 subtype++;
2981 if (!*subtype) {
2982 put_filesystem(type);
2983 return -EINVAL;
2984 }
a0c9a8b8
AV
2985 }
2986 }
0c55cfc4 2987
a0c9a8b8 2988 fc = fs_context_for_mount(type, sb_flags);
0c55cfc4 2989 put_filesystem(type);
a0c9a8b8
AV
2990 if (IS_ERR(fc))
2991 return PTR_ERR(fc);
2992
3e1aeb00
DH
2993 if (subtype)
2994 err = vfs_parse_fs_string(fc, "subtype",
2995 subtype, strlen(subtype));
2996 if (!err && name)
2997 err = vfs_parse_fs_string(fc, "source", name, strlen(name));
a0c9a8b8
AV
2998 if (!err)
2999 err = parse_monolithic_mount_data(fc, data);
c3aabf07
AV
3000 if (!err && !mount_capable(fc))
3001 err = -EPERM;
a0c9a8b8
AV
3002 if (!err)
3003 err = vfs_get_tree(fc);
132e4608
DH
3004 if (!err)
3005 err = do_new_mount_fc(fc, path, mnt_flags);
8654df4e 3006
a0c9a8b8 3007 put_fs_context(fc);
15f9a3f3 3008 return err;
1da177e4
LT
3009}
3010
19a167af
AV
3011int finish_automount(struct vfsmount *m, struct path *path)
3012{
26df6034 3013 struct dentry *dentry = path->dentry;
8f11538e 3014 struct mountpoint *mp;
25e195aa 3015 struct mount *mnt;
19a167af 3016 int err;
25e195aa
AV
3017
3018 if (!m)
3019 return 0;
3020 if (IS_ERR(m))
3021 return PTR_ERR(m);
3022
3023 mnt = real_mount(m);
19a167af
AV
3024 /* The new mount record should have at least 2 refs to prevent it being
3025 * expired before we get a chance to add it
3026 */
6776db3d 3027 BUG_ON(mnt_get_count(mnt) < 2);
19a167af
AV
3028
3029 if (m->mnt_sb == path->mnt->mnt_sb &&
26df6034 3030 m->mnt_root == dentry) {
b1e75df4 3031 err = -ELOOP;
26df6034 3032 goto discard;
19a167af
AV
3033 }
3034
26df6034
AV
3035 /*
3036 * we don't want to use lock_mount() - in this case finding something
3037 * that overmounts our mountpoint to be means "quitely drop what we've
3038 * got", not "try to mount it on top".
3039 */
3040 inode_lock(dentry->d_inode);
3041 namespace_lock();
3042 if (unlikely(cant_mount(dentry))) {
3043 err = -ENOENT;
3044 goto discard_locked;
3045 }
3046 rcu_read_lock();
3047 if (unlikely(__lookup_mnt(path->mnt, dentry))) {
3048 rcu_read_unlock();
3049 err = 0;
3050 goto discard_locked;
3051 }
3052 rcu_read_unlock();
3053 mp = get_mountpoint(dentry);
8f11538e
AV
3054 if (IS_ERR(mp)) {
3055 err = PTR_ERR(mp);
26df6034 3056 goto discard_locked;
8f11538e 3057 }
26df6034 3058
8f11538e
AV
3059 err = do_add_mount(mnt, mp, path, path->mnt->mnt_flags | MNT_SHRINKABLE);
3060 unlock_mount(mp);
26df6034
AV
3061 if (unlikely(err))
3062 goto discard;
3063 mntput(m);
3064 return 0;
3065
3066discard_locked:
3067 namespace_unlock();
3068 inode_unlock(dentry->d_inode);
3069discard:
b1e75df4 3070 /* remove m from any expiration list it may be on */
6776db3d 3071 if (!list_empty(&mnt->mnt_expire)) {
97216be0 3072 namespace_lock();
6776db3d 3073 list_del_init(&mnt->mnt_expire);
97216be0 3074 namespace_unlock();
19a167af 3075 }
b1e75df4
AV
3076 mntput(m);
3077 mntput(m);
19a167af
AV
3078 return err;
3079}
3080
ea5b778a
DH
3081/**
3082 * mnt_set_expiry - Put a mount on an expiration list
3083 * @mnt: The mount to list.
3084 * @expiry_list: The list to add the mount to.
3085 */
3086void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
3087{
97216be0 3088 namespace_lock();
ea5b778a 3089
6776db3d 3090 list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list);
ea5b778a 3091
97216be0 3092 namespace_unlock();
ea5b778a
DH
3093}
3094EXPORT_SYMBOL(mnt_set_expiry);
3095
1da177e4
LT
3096/*
3097 * process a list of expirable mountpoints with the intent of discarding any
3098 * mountpoints that aren't in use and haven't been touched since last we came
3099 * here
3100 */
3101void mark_mounts_for_expiry(struct list_head *mounts)
3102{
761d5c38 3103 struct mount *mnt, *next;
1da177e4
LT
3104 LIST_HEAD(graveyard);
3105
3106 if (list_empty(mounts))
3107 return;
3108
97216be0 3109 namespace_lock();
719ea2fb 3110 lock_mount_hash();
1da177e4
LT
3111
3112 /* extract from the expiration list every vfsmount that matches the
3113 * following criteria:
3114 * - only referenced by its parent vfsmount
3115 * - still marked for expiry (marked on the last call here; marks are
3116 * cleared by mntput())
3117 */
6776db3d 3118 list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
863d684f 3119 if (!xchg(&mnt->mnt_expiry_mark, 1) ||
1ab59738 3120 propagate_mount_busy(mnt, 1))
1da177e4 3121 continue;
6776db3d 3122 list_move(&mnt->mnt_expire, &graveyard);
1da177e4 3123 }
bcc5c7d2 3124 while (!list_empty(&graveyard)) {
6776db3d 3125 mnt = list_first_entry(&graveyard, struct mount, mnt_expire);
143c8c91 3126 touch_mnt_namespace(mnt->mnt_ns);
e819f152 3127 umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
bcc5c7d2 3128 }
719ea2fb 3129 unlock_mount_hash();
3ab6abee 3130 namespace_unlock();
5528f911
TM
3131}
3132
3133EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
3134
3135/*
3136 * Ripoff of 'select_parent()'
3137 *
3138 * search the list of submounts for a given mountpoint, and move any
3139 * shrinkable submounts to the 'graveyard' list.
3140 */
692afc31 3141static int select_submounts(struct mount *parent, struct list_head *graveyard)
5528f911 3142{
692afc31 3143 struct mount *this_parent = parent;
5528f911
TM
3144 struct list_head *next;
3145 int found = 0;
3146
3147repeat:
6b41d536 3148 next = this_parent->mnt_mounts.next;
5528f911 3149resume:
6b41d536 3150 while (next != &this_parent->mnt_mounts) {
5528f911 3151 struct list_head *tmp = next;
6b41d536 3152 struct mount *mnt = list_entry(tmp, struct mount, mnt_child);
5528f911
TM
3153
3154 next = tmp->next;
692afc31 3155 if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE))
1da177e4 3156 continue;
5528f911
TM
3157 /*
3158 * Descend a level if the d_mounts list is non-empty.
3159 */
6b41d536 3160 if (!list_empty(&mnt->mnt_mounts)) {
5528f911
TM
3161 this_parent = mnt;
3162 goto repeat;
3163 }
1da177e4 3164
1ab59738 3165 if (!propagate_mount_busy(mnt, 1)) {
6776db3d 3166 list_move_tail(&mnt->mnt_expire, graveyard);
5528f911
TM
3167 found++;
3168 }
1da177e4 3169 }
5528f911
TM
3170 /*
3171 * All done at this level ... ascend and resume the search
3172 */
3173 if (this_parent != parent) {
6b41d536 3174 next = this_parent->mnt_child.next;
0714a533 3175 this_parent = this_parent->mnt_parent;
5528f911
TM
3176 goto resume;
3177 }
3178 return found;
3179}
3180
3181/*
3182 * process a list of expirable mountpoints with the intent of discarding any
3183 * submounts of a specific parent mountpoint
99b7db7b 3184 *
48a066e7 3185 * mount_lock must be held for write
5528f911 3186 */
b54b9be7 3187static void shrink_submounts(struct mount *mnt)
5528f911
TM
3188{
3189 LIST_HEAD(graveyard);
761d5c38 3190 struct mount *m;
5528f911 3191
5528f911 3192 /* extract submounts of 'mountpoint' from the expiration list */
c35038be 3193 while (select_submounts(mnt, &graveyard)) {
bcc5c7d2 3194 while (!list_empty(&graveyard)) {
761d5c38 3195 m = list_first_entry(&graveyard, struct mount,
6776db3d 3196 mnt_expire);
143c8c91 3197 touch_mnt_namespace(m->mnt_ns);
e819f152 3198 umount_tree(m, UMOUNT_PROPAGATE|UMOUNT_SYNC);
bcc5c7d2
AV
3199 }
3200 }
1da177e4
LT
3201}
3202
028abd92 3203static void *copy_mount_options(const void __user * data)
1da177e4 3204{
b40ef869 3205 char *copy;
d563d678 3206 unsigned left, offset;
b58fed8b 3207
1da177e4 3208 if (!data)
b40ef869 3209 return NULL;
1da177e4 3210
b40ef869
AV
3211 copy = kmalloc(PAGE_SIZE, GFP_KERNEL);
3212 if (!copy)
3213 return ERR_PTR(-ENOMEM);
1da177e4 3214
d563d678 3215 left = copy_from_user(copy, data, PAGE_SIZE);
1da177e4 3216
d563d678
CM
3217 /*
3218 * Not all architectures have an exact copy_from_user(). Resort to
3219 * byte at a time.
3220 */
3221 offset = PAGE_SIZE - left;
3222 while (left) {
3223 char c;
3224 if (get_user(c, (const char __user *)data + offset))
3225 break;
3226 copy[offset] = c;
3227 left--;
3228 offset++;
3229 }
3230
3231 if (left == PAGE_SIZE) {
b40ef869
AV
3232 kfree(copy);
3233 return ERR_PTR(-EFAULT);
1da177e4 3234 }
d563d678 3235
b40ef869 3236 return copy;
1da177e4
LT
3237}
3238
028abd92 3239static char *copy_mount_string(const void __user *data)
eca6f534 3240{
fbdb4401 3241 return data ? strndup_user(data, PATH_MAX) : NULL;
eca6f534
VN
3242}
3243
1da177e4
LT
3244/*
3245 * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
3246 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
3247 *
3248 * data is a (void *) that can point to any structure up to
3249 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
3250 * information (or be NULL).
3251 *
3252 * Pre-0.97 versions of mount() didn't have a flags word.
3253 * When the flags word was introduced its top half was required
3254 * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
3255 * Therefore, if this magic number is present, it carries no information
3256 * and must be discarded.
3257 */
c60166f0 3258int path_mount(const char *dev_name, struct path *path,
808d4e3c 3259 const char *type_page, unsigned long flags, void *data_page)
1da177e4 3260{
e462ec50 3261 unsigned int mnt_flags = 0, sb_flags;
a1e6aaa3 3262 int ret;
1da177e4
LT
3263
3264 /* Discard magic */
3265 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
3266 flags &= ~MS_MGC_MSK;
3267
3268 /* Basic sanity checks */
1da177e4
LT
3269 if (data_page)
3270 ((char *)data_page)[PAGE_SIZE - 1] = 0;
3271
e462ec50
DH
3272 if (flags & MS_NOUSER)
3273 return -EINVAL;
3274
a1e6aaa3
CH
3275 ret = security_sb_mount(dev_name, path, type_page, flags, data_page);
3276 if (ret)
3277 return ret;
3278 if (!may_mount())
3279 return -EPERM;
f7e33bdb
JL
3280 if (flags & SB_MANDLOCK)
3281 warn_mandlock();
a27ab9f2 3282
613cbe3d
AK
3283 /* Default to relatime unless overriden */
3284 if (!(flags & MS_NOATIME))
3285 mnt_flags |= MNT_RELATIME;
0a1c01c9 3286
1da177e4
LT
3287 /* Separate the per-mountpoint flags */
3288 if (flags & MS_NOSUID)
3289 mnt_flags |= MNT_NOSUID;
3290 if (flags & MS_NODEV)
3291 mnt_flags |= MNT_NODEV;
3292 if (flags & MS_NOEXEC)
3293 mnt_flags |= MNT_NOEXEC;
fc33a7bb
CH
3294 if (flags & MS_NOATIME)
3295 mnt_flags |= MNT_NOATIME;
3296 if (flags & MS_NODIRATIME)
3297 mnt_flags |= MNT_NODIRATIME;
d0adde57
MG
3298 if (flags & MS_STRICTATIME)
3299 mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
a9e5b732 3300 if (flags & MS_RDONLY)
2e4b7fcd 3301 mnt_flags |= MNT_READONLY;
dab741e0
MN
3302 if (flags & MS_NOSYMFOLLOW)
3303 mnt_flags |= MNT_NOSYMFOLLOW;
fc33a7bb 3304
ffbc6f0e
EB
3305 /* The default atime for remount is preservation */
3306 if ((flags & MS_REMOUNT) &&
3307 ((flags & (MS_NOATIME | MS_NODIRATIME | MS_RELATIME |
3308 MS_STRICTATIME)) == 0)) {
3309 mnt_flags &= ~MNT_ATIME_MASK;
a1e6aaa3 3310 mnt_flags |= path->mnt->mnt_flags & MNT_ATIME_MASK;
ffbc6f0e
EB
3311 }
3312
e462ec50
DH
3313 sb_flags = flags & (SB_RDONLY |
3314 SB_SYNCHRONOUS |
3315 SB_MANDLOCK |
3316 SB_DIRSYNC |
3317 SB_SILENT |
917086ff 3318 SB_POSIXACL |
d7ee9469 3319 SB_LAZYTIME |
917086ff 3320 SB_I_VERSION);
1da177e4 3321
43f5e655 3322 if ((flags & (MS_REMOUNT | MS_BIND)) == (MS_REMOUNT | MS_BIND))
a1e6aaa3
CH
3323 return do_reconfigure_mnt(path, mnt_flags);
3324 if (flags & MS_REMOUNT)
3325 return do_remount(path, flags, sb_flags, mnt_flags, data_page);
3326 if (flags & MS_BIND)
3327 return do_loopback(path, dev_name, flags & MS_REC);
3328 if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
3329 return do_change_type(path, flags);
3330 if (flags & MS_MOVE)
3331 return do_move_mount_old(path, dev_name);
3332
3333 return do_new_mount(path, type_page, sb_flags, mnt_flags, dev_name,
3334 data_page);
3335}
3336
3337long do_mount(const char *dev_name, const char __user *dir_name,
3338 const char *type_page, unsigned long flags, void *data_page)
3339{
3340 struct path path;
3341 int ret;
3342
3343 ret = user_path_at(AT_FDCWD, dir_name, LOOKUP_FOLLOW, &path);
3344 if (ret)
3345 return ret;
3346 ret = path_mount(dev_name, &path, type_page, flags, data_page);
2d92ab3c 3347 path_put(&path);
a1e6aaa3 3348 return ret;
1da177e4
LT
3349}
3350
537f7ccb
EB
3351static struct ucounts *inc_mnt_namespaces(struct user_namespace *ns)
3352{
3353 return inc_ucount(ns, current_euid(), UCOUNT_MNT_NAMESPACES);
3354}
3355
3356static void dec_mnt_namespaces(struct ucounts *ucounts)
3357{
3358 dec_ucount(ucounts, UCOUNT_MNT_NAMESPACES);
3359}
3360
771b1371
EB
3361static void free_mnt_ns(struct mnt_namespace *ns)
3362{
74e83122
AV
3363 if (!is_anon_ns(ns))
3364 ns_free_inum(&ns->ns);
537f7ccb 3365 dec_mnt_namespaces(ns->ucounts);
771b1371
EB
3366 put_user_ns(ns->user_ns);
3367 kfree(ns);
3368}
3369
8823c079
EB
3370/*
3371 * Assign a sequence number so we can detect when we attempt to bind
3372 * mount a reference to an older mount namespace into the current
3373 * mount namespace, preventing reference counting loops. A 64bit
3374 * number incrementing at 10Ghz will take 12,427 years to wrap which
3375 * is effectively never, so we can ignore the possibility.
3376 */
3377static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1);
3378
74e83122 3379static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool anon)
cf8d2c11
TM
3380{
3381 struct mnt_namespace *new_ns;
537f7ccb 3382 struct ucounts *ucounts;
98f842e6 3383 int ret;
cf8d2c11 3384
537f7ccb
EB
3385 ucounts = inc_mnt_namespaces(user_ns);
3386 if (!ucounts)
df75e774 3387 return ERR_PTR(-ENOSPC);
537f7ccb 3388
30acd0bd 3389 new_ns = kzalloc(sizeof(struct mnt_namespace), GFP_KERNEL_ACCOUNT);
537f7ccb
EB
3390 if (!new_ns) {
3391 dec_mnt_namespaces(ucounts);
cf8d2c11 3392 return ERR_PTR(-ENOMEM);
537f7ccb 3393 }
74e83122
AV
3394 if (!anon) {
3395 ret = ns_alloc_inum(&new_ns->ns);
3396 if (ret) {
3397 kfree(new_ns);
3398 dec_mnt_namespaces(ucounts);
3399 return ERR_PTR(ret);
3400 }
98f842e6 3401 }
33c42940 3402 new_ns->ns.ops = &mntns_operations;
74e83122
AV
3403 if (!anon)
3404 new_ns->seq = atomic64_add_return(1, &mnt_ns_seq);
1a7b8969 3405 refcount_set(&new_ns->ns.count, 1);
cf8d2c11
TM
3406 INIT_LIST_HEAD(&new_ns->list);
3407 init_waitqueue_head(&new_ns->poll);
9f6c61f9 3408 spin_lock_init(&new_ns->ns_lock);
771b1371 3409 new_ns->user_ns = get_user_ns(user_ns);
537f7ccb 3410 new_ns->ucounts = ucounts;
cf8d2c11
TM
3411 return new_ns;
3412}
3413
0766f788 3414__latent_entropy
9559f689
AV
3415struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
3416 struct user_namespace *user_ns, struct fs_struct *new_fs)
1da177e4 3417{
6b3286ed 3418 struct mnt_namespace *new_ns;
7f2da1e7 3419 struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
315fc83e 3420 struct mount *p, *q;
9559f689 3421 struct mount *old;
cb338d06 3422 struct mount *new;
7a472ef4 3423 int copy_flags;
1da177e4 3424
9559f689
AV
3425 BUG_ON(!ns);
3426
3427 if (likely(!(flags & CLONE_NEWNS))) {
3428 get_mnt_ns(ns);
3429 return ns;
3430 }
3431
3432 old = ns->root;
3433
74e83122 3434 new_ns = alloc_mnt_ns(user_ns, false);
cf8d2c11
TM
3435 if (IS_ERR(new_ns))
3436 return new_ns;
1da177e4 3437
97216be0 3438 namespace_lock();
1da177e4 3439 /* First pass: copy the tree topology */
4ce5d2b1 3440 copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE;
9559f689 3441 if (user_ns != ns->user_ns)
3bd045cc 3442 copy_flags |= CL_SHARED_TO_SLAVE;
7a472ef4 3443 new = copy_tree(old, old->mnt.mnt_root, copy_flags);
be34d1a3 3444 if (IS_ERR(new)) {
328e6d90 3445 namespace_unlock();
771b1371 3446 free_mnt_ns(new_ns);
be34d1a3 3447 return ERR_CAST(new);
1da177e4 3448 }
3bd045cc
AV
3449 if (user_ns != ns->user_ns) {
3450 lock_mount_hash();
3451 lock_mnt_tree(new);
3452 unlock_mount_hash();
3453 }
be08d6d2 3454 new_ns->root = new;
1a4eeaf2 3455 list_add_tail(&new_ns->list, &new->mnt_list);
1da177e4
LT
3456
3457 /*
3458 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
3459 * as belonging to new namespace. We have already acquired a private
3460 * fs_struct, so tsk->fs->lock is not needed.
3461 */
909b0a88 3462 p = old;
cb338d06 3463 q = new;
1da177e4 3464 while (p) {
143c8c91 3465 q->mnt_ns = new_ns;
d2921684 3466 new_ns->mounts++;
9559f689
AV
3467 if (new_fs) {
3468 if (&p->mnt == new_fs->root.mnt) {
3469 new_fs->root.mnt = mntget(&q->mnt);
315fc83e 3470 rootmnt = &p->mnt;
1da177e4 3471 }
9559f689
AV
3472 if (&p->mnt == new_fs->pwd.mnt) {
3473 new_fs->pwd.mnt = mntget(&q->mnt);
315fc83e 3474 pwdmnt = &p->mnt;
1da177e4 3475 }
1da177e4 3476 }
909b0a88
AV
3477 p = next_mnt(p, old);
3478 q = next_mnt(q, new);
4ce5d2b1
EB
3479 if (!q)
3480 break;
3481 while (p->mnt.mnt_root != q->mnt.mnt_root)
3482 p = next_mnt(p, old);
1da177e4 3483 }
328e6d90 3484 namespace_unlock();
1da177e4 3485
1da177e4 3486 if (rootmnt)
f03c6599 3487 mntput(rootmnt);
1da177e4 3488 if (pwdmnt)
f03c6599 3489 mntput(pwdmnt);
1da177e4 3490
741a2951 3491 return new_ns;
1da177e4
LT
3492}
3493
74e83122 3494struct dentry *mount_subtree(struct vfsmount *m, const char *name)
ea441d11 3495{
74e83122 3496 struct mount *mnt = real_mount(m);
ea441d11 3497 struct mnt_namespace *ns;
d31da0f0 3498 struct super_block *s;
ea441d11
AV
3499 struct path path;
3500 int err;
3501
74e83122
AV
3502 ns = alloc_mnt_ns(&init_user_ns, true);
3503 if (IS_ERR(ns)) {
3504 mntput(m);
ea441d11 3505 return ERR_CAST(ns);
74e83122
AV
3506 }
3507 mnt->mnt_ns = ns;
3508 ns->root = mnt;
3509 ns->mounts++;
3510 list_add(&mnt->mnt_list, &ns->list);
ea441d11 3511
74e83122 3512 err = vfs_path_lookup(m->mnt_root, m,
ea441d11
AV
3513 name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
3514
3515 put_mnt_ns(ns);
3516
3517 if (err)
3518 return ERR_PTR(err);
3519
3520 /* trade a vfsmount reference for active sb one */
d31da0f0
AV
3521 s = path.mnt->mnt_sb;
3522 atomic_inc(&s->s_active);
ea441d11
AV
3523 mntput(path.mnt);
3524 /* lock the sucker */
d31da0f0 3525 down_write(&s->s_umount);
ea441d11
AV
3526 /* ... and return the root of (sub)tree on it */
3527 return path.dentry;
3528}
3529EXPORT_SYMBOL(mount_subtree);
3530
cccaa5e3
DB
3531SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
3532 char __user *, type, unsigned long, flags, void __user *, data)
1da177e4 3533{
eca6f534
VN
3534 int ret;
3535 char *kernel_type;
eca6f534 3536 char *kernel_dev;
b40ef869 3537 void *options;
1da177e4 3538
b8850d1f
TG
3539 kernel_type = copy_mount_string(type);
3540 ret = PTR_ERR(kernel_type);
3541 if (IS_ERR(kernel_type))
eca6f534 3542 goto out_type;
1da177e4 3543
b8850d1f
TG
3544 kernel_dev = copy_mount_string(dev_name);
3545 ret = PTR_ERR(kernel_dev);
3546 if (IS_ERR(kernel_dev))
eca6f534 3547 goto out_dev;
1da177e4 3548
b40ef869
AV
3549 options = copy_mount_options(data);
3550 ret = PTR_ERR(options);
3551 if (IS_ERR(options))
eca6f534 3552 goto out_data;
1da177e4 3553
b40ef869 3554 ret = do_mount(kernel_dev, dir_name, kernel_type, flags, options);
1da177e4 3555
b40ef869 3556 kfree(options);
eca6f534
VN
3557out_data:
3558 kfree(kernel_dev);
3559out_dev:
eca6f534
VN
3560 kfree(kernel_type);
3561out_type:
3562 return ret;
1da177e4
LT
3563}
3564
dd8b477f
CB
3565#define FSMOUNT_VALID_FLAGS \
3566 (MOUNT_ATTR_RDONLY | MOUNT_ATTR_NOSUID | MOUNT_ATTR_NODEV | \
3567 MOUNT_ATTR_NOEXEC | MOUNT_ATTR__ATIME | MOUNT_ATTR_NODIRATIME | \
3568 MOUNT_ATTR_NOSYMFOLLOW)
5b490500 3569
9caccd41 3570#define MOUNT_SETATTR_VALID_FLAGS (FSMOUNT_VALID_FLAGS | MOUNT_ATTR_IDMAP)
2a186721
CB
3571
3572#define MOUNT_SETATTR_PROPAGATION_FLAGS \
3573 (MS_UNBINDABLE | MS_PRIVATE | MS_SLAVE | MS_SHARED)
3574
5b490500
CB
3575static unsigned int attr_flags_to_mnt_flags(u64 attr_flags)
3576{
3577 unsigned int mnt_flags = 0;
3578
3579 if (attr_flags & MOUNT_ATTR_RDONLY)
3580 mnt_flags |= MNT_READONLY;
3581 if (attr_flags & MOUNT_ATTR_NOSUID)
3582 mnt_flags |= MNT_NOSUID;
3583 if (attr_flags & MOUNT_ATTR_NODEV)
3584 mnt_flags |= MNT_NODEV;
3585 if (attr_flags & MOUNT_ATTR_NOEXEC)
3586 mnt_flags |= MNT_NOEXEC;
3587 if (attr_flags & MOUNT_ATTR_NODIRATIME)
3588 mnt_flags |= MNT_NODIRATIME;
dd8b477f
CB
3589 if (attr_flags & MOUNT_ATTR_NOSYMFOLLOW)
3590 mnt_flags |= MNT_NOSYMFOLLOW;
5b490500
CB
3591
3592 return mnt_flags;
3593}
3594
2db154b3 3595/*
93766fbd
DH
3596 * Create a kernel mount representation for a new, prepared superblock
3597 * (specified by fs_fd) and attach to an open_tree-like file descriptor.
3598 */
3599SYSCALL_DEFINE3(fsmount, int, fs_fd, unsigned int, flags,
3600 unsigned int, attr_flags)
3601{
3602 struct mnt_namespace *ns;
3603 struct fs_context *fc;
3604 struct file *file;
3605 struct path newmount;
3606 struct mount *mnt;
3607 struct fd f;
3608 unsigned int mnt_flags = 0;
3609 long ret;
3610
3611 if (!may_mount())
3612 return -EPERM;
3613
3614 if ((flags & ~(FSMOUNT_CLOEXEC)) != 0)
3615 return -EINVAL;
3616
5b490500 3617 if (attr_flags & ~FSMOUNT_VALID_FLAGS)
93766fbd
DH
3618 return -EINVAL;
3619
5b490500 3620 mnt_flags = attr_flags_to_mnt_flags(attr_flags);
93766fbd
DH
3621
3622 switch (attr_flags & MOUNT_ATTR__ATIME) {
3623 case MOUNT_ATTR_STRICTATIME:
3624 break;
3625 case MOUNT_ATTR_NOATIME:
3626 mnt_flags |= MNT_NOATIME;
3627 break;
3628 case MOUNT_ATTR_RELATIME:
3629 mnt_flags |= MNT_RELATIME;
3630 break;
3631 default:
3632 return -EINVAL;
3633 }
3634
3635 f = fdget(fs_fd);
3636 if (!f.file)
3637 return -EBADF;
3638
3639 ret = -EINVAL;
3640 if (f.file->f_op != &fscontext_fops)
3641 goto err_fsfd;
3642
3643 fc = f.file->private_data;
3644
3645 ret = mutex_lock_interruptible(&fc->uapi_mutex);
3646 if (ret < 0)
3647 goto err_fsfd;
3648
3649 /* There must be a valid superblock or we can't mount it */
3650 ret = -EINVAL;
3651 if (!fc->root)
3652 goto err_unlock;
3653
3654 ret = -EPERM;
3655 if (mount_too_revealing(fc->root->d_sb, &mnt_flags)) {
3656 pr_warn("VFS: Mount too revealing\n");
3657 goto err_unlock;
3658 }
3659
3660 ret = -EBUSY;
3661 if (fc->phase != FS_CONTEXT_AWAITING_MOUNT)
3662 goto err_unlock;
3663
f7e33bdb
JL
3664 if (fc->sb_flags & SB_MANDLOCK)
3665 warn_mandlock();
93766fbd
DH
3666
3667 newmount.mnt = vfs_create_mount(fc);
3668 if (IS_ERR(newmount.mnt)) {
3669 ret = PTR_ERR(newmount.mnt);
3670 goto err_unlock;
3671 }
3672 newmount.dentry = dget(fc->root);
3673 newmount.mnt->mnt_flags = mnt_flags;
3674
3675 /* We've done the mount bit - now move the file context into more or
3676 * less the same state as if we'd done an fspick(). We don't want to
3677 * do any memory allocation or anything like that at this point as we
3678 * don't want to have to handle any errors incurred.
3679 */
3680 vfs_clean_context(fc);
3681
3682 ns = alloc_mnt_ns(current->nsproxy->mnt_ns->user_ns, true);
3683 if (IS_ERR(ns)) {
3684 ret = PTR_ERR(ns);
3685 goto err_path;
3686 }
3687 mnt = real_mount(newmount.mnt);
3688 mnt->mnt_ns = ns;
3689 ns->root = mnt;
3690 ns->mounts = 1;
3691 list_add(&mnt->mnt_list, &ns->list);
1b0b9cc8 3692 mntget(newmount.mnt);
93766fbd
DH
3693
3694 /* Attach to an apparent O_PATH fd with a note that we need to unmount
3695 * it, not just simply put it.
3696 */
3697 file = dentry_open(&newmount, O_PATH, fc->cred);
3698 if (IS_ERR(file)) {
3699 dissolve_on_fput(newmount.mnt);
3700 ret = PTR_ERR(file);
3701 goto err_path;
3702 }
3703 file->f_mode |= FMODE_NEED_UNMOUNT;
3704
3705 ret = get_unused_fd_flags((flags & FSMOUNT_CLOEXEC) ? O_CLOEXEC : 0);
3706 if (ret >= 0)
3707 fd_install(ret, file);
3708 else
3709 fput(file);
3710
3711err_path:
3712 path_put(&newmount);
3713err_unlock:
3714 mutex_unlock(&fc->uapi_mutex);
3715err_fsfd:
3716 fdput(f);
3717 return ret;
3718}
3719
3720/*
3721 * Move a mount from one place to another. In combination with
3722 * fsopen()/fsmount() this is used to install a new mount and in combination
3723 * with open_tree(OPEN_TREE_CLONE [| AT_RECURSIVE]) it can be used to copy
3724 * a mount subtree.
2db154b3
DH
3725 *
3726 * Note the flags value is a combination of MOVE_MOUNT_* flags.
3727 */
3728SYSCALL_DEFINE5(move_mount,
2658ce09
BD
3729 int, from_dfd, const char __user *, from_pathname,
3730 int, to_dfd, const char __user *, to_pathname,
2db154b3
DH
3731 unsigned int, flags)
3732{
3733 struct path from_path, to_path;
3734 unsigned int lflags;
3735 int ret = 0;
3736
3737 if (!may_mount())
3738 return -EPERM;
3739
3740 if (flags & ~MOVE_MOUNT__MASK)
3741 return -EINVAL;
3742
3743 /* If someone gives a pathname, they aren't permitted to move
3744 * from an fd that requires unmount as we can't get at the flag
3745 * to clear it afterwards.
3746 */
3747 lflags = 0;
3748 if (flags & MOVE_MOUNT_F_SYMLINKS) lflags |= LOOKUP_FOLLOW;
3749 if (flags & MOVE_MOUNT_F_AUTOMOUNTS) lflags |= LOOKUP_AUTOMOUNT;
3750 if (flags & MOVE_MOUNT_F_EMPTY_PATH) lflags |= LOOKUP_EMPTY;
3751
3752 ret = user_path_at(from_dfd, from_pathname, lflags, &from_path);
3753 if (ret < 0)
3754 return ret;
3755
3756 lflags = 0;
3757 if (flags & MOVE_MOUNT_T_SYMLINKS) lflags |= LOOKUP_FOLLOW;
3758 if (flags & MOVE_MOUNT_T_AUTOMOUNTS) lflags |= LOOKUP_AUTOMOUNT;
3759 if (flags & MOVE_MOUNT_T_EMPTY_PATH) lflags |= LOOKUP_EMPTY;
3760
3761 ret = user_path_at(to_dfd, to_pathname, lflags, &to_path);
3762 if (ret < 0)
3763 goto out_from;
3764
3765 ret = security_move_mount(&from_path, &to_path);
3766 if (ret < 0)
3767 goto out_to;
3768
9ffb14ef
PT
3769 if (flags & MOVE_MOUNT_SET_GROUP)
3770 ret = do_set_group(&from_path, &to_path);
3771 else
3772 ret = do_move_mount(&from_path, &to_path);
2db154b3
DH
3773
3774out_to:
3775 path_put(&to_path);
3776out_from:
3777 path_put(&from_path);
3778 return ret;
3779}
3780
afac7cba
AV
3781/*
3782 * Return true if path is reachable from root
3783 *
48a066e7 3784 * namespace_sem or mount_lock is held
afac7cba 3785 */
643822b4 3786bool is_path_reachable(struct mount *mnt, struct dentry *dentry,
afac7cba
AV
3787 const struct path *root)
3788{
643822b4 3789 while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) {
a73324da 3790 dentry = mnt->mnt_mountpoint;
0714a533 3791 mnt = mnt->mnt_parent;
afac7cba 3792 }
643822b4 3793 return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry);
afac7cba
AV
3794}
3795
640eb7e7 3796bool path_is_under(const struct path *path1, const struct path *path2)
afac7cba 3797{
25ab4c9b 3798 bool res;
48a066e7 3799 read_seqlock_excl(&mount_lock);
643822b4 3800 res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2);
48a066e7 3801 read_sequnlock_excl(&mount_lock);
afac7cba
AV
3802 return res;
3803}
3804EXPORT_SYMBOL(path_is_under);
3805
1da177e4
LT
3806/*
3807 * pivot_root Semantics:
3808 * Moves the root file system of the current process to the directory put_old,
3809 * makes new_root as the new root file system of the current process, and sets
3810 * root/cwd of all processes which had them on the current root to new_root.
3811 *
3812 * Restrictions:
3813 * The new_root and put_old must be directories, and must not be on the
3814 * same file system as the current process root. The put_old must be
3815 * underneath new_root, i.e. adding a non-zero number of /.. to the string
3816 * pointed to by put_old must yield the same directory as new_root. No other
3817 * file system may be mounted on put_old. After all, new_root is a mountpoint.
3818 *
4a0d11fa 3819 * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
0c1bc6b8 3820 * See Documentation/filesystems/ramfs-rootfs-initramfs.rst for alternatives
4a0d11fa
NB
3821 * in this situation.
3822 *
1da177e4
LT
3823 * Notes:
3824 * - we don't move root/cwd if they are not at the root (reason: if something
3825 * cared enough to change them, it's probably wrong to force them elsewhere)
3826 * - it's okay to pick a root that isn't the root of a file system, e.g.
3827 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
3828 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
3829 * first.
3830 */
3480b257
HC
3831SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
3832 const char __user *, put_old)
1da177e4 3833{
2763d119
AV
3834 struct path new, old, root;
3835 struct mount *new_mnt, *root_mnt, *old_mnt, *root_parent, *ex_parent;
84d17192 3836 struct mountpoint *old_mp, *root_mp;
1da177e4
LT
3837 int error;
3838
9b40bc90 3839 if (!may_mount())
1da177e4
LT
3840 return -EPERM;
3841
ce6595a2
AV
3842 error = user_path_at(AT_FDCWD, new_root,
3843 LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &new);
1da177e4
LT
3844 if (error)
3845 goto out0;
1da177e4 3846
ce6595a2
AV
3847 error = user_path_at(AT_FDCWD, put_old,
3848 LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &old);
1da177e4
LT
3849 if (error)
3850 goto out1;
3851
2d8f3038 3852 error = security_sb_pivotroot(&old, &new);
b12cea91
AV
3853 if (error)
3854 goto out2;
1da177e4 3855
f7ad3c6b 3856 get_fs_root(current->fs, &root);
84d17192
AV
3857 old_mp = lock_mount(&old);
3858 error = PTR_ERR(old_mp);
3859 if (IS_ERR(old_mp))
b12cea91
AV
3860 goto out3;
3861
1da177e4 3862 error = -EINVAL;
419148da
AV
3863 new_mnt = real_mount(new.mnt);
3864 root_mnt = real_mount(root.mnt);
84d17192 3865 old_mnt = real_mount(old.mnt);
2763d119
AV
3866 ex_parent = new_mnt->mnt_parent;
3867 root_parent = root_mnt->mnt_parent;
84d17192 3868 if (IS_MNT_SHARED(old_mnt) ||
2763d119
AV
3869 IS_MNT_SHARED(ex_parent) ||
3870 IS_MNT_SHARED(root_parent))
b12cea91 3871 goto out4;
143c8c91 3872 if (!check_mnt(root_mnt) || !check_mnt(new_mnt))
b12cea91 3873 goto out4;
5ff9d8a6
EB
3874 if (new_mnt->mnt.mnt_flags & MNT_LOCKED)
3875 goto out4;
1da177e4 3876 error = -ENOENT;
f3da392e 3877 if (d_unlinked(new.dentry))
b12cea91 3878 goto out4;
1da177e4 3879 error = -EBUSY;
84d17192 3880 if (new_mnt == root_mnt || old_mnt == root_mnt)
b12cea91 3881 goto out4; /* loop, on the same file system */
1da177e4 3882 error = -EINVAL;
8c3ee42e 3883 if (root.mnt->mnt_root != root.dentry)
b12cea91 3884 goto out4; /* not a mountpoint */
676da58d 3885 if (!mnt_has_parent(root_mnt))
b12cea91 3886 goto out4; /* not attached */
2d8f3038 3887 if (new.mnt->mnt_root != new.dentry)
b12cea91 3888 goto out4; /* not a mountpoint */
676da58d 3889 if (!mnt_has_parent(new_mnt))
b12cea91 3890 goto out4; /* not attached */
4ac91378 3891 /* make sure we can reach put_old from new_root */
84d17192 3892 if (!is_path_reachable(old_mnt, old.dentry, &new))
b12cea91 3893 goto out4;
0d082601
EB
3894 /* make certain new is below the root */
3895 if (!is_path_reachable(new_mnt, new.dentry, &root))
3896 goto out4;
719ea2fb 3897 lock_mount_hash();
2763d119
AV
3898 umount_mnt(new_mnt);
3899 root_mp = unhash_mnt(root_mnt); /* we'll need its mountpoint */
5ff9d8a6
EB
3900 if (root_mnt->mnt.mnt_flags & MNT_LOCKED) {
3901 new_mnt->mnt.mnt_flags |= MNT_LOCKED;
3902 root_mnt->mnt.mnt_flags &= ~MNT_LOCKED;
3903 }
4ac91378 3904 /* mount old root on put_old */
84d17192 3905 attach_mnt(root_mnt, old_mnt, old_mp);
4ac91378 3906 /* mount new_root on / */
2763d119
AV
3907 attach_mnt(new_mnt, root_parent, root_mp);
3908 mnt_add_count(root_parent, -1);
6b3286ed 3909 touch_mnt_namespace(current->nsproxy->mnt_ns);
4fed655c
EB
3910 /* A moved mount should not expire automatically */
3911 list_del_init(&new_mnt->mnt_expire);
3895dbf8 3912 put_mountpoint(root_mp);
719ea2fb 3913 unlock_mount_hash();
2d8f3038 3914 chroot_fs_refs(&root, &new);
1da177e4 3915 error = 0;
b12cea91 3916out4:
84d17192 3917 unlock_mount(old_mp);
2763d119
AV
3918 if (!error)
3919 mntput_no_expire(ex_parent);
b12cea91 3920out3:
8c3ee42e 3921 path_put(&root);
b12cea91 3922out2:
2d8f3038 3923 path_put(&old);
1da177e4 3924out1:
2d8f3038 3925 path_put(&new);
1da177e4 3926out0:
1da177e4 3927 return error;
1da177e4
LT
3928}
3929
2a186721
CB
3930static unsigned int recalc_flags(struct mount_kattr *kattr, struct mount *mnt)
3931{
3932 unsigned int flags = mnt->mnt.mnt_flags;
3933
3934 /* flags to clear */
3935 flags &= ~kattr->attr_clr;
3936 /* flags to raise */
3937 flags |= kattr->attr_set;
3938
3939 return flags;
3940}
3941
9caccd41
CB
3942static int can_idmap_mount(const struct mount_kattr *kattr, struct mount *mnt)
3943{
3944 struct vfsmount *m = &mnt->mnt;
911a39c1 3945 struct user_namespace *fs_userns = m->mnt_sb->s_user_ns;
9caccd41
CB
3946
3947 if (!kattr->mnt_userns)
3948 return 0;
3949
911a39c1
CB
3950 /*
3951 * Creating an idmapped mount with the filesystem wide idmapping
3952 * doesn't make sense so block that. We don't allow mushy semantics.
3953 */
3954 if (kattr->mnt_userns == fs_userns)
3955 return -EINVAL;
3956
9caccd41
CB
3957 /*
3958 * Once a mount has been idmapped we don't allow it to change its
3959 * mapping. It makes things simpler and callers can just create
3960 * another bind-mount they can idmap if they want to.
3961 */
03dfc6a8 3962 if (is_idmapped_mnt(m))
9caccd41
CB
3963 return -EPERM;
3964
3965 /* The underlying filesystem doesn't support idmapped mounts yet. */
3966 if (!(m->mnt_sb->s_type->fs_flags & FS_ALLOW_IDMAP))
3967 return -EINVAL;
3968
3969 /* We're not controlling the superblock. */
911a39c1 3970 if (!ns_capable(fs_userns, CAP_SYS_ADMIN))
9caccd41
CB
3971 return -EPERM;
3972
3973 /* Mount has already been visible in the filesystem hierarchy. */
3974 if (!is_anon_ns(mnt->mnt_ns))
3975 return -EINVAL;
3976
3977 return 0;
3978}
3979
2a186721
CB
3980static struct mount *mount_setattr_prepare(struct mount_kattr *kattr,
3981 struct mount *mnt, int *err)
3982{
3983 struct mount *m = mnt, *last = NULL;
3984
3985 if (!is_mounted(&m->mnt)) {
3986 *err = -EINVAL;
3987 goto out;
3988 }
3989
3990 if (!(mnt_has_parent(m) ? check_mnt(m) : is_anon_ns(m->mnt_ns))) {
3991 *err = -EINVAL;
3992 goto out;
3993 }
3994
3995 do {
3996 unsigned int flags;
3997
3998 flags = recalc_flags(kattr, m);
3999 if (!can_change_locked_flags(m, flags)) {
4000 *err = -EPERM;
4001 goto out;
4002 }
4003
9caccd41
CB
4004 *err = can_idmap_mount(kattr, m);
4005 if (*err)
4006 goto out;
4007
2a186721
CB
4008 last = m;
4009
4010 if ((kattr->attr_set & MNT_READONLY) &&
4011 !(m->mnt.mnt_flags & MNT_READONLY)) {
4012 *err = mnt_hold_writers(m);
4013 if (*err)
4014 goto out;
4015 }
4016 } while (kattr->recurse && (m = next_mnt(m, mnt)));
4017
4018out:
4019 return last;
4020}
4021
9caccd41
CB
4022static void do_idmap_mount(const struct mount_kattr *kattr, struct mount *mnt)
4023{
911a39c1 4024 struct user_namespace *mnt_userns, *old_mnt_userns;
9caccd41
CB
4025
4026 if (!kattr->mnt_userns)
4027 return;
4028
911a39c1
CB
4029 /*
4030 * We're the only ones able to change the mount's idmapping. So
4031 * mnt->mnt.mnt_userns is stable and we can retrieve it directly.
4032 */
4033 old_mnt_userns = mnt->mnt.mnt_userns;
4034
9caccd41
CB
4035 mnt_userns = get_user_ns(kattr->mnt_userns);
4036 /* Pairs with smp_load_acquire() in mnt_user_ns(). */
4037 smp_store_release(&mnt->mnt.mnt_userns, mnt_userns);
911a39c1
CB
4038
4039 /*
4040 * If this is an idmapped filesystem drop the reference we've taken
4041 * in vfs_create_mount() before.
4042 */
4043 if (!initial_idmapping(old_mnt_userns))
4044 put_user_ns(old_mnt_userns);
9caccd41
CB
4045}
4046
2a186721
CB
4047static void mount_setattr_commit(struct mount_kattr *kattr,
4048 struct mount *mnt, struct mount *last,
4049 int err)
4050{
4051 struct mount *m = mnt;
4052
4053 do {
4054 if (!err) {
4055 unsigned int flags;
4056
9caccd41 4057 do_idmap_mount(kattr, m);
2a186721
CB
4058 flags = recalc_flags(kattr, m);
4059 WRITE_ONCE(m->mnt.mnt_flags, flags);
4060 }
4061
4062 /*
4063 * We either set MNT_READONLY above so make it visible
4064 * before ~MNT_WRITE_HOLD or we failed to recursively
4065 * apply mount options.
4066 */
4067 if ((kattr->attr_set & MNT_READONLY) &&
4068 (m->mnt.mnt_flags & MNT_WRITE_HOLD))
4069 mnt_unhold_writers(m);
4070
4071 if (!err && kattr->propagation)
4072 change_mnt_propagation(m, kattr->propagation);
4073
4074 /*
4075 * On failure, only cleanup until we found the first mount
4076 * we failed to handle.
4077 */
4078 if (err && m == last)
4079 break;
4080 } while (kattr->recurse && (m = next_mnt(m, mnt)));
4081
4082 if (!err)
4083 touch_mnt_namespace(mnt->mnt_ns);
4084}
4085
4086static int do_mount_setattr(struct path *path, struct mount_kattr *kattr)
4087{
4088 struct mount *mnt = real_mount(path->mnt), *last = NULL;
4089 int err = 0;
4090
4091 if (path->dentry != mnt->mnt.mnt_root)
4092 return -EINVAL;
4093
4094 if (kattr->propagation) {
4095 /*
4096 * Only take namespace_lock() if we're actually changing
4097 * propagation.
4098 */
4099 namespace_lock();
4100 if (kattr->propagation == MS_SHARED) {
4101 err = invent_group_ids(mnt, kattr->recurse);
4102 if (err) {
4103 namespace_unlock();
4104 return err;
4105 }
4106 }
4107 }
4108
4109 lock_mount_hash();
4110
4111 /*
4112 * Get the mount tree in a shape where we can change mount
4113 * properties without failure.
4114 */
4115 last = mount_setattr_prepare(kattr, mnt, &err);
4116 if (last) /* Commit all changes or revert to the old state. */
4117 mount_setattr_commit(kattr, mnt, last, err);
4118
4119 unlock_mount_hash();
4120
4121 if (kattr->propagation) {
4122 namespace_unlock();
4123 if (err)
4124 cleanup_group_ids(mnt, NULL);
4125 }
4126
4127 return err;
4128}
4129
9caccd41
CB
4130static int build_mount_idmapped(const struct mount_attr *attr, size_t usize,
4131 struct mount_kattr *kattr, unsigned int flags)
4132{
4133 int err = 0;
4134 struct ns_common *ns;
4135 struct user_namespace *mnt_userns;
4136 struct file *file;
4137
4138 if (!((attr->attr_set | attr->attr_clr) & MOUNT_ATTR_IDMAP))
4139 return 0;
4140
4141 /*
4142 * We currently do not support clearing an idmapped mount. If this ever
4143 * is a use-case we can revisit this but for now let's keep it simple
4144 * and not allow it.
4145 */
4146 if (attr->attr_clr & MOUNT_ATTR_IDMAP)
4147 return -EINVAL;
4148
4149 if (attr->userns_fd > INT_MAX)
4150 return -EINVAL;
4151
4152 file = fget(attr->userns_fd);
4153 if (!file)
4154 return -EBADF;
4155
4156 if (!proc_ns_file(file)) {
4157 err = -EINVAL;
4158 goto out_fput;
4159 }
4160
4161 ns = get_proc_ns(file_inode(file));
4162 if (ns->ops->type != CLONE_NEWUSER) {
4163 err = -EINVAL;
4164 goto out_fput;
4165 }
4166
4167 /*
911a39c1
CB
4168 * The initial idmapping cannot be used to create an idmapped
4169 * mount. We use the initial idmapping as an indicator of a mount
4170 * that is not idmapped. It can simply be passed into helpers that
4171 * are aware of idmapped mounts as a convenient shortcut. A user
4172 * can just create a dedicated identity mapping to achieve the same
4173 * result.
9caccd41
CB
4174 */
4175 mnt_userns = container_of(ns, struct user_namespace, ns);
911a39c1 4176 if (initial_idmapping(mnt_userns)) {
9caccd41
CB
4177 err = -EPERM;
4178 goto out_fput;
4179 }
da89fd6a
SF
4180
4181 /* We're not controlling the target namespace. */
4182 if (!ns_capable(mnt_userns, CAP_SYS_ADMIN)) {
4183 err = -EPERM;
4184 goto out_fput;
4185 }
4186
9caccd41
CB
4187 kattr->mnt_userns = get_user_ns(mnt_userns);
4188
4189out_fput:
4190 fput(file);
4191 return err;
4192}
4193
4194static int build_mount_kattr(const struct mount_attr *attr, size_t usize,
2a186721
CB
4195 struct mount_kattr *kattr, unsigned int flags)
4196{
4197 unsigned int lookup_flags = LOOKUP_AUTOMOUNT | LOOKUP_FOLLOW;
4198
4199 if (flags & AT_NO_AUTOMOUNT)
4200 lookup_flags &= ~LOOKUP_AUTOMOUNT;
4201 if (flags & AT_SYMLINK_NOFOLLOW)
4202 lookup_flags &= ~LOOKUP_FOLLOW;
4203 if (flags & AT_EMPTY_PATH)
4204 lookup_flags |= LOOKUP_EMPTY;
4205
4206 *kattr = (struct mount_kattr) {
4207 .lookup_flags = lookup_flags,
4208 .recurse = !!(flags & AT_RECURSIVE),
4209 };
4210
4211 if (attr->propagation & ~MOUNT_SETATTR_PROPAGATION_FLAGS)
4212 return -EINVAL;
4213 if (hweight32(attr->propagation & MOUNT_SETATTR_PROPAGATION_FLAGS) > 1)
4214 return -EINVAL;
4215 kattr->propagation = attr->propagation;
4216
4217 if ((attr->attr_set | attr->attr_clr) & ~MOUNT_SETATTR_VALID_FLAGS)
4218 return -EINVAL;
4219
2a186721
CB
4220 kattr->attr_set = attr_flags_to_mnt_flags(attr->attr_set);
4221 kattr->attr_clr = attr_flags_to_mnt_flags(attr->attr_clr);
4222
4223 /*
4224 * Since the MOUNT_ATTR_<atime> values are an enum, not a bitmap,
4225 * users wanting to transition to a different atime setting cannot
4226 * simply specify the atime setting in @attr_set, but must also
4227 * specify MOUNT_ATTR__ATIME in the @attr_clr field.
4228 * So ensure that MOUNT_ATTR__ATIME can't be partially set in
4229 * @attr_clr and that @attr_set can't have any atime bits set if
4230 * MOUNT_ATTR__ATIME isn't set in @attr_clr.
4231 */
4232 if (attr->attr_clr & MOUNT_ATTR__ATIME) {
4233 if ((attr->attr_clr & MOUNT_ATTR__ATIME) != MOUNT_ATTR__ATIME)
4234 return -EINVAL;
4235
4236 /*
4237 * Clear all previous time settings as they are mutually
4238 * exclusive.
4239 */
4240 kattr->attr_clr |= MNT_RELATIME | MNT_NOATIME;
4241 switch (attr->attr_set & MOUNT_ATTR__ATIME) {
4242 case MOUNT_ATTR_RELATIME:
4243 kattr->attr_set |= MNT_RELATIME;
4244 break;
4245 case MOUNT_ATTR_NOATIME:
4246 kattr->attr_set |= MNT_NOATIME;
4247 break;
4248 case MOUNT_ATTR_STRICTATIME:
4249 break;
4250 default:
4251 return -EINVAL;
4252 }
4253 } else {
4254 if (attr->attr_set & MOUNT_ATTR__ATIME)
4255 return -EINVAL;
4256 }
4257
9caccd41
CB
4258 return build_mount_idmapped(attr, usize, kattr, flags);
4259}
4260
4261static void finish_mount_kattr(struct mount_kattr *kattr)
4262{
4263 put_user_ns(kattr->mnt_userns);
4264 kattr->mnt_userns = NULL;
2a186721
CB
4265}
4266
4267SYSCALL_DEFINE5(mount_setattr, int, dfd, const char __user *, path,
4268 unsigned int, flags, struct mount_attr __user *, uattr,
4269 size_t, usize)
4270{
4271 int err;
4272 struct path target;
4273 struct mount_attr attr;
4274 struct mount_kattr kattr;
4275
4276 BUILD_BUG_ON(sizeof(struct mount_attr) != MOUNT_ATTR_SIZE_VER0);
4277
4278 if (flags & ~(AT_EMPTY_PATH |
4279 AT_RECURSIVE |
4280 AT_SYMLINK_NOFOLLOW |
4281 AT_NO_AUTOMOUNT))
4282 return -EINVAL;
4283
4284 if (unlikely(usize > PAGE_SIZE))
4285 return -E2BIG;
4286 if (unlikely(usize < MOUNT_ATTR_SIZE_VER0))
4287 return -EINVAL;
4288
4289 if (!may_mount())
4290 return -EPERM;
4291
4292 err = copy_struct_from_user(&attr, sizeof(attr), uattr, usize);
4293 if (err)
4294 return err;
4295
4296 /* Don't bother walking through the mounts if this is a nop. */
4297 if (attr.attr_set == 0 &&
4298 attr.attr_clr == 0 &&
4299 attr.propagation == 0)
4300 return 0;
4301
9caccd41 4302 err = build_mount_kattr(&attr, usize, &kattr, flags);
2a186721
CB
4303 if (err)
4304 return err;
4305
4306 err = user_path_at(dfd, path, kattr.lookup_flags, &target);
24bd2263
CB
4307 if (!err) {
4308 err = do_mount_setattr(&target, &kattr);
4309 path_put(&target);
4310 }
9caccd41 4311 finish_mount_kattr(&kattr);
2a186721
CB
4312 return err;
4313}
4314
1da177e4
LT
4315static void __init init_mount_tree(void)
4316{
4317 struct vfsmount *mnt;
74e83122 4318 struct mount *m;
6b3286ed 4319 struct mnt_namespace *ns;
ac748a09 4320 struct path root;
1da177e4 4321
fd3e007f 4322 mnt = vfs_kern_mount(&rootfs_fs_type, 0, "rootfs", NULL);
1da177e4
LT
4323 if (IS_ERR(mnt))
4324 panic("Can't create rootfs");
b3e19d92 4325
74e83122 4326 ns = alloc_mnt_ns(&init_user_ns, false);
3b22edc5 4327 if (IS_ERR(ns))
1da177e4 4328 panic("Can't allocate initial namespace");
74e83122
AV
4329 m = real_mount(mnt);
4330 m->mnt_ns = ns;
4331 ns->root = m;
4332 ns->mounts = 1;
4333 list_add(&m->mnt_list, &ns->list);
6b3286ed
KK
4334 init_task.nsproxy->mnt_ns = ns;
4335 get_mnt_ns(ns);
4336
be08d6d2
AV
4337 root.mnt = mnt;
4338 root.dentry = mnt->mnt_root;
da362b09 4339 mnt->mnt_flags |= MNT_LOCKED;
ac748a09
JB
4340
4341 set_fs_pwd(current->fs, &root);
4342 set_fs_root(current->fs, &root);
1da177e4
LT
4343}
4344
74bf17cf 4345void __init mnt_init(void)
1da177e4 4346{
15a67dd8 4347 int err;
1da177e4 4348
7d6fec45 4349 mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
79f6540b 4350 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, NULL);
1da177e4 4351
0818bf27 4352 mount_hashtable = alloc_large_system_hash("Mount-cache",
38129a13 4353 sizeof(struct hlist_head),
0818bf27 4354 mhash_entries, 19,
3d375d78 4355 HASH_ZERO,
0818bf27
AV
4356 &m_hash_shift, &m_hash_mask, 0, 0);
4357 mountpoint_hashtable = alloc_large_system_hash("Mountpoint-cache",
4358 sizeof(struct hlist_head),
4359 mphash_entries, 19,
3d375d78 4360 HASH_ZERO,
0818bf27 4361 &mp_hash_shift, &mp_hash_mask, 0, 0);
1da177e4 4362
84d17192 4363 if (!mount_hashtable || !mountpoint_hashtable)
1da177e4
LT
4364 panic("Failed to allocate mount hash table\n");
4365
4b93dc9b
TH
4366 kernfs_init();
4367
15a67dd8
RD
4368 err = sysfs_init();
4369 if (err)
4370 printk(KERN_WARNING "%s: sysfs_init error: %d\n",
8e24eea7 4371 __func__, err);
00d26666
GKH
4372 fs_kobj = kobject_create_and_add("fs", NULL);
4373 if (!fs_kobj)
8e24eea7 4374 printk(KERN_WARNING "%s: kobj create error\n", __func__);
037f11b4 4375 shmem_init();
1da177e4
LT
4376 init_rootfs();
4377 init_mount_tree();
4378}
4379
616511d0 4380void put_mnt_ns(struct mnt_namespace *ns)
1da177e4 4381{
1a7b8969 4382 if (!refcount_dec_and_test(&ns->ns.count))
616511d0 4383 return;
7b00ed6f 4384 drop_collected_mounts(&ns->root->mnt);
771b1371 4385 free_mnt_ns(ns);
1da177e4 4386}
9d412a43 4387
d911b458 4388struct vfsmount *kern_mount(struct file_system_type *type)
9d412a43 4389{
423e0ab0 4390 struct vfsmount *mnt;
d911b458 4391 mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, NULL);
423e0ab0
TC
4392 if (!IS_ERR(mnt)) {
4393 /*
4394 * it is a longterm mount, don't release mnt until
4395 * we unmount before file sys is unregistered
4396 */
f7a99c5b 4397 real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL;
423e0ab0
TC
4398 }
4399 return mnt;
9d412a43 4400}
d911b458 4401EXPORT_SYMBOL_GPL(kern_mount);
423e0ab0
TC
4402
4403void kern_unmount(struct vfsmount *mnt)
4404{
4405 /* release long term mount so mount point can be released */
4406 if (!IS_ERR_OR_NULL(mnt)) {
f7a99c5b 4407 real_mount(mnt)->mnt_ns = NULL;
48a066e7 4408 synchronize_rcu(); /* yecchhh... */
423e0ab0
TC
4409 mntput(mnt);
4410 }
4411}
4412EXPORT_SYMBOL(kern_unmount);
02125a82 4413
df820f8d
MS
4414void kern_unmount_array(struct vfsmount *mnt[], unsigned int num)
4415{
4416 unsigned int i;
4417
4418 for (i = 0; i < num; i++)
4419 if (mnt[i])
4420 real_mount(mnt[i])->mnt_ns = NULL;
4421 synchronize_rcu_expedited();
4422 for (i = 0; i < num; i++)
4423 mntput(mnt[i]);
4424}
4425EXPORT_SYMBOL(kern_unmount_array);
4426
02125a82
AV
4427bool our_mnt(struct vfsmount *mnt)
4428{
143c8c91 4429 return check_mnt(real_mount(mnt));
02125a82 4430}
8823c079 4431
3151527e
EB
4432bool current_chrooted(void)
4433{
4434 /* Does the current process have a non-standard root */
4435 struct path ns_root;
4436 struct path fs_root;
4437 bool chrooted;
4438
4439 /* Find the namespace root */
4440 ns_root.mnt = &current->nsproxy->mnt_ns->root->mnt;
4441 ns_root.dentry = ns_root.mnt->mnt_root;
4442 path_get(&ns_root);
4443 while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root))
4444 ;
4445
4446 get_fs_root(current->fs, &fs_root);
4447
4448 chrooted = !path_equal(&fs_root, &ns_root);
4449
4450 path_put(&fs_root);
4451 path_put(&ns_root);
4452
4453 return chrooted;
4454}
4455
132e4608
DH
4456static bool mnt_already_visible(struct mnt_namespace *ns,
4457 const struct super_block *sb,
8654df4e 4458 int *new_mnt_flags)
87a8ebd6 4459{
8c6cf9cc 4460 int new_flags = *new_mnt_flags;
87a8ebd6 4461 struct mount *mnt;
e51db735 4462 bool visible = false;
87a8ebd6 4463
44bb4385 4464 down_read(&namespace_sem);
9f6c61f9 4465 lock_ns_list(ns);
87a8ebd6 4466 list_for_each_entry(mnt, &ns->list, mnt_list) {
e51db735 4467 struct mount *child;
77b1a97d
EB
4468 int mnt_flags;
4469
9f6c61f9
MS
4470 if (mnt_is_cursor(mnt))
4471 continue;
4472
132e4608 4473 if (mnt->mnt.mnt_sb->s_type != sb->s_type)
e51db735
EB
4474 continue;
4475
7e96c1b0
EB
4476 /* This mount is not fully visible if it's root directory
4477 * is not the root directory of the filesystem.
4478 */
4479 if (mnt->mnt.mnt_root != mnt->mnt.mnt_sb->s_root)
4480 continue;
4481
a1935c17 4482 /* A local view of the mount flags */
77b1a97d 4483 mnt_flags = mnt->mnt.mnt_flags;
77b1a97d 4484
695e9df0 4485 /* Don't miss readonly hidden in the superblock flags */
bc98a42c 4486 if (sb_rdonly(mnt->mnt.mnt_sb))
695e9df0
EB
4487 mnt_flags |= MNT_LOCK_READONLY;
4488
8c6cf9cc
EB
4489 /* Verify the mount flags are equal to or more permissive
4490 * than the proposed new mount.
4491 */
77b1a97d 4492 if ((mnt_flags & MNT_LOCK_READONLY) &&
8c6cf9cc
EB
4493 !(new_flags & MNT_READONLY))
4494 continue;
77b1a97d
EB
4495 if ((mnt_flags & MNT_LOCK_ATIME) &&
4496 ((mnt_flags & MNT_ATIME_MASK) != (new_flags & MNT_ATIME_MASK)))
8c6cf9cc
EB
4497 continue;
4498
ceeb0e5d
EB
4499 /* This mount is not fully visible if there are any
4500 * locked child mounts that cover anything except for
4501 * empty directories.
e51db735
EB
4502 */
4503 list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
4504 struct inode *inode = child->mnt_mountpoint->d_inode;
ceeb0e5d 4505 /* Only worry about locked mounts */
d71ed6c9 4506 if (!(child->mnt.mnt_flags & MNT_LOCKED))
ceeb0e5d 4507 continue;
7236c85e
EB
4508 /* Is the directory permanetly empty? */
4509 if (!is_empty_dir_inode(inode))
e51db735 4510 goto next;
87a8ebd6 4511 }
8c6cf9cc 4512 /* Preserve the locked attributes */
77b1a97d 4513 *new_mnt_flags |= mnt_flags & (MNT_LOCK_READONLY | \
77b1a97d 4514 MNT_LOCK_ATIME);
e51db735
EB
4515 visible = true;
4516 goto found;
4517 next: ;
87a8ebd6 4518 }
e51db735 4519found:
9f6c61f9 4520 unlock_ns_list(ns);
44bb4385 4521 up_read(&namespace_sem);
e51db735 4522 return visible;
87a8ebd6
EB
4523}
4524
132e4608 4525static bool mount_too_revealing(const struct super_block *sb, int *new_mnt_flags)
8654df4e 4526{
a1935c17 4527 const unsigned long required_iflags = SB_I_NOEXEC | SB_I_NODEV;
8654df4e
EB
4528 struct mnt_namespace *ns = current->nsproxy->mnt_ns;
4529 unsigned long s_iflags;
4530
4531 if (ns->user_ns == &init_user_ns)
4532 return false;
4533
4534 /* Can this filesystem be too revealing? */
132e4608 4535 s_iflags = sb->s_iflags;
8654df4e
EB
4536 if (!(s_iflags & SB_I_USERNS_VISIBLE))
4537 return false;
4538
a1935c17
EB
4539 if ((s_iflags & required_iflags) != required_iflags) {
4540 WARN_ONCE(1, "Expected s_iflags to contain 0x%lx\n",
4541 required_iflags);
4542 return true;
4543 }
4544
132e4608 4545 return !mnt_already_visible(ns, sb, new_mnt_flags);
8654df4e
EB
4546}
4547
380cf5ba
AL
4548bool mnt_may_suid(struct vfsmount *mnt)
4549{
4550 /*
4551 * Foreign mounts (accessed via fchdir or through /proc
4552 * symlinks) are always treated as if they are nosuid. This
4553 * prevents namespaces from trusting potentially unsafe
4554 * suid/sgid bits, file caps, or security labels that originate
4555 * in other namespaces.
4556 */
4557 return !(mnt->mnt_flags & MNT_NOSUID) && check_mnt(real_mount(mnt)) &&
4558 current_in_userns(mnt->mnt_sb->s_user_ns);
4559}
4560
64964528 4561static struct ns_common *mntns_get(struct task_struct *task)
8823c079 4562{
58be2825 4563 struct ns_common *ns = NULL;
8823c079
EB
4564 struct nsproxy *nsproxy;
4565
728dba3a
EB
4566 task_lock(task);
4567 nsproxy = task->nsproxy;
8823c079 4568 if (nsproxy) {
58be2825
AV
4569 ns = &nsproxy->mnt_ns->ns;
4570 get_mnt_ns(to_mnt_ns(ns));
8823c079 4571 }
728dba3a 4572 task_unlock(task);
8823c079
EB
4573
4574 return ns;
4575}
4576
64964528 4577static void mntns_put(struct ns_common *ns)
8823c079 4578{
58be2825 4579 put_mnt_ns(to_mnt_ns(ns));
8823c079
EB
4580}
4581
f2a8d52e 4582static int mntns_install(struct nsset *nsset, struct ns_common *ns)
8823c079 4583{
f2a8d52e
CB
4584 struct nsproxy *nsproxy = nsset->nsproxy;
4585 struct fs_struct *fs = nsset->fs;
4f757f3c 4586 struct mnt_namespace *mnt_ns = to_mnt_ns(ns), *old_mnt_ns;
f2a8d52e 4587 struct user_namespace *user_ns = nsset->cred->user_ns;
8823c079 4588 struct path root;
4f757f3c 4589 int err;
8823c079 4590
0c55cfc4 4591 if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) ||
f2a8d52e
CB
4592 !ns_capable(user_ns, CAP_SYS_CHROOT) ||
4593 !ns_capable(user_ns, CAP_SYS_ADMIN))
ae11e0f1 4594 return -EPERM;
8823c079 4595
74e83122
AV
4596 if (is_anon_ns(mnt_ns))
4597 return -EINVAL;
4598
8823c079
EB
4599 if (fs->users != 1)
4600 return -EINVAL;
4601
4602 get_mnt_ns(mnt_ns);
4f757f3c 4603 old_mnt_ns = nsproxy->mnt_ns;
8823c079
EB
4604 nsproxy->mnt_ns = mnt_ns;
4605
4606 /* Find the root */
4f757f3c
AV
4607 err = vfs_path_lookup(mnt_ns->root->mnt.mnt_root, &mnt_ns->root->mnt,
4608 "/", LOOKUP_DOWN, &root);
4609 if (err) {
4610 /* revert to old namespace */
4611 nsproxy->mnt_ns = old_mnt_ns;
4612 put_mnt_ns(mnt_ns);
4613 return err;
4614 }
8823c079 4615
4068367c
AV
4616 put_mnt_ns(old_mnt_ns);
4617
8823c079
EB
4618 /* Update the pwd and root */
4619 set_fs_pwd(fs, &root);
4620 set_fs_root(fs, &root);
4621
4622 path_put(&root);
4623 return 0;
4624}
4625
bcac25a5
AV
4626static struct user_namespace *mntns_owner(struct ns_common *ns)
4627{
4628 return to_mnt_ns(ns)->user_ns;
4629}
4630
8823c079
EB
4631const struct proc_ns_operations mntns_operations = {
4632 .name = "mnt",
4633 .type = CLONE_NEWNS,
4634 .get = mntns_get,
4635 .put = mntns_put,
4636 .install = mntns_install,
bcac25a5 4637 .owner = mntns_owner,
8823c079 4638};