xref: /openbmc/linux/fs/overlayfs/namei.c (revision 5b2cccd32c668de6bd1979545184cd7f0260f053)
1bbb1e54dSMiklos Szeredi /*
2bbb1e54dSMiklos Szeredi  * Copyright (C) 2011 Novell Inc.
3bbb1e54dSMiklos Szeredi  * Copyright (C) 2016 Red Hat, Inc.
4bbb1e54dSMiklos Szeredi  *
5bbb1e54dSMiklos Szeredi  * This program is free software; you can redistribute it and/or modify it
6bbb1e54dSMiklos Szeredi  * under the terms of the GNU General Public License version 2 as published by
7bbb1e54dSMiklos Szeredi  * the Free Software Foundation.
8bbb1e54dSMiklos Szeredi  */
9bbb1e54dSMiklos Szeredi 
10bbb1e54dSMiklos Szeredi #include <linux/fs.h>
115b825c3aSIngo Molnar #include <linux/cred.h>
129ee60ce2SAmir Goldstein #include <linux/ctype.h>
13bbb1e54dSMiklos Szeredi #include <linux/namei.h>
14bbb1e54dSMiklos Szeredi #include <linux/xattr.h>
1502b69b28SMiklos Szeredi #include <linux/ratelimit.h>
16a9d01957SAmir Goldstein #include <linux/mount.h>
17a9d01957SAmir Goldstein #include <linux/exportfs.h>
18bbb1e54dSMiklos Szeredi #include "overlayfs.h"
19bbb1e54dSMiklos Szeredi 
20e28edc46SMiklos Szeredi struct ovl_lookup_data {
21e28edc46SMiklos Szeredi 	struct qstr name;
22e28edc46SMiklos Szeredi 	bool is_dir;
23e28edc46SMiklos Szeredi 	bool opaque;
24e28edc46SMiklos Szeredi 	bool stop;
25e28edc46SMiklos Szeredi 	bool last;
2602b69b28SMiklos Szeredi 	char *redirect;
27e28edc46SMiklos Szeredi };
28bbb1e54dSMiklos Szeredi 
2902b69b28SMiklos Szeredi static int ovl_check_redirect(struct dentry *dentry, struct ovl_lookup_data *d,
3002b69b28SMiklos Szeredi 			      size_t prelen, const char *post)
3102b69b28SMiklos Szeredi {
3202b69b28SMiklos Szeredi 	int res;
3302b69b28SMiklos Szeredi 	char *s, *next, *buf = NULL;
3402b69b28SMiklos Szeredi 
3502b69b28SMiklos Szeredi 	res = vfs_getxattr(dentry, OVL_XATTR_REDIRECT, NULL, 0);
3602b69b28SMiklos Szeredi 	if (res < 0) {
3702b69b28SMiklos Szeredi 		if (res == -ENODATA || res == -EOPNOTSUPP)
3802b69b28SMiklos Szeredi 			return 0;
3902b69b28SMiklos Szeredi 		goto fail;
4002b69b28SMiklos Szeredi 	}
410ee931c4SMichal Hocko 	buf = kzalloc(prelen + res + strlen(post) + 1, GFP_KERNEL);
4202b69b28SMiklos Szeredi 	if (!buf)
4302b69b28SMiklos Szeredi 		return -ENOMEM;
4402b69b28SMiklos Szeredi 
4502b69b28SMiklos Szeredi 	if (res == 0)
4602b69b28SMiklos Szeredi 		goto invalid;
4702b69b28SMiklos Szeredi 
4802b69b28SMiklos Szeredi 	res = vfs_getxattr(dentry, OVL_XATTR_REDIRECT, buf, res);
4902b69b28SMiklos Szeredi 	if (res < 0)
5002b69b28SMiklos Szeredi 		goto fail;
5102b69b28SMiklos Szeredi 	if (res == 0)
5202b69b28SMiklos Szeredi 		goto invalid;
5302b69b28SMiklos Szeredi 	if (buf[0] == '/') {
5402b69b28SMiklos Szeredi 		for (s = buf; *s++ == '/'; s = next) {
5502b69b28SMiklos Szeredi 			next = strchrnul(s, '/');
5602b69b28SMiklos Szeredi 			if (s == next)
5702b69b28SMiklos Szeredi 				goto invalid;
5802b69b28SMiklos Szeredi 		}
593ec9b3faSAmir Goldstein 		/*
603ec9b3faSAmir Goldstein 		 * One of the ancestor path elements in an absolute path
613ec9b3faSAmir Goldstein 		 * lookup in ovl_lookup_layer() could have been opaque and
623ec9b3faSAmir Goldstein 		 * that will stop further lookup in lower layers (d->stop=true)
633ec9b3faSAmir Goldstein 		 * But we have found an absolute redirect in decendant path
643ec9b3faSAmir Goldstein 		 * element and that should force continue lookup in lower
653ec9b3faSAmir Goldstein 		 * layers (reset d->stop).
663ec9b3faSAmir Goldstein 		 */
673ec9b3faSAmir Goldstein 		d->stop = false;
6802b69b28SMiklos Szeredi 	} else {
6902b69b28SMiklos Szeredi 		if (strchr(buf, '/') != NULL)
7002b69b28SMiklos Szeredi 			goto invalid;
7102b69b28SMiklos Szeredi 
7202b69b28SMiklos Szeredi 		memmove(buf + prelen, buf, res);
7302b69b28SMiklos Szeredi 		memcpy(buf, d->name.name, prelen);
7402b69b28SMiklos Szeredi 	}
7502b69b28SMiklos Szeredi 
7602b69b28SMiklos Szeredi 	strcat(buf, post);
7702b69b28SMiklos Szeredi 	kfree(d->redirect);
7802b69b28SMiklos Szeredi 	d->redirect = buf;
7902b69b28SMiklos Szeredi 	d->name.name = d->redirect;
8002b69b28SMiklos Szeredi 	d->name.len = strlen(d->redirect);
8102b69b28SMiklos Szeredi 
8202b69b28SMiklos Szeredi 	return 0;
8302b69b28SMiklos Szeredi 
8402b69b28SMiklos Szeredi err_free:
8502b69b28SMiklos Szeredi 	kfree(buf);
8602b69b28SMiklos Szeredi 	return 0;
8702b69b28SMiklos Szeredi fail:
8802b69b28SMiklos Szeredi 	pr_warn_ratelimited("overlayfs: failed to get redirect (%i)\n", res);
8902b69b28SMiklos Szeredi 	goto err_free;
9002b69b28SMiklos Szeredi invalid:
9102b69b28SMiklos Szeredi 	pr_warn_ratelimited("overlayfs: invalid redirect (%s)\n", buf);
9202b69b28SMiklos Szeredi 	goto err_free;
9302b69b28SMiklos Szeredi }
9402b69b28SMiklos Szeredi 
95a9d01957SAmir Goldstein static int ovl_acceptable(void *ctx, struct dentry *dentry)
96a9d01957SAmir Goldstein {
97e8f9e5b7SAmir Goldstein 	/*
98e8f9e5b7SAmir Goldstein 	 * A non-dir origin may be disconnected, which is fine, because
99e8f9e5b7SAmir Goldstein 	 * we only need it for its unique inode number.
100e8f9e5b7SAmir Goldstein 	 */
101e8f9e5b7SAmir Goldstein 	if (!d_is_dir(dentry))
102a9d01957SAmir Goldstein 		return 1;
103e8f9e5b7SAmir Goldstein 
104e8f9e5b7SAmir Goldstein 	/* Don't decode a deleted empty directory */
105e8f9e5b7SAmir Goldstein 	if (d_unhashed(dentry))
106e8f9e5b7SAmir Goldstein 		return 0;
107e8f9e5b7SAmir Goldstein 
108e8f9e5b7SAmir Goldstein 	/* Check if directory belongs to the layer we are decoding from */
109e8f9e5b7SAmir Goldstein 	return is_subdir(dentry, ((struct vfsmount *)ctx)->mnt_root);
110a9d01957SAmir Goldstein }
111a9d01957SAmir Goldstein 
1122e1a5328SAmir Goldstein /*
1132e1a5328SAmir Goldstein  * Check validity of an overlay file handle buffer.
1142e1a5328SAmir Goldstein  *
1152e1a5328SAmir Goldstein  * Return 0 for a valid file handle.
1162e1a5328SAmir Goldstein  * Return -ENODATA for "origin unknown".
1172e1a5328SAmir Goldstein  * Return <0 for an invalid file handle.
1182e1a5328SAmir Goldstein  */
1198556a420SAmir Goldstein int ovl_check_fh_len(struct ovl_fh *fh, int fh_len)
1202e1a5328SAmir Goldstein {
1212e1a5328SAmir Goldstein 	if (fh_len < sizeof(struct ovl_fh) || fh_len < fh->len)
1222e1a5328SAmir Goldstein 		return -EINVAL;
1232e1a5328SAmir Goldstein 
1242e1a5328SAmir Goldstein 	if (fh->magic != OVL_FH_MAGIC)
1252e1a5328SAmir Goldstein 		return -EINVAL;
1262e1a5328SAmir Goldstein 
1272e1a5328SAmir Goldstein 	/* Treat larger version and unknown flags as "origin unknown" */
1282e1a5328SAmir Goldstein 	if (fh->version > OVL_FH_VERSION || fh->flags & ~OVL_FH_FLAG_ALL)
1292e1a5328SAmir Goldstein 		return -ENODATA;
1302e1a5328SAmir Goldstein 
1312e1a5328SAmir Goldstein 	/* Treat endianness mismatch as "origin unknown" */
1322e1a5328SAmir Goldstein 	if (!(fh->flags & OVL_FH_FLAG_ANY_ENDIAN) &&
1332e1a5328SAmir Goldstein 	    (fh->flags & OVL_FH_FLAG_BIG_ENDIAN) != OVL_FH_FLAG_CPU_ENDIAN)
1342e1a5328SAmir Goldstein 		return -ENODATA;
1352e1a5328SAmir Goldstein 
1362e1a5328SAmir Goldstein 	return 0;
1372e1a5328SAmir Goldstein }
1382e1a5328SAmir Goldstein 
13905122443SAmir Goldstein static struct ovl_fh *ovl_get_fh(struct dentry *dentry, const char *name)
140a9d01957SAmir Goldstein {
1412e1a5328SAmir Goldstein 	int res, err;
142a9d01957SAmir Goldstein 	struct ovl_fh *fh = NULL;
143a9d01957SAmir Goldstein 
14405122443SAmir Goldstein 	res = vfs_getxattr(dentry, name, NULL, 0);
145a9d01957SAmir Goldstein 	if (res < 0) {
146a9d01957SAmir Goldstein 		if (res == -ENODATA || res == -EOPNOTSUPP)
147a9d01957SAmir Goldstein 			return NULL;
148a9d01957SAmir Goldstein 		goto fail;
149a9d01957SAmir Goldstein 	}
150a9d01957SAmir Goldstein 	/* Zero size value means "copied up but origin unknown" */
151a9d01957SAmir Goldstein 	if (res == 0)
152a9d01957SAmir Goldstein 		return NULL;
153a9d01957SAmir Goldstein 
1540ee931c4SMichal Hocko 	fh = kzalloc(res, GFP_KERNEL);
155a9d01957SAmir Goldstein 	if (!fh)
156a9d01957SAmir Goldstein 		return ERR_PTR(-ENOMEM);
157a9d01957SAmir Goldstein 
15805122443SAmir Goldstein 	res = vfs_getxattr(dentry, name, fh, res);
159a9d01957SAmir Goldstein 	if (res < 0)
160a9d01957SAmir Goldstein 		goto fail;
161a9d01957SAmir Goldstein 
1622e1a5328SAmir Goldstein 	err = ovl_check_fh_len(fh, res);
1632e1a5328SAmir Goldstein 	if (err < 0) {
1642e1a5328SAmir Goldstein 		if (err == -ENODATA)
165a9d01957SAmir Goldstein 			goto out;
1662e1a5328SAmir Goldstein 		goto invalid;
1672e1a5328SAmir Goldstein 	}
168a9d01957SAmir Goldstein 
1698b88a2e6SAmir Goldstein 	return fh;
1708b88a2e6SAmir Goldstein 
1718b88a2e6SAmir Goldstein out:
1728b88a2e6SAmir Goldstein 	kfree(fh);
1738b88a2e6SAmir Goldstein 	return NULL;
1748b88a2e6SAmir Goldstein 
1758b88a2e6SAmir Goldstein fail:
1768b88a2e6SAmir Goldstein 	pr_warn_ratelimited("overlayfs: failed to get origin (%i)\n", res);
1778b88a2e6SAmir Goldstein 	goto out;
1788b88a2e6SAmir Goldstein invalid:
1798b88a2e6SAmir Goldstein 	pr_warn_ratelimited("overlayfs: invalid origin (%*phN)\n", res, fh);
1808b88a2e6SAmir Goldstein 	goto out;
1818b88a2e6SAmir Goldstein }
1828b88a2e6SAmir Goldstein 
183*5b2cccd3SAmir Goldstein struct dentry *ovl_decode_real_fh(struct ovl_fh *fh, struct vfsmount *mnt)
1848b88a2e6SAmir Goldstein {
185e8f9e5b7SAmir Goldstein 	struct dentry *real;
1868b88a2e6SAmir Goldstein 	int bytes;
1878b88a2e6SAmir Goldstein 
188a9d01957SAmir Goldstein 	/*
189a9d01957SAmir Goldstein 	 * Make sure that the stored uuid matches the uuid of the lower
190a9d01957SAmir Goldstein 	 * layer where file handle will be decoded.
191a9d01957SAmir Goldstein 	 */
19285787090SChristoph Hellwig 	if (!uuid_equal(&fh->uuid, &mnt->mnt_sb->s_uuid))
1932e1a5328SAmir Goldstein 		return NULL;
194a9d01957SAmir Goldstein 
1958b88a2e6SAmir Goldstein 	bytes = (fh->len - offsetof(struct ovl_fh, fid));
196e8f9e5b7SAmir Goldstein 	real = exportfs_decode_fh(mnt, (struct fid *)fh->fid,
197a9d01957SAmir Goldstein 				  bytes >> 2, (int)fh->type,
198e8f9e5b7SAmir Goldstein 				  ovl_acceptable, mnt);
199e8f9e5b7SAmir Goldstein 	if (IS_ERR(real)) {
200e8f9e5b7SAmir Goldstein 		/*
201e8f9e5b7SAmir Goldstein 		 * Treat stale file handle to lower file as "origin unknown".
202e8f9e5b7SAmir Goldstein 		 * upper file handle could become stale when upper file is
203e8f9e5b7SAmir Goldstein 		 * unlinked and this information is needed to handle stale
204e8f9e5b7SAmir Goldstein 		 * index entries correctly.
205e8f9e5b7SAmir Goldstein 		 */
206e8f9e5b7SAmir Goldstein 		if (real == ERR_PTR(-ESTALE) &&
207e8f9e5b7SAmir Goldstein 		    !(fh->flags & OVL_FH_FLAG_PATH_UPPER))
208e8f9e5b7SAmir Goldstein 			real = NULL;
209e8f9e5b7SAmir Goldstein 		return real;
210a9d01957SAmir Goldstein 	}
211a9d01957SAmir Goldstein 
212e8f9e5b7SAmir Goldstein 	if (ovl_dentry_weird(real)) {
213e8f9e5b7SAmir Goldstein 		dput(real);
2142e1a5328SAmir Goldstein 		return NULL;
2152e1a5328SAmir Goldstein 	}
2162e1a5328SAmir Goldstein 
217e8f9e5b7SAmir Goldstein 	return real;
218a9d01957SAmir Goldstein }
219a9d01957SAmir Goldstein 
220ee1d6d37SAmir Goldstein static bool ovl_is_opaquedir(struct dentry *dentry)
221ee1d6d37SAmir Goldstein {
222ee1d6d37SAmir Goldstein 	return ovl_check_dir_xattr(dentry, OVL_XATTR_OPAQUE);
223ee1d6d37SAmir Goldstein }
224ee1d6d37SAmir Goldstein 
225e28edc46SMiklos Szeredi static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d,
226e28edc46SMiklos Szeredi 			     const char *name, unsigned int namelen,
22702b69b28SMiklos Szeredi 			     size_t prelen, const char *post,
228e28edc46SMiklos Szeredi 			     struct dentry **ret)
229e28edc46SMiklos Szeredi {
230e28edc46SMiklos Szeredi 	struct dentry *this;
231e28edc46SMiklos Szeredi 	int err;
232e28edc46SMiklos Szeredi 
233e28edc46SMiklos Szeredi 	this = lookup_one_len_unlocked(name, base, namelen);
234e28edc46SMiklos Szeredi 	if (IS_ERR(this)) {
235e28edc46SMiklos Szeredi 		err = PTR_ERR(this);
236e28edc46SMiklos Szeredi 		this = NULL;
237e28edc46SMiklos Szeredi 		if (err == -ENOENT || err == -ENAMETOOLONG)
238e28edc46SMiklos Szeredi 			goto out;
239e28edc46SMiklos Szeredi 		goto out_err;
240e28edc46SMiklos Szeredi 	}
241e28edc46SMiklos Szeredi 	if (!this->d_inode)
242e28edc46SMiklos Szeredi 		goto put_and_out;
243e28edc46SMiklos Szeredi 
244e28edc46SMiklos Szeredi 	if (ovl_dentry_weird(this)) {
245e28edc46SMiklos Szeredi 		/* Don't support traversing automounts and other weirdness */
246e28edc46SMiklos Szeredi 		err = -EREMOTE;
247e28edc46SMiklos Szeredi 		goto out_err;
248e28edc46SMiklos Szeredi 	}
249e28edc46SMiklos Szeredi 	if (ovl_is_whiteout(this)) {
250e28edc46SMiklos Szeredi 		d->stop = d->opaque = true;
251e28edc46SMiklos Szeredi 		goto put_and_out;
252e28edc46SMiklos Szeredi 	}
253e28edc46SMiklos Szeredi 	if (!d_can_lookup(this)) {
254e28edc46SMiklos Szeredi 		d->stop = true;
255e28edc46SMiklos Szeredi 		if (d->is_dir)
256e28edc46SMiklos Szeredi 			goto put_and_out;
257e28edc46SMiklos Szeredi 		goto out;
258e28edc46SMiklos Szeredi 	}
259e28edc46SMiklos Szeredi 	d->is_dir = true;
260e28edc46SMiklos Szeredi 	if (!d->last && ovl_is_opaquedir(this)) {
261e28edc46SMiklos Szeredi 		d->stop = d->opaque = true;
262e28edc46SMiklos Szeredi 		goto out;
263e28edc46SMiklos Szeredi 	}
26402b69b28SMiklos Szeredi 	err = ovl_check_redirect(this, d, prelen, post);
26502b69b28SMiklos Szeredi 	if (err)
26602b69b28SMiklos Szeredi 		goto out_err;
267e28edc46SMiklos Szeredi out:
268e28edc46SMiklos Szeredi 	*ret = this;
269e28edc46SMiklos Szeredi 	return 0;
270e28edc46SMiklos Szeredi 
271e28edc46SMiklos Szeredi put_and_out:
272e28edc46SMiklos Szeredi 	dput(this);
273e28edc46SMiklos Szeredi 	this = NULL;
274e28edc46SMiklos Szeredi 	goto out;
275e28edc46SMiklos Szeredi 
276e28edc46SMiklos Szeredi out_err:
277e28edc46SMiklos Szeredi 	dput(this);
278e28edc46SMiklos Szeredi 	return err;
279e28edc46SMiklos Szeredi }
280e28edc46SMiklos Szeredi 
281e28edc46SMiklos Szeredi static int ovl_lookup_layer(struct dentry *base, struct ovl_lookup_data *d,
282e28edc46SMiklos Szeredi 			    struct dentry **ret)
283e28edc46SMiklos Szeredi {
2844c7d0c9cSAmir Goldstein 	/* Counting down from the end, since the prefix can change */
2854c7d0c9cSAmir Goldstein 	size_t rem = d->name.len - 1;
28602b69b28SMiklos Szeredi 	struct dentry *dentry = NULL;
28702b69b28SMiklos Szeredi 	int err;
28802b69b28SMiklos Szeredi 
2894c7d0c9cSAmir Goldstein 	if (d->name.name[0] != '/')
29002b69b28SMiklos Szeredi 		return ovl_lookup_single(base, d, d->name.name, d->name.len,
29102b69b28SMiklos Szeredi 					 0, "", ret);
29202b69b28SMiklos Szeredi 
2934c7d0c9cSAmir Goldstein 	while (!IS_ERR_OR_NULL(base) && d_can_lookup(base)) {
2944c7d0c9cSAmir Goldstein 		const char *s = d->name.name + d->name.len - rem;
29502b69b28SMiklos Szeredi 		const char *next = strchrnul(s, '/');
2964c7d0c9cSAmir Goldstein 		size_t thislen = next - s;
2974c7d0c9cSAmir Goldstein 		bool end = !next[0];
29802b69b28SMiklos Szeredi 
2994c7d0c9cSAmir Goldstein 		/* Verify we did not go off the rails */
3004c7d0c9cSAmir Goldstein 		if (WARN_ON(s[-1] != '/'))
30102b69b28SMiklos Szeredi 			return -EIO;
30202b69b28SMiklos Szeredi 
3034c7d0c9cSAmir Goldstein 		err = ovl_lookup_single(base, d, s, thislen,
3044c7d0c9cSAmir Goldstein 					d->name.len - rem, next, &base);
30502b69b28SMiklos Szeredi 		dput(dentry);
30602b69b28SMiklos Szeredi 		if (err)
30702b69b28SMiklos Szeredi 			return err;
30802b69b28SMiklos Szeredi 		dentry = base;
3094c7d0c9cSAmir Goldstein 		if (end)
3104c7d0c9cSAmir Goldstein 			break;
3114c7d0c9cSAmir Goldstein 
3124c7d0c9cSAmir Goldstein 		rem -= thislen + 1;
3134c7d0c9cSAmir Goldstein 
3144c7d0c9cSAmir Goldstein 		if (WARN_ON(rem >= d->name.len))
3154c7d0c9cSAmir Goldstein 			return -EIO;
31602b69b28SMiklos Szeredi 	}
31702b69b28SMiklos Szeredi 	*ret = dentry;
31802b69b28SMiklos Szeredi 	return 0;
319e28edc46SMiklos Szeredi }
320e28edc46SMiklos Szeredi 
321a9d01957SAmir Goldstein 
322f941866fSAmir Goldstein int ovl_check_origin_fh(struct ovl_fs *ofs, struct ovl_fh *fh,
323f941866fSAmir Goldstein 			struct dentry *upperdentry, struct ovl_path **stackp)
324a9d01957SAmir Goldstein {
325f7d3dacaSAmir Goldstein 	struct dentry *origin = NULL;
326f7d3dacaSAmir Goldstein 	int i;
327a9d01957SAmir Goldstein 
3281eff1a1dSAmir Goldstein 	for (i = 0; i < ofs->numlower; i++) {
329*5b2cccd3SAmir Goldstein 		origin = ovl_decode_real_fh(fh, ofs->lower_layers[i].mnt);
330f7d3dacaSAmir Goldstein 		if (origin)
331f7d3dacaSAmir Goldstein 			break;
332f7d3dacaSAmir Goldstein 	}
333f7d3dacaSAmir Goldstein 
334f7d3dacaSAmir Goldstein 	if (!origin)
3352e1a5328SAmir Goldstein 		return -ESTALE;
3362e1a5328SAmir Goldstein 	else if (IS_ERR(origin))
3372e1a5328SAmir Goldstein 		return PTR_ERR(origin);
338f7d3dacaSAmir Goldstein 
339f941866fSAmir Goldstein 	if (upperdentry && !ovl_is_whiteout(upperdentry) &&
3402e1a5328SAmir Goldstein 	    ((d_inode(origin)->i_mode ^ d_inode(upperdentry)->i_mode) & S_IFMT))
3412e1a5328SAmir Goldstein 		goto invalid;
3422e1a5328SAmir Goldstein 
343415543d5SAmir Goldstein 	if (!*stackp)
344b9343632SChandan Rajendra 		*stackp = kmalloc(sizeof(struct ovl_path), GFP_KERNEL);
345a9d01957SAmir Goldstein 	if (!*stackp) {
346a9d01957SAmir Goldstein 		dput(origin);
347a9d01957SAmir Goldstein 		return -ENOMEM;
348a9d01957SAmir Goldstein 	}
3491eff1a1dSAmir Goldstein 	**stackp = (struct ovl_path){
3501eff1a1dSAmir Goldstein 		.dentry = origin,
3511eff1a1dSAmir Goldstein 		.layer = &ofs->lower_layers[i]
3521eff1a1dSAmir Goldstein 	};
353a9d01957SAmir Goldstein 
354a9d01957SAmir Goldstein 	return 0;
3552e1a5328SAmir Goldstein 
3562e1a5328SAmir Goldstein invalid:
3572e1a5328SAmir Goldstein 	pr_warn_ratelimited("overlayfs: invalid origin (%pd2, ftype=%x, origin ftype=%x).\n",
3582e1a5328SAmir Goldstein 			    upperdentry, d_inode(upperdentry)->i_mode & S_IFMT,
3592e1a5328SAmir Goldstein 			    d_inode(origin)->i_mode & S_IFMT);
3602e1a5328SAmir Goldstein 	dput(origin);
3612e1a5328SAmir Goldstein 	return -EIO;
3622e1a5328SAmir Goldstein }
3632e1a5328SAmir Goldstein 
3641eff1a1dSAmir Goldstein static int ovl_check_origin(struct ovl_fs *ofs, struct dentry *upperdentry,
3652e1a5328SAmir Goldstein 			    struct ovl_path **stackp, unsigned int *ctrp)
3662e1a5328SAmir Goldstein {
36705122443SAmir Goldstein 	struct ovl_fh *fh = ovl_get_fh(upperdentry, OVL_XATTR_ORIGIN);
3682e1a5328SAmir Goldstein 	int err;
3692e1a5328SAmir Goldstein 
3702e1a5328SAmir Goldstein 	if (IS_ERR_OR_NULL(fh))
3712e1a5328SAmir Goldstein 		return PTR_ERR(fh);
3722e1a5328SAmir Goldstein 
3731eff1a1dSAmir Goldstein 	err = ovl_check_origin_fh(ofs, fh, upperdentry, stackp);
3742e1a5328SAmir Goldstein 	kfree(fh);
3752e1a5328SAmir Goldstein 
3762e1a5328SAmir Goldstein 	if (err) {
3772e1a5328SAmir Goldstein 		if (err == -ESTALE)
3782e1a5328SAmir Goldstein 			return 0;
3792e1a5328SAmir Goldstein 		return err;
3802e1a5328SAmir Goldstein 	}
3812e1a5328SAmir Goldstein 
3822e1a5328SAmir Goldstein 	if (WARN_ON(*ctrp))
3832e1a5328SAmir Goldstein 		return -EIO;
3842e1a5328SAmir Goldstein 
3852e1a5328SAmir Goldstein 	*ctrp = 1;
3862e1a5328SAmir Goldstein 	return 0;
387a9d01957SAmir Goldstein }
388a9d01957SAmir Goldstein 
389bbb1e54dSMiklos Szeredi /*
39005122443SAmir Goldstein  * Verify that @fh matches the file handle stored in xattr @name.
3918b88a2e6SAmir Goldstein  * Return 0 on match, -ESTALE on mismatch, < 0 on error.
3928b88a2e6SAmir Goldstein  */
39305122443SAmir Goldstein static int ovl_verify_fh(struct dentry *dentry, const char *name,
39405122443SAmir Goldstein 			 const struct ovl_fh *fh)
3958b88a2e6SAmir Goldstein {
39605122443SAmir Goldstein 	struct ovl_fh *ofh = ovl_get_fh(dentry, name);
3978b88a2e6SAmir Goldstein 	int err = 0;
3988b88a2e6SAmir Goldstein 
3998b88a2e6SAmir Goldstein 	if (!ofh)
4008b88a2e6SAmir Goldstein 		return -ENODATA;
4018b88a2e6SAmir Goldstein 
4028b88a2e6SAmir Goldstein 	if (IS_ERR(ofh))
4038b88a2e6SAmir Goldstein 		return PTR_ERR(ofh);
4048b88a2e6SAmir Goldstein 
4058b88a2e6SAmir Goldstein 	if (fh->len != ofh->len || memcmp(fh, ofh, fh->len))
4068b88a2e6SAmir Goldstein 		err = -ESTALE;
4078b88a2e6SAmir Goldstein 
4088b88a2e6SAmir Goldstein 	kfree(ofh);
4098b88a2e6SAmir Goldstein 	return err;
4108b88a2e6SAmir Goldstein }
4118b88a2e6SAmir Goldstein 
4128b88a2e6SAmir Goldstein /*
41305122443SAmir Goldstein  * Verify that @real dentry matches the file handle stored in xattr @name.
4148b88a2e6SAmir Goldstein  *
41505122443SAmir Goldstein  * If @set is true and there is no stored file handle, encode @real and store
41605122443SAmir Goldstein  * file handle in xattr @name.
4178b88a2e6SAmir Goldstein  *
41805122443SAmir Goldstein  * Return 0 on match, -ESTALE on mismatch, -ENODATA on no xattr, < 0 on error.
4198b88a2e6SAmir Goldstein  */
42005122443SAmir Goldstein int ovl_verify_set_fh(struct dentry *dentry, const char *name,
42105122443SAmir Goldstein 		      struct dentry *real, bool is_upper, bool set)
4228b88a2e6SAmir Goldstein {
4238b88a2e6SAmir Goldstein 	struct inode *inode;
4248b88a2e6SAmir Goldstein 	struct ovl_fh *fh;
4258b88a2e6SAmir Goldstein 	int err;
4268b88a2e6SAmir Goldstein 
427*5b2cccd3SAmir Goldstein 	fh = ovl_encode_real_fh(real, is_upper);
4288b88a2e6SAmir Goldstein 	err = PTR_ERR(fh);
4298b88a2e6SAmir Goldstein 	if (IS_ERR(fh))
4308b88a2e6SAmir Goldstein 		goto fail;
4318b88a2e6SAmir Goldstein 
43205122443SAmir Goldstein 	err = ovl_verify_fh(dentry, name, fh);
4338b88a2e6SAmir Goldstein 	if (set && err == -ENODATA)
43405122443SAmir Goldstein 		err = ovl_do_setxattr(dentry, name, fh, fh->len, 0);
4358b88a2e6SAmir Goldstein 	if (err)
4368b88a2e6SAmir Goldstein 		goto fail;
4378b88a2e6SAmir Goldstein 
4388b88a2e6SAmir Goldstein out:
4398b88a2e6SAmir Goldstein 	kfree(fh);
4408b88a2e6SAmir Goldstein 	return err;
4418b88a2e6SAmir Goldstein 
4428b88a2e6SAmir Goldstein fail:
44305122443SAmir Goldstein 	inode = d_inode(real);
44405122443SAmir Goldstein 	pr_warn_ratelimited("overlayfs: failed to verify %s (%pd2, ino=%lu, err=%i)\n",
44505122443SAmir Goldstein 			    is_upper ? "upper" : "origin", real,
44605122443SAmir Goldstein 			    inode ? inode->i_ino : 0, err);
4478b88a2e6SAmir Goldstein 	goto out;
4488b88a2e6SAmir Goldstein }
4498b88a2e6SAmir Goldstein 
450e8f9e5b7SAmir Goldstein /* Get upper dentry from index */
4513b0bfc6eSAmir Goldstein struct dentry *ovl_index_upper(struct ovl_fs *ofs, struct dentry *index)
452e8f9e5b7SAmir Goldstein {
453e8f9e5b7SAmir Goldstein 	struct ovl_fh *fh;
454e8f9e5b7SAmir Goldstein 	struct dentry *upper;
455e8f9e5b7SAmir Goldstein 
456e8f9e5b7SAmir Goldstein 	if (!d_is_dir(index))
457e8f9e5b7SAmir Goldstein 		return dget(index);
458e8f9e5b7SAmir Goldstein 
459e8f9e5b7SAmir Goldstein 	fh = ovl_get_fh(index, OVL_XATTR_UPPER);
460e8f9e5b7SAmir Goldstein 	if (IS_ERR_OR_NULL(fh))
461e8f9e5b7SAmir Goldstein 		return ERR_CAST(fh);
462e8f9e5b7SAmir Goldstein 
463*5b2cccd3SAmir Goldstein 	upper = ovl_decode_real_fh(fh, ofs->upper_mnt);
464e8f9e5b7SAmir Goldstein 	kfree(fh);
465e8f9e5b7SAmir Goldstein 
466e8f9e5b7SAmir Goldstein 	if (IS_ERR_OR_NULL(upper))
467e8f9e5b7SAmir Goldstein 		return upper ?: ERR_PTR(-ESTALE);
468e8f9e5b7SAmir Goldstein 
469e8f9e5b7SAmir Goldstein 	if (!d_is_dir(upper)) {
470e8f9e5b7SAmir Goldstein 		pr_warn_ratelimited("overlayfs: invalid index upper (%pd2, upper=%pd2).\n",
471e8f9e5b7SAmir Goldstein 				    index, upper);
472e8f9e5b7SAmir Goldstein 		dput(upper);
473e8f9e5b7SAmir Goldstein 		return ERR_PTR(-EIO);
474e8f9e5b7SAmir Goldstein 	}
475e8f9e5b7SAmir Goldstein 
476e8f9e5b7SAmir Goldstein 	return upper;
477e8f9e5b7SAmir Goldstein }
478e8f9e5b7SAmir Goldstein 
4799ee60ce2SAmir Goldstein /* Is this a leftover from create/whiteout of directory index entry? */
4809ee60ce2SAmir Goldstein static bool ovl_is_temp_index(struct dentry *index)
4819ee60ce2SAmir Goldstein {
4829ee60ce2SAmir Goldstein 	return index->d_name.name[0] == '#';
4839ee60ce2SAmir Goldstein }
4849ee60ce2SAmir Goldstein 
4858b88a2e6SAmir Goldstein /*
486415543d5SAmir Goldstein  * Verify that an index entry name matches the origin file handle stored in
487415543d5SAmir Goldstein  * OVL_XATTR_ORIGIN and that origin file handle can be decoded to lower path.
488415543d5SAmir Goldstein  * Return 0 on match, -ESTALE on mismatch or stale origin, < 0 on error.
489415543d5SAmir Goldstein  */
4901eff1a1dSAmir Goldstein int ovl_verify_index(struct ovl_fs *ofs, struct dentry *index)
491415543d5SAmir Goldstein {
492415543d5SAmir Goldstein 	struct ovl_fh *fh = NULL;
493415543d5SAmir Goldstein 	size_t len;
494b9343632SChandan Rajendra 	struct ovl_path origin = { };
495b9343632SChandan Rajendra 	struct ovl_path *stack = &origin;
496e8f9e5b7SAmir Goldstein 	struct dentry *upper = NULL;
497415543d5SAmir Goldstein 	int err;
498415543d5SAmir Goldstein 
499415543d5SAmir Goldstein 	if (!d_inode(index))
500415543d5SAmir Goldstein 		return 0;
501415543d5SAmir Goldstein 
5029ee60ce2SAmir Goldstein 	/* Cleanup leftover from index create/cleanup attempt */
5039ee60ce2SAmir Goldstein 	err = -ESTALE;
5049ee60ce2SAmir Goldstein 	if (ovl_is_temp_index(index))
5059ee60ce2SAmir Goldstein 		goto fail;
5069ee60ce2SAmir Goldstein 
507fa0096e3SAmir Goldstein 	err = -EINVAL;
508415543d5SAmir Goldstein 	if (index->d_name.len < sizeof(struct ovl_fh)*2)
509415543d5SAmir Goldstein 		goto fail;
510415543d5SAmir Goldstein 
511415543d5SAmir Goldstein 	err = -ENOMEM;
512415543d5SAmir Goldstein 	len = index->d_name.len / 2;
5130ee931c4SMichal Hocko 	fh = kzalloc(len, GFP_KERNEL);
514415543d5SAmir Goldstein 	if (!fh)
515415543d5SAmir Goldstein 		goto fail;
516415543d5SAmir Goldstein 
517415543d5SAmir Goldstein 	err = -EINVAL;
5182e1a5328SAmir Goldstein 	if (hex2bin((u8 *)fh, index->d_name.name, len))
5192e1a5328SAmir Goldstein 		goto fail;
5202e1a5328SAmir Goldstein 
5212e1a5328SAmir Goldstein 	err = ovl_check_fh_len(fh, len);
5222e1a5328SAmir Goldstein 	if (err)
523415543d5SAmir Goldstein 		goto fail;
524415543d5SAmir Goldstein 
5257db25d36SAmir Goldstein 	/*
5267db25d36SAmir Goldstein 	 * Whiteout index entries are used as an indication that an exported
5277db25d36SAmir Goldstein 	 * overlay file handle should be treated as stale (i.e. after unlink
5287db25d36SAmir Goldstein 	 * of the overlay inode). These entries contain no origin xattr.
5297db25d36SAmir Goldstein 	 */
5307db25d36SAmir Goldstein 	if (ovl_is_whiteout(index))
5317db25d36SAmir Goldstein 		goto out;
5327db25d36SAmir Goldstein 
533e8f9e5b7SAmir Goldstein 	/*
534e8f9e5b7SAmir Goldstein 	 * Verifying directory index entries are not stale is expensive, so
535e8f9e5b7SAmir Goldstein 	 * only verify stale dir index if NFS export is enabled.
536e8f9e5b7SAmir Goldstein 	 */
537e8f9e5b7SAmir Goldstein 	if (d_is_dir(index) && !ofs->config.nfs_export)
538e8f9e5b7SAmir Goldstein 		goto out;
539e8f9e5b7SAmir Goldstein 
540e8f9e5b7SAmir Goldstein 	/*
541e8f9e5b7SAmir Goldstein 	 * Directory index entries should have 'upper' xattr pointing to the
542e8f9e5b7SAmir Goldstein 	 * real upper dir. Non-dir index entries are hardlinks to the upper
543e8f9e5b7SAmir Goldstein 	 * real inode. For non-dir index, we can read the copy up origin xattr
544e8f9e5b7SAmir Goldstein 	 * directly from the index dentry, but for dir index we first need to
545e8f9e5b7SAmir Goldstein 	 * decode the upper directory.
546e8f9e5b7SAmir Goldstein 	 */
547e8f9e5b7SAmir Goldstein 	upper = ovl_index_upper(ofs, index);
548e8f9e5b7SAmir Goldstein 	if (IS_ERR_OR_NULL(upper)) {
549e8f9e5b7SAmir Goldstein 		err = PTR_ERR(upper);
55024f0b172SAmir Goldstein 		/*
55124f0b172SAmir Goldstein 		 * Directory index entries with no 'upper' xattr need to be
55224f0b172SAmir Goldstein 		 * removed. When dir index entry has a stale 'upper' xattr,
55324f0b172SAmir Goldstein 		 * we assume that upper dir was removed and we treat the dir
55424f0b172SAmir Goldstein 		 * index as orphan entry that needs to be whited out.
55524f0b172SAmir Goldstein 		 */
55624f0b172SAmir Goldstein 		if (err == -ESTALE)
55724f0b172SAmir Goldstein 			goto orphan;
55824f0b172SAmir Goldstein 		else if (!err)
559e8f9e5b7SAmir Goldstein 			err = -ESTALE;
560e8f9e5b7SAmir Goldstein 		goto fail;
561e8f9e5b7SAmir Goldstein 	}
562e8f9e5b7SAmir Goldstein 
563e8f9e5b7SAmir Goldstein 	err = ovl_verify_fh(upper, OVL_XATTR_ORIGIN, fh);
564e8f9e5b7SAmir Goldstein 	dput(upper);
565415543d5SAmir Goldstein 	if (err)
566415543d5SAmir Goldstein 		goto fail;
567415543d5SAmir Goldstein 
568e8f9e5b7SAmir Goldstein 	/* Check if non-dir index is orphan and don't warn before cleaning it */
569e8f9e5b7SAmir Goldstein 	if (!d_is_dir(index) && d_inode(index)->i_nlink == 1) {
5701eff1a1dSAmir Goldstein 		err = ovl_check_origin_fh(ofs, fh, index, &stack);
571415543d5SAmir Goldstein 		if (err)
572415543d5SAmir Goldstein 			goto fail;
573415543d5SAmir Goldstein 
574e8f9e5b7SAmir Goldstein 		if (ovl_get_nlink(origin.dentry, index, 0) == 0)
57524f0b172SAmir Goldstein 			goto orphan;
576e8f9e5b7SAmir Goldstein 	}
577caf70cb2SAmir Goldstein 
578415543d5SAmir Goldstein out:
579e8f9e5b7SAmir Goldstein 	dput(origin.dentry);
580415543d5SAmir Goldstein 	kfree(fh);
581415543d5SAmir Goldstein 	return err;
582415543d5SAmir Goldstein 
583415543d5SAmir Goldstein fail:
58461b67471SAmir Goldstein 	pr_warn_ratelimited("overlayfs: failed to verify index (%pd2, ftype=%x, err=%i)\n",
58561b67471SAmir Goldstein 			    index, d_inode(index)->i_mode & S_IFMT, err);
586415543d5SAmir Goldstein 	goto out;
58724f0b172SAmir Goldstein 
58824f0b172SAmir Goldstein orphan:
58924f0b172SAmir Goldstein 	pr_warn_ratelimited("overlayfs: orphan index entry (%pd2, ftype=%x, nlink=%u)\n",
59024f0b172SAmir Goldstein 			    index, d_inode(index)->i_mode & S_IFMT,
59124f0b172SAmir Goldstein 			    d_inode(index)->i_nlink);
59224f0b172SAmir Goldstein 	err = -ENOENT;
59324f0b172SAmir Goldstein 	goto out;
594415543d5SAmir Goldstein }
595415543d5SAmir Goldstein 
59691ffe7beSAmir Goldstein static int ovl_get_index_name_fh(struct ovl_fh *fh, struct qstr *name)
59791ffe7beSAmir Goldstein {
59891ffe7beSAmir Goldstein 	char *n, *s;
59991ffe7beSAmir Goldstein 
60091ffe7beSAmir Goldstein 	n = kzalloc(fh->len * 2, GFP_KERNEL);
60191ffe7beSAmir Goldstein 	if (!n)
60291ffe7beSAmir Goldstein 		return -ENOMEM;
60391ffe7beSAmir Goldstein 
60491ffe7beSAmir Goldstein 	s  = bin2hex(n, fh, fh->len);
60591ffe7beSAmir Goldstein 	*name = (struct qstr) QSTR_INIT(n, s - n);
60691ffe7beSAmir Goldstein 
60791ffe7beSAmir Goldstein 	return 0;
60891ffe7beSAmir Goldstein 
60991ffe7beSAmir Goldstein }
61091ffe7beSAmir Goldstein 
611415543d5SAmir Goldstein /*
612359f392cSAmir Goldstein  * Lookup in indexdir for the index entry of a lower real inode or a copy up
613359f392cSAmir Goldstein  * origin inode. The index entry name is the hex representation of the lower
614359f392cSAmir Goldstein  * inode file handle.
615359f392cSAmir Goldstein  *
616359f392cSAmir Goldstein  * If the index dentry in negative, then either no lower aliases have been
617359f392cSAmir Goldstein  * copied up yet, or aliases have been copied up in older kernels and are
618359f392cSAmir Goldstein  * not indexed.
619359f392cSAmir Goldstein  *
620359f392cSAmir Goldstein  * If the index dentry for a copy up origin inode is positive, but points
621359f392cSAmir Goldstein  * to an inode different than the upper inode, then either the upper inode
622359f392cSAmir Goldstein  * has been copied up and not indexed or it was indexed, but since then
623359f392cSAmir Goldstein  * index dir was cleared. Either way, that index cannot be used to indentify
624359f392cSAmir Goldstein  * the overlay inode.
625359f392cSAmir Goldstein  */
626359f392cSAmir Goldstein int ovl_get_index_name(struct dentry *origin, struct qstr *name)
627359f392cSAmir Goldstein {
628359f392cSAmir Goldstein 	struct ovl_fh *fh;
62991ffe7beSAmir Goldstein 	int err;
630359f392cSAmir Goldstein 
631*5b2cccd3SAmir Goldstein 	fh = ovl_encode_real_fh(origin, false);
632359f392cSAmir Goldstein 	if (IS_ERR(fh))
633359f392cSAmir Goldstein 		return PTR_ERR(fh);
634359f392cSAmir Goldstein 
63591ffe7beSAmir Goldstein 	err = ovl_get_index_name_fh(fh, name);
63691ffe7beSAmir Goldstein 
637359f392cSAmir Goldstein 	kfree(fh);
638359f392cSAmir Goldstein 	return err;
63991ffe7beSAmir Goldstein }
640359f392cSAmir Goldstein 
64191ffe7beSAmir Goldstein /* Lookup index by file handle for NFS export */
64291ffe7beSAmir Goldstein struct dentry *ovl_get_index_fh(struct ovl_fs *ofs, struct ovl_fh *fh)
64391ffe7beSAmir Goldstein {
64491ffe7beSAmir Goldstein 	struct dentry *index;
64591ffe7beSAmir Goldstein 	struct qstr name;
64691ffe7beSAmir Goldstein 	int err;
64791ffe7beSAmir Goldstein 
64891ffe7beSAmir Goldstein 	err = ovl_get_index_name_fh(fh, &name);
64991ffe7beSAmir Goldstein 	if (err)
65091ffe7beSAmir Goldstein 		return ERR_PTR(err);
65191ffe7beSAmir Goldstein 
65291ffe7beSAmir Goldstein 	index = lookup_one_len_unlocked(name.name, ofs->indexdir, name.len);
65391ffe7beSAmir Goldstein 	kfree(name.name);
65491ffe7beSAmir Goldstein 	if (IS_ERR(index)) {
65591ffe7beSAmir Goldstein 		if (PTR_ERR(index) == -ENOENT)
65691ffe7beSAmir Goldstein 			index = NULL;
65791ffe7beSAmir Goldstein 		return index;
65891ffe7beSAmir Goldstein 	}
65991ffe7beSAmir Goldstein 
66091ffe7beSAmir Goldstein 	if (d_is_negative(index))
66191ffe7beSAmir Goldstein 		err = 0;
66291ffe7beSAmir Goldstein 	else if (ovl_is_whiteout(index))
66391ffe7beSAmir Goldstein 		err = -ESTALE;
66491ffe7beSAmir Goldstein 	else if (ovl_dentry_weird(index))
66591ffe7beSAmir Goldstein 		err = -EIO;
66691ffe7beSAmir Goldstein 	else
66791ffe7beSAmir Goldstein 		return index;
66891ffe7beSAmir Goldstein 
66991ffe7beSAmir Goldstein 	dput(index);
67091ffe7beSAmir Goldstein 	return ERR_PTR(err);
671359f392cSAmir Goldstein }
672359f392cSAmir Goldstein 
67306170154SAmir Goldstein struct dentry *ovl_lookup_index(struct ovl_fs *ofs, struct dentry *upper,
67406170154SAmir Goldstein 				struct dentry *origin, bool verify)
675359f392cSAmir Goldstein {
676359f392cSAmir Goldstein 	struct dentry *index;
677359f392cSAmir Goldstein 	struct inode *inode;
678359f392cSAmir Goldstein 	struct qstr name;
679ad1d615cSAmir Goldstein 	bool is_dir = d_is_dir(origin);
680359f392cSAmir Goldstein 	int err;
681359f392cSAmir Goldstein 
682359f392cSAmir Goldstein 	err = ovl_get_index_name(origin, &name);
683359f392cSAmir Goldstein 	if (err)
684359f392cSAmir Goldstein 		return ERR_PTR(err);
685359f392cSAmir Goldstein 
686359f392cSAmir Goldstein 	index = lookup_one_len_unlocked(name.name, ofs->indexdir, name.len);
687359f392cSAmir Goldstein 	if (IS_ERR(index)) {
688e0082a0fSAmir Goldstein 		err = PTR_ERR(index);
6897937a56fSAmir Goldstein 		if (err == -ENOENT) {
6907937a56fSAmir Goldstein 			index = NULL;
6917937a56fSAmir Goldstein 			goto out;
6927937a56fSAmir Goldstein 		}
693359f392cSAmir Goldstein 		pr_warn_ratelimited("overlayfs: failed inode index lookup (ino=%lu, key=%*s, err=%i);\n"
694359f392cSAmir Goldstein 				    "overlayfs: mount with '-o index=off' to disable inodes index.\n",
695359f392cSAmir Goldstein 				    d_inode(origin)->i_ino, name.len, name.name,
696359f392cSAmir Goldstein 				    err);
697359f392cSAmir Goldstein 		goto out;
698359f392cSAmir Goldstein 	}
699359f392cSAmir Goldstein 
7000e082555SAmir Goldstein 	inode = d_inode(index);
701359f392cSAmir Goldstein 	if (d_is_negative(index)) {
7026eaf0111SAmir Goldstein 		goto out_dput;
70306170154SAmir Goldstein 	} else if (ovl_is_whiteout(index) && !verify) {
70406170154SAmir Goldstein 		/*
70506170154SAmir Goldstein 		 * When index lookup is called with !verify for decoding an
70606170154SAmir Goldstein 		 * overlay file handle, a whiteout index implies that decode
70706170154SAmir Goldstein 		 * should treat file handle as stale and no need to print a
70806170154SAmir Goldstein 		 * warning about it.
70906170154SAmir Goldstein 		 */
71006170154SAmir Goldstein 		dput(index);
71106170154SAmir Goldstein 		index = ERR_PTR(-ESTALE);
71206170154SAmir Goldstein 		goto out;
7130e082555SAmir Goldstein 	} else if (ovl_dentry_weird(index) || ovl_is_whiteout(index) ||
7140e082555SAmir Goldstein 		   ((inode->i_mode ^ d_inode(origin)->i_mode) & S_IFMT)) {
7150e082555SAmir Goldstein 		/*
7160e082555SAmir Goldstein 		 * Index should always be of the same file type as origin
7170e082555SAmir Goldstein 		 * except for the case of a whiteout index. A whiteout
7180e082555SAmir Goldstein 		 * index should only exist if all lower aliases have been
7190e082555SAmir Goldstein 		 * unlinked, which means that finding a lower origin on lookup
7200e082555SAmir Goldstein 		 * whose index is a whiteout should be treated as an error.
7210e082555SAmir Goldstein 		 */
7220e082555SAmir Goldstein 		pr_warn_ratelimited("overlayfs: bad index found (index=%pd2, ftype=%x, origin ftype=%x).\n",
7230e082555SAmir Goldstein 				    index, d_inode(index)->i_mode & S_IFMT,
7240e082555SAmir Goldstein 				    d_inode(origin)->i_mode & S_IFMT);
725359f392cSAmir Goldstein 		goto fail;
72606170154SAmir Goldstein 	} else if (is_dir && verify) {
727ad1d615cSAmir Goldstein 		if (!upper) {
728ad1d615cSAmir Goldstein 			pr_warn_ratelimited("overlayfs: suspected uncovered redirected dir found (origin=%pd2, index=%pd2).\n",
729ad1d615cSAmir Goldstein 					    origin, index);
730ad1d615cSAmir Goldstein 			goto fail;
731359f392cSAmir Goldstein 		}
732359f392cSAmir Goldstein 
733ad1d615cSAmir Goldstein 		/* Verify that dir index 'upper' xattr points to upper dir */
734ad1d615cSAmir Goldstein 		err = ovl_verify_upper(index, upper, false);
735ad1d615cSAmir Goldstein 		if (err) {
736ad1d615cSAmir Goldstein 			if (err == -ESTALE) {
737ad1d615cSAmir Goldstein 				pr_warn_ratelimited("overlayfs: suspected multiply redirected dir found (upper=%pd2, origin=%pd2, index=%pd2).\n",
738ad1d615cSAmir Goldstein 						    upper, origin, index);
739ad1d615cSAmir Goldstein 			}
740ad1d615cSAmir Goldstein 			goto fail;
741ad1d615cSAmir Goldstein 		}
742ad1d615cSAmir Goldstein 	} else if (upper && d_inode(upper) != inode) {
743ad1d615cSAmir Goldstein 		goto out_dput;
744ad1d615cSAmir Goldstein 	}
745359f392cSAmir Goldstein out:
746359f392cSAmir Goldstein 	kfree(name.name);
747359f392cSAmir Goldstein 	return index;
748359f392cSAmir Goldstein 
7496eaf0111SAmir Goldstein out_dput:
7506eaf0111SAmir Goldstein 	dput(index);
7516eaf0111SAmir Goldstein 	index = NULL;
7526eaf0111SAmir Goldstein 	goto out;
7536eaf0111SAmir Goldstein 
754359f392cSAmir Goldstein fail:
755359f392cSAmir Goldstein 	dput(index);
756359f392cSAmir Goldstein 	index = ERR_PTR(-EIO);
757359f392cSAmir Goldstein 	goto out;
758359f392cSAmir Goldstein }
759359f392cSAmir Goldstein 
760359f392cSAmir Goldstein /*
761bbb1e54dSMiklos Szeredi  * Returns next layer in stack starting from top.
762bbb1e54dSMiklos Szeredi  * Returns -1 if this is the last layer.
763bbb1e54dSMiklos Szeredi  */
764bbb1e54dSMiklos Szeredi int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
765bbb1e54dSMiklos Szeredi {
766bbb1e54dSMiklos Szeredi 	struct ovl_entry *oe = dentry->d_fsdata;
767bbb1e54dSMiklos Szeredi 
768bbb1e54dSMiklos Szeredi 	BUG_ON(idx < 0);
769bbb1e54dSMiklos Szeredi 	if (idx == 0) {
770bbb1e54dSMiklos Szeredi 		ovl_path_upper(dentry, path);
771bbb1e54dSMiklos Szeredi 		if (path->dentry)
772bbb1e54dSMiklos Szeredi 			return oe->numlower ? 1 : -1;
773bbb1e54dSMiklos Szeredi 		idx++;
774bbb1e54dSMiklos Szeredi 	}
775bbb1e54dSMiklos Szeredi 	BUG_ON(idx > oe->numlower);
776b9343632SChandan Rajendra 	path->dentry = oe->lowerstack[idx - 1].dentry;
777b9343632SChandan Rajendra 	path->mnt = oe->lowerstack[idx - 1].layer->mnt;
778bbb1e54dSMiklos Szeredi 
779bbb1e54dSMiklos Szeredi 	return (idx < oe->numlower) ? idx + 1 : -1;
780bbb1e54dSMiklos Szeredi }
781bbb1e54dSMiklos Szeredi 
7829678e630SAmir Goldstein /* Fix missing 'origin' xattr */
7839678e630SAmir Goldstein static int ovl_fix_origin(struct dentry *dentry, struct dentry *lower,
7849678e630SAmir Goldstein 			  struct dentry *upper)
7859678e630SAmir Goldstein {
7869678e630SAmir Goldstein 	int err;
7879678e630SAmir Goldstein 
7889678e630SAmir Goldstein 	if (ovl_check_origin_xattr(upper))
7899678e630SAmir Goldstein 		return 0;
7909678e630SAmir Goldstein 
7919678e630SAmir Goldstein 	err = ovl_want_write(dentry);
7929678e630SAmir Goldstein 	if (err)
7939678e630SAmir Goldstein 		return err;
7949678e630SAmir Goldstein 
7959678e630SAmir Goldstein 	err = ovl_set_origin(dentry, lower, upper);
7969678e630SAmir Goldstein 	if (!err)
7979678e630SAmir Goldstein 		err = ovl_set_impure(dentry->d_parent, upper->d_parent);
7989678e630SAmir Goldstein 
7999678e630SAmir Goldstein 	ovl_drop_write(dentry);
8009678e630SAmir Goldstein 	return err;
8019678e630SAmir Goldstein }
8029678e630SAmir Goldstein 
803bbb1e54dSMiklos Szeredi struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
804bbb1e54dSMiklos Szeredi 			  unsigned int flags)
805bbb1e54dSMiklos Szeredi {
806bbb1e54dSMiklos Szeredi 	struct ovl_entry *oe;
807bbb1e54dSMiklos Szeredi 	const struct cred *old_cred;
8086b2d5fe4SMiklos Szeredi 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
809bbb1e54dSMiklos Szeredi 	struct ovl_entry *poe = dentry->d_parent->d_fsdata;
810c22205d0SAmir Goldstein 	struct ovl_entry *roe = dentry->d_sb->s_root->d_fsdata;
811b9343632SChandan Rajendra 	struct ovl_path *stack = NULL;
812bbb1e54dSMiklos Szeredi 	struct dentry *upperdir, *upperdentry = NULL;
813ad1d615cSAmir Goldstein 	struct dentry *origin = NULL;
814359f392cSAmir Goldstein 	struct dentry *index = NULL;
815bbb1e54dSMiklos Szeredi 	unsigned int ctr = 0;
816bbb1e54dSMiklos Szeredi 	struct inode *inode = NULL;
817bbb1e54dSMiklos Szeredi 	bool upperopaque = false;
81802b69b28SMiklos Szeredi 	char *upperredirect = NULL;
819bbb1e54dSMiklos Szeredi 	struct dentry *this;
820bbb1e54dSMiklos Szeredi 	unsigned int i;
821bbb1e54dSMiklos Szeredi 	int err;
822e28edc46SMiklos Szeredi 	struct ovl_lookup_data d = {
823e28edc46SMiklos Szeredi 		.name = dentry->d_name,
824e28edc46SMiklos Szeredi 		.is_dir = false,
825e28edc46SMiklos Szeredi 		.opaque = false,
826e28edc46SMiklos Szeredi 		.stop = false,
827452061fdSVivek Goyal 		.last = ofs->config.redirect_follow ? false : !poe->numlower,
82802b69b28SMiklos Szeredi 		.redirect = NULL,
829e28edc46SMiklos Szeredi 	};
830bbb1e54dSMiklos Szeredi 
8316b2d5fe4SMiklos Szeredi 	if (dentry->d_name.len > ofs->namelen)
8326b2d5fe4SMiklos Szeredi 		return ERR_PTR(-ENAMETOOLONG);
8336b2d5fe4SMiklos Szeredi 
834bbb1e54dSMiklos Szeredi 	old_cred = ovl_override_creds(dentry->d_sb);
83509d8b586SMiklos Szeredi 	upperdir = ovl_dentry_upper(dentry->d_parent);
836bbb1e54dSMiklos Szeredi 	if (upperdir) {
837e28edc46SMiklos Szeredi 		err = ovl_lookup_layer(upperdir, &d, &upperdentry);
838e28edc46SMiklos Szeredi 		if (err)
839bbb1e54dSMiklos Szeredi 			goto out;
840bbb1e54dSMiklos Szeredi 
841e28edc46SMiklos Szeredi 		if (upperdentry && unlikely(ovl_dentry_remote(upperdentry))) {
842e28edc46SMiklos Szeredi 			dput(upperdentry);
843bbb1e54dSMiklos Szeredi 			err = -EREMOTE;
844bbb1e54dSMiklos Szeredi 			goto out;
845bbb1e54dSMiklos Szeredi 		}
846a9d01957SAmir Goldstein 		if (upperdentry && !d.is_dir) {
847a9d01957SAmir Goldstein 			BUG_ON(!d.stop || d.redirect);
848f7d3dacaSAmir Goldstein 			/*
849f7d3dacaSAmir Goldstein 			 * Lookup copy up origin by decoding origin file handle.
850f7d3dacaSAmir Goldstein 			 * We may get a disconnected dentry, which is fine,
851f7d3dacaSAmir Goldstein 			 * because we only need to hold the origin inode in
852f7d3dacaSAmir Goldstein 			 * cache and use its inode number.  We may even get a
853f7d3dacaSAmir Goldstein 			 * connected dentry, that is not under any of the lower
854f7d3dacaSAmir Goldstein 			 * layers root.  That is also fine for using it's inode
855f7d3dacaSAmir Goldstein 			 * number - it's the same as if we held a reference
856f7d3dacaSAmir Goldstein 			 * to a dentry in lower layer that was moved under us.
857f7d3dacaSAmir Goldstein 			 */
8581eff1a1dSAmir Goldstein 			err = ovl_check_origin(ofs, upperdentry, &stack, &ctr);
859a9d01957SAmir Goldstein 			if (err)
8605455f92bSVivek Goyal 				goto out_put_upper;
861a9d01957SAmir Goldstein 		}
86202b69b28SMiklos Szeredi 
86302b69b28SMiklos Szeredi 		if (d.redirect) {
8640ce5cdc9SDan Carpenter 			err = -ENOMEM;
86502b69b28SMiklos Szeredi 			upperredirect = kstrdup(d.redirect, GFP_KERNEL);
86602b69b28SMiklos Szeredi 			if (!upperredirect)
86702b69b28SMiklos Szeredi 				goto out_put_upper;
86802b69b28SMiklos Szeredi 			if (d.redirect[0] == '/')
869c22205d0SAmir Goldstein 				poe = roe;
87002b69b28SMiklos Szeredi 		}
871e28edc46SMiklos Szeredi 		upperopaque = d.opaque;
872bbb1e54dSMiklos Szeredi 	}
873bbb1e54dSMiklos Szeredi 
874e28edc46SMiklos Szeredi 	if (!d.stop && poe->numlower) {
875bbb1e54dSMiklos Szeredi 		err = -ENOMEM;
876b9343632SChandan Rajendra 		stack = kcalloc(ofs->numlower, sizeof(struct ovl_path),
8770ee931c4SMichal Hocko 				GFP_KERNEL);
878bbb1e54dSMiklos Szeredi 		if (!stack)
879bbb1e54dSMiklos Szeredi 			goto out_put_upper;
880bbb1e54dSMiklos Szeredi 	}
881bbb1e54dSMiklos Szeredi 
882e28edc46SMiklos Szeredi 	for (i = 0; !d.stop && i < poe->numlower; i++) {
883b9343632SChandan Rajendra 		struct ovl_path lower = poe->lowerstack[i];
884bbb1e54dSMiklos Szeredi 
885452061fdSVivek Goyal 		if (!ofs->config.redirect_follow)
886e28edc46SMiklos Szeredi 			d.last = i == poe->numlower - 1;
887452061fdSVivek Goyal 		else
888452061fdSVivek Goyal 			d.last = lower.layer->idx == roe->numlower;
889452061fdSVivek Goyal 
890b9343632SChandan Rajendra 		err = ovl_lookup_layer(lower.dentry, &d, &this);
891e28edc46SMiklos Szeredi 		if (err)
892bbb1e54dSMiklos Szeredi 			goto out_put;
8936b2d5fe4SMiklos Szeredi 
894bbb1e54dSMiklos Szeredi 		if (!this)
895bbb1e54dSMiklos Szeredi 			continue;
896bbb1e54dSMiklos Szeredi 
8979678e630SAmir Goldstein 		/*
8989678e630SAmir Goldstein 		 * If no origin fh is stored in upper of a merge dir, store fh
8999678e630SAmir Goldstein 		 * of lower dir and set upper parent "impure".
9009678e630SAmir Goldstein 		 */
9019678e630SAmir Goldstein 		if (upperdentry && !ctr && !ofs->noxattr) {
9029678e630SAmir Goldstein 			err = ovl_fix_origin(dentry, this, upperdentry);
9039678e630SAmir Goldstein 			if (err) {
9049678e630SAmir Goldstein 				dput(this);
9059678e630SAmir Goldstein 				goto out_put;
9069678e630SAmir Goldstein 			}
9079678e630SAmir Goldstein 		}
9089678e630SAmir Goldstein 
90937b12916SAmir Goldstein 		/*
91037b12916SAmir Goldstein 		 * When "verify_lower" feature is enabled, do not merge with a
911ad1d615cSAmir Goldstein 		 * lower dir that does not match a stored origin xattr. In any
912ad1d615cSAmir Goldstein 		 * case, only verified origin is used for index lookup.
91337b12916SAmir Goldstein 		 */
91437b12916SAmir Goldstein 		if (upperdentry && !ctr && ovl_verify_lower(dentry->d_sb)) {
91537b12916SAmir Goldstein 			err = ovl_verify_origin(upperdentry, this, false);
91637b12916SAmir Goldstein 			if (err) {
91737b12916SAmir Goldstein 				dput(this);
91837b12916SAmir Goldstein 				break;
91937b12916SAmir Goldstein 			}
920ad1d615cSAmir Goldstein 
921ad1d615cSAmir Goldstein 			/* Bless lower dir as verified origin */
922ad1d615cSAmir Goldstein 			origin = this;
92337b12916SAmir Goldstein 		}
92437b12916SAmir Goldstein 
925bbb1e54dSMiklos Szeredi 		stack[ctr].dentry = this;
926b9343632SChandan Rajendra 		stack[ctr].layer = lower.layer;
927bbb1e54dSMiklos Szeredi 		ctr++;
92802b69b28SMiklos Szeredi 
929438c84c2SMiklos Szeredi 		/*
930438c84c2SMiklos Szeredi 		 * Following redirects can have security consequences: it's like
931438c84c2SMiklos Szeredi 		 * a symlink into the lower layer without the permission checks.
932438c84c2SMiklos Szeredi 		 * This is only a problem if the upper layer is untrusted (e.g
933438c84c2SMiklos Szeredi 		 * comes from an USB drive).  This can allow a non-readable file
934438c84c2SMiklos Szeredi 		 * or directory to become readable.
935438c84c2SMiklos Szeredi 		 *
936438c84c2SMiklos Szeredi 		 * Only following redirects when redirects are enabled disables
937438c84c2SMiklos Szeredi 		 * this attack vector when not necessary.
938438c84c2SMiklos Szeredi 		 */
939438c84c2SMiklos Szeredi 		err = -EPERM;
940438c84c2SMiklos Szeredi 		if (d.redirect && !ofs->config.redirect_follow) {
941f8167817SAmir Goldstein 			pr_warn_ratelimited("overlayfs: refusing to follow redirect for (%pd2)\n",
942f8167817SAmir Goldstein 					    dentry);
943438c84c2SMiklos Szeredi 			goto out_put;
944438c84c2SMiklos Szeredi 		}
945438c84c2SMiklos Szeredi 
946d1fe96c0SVivek Goyal 		if (d.stop)
947d1fe96c0SVivek Goyal 			break;
948d1fe96c0SVivek Goyal 
949c22205d0SAmir Goldstein 		if (d.redirect && d.redirect[0] == '/' && poe != roe) {
950c22205d0SAmir Goldstein 			poe = roe;
95102b69b28SMiklos Szeredi 			/* Find the current layer on the root dentry */
952d583ed7dSAmir Goldstein 			i = lower.layer->idx - 1;
95302b69b28SMiklos Szeredi 		}
954bbb1e54dSMiklos Szeredi 	}
955bbb1e54dSMiklos Szeredi 
956ad1d615cSAmir Goldstein 	/*
957ad1d615cSAmir Goldstein 	 * Lookup index by lower inode and verify it matches upper inode.
958ad1d615cSAmir Goldstein 	 * We only trust dir index if we verified that lower dir matches
959ad1d615cSAmir Goldstein 	 * origin, otherwise dir index entries may be inconsistent and we
960ad1d615cSAmir Goldstein 	 * ignore them. Always lookup index of non-dir and non-upper.
961ad1d615cSAmir Goldstein 	 */
962ad1d615cSAmir Goldstein 	if (ctr && (!upperdentry || !d.is_dir))
963ad1d615cSAmir Goldstein 		origin = stack[0].dentry;
964359f392cSAmir Goldstein 
965ad1d615cSAmir Goldstein 	if (origin && ovl_indexdir(dentry->d_sb) &&
966ad1d615cSAmir Goldstein 	    (!d.is_dir || ovl_index_all(dentry->d_sb))) {
96706170154SAmir Goldstein 		index = ovl_lookup_index(ofs, upperdentry, origin, true);
968359f392cSAmir Goldstein 		if (IS_ERR(index)) {
969359f392cSAmir Goldstein 			err = PTR_ERR(index);
970359f392cSAmir Goldstein 			index = NULL;
971359f392cSAmir Goldstein 			goto out_put;
972359f392cSAmir Goldstein 		}
973359f392cSAmir Goldstein 	}
974359f392cSAmir Goldstein 
975bbb1e54dSMiklos Szeredi 	oe = ovl_alloc_entry(ctr);
976bbb1e54dSMiklos Szeredi 	err = -ENOMEM;
977bbb1e54dSMiklos Szeredi 	if (!oe)
978bbb1e54dSMiklos Szeredi 		goto out_put;
979bbb1e54dSMiklos Szeredi 
980b9343632SChandan Rajendra 	memcpy(oe->lowerstack, stack, sizeof(struct ovl_path) * ctr);
981e6d2ebddSMiklos Szeredi 	dentry->d_fsdata = oe;
982e6d2ebddSMiklos Szeredi 
983c62520a8SAmir Goldstein 	if (upperopaque)
984c62520a8SAmir Goldstein 		ovl_dentry_set_opaque(dentry);
985c62520a8SAmir Goldstein 
98655acc661SMiklos Szeredi 	if (upperdentry)
98755acc661SMiklos Szeredi 		ovl_dentry_set_upper_alias(dentry);
98855acc661SMiklos Szeredi 	else if (index)
989359f392cSAmir Goldstein 		upperdentry = dget(index);
990359f392cSAmir Goldstein 
991e6d2ebddSMiklos Szeredi 	if (upperdentry || ctr) {
9922aed489dSAmir Goldstein 		if (ctr)
9932aed489dSAmir Goldstein 			origin = stack[0].dentry;
9940aceb53eSAmir Goldstein 		inode = ovl_get_inode(dentry->d_sb, upperdentry, origin, index,
9950aceb53eSAmir Goldstein 				      ctr);
996b9ac5c27SMiklos Szeredi 		err = PTR_ERR(inode);
997b9ac5c27SMiklos Szeredi 		if (IS_ERR(inode))
998e6d2ebddSMiklos Szeredi 			goto out_free_oe;
999cf31c463SMiklos Szeredi 
1000cf31c463SMiklos Szeredi 		OVL_I(inode)->redirect = upperredirect;
1001359f392cSAmir Goldstein 		if (index)
1002359f392cSAmir Goldstein 			ovl_set_flag(OVL_INDEX, inode);
1003e6d2ebddSMiklos Szeredi 	}
1004e6d2ebddSMiklos Szeredi 
1005e6d2ebddSMiklos Szeredi 	revert_creds(old_cred);
1006359f392cSAmir Goldstein 	dput(index);
1007bbb1e54dSMiklos Szeredi 	kfree(stack);
100802b69b28SMiklos Szeredi 	kfree(d.redirect);
1009829c28beSAmir Goldstein 	return d_splice_alias(inode, dentry);
1010bbb1e54dSMiklos Szeredi 
1011bbb1e54dSMiklos Szeredi out_free_oe:
1012e6d2ebddSMiklos Szeredi 	dentry->d_fsdata = NULL;
1013bbb1e54dSMiklos Szeredi 	kfree(oe);
1014bbb1e54dSMiklos Szeredi out_put:
1015359f392cSAmir Goldstein 	dput(index);
1016bbb1e54dSMiklos Szeredi 	for (i = 0; i < ctr; i++)
1017bbb1e54dSMiklos Szeredi 		dput(stack[i].dentry);
1018bbb1e54dSMiklos Szeredi 	kfree(stack);
1019bbb1e54dSMiklos Szeredi out_put_upper:
1020bbb1e54dSMiklos Szeredi 	dput(upperdentry);
102102b69b28SMiklos Szeredi 	kfree(upperredirect);
1022bbb1e54dSMiklos Szeredi out:
102302b69b28SMiklos Szeredi 	kfree(d.redirect);
1024bbb1e54dSMiklos Szeredi 	revert_creds(old_cred);
1025bbb1e54dSMiklos Szeredi 	return ERR_PTR(err);
1026bbb1e54dSMiklos Szeredi }
1027bbb1e54dSMiklos Szeredi 
1028bbb1e54dSMiklos Szeredi bool ovl_lower_positive(struct dentry *dentry)
1029bbb1e54dSMiklos Szeredi {
1030bbb1e54dSMiklos Szeredi 	struct ovl_entry *poe = dentry->d_parent->d_fsdata;
1031bbb1e54dSMiklos Szeredi 	const struct qstr *name = &dentry->d_name;
10326d0a8a90SAmir Goldstein 	const struct cred *old_cred;
1033bbb1e54dSMiklos Szeredi 	unsigned int i;
1034bbb1e54dSMiklos Szeredi 	bool positive = false;
1035bbb1e54dSMiklos Szeredi 	bool done = false;
1036bbb1e54dSMiklos Szeredi 
1037bbb1e54dSMiklos Szeredi 	/*
1038bbb1e54dSMiklos Szeredi 	 * If dentry is negative, then lower is positive iff this is a
1039bbb1e54dSMiklos Szeredi 	 * whiteout.
1040bbb1e54dSMiklos Szeredi 	 */
1041bbb1e54dSMiklos Szeredi 	if (!dentry->d_inode)
1042c62520a8SAmir Goldstein 		return ovl_dentry_is_opaque(dentry);
1043bbb1e54dSMiklos Szeredi 
1044bbb1e54dSMiklos Szeredi 	/* Negative upper -> positive lower */
104509d8b586SMiklos Szeredi 	if (!ovl_dentry_upper(dentry))
1046bbb1e54dSMiklos Szeredi 		return true;
1047bbb1e54dSMiklos Szeredi 
10486d0a8a90SAmir Goldstein 	old_cred = ovl_override_creds(dentry->d_sb);
1049bbb1e54dSMiklos Szeredi 	/* Positive upper -> have to look up lower to see whether it exists */
1050bbb1e54dSMiklos Szeredi 	for (i = 0; !done && !positive && i < poe->numlower; i++) {
1051bbb1e54dSMiklos Szeredi 		struct dentry *this;
1052bbb1e54dSMiklos Szeredi 		struct dentry *lowerdir = poe->lowerstack[i].dentry;
1053bbb1e54dSMiklos Szeredi 
1054bbb1e54dSMiklos Szeredi 		this = lookup_one_len_unlocked(name->name, lowerdir,
1055bbb1e54dSMiklos Szeredi 					       name->len);
1056bbb1e54dSMiklos Szeredi 		if (IS_ERR(this)) {
1057bbb1e54dSMiklos Szeredi 			switch (PTR_ERR(this)) {
1058bbb1e54dSMiklos Szeredi 			case -ENOENT:
1059bbb1e54dSMiklos Szeredi 			case -ENAMETOOLONG:
1060bbb1e54dSMiklos Szeredi 				break;
1061bbb1e54dSMiklos Szeredi 
1062bbb1e54dSMiklos Szeredi 			default:
1063bbb1e54dSMiklos Szeredi 				/*
1064bbb1e54dSMiklos Szeredi 				 * Assume something is there, we just couldn't
1065bbb1e54dSMiklos Szeredi 				 * access it.
1066bbb1e54dSMiklos Szeredi 				 */
1067bbb1e54dSMiklos Szeredi 				positive = true;
1068bbb1e54dSMiklos Szeredi 				break;
1069bbb1e54dSMiklos Szeredi 			}
1070bbb1e54dSMiklos Szeredi 		} else {
1071bbb1e54dSMiklos Szeredi 			if (this->d_inode) {
1072bbb1e54dSMiklos Szeredi 				positive = !ovl_is_whiteout(this);
1073bbb1e54dSMiklos Szeredi 				done = true;
1074bbb1e54dSMiklos Szeredi 			}
1075bbb1e54dSMiklos Szeredi 			dput(this);
1076bbb1e54dSMiklos Szeredi 		}
1077bbb1e54dSMiklos Szeredi 	}
10786d0a8a90SAmir Goldstein 	revert_creds(old_cred);
1079bbb1e54dSMiklos Szeredi 
1080bbb1e54dSMiklos Szeredi 	return positive;
1081bbb1e54dSMiklos Szeredi }
1082