Skip to content

Commit

Permalink
capabilities: Don't allow writing ambiguous v3 file capabilities
Browse files Browse the repository at this point in the history
[ Upstream commit 95ebabd ]

The v3 file capabilities have a uid field that records the filesystem
uid of the root user of the user namespace the file capabilities are
valid in.

When someone is silly enough to have the same underlying uid as the
root uid of multiple nested containers a v3 filesystem capability can
be ambiguous.

In the spirit of don't do that then, forbid writing a v3 filesystem
capability if it is ambiguous.

Fixes: 8db6c34 ("Introduce v3 namespaced file capabilities")
Reviewed-by: Andrew G. Morgan <[email protected]>
Reviewed-by: Serge Hallyn <[email protected]>
Signed-off-by: Eric W. Biederman <[email protected]>
Signed-off-by: Sasha Levin <[email protected]>
  • Loading branch information
ebiederm authored and gregkh committed Mar 3, 2021
1 parent 9bbd723 commit e516a30
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion security/commoncap.c
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,8 @@ int cap_convert_nscap(struct dentry *dentry, void **ivalue, size_t size)
__u32 magic, nsmagic;
struct inode *inode = d_backing_inode(dentry);
struct user_namespace *task_ns = current_user_ns(),
*fs_ns = inode->i_sb->s_user_ns;
*fs_ns = inode->i_sb->s_user_ns,
*ancestor;
kuid_t rootid;
size_t newsize;

Expand All @@ -530,6 +531,15 @@ int cap_convert_nscap(struct dentry *dentry, void **ivalue, size_t size)
if (nsrootid == -1)
return -EINVAL;

/*
* Do not allow allow adding a v3 filesystem capability xattr
* if the rootid field is ambiguous.
*/
for (ancestor = task_ns->parent; ancestor; ancestor = ancestor->parent) {
if (from_kuid(ancestor, rootid) == 0)
return -EINVAL;
}

newsize = sizeof(struct vfs_ns_cap_data);
nscap = kmalloc(newsize, GFP_ATOMIC);
if (!nscap)
Expand Down

0 comments on commit e516a30

Please sign in to comment.