2 * Copyright (C) 2011 Novell Inc.
3 * Copyright (C) 2016 Red Hat, Inc.
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.
11 #include <linux/cred.h>
12 #include <linux/ctype.h>
13 #include <linux/namei.h>
14 #include <linux/xattr.h>
15 #include <linux/ratelimit.h>
16 #include <linux/mount.h>
17 #include <linux/exportfs.h>
18 #include "overlayfs.h"
20 struct ovl_lookup_data {
29 static int ovl_check_redirect(struct dentry *dentry, struct ovl_lookup_data *d,
30 size_t prelen, const char *post)
33 char *s, *next, *buf = NULL;
35 res = vfs_getxattr(dentry, OVL_XATTR_REDIRECT, NULL, 0);
37 if (res == -ENODATA || res == -EOPNOTSUPP)
41 buf = kzalloc(prelen + res + strlen(post) + 1, GFP_KERNEL);
48 res = vfs_getxattr(dentry, OVL_XATTR_REDIRECT, buf, res);
54 for (s = buf; *s++ == '/'; s = next) {
55 next = strchrnul(s, '/');
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).
69 if (strchr(buf, '/') != NULL)
72 memmove(buf + prelen, buf, res);
73 memcpy(buf, d->name.name, prelen);
79 d->name.name = d->redirect;
80 d->name.len = strlen(d->redirect);
88 pr_warn_ratelimited("overlayfs: failed to get redirect (%i)\n", res);
91 pr_warn_ratelimited("overlayfs: invalid redirect (%s)\n", buf);
95 static int ovl_acceptable(void *ctx, struct dentry *dentry)
98 * A non-dir origin may be disconnected, which is fine, because
99 * we only need it for its unique inode number.
101 if (!d_is_dir(dentry))
104 /* Don't decode a deleted empty directory */
105 if (d_unhashed(dentry))
108 /* Check if directory belongs to the layer we are decoding from */
109 return is_subdir(dentry, ((struct vfsmount *)ctx)->mnt_root);
113 * Check validity of an overlay file handle buffer.
115 * Return 0 for a valid file handle.
116 * Return -ENODATA for "origin unknown".
117 * Return <0 for an invalid file handle.
119 int ovl_check_fh_len(struct ovl_fh *fh, int fh_len)
121 if (fh_len < sizeof(struct ovl_fh) || fh_len < fh->len)
124 if (fh->magic != OVL_FH_MAGIC)
127 /* Treat larger version and unknown flags as "origin unknown" */
128 if (fh->version > OVL_FH_VERSION || fh->flags & ~OVL_FH_FLAG_ALL)
131 /* Treat endianness mismatch as "origin unknown" */
132 if (!(fh->flags & OVL_FH_FLAG_ANY_ENDIAN) &&
133 (fh->flags & OVL_FH_FLAG_BIG_ENDIAN) != OVL_FH_FLAG_CPU_ENDIAN)
139 static struct ovl_fh *ovl_get_fh(struct dentry *dentry, const char *name)
142 struct ovl_fh *fh = NULL;
144 res = vfs_getxattr(dentry, name, NULL, 0);
146 if (res == -ENODATA || res == -EOPNOTSUPP)
150 /* Zero size value means "copied up but origin unknown" */
154 fh = kzalloc(res, GFP_KERNEL);
156 return ERR_PTR(-ENOMEM);
158 res = vfs_getxattr(dentry, name, fh, res);
162 err = ovl_check_fh_len(fh, res);
176 pr_warn_ratelimited("overlayfs: failed to get origin (%i)\n", res);
179 pr_warn_ratelimited("overlayfs: invalid origin (%*phN)\n", res, fh);
183 struct dentry *ovl_decode_real_fh(struct ovl_fh *fh, struct vfsmount *mnt,
190 * Make sure that the stored uuid matches the uuid of the lower
191 * layer where file handle will be decoded.
193 if (!uuid_equal(&fh->uuid, &mnt->mnt_sb->s_uuid))
196 bytes = (fh->len - offsetof(struct ovl_fh, fid));
197 real = exportfs_decode_fh(mnt, (struct fid *)fh->fid,
198 bytes >> 2, (int)fh->type,
199 connected ? ovl_acceptable : NULL, mnt);
202 * Treat stale file handle to lower file as "origin unknown".
203 * upper file handle could become stale when upper file is
204 * unlinked and this information is needed to handle stale
205 * index entries correctly.
207 if (real == ERR_PTR(-ESTALE) &&
208 !(fh->flags & OVL_FH_FLAG_PATH_UPPER))
213 if (ovl_dentry_weird(real)) {
221 static bool ovl_is_opaquedir(struct dentry *dentry)
223 return ovl_check_dir_xattr(dentry, OVL_XATTR_OPAQUE);
226 static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d,
227 const char *name, unsigned int namelen,
228 size_t prelen, const char *post,
234 this = lookup_one_len_unlocked(name, base, namelen);
238 if (err == -ENOENT || err == -ENAMETOOLONG)
245 if (ovl_dentry_weird(this)) {
246 /* Don't support traversing automounts and other weirdness */
250 if (ovl_is_whiteout(this)) {
251 d->stop = d->opaque = true;
254 if (!d_can_lookup(this)) {
264 if (ovl_is_opaquedir(this)) {
265 d->stop = d->opaque = true;
268 err = ovl_check_redirect(this, d, prelen, post);
285 static int ovl_lookup_layer(struct dentry *base, struct ovl_lookup_data *d,
288 /* Counting down from the end, since the prefix can change */
289 size_t rem = d->name.len - 1;
290 struct dentry *dentry = NULL;
293 if (d->name.name[0] != '/')
294 return ovl_lookup_single(base, d, d->name.name, d->name.len,
297 while (!IS_ERR_OR_NULL(base) && d_can_lookup(base)) {
298 const char *s = d->name.name + d->name.len - rem;
299 const char *next = strchrnul(s, '/');
300 size_t thislen = next - s;
303 /* Verify we did not go off the rails */
304 if (WARN_ON(s[-1] != '/'))
307 err = ovl_lookup_single(base, d, s, thislen,
308 d->name.len - rem, next, &base);
318 if (WARN_ON(rem >= d->name.len))
326 int ovl_check_origin_fh(struct ovl_fs *ofs, struct ovl_fh *fh, bool connected,
327 struct dentry *upperdentry, struct ovl_path **stackp)
329 struct dentry *origin = NULL;
332 for (i = 0; i < ofs->numlower; i++) {
333 origin = ovl_decode_real_fh(fh, ofs->lower_layers[i].mnt,
341 else if (IS_ERR(origin))
342 return PTR_ERR(origin);
344 if (upperdentry && !ovl_is_whiteout(upperdentry) &&
345 ((d_inode(origin)->i_mode ^ d_inode(upperdentry)->i_mode) & S_IFMT))
349 *stackp = kmalloc(sizeof(struct ovl_path), GFP_KERNEL);
354 **stackp = (struct ovl_path){
356 .layer = &ofs->lower_layers[i]
362 pr_warn_ratelimited("overlayfs: invalid origin (%pd2, ftype=%x, origin ftype=%x).\n",
363 upperdentry, d_inode(upperdentry)->i_mode & S_IFMT,
364 d_inode(origin)->i_mode & S_IFMT);
369 static int ovl_check_origin(struct ovl_fs *ofs, struct dentry *upperdentry,
370 struct ovl_path **stackp, unsigned int *ctrp)
372 struct ovl_fh *fh = ovl_get_fh(upperdentry, OVL_XATTR_ORIGIN);
375 if (IS_ERR_OR_NULL(fh))
378 err = ovl_check_origin_fh(ofs, fh, false, upperdentry, stackp);
395 * Verify that @fh matches the file handle stored in xattr @name.
396 * Return 0 on match, -ESTALE on mismatch, < 0 on error.
398 static int ovl_verify_fh(struct dentry *dentry, const char *name,
399 const struct ovl_fh *fh)
401 struct ovl_fh *ofh = ovl_get_fh(dentry, name);
410 if (fh->len != ofh->len || memcmp(fh, ofh, fh->len))
418 * Verify that @real dentry matches the file handle stored in xattr @name.
420 * If @set is true and there is no stored file handle, encode @real and store
421 * file handle in xattr @name.
423 * Return 0 on match, -ESTALE on mismatch, -ENODATA on no xattr, < 0 on error.
425 int ovl_verify_set_fh(struct dentry *dentry, const char *name,
426 struct dentry *real, bool is_upper, bool set)
432 fh = ovl_encode_real_fh(real, is_upper);
437 err = ovl_verify_fh(dentry, name, fh);
438 if (set && err == -ENODATA)
439 err = ovl_do_setxattr(dentry, name, fh, fh->len, 0);
448 inode = d_inode(real);
449 pr_warn_ratelimited("overlayfs: failed to verify %s (%pd2, ino=%lu, err=%i)\n",
450 is_upper ? "upper" : "origin", real,
451 inode ? inode->i_ino : 0, err);
455 /* Get upper dentry from index */
456 struct dentry *ovl_index_upper(struct ovl_fs *ofs, struct dentry *index)
459 struct dentry *upper;
461 if (!d_is_dir(index))
464 fh = ovl_get_fh(index, OVL_XATTR_UPPER);
465 if (IS_ERR_OR_NULL(fh))
468 upper = ovl_decode_real_fh(fh, ofs->upper_mnt, true);
471 if (IS_ERR_OR_NULL(upper))
472 return upper ?: ERR_PTR(-ESTALE);
474 if (!d_is_dir(upper)) {
475 pr_warn_ratelimited("overlayfs: invalid index upper (%pd2, upper=%pd2).\n",
478 return ERR_PTR(-EIO);
484 /* Is this a leftover from create/whiteout of directory index entry? */
485 static bool ovl_is_temp_index(struct dentry *index)
487 return index->d_name.name[0] == '#';
491 * Verify that an index entry name matches the origin file handle stored in
492 * OVL_XATTR_ORIGIN and that origin file handle can be decoded to lower path.
493 * Return 0 on match, -ESTALE on mismatch or stale origin, < 0 on error.
495 int ovl_verify_index(struct ovl_fs *ofs, struct dentry *index)
497 struct ovl_fh *fh = NULL;
499 struct ovl_path origin = { };
500 struct ovl_path *stack = &origin;
501 struct dentry *upper = NULL;
507 /* Cleanup leftover from index create/cleanup attempt */
509 if (ovl_is_temp_index(index))
513 if (index->d_name.len < sizeof(struct ovl_fh)*2)
517 len = index->d_name.len / 2;
518 fh = kzalloc(len, GFP_KERNEL);
523 if (hex2bin((u8 *)fh, index->d_name.name, len))
526 err = ovl_check_fh_len(fh, len);
531 * Whiteout index entries are used as an indication that an exported
532 * overlay file handle should be treated as stale (i.e. after unlink
533 * of the overlay inode). These entries contain no origin xattr.
535 if (ovl_is_whiteout(index))
539 * Verifying directory index entries are not stale is expensive, so
540 * only verify stale dir index if NFS export is enabled.
542 if (d_is_dir(index) && !ofs->config.nfs_export)
546 * Directory index entries should have 'upper' xattr pointing to the
547 * real upper dir. Non-dir index entries are hardlinks to the upper
548 * real inode. For non-dir index, we can read the copy up origin xattr
549 * directly from the index dentry, but for dir index we first need to
550 * decode the upper directory.
552 upper = ovl_index_upper(ofs, index);
553 if (IS_ERR_OR_NULL(upper)) {
554 err = PTR_ERR(upper);
556 * Directory index entries with no 'upper' xattr need to be
557 * removed. When dir index entry has a stale 'upper' xattr,
558 * we assume that upper dir was removed and we treat the dir
559 * index as orphan entry that needs to be whited out.
568 err = ovl_verify_fh(upper, OVL_XATTR_ORIGIN, fh);
573 /* Check if non-dir index is orphan and don't warn before cleaning it */
574 if (!d_is_dir(index) && d_inode(index)->i_nlink == 1) {
575 err = ovl_check_origin_fh(ofs, fh, false, index, &stack);
579 if (ovl_get_nlink(origin.dentry, index, 0) == 0)
589 pr_warn_ratelimited("overlayfs: failed to verify index (%pd2, ftype=%x, err=%i)\n",
590 index, d_inode(index)->i_mode & S_IFMT, err);
594 pr_warn_ratelimited("overlayfs: orphan index entry (%pd2, ftype=%x, nlink=%u)\n",
595 index, d_inode(index)->i_mode & S_IFMT,
596 d_inode(index)->i_nlink);
601 static int ovl_get_index_name_fh(struct ovl_fh *fh, struct qstr *name)
605 n = kzalloc(fh->len * 2, GFP_KERNEL);
609 s = bin2hex(n, fh, fh->len);
610 *name = (struct qstr) QSTR_INIT(n, s - n);
617 * Lookup in indexdir for the index entry of a lower real inode or a copy up
618 * origin inode. The index entry name is the hex representation of the lower
621 * If the index dentry in negative, then either no lower aliases have been
622 * copied up yet, or aliases have been copied up in older kernels and are
625 * If the index dentry for a copy up origin inode is positive, but points
626 * to an inode different than the upper inode, then either the upper inode
627 * has been copied up and not indexed or it was indexed, but since then
628 * index dir was cleared. Either way, that index cannot be used to indentify
631 int ovl_get_index_name(struct dentry *origin, struct qstr *name)
636 fh = ovl_encode_real_fh(origin, false);
640 err = ovl_get_index_name_fh(fh, name);
646 /* Lookup index by file handle for NFS export */
647 struct dentry *ovl_get_index_fh(struct ovl_fs *ofs, struct ovl_fh *fh)
649 struct dentry *index;
653 err = ovl_get_index_name_fh(fh, &name);
657 index = lookup_one_len_unlocked(name.name, ofs->indexdir, name.len);
660 if (PTR_ERR(index) == -ENOENT)
665 if (d_is_negative(index))
667 else if (ovl_is_whiteout(index))
669 else if (ovl_dentry_weird(index))
678 struct dentry *ovl_lookup_index(struct ovl_fs *ofs, struct dentry *upper,
679 struct dentry *origin, bool verify)
681 struct dentry *index;
684 bool is_dir = d_is_dir(origin);
687 err = ovl_get_index_name(origin, &name);
691 index = lookup_one_len_unlocked(name.name, ofs->indexdir, name.len);
693 err = PTR_ERR(index);
694 if (err == -ENOENT) {
698 pr_warn_ratelimited("overlayfs: failed inode index lookup (ino=%lu, key=%*s, err=%i);\n"
699 "overlayfs: mount with '-o index=off' to disable inodes index.\n",
700 d_inode(origin)->i_ino, name.len, name.name,
705 inode = d_inode(index);
706 if (d_is_negative(index)) {
708 } else if (ovl_is_whiteout(index) && !verify) {
710 * When index lookup is called with !verify for decoding an
711 * overlay file handle, a whiteout index implies that decode
712 * should treat file handle as stale and no need to print a
716 index = ERR_PTR(-ESTALE);
718 } else if (ovl_dentry_weird(index) || ovl_is_whiteout(index) ||
719 ((inode->i_mode ^ d_inode(origin)->i_mode) & S_IFMT)) {
721 * Index should always be of the same file type as origin
722 * except for the case of a whiteout index. A whiteout
723 * index should only exist if all lower aliases have been
724 * unlinked, which means that finding a lower origin on lookup
725 * whose index is a whiteout should be treated as an error.
727 pr_warn_ratelimited("overlayfs: bad index found (index=%pd2, ftype=%x, origin ftype=%x).\n",
728 index, d_inode(index)->i_mode & S_IFMT,
729 d_inode(origin)->i_mode & S_IFMT);
731 } else if (is_dir && verify) {
733 pr_warn_ratelimited("overlayfs: suspected uncovered redirected dir found (origin=%pd2, index=%pd2).\n",
738 /* Verify that dir index 'upper' xattr points to upper dir */
739 err = ovl_verify_upper(index, upper, false);
741 if (err == -ESTALE) {
742 pr_warn_ratelimited("overlayfs: suspected multiply redirected dir found (upper=%pd2, origin=%pd2, index=%pd2).\n",
743 upper, origin, index);
747 } else if (upper && d_inode(upper) != inode) {
761 index = ERR_PTR(-EIO);
766 * Returns next layer in stack starting from top.
767 * Returns -1 if this is the last layer.
769 int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
771 struct ovl_entry *oe = dentry->d_fsdata;
775 ovl_path_upper(dentry, path);
777 return oe->numlower ? 1 : -1;
780 BUG_ON(idx > oe->numlower);
781 path->dentry = oe->lowerstack[idx - 1].dentry;
782 path->mnt = oe->lowerstack[idx - 1].layer->mnt;
784 return (idx < oe->numlower) ? idx + 1 : -1;
787 /* Fix missing 'origin' xattr */
788 static int ovl_fix_origin(struct dentry *dentry, struct dentry *lower,
789 struct dentry *upper)
793 if (ovl_check_origin_xattr(upper))
796 err = ovl_want_write(dentry);
800 err = ovl_set_origin(dentry, lower, upper);
802 err = ovl_set_impure(dentry->d_parent, upper->d_parent);
804 ovl_drop_write(dentry);
808 struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
811 struct ovl_entry *oe;
812 const struct cred *old_cred;
813 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
814 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
815 struct ovl_entry *roe = dentry->d_sb->s_root->d_fsdata;
816 struct ovl_path *stack = NULL;
817 struct dentry *upperdir, *upperdentry = NULL;
818 struct dentry *origin = NULL;
819 struct dentry *index = NULL;
820 unsigned int ctr = 0;
821 struct inode *inode = NULL;
822 bool upperopaque = false;
823 char *upperredirect = NULL;
827 struct ovl_lookup_data d = {
828 .name = dentry->d_name,
832 .last = ofs->config.redirect_follow ? false : !poe->numlower,
836 if (dentry->d_name.len > ofs->namelen)
837 return ERR_PTR(-ENAMETOOLONG);
839 old_cred = ovl_override_creds(dentry->d_sb);
840 upperdir = ovl_dentry_upper(dentry->d_parent);
842 err = ovl_lookup_layer(upperdir, &d, &upperdentry);
846 if (upperdentry && unlikely(ovl_dentry_remote(upperdentry))) {
851 if (upperdentry && !d.is_dir) {
852 BUG_ON(!d.stop || d.redirect);
854 * Lookup copy up origin by decoding origin file handle.
855 * We may get a disconnected dentry, which is fine,
856 * because we only need to hold the origin inode in
857 * cache and use its inode number. We may even get a
858 * connected dentry, that is not under any of the lower
859 * layers root. That is also fine for using it's inode
860 * number - it's the same as if we held a reference
861 * to a dentry in lower layer that was moved under us.
863 err = ovl_check_origin(ofs, upperdentry, &stack, &ctr);
870 upperredirect = kstrdup(d.redirect, GFP_KERNEL);
873 if (d.redirect[0] == '/')
876 upperopaque = d.opaque;
879 if (!d.stop && poe->numlower) {
881 stack = kcalloc(ofs->numlower, sizeof(struct ovl_path),
887 for (i = 0; !d.stop && i < poe->numlower; i++) {
888 struct ovl_path lower = poe->lowerstack[i];
890 if (!ofs->config.redirect_follow)
891 d.last = i == poe->numlower - 1;
893 d.last = lower.layer->idx == roe->numlower;
895 err = ovl_lookup_layer(lower.dentry, &d, &this);
903 * If no origin fh is stored in upper of a merge dir, store fh
904 * of lower dir and set upper parent "impure".
906 if (upperdentry && !ctr && !ofs->noxattr) {
907 err = ovl_fix_origin(dentry, this, upperdentry);
915 * When "verify_lower" feature is enabled, do not merge with a
916 * lower dir that does not match a stored origin xattr. In any
917 * case, only verified origin is used for index lookup.
919 if (upperdentry && !ctr && ovl_verify_lower(dentry->d_sb)) {
920 err = ovl_verify_origin(upperdentry, this, false);
926 /* Bless lower dir as verified origin */
930 stack[ctr].dentry = this;
931 stack[ctr].layer = lower.layer;
935 * Following redirects can have security consequences: it's like
936 * a symlink into the lower layer without the permission checks.
937 * This is only a problem if the upper layer is untrusted (e.g
938 * comes from an USB drive). This can allow a non-readable file
939 * or directory to become readable.
941 * Only following redirects when redirects are enabled disables
942 * this attack vector when not necessary.
945 if (d.redirect && !ofs->config.redirect_follow) {
946 pr_warn_ratelimited("overlayfs: refusing to follow redirect for (%pd2)\n",
954 if (d.redirect && d.redirect[0] == '/' && poe != roe) {
956 /* Find the current layer on the root dentry */
957 i = lower.layer->idx - 1;
962 * Lookup index by lower inode and verify it matches upper inode.
963 * We only trust dir index if we verified that lower dir matches
964 * origin, otherwise dir index entries may be inconsistent and we
965 * ignore them. Always lookup index of non-dir and non-upper.
967 if (ctr && (!upperdentry || !d.is_dir))
968 origin = stack[0].dentry;
970 if (origin && ovl_indexdir(dentry->d_sb) &&
971 (!d.is_dir || ovl_index_all(dentry->d_sb))) {
972 index = ovl_lookup_index(ofs, upperdentry, origin, true);
974 err = PTR_ERR(index);
980 oe = ovl_alloc_entry(ctr);
985 memcpy(oe->lowerstack, stack, sizeof(struct ovl_path) * ctr);
986 dentry->d_fsdata = oe;
989 ovl_dentry_set_opaque(dentry);
992 ovl_dentry_set_upper_alias(dentry);
994 upperdentry = dget(index);
996 if (upperdentry || ctr) {
998 origin = stack[0].dentry;
999 inode = ovl_get_inode(dentry->d_sb, upperdentry, origin, index,
1001 err = PTR_ERR(inode);
1005 OVL_I(inode)->redirect = upperredirect;
1007 ovl_set_flag(OVL_INDEX, inode);
1010 revert_creds(old_cred);
1014 return d_splice_alias(inode, dentry);
1017 dentry->d_fsdata = NULL;
1021 for (i = 0; i < ctr; i++)
1022 dput(stack[i].dentry);
1026 kfree(upperredirect);
1029 revert_creds(old_cred);
1030 return ERR_PTR(err);
1033 bool ovl_lower_positive(struct dentry *dentry)
1035 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
1036 const struct qstr *name = &dentry->d_name;
1037 const struct cred *old_cred;
1039 bool positive = false;
1043 * If dentry is negative, then lower is positive iff this is a
1046 if (!dentry->d_inode)
1047 return ovl_dentry_is_opaque(dentry);
1049 /* Negative upper -> positive lower */
1050 if (!ovl_dentry_upper(dentry))
1053 old_cred = ovl_override_creds(dentry->d_sb);
1054 /* Positive upper -> have to look up lower to see whether it exists */
1055 for (i = 0; !done && !positive && i < poe->numlower; i++) {
1056 struct dentry *this;
1057 struct dentry *lowerdir = poe->lowerstack[i].dentry;
1059 this = lookup_one_len_unlocked(name->name, lowerdir,
1062 switch (PTR_ERR(this)) {
1069 * Assume something is there, we just couldn't
1076 if (this->d_inode) {
1077 positive = !ovl_is_whiteout(this);
1083 revert_creds(old_cred);