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