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