xref: /linux/fs/overlayfs/file.c (revision 81dcc79758cd0c0cfddf539bbdb6e7307053fc0d)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2017 Red Hat, Inc.
4  */
5 
6 #include <linux/cred.h>
7 #include <linux/file.h>
8 #include <linux/mount.h>
9 #include <linux/xattr.h>
10 #include <linux/uio.h>
11 #include <linux/uaccess.h>
12 #include <linux/security.h>
13 #include <linux/fs.h>
14 #include <linux/backing-file.h>
15 #include "overlayfs.h"
16 
ovl_whatisit(struct inode * inode,struct inode * realinode)17 static char ovl_whatisit(struct inode *inode, struct inode *realinode)
18 {
19 	if (realinode != ovl_inode_upper(inode))
20 		return 'l';
21 	if (ovl_has_upperdata(inode))
22 		return 'u';
23 	else
24 		return 'm';
25 }
26 
ovl_open_realfile(const struct file * file,const struct path * realpath)27 static struct file *ovl_open_realfile(const struct file *file,
28 				      const struct path *realpath)
29 {
30 	struct inode *realinode = d_inode(realpath->dentry);
31 	struct inode *inode = file_inode(file);
32 	struct mnt_idmap *real_idmap;
33 	struct file *realfile;
34 	const struct cred *old_cred;
35 	int flags = file->f_flags | OVL_OPEN_FLAGS;
36 	int acc_mode = ACC_MODE(flags);
37 	int err;
38 
39 	if (flags & O_APPEND)
40 		acc_mode |= MAY_APPEND;
41 
42 	old_cred = ovl_override_creds(inode->i_sb);
43 	real_idmap = mnt_idmap(realpath->mnt);
44 	err = inode_permission(real_idmap, realinode, MAY_OPEN | acc_mode);
45 	if (err) {
46 		realfile = ERR_PTR(err);
47 	} else {
48 		if (!inode_owner_or_capable(real_idmap, realinode))
49 			flags &= ~O_NOATIME;
50 
51 		realfile = backing_file_open(&file->f_path, flags, realpath,
52 					     current_cred());
53 	}
54 	revert_creds(old_cred);
55 
56 	pr_debug("open(%p[%pD2/%c], 0%o) -> (%p, 0%o)\n",
57 		 file, file, ovl_whatisit(inode, realinode), file->f_flags,
58 		 realfile, IS_ERR(realfile) ? 0 : realfile->f_flags);
59 
60 	return realfile;
61 }
62 
63 #define OVL_SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT)
64 
ovl_change_flags(struct file * file,unsigned int flags)65 static int ovl_change_flags(struct file *file, unsigned int flags)
66 {
67 	struct inode *inode = file_inode(file);
68 	int err;
69 
70 	flags &= OVL_SETFL_MASK;
71 
72 	if (((flags ^ file->f_flags) & O_APPEND) && IS_APPEND(inode))
73 		return -EPERM;
74 
75 	if ((flags & O_DIRECT) && !(file->f_mode & FMODE_CAN_ODIRECT))
76 		return -EINVAL;
77 
78 	if (file->f_op->check_flags) {
79 		err = file->f_op->check_flags(flags);
80 		if (err)
81 			return err;
82 	}
83 
84 	spin_lock(&file->f_lock);
85 	file->f_flags = (file->f_flags & ~OVL_SETFL_MASK) | flags;
86 	file->f_iocb_flags = iocb_flags(file);
87 	spin_unlock(&file->f_lock);
88 
89 	return 0;
90 }
91 
ovl_real_fdget_meta(const struct file * file,struct fd * real,bool allow_meta)92 static int ovl_real_fdget_meta(const struct file *file, struct fd *real,
93 			       bool allow_meta)
94 {
95 	struct dentry *dentry = file_dentry(file);
96 	struct file *realfile = file->private_data;
97 	struct path realpath;
98 	int err;
99 
100 	real->word = (unsigned long)realfile;
101 
102 	if (allow_meta) {
103 		ovl_path_real(dentry, &realpath);
104 	} else {
105 		/* lazy lookup and verify of lowerdata */
106 		err = ovl_verify_lowerdata(dentry);
107 		if (err)
108 			return err;
109 
110 		ovl_path_realdata(dentry, &realpath);
111 	}
112 	if (!realpath.dentry)
113 		return -EIO;
114 
115 	/* Has it been copied up since we'd opened it? */
116 	if (unlikely(file_inode(realfile) != d_inode(realpath.dentry))) {
117 		struct file *f = ovl_open_realfile(file, &realpath);
118 		if (IS_ERR(f))
119 			return PTR_ERR(f);
120 		real->word = (unsigned long)f | FDPUT_FPUT;
121 		return 0;
122 	}
123 
124 	/* Did the flags change since open? */
125 	if (unlikely((file->f_flags ^ realfile->f_flags) & ~OVL_OPEN_FLAGS))
126 		return ovl_change_flags(realfile, file->f_flags);
127 
128 	return 0;
129 }
130 
ovl_real_fdget(const struct file * file,struct fd * real)131 static int ovl_real_fdget(const struct file *file, struct fd *real)
132 {
133 	if (d_is_dir(file_dentry(file))) {
134 		struct file *f = ovl_dir_real_file(file, false);
135 		if (IS_ERR(f))
136 			return PTR_ERR(f);
137 		real->word = (unsigned long)f;
138 		return 0;
139 	}
140 
141 	return ovl_real_fdget_meta(file, real, false);
142 }
143 
ovl_open(struct inode * inode,struct file * file)144 static int ovl_open(struct inode *inode, struct file *file)
145 {
146 	struct dentry *dentry = file_dentry(file);
147 	struct file *realfile;
148 	struct path realpath;
149 	int err;
150 
151 	/* lazy lookup and verify lowerdata */
152 	err = ovl_verify_lowerdata(dentry);
153 	if (err)
154 		return err;
155 
156 	err = ovl_maybe_copy_up(dentry, file->f_flags);
157 	if (err)
158 		return err;
159 
160 	/* No longer need these flags, so don't pass them on to underlying fs */
161 	file->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
162 
163 	ovl_path_realdata(dentry, &realpath);
164 	if (!realpath.dentry)
165 		return -EIO;
166 
167 	realfile = ovl_open_realfile(file, &realpath);
168 	if (IS_ERR(realfile))
169 		return PTR_ERR(realfile);
170 
171 	file->private_data = realfile;
172 
173 	return 0;
174 }
175 
ovl_release(struct inode * inode,struct file * file)176 static int ovl_release(struct inode *inode, struct file *file)
177 {
178 	fput(file->private_data);
179 
180 	return 0;
181 }
182 
ovl_llseek(struct file * file,loff_t offset,int whence)183 static loff_t ovl_llseek(struct file *file, loff_t offset, int whence)
184 {
185 	struct inode *inode = file_inode(file);
186 	struct fd real;
187 	const struct cred *old_cred;
188 	loff_t ret;
189 
190 	/*
191 	 * The two special cases below do not need to involve real fs,
192 	 * so we can optimizing concurrent callers.
193 	 */
194 	if (offset == 0) {
195 		if (whence == SEEK_CUR)
196 			return file->f_pos;
197 
198 		if (whence == SEEK_SET)
199 			return vfs_setpos(file, 0, 0);
200 	}
201 
202 	ret = ovl_real_fdget(file, &real);
203 	if (ret)
204 		return ret;
205 
206 	/*
207 	 * Overlay file f_pos is the master copy that is preserved
208 	 * through copy up and modified on read/write, but only real
209 	 * fs knows how to SEEK_HOLE/SEEK_DATA and real fs may impose
210 	 * limitations that are more strict than ->s_maxbytes for specific
211 	 * files, so we use the real file to perform seeks.
212 	 */
213 	ovl_inode_lock(inode);
214 	fd_file(real)->f_pos = file->f_pos;
215 
216 	old_cred = ovl_override_creds(inode->i_sb);
217 	ret = vfs_llseek(fd_file(real), offset, whence);
218 	revert_creds(old_cred);
219 
220 	file->f_pos = fd_file(real)->f_pos;
221 	ovl_inode_unlock(inode);
222 
223 	fdput(real);
224 
225 	return ret;
226 }
227 
ovl_file_modified(struct file * file)228 static void ovl_file_modified(struct file *file)
229 {
230 	/* Update size/mtime */
231 	ovl_copyattr(file_inode(file));
232 }
233 
ovl_file_end_write(struct file * file,loff_t pos,ssize_t ret)234 static void ovl_file_end_write(struct file *file, loff_t pos, ssize_t ret)
235 {
236 	ovl_file_modified(file);
237 }
238 
ovl_file_accessed(struct file * file)239 static void ovl_file_accessed(struct file *file)
240 {
241 	struct inode *inode, *upperinode;
242 	struct timespec64 ctime, uctime;
243 	struct timespec64 mtime, umtime;
244 
245 	if (file->f_flags & O_NOATIME)
246 		return;
247 
248 	inode = file_inode(file);
249 	upperinode = ovl_inode_upper(inode);
250 
251 	if (!upperinode)
252 		return;
253 
254 	ctime = inode_get_ctime(inode);
255 	uctime = inode_get_ctime(upperinode);
256 	mtime = inode_get_mtime(inode);
257 	umtime = inode_get_mtime(upperinode);
258 	if ((!timespec64_equal(&mtime, &umtime)) ||
259 	     !timespec64_equal(&ctime, &uctime)) {
260 		inode_set_mtime_to_ts(inode, inode_get_mtime(upperinode));
261 		inode_set_ctime_to_ts(inode, uctime);
262 	}
263 
264 	touch_atime(&file->f_path);
265 }
266 
ovl_read_iter(struct kiocb * iocb,struct iov_iter * iter)267 static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter)
268 {
269 	struct file *file = iocb->ki_filp;
270 	struct fd real;
271 	ssize_t ret;
272 	struct backing_file_ctx ctx = {
273 		.cred = ovl_creds(file_inode(file)->i_sb),
274 		.user_file = file,
275 		.accessed = ovl_file_accessed,
276 	};
277 
278 	if (!iov_iter_count(iter))
279 		return 0;
280 
281 	ret = ovl_real_fdget(file, &real);
282 	if (ret)
283 		return ret;
284 
285 	ret = backing_file_read_iter(fd_file(real), iter, iocb, iocb->ki_flags,
286 				     &ctx);
287 	fdput(real);
288 
289 	return ret;
290 }
291 
ovl_write_iter(struct kiocb * iocb,struct iov_iter * iter)292 static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter)
293 {
294 	struct file *file = iocb->ki_filp;
295 	struct inode *inode = file_inode(file);
296 	struct fd real;
297 	ssize_t ret;
298 	int ifl = iocb->ki_flags;
299 	struct backing_file_ctx ctx = {
300 		.cred = ovl_creds(inode->i_sb),
301 		.user_file = file,
302 		.end_write = ovl_file_end_write,
303 	};
304 
305 	if (!iov_iter_count(iter))
306 		return 0;
307 
308 	inode_lock(inode);
309 	/* Update mode */
310 	ovl_copyattr(inode);
311 
312 	ret = ovl_real_fdget(file, &real);
313 	if (ret)
314 		goto out_unlock;
315 
316 	if (!ovl_should_sync(OVL_FS(inode->i_sb)))
317 		ifl &= ~(IOCB_DSYNC | IOCB_SYNC);
318 
319 	/*
320 	 * Overlayfs doesn't support deferred completions, don't copy
321 	 * this property in case it is set by the issuer.
322 	 */
323 	ifl &= ~IOCB_DIO_CALLER_COMP;
324 	ret = backing_file_write_iter(fd_file(real), iter, iocb, ifl, &ctx);
325 	fdput(real);
326 
327 out_unlock:
328 	inode_unlock(inode);
329 
330 	return ret;
331 }
332 
ovl_splice_read(struct file * in,loff_t * ppos,struct pipe_inode_info * pipe,size_t len,unsigned int flags)333 static ssize_t ovl_splice_read(struct file *in, loff_t *ppos,
334 			       struct pipe_inode_info *pipe, size_t len,
335 			       unsigned int flags)
336 {
337 	struct fd real;
338 	ssize_t ret;
339 	struct backing_file_ctx ctx = {
340 		.cred = ovl_creds(file_inode(in)->i_sb),
341 		.user_file = in,
342 		.accessed = ovl_file_accessed,
343 	};
344 
345 	ret = ovl_real_fdget(in, &real);
346 	if (ret)
347 		return ret;
348 
349 	ret = backing_file_splice_read(fd_file(real), ppos, pipe, len, flags, &ctx);
350 	fdput(real);
351 
352 	return ret;
353 }
354 
355 /*
356  * Calling iter_file_splice_write() directly from overlay's f_op may deadlock
357  * due to lock order inversion between pipe->mutex in iter_file_splice_write()
358  * and file_start_write(fd_file(real)) in ovl_write_iter().
359  *
360  * So do everything ovl_write_iter() does and call iter_file_splice_write() on
361  * the real file.
362  */
ovl_splice_write(struct pipe_inode_info * pipe,struct file * out,loff_t * ppos,size_t len,unsigned int flags)363 static ssize_t ovl_splice_write(struct pipe_inode_info *pipe, struct file *out,
364 				loff_t *ppos, size_t len, unsigned int flags)
365 {
366 	struct fd real;
367 	struct inode *inode = file_inode(out);
368 	ssize_t ret;
369 	struct backing_file_ctx ctx = {
370 		.cred = ovl_creds(inode->i_sb),
371 		.user_file = out,
372 		.end_write = ovl_file_end_write,
373 	};
374 
375 	inode_lock(inode);
376 	/* Update mode */
377 	ovl_copyattr(inode);
378 
379 	ret = ovl_real_fdget(out, &real);
380 	if (ret)
381 		goto out_unlock;
382 
383 	ret = backing_file_splice_write(pipe, fd_file(real), ppos, len, flags, &ctx);
384 	fdput(real);
385 
386 out_unlock:
387 	inode_unlock(inode);
388 
389 	return ret;
390 }
391 
ovl_fsync(struct file * file,loff_t start,loff_t end,int datasync)392 static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync)
393 {
394 	struct fd real;
395 	const struct cred *old_cred;
396 	int ret;
397 
398 	ret = ovl_sync_status(OVL_FS(file_inode(file)->i_sb));
399 	if (ret <= 0)
400 		return ret;
401 
402 	ret = ovl_real_fdget_meta(file, &real, !datasync);
403 	if (ret)
404 		return ret;
405 
406 	/* Don't sync lower file for fear of receiving EROFS error */
407 	if (file_inode(fd_file(real)) == ovl_inode_upper(file_inode(file))) {
408 		old_cred = ovl_override_creds(file_inode(file)->i_sb);
409 		ret = vfs_fsync_range(fd_file(real), start, end, datasync);
410 		revert_creds(old_cred);
411 	}
412 
413 	fdput(real);
414 
415 	return ret;
416 }
417 
ovl_mmap(struct file * file,struct vm_area_struct * vma)418 static int ovl_mmap(struct file *file, struct vm_area_struct *vma)
419 {
420 	struct file *realfile = file->private_data;
421 	struct backing_file_ctx ctx = {
422 		.cred = ovl_creds(file_inode(file)->i_sb),
423 		.user_file = file,
424 		.accessed = ovl_file_accessed,
425 	};
426 
427 	return backing_file_mmap(realfile, vma, &ctx);
428 }
429 
ovl_fallocate(struct file * file,int mode,loff_t offset,loff_t len)430 static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
431 {
432 	struct inode *inode = file_inode(file);
433 	struct fd real;
434 	const struct cred *old_cred;
435 	int ret;
436 
437 	inode_lock(inode);
438 	/* Update mode */
439 	ovl_copyattr(inode);
440 	ret = file_remove_privs(file);
441 	if (ret)
442 		goto out_unlock;
443 
444 	ret = ovl_real_fdget(file, &real);
445 	if (ret)
446 		goto out_unlock;
447 
448 	old_cred = ovl_override_creds(file_inode(file)->i_sb);
449 	ret = vfs_fallocate(fd_file(real), mode, offset, len);
450 	revert_creds(old_cred);
451 
452 	/* Update size */
453 	ovl_file_modified(file);
454 
455 	fdput(real);
456 
457 out_unlock:
458 	inode_unlock(inode);
459 
460 	return ret;
461 }
462 
ovl_fadvise(struct file * file,loff_t offset,loff_t len,int advice)463 static int ovl_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
464 {
465 	struct fd real;
466 	const struct cred *old_cred;
467 	int ret;
468 
469 	ret = ovl_real_fdget(file, &real);
470 	if (ret)
471 		return ret;
472 
473 	old_cred = ovl_override_creds(file_inode(file)->i_sb);
474 	ret = vfs_fadvise(fd_file(real), offset, len, advice);
475 	revert_creds(old_cred);
476 
477 	fdput(real);
478 
479 	return ret;
480 }
481 
482 enum ovl_copyop {
483 	OVL_COPY,
484 	OVL_CLONE,
485 	OVL_DEDUPE,
486 };
487 
ovl_copyfile(struct file * file_in,loff_t pos_in,struct file * file_out,loff_t pos_out,loff_t len,unsigned int flags,enum ovl_copyop op)488 static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in,
489 			    struct file *file_out, loff_t pos_out,
490 			    loff_t len, unsigned int flags, enum ovl_copyop op)
491 {
492 	struct inode *inode_out = file_inode(file_out);
493 	struct fd real_in, real_out;
494 	const struct cred *old_cred;
495 	loff_t ret;
496 
497 	inode_lock(inode_out);
498 	if (op != OVL_DEDUPE) {
499 		/* Update mode */
500 		ovl_copyattr(inode_out);
501 		ret = file_remove_privs(file_out);
502 		if (ret)
503 			goto out_unlock;
504 	}
505 
506 	ret = ovl_real_fdget(file_out, &real_out);
507 	if (ret)
508 		goto out_unlock;
509 
510 	ret = ovl_real_fdget(file_in, &real_in);
511 	if (ret) {
512 		fdput(real_out);
513 		goto out_unlock;
514 	}
515 
516 	old_cred = ovl_override_creds(file_inode(file_out)->i_sb);
517 	switch (op) {
518 	case OVL_COPY:
519 		ret = vfs_copy_file_range(fd_file(real_in), pos_in,
520 					  fd_file(real_out), pos_out, len, flags);
521 		break;
522 
523 	case OVL_CLONE:
524 		ret = vfs_clone_file_range(fd_file(real_in), pos_in,
525 					   fd_file(real_out), pos_out, len, flags);
526 		break;
527 
528 	case OVL_DEDUPE:
529 		ret = vfs_dedupe_file_range_one(fd_file(real_in), pos_in,
530 						fd_file(real_out), pos_out, len,
531 						flags);
532 		break;
533 	}
534 	revert_creds(old_cred);
535 
536 	/* Update size */
537 	ovl_file_modified(file_out);
538 
539 	fdput(real_in);
540 	fdput(real_out);
541 
542 out_unlock:
543 	inode_unlock(inode_out);
544 
545 	return ret;
546 }
547 
ovl_copy_file_range(struct file * file_in,loff_t pos_in,struct file * file_out,loff_t pos_out,size_t len,unsigned int flags)548 static ssize_t ovl_copy_file_range(struct file *file_in, loff_t pos_in,
549 				   struct file *file_out, loff_t pos_out,
550 				   size_t len, unsigned int flags)
551 {
552 	return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, flags,
553 			    OVL_COPY);
554 }
555 
ovl_remap_file_range(struct file * file_in,loff_t pos_in,struct file * file_out,loff_t pos_out,loff_t len,unsigned int remap_flags)556 static loff_t ovl_remap_file_range(struct file *file_in, loff_t pos_in,
557 				   struct file *file_out, loff_t pos_out,
558 				   loff_t len, unsigned int remap_flags)
559 {
560 	enum ovl_copyop op;
561 
562 	if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
563 		return -EINVAL;
564 
565 	if (remap_flags & REMAP_FILE_DEDUP)
566 		op = OVL_DEDUPE;
567 	else
568 		op = OVL_CLONE;
569 
570 	/*
571 	 * Don't copy up because of a dedupe request, this wouldn't make sense
572 	 * most of the time (data would be duplicated instead of deduplicated).
573 	 */
574 	if (op == OVL_DEDUPE &&
575 	    (!ovl_inode_upper(file_inode(file_in)) ||
576 	     !ovl_inode_upper(file_inode(file_out))))
577 		return -EPERM;
578 
579 	return ovl_copyfile(file_in, pos_in, file_out, pos_out, len,
580 			    remap_flags, op);
581 }
582 
ovl_flush(struct file * file,fl_owner_t id)583 static int ovl_flush(struct file *file, fl_owner_t id)
584 {
585 	struct fd real;
586 	const struct cred *old_cred;
587 	int err;
588 
589 	err = ovl_real_fdget(file, &real);
590 	if (err)
591 		return err;
592 
593 	if (fd_file(real)->f_op->flush) {
594 		old_cred = ovl_override_creds(file_inode(file)->i_sb);
595 		err = fd_file(real)->f_op->flush(fd_file(real), id);
596 		revert_creds(old_cred);
597 	}
598 	fdput(real);
599 
600 	return err;
601 }
602 
603 const struct file_operations ovl_file_operations = {
604 	.open		= ovl_open,
605 	.release	= ovl_release,
606 	.llseek		= ovl_llseek,
607 	.read_iter	= ovl_read_iter,
608 	.write_iter	= ovl_write_iter,
609 	.fsync		= ovl_fsync,
610 	.mmap		= ovl_mmap,
611 	.fallocate	= ovl_fallocate,
612 	.fadvise	= ovl_fadvise,
613 	.flush		= ovl_flush,
614 	.splice_read    = ovl_splice_read,
615 	.splice_write   = ovl_splice_write,
616 
617 	.copy_file_range	= ovl_copy_file_range,
618 	.remap_file_range	= ovl_remap_file_range,
619 };
620