1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2017 Red Hat, Inc.
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/splice.h>
13 #include <linux/security.h>
16 #include "overlayfs.h"
20 struct kiocb *orig_iocb;
24 static struct kmem_cache *ovl_aio_request_cachep;
26 static char ovl_whatisit(struct inode *inode, struct inode *realinode)
28 if (realinode != ovl_inode_upper(inode))
30 if (ovl_has_upperdata(inode))
36 static struct file *ovl_open_realfile(const struct file *file,
37 struct inode *realinode)
39 struct inode *inode = file_inode(file);
40 struct file *realfile;
41 const struct cred *old_cred;
42 int flags = file->f_flags | O_NOATIME | FMODE_NONOTIFY;
43 int acc_mode = ACC_MODE(flags);
47 acc_mode |= MAY_APPEND;
49 old_cred = ovl_override_creds(inode->i_sb);
50 err = inode_permission(realinode, MAY_OPEN | acc_mode);
52 realfile = ERR_PTR(err);
53 } else if (!inode_owner_or_capable(realinode)) {
54 realfile = ERR_PTR(-EPERM);
56 realfile = open_with_fake_path(&file->f_path, flags, realinode,
59 revert_creds(old_cred);
61 pr_debug("open(%p[%pD2/%c], 0%o) -> (%p, 0%o)\n",
62 file, file, ovl_whatisit(inode, realinode), file->f_flags,
63 realfile, IS_ERR(realfile) ? 0 : realfile->f_flags);
68 #define OVL_SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT)
70 static int ovl_change_flags(struct file *file, unsigned int flags)
72 struct inode *inode = file_inode(file);
75 /* No atime modificaton on underlying */
76 flags |= O_NOATIME | FMODE_NONOTIFY;
78 /* If some flag changed that cannot be changed then something's amiss */
79 if (WARN_ON((file->f_flags ^ flags) & ~OVL_SETFL_MASK))
82 flags &= OVL_SETFL_MASK;
84 if (((flags ^ file->f_flags) & O_APPEND) && IS_APPEND(inode))
87 if (flags & O_DIRECT) {
88 if (!file->f_mapping->a_ops ||
89 !file->f_mapping->a_ops->direct_IO)
93 if (file->f_op->check_flags) {
94 err = file->f_op->check_flags(flags);
99 spin_lock(&file->f_lock);
100 file->f_flags = (file->f_flags & ~OVL_SETFL_MASK) | flags;
101 spin_unlock(&file->f_lock);
106 static int ovl_real_fdget_meta(const struct file *file, struct fd *real,
109 struct inode *inode = file_inode(file);
110 struct inode *realinode;
113 real->file = file->private_data;
116 realinode = ovl_inode_real(inode);
118 realinode = ovl_inode_realdata(inode);
120 /* Has it been copied up since we'd opened it? */
121 if (unlikely(file_inode(real->file) != realinode)) {
122 real->flags = FDPUT_FPUT;
123 real->file = ovl_open_realfile(file, realinode);
125 return PTR_ERR_OR_ZERO(real->file);
128 /* Did the flags change since open? */
129 if (unlikely((file->f_flags ^ real->file->f_flags) & ~O_NOATIME))
130 return ovl_change_flags(real->file, file->f_flags);
135 static int ovl_real_fdget(const struct file *file, struct fd *real)
137 return ovl_real_fdget_meta(file, real, false);
140 static int ovl_open(struct inode *inode, struct file *file)
142 struct file *realfile;
145 err = ovl_maybe_copy_up(file_dentry(file), file->f_flags);
149 /* No longer need these flags, so don't pass them on to underlying fs */
150 file->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
152 realfile = ovl_open_realfile(file, ovl_inode_realdata(inode));
153 if (IS_ERR(realfile))
154 return PTR_ERR(realfile);
156 file->private_data = realfile;
161 static int ovl_release(struct inode *inode, struct file *file)
163 fput(file->private_data);
168 static loff_t ovl_llseek(struct file *file, loff_t offset, int whence)
170 struct inode *inode = file_inode(file);
172 const struct cred *old_cred;
176 * The two special cases below do not need to involve real fs,
177 * so we can optimizing concurrent callers.
180 if (whence == SEEK_CUR)
183 if (whence == SEEK_SET)
184 return vfs_setpos(file, 0, 0);
187 ret = ovl_real_fdget(file, &real);
192 * Overlay file f_pos is the master copy that is preserved
193 * through copy up and modified on read/write, but only real
194 * fs knows how to SEEK_HOLE/SEEK_DATA and real fs may impose
195 * limitations that are more strict than ->s_maxbytes for specific
196 * files, so we use the real file to perform seeks.
198 ovl_inode_lock(inode);
199 real.file->f_pos = file->f_pos;
201 old_cred = ovl_override_creds(inode->i_sb);
202 ret = vfs_llseek(real.file, offset, whence);
203 revert_creds(old_cred);
205 file->f_pos = real.file->f_pos;
206 ovl_inode_unlock(inode);
213 static void ovl_file_accessed(struct file *file)
215 struct inode *inode, *upperinode;
217 if (file->f_flags & O_NOATIME)
220 inode = file_inode(file);
221 upperinode = ovl_inode_upper(inode);
226 if ((!timespec64_equal(&inode->i_mtime, &upperinode->i_mtime) ||
227 !timespec64_equal(&inode->i_ctime, &upperinode->i_ctime))) {
228 inode->i_mtime = upperinode->i_mtime;
229 inode->i_ctime = upperinode->i_ctime;
232 touch_atime(&file->f_path);
235 static rwf_t ovl_iocb_to_rwf(int ifl)
239 if (ifl & IOCB_NOWAIT)
241 if (ifl & IOCB_HIPRI)
243 if (ifl & IOCB_DSYNC)
251 static void ovl_aio_cleanup_handler(struct ovl_aio_req *aio_req)
253 struct kiocb *iocb = &aio_req->iocb;
254 struct kiocb *orig_iocb = aio_req->orig_iocb;
256 if (iocb->ki_flags & IOCB_WRITE) {
257 struct inode *inode = file_inode(orig_iocb->ki_filp);
259 /* Actually acquired in ovl_write_iter() */
260 __sb_writers_acquired(file_inode(iocb->ki_filp)->i_sb,
262 file_end_write(iocb->ki_filp);
263 ovl_copyattr(ovl_inode_real(inode), inode);
266 orig_iocb->ki_pos = iocb->ki_pos;
268 kmem_cache_free(ovl_aio_request_cachep, aio_req);
271 static void ovl_aio_rw_complete(struct kiocb *iocb, long res, long res2)
273 struct ovl_aio_req *aio_req = container_of(iocb,
274 struct ovl_aio_req, iocb);
275 struct kiocb *orig_iocb = aio_req->orig_iocb;
277 ovl_aio_cleanup_handler(aio_req);
278 orig_iocb->ki_complete(orig_iocb, res, res2);
281 static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter)
283 struct file *file = iocb->ki_filp;
285 const struct cred *old_cred;
288 if (!iov_iter_count(iter))
291 ret = ovl_real_fdget(file, &real);
295 old_cred = ovl_override_creds(file_inode(file)->i_sb);
296 if (is_sync_kiocb(iocb)) {
297 ret = vfs_iter_read(real.file, iter, &iocb->ki_pos,
298 ovl_iocb_to_rwf(iocb->ki_flags));
300 struct ovl_aio_req *aio_req;
303 aio_req = kmem_cache_zalloc(ovl_aio_request_cachep, GFP_KERNEL);
309 aio_req->orig_iocb = iocb;
310 kiocb_clone(&aio_req->iocb, iocb, real.file);
311 aio_req->iocb.ki_complete = ovl_aio_rw_complete;
312 ret = vfs_iocb_iter_read(real.file, &aio_req->iocb, iter);
313 if (ret != -EIOCBQUEUED)
314 ovl_aio_cleanup_handler(aio_req);
317 revert_creds(old_cred);
318 ovl_file_accessed(file);
325 static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter)
327 struct file *file = iocb->ki_filp;
328 struct inode *inode = file_inode(file);
330 const struct cred *old_cred;
333 if (!iov_iter_count(iter))
338 ovl_copyattr(ovl_inode_real(inode), inode);
339 ret = file_remove_privs(file);
343 ret = ovl_real_fdget(file, &real);
347 old_cred = ovl_override_creds(file_inode(file)->i_sb);
348 if (is_sync_kiocb(iocb)) {
349 file_start_write(real.file);
350 ret = vfs_iter_write(real.file, iter, &iocb->ki_pos,
351 ovl_iocb_to_rwf(iocb->ki_flags));
352 file_end_write(real.file);
354 ovl_copyattr(ovl_inode_real(inode), inode);
356 struct ovl_aio_req *aio_req;
359 aio_req = kmem_cache_zalloc(ovl_aio_request_cachep, GFP_KERNEL);
363 file_start_write(real.file);
364 /* Pacify lockdep, same trick as done in aio_write() */
365 __sb_writers_release(file_inode(real.file)->i_sb,
369 aio_req->orig_iocb = iocb;
370 kiocb_clone(&aio_req->iocb, iocb, real.file);
371 aio_req->iocb.ki_complete = ovl_aio_rw_complete;
372 ret = vfs_iocb_iter_write(real.file, &aio_req->iocb, iter);
373 if (ret != -EIOCBQUEUED)
374 ovl_aio_cleanup_handler(aio_req);
377 revert_creds(old_cred);
386 static ssize_t ovl_splice_read(struct file *in, loff_t *ppos,
387 struct pipe_inode_info *pipe, size_t len,
392 const struct cred *old_cred;
394 ret = ovl_real_fdget(in, &real);
398 old_cred = ovl_override_creds(file_inode(in)->i_sb);
399 ret = generic_file_splice_read(real.file, ppos, pipe, len, flags);
400 revert_creds(old_cred);
402 ovl_file_accessed(in);
408 ovl_splice_write(struct pipe_inode_info *pipe, struct file *out,
409 loff_t *ppos, size_t len, unsigned int flags)
412 const struct cred *old_cred;
415 ret = ovl_real_fdget(out, &real);
419 old_cred = ovl_override_creds(file_inode(out)->i_sb);
420 ret = iter_file_splice_write(pipe, real.file, ppos, len, flags);
421 revert_creds(old_cred);
423 ovl_file_accessed(out);
428 static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync)
431 const struct cred *old_cred;
434 ret = ovl_real_fdget_meta(file, &real, !datasync);
438 /* Don't sync lower file for fear of receiving EROFS error */
439 if (file_inode(real.file) == ovl_inode_upper(file_inode(file))) {
440 old_cred = ovl_override_creds(file_inode(file)->i_sb);
441 ret = vfs_fsync_range(real.file, start, end, datasync);
442 revert_creds(old_cred);
450 static int ovl_mmap(struct file *file, struct vm_area_struct *vma)
452 struct file *realfile = file->private_data;
453 const struct cred *old_cred;
456 if (!realfile->f_op->mmap)
459 if (WARN_ON(file != vma->vm_file))
462 vma->vm_file = get_file(realfile);
464 old_cred = ovl_override_creds(file_inode(file)->i_sb);
465 ret = call_mmap(vma->vm_file, vma);
466 revert_creds(old_cred);
469 /* Drop reference count from new vm_file value */
472 /* Drop reference count from previous vm_file value */
476 ovl_file_accessed(file);
481 static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
483 struct inode *inode = file_inode(file);
485 const struct cred *old_cred;
488 ret = ovl_real_fdget(file, &real);
492 old_cred = ovl_override_creds(file_inode(file)->i_sb);
493 ret = vfs_fallocate(real.file, mode, offset, len);
494 revert_creds(old_cred);
497 ovl_copyattr(ovl_inode_real(inode), inode);
504 static int ovl_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
507 const struct cred *old_cred;
510 ret = ovl_real_fdget(file, &real);
514 old_cred = ovl_override_creds(file_inode(file)->i_sb);
515 ret = vfs_fadvise(real.file, offset, len, advice);
516 revert_creds(old_cred);
523 static long ovl_real_ioctl(struct file *file, unsigned int cmd,
527 const struct cred *old_cred;
530 ret = ovl_real_fdget(file, &real);
534 old_cred = ovl_override_creds(file_inode(file)->i_sb);
535 ret = security_file_ioctl(real.file, cmd, arg);
537 ret = vfs_ioctl(real.file, cmd, arg);
538 revert_creds(old_cred);
545 static long ovl_ioctl_set_flags(struct file *file, unsigned int cmd,
546 unsigned long arg, unsigned int iflags)
549 struct inode *inode = file_inode(file);
550 unsigned int old_iflags;
552 if (!inode_owner_or_capable(inode))
555 ret = mnt_want_write_file(file);
561 /* Check the capability before cred override */
563 old_iflags = READ_ONCE(inode->i_flags);
564 if (((iflags ^ old_iflags) & (S_APPEND | S_IMMUTABLE)) &&
565 !capable(CAP_LINUX_IMMUTABLE))
568 ret = ovl_maybe_copy_up(file_dentry(file), O_WRONLY);
572 ret = ovl_real_ioctl(file, cmd, arg);
574 ovl_copyflags(ovl_inode_real(inode), inode);
578 mnt_drop_write_file(file);
584 static unsigned int ovl_fsflags_to_iflags(unsigned int flags)
586 unsigned int iflags = 0;
588 if (flags & FS_SYNC_FL)
590 if (flags & FS_APPEND_FL)
592 if (flags & FS_IMMUTABLE_FL)
593 iflags |= S_IMMUTABLE;
594 if (flags & FS_NOATIME_FL)
600 static long ovl_ioctl_set_fsflags(struct file *file, unsigned int cmd,
605 if (get_user(flags, (int __user *) arg))
608 return ovl_ioctl_set_flags(file, cmd, arg,
609 ovl_fsflags_to_iflags(flags));
612 static unsigned int ovl_fsxflags_to_iflags(unsigned int xflags)
614 unsigned int iflags = 0;
616 if (xflags & FS_XFLAG_SYNC)
618 if (xflags & FS_XFLAG_APPEND)
620 if (xflags & FS_XFLAG_IMMUTABLE)
621 iflags |= S_IMMUTABLE;
622 if (xflags & FS_XFLAG_NOATIME)
628 static long ovl_ioctl_set_fsxflags(struct file *file, unsigned int cmd,
633 memset(&fa, 0, sizeof(fa));
634 if (copy_from_user(&fa, (void __user *) arg, sizeof(fa)))
637 return ovl_ioctl_set_flags(file, cmd, arg,
638 ovl_fsxflags_to_iflags(fa.fsx_xflags));
641 static long ovl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
646 case FS_IOC_GETFLAGS:
647 case FS_IOC_FSGETXATTR:
648 ret = ovl_real_ioctl(file, cmd, arg);
651 case FS_IOC_SETFLAGS:
652 ret = ovl_ioctl_set_fsflags(file, cmd, arg);
655 case FS_IOC_FSSETXATTR:
656 ret = ovl_ioctl_set_fsxflags(file, cmd, arg);
666 static long ovl_compat_ioctl(struct file *file, unsigned int cmd,
670 case FS_IOC32_GETFLAGS:
671 cmd = FS_IOC_GETFLAGS;
674 case FS_IOC32_SETFLAGS:
675 cmd = FS_IOC_SETFLAGS;
682 return ovl_ioctl(file, cmd, arg);
691 static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in,
692 struct file *file_out, loff_t pos_out,
693 loff_t len, unsigned int flags, enum ovl_copyop op)
695 struct inode *inode_out = file_inode(file_out);
696 struct fd real_in, real_out;
697 const struct cred *old_cred;
700 ret = ovl_real_fdget(file_out, &real_out);
704 ret = ovl_real_fdget(file_in, &real_in);
710 old_cred = ovl_override_creds(file_inode(file_out)->i_sb);
713 ret = vfs_copy_file_range(real_in.file, pos_in,
714 real_out.file, pos_out, len, flags);
718 ret = vfs_clone_file_range(real_in.file, pos_in,
719 real_out.file, pos_out, len, flags);
723 ret = vfs_dedupe_file_range_one(real_in.file, pos_in,
724 real_out.file, pos_out, len,
728 revert_creds(old_cred);
731 ovl_copyattr(ovl_inode_real(inode_out), inode_out);
739 static ssize_t ovl_copy_file_range(struct file *file_in, loff_t pos_in,
740 struct file *file_out, loff_t pos_out,
741 size_t len, unsigned int flags)
743 return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, flags,
747 static loff_t ovl_remap_file_range(struct file *file_in, loff_t pos_in,
748 struct file *file_out, loff_t pos_out,
749 loff_t len, unsigned int remap_flags)
753 if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
756 if (remap_flags & REMAP_FILE_DEDUP)
762 * Don't copy up because of a dedupe request, this wouldn't make sense
763 * most of the time (data would be duplicated instead of deduplicated).
765 if (op == OVL_DEDUPE &&
766 (!ovl_inode_upper(file_inode(file_in)) ||
767 !ovl_inode_upper(file_inode(file_out))))
770 return ovl_copyfile(file_in, pos_in, file_out, pos_out, len,
774 const struct file_operations ovl_file_operations = {
776 .release = ovl_release,
777 .llseek = ovl_llseek,
778 .read_iter = ovl_read_iter,
779 .write_iter = ovl_write_iter,
782 .fallocate = ovl_fallocate,
783 .fadvise = ovl_fadvise,
784 .unlocked_ioctl = ovl_ioctl,
785 .compat_ioctl = ovl_compat_ioctl,
786 .splice_read = ovl_splice_read,
787 .splice_write = ovl_splice_write,
789 .copy_file_range = ovl_copy_file_range,
790 .remap_file_range = ovl_remap_file_range,
793 int __init ovl_aio_request_cache_init(void)
795 ovl_aio_request_cachep = kmem_cache_create("ovl_aio_req",
796 sizeof(struct ovl_aio_req),
797 0, SLAB_HWCACHE_ALIGN, NULL);
798 if (!ovl_aio_request_cachep)
804 void ovl_aio_request_cache_destroy(void)
806 kmem_cache_destroy(ovl_aio_request_cachep);