Skip to content

eBPF-based security platform

bpm: read a long kernel configuration line as one line

Daniel Borkmanne704d9b857bb
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index def4d113..b3d24fa6 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -491,11 +491,11 @@ signed config image.
Unit suites and fuzz targets
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-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
+The unit suites in :src:`testing/unit/` cover fifteen of the modules in
+:src:`util/`: ``bpm_oci``, ``bpm_package``, ``bpm_tree``,
+``bpm_tree_state``, ``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.
@@ -629,8 +629,8 @@ Coverage by area
* - upatch tree
- ``t3011``, ``t5004``, ``t7005``, ``t7101``, ``t7102``,
``t7103``, ``t7104``, ``t9105``. Fuzz: ``tree``, ``tree-state``,
- ``kconfig``. Unit: ``bpm_tree``, ``kernel_build_id``,
- ``kernel_release``
+ ``kconfig``. Unit: ``bpm_tree``, ``bpm_tree_state``,
+ ``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 7e4b98ed..ba9a9e40 100644
--- a/Documentation/upatches/publish/beefile.rst
+++ b/Documentation/upatches/publish/beefile.rst
@@ -305,7 +305,8 @@ to 16 accepted values, and each key may appear once.
such as ``CONFIG_IPV6 = ["y", "m"]``. An option that is not set matches
``"n"``. The target reads ``/proc/config.gz`` and, when that is
missing or names none of the options, ``/boot/config-<release>``. A
- target with neither does not match.
+ target with neither does not match. A line of 1024 bytes or more is
+ skipped whole, so its option reads as not set.
``[upatches.tree.selector.labels]``
Up to 64 keys, each a node label, once, with its accepted values, such
as ``env = ["prod", "canary"]``. The target reads its labels from the
diff --git a/testing/unit/bpm_tree_state.c b/testing/unit/bpm_tree_state.c
new file mode 100644
index 00000000..851c6221
--- /dev/null
+++ b/testing/unit/bpm_tree_state.c
@@ -0,0 +1,77 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/* Copyright (c) 2026 Authors of shirudo */
+
+#include "../../util/bpm_tree_state.c"
+
+#include "unit.h"
+
+static char path[4096];
+
+static void write_config(const char *text, bool gz)
+{
+ const char *tmp = getenv("TMPDIR");
+ int fd;
+
+ ASSERT_TRUE(strsprintf(path, sizeof(path), "%s/shirudo-unit-kconfig-XXXXXX",
+ tmp && tmp[0] ? tmp : "/tmp") > 0);
+ fd = mkstemp(path);
+ ASSERT_TRUE(fd >= 0);
+ if (gz) {
+ gzFile f = gzdopen(fd, "wb");
+
+ ASSERT_TRUE(f);
+ ASSERT_EQ(gzwrite(f, text, (__u32)strlen(text)),
+ (int)strlen(text));
+ ASSERT_EQ(gzclose(f), Z_OK);
+ } else {
+ ASSERT_EQ(write_complete(fd, text, strlen(text)), 0);
+ ASSERT_EQ(close(fd), 0);
+ }
+}
+
+static const char *modules_in(const char *text, bool gz)
+{
+ static char values[TREE_MAX_KCONFIG][TREE_STRING_MAX];
+ static int slots[TREE_SELECTOR_HASH_SIZE];
+ static struct tree_selector sel;
+ bool found[TREE_MAX_KCONFIG] = {};
+ 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;
+ selector_hash_build(&sel, slots);
+ write_config(text, gz);
+ ret = read_selector_config_file(path, &sel, slots, values, found, gz);
+ unlink(path);
+ ASSERT_EQ(ret, 0);
+ return found[0] ? values[0] : "-";
+}
+
+/*
+ * A line longer than the read buffer came in parts, and the part from
+ * the middle of a long string read as an option of its own.
+ */
+static void long_line_is_one_line(void)
+{
+ static const char head[] =
+ "# CONFIG_MODULES is not set\nCONFIG_CMDLINE=\"";
+ static const char tail[] = "CONFIG_MODULES=y\"\nCONFIG_X=y\n";
+ char text[4096];
+
+ memcpy(text, head, sizeof(head) - 1);
+ memset(text + sizeof(head) - 1, ' ', 2014);
+ memcpy(text + sizeof(head) - 1 + 2014, tail, sizeof(tail));
+ EXPECT_STREQ(modules_in(text, false), "n");
+ EXPECT_STREQ(modules_in(text, true), "n");
+ EXPECT_STREQ(modules_in("CONFIG_MODULES=y\n", false), "y");
+ EXPECT_STREQ(modules_in("CONFIG_MODULES=m", true), "m");
+}
+
+static const struct unit_case cases[] = {
+ UNIT_CASE(long_line_is_one_line),
+ {}
+};
+
+UNIT_SUITE(bpm_tree_state, cases);
diff --git a/util/bpm_tree_state.c b/util/bpm_tree_state.c
index 7f39abc9..97e69c1b 100644
--- a/util/bpm_tree_state.c
+++ b/util/bpm_tree_state.c
@@ -310,16 +310,26 @@ static int read_selector_config_file(const char *path,
char values[][TREE_STRING_MAX],
bool found[], bool gz)
{
+ bool skip = false, whole;
char line[1024];
int ret = 0;
+ /*
+ * A line longer than the buffer comes in parts, and a part from the
+ * middle of a long string value would read as an option of its own:
+ * none of such a line is taken, and its option reads as not set.
+ */
if (gz) {
gzFile f = gzopen(path, "rb");
if (!f)
return -errno;
while (gzgets(f, line, sizeof(line))) {
- ret = selector_config_line(line, s, slots, values, found);
+ whole = strchr(line, '\n') || gzeof(f);
+ if (!skip && whole)
+ ret = selector_config_line(line, s, slots, values,
+ found);
+ skip = !whole;
if (ret)
break;
}
@@ -336,7 +346,11 @@ static int read_selector_config_file(const char *path,
if (!f)
return -errno;
while (fgets(line, sizeof(line), f)) {
- ret = selector_config_line(line, s, slots, values, found);
+ whole = strchr(line, '\n') || feof(f);
+ if (!skip && whole)
+ ret = selector_config_line(line, s, slots, values,
+ found);
+ skip = !whole;
if (ret)
break;
}