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