blob: 2a5dfec8efe0504fc974a9500e934c51b78a5207 [file] [log] [blame]
Serge E. Hallyn858d72e2007-10-18 23:39:45 -07001/*
2 * ns_cgroup.c - namespace cgroup subsystem
3 *
4 * Copyright 2006, 2007 IBM Corp
5 */
6
7#include <linux/module.h>
8#include <linux/cgroup.h>
9#include <linux/fs.h>
Serge E. Hallyne885dcd2008-07-25 01:47:06 -070010#include <linux/proc_fs.h>
Robert P. J. Day1aeb2722008-04-29 00:59:25 -070011#include <linux/slab.h>
Adrian Bunk3ff31d02008-04-29 00:59:55 -070012#include <linux/nsproxy.h>
Serge E. Hallyn858d72e2007-10-18 23:39:45 -070013
14struct ns_cgroup {
15 struct cgroup_subsys_state css;
Serge E. Hallyn858d72e2007-10-18 23:39:45 -070016};
17
18struct cgroup_subsys ns_subsys;
19
20static inline struct ns_cgroup *cgroup_to_ns(
21 struct cgroup *cgroup)
22{
23 return container_of(cgroup_subsys_state(cgroup, ns_subsys_id),
24 struct ns_cgroup, css);
25}
26
Serge E. Hallyne885dcd2008-07-25 01:47:06 -070027int ns_cgroup_clone(struct task_struct *task, struct pid *pid)
Serge E. Hallyn858d72e2007-10-18 23:39:45 -070028{
Serge E. Hallyne885dcd2008-07-25 01:47:06 -070029 char name[PROC_NUMBUF];
30
31 snprintf(name, PROC_NUMBUF, "%d", pid_vnr(pid));
32 return cgroup_clone(task, &ns_subsys, name);
Serge E. Hallyn858d72e2007-10-18 23:39:45 -070033}
34
35/*
36 * Rules:
Grzegorz Nosek313e9242009-04-02 16:57:23 -070037 * 1. you can only enter a cgroup which is a descendant of your current
Serge E. Hallyn858d72e2007-10-18 23:39:45 -070038 * cgroup
39 * 2. you can only place another process into a cgroup if
40 * a. you have CAP_SYS_ADMIN
41 * b. your cgroup is an ancestor of task's destination cgroup
42 * (hence either you are in the same cgroup as task, or in an
43 * ancestor cgroup thereof)
44 */
Ben Blumbe367d02009-09-23 15:56:31 -070045static int ns_can_attach(struct cgroup_subsys *ss, struct cgroup *new_cgroup,
46 struct task_struct *task, bool threadgroup)
Serge E. Hallyn858d72e2007-10-18 23:39:45 -070047{
Serge E. Hallyn858d72e2007-10-18 23:39:45 -070048 if (current != task) {
49 if (!capable(CAP_SYS_ADMIN))
50 return -EPERM;
51
Grzegorz Nosek313e9242009-04-02 16:57:23 -070052 if (!cgroup_is_descendant(new_cgroup, current))
Serge E. Hallyn858d72e2007-10-18 23:39:45 -070053 return -EPERM;
54 }
55
Grzegorz Nosek313e9242009-04-02 16:57:23 -070056 if (!cgroup_is_descendant(new_cgroup, task))
Serge E. Hallyn858d72e2007-10-18 23:39:45 -070057 return -EPERM;
58
Ben Blumbe367d02009-09-23 15:56:31 -070059 if (threadgroup) {
60 struct task_struct *c;
61 rcu_read_lock();
62 list_for_each_entry_rcu(c, &task->thread_group, thread_group) {
63 if (!cgroup_is_descendant(new_cgroup, c)) {
64 rcu_read_unlock();
65 return -EPERM;
66 }
67 }
68 rcu_read_unlock();
69 }
70
Serge E. Hallyn858d72e2007-10-18 23:39:45 -070071 return 0;
72}
73
74/*
75 * Rules: you can only create a cgroup if
76 * 1. you are capable(CAP_SYS_ADMIN)
77 * 2. the target cgroup is a descendant of your own cgroup
78 */
79static struct cgroup_subsys_state *ns_create(struct cgroup_subsys *ss,
80 struct cgroup *cgroup)
81{
82 struct ns_cgroup *ns_cgroup;
83
84 if (!capable(CAP_SYS_ADMIN))
85 return ERR_PTR(-EPERM);
Grzegorz Nosek313e9242009-04-02 16:57:23 -070086 if (!cgroup_is_descendant(cgroup, current))
Serge E. Hallyn858d72e2007-10-18 23:39:45 -070087 return ERR_PTR(-EPERM);
88
89 ns_cgroup = kzalloc(sizeof(*ns_cgroup), GFP_KERNEL);
90 if (!ns_cgroup)
91 return ERR_PTR(-ENOMEM);
Serge E. Hallyn858d72e2007-10-18 23:39:45 -070092 return &ns_cgroup->css;
93}
94
95static void ns_destroy(struct cgroup_subsys *ss,
96 struct cgroup *cgroup)
97{
98 struct ns_cgroup *ns_cgroup;
99
100 ns_cgroup = cgroup_to_ns(cgroup);
101 kfree(ns_cgroup);
102}
103
104struct cgroup_subsys ns_subsys = {
105 .name = "ns",
106 .can_attach = ns_can_attach,
107 .create = ns_create,
108 .destroy = ns_destroy,
109 .subsys_id = ns_subsys_id,
110};