From patchwork Fri Aug 26 23:08:10 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Vagin X-Patchwork-Id: 9302105 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 04BFE607D8 for ; Fri, 26 Aug 2016 23:09:08 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E94E029603 for ; Fri, 26 Aug 2016 23:09:07 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id DE1F32966A; Fri, 26 Aug 2016 23:09:07 +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 4A66D29603 for ; Fri, 26 Aug 2016 23:09:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932109AbcHZXIl (ORCPT ); Fri, 26 Aug 2016 19:08:41 -0400 Received: from mail-pa0-f68.google.com ([209.85.220.68]:36785 "EHLO mail-pa0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754533AbcHZXIj (ORCPT ); Fri, 26 Aug 2016 19:08:39 -0400 Received: by mail-pa0-f68.google.com with SMTP id ez1so5350205pab.3; Fri, 26 Aug 2016 16:08:38 -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=mblLSdRhUvN4qSF/Vx3s2BCFt4LhywqWDUrW/2q+/3M=; b=vkQfVpD0xyOvAuvNpX0Osoa/1SgHkEUN7UGVFFVCSs5565J5MrdmfURkFH8zVFo9nL LtJIFXUuHr6QiJvQupl+JGsEry4+JbTx9YM4whNfC1O6bb6L7gJepj6pqT1PfUlY1TeZ KyudcvpkgKzgWieOvI9w8MBTZpq133+rf8AxT/HLIhTNxgV4srecmSBUpGOVZn8H/7x3 lIO3w0HPJ3M9qkkolUitsTfY+2yfz/Jh0+guFryw0qYLKX2IxMnOo/aDtJFbgpMJ7PZb Kh2NEEiXx0maZw8kjiZdkaPoyYyRP856dVOJGVLyskIU8PUL8aYFb0vr7TVLWLKo+HZh WKSA== 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=mblLSdRhUvN4qSF/Vx3s2BCFt4LhywqWDUrW/2q+/3M=; b=Mkvr5DPWs675q6WmYd+LoHOM3RDM7EZOOrbwBW3UhTVHFEdy6SYszyI8BG2waruLAY ggaG2sK60VGz9Z2IaxBSFjGy3Txahx+KdmiL/9QfP6RkgutfM8VY9j2PBdxFEOdR+kYE DKG/aKwE4X+XSYdRTf6WKsGCJ++rzeW5hILelX90cI7BQjcyLKH+1oE0/ziVGSNRRcDy agePGZCdeQDbSMMPhDF8c8Cfx7AwV860mUkjVG1KA6h8yi5Q3MACJ5bo7d3AuoBWeLPk 1p5ZIBI71HxcNZAI2IE/HKFLHclKkfMfjr+cfIQ6Fs8QRCMNjbiwTiC7+5vbL2g6znI8 JMUg== X-Gm-Message-State: AE9vXwPWT25le1o/6xhOqpss0KBoKuHceEr0FPKXNsi3+T3P4SroifFwgjuyFzc4QT3SQQ== X-Received: by 10.66.182.232 with SMTP id eh8mr10292991pac.146.1472252918156; Fri, 26 Aug 2016 16:08:38 -0700 (PDT) Received: from laptop.vz.com ([162.246.95.100]) by smtp.gmail.com with ESMTPSA id ro14sm31245680pab.32.2016.08.26.16.08.37 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 26 Aug 2016 16:08:37 -0700 (PDT) From: Andrei Vagin To: "Eric W. Biederman" , containers@lists.linux-foundation.org Cc: linux-api@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, Andrey Vagin , James Bottomley , "Michael Kerrisk (man-pages)" , "W. Trevor King" , Alexander Viro , Serge Hallyn Subject: [PATCH 3/4] nsfs: add ioctl to get a parent namespace Date: Fri, 26 Aug 2016 16:08:10 -0700 Message-Id: <1472252891-4963-4-git-send-email-avagin@openvz.org> X-Mailer: git-send-email 2.5.5 In-Reply-To: <1472252891-4963-1-git-send-email-avagin@openvz.org> References: <1472252891-4963-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 From: Andrey Vagin 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: Andrei Vagin --- fs/nsfs.c | 4 ++++ include/linux/proc_ns.h | 1 + include/uapi/linux/nsfs.h | 2 ++ kernel/pid_namespace.c | 19 +++++++++++++++++++ kernel/user_namespace.c | 1 + 5 files changed, 27 insertions(+) diff --git a/fs/nsfs.c b/fs/nsfs.c index be7d193..11a4b15 100644 --- a/fs/nsfs.c +++ b/fs/nsfs.c @@ -170,6 +170,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 cfc8be7..e4a904b 100644 --- a/include/linux/proc_ns.h +++ b/include/linux/proc_ns.h @@ -19,6 +19,7 @@ struct proc_ns_operations { void (*put)(struct ns_common *ns); int (*install)(struct nsproxy *nsproxy, struct ns_common *ns); struct user_namespace *(*get_owner)(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 5cacd5c..3af6172 100644 --- a/include/uapi/linux/nsfs.h +++ b/include/uapi/linux/nsfs.h @@ -7,5 +7,7 @@ /* Returns a file descriptor that refers to an owning user namespace */ #define NS_GET_USERNS _IO(NSIO, 0x1) +/* Returns a file descriptor that refers to a parent namespace */ +#define NS_GET_PARENT _IO(NSIO, 0x2) #endif /* __LINUX_NSFS_H */ diff --git a/kernel/pid_namespace.c b/kernel/pid_namespace.c index 1860d9d..6cd473c 100644 --- a/kernel/pid_namespace.c +++ b/kernel/pid_namespace.c @@ -405,6 +405,24 @@ 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; + + /* See if the parent is in the current namespace */ + pid_ns = p = to_pid_ns(ns)->parent; + for (;;) { + if (!p) + return ERR_PTR(-EPERM); + if (p == active) + break; + p = p->parent; + } + + return &get_pid_ns(pid_ns)->ns; +} + static struct user_namespace *pidns_get_owner(struct ns_common *ns) { return to_pid_ns(ns)->user_ns; @@ -417,6 +435,7 @@ const struct proc_ns_operations pidns_operations = { .put = pidns_put, .install = pidns_install, .get_owner = pidns_get_owner, + .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 f0521e4..1516c47 100644 --- a/kernel/user_namespace.c +++ b/kernel/user_namespace.c @@ -1080,6 +1080,7 @@ const struct proc_ns_operations userns_operations = { .put = userns_put, .install = userns_install, .get_owner = userns_get_owner, + .get_parent = ns_get_owner, }; static __init int user_namespaces_init(void)