]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - fs/overlayfs/namei.c
b88b2165e8eb5c84fa666973c04a49b005c8ca06
2 * Copyright (C) 2011 Novell Inc.
3 * Copyright (C) 2016 Red Hat, Inc.
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.
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"
20 struct ovl_lookup_data
{
29 static int ovl_check_redirect(struct dentry
*dentry
, struct ovl_lookup_data
*d
,
30 size_t prelen
, const char *post
)
33 char *s
, *next
, *buf
= NULL
;
35 res
= vfs_getxattr(dentry
, OVL_XATTR_REDIRECT
, NULL
, 0);
37 if (res
== -ENODATA
|| res
== -EOPNOTSUPP
)
41 buf
= kzalloc(prelen
+ res
+ strlen(post
) + 1, GFP_TEMPORARY
);
48 res
= vfs_getxattr(dentry
, OVL_XATTR_REDIRECT
, buf
, res
);
54 for (s
= buf
; *s
++ == '/'; s
= next
) {
55 next
= strchrnul(s
, '/');
60 if (strchr(buf
, '/') != NULL
)
63 memmove(buf
+ prelen
, buf
, res
);
64 memcpy(buf
, d
->name
.name
, prelen
);
70 d
->name
.name
= d
->redirect
;
71 d
->name
.len
= strlen(d
->redirect
);
79 pr_warn_ratelimited("overlayfs: failed to get redirect (%i)\n", res
);
82 pr_warn_ratelimited("overlayfs: invalid redirect (%s)\n", buf
);
86 static int ovl_acceptable(void *ctx
, struct dentry
*dentry
)
91 static struct ovl_fh
*ovl_get_origin_fh(struct dentry
*dentry
)
94 struct ovl_fh
*fh
= NULL
;
96 res
= vfs_getxattr(dentry
, OVL_XATTR_ORIGIN
, NULL
, 0);
98 if (res
== -ENODATA
|| res
== -EOPNOTSUPP
)
102 /* Zero size value means "copied up but origin unknown" */
106 fh
= kzalloc(res
, GFP_TEMPORARY
);
108 return ERR_PTR(-ENOMEM
);
110 res
= vfs_getxattr(dentry
, OVL_XATTR_ORIGIN
, fh
, res
);
114 if (res
< sizeof(struct ovl_fh
) || res
< fh
->len
)
117 if (fh
->magic
!= OVL_FH_MAGIC
)
120 /* Treat larger version and unknown flags as "origin unknown" */
121 if (fh
->version
> OVL_FH_VERSION
|| fh
->flags
& ~OVL_FH_FLAG_ALL
)
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
)
136 pr_warn_ratelimited("overlayfs: failed to get origin (%i)\n", res
);
139 pr_warn_ratelimited("overlayfs: invalid origin (%*phN)\n", res
, fh
);
143 static struct dentry
*ovl_get_origin(struct dentry
*dentry
,
144 struct vfsmount
*mnt
)
146 struct dentry
*origin
= NULL
;
147 struct ovl_fh
*fh
= ovl_get_origin_fh(dentry
);
150 if (IS_ERR_OR_NULL(fh
))
151 return (struct dentry
*)fh
;
154 * Make sure that the stored uuid matches the uuid of the lower
155 * layer where file handle will be decoded.
157 if (!uuid_equal(&fh
->uuid
, &mnt
->mnt_sb
->s_uuid
))
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
))
171 if (ovl_dentry_weird(origin
) ||
172 ((d_inode(origin
)->i_mode
^ d_inode(dentry
)->i_mode
) & S_IFMT
))
180 pr_warn_ratelimited("overlayfs: invalid origin (%pd2)\n", origin
);
186 static bool ovl_is_opaquedir(struct dentry
*dentry
)
188 return ovl_check_dir_xattr(dentry
, OVL_XATTR_OPAQUE
);
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
,
199 this = lookup_one_len_unlocked(name
, base
, namelen
);
203 if (err
== -ENOENT
|| err
== -ENAMETOOLONG
)
210 if (ovl_dentry_weird(this)) {
211 /* Don't support traversing automounts and other weirdness */
215 if (ovl_is_whiteout(this)) {
216 d
->stop
= d
->opaque
= true;
219 if (!d_can_lookup(this)) {
226 if (!d
->last
&& ovl_is_opaquedir(this)) {
227 d
->stop
= d
->opaque
= true;
230 err
= ovl_check_redirect(this, d
, prelen
, post
);
247 static int ovl_lookup_layer(struct dentry
*base
, struct ovl_lookup_data
*d
,
250 /* Counting down from the end, since the prefix can change */
251 size_t rem
= d
->name
.len
- 1;
252 struct dentry
*dentry
= NULL
;
255 if (d
->name
.name
[0] != '/')
256 return ovl_lookup_single(base
, d
, d
->name
.name
, d
->name
.len
,
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
;
265 /* Verify we did not go off the rails */
266 if (WARN_ON(s
[-1] != '/'))
269 err
= ovl_lookup_single(base
, d
, s
, thislen
,
270 d
->name
.len
- rem
, next
, &base
);
280 if (WARN_ON(rem
>= d
->name
.len
))
288 static int ovl_check_origin(struct dentry
*upperdentry
,
289 struct path
*lowerstack
, unsigned int numlower
,
290 struct path
**stackp
, unsigned int *ctrp
)
292 struct vfsmount
*mnt
;
293 struct dentry
*origin
= NULL
;
297 for (i
= 0; i
< numlower
; i
++) {
298 mnt
= lowerstack
[i
].mnt
;
299 origin
= ovl_get_origin(upperdentry
, mnt
);
301 return PTR_ERR(origin
);
312 *stackp
= kmalloc(sizeof(struct path
), GFP_TEMPORARY
);
317 **stackp
= (struct path
) { .dentry
= origin
, .mnt
= mnt
};
324 * Verify that @fh matches the origin file handle stored in OVL_XATTR_ORIGIN.
325 * Return 0 on match, -ESTALE on mismatch, < 0 on error.
327 static int ovl_verify_origin_fh(struct dentry
*dentry
, const struct ovl_fh
*fh
)
329 struct ovl_fh
*ofh
= ovl_get_origin_fh(dentry
);
338 if (fh
->len
!= ofh
->len
|| memcmp(fh
, ofh
, fh
->len
))
346 * Verify that an inode matches the origin file handle stored in upper inode.
348 * If @set is true and there is no stored file handle, encode and store origin
349 * file handle in OVL_XATTR_ORIGIN.
351 * Return 0 on match, -ESTALE on mismatch, < 0 on error.
353 int ovl_verify_origin(struct dentry
*dentry
, struct vfsmount
*mnt
,
354 struct dentry
*origin
, bool is_upper
, bool set
)
360 fh
= ovl_encode_fh(origin
, is_upper
);
365 err
= ovl_verify_origin_fh(dentry
, fh
);
366 if (set
&& err
== -ENODATA
)
367 err
= ovl_do_setxattr(dentry
, OVL_XATTR_ORIGIN
, fh
, fh
->len
, 0);
376 inode
= d_inode(origin
);
377 pr_warn_ratelimited("overlayfs: failed to verify origin (%pd2, ino=%lu, err=%i)\n",
378 origin
, inode
? inode
->i_ino
: 0, err
);
383 * Verify that an index entry name matches the origin file handle stored in
384 * OVL_XATTR_ORIGIN and that origin file handle can be decoded to lower path.
385 * Return 0 on match, -ESTALE on mismatch or stale origin, < 0 on error.
387 int ovl_verify_index(struct dentry
*index
, struct path
*lowerstack
,
388 unsigned int numlower
)
390 struct ovl_fh
*fh
= NULL
;
392 struct path origin
= { };
393 struct path
*stack
= &origin
;
394 unsigned int ctr
= 0;
401 * Directory index entries are going to be used for looking up
402 * redirected upper dirs by lower dir fh when decoding an overlay
403 * file handle of a merge dir. Whiteout index entries are going to be
404 * used as an indication that an exported overlay file handle should
405 * be treated as stale (i.e. after unlink of the overlay inode).
406 * We don't know the verification rules for directory and whiteout
407 * index entries, because they have not been implemented yet, so return
408 * EROFS if those entries are found to avoid corrupting an index that
409 * was created by a newer kernel.
412 if (d_is_dir(index
) || ovl_is_whiteout(index
))
416 if (index
->d_name
.len
< sizeof(struct ovl_fh
)*2)
420 len
= index
->d_name
.len
/ 2;
421 fh
= kzalloc(len
, GFP_TEMPORARY
);
426 if (hex2bin((u8
*)fh
, index
->d_name
.name
, len
) || len
!= fh
->len
)
429 err
= ovl_verify_origin_fh(index
, fh
);
433 err
= ovl_check_origin(index
, lowerstack
, numlower
, &stack
, &ctr
);
439 /* Check if index is orphan and don't warn before cleaning it */
440 if (d_inode(index
)->i_nlink
== 1 &&
441 ovl_get_nlink(index
, origin
.dentry
, 0) == 0)
450 pr_warn_ratelimited("overlayfs: failed to verify index (%pd2, ftype=%x, err=%i)\n",
451 index
, d_inode(index
)->i_mode
& S_IFMT
, err
);
456 * Lookup in indexdir for the index entry of a lower real inode or a copy up
457 * origin inode. The index entry name is the hex representation of the lower
460 * If the index dentry in negative, then either no lower aliases have been
461 * copied up yet, or aliases have been copied up in older kernels and are
464 * If the index dentry for a copy up origin inode is positive, but points
465 * to an inode different than the upper inode, then either the upper inode
466 * has been copied up and not indexed or it was indexed, but since then
467 * index dir was cleared. Either way, that index cannot be used to indentify
470 int ovl_get_index_name(struct dentry
*origin
, struct qstr
*name
)
476 fh
= ovl_encode_fh(origin
, false);
481 n
= kzalloc(fh
->len
* 2, GFP_TEMPORARY
);
483 s
= bin2hex(n
, fh
, fh
->len
);
484 *name
= (struct qstr
) QSTR_INIT(n
, s
- n
);
493 static struct dentry
*ovl_lookup_index(struct dentry
*dentry
,
494 struct dentry
*upper
,
495 struct dentry
*origin
)
497 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
498 struct dentry
*index
;
503 err
= ovl_get_index_name(origin
, &name
);
507 index
= lookup_one_len_unlocked(name
.name
, ofs
->indexdir
, name
.len
);
509 err
= PTR_ERR(index
);
510 pr_warn_ratelimited("overlayfs: failed inode index lookup (ino=%lu, key=%*s, err=%i);\n"
511 "overlayfs: mount with '-o index=off' to disable inodes index.\n",
512 d_inode(origin
)->i_ino
, name
.len
, name
.name
,
517 inode
= d_inode(index
);
518 if (d_is_negative(index
)) {
520 } else if (upper
&& d_inode(upper
) != inode
) {
522 } else if (ovl_dentry_weird(index
) || ovl_is_whiteout(index
) ||
523 ((inode
->i_mode
^ d_inode(origin
)->i_mode
) & S_IFMT
)) {
525 * Index should always be of the same file type as origin
526 * except for the case of a whiteout index. A whiteout
527 * index should only exist if all lower aliases have been
528 * unlinked, which means that finding a lower origin on lookup
529 * whose index is a whiteout should be treated as an error.
531 pr_warn_ratelimited("overlayfs: bad index found (index=%pd2, ftype=%x, origin ftype=%x).\n",
532 index
, d_inode(index
)->i_mode
& S_IFMT
,
533 d_inode(origin
)->i_mode
& S_IFMT
);
548 index
= ERR_PTR(-EIO
);
553 * Returns next layer in stack starting from top.
554 * Returns -1 if this is the last layer.
556 int ovl_path_next(int idx
, struct dentry
*dentry
, struct path
*path
)
558 struct ovl_entry
*oe
= dentry
->d_fsdata
;
562 ovl_path_upper(dentry
, path
);
564 return oe
->numlower
? 1 : -1;
567 BUG_ON(idx
> oe
->numlower
);
568 *path
= oe
->lowerstack
[idx
- 1];
570 return (idx
< oe
->numlower
) ? idx
+ 1 : -1;
573 struct dentry
*ovl_lookup(struct inode
*dir
, struct dentry
*dentry
,
576 struct ovl_entry
*oe
;
577 const struct cred
*old_cred
;
578 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
579 struct ovl_entry
*poe
= dentry
->d_parent
->d_fsdata
;
580 struct ovl_entry
*roe
= dentry
->d_sb
->s_root
->d_fsdata
;
581 struct path
*stack
= NULL
;
582 struct dentry
*upperdir
, *upperdentry
= NULL
;
583 struct dentry
*index
= NULL
;
584 unsigned int ctr
= 0;
585 struct inode
*inode
= NULL
;
586 bool upperopaque
= false;
587 char *upperredirect
= NULL
;
591 struct ovl_lookup_data d
= {
592 .name
= dentry
->d_name
,
596 .last
= !poe
->numlower
,
600 if (dentry
->d_name
.len
> ofs
->namelen
)
601 return ERR_PTR(-ENAMETOOLONG
);
603 old_cred
= ovl_override_creds(dentry
->d_sb
);
604 upperdir
= ovl_dentry_upper(dentry
->d_parent
);
606 err
= ovl_lookup_layer(upperdir
, &d
, &upperdentry
);
610 if (upperdentry
&& unlikely(ovl_dentry_remote(upperdentry
))) {
615 if (upperdentry
&& !d
.is_dir
) {
616 BUG_ON(!d
.stop
|| d
.redirect
);
618 * Lookup copy up origin by decoding origin file handle.
619 * We may get a disconnected dentry, which is fine,
620 * because we only need to hold the origin inode in
621 * cache and use its inode number. We may even get a
622 * connected dentry, that is not under any of the lower
623 * layers root. That is also fine for using it's inode
624 * number - it's the same as if we held a reference
625 * to a dentry in lower layer that was moved under us.
627 err
= ovl_check_origin(upperdentry
, roe
->lowerstack
,
628 roe
->numlower
, &stack
, &ctr
);
634 upperredirect
= kstrdup(d
.redirect
, GFP_KERNEL
);
637 if (d
.redirect
[0] == '/')
640 upperopaque
= d
.opaque
;
643 if (!d
.stop
&& poe
->numlower
) {
645 stack
= kcalloc(ofs
->numlower
, sizeof(struct path
),
651 for (i
= 0; !d
.stop
&& i
< poe
->numlower
; i
++) {
652 struct path lowerpath
= poe
->lowerstack
[i
];
654 d
.last
= i
== poe
->numlower
- 1;
655 err
= ovl_lookup_layer(lowerpath
.dentry
, &d
, &this);
662 stack
[ctr
].dentry
= this;
663 stack
[ctr
].mnt
= lowerpath
.mnt
;
669 if (d
.redirect
&& d
.redirect
[0] == '/' && poe
!= roe
) {
672 /* Find the current layer on the root dentry */
673 for (i
= 0; i
< poe
->numlower
; i
++)
674 if (poe
->lowerstack
[i
].mnt
== lowerpath
.mnt
)
676 if (WARN_ON(i
== poe
->numlower
))
681 /* Lookup index by lower inode and verify it matches upper inode */
682 if (ctr
&& !d
.is_dir
&& ovl_indexdir(dentry
->d_sb
)) {
683 struct dentry
*origin
= stack
[0].dentry
;
685 index
= ovl_lookup_index(dentry
, upperdentry
, origin
);
687 err
= PTR_ERR(index
);
693 oe
= ovl_alloc_entry(ctr
);
698 oe
->opaque
= upperopaque
;
699 memcpy(oe
->lowerstack
, stack
, sizeof(struct path
) * ctr
);
700 dentry
->d_fsdata
= oe
;
703 ovl_dentry_set_upper_alias(dentry
);
705 upperdentry
= dget(index
);
707 if (upperdentry
|| ctr
) {
708 inode
= ovl_get_inode(dentry
, upperdentry
, index
);
709 err
= PTR_ERR(inode
);
713 OVL_I(inode
)->redirect
= upperredirect
;
715 ovl_set_flag(OVL_INDEX
, inode
);
718 revert_creds(old_cred
);
722 d_add(dentry
, inode
);
727 dentry
->d_fsdata
= NULL
;
731 for (i
= 0; i
< ctr
; i
++)
732 dput(stack
[i
].dentry
);
736 kfree(upperredirect
);
739 revert_creds(old_cred
);
743 bool ovl_lower_positive(struct dentry
*dentry
)
745 struct ovl_entry
*oe
= dentry
->d_fsdata
;
746 struct ovl_entry
*poe
= dentry
->d_parent
->d_fsdata
;
747 const struct qstr
*name
= &dentry
->d_name
;
749 bool positive
= false;
753 * If dentry is negative, then lower is positive iff this is a
756 if (!dentry
->d_inode
)
759 /* Negative upper -> positive lower */
760 if (!ovl_dentry_upper(dentry
))
763 /* Positive upper -> have to look up lower to see whether it exists */
764 for (i
= 0; !done
&& !positive
&& i
< poe
->numlower
; i
++) {
766 struct dentry
*lowerdir
= poe
->lowerstack
[i
].dentry
;
768 this = lookup_one_len_unlocked(name
->name
, lowerdir
,
771 switch (PTR_ERR(this)) {
778 * Assume something is there, we just couldn't
786 positive
= !ovl_is_whiteout(this);