1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * 4 * Copyright (C) 2011 Novell Inc. 5 */ 6 7 #include <linux/module.h> 8 #include <linux/fs.h> 9 #include <linux/slab.h> 10 #include <linux/file.h> 11 #include <linux/fileattr.h> 12 #include <linux/splice.h> 13 #include <linux/xattr.h> 14 #include <linux/security.h> 15 #include <linux/uaccess.h> 16 #include <linux/sched/signal.h> 17 #include <linux/cred.h> 18 #include <linux/namei.h> 19 #include <linux/fdtable.h> 20 #include <linux/ratelimit.h> 21 #include <linux/exportfs.h> 22 #include "overlayfs.h" 23 24 #define OVL_COPY_UP_CHUNK_SIZE (1 << 20) 25 26 static int ovl_ccup_set(const char *buf, const struct kernel_param *param) 27 { 28 pr_warn("\"check_copy_up\" module option is obsolete\n"); 29 return 0; 30 } 31 32 static int ovl_ccup_get(char *buf, const struct kernel_param *param) 33 { 34 return sprintf(buf, "N\n"); 35 } 36 37 module_param_call(check_copy_up, ovl_ccup_set, ovl_ccup_get, NULL, 0644); 38 MODULE_PARM_DESC(check_copy_up, "Obsolete; does nothing"); 39 40 static bool ovl_must_copy_xattr(const char *name) 41 { 42 return !strcmp(name, XATTR_POSIX_ACL_ACCESS) || 43 !strcmp(name, XATTR_POSIX_ACL_DEFAULT) || 44 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN); 45 } 46 47 static int ovl_copy_acl(struct ovl_fs *ofs, const struct path *path, 48 struct dentry *dentry, const char *acl_name) 49 { 50 int err; 51 struct posix_acl *clone, *real_acl = NULL; 52 53 real_acl = ovl_get_acl_path(path, acl_name, false); 54 if (!real_acl) 55 return 0; 56 57 if (IS_ERR(real_acl)) { 58 err = PTR_ERR(real_acl); 59 if (err == -ENODATA || err == -EOPNOTSUPP) 60 return 0; 61 return err; 62 } 63 64 clone = posix_acl_clone(real_acl, GFP_KERNEL); 65 posix_acl_release(real_acl); /* release original acl */ 66 if (!clone) 67 return -ENOMEM; 68 69 err = ovl_do_set_acl(ofs, dentry, acl_name, clone); 70 71 /* release cloned acl */ 72 posix_acl_release(clone); 73 return err; 74 } 75 76 int ovl_copy_xattr(struct super_block *sb, const struct path *oldpath, struct dentry *new) 77 { 78 struct dentry *old = oldpath->dentry; 79 ssize_t list_size, size, value_size = 0; 80 char *buf, *name, *value = NULL; 81 int error = 0; 82 size_t slen; 83 84 if (!old->d_inode->i_op->listxattr || !new->d_inode->i_op->listxattr) 85 return 0; 86 87 list_size = vfs_listxattr(old, NULL, 0); 88 if (list_size <= 0) { 89 if (list_size == -EOPNOTSUPP) 90 return 0; 91 return list_size; 92 } 93 94 buf = kvzalloc(list_size, GFP_KERNEL); 95 if (!buf) 96 return -ENOMEM; 97 98 list_size = vfs_listxattr(old, buf, list_size); 99 if (list_size <= 0) { 100 error = list_size; 101 goto out; 102 } 103 104 for (name = buf; list_size; name += slen) { 105 slen = strnlen(name, list_size) + 1; 106 107 /* underlying fs providing us with an broken xattr list? */ 108 if (WARN_ON(slen > list_size)) { 109 error = -EIO; 110 break; 111 } 112 list_size -= slen; 113 114 if (ovl_is_private_xattr(sb, name)) 115 continue; 116 117 error = security_inode_copy_up_xattr(name); 118 if (error < 0 && error != -EOPNOTSUPP) 119 break; 120 if (error == 1) { 121 error = 0; 122 continue; /* Discard */ 123 } 124 125 if (is_posix_acl_xattr(name)) { 126 error = ovl_copy_acl(OVL_FS(sb), oldpath, new, name); 127 if (!error) 128 continue; 129 /* POSIX ACLs must be copied. */ 130 break; 131 } 132 133 retry: 134 size = ovl_do_getxattr(oldpath, name, value, value_size); 135 if (size == -ERANGE) 136 size = ovl_do_getxattr(oldpath, name, NULL, 0); 137 138 if (size < 0) { 139 error = size; 140 break; 141 } 142 143 if (size > value_size) { 144 void *new; 145 146 new = kvmalloc(size, GFP_KERNEL); 147 if (!new) { 148 error = -ENOMEM; 149 break; 150 } 151 kvfree(value); 152 value = new; 153 value_size = size; 154 goto retry; 155 } 156 157 error = ovl_do_setxattr(OVL_FS(sb), new, name, value, size, 0); 158 if (error) { 159 if (error != -EOPNOTSUPP || ovl_must_copy_xattr(name)) 160 break; 161 162 /* Ignore failure to copy unknown xattrs */ 163 error = 0; 164 } 165 } 166 kvfree(value); 167 out: 168 kvfree(buf); 169 return error; 170 } 171 172 static int ovl_copy_fileattr(struct inode *inode, const struct path *old, 173 const struct path *new) 174 { 175 struct fileattr oldfa = { .flags_valid = true }; 176 struct fileattr newfa = { .flags_valid = true }; 177 int err; 178 179 err = ovl_real_fileattr_get(old, &oldfa); 180 if (err) { 181 /* Ntfs-3g returns -EINVAL for "no fileattr support" */ 182 if (err == -ENOTTY || err == -EINVAL) 183 return 0; 184 pr_warn("failed to retrieve lower fileattr (%pd2, err=%i)\n", 185 old->dentry, err); 186 return err; 187 } 188 189 /* 190 * We cannot set immutable and append-only flags on upper inode, 191 * because we would not be able to link upper inode to upper dir 192 * not set overlay private xattr on upper inode. 193 * Store these flags in overlay.protattr xattr instead. 194 */ 195 if (oldfa.flags & OVL_PROT_FS_FLAGS_MASK) { 196 err = ovl_set_protattr(inode, new->dentry, &oldfa); 197 if (err == -EPERM) 198 pr_warn_once("copying fileattr: no xattr on upper\n"); 199 else if (err) 200 return err; 201 } 202 203 /* Don't bother copying flags if none are set */ 204 if (!(oldfa.flags & OVL_COPY_FS_FLAGS_MASK)) 205 return 0; 206 207 err = ovl_real_fileattr_get(new, &newfa); 208 if (err) { 209 /* 210 * Returning an error if upper doesn't support fileattr will 211 * result in a regression, so revert to the old behavior. 212 */ 213 if (err == -ENOTTY || err == -EINVAL) { 214 pr_warn_once("copying fileattr: no support on upper\n"); 215 return 0; 216 } 217 pr_warn("failed to retrieve upper fileattr (%pd2, err=%i)\n", 218 new->dentry, err); 219 return err; 220 } 221 222 BUILD_BUG_ON(OVL_COPY_FS_FLAGS_MASK & ~FS_COMMON_FL); 223 newfa.flags &= ~OVL_COPY_FS_FLAGS_MASK; 224 newfa.flags |= (oldfa.flags & OVL_COPY_FS_FLAGS_MASK); 225 226 BUILD_BUG_ON(OVL_COPY_FSX_FLAGS_MASK & ~FS_XFLAG_COMMON); 227 newfa.fsx_xflags &= ~OVL_COPY_FSX_FLAGS_MASK; 228 newfa.fsx_xflags |= (oldfa.fsx_xflags & OVL_COPY_FSX_FLAGS_MASK); 229 230 return ovl_real_fileattr_set(new, &newfa); 231 } 232 233 static int ovl_copy_up_file(struct ovl_fs *ofs, struct dentry *dentry, 234 struct file *new_file, loff_t len) 235 { 236 struct path datapath; 237 struct file *old_file; 238 loff_t old_pos = 0; 239 loff_t new_pos = 0; 240 loff_t cloned; 241 loff_t data_pos = -1; 242 loff_t hole_len; 243 bool skip_hole = false; 244 int error = 0; 245 246 ovl_path_lowerdata(dentry, &datapath); 247 if (WARN_ON(datapath.dentry == NULL)) 248 return -EIO; 249 250 old_file = ovl_path_open(&datapath, O_LARGEFILE | O_RDONLY); 251 if (IS_ERR(old_file)) 252 return PTR_ERR(old_file); 253 254 /* Try to use clone_file_range to clone up within the same fs */ 255 ovl_start_write(dentry); 256 cloned = do_clone_file_range(old_file, 0, new_file, 0, len, 0); 257 ovl_end_write(dentry); 258 if (cloned == len) 259 goto out_fput; 260 /* Couldn't clone, so now we try to copy the data */ 261 262 /* Check if lower fs supports seek operation */ 263 if (old_file->f_mode & FMODE_LSEEK) 264 skip_hole = true; 265 266 while (len) { 267 size_t this_len = OVL_COPY_UP_CHUNK_SIZE; 268 long bytes; 269 270 if (len < this_len) 271 this_len = len; 272 273 if (signal_pending_state(TASK_KILLABLE, current)) { 274 error = -EINTR; 275 break; 276 } 277 278 /* 279 * Fill zero for hole will cost unnecessary disk space 280 * and meanwhile slow down the copy-up speed, so we do 281 * an optimization for hole during copy-up, it relies 282 * on SEEK_DATA implementation in lower fs so if lower 283 * fs does not support it, copy-up will behave as before. 284 * 285 * Detail logic of hole detection as below: 286 * When we detect next data position is larger than current 287 * position we will skip that hole, otherwise we copy 288 * data in the size of OVL_COPY_UP_CHUNK_SIZE. Actually, 289 * it may not recognize all kind of holes and sometimes 290 * only skips partial of hole area. However, it will be 291 * enough for most of the use cases. 292 * 293 * We do not hold upper sb_writers throughout the loop to avert 294 * lockdep warning with llseek of lower file in nested overlay: 295 * - upper sb_writers 296 * -- lower ovl_inode_lock (ovl_llseek) 297 */ 298 if (skip_hole && data_pos < old_pos) { 299 data_pos = vfs_llseek(old_file, old_pos, SEEK_DATA); 300 if (data_pos > old_pos) { 301 hole_len = data_pos - old_pos; 302 len -= hole_len; 303 old_pos = new_pos = data_pos; 304 continue; 305 } else if (data_pos == -ENXIO) { 306 break; 307 } else if (data_pos < 0) { 308 skip_hole = false; 309 } 310 } 311 312 ovl_start_write(dentry); 313 bytes = do_splice_direct(old_file, &old_pos, 314 new_file, &new_pos, 315 this_len, SPLICE_F_MOVE); 316 ovl_end_write(dentry); 317 if (bytes <= 0) { 318 error = bytes; 319 break; 320 } 321 WARN_ON(old_pos != new_pos); 322 323 len -= bytes; 324 } 325 if (!error && ovl_should_sync(ofs)) 326 error = vfs_fsync(new_file, 0); 327 out_fput: 328 fput(old_file); 329 return error; 330 } 331 332 static int ovl_set_size(struct ovl_fs *ofs, 333 struct dentry *upperdentry, struct kstat *stat) 334 { 335 struct iattr attr = { 336 .ia_valid = ATTR_SIZE, 337 .ia_size = stat->size, 338 }; 339 340 return ovl_do_notify_change(ofs, upperdentry, &attr); 341 } 342 343 static int ovl_set_timestamps(struct ovl_fs *ofs, struct dentry *upperdentry, 344 struct kstat *stat) 345 { 346 struct iattr attr = { 347 .ia_valid = 348 ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_CTIME, 349 .ia_atime = stat->atime, 350 .ia_mtime = stat->mtime, 351 }; 352 353 return ovl_do_notify_change(ofs, upperdentry, &attr); 354 } 355 356 int ovl_set_attr(struct ovl_fs *ofs, struct dentry *upperdentry, 357 struct kstat *stat) 358 { 359 int err = 0; 360 361 if (!S_ISLNK(stat->mode)) { 362 struct iattr attr = { 363 .ia_valid = ATTR_MODE, 364 .ia_mode = stat->mode, 365 }; 366 err = ovl_do_notify_change(ofs, upperdentry, &attr); 367 } 368 if (!err) { 369 struct iattr attr = { 370 .ia_valid = ATTR_UID | ATTR_GID, 371 .ia_vfsuid = VFSUIDT_INIT(stat->uid), 372 .ia_vfsgid = VFSGIDT_INIT(stat->gid), 373 }; 374 err = ovl_do_notify_change(ofs, upperdentry, &attr); 375 } 376 if (!err) 377 ovl_set_timestamps(ofs, upperdentry, stat); 378 379 return err; 380 } 381 382 struct ovl_fh *ovl_encode_real_fh(struct ovl_fs *ofs, struct dentry *real, 383 bool is_upper) 384 { 385 struct ovl_fh *fh; 386 int fh_type, dwords; 387 int buflen = MAX_HANDLE_SZ; 388 uuid_t *uuid = &real->d_sb->s_uuid; 389 int err; 390 391 /* Make sure the real fid stays 32bit aligned */ 392 BUILD_BUG_ON(OVL_FH_FID_OFFSET % 4); 393 BUILD_BUG_ON(MAX_HANDLE_SZ + OVL_FH_FID_OFFSET > 255); 394 395 fh = kzalloc(buflen + OVL_FH_FID_OFFSET, GFP_KERNEL); 396 if (!fh) 397 return ERR_PTR(-ENOMEM); 398 399 /* 400 * We encode a non-connectable file handle for non-dir, because we 401 * only need to find the lower inode number and we don't want to pay 402 * the price or reconnecting the dentry. 403 */ 404 dwords = buflen >> 2; 405 fh_type = exportfs_encode_fh(real, (void *)fh->fb.fid, &dwords, 0); 406 buflen = (dwords << 2); 407 408 err = -EIO; 409 if (WARN_ON(fh_type < 0) || 410 WARN_ON(buflen > MAX_HANDLE_SZ) || 411 WARN_ON(fh_type == FILEID_INVALID)) 412 goto out_err; 413 414 fh->fb.version = OVL_FH_VERSION; 415 fh->fb.magic = OVL_FH_MAGIC; 416 fh->fb.type = fh_type; 417 fh->fb.flags = OVL_FH_FLAG_CPU_ENDIAN; 418 /* 419 * When we will want to decode an overlay dentry from this handle 420 * and all layers are on the same fs, if we get a disconncted real 421 * dentry when we decode fid, the only way to tell if we should assign 422 * it to upperdentry or to lowerstack is by checking this flag. 423 */ 424 if (is_upper) 425 fh->fb.flags |= OVL_FH_FLAG_PATH_UPPER; 426 fh->fb.len = sizeof(fh->fb) + buflen; 427 if (ovl_origin_uuid(ofs)) 428 fh->fb.uuid = *uuid; 429 430 return fh; 431 432 out_err: 433 kfree(fh); 434 return ERR_PTR(err); 435 } 436 437 struct ovl_fh *ovl_get_origin_fh(struct ovl_fs *ofs, struct dentry *origin) 438 { 439 /* 440 * When lower layer doesn't support export operations store a 'null' fh, 441 * so we can use the overlay.origin xattr to distignuish between a copy 442 * up and a pure upper inode. 443 */ 444 if (!ovl_can_decode_fh(origin->d_sb)) 445 return NULL; 446 447 return ovl_encode_real_fh(ofs, origin, false); 448 } 449 450 int ovl_set_origin_fh(struct ovl_fs *ofs, const struct ovl_fh *fh, 451 struct dentry *upper) 452 { 453 int err; 454 455 /* 456 * Do not fail when upper doesn't support xattrs. 457 */ 458 err = ovl_check_setxattr(ofs, upper, OVL_XATTR_ORIGIN, fh->buf, 459 fh ? fh->fb.len : 0, 0); 460 461 /* Ignore -EPERM from setting "user.*" on symlink/special */ 462 return err == -EPERM ? 0 : err; 463 } 464 465 /* Store file handle of @upper dir in @index dir entry */ 466 static int ovl_set_upper_fh(struct ovl_fs *ofs, struct dentry *upper, 467 struct dentry *index) 468 { 469 const struct ovl_fh *fh; 470 int err; 471 472 fh = ovl_encode_real_fh(ofs, upper, true); 473 if (IS_ERR(fh)) 474 return PTR_ERR(fh); 475 476 err = ovl_setxattr(ofs, index, OVL_XATTR_UPPER, fh->buf, fh->fb.len); 477 478 kfree(fh); 479 return err; 480 } 481 482 /* 483 * Create and install index entry. 484 * 485 * Caller must hold i_mutex on indexdir. 486 */ 487 static int ovl_create_index(struct dentry *dentry, const struct ovl_fh *fh, 488 struct dentry *upper) 489 { 490 struct ovl_fs *ofs = OVL_FS(dentry->d_sb); 491 struct dentry *indexdir = ovl_indexdir(dentry->d_sb); 492 struct inode *dir = d_inode(indexdir); 493 struct dentry *index = NULL; 494 struct dentry *temp = NULL; 495 struct qstr name = { }; 496 int err; 497 498 /* 499 * For now this is only used for creating index entry for directories, 500 * because non-dir are copied up directly to index and then hardlinked 501 * to upper dir. 502 * 503 * TODO: implement create index for non-dir, so we can call it when 504 * encoding file handle for non-dir in case index does not exist. 505 */ 506 if (WARN_ON(!d_is_dir(dentry))) 507 return -EIO; 508 509 /* Directory not expected to be indexed before copy up */ 510 if (WARN_ON(ovl_test_flag(OVL_INDEX, d_inode(dentry)))) 511 return -EIO; 512 513 err = ovl_get_index_name_fh(fh, &name); 514 if (err) 515 return err; 516 517 temp = ovl_create_temp(ofs, indexdir, OVL_CATTR(S_IFDIR | 0)); 518 err = PTR_ERR(temp); 519 if (IS_ERR(temp)) 520 goto free_name; 521 522 err = ovl_set_upper_fh(ofs, upper, temp); 523 if (err) 524 goto out; 525 526 index = ovl_lookup_upper(ofs, name.name, indexdir, name.len); 527 if (IS_ERR(index)) { 528 err = PTR_ERR(index); 529 } else { 530 err = ovl_do_rename(ofs, dir, temp, dir, index, 0); 531 dput(index); 532 } 533 out: 534 if (err) 535 ovl_cleanup(ofs, dir, temp); 536 dput(temp); 537 free_name: 538 kfree(name.name); 539 return err; 540 } 541 542 struct ovl_copy_up_ctx { 543 struct dentry *parent; 544 struct dentry *dentry; 545 struct path lowerpath; 546 struct kstat stat; 547 struct kstat pstat; 548 const char *link; 549 struct dentry *destdir; 550 struct qstr destname; 551 struct dentry *workdir; 552 const struct ovl_fh *origin_fh; 553 bool origin; 554 bool indexed; 555 bool metacopy; 556 bool metacopy_digest; 557 }; 558 559 static int ovl_link_up(struct ovl_copy_up_ctx *c) 560 { 561 int err; 562 struct dentry *upper; 563 struct dentry *upperdir = ovl_dentry_upper(c->parent); 564 struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb); 565 struct inode *udir = d_inode(upperdir); 566 567 ovl_start_write(c->dentry); 568 569 /* Mark parent "impure" because it may now contain non-pure upper */ 570 err = ovl_set_impure(c->parent, upperdir); 571 if (err) 572 goto out; 573 574 err = ovl_set_nlink_lower(c->dentry); 575 if (err) 576 goto out; 577 578 inode_lock_nested(udir, I_MUTEX_PARENT); 579 upper = ovl_lookup_upper(ofs, c->dentry->d_name.name, upperdir, 580 c->dentry->d_name.len); 581 err = PTR_ERR(upper); 582 if (!IS_ERR(upper)) { 583 err = ovl_do_link(ofs, ovl_dentry_upper(c->dentry), udir, upper); 584 dput(upper); 585 586 if (!err) { 587 /* Restore timestamps on parent (best effort) */ 588 ovl_set_timestamps(ofs, upperdir, &c->pstat); 589 ovl_dentry_set_upper_alias(c->dentry); 590 ovl_dentry_update_reval(c->dentry, upper); 591 } 592 } 593 inode_unlock(udir); 594 if (err) 595 goto out; 596 597 err = ovl_set_nlink_upper(c->dentry); 598 599 out: 600 ovl_end_write(c->dentry); 601 return err; 602 } 603 604 static int ovl_copy_up_data(struct ovl_copy_up_ctx *c, const struct path *temp) 605 { 606 struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb); 607 struct file *new_file; 608 int err; 609 610 if (!S_ISREG(c->stat.mode) || c->metacopy || !c->stat.size) 611 return 0; 612 613 new_file = ovl_path_open(temp, O_LARGEFILE | O_WRONLY); 614 if (IS_ERR(new_file)) 615 return PTR_ERR(new_file); 616 617 err = ovl_copy_up_file(ofs, c->dentry, new_file, c->stat.size); 618 fput(new_file); 619 620 return err; 621 } 622 623 static int ovl_copy_up_metadata(struct ovl_copy_up_ctx *c, struct dentry *temp) 624 { 625 struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb); 626 struct inode *inode = d_inode(c->dentry); 627 struct path upperpath = { .mnt = ovl_upper_mnt(ofs), .dentry = temp }; 628 int err; 629 630 err = ovl_copy_xattr(c->dentry->d_sb, &c->lowerpath, temp); 631 if (err) 632 return err; 633 634 if (inode->i_flags & OVL_COPY_I_FLAGS_MASK && 635 (S_ISREG(c->stat.mode) || S_ISDIR(c->stat.mode))) { 636 /* 637 * Copy the fileattr inode flags that are the source of already 638 * copied i_flags 639 */ 640 err = ovl_copy_fileattr(inode, &c->lowerpath, &upperpath); 641 if (err) 642 return err; 643 } 644 645 /* 646 * Store identifier of lower inode in upper inode xattr to 647 * allow lookup of the copy up origin inode. 648 * 649 * Don't set origin when we are breaking the association with a lower 650 * hard link. 651 */ 652 if (c->origin) { 653 err = ovl_set_origin_fh(ofs, c->origin_fh, temp); 654 if (err) 655 return err; 656 } 657 658 if (c->metacopy) { 659 struct path lowerdatapath; 660 struct ovl_metacopy metacopy_data = OVL_METACOPY_INIT; 661 662 ovl_path_lowerdata(c->dentry, &lowerdatapath); 663 if (WARN_ON_ONCE(lowerdatapath.dentry == NULL)) 664 return -EIO; 665 err = ovl_get_verity_digest(ofs, &lowerdatapath, &metacopy_data); 666 if (err) 667 return err; 668 669 if (metacopy_data.digest_algo) 670 c->metacopy_digest = true; 671 672 err = ovl_set_metacopy_xattr(ofs, temp, &metacopy_data); 673 if (err) 674 return err; 675 } 676 677 inode_lock(temp->d_inode); 678 if (S_ISREG(c->stat.mode)) 679 err = ovl_set_size(ofs, temp, &c->stat); 680 if (!err) 681 err = ovl_set_attr(ofs, temp, &c->stat); 682 inode_unlock(temp->d_inode); 683 684 return err; 685 } 686 687 struct ovl_cu_creds { 688 const struct cred *old; 689 struct cred *new; 690 }; 691 692 static int ovl_prep_cu_creds(struct dentry *dentry, struct ovl_cu_creds *cc) 693 { 694 int err; 695 696 cc->old = cc->new = NULL; 697 err = security_inode_copy_up(dentry, &cc->new); 698 if (err < 0) 699 return err; 700 701 if (cc->new) 702 cc->old = override_creds(cc->new); 703 704 return 0; 705 } 706 707 static void ovl_revert_cu_creds(struct ovl_cu_creds *cc) 708 { 709 if (cc->new) { 710 revert_creds(cc->old); 711 put_cred(cc->new); 712 } 713 } 714 715 /* 716 * Copyup using workdir to prepare temp file. Used when copying up directories, 717 * special files or when upper fs doesn't support O_TMPFILE. 718 */ 719 static int ovl_copy_up_workdir(struct ovl_copy_up_ctx *c) 720 { 721 struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb); 722 struct inode *inode; 723 struct inode *udir = d_inode(c->destdir), *wdir = d_inode(c->workdir); 724 struct path path = { .mnt = ovl_upper_mnt(ofs) }; 725 struct dentry *temp, *upper; 726 struct ovl_cu_creds cc; 727 int err; 728 struct ovl_cattr cattr = { 729 /* Can't properly set mode on creation because of the umask */ 730 .mode = c->stat.mode & S_IFMT, 731 .rdev = c->stat.rdev, 732 .link = c->link 733 }; 734 735 err = ovl_prep_cu_creds(c->dentry, &cc); 736 if (err) 737 return err; 738 739 ovl_start_write(c->dentry); 740 inode_lock(wdir); 741 temp = ovl_create_temp(ofs, c->workdir, &cattr); 742 inode_unlock(wdir); 743 ovl_end_write(c->dentry); 744 ovl_revert_cu_creds(&cc); 745 746 if (IS_ERR(temp)) 747 return PTR_ERR(temp); 748 749 /* 750 * Copy up data first and then xattrs. Writing data after 751 * xattrs will remove security.capability xattr automatically. 752 */ 753 path.dentry = temp; 754 err = ovl_copy_up_data(c, &path); 755 /* 756 * We cannot hold lock_rename() throughout this helper, because of 757 * lock ordering with sb_writers, which shouldn't be held when calling 758 * ovl_copy_up_data(), so lock workdir and destdir and make sure that 759 * temp wasn't moved before copy up completion or cleanup. 760 */ 761 ovl_start_write(c->dentry); 762 if (lock_rename(c->workdir, c->destdir) != NULL || 763 temp->d_parent != c->workdir) { 764 /* temp or workdir moved underneath us? abort without cleanup */ 765 dput(temp); 766 err = -EIO; 767 goto unlock; 768 } else if (err) { 769 goto cleanup; 770 } 771 772 err = ovl_copy_up_metadata(c, temp); 773 if (err) 774 goto cleanup; 775 776 if (S_ISDIR(c->stat.mode) && c->indexed) { 777 err = ovl_create_index(c->dentry, c->origin_fh, temp); 778 if (err) 779 goto cleanup; 780 } 781 782 upper = ovl_lookup_upper(ofs, c->destname.name, c->destdir, 783 c->destname.len); 784 err = PTR_ERR(upper); 785 if (IS_ERR(upper)) 786 goto cleanup; 787 788 err = ovl_do_rename(ofs, wdir, temp, udir, upper, 0); 789 dput(upper); 790 if (err) 791 goto cleanup; 792 793 inode = d_inode(c->dentry); 794 if (c->metacopy_digest) 795 ovl_set_flag(OVL_HAS_DIGEST, inode); 796 else 797 ovl_clear_flag(OVL_HAS_DIGEST, inode); 798 ovl_clear_flag(OVL_VERIFIED_DIGEST, inode); 799 800 if (!c->metacopy) 801 ovl_set_upperdata(inode); 802 ovl_inode_update(inode, temp); 803 if (S_ISDIR(inode->i_mode)) 804 ovl_set_flag(OVL_WHITEOUTS, inode); 805 unlock: 806 unlock_rename(c->workdir, c->destdir); 807 ovl_end_write(c->dentry); 808 809 return err; 810 811 cleanup: 812 ovl_cleanup(ofs, wdir, temp); 813 dput(temp); 814 goto unlock; 815 } 816 817 /* Copyup using O_TMPFILE which does not require cross dir locking */ 818 static int ovl_copy_up_tmpfile(struct ovl_copy_up_ctx *c) 819 { 820 struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb); 821 struct inode *udir = d_inode(c->destdir); 822 struct dentry *temp, *upper; 823 struct file *tmpfile; 824 struct ovl_cu_creds cc; 825 int err; 826 827 err = ovl_prep_cu_creds(c->dentry, &cc); 828 if (err) 829 return err; 830 831 ovl_start_write(c->dentry); 832 tmpfile = ovl_do_tmpfile(ofs, c->workdir, c->stat.mode); 833 ovl_end_write(c->dentry); 834 ovl_revert_cu_creds(&cc); 835 if (IS_ERR(tmpfile)) 836 return PTR_ERR(tmpfile); 837 838 temp = tmpfile->f_path.dentry; 839 if (!c->metacopy && c->stat.size) { 840 err = ovl_copy_up_file(ofs, c->dentry, tmpfile, c->stat.size); 841 if (err) 842 goto out_fput; 843 } 844 845 ovl_start_write(c->dentry); 846 847 err = ovl_copy_up_metadata(c, temp); 848 if (err) 849 goto out; 850 851 inode_lock_nested(udir, I_MUTEX_PARENT); 852 853 upper = ovl_lookup_upper(ofs, c->destname.name, c->destdir, 854 c->destname.len); 855 err = PTR_ERR(upper); 856 if (!IS_ERR(upper)) { 857 err = ovl_do_link(ofs, temp, udir, upper); 858 dput(upper); 859 } 860 inode_unlock(udir); 861 862 if (err) 863 goto out; 864 865 if (c->metacopy_digest) 866 ovl_set_flag(OVL_HAS_DIGEST, d_inode(c->dentry)); 867 else 868 ovl_clear_flag(OVL_HAS_DIGEST, d_inode(c->dentry)); 869 ovl_clear_flag(OVL_VERIFIED_DIGEST, d_inode(c->dentry)); 870 871 if (!c->metacopy) 872 ovl_set_upperdata(d_inode(c->dentry)); 873 ovl_inode_update(d_inode(c->dentry), dget(temp)); 874 875 out: 876 ovl_end_write(c->dentry); 877 out_fput: 878 fput(tmpfile); 879 return err; 880 } 881 882 /* 883 * Copy up a single dentry 884 * 885 * All renames start with copy up of source if necessary. The actual 886 * rename will only proceed once the copy up was successful. Copy up uses 887 * upper parent i_mutex for exclusion. Since rename can change d_parent it 888 * is possible that the copy up will lock the old parent. At that point 889 * the file will have already been copied up anyway. 890 */ 891 static int ovl_do_copy_up(struct ovl_copy_up_ctx *c) 892 { 893 int err; 894 struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb); 895 struct dentry *origin = c->lowerpath.dentry; 896 struct ovl_fh *fh = NULL; 897 bool to_index = false; 898 899 /* 900 * Indexed non-dir is copied up directly to the index entry and then 901 * hardlinked to upper dir. Indexed dir is copied up to indexdir, 902 * then index entry is created and then copied up dir installed. 903 * Copying dir up to indexdir instead of workdir simplifies locking. 904 */ 905 if (ovl_need_index(c->dentry)) { 906 c->indexed = true; 907 if (S_ISDIR(c->stat.mode)) 908 c->workdir = ovl_indexdir(c->dentry->d_sb); 909 else 910 to_index = true; 911 } 912 913 if (S_ISDIR(c->stat.mode) || c->stat.nlink == 1 || to_index) { 914 fh = ovl_get_origin_fh(ofs, origin); 915 if (IS_ERR(fh)) 916 return PTR_ERR(fh); 917 918 /* origin_fh may be NULL */ 919 c->origin_fh = fh; 920 c->origin = true; 921 } 922 923 if (to_index) { 924 c->destdir = ovl_indexdir(c->dentry->d_sb); 925 err = ovl_get_index_name(ofs, origin, &c->destname); 926 if (err) 927 goto out_free_fh; 928 } else if (WARN_ON(!c->parent)) { 929 /* Disconnected dentry must be copied up to index dir */ 930 err = -EIO; 931 goto out_free_fh; 932 } else { 933 /* 934 * Mark parent "impure" because it may now contain non-pure 935 * upper 936 */ 937 ovl_start_write(c->dentry); 938 err = ovl_set_impure(c->parent, c->destdir); 939 ovl_end_write(c->dentry); 940 if (err) 941 goto out_free_fh; 942 } 943 944 /* Should we copyup with O_TMPFILE or with workdir? */ 945 if (S_ISREG(c->stat.mode) && ofs->tmpfile) 946 err = ovl_copy_up_tmpfile(c); 947 else 948 err = ovl_copy_up_workdir(c); 949 if (err) 950 goto out; 951 952 if (c->indexed) 953 ovl_set_flag(OVL_INDEX, d_inode(c->dentry)); 954 955 ovl_start_write(c->dentry); 956 if (to_index) { 957 /* Initialize nlink for copy up of disconnected dentry */ 958 err = ovl_set_nlink_upper(c->dentry); 959 } else { 960 struct inode *udir = d_inode(c->destdir); 961 962 /* Restore timestamps on parent (best effort) */ 963 inode_lock(udir); 964 ovl_set_timestamps(ofs, c->destdir, &c->pstat); 965 inode_unlock(udir); 966 967 ovl_dentry_set_upper_alias(c->dentry); 968 ovl_dentry_update_reval(c->dentry, ovl_dentry_upper(c->dentry)); 969 } 970 ovl_end_write(c->dentry); 971 972 out: 973 if (to_index) 974 kfree(c->destname.name); 975 out_free_fh: 976 kfree(fh); 977 return err; 978 } 979 980 static bool ovl_need_meta_copy_up(struct dentry *dentry, umode_t mode, 981 int flags) 982 { 983 struct ovl_fs *ofs = OVL_FS(dentry->d_sb); 984 985 if (!ofs->config.metacopy) 986 return false; 987 988 if (!S_ISREG(mode)) 989 return false; 990 991 if (flags && ((OPEN_FMODE(flags) & FMODE_WRITE) || (flags & O_TRUNC))) 992 return false; 993 994 /* Fall back to full copy if no fsverity on source data and we require verity */ 995 if (ofs->config.verity_mode == OVL_VERITY_REQUIRE) { 996 struct path lowerdata; 997 998 ovl_path_lowerdata(dentry, &lowerdata); 999 1000 if (WARN_ON_ONCE(lowerdata.dentry == NULL) || 1001 ovl_ensure_verity_loaded(&lowerdata) || 1002 !fsverity_active(d_inode(lowerdata.dentry))) { 1003 return false; 1004 } 1005 } 1006 1007 return true; 1008 } 1009 1010 static ssize_t ovl_getxattr_value(const struct path *path, char *name, char **value) 1011 { 1012 ssize_t res; 1013 char *buf; 1014 1015 res = ovl_do_getxattr(path, name, NULL, 0); 1016 if (res == -ENODATA || res == -EOPNOTSUPP) 1017 res = 0; 1018 1019 if (res > 0) { 1020 buf = kzalloc(res, GFP_KERNEL); 1021 if (!buf) 1022 return -ENOMEM; 1023 1024 res = ovl_do_getxattr(path, name, buf, res); 1025 if (res < 0) 1026 kfree(buf); 1027 else 1028 *value = buf; 1029 } 1030 return res; 1031 } 1032 1033 /* Copy up data of an inode which was copied up metadata only in the past. */ 1034 static int ovl_copy_up_meta_inode_data(struct ovl_copy_up_ctx *c) 1035 { 1036 struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb); 1037 struct path upperpath; 1038 int err; 1039 char *capability = NULL; 1040 ssize_t cap_size; 1041 1042 ovl_path_upper(c->dentry, &upperpath); 1043 if (WARN_ON(upperpath.dentry == NULL)) 1044 return -EIO; 1045 1046 if (c->stat.size) { 1047 err = cap_size = ovl_getxattr_value(&upperpath, XATTR_NAME_CAPS, 1048 &capability); 1049 if (cap_size < 0) 1050 goto out; 1051 } 1052 1053 err = ovl_copy_up_data(c, &upperpath); 1054 if (err) 1055 goto out_free; 1056 1057 /* 1058 * Writing to upper file will clear security.capability xattr. We 1059 * don't want that to happen for normal copy-up operation. 1060 */ 1061 ovl_start_write(c->dentry); 1062 if (capability) { 1063 err = ovl_do_setxattr(ofs, upperpath.dentry, XATTR_NAME_CAPS, 1064 capability, cap_size, 0); 1065 } 1066 if (!err) { 1067 err = ovl_removexattr(ofs, upperpath.dentry, 1068 OVL_XATTR_METACOPY); 1069 } 1070 ovl_end_write(c->dentry); 1071 if (err) 1072 goto out_free; 1073 1074 ovl_clear_flag(OVL_HAS_DIGEST, d_inode(c->dentry)); 1075 ovl_clear_flag(OVL_VERIFIED_DIGEST, d_inode(c->dentry)); 1076 ovl_set_upperdata(d_inode(c->dentry)); 1077 out_free: 1078 kfree(capability); 1079 out: 1080 return err; 1081 } 1082 1083 static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry, 1084 int flags) 1085 { 1086 int err; 1087 DEFINE_DELAYED_CALL(done); 1088 struct path parentpath; 1089 struct ovl_copy_up_ctx ctx = { 1090 .parent = parent, 1091 .dentry = dentry, 1092 .workdir = ovl_workdir(dentry), 1093 }; 1094 1095 if (WARN_ON(!ctx.workdir)) 1096 return -EROFS; 1097 1098 ovl_path_lower(dentry, &ctx.lowerpath); 1099 err = vfs_getattr(&ctx.lowerpath, &ctx.stat, 1100 STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT); 1101 if (err) 1102 return err; 1103 1104 if (!kuid_has_mapping(current_user_ns(), ctx.stat.uid) || 1105 !kgid_has_mapping(current_user_ns(), ctx.stat.gid)) 1106 return -EOVERFLOW; 1107 1108 ctx.metacopy = ovl_need_meta_copy_up(dentry, ctx.stat.mode, flags); 1109 1110 if (parent) { 1111 ovl_path_upper(parent, &parentpath); 1112 ctx.destdir = parentpath.dentry; 1113 ctx.destname = dentry->d_name; 1114 1115 err = vfs_getattr(&parentpath, &ctx.pstat, 1116 STATX_ATIME | STATX_MTIME, 1117 AT_STATX_SYNC_AS_STAT); 1118 if (err) 1119 return err; 1120 } 1121 1122 /* maybe truncate regular file. this has no effect on dirs */ 1123 if (flags & O_TRUNC) 1124 ctx.stat.size = 0; 1125 1126 if (S_ISLNK(ctx.stat.mode)) { 1127 ctx.link = vfs_get_link(ctx.lowerpath.dentry, &done); 1128 if (IS_ERR(ctx.link)) 1129 return PTR_ERR(ctx.link); 1130 } 1131 1132 err = ovl_copy_up_start(dentry, flags); 1133 /* err < 0: interrupted, err > 0: raced with another copy-up */ 1134 if (unlikely(err)) { 1135 if (err > 0) 1136 err = 0; 1137 } else { 1138 if (!ovl_dentry_upper(dentry)) 1139 err = ovl_do_copy_up(&ctx); 1140 if (!err && parent && !ovl_dentry_has_upper_alias(dentry)) 1141 err = ovl_link_up(&ctx); 1142 if (!err && ovl_dentry_needs_data_copy_up_locked(dentry, flags)) 1143 err = ovl_copy_up_meta_inode_data(&ctx); 1144 ovl_copy_up_end(dentry); 1145 } 1146 do_delayed_call(&done); 1147 1148 return err; 1149 } 1150 1151 static int ovl_copy_up_flags(struct dentry *dentry, int flags) 1152 { 1153 int err = 0; 1154 const struct cred *old_cred; 1155 bool disconnected = (dentry->d_flags & DCACHE_DISCONNECTED); 1156 1157 /* 1158 * With NFS export, copy up can get called for a disconnected non-dir. 1159 * In this case, we will copy up lower inode to index dir without 1160 * linking it to upper dir. 1161 */ 1162 if (WARN_ON(disconnected && d_is_dir(dentry))) 1163 return -EIO; 1164 1165 /* 1166 * We may not need lowerdata if we are only doing metacopy up, but it is 1167 * not very important to optimize this case, so do lazy lowerdata lookup 1168 * before any copy up, so we can do it before taking ovl_inode_lock(). 1169 */ 1170 err = ovl_verify_lowerdata(dentry); 1171 if (err) 1172 return err; 1173 1174 old_cred = ovl_override_creds(dentry->d_sb); 1175 while (!err) { 1176 struct dentry *next; 1177 struct dentry *parent = NULL; 1178 1179 if (ovl_already_copied_up(dentry, flags)) 1180 break; 1181 1182 next = dget(dentry); 1183 /* find the topmost dentry not yet copied up */ 1184 for (; !disconnected;) { 1185 parent = dget_parent(next); 1186 1187 if (ovl_dentry_upper(parent)) 1188 break; 1189 1190 dput(next); 1191 next = parent; 1192 } 1193 1194 err = ovl_copy_up_one(parent, next, flags); 1195 1196 dput(parent); 1197 dput(next); 1198 } 1199 revert_creds(old_cred); 1200 1201 return err; 1202 } 1203 1204 static bool ovl_open_need_copy_up(struct dentry *dentry, int flags) 1205 { 1206 /* Copy up of disconnected dentry does not set upper alias */ 1207 if (ovl_already_copied_up(dentry, flags)) 1208 return false; 1209 1210 if (special_file(d_inode(dentry)->i_mode)) 1211 return false; 1212 1213 if (!ovl_open_flags_need_copy_up(flags)) 1214 return false; 1215 1216 return true; 1217 } 1218 1219 int ovl_maybe_copy_up(struct dentry *dentry, int flags) 1220 { 1221 if (!ovl_open_need_copy_up(dentry, flags)) 1222 return 0; 1223 1224 return ovl_copy_up_flags(dentry, flags); 1225 } 1226 1227 int ovl_copy_up_with_data(struct dentry *dentry) 1228 { 1229 return ovl_copy_up_flags(dentry, O_WRONLY); 1230 } 1231 1232 int ovl_copy_up(struct dentry *dentry) 1233 { 1234 return ovl_copy_up_flags(dentry, 0); 1235 } 1236