Skip to content

eBPF-based security platform

bpm: size tree selector pairs to what a selector names

Daniel Borkmann73006660d395
diff --git a/Makefile.shirudo b/Makefile.shirudo
index 86cf00bd..e79d2639 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -1058,7 +1058,7 @@ testing/fuzz/fuzz-registry-auth: testing/fuzz/fuzz-registry-auth.c \
testing/fuzz/fuzz-config: testing/fuzz/fuzz-config.c $(FUZZ_BASE) \
$(FUZZ_CONFIG) util/seal.c util/io.c \
util/bpm_source_config.c
-FUZZ_TREE_STATE := util/bpm_tree_state.c util/io.c $(FUZZ_SEAL) \
+FUZZ_TREE_STATE := util/bpm_tree_state.c util/bpm_tree.c util/io.c $(FUZZ_SEAL) \
util/kernel_build_id.c util/kernel_release.c \
util/crypto_openssl.c testing/fuzz/chain.c
testing/fuzz/fuzz-tree-state: testing/fuzz/fuzz-tree-state.c $(FUZZ_BASE) \
diff --git a/testing/fuzz/fuzz-kconfig.c b/testing/fuzz/fuzz-kconfig.c
index 72597947..e2c367b2 100644
--- a/testing/fuzz/fuzz-kconfig.c
+++ b/testing/fuzz/fuzz-kconfig.c
@@ -19,9 +19,14 @@ int LLVMFuzzerInitialize(int *argc, char ***argv)
"CONFIG_LSM", "CONFIG_MODULES",
};
- for (size_t i = 0; i < ARRAY_SIZE(keys); i++)
- strscpy(sel.kconfig[i].key, keys[i], sizeof(sel.kconfig[i].key));
- sel.n_kconfig = ARRAY_SIZE(keys);
+ for (size_t i = 0; i < ARRAY_SIZE(keys); i++) {
+ struct tree_selector_kv *kv;
+
+ if (tree_selector_kv_add(&sel.kconfig, &sel.n_kconfig,
+ TREE_MAX_KCONFIG, &kv))
+ abort();
+ strscpy(kv->key, keys[i], sizeof(kv->key));
+ }
selector_hash_build(&sel, slots);
return 0;
}
diff --git a/testing/unit/bpm_tree.c b/testing/unit/bpm_tree.c
index 556f52d0..9ba92dd7 100644
--- a/testing/unit/bpm_tree.c
+++ b/testing/unit/bpm_tree.c
@@ -33,8 +33,9 @@ static struct tree_operation *operation(void)
DIGEST);
set(op->artifact.payload_sha256, sizeof(op->artifact.payload_sha256),
DIGEST);
- op->selector.n_labels = 1;
- label = &op->selector.labels[0];
+ ASSERT_EQ(tree_selector_kv_add(&op->selector.labels,
+ &op->selector.n_labels, TREE_MAX_LABELS,
+ &label), 0);
set(label->key, sizeof(label->key), "site");
label->n_values = 1;
set(label->values[0], sizeof(label->values[0]), "a b");
@@ -60,6 +61,7 @@ static void operation_takes_no_control_character(void)
label->key[0] = 's';
op->artifact.repository[0] = '\r';
EXPECT_EQ(tree_operation_digest(op, digest), -EINVAL);
+ tree_selector_free(&op->selector);
xfree(op, sizeof(*op));
}
@@ -85,6 +87,7 @@ static void publication_reads_back(void)
xfree(doc, len);
tree_publication_free(&pub);
tree_publication_free(&back);
+ tree_selector_free(&op->selector);
xfree(op, sizeof(*op));
}
diff --git a/testing/unit/bpm_tree_state.c b/testing/unit/bpm_tree_state.c
index 851c6221..faecebf9 100644
--- a/testing/unit/bpm_tree_state.c
+++ b/testing/unit/bpm_tree_state.c
@@ -35,15 +35,18 @@ static const char *modules_in(const char *text, bool gz)
static int slots[TREE_SELECTOR_HASH_SIZE];
static struct tree_selector sel;
bool found[TREE_MAX_KCONFIG] = {};
+ struct tree_selector_kv *kv;
int ret;
memset(&sel, 0, sizeof(sel));
memset(values, 0, sizeof(values));
- strscpy(sel.kconfig[0].key, "CONFIG_MODULES", sizeof(sel.kconfig[0].key));
- sel.n_kconfig = 1;
+ ASSERT_EQ(tree_selector_kv_add(&sel.kconfig, &sel.n_kconfig,
+ TREE_MAX_KCONFIG, &kv), 0);
+ strscpy(kv->key, "CONFIG_MODULES", sizeof(kv->key));
selector_hash_build(&sel, slots);
write_config(text, gz);
ret = read_selector_config_file(path, &sel, slots, values, found, gz);
+ tree_selector_free(&sel);
unlink(path);
ASSERT_EQ(ret, 0);
return found[0] ? values[0] : "-";
diff --git a/tools/shirudo-build/bpm-build.c b/tools/shirudo-build/bpm-build.c
index 710e8d4b..36ece70c 100644
--- a/tools/shirudo-build/bpm-build.c
+++ b/tools/shirudo-build/bpm-build.c
@@ -267,11 +267,12 @@ static int build_parse_string_array(const char *value,
}
static int build_parse_selector_kv(const char *key, const char *value,
- struct tree_selector_kv *kvs,
+ struct tree_selector_kv **kvs,
int max, int *n)
{
struct tree_selector_kv *kv;
size_t key_len = strlen(key);
+ int ret;
if (key_len >= 2 && (key[0] == '"' || key[0] == '\'') &&
key[key_len - 1] == key[0]) {
@@ -281,20 +282,19 @@ static int build_parse_selector_kv(const char *key, const char *value,
if (!key_len || key_len >= TREE_STRING_MAX)
return -EINVAL;
for (int i = 0; i < *n; i++) {
- if (strlen(kvs[i].key) == key_len &&
- !strncmp(kvs[i].key, key, key_len))
+ if (strlen((*kvs)[i].key) == key_len &&
+ !strncmp((*kvs)[i].key, key, key_len))
return -EEXIST;
}
- if (*n >= max)
- return -E2BIG;
- kv = &kvs[*n];
+ ret = tree_selector_kv_add(kvs, n, max, &kv);
+ if (ret)
+ return ret;
memcpy(kv->key, key, key_len);
kv->key[key_len] = '\0';
if (build_parse_string_array(value, kv->values,
TREE_MAX_SELECTOR_VALUES,
&kv->n_values))
return -EINVAL;
- (*n)++;
return 0;
}
@@ -625,12 +625,12 @@ static int build_parse_spec(struct bpm_build_spec *spec, char *body)
TREE_MAX_SELECTOR_VALUES, n))
return -EINVAL;
} else if (tree_section && !strcmp(section, "upatches.tree.selector.kconfig")) {
- if (build_parse_selector_kv(key, value, sel->kconfig,
+ if (build_parse_selector_kv(key, value, &sel->kconfig,
TREE_MAX_KCONFIG,
&sel->n_kconfig))
return -EINVAL;
} else if (tree_section) {
- if (build_parse_selector_kv(key, value, sel->labels,
+ if (build_parse_selector_kv(key, value, &sel->labels,
TREE_MAX_LABELS,
&sel->n_labels))
return -EINVAL;
@@ -714,6 +714,7 @@ void bpm_build_spec_free(struct bpm_build_spec *spec)
if (!spec)
return;
for (size_t i = 0; i < spec->n_upatches; i++) {
+ tree_selector_free(spec->upatches[i].tree.selector);
xfree(spec->upatches[i].tree.selector,
sizeof(*spec->upatches[i].tree.selector));
xfree(spec->upatches[i].rodata,
diff --git a/tools/shirudo-build/bpm-publish.c b/tools/shirudo-build/bpm-publish.c
index e8cb294d..b69891d9 100644
--- a/tools/shirudo-build/bpm-publish.c
+++ b/tools/shirudo-build/bpm-publish.c
@@ -820,11 +820,17 @@ static int cmd_bpm_tree_append(int argc, char **argv)
pr_err("bpm tree append: package entry %s not found", member);
goto out;
}
+ tree_selector_free(&op->selector);
*op = (struct tree_operation){ .seq = seq };
bpm_copy_string(op->op, sizeof(op->op), operation);
bpm_copy_string(op->id, sizeof(op->id), u->tree.id);
- if (u->tree.selector)
- op->selector = *u->tree.selector;
+ if (u->tree.selector) {
+ ret = tree_selector_copy(&op->selector, u->tree.selector);
+ if (ret) {
+ xfree(body, body_len + 1);
+ goto out;
+ }
+ }
tree_selector_canonicalize(&op->selector);
ret = tree_push_artifact(&c, repository, repo_ref, body, body_len,
u->tree.id, unload, &op->artifact);
@@ -891,6 +897,8 @@ out:
xfree(manifest, BPM_MANIFEST_CAP);
sig_free(sig);
xfree(payload, payload_len);
+ if (op)
+ tree_selector_free(&op->selector);
xfree(op, sizeof(*op));
tree_publication_free(&pub);
bpm_build_spec_free(spec);
diff --git a/util/bpm_tree.c b/util/bpm_tree.c
index f3862dac..9db21ce9 100644
--- a/util/bpm_tree.c
+++ b/util/bpm_tree.c
@@ -223,7 +223,7 @@ static int tp_string_array(struct tree_parser *p,
}
static int tp_selector_string_map(struct tree_parser *p,
- struct tree_selector_kv *dst,
+ struct tree_selector_kv **dst,
int *n, int max)
{
char key[TREE_STRING_MAX];
@@ -232,20 +232,19 @@ static int tp_selector_string_map(struct tree_parser *p,
if (tp_char(p, '{'))
return -EINVAL;
for (;;) {
- int values = 0;
+ struct tree_selector_kv *kv;
tp_ws(p);
if (p->p < p->end && *p->p == '}') {
p->p++;
return 0;
}
- if (tp_key(p, key, sizeof(key), &first) || *n >= max ||
- tp_string_array(p, dst[*n].values, &values,
+ if (tp_key(p, key, sizeof(key), &first) ||
+ tree_selector_kv_add(dst, n, max, &kv) ||
+ tp_string_array(p, kv->values, &kv->n_values,
TREE_MAX_SELECTOR_VALUES))
return -EINVAL;
- strscpy(dst[*n].key, key, sizeof(dst[*n].key));
- dst[*n].n_values = values;
- (*n)++;
+ strscpy(kv->key, key, sizeof(kv->key));
}
}
@@ -281,11 +280,11 @@ static int tp_selector(struct tree_parser *p, struct tree_selector *s)
return -EINVAL;
} else if (!strcmp(key, "kconfig") ||
!strcmp(key, "kernel_config")) {
- if (tp_selector_string_map(p, s->kconfig, &s->n_kconfig,
+ if (tp_selector_string_map(p, &s->kconfig, &s->n_kconfig,
TREE_MAX_KCONFIG))
return -EINVAL;
} else if (!strcmp(key, "labels")) {
- if (tp_selector_string_map(p, s->labels, &s->n_labels,
+ if (tp_selector_string_map(p, &s->labels, &s->n_labels,
TREE_MAX_LABELS))
return -EINVAL;
} else if (tp_skip(p)) {
@@ -375,10 +374,66 @@ static int tp_operation(struct tree_parser *p, struct tree_operation *o)
}
}
+int tree_selector_kv_add(struct tree_selector_kv **kvs, int *n, int max,
+ struct tree_selector_kv **kv)
+{
+ struct tree_selector_kv *grown;
+
+ if (*n >= max)
+ return -E2BIG;
+ grown = xreallocarray(*kvs, (size_t)*n, (size_t)*n + 1,
+ sizeof(**kvs));
+ if (!grown)
+ return -ENOMEM;
+ *kvs = grown;
+ *kv = &grown[(*n)++];
+ return 0;
+}
+
+static int tree_kvs_copy(struct tree_selector_kv **dst,
+ const struct tree_selector_kv *src, int n)
+{
+ *dst = NULL;
+ if (!n)
+ return 0;
+ *dst = xcalloc((size_t)n, sizeof(**dst));
+ if (!*dst)
+ return -ENOMEM;
+ memcpy(*dst, src, (size_t)n * sizeof(**dst));
+ return 0;
+}
+
+int tree_selector_copy(struct tree_selector *dst,
+ const struct tree_selector *src)
+{
+ *dst = *src;
+ if (tree_kvs_copy(&dst->kconfig, src->kconfig, src->n_kconfig) ||
+ tree_kvs_copy(&dst->labels, src->labels, src->n_labels)) {
+ tree_selector_free(dst);
+ return -ENOMEM;
+ }
+ return 0;
+}
+
+void tree_selector_free(struct tree_selector *s)
+{
+ if (!s)
+ return;
+ xfree(s->kconfig, (size_t)s->n_kconfig * sizeof(*s->kconfig));
+ xfree(s->labels, (size_t)s->n_labels * sizeof(*s->labels));
+ s->kconfig = NULL;
+ s->labels = NULL;
+ s->n_kconfig = 0;
+ s->n_labels = 0;
+}
+
void tree_publication_free(struct tree_publication *pub)
{
if (!pub)
return;
+ /* A slot past n_operations may hold an operation that failed to parse. */
+ for (int i = 0; i < pub->operations_capacity; i++)
+ tree_selector_free(&pub->operations[i].selector);
xfree(pub->operations,
(size_t)pub->operations_capacity * sizeof(*pub->operations));
*pub = (struct tree_publication){};
@@ -415,7 +470,15 @@ int tree_publication_append(struct tree_publication *pub,
ret = tree_operations_reserve(pub);
if (ret)
return ret;
- pub->operations[pub->n_operations++] = *op;
+ pub->operations[pub->n_operations] = *op;
+ ret = tree_selector_copy(&pub->operations[pub->n_operations].selector,
+ &op->selector);
+ if (ret) {
+ memset(&pub->operations[pub->n_operations], 0,
+ sizeof(pub->operations[pub->n_operations]));
+ return ret;
+ }
+ pub->n_operations++;
return 0;
}
diff --git a/util/bpm_tree.h b/util/bpm_tree.h
index 54eb1598..0ae233d6 100644
--- a/util/bpm_tree.h
+++ b/util/bpm_tree.h
@@ -36,12 +36,28 @@ struct tree_selector {
int n_kernel_build_id;
char architecture[TREE_MAX_SELECTOR_VALUES][TREE_STRING_MAX];
int n_architecture;
- struct tree_selector_kv kconfig[TREE_MAX_KCONFIG];
+ /*
+ * Owned and sized to what a selector names: at their full limits,
+ * every operation of a publication took 545 KiB for them.
+ */
+ struct tree_selector_kv *kconfig;
int n_kconfig;
- struct tree_selector_kv labels[TREE_MAX_LABELS];
+ struct tree_selector_kv *labels;
int n_labels;
};
+/*
+ * A new zeroed pair at the end of the n at *kvs, of at most max: 0 with
+ * it in *kv, -E2BIG or -ENOMEM. tree_selector_copy() fills dst, which
+ * holds no pairs, with copies of src's; tree_selector_free() releases
+ * the pairs, and a zeroed selector holds none.
+ */
+int tree_selector_kv_add(struct tree_selector_kv **kvs, int *n, int max,
+ struct tree_selector_kv **kv);
+int tree_selector_copy(struct tree_selector *dst,
+ const struct tree_selector *src);
+void tree_selector_free(struct tree_selector *s);
+
struct tree_artifact {
char repository[512];
char manifest_digest[128];
@@ -83,6 +99,7 @@ struct tree_state {
int tree_parse(const char *body, size_t len,
struct tree_publication *out);
+/* Appends a copy of op, its selector included. */
int tree_publication_append(struct tree_publication *pub,
const struct tree_operation *op);
void tree_publication_free(struct tree_publication *pub);