blob: 49984c9f368980fbf433bd540d5b9e0200591533 [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 }
59 } else {
60 if (strchr(buf, '/') != NULL)
61 goto invalid;
62
63 memmove(buf + prelen, buf, res);
64 memcpy(buf, d->name.name, prelen);
65 }
66
67 strcat(buf, post);
68 kfree(d->redirect);
69 d->redirect = buf;
70 d->name.name = d->redirect;
71 d->name.len = strlen(d->redirect);
72
73 return 0;
74
75err_free:
76 kfree(buf);
77 return 0;
78fail:
79 pr_warn_ratelimited("overlayfs: failed to get redirect (%i)\n", res);
80 goto err_free;
81invalid:
82 pr_warn_ratelimited("overlayfs: invalid redirect (%s)\n", buf);
83 goto err_free;
84}
85
Amir Goldsteina9d01952017-04-30 14:46:31 +030086static int ovl_acceptable(void *ctx, struct dentry *dentry)
87{
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +020088 /*
89 * A non-dir origin may be disconnected, which is fine, because
90 * we only need it for its unique inode number.
91 */
92 if (!d_is_dir(dentry))
93 return 1;
94
95 /* Don't decode a deleted empty directory */
96 if (d_unhashed(dentry))
97 return 0;
98
99 /* Check if directory belongs to the layer we are decoding from */
100 return is_subdir(dentry, ((struct vfsmount *)ctx)->mnt_root);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300101}
102
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300103/*
104 * Check validity of an overlay file handle buffer.
105 *
106 * Return 0 for a valid file handle.
107 * Return -ENODATA for "origin unknown".
108 * Return <0 for an invalid file handle.
109 */
110static int ovl_check_fh_len(struct ovl_fh *fh, int fh_len)
111{
112 if (fh_len < sizeof(struct ovl_fh) || fh_len < fh->len)
113 return -EINVAL;
114
115 if (fh->magic != OVL_FH_MAGIC)
116 return -EINVAL;
117
118 /* Treat larger version and unknown flags as "origin unknown" */
119 if (fh->version > OVL_FH_VERSION || fh->flags & ~OVL_FH_FLAG_ALL)
120 return -ENODATA;
121
122 /* Treat endianness mismatch as "origin unknown" */
123 if (!(fh->flags & OVL_FH_FLAG_ANY_ENDIAN) &&
124 (fh->flags & OVL_FH_FLAG_BIG_ENDIAN) != OVL_FH_FLAG_CPU_ENDIAN)
125 return -ENODATA;
126
127 return 0;
128}
129
Amir Goldstein05122442018-01-11 08:25:32 +0200130static struct ovl_fh *ovl_get_fh(struct dentry *dentry, const char *name)
Amir Goldsteina9d01952017-04-30 14:46:31 +0300131{
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300132 int res, err;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300133 struct ovl_fh *fh = NULL;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300134
Amir Goldstein05122442018-01-11 08:25:32 +0200135 res = vfs_getxattr(dentry, name, NULL, 0);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300136 if (res < 0) {
137 if (res == -ENODATA || res == -EOPNOTSUPP)
138 return NULL;
139 goto fail;
140 }
141 /* Zero size value means "copied up but origin unknown" */
142 if (res == 0)
143 return NULL;
144
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300145 fh = kzalloc(res, GFP_KERNEL);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300146 if (!fh)
147 return ERR_PTR(-ENOMEM);
148
Amir Goldstein05122442018-01-11 08:25:32 +0200149 res = vfs_getxattr(dentry, name, fh, res);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300150 if (res < 0)
151 goto fail;
152
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300153 err = ovl_check_fh_len(fh, res);
154 if (err < 0) {
155 if (err == -ENODATA)
156 goto out;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300157 goto invalid;
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300158 }
Amir Goldsteina9d01952017-04-30 14:46:31 +0300159
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300160 return fh;
161
162out:
163 kfree(fh);
164 return NULL;
165
166fail:
167 pr_warn_ratelimited("overlayfs: failed to get origin (%i)\n", res);
168 goto out;
169invalid:
170 pr_warn_ratelimited("overlayfs: invalid origin (%*phN)\n", res, fh);
171 goto out;
172}
173
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300174static struct dentry *ovl_decode_fh(struct ovl_fh *fh, struct vfsmount *mnt)
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300175{
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200176 struct dentry *real;
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300177 int bytes;
178
Amir Goldsteina9d01952017-04-30 14:46:31 +0300179 /*
180 * Make sure that the stored uuid matches the uuid of the lower
181 * layer where file handle will be decoded.
182 */
Christoph Hellwig85787092017-05-10 15:06:33 +0200183 if (!uuid_equal(&fh->uuid, &mnt->mnt_sb->s_uuid))
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300184 return NULL;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300185
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300186 bytes = (fh->len - offsetof(struct ovl_fh, fid));
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200187 real = exportfs_decode_fh(mnt, (struct fid *)fh->fid,
188 bytes >> 2, (int)fh->type,
189 ovl_acceptable, mnt);
190 if (IS_ERR(real)) {
191 /*
192 * Treat stale file handle to lower file as "origin unknown".
193 * upper file handle could become stale when upper file is
194 * unlinked and this information is needed to handle stale
195 * index entries correctly.
196 */
197 if (real == ERR_PTR(-ESTALE) &&
198 !(fh->flags & OVL_FH_FLAG_PATH_UPPER))
199 real = NULL;
200 return real;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300201 }
202
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200203 if (ovl_dentry_weird(real)) {
204 dput(real);
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300205 return NULL;
206 }
Amir Goldsteina9d01952017-04-30 14:46:31 +0300207
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200208 return real;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300209}
210
Amir Goldsteinee1d6d372017-05-11 16:42:26 +0300211static bool ovl_is_opaquedir(struct dentry *dentry)
212{
213 return ovl_check_dir_xattr(dentry, OVL_XATTR_OPAQUE);
214}
215
Miklos Szeredie28edc42016-12-16 11:02:56 +0100216static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d,
217 const char *name, unsigned int namelen,
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100218 size_t prelen, const char *post,
Miklos Szeredie28edc42016-12-16 11:02:56 +0100219 struct dentry **ret)
220{
221 struct dentry *this;
222 int err;
223
224 this = lookup_one_len_unlocked(name, base, namelen);
225 if (IS_ERR(this)) {
226 err = PTR_ERR(this);
227 this = NULL;
228 if (err == -ENOENT || err == -ENAMETOOLONG)
229 goto out;
230 goto out_err;
231 }
232 if (!this->d_inode)
233 goto put_and_out;
234
235 if (ovl_dentry_weird(this)) {
236 /* Don't support traversing automounts and other weirdness */
237 err = -EREMOTE;
238 goto out_err;
239 }
240 if (ovl_is_whiteout(this)) {
241 d->stop = d->opaque = true;
242 goto put_and_out;
243 }
244 if (!d_can_lookup(this)) {
245 d->stop = true;
246 if (d->is_dir)
247 goto put_and_out;
248 goto out;
249 }
250 d->is_dir = true;
251 if (!d->last && ovl_is_opaquedir(this)) {
252 d->stop = d->opaque = true;
253 goto out;
254 }
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100255 err = ovl_check_redirect(this, d, prelen, post);
256 if (err)
257 goto out_err;
Miklos Szeredie28edc42016-12-16 11:02:56 +0100258out:
259 *ret = this;
260 return 0;
261
262put_and_out:
263 dput(this);
264 this = NULL;
265 goto out;
266
267out_err:
268 dput(this);
269 return err;
270}
271
272static int ovl_lookup_layer(struct dentry *base, struct ovl_lookup_data *d,
273 struct dentry **ret)
274{
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100275 /* Counting down from the end, since the prefix can change */
276 size_t rem = d->name.len - 1;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100277 struct dentry *dentry = NULL;
278 int err;
279
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100280 if (d->name.name[0] != '/')
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100281 return ovl_lookup_single(base, d, d->name.name, d->name.len,
282 0, "", ret);
283
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100284 while (!IS_ERR_OR_NULL(base) && d_can_lookup(base)) {
285 const char *s = d->name.name + d->name.len - rem;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100286 const char *next = strchrnul(s, '/');
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100287 size_t thislen = next - s;
288 bool end = !next[0];
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100289
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100290 /* Verify we did not go off the rails */
291 if (WARN_ON(s[-1] != '/'))
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100292 return -EIO;
293
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100294 err = ovl_lookup_single(base, d, s, thislen,
295 d->name.len - rem, next, &base);
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100296 dput(dentry);
297 if (err)
298 return err;
299 dentry = base;
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100300 if (end)
301 break;
302
303 rem -= thislen + 1;
304
305 if (WARN_ON(rem >= d->name.len))
306 return -EIO;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100307 }
308 *ret = dentry;
309 return 0;
Miklos Szeredie28edc42016-12-16 11:02:56 +0100310}
311
Amir Goldsteina9d01952017-04-30 14:46:31 +0300312
Amir Goldstein1eff1a12017-12-12 22:40:46 +0200313static int ovl_check_origin_fh(struct ovl_fs *ofs, struct ovl_fh *fh,
314 struct dentry *upperdentry,
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300315 struct ovl_path **stackp)
Amir Goldsteina9d01952017-04-30 14:46:31 +0300316{
Amir Goldsteinf7d3dac2017-06-21 15:28:34 +0300317 struct dentry *origin = NULL;
318 int i;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300319
Amir Goldstein1eff1a12017-12-12 22:40:46 +0200320 for (i = 0; i < ofs->numlower; i++) {
321 origin = ovl_decode_fh(fh, ofs->lower_layers[i].mnt);
Amir Goldsteinf7d3dac2017-06-21 15:28:34 +0300322 if (origin)
323 break;
324 }
325
326 if (!origin)
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300327 return -ESTALE;
328 else if (IS_ERR(origin))
329 return PTR_ERR(origin);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300330
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300331 if (!ovl_is_whiteout(upperdentry) &&
332 ((d_inode(origin)->i_mode ^ d_inode(upperdentry)->i_mode) & S_IFMT))
333 goto invalid;
334
Amir Goldstein415543d2017-06-21 15:28:42 +0300335 if (!*stackp)
Chandan Rajendrab9343632017-07-24 01:57:54 -0500336 *stackp = kmalloc(sizeof(struct ovl_path), GFP_KERNEL);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300337 if (!*stackp) {
338 dput(origin);
339 return -ENOMEM;
340 }
Amir Goldstein1eff1a12017-12-12 22:40:46 +0200341 **stackp = (struct ovl_path){
342 .dentry = origin,
343 .layer = &ofs->lower_layers[i]
344 };
Amir Goldsteina9d01952017-04-30 14:46:31 +0300345
346 return 0;
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300347
348invalid:
349 pr_warn_ratelimited("overlayfs: invalid origin (%pd2, ftype=%x, origin ftype=%x).\n",
350 upperdentry, d_inode(upperdentry)->i_mode & S_IFMT,
351 d_inode(origin)->i_mode & S_IFMT);
352 dput(origin);
353 return -EIO;
354}
355
Amir Goldstein1eff1a12017-12-12 22:40:46 +0200356static int ovl_check_origin(struct ovl_fs *ofs, struct dentry *upperdentry,
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300357 struct ovl_path **stackp, unsigned int *ctrp)
358{
Amir Goldstein05122442018-01-11 08:25:32 +0200359 struct ovl_fh *fh = ovl_get_fh(upperdentry, OVL_XATTR_ORIGIN);
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300360 int err;
361
362 if (IS_ERR_OR_NULL(fh))
363 return PTR_ERR(fh);
364
Amir Goldstein1eff1a12017-12-12 22:40:46 +0200365 err = ovl_check_origin_fh(ofs, fh, upperdentry, stackp);
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300366 kfree(fh);
367
368 if (err) {
369 if (err == -ESTALE)
370 return 0;
371 return err;
372 }
373
374 if (WARN_ON(*ctrp))
375 return -EIO;
376
377 *ctrp = 1;
378 return 0;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300379}
380
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100381/*
Amir Goldstein05122442018-01-11 08:25:32 +0200382 * Verify that @fh matches the file handle stored in xattr @name.
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300383 * Return 0 on match, -ESTALE on mismatch, < 0 on error.
384 */
Amir Goldstein05122442018-01-11 08:25:32 +0200385static int ovl_verify_fh(struct dentry *dentry, const char *name,
386 const struct ovl_fh *fh)
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300387{
Amir Goldstein05122442018-01-11 08:25:32 +0200388 struct ovl_fh *ofh = ovl_get_fh(dentry, name);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300389 int err = 0;
390
391 if (!ofh)
392 return -ENODATA;
393
394 if (IS_ERR(ofh))
395 return PTR_ERR(ofh);
396
397 if (fh->len != ofh->len || memcmp(fh, ofh, fh->len))
398 err = -ESTALE;
399
400 kfree(ofh);
401 return err;
402}
403
404/*
Amir Goldstein05122442018-01-11 08:25:32 +0200405 * Verify that @real dentry matches the file handle stored in xattr @name.
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300406 *
Amir Goldstein05122442018-01-11 08:25:32 +0200407 * If @set is true and there is no stored file handle, encode @real and store
408 * file handle in xattr @name.
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300409 *
Amir Goldstein05122442018-01-11 08:25:32 +0200410 * Return 0 on match, -ESTALE on mismatch, -ENODATA on no xattr, < 0 on error.
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300411 */
Amir Goldstein05122442018-01-11 08:25:32 +0200412int ovl_verify_set_fh(struct dentry *dentry, const char *name,
413 struct dentry *real, bool is_upper, bool set)
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300414{
415 struct inode *inode;
416 struct ovl_fh *fh;
417 int err;
418
Amir Goldstein05122442018-01-11 08:25:32 +0200419 fh = ovl_encode_fh(real, is_upper);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300420 err = PTR_ERR(fh);
421 if (IS_ERR(fh))
422 goto fail;
423
Amir Goldstein05122442018-01-11 08:25:32 +0200424 err = ovl_verify_fh(dentry, name, fh);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300425 if (set && err == -ENODATA)
Amir Goldstein05122442018-01-11 08:25:32 +0200426 err = ovl_do_setxattr(dentry, name, fh, fh->len, 0);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300427 if (err)
428 goto fail;
429
430out:
431 kfree(fh);
432 return err;
433
434fail:
Amir Goldstein05122442018-01-11 08:25:32 +0200435 inode = d_inode(real);
436 pr_warn_ratelimited("overlayfs: failed to verify %s (%pd2, ino=%lu, err=%i)\n",
437 is_upper ? "upper" : "origin", real,
438 inode ? inode->i_ino : 0, err);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300439 goto out;
440}
441
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200442/* Get upper dentry from index */
443static struct dentry *ovl_index_upper(struct ovl_fs *ofs, struct dentry *index)
444{
445 struct ovl_fh *fh;
446 struct dentry *upper;
447
448 if (!d_is_dir(index))
449 return dget(index);
450
451 fh = ovl_get_fh(index, OVL_XATTR_UPPER);
452 if (IS_ERR_OR_NULL(fh))
453 return ERR_CAST(fh);
454
455 upper = ovl_decode_fh(fh, ofs->upper_mnt);
456 kfree(fh);
457
458 if (IS_ERR_OR_NULL(upper))
459 return upper ?: ERR_PTR(-ESTALE);
460
461 if (!d_is_dir(upper)) {
462 pr_warn_ratelimited("overlayfs: invalid index upper (%pd2, upper=%pd2).\n",
463 index, upper);
464 dput(upper);
465 return ERR_PTR(-EIO);
466 }
467
468 return upper;
469}
470
Amir Goldstein9ee60ce2017-11-01 10:13:51 +0200471/* Is this a leftover from create/whiteout of directory index entry? */
472static bool ovl_is_temp_index(struct dentry *index)
473{
474 return index->d_name.name[0] == '#';
475}
476
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300477/*
Amir Goldstein415543d2017-06-21 15:28:42 +0300478 * Verify that an index entry name matches the origin file handle stored in
479 * OVL_XATTR_ORIGIN and that origin file handle can be decoded to lower path.
480 * Return 0 on match, -ESTALE on mismatch or stale origin, < 0 on error.
481 */
Amir Goldstein1eff1a12017-12-12 22:40:46 +0200482int ovl_verify_index(struct ovl_fs *ofs, struct dentry *index)
Amir Goldstein415543d2017-06-21 15:28:42 +0300483{
484 struct ovl_fh *fh = NULL;
485 size_t len;
Chandan Rajendrab9343632017-07-24 01:57:54 -0500486 struct ovl_path origin = { };
487 struct ovl_path *stack = &origin;
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200488 struct dentry *upper = NULL;
Amir Goldstein415543d2017-06-21 15:28:42 +0300489 int err;
490
491 if (!d_inode(index))
492 return 0;
493
Amir Goldstein9ee60ce2017-11-01 10:13:51 +0200494 /* Cleanup leftover from index create/cleanup attempt */
495 err = -ESTALE;
496 if (ovl_is_temp_index(index))
497 goto fail;
498
Amir Goldsteinfa0096e2017-10-24 12:24:11 +0300499 err = -EINVAL;
Amir Goldstein415543d2017-06-21 15:28:42 +0300500 if (index->d_name.len < sizeof(struct ovl_fh)*2)
501 goto fail;
502
503 err = -ENOMEM;
504 len = index->d_name.len / 2;
Michal Hocko0ee931c2017-09-13 16:28:29 -0700505 fh = kzalloc(len, GFP_KERNEL);
Amir Goldstein415543d2017-06-21 15:28:42 +0300506 if (!fh)
507 goto fail;
508
509 err = -EINVAL;
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300510 if (hex2bin((u8 *)fh, index->d_name.name, len))
511 goto fail;
512
513 err = ovl_check_fh_len(fh, len);
514 if (err)
Amir Goldstein415543d2017-06-21 15:28:42 +0300515 goto fail;
516
Amir Goldstein7db25d32018-01-11 11:03:13 +0200517 /*
518 * Whiteout index entries are used as an indication that an exported
519 * overlay file handle should be treated as stale (i.e. after unlink
520 * of the overlay inode). These entries contain no origin xattr.
521 */
522 if (ovl_is_whiteout(index))
523 goto out;
524
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200525 /*
526 * Verifying directory index entries are not stale is expensive, so
527 * only verify stale dir index if NFS export is enabled.
528 */
529 if (d_is_dir(index) && !ofs->config.nfs_export)
530 goto out;
531
532 /*
533 * Directory index entries should have 'upper' xattr pointing to the
534 * real upper dir. Non-dir index entries are hardlinks to the upper
535 * real inode. For non-dir index, we can read the copy up origin xattr
536 * directly from the index dentry, but for dir index we first need to
537 * decode the upper directory.
538 */
539 upper = ovl_index_upper(ofs, index);
540 if (IS_ERR_OR_NULL(upper)) {
541 err = PTR_ERR(upper);
Amir Goldstein24f0b172018-01-11 15:33:51 +0200542 /*
543 * Directory index entries with no 'upper' xattr need to be
544 * removed. When dir index entry has a stale 'upper' xattr,
545 * we assume that upper dir was removed and we treat the dir
546 * index as orphan entry that needs to be whited out.
547 */
548 if (err == -ESTALE)
549 goto orphan;
550 else if (!err)
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200551 err = -ESTALE;
552 goto fail;
553 }
554
555 err = ovl_verify_fh(upper, OVL_XATTR_ORIGIN, fh);
556 dput(upper);
Amir Goldstein415543d2017-06-21 15:28:42 +0300557 if (err)
558 goto fail;
559
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200560 /* Check if non-dir index is orphan and don't warn before cleaning it */
561 if (!d_is_dir(index) && d_inode(index)->i_nlink == 1) {
562 err = ovl_check_origin_fh(ofs, fh, index, &stack);
563 if (err)
564 goto fail;
Amir Goldstein415543d2017-06-21 15:28:42 +0300565
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200566 if (ovl_get_nlink(origin.dentry, index, 0) == 0)
Amir Goldstein24f0b172018-01-11 15:33:51 +0200567 goto orphan;
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200568 }
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300569
Amir Goldstein415543d2017-06-21 15:28:42 +0300570out:
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200571 dput(origin.dentry);
Amir Goldstein415543d2017-06-21 15:28:42 +0300572 kfree(fh);
573 return err;
574
575fail:
Amir Goldstein61b67472017-07-18 21:07:42 +0300576 pr_warn_ratelimited("overlayfs: failed to verify index (%pd2, ftype=%x, err=%i)\n",
577 index, d_inode(index)->i_mode & S_IFMT, err);
Amir Goldstein415543d2017-06-21 15:28:42 +0300578 goto out;
Amir Goldstein24f0b172018-01-11 15:33:51 +0200579
580orphan:
581 pr_warn_ratelimited("overlayfs: orphan index entry (%pd2, ftype=%x, nlink=%u)\n",
582 index, d_inode(index)->i_mode & S_IFMT,
583 d_inode(index)->i_nlink);
584 err = -ENOENT;
585 goto out;
Amir Goldstein415543d2017-06-21 15:28:42 +0300586}
587
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200588static int ovl_get_index_name_fh(struct ovl_fh *fh, struct qstr *name)
589{
590 char *n, *s;
591
592 n = kzalloc(fh->len * 2, GFP_KERNEL);
593 if (!n)
594 return -ENOMEM;
595
596 s = bin2hex(n, fh, fh->len);
597 *name = (struct qstr) QSTR_INIT(n, s - n);
598
599 return 0;
600
601}
602
Amir Goldstein415543d2017-06-21 15:28:42 +0300603/*
Amir Goldstein359f3922017-06-21 15:28:41 +0300604 * Lookup in indexdir for the index entry of a lower real inode or a copy up
605 * origin inode. The index entry name is the hex representation of the lower
606 * inode file handle.
607 *
608 * If the index dentry in negative, then either no lower aliases have been
609 * copied up yet, or aliases have been copied up in older kernels and are
610 * not indexed.
611 *
612 * If the index dentry for a copy up origin inode is positive, but points
613 * to an inode different than the upper inode, then either the upper inode
614 * has been copied up and not indexed or it was indexed, but since then
615 * index dir was cleared. Either way, that index cannot be used to indentify
616 * the overlay inode.
617 */
618int ovl_get_index_name(struct dentry *origin, struct qstr *name)
619{
Amir Goldstein359f3922017-06-21 15:28:41 +0300620 struct ovl_fh *fh;
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200621 int err;
Amir Goldstein359f3922017-06-21 15:28:41 +0300622
623 fh = ovl_encode_fh(origin, false);
624 if (IS_ERR(fh))
625 return PTR_ERR(fh);
626
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200627 err = ovl_get_index_name_fh(fh, name);
628
Amir Goldstein359f3922017-06-21 15:28:41 +0300629 kfree(fh);
Amir Goldstein359f3922017-06-21 15:28:41 +0300630 return err;
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200631}
Amir Goldstein359f3922017-06-21 15:28:41 +0300632
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200633/* Lookup index by file handle for NFS export */
634struct dentry *ovl_get_index_fh(struct ovl_fs *ofs, struct ovl_fh *fh)
635{
636 struct dentry *index;
637 struct qstr name;
638 int err;
639
640 err = ovl_get_index_name_fh(fh, &name);
641 if (err)
642 return ERR_PTR(err);
643
644 index = lookup_one_len_unlocked(name.name, ofs->indexdir, name.len);
645 kfree(name.name);
646 if (IS_ERR(index)) {
647 if (PTR_ERR(index) == -ENOENT)
648 index = NULL;
649 return index;
650 }
651
652 if (d_is_negative(index))
653 err = 0;
654 else if (ovl_is_whiteout(index))
655 err = -ESTALE;
656 else if (ovl_dentry_weird(index))
657 err = -EIO;
658 else
659 return index;
660
661 dput(index);
662 return ERR_PTR(err);
Amir Goldstein359f3922017-06-21 15:28:41 +0300663}
664
665static struct dentry *ovl_lookup_index(struct dentry *dentry,
666 struct dentry *upper,
667 struct dentry *origin)
668{
669 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
670 struct dentry *index;
671 struct inode *inode;
672 struct qstr name;
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200673 bool is_dir = d_is_dir(origin);
Amir Goldstein359f3922017-06-21 15:28:41 +0300674 int err;
675
676 err = ovl_get_index_name(origin, &name);
677 if (err)
678 return ERR_PTR(err);
679
680 index = lookup_one_len_unlocked(name.name, ofs->indexdir, name.len);
681 if (IS_ERR(index)) {
Amir Goldsteine0082a02017-09-24 13:01:35 +0300682 err = PTR_ERR(index);
Amir Goldstein7937a562017-10-20 17:19:06 +0300683 if (err == -ENOENT) {
684 index = NULL;
685 goto out;
686 }
Amir Goldstein359f3922017-06-21 15:28:41 +0300687 pr_warn_ratelimited("overlayfs: failed inode index lookup (ino=%lu, key=%*s, err=%i);\n"
688 "overlayfs: mount with '-o index=off' to disable inodes index.\n",
689 d_inode(origin)->i_ino, name.len, name.name,
690 err);
691 goto out;
692 }
693
Amir Goldstein0e082552017-07-18 21:07:43 +0300694 inode = d_inode(index);
Amir Goldstein359f3922017-06-21 15:28:41 +0300695 if (d_is_negative(index)) {
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300696 goto out_dput;
Amir Goldstein0e082552017-07-18 21:07:43 +0300697 } else if (ovl_dentry_weird(index) || ovl_is_whiteout(index) ||
698 ((inode->i_mode ^ d_inode(origin)->i_mode) & S_IFMT)) {
699 /*
700 * Index should always be of the same file type as origin
701 * except for the case of a whiteout index. A whiteout
702 * index should only exist if all lower aliases have been
703 * unlinked, which means that finding a lower origin on lookup
704 * whose index is a whiteout should be treated as an error.
705 */
706 pr_warn_ratelimited("overlayfs: bad index found (index=%pd2, ftype=%x, origin ftype=%x).\n",
707 index, d_inode(index)->i_mode & S_IFMT,
708 d_inode(origin)->i_mode & S_IFMT);
Amir Goldstein359f3922017-06-21 15:28:41 +0300709 goto fail;
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200710 } else if (is_dir) {
711 if (!upper) {
712 pr_warn_ratelimited("overlayfs: suspected uncovered redirected dir found (origin=%pd2, index=%pd2).\n",
713 origin, index);
714 goto fail;
715 }
Amir Goldstein359f3922017-06-21 15:28:41 +0300716
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200717 /* Verify that dir index 'upper' xattr points to upper dir */
718 err = ovl_verify_upper(index, upper, false);
719 if (err) {
720 if (err == -ESTALE) {
721 pr_warn_ratelimited("overlayfs: suspected multiply redirected dir found (upper=%pd2, origin=%pd2, index=%pd2).\n",
722 upper, origin, index);
723 }
724 goto fail;
725 }
726 } else if (upper && d_inode(upper) != inode) {
727 goto out_dput;
728 }
Amir Goldstein359f3922017-06-21 15:28:41 +0300729out:
730 kfree(name.name);
731 return index;
732
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300733out_dput:
734 dput(index);
735 index = NULL;
736 goto out;
737
Amir Goldstein359f3922017-06-21 15:28:41 +0300738fail:
739 dput(index);
740 index = ERR_PTR(-EIO);
741 goto out;
742}
743
744/*
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100745 * Returns next layer in stack starting from top.
746 * Returns -1 if this is the last layer.
747 */
748int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
749{
750 struct ovl_entry *oe = dentry->d_fsdata;
751
752 BUG_ON(idx < 0);
753 if (idx == 0) {
754 ovl_path_upper(dentry, path);
755 if (path->dentry)
756 return oe->numlower ? 1 : -1;
757 idx++;
758 }
759 BUG_ON(idx > oe->numlower);
Chandan Rajendrab9343632017-07-24 01:57:54 -0500760 path->dentry = oe->lowerstack[idx - 1].dentry;
761 path->mnt = oe->lowerstack[idx - 1].layer->mnt;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100762
763 return (idx < oe->numlower) ? idx + 1 : -1;
764}
765
Amir Goldstein9678e632018-01-03 19:34:45 +0200766/* Fix missing 'origin' xattr */
767static int ovl_fix_origin(struct dentry *dentry, struct dentry *lower,
768 struct dentry *upper)
769{
770 int err;
771
772 if (ovl_check_origin_xattr(upper))
773 return 0;
774
775 err = ovl_want_write(dentry);
776 if (err)
777 return err;
778
779 err = ovl_set_origin(dentry, lower, upper);
780 if (!err)
781 err = ovl_set_impure(dentry->d_parent, upper->d_parent);
782
783 ovl_drop_write(dentry);
784 return err;
785}
786
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100787struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
788 unsigned int flags)
789{
790 struct ovl_entry *oe;
791 const struct cred *old_cred;
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100792 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100793 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
Amir Goldsteinc22205d2017-04-26 23:40:52 +0300794 struct ovl_entry *roe = dentry->d_sb->s_root->d_fsdata;
Chandan Rajendrab9343632017-07-24 01:57:54 -0500795 struct ovl_path *stack = NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100796 struct dentry *upperdir, *upperdentry = NULL;
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200797 struct dentry *origin = NULL;
Amir Goldstein359f3922017-06-21 15:28:41 +0300798 struct dentry *index = NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100799 unsigned int ctr = 0;
800 struct inode *inode = NULL;
801 bool upperopaque = false;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100802 char *upperredirect = NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100803 struct dentry *this;
804 unsigned int i;
805 int err;
Miklos Szeredie28edc42016-12-16 11:02:56 +0100806 struct ovl_lookup_data d = {
807 .name = dentry->d_name,
808 .is_dir = false,
809 .opaque = false,
810 .stop = false,
811 .last = !poe->numlower,
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100812 .redirect = NULL,
Miklos Szeredie28edc42016-12-16 11:02:56 +0100813 };
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100814
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100815 if (dentry->d_name.len > ofs->namelen)
816 return ERR_PTR(-ENAMETOOLONG);
817
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100818 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200819 upperdir = ovl_dentry_upper(dentry->d_parent);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100820 if (upperdir) {
Miklos Szeredie28edc42016-12-16 11:02:56 +0100821 err = ovl_lookup_layer(upperdir, &d, &upperdentry);
822 if (err)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100823 goto out;
824
Miklos Szeredie28edc42016-12-16 11:02:56 +0100825 if (upperdentry && unlikely(ovl_dentry_remote(upperdentry))) {
826 dput(upperdentry);
827 err = -EREMOTE;
828 goto out;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100829 }
Amir Goldsteina9d01952017-04-30 14:46:31 +0300830 if (upperdentry && !d.is_dir) {
831 BUG_ON(!d.stop || d.redirect);
Amir Goldsteinf7d3dac2017-06-21 15:28:34 +0300832 /*
833 * Lookup copy up origin by decoding origin file handle.
834 * We may get a disconnected dentry, which is fine,
835 * because we only need to hold the origin inode in
836 * cache and use its inode number. We may even get a
837 * connected dentry, that is not under any of the lower
838 * layers root. That is also fine for using it's inode
839 * number - it's the same as if we held a reference
840 * to a dentry in lower layer that was moved under us.
841 */
Amir Goldstein1eff1a12017-12-12 22:40:46 +0200842 err = ovl_check_origin(ofs, upperdentry, &stack, &ctr);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300843 if (err)
Vivek Goyal5455f922017-11-01 15:37:22 -0400844 goto out_put_upper;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300845 }
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100846
847 if (d.redirect) {
Dan Carpenter0ce5cdc2017-09-22 23:45:18 +0300848 err = -ENOMEM;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100849 upperredirect = kstrdup(d.redirect, GFP_KERNEL);
850 if (!upperredirect)
851 goto out_put_upper;
852 if (d.redirect[0] == '/')
Amir Goldsteinc22205d2017-04-26 23:40:52 +0300853 poe = roe;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100854 }
Miklos Szeredie28edc42016-12-16 11:02:56 +0100855 upperopaque = d.opaque;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100856 }
857
Miklos Szeredie28edc42016-12-16 11:02:56 +0100858 if (!d.stop && poe->numlower) {
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100859 err = -ENOMEM;
Chandan Rajendrab9343632017-07-24 01:57:54 -0500860 stack = kcalloc(ofs->numlower, sizeof(struct ovl_path),
Michal Hocko0ee931c2017-09-13 16:28:29 -0700861 GFP_KERNEL);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100862 if (!stack)
863 goto out_put_upper;
864 }
865
Miklos Szeredie28edc42016-12-16 11:02:56 +0100866 for (i = 0; !d.stop && i < poe->numlower; i++) {
Chandan Rajendrab9343632017-07-24 01:57:54 -0500867 struct ovl_path lower = poe->lowerstack[i];
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100868
Miklos Szeredie28edc42016-12-16 11:02:56 +0100869 d.last = i == poe->numlower - 1;
Chandan Rajendrab9343632017-07-24 01:57:54 -0500870 err = ovl_lookup_layer(lower.dentry, &d, &this);
Miklos Szeredie28edc42016-12-16 11:02:56 +0100871 if (err)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100872 goto out_put;
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100873
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100874 if (!this)
875 continue;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100876
Amir Goldstein9678e632018-01-03 19:34:45 +0200877 /*
878 * If no origin fh is stored in upper of a merge dir, store fh
879 * of lower dir and set upper parent "impure".
880 */
881 if (upperdentry && !ctr && !ofs->noxattr) {
882 err = ovl_fix_origin(dentry, this, upperdentry);
883 if (err) {
884 dput(this);
885 goto out_put;
886 }
887 }
888
Amir Goldstein37b129162018-01-10 22:29:38 +0200889 /*
890 * When "verify_lower" feature is enabled, do not merge with a
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200891 * lower dir that does not match a stored origin xattr. In any
892 * case, only verified origin is used for index lookup.
Amir Goldstein37b129162018-01-10 22:29:38 +0200893 */
894 if (upperdentry && !ctr && ovl_verify_lower(dentry->d_sb)) {
895 err = ovl_verify_origin(upperdentry, this, false);
896 if (err) {
897 dput(this);
898 break;
899 }
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200900
901 /* Bless lower dir as verified origin */
902 origin = this;
Amir Goldstein37b129162018-01-10 22:29:38 +0200903 }
904
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100905 stack[ctr].dentry = this;
Chandan Rajendrab9343632017-07-24 01:57:54 -0500906 stack[ctr].layer = lower.layer;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100907 ctr++;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100908
909 if (d.stop)
910 break;
911
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100912 /*
913 * Following redirects can have security consequences: it's like
914 * a symlink into the lower layer without the permission checks.
915 * This is only a problem if the upper layer is untrusted (e.g
916 * comes from an USB drive). This can allow a non-readable file
917 * or directory to become readable.
918 *
919 * Only following redirects when redirects are enabled disables
920 * this attack vector when not necessary.
921 */
922 err = -EPERM;
923 if (d.redirect && !ofs->config.redirect_follow) {
Amir Goldsteinf8167812017-12-18 14:25:56 +0200924 pr_warn_ratelimited("overlayfs: refusing to follow redirect for (%pd2)\n",
925 dentry);
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100926 goto out_put;
927 }
928
Amir Goldsteinc22205d2017-04-26 23:40:52 +0300929 if (d.redirect && d.redirect[0] == '/' && poe != roe) {
930 poe = roe;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100931 /* Find the current layer on the root dentry */
Amir Goldsteind583ed72017-11-08 19:23:36 +0200932 i = lower.layer->idx - 1;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100933 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100934 }
935
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200936 /*
937 * Lookup index by lower inode and verify it matches upper inode.
938 * We only trust dir index if we verified that lower dir matches
939 * origin, otherwise dir index entries may be inconsistent and we
940 * ignore them. Always lookup index of non-dir and non-upper.
941 */
942 if (ctr && (!upperdentry || !d.is_dir))
943 origin = stack[0].dentry;
Amir Goldstein359f3922017-06-21 15:28:41 +0300944
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200945 if (origin && ovl_indexdir(dentry->d_sb) &&
946 (!d.is_dir || ovl_index_all(dentry->d_sb))) {
Amir Goldstein359f3922017-06-21 15:28:41 +0300947 index = ovl_lookup_index(dentry, upperdentry, origin);
948 if (IS_ERR(index)) {
949 err = PTR_ERR(index);
950 index = NULL;
951 goto out_put;
952 }
953 }
954
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100955 oe = ovl_alloc_entry(ctr);
956 err = -ENOMEM;
957 if (!oe)
958 goto out_put;
959
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100960 oe->opaque = upperopaque;
Chandan Rajendrab9343632017-07-24 01:57:54 -0500961 memcpy(oe->lowerstack, stack, sizeof(struct ovl_path) * ctr);
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200962 dentry->d_fsdata = oe;
963
Miklos Szeredi55acc662017-07-04 22:03:18 +0200964 if (upperdentry)
965 ovl_dentry_set_upper_alias(dentry);
966 else if (index)
Amir Goldstein359f3922017-06-21 15:28:41 +0300967 upperdentry = dget(index);
968
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200969 if (upperdentry || ctr) {
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300970 inode = ovl_get_inode(dentry, upperdentry, index);
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200971 err = PTR_ERR(inode);
972 if (IS_ERR(inode))
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200973 goto out_free_oe;
Miklos Szeredicf31c462017-07-04 22:03:16 +0200974
975 OVL_I(inode)->redirect = upperredirect;
Amir Goldstein359f3922017-06-21 15:28:41 +0300976 if (index)
977 ovl_set_flag(OVL_INDEX, inode);
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200978 }
979
980 revert_creds(old_cred);
Amir Goldstein359f3922017-06-21 15:28:41 +0300981 dput(index);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100982 kfree(stack);
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100983 kfree(d.redirect);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100984 d_add(dentry, inode);
985
986 return NULL;
987
988out_free_oe:
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200989 dentry->d_fsdata = NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100990 kfree(oe);
991out_put:
Amir Goldstein359f3922017-06-21 15:28:41 +0300992 dput(index);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100993 for (i = 0; i < ctr; i++)
994 dput(stack[i].dentry);
995 kfree(stack);
996out_put_upper:
997 dput(upperdentry);
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100998 kfree(upperredirect);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100999out:
Miklos Szeredi02b69b22016-12-16 11:02:56 +01001000 kfree(d.redirect);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001001 revert_creds(old_cred);
1002 return ERR_PTR(err);
1003}
1004
1005bool ovl_lower_positive(struct dentry *dentry)
1006{
1007 struct ovl_entry *oe = dentry->d_fsdata;
1008 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
1009 const struct qstr *name = &dentry->d_name;
Amir Goldstein6d0a8a92017-11-10 13:18:07 +02001010 const struct cred *old_cred;
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001011 unsigned int i;
1012 bool positive = false;
1013 bool done = false;
1014
1015 /*
1016 * If dentry is negative, then lower is positive iff this is a
1017 * whiteout.
1018 */
1019 if (!dentry->d_inode)
1020 return oe->opaque;
1021
1022 /* Negative upper -> positive lower */
Miklos Szeredi09d8b582017-07-04 22:03:16 +02001023 if (!ovl_dentry_upper(dentry))
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001024 return true;
1025
Amir Goldstein6d0a8a92017-11-10 13:18:07 +02001026 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001027 /* Positive upper -> have to look up lower to see whether it exists */
1028 for (i = 0; !done && !positive && i < poe->numlower; i++) {
1029 struct dentry *this;
1030 struct dentry *lowerdir = poe->lowerstack[i].dentry;
1031
1032 this = lookup_one_len_unlocked(name->name, lowerdir,
1033 name->len);
1034 if (IS_ERR(this)) {
1035 switch (PTR_ERR(this)) {
1036 case -ENOENT:
1037 case -ENAMETOOLONG:
1038 break;
1039
1040 default:
1041 /*
1042 * Assume something is there, we just couldn't
1043 * access it.
1044 */
1045 positive = true;
1046 break;
1047 }
1048 } else {
1049 if (this->d_inode) {
1050 positive = !ovl_is_whiteout(this);
1051 done = true;
1052 }
1053 dput(this);
1054 }
1055 }
Amir Goldstein6d0a8a92017-11-10 13:18:07 +02001056 revert_creds(old_cred);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001057
1058 return positive;
1059}