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 /* No atime modificaton nor notify on underlying */
37 #define OVL_OPEN_FLAGS (O_NOATIME | FMODE_NONOTIFY)
39 static struct file *ovl_open_realfile(const struct file *file,
40 struct inode *realinode)
42 struct inode *inode = file_inode(file);
43 struct file *realfile;
44 const struct cred *old_cred;
45 int flags = file->f_flags | OVL_OPEN_FLAGS;
46 int acc_mode = ACC_MODE(flags);
50 acc_mode |= MAY_APPEND;
52 old_cred = ovl_override_creds(inode->i_sb);
53 err = inode_permission(realinode, MAY_OPEN | acc_mode);
55 realfile = ERR_PTR(err);
56 } else if (!inode_owner_or_capable(realinode)) {
57 realfile = ERR_PTR(-EPERM);
59 realfile = open_with_fake_path(&file->f_path, flags, realinode,
62 revert_creds(old_cred);
64 pr_debug("open(%p[%pD2/%c], 0%o) -> (%p, 0%o)\n",
65 file, file, ovl_whatisit(inode, realinode), file->f_flags,
66 realfile, IS_ERR(realfile) ? 0 : realfile->f_flags);
71 #define OVL_SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT)
73 static int ovl_change_flags(struct file *file, unsigned int flags)
75 struct inode *inode = file_inode(file);
78 flags |= OVL_OPEN_FLAGS;
80 /* If some flag changed that cannot be changed then something's amiss */
81 if (WARN_ON((file->f_flags ^ flags) & ~OVL_SETFL_MASK))
84 flags &= OVL_SETFL_MASK;
86 if (((flags ^ file->f_flags) & O_APPEND) && IS_APPEND(inode))
89 if (flags & O_DIRECT) {
90 if (!file->f_mapping->a_ops ||
91 !file->f_mapping->a_ops->direct_IO)
95 if (file->f_op->check_flags) {
96 err = file->f_op->check_flags(flags);
101 spin_lock(&file->f_lock);
102 file->f_flags = (file->f_flags & ~OVL_SETFL_MASK) | flags;
103 spin_unlock(&file->f_lock);
108 static int ovl_real_fdget_meta(const struct file *file, struct fd *real,
111 struct inode *inode = file_inode(file);
112 struct inode *realinode;
115 real->file = file->private_data;
118 realinode = ovl_inode_real(inode);
120 realinode = ovl_inode_realdata(inode);
122 /* Has it been copied up since we'd opened it? */
123 if (unlikely(file_inode(real->file) != realinode)) {
124 real->flags = FDPUT_FPUT;
125 real->file = ovl_open_realfile(file, realinode);
127 return PTR_ERR_OR_ZERO(real->file);
130 /* Did the flags change since open? */
131 if (unlikely((file->f_flags ^ real->file->f_flags) & ~OVL_OPEN_FLAGS))
132 return ovl_change_flags(real->file, file->f_flags);
137 static int ovl_real_fdget(const struct file *file, struct fd *real)
139 return ovl_real_fdget_meta(file, real, false);
142 static int ovl_open(struct inode *inode, struct file *file)
144 struct file *realfile;
147 err = ovl_maybe_copy_up(file_dentry(file), file->f_flags);
151 /* No longer need these flags, so don't pass them on to underlying fs */
152 file->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
154 realfile = ovl_open_realfile(file, ovl_inode_realdata(inode));
155 if (IS_ERR(realfile))
156 return PTR_ERR(realfile);
158 file->private_data = realfile;
163 static int ovl_release(struct inode *inode, struct file *file)
165 fput(file->private_data);
170 static loff_t ovl_llseek(struct file *file, loff_t offset, int whence)
172 struct inode *inode = file_inode(file);
174 const struct cred *old_cred;
178 * The two special cases below do not need to involve real fs,
179 * so we can optimizing concurrent callers.
182 if (whence == SEEK_CUR)
185 if (whence == SEEK_SET)
186 return vfs_setpos(file, 0, 0);
189 ret = ovl_real_fdget(file, &real);
194 * Overlay file f_pos is the master copy that is preserved
195 * through copy up and modified on read/write, but only real
196 * fs knows how to SEEK_HOLE/SEEK_DATA and real fs may impose
197 * limitations that are more strict than ->s_maxbytes for specific
198 * files, so we use the real file to perform seeks.
200 ovl_inode_lock(inode);
201 real.file->f_pos = file->f_pos;
203 old_cred = ovl_override_creds(inode->i_sb);
204 ret = vfs_llseek(real.file, offset, whence);
205 revert_creds(old_cred);
207 file->f_pos = real.file->f_pos;
208 ovl_inode_unlock(inode);
215 static void ovl_file_accessed(struct file *file)
217 struct inode *inode, *upperinode;
219 if (file->f_flags & O_NOATIME)
222 inode = file_inode(file);
223 upperinode = ovl_inode_upper(inode);
228 if ((!timespec64_equal(&inode->i_mtime, &upperinode->i_mtime) ||
229 !timespec64_equal(&inode->i_ctime, &upperinode->i_ctime))) {
230 inode->i_mtime = upperinode->i_mtime;
231 inode->i_ctime = upperinode->i_ctime;
234 touch_atime(&file->f_path);
237 static rwf_t ovl_iocb_to_rwf(int ifl)
241 if (ifl & IOCB_NOWAIT)
243 if (ifl & IOCB_HIPRI)
245 if (ifl & IOCB_DSYNC)
253 static void ovl_aio_cleanup_handler(struct ovl_aio_req *aio_req)
255 struct kiocb *iocb = &aio_req->iocb;
256 struct kiocb *orig_iocb = aio_req->orig_iocb;
258 if (iocb->ki_flags & IOCB_WRITE) {
259 struct inode *inode = file_inode(orig_iocb->ki_filp);
261 /* Actually acquired in ovl_write_iter() */
262 __sb_writers_acquired(file_inode(iocb->ki_filp)->i_sb,
264 file_end_write(iocb->ki_filp);
265 ovl_copyattr(ovl_inode_real(inode), inode);
268 orig_iocb->ki_pos = iocb->ki_pos;
270 kmem_cache_free(ovl_aio_request_cachep, aio_req);
273 static void ovl_aio_rw_complete(struct kiocb *iocb, long res, long res2)
275 struct ovl_aio_req *aio_req = container_of(iocb,
276 struct ovl_aio_req, iocb);
277 struct kiocb *orig_iocb = aio_req->orig_iocb;
279 ovl_aio_cleanup_handler(aio_req);
280 orig_iocb->ki_complete(orig_iocb, res, res2);
283 static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter)
285 struct file *file = iocb->ki_filp;
287 const struct cred *old_cred;
290 if (!iov_iter_count(iter))
293 ret = ovl_real_fdget(file, &real);
297 old_cred = ovl_override_creds(file_inode(file)->i_sb);
298 if (is_sync_kiocb(iocb)) {
299 ret = vfs_iter_read(real.file, iter, &iocb->ki_pos,
300 ovl_iocb_to_rwf(iocb->ki_flags));
302 struct ovl_aio_req *aio_req;
305 aio_req = kmem_cache_zalloc(ovl_aio_request_cachep, GFP_KERNEL);
311 aio_req->orig_iocb = iocb;
312 kiocb_clone(&aio_req->iocb, iocb, real.file);
313 aio_req->iocb.ki_complete = ovl_aio_rw_complete;
314 ret = vfs_iocb_iter_read(real.file, &aio_req->iocb, iter);
315 if (ret != -EIOCBQUEUED)
316 ovl_aio_cleanup_handler(aio_req);
319 revert_creds(old_cred);
320 ovl_file_accessed(file);
327 static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter)
329 struct file *file = iocb->ki_filp;
330 struct inode *inode = file_inode(file);
332 const struct cred *old_cred;
335 if (!iov_iter_count(iter))
340 ovl_copyattr(ovl_inode_real(inode), inode);
341 ret = file_remove_privs(file);
345 ret = ovl_real_fdget(file, &real);
349 old_cred = ovl_override_creds(file_inode(file)->i_sb);
350 if (is_sync_kiocb(iocb)) {
351 file_start_write(real.file);
352 ret = vfs_iter_write(real.file, iter, &iocb->ki_pos,
353 ovl_iocb_to_rwf(iocb->ki_flags));
354 file_end_write(real.file);
356 ovl_copyattr(ovl_inode_real(inode), inode);
358 struct ovl_aio_req *aio_req;
361 aio_req = kmem_cache_zalloc(ovl_aio_request_cachep, GFP_KERNEL);
365 file_start_write(real.file);
366 /* Pacify lockdep, same trick as done in aio_write() */
367 __sb_writers_release(file_inode(real.file)->i_sb,
371 aio_req->orig_iocb = iocb;
372 kiocb_clone(&aio_req->iocb, iocb, real.file);
373 aio_req->iocb.ki_complete = ovl_aio_rw_complete;
374 ret = vfs_iocb_iter_write(real.file, &aio_req->iocb, iter);
375 if (ret != -EIOCBQUEUED)
376 ovl_aio_cleanup_handler(aio_req);
379 revert_creds(old_cred);
388 static ssize_t ovl_splice_read(struct file *in, loff_t *ppos,
389 struct pipe_inode_info *pipe, size_t len,
394 const struct cred *old_cred;
396 ret = ovl_real_fdget(in, &real);
400 old_cred = ovl_override_creds(file_inode(in)->i_sb);
401 ret = generic_file_splice_read(real.file, ppos, pipe, len, flags);
402 revert_creds(old_cred);
404 ovl_file_accessed(in);
410 ovl_splice_write(struct pipe_inode_info *pipe, struct file *out,
411 loff_t *ppos, size_t len, unsigned int flags)
414 const struct cred *old_cred;
417 ret = ovl_real_fdget(out, &real);
421 old_cred = ovl_override_creds(file_inode(out)->i_sb);
422 ret = iter_file_splice_write(pipe, real.file, ppos, len, flags);
423 revert_creds(old_cred);
425 ovl_file_accessed(out);
430 static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync)
433 const struct cred *old_cred;
436 ret = ovl_real_fdget_meta(file, &real, !datasync);
440 /* Don't sync lower file for fear of receiving EROFS error */
441 if (file_inode(real.file) == ovl_inode_upper(file_inode(file))) {
442 old_cred = ovl_override_creds(file_inode(file)->i_sb);
443 ret = vfs_fsync_range(real.file, start, end, datasync);
444 revert_creds(old_cred);
452 static int ovl_mmap(struct file *file, struct vm_area_struct *vma)
454 struct file *realfile = file->private_data;
455 const struct cred *old_cred;
458 if (!realfile->f_op->mmap)
461 if (WARN_ON(file != vma->vm_file))
464 vma->vm_file = get_file(realfile);
466 old_cred = ovl_override_creds(file_inode(file)->i_sb);
467 ret = call_mmap(vma->vm_file, vma);
468 revert_creds(old_cred);
471 /* Drop reference count from new vm_file value */
474 /* Drop reference count from previous vm_file value */
478 ovl_file_accessed(file);
483 static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
485 struct inode *inode = file_inode(file);
487 const struct cred *old_cred;
490 ret = ovl_real_fdget(file, &real);
494 old_cred = ovl_override_creds(file_inode(file)->i_sb);
495 ret = vfs_fallocate(real.file, mode, offset, len);
496 revert_creds(old_cred);
499 ovl_copyattr(ovl_inode_real(inode), inode);
506 static int ovl_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
509 const struct cred *old_cred;
512 ret = ovl_real_fdget(file, &real);
516 old_cred = ovl_override_creds(file_inode(file)->i_sb);
517 ret = vfs_fadvise(real.file, offset, len, advice);
518 revert_creds(old_cred);
525 static long ovl_real_ioctl(struct file *file, unsigned int cmd,
529 const struct cred *old_cred;
532 ret = ovl_real_fdget(file, &real);
536 old_cred = ovl_override_creds(file_inode(file)->i_sb);
537 ret = security_file_ioctl(real.file, cmd, arg);
539 ret = vfs_ioctl(real.file, cmd, arg);
540 revert_creds(old_cred);
547 static long ovl_ioctl_set_flags(struct file *file, unsigned int cmd,
548 unsigned long arg, unsigned int iflags)
551 struct inode *inode = file_inode(file);
552 unsigned int old_iflags;
554 if (!inode_owner_or_capable(inode))
557 ret = mnt_want_write_file(file);
563 /* Check the capability before cred override */
565 old_iflags = READ_ONCE(inode->i_flags);
566 if (((iflags ^ old_iflags) & (S_APPEND | S_IMMUTABLE)) &&
567 !capable(CAP_LINUX_IMMUTABLE))
570 ret = ovl_maybe_copy_up(file_dentry(file), O_WRONLY);
574 ret = ovl_real_ioctl(file, cmd, arg);
576 ovl_copyflags(ovl_inode_real(inode), inode);
580 mnt_drop_write_file(file);
586 static unsigned int ovl_fsflags_to_iflags(unsigned int flags)
588 unsigned int iflags = 0;
590 if (flags & FS_SYNC_FL)
592 if (flags & FS_APPEND_FL)
594 if (flags & FS_IMMUTABLE_FL)
595 iflags |= S_IMMUTABLE;
596 if (flags & FS_NOATIME_FL)
602 static long ovl_ioctl_set_fsflags(struct file *file, unsigned int cmd,
607 if (get_user(flags, (int __user *) arg))
610 return ovl_ioctl_set_flags(file, cmd, arg,
611 ovl_fsflags_to_iflags(flags));
614 static unsigned int ovl_fsxflags_to_iflags(unsigned int xflags)
616 unsigned int iflags = 0;
618 if (xflags & FS_XFLAG_SYNC)
620 if (xflags & FS_XFLAG_APPEND)
622 if (xflags & FS_XFLAG_IMMUTABLE)
623 iflags |= S_IMMUTABLE;
624 if (xflags & FS_XFLAG_NOATIME)
630 static long ovl_ioctl_set_fsxflags(struct file *file, unsigned int cmd,
635 memset(&fa, 0, sizeof(fa));
636 if (copy_from_user(&fa, (void __user *) arg, sizeof(fa)))
639 return ovl_ioctl_set_flags(file, cmd, arg,
640 ovl_fsxflags_to_iflags(fa.fsx_xflags));
643 static long ovl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
648 case FS_IOC_GETFLAGS:
649 case FS_IOC_FSGETXATTR:
650 ret = ovl_real_ioctl(file, cmd, arg);
653 case FS_IOC_SETFLAGS:
654 ret = ovl_ioctl_set_fsflags(file, cmd, arg);
657 case FS_IOC_FSSETXATTR:
658 ret = ovl_ioctl_set_fsxflags(file, cmd, arg);
668 static long ovl_compat_ioctl(struct file *file, unsigned int cmd,
672 case FS_IOC32_GETFLAGS:
673 cmd = FS_IOC_GETFLAGS;
676 case FS_IOC32_SETFLAGS:
677 cmd = FS_IOC_SETFLAGS;
684 return ovl_ioctl(file, cmd, arg);
693 static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in,
694 struct file *file_out, loff_t pos_out,
695 loff_t len, unsigned int flags, enum ovl_copyop op)
697 struct inode *inode_out = file_inode(file_out);
698 struct fd real_in, real_out;
699 const struct cred *old_cred;
702 ret = ovl_real_fdget(file_out, &real_out);
706 ret = ovl_real_fdget(file_in, &real_in);
712 old_cred = ovl_override_creds(file_inode(file_out)->i_sb);
715 ret = vfs_copy_file_range(real_in.file, pos_in,
716 real_out.file, pos_out, len, flags);
720 ret = vfs_clone_file_range(real_in.file, pos_in,
721 real_out.file, pos_out, len, flags);
725 ret = vfs_dedupe_file_range_one(real_in.file, pos_in,
726 real_out.file, pos_out, len,
730 revert_creds(old_cred);
733 ovl_copyattr(ovl_inode_real(inode_out), inode_out);
741 static ssize_t ovl_copy_file_range(struct file *file_in, loff_t pos_in,
742 struct file *file_out, loff_t pos_out,
743 size_t len, unsigned int flags)
745 return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, flags,
749 static loff_t ovl_remap_file_range(struct file *file_in, loff_t pos_in,
750 struct file *file_out, loff_t pos_out,
751 loff_t len, unsigned int remap_flags)
755 if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
758 if (remap_flags & REMAP_FILE_DEDUP)
764 * Don't copy up because of a dedupe request, this wouldn't make sense
765 * most of the time (data would be duplicated instead of deduplicated).
767 if (op == OVL_DEDUPE &&
768 (!ovl_inode_upper(file_inode(file_in)) ||
769 !ovl_inode_upper(file_inode(file_out))))
772 return ovl_copyfile(file_in, pos_in, file_out, pos_out, len,
776 const struct file_operations ovl_file_operations = {
778 .release = ovl_release,
779 .llseek = ovl_llseek,
780 .read_iter = ovl_read_iter,
781 .write_iter = ovl_write_iter,
784 .fallocate = ovl_fallocate,
785 .fadvise = ovl_fadvise,
786 .unlocked_ioctl = ovl_ioctl,
787 .compat_ioctl = ovl_compat_ioctl,
788 .splice_read = ovl_splice_read,
789 .splice_write = ovl_splice_write,
791 .copy_file_range = ovl_copy_file_range,
792 .remap_file_range = ovl_remap_file_range,
795 int __init ovl_aio_request_cache_init(void)
797 ovl_aio_request_cachep = kmem_cache_create("ovl_aio_req",
798 sizeof(struct ovl_aio_req),
799 0, SLAB_HWCACHE_ALIGN, NULL);
800 if (!ovl_aio_request_cachep)
806 void ovl_aio_request_cache_destroy(void)
808 kmem_cache_destroy(ovl_aio_request_cachep);