eBPF-based security platform
bpm: parse upatch-tree.conf lines beside the registry sources'
Daniel Borkmann835dde3c6190
diff --git a/builtin-bpm.c b/builtin-bpm.cindex c8608341..db74eac4 100644--- a/builtin-bpm.c+++ b/builtin-bpm.c@@ -1483,28 +1483,13 @@ static int load_tree_config(struct bpm_tree_config *cfg)return -EPERM;}while (fgets(line, sizeof(line), fp)) {- char key[64], value[PATH_MAX];- char *eq = strchr(line, '=');-- if (!eq)- continue;- *eq++ = '\0';- bpm_trim(line, key, sizeof(key));- bpm_trim(eq, value, sizeof(value));- if (value[0] == '"') {- size_t len = strlen(value);-- if (len < 2 || value[len - 1] != '"') {- fclose(fp);- return -EINVAL;- }- memmove(value, value + 1, len - 2);- value[len - 2] = '\0';+ if (shirudo_bpm_parse_tree_line(line, cfg->endpoint,+ sizeof(cfg->endpoint),+ cfg->reference,+ sizeof(cfg->reference))) {+ fclose(fp);+ return -EINVAL;}- if (!strcmp(key, "endpoint"))- bpm_copy_string(cfg->endpoint, sizeof(cfg->endpoint), value);- else if (!strcmp(key, "reference"))- bpm_copy_string(cfg->reference, sizeof(cfg->reference), value);}fclose(fp);if (!cfg->endpoint[0] || !cfg->reference[0])diff --git a/testing/helper/test-bpm-source.c b/testing/helper/test-bpm-source.cindex b4a8eafe..aee78dfc 100644--- a/testing/helper/test-bpm-source.c+++ b/testing/helper/test-bpm-source.c@@ -2,9 +2,11 @@/** Copyright (c) 2026 Authors of shirudo*- * Focused tests for target-local BPM repository source parsing.+ * Focused tests for target-local BPM source and upatch tree configuration+ * parsing.*/+#include <errno.h>#include <stdio.h>#include <string.h>@@ -12,10 +14,14 @@int main(void){- char endpoint[512] = {};- char key_file[256] = {};- char endpoint_line[] = "endpoint = oci://registry.test:5443\n";+ char tree_endpoint_line[] = " endpoint=\"oci://registry.test/tree\"\n";char key_file_line[] = "key_file = /etc/shirudo/bpm/keys/jbrust.pem\n";+ char endpoint_line[] = "endpoint = oci://registry.test:5443\n";+ char unbalanced_line[] = "reference = \"sha256:feed\n";+ char reference_line[] = "reference = sha256:feed\r\n";+ char key_file[256] = {};+ char reference[80] = {};+ char endpoint[512] = {};if (shirudo_bpm_parse_source_line(endpoint_line, endpoint,sizeof(endpoint), key_file,@@ -29,5 +35,17 @@ int main(void)return 1;printf("endpoint=%s\nkey_file=%s\n", endpoint, key_file);++ if (shirudo_bpm_parse_tree_line(tree_endpoint_line, endpoint,+ sizeof(endpoint), reference,+ sizeof(reference)) ||+ shirudo_bpm_parse_tree_line(reference_line, endpoint,+ sizeof(endpoint), reference,+ sizeof(reference)) ||+ shirudo_bpm_parse_tree_line(unbalanced_line, endpoint,+ sizeof(endpoint), reference,+ sizeof(reference)) != -EINVAL)+ return 1;+ printf("tree endpoint=%s\ntree reference=%s\n", endpoint, reference);return 0;}diff --git a/testing/t0020-bpm-cli.sh b/testing/t0020-bpm-cli.shindex ca00a542..c432f458 100644--- a/testing/t0020-bpm-cli.sh+++ b/testing/t0020-bpm-cli.sh@@ -67,10 +67,12 @@ test_expect_success 'bpm uninstall and list validate their arguments' '$SHIRUDO bpm uninstall --help 2>&1 | grep -q "usage: shirudo bpm uninstall <package>"'-test_expect_success 'bpm parses spaced repository source assignments' '+test_expect_success 'bpm parses repository source and tree configuration lines' 'out=$($BPM_SOURCE_HELPER) &&echo "$out" | grep -qx "endpoint=oci://registry.test:5443" &&- echo "$out" | grep -qx "key_file=/etc/shirudo/bpm/keys/jbrust.pem"+ echo "$out" | grep -qx "key_file=/etc/shirudo/bpm/keys/jbrust.pem" &&+ echo "$out" | grep -qx "tree endpoint=oci://registry.test/tree" &&+ echo "$out" | grep -qx "tree reference=sha256:feed"'test_donediff --git a/util/bpm_source_config.c b/util/bpm_source_config.cindex 5eeeff19..0358fccc 100644--- a/util/bpm_source_config.c+++ b/util/bpm_source_config.c@@ -43,3 +43,43 @@ int shirudo_bpm_parse_source_line(char *line,snprintf(key_file, key_file_sz, "%s", val);return 0;}++static char *trim(char *s)+{+ char *end;++ while (isspace((__u8)*s))+ s++;+ end = s + strlen(s);+ while (end > s && isspace((__u8)end[-1]))+ end--;+ *end = '\0';+ return s;+}++int shirudo_bpm_parse_tree_line(char *line,+ char *endpoint, size_t endpoint_sz,+ char *reference, size_t reference_sz)+{+ char *eq = strchr(line, '=');+ char *key, *value;+ size_t len;++ if (!eq)+ return 0;+ *eq++ = '\0';+ key = trim(line);+ value = trim(eq);+ len = strlen(value);+ if (value[0] == '"') {+ if (len < 2 || value[len - 1] != '"')+ return -EINVAL;+ value[len - 1] = '\0';+ value++;+ }+ if (!strcmp(key, "endpoint"))+ snprintf(endpoint, endpoint_sz, "%s", value);+ else if (!strcmp(key, "reference"))+ snprintf(reference, reference_sz, "%s", value);+ return 0;+}diff --git a/util/bpm_source_config.h b/util/bpm_source_config.hindex 93564176..52fc0c64 100644--- a/util/bpm_source_config.h+++ b/util/bpm_source_config.h@@ -11,5 +11,9 @@int shirudo_bpm_parse_source_line(char *line,char *endpoint, size_t endpoint_sz,char *key_file, size_t key_file_sz);+/* A line of upatch-tree.conf: endpoint and reference, either quoted. */+int shirudo_bpm_parse_tree_line(char *line,+ char *endpoint, size_t endpoint_sz,+ char *reference, size_t reference_sz);#endif /* SHIRUDO_BPM_SOURCE_CONFIG_H */