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