Skip to content

eBPF-based security platform

status: list the eventlog line by line, past a NUL

Daniel Borkmannf33a58c33d32
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index 1b2305a8..31f0580e 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -331,6 +331,11 @@ signed config image.
key and node B's under another; a quote over an altered body is
``body_mismatch``; with the libtpms simulator built, the capture
script rebuilds a file that verifies
+ * - ``t4007-status-eventlog``
+ - ``status --include-eventlog`` lists the eventlog line by line: a
+ NUL in the log hides no record behind it and its line reads as
+ ``unparseable``, an append that did not finish is no entry, and
+ the entries counted are the entries listed
* - ``t4101-initrd``
- The production configuration: ``init`` from the initrd, its
messages in the journal once, adoption, PCR 11 after the binding,
@@ -1070,8 +1075,8 @@ Coverage by area
``t4104``, ``t9105``, ``t9109``, ``t9112``. Fuzz: ``attest``,
``attest-report``, ``tpm-wire``
* - Eventlog
- - ``t4002``, ``t4102``, ``t5007``, ``t6114``, ``t6128``. Fuzz:
- ``eventlog``. Unit: ``eventlog``, ``io``
+ - ``t4002``, ``t4007``, ``t4102``, ``t5007``, ``t6114``, ``t6128``.
+ Fuzz: ``eventlog``. Unit: ``eventlog``, ``io``
* - Boot and initrd
- ``t1006``, ``t4002``, ``t4101``, ``t5006``, ``t6115``,
``t6126``, ``t6157``. Unit: ``kernel_prereq``
@@ -1291,7 +1296,8 @@ Commands and procedures
- ``upatch load`` or ``upatch unload`` refusing ``tampered`` for a
stale ``--expect-chain``. Only ``attest verify`` is tested with that
option.
-- ``status --include-eventlog`` and ``--show-full``.
+- ``status --show-full``, and ``--include-eventlog`` on a target:
+ ``t4007`` reads a log on the host.
- ``upatch rewrite-rodata`` in ``shirudo-build``, which
``kernel-obs`` uses to rewrite its watch lists.
- The ``kernel-cve`` packages, and :src:`tools/put-shields`, which
diff --git a/Documentation/operate/run/read-status.rst b/Documentation/operate/run/read-status.rst
index ff49abe0..bb5f58e2 100644
--- a/Documentation/operate/run/read-status.rst
+++ b/Documentation/operate/run/read-status.rst
@@ -522,7 +522,12 @@ unload:
The ``init`` record comes from the initrd run. Every later record is a
state change that extended the register. :doc:`/reference/eventlog`
-describes the records.
+describes the records. A line that does not parse as a record, one
+that holds a NUL byte among them, is listed as ``unparseable`` in its
+place, and the records after it follow: the section shows the file line
+by line, where the replay behind ``ChainIntact`` refuses such a log as a
+whole. A last line without its newline, an append that did not finish,
+is not listed.
.. _status-verdicts:
diff --git a/Documentation/reference/formats/status-json.rst b/Documentation/reference/formats/status-json.rst
index d230d168..e8de13e5 100644
--- a/Documentation/reference/formats/status-json.rst
+++ b/Documentation/reference/formats/status-json.rst
@@ -710,7 +710,10 @@ Optional blocks
``/var/lib/shirudo/eventlog``, all boots, with ``ts``, ``type``
and ``digest``, plus ``set_digest`` for ``init`` and ``upatch``
(``name``, ``payload_sha256``) for upatch events. A line that
- does not parse gives ``{"type": "unparseable"}``.
+ does not parse, one that holds a NUL byte among them, gives
+ ``{"type": "unparseable"}``, and the lines after it follow. A
+ last line without its newline, an append that did not finish,
+ gives none, so that ``events`` holds ``entries`` objects.
``status`` sizes its output buffer from the options, the eventlog size
and the number of loaded upatches, at most 8 MiB. A document that does
diff --git a/builtin-status.c b/builtin-status.c
index 934ef794..a9e0a8d1 100644
--- a/builtin-status.c
+++ b/builtin-status.c
@@ -210,42 +210,62 @@ static int parse_event_line(const char *line, size_t len, struct ev_proj *ev)
return json_parse(line, len, f, ARRAY_SIZE(f));
}
-static void emit_eventlog_events(struct json_emit *e)
+/*
+ * Each line of the len bytes of the log at buf that ends in its newline
+ * to cb, as it parses or NULL where it does not. The lines are taken by
+ * their length: read as one string the log would end at its first NUL,
+ * and the records behind one would not be listed.
+ */
+static void eventlog_each(const char *buf, size_t len,
+ void (*cb)(const struct ev_proj *ev, void *ctx),
+ void *ctx)
{
- char *save, *line, *buf;
- size_t len;
+ const char *line, *nl, *end = buf + len;
+ struct ev_proj ev;
- json_arr_open(e, "events");
- if (read_file(eventlog_path(), JSON_MAX_BYTES,
- &buf, &len) != 0) {
- json_arr_close(e);
- return;
+ for (line = buf; (nl = memchr(line, '\n', (size_t)(end - line)));
+ line = nl + 1) {
+ bool parsed = !parse_event_line(line, (size_t)(nl - line),
+ &ev);
+
+ cb(parsed ? &ev : NULL, ctx);
}
- for (line = strtok_r(buf, "\n", &save); line;
- line = strtok_r(NULL, "\n", &save)) {
- struct ev_proj ev;
+}
- json_obj_open(e);
- if (parse_event_line(line, strlen(line), &ev) != 0) {
- json_kv_string(e, "type", "unparseable");
- json_obj_close(e);
- continue;
- }
- json_kv_string(e, "ts", ev.ts);
- json_kv_string(e, "type", ev.type);
- json_kv_string(e, "digest", ev.digest);
- if (ev.set_digest[0])
- json_kv_string(e, "set_digest", ev.set_digest);
- if (ev.up_name[0]) {
- json_obj_open_key(e, "upatch");
- json_kv_string(e, "name", ev.up_name);
- json_kv_string(e, "payload_sha256",
- ev.up_payload);
- json_obj_close(e);
- }
+static void emit_event(const struct ev_proj *ev, void *ctx)
+{
+ struct json_emit *e = ctx;
+
+ json_obj_open(e);
+ if (!ev) {
+ json_kv_string(e, "type", "unparseable");
json_obj_close(e);
+ return;
+ }
+ json_kv_string(e, "ts", ev->ts);
+ json_kv_string(e, "type", ev->type);
+ json_kv_string(e, "digest", ev->digest);
+ if (ev->set_digest[0])
+ json_kv_string(e, "set_digest", ev->set_digest);
+ if (ev->up_name[0]) {
+ json_obj_open_key(e, "upatch");
+ json_kv_string(e, "name", ev->up_name);
+ json_kv_string(e, "payload_sha256", ev->up_payload);
+ json_obj_close(e);
+ }
+ json_obj_close(e);
+}
+
+static void emit_eventlog_events(struct json_emit *e)
+{
+ size_t len;
+ char *buf;
+
+ json_arr_open(e, "events");
+ if (!read_file(eventlog_path(), JSON_MAX_BYTES, &buf, &len)) {
+ eventlog_each(buf, len, emit_event, e);
+ xfree(buf, len + 1);
}
- xfree(buf, len + 1);
json_arr_close(e);
}
@@ -261,10 +281,23 @@ static int event_detail(const struct ev_proj *ev, char *out, size_t cap)
return 0;
}
+static void print_event(const struct ev_proj *ev, void *ctx)
+{
+ char detail[256];
+
+ if (!ev) {
+ printf(" %-20s %-13s %-13s\n", "?", "unparseable", "");
+ return;
+ }
+ event_detail(ev, detail, sizeof(detail));
+ printf(" %-20s %-13s %-13.12s %s\n", ev->ts, ev->type,
+ ev->digest[0] ? ev->digest : "-", detail);
+}
+
static void print_eventlog_table(void)
{
- char *save, *line, *buf;
size_t len;
+ char *buf;
if (read_file(eventlog_path(), JSON_MAX_BYTES,
&buf, &len) != 0)
@@ -272,20 +305,7 @@ static void print_eventlog_table(void)
printf("\nEventlog:\n");
printf(" %-20s %-13s %-13s %s\n", "TIME", "TYPE", "DIGEST",
"DETAIL");
- for (line = strtok_r(buf, "\n", &save); line;
- line = strtok_r(NULL, "\n", &save)) {
- char detail[256];
- struct ev_proj ev;
-
- if (parse_event_line(line, strlen(line), &ev) != 0) {
- printf(" %-20s %-13s %-13s\n", "?", "unparseable",
- "");
- continue;
- }
- event_detail(&ev, detail, sizeof(detail));
- printf(" %-20s %-13s %-13.12s %s\n", ev.ts, ev.type,
- ev.digest[0] ? ev.digest : "-", detail);
- }
+ eventlog_each(buf, len, print_event, NULL);
xfree(buf, len + 1);
}
diff --git a/testing/functional/tpm/t4007-status-eventlog.sh b/testing/functional/tpm/t4007-status-eventlog.sh
new file mode 100755
index 00000000..3d438aaa
--- /dev/null
+++ b/testing/functional/tpm/t4007-status-eventlog.sh
@@ -0,0 +1,67 @@
+#!/bin/sh
+# SPDX-License-Identifier: BSD-2-Clause
+# Copyright (c) 2026 Authors of shirudo
+#
+# t4007 -- status --include-eventlog lists the eventlog line by line: a
+# NUL in the log hides no record behind it, the line that holds it is
+# listed as one that does not parse, an append that did not finish is no
+# entry, and the entries counted are the entries listed. The walk read
+# the log as one string, so that the list ended at a NUL without a word.
+
+cd "$(dirname "$0")/../.."
+. ./test-lib.sh
+
+SHIRUDO=${SHIRUDO:-../shirudo}
+W=$(mktemp -d)
+trap 'rm -rf "$W"' EXIT
+export SHIRUDO_EVENTLOG_PATH="$W/eventlog"
+
+record() {
+ printf '{"schema":1,"ts":"2026-11-20T00:00:0%sZ","type":"%s","digest":"%064d","upatch":{"name":"%s","payload_sha256":"%064d"}}' \
+ "$1" "$2" "$1" "$3" "$1"
+}
+
+# How many events of the JSON report have type $1.
+events() {
+ "$SHIRUDO" status --json --include-eventlog 2>/dev/null |
+ grep -o "\"type\":\"$1\"" | wc -l
+}
+
+entries() {
+ "$SHIRUDO" status --json --include-eventlog 2>/dev/null |
+ grep -o '"entries":[0-9]*' | cut -d: -f2
+}
+
+test_expect_success 'every record of the log is listed' '
+ { record 1 upatch_load one; echo; record 2 upatch_unload one; echo; } \
+ >"$SHIRUDO_EVENTLOG_PATH" &&
+ [ "$(entries)" = 2 ] &&
+ [ "$(events upatch_load)" = 1 ] &&
+ [ "$(events upatch_unload)" = 1 ] &&
+ "$SHIRUDO" status --include-eventlog >"$W/table" 2>/dev/null &&
+ grep -q "upatch_unload .* one payload=" "$W/table"
+'
+
+test_expect_success 'a NUL in the log hides no record behind it' '
+ { record 1 upatch_load one; echo; printf "\000"; record 2 upatch_load two
+ echo; record 3 upatch_unload one; echo; } >"$SHIRUDO_EVENTLOG_PATH" &&
+ [ "$(entries)" = 3 ] &&
+ [ "$(events upatch_load)" = 1 ] &&
+ [ "$(events unparseable)" = 1 ] &&
+ [ "$(events upatch_unload)" = 1 ] &&
+ "$SHIRUDO" status --include-eventlog >"$W/table" 2>/dev/null &&
+ grep -q "unparseable" "$W/table" &&
+ grep -q "upatch_unload .* one payload=" "$W/table"
+'
+
+test_expect_success 'an append that did not finish is no entry' '
+ { record 1 upatch_load one; echo; record 2 upatch_unload one; } \
+ >"$SHIRUDO_EVENTLOG_PATH" &&
+ [ "$(entries)" = 1 ] &&
+ [ "$(events upatch_load)" = 1 ] &&
+ [ "$(events upatch_unload)" = 0 ] &&
+ "$SHIRUDO" status --include-eventlog >"$W/table" 2>/dev/null &&
+ ! grep -q "upatch_unload" "$W/table"
+'
+
+test_done