]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/nspawn/nspawn-oci.c
Merge pull request #16271 from yuwata/network-cleanups-around-link-get
[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
668 n |= data.type;
669 }
670
671 if (!FLAGS_SET(n, CLONE_NEWNS))
672 return json_log(v, flags, SYNTHETIC_ERRNO(EOPNOTSUPP),
673 "Containers without file system namespace aren't supported.");
674
675 s->private_network = FLAGS_SET(n, CLONE_NEWNET);
676 s->userns_mode = FLAGS_SET(n, CLONE_NEWUSER) ? USER_NAMESPACE_FIXED : USER_NAMESPACE_NO;
677 s->use_cgns = FLAGS_SET(n, CLONE_NEWCGROUP);
678
679 s->clone_ns_flags = n & (CLONE_NEWIPC|CLONE_NEWPID|CLONE_NEWUTS);
680
681 return 0;
682 }
683
684 static int oci_uid_gid_range(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
685 uid_t *uid = userdata, u;
686 uintmax_t k;
687
688 assert(uid);
689 assert_cc(sizeof(uid_t) == sizeof(gid_t));
690
691 /* This is very much like oci_uid_gid(), except the checks are a bit different, as this is a UID range rather
692 * than a specific UID, and hence (uid_t) -1 has no special significance. OTOH a range of zero makes no
693 * sense. */
694
695 k = json_variant_unsigned(v);
696 u = (uid_t) k;
697 if ((uintmax_t) u != k)
698 return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
699 "UID/GID out of range: %ji", k);
700 if (u == 0)
701 return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
702 "UID/GID range can't be zero.");
703
704 *uid = u;
705 return 0;
706 }
707
708 static int oci_uid_gid_mappings(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
709 struct mapping_data {
710 uid_t host_id;
711 uid_t container_id;
712 uid_t range;
713 } data = {
714 .host_id = UID_INVALID,
715 .container_id = UID_INVALID,
716 .range = 0,
717 };
718
719 static const JsonDispatch table[] = {
720 { "containerID", JSON_VARIANT_UNSIGNED, oci_uid_gid, offsetof(struct mapping_data, container_id), JSON_MANDATORY },
721 { "hostID", JSON_VARIANT_UNSIGNED, oci_uid_gid, offsetof(struct mapping_data, host_id), JSON_MANDATORY },
722 { "size", JSON_VARIANT_UNSIGNED, oci_uid_gid_range, offsetof(struct mapping_data, range), JSON_MANDATORY },
723 {}
724 };
725
726 Settings *s = userdata;
727 JsonVariant *e;
728 int r;
729
730 assert(s);
731
732 if (json_variant_elements(v) == 0)
733 return 0;
734
735 if (json_variant_elements(v) > 1)
736 return json_log(v, flags, SYNTHETIC_ERRNO(EOPNOTSUPP),
737 "UID/GID mappings with more than one entry are not supported.");
738
739 assert_se(e = json_variant_by_index(v, 0));
740
741 r = json_dispatch(e, table, oci_unexpected, flags, &data);
742 if (r < 0)
743 return r;
744
745 if (data.host_id + data.range < data.host_id ||
746 data.container_id + data.range < data.container_id)
747 return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
748 "UID/GID range goes beyond UID/GID validity range, refusing.");
749
750 if (data.container_id != 0)
751 return json_log(v, flags, SYNTHETIC_ERRNO(EOPNOTSUPP),
752 "UID/GID mappings with a non-zero container base are not supported.");
753
754 if (data.range < 0x10000)
755 json_log(v, flags|JSON_WARNING, 0,
756 "UID/GID mapping with less than 65536 UID/GIDS set up, you are looking for trouble.");
757
758 if (s->uid_range != UID_INVALID &&
759 (s->uid_shift != data.host_id || s->uid_range != data.range))
760 return json_log(v, flags, SYNTHETIC_ERRNO(EOPNOTSUPP),
761 "Non-matching UID and GID mappings are not supported.");
762
763 s->uid_shift = data.host_id;
764 s->uid_range = data.range;
765
766 return 0;
767 }
768
769 static int oci_device_type(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
770 mode_t *mode = userdata;
771 const char *t;
772
773 assert(mode);
774 assert_se(t = json_variant_string(v));
775
776 if (STR_IN_SET(t, "c", "u"))
777 *mode = (*mode & ~S_IFMT) | S_IFCHR;
778 else if (streq(t, "b"))
779 *mode = (*mode & ~S_IFMT) | S_IFBLK;
780 else if (streq(t, "p"))
781 *mode = (*mode & ~S_IFMT) | S_IFIFO;
782 else
783 return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
784 "Unknown device type: %s", t);
785
786 return 0;
787 }
788
789 static int oci_device_major(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
790 unsigned *u = userdata;
791 uintmax_t k;
792
793 assert_se(u);
794
795 k = json_variant_unsigned(v);
796 if (!DEVICE_MAJOR_VALID(k))
797 return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
798 "Device major %ji out of range.", k);
799
800 *u = (unsigned) k;
801 return 0;
802 }
803
804 static int oci_device_minor(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
805 unsigned *u = userdata;
806 uintmax_t k;
807
808 assert_se(u);
809
810 k = json_variant_unsigned(v);
811 if (!DEVICE_MINOR_VALID(k))
812 return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
813 "Device minor %ji out of range.", k);
814
815 *u = (unsigned) k;
816 return 0;
817 }
818
819 static int oci_device_file_mode(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
820 mode_t *mode = userdata, m;
821 uintmax_t k;
822
823 assert(mode);
824
825 k = json_variant_unsigned(v);
826 m = (mode_t) k;
827
828 if ((m & ~07777) != 0 || (uintmax_t) m != k)
829 return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
830 "fileMode out of range, refusing.");
831
832 *mode = m;
833 return 0;
834 }
835
836 static int oci_devices(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
837 Settings *s = userdata;
838 JsonVariant *e;
839 int r;
840
841 assert(s);
842
843 JSON_VARIANT_ARRAY_FOREACH(e, v) {
844
845 static const JsonDispatch table[] = {
846 { "type", JSON_VARIANT_STRING, oci_device_type, offsetof(DeviceNode, mode), JSON_MANDATORY },
847 { "path", JSON_VARIANT_STRING, oci_absolute_path, offsetof(DeviceNode, path), JSON_MANDATORY },
848 { "major", JSON_VARIANT_UNSIGNED, oci_device_major, offsetof(DeviceNode, major), 0 },
849 { "minor", JSON_VARIANT_UNSIGNED, oci_device_minor, offsetof(DeviceNode, minor), 0 },
850 { "fileMode", JSON_VARIANT_UNSIGNED, oci_device_file_mode, offsetof(DeviceNode, mode), 0 },
851 { "uid", JSON_VARIANT_UNSIGNED, oci_uid_gid, offsetof(DeviceNode, uid), 0 },
852 { "gid", JSON_VARIANT_UNSIGNED, oci_uid_gid, offsetof(DeviceNode, gid), 0 },
853 {}
854 };
855
856 DeviceNode *node, *nodes;
857
858 nodes = reallocarray(s->extra_nodes, s->n_extra_nodes + 1, sizeof(DeviceNode));
859 if (!nodes)
860 return log_oom();
861
862 s->extra_nodes = nodes;
863
864 node = nodes + s->n_extra_nodes;
865 *node = (DeviceNode) {
866 .uid = UID_INVALID,
867 .gid = GID_INVALID,
868 .major = (unsigned) -1,
869 .minor = (unsigned) -1,
870 .mode = 0644,
871 };
872
873 r = json_dispatch(e, table, oci_unexpected, flags, node);
874 if (r < 0)
875 goto fail_element;
876
877 if (S_ISCHR(node->mode) || S_ISBLK(node->mode)) {
878 _cleanup_free_ char *path = NULL;
879
880 if (node->major == (unsigned) -1 || node->minor == (unsigned) -1) {
881 r = json_log(e, flags, SYNTHETIC_ERRNO(EINVAL),
882 "Major/minor required when device node is device node");
883 goto fail_element;
884 }
885
886 /* Suppress a couple of implicit device nodes */
887 r = device_path_make_canonical(node->mode, makedev(node->major, node->minor), &path);
888 if (r < 0)
889 json_log(e, flags|JSON_DEBUG, 0, "Failed to resolve device node %u:%u, ignoring: %m", node->major, node->minor);
890 else {
891 if (PATH_IN_SET(path,
892 "/dev/null",
893 "/dev/zero",
894 "/dev/full",
895 "/dev/random",
896 "/dev/urandom",
897 "/dev/tty",
898 "/dev/net/tun",
899 "/dev/ptmx",
900 "/dev/pts/ptmx",
901 "/dev/console")) {
902
903 json_log(e, flags|JSON_DEBUG, 0, "Ignoring devices item for device '%s', as it is implicitly created anyway.", path);
904 free(node->path);
905 continue;
906 }
907 }
908 }
909
910 s->n_extra_nodes++;
911 continue;
912
913 fail_element:
914 free(node->path);
915 return r;
916 }
917
918 return 0;
919 }
920
921 static int oci_cgroups_path(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
922 _cleanup_free_ char *slice = NULL, *backwards = NULL;
923 Settings *s = userdata;
924 const char *p;
925 int r;
926
927 assert(s);
928
929 assert_se(p = json_variant_string(v));
930
931 r = cg_path_get_slice(p, &slice);
932 if (r < 0)
933 return json_log(v, flags, r, "Couldn't derive slice unit name from path '%s': %m", p);
934
935 r = cg_slice_to_path(slice, &backwards);
936 if (r < 0)
937 return json_log(v, flags, r, "Couldn't convert slice unit name '%s' back to path: %m", slice);
938
939 if (!path_equal(backwards, p))
940 return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
941 "Control group path '%s' does not refer to slice unit, refusing.", p);
942
943 free_and_replace(s->slice, slice);
944 return 0;
945 }
946
947 static int oci_cgroup_device_type(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
948 mode_t *mode = userdata;
949 const char *n;
950
951 assert_se(n = json_variant_string(v));
952
953 if (streq(n, "c"))
954 *mode = S_IFCHR;
955 else if (streq(n, "b"))
956 *mode = S_IFBLK;
957 else
958 return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
959 "Control group device type unknown: %s", n);
960
961 return 0;
962 }
963
964 struct device_data {
965 bool allow;
966 bool r;
967 bool w;
968 bool m;
969 mode_t type;
970 unsigned major;
971 unsigned minor;
972 };
973
974 static int oci_cgroup_device_access(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
975 struct device_data *d = userdata;
976 bool r = false, w = false, m = false;
977 const char *s;
978 size_t i;
979
980 assert_se(s = json_variant_string(v));
981
982 for (i = 0; s[i]; i++)
983 if (s[i] == 'r')
984 r = true;
985 else if (s[i] == 'w')
986 w = true;
987 else if (s[i] == 'm')
988 m = true;
989 else
990 return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
991 "Unknown device access character '%c'.", s[i]);
992
993 d->r = r;
994 d->w = w;
995 d->m = m;
996
997 return 0;
998 }
999
1000 static int oci_cgroup_devices(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
1001
1002 _cleanup_free_ struct device_data *list = NULL;
1003 Settings *s = userdata;
1004 size_t n_list = 0, i;
1005 bool noop = false;
1006 JsonVariant *e;
1007 int r;
1008
1009 assert(s);
1010
1011 JSON_VARIANT_ARRAY_FOREACH(e, v) {
1012
1013 struct device_data data = {
1014 .major = (unsigned) -1,
1015 .minor = (unsigned) -1,
1016 }, *a;
1017
1018 static const JsonDispatch table[] = {
1019 { "allow", JSON_VARIANT_BOOLEAN, json_dispatch_boolean, offsetof(struct device_data, allow), JSON_MANDATORY },
1020 { "type", JSON_VARIANT_STRING, oci_cgroup_device_type, offsetof(struct device_data, type), 0 },
1021 { "major", JSON_VARIANT_UNSIGNED, oci_device_major, offsetof(struct device_data, major), 0 },
1022 { "minor", JSON_VARIANT_UNSIGNED, oci_device_minor, offsetof(struct device_data, minor), 0 },
1023 { "access", JSON_VARIANT_STRING, oci_cgroup_device_access, 0, 0 },
1024 {}
1025 };
1026
1027 r = json_dispatch(e, table, oci_unexpected, flags, &data);
1028 if (r < 0)
1029 return r;
1030
1031 if (!data.allow) {
1032 /* The fact that OCI allows 'deny' entries makes really no sense, as 'allow'
1033 * vs. 'deny' for the devices cgroup controller is really not about allow-listing and
1034 * deny-listing but about adding and removing entries from the allow list. Since we
1035 * always start out with an empty allow list we hence ignore the whole thing, as
1036 * removing entries which don't exist make no sense. We'll log about this, since this
1037 * is really borked in the spec, with one exception: the entry that's supposed to
1038 * drop the kernel's default we ignore silently */
1039
1040 if (!data.r || !data.w || !data.m || data.type != 0 || data.major != (unsigned) -1 || data.minor != (unsigned) -1)
1041 json_log(v, flags|JSON_WARNING, 0, "Devices cgroup allow list with arbitrary 'allow' entries not supported, ignoring.");
1042
1043 /* We ignore the 'deny' entry as for us that's implied */
1044 continue;
1045 }
1046
1047 if (!data.r && !data.w && !data.m) {
1048 json_log(v, flags|LOG_WARNING, 0, "Device cgroup allow list entry with no effect found, ignoring.");
1049 continue;
1050 }
1051
1052 if (data.minor != (unsigned) -1 && data.major == (unsigned) -1)
1053 return json_log(v, flags, SYNTHETIC_ERRNO(EOPNOTSUPP),
1054 "Device cgroup allow list entries with minors but no majors not supported.");
1055
1056 if (data.major != (unsigned) -1 && data.type == 0)
1057 return json_log(v, flags, SYNTHETIC_ERRNO(EOPNOTSUPP),
1058 "Device cgroup allow list entries with majors but no device node type not supported.");
1059
1060 if (data.type == 0) {
1061 if (data.r && data.w && data.m) /* a catchall allow list entry means we are looking at a noop */
1062 noop = true;
1063 else
1064 return json_log(v, flags, SYNTHETIC_ERRNO(EOPNOTSUPP),
1065 "Device cgroup allow list entries with no type not supported.");
1066 }
1067
1068 a = reallocarray(list, n_list + 1, sizeof(struct device_data));
1069 if (!a)
1070 return log_oom();
1071
1072 list = a;
1073 list[n_list++] = data;
1074 }
1075
1076 if (noop)
1077 return 0;
1078
1079 r = settings_allocate_properties(s);
1080 if (r < 0)
1081 return r;
1082
1083 r = sd_bus_message_open_container(s->properties, 'r', "sv");
1084 if (r < 0)
1085 return bus_log_create_error(r);
1086
1087 r = sd_bus_message_append(s->properties, "s", "DeviceAllow");
1088 if (r < 0)
1089 return bus_log_create_error(r);
1090
1091 r = sd_bus_message_open_container(s->properties, 'v', "a(ss)");
1092 if (r < 0)
1093 return bus_log_create_error(r);
1094
1095 r = sd_bus_message_open_container(s->properties, 'a', "(ss)");
1096 if (r < 0)
1097 return bus_log_create_error(r);
1098
1099 for (i = 0; i < n_list; i++) {
1100 _cleanup_free_ char *pattern = NULL;
1101 char access[4];
1102 size_t n = 0;
1103
1104 if (list[i].minor == (unsigned) -1) {
1105 const char *t;
1106
1107 if (list[i].type == S_IFBLK)
1108 t = "block";
1109 else {
1110 assert(list[i].type == S_IFCHR);
1111 t = "char";
1112 }
1113
1114 if (list[i].major == (unsigned) -1) {
1115 pattern = strjoin(t, "-*");
1116 if (!pattern)
1117 return log_oom();
1118 } else {
1119 if (asprintf(&pattern, "%s-%u", t, list[i].major) < 0)
1120 return log_oom();
1121 }
1122
1123 } else {
1124 assert(list[i].major != (unsigned) -1); /* If a minor is specified, then a major also needs to be specified */
1125
1126 r = device_path_make_major_minor(list[i].type, makedev(list[i].major, list[i].minor), &pattern);
1127 if (r < 0)
1128 return log_oom();
1129 }
1130
1131 if (list[i].r)
1132 access[n++] = 'r';
1133 if (list[i].w)
1134 access[n++] = 'w';
1135 if (list[i].m)
1136 access[n++] = 'm';
1137 access[n] = 0;
1138
1139 assert(n > 0);
1140
1141 r = sd_bus_message_append(s->properties, "(ss)", pattern, access);
1142 if (r < 0)
1143 return bus_log_create_error(r);
1144 }
1145
1146 r = sd_bus_message_close_container(s->properties);
1147 if (r < 0)
1148 return bus_log_create_error(r);
1149
1150 r = sd_bus_message_close_container(s->properties);
1151 if (r < 0)
1152 return bus_log_create_error(r);
1153
1154 r = sd_bus_message_close_container(s->properties);
1155 if (r < 0)
1156 return bus_log_create_error(r);
1157
1158 return 0;
1159 }
1160
1161 static int oci_cgroup_memory_limit(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
1162 uint64_t *m = userdata;
1163 uintmax_t k;
1164
1165 assert(m);
1166
1167 if (json_variant_is_negative(v)) {
1168 *m = UINT64_MAX;
1169 return 0;
1170 }
1171
1172 if (!json_variant_is_unsigned(v))
1173 return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
1174 "Memory limit is not an unsigned integer");
1175
1176 k = json_variant_unsigned(v);
1177 if (k >= UINT64_MAX)
1178 return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
1179 "Memory limit too large: %ji", k);
1180
1181 *m = (uint64_t) k;
1182 return 0;
1183 }
1184
1185 static int oci_cgroup_memory(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
1186
1187 struct memory_data {
1188 uint64_t limit;
1189 uint64_t reservation;
1190 uint64_t swap;
1191 } data = {
1192 .limit = UINT64_MAX,
1193 .reservation = UINT64_MAX,
1194 .swap = UINT64_MAX,
1195 };
1196
1197 static const JsonDispatch table[] = {
1198 { "limit", JSON_VARIANT_NUMBER, oci_cgroup_memory_limit, offsetof(struct memory_data, limit), 0 },
1199 { "reservation", JSON_VARIANT_NUMBER, oci_cgroup_memory_limit, offsetof(struct memory_data, reservation), 0 },
1200 { "swap", JSON_VARIANT_NUMBER, oci_cgroup_memory_limit, offsetof(struct memory_data, swap), 0 },
1201 { "kernel", JSON_VARIANT_NUMBER, oci_unsupported, 0, JSON_PERMISSIVE },
1202 { "kernelTCP", JSON_VARIANT_NUMBER, oci_unsupported, 0, JSON_PERMISSIVE },
1203 { "swapiness", JSON_VARIANT_NUMBER, oci_unsupported, 0, JSON_PERMISSIVE },
1204 { "disableOOMKiller", JSON_VARIANT_NUMBER, oci_unsupported, 0, JSON_PERMISSIVE },
1205 {}
1206 };
1207
1208 Settings *s = userdata;
1209 int r;
1210
1211 r = json_dispatch(v, table, oci_unexpected, flags, &data);
1212 if (r < 0)
1213 return r;
1214
1215 if (data.swap != UINT64_MAX) {
1216 if (data.limit == UINT64_MAX)
1217 json_log(v, flags|LOG_WARNING, 0, "swap limit without memory limit is not supported, ignoring.");
1218 else if (data.swap < data.limit)
1219 json_log(v, flags|LOG_WARNING, 0, "swap limit is below memory limit, ignoring.");
1220 else {
1221 r = settings_allocate_properties(s);
1222 if (r < 0)
1223 return r;
1224
1225 r = sd_bus_message_append(s->properties, "(sv)", "MemorySwapMax", "t", data.swap - data.limit);
1226 if (r < 0)
1227 return bus_log_create_error(r);
1228 }
1229 }
1230
1231 if (data.limit != UINT64_MAX) {
1232 r = settings_allocate_properties(s);
1233 if (r < 0)
1234 return r;
1235
1236 r = sd_bus_message_append(s->properties, "(sv)", "MemoryMax", "t", data.limit);
1237 if (r < 0)
1238 return bus_log_create_error(r);
1239 }
1240
1241 if (data.reservation != UINT64_MAX) {
1242 r = settings_allocate_properties(s);
1243 if (r < 0)
1244 return r;
1245
1246 r = sd_bus_message_append(s->properties, "(sv)", "MemoryLow", "t", data.reservation);
1247 if (r < 0)
1248 return bus_log_create_error(r);
1249 }
1250
1251 return 0;
1252 }
1253
1254 struct cpu_data {
1255 uint64_t shares;
1256 uint64_t quota;
1257 uint64_t period;
1258 CPUSet cpu_set;
1259 };
1260
1261 static int oci_cgroup_cpu_shares(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
1262 uint64_t *u = userdata;
1263 uintmax_t k;
1264
1265 assert(u);
1266
1267 k = json_variant_unsigned(v);
1268 if (k < CGROUP_CPU_SHARES_MIN || k > CGROUP_CPU_SHARES_MAX)
1269 return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
1270 "shares value out of range.");
1271
1272 *u = (uint64_t) k;
1273 return 0;
1274 }
1275
1276 static int oci_cgroup_cpu_quota(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
1277 uint64_t *u = userdata;
1278 uintmax_t k;
1279
1280 assert(u);
1281
1282 k = json_variant_unsigned(v);
1283 if (k <= 0 || k >= UINT64_MAX)
1284 return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
1285 "period/quota value out of range.");
1286
1287 *u = (uint64_t) k;
1288 return 0;
1289 }
1290
1291 static int oci_cgroup_cpu_cpus(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
1292 struct cpu_data *data = userdata;
1293 CPUSet set;
1294 const char *n;
1295 int r;
1296
1297 assert(data);
1298
1299 assert_se(n = json_variant_string(v));
1300
1301 r = parse_cpu_set(n, &set);
1302 if (r < 0)
1303 return json_log(v, flags, r, "Failed to parse CPU set specification: %s", n);
1304
1305 cpu_set_reset(&data->cpu_set);
1306 data->cpu_set = set;
1307
1308 return 0;
1309 }
1310
1311 static int oci_cgroup_cpu(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
1312
1313 static const JsonDispatch table[] = {
1314 { "shares", JSON_VARIANT_UNSIGNED, oci_cgroup_cpu_shares, offsetof(struct cpu_data, shares), 0 },
1315 { "quota", JSON_VARIANT_UNSIGNED, oci_cgroup_cpu_quota, offsetof(struct cpu_data, quota), 0 },
1316 { "period", JSON_VARIANT_UNSIGNED, oci_cgroup_cpu_quota, offsetof(struct cpu_data, period), 0 },
1317 { "realtimeRuntime", JSON_VARIANT_UNSIGNED, oci_unsupported, 0, 0 },
1318 { "realtimePeriod", JSON_VARIANT_UNSIGNED, oci_unsupported, 0, 0 },
1319 { "cpus", JSON_VARIANT_STRING, oci_cgroup_cpu_cpus, 0, 0 },
1320 { "mems", JSON_VARIANT_STRING, oci_unsupported, 0, 0 },
1321 {}
1322 };
1323
1324 struct cpu_data data = {
1325 .shares = UINT64_MAX,
1326 .quota = UINT64_MAX,
1327 .period = UINT64_MAX,
1328 };
1329
1330 Settings *s = userdata;
1331 int r;
1332
1333 r = json_dispatch(v, table, oci_unexpected, flags, &data);
1334 if (r < 0) {
1335 cpu_set_reset(&data.cpu_set);
1336 return r;
1337 }
1338
1339 cpu_set_reset(&s->cpu_set);
1340 s->cpu_set = data.cpu_set;
1341
1342 if (data.shares != UINT64_MAX) {
1343 r = settings_allocate_properties(s);
1344 if (r < 0)
1345 return r;
1346
1347 r = sd_bus_message_append(s->properties, "(sv)", "CPUShares", "t", data.shares);
1348 if (r < 0)
1349 return bus_log_create_error(r);
1350 }
1351
1352 if (data.quota != UINT64_MAX && data.period != UINT64_MAX) {
1353 r = settings_allocate_properties(s);
1354 if (r < 0)
1355 return r;
1356
1357 r = sd_bus_message_append(s->properties, "(sv)", "CPUQuotaPerSecUSec", "t", (uint64_t) (data.quota * USEC_PER_SEC / data.period));
1358 if (r < 0)
1359 return bus_log_create_error(r);
1360
1361 } else if ((data.quota != UINT64_MAX) != (data.period != UINT64_MAX))
1362 return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
1363 "CPU quota and period not used together.");
1364
1365 return 0;
1366 }
1367
1368 static int oci_cgroup_block_io_weight(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
1369 Settings *s = userdata;
1370 uintmax_t k;
1371 int r;
1372
1373 assert(s);
1374
1375 k = json_variant_unsigned(v);
1376 if (k < CGROUP_BLKIO_WEIGHT_MIN || k > CGROUP_BLKIO_WEIGHT_MAX)
1377 return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
1378 "Block I/O weight out of range.");
1379
1380 r = settings_allocate_properties(s);
1381 if (r < 0)
1382 return r;
1383
1384 r = sd_bus_message_append(s->properties, "(sv)", "BlockIOWeight", "t", (uint64_t) k);
1385 if (r < 0)
1386 return bus_log_create_error(r);
1387
1388 return 0;
1389 }
1390
1391 static int oci_cgroup_block_io_weight_device(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
1392 Settings *s = userdata;
1393 JsonVariant *e;
1394 int r;
1395
1396 assert(s);
1397
1398 JSON_VARIANT_ARRAY_FOREACH(e, v) {
1399 struct device_data {
1400 unsigned major;
1401 unsigned minor;
1402 uintmax_t weight;
1403 } data = {
1404 .major = (unsigned) -1,
1405 .minor = (unsigned) -1,
1406 .weight = UINTMAX_MAX,
1407 };
1408
1409 static const JsonDispatch table[] = {
1410 { "major", JSON_VARIANT_UNSIGNED, oci_device_major, offsetof(struct device_data, major), JSON_MANDATORY },
1411 { "minor", JSON_VARIANT_UNSIGNED, oci_device_minor, offsetof(struct device_data, minor), JSON_MANDATORY },
1412 { "weight", JSON_VARIANT_UNSIGNED, json_dispatch_unsigned, offsetof(struct device_data, weight), 0 },
1413 { "leafWeight", JSON_VARIANT_INTEGER, oci_unsupported, 0, JSON_PERMISSIVE },
1414 {}
1415 };
1416
1417 _cleanup_free_ char *path = NULL;
1418
1419 r = json_dispatch(e, table, oci_unexpected, flags, &data);
1420 if (r < 0)
1421 return r;
1422
1423 if (data.weight == UINTMAX_MAX)
1424 continue;
1425
1426 if (data.weight < CGROUP_BLKIO_WEIGHT_MIN || data.weight > CGROUP_BLKIO_WEIGHT_MAX)
1427 return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
1428 "Block I/O device weight out of range.");
1429
1430 r = device_path_make_major_minor(S_IFBLK, makedev(data.major, data.minor), &path);
1431 if (r < 0)
1432 return json_log(v, flags, r, "Failed to build device path: %m");
1433
1434 r = settings_allocate_properties(s);
1435 if (r < 0)
1436 return r;
1437
1438 r = sd_bus_message_append(s->properties, "(sv)", "BlockIODeviceWeight", "a(st)", 1, path, (uint64_t) data.weight);
1439 if (r < 0)
1440 return bus_log_create_error(r);
1441 }
1442
1443 return 0;
1444 }
1445
1446 static int oci_cgroup_block_io_throttle(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
1447 Settings *s = userdata;
1448 const char *pname;
1449 JsonVariant *e;
1450 int r;
1451
1452 assert(s);
1453
1454 pname = streq(name, "throttleReadBpsDevice") ? "IOReadBandwidthMax" :
1455 streq(name, "throttleWriteBpsDevice") ? "IOWriteBandwidthMax" :
1456 streq(name, "throttleReadIOPSDevice") ? "IOReadIOPSMax" :
1457 "IOWriteIOPSMax";
1458
1459 JSON_VARIANT_ARRAY_FOREACH(e, v) {
1460 struct device_data {
1461 unsigned major;
1462 unsigned minor;
1463 uintmax_t rate;
1464 } data = {
1465 .major = (unsigned) -1,
1466 .minor = (unsigned) -1,
1467 };
1468
1469 static const JsonDispatch table[] = {
1470 { "major", JSON_VARIANT_UNSIGNED, oci_device_major, offsetof(struct device_data, major), JSON_MANDATORY },
1471 { "minor", JSON_VARIANT_UNSIGNED, oci_device_minor, offsetof(struct device_data, minor), JSON_MANDATORY },
1472 { "rate", JSON_VARIANT_UNSIGNED, json_dispatch_unsigned, offsetof(struct device_data, rate), JSON_MANDATORY },
1473 {}
1474 };
1475
1476 _cleanup_free_ char *path = NULL;
1477
1478 r = json_dispatch(e, table, oci_unexpected, flags, &data);
1479 if (r < 0)
1480 return r;
1481
1482 if (data.rate >= UINT64_MAX)
1483 return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
1484 "Block I/O device rate out of range.");
1485
1486 r = device_path_make_major_minor(S_IFBLK, makedev(data.major, data.minor), &path);
1487 if (r < 0)
1488 return json_log(v, flags, r, "Failed to build device path: %m");
1489
1490 r = settings_allocate_properties(s);
1491 if (r < 0)
1492 return r;
1493
1494 r = sd_bus_message_append(s->properties, "(sv)", pname, "a(st)", 1, path, (uint64_t) data.rate);
1495 if (r < 0)
1496 return bus_log_create_error(r);
1497 }
1498
1499 return 0;
1500 }
1501
1502 static int oci_cgroup_block_io(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
1503
1504 static const JsonDispatch table[] = {
1505 { "weight", JSON_VARIANT_UNSIGNED, oci_cgroup_block_io_weight, 0, 0 },
1506 { "leafWeight", JSON_VARIANT_UNSIGNED, oci_unsupported, 0, JSON_PERMISSIVE },
1507 { "weightDevice", JSON_VARIANT_ARRAY, oci_cgroup_block_io_weight_device, 0, 0 },
1508 { "throttleReadBpsDevice", JSON_VARIANT_ARRAY, oci_cgroup_block_io_throttle, 0, 0 },
1509 { "throttleWriteBpsDevice", JSON_VARIANT_ARRAY, oci_cgroup_block_io_throttle, 0, 0 },
1510 { "throttleReadIOPSDevice", JSON_VARIANT_ARRAY, oci_cgroup_block_io_throttle, 0, 0 },
1511 { "throttleWriteIOPSDevice", JSON_VARIANT_ARRAY, oci_cgroup_block_io_throttle, 0, 0 },
1512 {}
1513 };
1514
1515 return json_dispatch(v, table, oci_unexpected, flags, userdata);
1516 }
1517
1518 static int oci_cgroup_pids(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
1519
1520 static const JsonDispatch table[] = {
1521 { "limit", JSON_VARIANT_NUMBER, json_dispatch_variant, 0, JSON_MANDATORY },
1522 {}
1523 };
1524
1525 _cleanup_(json_variant_unrefp) JsonVariant *k = NULL;
1526 Settings *s = userdata;
1527 uint64_t m;
1528 int r;
1529
1530 assert(s);
1531
1532 r = json_dispatch(v, table, oci_unexpected, flags, &k);
1533 if (r < 0)
1534 return r;
1535
1536 if (json_variant_is_negative(k))
1537 m = UINT64_MAX;
1538 else {
1539 if (!json_variant_is_unsigned(k))
1540 return json_log(k, flags, SYNTHETIC_ERRNO(EINVAL),
1541 "pids limit not unsigned integer, refusing.");
1542
1543 m = (uint64_t) json_variant_unsigned(k);
1544
1545 if ((uintmax_t) m != json_variant_unsigned(k))
1546 return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
1547 "pids limit out of range, refusing.");
1548 }
1549
1550 r = settings_allocate_properties(s);
1551 if (r < 0)
1552 return r;
1553
1554 r = sd_bus_message_append(s->properties, "(sv)", "TasksMax", "t", m);
1555 if (r < 0)
1556 return bus_log_create_error(r);
1557
1558 return 0;
1559 }
1560
1561 static int oci_resources(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
1562
1563 static const JsonDispatch table[] = {
1564 { "devices", JSON_VARIANT_ARRAY, oci_cgroup_devices, 0, 0 },
1565 { "memory", JSON_VARIANT_OBJECT, oci_cgroup_memory, 0, 0 },
1566 { "cpu", JSON_VARIANT_OBJECT, oci_cgroup_cpu, 0, 0 },
1567 { "blockIO", JSON_VARIANT_OBJECT, oci_cgroup_block_io, 0, 0 },
1568 { "hugepageLimits", JSON_VARIANT_ARRAY, oci_unsupported, 0, 0 },
1569 { "network", JSON_VARIANT_OBJECT, oci_unsupported, 0, 0 },
1570 { "pids", JSON_VARIANT_OBJECT, oci_cgroup_pids, 0, 0 },
1571 { "rdma", JSON_VARIANT_OBJECT, oci_unsupported, 0, 0 },
1572 {}
1573 };
1574
1575 return json_dispatch(v, table, oci_unexpected, flags, userdata);
1576 }
1577
1578 static bool sysctl_key_valid(const char *s) {
1579 bool dot = true;
1580
1581 /* Note that we are a bit stricter here than in systemd-sysctl, as that inherited semantics from the old sysctl
1582 * tool, which were really weird (as it swaps / and . in both ways) */
1583
1584 if (isempty(s))
1585 return false;
1586
1587 for (; *s; s++) {
1588
1589 if (*s <= ' ' || *s >= 127)
1590 return false;
1591 if (*s == '/')
1592 return false;
1593 if (*s == '.') {
1594
1595 if (dot) /* Don't allow two dots next to each other (or at the beginning) */
1596 return false;
1597
1598 dot = true;
1599 } else
1600 dot = false;
1601 }
1602
1603 if (dot) /* don't allow a dot at the end */
1604 return false;
1605
1606 return true;
1607 }
1608
1609 static int oci_sysctl(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
1610 Settings *s = userdata;
1611 JsonVariant *w;
1612 const char *k;
1613 int r;
1614
1615 assert(s);
1616
1617 JSON_VARIANT_OBJECT_FOREACH(k, w, v) {
1618 const char *m;
1619
1620 if (!json_variant_is_string(w))
1621 return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
1622 "sysctl parameter is not a string, refusing.");
1623
1624 assert_se(m = json_variant_string(w));
1625
1626 if (sysctl_key_valid(k))
1627 return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
1628 "sysctl key invalid, refusing: %s", k);
1629
1630 r = strv_extend_strv(&s->sysctl, STRV_MAKE(k, m), false);
1631 if (r < 0)
1632 return log_oom();
1633 }
1634
1635 return 0;
1636 }
1637
1638 #if HAVE_SECCOMP
1639 static int oci_seccomp_action_from_string(const char *name, uint32_t *ret) {
1640
1641 static const struct {
1642 const char *name;
1643 uint32_t action;
1644 } table[] = {
1645 { "SCMP_ACT_ALLOW", SCMP_ACT_ALLOW },
1646 { "SCMP_ACT_ERRNO", SCMP_ACT_ERRNO(EPERM) }, /* the OCI spec doesn't document the error, but it appears EPERM is supposed to be used */
1647 { "SCMP_ACT_KILL", SCMP_ACT_KILL },
1648 #ifdef SCMP_ACT_KILL_PROCESS
1649 { "SCMP_ACT_KILL_PROCESS", SCMP_ACT_KILL_PROCESS },
1650 #endif
1651 #ifdef SCMP_ACT_KILL_THREAD
1652 { "SCMP_ACT_KILL_THREAD", SCMP_ACT_KILL_THREAD },
1653 #endif
1654 #ifdef SCMP_ACT_LOG
1655 { "SCMP_ACT_LOG", SCMP_ACT_LOG },
1656 #endif
1657 { "SCMP_ACT_TRAP", SCMP_ACT_TRAP },
1658
1659 /* We don't support SCMP_ACT_TRACE because that requires a tracer, and that doesn't really make sense
1660 * here */
1661 };
1662
1663 size_t i;
1664
1665 for (i = 0; i < ELEMENTSOF(table); i++)
1666 if (streq_ptr(name, table[i].name)) {
1667 *ret = table[i].action;
1668 return 0;
1669 }
1670
1671 return -EINVAL;
1672 }
1673
1674 static int oci_seccomp_arch_from_string(const char *name, uint32_t *ret) {
1675
1676 static const struct {
1677 const char *name;
1678 uint32_t arch;
1679 } table[] = {
1680 { "SCMP_ARCH_AARCH64", SCMP_ARCH_AARCH64 },
1681 { "SCMP_ARCH_ARM", SCMP_ARCH_ARM },
1682 { "SCMP_ARCH_MIPS", SCMP_ARCH_MIPS },
1683 { "SCMP_ARCH_MIPS64", SCMP_ARCH_MIPS64 },
1684 { "SCMP_ARCH_MIPS64N32", SCMP_ARCH_MIPS64N32 },
1685 { "SCMP_ARCH_MIPSEL", SCMP_ARCH_MIPSEL },
1686 { "SCMP_ARCH_MIPSEL64", SCMP_ARCH_MIPSEL64 },
1687 { "SCMP_ARCH_MIPSEL64N32", SCMP_ARCH_MIPSEL64N32 },
1688 { "SCMP_ARCH_NATIVE", SCMP_ARCH_NATIVE },
1689 #ifdef SCMP_ARCH_PARISC
1690 { "SCMP_ARCH_PARISC", SCMP_ARCH_PARISC },
1691 #endif
1692 #ifdef SCMP_ARCH_PARISC64
1693 { "SCMP_ARCH_PARISC64", SCMP_ARCH_PARISC64 },
1694 #endif
1695 { "SCMP_ARCH_PPC", SCMP_ARCH_PPC },
1696 { "SCMP_ARCH_PPC64", SCMP_ARCH_PPC64 },
1697 { "SCMP_ARCH_PPC64LE", SCMP_ARCH_PPC64LE },
1698 { "SCMP_ARCH_S390", SCMP_ARCH_S390 },
1699 { "SCMP_ARCH_S390X", SCMP_ARCH_S390X },
1700 { "SCMP_ARCH_X32", SCMP_ARCH_X32 },
1701 { "SCMP_ARCH_X86", SCMP_ARCH_X86 },
1702 { "SCMP_ARCH_X86_64", SCMP_ARCH_X86_64 },
1703 };
1704
1705 size_t i;
1706
1707 for (i = 0; i < ELEMENTSOF(table); i++)
1708 if (streq_ptr(table[i].name, name)) {
1709 *ret = table[i].arch;
1710 return 0;
1711 }
1712
1713 return -EINVAL;
1714 }
1715
1716 static int oci_seccomp_compare_from_string(const char *name, enum scmp_compare *ret) {
1717
1718 static const struct {
1719 const char *name;
1720 enum scmp_compare op;
1721 } table[] = {
1722 { "SCMP_CMP_NE", SCMP_CMP_NE },
1723 { "SCMP_CMP_LT", SCMP_CMP_LT },
1724 { "SCMP_CMP_LE", SCMP_CMP_LE },
1725 { "SCMP_CMP_EQ", SCMP_CMP_EQ },
1726 { "SCMP_CMP_GE", SCMP_CMP_GE },
1727 { "SCMP_CMP_GT", SCMP_CMP_GT },
1728 { "SCMP_CMP_MASKED_EQ", SCMP_CMP_MASKED_EQ },
1729 };
1730
1731 size_t i;
1732
1733 for (i = 0; i < ELEMENTSOF(table); i++)
1734 if (streq_ptr(table[i].name, name)) {
1735 *ret = table[i].op;
1736 return 0;
1737 }
1738
1739 return -EINVAL;
1740 }
1741
1742 static int oci_seccomp_archs(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
1743 scmp_filter_ctx *sc = userdata;
1744 JsonVariant *e;
1745 int r;
1746
1747 assert(sc);
1748
1749 JSON_VARIANT_ARRAY_FOREACH(e, v) {
1750 uint32_t a;
1751
1752 if (!json_variant_is_string(e))
1753 return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL),
1754 "Architecture entry is not a string");
1755
1756 r = oci_seccomp_arch_from_string(json_variant_string(e), &a);
1757 if (r < 0)
1758 return json_log(e, flags, r, "Unknown architecture: %s", json_variant_string(e));
1759
1760 r = seccomp_arch_add(sc, a);
1761 if (r == -EEXIST)
1762 continue;
1763 if (r < 0)
1764 return json_log(e, flags, r, "Failed to add architecture to seccomp filter: %m");
1765 }
1766
1767 return 0;
1768 }
1769
1770 struct syscall_rule {
1771 char **names;
1772 uint32_t action;
1773 struct scmp_arg_cmp *arguments;
1774 size_t n_arguments;
1775 };
1776
1777 static void syscall_rule_free(struct syscall_rule *rule) {
1778 assert(rule);
1779
1780 strv_free(rule->names);
1781 free(rule->arguments);
1782 };
1783
1784 static int oci_seccomp_action(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
1785 uint32_t *action = userdata;
1786 int r;
1787
1788 assert(action);
1789
1790 r = oci_seccomp_action_from_string(json_variant_string(v), action);
1791 if (r < 0)
1792 return json_log(v, flags, r, "Unknown system call action '%s': %m", json_variant_string(v));
1793
1794 return 0;
1795 }
1796
1797 static int oci_seccomp_op(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
1798 enum scmp_compare *op = userdata;
1799 int r;
1800
1801 assert(op);
1802
1803 r = oci_seccomp_compare_from_string(json_variant_string(v), op);
1804 if (r < 0)
1805 return json_log(v, flags, r, "Unknown seccomp operator '%s': %m", json_variant_string(v));
1806
1807 return 0;
1808 }
1809
1810 static int oci_seccomp_args(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
1811 struct syscall_rule *rule = userdata;
1812 JsonVariant *e;
1813 int r;
1814
1815 assert(rule);
1816
1817 JSON_VARIANT_ARRAY_FOREACH(e, v) {
1818 static const struct JsonDispatch table[] = {
1819 { "index", JSON_VARIANT_UNSIGNED, json_dispatch_uint32, offsetof(struct scmp_arg_cmp, arg), JSON_MANDATORY },
1820 { "value", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(struct scmp_arg_cmp, datum_a), JSON_MANDATORY },
1821 { "valueTwo", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(struct scmp_arg_cmp, datum_b), 0 },
1822 { "op", JSON_VARIANT_STRING, oci_seccomp_op, offsetof(struct scmp_arg_cmp, op), JSON_MANDATORY },
1823 {},
1824 };
1825
1826 struct scmp_arg_cmp *a, *p;
1827 int expected;
1828
1829 a = reallocarray(rule->arguments, rule->n_arguments + 1, sizeof(struct syscall_rule));
1830 if (!a)
1831 return log_oom();
1832
1833 rule->arguments = a;
1834 p = rule->arguments + rule->n_arguments;
1835
1836 *p = (struct scmp_arg_cmp) {
1837 .arg = 0,
1838 .datum_a = 0,
1839 .datum_b = 0,
1840 .op = 0,
1841 };
1842
1843 r = json_dispatch(e, table, oci_unexpected, flags, p);
1844 if (r < 0)
1845 return r;
1846
1847 expected = p->op == SCMP_CMP_MASKED_EQ ? 4 : 3;
1848 if (r != expected)
1849 json_log(e, flags|JSON_WARNING, 0, "Wrong number of system call arguments for JSON data data, ignoring.");
1850
1851 /* Note that we are a bit sloppy here and do not insist that SCMP_CMP_MASKED_EQ gets two datum values,
1852 * and the other only one. That's because buildah for example by default calls things with
1853 * SCMP_CMP_MASKED_EQ but only one argument. We use 0 when the value is not specified. */
1854
1855 rule->n_arguments++;
1856 }
1857
1858 return 0;
1859 }
1860
1861 static int oci_seccomp_syscalls(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
1862 scmp_filter_ctx *sc = userdata;
1863 JsonVariant *e;
1864 int r;
1865
1866 assert(sc);
1867
1868 JSON_VARIANT_ARRAY_FOREACH(e, v) {
1869 static const JsonDispatch table[] = {
1870 { "names", JSON_VARIANT_ARRAY, json_dispatch_strv, offsetof(struct syscall_rule, names), JSON_MANDATORY },
1871 { "action", JSON_VARIANT_STRING, oci_seccomp_action, offsetof(struct syscall_rule, action), JSON_MANDATORY },
1872 { "args", JSON_VARIANT_ARRAY, oci_seccomp_args, 0, 0 },
1873 };
1874 struct syscall_rule rule = {
1875 .action = (uint32_t) -1,
1876 };
1877 char **i;
1878
1879 r = json_dispatch(e, table, oci_unexpected, flags, &rule);
1880 if (r < 0)
1881 goto fail_rule;
1882
1883 if (strv_isempty(rule.names)) {
1884 json_log(e, flags, 0, "System call name list is empty.");
1885 r = -EINVAL;
1886 goto fail_rule;
1887 }
1888
1889 STRV_FOREACH(i, rule.names) {
1890 int nr;
1891
1892 nr = seccomp_syscall_resolve_name(*i);
1893 if (nr == __NR_SCMP_ERROR) {
1894 log_debug("Unknown syscall %s, skipping.", *i);
1895 continue;
1896 }
1897
1898 r = seccomp_rule_add_array(sc, rule.action, nr, rule.n_arguments, rule.arguments);
1899 if (r < 0)
1900 goto fail_rule;
1901 }
1902
1903 syscall_rule_free(&rule);
1904 continue;
1905
1906 fail_rule:
1907 syscall_rule_free(&rule);
1908 return r;
1909 }
1910
1911 return 0;
1912 }
1913 #endif
1914
1915 static int oci_seccomp(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
1916
1917 #if HAVE_SECCOMP
1918 static const JsonDispatch table[] = {
1919 { "defaultAction", JSON_VARIANT_STRING, NULL, 0, JSON_MANDATORY },
1920 { "architectures", JSON_VARIANT_ARRAY, oci_seccomp_archs, 0, 0 },
1921 { "syscalls", JSON_VARIANT_ARRAY, oci_seccomp_syscalls, 0, 0 },
1922 {}
1923 };
1924
1925 _cleanup_(seccomp_releasep) scmp_filter_ctx sc = NULL;
1926 Settings *s = userdata;
1927 JsonVariant *def;
1928 uint32_t d;
1929 int r;
1930
1931 assert(s);
1932
1933 def = json_variant_by_key(v, "defaultAction");
1934 if (!def)
1935 return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL), "defaultAction element missing.");
1936
1937 if (!json_variant_is_string(def))
1938 return json_log(def, flags, SYNTHETIC_ERRNO(EINVAL), "defaultAction is not a string.");
1939
1940 r = oci_seccomp_action_from_string(json_variant_string(def), &d);
1941 if (r < 0)
1942 return json_log(def, flags, r, "Unknown default action: %s", json_variant_string(def));
1943
1944 sc = seccomp_init(d);
1945 if (!sc)
1946 return json_log(v, flags, SYNTHETIC_ERRNO(ENOMEM), "Couldn't allocate seccomp object.");
1947
1948 r = json_dispatch(v, table, oci_unexpected, flags, sc);
1949 if (r < 0)
1950 return r;
1951
1952 seccomp_release(s->seccomp);
1953 s->seccomp = TAKE_PTR(sc);
1954 return 0;
1955 #else
1956 return json_log(v, flags, SYNTHETIC_ERRNO(EOPNOTSUPP), "libseccomp support not enabled, can't parse seccomp object.");
1957 #endif
1958 }
1959
1960 static int oci_rootfs_propagation(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
1961 const char *s;
1962
1963 s = json_variant_string(v);
1964
1965 if (streq(s, "shared"))
1966 return 0;
1967
1968 json_log(v, flags|JSON_DEBUG, 0, "Ignoring rootfsPropagation setting '%s'.", s);
1969 return 0;
1970 }
1971
1972 static int oci_masked_paths(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
1973 Settings *s = userdata;
1974 JsonVariant *e;
1975
1976 assert(s);
1977
1978 JSON_VARIANT_ARRAY_FOREACH(e, v) {
1979 _cleanup_free_ char *destination = NULL;
1980 CustomMount *m;
1981 const char *p;
1982
1983 if (!json_variant_is_string(e))
1984 return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
1985 "Path is not a string, refusing.");
1986
1987 assert_se(p = json_variant_string(e));
1988
1989 if (!path_is_absolute(p))
1990 return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
1991 "Path is not not absolute, refusing: %s", p);
1992
1993 if (oci_exclude_mount(p))
1994 continue;
1995
1996 destination = strdup(p);
1997 if (!destination)
1998 return log_oom();
1999
2000 m = custom_mount_add(&s->custom_mounts, &s->n_custom_mounts, CUSTOM_MOUNT_INACCESSIBLE);
2001 if (!m)
2002 return log_oom();
2003
2004 m->destination = TAKE_PTR(destination);
2005
2006 /* The spec doesn't say this, but apparently pre-existing implementations are lenient towards
2007 * non-existing paths to mask. Let's hence be too. */
2008 m->graceful = true;
2009 }
2010
2011 return 0;
2012 }
2013
2014 static int oci_readonly_paths(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
2015 Settings *s = userdata;
2016 JsonVariant *e;
2017
2018 assert(s);
2019
2020 JSON_VARIANT_ARRAY_FOREACH(e, v) {
2021 _cleanup_free_ char *source = NULL, *destination = NULL;
2022 CustomMount *m;
2023 const char *p;
2024
2025 if (!json_variant_is_string(e))
2026 return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
2027 "Path is not a string, refusing.");
2028
2029 assert_se(p = json_variant_string(e));
2030
2031 if (!path_is_absolute(p))
2032 return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
2033 "Path is not not absolute, refusing: %s", p);
2034
2035 if (oci_exclude_mount(p))
2036 continue;
2037
2038 source = strjoin("+", p);
2039 if (!source)
2040 return log_oom();
2041
2042 destination = strdup(p);
2043 if (!destination)
2044 return log_oom();
2045
2046 m = custom_mount_add(&s->custom_mounts, &s->n_custom_mounts, CUSTOM_MOUNT_BIND);
2047 if (!m)
2048 return log_oom();
2049
2050 m->source = TAKE_PTR(source);
2051 m->destination = TAKE_PTR(destination);
2052 m->read_only = true;
2053 }
2054
2055 return 0;
2056 }
2057
2058 static int oci_linux(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
2059
2060 static const JsonDispatch table[] = {
2061 { "namespaces", JSON_VARIANT_ARRAY, oci_namespaces, 0, 0 },
2062 { "uidMappings", JSON_VARIANT_ARRAY, oci_uid_gid_mappings, 0, 0 },
2063 { "gidMappings", JSON_VARIANT_ARRAY, oci_uid_gid_mappings, 0, 0 },
2064 { "devices", JSON_VARIANT_ARRAY, oci_devices, 0, 0 },
2065 { "cgroupsPath", JSON_VARIANT_STRING, oci_cgroups_path, 0, 0 },
2066 { "resources", JSON_VARIANT_OBJECT, oci_resources, 0, 0 },
2067 { "intelRdt", JSON_VARIANT_OBJECT, oci_unsupported, 0, JSON_PERMISSIVE },
2068 { "sysctl", JSON_VARIANT_OBJECT, oci_sysctl, 0, 0 },
2069 { "seccomp", JSON_VARIANT_OBJECT, oci_seccomp, 0, 0 },
2070 { "rootfsPropagation", JSON_VARIANT_STRING, oci_rootfs_propagation, 0, 0 },
2071 { "maskedPaths", JSON_VARIANT_ARRAY, oci_masked_paths, 0, 0 },
2072 { "readonlyPaths", JSON_VARIANT_ARRAY, oci_readonly_paths, 0, 0 },
2073 { "mountLabel", JSON_VARIANT_STRING, oci_unsupported, 0, JSON_PERMISSIVE },
2074 {}
2075 };
2076
2077 return json_dispatch(v, table, oci_unexpected, flags, userdata);
2078 }
2079
2080 static int oci_hook_timeout(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
2081 usec_t *u = userdata;
2082 uintmax_t k;
2083
2084 k = json_variant_unsigned(v);
2085 if (k == 0 || k > (UINT64_MAX-1)/USEC_PER_SEC)
2086 return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
2087 "Hook timeout value out of range.");
2088
2089 *u = k * USEC_PER_SEC;
2090 return 0;
2091 }
2092
2093 static int oci_hooks_array(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
2094 Settings *s = userdata;
2095 JsonVariant *e;
2096 int r;
2097
2098 assert(s);
2099
2100 JSON_VARIANT_ARRAY_FOREACH(e, v) {
2101
2102 static const JsonDispatch table[] = {
2103 { "path", JSON_VARIANT_STRING, oci_absolute_path, offsetof(OciHook, path), JSON_MANDATORY },
2104 { "args", JSON_VARIANT_ARRAY, oci_args, offsetof(OciHook, args), 0 },
2105 { "env", JSON_VARIANT_ARRAY, oci_env, offsetof(OciHook, env), 0 },
2106 { "timeout", JSON_VARIANT_UNSIGNED, oci_hook_timeout, offsetof(OciHook, timeout), 0 },
2107 {}
2108 };
2109
2110 OciHook *a, **array, *new_item;
2111 size_t *n_array;
2112
2113 if (streq(name, "prestart")) {
2114 array = &s->oci_hooks_prestart;
2115 n_array = &s->n_oci_hooks_prestart;
2116 } else if (streq(name, "poststart")) {
2117 array = &s->oci_hooks_poststart;
2118 n_array = &s->n_oci_hooks_poststart;
2119 } else {
2120 assert(streq(name, "poststop"));
2121 array = &s->oci_hooks_poststop;
2122 n_array = &s->n_oci_hooks_poststop;
2123 }
2124
2125 a = reallocarray(*array, *n_array + 1, sizeof(OciHook));
2126 if (!a)
2127 return log_oom();
2128
2129 *array = a;
2130 new_item = a + *n_array;
2131
2132 *new_item = (OciHook) {
2133 .timeout = USEC_INFINITY,
2134 };
2135
2136 r = json_dispatch(e, table, oci_unexpected, flags, userdata);
2137 if (r < 0) {
2138 free(new_item->path);
2139 strv_free(new_item->args);
2140 strv_free(new_item->env);
2141 return r;
2142 }
2143
2144 (*n_array) ++;
2145 }
2146
2147 return 0;
2148 }
2149
2150 static int oci_hooks(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
2151
2152 static const JsonDispatch table[] = {
2153 { "prestart", JSON_VARIANT_OBJECT, oci_hooks_array, 0, 0 },
2154 { "poststart", JSON_VARIANT_OBJECT, oci_hooks_array, 0, 0 },
2155 { "poststop", JSON_VARIANT_OBJECT, oci_hooks_array, 0, 0 },
2156 {}
2157 };
2158
2159 return json_dispatch(v, table, oci_unexpected, flags, userdata);
2160 }
2161
2162 static int oci_annotations(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
2163 JsonVariant *w;
2164 const char *k;
2165
2166 JSON_VARIANT_OBJECT_FOREACH(k, w, v) {
2167
2168 if (isempty(k))
2169 return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
2170 "Annotation with empty key, refusing.");
2171
2172 if (!json_variant_is_string(w))
2173 return json_log(w, flags, SYNTHETIC_ERRNO(EINVAL),
2174 "Annotation has non-string value, refusing.");
2175
2176 json_log(w, flags|JSON_DEBUG, 0, "Ignoring annotation '%s' with value '%s'.", k, json_variant_string(w));
2177 }
2178
2179 return 0;
2180 }
2181
2182 int oci_load(FILE *f, const char *bundle, Settings **ret) {
2183
2184 static const JsonDispatch table[] = {
2185 { "ociVersion", JSON_VARIANT_STRING, NULL, 0, JSON_MANDATORY },
2186 { "process", JSON_VARIANT_OBJECT, oci_process, 0, 0 },
2187 { "root", JSON_VARIANT_OBJECT, oci_root, 0, 0 },
2188 { "hostname", JSON_VARIANT_STRING, oci_hostname, 0, 0 },
2189 { "mounts", JSON_VARIANT_ARRAY, oci_mounts, 0, 0 },
2190 { "linux", JSON_VARIANT_OBJECT, oci_linux, 0, 0 },
2191 { "hooks", JSON_VARIANT_OBJECT, oci_hooks, 0, 0 },
2192 { "annotations", JSON_VARIANT_OBJECT, oci_annotations, 0, 0 },
2193 {}
2194 };
2195
2196 _cleanup_(json_variant_unrefp) JsonVariant *oci = NULL;
2197 _cleanup_(settings_freep) Settings *s = NULL;
2198 unsigned line = 0, column = 0;
2199 JsonVariant *v;
2200 const char *path;
2201 int r;
2202
2203 assert_se(bundle);
2204
2205 path = strjoina(bundle, "/config.json");
2206
2207 r = json_parse_file(f, path, 0, &oci, &line, &column);
2208 if (r < 0) {
2209 if (line != 0 && column != 0)
2210 return log_error_errno(r, "Failed to parse '%s' at %u:%u: %m", path, line, column);
2211 else
2212 return log_error_errno(r, "Failed to parse '%s': %m", path);
2213 }
2214
2215 v = json_variant_by_key(oci, "ociVersion");
2216 if (!v) {
2217 log_error("JSON file '%s' is not an OCI bundle configuration file. Refusing.", path);
2218 return -EINVAL;
2219 }
2220 if (!streq_ptr(json_variant_string(v), "1.0.0")) {
2221 log_error("OCI bundle version not supported: %s", strna(json_variant_string(v)));
2222 return -EINVAL;
2223 }
2224
2225 // {
2226 // _cleanup_free_ char *formatted = NULL;
2227 // assert_se(json_variant_format(oci, JSON_FORMAT_PRETTY|JSON_FORMAT_COLOR, &formatted) >= 0);
2228 // fputs(formatted, stdout);
2229 // }
2230
2231 s = settings_new();
2232 if (!s)
2233 return log_oom();
2234
2235 s->start_mode = START_PID1;
2236 s->resolv_conf = RESOLV_CONF_OFF;
2237 s->link_journal = LINK_NO;
2238 s->timezone = TIMEZONE_OFF;
2239
2240 s->bundle = strdup(bundle);
2241 if (!s->bundle)
2242 return log_oom();
2243
2244 r = json_dispatch(oci, table, oci_unexpected, 0, s);
2245 if (r < 0)
2246 return r;
2247
2248 if (s->properties) {
2249 r = sd_bus_message_seal(s->properties, 0, 0);
2250 if (r < 0)
2251 return log_error_errno(r, "Cannot seal properties bus message: %m");
2252 }
2253
2254 *ret = TAKE_PTR(s);
2255 return 0;
2256 }