• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 14895667988

07 May 2025 08:57PM UTC coverage: 72.225% (-0.007%) from 72.232%
14895667988

push

github

yuwata
network: log_link_message_debug_errno() automatically append %m if necessary

Follow-up for d28746ef5.
Fixes CID#1609753.

0 of 1 new or added line in 1 file covered. (0.0%)

20297 existing lines in 338 files now uncovered.

297407 of 411780 relevant lines covered (72.22%)

695716.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.93
/src/nspawn/nspawn-oci.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <linux/oom.h>
4

5
#include "sd-json.h"
6

7
#include "alloc-util.h"
8
#include "bus-util.h"
9
#include "cap-list.h"
10
#include "cpu-set-util.h"
11
#include "device-util.h"
12
#include "devnum-util.h"
13
#include "env-util.h"
14
#include "format-util.h"
15
#include "fs-util.h"
16
#include "hostname-util.h"
17
#include "json-util.h"
18
#include "missing_sched.h"
19
#include "nspawn-oci.h"
20
#include "path-util.h"
21
#include "rlimit-util.h"
22
#include "seccomp-util.h"
23
#include "stdio-util.h"
24
#include "string-util.h"
25
#include "strv.h"
26
#include "user-util.h"
27

28
/* TODO:
29
 * OCI runtime tool implementation
30
 * hooks
31
 *
32
 * Spec issues:
33
 *
34
 * How is RLIM_INFINITY supposed to be encoded?
35
 * configured effective caps is bullshit, as execv() corrupts it anyway
36
 * pipes bind mounted is *very* different from pipes newly created, comments regarding bind mount or not are bogus
37
 * annotation values structured? or string?
38
 * configurable file system namespace path, but then also root path? wtf?
39
 * apply sysctl inside of the container? or outside?
40
 * how is unlimited pids tasks limit to be encoded?
41
 * what are the defaults for caps if not specified?
42
 * 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
43
 * 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
44
 * 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
45
 * if type of mount is left out, what shall be assumed? "bind"?
46
 * readonly mounts is entirely redundant?
47
 * should escaping be applied when joining mount options with ","?
48
 * devices cgroup support is bogus, "allow" and "deny" on the kernel level is about adding/removing entries, not about access
49
 * spec needs to say that "rwm" devices cgroup combination can't be the empty string
50
 * cgrouspv1 crap: kernel, kernelTCP, swappiness, disableOOMKiller, swap, devices, leafWeight
51
 * general: it shouldn't leak lower level abstractions this obviously
52
 * unmanagable cgroups stuff: realtimeRuntime/realtimePeriod
53
 * needs to say what happense when some option is not specified, i.e. which defaults apply
54
 * no architecture? no personality?
55
 * seccomp example and logic is simply broken: there's no constant "SCMP_ACT_ERRNO".
56
 * spec should say what to do with unknown props
57
 * /bin/mount regarding NFS and FUSE required?
58
 * what does terminal=false mean?
59
 * sysctl inside or outside? allow-listing?
60
 * swapiness typo -> swappiness
61
 *
62
 * Unsupported:
63
 *
64
 * apparmorProfile
65
 * selinuxLabel + mountLabel
66
 * hugepageLimits
67
 * network
68
 * rdma
69
 * intelRdt
70
 * swappiness, disableOOMKiller, kernel, kernelTCP, leafWeight (because it's dead, cgroupsv2 can't do it and hence systemd neither)
71
 *
72
 * Non-slice cgroup paths
73
 * Propagation that is not slave + shared
74
 * more than one uid/gid mapping, mappings with a container base != 0, or non-matching uid/gid mappings
75
 * device cgroups access = false items that are not catchall
76
 * device cgroups matches where minor is specified, but major isn't. similar where major is specified but char/block is not. also, any match that only has a type set that has less than "rwm" set. also, any entry that has none of rwm set.
77
 *
78
 */
79

80
static int oci_unexpected(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
2✔
81
        return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
2✔
82
                        "Unexpected OCI element '%s' of type '%s'.", name, sd_json_variant_type_to_string(sd_json_variant_type(v)));
83
}
84

85
static int oci_dispatch(sd_json_variant *v, const sd_json_dispatch_field table[], sd_json_dispatch_flags_t flags, void *userdata) {
151✔
86
        return sd_json_dispatch_full(v, table, oci_unexpected, flags, userdata, /* reterr_bad_field= */ NULL);
151✔
87
}
88

89
static int oci_unsupported(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
1✔
90
        return json_log(v, flags, SYNTHETIC_ERRNO(EOPNOTSUPP),
1✔
91
                        "Unsupported OCI element '%s' of type '%s'.", name, sd_json_variant_type_to_string(sd_json_variant_type(v)));
92
}
93

94
static int oci_terminal(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
1✔
95
        Settings *s = ASSERT_PTR(userdata);
1✔
96

97
        /* If not specified, or set to true, we'll default to either an interactive or a read-only
98
         * console. If specified as false, we'll forcibly move to "pipe" mode though. */
99
        s->console_mode = sd_json_variant_boolean(v) ? _CONSOLE_MODE_INVALID : CONSOLE_PIPE;
1✔
100
        return 0;
1✔
101
}
102

103
static int oci_console_dimension(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
2✔
104
        unsigned *u = ASSERT_PTR(userdata);
2✔
105
        uint64_t k;
2✔
106

107
        k = sd_json_variant_unsigned(variant);
2✔
108
        if (k == 0)
2✔
UNCOV
109
                return json_log(variant, flags, SYNTHETIC_ERRNO(ERANGE),
×
110
                                "Console size field '%s' is too small.", strna(name));
111
        if (k > USHRT_MAX) /* TIOCSWINSZ's struct winsize uses "unsigned short" for width and height */
2✔
UNCOV
112
                return json_log(variant, flags, SYNTHETIC_ERRNO(ERANGE),
×
113
                                "Console size field '%s' is too large.", strna(name));
114

115
        *u = (unsigned) k;
2✔
116
        return 0;
2✔
117
}
118

119
static int oci_console_size(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
1✔
120
        Settings *s = ASSERT_PTR(userdata);
1✔
121

122
        static const sd_json_dispatch_field table[] = {
1✔
123
                { "height", SD_JSON_VARIANT_UNSIGNED, oci_console_dimension, offsetof(Settings, console_height), SD_JSON_MANDATORY },
124
                { "width",  SD_JSON_VARIANT_UNSIGNED, oci_console_dimension, offsetof(Settings, console_width),  SD_JSON_MANDATORY },
125
                {}
126
        };
127

128
        return oci_dispatch(v, table, flags, s);
1✔
129
}
130

131
static int oci_env(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
4✔
132
        char ***l = ASSERT_PTR(userdata);
4✔
133
        sd_json_variant *e;
4✔
134
        int r;
4✔
135

136
        JSON_VARIANT_ARRAY_FOREACH(e, v) {
9✔
137
                const char *n;
7✔
138

139
                if (!sd_json_variant_is_string(e))
7✔
140
                        return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL),
2✔
141
                                        "Environment array contains non-string.");
142

143
                assert_se(n = sd_json_variant_string(e));
6✔
144

145
                if (!env_assignment_is_valid(n))
6✔
146
                        return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL),
1✔
147
                                        "Environment assignment not valid: %s", n);
148

149
                r = strv_extend(l, n);
5✔
150
                if (r < 0)
5✔
UNCOV
151
                        return log_oom();
×
152
        }
153

154
        return 0;
2✔
155
}
156

157
static int oci_args(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
8✔
158
        _cleanup_strv_free_ char **l = NULL;
8✔
159
        char ***value = ASSERT_PTR(userdata);
8✔
160
        int r;
8✔
161

162
        r = sd_json_variant_strv(v, &l);
8✔
163
        if (r < 0)
8✔
164
                return json_log(v, flags, r, "Cannot parse arguments as list of strings: %m");
1✔
165

166
        if (strv_isempty(l))
7✔
167
                return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
1✔
168
                                "Argument list empty, refusing.");
169

170
        if (isempty(l[0]))
6✔
171
                return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
1✔
172
                                "Executable name is empty, refusing.");
173

174
        return strv_free_and_replace(*value, l);
5✔
175
}
176

177
static int oci_rlimit_type(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
4✔
178
        const char *z;
4✔
179
        int *type = ASSERT_PTR(userdata);
4✔
180
        int t;
4✔
181

182
        z = startswith(sd_json_variant_string(v), "RLIMIT_");
4✔
183
        if (!z)
4✔
184
                return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
1✔
185
                                "rlimit entry's name does not begin with 'RLIMIT_', refusing: %s",
186
                                sd_json_variant_string(v));
187

188
        t = rlimit_from_string(z);
3✔
189
        if (t < 0)
3✔
190
                return json_log(v, flags, t,
1✔
191
                                "rlimit name unknown: %s", sd_json_variant_string(v));
192

193
        *type = t;
2✔
194
        return 0;
2✔
195
}
196

197
static int oci_rlimit_value(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
4✔
198
        rlim_t *value = ASSERT_PTR(userdata);
4✔
199
        rlim_t z;
4✔
200

201
        if (sd_json_variant_is_negative(v))
4✔
202
                z = RLIM_INFINITY;
203
        else {
204
                if (!sd_json_variant_is_unsigned(v))
4✔
UNCOV
205
                        return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
×
206
                                        "rlimits limit not unsigned, refusing.");
207

208
                z = (rlim_t) sd_json_variant_unsigned(v);
4✔
209

210
                if ((uint64_t) z != sd_json_variant_unsigned(v))
4✔
UNCOV
211
                        return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
×
212
                                        "rlimits limit out of range, refusing.");
213
        }
214

215
        *value = z;
4✔
216
        return 0;
4✔
217
}
218

219
static int oci_rlimits(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
3✔
220
        Settings *s = ASSERT_PTR(userdata);
3✔
221
        sd_json_variant *e;
3✔
222
        int r;
3✔
223

224
        JSON_VARIANT_ARRAY_FOREACH(e, v) {
5✔
225

226
                struct rlimit_data {
4✔
227
                        int type;
228
                        rlim_t soft;
229
                        rlim_t hard;
230
                } data = {
4✔
231
                        .type = -1,
232
                        .soft = RLIM_INFINITY,
233
                        .hard = RLIM_INFINITY,
234
                };
235

236
                static const sd_json_dispatch_field table[] = {
4✔
237
                        { "soft", SD_JSON_VARIANT_NUMBER, oci_rlimit_value, offsetof(struct rlimit_data, soft), SD_JSON_MANDATORY },
238
                        { "hard", SD_JSON_VARIANT_NUMBER, oci_rlimit_value, offsetof(struct rlimit_data, hard), SD_JSON_MANDATORY },
239
                        { "type", SD_JSON_VARIANT_STRING, oci_rlimit_type,  offsetof(struct rlimit_data, type), SD_JSON_MANDATORY },
240
                        {}
241
                };
242

243
                r = oci_dispatch(e, table, flags, &data);
4✔
244
                if (r < 0)
4✔
245
                        return r;
2✔
246

247
                assert(data.type >= 0);
2✔
248
                assert(data.type < _RLIMIT_MAX);
2✔
249

250
                if (s->rlimit[data.type])
2✔
UNCOV
251
                        return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
×
252
                                        "rlimits array contains duplicate entry, refusing.");
253

254
                s->rlimit[data.type] = new(struct rlimit, 1);
2✔
255
                if (!s->rlimit[data.type])
2✔
UNCOV
256
                        return log_oom();
×
257

258
                *s->rlimit[data.type] = (struct rlimit) {
2✔
259
                        .rlim_cur = data.soft,
2✔
260
                        .rlim_max = data.hard,
2✔
261
                };
262

263
        }
264
        return 0;
1✔
265
}
266

267
static int oci_capability_array(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
7✔
268
        uint64_t *mask = ASSERT_PTR(userdata);
7✔
269
        uint64_t m = 0;
7✔
270
        sd_json_variant *e;
7✔
271

272
        JSON_VARIANT_ARRAY_FOREACH(e, v) {
19✔
273
                const char *n;
14✔
274
                int cap;
14✔
275

276
                if (!sd_json_variant_is_string(e))
14✔
277
                        return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
2✔
278
                                        "Entry in capabilities array is not a string.");
279

280
                assert_se(n = sd_json_variant_string(e));
13✔
281

282
                cap = capability_from_name(n);
13✔
283
                if (cap < 0)
13✔
284
                        return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
1✔
285
                                        "Unknown capability: %s", n);
286

287
                m |= UINT64_C(1) << cap;
