Package: cgmanager / 0.33-2+deb8u2

0003-make-sure-to-check-cgroup-hierarchy.patch Patch series | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
From 6267916d4ea939794e0583cd8b08bd0b9594a6e2 Mon Sep 17 00:00:00 2001
From: Serge Hallyn <serge.hallyn@ubuntu.com>
Date: Wed, 26 Nov 2014 01:00:10 -0600
Subject: [PATCH 1/1] make sure to check cgroup hierarchy

Some cases weren't doing that, although at least those were still
checking for proper ownership.

Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
---
 cgmanager.c |   85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 80 insertions(+), 5 deletions(-)

Index: cgmanager-0.33/cgmanager.c
===================================================================
--- cgmanager-0.33.orig/cgmanager.c
+++ cgmanager-0.33/cgmanager.c
@@ -558,13 +558,20 @@ next:
 int get_value_main(void *parent, const char *controller, const char *cgroup,
 		const char *key, struct ucred p, struct ucred r, char **value)
 {
-	char path[MAXPATHLEN];
+	char pcgpath[MAXPATHLEN], path[MAXPATHLEN];
 
 	if (!sane_cgroup(cgroup)) {
 		nih_error("%s: unsafe cgroup", __func__);
 		return -1;
 	}
 
+	// Get p's current cgroup in pcgpath
+	if (!compute_pid_cgroup(p.pid, controller, "", pcgpath, NULL)) {
+		nih_error("%s: Could not determine the proxy's cgroup for %s",
+				__func__, controller);
+		return -1;
+	}
+
 	if (!compute_pid_cgroup(r.pid, controller, cgroup, path, NULL)) {
 		nih_error("%s: Could not determine the requested cgroup (%s:%s)",
                 __func__, controller, cgroup);
@@ -577,6 +584,14 @@ int get_value_main(void *parent, const c
 		return -1;
 	}
 
+	// Make sure target cgroup is under proxy's
+	int plen = strlen(pcgpath);
+	if (strncmp(pcgpath, path, plen) != 0) {
+		nih_error("%s: target cgroup is not below r (%d)'s", __func__,
+			r.pid);
+		return -1;
+	}
+
 	/* append the filename */
 	if (strlen(path) + strlen(key) + 2 > MAXPATHLEN) {
 		nih_error("%s: filename too long for cgroup %s key %s", __func__, path, key);
@@ -608,19 +623,34 @@ int set_value_main(const char *controlle
 		struct ucred r)
 
 {
-	char path[MAXPATHLEN];
+	char pcgpath[MAXPATHLEN], path[MAXPATHLEN];
 
 	if (!sane_cgroup(cgroup)) {
 		nih_error("%s: unsafe cgroup", __func__);
 		return -1;
 	}
 
+	// Get p's current cgroup in pcgpath
+	if (!compute_pid_cgroup(p.pid, controller, "", pcgpath, NULL)) {
+		nih_error("%s: Could not determine the proxy's cgroup for %s",
+				__func__, controller);
+		return -1;
+	}
+
 	if (!compute_pid_cgroup(r.pid, controller, cgroup, path, NULL)) {
 		nih_error("%s: Could not determine the requested cgroup (%s:%s)",
                 __func__, controller, cgroup);
 		return -1;
 	}
 
+	// Make sure target cgroup is under proxy's
+	int plen = strlen(pcgpath);
+	if (strncmp(pcgpath, path, plen) != 0) {
+		nih_error("%s: target cgroup is not below r (%d)'s", __func__,
+			r.pid);
+		return -1;
+	}
+
 	/* Check access rights to the cgroup directory */
 	if (!may_access(r.pid, r.uid, r.gid, path, O_RDONLY)) {
 		nih_error("%s: Pid %d may not access %s\n", __func__, r.pid, path);
@@ -823,7 +853,7 @@ next:
 int get_tasks_main(void *parent, const char *controller, const char *cgroup,
 			struct ucred p, struct ucred r, int32_t **pids)
 {
-	char path[MAXPATHLEN];
+	char pcgpath[MAXPATHLEN], path[MAXPATHLEN];
 	const char *key = "tasks";
 	int alloced_pids = 0, nrpids = 0;
 
@@ -832,12 +862,27 @@ int get_tasks_main(void *parent, const c
 		return -1;
 	}
 
+	// Get p's current cgroup in pcgpath
+	if (!compute_pid_cgroup(p.pid, controller, "", pcgpath, NULL)) {
+		nih_error("%s: Could not determine the proxy's cgroup for %s",
+				__func__, controller);
+		return -1;
+	}
+
 	if (!compute_pid_cgroup(r.pid, controller, cgroup, path, NULL)) {
 		nih_error("%s: Could not determine the requested cgroup (%s:%s)",
                 __func__, controller, cgroup);
 		return -1;
 	}
 
+	// Make sure target cgroup is under proxy's
+	int plen = strlen(pcgpath);
+	if (strncmp(pcgpath, path, plen) != 0) {
+		nih_error("%s: target cgroup is not below r (%d)'s", __func__,
+			r.pid);
+		return -1;
+	}
+
 	/* Check access rights to the cgroup directory */
 	if (!may_access(r.pid, r.uid, r.gid, path, O_RDONLY)) {
 		nih_error("%s: Pid %d may not access %s\n", __func__, r.pid, path);
@@ -906,7 +951,7 @@ int collect_tasks(void *parent, const ch
 		struct ucred p, struct ucred r, int32_t **pids,
 		int *alloced_pids, int *nrpids)
 {
-	char path[MAXPATHLEN];
+	char pcgpath[MAXPATHLEN], path[MAXPATHLEN];
 	nih_local char *rpath = NULL;
 
 	if (!sane_cgroup(cgroup)) {
@@ -914,12 +959,27 @@ int collect_tasks(void *parent, const ch
 		return -1;
 	}
 
+	// Get p's current cgroup in pcgpath
+	if (!compute_pid_cgroup(p.pid, controller, "", pcgpath, NULL)) {
+		nih_error("%s: Could not determine the proxy's cgroup for %s",
+				__func__, controller);
+		return -1;
+	}
+
 	if (!compute_pid_cgroup(r.pid, controller, cgroup, path, NULL)) {
 		nih_error("%s: Could not determine the requested cgroup (%s:%s)",
                 __func__, controller, cgroup);
 		return -2;
 	}
 
+	// Make sure target cgroup is under proxy's
+	int plen = strlen(pcgpath);
+	if (strncmp(pcgpath, path, plen) != 0) {
+		nih_error("%s: target cgroup is not below r (%d)'s", __func__,
+			r.pid);
+		return -1;
+	}
+
 	/* Check access rights to the cgroup directory */
 	if (!may_access(r.pid, r.uid, r.gid, path, O_RDONLY)) {
 		nih_error("%s: Pid %d may not access %s\n", __func__, r.pid, path);
@@ -982,7 +1042,7 @@ next:
 int list_children_main(void *parent, const char *controller, const char *cgroup,
 			struct ucred p, struct ucred r, char ***output)
 {
-	char path[MAXPATHLEN];
+	char pcgpath[MAXPATHLEN], path[MAXPATHLEN];
 
 	*output = NULL;
 	if (!sane_cgroup(cgroup)) {
@@ -990,12 +1050,27 @@ int list_children_main(void *parent, con
 		return -1;
 	}
 
+	// Get p's current cgroup in pcgpath
+	if (!compute_pid_cgroup(p.pid, controller, "", pcgpath, NULL)) {
+		nih_error("%s: Could not determine the proxy's cgroup for %s",
+				__func__, controller);
+		return -1;
+	}
+
 	if (!compute_pid_cgroup(r.pid, controller, cgroup, path, NULL)) {
 		nih_error("%s: Could not determine the requested cgroup (%s:%s)",
                 __func__, controller, cgroup);
 		return -1;
 	}
 
+	// Make sure target cgroup is under proxy's
+	int plen = strlen(pcgpath);
+	if (strncmp(pcgpath, path, plen) != 0) {
+		nih_error("%s: target cgroup is not below r (%d)'s", __func__,
+			r.pid);
+		return -1;
+	}
+
 	/* Check access rights to the cgroup directory */
 	if (!may_access(r.pid, r.uid, r.gid, path, O_RDONLY)) {
 		nih_error("%s: Pid %d may not access %s\n", __func__, r.pid, path);