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