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