eBPF-based security platform
bpm: read and write the package state in util/bpm_state.c
Daniel Borkmann5cabb435ed0f
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex 7bd81275..6ba135b0 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -492,12 +492,13 @@ signed config image.Unit suites and fuzz targets~~~~~~~~~~~~~~~~~~~~~~~~~~~~-The unit suites in :src:`testing/unit/` cover eighteen of the modules in-:src:`util/`: ``attest``, ``bpm_oci``, ``bpm_package``, ``bpm_tree``,-``bpm_tree_state``, ``chain``, ``eventlog``, ``format``, ``hex``, ``io``,-``json``, ``kernel_abi``, ``kernel_build_id``, ``kernel_release``,-``paths``, ``string``, ``upatch`` and ``upatch_decode``, and ``overflow``-the arithmetic of :src:`include/shirudo/overflow.h`. The other modules+The unit suites in :src:`testing/unit/` cover nineteen of the modules in+:src:`util/`: ``attest``, ``bpm_oci``, ``bpm_package``, ``bpm_state``,+``bpm_tree``, ``bpm_tree_state``, ``chain``, ``eventlog``, ``format``,+``hex``, ``io``, ``json``, ``kernel_abi``, ``kernel_build_id``,+``kernel_release``, ``paths``, ``string``, ``upatch`` and+``upatch_decode``, and ``overflow`` the arithmetic of+:src:`include/shirudo/overflow.h`. The other modulesare reached through the test helpers, the fuzz targets and the binariesthe functional tests run.diff --git a/Makefile.shirudo b/Makefile.shirudoindex f2acfec2..d7873bf7 100644--- a/Makefile.shirudo+++ b/Makefile.shirudo@@ -244,6 +244,7 @@ UTIL_SRCS := \util/bpm_tree_state.c \util/bpm_oci.c \util/bpm_package.c \+ util/bpm_state.c \util/kernel_build_id.c \util/kernel_release.c \util/config.c \diff --git a/builtin-bpm.c b/builtin-bpm.cindex f73aab6b..776fc5be 100644--- a/builtin-bpm.c+++ b/builtin-bpm.c@@ -35,6 +35,7 @@#include "builtin.h"#include "util/bpm_oci.h"#include "util/bpm_package.h"+#include "util/bpm_state.h"#include "util/bpm_tree.h"#include "util/bpm_source_config.h"#include "util/chain.h"@@ -59,13 +60,6 @@ struct bpm_source {char key_file[BPM_MAX_KEY];};-struct bpm_state_entry {- char name[BPM_MAX_NAME];- char version[BPM_MAX_NAME];- char source[BPM_MAX_NAME];- char path[PATH_MAX];-};-static int sha256_file_hex(const char *path, char *hex, size_t hex_sz);static int remove_tree(const char *path);@@ -945,112 +939,6 @@ static int list_keys(bool json)return ret;}-struct state_sink {- struct bpm_state_entry *items;- int n;- struct bpm_state_entry scratch;-};--static int on_state_entry(void *ud)-{- struct state_sink *s = ud;- char expected[PATH_MAX];-- if (s->n >= BPM_MAX_STATE || !s->scratch.name[0] || !s->scratch.version[0])- return -E2BIG;- if (!bpm_name_ok(s->scratch.name) || !bpm_version_ok(s->scratch.version) ||- !bpm_name_ok(s->scratch.source))- return -EINVAL;- if (strsprintf(expected, sizeof(expected), "%s/%s/%s",- BPM_PACKAGES_DIR, s->scratch.name,- s->scratch.version) < 0 ||- strcmp(expected, s->scratch.path))- return -EINVAL;- s->items[s->n++] = s->scratch;- /* A field the next entry leaves out is not this entry's. */- memset(&s->scratch, 0, sizeof(s->scratch));- return 0;-}--static int state_read(struct bpm_state_entry *st, int *n)-{- __u8 *body;- size_t len;- int ret;- struct state_sink sink = { .items = st };- struct json_array arr = { on_state_entry, &sink };- struct json_field sub[4], root[1];-- ret = read_file(BPM_STATE_PATH, 8u * 1024u * 1024u,- &body, &len);- if (ret == -ENOENT) {- *n = 0;- return 0;- }- if (ret)- return ret;- sub[0] = (struct json_field){- .key = "name", .type = JSON_T_STRING,- .dst = sink.scratch.name, .dst_cap = sizeof(sink.scratch.name),- };- sub[1] = (struct json_field){- .key = "version", .type = JSON_T_STRING,- .dst = sink.scratch.version,- .dst_cap = sizeof(sink.scratch.version),- };- sub[2] = (struct json_field){- .key = "source", .type = JSON_T_STRING,- .dst = sink.scratch.source,- .dst_cap = sizeof(sink.scratch.source),- };- sub[3] = (struct json_field){- .key = "path", .type = JSON_T_STRING,- .dst = sink.scratch.path, .dst_cap = sizeof(sink.scratch.path),- };- root[0] = (struct json_field){- .key = "packages", .type = JSON_T_ARRAY,- .dst = &arr, .sub = sub, .n_sub = ARRAY_SIZE(sub),- };- ret = json_parse((const char *)body, len, root, ARRAY_SIZE(root));- xfree(body, len + 1);- if (ret || !root[0].seen)- return ret ? ret : -EINVAL;- *n = sink.n;- return 0;-}--static int state_write(const struct bpm_state_entry *st, int n)-{- const size_t cap = 1024 * 1024;- struct json_emit e = {};- char *buf;- int ret;-- buf = xmalloc(cap);- if (!buf)- return -ENOMEM;- e = (struct json_emit){ .buf = buf, .cap = cap };- json_obj_open(&e);- json_kv_int(&e, "schema", 1);- json_arr_open(&e, "packages");- for (int i = 0; i < n; i++) {- json_obj_open(&e);- json_kv_string(&e, "name", st[i].name);- json_kv_string(&e, "version", st[i].version);- json_kv_string(&e, "source", st[i].source);- json_kv_string(&e, "path", st[i].path);- json_obj_close(&e);- }- json_arr_close(&e);- json_obj_close(&e);- ret = e.err ? -E2BIG : seal_mkdir_p(BPM_VAR_DIR, 0700);- if (!ret)- ret = seal_write_file(BPM_STATE_PATH, buf, e.len,- 0600);- xfree(buf, cap);- return ret;-}-static int load_cached_index(const char *source, struct bpm_index_entry *entries, int *n){char path[PATH_MAX];@@ -2635,7 +2523,7 @@ static int bpm_install(const char *ref, struct bpm_tables *t)sizeof(name), version, sizeof(version));}if (!ret)- ret = state_read(state, &n_state);+ ret = bpm_state_read(state, &n_state);if (ret)return bpm_finish(lock, ret, "install");for (i = 0; i < n_state; i++) {@@ -2674,7 +2562,7 @@ static int bpm_install(const char *ref, struct bpm_tables *t)direct ? selected.name : name,selected.version) < 0)return bpm_finish(lock, -ENAMETOOLONG, "install");- ret = state_write(state, n_state + 1);+ ret = bpm_state_write(state, n_state + 1);if (ret) {/* Unlisted, the package could never be removed. */if (package_uninstall(&state[n_state]))@@ -2698,7 +2586,7 @@ static int bpm_upgrade(const char *unused, struct bpm_tables *t)lock = with_bpm_lock();if (lock < 0)return bpm_finish(-1, lock, "lock");- ret = state_read(state, &n_state);+ ret = bpm_state_read(state, &n_state);if (ret)return bpm_finish(lock, ret, "read state");for (i = 0; i < n_state; i++) {@@ -2738,7 +2626,7 @@ static int bpm_upgrade(const char *unused, struct bpm_tables *t)bpm_copy_string(state[i].path, sizeof(state[i].path), path);}/* Packages upgraded before a failure stay on record. */- wrc = state_write(state, n_state);+ wrc = bpm_state_write(state, n_state);if (ret)return bpm_finish(lock, ret, "upgrade");return bpm_finish(lock, wrc, "write state");@@ -2931,7 +2819,7 @@ int cmd_bpm(int argc, const char **argv)state = xcalloc(BPM_MAX_STATE, sizeof(*state));if (!state)return bpm_fail("list", -ENOMEM);- ret = state_read(state, &n_state);+ ret = bpm_state_read(state, &n_state);if (!ret && json) {size_t cap = 256 + (size_t)n_state * 3 * BPM_MAX_NAME;char *buf = xmalloc(cap);@@ -2993,7 +2881,7 @@ int cmd_bpm(int argc, const char **argv)xfree(state, BPM_MAX_STATE * sizeof(*state));return bpm_finish(-1, lock, "lock");}- ret = state_read(state, &n_state);+ ret = bpm_state_read(state, &n_state);for (i = 0; !ret && i < n_state; i++) {if (!strcmp(state[i].name, argv[2]))break;@@ -3005,7 +2893,7 @@ int cmd_bpm(int argc, const char **argv)if (!ret) {memmove(&state[i], &state[i + 1],(size_t)(n_state - i - 1) * sizeof(*state));- ret = state_write(state, n_state - 1);+ ret = bpm_state_write(state, n_state - 1);}xfree(state, BPM_MAX_STATE * sizeof(*state));return bpm_finish(lock, ret, "uninstall");diff --git a/testing/unit/bpm_state.c b/testing/unit/bpm_state.cnew file mode 100644index 00000000..be4e8d90--- /dev/null+++ b/testing/unit/bpm_state.c@@ -0,0 +1,64 @@+// SPDX-License-Identifier: BSD-2-Clause+/* Copyright (c) 2026 Authors of shirudo */++#include <errno.h>+#include <string.h>++#include <shirudo/string.h>++#include "bpm_state.h"+#include "paths.h"+#include "unit.h"++static void entry(struct bpm_state_entry *e, const char *name,+ const char *version)+{+ ASSERT_TRUE(strscpy(e->name, name, sizeof(e->name)) > 0);+ ASSERT_TRUE(strscpy(e->version, version, sizeof(e->version)) > 0);+ ASSERT_TRUE(strscpy(e->source, "main", sizeof(e->source)) > 0);+ ASSERT_TRUE(strsprintf(e->path, sizeof(e->path), "%s/%s/%s",+ BPM_PACKAGES_DIR, name, version) > 0);+}++static struct bpm_state_entry out[BPM_MAX_STATE];++static void state_reads_back_what_it_writes(void)+{+ struct bpm_state_entry in[2] = {};+ char buf[4096];+ size_t len;+ int n;++ entry(&in[0], "kernel-obs", "1.0.0");+ entry(&in[1], "fix", "2.1.0-rc.1");+ ASSERT_EQ(bpm_state_emit(in, 2, buf, sizeof(buf), &len), 0);+ ASSERT_EQ(bpm_state_parse(buf, len, out, &n), 0);+ EXPECT_EQ(n, 2);+ EXPECT_MEMEQ(out, in, sizeof(in));+ EXPECT_EQ(bpm_state_emit(in, 2, buf, 16, &len), -E2BIG);+}++static void state_refuses_an_entry_out_of_place(void)+{+ struct bpm_state_entry in = {};+ char buf[4096];+ size_t len;+ int n;++ entry(&in, "fix", "1.0.0");+ in.path[strlen(in.path) - 1] = '1';+ ASSERT_EQ(bpm_state_emit(&in, 1, buf, sizeof(buf), &len), 0);+ EXPECT_EQ(bpm_state_parse(buf, len, out, &n), -EINVAL);+ entry(&in, "../fix", "1.0.0");+ ASSERT_EQ(bpm_state_emit(&in, 1, buf, sizeof(buf), &len), 0);+ EXPECT_EQ(bpm_state_parse(buf, len, out, &n), -EINVAL);+ EXPECT_EQ(bpm_state_parse("{\"schema\":1}", 12, out, &n), -EINVAL);+}++static const struct unit_case cases[] = {+ UNIT_CASE(state_reads_back_what_it_writes),+ UNIT_CASE(state_refuses_an_entry_out_of_place),+ {}+};++UNIT_SUITE(bpm_state, cases);diff --git a/util/bpm_state.c b/util/bpm_state.cnew file mode 100644index 00000000..40059295--- /dev/null+++ b/util/bpm_state.c@@ -0,0 +1,136 @@+// SPDX-License-Identifier: BSD-2-Clause+/* Copyright (c) 2026 Authors of shirudo */++#include <errno.h>+#include <string.h>++#include <shirudo/misc.h>+#include <shirudo/string.h>++#include "bpm_state.h"+#include "io.h"+#include "json.h"+#include "malloc.h"+#include "paths.h"+#include "seal.h"++#define STATE_FILE_MAX (8u * 1024u * 1024u)+#define STATE_EMIT_MAX (1024u * 1024u)++struct state_sink {+ struct bpm_state_entry *items;+ int n;+ struct bpm_state_entry scratch;+};++static int on_state_entry(void *ud)+{+ struct state_sink *s = ud;+ char expected[PATH_MAX];++ if (s->n >= BPM_MAX_STATE || !s->scratch.name[0] ||+ !s->scratch.version[0])+ return -E2BIG;+ if (!bpm_name_ok(s->scratch.name) ||+ !bpm_version_ok(s->scratch.version) ||+ !bpm_name_ok(s->scratch.source))+ return -EINVAL;+ if (strsprintf(expected, sizeof(expected), "%s/%s/%s",+ BPM_PACKAGES_DIR, s->scratch.name,+ s->scratch.version) < 0 ||+ strcmp(expected, s->scratch.path))+ return -EINVAL;+ s->items[s->n++] = s->scratch;+ /* A field the next entry leaves out is not this entry's. */+ memset(&s->scratch, 0, sizeof(s->scratch));+ return 0;+}++int bpm_state_parse(const char *body, size_t len,+ struct bpm_state_entry *st, int *n)+{+ struct state_sink sink = { .items = st };+ struct json_array arr = { on_state_entry, &sink };+ struct json_field sub[] = {+ { "name", JSON_T_STRING, sink.scratch.name,+ sizeof(sink.scratch.name), 0, NULL, 0 },+ { "version", JSON_T_STRING, sink.scratch.version,+ sizeof(sink.scratch.version), 0, NULL, 0 },+ { "source", JSON_T_STRING, sink.scratch.source,+ sizeof(sink.scratch.source), 0, NULL, 0 },+ { "path", JSON_T_STRING, sink.scratch.path,+ sizeof(sink.scratch.path), 0, NULL, 0 },+ };+ struct json_field root[] = {+ { "packages", JSON_T_ARRAY, &arr, 0, 0, sub, ARRAY_SIZE(sub) },+ };+ int ret;++ ret = json_parse(body, len, root, ARRAY_SIZE(root));+ if (ret)+ return ret;+ if (!root[0].seen)+ return -EINVAL;+ *n = sink.n;+ return 0;+}++int bpm_state_emit(const struct bpm_state_entry *st, int n, char *buf,+ size_t cap, size_t *len)+{+ struct json_emit e = { .buf = buf, .cap = cap };++ json_obj_open(&e);+ json_kv_int(&e, "schema", 1);+ json_arr_open(&e, "packages");+ for (int i = 0; i < n; i++) {+ json_obj_open(&e);+ json_kv_string(&e, "name", st[i].name);+ json_kv_string(&e, "version", st[i].version);+ json_kv_string(&e, "source", st[i].source);+ json_kv_string(&e, "path", st[i].path);+ json_obj_close(&e);+ }+ json_arr_close(&e);+ json_obj_close(&e);+ if (e.err)+ return -E2BIG;+ *len = e.len;+ return 0;+}++int bpm_state_read(struct bpm_state_entry *st, int *n)+{+ size_t len;+ char *body;+ int ret;++ ret = read_file(BPM_STATE_PATH, STATE_FILE_MAX, &body, &len);+ if (ret == -ENOENT) {+ *n = 0;+ return 0;+ }+ if (ret)+ return ret;+ ret = bpm_state_parse(body, len, st, n);+ xfree(body, len + 1);+ return ret;+}++int bpm_state_write(const struct bpm_state_entry *st, int n)+{+ size_t len = 0;+ char *buf;+ int ret;++ buf = xmalloc(STATE_EMIT_MAX);+ if (!buf)+ return -ENOMEM;+ ret = bpm_state_emit(st, n, buf, STATE_EMIT_MAX, &len);+ if (!ret)+ ret = seal_mkdir_p(BPM_VAR_DIR, 0700);+ if (!ret)+ ret = seal_write_file(BPM_STATE_PATH, buf, len, 0600);+ xfree(buf, STATE_EMIT_MAX);+ return ret;+}diff --git a/util/bpm_state.h b/util/bpm_state.hnew file mode 100644index 00000000..425e1cb7--- /dev/null+++ b/util/bpm_state.h@@ -0,0 +1,40 @@+/* SPDX-License-Identifier: BSD-2-Clause */+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The packages bpm installed, as /var/lib/shirudo/bpm/state.json records+ * them.+ */++#ifndef SHIRUDO_BPM_STATE_H+#define SHIRUDO_BPM_STATE_H++#include <limits.h>+#include <stddef.h>++#include "bpm_oci.h"++struct bpm_state_entry {+ char name[BPM_MAX_NAME];+ char version[BPM_MAX_NAME];+ char source[BPM_MAX_NAME];+ char path[PATH_MAX];+};++/*+ * The len bytes at body as up to BPM_MAX_STATE entries: each a package+ * name and version, the name of the source it came from, and the path+ * under BPM_PACKAGES_DIR that name and version make. 0, -EINVAL or+ * -E2BIG.+ */+int bpm_state_parse(const char *body, size_t len,+ struct bpm_state_entry *st, int *n);+/* The n entries as the document bpm_state_parse() reads. 0 or -E2BIG. */+int bpm_state_emit(const struct bpm_state_entry *st, int n, char *buf,+ size_t cap, size_t *len);++/* The recorded entries, none when there is no state yet. */+int bpm_state_read(struct bpm_state_entry *st, int *n);+int bpm_state_write(const struct bpm_state_entry *st, int n);++#endif /* SHIRUDO_BPM_STATE_H */