xref: /linux/fs/overlayfs/util.c (revision 57fcb7d930d8f00f383e995aeebdcd2b416a187a)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2011 Novell Inc.
4  * Copyright (C) 2016 Red Hat, Inc.
5  */
6 
7 #include <linux/fs.h>
8 #include <linux/mount.h>
9 #include <linux/slab.h>
10 #include <linux/cred.h>
11 #include <linux/xattr.h>
12 #include <linux/exportfs.h>
13 #include <linux/file.h>
14 #include <linux/fileattr.h>
15 #include <linux/uuid.h>
16 #include <linux/namei.h>
17 #include <linux/ratelimit.h>
18 #include <linux/overflow.h>
19 #include "overlayfs.h"
20 
21 /* Get write access to upper mnt - may fail if upper sb was remounted ro */
ovl_get_write_access(struct dentry * dentry)22 int ovl_get_write_access(struct dentry *dentry)
23 {
24 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
25 	return mnt_get_write_access(ovl_upper_mnt(ofs));
26 }
27 
28 /* Get write access to upper sb - may block if upper sb is frozen */
ovl_start_write(struct dentry * dentry)29 void ovl_start_write(struct dentry *dentry)
30 {
31 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
32 	sb_start_write(ovl_upper_mnt(ofs)->mnt_sb);
33 }
34 
ovl_want_write(struct dentry * dentry)35 int ovl_want_write(struct dentry *dentry)
36 {
37 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
38 	return mnt_want_write(ovl_upper_mnt(ofs));
39 }
40 
ovl_put_write_access(struct dentry * dentry)41 void ovl_put_write_access(struct dentry *dentry)
42 {
43 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
44 	mnt_put_write_access(ovl_upper_mnt(ofs));
45 }
46 
ovl_end_write(struct dentry * dentry)47 void ovl_end_write(struct dentry *dentry)
48 {
49 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
50 	sb_end_write(ovl_upper_mnt(ofs)->mnt_sb);
51 }
52 
ovl_drop_write(struct dentry * dentry)53 void ovl_drop_write(struct dentry *dentry)
54 {
55 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
56 	mnt_drop_write(ovl_upper_mnt(ofs));
57 }
58 
ovl_workdir(struct dentry * dentry)59 struct dentry *ovl_workdir(struct dentry *dentry)
60 {
61 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
62 	return ofs->workdir;
63 }
64 
ovl_override_creds(struct super_block * sb)65 const struct cred *ovl_override_creds(struct super_block *sb)
66 {
67 	struct ovl_fs *ofs = OVL_FS(sb);
68 
69 	return override_creds(ofs->creator_cred);
70 }
71 
ovl_revert_creds(const struct cred * old_cred)72 void ovl_revert_creds(const struct cred *old_cred)
73 {
74 	revert_creds(old_cred);
75 }
76 
77 /*
78  * Check if underlying fs supports file handles and try to determine encoding
79  * type, in order to deduce maximum inode number used by fs.
80  *
81  * Return 0 if file handles are not supported.
82  * Return 1 (FILEID_INO32_GEN) if fs uses the default 32bit inode encoding.
83  * Return -1 if fs uses a non default encoding with unknown inode size.
84  */
ovl_can_decode_fh(struct super_block * sb)85 int ovl_can_decode_fh(struct super_block *sb)
86 {
87 	if (!capable(CAP_DAC_READ_SEARCH))
88 		return 0;
89 
90 	if (!exportfs_can_decode_fh(sb->s_export_op))
91 		return 0;
92 
93 	return sb->s_export_op->encode_fh ? -1 : FILEID_INO32_GEN;
94 }
95 
ovl_indexdir(struct super_block * sb)96 struct dentry *ovl_indexdir(struct super_block *sb)
97 {
98 	struct ovl_fs *ofs = OVL_FS(sb);
99 
100 	return ofs->config.index ? ofs->workdir : NULL;
101 }
102 
103 /* Index all files on copy up. For now only enabled for NFS export */
ovl_index_all(struct super_block * sb)104 bool ovl_index_all(struct super_block *sb)
105 {
106 	struct ovl_fs *ofs = OVL_FS(sb);
107 
108 	return ofs->config.nfs_export && ofs->config.index;
109 }
110 
111 /* Verify lower origin on lookup. For now only enabled for NFS export */
ovl_verify_lower(struct super_block * sb)112 bool ovl_verify_lower(struct super_block *sb)
113 {
114 	struct ovl_fs *ofs = OVL_FS(sb);
115 
116 	return ofs->config.nfs_export && ofs->config.index;
117 }
118 
ovl_stack_alloc(unsigned int n)119 struct ovl_path *ovl_stack_alloc(unsigned int n)
120 {
121 	return kcalloc(n, sizeof(struct ovl_path), GFP_KERNEL);
122 }
123 
ovl_stack_cpy(struct ovl_path * dst,struct ovl_path * src,unsigned int n)124 void ovl_stack_cpy(struct ovl_path *dst, struct ovl_path *src, unsigned int n)
125 {
126 	unsigned int i;
127 
128 	memcpy(dst, src, sizeof(struct ovl_path) * n);
129 	for (i = 0; i < n; i++)
130 		dget(src[i].dentry);
131 }
132 
ovl_stack_put(struct ovl_path * stack,unsigned int n)133 void ovl_stack_put(struct ovl_path *stack, unsigned int n)
134 {
135 	unsigned int i;
136 
137 	for (i = 0; stack && i < n; i++)
138 		dput(stack[i].dentry);
139 }
140 
ovl_stack_free(struct ovl_path * stack,unsigned int n)141 void ovl_stack_free(struct ovl_path *stack, unsigned int n)
142 {
143 	ovl_stack_put(stack, n);
144 	kfree(stack);
145 }
146 
ovl_alloc_entry(unsigned int numlower)147 struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
148 {
149 	struct ovl_entry *oe;
150 
151 	oe = kzalloc(struct_size(oe, __lowerstack, numlower), GFP_KERNEL);
152 	if (oe)
153 		oe->__numlower = numlower;
154 
155 	return oe;
156 }
157 
ovl_free_entry(struct ovl_entry * oe)158 void ovl_free_entry(struct ovl_entry *oe)
159 {
160 	ovl_stack_put(ovl_lowerstack(oe), ovl_numlower(oe));
161 	kfree(oe);
162 }
163 
164 #define OVL_D_REVALIDATE (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE)
165 
ovl_dentry_remote(struct dentry * dentry)166 bool ovl_dentry_remote(struct dentry *dentry)
167 {
168 	return dentry->d_flags & OVL_D_REVALIDATE;
169 }
170 
ovl_dentry_update_reval(struct dentry * dentry,struct dentry * realdentry)171 void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *realdentry)
172 {
173 	if (!ovl_dentry_remote(realdentry))
174 		return;
175 
176 	spin_lock(&dentry->d_lock);
177 	dentry->d_flags |= realdentry->d_flags & OVL_D_REVALIDATE;
178 	spin_unlock(&dentry->d_lock);
179 }
180 
ovl_dentry_init_reval(struct dentry * dentry,struct dentry * upperdentry,struct ovl_entry * oe)181 void ovl_dentry_init_reval(struct dentry *dentry, struct dentry *upperdentry,
182 			   struct ovl_entry *oe)
183 {
184 	return ovl_dentry_init_flags(dentry, upperdentry, oe, OVL_D_REVALIDATE);
185 }
186 
ovl_dentry_init_flags(struct dentry * dentry,struct dentry * upperdentry,struct ovl_entry * oe,unsigned int mask)187 void ovl_dentry_init_flags(struct dentry *dentry, struct dentry *upperdentry,
188 			   struct ovl_entry *oe, unsigned int mask)
189 {
190 	struct ovl_path *lowerstack = ovl_lowerstack(oe);
191 	unsigned int i, flags = 0;
192 
193 	if (upperdentry)
194 		flags |= upperdentry->d_flags;
195 	for (i = 0; i < ovl_numlower(oe) && lowerstack[i].dentry; i++)
196 		flags |= lowerstack[i].dentry->d_flags;
197 
198 	spin_lock(&dentry->d_lock);
199 	dentry->d_flags &= ~mask;
200 	dentry->d_flags |= flags & mask;
201 	spin_unlock(&dentry->d_lock);
202 }
203 
ovl_dentry_weird(struct dentry * dentry)204 bool ovl_dentry_weird(struct dentry *dentry)
205 {
206 	if (!d_can_lookup(dentry) && !d_is_file(dentry) && !d_is_symlink(dentry))
207 		return true;
208 
209 	if (dentry->d_flags & (DCACHE_NEED_AUTOMOUNT | DCACHE_MANAGE_TRANSIT))
210 		return true;
211 
212 	/*
213 	 * Allow filesystems that are case-folding capable but deny composing
214 	 * ovl stack from case-folded directories.
215 	 */
216 	if (sb_has_encoding(dentry->d_sb))
217 		return IS_CASEFOLDED(d_inode(dentry));
218 
219 	return dentry->d_flags & (DCACHE_OP_HASH | DCACHE_OP_COMPARE);
220 }
221 
ovl_path_type(struct dentry * dentry)222 enum ovl_path_type ovl_path_type(struct dentry *dentry)
223 {
224 	struct ovl_entry *oe = OVL_E(dentry);
225 	enum ovl_path_type type = 0;
226 
227 	if (ovl_dentry_upper(dentry)) {
228 		type = __OVL_PATH_UPPER;
229 
230 		/*
231 		 * Non-dir dentry can hold lower dentry of its copy up origin.
232 		 */
233 		if (ovl_numlower(oe)) {
234 			if (ovl_test_flag(OVL_CONST_INO, d_inode(dentry)))
235 				type |= __OVL_PATH_ORIGIN;
236 			if (d_is_dir(dentry) ||
237 			    !ovl_has_upperdata(d_inode(dentry)))
238 				type |= __OVL_PATH_MERGE;
239 		}
240 	} else {
241 		if (ovl_numlower(oe) > 1)
242 			type |= __OVL_PATH_MERGE;
243 	}
244 	return type;
245 }
246 
ovl_path_upper(struct dentry * dentry,struct path * path)247 void ovl_path_upper(struct dentry *dentry, struct path *path)
248 {
249 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
250 
251 	path->mnt = ovl_upper_mnt(ofs);
252 	path->dentry = ovl_dentry_upper(dentry);
253 }
254 
ovl_path_lower(struct dentry * dentry,struct path * path)255 void ovl_path_lower(struct dentry *dentry, struct path *path)
256 {
257 	struct ovl_entry *oe = OVL_E(dentry);
258 	struct ovl_path *lowerpath = ovl_lowerstack(oe);
259 
260 	if (ovl_numlower(oe)) {
261 		path->mnt = lowerpath->layer->mnt;
262 		path->dentry = lowerpath->dentry;
263 	} else {
264 		*path = (struct path) { };
265 	}
266 }
267 
ovl_path_lowerdata(struct dentry * dentry,struct path * path)268 void ovl_path_lowerdata(struct dentry *dentry, struct path *path)
269 {
270 	struct ovl_entry *oe = OVL_E(dentry);
271 	struct ovl_path *lowerdata = ovl_lowerdata(oe);
272 	struct dentry *lowerdata_dentry = ovl_lowerdata_dentry(oe);
273 
274 	if (lowerdata_dentry) {
275 		path->dentry = lowerdata_dentry;
276 		/*
277 		 * Pairs with smp_wmb() in ovl_dentry_set_lowerdata().
278 		 * Make sure that if lowerdata->dentry is visible, then
279 		 * datapath->layer is visible as well.
280 		 */
281 		smp_rmb();
282 		path->mnt = READ_ONCE(lowerdata->layer)->mnt;
283 	} else {
284 		*path = (struct path) { };
285 	}
286 }
287 
ovl_path_real(struct dentry * dentry,struct path * path)288 enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
289 {
290 	enum ovl_path_type type = ovl_path_type(dentry);
291 
292 	if (!OVL_TYPE_UPPER(type))
293 		ovl_path_lower(dentry, path);
294 	else
295 		ovl_path_upper(dentry, path);
296 
297 	return type;
298 }
299 
ovl_path_realdata(struct dentry * dentry,struct path * path)300 enum ovl_path_type ovl_path_realdata(struct dentry *dentry, struct path *path)
301 {
302 	enum ovl_path_type type = ovl_path_type(dentry);
303 
304 	WARN_ON_ONCE(d_is_dir(dentry));
305 
306 	if (!OVL_TYPE_UPPER(type) || OVL_TYPE_MERGE(type))
307 		ovl_path_lowerdata(dentry, path);
308 	else
309 		ovl_path_upper(dentry, path);
310 
311 	return type;
312 }
313 
ovl_dentry_upper(struct dentry * dentry)314 struct dentry *ovl_dentry_upper(struct dentry *dentry)
315 {
316 	struct inode *inode = d_inode(dentry);
317 
318 	return inode ? ovl_upperdentry_dereference(OVL_I(inode)) : NULL;
319 }
320 
ovl_dentry_lower(struct dentry * dentry)321 struct dentry *ovl_dentry_lower(struct dentry *dentry)
322 {
323 	struct ovl_entry *oe = OVL_E(dentry);
324 
325 	return ovl_numlower(oe) ? ovl_lowerstack(oe)->dentry : NULL;
326 }
327 
ovl_layer_lower(struct dentry * dentry)328 const struct ovl_layer *ovl_layer_lower(struct dentry *dentry)
329 {
330 	struct ovl_entry *oe = OVL_E(dentry);
331 
332 	return ovl_numlower(oe) ? ovl_lowerstack(oe)->layer : NULL;
333 }
334 
335 /*
336  * ovl_dentry_lower() could return either a data dentry or metacopy dentry
337  * depending on what is stored in lowerstack[0]. At times we need to find
338  * lower dentry which has data (and not metacopy dentry). This helper
339  * returns the lower data dentry.
340  */
ovl_dentry_lowerdata(struct dentry * dentry)341 struct dentry *ovl_dentry_lowerdata(struct dentry *dentry)
342 {
343 	return ovl_lowerdata_dentry(OVL_E(dentry));
344 }
345 
ovl_dentry_set_lowerdata(struct dentry * dentry,struct ovl_path * datapath)346 int ovl_dentry_set_lowerdata(struct dentry *dentry, struct ovl_path *datapath)
347 {
348 	struct ovl_entry *oe = OVL_E(dentry);
349 	struct ovl_path *lowerdata = ovl_lowerdata(oe);
350 	struct dentry *datadentry = datapath->dentry;
351 
352 	if (WARN_ON_ONCE(ovl_numlower(oe) <= 1))
353 		return -EIO;
354 
355 	WRITE_ONCE(lowerdata->layer, datapath->layer);
356 	/*
357 	 * Pairs with smp_rmb() in ovl_path_lowerdata().
358 	 * Make sure that if lowerdata->dentry is visible, then
359 	 * lowerdata->layer is visible as well.
360 	 */
361 	smp_wmb();
362 	WRITE_ONCE(lowerdata->dentry, dget(datadentry));
363 
364 	ovl_dentry_update_reval(dentry, datadentry);
365 
366 	return 0;
367 }
368 
ovl_dentry_real(struct dentry * dentry)369 struct dentry *ovl_dentry_real(struct dentry *dentry)
370 {
371 	return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
372 }
373 
ovl_i_dentry_upper(struct inode * inode)374 struct dentry *ovl_i_dentry_upper(struct inode *inode)
375 {
376 	return ovl_upperdentry_dereference(OVL_I(inode));
377 }
378 
ovl_i_path_real(struct inode * inode,struct path * path)379 struct inode *ovl_i_path_real(struct inode *inode, struct path *path)
380 {
381 	struct ovl_path *lowerpath = ovl_lowerpath(OVL_I_E(inode));
382 
383 	path->dentry = ovl_i_dentry_upper(inode);
384 	if (!path->dentry) {
385 		path->dentry = lowerpath->dentry;
386 		path->mnt = lowerpath->layer->mnt;
387 	} else {
388 		path->mnt = ovl_upper_mnt(OVL_FS(inode->i_sb));
389 	}
390 
391 	return path->dentry ? d_inode_rcu(path->dentry) : NULL;
392 }
393 
ovl_inode_upper(struct inode * inode)394 struct inode *ovl_inode_upper(struct inode *inode)
395 {
396 	struct dentry *upperdentry = ovl_i_dentry_upper(inode);
397 
398 	return upperdentry ? d_inode(upperdentry) : NULL;
399 }
400 
ovl_inode_lower(struct inode * inode)401 struct inode *ovl_inode_lower(struct inode *inode)
402 {
403 	struct ovl_path *lowerpath = ovl_lowerpath(OVL_I_E(inode));
404 
405 	return lowerpath ? d_inode(lowerpath->dentry) : NULL;
406 }
407 
ovl_inode_real(struct inode * inode)408 struct inode *ovl_inode_real(struct inode *inode)
409 {
410 	return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
411 }
412 
413 /* Return inode which contains lower data. Do not return metacopy */
ovl_inode_lowerdata(struct inode * inode)414 struct inode *ovl_inode_lowerdata(struct inode *inode)
415 {
416 	struct dentry *lowerdata = ovl_lowerdata_dentry(OVL_I_E(inode));
417 
418 	if (WARN_ON(!S_ISREG(inode->i_mode)))
419 		return NULL;
420 
421 	return lowerdata ? d_inode(lowerdata) : NULL;
422 }
423 
424 /* Return real inode which contains data. Does not return metacopy inode */
ovl_inode_realdata(struct inode * inode)425 struct inode *ovl_inode_realdata(struct inode *inode)
426 {
427 	struct inode *upperinode;
428 
429 	upperinode = ovl_inode_upper(inode);
430 	if (upperinode && ovl_has_upperdata(inode))
431 		return upperinode;
432 
433 	return ovl_inode_lowerdata(inode);
434 }
435 
ovl_lowerdata_redirect(struct inode * inode)436 const char *ovl_lowerdata_redirect(struct inode *inode)
437 {
438 	return inode && S_ISREG(inode->i_mode) ?
439 		OVL_I(inode)->lowerdata_redirect : NULL;
440 }
441 
ovl_dir_cache(struct inode * inode)442 struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
443 {
444 	return inode && S_ISDIR(inode->i_mode) ? OVL_I(inode)->cache : NULL;
445 }
446 
ovl_set_dir_cache(struct inode * inode,struct ovl_dir_cache * cache)447 void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
448 {
449 	OVL_I(inode)->cache = cache;
450 }
451 
ovl_dentry_set_flag(unsigned long flag,struct dentry * dentry)452 void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry)
453 {
454 	set_bit(flag, OVL_E_FLAGS(dentry));
455 }
456 
ovl_dentry_clear_flag(unsigned long flag,struct dentry * dentry)457 void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry)
458 {
459 	clear_bit(flag, OVL_E_FLAGS(dentry));
460 }
461 
ovl_dentry_test_flag(unsigned long flag,struct dentry * dentry)462 bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry)
463 {
464 	return test_bit(flag, OVL_E_FLAGS(dentry));
465 }
466 
ovl_dentry_is_opaque(struct dentry * dentry)467 bool ovl_dentry_is_opaque(struct dentry *dentry)
468 {
469 	return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry);
470 }
471 
ovl_dentry_is_whiteout(struct dentry * dentry)472 bool ovl_dentry_is_whiteout(struct dentry *dentry)
473 {
474 	return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
475 }
476 
ovl_dentry_set_opaque(struct dentry * dentry)477 void ovl_dentry_set_opaque(struct dentry *dentry)
478 {
479 	ovl_dentry_set_flag(OVL_E_OPAQUE, dentry);
480 }
481 
ovl_dentry_has_xwhiteouts(struct dentry * dentry)482 bool ovl_dentry_has_xwhiteouts(struct dentry *dentry)
483 {
484 	return ovl_dentry_test_flag(OVL_E_XWHITEOUTS, dentry);
485 }
486 
ovl_dentry_set_xwhiteouts(struct dentry * dentry)487 void ovl_dentry_set_xwhiteouts(struct dentry *dentry)
488 {
489 	ovl_dentry_set_flag(OVL_E_XWHITEOUTS, dentry);
490 }
491 
492 /*
493  * ovl_layer_set_xwhiteouts() is called before adding the overlay dir
494  * dentry to dcache, while readdir of that same directory happens after
495  * the overlay dir dentry is in dcache, so if some cpu observes that
496  * ovl_dentry_is_xwhiteouts(), it will also observe layer->has_xwhiteouts
497  * for the layers where xwhiteouts marker was found in that merge dir.
498  */
ovl_layer_set_xwhiteouts(struct ovl_fs * ofs,const struct ovl_layer * layer)499 void ovl_layer_set_xwhiteouts(struct ovl_fs *ofs,
500 			      const struct ovl_layer *layer)
501 {
502 	if (layer->has_xwhiteouts)
503 		return;
504 
505 	/* Write once to read-mostly layer properties */
506 	ofs->layers[layer->idx].has_xwhiteouts = true;
507 }
508 
509 /*
510  * For hard links and decoded file handles, it's possible for ovl_dentry_upper()
511  * to return positive, while there's no actual upper alias for the inode.
512  * Copy up code needs to know about the existence of the upper alias, so it
513  * can't use ovl_dentry_upper().
514  */
ovl_dentry_has_upper_alias(struct dentry * dentry)515 bool ovl_dentry_has_upper_alias(struct dentry *dentry)
516 {
517 	return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry);
518 }
519 
ovl_dentry_set_upper_alias(struct dentry * dentry)520 void ovl_dentry_set_upper_alias(struct dentry *dentry)
521 {
522 	ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry);
523 }
524 
ovl_should_check_upperdata(struct inode * inode)525 static bool ovl_should_check_upperdata(struct inode *inode)
526 {
527 	if (!S_ISREG(inode->i_mode))
528 		return false;
529 
530 	if (!ovl_inode_lower(inode))
531 		return false;
532 
533 	return true;
534 }
535 
ovl_has_upperdata(struct inode * inode)536 bool ovl_has_upperdata(struct inode *inode)
537 {
538 	if (!ovl_should_check_upperdata(inode))
539 		return true;
540 
541 	if (!ovl_test_flag(OVL_UPPERDATA, inode))
542 		return false;
543 	/*
544 	 * Pairs with smp_wmb() in ovl_set_upperdata(). Main user of
545 	 * ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure
546 	 * if setting of OVL_UPPERDATA is visible, then effects of writes
547 	 * before that are visible too.
548 	 */
549 	smp_rmb();
550 	return true;
551 }
552 
ovl_set_upperdata(struct inode * inode)553 void ovl_set_upperdata(struct inode *inode)
554 {
555 	/*
556 	 * Pairs with smp_rmb() in ovl_has_upperdata(). Make sure
557 	 * if OVL_UPPERDATA flag is visible, then effects of write operations
558 	 * before it are visible as well.
559 	 */
560 	smp_wmb();
561 	ovl_set_flag(OVL_UPPERDATA, inode);
562 }
563 
564 /* Caller should hold ovl_inode->lock */
ovl_dentry_needs_data_copy_up_locked(struct dentry * dentry,int flags)565 bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags)
566 {
567 	if (!ovl_open_flags_need_copy_up(flags))
568 		return false;
569 
570 	return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry));
571 }
572 
ovl_dentry_needs_data_copy_up(struct dentry * dentry,int flags)573 bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags)
574 {
575 	if (!ovl_open_flags_need_copy_up(flags))
576 		return false;
577 
578 	return !ovl_has_upperdata(d_inode(dentry));
579 }
580 
ovl_dentry_get_redirect(struct dentry * dentry)581 const char *ovl_dentry_get_redirect(struct dentry *dentry)
582 {
583 	return OVL_I(d_inode(dentry))->redirect;
584 }
585 
ovl_dentry_set_redirect(struct dentry * dentry,const char * redirect)586 void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
587 {
588 	struct ovl_inode *oi = OVL_I(d_inode(dentry));
589 
590 	kfree(oi->redirect);
591 	oi->redirect = redirect;
592 }
593 
ovl_inode_update(struct inode * inode,struct dentry * upperdentry)594 void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
595 {
596 	struct inode *upperinode = d_inode(upperdentry);
597 
598 	WARN_ON(OVL_I(inode)->__upperdentry);
599 
600 	/*
601 	 * Make sure upperdentry is consistent before making it visible
602 	 */
603 	smp_wmb();
604 	OVL_I(inode)->__upperdentry = upperdentry;
605 	if (inode_unhashed(inode)) {
606 		inode->i_private = upperinode;
607 		__insert_inode_hash(inode, (unsigned long) upperinode);
608 	}
609 }
610 
ovl_dir_version_inc(struct dentry * dentry,bool impurity)611 static void ovl_dir_version_inc(struct dentry *dentry, bool impurity)
612 {
613 	struct inode *inode = d_inode(dentry);
614 
615 	WARN_ON(!inode_is_locked(inode));
616 	WARN_ON(!d_is_dir(dentry));
617 	/*
618 	 * Version is used by readdir code to keep cache consistent.
619 	 * For merge dirs (or dirs with origin) all changes need to be noted.
620 	 * For non-merge dirs, cache contains only impure entries (i.e. ones
621 	 * which have been copied up and have origins), so only need to note
622 	 * changes to impure entries.
623 	 */
624 	if (!ovl_dir_is_real(inode) || impurity)
625 		OVL_I(inode)->version++;
626 }
627 
ovl_dir_modified(struct dentry * dentry,bool impurity)628 void ovl_dir_modified(struct dentry *dentry, bool impurity)
629 {
630 	/* Copy mtime/ctime */
631 	ovl_copyattr(d_inode(dentry));
632 
633 	ovl_dir_version_inc(dentry, impurity);
634 }
635 
ovl_inode_version_get(struct inode * inode)636 u64 ovl_inode_version_get(struct inode *inode)
637 {
638 	WARN_ON(!inode_is_locked(inode));
639 	return OVL_I(inode)->version;
640 }
641 
ovl_is_whiteout(struct dentry * dentry)642 bool ovl_is_whiteout(struct dentry *dentry)
643 {
644 	struct inode *inode = dentry->d_inode;
645 
646 	return inode && IS_WHITEOUT(inode);
647 }
648 
649 /*
650  * Use this over ovl_is_whiteout for upper and lower files, as it also
651  * handles overlay.whiteout xattr whiteout files.
652  */
ovl_path_is_whiteout(struct ovl_fs * ofs,const struct path * path)653 bool ovl_path_is_whiteout(struct ovl_fs *ofs, const struct path *path)
654 {
655 	return ovl_is_whiteout(path->dentry) ||
656 		ovl_path_check_xwhiteout_xattr(ofs, path);
657 }
658 
ovl_path_open(const struct path * path,int flags)659 struct file *ovl_path_open(const struct path *path, int flags)
660 {
661 	struct inode *inode = d_inode(path->dentry);
662 	struct mnt_idmap *real_idmap = mnt_idmap(path->mnt);
663 	int err, acc_mode;
664 
665 	if (flags & ~(O_ACCMODE | O_LARGEFILE))
666 		BUG();
667 
668 	switch (flags & O_ACCMODE) {
669 	case O_RDONLY:
670 		acc_mode = MAY_READ;
671 		break;
672 	case O_WRONLY:
673 		acc_mode = MAY_WRITE;
674 		break;
675 	default:
676 		BUG();
677 	}
678 
679 	err = inode_permission(real_idmap, inode, acc_mode | MAY_OPEN);
680 	if (err)
681 		return ERR_PTR(err);
682 
683 	/* O_NOATIME is an optimization, don't fail if not permitted */
684 	if (inode_owner_or_capable(real_idmap, inode))
685 		flags |= O_NOATIME;
686 
687 	return dentry_open(path, flags, current_cred());
688 }
689 
690 /* Caller should hold ovl_inode->lock */
ovl_already_copied_up_locked(struct dentry * dentry,int flags)691 static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags)
692 {
693 	bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
694 
695 	if (ovl_dentry_upper(dentry) &&
696 	    (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
697 	    !ovl_dentry_needs_data_copy_up_locked(dentry, flags))
698 		return true;
699 
700 	return false;
701 }
702 
ovl_already_copied_up(struct dentry * dentry,int flags)703 bool ovl_already_copied_up(struct dentry *dentry, int flags)
704 {
705 	bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
706 
707 	/*
708 	 * Check if copy-up has happened as well as for upper alias (in
709 	 * case of hard links) is there.
710 	 *
711 	 * Both checks are lockless:
712 	 *  - false negatives: will recheck under oi->lock
713 	 *  - false positives:
714 	 *    + ovl_dentry_upper() uses memory barriers to ensure the
715 	 *      upper dentry is up-to-date
716 	 *    + ovl_dentry_has_upper_alias() relies on locking of
717 	 *      upper parent i_rwsem to prevent reordering copy-up
718 	 *      with rename.
719 	 */
720 	if (ovl_dentry_upper(dentry) &&
721 	    (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
722 	    !ovl_dentry_needs_data_copy_up(dentry, flags))
723 		return true;
724 
725 	return false;
726 }
727 
728 /*
729  * The copy up "transaction" keeps an elevated mnt write count on upper mnt,
730  * but leaves taking freeze protection on upper sb to lower level helpers.
731  */
ovl_copy_up_start(struct dentry * dentry,int flags)732 int ovl_copy_up_start(struct dentry *dentry, int flags)
733 {
734 	struct inode *inode = d_inode(dentry);
735 	int err;
736 
737 	err = ovl_inode_lock_interruptible(inode);
738 	if (err)
739 		return err;
740 
741 	if (ovl_already_copied_up_locked(dentry, flags))
742 		err = 1; /* Already copied up */
743 	else
744 		err = ovl_get_write_access(dentry);
745 	if (err)
746 		goto out_unlock;
747 
748 	return 0;
749 
750 out_unlock:
751 	ovl_inode_unlock(inode);
752 	return err;
753 }
754 
ovl_copy_up_end(struct dentry * dentry)755 void ovl_copy_up_end(struct dentry *dentry)
756 {
757 	ovl_put_write_access(dentry);
758 	ovl_inode_unlock(d_inode(dentry));
759 }
760 
ovl_path_check_origin_xattr(struct ovl_fs * ofs,const struct path * path)761 bool ovl_path_check_origin_xattr(struct ovl_fs *ofs, const struct path *path)
762 {
763 	int res;
764 
765 	res = ovl_path_getxattr(ofs, path, OVL_XATTR_ORIGIN, NULL, 0);
766 
767 	/* Zero size value means "copied up but origin unknown" */
768 	if (res >= 0)
769 		return true;
770 
771 	return false;
772 }
773 
ovl_path_check_xwhiteout_xattr(struct ovl_fs * ofs,const struct path * path)774 bool ovl_path_check_xwhiteout_xattr(struct ovl_fs *ofs, const struct path *path)
775 {
776 	struct dentry *dentry = path->dentry;
777 	int res;
778 
779 	/* xattr.whiteout must be a zero size regular file */
780 	if (!d_is_reg(dentry) || i_size_read(d_inode(dentry)) != 0)
781 		return false;
782 
783 	res = ovl_path_getxattr(ofs, path, OVL_XATTR_XWHITEOUT, NULL, 0);
784 	return res >= 0;
785 }
786 
787 /*
788  * Load persistent uuid from xattr into s_uuid if found, or store a new
789  * random generated value in s_uuid and in xattr.
790  */
ovl_init_uuid_xattr(struct super_block * sb,struct ovl_fs * ofs,const struct path * upperpath)791 bool ovl_init_uuid_xattr(struct super_block *sb, struct ovl_fs *ofs,
792 			 const struct path *upperpath)
793 {
794 	bool set = false;
795 	uuid_t uuid;
796 	int res;
797 
798 	/* Try to load existing persistent uuid */
799 	res = ovl_path_getxattr(ofs, upperpath, OVL_XATTR_UUID, uuid.b,
800 				UUID_SIZE);
801 	if (res == UUID_SIZE)
802 		goto set_uuid;
803 
804 	if (res != -ENODATA)
805 		goto fail;
806 
807 	/*
808 	 * With uuid=auto, if uuid xattr is found, it will be used.
809 	 * If uuid xattrs is not found, generate a persistent uuid only on mount
810 	 * of new overlays where upper root dir is not yet marked as impure.
811 	 * An upper dir is marked as impure on copy up or lookup of its subdirs.
812 	 */
813 	if (ofs->config.uuid == OVL_UUID_AUTO) {
814 		res = ovl_path_getxattr(ofs, upperpath, OVL_XATTR_IMPURE, NULL,
815 					0);
816 		if (res > 0) {
817 			/* Any mount of old overlay - downgrade to uuid=null */
818 			ofs->config.uuid = OVL_UUID_NULL;
819 			return true;
820 		} else if (res == -ENODATA) {
821 			/* First mount of new overlay - upgrade to uuid=on */
822 			ofs->config.uuid = OVL_UUID_ON;
823 		} else if (res < 0) {
824 			goto fail;
825 		}
826 
827 	}
828 
829 	/* Generate overlay instance uuid */
830 	uuid_gen(&uuid);
831 
832 	/* Try to store persistent uuid */
833 	set = true;
834 	res = ovl_setxattr(ofs, upperpath->dentry, OVL_XATTR_UUID, uuid.b,
835 			   UUID_SIZE);
836 	if (res)
837 		goto fail;
838 
839 set_uuid:
840 	super_set_uuid(sb, uuid.b, sizeof(uuid));
841 	return true;
842 
843 fail:
844 	ofs->config.uuid = OVL_UUID_NULL;
845 	pr_warn("failed to %s uuid (%pd2, err=%i); falling back to uuid=null.\n",
846 		set ? "set" : "get", upperpath->dentry, res);
847 	return false;
848 }
849 
ovl_get_dir_xattr_val(struct ovl_fs * ofs,const struct path * path,enum ovl_xattr ox)850 char ovl_get_dir_xattr_val(struct ovl_fs *ofs, const struct path *path,
851 			   enum ovl_xattr ox)
852 {
853 	int res;
854 	char val;
855 
856 	if (!d_is_dir(path->dentry))
857 		return 0;
858 
859 	res = ovl_path_getxattr(ofs, path, ox, &val, 1);
860 	return res == 1 ? val : 0;
861 }
862 
863 #define OVL_XATTR_OPAQUE_POSTFIX	"opaque"
864 #define OVL_XATTR_REDIRECT_POSTFIX	"redirect"
865 #define OVL_XATTR_ORIGIN_POSTFIX	"origin"
866 #define OVL_XATTR_IMPURE_POSTFIX	"impure"
867 #define OVL_XATTR_NLINK_POSTFIX		"nlink"
868 #define OVL_XATTR_UPPER_POSTFIX		"upper"
869 #define OVL_XATTR_UUID_POSTFIX		"uuid"
870 #define OVL_XATTR_METACOPY_POSTFIX	"metacopy"
871 #define OVL_XATTR_PROTATTR_POSTFIX	"protattr"
872 #define OVL_XATTR_XWHITEOUT_POSTFIX	"whiteout"
873 
874 #define OVL_XATTR_TAB_ENTRY(x) \
875 	[x] = { [false] = OVL_XATTR_TRUSTED_PREFIX x ## _POSTFIX, \
876 		[true] = OVL_XATTR_USER_PREFIX x ## _POSTFIX }
877 
878 const char *const ovl_xattr_table[][2] = {
879 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_OPAQUE),
880 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_REDIRECT),
881 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_ORIGIN),
882 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_IMPURE),
883 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_NLINK),
884 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_UPPER),
885 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_UUID),
886 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_METACOPY),
887 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_PROTATTR),
888 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_XWHITEOUT),
889 };
890 
ovl_check_setxattr(struct ovl_fs * ofs,struct dentry * upperdentry,enum ovl_xattr ox,const void * value,size_t size,int xerr)891 int ovl_check_setxattr(struct ovl_fs *ofs, struct dentry *upperdentry,
892 		       enum ovl_xattr ox, const void *value, size_t size,
893 		       int xerr)
894 {
895 	int err;
896 
897 	if (ofs->noxattr)
898 		return xerr;
899 
900 	err = ovl_setxattr(ofs, upperdentry, ox, value, size);
901 
902 	if (err == -EOPNOTSUPP) {
903 		pr_warn("cannot set %s xattr on upper\n", ovl_xattr(ofs, ox));
904 		ofs->noxattr = true;
905 		return xerr;
906 	}
907 
908 	return err;
909 }
910 
ovl_set_impure(struct dentry * dentry,struct dentry * upperdentry)911 int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
912 {
913 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
914 	int err;
915 
916 	if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
917 		return 0;
918 
919 	/*
920 	 * Do not fail when upper doesn't support xattrs.
921 	 * Upper inodes won't have origin nor redirect xattr anyway.
922 	 */
923 	err = ovl_check_setxattr(ofs, upperdentry, OVL_XATTR_IMPURE, "y", 1, 0);
924 	if (!err)
925 		ovl_set_flag(OVL_IMPURE, d_inode(dentry));
926 
927 	return err;
928 }
929 
930 
931 #define OVL_PROTATTR_MAX 32 /* Reserved for future flags */
932 
ovl_check_protattr(struct inode * inode,struct dentry * upper)933 void ovl_check_protattr(struct inode *inode, struct dentry *upper)
934 {
935 	struct ovl_fs *ofs = OVL_FS(inode->i_sb);
936 	u32 iflags = inode->i_flags & OVL_PROT_I_FLAGS_MASK;
937 	char buf[OVL_PROTATTR_MAX+1];
938 	int res, n;
939 
940 	res = ovl_getxattr_upper(ofs, upper, OVL_XATTR_PROTATTR, buf,
941 				 OVL_PROTATTR_MAX);
942 	if (res < 0)
943 		return;
944 
945 	/*
946 	 * Initialize inode flags from overlay.protattr xattr and upper inode
947 	 * flags.  If upper inode has those fileattr flags set (i.e. from old
948 	 * kernel), we do not clear them on ovl_get_inode(), but we will clear
949 	 * them on next fileattr_set().
950 	 */
951 	for (n = 0; n < res; n++) {
952 		if (buf[n] == 'a')
953 			iflags |= S_APPEND;
954 		else if (buf[n] == 'i')
955 			iflags |= S_IMMUTABLE;
956 		else
957 			break;
958 	}
959 
960 	if (!res || n < res) {
961 		pr_warn_ratelimited("incompatible overlay.protattr format (%pd2, len=%d)\n",
962 				    upper, res);
963 	} else {
964 		inode_set_flags(inode, iflags, OVL_PROT_I_FLAGS_MASK);
965 	}
966 }
967 
ovl_set_protattr(struct inode * inode,struct dentry * upper,struct file_kattr * fa)968 int ovl_set_protattr(struct inode *inode, struct dentry *upper,
969 		      struct file_kattr *fa)
970 {
971 	struct ovl_fs *ofs = OVL_FS(inode->i_sb);
972 	char buf[OVL_PROTATTR_MAX];
973 	int len = 0, err = 0;
974 	u32 iflags = 0;
975 
976 	BUILD_BUG_ON(HWEIGHT32(OVL_PROT_FS_FLAGS_MASK) > OVL_PROTATTR_MAX);
977 
978 	if (fa->flags & FS_APPEND_FL) {
979 		buf[len++] = 'a';
980 		iflags |= S_APPEND;
981 	}
982 	if (fa->flags & FS_IMMUTABLE_FL) {
983 		buf[len++] = 'i';
984 		iflags |= S_IMMUTABLE;
985 	}
986 
987 	/*
988 	 * Do not allow to set protection flags when upper doesn't support
989 	 * xattrs, because we do not set those fileattr flags on upper inode.
990 	 * Remove xattr if it exist and all protection flags are cleared.
991 	 */
992 	if (len) {
993 		err = ovl_check_setxattr(ofs, upper, OVL_XATTR_PROTATTR,
994 					 buf, len, -EPERM);
995 	} else if (inode->i_flags & OVL_PROT_I_FLAGS_MASK) {
996 		err = ovl_removexattr(ofs, upper, OVL_XATTR_PROTATTR);
997 		if (err == -EOPNOTSUPP || err == -ENODATA)
998 			err = 0;
999 	}
1000 	if (err)
1001 		return err;
1002 
1003 	inode_set_flags(inode, iflags, OVL_PROT_I_FLAGS_MASK);
1004 
1005 	/* Mask out the fileattr flags that should not be set in upper inode */
1006 	fa->flags &= ~OVL_PROT_FS_FLAGS_MASK;
1007 	fa->fsx_xflags &= ~OVL_PROT_FSX_FLAGS_MASK;
1008 
1009 	return 0;
1010 }
1011 
1012 /*
1013  * Caller must hold a reference to inode to prevent it from being freed while
1014  * it is marked inuse.
1015  */
ovl_inuse_trylock(struct dentry * dentry)1016 bool ovl_inuse_trylock(struct dentry *dentry)
1017 {
1018 	struct inode *inode = d_inode(dentry);
1019 	bool locked = false;
1020 
1021 	spin_lock(&inode->i_lock);
1022 	if (!(inode->i_state & I_OVL_INUSE)) {
1023 		inode->i_state |= I_OVL_INUSE;
1024 		locked = true;
1025 	}
1026 	spin_unlock(&inode->i_lock);
1027 
1028 	return locked;
1029 }
1030 
ovl_inuse_unlock(struct dentry * dentry)1031 void ovl_inuse_unlock(struct dentry *dentry)
1032 {
1033 	if (dentry) {
1034 		struct inode *inode = d_inode(dentry);
1035 
1036 		spin_lock(&inode->i_lock);
1037 		WARN_ON(!(inode->i_state & I_OVL_INUSE));
1038 		inode->i_state &= ~I_OVL_INUSE;
1039 		spin_unlock(&inode->i_lock);
1040 	}
1041 }
1042 
ovl_is_inuse(struct dentry * dentry)1043 bool ovl_is_inuse(struct dentry *dentry)
1044 {
1045 	struct inode *inode = d_inode(dentry);
1046 	bool inuse;
1047 
1048 	spin_lock(&inode->i_lock);
1049 	inuse = (inode->i_state & I_OVL_INUSE);
1050 	spin_unlock(&inode->i_lock);
1051 
1052 	return inuse;
1053 }
1054 
1055 /*
1056  * Does this overlay dentry need to be indexed on copy up?
1057  */
ovl_need_index(struct dentry * dentry)1058 bool ovl_need_index(struct dentry *dentry)
1059 {
1060 	struct dentry *lower = ovl_dentry_lower(dentry);
1061 
1062 	if (!lower || !ovl_indexdir(dentry->d_sb))
1063 		return false;
1064 
1065 	/* Index all files for NFS export and consistency verification */
1066 	if (ovl_index_all(dentry->d_sb))
1067 		return true;
1068 
1069 	/* Index only lower hardlinks on copy up */
1070 	if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
1071 		return true;
1072 
1073 	return false;
1074 }
1075 
1076 /* Caller must hold OVL_I(inode)->lock */
ovl_cleanup_index(struct dentry * dentry)1077 static void ovl_cleanup_index(struct dentry *dentry)
1078 {
1079 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
1080 	struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
1081 	struct dentry *lowerdentry = ovl_dentry_lower(dentry);
1082 	struct dentry *upperdentry = ovl_dentry_upper(dentry);
1083 	struct dentry *index = NULL;
1084 	struct inode *inode;
1085 	struct qstr name = { };
1086 	bool got_write = false;
1087 	int err;
1088 
1089 	err = ovl_get_index_name(ofs, lowerdentry, &name);
1090 	if (err)
1091 		goto fail;
1092 
1093 	err = ovl_want_write(dentry);
1094 	if (err)
1095 		goto fail;
1096 
1097 	got_write = true;
1098 	inode = d_inode(upperdentry);
1099 	if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) {
1100 		pr_warn_ratelimited("cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
1101 				    upperdentry, inode->i_ino, inode->i_nlink);
1102 		/*
1103 		 * We either have a bug with persistent union nlink or a lower
1104 		 * hardlink was added while overlay is mounted. Adding a lower
1105 		 * hardlink and then unlinking all overlay hardlinks would drop
1106 		 * overlay nlink to zero before all upper inodes are unlinked.
1107 		 * As a safety measure, when that situation is detected, set
1108 		 * the overlay nlink to the index inode nlink minus one for the
1109 		 * index entry itself.
1110 		 */
1111 		set_nlink(d_inode(dentry), inode->i_nlink - 1);
1112 		ovl_set_nlink_upper(dentry);
1113 		goto out;
1114 	}
1115 
1116 	index = ovl_lookup_upper_unlocked(ofs, name.name, indexdir, name.len);
1117 	err = PTR_ERR(index);
1118 	if (IS_ERR(index)) {
1119 		index = NULL;
1120 	} else if (ovl_index_all(dentry->d_sb)) {
1121 		/* Whiteout orphan index to block future open by handle */
1122 		err = ovl_cleanup_and_whiteout(OVL_FS(dentry->d_sb),
1123 					       indexdir, index);
1124 	} else {
1125 		/* Cleanup orphan index entries */
1126 		err = ovl_cleanup(ofs, indexdir, index);
1127 	}
1128 	if (err)
1129 		goto fail;
1130 
1131 out:
1132 	if (got_write)
1133 		ovl_drop_write(dentry);
1134 	kfree(name.name);
1135 	dput(index);
1136 	return;
1137 
1138 fail:
1139 	pr_err("cleanup index of '%pd2' failed (%i)\n", dentry, err);
1140 	goto out;
1141 }
1142 
1143 /*
1144  * Operations that change overlay inode and upper inode nlink need to be
1145  * synchronized with copy up for persistent nlink accounting.
1146  */
ovl_nlink_start(struct dentry * dentry)1147 int ovl_nlink_start(struct dentry *dentry)
1148 {
1149 	struct inode *inode = d_inode(dentry);
1150 	const struct cred *old_cred;
1151 	int err;
1152 
1153 	if (WARN_ON(!inode))
1154 		return -ENOENT;
1155 
1156 	/*
1157 	 * With inodes index is enabled, we store the union overlay nlink
1158 	 * in an xattr on the index inode. When whiting out an indexed lower,
1159 	 * we need to decrement the overlay persistent nlink, but before the
1160 	 * first copy up, we have no upper index inode to store the xattr.
1161 	 *
1162 	 * As a workaround, before whiteout/rename over an indexed lower,
1163 	 * copy up to create the upper index. Creating the upper index will
1164 	 * initialize the overlay nlink, so it could be dropped if unlink
1165 	 * or rename succeeds.
1166 	 *
1167 	 * TODO: implement metadata only index copy up when called with
1168 	 *       ovl_copy_up_flags(dentry, O_PATH).
1169 	 */
1170 	if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) {
1171 		err = ovl_copy_up(dentry);
1172 		if (err)
1173 			return err;
1174 	}
1175 
1176 	err = ovl_inode_lock_interruptible(inode);
1177 	if (err)
1178 		return err;
1179 
1180 	err = ovl_want_write(dentry);
1181 	if (err)
1182 		goto out_unlock;
1183 
1184 	if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, inode))
1185 		return 0;
1186 
1187 	old_cred = ovl_override_creds(dentry->d_sb);
1188 	/*
1189 	 * The overlay inode nlink should be incremented/decremented IFF the
1190 	 * upper operation succeeds, along with nlink change of upper inode.
1191 	 * Therefore, before link/unlink/rename, we store the union nlink
1192 	 * value relative to the upper inode nlink in an upper inode xattr.
1193 	 */
1194 	err = ovl_set_nlink_upper(dentry);
1195 	ovl_revert_creds(old_cred);
1196 	if (err)
1197 		goto out_drop_write;
1198 
1199 	return 0;
1200 
1201 out_drop_write:
1202 	ovl_drop_write(dentry);
1203 out_unlock:
1204 	ovl_inode_unlock(inode);
1205 
1206 	return err;
1207 }
1208 
ovl_nlink_end(struct dentry * dentry)1209 void ovl_nlink_end(struct dentry *dentry)
1210 {
1211 	struct inode *inode = d_inode(dentry);
1212 
1213 	ovl_drop_write(dentry);
1214 
1215 	if (ovl_test_flag(OVL_INDEX, inode) && inode->i_nlink == 0) {
1216 		const struct cred *old_cred;
1217 
1218 		old_cred = ovl_override_creds(dentry->d_sb);
1219 		ovl_cleanup_index(dentry);
1220 		ovl_revert_creds(old_cred);
1221 	}
1222 
1223 	ovl_inode_unlock(inode);
1224 }
1225 
ovl_lock_rename_workdir(struct dentry * workdir,struct dentry * work,struct dentry * upperdir,struct dentry * upper)1226 int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *work,
1227 			    struct dentry *upperdir, struct dentry *upper)
1228 {
1229 	struct dentry *trap;
1230 
1231 	/* Workdir should not be subdir of upperdir and vice versa */
1232 	trap = lock_rename(workdir, upperdir);
1233 	if (IS_ERR(trap))
1234 		goto err;
1235 	if (trap)
1236 		goto err_unlock;
1237 	if (work && work->d_parent != workdir)
1238 		goto err_unlock;
1239 	if (upper && upper->d_parent != upperdir)
1240 		goto err_unlock;
1241 
1242 	return 0;
1243 
1244 err_unlock:
1245 	unlock_rename(workdir, upperdir);
1246 err:
1247 	pr_err("failed to lock workdir+upperdir\n");
1248 	return -EIO;
1249 }
1250 
1251 /*
1252  * err < 0, 0 if no metacopy xattr, metacopy data size if xattr found.
1253  * an empty xattr returns OVL_METACOPY_MIN_SIZE to distinguish from no xattr value.
1254  */
ovl_check_metacopy_xattr(struct ovl_fs * ofs,const struct path * path,struct ovl_metacopy * data)1255 int ovl_check_metacopy_xattr(struct ovl_fs *ofs, const struct path *path,
1256 			     struct ovl_metacopy *data)
1257 {
1258 	int res;
1259 
1260 	/* Only regular files can have metacopy xattr */
1261 	if (!S_ISREG(d_inode(path->dentry)->i_mode))
1262 		return 0;
1263 
1264 	res = ovl_path_getxattr(ofs, path, OVL_XATTR_METACOPY,
1265 				data, data ? OVL_METACOPY_MAX_SIZE : 0);
1266 	if (res < 0) {
1267 		if (res == -ENODATA || res == -EOPNOTSUPP)
1268 			return 0;
1269 		/*
1270 		 * getxattr on user.* may fail with EACCES in case there's no
1271 		 * read permission on the inode.  Not much we can do, other than
1272 		 * tell the caller that this is not a metacopy inode.
1273 		 */
1274 		if (ofs->config.userxattr && res == -EACCES)
1275 			return 0;
1276 		goto out;
1277 	}
1278 
1279 	if (res == 0) {
1280 		/* Emulate empty data for zero size metacopy xattr */
1281 		res = OVL_METACOPY_MIN_SIZE;
1282 		if (data) {
1283 			memset(data, 0, res);
1284 			data->len = res;
1285 		}
1286 	} else if (res < OVL_METACOPY_MIN_SIZE) {
1287 		pr_warn_ratelimited("metacopy file '%pd' has too small xattr\n",
1288 				    path->dentry);
1289 		return -EIO;
1290 	} else if (data) {
1291 		if (data->version != 0) {
1292 			pr_warn_ratelimited("metacopy file '%pd' has unsupported version\n",
1293 					    path->dentry);
1294 			return -EIO;
1295 		}
1296 		if (res != data->len) {
1297 			pr_warn_ratelimited("metacopy file '%pd' has invalid xattr size\n",
1298 					    path->dentry);
1299 			return -EIO;
1300 		}
1301 	}
1302 
1303 	return res;
1304 out:
1305 	pr_warn_ratelimited("failed to get metacopy (%i)\n", res);
1306 	return res;
1307 }
1308 
ovl_set_metacopy_xattr(struct ovl_fs * ofs,struct dentry * d,struct ovl_metacopy * metacopy)1309 int ovl_set_metacopy_xattr(struct ovl_fs *ofs, struct dentry *d, struct ovl_metacopy *metacopy)
1310 {
1311 	size_t len = metacopy->len;
1312 
1313 	/* If no flags or digest fall back to empty metacopy file */
1314 	if (metacopy->version == 0 && metacopy->flags == 0 && metacopy->digest_algo == 0)
1315 		len = 0;
1316 
1317 	return ovl_check_setxattr(ofs, d, OVL_XATTR_METACOPY,
1318 				  metacopy, len, -EOPNOTSUPP);
1319 }
1320 
ovl_is_metacopy_dentry(struct dentry * dentry)1321 bool ovl_is_metacopy_dentry(struct dentry *dentry)
1322 {
1323 	struct ovl_entry *oe = OVL_E(dentry);
1324 
1325 	if (!d_is_reg(dentry))
1326 		return false;
1327 
1328 	if (ovl_dentry_upper(dentry)) {
1329 		if (!ovl_has_upperdata(d_inode(dentry)))
1330 			return true;
1331 		return false;
1332 	}
1333 
1334 	return (ovl_numlower(oe) > 1);
1335 }
1336 
ovl_get_redirect_xattr(struct ovl_fs * ofs,const struct path * path,int padding)1337 char *ovl_get_redirect_xattr(struct ovl_fs *ofs, const struct path *path, int padding)
1338 {
1339 	int res;
1340 	char *s, *next, *buf = NULL;
1341 
1342 	res = ovl_path_getxattr(ofs, path, OVL_XATTR_REDIRECT, NULL, 0);
1343 	if (res == -ENODATA || res == -EOPNOTSUPP)
1344 		return NULL;
1345 	if (res < 0)
1346 		goto fail;
1347 	if (res == 0)
1348 		goto invalid;
1349 
1350 	buf = kzalloc(res + padding + 1, GFP_KERNEL);
1351 	if (!buf)
1352 		return ERR_PTR(-ENOMEM);
1353 
1354 	res = ovl_path_getxattr(ofs, path, OVL_XATTR_REDIRECT, buf, res);
1355 	if (res < 0)
1356 		goto fail;
1357 	if (res == 0)
1358 		goto invalid;
1359 
1360 	if (buf[0] == '/') {
1361 		for (s = buf; *s++ == '/'; s = next) {
1362 			next = strchrnul(s, '/');
1363 			if (s == next)
1364 				goto invalid;
1365 		}
1366 	} else {
1367 		if (strchr(buf, '/') != NULL)
1368 			goto invalid;
1369 	}
1370 
1371 	return buf;
1372 invalid:
1373 	pr_warn_ratelimited("invalid redirect (%s)\n", buf);
1374 	res = -EINVAL;
1375 	goto err_free;
1376 fail:
1377 	pr_warn_ratelimited("failed to get redirect (%i)\n", res);
1378 err_free:
1379 	kfree(buf);
1380 	return ERR_PTR(res);
1381 }
1382 
1383 /* Call with mounter creds as it may open the file */
ovl_ensure_verity_loaded(struct path * datapath)1384 int ovl_ensure_verity_loaded(struct path *datapath)
1385 {
1386 	struct inode *inode = d_inode(datapath->dentry);
1387 	struct file *filp;
1388 
1389 	if (!fsverity_active(inode) && IS_VERITY(inode)) {
1390 		/*
1391 		 * If this inode was not yet opened, the verity info hasn't been
1392 		 * loaded yet, so we need to do that here to force it into memory.
1393 		 */
1394 		filp = kernel_file_open(datapath, O_RDONLY, current_cred());
1395 		if (IS_ERR(filp))
1396 			return PTR_ERR(filp);
1397 		fput(filp);
1398 	}
1399 
1400 	return 0;
1401 }
1402 
ovl_validate_verity(struct ovl_fs * ofs,struct path * metapath,struct path * datapath)1403 int ovl_validate_verity(struct ovl_fs *ofs,
1404 			struct path *metapath,
1405 			struct path *datapath)
1406 {
1407 	struct ovl_metacopy metacopy_data;
1408 	u8 actual_digest[FS_VERITY_MAX_DIGEST_SIZE];
1409 	int xattr_digest_size, digest_size;
1410 	int xattr_size, err;
1411 	u8 verity_algo;
1412 
1413 	if (!ofs->config.verity_mode ||
1414 	    /* Verity only works on regular files */
1415 	    !S_ISREG(d_inode(metapath->dentry)->i_mode))
1416 		return 0;
1417 
1418 	xattr_size = ovl_check_metacopy_xattr(ofs, metapath, &metacopy_data);
1419 	if (xattr_size < 0)
1420 		return xattr_size;
1421 
1422 	if (!xattr_size || !metacopy_data.digest_algo) {
1423 		if (ofs->config.verity_mode == OVL_VERITY_REQUIRE) {
1424 			pr_warn_ratelimited("metacopy file '%pd' has no digest specified\n",
1425 					    metapath->dentry);
1426 			return -EIO;
1427 		}
1428 		return 0;
1429 	}
1430 
1431 	xattr_digest_size = ovl_metadata_digest_size(&metacopy_data);
1432 
1433 	err = ovl_ensure_verity_loaded(datapath);
1434 	if (err < 0) {
1435 		pr_warn_ratelimited("lower file '%pd' failed to load fs-verity info\n",
1436 				    datapath->dentry);
1437 		return -EIO;
1438 	}
1439 
1440 	digest_size = fsverity_get_digest(d_inode(datapath->dentry), actual_digest,
1441 					  &verity_algo, NULL);
1442 	if (digest_size == 0) {
1443 		pr_warn_ratelimited("lower file '%pd' has no fs-verity digest\n", datapath->dentry);
1444 		return -EIO;
1445 	}
1446 
1447 	if (xattr_digest_size != digest_size ||
1448 	    metacopy_data.digest_algo != verity_algo ||
1449 	    memcmp(metacopy_data.digest, actual_digest, xattr_digest_size) != 0) {
1450 		pr_warn_ratelimited("lower file '%pd' has the wrong fs-verity digest\n",
1451 				    datapath->dentry);
1452 		return -EIO;
1453 	}
1454 
1455 	return 0;
1456 }
1457 
ovl_get_verity_digest(struct ovl_fs * ofs,struct path * src,struct ovl_metacopy * metacopy)1458 int ovl_get_verity_digest(struct ovl_fs *ofs, struct path *src,
1459 			  struct ovl_metacopy *metacopy)
1460 {
1461 	int err, digest_size;
1462 
1463 	if (!ofs->config.verity_mode || !S_ISREG(d_inode(src->dentry)->i_mode))
1464 		return 0;
1465 
1466 	err = ovl_ensure_verity_loaded(src);
1467 	if (err < 0) {
1468 		pr_warn_ratelimited("lower file '%pd' failed to load fs-verity info\n",
1469 				    src->dentry);
1470 		return -EIO;
1471 	}
1472 
1473 	digest_size = fsverity_get_digest(d_inode(src->dentry),
1474 					  metacopy->digest, &metacopy->digest_algo, NULL);
1475 	if (digest_size == 0 ||
1476 	    WARN_ON_ONCE(digest_size > FS_VERITY_MAX_DIGEST_SIZE)) {
1477 		if (ofs->config.verity_mode == OVL_VERITY_REQUIRE) {
1478 			pr_warn_ratelimited("lower file '%pd' has no fs-verity digest\n",
1479 					    src->dentry);
1480 			return -EIO;
1481 		}
1482 		return 0;
1483 	}
1484 
1485 	metacopy->len += digest_size;
1486 	return 0;
1487 }
1488 
1489 /*
1490  * ovl_sync_status() - Check fs sync status for volatile mounts
1491  *
1492  * Returns 1 if this is not a volatile mount and a real sync is required.
1493  *
1494  * Returns 0 if syncing can be skipped because mount is volatile, and no errors
1495  * have occurred on the upperdir since the mount.
1496  *
1497  * Returns -errno if it is a volatile mount, and the error that occurred since
1498  * the last mount. If the error code changes, it'll return the latest error
1499  * code.
1500  */
1501 
ovl_sync_status(struct ovl_fs * ofs)1502 int ovl_sync_status(struct ovl_fs *ofs)
1503 {
1504 	struct vfsmount *mnt;
1505 
1506 	if (ovl_should_sync(ofs))
1507 		return 1;
1508 
1509 	mnt = ovl_upper_mnt(ofs);
1510 	if (!mnt)
1511 		return 0;
1512 
1513 	return errseq_check(&mnt->mnt_sb->s_wb_err, ofs->errseq);
1514 }
1515 
1516 /*
1517  * ovl_copyattr() - copy inode attributes from layer to ovl inode
1518  *
1519  * When overlay copies inode information from an upper or lower layer to the
1520  * relevant overlay inode it will apply the idmapping of the upper or lower
1521  * layer when doing so ensuring that the ovl inode ownership will correctly
1522  * reflect the ownership of the idmapped upper or lower layer. For example, an
1523  * idmapped upper or lower layer mapping id 1001 to id 1000 will take care to
1524  * map any lower or upper inode owned by id 1001 to id 1000. These mapping
1525  * helpers are nops when the relevant layer isn't idmapped.
1526  */
ovl_copyattr(struct inode * inode)1527 void ovl_copyattr(struct inode *inode)
1528 {
1529 	struct path realpath;
1530 	struct inode *realinode;
1531 	struct mnt_idmap *real_idmap;
1532 	vfsuid_t vfsuid;
1533 	vfsgid_t vfsgid;
1534 
1535 	realinode = ovl_i_path_real(inode, &realpath);
1536 	real_idmap = mnt_idmap(realpath.mnt);
1537 
1538 	spin_lock(&inode->i_lock);
1539 	vfsuid = i_uid_into_vfsuid(real_idmap, realinode);
1540 	vfsgid = i_gid_into_vfsgid(real_idmap, realinode);
1541 
1542 	inode->i_uid = vfsuid_into_kuid(vfsuid);
1543 	inode->i_gid = vfsgid_into_kgid(vfsgid);
1544 	inode->i_mode = realinode->i_mode;
1545 	inode_set_atime_to_ts(inode, inode_get_atime(realinode));
1546 	inode_set_mtime_to_ts(inode, inode_get_mtime(realinode));
1547 	inode_set_ctime_to_ts(inode, inode_get_ctime(realinode));
1548 	i_size_write(inode, i_size_read(realinode));
1549 	spin_unlock(&inode->i_lock);
1550 }
1551 
ovl_parent_lock(struct dentry * parent,struct dentry * child)1552 int ovl_parent_lock(struct dentry *parent, struct dentry *child)
1553 {
1554 	inode_lock_nested(parent->d_inode, I_MUTEX_PARENT);
1555 	if (!child || child->d_parent == parent)
1556 		return 0;
1557 
1558 	inode_unlock(parent->d_inode);
1559 	return -EINVAL;
1560 }
1561