1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2011 Novell Inc. 4 * Copyright (C) 2016 Red Hat, Inc. 5 */ 6 7 #include <linux/fs.h> 8 #include <linux/cred.h> 9 #include <linux/ctype.h> 10 #include <linux/namei.h> 11 #include <linux/xattr.h> 12 #include <linux/ratelimit.h> 13 #include <linux/mount.h> 14 #include <linux/exportfs.h> 15 #include "overlayfs.h" 16 17 struct ovl_lookup_data { 18 struct super_block *sb; 19 struct qstr name; 20 bool is_dir; 21 bool opaque; 22 bool stop; 23 bool last; 24 char *redirect; 25 bool metacopy; 26 }; 27 28 static int ovl_check_redirect(struct dentry *dentry, struct ovl_lookup_data *d, 29 size_t prelen, const char *post) 30 { 31 int res; 32 char *buf; 33 34 buf = ovl_get_redirect_xattr(dentry, prelen + strlen(post)); 35 if (IS_ERR_OR_NULL(buf)) 36 return PTR_ERR(buf); 37 38 if (buf[0] == '/') { 39 /* 40 * One of the ancestor path elements in an absolute path 41 * lookup in ovl_lookup_layer() could have been opaque and 42 * that will stop further lookup in lower layers (d->stop=true) 43 * But we have found an absolute redirect in decendant path 44 * element and that should force continue lookup in lower 45 * layers (reset d->stop). 46 */ 47 d->stop = false; 48 } else { 49 res = strlen(buf) + 1; 50 memmove(buf + prelen, buf, res); 51 memcpy(buf, d->name.name, prelen); 52 } 53 54 strcat(buf, post); 55 kfree(d->redirect); 56 d->redirect = buf; 57 d->name.name = d->redirect; 58 d->name.len = strlen(d->redirect); 59 60 return 0; 61 } 62 63 static int ovl_acceptable(void *ctx, struct dentry *dentry) 64 { 65 /* 66 * A non-dir origin may be disconnected, which is fine, because 67 * we only need it for its unique inode number. 68 */ 69 if (!d_is_dir(dentry)) 70 return 1; 71 72 /* Don't decode a deleted empty directory */ 73 if (d_unhashed(dentry)) 74 return 0; 75 76 /* Check if directory belongs to the layer we are decoding from */ 77 return is_subdir(dentry, ((struct vfsmount *)ctx)->mnt_root); 78 } 79 80 /* 81 * Check validity of an overlay file handle buffer. 82 * 83 * Return 0 for a valid file handle. 84 * Return -ENODATA for "origin unknown". 85 * Return <0 for an invalid file handle. 86 */ 87 int ovl_check_fh_len(struct ovl_fh *fh, int fh_len) 88 { 89 if (fh_len < sizeof(struct ovl_fh) || fh_len < fh->len) 90 return -EINVAL; 91 92 if (fh->magic != OVL_FH_MAGIC) 93 return -EINVAL; 94 95 /* Treat larger version and unknown flags as "origin unknown" */ 96 if (fh->version > OVL_FH_VERSION || fh->flags & ~OVL_FH_FLAG_ALL) 97 return -ENODATA; 98 99 /* Treat endianness mismatch as "origin unknown" */ 100 if (!(fh->flags & OVL_FH_FLAG_ANY_ENDIAN) && 101 (fh->flags & OVL_FH_FLAG_BIG_ENDIAN) != OVL_FH_FLAG_CPU_ENDIAN) 102 return -ENODATA; 103 104 return 0; 105 } 106 107 static struct ovl_fh *ovl_get_fh(struct dentry *dentry, const char *name) 108 { 109 int res, err; 110 struct ovl_fh *fh = NULL; 111 112 res = vfs_getxattr(dentry, name, NULL, 0); 113 if (res < 0) { 114 if (res == -ENODATA || res == -EOPNOTSUPP) 115 return NULL; 116 goto fail; 117 } 118 /* Zero size value means "copied up but origin unknown" */ 119 if (res == 0) 120 return NULL; 121 122 fh = kzalloc(res, GFP_KERNEL); 123 if (!fh) 124 return ERR_PTR(-ENOMEM); 125 126 res = vfs_getxattr(dentry, name, fh, res); 127 if (res < 0) 128 goto fail; 129 130 err = ovl_check_fh_len(fh, res); 131 if (err < 0) { 132 if (err == -ENODATA) 133 goto out; 134 goto invalid; 135 } 136 137 return fh; 138 139 out: 140 kfree(fh); 141 return NULL; 142 143 fail: 144 pr_warn_ratelimited("overlayfs: failed to get origin (%i)\n", res); 145 goto out; 146 invalid: 147 pr_warn_ratelimited("overlayfs: invalid origin (%*phN)\n", res, fh); 148 goto out; 149 } 150 151 struct dentry *ovl_decode_real_fh(struct ovl_fh *fh, struct vfsmount *mnt, 152 bool connected) 153 { 154 struct dentry *real; 155 int bytes; 156 157 /* 158 * Make sure that the stored uuid matches the uuid of the lower 159 * layer where file handle will be decoded. 160 */ 161 if (!uuid_equal(&fh->uuid, &mnt->mnt_sb->s_uuid)) 162 return NULL; 163 164 bytes = (fh->len - offsetof(struct ovl_fh, fid)); 165 real = exportfs_decode_fh(mnt, (struct fid *)fh->fid, 166 bytes >> 2, (int)fh->type, 167 connected ? ovl_acceptable : NULL, mnt); 168 if (IS_ERR(real)) { 169 /* 170 * Treat stale file handle to lower file as "origin unknown". 171 * upper file handle could become stale when upper file is 172 * unlinked and this information is needed to handle stale 173 * index entries correctly. 174 */ 175 if (real == ERR_PTR(-ESTALE) && 176 !(fh->flags & OVL_FH_FLAG_PATH_UPPER)) 177 real = NULL; 178 return real; 179 } 180 181 if (ovl_dentry_weird(real)) { 182 dput(real); 183 return NULL; 184 } 185 186 return real; 187 } 188 189 static bool ovl_is_opaquedir(struct dentry *dentry) 190 { 191 return ovl_check_dir_xattr(dentry, OVL_XATTR_OPAQUE); 192 } 193 194 static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d, 195 const char *name, unsigned int namelen, 196 size_t prelen, const char *post, 197 struct dentry **ret) 198 { 199 struct dentry *this; 200 int err; 201 bool last_element = !post[0]; 202 203 this = lookup_positive_unlocked(name, base, namelen); 204 if (IS_ERR(this)) { 205 err = PTR_ERR(this); 206 this = NULL; 207 if (err == -ENOENT || err == -ENAMETOOLONG) 208 goto out; 209 goto out_err; 210 } 211 212 if (ovl_dentry_weird(this)) { 213 /* Don't support traversing automounts and other weirdness */ 214 err = -EREMOTE; 215 goto out_err; 216 } 217 if (ovl_is_whiteout(this)) { 218 d->stop = d->opaque = true; 219 goto put_and_out; 220 } 221 /* 222 * This dentry should be a regular file if previous layer lookup 223 * found a metacopy dentry. 224 */ 225 if (last_element && d->metacopy && !d_is_reg(this)) { 226 d->stop = true; 227 goto put_and_out; 228 } 229 if (!d_can_lookup(this)) { 230 if (d->is_dir || !last_element) { 231 d->stop = true; 232 goto put_and_out; 233 } 234 err = ovl_check_metacopy_xattr(this); 235 if (err < 0) 236 goto out_err; 237 238 d->metacopy = err; 239 d->stop = !d->metacopy; 240 if (!d->metacopy || d->last) 241 goto out; 242 } else { 243 if (ovl_lookup_trap_inode(d->sb, this)) { 244 /* Caught in a trap of overlapping layers */ 245 err = -ELOOP; 246 goto out_err; 247 } 248 249 if (last_element) 250 d->is_dir = true; 251 if (d->last) 252 goto out; 253 254 if (ovl_is_opaquedir(this)) { 255 d->stop = true; 256 if (last_element) 257 d->opaque = true; 258 goto out; 259 } 260 } 261 err = ovl_check_redirect(this, d, prelen, post); 262 if (err) 263 goto out_err; 264 out: 265 *ret = this; 266 return 0; 267 268 put_and_out: 269 dput(this); 270 this = NULL; 271 goto out; 272 273 out_err: 274 dput(this); 275 return err; 276 } 277 278 static int ovl_lookup_layer(struct dentry *base, struct ovl_lookup_data *d, 279 struct dentry **ret) 280 { 281 /* Counting down from the end, since the prefix can change */ 282 size_t rem = d->name.len - 1; 283 struct dentry *dentry = NULL; 284 int err; 285 286 if (d->name.name[0] != '/') 287 return ovl_lookup_single(base, d, d->name.name, d->name.len, 288 0, "", ret); 289 290 while (!IS_ERR_OR_NULL(base) && d_can_lookup(base)) { 291 const char *s = d->name.name + d->name.len - rem; 292 const char *next = strchrnul(s, '/'); 293 size_t thislen = next - s; 294 bool end = !next[0]; 295 296 /* Verify we did not go off the rails */ 297 if (WARN_ON(s[-1] != '/')) 298 return -EIO; 299 300 err = ovl_lookup_single(base, d, s, thislen, 301 d->name.len - rem, next, &base); 302 dput(dentry); 303 if (err) 304 return err; 305 dentry = base; 306 if (end) 307 break; 308 309 rem -= thislen + 1; 310 311 if (WARN_ON(rem >= d->name.len)) 312 return -EIO; 313 } 314 *ret = dentry; 315 return 0; 316 } 317 318 319 int ovl_check_origin_fh(struct ovl_fs *ofs, struct ovl_fh *fh, bool connected, 320 struct dentry *upperdentry, struct ovl_path **stackp) 321 { 322 struct dentry *origin = NULL; 323 int i; 324 325 for (i = 0; i < ofs->numlower; i++) { 326 origin = ovl_decode_real_fh(fh, ofs->lower_layers[i].mnt, 327 connected); 328 if (origin) 329 break; 330 } 331 332 if (!origin) 333 return -ESTALE; 334 else if (IS_ERR(origin)) 335 return PTR_ERR(origin); 336 337 if (upperdentry && !ovl_is_whiteout(upperdentry) && 338 ((d_inode(origin)->i_mode ^ d_inode(upperdentry)->i_mode) & S_IFMT)) 339 goto invalid; 340 341 if (!*stackp) 342 *stackp = kmalloc(sizeof(struct ovl_path), GFP_KERNEL); 343 if (!*stackp) { 344 dput(origin); 345 return -ENOMEM; 346 } 347 **stackp = (struct ovl_path){ 348 .dentry = origin, 349 .layer = &ofs->lower_layers[i] 350 }; 351 352 return 0; 353 354 invalid: 355 pr_warn_ratelimited("overlayfs: invalid origin (%pd2, ftype=%x, origin ftype=%x).\n", 356 upperdentry, d_inode(upperdentry)->i_mode & S_IFMT, 357 d_inode(origin)->i_mode & S_IFMT); 358 dput(origin); 359 return -EIO; 360 } 361 362 static int ovl_check_origin(struct ovl_fs *ofs, struct dentry *upperdentry, 363 struct ovl_path **stackp, unsigned int *ctrp) 364 { 365 struct ovl_fh *fh = ovl_get_fh(upperdentry, OVL_XATTR_ORIGIN); 366 int err; 367 368 if (IS_ERR_OR_NULL(fh)) 369 return PTR_ERR(fh); 370 371 err = ovl_check_origin_fh(ofs, fh, false, upperdentry, stackp); 372 kfree(fh); 373 374 if (err) { 375 if (err == -ESTALE) 376 return 0; 377 return err; 378 } 379 380 if (WARN_ON(*ctrp)) 381 return -EIO; 382 383 *ctrp = 1; 384 return 0; 385 } 386 387 /* 388 * Verify that @fh matches the file handle stored in xattr @name. 389 * Return 0 on match, -ESTALE on mismatch, < 0 on error. 390 */ 391 static int ovl_verify_fh(struct dentry *dentry, const char *name, 392 const struct ovl_fh *fh) 393 { 394 struct ovl_fh *ofh = ovl_get_fh(dentry, name); 395 int err = 0; 396 397 if (!ofh) 398 return -ENODATA; 399 400 if (IS_ERR(ofh)) 401 return PTR_ERR(ofh); 402 403 if (fh->len != ofh->len || memcmp(fh, ofh, fh->len)) 404 err = -ESTALE; 405 406 kfree(ofh); 407 return err; 408 } 409 410 /* 411 * Verify that @real dentry matches the file handle stored in xattr @name. 412 * 413 * If @set is true and there is no stored file handle, encode @real and store 414 * file handle in xattr @name. 415 * 416 * Return 0 on match, -ESTALE on mismatch, -ENODATA on no xattr, < 0 on error. 417 */ 418 int ovl_verify_set_fh(struct dentry *dentry, const char *name, 419 struct dentry *real, bool is_upper, bool set) 420 { 421 struct inode *inode; 422 struct ovl_fh *fh; 423 int err; 424 425 fh = ovl_encode_real_fh(real, is_upper); 426 err = PTR_ERR(fh); 427 if (IS_ERR(fh)) { 428 fh = NULL; 429 goto fail; 430 } 431 432 err = ovl_verify_fh(dentry, name, fh); 433 if (set && err == -ENODATA) 434 err = ovl_do_setxattr(dentry, name, fh, fh->len, 0); 435 if (err) 436 goto fail; 437 438 out: 439 kfree(fh); 440 return err; 441 442 fail: 443 inode = d_inode(real); 444 pr_warn_ratelimited("overlayfs: failed to verify %s (%pd2, ino=%lu, err=%i)\n", 445 is_upper ? "upper" : "origin", real, 446 inode ? inode->i_ino : 0, err); 447 goto out; 448 } 449 450 /* Get upper dentry from index */ 451 struct dentry *ovl_index_upper(struct ovl_fs *ofs, struct dentry *index) 452 { 453 struct ovl_fh *fh; 454 struct dentry *upper; 455 456 if (!d_is_dir(index)) 457 return dget(index); 458 459 fh = ovl_get_fh(index, OVL_XATTR_UPPER); 460 if (IS_ERR_OR_NULL(fh)) 461 return ERR_CAST(fh); 462 463 upper = ovl_decode_real_fh(fh, ofs->upper_mnt, true); 464 kfree(fh); 465 466 if (IS_ERR_OR_NULL(upper)) 467 return upper ?: ERR_PTR(-ESTALE); 468 469 if (!d_is_dir(upper)) { 470 pr_warn_ratelimited("overlayfs: invalid index upper (%pd2, upper=%pd2).\n", 471 index, upper); 472 dput(upper); 473 return ERR_PTR(-EIO); 474 } 475 476 return upper; 477 } 478 479 /* Is this a leftover from create/whiteout of directory index entry? */ 480 static bool ovl_is_temp_index(struct dentry *index) 481 { 482 return index->d_name.name[0] == '#'; 483 } 484 485 /* 486 * Verify that an index entry name matches the origin file handle stored in 487 * OVL_XATTR_ORIGIN and that origin file handle can be decoded to lower path. 488 * Return 0 on match, -ESTALE on mismatch or stale origin, < 0 on error. 489 */ 490 int ovl_verify_index(struct ovl_fs *ofs, struct dentry *index) 491 { 492 struct ovl_fh *fh = NULL; 493 size_t len; 494 struct ovl_path origin = { }; 495 struct ovl_path *stack = &origin; 496 struct dentry *upper = NULL; 497 int err; 498 499 if (!d_inode(index)) 500 return 0; 501 502 /* Cleanup leftover from index create/cleanup attempt */ 503 err = -ESTALE; 504 if (ovl_is_temp_index(index)) 505 goto fail; 506 507 err = -EINVAL; 508 if (index->d_name.len < sizeof(struct ovl_fh)*2) 509 goto fail; 510 511 err = -ENOMEM; 512 len = index->d_name.len / 2; 513 fh = kzalloc(len, GFP_KERNEL); 514 if (!fh) 515 goto fail; 516 517 err = -EINVAL; 518 if (hex2bin((u8 *)fh, index->d_name.name, len)) 519 goto fail; 520 521 err = ovl_check_fh_len(fh, len); 522 if (err) 523 goto fail; 524 525 /* 526 * Whiteout index entries are used as an indication that an exported 527 * overlay file handle should be treated as stale (i.e. after unlink 528 * of the overlay inode). These entries contain no origin xattr. 529 */ 530 if (ovl_is_whiteout(index)) 531 goto out; 532 533 /* 534 * Verifying directory index entries are not stale is expensive, so 535 * only verify stale dir index if NFS export is enabled. 536 */ 537 if (d_is_dir(index) && !ofs->config.nfs_export) 538 goto out; 539 540 /* 541 * Directory index entries should have 'upper' xattr pointing to the 542 * real upper dir. Non-dir index entries are hardlinks to the upper 543 * real inode. For non-dir index, we can read the copy up origin xattr 544 * directly from the index dentry, but for dir index we first need to 545 * decode the upper directory. 546 */ 547 upper = ovl_index_upper(ofs, index); 548 if (IS_ERR_OR_NULL(upper)) { 549 err = PTR_ERR(upper); 550 /* 551 * Directory index entries with no 'upper' xattr need to be 552 * removed. When dir index entry has a stale 'upper' xattr, 553 * we assume that upper dir was removed and we treat the dir 554 * index as orphan entry that needs to be whited out. 555 */ 556 if (err == -ESTALE) 557 goto orphan; 558 else if (!err) 559 err = -ESTALE; 560 goto fail; 561 } 562 563 err = ovl_verify_fh(upper, OVL_XATTR_ORIGIN, fh); 564 dput(upper); 565 if (err) 566 goto fail; 567 568 /* Check if non-dir index is orphan and don't warn before cleaning it */ 569 if (!d_is_dir(index) && d_inode(index)->i_nlink == 1) { 570 err = ovl_check_origin_fh(ofs, fh, false, index, &stack); 571 if (err) 572 goto fail; 573 574 if (ovl_get_nlink(origin.dentry, index, 0) == 0) 575 goto orphan; 576 } 577 578 out: 579 dput(origin.dentry); 580 kfree(fh); 581 return err; 582 583 fail: 584 pr_warn_ratelimited("overlayfs: failed to verify index (%pd2, ftype=%x, err=%i)\n", 585 index, d_inode(index)->i_mode & S_IFMT, err); 586 goto out; 587 588 orphan: 589 pr_warn_ratelimited("overlayfs: orphan index entry (%pd2, ftype=%x, nlink=%u)\n", 590 index, d_inode(index)->i_mode & S_IFMT, 591 d_inode(index)->i_nlink); 592 err = -ENOENT; 593 goto out; 594 } 595 596 static int ovl_get_index_name_fh(struct ovl_fh *fh, struct qstr *name) 597 { 598 char *n, *s; 599 600 n = kcalloc(fh->len, 2, GFP_KERNEL); 601 if (!n) 602 return -ENOMEM; 603 604 s = bin2hex(n, fh, fh->len); 605 *name = (struct qstr) QSTR_INIT(n, s - n); 606 607 return 0; 608 609 } 610 611 /* 612 * Lookup in indexdir for the index entry of a lower real inode or a copy up 613 * origin inode. The index entry name is the hex representation of the lower 614 * inode file handle. 615 * 616 * If the index dentry in negative, then either no lower aliases have been 617 * copied up yet, or aliases have been copied up in older kernels and are 618 * not indexed. 619 * 620 * If the index dentry for a copy up origin inode is positive, but points 621 * to an inode different than the upper inode, then either the upper inode 622 * has been copied up and not indexed or it was indexed, but since then 623 * index dir was cleared. Either way, that index cannot be used to indentify 624 * the overlay inode. 625 */ 626 int ovl_get_index_name(struct dentry *origin, struct qstr *name) 627 { 628 struct ovl_fh *fh; 629 int err; 630 631 fh = ovl_encode_real_fh(origin, false); 632 if (IS_ERR(fh)) 633 return PTR_ERR(fh); 634 635 err = ovl_get_index_name_fh(fh, name); 636 637 kfree(fh); 638 return err; 639 } 640 641 /* Lookup index by file handle for NFS export */ 642 struct dentry *ovl_get_index_fh(struct ovl_fs *ofs, struct ovl_fh *fh) 643 { 644 struct dentry *index; 645 struct qstr name; 646 int err; 647 648 err = ovl_get_index_name_fh(fh, &name); 649 if (err) 650 return ERR_PTR(err); 651 652 index = lookup_positive_unlocked(name.name, ofs->indexdir, name.len); 653 kfree(name.name); 654 if (IS_ERR(index)) { 655 if (PTR_ERR(index) == -ENOENT) 656 index = NULL; 657 return index; 658 } 659 660 if (ovl_is_whiteout(index)) 661 err = -ESTALE; 662 else if (ovl_dentry_weird(index)) 663 err = -EIO; 664 else 665 return index; 666 667 dput(index); 668 return ERR_PTR(err); 669 } 670 671 struct dentry *ovl_lookup_index(struct ovl_fs *ofs, struct dentry *upper, 672 struct dentry *origin, bool verify) 673 { 674 struct dentry *index; 675 struct inode *inode; 676 struct qstr name; 677 bool is_dir = d_is_dir(origin); 678 int err; 679 680 err = ovl_get_index_name(origin, &name); 681 if (err) 682 return ERR_PTR(err); 683 684 index = lookup_positive_unlocked(name.name, ofs->indexdir, name.len); 685 if (IS_ERR(index)) { 686 err = PTR_ERR(index); 687 if (err == -ENOENT) { 688 index = NULL; 689 goto out; 690 } 691 pr_warn_ratelimited("overlayfs: failed inode index lookup (ino=%lu, key=%.*s, err=%i);\n" 692 "overlayfs: mount with '-o index=off' to disable inodes index.\n", 693 d_inode(origin)->i_ino, name.len, name.name, 694 err); 695 goto out; 696 } 697 698 inode = d_inode(index); 699 if (ovl_is_whiteout(index) && !verify) { 700 /* 701 * When index lookup is called with !verify for decoding an 702 * overlay file handle, a whiteout index implies that decode 703 * should treat file handle as stale and no need to print a 704 * warning about it. 705 */ 706 dput(index); 707 index = ERR_PTR(-ESTALE); 708 goto out; 709 } else if (ovl_dentry_weird(index) || ovl_is_whiteout(index) || 710 ((inode->i_mode ^ d_inode(origin)->i_mode) & S_IFMT)) { 711 /* 712 * Index should always be of the same file type as origin 713 * except for the case of a whiteout index. A whiteout 714 * index should only exist if all lower aliases have been 715 * unlinked, which means that finding a lower origin on lookup 716 * whose index is a whiteout should be treated as an error. 717 */ 718 pr_warn_ratelimited("overlayfs: bad index found (index=%pd2, ftype=%x, origin ftype=%x).\n", 719 index, d_inode(index)->i_mode & S_IFMT, 720 d_inode(origin)->i_mode & S_IFMT); 721 goto fail; 722 } else if (is_dir && verify) { 723 if (!upper) { 724 pr_warn_ratelimited("overlayfs: suspected uncovered redirected dir found (origin=%pd2, index=%pd2).\n", 725 origin, index); 726 goto fail; 727 } 728 729 /* Verify that dir index 'upper' xattr points to upper dir */ 730 err = ovl_verify_upper(index, upper, false); 731 if (err) { 732 if (err == -ESTALE) { 733 pr_warn_ratelimited("overlayfs: suspected multiply redirected dir found (upper=%pd2, origin=%pd2, index=%pd2).\n", 734 upper, origin, index); 735 } 736 goto fail; 737 } 738 } else if (upper && d_inode(upper) != inode) { 739 goto out_dput; 740 } 741 out: 742 kfree(name.name); 743 return index; 744 745 out_dput: 746 dput(index); 747 index = NULL; 748 goto out; 749 750 fail: 751 dput(index); 752 index = ERR_PTR(-EIO); 753 goto out; 754 } 755 756 /* 757 * Returns next layer in stack starting from top. 758 * Returns -1 if this is the last layer. 759 */ 760 int ovl_path_next(int idx, struct dentry *dentry, struct path *path) 761 { 762 struct ovl_entry *oe = dentry->d_fsdata; 763 764 BUG_ON(idx < 0); 765 if (idx == 0) { 766 ovl_path_upper(dentry, path); 767 if (path->dentry) 768 return oe->numlower ? 1 : -1; 769 idx++; 770 } 771 BUG_ON(idx > oe->numlower); 772 path->dentry = oe->lowerstack[idx - 1].dentry; 773 path->mnt = oe->lowerstack[idx - 1].layer->mnt; 774 775 return (idx < oe->numlower) ? idx + 1 : -1; 776 } 777 778 /* Fix missing 'origin' xattr */ 779 static int ovl_fix_origin(struct dentry *dentry, struct dentry *lower, 780 struct dentry *upper) 781 { 782 int err; 783 784 if (ovl_check_origin_xattr(upper)) 785 return 0; 786 787 err = ovl_want_write(dentry); 788 if (err) 789 return err; 790 791 err = ovl_set_origin(dentry, lower, upper); 792 if (!err) 793 err = ovl_set_impure(dentry->d_parent, upper->d_parent); 794 795 ovl_drop_write(dentry); 796 return err; 797 } 798 799 struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, 800 unsigned int flags) 801 { 802 struct ovl_entry *oe; 803 const struct cred *old_cred; 804 struct ovl_fs *ofs = dentry->d_sb->s_fs_info; 805 struct ovl_entry *poe = dentry->d_parent->d_fsdata; 806 struct ovl_entry *roe = dentry->d_sb->s_root->d_fsdata; 807 struct ovl_path *stack = NULL, *origin_path = NULL; 808 struct dentry *upperdir, *upperdentry = NULL; 809 struct dentry *origin = NULL; 810 struct dentry *index = NULL; 811 unsigned int ctr = 0; 812 struct inode *inode = NULL; 813 bool upperopaque = false; 814 char *upperredirect = NULL; 815 struct dentry *this; 816 unsigned int i; 817 int err; 818 bool metacopy = false; 819 struct ovl_lookup_data d = { 820 .sb = dentry->d_sb, 821 .name = dentry->d_name, 822 .is_dir = false, 823 .opaque = false, 824 .stop = false, 825 .last = ofs->config.redirect_follow ? false : !poe->numlower, 826 .redirect = NULL, 827 .metacopy = false, 828 }; 829 830 if (dentry->d_name.len > ofs->namelen) 831 return ERR_PTR(-ENAMETOOLONG); 832 833 old_cred = ovl_override_creds(dentry->d_sb); 834 upperdir = ovl_dentry_upper(dentry->d_parent); 835 if (upperdir) { 836 err = ovl_lookup_layer(upperdir, &d, &upperdentry); 837 if (err) 838 goto out; 839 840 if (upperdentry && unlikely(ovl_dentry_remote(upperdentry))) { 841 dput(upperdentry); 842 err = -EREMOTE; 843 goto out; 844 } 845 if (upperdentry && !d.is_dir) { 846 unsigned int origin_ctr = 0; 847 848 /* 849 * Lookup copy up origin by decoding origin file handle. 850 * We may get a disconnected dentry, which is fine, 851 * because we only need to hold the origin inode in 852 * cache and use its inode number. We may even get a 853 * connected dentry, that is not under any of the lower 854 * layers root. That is also fine for using it's inode 855 * number - it's the same as if we held a reference 856 * to a dentry in lower layer that was moved under us. 857 */ 858 err = ovl_check_origin(ofs, upperdentry, &origin_path, 859 &origin_ctr); 860 if (err) 861 goto out_put_upper; 862 863 if (d.metacopy) 864 metacopy = true; 865 } 866 867 if (d.redirect) { 868 err = -ENOMEM; 869 upperredirect = kstrdup(d.redirect, GFP_KERNEL); 870 if (!upperredirect) 871 goto out_put_upper; 872 if (d.redirect[0] == '/') 873 poe = roe; 874 } 875 upperopaque = d.opaque; 876 } 877 878 if (!d.stop && poe->numlower) { 879 err = -ENOMEM; 880 stack = kcalloc(ofs->numlower, sizeof(struct ovl_path), 881 GFP_KERNEL); 882 if (!stack) 883 goto out_put_upper; 884 } 885 886 for (i = 0; !d.stop && i < poe->numlower; i++) { 887 struct ovl_path lower = poe->lowerstack[i]; 888 889 if (!ofs->config.redirect_follow) 890 d.last = i == poe->numlower - 1; 891 else 892 d.last = lower.layer->idx == roe->numlower; 893 894 err = ovl_lookup_layer(lower.dentry, &d, &this); 895 if (err) 896 goto out_put; 897 898 if (!this) 899 continue; 900 901 /* 902 * If no origin fh is stored in upper of a merge dir, store fh 903 * of lower dir and set upper parent "impure". 904 */ 905 if (upperdentry && !ctr && !ofs->noxattr && d.is_dir) { 906 err = ovl_fix_origin(dentry, this, upperdentry); 907 if (err) { 908 dput(this); 909 goto out_put; 910 } 911 } 912 913 /* 914 * When "verify_lower" feature is enabled, do not merge with a 915 * lower dir that does not match a stored origin xattr. In any 916 * case, only verified origin is used for index lookup. 917 * 918 * For non-dir dentry, if index=on, then ensure origin 919 * matches the dentry found using path based lookup, 920 * otherwise error out. 921 */ 922 if (upperdentry && !ctr && 923 ((d.is_dir && ovl_verify_lower(dentry->d_sb)) || 924 (!d.is_dir && ofs->config.index && origin_path))) { 925 err = ovl_verify_origin(upperdentry, this, false); 926 if (err) { 927 dput(this); 928 if (d.is_dir) 929 break; 930 goto out_put; 931 } 932 origin = this; 933 } 934 935 if (d.metacopy) 936 metacopy = true; 937 /* 938 * Do not store intermediate metacopy dentries in chain, 939 * except top most lower metacopy dentry 940 */ 941 if (d.metacopy && ctr) { 942 dput(this); 943 continue; 944 } 945 946 stack[ctr].dentry = this; 947 stack[ctr].layer = lower.layer; 948 ctr++; 949 950 /* 951 * Following redirects can have security consequences: it's like 952 * a symlink into the lower layer without the permission checks. 953 * This is only a problem if the upper layer is untrusted (e.g 954 * comes from an USB drive). This can allow a non-readable file 955 * or directory to become readable. 956 * 957 * Only following redirects when redirects are enabled disables 958 * this attack vector when not necessary. 959 */ 960 err = -EPERM; 961 if (d.redirect && !ofs->config.redirect_follow) { 962 pr_warn_ratelimited("overlayfs: refusing to follow redirect for (%pd2)\n", 963 dentry); 964 goto out_put; 965 } 966 967 if (d.stop) 968 break; 969 970 if (d.redirect && d.redirect[0] == '/' && poe != roe) { 971 poe = roe; 972 /* Find the current layer on the root dentry */ 973 i = lower.layer->idx - 1; 974 } 975 } 976 977 if (metacopy) { 978 /* 979 * Found a metacopy dentry but did not find corresponding 980 * data dentry 981 */ 982 if (d.metacopy) { 983 err = -EIO; 984 goto out_put; 985 } 986 987 err = -EPERM; 988 if (!ofs->config.metacopy) { 989 pr_warn_ratelimited("overlay: refusing to follow metacopy origin for (%pd2)\n", 990 dentry); 991 goto out_put; 992 } 993 } else if (!d.is_dir && upperdentry && !ctr && origin_path) { 994 if (WARN_ON(stack != NULL)) { 995 err = -EIO; 996 goto out_put; 997 } 998 stack = origin_path; 999 ctr = 1; 1000 origin_path = NULL; 1001 } 1002 1003 /* 1004 * Lookup index by lower inode and verify it matches upper inode. 1005 * We only trust dir index if we verified that lower dir matches 1006 * origin, otherwise dir index entries may be inconsistent and we 1007 * ignore them. 1008 * 1009 * For non-dir upper metacopy dentry, we already set "origin" if we 1010 * verified that lower matched upper origin. If upper origin was 1011 * not present (because lower layer did not support fh encode/decode), 1012 * or indexing is not enabled, do not set "origin" and skip looking up 1013 * index. This case should be handled in same way as a non-dir upper 1014 * without ORIGIN is handled. 1015 * 1016 * Always lookup index of non-dir non-metacopy and non-upper. 1017 */ 1018 if (ctr && (!upperdentry || (!d.is_dir && !metacopy))) 1019 origin = stack[0].dentry; 1020 1021 if (origin && ovl_indexdir(dentry->d_sb) && 1022 (!d.is_dir || ovl_index_all(dentry->d_sb))) { 1023 index = ovl_lookup_index(ofs, upperdentry, origin, true); 1024 if (IS_ERR(index)) { 1025 err = PTR_ERR(index); 1026 index = NULL; 1027 goto out_put; 1028 } 1029 } 1030 1031 oe = ovl_alloc_entry(ctr); 1032 err = -ENOMEM; 1033 if (!oe) 1034 goto out_put; 1035 1036 memcpy(oe->lowerstack, stack, sizeof(struct ovl_path) * ctr); 1037 dentry->d_fsdata = oe; 1038 1039 if (upperopaque) 1040 ovl_dentry_set_opaque(dentry); 1041 1042 if (upperdentry) 1043 ovl_dentry_set_upper_alias(dentry); 1044 else if (index) { 1045 upperdentry = dget(index); 1046 upperredirect = ovl_get_redirect_xattr(upperdentry, 0); 1047 if (IS_ERR(upperredirect)) { 1048 err = PTR_ERR(upperredirect); 1049 upperredirect = NULL; 1050 goto out_free_oe; 1051 } 1052 } 1053 1054 if (upperdentry || ctr) { 1055 struct ovl_inode_params oip = { 1056 .upperdentry = upperdentry, 1057 .lowerpath = stack, 1058 .index = index, 1059 .numlower = ctr, 1060 .redirect = upperredirect, 1061 .lowerdata = (ctr > 1 && !d.is_dir) ? 1062 stack[ctr - 1].dentry : NULL, 1063 }; 1064 1065 inode = ovl_get_inode(dentry->d_sb, &oip); 1066 err = PTR_ERR(inode); 1067 if (IS_ERR(inode)) 1068 goto out_free_oe; 1069 } 1070 1071 revert_creds(old_cred); 1072 if (origin_path) { 1073 dput(origin_path->dentry); 1074 kfree(origin_path); 1075 } 1076 dput(index); 1077 kfree(stack); 1078 kfree(d.redirect); 1079 return d_splice_alias(inode, dentry); 1080 1081 out_free_oe: 1082 dentry->d_fsdata = NULL; 1083 kfree(oe); 1084 out_put: 1085 dput(index); 1086 for (i = 0; i < ctr; i++) 1087 dput(stack[i].dentry); 1088 kfree(stack); 1089 out_put_upper: 1090 if (origin_path) { 1091 dput(origin_path->dentry); 1092 kfree(origin_path); 1093 } 1094 dput(upperdentry); 1095 kfree(upperredirect); 1096 out: 1097 kfree(d.redirect); 1098 revert_creds(old_cred); 1099 return ERR_PTR(err); 1100 } 1101 1102 bool ovl_lower_positive(struct dentry *dentry) 1103 { 1104 struct ovl_entry *poe = dentry->d_parent->d_fsdata; 1105 const struct qstr *name = &dentry->d_name; 1106 const struct cred *old_cred; 1107 unsigned int i; 1108 bool positive = false; 1109 bool done = false; 1110 1111 /* 1112 * If dentry is negative, then lower is positive iff this is a 1113 * whiteout. 1114 */ 1115 if (!dentry->d_inode) 1116 return ovl_dentry_is_opaque(dentry); 1117 1118 /* Negative upper -> positive lower */ 1119 if (!ovl_dentry_upper(dentry)) 1120 return true; 1121 1122 old_cred = ovl_override_creds(dentry->d_sb); 1123 /* Positive upper -> have to look up lower to see whether it exists */ 1124 for (i = 0; !done && !positive && i < poe->numlower; i++) { 1125 struct dentry *this; 1126 struct dentry *lowerdir = poe->lowerstack[i].dentry; 1127 1128 this = lookup_positive_unlocked(name->name, lowerdir, 1129 name->len); 1130 if (IS_ERR(this)) { 1131 switch (PTR_ERR(this)) { 1132 case -ENOENT: 1133 case -ENAMETOOLONG: 1134 break; 1135 1136 default: 1137 /* 1138 * Assume something is there, we just couldn't 1139 * access it. 1140 */ 1141 positive = true; 1142 break; 1143 } 1144 } else { 1145 positive = !ovl_is_whiteout(this); 1146 done = true; 1147 dput(this); 1148 } 1149 } 1150 revert_creds(old_cred); 1151 1152 return positive; 1153 } 1154