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