public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: ebiederm@xmission.com (Eric W. Biederman)
To: Kirill Tkhai <ktkhai@virtuozzo.com>
Cc: Linux Containers <containers@lists.linux-foundation.org>,
	<serge@hallyn.com>, <agruenba@redhat.com>,
	<gregkh@linuxfoundation.org>, <linux-kernel@vger.kernel.org>,
	<oleg@redhat.com>, <paul@paul-moore.com>,
	<viro@zeniv.linux.org.uk>, <avagin@openvz.org>,
	<linux-api@vger.kernel.org>, <linux-fsdevel@vger.kernel.org>,
	<mtk.manpages@gmail.com>, <akpm@linux-foundation.org>,
	<luto@amacapital.net>, <gorcunov@openvz.org>, <mingo@kernel.org>,
	<keescook@chromium.org>
Subject: Re: [PATCH] userns,pidns: Verify the userns for new pid namespaces
Date: Tue, 02 May 2017 15:39:50 -0500	[thread overview]
Message-ID: <87y3ufgfhl.fsf@xmission.com> (raw)
In-Reply-To: <fab323b8-a909-bbce-77d2-afbcd3b0452e@virtuozzo.com> (Kirill Tkhai's message of "Tue, 2 May 2017 13:04:37 +0300")

Kirill Tkhai <ktkhai@virtuozzo.com> writes:
>>> diff --git a/kernel/user_namespace.c b/kernel/user_namespace.c
>>> index 2f735cbe05e8..7d8658fbabc8 100644
>>> --- a/kernel/user_namespace.c
>>> +++ b/kernel/user_namespace.c
>>> @@ -986,19 +986,25 @@ bool userns_may_setgroups(const struct user_namespace *ns)
>>>  }
>>>  
>>>  /*
>>> - * Returns true if @ns is the same namespace as or a descendant of
>>> - * @target_ns.
>>> + * Returns true if @child is the same namespace or a descendant of
>>> + * @ancestor.
>>>   */
>>> -bool current_in_userns(const struct user_namespace *target_ns)
>>> +bool in_userns(const struct user_namespace *ancestor,
>>> +	       const struct user_namespace *child)
>>>  {
>>> -	struct user_namespace *ns;
>>> -	for (ns = current_user_ns(); ns; ns = ns->parent) {
>>> -		if (ns == target_ns)
>>> +	const struct user_namespace *ns;
>>> +	for (ns = child; ns; ns = ns->parent) {
>>> +		if (ns == ancestor)
>>>  			return true;
>>>  	}
>>>  	return false;
>>>  }
>> 
>> We have user_namespace::level, so it's possible to stop iterations earlier
>> and save some cpu cycles:
>> 
>> 	for (ns = child; ns->level >= ancestor->level; ns = ns->parent)
>
> Just ">" here.
>
>> 		;
>> 	return (ns == ancestor);

Good observation.  Thank you.

Eric

  reply	other threads:[~2017-05-02 20:46 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-04-27 12:32 [PATCH v3] pid_ns: Introduce ioctl to set vector of ns_last_pid's on ns hierarhy Kirill Tkhai
2017-04-27 15:15 ` Eric W. Biederman
2017-04-27 15:52   ` Kirill Tkhai
2017-04-27 16:07     ` Eric W. Biederman
2017-04-28  9:13       ` Kirill Tkhai
2017-04-29 19:12         ` Eric W. Biederman
2017-04-29 19:25           ` [PATCH] userns,pidns: Verify the userns for new pid namespaces Eric W. Biederman
2017-04-29 20:53             ` Serge E. Hallyn
2017-04-30  4:33               ` Eric W. Biederman
2017-04-30  4:42                 ` Eric W. Biederman
2017-05-02 10:03             ` Kirill Tkhai
2017-05-02 10:04               ` Kirill Tkhai
2017-05-02 20:39                 ` Eric W. Biederman [this message]
2017-05-02  9:53           ` [PATCH v3] pid_ns: Introduce ioctl to set vector of ns_last_pid's on ns hierarhy Kirill Tkhai
2017-05-02 21:26             ` Eric W. Biederman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87y3ufgfhl.fsf@xmission.com \
    --to=ebiederm@xmission.com \
    --cc=agruenba@redhat.com \
    --cc=akpm@linux-foundation.org \
    --cc=avagin@openvz.org \
    --cc=containers@lists.linux-foundation.org \
    --cc=gorcunov@openvz.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=keescook@chromium.org \
    --cc=ktkhai@virtuozzo.com \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@amacapital.net \
    --cc=mingo@kernel.org \
    --cc=mtk.manpages@gmail.com \
    --cc=oleg@redhat.com \
    --cc=paul@paul-moore.com \
    --cc=serge@hallyn.com \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox