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