bpf: Prevent memory disambiguation attack
[linux/fpc-iii.git] / fs / overlayfs / namei.c
blob35418317ecf2355cf9c6b1ea46876e4f83070fba
1 /*
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.
8 */
10 #include <linux/fs.h>
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 {
21 struct qstr name;
22 bool is_dir;
23 bool opaque;
24 bool stop;
25 bool last;
26 char *redirect;
29 static int ovl_check_redirect(struct dentry *dentry, struct ovl_lookup_data *d,
30 size_t prelen, const char *post)
32 int res;
33 char *s, *next, *buf = NULL;
35 res = vfs_getxattr(dentry, OVL_XATTR_REDIRECT, NULL, 0);
36 if (res < 0) {
37 if (res == -ENODATA || res == -EOPNOTSUPP)
38 return 0;
39 goto fail;
41 buf = kzalloc(prelen + res + strlen(post) + 1, GFP_KERNEL);
42 if (!buf)
43 return -ENOMEM;
45 if (res == 0)
46 goto invalid;
48 res = vfs_getxattr(dentry, OVL_XATTR_REDIRECT, buf, res);
49 if (res < 0)
50 goto fail;
51 if (res == 0)
52 goto invalid;
53 if (buf[0] == '/') {
54 for (s = buf; *s++ == '/'; s = next) {
55 next = strchrnul(s, '/');
56 if (s == next)
57 goto invalid;
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).
67 d->stop = false;
68 } else {
69 if (strchr(buf, '/') != NULL)
70 goto invalid;
72 memmove(buf + prelen, buf, res);
73 memcpy(buf, d->name.name, prelen);
76 strcat(buf, post);
77 kfree(d->redirect);
78 d->redirect = buf;
79 d->name.name = d->redirect;
80 d->name.len = strlen(d->redirect);
82 return 0;
84 err_free:
85 kfree(buf);
86 return 0;
87 fail:
88 pr_warn_ratelimited("overlayfs: failed to get redirect (%i)\n", res);
89 goto err_free;
90 invalid:
91 pr_warn_ratelimited("overlayfs: invalid redirect (%s)\n", buf);
92 goto err_free;
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))
102 return 1;
104 /* Don't decode a deleted empty directory */
105 if (d_unhashed(dentry))
106 return 0;
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)
122 return -EINVAL;
124 if (fh->magic != OVL_FH_MAGIC)
125 return -EINVAL;
127 /* Treat larger version and unknown flags as "origin unknown" */
128 if (fh->version > OVL_FH_VERSION || fh->flags & ~OVL_FH_FLAG_ALL)
129 return -ENODATA;
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)
134 return -ENODATA;
136 return 0;
139 static struct ovl_fh *ovl_get_fh(struct dentry *dentry, const char *name)
141 int res, err;
142 struct ovl_fh *fh = NULL;
144 res = vfs_getxattr(dentry, name, NULL, 0);
145 if (res < 0) {
146 if (res == -ENODATA || res == -EOPNOTSUPP)
147 return NULL;
148 goto fail;
150 /* Zero size value means "copied up but origin unknown" */
151 if (res == 0)
152 return NULL;
154 fh = kzalloc(res, GFP_KERNEL);
155 if (!fh)
156 return ERR_PTR(-ENOMEM);
158 res = vfs_getxattr(dentry, name, fh, res);
159 if (res < 0)
160 goto fail;
162 err = ovl_check_fh_len(fh, res);
163 if (err < 0) {
164 if (err == -ENODATA)
165 goto out;
166 goto invalid;
169 return fh;
171 out:
172 kfree(fh);
173 return NULL;
175 fail:
176 pr_warn_ratelimited("overlayfs: failed to get origin (%i)\n", res);
177 goto out;
178 invalid:
179 pr_warn_ratelimited("overlayfs: invalid origin (%*phN)\n", res, fh);
180 goto out;
183 struct dentry *ovl_decode_fh(struct ovl_fh *fh, struct vfsmount *mnt)
185 struct dentry *real;
186 int bytes;
189 * Make sure that the stored uuid matches the uuid of the lower
190 * layer where file handle will be decoded.
192 if (!uuid_equal(&fh->uuid, &mnt->mnt_sb->s_uuid))
193 return NULL;
195 bytes = (fh->len - offsetof(struct ovl_fh, fid));
196 real = exportfs_decode_fh(mnt, (struct fid *)fh->fid,
197 bytes >> 2, (int)fh->type,
198 ovl_acceptable, mnt);
199 if (IS_ERR(real)) {
201 * Treat stale file handle to lower file as "origin unknown".
202 * upper file handle could become stale when upper file is
203 * unlinked and this information is needed to handle stale
204 * index entries correctly.
206 if (real == ERR_PTR(-ESTALE) &&
207 !(fh->flags & OVL_FH_FLAG_PATH_UPPER))
208 real = NULL;
209 return real;
212 if (ovl_dentry_weird(real)) {
213 dput(real);
214 return NULL;
217 return real;
220 static bool ovl_is_opaquedir(struct dentry *dentry)
222 return ovl_check_dir_xattr(dentry, OVL_XATTR_OPAQUE);
225 static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d,
226 const char *name, unsigned int namelen,
227 size_t prelen, const char *post,
228 struct dentry **ret)
230 struct dentry *this;
231 int err;
233 this = lookup_one_len_unlocked(name, base, namelen);
234 if (IS_ERR(this)) {
235 err = PTR_ERR(this);
236 this = NULL;
237 if (err == -ENOENT || err == -ENAMETOOLONG)
238 goto out;
239 goto out_err;
241 if (!this->d_inode)
242 goto put_and_out;
244 if (ovl_dentry_weird(this)) {
245 /* Don't support traversing automounts and other weirdness */
246 err = -EREMOTE;
247 goto out_err;
249 if (ovl_is_whiteout(this)) {
250 d->stop = d->opaque = true;
251 goto put_and_out;
253 if (!d_can_lookup(this)) {
254 d->stop = true;
255 if (d->is_dir)
256 goto put_and_out;
257 goto out;
259 d->is_dir = true;
260 if (!d->last && ovl_is_opaquedir(this)) {
261 d->stop = d->opaque = true;
262 goto out;
264 err = ovl_check_redirect(this, d, prelen, post);
265 if (err)
266 goto out_err;
267 out:
268 *ret = this;
269 return 0;
271 put_and_out:
272 dput(this);
273 this = NULL;
274 goto out;
276 out_err:
277 dput(this);
278 return err;
281 static int ovl_lookup_layer(struct dentry *base, struct ovl_lookup_data *d,
282 struct dentry **ret)
284 /* Counting down from the end, since the prefix can change */
285 size_t rem = d->name.len - 1;
286 struct dentry *dentry = NULL;
287 int err;
289 if (d->name.name[0] != '/')
290 return ovl_lookup_single(base, d, d->name.name, d->name.len,
291 0, "", ret);
293 while (!IS_ERR_OR_NULL(base) && d_can_lookup(base)) {
294 const char *s = d->name.name + d->name.len - rem;
295 const char *next = strchrnul(s, '/');
296 size_t thislen = next - s;
297 bool end = !next[0];
299 /* Verify we did not go off the rails */
300 if (WARN_ON(s[-1] != '/'))
301 return -EIO;
303 err = ovl_lookup_single(base, d, s, thislen,
304 d->name.len - rem, next, &base);
305 dput(dentry);
306 if (err)
307 return err;
308 dentry = base;
309 if (end)
310 break;
312 rem -= thislen + 1;
314 if (WARN_ON(rem >= d->name.len))
315 return -EIO;
317 *ret = dentry;
318 return 0;
322 int ovl_check_origin_fh(struct ovl_fs *ofs, struct ovl_fh *fh,
323 struct dentry *upperdentry, struct ovl_path **stackp)
325 struct dentry *origin = NULL;
326 int i;
328 for (i = 0; i < ofs->numlower; i++) {
329 origin = ovl_decode_fh(fh, ofs->lower_layers[i].mnt);
330 if (origin)
331 break;
334 if (!origin)
335 return -ESTALE;
336 else if (IS_ERR(origin))
337 return PTR_ERR(origin);
339 if (upperdentry && !ovl_is_whiteout(upperdentry) &&
340 ((d_inode(origin)->i_mode ^ d_inode(upperdentry)->i_mode) & S_IFMT))
341 goto invalid;
343 if (!*stackp)
344 *stackp = kmalloc(sizeof(struct ovl_path), GFP_KERNEL);
345 if (!*stackp) {
346 dput(origin);
347 return -ENOMEM;
349 **stackp = (struct ovl_path){
350 .dentry = origin,
351 .layer = &ofs->lower_layers[i]
354 return 0;
356 invalid:
357 pr_warn_ratelimited("overlayfs: invalid origin (%pd2, ftype=%x, origin ftype=%x).\n",
358 upperdentry, d_inode(upperdentry)->i_mode & S_IFMT,
359 d_inode(origin)->i_mode & S_IFMT);
360 dput(origin);
361 return -EIO;
364 static int ovl_check_origin(struct ovl_fs *ofs, struct dentry *upperdentry,
365 struct ovl_path **stackp, unsigned int *ctrp)
367 struct ovl_fh *fh = ovl_get_fh(upperdentry, OVL_XATTR_ORIGIN);
368 int err;
370 if (IS_ERR_OR_NULL(fh))
371 return PTR_ERR(fh);
373 err = ovl_check_origin_fh(ofs, fh, upperdentry, stackp);
374 kfree(fh);
376 if (err) {
377 if (err == -ESTALE)
378 return 0;
379 return err;
382 if (WARN_ON(*ctrp))
383 return -EIO;
385 *ctrp = 1;
386 return 0;
390 * Verify that @fh matches the file handle stored in xattr @name.
391 * Return 0 on match, -ESTALE on mismatch, < 0 on error.
393 static int ovl_verify_fh(struct dentry *dentry, const char *name,
394 const struct ovl_fh *fh)
396 struct ovl_fh *ofh = ovl_get_fh(dentry, name);
397 int err = 0;
399 if (!ofh)
400 return -ENODATA;
402 if (IS_ERR(ofh))
403 return PTR_ERR(ofh);
405 if (fh->len != ofh->len || memcmp(fh, ofh, fh->len))
406 err = -ESTALE;
408 kfree(ofh);
409 return err;
413 * Verify that @real dentry matches the file handle stored in xattr @name.
415 * If @set is true and there is no stored file handle, encode @real and store
416 * file handle in xattr @name.
418 * Return 0 on match, -ESTALE on mismatch, -ENODATA on no xattr, < 0 on error.
420 int ovl_verify_set_fh(struct dentry *dentry, const char *name,
421 struct dentry *real, bool is_upper, bool set)
423 struct inode *inode;
424 struct ovl_fh *fh;
425 int err;
427 fh = ovl_encode_fh(real, is_upper);
428 err = PTR_ERR(fh);
429 if (IS_ERR(fh))
430 goto fail;
432 err = ovl_verify_fh(dentry, name, fh);
433 if (set && err == -ENODATA)
434 err = ovl_do_setxattr(dentry, name, fh, fh->len, 0);
435 if (err)
436 goto fail;
438 out:
439 kfree(fh);
440 return err;
442 fail:
443 inode = d_inode(real);
444 pr_warn_ratelimited("overlayfs: failed to verify %s (%pd2, ino=%lu, err=%i)\n",
445 is_upper ? "upper" : "origin", real,
446 inode ? inode->i_ino : 0, err);
447 goto out;
450 /* Get upper dentry from index */
451 struct dentry *ovl_index_upper(struct ovl_fs *ofs, struct dentry *index)
453 struct ovl_fh *fh;
454 struct dentry *upper;
456 if (!d_is_dir(index))
457 return dget(index);
459 fh = ovl_get_fh(index, OVL_XATTR_UPPER);
460 if (IS_ERR_OR_NULL(fh))
461 return ERR_CAST(fh);
463 upper = ovl_decode_fh(fh, ofs->upper_mnt);
464 kfree(fh);
466 if (IS_ERR_OR_NULL(upper))
467 return upper ?: ERR_PTR(-ESTALE);
469 if (!d_is_dir(upper)) {
470 pr_warn_ratelimited("overlayfs: invalid index upper (%pd2, upper=%pd2).\n",
471 index, upper);
472 dput(upper);
473 return ERR_PTR(-EIO);
476 return upper;
479 /* Is this a leftover from create/whiteout of directory index entry? */
480 static bool ovl_is_temp_index(struct dentry *index)
482 return index->d_name.name[0] == '#';
486 * Verify that an index entry name matches the origin file handle stored in
487 * OVL_XATTR_ORIGIN and that origin file handle can be decoded to lower path.
488 * Return 0 on match, -ESTALE on mismatch or stale origin, < 0 on error.
490 int ovl_verify_index(struct ovl_fs *ofs, struct dentry *index)
492 struct ovl_fh *fh = NULL;
493 size_t len;
494 struct ovl_path origin = { };
495 struct ovl_path *stack = &origin;
496 struct dentry *upper = NULL;
497 int err;
499 if (!d_inode(index))
500 return 0;
502 /* Cleanup leftover from index create/cleanup attempt */
503 err = -ESTALE;
504 if (ovl_is_temp_index(index))
505 goto fail;
507 err = -EINVAL;
508 if (index->d_name.len < sizeof(struct ovl_fh)*2)
509 goto fail;
511 err = -ENOMEM;
512 len = index->d_name.len / 2;
513 fh = kzalloc(len, GFP_KERNEL);
514 if (!fh)
515 goto fail;
517 err = -EINVAL;
518 if (hex2bin((u8 *)fh, index->d_name.name, len))
519 goto fail;
521 err = ovl_check_fh_len(fh, len);
522 if (err)
523 goto fail;
526 * Whiteout index entries are used as an indication that an exported
527 * overlay file handle should be treated as stale (i.e. after unlink
528 * of the overlay inode). These entries contain no origin xattr.
530 if (ovl_is_whiteout(index))
531 goto out;
534 * Verifying directory index entries are not stale is expensive, so
535 * only verify stale dir index if NFS export is enabled.
537 if (d_is_dir(index) && !ofs->config.nfs_export)
538 goto out;
541 * Directory index entries should have 'upper' xattr pointing to the
542 * real upper dir. Non-dir index entries are hardlinks to the upper
543 * real inode. For non-dir index, we can read the copy up origin xattr
544 * directly from the index dentry, but for dir index we first need to
545 * decode the upper directory.
547 upper = ovl_index_upper(ofs, index);
548 if (IS_ERR_OR_NULL(upper)) {
549 err = PTR_ERR(upper);
551 * Directory index entries with no 'upper' xattr need to be
552 * removed. When dir index entry has a stale 'upper' xattr,
553 * we assume that upper dir was removed and we treat the dir
554 * index as orphan entry that needs to be whited out.
556 if (err == -ESTALE)
557 goto orphan;
558 else if (!err)
559 err = -ESTALE;
560 goto fail;
563 err = ovl_verify_fh(upper, OVL_XATTR_ORIGIN, fh);
564 dput(upper);
565 if (err)
566 goto fail;
568 /* Check if non-dir index is orphan and don't warn before cleaning it */
569 if (!d_is_dir(index) && d_inode(index)->i_nlink == 1) {
570 err = ovl_check_origin_fh(ofs, fh, index, &stack);
571 if (err)
572 goto fail;
574 if (ovl_get_nlink(origin.dentry, index, 0) == 0)
575 goto orphan;
578 out:
579 dput(origin.dentry);
580 kfree(fh);
581 return err;
583 fail:
584 pr_warn_ratelimited("overlayfs: failed to verify index (%pd2, ftype=%x, err=%i)\n",
585 index, d_inode(index)->i_mode & S_IFMT, err);
586 goto out;
588 orphan:
589 pr_warn_ratelimited("overlayfs: orphan index entry (%pd2, ftype=%x, nlink=%u)\n",
590 index, d_inode(index)->i_mode & S_IFMT,
591 d_inode(index)->i_nlink);
592 err = -ENOENT;
593 goto out;
596 static int ovl_get_index_name_fh(struct ovl_fh *fh, struct qstr *name)
598 char *n, *s;
600 n = kzalloc(fh->len * 2, GFP_KERNEL);
601 if (!n)
602 return -ENOMEM;
604 s = bin2hex(n, fh, fh->len);
605 *name = (struct qstr) QSTR_INIT(n, s - n);
607 return 0;
612 * Lookup in indexdir for the index entry of a lower real inode or a copy up
613 * origin inode. The index entry name is the hex representation of the lower
614 * inode file handle.
616 * If the index dentry in negative, then either no lower aliases have been
617 * copied up yet, or aliases have been copied up in older kernels and are
618 * not indexed.
620 * If the index dentry for a copy up origin inode is positive, but points
621 * to an inode different than the upper inode, then either the upper inode
622 * has been copied up and not indexed or it was indexed, but since then
623 * index dir was cleared. Either way, that index cannot be used to indentify
624 * the overlay inode.
626 int ovl_get_index_name(struct dentry *origin, struct qstr *name)
628 struct ovl_fh *fh;
629 int err;
631 fh = ovl_encode_fh(origin, false);
632 if (IS_ERR(fh))
633 return PTR_ERR(fh);
635 err = ovl_get_index_name_fh(fh, name);
637 kfree(fh);
638 return err;
641 /* Lookup index by file handle for NFS export */
642 struct dentry *ovl_get_index_fh(struct ovl_fs *ofs, struct ovl_fh *fh)
644 struct dentry *index;
645 struct qstr name;
646 int err;
648 err = ovl_get_index_name_fh(fh, &name);
649 if (err)
650 return ERR_PTR(err);
652 index = lookup_one_len_unlocked(name.name, ofs->indexdir, name.len);
653 kfree(name.name);
654 if (IS_ERR(index)) {
655 if (PTR_ERR(index) == -ENOENT)
656 index = NULL;
657 return index;
660 if (d_is_negative(index))
661 err = 0;
662 else if (ovl_is_whiteout(index))
663 err = -ESTALE;
664 else if (ovl_dentry_weird(index))
665 err = -EIO;
666 else
667 return index;
669 dput(index);
670 return ERR_PTR(err);
673 struct dentry *ovl_lookup_index(struct ovl_fs *ofs, struct dentry *upper,
674 struct dentry *origin, bool verify)
676 struct dentry *index;
677 struct inode *inode;
678 struct qstr name;
679 bool is_dir = d_is_dir(origin);
680 int err;
682 err = ovl_get_index_name(origin, &name);
683 if (err)
684 return ERR_PTR(err);
686 index = lookup_one_len_unlocked(name.name, ofs->indexdir, name.len);
687 if (IS_ERR(index)) {
688 err = PTR_ERR(index);
689 if (err == -ENOENT) {
690 index = NULL;
691 goto out;
693 pr_warn_ratelimited("overlayfs: failed inode index lookup (ino=%lu, key=%*s, err=%i);\n"
694 "overlayfs: mount with '-o index=off' to disable inodes index.\n",
695 d_inode(origin)->i_ino, name.len, name.name,
696 err);
697 goto out;
700 inode = d_inode(index);
701 if (d_is_negative(index)) {
702 goto out_dput;
703 } else if (ovl_is_whiteout(index) && !verify) {
705 * When index lookup is called with !verify for decoding an
706 * overlay file handle, a whiteout index implies that decode
707 * should treat file handle as stale and no need to print a
708 * warning about it.
710 dput(index);
711 index = ERR_PTR(-ESTALE);
712 goto out;
713 } else if (ovl_dentry_weird(index) || ovl_is_whiteout(index) ||
714 ((inode->i_mode ^ d_inode(origin)->i_mode) & S_IFMT)) {
716 * Index should always be of the same file type as origin
717 * except for the case of a whiteout index. A whiteout
718 * index should only exist if all lower aliases have been
719 * unlinked, which means that finding a lower origin on lookup
720 * whose index is a whiteout should be treated as an error.
722 pr_warn_ratelimited("overlayfs: bad index found (index=%pd2, ftype=%x, origin ftype=%x).\n",
723 index, d_inode(index)->i_mode & S_IFMT,
724 d_inode(origin)->i_mode & S_IFMT);
725 goto fail;
726 } else if (is_dir && verify) {
727 if (!upper) {
728 pr_warn_ratelimited("overlayfs: suspected uncovered redirected dir found (origin=%pd2, index=%pd2).\n",
729 origin, index);
730 goto fail;
733 /* Verify that dir index 'upper' xattr points to upper dir */
734 err = ovl_verify_upper(index, upper, false);
735 if (err) {
736 if (err == -ESTALE) {
737 pr_warn_ratelimited("overlayfs: suspected multiply redirected dir found (upper=%pd2, origin=%pd2, index=%pd2).\n",
738 upper, origin, index);
740 goto fail;
742 } else if (upper && d_inode(upper) != inode) {
743 goto out_dput;
745 out:
746 kfree(name.name);
747 return index;
749 out_dput:
750 dput(index);
751 index = NULL;
752 goto out;
754 fail:
755 dput(index);
756 index = ERR_PTR(-EIO);
757 goto out;
761 * Returns next layer in stack starting from top.
762 * Returns -1 if this is the last layer.
764 int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
766 struct ovl_entry *oe = dentry->d_fsdata;
768 BUG_ON(idx < 0);
769 if (idx == 0) {
770 ovl_path_upper(dentry, path);
771 if (path->dentry)
772 return oe->numlower ? 1 : -1;
773 idx++;
775 BUG_ON(idx > oe->numlower);
776 path->dentry = oe->lowerstack[idx - 1].dentry;
777 path->mnt = oe->lowerstack[idx - 1].layer->mnt;
779 return (idx < oe->numlower) ? idx + 1 : -1;
782 /* Fix missing 'origin' xattr */
783 static int ovl_fix_origin(struct dentry *dentry, struct dentry *lower,
784 struct dentry *upper)
786 int err;
788 if (ovl_check_origin_xattr(upper))
789 return 0;
791 err = ovl_want_write(dentry);
792 if (err)
793 return err;
795 err = ovl_set_origin(dentry, lower, upper);
796 if (!err)
797 err = ovl_set_impure(dentry->d_parent, upper->d_parent);
799 ovl_drop_write(dentry);
800 return err;
803 struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
804 unsigned int flags)
806 struct ovl_entry *oe;
807 const struct cred *old_cred;
808 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
809 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
810 struct ovl_entry *roe = dentry->d_sb->s_root->d_fsdata;
811 struct ovl_path *stack = NULL;
812 struct dentry *upperdir, *upperdentry = NULL;
813 struct dentry *origin = NULL;
814 struct dentry *index = NULL;
815 unsigned int ctr = 0;
816 struct inode *inode = NULL;
817 bool upperopaque = false;
818 char *upperredirect = NULL;
819 struct dentry *this;
820 unsigned int i;
821 int err;
822 struct ovl_lookup_data d = {
823 .name = dentry->d_name,
824 .is_dir = false,
825 .opaque = false,
826 .stop = false,
827 .last = ofs->config.redirect_follow ? false : !poe->numlower,
828 .redirect = NULL,
831 if (dentry->d_name.len > ofs->namelen)
832 return ERR_PTR(-ENAMETOOLONG);
834 old_cred = ovl_override_creds(dentry->d_sb);
835 upperdir = ovl_dentry_upper(dentry->d_parent);
836 if (upperdir) {
837 err = ovl_lookup_layer(upperdir, &d, &upperdentry);
838 if (err)
839 goto out;
841 if (upperdentry && unlikely(ovl_dentry_remote(upperdentry))) {
842 dput(upperdentry);
843 err = -EREMOTE;
844 goto out;
846 if (upperdentry && !d.is_dir) {
847 BUG_ON(!d.stop || d.redirect);
849 * Lookup copy up origin by decoding origin file handle.
850 * We may get a disconnected dentry, which is fine,
851 * because we only need to hold the origin inode in
852 * cache and use its inode number. We may even get a
853 * connected dentry, that is not under any of the lower
854 * layers root. That is also fine for using it's inode
855 * number - it's the same as if we held a reference
856 * to a dentry in lower layer that was moved under us.
858 err = ovl_check_origin(ofs, upperdentry, &stack, &ctr);
859 if (err)
860 goto out_put_upper;
863 if (d.redirect) {
864 err = -ENOMEM;
865 upperredirect = kstrdup(d.redirect, GFP_KERNEL);
866 if (!upperredirect)
867 goto out_put_upper;
868 if (d.redirect[0] == '/')
869 poe = roe;
871 upperopaque = d.opaque;
874 if (!d.stop && poe->numlower) {
875 err = -ENOMEM;
876 stack = kcalloc(ofs->numlower, sizeof(struct ovl_path),
877 GFP_KERNEL);
878 if (!stack)
879 goto out_put_upper;
882 for (i = 0; !d.stop && i < poe->numlower; i++) {
883 struct ovl_path lower = poe->lowerstack[i];
885 if (!ofs->config.redirect_follow)
886 d.last = i == poe->numlower - 1;
887 else
888 d.last = lower.layer->idx == roe->numlower;
890 err = ovl_lookup_layer(lower.dentry, &d, &this);
891 if (err)
892 goto out_put;
894 if (!this)
895 continue;
898 * If no origin fh is stored in upper of a merge dir, store fh
899 * of lower dir and set upper parent "impure".
901 if (upperdentry && !ctr && !ofs->noxattr) {
902 err = ovl_fix_origin(dentry, this, upperdentry);
903 if (err) {
904 dput(this);
905 goto out_put;
910 * When "verify_lower" feature is enabled, do not merge with a
911 * lower dir that does not match a stored origin xattr. In any
912 * case, only verified origin is used for index lookup.
914 if (upperdentry && !ctr && ovl_verify_lower(dentry->d_sb)) {
915 err = ovl_verify_origin(upperdentry, this, false);
916 if (err) {
917 dput(this);
918 break;
921 /* Bless lower dir as verified origin */
922 origin = this;
925 stack[ctr].dentry = this;
926 stack[ctr].layer = lower.layer;
927 ctr++;
930 * Following redirects can have security consequences: it's like
931 * a symlink into the lower layer without the permission checks.
932 * This is only a problem if the upper layer is untrusted (e.g
933 * comes from an USB drive). This can allow a non-readable file
934 * or directory to become readable.
936 * Only following redirects when redirects are enabled disables
937 * this attack vector when not necessary.
939 err = -EPERM;
940 if (d.redirect && !ofs->config.redirect_follow) {
941 pr_warn_ratelimited("overlayfs: refusing to follow redirect for (%pd2)\n",
942 dentry);
943 goto out_put;
946 if (d.stop)
947 break;
949 if (d.redirect && d.redirect[0] == '/' && poe != roe) {
950 poe = roe;
951 /* Find the current layer on the root dentry */
952 i = lower.layer->idx - 1;
957 * Lookup index by lower inode and verify it matches upper inode.
958 * We only trust dir index if we verified that lower dir matches
959 * origin, otherwise dir index entries may be inconsistent and we
960 * ignore them. Always lookup index of non-dir and non-upper.
962 if (ctr && (!upperdentry || !d.is_dir))
963 origin = stack[0].dentry;
965 if (origin && ovl_indexdir(dentry->d_sb) &&
966 (!d.is_dir || ovl_index_all(dentry->d_sb))) {
967 index = ovl_lookup_index(ofs, upperdentry, origin, true);
968 if (IS_ERR(index)) {
969 err = PTR_ERR(index);
970 index = NULL;
971 goto out_put;
975 oe = ovl_alloc_entry(ctr);
976 err = -ENOMEM;
977 if (!oe)
978 goto out_put;
980 memcpy(oe->lowerstack, stack, sizeof(struct ovl_path) * ctr);
981 dentry->d_fsdata = oe;
983 if (upperopaque)
984 ovl_dentry_set_opaque(dentry);
986 if (upperdentry)
987 ovl_dentry_set_upper_alias(dentry);
988 else if (index)
989 upperdentry = dget(index);
991 if (upperdentry || ctr) {
992 if (ctr)
993 origin = stack[0].dentry;
994 inode = ovl_get_inode(dentry->d_sb, upperdentry, origin, index,
995 ctr);
996 err = PTR_ERR(inode);
997 if (IS_ERR(inode))
998 goto out_free_oe;
1000 OVL_I(inode)->redirect = upperredirect;
1001 if (index)
1002 ovl_set_flag(OVL_INDEX, inode);
1005 revert_creds(old_cred);
1006 dput(index);
1007 kfree(stack);
1008 kfree(d.redirect);
1009 return d_splice_alias(inode, dentry);
1011 out_free_oe:
1012 dentry->d_fsdata = NULL;
1013 kfree(oe);
1014 out_put:
1015 dput(index);
1016 for (i = 0; i < ctr; i++)
1017 dput(stack[i].dentry);
1018 kfree(stack);
1019 out_put_upper:
1020 dput(upperdentry);
1021 kfree(upperredirect);
1022 out:
1023 kfree(d.redirect);
1024 revert_creds(old_cred);
1025 return ERR_PTR(err);
1028 bool ovl_lower_positive(struct dentry *dentry)
1030 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
1031 const struct qstr *name = &dentry->d_name;
1032 const struct cred *old_cred;
1033 unsigned int i;
1034 bool positive = false;
1035 bool done = false;
1038 * If dentry is negative, then lower is positive iff this is a
1039 * whiteout.
1041 if (!dentry->d_inode)
1042 return ovl_dentry_is_opaque(dentry);
1044 /* Negative upper -> positive lower */
1045 if (!ovl_dentry_upper(dentry))
1046 return true;
1048 old_cred = ovl_override_creds(dentry->d_sb);
1049 /* Positive upper -> have to look up lower to see whether it exists */
1050 for (i = 0; !done && !positive && i < poe->numlower; i++) {
1051 struct dentry *this;
1052 struct dentry *lowerdir = poe->lowerstack[i].dentry;
1054 this = lookup_one_len_unlocked(name->name, lowerdir,
1055 name->len);
1056 if (IS_ERR(this)) {
1057 switch (PTR_ERR(this)) {
1058 case -ENOENT:
1059 case -ENAMETOOLONG:
1060 break;
1062 default:
1064 * Assume something is there, we just couldn't
1065 * access it.
1067 positive = true;
1068 break;
1070 } else {
1071 if (this->d_inode) {
1072 positive = !ovl_is_whiteout(this);
1073 done = true;
1075 dput(this);
1078 revert_creds(old_cred);
1080 return positive;