12✔
288
        }
289

290
        if (*mask == UINT64_MAX)
5✔
291
                *mask = m;
5✔
292
        else
UNCOV
293
                *mask |= m;
×
294

295
        return 0;
296
}
297

298
static int oci_capabilities(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
3✔
299

300
        static const sd_json_dispatch_field table[] = {
3✔
301
                { "effective",   SD_JSON_VARIANT_ARRAY, oci_capability_array, offsetof(CapabilityQuintet, effective)   },
302
                { "bounding",    SD_JSON_VARIANT_ARRAY, oci_capability_array, offsetof(CapabilityQuintet, bounding)    },
303
                { "inheritable", SD_JSON_VARIANT_ARRAY, oci_capability_array, offsetof(CapabilityQuintet, inheritable) },
304
                { "permitted",   SD_JSON_VARIANT_ARRAY, oci_capability_array, offsetof(CapabilityQuintet, permitted)   },
305
                { "ambient",     SD_JSON_VARIANT_ARRAY, oci_capability_array, offsetof(CapabilityQuintet, ambient)     },
306
                {}
307
        };
308

309
        Settings *s = ASSERT_PTR(userdata);
3✔
310
        int r;
3✔
311

312
        r = oci_dispatch(v, table, flags, &s->full_capabilities);
3✔
313
        if (r < 0)
3✔
314
                return r;
315

316
        if (s->full_capabilities.bounding != UINT64_MAX) {
1✔
317
                s->capability = s->full_capabilities.bounding;
1✔
318
                s->drop_capability = ~s->full_capabilities.bounding;
1✔
319
        }
320

321
        return 0;
322
}
323

324
static int oci_oom_score_adj(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
1✔
325
        Settings *s = ASSERT_PTR(userdata);
1✔
326
        int64_t k;
1✔
327

328
        k = sd_json_variant_integer(v);
1✔
329
        if (k < OOM_SCORE_ADJ_MIN || k > OOM_SCORE_ADJ_MAX)
1✔
UNCOV
330
                return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
×
331
                                "oomScoreAdj value out of range: %" PRIi64, k);
332

333
        s->oom_score_adjust = (int) k;
1✔
334
        s->oom_score_adjust_set = true;
1✔
335

336
        return 0;
1✔
337
}
338

339
static int oci_supplementary_gids(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
1✔
340
        Settings *s = ASSERT_PTR(userdata);
1✔
341
        sd_json_variant *e;
1✔
342
        int r;
1✔
343

344
        JSON_VARIANT_ARRAY_FOREACH(e, v) {
3✔
345
                gid_t gid;
2✔
346

347
                if (!sd_json_variant_is_unsigned(e))
2✔
UNCOV
348
                        return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
×
349
                                        "Supplementary GID entry is not a UID.");
350

351
                r = sd_json_dispatch_uid_gid(name, e, flags, &gid);
2✔
352
                if (r < 0)
2✔
353
                        return r;
354

355
                if (!GREEDY_REALLOC(s->supplementary_gids, s->n_supplementary_gids + 1))
2✔
UNCOV
356
                        return log_oom();
×
357

358
                s->supplementary_gids[s->n_supplementary_gids++] = gid;
2✔
359
        }
360

361
        return 0;
1✔
362
}
363

364
static int oci_user(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
1✔
365

366
        static const sd_json_dispatch_field table[] = {
1✔
367
                { "uid",            SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(Settings, uid), SD_JSON_MANDATORY },
368
                { "gid",            SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(Settings, gid), SD_JSON_MANDATORY },
369
                { "additionalGids", SD_JSON_VARIANT_ARRAY,    oci_supplementary_gids,   0,                       0                 },
370
                {}
371
        };
372

373
        return oci_dispatch(v, table, flags, userdata);
1✔
374
}
375

376
static int oci_process(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
11✔
377

378
        static const sd_json_dispatch_field table[] = {
11✔
379
                { "terminal",        SD_JSON_VARIANT_BOOLEAN, oci_terminal,              0,                                     0                  },
380
                { "consoleSize",     SD_JSON_VARIANT_OBJECT,  oci_console_size,          0,                                     0                  },
381
                { "cwd",             SD_JSON_VARIANT_STRING,  json_dispatch_path,        offsetof(Settings, working_directory), 0                  },
382
                { "env",             SD_JSON_VARIANT_ARRAY,   oci_env,                   offsetof(Settings, environment),       0                  },
383
                { "args",            SD_JSON_VARIANT_ARRAY,   oci_args,                  offsetof(Settings, parameters),        0                  },
384
                { "rlimits",         SD_JSON_VARIANT_ARRAY,   oci_rlimits,               0,                                     0                  },
385
                { "apparmorProfile", SD_JSON_VARIANT_STRING,  oci_unsupported,           0,                                     SD_JSON_PERMISSIVE },
386
                { "capabilities",    SD_JSON_VARIANT_OBJECT,  oci_capabilities,          0,                                     0                  },
387
                { "noNewPrivileges", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(Settings, no_new_privileges), 0                  },
388
                { "oomScoreAdj",     SD_JSON_VARIANT_INTEGER, oci_oom_score_adj,         0,                                     0                  },
389
                { "selinuxLabel",    SD_JSON_VARIANT_STRING,  oci_unsupported,           0,                                     SD_JSON_PERMISSIVE },
390
                { "user",            SD_JSON_VARIANT_OBJECT,  oci_user,                  0,                                     0                  },
391
                {}
392
        };
393

394
        return oci_dispatch(v, table, flags, userdata);
11✔
395
}
396

397
static int oci_root(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
34✔
398
        Settings *s = ASSERT_PTR(userdata);
34✔
399
        int r;
34✔
400

401
        static const sd_json_dispatch_field table[] = {
34✔
402
                { "path",     SD_JSON_VARIANT_STRING,  sd_json_dispatch_string,   offsetof(Settings, root)      },
403
                { "readonly", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(Settings, read_only) },
404
                {}
405
        };
406

407
        r = oci_dispatch(v, table, flags, s);
34✔
408
        if (r < 0)
34✔
409
                return r;
410

411
        if (s->root && !path_is_absolute(s->root)) {
34✔
412
                char *joined;
34✔
413

414
                joined = path_join(s->bundle, s->root);
34✔
415
                if (!joined)
34✔
UNCOV
416
                        return log_oom();
×
417

418
                free_and_replace(s->root, joined);
34✔
419
        }
420

421
        return 0;
422
}
423

424
static int oci_hostname(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
1✔
425
        Settings *s = ASSERT_PTR(userdata);
1✔
426
        const char *n;
1✔
427

428
        assert_se(n = sd_json_variant_string(v));
1✔
429

430
        if (!hostname_is_valid(n, 0))
1✔
UNCOV
431
                return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
×
432
                                "Hostname string is not a valid hostname: %s", n);
433

434
        return free_and_strdup_warn(&s->hostname, n);
1✔
435
}
436

437
static bool oci_exclude_mount(const char *path) {
12✔
438

439
        /* Returns "true" for all mounts we insist to mount on our own, and hence ignore the OCI data. */
440

441
        if (PATH_IN_SET(path,
12✔
442
                        "/dev",
443
                        "/dev/mqueue",
444
                        "/dev/pts",
445
                        "/dev/shm",
446
                        "/proc",
447
                        "/proc/acpi",
448
                        "/proc/apm",
449
                        "/proc/asound",
450
                        "/proc/bus",
451
                        "/proc/fs",
452
                        "/proc/irq",
453
                        "/proc/kallsyms",
454
                        "/proc/kcore",
455
                        "/proc/keys",
456
                        "/proc/scsi",
457
                        "/proc/sys",
458
                        "/proc/sys/net",
459
                        "/proc/sysrq-trigger",
460
                        "/proc/timer_list",
461
                        "/run",
462
                        "/sys",
463
                        "/sys",
464
                        "/sys/fs/selinux",
465
                        "/tmp"))
466
                return true;
2✔
467

468
        /* Similar, skip the whole /sys/fs/cgroups subtree */
469
        if (path_startswith(path, "/sys/fs/cgroup"))
10✔
UNCOV
470
                return true;
×
471

472
        return false;
473
}
474

475
typedef struct oci_mount_data {
476
        char *destination;
477
        char *source;
478
        char *type;
479
        char **options;
480
} oci_mount_data;
481

482
static void oci_mount_data_done(oci_mount_data *data) {
5✔
483
        assert(data);
5✔
484

485
        free(data->destination);
5✔
486
        free(data->source);
5✔
487
        free(data->type);
5✔
488
        strv_free(data->options);
5✔
489
}
5✔
490

491
static int oci_mounts(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
3✔
492
        Settings *s = ASSERT_PTR(userdata);
3✔
493
        sd_json_variant *e;
3✔
494
        int r;
3✔
495

496
        JSON_VARIANT_ARRAY_FOREACH(e, v) {
7✔
497
                static const sd_json_dispatch_field table[] = {
5✔
498
                        { "destination", SD_JSON_VARIANT_STRING, json_dispatch_path,      offsetof(oci_mount_data, destination), SD_JSON_MANDATORY },
499
                        { "source",      SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(oci_mount_data, source),      0                 },
500
                        { "options",     SD_JSON_VARIANT_ARRAY,  sd_json_dispatch_strv,   offsetof(oci_mount_data, options),     0,                },
501
                        { "type",        SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(oci_mount_data, type),        0                 },
502
                        {}
503
                };
504

505
                _cleanup_free_ char *joined_options = NULL;
5✔
UNCOV
506
                _cleanup_(oci_mount_data_done) oci_mount_data data = {};
×
507
                CustomMount *m;
5✔
508

509
                r = oci_dispatch(e, table, flags, &data);
5✔
510
                if (r < 0)
5✔
511
                        return r;
512

513
                if (!path_is_absolute(data.destination))
4✔
UNCOV
514
                        return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL),
×
515
                                        "Mount destination not an absolute path: %s", data.destination);
516

517
                if (oci_exclude_mount(data.destination))
4✔
UNCOV
518
                        continue;
×
519

520
                if (data.options) {
4✔
521
                        joined_options = strv_join(data.options, ",");
1✔
522
                        if (!joined_options)
1✔
UNCOV
523
                                return log_oom();
×
524
                }
525

526
                if (!data.type || streq(data.type, "bind")) {
4✔
527
                        if (data.source && !path_is_absolute(data.source)) {
1✔
UNCOV
528
                                char *joined;
×
529

530
                                joined = path_join(s->bundle, data.source);
×
UNCOV
531
                                if (!joined)
×
532
                                        return log_oom();
1✔
533

UNCOV
534
                                free_and_replace(data.source, joined);
×
535
                        }
536

537
                        data.type = mfree(data.type);
1✔
538

539
                        m = custom_mount_add(&s->custom_mounts, &s->n_custom_mounts, CUSTOM_MOUNT_BIND);
1✔
540
                } else
541
                        m = custom_mount_add(&s->custom_mounts, &s->n_custom_mounts, CUSTOM_MOUNT_ARBITRARY);
3✔
542
                if (!m)
4✔
UNCOV
543
                        return log_oom();
×
544

545
                m->destination = TAKE_PTR(data.destination);
4✔
546
                m->source = TAKE_PTR(data.source);
4✔
547
                m->options = TAKE_PTR(joined_options);
4✔
548
                m->type_argument = TAKE_PTR(data.type);
4✔
549
        }
550

551
        return 0;
2✔
552
}
553

554
static int oci_namespace_type(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
8✔
555
        unsigned long *nsflags = ASSERT_PTR(userdata);
8✔
556
        const char *n;
8✔
557

558
        assert_se(n = sd_json_variant_string(v));
8✔
559

560
        /* We don't use namespace_flags_from_string() here, as the OCI spec uses slightly different names than the
561
         * kernel here. */
562
        if (streq(n, "pid"))
8✔
563
                *nsflags = CLONE_NEWPID;
1✔
564
        else if (streq(n, "network"))
7✔
565
                *nsflags = CLONE_NEWNET;
1✔
566
        else if (streq(n, "mount"))
6✔
567
                *nsflags = CLONE_NEWNS;
1✔
568
        else if (streq(n, "ipc"))
5✔
569
                *nsflags = CLONE_NEWIPC;
2✔
570
        else if (streq(n, "uts"))
3✔
571
                *nsflags = CLONE_NEWUTS;
1✔
572
        else if (streq(n, "user"))
2✔
573
                *nsflags = CLONE_NEWUSER;
1✔
574
        else if (streq(n, "cgroup"))
1✔
UNCOV
575
                *nsflags = CLONE_NEWCGROUP;
×
576
        else
577
                return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
1✔
578
                                "Unknown namespace type, refusing: %s", n);
