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