blob: 85ab856dd134564e7b13cbd648291e30a001814f [file] [log] [blame]
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001/*
2 * Copyright (C) 2011 Novell Inc.
3 * Copyright (C) 2016 Red Hat, Inc.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 */
9
10#include <linux/fs.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +010011#include <linux/cred.h>
Amir Goldstein9ee60ce2017-11-01 10:13:51 +020012#include <linux/ctype.h>
Miklos Szeredibbb1e542016-12-16 11:02:56 +010013#include <linux/namei.h>
14#include <linux/xattr.h>
Miklos Szeredi02b69b22016-12-16 11:02:56 +010015#include <linux/ratelimit.h>
Amir Goldsteina9d01952017-04-30 14:46:31 +030016#include <linux/mount.h>
17#include <linux/exportfs.h>
Miklos Szeredibbb1e542016-12-16 11:02:56 +010018#include "overlayfs.h"
Miklos Szeredibbb1e542016-12-16 11:02:56 +010019
Miklos Szeredie28edc42016-12-16 11:02:56 +010020struct ovl_lookup_data {
21 struct qstr name;
22 bool is_dir;
23 bool opaque;
24 bool stop;
25 bool last;
Miklos Szeredi02b69b22016-12-16 11:02:56 +010026 char *redirect;
Vivek Goyal9d3dfea2018-05-11 11:49:28 -040027 bool metacopy;
Miklos Szeredie28edc42016-12-16 11:02:56 +010028};
Miklos Szeredibbb1e542016-12-16 11:02:56 +010029
Miklos Szeredi02b69b22016-12-16 11:02:56 +010030static int ovl_check_redirect(struct dentry *dentry, struct ovl_lookup_data *d,
31 size_t prelen, const char *post)
32{
33 int res;
34 char *s, *next, *buf = NULL;
35
36 res = vfs_getxattr(dentry, OVL_XATTR_REDIRECT, NULL, 0);
37 if (res < 0) {
38 if (res == -ENODATA || res == -EOPNOTSUPP)
39 return 0;
40 goto fail;
41 }
Michal Hocko0ee931c2017-09-13 16:28:29 -070042 buf = kzalloc(prelen + res + strlen(post) + 1, GFP_KERNEL);
Miklos Szeredi02b69b22016-12-16 11:02:56 +010043 if (!buf)
44 return -ENOMEM;
45
46 if (res == 0)
47 goto invalid;
48
49 res = vfs_getxattr(dentry, OVL_XATTR_REDIRECT, buf, res);
50 if (res < 0)
51 goto fail;
52 if (res == 0)
53 goto invalid;
54 if (buf[0] == '/') {
55 for (s = buf; *s++ == '/'; s = next) {
56 next = strchrnul(s, '/');
57 if (s == next)
58 goto invalid;
59 }
Amir Goldstein3ec9b3f2018-03-12 10:30:41 -040060 /*
61 * One of the ancestor path elements in an absolute path
62 * lookup in ovl_lookup_layer() could have been opaque and
63 * that will stop further lookup in lower layers (d->stop=true)
64 * But we have found an absolute redirect in decendant path
65 * element and that should force continue lookup in lower
66 * layers (reset d->stop).
67 */
68 d->stop = false;
Miklos Szeredi02b69b22016-12-16 11:02:56 +010069 } else {
70 if (strchr(buf, '/') != NULL)
71 goto invalid;
72
73 memmove(buf + prelen, buf, res);
74 memcpy(buf, d->name.name, prelen);
75 }
76
77 strcat(buf, post);
78 kfree(d->redirect);
79 d->redirect = buf;
80 d->name.name = d->redirect;
81 d->name.len = strlen(d->redirect);
82
83 return 0;
84
85err_free:
86 kfree(buf);
87 return 0;
88fail:
89 pr_warn_ratelimited("overlayfs: failed to get redirect (%i)\n", res);
90 goto err_free;
91invalid:
92 pr_warn_ratelimited("overlayfs: invalid redirect (%s)\n", buf);
93 goto err_free;
94}
95
Amir Goldsteina9d01952017-04-30 14:46:31 +030096static int ovl_acceptable(void *ctx, struct dentry *dentry)
97{
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +020098 /*
99 * A non-dir origin may be disconnected, which is fine, because
100 * we only need it for its unique inode number.
101 */
102 if (!d_is_dir(dentry))
103 return 1;
104
105 /* Don't decode a deleted empty directory */
106 if (d_unhashed(dentry))
107 return 0;
108
109 /* Check if directory belongs to the layer we are decoding from */
110 return is_subdir(dentry, ((struct vfsmount *)ctx)->mnt_root);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300111}
112
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300113/*
114 * Check validity of an overlay file handle buffer.
115 *
116 * Return 0 for a valid file handle.
117 * Return -ENODATA for "origin unknown".
118 * Return <0 for an invalid file handle.
119 */
Amir Goldstein8556a422018-01-19 01:03:23 +0200120int ovl_check_fh_len(struct ovl_fh *fh, int fh_len)
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300121{
122 if (fh_len < sizeof(struct ovl_fh) || fh_len < fh->len)
123 return -EINVAL;
124
125 if (fh->magic != OVL_FH_MAGIC)
126 return -EINVAL;
127
128 /* Treat larger version and unknown flags as "origin unknown" */
129 if (fh->version > OVL_FH_VERSION || fh->flags & ~OVL_FH_FLAG_ALL)
130 return -ENODATA;
131
132 /* Treat endianness mismatch as "origin unknown" */
133 if (!(fh->flags & OVL_FH_FLAG_ANY_ENDIAN) &&
134 (fh->flags & OVL_FH_FLAG_BIG_ENDIAN) != OVL_FH_FLAG_CPU_ENDIAN)
135 return -ENODATA;
136
137 return 0;
138}
139
Amir Goldstein05122442018-01-11 08:25:32 +0200140static struct ovl_fh *ovl_get_fh(struct dentry *dentry, const char *name)
Amir Goldsteina9d01952017-04-30 14:46:31 +0300141{
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300142 int res, err;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300143 struct ovl_fh *fh = NULL;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300144
Amir Goldstein05122442018-01-11 08:25:32 +0200145 res = vfs_getxattr(dentry, name, NULL, 0);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300146 if (res < 0) {
147 if (res == -ENODATA || res == -EOPNOTSUPP)
148 return NULL;
149 goto fail;
150 }
151 /* Zero size value means "copied up but origin unknown" */
152 if (res == 0)
153 return NULL;
154
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300155 fh = kzalloc(res, GFP_KERNEL);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300156 if (!fh)
157 return ERR_PTR(-ENOMEM);
158
Amir Goldstein05122442018-01-11 08:25:32 +0200159 res = vfs_getxattr(dentry, name, fh, res);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300160 if (res < 0)
161 goto fail;
162
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300163 err = ovl_check_fh_len(fh, res);
164 if (err < 0) {
165 if (err == -ENODATA)
166 goto out;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300167 goto invalid;
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300168 }
Amir Goldsteina9d01952017-04-30 14:46:31 +0300169
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300170 return fh;
171
172out:
173 kfree(fh);
174 return NULL;
175
176fail:
177 pr_warn_ratelimited("overlayfs: failed to get origin (%i)\n", res);
178 goto out;
179invalid:
180 pr_warn_ratelimited("overlayfs: invalid origin (%*phN)\n", res, fh);
181 goto out;
182}
183
Amir Goldstein8a22efa2018-03-09 15:51:02 +0200184struct dentry *ovl_decode_real_fh(struct ovl_fh *fh, struct vfsmount *mnt,
185 bool connected)
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300186{
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200187 struct dentry *real;
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300188 int bytes;
189
Amir Goldsteina9d01952017-04-30 14:46:31 +0300190 /*
191 * Make sure that the stored uuid matches the uuid of the lower
192 * layer where file handle will be decoded.
193 */
Christoph Hellwig85787092017-05-10 15:06:33 +0200194 if (!uuid_equal(&fh->uuid, &mnt->mnt_sb->s_uuid))
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300195 return NULL;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300196
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300197 bytes = (fh->len - offsetof(struct ovl_fh, fid));
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200198 real = exportfs_decode_fh(mnt, (struct fid *)fh->fid,
199 bytes >> 2, (int)fh->type,
Amir Goldstein8a22efa2018-03-09 15:51:02 +0200200 connected ? ovl_acceptable : NULL, mnt);
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200201 if (IS_ERR(real)) {
202 /*
203 * Treat stale file handle to lower file as "origin unknown".
204 * upper file handle could become stale when upper file is
205 * unlinked and this information is needed to handle stale
206 * index entries correctly.
207 */
208 if (real == ERR_PTR(-ESTALE) &&
209 !(fh->flags & OVL_FH_FLAG_PATH_UPPER))
210 real = NULL;
211 return real;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300212 }
213
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200214 if (ovl_dentry_weird(real)) {
215 dput(real);
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300216 return NULL;
217 }
Amir Goldsteina9d01952017-04-30 14:46:31 +0300218
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200219 return real;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300220}
221
Amir Goldsteinee1d6d372017-05-11 16:42:26 +0300222static bool ovl_is_opaquedir(struct dentry *dentry)
223{
224 return ovl_check_dir_xattr(dentry, OVL_XATTR_OPAQUE);
225}
226
Miklos Szeredie28edc42016-12-16 11:02:56 +0100227static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d,
228 const char *name, unsigned int namelen,
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100229 size_t prelen, const char *post,
Miklos Szeredie28edc42016-12-16 11:02:56 +0100230 struct dentry **ret)
231{
232 struct dentry *this;
233 int err;
Vivek Goyal102b0d12018-03-09 15:44:43 -0500234 bool last_element = !post[0];
Miklos Szeredie28edc42016-12-16 11:02:56 +0100235
236 this = lookup_one_len_unlocked(name, base, namelen);
237 if (IS_ERR(this)) {
238 err = PTR_ERR(this);
239 this = NULL;
240 if (err == -ENOENT || err == -ENAMETOOLONG)
241 goto out;
242 goto out_err;
243 }
244 if (!this->d_inode)
245 goto put_and_out;
246
247 if (ovl_dentry_weird(this)) {
248 /* Don't support traversing automounts and other weirdness */
249 err = -EREMOTE;
250 goto out_err;
251 }
252 if (ovl_is_whiteout(this)) {
253 d->stop = d->opaque = true;
254 goto put_and_out;
255 }
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400256 /*
257 * This dentry should be a regular file if previous layer lookup
258 * found a metacopy dentry.
259 */
260 if (last_element && d->metacopy && !d_is_reg(this)) {
Miklos Szeredie28edc42016-12-16 11:02:56 +0100261 d->stop = true;
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400262 goto put_and_out;
263 }
264 if (!d_can_lookup(this)) {
265 if (d->is_dir || !last_element) {
266 d->stop = true;
Miklos Szeredie28edc42016-12-16 11:02:56 +0100267 goto put_and_out;
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400268 }
269 err = ovl_check_metacopy_xattr(this);
270 if (err < 0)
271 goto out_err;
Miklos Szeredi3a291772018-04-12 12:04:49 +0200272
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400273 d->metacopy = err;
274 d->stop = !d->metacopy;
Miklos Szeredie28edc42016-12-16 11:02:56 +0100275 goto out;
276 }
Vivek Goyal102b0d12018-03-09 15:44:43 -0500277 if (last_element)
278 d->is_dir = true;
Vivek Goyale9b77f92018-03-09 15:44:42 -0500279 if (d->last)
280 goto out;
281
282 if (ovl_is_opaquedir(this)) {
Vivek Goyal102b0d12018-03-09 15:44:43 -0500283 d->stop = true;
284 if (last_element)
285 d->opaque = true;
Miklos Szeredie28edc42016-12-16 11:02:56 +0100286 goto out;
287 }
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100288 err = ovl_check_redirect(this, d, prelen, post);
289 if (err)
290 goto out_err;
Miklos Szeredie28edc42016-12-16 11:02:56 +0100291out:
292 *ret = this;
293 return 0;
294
295put_and_out:
296 dput(this);
297 this = NULL;
298 goto out;
299
300out_err:
301 dput(this);
302 return err;
303}
304
305static int ovl_lookup_layer(struct dentry *base, struct ovl_lookup_data *d,
306 struct dentry **ret)
307{
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100308 /* Counting down from the end, since the prefix can change */
309 size_t rem = d->name.len - 1;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100310 struct dentry *dentry = NULL;
311 int err;
312
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100313 if (d->name.name[0] != '/')
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100314 return ovl_lookup_single(base, d, d->name.name, d->name.len,
315 0, "", ret);
316
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100317 while (!IS_ERR_OR_NULL(base) && d_can_lookup(base)) {
318 const char *s = d->name.name + d->name.len - rem;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100319 const char *next = strchrnul(s, '/');
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100320 size_t thislen = next - s;
321 bool end = !next[0];
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100322
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100323 /* Verify we did not go off the rails */
324 if (WARN_ON(s[-1] != '/'))
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100325 return -EIO;
326
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100327 err = ovl_lookup_single(base, d, s, thislen,
328 d->name.len - rem, next, &base);
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100329 dput(dentry);
330 if (err)
331 return err;
332 dentry = base;
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100333 if (end)
334 break;
335
336 rem -= thislen + 1;
337
338 if (WARN_ON(rem >= d->name.len))
339 return -EIO;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100340 }
341 *ret = dentry;
342 return 0;
Miklos Szeredie28edc42016-12-16 11:02:56 +0100343}
344
Amir Goldsteina9d01952017-04-30 14:46:31 +0300345
Amir Goldstein8a22efa2018-03-09 15:51:02 +0200346int ovl_check_origin_fh(struct ovl_fs *ofs, struct ovl_fh *fh, bool connected,
Amir Goldsteinf9418662018-01-19 21:33:44 +0200347 struct dentry *upperdentry, struct ovl_path **stackp)
Amir Goldsteina9d01952017-04-30 14:46:31 +0300348{
Amir Goldsteinf7d3dac2017-06-21 15:28:34 +0300349 struct dentry *origin = NULL;
350 int i;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300351
Amir Goldstein1eff1a12017-12-12 22:40:46 +0200352 for (i = 0; i < ofs->numlower; i++) {
Amir Goldstein8a22efa2018-03-09 15:51:02 +0200353 origin = ovl_decode_real_fh(fh, ofs->lower_layers[i].mnt,
354 connected);
Amir Goldsteinf7d3dac2017-06-21 15:28:34 +0300355 if (origin)
356 break;
357 }
358
359 if (!origin)
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300360 return -ESTALE;
361 else if (IS_ERR(origin))
362 return PTR_ERR(origin);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300363
Amir Goldsteinf9418662018-01-19 21:33:44 +0200364 if (upperdentry && !ovl_is_whiteout(upperdentry) &&
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300365 ((d_inode(origin)->i_mode ^ d_inode(upperdentry)->i_mode) & S_IFMT))
366 goto invalid;
367
Amir Goldstein415543d2017-06-21 15:28:42 +0300368 if (!*stackp)
Chandan Rajendrab9343632017-07-24 01:57:54 -0500369 *stackp = kmalloc(sizeof(struct ovl_path), GFP_KERNEL);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300370 if (!*stackp) {
371 dput(origin);
372 return -ENOMEM;
373 }
Amir Goldstein1eff1a12017-12-12 22:40:46 +0200374 **stackp = (struct ovl_path){
375 .dentry = origin,
376 .layer = &ofs->lower_layers[i]
377 };
Amir Goldsteina9d01952017-04-30 14:46:31 +0300378
379 return 0;
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300380
381invalid:
382 pr_warn_ratelimited("overlayfs: invalid origin (%pd2, ftype=%x, origin ftype=%x).\n",
383 upperdentry, d_inode(upperdentry)->i_mode & S_IFMT,
384 d_inode(origin)->i_mode & S_IFMT);
385 dput(origin);
386 return -EIO;
387}
388
Amir Goldstein1eff1a12017-12-12 22:40:46 +0200389static int ovl_check_origin(struct ovl_fs *ofs, struct dentry *upperdentry,
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300390 struct ovl_path **stackp, unsigned int *ctrp)
391{
Amir Goldstein05122442018-01-11 08:25:32 +0200392 struct ovl_fh *fh = ovl_get_fh(upperdentry, OVL_XATTR_ORIGIN);
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300393 int err;
394
395 if (IS_ERR_OR_NULL(fh))
396 return PTR_ERR(fh);
397
Amir Goldstein8a22efa2018-03-09 15:51:02 +0200398 err = ovl_check_origin_fh(ofs, fh, false, upperdentry, stackp);
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300399 kfree(fh);
400
401 if (err) {
402 if (err == -ESTALE)
403 return 0;
404 return err;
405 }
406
407 if (WARN_ON(*ctrp))
408 return -EIO;
409
410 *ctrp = 1;
411 return 0;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300412}
413
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100414/*
Amir Goldstein05122442018-01-11 08:25:32 +0200415 * Verify that @fh matches the file handle stored in xattr @name.
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300416 * Return 0 on match, -ESTALE on mismatch, < 0 on error.
417 */
Amir Goldstein05122442018-01-11 08:25:32 +0200418static int ovl_verify_fh(struct dentry *dentry, const char *name,
419 const struct ovl_fh *fh)
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300420{
Amir Goldstein05122442018-01-11 08:25:32 +0200421 struct ovl_fh *ofh = ovl_get_fh(dentry, name);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300422 int err = 0;
423
424 if (!ofh)
425 return -ENODATA;
426
427 if (IS_ERR(ofh))
428 return PTR_ERR(ofh);
429
430 if (fh->len != ofh->len || memcmp(fh, ofh, fh->len))
431 err = -ESTALE;
432
433 kfree(ofh);
434 return err;
435}
436
437/*
Amir Goldstein05122442018-01-11 08:25:32 +0200438 * Verify that @real dentry matches the file handle stored in xattr @name.
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300439 *
Amir Goldstein05122442018-01-11 08:25:32 +0200440 * If @set is true and there is no stored file handle, encode @real and store
441 * file handle in xattr @name.
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300442 *
Amir Goldstein05122442018-01-11 08:25:32 +0200443 * Return 0 on match, -ESTALE on mismatch, -ENODATA on no xattr, < 0 on error.
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300444 */
Amir Goldstein05122442018-01-11 08:25:32 +0200445int ovl_verify_set_fh(struct dentry *dentry, const char *name,
446 struct dentry *real, bool is_upper, bool set)
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300447{
448 struct inode *inode;
449 struct ovl_fh *fh;
450 int err;
451
Amir Goldstein5b2cccd2018-02-02 10:42:03 +0200452 fh = ovl_encode_real_fh(real, is_upper);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300453 err = PTR_ERR(fh);
454 if (IS_ERR(fh))
455 goto fail;
456
Amir Goldstein05122442018-01-11 08:25:32 +0200457 err = ovl_verify_fh(dentry, name, fh);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300458 if (set && err == -ENODATA)
Amir Goldstein05122442018-01-11 08:25:32 +0200459 err = ovl_do_setxattr(dentry, name, fh, fh->len, 0);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300460 if (err)
461 goto fail;
462
463out:
464 kfree(fh);
465 return err;
466
467fail:
Amir Goldstein05122442018-01-11 08:25:32 +0200468 inode = d_inode(real);
469 pr_warn_ratelimited("overlayfs: failed to verify %s (%pd2, ino=%lu, err=%i)\n",
470 is_upper ? "upper" : "origin", real,
471 inode ? inode->i_ino : 0, err);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300472 goto out;
473}
474
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200475/* Get upper dentry from index */
Amir Goldstein3b0bfc62017-12-24 18:42:16 +0200476struct dentry *ovl_index_upper(struct ovl_fs *ofs, struct dentry *index)
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200477{
478 struct ovl_fh *fh;
479 struct dentry *upper;
480
481 if (!d_is_dir(index))
482 return dget(index);
483
484 fh = ovl_get_fh(index, OVL_XATTR_UPPER);
485 if (IS_ERR_OR_NULL(fh))
486 return ERR_CAST(fh);
487
Amir Goldstein8a22efa2018-03-09 15:51:02 +0200488 upper = ovl_decode_real_fh(fh, ofs->upper_mnt, true);
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200489 kfree(fh);
490
491 if (IS_ERR_OR_NULL(upper))
492 return upper ?: ERR_PTR(-ESTALE);
493
494 if (!d_is_dir(upper)) {
495 pr_warn_ratelimited("overlayfs: invalid index upper (%pd2, upper=%pd2).\n",
496 index, upper);
497 dput(upper);
498 return ERR_PTR(-EIO);
499 }
500
501 return upper;
502}
503
Amir Goldstein9ee60ce2017-11-01 10:13:51 +0200504/* Is this a leftover from create/whiteout of directory index entry? */
505static bool ovl_is_temp_index(struct dentry *index)
506{
507 return index->d_name.name[0] == '#';
508}
509
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300510/*
Amir Goldstein415543d2017-06-21 15:28:42 +0300511 * Verify that an index entry name matches the origin file handle stored in
512 * OVL_XATTR_ORIGIN and that origin file handle can be decoded to lower path.
513 * Return 0 on match, -ESTALE on mismatch or stale origin, < 0 on error.
514 */
Amir Goldstein1eff1a12017-12-12 22:40:46 +0200515int ovl_verify_index(struct ovl_fs *ofs, struct dentry *index)
Amir Goldstein415543d2017-06-21 15:28:42 +0300516{
517 struct ovl_fh *fh = NULL;
518 size_t len;
Chandan Rajendrab9343632017-07-24 01:57:54 -0500519 struct ovl_path origin = { };
520 struct ovl_path *stack = &origin;
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200521 struct dentry *upper = NULL;
Amir Goldstein415543d2017-06-21 15:28:42 +0300522 int err;
523
524 if (!d_inode(index))
525 return 0;
526
Amir Goldstein9ee60ce2017-11-01 10:13:51 +0200527 /* Cleanup leftover from index create/cleanup attempt */
528 err = -ESTALE;
529 if (ovl_is_temp_index(index))
530 goto fail;
531
Amir Goldsteinfa0096e2017-10-24 12:24:11 +0300532 err = -EINVAL;
Amir Goldstein415543d2017-06-21 15:28:42 +0300533 if (index->d_name.len < sizeof(struct ovl_fh)*2)
534 goto fail;
535
536 err = -ENOMEM;
537 len = index->d_name.len / 2;
Michal Hocko0ee931c2017-09-13 16:28:29 -0700538 fh = kzalloc(len, GFP_KERNEL);
Amir Goldstein415543d2017-06-21 15:28:42 +0300539 if (!fh)
540 goto fail;
541
542 err = -EINVAL;
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300543 if (hex2bin((u8 *)fh, index->d_name.name, len))
544 goto fail;
545
546 err = ovl_check_fh_len(fh, len);
547 if (err)
Amir Goldstein415543d2017-06-21 15:28:42 +0300548 goto fail;
549
Amir Goldstein7db25d32018-01-11 11:03:13 +0200550 /*
551 * Whiteout index entries are used as an indication that an exported
552 * overlay file handle should be treated as stale (i.e. after unlink
553 * of the overlay inode). These entries contain no origin xattr.
554 */
555 if (ovl_is_whiteout(index))
556 goto out;
557
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200558 /*
559 * Verifying directory index entries are not stale is expensive, so
560 * only verify stale dir index if NFS export is enabled.
561 */
562 if (d_is_dir(index) && !ofs->config.nfs_export)
563 goto out;
564
565 /*
566 * Directory index entries should have 'upper' xattr pointing to the
567 * real upper dir. Non-dir index entries are hardlinks to the upper
568 * real inode. For non-dir index, we can read the copy up origin xattr
569 * directly from the index dentry, but for dir index we first need to
570 * decode the upper directory.
571 */
572 upper = ovl_index_upper(ofs, index);
573 if (IS_ERR_OR_NULL(upper)) {
574 err = PTR_ERR(upper);
Amir Goldstein24f0b172018-01-11 15:33:51 +0200575 /*
576 * Directory index entries with no 'upper' xattr need to be
577 * removed. When dir index entry has a stale 'upper' xattr,
578 * we assume that upper dir was removed and we treat the dir
579 * index as orphan entry that needs to be whited out.
580 */
581 if (err == -ESTALE)
582 goto orphan;
583 else if (!err)
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200584 err = -ESTALE;
585 goto fail;
586 }
587
588 err = ovl_verify_fh(upper, OVL_XATTR_ORIGIN, fh);
589 dput(upper);
Amir Goldstein415543d2017-06-21 15:28:42 +0300590 if (err)
591 goto fail;
592
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200593 /* Check if non-dir index is orphan and don't warn before cleaning it */
594 if (!d_is_dir(index) && d_inode(index)->i_nlink == 1) {
Amir Goldstein8a22efa2018-03-09 15:51:02 +0200595 err = ovl_check_origin_fh(ofs, fh, false, index, &stack);
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200596 if (err)
597 goto fail;
Amir Goldstein415543d2017-06-21 15:28:42 +0300598
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200599 if (ovl_get_nlink(origin.dentry, index, 0) == 0)
Amir Goldstein24f0b172018-01-11 15:33:51 +0200600 goto orphan;
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200601 }
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300602
Amir Goldstein415543d2017-06-21 15:28:42 +0300603out:
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200604 dput(origin.dentry);
Amir Goldstein415543d2017-06-21 15:28:42 +0300605 kfree(fh);
606 return err;
607
608fail:
Amir Goldstein61b67472017-07-18 21:07:42 +0300609 pr_warn_ratelimited("overlayfs: failed to verify index (%pd2, ftype=%x, err=%i)\n",
610 index, d_inode(index)->i_mode & S_IFMT, err);
Amir Goldstein415543d2017-06-21 15:28:42 +0300611 goto out;
Amir Goldstein24f0b172018-01-11 15:33:51 +0200612
613orphan:
614 pr_warn_ratelimited("overlayfs: orphan index entry (%pd2, ftype=%x, nlink=%u)\n",
615 index, d_inode(index)->i_mode & S_IFMT,
616 d_inode(index)->i_nlink);
617 err = -ENOENT;
618 goto out;
Amir Goldstein415543d2017-06-21 15:28:42 +0300619}
620
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200621static int ovl_get_index_name_fh(struct ovl_fh *fh, struct qstr *name)
622{
623 char *n, *s;
624
Kees Cook6396bb22018-06-12 14:03:40 -0700625 n = kcalloc(fh->len, 2, GFP_KERNEL);
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200626 if (!n)
627 return -ENOMEM;
628
629 s = bin2hex(n, fh, fh->len);
630 *name = (struct qstr) QSTR_INIT(n, s - n);
631
632 return 0;
633
634}
635
Amir Goldstein415543d2017-06-21 15:28:42 +0300636/*
Amir Goldstein359f3922017-06-21 15:28:41 +0300637 * Lookup in indexdir for the index entry of a lower real inode or a copy up
638 * origin inode. The index entry name is the hex representation of the lower
639 * inode file handle.
640 *
641 * If the index dentry in negative, then either no lower aliases have been
642 * copied up yet, or aliases have been copied up in older kernels and are
643 * not indexed.
644 *
645 * If the index dentry for a copy up origin inode is positive, but points
646 * to an inode different than the upper inode, then either the upper inode
647 * has been copied up and not indexed or it was indexed, but since then
648 * index dir was cleared. Either way, that index cannot be used to indentify
649 * the overlay inode.
650 */
651int ovl_get_index_name(struct dentry *origin, struct qstr *name)
652{
Amir Goldstein359f3922017-06-21 15:28:41 +0300653 struct ovl_fh *fh;
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200654 int err;
Amir Goldstein359f3922017-06-21 15:28:41 +0300655
Amir Goldstein5b2cccd2018-02-02 10:42:03 +0200656 fh = ovl_encode_real_fh(origin, false);
Amir Goldstein359f3922017-06-21 15:28:41 +0300657 if (IS_ERR(fh))
658 return PTR_ERR(fh);
659
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200660 err = ovl_get_index_name_fh(fh, name);
661
Amir Goldstein359f3922017-06-21 15:28:41 +0300662 kfree(fh);
Amir Goldstein359f3922017-06-21 15:28:41 +0300663 return err;
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200664}
Amir Goldstein359f3922017-06-21 15:28:41 +0300665
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200666/* Lookup index by file handle for NFS export */
667struct dentry *ovl_get_index_fh(struct ovl_fs *ofs, struct ovl_fh *fh)
668{
669 struct dentry *index;
670 struct qstr name;
671 int err;
672
673 err = ovl_get_index_name_fh(fh, &name);
674 if (err)
675 return ERR_PTR(err);
676
677 index = lookup_one_len_unlocked(name.name, ofs->indexdir, name.len);
678 kfree(name.name);
679 if (IS_ERR(index)) {
680 if (PTR_ERR(index) == -ENOENT)
681 index = NULL;
682 return index;
683 }
684
685 if (d_is_negative(index))
686 err = 0;
687 else if (ovl_is_whiteout(index))
688 err = -ESTALE;
689 else if (ovl_dentry_weird(index))
690 err = -EIO;
691 else
692 return index;
693
694 dput(index);
695 return ERR_PTR(err);
Amir Goldstein359f3922017-06-21 15:28:41 +0300696}
697
Amir Goldstein06170152018-01-17 14:40:27 +0200698struct dentry *ovl_lookup_index(struct ovl_fs *ofs, struct dentry *upper,
699 struct dentry *origin, bool verify)
Amir Goldstein359f3922017-06-21 15:28:41 +0300700{
Amir Goldstein359f3922017-06-21 15:28:41 +0300701 struct dentry *index;
702 struct inode *inode;
703 struct qstr name;
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200704 bool is_dir = d_is_dir(origin);
Amir Goldstein359f3922017-06-21 15:28:41 +0300705 int err;
706
707 err = ovl_get_index_name(origin, &name);
708 if (err)
709 return ERR_PTR(err);
710
711 index = lookup_one_len_unlocked(name.name, ofs->indexdir, name.len);
712 if (IS_ERR(index)) {
Amir Goldsteine0082a02017-09-24 13:01:35 +0300713 err = PTR_ERR(index);
Amir Goldstein7937a562017-10-20 17:19:06 +0300714 if (err == -ENOENT) {
715 index = NULL;
716 goto out;
717 }
Amir Goldstein359f3922017-06-21 15:28:41 +0300718 pr_warn_ratelimited("overlayfs: failed inode index lookup (ino=%lu, key=%*s, err=%i);\n"
719 "overlayfs: mount with '-o index=off' to disable inodes index.\n",
720 d_inode(origin)->i_ino, name.len, name.name,
721 err);
722 goto out;
723 }
724
Amir Goldstein0e082552017-07-18 21:07:43 +0300725 inode = d_inode(index);
Amir Goldstein359f3922017-06-21 15:28:41 +0300726 if (d_is_negative(index)) {
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300727 goto out_dput;
Amir Goldstein06170152018-01-17 14:40:27 +0200728 } else if (ovl_is_whiteout(index) && !verify) {
729 /*
730 * When index lookup is called with !verify for decoding an
731 * overlay file handle, a whiteout index implies that decode
732 * should treat file handle as stale and no need to print a
733 * warning about it.
734 */
735 dput(index);
736 index = ERR_PTR(-ESTALE);
737 goto out;
Amir Goldstein0e082552017-07-18 21:07:43 +0300738 } else if (ovl_dentry_weird(index) || ovl_is_whiteout(index) ||
739 ((inode->i_mode ^ d_inode(origin)->i_mode) & S_IFMT)) {
740 /*
741 * Index should always be of the same file type as origin
742 * except for the case of a whiteout index. A whiteout
743 * index should only exist if all lower aliases have been
744 * unlinked, which means that finding a lower origin on lookup
745 * whose index is a whiteout should be treated as an error.
746 */
747 pr_warn_ratelimited("overlayfs: bad index found (index=%pd2, ftype=%x, origin ftype=%x).\n",
748 index, d_inode(index)->i_mode & S_IFMT,
749 d_inode(origin)->i_mode & S_IFMT);
Amir Goldstein359f3922017-06-21 15:28:41 +0300750 goto fail;
Amir Goldstein06170152018-01-17 14:40:27 +0200751 } else if (is_dir && verify) {
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200752 if (!upper) {
753 pr_warn_ratelimited("overlayfs: suspected uncovered redirected dir found (origin=%pd2, index=%pd2).\n",
754 origin, index);
755 goto fail;
756 }
Amir Goldstein359f3922017-06-21 15:28:41 +0300757
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200758 /* Verify that dir index 'upper' xattr points to upper dir */
759 err = ovl_verify_upper(index, upper, false);
760 if (err) {
761 if (err == -ESTALE) {
762 pr_warn_ratelimited("overlayfs: suspected multiply redirected dir found (upper=%pd2, origin=%pd2, index=%pd2).\n",
763 upper, origin, index);
764 }
765 goto fail;
766 }
767 } else if (upper && d_inode(upper) != inode) {
768 goto out_dput;
769 }
Amir Goldstein359f3922017-06-21 15:28:41 +0300770out:
771 kfree(name.name);
772 return index;
773
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300774out_dput:
775 dput(index);
776 index = NULL;
777 goto out;
778
Amir Goldstein359f3922017-06-21 15:28:41 +0300779fail:
780 dput(index);
781 index = ERR_PTR(-EIO);
782 goto out;
783}
784
785/*
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100786 * Returns next layer in stack starting from top.
787 * Returns -1 if this is the last layer.
788 */
789int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
790{
791 struct ovl_entry *oe = dentry->d_fsdata;
792
793 BUG_ON(idx < 0);
794 if (idx == 0) {
795 ovl_path_upper(dentry, path);
796 if (path->dentry)
797 return oe->numlower ? 1 : -1;
798 idx++;
799 }
800 BUG_ON(idx > oe->numlower);
Chandan Rajendrab9343632017-07-24 01:57:54 -0500801 path->dentry = oe->lowerstack[idx - 1].dentry;
802 path->mnt = oe->lowerstack[idx - 1].layer->mnt;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100803
804 return (idx < oe->numlower) ? idx + 1 : -1;
805}
806
Amir Goldstein9678e632018-01-03 19:34:45 +0200807/* Fix missing 'origin' xattr */
808static int ovl_fix_origin(struct dentry *dentry, struct dentry *lower,
809 struct dentry *upper)
810{
811 int err;
812
813 if (ovl_check_origin_xattr(upper))
814 return 0;
815
816 err = ovl_want_write(dentry);
817 if (err)
818 return err;
819
820 err = ovl_set_origin(dentry, lower, upper);
821 if (!err)
822 err = ovl_set_impure(dentry->d_parent, upper->d_parent);
823
824 ovl_drop_write(dentry);
825 return err;
826}
827
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100828struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
829 unsigned int flags)
830{
831 struct ovl_entry *oe;
832 const struct cred *old_cred;
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100833 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100834 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
Amir Goldsteinc22205d2017-04-26 23:40:52 +0300835 struct ovl_entry *roe = dentry->d_sb->s_root->d_fsdata;
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400836 struct ovl_path *stack = NULL, *origin_path = NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100837 struct dentry *upperdir, *upperdentry = NULL;
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200838 struct dentry *origin = NULL;
Amir Goldstein359f3922017-06-21 15:28:41 +0300839 struct dentry *index = NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100840 unsigned int ctr = 0;
841 struct inode *inode = NULL;
842 bool upperopaque = false;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100843 char *upperredirect = NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100844 struct dentry *this;
845 unsigned int i;
846 int err;
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400847 bool metacopy = false;
Miklos Szeredie28edc42016-12-16 11:02:56 +0100848 struct ovl_lookup_data d = {
849 .name = dentry->d_name,
850 .is_dir = false,
851 .opaque = false,
852 .stop = false,
Vivek Goyal452061f2018-03-09 15:44:41 -0500853 .last = ofs->config.redirect_follow ? false : !poe->numlower,
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100854 .redirect = NULL,
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400855 .metacopy = false,
Miklos Szeredie28edc42016-12-16 11:02:56 +0100856 };
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100857
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100858 if (dentry->d_name.len > ofs->namelen)
859 return ERR_PTR(-ENAMETOOLONG);
860
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100861 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200862 upperdir = ovl_dentry_upper(dentry->d_parent);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100863 if (upperdir) {
Miklos Szeredie28edc42016-12-16 11:02:56 +0100864 err = ovl_lookup_layer(upperdir, &d, &upperdentry);
865 if (err)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100866 goto out;
867
Miklos Szeredie28edc42016-12-16 11:02:56 +0100868 if (upperdentry && unlikely(ovl_dentry_remote(upperdentry))) {
869 dput(upperdentry);
870 err = -EREMOTE;
871 goto out;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100872 }
Amir Goldsteina9d01952017-04-30 14:46:31 +0300873 if (upperdentry && !d.is_dir) {
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400874 unsigned int origin_ctr = 0;
875
876 BUG_ON(d.redirect);
Amir Goldsteinf7d3dac2017-06-21 15:28:34 +0300877 /*
878 * Lookup copy up origin by decoding origin file handle.
879 * We may get a disconnected dentry, which is fine,
880 * because we only need to hold the origin inode in
881 * cache and use its inode number. We may even get a
882 * connected dentry, that is not under any of the lower
883 * layers root. That is also fine for using it's inode
884 * number - it's the same as if we held a reference
885 * to a dentry in lower layer that was moved under us.
886 */
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400887 err = ovl_check_origin(ofs, upperdentry, &origin_path,
888 &origin_ctr);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300889 if (err)
Vivek Goyal5455f922017-11-01 15:37:22 -0400890 goto out_put_upper;
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400891
892 if (d.metacopy)
893 metacopy = true;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300894 }
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100895
896 if (d.redirect) {
Dan Carpenter0ce5cdc2017-09-22 23:45:18 +0300897 err = -ENOMEM;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100898 upperredirect = kstrdup(d.redirect, GFP_KERNEL);
899 if (!upperredirect)
900 goto out_put_upper;
901 if (d.redirect[0] == '/')
Amir Goldsteinc22205d2017-04-26 23:40:52 +0300902 poe = roe;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100903 }
Miklos Szeredie28edc42016-12-16 11:02:56 +0100904 upperopaque = d.opaque;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100905 }
906
Miklos Szeredie28edc42016-12-16 11:02:56 +0100907 if (!d.stop && poe->numlower) {
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100908 err = -ENOMEM;
Chandan Rajendrab9343632017-07-24 01:57:54 -0500909 stack = kcalloc(ofs->numlower, sizeof(struct ovl_path),
Michal Hocko0ee931c2017-09-13 16:28:29 -0700910 GFP_KERNEL);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100911 if (!stack)
912 goto out_put_upper;
913 }
914
Miklos Szeredie28edc42016-12-16 11:02:56 +0100915 for (i = 0; !d.stop && i < poe->numlower; i++) {
Chandan Rajendrab9343632017-07-24 01:57:54 -0500916 struct ovl_path lower = poe->lowerstack[i];
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100917
Vivek Goyal452061f2018-03-09 15:44:41 -0500918 if (!ofs->config.redirect_follow)
919 d.last = i == poe->numlower - 1;
920 else
921 d.last = lower.layer->idx == roe->numlower;
922
Chandan Rajendrab9343632017-07-24 01:57:54 -0500923 err = ovl_lookup_layer(lower.dentry, &d, &this);
Miklos Szeredie28edc42016-12-16 11:02:56 +0100924 if (err)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100925 goto out_put;
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100926
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100927 if (!this)
928 continue;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100929
Amir Goldstein9678e632018-01-03 19:34:45 +0200930 /*
931 * If no origin fh is stored in upper of a merge dir, store fh
932 * of lower dir and set upper parent "impure".
933 */
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400934 if (upperdentry && !ctr && !ofs->noxattr && d.is_dir) {
Amir Goldstein9678e632018-01-03 19:34:45 +0200935 err = ovl_fix_origin(dentry, this, upperdentry);
936 if (err) {
937 dput(this);
938 goto out_put;
939 }
940 }
941
Amir Goldstein37b129162018-01-10 22:29:38 +0200942 /*
943 * When "verify_lower" feature is enabled, do not merge with a
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200944 * lower dir that does not match a stored origin xattr. In any
945 * case, only verified origin is used for index lookup.
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400946 *
947 * For non-dir dentry, if index=on, then ensure origin
948 * matches the dentry found using path based lookup,
949 * otherwise error out.
Amir Goldstein37b129162018-01-10 22:29:38 +0200950 */
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400951 if (upperdentry && !ctr &&
952 ((d.is_dir && ovl_verify_lower(dentry->d_sb)) ||
953 (!d.is_dir && ofs->config.index && origin_path))) {
Amir Goldstein37b129162018-01-10 22:29:38 +0200954 err = ovl_verify_origin(upperdentry, this, false);
955 if (err) {
956 dput(this);
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400957 if (d.is_dir)
958 break;
959 goto out_put;
Amir Goldstein37b129162018-01-10 22:29:38 +0200960 }
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200961 origin = this;
Amir Goldstein37b129162018-01-10 22:29:38 +0200962 }
963
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400964 if (d.metacopy)
965 metacopy = true;
966 /*
967 * Do not store intermediate metacopy dentries in chain,
968 * except top most lower metacopy dentry
969 */
970 if (d.metacopy && ctr) {
971 dput(this);
972 continue;
973 }
974
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100975 stack[ctr].dentry = this;
Chandan Rajendrab9343632017-07-24 01:57:54 -0500976 stack[ctr].layer = lower.layer;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100977 ctr++;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100978
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100979 /*
980 * Following redirects can have security consequences: it's like
981 * a symlink into the lower layer without the permission checks.
982 * This is only a problem if the upper layer is untrusted (e.g
983 * comes from an USB drive). This can allow a non-readable file
984 * or directory to become readable.
985 *
986 * Only following redirects when redirects are enabled disables
987 * this attack vector when not necessary.
988 */
989 err = -EPERM;
990 if (d.redirect && !ofs->config.redirect_follow) {
Amir Goldsteinf8167812017-12-18 14:25:56 +0200991 pr_warn_ratelimited("overlayfs: refusing to follow redirect for (%pd2)\n",
992 dentry);
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100993 goto out_put;
994 }
995
Vivek Goyald1fe96c2018-02-02 10:23:24 -0500996 if (d.stop)
997 break;
998
Amir Goldsteinc22205d2017-04-26 23:40:52 +0300999 if (d.redirect && d.redirect[0] == '/' && poe != roe) {
1000 poe = roe;
Miklos Szeredi02b69b22016-12-16 11:02:56 +01001001 /* Find the current layer on the root dentry */
Amir Goldsteind583ed72017-11-08 19:23:36 +02001002 i = lower.layer->idx - 1;
Miklos Szeredi02b69b22016-12-16 11:02:56 +01001003 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001004 }
1005
Vivek Goyal9d3dfea2018-05-11 11:49:28 -04001006 if (metacopy) {
1007 /*
1008 * Found a metacopy dentry but did not find corresponding
1009 * data dentry
1010 */
1011 if (d.metacopy) {
1012 err = -EIO;
1013 goto out_put;
1014 }
1015
1016 err = -EPERM;
1017 if (!ofs->config.metacopy) {
1018 pr_warn_ratelimited("overlay: refusing to follow metacopy origin for (%pd2)\n",
1019 dentry);
1020 goto out_put;
1021 }
1022 } else if (!d.is_dir && upperdentry && !ctr && origin_path) {
1023 if (WARN_ON(stack != NULL)) {
1024 err = -EIO;
1025 goto out_put;
1026 }
1027 stack = origin_path;
1028 ctr = 1;
1029 origin_path = NULL;
1030 }
1031
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001032 /*
1033 * Lookup index by lower inode and verify it matches upper inode.
1034 * We only trust dir index if we verified that lower dir matches
1035 * origin, otherwise dir index entries may be inconsistent and we
Vivek Goyal9d3dfea2018-05-11 11:49:28 -04001036 * ignore them.
1037 *
1038 * For non-dir upper metacopy dentry, we already set "origin" if we
1039 * verified that lower matched upper origin. If upper origin was
1040 * not present (because lower layer did not support fh encode/decode),
1041 * or indexing is not enabled, do not set "origin" and skip looking up
1042 * index. This case should be handled in same way as a non-dir upper
1043 * without ORIGIN is handled.
1044 *
1045 * Always lookup index of non-dir non-metacopy and non-upper.
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001046 */
Vivek Goyal9d3dfea2018-05-11 11:49:28 -04001047 if (ctr && (!upperdentry || (!d.is_dir && !metacopy)))
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001048 origin = stack[0].dentry;
Amir Goldstein359f3922017-06-21 15:28:41 +03001049
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001050 if (origin && ovl_indexdir(dentry->d_sb) &&
1051 (!d.is_dir || ovl_index_all(dentry->d_sb))) {
Amir Goldstein06170152018-01-17 14:40:27 +02001052 index = ovl_lookup_index(ofs, upperdentry, origin, true);
Amir Goldstein359f3922017-06-21 15:28:41 +03001053 if (IS_ERR(index)) {
1054 err = PTR_ERR(index);
1055 index = NULL;
1056 goto out_put;
1057 }
1058 }
1059
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001060 oe = ovl_alloc_entry(ctr);
1061 err = -ENOMEM;
1062 if (!oe)
1063 goto out_put;
1064
Chandan Rajendrab9343632017-07-24 01:57:54 -05001065 memcpy(oe->lowerstack, stack, sizeof(struct ovl_path) * ctr);
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +02001066 dentry->d_fsdata = oe;
1067
Amir Goldsteinc62520a2018-01-14 19:25:31 +02001068 if (upperopaque)
1069 ovl_dentry_set_opaque(dentry);
1070
Miklos Szeredi55acc662017-07-04 22:03:18 +02001071 if (upperdentry)
1072 ovl_dentry_set_upper_alias(dentry);
1073 else if (index)
Amir Goldstein359f3922017-06-21 15:28:41 +03001074 upperdentry = dget(index);
1075
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +02001076 if (upperdentry || ctr) {
Vivek Goyalac6a52e2018-05-08 09:27:21 -04001077 struct ovl_inode_params oip = {
1078 .upperdentry = upperdentry,
1079 .lowerpath = stack,
1080 .index = index,
1081 .numlower = ctr,
Vivek Goyal9cec54c2018-05-11 11:49:27 -04001082 .redirect = upperredirect,
Vivek Goyal2664bd02018-05-11 11:49:30 -04001083 .lowerdata = (ctr > 1 && !d.is_dir) ?
1084 stack[ctr - 1].dentry : NULL,
Vivek Goyalac6a52e2018-05-08 09:27:21 -04001085 };
1086
1087 inode = ovl_get_inode(dentry->d_sb, &oip);
Miklos Szeredib9ac5c272017-07-04 22:03:17 +02001088 err = PTR_ERR(inode);
1089 if (IS_ERR(inode))
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +02001090 goto out_free_oe;
1091 }
1092
1093 revert_creds(old_cred);
Vivek Goyal9d3dfea2018-05-11 11:49:28 -04001094 if (origin_path) {
1095 dput(origin_path->dentry);
1096 kfree(origin_path);
1097 }
Amir Goldstein359f3922017-06-21 15:28:41 +03001098 dput(index);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001099 kfree(stack);
Miklos Szeredi02b69b22016-12-16 11:02:56 +01001100 kfree(d.redirect);
Amir Goldstein829c28b2017-09-29 21:43:07 +03001101 return d_splice_alias(inode, dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001102
1103out_free_oe:
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +02001104 dentry->d_fsdata = NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001105 kfree(oe);
1106out_put:
Amir Goldstein359f3922017-06-21 15:28:41 +03001107 dput(index);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001108 for (i = 0; i < ctr; i++)
1109 dput(stack[i].dentry);
1110 kfree(stack);
1111out_put_upper:
Vivek Goyal9d3dfea2018-05-11 11:49:28 -04001112 if (origin_path) {
1113 dput(origin_path->dentry);
1114 kfree(origin_path);
1115 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001116 dput(upperdentry);
Miklos Szeredi02b69b22016-12-16 11:02:56 +01001117 kfree(upperredirect);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001118out:
Miklos Szeredi02b69b22016-12-16 11:02:56 +01001119 kfree(d.redirect);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001120 revert_creds(old_cred);
1121 return ERR_PTR(err);
1122}
1123
1124bool ovl_lower_positive(struct dentry *dentry)
1125{
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001126 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
1127 const struct qstr *name = &dentry->d_name;
Amir Goldstein6d0a8a92017-11-10 13:18:07 +02001128 const struct cred *old_cred;
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001129 unsigned int i;
1130 bool positive = false;
1131 bool done = false;
1132
1133 /*
1134 * If dentry is negative, then lower is positive iff this is a
1135 * whiteout.
1136 */
1137 if (!dentry->d_inode)
Amir Goldsteinc62520a2018-01-14 19:25:31 +02001138 return ovl_dentry_is_opaque(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001139
1140 /* Negative upper -> positive lower */
Miklos Szeredi09d8b582017-07-04 22:03:16 +02001141 if (!ovl_dentry_upper(dentry))
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001142 return true;
1143
Amir Goldstein6d0a8a92017-11-10 13:18:07 +02001144 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001145 /* Positive upper -> have to look up lower to see whether it exists */
1146 for (i = 0; !done && !positive && i < poe->numlower; i++) {
1147 struct dentry *this;
1148 struct dentry *lowerdir = poe->lowerstack[i].dentry;
1149
1150 this = lookup_one_len_unlocked(name->name, lowerdir,
1151 name->len);
1152 if (IS_ERR(this)) {
1153 switch (PTR_ERR(this)) {
1154 case -ENOENT:
1155 case -ENAMETOOLONG:
1156 break;
1157
1158 default:
1159 /*
1160 * Assume something is there, we just couldn't
1161 * access it.
1162 */
1163 positive = true;
1164 break;
1165 }
1166 } else {
1167 if (this->d_inode) {
1168 positive = !ovl_is_whiteout(this);
1169 done = true;
1170 }
1171 dput(this);
1172 }
1173 }
Amir Goldstein6d0a8a92017-11-10 13:18:07 +02001174 revert_creds(old_cred);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001175
1176 return positive;
1177}