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