From patchwork Tue Sep 6 07:47:15 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Vagin X-Patchwork-Id: 9315857 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 154C760760 for ; Tue, 6 Sep 2016 07:48:12 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1AC6D28B2F for ; Tue, 6 Sep 2016 07:48:12 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0F42E28BC0; Tue, 6 Sep 2016 07:48:12 +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 6B17C28BCD for ; Tue, 6 Sep 2016 07:48:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755596AbcIFHrv (ORCPT ); Tue, 6 Sep 2016 03:47:51 -0400 Received: from mail-lf0-f66.google.com ([209.85.215.66]:32773 "EHLO mail-lf0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755529AbcIFHro (ORCPT ); Tue, 6 Sep 2016 03:47:44 -0400 Received: by mail-lf0-f66.google.com with SMTP id l131so4099881lfl.0; Tue, 06 Sep 2016 00:47:42 -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=uLkhvbnapxIUBqh3i4mpjWm+r1OOF7OzvNlfNP4Yscg=; b=xq6QbEYrfrDzdjbkkfCGIb7dcNILD6NMDi7JGf3ApXTVFJ0qpGKzGnFYseZu1OPbil nRSDcoslozOtiOswNpNu0ULNrVRARI1YVj3jCi75aw7X4AeiKev7Os5Ce6DM4/NBj5yi OfXUi+HqsRUum6oHLeT+nOVUDARTZrbOiFRBPbs9rYdqAv8hGn+fe5K6vyjy78EjNJoY t6IpStim19I1Dm8jnV94ohi7ytNSoy+SYCyu3HdwOXYia2D52R0Lfo6WslL1BUenWxoa G0u1ZbPDVDYxtElMXc1QB2ahy7FbctaIlMlcNBqB0uJ3B1Sq87+1avu3Td8hfTPEjoWw 52Ng== 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=uLkhvbnapxIUBqh3i4mpjWm+r1OOF7OzvNlfNP4Yscg=; b=YTB3Y0nklBq8YIecduErcLlEVlGIybW/anzse4K6Su6nEyam+w1SDiqUqxSduC7DEb zbXjQEhbgGBZFmPHCI+L8ttc4OTCpZwdjJkzagzuHVuZagSPB9Gha9JH02+M634z5m7s 7QCF69whXcSlcA8p4R0nLgediIFb/FM+4L8M0foKMaSJJEcpINad2Bf4HqozBWSjQXf5 bYHFO4gHhIXy1c9jAVEPxveg7qEG7gaSivGX5I0wkg+ccW6CkWsLwl7HgLfyv1asrHng 9Ih0CGl+1mS6UmDXNtiFD6SY7AxO/OMgefPD8vqcsmXH4YZvH6f9L94ENuFct4gHKRGf JXgw== X-Gm-Message-State: AE9vXwPlM1zZNR1tmF+gmtNrZ2MIgp0rTi79Qo+bpFQ/FW8fl57eOcTvacBA6jncMXHfQA== X-Received: by 10.46.1.170 with SMTP id f42mr1363344lji.50.1473148061857; Tue, 06 Sep 2016 00:47:41 -0700 (PDT) Received: from laptop.sw.ru (swsoft-msk-nat.sw.ru. [195.214.232.10]) by smtp.gmail.com with ESMTPSA id e73sm5352388lji.31.2016.09.06.00.47.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 06 Sep 2016 00:47:41 -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: Tue, 6 Sep 2016 00:47:15 -0700 Message-Id: <1473148036-32630-4-git-send-email-avagin@openvz.org> X-Mailer: git-send-email 2.5.5 In-Reply-To: <1473148036-32630-1-git-send-email-avagin@openvz.org> References: <1473148036-32630-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 Acked-by: Serge Hallyn --- 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 ca85a43..12cb8bd 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 *(*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 c18f0f4f..aad470b 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_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, .owner = pidns_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 42a64d5..33b523c 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, .owner = userns_owner, + .get_parent = ns_get_owner, }; static __init int user_namespaces_init(void)