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