Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S376946AbdD2Tby (ORCPT ); Sat, 29 Apr 2017 15:31:54 -0400 Received: from out01.mta.xmission.com ([166.70.13.231]:49593 "EHLO out01.mta.xmission.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1036424AbdD2Tbr (ORCPT ); Sat, 29 Apr 2017 15:31:47 -0400 From: ebiederm@xmission.com (Eric W. Biederman) To: Linux Containers Cc: , , , , , , , , , , , , , , , , Kirill Tkhai 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> Date: Sat, 29 Apr 2017 14:25:24 -0500 In-Reply-To: <8737crt4dz.fsf@xmission.com> (Eric W. Biederman's message of "Sat, 29 Apr 2017 14:12:08 -0500") Message-ID: <87vapnrp7f.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=1d4Y5f-0003bw-QS;;;mid=<87vapnrp7f.fsf_-_@xmission.com>;;;hst=in02.mta.xmission.com;;;ip=67.3.233.227;;;frm=ebiederm@xmission.com;;;spf=neutral X-XM-AID: U2FsdGVkX18vZyL7CvoWM5dqsRnSZZ6/7eqBVbtnGmo= 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.8 BAYES_50 BODY: Bayes spam probability is 40 to 60% * [score: 0.5000] * -0.0 DCC_CHECK_NEGATIVE Not listed in DCC * [sa01 1397; Body=1 Fuz1=1 Fuz2=1] X-Spam-DCC: XMission; sa01 1397; Body=1 Fuz1=1 Fuz2=1 X-Spam-Combo: ;Linux Containers X-Spam-Relay-Country: X-Spam-Timing: total 5560 ms - load_scoreonly_sql: 0.05 (0.0%), signal_user_changed: 3.3 (0.1%), b_tie_ro: 2.3 (0.0%), parse: 1.55 (0.0%), extract_message_metadata: 28 (0.5%), get_uri_detail_list: 4.0 (0.1%), tests_pri_-1000: 11 (0.2%), tests_pri_-950: 2.0 (0.0%), tests_pri_-900: 1.70 (0.0%), tests_pri_-400: 38 (0.7%), check_bayes: 36 (0.6%), b_tokenize: 17 (0.3%), b_tok_get_all: 8 (0.2%), b_comp_prob: 4.1 (0.1%), b_tok_touch_all: 3.2 (0.1%), b_finish: 0.81 (0.0%), tests_pri_0: 789 (14.2%), check_dkim_signature: 0.92 (0.0%), check_dkim_adsp: 4.0 (0.1%), tests_pri_500: 4681 (84.2%), poll_dns_idle: 4673 (84.0%), rewrite_mail: 0.00 (0.0%) Subject: [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 Content-Length: 3515 Lines: 105 It is pointless and confusing to allow a pid namespace hierarchy and the user namespace hierarchy to get out of sync. The owner of a child pid namespace should be the owner of the parent pid namespace or a descendant of the owner of the parent pid namespace. Otherwise it is possible to construct scenarios where it is legal to do something in a parent pid namespace but in a child pid namespace. It requires use of setns into a pid namespace (but not into a user namespace) to create such a scenario. Add the function in_userns to help in making this determination. Signed-off-by: "Eric W. Biederman" --- While review a patch from Kiril Tkhai I realized we were missing this sanity check.... include/linux/user_namespace.h | 8 +++++++- kernel/pid_namespace.c | 4 ++++ kernel/user_namespace.c | 18 ++++++++++++------ 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/include/linux/user_namespace.h b/include/linux/user_namespace.h index 32354b4b4b2b..497ed50004db 100644 --- a/include/linux/user_namespace.h +++ b/include/linux/user_namespace.h @@ -112,8 +112,9 @@ extern ssize_t proc_projid_map_write(struct file *, const char __user *, size_t, extern ssize_t proc_setgroups_write(struct file *, const char __user *, size_t, loff_t *); extern int proc_setgroups_show(struct seq_file *m, void *v); extern bool userns_may_setgroups(const struct user_namespace *ns); +extern bool in_userns(const struct user_namespace *ancestor, + const struct user_namespace *child); extern bool current_in_userns(const struct user_namespace *target_ns); - struct ns_common *ns_get_owner(struct ns_common *ns); #else @@ -144,6 +145,11 @@ static inline bool userns_may_setgroups(const struct user_namespace *ns) return true; } +static inline bool in_userns(const struct user_namespace *target_ns) +{ + return true; +} + static inline bool current_in_userns(const struct user_namespace *target_ns) { return true; diff --git a/kernel/pid_namespace.c b/kernel/pid_namespace.c index de461aa0bf9a..749147f5a613 100644 --- a/kernel/pid_namespace.c +++ b/kernel/pid_namespace.c @@ -101,6 +101,10 @@ static struct pid_namespace *create_pid_namespace(struct user_namespace *user_ns int i; int err; + err = -EINVAL; + if (!in_userns(parent_pid_ns->user_ns, user_ns)) + goto out; + err = -ENOSPC; if (level > MAX_PID_NS_LEVEL) goto out; 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; } +bool current_in_userns(const struct user_namespace *target_ns) +{ + return in_userns(target_ns, current_user_ns()); +} + static inline struct user_namespace *to_user_ns(struct ns_common *ns) { return container_of(ns, struct user_namespace, ns); -- 2.10.1