forked from google/nsjail
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcgroup2.cc
More file actions
479 lines (426 loc) · 15.2 KB
/
Copy pathcgroup2.cc
File metadata and controls
479 lines (426 loc) · 15.2 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/*
nsjail - cgroup2 namespacing
-----------------------------------------
Copyright 2014 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "cgroup2.h"
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <linux/magic.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/vfs.h>
#include <unistd.h>
#include "logs.h"
#include "util.h"
namespace cgroup2 {
static bool addPidToProcList(const std::string &cgroup_path, pid_t pid) {
std::string pid_str = std::to_string(pid);
LOG_D("Adding pid='%s' to cgroup.procs", pid_str.c_str());
if (!util::writeBufToFile((cgroup_path + "/cgroup.procs").c_str(), pid_str.c_str(),
pid_str.length(), O_WRONLY)) {
LOG_W("Could not update cgroup.procs");
return false;
}
return true;
}
static std::string getCgroupPath(nsjconf_t *nsjconf, pid_t pid) {
return nsjconf->cgroupv2_mount + "/NSJAIL." + std::to_string(pid);
}
static std::string getJailCgroupPath(nsjconf_t *nsjconf) {
return nsjconf->cgroupv2_mount + "/NSJAIL_SELF." + std::to_string(getpid());
}
static bool createCgroup(const std::string &cgroup_path, pid_t pid) {
LOG_D("Create '%s' for pid=%d", cgroup_path.c_str(), (int)pid);
if (mkdir(cgroup_path.c_str(), 0700) == -1 && errno != EEXIST) {
PLOG_W("mkdir('%s', 0700) failed", cgroup_path.c_str());
return false;
}
return true;
}
static bool moveSelfIntoChildCgroup(nsjconf_t *nsjconf) {
/*
* Move ourselves into another group to avoid the 'No internal processes' rule
* https://unix.stackexchange.com/a/713343
*/
std::string jail_cgroup_path = getJailCgroupPath(nsjconf);
LOG_I("nsjail is moving itself to a new child cgroup: %s\n", jail_cgroup_path.c_str());
RETURN_ON_FAILURE(createCgroup(jail_cgroup_path, getpid()));
RETURN_ON_FAILURE(addPidToProcList(jail_cgroup_path, 0));
return true;
}
static bool enableCgroupSubtree(nsjconf_t *nsjconf, const std::string &controller, pid_t pid) {
std::string cgroup_path = nsjconf->cgroupv2_mount;
LOG_D("Enable cgroup.subtree_control +'%s' to '%s' for pid=%d", controller.c_str(),
cgroup_path.c_str(), pid);
std::string val = "+" + controller;
/*
* Try once without moving the nsjail process and if that fails then try moving the nsjail
* process into a child cgroup before trying a second time.
*/
if (util::writeBufToFile((cgroup_path + "/cgroup.subtree_control").c_str(), val.c_str(),
val.length(), O_WRONLY, false)) {
return true;
}
if (errno == EBUSY) {
RETURN_ON_FAILURE(moveSelfIntoChildCgroup(nsjconf));
if (util::writeBufToFile((cgroup_path + "/cgroup.subtree_control").c_str(),
val.c_str(), val.length(), O_WRONLY)) {
return true;
}
}
LOG_E(
"Could not apply '%s' to cgroup.subtree_control in '%s'. nsjail MUST be run from root "
"and the cgroup mount path must refer to the root/host cgroup to use cgroupv2. If you "
"use Docker, you may need to run the container with --cgroupns=host so that nsjail can"
" access the host/root cgroupv2 hierarchy. An alternative is mounting (or remounting) "
"the cgroupv2 filesystem but using the flag is just simpler.",
val.c_str(), cgroup_path.c_str());
return false;
}
static bool writeToCgroup(
const std::string &cgroup_path, const std::string &resource, const std::string &value) {
LOG_I("Setting '%s' to '%s'", resource.c_str(), value.c_str());
if (!util::writeBufToFile(
(cgroup_path + "/" + resource).c_str(), value.c_str(), value.length(), O_WRONLY)) {
LOG_W("Could not update %s", resource.c_str());
return false;
}
return true;
}
static void removeCgroup(const std::string &cgroup_path) {
long long memory_peak_bytes = -1;
long long user_usec = -1;
long long system_usec = -1;
long long total_cpu_usec = -1;
// 1. Read memory.peak using C stdio
std::string mem_peak_path_str = cgroup_path + "/memory.peak";
const char *mem_peak_path = mem_peak_path_str.c_str();
FILE *fp_mem = fopen(mem_peak_path, "r");
if (fp_mem != NULL) {
char mem_buf[64]; // Buffer to read the number string
if (fgets(mem_buf, sizeof(mem_buf), fp_mem) != NULL) {
char *endptr = NULL;
errno = 0; // Reset errno before strtoll
long long val = strtoll(mem_buf, &endptr, 10);
// Check for conversion errors
if (errno == ERANGE) {
PLOG_W(
"Value in '%s' is out of range for long long", mem_peak_path);
memory_peak_bytes = -1;
} else if (errno != 0 && val == 0) {
PLOG_W("Error converting value in '%s'", mem_peak_path);
memory_peak_bytes = -1;
} else if (endptr == mem_buf) {
LOG_W("No numerical digits found in '%s'. Content starting with: "
"'%.10s'",
mem_peak_path, mem_buf);
memory_peak_bytes = -1;
} else {
char *check_ptr = endptr;
while (*check_ptr != '\0' && isspace((unsigned char)*check_ptr)) {
check_ptr++;
}
if (*check_ptr != '\0') {
LOG_W("Extra non-numeric/non-whitespace characters found "
"after number in '%s'. Content: '%.20s'",
mem_peak_path, mem_buf);
memory_peak_bytes = -1;
} else if (val < 0) {
LOG_W("Parsed negative memory peak value from '%s': %lld",
mem_peak_path, val);
memory_peak_bytes = -1;
} else {
memory_peak_bytes = val; // Success
}
}
} else {
if (feof(fp_mem)) {
LOG_W("File '%s' is empty.", mem_peak_path);
} else { // ferror(fp_mem) must be true
PLOG_W("Error reading from file '%s'", mem_peak_path);
}
memory_peak_bytes = -1;
}
fclose(fp_mem);
} else {
if (errno == ENOENT) {
LOG_D("File '%s' not found (errno=%d). Cgroup might have been removed.",
mem_peak_path, errno);
} else {
PLOG_W("Failed to open file '%s'", mem_peak_path);
}
// memory_peak_bytes remains -1
}
// 2. Read cpu.stat using C stdio
std::string cpu_stat_path_str = cgroup_path + "/cpu.stat";
const char *cpu_stat_path = cpu_stat_path_str.c_str();
FILE *fp_cpu = fopen(cpu_stat_path, "r");
if (fp_cpu != NULL) {
char line[256];
while (fgets(line, sizeof(line), fp_cpu) != NULL) {
if (user_usec == -1 && strncmp(line, "user_usec ", 10) ==
0) { // Process only if not already found
char *value_ptr = line + 10;
char *endptr = NULL;
errno = 0;
long long val = strtoll(value_ptr, &endptr, 10);
if (errno == ERANGE) {
PLOG_W("user_usec value out of range in '%s'. Line: '%s'",
cpu_stat_path, line);
user_usec = -2;
} // Use -2 to distinguish parse error from not found? Or just keep
// -1.
else if (errno != 0 && val == 0) {
PLOG_W("Error converting user_usec in '%s'. Line: '%s'",
cpu_stat_path, line);
user_usec = -1;
} else if (endptr == value_ptr) {
LOG_W("No numerical digits found for user_usec in '%s'. "
"Line: '%s'",
cpu_stat_path, line);
user_usec = -1;
} else {
char *check_ptr = endptr;
while (*check_ptr != '\0' &&
isspace((unsigned char)*check_ptr))
check_ptr++;
if (*check_ptr != '\0') {
LOG_W("Extra chars after user_usec value in '%s'. "
"Line: '%s'",
cpu_stat_path, line);
user_usec = -1;
} else if (val < 0) {
LOG_W("Parsed negative user_usec value from '%s': "
"%lld",
cpu_stat_path, val);
user_usec = -1;
} else {
user_usec = val;
} // Success
}
} else if (system_usec == -1 &&
strncmp(line, "system_usec ", 12) ==
0) { // Process only if not already found
char *value_ptr = line + 12;
char *endptr = NULL;
errno = 0;
long long val = strtoll(value_ptr, &endptr, 10);
if (errno == ERANGE) {
PLOG_W("system_usec value out of range in '%s'. Line: '%s'",
cpu_stat_path, line);
system_usec = -1;
} else if (errno != 0 && val == 0) {
PLOG_W("Error converting system_usec in '%s'. Line: '%s'",
cpu_stat_path, line);
system_usec = -1;
} else if (endptr == value_ptr) {
LOG_W("No numerical digits for system_usec in '%s'. Line: "
"'%s'",
cpu_stat_path, line);
system_usec = -1;
} else {
char *check_ptr = endptr;
while (*check_ptr != '\0' &&
isspace((unsigned char)*check_ptr))
check_ptr++;
if (*check_ptr != '\0') {
LOG_W("Extra chars after system_usec value in "
"'%s'. Line: '%s'",
cpu_stat_path, line);
system_usec = -1;
} else if (val < 0) {
LOG_W("Parsed negative system_usec value from "
"'%s': %lld",
cpu_stat_path, val);
system_usec = -1;
} else {
system_usec = val;
} // Success
}
}
// Optimization: if both valid values found, stop reading
if (user_usec != -1 && system_usec != -1) {
break;
}
} // end while fgets
if (ferror(fp_cpu)) { // Check if loop terminated due to read error
PLOG_W("Error occurred while reading '%s'", cpu_stat_path);
// If an error occurred mid-read, already found values might be suspect?
// For simplicity, keep them, but total will likely remain -1 if one is
// missing.
}
fclose(fp_cpu);
// Calculate total only if both components were successfully parsed and are
// non-negative
if (user_usec >= 0 && system_usec >= 0) {
total_cpu_usec = user_usec + system_usec;
} else {
// Log if we couldn't get both valid CPU times (and at least one wasn't just
// missing due to early exit)
if (user_usec == -1 || system_usec == -1) {
LOG_W("Could not determine total CPU usage from '%s' "
"(user_usec=%lld, system_usec=%lld)",
cpu_stat_path, user_usec, system_usec);
}
total_cpu_usec =
-1; // Ensure total is -1 if components are invalid or missing
}
} else {
// fopen failed for cpu.stat
if (errno == ENOENT) {
LOG_D("File '%s' not found (errno=%d). Cgroup might have been removed.",
cpu_stat_path, errno);
} else {
PLOG_W("Failed to open file '%s'", cpu_stat_path);
}
// Values remain -1
}
// 3. Log the collected statistics using nsjail's logging
// Use LOG_I for informational, or LOG_D for debug level.
LOG_I("Cgroup Stats: CPU_usec=%lld MEM_peak_bytes=%lld (user=%lld, system=%lld)",
total_cpu_usec, memory_peak_bytes, user_usec, system_usec);
LOG_D("Remove '%s'", cgroup_path.c_str());
if (rmdir(cgroup_path.c_str()) == -1) {
PLOG_W("rmdir('%s') failed", cgroup_path.c_str());
}
}
static bool needMemoryController(nsjconf_t *nsjconf) {
/*
* Check if we need 'memory'
* This matches the check in initNsFromParentMem()
*/
ssize_t swap_max = nsjconf->cgroup_mem_swap_max;
if (nsjconf->cgroup_mem_memsw_max > (size_t)0) {
swap_max = nsjconf->cgroup_mem_memsw_max - nsjconf->cgroup_mem_max;
}
if (nsjconf->cgroup_mem_max == (size_t)0 && swap_max < (ssize_t)0) {
return false;
}
return true;
}
static bool needPidsController(nsjconf_t *nsjconf) {
return nsjconf->cgroup_pids_max != 0;
}
static bool needCpuController(nsjconf_t *nsjconf) {
return nsjconf->cgroup_cpu_ms_per_sec != 0U;
}
/*
* We will use this buf to read from cgroup.subtree_control to see if
* the root cgroup has the necessary controllers listed
*/
#define SUBTREE_CONTROL_BUF_LEN 0x40
bool setup(nsjconf_t *nsjconf) {
/*
* Read from cgroup.subtree_control in the root to see if
* the controllers we need are there.
*/
auto p = nsjconf->cgroupv2_mount + "/cgroup.subtree_control";
char buf[SUBTREE_CONTROL_BUF_LEN];
int read = util::readFromFile(p.c_str(), buf, SUBTREE_CONTROL_BUF_LEN - 1);
if (read < 0) {
LOG_W("cgroupv2 setup: Could not read root subtree_control");
return false;
}
buf[read] = 0;
/* Are the controllers we need there? */
bool subtree_ok = (!needMemoryController(nsjconf) || strstr(buf, "memory")) &&
(!needPidsController(nsjconf) || strstr(buf, "pids")) &&
(!needCpuController(nsjconf) || strstr(buf, "cpu"));
if (!subtree_ok) {
/* Now we can write to the root cgroup.subtree_control */
if (needMemoryController(nsjconf)) {
RETURN_ON_FAILURE(enableCgroupSubtree(nsjconf, "memory", getpid()));
}
if (needPidsController(nsjconf)) {
RETURN_ON_FAILURE(enableCgroupSubtree(nsjconf, "pids", getpid()));
}
if (needCpuController(nsjconf)) {
RETURN_ON_FAILURE(enableCgroupSubtree(nsjconf, "cpu", getpid()));
}
}
return true;
}
bool detectCgroupv2(nsjconf_t *nsjconf) {
/*
* Check cgroupv2_mount, if it is a cgroup2 mount, use it.
*/
struct statfs buf;
if (statfs(nsjconf->cgroupv2_mount.c_str(), &buf)) {
LOG_D("statfs %s failed with %d", nsjconf->cgroupv2_mount.c_str(), errno);
nsjconf->use_cgroupv2 = false;
return false;
}
nsjconf->use_cgroupv2 = (buf.f_type == CGROUP2_SUPER_MAGIC);
return true;
}
static bool initNsFromParentMem(nsjconf_t *nsjconf, pid_t pid) {
ssize_t swap_max = nsjconf->cgroup_mem_swap_max;
if (nsjconf->cgroup_mem_memsw_max > (size_t)0) {
swap_max = nsjconf->cgroup_mem_memsw_max - nsjconf->cgroup_mem_max;
}
if (nsjconf->cgroup_mem_max == (size_t)0 && swap_max < (ssize_t)0) {
return true;
}
std::string cgroup_path = getCgroupPath(nsjconf, pid);
RETURN_ON_FAILURE(createCgroup(cgroup_path, pid));
RETURN_ON_FAILURE(addPidToProcList(cgroup_path, pid));
if (nsjconf->cgroup_mem_max > (size_t)0) {
RETURN_ON_FAILURE(writeToCgroup(
cgroup_path, "memory.max", std::to_string(nsjconf->cgroup_mem_max)));
}
if (swap_max >= (ssize_t)0) {
RETURN_ON_FAILURE(
writeToCgroup(cgroup_path, "memory.swap.max", std::to_string(swap_max)));
}
return true;
}
static bool initNsFromParentPids(nsjconf_t *nsjconf, pid_t pid) {
if (nsjconf->cgroup_pids_max == 0U) {
return true;
}
std::string cgroup_path = getCgroupPath(nsjconf, pid);
RETURN_ON_FAILURE(createCgroup(cgroup_path, pid));
RETURN_ON_FAILURE(addPidToProcList(cgroup_path, pid));
return writeToCgroup(cgroup_path, "pids.max", std::to_string(nsjconf->cgroup_pids_max));
}
static bool initNsFromParentCpu(nsjconf_t *nsjconf, pid_t pid) {
if (nsjconf->cgroup_cpu_ms_per_sec == 0U) {
return true;
}
std::string cgroup_path = getCgroupPath(nsjconf, pid);
RETURN_ON_FAILURE(createCgroup(cgroup_path, pid));
RETURN_ON_FAILURE(addPidToProcList(cgroup_path, pid));
/*
* The maximum bandwidth limit in the format: `$MAX $PERIOD`.
* This indicates that the group may consume up to $MAX in each $PERIOD
* duration.
*/
std::string cpu_ms_per_sec_str = std::to_string(nsjconf->cgroup_cpu_ms_per_sec * 1000U);
cpu_ms_per_sec_str += " 1000000";
return writeToCgroup(cgroup_path, "cpu.max", cpu_ms_per_sec_str);
}
bool initNsFromParent(nsjconf_t *nsjconf, pid_t pid) {
RETURN_ON_FAILURE(initNsFromParentMem(nsjconf, pid));
RETURN_ON_FAILURE(initNsFromParentPids(nsjconf, pid));
return initNsFromParentCpu(nsjconf, pid);
}
void finishFromParent(nsjconf_t *nsjconf, pid_t pid) {
if (nsjconf->cgroup_mem_max != (size_t)0 || nsjconf->cgroup_pids_max != 0U ||
nsjconf->cgroup_cpu_ms_per_sec != 0U) {
removeCgroup(getCgroupPath(nsjconf, pid));
}
}
} // namespace cgroup2