All of lore.kernel.org
 help / color / mirror / Atom feed
From: menage@google.com
To: akpm@osdl.org, pj@sgi.com, sekharan@us.ibm.com, dev@sw.ru,
	xemul@sw.ru, serue@us.ibm.com, vatsa@in.ibm.com,
	ebiederm@xmission.com
Cc: ckrm-tech@lists.sourceforge.net, linux-kernel@vger.kernel.org,
	rohitseth@google.com, mbligh@google.com, winget@google.com,
	containers@lists.osdl.org, devel@openvz.org
Subject: [PATCH 7/7] containers (V7): Container interface to nsproxy subsystem
Date: Mon, 12 Feb 2007 00:15:28 -0800	[thread overview]
Message-ID: <20070212085105.170265000@menage.corp.google.com> (raw)
In-Reply-To: 20070212081521.808338000@menage.corp.google.com

[-- Attachment #1: nsproxy_container.patch --]
[-- Type: text/plain, Size: 6085 bytes --]

When a task enters a new namespace via a clone() or unshare(), a new
container is created and the task moves into it. Developed by Serge
Hallyn <serue@us.ibm.com>, adapted by Paul Menage <menage@google.com>

---
 include/linux/nsproxy.h |    6 ++
 init/Kconfig            |    9 +++
 kernel/Makefile         |    1 
 kernel/fork.c           |    4 +
 kernel/ns_container.c   |  110 ++++++++++++++++++++++++++++++++++++++++++++++++
 kernel/nsproxy.c        |    6 ++
 6 files changed, 136 insertions(+)

Index: container-2.6.20/include/linux/nsproxy.h
===================================================================
--- container-2.6.20.orig/include/linux/nsproxy.h
+++ container-2.6.20/include/linux/nsproxy.h
@@ -53,4 +53,10 @@ static inline void exit_task_namespaces(
 		put_nsproxy(ns);
 	}
 }
+#ifdef CONFIG_CONTAINER_NS
+int ns_container_clone(struct task_struct *tsk);
+#else
+static inline int ns_container_clone(struct task_struct *tsk) { return 0; }
+#endif
+
 #endif
Index: container-2.6.20/init/Kconfig
===================================================================
--- container-2.6.20.orig/init/Kconfig
+++ container-2.6.20/init/Kconfig
@@ -297,6 +297,15 @@ config CONTAINER_CPUACCT
 	  Provides a simple Resource Controller for monitoring the
 	  total CPU consumed by the tasks in a container
 
+config CONTAINER_NS
+        bool "Namespace container subsystem"
+        select CONTAINERS
+        help
+          Provides a simple namespace container subsystem to
+          provide hierarchical naming of sets of namespaces,
+          for instance virtual servers and checkpoint/restart
+          jobs.
+
 config RELAY
 	bool "Kernel->user space relay support (formerly relayfs)"
 	help
Index: container-2.6.20/kernel/Makefile
===================================================================
--- container-2.6.20.orig/kernel/Makefile
+++ container-2.6.20/kernel/Makefile
@@ -39,6 +39,7 @@ obj-$(CONFIG_COMPAT) += compat.o
 obj-$(CONFIG_CONTAINERS) += container.o
 obj-$(CONFIG_CPUSETS) += cpuset.o
 obj-$(CONFIG_CONTAINER_CPUACCT) += cpu_acct.o
+obj-$(CONFIG_CONTAINER_NS) += ns_container.o
 obj-$(CONFIG_IKCONFIG) += configs.o
 obj-$(CONFIG_STOP_MACHINE) += stop_machine.o
 obj-$(CONFIG_AUDIT) += audit.o auditfilter.o
Index: container-2.6.20/kernel/fork.c
===================================================================
--- container-2.6.20.orig/kernel/fork.c
+++ container-2.6.20/kernel/fork.c
@@ -1661,6 +1661,9 @@ asmlinkage long sys_unshare(unsigned lon
 			err = -ENOMEM;
 			goto bad_unshare_cleanup_ipc;
 		}
