Skip to content

Commit

Permalink
vfs: move cap_convert_nscap() call into vfs_setxattr()
Browse files Browse the repository at this point in the history
BugLink: https://bugs.launchpad.net/bugs/1923447 (overlayfs calls vfs_setxattr without cap_convert_nscap)

cap_convert_nscap() does permission checking as well as conversion of the
xattr value conditionally based on fs's user-ns.

This is needed by overlayfs and probably other layered fs (ecryptfs) and is
what vfs_foo() is supposed to do anyway.

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
Acked-by: James Morris <jamorris@linux.microsoft.com>
(backported from commit 7c03e2c)
[cascardo: context adjustment on vfs_setxattr]
Signed-off-by: Thadeu Lima de Souza Cascardo <cascardo@canonical.com>
Acked-by: Stefan Bader <stefan.bader@canonical.com>
Acked-by: Marcelo Henrique Cerri <marcelo.cerri@canonical.com>
  • Loading branch information
Miklos Szeredi authored and Thadeu Lima de Souza Cascardo committed Apr 14, 2021
1 parent aa51696 commit b81c426
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
17 changes: 11 additions & 6 deletions fs/xattr.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,16 @@ vfs_setxattr(struct dentry *dentry, const char *name, const void *value,
size_t size, int flags)
{
struct inode *inode = dentry->d_inode;
const void *orig_value = value;
int error;

if (size && strcmp(name, XATTR_NAME_CAPS) == 0) {
error = cap_convert_nscap(dentry, &value, size);
if (error < 0)
return error;
size = error;
}

error = xattr_permission(inode, name, MAY_WRITE);
if (error)
return error;
Expand All @@ -226,6 +234,9 @@ vfs_setxattr(struct dentry *dentry, const char *name, const void *value,

out:
inode_unlock(inode);
if (value != orig_value)
kfree(value);

return error;
}
EXPORT_SYMBOL_GPL(vfs_setxattr);
Expand Down Expand Up @@ -464,12 +475,6 @@ setxattr(struct dentry *d, const char __user *name, const void __user *value,
if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
(strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
posix_acl_fix_xattr_from_user(kvalue, size);
else if (strcmp(kname, XATTR_NAME_CAPS) == 0) {
error = cap_convert_nscap(d, &kvalue, size);
if (error < 0)
goto out;
size = error;
}
}

error = vfs_setxattr(d, kname, kvalue, size, flags);
Expand Down
2 changes: 1 addition & 1 deletion include/linux/capability.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,6 @@ extern bool ptracer_capable(struct task_struct *tsk, struct user_namespace *ns);
/* audit system wants to get cap info from files as well */
extern int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps);

extern int cap_convert_nscap(struct dentry *dentry, void **ivalue, size_t size);
extern int cap_convert_nscap(struct dentry *dentry, const void **ivalue, size_t size);

#endif /* !_LINUX_CAPABILITY_H */
3 changes: 1 addition & 2 deletions security/commoncap.c
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ static bool validheader(size_t size, const struct vfs_cap_data *cap)
*
* If all is ok, we return the new size, on error return < 0.
*/
int cap_convert_nscap(struct dentry *dentry, void **ivalue, size_t size)
int cap_convert_nscap(struct dentry *dentry, const void **ivalue, size_t size)
{
struct vfs_ns_cap_data *nscap;
uid_t nsrootid;
Expand Down Expand Up @@ -516,7 +516,6 @@ int cap_convert_nscap(struct dentry *dentry, void **ivalue, size_t size)
nscap->magic_etc = cpu_to_le32(nsmagic);
memcpy(&nscap->data, &cap->data, sizeof(__le32) * 2 * VFS_CAP_U32);

kvfree(*ivalue);
*ivalue = nscap;
return newsize;
}
Expand Down

0 comments on commit b81c426

Please sign in to comment.