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 return 1; 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 static int ovl_check_fh_len(struct ovl_fh *fh, int fh_len) 98 { 99 if (fh_len < sizeof(struct ovl_fh) || fh_len < fh->len) 100 return -EINVAL; 101 102 if (fh->magic != OVL_FH_MAGIC) 103 return -EINVAL; 104 105 /* Treat larger version and unknown flags as "origin unknown" */ 106 if (fh->version > OVL_FH_VERSION || fh->flags & ~OVL_FH_FLAG_ALL) 107 return -ENODATA; 108 109 /* Treat endianness mismatch as "origin unknown" */ 110 if (!(fh->flags & OVL_FH_FLAG_ANY_ENDIAN) && 111 (fh->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_origin_fh(struct dentry *dentry) 118 { 119 int res, err; 120 struct ovl_fh *fh = NULL; 121 122 res = vfs_getxattr(dentry, OVL_XATTR_ORIGIN, NULL, 0); 123 if (res < 0) { 124 if (res == -ENODATA || res == -EOPNOTSUPP) 125 return NULL; 126 goto fail; 127 } 128 /* Zero size value means "copied up but origin unknown" */ 129 if (res == 0) 130 return NULL; 131 132 fh = kzalloc(res, GFP_KERNEL); 133 if (!fh) 134 return ERR_PTR(-ENOMEM); 135 136 res = vfs_getxattr(dentry, OVL_XATTR_ORIGIN, fh, res); 137 if (res < 0) 138 goto fail; 139 140 err = ovl_check_fh_len(fh, res); 141 if (err < 0) { 142 if (err == -ENODATA) 143 goto out; 144 goto invalid; 145 } 146 147 return fh; 148 149 out: 150 kfree(fh); 151 return NULL; 152 153 fail: 154 pr_warn_ratelimited("overlayfs: failed to get origin (%i)\n", res); 155 goto out; 156 invalid: 157 pr_warn_ratelimited("overlayfs: invalid origin (%*phN)\n", res, fh); 158 goto out; 159 } 160 161 static struct dentry *ovl_decode_fh(struct ovl_fh *fh, struct vfsmount *mnt) 162 { 163 struct dentry *origin; 164 int bytes; 165 166 /* 167 * Make sure that the stored uuid matches the uuid of the lower 168 * layer where file handle will be decoded. 169 */ 170 if (!uuid_equal(&fh->uuid, &mnt->mnt_sb->s_uuid)) 171 return NULL; 172 173 bytes = (fh->len - offsetof(struct ovl_fh, fid)); 174 origin = exportfs_decode_fh(mnt, (struct fid *)fh->fid, 175 bytes >> 2, (int)fh->type, 176 ovl_acceptable, NULL); 177 if (IS_ERR(origin)) { 178 /* Treat stale file handle as "origin unknown" */ 179 if (origin == ERR_PTR(-ESTALE)) 180 origin = NULL; 181 return origin; 182 } 183 184 if (ovl_dentry_weird(origin)) { 185 dput(origin); 186 return NULL; 187 } 188 189 return origin; 190 } 191 192 static bool ovl_is_opaquedir(struct dentry *dentry) 193 { 194 return ovl_check_dir_xattr(dentry, OVL_XATTR_OPAQUE); 195 } 196 197 static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d, 198 const char *name, unsigned int namelen, 199 size_t prelen, const char *post, 200 struct dentry **ret) 201 { 202 struct dentry *this; 203 int err; 204 205 this = lookup_one_len_unlocked(name, base, namelen); 206 if (IS_ERR(this)) { 207 err = PTR_ERR(this); 208 this = NULL; 209 if (err == -ENOENT || err == -ENAMETOOLONG) 210 goto out; 211 goto out_err; 212 } 213 if (!this->d_inode) 214 goto put_and_out; 215 216 if (ovl_dentry_weird(this)) { 217 /* Don't support traversing automounts and other weirdness */ 218 err = -EREMOTE; 219 goto out_err; 220 } 221 if (ovl_is_whiteout(this)) { 222 d->stop = d->opaque = true; 223 goto put_and_out; 224 } 225 if (!d_can_lookup(this)) { 226 d->stop = true; 227 if (d->is_dir) 228 goto put_and_out; 229 goto out; 230 } 231 d->is_dir = true; 232 if (!d->last && ovl_is_opaquedir(this)) { 233 d->stop = d->opaque = true; 234 goto out; 235 } 236 err = ovl_check_redirect(this, d, prelen, post); 237 if (err) 238 goto out_err; 239 out: 240 *ret = this; 241 return 0; 242 243 put_and_out: 244 dput(this); 245 this = NULL; 246 goto out; 247 248 out_err: 249 dput(this); 250 return err; 251 } 252 253 static int ovl_lookup_layer(struct dentry *base, struct ovl_lookup_data *d, 254 struct dentry **ret) 255 { 256 /* Counting down from the end, since the prefix can change */ 257 size_t rem = d->name.len - 1; 258 struct dentry *dentry = NULL; 259 int err; 260 261 if (d->name.name[0] != '/') 262 return ovl_lookup_single(base, d, d->name.name, d->name.len, 263 0, "", ret); 264 265 while (!IS_ERR_OR_NULL(base) && d_can_lookup(base)) { 266 const char *s = d->name.name + d->name.len - rem; 267 const char *next = strchrnul(s, '/'); 268 size_t thislen = next - s; 269 bool end = !next[0]; 270 271 /* Verify we did not go off the rails */ 272 if (WARN_ON(s[-1] != '/')) 273 return -EIO; 274 275 err = ovl_lookup_single(base, d, s, thislen, 276 d->name.len - rem, next, &base); 277 dput(dentry); 278 if (err) 279 return err; 280 dentry = base; 281 if (end) 282 break; 283 284 rem -= thislen + 1; 285 286 if (WARN_ON(rem >= d->name.len)) 287 return -EIO; 288 } 289 *ret = dentry; 290 return 0; 291 } 292 293 294 static int ovl_check_origin_fh(struct ovl_fs *ofs, struct ovl_fh *fh, 295 struct dentry *upperdentry, 296 struct ovl_path **stackp) 297 { 298 struct dentry *origin = NULL; 299 int i; 300 301 for (i = 0; i < ofs->numlower; i++) { 302 origin = ovl_decode_fh(fh, ofs->lower_layers[i].mnt); 303 if (origin) 304 break; 305 } 306 307 if (!origin) 308 return -ESTALE; 309 else if (IS_ERR(origin)) 310 return PTR_ERR(origin); 311 312 if (!ovl_is_whiteout(upperdentry) && 313 ((d_inode(origin)->i_mode ^ d_inode(upperdentry)->i_mode) & S_IFMT)) 314 goto invalid; 315 316 if (!*stackp) 317 *stackp = kmalloc(sizeof(struct ovl_path), GFP_KERNEL); 318 if (!*stackp) { 319 dput(origin); 320 return -ENOMEM; 321 } 322 **stackp = (struct ovl_path){ 323 .dentry = origin, 324 .layer = &ofs->lower_layers[i] 325 }; 326 327 return 0; 328 329 invalid: 330 pr_warn_ratelimited("overlayfs: invalid origin (%pd2, ftype=%x, origin ftype=%x).\n", 331 upperdentry, d_inode(upperdentry)->i_mode & S_IFMT, 332 d_inode(origin)->i_mode & S_IFMT); 333 dput(origin); 334 return -EIO; 335 } 336 337 static int ovl_check_origin(struct ovl_fs *ofs, struct dentry *upperdentry, 338 struct ovl_path **stackp, unsigned int *ctrp) 339 { 340 struct ovl_fh *fh = ovl_get_origin_fh(upperdentry); 341 int err; 342 343 if (IS_ERR_OR_NULL(fh)) 344 return PTR_ERR(fh); 345 346 err = ovl_check_origin_fh(ofs, fh, upperdentry, stackp); 347 kfree(fh); 348 349 if (err) { 350 if (err == -ESTALE) 351 return 0; 352 return err; 353 } 354 355 if (WARN_ON(*ctrp)) 356 return -EIO; 357 358 *ctrp = 1; 359 return 0; 360 } 361 362 /* 363 * Verify that @fh matches the origin file handle stored in OVL_XATTR_ORIGIN. 364 * Return 0 on match, -ESTALE on mismatch, < 0 on error. 365 */ 366 static int ovl_verify_origin_fh(struct dentry *dentry, const struct ovl_fh *fh) 367 { 368 struct ovl_fh *ofh = ovl_get_origin_fh(dentry); 369 int err = 0; 370 371 if (!ofh) 372 return -ENODATA; 373 374 if (IS_ERR(ofh)) 375 return PTR_ERR(ofh); 376 377 if (fh->len != ofh->len || memcmp(fh, ofh, fh->len)) 378 err = -ESTALE; 379 380 kfree(ofh); 381 return err; 382 } 383 384 /* 385 * Verify that an inode matches the origin file handle stored in upper inode. 386 * 387 * If @set is true and there is no stored file handle, encode and store origin 388 * file handle in OVL_XATTR_ORIGIN. 389 * 390 * Return 0 on match, -ESTALE on mismatch, < 0 on error. 391 */ 392 int ovl_verify_origin(struct dentry *dentry, struct dentry *origin, 393 bool is_upper, bool set) 394 { 395 struct inode *inode; 396 struct ovl_fh *fh; 397 int err; 398 399 fh = ovl_encode_fh(origin, is_upper); 400 err = PTR_ERR(fh); 401 if (IS_ERR(fh)) 402 goto fail; 403 404 err = ovl_verify_origin_fh(dentry, fh); 405 if (set && err == -ENODATA) 406 err = ovl_do_setxattr(dentry, OVL_XATTR_ORIGIN, fh, fh->len, 0); 407 if (err) 408 goto fail; 409 410 out: 411 kfree(fh); 412 return err; 413 414 fail: 415 inode = d_inode(origin); 416 pr_warn_ratelimited("overlayfs: failed to verify origin (%pd2, ino=%lu, err=%i)\n", 417 origin, inode ? inode->i_ino : 0, err); 418 goto out; 419 } 420 421 /* 422 * Verify that an index entry name matches the origin file handle stored in 423 * OVL_XATTR_ORIGIN and that origin file handle can be decoded to lower path. 424 * Return 0 on match, -ESTALE on mismatch or stale origin, < 0 on error. 425 */ 426 int ovl_verify_index(struct ovl_fs *ofs, struct dentry *index) 427 { 428 struct ovl_fh *fh = NULL; 429 size_t len; 430 struct ovl_path origin = { }; 431 struct ovl_path *stack = &origin; 432 int err; 433 434 if (!d_inode(index)) 435 return 0; 436 437 /* 438 * Directory index entries are going to be used for looking up 439 * redirected upper dirs by lower dir fh when decoding an overlay 440 * file handle of a merge dir. Whiteout index entries are going to be 441 * used as an indication that an exported overlay file handle should 442 * be treated as stale (i.e. after unlink of the overlay inode). 443 * We don't know the verification rules for directory and whiteout 444 * index entries, because they have not been implemented yet, so return 445 * EINVAL if those entries are found to abort the mount to avoid 446 * corrupting an index that was created by a newer kernel. 447 */ 448 err = -EINVAL; 449 if (d_is_dir(index) || ovl_is_whiteout(index)) 450 goto fail; 451 452 if (index->d_name.len < sizeof(struct ovl_fh)*2) 453 goto fail; 454 455 err = -ENOMEM; 456 len = index->d_name.len / 2; 457 fh = kzalloc(len, GFP_KERNEL); 458 if (!fh) 459 goto fail; 460 461 err = -EINVAL; 462 if (hex2bin((u8 *)fh, index->d_name.name, len)) 463 goto fail; 464 465 err = ovl_check_fh_len(fh, len); 466 if (err) 467 goto fail; 468 469 err = ovl_verify_origin_fh(index, fh); 470 if (err) 471 goto fail; 472 473 err = ovl_check_origin_fh(ofs, fh, index, &stack); 474 if (err) 475 goto fail; 476 477 /* Check if index is orphan and don't warn before cleaning it */ 478 if (d_inode(index)->i_nlink == 1 && 479 ovl_get_nlink(origin.dentry, index, 0) == 0) 480 err = -ENOENT; 481 482 dput(origin.dentry); 483 out: 484 kfree(fh); 485 return err; 486 487 fail: 488 pr_warn_ratelimited("overlayfs: failed to verify index (%pd2, ftype=%x, err=%i)\n", 489 index, d_inode(index)->i_mode & S_IFMT, err); 490 goto out; 491 } 492 493 /* 494 * Lookup in indexdir for the index entry of a lower real inode or a copy up 495 * origin inode. The index entry name is the hex representation of the lower 496 * inode file handle. 497 * 498 * If the index dentry in negative, then either no lower aliases have been 499 * copied up yet, or aliases have been copied up in older kernels and are 500 * not indexed. 501 * 502 * If the index dentry for a copy up origin inode is positive, but points 503 * to an inode different than the upper inode, then either the upper inode 504 * has been copied up and not indexed or it was indexed, but since then 505 * index dir was cleared. Either way, that index cannot be used to indentify 506 * the overlay inode. 507 */ 508 int ovl_get_index_name(struct dentry *origin, struct qstr *name) 509 { 510 int err; 511 struct ovl_fh *fh; 512 char *n, *s; 513 514 fh = ovl_encode_fh(origin, false); 515 if (IS_ERR(fh)) 516 return PTR_ERR(fh); 517 518 err = -ENOMEM; 519 n = kzalloc(fh->len * 2, GFP_KERNEL); 520 if (n) { 521 s = bin2hex(n, fh, fh->len); 522 *name = (struct qstr) QSTR_INIT(n, s - n); 523 err = 0; 524 } 525 kfree(fh); 526 527 return err; 528 529 } 530 531 static struct dentry *ovl_lookup_index(struct dentry *dentry, 532 struct dentry *upper, 533 struct dentry *origin) 534 { 535 struct ovl_fs *ofs = dentry->d_sb->s_fs_info; 536 struct dentry *index; 537 struct inode *inode; 538 struct qstr name; 539 int err; 540 541 err = ovl_get_index_name(origin, &name); 542 if (err) 543 return ERR_PTR(err); 544 545 index = lookup_one_len_unlocked(name.name, ofs->indexdir, name.len); 546 if (IS_ERR(index)) { 547 err = PTR_ERR(index); 548 if (err == -ENOENT) { 549 index = NULL; 550 goto out; 551 } 552 pr_warn_ratelimited("overlayfs: failed inode index lookup (ino=%lu, key=%*s, err=%i);\n" 553 "overlayfs: mount with '-o index=off' to disable inodes index.\n", 554 d_inode(origin)->i_ino, name.len, name.name, 555 err); 556 goto out; 557 } 558 559 inode = d_inode(index); 560 if (d_is_negative(index)) { 561 goto out_dput; 562 } else if (upper && d_inode(upper) != inode) { 563 goto out_dput; 564 } else if (ovl_dentry_weird(index) || ovl_is_whiteout(index) || 565 ((inode->i_mode ^ d_inode(origin)->i_mode) & S_IFMT)) { 566 /* 567 * Index should always be of the same file type as origin 568 * except for the case of a whiteout index. A whiteout 569 * index should only exist if all lower aliases have been 570 * unlinked, which means that finding a lower origin on lookup 571 * whose index is a whiteout should be treated as an error. 572 */ 573 pr_warn_ratelimited("overlayfs: bad index found (index=%pd2, ftype=%x, origin ftype=%x).\n", 574 index, d_inode(index)->i_mode & S_IFMT, 575 d_inode(origin)->i_mode & S_IFMT); 576 goto fail; 577 } 578 579 out: 580 kfree(name.name); 581 return index; 582 583 out_dput: 584 dput(index); 585 index = NULL; 586 goto out; 587 588 fail: 589 dput(index); 590 index = ERR_PTR(-EIO); 591 goto out; 592 } 593 594 /* 595 * Returns next layer in stack starting from top. 596 * Returns -1 if this is the last layer. 597 */ 598 int ovl_path_next(int idx, struct dentry *dentry, struct path *path) 599 { 600 struct ovl_entry *oe = dentry->d_fsdata; 601 602 BUG_ON(idx < 0); 603 if (idx == 0) { 604 ovl_path_upper(dentry, path); 605 if (path->dentry) 606 return oe->numlower ? 1 : -1; 607 idx++; 608 } 609 BUG_ON(idx > oe->numlower); 610 path->dentry = oe->lowerstack[idx - 1].dentry; 611 path->mnt = oe->lowerstack[idx - 1].layer->mnt; 612 613 return (idx < oe->numlower) ? idx + 1 : -1; 614 } 615 616 /* Fix missing 'origin' xattr */ 617 static int ovl_fix_origin(struct dentry *dentry, struct dentry *lower, 618 struct dentry *upper) 619 { 620 int err; 621 622 if (ovl_check_origin_xattr(upper)) 623 return 0; 624 625 err = ovl_want_write(dentry); 626 if (err) 627 return err; 628 629 err = ovl_set_origin(dentry, lower, upper); 630 if (!err) 631 err = ovl_set_impure(dentry->d_parent, upper->d_parent); 632 633 ovl_drop_write(dentry); 634 return err; 635 } 636 637 struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, 638 unsigned int flags) 639 { 640 struct ovl_entry *oe; 641 const struct cred *old_cred; 642 struct ovl_fs *ofs = dentry->d_sb->s_fs_info; 643 struct ovl_entry *poe = dentry->d_parent->d_fsdata; 644 struct ovl_entry *roe = dentry->d_sb->s_root->d_fsdata; 645 struct ovl_path *stack = NULL; 646 struct dentry *upperdir, *upperdentry = NULL; 647 struct dentry *index = NULL; 648 unsigned int ctr = 0; 649 struct inode *inode = NULL; 650 bool upperopaque = false; 651 char *upperredirect = NULL; 652 struct dentry *this; 653 unsigned int i; 654 int err; 655 struct ovl_lookup_data d = { 656 .name = dentry->d_name, 657 .is_dir = false, 658 .opaque = false, 659 .stop = false, 660 .last = !poe->numlower, 661 .redirect = NULL, 662 }; 663 664 if (dentry->d_name.len > ofs->namelen) 665 return ERR_PTR(-ENAMETOOLONG); 666 667 old_cred = ovl_override_creds(dentry->d_sb); 668 upperdir = ovl_dentry_upper(dentry->d_parent); 669 if (upperdir) { 670 err = ovl_lookup_layer(upperdir, &d, &upperdentry); 671 if (err) 672 goto out; 673 674 if (upperdentry && unlikely(ovl_dentry_remote(upperdentry))) { 675 dput(upperdentry); 676 err = -EREMOTE; 677 goto out; 678 } 679 if (upperdentry && !d.is_dir) { 680 BUG_ON(!d.stop || d.redirect); 681 /* 682 * Lookup copy up origin by decoding origin file handle. 683 * We may get a disconnected dentry, which is fine, 684 * because we only need to hold the origin inode in 685 * cache and use its inode number. We may even get a 686 * connected dentry, that is not under any of the lower 687 * layers root. That is also fine for using it's inode 688 * number - it's the same as if we held a reference 689 * to a dentry in lower layer that was moved under us. 690 */ 691 err = ovl_check_origin(ofs, upperdentry, &stack, &ctr); 692 if (err) 693 goto out_put_upper; 694 } 695 696 if (d.redirect) { 697 err = -ENOMEM; 698 upperredirect = kstrdup(d.redirect, GFP_KERNEL); 699 if (!upperredirect) 700 goto out_put_upper; 701 if (d.redirect[0] == '/') 702 poe = roe; 703 } 704 upperopaque = d.opaque; 705 } 706 707 if (!d.stop && poe->numlower) { 708 err = -ENOMEM; 709 stack = kcalloc(ofs->numlower, sizeof(struct ovl_path), 710 GFP_KERNEL); 711 if (!stack) 712 goto out_put_upper; 713 } 714 715 for (i = 0; !d.stop && i < poe->numlower; i++) { 716 struct ovl_path lower = poe->lowerstack[i]; 717 718 d.last = i == poe->numlower - 1; 719 err = ovl_lookup_layer(lower.dentry, &d, &this); 720 if (err) 721 goto out_put; 722 723 if (!this) 724 continue; 725 726 /* 727 * If no origin fh is stored in upper of a merge dir, store fh 728 * of lower dir and set upper parent "impure". 729 */ 730 if (upperdentry && !ctr && !ofs->noxattr) { 731 err = ovl_fix_origin(dentry, this, upperdentry); 732 if (err) { 733 dput(this); 734 goto out_put; 735 } 736 } 737 738 stack[ctr].dentry = this; 739 stack[ctr].layer = lower.layer; 740 ctr++; 741 742 if (d.stop) 743 break; 744 745 /* 746 * Following redirects can have security consequences: it's like 747 * a symlink into the lower layer without the permission checks. 748 * This is only a problem if the upper layer is untrusted (e.g 749 * comes from an USB drive). This can allow a non-readable file 750 * or directory to become readable. 751 * 752 * Only following redirects when redirects are enabled disables 753 * this attack vector when not necessary. 754 */ 755 err = -EPERM; 756 if (d.redirect && !ofs->config.redirect_follow) { 757 pr_warn_ratelimited("overlayfs: refusing to follow redirect for (%pd2)\n", 758 dentry); 759 goto out_put; 760 } 761 762 if (d.redirect && d.redirect[0] == '/' && poe != roe) { 763 poe = roe; 764 /* Find the current layer on the root dentry */ 765 i = lower.layer->idx - 1; 766 } 767 } 768 769 /* Lookup index by lower inode and verify it matches upper inode */ 770 if (ctr && !d.is_dir && ovl_indexdir(dentry->d_sb)) { 771 struct dentry *origin = stack[0].dentry; 772 773 index = ovl_lookup_index(dentry, upperdentry, origin); 774 if (IS_ERR(index)) { 775 err = PTR_ERR(index); 776 index = NULL; 777 goto out_put; 778 } 779 } 780 781 oe = ovl_alloc_entry(ctr); 782 err = -ENOMEM; 783 if (!oe) 784 goto out_put; 785 786 oe->opaque = upperopaque; 787 memcpy(oe->lowerstack, stack, sizeof(struct ovl_path) * ctr); 788 dentry->d_fsdata = oe; 789 790 if (upperdentry) 791 ovl_dentry_set_upper_alias(dentry); 792 else if (index) 793 upperdentry = dget(index); 794 795 if (upperdentry || ctr) { 796 inode = ovl_get_inode(dentry, upperdentry, index); 797 err = PTR_ERR(inode); 798 if (IS_ERR(inode)) 799 goto out_free_oe; 800 801 OVL_I(inode)->redirect = upperredirect; 802 if (index) 803 ovl_set_flag(OVL_INDEX, inode); 804 } 805 806 revert_creds(old_cred); 807 dput(index); 808 kfree(stack); 809 kfree(d.redirect); 810 d_add(dentry, inode); 811 812 return NULL; 813 814 out_free_oe: 815 dentry->d_fsdata = NULL; 816 kfree(oe); 817 out_put: 818 dput(index); 819 for (i = 0; i < ctr; i++) 820 dput(stack[i].dentry); 821 kfree(stack); 822 out_put_upper: 823 dput(upperdentry); 824 kfree(upperredirect); 825 out: 826 kfree(d.redirect); 827 revert_creds(old_cred); 828 return ERR_PTR(err); 829 } 830 831 bool ovl_lower_positive(struct dentry *dentry) 832 { 833 struct ovl_entry *oe = dentry->d_fsdata; 834 struct ovl_entry *poe = dentry->d_parent->d_fsdata; 835 const struct qstr *name = &dentry->d_name; 836 const struct cred *old_cred; 837 unsigned int i; 838 bool positive = false; 839 bool done = false; 840 841 /* 842 * If dentry is negative, then lower is positive iff this is a 843 * whiteout. 844 */ 845 if (!dentry->d_inode) 846 return oe->opaque; 847 848 /* Negative upper -> positive lower */ 849 if (!ovl_dentry_upper(dentry)) 850 return true; 851 852 old_cred = ovl_override_creds(dentry->d_sb); 853 /* Positive upper -> have to look up lower to see whether it exists */ 854 for (i = 0; !done && !positive && i < poe->numlower; i++) { 855 struct dentry *this; 856 struct dentry *lowerdir = poe->lowerstack[i].dentry; 857 858 this = lookup_one_len_unlocked(name->name, lowerdir, 859 name->len); 860 if (IS_ERR(this)) { 861 switch (PTR_ERR(this)) { 862 case -ENOENT: 863 case -ENAMETOOLONG: 864 break; 865 866 default: 867 /* 868 * Assume something is there, we just couldn't 869 * access it. 870 */ 871 positive = true; 872 break; 873 } 874 } else { 875 if (this->d_inode) { 876 positive = !ovl_is_whiteout(this); 877 done = true; 878 } 879 dput(this); 880 } 881 } 882 revert_creds(old_cred); 883 884 return positive; 885 } 886