579

580
        return 0;
581
}
582

583
struct namespace_data {
584
        unsigned long type;
585
        char *path;
586
};
587

588
static void namespace_data_done(struct namespace_data *data) {
8✔
589
        assert(data);
8✔
590

591
        free(data->path);
8✔
592
}
8✔
593

594
static int oci_namespaces(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
4✔
595
        Settings *s = ASSERT_PTR(userdata);
4✔
596
        unsigned long n = 0;
4✔
597
        sd_json_variant *e;
4✔
598
        int r;
4✔
599

600
        JSON_VARIANT_ARRAY_FOREACH(e, v) {
9✔
601
                _cleanup_(namespace_data_done) struct namespace_data data = {};
3✔
602

603
                static const sd_json_dispatch_field table[] = {
8✔
604
                        { "type", SD_JSON_VARIANT_STRING, oci_namespace_type, offsetof(struct namespace_data, type), SD_JSON_MANDATORY },
605
                        { "path", SD_JSON_VARIANT_STRING, json_dispatch_path, offsetof(struct namespace_data, path), 0                 },
606
                        {}
607
                };
608

609
                r = oci_dispatch(e, table, flags, &data);
8✔
610
                if (r < 0)
8✔
611
                        return r;
612

613
                if (data.path) {
7✔
614
                        if (data.type != CLONE_NEWNET)
2✔
615
                                return json_log(e, flags, SYNTHETIC_ERRNO(EOPNOTSUPP),
1✔
616
                                                "Specifying namespace path for non-network namespace is not supported.");
617

618
                        if (s->network_namespace_path)
1✔
UNCOV
619
                                return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL),
×
620
                                                "Network namespace path specified more than once, refusing.");
621

622
                        free_and_replace(s->network_namespace_path, data.path);
1✔
623
                }
624

625
                if (FLAGS_SET(n, data.type))
6✔
626
                        return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL),
1✔
627
                                        "Duplicate namespace specification, refusing.");
628

629
                n |= data.type;
5✔
630
        }
631

632
        if (!FLAGS_SET(n, CLONE_NEWNS))
1✔
UNCOV
633
                return json_log(v, flags, SYNTHETIC_ERRNO(EOPNOTSUPP),
×
634
                                "Containers without a mount namespace aren't supported.");
635

636
        s->private_network = FLAGS_SET(n, CLONE_NEWNET);
1✔
637
        s->userns_mode = FLAGS_SET(n, CLONE_NEWUSER) ? USER_NAMESPACE_FIXED : USER_NAMESPACE_NO;
1✔
638
        s->use_cgns = FLAGS_SET(n, CLONE_NEWCGROUP);
1✔
639

640
        s->clone_ns_flags = n & (CLONE_NEWIPC|CLONE_NEWPID|CLONE_NEWUTS);
1✔
641

642
        return 0;
1✔
643
}
644

645
static int oci_uid_gid_range(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
2✔
646
        uid_t *uid = ASSERT_PTR(userdata);
2✔
647
        uid_t u;
2✔
648
        uint64_t k;
2✔
649

650
        assert_cc(sizeof(uid_t) == sizeof(gid_t));
2✔
651

652
        /* This is very much like oci_uid_gid(), except the checks are a bit different, as this is a UID range rather
653
         * than a specific UID, and hence UID_INVALID has no special significance. OTOH a range of zero makes no
654
         * sense. */
655

656
        k = sd_json_variant_unsigned(v);
2✔
657
        u = (uid_t) k;
2✔
658
        if ((uint64_t) u != k)
2✔
UNCOV
659
                return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
×
660
                                "UID/GID out of range: %" PRIu64, k);
661
        if (u == 0)
2✔
UNCOV
662
                return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
×
663
                                "UID/GID range can't be zero.");
664

665
        *uid = u;
2✔
666
        return 0;
2✔
667
}
668

669
static int oci_uid_gid_mappings(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
2✔
670
        struct mapping_data {
2✔
671
                uid_t host_id;
672
                uid_t container_id;
673
                uid_t range;
674
        } data = {
2✔
675
                .host_id = UID_INVALID,
676
                .container_id = UID_INVALID,
677
                .range = 0,
678
        };
679

680
        static const sd_json_dispatch_field table[] = {
2✔
681
                { "containerID", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(struct mapping_data, container_id), SD_JSON_MANDATORY },
682
                { "hostID",      SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(struct mapping_data, host_id),      SD_JSON_MANDATORY },
683
                { "size",        SD_JSON_VARIANT_UNSIGNED, oci_uid_gid_range,        offsetof(struct mapping_data, range),        SD_JSON_MANDATORY },
684
                {}
685
        };
686

687
        Settings *s = ASSERT_PTR(userdata);
2✔
688
        sd_json_variant *e;
2✔
689
        int r;
2✔
690

691
        if (sd_json_variant_elements(v) == 0)
2✔
692
                return 0;
2✔
693

694
        if (sd_json_variant_elements(v) > 1)
2✔
UNCOV
695
                return json_log(v, flags, SYNTHETIC_ERRNO(EOPNOTSUPP),
×
696
                                "UID/GID mappings with more than one entry are not supported.");
697

698
        assert_se(e = sd_json_variant_by_index(v, 0));
2✔
699

700
        r = oci_dispatch(e, table, flags, &data);
2✔
701
        if (r < 0)
2✔
702
                return r;
703

704
        if (data.range > UINT32_MAX - data.host_id ||
2✔
705
            data.range > UINT32_MAX - data.container_id)
2✔
UNCOV
706
                return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
×
707
                                "UID/GID range goes beyond UID/GID validity range, refusing.");
708

709
        if (data.container_id != 0)
2✔
UNCOV
710
                return json_log(v, flags, SYNTHETIC_ERRNO(EOPNOTSUPP),
×
711
                                "UID/GID mappings with a non-zero container base are not supported.");
712

713
        if (data.range < 0x10000)
2✔
714
                json_log(v, flags|SD_JSON_WARNING, 0,
2✔
715
                         "UID/GID mapping with less than 65536 UID/GIDS set up, you are looking for trouble.");
716

717
        if (s->uid_range != UID_INVALID &&
2✔
718
            (s->uid_shift != data.host_id || s->uid_range != data.range))
1✔
UNCOV
719
                return json_log(v, flags, SYNTHETIC_ERRNO(EOPNOTSUPP),
×
720
                                "Non-matching UID and GID mappings are not supported.");
721

722
        s->uid_shift = data.host_id;
2✔
723
        s->uid_range = data.range;
2✔
724

725
        return 0;
2✔
726
}
727

728
static int oci_device_type(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
3✔
729
        mode_t *mode = ASSERT_PTR(userdata);
3✔
730
        const char *t;
3✔
731

732
        assert_se(t = sd_json_variant_string(v));
3✔
733

734
        if (STR_IN_SET(t, "c", "u"))
3✔
735
                *mode = (*mode & ~S_IFMT) | S_IFCHR;
1✔
736
        else if (streq(t, "b"))
2✔
737
                *mode = (*mode & ~S_IFMT) | S_IFBLK;
1✔
738
        else if (streq(t, "p"))
1✔
UNCOV
739
                *mode = (*mode & ~S_IFMT) | S_IFIFO;
×
740
        else
741
                return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
1✔
742
                                "Unknown device type: %s", t);
743

744
        return 0;
2✔
745
}
746

747
static int oci_device_major(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
8✔
748
        unsigned *u = ASSERT_PTR(userdata);
8✔
749
        uint64_t k;
8✔
750

751
        k = sd_json_variant_unsigned(v);
8✔
752
        if (!DEVICE_MAJOR_VALID(k))
8✔
UNCOV
753
                return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
×
754
                                "Device major %" PRIu64 " out of range.", k);
755

756
        *u = (unsigned) k;
8✔
757
        return 0;
8✔
758
}
759

760
static int oci_device_minor(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
8✔
761
        unsigned *u = ASSERT_PTR(userdata);
8✔
762
        uint64_t k;
8✔
763

764
        k = sd_json_variant_unsigned(v);
8✔
765
        if (!DEVICE_MINOR_VALID(k))
8✔
UNCOV
766
                return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
×
767
                                "Device minor %" PRIu64 " out of range.", k);
768

769
        *u = (unsigned) k;
8✔
770
        return 0;
8✔
771
}
772

773
static int oci_device_file_mode(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
2✔
774
        mode_t *mode = ASSERT_PTR(userdata);
2✔
775
        mode_t m;
2✔
776
        uint64_t k;
2✔
777

778
        k = sd_json_variant_unsigned(v);
2✔
779
        m = (mode_t) k;
2✔
780

781
        if ((m & ~07777) != 0 || (uint64_t) m != k)
2✔
UNCOV
782
                return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
×
783
                                "fileMode out of range, refusing.");
784

785
        *mode = (*mode & ~07777) | m;
2✔
786
        return 0;
2✔
787
}
788

789
static int oci_devices(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
2✔
790
        Settings *s = ASSERT_PTR(userdata);
2✔
791
        sd_json_variant *e;
2✔
792
        int r;
2✔
793

794
        JSON_VARIANT_ARRAY_FOREACH(e, v) {
4✔
795

796
                static const sd_json_dispatch_field table[] = {
3✔
797
                        { "type",     SD_JSON_VARIANT_STRING,   oci_device_type,          offsetof(DeviceNode, mode),  SD_JSON_MANDATORY },
798
                        { "path",     SD_JSON_VARIANT_STRING,   json_dispatch_path,       offsetof(DeviceNode, path),  SD_JSON_MANDATORY },
799
                        { "major",    SD_JSON_VARIANT_UNSIGNED, oci_device_major,         offsetof(DeviceNode, major), 0                 },
800
                        { "minor",    SD_JSON_VARIANT_UNSIGNED, oci_device_minor,         offsetof(DeviceNode, minor), 0                 },
801
                        { "fileMode", SD_JSON_VARIANT_UNSIGNED, oci_device_file_mode,     offsetof(DeviceNode, mode),  0                 },
802
                        { "uid",      SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(DeviceNode, uid),   0                 },
803
                        { "gid",      SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(DeviceNode, gid),   0                 },
804
                        {}
805
                };
806

807
                DeviceNode *node;
3✔
808

809
                if (!GREEDY_REALLOC(s->extra_nodes, s->n_extra_nodes + 1))
3✔
UNCOV
810
                        return log_oom();
×
811

812
                node = s->extra_nodes + s->n_extra_nodes;
3✔
813
                *node = (DeviceNode) {
3✔
814
                        .uid = UID_INVALID,
815
                        .gid = GID_INVALID,
816
                        .major = UINT_MAX,
817
                        .minor = UINT_MAX,
818
                        .mode = 0644,
819
                };
820

821
                r = oci_dispatch(e, table, flags, node);
3✔
822
                if (r < 0)
3✔
823
                        goto fail_element;
1✔
824

825
                if (S_ISCHR(node->mode) || S_ISBLK(node->mode)) {
2✔
826
                        _cleanup_free_ char *path = NULL;
2✔
827

828
                        if (node->major == UINT_MAX || node->minor == UINT_MAX) {
2✔
UNCOV
829
                                r = json_log(e, flags, SYNTHETIC_ERRNO(EINVAL),
×
830
                                             "Major/minor required when device node is device node.");
UNCOV
831
                                goto fail_element;
×
832
                        }
833

834
                        /* Suppress a couple of implicit device nodes */
835
                        r = devname_from_devnum(node->mode, makedev(node->major, node->minor), &path);
2✔
836
                        if (r < 0)
2✔
837
                                json_log(e, flags|SD_JSON_DEBUG, r, "Failed to resolve device node %u:%u, ignoring: %m", node->major, node->minor);
1✔
838
                        else {
839
                                if (PATH_IN_SET(path,
1✔
840
                                                "/dev/null",
841
                                                "/dev/zero",
842
                                                "/dev/full",
843
                                                "/dev/random",
844
                                                "/dev/urandom",
845
                                                "/dev/tty",
846
                                                "/dev/net/tun",
847
                                                "/dev/ptmx",
848
                                                "/dev/pts/ptmx",
849
                                                "/dev/console")) {
850

851
                                        json_log(e, flags|SD_JSON_DEBUG, 0, "Ignoring devices item for device '%s', as it is implicitly created anyway.", path);
1✔
852
                                        free(node->path);
1✔
853
                                        continue;
1✔
854
                                }
855
                        }
856
                }
857

858
                s->n_extra_nodes++;
1✔
859
                continue;
1✔
860

861
        fail_element:
1✔
862
                free(node->path);
1✔
863
                return r;
1✔
864
        }
865

866
        return 0;
1✔
867
}
868

