Commit 02b69b28 authored by Miklos Szeredi's avatar Miklos Szeredi

ovl: lookup redirects

If a directory has the "trusted.overlay.redirect" xattr, it means that the
value of the xattr should be used to find the underlying directory on the
next lower layer.

The redirect may be relative or absolute.  Absolute redirects begin with a
slash.

A relative redirect means: instead of the current dentry's name use the
value of the redirect to find the directory in the next lower
layer. Relative redirects must not contain a slash.

An absolute redirect means: look up the directory relative to the root of
the overlay using the value of the redirect in the next lower layer.

Redirects work on lower layers as well.
Signed-off-by: default avatarMiklos Szeredi <mszeredi@redhat.com>
parent e28edc46
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include <linux/fs.h> #include <linux/fs.h>
#include <linux/namei.h> #include <linux/namei.h>
#include <linux/xattr.h> #include <linux/xattr.h>
#include <linux/ratelimit.h>
#include "overlayfs.h" #include "overlayfs.h"
#include "ovl_entry.h" #include "ovl_entry.h"
...@@ -19,8 +20,66 @@ struct ovl_lookup_data { ...@@ -19,8 +20,66 @@ struct ovl_lookup_data {
bool opaque; bool opaque;
bool stop; bool stop;
bool last; bool last;
char *redirect;
}; };
static int ovl_check_redirect(struct dentry *dentry, struct ovl_lookup_data *d,
size_t prelen, const char *post)
{
int res;
char *s, *next, *buf = NULL;
res = vfs_getxattr(dentry, OVL_XATTR_REDIRECT, NULL, 0);
if (res < 0) {
if (res == -ENODATA || res == -EOPNOTSUPP)
return 0;
goto fail;
}
buf = kzalloc(prelen + res + strlen(post) + 1, GFP_TEMPORARY);
if (!buf)
return -ENOMEM;
if (res == 0)
goto invalid;
res = vfs_getxattr(dentry, OVL_XATTR_REDIRECT, buf, res);
if (res < 0)
goto fail;
if (res == 0)
goto invalid;
if (buf[0] == '/') {
for (s = buf; *s++ == '/'; s = next) {
next = strchrnul(s, '/');
if (s == next)
goto invalid;
}
} else {
if (strchr(buf, '/') != NULL)
goto invalid;
memmove(buf + prelen, buf, res);
memcpy(buf, d->name.name, prelen);
}
strcat(buf, post);
kfree(d->redirect);
d->redirect = buf;
d->name.name = d->redirect;
d->name.len = strlen(d->redirect);
return 0;
err_free:
kfree(buf);
return 0;
fail:
pr_warn_ratelimited("overlayfs: failed to get redirect (%i)\n", res);
goto err_free;
invalid:
pr_warn_ratelimited("overlayfs: invalid redirect (%s)\n", buf);
goto err_free;
}
static bool ovl_is_opaquedir(struct dentry *dentry) static bool ovl_is_opaquedir(struct dentry *dentry)
{ {
int res; int res;
...@@ -38,6 +97,7 @@ static bool ovl_is_opaquedir(struct dentry *dentry) ...@@ -38,6 +97,7 @@ static bool ovl_is_opaquedir(struct dentry *dentry)
static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d, static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d,
const char *name, unsigned int namelen, const char *name, unsigned int namelen,
size_t prelen, const char *post,
struct dentry **ret) struct dentry **ret)
{ {
struct dentry *this; struct dentry *this;
...@@ -74,6 +134,9 @@ static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d, ...@@ -74,6 +134,9 @@ static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d,
d->stop = d->opaque = true; d->stop = d->opaque = true;
goto out; goto out;
} }
err = ovl_check_redirect(this, d, prelen, post);
if (err)
goto out_err;
out: out:
*ret = this; *ret = this;
return 0; return 0;
...@@ -91,7 +154,32 @@ static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d, ...@@ -91,7 +154,32 @@ static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d,
static int ovl_lookup_layer(struct dentry *base, struct ovl_lookup_data *d, static int ovl_lookup_layer(struct dentry *base, struct ovl_lookup_data *d,
struct dentry **ret) struct dentry **ret)
{ {
return ovl_lookup_single(base, d, d->name.name, d->name.len, ret); const char *s = d->name.name;
struct dentry *dentry = NULL;
int err;
if (*s != '/')
return ovl_lookup_single(base, d, d->name.name, d->name.len,
0, "", ret);
while (*s++ == '/' && !IS_ERR_OR_NULL(base) && d_can_lookup(base)) {
const char *next = strchrnul(s, '/');
size_t slen = strlen(s);
if (WARN_ON(slen > d->name.len) ||
WARN_ON(strcmp(d->name.name + d->name.len - slen, s)))
return -EIO;
err = ovl_lookup_single(base, d, s, next - s,
d->name.len - slen, next, &base);
dput(dentry);
if (err)
return err;
dentry = base;
s = next;
}
*ret = dentry;
return 0;
} }
/* /*
...@@ -127,6 +215,7 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, ...@@ -127,6 +215,7 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
unsigned int ctr = 0; unsigned int ctr = 0;
struct inode *inode = NULL; struct inode *inode = NULL;
bool upperopaque = false; bool upperopaque = false;
char *upperredirect = NULL;
struct dentry *this; struct dentry *this;
unsigned int i; unsigned int i;
int err; int err;
...@@ -136,6 +225,7 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, ...@@ -136,6 +225,7 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
.opaque = false, .opaque = false,
.stop = false, .stop = false,
.last = !poe->numlower, .last = !poe->numlower,
.redirect = NULL,
}; };
if (dentry->d_name.len > ofs->namelen) if (dentry->d_name.len > ofs->namelen)
...@@ -153,12 +243,20 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, ...@@ -153,12 +243,20 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
err = -EREMOTE; err = -EREMOTE;
goto out; goto out;
} }
if (d.redirect) {
upperredirect = kstrdup(d.redirect, GFP_KERNEL);
if (!upperredirect)
goto out_put_upper;
if (d.redirect[0] == '/')
poe = dentry->d_sb->s_root->d_fsdata;
}
upperopaque = d.opaque; upperopaque = d.opaque;
} }
if (!d.stop && poe->numlower) { if (!d.stop && poe->numlower) {
err = -ENOMEM; err = -ENOMEM;
stack = kcalloc(poe->numlower, sizeof(struct path), stack = kcalloc(ofs->numlower, sizeof(struct path),
GFP_TEMPORARY); GFP_TEMPORARY);
if (!stack) if (!stack)
goto out_put_upper; goto out_put_upper;
...@@ -178,6 +276,22 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, ...@@ -178,6 +276,22 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
stack[ctr].dentry = this; stack[ctr].dentry = this;
stack[ctr].mnt = lowerpath.mnt; stack[ctr].mnt = lowerpath.mnt;
ctr++; ctr++;
if (d.stop)
break;
if (d.redirect &&
d.redirect[0] == '/' &&
poe != dentry->d_sb->s_root->d_fsdata) {
poe = dentry->d_sb->s_root->d_fsdata;
/* Find the current layer on the root dentry */
for (i = 0; i < poe->numlower; i++)
if (poe->lowerstack[i].mnt == lowerpath.mnt)
break;
if (WARN_ON(i == poe->numlower))
break;
}
} }
oe = ovl_alloc_entry(ctr); oe = ovl_alloc_entry(ctr);
...@@ -208,9 +322,11 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, ...@@ -208,9 +322,11 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
revert_creds(old_cred); revert_creds(old_cred);
oe->opaque = upperopaque; oe->opaque = upperopaque;
oe->redirect = upperredirect;
oe->__upperdentry = upperdentry; oe->__upperdentry = upperdentry;
memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr); memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr);
kfree(stack); kfree(stack);
kfree(d.redirect);
dentry->d_fsdata = oe; dentry->d_fsdata = oe;
d_add(dentry, inode); d_add(dentry, inode);
...@@ -224,7 +340,9 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, ...@@ -224,7 +340,9 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
kfree(stack); kfree(stack);
out_put_upper: out_put_upper:
dput(upperdentry); dput(upperdentry);
kfree(upperredirect);
out: out:
kfree(d.redirect);
revert_creds(old_cred); revert_creds(old_cred);
return ERR_PTR(err); return ERR_PTR(err);
} }
......
...@@ -19,6 +19,7 @@ enum ovl_path_type { ...@@ -19,6 +19,7 @@ enum ovl_path_type {
#define OVL_XATTR_PREFIX XATTR_TRUSTED_PREFIX "overlay." #define OVL_XATTR_PREFIX XATTR_TRUSTED_PREFIX "overlay."
#define OVL_XATTR_OPAQUE OVL_XATTR_PREFIX "opaque" #define OVL_XATTR_OPAQUE OVL_XATTR_PREFIX "opaque"
#define OVL_XATTR_REDIRECT OVL_XATTR_PREFIX "redirect"
#define OVL_ISUPPER_MASK 1UL #define OVL_ISUPPER_MASK 1UL
......
...@@ -35,6 +35,7 @@ struct ovl_entry { ...@@ -35,6 +35,7 @@ struct ovl_entry {
union { union {
struct { struct {
u64 version; u64 version;
const char *redirect;
bool opaque; bool opaque;
}; };
struct rcu_head rcu; struct rcu_head rcu;
......
...@@ -37,6 +37,7 @@ static void ovl_dentry_release(struct dentry *dentry) ...@@ -37,6 +37,7 @@ static void ovl_dentry_release(struct dentry *dentry)
unsigned int i; unsigned int i;
dput(oe->__upperdentry); dput(oe->__upperdentry);
kfree(oe->redirect);
for (i = 0; i < oe->numlower; i++) for (i = 0; i < oe->numlower; i++)
dput(oe->lowerstack[i].dentry); dput(oe->lowerstack[i].dentry);
kfree_rcu(oe, rcu); kfree_rcu(oe, rcu);
......
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