From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751267AbdEBQhJ (ORCPT ); Tue, 2 May 2017 12:37:09 -0400 Received: from mail.kernel.org ([198.145.29.136]:43316 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750735AbdEBQhG (ORCPT ); Tue, 2 May 2017 12:37:06 -0400 MIME-Version: 1.0 In-Reply-To: <149372879732.11927.12565684278286604561.stgit@localhost.localdomain> References: <149372879732.11927.12565684278286604561.stgit@localhost.localdomain> From: Andy Lutomirski Date: Tue, 2 May 2017 09:36:40 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] security: Use user_namespace::level to avoid redundant iterations in cap_capable() To: Kirill Tkhai Cc: "Serge E. Hallyn" , "Eric W. Biederman" , Andreas Gruenbacher , Greg KH , "linux-kernel@vger.kernel.org" , Oleg Nesterov , Paul Moore , Al Viro , Andrey Vagin , Linux API , Linux FS Devel , Michael Kerrisk , Andrew Morton , Cyrill Gorcunov , Ingo Molnar , Kees Cook Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, May 2, 2017 at 5:40 AM, Kirill Tkhai wrote: > 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 > --- > security/commoncap.c | 7 +++++-- > 1 file changed, 5 insertions(+), 2 deletions(-) > > diff --git a/security/commoncap.c b/security/commoncap.c > index 78b37838a2d3..f6ef78208d2d 100644 > --- a/security/commoncap.c > +++ b/security/commoncap.c > @@ -82,8 +82,11 @@ int cap_capable(const struct cred *cred, struct user_namespace *targ_ns, > if (ns == cred->user_ns) > return cap_raised(cred->cap_effective, cap) ? 0 : -EPERM; > > - /* Have we tried all of the parent namespaces? */ > - if (ns == &init_user_ns) > + /* > + * If ns can't be a descendant of cred->user_ns, then it's > + * needlessly to go up. > + */ > + 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