869
static int oci_cgroups_path(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
2✔
870
        _cleanup_free_ char *slice = NULL, *backwards = NULL;
2✔
871
        Settings *s = ASSERT_PTR(userdata);
2✔
872
        const char *p;
2✔
873
        int r;
2✔
874

875
        assert_se(p = sd_json_variant_string(v));
2✔
876

877
        r = cg_path_get_slice(p, &slice);
2✔
878
        if (r < 0)
2✔
UNCOV
879
                return json_log(v, flags, r, "Couldn't derive slice unit name from path '%s': %m", p);
×
880

881
        r = cg_slice_to_path(slice, &backwards);
2✔
882
        if (r < 0)
2✔
UNCOV
883
                return json_log(v, flags, r, "Couldn't convert slice unit name '%s' back to path: %m", slice);
×
884

885
        if (!path_equal(backwards, p))
2✔
886
                return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
2✔
887
                                "Control group path '%s' does not refer to slice unit, refusing.", p);
888

889
        free_and_replace(s->slice, slice);
×
UNCOV
890
        return 0;
×
891
}
892

893
static int oci_cgroup_device_type(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
1✔
894
        mode_t *mode = ASSERT_PTR(userdata);
1✔
895
        const char *n;
1✔
896

897
        assert_se(n = sd_json_variant_string(v));
1✔
898

899
        if (streq(n, "c"))
1✔
UNCOV
900
                *mode = S_IFCHR;
×
901
        else if (streq(n, "b"))
1✔
902
                *mode = S_IFBLK;
1✔
903
        else
UNCOV
904
                return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
×
905
                                "Control group device type unknown: %s", n);
906

907
        return 0;
908
}
909

910
struct device_data {
911
        bool allow;
912
        bool r;
913
        bool w;
914
        bool m;
915
        mode_t type;
916
        unsigned major;
917
        unsigned minor;
918
};
919

920
static int oci_cgroup_device_access(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
2✔
921
        struct device_data *d = ASSERT_PTR(userdata);
2✔
922
        bool r = false, w = false, m = false;
2✔
923

924
        for (const char *s = ASSERT_PTR(sd_json_variant_string(v)); *s; s++)
6✔
925
                if (*s == 'r')
4✔
926
                        r = true;
927
                else if (*s == 'w')
3✔
928
                        w = true;
929
                else if (*s == 'm')
2✔
930
                        m = true;
931
                else
UNCOV
932
                        return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
×
933
                                        "Unknown device access character '%c'.", *s);
934

935
        d->r = r;
2✔
936
        d->w = w;
2✔
937
        d->m = m;
2✔
938

939
        return 0;
2✔
940
}
941

942
static int oci_cgroup_devices(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
1✔
943
        _cleanup_free_ struct device_data *list = NULL;
1✔
944
        Settings *s = ASSERT_PTR(userdata);
1✔
945
        size_t n_list = 0;
1✔
946
        bool noop = false;
1✔
947
        sd_json_variant *e;
1✔
948
        int r;
1✔
949

950
        JSON_VARIANT_ARRAY_FOREACH(e, v) {
3✔
951

952
                struct device_data data = {
2✔
953
                        .major = UINT_MAX,
954
                        .minor = UINT_MAX,
955
                };
956

957
                static const sd_json_dispatch_field table[] = {
2✔
958
                        { "allow",  SD_JSON_VARIANT_BOOLEAN,  sd_json_dispatch_stdbool, offsetof(struct device_data, allow), SD_JSON_MANDATORY },
959
                        { "type",   SD_JSON_VARIANT_STRING,   oci_cgroup_device_type,   offsetof(struct device_data, type),  0                 },
960
                        { "major",  SD_JSON_VARIANT_UNSIGNED, oci_device_major,         offsetof(struct device_data, major), 0                 },
961
                        { "minor",  SD_JSON_VARIANT_UNSIGNED, oci_device_minor,         offsetof(struct device_data, minor), 0                 },
962
                        { "access", SD_JSON_VARIANT_STRING,   oci_cgroup_device_access, 0,                                   0                 },
963
                        {}
964
                };
965

966
                r = oci_dispatch(e, table, flags, &data);
2✔
967
                if (r < 0)
2✔
UNCOV
968
                        return r;
×
969

970
                if (!data.allow) {
2✔
971
                        /* The fact that OCI allows 'deny' entries makes really no sense, as 'allow'
972
                         * vs. 'deny' for the devices cgroup controller is really not about allow-listing and
973
                         * deny-listing but about adding and removing entries from the allow list. Since we
974
                         * always start out with an empty allow list we hence ignore the whole thing, as
975
                         * removing entries which don't exist make no sense. We'll log about this, since this
976
                         * is really borked in the spec, with one exception: the entry that's supposed to
977
                         * drop the kernel's default we ignore silently */
978

979
                        if (!data.r || !data.w || !data.m || data.type != 0 || data.major != UINT_MAX || data.minor != UINT_MAX)
1✔
980
                                json_log(v, flags|SD_JSON_WARNING, 0, "Devices cgroup allow list with arbitrary 'allow' entries not supported, ignoring.");
1✔
981

982
                        /* We ignore the 'deny' entry as for us that's implied */
983
                        continue;
1✔
984
                }
985

986
                if (!data.r && !data.w && !data.m) {
1✔
987
                        json_log(v, flags|LOG_WARNING, 0, "Device cgroup allow list entry with no effect found, ignoring.");
×
UNCOV
988
                        continue;
×
989
                }
990

991
                if (data.minor != UINT_MAX && data.major == UINT_MAX)
1✔
UNCOV
992
                        return json_log(v, flags, SYNTHETIC_ERRNO(EOPNOTSUPP),
×
993
                                        "Device cgroup allow list entries with minors but no majors not supported.");
994

995
                if (data.major != UINT_MAX && data.type == 0)
1✔
UNCOV
996
                        return json_log(v, flags, SYNTHETIC_ERRNO(EOPNOTSUPP),
×
997
                                        "Device cgroup allow list entries with majors but no device node type not supported.");
998

999
                if (data.type == 0) {
1✔
UNCOV
1000
                        if (data.r && data.w && data.m) /* a catchall allow list entry means we are looking at a noop */
×
1001
                                noop = true;
1002
                        else
UNCOV
1003
                                return json_log(v, flags, SYNTHETIC_ERRNO(EOPNOTSUPP),
×
1004
                                                "Device cgroup allow list entries with no type not supported.");
1005
                }
1006

1007
                if (!GREEDY_REALLOC(list, n_list + 1))
1✔
UNCOV
1008
                        return log_oom();
×
1009

1010
                list[n_list++] = data;
1✔
1011
        }
1012

1013
        if (noop)
1✔
1014
                return 0;
1015

1016
        r = settings_allocate_properties(s);
1✔
1017
        if (r < 0)
1✔
1018
                return r;
1019

1020
        r = sd_bus_message_open_container(s->properties, 'r', "sv");
1✔
1021
        if (r < 0)
1✔
UNCOV
1022
                return bus_log_create_error(r);
×
1023

1024
        r = sd_bus_message_append(s->properties, "s", "DeviceAllow");
1✔
1025
        if (r < 0)
1✔
UNCOV
1026
                return bus_log_create_error(r);
×
1027

1028
        r = sd_bus_message_open_container(s->properties, 'v', "a(ss)");
1✔
1029
        if (r < 0)
1✔
UNCOV
1030
                return bus_log_create_error(r);
×
1031

1032
        r = sd_bus_message_open_container(s->properties, 'a', "(ss)");
1✔
1033
        if (r < 0)
1✔
UNCOV
1034
                return bus_log_create_error(r);
×
1035

1036
        FOREACH_ARRAY(d, list, n_list) {
2✔
1037
                _cleanup_free_ char *pattern = NULL;
1✔
1038
                char access[4];
1✔
1039
                size_t n = 0;
1✔
1040

1041
                if (d->minor == UINT_MAX) {
1✔
UNCOV
1042
                        const char *t;
×
1043

UNCOV
1044
                        if (d->type == S_IFBLK)
×
1045
                                t = "block";
1046
                        else {
UNCOV
1047
                                assert(d->type == S_IFCHR);
×
1048
                                t = "char";
1049
                        }
1050

1051
                        if (d->major == UINT_MAX) {
×
1052
                                pattern = strjoin(t, "-*");
×
1053
                                if (!pattern)
×
UNCOV
1054
                                        return log_oom();
×
1055
                        } else {
1056
                                if (asprintf(&pattern, "%s-%u", t, d->major) < 0)
×
UNCOV
1057
                                        return log_oom();
×
1058
                        }
1059

1060
                } else {
1061
                        assert(d->major != UINT_MAX); /* If a minor is specified, then a major also needs to be specified */
1✔
1062

1063
                        r = device_path_make_major_minor(d->type, makedev(d->major, d->minor), &pattern);
1✔
1064
                        if (r < 0)
1✔
UNCOV
1065
                                return log_oom();
×
1066
                }
1067

1068
                if (d->r)
1✔
1069
                        access[n++] = 'r';
1✔
1070
                if (d->w)
1✔
1071
                        access[n++] = 'w';
1✔
1072
                if (d->m)
1✔
1073
                        access[n++] = 'm';
1✔
1074
                access[n] = 0;
1✔
1075

1076
                assert(n > 0);
1✔
1077

1078
                r = sd_bus_message_append(s->properties, "(ss)", pattern, access);
1✔
1079
                if (r < 0)
1✔
UNCOV
1080
                        return bus_log_create_error(r);
×
1081
        }
1082

1083
        r = sd_bus_message_close_container(s->properties);
1✔
1084
        if (r < 0)
1✔
UNCOV
1085
                return bus_log_create_error(r);
×
1086

1087
        r = sd_bus_message_close_container(s->properties);
1✔
1088
        if (r < 0)
1✔
UNCOV
1089
                return bus_log_create_error(r);
×
1090

1091
        r = sd_bus_message_close_container(s->properties);
1✔
1092
        if (r < 0)
1✔
UNCOV
1093
                return bus_log_create_error(r);
×
1094

1095
        return 0;
1096
}
1097

1098
static int oci_cgroup_memory_limit(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
3✔
1099
        uint64_t *m = ASSERT_PTR(userdata);
3✔
1100
        uint64_t k;
3✔
1101

1102
        if (sd_json_variant_is_negative(v)) {
3✔
1103
                *m = UINT64_MAX;
×
UNCOV
1104
                return 0;
×
1105
        }
1106

1107
        if (!sd_json_variant_is_unsigned(v))
3✔
UNCOV
1108
                return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
×
1109
                                "Memory limit is not an unsigned integer.");
1110

1111
        k = sd_json_variant_unsigned(v);
3✔
1112
        if (k >= UINT64_MAX)
3✔
UNCOV
1113
                return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
×
1114
                                "Memory limit too large: %" PRIu64, k);
1115

1116
        *m = (uint64_t) k;
3✔
1117
        return 0;
3✔
1118
}
1119

1120
static int oci_cgroup_memory(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
1✔
1121

1122
        struct memory_data {
1✔
1123
                uint64_t limit;
1124
                uint64_t reservation;
1125
                uint64_t swap;
1126
        } data = {
1✔
1127
                .limit = UINT64_MAX,
1128
                .reservation = UINT64_MAX,
1129
                .swap = UINT64_MAX,
1130
        };
1131

1132
        static const sd_json_dispatch_field table[] = {
1✔
1133
                { "limit",            SD_JSON_VARIANT_NUMBER,  oci_cgroup_memory_limit, offsetof(struct memory_data, limit),       0                  },
1134
                { "reservation",      SD_JSON_VARIANT_NUMBER,  oci_cgroup_memory_limit, offsetof(struct memory_data, reservation), 0                  },
1135
                { "swap",             SD_JSON_VARIANT_NUMBER,  oci_cgroup_memory_limit, offsetof(struct memory_data, swap),        0                  },
1136
                { "kernel",           SD_JSON_VARIANT_NUMBER,  oci_unsupported,         0,                                         SD_JSON_PERMISSIVE },
1137
                { "kernelTCP",        SD_JSON_VARIANT_NUMBER,  oci_unsupported,         0,                                         SD_JSON_PERMISSIVE },
1138
                { "swapiness",        SD_JSON_VARIANT_NUMBER,  oci_unsupported,         0,                                         SD_JSON_PERMISSIVE },
1139
                { "disableOOMKiller", SD_JSON_VARIANT_BOOLEAN, oci_unsupported,         0,                                         SD_JSON_PERMISSIVE },
1140
                {}
1141
        };
1142

1143
        Settings *s = ASSERT_PTR(userdata);
1✔
1144
        int r;
1✔
1145

1146
        r = oci_dispatch(v, table, flags, &data);
1✔
1147
        if (r < 0)
1✔
1148
                return r;
1✔
1149

1150
        if (data.swap != UINT64_MAX) {
1✔
1151
                if (data.limit == UINT64_MAX)
1✔
UNCOV
1152
                        json_log(v, flags|LOG_WARNING, 0, "swap limit without memory limit is not supported, ignoring.");
×
1153
                else if (data.swap < data.limit)
1✔
UNCOV
1154
                        json_log(v, flags|LOG_WARNING, 0, "swap limit is below memory limit, ignoring.");
×
1155
                else {
1156
                        r = settings_allocate_properties(s);
1✔
1157
                        if (r < 0)
1✔
1158
                                return r;
1159

1160
                        r = sd_bus_message_append(s->properties, "(sv)", "MemorySwapMax", "t", data.swap - data.limit);
1✔
1161
                        if (r < 0)
1✔
UNCOV
1162
                                return bus_log_create_error(r);
×
1163
                }
1164
        }
1165

1166
        if (data.limit != UINT64_MAX) {
1✔
1167
                r = settings_allocate_properties(s);
1✔
1168
                if (r < 0)
1✔
1169
                        return r;
1170

1171
                r = sd_bus_message_append(s->properties, "(sv)", "MemoryMax", "t", data.limit);
1✔
1172
                if (r < 0)
1✔
UNCOV
1173
                        return bus_log_create_error(r);
×
1174
        }
1175

1176
        if (data.reservation != UINT64_MAX) {
1✔
1177
                r = settings_allocate_properties(s);
1✔
1178
                if (r < 0)
1✔
1179
                        return r;
1180

1181
                r = sd_bus_message_append(s->properties, "(sv)", "MemoryLow", "t", data.reservation);
1✔
1182
                if (r < 0)
1✔
UNCOV
1183
                        return bus_log_create_error(r);
×
1184
        }
1185

1186
        return 0;
1187
}
1188

1189
struct cpu_data {
1190
        uint64_t weight;
1191
        uint64_t quota;
1192
        uint64_t period;
1193
        CPUSet cpu_set;
1194
};
1195

1196
static int oci_cgroup_cpu_shares(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
1✔
1197
        uint64_t k, *u = ASSERT_PTR(userdata);
1✔
1198

1199
        k = sd_json_variant_unsigned(v);
1✔
1200
        if (k < CGROUP_CPU_SHARES_MIN || k > CGROUP_CPU_SHARES_MAX)
1✔
UNCOV
1201
                return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE), "shares value out of range.");
×
1202

1203
        /* convert from cgroup v1 cpu.shares to v2 cpu.weight */
1204
        assert_cc(CGROUP_CPU_SHARES_MAX <= UINT64_MAX / CGROUP_WEIGHT_DEFAULT);
1✔
1205
        *u = CLAMP(k * CGROUP_WEIGHT_DEFAULT / CGROUP_CPU_SHARES_DEFAULT, CGROUP_WEIGHT_MIN, CGROUP_WEIGHT_MAX);
1✔
1206
        return 0;
1✔
1207
}
1208

1209
static int oci_cgroup_cpu_quota(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
2✔
1210
        uint64_t k, *u = ASSERT_PTR(userdata);
2✔
1211

1212
        k = sd_json_variant_unsigned(v);
2✔
1213
        if (k <= 0 || k >= UINT64_MAX)
2✔
UNCOV
1214
                return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE), "period/quota value out of range.");
×
1215

1216
        *u = k;
2✔
1217
        return 0;
2✔
1218
}
1219

1220
static int oci_cgroup_cpu_cpus(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
1✔
1221
        struct cpu_data *data = ASSERT_PTR(userdata);
1✔
1222
        CPUSet set;
1✔
1223
        const char *n;
1✔
1224
        int r;
1✔
1225

1226
        assert_se(n = sd_json_variant_string(v));
1✔
1227

1228
        r = parse_cpu_set(n, &set);
1✔
1229
        if (r < 0)
1✔
UNCOV
1230
                return json_log(v, flags, r, "Failed to parse CPU set specification: %s", n);
×
1231

1232
        cpu_set_reset(&data->cpu_set);
1✔
1233
        data->cpu_set = set;
1✔
1234

1235
        return 0;
1✔
1236
}
1237

1238
static int oci_cgroup_cpu(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
2✔
1239

1240
        static const sd_json_dispatch_field table[] = {
2✔
1241
                { "shares",          SD_JSON_VARIANT_UNSIGNED, oci_cgroup_cpu_shares, offsetof(struct cpu_data, weight), 0 },
1242
                { "quota",           SD_JSON_VARIANT_UNSIGNED, oci_cgroup_cpu_quota,  offsetof(struct cpu_data, quota),  0 },
1243
                { "period",          SD_JSON_VARIANT_UNSIGNED, oci_cgroup_cpu_quota,  offsetof(struct cpu_data, period), 0 },
1244
                { "realtimeRuntime", SD_JSON_VARIANT_UNSIGNED, oci_unsupported,       0,                                 0 },
1245
                { "realtimePeriod",  SD_JSON_VARIANT_UNSIGNED, oci_unsupported,       0,                                 0 },
1246
                { "cpus",            SD_JSON_VARIANT_STRING,   oci_cgroup_cpu_cpus,   0,                                 0 },
1247
                { "mems",            SD_JSON_VARIANT_STRING,   oci_unsupported,       0,                                 0 },
1248
                {}
1249
        };
1250

1251
        struct cpu_data data = {
2✔
1252
                .weight = UINT64_MAX,
1253
                .quota = UINT64_MAX,
1254
                .period = UINT64_MAX,
1255
        };
1256

1257
        Settings *s = ASSERT_PTR(userdata);
2✔
1258
        int r;
2✔
1259

1260
        r = oci_dispatch(v, table, flags, &data);
2✔
1261
        if (r < 0) {
2✔
1262
                cpu_set_reset(&data.cpu_set);
1✔
1263
                return r;
1✔
1264
        }
1265

1266
        cpu_set_reset(&s->cpu_set);
1✔
1267
        s->cpu_set = data.cpu_set;
1✔
1268

1269
        if (data.weight != UINT64_MAX) {
1✔
1270
                r = settings_allocate_properties(s);
1✔
1271
                if (r < 0)
1✔
1272
                        return r;
1273

1274
                r = sd_bus_message_append(s->properties, "(sv)", "CPUWeight", "t", data.weight);
1✔
1275
                if (r < 0)
1✔
UNCOV
1276
                        return bus_log_create_error(r);
×
1277
        }
1278

1279
        if (data.quota != UINT64_MAX && data.period != UINT64_MAX) {
1✔
1280
                r = settings_allocate_properties(s);
1✔
1281
                if (r < 0)
1✔
1282
                        return r;
1283

1284
                r = sd_bus_message_append(s->properties, "(sv)", "CPUQuotaPerSecUSec", "t", data.quota * USEC_PER_SEC / data.period);
1✔
1285
                if (r < 0)
1✔
UNCOV
1286
                        return bus_log_create_error(r);
×
1287

1288
                r = sd_bus_message_append(s->properties, "(sv)", "CPUQuotaPeriodUSec", "t", data.period);
1✔
1289
                if (r < 0)
1✔
UNCOV
1290
                        return bus_log_create_error(r);
×
1291

1292
        } else if ((data.quota != UINT64_MAX) != (data.period != UINT64_MAX))
×
UNCOV
1293
                return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
×
1294
                                "CPU quota and period not used together.");
1295

1296
        return 0;
1297
}
1298

1299
static uint64_t cgroup_weight_blkio_to_io(uint64_t blkio_weight) {
2✔
1300
        /* convert from cgroup v1 blkio.weight to v2 io.weight */
1301
        assert_cc(CGROUP_BLKIO_WEIGHT_MAX <= UINT64_MAX / CGROUP_WEIGHT_DEFAULT);
2✔
1302
        return CLAMP(blkio_weight * CGROUP_WEIGHT_DEFAULT / CGROUP_BLKIO_WEIGHT_DEFAULT,
2✔
1303
                     CGROUP_WEIGHT_MIN, CGROUP_WEIGHT_MAX);
1304
}
1305

1306
static int oci_cgroup_block_io_weight(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
1✔
1307
        Settings *s = ASSERT_PTR(userdata);
1✔
1308
        uint64_t k;
1✔
1309
        int r;
1✔
1310

1311
        k = sd_json_variant_unsigned(v);
1✔
1312
        if (k < CGROUP_BLKIO_WEIGHT_MIN || k > CGROUP_BLKIO_WEIGHT_MAX)
1✔
UNCOV
1313
                return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
×
1314
                                "Block I/O weight out of range.");
1315

1316
        r = settings_allocate_properties(s);
1✔
1317
        if (r < 0)
1✔
1318
                return r;
1319

1320
        r = sd_bus_message_append(s->properties, "(sv)", "IOWeight", "t", cgroup_weight_blkio_to_io(k));
1✔
1321
        if (r < 0)
1✔
UNCOV
1322
                return bus_log_create_error(r);
×
1323

1324
        return 0;
1325
}
1326

1327
static int oci_cgroup_block_io_weight_device(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
1✔
1328
        Settings *s = ASSERT_PTR(userdata);
1✔
1329
        sd_json_variant *e;
1✔
1330
        int r;
1✔
1331

1332
        JSON_VARIANT_ARRAY_FOREACH(e, v) {
2✔
1333
                struct device_data {
1✔
1334
                        unsigned major;
1335
                        unsigned minor;
1336
                        uint64_t weight;
1337
                } data = {
1✔
1338
                        .major = UINT_MAX,
1339
                        .minor = UINT_MAX,
1340
                        .weight = UINT64_MAX,
1341
                };
1342

1343
                static const sd_json_dispatch_field table[] =  {
1✔
1344
                        { "major",      SD_JSON_VARIANT_UNSIGNED, oci_device_major,        offsetof(struct device_data, major),  SD_JSON_MANDATORY  },
1345
                        { "minor",      SD_JSON_VARIANT_UNSIGNED, oci_device_minor,        offsetof(struct device_data, minor),  SD_JSON_MANDATORY  },
1346
                        { "weight",     SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uint64, offsetof(struct device_data, weight), 0                  },
1347
                        { "leafWeight", SD_JSON_VARIANT_INTEGER,  oci_unsupported,         0,                                    SD_JSON_PERMISSIVE },
1348
                        {}
1349
                };
1350

1351
                _cleanup_free_ char *path = NULL;
1✔
1352

1353
                r = oci_dispatch(e, table, flags, &data);
1✔
1354
                if (r < 0)
1✔
1355
                        return r;
1356

1357
                if (data.weight == UINT64_MAX)
1✔
UNCOV
1358
                        continue;
×
1359

1360
                if (data.weight < CGROUP_BLKIO_WEIGHT_MIN || data.weight > CGROUP_BLKIO_WEIGHT_MAX)
1✔
UNCOV
1361
                        return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
×
1362
                                        "Block I/O device weight out of range.");
1363

1364
                r = device_path_make_major_minor(S_IFBLK, makedev(data.major, data.minor), &path);
1✔
1365
                if (r < 0)
1✔
UNCOV
1366
                        return json_log(v, flags, r, "Failed to build device path: %m");
×
1367

1368
                r = settings_allocate_properties(s);
1✔
1369
                if (r < 0)
1✔
1370
                        return r;
1371

1372
                r = sd_bus_message_append(s->properties, "(sv)", "IODeviceWeight", "a(st)", 1,
1✔
1373
                                          path, cgroup_weight_blkio_to_io(data.weight));
1374
                if (r < 0)
1✔
UNCOV
1375
                        return bus_log_create_error(r);
×
1376
        }
1377

1378
        return 0;
1✔
1379
}
1380

