Commit 9d3dfea3 authored by Vivek Goyal's avatar Vivek Goyal Committed by Miklos Szeredi

ovl: Modify ovl_lookup() and friends to lookup metacopy dentry

This patch modifies ovl_lookup() and friends to lookup metacopy dentries.
It also allows for presence of metacopy dentries in lower layer.

During lookup, check for presence of OVL_XATTR_METACOPY and if not present,
set OVL_UPPERDATA bit in flags.

We don't support metacopy feature with nfs_export.  So in nfs_export code,
we set OVL_UPPERDATA flag set unconditionally if upper inode exists.

Do not follow metacopy origin if we find a metacopy only inode and metacopy
feature is not enabled for that mount.  Like redirect, this can have
security implications where an attacker could hand craft upper and try to
gain access to file on lower which it should not have to begin with.
Signed-off-by: default avatarVivek Goyal <vgoyal@redhat.com>
Reviewed-by: default avatarAmir Goldstein <amir73il@gmail.com>
Signed-off-by: default avatarMiklos Szeredi <mszeredi@redhat.com>
parent 027065b7
...@@ -317,6 +317,9 @@ static struct dentry *ovl_obtain_alias(struct super_block *sb, ...@@ -317,6 +317,9 @@ static struct dentry *ovl_obtain_alias(struct super_block *sb,
return ERR_CAST(inode); return ERR_CAST(inode);
} }
if (upper)
ovl_set_flag(OVL_UPPERDATA, inode);
dentry = d_find_any_alias(inode); dentry = d_find_any_alias(inode);
if (!dentry) { if (!dentry) {
dentry = d_alloc_anon(inode->i_sb); dentry = d_alloc_anon(inode->i_sb);
......
...@@ -780,7 +780,7 @@ struct inode *ovl_get_inode(struct super_block *sb, ...@@ -780,7 +780,7 @@ struct inode *ovl_get_inode(struct super_block *sb,
bool bylower = ovl_hash_bylower(sb, upperdentry, lowerdentry, bool bylower = ovl_hash_bylower(sb, upperdentry, lowerdentry,
oip->index); oip->index);
int fsid = bylower ? oip->lowerpath->layer->fsid : 0; int fsid = bylower ? oip->lowerpath->layer->fsid : 0;
bool is_dir; bool is_dir, metacopy = false;
unsigned long ino = 0; unsigned long ino = 0;
int err = -ENOMEM; int err = -ENOMEM;
...@@ -839,6 +839,15 @@ struct inode *ovl_get_inode(struct super_block *sb, ...@@ -839,6 +839,15 @@ struct inode *ovl_get_inode(struct super_block *sb,
if (oip->index) if (oip->index)
ovl_set_flag(OVL_INDEX, inode); ovl_set_flag(OVL_INDEX, inode);
if (upperdentry) {
err = ovl_check_metacopy_xattr(upperdentry);
if (err < 0)
goto out_err;
metacopy = err;
if (!metacopy)
ovl_set_flag(OVL_UPPERDATA, inode);
}
OVL_I(inode)->redirect = oip->redirect; OVL_I(inode)->redirect = oip->redirect;
/* Check for non-merge dir that may have whiteouts */ /* Check for non-merge dir that may have whiteouts */
......
...@@ -24,6 +24,7 @@ struct ovl_lookup_data { ...@@ -24,6 +24,7 @@ struct ovl_lookup_data {
bool stop; bool stop;
bool last; bool last;
char *redirect; char *redirect;
bool metacopy;
}; };
static int ovl_check_redirect(struct dentry *dentry, struct ovl_lookup_data *d, static int ovl_check_redirect(struct dentry *dentry, struct ovl_lookup_data *d,
...@@ -252,16 +253,25 @@ static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d, ...@@ -252,16 +253,25 @@ static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d,
d->stop = d->opaque = true; d->stop = d->opaque = true;
goto put_and_out; goto put_and_out;
} }
/*
* This dentry should be a regular file if previous layer lookup
* found a metacopy dentry.
*/
if (last_element && d->metacopy && !d_is_reg(this)) {
d->stop = true;
goto put_and_out;
}
if (!d_can_lookup(this)) { if (!d_can_lookup(this)) {
if (d->is_dir || !last_element) {
d->stop = true; d->stop = true;
if (d->is_dir)
goto put_and_out; goto put_and_out;
}
err = ovl_check_metacopy_xattr(this);
if (err < 0)
goto out_err;
/* d->metacopy = err;
* NB: handle failure to lookup non-last element when non-dir d->stop = !d->metacopy;
* redirects become possible
*/
WARN_ON(!last_element);
goto out; goto out;
} }
if (last_element) if (last_element)
...@@ -823,7 +833,7 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, ...@@ -823,7 +833,7 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
struct ovl_fs *ofs = dentry->d_sb->s_fs_info; struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
struct ovl_entry *poe = dentry->d_parent->d_fsdata; struct ovl_entry *poe = dentry->d_parent->d_fsdata;
struct ovl_entry *roe = dentry->d_sb->s_root->d_fsdata; struct ovl_entry *roe = dentry->d_sb->s_root->d_fsdata;
struct ovl_path *stack = NULL; struct ovl_path *stack = NULL, *origin_path = NULL;
struct dentry *upperdir, *upperdentry = NULL; struct dentry *upperdir, *upperdentry = NULL;
struct dentry *origin = NULL; struct dentry *origin = NULL;
struct dentry *index = NULL; struct dentry *index = NULL;
...@@ -834,6 +844,7 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, ...@@ -834,6 +844,7 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
struct dentry *this; struct dentry *this;
unsigned int i; unsigned int i;
int err; int err;
bool metacopy = false;
struct ovl_lookup_data d = { struct ovl_lookup_data d = {
.name = dentry->d_name, .name = dentry->d_name,
.is_dir = false, .is_dir = false,
...@@ -841,6 +852,7 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, ...@@ -841,6 +852,7 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
.stop = false, .stop = false,
.last = ofs->config.redirect_follow ? false : !poe->numlower, .last = ofs->config.redirect_follow ? false : !poe->numlower,
.redirect = NULL, .redirect = NULL,
.metacopy = false,
}; };
if (dentry->d_name.len > ofs->namelen) if (dentry->d_name.len > ofs->namelen)
...@@ -859,7 +871,9 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, ...@@ -859,7 +871,9 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
goto out; goto out;
} }
if (upperdentry && !d.is_dir) { if (upperdentry && !d.is_dir) {
BUG_ON(!d.stop || d.redirect); unsigned int origin_ctr = 0;
BUG_ON(d.redirect);
/* /*
* Lookup copy up origin by decoding origin file handle. * Lookup copy up origin by decoding origin file handle.
* We may get a disconnected dentry, which is fine, * We may get a disconnected dentry, which is fine,
...@@ -870,9 +884,13 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, ...@@ -870,9 +884,13 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
* number - it's the same as if we held a reference * number - it's the same as if we held a reference
* to a dentry in lower layer that was moved under us. * to a dentry in lower layer that was moved under us.
*/ */
err = ovl_check_origin(ofs, upperdentry, &stack, &ctr); err = ovl_check_origin(ofs, upperdentry, &origin_path,
&origin_ctr);
if (err) if (err)
goto out_put_upper; goto out_put_upper;
if (d.metacopy)
metacopy = true;
} }
if (d.redirect) { if (d.redirect) {
...@@ -913,7 +931,7 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, ...@@ -913,7 +931,7 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
* If no origin fh is stored in upper of a merge dir, store fh * If no origin fh is stored in upper of a merge dir, store fh
* of lower dir and set upper parent "impure". * of lower dir and set upper parent "impure".
*/ */
if (upperdentry && !ctr && !ofs->noxattr) { if (upperdentry && !ctr && !ofs->noxattr && d.is_dir) {
err = ovl_fix_origin(dentry, this, upperdentry); err = ovl_fix_origin(dentry, this, upperdentry);
if (err) { if (err) {
dput(this); dput(this);
...@@ -925,18 +943,35 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, ...@@ -925,18 +943,35 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
* When "verify_lower" feature is enabled, do not merge with a * When "verify_lower" feature is enabled, do not merge with a
* lower dir that does not match a stored origin xattr. In any * lower dir that does not match a stored origin xattr. In any
* case, only verified origin is used for index lookup. * case, only verified origin is used for index lookup.
*
* For non-dir dentry, if index=on, then ensure origin
* matches the dentry found using path based lookup,
* otherwise error out.
*/ */
if (upperdentry && !ctr && ovl_verify_lower(dentry->d_sb)) { if (upperdentry && !ctr &&
((d.is_dir && ovl_verify_lower(dentry->d_sb)) ||
(!d.is_dir && ofs->config.index && origin_path))) {
err = ovl_verify_origin(upperdentry, this, false); err = ovl_verify_origin(upperdentry, this, false);
if (err) { if (err) {
dput(this); dput(this);
if (d.is_dir)
break; break;
goto out_put;
} }
/* Bless lower dir as verified origin */
origin = this; origin = this;
} }
if (d.metacopy)
metacopy = true;
/*
* Do not store intermediate metacopy dentries in chain,
* except top most lower metacopy dentry
*/
if (d.metacopy && ctr) {
dput(this);
continue;
}
stack[ctr].dentry = this; stack[ctr].dentry = this;
stack[ctr].layer = lower.layer; stack[ctr].layer = lower.layer;
ctr++; ctr++;
...@@ -968,13 +1003,48 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, ...@@ -968,13 +1003,48 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
} }
} }
if (metacopy) {
/*
* Found a metacopy dentry but did not find corresponding
* data dentry
*/
if (d.metacopy) {
err = -EIO;
goto out_put;
}
err = -EPERM;
if (!ofs->config.metacopy) {
pr_warn_ratelimited("overlay: refusing to follow metacopy origin for (%pd2)\n",
dentry);
goto out_put;
}
} else if (!d.is_dir && upperdentry && !ctr && origin_path) {
if (WARN_ON(stack != NULL)) {
err = -EIO;
goto out_put;
}
stack = origin_path;
ctr = 1;
origin_path = NULL;
}
/* /*
* Lookup index by lower inode and verify it matches upper inode. * Lookup index by lower inode and verify it matches upper inode.
* We only trust dir index if we verified that lower dir matches * We only trust dir index if we verified that lower dir matches
* origin, otherwise dir index entries may be inconsistent and we * origin, otherwise dir index entries may be inconsistent and we
* ignore them. Always lookup index of non-dir and non-upper. * ignore them.
*
* For non-dir upper metacopy dentry, we already set "origin" if we
* verified that lower matched upper origin. If upper origin was
* not present (because lower layer did not support fh encode/decode),
* or indexing is not enabled, do not set "origin" and skip looking up
* index. This case should be handled in same way as a non-dir upper
* without ORIGIN is handled.
*
* Always lookup index of non-dir non-metacopy and non-upper.
*/ */
if (ctr && (!upperdentry || !d.is_dir)) if (ctr && (!upperdentry || (!d.is_dir && !metacopy)))
origin = stack[0].dentry; origin = stack[0].dentry;
if (origin && ovl_indexdir(dentry->d_sb) && if (origin && ovl_indexdir(dentry->d_sb) &&
...@@ -1019,6 +1089,10 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, ...@@ -1019,6 +1089,10 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
} }
revert_creds(old_cred); revert_creds(old_cred);
if (origin_path) {
dput(origin_path->dentry);
kfree(origin_path);
}
dput(index); dput(index);
kfree(stack); kfree(stack);
kfree(d.redirect); kfree(d.redirect);
...@@ -1033,6 +1107,10 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, ...@@ -1033,6 +1107,10 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
dput(stack[i].dentry); dput(stack[i].dentry);
kfree(stack); kfree(stack);
out_put_upper: out_put_upper:
if (origin_path) {
dput(origin_path->dentry);
kfree(origin_path);
}
dput(upperdentry); dput(upperdentry);
kfree(upperredirect); kfree(upperredirect);
out: out:
......
...@@ -268,6 +268,7 @@ bool ovl_need_index(struct dentry *dentry); ...@@ -268,6 +268,7 @@ bool ovl_need_index(struct dentry *dentry);
int ovl_nlink_start(struct dentry *dentry, bool *locked); int ovl_nlink_start(struct dentry *dentry, bool *locked);
void ovl_nlink_end(struct dentry *dentry, bool locked); void ovl_nlink_end(struct dentry *dentry, bool locked);
int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir); int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir);
int ovl_check_metacopy_xattr(struct dentry *dentry);
static inline bool ovl_is_impuredir(struct dentry *dentry) static inline bool ovl_is_impuredir(struct dentry *dentry)
{ {
......
...@@ -778,3 +778,25 @@ int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir) ...@@ -778,3 +778,25 @@ int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
pr_err("overlayfs: failed to lock workdir+upperdir\n"); pr_err("overlayfs: failed to lock workdir+upperdir\n");
return -EIO; return -EIO;
} }
/* err < 0, 0 if no metacopy xattr, 1 if metacopy xattr found */
int ovl_check_metacopy_xattr(struct dentry *dentry)
{
int res;
/* Only regular files can have metacopy xattr */
if (!S_ISREG(d_inode(dentry)->i_mode))
return 0;
res = vfs_getxattr(dentry, OVL_XATTR_METACOPY, NULL, 0);
if (res < 0) {
if (res == -ENODATA || res == -EOPNOTSUPP)
return 0;
goto out;
}
return 1;
out:
pr_warn_ratelimited("overlayfs: failed to get metacopy (%i)\n", res);
return res;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment