Merge tag 'gpio-fixes-for-v6.6-rc3' of git://git.kernel.org/pub/scm/linux/kernel...
[platform/kernel/linux-starfive.git] / fs / overlayfs / file.c
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/splice.h>
13 #include <linux/security.h>
14 #include <linux/mm.h>
15 #include <linux/fs.h>
16 #include "overlayfs.h"
17
18 struct ovl_aio_req {
19         struct kiocb iocb;
20         refcount_t ref;
21         struct kiocb *orig_iocb;
22 };
23
24 static struct kmem_cache *ovl_aio_request_cachep;
25
26 static char ovl_whatisit(struct inode *inode, struct inode *realinode)
27 {
28         if (realinode != ovl_inode_upper(inode))
29                 return 'l';
30         if (ovl_has_upperdata(inode))
31                 return 'u';
32         else
33                 return 'm';
34 }
35
36 /* No atime modification on underlying */
37 #define OVL_OPEN_FLAGS (O_NOATIME)
38
39 static struct file *ovl_open_realfile(const struct file *file,
40                                       const struct path *realpath)
41 {
42         struct inode *realinode = d_inode(realpath->dentry);
43         struct inode *inode = file_inode(file);
44         struct mnt_idmap *real_idmap;
45         struct file *realfile;
46         const struct cred *old_cred;
47         int flags = file->f_flags | OVL_OPEN_FLAGS;
48         int acc_mode = ACC_MODE(flags);
49         int err;
50
51         if (flags & O_APPEND)
52                 acc_mode |= MAY_APPEND;
53
54         old_cred = ovl_override_creds(inode->i_sb);
55         real_idmap = mnt_idmap(realpath->mnt);
56         err = inode_permission(real_idmap, realinode, MAY_OPEN | acc_mode);
57         if (err) {
58                 realfile = ERR_PTR(err);
59         } else {
60                 if (!inode_owner_or_capable(real_idmap, realinode))
61                         flags &= ~O_NOATIME;
62
63                 realfile = backing_file_open(&file->f_path, flags, realpath,
64                                              current_cred());
65         }
66         revert_creds(old_cred);
67
68         pr_debug("open(%p[%pD2/%c], 0%o) -> (%p, 0%o)\n",
69                  file, file, ovl_whatisit(inode, realinode), file->f_flags,
70                  realfile, IS_ERR(realfile) ? 0 : realfile->f_flags);
71
72         return realfile;
73 }
74
75 #define OVL_SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT)
76
77 static int ovl_change_flags(struct file *file, unsigned int flags)
78 {
79         struct inode *inode = file_inode(file);
80         int err;
81
82         flags &= OVL_SETFL_MASK;
83
84         if (((flags ^ file->f_flags) & O_APPEND) && IS_APPEND(inode))
85                 return -EPERM;
86
87         if ((flags & O_DIRECT) && !(file->f_mode & FMODE_CAN_ODIRECT))
88                 return -EINVAL;
89
90         if (file->f_op->check_flags) {
91                 err = file->f_op->check_flags(flags);
92                 if (err)
93                         return err;
94         }
95
96         spin_lock(&file->f_lock);
97         file->f_flags = (file->f_flags & ~OVL_SETFL_MASK) | flags;
98         file->f_iocb_flags = iocb_flags(file);
99         spin_unlock(&file->f_lock);
100
101         return 0;
102 }
103
104 static int ovl_real_fdget_meta(const struct file *file, struct fd *real,
105                                bool allow_meta)
106 {
107         struct dentry *dentry = file_dentry(file);
108         struct path realpath;
109         int err;
110
111         real->flags = 0;
112         real->file = file->private_data;
113
114         if (allow_meta) {
115                 ovl_path_real(dentry, &realpath);
116         } else {
117                 /* lazy lookup and verify of lowerdata */
118                 err = ovl_verify_lowerdata(dentry);
119                 if (err)
120                         return err;
121
122                 ovl_path_realdata(dentry, &realpath);
123         }
124         if (!realpath.dentry)
125                 return -EIO;
126
127         /* Has it been copied up since we'd opened it? */
128         if (unlikely(file_inode(real->file) != d_inode(realpath.dentry))) {
129                 real->flags = FDPUT_FPUT;
130                 real->file = ovl_open_realfile(file, &realpath);
131
132                 return PTR_ERR_OR_ZERO(real->file);
133         }
134
135         /* Did the flags change since open? */
136         if (unlikely((file->f_flags ^ real->file->f_flags) & ~OVL_OPEN_FLAGS))
137                 return ovl_change_flags(real->file, file->f_flags);
138
139         return 0;
140 }
141
142 static int ovl_real_fdget(const struct file *file, struct fd *real)
143 {
144         if (d_is_dir(file_dentry(file))) {
145                 real->flags = 0;
146                 real->file = ovl_dir_real_file(file, false);
147
148                 return PTR_ERR_OR_ZERO(real->file);
149         }
150
151         return ovl_real_fdget_meta(file, real, false);
152 }
153
154 static int ovl_open(struct inode *inode, struct file *file)
155 {
156         struct dentry *dentry = file_dentry(file);
157         struct file *realfile;
158         struct path realpath;
159         int err;
160
161         /* lazy lookup and verify lowerdata */
162         err = ovl_verify_lowerdata(dentry);
163         if (err)
164                 return err;
165
166         err = ovl_maybe_copy_up(dentry, file->f_flags);
167         if (err)
168                 return err;
169
170         /* No longer need these flags, so don't pass them on to underlying fs */
171         file->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
172
173         ovl_path_realdata(dentry, &realpath);
174         if (!realpath.dentry)
175                 return -EIO;
176
177         realfile = ovl_open_realfile(file, &realpath);
178         if (IS_ERR(realfile))
179                 return PTR_ERR(realfile);
180
181         file->private_data = realfile;
182
183         return 0;
184 }
185
186 static int ovl_release(struct inode *inode, struct file *file)
187 {
188         fput(file->private_data);
189
190         return 0;
191 }
192
193 static loff_t ovl_llseek(struct file *file, loff_t offset, int whence)
194 {
195         struct inode *inode = file_inode(file);
196         struct fd real;
197         const struct cred *old_cred;
198         loff_t ret;
199
200         /*
201          * The two special cases below do not need to involve real fs,
202          * so we can optimizing concurrent callers.
203          */
204         if (offset == 0) {
205                 if (whence == SEEK_CUR)
206                         return file->f_pos;
207
208                 if (whence == SEEK_SET)
209                         return vfs_setpos(file, 0, 0);
210         }
211
212         ret = ovl_real_fdget(file, &real);
213         if (ret)
214                 return ret;
215
216         /*
217          * Overlay file f_pos is the master copy that is preserved
218          * through copy up and modified on read/write, but only real
219          * fs knows how to SEEK_HOLE/SEEK_DATA and real fs may impose
220          * limitations that are more strict than ->s_maxbytes for specific
221          * files, so we use the real file to perform seeks.
222          */
223         ovl_inode_lock(inode);
224         real.file->f_pos = file->f_pos;
225
226         old_cred = ovl_override_creds(inode->i_sb);
227         ret = vfs_llseek(real.file, offset, whence);
228         revert_creds(old_cred);
229
230         file->f_pos = real.file->f_pos;
231         ovl_inode_unlock(inode);
232
233         fdput(real);
234
235         return ret;
236 }
237
238 static void ovl_file_accessed(struct file *file)
239 {
240         struct inode *inode, *upperinode;
241         struct timespec64 ctime, uctime;
242
243         if (file->f_flags & O_NOATIME)
244                 return;
245
246         inode = file_inode(file);
247         upperinode = ovl_inode_upper(inode);
248
249         if (!upperinode)
250                 return;
251
252         ctime = inode_get_ctime(inode);
253         uctime = inode_get_ctime(upperinode);
254         if ((!timespec64_equal(&inode->i_mtime, &upperinode->i_mtime) ||
255              !timespec64_equal(&ctime, &uctime))) {
256                 inode->i_mtime = upperinode->i_mtime;
257                 inode_set_ctime_to_ts(inode, uctime);
258         }
259
260         touch_atime(&file->f_path);
261 }
262
263 static rwf_t ovl_iocb_to_rwf(int ifl)
264 {
265         rwf_t flags = 0;
266
267         if (ifl & IOCB_NOWAIT)
268                 flags |= RWF_NOWAIT;
269         if (ifl & IOCB_HIPRI)
270                 flags |= RWF_HIPRI;
271         if (ifl & IOCB_DSYNC)
272                 flags |= RWF_DSYNC;
273         if (ifl & IOCB_SYNC)
274                 flags |= RWF_SYNC;
275
276         return flags;
277 }
278
279 static inline void ovl_aio_put(struct ovl_aio_req *aio_req)
280 {
281         if (refcount_dec_and_test(&aio_req->ref)) {
282                 fput(aio_req->iocb.ki_filp);
283                 kmem_cache_free(ovl_aio_request_cachep, aio_req);
284         }
285 }
286
287 static void ovl_aio_cleanup_handler(struct ovl_aio_req *aio_req)
288 {
289         struct kiocb *iocb = &aio_req->iocb;
290         struct kiocb *orig_iocb = aio_req->orig_iocb;
291
292         if (iocb->ki_flags & IOCB_WRITE) {
293                 struct inode *inode = file_inode(orig_iocb->ki_filp);
294
295                 kiocb_end_write(iocb);
296                 ovl_copyattr(inode);
297         }
298
299         orig_iocb->ki_pos = iocb->ki_pos;
300         ovl_aio_put(aio_req);
301 }
302
303 static void ovl_aio_rw_complete(struct kiocb *iocb, long res)
304 {
305         struct ovl_aio_req *aio_req = container_of(iocb,
306                                                    struct ovl_aio_req, iocb);
307         struct kiocb *orig_iocb = aio_req->orig_iocb;
308
309         ovl_aio_cleanup_handler(aio_req);
310         orig_iocb->ki_complete(orig_iocb, res);
311 }
312
313 static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter)
314 {
315         struct file *file = iocb->ki_filp;
316         struct fd real;
317         const struct cred *old_cred;
318         ssize_t ret;
319
320         if (!iov_iter_count(iter))
321                 return 0;
322
323         ret = ovl_real_fdget(file, &real);
324         if (ret)
325                 return ret;
326
327         ret = -EINVAL;
328         if (iocb->ki_flags & IOCB_DIRECT &&
329             !(real.file->f_mode & FMODE_CAN_ODIRECT))
330                 goto out_fdput;
331
332         old_cred = ovl_override_creds(file_inode(file)->i_sb);
333         if (is_sync_kiocb(iocb)) {
334                 ret = vfs_iter_read(real.file, iter, &iocb->ki_pos,
335                                     ovl_iocb_to_rwf(iocb->ki_flags));
336         } else {
337                 struct ovl_aio_req *aio_req;
338
339                 ret = -ENOMEM;
340                 aio_req = kmem_cache_zalloc(ovl_aio_request_cachep, GFP_KERNEL);
341                 if (!aio_req)
342                         goto out;
343
344                 real.flags = 0;
345                 aio_req->orig_iocb = iocb;
346                 kiocb_clone(&aio_req->iocb, iocb, get_file(real.file));
347                 aio_req->iocb.ki_complete = ovl_aio_rw_complete;
348                 refcount_set(&aio_req->ref, 2);
349                 ret = vfs_iocb_iter_read(real.file, &aio_req->iocb, iter);
350                 ovl_aio_put(aio_req);
351                 if (ret != -EIOCBQUEUED)
352                         ovl_aio_cleanup_handler(aio_req);
353         }
354 out:
355         revert_creds(old_cred);
356         ovl_file_accessed(file);
357 out_fdput:
358         fdput(real);
359
360         return ret;
361 }
362
363 static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter)
364 {
365         struct file *file = iocb->ki_filp;
366         struct inode *inode = file_inode(file);
367         struct fd real;
368         const struct cred *old_cred;
369         ssize_t ret;
370         int ifl = iocb->ki_flags;
371
372         if (!iov_iter_count(iter))
373                 return 0;
374
375         inode_lock(inode);
376         /* Update mode */
377         ovl_copyattr(inode);
378         ret = file_remove_privs(file);
379         if (ret)
380                 goto out_unlock;
381
382         ret = ovl_real_fdget(file, &real);
383         if (ret)
384                 goto out_unlock;
385
386         ret = -EINVAL;
387         if (iocb->ki_flags & IOCB_DIRECT &&
388             !(real.file->f_mode & FMODE_CAN_ODIRECT))
389                 goto out_fdput;
390
391         if (!ovl_should_sync(OVL_FS(inode->i_sb)))
392                 ifl &= ~(IOCB_DSYNC | IOCB_SYNC);
393
394         old_cred = ovl_override_creds(file_inode(file)->i_sb);
395         if (is_sync_kiocb(iocb)) {
396                 file_start_write(real.file);
397                 ret = vfs_iter_write(real.file, iter, &iocb->ki_pos,
398                                      ovl_iocb_to_rwf(ifl));
399                 file_end_write(real.file);
400                 /* Update size */
401                 ovl_copyattr(inode);
402         } else {
403                 struct ovl_aio_req *aio_req;
404
405                 ret = -ENOMEM;
406                 aio_req = kmem_cache_zalloc(ovl_aio_request_cachep, GFP_KERNEL);
407                 if (!aio_req)
408                         goto out;
409
410                 real.flags = 0;
411                 aio_req->orig_iocb = iocb;
412                 kiocb_clone(&aio_req->iocb, iocb, get_file(real.file));
413                 aio_req->iocb.ki_flags = ifl;
414                 aio_req->iocb.ki_complete = ovl_aio_rw_complete;
415                 refcount_set(&aio_req->ref, 2);
416                 kiocb_start_write(&aio_req->iocb);
417                 ret = vfs_iocb_iter_write(real.file, &aio_req->iocb, iter);
418                 ovl_aio_put(aio_req);
419                 if (ret != -EIOCBQUEUED)
420                         ovl_aio_cleanup_handler(aio_req);
421         }
422 out:
423         revert_creds(old_cred);
424 out_fdput:
425         fdput(real);
426
427 out_unlock:
428         inode_unlock(inode);
429
430         return ret;
431 }
432
433 static ssize_t ovl_splice_read(struct file *in, loff_t *ppos,
434                                struct pipe_inode_info *pipe, size_t len,
435                                unsigned int flags)
436 {
437         const struct cred *old_cred;
438         struct fd real;
439         ssize_t ret;
440
441         ret = ovl_real_fdget(in, &real);
442         if (ret)
443                 return ret;
444
445         old_cred = ovl_override_creds(file_inode(in)->i_sb);
446         ret = vfs_splice_read(real.file, ppos, pipe, len, flags);
447         revert_creds(old_cred);
448         ovl_file_accessed(in);
449
450         fdput(real);
451         return ret;
452 }
453
454 /*
455  * Calling iter_file_splice_write() directly from overlay's f_op may deadlock
456  * due to lock order inversion between pipe->mutex in iter_file_splice_write()
457  * and file_start_write(real.file) in ovl_write_iter().
458  *
459  * So do everything ovl_write_iter() does and call iter_file_splice_write() on
460  * the real file.
461  */
462 static ssize_t ovl_splice_write(struct pipe_inode_info *pipe, struct file *out,
463                                 loff_t *ppos, size_t len, unsigned int flags)
464 {
465         struct fd real;
466         const struct cred *old_cred;
467         struct inode *inode = file_inode(out);
468         ssize_t ret;
469
470         inode_lock(inode);
471         /* Update mode */
472         ovl_copyattr(inode);
473         ret = file_remove_privs(out);
474         if (ret)
475                 goto out_unlock;
476
477         ret = ovl_real_fdget(out, &real);
478         if (ret)
479                 goto out_unlock;
480
481         old_cred = ovl_override_creds(inode->i_sb);
482         file_start_write(real.file);
483
484         ret = iter_file_splice_write(pipe, real.file, ppos, len, flags);
485
486         file_end_write(real.file);
487         /* Update size */
488         ovl_copyattr(inode);
489         revert_creds(old_cred);
490         fdput(real);
491
492 out_unlock:
493         inode_unlock(inode);
494
495         return ret;
496 }
497
498 static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync)
499 {
500         struct fd real;
501         const struct cred *old_cred;
502         int ret;
503
504         ret = ovl_sync_status(OVL_FS(file_inode(file)->i_sb));
505         if (ret <= 0)
506                 return ret;
507
508         ret = ovl_real_fdget_meta(file, &real, !datasync);
509         if (ret)
510                 return ret;
511
512         /* Don't sync lower file for fear of receiving EROFS error */
513         if (file_inode(real.file) == ovl_inode_upper(file_inode(file))) {
514                 old_cred = ovl_override_creds(file_inode(file)->i_sb);
515                 ret = vfs_fsync_range(real.file, start, end, datasync);
516                 revert_creds(old_cred);
517         }
518
519         fdput(real);
520
521         return ret;
522 }
523
524 static int ovl_mmap(struct file *file, struct vm_area_struct *vma)
525 {
526         struct file *realfile = file->private_data;
527         const struct cred *old_cred;
528         int ret;
529
530         if (!realfile->f_op->mmap)
531                 return -ENODEV;
532
533         if (WARN_ON(file != vma->vm_file))
534                 return -EIO;
535
536         vma_set_file(vma, realfile);
537
538         old_cred = ovl_override_creds(file_inode(file)->i_sb);
539         ret = call_mmap(vma->vm_file, vma);
540         revert_creds(old_cred);
541         ovl_file_accessed(file);
542
543         return ret;
544 }
545
546 static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
547 {
548         struct inode *inode = file_inode(file);
549         struct fd real;
550         const struct cred *old_cred;
551         int ret;
552
553         inode_lock(inode);
554         /* Update mode */
555         ovl_copyattr(inode);
556         ret = file_remove_privs(file);
557         if (ret)
558                 goto out_unlock;
559
560         ret = ovl_real_fdget(file, &real);
561         if (ret)
562                 goto out_unlock;
563
564         old_cred = ovl_override_creds(file_inode(file)->i_sb);
565         ret = vfs_fallocate(real.file, mode, offset, len);
566         revert_creds(old_cred);
567
568         /* Update size */
569         ovl_copyattr(inode);
570
571         fdput(real);
572
573 out_unlock:
574         inode_unlock(inode);
575
576         return ret;
577 }
578
579 static int ovl_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
580 {
581         struct fd real;
582         const struct cred *old_cred;
583         int ret;
584
585         ret = ovl_real_fdget(file, &real);
586         if (ret)
587                 return ret;
588
589         old_cred = ovl_override_creds(file_inode(file)->i_sb);
590         ret = vfs_fadvise(real.file, offset, len, advice);
591         revert_creds(old_cred);
592
593         fdput(real);
594
595         return ret;
596 }
597
598 enum ovl_copyop {
599         OVL_COPY,
600         OVL_CLONE,
601         OVL_DEDUPE,
602 };
603
604 static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in,
605                             struct file *file_out, loff_t pos_out,
606                             loff_t len, unsigned int flags, enum ovl_copyop op)
607 {
608         struct inode *inode_out = file_inode(file_out);
609         struct fd real_in, real_out;
610         const struct cred *old_cred;
611         loff_t ret;
612
613         inode_lock(inode_out);
614         if (op != OVL_DEDUPE) {
615                 /* Update mode */
616                 ovl_copyattr(inode_out);
617                 ret = file_remove_privs(file_out);
618                 if (ret)
619                         goto out_unlock;
620         }
621
622         ret = ovl_real_fdget(file_out, &real_out);
623         if (ret)
624                 goto out_unlock;
625
626         ret = ovl_real_fdget(file_in, &real_in);
627         if (ret) {
628                 fdput(real_out);
629                 goto out_unlock;
630         }
631
632         old_cred = ovl_override_creds(file_inode(file_out)->i_sb);
633         switch (op) {
634         case OVL_COPY:
635                 ret = vfs_copy_file_range(real_in.file, pos_in,
636                                           real_out.file, pos_out, len, flags);
637                 break;
638
639         case OVL_CLONE:
640                 ret = vfs_clone_file_range(real_in.file, pos_in,
641                                            real_out.file, pos_out, len, flags);
642                 break;
643
644         case OVL_DEDUPE:
645                 ret = vfs_dedupe_file_range_one(real_in.file, pos_in,
646                                                 real_out.file, pos_out, len,
647                                                 flags);
648                 break;
649         }
650         revert_creds(old_cred);
651
652         /* Update size */
653         ovl_copyattr(inode_out);
654
655         fdput(real_in);
656         fdput(real_out);
657
658 out_unlock:
659         inode_unlock(inode_out);
660
661         return ret;
662 }
663
664 static ssize_t ovl_copy_file_range(struct file *file_in, loff_t pos_in,
665                                    struct file *file_out, loff_t pos_out,
666                                    size_t len, unsigned int flags)
667 {
668         return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, flags,
669                             OVL_COPY);
670 }
671
672 static loff_t ovl_remap_file_range(struct file *file_in, loff_t pos_in,
673                                    struct file *file_out, loff_t pos_out,
674                                    loff_t len, unsigned int remap_flags)
675 {
676         enum ovl_copyop op;
677
678         if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
679                 return -EINVAL;
680
681         if (remap_flags & REMAP_FILE_DEDUP)
682                 op = OVL_DEDUPE;
683         else
684                 op = OVL_CLONE;
685
686         /*
687          * Don't copy up because of a dedupe request, this wouldn't make sense
688          * most of the time (data would be duplicated instead of deduplicated).
689          */
690         if (op == OVL_DEDUPE &&
691             (!ovl_inode_upper(file_inode(file_in)) ||
692              !ovl_inode_upper(file_inode(file_out))))
693                 return -EPERM;
694
695         return ovl_copyfile(file_in, pos_in, file_out, pos_out, len,
696                             remap_flags, op);
697 }
698
699 static int ovl_flush(struct file *file, fl_owner_t id)
700 {
701         struct fd real;
702         const struct cred *old_cred;
703         int err;
704
705         err = ovl_real_fdget(file, &real);
706         if (err)
707                 return err;
708
709         if (real.file->f_op->flush) {
710                 old_cred = ovl_override_creds(file_inode(file)->i_sb);
711                 err = real.file->f_op->flush(real.file, id);
712                 revert_creds(old_cred);
713         }
714         fdput(real);
715
716         return err;
717 }
718
719 const struct file_operations ovl_file_operations = {
720         .open           = ovl_open,
721         .release        = ovl_release,
722         .llseek         = ovl_llseek,
723         .read_iter      = ovl_read_iter,
724         .write_iter     = ovl_write_iter,
725         .fsync          = ovl_fsync,
726         .mmap           = ovl_mmap,
727         .fallocate      = ovl_fallocate,
728         .fadvise        = ovl_fadvise,
729         .flush          = ovl_flush,
730         .splice_read    = ovl_splice_read,
731         .splice_write   = ovl_splice_write,
732
733         .copy_file_range        = ovl_copy_file_range,
734         .remap_file_range       = ovl_remap_file_range,
735 };
736
737 int __init ovl_aio_request_cache_init(void)
738 {
739         ovl_aio_request_cachep = kmem_cache_create("ovl_aio_req",
740                                                    sizeof(struct ovl_aio_req),
741                                                    0, SLAB_HWCACHE_ALIGN, NULL);
742         if (!ovl_aio_request_cachep)
743                 return -ENOMEM;
744
745         return 0;
746 }
747
748 void ovl_aio_request_cache_destroy(void)
749 {
750         kmem_cache_destroy(ovl_aio_request_cachep);
751 }