1381
static int oci_cgroup_block_io_throttle(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
4✔
1382
        Settings *s = ASSERT_PTR(userdata);
4✔
1383
        const char *pname;
4✔
1384
        sd_json_variant *e;
4✔
1385
        int r;
4✔
1386

1387
        pname = streq(name, "throttleReadBpsDevice")  ? "IOReadBandwidthMax" :
5✔
1388
                streq(name, "throttleWriteBpsDevice") ? "IOWriteBandwidthMax" :
3✔
1389
                streq(name, "throttleReadIOPSDevice") ? "IOReadIOPSMax" :
2✔
1390
                                                        "IOWriteIOPSMax";
1391

1392
        JSON_VARIANT_ARRAY_FOREACH(e, v) {
8✔
1393
                struct device_data {
4✔
1394
                        unsigned major;
1395
                        unsigned minor;
1396
                        uint64_t rate;
1397
                } data = {
4✔
1398
                        .major = UINT_MAX,
1399
                        .minor = UINT_MAX,
1400
                };
1401

1402
                static const sd_json_dispatch_field table[] = {
4✔
1403
                        { "major", SD_JSON_VARIANT_UNSIGNED, oci_device_major,        offsetof(struct device_data, major), SD_JSON_MANDATORY },
1404
                        { "minor", SD_JSON_VARIANT_UNSIGNED, oci_device_minor,        offsetof(struct device_data, minor), SD_JSON_MANDATORY },
1405
                        { "rate",  SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uint64, offsetof(struct device_data, rate),  SD_JSON_MANDATORY },
1406
                        {}
1407
                };
1408

1409
                _cleanup_free_ char *path = NULL;
4✔
1410

1411
                r = oci_dispatch(e, table, flags, &data);
4✔
1412
                if (r < 0)
4✔
1413
                        return r;
1414

1415
                if (data.rate >= UINT64_MAX)
4✔
UNCOV
1416
                        return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
×
1417
                                        "Block I/O device rate out of range.");
1418

1419
                r = device_path_make_major_minor(S_IFBLK, makedev(data.major, data.minor), &path);
4✔
1420
                if (r < 0)
4✔
UNCOV
1421
                        return json_log(v, flags, r, "Failed to build device path: %m");
×
1422

1423
                r = settings_allocate_properties(s);
4✔
1424
                if (r < 0)
4✔
1425
                        return r;
1426

1427
                r = sd_bus_message_append(s->properties, "(sv)", pname, "a(st)", 1, path, (uint64_t) data.rate);
4✔
1428
                if (r < 0)
4✔
UNCOV
1429
                        return bus_log_create_error(r);
×
1430
        }
1431

1432
        return 0;
4✔
1433
}
1434

1435
static int oci_cgroup_block_io(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
1✔
1436

1437
        static const sd_json_dispatch_field table[] = {
1✔
1438
                { "weight",                  SD_JSON_VARIANT_UNSIGNED, oci_cgroup_block_io_weight,        0, 0                  },
1439
                { "leafWeight",              SD_JSON_VARIANT_UNSIGNED, oci_unsupported,                   0, SD_JSON_PERMISSIVE },
1440
                { "weightDevice",            SD_JSON_VARIANT_ARRAY,    oci_cgroup_block_io_weight_device, 0, 0                  },
1441
                { "throttleReadBpsDevice",   SD_JSON_VARIANT_ARRAY,    oci_cgroup_block_io_throttle,      0, 0                  },
1442
                { "throttleWriteBpsDevice",  SD_JSON_VARIANT_ARRAY,    oci_cgroup_block_io_throttle,      0, 0                  },
1443
                { "throttleReadIOPSDevice",  SD_JSON_VARIANT_ARRAY,    oci_cgroup_block_io_throttle,      0, 0                  },
1444
                { "throttleWriteIOPSDevice", SD_JSON_VARIANT_ARRAY,    oci_cgroup_block_io_throttle,      0, 0                  },
1445
                {}
1446
        };
1447

1448
        return oci_dispatch(v, table, flags, userdata);
1✔
1449
}
1450

1451
static int oci_cgroup_pids(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
1✔
1452

1453
        static const sd_json_dispatch_field table[] = {
1✔
1454
                { "limit", SD_JSON_VARIANT_NUMBER, sd_json_dispatch_variant, 0, SD_JSON_MANDATORY },
1455
                {}
1456
        };
1457

1458
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *k = NULL;
1✔
1459
        Settings *s = ASSERT_PTR(userdata);
1✔
1460
        uint64_t m;
1✔
1461
        int r;
1✔
1462

1463
        r = oci_dispatch(v, table, flags, &k);
1✔
1464
        if (r < 0)
1✔
1465
                return r;
1466

1467
        if (sd_json_variant_is_negative(k))
1✔
1468
                m = UINT64_MAX;
1469
        else {
1470
                if (!sd_json_variant_is_unsigned(k))
1✔
UNCOV
1471
                        return json_log(k, flags, SYNTHETIC_ERRNO(EINVAL),
×
1472
                                        "pids limit not unsigned integer, refusing.");
1473

1474
                m = (uint64_t) sd_json_variant_unsigned(k);
1✔
1475

1476
                if ((uint64_t) m != sd_json_variant_unsigned(k))
1✔
UNCOV
1477
                        return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
×
1478
                                        "pids limit out of range, refusing.");
1479
        }
1480

1481
        r = settings_allocate_properties(s);
1✔
1482
        if (r < 0)
1✔
1483
                return r;
1484

1485
        r = sd_bus_message_append(s->properties, "(sv)", "TasksMax", "t", m);
1✔
1486
        if (r < 0)
1✔
UNCOV
1487
                return bus_log_create_error(r);
×
1488

1489
        return 0;
1490
}
1491

1492
static int oci_resources(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
2✔
1493

1494
        static const sd_json_dispatch_field table[] = {
2✔
1495
                { "devices",        SD_JSON_VARIANT_ARRAY,  oci_cgroup_devices,  0, 0 },
1496
                { "memory",         SD_JSON_VARIANT_OBJECT, oci_cgroup_memory,   0, 0 },
1497
                { "cpu",            SD_JSON_VARIANT_OBJECT, oci_cgroup_cpu,      0, 0 },
1498
                { "blockIO",        SD_JSON_VARIANT_OBJECT, oci_cgroup_block_io, 0, 0 },
1499
                { "hugepageLimits", SD_JSON_VARIANT_ARRAY,  oci_unsupported,     0, 0 },
1500
                { "network",        SD_JSON_VARIANT_OBJECT, oci_unsupported,     0, 0 },
1501
                { "pids",           SD_JSON_VARIANT_OBJECT, oci_cgroup_pids,     0, 0 },
1502
                { "rdma",           SD_JSON_VARIANT_OBJECT, oci_unsupported,     0, 0 },
1503
                {}
1504
        };
1505

1506
        return oci_dispatch(v, table, flags, userdata);
2✔
1507
}
1508

1509
static bool sysctl_key_valid(const char *s) {
3✔
1510
        bool dot = true;
3✔
1511

1512
        /* Note that we are a bit stricter here than in systemd-sysctl, as that inherited semantics from the old sysctl
1513
         * tool, which were really weird (as it swaps / and . in both ways) */
1514

1515
        if (isempty(s))
3✔
1516
                return false;
1517

1518
        for (; *s; s++) {
37✔
1519

1520
                if (*s <= ' ' || *s >= 127)
35✔
1521
                        return false;
1522
                if (*s == '/')
35✔
1523
                        return false;
1524
                if (*s == '.') {
35✔
1525

1526
                        if (dot) /* Don't allow two dots next to each other (or at the beginning) */
4✔
1527
                                return false;
1528

1529
                        dot = true;
1530
                } else
1531
                        dot = false;
1532
        }
1533

1534
        if (dot) /* don't allow a dot at the end */
2✔
UNCOV
1535
                return false;
×
1536

1537
        return true;
1538
}
1539

1540
static int oci_sysctl(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
3✔
1541
        Settings *s = ASSERT_PTR(userdata);
3✔
1542
        sd_json_variant *w;
3✔
1543
        const char *k;
3✔
1544
        int r;
3✔
1545

1546
        JSON_VARIANT_OBJECT_FOREACH(k, w, v) {
5✔
1547
                const char *m;
4✔
1548

1549
                if (!sd_json_variant_is_string(w))
4✔
1550
                        return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
2✔
1551
                                        "sysctl parameter is not a string, refusing.");
1552

1553
                assert_se(m = sd_json_variant_string(w));
3✔
1554

1555
                if (!sysctl_key_valid(k))
3✔
1556
                        return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
1✔
1557
                                        "sysctl key invalid, refusing: %s", k);
1558

1559
                r = strv_extend_many(&s->sysctl, k, m);
2✔
1560
                if (r < 0)
2✔
UNCOV
1561
                        return log_oom();
×
1562
        }
1563

1564
        return 0;
1✔
1565
}
1566

1567
#if HAVE_SECCOMP
1568
static int oci_seccomp_action_from_string(const char *name, uint32_t *ret) {
5✔
1569

1570
        static const struct {
5✔
1571
                const char *name;
1572
                uint32_t action;
1573
        } table[] = {
1574
                { "SCMP_ACT_ALLOW",         SCMP_ACT_ALLOW        },
1575
                { "SCMP_ACT_ERRNO",         SCMP_ACT_ERRNO(EPERM) }, /* the OCI spec doesn't document the error, but it appears EPERM is supposed to be used */
1576
                { "SCMP_ACT_KILL",          SCMP_ACT_KILL         },
1577
#ifdef SCMP_ACT_KILL_PROCESS
1578
                { "SCMP_ACT_KILL_PROCESS",  SCMP_ACT_KILL_PROCESS },
1579
#endif
1580
#ifdef SCMP_ACT_KILL_THREAD
1581
                { "SCMP_ACT_KILL_THREAD",   SCMP_ACT_KILL_THREAD  },
1582
#endif
1583
#ifdef SCMP_ACT_LOG
1584
                { "SCMP_ACT_LOG",           SCMP_ACT_LOG          },
1585
#endif
1586
                { "SCMP_ACT_TRAP",          SCMP_ACT_TRAP         },
1587

1588
                /* We don't support SCMP_ACT_TRACE because that requires a tracer, and that doesn't really make sense
1589
                 * here */
1590
        };
1591

1592
        FOREACH_ELEMENT(i, table)
14✔
1593
                if (streq_ptr(name, i->name)) {
13✔
1594
                        *ret = i->action;
4✔
1595
                        return 0;
4✔
1596
                }
1597

1598
        return -EINVAL;
1599
}
1600

1601
static int oci_seccomp_arch_from_string(const char *name, uint32_t *ret) {
2✔
1602

1603
        static const struct {
2✔
1604
                const char *name;
1605
                uint32_t arch;
1606
        } table[] = {
1607
                { "SCMP_ARCH_AARCH64",     SCMP_ARCH_AARCH64     },
1608
                { "SCMP_ARCH_ARM",         SCMP_ARCH_ARM         },
1609
#ifdef SCMP_ARCH_LOONGARCH64
1610
                { "SCMP_ARCH_LOONGARCH64", SCMP_ARCH_LOONGARCH64 },
1611
#endif
1612
                { "SCMP_ARCH_MIPS",        SCMP_ARCH_MIPS        },
1613
                { "SCMP_ARCH_MIPS64",      SCMP_ARCH_MIPS64      },
1614
                { "SCMP_ARCH_MIPS64N32",   SCMP_ARCH_MIPS64N32   },
1615
                { "SCMP_ARCH_MIPSEL",      SCMP_ARCH_MIPSEL      },
1616
                { "SCMP_ARCH_MIPSEL64",    SCMP_ARCH_MIPSEL64    },
1617
                { "SCMP_ARCH_MIPSEL64N32", SCMP_ARCH_MIPSEL64N32 },
1618
                { "SCMP_ARCH_NATIVE",      SCMP_ARCH_NATIVE      },
1619
#ifdef SCMP_ARCH_PARISC
1620
                { "SCMP_ARCH_PARISC",      SCMP_ARCH_PARISC      },
1621
#endif
1622
#ifdef SCMP_ARCH_PARISC64
1623
                { "SCMP_ARCH_PARISC64",    SCMP_ARCH_PARISC64    },
1624
#endif
1625
                { "SCMP_ARCH_PPC",         SCMP_ARCH_PPC         },
1626
                { "SCMP_ARCH_PPC64",       SCMP_ARCH_PPC64       },
1627
                { "SCMP_ARCH_PPC64LE",     SCMP_ARCH_PPC64LE     },
1628
#ifdef SCMP_ARCH_RISCV64
1629
                { "SCMP_ARCH_RISCV64",     SCMP_ARCH_RISCV64     },
1630
#endif
1631
                { "SCMP_ARCH_S390",        SCMP_ARCH_S390        },
1632
                { "SCMP_ARCH_S390X",       SCMP_ARCH_S390X       },
1633
                { "SCMP_ARCH_X32",         SCMP_ARCH_X32         },
1634
                { "SCMP_ARCH_X86",         SCMP_ARCH_X86         },
1635
                { "SCMP_ARCH_X86_64",      SCMP_ARCH_X86_64      },
1636
        };
1637

1638
        FOREACH_ELEMENT(i, table)
22✔
1639
                if (streq_ptr(i->name, name)) {
22✔
1640
                        *ret = i->arch;
2✔
1641
                        return 0;
2✔
1642
                }
1643

1644
        return -EINVAL;
1645
}
1646