+		err = ns_container_clone(current);
+		if (err)
+			goto bad_unshare_cleanup_dupns;
 	}
 
 	if (new_fs || new_ns || new_mm || new_fd || new_ulist ||
@@ -1715,6 +1718,7 @@ asmlinkage long sys_unshare(unsigned lon
 		task_unlock(current);
 	}
 
+ bad_unshare_cleanup_dupns:
 	if (new_nsproxy)
 		put_nsproxy(new_nsproxy);
 
Index: container-2.6.20/kernel/ns_container.c
===================================================================
--- /dev/null
+++ container-2.6.20/kernel/ns_container.c
@@ -0,0 +1,110 @@
+/*
+ * ns_container.c - namespace container subsystem
+ *
+ * Copyright IBM, 2006
+ */
+
+#include <linux/module.h>
+#include <linux/container.h>
+#include <linux/fs.h>
+
+struct nscont {
+	struct container_subsys_state css;
+	spinlock_t lock;
+};
+
+static struct container_subsys ns_subsys;
+
+static inline struct nscont *container_nscont(struct container *cont)
+{
+	return container_of(container_subsys_state(cont, &ns_subsys),
+			    struct nscont, css);
+}
+
+int ns_container_clone(struct task_struct *tsk)
+{
+	return container_clone(tsk, &ns_subsys);
+}
+
+/*
+ * Rules:
+ *   1. you can only enter a container which is a child of your current
+ *     container
+ *   2. you can only place another process into a container if
+ *     a. you have CAP_SYS_ADMIN
+ *     b. your container is an ancestor of tsk's destination container
+ *       (hence either you are in the same container as tsk, or in an
+ *        ancestor container thereof)
+ */
+int ns_can_attach(struct container_subsys *ss,
+		  struct container *cont, struct task_struct *tsk)
+{
+	struct container *c;
+
+	if (current != tsk) {
+		if (!capable(CAP_SYS_ADMIN))
+			return -EPERM;
+
+		if (!container_is_descendant(cont))
+			return -EPERM;
+	}
+
+	if (atomic_read(&cont->count) != 0)
+		return -EPERM;
+
+	c = task_container(tsk, &ns_subsys);
+	if (c && c != cont->parent)
+		return -EPERM;
+
+	return 0;
+}
+
+/*
+ * Rules: you can only create a container if
+ *     1. you are capable(CAP_SYS_ADMIN)
+ *     2. the target container is a descendant of your own container
+ */
+static int ns_create(struct container_subsys *ss, struct container *cont)
+{
+	struct nscont *ns;
+
+	if (!capable(CAP_SYS_ADMIN))
+		return -EPERM;
+	if (!container_is_descendant(cont))
+		return -EPERM;
+
+	ns = kzalloc(sizeof(*ns), GFP_KERNEL);
+	if (!ns) return -ENOMEM;
+	spin_lock_init(&ns->lock);
+	cont->subsys[ns_subsys.subsys_id] = &ns->css;
+	return 0;
+}
+
+static void ns_destroy(struct container_subsys *ss,
+		       struct container *cont)
+{
+	struct nscont *ns = container_nscont(cont);
+	kfree(ns);
+}
+
+static struct container_subsys ns_subsys = {
+	.name = "ns",
+	.create = ns_create,
+	.destroy  = ns_destroy,
+	.can_attach = ns_can_attach,
+	//.attach = ns_attach,
+	//.post_attach = ns_post_attach,
+	//.populate = ns_populate,
+	.subsys_id = -1,
+};
+
+int __init ns_init(void)
+{
+	int ret;
+
+	ret = container_register_subsys(&ns_subsys);
+
+	return ret < 0 ? ret : 0;
+}
+
+module_init(ns_init)
Index: container-2.6.20/kernel/nsproxy.c
===================================================================
--- container-2.6.20.orig/kernel/nsproxy.c
+++ container-2.6.20/kernel/nsproxy.c
@@ -116,10 +116,16 @@ int copy_namespaces(int flags, struct ta
 	if (err)
 		goto out_pid;
 
+	err = ns_container_clone(tsk);
+	if (err)
+		goto out_container;
 out:
 	put_nsproxy(old_ns);
 	return err;
 
+ out_container:
+	if (new_ns->pid_ns)
+		put_pid_ns(new_ns->pid_ns);
 out_pid:
 	if (new_ns->ipc_ns)
 		put_ipc_ns(new_ns->ipc_ns);

--

  parent reply	other threads:[~2007-02-12  8:53 UTC|newest]

Thread overview: 132+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-02-12  8:15 [PATCH 0/7] containers (V7): Generic Process Containers menage
2007-02-12  8:15 ` [PATCH 1/7] containers (V7): Generic container system abstracted from cpusets code menage
2007-02-12 12:33   ` Srivatsa Vaddagiri
2007-02-12 19:26     ` Paul Menage
2007-02-12 19:46       ` Paul Menage
2007-02-13  5:48         ` Srivatsa Vaddagiri
2007-02-13  8:16           ` [ckrm-tech] " Srivatsa Vaddagiri
2007-03-07 12:21   ` Srivatsa Vaddagiri
2007-03-07 14:06     ` [ckrm-tech] " Srivatsa Vaddagiri
2007-03-07 20:50     ` Paul Menage
2007-03-08 10:38       ` [ckrm-tech] " Srivatsa Vaddagiri
2007-03-08 10:40         ` Paul Menage
2007-03-11 19:38         ` Paul Jackson
2007-03-12 14:19           ` Srivatsa Vaddagiri
2007-03-22  9:56   ` Srivatsa Vaddagiri
2007-03-22 10:20     ` Srivatsa Vaddagiri
2007-03-24 15:05   ` Srivatsa Vaddagiri
2007-03-24 19:25     ` Paul Jackson
2007-03-25  0:45       ` Srivatsa Vaddagiri
2007-03-25  1:41         ` Paul Jackson
2007-03-25  2:28           ` Srivatsa Vaddagiri
2007-03-25  4:16             ` Srivatsa Vaddagiri
2007-03-25  5:43               ` Paul Jackson
2007-03-25  8:21                 ` Srivatsa Vaddagiri
2007-03-25  4:45             ` Paul Jackson
2007-03-25  5:05               ` Srivatsa Vaddagiri
2007-03-25  4:59                 ` Paul Jackson
2007-02-12  8:15 ` [PATCH 2/7] containers (V7): Cpusets hooked into containers menage
2007-02-15 20:35   ` Serge E. Hallyn
2007-02-15 20:49     ` Paul Menage
2007-03-07 14:34   ` Srivatsa Vaddagiri
2007-03-07 16:01     ` Paul Menage
2007-03-07 16:31       ` [ckrm-tech] " Srivatsa Vaddagiri
2007-03-07 16:31         ` Paul Menage
2007-03-07 14:52   ` Srivatsa Vaddagiri
2007-03-07 16:12     ` Paul Menage
2007-02-12  8:15 ` [PATCH 4/7] containers (V7): Simple CPU accounting container subsystem menage
2007-02-12  8:15 ` [PATCH 5/7] containers (V7): Resource Groups over generic containers menage
2007-02-12  8:15 ` [PATCH 6/7] containers (V7): BeanCounters over generic process containers menage
2007-02-12 15:34   ` Srivatsa Vaddagiri
2007-02-12 18:49     ` Paul Menage
2007-02-13  8:52   ` Pavel Emelianov
2007-02-13  9:03     ` Paul Menage
2007-02-13  9:18       ` Pavel Emelianov
2007-02-13  9:37         ` Paul Menage
2007-02-13  9:49           ` Pavel Emelianov
2007-02-12  8:15 ` menage [this message]
2007-03-24  5:05   ` [ckrm-tech] [PATCH 7/7] containers (V7): Container interface to nsproxy subsystem Srivatsa Vaddagiri
2007-03-24 16:23     ` Srivatsa Vaddagiri
2007-03-26 21:57       ` Serge E. Hallyn
2007-03-28 14:55         ` Srivatsa Vaddagiri
2007-03-28 15:26           ` Serge E. Hallyn
2007-03-26 21:55     ` Serge E. Hallyn
2007-03-31  2:47   ` Srivatsa Vaddagiri
2007-04-02 14:09     ` Serge E. Hallyn
2007-04-02 14:27       ` Srivatsa Vaddagiri
2007-04-02 18:02         ` Eric W. Biederman
2007-04-03 14:16           ` Srivatsa Vaddagiri
2007-04-03 15:32           ` Serge E. Hallyn
2007-04-03 15:45             ` Paul Menage
2007-04-03 15:54               ` Serge E. Hallyn
2007-04-03 16:16               ` Srivatsa Vaddagiri
2007-04-03 16:26               ` Kirill Korotaev
2007-04-03 16:46               ` Srivatsa Vaddagiri
2007-04-03 16:52                 ` Paul Menage
2007-04-03 17:11                   ` Srivatsa Vaddagiri
2007-04-03 17:10                     ` Paul Menage
2007-04-03 17:30                       ` Srivatsa Vaddagiri
2007-04-03 17:30                         ` Paul Menage
2007-04-03 17:51                           ` Srivatsa Vaddagiri
2007-04-03 17:49                             ` Paul Menage
2007-04-04  3:07                               ` Srivatsa Vaddagiri
2007-04-04  3:44                                 ` Paul Jackson
2007-04-04  4:04                                 ` Paul Menage
2007-04-04  5:15                                   ` Srivatsa Vaddagiri
2007-04-04  7:00                                     ` Paul Menage
2007-04-04 17:26                                       ` Srivatsa Vaddagiri
2007-04-04 17:42                                         ` Srivatsa Vaddagiri
2007-04-04 18:57                                         ` Paul Menage
2007-04-04 23:02                                           ` Eric W. Biederman
2007-04-05  1:35                                             ` Paul Menage
2007-04-05  1:37                                               ` Paul Menage
2007-04-05 16:57                                           ` Srivatsa Vaddagiri
2007-04-05 17:14                                             ` Srivatsa Vaddagiri
2007-04-06 21:54                                             ` Paul Menage
2007-04-05  2:57                                         ` Paul Menage
2007-04-05  6:39                                           ` Srivatsa Vaddagiri
2007-04-05  6:46                                             ` Srivatsa Vaddagiri
2007-04-05  6:48                                             ` Paul Menage
2007-04-05  8:49                                               ` Srivatsa Vaddagiri
2007-04-05  9:29                                                 ` Paul Menage
2007-04-05 12:43                                                   ` Srivatsa Vaddagiri
2007-04-05 14:13                                                     ` Srivatsa Vaddagiri
2007-04-05 14:13                                                     ` Paul Menage
2007-04-05 14:46                                                       ` Srivatsa Vaddagiri
2007-04-03 17:34                       ` Srivatsa Vaddagiri
2007-04-03 17:29                         ` Paul Menage
2007-04-03 16:10             ` Srivatsa Vaddagiri
2007-04-03 15:41           ` Serge E. Hallyn
2007-02-12  9:18 ` [PATCH 0/7] containers (V7): Generic Process Containers Paul Jackson
2007-02-12  9:32   ` Paul Menage
2007-02-12  9:52     ` Paul Jackson
     [not found] ` <20070212085104.485337000@menage.corp.google.com>
2007-02-12 15:27   ` [PATCH 3/7] containers (V7): Add generic multi-subsystem API to containers Srivatsa Vaddagiri
2007-02-12 18:40     ` Paul Menage
2007-02-13 13:19       ` Srivatsa Vaddagiri
2007-02-15  1:17         ` Paul Menage
2007-02-12 15:39   ` Serge E. Hallyn
2007-02-12 15:56     ` Cedric Le Goater
2007-02-12 18:31       ` Paul Menage
2007-02-14  8:49   ` Balbir Singh
2007-03-08 17:52   ` Srivatsa Vaddagiri
2007-03-24 12:51   ` [ckrm-tech] " Srivatsa Vaddagiri
2007-02-12 22:38 ` [PATCH 0/7] containers (V7): Generic Process Containers Sam Vilain
2007-02-12 22:47   ` Serge E. Hallyn
2007-02-12 23:18     ` Paul Menage
2007-02-12 23:15   ` Paul Menage
2007-02-13  0:30     ` Sam Vilain
2007-02-13  0:42       ` [ckrm-tech] " Paul Menage
2007-02-13  1:13         ` Sam Vilain
2007-02-13  1:47           ` Paul Menage
2007-02-20 17:34     ` Eric W. Biederman
2007-02-20 17:55       ` Paul Menage
2007-02-20 19:29         ` Eric W. Biederman
2007-02-20 22:47           ` Paul Menage
2007-02-20 23:08             ` Sam Vilain
2007-02-20 23:36               ` Paul Menage
2007-02-20 23:32             ` Serge E. Hallyn
2007-02-20 21:58         ` Sam Vilain
2007-02-20 22:19           ` Paul Menage
2007-02-20 22:58             ` Sam Vilain
2007-02-20 23:28               ` Paul Menage
2007-02-20 23:37               ` Serge E. Hallyn

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20070212085105.170265000@menage.corp.google.com \
    --to=menage@google.com \
    --cc=akpm@osdl.org \
    --cc=ckrm-tech@lists.sourceforge.net \
    --cc=containers@lists.osdl.org \
    --cc=dev@sw.ru \
    --cc=devel@openvz.org \
    --cc=ebiederm@xmission.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mbligh@google.com \
    --cc=pj@sgi.com \
    --cc=rohitseth@google.com \
    --cc=sekharan@us.ibm.com \
    --cc=serue@us.ibm.com \
    --cc=vatsa@in.ibm.com \
    --cc=winget@google.com \
    --cc=xemul@sw.ru \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.