]> Git Repo - linux.git/blame - fs/overlayfs/namei.c
afs: Use d_instantiate() rather than d_add() and don't d_drop()
[linux.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>
9ee60ce2 12#include <linux/ctype.h>
bbb1e54d
MS
13#include <linux/namei.h>
14#include <linux/xattr.h>
02b69b28 15#include <linux/ratelimit.h>
a9d01957
AG
16#include <linux/mount.h>
17#include <linux/exportfs.h>
bbb1e54d 18#include "overlayfs.h"
bbb1e54d 19
e28edc46
MS
20struct ovl_lookup_data {
21 struct qstr name;
22 bool is_dir;
23 bool opaque;
24 bool stop;
25 bool last;
02b69b28 26 char *redirect;
9d3dfea3 27 bool metacopy;
e28edc46 28};
bbb1e54d 29
02b69b28
MS
30static int ovl_check_redirect(struct dentry *dentry, struct ovl_lookup_data *d,
31 size_t prelen, const char *post)
32{
33 int res;
0a2d0d3f 34 char *buf;
02b69b28 35
0a2d0d3f
VG
36 buf = ovl_get_redirect_xattr(dentry, prelen + strlen(post));
37 if (IS_ERR_OR_NULL(buf))
38 return PTR_ERR(buf);
02b69b28 39
02b69b28 40 if (buf[0] == '/') {
3ec9b3fa
AG
41 /*
42 * One of the ancestor path elements in an absolute path
43 * lookup in ovl_lookup_layer() could have been opaque and
44 * that will stop further lookup in lower layers (d->stop=true)
45 * But we have found an absolute redirect in decendant path
46 * element and that should force continue lookup in lower
47 * layers (reset d->stop).
48 */
49 d->stop = false;
02b69b28 50 } else {
0a2d0d3f 51 res = strlen(buf) + 1;
02b69b28
MS
52 memmove(buf + prelen, buf, res);
53 memcpy(buf, d->name.name, prelen);
54 }
55
56 strcat(buf, post);
57 kfree(d->redirect);
58 d->redirect = buf;
59 d->name.name = d->redirect;
60 d->name.len = strlen(d->redirect);
61
62 return 0;
02b69b28
MS
63}
64
a9d01957
AG
65static int ovl_acceptable(void *ctx, struct dentry *dentry)
66{
e8f9e5b7
AG
67 /*
68 * A non-dir origin may be disconnected, which is fine, because
69 * we only need it for its unique inode number.
70 */
71 if (!d_is_dir(dentry))
72 return 1;
73
74 /* Don't decode a deleted empty directory */
75 if (d_unhashed(dentry))
76 return 0;
77
78 /* Check if directory belongs to the layer we are decoding from */
79 return is_subdir(dentry, ((struct vfsmount *)ctx)->mnt_root);
a9d01957
AG
80}
81
2e1a5328
AG
82/*
83 * Check validity of an overlay file handle buffer.
84 *
85 * Return 0 for a valid file handle.
86 * Return -ENODATA for "origin unknown".
87 * Return <0 for an invalid file handle.
88 */
8556a420 89int ovl_check_fh_len(struct ovl_fh *fh, int fh_len)
2e1a5328
AG
90{
91 if (fh_len < sizeof(struct ovl_fh) || fh_len < fh->len)
92 return -EINVAL;
93
94 if (fh->magic != OVL_FH_MAGIC)
95 return -EINVAL;
96
97 /* Treat larger version and unknown flags as "origin unknown" */
98 if (fh->version > OVL_FH_VERSION || fh->flags & ~OVL_FH_FLAG_ALL)
99 return -ENODATA;
100
101 /* Treat endianness mismatch as "origin unknown" */
102 if (!(fh->flags & OVL_FH_FLAG_ANY_ENDIAN) &&
103 (fh->flags & OVL_FH_FLAG_BIG_ENDIAN) != OVL_FH_FLAG_CPU_ENDIAN)
104 return -ENODATA;
105
106 return 0;
107}
108
05122443 109static struct ovl_fh *ovl_get_fh(struct dentry *dentry, const char *name)
a9d01957 110{
2e1a5328 111 int res, err;
a9d01957 112 struct ovl_fh *fh = NULL;
a9d01957 113
05122443 114 res = vfs_getxattr(dentry, name, NULL, 0);
a9d01957
AG
115 if (res < 0) {
116 if (res == -ENODATA || res == -EOPNOTSUPP)
117 return NULL;
118 goto fail;
119 }
120 /* Zero size value means "copied up but origin unknown" */
121 if (res == 0)
122 return NULL;
123
2e1a5328 124 fh = kzalloc(res, GFP_KERNEL);
a9d01957
AG
125 if (!fh)
126 return ERR_PTR(-ENOMEM);
127
05122443 128 res = vfs_getxattr(dentry, name, fh, res);
a9d01957
AG
129 if (res < 0)
130 goto fail;
131
2e1a5328
AG
132 err = ovl_check_fh_len(fh, res);
133 if (err < 0) {
134 if (err == -ENODATA)
135 goto out;
a9d01957 136 goto invalid;
2e1a5328 137 }
a9d01957 138
8b88a2e6
AG
139 return fh;
140
141out:
142 kfree(fh);
143 return NULL;
144
145fail:
146 pr_warn_ratelimited("overlayfs: failed to get origin (%i)\n", res);
147 goto out;
148invalid:
149 pr_warn_ratelimited("overlayfs: invalid origin (%*phN)\n", res, fh);
150 goto out;
151}
152
8a22efa1
AG
153struct dentry *ovl_decode_real_fh(struct ovl_fh *fh, struct vfsmount *mnt,
154 bool connected)
8b88a2e6 155{
e8f9e5b7 156 struct dentry *real;
8b88a2e6
AG
157 int bytes;
158
a9d01957
AG
159 /*
160 * Make sure that the stored uuid matches the uuid of the lower
161 * layer where file handle will be decoded.
162 */
85787090 163 if (!uuid_equal(&fh->uuid, &mnt->mnt_sb->s_uuid))
2e1a5328 164 return NULL;
a9d01957 165
8b88a2e6 166 bytes = (fh->len - offsetof(struct ovl_fh, fid));
e8f9e5b7
AG
167 real = exportfs_decode_fh(mnt, (struct fid *)fh->fid,
168 bytes >> 2, (int)fh->type,
8a22efa1 169 connected ? ovl_acceptable : NULL, mnt);
e8f9e5b7
AG
170 if (IS_ERR(real)) {
171 /*
172 * Treat stale file handle to lower file as "origin unknown".
173 * upper file handle could become stale when upper file is
174 * unlinked and this information is needed to handle stale
175 * index entries correctly.
176 */
177 if (real == ERR_PTR(-ESTALE) &&
178 !(fh->flags & OVL_FH_FLAG_PATH_UPPER))
179 real = NULL;
180 return real;
a9d01957
AG
181 }
182
e8f9e5b7
AG
183 if (ovl_dentry_weird(real)) {
184 dput(real);
2e1a5328
AG
185 return NULL;
186 }
a9d01957 187
e8f9e5b7 188 return real;
a9d01957
AG
189}
190
ee1d6d37
AG
191static bool ovl_is_opaquedir(struct dentry *dentry)
192{
193 return ovl_check_dir_xattr(dentry, OVL_XATTR_OPAQUE);
194}
195
e28edc46
MS
196static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d,
197 const char *name, unsigned int namelen,
02b69b28 198 size_t prelen, const char *post,
e28edc46
MS
199 struct dentry **ret)
200{
201 struct dentry *this;
202 int err;
102b0d11 203 bool last_element = !post[0];
e28edc46
MS
204
205 this = lookup_one_len_unlocked(name, base, namelen);
206 if (IS_ERR(this)) {
207 err = PTR_ERR(this);
208 this = NULL;
209 if (err == -ENOENT || err == -ENAMETOOLONG)
210 goto out;
211 goto out_err;
212 }
213 if (!this->d_inode)
214 goto put_and_out;
215
216 if (ovl_dentry_weird(this)) {
217 /* Don't support traversing automounts and other weirdness */
218 err = -EREMOTE;
219 goto out_err;
220 }
221 if (ovl_is_whiteout(this)) {
222 d->stop = d->opaque = true;
223 goto put_and_out;
224 }
9d3dfea3
VG
225 /*
226 * This dentry should be a regular file if previous layer lookup
227 * found a metacopy dentry.
228 */
229 if (last_element && d->metacopy && !d_is_reg(this)) {
e28edc46 230 d->stop = true;
9d3dfea3
VG
231 goto put_and_out;
232 }
233 if (!d_can_lookup(this)) {
234 if (d->is_dir || !last_element) {
235 d->stop = true;
e28edc46 236 goto put_and_out;
9d3dfea3
VG
237 }
238 err = ovl_check_metacopy_xattr(this);
239 if (err < 0)
240 goto out_err;
3a291774 241
9d3dfea3
VG
242 d->metacopy = err;
243 d->stop = !d->metacopy;
b8a8824c
VG
244 if (!d->metacopy || d->last)
245 goto out;
0618a816 246 } else {
102b0d11 247 if (last_element)
0618a816
VG
248 d->is_dir = true;
249 if (d->last)
250 goto out;
251
252 if (ovl_is_opaquedir(this)) {
253 d->stop = true;
254 if (last_element)
255 d->opaque = true;
256 goto out;
257 }
e28edc46 258 }
02b69b28
MS
259 err = ovl_check_redirect(this, d, prelen, post);
260 if (err)
261 goto out_err;
e28edc46
MS
262out:
263 *ret = this;
264 return 0;
265
266put_and_out:
267 dput(this);
268 this = NULL;
269 goto out;
270
271out_err:
272 dput(this);
273 return err;
274}
275
276static int ovl_lookup_layer(struct dentry *base, struct ovl_lookup_data *d,
277 struct dentry **ret)
278{
4c7d0c9c
AG
279 /* Counting down from the end, since the prefix can change */
280 size_t rem = d->name.len - 1;
02b69b28
MS
281 struct dentry *dentry = NULL;
282 int err;
283
4c7d0c9c 284 if (d->name.name[0] != '/')
02b69b28
MS
285 return ovl_lookup_single(base, d, d->name.name, d->name.len,
286 0, "", ret);
287
4c7d0c9c
AG
288 while (!IS_ERR_OR_NULL(base) && d_can_lookup(base)) {
289 const char *s = d->name.name + d->name.len - rem;
02b69b28 290 const char *next = strchrnul(s, '/');
4c7d0c9c
AG
291 size_t thislen = next - s;
292 bool end = !next[0];
02b69b28 293
4c7d0c9c
AG
294 /* Verify we did not go off the rails */
295 if (WARN_ON(s[-1] != '/'))
02b69b28
MS
296 return -EIO;
297
4c7d0c9c
AG
298 err = ovl_lookup_single(base, d, s, thislen,
299 d->name.len - rem, next, &base);
02b69b28
MS
300 dput(dentry);
301 if (err)
302 return err;
303 dentry = base;
4c7d0c9c
AG
304 if (end)
305 break;
306
307 rem -= thislen + 1;
308
309 if (WARN_ON(rem >= d->name.len))
310 return -EIO;
02b69b28
MS
311 }
312 *ret = dentry;
313 return 0;
e28edc46
MS
314}
315
a9d01957 316
8a22efa1 317int ovl_check_origin_fh(struct ovl_fs *ofs, struct ovl_fh *fh, bool connected,
f941866f 318 struct dentry *upperdentry, struct ovl_path **stackp)
a9d01957 319{
f7d3daca
AG
320 struct dentry *origin = NULL;
321 int i;
a9d01957 322
1eff1a1d 323 for (i = 0; i < ofs->numlower; i++) {
8a22efa1
AG
324 origin = ovl_decode_real_fh(fh, ofs->lower_layers[i].mnt,
325 connected);
f7d3daca
AG
326 if (origin)
327 break;
328 }
329
330 if (!origin)
2e1a5328
AG
331 return -ESTALE;
332 else if (IS_ERR(origin))
333 return PTR_ERR(origin);
334
f941866f 335 if (upperdentry && !ovl_is_whiteout(upperdentry) &&
2e1a5328
AG
336 ((d_inode(origin)->i_mode ^ d_inode(upperdentry)->i_mode) & S_IFMT))
337 goto invalid;
a9d01957 338
415543d5 339 if (!*stackp)
b9343632 340 *stackp = kmalloc(sizeof(struct ovl_path), GFP_KERNEL);
a9d01957
AG
341 if (!*stackp) {
342 dput(origin);
343 return -ENOMEM;
344 }
1eff1a1d
AG
345 **stackp = (struct ovl_path){
346 .dentry = origin,
347 .layer = &ofs->lower_layers[i]
348 };
a9d01957
AG
349
350 return 0;
2e1a5328
AG
351
352invalid:
353 pr_warn_ratelimited("overlayfs: invalid origin (%pd2, ftype=%x, origin ftype=%x).\n",
354 upperdentry, d_inode(upperdentry)->i_mode & S_IFMT,
355 d_inode(origin)->i_mode & S_IFMT);
356 dput(origin);
357 return -EIO;
358}
359
1eff1a1d 360static int ovl_check_origin(struct ovl_fs *ofs, struct dentry *upperdentry,
2e1a5328
AG
361 struct ovl_path **stackp, unsigned int *ctrp)
362{
05122443 363 struct ovl_fh *fh = ovl_get_fh(upperdentry, OVL_XATTR_ORIGIN);
2e1a5328
AG
364 int err;
365
366 if (IS_ERR_OR_NULL(fh))
367 return PTR_ERR(fh);
368
8a22efa1 369 err = ovl_check_origin_fh(ofs, fh, false, upperdentry, stackp);
2e1a5328
AG
370 kfree(fh);
371
372 if (err) {
373 if (err == -ESTALE)
374 return 0;
375 return err;
376 }
377
378 if (WARN_ON(*ctrp))
379 return -EIO;
380
381 *ctrp = 1;
382 return 0;
a9d01957
AG
383}
384
8b88a2e6 385/*
05122443 386 * Verify that @fh matches the file handle stored in xattr @name.
8b88a2e6
AG
387 * Return 0 on match, -ESTALE on mismatch, < 0 on error.
388 */
05122443
AG
389static int ovl_verify_fh(struct dentry *dentry, const char *name,
390 const struct ovl_fh *fh)
8b88a2e6 391{
05122443 392 struct ovl_fh *ofh = ovl_get_fh(dentry, name);
8b88a2e6
AG
393 int err = 0;
394
395 if (!ofh)
396 return -ENODATA;
397
398 if (IS_ERR(ofh))
399 return PTR_ERR(ofh);
400
401 if (fh->len != ofh->len || memcmp(fh, ofh, fh->len))
402 err = -ESTALE;
403
404 kfree(ofh);
405 return err;
406}
407
408/*
05122443 409 * Verify that @real dentry matches the file handle stored in xattr @name.
8b88a2e6 410 *
05122443
AG
411 * If @set is true and there is no stored file handle, encode @real and store
412 * file handle in xattr @name.
8b88a2e6 413 *
05122443 414 * Return 0 on match, -ESTALE on mismatch, -ENODATA on no xattr, < 0 on error.
8b88a2e6 415 */
05122443
AG
416int ovl_verify_set_fh(struct dentry *dentry, const char *name,
417 struct dentry *real, bool is_upper, bool set)
8b88a2e6
AG
418{
419 struct inode *inode;
420 struct ovl_fh *fh;
421 int err;
422
5b2cccd3 423 fh = ovl_encode_real_fh(real, is_upper);
8b88a2e6 424 err = PTR_ERR(fh);
babf4770
AG
425 if (IS_ERR(fh)) {
426 fh = NULL;
8b88a2e6 427 goto fail;
babf4770 428 }
8b88a2e6 429
05122443 430 err = ovl_verify_fh(dentry, name, fh);
8b88a2e6 431 if (set && err == -ENODATA)
05122443 432 err = ovl_do_setxattr(dentry, name, fh, fh->len, 0);
8b88a2e6
AG
433 if (err)
434 goto fail;
435
436out:
437 kfree(fh);
438 return err;
439
440fail:
05122443
AG
441 inode = d_inode(real);
442 pr_warn_ratelimited("overlayfs: failed to verify %s (%pd2, ino=%lu, err=%i)\n",
443 is_upper ? "upper" : "origin", real,
444 inode ? inode->i_ino : 0, err);
8b88a2e6
AG
445 goto out;
446}
447
e8f9e5b7 448/* Get upper dentry from index */
3b0bfc6e 449struct dentry *ovl_index_upper(struct ovl_fs *ofs, struct dentry *index)
e8f9e5b7
AG
450{
451 struct ovl_fh *fh;
452 struct dentry *upper;
453
454 if (!d_is_dir(index))
455 return dget(index);
456
457 fh = ovl_get_fh(index, OVL_XATTR_UPPER);
458 if (IS_ERR_OR_NULL(fh))
459 return ERR_CAST(fh);
460
8a22efa1 461 upper = ovl_decode_real_fh(fh, ofs->upper_mnt, true);
e8f9e5b7
AG
462 kfree(fh);
463
464 if (IS_ERR_OR_NULL(upper))
465 return upper ?: ERR_PTR(-ESTALE);
466
467 if (!d_is_dir(upper)) {
468 pr_warn_ratelimited("overlayfs: invalid index upper (%pd2, upper=%pd2).\n",
469 index, upper);
470 dput(upper);
471 return ERR_PTR(-EIO);
472 }
473
474 return upper;
475}
476
9ee60ce2
AG
477/* Is this a leftover from create/whiteout of directory index entry? */
478static bool ovl_is_temp_index(struct dentry *index)
479{
480 return index->d_name.name[0] == '#';
481}
482
415543d5
AG
483/*
484 * Verify that an index entry name matches the origin file handle stored in
485 * OVL_XATTR_ORIGIN and that origin file handle can be decoded to lower path.
486 * Return 0 on match, -ESTALE on mismatch or stale origin, < 0 on error.
487 */
1eff1a1d 488int ovl_verify_index(struct ovl_fs *ofs, struct dentry *index)
415543d5
AG
489{
490 struct ovl_fh *fh = NULL;
491 size_t len;
b9343632
CR
492 struct ovl_path origin = { };
493 struct ovl_path *stack = &origin;
e8f9e5b7 494 struct dentry *upper = NULL;
415543d5
AG
495 int err;
496
497 if (!d_inode(index))
498 return 0;
499
9ee60ce2
AG
500 /* Cleanup leftover from index create/cleanup attempt */
501 err = -ESTALE;
502 if (ovl_is_temp_index(index))
503 goto fail;
504
fa0096e3 505 err = -EINVAL;
415543d5
AG
506 if (index->d_name.len < sizeof(struct ovl_fh)*2)
507 goto fail;
508
509 err = -ENOMEM;
510 len = index->d_name.len / 2;
0ee931c4 511 fh = kzalloc(len, GFP_KERNEL);
415543d5
AG
512 if (!fh)
513 goto fail;
514
515 err = -EINVAL;
2e1a5328
AG
516 if (hex2bin((u8 *)fh, index->d_name.name, len))
517 goto fail;
518
519 err = ovl_check_fh_len(fh, len);
520 if (err)
415543d5
AG
521 goto fail;
522
7db25d36
AG
523 /*
524 * Whiteout index entries are used as an indication that an exported
525 * overlay file handle should be treated as stale (i.e. after unlink
526 * of the overlay inode). These entries contain no origin xattr.
527 */
528 if (ovl_is_whiteout(index))
529 goto out;
530
e8f9e5b7
AG
531 /*
532 * Verifying directory index entries are not stale is expensive, so
533 * only verify stale dir index if NFS export is enabled.
534 */
535 if (d_is_dir(index) && !ofs->config.nfs_export)
536 goto out;
537
538 /*
539 * Directory index entries should have 'upper' xattr pointing to the
540 * real upper dir. Non-dir index entries are hardlinks to the upper
541 * real inode. For non-dir index, we can read the copy up origin xattr
542 * directly from the index dentry, but for dir index we first need to
543 * decode the upper directory.
544 */
545 upper = ovl_index_upper(ofs, index);
546 if (IS_ERR_OR_NULL(upper)) {
547 err = PTR_ERR(upper);
24f0b172
AG
548 /*
549 * Directory index entries with no 'upper' xattr need to be
550 * removed. When dir index entry has a stale 'upper' xattr,
551 * we assume that upper dir was removed and we treat the dir
552 * index as orphan entry that needs to be whited out.
553 */
554 if (err == -ESTALE)
555 goto orphan;
556 else if (!err)
e8f9e5b7 557 err = -ESTALE;
415543d5 558 goto fail;
e8f9e5b7 559 }
415543d5 560
e8f9e5b7
AG
561 err = ovl_verify_fh(upper, OVL_XATTR_ORIGIN, fh);
562 dput(upper);
415543d5
AG
563 if (err)
564 goto fail;
565
e8f9e5b7
AG
566 /* Check if non-dir index is orphan and don't warn before cleaning it */
567 if (!d_is_dir(index) && d_inode(index)->i_nlink == 1) {
8a22efa1 568 err = ovl_check_origin_fh(ofs, fh, false, index, &stack);
e8f9e5b7
AG
569 if (err)
570 goto fail;
571
572 if (ovl_get_nlink(origin.dentry, index, 0) == 0)
24f0b172 573 goto orphan;
e8f9e5b7 574 }
caf70cb2 575
415543d5 576out:
e8f9e5b7 577 dput(origin.dentry);
415543d5
AG
578 kfree(fh);
579 return err;
580
581fail:
61b67471
AG
582 pr_warn_ratelimited("overlayfs: failed to verify index (%pd2, ftype=%x, err=%i)\n",
583 index, d_inode(index)->i_mode & S_IFMT, err);
415543d5 584 goto out;
24f0b172
AG
585
586orphan:
587 pr_warn_ratelimited("overlayfs: orphan index entry (%pd2, ftype=%x, nlink=%u)\n",
588 index, d_inode(index)->i_mode & S_IFMT,
589 d_inode(index)->i_nlink);
590 err = -ENOENT;
591 goto out;
415543d5
AG
592}
593
91ffe7be
AG
594static int ovl_get_index_name_fh(struct ovl_fh *fh, struct qstr *name)
595{
596 char *n, *s;
597
6396bb22 598 n = kcalloc(fh->len, 2, GFP_KERNEL);
91ffe7be
AG
599 if (!n)
600 return -ENOMEM;
601
602 s = bin2hex(n, fh, fh->len);
603 *name = (struct qstr) QSTR_INIT(n, s - n);
604
605 return 0;
606
607}
608
359f392c
AG
609/*
610 * Lookup in indexdir for the index entry of a lower real inode or a copy up
611 * origin inode. The index entry name is the hex representation of the lower
612 * inode file handle.
613 *
614 * If the index dentry in negative, then either no lower aliases have been
615 * copied up yet, or aliases have been copied up in older kernels and are
616 * not indexed.
617 *
618 * If the index dentry for a copy up origin inode is positive, but points
619 * to an inode different than the upper inode, then either the upper inode
620 * has been copied up and not indexed or it was indexed, but since then
621 * index dir was cleared. Either way, that index cannot be used to indentify
622 * the overlay inode.
623 */
624int ovl_get_index_name(struct dentry *origin, struct qstr *name)
625{
359f392c 626 struct ovl_fh *fh;
91ffe7be 627 int err;
359f392c 628
5b2cccd3 629 fh = ovl_encode_real_fh(origin, false);
359f392c
AG
630 if (IS_ERR(fh))
631 return PTR_ERR(fh);
632
91ffe7be 633 err = ovl_get_index_name_fh(fh, name);
359f392c 634
91ffe7be 635 kfree(fh);
359f392c 636 return err;
91ffe7be
AG
637}
638
639/* Lookup index by file handle for NFS export */
640struct dentry *ovl_get_index_fh(struct ovl_fs *ofs, struct ovl_fh *fh)
641{
642 struct dentry *index;
643 struct qstr name;
644 int err;
645
646 err = ovl_get_index_name_fh(fh, &name);
647 if (err)
648 return ERR_PTR(err);
649
650 index = lookup_one_len_unlocked(name.name, ofs->indexdir, name.len);
651 kfree(name.name);
652 if (IS_ERR(index)) {
653 if (PTR_ERR(index) == -ENOENT)
654 index = NULL;
655 return index;
656 }
657
658 if (d_is_negative(index))
659 err = 0;
660 else if (ovl_is_whiteout(index))
661 err = -ESTALE;
662 else if (ovl_dentry_weird(index))
663 err = -EIO;
664 else
665 return index;
359f392c 666
91ffe7be
AG
667 dput(index);
668 return ERR_PTR(err);
359f392c
AG
669}
670
06170154
AG
671struct dentry *ovl_lookup_index(struct ovl_fs *ofs, struct dentry *upper,
672 struct dentry *origin, bool verify)
359f392c 673{
359f392c
AG
674 struct dentry *index;
675 struct inode *inode;
676 struct qstr name;
ad1d615c 677 bool is_dir = d_is_dir(origin);
359f392c
AG
678 int err;
679
680 err = ovl_get_index_name(origin, &name);
681 if (err)
682 return ERR_PTR(err);
683
684 index = lookup_one_len_unlocked(name.name, ofs->indexdir, name.len);
685 if (IS_ERR(index)) {
e0082a0f 686 err = PTR_ERR(index);
7937a56f
AG
687 if (err == -ENOENT) {
688 index = NULL;
689 goto out;
690 }
601350ff 691 pr_warn_ratelimited("overlayfs: failed inode index lookup (ino=%lu, key=%.*s, err=%i);\n"
359f392c
AG
692 "overlayfs: mount with '-o index=off' to disable inodes index.\n",
693 d_inode(origin)->i_ino, name.len, name.name,
694 err);
695 goto out;
696 }
697
0e082555 698 inode = d_inode(index);
359f392c 699 if (d_is_negative(index)) {
6eaf0111 700 goto out_dput;
06170154
AG
701 } else if (ovl_is_whiteout(index) && !verify) {
702 /*
703 * When index lookup is called with !verify for decoding an
704 * overlay file handle, a whiteout index implies that decode
705 * should treat file handle as stale and no need to print a
706 * warning about it.
707 */
708 dput(index);
709 index = ERR_PTR(-ESTALE);
710 goto out;
0e082555
AG
711 } else if (ovl_dentry_weird(index) || ovl_is_whiteout(index) ||
712 ((inode->i_mode ^ d_inode(origin)->i_mode) & S_IFMT)) {
713 /*
714 * Index should always be of the same file type as origin
715 * except for the case of a whiteout index. A whiteout
716 * index should only exist if all lower aliases have been
717 * unlinked, which means that finding a lower origin on lookup
718 * whose index is a whiteout should be treated as an error.
719 */
720 pr_warn_ratelimited("overlayfs: bad index found (index=%pd2, ftype=%x, origin ftype=%x).\n",
721 index, d_inode(index)->i_mode & S_IFMT,
722 d_inode(origin)->i_mode & S_IFMT);
359f392c 723 goto fail;
06170154 724 } else if (is_dir && verify) {
ad1d615c
AG
725 if (!upper) {
726 pr_warn_ratelimited("overlayfs: suspected uncovered redirected dir found (origin=%pd2, index=%pd2).\n",
727 origin, index);
728 goto fail;
729 }
359f392c 730
ad1d615c
AG
731 /* Verify that dir index 'upper' xattr points to upper dir */
732 err = ovl_verify_upper(index, upper, false);
733 if (err) {
734 if (err == -ESTALE) {
735 pr_warn_ratelimited("overlayfs: suspected multiply redirected dir found (upper=%pd2, origin=%pd2, index=%pd2).\n",
736 upper, origin, index);
737 }
738 goto fail;
739 }
740 } else if (upper && d_inode(upper) != inode) {
741 goto out_dput;
742 }
359f392c
AG
743out:
744 kfree(name.name);
745 return index;
746
6eaf0111
AG
747out_dput:
748 dput(index);
749 index = NULL;
750 goto out;
751
359f392c
AG
752fail:
753 dput(index);
754 index = ERR_PTR(-EIO);
755 goto out;
756}
757
bbb1e54d
MS
758/*
759 * Returns next layer in stack starting from top.
760 * Returns -1 if this is the last layer.
761 */
762int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
763{
764 struct ovl_entry *oe = dentry->d_fsdata;
765
766 BUG_ON(idx < 0);
767 if (idx == 0) {
768 ovl_path_upper(dentry, path);
769 if (path->dentry)
770 return oe->numlower ? 1 : -1;
771 idx++;
772 }
773 BUG_ON(idx > oe->numlower);
b9343632
CR
774 path->dentry = oe->lowerstack[idx - 1].dentry;
775 path->mnt = oe->lowerstack[idx - 1].layer->mnt;
bbb1e54d
MS
776
777 return (idx < oe->numlower) ? idx + 1 : -1;
778}
779
9678e630
AG
780/* Fix missing 'origin' xattr */
781static int ovl_fix_origin(struct dentry *dentry, struct dentry *lower,
782 struct dentry *upper)
783{
784 int err;
785
786 if (ovl_check_origin_xattr(upper))
787 return 0;
788
789 err = ovl_want_write(dentry);
790 if (err)
791 return err;
792
793 err = ovl_set_origin(dentry, lower, upper);
794 if (!err)
795 err = ovl_set_impure(dentry->d_parent, upper->d_parent);
796
797 ovl_drop_write(dentry);
798 return err;
799}
800
bbb1e54d
MS
801struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
802 unsigned int flags)
803{
804 struct ovl_entry *oe;
805 const struct cred *old_cred;
6b2d5fe4 806 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
bbb1e54d 807 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
c22205d0 808 struct ovl_entry *roe = dentry->d_sb->s_root->d_fsdata;
9d3dfea3 809 struct ovl_path *stack = NULL, *origin_path = NULL;
bbb1e54d 810 struct dentry *upperdir, *upperdentry = NULL;
ad1d615c 811 struct dentry *origin = NULL;
359f392c 812 struct dentry *index = NULL;
bbb1e54d
MS
813 unsigned int ctr = 0;
814 struct inode *inode = NULL;
815 bool upperopaque = false;
02b69b28 816 char *upperredirect = NULL;
bbb1e54d
MS
817 struct dentry *this;
818 unsigned int i;
819 int err;
9d3dfea3 820 bool metacopy = false;
e28edc46
MS
821 struct ovl_lookup_data d = {
822 .name = dentry->d_name,
823 .is_dir = false,
824 .opaque = false,
825 .stop = false,
452061fd 826 .last = ofs->config.redirect_follow ? false : !poe->numlower,
02b69b28 827 .redirect = NULL,
9d3dfea3 828 .metacopy = false,
e28edc46 829 };
bbb1e54d 830
6b2d5fe4
MS
831 if (dentry->d_name.len > ofs->namelen)
832 return ERR_PTR(-ENAMETOOLONG);
833
bbb1e54d 834 old_cred = ovl_override_creds(dentry->d_sb);
09d8b586 835 upperdir = ovl_dentry_upper(dentry->d_parent);
bbb1e54d 836 if (upperdir) {
e28edc46
MS
837 err = ovl_lookup_layer(upperdir, &d, &upperdentry);
838 if (err)
bbb1e54d
MS
839 goto out;
840
e28edc46
MS
841 if (upperdentry && unlikely(ovl_dentry_remote(upperdentry))) {
842 dput(upperdentry);
843 err = -EREMOTE;
844 goto out;
bbb1e54d 845 }
a9d01957 846 if (upperdentry && !d.is_dir) {
9d3dfea3
VG
847 unsigned int origin_ctr = 0;
848
f7d3daca
AG
849 /*
850 * Lookup copy up origin by decoding origin file handle.
851 * We may get a disconnected dentry, which is fine,
852 * because we only need to hold the origin inode in
853 * cache and use its inode number. We may even get a
854 * connected dentry, that is not under any of the lower
855 * layers root. That is also fine for using it's inode
856 * number - it's the same as if we held a reference
857 * to a dentry in lower layer that was moved under us.
858 */
9d3dfea3
VG
859 err = ovl_check_origin(ofs, upperdentry, &origin_path,
860 &origin_ctr);
a9d01957 861 if (err)
5455f92b 862 goto out_put_upper;
9d3dfea3
VG
863
864 if (d.metacopy)
865 metacopy = true;
a9d01957 866 }
02b69b28
MS
867
868 if (d.redirect) {
0ce5cdc9 869 err = -ENOMEM;
02b69b28
MS
870 upperredirect = kstrdup(d.redirect, GFP_KERNEL);
871 if (!upperredirect)
872 goto out_put_upper;
873 if (d.redirect[0] == '/')
c22205d0 874 poe = roe;
02b69b28 875 }
e28edc46 876 upperopaque = d.opaque;
bbb1e54d
MS
877 }
878
e28edc46 879 if (!d.stop && poe->numlower) {
bbb1e54d 880 err = -ENOMEM;
b9343632 881 stack = kcalloc(ofs->numlower, sizeof(struct ovl_path),
0ee931c4 882 GFP_KERNEL);
bbb1e54d
MS
883 if (!stack)
884 goto out_put_upper;
885 }
886
e28edc46 887 for (i = 0; !d.stop && i < poe->numlower; i++) {
b9343632 888 struct ovl_path lower = poe->lowerstack[i];
bbb1e54d 889
452061fd
VG
890 if (!ofs->config.redirect_follow)
891 d.last = i == poe->numlower - 1;
892 else
893 d.last = lower.layer->idx == roe->numlower;
894
b9343632 895 err = ovl_lookup_layer(lower.dentry, &d, &this);
e28edc46 896 if (err)
bbb1e54d 897 goto out_put;
6b2d5fe4 898
bbb1e54d
MS
899 if (!this)
900 continue;
bbb1e54d 901
9678e630
AG
902 /*
903 * If no origin fh is stored in upper of a merge dir, store fh
904 * of lower dir and set upper parent "impure".
905 */
9d3dfea3 906 if (upperdentry && !ctr && !ofs->noxattr && d.is_dir) {
9678e630
AG
907 err = ovl_fix_origin(dentry, this, upperdentry);
908 if (err) {
909 dput(this);
910 goto out_put;
911 }
912 }
913
37b12916
AG
914 /*
915 * When "verify_lower" feature is enabled, do not merge with a
ad1d615c
AG
916 * lower dir that does not match a stored origin xattr. In any
917 * case, only verified origin is used for index lookup.
9d3dfea3
VG
918 *
919 * For non-dir dentry, if index=on, then ensure origin
920 * matches the dentry found using path based lookup,
921 * otherwise error out.
37b12916 922 */
9d3dfea3
VG
923 if (upperdentry && !ctr &&
924 ((d.is_dir && ovl_verify_lower(dentry->d_sb)) ||
925 (!d.is_dir && ofs->config.index && origin_path))) {
37b12916
AG
926 err = ovl_verify_origin(upperdentry, this, false);
927 if (err) {
928 dput(this);
9d3dfea3
VG
929 if (d.is_dir)
930 break;
931 goto out_put;
37b12916 932 }
ad1d615c 933 origin = this;
37b12916
AG
934 }
935
9d3dfea3
VG
936 if (d.metacopy)
937 metacopy = true;
938 /*
939 * Do not store intermediate metacopy dentries in chain,
940 * except top most lower metacopy dentry
941 */
942 if (d.metacopy && ctr) {
943 dput(this);
944 continue;
945 }
946
bbb1e54d 947 stack[ctr].dentry = this;
b9343632 948 stack[ctr].layer = lower.layer;
bbb1e54d 949 ctr++;
02b69b28 950
438c84c2
MS
951 /*
952 * Following redirects can have security consequences: it's like
953 * a symlink into the lower layer without the permission checks.
954 * This is only a problem if the upper layer is untrusted (e.g
955 * comes from an USB drive). This can allow a non-readable file
956 * or directory to become readable.
957 *
958 * Only following redirects when redirects are enabled disables
959 * this attack vector when not necessary.
960 */
961 err = -EPERM;
962 if (d.redirect && !ofs->config.redirect_follow) {
f8167817
AG
963 pr_warn_ratelimited("overlayfs: refusing to follow redirect for (%pd2)\n",
964 dentry);
438c84c2
MS
965 goto out_put;
966 }
967
d1fe96c0
VG
968 if (d.stop)
969 break;
970
c22205d0
AG
971 if (d.redirect && d.redirect[0] == '/' && poe != roe) {
972 poe = roe;
02b69b28 973 /* Find the current layer on the root dentry */
d583ed7d 974 i = lower.layer->idx - 1;
02b69b28 975 }
bbb1e54d
MS
976 }
977
9d3dfea3
VG
978 if (metacopy) {
979 /*
980 * Found a metacopy dentry but did not find corresponding
981 * data dentry
982 */
983 if (d.metacopy) {
984 err = -EIO;
985 goto out_put;
986 }
987
988 err = -EPERM;
989 if (!ofs->config.metacopy) {
990 pr_warn_ratelimited("overlay: refusing to follow metacopy origin for (%pd2)\n",
991 dentry);
992 goto out_put;
993 }
994 } else if (!d.is_dir && upperdentry && !ctr && origin_path) {
995 if (WARN_ON(stack != NULL)) {
996 err = -EIO;
997 goto out_put;
998 }
999 stack = origin_path;
1000 ctr = 1;
1001 origin_path = NULL;
1002 }
1003
ad1d615c
AG
1004 /*
1005 * Lookup index by lower inode and verify it matches upper inode.
1006 * We only trust dir index if we verified that lower dir matches
1007 * origin, otherwise dir index entries may be inconsistent and we
9d3dfea3
VG
1008 * ignore them.
1009 *
1010 * For non-dir upper metacopy dentry, we already set "origin" if we
1011 * verified that lower matched upper origin. If upper origin was
1012 * not present (because lower layer did not support fh encode/decode),
1013 * or indexing is not enabled, do not set "origin" and skip looking up
1014 * index. This case should be handled in same way as a non-dir upper
1015 * without ORIGIN is handled.
1016 *
1017 * Always lookup index of non-dir non-metacopy and non-upper.
ad1d615c 1018 */
9d3dfea3 1019 if (ctr && (!upperdentry || (!d.is_dir && !metacopy)))
ad1d615c 1020 origin = stack[0].dentry;
359f392c 1021
ad1d615c
AG
1022 if (origin && ovl_indexdir(dentry->d_sb) &&
1023 (!d.is_dir || ovl_index_all(dentry->d_sb))) {
06170154 1024 index = ovl_lookup_index(ofs, upperdentry, origin, true);
359f392c
AG
1025 if (IS_ERR(index)) {
1026 err = PTR_ERR(index);
1027 index = NULL;
1028 goto out_put;
1029 }
1030 }
1031
bbb1e54d
MS
1032 oe = ovl_alloc_entry(ctr);
1033 err = -ENOMEM;
1034 if (!oe)
1035 goto out_put;
1036
b9343632 1037 memcpy(oe->lowerstack, stack, sizeof(struct ovl_path) * ctr);
e6d2ebdd 1038 dentry->d_fsdata = oe;
bbb1e54d 1039
c62520a8
AG
1040 if (upperopaque)
1041 ovl_dentry_set_opaque(dentry);
1042
55acc661
MS
1043 if (upperdentry)
1044 ovl_dentry_set_upper_alias(dentry);
0a2d0d3f 1045 else if (index) {
359f392c 1046 upperdentry = dget(index);
0a2d0d3f
VG
1047 upperredirect = ovl_get_redirect_xattr(upperdentry, 0);
1048 if (IS_ERR(upperredirect)) {
1049 err = PTR_ERR(upperredirect);
1050 upperredirect = NULL;
1051 goto out_free_oe;
1052 }
1053 }
359f392c 1054
e6d2ebdd 1055 if (upperdentry || ctr) {
ac6a52eb
VG
1056 struct ovl_inode_params oip = {
1057 .upperdentry = upperdentry,
1058 .lowerpath = stack,
1059 .index = index,
1060 .numlower = ctr,
9cec54c8 1061 .redirect = upperredirect,
2664bd08
VG
1062 .lowerdata = (ctr > 1 && !d.is_dir) ?
1063 stack[ctr - 1].dentry : NULL,
ac6a52eb
VG
1064 };
1065
1066 inode = ovl_get_inode(dentry->d_sb, &oip);
b9ac5c27
MS
1067 err = PTR_ERR(inode);
1068 if (IS_ERR(inode))
bbb1e54d 1069 goto out_free_oe;
bbb1e54d
MS
1070 }
1071
1072 revert_creds(old_cred);
9d3dfea3
VG
1073 if (origin_path) {
1074 dput(origin_path->dentry);
1075 kfree(origin_path);
1076 }
359f392c 1077 dput(index);
bbb1e54d 1078 kfree(stack);
02b69b28 1079 kfree(d.redirect);
829c28be 1080 return d_splice_alias(inode, dentry);
bbb1e54d
MS
1081
1082out_free_oe:
e6d2ebdd 1083 dentry->d_fsdata = NULL;
bbb1e54d
MS
1084 kfree(oe);
1085out_put:
359f392c 1086 dput(index);
bbb1e54d
MS
1087 for (i = 0; i < ctr; i++)
1088 dput(stack[i].dentry);
1089 kfree(stack);
1090out_put_upper:
9d3dfea3
VG
1091 if (origin_path) {
1092 dput(origin_path->dentry);
1093 kfree(origin_path);
1094 }
bbb1e54d 1095 dput(upperdentry);
02b69b28 1096 kfree(upperredirect);
bbb1e54d 1097out:
02b69b28 1098 kfree(d.redirect);
bbb1e54d
MS
1099 revert_creds(old_cred);
1100 return ERR_PTR(err);
1101}
1102
1103bool ovl_lower_positive(struct dentry *dentry)
1104{
bbb1e54d
MS
1105 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
1106 const struct qstr *name = &dentry->d_name;
6d0a8a90 1107 const struct cred *old_cred;
bbb1e54d
MS
1108 unsigned int i;
1109 bool positive = false;
1110 bool done = false;
1111
1112 /*
1113 * If dentry is negative, then lower is positive iff this is a
1114 * whiteout.
1115 */
1116 if (!dentry->d_inode)
c62520a8 1117 return ovl_dentry_is_opaque(dentry);
bbb1e54d
MS
1118
1119 /* Negative upper -> positive lower */
09d8b586 1120 if (!ovl_dentry_upper(dentry))
bbb1e54d
MS
1121 return true;
1122
6d0a8a90 1123 old_cred = ovl_override_creds(dentry->d_sb);
bbb1e54d
MS
1124 /* Positive upper -> have to look up lower to see whether it exists */
1125 for (i = 0; !done && !positive && i < poe->numlower; i++) {
1126 struct dentry *this;
1127 struct dentry *lowerdir = poe->lowerstack[i].dentry;
1128
1129 this = lookup_one_len_unlocked(name->name, lowerdir,
1130 name->len);
1131 if (IS_ERR(this)) {
1132 switch (PTR_ERR(this)) {
1133 case -ENOENT:
1134 case -ENAMETOOLONG:
1135 break;
1136
1137 default:
1138 /*
1139 * Assume something is there, we just couldn't
1140 * access it.
1141 */
1142 positive = true;
1143 break;
1144 }
1145 } else {
1146 if (this->d_inode) {
1147 positive = !ovl_is_whiteout(this);
1148 done = true;
1149 }
1150 dput(this);
1151 }
1152 }
6d0a8a90 1153 revert_creds(old_cred);
bbb1e54d
MS
1154
1155 return positive;
1156}
This page took 0.319421 seconds and 4 git commands to generate.