blob: 24bd387321d14829e4c68de2c5b339a954d1a14c [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;
Miklos Szeredie28edc42016-12-16 11:02:56 +010027};
Miklos Szeredibbb1e542016-12-16 11:02:56 +010028
Miklos Szeredi02b69b22016-12-16 11:02:56 +010029static int ovl_check_redirect(struct dentry *dentry, struct ovl_lookup_data *d,
30 size_t prelen, const char *post)
31{
32 int res;
33 char *s, *next, *buf = NULL;
34
35 res = vfs_getxattr(dentry, OVL_XATTR_REDIRECT, NULL, 0);
36 if (res < 0) {
37 if (res == -ENODATA || res == -EOPNOTSUPP)
38 return 0;
39 goto fail;
40 }
Michal Hocko0ee931c2017-09-13 16:28:29 -070041 buf = kzalloc(prelen + res + strlen(post) + 1, GFP_KERNEL);
Miklos Szeredi02b69b22016-12-16 11:02:56 +010042 if (!buf)
43 return -ENOMEM;
44
45 if (res == 0)
46 goto invalid;
47
48 res = vfs_getxattr(dentry, OVL_XATTR_REDIRECT, buf, res);
49 if (res < 0)
50 goto fail;
51 if (res == 0)
52 goto invalid;
53 if (buf[0] == '/') {
54 for (s = buf; *s++ == '/'; s = next) {
55 next = strchrnul(s, '/');
56 if (s == next)
57 goto invalid;
58 }
Amir Goldstein3ec9b3f2018-03-12 10:30:41 -040059 /*
60 * One of the ancestor path elements in an absolute path
61 * lookup in ovl_lookup_layer() could have been opaque and
62 * that will stop further lookup in lower layers (d->stop=true)
63 * But we have found an absolute redirect in decendant path
64 * element and that should force continue lookup in lower
65 * layers (reset d->stop).
66 */
67 d->stop = false;
Miklos Szeredi02b69b22016-12-16 11:02:56 +010068 } else {
69 if (strchr(buf, '/') != NULL)
70 goto invalid;
71
72 memmove(buf + prelen, buf, res);
73 memcpy(buf, d->name.name, prelen);
74 }
75
76 strcat(buf, post);
77 kfree(d->redirect);
78 d->redirect = buf;
79 d->name.name = d->redirect;
80 d->name.len = strlen(d->redirect);
81
82 return 0;
83
84err_free:
85 kfree(buf);
86 return 0;
87fail:
88 pr_warn_ratelimited("overlayfs: failed to get redirect (%i)\n", res);
89 goto err_free;
90invalid:
91 pr_warn_ratelimited("overlayfs: invalid redirect (%s)\n", buf);
92 goto err_free;
93}
94
Amir Goldsteina9d01952017-04-30 14:46:31 +030095static int ovl_acceptable(void *ctx, struct dentry *dentry)
96{
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +020097 /*
98 * A non-dir origin may be disconnected, which is fine, because
99 * we only need it for its unique inode number.
100 */
101 if (!d_is_dir(dentry))
102 return 1;
103
104 /* Don't decode a deleted empty directory */
105 if (d_unhashed(dentry))
106 return 0;
107
108 /* Check if directory belongs to the layer we are decoding from */
109 return is_subdir(dentry, ((struct vfsmount *)ctx)->mnt_root);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300110}
111
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300112/*
113 * Check validity of an overlay file handle buffer.
114 *
115 * Return 0 for a valid file handle.
116 * Return -ENODATA for "origin unknown".
117 * Return <0 for an invalid file handle.
118 */
Amir Goldstein8556a422018-01-19 01:03:23 +0200119int ovl_check_fh_len(struct ovl_fh *fh, int fh_len)
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300120{
121 if (fh_len < sizeof(struct ovl_fh) || fh_len < fh->len)
122 return -EINVAL;
123
124 if (fh->magic != OVL_FH_MAGIC)
125 return -EINVAL;
126
127 /* Treat larger version and unknown flags as "origin unknown" */
128 if (fh->version > OVL_FH_VERSION || fh->flags & ~OVL_FH_FLAG_ALL)
129 return -ENODATA;
130
131 /* Treat endianness mismatch as "origin unknown" */
132 if (!(fh->flags & OVL_FH_FLAG_ANY_ENDIAN) &&
133 (fh->flags & OVL_FH_FLAG_BIG_ENDIAN) != OVL_FH_FLAG_CPU_ENDIAN)
134 return -ENODATA;
135
136 return 0;
137}
138
Amir Goldstein05122442018-01-11 08:25:32 +0200139static struct ovl_fh *ovl_get_fh(struct dentry *dentry, const char *name)
Amir Goldsteina9d01952017-04-30 14:46:31 +0300140{
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300141 int res, err;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300142 struct ovl_fh *fh = NULL;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300143
Amir Goldstein05122442018-01-11 08:25:32 +0200144 res = vfs_getxattr(dentry, name, NULL, 0);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300145 if (res < 0) {
146 if (res == -ENODATA || res == -EOPNOTSUPP)
147 return NULL;
148 goto fail;
149 }
150 /* Zero size value means "copied up but origin unknown" */
151 if (res == 0)
152 return NULL;
153
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300154 fh = kzalloc(res, GFP_KERNEL);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300155 if (!fh)
156 return ERR_PTR(-ENOMEM);
157
Amir Goldstein05122442018-01-11 08:25:32 +0200158 res = vfs_getxattr(dentry, name, fh, res);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300159 if (res < 0)
160 goto fail;
161
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300162 err = ovl_check_fh_len(fh, res);
163 if (err < 0) {
164 if (err == -ENODATA)
165 goto out;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300166 goto invalid;
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300167 }
Amir Goldsteina9d01952017-04-30 14:46:31 +0300168
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300169 return fh;
170
171out:
172 kfree(fh);
173 return NULL;
174
175fail:
176 pr_warn_ratelimited("overlayfs: failed to get origin (%i)\n", res);
177 goto out;
178invalid:
179 pr_warn_ratelimited("overlayfs: invalid origin (%*phN)\n", res, fh);
180 goto out;
181}
182
Amir Goldstein5b2cccd2018-02-02 10:42:03 +0200183struct dentry *ovl_decode_real_fh(struct ovl_fh *fh, struct vfsmount *mnt)
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300184{
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200185 struct dentry *real;
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300186 int bytes;
187
Amir Goldsteina9d01952017-04-30 14:46:31 +0300188 /*
189 * Make sure that the stored uuid matches the uuid of the lower
190 * layer where file handle will be decoded.
191 */
Christoph Hellwig85787092017-05-10 15:06:33 +0200192 if (!uuid_equal(&fh->uuid, &mnt->mnt_sb->s_uuid))
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300193 return NULL;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300194
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300195 bytes = (fh->len - offsetof(struct ovl_fh, fid));
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200196 real = exportfs_decode_fh(mnt, (struct fid *)fh->fid,
197 bytes >> 2, (int)fh->type,
198 ovl_acceptable, mnt);
199 if (IS_ERR(real)) {
200 /*
201 * Treat stale file handle to lower file as "origin unknown".
202 * upper file handle could become stale when upper file is
203 * unlinked and this information is needed to handle stale
204 * index entries correctly.
205 */
206 if (real == ERR_PTR(-ESTALE) &&
207 !(fh->flags & OVL_FH_FLAG_PATH_UPPER))
208 real = NULL;
209 return real;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300210 }
211
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200212 if (ovl_dentry_weird(real)) {
213 dput(real);
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300214 return NULL;
215 }
Amir Goldsteina9d01952017-04-30 14:46:31 +0300216
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200217 return real;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300218}
219
Amir Goldsteinee1d6d372017-05-11 16:42:26 +0300220static bool ovl_is_opaquedir(struct dentry *dentry)
221{
222 return ovl_check_dir_xattr(dentry, OVL_XATTR_OPAQUE);
223}
224
Miklos Szeredie28edc42016-12-16 11:02:56 +0100225static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d,
226 const char *name, unsigned int namelen,
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100227 size_t prelen, const char *post,
Miklos Szeredie28edc42016-12-16 11:02:56 +0100228 struct dentry **ret)
229{
230 struct dentry *this;
231 int err;
232
233 this = lookup_one_len_unlocked(name, base, namelen);
234 if (IS_ERR(this)) {
235 err = PTR_ERR(this);
236 this = NULL;
237 if (err == -ENOENT || err == -ENAMETOOLONG)
238 goto out;
239 goto out_err;
240 }
241 if (!this->d_inode)
242 goto put_and_out;
243
244 if (ovl_dentry_weird(this)) {
245 /* Don't support traversing automounts and other weirdness */
246 err = -EREMOTE;
247 goto out_err;
248 }
249 if (ovl_is_whiteout(this)) {
250 d->stop = d->opaque = true;
251 goto put_and_out;
252 }
253 if (!d_can_lookup(this)) {
254 d->stop = true;
255 if (d->is_dir)
256 goto put_and_out;
257 goto out;
258 }
259 d->is_dir = true;
260 if (!d->last && ovl_is_opaquedir(this)) {
261 d->stop = d->opaque = true;
262 goto out;
263 }
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100264 err = ovl_check_redirect(this, d, prelen, post);
265 if (err)
266 goto out_err;
Miklos Szeredie28edc42016-12-16 11:02:56 +0100267out:
268 *ret = this;
269 return 0;
270
271put_and_out:
272 dput(this);
273 this = NULL;
274 goto out;
275
276out_err:
277 dput(this);
278 return err;
279}
280
281static int ovl_lookup_layer(struct dentry *base, struct ovl_lookup_data *d,
282 struct dentry **ret)
283{
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100284 /* Counting down from the end, since the prefix can change */
285 size_t rem = d->name.len - 1;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100286 struct dentry *dentry = NULL;
287 int err;
288
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100289 if (d->name.name[0] != '/')
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100290 return ovl_lookup_single(base, d, d->name.name, d->name.len,
291 0, "", ret);
292
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100293 while (!IS_ERR_OR_NULL(base) && d_can_lookup(base)) {
294 const char *s = d->name.name + d->name.len - rem;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100295 const char *next = strchrnul(s, '/');
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100296 size_t thislen = next - s;
297 bool end = !next[0];
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100298
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100299 /* Verify we did not go off the rails */
300 if (WARN_ON(s[-1] != '/'))
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100301 return -EIO;
302
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100303 err = ovl_lookup_single(base, d, s, thislen,
304 d->name.len - rem, next, &base);
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100305 dput(dentry);
306 if (err)
307 return err;
308 dentry = base;
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100309 if (end)
310 break;
311
312 rem -= thislen + 1;
313
314 if (WARN_ON(rem >= d->name.len))
315 return -EIO;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100316 }
317 *ret = dentry;
318 return 0;
Miklos Szeredie28edc42016-12-16 11:02:56 +0100319}
320
Amir Goldsteina9d01952017-04-30 14:46:31 +0300321
Amir Goldsteinf9418662018-01-19 21:33:44 +0200322int ovl_check_origin_fh(struct ovl_fs *ofs, struct ovl_fh *fh,
323 struct dentry *upperdentry, struct ovl_path **stackp)
Amir Goldsteina9d01952017-04-30 14:46:31 +0300324{
Amir Goldsteinf7d3dac2017-06-21 15:28:34 +0300325 struct dentry *origin = NULL;
326 int i;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300327
Amir Goldstein1eff1a12017-12-12 22:40:46 +0200328 for (i = 0; i < ofs->numlower; i++) {
Amir Goldstein5b2cccd2018-02-02 10:42:03 +0200329 origin = ovl_decode_real_fh(fh, ofs->lower_layers[i].mnt);
Amir Goldsteinf7d3dac2017-06-21 15:28:34 +0300330 if (origin)
331 break;
332 }
333
334 if (!origin)
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300335 return -ESTALE;
336 else if (IS_ERR(origin))
337 return PTR_ERR(origin);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300338
Amir Goldsteinf9418662018-01-19 21:33:44 +0200339 if (upperdentry && !ovl_is_whiteout(upperdentry) &&
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300340 ((d_inode(origin)->i_mode ^ d_inode(upperdentry)->i_mode) & S_IFMT))
341 goto invalid;
342
Amir Goldstein415543d2017-06-21 15:28:42 +0300343 if (!*stackp)
Chandan Rajendrab9343632017-07-24 01:57:54 -0500344 *stackp = kmalloc(sizeof(struct ovl_path), GFP_KERNEL);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300345 if (!*stackp) {
346 dput(origin);
347 return -ENOMEM;
348 }
Amir Goldstein1eff1a12017-12-12 22:40:46 +0200349 **stackp = (struct ovl_path){
350 .dentry = origin,
351 .layer = &ofs->lower_layers[i]
352 };
Amir Goldsteina9d01952017-04-30 14:46:31 +0300353
354 return 0;
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300355
356invalid:
357 pr_warn_ratelimited("overlayfs: invalid origin (%pd2, ftype=%x, origin ftype=%x).\n",
358 upperdentry, d_inode(upperdentry)->i_mode & S_IFMT,
359 d_inode(origin)->i_mode & S_IFMT);
360 dput(origin);
361 return -EIO;
362}
363
Amir Goldstein1eff1a12017-12-12 22:40:46 +0200364static int ovl_check_origin(struct ovl_fs *ofs, struct dentry *upperdentry,
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300365 struct ovl_path **stackp, unsigned int *ctrp)
366{
Amir Goldstein05122442018-01-11 08:25:32 +0200367 struct ovl_fh *fh = ovl_get_fh(upperdentry, OVL_XATTR_ORIGIN);
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300368 int err;
369
370 if (IS_ERR_OR_NULL(fh))
371 return PTR_ERR(fh);
372
Amir Goldstein1eff1a12017-12-12 22:40:46 +0200373 err = ovl_check_origin_fh(ofs, fh, upperdentry, stackp);
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300374 kfree(fh);
375
376 if (err) {
377 if (err == -ESTALE)
378 return 0;
379 return err;
380 }
381
382 if (WARN_ON(*ctrp))
383 return -EIO;
384
385 *ctrp = 1;
386 return 0;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300387}
388
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100389/*
Amir Goldstein05122442018-01-11 08:25:32 +0200390 * Verify that @fh matches the file handle stored in xattr @name.
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300391 * Return 0 on match, -ESTALE on mismatch, < 0 on error.
392 */
Amir Goldstein05122442018-01-11 08:25:32 +0200393static int ovl_verify_fh(struct dentry *dentry, const char *name,
394 const struct ovl_fh *fh)
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300395{
Amir Goldstein05122442018-01-11 08:25:32 +0200396 struct ovl_fh *ofh = ovl_get_fh(dentry, name);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300397 int err = 0;
398
399 if (!ofh)
400 return -ENODATA;
401
402 if (IS_ERR(ofh))
403 return PTR_ERR(ofh);
404
405 if (fh->len != ofh->len || memcmp(fh, ofh, fh->len))
406 err = -ESTALE;
407
408 kfree(ofh);
409 return err;
410}
411
412/*
Amir Goldstein05122442018-01-11 08:25:32 +0200413 * Verify that @real dentry matches the file handle stored in xattr @name.
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300414 *
Amir Goldstein05122442018-01-11 08:25:32 +0200415 * If @set is true and there is no stored file handle, encode @real and store
416 * file handle in xattr @name.
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300417 *
Amir Goldstein05122442018-01-11 08:25:32 +0200418 * Return 0 on match, -ESTALE on mismatch, -ENODATA on no xattr, < 0 on error.
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300419 */
Amir Goldstein05122442018-01-11 08:25:32 +0200420int ovl_verify_set_fh(struct dentry *dentry, const char *name,
421 struct dentry *real, bool is_upper, bool set)
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300422{
423 struct inode *inode;
424 struct ovl_fh *fh;
425 int err;
426
Amir Goldstein5b2cccd2018-02-02 10:42:03 +0200427 fh = ovl_encode_real_fh(real, is_upper);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300428 err = PTR_ERR(fh);
429 if (IS_ERR(fh))
430 goto fail;
431
Amir Goldstein05122442018-01-11 08:25:32 +0200432 err = ovl_verify_fh(dentry, name, fh);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300433 if (set && err == -ENODATA)
Amir Goldstein05122442018-01-11 08:25:32 +0200434 err = ovl_do_setxattr(dentry, name, fh, fh->len, 0);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300435 if (err)
436 goto fail;
437
438out:
439 kfree(fh);
440 return err;
441
442fail:
Amir Goldstein05122442018-01-11 08:25:32 +0200443 inode = d_inode(real);
444 pr_warn_ratelimited("overlayfs: failed to verify %s (%pd2, ino=%lu, err=%i)\n",
445 is_upper ? "upper" : "origin", real,
446 inode ? inode->i_ino : 0, err);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300447 goto out;
448}
449
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200450/* Get upper dentry from index */
Amir Goldstein3b0bfc62017-12-24 18:42:16 +0200451struct dentry *ovl_index_upper(struct ovl_fs *ofs, struct dentry *index)
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200452{
453 struct ovl_fh *fh;
454 struct dentry *upper;
455
456 if (!d_is_dir(index))
457 return dget(index);
458
459 fh = ovl_get_fh(index, OVL_XATTR_UPPER);
460 if (IS_ERR_OR_NULL(fh))
461 return ERR_CAST(fh);
462
Amir Goldstein5b2cccd2018-02-02 10:42:03 +0200463 upper = ovl_decode_real_fh(fh, ofs->upper_mnt);
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200464 kfree(fh);
465
466 if (IS_ERR_OR_NULL(upper))
467 return upper ?: ERR_PTR(-ESTALE);
468
469 if (!d_is_dir(upper)) {
470 pr_warn_ratelimited("overlayfs: invalid index upper (%pd2, upper=%pd2).\n",
471 index, upper);
472 dput(upper);
473 return ERR_PTR(-EIO);
474 }
475
476 return upper;
477}
478
Amir Goldstein9ee60ce2017-11-01 10:13:51 +0200479/* Is this a leftover from create/whiteout of directory index entry? */
480static bool ovl_is_temp_index(struct dentry *index)
481{
482 return index->d_name.name[0] == '#';
483}
484
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300485/*
Amir Goldstein415543d2017-06-21 15:28:42 +0300486 * Verify that an index entry name matches the origin file handle stored in
487 * OVL_XATTR_ORIGIN and that origin file handle can be decoded to lower path.
488 * Return 0 on match, -ESTALE on mismatch or stale origin, < 0 on error.
489 */
Amir Goldstein1eff1a12017-12-12 22:40:46 +0200490int ovl_verify_index(struct ovl_fs *ofs, struct dentry *index)
Amir Goldstein415543d2017-06-21 15:28:42 +0300491{
492 struct ovl_fh *fh = NULL;
493 size_t len;
Chandan Rajendrab9343632017-07-24 01:57:54 -0500494 struct ovl_path origin = { };
495 struct ovl_path *stack = &origin;
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200496 struct dentry *upper = NULL;
Amir Goldstein415543d2017-06-21 15:28:42 +0300497 int err;
498
499 if (!d_inode(index))
500 return 0;
501
Amir Goldstein9ee60ce2017-11-01 10:13:51 +0200502 /* Cleanup leftover from index create/cleanup attempt */
503 err = -ESTALE;
504 if (ovl_is_temp_index(index))
505 goto fail;
506
Amir Goldsteinfa0096e2017-10-24 12:24:11 +0300507 err = -EINVAL;
Amir Goldstein415543d2017-06-21 15:28:42 +0300508 if (index->d_name.len < sizeof(struct ovl_fh)*2)
509 goto fail;
510
511 err = -ENOMEM;
512 len = index->d_name.len / 2;
Michal Hocko0ee931c2017-09-13 16:28:29 -0700513 fh = kzalloc(len, GFP_KERNEL);
Amir Goldstein415543d2017-06-21 15:28:42 +0300514 if (!fh)
515 goto fail;
516
517 err = -EINVAL;
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300518 if (hex2bin((u8 *)fh, index->d_name.name, len))
519 goto fail;
520
521 err = ovl_check_fh_len(fh, len);
522 if (err)
Amir Goldstein415543d2017-06-21 15:28:42 +0300523 goto fail;
524
Amir Goldstein7db25d32018-01-11 11:03:13 +0200525 /*
526 * Whiteout index entries are used as an indication that an exported
527 * overlay file handle should be treated as stale (i.e. after unlink
528 * of the overlay inode). These entries contain no origin xattr.
529 */
530 if (ovl_is_whiteout(index))
531 goto out;
532
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200533 /*
534 * Verifying directory index entries are not stale is expensive, so
535 * only verify stale dir index if NFS export is enabled.
536 */
537 if (d_is_dir(index) && !ofs->config.nfs_export)
538 goto out;
539
540 /*
541 * Directory index entries should have 'upper' xattr pointing to the
542 * real upper dir. Non-dir index entries are hardlinks to the upper
543 * real inode. For non-dir index, we can read the copy up origin xattr
544 * directly from the index dentry, but for dir index we first need to
545 * decode the upper directory.
546 */
547 upper = ovl_index_upper(ofs, index);
548 if (IS_ERR_OR_NULL(upper)) {
549 err = PTR_ERR(upper);
Amir Goldstein24f0b172018-01-11 15:33:51 +0200550 /*
551 * Directory index entries with no 'upper' xattr need to be
552 * removed. When dir index entry has a stale 'upper' xattr,
553 * we assume that upper dir was removed and we treat the dir
554 * index as orphan entry that needs to be whited out.
555 */
556 if (err == -ESTALE)
557 goto orphan;
558 else if (!err)
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200559 err = -ESTALE;
560 goto fail;
561 }
562
563 err = ovl_verify_fh(upper, OVL_XATTR_ORIGIN, fh);
564 dput(upper);
Amir Goldstein415543d2017-06-21 15:28:42 +0300565 if (err)
566 goto fail;
567
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200568 /* Check if non-dir index is orphan and don't warn before cleaning it */
569 if (!d_is_dir(index) && d_inode(index)->i_nlink == 1) {
570 err = ovl_check_origin_fh(ofs, fh, index, &stack);
571 if (err)
572 goto fail;
Amir Goldstein415543d2017-06-21 15:28:42 +0300573
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200574 if (ovl_get_nlink(origin.dentry, index, 0) == 0)
Amir Goldstein24f0b172018-01-11 15:33:51 +0200575 goto orphan;
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200576 }
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300577
Amir Goldstein415543d2017-06-21 15:28:42 +0300578out:
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200579 dput(origin.dentry);
Amir Goldstein415543d2017-06-21 15:28:42 +0300580 kfree(fh);
581 return err;
582
583fail:
Amir Goldstein61b67472017-07-18 21:07:42 +0300584 pr_warn_ratelimited("overlayfs: failed to verify index (%pd2, ftype=%x, err=%i)\n",
585 index, d_inode(index)->i_mode & S_IFMT, err);
Amir Goldstein415543d2017-06-21 15:28:42 +0300586 goto out;
Amir Goldstein24f0b172018-01-11 15:33:51 +0200587
588orphan:
589 pr_warn_ratelimited("overlayfs: orphan index entry (%pd2, ftype=%x, nlink=%u)\n",
590 index, d_inode(index)->i_mode & S_IFMT,
591 d_inode(index)->i_nlink);
592 err = -ENOENT;
593 goto out;
Amir Goldstein415543d2017-06-21 15:28:42 +0300594}
595
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200596static int ovl_get_index_name_fh(struct ovl_fh *fh, struct qstr *name)
597{
598 char *n, *s;
599
600 n = kzalloc(fh->len * 2, GFP_KERNEL);
601 if (!n)
602 return -ENOMEM;
603
604 s = bin2hex(n, fh, fh->len);
605 *name = (struct qstr) QSTR_INIT(n, s - n);
606
607 return 0;
608
609}
610
Amir Goldstein415543d2017-06-21 15:28:42 +0300611/*
Amir Goldstein359f3922017-06-21 15:28:41 +0300612 * Lookup in indexdir for the index entry of a lower real inode or a copy up
613 * origin inode. The index entry name is the hex representation of the lower
614 * inode file handle.
615 *
616 * If the index dentry in negative, then either no lower aliases have been
617 * copied up yet, or aliases have been copied up in older kernels and are
618 * not indexed.
619 *
620 * If the index dentry for a copy up origin inode is positive, but points
621 * to an inode different than the upper inode, then either the upper inode
622 * has been copied up and not indexed or it was indexed, but since then
623 * index dir was cleared. Either way, that index cannot be used to indentify
624 * the overlay inode.
625 */
626int ovl_get_index_name(struct dentry *origin, struct qstr *name)
627{
Amir Goldstein359f3922017-06-21 15:28:41 +0300628 struct ovl_fh *fh;
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200629 int err;
Amir Goldstein359f3922017-06-21 15:28:41 +0300630
Amir Goldstein5b2cccd2018-02-02 10:42:03 +0200631 fh = ovl_encode_real_fh(origin, false);
Amir Goldstein359f3922017-06-21 15:28:41 +0300632 if (IS_ERR(fh))
633 return PTR_ERR(fh);
634
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200635 err = ovl_get_index_name_fh(fh, name);
636
Amir Goldstein359f3922017-06-21 15:28:41 +0300637 kfree(fh);
Amir Goldstein359f3922017-06-21 15:28:41 +0300638 return err;
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200639}
Amir Goldstein359f3922017-06-21 15:28:41 +0300640
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200641/* Lookup index by file handle for NFS export */
642struct dentry *ovl_get_index_fh(struct ovl_fs *ofs, struct ovl_fh *fh)
643{
644 struct dentry *index;
645 struct qstr name;
646 int err;
647
648 err = ovl_get_index_name_fh(fh, &name);
649 if (err)
650 return ERR_PTR(err);
651
652 index = lookup_one_len_unlocked(name.name, ofs->indexdir, name.len);
653 kfree(name.name);
654 if (IS_ERR(index)) {
655 if (PTR_ERR(index) == -ENOENT)
656 index = NULL;
657 return index;
658 }
659
660 if (d_is_negative(index))
661 err = 0;
662 else if (ovl_is_whiteout(index))
663 err = -ESTALE;
664 else if (ovl_dentry_weird(index))
665 err = -EIO;
666 else
667 return index;
668
669 dput(index);
670 return ERR_PTR(err);
Amir Goldstein359f3922017-06-21 15:28:41 +0300671}
672
Amir Goldstein06170152018-01-17 14:40:27 +0200673struct dentry *ovl_lookup_index(struct ovl_fs *ofs, struct dentry *upper,
674 struct dentry *origin, bool verify)
Amir Goldstein359f3922017-06-21 15:28:41 +0300675{
Amir Goldstein359f3922017-06-21 15:28:41 +0300676 struct dentry *index;
677 struct inode *inode;
678 struct qstr name;
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200679 bool is_dir = d_is_dir(origin);
Amir Goldstein359f3922017-06-21 15:28:41 +0300680 int err;
681
682 err = ovl_get_index_name(origin, &name);
683 if (err)
684 return ERR_PTR(err);
685
686 index = lookup_one_len_unlocked(name.name, ofs->indexdir, name.len);
687 if (IS_ERR(index)) {
Amir Goldsteine0082a02017-09-24 13:01:35 +0300688 err = PTR_ERR(index);
Amir Goldstein7937a562017-10-20 17:19:06 +0300689 if (err == -ENOENT) {
690 index = NULL;
691 goto out;
692 }
Amir Goldstein359f3922017-06-21 15:28:41 +0300693 pr_warn_ratelimited("overlayfs: failed inode index lookup (ino=%lu, key=%*s, err=%i);\n"
694 "overlayfs: mount with '-o index=off' to disable inodes index.\n",
695 d_inode(origin)->i_ino, name.len, name.name,
696 err);
697 goto out;
698 }
699
Amir Goldstein0e082552017-07-18 21:07:43 +0300700 inode = d_inode(index);
Amir Goldstein359f3922017-06-21 15:28:41 +0300701 if (d_is_negative(index)) {
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300702 goto out_dput;
Amir Goldstein06170152018-01-17 14:40:27 +0200703 } else if (ovl_is_whiteout(index) && !verify) {
704 /*
705 * When index lookup is called with !verify for decoding an
706 * overlay file handle, a whiteout index implies that decode
707 * should treat file handle as stale and no need to print a
708 * warning about it.
709 */
710 dput(index);
711 index = ERR_PTR(-ESTALE);
712 goto out;
Amir Goldstein0e082552017-07-18 21:07:43 +0300713 } else if (ovl_dentry_weird(index) || ovl_is_whiteout(index) ||
714 ((inode->i_mode ^ d_inode(origin)->i_mode) & S_IFMT)) {
715 /*
716 * Index should always be of the same file type as origin
717 * except for the case of a whiteout index. A whiteout
718 * index should only exist if all lower aliases have been
719 * unlinked, which means that finding a lower origin on lookup
720 * whose index is a whiteout should be treated as an error.
721 */
722 pr_warn_ratelimited("overlayfs: bad index found (index=%pd2, ftype=%x, origin ftype=%x).\n",
723 index, d_inode(index)->i_mode & S_IFMT,
724 d_inode(origin)->i_mode & S_IFMT);
Amir Goldstein359f3922017-06-21 15:28:41 +0300725 goto fail;
Amir Goldstein06170152018-01-17 14:40:27 +0200726 } else if (is_dir && verify) {
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200727 if (!upper) {
728 pr_warn_ratelimited("overlayfs: suspected uncovered redirected dir found (origin=%pd2, index=%pd2).\n",
729 origin, index);
730 goto fail;
731 }
Amir Goldstein359f3922017-06-21 15:28:41 +0300732
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200733 /* Verify that dir index 'upper' xattr points to upper dir */
734 err = ovl_verify_upper(index, upper, false);
735 if (err) {
736 if (err == -ESTALE) {
737 pr_warn_ratelimited("overlayfs: suspected multiply redirected dir found (upper=%pd2, origin=%pd2, index=%pd2).\n",
738 upper, origin, index);
739 }
740 goto fail;
741 }
742 } else if (upper && d_inode(upper) != inode) {
743 goto out_dput;
744 }
Amir Goldstein359f3922017-06-21 15:28:41 +0300745out:
746 kfree(name.name);
747 return index;
748
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300749out_dput:
750 dput(index);
751 index = NULL;
752 goto out;
753
Amir Goldstein359f3922017-06-21 15:28:41 +0300754fail:
755 dput(index);
756 index = ERR_PTR(-EIO);
757 goto out;
758}
759
760/*
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100761 * Returns next layer in stack starting from top.
762 * Returns -1 if this is the last layer.
763 */
764int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
765{
766 struct ovl_entry *oe = dentry->d_fsdata;
767
768 BUG_ON(idx < 0);
769 if (idx == 0) {
770 ovl_path_upper(dentry, path);
771 if (path->dentry)
772 return oe->numlower ? 1 : -1;
773 idx++;
774 }
775 BUG_ON(idx > oe->numlower);
Chandan Rajendrab9343632017-07-24 01:57:54 -0500776 path->dentry = oe->lowerstack[idx - 1].dentry;
777 path->mnt = oe->lowerstack[idx - 1].layer->mnt;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100778
779 return (idx < oe->numlower) ? idx + 1 : -1;
780}
781
Amir Goldstein9678e632018-01-03 19:34:45 +0200782/* Fix missing 'origin' xattr */
783static int ovl_fix_origin(struct dentry *dentry, struct dentry *lower,
784 struct dentry *upper)
785{
786 int err;
787
788 if (ovl_check_origin_xattr(upper))
789 return 0;
790
791 err = ovl_want_write(dentry);
792 if (err)
793 return err;
794
795 err = ovl_set_origin(dentry, lower, upper);
796 if (!err)
797 err = ovl_set_impure(dentry->d_parent, upper->d_parent);
798
799 ovl_drop_write(dentry);
800 return err;
801}
802
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100803struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
804 unsigned int flags)
805{
806 struct ovl_entry *oe;
807 const struct cred *old_cred;
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100808 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100809 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
Amir Goldsteinc22205d2017-04-26 23:40:52 +0300810 struct ovl_entry *roe = dentry->d_sb->s_root->d_fsdata;
Chandan Rajendrab9343632017-07-24 01:57:54 -0500811 struct ovl_path *stack = NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100812 struct dentry *upperdir, *upperdentry = NULL;
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200813 struct dentry *origin = NULL;
Amir Goldstein359f3922017-06-21 15:28:41 +0300814 struct dentry *index = NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100815 unsigned int ctr = 0;
816 struct inode *inode = NULL;
817 bool upperopaque = false;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100818 char *upperredirect = NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100819 struct dentry *this;
820 unsigned int i;
821 int err;
Miklos Szeredie28edc42016-12-16 11:02:56 +0100822 struct ovl_lookup_data d = {
823 .name = dentry->d_name,
824 .is_dir = false,
825 .opaque = false,
826 .stop = false,
Vivek Goyal452061f2018-03-09 15:44:41 -0500827 .last = ofs->config.redirect_follow ? false : !poe->numlower,
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100828 .redirect = NULL,
Miklos Szeredie28edc42016-12-16 11:02:56 +0100829 };
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100830
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100831 if (dentry->d_name.len > ofs->namelen)
832 return ERR_PTR(-ENAMETOOLONG);
833
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100834 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200835 upperdir = ovl_dentry_upper(dentry->d_parent);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100836 if (upperdir) {
Miklos Szeredie28edc42016-12-16 11:02:56 +0100837 err = ovl_lookup_layer(upperdir, &d, &upperdentry);
838 if (err)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100839 goto out;
840
Miklos Szeredie28edc42016-12-16 11:02:56 +0100841 if (upperdentry && unlikely(ovl_dentry_remote(upperdentry))) {
842 dput(upperdentry);
843 err = -EREMOTE;
844 goto out;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100845 }
Amir Goldsteina9d01952017-04-30 14:46:31 +0300846 if (upperdentry && !d.is_dir) {
847 BUG_ON(!d.stop || d.redirect);
Amir Goldsteinf7d3dac2017-06-21 15:28:34 +0300848 /*
849 * Lookup copy up origin by decoding origin file handle.
850 * We may get a disconnected dentry, which is fine,
851 * because we only need to hold the origin inode in
852 * cache and use its inode number. We may even get a
853 * connected dentry, that is not under any of the lower
854 * layers root. That is also fine for using it's inode
855 * number - it's the same as if we held a reference
856 * to a dentry in lower layer that was moved under us.
857 */
Amir Goldstein1eff1a12017-12-12 22:40:46 +0200858 err = ovl_check_origin(ofs, upperdentry, &stack, &ctr);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300859 if (err)
Vivek Goyal5455f922017-11-01 15:37:22 -0400860 goto out_put_upper;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300861 }
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100862
863 if (d.redirect) {
Dan Carpenter0ce5cdc2017-09-22 23:45:18 +0300864 err = -ENOMEM;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100865 upperredirect = kstrdup(d.redirect, GFP_KERNEL);
866 if (!upperredirect)
867 goto out_put_upper;
868 if (d.redirect[0] == '/')
Amir Goldsteinc22205d2017-04-26 23:40:52 +0300869 poe = roe;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100870 }
Miklos Szeredie28edc42016-12-16 11:02:56 +0100871 upperopaque = d.opaque;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100872 }
873
Miklos Szeredie28edc42016-12-16 11:02:56 +0100874 if (!d.stop && poe->numlower) {
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100875 err = -ENOMEM;
Chandan Rajendrab9343632017-07-24 01:57:54 -0500876 stack = kcalloc(ofs->numlower, sizeof(struct ovl_path),
Michal Hocko0ee931c2017-09-13 16:28:29 -0700877 GFP_KERNEL);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100878 if (!stack)
879 goto out_put_upper;
880 }
881
Miklos Szeredie28edc42016-12-16 11:02:56 +0100882 for (i = 0; !d.stop && i < poe->numlower; i++) {
Chandan Rajendrab9343632017-07-24 01:57:54 -0500883 struct ovl_path lower = poe->lowerstack[i];
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100884
Vivek Goyal452061f2018-03-09 15:44:41 -0500885 if (!ofs->config.redirect_follow)
886 d.last = i == poe->numlower - 1;
887 else
888 d.last = lower.layer->idx == roe->numlower;
889
Chandan Rajendrab9343632017-07-24 01:57:54 -0500890 err = ovl_lookup_layer(lower.dentry, &d, &this);
Miklos Szeredie28edc42016-12-16 11:02:56 +0100891 if (err)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100892 goto out_put;
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100893
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100894 if (!this)
895 continue;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100896
Amir Goldstein9678e632018-01-03 19:34:45 +0200897 /*
898 * If no origin fh is stored in upper of a merge dir, store fh
899 * of lower dir and set upper parent "impure".
900 */
901 if (upperdentry && !ctr && !ofs->noxattr) {
902 err = ovl_fix_origin(dentry, this, upperdentry);
903 if (err) {
904 dput(this);
905 goto out_put;
906 }
907 }
908
Amir Goldstein37b129162018-01-10 22:29:38 +0200909 /*
910 * When "verify_lower" feature is enabled, do not merge with a
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200911 * lower dir that does not match a stored origin xattr. In any
912 * case, only verified origin is used for index lookup.
Amir Goldstein37b129162018-01-10 22:29:38 +0200913 */
914 if (upperdentry && !ctr && ovl_verify_lower(dentry->d_sb)) {
915 err = ovl_verify_origin(upperdentry, this, false);
916 if (err) {
917 dput(this);
918 break;
919 }
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200920
921 /* Bless lower dir as verified origin */
922 origin = this;
Amir Goldstein37b129162018-01-10 22:29:38 +0200923 }
924
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100925 stack[ctr].dentry = this;
Chandan Rajendrab9343632017-07-24 01:57:54 -0500926 stack[ctr].layer = lower.layer;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100927 ctr++;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100928
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100929 /*
930 * Following redirects can have security consequences: it's like
931 * a symlink into the lower layer without the permission checks.
932 * This is only a problem if the upper layer is untrusted (e.g
933 * comes from an USB drive). This can allow a non-readable file
934 * or directory to become readable.
935 *
936 * Only following redirects when redirects are enabled disables
937 * this attack vector when not necessary.
938 */
939 err = -EPERM;
940 if (d.redirect && !ofs->config.redirect_follow) {
Amir Goldsteinf8167812017-12-18 14:25:56 +0200941 pr_warn_ratelimited("overlayfs: refusing to follow redirect for (%pd2)\n",
942 dentry);
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100943 goto out_put;
944 }
945
Vivek Goyald1fe96c2018-02-02 10:23:24 -0500946 if (d.stop)
947 break;
948
Amir Goldsteinc22205d2017-04-26 23:40:52 +0300949 if (d.redirect && d.redirect[0] == '/' && poe != roe) {
950 poe = roe;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100951 /* Find the current layer on the root dentry */
Amir Goldsteind583ed72017-11-08 19:23:36 +0200952 i = lower.layer->idx - 1;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100953 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100954 }
955
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200956 /*
957 * Lookup index by lower inode and verify it matches upper inode.
958 * We only trust dir index if we verified that lower dir matches
959 * origin, otherwise dir index entries may be inconsistent and we
960 * ignore them. Always lookup index of non-dir and non-upper.
961 */
962 if (ctr && (!upperdentry || !d.is_dir))
963 origin = stack[0].dentry;
Amir Goldstein359f3922017-06-21 15:28:41 +0300964
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200965 if (origin && ovl_indexdir(dentry->d_sb) &&
966 (!d.is_dir || ovl_index_all(dentry->d_sb))) {
Amir Goldstein06170152018-01-17 14:40:27 +0200967 index = ovl_lookup_index(ofs, upperdentry, origin, true);
Amir Goldstein359f3922017-06-21 15:28:41 +0300968 if (IS_ERR(index)) {
969 err = PTR_ERR(index);
970 index = NULL;
971 goto out_put;
972 }
973 }
974
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100975 oe = ovl_alloc_entry(ctr);
976 err = -ENOMEM;
977 if (!oe)
978 goto out_put;
979
Chandan Rajendrab9343632017-07-24 01:57:54 -0500980 memcpy(oe->lowerstack, stack, sizeof(struct ovl_path) * ctr);
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200981 dentry->d_fsdata = oe;
982
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200983 if (upperopaque)
984 ovl_dentry_set_opaque(dentry);
985
Miklos Szeredi55acc662017-07-04 22:03:18 +0200986 if (upperdentry)
987 ovl_dentry_set_upper_alias(dentry);
988 else if (index)
Amir Goldstein359f3922017-06-21 15:28:41 +0300989 upperdentry = dget(index);
990
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200991 if (upperdentry || ctr) {
Amir Goldstein2aed4892018-01-28 02:35:48 +0200992 if (ctr)
993 origin = stack[0].dentry;
Amir Goldstein0aceb532017-12-12 23:43:16 +0200994 inode = ovl_get_inode(dentry->d_sb, upperdentry, origin, index,
995 ctr);
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200996 err = PTR_ERR(inode);
997 if (IS_ERR(inode))
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200998 goto out_free_oe;
Miklos Szeredicf31c462017-07-04 22:03:16 +0200999
1000 OVL_I(inode)->redirect = upperredirect;
Amir Goldstein359f3922017-06-21 15:28:41 +03001001 if (index)
1002 ovl_set_flag(OVL_INDEX, inode);
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +02001003 }
1004
1005 revert_creds(old_cred);
Amir Goldstein359f3922017-06-21 15:28:41 +03001006 dput(index);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001007 kfree(stack);
Miklos Szeredi02b69b22016-12-16 11:02:56 +01001008 kfree(d.redirect);
Amir Goldstein829c28b2017-09-29 21:43:07 +03001009 return d_splice_alias(inode, dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001010
1011out_free_oe:
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +02001012 dentry->d_fsdata = NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001013 kfree(oe);
1014out_put:
Amir Goldstein359f3922017-06-21 15:28:41 +03001015 dput(index);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001016 for (i = 0; i < ctr; i++)
1017 dput(stack[i].dentry);
1018 kfree(stack);
1019out_put_upper:
1020 dput(upperdentry);
Miklos Szeredi02b69b22016-12-16 11:02:56 +01001021 kfree(upperredirect);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001022out:
Miklos Szeredi02b69b22016-12-16 11:02:56 +01001023 kfree(d.redirect);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001024 revert_creds(old_cred);
1025 return ERR_PTR(err);
1026}
1027
1028bool ovl_lower_positive(struct dentry *dentry)
1029{
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001030 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
1031 const struct qstr *name = &dentry->d_name;
Amir Goldstein6d0a8a92017-11-10 13:18:07 +02001032 const struct cred *old_cred;
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001033 unsigned int i;
1034 bool positive = false;
1035 bool done = false;
1036
1037 /*
1038 * If dentry is negative, then lower is positive iff this is a
1039 * whiteout.
1040 */
1041 if (!dentry->d_inode)
Amir Goldsteinc62520a2018-01-14 19:25:31 +02001042 return ovl_dentry_is_opaque(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001043
1044 /* Negative upper -> positive lower */
Miklos Szeredi09d8b582017-07-04 22:03:16 +02001045 if (!ovl_dentry_upper(dentry))
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001046 return true;
1047
Amir Goldstein6d0a8a92017-11-10 13:18:07 +02001048 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001049 /* Positive upper -> have to look up lower to see whether it exists */
1050 for (i = 0; !done && !positive && i < poe->numlower; i++) {
1051 struct dentry *this;
1052 struct dentry *lowerdir = poe->lowerstack[i].dentry;
1053
1054 this = lookup_one_len_unlocked(name->name, lowerdir,
1055 name->len);
1056 if (IS_ERR(this)) {
1057 switch (PTR_ERR(this)) {
1058 case -ENOENT:
1059 case -ENAMETOOLONG:
1060 break;
1061
1062 default:
1063 /*
1064 * Assume something is there, we just couldn't
1065 * access it.
1066 */
1067 positive = true;
1068 break;
1069 }
1070 } else {
1071 if (this->d_inode) {
1072 positive = !ovl_is_whiteout(this);
1073 done = true;
1074 }
1075 dput(this);
1076 }
1077 }
Amir Goldstein6d0a8a92017-11-10 13:18:07 +02001078 revert_creds(old_cred);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001079
1080 return positive;
1081}