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