Skip to content

eBPF-based security platform

bpm: write no tree operation the node cannot read

Daniel Borkmann609f5f601eb3
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index dc45be64..def4d113 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -491,13 +491,14 @@ signed config image.
Unit suites and fuzz targets
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-The unit suites in :src:`testing/unit/` cover thirteen of the modules in
-:src:`util/`: ``bpm_oci``, ``bpm_package``, ``chain``, ``eventlog``,
-``hex``, ``io``, ``json``, ``kernel_abi``, ``kernel_build_id``,
-``kernel_release``, ``string``, ``upatch`` and ``upatch_decode``, and
-``overflow`` the arithmetic of :src:`include/shirudo/overflow.h`. The
-other modules are reached through the test helpers, the fuzz targets and
-the binaries the functional tests run.
+The unit suites in :src:`testing/unit/` cover fourteen of the modules in
+:src:`util/`: ``bpm_oci``, ``bpm_package``, ``bpm_tree``, ``chain``,
+``eventlog``, ``hex``, ``io``, ``json``, ``kernel_abi``,
+``kernel_build_id``, ``kernel_release``, ``string``, ``upatch`` and
+``upatch_decode``, and ``overflow`` the arithmetic of
+:src:`include/shirudo/overflow.h`. The other modules are reached through
+the test helpers, the fuzz targets and the binaries the functional tests
+run.
.. list-table::
:header-rows: 1
@@ -628,7 +629,8 @@ Coverage by area
* - upatch tree
- ``t3011``, ``t5004``, ``t7005``, ``t7101``, ``t7102``,
``t7103``, ``t7104``, ``t9105``. Fuzz: ``tree``, ``tree-state``,
- ``kconfig``. Unit: ``kernel_build_id``, ``kernel_release``
+ ``kconfig``. Unit: ``bpm_tree``, ``kernel_build_id``,
+ ``kernel_release``
* - Policy labels
- upatch labels and Beefile serialization: ``t3109``, ``t7006``.
Unauthorized policy-xattr changes: ``t6111``. Task and socket
diff --git a/Documentation/upatches/publish/beefile.rst b/Documentation/upatches/publish/beefile.rst
index 2c057c51..7e4b98ed 100644
--- a/Documentation/upatches/publish/beefile.rst
+++ b/Documentation/upatches/publish/beefile.rst
@@ -27,7 +27,10 @@ A Beefile is a small subset of TOML, read line by line.
or nothing.
- A value is a quoted string or an array of quoted strings on the same
line. A ``"..."`` string takes the escapes ``\n``, ``\r``, ``\t``,
- ``\\`` and ``\"``; a ``'...'`` string takes none.
+ ``\\`` and ``\"``; a ``'...'`` string takes none. A tree id or
+ selector value holding a control character, as the first three spell,
+ is refused by ``bpm tree append``, as no node could read the
+ publication.
- ``#`` starts a comment outside a string.
- Numeric values are accepted for policy bit masks; booleans, inline
tables and values that span lines are not supported.
diff --git a/testing/unit/bpm_tree.c b/testing/unit/bpm_tree.c
new file mode 100644
index 00000000..556f52d0
--- /dev/null
+++ b/testing/unit/bpm_tree.c
@@ -0,0 +1,97 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/* Copyright (c) 2026 Authors of shirudo */
+
+#include <errno.h>
+
+#include <shirudo/string.h>
+
+#include "bpm_tree.h"
+#include "malloc.h"
+#include "unit.h"
+
+#define DIGEST "sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+
+static void set(char *dst, size_t cap, const char *src)
+{
+ ASSERT_TRUE(strscpy(dst, src, cap) >= 0);
+}
+
+static struct tree_operation *operation(void)
+{
+ struct tree_operation *op = xcalloc(1, sizeof(*op));
+ struct tree_selector_kv *label;
+
+ ASSERT_TRUE(op);
+ op->seq = 1;
+ set(op->op, sizeof(op->op), "add");
+ set(op->id, sizeof(op->id), "t");
+ set(op->artifact.repository, sizeof(op->artifact.repository),
+ "oci://registry.example/t");
+ set(op->artifact.manifest_digest, sizeof(op->artifact.manifest_digest),
+ DIGEST);
+ set(op->artifact.layer_digest, sizeof(op->artifact.layer_digest),
+ DIGEST);
+ set(op->artifact.payload_sha256, sizeof(op->artifact.payload_sha256),
+ DIGEST);
+ op->selector.n_labels = 1;
+ label = &op->selector.labels[0];
+ set(label->key, sizeof(label->key), "site");
+ label->n_values = 1;
+ set(label->values[0], sizeof(label->values[0]), "a b");
+ return op;
+}
+
+/*
+ * The node's parser refuses a control character, so a publication
+ * holding one would go out, signed, and fail every node's sync.
+ */
+static void operation_takes_no_control_character(void)
+{
+ struct tree_operation *op = operation();
+ struct tree_selector_kv *label = &op->selector.labels[0];
+ char digest[72];
+
+ EXPECT_EQ(tree_operation_digest(op, digest), 0);
+ label->values[0][1] = '\t';
+ EXPECT_EQ(tree_operation_digest(op, digest), -EINVAL);
+ label->values[0][1] = ' ';
+ label->key[0] = '\n';
+ EXPECT_EQ(tree_operation_digest(op, digest), -EINVAL);
+ label->key[0] = 's';
+ op->artifact.repository[0] = '\r';
+ EXPECT_EQ(tree_operation_digest(op, digest), -EINVAL);
+ xfree(op, sizeof(*op));
+}
+
+/* What the publisher writes, the node reads back to the same digest. */
+static void publication_reads_back(void)
+{
+ struct tree_publication pub = { .schema = 1, .generation = 1 };
+ struct tree_publication back = {};
+ struct tree_operation *op = operation();
+ char *doc = NULL, again[72];
+ size_t len = 0;
+
+ set(pub.tree_id, sizeof(pub.tree_id), "t");
+ ASSERT_EQ(tree_operation_digest(op, op->operation_digest), 0);
+ ASSERT_EQ(tree_publication_append(&pub, op), 0);
+ pub.head_seq = 1;
+ ASSERT_EQ(tree_emit_publication(&pub, &doc, &len), 0);
+ ASSERT_EQ(tree_parse(doc, len, &back), 0);
+ ASSERT_EQ(back.n_operations, 1);
+ ASSERT_EQ(tree_operation_digest(&back.operations[0], again), 0);
+ EXPECT_STREQ(again, op->operation_digest);
+ EXPECT_STREQ(back.operations[0].selector.labels[0].values[0], "a b");
+ xfree(doc, len);
+ tree_publication_free(&pub);
+ tree_publication_free(&back);
+ xfree(op, sizeof(*op));
+}
+
+static const struct unit_case cases[] = {
+ UNIT_CASE(operation_takes_no_control_character),
+ UNIT_CASE(publication_reads_back),
+ {}
+};
+
+UNIT_SUITE(bpm_tree, cases);
diff --git a/util/bpm_tree.c b/util/bpm_tree.c
index 56828109..4811023a 100644
--- a/util/bpm_tree.c
+++ b/util/bpm_tree.c
@@ -600,6 +600,52 @@ static int tree_selector_kv_compare(const void *left, const void *right)
* the publisher's order. Targets recompute operation digests from this
* form, so publisher and consumer share one emitter.
*/
+/*
+ * tp_string() refuses a control character, however it is escaped, so
+ * the emitter writes none: a publication would go out that no node can
+ * read, and every later sync of the tree would fail on it.
+ */
+static bool tree_text(const char *s)
+{
+ for (; *s; s++)
+ if ((__u8)*s < 0x20)
+ return false;
+ return true;
+}
+
+static bool tree_texts(const char (*v)[TREE_STRING_MAX], int n)
+{
+ for (int i = 0; i < n; i++)
+ if (!tree_text(v[i]))
+ return false;
+ return true;
+}
+
+static bool tree_kv_texts(const struct tree_selector_kv *kv, int n)
+{
+ for (int i = 0; i < n; i++)
+ if (!tree_text(kv[i].key) ||
+ !tree_texts(kv[i].values, kv[i].n_values))
+ return false;
+ return true;
+}
+
+static bool tree_operation_text(const struct tree_operation *op)
+{
+ const struct tree_selector *s = &op->selector;
+ const struct tree_artifact *a = &op->artifact;
+
+ return tree_text(op->op) && tree_text(op->id) &&
+ tree_texts(s->kernel_release_prefix,
+ s->n_kernel_release_prefix) &&
+ tree_texts(s->kernel_build_id, s->n_kernel_build_id) &&
+ tree_texts(s->architecture, s->n_architecture) &&
+ tree_kv_texts(s->kconfig, s->n_kconfig) &&
+ tree_kv_texts(s->labels, s->n_labels) &&
+ tree_text(a->repository) && tree_text(a->manifest_digest) &&
+ tree_text(a->layer_digest) && tree_text(a->payload_sha256);
+}
+
static int tree_emit_selector(struct json_emit *e,
const struct tree_selector *s)
{
@@ -678,6 +724,8 @@ static int tree_emit_operation(struct json_emit *e,
{
int ret;
+ if (!tree_operation_text(op))
+ return -EINVAL;
json_obj_open(e);
json_kv_int(e, "seq", op->seq);
json_kv_string(e, "op", op->op);