xref: /linux/fs/overlayfs/copy_up.c (revision 69fb09f6ccdb2f070557fd1f4c56c4d646694c8e)
1 /*
2  *
3  * Copyright (C) 2011 Novell Inc.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published by
7  * the Free Software Foundation.
8  */
9 
10 #include <linux/module.h>
11 #include <linux/fs.h>
12 #include <linux/slab.h>
13 #include <linux/file.h>
14 #include <linux/splice.h>
15 #include <linux/xattr.h>
16 #include <linux/security.h>
17 #include <linux/uaccess.h>
18 #include <linux/sched/signal.h>
19 #include <linux/cred.h>
20 #include <linux/namei.h>
21 #include <linux/fdtable.h>
22 #include <linux/ratelimit.h>
23 #include <linux/exportfs.h>
24 #include "overlayfs.h"
25 #include "ovl_entry.h"
26 
27 #define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
28 
29 static bool __read_mostly ovl_check_copy_up;
30 module_param_named(check_copy_up, ovl_check_copy_up, bool,
31 		   S_IWUSR | S_IRUGO);
32 MODULE_PARM_DESC(ovl_check_copy_up,
33 		 "Warn on copy-up when causing process also has a R/O fd open");
34 
35 static int ovl_check_fd(const void *data, struct file *f, unsigned int fd)
36 {
37 	const struct dentry *dentry = data;
38 
39 	if (file_inode(f) == d_inode(dentry))
40 		pr_warn_ratelimited("overlayfs: Warning: Copying up %pD, but open R/O on fd %u which will cease to be coherent [pid=%d %s]\n",
41 				    f, fd, current->pid, current->comm);
42 	return 0;
43 }
44 
45 /*
46  * Check the fds open by this process and warn if something like the following
47  * scenario is about to occur:
48  *
49  *	fd1 = open("foo", O_RDONLY);
50  *	fd2 = open("foo", O_RDWR);
51  */
52 static void ovl_do_check_copy_up(struct dentry *dentry)
53 {
54 	if (ovl_check_copy_up)
55 		iterate_fd(current->files, 0, ovl_check_fd, dentry);
56 }
57 
58 int ovl_copy_xattr(struct dentry *old, struct dentry *new)
59 {
60 	ssize_t list_size, size, value_size = 0;
61 	char *buf, *name, *value = NULL;
62 	int uninitialized_var(error);
63 	size_t slen;
64 
65 	if (!(old->d_inode->i_opflags & IOP_XATTR) ||
66 	    !(new->d_inode->i_opflags & IOP_XATTR))
67 		return 0;
68 
69 	list_size = vfs_listxattr(old, NULL, 0);
70 	if (list_size <= 0) {
71 		if (list_size == -EOPNOTSUPP)
72 			return 0;
73 		return list_size;
74 	}
75 
76 	buf = kzalloc(list_size, GFP_KERNEL);
77 	if (!buf)
78 		return -ENOMEM;
79 
80 	list_size = vfs_listxattr(old, buf, list_size);
81 	if (list_size <= 0) {
82 		error = list_size;
83 		goto out;
84 	}
85 
86 	for (name = buf; list_size; name += slen) {
87 		slen = strnlen(name, list_size) + 1;
88 
89 		/* underlying fs providing us with an broken xattr list? */
90 		if (WARN_ON(slen > list_size)) {
91 			error = -EIO;
92 			break;
93 		}
94 		list_size -= slen;
95 
96 		if (ovl_is_private_xattr(name))
97 			continue;
98 retry:
99 		size = vfs_getxattr(old, name, value, value_size);
100 		if (size == -ERANGE)
101 			size = vfs_getxattr(old, name, NULL, 0);
102 
103 		if (size < 0) {
104 			error = size;
105 			break;
106 		}
107 
108 		if (size > value_size) {
109 			void *new;
110 
111 			new = krealloc(value, size, GFP_KERNEL);
112 			if (!new) {
113 				error = -ENOMEM;
114 				break;
115 			}
116 			value = new;
117 			value_size = size;
118 			goto retry;
119 		}
120 
121 		error = security_inode_copy_up_xattr(name);
122 		if (error < 0 && error != -EOPNOTSUPP)
123 			break;
124 		if (error == 1) {
125 			error = 0;
126 			continue; /* Discard */
127 		}
128 		error = vfs_setxattr(new, name, value, size, 0);
129 		if (error)
130 			break;
131 	}
132 	kfree(value);
133 out:
134 	kfree(buf);
135 	return error;
136 }
137 
138 static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
139 {
140 	struct file *old_file;
141 	struct file *new_file;
142 	loff_t old_pos = 0;
143 	loff_t new_pos = 0;
144 	int error = 0;
145 
146 	if (len == 0)
147 		return 0;
148 
149 	old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY);
150 	if (IS_ERR(old_file))
151 		return PTR_ERR(old_file);
152 
153 	new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY);
154 	if (IS_ERR(new_file)) {
155 		error = PTR_ERR(new_file);
156 		goto out_fput;
157 	}
158 
159 	/* Try to use clone_file_range to clone up within the same fs */
160 	error = vfs_clone_file_range(old_file, 0, new_file, 0, len);
161 	if (!error)
162 		goto out;
163 	/* Couldn't clone, so now we try to copy the data */
164 	error = 0;
165 
166 	/* FIXME: copy up sparse files efficiently */
167 	while (len) {
168 		size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
169 		long bytes;
170 
171 		if (len < this_len)
172 			this_len = len;
173 
174 		if (signal_pending_state(TASK_KILLABLE, current)) {
175 			error = -EINTR;
176 			break;
177 		}
178 
179 		bytes = do_splice_direct(old_file, &old_pos,
180 					 new_file, &new_pos,
181 					 this_len, SPLICE_F_MOVE);
182 		if (bytes <= 0) {
183 			error = bytes;
184 			break;
185 		}
186 		WARN_ON(old_pos != new_pos);
187 
188 		len -= bytes;
189 	}
190 out:
191 	if (!error)
192 		error = vfs_fsync(new_file, 0);
193 	fput(new_file);
194 out_fput:
195 	fput(old_file);
196 	return error;
197 }
198 
199 static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
200 {
201 	struct iattr attr = {
202 		.ia_valid =
203 		     ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
204 		.ia_atime = stat->atime,
205 		.ia_mtime = stat->mtime,
206 	};
207 
208 	return notify_change(upperdentry, &attr, NULL);
209 }
210 
211 int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat)
212 {
213 	int err = 0;
214 
215 	if (!S_ISLNK(stat->mode)) {
216 		struct iattr attr = {
217 			.ia_valid = ATTR_MODE,
218 			.ia_mode = stat->mode,
219 		};
220 		err = notify_change(upperdentry, &attr, NULL);
221 	}
222 	if (!err) {
223 		struct iattr attr = {
224 			.ia_valid = ATTR_UID | ATTR_GID,
225 			.ia_uid = stat->uid,
226 			.ia_gid = stat->gid,
227 		};
228 		err = notify_change(upperdentry, &attr, NULL);
229 	}
230 	if (!err)
231 		ovl_set_timestamps(upperdentry, stat);
232 
233 	return err;
234 }
235 
236 static struct ovl_fh *ovl_encode_fh(struct dentry *lower, uuid_t *uuid)
237 {
238 	struct ovl_fh *fh;
239 	int fh_type, fh_len, dwords;
240 	void *buf;
241 	int buflen = MAX_HANDLE_SZ;
242 
243 	buf = kmalloc(buflen, GFP_TEMPORARY);
244 	if (!buf)
245 		return ERR_PTR(-ENOMEM);
246 
247 	/*
248 	 * We encode a non-connectable file handle for non-dir, because we
249 	 * only need to find the lower inode number and we don't want to pay
250 	 * the price or reconnecting the dentry.
251 	 */
252 	dwords = buflen >> 2;
253 	fh_type = exportfs_encode_fh(lower, buf, &dwords, 0);
254 	buflen = (dwords << 2);
255 
256 	fh = ERR_PTR(-EIO);
257 	if (WARN_ON(fh_type < 0) ||
258 	    WARN_ON(buflen > MAX_HANDLE_SZ) ||
259 	    WARN_ON(fh_type == FILEID_INVALID))
260 		goto out;
261 
262 	BUILD_BUG_ON(MAX_HANDLE_SZ + offsetof(struct ovl_fh, fid) > 255);
263 	fh_len = offsetof(struct ovl_fh, fid) + buflen;
264 	fh = kmalloc(fh_len, GFP_KERNEL);
265 	if (!fh) {
266 		fh = ERR_PTR(-ENOMEM);
267 		goto out;
268 	}
269 
270 	fh->version = OVL_FH_VERSION;
271 	fh->magic = OVL_FH_MAGIC;
272 	fh->type = fh_type;
273 	fh->flags = OVL_FH_FLAG_CPU_ENDIAN;
274 	fh->len = fh_len;
275 	fh->uuid = *uuid;
276 	memcpy(fh->fid, buf, buflen);
277 
278 out:
279 	kfree(buf);
280 	return fh;
281 }
282 
283 static int ovl_set_origin(struct dentry *dentry, struct dentry *lower,
284 			  struct dentry *upper)
285 {
286 	struct super_block *sb = lower->d_sb;
287 	const struct ovl_fh *fh = NULL;
288 	int err;
289 
290 	/*
291 	 * When lower layer doesn't support export operations store a 'null' fh,
292 	 * so we can use the overlay.origin xattr to distignuish between a copy
293 	 * up and a pure upper inode.
294 	 */
295 	if (sb->s_export_op && sb->s_export_op->fh_to_dentry &&
296 	    !uuid_is_null(&sb->s_uuid)) {
297 		fh = ovl_encode_fh(lower, &sb->s_uuid);
298 		if (IS_ERR(fh))
299 			return PTR_ERR(fh);
300 	}
301 
302 	/*
303 	 * Do not fail when upper doesn't support xattrs.
304 	 */
305 	err = ovl_check_setxattr(dentry, upper, OVL_XATTR_ORIGIN, fh,
306 				 fh ? fh->len : 0, 0);
307 	kfree(fh);
308 
309 	return err;
310 }
311 
312 static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir,
313 			      struct dentry *dentry, struct path *lowerpath,
314 			      struct kstat *stat, const char *link,
315 			      struct kstat *pstat, bool tmpfile)
316 {
317 	struct inode *wdir = workdir->d_inode;
318 	struct inode *udir = upperdir->d_inode;
319 	struct dentry *newdentry = NULL;
320 	struct dentry *upper = NULL;
321 	struct dentry *temp = NULL;
322 	int err;
323 	const struct cred *old_creds = NULL;
324 	struct cred *new_creds = NULL;
325 	struct cattr cattr = {
326 		/* Can't properly set mode on creation because of the umask */
327 		.mode = stat->mode & S_IFMT,
328 		.rdev = stat->rdev,
329 		.link = link
330 	};
331 
332 	err = security_inode_copy_up(dentry, &new_creds);
333 	if (err < 0)
334 		goto out;
335 
336 	if (new_creds)
337 		old_creds = override_creds(new_creds);
338 
339 	if (tmpfile)
340 		temp = ovl_do_tmpfile(upperdir, stat->mode);
341 	else
342 		temp = ovl_lookup_temp(workdir);
343 	err = 0;
344 	if (IS_ERR(temp)) {
345 		err = PTR_ERR(temp);
346 		temp = NULL;
347 	}
348 
349 	if (!err && !tmpfile)
350 		err = ovl_create_real(wdir, temp, &cattr, NULL, true);
351 
352 	if (new_creds) {
353 		revert_creds(old_creds);
354 		put_cred(new_creds);
355 	}
356 
357 	if (err)
358 		goto out;
359 
360 	if (S_ISREG(stat->mode)) {
361 		struct path upperpath;
362 
363 		ovl_path_upper(dentry, &upperpath);
364 		BUG_ON(upperpath.dentry != NULL);
365 		upperpath.dentry = temp;
366 
367 		if (tmpfile) {
368 			inode_unlock(udir);
369 			err = ovl_copy_up_data(lowerpath, &upperpath,
370 					       stat->size);
371 			inode_lock_nested(udir, I_MUTEX_PARENT);
372 		} else {
373 			err = ovl_copy_up_data(lowerpath, &upperpath,
374 					       stat->size);
375 		}
376 
377 		if (err)
378 			goto out_cleanup;
379 	}
380 
381 	err = ovl_copy_xattr(lowerpath->dentry, temp);
382 	if (err)
383 		goto out_cleanup;
384 
385 	inode_lock(temp->d_inode);
386 	err = ovl_set_attr(temp, stat);
387 	inode_unlock(temp->d_inode);
388 	if (err)
389 		goto out_cleanup;
390 
391 	/*
392 	 * Store identifier of lower inode in upper inode xattr to
393 	 * allow lookup of the copy up origin inode.
394 	 *
395 	 * Don't set origin when we are breaking the association with a lower
396 	 * hard link.
397 	 */
398 	if (S_ISDIR(stat->mode) || stat->nlink == 1) {
399 		err = ovl_set_origin(dentry, lowerpath->dentry, temp);
400 		if (err)
401 			goto out_cleanup;
402 	}
403 
404 	upper = lookup_one_len(dentry->d_name.name, upperdir,
405 			       dentry->d_name.len);
406 	if (IS_ERR(upper)) {
407 		err = PTR_ERR(upper);
408 		upper = NULL;
409 		goto out_cleanup;
410 	}
411 
412 	if (tmpfile)
413 		err = ovl_do_link(temp, udir, upper, true);
414 	else
415 		err = ovl_do_rename(wdir, temp, udir, upper, 0);
416 	if (err)
417 		goto out_cleanup;
418 
419 	newdentry = dget(tmpfile ? upper : temp);
420 	ovl_dentry_update(dentry, newdentry);
421 	ovl_inode_update(d_inode(dentry), d_inode(newdentry));
422 
423 	/* Restore timestamps on parent (best effort) */
424 	ovl_set_timestamps(upperdir, pstat);
425 out:
426 	dput(temp);
427 	dput(upper);
428 	return err;
429 
430 out_cleanup:
431 	if (!tmpfile)
432 		ovl_cleanup(wdir, temp);
433 	goto out;
434 }
435 
436 /*
437  * Copy up a single dentry
438  *
439  * All renames start with copy up of source if necessary.  The actual
440  * rename will only proceed once the copy up was successful.  Copy up uses
441  * upper parent i_mutex for exclusion.  Since rename can change d_parent it
442  * is possible that the copy up will lock the old parent.  At that point
443  * the file will have already been copied up anyway.
444  */
445 static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
446 			   struct path *lowerpath, struct kstat *stat)
447 {
448 	DEFINE_DELAYED_CALL(done);
449 	struct dentry *workdir = ovl_workdir(dentry);
450 	int err;
451 	struct kstat pstat;
452 	struct path parentpath;
453 	struct dentry *lowerdentry = lowerpath->dentry;
454 	struct dentry *upperdir;
455 	const char *link = NULL;
456 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
457 
458 	if (WARN_ON(!workdir))
459 		return -EROFS;
460 
461 	ovl_do_check_copy_up(lowerdentry);
462 
463 	ovl_path_upper(parent, &parentpath);
464 	upperdir = parentpath.dentry;
465 
466 	/* Mark parent "impure" because it may now contain non-pure upper */
467 	err = ovl_set_impure(parent, upperdir);
468 	if (err)
469 		return err;
470 
471 	err = vfs_getattr(&parentpath, &pstat,
472 			  STATX_ATIME | STATX_MTIME, AT_STATX_SYNC_AS_STAT);
473 	if (err)
474 		return err;
475 
476 	if (S_ISLNK(stat->mode)) {
477 		link = vfs_get_link(lowerdentry, &done);
478 		if (IS_ERR(link))
479 			return PTR_ERR(link);
480 	}
481 
482 	/* Should we copyup with O_TMPFILE or with workdir? */
483 	if (S_ISREG(stat->mode) && ofs->tmpfile) {
484 		err = ovl_copy_up_start(dentry);
485 		/* err < 0: interrupted, err > 0: raced with another copy-up */
486 		if (unlikely(err)) {
487 			pr_debug("ovl_copy_up_start(%pd2) = %i\n", dentry, err);
488 			if (err > 0)
489 				err = 0;
490 			goto out_done;
491 		}
492 
493 		inode_lock_nested(upperdir->d_inode, I_MUTEX_PARENT);
494 		err = ovl_copy_up_locked(workdir, upperdir, dentry, lowerpath,
495 					 stat, link, &pstat, true);
496 		inode_unlock(upperdir->d_inode);
497 		ovl_copy_up_end(dentry);
498 		goto out_done;
499 	}
500 
501 	err = -EIO;
502 	if (lock_rename(workdir, upperdir) != NULL) {
503 		pr_err("overlayfs: failed to lock workdir+upperdir\n");
504 		goto out_unlock;
505 	}
506 	if (ovl_dentry_upper(dentry)) {
507 		/* Raced with another copy-up?  Nothing to do, then... */
508 		err = 0;
509 		goto out_unlock;
510 	}
511 
512 	err = ovl_copy_up_locked(workdir, upperdir, dentry, lowerpath,
513 				 stat, link, &pstat, false);
514 out_unlock:
515 	unlock_rename(workdir, upperdir);
516 out_done:
517 	do_delayed_call(&done);
518 
519 	return err;
520 }
521 
522 int ovl_copy_up_flags(struct dentry *dentry, int flags)
523 {
524 	int err = 0;
525 	const struct cred *old_cred = ovl_override_creds(dentry->d_sb);
526 
527 	while (!err) {
528 		struct dentry *next;
529 		struct dentry *parent;
530 		struct path lowerpath;
531 		struct kstat stat;
532 		enum ovl_path_type type = ovl_path_type(dentry);
533 
534 		if (OVL_TYPE_UPPER(type))
535 			break;
536 
537 		next = dget(dentry);
538 		/* find the topmost dentry not yet copied up */
539 		for (;;) {
540 			parent = dget_parent(next);
541 
542 			type = ovl_path_type(parent);
543 			if (OVL_TYPE_UPPER(type))
544 				break;
545 
546 			dput(next);
547 			next = parent;
548 		}
549 
550 		ovl_path_lower(next, &lowerpath);
551 		err = vfs_getattr(&lowerpath, &stat,
552 				  STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
553 		/* maybe truncate regular file. this has no effect on dirs */
554 		if (flags & O_TRUNC)
555 			stat.size = 0;
556 		if (!err)
557 			err = ovl_copy_up_one(parent, next, &lowerpath, &stat);
558 
559 		dput(parent);
560 		dput(next);
561 	}
562 	revert_creds(old_cred);
563 
564 	return err;
565 }
566 
567 int ovl_copy_up(struct dentry *dentry)
568 {
569 	return ovl_copy_up_flags(dentry, 0);
570 }
571