1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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 "devnum-util.h"
12 #include "env-util.h"
13 #include "format-util.h"
14 #include "fs-util.h"
15 #include "hostname-util.h"
16 #include "json.h"
17 #include "missing_sched.h"
18 #include "nspawn-oci.h"
19 #include "path-util.h"
20 #include "rlimit-util.h"
21 #if HAVE_SECCOMP
22 #include "seccomp-util.h"
23 #endif
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, swappiness, disableOOMKiller, swap, devices, leafWeight
52 * general: it shouldn't leak lower level abstractions this obviously
53 * unmanagable cgroups stuff: realtimeRuntime/realtimePeriod
54 * needs to say what happense when some option is not specified, i.e. which defaults apply
55 * no architecture? no personality?
56 * seccomp example and logic is simply broken: there's no constant "SCMP_ACT_ERRNO".
57 * spec should say what to do with unknown props
58 * /bin/mount regarding NFS and FUSE required?
59 * what does terminal=false mean?
60 * sysctl inside or outside? allow-listing?
61 * swapiness typo -> swappiness
62 *
63 * Unsupported:
64 *
65 * apparmorProfile
66 * selinuxLabel + mountLabel
67 * hugepageLimits
68 * network
69 * rdma
70 * intelRdt
71 * swappiness, disableOOMKiller, kernel, kernelTCP, leafWeight (because it's dead, cgroupsv2 can't do it and hence systemd neither)
72 *
73 * Non-slice cgroup paths
74 * Propagation that is not slave + shared
75 * more than one uid/gid mapping, mappings with a container base != 0, or non-matching uid/gid mappings
76 * device cgroups access = false items that are not catchall
77 * device cgroups matches where minor is specified, but major isn't. similar where major is specified but char/block is not. also, any match that only has a type set that has less than "rwm" set. also, any entry that has none of rwm set.
78 *
79 */
80
oci_unexpected(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)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
oci_unsupported(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)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
oci_terminal(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)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
oci_console_dimension(const char * name,JsonVariant * variant,JsonDispatchFlags flags,void * userdata)100 static int oci_console_dimension(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
101 unsigned *u = userdata;
102 uint64_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
oci_console_size(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)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
oci_absolute_path(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)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
oci_env(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)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
oci_args(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)172 static int oci_args(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
173 _cleanup_strv_free_ char **l = NULL;
174 char ***value = userdata;
175 int r;
176
177 assert(value);
178
179 r = json_variant_strv(v, &l);
180 if (r < 0)
181 return json_log(v, flags, r, "Cannot parse arguments as list of strings: %m");
182
183 if (strv_isempty(l))
184 return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
185 "Argument list empty, refusing.");
186
187 if (isempty(l[0]))
188 return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
189 "Executable name is empty, refusing.");
190
191 return strv_free_and_replace(*value, l);
192 }
193
oci_rlimit_type(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)194 static int oci_rlimit_type(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
195 const char *z;
196 int t, *type = userdata;
197
198 assert_se(type);
199
200 z = startswith(json_variant_string(v), "RLIMIT_");
201 if (!z)
202 return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
203 "rlimit entry's name does not begin with 'RLIMIT_', refusing: %s",
204 json_variant_string(v));
205
206 t = rlimit_from_string(z);
207 if (t < 0)
208 return json_log(v, flags, t,
209 "rlimit name unknown: %s", json_variant_string(v));
210
211 *type = t;
212 return 0;
213 }
214
oci_rlimit_value(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)215 static int oci_rlimit_value(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
216 rlim_t z, *value = userdata;
217
218 assert(value);
219
220 if (json_variant_is_negative(v))
221 z = RLIM_INFINITY;
222 else {
223 if (!json_variant_is_unsigned(v))
224 return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
225 "rlimits limit not unsigned, refusing.");
226
227 z = (rlim_t) json_variant_unsigned(v);
228
229 if ((uint64_t) z != json_variant_unsigned(v))
230 return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
231 "rlimits limit out of range, refusing.");
232 }
233
234 *value = z;
235 return 0;
236 }
237
oci_rlimits(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)238 static int oci_rlimits(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
239
240 Settings *s = userdata;
241 JsonVariant *e;
242 int r;
243
244 assert(s);
245
246 JSON_VARIANT_ARRAY_FOREACH(e, v) {
247
248 struct rlimit_data {
249 int type;
250 rlim_t soft;
251 rlim_t hard;
252 } data = {
253 .type = -1,
254 .soft = RLIM_INFINITY,
255 .hard = RLIM_INFINITY,
256 };
257
258 static const JsonDispatch table[] = {
259 { "soft", JSON_VARIANT_NUMBER, oci_rlimit_value, offsetof(struct rlimit_data, soft), JSON_MANDATORY },
260 { "hard", JSON_VARIANT_NUMBER, oci_rlimit_value, offsetof(struct rlimit_data, hard), JSON_MANDATORY },
261 { "type", JSON_VARIANT_STRING, oci_rlimit_type, offsetof(struct rlimit_data, type), JSON_MANDATORY },
262 {}
263 };
264
265 r = json_dispatch(e, table, oci_unexpected, flags, &data);
266 if (r < 0)
267 return r;
268
269 assert(data.type >= 0);
270 assert(data.type < _RLIMIT_MAX);
271
272 if (s->rlimit[data.type])
273 return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
274 "rlimits array contains duplicate entry, refusing.");
275
276 s->rlimit[data.type] = new(struct rlimit, 1);
277 if (!s->rlimit[data.type])
278 return log_oom();
279
280 *s->rlimit[data.type] = (struct rlimit) {
281 .rlim_cur = data.soft,
282 .rlim_max = data.hard,
283 };
284
285 }
286 return 0;
287 }
288
oci_capability_array(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)289 static int oci_capability_array(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
290 uint64_t *mask = userdata, m = 0;
291 JsonVariant *e;
292
293 JSON_VARIANT_ARRAY_FOREACH(e, v) {
294 const char *n;
295 int cap;
296
297 if (!json_variant_is_string(e))
298 return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
299 "Entry in capabilities array is not a string.");
300
301 assert_se(n = json_variant_string(e));
302
303 cap = capability_from_name(n);
304 if (cap < 0)
305 return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
306 "Unknown capability: %s", n);
307
308 m |= UINT64_C(1) << cap;
309 }
310
311 if (*mask == UINT64_MAX)
312 *mask = m;
313 else
314 *mask |= m;
315
316 return 0;
317 }
318
oci_capabilities(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)319 static int oci_capabilities(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
320
321 static const JsonDispatch table[] = {
322 { "effective", JSON_VARIANT_ARRAY, oci_capability_array, offsetof(CapabilityQuintet, effective) },
323 { "bounding", JSON_VARIANT_ARRAY, oci_capability_array, offsetof(CapabilityQuintet, bounding) },
324 { "inheritable", JSON_VARIANT_ARRAY, oci_capability_array, offsetof(CapabilityQuintet, inheritable) },
325 { "permitted", JSON_VARIANT_ARRAY, oci_capability_array, offsetof(CapabilityQuintet, permitted) },
326 { "ambient", JSON_VARIANT_ARRAY, oci_capability_array, offsetof(CapabilityQuintet, ambient) },
327 {}
328 };
329
330 Settings *s = userdata;
331 int r;
332
333 assert(s);
334
335 r = json_dispatch(v, table, oci_unexpected, flags, &s->full_capabilities);
336 if (r < 0)
337 return r;
338
339 if (s->full_capabilities.bounding != UINT64_MAX) {
340 s->capability = s->full_capabilities.bounding;
341 s->drop_capability = ~s->full_capabilities.bounding;
342 }
343
344 return 0;
345 }
346
oci_oom_score_adj(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)347 static int oci_oom_score_adj(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
348 Settings *s = userdata;
349 int64_t k;
350
351 assert(s);
352
353 k = json_variant_integer(v);
354 if (k < OOM_SCORE_ADJ_MIN || k > OOM_SCORE_ADJ_MAX)
355 return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
356 "oomScoreAdj value out of range: %ji", k);
357
358 s->oom_score_adjust = (int) k;
359 s->oom_score_adjust_set = true;
360
361 return 0;
362 }
363
oci_uid_gid(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)364 static int oci_uid_gid(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
365 uid_t *uid = userdata, u;
366 uint64_t k;
367
368 assert(uid);
369 assert_cc(sizeof(uid_t) == sizeof(gid_t));
370
371 k = json_variant_unsigned(v);
372 u = (uid_t) k;
373 if ((uint64_t) u != k)
374 return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
375 "UID/GID out of range: %ji", k);
376
377 if (!uid_is_valid(u))
378 return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
379 "UID/GID is not valid: " UID_FMT, u);
380
381 *uid = u;
382 return 0;
383 }
384
oci_supplementary_gids(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)385 static int oci_supplementary_gids(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
386 Settings *s = userdata;
387 JsonVariant *e;
388 int r;
389
390 assert(s);
391
392 JSON_VARIANT_ARRAY_FOREACH(e, v) {
393 gid_t gid, *a;
394
395 if (!json_variant_is_unsigned(e))
396 return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
397 "Supplementary GID entry is not a UID.");
398
399 r = oci_uid_gid(name, e, flags, &gid);
400 if (r < 0)
401 return r;
402
403 a = reallocarray(s->supplementary_gids, s->n_supplementary_gids + 1, sizeof(gid_t));
404 if (!a)
405 return log_oom();
406
407 s->supplementary_gids = a;
408 s->supplementary_gids[s->n_supplementary_gids++] = gid;
409 }
410
411 return 0;
412 }
413
oci_user(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)414 static int oci_user(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
415 static const JsonDispatch table[] = {
416 { "uid", JSON_VARIANT_UNSIGNED, oci_uid_gid, offsetof(Settings, uid), JSON_MANDATORY },
417 { "gid", JSON_VARIANT_UNSIGNED, oci_uid_gid, offsetof(Settings, gid), JSON_MANDATORY },
418 { "additionalGids", JSON_VARIANT_ARRAY, oci_supplementary_gids, 0, 0 },
419 {}
420 };
421
422 return json_dispatch(v, table, oci_unexpected, flags, userdata);
423 }
424
oci_process(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)425 static int oci_process(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
426
427 static const JsonDispatch table[] = {
428 { "terminal", JSON_VARIANT_BOOLEAN, oci_terminal, 0, 0 },
429 { "consoleSize", JSON_VARIANT_OBJECT, oci_console_size, 0, 0 },
430 { "cwd", JSON_VARIANT_STRING, oci_absolute_path, offsetof(Settings, working_directory), 0 },
431 { "env", JSON_VARIANT_ARRAY, oci_env, offsetof(Settings, environment), 0 },
432 { "args", JSON_VARIANT_ARRAY, oci_args, offsetof(Settings, parameters), 0 },
433 { "rlimits", JSON_VARIANT_ARRAY, oci_rlimits, 0, 0 },
434 { "apparmorProfile", JSON_VARIANT_STRING, oci_unsupported, 0, JSON_PERMISSIVE },
435 { "capabilities", JSON_VARIANT_OBJECT, oci_capabilities, 0, 0 },
436 { "noNewPrivileges", JSON_VARIANT_BOOLEAN, json_dispatch_boolean, offsetof(Settings, no_new_privileges), 0 },
437 { "oomScoreAdj", JSON_VARIANT_INTEGER, oci_oom_score_adj, 0, 0 },
438 { "selinuxLabel", JSON_VARIANT_STRING, oci_unsupported, 0, JSON_PERMISSIVE },
439 { "user", JSON_VARIANT_OBJECT, oci_user, 0, 0 },
440 {}
441 };
442
443 return json_dispatch(v, table, oci_unexpected, flags, userdata);
444 }
445
oci_root(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)446 static int oci_root(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
447 Settings *s = userdata;
448 int r;
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
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;
471 }
472
oci_hostname(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)473 static 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
481 if (!hostname_is_valid(n, 0))
482 return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
483 "Hostname string is not a valid hostname: %s", n);
484
485 return free_and_strdup_warn(&s->hostname, n);
486 }
487
oci_exclude_mount(const char * path)488 static 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
526 typedef struct oci_mount_data {
527 char *destination;
528 char *source;
529 char *type;
530 char **options;
531 } oci_mount_data;
532
cleanup_oci_mount_data(oci_mount_data * data)533 static 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
oci_mounts(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)540 static 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) {
548 static const JsonDispatch table[] = {
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 },
553 {}
554 };
555
556 _cleanup_free_ char *joined_options = NULL;
557 CustomMount *m;
558 _cleanup_(cleanup_oci_mount_data) oci_mount_data data = {};
559
560 r = json_dispatch(e, table, oci_unexpected, flags, &data);
561 if (r < 0)
562 return r;
563
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);
567
568 if (oci_exclude_mount(data.destination))
569 continue;
570
571 if (data.options) {
572 joined_options = strv_join(data.options, ",");
573 if (!joined_options)
574 return log_oom();
575 }
576
577 if (!data.type || streq(data.type, "bind")) {
578 if (data.source && !path_is_absolute(data.source)) {
579 char *joined;
580
581 joined = path_join(s->bundle, data.source);
582 if (!joined)
583 return log_oom();
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);
593 if (!m)
594 return log_oom();
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);
600 }
601
602 return 0;
603 }
604
oci_namespace_type(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)605 static 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;
628 else
629 return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
630 "Unknown cgroup type, refusing: %s", n);
631
632 return 0;
633 }
634
oci_namespaces(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)635 static 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) {
664 free(data.path);
665 return json_log(e, flags, SYNTHETIC_ERRNO(EOPNOTSUPP),
666 "Specifying namespace path for non-network namespace is not supported.");
667 }
668
669 if (s->network_namespace_path) {
670 free(data.path);
671 return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL),
672 "Network namespace path specified more than once, refusing.");
673 }
674
675 free(s->network_namespace_path);
676 s->network_namespace_path = data.path;
677 }
678
679 if (FLAGS_SET(n, data.type))
680 return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL),
681 "Duplicate namespace specification, refusing.");
682
683 n |= data.type;
684 }
685
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.");
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
oci_uid_gid_range(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)699 static int oci_uid_gid_range(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
700 uid_t *uid = userdata, u;
701 uint64_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
707 * than a specific UID, and hence UID_INVALID has no special significance. OTOH a range of zero makes no
708 * sense. */
709
710 k = json_variant_unsigned(v);
711 u = (uid_t) k;
712 if ((uint64_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.");
718
719 *uid = u;
720 return 0;
721 }
722
oci_uid_gid_mappings(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)723 static 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
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.");
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 ||
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.");
764
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.");
768
769 if (data.range < 0x10000)
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.");
772
773 if (s->uid_range != UID_INVALID &&
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.");
777
778 s->uid_shift = data.host_id;
779 s->uid_range = data.range;
780
781 return 0;
782 }
783
oci_device_type(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)784 static 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;
797 else
798 return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
799 "Unknown device type: %s", t);
800
801 return 0;
802 }
803
oci_device_major(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)804 static int oci_device_major(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
805 unsigned *u = userdata;
806 uint64_t k;
807
808 assert_se(u);
809
810 k = json_variant_unsigned(v);
811 if (!DEVICE_MAJOR_VALID(k))
812 return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
813 "Device major %ji out of range.", k);
814
815 *u = (unsigned) k;
816 return 0;
817 }
818
oci_device_minor(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)819 static int oci_device_minor(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
820 unsigned *u = userdata;
821 uint64_t k;
822
823 assert_se(u);
824
825 k = json_variant_unsigned(v);
826 if (!DEVICE_MINOR_VALID(k))
827 return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
828 "Device minor %ji out of range.", k);
829
830 *u = (unsigned) k;
831 return 0;
832 }
833
oci_device_file_mode(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)834 static int oci_device_file_mode(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
835 mode_t *mode = userdata, m;
836 uint64_t k;
837
838 assert(mode);
839
840 k = json_variant_unsigned(v);
841 m = (mode_t) k;
842
843 if ((m & ~07777) != 0 || (uint64_t) m != k)
844 return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
845 "fileMode out of range, refusing.");
846
847 *mode = m;
848 return 0;
849 }
850
oci_devices(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)851 static 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,
883 .major = UINT_MAX,
884 .minor = UINT_MAX,
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
895 if (node->major == UINT_MAX || node->minor == UINT_MAX) {
896 r = json_log(e, flags, SYNTHETIC_ERRNO(EINVAL),
897 "Major/minor required when device node is device node");
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
oci_cgroups_path(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)936 static 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
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);
957
958 free_and_replace(s->slice, slice);
959 return 0;
960 }
961
oci_cgroup_device_type(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)962 static 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;
972 else
973 return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
974 "Control group device type unknown: %s", n);
975
976 return 0;
977 }
978
979 struct 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
oci_cgroup_device_access(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)989 static 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
997 for (i = 0; s[i]; i++)
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;
1004 else
1005 return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
1006 "Unknown device access character '%c'.", s[i]);
1007
1008 d->r = r;
1009 d->w = w;
1010 d->m = m;
1011
1012 return 0;
1013 }
1014
oci_cgroup_devices(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)1015 static 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 = {
1029 .major = UINT_MAX,
1030 .minor = UINT_MAX,
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) {
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 */
1054
1055 if (!data.r || !data.w || !data.m || data.type != 0 || data.major != UINT_MAX || data.minor != UINT_MAX)
1056 json_log(v, flags|JSON_WARNING, 0, "Devices cgroup allow list with arbitrary 'allow' entries not supported, ignoring.");
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) {
1063 json_log(v, flags|LOG_WARNING, 0, "Device cgroup allow list entry with no effect found, ignoring.");
1064 continue;
1065 }
1066
1067 if (data.minor != UINT_MAX && data.major == UINT_MAX)
1068 return json_log(v, flags, SYNTHETIC_ERRNO(EOPNOTSUPP),
1069 "Device cgroup allow list entries with minors but no majors not supported.");
1070
1071 if (data.major != UINT_MAX && data.type == 0)
1072 return json_log(v, flags, SYNTHETIC_ERRNO(EOPNOTSUPP),
1073 "Device cgroup allow list entries with majors but no device node type not supported.");
1074
1075 if (data.type == 0) {
1076 if (data.r && data.w && data.m) /* a catchall allow list entry means we are looking at a noop */
1077 noop = true;
1078 else
1079 return json_log(v, flags, SYNTHETIC_ERRNO(EOPNOTSUPP),
1080 "Device cgroup allow list entries with no type not supported.");
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
1119 if (list[i].minor == UINT_MAX) {
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
1129 if (list[i].major == UINT_MAX) {
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 {
1139 assert(list[i].major != UINT_MAX); /* If a minor is specified, then a major also needs to be specified */
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
oci_cgroup_memory_limit(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)1176 static int oci_cgroup_memory_limit(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
1177 uint64_t *m = userdata;
1178 uint64_t k;
1179
1180 assert(m);
1181
1182 if (json_variant_is_negative(v)) {
1183 *m = UINT64_MAX;
1184 return 0;
1185 }
1186
1187 if (!json_variant_is_unsigned(v))
1188 return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
1189 "Memory limit is not an unsigned integer");
1190
1191 k = json_variant_unsigned(v);
1192 if (k >= UINT64_MAX)
1193 return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
1194 "Memory limit too large: %ji", k);
1195
1196 *m = (uint64_t) k;
1197 return 0;
1198 }
1199
oci_cgroup_memory(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)1200 static 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
1269 struct cpu_data {
1270 uint64_t shares;
1271 uint64_t quota;
1272 uint64_t period;
1273 CPUSet cpu_set;
1274 };
1275
oci_cgroup_cpu_shares(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)1276 static int oci_cgroup_cpu_shares(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
1277 uint64_t *u = userdata;
1278 uint64_t k;
1279
1280 assert(u);
1281
1282 k = json_variant_unsigned(v);
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.");
1286
1287 *u = (uint64_t) k;
1288 return 0;
1289 }
1290
oci_cgroup_cpu_quota(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)1291 static int oci_cgroup_cpu_quota(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
1292 uint64_t *u = userdata;
1293 uint64_t k;
1294
1295 assert(u);
1296
1297 k = json_variant_unsigned(v);
1298 if (k <= 0 || k >= UINT64_MAX)
1299 return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
1300 "period/quota value out of range.");
1301
1302 *u = (uint64_t) k;
1303 return 0;
1304 }
1305
oci_cgroup_cpu_cpus(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)1306 static int oci_cgroup_cpu_cpus(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
1307 struct cpu_data *data = userdata;
1308 CPUSet set;
1309 const char *n;
1310 int r;
1311
1312 assert(data);
1313
1314 assert_se(n = json_variant_string(v));
1315
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);
1319
1320 cpu_set_reset(&data->cpu_set);
1321 data->cpu_set = set;
1322
1323 return 0;
1324 }
1325
oci_cgroup_cpu(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)1326 static 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) {
1350 cpu_set_reset(&data.cpu_set);
1351 return r;
1352 }
1353
1354 cpu_set_reset(&s->cpu_set);
1355 s->cpu_set = data.cpu_set;
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
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.");
1379
1380 return 0;
1381 }
1382
oci_cgroup_block_io_weight(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)1383 static int oci_cgroup_block_io_weight(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
1384 Settings *s = userdata;
1385 uint64_t k;
1386 int r;
1387
1388 assert(s);
1389
1390 k = json_variant_unsigned(v);
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.");
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
oci_cgroup_block_io_weight_device(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)1406 static 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 uint64_t weight;
1418 } data = {
1419 .major = UINT_MAX,
1420 .minor = UINT_MAX,
1421 .weight = UINT64_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 },
1427 { "weight", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(struct device_data, weight), 0 },
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 == UINT64_MAX)
1439 continue;
1440
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.");
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
oci_cgroup_block_io_throttle(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)1461 static 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 uint64_t rate;
1479 } data = {
1480 .major = UINT_MAX,
1481 .minor = UINT_MAX,
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 },
1487 { "rate", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(struct device_data, rate), JSON_MANDATORY },
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
1497 if (data.rate >= UINT64_MAX)
1498 return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
1499 "Block I/O device rate out of range.");
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
oci_cgroup_block_io(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)1517 static 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
oci_cgroup_pids(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)1533 static 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 {
1554 if (!json_variant_is_unsigned(k))
1555 return json_log(k, flags, SYNTHETIC_ERRNO(EINVAL),
1556 "pids limit not unsigned integer, refusing.");
1557
1558 m = (uint64_t) json_variant_unsigned(k);
1559
1560 if ((uint64_t) m != json_variant_unsigned(k))
1561 return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
1562 "pids limit out of range, refusing.");
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
oci_resources(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)1576 static 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
sysctl_key_valid(const char * s)1593 static 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
oci_sysctl(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)1624 static int oci_sysctl(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
1625 Settings *s = userdata;
1626 JsonVariant *w;
1627 const char *k;
1628 int r;
1629
1630 assert(s);
1631
1632 JSON_VARIANT_OBJECT_FOREACH(k, w, v) {
1633 const char *m;
1634
1635 if (!json_variant_is_string(w))
1636 return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
1637 "sysctl parameter is not a string, refusing.");
1638
1639 assert_se(m = json_variant_string(w));
1640
1641 if (sysctl_key_valid(k))
1642 return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
1643 "sysctl key invalid, refusing: %s", k);
1644
1645 r = strv_extend_strv(&s->sysctl, STRV_MAKE(k, m), false);
1646 if (r < 0)
1647 return log_oom();
1648 }
1649
1650 return 0;
1651 }
1652
1653 #if HAVE_SECCOMP
oci_seccomp_action_from_string(const char * name,uint32_t * ret)1654 static 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[] = {
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
1669 #ifdef SCMP_ACT_LOG
1670 { "SCMP_ACT_LOG", SCMP_ACT_LOG },
1671 #endif
1672 { "SCMP_ACT_TRAP", SCMP_ACT_TRAP },
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
oci_seccomp_arch_from_string(const char * name,uint32_t * ret)1689 static 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 },
1713 #ifdef SCMP_ARCH_RISCV64
1714 { "SCMP_ARCH_RISCV64", SCMP_ARCH_RISCV64 },
1715 #endif
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
oci_seccomp_compare_from_string(const char * name,enum scmp_compare * ret)1734 static 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
oci_seccomp_archs(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)1760 static 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
1770 if (!json_variant_is_string(e))
1771 return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL),
1772 "Architecture entry is not a string");
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
1788 struct syscall_rule {
1789 char **names;
1790 uint32_t action;
1791 struct scmp_arg_cmp *arguments;
1792 size_t n_arguments;
1793 };
1794
syscall_rule_free(struct syscall_rule * rule)1795 static void syscall_rule_free(struct syscall_rule *rule) {
1796 assert(rule);
1797
1798 strv_free(rule->names);
1799 free(rule->arguments);
1800 };
1801
oci_seccomp_action(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)1802 static 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
oci_seccomp_op(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)1815 static 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
oci_seccomp_args(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)1828 static 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
oci_seccomp_syscalls(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)1879 static 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 = {
1893 .action = UINT32_MAX,
1894 };
1895
1896 r = json_dispatch(e, table, oci_unexpected, flags, &rule);
1897 if (r < 0)
1898 goto fail_rule;
1899
1900 if (strv_isempty(rule.names)) {
1901 json_log(e, flags, 0, "System call name list is empty.");
1902 r = -EINVAL;
1903 goto fail_rule;
1904 }
1905
1906 STRV_FOREACH(i, rule.names) {
1907 int nr;
1908
1909 nr = seccomp_syscall_resolve_name(*i);
1910 if (nr == __NR_SCMP_ERROR) {
1911 log_debug("Unknown syscall %s, skipping.", *i);
1912 continue;
1913 }
1914
1915 r = seccomp_rule_add_array(sc, rule.action, nr, rule.n_arguments, rule.arguments);
1916 if (r < 0)
1917 goto fail_rule;
1918 }
1919
1920 syscall_rule_free(&rule);
1921 continue;
1922
1923 fail_rule:
1924 syscall_rule_free(&rule);
1925 return r;
1926 }
1927
1928 return 0;
1929 }
1930 #endif
1931
oci_seccomp(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)1932 static int oci_seccomp(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
1933
1934 #if HAVE_SECCOMP
1935 static const JsonDispatch table[] = {
1936 { "defaultAction", JSON_VARIANT_STRING, NULL, 0, JSON_MANDATORY },
1937 { "architectures", JSON_VARIANT_ARRAY, oci_seccomp_archs, 0, 0 },
1938 { "syscalls", JSON_VARIANT_ARRAY, oci_seccomp_syscalls, 0, 0 },
1939 {}
1940 };
1941
1942 _cleanup_(seccomp_releasep) scmp_filter_ctx sc = NULL;
1943 Settings *s = userdata;
1944 JsonVariant *def;
1945 uint32_t d;
1946 int r;
1947
1948 assert(s);
1949
1950 def = json_variant_by_key(v, "defaultAction");
1951 if (!def)
1952 return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL), "defaultAction element missing.");
1953
1954 if (!json_variant_is_string(def))
1955 return json_log(def, flags, SYNTHETIC_ERRNO(EINVAL), "defaultAction is not a string.");
1956
1957 r = oci_seccomp_action_from_string(json_variant_string(def), &d);
1958 if (r < 0)
1959 return json_log(def, flags, r, "Unknown default action: %s", json_variant_string(def));
1960
1961 sc = seccomp_init(d);
1962 if (!sc)
1963 return json_log(v, flags, SYNTHETIC_ERRNO(ENOMEM), "Couldn't allocate seccomp object.");
1964
1965 r = json_dispatch(v, table, oci_unexpected, flags, sc);
1966 if (r < 0)
1967 return r;
1968
1969 seccomp_release(s->seccomp);
1970 s->seccomp = TAKE_PTR(sc);
1971 return 0;
1972 #else
1973 return json_log(v, flags, SYNTHETIC_ERRNO(EOPNOTSUPP), "libseccomp support not enabled, can't parse seccomp object.");
1974 #endif
1975 }
1976
oci_rootfs_propagation(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)1977 static int oci_rootfs_propagation(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
1978 const char *s;
1979
1980 s = json_variant_string(v);
1981
1982 if (streq(s, "shared"))
1983 return 0;
1984
1985 json_log(v, flags|JSON_DEBUG, 0, "Ignoring rootfsPropagation setting '%s'.", s);
1986 return 0;
1987 }
1988
oci_masked_paths(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)1989 static int oci_masked_paths(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
1990 Settings *s = userdata;
1991 JsonVariant *e;
1992
1993 assert(s);
1994
1995 JSON_VARIANT_ARRAY_FOREACH(e, v) {
1996 _cleanup_free_ char *destination = NULL;
1997 CustomMount *m;
1998 const char *p;
1999
2000 if (!json_variant_is_string(e))
2001 return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
2002 "Path is not a string, refusing.");
2003
2004 assert_se(p = json_variant_string(e));
2005
2006 if (!path_is_absolute(p))
2007 return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
2008 "Path is not absolute, refusing: %s", p);
2009
2010 if (oci_exclude_mount(p))
2011 continue;
2012
2013 destination = strdup(p);
2014 if (!destination)
2015 return log_oom();
2016
2017 m = custom_mount_add(&s->custom_mounts, &s->n_custom_mounts, CUSTOM_MOUNT_INACCESSIBLE);
2018 if (!m)
2019 return log_oom();
2020
2021 m->destination = TAKE_PTR(destination);
2022
2023 /* The spec doesn't say this, but apparently pre-existing implementations are lenient towards
2024 * non-existing paths to mask. Let's hence be too. */
2025 m->graceful = true;
2026 }
2027
2028 return 0;
2029 }
2030
oci_readonly_paths(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)2031 static int oci_readonly_paths(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
2032 Settings *s = userdata;
2033 JsonVariant *e;
2034
2035 assert(s);
2036
2037 JSON_VARIANT_ARRAY_FOREACH(e, v) {
2038 _cleanup_free_ char *source = NULL, *destination = NULL;
2039 CustomMount *m;
2040 const char *p;
2041
2042 if (!json_variant_is_string(e))
2043 return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
2044 "Path is not a string, refusing.");
2045
2046 assert_se(p = json_variant_string(e));
2047
2048 if (!path_is_absolute(p))
2049 return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
2050 "Path is not absolute, refusing: %s", p);
2051
2052 if (oci_exclude_mount(p))
2053 continue;
2054
2055 source = strjoin("+", p);
2056 if (!source)
2057 return log_oom();
2058
2059 destination = strdup(p);
2060 if (!destination)
2061 return log_oom();
2062
2063 m = custom_mount_add(&s->custom_mounts, &s->n_custom_mounts, CUSTOM_MOUNT_BIND);
2064 if (!m)
2065 return log_oom();
2066
2067 m->source = TAKE_PTR(source);
2068 m->destination = TAKE_PTR(destination);
2069 m->read_only = true;
2070 }
2071
2072 return 0;
2073 }
2074
oci_linux(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)2075 static int oci_linux(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
2076
2077 static const JsonDispatch table[] = {
2078 { "namespaces", JSON_VARIANT_ARRAY, oci_namespaces, 0, 0 },
2079 { "uidMappings", JSON_VARIANT_ARRAY, oci_uid_gid_mappings, 0, 0 },
2080 { "gidMappings", JSON_VARIANT_ARRAY, oci_uid_gid_mappings, 0, 0 },
2081 { "devices", JSON_VARIANT_ARRAY, oci_devices, 0, 0 },
2082 { "cgroupsPath", JSON_VARIANT_STRING, oci_cgroups_path, 0, 0 },
2083 { "resources", JSON_VARIANT_OBJECT, oci_resources, 0, 0 },
2084 { "intelRdt", JSON_VARIANT_OBJECT, oci_unsupported, 0, JSON_PERMISSIVE },
2085 { "sysctl", JSON_VARIANT_OBJECT, oci_sysctl, 0, 0 },
2086 { "seccomp", JSON_VARIANT_OBJECT, oci_seccomp, 0, 0 },
2087 { "rootfsPropagation", JSON_VARIANT_STRING, oci_rootfs_propagation, 0, 0 },
2088 { "maskedPaths", JSON_VARIANT_ARRAY, oci_masked_paths, 0, 0 },
2089 { "readonlyPaths", JSON_VARIANT_ARRAY, oci_readonly_paths, 0, 0 },
2090 { "mountLabel", JSON_VARIANT_STRING, oci_unsupported, 0, JSON_PERMISSIVE },
2091 {}
2092 };
2093
2094 return json_dispatch(v, table, oci_unexpected, flags, userdata);
2095 }
2096
oci_hook_timeout(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)2097 static int oci_hook_timeout(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
2098 usec_t *u = userdata;
2099 uint64_t k;
2100
2101 k = json_variant_unsigned(v);
2102 if (k == 0 || k > (UINT64_MAX-1)/USEC_PER_SEC)
2103 return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
2104 "Hook timeout value out of range.");
2105
2106 *u = k * USEC_PER_SEC;
2107 return 0;
2108 }
2109
oci_hooks_array(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)2110 static int oci_hooks_array(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
2111 Settings *s = userdata;
2112 JsonVariant *e;
2113 int r;
2114
2115 assert(s);
2116
2117 JSON_VARIANT_ARRAY_FOREACH(e, v) {
2118
2119 static const JsonDispatch table[] = {
2120 { "path", JSON_VARIANT_STRING, oci_absolute_path, offsetof(OciHook, path), JSON_MANDATORY },
2121 { "args", JSON_VARIANT_ARRAY, oci_args, offsetof(OciHook, args), 0 },
2122 { "env", JSON_VARIANT_ARRAY, oci_env, offsetof(OciHook, env), 0 },
2123 { "timeout", JSON_VARIANT_UNSIGNED, oci_hook_timeout, offsetof(OciHook, timeout), 0 },
2124 {}
2125 };
2126
2127 OciHook *a, **array, *new_item;
2128 size_t *n_array;
2129
2130 if (streq(name, "prestart")) {
2131 array = &s->oci_hooks_prestart;
2132 n_array = &s->n_oci_hooks_prestart;
2133 } else if (streq(name, "poststart")) {
2134 array = &s->oci_hooks_poststart;
2135 n_array = &s->n_oci_hooks_poststart;
2136 } else {
2137 assert(streq(name, "poststop"));
2138 array = &s->oci_hooks_poststop;
2139 n_array = &s->n_oci_hooks_poststop;
2140 }
2141
2142 a = reallocarray(*array, *n_array + 1, sizeof(OciHook));
2143 if (!a)
2144 return log_oom();
2145
2146 *array = a;
2147 new_item = a + *n_array;
2148
2149 *new_item = (OciHook) {
2150 .timeout = USEC_INFINITY,
2151 };
2152
2153 r = json_dispatch(e, table, oci_unexpected, flags, userdata);
2154 if (r < 0) {
2155 free(new_item->path);
2156 strv_free(new_item->args);
2157 strv_free(new_item->env);
2158 return r;
2159 }
2160
2161 (*n_array) ++;
2162 }
2163
2164 return 0;
2165 }
2166
oci_hooks(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)2167 static int oci_hooks(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
2168
2169 static const JsonDispatch table[] = {
2170 { "prestart", JSON_VARIANT_OBJECT, oci_hooks_array, 0, 0 },
2171 { "poststart", JSON_VARIANT_OBJECT, oci_hooks_array, 0, 0 },
2172 { "poststop", JSON_VARIANT_OBJECT, oci_hooks_array, 0, 0 },
2173 {}
2174 };
2175
2176 return json_dispatch(v, table, oci_unexpected, flags, userdata);
2177 }
2178
oci_annotations(const char * name,JsonVariant * v,JsonDispatchFlags flags,void * userdata)2179 static int oci_annotations(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
2180 JsonVariant *w;
2181 const char *k;
2182
2183 JSON_VARIANT_OBJECT_FOREACH(k, w, v) {
2184
2185 if (isempty(k))
2186 return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
2187 "Annotation with empty key, refusing.");
2188
2189 if (!json_variant_is_string(w))
2190 return json_log(w, flags, SYNTHETIC_ERRNO(EINVAL),
2191 "Annotation has non-string value, refusing.");
2192
2193 json_log(w, flags|JSON_DEBUG, 0, "Ignoring annotation '%s' with value '%s'.", k, json_variant_string(w));
2194 }
2195
2196 return 0;
2197 }
2198
oci_load(FILE * f,const char * bundle,Settings ** ret)2199 int oci_load(FILE *f, const char *bundle, Settings **ret) {
2200
2201 static const JsonDispatch table[] = {
2202 { "ociVersion", JSON_VARIANT_STRING, NULL, 0, JSON_MANDATORY },
2203 { "process", JSON_VARIANT_OBJECT, oci_process, 0, 0 },
2204 { "root", JSON_VARIANT_OBJECT, oci_root, 0, 0 },
2205 { "hostname", JSON_VARIANT_STRING, oci_hostname, 0, 0 },
2206 { "mounts", JSON_VARIANT_ARRAY, oci_mounts, 0, 0 },
2207 { "linux", JSON_VARIANT_OBJECT, oci_linux, 0, 0 },
2208 { "hooks", JSON_VARIANT_OBJECT, oci_hooks, 0, 0 },
2209 { "annotations", JSON_VARIANT_OBJECT, oci_annotations, 0, 0 },
2210 {}
2211 };
2212
2213 _cleanup_(json_variant_unrefp) JsonVariant *oci = NULL;
2214 _cleanup_(settings_freep) Settings *s = NULL;
2215 unsigned line = 0, column = 0;
2216 JsonVariant *v;
2217 const char *path;
2218 int r;
2219
2220 assert_se(bundle);
2221
2222 path = strjoina(bundle, "/config.json");
2223
2224 r = json_parse_file(f, path, 0, &oci, &line, &column);
2225 if (r < 0) {
2226 if (line != 0 && column != 0)
2227 return log_error_errno(r, "Failed to parse '%s' at %u:%u: %m", path, line, column);
2228 else
2229 return log_error_errno(r, "Failed to parse '%s': %m", path);
2230 }
2231
2232 v = json_variant_by_key(oci, "ociVersion");
2233 if (!v)
2234 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2235 "JSON file '%s' is not an OCI bundle configuration file. Refusing.",
2236 path);
2237 if (!streq_ptr(json_variant_string(v), "1.0.0"))
2238 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2239 "OCI bundle version not supported: %s",
2240 strna(json_variant_string(v)));
2241
2242 // {
2243 // _cleanup_free_ char *formatted = NULL;
2244 // assert_se(json_variant_format(oci, JSON_FORMAT_PRETTY|JSON_FORMAT_COLOR, &formatted) >= 0);
2245 // fputs(formatted, stdout);
2246 // }
2247
2248 s = settings_new();
2249 if (!s)
2250 return log_oom();
2251
2252 s->start_mode = START_PID1;
2253 s->resolv_conf = RESOLV_CONF_OFF;
2254 s->link_journal = LINK_NO;
2255 s->timezone = TIMEZONE_OFF;
2256
2257 s->bundle = strdup(bundle);
2258 if (!s->bundle)
2259 return log_oom();
2260
2261 r = json_dispatch(oci, table, oci_unexpected, 0, s);
2262 if (r < 0)
2263 return r;
2264
2265 if (s->properties) {
2266 r = sd_bus_message_seal(s->properties, 0, 0);
2267 if (r < 0)
2268 return log_error_errno(r, "Cannot seal properties bus message: %m");
2269 }
2270
2271 *ret = TAKE_PTR(s);
2272 return 0;
2273 }
2274