eBPF-based security platform
eventlog: keep an unfinished append from joining the next record
Daniel Borkmanna0e82bf879c5
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex 51278191..777ed0bb 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -491,13 +491,13 @@ signed config image.Unit suites and fuzz targets~~~~~~~~~~~~~~~~~~~~~~~~~~~~-The unit suites in :src:`testing/unit/` cover nine of the modules in-:src:`util/`: ``chain``, ``hex``, ``json``, ``kernel_abi``,+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-:src:`include/shirudo/overflow.h`. The-other modules are reached through the test helpers, the fuzz targets and-the binaries the functional tests run.+:src:`include/shirudo/overflow.h`. The other modules are reached through+the test helpers, the fuzz targets and the binaries the functional tests+run... list-table:::header-rows: 1@@ -610,7 +610,7 @@ Coverage by area``t9105``. Fuzz: ``attest``, ``tpm-wire``* - Eventlog- ``t4002``, ``t4102``, ``t5007``, ``t6114``, ``t6128``. Fuzz:- ``eventlog``+ ``eventlog``. Unit: ``io``* - Boot and initrd- ``t1006``, ``t4002``, ``t4101``, ``t5006``, ``t6115``,``t6126``diff --git a/Documentation/internals/measurement-register.rst b/Documentation/internals/measurement-register.rstindex 5e9febc8..6725bf84 100644--- a/Documentation/internals/measurement-register.rst+++ b/Documentation/internals/measurement-register.rst@@ -218,8 +218,11 @@ Eventlog``/var/lib/shirudo/eventlog`` holds one JSON line per extension: theevent digest and type, the boot ID, and the fields the digest was-computed from. Each line is appended with ``fsync``, and a partial append-is truncated away. The file is sealed like the rest of the state tree.+computed from. Each line goes out with its newline in one write and is+followed by ``fsync``. A partial append is truncated away, and one that+was killed before it could be is skipped by every reader and dropped by+the next append, so that it cannot join the next record on its line.+The file is sealed like the rest of the state tree.:doc:`/reference/formats/eventlog` gives the format.The ``init`` run in the initrd writes its record todiff --git a/Documentation/reference/formats/eventlog.rst b/Documentation/reference/formats/eventlog.rstindex 3bdafe23..82515b09 100644--- a/Documentation/reference/formats/eventlog.rst+++ b/Documentation/reference/formats/eventlog.rst@@ -52,10 +52,14 @@ Neither is part of the eventlog.Record format--------------- One compact JSON object per line, terminated by ``\n``.+- One compact JSON object per line, terminated by ``\n``. A record is+ not in the log until its ``\n`` is: a last line without one is an+ append that did not finish, killed or cut short by a crash.- A command appends a record only after the register extend succeeded,- then calls ``fsync``. An append that fails part-way is truncated back- to the previous size.+ writes it and its ``\n`` in one ``write``, then calls ``fsync``. An+ append that fails part-way is truncated back to the previous size, and+ the next append first drops a last line that did not finish, with a+ warning in the journal.- Digests are SHA-256, written as 64 lowercase hex digits.Every record carries these fields:@@ -240,7 +244,9 @@ 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.+ 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.- 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/io.c b/testing/unit/io.cnew file mode 100644index 00000000..f8241049--- /dev/null+++ b/testing/unit/io.c@@ -0,0 +1,105 @@+// 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 "io.h"+#include "unit.h"++static int log_with(const char *text, size_t len)+{+ const char *dir = getenv("TMPDIR");+ char path[4096];+ int fd;++ ASSERT_TRUE(strsprintf(path, sizeof(path), "%s/shirudo-unit-io-XXXXXX",+ dir && dir[0] ? dir : "/tmp") > 0);+ fd = mkstemp(path);+ ASSERT_TRUE(fd >= 0);+ ASSERT_EQ(unlink(path), 0);+ ASSERT_EQ(fcntl(fd, F_SETFL, O_APPEND), 0);+ ASSERT_EQ(write_complete(fd, text, len), 0);+ return fd;+}++static void expect_log(int fd, const char *want)+{+ char buf[16384];+ ssize_t n = pread(fd, buf, sizeof(buf) - 1, 0);++ ASSERT_TRUE(n >= 0);+ buf[n] = '\0';+ EXPECT_STREQ(buf, want);+}++static void lines_len_ends_at_the_last_newline(void)+{+ EXPECT_EQ(lines_len("", 0), 0);+ EXPECT_EQ(lines_len("abc", 3), 0);+ EXPECT_EQ(lines_len("a\n", 2), 2);+ EXPECT_EQ(lines_len("a\nb", 3), 2);+ EXPECT_EQ(lines_len("a\nb\n", 4), 4);+ EXPECT_EQ(lines_len("\n\n", 2), 2);+}++static void append_line_adds_one_record(void)+{+ int fd = log_with("", 0);++ EXPECT_EQ(append_line(fd, "log", "x", 1), 0);+ EXPECT_EQ(append_line(fd, "log", "y\n", 2), 0);+ expect_log(fd, "x\ny\n");+ close(fd);+}++/*+ * A record killed before its newline would run into the next one on+ * its line, and every reader after would refuse the log.+ */+static void append_line_drops_what_did_not_finish(void)+{+ char text[12000];+ int fd;++ fd = log_with("x\n{\"half\":", 10);+ EXPECT_EQ(append_line(fd, "log", "z", 1), 0);+ expect_log(fd, "x\nz\n");+ close(fd);++ fd = log_with("{\"whole\":1}", 11);+ EXPECT_EQ(append_line(fd, "log", "z", 1), 0);+ expect_log(fd, "z\n");+ close(fd);++ memcpy(text, "x\n", 2);+ memset(text + 2, 'a', sizeof(text) - 2);+ fd = log_with(text, sizeof(text));+ EXPECT_EQ(append_line(fd, "log", "z", 1), 0);+ expect_log(fd, "x\nz\n");+ close(fd);+}++static void append_line_takes_one_line_alone(void)+{+ int fd = log_with("x\n", 2);++ EXPECT_EQ(append_line(fd, "log", "a\nb", 3), -EINVAL);+ EXPECT_EQ(append_line(fd, "log", "a\0b", 3), -EINVAL);+ expect_log(fd, "x\n");+ close(fd);+}++static const struct unit_case cases[] = {+ UNIT_CASE(lines_len_ends_at_the_last_newline),+ UNIT_CASE(append_line_adds_one_record),+ UNIT_CASE(append_line_drops_what_did_not_finish),+ UNIT_CASE(append_line_takes_one_line_alone),+ {}+};++UNIT_SUITE(io, cases);diff --git a/util/eventlog.c b/util/eventlog.cindex 3448a872..19515272 100644--- a/util/eventlog.c+++ b/util/eventlog.c@@ -66,17 +66,16 @@ int eventlog_append(const char *json_line)}/*- * A line cut short, by a full file system say, would leave every reader- * of the log failing on it, so a failed append is rolled back under the- * lock that keeps appenders from interleaving.+ * A line cut short, by a full file system or a kill between writes,+ * would join the next record on its line and leave every reader of the+ * log failing on it, so appenders take the lock that keeps them from+ * interleaving and append_line() drops what did not finish.*/int eventlog_append_to(const char *path, const char *json_line){- size_t len = strlen(json_line);- struct stat st;int fd, ret;- fd = seal_open(path, O_WRONLY | O_CREAT | O_APPEND, 0600);+ fd = seal_open(path, O_RDWR | O_CREAT | O_APPEND, 0600);if (fd < 0) {pr_err("eventlog: open %s: %s", path, strerror(-fd));return fd;@@ -87,19 +86,7 @@ int eventlog_append_to(const char *path, const char *json_line)goto out;}}- if (fstat(fd, &st) != 0) {- ret = -errno;- goto out;- }-- ret = write_complete(fd, json_line, len);- if (!ret && (len == 0 || json_line[len - 1] != '\n'))- ret = write_complete(fd, "\n", 1);- if (!ret && fsync(fd) != 0)- ret = -errno;- if (ret && ftruncate(fd, st.st_size) != 0)- pr_warn("eventlog: could not roll back a partial append to "- "%s: %s", path, strerror(errno));+ ret = append_line(fd, path, json_line, strlen(json_line));out:if (close(fd) != 0 && !ret)ret = -errno;@@ -142,11 +129,13 @@ static int read_log(char **out, size_t *out_len)if (ret && rc_pending)return -ENOENT;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;@@ -155,17 +144,15 @@ static int read_log(char **out, size_t *out_len)ret = -EFBIG;goto out;}- buf = xmalloc(base_len + pending_len + 2);+ buf = xmalloc(base_len + pending_len + 1);if (!buf) {ret = -ENOMEM;goto out;}- memcpy(buf, base, base_len);- off = base_len;- if (off && buf[off - 1] != '\n')- buf[off++] = '\n';- memcpy(buf + off, pending, pending_len);- off += pending_len;+ off = lines_len(base, base_len);+ memcpy(buf, base, off);+ memcpy(buf + off, pending, lines_len(pending, pending_len));+ off += lines_len(pending, pending_len);buf[off] = '\0';*out = buf;*out_len = off;@@ -269,6 +256,7 @@ 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);xfree(buf, blen + 1);if (ret && ret != -ENOENT)@@ -288,6 +276,7 @@ static int adopt_pending(void)return 0;if (ret)return ret;+ buf[lines_len(buf, len)] = '\0';for (line = strtok_r(buf, "\n", &save); line && !ret;line = strtok_r(NULL, "\n", &save)) {if (!*line)diff --git a/util/io.c b/util/io.cindex 24d03aa4..94f9a254 100644--- a/util/io.c+++ b/util/io.c@@ -50,6 +50,79 @@ int write_complete(int fd, const void *buf, size_t len)* mounts or links elsewhere, /proc/<pid>/fd before an exec say, is* followed.*/+size_t lines_len(const char *buf, size_t len)+{+ while (len && buf[len - 1] != '\n')+ len--;+ return len;+}++static int last_line_end(int fd, off_t size, off_t *end)+{+ char chunk[4096];+ const off_t step = sizeof(chunk);+ off_t off = size;++ while (off > 0) {+ size_t n = (size_t)min(off, step), i;+ off_t start = off - (off_t)n;+ ssize_t got = pread(fd, chunk, n, start);++ if (got < 0 && errno == EINTR)+ continue;+ if (got < 0)+ return -errno;+ if ((size_t)got != n)+ return -EIO;+ for (i = n; i; i--) {+ if (chunk[i - 1] == '\n') {+ *end = start + (off_t)i;+ return 0;+ }+ }+ off = start;+ }+ *end = 0;+ return 0;+}++int append_line(int fd, const char *path, const char *line, size_t len)+{+ struct stat st;+ off_t end;+ char *buf;+ int ret;++ if (len && line[len - 1] == '\n')+ len--;+ if (memchr(line, '\n', len) || memchr(line, '\0', len))+ return -EINVAL;+ if (fstat(fd, &st))+ return -errno;+ ret = last_line_end(fd, st.st_size, &end);+ if (ret)+ return ret;+ if (end != st.st_size) {+ if (ftruncate(fd, end))+ return -errno;+ pr_warn("%s: dropped %lld bytes at its end, an append that did not finish",+ path, (__s64)(st.st_size - end));+ }+ buf = xmalloc(len + 1);+ if (!buf)+ return -ENOMEM;+ memcpy(buf, line, len);+ buf[len] = '\n';+ ret = write_complete(fd, buf, len + 1);+ xfree(buf, len + 1);+ if (!ret && fsync(fd))+ ret = -errno;+ if (ret && ftruncate(fd, end))+ pr_warn("%s: could not roll back a partial append: %s", path,+ strerror(errno));+ return ret;+}+int open_beneath(int dirfd, const char *rel, int flags){struct open_how how = {diff --git a/util/io.h b/util/io.hindex 6e5e0a17..e74f9c27 100644--- a/util/io.h+++ b/util/io.h@@ -10,6 +10,19 @@int write_complete(int fd, const void *buf, size_t len);+/*+ * A log record is a line that ends in its newline: a tail without one+ * is an append that did not finish, killed between its writes or cut by+ * a crash. lines_len() is how much of the len bytes at buf are whole+ * records, which is all a reader may take. append_line() drops such a+ * tail from the log open on fd at path, which the caller has opened+ * O_RDWR | O_APPEND and locked, and adds line and its newline in one+ * write, taking the file back to its last record when that fails. A+ * line with a newline or a NUL inside is -EINVAL.+ */+size_t lines_len(const char *buf, size_t len);+int append_line(int fd, const char *path, const char *line, size_t len);+/** A regular file of at most max bytes, into *out xmalloc'd to *len + 1* bytes and NUL-terminated; out is the address of a char or unsigned