eBPF-based security platform
bpm: drop a gzip branch and a trim loop the label reader never runs
Daniel Borkmannb4f141164fad
diff --git a/testing/fuzz/fuzz-kconfig.c b/testing/fuzz/fuzz-kconfig.cindex e2c367b2..2d4663fa 100644--- a/testing/fuzz/fuzz-kconfig.c+++ b/testing/fuzz/fuzz-kconfig.c@@ -44,7 +44,7 @@ int LLVMFuzzerTestOneInput(const __u8 *data, size_t size)return 0;read_selector_config_file(path, &sel, slots, values, found, false);read_selector_config_file(path, &sel, slots, values, found, true);- read_config_file(path, labels, &n, ARRAY_SIZE(labels), false);+ read_config_file(path, labels, &n, ARRAY_SIZE(labels));close(fd);return 0;}diff --git a/testing/proofs/cbmc/bpm-tree-state-pairs.c b/testing/proofs/cbmc/bpm-tree-state-pairs.cindex 39fd778b..f1a54dd2 100644--- a/testing/proofs/cbmc/bpm-tree-state-pairs.c+++ b/testing/proofs/cbmc/bpm-tree-state-pairs.c@@ -49,9 +49,9 @@ static char *string_of(size_t len)}/*- * read_config_stream() and the gzip branch of read_config_file() add to- * the max pairs of labels[] from a count that starts at 0 and that only- * kv_add() moves, so n is at most max and max the pairs there are.+ * read_config_stream() adds to the max pairs of labels[] from a count+ * that starts at 0 and that only kv_add() moves, so n is at most max and+ * max the pairs there are.*/static void pair_added(size_t key_len, size_t value_len, int n){diff --git a/testing/unit/bpm_tree_state.c b/testing/unit/bpm_tree_state.cindex e219ff47..30dd068c 100644--- a/testing/unit/bpm_tree_state.c+++ b/testing/unit/bpm_tree_state.c@@ -142,10 +142,33 @@ static void pending_is_an_operation_in_flight(void)"\"operation_digest\":\"sha256:00\"}", &p), -EINVAL);}+/* As the format has it: blanks go but for those between a key and its =. */+static void labels_are_cut_at_the_first_equals(void)+{+ static struct tree_kv labels[4];+ int n = -1;++ write_config("# site=x\n site = a \t\nzone=b=c\njunk\nempty=\t\n",+ false);+ ASSERT_EQ(read_config_file(path, labels, &n, ARRAY_SIZE(labels)), 0);+ unlink(path);+ ASSERT_EQ(n, 3);+ EXPECT_STREQ(labels[0].key, "site ");+ EXPECT_STREQ(labels[0].value, "a");+ EXPECT_STREQ(labels[1].key, "zone");+ EXPECT_STREQ(labels[1].value, "b=c");+ EXPECT_STREQ(labels[2].key, "empty");+ EXPECT_STREQ(labels[2].value, "");+ EXPECT_EQ(read_config_file("/nonexistent/node.labels", labels, &n,+ ARRAY_SIZE(labels)), -ENOENT);+ EXPECT_EQ(n, 0);+}+static const struct unit_case cases[] = {UNIT_CASE(long_line_is_one_line),UNIT_CASE(gzip_config_is_one_whole_member),UNIT_CASE(pending_is_an_operation_in_flight),+ UNIT_CASE(labels_are_cut_at_the_first_equals),{}};diff --git a/util/bpm_tree_state.c b/util/bpm_tree_state.cindex aec7dde9..b490de11 100644--- a/util/bpm_tree_state.c+++ b/util/bpm_tree_state.c@@ -81,62 +81,6 @@ bool tree_configured(void)return true;}-static int read_config_stream(FILE *f, struct tree_kv *out, int *n,- int max);--static int read_config_file(const char *path, struct tree_kv *out,- int *n, int max, bool gz)-{- char line[512];- int ret = 0;-- *n = 0;- if (gz) {- gzFile f = gzopen(path, "rb");-- if (!f)- return -errno;- while (gzgets(f, line, sizeof(line))) {- char *eq, *key, *value;-- eq = strchr(line, '=');- if (line[0] == '#' && !strncmp(line, "# CONFIG_", 9)) {- key = line + 2;- eq = strstr(key, " is not set");- if (eq) {- *eq = '\0';- ret = kv_add(out, n, max, key, "n");- }- } else if (eq) {- *eq++ = '\0';- key = line;- value = eq;- value[strcspn(value, "\r\n")] = '\0';- if (value[0] == '"') {- size_t l = strlen(value);-- if (l > 1 && value[l - 1] == '"') {- value[l - 1] = '\0';- value++;- }- }- ret = kv_add(out, n, max, key, value);- }- if (ret)- break;- }- gzclose(f);- return ret;- }- {- FILE *f = fopen(path, "re");-- if (!f)- return -errno;- return read_config_stream(f, out, n, max);- }-}-/* The stream is consumed and closed. */static int read_config_stream(FILE *f, struct tree_kv *out, int *n,int max)@@ -165,9 +109,6 @@ static int read_config_stream(FILE *f, struct tree_kv *out, int *n,if (!eq)continue;*eq++ = '\0';- end = eq + strlen(eq);- while (end > eq && isspace((__u8)end[-1]))- *--end = '\0';while (*eq && isspace((__u8)*eq))eq++;ret = kv_add(out, n, max, start, eq);@@ -179,6 +120,17 @@ static int read_config_stream(FILE *f, struct tree_kv *out, int *n,return ret;}+static int read_config_file(const char *path, struct tree_kv *out,+ int *n, int max)+{+ FILE *f = fopen(path, "re");++ *n = 0;+ if (!f)+ return -errno;+ return read_config_stream(f, out, n, max);+}+static int read_config_fd(int fd, struct tree_kv *out, int *n,int max){@@ -540,8 +492,7 @@ int tree_selector_matches(const struct tree_selector *s)ret = read_config_fd(fd, labels, &nl, ARRAY_SIZE(labels));} else {ret = read_config_file("/etc/shirudo/node.labels",- labels, &nl, ARRAY_SIZE(labels),- false);+ labels, &nl, ARRAY_SIZE(labels));}if (ret == -ENOENT)return 0;