]> git.ipfire.org Git - thirdparty/kernel/linux.git/blob - fs/overlayfs/namei.c
ovl: do not encode lower fh with upper sb_writers held
[thirdparty/kernel/linux.git] / fs / overlayfs / namei.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2011 Novell Inc.
4 * Copyright (C) 2016 Red Hat, Inc.
5 */
6
7 #include <linux/fs.h>
8 #include <linux/cred.h>
9 #include <linux/ctype.h>
10 #include <linux/namei.h>
11 #include <linux/xattr.h>
12 #include <linux/ratelimit.h>
13 #include <linux/mount.h>
14 #include <linux/exportfs.h>
15 #include "overlayfs.h"
16
17 #include "../internal.h" /* for vfs_path_lookup */
18
19 struct ovl_lookup_data {
20 struct super_block *sb;
21 struct vfsmount *mnt;
22 struct qstr name;
23 bool is_dir;
24 bool opaque;
25 bool stop;
26 bool last;
27 char *redirect;
28 int metacopy;
29 /* Referring to last redirect xattr */
30 bool absolute_redirect;
31 };
32
33 static int ovl_check_redirect(const struct path *path, struct ovl_lookup_data *d,
34 size_t prelen, const char *post)
35 {
36 int res;
37 char *buf;
38 struct ovl_fs *ofs = OVL_FS(d->sb);
39
40 d->absolute_redirect = false;
41 buf = ovl_get_redirect_xattr(ofs, path, prelen + strlen(post));
42 if (IS_ERR_OR_NULL(buf))
43 return PTR_ERR(buf);
44
45 if (buf[0] == '/') {
46 d->absolute_redirect = true;
47 /*
48 * One of the ancestor path elements in an absolute path
49 * lookup in ovl_lookup_layer() could have been opaque and
50 * that will stop further lookup in lower layers (d->stop=true)
51 * But we have found an absolute redirect in descendant path
52 * element and that should force continue lookup in lower
53 * layers (reset d->stop).
54 */
55 d->stop = false;
56 } else {
57 res = strlen(buf) + 1;
58 memmove(buf + prelen, buf, res);
59 memcpy(buf, d->name.name, prelen);
60 }
61
62 strcat(buf, post);
63 kfree(d->redirect);
64 d->redirect = buf;
65 d->name.name = d->redirect;
66 d->name.len = strlen(d->redirect);
67
68 return 0;
69 }
70
71 static int ovl_acceptable(void *ctx, struct dentry *dentry)
72 {
73 /*
74 * A non-dir origin may be disconnected, which is fine, because
75 * we only need it for its unique inode number.
76 */
77 if (!d_is_dir(dentry))
78 return 1;
79
80 /* Don't decode a deleted empty directory */
81 if (d_unhashed(dentry))
82 return 0;
83
84 /* Check if directory belongs to the layer we are decoding from */
85 return is_subdir(dentry, ((struct vfsmount *)ctx)->mnt_root);
86 }
87
88 /*
89 * Check validity of an overlay file handle buffer.
90 *
91 * Return 0 for a valid file handle.
92 * Return -ENODATA for "origin unknown".
93 * Return <0 for an invalid file handle.
94 */
95 int ovl_check_fb_len(struct ovl_fb *fb, int fb_len)
96 {
97 if (fb_len < sizeof(struct ovl_fb) || fb_len < fb->len)
98 return -EINVAL;
99
100 if (fb->magic != OVL_FH_MAGIC)
101 return -EINVAL;
102
103 /* Treat larger version and unknown flags as "origin unknown" */
104 if (fb->version > OVL_FH_VERSION || fb->flags & ~OVL_FH_FLAG_ALL)
105 return -ENODATA;
106
107 /* Treat endianness mismatch as "origin unknown" */
108 if (!(fb->flags & OVL_FH_FLAG_ANY_ENDIAN) &&
109 (fb->flags & OVL_FH_FLAG_BIG_ENDIAN) != OVL_FH_FLAG_CPU_ENDIAN)
110 return -ENODATA;
111
112 return 0;
113 }
114
115 static struct ovl_fh *ovl_get_fh(struct ovl_fs *ofs, struct dentry *upperdentry,
116 enum ovl_xattr ox)
117 {
118 int res, err;
119 struct ovl_fh *fh = NULL;
120
121 res = ovl_getxattr_upper(ofs, upperdentry, ox, NULL, 0);
122 if (res < 0) {
123 if (res == -ENODATA || res == -EOPNOTSUPP)
124 return NULL;
125 goto fail;
126 }
127 /* Zero size value means "copied up but origin unknown" */
128 if (res == 0)
129 return NULL;
130
131 fh = kzalloc(res + OVL_FH_WIRE_OFFSET, GFP_KERNEL);
132 if (!fh)
133 return ERR_PTR(-ENOMEM);
134
135 res = ovl_getxattr_upper(ofs, upperdentry, ox, fh->buf, res);
136 if (res < 0)
137 goto fail;
138
139 err = ovl_check_fb_len(&fh->fb, res);
140 if (err < 0) {
141 if (err == -ENODATA)
142 goto out;
143 goto invalid;
144 }
145
146 return fh;
147
148 out:
149 kfree(fh);
150 return NULL;
151
152 fail:
153 pr_warn_ratelimited("failed to get origin (%i)\n", res);
154 goto out;
155 invalid:
156 pr_warn_ratelimited("invalid origin (%*phN)\n", res, fh);
157 goto out;
158 }
159
160 struct dentry *ovl_decode_real_fh(struct ovl_fs *ofs, struct ovl_fh *fh,
161 struct vfsmount *mnt, bool connected)
162 {
163 struct dentry *real;
164 int bytes;
165
166 if (!capable(CAP_DAC_READ_SEARCH))
167 return NULL;
168
169 /*
170 * Make sure that the stored uuid matches the uuid of the lower
171 * layer where file handle will be decoded.
172 * In case of uuid=off option just make sure that stored uuid is null.
173 */
174 if (ovl_origin_uuid(ofs) ?
175 !uuid_equal(&fh->fb.uuid, &mnt->mnt_sb->s_uuid) :
176 !uuid_is_null(&fh->fb.uuid))
177 return NULL;
178
179 bytes = (fh->fb.len - offsetof(struct ovl_fb, fid));
180 real = exportfs_decode_fh(mnt, (struct fid *)fh->fb.fid,
181 bytes >> 2, (int)fh->fb.type,
182 connected ? ovl_acceptable : NULL, mnt);
183 if (IS_ERR(real)) {
184 /*
185 * Treat stale file handle to lower file as "origin unknown".
186 * upper file handle could become stale when upper file is
187 * unlinked and this information is needed to handle stale
188 * index entries correctly.
189 */
190 if (real == ERR_PTR(-ESTALE) &&
191 !(fh->fb.flags & OVL_FH_FLAG_PATH_UPPER))
192 real = NULL;
193 return real;
194 }
195
196 if (ovl_dentry_weird(real)) {
197 dput(real);
198 return NULL;
199 }
200
201 return real;
202 }
203
204 static bool ovl_is_opaquedir(struct ovl_fs *ofs, const struct path *path)
205 {
206 return ovl_path_check_dir_xattr(ofs, path, OVL_XATTR_OPAQUE);
207 }
208
209 static struct dentry *ovl_lookup_positive_unlocked(struct ovl_lookup_data *d,
210 const char *name,
211 struct dentry *base, int len,
212 bool drop_negative)
213 {
214 struct dentry *ret = lookup_one_unlocked(mnt_idmap(d->mnt), name, base, len);
215
216 if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) {
217 if (drop_negative && ret->d_lockref.count == 1) {
218 spin_lock(&ret->d_lock);
219 /* Recheck condition under lock */
220 if (d_is_negative(ret) && ret->d_lockref.count == 1)
221 __d_drop(ret);
222 spin_unlock(&ret->d_lock);
223 }
224 dput(ret);
225 ret = ERR_PTR(-ENOENT);
226 }
227 return ret;
228 }
229
230 static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d,
231 const char *name, unsigned int namelen,
232 size_t prelen, const char *post,
233 struct dentry **ret, bool drop_negative)
234 {
235 struct dentry *this;
236 struct path path;
237 int err;
238 bool last_element = !post[0];
239
240 this = ovl_lookup_positive_unlocked(d, name, base, namelen, drop_negative);
241 if (IS_ERR(this)) {
242 err = PTR_ERR(this);
243 this = NULL;
244 if (err == -ENOENT || err == -ENAMETOOLONG)
245 goto out;
246 goto out_err;
247 }
248
249 if (ovl_dentry_weird(this)) {
250 /* Don't support traversing automounts and other weirdness */
251 err = -EREMOTE;
252 goto out_err;
253 }
254 if (ovl_is_whiteout(this)) {
255 d->stop = d->opaque = true;
256 goto put_and_out;
257 }
258 /*
259 * This dentry should be a regular file if previous layer lookup
260 * found a metacopy dentry.
261 */
262 if (last_element && d->metacopy && !d_is_reg(this)) {
263 d->stop = true;
264 goto put_and_out;
265 }
266
267 path.dentry = this;
268 path.mnt = d->mnt;
269 if (!d_can_lookup(this)) {
270 if (d->is_dir || !last_element) {
271 d->stop = true;
272 goto put_and_out;
273 }
274 err = ovl_check_metacopy_xattr(OVL_FS(d->sb), &path, NULL);
275 if (err < 0)
276 goto out_err;
277
278 d->metacopy = err;
279 d->stop = !d->metacopy;
280 if (!d->metacopy || d->last)
281 goto out;
282 } else {
283 if (ovl_lookup_trap_inode(d->sb, this)) {
284 /* Caught in a trap of overlapping layers */
285 err = -ELOOP;
286 goto out_err;
287 }
288
289 if (last_element)
290 d->is_dir = true;
291 if (d->last)
292 goto out;
293
294 if (ovl_is_opaquedir(OVL_FS(d->sb), &path)) {
295 d->stop = true;
296 if (last_element)
297 d->opaque = true;
298 goto out;
299 }
300 }
301 err = ovl_check_redirect(&path, d, prelen, post);
302 if (err)
303 goto out_err;
304 out:
305 *ret = this;
306 return 0;
307
308 put_and_out:
309 dput(this);
310 this = NULL;
311 goto out;
312
313 out_err:
314 dput(this);
315 return err;
316 }
317
318 static int ovl_lookup_layer(struct dentry *base, struct ovl_lookup_data *d,
319 struct dentry **ret, bool drop_negative)
320 {
321 /* Counting down from the end, since the prefix can change */
322 size_t rem = d->name.len - 1;
323 struct dentry *dentry = NULL;
324 int err;
325
326 if (d->name.name[0] != '/')
327 return ovl_lookup_single(base, d, d->name.name, d->name.len,
328 0, "", ret, drop_negative);
329
330 while (!IS_ERR_OR_NULL(base) && d_can_lookup(base)) {
331 const char *s = d->name.name + d->name.len - rem;
332 const char *next = strchrnul(s, '/');
333 size_t thislen = next - s;
334 bool end = !next[0];
335
336 /* Verify we did not go off the rails */
337 if (WARN_ON(s[-1] != '/'))
338 return -EIO;
339
340 err = ovl_lookup_single(base, d, s, thislen,
341 d->name.len - rem, next, &base,
342 drop_negative);
343 dput(dentry);
344 if (err)
345 return err;
346 dentry = base;
347 if (end)
348 break;
349
350 rem -= thislen + 1;
351
352 if (WARN_ON(rem >= d->name.len))
353 return -EIO;
354 }
355 *ret = dentry;
356 return 0;
357 }
358
359 static int ovl_lookup_data_layer(struct dentry *dentry, const char *redirect,
360 const struct ovl_layer *layer,
361 struct path *datapath)
362 {
363 int err;
364
365 err = vfs_path_lookup(layer->mnt->mnt_root, layer->mnt, redirect,
366 LOOKUP_BENEATH | LOOKUP_NO_SYMLINKS | LOOKUP_NO_XDEV,
367 datapath);
368 pr_debug("lookup lowerdata (%pd2, redirect=\"%s\", layer=%d, err=%i)\n",
369 dentry, redirect, layer->idx, err);
370
371 if (err)
372 return err;
373
374 err = -EREMOTE;
375 if (ovl_dentry_weird(datapath->dentry))
376 goto out_path_put;
377
378 err = -ENOENT;
379 /* Only regular file is acceptable as lower data */
380 if (!d_is_reg(datapath->dentry))
381 goto out_path_put;
382
383 return 0;
384
385 out_path_put:
386 path_put(datapath);
387
388 return err;
389 }
390
391 /* Lookup in data-only layers by absolute redirect to layer root */
392 static int ovl_lookup_data_layers(struct dentry *dentry, const char *redirect,
393 struct ovl_path *lowerdata)
394 {
395 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
396 const struct ovl_layer *layer;
397 struct path datapath;
398 int err = -ENOENT;
399 int i;
400
401 layer = &ofs->layers[ofs->numlayer - ofs->numdatalayer];
402 for (i = 0; i < ofs->numdatalayer; i++, layer++) {
403 err = ovl_lookup_data_layer(dentry, redirect, layer, &datapath);
404 if (!err) {
405 mntput(datapath.mnt);
406 lowerdata->dentry = datapath.dentry;
407 lowerdata->layer = layer;
408 return 0;
409 }
410 }
411
412 return err;
413 }
414
415 int ovl_check_origin_fh(struct ovl_fs *ofs, struct ovl_fh *fh, bool connected,
416 struct dentry *upperdentry, struct ovl_path **stackp)
417 {
418 struct dentry *origin = NULL;
419 int i;
420
421 for (i = 1; i <= ovl_numlowerlayer(ofs); i++) {
422 /*
423 * If lower fs uuid is not unique among lower fs we cannot match
424 * fh->uuid to layer.
425 */
426 if (ofs->layers[i].fsid &&
427 ofs->layers[i].fs->bad_uuid)
428 continue;
429
430 origin = ovl_decode_real_fh(ofs, fh, ofs->layers[i].mnt,
431 connected);
432 if (origin)
433 break;
434 }
435
436 if (!origin)
437 return -ESTALE;
438 else if (IS_ERR(origin))
439 return PTR_ERR(origin);
440
441 if (upperdentry && !ovl_is_whiteout(upperdentry) &&
442 inode_wrong_type(d_inode(upperdentry), d_inode(origin)->i_mode))
443 goto invalid;
444
445 if (!*stackp)
446 *stackp = kmalloc(sizeof(struct ovl_path), GFP_KERNEL);
447 if (!*stackp) {
448 dput(origin);
449 return -ENOMEM;
450 }
451 **stackp = (struct ovl_path){
452 .dentry = origin,
453 .layer = &ofs->layers[i]
454 };
455
456 return 0;
457
458 invalid:
459 pr_warn_ratelimited("invalid origin (%pd2, ftype=%x, origin ftype=%x).\n",
460 upperdentry, d_inode(upperdentry)->i_mode & S_IFMT,
461 d_inode(origin)->i_mode & S_IFMT);
462 dput(origin);
463 return -ESTALE;
464 }
465
466 static int ovl_check_origin(struct ovl_fs *ofs, struct dentry *upperdentry,
467 struct ovl_path **stackp)
468 {
469 struct ovl_fh *fh = ovl_get_fh(ofs, upperdentry, OVL_XATTR_ORIGIN);
470 int err;
471
472 if (IS_ERR_OR_NULL(fh))
473 return PTR_ERR(fh);
474
475 err = ovl_check_origin_fh(ofs, fh, false, upperdentry, stackp);
476 kfree(fh);
477
478 if (err) {
479 if (err == -ESTALE)
480 return 0;
481 return err;
482 }
483
484 return 0;
485 }
486
487 /*
488 * Verify that @fh matches the file handle stored in xattr @name.
489 * Return 0 on match, -ESTALE on mismatch, < 0 on error.
490 */
491 static int ovl_verify_fh(struct ovl_fs *ofs, struct dentry *dentry,
492 enum ovl_xattr ox, const struct ovl_fh *fh)
493 {
494 struct ovl_fh *ofh = ovl_get_fh(ofs, dentry, ox);
495 int err = 0;
496
497 if (!ofh)
498 return -ENODATA;
499
500 if (IS_ERR(ofh))
501 return PTR_ERR(ofh);
502
503 if (fh->fb.len != ofh->fb.len || memcmp(&fh->fb, &ofh->fb, fh->fb.len))
504 err = -ESTALE;
505
506 kfree(ofh);
507 return err;
508 }
509
510 int ovl_verify_set_fh(struct ovl_fs *ofs, struct dentry *dentry,
511 enum ovl_xattr ox, const struct ovl_fh *fh,
512 bool is_upper, bool set)
513 {
514 int err;
515
516 err = ovl_verify_fh(ofs, dentry, ox, fh);
517 if (set && err == -ENODATA)
518 err = ovl_setxattr(ofs, dentry, ox, fh->buf, fh->fb.len);
519
520 return err;
521 }
522
523 /*
524 * Verify that @real dentry matches the file handle stored in xattr @name.
525 *
526 * If @set is true and there is no stored file handle, encode @real and store
527 * file handle in xattr @name.
528 *
529 * Return 0 on match, -ESTALE on mismatch, -ENODATA on no xattr, < 0 on error.
530 */
531 int ovl_verify_origin_xattr(struct ovl_fs *ofs, struct dentry *dentry,
532 enum ovl_xattr ox, struct dentry *real,
533 bool is_upper, bool set)
534 {
535 struct inode *inode;
536 struct ovl_fh *fh;
537 int err;
538
539 fh = ovl_encode_real_fh(ofs, real, is_upper);
540 err = PTR_ERR(fh);
541 if (IS_ERR(fh)) {
542 fh = NULL;
543 goto fail;
544 }
545
546 err = ovl_verify_set_fh(ofs, dentry, ox, fh, is_upper, set);
547 if (err)
548 goto fail;
549
550 out:
551 kfree(fh);
552 return err;
553
554 fail:
555 inode = d_inode(real);
556 pr_warn_ratelimited("failed to verify %s (%pd2, ino=%lu, err=%i)\n",
557 is_upper ? "upper" : "origin", real,
558 inode ? inode->i_ino : 0, err);
559 goto out;
560 }
561
562
563 /* Get upper dentry from index */
564 struct dentry *ovl_index_upper(struct ovl_fs *ofs, struct dentry *index,
565 bool connected)
566 {
567 struct ovl_fh *fh;
568 struct dentry *upper;
569
570 if (!d_is_dir(index))
571 return dget(index);
572
573 fh = ovl_get_fh(ofs, index, OVL_XATTR_UPPER);
574 if (IS_ERR_OR_NULL(fh))
575 return ERR_CAST(fh);
576
577 upper = ovl_decode_real_fh(ofs, fh, ovl_upper_mnt(ofs), connected);
578 kfree(fh);
579
580 if (IS_ERR_OR_NULL(upper))
581 return upper ?: ERR_PTR(-ESTALE);
582
583 if (!d_is_dir(upper)) {
584 pr_warn_ratelimited("invalid index upper (%pd2, upper=%pd2).\n",
585 index, upper);
586 dput(upper);
587 return ERR_PTR(-EIO);
588 }
589
590 return upper;
591 }
592
593 /*
594 * Verify that an index entry name matches the origin file handle stored in
595 * OVL_XATTR_ORIGIN and that origin file handle can be decoded to lower path.
596 * Return 0 on match, -ESTALE on mismatch or stale origin, < 0 on error.
597 */
598 int ovl_verify_index(struct ovl_fs *ofs, struct dentry *index)
599 {
600 struct ovl_fh *fh = NULL;
601 size_t len;
602 struct ovl_path origin = { };
603 struct ovl_path *stack = &origin;
604 struct dentry *upper = NULL;
605 int err;
606
607 if (!d_inode(index))
608 return 0;
609
610 err = -EINVAL;
611 if (index->d_name.len < sizeof(struct ovl_fb)*2)
612 goto fail;
613
614 err = -ENOMEM;
615 len = index->d_name.len / 2;
616 fh = kzalloc(len + OVL_FH_WIRE_OFFSET, GFP_KERNEL);
617 if (!fh)
618 goto fail;
619
620 err = -EINVAL;
621 if (hex2bin(fh->buf, index->d_name.name, len))
622 goto fail;
623
624 err = ovl_check_fb_len(&fh->fb, len);
625 if (err)
626 goto fail;
627
628 /*
629 * Whiteout index entries are used as an indication that an exported
630 * overlay file handle should be treated as stale (i.e. after unlink
631 * of the overlay inode). These entries contain no origin xattr.
632 */
633 if (ovl_is_whiteout(index))
634 goto out;
635
636 /*
637 * Verifying directory index entries are not stale is expensive, so
638 * only verify stale dir index if NFS export is enabled.
639 */
640 if (d_is_dir(index) && !ofs->config.nfs_export)
641 goto out;
642
643 /*
644 * Directory index entries should have 'upper' xattr pointing to the
645 * real upper dir. Non-dir index entries are hardlinks to the upper
646 * real inode. For non-dir index, we can read the copy up origin xattr
647 * directly from the index dentry, but for dir index we first need to
648 * decode the upper directory.
649 */
650 upper = ovl_index_upper(ofs, index, false);
651 if (IS_ERR_OR_NULL(upper)) {
652 err = PTR_ERR(upper);
653 /*
654 * Directory index entries with no 'upper' xattr need to be
655 * removed. When dir index entry has a stale 'upper' xattr,
656 * we assume that upper dir was removed and we treat the dir
657 * index as orphan entry that needs to be whited out.
658 */
659 if (err == -ESTALE)
660 goto orphan;
661 else if (!err)
662 err = -ESTALE;
663 goto fail;
664 }
665
666 err = ovl_verify_fh(ofs, upper, OVL_XATTR_ORIGIN, fh);
667 dput(upper);
668 if (err)
669 goto fail;
670
671 /* Check if non-dir index is orphan and don't warn before cleaning it */
672 if (!d_is_dir(index) && d_inode(index)->i_nlink == 1) {
673 err = ovl_check_origin_fh(ofs, fh, false, index, &stack);
674 if (err)
675 goto fail;
676
677 if (ovl_get_nlink(ofs, origin.dentry, index, 0) == 0)
678 goto orphan;
679 }
680
681 out:
682 dput(origin.dentry);
683 kfree(fh);
684 return err;
685
686 fail:
687 pr_warn_ratelimited("failed to verify index (%pd2, ftype=%x, err=%i)\n",
688 index, d_inode(index)->i_mode & S_IFMT, err);
689 goto out;
690
691 orphan:
692 pr_warn_ratelimited("orphan index entry (%pd2, ftype=%x, nlink=%u)\n",
693 index, d_inode(index)->i_mode & S_IFMT,
694 d_inode(index)->i_nlink);
695 err = -ENOENT;
696 goto out;
697 }
698
699 int ovl_get_index_name_fh(const struct ovl_fh *fh, struct qstr *name)
700 {
701 char *n, *s;
702
703 n = kcalloc(fh->fb.len, 2, GFP_KERNEL);
704 if (!n)
705 return -ENOMEM;
706
707 s = bin2hex(n, fh->buf, fh->fb.len);
708 *name = (struct qstr) QSTR_INIT(n, s - n);
709
710 return 0;
711
712 }
713
714 /*
715 * Lookup in indexdir for the index entry of a lower real inode or a copy up
716 * origin inode. The index entry name is the hex representation of the lower
717 * inode file handle.
718 *
719 * If the index dentry in negative, then either no lower aliases have been
720 * copied up yet, or aliases have been copied up in older kernels and are
721 * not indexed.
722 *
723 * If the index dentry for a copy up origin inode is positive, but points
724 * to an inode different than the upper inode, then either the upper inode
725 * has been copied up and not indexed or it was indexed, but since then
726 * index dir was cleared. Either way, that index cannot be used to identify
727 * the overlay inode.
728 */
729 int ovl_get_index_name(struct ovl_fs *ofs, struct dentry *origin,
730 struct qstr *name)
731 {
732 struct ovl_fh *fh;
733 int err;
734
735 fh = ovl_encode_real_fh(ofs, origin, false);
736 if (IS_ERR(fh))
737 return PTR_ERR(fh);
738
739 err = ovl_get_index_name_fh(fh, name);
740
741 kfree(fh);
742 return err;
743 }
744
745 /* Lookup index by file handle for NFS export */
746 struct dentry *ovl_get_index_fh(struct ovl_fs *ofs, struct ovl_fh *fh)
747 {
748 struct dentry *index;
749 struct qstr name;
750 int err;
751
752 err = ovl_get_index_name_fh(fh, &name);
753 if (err)
754 return ERR_PTR(err);
755
756 index = lookup_positive_unlocked(name.name, ofs->indexdir, name.len);
757 kfree(name.name);
758 if (IS_ERR(index)) {
759 if (PTR_ERR(index) == -ENOENT)
760 index = NULL;
761 return index;
762 }
763
764 if (ovl_is_whiteout(index))
765 err = -ESTALE;
766 else if (ovl_dentry_weird(index))
767 err = -EIO;
768 else
769 return index;
770
771 dput(index);
772 return ERR_PTR(err);
773 }
774
775 struct dentry *ovl_lookup_index(struct ovl_fs *ofs, struct dentry *upper,
776 struct dentry *origin, bool verify)
777 {
778 struct dentry *index;
779 struct inode *inode;
780 struct qstr name;
781 bool is_dir = d_is_dir(origin);
782 int err;
783
784 err = ovl_get_index_name(ofs, origin, &name);
785 if (err)
786 return ERR_PTR(err);
787
788 index = lookup_one_positive_unlocked(ovl_upper_mnt_idmap(ofs), name.name,
789 ofs->indexdir, name.len);
790 if (IS_ERR(index)) {
791 err = PTR_ERR(index);
792 if (err == -ENOENT) {
793 index = NULL;
794 goto out;
795 }
796 pr_warn_ratelimited("failed inode index lookup (ino=%lu, key=%.*s, err=%i);\n"
797 "overlayfs: mount with '-o index=off' to disable inodes index.\n",
798 d_inode(origin)->i_ino, name.len, name.name,
799 err);
800 goto out;
801 }
802
803 inode = d_inode(index);
804 if (ovl_is_whiteout(index) && !verify) {
805 /*
806 * When index lookup is called with !verify for decoding an
807 * overlay file handle, a whiteout index implies that decode
808 * should treat file handle as stale and no need to print a
809 * warning about it.
810 */
811 dput(index);
812 index = ERR_PTR(-ESTALE);
813 goto out;
814 } else if (ovl_dentry_weird(index) || ovl_is_whiteout(index) ||
815 inode_wrong_type(inode, d_inode(origin)->i_mode)) {
816 /*
817 * Index should always be of the same file type as origin
818 * except for the case of a whiteout index. A whiteout
819 * index should only exist if all lower aliases have been
820 * unlinked, which means that finding a lower origin on lookup
821 * whose index is a whiteout should be treated as an error.
822 */
823 pr_warn_ratelimited("bad index found (index=%pd2, ftype=%x, origin ftype=%x).\n",
824 index, d_inode(index)->i_mode & S_IFMT,
825 d_inode(origin)->i_mode & S_IFMT);
826 goto fail;
827 } else if (is_dir && verify) {
828 if (!upper) {
829 pr_warn_ratelimited("suspected uncovered redirected dir found (origin=%pd2, index=%pd2).\n",
830 origin, index);
831 goto fail;
832 }
833
834 /* Verify that dir index 'upper' xattr points to upper dir */
835 err = ovl_verify_upper(ofs, index, upper, false);
836 if (err) {
837 if (err == -ESTALE) {
838 pr_warn_ratelimited("suspected multiply redirected dir found (upper=%pd2, origin=%pd2, index=%pd2).\n",
839 upper, origin, index);
840 }
841 goto fail;
842 }
843 } else if (upper && d_inode(upper) != inode) {
844 goto out_dput;
845 }
846 out:
847 kfree(name.name);
848 return index;
849
850 out_dput:
851 dput(index);
852 index = NULL;
853 goto out;
854
855 fail:
856 dput(index);
857 index = ERR_PTR(-EIO);
858 goto out;
859 }
860
861 /*
862 * Returns next layer in stack starting from top.
863 * Returns -1 if this is the last layer.
864 */
865 int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
866 {
867 struct ovl_entry *oe = OVL_E(dentry);
868 struct ovl_path *lowerstack = ovl_lowerstack(oe);
869
870 BUG_ON(idx < 0);
871 if (idx == 0) {
872 ovl_path_upper(dentry, path);
873 if (path->dentry)
874 return ovl_numlower(oe) ? 1 : -1;
875 idx++;
876 }
877 BUG_ON(idx > ovl_numlower(oe));
878 path->dentry = lowerstack[idx - 1].dentry;
879 path->mnt = lowerstack[idx - 1].layer->mnt;
880
881 return (idx < ovl_numlower(oe)) ? idx + 1 : -1;
882 }
883
884 /* Fix missing 'origin' xattr */
885 static int ovl_fix_origin(struct ovl_fs *ofs, struct dentry *dentry,
886 struct dentry *lower, struct dentry *upper)
887 {
888 const struct ovl_fh *fh;
889 int err;
890
891 if (ovl_check_origin_xattr(ofs, upper))
892 return 0;
893
894 fh = ovl_get_origin_fh(ofs, lower);
895 if (IS_ERR(fh))
896 return PTR_ERR(fh);
897
898 err = ovl_want_write(dentry);
899 if (err)
900 goto out;
901
902 err = ovl_set_origin_fh(ofs, fh, upper);
903 if (!err)
904 err = ovl_set_impure(dentry->d_parent, upper->d_parent);
905
906 ovl_drop_write(dentry);
907 out:
908 kfree(fh);
909 return err;
910 }
911
912 static int ovl_maybe_validate_verity(struct dentry *dentry)
913 {
914 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
915 struct inode *inode = d_inode(dentry);
916 struct path datapath, metapath;
917 int err;
918
919 if (!ofs->config.verity_mode ||
920 !ovl_is_metacopy_dentry(dentry) ||
921 ovl_test_flag(OVL_VERIFIED_DIGEST, inode))
922 return 0;
923
924 if (!ovl_test_flag(OVL_HAS_DIGEST, inode)) {
925 if (ofs->config.verity_mode == OVL_VERITY_REQUIRE) {
926 pr_warn_ratelimited("metacopy file '%pd' has no digest specified\n",
927 dentry);
928 return -EIO;
929 }
930 return 0;
931 }
932
933 ovl_path_lowerdata(dentry, &datapath);
934 if (!datapath.dentry)
935 return -EIO;
936
937 ovl_path_real(dentry, &metapath);
938 if (!metapath.dentry)
939 return -EIO;
940
941 err = ovl_inode_lock_interruptible(inode);
942 if (err)
943 return err;
944
945 if (!ovl_test_flag(OVL_VERIFIED_DIGEST, inode)) {
946 const struct cred *old_cred;
947
948 old_cred = ovl_override_creds(dentry->d_sb);
949
950 err = ovl_validate_verity(ofs, &metapath, &datapath);
951 if (err == 0)
952 ovl_set_flag(OVL_VERIFIED_DIGEST, inode);
953
954 revert_creds(old_cred);
955 }
956
957 ovl_inode_unlock(inode);
958
959 return err;
960 }
961
962 /* Lazy lookup of lowerdata */
963 static int ovl_maybe_lookup_lowerdata(struct dentry *dentry)
964 {
965 struct inode *inode = d_inode(dentry);
966 const char *redirect = ovl_lowerdata_redirect(inode);
967 struct ovl_path datapath = {};
968 const struct cred *old_cred;
969 int err;
970
971 if (!redirect || ovl_dentry_lowerdata(dentry))
972 return 0;
973
974 if (redirect[0] != '/')
975 return -EIO;
976
977 err = ovl_inode_lock_interruptible(inode);
978 if (err)
979 return err;
980
981 err = 0;
982 /* Someone got here before us? */
983 if (ovl_dentry_lowerdata(dentry))
984 goto out;
985
986 old_cred = ovl_override_creds(dentry->d_sb);
987 err = ovl_lookup_data_layers(dentry, redirect, &datapath);
988 revert_creds(old_cred);
989 if (err)
990 goto out_err;
991
992 err = ovl_dentry_set_lowerdata(dentry, &datapath);
993 if (err)
994 goto out_err;
995
996 out:
997 ovl_inode_unlock(inode);
998 dput(datapath.dentry);
999
1000 return err;
1001
1002 out_err:
1003 pr_warn_ratelimited("lazy lowerdata lookup failed (%pd2, err=%i)\n",
1004 dentry, err);
1005 goto out;
1006 }
1007
1008 int ovl_verify_lowerdata(struct dentry *dentry)
1009 {
1010 int err;
1011
1012 err = ovl_maybe_lookup_lowerdata(dentry);
1013 if (err)
1014 return err;
1015
1016 return ovl_maybe_validate_verity(dentry);
1017 }
1018
1019 struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
1020 unsigned int flags)
1021 {
1022 struct ovl_entry *oe = NULL;
1023 const struct cred *old_cred;
1024 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
1025 struct ovl_entry *poe = OVL_E(dentry->d_parent);
1026 struct ovl_entry *roe = OVL_E(dentry->d_sb->s_root);
1027 struct ovl_path *stack = NULL, *origin_path = NULL;
1028 struct dentry *upperdir, *upperdentry = NULL;
1029 struct dentry *origin = NULL;
1030 struct dentry *index = NULL;
1031 unsigned int ctr = 0;
1032 struct inode *inode = NULL;
1033 bool upperopaque = false;
1034 char *upperredirect = NULL;
1035 struct dentry *this;
1036 unsigned int i;
1037 int err;
1038 bool uppermetacopy = false;
1039 int metacopy_size = 0;
1040 struct ovl_lookup_data d = {
1041 .sb = dentry->d_sb,
1042 .name = dentry->d_name,
1043 .is_dir = false,
1044 .opaque = false,
1045 .stop = false,
1046 .last = ovl_redirect_follow(ofs) ? false : !ovl_numlower(poe),
1047 .redirect = NULL,
1048 .metacopy = 0,
1049 };
1050
1051 if (dentry->d_name.len > ofs->namelen)
1052 return ERR_PTR(-ENAMETOOLONG);
1053
1054 old_cred = ovl_override_creds(dentry->d_sb);
1055 upperdir = ovl_dentry_upper(dentry->d_parent);
1056 if (upperdir) {
1057 d.mnt = ovl_upper_mnt(ofs);
1058 err = ovl_lookup_layer(upperdir, &d, &upperdentry, true);
1059 if (err)
1060 goto out;
1061
1062 if (upperdentry && upperdentry->d_flags & DCACHE_OP_REAL) {
1063 dput(upperdentry);
1064 err = -EREMOTE;
1065 goto out;
1066 }
1067 if (upperdentry && !d.is_dir) {
1068 /*
1069 * Lookup copy up origin by decoding origin file handle.
1070 * We may get a disconnected dentry, which is fine,
1071 * because we only need to hold the origin inode in
1072 * cache and use its inode number. We may even get a
1073 * connected dentry, that is not under any of the lower
1074 * layers root. That is also fine for using it's inode
1075 * number - it's the same as if we held a reference
1076 * to a dentry in lower layer that was moved under us.
1077 */
1078 err = ovl_check_origin(ofs, upperdentry, &origin_path);
1079 if (err)
1080 goto out_put_upper;
1081
1082 if (d.metacopy)
1083 uppermetacopy = true;
1084 metacopy_size = d.metacopy;
1085 }
1086
1087 if (d.redirect) {
1088 err = -ENOMEM;
1089 upperredirect = kstrdup(d.redirect, GFP_KERNEL);
1090 if (!upperredirect)
1091 goto out_put_upper;
1092 if (d.redirect[0] == '/')
1093 poe = roe;
1094 }
1095 upperopaque = d.opaque;
1096 }
1097
1098 if (!d.stop && ovl_numlower(poe)) {
1099 err = -ENOMEM;
1100 stack = ovl_stack_alloc(ofs->numlayer - 1);
1101 if (!stack)
1102 goto out_put_upper;
1103 }
1104
1105 for (i = 0; !d.stop && i < ovl_numlower(poe); i++) {
1106 struct ovl_path lower = ovl_lowerstack(poe)[i];
1107
1108 if (!ovl_redirect_follow(ofs))
1109 d.last = i == ovl_numlower(poe) - 1;
1110 else if (d.is_dir || !ofs->numdatalayer)
1111 d.last = lower.layer->idx == ovl_numlower(roe);
1112
1113 d.mnt = lower.layer->mnt;
1114 err = ovl_lookup_layer(lower.dentry, &d, &this, false);
1115 if (err)
1116 goto out_put;
1117
1118 if (!this)
1119 continue;
1120
1121 if ((uppermetacopy || d.metacopy) && !ofs->config.metacopy) {
1122 dput(this);
1123 err = -EPERM;
1124 pr_warn_ratelimited("refusing to follow metacopy origin for (%pd2)\n", dentry);
1125 goto out_put;
1126 }
1127
1128 /*
1129 * If no origin fh is stored in upper of a merge dir, store fh
1130 * of lower dir and set upper parent "impure".
1131 */
1132 if (upperdentry && !ctr && !ofs->noxattr && d.is_dir) {
1133 err = ovl_fix_origin(ofs, dentry, this, upperdentry);
1134 if (err) {
1135 dput(this);
1136 goto out_put;
1137 }
1138 }
1139
1140 /*
1141 * When "verify_lower" feature is enabled, do not merge with a
1142 * lower dir that does not match a stored origin xattr. In any
1143 * case, only verified origin is used for index lookup.
1144 *
1145 * For non-dir dentry, if index=on, then ensure origin
1146 * matches the dentry found using path based lookup,
1147 * otherwise error out.
1148 */
1149 if (upperdentry && !ctr &&
1150 ((d.is_dir && ovl_verify_lower(dentry->d_sb)) ||
1151 (!d.is_dir && ofs->config.index && origin_path))) {
1152 err = ovl_verify_origin(ofs, upperdentry, this, false);
1153 if (err) {
1154 dput(this);
1155 if (d.is_dir)
1156 break;
1157 goto out_put;
1158 }
1159 origin = this;
1160 }
1161
1162 if (!upperdentry && !d.is_dir && !ctr && d.metacopy)
1163 metacopy_size = d.metacopy;
1164
1165 if (d.metacopy && ctr) {
1166 /*
1167 * Do not store intermediate metacopy dentries in
1168 * lower chain, except top most lower metacopy dentry.
1169 * Continue the loop so that if there is an absolute
1170 * redirect on this dentry, poe can be reset to roe.
1171 */
1172 dput(this);
1173 this = NULL;
1174 } else {
1175 stack[ctr].dentry = this;
1176 stack[ctr].layer = lower.layer;
1177 ctr++;
1178 }
1179
1180 /*
1181 * Following redirects can have security consequences: it's like
1182 * a symlink into the lower layer without the permission checks.
1183 * This is only a problem if the upper layer is untrusted (e.g
1184 * comes from an USB drive). This can allow a non-readable file
1185 * or directory to become readable.
1186 *
1187 * Only following redirects when redirects are enabled disables
1188 * this attack vector when not necessary.
1189 */
1190 err = -EPERM;
1191 if (d.redirect && !ovl_redirect_follow(ofs)) {
1192 pr_warn_ratelimited("refusing to follow redirect for (%pd2)\n",
1193 dentry);
1194 goto out_put;
1195 }
1196
1197 if (d.stop)
1198 break;
1199
1200 if (d.redirect && d.redirect[0] == '/' && poe != roe) {
1201 poe = roe;
1202 /* Find the current layer on the root dentry */
1203 i = lower.layer->idx - 1;
1204 }
1205 }
1206
1207 /* Defer lookup of lowerdata in data-only layers to first access */
1208 if (d.metacopy && ctr && ofs->numdatalayer && d.absolute_redirect) {
1209 d.metacopy = 0;
1210 ctr++;
1211 }
1212
1213 /*
1214 * For regular non-metacopy upper dentries, there is no lower
1215 * path based lookup, hence ctr will be zero. If a dentry is found
1216 * using ORIGIN xattr on upper, install it in stack.
1217 *
1218 * For metacopy dentry, path based lookup will find lower dentries.
1219 * Just make sure a corresponding data dentry has been found.
1220 */
1221 if (d.metacopy || (uppermetacopy && !ctr)) {
1222 pr_warn_ratelimited("metacopy with no lower data found - abort lookup (%pd2)\n",
1223 dentry);
1224 err = -EIO;
1225 goto out_put;
1226 } else if (!d.is_dir && upperdentry && !ctr && origin_path) {
1227 if (WARN_ON(stack != NULL)) {
1228 err = -EIO;
1229 goto out_put;
1230 }
1231 stack = origin_path;
1232 ctr = 1;
1233 origin = origin_path->dentry;
1234 origin_path = NULL;
1235 }
1236
1237 /*
1238 * Always lookup index if there is no-upperdentry.
1239 *
1240 * For the case of upperdentry, we have set origin by now if it
1241 * needed to be set. There are basically three cases.
1242 *
1243 * For directories, lookup index by lower inode and verify it matches
1244 * upper inode. We only trust dir index if we verified that lower dir
1245 * matches origin, otherwise dir index entries may be inconsistent
1246 * and we ignore them.
1247 *
1248 * For regular upper, we already set origin if upper had ORIGIN
1249 * xattr. There is no verification though as there is no path
1250 * based dentry lookup in lower in this case.
1251 *
1252 * For metacopy upper, we set a verified origin already if index
1253 * is enabled and if upper had an ORIGIN xattr.
1254 *
1255 */
1256 if (!upperdentry && ctr)
1257 origin = stack[0].dentry;
1258
1259 if (origin && ovl_indexdir(dentry->d_sb) &&
1260 (!d.is_dir || ovl_index_all(dentry->d_sb))) {
1261 index = ovl_lookup_index(ofs, upperdentry, origin, true);
1262 if (IS_ERR(index)) {
1263 err = PTR_ERR(index);
1264 index = NULL;
1265 goto out_put;
1266 }
1267 }
1268
1269 if (ctr) {
1270 oe = ovl_alloc_entry(ctr);
1271 err = -ENOMEM;
1272 if (!oe)
1273 goto out_put;
1274
1275 ovl_stack_cpy(ovl_lowerstack(oe), stack, ctr);
1276 }
1277
1278 if (upperopaque)
1279 ovl_dentry_set_opaque(dentry);
1280
1281 if (upperdentry)
1282 ovl_dentry_set_upper_alias(dentry);
1283 else if (index) {
1284 struct path upperpath = {
1285 .dentry = upperdentry = dget(index),
1286 .mnt = ovl_upper_mnt(ofs),
1287 };
1288
1289 /*
1290 * It's safe to assign upperredirect here: the previous
1291 * assignment of happens only if upperdentry is non-NULL, and
1292 * this one only if upperdentry is NULL.
1293 */
1294 upperredirect = ovl_get_redirect_xattr(ofs, &upperpath, 0);
1295 if (IS_ERR(upperredirect)) {
1296 err = PTR_ERR(upperredirect);
1297 upperredirect = NULL;
1298 goto out_free_oe;
1299 }
1300 err = ovl_check_metacopy_xattr(ofs, &upperpath, NULL);
1301 if (err < 0)
1302 goto out_free_oe;
1303 uppermetacopy = err;
1304 metacopy_size = err;
1305 }
1306
1307 if (upperdentry || ctr) {
1308 struct ovl_inode_params oip = {
1309 .upperdentry = upperdentry,
1310 .oe = oe,
1311 .index = index,
1312 .redirect = upperredirect,
1313 };
1314
1315 /* Store lowerdata redirect for lazy lookup */
1316 if (ctr > 1 && !d.is_dir && !stack[ctr - 1].dentry) {
1317 oip.lowerdata_redirect = d.redirect;
1318 d.redirect = NULL;
1319 }
1320 inode = ovl_get_inode(dentry->d_sb, &oip);
1321 err = PTR_ERR(inode);
1322 if (IS_ERR(inode))
1323 goto out_free_oe;
1324 if (upperdentry && !uppermetacopy)
1325 ovl_set_flag(OVL_UPPERDATA, inode);
1326
1327 if (metacopy_size > OVL_METACOPY_MIN_SIZE)
1328 ovl_set_flag(OVL_HAS_DIGEST, inode);
1329 }
1330
1331 ovl_dentry_init_reval(dentry, upperdentry, OVL_I_E(inode));
1332
1333 revert_creds(old_cred);
1334 if (origin_path) {
1335 dput(origin_path->dentry);
1336 kfree(origin_path);
1337 }
1338 dput(index);
1339 ovl_stack_free(stack, ctr);
1340 kfree(d.redirect);
1341 return d_splice_alias(inode, dentry);
1342
1343 out_free_oe:
1344 ovl_free_entry(oe);
1345 out_put:
1346 dput(index);
1347 ovl_stack_free(stack, ctr);
1348 out_put_upper:
1349 if (origin_path) {
1350 dput(origin_path->dentry);
1351 kfree(origin_path);
1352 }
1353 dput(upperdentry);
1354 kfree(upperredirect);
1355 out:
1356 kfree(d.redirect);
1357 revert_creds(old_cred);
1358 return ERR_PTR(err);
1359 }
1360
1361 bool ovl_lower_positive(struct dentry *dentry)
1362 {
1363 struct ovl_entry *poe = OVL_E(dentry->d_parent);
1364 const struct qstr *name = &dentry->d_name;
1365 const struct cred *old_cred;
1366 unsigned int i;
1367 bool positive = false;
1368 bool done = false;
1369
1370 /*
1371 * If dentry is negative, then lower is positive iff this is a
1372 * whiteout.
1373 */
1374 if (!dentry->d_inode)
1375 return ovl_dentry_is_opaque(dentry);
1376
1377 /* Negative upper -> positive lower */
1378 if (!ovl_dentry_upper(dentry))
1379 return true;
1380
1381 old_cred = ovl_override_creds(dentry->d_sb);
1382 /* Positive upper -> have to look up lower to see whether it exists */
1383 for (i = 0; !done && !positive && i < ovl_numlower(poe); i++) {
1384 struct dentry *this;
1385 struct ovl_path *parentpath = &ovl_lowerstack(poe)[i];
1386
1387 this = lookup_one_positive_unlocked(
1388 mnt_idmap(parentpath->layer->mnt),
1389 name->name, parentpath->dentry, name->len);
1390 if (IS_ERR(this)) {
1391 switch (PTR_ERR(this)) {
1392 case -ENOENT:
1393 case -ENAMETOOLONG:
1394 break;
1395
1396 default:
1397 /*
1398 * Assume something is there, we just couldn't
1399 * access it.
1400 */
1401 positive = true;
1402 break;
1403 }
1404 } else {
1405 positive = !ovl_is_whiteout(this);
1406 done = true;
1407 dput(this);
1408 }
1409 }
1410 revert_creds(old_cred);
1411
1412 return positive;
1413 }