]>
Commit | Line | Data |
---|---|---|
de40a303 LP |
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
2 | ||
3 | #include <linux/seccomp.h> | |
4 | #include <linux/oom.h> | |
5 | #include <seccomp.h> | |
6 | ||
7 | #include "bus-util.h" | |
8 | #include "cap-list.h" | |
9 | #include "cpu-set-util.h" | |
10 | #include "env-util.h" | |
11 | #include "fs-util.h" | |
12 | #include "hostname-util.h" | |
13 | #include "json.h" | |
14 | #include "missing_sched.h" | |
15 | #include "nspawn-oci.h" | |
16 | #include "path-util.h" | |
17 | #include "rlimit-util.h" | |
18 | #include "seccomp-util.h" | |
19 | #include "stat-util.h" | |
20 | #include "stdio-util.h" | |
21 | #include "string-util.h" | |
22 | #include "strv.h" | |
23 | #include "user-util.h" | |
24 | ||
25 | /* TODO: | |
26 | * OCI runtime tool implementation | |
27 | * hooks | |
28 | * | |
29 | * Spec issues: | |
30 | * | |
31 | * How is RLIM_INFINITY supposed to be encoded? | |
32 | * configured effective caps is bullshit, as execv() corrupts it anyway | |
33 | * pipes bind mounted is *very* different from pipes newly created, comments regarding bind mount or not are bogus | |
34 | * annotation values structured? or string? | |
35 | * configurable file system namespace path, but then also root path? wtf? | |
36 | * apply sysctl inside of the container? or outside? | |
37 | * how is unlimited pids tasks limit to be encoded? | |
38 | * what are the defaults for caps if not specified? | |
39 | * what are the default uid/gid mappings if one is missing but the other set, or when user ns is on but no namespace configured | |
40 | * the source field of "mounts" is really weird, as it cannot realistically be relative to the bundle, since we never know if that's what the fs wants | |
41 | * spec contradicts itself on the mount "type" field, as the example uses "bind" as type, but it's not listed in /proc/filesystem, and is something made up by /bin/mount | |
42 | * if type of mount is left out, what shall be assumed? "bind"? | |
43 | * readonly mounts is entirely redundant? | |
44 | * should escaping be applied when joining mount options with ","? | |
45 | * devices cgroup support is bogus, "allow" and "deny" on the kernel level is about adding/removing entries, not about access | |
46 | * spec needs to say that "rwm" devices cgroup combination can't be the empty string | |
47 | * cgrouspv1 crap: kernel, kernelTCP, swapiness, disableOOMKiller, swap, devices, leafWeight | |
48 | * general: it shouldn't leak lower level abstractions this obviously | |
49 | * unmanagable cgroups stuff: realtimeRuntime/realtimePeriod | |
50 | * needs to say what happense when some option is not specified, i.e. which defautls apply | |
51 | * no architecture? no personality? | |
52 | * seccomp example and logic is simply broken: there's no constant "SCMP_ACT_ERRNO". | |
53 | * spec should say what to do with unknown props | |
54 | * /bin/mount regarding NFS and FUSE required? | |
55 | * what does terminal=false mean? | |
56 | * sysctl inside or outside? whitelisting? | |
57 | * | |
58 | * Unsupported: | |
59 | * | |
60 | * apparmorProfile | |
61 | * selinuxLabel + mountLabel | |
62 | * hugepageLimits | |
63 | * network | |
64 | * rdma | |
65 | * intelRdt | |
66 | * swappiness, disableOOMKiller, kernel, kernelTCP, leafWeight (because it's dead, cgroupsv2 can't do it and hence systemd neither) | |
67 | * | |
68 | * Non-slice cgroup paths | |
69 | * Propagation that is not slave + shared | |
70 | * more than one uid/gid mapping, mappings with a container base != 0, or non-matching uid/gid mappings | |
71 | * device cgroups access = false items that are not catchall | |
72 | * device cgroups matches where minor is specified, but major isn't. similar where major is specified but char/block is not. also, any match that only has a type set that has less than "rwm" set. also, any entry that has none of rwm set. | |
73 | * | |
74 | */ | |
75 | ||
76 | static int oci_unexpected(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
77 | json_log(v, flags, 0, "Unexpected OCI element '%s' of type '%s'.", name, json_variant_type_to_string(json_variant_type(v))); | |
78 | return -EINVAL; | |
79 | } | |
80 | ||
81 | static int oci_unsupported(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
82 | json_log(v, flags, 0, "Unsupported OCI element '%s' of type '%s'.", name, json_variant_type_to_string(json_variant_type(v))); | |
83 | return -EOPNOTSUPP; | |
84 | } | |
85 | ||
86 | static int oci_terminal(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
87 | Settings *s = userdata; | |
88 | ||
89 | /* If not specified, or set to true, we'll default to either an interactive or a read-only | |
90 | * console. If specifiy as false, we'll forcibly move to "pipe" mode though. */ | |
91 | s->console_mode = json_variant_boolean(v) ? _CONSOLE_MODE_INVALID : CONSOLE_PIPE; | |
92 | return 0; | |
93 | } | |
94 | ||
95 | static int oci_console_dimension(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) { | |
96 | unsigned *u = userdata; | |
97 | uintmax_t k; | |
98 | ||
99 | assert(u); | |
100 | ||
101 | k = json_variant_unsigned(variant); | |
102 | if (k == 0) { | |
103 | json_log(variant, flags, 0, "Console size field '%s' is too small.", strna(name)); | |
104 | return -ERANGE; | |
105 | } | |
106 | if (k > USHRT_MAX) { /* TIOCSWINSZ's struct winsize uses "unsigned short" for width and height */ | |
107 | json_log(variant, flags, 0, "Console size field '%s' is too large.", strna(name)); | |
108 | return -ERANGE; | |
109 | } | |
110 | ||
111 | *u = (unsigned) k; | |
112 | return 0; | |
113 | } | |
114 | ||
115 | static int oci_console_size(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
116 | ||
117 | static const JsonDispatch table[] = { | |
118 | { "height", JSON_VARIANT_UNSIGNED, oci_console_dimension, offsetof(Settings, console_height), JSON_MANDATORY }, | |
119 | { "width", JSON_VARIANT_UNSIGNED, oci_console_dimension, offsetof(Settings, console_width), JSON_MANDATORY }, | |
120 | {} | |
121 | }; | |
122 | ||
123 | return json_dispatch(v, table, oci_unexpected, flags, userdata); | |
124 | } | |
125 | ||
126 | static int oci_absolute_path(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
127 | char **p = userdata; | |
128 | const char *n; | |
129 | int r; | |
130 | ||
131 | assert(p); | |
132 | ||
133 | n = json_variant_string(v); | |
134 | ||
135 | if (!path_is_absolute(n)) { | |
136 | json_log(v, flags, 0, "Path in JSON field '%s' is not absolute: %s", strna(name), n); | |
137 | return -EINVAL; | |
138 | } | |
139 | ||
140 | r = free_and_strdup(p, n); | |
141 | if (r < 0) | |
142 | return log_oom(); | |
143 | ||
144 | return 0; | |
145 | } | |
146 | ||
147 | static int oci_env(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
148 | char ***l = userdata; | |
149 | JsonVariant *e; | |
150 | int r; | |
151 | ||
152 | assert(l); | |
153 | ||
154 | JSON_VARIANT_ARRAY_FOREACH(e, v) { | |
155 | const char *n; | |
156 | ||
157 | if (!json_variant_is_string(e)) { | |
158 | json_log(e, flags, 0, "Environment array contains non-string."); | |
159 | return -EINVAL; | |
160 | } | |
161 | ||
162 | assert_se(n = json_variant_string(e)); | |
163 | ||
164 | if (!env_assignment_is_valid(n)) { | |
165 | json_log(e, flags, 0, "Environment assignment not valid: %s", n); | |
166 | return -EINVAL; | |
167 | } | |
168 | ||
169 | r = strv_extend(l, n); | |
170 | if (r < 0) | |
171 | return log_oom(); | |
172 | } | |
173 | ||
174 | return 0; | |
175 | } | |
176 | ||
177 | static int oci_args(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
178 | _cleanup_strv_free_ char **l = NULL; | |
179 | char ***value = userdata; | |
180 | JsonVariant *e; | |
181 | int r; | |
182 | ||
183 | assert(value); | |
184 | ||
185 | JSON_VARIANT_ARRAY_FOREACH(e, v) { | |
186 | const char *n; | |
187 | ||
188 | if (!json_variant_is_string(e)) { | |
189 | json_log(v, flags, 0, "Argument is not a string."); | |
190 | return -EINVAL; | |
191 | } | |
192 | ||
193 | assert_se(n = json_variant_string(e)); | |
194 | ||
195 | r = strv_extend(&l, n); | |
196 | if (r < 0) | |
197 | return log_oom(); | |
198 | } | |
199 | ||
200 | if (strv_isempty(l)) { | |
201 | json_log(v, flags, 0, "Argument list empty, refusing."); | |
202 | return -EINVAL; | |
203 | } | |
204 | ||
205 | if (isempty(l[0])) { | |
206 | json_log(v, flags, 0, "Executable name is empty, refusing."); | |
207 | return -EINVAL; | |
208 | } | |
209 | ||
210 | strv_free_and_replace(*value, l); | |
211 | return 0; | |
212 | } | |
213 | ||
214 | static int oci_rlimit_type(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
215 | const char *z; | |
216 | int t, *type = userdata; | |
217 | ||
218 | assert_se(type); | |
219 | ||
220 | z = startswith(json_variant_string(v), "RLIMIT_"); | |
221 | if (!z) { | |
222 | json_log(v, flags, 0, "rlimit entry's name does not begin with 'RLIMIT_', refusing: %s", json_variant_string(v)); | |
223 | return -EINVAL; | |
224 | } | |
225 | ||
226 | t = rlimit_from_string(z); | |
227 | if (t < 0) { | |
228 | json_log(v, flags, 0, "rlimit name unknown: %s", json_variant_string(v)); | |
229 | return -EINVAL; | |
230 | } | |
231 | ||
232 | *type = t; | |
233 | return 0; | |
234 | } | |
235 | ||
236 | static int oci_rlimit_value(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
237 | rlim_t z, *value = userdata; | |
238 | ||
239 | assert(value); | |
240 | ||
241 | if (json_variant_is_negative(v)) | |
242 | z = RLIM_INFINITY; | |
243 | else { | |
244 | if (!json_variant_is_unsigned(v)) { | |
245 | json_log(v, flags, 0, "rlimits limit not unsigned, refusing."); | |
246 | return -ERANGE; | |
247 | } | |
248 | ||
249 | z = (rlim_t) json_variant_unsigned(v); | |
250 | ||
251 | if ((uintmax_t) z != json_variant_unsigned(v)) { | |
252 | json_log(v, flags, 0, "rlimits limit out of range, refusing."); | |
253 | return -EINVAL; | |
254 | } | |
255 | } | |
256 | ||
257 | *value = z; | |
258 | return 0; | |
259 | } | |
260 | ||
261 | static int oci_rlimits(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
262 | ||
263 | Settings *s = userdata; | |
264 | JsonVariant *e; | |
265 | int r; | |
266 | ||
267 | assert(s); | |
268 | ||
269 | JSON_VARIANT_ARRAY_FOREACH(e, v) { | |
270 | ||
271 | struct rlimit_data { | |
272 | int type; | |
273 | rlim_t soft; | |
274 | rlim_t hard; | |
275 | } data = { | |
276 | .type = -1, | |
277 | .soft = RLIM_INFINITY, | |
278 | .hard = RLIM_INFINITY, | |
279 | }; | |
280 | ||
281 | static const JsonDispatch table[] = { | |
282 | { "soft", JSON_VARIANT_NUMBER, oci_rlimit_value, offsetof(struct rlimit_data, soft), JSON_MANDATORY }, | |
283 | { "hard", JSON_VARIANT_NUMBER, oci_rlimit_value, offsetof(struct rlimit_data, hard), JSON_MANDATORY }, | |
284 | { "type", JSON_VARIANT_STRING, oci_rlimit_type, offsetof(struct rlimit_data, type), JSON_MANDATORY }, | |
285 | {} | |
286 | }; | |
287 | ||
288 | ||
289 | r = json_dispatch(e, table, oci_unexpected, flags, &data); | |
290 | if (r < 0) | |
291 | return r; | |
292 | ||
293 | assert(data.type >= 0); | |
294 | assert(data.type < _RLIMIT_MAX); | |
295 | ||
296 | if (s->rlimit[data.type]) { | |
297 | json_log(v, flags, 0, "rlimits array contains duplicate entry, refusing."); | |
298 | return -EINVAL; | |
299 | } | |
300 | ||
301 | s->rlimit[data.type] = new(struct rlimit, 1); | |
302 | if (!s->rlimit[data.type]) | |
303 | return log_oom(); | |
304 | ||
305 | *s->rlimit[data.type] = (struct rlimit) { | |
306 | .rlim_cur = data.soft, | |
307 | .rlim_max = data.hard, | |
308 | }; | |
309 | ||
310 | } | |
311 | return 0; | |
312 | } | |
313 | ||
314 | static int oci_capability_array(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
315 | uint64_t *mask = userdata, m = 0; | |
316 | JsonVariant *e; | |
317 | ||
318 | JSON_VARIANT_ARRAY_FOREACH(e, v) { | |
319 | const char *n; | |
320 | int cap; | |
321 | ||
322 | if (!json_variant_is_string(e)) { | |
323 | json_log(v, flags, 0, "Entry in capabilities array is not a string."); | |
324 | return -EINVAL; | |
325 | } | |
326 | ||
327 | assert_se(n = json_variant_string(e)); | |
328 | ||
329 | cap = capability_from_name(n); | |
330 | if (cap < 0) { | |
331 | json_log(v, flags, 0, "Unknown capability: %s", n); | |
332 | return -EINVAL; | |
333 | } | |
334 | ||
335 | m |= UINT64_C(1) << cap; | |
336 | } | |
337 | ||
338 | if (*mask == (uint64_t) -1) | |
339 | *mask = m; | |
340 | else | |
341 | *mask |= m; | |
342 | ||
343 | return 0; | |
344 | } | |
345 | ||
346 | static int oci_capabilities(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
347 | ||
348 | static const JsonDispatch table[] = { | |
349 | { "effective", JSON_VARIANT_ARRAY, oci_capability_array, offsetof(CapabilityQuintet, effective) }, | |
350 | { "bounding", JSON_VARIANT_ARRAY, oci_capability_array, offsetof(CapabilityQuintet, bounding) }, | |
351 | { "inheritable", JSON_VARIANT_ARRAY, oci_capability_array, offsetof(CapabilityQuintet, inheritable) }, | |
352 | { "permitted", JSON_VARIANT_ARRAY, oci_capability_array, offsetof(CapabilityQuintet, permitted) }, | |
353 | { "ambient", JSON_VARIANT_ARRAY, oci_capability_array, offsetof(CapabilityQuintet, ambient) }, | |
354 | {} | |
355 | }; | |
356 | ||
357 | Settings *s = userdata; | |
358 | int r; | |
359 | ||
360 | assert(s); | |
361 | ||
362 | r = json_dispatch(v, table, oci_unexpected, flags, &s->full_capabilities); | |
363 | if (r < 0) | |
364 | return r; | |
365 | ||
366 | if (s->full_capabilities.bounding != (uint64_t) -1) { | |
367 | s->capability = s->full_capabilities.bounding; | |
368 | s->drop_capability = ~s->full_capabilities.bounding; | |
369 | } | |
370 | ||
371 | return 0; | |
372 | } | |
373 | ||
374 | static int oci_oom_score_adj(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
375 | Settings *s = userdata; | |
376 | intmax_t k; | |
377 | ||
378 | assert(s); | |
379 | ||
380 | k = json_variant_integer(v); | |
381 | if (k < OOM_SCORE_ADJ_MIN || k > OOM_SCORE_ADJ_MAX) { | |
382 | json_log(v, flags, 0, "oomScoreAdj value out of range: %ji", k); | |
383 | return -EINVAL; | |
384 | } | |
385 | ||
386 | s->oom_score_adjust = (int) k; | |
387 | s->oom_score_adjust_set = true; | |
388 | ||
389 | return 0; | |
390 | } | |
391 | ||
392 | static int oci_uid_gid(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
393 | uid_t *uid = userdata, u; | |
394 | uintmax_t k; | |
395 | ||
396 | assert(uid); | |
397 | assert_cc(sizeof(uid_t) == sizeof(gid_t)); | |
398 | ||
399 | k = json_variant_unsigned(v); | |
400 | u = (uid_t) k; | |
401 | if ((uintmax_t) u != k) { | |
402 | json_log(v, flags, 0, "UID/GID out of range: %ji", k); | |
403 | return -EINVAL; | |
404 | } | |
405 | ||
406 | if (!uid_is_valid(u)) { | |
407 | json_log(v, flags, 0, "UID/GID is not valid: " UID_FMT, u); | |
408 | return -EINVAL; | |
409 | } | |
410 | ||
411 | *uid = u; | |
412 | return 0; | |
413 | } | |
414 | ||
415 | static int oci_supplementary_gids(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
416 | Settings *s = userdata; | |
417 | JsonVariant *e; | |
418 | int r; | |
419 | ||
420 | assert(s); | |
421 | ||
422 | JSON_VARIANT_ARRAY_FOREACH(e, v) { | |
423 | gid_t gid, *a; | |
424 | ||
425 | if (!json_variant_is_unsigned(e)) { | |
426 | json_log(v, flags, 0, "Supplementary GID entry is not a UID."); | |
427 | return -EINVAL; | |
428 | } | |
429 | ||
430 | r = oci_uid_gid(name, e, flags, &gid); | |
431 | if (r < 0) | |
432 | return r; | |
433 | ||
434 | a = reallocarray(s->supplementary_gids, s->n_supplementary_gids + 1, sizeof(gid_t)); | |
435 | if (!a) | |
436 | return log_oom(); | |
437 | ||
438 | s->supplementary_gids = a; | |
439 | s->supplementary_gids[s->n_supplementary_gids++] = gid; | |
440 | } | |
441 | ||
442 | return 0; | |
443 | } | |
444 | ||
445 | static int oci_user(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
446 | static const JsonDispatch table[] = { | |
447 | { "uid", JSON_VARIANT_UNSIGNED, oci_uid_gid, offsetof(Settings, uid), JSON_MANDATORY }, | |
448 | { "gid", JSON_VARIANT_UNSIGNED, oci_uid_gid, offsetof(Settings, gid), JSON_MANDATORY }, | |
449 | { "additionalGids", JSON_VARIANT_ARRAY, oci_supplementary_gids, 0, 0 }, | |
450 | {} | |
451 | }; | |
452 | ||
453 | return json_dispatch(v, table, oci_unexpected, flags, userdata); | |
454 | } | |
455 | ||
456 | static int oci_process(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
457 | ||
458 | static const JsonDispatch table[] = { | |
459 | { "terminal", JSON_VARIANT_BOOLEAN, oci_terminal, 0, 0 }, | |
460 | { "consoleSize", JSON_VARIANT_OBJECT, oci_console_size, 0, 0 }, | |
461 | { "cwd", JSON_VARIANT_STRING, oci_absolute_path, offsetof(Settings, working_directory), 0 }, | |
462 | { "env", JSON_VARIANT_ARRAY, oci_env, offsetof(Settings, environment), 0 }, | |
463 | { "args", JSON_VARIANT_ARRAY, oci_args, offsetof(Settings, parameters), 0 }, | |
464 | { "rlimits", JSON_VARIANT_ARRAY, oci_rlimits, 0, 0 }, | |
465 | { "apparmorProfile", JSON_VARIANT_STRING, oci_unsupported, 0, JSON_PERMISSIVE }, | |
466 | { "capabilities", JSON_VARIANT_OBJECT, oci_capabilities, 0, 0 }, | |
467 | { "noNewPrivileges", JSON_VARIANT_BOOLEAN, json_dispatch_boolean, offsetof(Settings, no_new_privileges), 0 }, | |
468 | { "oomScoreAdj", JSON_VARIANT_INTEGER, oci_oom_score_adj, 0, 0 }, | |
469 | { "selinuxLabel", JSON_VARIANT_STRING, oci_unsupported, 0, JSON_PERMISSIVE }, | |
470 | { "user", JSON_VARIANT_OBJECT, oci_user, 0, 0 }, | |
471 | {} | |
472 | }; | |
473 | ||
474 | return json_dispatch(v, table, oci_unexpected, flags, userdata); | |
475 | } | |
476 | ||
477 | static int oci_root(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
478 | ||
479 | static const JsonDispatch table[] = { | |
480 | { "path", JSON_VARIANT_STRING, json_dispatch_string, offsetof(Settings, root) }, | |
481 | { "readonly", JSON_VARIANT_BOOLEAN, json_dispatch_boolean, offsetof(Settings, read_only) }, | |
482 | {} | |
483 | }; | |
484 | ||
485 | return json_dispatch(v, table, oci_unexpected, flags, userdata); | |
486 | } | |
487 | ||
488 | static int oci_hostname(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
489 | Settings *s = userdata; | |
490 | const char *n; | |
491 | ||
492 | assert(s); | |
493 | ||
494 | assert_se(n = json_variant_string(v)); | |
495 | ||
496 | if (!hostname_is_valid(n, false)) { | |
497 | json_log(v, flags, 0, "Hostname string is not a valid hostname: %s", n); | |
498 | return -EINVAL; | |
499 | } | |
500 | ||
501 | if (free_and_strdup(&s->hostname, n) < 0) | |
502 | return log_oom(); | |
503 | ||
504 | return 0; | |
505 | } | |
506 | ||
507 | static bool oci_exclude_mount(const char *path) { | |
508 | ||
509 | /* Returns "true" for all mounts we insist to mount on our own, and hence ignore the OCI data. */ | |
510 | ||
511 | if (PATH_IN_SET(path, | |
512 | "/dev", | |
513 | "/dev/mqueue", | |
514 | "/dev/pts", | |
515 | "/dev/shm", | |
516 | "/proc", | |
517 | "/proc/acpi", | |
518 | "/proc/apm", | |
519 | "/proc/asound", | |
520 | "/proc/bus", | |
521 | "/proc/fs", | |
522 | "/proc/irq", | |
523 | "/proc/kallsyms", | |
524 | "/proc/kcore", | |
525 | "/proc/keys", | |
526 | "/proc/scsi", | |
527 | "/proc/sys", | |
528 | "/proc/sys/net", | |
529 | "/proc/sysrq-trigger", | |
530 | "/proc/timer_list", | |
531 | "/run", | |
532 | "/sys", | |
533 | "/sys", | |
534 | "/sys/fs/selinux", | |
535 | "/tmp")) | |
536 | return true; | |
537 | ||
538 | /* Similar, skip the whole /sys/fs/cgroups subtree */ | |
539 | if (path_startswith(path, "/sys/fs/cgroup")) | |
540 | return true; | |
541 | ||
542 | return false; | |
543 | } | |
544 | ||
545 | static int oci_mounts(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
546 | Settings *s = userdata; | |
547 | JsonVariant *e; | |
548 | int r; | |
549 | ||
550 | assert(s); | |
551 | ||
552 | JSON_VARIANT_ARRAY_FOREACH(e, v) { | |
553 | ||
554 | struct mount_data { | |
555 | char *destination; | |
556 | char *source; | |
557 | char *type; | |
558 | char **options; | |
559 | } data = {}; | |
560 | ||
561 | static const JsonDispatch table[] = { | |
562 | { "destination", JSON_VARIANT_STRING, oci_absolute_path, offsetof(struct mount_data, destination), JSON_MANDATORY }, | |
563 | { "source", JSON_VARIANT_STRING, json_dispatch_string, offsetof(struct mount_data, source), 0 }, | |
564 | { "options", JSON_VARIANT_ARRAY, json_dispatch_strv, offsetof(struct mount_data, options), 0, }, | |
565 | { "type", JSON_VARIANT_STRING, json_dispatch_string, offsetof(struct mount_data, type), 0 }, | |
566 | {} | |
567 | }; | |
568 | ||
569 | _cleanup_free_ char *joined_options = NULL; | |
570 | CustomMount *m; | |
571 | ||
572 | r = json_dispatch(e, table, oci_unexpected, flags, &data); | |
573 | if (r < 0) | |
574 | goto fail_item; | |
575 | ||
576 | if (!path_is_absolute(data.destination)) { | |
577 | json_log(e, flags, 0, "Mount destination not an absolute path: %s", data.destination); | |
578 | r = -EINVAL; | |
579 | goto fail_item; | |
580 | } | |
581 | ||
582 | if (oci_exclude_mount(data.destination)) | |
583 | goto skip_item; | |
584 | ||
585 | if (data.options) { | |
586 | joined_options = strv_join(data.options, ","); | |
587 | if (!joined_options) { | |
588 | r = log_oom(); | |
589 | goto fail_item; | |
590 | } | |
591 | } | |
592 | ||
593 | if (!data.type || streq(data.type, "bind")) { | |
594 | ||
595 | if (!path_is_absolute(data.source)) { | |
596 | char *joined; | |
597 | ||
598 | joined = path_join(s->bundle, data.source); | |
599 | if (!joined) { | |
600 | r = log_oom(); | |
601 | goto fail_item; | |
602 | } | |
603 | ||
604 | free_and_replace(data.source, joined); | |
605 | } | |
606 | ||
607 | data.type = mfree(data.type); | |
608 | ||
609 | m = custom_mount_add(&s->custom_mounts, &s->n_custom_mounts, CUSTOM_MOUNT_BIND); | |
610 | } else | |
611 | m = custom_mount_add(&s->custom_mounts, &s->n_custom_mounts, CUSTOM_MOUNT_ARBITRARY); | |
612 | if (!m) { | |
613 | r = log_oom(); | |
614 | goto fail_item; | |
615 | } | |
616 | ||
617 | m->destination = TAKE_PTR(data.destination); | |
618 | m->source = TAKE_PTR(data.source); | |
619 | m->options = TAKE_PTR(joined_options); | |
620 | m->type_argument = TAKE_PTR(data.type); | |
621 | ||
622 | strv_free(data.options); | |
623 | continue; | |
624 | ||
625 | fail_item: | |
626 | free(data.destination); | |
627 | free(data.source); | |
628 | strv_free(data.options); | |
629 | free(data.type); | |
630 | ||
631 | return r; | |
632 | ||
633 | skip_item: | |
634 | free(data.destination); | |
635 | free(data.source); | |
636 | strv_free(data.options); | |
637 | free(data.type); | |
638 | } | |
639 | ||
640 | return 0; | |
641 | } | |
642 | ||
643 | static int oci_namespace_type(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
644 | unsigned long *nsflags = userdata; | |
645 | const char *n; | |
646 | ||
647 | assert(nsflags); | |
648 | assert_se(n = json_variant_string(v)); | |
649 | ||
650 | /* We don't use namespace_flags_from_string() here, as the OCI spec uses slightly different names than the | |
651 | * kernel here. */ | |
652 | if (streq(n, "pid")) | |
653 | *nsflags = CLONE_NEWPID; | |
654 | else if (streq(n, "network")) | |
655 | *nsflags = CLONE_NEWNET; | |
656 | else if (streq(n, "mount")) | |
657 | *nsflags = CLONE_NEWNS; | |
658 | else if (streq(n, "ipc")) | |
659 | *nsflags = CLONE_NEWIPC; | |
660 | else if (streq(n, "uts")) | |
661 | *nsflags = CLONE_NEWUTS; | |
662 | else if (streq(n, "user")) | |
663 | *nsflags = CLONE_NEWUSER; | |
664 | else if (streq(n, "cgroup")) | |
665 | *nsflags = CLONE_NEWCGROUP; | |
666 | else { | |
667 | json_log(v, flags, 0, "Unknown cgroup type, refusing: %s", n); | |
668 | return -EINVAL; | |
669 | } | |
670 | ||
671 | return 0; | |
672 | } | |
673 | ||
674 | static int oci_namespaces(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
675 | Settings *s = userdata; | |
676 | unsigned long n = 0; | |
677 | JsonVariant *e; | |
678 | int r; | |
679 | ||
680 | assert_se(s); | |
681 | ||
682 | JSON_VARIANT_ARRAY_FOREACH(e, v) { | |
683 | ||
684 | struct namespace_data { | |
685 | unsigned long type; | |
686 | char *path; | |
687 | } data = {}; | |
688 | ||
689 | static const JsonDispatch table[] = { | |
690 | { "type", JSON_VARIANT_STRING, oci_namespace_type, offsetof(struct namespace_data, type), JSON_MANDATORY }, | |
691 | { "path", JSON_VARIANT_STRING, oci_absolute_path, offsetof(struct namespace_data, path), 0 }, | |
692 | {} | |
693 | }; | |
694 | ||
695 | r = json_dispatch(e, table, oci_unexpected, flags, &data); | |
696 | if (r < 0) { | |
697 | free(data.path); | |
698 | return r; | |
699 | } | |
700 | ||
701 | if (data.path) { | |
702 | if (data.type != CLONE_NEWNET) { | |
703 | json_log(e, flags, 0, "Specifying namespace path for non-network namespace is not supported."); | |
704 | free(data.path); | |
705 | return -EOPNOTSUPP; | |
706 | } | |
707 | ||
708 | if (s->network_namespace_path) { | |
709 | json_log(e, flags, 0, "Network namespace path specified more than once, refusing."); | |
710 | free(data.path); | |
711 | return -EINVAL; | |
712 | } | |
713 | ||
714 | free(s->network_namespace_path); | |
715 | s->network_namespace_path = data.path; | |
716 | } | |
717 | ||
718 | if (FLAGS_SET(n, data.type)) { | |
719 | json_log(e, flags, 0, "Duplicat namespace specification, refusing."); | |
720 | return -EINVAL; | |
721 | } | |
722 | ||
723 | n |= data.type; | |
724 | } | |
725 | ||
726 | if (!FLAGS_SET(n, CLONE_NEWNS)) { | |
727 | json_log(v, flags, 0, "Containers without file system namespace aren't supported."); | |
728 | return -EOPNOTSUPP; | |
729 | } | |
730 | ||
731 | s->private_network = FLAGS_SET(n, CLONE_NEWNET); | |
732 | s->userns_mode = FLAGS_SET(n, CLONE_NEWUSER) ? USER_NAMESPACE_FIXED : USER_NAMESPACE_NO; | |
733 | s->use_cgns = FLAGS_SET(n, CLONE_NEWCGROUP); | |
734 | ||
735 | s->clone_ns_flags = n & (CLONE_NEWIPC|CLONE_NEWPID|CLONE_NEWUTS); | |
736 | ||
737 | return 0; | |
738 | } | |
739 | ||
740 | static int oci_uid_gid_range(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
741 | uid_t *uid = userdata, u; | |
742 | uintmax_t k; | |
743 | ||
744 | assert(uid); | |
745 | assert_cc(sizeof(uid_t) == sizeof(gid_t)); | |
746 | ||
747 | /* This is very much like oci_uid_gid(), except the checks are a bit different, as this is a UID range rather | |
748 | * than a specific UID, and hence (uid_t) -1 has no special significance. OTOH a range of zero makes no | |
749 | * sense. */ | |
750 | ||
751 | k = json_variant_unsigned(v); | |
752 | u = (uid_t) k; | |
753 | if ((uintmax_t) u != k) { | |
754 | json_log(v, flags, 0, "UID/GID out of range: %ji", k); | |
755 | return -ERANGE; | |
756 | } | |
757 | if (u == 0) { | |
758 | json_log(v, flags, 0, "UID/GID range can't be zero."); | |
759 | return -ERANGE; | |
760 | } | |
761 | ||
762 | *uid = u; | |
763 | return 0; | |
764 | } | |
765 | ||
766 | static int oci_uid_gid_mappings(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
767 | struct mapping_data { | |
768 | uid_t host_id; | |
769 | uid_t container_id; | |
770 | uid_t range; | |
771 | } data = { | |
772 | .host_id = UID_INVALID, | |
773 | .container_id = UID_INVALID, | |
774 | .range = 0, | |
775 | }; | |
776 | ||
777 | static const JsonDispatch table[] = { | |
778 | { "containerID", JSON_VARIANT_UNSIGNED, oci_uid_gid, offsetof(struct mapping_data, container_id), JSON_MANDATORY }, | |
779 | { "hostID", JSON_VARIANT_UNSIGNED, oci_uid_gid, offsetof(struct mapping_data, host_id), JSON_MANDATORY }, | |
780 | { "size", JSON_VARIANT_UNSIGNED, oci_uid_gid_range, offsetof(struct mapping_data, range), JSON_MANDATORY }, | |
781 | {} | |
782 | }; | |
783 | ||
784 | Settings *s = userdata; | |
785 | JsonVariant *e; | |
786 | int r; | |
787 | ||
788 | assert(s); | |
789 | ||
790 | if (json_variant_elements(v) == 0) | |
791 | return 0; | |
792 | ||
793 | if (json_variant_elements(v) > 1) { | |
794 | json_log(v, flags, 0, "UID/GID mappings with more than one entry are not supported."); | |
795 | return -EOPNOTSUPP; | |
796 | } | |
797 | ||
798 | assert_se(e = json_variant_by_index(v, 0)); | |
799 | ||
800 | r = json_dispatch(e, table, oci_unexpected, flags, &data); | |
801 | if (r < 0) | |
802 | return r; | |
803 | ||
804 | if (data.host_id + data.range < data.host_id || | |
805 | data.container_id + data.range < data.container_id) { | |
806 | json_log(v, flags, 0, "UID/GID range goes beyond UID/GID validity range, refusing."); | |
807 | return -EINVAL; | |
808 | } | |
809 | ||
810 | if (data.container_id != 0) { | |
811 | json_log(v, flags, 0, "UID/GID mappings with a non-zero container base are not supported."); | |
812 | return -EOPNOTSUPP; | |
813 | } | |
814 | ||
815 | if (data.range < 0x10000) | |
816 | json_log(v, flags|JSON_WARNING, 0, "UID/GID mapping with less than 65536 UID/GIDS set up, you are looking for trouble."); | |
817 | ||
818 | if (s->uid_range != UID_INVALID && | |
819 | (s->uid_shift != data.host_id || s->uid_range != data.range)) { | |
820 | json_log(v, flags, 0, "Non-matching UID and GID mappings are not supported."); | |
821 | return -EOPNOTSUPP; | |
822 | } | |
823 | ||
824 | s->uid_shift = data.host_id; | |
825 | s->uid_range = data.range; | |
826 | ||
827 | return 0; | |
828 | } | |
829 | ||
830 | static int oci_device_type(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
831 | mode_t *mode = userdata; | |
832 | const char *t; | |
833 | ||
834 | assert(mode); | |
835 | assert_se(t = json_variant_string(v)); | |
836 | ||
837 | if (STR_IN_SET(t, "c", "u")) | |
838 | *mode = (*mode & ~S_IFMT) | S_IFCHR; | |
839 | else if (streq(t, "b")) | |
840 | *mode = (*mode & ~S_IFMT) | S_IFBLK; | |
841 | else if (streq(t, "p")) | |
842 | *mode = (*mode & ~S_IFMT) | S_IFIFO; | |
843 | else { | |
844 | json_log(v, flags, 0, "Unknown device type: %s", t); | |
845 | return -EINVAL; | |
846 | } | |
847 | ||
848 | return 0; | |
849 | } | |
850 | ||
851 | static int oci_device_major(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
852 | unsigned *u = userdata; | |
853 | uintmax_t k; | |
854 | ||
855 | assert_se(u); | |
856 | ||
857 | k = json_variant_unsigned(v); | |
858 | if (!DEVICE_MAJOR_VALID(k)) { | |
859 | json_log(v, flags, 0, "Device major %ji out of range.", k); | |
860 | return -ERANGE; | |
861 | } | |
862 | ||
863 | *u = (unsigned) k; | |
864 | return 0; | |
865 | } | |
866 | ||
867 | static int oci_device_minor(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
868 | unsigned *u = userdata; | |
869 | uintmax_t k; | |
870 | ||
871 | assert_se(u); | |
872 | ||
873 | k = json_variant_unsigned(v); | |
874 | if (!DEVICE_MINOR_VALID(k)) { | |
875 | json_log(v, flags, 0, "Device minor %ji out of range.", k); | |
876 | return -ERANGE; | |
877 | } | |
878 | ||
879 | *u = (unsigned) k; | |
880 | return 0; | |
881 | } | |
882 | ||
883 | static int oci_device_file_mode(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
884 | mode_t *mode = userdata, m; | |
885 | uintmax_t k; | |
886 | ||
887 | assert(mode); | |
888 | ||
889 | k = json_variant_unsigned(v); | |
890 | m = (mode_t) k; | |
891 | ||
892 | if ((m & ~07777) != 0 || (uintmax_t) m != k) { | |
893 | json_log(v, flags, 0, "fileMode out of range, refusing."); | |
894 | return -ERANGE; | |
895 | } | |
896 | ||
897 | *mode = m; | |
898 | return 0; | |
899 | } | |
900 | ||
901 | static int oci_devices(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
902 | Settings *s = userdata; | |
903 | JsonVariant *e; | |
904 | int r; | |
905 | ||
906 | assert(s); | |
907 | ||
908 | JSON_VARIANT_ARRAY_FOREACH(e, v) { | |
909 | ||
910 | static const JsonDispatch table[] = { | |
911 | { "type", JSON_VARIANT_STRING, oci_device_type, offsetof(DeviceNode, mode), JSON_MANDATORY }, | |
912 | { "path", JSON_VARIANT_STRING, oci_absolute_path, offsetof(DeviceNode, path), JSON_MANDATORY }, | |
913 | { "major", JSON_VARIANT_UNSIGNED, oci_device_major, offsetof(DeviceNode, major), 0 }, | |
914 | { "minor", JSON_VARIANT_UNSIGNED, oci_device_minor, offsetof(DeviceNode, minor), 0 }, | |
915 | { "fileMode", JSON_VARIANT_UNSIGNED, oci_device_file_mode, offsetof(DeviceNode, mode), 0 }, | |
916 | { "uid", JSON_VARIANT_UNSIGNED, oci_uid_gid, offsetof(DeviceNode, uid), 0 }, | |
917 | { "gid", JSON_VARIANT_UNSIGNED, oci_uid_gid, offsetof(DeviceNode, gid), 0 }, | |
918 | {} | |
919 | }; | |
920 | ||
921 | DeviceNode *node, *nodes; | |
922 | ||
923 | nodes = reallocarray(s->extra_nodes, s->n_extra_nodes + 1, sizeof(DeviceNode)); | |
924 | if (!nodes) | |
925 | return log_oom(); | |
926 | ||
927 | s->extra_nodes = nodes; | |
928 | ||
929 | node = nodes + s->n_extra_nodes; | |
930 | *node = (DeviceNode) { | |
931 | .uid = UID_INVALID, | |
932 | .gid = GID_INVALID, | |
933 | .major = (unsigned) -1, | |
934 | .minor = (unsigned) -1, | |
935 | .mode = 0644, | |
936 | }; | |
937 | ||
938 | r = json_dispatch(e, table, oci_unexpected, flags, node); | |
939 | if (r < 0) | |
940 | goto fail_element; | |
941 | ||
942 | if (S_ISCHR(node->mode) || S_ISBLK(node->mode)) { | |
943 | _cleanup_free_ char *path = NULL; | |
944 | ||
945 | if (node->major == (unsigned) -1 || node->minor == (unsigned) -1) { | |
946 | json_log(e, flags, 0, "Major/minor required when device node is device node"); | |
947 | r = -EINVAL; | |
948 | goto fail_element; | |
949 | } | |
950 | ||
951 | /* Suppress a couple of implicit device nodes */ | |
952 | r = device_path_make_canonical(node->mode, makedev(node->major, node->minor), &path); | |
953 | if (r < 0) | |
954 | json_log(e, flags|JSON_DEBUG, 0, "Failed to resolve device node %u:%u, ignoring: %m", node->major, node->minor); | |
955 | else { | |
956 | if (PATH_IN_SET(path, | |
957 | "/dev/null", | |
958 | "/dev/zero", | |
959 | "/dev/full", | |
960 | "/dev/random", | |
961 | "/dev/urandom", | |
962 | "/dev/tty", | |
963 | "/dev/net/tun", | |
964 | "/dev/ptmx", | |
965 | "/dev/pts/ptmx", | |
966 | "/dev/console")) { | |
967 | ||
968 | json_log(e, flags|JSON_DEBUG, 0, "Ignoring devices item for device '%s', as it is implicitly created anyway.", path); | |
969 | free(node->path); | |
970 | continue; | |
971 | } | |
972 | } | |
973 | } | |
974 | ||
975 | s->n_extra_nodes++; | |
976 | continue; | |
977 | ||
978 | fail_element: | |
979 | free(node->path); | |
980 | return r; | |
981 | } | |
982 | ||
983 | return 0; | |
984 | } | |
985 | ||
986 | static int oci_cgroups_path(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
987 | _cleanup_free_ char *slice = NULL, *backwards = NULL; | |
988 | Settings *s = userdata; | |
989 | const char *p; | |
990 | int r; | |
991 | ||
992 | assert(s); | |
993 | ||
994 | assert_se(p = json_variant_string(v)); | |
995 | ||
996 | r = cg_path_get_slice(p, &slice); | |
997 | if (r < 0) | |
998 | return json_log(v, flags, r, "Couldn't derive slice unit name from path '%s': %m", p); | |
999 | ||
1000 | r = cg_slice_to_path(slice, &backwards); | |
1001 | if (r < 0) | |
1002 | return json_log(v, flags, r, "Couldn't convert slice unit name '%s' back to path: %m", slice); | |
1003 | ||
1004 | if (!path_equal(backwards, p)) { | |
1005 | json_log(v, flags, 0, "Control group path '%s' does not refer to slice unit, refusing.", p); | |
1006 | return -EINVAL; | |
1007 | } | |
1008 | ||
1009 | free_and_replace(s->slice, slice); | |
1010 | return 0; | |
1011 | } | |
1012 | ||
1013 | static int oci_cgroup_device_type(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
1014 | mode_t *mode = userdata; | |
1015 | const char *n; | |
1016 | ||
1017 | assert_se(n = json_variant_string(v)); | |
1018 | ||
1019 | if (streq(n, "c")) | |
1020 | *mode = S_IFCHR; | |
1021 | else if (streq(n, "b")) | |
1022 | *mode = S_IFBLK; | |
1023 | else { | |
1024 | json_log(v, flags, 0, "Control group device type unknown: %s", n); | |
1025 | return -EINVAL; | |
1026 | } | |
1027 | ||
1028 | return 0; | |
1029 | } | |
1030 | ||
1031 | struct device_data { | |
1032 | bool allow; | |
1033 | bool r; | |
1034 | bool w; | |
1035 | bool m; | |
1036 | mode_t type; | |
1037 | unsigned major; | |
1038 | unsigned minor; | |
1039 | }; | |
1040 | ||
1041 | static int oci_cgroup_device_access(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
1042 | struct device_data *d = userdata; | |
1043 | bool r = false, w = false, m = false; | |
1044 | const char *s; | |
1045 | size_t i; | |
1046 | ||
1047 | assert_se(s = json_variant_string(v)); | |
1048 | ||
1049 | for (i = 0; s[i]; i++) { | |
1050 | if (s[i] == 'r') | |
1051 | r = true; | |
1052 | else if (s[i] == 'w') | |
1053 | w = true; | |
1054 | else if (s[i] == 'm') | |
1055 | m = true; | |
1056 | else { | |
1057 | json_log(v, flags, 0, "Unknown device access character '%c'.", s[i]); | |
1058 | return -EINVAL; | |
1059 | } | |
1060 | } | |
1061 | ||
1062 | d->r = r; | |
1063 | d->w = w; | |
1064 | d->m = m; | |
1065 | ||
1066 | return 0; | |
1067 | } | |
1068 | ||
1069 | static int oci_cgroup_devices(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
1070 | ||
1071 | _cleanup_free_ struct device_data *list = NULL; | |
1072 | Settings *s = userdata; | |
1073 | size_t n_list = 0, i; | |
1074 | bool noop = false; | |
1075 | JsonVariant *e; | |
1076 | int r; | |
1077 | ||
1078 | assert(s); | |
1079 | ||
1080 | JSON_VARIANT_ARRAY_FOREACH(e, v) { | |
1081 | ||
1082 | struct device_data data = { | |
1083 | .major = (unsigned) -1, | |
1084 | .minor = (unsigned) -1, | |
1085 | }, *a; | |
1086 | ||
1087 | static const JsonDispatch table[] = { | |
1088 | { "allow", JSON_VARIANT_BOOLEAN, json_dispatch_boolean, offsetof(struct device_data, allow), JSON_MANDATORY }, | |
1089 | { "type", JSON_VARIANT_STRING, oci_cgroup_device_type, offsetof(struct device_data, type), 0 }, | |
1090 | { "major", JSON_VARIANT_UNSIGNED, oci_device_major, offsetof(struct device_data, major), 0 }, | |
1091 | { "minor", JSON_VARIANT_UNSIGNED, oci_device_minor, offsetof(struct device_data, minor), 0 }, | |
1092 | { "access", JSON_VARIANT_STRING, oci_cgroup_device_access, 0, 0 }, | |
1093 | {} | |
1094 | }; | |
1095 | ||
1096 | r = json_dispatch(e, table, oci_unexpected, flags, &data); | |
1097 | if (r < 0) | |
1098 | return r; | |
1099 | ||
1100 | if (!data.allow) { | |
1101 | /* The fact that OCI allows 'deny' entries makes really no sense, as 'allow' vs. 'deny' for the | |
1102 | * devices cgroup controller is really not about whitelisting and blacklisting but about adding | |
1103 | * and removing entries from the whitelist. Since we always start out with an empty whitelist | |
1104 | * we hence ignore the whole thing, as removing entries which don't exist make no sense. We'll | |
1105 | * log about this, since this is really borked in the spec, with one exception: the entry | |
1106 | * that's supposed to drop the kernel's default we ignore silently */ | |
1107 | ||
1108 | if (!data.r || !data.w || !data.m || data.type != 0 || data.major != (unsigned) -1 || data.minor != (unsigned) -1) | |
1109 | json_log(v, flags|JSON_WARNING, 0, "Devices cgroup whitelist with arbitrary 'allow' entries not supported, ignoring."); | |
1110 | ||
1111 | /* We ignore the 'deny' entry as for us that's implied */ | |
1112 | continue; | |
1113 | } | |
1114 | ||
1115 | if (!data.r && !data.w && !data.m) { | |
1116 | json_log(v, flags|LOG_WARNING, 0, "Device cgroup whitelist entry with no effect found, ignoring."); | |
1117 | continue; | |
1118 | } | |
1119 | ||
1120 | if (data.minor != (unsigned) -1 && data.major == (unsigned) -1) { | |
1121 | json_log(v, flags, 0, "Device cgroup whitelist entries with minors but no majors not supported."); | |
1122 | return -EOPNOTSUPP; | |
1123 | } | |
1124 | ||
1125 | if (data.major != (unsigned) -1 && data.type == 0) { | |
1126 | json_log(v, flags, 0, "Device cgroup whitelist entries with majors but no device node type not supported."); | |
1127 | return -EOPNOTSUPP; | |
1128 | } | |
1129 | ||
1130 | if (data.type == 0) { | |
1131 | if (data.r && data.w && data.m) /* a catchall whitelist entry means we are looking at a noop */ | |
1132 | noop = true; | |
1133 | else { | |
1134 | json_log(v, flags, 0, "Device cgroup whitelist entries with no type not supported."); | |
1135 | return -EOPNOTSUPP; | |
1136 | } | |
1137 | } | |
1138 | ||
1139 | a = reallocarray(list, n_list + 1, sizeof(struct device_data)); | |
1140 | if (!a) | |
1141 | return log_oom(); | |
1142 | ||
1143 | list = a; | |
1144 | list[n_list++] = data; | |
1145 | } | |
1146 | ||
1147 | if (noop) | |
1148 | return 0; | |
1149 | ||
1150 | r = settings_allocate_properties(s); | |
1151 | if (r < 0) | |
1152 | return r; | |
1153 | ||
1154 | r = sd_bus_message_open_container(s->properties, 'r', "sv"); | |
1155 | if (r < 0) | |
1156 | return bus_log_create_error(r); | |
1157 | ||
1158 | r = sd_bus_message_append(s->properties, "s", "DeviceAllow"); | |
1159 | if (r < 0) | |
1160 | return bus_log_create_error(r); | |
1161 | ||
1162 | r = sd_bus_message_open_container(s->properties, 'v', "a(ss)"); | |
1163 | if (r < 0) | |
1164 | return bus_log_create_error(r); | |
1165 | ||
1166 | r = sd_bus_message_open_container(s->properties, 'a', "(ss)"); | |
1167 | if (r < 0) | |
1168 | return bus_log_create_error(r); | |
1169 | ||
1170 | for (i = 0; i < n_list; i++) { | |
1171 | _cleanup_free_ char *pattern = NULL; | |
1172 | char access[4]; | |
1173 | size_t n = 0; | |
1174 | ||
1175 | if (list[i].minor == (unsigned) -1) { | |
1176 | const char *t; | |
1177 | ||
1178 | if (list[i].type == S_IFBLK) | |
1179 | t = "block"; | |
1180 | else { | |
1181 | assert(list[i].type == S_IFCHR); | |
1182 | t = "char"; | |
1183 | } | |
1184 | ||
1185 | if (list[i].major == (unsigned) -1) { | |
1186 | pattern = strjoin(t, "-*"); | |
1187 | if (!pattern) | |
1188 | return log_oom(); | |
1189 | } else { | |
1190 | if (asprintf(&pattern, "%s-%u", t, list[i].major) < 0) | |
1191 | return log_oom(); | |
1192 | } | |
1193 | ||
1194 | } else { | |
1195 | assert(list[i].major != (unsigned) -1); /* If a minor is specified, then a major also needs to be specified */ | |
1196 | ||
1197 | r = device_path_make_major_minor(list[i].type, makedev(list[i].major, list[i].minor), &pattern); | |
1198 | if (r < 0) | |
1199 | return log_oom(); | |
1200 | } | |
1201 | ||
1202 | if (list[i].r) | |
1203 | access[n++] = 'r'; | |
1204 | if (list[i].w) | |
1205 | access[n++] = 'w'; | |
1206 | if (list[i].m) | |
1207 | access[n++] = 'm'; | |
1208 | access[n] = 0; | |
1209 | ||
1210 | assert(n > 0); | |
1211 | ||
1212 | r = sd_bus_message_append(s->properties, "(ss)", pattern, access); | |
1213 | if (r < 0) | |
1214 | return bus_log_create_error(r); | |
1215 | } | |
1216 | ||
1217 | r = sd_bus_message_close_container(s->properties); | |
1218 | if (r < 0) | |
1219 | return bus_log_create_error(r); | |
1220 | ||
1221 | r = sd_bus_message_close_container(s->properties); | |
1222 | if (r < 0) | |
1223 | return bus_log_create_error(r); | |
1224 | ||
1225 | r = sd_bus_message_close_container(s->properties); | |
1226 | if (r < 0) | |
1227 | return bus_log_create_error(r); | |
1228 | ||
1229 | return 0; | |
1230 | } | |
1231 | ||
1232 | static int oci_cgroup_memory_limit(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
1233 | uint64_t *m = userdata; | |
1234 | uintmax_t k; | |
1235 | ||
1236 | assert(m); | |
1237 | ||
1238 | if (json_variant_is_negative(v)) { | |
1239 | *m = UINT64_MAX; | |
1240 | return 0; | |
1241 | } | |
1242 | ||
1243 | if (!json_variant_is_unsigned(v)) { | |
1244 | json_log(v, flags, 0, "Memory limit is not an unsigned integer"); | |
1245 | return -EINVAL; | |
1246 | } | |
1247 | ||
1248 | k = json_variant_unsigned(v); | |
1249 | if (k >= UINT64_MAX) { | |
1250 | json_log(v, flags, 0, "Memory limit too large: %ji", k); | |
1251 | return -ERANGE; | |
1252 | } | |
1253 | ||
1254 | *m = (uint64_t) k; | |
1255 | return 0; | |
1256 | } | |
1257 | ||
1258 | static int oci_cgroup_memory(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
1259 | ||
1260 | struct memory_data { | |
1261 | uint64_t limit; | |
1262 | uint64_t reservation; | |
1263 | uint64_t swap; | |
1264 | } data = { | |
1265 | .limit = UINT64_MAX, | |
1266 | .reservation = UINT64_MAX, | |
1267 | .swap = UINT64_MAX, | |
1268 | }; | |
1269 | ||
1270 | static const JsonDispatch table[] = { | |
1271 | { "limit", JSON_VARIANT_NUMBER, oci_cgroup_memory_limit, offsetof(struct memory_data, limit), 0 }, | |
1272 | { "reservation", JSON_VARIANT_NUMBER, oci_cgroup_memory_limit, offsetof(struct memory_data, reservation), 0 }, | |
1273 | { "swap", JSON_VARIANT_NUMBER, oci_cgroup_memory_limit, offsetof(struct memory_data, swap), 0 }, | |
1274 | { "kernel", JSON_VARIANT_NUMBER, oci_unsupported, 0, JSON_PERMISSIVE }, | |
1275 | { "kernelTCP", JSON_VARIANT_NUMBER, oci_unsupported, 0, JSON_PERMISSIVE }, | |
1276 | { "swapiness", JSON_VARIANT_NUMBER, oci_unsupported, 0, JSON_PERMISSIVE }, | |
1277 | { "disableOOMKiller", JSON_VARIANT_NUMBER, oci_unsupported, 0, JSON_PERMISSIVE }, | |
1278 | {} | |
1279 | }; | |
1280 | ||
1281 | Settings *s = userdata; | |
1282 | int r; | |
1283 | ||
1284 | r = json_dispatch(v, table, oci_unexpected, flags, &data); | |
1285 | if (r < 0) | |
1286 | return r; | |
1287 | ||
1288 | if (data.swap != UINT64_MAX) { | |
1289 | if (data.limit == UINT64_MAX) | |
1290 | json_log(v, flags|LOG_WARNING, 0, "swap limit without memory limit is not supported, ignoring."); | |
1291 | else if (data.swap < data.limit) | |
1292 | json_log(v, flags|LOG_WARNING, 0, "swap limit is below memory limit, ignoring."); | |
1293 | else { | |
1294 | r = settings_allocate_properties(s); | |
1295 | if (r < 0) | |
1296 | return r; | |
1297 | ||
1298 | r = sd_bus_message_append(s->properties, "(sv)", "MemorySwapMax", "t", data.swap - data.limit); | |
1299 | if (r < 0) | |
1300 | return bus_log_create_error(r); | |
1301 | } | |
1302 | } | |
1303 | ||
1304 | if (data.limit != UINT64_MAX) { | |
1305 | r = settings_allocate_properties(s); | |
1306 | if (r < 0) | |
1307 | return r; | |
1308 | ||
1309 | r = sd_bus_message_append(s->properties, "(sv)", "MemoryMax", "t", data.limit); | |
1310 | if (r < 0) | |
1311 | return bus_log_create_error(r); | |
1312 | } | |
1313 | ||
1314 | if (data.reservation != UINT64_MAX) { | |
1315 | r = settings_allocate_properties(s); | |
1316 | if (r < 0) | |
1317 | return r; | |
1318 | ||
1319 | r = sd_bus_message_append(s->properties, "(sv)", "MemoryLow", "t", data.reservation); | |
1320 | if (r < 0) | |
1321 | return bus_log_create_error(r); | |
1322 | } | |
1323 | ||
1324 | return 0; | |
1325 | } | |
1326 | ||
1327 | struct cpu_data { | |
1328 | uint64_t shares; | |
1329 | uint64_t quota; | |
1330 | uint64_t period; | |
1331 | cpu_set_t *cpuset; | |
1332 | unsigned ncpus; | |
1333 | }; | |
1334 | ||
1335 | static int oci_cgroup_cpu_shares(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
1336 | uint64_t *u = userdata; | |
1337 | uintmax_t k; | |
1338 | ||
1339 | assert(u); | |
1340 | ||
1341 | k = json_variant_unsigned(v); | |
1342 | if (k < CGROUP_CPU_SHARES_MIN || k > CGROUP_CPU_SHARES_MAX) { | |
1343 | json_log(v, flags, 0, "shares value out of range."); | |
1344 | return -ERANGE; | |
1345 | } | |
1346 | ||
1347 | *u = (uint64_t) k; | |
1348 | return 0; | |
1349 | } | |
1350 | ||
1351 | static int oci_cgroup_cpu_quota(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
1352 | uint64_t *u = userdata; | |
1353 | uintmax_t k; | |
1354 | ||
1355 | assert(u); | |
1356 | ||
1357 | k = json_variant_unsigned(v); | |
1358 | if (k <= 0 || k >= UINT64_MAX) { | |
1359 | json_log(v, flags, 0, "period/quota value out of range."); | |
1360 | return -ERANGE; | |
1361 | } | |
1362 | ||
1363 | *u = (uint64_t) k; | |
1364 | return 0; | |
1365 | } | |
1366 | ||
1367 | static int oci_cgroup_cpu_cpus(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
1368 | struct cpu_data *data = userdata; | |
1369 | cpu_set_t *set; | |
1370 | const char *n; | |
1371 | int ncpus; | |
1372 | ||
1373 | assert(data); | |
1374 | ||
1375 | assert_se(n = json_variant_string(v)); | |
1376 | ||
1377 | ncpus = parse_cpu_set(n, &set); | |
1378 | if (ncpus < 0) | |
1379 | return json_log(v, flags, ncpus, "Failed to parse CPU set specification: %s", n); | |
1380 | ||
1381 | CPU_FREE(data->cpuset); | |
1382 | data->cpuset = set; | |
1383 | data->ncpus = ncpus; | |
1384 | ||
1385 | return 0; | |
1386 | } | |
1387 | ||
1388 | static int oci_cgroup_cpu(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
1389 | ||
1390 | static const JsonDispatch table[] = { | |
1391 | { "shares", JSON_VARIANT_UNSIGNED, oci_cgroup_cpu_shares, offsetof(struct cpu_data, shares), 0 }, | |
1392 | { "quota", JSON_VARIANT_UNSIGNED, oci_cgroup_cpu_quota, offsetof(struct cpu_data, quota), 0 }, | |
1393 | { "period", JSON_VARIANT_UNSIGNED, oci_cgroup_cpu_quota, offsetof(struct cpu_data, period), 0 }, | |
1394 | { "realtimeRuntime", JSON_VARIANT_UNSIGNED, oci_unsupported, 0, 0 }, | |
1395 | { "realtimePeriod", JSON_VARIANT_UNSIGNED, oci_unsupported, 0, 0 }, | |
1396 | { "cpus", JSON_VARIANT_STRING, oci_cgroup_cpu_cpus, 0, 0 }, | |
1397 | { "mems", JSON_VARIANT_STRING, oci_unsupported, 0, 0 }, | |
1398 | {} | |
1399 | }; | |
1400 | ||
1401 | struct cpu_data data = { | |
1402 | .shares = UINT64_MAX, | |
1403 | .quota = UINT64_MAX, | |
1404 | .period = UINT64_MAX, | |
1405 | }; | |
1406 | ||
1407 | Settings *s = userdata; | |
1408 | int r; | |
1409 | ||
1410 | r = json_dispatch(v, table, oci_unexpected, flags, &data); | |
1411 | if (r < 0) { | |
1412 | CPU_FREE(data.cpuset); | |
1413 | return r; | |
1414 | } | |
1415 | ||
1416 | CPU_FREE(s->cpuset); | |
1417 | s->cpuset = data.cpuset; | |
1418 | s->cpuset_ncpus = data.ncpus; | |
1419 | ||
1420 | if (data.shares != UINT64_MAX) { | |
1421 | r = settings_allocate_properties(s); | |
1422 | if (r < 0) | |
1423 | return r; | |
1424 | ||
1425 | r = sd_bus_message_append(s->properties, "(sv)", "CPUShares", "t", data.shares); | |
1426 | if (r < 0) | |
1427 | return bus_log_create_error(r); | |
1428 | } | |
1429 | ||
1430 | if (data.quota != UINT64_MAX && data.period != UINT64_MAX) { | |
1431 | r = settings_allocate_properties(s); | |
1432 | if (r < 0) | |
1433 | return r; | |
1434 | ||
1435 | r = sd_bus_message_append(s->properties, "(sv)", "CPUQuotaPerSecUSec", "t", (uint64_t) (data.quota * USEC_PER_SEC / data.period)); | |
1436 | if (r < 0) | |
1437 | return bus_log_create_error(r); | |
1438 | ||
1439 | } else if ((data.quota != UINT64_MAX) != (data.period != UINT64_MAX)) { | |
1440 | json_log(v, flags, 0, "CPU quota and period not used together."); | |
1441 | return -EINVAL; | |
1442 | } | |
1443 | ||
1444 | return 0; | |
1445 | } | |
1446 | ||
1447 | static int oci_cgroup_block_io_weight(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
1448 | Settings *s = userdata; | |
1449 | uintmax_t k; | |
1450 | int r; | |
1451 | ||
1452 | assert(s); | |
1453 | ||
1454 | k = json_variant_unsigned(v); | |
1455 | if (k < CGROUP_BLKIO_WEIGHT_MIN || k > CGROUP_BLKIO_WEIGHT_MAX) { | |
1456 | json_log(v, flags, 0, "Block I/O weight out of range."); | |
1457 | return -ERANGE; | |
1458 | } | |
1459 | ||
1460 | r = settings_allocate_properties(s); | |
1461 | if (r < 0) | |
1462 | return r; | |
1463 | ||
1464 | r = sd_bus_message_append(s->properties, "(sv)", "BlockIOWeight", "t", (uint64_t) k); | |
1465 | if (r < 0) | |
1466 | return bus_log_create_error(r); | |
1467 | ||
1468 | return 0; | |
1469 | } | |
1470 | ||
1471 | static int oci_cgroup_block_io_weight_device(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
1472 | Settings *s = userdata; | |
1473 | JsonVariant *e; | |
1474 | int r; | |
1475 | ||
1476 | assert(s); | |
1477 | ||
1478 | JSON_VARIANT_ARRAY_FOREACH(e, v) { | |
1479 | struct device_data { | |
1480 | unsigned major; | |
1481 | unsigned minor; | |
1482 | uintmax_t weight; | |
1483 | } data = { | |
1484 | .major = (unsigned) -1, | |
1485 | .minor = (unsigned) -1, | |
1486 | .weight = UINTMAX_MAX, | |
1487 | }; | |
1488 | ||
1489 | static const JsonDispatch table[] = { | |
1490 | { "major", JSON_VARIANT_UNSIGNED, oci_device_major, offsetof(struct device_data, major), JSON_MANDATORY }, | |
1491 | { "minor", JSON_VARIANT_UNSIGNED, oci_device_minor, offsetof(struct device_data, minor), JSON_MANDATORY }, | |
1492 | { "weight", JSON_VARIANT_UNSIGNED, json_dispatch_unsigned, offsetof(struct device_data, weight), 0 }, | |
1493 | { "leafWeight", JSON_VARIANT_INTEGER, oci_unsupported, 0, JSON_PERMISSIVE }, | |
1494 | {} | |
1495 | }; | |
1496 | ||
1497 | _cleanup_free_ char *path = NULL; | |
1498 | ||
1499 | r = json_dispatch(e, table, oci_unexpected, flags, &data); | |
1500 | if (r < 0) | |
1501 | return r; | |
1502 | ||
1503 | if (data.weight == UINTMAX_MAX) | |
1504 | continue; | |
1505 | ||
1506 | if (data.weight < CGROUP_BLKIO_WEIGHT_MIN || data.weight > CGROUP_BLKIO_WEIGHT_MAX) { | |
1507 | json_log(v, flags, 0, "Block I/O device weight out of range."); | |
1508 | return -ERANGE; | |
1509 | } | |
1510 | ||
1511 | r = device_path_make_major_minor(S_IFBLK, makedev(data.major, data.minor), &path); | |
1512 | if (r < 0) | |
1513 | return json_log(v, flags, r, "Failed to build device path: %m"); | |
1514 | ||
1515 | r = settings_allocate_properties(s); | |
1516 | if (r < 0) | |
1517 | return r; | |
1518 | ||
1519 | r = sd_bus_message_append(s->properties, "(sv)", "BlockIODeviceWeight", "a(st)", 1, path, (uint64_t) data.weight); | |
1520 | if (r < 0) | |
1521 | return bus_log_create_error(r); | |
1522 | } | |
1523 | ||
1524 | return 0; | |
1525 | } | |
1526 | ||
1527 | static int oci_cgroup_block_io_throttle(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
1528 | Settings *s = userdata; | |
1529 | const char *pname; | |
1530 | JsonVariant *e; | |
1531 | int r; | |
1532 | ||
1533 | assert(s); | |
1534 | ||
1535 | pname = streq(name, "throttleReadBpsDevice") ? "IOReadBandwidthMax" : | |
1536 | streq(name, "throttleWriteBpsDevice") ? "IOWriteBandwidthMax" : | |
1537 | streq(name, "throttleReadIOPSDevice") ? "IOReadIOPSMax" : | |
1538 | "IOWriteIOPSMax"; | |
1539 | ||
1540 | JSON_VARIANT_ARRAY_FOREACH(e, v) { | |
1541 | struct device_data { | |
1542 | unsigned major; | |
1543 | unsigned minor; | |
1544 | uintmax_t rate; | |
1545 | } data = { | |
1546 | .major = (unsigned) -1, | |
1547 | .minor = (unsigned) -1, | |
1548 | }; | |
1549 | ||
1550 | static const JsonDispatch table[] = { | |
1551 | { "major", JSON_VARIANT_UNSIGNED, oci_device_major, offsetof(struct device_data, major), JSON_MANDATORY }, | |
1552 | { "minor", JSON_VARIANT_UNSIGNED, oci_device_minor, offsetof(struct device_data, minor), JSON_MANDATORY }, | |
1553 | { "rate", JSON_VARIANT_UNSIGNED, json_dispatch_unsigned, offsetof(struct device_data, rate), JSON_MANDATORY }, | |
1554 | {} | |
1555 | }; | |
1556 | ||
1557 | _cleanup_free_ char *path = NULL; | |
1558 | ||
1559 | r = json_dispatch(e, table, oci_unexpected, flags, &data); | |
1560 | if (r < 0) | |
1561 | return r; | |
1562 | ||
1563 | if (data.rate >= UINT64_MAX) { | |
1564 | json_log(v, flags, 0, "Block I/O device rate out of range."); | |
1565 | return -ERANGE; | |
1566 | } | |
1567 | ||
1568 | r = device_path_make_major_minor(S_IFBLK, makedev(data.major, data.minor), &path); | |
1569 | if (r < 0) | |
1570 | return json_log(v, flags, r, "Failed to build device path: %m"); | |
1571 | ||
1572 | r = settings_allocate_properties(s); | |
1573 | if (r < 0) | |
1574 | return r; | |
1575 | ||
1576 | r = sd_bus_message_append(s->properties, "(sv)", pname, "a(st)", 1, path, (uint64_t) data.rate); | |
1577 | if (r < 0) | |
1578 | return bus_log_create_error(r); | |
1579 | } | |
1580 | ||
1581 | return 0; | |
1582 | } | |
1583 | ||
1584 | static int oci_cgroup_block_io(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
1585 | ||
1586 | static const JsonDispatch table[] = { | |
1587 | { "weight", JSON_VARIANT_UNSIGNED, oci_cgroup_block_io_weight, 0, 0 }, | |
1588 | { "leafWeight", JSON_VARIANT_UNSIGNED, oci_unsupported, 0, JSON_PERMISSIVE }, | |
1589 | { "weightDevice", JSON_VARIANT_ARRAY, oci_cgroup_block_io_weight_device, 0, 0 }, | |
1590 | { "throttleReadBpsDevice", JSON_VARIANT_ARRAY, oci_cgroup_block_io_throttle, 0, 0 }, | |
1591 | { "throttleWriteBpsDevice", JSON_VARIANT_ARRAY, oci_cgroup_block_io_throttle, 0, 0 }, | |
1592 | { "throttleReadIOPSDevice", JSON_VARIANT_ARRAY, oci_cgroup_block_io_throttle, 0, 0 }, | |
1593 | { "throttleWriteIOPSDevice", JSON_VARIANT_ARRAY, oci_cgroup_block_io_throttle, 0, 0 }, | |
1594 | {} | |
1595 | }; | |
1596 | ||
1597 | return json_dispatch(v, table, oci_unexpected, flags, userdata); | |
1598 | } | |
1599 | ||
1600 | static int oci_cgroup_pids(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
1601 | ||
1602 | static const JsonDispatch table[] = { | |
1603 | { "limit", JSON_VARIANT_NUMBER, json_dispatch_variant, 0, JSON_MANDATORY }, | |
1604 | {} | |
1605 | }; | |
1606 | ||
1607 | _cleanup_(json_variant_unrefp) JsonVariant *k = NULL; | |
1608 | Settings *s = userdata; | |
1609 | uint64_t m; | |
1610 | int r; | |
1611 | ||
1612 | assert(s); | |
1613 | ||
1614 | r = json_dispatch(v, table, oci_unexpected, flags, &k); | |
1615 | if (r < 0) | |
1616 | return r; | |
1617 | ||
1618 | if (json_variant_is_negative(k)) | |
1619 | m = UINT64_MAX; | |
1620 | else { | |
1621 | if (!json_variant_is_unsigned(k)) { | |
1622 | json_log(k, flags, 0, "pids limit not unsigned integer, refusing."); | |
1623 | return -EINVAL; | |
1624 | } | |
1625 | ||
1626 | m = (uint64_t) json_variant_unsigned(k); | |
1627 | ||
1628 | if ((uintmax_t) m != json_variant_unsigned(k)) { | |
1629 | json_log(v, flags, 0, "pids limit out of range, refusing."); | |
1630 | return -EINVAL; | |
1631 | } | |
1632 | } | |
1633 | ||
1634 | r = settings_allocate_properties(s); | |
1635 | if (r < 0) | |
1636 | return r; | |
1637 | ||
1638 | r = sd_bus_message_append(s->properties, "(sv)", "TasksMax", "t", m); | |
1639 | if (r < 0) | |
1640 | return bus_log_create_error(r); | |
1641 | ||
1642 | return 0; | |
1643 | } | |
1644 | ||
1645 | static int oci_resources(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
1646 | ||
1647 | static const JsonDispatch table[] = { | |
1648 | { "devices", JSON_VARIANT_ARRAY, oci_cgroup_devices, 0, 0 }, | |
1649 | { "memory", JSON_VARIANT_OBJECT, oci_cgroup_memory, 0, 0 }, | |
1650 | { "cpu", JSON_VARIANT_OBJECT, oci_cgroup_cpu, 0, 0 }, | |
1651 | { "blockIO", JSON_VARIANT_OBJECT, oci_cgroup_block_io, 0, 0 }, | |
1652 | { "hugepageLimits", JSON_VARIANT_ARRAY, oci_unsupported, 0, 0 }, | |
1653 | { "network", JSON_VARIANT_OBJECT, oci_unsupported, 0, 0 }, | |
1654 | { "pids", JSON_VARIANT_OBJECT, oci_cgroup_pids, 0, 0 }, | |
1655 | { "rdma", JSON_VARIANT_OBJECT, oci_unsupported, 0, 0 }, | |
1656 | {} | |
1657 | }; | |
1658 | ||
1659 | return json_dispatch(v, table, oci_unexpected, flags, userdata); | |
1660 | } | |
1661 | ||
1662 | static bool sysctl_key_valid(const char *s) { | |
1663 | bool dot = true; | |
1664 | ||
1665 | /* Note that we are a bit stricter here than in systemd-sysctl, as that inherited semantics from the old sysctl | |
1666 | * tool, which were really weird (as it swaps / and . in both ways) */ | |
1667 | ||
1668 | if (isempty(s)) | |
1669 | return false; | |
1670 | ||
1671 | for (; *s; s++) { | |
1672 | ||
1673 | if (*s <= ' ' || *s >= 127) | |
1674 | return false; | |
1675 | if (*s == '/') | |
1676 | return false; | |
1677 | if (*s == '.') { | |
1678 | ||
1679 | if (dot) /* Don't allow two dots next to each other (or at the beginning) */ | |
1680 | return false; | |
1681 | ||
1682 | dot = true; | |
1683 | } else | |
1684 | dot = false; | |
1685 | } | |
1686 | ||
1687 | if (dot) /* don't allow a dot at the end */ | |
1688 | return false; | |
1689 | ||
1690 | return true; | |
1691 | } | |
1692 | ||
1693 | static int oci_sysctl(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
1694 | Settings *s = userdata; | |
1695 | JsonVariant *k, *w; | |
1696 | int r; | |
1697 | ||
1698 | assert(s); | |
1699 | ||
1700 | JSON_VARIANT_OBJECT_FOREACH(k, w, v) { | |
1701 | const char *n, *m; | |
1702 | ||
1703 | if (!json_variant_is_string(w)) { | |
1704 | json_log(v, flags, 0, "sysctl parameter is not a string, refusing."); | |
1705 | return -EINVAL; | |
1706 | } | |
1707 | ||
1708 | assert_se(n = json_variant_string(k)); | |
1709 | assert_se(m = json_variant_string(w)); | |
1710 | ||
1711 | if (sysctl_key_valid(n)) { | |
1712 | json_log(v, flags, 0, "sysctl key invalid, refusing: %s", n); | |
1713 | return -EINVAL; | |
1714 | } | |
1715 | ||
1716 | r = strv_extend_strv(&s->sysctl, STRV_MAKE(n, m), false); | |
1717 | if (r < 0) | |
1718 | return log_oom(); | |
1719 | } | |
1720 | ||
1721 | return 0; | |
1722 | } | |
1723 | ||
1724 | static int oci_seccomp_action_from_string(const char *name, uint32_t *ret) { | |
1725 | ||
1726 | static const struct { | |
1727 | const char *name; | |
1728 | uint32_t action; | |
1729 | } table[] = { | |
1730 | { "SCMP_ACT_ALLOW", SCMP_ACT_ALLOW }, | |
1731 | { "SCMP_ACT_ERRNO", SCMP_ACT_ERRNO(EPERM) }, /* the OCI spec doesn't document the error, but it appears EPERM is supposed to be used */ | |
1732 | { "SCMP_ACT_KILL", SCMP_ACT_KILL }, | |
1733 | #ifdef SCMP_ACT_LOG | |
1734 | { "SCMP_ACT_LOG", SCMP_ACT_LOG }, | |
1735 | #endif | |
1736 | { "SCMP_ACT_TRAP", SCMP_ACT_TRAP }, | |
1737 | ||
1738 | /* We don't support SCMP_ACT_TRACE because that requires a tracer, and that doesn't really make sense | |
1739 | * here */ | |
1740 | }; | |
1741 | ||
1742 | size_t i; | |
1743 | ||
1744 | for (i = 0; i < ELEMENTSOF(table); i++) | |
1745 | if (streq_ptr(name, table[i].name)) { | |
1746 | *ret = table[i].action; | |
1747 | return 0; | |
1748 | } | |
1749 | ||
1750 | return -EINVAL; | |
1751 | } | |
1752 | ||
1753 | static int oci_seccomp_arch_from_string(const char *name, uint32_t *ret) { | |
1754 | ||
1755 | static const struct { | |
1756 | const char *name; | |
1757 | uint32_t arch; | |
1758 | } table[] = { | |
1759 | { "SCMP_ARCH_AARCH64", SCMP_ARCH_AARCH64 }, | |
1760 | { "SCMP_ARCH_ARM", SCMP_ARCH_ARM }, | |
1761 | { "SCMP_ARCH_MIPS", SCMP_ARCH_MIPS }, | |
1762 | { "SCMP_ARCH_MIPS64", SCMP_ARCH_MIPS64 }, | |
1763 | { "SCMP_ARCH_MIPS64N32", SCMP_ARCH_MIPS64N32 }, | |
1764 | { "SCMP_ARCH_MIPSEL", SCMP_ARCH_MIPSEL }, | |
1765 | { "SCMP_ARCH_MIPSEL64", SCMP_ARCH_MIPSEL64 }, | |
1766 | { "SCMP_ARCH_MIPSEL64N32", SCMP_ARCH_MIPSEL64N32 }, | |
1767 | { "SCMP_ARCH_NATIVE", SCMP_ARCH_NATIVE }, | |
1768 | #ifdef SCMP_ARCH_PARISC | |
1769 | { "SCMP_ARCH_PARISC", SCMP_ARCH_PARISC }, | |
1770 | #endif | |
1771 | #ifdef SCMP_ARCH_PARISC64 | |
1772 | { "SCMP_ARCH_PARISC64", SCMP_ARCH_PARISC64 }, | |
1773 | #endif | |
1774 | { "SCMP_ARCH_PPC", SCMP_ARCH_PPC }, | |
1775 | { "SCMP_ARCH_PPC64", SCMP_ARCH_PPC64 }, | |
1776 | { "SCMP_ARCH_PPC64LE", SCMP_ARCH_PPC64LE }, | |
1777 | { "SCMP_ARCH_S390", SCMP_ARCH_S390 }, | |
1778 | { "SCMP_ARCH_S390X", SCMP_ARCH_S390X }, | |
1779 | { "SCMP_ARCH_X32", SCMP_ARCH_X32 }, | |
1780 | { "SCMP_ARCH_X86", SCMP_ARCH_X86 }, | |
1781 | { "SCMP_ARCH_X86_64", SCMP_ARCH_X86_64 }, | |
1782 | }; | |
1783 | ||
1784 | size_t i; | |
1785 | ||
1786 | for (i = 0; i < ELEMENTSOF(table); i++) | |
1787 | if (streq_ptr(table[i].name, name)) { | |
1788 | *ret = table[i].arch; | |
1789 | return 0; | |
1790 | } | |
1791 | ||
1792 | return -EINVAL; | |
1793 | } | |
1794 | ||
1795 | static int oci_seccomp_compare_from_string(const char *name, enum scmp_compare *ret) { | |
1796 | ||
1797 | static const struct { | |
1798 | const char *name; | |
1799 | enum scmp_compare op; | |
1800 | } table[] = { | |
1801 | { "SCMP_CMP_NE", SCMP_CMP_NE }, | |
1802 | { "SCMP_CMP_LT", SCMP_CMP_LT }, | |
1803 | { "SCMP_CMP_LE", SCMP_CMP_LE }, | |
1804 | { "SCMP_CMP_EQ", SCMP_CMP_EQ }, | |
1805 | { "SCMP_CMP_GE", SCMP_CMP_GE }, | |
1806 | { "SCMP_CMP_GT", SCMP_CMP_GT }, | |
1807 | { "SCMP_CMP_MASKED_EQ", SCMP_CMP_MASKED_EQ }, | |
1808 | }; | |
1809 | ||
1810 | size_t i; | |
1811 | ||
1812 | for (i = 0; i < ELEMENTSOF(table); i++) | |
1813 | if (streq_ptr(table[i].name, name)) { | |
1814 | *ret = table[i].op; | |
1815 | return 0; | |
1816 | } | |
1817 | ||
1818 | return -EINVAL; | |
1819 | } | |
1820 | ||
1821 | static int oci_seccomp_archs(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
1822 | scmp_filter_ctx *sc = userdata; | |
1823 | JsonVariant *e; | |
1824 | int r; | |
1825 | ||
1826 | assert(sc); | |
1827 | ||
1828 | JSON_VARIANT_ARRAY_FOREACH(e, v) { | |
1829 | uint32_t a; | |
1830 | ||
1831 | if (!json_variant_is_string(e)) { | |
1832 | json_log(e, flags, 0, "Architecture entry is not a string"); | |
1833 | return -EINVAL; | |
1834 | } | |
1835 | ||
1836 | r = oci_seccomp_arch_from_string(json_variant_string(e), &a); | |
1837 | if (r < 0) | |
1838 | return json_log(e, flags, r, "Unknown architecture: %s", json_variant_string(e)); | |
1839 | ||
1840 | r = seccomp_arch_add(sc, a); | |
1841 | if (r == -EEXIST) | |
1842 | continue; | |
1843 | if (r < 0) | |
1844 | return json_log(e, flags, r, "Failed to add architecture to seccomp filter: %m"); | |
1845 | } | |
1846 | ||
1847 | return 0; | |
1848 | } | |
1849 | ||
1850 | struct syscall_rule { | |
1851 | char **names; | |
1852 | uint32_t action; | |
1853 | struct scmp_arg_cmp *arguments; | |
1854 | size_t n_arguments; | |
1855 | }; | |
1856 | ||
1857 | static void syscall_rule_free(struct syscall_rule *rule) { | |
1858 | assert(rule); | |
1859 | ||
1860 | strv_free(rule->names); | |
1861 | free(rule->arguments); | |
1862 | }; | |
1863 | ||
1864 | static int oci_seccomp_action(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
1865 | uint32_t *action = userdata; | |
1866 | int r; | |
1867 | ||
1868 | assert(action); | |
1869 | ||
1870 | r = oci_seccomp_action_from_string(json_variant_string(v), action); | |
1871 | if (r < 0) | |
1872 | return json_log(v, flags, r, "Unknown system call action '%s': %m", json_variant_string(v)); | |
1873 | ||
1874 | return 0; | |
1875 | } | |
1876 | ||
1877 | static int oci_seccomp_op(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
1878 | enum scmp_compare *op = userdata; | |
1879 | int r; | |
1880 | ||
1881 | assert(op); | |
1882 | ||
1883 | r = oci_seccomp_compare_from_string(json_variant_string(v), op); | |
1884 | if (r < 0) | |
1885 | return json_log(v, flags, r, "Unknown seccomp operator '%s': %m", json_variant_string(v)); | |
1886 | ||
1887 | return 0; | |
1888 | } | |
1889 | ||
1890 | static int oci_seccomp_args(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
1891 | struct syscall_rule *rule = userdata; | |
1892 | JsonVariant *e; | |
1893 | int r; | |
1894 | ||
1895 | assert(rule); | |
1896 | ||
1897 | JSON_VARIANT_ARRAY_FOREACH(e, v) { | |
1898 | static const struct JsonDispatch table[] = { | |
1899 | { "index", JSON_VARIANT_UNSIGNED, json_dispatch_uint32, offsetof(struct scmp_arg_cmp, arg), JSON_MANDATORY }, | |
1900 | { "value", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(struct scmp_arg_cmp, datum_a), JSON_MANDATORY }, | |
1901 | { "valueTwo", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(struct scmp_arg_cmp, datum_b), 0 }, | |
1902 | { "op", JSON_VARIANT_STRING, oci_seccomp_op, offsetof(struct scmp_arg_cmp, op), JSON_MANDATORY }, | |
1903 | {}, | |
1904 | }; | |
1905 | ||
1906 | struct scmp_arg_cmp *a, *p; | |
1907 | int expected; | |
1908 | ||
1909 | a = reallocarray(rule->arguments, rule->n_arguments + 1, sizeof(struct syscall_rule)); | |
1910 | if (!a) | |
1911 | return log_oom(); | |
1912 | ||
1913 | rule->arguments = a; | |
1914 | p = rule->arguments + rule->n_arguments; | |
1915 | ||
1916 | *p = (struct scmp_arg_cmp) { | |
1917 | .arg = 0, | |
1918 | .datum_a = 0, | |
1919 | .datum_b = 0, | |
1920 | .op = 0, | |
1921 | }; | |
1922 | ||
1923 | r = json_dispatch(e, table, oci_unexpected, flags, p); | |
1924 | if (r < 0) | |
1925 | return r; | |
1926 | ||
1927 | expected = p->op == SCMP_CMP_MASKED_EQ ? 4 : 3; | |
1928 | if (r != expected) | |
1929 | json_log(e, flags|JSON_WARNING, 0, "Wrong number of system call arguments for JSON data data, ignoring."); | |
1930 | ||
1931 | /* Note that we are a bit sloppy here and do not insist that SCMP_CMP_MASKED_EQ gets two datum values, | |
1932 | * and the other only one. That's because buildah for example by default calls things with | |
1933 | * SCMP_CMP_MASKED_EQ but only one argument. We use 0 when the value is not specified. */ | |
1934 | ||
1935 | rule->n_arguments++; | |
1936 | } | |
1937 | ||
1938 | return 0; | |
1939 | } | |
1940 | ||
1941 | static int oci_seccomp_syscalls(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
1942 | scmp_filter_ctx *sc = userdata; | |
1943 | JsonVariant *e; | |
1944 | int r; | |
1945 | ||
1946 | assert(sc); | |
1947 | ||
1948 | JSON_VARIANT_ARRAY_FOREACH(e, v) { | |
1949 | static const JsonDispatch table[] = { | |
1950 | { "names", JSON_VARIANT_ARRAY, json_dispatch_strv, offsetof(struct syscall_rule, names), JSON_MANDATORY }, | |
1951 | { "action", JSON_VARIANT_STRING, oci_seccomp_action, offsetof(struct syscall_rule, action), JSON_MANDATORY }, | |
1952 | { "args", JSON_VARIANT_ARRAY, oci_seccomp_args, 0, 0 }, | |
1953 | }; | |
1954 | struct syscall_rule rule = { | |
1955 | .action = (uint32_t) -1, | |
1956 | }; | |
1957 | char **i; | |
1958 | ||
1959 | r = json_dispatch(e, table, oci_unexpected, flags, &rule); | |
1960 | if (r < 0) | |
1961 | goto fail_rule; | |
1962 | ||
1963 | if (strv_isempty(rule.names)) { | |
1964 | json_log(e, flags, 0, "System call name list is empty."); | |
1965 | r = -EINVAL; | |
1966 | goto fail_rule; | |
1967 | } | |
1968 | ||
1969 | STRV_FOREACH(i, rule.names) { | |
1970 | int nr; | |
1971 | ||
1972 | nr = seccomp_syscall_resolve_name(*i); | |
1973 | if (nr == __NR_SCMP_ERROR) { | |
1974 | log_debug("Unknown syscall %s, skipping.", *i); | |
1975 | continue; | |
1976 | } | |
1977 | ||
1978 | r = seccomp_rule_add_array(sc, rule.action, nr, rule.n_arguments, rule.arguments); | |
1979 | if (r < 0) | |
1980 | goto fail_rule; | |
1981 | } | |
1982 | ||
1983 | syscall_rule_free(&rule); | |
1984 | continue; | |
1985 | ||
1986 | fail_rule: | |
1987 | syscall_rule_free(&rule); | |
1988 | return r; | |
1989 | } | |
1990 | ||
1991 | return 0; | |
1992 | } | |
1993 | ||
1994 | static int oci_seccomp(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
1995 | ||
1996 | static const JsonDispatch table[] = { | |
1997 | { "defaultAction", JSON_VARIANT_STRING, NULL, 0, JSON_MANDATORY }, | |
1998 | { "architectures", JSON_VARIANT_ARRAY, oci_seccomp_archs, 0, 0 }, | |
1999 | { "syscalls", JSON_VARIANT_ARRAY, oci_seccomp_syscalls, 0, 0 }, | |
2000 | {} | |
2001 | }; | |
2002 | ||
2003 | _cleanup_(seccomp_releasep) scmp_filter_ctx sc = NULL; | |
2004 | Settings *s = userdata; | |
2005 | JsonVariant *def; | |
2006 | uint32_t d; | |
2007 | int r; | |
2008 | ||
2009 | assert(s); | |
2010 | ||
2011 | def = json_variant_by_key(v, "defaultAction"); | |
2012 | if (!def) { | |
2013 | json_log(v, flags, 0, "defaultAction element missing."); | |
2014 | return -EINVAL; | |
2015 | } | |
2016 | ||
2017 | if (!json_variant_is_string(def)) { | |
2018 | json_log(def, flags, 0, "defaultAction is not a string."); | |
2019 | return -EINVAL; | |
2020 | } | |
2021 | ||
2022 | r = oci_seccomp_action_from_string(json_variant_string(def), &d); | |
2023 | if (r < 0) | |
2024 | return json_log(def, flags, r, "Unknown default action: %s", json_variant_string(def)); | |
2025 | ||
2026 | sc = seccomp_init(d); | |
2027 | if (!sc) { | |
2028 | log_error("Couldn't allocate seccomp object."); | |
2029 | return -ENOMEM; | |
2030 | } | |
2031 | ||
2032 | r = json_dispatch(v, table, oci_unexpected, flags, sc); | |
2033 | if (r < 0) | |
2034 | return r; | |
2035 | ||
2036 | seccomp_release(s->seccomp); | |
2037 | s->seccomp = TAKE_PTR(sc); | |
2038 | ||
2039 | return 0; | |
2040 | } | |
2041 | ||
2042 | static int oci_rootfs_propagation(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
2043 | const char *s; | |
2044 | ||
2045 | s = json_variant_string(v); | |
2046 | ||
2047 | if (streq(s, "shared")) | |
2048 | return 0; | |
2049 | ||
2050 | json_log(v, flags|JSON_DEBUG, 0, "Ignoring rootfsPropagation setting '%s'.", s); | |
2051 | return 0; | |
2052 | } | |
2053 | ||
2054 | static int oci_masked_paths(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
2055 | Settings *s = userdata; | |
2056 | JsonVariant *e; | |
2057 | ||
2058 | assert(s); | |
2059 | ||
2060 | JSON_VARIANT_ARRAY_FOREACH(e, v) { | |
2061 | _cleanup_free_ char *destination = NULL; | |
2062 | CustomMount *m; | |
2063 | const char *p; | |
2064 | ||
2065 | if (!json_variant_is_string(e)) { | |
2066 | json_log(v, flags, 0, "Path is not a string, refusing."); | |
2067 | return -EINVAL; | |
2068 | } | |
2069 | ||
2070 | assert_se(p = json_variant_string(e)); | |
2071 | ||
2072 | if (!path_is_absolute(p)) { | |
2073 | json_log(v, flags, 0, "Path is not not absolute, refusing: %s", p); | |
2074 | return -EINVAL; | |
2075 | } | |
2076 | ||
2077 | if (oci_exclude_mount(p)) | |
2078 | continue; | |
2079 | ||
2080 | destination = strdup(p); | |
2081 | if (!destination) | |
2082 | return log_oom(); | |
2083 | ||
2084 | m = custom_mount_add(&s->custom_mounts, &s->n_custom_mounts, CUSTOM_MOUNT_INACCESSIBLE); | |
2085 | if (!m) | |
2086 | return log_oom(); | |
2087 | ||
2088 | m->destination = TAKE_PTR(destination); | |
2089 | ||
2090 | /* The spec doesn't say this, but apparently pre-existing implementations are lenient towards | |
2091 | * non-existing paths to mask. Let's hence be too. */ | |
2092 | m->graceful = true; | |
2093 | } | |
2094 | ||
2095 | return 0; | |
2096 | } | |
2097 | ||
2098 | static int oci_readonly_paths(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
2099 | Settings *s = userdata; | |
2100 | JsonVariant *e; | |
2101 | ||
2102 | assert(s); | |
2103 | ||
2104 | JSON_VARIANT_ARRAY_FOREACH(e, v) { | |
2105 | _cleanup_free_ char *source = NULL, *destination = NULL; | |
2106 | CustomMount *m; | |
2107 | const char *p; | |
2108 | ||
2109 | if (!json_variant_is_string(e)) { | |
2110 | json_log(v, flags, 0, "Path is not a string, refusing."); | |
2111 | return -EINVAL; | |
2112 | } | |
2113 | ||
2114 | assert_se(p = json_variant_string(e)); | |
2115 | ||
2116 | if (!path_is_absolute(p)) { | |
2117 | json_log(v, flags, 0, "Path is not not absolute, refusing: %s", p); | |
2118 | return -EINVAL; | |
2119 | } | |
2120 | ||
2121 | if (oci_exclude_mount(p)) | |
2122 | continue; | |
2123 | ||
2124 | source = strjoin("+", p); | |
2125 | if (!source) | |
2126 | return log_oom(); | |
2127 | ||
2128 | destination = strdup(p); | |
2129 | if (!destination) | |
2130 | return log_oom(); | |
2131 | ||
2132 | m = custom_mount_add(&s->custom_mounts, &s->n_custom_mounts, CUSTOM_MOUNT_BIND); | |
2133 | if (!m) | |
2134 | return log_oom(); | |
2135 | ||
2136 | m->source = TAKE_PTR(source); | |
2137 | m->destination = TAKE_PTR(destination); | |
2138 | m->read_only = true; | |
2139 | } | |
2140 | ||
2141 | return 0; | |
2142 | } | |
2143 | ||
2144 | static int oci_linux(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
2145 | ||
2146 | static const JsonDispatch table[] = { | |
2147 | { "namespaces", JSON_VARIANT_ARRAY, oci_namespaces, 0, 0 }, | |
2148 | { "uidMappings", JSON_VARIANT_ARRAY, oci_uid_gid_mappings, 0, 0 }, | |
2149 | { "gidMappings", JSON_VARIANT_ARRAY, oci_uid_gid_mappings, 0, 0 }, | |
2150 | { "devices", JSON_VARIANT_ARRAY, oci_devices, 0, 0 }, | |
2151 | { "cgroupsPath", JSON_VARIANT_STRING, oci_cgroups_path, 0, 0 }, | |
2152 | { "resources", JSON_VARIANT_OBJECT, oci_resources, 0, 0 }, | |
2153 | { "intelRdt", JSON_VARIANT_OBJECT, oci_unsupported, 0, JSON_PERMISSIVE }, | |
2154 | { "sysctl", JSON_VARIANT_OBJECT, oci_sysctl, 0, 0 }, | |
2155 | { "seccomp", JSON_VARIANT_OBJECT, oci_seccomp, 0, 0 }, | |
2156 | { "rootfsPropagation", JSON_VARIANT_STRING, oci_rootfs_propagation, 0, 0 }, | |
2157 | { "maskedPaths", JSON_VARIANT_ARRAY, oci_masked_paths, 0, 0 }, | |
2158 | { "readonlyPaths", JSON_VARIANT_ARRAY, oci_readonly_paths, 0, 0 }, | |
2159 | { "mountLabel", JSON_VARIANT_STRING, oci_unsupported, 0, JSON_PERMISSIVE }, | |
2160 | {} | |
2161 | }; | |
2162 | ||
2163 | return json_dispatch(v, table, oci_unexpected, flags, userdata); | |
2164 | } | |
2165 | ||
2166 | static int oci_hook_timeout(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
2167 | usec_t *u = userdata; | |
2168 | uintmax_t k; | |
2169 | ||
2170 | k = json_variant_unsigned(v); | |
2171 | if (k == 0) { | |
2172 | json_log(v, flags, 0, "Hook timeout cannot be zero."); | |
2173 | return -EINVAL; | |
2174 | } | |
2175 | ||
2176 | if (k > (UINT64_MAX-1/USEC_PER_SEC)) { | |
2177 | json_log(v, flags, 0, "Hook timeout too large."); | |
2178 | return -EINVAL; | |
2179 | } | |
2180 | ||
2181 | *u = k * USEC_PER_SEC; | |
2182 | return 0; | |
2183 | } | |
2184 | ||
2185 | static int oci_hooks_array(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
2186 | Settings *s = userdata; | |
2187 | JsonVariant *e; | |
2188 | int r; | |
2189 | ||
2190 | assert(s); | |
2191 | ||
2192 | JSON_VARIANT_ARRAY_FOREACH(e, v) { | |
2193 | ||
2194 | static const JsonDispatch table[] = { | |
2195 | { "path", JSON_VARIANT_STRING, oci_absolute_path, offsetof(OciHook, path), JSON_MANDATORY }, | |
2196 | { "args", JSON_VARIANT_ARRAY, oci_args, offsetof(OciHook, args), 0 }, | |
2197 | { "env", JSON_VARIANT_ARRAY, oci_env, offsetof(OciHook, env), 0 }, | |
2198 | { "timeout", JSON_VARIANT_UNSIGNED, oci_hook_timeout, offsetof(OciHook, timeout), 0 }, | |
2199 | {} | |
2200 | }; | |
2201 | ||
2202 | OciHook *a, **array, *new_item; | |
2203 | size_t *n_array; | |
2204 | ||
2205 | if (streq(name, "prestart")) { | |
2206 | array = &s->oci_hooks_prestart; | |
2207 | n_array = &s->n_oci_hooks_prestart; | |
2208 | } else if (streq(name, "poststart")) { | |
2209 | array = &s->oci_hooks_poststart; | |
2210 | n_array = &s->n_oci_hooks_poststart; | |
2211 | } else { | |
2212 | assert(streq(name, "poststop")); | |
2213 | array = &s->oci_hooks_poststop; | |
2214 | n_array = &s->n_oci_hooks_poststop; | |
2215 | } | |
2216 | ||
2217 | a = reallocarray(*array, *n_array + 1, sizeof(OciHook)); | |
2218 | if (!a) | |
2219 | return log_oom(); | |
2220 | ||
2221 | *array = a; | |
2222 | new_item = a + *n_array; | |
2223 | ||
2224 | *new_item = (OciHook) { | |
2225 | .timeout = USEC_INFINITY, | |
2226 | }; | |
2227 | ||
2228 | r = json_dispatch(e, table, oci_unexpected, flags, userdata); | |
2229 | if (r < 0) { | |
2230 | free(new_item->path); | |
2231 | strv_free(new_item->args); | |
2232 | strv_free(new_item->env); | |
2233 | return r; | |
2234 | } | |
2235 | ||
2236 | (*n_array) ++; | |
2237 | } | |
2238 | ||
2239 | return 0; | |
2240 | } | |
2241 | ||
2242 | static int oci_hooks(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
2243 | ||
2244 | static const JsonDispatch table[] = { | |
2245 | { "prestart", JSON_VARIANT_OBJECT, oci_hooks_array, 0, 0 }, | |
2246 | { "poststart", JSON_VARIANT_OBJECT, oci_hooks_array, 0, 0 }, | |
2247 | { "poststop", JSON_VARIANT_OBJECT, oci_hooks_array, 0, 0 }, | |
2248 | {} | |
2249 | }; | |
2250 | ||
2251 | return json_dispatch(v, table, oci_unexpected, flags, userdata); | |
2252 | } | |
2253 | ||
2254 | static int oci_annotations(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) { | |
2255 | JsonVariant *k, *w; | |
2256 | ||
2257 | JSON_VARIANT_OBJECT_FOREACH(k, w, v) { | |
2258 | const char *n; | |
2259 | ||
2260 | assert_se(n = json_variant_string(k)); | |
2261 | ||
2262 | if (isempty(n)) { | |
2263 | json_log(k, flags, 0, "Annotation with empty key, refusing."); | |
2264 | return -EINVAL; | |
2265 | } | |
2266 | ||
2267 | if (!json_variant_is_string(w)) { | |
2268 | json_log(w, flags, 0, "Annotation has non-string value, refusing."); | |
2269 | return -EINVAL; | |
2270 | } | |
2271 | ||
2272 | json_log(k, flags|JSON_DEBUG, 0, "Ignoring annotation '%s' with value '%s'.", n, json_variant_string(w)); | |
2273 | } | |
2274 | ||
2275 | return 0; | |
2276 | } | |
2277 | ||
2278 | int oci_load(FILE *f, const char *bundle, Settings **ret) { | |
2279 | ||
2280 | static const JsonDispatch table[] = { | |
2281 | { "ociVersion", JSON_VARIANT_STRING, NULL, 0, JSON_MANDATORY }, | |
2282 | { "process", JSON_VARIANT_OBJECT, oci_process, 0, 0 }, | |
2283 | { "root", JSON_VARIANT_OBJECT, oci_root, 0, 0 }, | |
2284 | { "hostname", JSON_VARIANT_STRING, oci_hostname, 0, 0 }, | |
2285 | { "mounts", JSON_VARIANT_ARRAY, oci_mounts, 0, 0 }, | |
2286 | { "linux", JSON_VARIANT_OBJECT, oci_linux, 0, 0 }, | |
2287 | { "hooks", JSON_VARIANT_OBJECT, oci_hooks, 0, 0 }, | |
2288 | { "annotations", JSON_VARIANT_OBJECT, oci_annotations, 0, 0 }, | |
2289 | {} | |
2290 | }; | |
2291 | ||
2292 | _cleanup_(json_variant_unrefp) JsonVariant *oci = NULL; | |
2293 | _cleanup_(settings_freep) Settings *s = NULL; | |
2294 | unsigned line = 0, column = 0; | |
2295 | JsonVariant *v; | |
2296 | const char *path; | |
2297 | int r; | |
2298 | ||
2299 | assert_se(bundle); | |
2300 | ||
2301 | path = strjoina(bundle, "/config.json"); | |
2302 | ||
2303 | r = json_parse_file(f, path, &oci, &line, &column); | |
2304 | if (r < 0) { | |
2305 | if (line != 0 && column != 0) | |
2306 | return log_error_errno(r, "Failed to parse '%s' at %u:%u: %m", path, line, column); | |
2307 | else | |
2308 | return log_error_errno(r, "Failed to parse '%s': %m", path); | |
2309 | } | |
2310 | ||
2311 | v = json_variant_by_key(oci, "ociVersion"); | |
2312 | if (!v) { | |
2313 | log_error("JSON file '%s' is not an OCI bundle configuration file. Refusing.", path); | |
2314 | return -EINVAL; | |
2315 | } | |
2316 | if (!streq_ptr(json_variant_string(v), "1.0.0")) { | |
2317 | log_error("OCI bundle version not supported: %s", strna(json_variant_string(v))); | |
2318 | return -EINVAL; | |
2319 | } | |
2320 | ||
2321 | // { | |
2322 | // _cleanup_free_ char *formatted = NULL; | |
2323 | // assert_se(json_variant_format(oci, JSON_FORMAT_PRETTY|JSON_FORMAT_COLOR, &formatted) >= 0); | |
2324 | // fputs(formatted, stdout); | |
2325 | // } | |
2326 | ||
2327 | s = settings_new(); | |
2328 | if (!s) | |
2329 | return log_oom(); | |
2330 | ||
2331 | s->start_mode = START_PID1; | |
2332 | s->resolv_conf = RESOLV_CONF_OFF; | |
2333 | s->link_journal = LINK_NO; | |
2334 | s->timezone = TIMEZONE_OFF; | |
2335 | ||
2336 | s->bundle = strdup(bundle); | |
2337 | if (!s->bundle) | |
2338 | return log_oom(); | |
2339 | ||
2340 | r = json_dispatch(oci, table, oci_unexpected, 0, s); | |
2341 | if (r < 0) | |
2342 | return r; | |
2343 | ||
2344 | if (s->properties) { | |
2345 | r = sd_bus_message_seal(s->properties, 0, 0); | |
2346 | if (r < 0) | |
2347 | return log_error_errno(r, "Cannot seal properties bus message: %m"); | |
2348 | } | |
2349 | ||
2350 | *ret = TAKE_PTR(s); | |
2351 | return 0; | |
2352 | } |