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