blob: e9717c2f7d455902415d2c1c88f9ab15e872085c [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Miklos Szeredibbb1e542016-12-16 11:02:56 +01002/*
3 * Copyright (C) 2011 Novell Inc.
4 * Copyright (C) 2016 Red Hat, Inc.
Miklos Szeredibbb1e542016-12-16 11:02:56 +01005 */
6
7#include <linux/fs.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +01008#include <linux/cred.h>
Amir Goldstein9ee60ce2017-11-01 10:13:51 +02009#include <linux/ctype.h>
Miklos Szeredibbb1e542016-12-16 11:02:56 +010010#include <linux/namei.h>
11#include <linux/xattr.h>
Miklos Szeredi02b69b22016-12-16 11:02:56 +010012#include <linux/ratelimit.h>
Amir Goldsteina9d01952017-04-30 14:46:31 +030013#include <linux/mount.h>
14#include <linux/exportfs.h>
Miklos Szeredibbb1e542016-12-16 11:02:56 +010015#include "overlayfs.h"
Miklos Szeredibbb1e542016-12-16 11:02:56 +010016
Miklos Szeredie28edc42016-12-16 11:02:56 +010017struct ovl_lookup_data {
Amir Goldstein146d62e2019-04-18 17:42:08 +030018 struct super_block *sb;
Miklos Szeredie28edc42016-12-16 11:02:56 +010019 struct qstr name;
20 bool is_dir;
21 bool opaque;
22 bool stop;
23 bool last;
Miklos Szeredi02b69b22016-12-16 11:02:56 +010024 char *redirect;
Vivek Goyal9d3dfea2018-05-11 11:49:28 -040025 bool metacopy;
Miklos Szeredie28edc42016-12-16 11:02:56 +010026};
Miklos Szeredibbb1e542016-12-16 11:02:56 +010027
Miklos Szeredi02b69b22016-12-16 11:02:56 +010028static int ovl_check_redirect(struct dentry *dentry, struct ovl_lookup_data *d,
29 size_t prelen, const char *post)
30{
31 int res;
Vivek Goyal0a2d0d32018-05-11 11:49:32 -040032 char *buf;
Miklos Szeredi02b69b22016-12-16 11:02:56 +010033
Vivek Goyal0a2d0d32018-05-11 11:49:32 -040034 buf = ovl_get_redirect_xattr(dentry, prelen + strlen(post));
35 if (IS_ERR_OR_NULL(buf))
36 return PTR_ERR(buf);
Miklos Szeredi02b69b22016-12-16 11:02:56 +010037
Miklos Szeredi02b69b22016-12-16 11:02:56 +010038 if (buf[0] == '/') {
Amir Goldstein3ec9b3f2018-03-12 10:30:41 -040039 /*
40 * One of the ancestor path elements in an absolute path
41 * lookup in ovl_lookup_layer() could have been opaque and
42 * that will stop further lookup in lower layers (d->stop=true)
43 * But we have found an absolute redirect in decendant path
44 * element and that should force continue lookup in lower
45 * layers (reset d->stop).
46 */
47 d->stop = false;
Miklos Szeredi02b69b22016-12-16 11:02:56 +010048 } else {
Vivek Goyal0a2d0d32018-05-11 11:49:32 -040049 res = strlen(buf) + 1;
Miklos Szeredi02b69b22016-12-16 11:02:56 +010050 memmove(buf + prelen, buf, res);
51 memcpy(buf, d->name.name, prelen);
52 }
53
54 strcat(buf, post);
55 kfree(d->redirect);
56 d->redirect = buf;
57 d->name.name = d->redirect;
58 d->name.len = strlen(d->redirect);
59
60 return 0;
Miklos Szeredi02b69b22016-12-16 11:02:56 +010061}
62
Amir Goldsteina9d01952017-04-30 14:46:31 +030063static int ovl_acceptable(void *ctx, struct dentry *dentry)
64{
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +020065 /*
66 * A non-dir origin may be disconnected, which is fine, because
67 * we only need it for its unique inode number.
68 */
69 if (!d_is_dir(dentry))
70 return 1;
71
72 /* Don't decode a deleted empty directory */
73 if (d_unhashed(dentry))
74 return 0;
75
76 /* Check if directory belongs to the layer we are decoding from */
77 return is_subdir(dentry, ((struct vfsmount *)ctx)->mnt_root);
Amir Goldsteina9d01952017-04-30 14:46:31 +030078}
79
Amir Goldstein2e1a53282017-10-24 15:12:15 +030080/*
81 * Check validity of an overlay file handle buffer.
82 *
83 * Return 0 for a valid file handle.
84 * Return -ENODATA for "origin unknown".
85 * Return <0 for an invalid file handle.
86 */
Amir Goldstein8556a422018-01-19 01:03:23 +020087int ovl_check_fh_len(struct ovl_fh *fh, int fh_len)
Amir Goldstein2e1a53282017-10-24 15:12:15 +030088{
89 if (fh_len < sizeof(struct ovl_fh) || fh_len < fh->len)
90 return -EINVAL;
91
92 if (fh->magic != OVL_FH_MAGIC)
93 return -EINVAL;
94
95 /* Treat larger version and unknown flags as "origin unknown" */
96 if (fh->version > OVL_FH_VERSION || fh->flags & ~OVL_FH_FLAG_ALL)
97 return -ENODATA;
98
99 /* Treat endianness mismatch as "origin unknown" */
100 if (!(fh->flags & OVL_FH_FLAG_ANY_ENDIAN) &&
101 (fh->flags & OVL_FH_FLAG_BIG_ENDIAN) != OVL_FH_FLAG_CPU_ENDIAN)
102 return -ENODATA;
103
104 return 0;
105}
106
Amir Goldstein05122442018-01-11 08:25:32 +0200107static struct ovl_fh *ovl_get_fh(struct dentry *dentry, const char *name)
Amir Goldsteina9d01952017-04-30 14:46:31 +0300108{
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300109 int res, err;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300110 struct ovl_fh *fh = NULL;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300111
Amir Goldstein05122442018-01-11 08:25:32 +0200112 res = vfs_getxattr(dentry, name, NULL, 0);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300113 if (res < 0) {
114 if (res == -ENODATA || res == -EOPNOTSUPP)
115 return NULL;
116 goto fail;
117 }
118 /* Zero size value means "copied up but origin unknown" */
119 if (res == 0)
120 return NULL;
121
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300122 fh = kzalloc(res, GFP_KERNEL);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300123 if (!fh)
124 return ERR_PTR(-ENOMEM);
125
Amir Goldstein05122442018-01-11 08:25:32 +0200126 res = vfs_getxattr(dentry, name, fh, res);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300127 if (res < 0)
128 goto fail;
129
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300130 err = ovl_check_fh_len(fh, res);
131 if (err < 0) {
132 if (err == -ENODATA)
133 goto out;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300134 goto invalid;
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300135 }
Amir Goldsteina9d01952017-04-30 14:46:31 +0300136
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300137 return fh;
138
139out:
140 kfree(fh);
141 return NULL;
142
143fail:
144 pr_warn_ratelimited("overlayfs: failed to get origin (%i)\n", res);
145 goto out;
146invalid:
147 pr_warn_ratelimited("overlayfs: invalid origin (%*phN)\n", res, fh);
148 goto out;
149}
150
Amir Goldstein8a22efa2018-03-09 15:51:02 +0200151struct dentry *ovl_decode_real_fh(struct ovl_fh *fh, struct vfsmount *mnt,
152 bool connected)
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300153{
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200154 struct dentry *real;
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300155 int bytes;
156
Amir Goldsteina9d01952017-04-30 14:46:31 +0300157 /*
158 * Make sure that the stored uuid matches the uuid of the lower
159 * layer where file handle will be decoded.
160 */
Christoph Hellwig85787092017-05-10 15:06:33 +0200161 if (!uuid_equal(&fh->uuid, &mnt->mnt_sb->s_uuid))
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300162 return NULL;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300163
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300164 bytes = (fh->len - offsetof(struct ovl_fh, fid));
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200165 real = exportfs_decode_fh(mnt, (struct fid *)fh->fid,
166 bytes >> 2, (int)fh->type,
Amir Goldstein8a22efa2018-03-09 15:51:02 +0200167 connected ? ovl_acceptable : NULL, mnt);
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200168 if (IS_ERR(real)) {
169 /*
170 * Treat stale file handle to lower file as "origin unknown".
171 * upper file handle could become stale when upper file is
172 * unlinked and this information is needed to handle stale
173 * index entries correctly.
174 */
175 if (real == ERR_PTR(-ESTALE) &&
176 !(fh->flags & OVL_FH_FLAG_PATH_UPPER))
177 real = NULL;
178 return real;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300179 }
180
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200181 if (ovl_dentry_weird(real)) {
182 dput(real);
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300183 return NULL;
184 }
Amir Goldsteina9d01952017-04-30 14:46:31 +0300185
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200186 return real;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300187}
188
Amir Goldsteinee1d6d372017-05-11 16:42:26 +0300189static bool ovl_is_opaquedir(struct dentry *dentry)
190{
191 return ovl_check_dir_xattr(dentry, OVL_XATTR_OPAQUE);
192}
193
Miklos Szeredie28edc42016-12-16 11:02:56 +0100194static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d,
195 const char *name, unsigned int namelen,
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100196 size_t prelen, const char *post,
Miklos Szeredie28edc42016-12-16 11:02:56 +0100197 struct dentry **ret)
198{
199 struct dentry *this;
200 int err;
Vivek Goyal102b0d12018-03-09 15:44:43 -0500201 bool last_element = !post[0];
Miklos Szeredie28edc42016-12-16 11:02:56 +0100202
203 this = lookup_one_len_unlocked(name, base, namelen);
204 if (IS_ERR(this)) {
205 err = PTR_ERR(this);
206 this = NULL;
207 if (err == -ENOENT || err == -ENAMETOOLONG)
208 goto out;
209 goto out_err;
210 }
211 if (!this->d_inode)
212 goto put_and_out;
213
214 if (ovl_dentry_weird(this)) {
215 /* Don't support traversing automounts and other weirdness */
216 err = -EREMOTE;
217 goto out_err;
218 }
219 if (ovl_is_whiteout(this)) {
220 d->stop = d->opaque = true;
221 goto put_and_out;
222 }
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400223 /*
224 * This dentry should be a regular file if previous layer lookup
225 * found a metacopy dentry.
226 */
227 if (last_element && d->metacopy && !d_is_reg(this)) {
Miklos Szeredie28edc42016-12-16 11:02:56 +0100228 d->stop = true;
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400229 goto put_and_out;
230 }
231 if (!d_can_lookup(this)) {
232 if (d->is_dir || !last_element) {
233 d->stop = true;
Miklos Szeredie28edc42016-12-16 11:02:56 +0100234 goto put_and_out;
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400235 }
236 err = ovl_check_metacopy_xattr(this);
237 if (err < 0)
238 goto out_err;
Miklos Szeredi3a291772018-04-12 12:04:49 +0200239
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400240 d->metacopy = err;
241 d->stop = !d->metacopy;
Vivek Goyalb8a88242018-05-11 11:49:31 -0400242 if (!d->metacopy || d->last)
243 goto out;
Vivek Goyal0618a812018-05-11 11:49:31 -0400244 } else {
Amir Goldstein146d62e2019-04-18 17:42:08 +0300245 if (ovl_lookup_trap_inode(d->sb, this)) {
246 /* Caught in a trap of overlapping layers */
247 err = -ELOOP;
248 goto out_err;
249 }
250
Vivek Goyal102b0d12018-03-09 15:44:43 -0500251 if (last_element)
Vivek Goyal0618a812018-05-11 11:49:31 -0400252 d->is_dir = true;
253 if (d->last)
254 goto out;
255
256 if (ovl_is_opaquedir(this)) {
257 d->stop = true;
258 if (last_element)
259 d->opaque = true;
260 goto out;
261 }
Miklos Szeredie28edc42016-12-16 11:02:56 +0100262 }
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100263 err = ovl_check_redirect(this, d, prelen, post);
264 if (err)
265 goto out_err;
Miklos Szeredie28edc42016-12-16 11:02:56 +0100266out:
267 *ret = this;
268 return 0;
269
270put_and_out:
271 dput(this);
272 this = NULL;
273 goto out;
274
275out_err:
276 dput(this);
277 return err;
278}
279
280static int ovl_lookup_layer(struct dentry *base, struct ovl_lookup_data *d,
281 struct dentry **ret)
282{
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100283 /* Counting down from the end, since the prefix can change */
284 size_t rem = d->name.len - 1;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100285 struct dentry *dentry = NULL;
286 int err;
287
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100288 if (d->name.name[0] != '/')
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100289 return ovl_lookup_single(base, d, d->name.name, d->name.len,
290 0, "", ret);
291
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100292 while (!IS_ERR_OR_NULL(base) && d_can_lookup(base)) {
293 const char *s = d->name.name + d->name.len - rem;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100294 const char *next = strchrnul(s, '/');
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100295 size_t thislen = next - s;
296 bool end = !next[0];
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100297
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100298 /* Verify we did not go off the rails */
299 if (WARN_ON(s[-1] != '/'))
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100300 return -EIO;
301
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100302 err = ovl_lookup_single(base, d, s, thislen,
303 d->name.len - rem, next, &base);
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100304 dput(dentry);
305 if (err)
306 return err;
307 dentry = base;
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100308 if (end)
309 break;
310
311 rem -= thislen + 1;
312
313 if (WARN_ON(rem >= d->name.len))
314 return -EIO;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100315 }
316 *ret = dentry;
317 return 0;
Miklos Szeredie28edc42016-12-16 11:02:56 +0100318}
319
Amir Goldsteina9d01952017-04-30 14:46:31 +0300320
Amir Goldstein8a22efa2018-03-09 15:51:02 +0200321int ovl_check_origin_fh(struct ovl_fs *ofs, struct ovl_fh *fh, bool connected,
Amir Goldsteinf9418662018-01-19 21:33:44 +0200322 struct dentry *upperdentry, struct ovl_path **stackp)
Amir Goldsteina9d01952017-04-30 14:46:31 +0300323{
Amir Goldsteinf7d3dac2017-06-21 15:28:34 +0300324 struct dentry *origin = NULL;
325 int i;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300326
Amir Goldstein1eff1a12017-12-12 22:40:46 +0200327 for (i = 0; i < ofs->numlower; i++) {
Amir Goldstein8a22efa2018-03-09 15:51:02 +0200328 origin = ovl_decode_real_fh(fh, ofs->lower_layers[i].mnt,
329 connected);
Amir Goldsteinf7d3dac2017-06-21 15:28:34 +0300330 if (origin)
331 break;
332 }
333
334 if (!origin)
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300335 return -ESTALE;
336 else if (IS_ERR(origin))
337 return PTR_ERR(origin);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300338
Amir Goldsteinf9418662018-01-19 21:33:44 +0200339 if (upperdentry && !ovl_is_whiteout(upperdentry) &&
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300340 ((d_inode(origin)->i_mode ^ d_inode(upperdentry)->i_mode) & S_IFMT))
341 goto invalid;
342
Amir Goldstein415543d2017-06-21 15:28:42 +0300343 if (!*stackp)
Chandan Rajendrab9343632017-07-24 01:57:54 -0500344 *stackp = kmalloc(sizeof(struct ovl_path), GFP_KERNEL);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300345 if (!*stackp) {
346 dput(origin);
347 return -ENOMEM;
348 }
Amir Goldstein1eff1a12017-12-12 22:40:46 +0200349 **stackp = (struct ovl_path){
350 .dentry = origin,
351 .layer = &ofs->lower_layers[i]
352 };
Amir Goldsteina9d01952017-04-30 14:46:31 +0300353
354 return 0;
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300355
356invalid:
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;
362}
363
Amir Goldstein1eff1a12017-12-12 22:40:46 +0200364static int ovl_check_origin(struct ovl_fs *ofs, struct dentry *upperdentry,
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300365 struct ovl_path **stackp, unsigned int *ctrp)
366{
Amir Goldstein05122442018-01-11 08:25:32 +0200367 struct ovl_fh *fh = ovl_get_fh(upperdentry, OVL_XATTR_ORIGIN);
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300368 int err;
369
370 if (IS_ERR_OR_NULL(fh))
371 return PTR_ERR(fh);
372
Amir Goldstein8a22efa2018-03-09 15:51:02 +0200373 err = ovl_check_origin_fh(ofs, fh, false, upperdentry, stackp);
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300374 kfree(fh);
375
376 if (err) {
377 if (err == -ESTALE)
378 return 0;
379 return err;
380 }
381
382 if (WARN_ON(*ctrp))
383 return -EIO;
384
385 *ctrp = 1;
386 return 0;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300387}
388
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100389/*
Amir Goldstein05122442018-01-11 08:25:32 +0200390 * Verify that @fh matches the file handle stored in xattr @name.
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300391 * Return 0 on match, -ESTALE on mismatch, < 0 on error.
392 */
Amir Goldstein05122442018-01-11 08:25:32 +0200393static int ovl_verify_fh(struct dentry *dentry, const char *name,
394 const struct ovl_fh *fh)
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300395{
Amir Goldstein05122442018-01-11 08:25:32 +0200396 struct ovl_fh *ofh = ovl_get_fh(dentry, name);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300397 int err = 0;
398
399 if (!ofh)
400 return -ENODATA;
401
402 if (IS_ERR(ofh))
403 return PTR_ERR(ofh);
404
405 if (fh->len != ofh->len || memcmp(fh, ofh, fh->len))
406 err = -ESTALE;
407
408 kfree(ofh);
409 return err;
410}
411
412/*
Amir Goldstein05122442018-01-11 08:25:32 +0200413 * Verify that @real dentry matches the file handle stored in xattr @name.
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300414 *
Amir Goldstein05122442018-01-11 08:25:32 +0200415 * If @set is true and there is no stored file handle, encode @real and store
416 * file handle in xattr @name.
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300417 *
Amir Goldstein05122442018-01-11 08:25:32 +0200418 * Return 0 on match, -ESTALE on mismatch, -ENODATA on no xattr, < 0 on error.
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300419 */
Amir Goldstein05122442018-01-11 08:25:32 +0200420int ovl_verify_set_fh(struct dentry *dentry, const char *name,
421 struct dentry *real, bool is_upper, bool set)
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300422{
423 struct inode *inode;
424 struct ovl_fh *fh;
425 int err;
426
Amir Goldstein5b2cccd2018-02-02 10:42:03 +0200427 fh = ovl_encode_real_fh(real, is_upper);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300428 err = PTR_ERR(fh);
Amir Goldsteinbabf4772018-10-10 19:10:06 +0300429 if (IS_ERR(fh)) {
430 fh = NULL;
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300431 goto fail;
Amir Goldsteinbabf4772018-10-10 19:10:06 +0300432 }
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300433
Amir Goldstein05122442018-01-11 08:25:32 +0200434 err = ovl_verify_fh(dentry, name, fh);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300435 if (set && err == -ENODATA)
Amir Goldstein05122442018-01-11 08:25:32 +0200436 err = ovl_do_setxattr(dentry, name, fh, fh->len, 0);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300437 if (err)
438 goto fail;
439
440out:
441 kfree(fh);
442 return err;
443
444fail:
Amir Goldstein05122442018-01-11 08:25:32 +0200445 inode = d_inode(real);
446 pr_warn_ratelimited("overlayfs: failed to verify %s (%pd2, ino=%lu, err=%i)\n",
447 is_upper ? "upper" : "origin", real,
448 inode ? inode->i_ino : 0, err);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300449 goto out;
450}
451
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200452/* Get upper dentry from index */
Amir Goldstein3b0bfc62017-12-24 18:42:16 +0200453struct dentry *ovl_index_upper(struct ovl_fs *ofs, struct dentry *index)
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200454{
455 struct ovl_fh *fh;
456 struct dentry *upper;
457
458 if (!d_is_dir(index))
459 return dget(index);
460
461 fh = ovl_get_fh(index, OVL_XATTR_UPPER);
462 if (IS_ERR_OR_NULL(fh))
463 return ERR_CAST(fh);
464
Amir Goldstein8a22efa2018-03-09 15:51:02 +0200465 upper = ovl_decode_real_fh(fh, ofs->upper_mnt, true);
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200466 kfree(fh);
467
468 if (IS_ERR_OR_NULL(upper))
469 return upper ?: ERR_PTR(-ESTALE);
470
471 if (!d_is_dir(upper)) {
472 pr_warn_ratelimited("overlayfs: invalid index upper (%pd2, upper=%pd2).\n",
473 index, upper);
474 dput(upper);
475 return ERR_PTR(-EIO);
476 }
477
478 return upper;
479}
480
Amir Goldstein9ee60ce2017-11-01 10:13:51 +0200481/* Is this a leftover from create/whiteout of directory index entry? */
482static bool ovl_is_temp_index(struct dentry *index)
483{
484 return index->d_name.name[0] == '#';
485}
486
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300487/*
Amir Goldstein415543d2017-06-21 15:28:42 +0300488 * Verify that an index entry name matches the origin file handle stored in
489 * OVL_XATTR_ORIGIN and that origin file handle can be decoded to lower path.
490 * Return 0 on match, -ESTALE on mismatch or stale origin, < 0 on error.
491 */
Amir Goldstein1eff1a12017-12-12 22:40:46 +0200492int ovl_verify_index(struct ovl_fs *ofs, struct dentry *index)
Amir Goldstein415543d2017-06-21 15:28:42 +0300493{
494 struct ovl_fh *fh = NULL;
495 size_t len;
Chandan Rajendrab9343632017-07-24 01:57:54 -0500496 struct ovl_path origin = { };
497 struct ovl_path *stack = &origin;
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200498 struct dentry *upper = NULL;
Amir Goldstein415543d2017-06-21 15:28:42 +0300499 int err;
500
501 if (!d_inode(index))
502 return 0;
503
Amir Goldstein9ee60ce2017-11-01 10:13:51 +0200504 /* Cleanup leftover from index create/cleanup attempt */
505 err = -ESTALE;
506 if (ovl_is_temp_index(index))
507 goto fail;
508
Amir Goldsteinfa0096e2017-10-24 12:24:11 +0300509 err = -EINVAL;
Amir Goldstein415543d2017-06-21 15:28:42 +0300510 if (index->d_name.len < sizeof(struct ovl_fh)*2)
511 goto fail;
512
513 err = -ENOMEM;
514 len = index->d_name.len / 2;
Michal Hocko0ee931c2017-09-13 16:28:29 -0700515 fh = kzalloc(len, GFP_KERNEL);
Amir Goldstein415543d2017-06-21 15:28:42 +0300516 if (!fh)
517 goto fail;
518
519 err = -EINVAL;
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300520 if (hex2bin((u8 *)fh, index->d_name.name, len))
521 goto fail;
522
523 err = ovl_check_fh_len(fh, len);
524 if (err)
Amir Goldstein415543d2017-06-21 15:28:42 +0300525 goto fail;
526
Amir Goldstein7db25d32018-01-11 11:03:13 +0200527 /*
528 * Whiteout index entries are used as an indication that an exported
529 * overlay file handle should be treated as stale (i.e. after unlink
530 * of the overlay inode). These entries contain no origin xattr.
531 */
532 if (ovl_is_whiteout(index))
533 goto out;
534
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200535 /*
536 * Verifying directory index entries are not stale is expensive, so
537 * only verify stale dir index if NFS export is enabled.
538 */
539 if (d_is_dir(index) && !ofs->config.nfs_export)
540 goto out;
541
542 /*
543 * Directory index entries should have 'upper' xattr pointing to the
544 * real upper dir. Non-dir index entries are hardlinks to the upper
545 * real inode. For non-dir index, we can read the copy up origin xattr
546 * directly from the index dentry, but for dir index we first need to
547 * decode the upper directory.
548 */
549 upper = ovl_index_upper(ofs, index);
550 if (IS_ERR_OR_NULL(upper)) {
551 err = PTR_ERR(upper);
Amir Goldstein24f0b172018-01-11 15:33:51 +0200552 /*
553 * Directory index entries with no 'upper' xattr need to be
554 * removed. When dir index entry has a stale 'upper' xattr,
555 * we assume that upper dir was removed and we treat the dir
556 * index as orphan entry that needs to be whited out.
557 */
558 if (err == -ESTALE)
559 goto orphan;
560 else if (!err)
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200561 err = -ESTALE;
562 goto fail;
563 }
564
565 err = ovl_verify_fh(upper, OVL_XATTR_ORIGIN, fh);
566 dput(upper);
Amir Goldstein415543d2017-06-21 15:28:42 +0300567 if (err)
568 goto fail;
569
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200570 /* Check if non-dir index is orphan and don't warn before cleaning it */
571 if (!d_is_dir(index) && d_inode(index)->i_nlink == 1) {
Amir Goldstein8a22efa2018-03-09 15:51:02 +0200572 err = ovl_check_origin_fh(ofs, fh, false, index, &stack);
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200573 if (err)
574 goto fail;
Amir Goldstein415543d2017-06-21 15:28:42 +0300575
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200576 if (ovl_get_nlink(origin.dentry, index, 0) == 0)
Amir Goldstein24f0b172018-01-11 15:33:51 +0200577 goto orphan;
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200578 }
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300579
Amir Goldstein415543d2017-06-21 15:28:42 +0300580out:
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200581 dput(origin.dentry);
Amir Goldstein415543d2017-06-21 15:28:42 +0300582 kfree(fh);
583 return err;
584
585fail:
Amir Goldstein61b67472017-07-18 21:07:42 +0300586 pr_warn_ratelimited("overlayfs: failed to verify index (%pd2, ftype=%x, err=%i)\n",
587 index, d_inode(index)->i_mode & S_IFMT, err);
Amir Goldstein415543d2017-06-21 15:28:42 +0300588 goto out;
Amir Goldstein24f0b172018-01-11 15:33:51 +0200589
590orphan:
591 pr_warn_ratelimited("overlayfs: orphan index entry (%pd2, ftype=%x, nlink=%u)\n",
592 index, d_inode(index)->i_mode & S_IFMT,
593 d_inode(index)->i_nlink);
594 err = -ENOENT;
595 goto out;
Amir Goldstein415543d2017-06-21 15:28:42 +0300596}
597
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200598static int ovl_get_index_name_fh(struct ovl_fh *fh, struct qstr *name)
599{
600 char *n, *s;
601
Kees Cook6396bb22018-06-12 14:03:40 -0700602 n = kcalloc(fh->len, 2, GFP_KERNEL);
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200603 if (!n)
604 return -ENOMEM;
605
606 s = bin2hex(n, fh, fh->len);
607 *name = (struct qstr) QSTR_INIT(n, s - n);
608
609 return 0;
610
611}
612
Amir Goldstein415543d2017-06-21 15:28:42 +0300613/*
Amir Goldstein359f3922017-06-21 15:28:41 +0300614 * Lookup in indexdir for the index entry of a lower real inode or a copy up
615 * origin inode. The index entry name is the hex representation of the lower
616 * inode file handle.
617 *
618 * If the index dentry in negative, then either no lower aliases have been
619 * copied up yet, or aliases have been copied up in older kernels and are
620 * not indexed.
621 *
622 * If the index dentry for a copy up origin inode is positive, but points
623 * to an inode different than the upper inode, then either the upper inode
624 * has been copied up and not indexed or it was indexed, but since then
625 * index dir was cleared. Either way, that index cannot be used to indentify
626 * the overlay inode.
627 */
628int ovl_get_index_name(struct dentry *origin, struct qstr *name)
629{
Amir Goldstein359f3922017-06-21 15:28:41 +0300630 struct ovl_fh *fh;
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200631 int err;
Amir Goldstein359f3922017-06-21 15:28:41 +0300632
Amir Goldstein5b2cccd2018-02-02 10:42:03 +0200633 fh = ovl_encode_real_fh(origin, false);
Amir Goldstein359f3922017-06-21 15:28:41 +0300634 if (IS_ERR(fh))
635 return PTR_ERR(fh);
636
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200637 err = ovl_get_index_name_fh(fh, name);
638
Amir Goldstein359f3922017-06-21 15:28:41 +0300639 kfree(fh);
Amir Goldstein359f3922017-06-21 15:28:41 +0300640 return err;
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200641}
Amir Goldstein359f3922017-06-21 15:28:41 +0300642
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200643/* Lookup index by file handle for NFS export */
644struct dentry *ovl_get_index_fh(struct ovl_fs *ofs, struct ovl_fh *fh)
645{
646 struct dentry *index;
647 struct qstr name;
648 int err;
649
650 err = ovl_get_index_name_fh(fh, &name);
651 if (err)
652 return ERR_PTR(err);
653
654 index = lookup_one_len_unlocked(name.name, ofs->indexdir, name.len);
655 kfree(name.name);
656 if (IS_ERR(index)) {
657 if (PTR_ERR(index) == -ENOENT)
658 index = NULL;
659 return index;
660 }
661
662 if (d_is_negative(index))
663 err = 0;
664 else if (ovl_is_whiteout(index))
665 err = -ESTALE;
666 else if (ovl_dentry_weird(index))
667 err = -EIO;
668 else
669 return index;
670
671 dput(index);
672 return ERR_PTR(err);
Amir Goldstein359f3922017-06-21 15:28:41 +0300673}
674
Amir Goldstein06170152018-01-17 14:40:27 +0200675struct dentry *ovl_lookup_index(struct ovl_fs *ofs, struct dentry *upper,
676 struct dentry *origin, bool verify)
Amir Goldstein359f3922017-06-21 15:28:41 +0300677{
Amir Goldstein359f3922017-06-21 15:28:41 +0300678 struct dentry *index;
679 struct inode *inode;
680 struct qstr name;
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200681 bool is_dir = d_is_dir(origin);
Amir Goldstein359f3922017-06-21 15:28:41 +0300682 int err;
683
684 err = ovl_get_index_name(origin, &name);
685 if (err)
686 return ERR_PTR(err);
687
688 index = lookup_one_len_unlocked(name.name, ofs->indexdir, name.len);
689 if (IS_ERR(index)) {
Amir Goldsteine0082a02017-09-24 13:01:35 +0300690 err = PTR_ERR(index);
Amir Goldstein7937a562017-10-20 17:19:06 +0300691 if (err == -ENOENT) {
692 index = NULL;
693 goto out;
694 }
Amir Goldstein601350f2018-09-28 21:00:48 +0300695 pr_warn_ratelimited("overlayfs: failed inode index lookup (ino=%lu, key=%.*s, err=%i);\n"
Amir Goldstein359f3922017-06-21 15:28:41 +0300696 "overlayfs: mount with '-o index=off' to disable inodes index.\n",
697 d_inode(origin)->i_ino, name.len, name.name,
698 err);
699 goto out;
700 }
701
Amir Goldstein0e082552017-07-18 21:07:43 +0300702 inode = d_inode(index);
Amir Goldstein359f3922017-06-21 15:28:41 +0300703 if (d_is_negative(index)) {
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300704 goto out_dput;
Amir Goldstein06170152018-01-17 14:40:27 +0200705 } else if (ovl_is_whiteout(index) && !verify) {
706 /*
707 * When index lookup is called with !verify for decoding an
708 * overlay file handle, a whiteout index implies that decode
709 * should treat file handle as stale and no need to print a
710 * warning about it.
711 */
712 dput(index);
713 index = ERR_PTR(-ESTALE);
714 goto out;
Amir Goldstein0e082552017-07-18 21:07:43 +0300715 } else if (ovl_dentry_weird(index) || ovl_is_whiteout(index) ||
716 ((inode->i_mode ^ d_inode(origin)->i_mode) & S_IFMT)) {
717 /*
718 * Index should always be of the same file type as origin
719 * except for the case of a whiteout index. A whiteout
720 * index should only exist if all lower aliases have been
721 * unlinked, which means that finding a lower origin on lookup
722 * whose index is a whiteout should be treated as an error.
723 */
724 pr_warn_ratelimited("overlayfs: bad index found (index=%pd2, ftype=%x, origin ftype=%x).\n",
725 index, d_inode(index)->i_mode & S_IFMT,
726 d_inode(origin)->i_mode & S_IFMT);
Amir Goldstein359f3922017-06-21 15:28:41 +0300727 goto fail;
Amir Goldstein06170152018-01-17 14:40:27 +0200728 } else if (is_dir && verify) {
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200729 if (!upper) {
730 pr_warn_ratelimited("overlayfs: suspected uncovered redirected dir found (origin=%pd2, index=%pd2).\n",
731 origin, index);
732 goto fail;
733 }
Amir Goldstein359f3922017-06-21 15:28:41 +0300734
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200735 /* Verify that dir index 'upper' xattr points to upper dir */
736 err = ovl_verify_upper(index, upper, false);
737 if (err) {
738 if (err == -ESTALE) {
739 pr_warn_ratelimited("overlayfs: suspected multiply redirected dir found (upper=%pd2, origin=%pd2, index=%pd2).\n",
740 upper, origin, index);
741 }
742 goto fail;
743 }
744 } else if (upper && d_inode(upper) != inode) {
745 goto out_dput;
746 }
Amir Goldstein359f3922017-06-21 15:28:41 +0300747out:
748 kfree(name.name);
749 return index;
750
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300751out_dput:
752 dput(index);
753 index = NULL;
754 goto out;
755
Amir Goldstein359f3922017-06-21 15:28:41 +0300756fail:
757 dput(index);
758 index = ERR_PTR(-EIO);
759 goto out;
760}
761
762/*
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100763 * Returns next layer in stack starting from top.
764 * Returns -1 if this is the last layer.
765 */
766int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
767{
768 struct ovl_entry *oe = dentry->d_fsdata;
769
770 BUG_ON(idx < 0);
771 if (idx == 0) {
772 ovl_path_upper(dentry, path);
773 if (path->dentry)
774 return oe->numlower ? 1 : -1;
775 idx++;
776 }
777 BUG_ON(idx > oe->numlower);
Chandan Rajendrab9343632017-07-24 01:57:54 -0500778 path->dentry = oe->lowerstack[idx - 1].dentry;
779 path->mnt = oe->lowerstack[idx - 1].layer->mnt;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100780
781 return (idx < oe->numlower) ? idx + 1 : -1;
782}
783
Amir Goldstein9678e632018-01-03 19:34:45 +0200784/* Fix missing 'origin' xattr */
785static int ovl_fix_origin(struct dentry *dentry, struct dentry *lower,
786 struct dentry *upper)
787{
788 int err;
789
790 if (ovl_check_origin_xattr(upper))
791 return 0;
792
793 err = ovl_want_write(dentry);
794 if (err)
795 return err;
796
797 err = ovl_set_origin(dentry, lower, upper);
798 if (!err)
799 err = ovl_set_impure(dentry->d_parent, upper->d_parent);
800
801 ovl_drop_write(dentry);
802 return err;
803}
804
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100805struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
806 unsigned int flags)
807{
808 struct ovl_entry *oe;
809 const struct cred *old_cred;
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100810 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100811 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
Amir Goldsteinc22205d2017-04-26 23:40:52 +0300812 struct ovl_entry *roe = dentry->d_sb->s_root->d_fsdata;
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400813 struct ovl_path *stack = NULL, *origin_path = NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100814 struct dentry *upperdir, *upperdentry = NULL;
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200815 struct dentry *origin = NULL;
Amir Goldstein359f3922017-06-21 15:28:41 +0300816 struct dentry *index = NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100817 unsigned int ctr = 0;
818 struct inode *inode = NULL;
819 bool upperopaque = false;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100820 char *upperredirect = NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100821 struct dentry *this;
822 unsigned int i;
823 int err;
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400824 bool metacopy = false;
Miklos Szeredie28edc42016-12-16 11:02:56 +0100825 struct ovl_lookup_data d = {
Amir Goldstein146d62e2019-04-18 17:42:08 +0300826 .sb = dentry->d_sb,
Miklos Szeredie28edc42016-12-16 11:02:56 +0100827 .name = dentry->d_name,
828 .is_dir = false,
829 .opaque = false,
830 .stop = false,
Vivek Goyal452061f2018-03-09 15:44:41 -0500831 .last = ofs->config.redirect_follow ? false : !poe->numlower,
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100832 .redirect = NULL,
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400833 .metacopy = false,
Miklos Szeredie28edc42016-12-16 11:02:56 +0100834 };
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100835
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100836 if (dentry->d_name.len > ofs->namelen)
837 return ERR_PTR(-ENAMETOOLONG);
838
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100839 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200840 upperdir = ovl_dentry_upper(dentry->d_parent);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100841 if (upperdir) {
Miklos Szeredie28edc42016-12-16 11:02:56 +0100842 err = ovl_lookup_layer(upperdir, &d, &upperdentry);
843 if (err)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100844 goto out;
845
Miklos Szeredie28edc42016-12-16 11:02:56 +0100846 if (upperdentry && unlikely(ovl_dentry_remote(upperdentry))) {
847 dput(upperdentry);
848 err = -EREMOTE;
849 goto out;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100850 }
Amir Goldsteina9d01952017-04-30 14:46:31 +0300851 if (upperdentry && !d.is_dir) {
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400852 unsigned int origin_ctr = 0;
853
Amir Goldsteinf7d3dac2017-06-21 15:28:34 +0300854 /*
855 * Lookup copy up origin by decoding origin file handle.
856 * We may get a disconnected dentry, which is fine,
857 * because we only need to hold the origin inode in
858 * cache and use its inode number. We may even get a
859 * connected dentry, that is not under any of the lower
860 * layers root. That is also fine for using it's inode
861 * number - it's the same as if we held a reference
862 * to a dentry in lower layer that was moved under us.
863 */
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400864 err = ovl_check_origin(ofs, upperdentry, &origin_path,
865 &origin_ctr);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300866 if (err)
Vivek Goyal5455f922017-11-01 15:37:22 -0400867 goto out_put_upper;
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400868
869 if (d.metacopy)
870 metacopy = true;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300871 }
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100872
873 if (d.redirect) {
Dan Carpenter0ce5cdc2017-09-22 23:45:18 +0300874 err = -ENOMEM;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100875 upperredirect = kstrdup(d.redirect, GFP_KERNEL);
876 if (!upperredirect)
877 goto out_put_upper;
878 if (d.redirect[0] == '/')
Amir Goldsteinc22205d2017-04-26 23:40:52 +0300879 poe = roe;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100880 }
Miklos Szeredie28edc42016-12-16 11:02:56 +0100881 upperopaque = d.opaque;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100882 }
883
Miklos Szeredie28edc42016-12-16 11:02:56 +0100884 if (!d.stop && poe->numlower) {
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100885 err = -ENOMEM;
Chandan Rajendrab9343632017-07-24 01:57:54 -0500886 stack = kcalloc(ofs->numlower, sizeof(struct ovl_path),
Michal Hocko0ee931c2017-09-13 16:28:29 -0700887 GFP_KERNEL);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100888 if (!stack)
889 goto out_put_upper;
890 }
891
Miklos Szeredie28edc42016-12-16 11:02:56 +0100892 for (i = 0; !d.stop && i < poe->numlower; i++) {
Chandan Rajendrab9343632017-07-24 01:57:54 -0500893 struct ovl_path lower = poe->lowerstack[i];
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100894
Vivek Goyal452061f2018-03-09 15:44:41 -0500895 if (!ofs->config.redirect_follow)
896 d.last = i == poe->numlower - 1;
897 else
898 d.last = lower.layer->idx == roe->numlower;
899
Chandan Rajendrab9343632017-07-24 01:57:54 -0500900 err = ovl_lookup_layer(lower.dentry, &d, &this);
Miklos Szeredie28edc42016-12-16 11:02:56 +0100901 if (err)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100902 goto out_put;
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100903
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100904 if (!this)
905 continue;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100906
Amir Goldstein9678e632018-01-03 19:34:45 +0200907 /*
908 * If no origin fh is stored in upper of a merge dir, store fh
909 * of lower dir and set upper parent "impure".
910 */
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400911 if (upperdentry && !ctr && !ofs->noxattr && d.is_dir) {
Amir Goldstein9678e632018-01-03 19:34:45 +0200912 err = ovl_fix_origin(dentry, this, upperdentry);
913 if (err) {
914 dput(this);
915 goto out_put;
916 }
917 }
918
Amir Goldstein37b129162018-01-10 22:29:38 +0200919 /*
920 * When "verify_lower" feature is enabled, do not merge with a
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200921 * lower dir that does not match a stored origin xattr. In any
922 * case, only verified origin is used for index lookup.
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400923 *
924 * For non-dir dentry, if index=on, then ensure origin
925 * matches the dentry found using path based lookup,
926 * otherwise error out.
Amir Goldstein37b129162018-01-10 22:29:38 +0200927 */
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400928 if (upperdentry && !ctr &&
929 ((d.is_dir && ovl_verify_lower(dentry->d_sb)) ||
930 (!d.is_dir && ofs->config.index && origin_path))) {
Amir Goldstein37b129162018-01-10 22:29:38 +0200931 err = ovl_verify_origin(upperdentry, this, false);
932 if (err) {
933 dput(this);
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400934 if (d.is_dir)
935 break;
936 goto out_put;
Amir Goldstein37b129162018-01-10 22:29:38 +0200937 }
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200938 origin = this;
Amir Goldstein37b129162018-01-10 22:29:38 +0200939 }
940
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400941 if (d.metacopy)
942 metacopy = true;
943 /*
944 * Do not store intermediate metacopy dentries in chain,
945 * except top most lower metacopy dentry
946 */
947 if (d.metacopy && ctr) {
948 dput(this);
949 continue;
950 }
951
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100952 stack[ctr].dentry = this;
Chandan Rajendrab9343632017-07-24 01:57:54 -0500953 stack[ctr].layer = lower.layer;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100954 ctr++;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100955
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100956 /*
957 * Following redirects can have security consequences: it's like
958 * a symlink into the lower layer without the permission checks.
959 * This is only a problem if the upper layer is untrusted (e.g
960 * comes from an USB drive). This can allow a non-readable file
961 * or directory to become readable.
962 *
963 * Only following redirects when redirects are enabled disables
964 * this attack vector when not necessary.
965 */
966 err = -EPERM;
967 if (d.redirect && !ofs->config.redirect_follow) {
Amir Goldsteinf8167812017-12-18 14:25:56 +0200968 pr_warn_ratelimited("overlayfs: refusing to follow redirect for (%pd2)\n",
969 dentry);
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100970 goto out_put;
971 }
972
Vivek Goyald1fe96c2018-02-02 10:23:24 -0500973 if (d.stop)
974 break;
975
Amir Goldsteinc22205d2017-04-26 23:40:52 +0300976 if (d.redirect && d.redirect[0] == '/' && poe != roe) {
977 poe = roe;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100978 /* Find the current layer on the root dentry */
Amir Goldsteind583ed72017-11-08 19:23:36 +0200979 i = lower.layer->idx - 1;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100980 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100981 }
982
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400983 if (metacopy) {
984 /*
985 * Found a metacopy dentry but did not find corresponding
986 * data dentry
987 */
988 if (d.metacopy) {
989 err = -EIO;
990 goto out_put;
991 }
992
993 err = -EPERM;
994 if (!ofs->config.metacopy) {
995 pr_warn_ratelimited("overlay: refusing to follow metacopy origin for (%pd2)\n",
996 dentry);
997 goto out_put;
998 }
999 } else if (!d.is_dir && upperdentry && !ctr && origin_path) {
1000 if (WARN_ON(stack != NULL)) {
1001 err = -EIO;
1002 goto out_put;
1003 }
1004 stack = origin_path;
1005 ctr = 1;
1006 origin_path = NULL;
1007 }
1008
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001009 /*
1010 * Lookup index by lower inode and verify it matches upper inode.
1011 * We only trust dir index if we verified that lower dir matches
1012 * origin, otherwise dir index entries may be inconsistent and we
Vivek Goyal9d3dfea2018-05-11 11:49:28 -04001013 * ignore them.
1014 *
1015 * For non-dir upper metacopy dentry, we already set "origin" if we
1016 * verified that lower matched upper origin. If upper origin was
1017 * not present (because lower layer did not support fh encode/decode),
1018 * or indexing is not enabled, do not set "origin" and skip looking up
1019 * index. This case should be handled in same way as a non-dir upper
1020 * without ORIGIN is handled.
1021 *
1022 * Always lookup index of non-dir non-metacopy and non-upper.
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001023 */
Vivek Goyal9d3dfea2018-05-11 11:49:28 -04001024 if (ctr && (!upperdentry || (!d.is_dir && !metacopy)))
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001025 origin = stack[0].dentry;
Amir Goldstein359f3922017-06-21 15:28:41 +03001026
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001027 if (origin && ovl_indexdir(dentry->d_sb) &&
1028 (!d.is_dir || ovl_index_all(dentry->d_sb))) {
Amir Goldstein06170152018-01-17 14:40:27 +02001029 index = ovl_lookup_index(ofs, upperdentry, origin, true);
Amir Goldstein359f3922017-06-21 15:28:41 +03001030 if (IS_ERR(index)) {
1031 err = PTR_ERR(index);
1032 index = NULL;
1033 goto out_put;
1034 }
1035 }
1036
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001037 oe = ovl_alloc_entry(ctr);
1038 err = -ENOMEM;
1039 if (!oe)
1040 goto out_put;
1041
Chandan Rajendrab9343632017-07-24 01:57:54 -05001042 memcpy(oe->lowerstack, stack, sizeof(struct ovl_path) * ctr);
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +02001043 dentry->d_fsdata = oe;
1044
Amir Goldsteinc62520a2018-01-14 19:25:31 +02001045 if (upperopaque)
1046 ovl_dentry_set_opaque(dentry);
1047
Miklos Szeredi55acc662017-07-04 22:03:18 +02001048 if (upperdentry)
1049 ovl_dentry_set_upper_alias(dentry);
Vivek Goyal0a2d0d32018-05-11 11:49:32 -04001050 else if (index) {
Amir Goldstein359f3922017-06-21 15:28:41 +03001051 upperdentry = dget(index);
Vivek Goyal0a2d0d32018-05-11 11:49:32 -04001052 upperredirect = ovl_get_redirect_xattr(upperdentry, 0);
1053 if (IS_ERR(upperredirect)) {
1054 err = PTR_ERR(upperredirect);
1055 upperredirect = NULL;
1056 goto out_free_oe;
1057 }
1058 }
Amir Goldstein359f3922017-06-21 15:28:41 +03001059
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +02001060 if (upperdentry || ctr) {
Vivek Goyalac6a52e2018-05-08 09:27:21 -04001061 struct ovl_inode_params oip = {
1062 .upperdentry = upperdentry,
1063 .lowerpath = stack,
1064 .index = index,
1065 .numlower = ctr,
Vivek Goyal9cec54c2018-05-11 11:49:27 -04001066 .redirect = upperredirect,
Vivek Goyal2664bd02018-05-11 11:49:30 -04001067 .lowerdata = (ctr > 1 && !d.is_dir) ?
1068 stack[ctr - 1].dentry : NULL,
Vivek Goyalac6a52e2018-05-08 09:27:21 -04001069 };
1070
1071 inode = ovl_get_inode(dentry->d_sb, &oip);
Miklos Szeredib9ac5c272017-07-04 22:03:17 +02001072 err = PTR_ERR(inode);
1073 if (IS_ERR(inode))
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +02001074 goto out_free_oe;
1075 }
1076
1077 revert_creds(old_cred);
Vivek Goyal9d3dfea2018-05-11 11:49:28 -04001078 if (origin_path) {
1079 dput(origin_path->dentry);
1080 kfree(origin_path);
1081 }
Amir Goldstein359f3922017-06-21 15:28:41 +03001082 dput(index);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001083 kfree(stack);
Miklos Szeredi02b69b22016-12-16 11:02:56 +01001084 kfree(d.redirect);
Amir Goldstein829c28b2017-09-29 21:43:07 +03001085 return d_splice_alias(inode, dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001086
1087out_free_oe:
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +02001088 dentry->d_fsdata = NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001089 kfree(oe);
1090out_put:
Amir Goldstein359f3922017-06-21 15:28:41 +03001091 dput(index);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001092 for (i = 0; i < ctr; i++)
1093 dput(stack[i].dentry);
1094 kfree(stack);
1095out_put_upper:
Vivek Goyal9d3dfea2018-05-11 11:49:28 -04001096 if (origin_path) {
1097 dput(origin_path->dentry);
1098 kfree(origin_path);
1099 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001100 dput(upperdentry);
Miklos Szeredi02b69b22016-12-16 11:02:56 +01001101 kfree(upperredirect);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001102out:
Miklos Szeredi02b69b22016-12-16 11:02:56 +01001103 kfree(d.redirect);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001104 revert_creds(old_cred);
1105 return ERR_PTR(err);
1106}
1107
1108bool ovl_lower_positive(struct dentry *dentry)
1109{
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001110 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
1111 const struct qstr *name = &dentry->d_name;
Amir Goldstein6d0a8a92017-11-10 13:18:07 +02001112 const struct cred *old_cred;
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001113 unsigned int i;
1114 bool positive = false;
1115 bool done = false;
1116
1117 /*
1118 * If dentry is negative, then lower is positive iff this is a
1119 * whiteout.
1120 */
1121 if (!dentry->d_inode)
Amir Goldsteinc62520a2018-01-14 19:25:31 +02001122 return ovl_dentry_is_opaque(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001123
1124 /* Negative upper -> positive lower */
Miklos Szeredi09d8b582017-07-04 22:03:16 +02001125 if (!ovl_dentry_upper(dentry))
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001126 return true;
1127
Amir Goldstein6d0a8a92017-11-10 13:18:07 +02001128 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001129 /* Positive upper -> have to look up lower to see whether it exists */
1130 for (i = 0; !done && !positive && i < poe->numlower; i++) {
1131 struct dentry *this;
1132 struct dentry *lowerdir = poe->lowerstack[i].dentry;
1133
1134 this = lookup_one_len_unlocked(name->name, lowerdir,
1135 name->len);
1136 if (IS_ERR(this)) {
1137 switch (PTR_ERR(this)) {
1138 case -ENOENT:
1139 case -ENAMETOOLONG:
1140 break;
1141
1142 default:
1143 /*
1144 * Assume something is there, we just couldn't
1145 * access it.
1146 */
1147 positive = true;
1148 break;
1149 }
1150 } else {
1151 if (this->d_inode) {
1152 positive = !ovl_is_whiteout(this);
1153 done = true;
1154 }
1155 dput(this);
1156 }
1157 }
Amir Goldstein6d0a8a92017-11-10 13:18:07 +02001158 revert_creds(old_cred);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001159
1160 return positive;
1161}