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