From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751362AbdEBUqR (ORCPT ); Tue, 2 May 2017 16:46:17 -0400 Received: from out01.mta.xmission.com ([166.70.13.231]:40805 "EHLO out01.mta.xmission.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750710AbdEBUqP (ORCPT ); Tue, 2 May 2017 16:46:15 -0400 From: ebiederm@xmission.com (Eric W. Biederman) To: Kirill Tkhai Cc: Linux Containers , , , , , , , , , , , , , , , , References: <149329634856.21195.14196911999722279118.stgit@localhost.localdomain> <87mvb16fv7.fsf@xmission.com> <12a73543-79ea-4bac-7e96-6ab237534af2@virtuozzo.com> <877f254yx0.fsf@xmission.com> <8737crt4dz.fsf@xmission.com> <87vapnrp7f.fsf_-_@xmission.com> Date: Tue, 02 May 2017 15:39:50 -0500 In-Reply-To: (Kirill Tkhai's message of "Tue, 2 May 2017 13:04:37 +0300") Message-ID: <87y3ufgfhl.fsf@xmission.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-XM-SPF: eid=1d5egO-00046M-Hk;;;mid=<87y3ufgfhl.fsf@xmission.com>;;;hst=in02.mta.xmission.com;;;ip=67.3.233.227;;;frm=ebiederm@xmission.com;;;spf=neutral X-XM-AID: U2FsdGVkX19xL7OvVYDRndCg+KykojMa7Q9nH8Rtr64= X-SA-Exim-Connect-IP: 67.3.233.227 X-SA-Exim-Mail-From: ebiederm@xmission.com X-Spam-Report: * -1.0 ALL_TRUSTED Passed through trusted hosts only via SMTP * 0.7 XMSubLong Long Subject * 0.0 TVD_RCVD_IP Message was received from an IP address * 0.0 T_TM2_M_HEADER_IN_MSG BODY: No description available. * 0.8 BAYES_50 BODY: Bayes spam probability is 40 to 60% * [score: 0.5000] * -0.0 DCC_CHECK_NEGATIVE Not listed in DCC * [sa07 1397; Body=1 Fuz1=1 Fuz2=1] X-Spam-DCC: XMission; sa07 1397; Body=1 Fuz1=1 Fuz2=1 X-Spam-Combo: ;Kirill Tkhai X-Spam-Relay-Country: X-Spam-Timing: total 5299 ms - load_scoreonly_sql: 0.05 (0.0%), signal_user_changed: 3.0 (0.1%), b_tie_ro: 2.1 (0.0%), parse: 0.78 (0.0%), extract_message_metadata: 11 (0.2%), get_uri_detail_list: 0.90 (0.0%), tests_pri_-1000: 5.0 (0.1%), tests_pri_-950: 1.16 (0.0%), tests_pri_-900: 1.00 (0.0%), tests_pri_-400: 19 (0.4%), check_bayes: 18 (0.3%), b_tokenize: 6 (0.1%), b_tok_get_all: 5 (0.1%), b_comp_prob: 1.54 (0.0%), b_tok_touch_all: 3.2 (0.1%), b_finish: 0.59 (0.0%), tests_pri_0: 227 (4.3%), check_dkim_signature: 0.52 (0.0%), check_dkim_adsp: 3.0 (0.1%), tests_pri_500: 5028 (94.9%), poll_dns_idle: 5022 (94.8%), rewrite_mail: 0.00 (0.0%) Subject: Re: [PATCH] userns,pidns: Verify the userns for new pid namespaces X-Spam-Flag: No X-SA-Exim-Version: 4.2.1 (built Thu, 05 May 2016 13:38:54 -0600) X-SA-Exim-Scanned: Yes (on in02.mta.xmission.com) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Kirill Tkhai 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