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