Skip to content

eBPF-based security platform

eventlog: refuse a NUL in the log

Daniel Borkmann96f99c675074
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index 777ed0bb..cfe1e4ee 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -491,10 +491,10 @@ signed config image.
Unit suites and fuzz targets
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-The unit suites in :src:`testing/unit/` cover ten of the modules in
-:src:`util/`: ``chain``, ``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 eleven of the modules in
+:src:`util/`: ``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.
@@ -610,7 +610,7 @@ Coverage by area
``t9105``. Fuzz: ``attest``, ``tpm-wire``
* - Eventlog
- ``t4002``, ``t4102``, ``t5007``, ``t6114``, ``t6128``. Fuzz:
- ``eventlog``. Unit: ``io``
+ ``eventlog``. Unit: ``eventlog``, ``io``
* - Boot and initrd
- ``t1006``, ``t4002``, ``t4101``, ``t5006``, ``t6115``,
``t6126``
diff --git a/Documentation/reference/formats/eventlog.rst b/Documentation/reference/formats/eventlog.rst
index 82515b09..35fa587e 100644
--- a/Documentation/reference/formats/eventlog.rst
+++ b/Documentation/reference/formats/eventlog.rst
@@ -244,9 +244,10 @@ this boot's records the same way:
- They read ``/var/lib/shirudo/eventlog`` followed by
``/run/shirudo/eventlog.pending``.
- They parse every line. One line that is not valid JSON fails the
- replay, whatever boot it belongs to. A last line without ``\n`` is
- skipped, as it is no record yet. The register then runs ahead of the
- log, as after an append that failed.
+ replay, whatever boot it belongs to, and so does a NUL anywhere in
+ either file, which would end the walk early. A last line without
+ ``\n`` is skipped, as it is no record yet. The register then runs
+ ahead of the log, as after an append that failed.
- They skip lines without a ``boot_id`` or with another boot's
``boot_id``.
- They start at this boot's first ``init`` record. Every ``init``
diff --git a/testing/unit/eventlog.c b/testing/unit/eventlog.c
new file mode 100644
index 00000000..2e0beaa8
--- /dev/null
+++ b/testing/unit/eventlog.c
@@ -0,0 +1,131 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/* Copyright (c) 2026 Authors of shirudo */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <shirudo/string.h>
+
+#include "eventlog.h"
+#include "io.h"
+#include "json.h"
+#include "unit.h"
+
+static char dir[4096], log_path[4200], pending_path[4200];
+
+static void use_log(void)
+{
+ const char *tmp = getenv("TMPDIR");
+
+ ASSERT_TRUE(strsprintf(dir, sizeof(dir),
+ "%s/shirudo-unit-eventlog-XXXXXX",
+ tmp && tmp[0] ? tmp : "/tmp") > 0);
+ ASSERT_TRUE(mkdtemp(dir));
+ ASSERT_TRUE(strsprintf(log_path, sizeof(log_path), "%s/eventlog",
+ dir) > 0);
+ ASSERT_TRUE(strsprintf(pending_path, sizeof(pending_path),
+ "%s/pending", dir) > 0);
+ ASSERT_EQ(setenv("SHIRUDO_EVENTLOG_PATH", log_path, 1), 0);
+ ASSERT_EQ(setenv("SHIRUDO_EVENTLOG_PENDING_PATH", pending_path, 1), 0);
+}
+
+static void drop_log(void)
+{
+ unlink(log_path);
+ unlink(pending_path);
+ rmdir(dir);
+}
+
+static void record(char *buf, size_t cap, const char *type, __u8 fill)
+{
+ struct json_emit e = { .buf = buf, .cap = cap };
+ __u8 digest[32];
+
+ memset(digest, fill, sizeof(digest));
+ eventlog_record_open(&e, type, digest);
+ json_obj_close(&e);
+ ASSERT_EQ(e.err, 0);
+}
+
+static void write_log(const char *text, size_t len)
+{
+ int fd = open(log_path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
+
+ ASSERT_TRUE(fd >= 0);
+ ASSERT_EQ(write_complete(fd, text, len), 0);
+ ASSERT_EQ(close(fd), 0);
+}
+
+static void replay_of(const char *a, const char *b, __u8 out[32])
+{
+ char text[2048];
+ int len;
+
+ len = strsprintf(text, sizeof(text), "%s\n%s%s", a, b, b[0] ? "\n" : "");
+ ASSERT_TRUE(len > 0);
+ write_log(text, (size_t)len);
+ ASSERT_EQ(eventlog_replay(out), 0);
+}
+
+/*
+ * A record killed before its newline is no record yet: readers skip it,
+ * and the next append drops it rather than run on from it.
+ */
+static void unfinished_append_is_no_record(void)
+{
+ char init[1024], load[1024], unload[1024], text[2048];
+ __u8 alone[32], both[32], got[32];
+ int len;
+
+ use_log();
+ record(init, sizeof(init), "init", 1);
+ record(load, sizeof(load), "upatch_load", 2);
+ record(unload, sizeof(unload), "upatch_unload", 3);
+ replay_of(init, "", alone);
+ replay_of(init, unload, both);
+
+ len = strsprintf(text, sizeof(text), "%s\n%.*s", init,
+ (int)strlen(load) - 1, load);
+ ASSERT_TRUE(len > 0);
+ write_log(text, (size_t)len);
+ ASSERT_EQ(eventlog_replay(got), 0);
+ EXPECT_MEMEQ(got, alone, 32);
+
+ len = strsprintf(text, sizeof(text), "%s\n%s", init, load);
+ ASSERT_TRUE(len > 0);
+ write_log(text, (size_t)len);
+ ASSERT_EQ(eventlog_replay(got), 0);
+ EXPECT_MEMEQ(got, alone, 32);
+ ASSERT_EQ(eventlog_append_to(log_path, unload), 0);
+ ASSERT_EQ(eventlog_replay(got), 0);
+ EXPECT_MEMEQ(got, both, 32);
+ drop_log();
+}
+
+/* A NUL would end the walk early and hide the records after it. */
+static void nul_in_the_log_is_refused(void)
+{
+ char init[1024], unload[1024], text[2048];
+ __u8 got[32];
+ int len;
+
+ use_log();
+ record(init, sizeof(init), "init", 1);
+ record(unload, sizeof(unload), "upatch_unload", 3);
+ len = strsprintf(text, sizeof(text), "%s\n_%s\n", init, unload);
+ ASSERT_TRUE(len > 0);
+ text[strlen(init) + 1] = '\0';
+ write_log(text, (size_t)len);
+ EXPECT_EQ(eventlog_replay(got), -EINVAL);
+ drop_log();
+}
+
+static const struct unit_case cases[] = {
+ UNIT_CASE(unfinished_append_is_no_record),
+ UNIT_CASE(nul_in_the_log_is_refused),
+ {}
+};
+
+UNIT_SUITE(eventlog, cases);
diff --git a/util/eventlog.c b/util/eventlog.c
index 19515272..8a964091 100644
--- a/util/eventlog.c
+++ b/util/eventlog.c
@@ -109,6 +109,19 @@ int eventlog_would_exceed(__u64 max_bytes)
return cur + EVENTLOG_LINE_MAX > max_bytes;
}
+/*
+ * The records of the len bytes read_file() left at buf: a NUL would end
+ * every walk there and hide the records after it, and a last line
+ * without its newline is no record yet.
+ */
+static int log_records(char *buf, size_t len)
+{
+ if (memchr(buf, '\0', len))
+ return -EINVAL;
+ buf[lines_len(buf, len)] = '\0';
+ return 0;
+}
+
static int read_log(char **out, size_t *out_len)
{
size_t base_len = 0, pending_len = 0, off;
@@ -128,14 +141,17 @@ static int read_log(char **out, size_t *out_len)
}
if (ret && rc_pending)
return -ENOENT;
+ if ((!ret && log_records(base, base_len)) ||
+ (!rc_pending && log_records(pending, pending_len))) {
+ ret = -EINVAL;
+ goto out;
+ }
if (rc_pending) {
- base[lines_len(base, base_len)] = '\0';
*out = base;
*out_len = base_len;
return 0;
}
if (ret) {
- pending[lines_len(pending, pending_len)] = '\0';
*out = pending;
*out_len = pending_len;
return 0;
@@ -149,10 +165,10 @@ static int read_log(char **out, size_t *out_len)
ret = -ENOMEM;
goto out;
}
- off = lines_len(base, base_len);
+ off = strlen(base);
memcpy(buf, base, off);
- memcpy(buf + off, pending, lines_len(pending, pending_len));
- off += lines_len(pending, pending_len);
+ memcpy(buf + off, pending, strlen(pending));
+ off += strlen(pending);
buf[off] = '\0';
*out = buf;
*out_len = off;
@@ -256,8 +272,9 @@ static int log_holds(const char *line, size_t len)
return 0;
if (ret)
return ret;
- buf[lines_len(buf, blen)] = '\0';
- ret = walk_lines(buf, false, adopt_seen, &a);
+ ret = log_records(buf, blen);
+ if (!ret)
+ ret = walk_lines(buf, false, adopt_seen, &a);
xfree(buf, blen + 1);
if (ret && ret != -ENOENT)
return ret;
@@ -276,7 +293,7 @@ static int adopt_pending(void)
return 0;
if (ret)
return ret;
- buf[lines_len(buf, len)] = '\0';
+ ret = log_records(buf, len);
for (line = strtok_r(buf, "\n", &save); line && !ret;
line = strtok_r(NULL, "\n", &save)) {
if (!*line)