1bbb1e54dSMiklos Szeredi /* 2bbb1e54dSMiklos Szeredi * Copyright (C) 2011 Novell Inc. 3bbb1e54dSMiklos Szeredi * Copyright (C) 2016 Red Hat, Inc. 4bbb1e54dSMiklos Szeredi * 5bbb1e54dSMiklos Szeredi * This program is free software; you can redistribute it and/or modify it 6bbb1e54dSMiklos Szeredi * under the terms of the GNU General Public License version 2 as published by 7bbb1e54dSMiklos Szeredi * the Free Software Foundation. 8bbb1e54dSMiklos Szeredi */ 9bbb1e54dSMiklos Szeredi 10bbb1e54dSMiklos Szeredi #include <linux/fs.h> 115b825c3aSIngo Molnar #include <linux/cred.h> 129ee60ce2SAmir Goldstein #include <linux/ctype.h> 13bbb1e54dSMiklos Szeredi #include <linux/namei.h> 14bbb1e54dSMiklos Szeredi #include <linux/xattr.h> 1502b69b28SMiklos Szeredi #include <linux/ratelimit.h> 16a9d01957SAmir Goldstein #include <linux/mount.h> 17a9d01957SAmir Goldstein #include <linux/exportfs.h> 18bbb1e54dSMiklos Szeredi #include "overlayfs.h" 19bbb1e54dSMiklos Szeredi 20e28edc46SMiklos Szeredi struct ovl_lookup_data { 21e28edc46SMiklos Szeredi struct qstr name; 22e28edc46SMiklos Szeredi bool is_dir; 23e28edc46SMiklos Szeredi bool opaque; 24e28edc46SMiklos Szeredi bool stop; 25e28edc46SMiklos Szeredi bool last; 2602b69b28SMiklos Szeredi char *redirect; 27e28edc46SMiklos Szeredi }; 28bbb1e54dSMiklos Szeredi 2902b69b28SMiklos Szeredi static int ovl_check_redirect(struct dentry *dentry, struct ovl_lookup_data *d, 3002b69b28SMiklos Szeredi size_t prelen, const char *post) 3102b69b28SMiklos Szeredi { 3202b69b28SMiklos Szeredi int res; 3302b69b28SMiklos Szeredi char *s, *next, *buf = NULL; 3402b69b28SMiklos Szeredi 3502b69b28SMiklos Szeredi res = vfs_getxattr(dentry, OVL_XATTR_REDIRECT, NULL, 0); 3602b69b28SMiklos Szeredi if (res < 0) { 3702b69b28SMiklos Szeredi if (res == -ENODATA || res == -EOPNOTSUPP) 3802b69b28SMiklos Szeredi return 0; 3902b69b28SMiklos Szeredi goto fail; 4002b69b28SMiklos Szeredi } 410ee931c4SMichal Hocko buf = kzalloc(prelen + res + strlen(post) + 1, GFP_KERNEL); 4202b69b28SMiklos Szeredi if (!buf) 4302b69b28SMiklos Szeredi return -ENOMEM; 4402b69b28SMiklos Szeredi 4502b69b28SMiklos Szeredi if (res == 0) 4602b69b28SMiklos Szeredi goto invalid; 4702b69b28SMiklos Szeredi 4802b69b28SMiklos Szeredi res = vfs_getxattr(dentry, OVL_XATTR_REDIRECT, buf, res); 4902b69b28SMiklos Szeredi if (res < 0) 5002b69b28SMiklos Szeredi goto fail; 5102b69b28SMiklos Szeredi if (res == 0) 5202b69b28SMiklos Szeredi goto invalid; 5302b69b28SMiklos Szeredi if (buf[0] == '/') { 5402b69b28SMiklos Szeredi for (s = buf; *s++ == '/'; s = next) { 5502b69b28SMiklos Szeredi next = strchrnul(s, '/'); 5602b69b28SMiklos Szeredi if (s == next) 5702b69b28SMiklos Szeredi goto invalid; 5802b69b28SMiklos Szeredi } 593ec9b3faSAmir Goldstein /* 603ec9b3faSAmir Goldstein * One of the ancestor path elements in an absolute path 613ec9b3faSAmir Goldstein * lookup in ovl_lookup_layer() could have been opaque and 623ec9b3faSAmir Goldstein * that will stop further lookup in lower layers (d->stop=true) 633ec9b3faSAmir Goldstein * But we have found an absolute redirect in decendant path 643ec9b3faSAmir Goldstein * element and that should force continue lookup in lower 653ec9b3faSAmir Goldstein * layers (reset d->stop). 663ec9b3faSAmir Goldstein */ 673ec9b3faSAmir Goldstein d->stop = false; 6802b69b28SMiklos Szeredi } else { 6902b69b28SMiklos Szeredi if (strchr(buf, '/') != NULL) 7002b69b28SMiklos Szeredi goto invalid; 7102b69b28SMiklos Szeredi 7202b69b28SMiklos Szeredi memmove(buf + prelen, buf, res); 7302b69b28SMiklos Szeredi memcpy(buf, d->name.name, prelen); 7402b69b28SMiklos Szeredi } 7502b69b28SMiklos Szeredi 7602b69b28SMiklos Szeredi strcat(buf, post); 7702b69b28SMiklos Szeredi kfree(d->redirect); 7802b69b28SMiklos Szeredi d->redirect = buf; 7902b69b28SMiklos Szeredi d->name.name = d->redirect; 8002b69b28SMiklos Szeredi d->name.len = strlen(d->redirect); 8102b69b28SMiklos Szeredi 8202b69b28SMiklos Szeredi return 0; 8302b69b28SMiklos Szeredi 8402b69b28SMiklos Szeredi err_free: 8502b69b28SMiklos Szeredi kfree(buf); 8602b69b28SMiklos Szeredi return 0; 8702b69b28SMiklos Szeredi fail: 8802b69b28SMiklos Szeredi pr_warn_ratelimited("overlayfs: failed to get redirect (%i)\n", res); 8902b69b28SMiklos Szeredi goto err_free; 9002b69b28SMiklos Szeredi invalid: 9102b69b28SMiklos Szeredi pr_warn_ratelimited("overlayfs: invalid redirect (%s)\n", buf); 9202b69b28SMiklos Szeredi goto err_free; 9302b69b28SMiklos Szeredi } 9402b69b28SMiklos Szeredi 95a9d01957SAmir Goldstein static int ovl_acceptable(void *ctx, struct dentry *dentry) 96a9d01957SAmir Goldstein { 97e8f9e5b7SAmir Goldstein /* 98e8f9e5b7SAmir Goldstein * A non-dir origin may be disconnected, which is fine, because 99e8f9e5b7SAmir Goldstein * we only need it for its unique inode number. 100e8f9e5b7SAmir Goldstein */ 101e8f9e5b7SAmir Goldstein if (!d_is_dir(dentry)) 102a9d01957SAmir Goldstein return 1; 103e8f9e5b7SAmir Goldstein 104e8f9e5b7SAmir Goldstein /* Don't decode a deleted empty directory */ 105e8f9e5b7SAmir Goldstein if (d_unhashed(dentry)) 106e8f9e5b7SAmir Goldstein return 0; 107e8f9e5b7SAmir Goldstein 108e8f9e5b7SAmir Goldstein /* Check if directory belongs to the layer we are decoding from */ 109e8f9e5b7SAmir Goldstein return is_subdir(dentry, ((struct vfsmount *)ctx)->mnt_root); 110a9d01957SAmir Goldstein } 111a9d01957SAmir Goldstein 1122e1a5328SAmir Goldstein /* 1132e1a5328SAmir Goldstein * Check validity of an overlay file handle buffer. 1142e1a5328SAmir Goldstein * 1152e1a5328SAmir Goldstein * Return 0 for a valid file handle. 1162e1a5328SAmir Goldstein * Return -ENODATA for "origin unknown". 1172e1a5328SAmir Goldstein * Return <0 for an invalid file handle. 1182e1a5328SAmir Goldstein */ 1198556a420SAmir Goldstein int ovl_check_fh_len(struct ovl_fh *fh, int fh_len) 1202e1a5328SAmir Goldstein { 1212e1a5328SAmir Goldstein if (fh_len < sizeof(struct ovl_fh) || fh_len < fh->len) 1222e1a5328SAmir Goldstein return -EINVAL; 1232e1a5328SAmir Goldstein 1242e1a5328SAmir Goldstein if (fh->magic != OVL_FH_MAGIC) 1252e1a5328SAmir Goldstein return -EINVAL; 1262e1a5328SAmir Goldstein 1272e1a5328SAmir Goldstein /* Treat larger version and unknown flags as "origin unknown" */ 1282e1a5328SAmir Goldstein if (fh->version > OVL_FH_VERSION || fh->flags & ~OVL_FH_FLAG_ALL) 1292e1a5328SAmir Goldstein return -ENODATA; 1302e1a5328SAmir Goldstein 1312e1a5328SAmir Goldstein /* Treat endianness mismatch as "origin unknown" */ 1322e1a5328SAmir Goldstein if (!(fh->flags & OVL_FH_FLAG_ANY_ENDIAN) && 1332e1a5328SAmir Goldstein (fh->flags & OVL_FH_FLAG_BIG_ENDIAN) != OVL_FH_FLAG_CPU_ENDIAN) 1342e1a5328SAmir Goldstein return -ENODATA; 1352e1a5328SAmir Goldstein 1362e1a5328SAmir Goldstein return 0; 1372e1a5328SAmir Goldstein } 1382e1a5328SAmir Goldstein 13905122443SAmir Goldstein static struct ovl_fh *ovl_get_fh(struct dentry *dentry, const char *name) 140a9d01957SAmir Goldstein { 1412e1a5328SAmir Goldstein int res, err; 142a9d01957SAmir Goldstein struct ovl_fh *fh = NULL; 143a9d01957SAmir Goldstein 14405122443SAmir Goldstein res = vfs_getxattr(dentry, name, NULL, 0); 145a9d01957SAmir Goldstein if (res < 0) { 146a9d01957SAmir Goldstein if (res == -ENODATA || res == -EOPNOTSUPP) 147a9d01957SAmir Goldstein return NULL; 148a9d01957SAmir Goldstein goto fail; 149a9d01957SAmir Goldstein } 150a9d01957SAmir Goldstein /* Zero size value means "copied up but origin unknown" */ 151a9d01957SAmir Goldstein if (res == 0) 152a9d01957SAmir Goldstein return NULL; 153a9d01957SAmir Goldstein 1540ee931c4SMichal Hocko fh = kzalloc(res, GFP_KERNEL); 155a9d01957SAmir Goldstein if (!fh) 156a9d01957SAmir Goldstein return ERR_PTR(-ENOMEM); 157a9d01957SAmir Goldstein 15805122443SAmir Goldstein res = vfs_getxattr(dentry, name, fh, res); 159a9d01957SAmir Goldstein if (res < 0) 160a9d01957SAmir Goldstein goto fail; 161a9d01957SAmir Goldstein 1622e1a5328SAmir Goldstein err = ovl_check_fh_len(fh, res); 1632e1a5328SAmir Goldstein if (err < 0) { 1642e1a5328SAmir Goldstein if (err == -ENODATA) 165a9d01957SAmir Goldstein goto out; 1662e1a5328SAmir Goldstein goto invalid; 1672e1a5328SAmir Goldstein } 168a9d01957SAmir Goldstein 1698b88a2e6SAmir Goldstein return fh; 1708b88a2e6SAmir Goldstein 1718b88a2e6SAmir Goldstein out: 1728b88a2e6SAmir Goldstein kfree(fh); 1738b88a2e6SAmir Goldstein return NULL; 1748b88a2e6SAmir Goldstein 1758b88a2e6SAmir Goldstein fail: 1768b88a2e6SAmir Goldstein pr_warn_ratelimited("overlayfs: failed to get origin (%i)\n", res); 1778b88a2e6SAmir Goldstein goto out; 1788b88a2e6SAmir Goldstein invalid: 1798b88a2e6SAmir Goldstein pr_warn_ratelimited("overlayfs: invalid origin (%*phN)\n", res, fh); 1808b88a2e6SAmir Goldstein goto out; 1818b88a2e6SAmir Goldstein } 1828b88a2e6SAmir Goldstein 1838a22efa1SAmir Goldstein struct dentry *ovl_decode_real_fh(struct ovl_fh *fh, struct vfsmount *mnt, 1848a22efa1SAmir Goldstein bool connected) 1858b88a2e6SAmir Goldstein { 186e8f9e5b7SAmir Goldstein struct dentry *real; 1878b88a2e6SAmir Goldstein int bytes; 1888b88a2e6SAmir Goldstein 189a9d01957SAmir Goldstein /* 190a9d01957SAmir Goldstein * Make sure that the stored uuid matches the uuid of the lower 191a9d01957SAmir Goldstein * layer where file handle will be decoded. 192a9d01957SAmir Goldstein */ 19385787090SChristoph Hellwig if (!uuid_equal(&fh->uuid, &mnt->mnt_sb->s_uuid)) 1942e1a5328SAmir Goldstein return NULL; 195a9d01957SAmir Goldstein 1968b88a2e6SAmir Goldstein bytes = (fh->len - offsetof(struct ovl_fh, fid)); 197e8f9e5b7SAmir Goldstein real = exportfs_decode_fh(mnt, (struct fid *)fh->fid, 198a9d01957SAmir Goldstein bytes >> 2, (int)fh->type, 1998a22efa1SAmir Goldstein connected ? ovl_acceptable : NULL, mnt); 200e8f9e5b7SAmir Goldstein if (IS_ERR(real)) { 201e8f9e5b7SAmir Goldstein /* 202e8f9e5b7SAmir Goldstein * Treat stale file handle to lower file as "origin unknown". 203e8f9e5b7SAmir Goldstein * upper file handle could become stale when upper file is 204e8f9e5b7SAmir Goldstein * unlinked and this information is needed to handle stale 205e8f9e5b7SAmir Goldstein * index entries correctly. 206e8f9e5b7SAmir Goldstein */ 207e8f9e5b7SAmir Goldstein if (real == ERR_PTR(-ESTALE) && 208e8f9e5b7SAmir Goldstein !(fh->flags & OVL_FH_FLAG_PATH_UPPER)) 209e8f9e5b7SAmir Goldstein real = NULL; 210e8f9e5b7SAmir Goldstein return real; 211a9d01957SAmir Goldstein } 212a9d01957SAmir Goldstein 213e8f9e5b7SAmir Goldstein if (ovl_dentry_weird(real)) { 214e8f9e5b7SAmir Goldstein dput(real); 2152e1a5328SAmir Goldstein return NULL; 2162e1a5328SAmir Goldstein } 2172e1a5328SAmir Goldstein 218e8f9e5b7SAmir Goldstein return real; 219a9d01957SAmir Goldstein } 220a9d01957SAmir Goldstein 221ee1d6d37SAmir Goldstein static bool ovl_is_opaquedir(struct dentry *dentry) 222ee1d6d37SAmir Goldstein { 223ee1d6d37SAmir Goldstein return ovl_check_dir_xattr(dentry, OVL_XATTR_OPAQUE); 224ee1d6d37SAmir Goldstein } 225ee1d6d37SAmir Goldstein 226e28edc46SMiklos Szeredi static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d, 227e28edc46SMiklos Szeredi const char *name, unsigned int namelen, 22802b69b28SMiklos Szeredi size_t prelen, const char *post, 229e28edc46SMiklos Szeredi struct dentry **ret) 230e28edc46SMiklos Szeredi { 231e28edc46SMiklos Szeredi struct dentry *this; 232e28edc46SMiklos Szeredi int err; 233*102b0d11SVivek Goyal bool last_element = !post[0]; 234e28edc46SMiklos Szeredi 235e28edc46SMiklos Szeredi this = lookup_one_len_unlocked(name, base, namelen); 236e28edc46SMiklos Szeredi if (IS_ERR(this)) { 237e28edc46SMiklos Szeredi err = PTR_ERR(this); 238e28edc46SMiklos Szeredi this = NULL; 239e28edc46SMiklos Szeredi if (err == -ENOENT || err == -ENAMETOOLONG) 240e28edc46SMiklos Szeredi goto out; 241e28edc46SMiklos Szeredi goto out_err; 242e28edc46SMiklos Szeredi } 243e28edc46SMiklos Szeredi if (!this->d_inode) 244e28edc46SMiklos Szeredi goto put_and_out; 245e28edc46SMiklos Szeredi 246e28edc46SMiklos Szeredi if (ovl_dentry_weird(this)) { 247e28edc46SMiklos Szeredi /* Don't support traversing automounts and other weirdness */ 248e28edc46SMiklos Szeredi err = -EREMOTE; 249e28edc46SMiklos Szeredi goto out_err; 250e28edc46SMiklos Szeredi } 251e28edc46SMiklos Szeredi if (ovl_is_whiteout(this)) { 252e28edc46SMiklos Szeredi d->stop = d->opaque = true; 253e28edc46SMiklos Szeredi goto put_and_out; 254e28edc46SMiklos Szeredi } 255e28edc46SMiklos Szeredi if (!d_can_lookup(this)) { 256e28edc46SMiklos Szeredi d->stop = true; 257e28edc46SMiklos Szeredi if (d->is_dir) 258e28edc46SMiklos Szeredi goto put_and_out; 259e28edc46SMiklos Szeredi goto out; 260e28edc46SMiklos Szeredi } 261*102b0d11SVivek Goyal if (last_element) 262e28edc46SMiklos Szeredi d->is_dir = true; 263e9b77f90SVivek Goyal if (d->last) 264e9b77f90SVivek Goyal goto out; 265e9b77f90SVivek Goyal 266e9b77f90SVivek Goyal if (ovl_is_opaquedir(this)) { 267*102b0d11SVivek Goyal d->stop = true; 268*102b0d11SVivek Goyal if (last_element) 269*102b0d11SVivek Goyal d->opaque = true; 270e28edc46SMiklos Szeredi goto out; 271e28edc46SMiklos Szeredi } 27202b69b28SMiklos Szeredi err = ovl_check_redirect(this, d, prelen, post); 27302b69b28SMiklos Szeredi if (err) 27402b69b28SMiklos Szeredi goto out_err; 275e28edc46SMiklos Szeredi out: 276e28edc46SMiklos Szeredi *ret = this; 277e28edc46SMiklos Szeredi return 0; 278e28edc46SMiklos Szeredi 279e28edc46SMiklos Szeredi put_and_out: 280e28edc46SMiklos Szeredi dput(this); 281e28edc46SMiklos Szeredi this = NULL; 282e28edc46SMiklos Szeredi goto out; 283e28edc46SMiklos Szeredi 284e28edc46SMiklos Szeredi out_err: 285e28edc46SMiklos Szeredi dput(this); 286e28edc46SMiklos Szeredi return err; 287e28edc46SMiklos Szeredi } 288e28edc46SMiklos Szeredi 289e28edc46SMiklos Szeredi static int ovl_lookup_layer(struct dentry *base, struct ovl_lookup_data *d, 290e28edc46SMiklos Szeredi struct dentry **ret) 291e28edc46SMiklos Szeredi { 2924c7d0c9cSAmir Goldstein /* Counting down from the end, since the prefix can change */ 2934c7d0c9cSAmir Goldstein size_t rem = d->name.len - 1; 29402b69b28SMiklos Szeredi struct dentry *dentry = NULL; 29502b69b28SMiklos Szeredi int err; 29602b69b28SMiklos Szeredi 2974c7d0c9cSAmir Goldstein if (d->name.name[0] != '/') 29802b69b28SMiklos Szeredi return ovl_lookup_single(base, d, d->name.name, d->name.len, 29902b69b28SMiklos Szeredi 0, "", ret); 30002b69b28SMiklos Szeredi 3014c7d0c9cSAmir Goldstein while (!IS_ERR_OR_NULL(base) && d_can_lookup(base)) { 3024c7d0c9cSAmir Goldstein const char *s = d->name.name + d->name.len - rem; 30302b69b28SMiklos Szeredi const char *next = strchrnul(s, '/'); 3044c7d0c9cSAmir Goldstein size_t thislen = next - s; 3054c7d0c9cSAmir Goldstein bool end = !next[0]; 30602b69b28SMiklos Szeredi 3074c7d0c9cSAmir Goldstein /* Verify we did not go off the rails */ 3084c7d0c9cSAmir Goldstein if (WARN_ON(s[-1] != '/')) 30902b69b28SMiklos Szeredi return -EIO; 31002b69b28SMiklos Szeredi 3114c7d0c9cSAmir Goldstein err = ovl_lookup_single(base, d, s, thislen, 3124c7d0c9cSAmir Goldstein d->name.len - rem, next, &base); 31302b69b28SMiklos Szeredi dput(dentry); 31402b69b28SMiklos Szeredi if (err) 31502b69b28SMiklos Szeredi return err; 31602b69b28SMiklos Szeredi dentry = base; 3174c7d0c9cSAmir Goldstein if (end) 3184c7d0c9cSAmir Goldstein break; 3194c7d0c9cSAmir Goldstein 3204c7d0c9cSAmir Goldstein rem -= thislen + 1; 3214c7d0c9cSAmir Goldstein 3224c7d0c9cSAmir Goldstein if (WARN_ON(rem >= d->name.len)) 3234c7d0c9cSAmir Goldstein return -EIO; 32402b69b28SMiklos Szeredi } 32502b69b28SMiklos Szeredi *ret = dentry; 32602b69b28SMiklos Szeredi return 0; 327e28edc46SMiklos Szeredi } 328e28edc46SMiklos Szeredi 329a9d01957SAmir Goldstein 3308a22efa1SAmir Goldstein int ovl_check_origin_fh(struct ovl_fs *ofs, struct ovl_fh *fh, bool connected, 331f941866fSAmir Goldstein struct dentry *upperdentry, struct ovl_path **stackp) 332a9d01957SAmir Goldstein { 333f7d3dacaSAmir Goldstein struct dentry *origin = NULL; 334f7d3dacaSAmir Goldstein int i; 335a9d01957SAmir Goldstein 3361eff1a1dSAmir Goldstein for (i = 0; i < ofs->numlower; i++) { 3378a22efa1SAmir Goldstein origin = ovl_decode_real_fh(fh, ofs->lower_layers[i].mnt, 3388a22efa1SAmir Goldstein connected); 339f7d3dacaSAmir Goldstein if (origin) 340f7d3dacaSAmir Goldstein break; 341f7d3dacaSAmir Goldstein } 342f7d3dacaSAmir Goldstein 343f7d3dacaSAmir Goldstein if (!origin) 3442e1a5328SAmir Goldstein return -ESTALE; 3452e1a5328SAmir Goldstein else if (IS_ERR(origin)) 3462e1a5328SAmir Goldstein return PTR_ERR(origin); 347f7d3dacaSAmir Goldstein 348f941866fSAmir Goldstein if (upperdentry && !ovl_is_whiteout(upperdentry) && 3492e1a5328SAmir Goldstein ((d_inode(origin)->i_mode ^ d_inode(upperdentry)->i_mode) & S_IFMT)) 3502e1a5328SAmir Goldstein goto invalid; 3512e1a5328SAmir Goldstein 352415543d5SAmir Goldstein if (!*stackp) 353b9343632SChandan Rajendra *stackp = kmalloc(sizeof(struct ovl_path), GFP_KERNEL); 354a9d01957SAmir Goldstein if (!*stackp) { 355a9d01957SAmir Goldstein dput(origin); 356a9d01957SAmir Goldstein return -ENOMEM; 357a9d01957SAmir Goldstein } 3581eff1a1dSAmir Goldstein **stackp = (struct ovl_path){ 3591eff1a1dSAmir Goldstein .dentry = origin, 3601eff1a1dSAmir Goldstein .layer = &ofs->lower_layers[i] 3611eff1a1dSAmir Goldstein }; 362a9d01957SAmir Goldstein 363a9d01957SAmir Goldstein return 0; 3642e1a5328SAmir Goldstein 3652e1a5328SAmir Goldstein invalid: 3662e1a5328SAmir Goldstein pr_warn_ratelimited("overlayfs: invalid origin (%pd2, ftype=%x, origin ftype=%x).\n", 3672e1a5328SAmir Goldstein upperdentry, d_inode(upperdentry)->i_mode & S_IFMT, 3682e1a5328SAmir Goldstein d_inode(origin)->i_mode & S_IFMT); 3692e1a5328SAmir Goldstein dput(origin); 3702e1a5328SAmir Goldstein return -EIO; 3712e1a5328SAmir Goldstein } 3722e1a5328SAmir Goldstein 3731eff1a1dSAmir Goldstein static int ovl_check_origin(struct ovl_fs *ofs, struct dentry *upperdentry, 3742e1a5328SAmir Goldstein struct ovl_path **stackp, unsigned int *ctrp) 3752e1a5328SAmir Goldstein { 37605122443SAmir Goldstein struct ovl_fh *fh = ovl_get_fh(upperdentry, OVL_XATTR_ORIGIN); 3772e1a5328SAmir Goldstein int err; 3782e1a5328SAmir Goldstein 3792e1a5328SAmir Goldstein if (IS_ERR_OR_NULL(fh)) 3802e1a5328SAmir Goldstein return PTR_ERR(fh); 3812e1a5328SAmir Goldstein 3828a22efa1SAmir Goldstein err = ovl_check_origin_fh(ofs, fh, false, upperdentry, stackp); 3832e1a5328SAmir Goldstein kfree(fh); 3842e1a5328SAmir Goldstein 3852e1a5328SAmir Goldstein if (err) { 3862e1a5328SAmir Goldstein if (err == -ESTALE) 3872e1a5328SAmir Goldstein return 0; 3882e1a5328SAmir Goldstein return err; 3892e1a5328SAmir Goldstein } 3902e1a5328SAmir Goldstein 3912e1a5328SAmir Goldstein if (WARN_ON(*ctrp)) 3922e1a5328SAmir Goldstein return -EIO; 3932e1a5328SAmir Goldstein 3942e1a5328SAmir Goldstein *ctrp = 1; 3952e1a5328SAmir Goldstein return 0; 396a9d01957SAmir Goldstein } 397a9d01957SAmir Goldstein 398bbb1e54dSMiklos Szeredi /* 39905122443SAmir Goldstein * Verify that @fh matches the file handle stored in xattr @name. 4008b88a2e6SAmir Goldstein * Return 0 on match, -ESTALE on mismatch, < 0 on error. 4018b88a2e6SAmir Goldstein */ 40205122443SAmir Goldstein static int ovl_verify_fh(struct dentry *dentry, const char *name, 40305122443SAmir Goldstein const struct ovl_fh *fh) 4048b88a2e6SAmir Goldstein { 40505122443SAmir Goldstein struct ovl_fh *ofh = ovl_get_fh(dentry, name); 4068b88a2e6SAmir Goldstein int err = 0; 4078b88a2e6SAmir Goldstein 4088b88a2e6SAmir Goldstein if (!ofh) 4098b88a2e6SAmir Goldstein return -ENODATA; 4108b88a2e6SAmir Goldstein 4118b88a2e6SAmir Goldstein if (IS_ERR(ofh)) 4128b88a2e6SAmir Goldstein return PTR_ERR(ofh); 4138b88a2e6SAmir Goldstein 4148b88a2e6SAmir Goldstein if (fh->len != ofh->len || memcmp(fh, ofh, fh->len)) 4158b88a2e6SAmir Goldstein err = -ESTALE; 4168b88a2e6SAmir Goldstein 4178b88a2e6SAmir Goldstein kfree(ofh); 4188b88a2e6SAmir Goldstein return err; 4198b88a2e6SAmir Goldstein } 4208b88a2e6SAmir Goldstein 4218b88a2e6SAmir Goldstein /* 42205122443SAmir Goldstein * Verify that @real dentry matches the file handle stored in xattr @name. 4238b88a2e6SAmir Goldstein * 42405122443SAmir Goldstein * If @set is true and there is no stored file handle, encode @real and store 42505122443SAmir Goldstein * file handle in xattr @name. 4268b88a2e6SAmir Goldstein * 42705122443SAmir Goldstein * Return 0 on match, -ESTALE on mismatch, -ENODATA on no xattr, < 0 on error. 4288b88a2e6SAmir Goldstein */ 42905122443SAmir Goldstein int ovl_verify_set_fh(struct dentry *dentry, const char *name, 43005122443SAmir Goldstein struct dentry *real, bool is_upper, bool set) 4318b88a2e6SAmir Goldstein { 4328b88a2e6SAmir Goldstein struct inode *inode; 4338b88a2e6SAmir Goldstein struct ovl_fh *fh; 4348b88a2e6SAmir Goldstein int err; 4358b88a2e6SAmir Goldstein 4365b2cccd3SAmir Goldstein fh = ovl_encode_real_fh(real, is_upper); 4378b88a2e6SAmir Goldstein err = PTR_ERR(fh); 4388b88a2e6SAmir Goldstein if (IS_ERR(fh)) 4398b88a2e6SAmir Goldstein goto fail; 4408b88a2e6SAmir Goldstein 44105122443SAmir Goldstein err = ovl_verify_fh(dentry, name, fh); 4428b88a2e6SAmir Goldstein if (set && err == -ENODATA) 44305122443SAmir Goldstein err = ovl_do_setxattr(dentry, name, fh, fh->len, 0); 4448b88a2e6SAmir Goldstein if (err) 4458b88a2e6SAmir Goldstein goto fail; 4468b88a2e6SAmir Goldstein 4478b88a2e6SAmir Goldstein out: 4488b88a2e6SAmir Goldstein kfree(fh); 4498b88a2e6SAmir Goldstein return err; 4508b88a2e6SAmir Goldstein 4518b88a2e6SAmir Goldstein fail: 45205122443SAmir Goldstein inode = d_inode(real); 45305122443SAmir Goldstein pr_warn_ratelimited("overlayfs: failed to verify %s (%pd2, ino=%lu, err=%i)\n", 45405122443SAmir Goldstein is_upper ? "upper" : "origin", real, 45505122443SAmir Goldstein inode ? inode->i_ino : 0, err); 4568b88a2e6SAmir Goldstein goto out; 4578b88a2e6SAmir Goldstein } 4588b88a2e6SAmir Goldstein 459e8f9e5b7SAmir Goldstein /* Get upper dentry from index */ 4603b0bfc6eSAmir Goldstein struct dentry *ovl_index_upper(struct ovl_fs *ofs, struct dentry *index) 461e8f9e5b7SAmir Goldstein { 462e8f9e5b7SAmir Goldstein struct ovl_fh *fh; 463e8f9e5b7SAmir Goldstein struct dentry *upper; 464e8f9e5b7SAmir Goldstein 465e8f9e5b7SAmir Goldstein if (!d_is_dir(index)) 466e8f9e5b7SAmir Goldstein return dget(index); 467e8f9e5b7SAmir Goldstein 468e8f9e5b7SAmir Goldstein fh = ovl_get_fh(index, OVL_XATTR_UPPER); 469e8f9e5b7SAmir Goldstein if (IS_ERR_OR_NULL(fh)) 470e8f9e5b7SAmir Goldstein return ERR_CAST(fh); 471e8f9e5b7SAmir Goldstein 4728a22efa1SAmir Goldstein upper = ovl_decode_real_fh(fh, ofs->upper_mnt, true); 473e8f9e5b7SAmir Goldstein kfree(fh); 474e8f9e5b7SAmir Goldstein 475e8f9e5b7SAmir Goldstein if (IS_ERR_OR_NULL(upper)) 476e8f9e5b7SAmir Goldstein return upper ?: ERR_PTR(-ESTALE); 477e8f9e5b7SAmir Goldstein 478e8f9e5b7SAmir Goldstein if (!d_is_dir(upper)) { 479e8f9e5b7SAmir Goldstein pr_warn_ratelimited("overlayfs: invalid index upper (%pd2, upper=%pd2).\n", 480e8f9e5b7SAmir Goldstein index, upper); 481e8f9e5b7SAmir Goldstein dput(upper); 482e8f9e5b7SAmir Goldstein return ERR_PTR(-EIO); 483e8f9e5b7SAmir Goldstein } 484e8f9e5b7SAmir Goldstein 485e8f9e5b7SAmir Goldstein return upper; 486e8f9e5b7SAmir Goldstein } 487e8f9e5b7SAmir Goldstein 4889ee60ce2SAmir Goldstein /* Is this a leftover from create/whiteout of directory index entry? */ 4899ee60ce2SAmir Goldstein static bool ovl_is_temp_index(struct dentry *index) 4909ee60ce2SAmir Goldstein { 4919ee60ce2SAmir Goldstein return index->d_name.name[0] == '#'; 4929ee60ce2SAmir Goldstein } 4939ee60ce2SAmir Goldstein 4948b88a2e6SAmir Goldstein /* 495415543d5SAmir Goldstein * Verify that an index entry name matches the origin file handle stored in 496415543d5SAmir Goldstein * OVL_XATTR_ORIGIN and that origin file handle can be decoded to lower path. 497415543d5SAmir Goldstein * Return 0 on match, -ESTALE on mismatch or stale origin, < 0 on error. 498415543d5SAmir Goldstein */ 4991eff1a1dSAmir Goldstein int ovl_verify_index(struct ovl_fs *ofs, struct dentry *index) 500415543d5SAmir Goldstein { 501415543d5SAmir Goldstein struct ovl_fh *fh = NULL; 502415543d5SAmir Goldstein size_t len; 503b9343632SChandan Rajendra struct ovl_path origin = { }; 504b9343632SChandan Rajendra struct ovl_path *stack = &origin; 505e8f9e5b7SAmir Goldstein struct dentry *upper = NULL; 506415543d5SAmir Goldstein int err; 507415543d5SAmir Goldstein 508415543d5SAmir Goldstein if (!d_inode(index)) 509415543d5SAmir Goldstein return 0; 510415543d5SAmir Goldstein 5119ee60ce2SAmir Goldstein /* Cleanup leftover from index create/cleanup attempt */ 5129ee60ce2SAmir Goldstein err = -ESTALE; 5139ee60ce2SAmir Goldstein if (ovl_is_temp_index(index)) 5149ee60ce2SAmir Goldstein goto fail; 5159ee60ce2SAmir Goldstein 516fa0096e3SAmir Goldstein err = -EINVAL; 517415543d5SAmir Goldstein if (index->d_name.len < sizeof(struct ovl_fh)*2) 518415543d5SAmir Goldstein goto fail; 519415543d5SAmir Goldstein 520415543d5SAmir Goldstein err = -ENOMEM; 521415543d5SAmir Goldstein len = index->d_name.len / 2; 5220ee931c4SMichal Hocko fh = kzalloc(len, GFP_KERNEL); 523415543d5SAmir Goldstein if (!fh) 524415543d5SAmir Goldstein goto fail; 525415543d5SAmir Goldstein 526415543d5SAmir Goldstein err = -EINVAL; 5272e1a5328SAmir Goldstein if (hex2bin((u8 *)fh, index->d_name.name, len)) 5282e1a5328SAmir Goldstein goto fail; 5292e1a5328SAmir Goldstein 5302e1a5328SAmir Goldstein err = ovl_check_fh_len(fh, len); 5312e1a5328SAmir Goldstein if (err) 532415543d5SAmir Goldstein goto fail; 533415543d5SAmir Goldstein 5347db25d36SAmir Goldstein /* 5357db25d36SAmir Goldstein * Whiteout index entries are used as an indication that an exported 5367db25d36SAmir Goldstein * overlay file handle should be treated as stale (i.e. after unlink 5377db25d36SAmir Goldstein * of the overlay inode). These entries contain no origin xattr. 5387db25d36SAmir Goldstein */ 5397db25d36SAmir Goldstein if (ovl_is_whiteout(index)) 5407db25d36SAmir Goldstein goto out; 5417db25d36SAmir Goldstein 542e8f9e5b7SAmir Goldstein /* 543e8f9e5b7SAmir Goldstein * Verifying directory index entries are not stale is expensive, so 544e8f9e5b7SAmir Goldstein * only verify stale dir index if NFS export is enabled. 545e8f9e5b7SAmir Goldstein */ 546e8f9e5b7SAmir Goldstein if (d_is_dir(index) && !ofs->config.nfs_export) 547e8f9e5b7SAmir Goldstein goto out; 548e8f9e5b7SAmir Goldstein 549e8f9e5b7SAmir Goldstein /* 550e8f9e5b7SAmir Goldstein * Directory index entries should have 'upper' xattr pointing to the 551e8f9e5b7SAmir Goldstein * real upper dir. Non-dir index entries are hardlinks to the upper 552e8f9e5b7SAmir Goldstein * real inode. For non-dir index, we can read the copy up origin xattr 553e8f9e5b7SAmir Goldstein * directly from the index dentry, but for dir index we first need to 554e8f9e5b7SAmir Goldstein * decode the upper directory. 555e8f9e5b7SAmir Goldstein */ 556e8f9e5b7SAmir Goldstein upper = ovl_index_upper(ofs, index); 557e8f9e5b7SAmir Goldstein if (IS_ERR_OR_NULL(upper)) { 558e8f9e5b7SAmir Goldstein err = PTR_ERR(upper); 55924f0b172SAmir Goldstein /* 56024f0b172SAmir Goldstein * Directory index entries with no 'upper' xattr need to be 56124f0b172SAmir Goldstein * removed. When dir index entry has a stale 'upper' xattr, 56224f0b172SAmir Goldstein * we assume that upper dir was removed and we treat the dir 56324f0b172SAmir Goldstein * index as orphan entry that needs to be whited out. 56424f0b172SAmir Goldstein */ 56524f0b172SAmir Goldstein if (err == -ESTALE) 56624f0b172SAmir Goldstein goto orphan; 56724f0b172SAmir Goldstein else if (!err) 568e8f9e5b7SAmir Goldstein err = -ESTALE; 569e8f9e5b7SAmir Goldstein goto fail; 570e8f9e5b7SAmir Goldstein } 571e8f9e5b7SAmir Goldstein 572e8f9e5b7SAmir Goldstein err = ovl_verify_fh(upper, OVL_XATTR_ORIGIN, fh); 573e8f9e5b7SAmir Goldstein dput(upper); 574415543d5SAmir Goldstein if (err) 575415543d5SAmir Goldstein goto fail; 576415543d5SAmir Goldstein 577e8f9e5b7SAmir Goldstein /* Check if non-dir index is orphan and don't warn before cleaning it */ 578e8f9e5b7SAmir Goldstein if (!d_is_dir(index) && d_inode(index)->i_nlink == 1) { 5798a22efa1SAmir Goldstein err = ovl_check_origin_fh(ofs, fh, false, index, &stack); 580415543d5SAmir Goldstein if (err) 581415543d5SAmir Goldstein goto fail; 582415543d5SAmir Goldstein 583e8f9e5b7SAmir Goldstein if (ovl_get_nlink(origin.dentry, index, 0) == 0) 58424f0b172SAmir Goldstein goto orphan; 585e8f9e5b7SAmir Goldstein } 586caf70cb2SAmir Goldstein 587415543d5SAmir Goldstein out: 588e8f9e5b7SAmir Goldstein dput(origin.dentry); 589415543d5SAmir Goldstein kfree(fh); 590415543d5SAmir Goldstein return err; 591415543d5SAmir Goldstein 592415543d5SAmir Goldstein fail: 59361b67471SAmir Goldstein pr_warn_ratelimited("overlayfs: failed to verify index (%pd2, ftype=%x, err=%i)\n", 59461b67471SAmir Goldstein index, d_inode(index)->i_mode & S_IFMT, err); 595415543d5SAmir Goldstein goto out; 59624f0b172SAmir Goldstein 59724f0b172SAmir Goldstein orphan: 59824f0b172SAmir Goldstein pr_warn_ratelimited("overlayfs: orphan index entry (%pd2, ftype=%x, nlink=%u)\n", 59924f0b172SAmir Goldstein index, d_inode(index)->i_mode & S_IFMT, 60024f0b172SAmir Goldstein d_inode(index)->i_nlink); 60124f0b172SAmir Goldstein err = -ENOENT; 60224f0b172SAmir Goldstein goto out; 603415543d5SAmir Goldstein } 604415543d5SAmir Goldstein 60591ffe7beSAmir Goldstein static int ovl_get_index_name_fh(struct ovl_fh *fh, struct qstr *name) 60691ffe7beSAmir Goldstein { 60791ffe7beSAmir Goldstein char *n, *s; 60891ffe7beSAmir Goldstein 60991ffe7beSAmir Goldstein n = kzalloc(fh->len * 2, GFP_KERNEL); 61091ffe7beSAmir Goldstein if (!n) 61191ffe7beSAmir Goldstein return -ENOMEM; 61291ffe7beSAmir Goldstein 61391ffe7beSAmir Goldstein s = bin2hex(n, fh, fh->len); 61491ffe7beSAmir Goldstein *name = (struct qstr) QSTR_INIT(n, s - n); 61591ffe7beSAmir Goldstein 61691ffe7beSAmir Goldstein return 0; 61791ffe7beSAmir Goldstein 61891ffe7beSAmir Goldstein } 61991ffe7beSAmir Goldstein 620415543d5SAmir Goldstein /* 621359f392cSAmir Goldstein * Lookup in indexdir for the index entry of a lower real inode or a copy up 622359f392cSAmir Goldstein * origin inode. The index entry name is the hex representation of the lower 623359f392cSAmir Goldstein * inode file handle. 624359f392cSAmir Goldstein * 625359f392cSAmir Goldstein * If the index dentry in negative, then either no lower aliases have been 626359f392cSAmir Goldstein * copied up yet, or aliases have been copied up in older kernels and are 627359f392cSAmir Goldstein * not indexed. 628359f392cSAmir Goldstein * 629359f392cSAmir Goldstein * If the index dentry for a copy up origin inode is positive, but points 630359f392cSAmir Goldstein * to an inode different than the upper inode, then either the upper inode 631359f392cSAmir Goldstein * has been copied up and not indexed or it was indexed, but since then 632359f392cSAmir Goldstein * index dir was cleared. Either way, that index cannot be used to indentify 633359f392cSAmir Goldstein * the overlay inode. 634359f392cSAmir Goldstein */ 635359f392cSAmir Goldstein int ovl_get_index_name(struct dentry *origin, struct qstr *name) 636359f392cSAmir Goldstein { 637359f392cSAmir Goldstein struct ovl_fh *fh; 63891ffe7beSAmir Goldstein int err; 639359f392cSAmir Goldstein 6405b2cccd3SAmir Goldstein fh = ovl_encode_real_fh(origin, false); 641359f392cSAmir Goldstein if (IS_ERR(fh)) 642359f392cSAmir Goldstein return PTR_ERR(fh); 643359f392cSAmir Goldstein 64491ffe7beSAmir Goldstein err = ovl_get_index_name_fh(fh, name); 64591ffe7beSAmir Goldstein 646359f392cSAmir Goldstein kfree(fh); 647359f392cSAmir Goldstein return err; 64891ffe7beSAmir Goldstein } 649359f392cSAmir Goldstein 65091ffe7beSAmir Goldstein /* Lookup index by file handle for NFS export */ 65191ffe7beSAmir Goldstein struct dentry *ovl_get_index_fh(struct ovl_fs *ofs, struct ovl_fh *fh) 65291ffe7beSAmir Goldstein { 65391ffe7beSAmir Goldstein struct dentry *index; 65491ffe7beSAmir Goldstein struct qstr name; 65591ffe7beSAmir Goldstein int err; 65691ffe7beSAmir Goldstein 65791ffe7beSAmir Goldstein err = ovl_get_index_name_fh(fh, &name); 65891ffe7beSAmir Goldstein if (err) 65991ffe7beSAmir Goldstein return ERR_PTR(err); 66091ffe7beSAmir Goldstein 66191ffe7beSAmir Goldstein index = lookup_one_len_unlocked(name.name, ofs->indexdir, name.len); 66291ffe7beSAmir Goldstein kfree(name.name); 66391ffe7beSAmir Goldstein if (IS_ERR(index)) { 66491ffe7beSAmir Goldstein if (PTR_ERR(index) == -ENOENT) 66591ffe7beSAmir Goldstein index = NULL; 66691ffe7beSAmir Goldstein return index; 66791ffe7beSAmir Goldstein } 66891ffe7beSAmir Goldstein 66991ffe7beSAmir Goldstein if (d_is_negative(index)) 67091ffe7beSAmir Goldstein err = 0; 67191ffe7beSAmir Goldstein else if (ovl_is_whiteout(index)) 67291ffe7beSAmir Goldstein err = -ESTALE; 67391ffe7beSAmir Goldstein else if (ovl_dentry_weird(index)) 67491ffe7beSAmir Goldstein err = -EIO; 67591ffe7beSAmir Goldstein else 67691ffe7beSAmir Goldstein return index; 67791ffe7beSAmir Goldstein 67891ffe7beSAmir Goldstein dput(index); 67991ffe7beSAmir Goldstein return ERR_PTR(err); 680359f392cSAmir Goldstein } 681359f392cSAmir Goldstein 68206170154SAmir Goldstein struct dentry *ovl_lookup_index(struct ovl_fs *ofs, struct dentry *upper, 68306170154SAmir Goldstein struct dentry *origin, bool verify) 684359f392cSAmir Goldstein { 685359f392cSAmir Goldstein struct dentry *index; 686359f392cSAmir Goldstein struct inode *inode; 687359f392cSAmir Goldstein struct qstr name; 688ad1d615cSAmir Goldstein bool is_dir = d_is_dir(origin); 689359f392cSAmir Goldstein int err; 690359f392cSAmir Goldstein 691359f392cSAmir Goldstein err = ovl_get_index_name(origin, &name); 692359f392cSAmir Goldstein if (err) 693359f392cSAmir Goldstein return ERR_PTR(err); 694359f392cSAmir Goldstein 695359f392cSAmir Goldstein index = lookup_one_len_unlocked(name.name, ofs->indexdir, name.len); 696359f392cSAmir Goldstein if (IS_ERR(index)) { 697e0082a0fSAmir Goldstein err = PTR_ERR(index); 6987937a56fSAmir Goldstein if (err == -ENOENT) { 6997937a56fSAmir Goldstein index = NULL; 7007937a56fSAmir Goldstein goto out; 7017937a56fSAmir Goldstein } 702359f392cSAmir Goldstein pr_warn_ratelimited("overlayfs: failed inode index lookup (ino=%lu, key=%*s, err=%i);\n" 703359f392cSAmir Goldstein "overlayfs: mount with '-o index=off' to disable inodes index.\n", 704359f392cSAmir Goldstein d_inode(origin)->i_ino, name.len, name.name, 705359f392cSAmir Goldstein err); 706359f392cSAmir Goldstein goto out; 707359f392cSAmir Goldstein } 708359f392cSAmir Goldstein 7090e082555SAmir Goldstein inode = d_inode(index); 710359f392cSAmir Goldstein if (d_is_negative(index)) { 7116eaf0111SAmir Goldstein goto out_dput; 71206170154SAmir Goldstein } else if (ovl_is_whiteout(index) && !verify) { 71306170154SAmir Goldstein /* 71406170154SAmir Goldstein * When index lookup is called with !verify for decoding an 71506170154SAmir Goldstein * overlay file handle, a whiteout index implies that decode 71606170154SAmir Goldstein * should treat file handle as stale and no need to print a 71706170154SAmir Goldstein * warning about it. 71806170154SAmir Goldstein */ 71906170154SAmir Goldstein dput(index); 72006170154SAmir Goldstein index = ERR_PTR(-ESTALE); 72106170154SAmir Goldstein goto out; 7220e082555SAmir Goldstein } else if (ovl_dentry_weird(index) || ovl_is_whiteout(index) || 7230e082555SAmir Goldstein ((inode->i_mode ^ d_inode(origin)->i_mode) & S_IFMT)) { 7240e082555SAmir Goldstein /* 7250e082555SAmir Goldstein * Index should always be of the same file type as origin 7260e082555SAmir Goldstein * except for the case of a whiteout index. A whiteout 7270e082555SAmir Goldstein * index should only exist if all lower aliases have been 7280e082555SAmir Goldstein * unlinked, which means that finding a lower origin on lookup 7290e082555SAmir Goldstein * whose index is a whiteout should be treated as an error. 7300e082555SAmir Goldstein */ 7310e082555SAmir Goldstein pr_warn_ratelimited("overlayfs: bad index found (index=%pd2, ftype=%x, origin ftype=%x).\n", 7320e082555SAmir Goldstein index, d_inode(index)->i_mode & S_IFMT, 7330e082555SAmir Goldstein d_inode(origin)->i_mode & S_IFMT); 734359f392cSAmir Goldstein goto fail; 73506170154SAmir Goldstein } else if (is_dir && verify) { 736ad1d615cSAmir Goldstein if (!upper) { 737ad1d615cSAmir Goldstein pr_warn_ratelimited("overlayfs: suspected uncovered redirected dir found (origin=%pd2, index=%pd2).\n", 738ad1d615cSAmir Goldstein origin, index); 739ad1d615cSAmir Goldstein goto fail; 740359f392cSAmir Goldstein } 741359f392cSAmir Goldstein 742ad1d615cSAmir Goldstein /* Verify that dir index 'upper' xattr points to upper dir */ 743ad1d615cSAmir Goldstein err = ovl_verify_upper(index, upper, false); 744ad1d615cSAmir Goldstein if (err) { 745ad1d615cSAmir Goldstein if (err == -ESTALE) { 746ad1d615cSAmir Goldstein pr_warn_ratelimited("overlayfs: suspected multiply redirected dir found (upper=%pd2, origin=%pd2, index=%pd2).\n", 747ad1d615cSAmir Goldstein upper, origin, index); 748ad1d615cSAmir Goldstein } 749ad1d615cSAmir Goldstein goto fail; 750ad1d615cSAmir Goldstein } 751ad1d615cSAmir Goldstein } else if (upper && d_inode(upper) != inode) { 752ad1d615cSAmir Goldstein goto out_dput; 753ad1d615cSAmir Goldstein } 754359f392cSAmir Goldstein out: 755359f392cSAmir Goldstein kfree(name.name); 756359f392cSAmir Goldstein return index; 757359f392cSAmir Goldstein 7586eaf0111SAmir Goldstein out_dput: 7596eaf0111SAmir Goldstein dput(index); 7606eaf0111SAmir Goldstein index = NULL; 7616eaf0111SAmir Goldstein goto out; 7626eaf0111SAmir Goldstein 763359f392cSAmir Goldstein fail: 764359f392cSAmir Goldstein dput(index); 765359f392cSAmir Goldstein index = ERR_PTR(-EIO); 766359f392cSAmir Goldstein goto out; 767359f392cSAmir Goldstein } 768359f392cSAmir Goldstein 769359f392cSAmir Goldstein /* 770bbb1e54dSMiklos Szeredi * Returns next layer in stack starting from top. 771bbb1e54dSMiklos Szeredi * Returns -1 if this is the last layer. 772bbb1e54dSMiklos Szeredi */ 773bbb1e54dSMiklos Szeredi int ovl_path_next(int idx, struct dentry *dentry, struct path *path) 774bbb1e54dSMiklos Szeredi { 775bbb1e54dSMiklos Szeredi struct ovl_entry *oe = dentry->d_fsdata; 776bbb1e54dSMiklos Szeredi 777bbb1e54dSMiklos Szeredi BUG_ON(idx < 0); 778bbb1e54dSMiklos Szeredi if (idx == 0) { 779bbb1e54dSMiklos Szeredi ovl_path_upper(dentry, path); 780bbb1e54dSMiklos Szeredi if (path->dentry) 781bbb1e54dSMiklos Szeredi return oe->numlower ? 1 : -1; 782bbb1e54dSMiklos Szeredi idx++; 783bbb1e54dSMiklos Szeredi } 784bbb1e54dSMiklos Szeredi BUG_ON(idx > oe->numlower); 785b9343632SChandan Rajendra path->dentry = oe->lowerstack[idx - 1].dentry; 786b9343632SChandan Rajendra path->mnt = oe->lowerstack[idx - 1].layer->mnt; 787bbb1e54dSMiklos Szeredi 788bbb1e54dSMiklos Szeredi return (idx < oe->numlower) ? idx + 1 : -1; 789bbb1e54dSMiklos Szeredi } 790bbb1e54dSMiklos Szeredi 7919678e630SAmir Goldstein /* Fix missing 'origin' xattr */ 7929678e630SAmir Goldstein static int ovl_fix_origin(struct dentry *dentry, struct dentry *lower, 7939678e630SAmir Goldstein struct dentry *upper) 7949678e630SAmir Goldstein { 7959678e630SAmir Goldstein int err; 7969678e630SAmir Goldstein 7979678e630SAmir Goldstein if (ovl_check_origin_xattr(upper)) 7989678e630SAmir Goldstein return 0; 7999678e630SAmir Goldstein 8009678e630SAmir Goldstein err = ovl_want_write(dentry); 8019678e630SAmir Goldstein if (err) 8029678e630SAmir Goldstein return err; 8039678e630SAmir Goldstein 8049678e630SAmir Goldstein err = ovl_set_origin(dentry, lower, upper); 8059678e630SAmir Goldstein if (!err) 8069678e630SAmir Goldstein err = ovl_set_impure(dentry->d_parent, upper->d_parent); 8079678e630SAmir Goldstein 8089678e630SAmir Goldstein ovl_drop_write(dentry); 8099678e630SAmir Goldstein return err; 8109678e630SAmir Goldstein } 8119678e630SAmir Goldstein 812bbb1e54dSMiklos Szeredi struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, 813bbb1e54dSMiklos Szeredi unsigned int flags) 814bbb1e54dSMiklos Szeredi { 815bbb1e54dSMiklos Szeredi struct ovl_entry *oe; 816bbb1e54dSMiklos Szeredi const struct cred *old_cred; 8176b2d5fe4SMiklos Szeredi struct ovl_fs *ofs = dentry->d_sb->s_fs_info; 818bbb1e54dSMiklos Szeredi struct ovl_entry *poe = dentry->d_parent->d_fsdata; 819c22205d0SAmir Goldstein struct ovl_entry *roe = dentry->d_sb->s_root->d_fsdata; 820b9343632SChandan Rajendra struct ovl_path *stack = NULL; 821bbb1e54dSMiklos Szeredi struct dentry *upperdir, *upperdentry = NULL; 822ad1d615cSAmir Goldstein struct dentry *origin = NULL; 823359f392cSAmir Goldstein struct dentry *index = NULL; 824bbb1e54dSMiklos Szeredi unsigned int ctr = 0; 825bbb1e54dSMiklos Szeredi struct inode *inode = NULL; 826bbb1e54dSMiklos Szeredi bool upperopaque = false; 82702b69b28SMiklos Szeredi char *upperredirect = NULL; 828bbb1e54dSMiklos Szeredi struct dentry *this; 829bbb1e54dSMiklos Szeredi unsigned int i; 830bbb1e54dSMiklos Szeredi int err; 831e28edc46SMiklos Szeredi struct ovl_lookup_data d = { 832e28edc46SMiklos Szeredi .name = dentry->d_name, 833e28edc46SMiklos Szeredi .is_dir = false, 834e28edc46SMiklos Szeredi .opaque = false, 835e28edc46SMiklos Szeredi .stop = false, 836452061fdSVivek Goyal .last = ofs->config.redirect_follow ? false : !poe->numlower, 83702b69b28SMiklos Szeredi .redirect = NULL, 838e28edc46SMiklos Szeredi }; 839bbb1e54dSMiklos Szeredi 8406b2d5fe4SMiklos Szeredi if (dentry->d_name.len > ofs->namelen) 8416b2d5fe4SMiklos Szeredi return ERR_PTR(-ENAMETOOLONG); 8426b2d5fe4SMiklos Szeredi 843bbb1e54dSMiklos Szeredi old_cred = ovl_override_creds(dentry->d_sb); 84409d8b586SMiklos Szeredi upperdir = ovl_dentry_upper(dentry->d_parent); 845bbb1e54dSMiklos Szeredi if (upperdir) { 846e28edc46SMiklos Szeredi err = ovl_lookup_layer(upperdir, &d, &upperdentry); 847e28edc46SMiklos Szeredi if (err) 848bbb1e54dSMiklos Szeredi goto out; 849bbb1e54dSMiklos Szeredi 850e28edc46SMiklos Szeredi if (upperdentry && unlikely(ovl_dentry_remote(upperdentry))) { 851e28edc46SMiklos Szeredi dput(upperdentry); 852bbb1e54dSMiklos Szeredi err = -EREMOTE; 853bbb1e54dSMiklos Szeredi goto out; 854bbb1e54dSMiklos Szeredi } 855a9d01957SAmir Goldstein if (upperdentry && !d.is_dir) { 856a9d01957SAmir Goldstein BUG_ON(!d.stop || d.redirect); 857f7d3dacaSAmir Goldstein /* 858f7d3dacaSAmir Goldstein * Lookup copy up origin by decoding origin file handle. 859f7d3dacaSAmir Goldstein * We may get a disconnected dentry, which is fine, 860f7d3dacaSAmir Goldstein * because we only need to hold the origin inode in 861f7d3dacaSAmir Goldstein * cache and use its inode number. We may even get a 862f7d3dacaSAmir Goldstein * connected dentry, that is not under any of the lower 863f7d3dacaSAmir Goldstein * layers root. That is also fine for using it's inode 864f7d3dacaSAmir Goldstein * number - it's the same as if we held a reference 865f7d3dacaSAmir Goldstein * to a dentry in lower layer that was moved under us. 866f7d3dacaSAmir Goldstein */ 8671eff1a1dSAmir Goldstein err = ovl_check_origin(ofs, upperdentry, &stack, &ctr); 868a9d01957SAmir Goldstein if (err) 8695455f92bSVivek Goyal goto out_put_upper; 870a9d01957SAmir Goldstein } 87102b69b28SMiklos Szeredi 87202b69b28SMiklos Szeredi if (d.redirect) { 8730ce5cdc9SDan Carpenter err = -ENOMEM; 87402b69b28SMiklos Szeredi upperredirect = kstrdup(d.redirect, GFP_KERNEL); 87502b69b28SMiklos Szeredi if (!upperredirect) 87602b69b28SMiklos Szeredi goto out_put_upper; 87702b69b28SMiklos Szeredi if (d.redirect[0] == '/') 878c22205d0SAmir Goldstein poe = roe; 87902b69b28SMiklos Szeredi } 880e28edc46SMiklos Szeredi upperopaque = d.opaque; 881bbb1e54dSMiklos Szeredi } 882bbb1e54dSMiklos Szeredi 883e28edc46SMiklos Szeredi if (!d.stop && poe->numlower) { 884bbb1e54dSMiklos Szeredi err = -ENOMEM; 885b9343632SChandan Rajendra stack = kcalloc(ofs->numlower, sizeof(struct ovl_path), 8860ee931c4SMichal Hocko GFP_KERNEL); 887bbb1e54dSMiklos Szeredi if (!stack) 888bbb1e54dSMiklos Szeredi goto out_put_upper; 889bbb1e54dSMiklos Szeredi } 890bbb1e54dSMiklos Szeredi 891e28edc46SMiklos Szeredi for (i = 0; !d.stop && i < poe->numlower; i++) { 892b9343632SChandan Rajendra struct ovl_path lower = poe->lowerstack[i]; 893bbb1e54dSMiklos Szeredi 894452061fdSVivek Goyal if (!ofs->config.redirect_follow) 895e28edc46SMiklos Szeredi d.last = i == poe->numlower - 1; 896452061fdSVivek Goyal else 897452061fdSVivek Goyal d.last = lower.layer->idx == roe->numlower; 898452061fdSVivek Goyal 899b9343632SChandan Rajendra err = ovl_lookup_layer(lower.dentry, &d, &this); 900e28edc46SMiklos Szeredi if (err) 901bbb1e54dSMiklos Szeredi goto out_put; 9026b2d5fe4SMiklos Szeredi 903bbb1e54dSMiklos Szeredi if (!this) 904bbb1e54dSMiklos Szeredi continue; 905bbb1e54dSMiklos Szeredi 9069678e630SAmir Goldstein /* 9079678e630SAmir Goldstein * If no origin fh is stored in upper of a merge dir, store fh 9089678e630SAmir Goldstein * of lower dir and set upper parent "impure". 9099678e630SAmir Goldstein */ 9109678e630SAmir Goldstein if (upperdentry && !ctr && !ofs->noxattr) { 9119678e630SAmir Goldstein err = ovl_fix_origin(dentry, this, upperdentry); 9129678e630SAmir Goldstein if (err) { 9139678e630SAmir Goldstein dput(this); 9149678e630SAmir Goldstein goto out_put; 9159678e630SAmir Goldstein } 9169678e630SAmir Goldstein } 9179678e630SAmir Goldstein 91837b12916SAmir Goldstein /* 91937b12916SAmir Goldstein * When "verify_lower" feature is enabled, do not merge with a 920ad1d615cSAmir Goldstein * lower dir that does not match a stored origin xattr. In any 921ad1d615cSAmir Goldstein * case, only verified origin is used for index lookup. 92237b12916SAmir Goldstein */ 92337b12916SAmir Goldstein if (upperdentry && !ctr && ovl_verify_lower(dentry->d_sb)) { 92437b12916SAmir Goldstein err = ovl_verify_origin(upperdentry, this, false); 92537b12916SAmir Goldstein if (err) { 92637b12916SAmir Goldstein dput(this); 92737b12916SAmir Goldstein break; 92837b12916SAmir Goldstein } 929ad1d615cSAmir Goldstein 930ad1d615cSAmir Goldstein /* Bless lower dir as verified origin */ 931ad1d615cSAmir Goldstein origin = this; 93237b12916SAmir Goldstein } 93337b12916SAmir Goldstein 934bbb1e54dSMiklos Szeredi stack[ctr].dentry = this; 935b9343632SChandan Rajendra stack[ctr].layer = lower.layer; 936bbb1e54dSMiklos Szeredi ctr++; 93702b69b28SMiklos Szeredi 938438c84c2SMiklos Szeredi /* 939438c84c2SMiklos Szeredi * Following redirects can have security consequences: it's like 940438c84c2SMiklos Szeredi * a symlink into the lower layer without the permission checks. 941438c84c2SMiklos Szeredi * This is only a problem if the upper layer is untrusted (e.g 942438c84c2SMiklos Szeredi * comes from an USB drive). This can allow a non-readable file 943438c84c2SMiklos Szeredi * or directory to become readable. 944438c84c2SMiklos Szeredi * 945438c84c2SMiklos Szeredi * Only following redirects when redirects are enabled disables 946438c84c2SMiklos Szeredi * this attack vector when not necessary. 947438c84c2SMiklos Szeredi */ 948438c84c2SMiklos Szeredi err = -EPERM; 949438c84c2SMiklos Szeredi if (d.redirect && !ofs->config.redirect_follow) { 950f8167817SAmir Goldstein pr_warn_ratelimited("overlayfs: refusing to follow redirect for (%pd2)\n", 951f8167817SAmir Goldstein dentry); 952438c84c2SMiklos Szeredi goto out_put; 953438c84c2SMiklos Szeredi } 954438c84c2SMiklos Szeredi 955d1fe96c0SVivek Goyal if (d.stop) 956d1fe96c0SVivek Goyal break; 957d1fe96c0SVivek Goyal 958c22205d0SAmir Goldstein if (d.redirect && d.redirect[0] == '/' && poe != roe) { 959c22205d0SAmir Goldstein poe = roe; 96002b69b28SMiklos Szeredi /* Find the current layer on the root dentry */ 961d583ed7dSAmir Goldstein i = lower.layer->idx - 1; 96202b69b28SMiklos Szeredi } 963bbb1e54dSMiklos Szeredi } 964bbb1e54dSMiklos Szeredi 965ad1d615cSAmir Goldstein /* 966ad1d615cSAmir Goldstein * Lookup index by lower inode and verify it matches upper inode. 967ad1d615cSAmir Goldstein * We only trust dir index if we verified that lower dir matches 968ad1d615cSAmir Goldstein * origin, otherwise dir index entries may be inconsistent and we 969ad1d615cSAmir Goldstein * ignore them. Always lookup index of non-dir and non-upper. 970ad1d615cSAmir Goldstein */ 971ad1d615cSAmir Goldstein if (ctr && (!upperdentry || !d.is_dir)) 972ad1d615cSAmir Goldstein origin = stack[0].dentry; 973359f392cSAmir Goldstein 974ad1d615cSAmir Goldstein if (origin && ovl_indexdir(dentry->d_sb) && 975ad1d615cSAmir Goldstein (!d.is_dir || ovl_index_all(dentry->d_sb))) { 97606170154SAmir Goldstein index = ovl_lookup_index(ofs, upperdentry, origin, true); 977359f392cSAmir Goldstein if (IS_ERR(index)) { 978359f392cSAmir Goldstein err = PTR_ERR(index); 979359f392cSAmir Goldstein index = NULL; 980359f392cSAmir Goldstein goto out_put; 981359f392cSAmir Goldstein } 982359f392cSAmir Goldstein } 983359f392cSAmir Goldstein 984bbb1e54dSMiklos Szeredi oe = ovl_alloc_entry(ctr); 985bbb1e54dSMiklos Szeredi err = -ENOMEM; 986bbb1e54dSMiklos Szeredi if (!oe) 987bbb1e54dSMiklos Szeredi goto out_put; 988bbb1e54dSMiklos Szeredi 989b9343632SChandan Rajendra memcpy(oe->lowerstack, stack, sizeof(struct ovl_path) * ctr); 990e6d2ebddSMiklos Szeredi dentry->d_fsdata = oe; 991e6d2ebddSMiklos Szeredi 992c62520a8SAmir Goldstein if (upperopaque) 993c62520a8SAmir Goldstein ovl_dentry_set_opaque(dentry); 994c62520a8SAmir Goldstein 99555acc661SMiklos Szeredi if (upperdentry) 99655acc661SMiklos Szeredi ovl_dentry_set_upper_alias(dentry); 99755acc661SMiklos Szeredi else if (index) 998359f392cSAmir Goldstein upperdentry = dget(index); 999359f392cSAmir Goldstein 1000e6d2ebddSMiklos Szeredi if (upperdentry || ctr) { 10012aed489dSAmir Goldstein if (ctr) 10022aed489dSAmir Goldstein origin = stack[0].dentry; 10030aceb53eSAmir Goldstein inode = ovl_get_inode(dentry->d_sb, upperdentry, origin, index, 10040aceb53eSAmir Goldstein ctr); 1005b9ac5c27SMiklos Szeredi err = PTR_ERR(inode); 1006b9ac5c27SMiklos Szeredi if (IS_ERR(inode)) 1007e6d2ebddSMiklos Szeredi goto out_free_oe; 1008cf31c463SMiklos Szeredi 1009cf31c463SMiklos Szeredi OVL_I(inode)->redirect = upperredirect; 1010359f392cSAmir Goldstein if (index) 1011359f392cSAmir Goldstein ovl_set_flag(OVL_INDEX, inode); 1012e6d2ebddSMiklos Szeredi } 1013e6d2ebddSMiklos Szeredi 1014e6d2ebddSMiklos Szeredi revert_creds(old_cred); 1015359f392cSAmir Goldstein dput(index); 1016bbb1e54dSMiklos Szeredi kfree(stack); 101702b69b28SMiklos Szeredi kfree(d.redirect); 1018829c28beSAmir Goldstein return d_splice_alias(inode, dentry); 1019bbb1e54dSMiklos Szeredi 1020bbb1e54dSMiklos Szeredi out_free_oe: 1021e6d2ebddSMiklos Szeredi dentry->d_fsdata = NULL; 1022bbb1e54dSMiklos Szeredi kfree(oe); 1023bbb1e54dSMiklos Szeredi out_put: 1024359f392cSAmir Goldstein dput(index); 1025bbb1e54dSMiklos Szeredi for (i = 0; i < ctr; i++) 1026bbb1e54dSMiklos Szeredi dput(stack[i].dentry); 1027bbb1e54dSMiklos Szeredi kfree(stack); 1028bbb1e54dSMiklos Szeredi out_put_upper: 1029bbb1e54dSMiklos Szeredi dput(upperdentry); 103002b69b28SMiklos Szeredi kfree(upperredirect); 1031bbb1e54dSMiklos Szeredi out: 103202b69b28SMiklos Szeredi kfree(d.redirect); 1033bbb1e54dSMiklos Szeredi revert_creds(old_cred); 1034bbb1e54dSMiklos Szeredi return ERR_PTR(err); 1035bbb1e54dSMiklos Szeredi } 1036bbb1e54dSMiklos Szeredi 1037bbb1e54dSMiklos Szeredi bool ovl_lower_positive(struct dentry *dentry) 1038bbb1e54dSMiklos Szeredi { 1039bbb1e54dSMiklos Szeredi struct ovl_entry *poe = dentry->d_parent->d_fsdata; 1040bbb1e54dSMiklos Szeredi const struct qstr *name = &dentry->d_name; 10416d0a8a90SAmir Goldstein const struct cred *old_cred; 1042bbb1e54dSMiklos Szeredi unsigned int i; 1043bbb1e54dSMiklos Szeredi bool positive = false; 1044bbb1e54dSMiklos Szeredi bool done = false; 1045bbb1e54dSMiklos Szeredi 1046bbb1e54dSMiklos Szeredi /* 1047bbb1e54dSMiklos Szeredi * If dentry is negative, then lower is positive iff this is a 1048bbb1e54dSMiklos Szeredi * whiteout. 1049bbb1e54dSMiklos Szeredi */ 1050bbb1e54dSMiklos Szeredi if (!dentry->d_inode) 1051c62520a8SAmir Goldstein return ovl_dentry_is_opaque(dentry); 1052bbb1e54dSMiklos Szeredi 1053bbb1e54dSMiklos Szeredi /* Negative upper -> positive lower */ 105409d8b586SMiklos Szeredi if (!ovl_dentry_upper(dentry)) 1055bbb1e54dSMiklos Szeredi return true; 1056bbb1e54dSMiklos Szeredi 10576d0a8a90SAmir Goldstein old_cred = ovl_override_creds(dentry->d_sb); 1058bbb1e54dSMiklos Szeredi /* Positive upper -> have to look up lower to see whether it exists */ 1059bbb1e54dSMiklos Szeredi for (i = 0; !done && !positive && i < poe->numlower; i++) { 1060bbb1e54dSMiklos Szeredi struct dentry *this; 1061bbb1e54dSMiklos Szeredi struct dentry *lowerdir = poe->lowerstack[i].dentry; 1062bbb1e54dSMiklos Szeredi 1063bbb1e54dSMiklos Szeredi this = lookup_one_len_unlocked(name->name, lowerdir, 1064bbb1e54dSMiklos Szeredi name->len); 1065bbb1e54dSMiklos Szeredi if (IS_ERR(this)) { 1066bbb1e54dSMiklos Szeredi switch (PTR_ERR(this)) { 1067bbb1e54dSMiklos Szeredi case -ENOENT: 1068bbb1e54dSMiklos Szeredi case -ENAMETOOLONG: 1069bbb1e54dSMiklos Szeredi break; 1070bbb1e54dSMiklos Szeredi 1071bbb1e54dSMiklos Szeredi default: 1072bbb1e54dSMiklos Szeredi /* 1073bbb1e54dSMiklos Szeredi * Assume something is there, we just couldn't 1074bbb1e54dSMiklos Szeredi * access it. 1075bbb1e54dSMiklos Szeredi */ 1076bbb1e54dSMiklos Szeredi positive = true; 1077bbb1e54dSMiklos Szeredi break; 1078bbb1e54dSMiklos Szeredi } 1079bbb1e54dSMiklos Szeredi } else { 1080bbb1e54dSMiklos Szeredi if (this->d_inode) { 1081bbb1e54dSMiklos Szeredi positive = !ovl_is_whiteout(this); 1082bbb1e54dSMiklos Szeredi done = true; 1083bbb1e54dSMiklos Szeredi } 1084bbb1e54dSMiklos Szeredi dput(this); 1085bbb1e54dSMiklos Szeredi } 1086bbb1e54dSMiklos Szeredi } 10876d0a8a90SAmir Goldstein revert_creds(old_cred); 1088bbb1e54dSMiklos Szeredi 1089bbb1e54dSMiklos Szeredi return positive; 1090bbb1e54dSMiklos Szeredi } 1091