From patchwork Thu Jul 14 18:20:18 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Vagin X-Patchwork-Id: 9230399 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id A8052608A1 for ; Thu, 14 Jul 2016 18:21:57 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9CE2C2817F for ; Thu, 14 Jul 2016 18:21:57 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8180528285; Thu, 14 Jul 2016 18:21:57 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2DFCF281B7 for ; Thu, 14 Jul 2016 18:21:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751827AbcGNSVf (ORCPT ); Thu, 14 Jul 2016 14:21:35 -0400 Received: from mail-pa0-f68.google.com ([209.85.220.68]:33476 "EHLO mail-pa0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751439AbcGNSUf (ORCPT ); Thu, 14 Jul 2016 14:20:35 -0400 Received: by mail-pa0-f68.google.com with SMTP id q2so4923784pap.0; Thu, 14 Jul 2016 11:20:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=4ECzaC+9EvwImZ8T8clcEF6fqOpx0SMJTiEXNMHjAio=; b=z4ALrHDqFBGF+TwwO9LWiBACoOBadvbhaVoWAtYDFcHTrX1f5e2rqy/2AN/zxVleFC U32GX7HawUzHBVlqwW4FjPz7WzsMOxcwwRyLAhpIOq6dLuPnJfrarbCvhdeBtcNbhqB8 Y4wg4nhbogMXOu5VFvXXnl1Ld9q0ir+lCCST8rcPKuTX3Tm7/G05GRW4Aj0KF9k8TvsM KDYFoXJ9tHz6ehpzPQP57xFo1IUll2NpnKlO4mYxNK3RMFIrX/TJBiiMD+FFcqvLBWKL SfgnbFYT7xfdQl3uc7FueUhOLdtu3ReoH1M7CYAyhfNn97ntJg1bT+VbJ7Rs+ZGsn1Dm 3thw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=4ECzaC+9EvwImZ8T8clcEF6fqOpx0SMJTiEXNMHjAio=; b=TYSxo3t+q699FtOk/gK+VlctCd4y+TIF7vOKJakP82xDziEPaX0bz0oKHsybmxy0fO w8cnhS35LjUICdzC9z0pljftget5ph25uKj4qRA4yIItHLzDDBe0RDIdlBBquKx5bhe1 t+C7Or2xdvCWinJGyNrxXPeKh/dUbOC7tgon6SPErK+2qik5AIuYU8O2ptVNIqG/lT89 WS+jXgfw9sBaiFhqchJ3X7OEsTasoB/RshIRVQWrqSGkz+8GGaZELkOb8khWukZH78Eq 94eBvFq0y7XhHcMqLUxwedzMwf003MKEefYYxPPUSF23a1zxF2rkmjvhD0ofL6l6d8en bBhw== X-Gm-Message-State: ALyK8tIYu1+iOPQ3fAB7qqHIAAmLegIZipDE6Fv/Z1wtWvvODk3ObVX5JoWI3Rq/Og1KZQ== X-Received: by 10.66.242.201 with SMTP id ws9mr24856204pac.7.1468520434477; Thu, 14 Jul 2016 11:20:34 -0700 (PDT) Received: from laptop.vz.com ([162.246.95.100]) by smtp.gmail.com with ESMTPSA id 89sm3515657pft.96.2016.07.14.11.20.33 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 14 Jul 2016 11:20:33 -0700 (PDT) From: Andrey Vagin To: linux-kernel@vger.kernel.org Cc: linux-api@vger.kernel.org, containers@lists.linux-foundation.org, criu@openvz.org, linux-fsdevel@vger.kernel.org, Andrey Vagin Subject: [PATCH 4/5] nsfs: add ioctl to get a parent namespace Date: Thu, 14 Jul 2016 11:20:18 -0700 Message-Id: <1468520419-28220-5-git-send-email-avagin@openvz.org> X-Mailer: git-send-email 2.5.5 In-Reply-To: <1468520419-28220-1-git-send-email-avagin@openvz.org> References: <1468520419-28220-1-git-send-email-avagin@openvz.org> Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Pid and user namepaces are hierarchical. There is no way to discover parent-child relationships. In a future we will use this interface to dump and restore nested namespaces. Signed-off-by: Andrey Vagin --- fs/nsfs.c | 4 ++++ include/linux/proc_ns.h | 1 + include/uapi/linux/nsfs.h | 1 + kernel/pid_namespace.c | 26 ++++++++++++++++++++++++++ kernel/user_namespace.c | 1 + 5 files changed, 33 insertions(+) diff --git a/fs/nsfs.c b/fs/nsfs.c index 1e5d2d0..b607a42 100644 --- a/fs/nsfs.c +++ b/fs/nsfs.c @@ -169,6 +169,10 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl, switch (ioctl) { case NS_GET_USERNS: return open_related_ns(ns, ns_get_owner); + case NS_GET_PARENT: + if (!ns->ops->get_parent) + return -EINVAL; + return open_related_ns(ns, ns->ops->get_parent); default: return -ENOTTY; } diff --git a/include/linux/proc_ns.h b/include/linux/proc_ns.h index de0e771..1c9f720 100644 --- a/include/linux/proc_ns.h +++ b/include/linux/proc_ns.h @@ -18,6 +18,7 @@ struct proc_ns_operations { struct ns_common *(*get)(struct task_struct *task); void (*put)(struct ns_common *ns); int (*install)(struct nsproxy *nsproxy, struct ns_common *ns); + struct ns_common *(*get_parent)(struct ns_common *ns); }; extern const struct proc_ns_operations netns_operations; diff --git a/include/uapi/linux/nsfs.h b/include/uapi/linux/nsfs.h index 7a09ede..88098ea 100644 --- a/include/uapi/linux/nsfs.h +++ b/include/uapi/linux/nsfs.h @@ -5,5 +5,6 @@ #define NSIO 0xb7 #define NS_GET_USERNS _IO(NSIO, 0x1) +#define NS_GET_PARENT _IO(NSIO, 0x2) #endif /* __LINUX_NSFS_H */ diff --git a/kernel/pid_namespace.c b/kernel/pid_namespace.c index 3529a03..a63adfb 100644 --- a/kernel/pid_namespace.c +++ b/kernel/pid_namespace.c @@ -388,12 +388,38 @@ static int pidns_install(struct nsproxy *nsproxy, struct ns_common *ns) return 0; } +static struct ns_common *pidns_get_parent(struct ns_common *ns) +{ + struct pid_namespace *active = task_active_pid_ns(current); + struct pid_namespace *pid_ns, *p; + + pid_ns = to_pid_ns(ns); + if (pid_ns == &init_pid_ns) { + if (capable(CAP_SYS_ADMIN)) + return ERR_PTR(-ENOENT); + return ERR_PTR(-EPERM); + } + + pid_ns = p = pid_ns->parent; + + for (;;) { + if (p == active) + break; + if (p == &init_pid_ns) + return ERR_PTR(-EPERM); + p = p->parent; + } + + return &get_pid_ns(pid_ns)->ns; +} + const struct proc_ns_operations pidns_operations = { .name = "pid", .type = CLONE_NEWPID, .get = pidns_get, .put = pidns_put, .install = pidns_install, + .get_parent = pidns_get_parent, }; static __init int pid_namespaces_init(void) diff --git a/kernel/user_namespace.c b/kernel/user_namespace.c index 6382e5e..d6ba0b8 100644 --- a/kernel/user_namespace.c +++ b/kernel/user_namespace.c @@ -1024,6 +1024,7 @@ const struct proc_ns_operations userns_operations = { .get = userns_get, .put = userns_put, .install = userns_install, + .get_parent = ns_get_owner, }; static __init int user_namespaces_init(void)