• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *
4  * Copyright (C) 2011 Novell Inc.
5  */
6 
7 #include <linux/module.h>
8 #include <linux/fs.h>
9 #include <linux/slab.h>
10 #include <linux/file.h>
11 #include <linux/fileattr.h>
12 #include <linux/splice.h>
13 #include <linux/xattr.h>
14 #include <linux/security.h>
15 #include <linux/uaccess.h>
16 #include <linux/sched/signal.h>
17 #include <linux/cred.h>
18 #include <linux/namei.h>
19 #include <linux/fdtable.h>
20 #include <linux/ratelimit.h>
21 #include <linux/exportfs.h>
22 #include "overlayfs.h"
23 
24 #define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
25 
ovl_ccup_set(const char * buf,const struct kernel_param * param)26 static int ovl_ccup_set(const char *buf, const struct kernel_param *param)
27 {
28 	pr_warn("\"check_copy_up\" module option is obsolete\n");
29 	return 0;
30 }
31 
ovl_ccup_get(char * buf,const struct kernel_param * param)32 static int ovl_ccup_get(char *buf, const struct kernel_param *param)
33 {
34 	return sprintf(buf, "N\n");
35 }
36 
37 module_param_call(check_copy_up, ovl_ccup_set, ovl_ccup_get, NULL, 0644);
38 MODULE_PARM_DESC(check_copy_up, "Obsolete; does nothing");
39 
ovl_must_copy_xattr(const char * name)40 static bool ovl_must_copy_xattr(const char *name)
41 {
42 	return !strcmp(name, XATTR_POSIX_ACL_ACCESS) ||
43 	       !strcmp(name, XATTR_POSIX_ACL_DEFAULT) ||
44 	       !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN);
45 }
46 
ovl_copy_xattr(struct super_block * sb,const struct path * oldpath,struct dentry * new)47 int ovl_copy_xattr(struct super_block *sb, const struct path *oldpath, struct dentry *new)
48 {
49 	struct dentry *old = oldpath->dentry;
50 	ssize_t list_size, size, value_size = 0;
51 	char *buf, *name, *value = NULL;
52 	int error = 0;
53 	size_t slen;
54 
55 	if (!(old->d_inode->i_opflags & IOP_XATTR) ||
56 	    !(new->d_inode->i_opflags & IOP_XATTR))
57 		return 0;
58 
59 	list_size = vfs_listxattr(old, NULL, 0);
60 	if (list_size <= 0) {
61 		if (list_size == -EOPNOTSUPP)
62 			return 0;
63 		return list_size;
64 	}
65 
66 	buf = kvzalloc(list_size, GFP_KERNEL);
67 	if (!buf)
68 		return -ENOMEM;
69 
70 	list_size = vfs_listxattr(old, buf, list_size);
71 	if (list_size <= 0) {
72 		error = list_size;
73 		goto out;
74 	}
75 
76 	for (name = buf; list_size; name += slen) {
77 		slen = strnlen(name, list_size) + 1;
78 
79 		/* underlying fs providing us with an broken xattr list? */
80 		if (WARN_ON(slen > list_size)) {
81 			error = -EIO;
82 			break;
83 		}
84 		list_size -= slen;
85 
86 		if (ovl_is_private_xattr(sb, name))
87 			continue;
88 
89 		error = security_inode_copy_up_xattr(name);
90 		if (error < 0 && error != -EOPNOTSUPP)
91 			break;
92 		if (error == 1) {
93 			error = 0;
94 			continue; /* Discard */
95 		}
96 retry:
97 		size = ovl_do_getxattr(oldpath, name, value, value_size);
98 		if (size == -ERANGE)
99 			size = ovl_do_getxattr(oldpath, name, NULL, 0);
100 
101 		if (size < 0) {
102 			error = size;
103 			break;
104 		}
105 
106 		if (size > value_size) {
107 			void *new;
108 
109 			new = kvmalloc(size, GFP_KERNEL);
110 			if (!new) {
111 				error = -ENOMEM;
112 				break;
113 			}
114 			kvfree(value);
115 			value = new;
116 			value_size = size;
117 			goto retry;
118 		}
119 
120 		error = ovl_do_setxattr(OVL_FS(sb), new, name, value, size, 0);
121 		if (error) {
122 			if (error != -EOPNOTSUPP || ovl_must_copy_xattr(name))
123 				break;
124 
125 			/* Ignore failure to copy unknown xattrs */
126 			error = 0;
127 		}
128 	}
129 	kvfree(value);
130 out:
131 	kvfree(buf);
132 	return error;
133 }
134 
ovl_copy_fileattr(struct inode * inode,const struct path * old,const struct path * new)135 static int ovl_copy_fileattr(struct inode *inode, const struct path *old,
136 			     const struct path *new)
137 {
138 	struct fileattr oldfa = { .flags_valid = true };
139 	struct fileattr newfa = { .flags_valid = true };
140 	int err;
141 
142 	err = ovl_real_fileattr_get(old, &oldfa);
143 	if (err) {
144 		/* Ntfs-3g returns -EINVAL for "no fileattr support" */
145 		if (err == -ENOTTY || err == -EINVAL)
146 			return 0;
147 		pr_warn("failed to retrieve lower fileattr (%pd2, err=%i)\n",
148 			old->dentry, err);
149 		return err;
150 	}
151 
152 	/*
153 	 * We cannot set immutable and append-only flags on upper inode,
154 	 * because we would not be able to link upper inode to upper dir
155 	 * not set overlay private xattr on upper inode.
156 	 * Store these flags in overlay.protattr xattr instead.
157 	 */
158 	if (oldfa.flags & OVL_PROT_FS_FLAGS_MASK) {
159 		err = ovl_set_protattr(inode, new->dentry, &oldfa);
160 		if (err == -EPERM)
161 			pr_warn_once("copying fileattr: no xattr on upper\n");
162 		else if (err)
163 			return err;
164 	}
165 
166 	/* Don't bother copying flags if none are set */
167 	if (!(oldfa.flags & OVL_COPY_FS_FLAGS_MASK))
168 		return 0;
169 
170 	err = ovl_real_fileattr_get(new, &newfa);
171 	if (err) {
172 		/*
173 		 * Returning an error if upper doesn't support fileattr will
174 		 * result in a regression, so revert to the old behavior.
175 		 */
176 		if (err == -ENOTTY || err == -EINVAL) {
177 			pr_warn_once("copying fileattr: no support on upper\n");
178 			return 0;
179 		}
180 		pr_warn("failed to retrieve upper fileattr (%pd2, err=%i)\n",
181 			new->dentry, err);
182 		return err;
183 	}
184 
185 	BUILD_BUG_ON(OVL_COPY_FS_FLAGS_MASK & ~FS_COMMON_FL);
186 	newfa.flags &= ~OVL_COPY_FS_FLAGS_MASK;
187 	newfa.flags |= (oldfa.flags & OVL_COPY_FS_FLAGS_MASK);
188 
189 	BUILD_BUG_ON(OVL_COPY_FSX_FLAGS_MASK & ~FS_XFLAG_COMMON);
190 	newfa.fsx_xflags &= ~OVL_COPY_FSX_FLAGS_MASK;
191 	newfa.fsx_xflags |= (oldfa.fsx_xflags & OVL_COPY_FSX_FLAGS_MASK);
192 
193 	return ovl_real_fileattr_set(new, &newfa);
194 }
195 
ovl_copy_up_file(struct ovl_fs * ofs,struct dentry * dentry,struct file * new_file,loff_t len)196 static int ovl_copy_up_file(struct ovl_fs *ofs, struct dentry *dentry,
197 			    struct file *new_file, loff_t len)
198 {
199 	struct path datapath;
200 	struct file *old_file;
201 	loff_t old_pos = 0;
202 	loff_t new_pos = 0;
203 	loff_t cloned;
204 	loff_t data_pos = -1;
205 	loff_t hole_len;
206 	bool skip_hole = false;
207 	int error = 0;
208 
209 	ovl_path_lowerdata(dentry, &datapath);
210 	if (WARN_ON(datapath.dentry == NULL))
211 		return -EIO;
212 
213 	old_file = ovl_path_open(&datapath, O_LARGEFILE | O_RDONLY);
214 	if (IS_ERR(old_file))
215 		return PTR_ERR(old_file);
216 
217 	/* Try to use clone_file_range to clone up within the same fs */
218 	cloned = do_clone_file_range(old_file, 0, new_file, 0, len, 0);
219 	if (cloned == len)
220 		goto out_fput;
221 	/* Couldn't clone, so now we try to copy the data */
222 
223 	/* Check if lower fs supports seek operation */
224 	if (old_file->f_mode & FMODE_LSEEK)
225 		skip_hole = true;
226 
227 	while (len) {
228 		size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
229 		long bytes;
230 
231 		if (len < this_len)
232 			this_len = len;
233 
234 		if (signal_pending_state(TASK_KILLABLE, current)) {
235 			error = -EINTR;
236 			break;
237 		}
238 
239 		/*
240 		 * Fill zero for hole will cost unnecessary disk space
241 		 * and meanwhile slow down the copy-up speed, so we do
242 		 * an optimization for hole during copy-up, it relies
243 		 * on SEEK_DATA implementation in lower fs so if lower
244 		 * fs does not support it, copy-up will behave as before.
245 		 *
246 		 * Detail logic of hole detection as below:
247 		 * When we detect next data position is larger than current
248 		 * position we will skip that hole, otherwise we copy
249 		 * data in the size of OVL_COPY_UP_CHUNK_SIZE. Actually,
250 		 * it may not recognize all kind of holes and sometimes
251 		 * only skips partial of hole area. However, it will be
252 		 * enough for most of the use cases.
253 		 */
254 
255 		if (skip_hole && data_pos < old_pos) {
256 			data_pos = vfs_llseek(old_file, old_pos, SEEK_DATA);
257 			if (data_pos > old_pos) {
258 				hole_len = data_pos - old_pos;
259 				len -= hole_len;
260 				old_pos = new_pos = data_pos;
261 				continue;
262 			} else if (data_pos == -ENXIO) {
263 				break;
264 			} else if (data_pos < 0) {
265 				skip_hole = false;
266 			}
267 		}
268 
269 		bytes = do_splice_direct(old_file, &old_pos,
270 					 new_file, &new_pos,
271 					 this_len, SPLICE_F_MOVE);
272 		if (bytes <= 0) {
273 			error = bytes;
274 			break;
275 		}
276 		WARN_ON(old_pos != new_pos);
277 
278 		len -= bytes;
279 	}
280 	if (!error && ovl_should_sync(ofs))
281 		error = vfs_fsync(new_file, 0);
282 out_fput:
283 	fput(old_file);
284 	return error;
285 }
286 
ovl_set_size(struct ovl_fs * ofs,struct dentry * upperdentry,struct kstat * stat)287 static int ovl_set_size(struct ovl_fs *ofs,
288 			struct dentry *upperdentry, struct kstat *stat)
289 {
290 	struct iattr attr = {
291 		.ia_valid = ATTR_SIZE,
292 		.ia_size = stat->size,
293 	};
294 
295 	return ovl_do_notify_change(ofs, upperdentry, &attr);
296 }
297 
ovl_set_timestamps(struct ovl_fs * ofs,struct dentry * upperdentry,struct kstat * stat)298 static int ovl_set_timestamps(struct ovl_fs *ofs, struct dentry *upperdentry,
299 			      struct kstat *stat)
300 {
301 	struct iattr attr = {
302 		.ia_valid =
303 		     ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_CTIME,
304 		.ia_atime = stat->atime,
305 		.ia_mtime = stat->mtime,
306 	};
307 
308 	return ovl_do_notify_change(ofs, upperdentry, &attr);
309 }
310 
ovl_set_attr(struct ovl_fs * ofs,struct dentry * upperdentry,struct kstat * stat)311 int ovl_set_attr(struct ovl_fs *ofs, struct dentry *upperdentry,
312 		 struct kstat *stat)
313 {
314 	int err = 0;
315 
316 	if (!S_ISLNK(stat->mode)) {
317 		struct iattr attr = {
318 			.ia_valid = ATTR_MODE,
319 			.ia_mode = stat->mode,
320 		};
321 		err = ovl_do_notify_change(ofs, upperdentry, &attr);
322 	}
323 	if (!err) {
324 		struct iattr attr = {
325 			.ia_valid = ATTR_UID | ATTR_GID,
326 			.ia_vfsuid = VFSUIDT_INIT(stat->uid),
327 			.ia_vfsgid = VFSGIDT_INIT(stat->gid),
328 		};
329 		err = ovl_do_notify_change(ofs, upperdentry, &attr);
330 	}
331 	if (!err)
332 		ovl_set_timestamps(ofs, upperdentry, stat);
333 
334 	return err;
335 }
336 
ovl_encode_real_fh(struct ovl_fs * ofs,struct dentry * real,bool is_upper)337 struct ovl_fh *ovl_encode_real_fh(struct ovl_fs *ofs, struct dentry *real,
338 				  bool is_upper)
339 {
340 	struct ovl_fh *fh;
341 	int fh_type, dwords;
342 	int buflen = MAX_HANDLE_SZ;
343 	uuid_t *uuid = &real->d_sb->s_uuid;
344 	int err;
345 
346 	/* Make sure the real fid stays 32bit aligned */
347 	BUILD_BUG_ON(OVL_FH_FID_OFFSET % 4);
348 	BUILD_BUG_ON(MAX_HANDLE_SZ + OVL_FH_FID_OFFSET > 255);
349 
350 	fh = kzalloc(buflen + OVL_FH_FID_OFFSET, GFP_KERNEL);
351 	if (!fh)
352 		return ERR_PTR(-ENOMEM);
353 
354 	/*
355 	 * We encode a non-connectable file handle for non-dir, because we
356 	 * only need to find the lower inode number and we don't want to pay
357 	 * the price or reconnecting the dentry.
358 	 */
359 	dwords = buflen >> 2;
360 	fh_type = exportfs_encode_fh(real, (void *)fh->fb.fid, &dwords, 0);
361 	buflen = (dwords << 2);
362 
363 	err = -EIO;
364 	if (WARN_ON(fh_type < 0) ||
365 	    WARN_ON(buflen > MAX_HANDLE_SZ) ||
366 	    WARN_ON(fh_type == FILEID_INVALID))
367 		goto out_err;
368 
369 	fh->fb.version = OVL_FH_VERSION;
370 	fh->fb.magic = OVL_FH_MAGIC;
371 	fh->fb.type = fh_type;
372 	fh->fb.flags = OVL_FH_FLAG_CPU_ENDIAN;
373 	/*
374 	 * When we will want to decode an overlay dentry from this handle
375 	 * and all layers are on the same fs, if we get a disconncted real
376 	 * dentry when we decode fid, the only way to tell if we should assign
377 	 * it to upperdentry or to lowerstack is by checking this flag.
378 	 */
379 	if (is_upper)
380 		fh->fb.flags |= OVL_FH_FLAG_PATH_UPPER;
381 	fh->fb.len = sizeof(fh->fb) + buflen;
382 	if (ofs->config.uuid)
383 		fh->fb.uuid = *uuid;
384 
385 	return fh;
386 
387 out_err:
388 	kfree(fh);
389 	return ERR_PTR(err);
390 }
391 
ovl_set_origin(struct ovl_fs * ofs,struct dentry * lower,struct dentry * upper)392 int ovl_set_origin(struct ovl_fs *ofs, struct dentry *lower,
393 		   struct dentry *upper)
394 {
395 	const struct ovl_fh *fh = NULL;
396 	int err;
397 
398 	/*
399 	 * When lower layer doesn't support export operations store a 'null' fh,
400 	 * so we can use the overlay.origin xattr to distignuish between a copy
401 	 * up and a pure upper inode.
402 	 */
403 	if (ovl_can_decode_fh(lower->d_sb)) {
404 		fh = ovl_encode_real_fh(ofs, lower, false);
405 		if (IS_ERR(fh))
406 			return PTR_ERR(fh);
407 	}
408 
409 	/*
410 	 * Do not fail when upper doesn't support xattrs.
411 	 */
412 	err = ovl_check_setxattr(ofs, upper, OVL_XATTR_ORIGIN, fh->buf,
413 				 fh ? fh->fb.len : 0, 0);
414 	kfree(fh);
415 
416 	/* Ignore -EPERM from setting "user.*" on symlink/special */
417 	return err == -EPERM ? 0 : err;
418 }
419 
420 /* Store file handle of @upper dir in @index dir entry */
ovl_set_upper_fh(struct ovl_fs * ofs,struct dentry * upper,struct dentry * index)421 static int ovl_set_upper_fh(struct ovl_fs *ofs, struct dentry *upper,
422 			    struct dentry *index)
423 {
424 	const struct ovl_fh *fh;
425 	int err;
426 
427 	fh = ovl_encode_real_fh(ofs, upper, true);
428 	if (IS_ERR(fh))
429 		return PTR_ERR(fh);
430 
431 	err = ovl_setxattr(ofs, index, OVL_XATTR_UPPER, fh->buf, fh->fb.len);
432 
433 	kfree(fh);
434 	return err;
435 }
436 
437 /*
438  * Create and install index entry.
439  *
440  * Caller must hold i_mutex on indexdir.
441  */
ovl_create_index(struct dentry * dentry,struct dentry * origin,struct dentry * upper)442 static int ovl_create_index(struct dentry *dentry, struct dentry *origin,
443 			    struct dentry *upper)
444 {
445 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
446 	struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
447 	struct inode *dir = d_inode(indexdir);
448 	struct dentry *index = NULL;
449 	struct dentry *temp = NULL;
450 	struct qstr name = { };
451 	int err;
452 
453 	/*
454 	 * For now this is only used for creating index entry for directories,
455 	 * because non-dir are copied up directly to index and then hardlinked
456 	 * to upper dir.
457 	 *
458 	 * TODO: implement create index for non-dir, so we can call it when
459 	 * encoding file handle for non-dir in case index does not exist.
460 	 */
461 	if (WARN_ON(!d_is_dir(dentry)))
462 		return -EIO;
463 
464 	/* Directory not expected to be indexed before copy up */
465 	if (WARN_ON(ovl_test_flag(OVL_INDEX, d_inode(dentry))))
466 		return -EIO;
467 
468 	err = ovl_get_index_name(ofs, origin, &name);
469 	if (err)
470 		return err;
471 
472 	temp = ovl_create_temp(ofs, indexdir, OVL_CATTR(S_IFDIR | 0));
473 	err = PTR_ERR(temp);
474 	if (IS_ERR(temp))
475 		goto free_name;
476 
477 	err = ovl_set_upper_fh(ofs, upper, temp);
478 	if (err)
479 		goto out;
480 
481 	index = ovl_lookup_upper(ofs, name.name, indexdir, name.len);
482 	if (IS_ERR(index)) {
483 		err = PTR_ERR(index);
484 	} else {
485 		err = ovl_do_rename(ofs, dir, temp, dir, index, 0);
486 		dput(index);
487 	}
488 out:
489 	if (err)
490 		ovl_cleanup(ofs, dir, temp);
491 	dput(temp);
492 free_name:
493 	kfree(name.name);
494 	return err;
495 }
496 
497 struct ovl_copy_up_ctx {
498 	struct dentry *parent;
499 	struct dentry *dentry;
500 	struct path lowerpath;
501 	struct kstat stat;
502 	struct kstat pstat;
503 	const char *link;
504 	struct dentry *destdir;
505 	struct qstr destname;
506 	struct dentry *workdir;
507 	bool origin;
508 	bool indexed;
509 	bool metacopy;
510 };
511 
ovl_link_up(struct ovl_copy_up_ctx * c)512 static int ovl_link_up(struct ovl_copy_up_ctx *c)
513 {
514 	int err;
515 	struct dentry *upper;
516 	struct dentry *upperdir = ovl_dentry_upper(c->parent);
517 	struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
518 	struct inode *udir = d_inode(upperdir);
519 
520 	/* Mark parent "impure" because it may now contain non-pure upper */
521 	err = ovl_set_impure(c->parent, upperdir);
522 	if (err)
523 		return err;
524 
525 	err = ovl_set_nlink_lower(c->dentry);
526 	if (err)
527 		return err;
528 
529 	inode_lock_nested(udir, I_MUTEX_PARENT);
530 	upper = ovl_lookup_upper(ofs, c->dentry->d_name.name, upperdir,
531 				 c->dentry->d_name.len);
532 	err = PTR_ERR(upper);
533 	if (!IS_ERR(upper)) {
534 		err = ovl_do_link(ofs, ovl_dentry_upper(c->dentry), udir, upper);
535 		dput(upper);
536 
537 		if (!err) {
538 			/* Restore timestamps on parent (best effort) */
539 			ovl_set_timestamps(ofs, upperdir, &c->pstat);
540 			ovl_dentry_set_upper_alias(c->dentry);
541 			ovl_dentry_update_reval(c->dentry, upper);
542 		}
543 	}
544 	inode_unlock(udir);
545 	if (err)
546 		return err;
547 
548 	err = ovl_set_nlink_upper(c->dentry);
549 
550 	return err;
551 }
552 
ovl_copy_up_data(struct ovl_copy_up_ctx * c,const struct path * temp)553 static int ovl_copy_up_data(struct ovl_copy_up_ctx *c, const struct path *temp)
554 {
555 	struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
556 	struct file *new_file;
557 	int err;
558 
559 	if (!S_ISREG(c->stat.mode) || c->metacopy || !c->stat.size)
560 		return 0;
561 
562 	new_file = ovl_path_open(temp, O_LARGEFILE | O_WRONLY);
563 	if (IS_ERR(new_file))
564 		return PTR_ERR(new_file);
565 
566 	err = ovl_copy_up_file(ofs, c->dentry, new_file, c->stat.size);
567 	fput(new_file);
568 
569 	return err;
570 }
571 
ovl_copy_up_metadata(struct ovl_copy_up_ctx * c,struct dentry * temp)572 static int ovl_copy_up_metadata(struct ovl_copy_up_ctx *c, struct dentry *temp)
573 {
574 	struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
575 	struct inode *inode = d_inode(c->dentry);
576 	struct path upperpath = { .mnt = ovl_upper_mnt(ofs), .dentry = temp };
577 	int err;
578 
579 	err = ovl_copy_xattr(c->dentry->d_sb, &c->lowerpath, temp);
580 	if (err)
581 		return err;
582 
583 	if (inode->i_flags & OVL_COPY_I_FLAGS_MASK &&
584 	    (S_ISREG(c->stat.mode) || S_ISDIR(c->stat.mode))) {
585 		/*
586 		 * Copy the fileattr inode flags that are the source of already
587 		 * copied i_flags
588 		 */
589 		err = ovl_copy_fileattr(inode, &c->lowerpath, &upperpath);
590 		if (err)
591 			return err;
592 	}
593 
594 	/*
595 	 * Store identifier of lower inode in upper inode xattr to
596 	 * allow lookup of the copy up origin inode.
597 	 *
598 	 * Don't set origin when we are breaking the association with a lower
599 	 * hard link.
600 	 */
601 	if (c->origin) {
602 		err = ovl_set_origin(ofs, c->lowerpath.dentry, temp);
603 		if (err)
604 			return err;
605 	}
606 
607 	if (c->metacopy) {
608 		err = ovl_check_setxattr(ofs, temp, OVL_XATTR_METACOPY,
609 					 NULL, 0, -EOPNOTSUPP);
610 		if (err)
611 			return err;
612 	}
613 
614 	inode_lock(temp->d_inode);
615 	if (S_ISREG(c->stat.mode))
616 		err = ovl_set_size(ofs, temp, &c->stat);
617 	if (!err)
618 		err = ovl_set_attr(ofs, temp, &c->stat);
619 	inode_unlock(temp->d_inode);
620 
621 	return err;
622 }
623 
624 struct ovl_cu_creds {
625 	const struct cred *old;
626 	struct cred *new;
627 };
628 
ovl_prep_cu_creds(struct dentry * dentry,struct ovl_cu_creds * cc)629 static int ovl_prep_cu_creds(struct dentry *dentry, struct ovl_cu_creds *cc)
630 {
631 	int err;
632 
633 	cc->old = cc->new = NULL;
634 	err = security_inode_copy_up(dentry, &cc->new);
635 	if (err < 0)
636 		return err;
637 
638 	if (cc->new)
639 		cc->old = override_creds(cc->new);
640 
641 	return 0;
642 }
643 
ovl_revert_cu_creds(struct ovl_cu_creds * cc)644 static void ovl_revert_cu_creds(struct ovl_cu_creds *cc)
645 {
646 	if (cc->new) {
647 		revert_creds(cc->old);
648 		put_cred(cc->new);
649 	}
650 }
651 
652 /*
653  * Copyup using workdir to prepare temp file.  Used when copying up directories,
654  * special files or when upper fs doesn't support O_TMPFILE.
655  */
ovl_copy_up_workdir(struct ovl_copy_up_ctx * c)656 static int ovl_copy_up_workdir(struct ovl_copy_up_ctx *c)
657 {
658 	struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
659 	struct inode *inode;
660 	struct inode *udir = d_inode(c->destdir), *wdir = d_inode(c->workdir);
661 	struct path path = { .mnt = ovl_upper_mnt(ofs) };
662 	struct dentry *temp, *upper;
663 	struct ovl_cu_creds cc;
664 	int err;
665 	struct ovl_cattr cattr = {
666 		/* Can't properly set mode on creation because of the umask */
667 		.mode = c->stat.mode & S_IFMT,
668 		.rdev = c->stat.rdev,
669 		.link = c->link
670 	};
671 
672 	/* workdir and destdir could be the same when copying up to indexdir */
673 	err = -EIO;
674 	if (lock_rename(c->workdir, c->destdir) != NULL)
675 		goto unlock;
676 
677 	err = ovl_prep_cu_creds(c->dentry, &cc);
678 	if (err)
679 		goto unlock;
680 
681 	temp = ovl_create_temp(ofs, c->workdir, &cattr);
682 	ovl_revert_cu_creds(&cc);
683 
684 	err = PTR_ERR(temp);
685 	if (IS_ERR(temp))
686 		goto unlock;
687 
688 	/*
689 	 * Copy up data first and then xattrs. Writing data after
690 	 * xattrs will remove security.capability xattr automatically.
691 	 */
692 	path.dentry = temp;
693 	err = ovl_copy_up_data(c, &path);
694 	if (err)
695 		goto cleanup;
696 
697 	err = ovl_copy_up_metadata(c, temp);
698 	if (err)
699 		goto cleanup;
700 
701 	if (S_ISDIR(c->stat.mode) && c->indexed) {
702 		err = ovl_create_index(c->dentry, c->lowerpath.dentry, temp);
703 		if (err)
704 			goto cleanup;
705 	}
706 
707 	upper = ovl_lookup_upper(ofs, c->destname.name, c->destdir,
708 				 c->destname.len);
709 	err = PTR_ERR(upper);
710 	if (IS_ERR(upper))
711 		goto cleanup;
712 
713 	err = ovl_do_rename(ofs, wdir, temp, udir, upper, 0);
714 	dput(upper);
715 	if (err)
716 		goto cleanup;
717 
718 	if (!c->metacopy)
719 		ovl_set_upperdata(d_inode(c->dentry));
720 	inode = d_inode(c->dentry);
721 	ovl_inode_update(inode, temp);
722 	if (S_ISDIR(inode->i_mode))
723 		ovl_set_flag(OVL_WHITEOUTS, inode);
724 unlock:
725 	unlock_rename(c->workdir, c->destdir);
726 
727 	return err;
728 
729 cleanup:
730 	ovl_cleanup(ofs, wdir, temp);
731 	dput(temp);
732 	goto unlock;
733 }
734 
735 /* Copyup using O_TMPFILE which does not require cross dir locking */
ovl_copy_up_tmpfile(struct ovl_copy_up_ctx * c)736 static int ovl_copy_up_tmpfile(struct ovl_copy_up_ctx *c)
737 {
738 	struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
739 	struct inode *udir = d_inode(c->destdir);
740 	struct dentry *temp, *upper;
741 	struct file *tmpfile;
742 	struct ovl_cu_creds cc;
743 	int err;
744 
745 	err = ovl_prep_cu_creds(c->dentry, &cc);
746 	if (err)
747 		return err;
748 
749 	tmpfile = ovl_do_tmpfile(ofs, c->workdir, c->stat.mode);
750 	ovl_revert_cu_creds(&cc);
751 
752 	if (IS_ERR(tmpfile))
753 		return PTR_ERR(tmpfile);
754 
755 	temp = tmpfile->f_path.dentry;
756 	if (!c->metacopy && c->stat.size) {
757 		err = ovl_copy_up_file(ofs, c->dentry, tmpfile, c->stat.size);
758 		if (err)
759 			goto out_fput;
760 	}
761 
762 	err = ovl_copy_up_metadata(c, temp);
763 	if (err)
764 		goto out_fput;
765 
766 	inode_lock_nested(udir, I_MUTEX_PARENT);
767 
768 	upper = ovl_lookup_upper(ofs, c->destname.name, c->destdir,
769 				 c->destname.len);
770 	err = PTR_ERR(upper);
771 	if (!IS_ERR(upper)) {
772 		err = ovl_do_link(ofs, temp, udir, upper);
773 		dput(upper);
774 	}
775 	inode_unlock(udir);
776 
777 	if (err)
778 		goto out_fput;
779 
780 	if (!c->metacopy)
781 		ovl_set_upperdata(d_inode(c->dentry));
782 	ovl_inode_update(d_inode(c->dentry), dget(temp));
783 
784 out_fput:
785 	fput(tmpfile);
786 	return err;
787 }
788 
789 /*
790  * Copy up a single dentry
791  *
792  * All renames start with copy up of source if necessary.  The actual
793  * rename will only proceed once the copy up was successful.  Copy up uses
794  * upper parent i_mutex for exclusion.  Since rename can change d_parent it
795  * is possible that the copy up will lock the old parent.  At that point
796  * the file will have already been copied up anyway.
797  */
ovl_do_copy_up(struct ovl_copy_up_ctx * c)798 static int ovl_do_copy_up(struct ovl_copy_up_ctx *c)
799 {
800 	int err;
801 	struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
802 	bool to_index = false;
803 
804 	/*
805 	 * Indexed non-dir is copied up directly to the index entry and then
806 	 * hardlinked to upper dir. Indexed dir is copied up to indexdir,
807 	 * then index entry is created and then copied up dir installed.
808 	 * Copying dir up to indexdir instead of workdir simplifies locking.
809 	 */
810 	if (ovl_need_index(c->dentry)) {
811 		c->indexed = true;
812 		if (S_ISDIR(c->stat.mode))
813 			c->workdir = ovl_indexdir(c->dentry->d_sb);
814 		else
815 			to_index = true;
816 	}
817 
818 	if (S_ISDIR(c->stat.mode) || c->stat.nlink == 1 || to_index)
819 		c->origin = true;
820 
821 	if (to_index) {
822 		c->destdir = ovl_indexdir(c->dentry->d_sb);
823 		err = ovl_get_index_name(ofs, c->lowerpath.dentry, &c->destname);
824 		if (err)
825 			return err;
826 	} else if (WARN_ON(!c->parent)) {
827 		/* Disconnected dentry must be copied up to index dir */
828 		return -EIO;
829 	} else {
830 		/*
831 		 * Mark parent "impure" because it may now contain non-pure
832 		 * upper
833 		 */
834 		err = ovl_set_impure(c->parent, c->destdir);
835 		if (err)
836 			return err;
837 	}
838 
839 	/* Should we copyup with O_TMPFILE or with workdir? */
840 	if (S_ISREG(c->stat.mode) && ofs->tmpfile)
841 		err = ovl_copy_up_tmpfile(c);
842 	else
843 		err = ovl_copy_up_workdir(c);
844 	if (err)
845 		goto out;
846 
847 	if (c->indexed)
848 		ovl_set_flag(OVL_INDEX, d_inode(c->dentry));
849 
850 	if (to_index) {
851 		/* Initialize nlink for copy up of disconnected dentry */
852 		err = ovl_set_nlink_upper(c->dentry);
853 	} else {
854 		struct inode *udir = d_inode(c->destdir);
855 
856 		/* Restore timestamps on parent (best effort) */
857 		inode_lock(udir);
858 		ovl_set_timestamps(ofs, c->destdir, &c->pstat);
859 		inode_unlock(udir);
860 
861 		ovl_dentry_set_upper_alias(c->dentry);
862 		ovl_dentry_update_reval(c->dentry, ovl_dentry_upper(c->dentry));
863 	}
864 
865 out:
866 	if (to_index)
867 		kfree(c->destname.name);
868 	return err;
869 }
870 
ovl_need_meta_copy_up(struct dentry * dentry,umode_t mode,int flags)871 static bool ovl_need_meta_copy_up(struct dentry *dentry, umode_t mode,
872 				  int flags)
873 {
874 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
875 
876 	if (!ofs->config.metacopy)
877 		return false;
878 
879 	if (!S_ISREG(mode))
880 		return false;
881 
882 	if (flags && ((OPEN_FMODE(flags) & FMODE_WRITE) || (flags & O_TRUNC)))
883 		return false;
884 
885 	return true;
886 }
887 
ovl_getxattr_value(const struct path * path,char * name,char ** value)888 static ssize_t ovl_getxattr_value(const struct path *path, char *name, char **value)
889 {
890 	ssize_t res;
891 	char *buf;
892 
893 	res = ovl_do_getxattr(path, name, NULL, 0);
894 	if (res == -ENODATA || res == -EOPNOTSUPP)
895 		res = 0;
896 
897 	if (res > 0) {
898 		buf = kzalloc(res, GFP_KERNEL);
899 		if (!buf)
900 			return -ENOMEM;
901 
902 		res = ovl_do_getxattr(path, name, buf, res);
903 		if (res < 0)
904 			kfree(buf);
905 		else
906 			*value = buf;
907 	}
908 	return res;
909 }
910 
911 /* Copy up data of an inode which was copied up metadata only in the past. */
ovl_copy_up_meta_inode_data(struct ovl_copy_up_ctx * c)912 static int ovl_copy_up_meta_inode_data(struct ovl_copy_up_ctx *c)
913 {
914 	struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
915 	struct path upperpath;
916 	int err;
917 	char *capability = NULL;
918 	ssize_t cap_size;
919 
920 	ovl_path_upper(c->dentry, &upperpath);
921 	if (WARN_ON(upperpath.dentry == NULL))
922 		return -EIO;
923 
924 	if (c->stat.size) {
925 		err = cap_size = ovl_getxattr_value(&upperpath, XATTR_NAME_CAPS,
926 						    &capability);
927 		if (cap_size < 0)
928 			goto out;
929 	}
930 
931 	err = ovl_copy_up_data(c, &upperpath);
932 	if (err)
933 		goto out_free;
934 
935 	/*
936 	 * Writing to upper file will clear security.capability xattr. We
937 	 * don't want that to happen for normal copy-up operation.
938 	 */
939 	if (capability) {
940 		err = ovl_do_setxattr(ofs, upperpath.dentry, XATTR_NAME_CAPS,
941 				      capability, cap_size, 0);
942 		if (err)
943 			goto out_free;
944 	}
945 
946 
947 	err = ovl_removexattr(ofs, upperpath.dentry, OVL_XATTR_METACOPY);
948 	if (err)
949 		goto out_free;
950 
951 	ovl_set_upperdata(d_inode(c->dentry));
952 out_free:
953 	kfree(capability);
954 out:
955 	return err;
956 }
957 
ovl_copy_up_one(struct dentry * parent,struct dentry * dentry,int flags)958 static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
959 			   int flags)
960 {
961 	int err;
962 	DEFINE_DELAYED_CALL(done);
963 	struct path parentpath;
964 	struct ovl_copy_up_ctx ctx = {
965 		.parent = parent,
966 		.dentry = dentry,
967 		.workdir = ovl_workdir(dentry),
968 	};
969 
970 	if (WARN_ON(!ctx.workdir))
971 		return -EROFS;
972 
973 	ovl_path_lower(dentry, &ctx.lowerpath);
974 	err = vfs_getattr(&ctx.lowerpath, &ctx.stat,
975 			  STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
976 	if (err)
977 		return err;
978 
979 	if (!kuid_has_mapping(current_user_ns(), ctx.stat.uid) ||
980 	    !kgid_has_mapping(current_user_ns(), ctx.stat.gid))
981 		return -EOVERFLOW;
982 
983 	ctx.metacopy = ovl_need_meta_copy_up(dentry, ctx.stat.mode, flags);
984 
985 	if (parent) {
986 		ovl_path_upper(parent, &parentpath);
987 		ctx.destdir = parentpath.dentry;
988 		ctx.destname = dentry->d_name;
989 
990 		err = vfs_getattr(&parentpath, &ctx.pstat,
991 				  STATX_ATIME | STATX_MTIME,
992 				  AT_STATX_SYNC_AS_STAT);
993 		if (err)
994 			return err;
995 	}
996 
997 	/* maybe truncate regular file. this has no effect on dirs */
998 	if (flags & O_TRUNC)
999 		ctx.stat.size = 0;
1000 
1001 	if (S_ISLNK(ctx.stat.mode)) {
1002 		ctx.link = vfs_get_link(ctx.lowerpath.dentry, &done);
1003 		if (IS_ERR(ctx.link))
1004 			return PTR_ERR(ctx.link);
1005 	}
1006 
1007 	err = ovl_copy_up_start(dentry, flags);
1008 	/* err < 0: interrupted, err > 0: raced with another copy-up */
1009 	if (unlikely(err)) {
1010 		if (err > 0)
1011 			err = 0;
1012 	} else {
1013 		if (!ovl_dentry_upper(dentry))
1014 			err = ovl_do_copy_up(&ctx);
1015 		if (!err && parent && !ovl_dentry_has_upper_alias(dentry))
1016 			err = ovl_link_up(&ctx);
1017 		if (!err && ovl_dentry_needs_data_copy_up_locked(dentry, flags))
1018 			err = ovl_copy_up_meta_inode_data(&ctx);
1019 		ovl_copy_up_end(dentry);
1020 	}
1021 	do_delayed_call(&done);
1022 
1023 	return err;
1024 }
1025 
ovl_copy_up_flags(struct dentry * dentry,int flags)1026 static int ovl_copy_up_flags(struct dentry *dentry, int flags)
1027 {
1028 	int err = 0;
1029 	const struct cred *old_cred;
1030 	bool disconnected = (dentry->d_flags & DCACHE_DISCONNECTED);
1031 
1032 	/*
1033 	 * With NFS export, copy up can get called for a disconnected non-dir.
1034 	 * In this case, we will copy up lower inode to index dir without
1035 	 * linking it to upper dir.
1036 	 */
1037 	if (WARN_ON(disconnected && d_is_dir(dentry)))
1038 		return -EIO;
1039 
1040 	old_cred = ovl_override_creds(dentry->d_sb);
1041 	while (!err) {
1042 		struct dentry *next;
1043 		struct dentry *parent = NULL;
1044 
1045 		if (ovl_already_copied_up(dentry, flags))
1046 			break;
1047 
1048 		next = dget(dentry);
1049 		/* find the topmost dentry not yet copied up */
1050 		for (; !disconnected;) {
1051 			parent = dget_parent(next);
1052 
1053 			if (ovl_dentry_upper(parent))
1054 				break;
1055 
1056 			dput(next);
1057 			next = parent;
1058 		}
1059 
1060 		err = ovl_copy_up_one(parent, next, flags);
1061 
1062 		dput(parent);
1063 		dput(next);
1064 	}
1065 	ovl_revert_creds(dentry->d_sb, old_cred);
1066 
1067 	return err;
1068 }
1069 
ovl_open_need_copy_up(struct dentry * dentry,int flags)1070 static bool ovl_open_need_copy_up(struct dentry *dentry, int flags)
1071 {
1072 	/* Copy up of disconnected dentry does not set upper alias */
1073 	if (ovl_already_copied_up(dentry, flags))
1074 		return false;
1075 
1076 	if (special_file(d_inode(dentry)->i_mode))
1077 		return false;
1078 
1079 	if (!ovl_open_flags_need_copy_up(flags))
1080 		return false;
1081 
1082 	return true;
1083 }
1084 
ovl_maybe_copy_up(struct dentry * dentry,int flags)1085 int ovl_maybe_copy_up(struct dentry *dentry, int flags)
1086 {
1087 	int err = 0;
1088 
1089 	if (ovl_open_need_copy_up(dentry, flags)) {
1090 		err = ovl_want_write(dentry);
1091 		if (!err) {
1092 			err = ovl_copy_up_flags(dentry, flags);
1093 			ovl_drop_write(dentry);
1094 		}
1095 	}
1096 
1097 	return err;
1098 }
1099 
ovl_copy_up_with_data(struct dentry * dentry)1100 int ovl_copy_up_with_data(struct dentry *dentry)
1101 {
1102 	return ovl_copy_up_flags(dentry, O_WRONLY);
1103 }
1104 
ovl_copy_up(struct dentry * dentry)1105 int ovl_copy_up(struct dentry *dentry)
1106 {
1107 	return ovl_copy_up_flags(dentry, 0);
1108 }
1109