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