]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - fs/overlayfs/namei.c
Merge remote-tracking branches 'asoc/topic/tas6424', 'asoc/topic/tfa9879', 'asoc...
[mirror_ubuntu-focal-kernel.git] / fs / overlayfs / namei.c
CommitLineData
bbb1e54d
MS
1/*
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>
5b825c3a 11#include <linux/cred.h>
bbb1e54d
MS
12#include <linux/namei.h>
13#include <linux/xattr.h>
02b69b28 14#include <linux/ratelimit.h>
a9d01957
AG
15#include <linux/mount.h>
16#include <linux/exportfs.h>
bbb1e54d 17#include "overlayfs.h"
bbb1e54d 18
e28edc46
MS
19struct ovl_lookup_data {
20 struct qstr name;
21 bool is_dir;
22 bool opaque;
23 bool stop;
24 bool last;
02b69b28 25 char *redirect;
e28edc46 26};
bbb1e54d 27
02b69b28
MS
28static int ovl_check_redirect(struct dentry *dentry, struct ovl_lookup_data *d,
29 size_t prelen, const char *post)
30{
31 int res;
32 char *s, *next, *buf = NULL;
33
34 res = vfs_getxattr(dentry, OVL_XATTR_REDIRECT, NULL, 0);
35 if (res < 0) {
36 if (res == -ENODATA || res == -EOPNOTSUPP)
37 return 0;
38 goto fail;
39 }
0ee931c4 40 buf = kzalloc(prelen + res + strlen(post) + 1, GFP_KERNEL);
02b69b28
MS
41 if (!buf)
42 return -ENOMEM;
43
44 if (res == 0)
45 goto invalid;
46
47 res = vfs_getxattr(dentry, OVL_XATTR_REDIRECT, buf, res);
48 if (res < 0)
49 goto fail;
50 if (res == 0)
51 goto invalid;
52 if (buf[0] == '/') {
53 for (s = buf; *s++ == '/'; s = next) {
54 next = strchrnul(s, '/');
55 if (s == next)
56 goto invalid;
57 }
58 } else {
59 if (strchr(buf, '/') != NULL)
60 goto invalid;
61
62 memmove(buf + prelen, buf, res);
63 memcpy(buf, d->name.name, prelen);
64 }
65
66 strcat(buf, post);
67 kfree(d->redirect);
68 d->redirect = buf;
69 d->name.name = d->redirect;
70 d->name.len = strlen(d->redirect);
71
72 return 0;
73
74err_free:
75 kfree(buf);
76 return 0;
77fail:
78 pr_warn_ratelimited("overlayfs: failed to get redirect (%i)\n", res);
79 goto err_free;
80invalid:
81 pr_warn_ratelimited("overlayfs: invalid redirect (%s)\n", buf);
82 goto err_free;
83}
84
a9d01957
AG
85static int ovl_acceptable(void *ctx, struct dentry *dentry)
86{
87 return 1;
88}
89
8b88a2e6 90static struct ovl_fh *ovl_get_origin_fh(struct dentry *dentry)
a9d01957
AG
91{
92 int res;
93 struct ovl_fh *fh = NULL;
a9d01957
AG
94
95 res = vfs_getxattr(dentry, OVL_XATTR_ORIGIN, NULL, 0);
96 if (res < 0) {
97 if (res == -ENODATA || res == -EOPNOTSUPP)
98 return NULL;
99 goto fail;
100 }
101 /* Zero size value means "copied up but origin unknown" */
102 if (res == 0)
103 return NULL;
104
0ee931c4 105 fh = kzalloc(res, GFP_KERNEL);
a9d01957
AG
106 if (!fh)
107 return ERR_PTR(-ENOMEM);
108
109 res = vfs_getxattr(dentry, OVL_XATTR_ORIGIN, fh, res);
110 if (res < 0)
111 goto fail;
112
113 if (res < sizeof(struct ovl_fh) || res < fh->len)
114 goto invalid;
115
116 if (fh->magic != OVL_FH_MAGIC)
117 goto invalid;
118
119 /* Treat larger version and unknown flags as "origin unknown" */
120 if (fh->version > OVL_FH_VERSION || fh->flags & ~OVL_FH_FLAG_ALL)
121 goto out;
122
123 /* Treat endianness mismatch as "origin unknown" */
124 if (!(fh->flags & OVL_FH_FLAG_ANY_ENDIAN) &&
125 (fh->flags & OVL_FH_FLAG_BIG_ENDIAN) != OVL_FH_FLAG_CPU_ENDIAN)
126 goto out;
127
8b88a2e6
AG
128 return fh;
129
130out:
131 kfree(fh);
132 return NULL;
133
134fail:
135 pr_warn_ratelimited("overlayfs: failed to get origin (%i)\n", res);
136 goto out;
137invalid:
138 pr_warn_ratelimited("overlayfs: invalid origin (%*phN)\n", res, fh);
139 goto out;
140}
141
142static struct dentry *ovl_get_origin(struct dentry *dentry,
143 struct vfsmount *mnt)
144{
145 struct dentry *origin = NULL;
146 struct ovl_fh *fh = ovl_get_origin_fh(dentry);
147 int bytes;
148
149 if (IS_ERR_OR_NULL(fh))
150 return (struct dentry *)fh;
a9d01957
AG
151
152 /*
153 * Make sure that the stored uuid matches the uuid of the lower
154 * layer where file handle will be decoded.
155 */
85787090 156 if (!uuid_equal(&fh->uuid, &mnt->mnt_sb->s_uuid))
a9d01957
AG
157 goto out;
158
8b88a2e6 159 bytes = (fh->len - offsetof(struct ovl_fh, fid));
a9d01957
AG
160 origin = exportfs_decode_fh(mnt, (struct fid *)fh->fid,
161 bytes >> 2, (int)fh->type,
162 ovl_acceptable, NULL);
163 if (IS_ERR(origin)) {
164 /* Treat stale file handle as "origin unknown" */
165 if (origin == ERR_PTR(-ESTALE))
166 origin = NULL;
167 goto out;
168 }
169
170 if (ovl_dentry_weird(origin) ||
8b88a2e6 171 ((d_inode(origin)->i_mode ^ d_inode(dentry)->i_mode) & S_IFMT))
a9d01957 172 goto invalid;
a9d01957
AG
173
174out:
175 kfree(fh);
176 return origin;
177
a9d01957 178invalid:
8b88a2e6
AG
179 pr_warn_ratelimited("overlayfs: invalid origin (%pd2)\n", origin);
180 dput(origin);
181 origin = NULL;
a9d01957
AG
182 goto out;
183}
184
ee1d6d37
AG
185static bool ovl_is_opaquedir(struct dentry *dentry)
186{
187 return ovl_check_dir_xattr(dentry, OVL_XATTR_OPAQUE);
188}
189
e28edc46
MS
190static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d,
191 const char *name, unsigned int namelen,
02b69b28 192 size_t prelen, const char *post,
e28edc46
MS
193 struct dentry **ret)
194{
195 struct dentry *this;
196 int err;
197
198 this = lookup_one_len_unlocked(name, base, namelen);
199 if (IS_ERR(this)) {
200 err = PTR_ERR(this);
201 this = NULL;
202 if (err == -ENOENT || err == -ENAMETOOLONG)
203 goto out;
204 goto out_err;
205 }
206 if (!this->d_inode)
207 goto put_and_out;
208
209 if (ovl_dentry_weird(this)) {
210 /* Don't support traversing automounts and other weirdness */
211 err = -EREMOTE;
212 goto out_err;
213 }
214 if (ovl_is_whiteout(this)) {
215 d->stop = d->opaque = true;
216 goto put_and_out;
217 }
218 if (!d_can_lookup(this)) {
219 d->stop = true;
220 if (d->is_dir)
221 goto put_and_out;
222 goto out;
223 }
224 d->is_dir = true;
225 if (!d->last && ovl_is_opaquedir(this)) {
226 d->stop = d->opaque = true;
227 goto out;
228 }
02b69b28
MS
229 err = ovl_check_redirect(this, d, prelen, post);
230 if (err)
231 goto out_err;
e28edc46
MS
232out:
233 *ret = this;
234 return 0;
235
236put_and_out:
237 dput(this);
238 this = NULL;
239 goto out;
240
241out_err:
242 dput(this);
243 return err;
244}
245
246static int ovl_lookup_layer(struct dentry *base, struct ovl_lookup_data *d,
247 struct dentry **ret)
248{
4c7d0c9c
AG
249 /* Counting down from the end, since the prefix can change */
250 size_t rem = d->name.len - 1;
02b69b28
MS
251 struct dentry *dentry = NULL;
252 int err;
253
4c7d0c9c 254 if (d->name.name[0] != '/')
02b69b28
MS
255 return ovl_lookup_single(base, d, d->name.name, d->name.len,
256 0, "", ret);
257
4c7d0c9c
AG
258 while (!IS_ERR_OR_NULL(base) && d_can_lookup(base)) {
259 const char *s = d->name.name + d->name.len - rem;
02b69b28 260 const char *next = strchrnul(s, '/');
4c7d0c9c
AG
261 size_t thislen = next - s;
262 bool end = !next[0];
02b69b28 263
4c7d0c9c
AG
264 /* Verify we did not go off the rails */
265 if (WARN_ON(s[-1] != '/'))
02b69b28
MS
266 return -EIO;
267
4c7d0c9c
AG
268 err = ovl_lookup_single(base, d, s, thislen,
269 d->name.len - rem, next, &base);
02b69b28
MS
270 dput(dentry);
271 if (err)
272 return err;
273 dentry = base;
4c7d0c9c
AG
274 if (end)
275 break;
276
277 rem -= thislen + 1;
278
279 if (WARN_ON(rem >= d->name.len))
280 return -EIO;
02b69b28
MS
281 }
282 *ret = dentry;
283 return 0;
e28edc46
MS
284}
285
a9d01957 286
415543d5 287static int ovl_check_origin(struct dentry *upperdentry,
b9343632
CR
288 struct ovl_path *lower, unsigned int numlower,
289 struct ovl_path **stackp, unsigned int *ctrp)
a9d01957 290{
a9d01957 291 struct vfsmount *mnt;
f7d3daca
AG
292 struct dentry *origin = NULL;
293 int i;
a9d01957 294
415543d5 295 for (i = 0; i < numlower; i++) {
b9343632 296 mnt = lower[i].layer->mnt;
f7d3daca
AG
297 origin = ovl_get_origin(upperdentry, mnt);
298 if (IS_ERR(origin))
299 return PTR_ERR(origin);
300
301 if (origin)
302 break;
303 }
304
305 if (!origin)
306 return 0;
a9d01957 307
415543d5
AG
308 BUG_ON(*ctrp);
309 if (!*stackp)
b9343632 310 *stackp = kmalloc(sizeof(struct ovl_path), GFP_KERNEL);
a9d01957
AG
311 if (!*stackp) {
312 dput(origin);
313 return -ENOMEM;
314 }
b9343632 315 **stackp = (struct ovl_path){.dentry = origin, .layer = lower[i].layer};
a9d01957
AG
316 *ctrp = 1;
317
318 return 0;
319}
320
8b88a2e6
AG
321/*
322 * Verify that @fh matches the origin file handle stored in OVL_XATTR_ORIGIN.
323 * Return 0 on match, -ESTALE on mismatch, < 0 on error.
324 */
325static int ovl_verify_origin_fh(struct dentry *dentry, const struct ovl_fh *fh)
326{
327 struct ovl_fh *ofh = ovl_get_origin_fh(dentry);
328 int err = 0;
329
330 if (!ofh)
331 return -ENODATA;
332
333 if (IS_ERR(ofh))
334 return PTR_ERR(ofh);
335
336 if (fh->len != ofh->len || memcmp(fh, ofh, fh->len))
337 err = -ESTALE;
338
339 kfree(ofh);
340 return err;
341}
342
343/*
344 * Verify that an inode matches the origin file handle stored in upper inode.
345 *
346 * If @set is true and there is no stored file handle, encode and store origin
347 * file handle in OVL_XATTR_ORIGIN.
348 *
349 * Return 0 on match, -ESTALE on mismatch, < 0 on error.
350 */
d9768076
AG
351int ovl_verify_origin(struct dentry *dentry, struct dentry *origin,
352 bool is_upper, bool set)
8b88a2e6
AG
353{
354 struct inode *inode;
355 struct ovl_fh *fh;
356 int err;
357
54fb347e 358 fh = ovl_encode_fh(origin, is_upper);
8b88a2e6
AG
359 err = PTR_ERR(fh);
360 if (IS_ERR(fh))
361 goto fail;
362
363 err = ovl_verify_origin_fh(dentry, fh);
364 if (set && err == -ENODATA)
365 err = ovl_do_setxattr(dentry, OVL_XATTR_ORIGIN, fh, fh->len, 0);
366 if (err)
367 goto fail;
368
369out:
370 kfree(fh);
371 return err;
372
373fail:
374 inode = d_inode(origin);
375 pr_warn_ratelimited("overlayfs: failed to verify origin (%pd2, ino=%lu, err=%i)\n",
376 origin, inode ? inode->i_ino : 0, err);
377 goto out;
378}
379
415543d5
AG
380/*
381 * Verify that an index entry name matches the origin file handle stored in
382 * OVL_XATTR_ORIGIN and that origin file handle can be decoded to lower path.
383 * Return 0 on match, -ESTALE on mismatch or stale origin, < 0 on error.
384 */
b9343632 385int ovl_verify_index(struct dentry *index, struct ovl_path *lower,
415543d5
AG
386 unsigned int numlower)
387{
388 struct ovl_fh *fh = NULL;
389 size_t len;
b9343632
CR
390 struct ovl_path origin = { };
391 struct ovl_path *stack = &origin;
415543d5
AG
392 unsigned int ctr = 0;
393 int err;
394
395 if (!d_inode(index))
396 return 0;
397
61b67471
AG
398 /*
399 * Directory index entries are going to be used for looking up
400 * redirected upper dirs by lower dir fh when decoding an overlay
401 * file handle of a merge dir. Whiteout index entries are going to be
402 * used as an indication that an exported overlay file handle should
403 * be treated as stale (i.e. after unlink of the overlay inode).
404 * We don't know the verification rules for directory and whiteout
405 * index entries, because they have not been implemented yet, so return
fa0096e3
AG
406 * EINVAL if those entries are found to abort the mount to avoid
407 * corrupting an index that was created by a newer kernel.
61b67471 408 */
fa0096e3 409 err = -EINVAL;
61b67471 410 if (d_is_dir(index) || ovl_is_whiteout(index))
415543d5
AG
411 goto fail;
412
415543d5
AG
413 if (index->d_name.len < sizeof(struct ovl_fh)*2)
414 goto fail;
415
416 err = -ENOMEM;
417 len = index->d_name.len / 2;
0ee931c4 418 fh = kzalloc(len, GFP_KERNEL);
415543d5
AG
419 if (!fh)
420 goto fail;
421
422 err = -EINVAL;
423 if (hex2bin((u8 *)fh, index->d_name.name, len) || len != fh->len)
424 goto fail;
425
426 err = ovl_verify_origin_fh(index, fh);
427 if (err)
428 goto fail;
429
b9343632 430 err = ovl_check_origin(index, lower, numlower, &stack, &ctr);
415543d5
AG
431 if (!err && !ctr)
432 err = -ESTALE;
433 if (err)
434 goto fail;
435
caf70cb2
AG
436 /* Check if index is orphan and don't warn before cleaning it */
437 if (d_inode(index)->i_nlink == 1 &&
08d8f8a5 438 ovl_get_nlink(origin.dentry, index, 0) == 0)
caf70cb2
AG
439 err = -ENOENT;
440
415543d5
AG
441 dput(origin.dentry);
442out:
443 kfree(fh);
444 return err;
445
446fail:
61b67471
AG
447 pr_warn_ratelimited("overlayfs: failed to verify index (%pd2, ftype=%x, err=%i)\n",
448 index, d_inode(index)->i_mode & S_IFMT, err);
415543d5
AG
449 goto out;
450}
451
359f392c
AG
452/*
453 * Lookup in indexdir for the index entry of a lower real inode or a copy up
454 * origin inode. The index entry name is the hex representation of the lower
455 * inode file handle.
456 *
457 * If the index dentry in negative, then either no lower aliases have been
458 * copied up yet, or aliases have been copied up in older kernels and are
459 * not indexed.
460 *
461 * If the index dentry for a copy up origin inode is positive, but points
462 * to an inode different than the upper inode, then either the upper inode
463 * has been copied up and not indexed or it was indexed, but since then
464 * index dir was cleared. Either way, that index cannot be used to indentify
465 * the overlay inode.
466 */
467int ovl_get_index_name(struct dentry *origin, struct qstr *name)
468{
469 int err;
470 struct ovl_fh *fh;
471 char *n, *s;
472
473 fh = ovl_encode_fh(origin, false);
474 if (IS_ERR(fh))
475 return PTR_ERR(fh);
476
477 err = -ENOMEM;
0ee931c4 478 n = kzalloc(fh->len * 2, GFP_KERNEL);
359f392c
AG
479 if (n) {
480 s = bin2hex(n, fh, fh->len);
481 *name = (struct qstr) QSTR_INIT(n, s - n);
482 err = 0;
483 }
484 kfree(fh);
485
486 return err;
487
488}
489
490static struct dentry *ovl_lookup_index(struct dentry *dentry,
491 struct dentry *upper,
492 struct dentry *origin)
493{
494 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
495 struct dentry *index;
496 struct inode *inode;
497 struct qstr name;
498 int err;
499
500 err = ovl_get_index_name(origin, &name);
501 if (err)
502 return ERR_PTR(err);
503
504 index = lookup_one_len_unlocked(name.name, ofs->indexdir, name.len);
505 if (IS_ERR(index)) {
e0082a0f 506 err = PTR_ERR(index);
7937a56f
AG
507 if (err == -ENOENT) {
508 index = NULL;
509 goto out;
510 }
359f392c
AG
511 pr_warn_ratelimited("overlayfs: failed inode index lookup (ino=%lu, key=%*s, err=%i);\n"
512 "overlayfs: mount with '-o index=off' to disable inodes index.\n",
513 d_inode(origin)->i_ino, name.len, name.name,
514 err);
515 goto out;
516 }
517
0e082555 518 inode = d_inode(index);
359f392c 519 if (d_is_negative(index)) {
6eaf0111 520 goto out_dput;
0e082555 521 } else if (upper && d_inode(upper) != inode) {
6eaf0111 522 goto out_dput;
0e082555
AG
523 } else if (ovl_dentry_weird(index) || ovl_is_whiteout(index) ||
524 ((inode->i_mode ^ d_inode(origin)->i_mode) & S_IFMT)) {
525 /*
526 * Index should always be of the same file type as origin
527 * except for the case of a whiteout index. A whiteout
528 * index should only exist if all lower aliases have been
529 * unlinked, which means that finding a lower origin on lookup
530 * whose index is a whiteout should be treated as an error.
531 */
532 pr_warn_ratelimited("overlayfs: bad index found (index=%pd2, ftype=%x, origin ftype=%x).\n",
533 index, d_inode(index)->i_mode & S_IFMT,
534 d_inode(origin)->i_mode & S_IFMT);
359f392c
AG
535 goto fail;
536 }
537
538out:
539 kfree(name.name);
540 return index;
541
6eaf0111
AG
542out_dput:
543 dput(index);
544 index = NULL;
545 goto out;
546
359f392c
AG
547fail:
548 dput(index);
549 index = ERR_PTR(-EIO);
550 goto out;
551}
552
bbb1e54d
MS
553/*
554 * Returns next layer in stack starting from top.
555 * Returns -1 if this is the last layer.
556 */
557int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
558{
559 struct ovl_entry *oe = dentry->d_fsdata;
560
561 BUG_ON(idx < 0);
562 if (idx == 0) {
563 ovl_path_upper(dentry, path);
564 if (path->dentry)
565 return oe->numlower ? 1 : -1;
566 idx++;
567 }
568 BUG_ON(idx > oe->numlower);
b9343632
CR
569 path->dentry = oe->lowerstack[idx - 1].dentry;
570 path->mnt = oe->lowerstack[idx - 1].layer->mnt;
bbb1e54d
MS
571
572 return (idx < oe->numlower) ? idx + 1 : -1;
573}
574
b9343632
CR
575static int ovl_find_layer(struct ovl_fs *ofs, struct ovl_path *path)
576{
577 int i;
578
579 for (i = 0; i < ofs->numlower; i++) {
580 if (ofs->lower_layers[i].mnt == path->layer->mnt)
581 break;
582 }
583
584 return i;
585}
586
bbb1e54d
MS
587struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
588 unsigned int flags)
589{
590 struct ovl_entry *oe;
591 const struct cred *old_cred;
6b2d5fe4 592 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
bbb1e54d 593 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
c22205d0 594 struct ovl_entry *roe = dentry->d_sb->s_root->d_fsdata;
b9343632 595 struct ovl_path *stack = NULL;
bbb1e54d 596 struct dentry *upperdir, *upperdentry = NULL;
359f392c 597 struct dentry *index = NULL;
bbb1e54d
MS
598 unsigned int ctr = 0;
599 struct inode *inode = NULL;
600 bool upperopaque = false;
02b69b28 601 char *upperredirect = NULL;
bbb1e54d
MS
602 struct dentry *this;
603 unsigned int i;
604 int err;
e28edc46
MS
605 struct ovl_lookup_data d = {
606 .name = dentry->d_name,
607 .is_dir = false,
608 .opaque = false,
609 .stop = false,
610 .last = !poe->numlower,
02b69b28 611 .redirect = NULL,
e28edc46 612 };
bbb1e54d 613
6b2d5fe4
MS
614 if (dentry->d_name.len > ofs->namelen)
615 return ERR_PTR(-ENAMETOOLONG);
616
bbb1e54d 617 old_cred = ovl_override_creds(dentry->d_sb);
09d8b586 618 upperdir = ovl_dentry_upper(dentry->d_parent);
bbb1e54d 619 if (upperdir) {
e28edc46
MS
620 err = ovl_lookup_layer(upperdir, &d, &upperdentry);
621 if (err)
bbb1e54d
MS
622 goto out;
623
e28edc46
MS
624 if (upperdentry && unlikely(ovl_dentry_remote(upperdentry))) {
625 dput(upperdentry);
626 err = -EREMOTE;
627 goto out;
bbb1e54d 628 }
a9d01957
AG
629 if (upperdentry && !d.is_dir) {
630 BUG_ON(!d.stop || d.redirect);
f7d3daca
AG
631 /*
632 * Lookup copy up origin by decoding origin file handle.
633 * We may get a disconnected dentry, which is fine,
634 * because we only need to hold the origin inode in
635 * cache and use its inode number. We may even get a
636 * connected dentry, that is not under any of the lower
637 * layers root. That is also fine for using it's inode
638 * number - it's the same as if we held a reference
639 * to a dentry in lower layer that was moved under us.
640 */
415543d5
AG
641 err = ovl_check_origin(upperdentry, roe->lowerstack,
642 roe->numlower, &stack, &ctr);
a9d01957 643 if (err)
5455f92b 644 goto out_put_upper;
a9d01957 645 }
02b69b28
MS
646
647 if (d.redirect) {
0ce5cdc9 648 err = -ENOMEM;
02b69b28
MS
649 upperredirect = kstrdup(d.redirect, GFP_KERNEL);
650 if (!upperredirect)
651 goto out_put_upper;
652 if (d.redirect[0] == '/')
c22205d0 653 poe = roe;
02b69b28 654 }
e28edc46 655 upperopaque = d.opaque;
bbb1e54d
MS
656 }
657
e28edc46 658 if (!d.stop && poe->numlower) {
bbb1e54d 659 err = -ENOMEM;
b9343632 660 stack = kcalloc(ofs->numlower, sizeof(struct ovl_path),
0ee931c4 661 GFP_KERNEL);
bbb1e54d
MS
662 if (!stack)
663 goto out_put_upper;
664 }
665
e28edc46 666 for (i = 0; !d.stop && i < poe->numlower; i++) {
b9343632 667 struct ovl_path lower = poe->lowerstack[i];
bbb1e54d 668
e28edc46 669 d.last = i == poe->numlower - 1;
b9343632 670 err = ovl_lookup_layer(lower.dentry, &d, &this);
e28edc46 671 if (err)
bbb1e54d 672 goto out_put;
6b2d5fe4 673
bbb1e54d
MS
674 if (!this)
675 continue;
bbb1e54d
MS
676
677 stack[ctr].dentry = this;
b9343632 678 stack[ctr].layer = lower.layer;
bbb1e54d 679 ctr++;
02b69b28
MS
680
681 if (d.stop)
682 break;
683
438c84c2
MS
684 /*
685 * Following redirects can have security consequences: it's like
686 * a symlink into the lower layer without the permission checks.
687 * This is only a problem if the upper layer is untrusted (e.g
688 * comes from an USB drive). This can allow a non-readable file
689 * or directory to become readable.
690 *
691 * Only following redirects when redirects are enabled disables
692 * this attack vector when not necessary.
693 */
694 err = -EPERM;
695 if (d.redirect && !ofs->config.redirect_follow) {
696 pr_warn_ratelimited("overlay: refusing to follow redirect for (%pd2)\n", dentry);
697 goto out_put;
698 }
699
c22205d0
AG
700 if (d.redirect && d.redirect[0] == '/' && poe != roe) {
701 poe = roe;
02b69b28
MS
702
703 /* Find the current layer on the root dentry */
b9343632
CR
704 i = ovl_find_layer(ofs, &lower);
705 if (WARN_ON(i == ofs->numlower))
02b69b28
MS
706 break;
707 }
bbb1e54d
MS
708 }
709
359f392c
AG
710 /* Lookup index by lower inode and verify it matches upper inode */
711 if (ctr && !d.is_dir && ovl_indexdir(dentry->d_sb)) {
712 struct dentry *origin = stack[0].dentry;
713
714 index = ovl_lookup_index(dentry, upperdentry, origin);
715 if (IS_ERR(index)) {
716 err = PTR_ERR(index);
717 index = NULL;
718 goto out_put;
719 }
720 }
721
bbb1e54d
MS
722 oe = ovl_alloc_entry(ctr);
723 err = -ENOMEM;
724 if (!oe)
725 goto out_put;
726
e6d2ebdd 727 oe->opaque = upperopaque;
b9343632 728 memcpy(oe->lowerstack, stack, sizeof(struct ovl_path) * ctr);
e6d2ebdd 729 dentry->d_fsdata = oe;
bbb1e54d 730
55acc661
MS
731 if (upperdentry)
732 ovl_dentry_set_upper_alias(dentry);
733 else if (index)
359f392c
AG
734 upperdentry = dget(index);
735
e6d2ebdd 736 if (upperdentry || ctr) {
6eaf0111 737 inode = ovl_get_inode(dentry, upperdentry, index);
b9ac5c27
MS
738 err = PTR_ERR(inode);
739 if (IS_ERR(inode))
bbb1e54d 740 goto out_free_oe;
cf31c463
MS
741
742 OVL_I(inode)->redirect = upperredirect;
359f392c
AG
743 if (index)
744 ovl_set_flag(OVL_INDEX, inode);
bbb1e54d
MS
745 }
746
747 revert_creds(old_cred);
359f392c 748 dput(index);
bbb1e54d 749 kfree(stack);
02b69b28 750 kfree(d.redirect);
bbb1e54d
MS
751 d_add(dentry, inode);
752
753 return NULL;
754
755out_free_oe:
e6d2ebdd 756 dentry->d_fsdata = NULL;
bbb1e54d
MS
757 kfree(oe);
758out_put:
359f392c 759 dput(index);
bbb1e54d
MS
760 for (i = 0; i < ctr; i++)
761 dput(stack[i].dentry);
762 kfree(stack);
763out_put_upper:
764 dput(upperdentry);
02b69b28 765 kfree(upperredirect);
bbb1e54d 766out:
02b69b28 767 kfree(d.redirect);
bbb1e54d
MS
768 revert_creds(old_cred);
769 return ERR_PTR(err);
770}
771
772bool ovl_lower_positive(struct dentry *dentry)
773{
774 struct ovl_entry *oe = dentry->d_fsdata;
775 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
776 const struct qstr *name = &dentry->d_name;
777 unsigned int i;
778 bool positive = false;
779 bool done = false;
780
781 /*
782 * If dentry is negative, then lower is positive iff this is a
783 * whiteout.
784 */
785 if (!dentry->d_inode)
786 return oe->opaque;
787
788 /* Negative upper -> positive lower */
09d8b586 789 if (!ovl_dentry_upper(dentry))
bbb1e54d
MS
790 return true;
791
792 /* Positive upper -> have to look up lower to see whether it exists */
793 for (i = 0; !done && !positive && i < poe->numlower; i++) {
794 struct dentry *this;
795 struct dentry *lowerdir = poe->lowerstack[i].dentry;
796
797 this = lookup_one_len_unlocked(name->name, lowerdir,
798 name->len);
799 if (IS_ERR(this)) {
800 switch (PTR_ERR(this)) {
801 case -ENOENT:
802 case -ENAMETOOLONG:
803 break;
804
805 default:
806 /*
807 * Assume something is there, we just couldn't
808 * access it.
809 */
810 positive = true;
811 break;
812 }
813 } else {
814 if (this->d_inode) {
815 positive = !ovl_is_whiteout(this);
816 done = true;
817 }
818 dput(this);
819 }
820 }
821
822 return positive;
823}