1647
static int oci_seccomp_compare_from_string(const char *name, enum scmp_compare *ret) {
2✔
1648

1649
        static const struct {
2✔
1650
                const char *name;
1651
                enum scmp_compare op;
1652
        } table[] = {
1653
                { "SCMP_CMP_NE",        SCMP_CMP_NE        },
1654
                { "SCMP_CMP_LT",        SCMP_CMP_LT        },
1655
                { "SCMP_CMP_LE",        SCMP_CMP_LE        },
1656
                { "SCMP_CMP_EQ",        SCMP_CMP_EQ        },
1657
                { "SCMP_CMP_GE",        SCMP_CMP_GE        },
1658
                { "SCMP_CMP_GT",        SCMP_CMP_GT        },
1659
                { "SCMP_CMP_MASKED_EQ", SCMP_CMP_MASKED_EQ },
1660
        };
1661

1662
        FOREACH_ELEMENT(i, table)
8✔
1663
                if (streq_ptr(i->name, name)) {
8✔
1664
                        *ret = i->op;
2✔
1665
                        return 0;
2✔
1666
                }
1667

1668
        return -EINVAL;
1669
}
1670

1671
static int oci_seccomp_archs(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
1✔
1672
        scmp_filter_ctx *sc = ASSERT_PTR(userdata);
1✔
1673
        sd_json_variant *e;
1✔
1674
        int r;
1✔
1675

1676
        JSON_VARIANT_ARRAY_FOREACH(e, v) {
3✔
1677
                uint32_t a;
2✔
1678

1679
                if (!sd_json_variant_is_string(e))
2✔
UNCOV
1680
                        return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL),
×
1681
                                        "Architecture entry is not a string.");
1682

1683
                r = oci_seccomp_arch_from_string(sd_json_variant_string(e), &a);
2✔
1684
                if (r < 0)
2✔
UNCOV
1685
                        return json_log(e, flags, r, "Unknown architecture: %s", sd_json_variant_string(e));
×
1686

1687
                r = seccomp_arch_add(sc, a);
2✔
1688
                if (r == -EEXIST)
2✔
1689
                        continue;
1✔
1690
                if (r < 0)
1✔
UNCOV
1691
                        return json_log(e, flags, r, "Failed to add architecture to seccomp filter: %m");
×
1692
        }
1693

1694
        return 0;
1✔
1695
}
1696

1697
struct syscall_rule {
1698
        char **names;
1699
        uint32_t action;
1700
        struct scmp_arg_cmp *arguments;
1701
        size_t n_arguments;
1702
};
1703

1704
static void syscall_rule_done(struct syscall_rule *rule) {
2✔
1705
        assert(rule);
2✔
1706

1707
        strv_free(rule->names);
2✔
1708
        free(rule->arguments);
2✔
1709
};
2✔
1710

1711
static int oci_seccomp_action(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
2✔
1712
        uint32_t *action = ASSERT_PTR(userdata);
2✔
1713
        int r;
2✔
1714

1715
        r = oci_seccomp_action_from_string(sd_json_variant_string(v), action);
2✔
1716
        if (r < 0)
2✔
UNCOV
1717
                return json_log(v, flags, r, "Unknown system call action '%s': %m", sd_json_variant_string(v));
×
1718

1719
        return 0;
1720
}
1721

1722
static int oci_seccomp_op(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
2✔
1723
        enum scmp_compare *op = ASSERT_PTR(userdata);
2✔
1724
        int r;
2✔
1725

1726
        r = oci_seccomp_compare_from_string(sd_json_variant_string(v), op);
2✔
1727
        if (r < 0)
2✔
UNCOV
1728
                return json_log(v, flags, r, "Unknown seccomp operator '%s': %m", sd_json_variant_string(v));
×
1729

1730
        return 0;
1731
}
1732

1733
static int oci_seccomp_args(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
1✔
1734
        struct syscall_rule *rule = ASSERT_PTR(userdata);
1✔
1735
        sd_json_variant *e;
1✔
1736
        int r;
1✔
1737

1738
        JSON_VARIANT_ARRAY_FOREACH(e, v) {
3✔
1739
                static const sd_json_dispatch_field table[] = {
2✔
1740
                        { "index",    SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uint32, offsetof(struct scmp_arg_cmp, arg),     SD_JSON_MANDATORY },
1741
                        { "value",    SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uint64, offsetof(struct scmp_arg_cmp, datum_a), SD_JSON_MANDATORY },
1742
                        { "valueTwo", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uint64, offsetof(struct scmp_arg_cmp, datum_b), 0                 },
1743
                        { "op",       SD_JSON_VARIANT_STRING,   oci_seccomp_op,          offsetof(struct scmp_arg_cmp, op),      SD_JSON_MANDATORY },
1744
                        {},
1745
                };
1746

1747
                struct scmp_arg_cmp *p;
2✔
1748
                int expected;
2✔
1749

1750
                if (!GREEDY_REALLOC(rule->arguments, rule->n_arguments + 1))
2✔
UNCOV
1751
                        return log_oom();
×
1752

1753
                p = rule->arguments + rule->n_arguments;
2✔
1754

1755
                *p = (struct scmp_arg_cmp) {
2✔
1756
                        .arg = 0,
1757
                        .datum_a = 0,
1758
                        .datum_b = 0,
1759
                        .op = 0,
1760
                };
1761

1762
                r = oci_dispatch(e, table, flags, p);
2✔
1763
                if (r < 0)
2✔
1764
                        return r;
1765

1766
                expected = p->op == SCMP_CMP_MASKED_EQ ? 4 : 3;
2✔
1767
                if (r != expected)
2✔
UNCOV
1768
                        json_log(e, flags|SD_JSON_WARNING, 0, "Wrong number of system call arguments for JSON data, ignoring.");
×
1769

1770
                /* Note that we are a bit sloppy here and do not insist that SCMP_CMP_MASKED_EQ gets two datum values,
1771
                 * and the other only one. That's because buildah for example by default calls things with
1772
                 * SCMP_CMP_MASKED_EQ but only one argument. We use 0 when the value is not specified. */
1773

1774
                rule->n_arguments++;
2✔
1775
        }
1776

1777
        return 0;
1✔
1778
}
1779

1780
static int oci_seccomp_syscalls(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
2✔
1781
        scmp_filter_ctx *sc = ASSERT_PTR(userdata);
2✔
1782
        sd_json_variant *e;
2✔
1783
        int r;
2✔
1784

1785
        JSON_VARIANT_ARRAY_FOREACH(e, v) {
3✔
1786
                static const sd_json_dispatch_field table[] = {
2✔
1787
                        { "names",  SD_JSON_VARIANT_ARRAY,  sd_json_dispatch_strv, offsetof(struct syscall_rule, names),  SD_JSON_MANDATORY },
1788
                        { "action", SD_JSON_VARIANT_STRING, oci_seccomp_action,    offsetof(struct syscall_rule, action), SD_JSON_MANDATORY },
1789
                        { "args",   SD_JSON_VARIANT_ARRAY,  oci_seccomp_args,      0,                                     0              },
1790
                        {}
1791
                };
1792
                _cleanup_(syscall_rule_done) struct syscall_rule rule = {
2✔
1793
                        .action = UINT32_MAX,
1794
                };
1795

1796
                r = oci_dispatch(e, table, flags, &rule);
2✔
1797
                if (r < 0)
2✔
1798
                        return r;
1799

1800
                if (strv_isempty(rule.names))
2✔
1801
                        return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL), "System call name list is empty.");
1✔
1802

1803
                STRV_FOREACH(i, rule.names) {
3✔
1804
                        int nr;
2✔
1805

1806
                        nr = seccomp_syscall_resolve_name(*i);
2✔
1807
                        if (nr == __NR_SCMP_ERROR) {
2✔
1808
                                log_debug("Unknown syscall %s, skipping.", *i);
×
UNCOV
1809
                                continue;
×
1810
                        }
1811

1812
                        r = seccomp_rule_add_array(sc, rule.action, nr, rule.n_arguments, rule.arguments);
2✔
1813
                        if (r < 0)
2✔
1814
                                return r;
1815
                }
1816
        }
1817

1818
        return 0;
1✔
1819
}
1820
#endif
1821

1822
static int oci_seccomp(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
5✔
1823

1824
#if HAVE_SECCOMP
1825
        static const sd_json_dispatch_field table[] = {
5✔
1826
                { "defaultAction", SD_JSON_VARIANT_STRING, NULL,                 0, SD_JSON_MANDATORY },
1827
                { "architectures", SD_JSON_VARIANT_ARRAY,  oci_seccomp_archs,    0, 0                 },
1828
                { "syscalls",      SD_JSON_VARIANT_ARRAY,  oci_seccomp_syscalls, 0, 0                 },
1829
                {}
1830
        };
1831

1832
        _cleanup_(seccomp_releasep) scmp_filter_ctx sc = NULL;
5✔
1833
        Settings *s = ASSERT_PTR(userdata);
5✔
1834
        sd_json_variant *def;
5✔
1835
        uint32_t d;
5✔
1836
        int r;
5✔
1837

1838
        def = sd_json_variant_by_key(v, "defaultAction");
5✔
1839
        if (!def)
5✔
1840
                return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL), "defaultAction element missing.");
1✔
1841

1842
        if (!sd_json_variant_is_string(def))
4✔
1843
                return json_log(def, flags, SYNTHETIC_ERRNO(EINVAL), "defaultAction is not a string.");
1✔
1844

1845
        r = oci_seccomp_action_from_string(sd_json_variant_string(def), &d);
3✔
1846
        if (r < 0)
3✔
1847
                return json_log(def, flags, r, "Unknown default action: %s", sd_json_variant_string(def));
1✔
1848

1849
        sc = seccomp_init(d);
2✔
1850
        if (!sc)
2✔
UNCOV
1851
                return json_log(v, flags, SYNTHETIC_ERRNO(ENOMEM), "Couldn't allocate seccomp object.");
×
1852

1853
        r = oci_dispatch(v, table, flags, sc);
2✔
1854
        if (r < 0)
2✔
1855
                return r;
1856

1857
        seccomp_release(s->seccomp);
1✔
1858
        s->seccomp = TAKE_PTR(sc);
1✔
1859
        return 0;
1✔
1860
#else
1861
        return json_log(v, flags, SYNTHETIC_ERRNO(EOPNOTSUPP), "libseccomp support not enabled, can't parse seccomp object.");
1862
#endif
1863
}
1864

1865
static int oci_rootfs_propagation(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
1✔
1866
        const char *s;
1✔
1867

1868
        s = sd_json_variant_string(v);
1✔
1869

1870
        if (streq(s, "shared"))
1✔
1871
                return 0;
1872

UNCOV
1873
        json_log(v, flags|SD_JSON_DEBUG, 0, "Ignoring rootfsPropagation setting '%s'.", s);
×
1874
        return 0;
1875
}
1876

1877
static int oci_masked_paths(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
3✔
1878
        Settings *s = ASSERT_PTR(userdata);
3✔
1879
        sd_json_variant *e;
3✔
1880

1881
        JSON_VARIANT_ARRAY_FOREACH(e, v) {
7✔
1882
                _cleanup_free_ char *destination = NULL;
2✔
1883
                CustomMount *m;
6✔
1884
                const char *p;
6✔
1885

1886
                if (!sd_json_variant_is_string(e))
6✔
1887
                        return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
1✔
1888
                                        "Path is not a string, refusing.");
1889

1890
                assert_se(p = sd_json_variant_string(e));
5✔
1891

1892
                if (!path_is_absolute(p))
5✔
1893
                        return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
1✔
1894
                                        "Path is not absolute, refusing: %s", p);
1895

1896
                if (oci_exclude_mount(p))
4✔
1897
                        continue;
1✔
1898

1899
                destination = strdup(p);
3✔
1900
                if (!destination)
