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