Skip to content

eBPF-based security platform

proofs: prove append_line() and last_line_end()

Daniel Borkmannb4429f2736c0
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rst
index 0e7e8b62..b8cf1d8b 100644
--- a/Documentation/contribute/testing/proof-coverage.rst
+++ b/Documentation/contribute/testing/proof-coverage.rst
@@ -89,6 +89,15 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.
- ``lines_len()``
- Up to 8 bytes
- Ends at a newline or is 0, with no newline behind it
+ * - ``io-append``
+ - ``append_line()``, ``last_line_end()``, ``write_complete()``
+ - Files of 0 to 6 bytes, lines of up to 3; ``fstat()``,
+ ``ftruncate()``, ``write()`` and ``fsync()`` stubs on the file,
+ the first write failing, cut short or interrupted, ``pread()``
+ never failing
+ - A line holding a line break or a NUL refused, the file unchanged;
+ else the file cut back to its last line break, then the line and
+ one line break; a failed write or sync cut back to that line break
* - ``attest-pcrs``
- ``attest_parse_pcrs()``, ``attest_parse_pcr_value()``
- Up to 6 bytes
diff --git a/testing/proofs/cbmc/io-append.c b/testing/proofs/cbmc/io-append.c
new file mode 100644
index 00000000..2eeb1a44
--- /dev/null
+++ b/testing/proofs/cbmc/io-append.c
@@ -0,0 +1,138 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * append_line() and last_line_end() of util/io.c, which add a record to
+ * the eventlog, the tree eventlog and the outcomes record, on every file of
+ * LEN bytes and every line of up to LINE_MAX: a line holding a
+ * line break or a NUL is refused with the file unchanged; else the file is
+ * cut back to its last line break and the line and one line break follow;
+ * a failed write is cut back to that line break again.
+ */
+
+/*
+ * The file's length is a constant to CBMC: last_line_end() reads it into
+ * 4 KiB at offsets that would otherwise be symbolic.
+ */
+#define OLD_MAX LEN
+#define LINE_MAX 3
+#define PROOF_FILE (OLD_MAX + LINE_MAX + 1)
+
+#include "proof.h"
+#include "../../../util/malloc.c"
+#include "../../../util/io.c"
+
+static bool stat_fails, sync_fails;
+static int first_write; /* 0 any count, else a failure's errno */
+static size_t first_count;
+static int writes;
+
+void pr_warn(const char *fmt, ...)
+{
+}
+
+/* The size set either way, so that CBMC keeps it a constant past the call. */
+int fstat(int fd, struct stat *st)
+{
+ st->st_size = (off_t)proof_file_len;
+ if (stat_fails) {
+ errno = EIO;
+ return -1;
+ }
+ return 0;
+}
+
+int ftruncate(int fd, off_t len)
+{
+ __CPROVER_assert(len >= 0 && (size_t)len <= proof_file_len,
+ "a file cut, never grown");
+ proof_file_len = (size_t)len;
+ return 0;
+}
+
+/*
+ * The first write fails with EINTR or EIO, takes nothing, or takes any
+ * part; any later one takes all it is given.
+ */
+ssize_t write(int fd, const void *buf, size_t n)
+{
+ size_t count = n;
+
+ if (!writes++) {
+ if (first_write) {
+ errno = first_write;
+ return -1;
+ }
+ __CPROVER_assume(first_count <= n);
+ count = first_count;
+ }
+ __CPROVER_assert(count <= n && proof_file_len + count <= PROOF_FILE,
+ "a write inside the model");
+ memcpy(proof_file + proof_file_len, buf, count);
+ proof_file_len += count;
+ return (ssize_t)count;
+}
+
+int fsync(int fd)
+{
+ if (sync_fails) {
+ errno = EIO;
+ return -1;
+ }
+ return 0;
+}
+
+void harness(void)
+{
+ bool any_stat, any_sync, newline;
+ size_t old, len, end, i, any_count;
+ char was[OLD_MAX + 1], *line;
+ int any_write, ret;
+
+ proof_file_open();
+ old = OLD_MAX;
+ __CPROVER_assume(len <= LINE_MAX);
+ proof_file_len = old;
+ for (i = 0; i < OLD_MAX; i++)
+ was[i] = proof_file[i];
+ stat_fails = any_stat;
+ sync_fails = any_sync;
+ first_write = any_write;
+ first_count = any_count;
+ __CPROVER_assume(!first_write || first_write == EINTR ||
+ first_write == EIO);
+ line = any_bytes(LINE_MAX);
+ newline = len && line[len - 1] == '\n';
+ ret = append_line(3, "log", line, len);
+ if (memchr(line, '\n', len - newline) ||
+ memchr(line, '\0', len - newline)) {
+ __CPROVER_assert(ret == -EINVAL && proof_file_len == old,
+ "a line holding a line break or a NUL refused");
+ return;
+ }
+ if (stat_fails) {
+ __CPROVER_assert(ret == -EIO && proof_file_len == old,
+ "a file it cannot stat left alone");
+ return;
+ }
+ end = lines_len(was, old);
+ if (ret) {
+ __CPROVER_assert((first_write == EIO && ret == -EIO) ||
+ (!first_write && !first_count && ret == -EIO) ||
+ (sync_fails && ret == -EIO),
+ "a failed write or sync its error");
+ __CPROVER_assert(proof_file_len == end,
+ "and the file cut back to its last line break");
+ return;
+ }
+ __CPROVER_assert(proof_file_len == end + len - newline + 1,
+ "the file to its last line break, the line and one");
+ for (i = 0; i < end; i++)
+ __CPROVER_assert(proof_file[i] == was[i],
+ "the whole lines before kept");
+ for (i = 0; i < len - newline; i++)
+ __CPROVER_assert(proof_file[end + i] == line[i],
+ "then the line");
+ __CPROVER_assert(proof_file[end + len - newline] == '\n',
+ "then one line break");
+}
diff --git a/testing/proofs/cbmc/proofs.list b/testing/proofs/cbmc/proofs.list
index 2e552ed3..3670aae5 100644
--- a/testing/proofs/cbmc/proofs.list
+++ b/testing/proofs/cbmc/proofs.list
@@ -7,6 +7,7 @@ string-suffix --unwind 8
string-sanitize --unwind 8 LEN=0-6
hex --unwind 12
io-lines --unwind 10
+io-append --unwind 12 LEN=0-6
attest-pcrs --unwind 9
attest-name-digest --unwind 72 LEN=64-70
paths-bpffs-name --unwind 18