Skip to content

eBPF-based security platform

bpm: refuse a kernel config that holds a NUL

Daniel Borkmann4e6b5c5e17b2
diff --git a/Documentation/reference/formats/beefile.rst b/Documentation/reference/formats/beefile.rst
index 964aa333..89db212a 100644
--- a/Documentation/reference/formats/beefile.rst
+++ b/Documentation/reference/formats/beefile.rst
@@ -464,8 +464,9 @@ option that is not set matches ``"n"``. The target reads
``/proc/config.gz`` and, when that is missing, is not one whole gzip
stream or names none of the options, ``/boot/config-<release>``, each of
at most 4 MiB. A target with neither does not match, and one whose
-configuration cannot be read stops the sync at the operation. A line of
-1024 bytes or more is skipped whole, so its option reads as not set.
+configuration cannot be read, or holds a NUL byte, stops the sync at the
+operation. A line of 1024 bytes or more is skipped whole, so its option
+reads as not set.
.. code-block:: toml
diff --git a/testing/fuzz/fuzz-kconfig.c b/testing/fuzz/fuzz-kconfig.c
index 2d4663fa..03670b94 100644
--- a/testing/fuzz/fuzz-kconfig.c
+++ b/testing/fuzz/fuzz-kconfig.c
@@ -2,8 +2,9 @@
/*
* Copyright (c) 2026 Authors of shirudo
*
- * A kernel config, plain or gzip'd, as an upatch tree selector reads it,
- * and node.labels.
+ * A kernel config, plain or gzip'd, as a upatch tree selector reads it,
+ * and node.labels. A plain config that holds a NUL is refused: a line
+ * read as a string would end there.
*/
#include "fuzz.h"
@@ -42,7 +43,9 @@ int LLVMFuzzerTestOneInput(const __u8 *data, size_t size)
fd = fuzz_file(data, size, path, sizeof(path));
if (fd < 0)
return 0;
- read_selector_config_file(path, &sel, slots, values, found, false);
+ if (!read_selector_config_file(path, &sel, slots, values, found,
+ false) && memchr(data, '\0', size))
+ abort();
read_selector_config_file(path, &sel, slots, values, found, true);
read_config_file(path, labels, &n, ARRAY_SIZE(labels));
close(fd);
diff --git a/testing/unit/bpm_tree_state.c b/testing/unit/bpm_tree_state.c
index a117fce0..1b8a92dc 100644
--- a/testing/unit/bpm_tree_state.c
+++ b/testing/unit/bpm_tree_state.c
@@ -23,7 +23,7 @@ static char events_path[4300], pending_path[4300];
static char path[4096];
-static void write_config(const char *text, bool gz)
+static void write_config_bytes(const char *text, size_t len, bool gz)
{
const char *tmp = getenv("TMPDIR");
int fd;
@@ -36,16 +36,22 @@ static void write_config(const char *text, bool gz)
gzFile f = gzdopen(fd, "wb");
ASSERT_TRUE(f);
- ASSERT_EQ(gzwrite(f, text, (__u32)strlen(text)),
- (int)strlen(text));
+ ASSERT_EQ(gzwrite(f, text, (__u32)len), (int)len);
ASSERT_EQ(gzclose(f), Z_OK);
} else {
- ASSERT_EQ(write_complete(fd, text, strlen(text)), 0);
+ ASSERT_EQ(write_complete(fd, text, len), 0);
ASSERT_EQ(close(fd), 0);
}
}
-static const char *modules_in(const char *text, bool gz)
+static void write_config(const char *text, bool gz)
+{
+ write_config_bytes(text, strlen(text), gz);
+}
+
+/* What a config of len bytes reads as, with CONFIG_MODULES in *value. */
+static int modules_read(const char *text, size_t len, bool gz,
+ const char **value)
{
static char values[TREE_MAX_KCONFIG][TREE_STRING_MAX];
static int slots[TREE_SELECTOR_HASH_SIZE];
@@ -60,12 +66,20 @@ static const char *modules_in(const char *text, bool gz)
TREE_MAX_KCONFIG, &kv), 0);
strscpy(kv->key, "CONFIG_MODULES", sizeof(kv->key));
selector_hash_build(&sel, slots);
- write_config(text, gz);
+ write_config_bytes(text, len, 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] : "-";
+ *value = found[0] ? values[0] : "-";
+ return ret;
+}
+
+static const char *modules_in(const char *text, bool gz)
+{
+ const char *value;
+
+ ASSERT_EQ(modules_read(text, strlen(text), gz, &value), 0);
+ return value;
}
/*
@@ -88,6 +102,22 @@ static void long_line_is_one_line(void)
EXPECT_STREQ(modules_in("CONFIG_MODULES=m", true), "m");
}
+/*
+ * A line is read as a string, which a NUL ends: the option of a line
+ * that held one read as set to what stood ahead of it.
+ */
+static void config_with_a_nul_is_no_config(void)
+{
+ static const char text[] = "CONFIG_MODULES=y\0es\nCONFIG_X=y\n";
+ const char *value;
+
+ EXPECT_EQ(modules_read(text, sizeof(text) - 1, false, &value),
+ -EINVAL);
+ EXPECT_EQ(modules_read(text, sizeof(text) - 1, true, &value), -EINVAL);
+ ASSERT_EQ(modules_read(text, strlen(text), false, &value), 0);
+ EXPECT_STREQ(value, "y");
+}
+
/* A config written gzip'd or not, cut by cut bytes and extra appended. */
static int read_gz(bool gz, off_t cut, const char *extra)
{
@@ -557,6 +587,7 @@ static void eventlog_matches_only_a_state_read(void)
static const struct unit_case cases[] = {
UNIT_CASE(long_line_is_one_line),
+ UNIT_CASE(config_with_a_nul_is_no_config),
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.c
index e3fdc884..3591cf8d 100644
--- a/util/bpm_tree_state.c
+++ b/util/bpm_tree_state.c
@@ -317,7 +317,9 @@ static int gunzip(char *in, size_t len, char **out, size_t *out_len,
/*
* A line of sizeof(line) bytes or more with its newline is skipped whole,
* as a part from the middle of a long string value would read as an
- * option of its own: its option reads as not set.
+ * option of its own: its option reads as not set. A text with a NUL is
+ * no config: the line that holds it would read as what stands ahead of
+ * the NUL.
*/
static int selector_config_text(const char *text, size_t len,
const struct tree_selector *s,
@@ -332,11 +334,12 @@ static int selector_config_text(const char *text, size_t len,
const char *nl = memchr(text + off, '\n', len - off);
n = nl ? (size_t)(nl - text) - off : len - off;
- if (n >= sizeof(line) - 1)
+ ret = span_to_str(line, sizeof(line) - 1, text + off, n);
+ if (ret == -E2BIG)
continue;
- memcpy(line, text + off, n);
- line[n] = '\0';
- ret = selector_config_line(line, s, slots, values, found);
+ if (!ret)
+ ret = selector_config_line(line, s, slots, values,
+ found);
if (ret)
return ret;
}