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