]> git.ipfire.org Git - thirdparty/kernel/linux.git/blob - fs/overlayfs/file.c
Merge tag 'landlock-6.7-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/mic...
[thirdparty/kernel/linux.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 struct timespec64 mtime, umtime;
243
244 if (file->f_flags & O_NOATIME)
245 return;
246
247 inode = file_inode(file);
248 upperinode = ovl_inode_upper(inode);
249
250 if (!upperinode)
251 return;
252
253 ctime = inode_get_ctime(inode);
254 uctime = inode_get_ctime(upperinode);
255 mtime = inode_get_mtime(inode);
256 umtime = inode_get_mtime(upperinode);
257 if ((!timespec64_equal(&mtime, &umtime)) ||
258 !timespec64_equal(&ctime, &uctime)) {
259 inode_set_mtime_to_ts(inode, inode_get_mtime(upperinode));
260 inode_set_ctime_to_ts(inode, uctime);
261 }
262
263 touch_atime(&file->f_path);
264 }
265
266 static rwf_t ovl_iocb_to_rwf(int ifl)
267 {
268 rwf_t flags = 0;
269
270 if (ifl & IOCB_NOWAIT)
271 flags |= RWF_NOWAIT;
272 if (ifl & IOCB_HIPRI)
273 flags |= RWF_HIPRI;
274 if (ifl & IOCB_DSYNC)
275 flags |= RWF_DSYNC;
276 if (ifl & IOCB_SYNC)
277 flags |= RWF_SYNC;
278
279 return flags;
280 }
281
282 static inline void ovl_aio_put(struct ovl_aio_req *aio_req)
283 {
284 if (refcount_dec_and_test(&aio_req->ref)) {
285 fput(aio_req->iocb.ki_filp);
286 kmem_cache_free(ovl_aio_request_cachep, aio_req);
287 }
288 }
289
290 static void ovl_aio_cleanup_handler(struct ovl_aio_req *aio_req)
291 {
292 struct kiocb *iocb = &aio_req->iocb;
293 struct kiocb *orig_iocb = aio_req->orig_iocb;
294
295 if (iocb->ki_flags & IOCB_WRITE) {
296 struct inode *inode = file_inode(orig_iocb->ki_filp);
297
298 kiocb_end_write(iocb);
299 ovl_copyattr(inode);
300 }
301
302 orig_iocb->ki_pos = iocb->ki_pos;
303 ovl_aio_put(aio_req);
304 }
305
306 static void ovl_aio_rw_complete(struct kiocb *iocb, long res)
307 {
308 struct ovl_aio_req *aio_req = container_of(iocb,
309 struct ovl_aio_req, iocb);
310 struct kiocb *orig_iocb = aio_req->orig_iocb;
311
312 ovl_aio_cleanup_handler(aio_req);
313 orig_iocb->ki_complete(orig_iocb, res);
314 }
315
316 static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter)
317 {
318 struct file *file = iocb->ki_filp;
319 struct fd real;
320 const struct cred *old_cred;
321 ssize_t ret;
322
323 if (!iov_iter_count(iter))
324 return 0;
325
326 ret = ovl_real_fdget(file, &real);
327 if (ret)
328 return ret;
329
330 ret = -EINVAL;
331 if (iocb->ki_flags & IOCB_DIRECT &&
332 !(real.file->f_mode & FMODE_CAN_ODIRECT))
333 goto out_fdput;
334
335 old_cred = ovl_override_creds(file_inode(file)->i_sb);
336 if (is_sync_kiocb(iocb)) {
337 ret = vfs_iter_read(real.file, iter, &iocb->ki_pos,
338 ovl_iocb_to_rwf(iocb->ki_flags));
339 } else {
340 struct ovl_aio_req *aio_req;
341
342 ret = -ENOMEM;
343 aio_req = kmem_cache_zalloc(ovl_aio_request_cachep, GFP_KERNEL);
344 if (!aio_req)
345 goto out;
346
347 aio_req->orig_iocb = iocb;
348 kiocb_clone(&aio_req->iocb, iocb, get_file(real.file));
349 aio_req->iocb.ki_complete = ovl_aio_rw_complete;
350 refcount_set(&aio_req->ref, 2);
351 ret = vfs_iocb_iter_read(real.file, &aio_req->iocb, iter);
352 ovl_aio_put(aio_req);
353 if (ret != -EIOCBQUEUED)
354 ovl_aio_cleanup_handler(aio_req);
355 }
356 out:
357 revert_creds(old_cred);
358 ovl_file_accessed(file);
359 out_fdput:
360 fdput(real);
361
362 return ret;
363 }
364
365 static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter)
366 {
367 struct file *file = iocb->ki_filp;
368 struct inode *inode = file_inode(file);
369 struct fd real;
370 const struct cred *old_cred;
371 ssize_t ret;
372 int ifl = iocb->ki_flags;
373
374 if (!iov_iter_count(iter))
375 return 0;
376
377 inode_lock(inode);
378 /* Update mode */
379 ovl_copyattr(inode);
380 ret = file_remove_privs(file);
381 if (ret)
382 goto out_unlock;
383
384 ret = ovl_real_fdget(file, &real);
385 if (ret)
386 goto out_unlock;
387
388 ret = -EINVAL;
389 if (iocb->ki_flags & IOCB_DIRECT &&
390 !(real.file->f_mode & FMODE_CAN_ODIRECT))
391 goto out_fdput;
392
393 if (!ovl_should_sync(OVL_FS(inode->i_sb)))
394 ifl &= ~(IOCB_DSYNC | IOCB_SYNC);
395
396 /*
397 * Overlayfs doesn't support deferred completions, don't copy
398 * this property in case it is set by the issuer.
399 */
400 ifl &= ~IOCB_DIO_CALLER_COMP;
401
402 old_cred = ovl_override_creds(file_inode(file)->i_sb);
403 if (is_sync_kiocb(iocb)) {
404 file_start_write(real.file);
405 ret = vfs_iter_write(real.file, iter, &iocb->ki_pos,
406 ovl_iocb_to_rwf(ifl));
407 file_end_write(real.file);
408 /* Update size */
409 ovl_copyattr(inode);
410 } else {
411 struct ovl_aio_req *aio_req;
412
413 ret = -ENOMEM;
414 aio_req = kmem_cache_zalloc(ovl_aio_request_cachep, GFP_KERNEL);
415 if (!aio_req)
416 goto out;
417
418 aio_req->orig_iocb = iocb;
419 kiocb_clone(&aio_req->iocb, iocb, get_file(real.file));
420 aio_req->iocb.ki_flags = ifl;
421 aio_req->iocb.ki_complete = ovl_aio_rw_complete;
422 refcount_set(&aio_req->ref, 2);
423 kiocb_start_write(&aio_req->iocb);
424 ret = vfs_iocb_iter_write(real.file, &aio_req->iocb, iter);
425 ovl_aio_put(aio_req);
426 if (ret != -EIOCBQUEUED)
427 ovl_aio_cleanup_handler(aio_req);
428 }
429 out:
430 revert_creds(old_cred);
431 out_fdput:
432 fdput(real);
433
434 out_unlock:
435 inode_unlock(inode);
436
437 return ret;
438 }
439
440 static ssize_t ovl_splice_read(struct file *in, loff_t *ppos,
441 struct pipe_inode_info *pipe, size_t len,
442 unsigned int flags)
443 {
444 const struct cred *old_cred;
445 struct fd real;
446 ssize_t ret;
447
448 ret = ovl_real_fdget(in, &real);
449 if (ret)
450 return ret;
451
452 old_cred = ovl_override_creds(file_inode(in)->i_sb);
453 ret = vfs_splice_read(real.file, ppos, pipe, len, flags);
454 revert_creds(old_cred);
455 ovl_file_accessed(in);
456
457 fdput(real);
458 return ret;
459 }
460
461 /*
462 * Calling iter_file_splice_write() directly from overlay's f_op may deadlock
463 * due to lock order inversion between pipe->mutex in iter_file_splice_write()
464 * and file_start_write(real.file) in ovl_write_iter().
465 *
466 * So do everything ovl_write_iter() does and call iter_file_splice_write() on
467 * the real file.
468 */
469 static ssize_t ovl_splice_write(struct pipe_inode_info *pipe, struct file *out,
470 loff_t *ppos, size_t len, unsigned int flags)
471 {
472 struct fd real;
473 const struct cred *old_cred;
474 struct inode *inode = file_inode(out);
475 ssize_t ret;
476
477 inode_lock(inode);
478 /* Update mode */
479 ovl_copyattr(inode);
480 ret = file_remove_privs(out);
481 if (ret)
482 goto out_unlock;
483
484 ret = ovl_real_fdget(out, &real);
485 if (ret)
486 goto out_unlock;
487
488 old_cred = ovl_override_creds(inode->i_sb);
489 file_start_write(real.file);
490
491 ret = iter_file_splice_write(pipe, real.file, ppos, len, flags);
492
493 file_end_write(real.file);
494 /* Update size */
495 ovl_copyattr(inode);
496 revert_creds(old_cred);
497 fdput(real);
498
499 out_unlock:
500 inode_unlock(inode);
501
502 return ret;
503 }
504
505 static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync)
506 {
507 struct fd real;
508 const struct cred *old_cred;
509 int ret;
510
511 ret = ovl_sync_status(OVL_FS(file_inode(file)->i_sb));
512 if (ret <= 0)
513 return ret;
514
515 ret = ovl_real_fdget_meta(file, &real, !datasync);
516 if (ret)
517 return ret;
518
519 /* Don't sync lower file for fear of receiving EROFS error */
520 if (file_inode(real.file) == ovl_inode_upper(file_inode(file))) {
521 old_cred = ovl_override_creds(file_inode(file)->i_sb);
522 ret = vfs_fsync_range(real.file, start, end, datasync);
523 revert_creds(old_cred);
524 }
525
526 fdput(real);
527
528 return ret;
529 }
530
531 static int ovl_mmap(struct file *file, struct vm_area_struct *vma)
532 {
533 struct file *realfile = file->private_data;
534 const struct cred *old_cred;
535 int ret;
536
537 if (!realfile->f_op->mmap)
538 return -ENODEV;
539
540 if (WARN_ON(file != vma->vm_file))
541 return -EIO;
542
543 vma_set_file(vma, realfile);
544
545 old_cred = ovl_override_creds(file_inode(file)->i_sb);
546 ret = call_mmap(vma->vm_file, vma);
547 revert_creds(old_cred);
548 ovl_file_accessed(file);
549
550 return ret;
551 }
552
553 static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
554 {
555 struct inode *inode = file_inode(file);
556 struct fd real;
557 const struct cred *old_cred;
558 int ret;
559
560 inode_lock(inode);
561 /* Update mode */
562 ovl_copyattr(inode);
563 ret = file_remove_privs(file);
564 if (ret)
565 goto out_unlock;
566
567 ret = ovl_real_fdget(file, &real);
568 if (ret)
569 goto out_unlock;
570
571 old_cred = ovl_override_creds(file_inode(file)->i_sb);
572 ret = vfs_fallocate(real.file, mode, offset, len);
573 revert_creds(old_cred);
574
575 /* Update size */
576 ovl_copyattr(inode);
577
578 fdput(real);
579
580 out_unlock:
581 inode_unlock(inode);
582
583 return ret;
584 }
585
586 static int ovl_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
587 {
588 struct fd real;
589 const struct cred *old_cred;
590 int ret;
591
592 ret = ovl_real_fdget(file, &real);
593 if (ret)
594 return ret;
595
596 old_cred = ovl_override_creds(file_inode(file)->i_sb);
597 ret = vfs_fadvise(real.file, offset, len, advice);
598 revert_creds(old_cred);
599
600 fdput(real);
601
602 return ret;
603 }
604
605 enum ovl_copyop {
606 OVL_COPY,
607 OVL_CLONE,
608 OVL_DEDUPE,
609 };
610
611 static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in,
612 struct file *file_out, loff_t pos_out,
613 loff_t len, unsigned int flags, enum ovl_copyop op)
614 {
615 struct inode *inode_out = file_inode(file_out);
616 struct fd real_in, real_out;
617 const struct cred *old_cred;
618 loff_t ret;
619
620 inode_lock(inode_out);
621 if (op != OVL_DEDUPE) {
622 /* Update mode */
623 ovl_copyattr(inode_out);
624 ret = file_remove_privs(file_out);
625 if (ret)
626 goto out_unlock;
627 }
628
629 ret = ovl_real_fdget(file_out, &real_out);
630 if (ret)
631 goto out_unlock;
632
633 ret = ovl_real_fdget(file_in, &real_in);
634 if (ret) {
635 fdput(real_out);
636 goto out_unlock;
637 }
638
639 old_cred = ovl_override_creds(file_inode(file_out)->i_sb);
640 switch (op) {
641 case OVL_COPY:
642 ret = vfs_copy_file_range(real_in.file, pos_in,
643 real_out.file, pos_out, len, flags);
644 break;
645
646 case OVL_CLONE:
647 ret = vfs_clone_file_range(real_in.file, pos_in,
648 real_out.file, pos_out, len, flags);
649 break;
650
651 case OVL_DEDUPE:
652 ret = vfs_dedupe_file_range_one(real_in.file, pos_in,
653 real_out.file, pos_out, len,
654 flags);
655 break;
656 }
657 revert_creds(old_cred);
658
659 /* Update size */
660 ovl_copyattr(inode_out);
661
662 fdput(real_in);
663 fdput(real_out);
664
665 out_unlock:
666 inode_unlock(inode_out);
667
668 return ret;
669 }
670
671 static ssize_t ovl_copy_file_range(struct file *file_in, loff_t pos_in,
672 struct file *file_out, loff_t pos_out,
673 size_t len, unsigned int flags)
674 {
675 return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, flags,
676 OVL_COPY);
677 }
678
679 static loff_t ovl_remap_file_range(struct file *file_in, loff_t pos_in,
680 struct file *file_out, loff_t pos_out,
681 loff_t len, unsigned int remap_flags)
682 {
683 enum ovl_copyop op;
684
685 if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
686 return -EINVAL;
687
688 if (remap_flags & REMAP_FILE_DEDUP)
689 op = OVL_DEDUPE;
690 else
691 op = OVL_CLONE;
692
693 /*
694 * Don't copy up because of a dedupe request, this wouldn't make sense
695 * most of the time (data would be duplicated instead of deduplicated).
696 */
697 if (op == OVL_DEDUPE &&
698 (!ovl_inode_upper(file_inode(file_in)) ||
699 !ovl_inode_upper(file_inode(file_out))))
700 return -EPERM;
701
702 return ovl_copyfile(file_in, pos_in, file_out, pos_out, len,
703 remap_flags, op);
704 }
705
706 static int ovl_flush(struct file *file, fl_owner_t id)
707 {
708 struct fd real;
709 const struct cred *old_cred;
710 int err;
711
712 err = ovl_real_fdget(file, &real);
713 if (err)
714 return err;
715
716 if (real.file->f_op->flush) {
717 old_cred = ovl_override_creds(file_inode(file)->i_sb);
718 err = real.file->f_op->flush(real.file, id);
719 revert_creds(old_cred);
720 }
721 fdput(real);
722
723 return err;
724 }
725
726 const struct file_operations ovl_file_operations = {
727 .open = ovl_open,
728 .release = ovl_release,
729 .llseek = ovl_llseek,
730 .read_iter = ovl_read_iter,
731 .write_iter = ovl_write_iter,
732 .fsync = ovl_fsync,
733 .mmap = ovl_mmap,
734 .fallocate = ovl_fallocate,
735 .fadvise = ovl_fadvise,
736 .flush = ovl_flush,
737 .splice_read = ovl_splice_read,
738 .splice_write = ovl_splice_write,
739
740 .copy_file_range = ovl_copy_file_range,
741 .remap_file_range = ovl_remap_file_range,
742 };
743
744 int __init ovl_aio_request_cache_init(void)
745 {
746 ovl_aio_request_cachep = kmem_cache_create("ovl_aio_req",
747 sizeof(struct ovl_aio_req),
748 0, SLAB_HWCACHE_ALIGN, NULL);
749 if (!ovl_aio_request_cachep)
750 return -ENOMEM;
751
752 return 0;
753 }
754
755 void ovl_aio_request_cache_destroy(void)
756 {
757 kmem_cache_destroy(ovl_aio_request_cachep);
758 }