Skip to content

eBPF-based security platform

util: read a line of a kernel configuration in one place

Daniel Borkmann82fba35f421c
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rst
index 89df336e..9ddea36c 100644
--- a/Documentation/contribute/testing/proof-coverage.rst
+++ b/Documentation/contribute/testing/proof-coverage.rst
@@ -1251,6 +1251,14 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.
within its room; a number only of its exact size; a device with
an index, a name, terminated strings, and the bus and device of a
devlink port
+ * - ``kconfig-line``
+ - ``kconfig_line()`` of :src:`util/kconfig.c`
+ - Lines of 0 to 2, 10 and 21 to 23 bytes of any value, which reach
+ an option set, one not set and every refusal
+ - Nothing read or written outside the line; a name that is
+ ``CONFIG_`` and more, in the line; a value in the line, or the
+ ``n`` of an option not set; a comment sets an option only as one
+ that is not set
Lean 4 proofs
-------------
@@ -1669,7 +1677,8 @@ schemas, and the members and elements of a JSON document longer than
the two bytes ``json-walk`` takes, the OTLP headers a configuration adds
and the collector's reply to an export, the walk over a package's tar
members, the registry references and URLs and the registry credentials
-file, the kernel configuration and the labels the tree selectors read,
+file, the lines a tree selector cuts a kernel configuration into and
+the labels it reads,
the tree's ``state.json`` and its outcomes record, the evidence document
beyond its PCRs and its config
history, the config update record, the site configuration file, the
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index 67e15365..c76dd549 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -932,12 +932,12 @@ signed config image.
Unit suites and fuzz targets
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-The unit suites in :src:`testing/unit/` cover 41 of the modules in
+The unit suites in :src:`testing/unit/` cover 42 of the modules in
:src:`util/`: ``attest``, ``bpm_oci``, ``bpm_package``, ``bpm_state``,
``bpm_tree``, ``bpm_tree_overlap``, ``bpm_tree_state``, ``chain``,
``chain_history``, ``conditions``, ``config_update``, ``crypto_openssl``,
``curl``, ``eventlog``, ``format``, ``hex``, ``identity``, ``io``,
-``java_agent``, ``json``, ``kernel_abi``,
+``java_agent``, ``json``, ``kconfig``, ``kernel_abi``,
``keepalive``, ``keepalive_quote``, ``kernel_build_id``,
``kernel_prereq``, ``kernel_release``, ``keys``, ``log``, ``measured``,
``options``,
diff --git a/Documentation/reference/formats/beefile.rst b/Documentation/reference/formats/beefile.rst
index 89db212a..3373243a 100644
--- a/Documentation/reference/formats/beefile.rst
+++ b/Documentation/reference/formats/beefile.rst
@@ -465,8 +465,10 @@ option that is not set matches ``"n"``. The target reads
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, 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.
+operation. An option is a line ``CONFIG_<x>=<value>``, a string without
+the double quotes around it, or ``# CONFIG_<x> is not set``; any other
+line is ignored, and so is one of more than 8191 bytes, whose option
+then has no value and matches nothing.
.. code-block:: toml
diff --git a/Makefile.shirudo b/Makefile.shirudo
index aa0981af..272cd9f2 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -264,6 +264,7 @@ UTIL_SRCS := \
util/curl.c \
util/bpm_package.c \
util/bpm_state.c \
+ util/kconfig.c \
util/kernel_build_id.c \
util/kernel_release.c \
util/config.c \
@@ -1285,7 +1286,8 @@ $(FUZZ_OUT)/fuzz-config-update: testing/fuzz/fuzz-config-update.c \
util/crypto_openssl.c util/io.c $(FUZZ_SEAL)
FUZZ_TREE_STATE := util/bpm_tree_state.c util/bpm_tree.c \
util/chain_history.c util/upatch.c $(FUZZ_OCI) \
- util/kernel_build_id.c util/kernel_release.c \
+ util/kconfig.c util/kernel_build_id.c \
+ util/kernel_release.c \
testing/fuzz/chain.c
$(FUZZ_OUT)/fuzz-tree-state: testing/fuzz/fuzz-tree-state.c $(FUZZ_BASE) \
$(FUZZ_TREE_STATE)
@@ -1323,7 +1325,8 @@ $(FUZZ_OUT)/fuzz-bpm-info: testing/fuzz/fuzz-bpm-info.c $(FUZZ_BASE) \
util/bpm_state.c util/bpm_tree_state.c \
util/chain.c util/eventlog.c util/help.c \
util/identity.c util/image_keys.c \
- util/initrd.c util/kernel_build_id.c \
+ util/initrd.c util/kconfig.c \
+ util/kernel_build_id.c \
util/kernel_release.c util/keyring.c \
util/nv_writer.c util/options.c util/paths.c \
util/similar.c util/timefmt.c util/tpm.c \
diff --git a/testing/proofs/cbmc/kconfig-line.c b/testing/proofs/cbmc/kconfig-line.c
new file mode 100644
index 00000000..36d381d4
--- /dev/null
+++ b/testing/proofs/cbmc/kconfig-line.c
@@ -0,0 +1,42 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * kconfig_line() of util/kconfig.c on every line of up to LEN bytes: it
+ * reads and writes nothing outside the line, a name it gives is CONFIG_
+ * and more and lies in the line, a value lies in the line or is the "n"
+ * of an option not set, and a comment sets an option only as one that
+ * is not set.
+ */
+
+#include "proof.h"
+#include "../../../util/string.c"
+#include "../../../util/kconfig.c"
+
+void harness(void)
+{
+ const char *key = NULL, *value = NULL;
+ char *line = any_bytes(LEN + 1);
+ bool comment = false;
+ size_t i;
+
+ __CPROVER_assume(!line[LEN]);
+ for (i = 0; i < LEN && isspace((__u8)line[i]); i++)
+ ;
+ comment = line[i] == '#';
+ if (!kconfig_line(line, &key, &value)) {
+ __CPROVER_assert(!key && !value, "no option, no name or value");
+ return;
+ }
+ __CPROVER_assert(__CPROVER_same_object(key, line) &&
+ !strncmp(key, "CONFIG_", strlen("CONFIG_")) &&
+ key[strlen("CONFIG_")], "a name after CONFIG_, in the line");
+ if (comment) {
+ __CPROVER_assert(!__CPROVER_same_object(value, line) &&
+ value[0] == 'n' && !value[1],
+ "a comment sets an option only as not set");
+ return;
+ }
+ __CPROVER_assert(__CPROVER_same_object(value, line),
+ "a value in the line");
+}
diff --git a/testing/proofs/cbmc/proofs.list b/testing/proofs/cbmc/proofs.list
index 1c07b6f2..c3171021 100644
--- a/testing/proofs/cbmc/proofs.list
+++ b/testing/proofs/cbmc/proofs.list
@@ -160,3 +160,4 @@ bpm-tree-state-options --unwind 4
keepalive-pack --unwind 9 --unwindset strcmp.0:18,keepalive_mask.0:514,mask.0:514,mask.1:514 LEN=0-9,512,513
sysinfo-image --unwind 8 --unwindset strcmp.0:8,memcmp.0:10,os_release_unquote.0:258,cmdline_image_root.0:8,cmdline_image_root.1:76,cmdline_image_root.2:5,strlen.0:11,all_hex.0:66,image_root_normalise.0:65,command_line.0:66,command_line.1:65 LEN=0-6
xdp-attrs --unwind 12 --unwindset memchr.0:129 LEN=0-12,16,20,24,28,32,40
+kconfig-line --unwind 26 LEN=0-2,10,21-23
diff --git a/testing/unit/kconfig.c b/testing/unit/kconfig.c
new file mode 100644
index 00000000..83c037a8
--- /dev/null
+++ b/testing/unit/kconfig.c
@@ -0,0 +1,84 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/* Copyright (c) 2026 Authors of shirudo */
+
+#include <shirudo/misc.h>
+#include <shirudo/string.h>
+
+#include "kconfig.h"
+#include "unit.h"
+
+static char line[256];
+static const char *key, *value;
+
+static bool sets(const char *text)
+{
+ key = "";
+ value = "";
+ ASSERT_TRUE(strscpy(line, text, sizeof(line)) >= 0);
+ return kconfig_line(line, &key, &value);
+}
+
+static void an_option_has_its_value(void)
+{
+ ASSERT_TRUE(sets("CONFIG_BPF_LSM=y"));
+ EXPECT_STREQ(key, "CONFIG_BPF_LSM");
+ EXPECT_STREQ(value, "y");
+ ASSERT_TRUE(sets("CONFIG_HZ=250"));
+ EXPECT_STREQ(value, "250");
+ ASSERT_TRUE(sets("CONFIG_INITRAMFS_SOURCE="));
+ EXPECT_STREQ(value, "");
+}
+
+static void a_string_comes_without_its_quotes(void)
+{
+ ASSERT_TRUE(sets("CONFIG_LSM=\"lockdown,bpf\""));
+ EXPECT_STREQ(value, "lockdown,bpf");
+ ASSERT_TRUE(sets("CONFIG_CMDLINE=\"console=ttyS0 quiet\""));
+ EXPECT_STREQ(key, "CONFIG_CMDLINE");
+ EXPECT_STREQ(value, "console=ttyS0 quiet");
+ ASSERT_TRUE(sets("CONFIG_LOCALVERSION=\"\""));
+ EXPECT_STREQ(value, "");
+ ASSERT_TRUE(sets("CONFIG_LOCALVERSION=\""));
+ EXPECT_STREQ(value, "\"");
+}
+
+static void an_option_not_set_is_n(void)
+{
+ ASSERT_TRUE(sets("# CONFIG_MODULES is not set"));
+ EXPECT_STREQ(key, "CONFIG_MODULES");
+ EXPECT_STREQ(value, "n");
+}
+
+static void white_space_around_is_no_part(void)
+{
+ ASSERT_TRUE(sets(" \tCONFIG_A = y \r"));
+ EXPECT_STREQ(key, "CONFIG_A");
+ EXPECT_STREQ(value, "y");
+ ASSERT_TRUE(sets("\t# CONFIG_A is not set \r"));
+ EXPECT_STREQ(key, "CONFIG_A");
+ EXPECT_STREQ(value, "n");
+}
+
+static void any_other_line_sets_nothing(void)
+{
+ static const char * const lines[] = {
+ "", " \t\r", "#", "# Automatically generated file; DO NOT EDIT.",
+ "#CONFIG_A=y", "# CONFIG_A=y", "# CONFIG_A is not set at all",
+ "# CONFIG_ is not set", "CONFIG_A", "CONFIG_=y", "A=y", "=y",
+ };
+ size_t i;
+
+ for (i = 0; i < ARRAY_SIZE(lines); i++)
+ EXPECT_FALSE(sets(lines[i]));
+}
+
+static const struct unit_case cases[] = {
+ UNIT_CASE(an_option_has_its_value),
+ UNIT_CASE(a_string_comes_without_its_quotes),
+ UNIT_CASE(an_option_not_set_is_n),
+ UNIT_CASE(white_space_around_is_no_part),
+ UNIT_CASE(any_other_line_sets_nothing),
+ {}
+};
+
+UNIT_SUITE(kconfig, cases);
diff --git a/util/bpm_tree_state.c b/util/bpm_tree_state.c
index 87f5e698..cadb0cb8 100644
--- a/util/bpm_tree_state.c
+++ b/util/bpm_tree_state.c
@@ -28,6 +28,7 @@
#include "crypto.h"
#include "io.h"
#include "json.h"
+#include "kconfig.h"
#include "kernel_build_id.h"
#include "kernel_release.h"
#include "log.h"
@@ -197,57 +198,17 @@ static int selector_hash_find(const struct tree_selector *s,
return -1;
}
-static int selector_config_line(const char *line,
- const struct tree_selector *s,
+static int selector_config_line(char *line, const struct tree_selector *s,
const int slots[TREE_SELECTOR_HASH_SIZE],
char values[][TREE_STRING_MAX],
bool found[])
{
- char copy[1024], not_set[] = "n", *start, *eq, *end, *value;
+ const char *key, *value;
int index;
- if (strscpy(copy, line, sizeof(copy)) < 0)
- return 0;
- start = copy;
- while (*start && isspace((__u8)*start))
- start++;
- if (!*start)
+ if (!kconfig_line(line, &key, &value))
return 0;
-
- if (str_has_prefix(start, "# CONFIG_")) {
- start += strlen("# ");
- eq = span_find(start, strlen(start), " is not set");
- if (!eq)
- return 0;
- *eq = '\0';
- value = not_set;
- } else {
- if (*start == '#')
- return 0;
- eq = strchr(start, '=');
- if (!eq)
- return 0;
- *eq++ = '\0';
- end = start + strlen(start);
- while (end > start && isspace((__u8)end[-1]))
- *--end = '\0';
- while (*eq && isspace((__u8)*eq))
- eq++;
- end = eq + strlen(eq);
- while (end > eq && isspace((__u8)end[-1]))
- *--end = '\0';
- value = eq;
- if (value[0] == '"') {
- size_t len = strlen(value);
-
- if (len > 1 && value[len - 1] == '"') {
- value[len - 1] = '\0';
- value++;
- }
- }
- }
-
- index = selector_hash_find(s, slots, start);
+ index = selector_hash_find(s, slots, key);
if (index < 0)
return 0;
if (strscpy(values[index], value, TREE_STRING_MAX) < 0)
@@ -326,7 +287,7 @@ static int selector_config_text(const char *text, size_t len,
const int slots[TREE_SELECTOR_HASH_SIZE],
char values[][TREE_STRING_MAX], bool found[])
{
- char line[1024];
+ char line[KCONFIG_LINE_MAX + 1];
size_t off, n;
int ret;
@@ -334,7 +295,7 @@ 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;
- ret = span_to_str(line, sizeof(line) - 1, text + off, n);
+ ret = span_to_str(line, sizeof(line), text + off, n);
if (ret == -E2BIG)
continue;
if (!ret)
diff --git a/util/kconfig.c b/util/kconfig.c
new file mode 100644
index 00000000..f1d92b50
--- /dev/null
+++ b/util/kconfig.c
@@ -0,0 +1,72 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * A line of a kernel configuration.
+ */
+
+#include <ctype.h>
+#include <string.h>
+
+#include <shirudo/string.h>
+#include <shirudo/types.h>
+
+#include "kconfig.h"
+
+static char *trimmed(char *text)
+{
+ char *end;
+
+ while (isspace((__u8)*text))
+ text++;
+ end = text + strlen(text);
+ while (end > text && isspace((__u8)end[-1]))
+ *--end = '\0';
+ return text;
+}
+
+/* CONFIG_ and a name after it. */
+static bool option_name(const char *key)
+{
+ size_t n = str_has_prefix(key, "CONFIG_");
+
+ return n && key[n];
+}
+
+static const char *unquoted(char *text)
+{
+ size_t len = strlen(text);
+
+ if (len < 2 || text[0] != '"' || text[len - 1] != '"')
+ return text;
+ text[len - 1] = '\0';
+ return text + 1;
+}
+
+bool kconfig_line(char *line, const char **key, const char **value)
+{
+ static const char not_set[] = " is not set";
+ char *name = trimmed(line), *cut;
+ const char *text;
+
+ if (str_has_prefix(name, "# CONFIG_")) {
+ name += strlen("# ");
+ cut = span_find(name, strlen(name), not_set);
+ if (!cut || cut[strlen(not_set)])
+ return false;
+ *cut = '\0';
+ text = "n";
+ } else {
+ cut = strchr(name, '=');
+ if (!cut)
+ return false;
+ *cut = '\0';
+ name = trimmed(name);
+ text = unquoted(trimmed(cut + 1));
+ }
+ if (!option_name(name))
+ return false;
+ *key = name;
+ *value = text;
+ return true;
+}
diff --git a/util/kconfig.h b/util/kconfig.h
new file mode 100644
index 00000000..c8a9a4b2
--- /dev/null
+++ b/util/kconfig.h
@@ -0,0 +1,28 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * A line of a kernel configuration, as /proc/config.gz and
+ * /boot/config-<release> hold them.
+ */
+
+#ifndef SHIRUDO_KCONFIG_H
+#define SHIRUDO_KCONFIG_H
+
+#include <stdbool.h>
+
+/* The most bytes of a line that sets an option; a longer one sets none. */
+#define KCONFIG_LINE_MAX 8191u
+
+/*
+ * Whether line sets an option, and then its name in *key and its value
+ * in *value: "CONFIG_X=<value>" gives the value, without the double
+ * quotes around a string, and "# CONFIG_X is not set" gives "n". White
+ * space around the line, the name and the value is no part of them. Any
+ * other comment, a name that is not CONFIG_ and more, and text behind
+ * "is not set" set no option. line is cut up for the two, which point
+ * into it.
+ */
+bool kconfig_line(char *line, const char **key, const char **value);
+
+#endif /* SHIRUDO_KCONFIG_H */
diff --git a/util/sysinfo.c b/util/sysinfo.c
index 1da6a773..8a5cec77 100644
--- a/util/sysinfo.c
+++ b/util/sysinfo.c
@@ -21,6 +21,7 @@
#include "io.h"
#include "json.h"
+#include "kconfig.h"
#include "kernel_build_id.h"
#include "malloc.h"
#include "sysinfo.h"
@@ -208,18 +209,6 @@ static void read_root_disk(long *total_kb, long *used_kb)
*used_kb = (long)((tot_b - free_b) / 1024);
}
-static char *trim_config_text(char *text)
-{
- char *end;
-
- while (*text && isspace((__u8)*text))
- text++;
- end = text + strlen(text);
- while (end > text && isspace((__u8)end[-1]))
- *--end = '\0';
- return text;
-}
-
struct kernel_config_entry {
char *key;
char *value;
@@ -245,16 +234,6 @@ static void kernel_config_entries_free(struct kernel_config_entry *entries,
xfree(entries, cap * sizeof(*entries));
}
-/* CONFIG_ and a name after it. */
-static bool config_name(const char *key)
-{
- size_t n = str_has_prefix(key, "CONFIG_");
-
- return n && key[n];
-}
-
-#define KERNEL_CONFIG_LINE_MAX 8191u
-
int sysinfo_kernel_config_foreach(const char *kernel_release,
shirudo_kernel_config_cb cb,
void *ctx)
@@ -273,45 +252,14 @@ int sysinfo_kernel_config_foreach(const char *kernel_release,
if (!f)
return -1;
while ((got = read_text_line(f, &line, &capacity,
- KERNEL_CONFIG_LINE_MAX)) != 0) {
- char *key, *value, *eq, *not_set, unset[] = "n";
+ KCONFIG_LINE_MAX)) != 0) {
+ const char *key, *value;
if (got == -E2BIG)
continue;
if (got < 0)
break;
- line[span_until(line, strlen(line), "\r\n")] = '\0';
- key = trim_config_text(line);
- if (!*key)
- continue;
-
- if (str_has_prefix(key, "# CONFIG_")) {
- key += strlen("# ");
- not_set = span_find(key, strlen(key), " is not set");
- if (!not_set ||
- trim_config_text(not_set + strlen(" is not set"))[0] != '\0')
- continue;
- *not_set = '\0';
- value = unset;
- } else {
- eq = strchr(key, '=');
- if (!eq)
- continue;
- *eq = '\0';
- key = trim_config_text(key);
- if (!config_name(key))
- continue;
- value = trim_config_text(eq + 1);
- {
- size_t len = strlen(value);
-
- if (len >= 2 && value[0] == '"' && value[len - 1] == '"') {
- value[len - 1] = '\0';
- value++;
- }
- }
- }
- if (!config_name(key))
+ if (!kconfig_line(line, &key, &value))
continue;
if (n == cap) {
struct kernel_config_entry *next;