3✔
UNCOV
1901
                        return log_oom();
×
1902

1903
                m = custom_mount_add(&s->custom_mounts, &s->n_custom_mounts, CUSTOM_MOUNT_INACCESSIBLE);
3✔
1904
                if (!m)
3✔
UNCOV
1905
                        return log_oom();
×
1906

1907
                m->destination = TAKE_PTR(destination);
3✔
1908

1909
                /* The spec doesn't say this, but apparently pre-existing implementations are lenient towards
1910
                 * non-existing paths to mask. Let's hence be too. */
1911
                m->graceful = true;
3✔
1912
        }
1913

1914
        return 0;
1✔
1915
}
1916

1917
static int oci_readonly_paths(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
3✔
1918
        Settings *s = ASSERT_PTR(userdata);
3✔
1919
        sd_json_variant *e;
3✔
1920

1921
        JSON_VARIANT_ARRAY_FOREACH(e, v) {
7✔
1922
                _cleanup_free_ char *source = NULL, *destination = NULL;
6✔
1923
                CustomMount *m;
6✔
1924
                const char *p;
6✔
1925

1926
                if (!sd_json_variant_is_string(e))
6✔
1927
                        return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
1✔
1928
                                        "Path is not a string, refusing.");
1929

1930
                assert_se(p = sd_json_variant_string(e));
5✔
1931

1932
                if (!path_is_absolute(p))
5✔
1933
                        return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
1✔
1934
                                        "Path is not absolute, refusing: %s", p);
1935

1936
                if (oci_exclude_mount(p))
4✔
1937
                        continue;
1✔
1938

1939
                source = strjoin("+", p);
3✔
1940
                if (!source)
3✔
UNCOV
1941
                        return log_oom();
×
1942

1943
                destination = strdup(p);
3✔
1944
                if (!destination)
3✔
UNCOV
1945
                        return log_oom();
×
1946

1947
                m = custom_mount_add(&s->custom_mounts, &s->n_custom_mounts, CUSTOM_MOUNT_BIND);
3✔
1948
                if (!m)
3✔
UNCOV
1949
                        return log_oom();
×
1950

1951
                m->source = TAKE_PTR(source);
3✔
1952
                m->destination = TAKE_PTR(destination);
3✔
1953
                m->read_only = true;
3✔
1954
        }
1955

1956
        return 0;
1✔
1957
}
1958

1959
static int oci_linux(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
18✔
1960

1961
        static const sd_json_dispatch_field table[] = {
18✔
1962
                { "namespaces",        SD_JSON_VARIANT_ARRAY,  oci_namespaces,         0, 0                  },
1963
                { "uidMappings",       SD_JSON_VARIANT_ARRAY,  oci_uid_gid_mappings,   0, 0                  },
1964
                { "gidMappings",       SD_JSON_VARIANT_ARRAY,  oci_uid_gid_mappings,   0, 0                  },
1965
                { "devices",           SD_JSON_VARIANT_ARRAY,  oci_devices,            0, 0                  },
1966
                { "cgroupsPath",       SD_JSON_VARIANT_STRING, oci_cgroups_path,       0, 0                  },
1967
                { "resources",         SD_JSON_VARIANT_OBJECT, oci_resources,          0, 0                  },
1968
                { "intelRdt",          SD_JSON_VARIANT_OBJECT, oci_unsupported,        0, SD_JSON_PERMISSIVE },
1969
                { "sysctl",            SD_JSON_VARIANT_OBJECT, oci_sysctl,             0, 0                  },
1970
                { "seccomp",           SD_JSON_VARIANT_OBJECT, oci_seccomp,            0, 0                  },
1971
                { "rootfsPropagation", SD_JSON_VARIANT_STRING, oci_rootfs_propagation, 0, 0                  },
1972
                { "maskedPaths",       SD_JSON_VARIANT_ARRAY,  oci_masked_paths,       0, 0                  },
1973
                { "readonlyPaths",     SD_JSON_VARIANT_ARRAY,  oci_readonly_paths,     0, 0                  },
1974
                { "mountLabel",        SD_JSON_VARIANT_STRING, oci_unsupported,        0, SD_JSON_PERMISSIVE },
1975
                {}
1976
        };
1977

1978
        return oci_dispatch(v, table, flags, userdata);
18✔
1979
}
1980

1981
static int oci_hook_timeout(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
2✔
1982
        usec_t *u = ASSERT_PTR(userdata);
2✔
1983
        uint64_t k;
2✔
1984

1985
        k = sd_json_variant_unsigned(v);
2✔
1986
        if (k == 0 || k > (UINT64_MAX-1)/USEC_PER_SEC)
2✔
1987
                return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
1✔
1988
                                "Hook timeout value out of range.");
1989

1990
        *u = k * USEC_PER_SEC;
1✔
1991
        return 0;
1✔
1992
}
1993

1994
static int oci_hooks_array(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
4✔
1995
        Settings *s = ASSERT_PTR(userdata);
4✔
1996
        sd_json_variant *e;
4✔
1997
        int r;
4✔
1998

1999
        JSON_VARIANT_ARRAY_FOREACH(e, v) {
8✔
2000

2001
                static const sd_json_dispatch_field table[] = {
5✔
2002
                        { "path",    SD_JSON_VARIANT_STRING,   json_dispatch_path, offsetof(OciHook, path),    SD_JSON_MANDATORY },
2003
                        { "args",    SD_JSON_VARIANT_ARRAY,    oci_args,           offsetof(OciHook, args),    0,                },
2004
                        { "env",     SD_JSON_VARIANT_ARRAY,    oci_env,            offsetof(OciHook, env),     0                 },
2005
                        { "timeout", SD_JSON_VARIANT_UNSIGNED, oci_hook_timeout,   offsetof(OciHook, timeout), 0                 },
2006
                        {}
2007
                };
2008

2009
                OciHook **array, *new_item;
5✔
2010
                size_t *n_array;
5✔
2011

2012
                if (streq(name, "prestart")) {
5✔
2013
                        array = &s->oci_hooks_prestart;
3✔
2014
                        n_array = &s->n_oci_hooks_prestart;
3✔
2015
                } else if (streq(name, "poststart")) {
2✔
2016
                        array = &s->oci_hooks_poststart;
1✔
2017
                        n_array = &s->n_oci_hooks_poststart;
1✔
2018
                } else {
2019
                        assert(streq(name, "poststop"));
1✔
2020
                        array = &s->oci_hooks_poststop;
1✔
2021
                        n_array = &s->n_oci_hooks_poststop;
1✔
2022
                }
2023

2024
                if (!GREEDY_REALLOC(*array, *n_array + 1))
5✔
UNCOV
2025
                        return log_oom();
×
2026

2027
                new_item = *array + *n_array;
5✔
2028

2029
                *new_item = (OciHook) {
5✔
2030
                        .timeout = USEC_INFINITY,
2031
                };
2032

2033
                r = oci_dispatch(e, table, flags, new_item);
5✔
2034
                if (r < 0) {
5✔
2035
                        free(new_item->path);
1✔
2036
                        strv_free(new_item->args);
1✔
2037
                        strv_free(new_item->env);
1✔
2038
                        return r;
1✔
2039
                }
2040

2041
                (*n_array)++;
4✔
2042
        }
2043

2044
        return 0;
3✔
2045
}
2046

2047
static int oci_hooks(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
2✔
2048

2049
        static const sd_json_dispatch_field table[] = {
2✔
2050
                { "prestart",  SD_JSON_VARIANT_ARRAY, oci_hooks_array, 0, 0 },
2051
                { "poststart", SD_JSON_VARIANT_ARRAY, oci_hooks_array, 0, 0 },
2052
                { "poststop",  SD_JSON_VARIANT_ARRAY, oci_hooks_array, 0, 0 },
2053
                {}
2054
        };
2055

2056
        return oci_dispatch(v, table, flags, userdata);
2✔
2057
}
2058

2059
static int oci_annotations(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
3✔
2060
        sd_json_variant *w;
3✔
2061
        const char *k;
3✔
2062

2063
        JSON_VARIANT_OBJECT_FOREACH(k, w, v) {
5✔
2064

2065
                if (isempty(k))
4✔
2066
                        return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
2✔
2067
                                        "Annotation with empty key, refusing.");
2068

2069
                if (!sd_json_variant_is_string(w))
3✔
2070
                        return json_log(w, flags, SYNTHETIC_ERRNO(EINVAL),
1✔
2071
                                        "Annotation has non-string value, refusing.");
2072

2073
                json_log(w, flags|SD_JSON_DEBUG, 0, "Ignoring annotation '%s' with value '%s'.", k, sd_json_variant_string(w));
2✔
2074
        }
2075

2076
        return 0;
1✔
2077
}
2078

2079
int oci_load(FILE *f, const char *bundle, Settings **ret) {
35✔
2080

2081
        static const sd_json_dispatch_field table[] = {
35✔
2082
                { "ociVersion",  SD_JSON_VARIANT_STRING, NULL,            0, SD_JSON_MANDATORY },
2083
                { "process",     SD_JSON_VARIANT_OBJECT, oci_process,     0, 0                 },
2084
                { "root",        SD_JSON_VARIANT_OBJECT, oci_root,        0, 0                 },
2085
                { "hostname",    SD_JSON_VARIANT_STRING, oci_hostname,    0, 0                 },
2086
                { "mounts",      SD_JSON_VARIANT_ARRAY,  oci_mounts,      0, 0                 },
2087
                { "linux",       SD_JSON_VARIANT_OBJECT, oci_linux,       0, 0                 },
2088
                { "hooks",       SD_JSON_VARIANT_OBJECT, oci_hooks,       0, 0                 },
2089
                { "annotations", SD_JSON_VARIANT_OBJECT, oci_annotations, 0, 0                 },
2090
                {}
2091
        };
2092

2093
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *oci = NULL;
35✔
2094
        _cleanup_(settings_freep) Settings *s = NULL;
35✔
2095
        unsigned line = 0, column = 0;
35✔
2096
        sd_json_variant *v;
35✔
2097
        const char *path;
35✔
2098
        int r;
35✔
2099

2100
        assert_se(bundle);
35✔
2101

2102
        path = strjoina(bundle, "/config.json");
175✔
2103

2104
        r = sd_json_parse_file(f, path, 0, &oci, &line, &column);
35✔
2105
        if (r < 0) {
35✔
2106
                if (line != 0 && column != 0)
×
UNCOV
2107
                        return log_error_errno(r, "Failed to parse '%s' at %u:%u: %m", path, line, column);
×
2108
                else
UNCOV
2109
                        return log_error_errno(r, "Failed to parse '%s': %m", path);
×
2110
        }
2111

2112
        v = sd_json_variant_by_key(oci, "ociVersion");
35✔
2113
        if (!v)
35✔
UNCOV
2114
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
2115
                                       "JSON file '%s' is not an OCI bundle configuration file. Refusing.",
2116
                                       path);
2117
        if (!streq_ptr(sd_json_variant_string(v), "1.0.0"))
35✔
2118
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1✔
2119
                                       "OCI bundle version not supported: %s",
2120
                                       strna(sd_json_variant_string(v)));
2121

2122
        // {
2123
        //         _cleanup_free_ char *formatted = NULL;
2124
        //         assert_se(json_variant_format(oci, SD_JSON_FORMAT_PRETTY|JSON_FORMAT_COLOR, &formatted) >= 0);
2125
        //         fputs(formatted, stdout);
2126
        // }
2127

2128
        s = settings_new();
34✔
2129
        if (!s)
34✔
UNCOV
2130
                return log_oom();
×
2131

2132
        s->start_mode = START_PID1;
34✔
2133
        s->resolv_conf = RESOLV_CONF_OFF;
34✔
2134
        s->link_journal = LINK_NO;
34✔
2135
        s->timezone = TIMEZONE_OFF;
34✔
2136

2137
        s->bundle = strdup(bundle);
34✔
2138
        if (!s->bundle)
34✔
UNCOV
2139
                return log_oom();
×
2140

2141
        r = oci_dispatch(oci, table, 0, s);
34✔
2142
        if (r < 0)
34✔
2143
                return r;
2144

2145
        if (s->properties) {
2✔
2146
                r = sd_bus_message_seal(s->properties, 0, 0);
1✔
2147
                if (r < 0)
1✔
UNCOV
2148
                        return log_error_errno(r, "Cannot seal properties bus message: %m");
×
2149
        }
2150

2151
        *ret = TAKE_PTR(s);
2✔
2152
        return 0;
2✔
2153
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc