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 sence to search in parents.
So, breake the cycle earlier and skip needless iterations.
Signed-off-by: Kirill Tkhai <redacted>
---
security/commoncap.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
@@ -82,8 +82,11 @@ int cap_capable(const struct cred *cred, struct user_namespace *targ_ns,if(ns==cred->user_ns)returncap_raised(cred->cap_effective,cap)?0:-EPERM;-/* Have we tried all of the parent namespaces? */-if(ns==&init_user_ns)+/*+*Ifnscan'tbeadescendantofcred->user_ns,thenit's+*needlesslytogoup.+*/+if(ns->level<=cred->user_ns->level)return-EPERM;/*
From: Andy Lutomirski <luto@kernel.org> Date: 2017-05-02 16:37:07
On Tue, May 2, 2017 at 5:40 AM, Kirill Tkhai [off-list ref] wrote:
quoted hunk
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 sence to search in parents.
So, breake the cycle earlier and skip needless iterations.
Signed-off-by: Kirill Tkhai <redacted>
---
security/commoncap.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
@@ -82,8 +82,11 @@ int cap_capable(const struct cred *cred, struct user_namespace *targ_ns,if(ns==cred->user_ns)returncap_raised(cred->cap_effective,cap)?0:-EPERM;-/* Have we tried all of the parent namespaces? */-if(ns==&init_user_ns)+/*+*Ifnscan'tbeadescendantofcred->user_ns,thenit's+*needlesslytogoup.+*/+if(ns->level<=cred->user_ns->level)return-EPERM;
This is a nice improvement, but the comment could be better. How
about "If we're already at a lower level than we're looking for, we're
done searching."
--Andy