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