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/hex.h> 11 #include <linux/namei.h> 12 #include <linux/xattr.h> 13 #include <linux/ratelimit.h> 14 #include <linux/mount.h> 15 #include <linux/exportfs.h> 16 #include "overlayfs.h" 17 18 struct ovl_lookup_data { 19 struct super_block *sb; 20 struct dentry *dentry; 21 const struct ovl_layer *layer; 22 struct qstr name; 23 bool is_dir; 24 bool opaque; 25 bool xwhiteouts; 26 bool stop; 27 bool last; 28 char *redirect; 29 char *upperredirect; 30 int metacopy; 31 /* Referring to last redirect xattr */ 32 bool absolute_redirect; 33 }; 34 35 static int ovl_check_redirect(const struct path *path, struct ovl_lookup_data *d, 36 size_t prelen, const char *post) 37 { 38 int res; 39 char *buf; 40 struct ovl_fs *ofs = OVL_FS(d->sb); 41 42 d->absolute_redirect = false; 43 buf = ovl_get_redirect_xattr(ofs, path, prelen + strlen(post)); 44 if (IS_ERR_OR_NULL(buf)) 45 return PTR_ERR(buf); 46 47 if (buf[0] == '/') { 48 d->absolute_redirect = true; 49 /* 50 * One of the ancestor path elements in an absolute path 51 * lookup in ovl_lookup_layer() could have been opaque and 52 * that will stop further lookup in lower layers (d->stop=true) 53 * But we have found an absolute redirect in descendant path 54 * element and that should force continue lookup in lower 55 * layers (reset d->stop). 56 */ 57 d->stop = false; 58 } else { 59 res = strlen(buf) + 1; 60 memmove(buf + prelen, buf, res); 61 memcpy(buf, d->name.name, prelen); 62 } 63 64 strcat(buf, post); 65 kfree(d->redirect); 66 d->redirect = buf; 67 d->name.name = d->redirect; 68 d->name.len = strlen(d->redirect); 69 70 return 0; 71 } 72 73 static int ovl_acceptable(void *ctx, struct dentry *dentry) 74 { 75 /* 76 * A non-dir origin may be disconnected, which is fine, because 77 * we only need it for its unique inode number. 78 */ 79 if (!d_is_dir(dentry)) 80 return 1; 81 82 /* Don't decode a deleted empty directory */ 83 if (d_unhashed(dentry)) 84 return 0; 85 86 /* Check if directory belongs to the layer we are decoding from */ 87 return is_subdir(dentry, ((struct vfsmount *)ctx)->mnt_root); 88 } 89 90 /* 91 * Check validity of an overlay file handle buffer. 92 * 93 * Return 0 for a valid file handle. 94 * Return -ENODATA for "origin unknown". 95 * Return <0 for an invalid file handle. 96 */ 97 int ovl_check_fb_len(struct ovl_fb *fb, int fb_len) 98 { 99 if (fb_len < sizeof(struct ovl_fb) || fb_len < fb->len) 100 return -EINVAL; 101 102 if (fb->magic != OVL_FH_MAGIC) 103 return -EINVAL; 104 105 /* Treat larger version and unknown flags as "origin unknown" */ 106 if (fb->version > OVL_FH_VERSION || fb->flags & ~OVL_FH_FLAG_ALL) 107 return -ENODATA; 108 109 /* Treat endianness mismatch as "origin unknown" */ 110 if (!(fb->flags & OVL_FH_FLAG_ANY_ENDIAN) && 111 (fb->flags & OVL_FH_FLAG_BIG_ENDIAN) != OVL_FH_FLAG_CPU_ENDIAN) 112 return -ENODATA; 113 114 return 0; 115 } 116 117 static struct ovl_fh *ovl_get_fh(struct ovl_fs *ofs, struct dentry *upperdentry, 118 enum ovl_xattr ox) 119 { 120 int res, err; 121 struct ovl_fh *fh = NULL; 122 123 res = ovl_getxattr_upper(ofs, upperdentry, ox, NULL, 0); 124 if (res < 0) { 125 if (res == -ENODATA || res == -EOPNOTSUPP) 126 return NULL; 127 goto fail; 128 } 129 /* Zero size value means "copied up but origin unknown" */ 130 if (res == 0) 131 return NULL; 132 133 fh = kzalloc(res + OVL_FH_WIRE_OFFSET, GFP_KERNEL); 134 if (!fh) 135 return ERR_PTR(-ENOMEM); 136 137 res = ovl_getxattr_upper(ofs, upperdentry, ox, fh->buf, res); 138 if (res < 0) 139 goto fail; 140 141 err = ovl_check_fb_len(&fh->fb, res); 142 if (err < 0) { 143 if (err == -ENODATA) 144 goto out; 145 goto invalid; 146 } 147 148 return fh; 149 150 out: 151 kfree(fh); 152 return NULL; 153 154 fail: 155 pr_warn_ratelimited("failed to get origin (%i)\n", res); 156 goto out; 157 invalid: 158 pr_warn_ratelimited("invalid origin (%*phN)\n", res, fh); 159 goto out; 160 } 161 162 bool ovl_uuid_match(struct ovl_fs *ofs, const struct super_block *sb, 163 const uuid_t *uuid) 164 { 165 /* 166 * Make sure that the stored uuid matches the uuid of the lower 167 * layer where file handle will be decoded. 168 * In case of uuid=off option just make sure that stored uuid is null. 169 */ 170 return ovl_origin_uuid(ofs) ? uuid_equal(uuid, &sb->s_uuid) : 171 uuid_is_null(uuid); 172 } 173 174 struct dentry *ovl_decode_real_fh(struct ovl_fs *ofs, struct ovl_fh *fh, 175 struct vfsmount *mnt, bool connected) 176 { 177 struct dentry *real; 178 int bytes; 179 180 if (!capable(CAP_DAC_READ_SEARCH)) 181 return NULL; 182 183 if (!ovl_uuid_match(ofs, mnt->mnt_sb, &fh->fb.uuid)) 184 return NULL; 185 186 bytes = (fh->fb.len - offsetof(struct ovl_fb, fid)); 187 real = exportfs_decode_fh(mnt, (struct fid *)fh->fb.fid, 188 bytes >> 2, (int)fh->fb.type, 189 connected ? ovl_acceptable : NULL, mnt); 190 if (IS_ERR(real)) { 191 /* 192 * Treat stale file handle to lower file as "origin unknown". 193 * upper file handle could become stale when upper file is 194 * unlinked and this information is needed to handle stale 195 * index entries correctly. 196 */ 197 if (real == ERR_PTR(-ESTALE) && 198 !(fh->fb.flags & OVL_FH_FLAG_PATH_UPPER)) 199 real = NULL; 200 return real; 201 } 202 203 if (ovl_dentry_weird(real)) { 204 dput(real); 205 return NULL; 206 } 207 208 return real; 209 } 210 211 static struct dentry *ovl_lookup_positive_unlocked(struct ovl_lookup_data *d, 212 const char *name, 213 struct dentry *base, int len, 214 bool drop_negative) 215 { 216 struct dentry *ret = lookup_one_unlocked(mnt_idmap(d->layer->mnt), 217 &QSTR_LEN(name, len), base); 218 219 if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) { 220 if (drop_negative && ret->d_lockref.count == 1) { 221 spin_lock(&ret->d_lock); 222 /* Recheck condition under lock */ 223 if (d_is_negative(ret) && ret->d_lockref.count == 1) 224 __d_drop(ret); 225 spin_unlock(&ret->d_lock); 226 } 227 dput(ret); 228 ret = ERR_PTR(-ENOENT); 229 } 230 return ret; 231 } 232 233 static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d, 234 const char *name, unsigned int namelen, 235 size_t prelen, const char *post, 236 struct dentry **ret, bool drop_negative) 237 { 238 struct ovl_fs *ofs = OVL_FS(d->sb); 239 struct dentry *this = NULL; 240 const char *warn; 241 struct path path; 242 int err; 243 bool last_element = !post[0]; 244 bool is_upper = d->layer->idx == 0; 245 char val; 246 247 /* 248 * We allow filesystems that are case-folding capable as long as the 249 * layers are consistently enabled in the stack, enabled for every dir 250 * or disabled in all dirs. If someone has modified case folding on a 251 * directory on underlying layer, the warranty of the ovl stack is 252 * voided. 253 */ 254 if (ofs->casefold != ovl_dentry_casefolded(base)) { 255 warn = "parent wrong casefold"; 256 err = -ESTALE; 257 goto out_warn; 258 } 259 260 this = ovl_lookup_positive_unlocked(d, name, base, namelen, drop_negative); 261 if (IS_ERR(this)) { 262 err = PTR_ERR(this); 263 this = NULL; 264 if (err == -ENOENT || err == -ENAMETOOLONG) 265 goto out; 266 goto out_err; 267 } 268 269 /* Filesystems set the casefold flag only on directories */ 270 if (d_is_dir(this) && ofs->casefold != ovl_dentry_casefolded(this)) { 271 warn = "child wrong casefold"; 272 err = -EREMOTE; 273 goto out_warn; 274 } 275 276 if (ovl_dentry_weird(this)) { 277 /* Don't support traversing automounts and other weirdness */ 278 warn = "unsupported object type"; 279 err = -EREMOTE; 280 goto out_warn; 281 } 282 283 path.dentry = this; 284 path.mnt = d->layer->mnt; 285 if (ovl_path_is_whiteout(ofs, &path)) { 286 d->stop = d->opaque = true; 287 goto put_and_out; 288 } 289 /* 290 * This dentry should be a regular file if previous layer lookup 291 * found a metacopy dentry. 292 */ 293 if (last_element && d->metacopy && !d_is_reg(this)) { 294 d->stop = true; 295 goto put_and_out; 296 } 297 298 if (!d_can_lookup(this)) { 299 if (d->is_dir || !last_element) { 300 d->stop = true; 301 goto put_and_out; 302 } 303 err = ovl_check_metacopy_xattr(ofs, &path, NULL); 304 if (err < 0) 305 goto out_err; 306 307 d->metacopy = err; 308 d->stop = !d->metacopy; 309 if (!d->metacopy || d->last) 310 goto out; 311 } else { 312 if (ovl_lookup_trap_inode(d->sb, this)) { 313 /* Caught in a trap of overlapping layers */ 314 warn = "overlapping layers"; 315 err = -ELOOP; 316 goto out_warn; 317 } 318 319 if (last_element) 320 d->is_dir = true; 321 if (d->last) 322 goto out; 323 324 /* overlay.opaque=x means xwhiteouts directory */ 325 val = ovl_get_opaquedir_val(ofs, &path); 326 if (last_element && !is_upper && val == 'x') { 327 d->xwhiteouts = true; 328 ovl_layer_set_xwhiteouts(ofs, d->layer); 329 } else if (val == 'y') { 330 d->stop = true; 331 if (last_element) 332 d->opaque = true; 333 goto out; 334 } 335 } 336 err = ovl_check_redirect(&path, d, prelen, post); 337 if (err) 338 goto out_err; 339 out: 340 *ret = this; 341 return 0; 342 343 put_and_out: 344 dput(this); 345 this = NULL; 346 goto out; 347 348 out_warn: 349 pr_warn_ratelimited("failed lookup in %s (%pd2, name='%.*s', err=%i): %s\n", 350 is_upper ? "upper" : "lower", base, 351 namelen, name, err, warn); 352 out_err: 353 dput(this); 354 return err; 355 } 356 357 static int ovl_lookup_layer(struct dentry *base, struct ovl_lookup_data *d, 358 struct dentry **ret, bool drop_negative) 359 { 360 /* Counting down from the end, since the prefix can change */ 361 size_t rem = d->name.len - 1; 362 struct dentry *dentry = NULL; 363 int err; 364 365 if (d->name.name[0] != '/') 366 return ovl_lookup_single(base, d, d->name.name, d->name.len, 367 0, "", ret, drop_negative); 368 369 while (!IS_ERR_OR_NULL(base) && d_can_lookup(base)) { 370 const char *s = d->name.name + d->name.len - rem; 371 const char *next = strchrnul(s, '/'); 372 size_t thislen = next - s; 373 bool end = !next[0]; 374 375 /* Verify we did not go off the rails */ 376 if (WARN_ON(s[-1] != '/')) 377 return -EIO; 378 379 err = ovl_lookup_single(base, d, s, thislen, 380 d->name.len - rem, next, &base, 381 drop_negative); 382 dput(dentry); 383 if (err) 384 return err; 385 dentry = base; 386 if (end) 387 break; 388 389 rem -= thislen + 1; 390 391 if (WARN_ON(rem >= d->name.len)) 392 return -EIO; 393 } 394 *ret = dentry; 395 return 0; 396 } 397 398 static int ovl_lookup_data_layer(struct dentry *dentry, const char *redirect, 399 const struct ovl_layer *layer, 400 struct path *datapath) 401 { 402 int err; 403 404 err = vfs_path_lookup(layer->mnt->mnt_root, layer->mnt, redirect, 405 LOOKUP_BENEATH | LOOKUP_NO_SYMLINKS | LOOKUP_NO_XDEV, 406 datapath); 407 pr_debug("lookup lowerdata (%pd2, redirect=\"%s\", layer=%d, err=%i)\n", 408 dentry, redirect, layer->idx, err); 409 410 if (err) 411 return err; 412 413 err = -EREMOTE; 414 if (ovl_dentry_weird(datapath->dentry)) 415 goto out_path_put; 416 417 err = -ENOENT; 418 /* Only regular file is acceptable as lower data */ 419 if (!d_is_reg(datapath->dentry)) 420 goto out_path_put; 421 422 return 0; 423 424 out_path_put: 425 path_put(datapath); 426 427 return err; 428 } 429 430 /* Lookup in data-only layers by absolute redirect to layer root */ 431 static int ovl_lookup_data_layers(struct dentry *dentry, const char *redirect, 432 struct ovl_path *lowerdata) 433 { 434 struct ovl_fs *ofs = OVL_FS(dentry->d_sb); 435 const struct ovl_layer *layer; 436 struct path datapath; 437 int err = -ENOENT; 438 int i; 439 440 layer = &ofs->layers[ofs->numlayer - ofs->numdatalayer]; 441 for (i = 0; i < ofs->numdatalayer; i++, layer++) { 442 err = ovl_lookup_data_layer(dentry, redirect, layer, &datapath); 443 if (!err) { 444 mntput(datapath.mnt); 445 lowerdata->dentry = datapath.dentry; 446 lowerdata->layer = layer; 447 return 0; 448 } 449 } 450 451 return err; 452 } 453 454 int ovl_check_origin_fh(struct ovl_fs *ofs, struct ovl_fh *fh, bool connected, 455 struct dentry *upperdentry, struct ovl_path **stackp) 456 { 457 struct dentry *origin = NULL; 458 int i; 459 460 for (i = 1; i <= ovl_numlowerlayer(ofs); i++) { 461 /* 462 * If lower fs uuid is not unique among lower fs we cannot match 463 * fh->uuid to layer. 464 */ 465 if (ofs->layers[i].fsid && 466 ofs->layers[i].fs->bad_uuid) 467 continue; 468 469 origin = ovl_decode_real_fh(ofs, fh, ofs->layers[i].mnt, 470 connected); 471 if (origin) 472 break; 473 } 474 475 if (!origin) 476 return -ESTALE; 477 else if (IS_ERR(origin)) 478 return PTR_ERR(origin); 479 480 if (upperdentry && !ovl_upper_is_whiteout(ofs, upperdentry) && 481 inode_wrong_type(d_inode(upperdentry), d_inode(origin)->i_mode)) 482 goto invalid; 483 484 if (!*stackp) 485 *stackp = kmalloc_obj(struct ovl_path); 486 if (!*stackp) { 487 dput(origin); 488 return -ENOMEM; 489 } 490 **stackp = (struct ovl_path){ 491 .dentry = origin, 492 .layer = &ofs->layers[i] 493 }; 494 495 return 0; 496 497 invalid: 498 pr_warn_ratelimited("invalid origin (%pd2, ftype=%x, origin ftype=%x).\n", 499 upperdentry, d_inode(upperdentry)->i_mode & S_IFMT, 500 d_inode(origin)->i_mode & S_IFMT); 501 dput(origin); 502 return -ESTALE; 503 } 504 505 static int ovl_check_origin(struct ovl_fs *ofs, struct dentry *upperdentry, 506 struct ovl_path **stackp) 507 { 508 struct ovl_fh *fh = ovl_get_fh(ofs, upperdentry, OVL_XATTR_ORIGIN); 509 int err; 510 511 if (IS_ERR_OR_NULL(fh)) 512 return PTR_ERR(fh); 513 514 err = ovl_check_origin_fh(ofs, fh, false, upperdentry, stackp); 515 kfree(fh); 516 517 if (err) { 518 if (err == -ESTALE) 519 return 0; 520 return err; 521 } 522 523 return 0; 524 } 525 526 /* 527 * Verify that @fh matches the file handle stored in xattr @name. 528 * Return 0 on match, -ESTALE on mismatch, < 0 on error. 529 */ 530 static int ovl_verify_fh(struct ovl_fs *ofs, struct dentry *dentry, 531 enum ovl_xattr ox, const struct ovl_fh *fh) 532 { 533 struct ovl_fh *ofh = ovl_get_fh(ofs, dentry, ox); 534 int err = 0; 535 536 if (!ofh) 537 return -ENODATA; 538 539 if (IS_ERR(ofh)) 540 return PTR_ERR(ofh); 541 542 if (fh->fb.len != ofh->fb.len || memcmp(&fh->fb, &ofh->fb, fh->fb.len)) 543 err = -ESTALE; 544 545 kfree(ofh); 546 return err; 547 } 548 549 int ovl_verify_set_fh(struct ovl_fs *ofs, struct dentry *dentry, 550 enum ovl_xattr ox, const struct ovl_fh *fh, 551 bool is_upper, bool set) 552 { 553 int err; 554 555 err = ovl_verify_fh(ofs, dentry, ox, fh); 556 if (set && err == -ENODATA) 557 err = ovl_setxattr(ofs, dentry, ox, fh->buf, fh->fb.len); 558 559 return err; 560 } 561 562 /* 563 * Verify that @real dentry matches the file handle stored in xattr @name. 564 * 565 * If @set is true and there is no stored file handle, encode @real and store 566 * file handle in xattr @name. 567 * 568 * Return 0 on match, -ESTALE on mismatch, -ENODATA on no xattr, < 0 on error. 569 */ 570 int ovl_verify_origin_xattr(struct ovl_fs *ofs, struct dentry *dentry, 571 enum ovl_xattr ox, struct dentry *real, 572 bool is_upper, bool set) 573 { 574 struct inode *inode; 575 struct ovl_fh *fh; 576 int err; 577 578 fh = ovl_encode_real_fh(ofs, d_inode(real), is_upper); 579 err = PTR_ERR(fh); 580 if (IS_ERR(fh)) { 581 fh = NULL; 582 goto fail; 583 } 584 585 err = ovl_verify_set_fh(ofs, dentry, ox, fh, is_upper, set); 586 if (err) 587 goto fail; 588 589 out: 590 kfree(fh); 591 return err; 592 593 fail: 594 inode = d_inode(real); 595 pr_warn_ratelimited("failed to verify %s (%pd2, ino=%llu, err=%i)\n", 596 is_upper ? "upper" : "origin", real, 597 inode ? inode->i_ino : 0, err); 598 goto out; 599 } 600 601 602 /* Get upper dentry from index */ 603 struct dentry *ovl_index_upper(struct ovl_fs *ofs, struct dentry *index, 604 bool connected) 605 { 606 struct ovl_fh *fh; 607 struct dentry *upper; 608 609 if (!d_is_dir(index)) 610 return dget(index); 611 612 fh = ovl_get_fh(ofs, index, OVL_XATTR_UPPER); 613 if (IS_ERR_OR_NULL(fh)) 614 return ERR_CAST(fh); 615 616 upper = ovl_decode_real_fh(ofs, fh, ovl_upper_mnt(ofs), connected); 617 kfree(fh); 618 619 if (IS_ERR_OR_NULL(upper)) 620 return upper ?: ERR_PTR(-ESTALE); 621 622 if (!d_is_dir(upper)) { 623 pr_warn_ratelimited("invalid index upper (%pd2, upper=%pd2).\n", 624 index, upper); 625 dput(upper); 626 return ERR_PTR(-EIO); 627 } 628 629 return upper; 630 } 631 632 /* 633 * Verify that an index entry name matches the origin file handle stored in 634 * OVL_XATTR_ORIGIN and that origin file handle can be decoded to lower path. 635 * Return 0 on match, -ESTALE on mismatch or stale origin, < 0 on error. 636 */ 637 int ovl_verify_index(struct ovl_fs *ofs, struct dentry *index) 638 { 639 struct ovl_fh *fh = NULL; 640 size_t len; 641 struct ovl_path origin = { }; 642 struct ovl_path *stack = &origin; 643 struct dentry *upper = NULL; 644 int err; 645 646 if (!d_inode(index)) 647 return 0; 648 649 err = -EINVAL; 650 if (index->d_name.len < sizeof(struct ovl_fb)*2) 651 goto fail; 652 653 err = -ENOMEM; 654 len = index->d_name.len / 2; 655 fh = kzalloc(len + OVL_FH_WIRE_OFFSET, GFP_KERNEL); 656 if (!fh) 657 goto fail; 658 659 err = -EINVAL; 660 if (hex2bin(fh->buf, index->d_name.name, len)) 661 goto fail; 662 663 err = ovl_check_fb_len(&fh->fb, len); 664 if (err) 665 goto fail; 666 667 /* 668 * Whiteout index entries are used as an indication that an exported 669 * overlay file handle should be treated as stale (i.e. after unlink 670 * of the overlay inode). These entries contain no origin xattr. 671 */ 672 if (ovl_is_whiteout(index)) 673 goto out; 674 675 /* 676 * Verifying directory index entries are not stale is expensive, so 677 * only verify stale dir index if NFS export is enabled. 678 */ 679 if (d_is_dir(index) && !ofs->config.nfs_export) 680 goto out; 681 682 /* 683 * Directory index entries should have 'upper' xattr pointing to the 684 * real upper dir. Non-dir index entries are hardlinks to the upper 685 * real inode. For non-dir index, we can read the copy up origin xattr 686 * directly from the index dentry, but for dir index we first need to 687 * decode the upper directory. 688 */ 689 upper = ovl_index_upper(ofs, index, false); 690 if (IS_ERR_OR_NULL(upper)) { 691 err = PTR_ERR(upper); 692 /* 693 * Directory index entries with no 'upper' xattr need to be 694 * removed. When dir index entry has a stale 'upper' xattr, 695 * we assume that upper dir was removed and we treat the dir 696 * index as orphan entry that needs to be whited out. 697 */ 698 if (err == -ESTALE) 699 goto orphan; 700 else if (!err) 701 err = -ESTALE; 702 goto fail; 703 } 704 705 err = ovl_verify_fh(ofs, upper, OVL_XATTR_ORIGIN, fh); 706 dput(upper); 707 if (err) 708 goto fail; 709 710 /* Check if non-dir index is orphan and don't warn before cleaning it */ 711 if (!d_is_dir(index) && d_inode(index)->i_nlink == 1) { 712 err = ovl_check_origin_fh(ofs, fh, false, index, &stack); 713 if (err) 714 goto fail; 715 716 if (ovl_get_nlink(ofs, origin.dentry, index, true, 0) == 0) 717 goto orphan; 718 } 719 720 out: 721 dput(origin.dentry); 722 kfree(fh); 723 return err; 724 725 fail: 726 pr_warn_ratelimited("failed to verify index (%pd2, ftype=%x, err=%i)\n", 727 index, d_inode(index)->i_mode & S_IFMT, err); 728 goto out; 729 730 orphan: 731 pr_warn_ratelimited("orphan index entry (%pd2, ftype=%x, nlink=%u)\n", 732 index, d_inode(index)->i_mode & S_IFMT, 733 d_inode(index)->i_nlink); 734 err = -ENOENT; 735 goto out; 736 } 737 738 int ovl_get_index_name_fh(const struct ovl_fh *fh, struct qstr *name) 739 { 740 char *n, *s; 741 742 n = kcalloc(fh->fb.len, 2, GFP_KERNEL); 743 if (!n) 744 return -ENOMEM; 745 746 s = bin2hex(n, fh->buf, fh->fb.len); 747 *name = (struct qstr) QSTR_INIT(n, s - n); 748 749 return 0; 750 751 } 752 753 /* 754 * Lookup in indexdir for the index entry of a lower real inode or a copy up 755 * origin inode. The index entry name is the hex representation of the lower 756 * inode file handle. 757 * 758 * If the index dentry in negative, then either no lower aliases have been 759 * copied up yet, or aliases have been copied up in older kernels and are 760 * not indexed. 761 * 762 * If the index dentry for a copy up origin inode is positive, but points 763 * to an inode different than the upper inode, then either the upper inode 764 * has been copied up and not indexed or it was indexed, but since then 765 * index dir was cleared. Either way, that index cannot be used to identify 766 * the overlay inode. 767 */ 768 int ovl_get_index_name(struct ovl_fs *ofs, struct dentry *origin, 769 struct qstr *name) 770 { 771 struct ovl_fh *fh; 772 int err; 773 774 fh = ovl_encode_real_fh(ofs, d_inode(origin), false); 775 if (IS_ERR(fh)) 776 return PTR_ERR(fh); 777 778 err = ovl_get_index_name_fh(fh, name); 779 780 kfree(fh); 781 return err; 782 } 783 784 /* 785 * Lookup index by decoded origin for NFS export. 786 * 787 * The name of an index is the hex of the file handle that copy up encoded. 788 * The bytes of the handle to decode cannot be used as the name, because a 789 * lower fs may decode many handles to the same inode. For example, it may 790 * ignore the fid type, extra fid bytes or a zero generation. 791 */ 792 struct dentry *ovl_get_index_origin(struct ovl_fs *ofs, struct dentry *origin) 793 { 794 struct dentry *index; 795 struct qstr name; 796 int err; 797 798 err = ovl_get_index_name(ofs, origin, &name); 799 if (err) 800 return ERR_PTR(err); 801 802 index = lookup_noperm_positive_unlocked(&name, ofs->workdir); 803 kfree(name.name); 804 if (IS_ERR(index)) { 805 if (PTR_ERR(index) == -ENOENT) 806 index = NULL; 807 return index; 808 } 809 810 if (ovl_is_whiteout(index)) 811 err = -ESTALE; 812 else if (ovl_dentry_weird(index)) 813 err = -EIO; 814 else 815 return index; 816 817 dput(index); 818 return ERR_PTR(err); 819 } 820 821 struct dentry *ovl_lookup_index(struct ovl_fs *ofs, struct dentry *upper, 822 struct dentry *origin, bool verify) 823 { 824 struct dentry *index; 825 struct inode *inode; 826 struct qstr name; 827 bool is_dir = d_is_dir(origin); 828 int err; 829 830 err = ovl_get_index_name(ofs, origin, &name); 831 if (err) 832 return ERR_PTR(err); 833 834 index = lookup_one_positive_unlocked(ovl_upper_mnt_idmap(ofs), &name, 835 ofs->workdir); 836 if (IS_ERR(index)) { 837 err = PTR_ERR(index); 838 if (err == -ENOENT) { 839 index = NULL; 840 goto out; 841 } 842 pr_warn_ratelimited("failed inode index lookup (ino=%llu, key=%.*s, err=%i);\n" 843 "overlayfs: mount with '-o index=off' to disable inodes index.\n", 844 d_inode(origin)->i_ino, name.len, name.name, 845 err); 846 goto out; 847 } 848 849 inode = d_inode(index); 850 if (ovl_is_whiteout(index) && !verify) { 851 /* 852 * When index lookup is called with !verify for decoding an 853 * overlay file handle, a whiteout index implies that decode 854 * should treat file handle as stale and no need to print a 855 * warning about it. 856 */ 857 dput(index); 858 index = ERR_PTR(-ESTALE); 859 goto out; 860 } else if (ovl_dentry_weird(index) || ovl_is_whiteout(index) || 861 inode_wrong_type(inode, d_inode(origin)->i_mode)) { 862 /* 863 * Index should always be of the same file type as origin 864 * except for the case of a whiteout index. A whiteout 865 * index should only exist if all lower aliases have been 866 * unlinked, which means that finding a lower origin on lookup 867 * whose index is a whiteout should be treated as an error. 868 */ 869 pr_warn_ratelimited("bad index found (index=%pd2, ftype=%x, origin ftype=%x).\n", 870 index, d_inode(index)->i_mode & S_IFMT, 871 d_inode(origin)->i_mode & S_IFMT); 872 goto fail; 873 } else if (is_dir && verify) { 874 if (!upper) { 875 pr_warn_ratelimited("suspected uncovered redirected dir found (origin=%pd2, index=%pd2).\n", 876 origin, index); 877 goto fail; 878 } 879 880 /* Verify that dir index 'upper' xattr points to upper dir */ 881 err = ovl_verify_upper(ofs, index, upper, false); 882 if (err) { 883 if (err == -ESTALE) { 884 pr_warn_ratelimited("suspected multiply redirected dir found (upper=%pd2, origin=%pd2, index=%pd2).\n", 885 upper, origin, index); 886 } 887 goto fail; 888 } 889 } else if (upper && d_inode(upper) != inode) { 890 goto out_dput; 891 } 892 out: 893 kfree(name.name); 894 return index; 895 896 out_dput: 897 dput(index); 898 index = NULL; 899 goto out; 900 901 fail: 902 dput(index); 903 index = ERR_PTR(-EIO); 904 goto out; 905 } 906 907 /* 908 * Returns next layer in stack starting from top. 909 * Returns -1 if this is the last layer. 910 */ 911 int ovl_path_next(int idx, struct dentry *dentry, struct path *path, 912 const struct ovl_layer **layer) 913 { 914 struct ovl_entry *oe = OVL_E(dentry); 915 struct ovl_path *lowerstack = ovl_lowerstack(oe); 916 917 BUG_ON(idx < 0); 918 if (idx == 0) { 919 ovl_path_upper(dentry, path); 920 if (path->dentry) { 921 *layer = &OVL_FS(dentry->d_sb)->layers[0]; 922 return ovl_numlower(oe) ? 1 : -1; 923 } 924 idx++; 925 } 926 BUG_ON(idx > ovl_numlower(oe)); 927 path->dentry = lowerstack[idx - 1].dentry; 928 *layer = lowerstack[idx - 1].layer; 929 path->mnt = (*layer)->mnt; 930 931 return (idx < ovl_numlower(oe)) ? idx + 1 : -1; 932 } 933 934 /* Fix missing 'origin' xattr */ 935 static int ovl_fix_origin(struct ovl_fs *ofs, struct dentry *dentry, 936 struct dentry *lower, struct dentry *upper) 937 { 938 const struct ovl_fh *fh; 939 int err; 940 941 if (ovl_check_origin_xattr(ofs, upper)) 942 return 0; 943 944 fh = ovl_get_origin_fh(ofs, lower); 945 if (IS_ERR(fh)) 946 return PTR_ERR(fh); 947 948 err = ovl_want_write(dentry); 949 if (err) 950 goto out; 951 952 err = ovl_set_origin_fh(ofs, fh, upper); 953 if (!err) { 954 /* upper->d_parent is not stable, use the overlay parent */ 955 err = ovl_set_impure_dir(dentry->d_parent); 956 } 957 958 ovl_drop_write(dentry); 959 out: 960 kfree(fh); 961 return err; 962 } 963 964 static int ovl_maybe_validate_verity(struct dentry *dentry) 965 { 966 struct ovl_fs *ofs = OVL_FS(dentry->d_sb); 967 struct inode *inode = d_inode(dentry); 968 struct path datapath, metapath; 969 int err; 970 971 if (!ofs->config.verity_mode || 972 !ovl_is_metacopy_dentry(dentry) || 973 ovl_test_flag(OVL_VERIFIED_DIGEST, inode)) 974 return 0; 975 976 if (!ovl_test_flag(OVL_HAS_DIGEST, inode)) { 977 if (ofs->config.verity_mode == OVL_VERITY_REQUIRE) { 978 pr_warn_ratelimited("metacopy file '%pd' has no digest specified\n", 979 dentry); 980 return -EIO; 981 } 982 return 0; 983 } 984 985 ovl_path_lowerdata(dentry, &datapath); 986 if (!datapath.dentry) 987 return -EIO; 988 989 ovl_path_real(dentry, &metapath); 990 if (!metapath.dentry) 991 return -EIO; 992 993 err = ovl_inode_lock_interruptible(inode); 994 if (err) 995 return err; 996 997 if (!ovl_test_flag(OVL_VERIFIED_DIGEST, inode)) { 998 with_ovl_creds(dentry->d_sb) 999 err = ovl_validate_verity(ofs, &metapath, &datapath); 1000 if (err == 0) 1001 ovl_set_flag(OVL_VERIFIED_DIGEST, inode); 1002 } 1003 1004 ovl_inode_unlock(inode); 1005 1006 return err; 1007 } 1008 1009 /* Lazy lookup of lowerdata */ 1010 static int ovl_maybe_lookup_lowerdata(struct dentry *dentry) 1011 { 1012 struct inode *inode = d_inode(dentry); 1013 const char *redirect = ovl_lowerdata_redirect(inode); 1014 struct ovl_path datapath = {}; 1015 int err; 1016 1017 if (!redirect || ovl_dentry_lowerdata(dentry)) 1018 return 0; 1019 1020 if (redirect[0] != '/') 1021 return -EIO; 1022 1023 err = ovl_inode_lock_interruptible(inode); 1024 if (err) 1025 return err; 1026 1027 err = 0; 1028 /* Someone got here before us? */ 1029 if (ovl_dentry_lowerdata(dentry)) 1030 goto out; 1031 1032 with_ovl_creds(dentry->d_sb) 1033 err = ovl_lookup_data_layers(dentry, redirect, &datapath); 1034 if (err) 1035 goto out_err; 1036 1037 err = ovl_dentry_set_lowerdata(dentry, &datapath); 1038 if (err) 1039 goto out_err; 1040 1041 out: 1042 ovl_inode_unlock(inode); 1043 dput(datapath.dentry); 1044 1045 return err; 1046 1047 out_err: 1048 pr_warn_ratelimited("lazy lowerdata lookup failed (%pd2, err=%i)\n", 1049 dentry, err); 1050 goto out; 1051 } 1052 1053 int ovl_verify_lowerdata(struct dentry *dentry) 1054 { 1055 int err; 1056 1057 err = ovl_maybe_lookup_lowerdata(dentry); 1058 if (err) 1059 return err; 1060 1061 return ovl_maybe_validate_verity(dentry); 1062 } 1063 1064 /* 1065 * Following redirects/metacopy can have security consequences: it's like a 1066 * symlink into the lower layer without the permission checks. 1067 * 1068 * This is only a problem if the upper layer is untrusted (e.g comes from an USB 1069 * drive). This can allow a non-readable file or directory to become readable. 1070 * 1071 * Only following redirects when redirects are enabled disables this attack 1072 * vector when not necessary. 1073 */ 1074 static bool ovl_check_follow_redirect(struct ovl_lookup_data *d) 1075 { 1076 struct ovl_fs *ofs = OVL_FS(d->sb); 1077 1078 if (d->metacopy && !ofs->config.metacopy) { 1079 pr_warn_ratelimited("refusing to follow metacopy origin for (%pd2)\n", d->dentry); 1080 return false; 1081 } 1082 if ((d->redirect || d->upperredirect) && !ovl_redirect_follow(ofs)) { 1083 pr_warn_ratelimited("refusing to follow redirect for (%pd2)\n", d->dentry); 1084 return false; 1085 } 1086 return true; 1087 } 1088 1089 struct ovl_lookup_ctx { 1090 struct dentry *dentry; 1091 struct ovl_entry *oe; 1092 struct ovl_path *stack; 1093 struct ovl_path *origin_path; 1094 struct dentry *upperdentry; 1095 struct dentry *index; 1096 struct inode *inode; 1097 unsigned int ctr; 1098 }; 1099 1100 static int ovl_lookup_layers(struct ovl_lookup_ctx *ctx, struct ovl_lookup_data *d) 1101 { 1102 struct dentry *dentry = ctx->dentry; 1103 struct ovl_fs *ofs = OVL_FS(dentry->d_sb); 1104 struct ovl_entry *poe = OVL_E(dentry->d_parent); 1105 struct ovl_entry *roe = OVL_E(dentry->d_sb->s_root); 1106 bool check_redirect = (ovl_redirect_follow(ofs) || ofs->numdatalayer); 1107 struct dentry *upperdir; 1108 struct dentry *this; 1109 struct dentry *origin = NULL; 1110 bool upperopaque = false; 1111 bool uppermetacopy = false; 1112 int metacopy_size = 0; 1113 unsigned int i; 1114 int err; 1115 1116 upperdir = ovl_dentry_upper(dentry->d_parent); 1117 if (upperdir) { 1118 d->layer = &ofs->layers[0]; 1119 err = ovl_lookup_layer(upperdir, d, &ctx->upperdentry, true); 1120 if (err) 1121 return err; 1122 1123 if (ctx->upperdentry && ctx->upperdentry->d_flags & DCACHE_OP_REAL) 1124 return -EREMOTE; 1125 1126 if (ctx->upperdentry && !d->is_dir) { 1127 /* 1128 * Lookup copy up origin by decoding origin file handle. 1129 * We may get a disconnected dentry, which is fine, 1130 * because we only need to hold the origin inode in 1131 * cache and use its inode number. We may even get a 1132 * connected dentry, that is not under any of the lower 1133 * layers root. That is also fine for using it's inode 1134 * number - it's the same as if we held a reference 1135 * to a dentry in lower layer that was moved under us. 1136 */ 1137 err = ovl_check_origin(ofs, ctx->upperdentry, &ctx->origin_path); 1138 if (err) 1139 return err; 1140 1141 if (d->metacopy) 1142 uppermetacopy = true; 1143 metacopy_size = d->metacopy; 1144 } 1145 1146 if (d->redirect) { 1147 err = -ENOMEM; 1148 d->upperredirect = kstrdup(d->redirect, GFP_KERNEL); 1149 if (!d->upperredirect) 1150 return err; 1151 if (d->redirect[0] == '/') 1152 poe = roe; 1153 } 1154 upperopaque = d->opaque; 1155 } 1156 1157 if (!d->stop && ovl_numlower(poe)) { 1158 err = -ENOMEM; 1159 ctx->stack = ovl_stack_alloc(ofs->numlayer - 1); 1160 if (!ctx->stack) 1161 return err; 1162 } 1163 1164 for (i = 0; !d->stop && i < ovl_numlower(poe); i++) { 1165 struct ovl_path lower = ovl_lowerstack(poe)[i]; 1166 1167 if (!ovl_check_follow_redirect(d)) { 1168 err = -EPERM; 1169 return err; 1170 } 1171 1172 if (!check_redirect) 1173 d->last = i == ovl_numlower(poe) - 1; 1174 else if (d->is_dir || !ofs->numdatalayer) 1175 d->last = lower.layer->idx == ovl_numlower(roe); 1176 1177 d->layer = lower.layer; 1178 err = ovl_lookup_layer(lower.dentry, d, &this, false); 1179 if (err) 1180 return err; 1181 1182 if (!this) 1183 continue; 1184 1185 /* 1186 * If no origin fh is stored in upper of a merge dir, store fh 1187 * of lower dir and set upper parent "impure". 1188 */ 1189 if (ctx->upperdentry && !ctx->ctr && !ofs->noxattr && d->is_dir) { 1190 err = ovl_fix_origin(ofs, dentry, this, ctx->upperdentry); 1191 if (err) { 1192 dput(this); 1193 return err; 1194 } 1195 } 1196 1197 /* 1198 * When "verify_lower" feature is enabled, do not merge with a 1199 * lower dir that does not match a stored origin xattr. In any 1200 * case, only verified origin is used for index lookup. 1201 * 1202 * For non-dir dentry, if index=on, then ensure origin 1203 * matches the dentry found using path based lookup, 1204 * otherwise error out. 1205 */ 1206 if (ctx->upperdentry && !ctx->ctr && 1207 ((d->is_dir && ovl_verify_lower(dentry->d_sb)) || 1208 (!d->is_dir && ofs->config.index && ctx->origin_path))) { 1209 err = ovl_verify_origin(ofs, ctx->upperdentry, this, false); 1210 if (err) { 1211 dput(this); 1212 if (d->is_dir) 1213 break; 1214 return err; 1215 } 1216 origin = this; 1217 } 1218 1219 if (!ctx->upperdentry && !d->is_dir && !ctx->ctr && d->metacopy) 1220 metacopy_size = d->metacopy; 1221 1222 if (d->metacopy && ctx->ctr) { 1223 /* 1224 * Do not store intermediate metacopy dentries in 1225 * lower chain, except top most lower metacopy dentry. 1226 * Continue the loop so that if there is an absolute 1227 * redirect on this dentry, poe can be reset to roe. 1228 */ 1229 dput(this); 1230 this = NULL; 1231 } else { 1232 ctx->stack[ctx->ctr].dentry = this; 1233 ctx->stack[ctx->ctr].layer = lower.layer; 1234 ctx->ctr++; 1235 } 1236 1237 if (d->stop) 1238 break; 1239 1240 if (d->redirect && d->redirect[0] == '/' && poe != roe) { 1241 poe = roe; 1242 /* Find the current layer on the root dentry */ 1243 i = lower.layer->idx - 1; 1244 } 1245 } 1246 1247 /* 1248 * Defer lookup of lowerdata in data-only layers to first access. 1249 * Don't require redirect=follow and metacopy=on in this case. 1250 */ 1251 if (d->metacopy && ctx->ctr && ofs->numdatalayer && d->absolute_redirect) { 1252 d->metacopy = 0; 1253 ctx->ctr++; 1254 } else if (!ovl_check_follow_redirect(d)) { 1255 err = -EPERM; 1256 return err; 1257 } 1258 1259 /* 1260 * For regular non-metacopy upper dentries, there is no lower 1261 * path based lookup, hence ctr will be zero. If a dentry is found 1262 * using ORIGIN xattr on upper, install it in stack. 1263 * 1264 * For metacopy dentry, path based lookup will find lower dentries. 1265 * Just make sure a corresponding data dentry has been found. 1266 */ 1267 if (d->metacopy || (uppermetacopy && !ctx->ctr)) { 1268 pr_warn_ratelimited("metacopy with no lower data found - abort lookup (%pd2)\n", 1269 dentry); 1270 err = -EIO; 1271 return err; 1272 } else if (!d->is_dir && ctx->upperdentry && !ctx->ctr && ctx->origin_path) { 1273 if (WARN_ON(ctx->stack != NULL)) { 1274 err = -EIO; 1275 return err; 1276 } 1277 ctx->stack = ctx->origin_path; 1278 ctx->ctr = 1; 1279 origin = ctx->origin_path->dentry; 1280 ctx->origin_path = NULL; 1281 } 1282 1283 /* 1284 * Always lookup index if there is no-upperdentry. 1285 * 1286 * For the case of upperdentry, we have set origin by now if it 1287 * needed to be set. There are basically three cases. 1288 * 1289 * For directories, lookup index by lower inode and verify it matches 1290 * upper inode. We only trust dir index if we verified that lower dir 1291 * matches origin, otherwise dir index entries may be inconsistent 1292 * and we ignore them. 1293 * 1294 * For regular upper, we already set origin if upper had ORIGIN 1295 * xattr. There is no verification though as there is no path 1296 * based dentry lookup in lower in this case. 1297 * 1298 * For metacopy upper, we set a verified origin already if index 1299 * is enabled and if upper had an ORIGIN xattr. 1300 * 1301 */ 1302 if (!ctx->upperdentry && ctx->ctr) 1303 origin = ctx->stack[0].dentry; 1304 1305 if (origin && ovl_indexdir(dentry->d_sb) && 1306 (!d->is_dir || ovl_index_all(dentry->d_sb))) { 1307 ctx->index = ovl_lookup_index(ofs, ctx->upperdentry, origin, true); 1308 if (IS_ERR(ctx->index)) { 1309 err = PTR_ERR(ctx->index); 1310 ctx->index = NULL; 1311 return err; 1312 } 1313 } 1314 1315 if (ctx->ctr) { 1316 ctx->oe = ovl_alloc_entry(ctx->ctr); 1317 err = -ENOMEM; 1318 if (!ctx->oe) 1319 return err; 1320 1321 ovl_stack_cpy(ovl_lowerstack(ctx->oe), ctx->stack, ctx->ctr); 1322 } 1323 1324 if (upperopaque) 1325 ovl_dentry_set_opaque(dentry); 1326 if (d->xwhiteouts) 1327 ovl_dentry_set_xwhiteouts(dentry); 1328 1329 if (ctx->upperdentry) 1330 ovl_dentry_set_upper_alias(dentry); 1331 else if (ctx->index) { 1332 char *upperredirect; 1333 struct path upperpath = { 1334 .dentry = ctx->upperdentry = dget(ctx->index), 1335 .mnt = ovl_upper_mnt(ofs), 1336 }; 1337 1338 /* 1339 * It's safe to assign upperredirect here: the previous 1340 * assignment happens only if upperdentry is non-NULL, and 1341 * this one only if upperdentry is NULL. 1342 */ 1343 upperredirect = ovl_get_redirect_xattr(ofs, &upperpath, 0); 1344 if (IS_ERR(upperredirect)) 1345 return PTR_ERR(upperredirect); 1346 d->upperredirect = upperredirect; 1347 1348 err = ovl_check_metacopy_xattr(ofs, &upperpath, NULL); 1349 if (err < 0) 1350 return err; 1351 d->metacopy = uppermetacopy = err; 1352 metacopy_size = err; 1353 1354 if (!ovl_check_follow_redirect(d)) { 1355 err = -EPERM; 1356 return err; 1357 } 1358 } 1359 1360 if (ctx->upperdentry || ctx->ctr) { 1361 struct inode *inode; 1362 struct ovl_inode_params oip = { 1363 .upperdentry = ctx->upperdentry, 1364 .oe = ctx->oe, 1365 .index = ctx->index, 1366 .redirect = d->upperredirect, 1367 }; 1368 1369 /* Store lowerdata redirect for lazy lookup */ 1370 if (ctx->ctr > 1 && !d->is_dir && !ctx->stack[ctx->ctr - 1].dentry) { 1371 oip.lowerdata_redirect = d->redirect; 1372 d->redirect = NULL; 1373 } 1374 1375 inode = ovl_get_inode(dentry->d_sb, &oip); 1376 if (IS_ERR(inode)) 1377 return PTR_ERR(inode); 1378 1379 ctx->inode = inode; 1380 if (ctx->upperdentry && !uppermetacopy) 1381 ovl_set_flag(OVL_UPPERDATA, ctx->inode); 1382 1383 if (metacopy_size > OVL_METACOPY_MIN_SIZE) 1384 ovl_set_flag(OVL_HAS_DIGEST, ctx->inode); 1385 } 1386 1387 ovl_dentry_init_reval(dentry, ctx->upperdentry, OVL_I_E(ctx->inode)); 1388 1389 return 0; 1390 } 1391 1392 struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, 1393 unsigned int flags) 1394 { 1395 struct ovl_fs *ofs = OVL_FS(dentry->d_sb); 1396 struct ovl_entry *poe = OVL_E(dentry->d_parent); 1397 bool check_redirect = (ovl_redirect_follow(ofs) || ofs->numdatalayer); 1398 int err; 1399 struct ovl_lookup_ctx ctx = { 1400 .dentry = dentry, 1401 }; 1402 struct ovl_lookup_data d = { 1403 .sb = dentry->d_sb, 1404 .dentry = dentry, 1405 .name = dentry->d_name, 1406 .last = check_redirect ? false : !ovl_numlower(poe), 1407 }; 1408 1409 if (dentry->d_name.len > ofs->namelen) 1410 return ERR_PTR(-ENAMETOOLONG); 1411 1412 with_ovl_creds(dentry->d_sb) 1413 err = ovl_lookup_layers(&ctx, &d); 1414 1415 if (ctx.origin_path) { 1416 dput(ctx.origin_path->dentry); 1417 kfree(ctx.origin_path); 1418 } 1419 dput(ctx.index); 1420 ovl_stack_free(ctx.stack, ctx.ctr); 1421 kfree(d.redirect); 1422 1423 if (err) { 1424 ovl_free_entry(ctx.oe); 1425 dput(ctx.upperdentry); 1426 kfree(d.upperredirect); 1427 return ERR_PTR(err); 1428 } 1429 1430 return d_splice_alias(ctx.inode, dentry); 1431 } 1432 1433 bool ovl_lower_positive(struct dentry *dentry) 1434 { 1435 struct ovl_entry *poe = OVL_E(dentry->d_parent); 1436 const struct qstr *name = &dentry->d_name; 1437 unsigned int i; 1438 bool positive = false; 1439 bool done = false; 1440 1441 /* 1442 * If dentry is negative, then lower is positive iff this is a 1443 * whiteout. 1444 */ 1445 if (!dentry->d_inode) 1446 return ovl_dentry_is_opaque(dentry); 1447 1448 /* Negative upper -> positive lower */ 1449 if (!ovl_dentry_upper(dentry)) 1450 return true; 1451 1452 with_ovl_creds(dentry->d_sb) { 1453 /* Positive upper -> have to look up lower to see whether it exists */ 1454 for (i = 0; !done && !positive && i < ovl_numlower(poe); i++) { 1455 struct dentry *this; 1456 struct ovl_path *parentpath = &ovl_lowerstack(poe)[i]; 1457 1458 /* 1459 * We need to make a non-const copy of dentry->d_name, 1460 * because lookup_one_positive_unlocked() will hash name 1461 * with parentpath base, which is on another (lower fs). 1462 */ 1463 this = lookup_one_positive_unlocked(mnt_idmap(parentpath->layer->mnt), 1464 &QSTR_LEN(name->name, name->len), 1465 parentpath->dentry); 1466 if (IS_ERR(this)) { 1467 switch (PTR_ERR(this)) { 1468 case -ENOENT: 1469 case -ENAMETOOLONG: 1470 break; 1471 1472 default: 1473 /* 1474 * Assume something is there, we just couldn't 1475 * access it. 1476 */ 1477 positive = true; 1478 break; 1479 } 1480 } else { 1481 struct path path = { 1482 .dentry = this, 1483 .mnt = parentpath->layer->mnt, 1484 }; 1485 positive = !ovl_path_is_whiteout(OVL_FS(dentry->d_sb), &path); 1486 done = true; 1487 dput(this); 1488 } 1489 } 1490 } 1491 1492 return positive; 1493 } 1494