Skip to content

Commit

Permalink
ovl: use ovl_path_getxattr() wrapper
Browse files Browse the repository at this point in the history
Add a helper that allows to retrieve ovl xattrs from either lower or
upper layers. To stop passing mnt and dentry separately everywhere use
struct path which more accurately reflects the tight coupling between
mount and dentry in this helper. Swich over all places to pass a path
argument that can operate on either upper or lower layers. This is
needed to support idmapped base layers with overlayfs.

Some helpers are always called with an upper dentry, which is now utilized
by these helpers to create the path.  Make this usage explicit by renaming
the argument to "upperdentry" and by renaming the function as well in some
cases.  Also add a check in ovl_do_getxattr() to catch misuse of these
functions.

Cc: <linux-unionfs@vger.kernel.org>
Tested-by: Giuseppe Scrivano <gscrivan@redhat.com>
Reviewed-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Christian Brauner (Microsoft) <brauner@kernel.org>
Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
  • Loading branch information
Christian Brauner authored and Miklos Szeredi committed Apr 28, 2022
1 parent 22f289c commit dad7017
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 65 deletions.
21 changes: 10 additions & 11 deletions fs/overlayfs/copy_up.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ static bool ovl_must_copy_xattr(const char *name)
!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN);
}

int ovl_copy_xattr(struct super_block *sb, struct dentry *old,
struct dentry *new)
int ovl_copy_xattr(struct super_block *sb, struct path *oldpath, struct dentry *new)
{
struct dentry *old = oldpath->dentry;
ssize_t list_size, size, value_size = 0;
char *buf, *name, *value = NULL;
int error = 0;
Expand Down Expand Up @@ -94,9 +94,9 @@ int ovl_copy_xattr(struct super_block *sb, struct dentry *old,
continue; /* Discard */
}
retry:
size = vfs_getxattr(&init_user_ns, old, name, value, value_size);
size = ovl_do_getxattr(oldpath, name, value, value_size);
if (size == -ERANGE)
size = vfs_getxattr(&init_user_ns, old, name, NULL, 0);
size = ovl_do_getxattr(oldpath, name, NULL, 0);

if (size < 0) {
error = size;
Expand Down Expand Up @@ -582,7 +582,7 @@ static int ovl_copy_up_inode(struct ovl_copy_up_ctx *c, struct dentry *temp)
return err;
}

err = ovl_copy_xattr(c->dentry->d_sb, c->lowerpath.dentry, temp);
err = ovl_copy_xattr(c->dentry->d_sb, &c->lowerpath, temp);
if (err)
return err;

Expand Down Expand Up @@ -873,13 +873,12 @@ static bool ovl_need_meta_copy_up(struct dentry *dentry, umode_t mode,
return true;
}

static ssize_t ovl_getxattr_value(struct ovl_fs *ofs, struct dentry *dentry,
char *name, char **value)
static ssize_t ovl_getxattr_value(struct path *path, char *name, char **value)
{
ssize_t res;
char *buf;

res = ovl_do_getxattr(ofs, dentry, name, NULL, 0);
res = ovl_do_getxattr(path, name, NULL, 0);
if (res == -ENODATA || res == -EOPNOTSUPP)
res = 0;

Expand All @@ -888,7 +887,7 @@ static ssize_t ovl_getxattr_value(struct ovl_fs *ofs, struct dentry *dentry,
if (!buf)
return -ENOMEM;

res = ovl_do_getxattr(ofs, dentry, name, buf, res);
res = ovl_do_getxattr(path, name, buf, res);
if (res < 0)
kfree(buf);
else
Expand All @@ -915,8 +914,8 @@ static int ovl_copy_up_meta_inode_data(struct ovl_copy_up_ctx *c)
return -EIO;

if (c->stat.size) {
err = cap_size = ovl_getxattr_value(ofs, upperpath.dentry,
XATTR_NAME_CAPS, &capability);
err = cap_size = ovl_getxattr_value(&upperpath, XATTR_NAME_CAPS,
&capability);
if (cap_size < 0)
goto out;
}
Expand Down
2 changes: 1 addition & 1 deletion fs/overlayfs/dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ static struct dentry *ovl_clear_empty(struct dentry *dentry,
if (IS_ERR(opaquedir))
goto out_unlock;

err = ovl_copy_xattr(dentry->d_sb, upper, opaquedir);
err = ovl_copy_xattr(dentry->d_sb, &upperpath, opaquedir);
if (err)
goto out_cleanup;

Expand Down
10 changes: 7 additions & 3 deletions fs/overlayfs/inode.c
Original file line number Diff line number Diff line change
Expand Up @@ -899,8 +899,8 @@ unsigned int ovl_get_nlink(struct ovl_fs *ofs, struct dentry *lowerdentry,
if (!lowerdentry || !upperdentry || d_inode(lowerdentry)->i_nlink == 1)
return fallback;

err = ovl_getxattr(ofs, upperdentry, OVL_XATTR_NLINK,
&buf, sizeof(buf) - 1);
err = ovl_getxattr_upper(ofs, upperdentry, OVL_XATTR_NLINK,
&buf, sizeof(buf) - 1);
if (err < 0)
goto fail;

Expand Down Expand Up @@ -1104,6 +1104,10 @@ struct inode *ovl_get_inode(struct super_block *sb,
struct inode *realinode = upperdentry ? d_inode(upperdentry) : NULL;
struct inode *inode;
struct dentry *lowerdentry = lowerpath ? lowerpath->dentry : NULL;
struct path realpath = {
.dentry = upperdentry ?: lowerdentry,
.mnt = upperdentry ? ovl_upper_mnt(ofs) : lowerpath->layer->mnt,
};
bool bylower = ovl_hash_bylower(sb, upperdentry, lowerdentry,
oip->index);
int fsid = bylower ? lowerpath->layer->fsid : 0;
Expand Down Expand Up @@ -1177,7 +1181,7 @@ struct inode *ovl_get_inode(struct super_block *sb,
/* Check for non-merge dir that may have whiteouts */
if (is_dir) {
if (((upperdentry && lowerdentry) || oip->numlower > 1) ||
ovl_check_origin_xattr(ofs, upperdentry ?: lowerdentry)) {
ovl_path_check_origin_xattr(ofs, &realpath)) {
ovl_set_flag(OVL_WHITEOUTS, inode);
}
}
Expand Down
37 changes: 24 additions & 13 deletions fs/overlayfs/namei.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

struct ovl_lookup_data {
struct super_block *sb;
struct vfsmount *mnt;
struct qstr name;
bool is_dir;
bool opaque;
Expand All @@ -25,14 +26,14 @@ struct ovl_lookup_data {
bool metacopy;
};

static int ovl_check_redirect(struct dentry *dentry, struct ovl_lookup_data *d,
static int ovl_check_redirect(struct path *path, struct ovl_lookup_data *d,
size_t prelen, const char *post)
{
int res;
char *buf;
struct ovl_fs *ofs = OVL_FS(d->sb);

buf = ovl_get_redirect_xattr(ofs, dentry, prelen + strlen(post));
buf = ovl_get_redirect_xattr(ofs, path, prelen + strlen(post));
if (IS_ERR_OR_NULL(buf))
return PTR_ERR(buf);

Expand Down Expand Up @@ -105,13 +106,13 @@ int ovl_check_fb_len(struct ovl_fb *fb, int fb_len)
return 0;
}

static struct ovl_fh *ovl_get_fh(struct ovl_fs *ofs, struct dentry *dentry,
static struct ovl_fh *ovl_get_fh(struct ovl_fs *ofs, struct dentry *upperdentry,
enum ovl_xattr ox)
{
int res, err;
struct ovl_fh *fh = NULL;

res = ovl_getxattr(ofs, dentry, ox, NULL, 0);
res = ovl_getxattr_upper(ofs, upperdentry, ox, NULL, 0);
if (res < 0) {
if (res == -ENODATA || res == -EOPNOTSUPP)
return NULL;
Expand All @@ -125,7 +126,7 @@ static struct ovl_fh *ovl_get_fh(struct ovl_fs *ofs, struct dentry *dentry,
if (!fh)
return ERR_PTR(-ENOMEM);

res = ovl_getxattr(ofs, dentry, ox, fh->buf, res);
res = ovl_getxattr_upper(ofs, upperdentry, ox, fh->buf, res);
if (res < 0)
goto fail;

Expand Down Expand Up @@ -193,9 +194,9 @@ struct dentry *ovl_decode_real_fh(struct ovl_fs *ofs, struct ovl_fh *fh,
return real;
}

static bool ovl_is_opaquedir(struct super_block *sb, struct dentry *dentry)
static bool ovl_is_opaquedir(struct ovl_fs *ofs, struct path *path)
{
return ovl_check_dir_xattr(sb, dentry, OVL_XATTR_OPAQUE);
return ovl_path_check_dir_xattr(ofs, path, OVL_XATTR_OPAQUE);
}

static struct dentry *ovl_lookup_positive_unlocked(const char *name,
Expand Down Expand Up @@ -224,6 +225,7 @@ static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d,
struct dentry **ret, bool drop_negative)
{
struct dentry *this;
struct path path;
int err;
bool last_element = !post[0];

Expand Down Expand Up @@ -253,12 +255,15 @@ static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d,
d->stop = true;
goto put_and_out;
}

path.dentry = this;
path.mnt = d->mnt;
if (!d_can_lookup(this)) {
if (d->is_dir || !last_element) {
d->stop = true;
goto put_and_out;
}
err = ovl_check_metacopy_xattr(OVL_FS(d->sb), this);
err = ovl_check_metacopy_xattr(OVL_FS(d->sb), &path);
if (err < 0)
goto out_err;

Expand All @@ -278,14 +283,14 @@ static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d,
if (d->last)
goto out;

if (ovl_is_opaquedir(d->sb, this)) {
if (ovl_is_opaquedir(OVL_FS(d->sb), &path)) {
d->stop = true;
if (last_element)
d->opaque = true;
goto out;
}
}
err = ovl_check_redirect(this, d, prelen, post);
err = ovl_check_redirect(&path, d, prelen, post);
if (err)
goto out_err;
out:
Expand Down Expand Up @@ -856,6 +861,7 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
old_cred = ovl_override_creds(dentry->d_sb);
upperdir = ovl_dentry_upper(dentry->d_parent);
if (upperdir) {
d.mnt = ovl_upper_mnt(ofs);
err = ovl_lookup_layer(upperdir, &d, &upperdentry, true);
if (err)
goto out;
Expand Down Expand Up @@ -911,6 +917,7 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
else
d.last = lower.layer->idx == roe->numlower;

d.mnt = lower.layer->mnt;
err = ovl_lookup_layer(lower.dentry, &d, &this, false);
if (err)
goto out_put;
Expand Down Expand Up @@ -1071,14 +1078,18 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
if (upperdentry)
ovl_dentry_set_upper_alias(dentry);
else if (index) {
upperdentry = dget(index);
upperredirect = ovl_get_redirect_xattr(ofs, upperdentry, 0);
struct path upperpath = {
.dentry = upperdentry = dget(index),
.mnt = ovl_upper_mnt(ofs),
};

upperredirect = ovl_get_redirect_xattr(ofs, &upperpath, 0);
if (IS_ERR(upperredirect)) {
err = PTR_ERR(upperredirect);
upperredirect = NULL;
goto out_free_oe;
}
err = ovl_check_metacopy_xattr(ofs, upperdentry);
err = ovl_check_metacopy_xattr(ofs, &upperpath);
if (err < 0)
goto out_free_oe;
uppermetacopy = err;
Expand Down
77 changes: 55 additions & 22 deletions fs/overlayfs/overlayfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,30 +218,48 @@ static inline int ovl_do_symlink(struct ovl_fs *ofs,
return err;
}

static inline ssize_t ovl_do_getxattr(struct ovl_fs *ofs, struct dentry *dentry,
const char *name, void *value,
size_t size)
static inline ssize_t ovl_do_getxattr(struct path *path, const char *name,
void *value, size_t size)
{
int err = vfs_getxattr(&init_user_ns, dentry, name, value, size);
int len = (value && err > 0) ? err : 0;
int err, len;

WARN_ON(path->dentry->d_sb != path->mnt->mnt_sb);

err = vfs_getxattr(mnt_user_ns(path->mnt), path->dentry,
name, value, size);
len = (value && err > 0) ? err : 0;

pr_debug("getxattr(%pd2, \"%s\", \"%*pE\", %zu, 0) = %i\n",
dentry, name, min(len, 48), value, size, err);
path->dentry, name, min(len, 48), value, size, err);
return err;
}

static inline ssize_t ovl_getxattr(struct ovl_fs *ofs, struct dentry *dentry,
enum ovl_xattr ox, void *value,
size_t size)
static inline ssize_t ovl_getxattr_upper(struct ovl_fs *ofs,
struct dentry *upperdentry,
enum ovl_xattr ox, void *value,
size_t size)
{
struct path upperpath = {
.dentry = upperdentry,
.mnt = ovl_upper_mnt(ofs),
};

return ovl_do_getxattr(&upperpath, ovl_xattr(ofs, ox), value, size);
}

static inline ssize_t ovl_path_getxattr(struct ovl_fs *ofs,
struct path *path,
enum ovl_xattr ox, void *value,
size_t size)
{
return ovl_do_getxattr(ofs, dentry, ovl_xattr(ofs, ox), value, size);
return ovl_do_getxattr(path, ovl_xattr(ofs, ox), value, size);
}

static inline int ovl_do_setxattr(struct ovl_fs *ofs, struct dentry *dentry,
const char *name, const void *value,
size_t size, int flags)
{
int err = vfs_setxattr(&init_user_ns, dentry, name, value, size, flags);
int err = vfs_setxattr(ovl_upper_mnt_userns(ofs), dentry, name, value, size, flags);

pr_debug("setxattr(%pd2, \"%s\", \"%*pE\", %zu, %d) = %i\n",
dentry, name, min((int)size, 48), value, size, flags, err);
Expand All @@ -258,7 +276,7 @@ static inline int ovl_setxattr(struct ovl_fs *ofs, struct dentry *dentry,
static inline int ovl_do_removexattr(struct ovl_fs *ofs, struct dentry *dentry,
const char *name)
{
int err = vfs_removexattr(&init_user_ns, dentry, name);
int err = vfs_removexattr(ovl_upper_mnt_userns(ofs), dentry, name);
pr_debug("removexattr(%pd2, \"%s\") = %i\n", dentry, name, err);
return err;
}
Expand Down Expand Up @@ -394,9 +412,20 @@ struct file *ovl_path_open(struct path *path, int flags);
int ovl_copy_up_start(struct dentry *dentry, int flags);
void ovl_copy_up_end(struct dentry *dentry);
bool ovl_already_copied_up(struct dentry *dentry, int flags);
bool ovl_check_origin_xattr(struct ovl_fs *ofs, struct dentry *dentry);
bool ovl_check_dir_xattr(struct super_block *sb, struct dentry *dentry,
enum ovl_xattr ox);
bool ovl_path_check_dir_xattr(struct ovl_fs *ofs, struct path *path,
enum ovl_xattr ox);
bool ovl_path_check_origin_xattr(struct ovl_fs *ofs, struct path *path);

static inline bool ovl_check_origin_xattr(struct ovl_fs *ofs,
struct dentry *upperdentry)
{
struct path upperpath = {
.dentry = upperdentry,
.mnt = ovl_upper_mnt(ofs),
};
return ovl_path_check_origin_xattr(ofs, &upperpath);
}

int ovl_check_setxattr(struct ovl_fs *ofs, struct dentry *upperdentry,
enum ovl_xattr ox, const void *value, size_t size,
int xerr);
Expand All @@ -408,10 +437,9 @@ bool ovl_need_index(struct dentry *dentry);
int ovl_nlink_start(struct dentry *dentry);
void ovl_nlink_end(struct dentry *dentry);
int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir);
int ovl_check_metacopy_xattr(struct ovl_fs *ofs, struct dentry *dentry);
int ovl_check_metacopy_xattr(struct ovl_fs *ofs, struct path *path);
bool ovl_is_metacopy_dentry(struct dentry *dentry);
char *ovl_get_redirect_xattr(struct ovl_fs *ofs, struct dentry *dentry,
int padding);
char *ovl_get_redirect_xattr(struct ovl_fs *ofs, struct path *path, int padding);
int ovl_sync_status(struct ovl_fs *ofs);

static inline void ovl_set_flag(unsigned long flag, struct inode *inode)
Expand All @@ -430,9 +458,15 @@ static inline bool ovl_test_flag(unsigned long flag, struct inode *inode)
}

static inline bool ovl_is_impuredir(struct super_block *sb,
struct dentry *dentry)
struct dentry *upperdentry)
{
return ovl_check_dir_xattr(sb, dentry, OVL_XATTR_IMPURE);
struct ovl_fs *ofs = OVL_FS(sb);
struct path upperpath = {
.dentry = upperdentry,
.mnt = ovl_upper_mnt(ofs),
};

return ovl_path_check_dir_xattr(ofs, &upperpath, OVL_XATTR_IMPURE);
}

/*
Expand Down Expand Up @@ -659,8 +693,7 @@ int ovl_fileattr_set(struct user_namespace *mnt_userns,
int ovl_copy_up(struct dentry *dentry);
int ovl_copy_up_with_data(struct dentry *dentry);
int ovl_maybe_copy_up(struct dentry *dentry, int flags);
int ovl_copy_xattr(struct super_block *sb, struct dentry *old,
struct dentry *new);
int ovl_copy_xattr(struct super_block *sb, struct path *path, struct dentry *new);
int ovl_set_attr(struct ovl_fs *ofs, struct dentry *upper, struct kstat *stat);
struct ovl_fh *ovl_encode_real_fh(struct ovl_fs *ofs, struct dentry *real,
bool is_upper);
Expand Down
Loading

0 comments on commit dad7017

Please sign in to comment.