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,
233 bool last_element = !post[0];
235 this = lookup_one_len_unlocked(name, base, namelen);
239 if (err == -ENOENT || err == -ENAMETOOLONG)
246 if (ovl_dentry_weird(this)) {
247 /* Don't support traversing automounts and other weirdness */
251 if (ovl_is_whiteout(this)) {
252 d->stop = d->opaque = true;
255 if (!d_can_lookup(this)) {
261 * NB: handle failure to lookup non-last element when non-dir
262 * redirects become possible
264 WARN_ON(!last_element);
272 if (ovl_is_opaquedir(this)) {
278 err = ovl_check_redirect(this, d, prelen, post);
295 static int ovl_lookup_layer(struct dentry *base, struct ovl_lookup_data *d,
298 /* Counting down from the end, since the prefix can change */
299 size_t rem = d->name.len - 1;
300 struct dentry *dentry = NULL;
303 if (d->name.name[0] != '/')
304 return ovl_lookup_single(base, d, d->name.name, d->name.len,
307 while (!IS_ERR_OR_NULL(base) && d_can_lookup(base)) {
308 const char *s = d->name.name + d->name.len - rem;
309 const char *next = strchrnul(s, '/');
310 size_t thislen = next - s;
313 /* Verify we did not go off the rails */
314 if (WARN_ON(s[-1] != '/'))
317 err = ovl_lookup_single(base, d, s, thislen,
318 d->name.len - rem, next, &base);
328 if (WARN_ON(rem >= d->name.len))
336 int ovl_check_origin_fh(struct ovl_fs *ofs, struct ovl_fh *fh, bool connected,
337 struct dentry *upperdentry, struct ovl_path **stackp)
339 struct dentry *origin = NULL;
342 for (i = 0; i < ofs->numlower; i++) {
343 origin = ovl_decode_real_fh(fh, ofs->lower_layers[i].mnt,
351 else if (IS_ERR(origin))
352 return PTR_ERR(origin);
354 if (upperdentry && !ovl_is_whiteout(upperdentry) &&
355 ((d_inode(origin)->i_mode ^ d_inode(upperdentry)->i_mode) & S_IFMT))
359 *stackp = kmalloc(sizeof(struct ovl_path), GFP_KERNEL);
364 **stackp = (struct ovl_path){
366 .layer = &ofs->lower_layers[i]
372 pr_warn_ratelimited("overlayfs: invalid origin (%pd2, ftype=%x, origin ftype=%x).\n",
373 upperdentry, d_inode(upperdentry)->i_mode & S_IFMT,
374 d_inode(origin)->i_mode & S_IFMT);
379 static int ovl_check_origin(struct ovl_fs *ofs, struct dentry *upperdentry,
380 struct ovl_path **stackp, unsigned int *ctrp)
382 struct ovl_fh *fh = ovl_get_fh(upperdentry, OVL_XATTR_ORIGIN);
385 if (IS_ERR_OR_NULL(fh))
388 err = ovl_check_origin_fh(ofs, fh, false, upperdentry, stackp);
405 * Verify that @fh matches the file handle stored in xattr @name.
406 * Return 0 on match, -ESTALE on mismatch, < 0 on error.
408 static int ovl_verify_fh(struct dentry *dentry, const char *name,
409 const struct ovl_fh *fh)
411 struct ovl_fh *ofh = ovl_get_fh(dentry, name);
420 if (fh->len != ofh->len || memcmp(fh, ofh, fh->len))
428 * Verify that @real dentry matches the file handle stored in xattr @name.
430 * If @set is true and there is no stored file handle, encode @real and store
431 * file handle in xattr @name.
433 * Return 0 on match, -ESTALE on mismatch, -ENODATA on no xattr, < 0 on error.
435 int ovl_verify_set_fh(struct dentry *dentry, const char *name,
436 struct dentry *real, bool is_upper, bool set)
442 fh = ovl_encode_real_fh(real, is_upper);
447 err = ovl_verify_fh(dentry, name, fh);
448 if (set && err == -ENODATA)
449 err = ovl_do_setxattr(dentry, name, fh, fh->len, 0);
458 inode = d_inode(real);
459 pr_warn_ratelimited("overlayfs: failed to verify %s (%pd2, ino=%lu, err=%i)\n",
460 is_upper ? "upper" : "origin", real,
461 inode ? inode->i_ino : 0, err);
465 /* Get upper dentry from index */
466 struct dentry *ovl_index_upper(struct ovl_fs *ofs, struct dentry *index)
469 struct dentry *upper;
471 if (!d_is_dir(index))
474 fh = ovl_get_fh(index, OVL_XATTR_UPPER);
475 if (IS_ERR_OR_NULL(fh))
478 upper = ovl_decode_real_fh(fh, ofs->upper_mnt, true);
481 if (IS_ERR_OR_NULL(upper))
482 return upper ?: ERR_PTR(-ESTALE);
484 if (!d_is_dir(upper)) {
485 pr_warn_ratelimited("overlayfs: invalid index upper (%pd2, upper=%pd2).\n",
488 return ERR_PTR(-EIO);
494 /* Is this a leftover from create/whiteout of directory index entry? */
495 static bool ovl_is_temp_index(struct dentry *index)
497 return index->d_name.name[0] == '#';
501 * Verify that an index entry name matches the origin file handle stored in
502 * OVL_XATTR_ORIGIN and that origin file handle can be decoded to lower path.
503 * Return 0 on match, -ESTALE on mismatch or stale origin, < 0 on error.
505 int ovl_verify_index(struct ovl_fs *ofs, struct dentry *index)
507 struct ovl_fh *fh = NULL;
509 struct ovl_path origin = { };
510 struct ovl_path *stack = &origin;
511 struct dentry *upper = NULL;
517 /* Cleanup leftover from index create/cleanup attempt */
519 if (ovl_is_temp_index(index))
523 if (index->d_name.len < sizeof(struct ovl_fh)*2)
527 len = index->d_name.len / 2;
528 fh = kzalloc(len, GFP_KERNEL);
533 if (hex2bin((u8 *)fh, index->d_name.name, len))
536 err = ovl_check_fh_len(fh, len);
541 * Whiteout index entries are used as an indication that an exported
542 * overlay file handle should be treated as stale (i.e. after unlink
543 * of the overlay inode). These entries contain no origin xattr.
545 if (ovl_is_whiteout(index))
549 * Verifying directory index entries are not stale is expensive, so
550 * only verify stale dir index if NFS export is enabled.
552 if (d_is_dir(index) && !ofs->config.nfs_export)
556 * Directory index entries should have 'upper' xattr pointing to the
557 * real upper dir. Non-dir index entries are hardlinks to the upper
558 * real inode. For non-dir index, we can read the copy up origin xattr
559 * directly from the index dentry, but for dir index we first need to
560 * decode the upper directory.
562 upper = ovl_index_upper(ofs, index);
563 if (IS_ERR_OR_NULL(upper)) {
564 err = PTR_ERR(upper);
566 * Directory index entries with no 'upper' xattr need to be
567 * removed. When dir index entry has a stale 'upper' xattr,
568 * we assume that upper dir was removed and we treat the dir
569 * index as orphan entry that needs to be whited out.
578 err = ovl_verify_fh(upper, OVL_XATTR_ORIGIN, fh);
583 /* Check if non-dir index is orphan and don't warn before cleaning it */
584 if (!d_is_dir(index) && d_inode(index)->i_nlink == 1) {
585 err = ovl_check_origin_fh(ofs, fh, false, index, &stack);
589 if (ovl_get_nlink(origin.dentry, index, 0) == 0)
599 pr_warn_ratelimited("overlayfs: failed to verify index (%pd2, ftype=%x, err=%i)\n",
600 index, d_inode(index)->i_mode & S_IFMT, err);
604 pr_warn_ratelimited("overlayfs: orphan index entry (%pd2, ftype=%x, nlink=%u)\n",
605 index, d_inode(index)->i_mode & S_IFMT,
606 d_inode(index)->i_nlink);
611 static int ovl_get_index_name_fh(struct ovl_fh *fh, struct qstr *name)
615 n = kcalloc(fh->len, 2, GFP_KERNEL);
619 s = bin2hex(n, fh, fh->len);
620 *name = (struct qstr) QSTR_INIT(n, s - n);
627 * Lookup in indexdir for the index entry of a lower real inode or a copy up
628 * origin inode. The index entry name is the hex representation of the lower
631 * If the index dentry in negative, then either no lower aliases have been
632 * copied up yet, or aliases have been copied up in older kernels and are
635 * If the index dentry for a copy up origin inode is positive, but points
636 * to an inode different than the upper inode, then either the upper inode
637 * has been copied up and not indexed or it was indexed, but since then
638 * index dir was cleared. Either way, that index cannot be used to indentify
641 int ovl_get_index_name(struct dentry *origin, struct qstr *name)
646 fh = ovl_encode_real_fh(origin, false);
650 err = ovl_get_index_name_fh(fh, name);
656 /* Lookup index by file handle for NFS export */
657 struct dentry *ovl_get_index_fh(struct ovl_fs *ofs, struct ovl_fh *fh)
659 struct dentry *index;
663 err = ovl_get_index_name_fh(fh, &name);
667 index = lookup_one_len_unlocked(name.name, ofs->indexdir, name.len);
670 if (PTR_ERR(index) == -ENOENT)
675 if (d_is_negative(index))
677 else if (ovl_is_whiteout(index))
679 else if (ovl_dentry_weird(index))
688 struct dentry *ovl_lookup_index(struct ovl_fs *ofs, struct dentry *upper,
689 struct dentry *origin, bool verify)
691 struct dentry *index;
694 bool is_dir = d_is_dir(origin);
697 err = ovl_get_index_name(origin, &name);
701 index = lookup_one_len_unlocked(name.name, ofs->indexdir, name.len);
703 err = PTR_ERR(index);
704 if (err == -ENOENT) {
708 pr_warn_ratelimited("overlayfs: failed inode index lookup (ino=%lu, key=%*s, err=%i);\n"
709 "overlayfs: mount with '-o index=off' to disable inodes index.\n",
710 d_inode(origin)->i_ino, name.len, name.name,
715 inode = d_inode(index);
716 if (d_is_negative(index)) {
718 } else if (ovl_is_whiteout(index) && !verify) {
720 * When index lookup is called with !verify for decoding an
721 * overlay file handle, a whiteout index implies that decode
722 * should treat file handle as stale and no need to print a
726 index = ERR_PTR(-ESTALE);
728 } else if (ovl_dentry_weird(index) || ovl_is_whiteout(index) ||
729 ((inode->i_mode ^ d_inode(origin)->i_mode) & S_IFMT)) {
731 * Index should always be of the same file type as origin
732 * except for the case of a whiteout index. A whiteout
733 * index should only exist if all lower aliases have been
734 * unlinked, which means that finding a lower origin on lookup
735 * whose index is a whiteout should be treated as an error.
737 pr_warn_ratelimited("overlayfs: bad index found (index=%pd2, ftype=%x, origin ftype=%x).\n",
738 index, d_inode(index)->i_mode & S_IFMT,
739 d_inode(origin)->i_mode & S_IFMT);
741 } else if (is_dir && verify) {
743 pr_warn_ratelimited("overlayfs: suspected uncovered redirected dir found (origin=%pd2, index=%pd2).\n",
748 /* Verify that dir index 'upper' xattr points to upper dir */
749 err = ovl_verify_upper(index, upper, false);
751 if (err == -ESTALE) {
752 pr_warn_ratelimited("overlayfs: suspected multiply redirected dir found (upper=%pd2, origin=%pd2, index=%pd2).\n",
753 upper, origin, index);
757 } else if (upper && d_inode(upper) != inode) {
771 index = ERR_PTR(-EIO);
776 * Returns next layer in stack starting from top.
777 * Returns -1 if this is the last layer.
779 int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
781 struct ovl_entry *oe = dentry->d_fsdata;
785 ovl_path_upper(dentry, path);
787 return oe->numlower ? 1 : -1;
790 BUG_ON(idx > oe->numlower);
791 path->dentry = oe->lowerstack[idx - 1].dentry;
792 path->mnt = oe->lowerstack[idx - 1].layer->mnt;
794 return (idx < oe->numlower) ? idx + 1 : -1;
797 /* Fix missing 'origin' xattr */
798 static int ovl_fix_origin(struct dentry *dentry, struct dentry *lower,
799 struct dentry *upper)
803 if (ovl_check_origin_xattr(upper))
806 err = ovl_want_write(dentry);
810 err = ovl_set_origin(dentry, lower, upper);
812 err = ovl_set_impure(dentry->d_parent, upper->d_parent);
814 ovl_drop_write(dentry);
818 struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
821 struct ovl_entry *oe;
822 const struct cred *old_cred;
823 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
824 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
825 struct ovl_entry *roe = dentry->d_sb->s_root->d_fsdata;
826 struct ovl_path *stack = NULL;
827 struct dentry *upperdir, *upperdentry = NULL;
828 struct dentry *origin = NULL;
829 struct dentry *index = NULL;
830 unsigned int ctr = 0;
831 struct inode *inode = NULL;
832 bool upperopaque = false;
833 char *upperredirect = NULL;
837 struct ovl_lookup_data d = {
838 .name = dentry->d_name,
842 .last = ofs->config.redirect_follow ? false : !poe->numlower,
846 if (dentry->d_name.len > ofs->namelen)
847 return ERR_PTR(-ENAMETOOLONG);
849 old_cred = ovl_override_creds(dentry->d_sb);
850 upperdir = ovl_dentry_upper(dentry->d_parent);
852 err = ovl_lookup_layer(upperdir, &d, &upperdentry);
856 if (upperdentry && unlikely(ovl_dentry_remote(upperdentry))) {
861 if (upperdentry && !d.is_dir) {
862 BUG_ON(!d.stop || d.redirect);
864 * Lookup copy up origin by decoding origin file handle.
865 * We may get a disconnected dentry, which is fine,
866 * because we only need to hold the origin inode in
867 * cache and use its inode number. We may even get a
868 * connected dentry, that is not under any of the lower
869 * layers root. That is also fine for using it's inode
870 * number - it's the same as if we held a reference
871 * to a dentry in lower layer that was moved under us.
873 err = ovl_check_origin(ofs, upperdentry, &stack, &ctr);
880 upperredirect = kstrdup(d.redirect, GFP_KERNEL);
883 if (d.redirect[0] == '/')
886 upperopaque = d.opaque;
889 if (!d.stop && poe->numlower) {
891 stack = kcalloc(ofs->numlower, sizeof(struct ovl_path),
897 for (i = 0; !d.stop && i < poe->numlower; i++) {
898 struct ovl_path lower = poe->lowerstack[i];
900 if (!ofs->config.redirect_follow)
901 d.last = i == poe->numlower - 1;
903 d.last = lower.layer->idx == roe->numlower;
905 err = ovl_lookup_layer(lower.dentry, &d, &this);
913 * If no origin fh is stored in upper of a merge dir, store fh
914 * of lower dir and set upper parent "impure".
916 if (upperdentry && !ctr && !ofs->noxattr) {
917 err = ovl_fix_origin(dentry, this, upperdentry);
925 * When "verify_lower" feature is enabled, do not merge with a
926 * lower dir that does not match a stored origin xattr. In any
927 * case, only verified origin is used for index lookup.
929 if (upperdentry && !ctr && ovl_verify_lower(dentry->d_sb)) {
930 err = ovl_verify_origin(upperdentry, this, false);
936 /* Bless lower dir as verified origin */
940 stack[ctr].dentry = this;
941 stack[ctr].layer = lower.layer;
945 * Following redirects can have security consequences: it's like
946 * a symlink into the lower layer without the permission checks.
947 * This is only a problem if the upper layer is untrusted (e.g
948 * comes from an USB drive). This can allow a non-readable file
949 * or directory to become readable.
951 * Only following redirects when redirects are enabled disables
952 * this attack vector when not necessary.
955 if (d.redirect && !ofs->config.redirect_follow) {
956 pr_warn_ratelimited("overlayfs: refusing to follow redirect for (%pd2)\n",
964 if (d.redirect && d.redirect[0] == '/' && poe != roe) {
966 /* Find the current layer on the root dentry */
967 i = lower.layer->idx - 1;
972 * Lookup index by lower inode and verify it matches upper inode.
973 * We only trust dir index if we verified that lower dir matches
974 * origin, otherwise dir index entries may be inconsistent and we
975 * ignore them. Always lookup index of non-dir and non-upper.
977 if (ctr && (!upperdentry || !d.is_dir))
978 origin = stack[0].dentry;
980 if (origin && ovl_indexdir(dentry->d_sb) &&
981 (!d.is_dir || ovl_index_all(dentry->d_sb))) {
982 index = ovl_lookup_index(ofs, upperdentry, origin, true);
984 err = PTR_ERR(index);
990 oe = ovl_alloc_entry(ctr);
995 memcpy(oe->lowerstack, stack, sizeof(struct ovl_path) * ctr);
996 dentry->d_fsdata = oe;
999 ovl_dentry_set_opaque(dentry);
1002 ovl_dentry_set_upper_alias(dentry);
1004 upperdentry = dget(index);
1006 if (upperdentry || ctr) {
1007 struct ovl_inode_params oip = {
1008 .upperdentry = upperdentry,
1014 inode = ovl_get_inode(dentry->d_sb, &oip);
1015 err = PTR_ERR(inode);
1020 * NB: handle redirected hard links when non-dir redirects
1023 WARN_ON(OVL_I(inode)->redirect);
1024 OVL_I(inode)->redirect = upperredirect;
1027 revert_creds(old_cred);
1031 return d_splice_alias(inode, dentry);
1034 dentry->d_fsdata = NULL;
1038 for (i = 0; i < ctr; i++)
1039 dput(stack[i].dentry);
1043 kfree(upperredirect);
1046 revert_creds(old_cred);
1047 return ERR_PTR(err);
1050 bool ovl_lower_positive(struct dentry *dentry)
1052 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
1053 const struct qstr *name = &dentry->d_name;
1054 const struct cred *old_cred;
1056 bool positive = false;
1060 * If dentry is negative, then lower is positive iff this is a
1063 if (!dentry->d_inode)
1064 return ovl_dentry_is_opaque(dentry);
1066 /* Negative upper -> positive lower */
1067 if (!ovl_dentry_upper(dentry))
1070 old_cred = ovl_override_creds(dentry->d_sb);
1071 /* Positive upper -> have to look up lower to see whether it exists */
1072 for (i = 0; !done && !positive && i < poe->numlower; i++) {
1073 struct dentry *this;
1074 struct dentry *lowerdir = poe->lowerstack[i].dentry;
1076 this = lookup_one_len_unlocked(name->name, lowerdir,
1079 switch (PTR_ERR(this)) {
1086 * Assume something is there, we just couldn't
1093 if (this->d_inode) {
1094 positive = !ovl_is_whiteout(this);
1100 revert_creds(old_cred);