blob: 9c0ca6a7becfbe56e15efd596fbc6540b4bbd859 [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;
Vivek Goyal0a2d0d32018-05-11 11:49:32 -040034 char *buf;
Miklos Szeredi02b69b22016-12-16 11:02:56 +010035
Vivek Goyal0a2d0d32018-05-11 11:49:32 -040036 buf = ovl_get_redirect_xattr(dentry, prelen + strlen(post));
37 if (IS_ERR_OR_NULL(buf))
38 return PTR_ERR(buf);
Miklos Szeredi02b69b22016-12-16 11:02:56 +010039
Miklos Szeredi02b69b22016-12-16 11:02:56 +010040 if (buf[0] == '/') {
Amir Goldstein3ec9b3f2018-03-12 10:30:41 -040041 /*
42 * One of the ancestor path elements in an absolute path
43 * lookup in ovl_lookup_layer() could have been opaque and
44 * that will stop further lookup in lower layers (d->stop=true)
45 * But we have found an absolute redirect in decendant path
46 * element and that should force continue lookup in lower
47 * layers (reset d->stop).
48 */
49 d->stop = false;
Miklos Szeredi02b69b22016-12-16 11:02:56 +010050 } else {
Vivek Goyal0a2d0d32018-05-11 11:49:32 -040051 res = strlen(buf) + 1;
Miklos Szeredi02b69b22016-12-16 11:02:56 +010052 memmove(buf + prelen, buf, res);
53 memcpy(buf, d->name.name, prelen);
54 }
55
56 strcat(buf, post);
57 kfree(d->redirect);
58 d->redirect = buf;
59 d->name.name = d->redirect;
60 d->name.len = strlen(d->redirect);
61
62 return 0;
Miklos Szeredi02b69b22016-12-16 11:02:56 +010063}
64
Amir Goldsteina9d01952017-04-30 14:46:31 +030065static int ovl_acceptable(void *ctx, struct dentry *dentry)
66{
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +020067 /*
68 * A non-dir origin may be disconnected, which is fine, because
69 * we only need it for its unique inode number.
70 */
71 if (!d_is_dir(dentry))
72 return 1;
73
74 /* Don't decode a deleted empty directory */
75 if (d_unhashed(dentry))
76 return 0;
77
78 /* Check if directory belongs to the layer we are decoding from */
79 return is_subdir(dentry, ((struct vfsmount *)ctx)->mnt_root);
Amir Goldsteina9d01952017-04-30 14:46:31 +030080}
81
Amir Goldstein2e1a53282017-10-24 15:12:15 +030082/*
83 * Check validity of an overlay file handle buffer.
84 *
85 * Return 0 for a valid file handle.
86 * Return -ENODATA for "origin unknown".
87 * Return <0 for an invalid file handle.
88 */
Amir Goldstein8556a422018-01-19 01:03:23 +020089int ovl_check_fh_len(struct ovl_fh *fh, int fh_len)
Amir Goldstein2e1a53282017-10-24 15:12:15 +030090{
91 if (fh_len < sizeof(struct ovl_fh) || fh_len < fh->len)
92 return -EINVAL;
93
94 if (fh->magic != OVL_FH_MAGIC)
95 return -EINVAL;
96
97 /* Treat larger version and unknown flags as "origin unknown" */
98 if (fh->version > OVL_FH_VERSION || fh->flags & ~OVL_FH_FLAG_ALL)
99 return -ENODATA;
100
101 /* Treat endianness mismatch as "origin unknown" */
102 if (!(fh->flags & OVL_FH_FLAG_ANY_ENDIAN) &&
103 (fh->flags & OVL_FH_FLAG_BIG_ENDIAN) != OVL_FH_FLAG_CPU_ENDIAN)
104 return -ENODATA;
105
106 return 0;
107}
108
Amir Goldstein05122442018-01-11 08:25:32 +0200109static struct ovl_fh *ovl_get_fh(struct dentry *dentry, const char *name)
Amir Goldsteina9d01952017-04-30 14:46:31 +0300110{
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300111 int res, err;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300112 struct ovl_fh *fh = NULL;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300113
Amir Goldstein05122442018-01-11 08:25:32 +0200114 res = vfs_getxattr(dentry, name, NULL, 0);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300115 if (res < 0) {
116 if (res == -ENODATA || res == -EOPNOTSUPP)
117 return NULL;
118 goto fail;
119 }
120 /* Zero size value means "copied up but origin unknown" */
121 if (res == 0)
122 return NULL;
123
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300124 fh = kzalloc(res, GFP_KERNEL);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300125 if (!fh)
126 return ERR_PTR(-ENOMEM);
127
Amir Goldstein05122442018-01-11 08:25:32 +0200128 res = vfs_getxattr(dentry, name, fh, res);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300129 if (res < 0)
130 goto fail;
131
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300132 err = ovl_check_fh_len(fh, res);
133 if (err < 0) {
134 if (err == -ENODATA)
135 goto out;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300136 goto invalid;
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300137 }
Amir Goldsteina9d01952017-04-30 14:46:31 +0300138
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300139 return fh;
140
141out:
142 kfree(fh);
143 return NULL;
144
145fail:
146 pr_warn_ratelimited("overlayfs: failed to get origin (%i)\n", res);
147 goto out;
148invalid:
149 pr_warn_ratelimited("overlayfs: invalid origin (%*phN)\n", res, fh);
150 goto out;
151}
152
Amir Goldstein8a22efa2018-03-09 15:51:02 +0200153struct dentry *ovl_decode_real_fh(struct ovl_fh *fh, struct vfsmount *mnt,
154 bool connected)
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300155{
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200156 struct dentry *real;
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300157 int bytes;
158
Amir Goldsteina9d01952017-04-30 14:46:31 +0300159 /*
160 * Make sure that the stored uuid matches the uuid of the lower
161 * layer where file handle will be decoded.
162 */
Christoph Hellwig85787092017-05-10 15:06:33 +0200163 if (!uuid_equal(&fh->uuid, &mnt->mnt_sb->s_uuid))
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300164 return NULL;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300165
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300166 bytes = (fh->len - offsetof(struct ovl_fh, fid));
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200167 real = exportfs_decode_fh(mnt, (struct fid *)fh->fid,
168 bytes >> 2, (int)fh->type,
Amir Goldstein8a22efa2018-03-09 15:51:02 +0200169 connected ? ovl_acceptable : NULL, mnt);
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200170 if (IS_ERR(real)) {
171 /*
172 * Treat stale file handle to lower file as "origin unknown".
173 * upper file handle could become stale when upper file is
174 * unlinked and this information is needed to handle stale
175 * index entries correctly.
176 */
177 if (real == ERR_PTR(-ESTALE) &&
178 !(fh->flags & OVL_FH_FLAG_PATH_UPPER))
179 real = NULL;
180 return real;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300181 }
182
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200183 if (ovl_dentry_weird(real)) {
184 dput(real);
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300185 return NULL;
186 }
Amir Goldsteina9d01952017-04-30 14:46:31 +0300187
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200188 return real;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300189}
190
Amir Goldsteinee1d6d372017-05-11 16:42:26 +0300191static bool ovl_is_opaquedir(struct dentry *dentry)
192{
193 return ovl_check_dir_xattr(dentry, OVL_XATTR_OPAQUE);
194}
195
Miklos Szeredie28edc42016-12-16 11:02:56 +0100196static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d,
197 const char *name, unsigned int namelen,
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100198 size_t prelen, const char *post,
Miklos Szeredie28edc42016-12-16 11:02:56 +0100199 struct dentry **ret)
200{
201 struct dentry *this;
202 int err;
Vivek Goyal102b0d12018-03-09 15:44:43 -0500203 bool last_element = !post[0];
Miklos Szeredie28edc42016-12-16 11:02:56 +0100204
205 this = lookup_one_len_unlocked(name, base, namelen);
206 if (IS_ERR(this)) {
207 err = PTR_ERR(this);
208 this = NULL;
209 if (err == -ENOENT || err == -ENAMETOOLONG)
210 goto out;
211 goto out_err;
212 }
213 if (!this->d_inode)
214 goto put_and_out;
215
216 if (ovl_dentry_weird(this)) {
217 /* Don't support traversing automounts and other weirdness */
218 err = -EREMOTE;
219 goto out_err;
220 }
221 if (ovl_is_whiteout(this)) {
222 d->stop = d->opaque = true;
223 goto put_and_out;
224 }
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400225 /*
226 * This dentry should be a regular file if previous layer lookup
227 * found a metacopy dentry.
228 */
229 if (last_element && d->metacopy && !d_is_reg(this)) {
Miklos Szeredie28edc42016-12-16 11:02:56 +0100230 d->stop = true;
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400231 goto put_and_out;
232 }
233 if (!d_can_lookup(this)) {
234 if (d->is_dir || !last_element) {
235 d->stop = true;
Miklos Szeredie28edc42016-12-16 11:02:56 +0100236 goto put_and_out;
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400237 }
238 err = ovl_check_metacopy_xattr(this);
239 if (err < 0)
240 goto out_err;
Miklos Szeredi3a291772018-04-12 12:04:49 +0200241
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400242 d->metacopy = err;
243 d->stop = !d->metacopy;
Vivek Goyalb8a88242018-05-11 11:49:31 -0400244 if (!d->metacopy || d->last)
245 goto out;
Vivek Goyal0618a812018-05-11 11:49:31 -0400246 } else {
Vivek Goyal102b0d12018-03-09 15:44:43 -0500247 if (last_element)
Vivek Goyal0618a812018-05-11 11:49:31 -0400248 d->is_dir = true;
249 if (d->last)
250 goto out;
251
252 if (ovl_is_opaquedir(this)) {
253 d->stop = true;
254 if (last_element)
255 d->opaque = true;
256 goto out;
257 }
Miklos Szeredie28edc42016-12-16 11:02:56 +0100258 }
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100259 err = ovl_check_redirect(this, d, prelen, post);
260 if (err)
261 goto out_err;
Miklos Szeredie28edc42016-12-16 11:02:56 +0100262out:
263 *ret = this;
264 return 0;
265
266put_and_out:
267 dput(this);
268 this = NULL;
269 goto out;
270
271out_err:
272 dput(this);
273 return err;
274}
275
276static int ovl_lookup_layer(struct dentry *base, struct ovl_lookup_data *d,
277 struct dentry **ret)
278{
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100279 /* Counting down from the end, since the prefix can change */
280 size_t rem = d->name.len - 1;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100281 struct dentry *dentry = NULL;
282 int err;
283
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100284 if (d->name.name[0] != '/')
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100285 return ovl_lookup_single(base, d, d->name.name, d->name.len,
286 0, "", ret);
287
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100288 while (!IS_ERR_OR_NULL(base) && d_can_lookup(base)) {
289 const char *s = d->name.name + d->name.len - rem;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100290 const char *next = strchrnul(s, '/');
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100291 size_t thislen = next - s;
292 bool end = !next[0];
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100293
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100294 /* Verify we did not go off the rails */
295 if (WARN_ON(s[-1] != '/'))
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100296 return -EIO;
297
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100298 err = ovl_lookup_single(base, d, s, thislen,
299 d->name.len - rem, next, &base);
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100300 dput(dentry);
301 if (err)
302 return err;
303 dentry = base;
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100304 if (end)
305 break;
306
307 rem -= thislen + 1;
308
309 if (WARN_ON(rem >= d->name.len))
310 return -EIO;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100311 }
312 *ret = dentry;
313 return 0;
Miklos Szeredie28edc42016-12-16 11:02:56 +0100314}
315
Amir Goldsteina9d01952017-04-30 14:46:31 +0300316
Amir Goldstein8a22efa2018-03-09 15:51:02 +0200317int ovl_check_origin_fh(struct ovl_fs *ofs, struct ovl_fh *fh, bool connected,
Amir Goldsteinf9418662018-01-19 21:33:44 +0200318 struct dentry *upperdentry, struct ovl_path **stackp)
Amir Goldsteina9d01952017-04-30 14:46:31 +0300319{
Amir Goldsteinf7d3dac2017-06-21 15:28:34 +0300320 struct dentry *origin = NULL;
321 int i;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300322
Amir Goldstein1eff1a12017-12-12 22:40:46 +0200323 for (i = 0; i < ofs->numlower; i++) {
Amir Goldstein8a22efa2018-03-09 15:51:02 +0200324 origin = ovl_decode_real_fh(fh, ofs->lower_layers[i].mnt,
325 connected);
Amir Goldsteinf7d3dac2017-06-21 15:28:34 +0300326 if (origin)
327 break;
328 }
329
330 if (!origin)
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300331 return -ESTALE;
332 else if (IS_ERR(origin))
333 return PTR_ERR(origin);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300334
Amir Goldsteinf9418662018-01-19 21:33:44 +0200335 if (upperdentry && !ovl_is_whiteout(upperdentry) &&
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300336 ((d_inode(origin)->i_mode ^ d_inode(upperdentry)->i_mode) & S_IFMT))
337 goto invalid;
338
Amir Goldstein415543d2017-06-21 15:28:42 +0300339 if (!*stackp)
Chandan Rajendrab9343632017-07-24 01:57:54 -0500340 *stackp = kmalloc(sizeof(struct ovl_path), GFP_KERNEL);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300341 if (!*stackp) {
342 dput(origin);
343 return -ENOMEM;
344 }
Amir Goldstein1eff1a12017-12-12 22:40:46 +0200345 **stackp = (struct ovl_path){
346 .dentry = origin,
347 .layer = &ofs->lower_layers[i]
348 };
Amir Goldsteina9d01952017-04-30 14:46:31 +0300349
350 return 0;
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300351
352invalid:
353 pr_warn_ratelimited("overlayfs: invalid origin (%pd2, ftype=%x, origin ftype=%x).\n",
354 upperdentry, d_inode(upperdentry)->i_mode & S_IFMT,
355 d_inode(origin)->i_mode & S_IFMT);
356 dput(origin);
357 return -EIO;
358}
359
Amir Goldstein1eff1a12017-12-12 22:40:46 +0200360static int ovl_check_origin(struct ovl_fs *ofs, struct dentry *upperdentry,
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300361 struct ovl_path **stackp, unsigned int *ctrp)
362{
Amir Goldstein05122442018-01-11 08:25:32 +0200363 struct ovl_fh *fh = ovl_get_fh(upperdentry, OVL_XATTR_ORIGIN);
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300364 int err;
365
366 if (IS_ERR_OR_NULL(fh))
367 return PTR_ERR(fh);
368
Amir Goldstein8a22efa2018-03-09 15:51:02 +0200369 err = ovl_check_origin_fh(ofs, fh, false, upperdentry, stackp);
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300370 kfree(fh);
371
372 if (err) {
373 if (err == -ESTALE)
374 return 0;
375 return err;
376 }
377
378 if (WARN_ON(*ctrp))
379 return -EIO;
380
381 *ctrp = 1;
382 return 0;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300383}
384
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100385/*
Amir Goldstein05122442018-01-11 08:25:32 +0200386 * Verify that @fh matches the file handle stored in xattr @name.
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300387 * Return 0 on match, -ESTALE on mismatch, < 0 on error.
388 */
Amir Goldstein05122442018-01-11 08:25:32 +0200389static int ovl_verify_fh(struct dentry *dentry, const char *name,
390 const struct ovl_fh *fh)
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300391{
Amir Goldstein05122442018-01-11 08:25:32 +0200392 struct ovl_fh *ofh = ovl_get_fh(dentry, name);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300393 int err = 0;
394
395 if (!ofh)
396 return -ENODATA;
397
398 if (IS_ERR(ofh))
399 return PTR_ERR(ofh);
400
401 if (fh->len != ofh->len || memcmp(fh, ofh, fh->len))
402 err = -ESTALE;
403
404 kfree(ofh);
405 return err;
406}
407
408/*
Amir Goldstein05122442018-01-11 08:25:32 +0200409 * Verify that @real dentry matches the file handle stored in xattr @name.
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300410 *
Amir Goldstein05122442018-01-11 08:25:32 +0200411 * If @set is true and there is no stored file handle, encode @real and store
412 * file handle in xattr @name.
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300413 *
Amir Goldstein05122442018-01-11 08:25:32 +0200414 * Return 0 on match, -ESTALE on mismatch, -ENODATA on no xattr, < 0 on error.
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300415 */
Amir Goldstein05122442018-01-11 08:25:32 +0200416int ovl_verify_set_fh(struct dentry *dentry, const char *name,
417 struct dentry *real, bool is_upper, bool set)
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300418{
419 struct inode *inode;
420 struct ovl_fh *fh;
421 int err;
422
Amir Goldstein5b2cccd2018-02-02 10:42:03 +0200423 fh = ovl_encode_real_fh(real, is_upper);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300424 err = PTR_ERR(fh);
425 if (IS_ERR(fh))
426 goto fail;
427
Amir Goldstein05122442018-01-11 08:25:32 +0200428 err = ovl_verify_fh(dentry, name, fh);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300429 if (set && err == -ENODATA)
Amir Goldstein05122442018-01-11 08:25:32 +0200430 err = ovl_do_setxattr(dentry, name, fh, fh->len, 0);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300431 if (err)
432 goto fail;
433
434out:
435 kfree(fh);
436 return err;
437
438fail:
Amir Goldstein05122442018-01-11 08:25:32 +0200439 inode = d_inode(real);
440 pr_warn_ratelimited("overlayfs: failed to verify %s (%pd2, ino=%lu, err=%i)\n",
441 is_upper ? "upper" : "origin", real,
442 inode ? inode->i_ino : 0, err);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300443 goto out;
444}
445
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200446/* Get upper dentry from index */
Amir Goldstein3b0bfc62017-12-24 18:42:16 +0200447struct dentry *ovl_index_upper(struct ovl_fs *ofs, struct dentry *index)
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200448{
449 struct ovl_fh *fh;
450 struct dentry *upper;
451
452 if (!d_is_dir(index))
453 return dget(index);
454
455 fh = ovl_get_fh(index, OVL_XATTR_UPPER);
456 if (IS_ERR_OR_NULL(fh))
457 return ERR_CAST(fh);
458
Amir Goldstein8a22efa2018-03-09 15:51:02 +0200459 upper = ovl_decode_real_fh(fh, ofs->upper_mnt, true);
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200460 kfree(fh);
461
462 if (IS_ERR_OR_NULL(upper))
463 return upper ?: ERR_PTR(-ESTALE);
464
465 if (!d_is_dir(upper)) {
466 pr_warn_ratelimited("overlayfs: invalid index upper (%pd2, upper=%pd2).\n",
467 index, upper);
468 dput(upper);
469 return ERR_PTR(-EIO);
470 }
471
472 return upper;
473}
474
Amir Goldstein9ee60ce2017-11-01 10:13:51 +0200475/* Is this a leftover from create/whiteout of directory index entry? */
476static bool ovl_is_temp_index(struct dentry *index)
477{
478 return index->d_name.name[0] == '#';
479}
480
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300481/*
Amir Goldstein415543d2017-06-21 15:28:42 +0300482 * Verify that an index entry name matches the origin file handle stored in
483 * OVL_XATTR_ORIGIN and that origin file handle can be decoded to lower path.
484 * Return 0 on match, -ESTALE on mismatch or stale origin, < 0 on error.
485 */
Amir Goldstein1eff1a12017-12-12 22:40:46 +0200486int ovl_verify_index(struct ovl_fs *ofs, struct dentry *index)
Amir Goldstein415543d2017-06-21 15:28:42 +0300487{
488 struct ovl_fh *fh = NULL;
489 size_t len;
Chandan Rajendrab9343632017-07-24 01:57:54 -0500490 struct ovl_path origin = { };
491 struct ovl_path *stack = &origin;
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200492 struct dentry *upper = NULL;
Amir Goldstein415543d2017-06-21 15:28:42 +0300493 int err;
494
495 if (!d_inode(index))
496 return 0;
497
Amir Goldstein9ee60ce2017-11-01 10:13:51 +0200498 /* Cleanup leftover from index create/cleanup attempt */
499 err = -ESTALE;
500 if (ovl_is_temp_index(index))
501 goto fail;
502
Amir Goldsteinfa0096e2017-10-24 12:24:11 +0300503 err = -EINVAL;
Amir Goldstein415543d2017-06-21 15:28:42 +0300504 if (index->d_name.len < sizeof(struct ovl_fh)*2)
505 goto fail;
506
507 err = -ENOMEM;
508 len = index->d_name.len / 2;
Michal Hocko0ee931c2017-09-13 16:28:29 -0700509 fh = kzalloc(len, GFP_KERNEL);
Amir Goldstein415543d2017-06-21 15:28:42 +0300510 if (!fh)
511 goto fail;
512
513 err = -EINVAL;
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300514 if (hex2bin((u8 *)fh, index->d_name.name, len))
515 goto fail;
516
517 err = ovl_check_fh_len(fh, len);
518 if (err)
Amir Goldstein415543d2017-06-21 15:28:42 +0300519 goto fail;
520
Amir Goldstein7db25d32018-01-11 11:03:13 +0200521 /*
522 * Whiteout index entries are used as an indication that an exported
523 * overlay file handle should be treated as stale (i.e. after unlink
524 * of the overlay inode). These entries contain no origin xattr.
525 */
526 if (ovl_is_whiteout(index))
527 goto out;
528
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200529 /*
530 * Verifying directory index entries are not stale is expensive, so
531 * only verify stale dir index if NFS export is enabled.
532 */
533 if (d_is_dir(index) && !ofs->config.nfs_export)
534 goto out;
535
536 /*
537 * Directory index entries should have 'upper' xattr pointing to the
538 * real upper dir. Non-dir index entries are hardlinks to the upper
539 * real inode. For non-dir index, we can read the copy up origin xattr
540 * directly from the index dentry, but for dir index we first need to
541 * decode the upper directory.
542 */
543 upper = ovl_index_upper(ofs, index);
544 if (IS_ERR_OR_NULL(upper)) {
545 err = PTR_ERR(upper);
Amir Goldstein24f0b172018-01-11 15:33:51 +0200546 /*
547 * Directory index entries with no 'upper' xattr need to be
548 * removed. When dir index entry has a stale 'upper' xattr,
549 * we assume that upper dir was removed and we treat the dir
550 * index as orphan entry that needs to be whited out.
551 */
552 if (err == -ESTALE)
553 goto orphan;
554 else if (!err)
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200555 err = -ESTALE;
556 goto fail;
557 }
558
559 err = ovl_verify_fh(upper, OVL_XATTR_ORIGIN, fh);
560 dput(upper);
Amir Goldstein415543d2017-06-21 15:28:42 +0300561 if (err)
562 goto fail;
563
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200564 /* Check if non-dir index is orphan and don't warn before cleaning it */
565 if (!d_is_dir(index) && d_inode(index)->i_nlink == 1) {
Amir Goldstein8a22efa2018-03-09 15:51:02 +0200566 err = ovl_check_origin_fh(ofs, fh, false, index, &stack);
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200567 if (err)
568 goto fail;
Amir Goldstein415543d2017-06-21 15:28:42 +0300569
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200570 if (ovl_get_nlink(origin.dentry, index, 0) == 0)
Amir Goldstein24f0b172018-01-11 15:33:51 +0200571 goto orphan;
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200572 }
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300573
Amir Goldstein415543d2017-06-21 15:28:42 +0300574out:
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200575 dput(origin.dentry);
Amir Goldstein415543d2017-06-21 15:28:42 +0300576 kfree(fh);
577 return err;
578
579fail:
Amir Goldstein61b67472017-07-18 21:07:42 +0300580 pr_warn_ratelimited("overlayfs: failed to verify index (%pd2, ftype=%x, err=%i)\n",
581 index, d_inode(index)->i_mode & S_IFMT, err);
Amir Goldstein415543d2017-06-21 15:28:42 +0300582 goto out;
Amir Goldstein24f0b172018-01-11 15:33:51 +0200583
584orphan:
585 pr_warn_ratelimited("overlayfs: orphan index entry (%pd2, ftype=%x, nlink=%u)\n",
586 index, d_inode(index)->i_mode & S_IFMT,
587 d_inode(index)->i_nlink);
588 err = -ENOENT;
589 goto out;
Amir Goldstein415543d2017-06-21 15:28:42 +0300590}
591
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200592static int ovl_get_index_name_fh(struct ovl_fh *fh, struct qstr *name)
593{
594 char *n, *s;
595
Kees Cook6396bb22018-06-12 14:03:40 -0700596 n = kcalloc(fh->len, 2, GFP_KERNEL);
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200597 if (!n)
598 return -ENOMEM;
599
600 s = bin2hex(n, fh, fh->len);
601 *name = (struct qstr) QSTR_INIT(n, s - n);
602
603 return 0;
604
605}
606
Amir Goldstein415543d2017-06-21 15:28:42 +0300607/*
Amir Goldstein359f3922017-06-21 15:28:41 +0300608 * Lookup in indexdir for the index entry of a lower real inode or a copy up
609 * origin inode. The index entry name is the hex representation of the lower
610 * inode file handle.
611 *
612 * If the index dentry in negative, then either no lower aliases have been
613 * copied up yet, or aliases have been copied up in older kernels and are
614 * not indexed.
615 *
616 * If the index dentry for a copy up origin inode is positive, but points
617 * to an inode different than the upper inode, then either the upper inode
618 * has been copied up and not indexed or it was indexed, but since then
619 * index dir was cleared. Either way, that index cannot be used to indentify
620 * the overlay inode.
621 */
622int ovl_get_index_name(struct dentry *origin, struct qstr *name)
623{
Amir Goldstein359f3922017-06-21 15:28:41 +0300624 struct ovl_fh *fh;
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200625 int err;
Amir Goldstein359f3922017-06-21 15:28:41 +0300626
Amir Goldstein5b2cccd2018-02-02 10:42:03 +0200627 fh = ovl_encode_real_fh(origin, false);
Amir Goldstein359f3922017-06-21 15:28:41 +0300628 if (IS_ERR(fh))
629 return PTR_ERR(fh);
630
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200631 err = ovl_get_index_name_fh(fh, name);
632
Amir Goldstein359f3922017-06-21 15:28:41 +0300633 kfree(fh);
Amir Goldstein359f3922017-06-21 15:28:41 +0300634 return err;
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200635}
Amir Goldstein359f3922017-06-21 15:28:41 +0300636
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200637/* Lookup index by file handle for NFS export */
638struct dentry *ovl_get_index_fh(struct ovl_fs *ofs, struct ovl_fh *fh)
639{
640 struct dentry *index;
641 struct qstr name;
642 int err;
643
644 err = ovl_get_index_name_fh(fh, &name);
645 if (err)
646 return ERR_PTR(err);
647
648 index = lookup_one_len_unlocked(name.name, ofs->indexdir, name.len);
649 kfree(name.name);
650 if (IS_ERR(index)) {
651 if (PTR_ERR(index) == -ENOENT)
652 index = NULL;
653 return index;
654 }
655
656 if (d_is_negative(index))
657 err = 0;
658 else if (ovl_is_whiteout(index))
659 err = -ESTALE;
660 else if (ovl_dentry_weird(index))
661 err = -EIO;
662 else
663 return index;
664
665 dput(index);
666 return ERR_PTR(err);
Amir Goldstein359f3922017-06-21 15:28:41 +0300667}
668
Amir Goldstein06170152018-01-17 14:40:27 +0200669struct dentry *ovl_lookup_index(struct ovl_fs *ofs, struct dentry *upper,
670 struct dentry *origin, bool verify)
Amir Goldstein359f3922017-06-21 15:28:41 +0300671{
Amir Goldstein359f3922017-06-21 15:28:41 +0300672 struct dentry *index;
673 struct inode *inode;
674 struct qstr name;
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200675 bool is_dir = d_is_dir(origin);
Amir Goldstein359f3922017-06-21 15:28:41 +0300676 int err;
677
678 err = ovl_get_index_name(origin, &name);
679 if (err)
680 return ERR_PTR(err);
681
682 index = lookup_one_len_unlocked(name.name, ofs->indexdir, name.len);
683 if (IS_ERR(index)) {
Amir Goldsteine0082a02017-09-24 13:01:35 +0300684 err = PTR_ERR(index);
Amir Goldstein7937a562017-10-20 17:19:06 +0300685 if (err == -ENOENT) {
686 index = NULL;
687 goto out;
688 }
Amir Goldstein601350f2018-09-28 21:00:48 +0300689 pr_warn_ratelimited("overlayfs: failed inode index lookup (ino=%lu, key=%.*s, err=%i);\n"
Amir Goldstein359f3922017-06-21 15:28:41 +0300690 "overlayfs: mount with '-o index=off' to disable inodes index.\n",
691 d_inode(origin)->i_ino, name.len, name.name,
692 err);
693 goto out;
694 }
695
Amir Goldstein0e082552017-07-18 21:07:43 +0300696 inode = d_inode(index);
Amir Goldstein359f3922017-06-21 15:28:41 +0300697 if (d_is_negative(index)) {
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300698 goto out_dput;
Amir Goldstein06170152018-01-17 14:40:27 +0200699 } else if (ovl_is_whiteout(index) && !verify) {
700 /*
701 * When index lookup is called with !verify for decoding an
702 * overlay file handle, a whiteout index implies that decode
703 * should treat file handle as stale and no need to print a
704 * warning about it.
705 */
706 dput(index);
707 index = ERR_PTR(-ESTALE);
708 goto out;
Amir Goldstein0e082552017-07-18 21:07:43 +0300709 } else if (ovl_dentry_weird(index) || ovl_is_whiteout(index) ||
710 ((inode->i_mode ^ d_inode(origin)->i_mode) & S_IFMT)) {
711 /*
712 * Index should always be of the same file type as origin
713 * except for the case of a whiteout index. A whiteout
714 * index should only exist if all lower aliases have been
715 * unlinked, which means that finding a lower origin on lookup
716 * whose index is a whiteout should be treated as an error.
717 */
718 pr_warn_ratelimited("overlayfs: bad index found (index=%pd2, ftype=%x, origin ftype=%x).\n",
719 index, d_inode(index)->i_mode & S_IFMT,
720 d_inode(origin)->i_mode & S_IFMT);
Amir Goldstein359f3922017-06-21 15:28:41 +0300721 goto fail;
Amir Goldstein06170152018-01-17 14:40:27 +0200722 } else if (is_dir && verify) {
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200723 if (!upper) {
724 pr_warn_ratelimited("overlayfs: suspected uncovered redirected dir found (origin=%pd2, index=%pd2).\n",
725 origin, index);
726 goto fail;
727 }
Amir Goldstein359f3922017-06-21 15:28:41 +0300728
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200729 /* Verify that dir index 'upper' xattr points to upper dir */
730 err = ovl_verify_upper(index, upper, false);
731 if (err) {
732 if (err == -ESTALE) {
733 pr_warn_ratelimited("overlayfs: suspected multiply redirected dir found (upper=%pd2, origin=%pd2, index=%pd2).\n",
734 upper, origin, index);
735 }
736 goto fail;
737 }
738 } else if (upper && d_inode(upper) != inode) {
739 goto out_dput;
740 }
Amir Goldstein359f3922017-06-21 15:28:41 +0300741out:
742 kfree(name.name);
743 return index;
744
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300745out_dput:
746 dput(index);
747 index = NULL;
748 goto out;
749
Amir Goldstein359f3922017-06-21 15:28:41 +0300750fail:
751 dput(index);
752 index = ERR_PTR(-EIO);
753 goto out;
754}
755
756/*
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100757 * Returns next layer in stack starting from top.
758 * Returns -1 if this is the last layer.
759 */
760int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
761{
762 struct ovl_entry *oe = dentry->d_fsdata;
763
764 BUG_ON(idx < 0);
765 if (idx == 0) {
766 ovl_path_upper(dentry, path);
767 if (path->dentry)
768 return oe->numlower ? 1 : -1;
769 idx++;
770 }
771 BUG_ON(idx > oe->numlower);
Chandan Rajendrab9343632017-07-24 01:57:54 -0500772 path->dentry = oe->lowerstack[idx - 1].dentry;
773 path->mnt = oe->lowerstack[idx - 1].layer->mnt;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100774
775 return (idx < oe->numlower) ? idx + 1 : -1;
776}
777
Amir Goldstein9678e632018-01-03 19:34:45 +0200778/* Fix missing 'origin' xattr */
779static int ovl_fix_origin(struct dentry *dentry, struct dentry *lower,
780 struct dentry *upper)
781{
782 int err;
783
784 if (ovl_check_origin_xattr(upper))
785 return 0;
786
787 err = ovl_want_write(dentry);
788 if (err)
789 return err;
790
791 err = ovl_set_origin(dentry, lower, upper);
792 if (!err)
793 err = ovl_set_impure(dentry->d_parent, upper->d_parent);
794
795 ovl_drop_write(dentry);
796 return err;
797}
798
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100799struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
800 unsigned int flags)
801{
802 struct ovl_entry *oe;
803 const struct cred *old_cred;
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100804 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100805 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
Amir Goldsteinc22205d2017-04-26 23:40:52 +0300806 struct ovl_entry *roe = dentry->d_sb->s_root->d_fsdata;
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400807 struct ovl_path *stack = NULL, *origin_path = NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100808 struct dentry *upperdir, *upperdentry = NULL;
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200809 struct dentry *origin = NULL;
Amir Goldstein359f3922017-06-21 15:28:41 +0300810 struct dentry *index = NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100811 unsigned int ctr = 0;
812 struct inode *inode = NULL;
813 bool upperopaque = false;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100814 char *upperredirect = NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100815 struct dentry *this;
816 unsigned int i;
817 int err;
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400818 bool metacopy = false;
Miklos Szeredie28edc42016-12-16 11:02:56 +0100819 struct ovl_lookup_data d = {
820 .name = dentry->d_name,
821 .is_dir = false,
822 .opaque = false,
823 .stop = false,
Vivek Goyal452061f2018-03-09 15:44:41 -0500824 .last = ofs->config.redirect_follow ? false : !poe->numlower,
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100825 .redirect = NULL,
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400826 .metacopy = false,
Miklos Szeredie28edc42016-12-16 11:02:56 +0100827 };
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100828
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100829 if (dentry->d_name.len > ofs->namelen)
830 return ERR_PTR(-ENAMETOOLONG);
831
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100832 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200833 upperdir = ovl_dentry_upper(dentry->d_parent);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100834 if (upperdir) {
Miklos Szeredie28edc42016-12-16 11:02:56 +0100835 err = ovl_lookup_layer(upperdir, &d, &upperdentry);
836 if (err)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100837 goto out;
838
Miklos Szeredie28edc42016-12-16 11:02:56 +0100839 if (upperdentry && unlikely(ovl_dentry_remote(upperdentry))) {
840 dput(upperdentry);
841 err = -EREMOTE;
842 goto out;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100843 }
Amir Goldsteina9d01952017-04-30 14:46:31 +0300844 if (upperdentry && !d.is_dir) {
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400845 unsigned int origin_ctr = 0;
846
Amir Goldsteinf7d3dac2017-06-21 15:28:34 +0300847 /*
848 * Lookup copy up origin by decoding origin file handle.
849 * We may get a disconnected dentry, which is fine,
850 * because we only need to hold the origin inode in
851 * cache and use its inode number. We may even get a
852 * connected dentry, that is not under any of the lower
853 * layers root. That is also fine for using it's inode
854 * number - it's the same as if we held a reference
855 * to a dentry in lower layer that was moved under us.
856 */
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400857 err = ovl_check_origin(ofs, upperdentry, &origin_path,
858 &origin_ctr);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300859 if (err)
Vivek Goyal5455f922017-11-01 15:37:22 -0400860 goto out_put_upper;
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400861
862 if (d.metacopy)
863 metacopy = true;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300864 }
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100865
866 if (d.redirect) {
Dan Carpenter0ce5cdc2017-09-22 23:45:18 +0300867 err = -ENOMEM;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100868 upperredirect = kstrdup(d.redirect, GFP_KERNEL);
869 if (!upperredirect)
870 goto out_put_upper;
871 if (d.redirect[0] == '/')
Amir Goldsteinc22205d2017-04-26 23:40:52 +0300872 poe = roe;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100873 }
Miklos Szeredie28edc42016-12-16 11:02:56 +0100874 upperopaque = d.opaque;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100875 }
876
Miklos Szeredie28edc42016-12-16 11:02:56 +0100877 if (!d.stop && poe->numlower) {
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100878 err = -ENOMEM;
Chandan Rajendrab9343632017-07-24 01:57:54 -0500879 stack = kcalloc(ofs->numlower, sizeof(struct ovl_path),
Michal Hocko0ee931c2017-09-13 16:28:29 -0700880 GFP_KERNEL);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100881 if (!stack)
882 goto out_put_upper;
883 }
884
Miklos Szeredie28edc42016-12-16 11:02:56 +0100885 for (i = 0; !d.stop && i < poe->numlower; i++) {
Chandan Rajendrab9343632017-07-24 01:57:54 -0500886 struct ovl_path lower = poe->lowerstack[i];
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100887
Vivek Goyal452061f2018-03-09 15:44:41 -0500888 if (!ofs->config.redirect_follow)
889 d.last = i == poe->numlower - 1;
890 else
891 d.last = lower.layer->idx == roe->numlower;
892
Chandan Rajendrab9343632017-07-24 01:57:54 -0500893 err = ovl_lookup_layer(lower.dentry, &d, &this);
Miklos Szeredie28edc42016-12-16 11:02:56 +0100894 if (err)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100895 goto out_put;
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100896
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100897 if (!this)
898 continue;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100899
Amir Goldstein9678e632018-01-03 19:34:45 +0200900 /*
901 * If no origin fh is stored in upper of a merge dir, store fh
902 * of lower dir and set upper parent "impure".
903 */
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400904 if (upperdentry && !ctr && !ofs->noxattr && d.is_dir) {
Amir Goldstein9678e632018-01-03 19:34:45 +0200905 err = ovl_fix_origin(dentry, this, upperdentry);
906 if (err) {
907 dput(this);
908 goto out_put;
909 }
910 }
911
Amir Goldstein37b129162018-01-10 22:29:38 +0200912 /*
913 * When "verify_lower" feature is enabled, do not merge with a
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200914 * lower dir that does not match a stored origin xattr. In any
915 * case, only verified origin is used for index lookup.
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400916 *
917 * For non-dir dentry, if index=on, then ensure origin
918 * matches the dentry found using path based lookup,
919 * otherwise error out.
Amir Goldstein37b129162018-01-10 22:29:38 +0200920 */
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400921 if (upperdentry && !ctr &&
922 ((d.is_dir && ovl_verify_lower(dentry->d_sb)) ||
923 (!d.is_dir && ofs->config.index && origin_path))) {
Amir Goldstein37b129162018-01-10 22:29:38 +0200924 err = ovl_verify_origin(upperdentry, this, false);
925 if (err) {
926 dput(this);
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400927 if (d.is_dir)
928 break;
929 goto out_put;
Amir Goldstein37b129162018-01-10 22:29:38 +0200930 }
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200931 origin = this;
Amir Goldstein37b129162018-01-10 22:29:38 +0200932 }
933
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400934 if (d.metacopy)
935 metacopy = true;
936 /*
937 * Do not store intermediate metacopy dentries in chain,
938 * except top most lower metacopy dentry
939 */
940 if (d.metacopy && ctr) {
941 dput(this);
942 continue;
943 }
944
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100945 stack[ctr].dentry = this;
Chandan Rajendrab9343632017-07-24 01:57:54 -0500946 stack[ctr].layer = lower.layer;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100947 ctr++;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100948
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100949 /*
950 * Following redirects can have security consequences: it's like
951 * a symlink into the lower layer without the permission checks.
952 * This is only a problem if the upper layer is untrusted (e.g
953 * comes from an USB drive). This can allow a non-readable file
954 * or directory to become readable.
955 *
956 * Only following redirects when redirects are enabled disables
957 * this attack vector when not necessary.
958 */
959 err = -EPERM;
960 if (d.redirect && !ofs->config.redirect_follow) {
Amir Goldsteinf8167812017-12-18 14:25:56 +0200961 pr_warn_ratelimited("overlayfs: refusing to follow redirect for (%pd2)\n",
962 dentry);
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100963 goto out_put;
964 }
965
Vivek Goyald1fe96c2018-02-02 10:23:24 -0500966 if (d.stop)
967 break;
968
Amir Goldsteinc22205d2017-04-26 23:40:52 +0300969 if (d.redirect && d.redirect[0] == '/' && poe != roe) {
970 poe = roe;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100971 /* Find the current layer on the root dentry */
Amir Goldsteind583ed72017-11-08 19:23:36 +0200972 i = lower.layer->idx - 1;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100973 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100974 }
975
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400976 if (metacopy) {
977 /*
978 * Found a metacopy dentry but did not find corresponding
979 * data dentry
980 */
981 if (d.metacopy) {
982 err = -EIO;
983 goto out_put;
984 }
985
986 err = -EPERM;
987 if (!ofs->config.metacopy) {
988 pr_warn_ratelimited("overlay: refusing to follow metacopy origin for (%pd2)\n",
989 dentry);
990 goto out_put;
991 }
992 } else if (!d.is_dir && upperdentry && !ctr && origin_path) {
993 if (WARN_ON(stack != NULL)) {
994 err = -EIO;
995 goto out_put;
996 }
997 stack = origin_path;
998 ctr = 1;
999 origin_path = NULL;
1000 }
1001
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001002 /*
1003 * Lookup index by lower inode and verify it matches upper inode.
1004 * We only trust dir index if we verified that lower dir matches
1005 * origin, otherwise dir index entries may be inconsistent and we
Vivek Goyal9d3dfea2018-05-11 11:49:28 -04001006 * ignore them.
1007 *
1008 * For non-dir upper metacopy dentry, we already set "origin" if we
1009 * verified that lower matched upper origin. If upper origin was
1010 * not present (because lower layer did not support fh encode/decode),
1011 * or indexing is not enabled, do not set "origin" and skip looking up
1012 * index. This case should be handled in same way as a non-dir upper
1013 * without ORIGIN is handled.
1014 *
1015 * Always lookup index of non-dir non-metacopy and non-upper.
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001016 */
Vivek Goyal9d3dfea2018-05-11 11:49:28 -04001017 if (ctr && (!upperdentry || (!d.is_dir && !metacopy)))
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001018 origin = stack[0].dentry;
Amir Goldstein359f3922017-06-21 15:28:41 +03001019
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001020 if (origin && ovl_indexdir(dentry->d_sb) &&
1021 (!d.is_dir || ovl_index_all(dentry->d_sb))) {
Amir Goldstein06170152018-01-17 14:40:27 +02001022 index = ovl_lookup_index(ofs, upperdentry, origin, true);
Amir Goldstein359f3922017-06-21 15:28:41 +03001023 if (IS_ERR(index)) {
1024 err = PTR_ERR(index);
1025 index = NULL;
1026 goto out_put;
1027 }
1028 }
1029
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001030 oe = ovl_alloc_entry(ctr);
1031 err = -ENOMEM;
1032 if (!oe)
1033 goto out_put;
1034
Chandan Rajendrab9343632017-07-24 01:57:54 -05001035 memcpy(oe->lowerstack, stack, sizeof(struct ovl_path) * ctr);
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +02001036 dentry->d_fsdata = oe;
1037
Amir Goldsteinc62520a2018-01-14 19:25:31 +02001038 if (upperopaque)
1039 ovl_dentry_set_opaque(dentry);
1040
Miklos Szeredi55acc662017-07-04 22:03:18 +02001041 if (upperdentry)
1042 ovl_dentry_set_upper_alias(dentry);
Vivek Goyal0a2d0d32018-05-11 11:49:32 -04001043 else if (index) {
Amir Goldstein359f3922017-06-21 15:28:41 +03001044 upperdentry = dget(index);
Vivek Goyal0a2d0d32018-05-11 11:49:32 -04001045 upperredirect = ovl_get_redirect_xattr(upperdentry, 0);
1046 if (IS_ERR(upperredirect)) {
1047 err = PTR_ERR(upperredirect);
1048 upperredirect = NULL;
1049 goto out_free_oe;
1050 }
1051 }
Amir Goldstein359f3922017-06-21 15:28:41 +03001052
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +02001053 if (upperdentry || ctr) {
Vivek Goyalac6a52e2018-05-08 09:27:21 -04001054 struct ovl_inode_params oip = {
1055 .upperdentry = upperdentry,
1056 .lowerpath = stack,
1057 .index = index,
1058 .numlower = ctr,
Vivek Goyal9cec54c2018-05-11 11:49:27 -04001059 .redirect = upperredirect,
Vivek Goyal2664bd02018-05-11 11:49:30 -04001060 .lowerdata = (ctr > 1 && !d.is_dir) ?
1061 stack[ctr - 1].dentry : NULL,
Vivek Goyalac6a52e2018-05-08 09:27:21 -04001062 };
1063
1064 inode = ovl_get_inode(dentry->d_sb, &oip);
Miklos Szeredib9ac5c272017-07-04 22:03:17 +02001065 err = PTR_ERR(inode);
1066 if (IS_ERR(inode))
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +02001067 goto out_free_oe;
1068 }
1069
1070 revert_creds(old_cred);
Vivek Goyal9d3dfea2018-05-11 11:49:28 -04001071 if (origin_path) {
1072 dput(origin_path->dentry);
1073 kfree(origin_path);
1074 }
Amir Goldstein359f3922017-06-21 15:28:41 +03001075 dput(index);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001076 kfree(stack);
Miklos Szeredi02b69b22016-12-16 11:02:56 +01001077 kfree(d.redirect);
Amir Goldstein829c28b2017-09-29 21:43:07 +03001078 return d_splice_alias(inode, dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001079
1080out_free_oe:
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +02001081 dentry->d_fsdata = NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001082 kfree(oe);
1083out_put:
Amir Goldstein359f3922017-06-21 15:28:41 +03001084 dput(index);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001085 for (i = 0; i < ctr; i++)
1086 dput(stack[i].dentry);
1087 kfree(stack);
1088out_put_upper:
Vivek Goyal9d3dfea2018-05-11 11:49:28 -04001089 if (origin_path) {
1090 dput(origin_path->dentry);
1091 kfree(origin_path);
1092 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001093 dput(upperdentry);
Miklos Szeredi02b69b22016-12-16 11:02:56 +01001094 kfree(upperredirect);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001095out:
Miklos Szeredi02b69b22016-12-16 11:02:56 +01001096 kfree(d.redirect);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001097 revert_creds(old_cred);
1098 return ERR_PTR(err);
1099}
1100
1101bool ovl_lower_positive(struct dentry *dentry)
1102{
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001103 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
1104 const struct qstr *name = &dentry->d_name;
Amir Goldstein6d0a8a92017-11-10 13:18:07 +02001105 const struct cred *old_cred;
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001106 unsigned int i;
1107 bool positive = false;
1108 bool done = false;
1109
1110 /*
1111 * If dentry is negative, then lower is positive iff this is a
1112 * whiteout.
1113 */
1114 if (!dentry->d_inode)
Amir Goldsteinc62520a2018-01-14 19:25:31 +02001115 return ovl_dentry_is_opaque(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001116
1117 /* Negative upper -> positive lower */
Miklos Szeredi09d8b582017-07-04 22:03:16 +02001118 if (!ovl_dentry_upper(dentry))
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001119 return true;
1120
Amir Goldstein6d0a8a92017-11-10 13:18:07 +02001121 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001122 /* Positive upper -> have to look up lower to see whether it exists */
1123 for (i = 0; !done && !positive && i < poe->numlower; i++) {
1124 struct dentry *this;
1125 struct dentry *lowerdir = poe->lowerstack[i].dentry;
1126
1127 this = lookup_one_len_unlocked(name->name, lowerdir,
1128 name->len);
1129 if (IS_ERR(this)) {
1130 switch (PTR_ERR(this)) {
1131 case -ENOENT:
1132 case -ENAMETOOLONG:
1133 break;
1134
1135 default:
1136 /*
1137 * Assume something is there, we just couldn't
1138 * access it.
1139 */
1140 positive = true;
1141 break;
1142 }
1143 } else {
1144 if (this->d_inode) {
1145 positive = !ovl_is_whiteout(this);
1146 done = true;
1147 }
1148 dput(this);
1149 }
1150 }
Amir Goldstein6d0a8a92017-11-10 13:18:07 +02001151 revert_creds(old_cred);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001152
1153 return positive;
1154}