Skip to content

Commit 9891d2f

Browse files
hallynSerge Hallyn
authored andcommitted
Clarify the rootid_owns_currentns
Split most of the rootid_owns_currentns() functionality into a more generic rootid_owns_ns() function which will be easier to write tests for. Rename the functions and variables to make clear that the ids being tested could be any uid. Signed-off-by: Serge Hallyn <serge@hallyn.com> CC: Ryan Foster <foster.ryan.r@gmail.com> CC: Christian Brauner <brauner@kernel.org> --- v2: change the function parameter documentation to mollify the bot.
1 parent 211ddde commit 9891d2f

1 file changed

Lines changed: 22 additions & 12 deletions

File tree

security/commoncap.c

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -358,17 +358,17 @@ int cap_inode_killpriv(struct mnt_idmap *idmap, struct dentry *dentry)
358358
return error;
359359
}
360360

361-
static bool rootid_owns_currentns(vfsuid_t rootvfsuid)
361+
/**
362+
* kuid_root_in_ns - check whether the given kuid is root in the given ns
363+
* @kuid: the kuid to be tested
364+
* @ns: the user namespace to test against
365+
*
366+
* Returns true if @kuid represents the root user in @ns, false otherwise.
367+
*/
368+
static bool kuid_root_in_ns(kuid_t kuid, struct user_namespace *ns)
362369
{
363-
struct user_namespace *ns;
364-
kuid_t kroot;
365-
366-
if (!vfsuid_valid(rootvfsuid))
367-
return false;
368-
369-
kroot = vfsuid_into_kuid(rootvfsuid);
370-
for (ns = current_user_ns();; ns = ns->parent) {
371-
if (from_kuid(ns, kroot) == 0)
370+
for (;; ns = ns->parent) {
371+
if (from_kuid(ns, kuid) == 0)
372372
return true;
373373
if (ns == &init_user_ns)
374374
break;
@@ -377,6 +377,16 @@ static bool rootid_owns_currentns(vfsuid_t rootvfsuid)
377377
return false;
378378
}
379379

380+
static bool vfsuid_root_in_currentns(vfsuid_t vfsuid)
381+
{
382+
kuid_t kuid;
383+
384+
if (!vfsuid_valid(vfsuid))
385+
return false;
386+
kuid = vfsuid_into_kuid(vfsuid);
387+
return kuid_root_in_ns(kuid, current_user_ns());
388+
}
389+
380390
static __u32 sansflags(__u32 m)
381391
{
382392
return m & ~VFS_CAP_FLAGS_EFFECTIVE;
@@ -481,7 +491,7 @@ int cap_inode_getsecurity(struct mnt_idmap *idmap,
481491
goto out_free;
482492
}
483493

484-
if (!rootid_owns_currentns(vfsroot)) {
494+
if (!vfsuid_root_in_currentns(vfsroot)) {
485495
size = -EOVERFLOW;
486496
goto out_free;
487497
}
@@ -722,7 +732,7 @@ int get_vfs_caps_from_disk(struct mnt_idmap *idmap,
722732
/* Limit the caps to the mounter of the filesystem
723733
* or the more limited uid specified in the xattr.
724734
*/
725-
if (!rootid_owns_currentns(rootvfsuid))
735+
if (!vfsuid_root_in_currentns(rootvfsuid))
726736
return -ENODATA;
727737

728738
cpu_caps->permitted.val = le32_to_cpu(caps->data[0].permitted);

0 commit comments

Comments
 (0)