Skip to content

Commit 64db4c7

Browse files
Kirill Tkhaiebiederm
authored andcommitted
security: Use user_namespace::level to avoid redundant iterations in cap_capable()
When ns->level is not larger then cred->user_ns->level, then ns can't be cred->user_ns's descendant, and there is no a sense to search in parents. So, break the cycle earlier and skip needless iterations. v2: Change comment on suggested by Andy Lutomirski. Signed-off-by: Kirill Tkhai <[email protected]> Signed-off-by: Eric W. Biederman <[email protected]>
1 parent a2b4262 commit 64db4c7

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

security/commoncap.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,11 @@ int cap_capable(const struct cred *cred, struct user_namespace *targ_ns,
8282
if (ns == cred->user_ns)
8383
return cap_raised(cred->cap_effective, cap) ? 0 : -EPERM;
8484

85-
/* Have we tried all of the parent namespaces? */
86-
if (ns == &init_user_ns)
85+
/*
86+
* If we're already at a lower level than we're looking for,
87+
* we're done searching.
88+
*/
89+
if (ns->level <= cred->user_ns->level)
8790
return -EPERM;
8891

8992
/*

0 commit comments

Comments
 (0)