eBPF-based security platform
report: write a verifier log and a kernel release that are no UTF-8
Daniel Borkmann88a3e9689681
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex 501d1724..97f18759 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -928,7 +928,7 @@ signed config image.Unit suites and fuzz targets~~~~~~~~~~~~~~~~~~~~~~~~~~~~-The unit suites in :src:`testing/unit/` cover 40 of the modules in+The unit suites in :src:`testing/unit/` cover 41 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``,@@ -937,7 +937,7 @@ The unit suites in :src:`testing/unit/` cover 40 of the modules in``keepalive``, ``keepalive_quote``, ``kernel_build_id``,``kernel_prereq``, ``kernel_release``, ``keys``, ``log``, ``measured``,``options``,-``paths``, ``string``, ``sysinfo``, ``timefmt``, ``upatch``,+``paths``, ``report``, ``string``, ``sysinfo``, ``timefmt``, ``upatch``,``upatch_decode``, ``upatch_pins``, ``upatch_resolve``, ``upatch_verify``and ``xdp``, and``overflow`` the arithmetic ofdiff --git a/Documentation/reference/failure-categories.rst b/Documentation/reference/failure-categories.rstindex 3bc13ec1..032fb055 100644--- a/Documentation/reference/failure-categories.rst+++ b/Documentation/reference/failure-categories.rst@@ -482,7 +482,9 @@ link.* - ``verifier_log``- string- The kernel's log from ``signed_loader_load`` or ``payload_load``,- up to 16 MiB. Empty otherwise.+ up to 16 MiB, with U+FFFD for each byte that is no UTF-8, as a+ source line or a file name the log quotes can hold. Empty+ otherwise.* - ``loader_return``- integer- The loader's return value from ``BPF_PROG_TEST_RUN``, 0 when it did@@ -504,7 +506,8 @@ link.before the artifact was read.* - ``kernel.release``- string- - The running kernel's release, from ``uname``.+ - The running kernel's release, from ``uname``, with U+FFFD for+ each byte that is no UTF-8.* - ``tpm``- object or null- Present on success when the register moved, else null.diff --git a/testing/unit/report.c b/testing/unit/report.cnew file mode 100644index 00000000..4efdc749--- /dev/null+++ b/testing/unit/report.c@@ -0,0 +1,76 @@+// SPDX-License-Identifier: BSD-2-Clause+/* Copyright (c) 2026 Authors of shirudo */++#include <errno.h>++#include <shirudo/string.h>++#include "malloc.h"+#include "report.h"+#include "unit.h"++#define FFFD "\\ufffd"++static void a_refusal_is_one_document(void)+{+ struct op_report r = {+ .op = "upatch_load",+ .phase = "parse",+ .err = EINVAL,+ .category = "parser_rejected",+ .message = "unsafe upatch name",+ .path = "a.upatch",+ .name = "a",+ };+ size_t cap;+ char *doc;++ ASSERT_EQ(report_encode(&r, "7.4.0", &doc, &cap), 0);+ EXPECT_STREQ(doc,+ "{\"schema\":1,\"result\":\"failed\",\"op\":\"upatch_load\","+ "\"phase\":\"parse\",\"errno\":22,\"errno_name\":\"EINVAL\","+ "\"category\":\"parser_rejected\","+ "\"message\":\"unsafe upatch name\",\"verifier_log\":\"\","+ "\"loader_return\":0,\"upatch\":{\"path\":\"a.upatch\","+ "\"name\":\"a\",\"payload_sha256\":\"\"},"+ "\"kernel\":{\"release\":\"7.4.0\"},\"tpm\":null}");+ xfree(doc, cap);+}++/*+ * The verifier log quotes source lines and file names as the compiler+ * took them, and the release is the kernel's to choose.+ */+static void text_that_is_no_utf8_is_written(void)+{+ struct op_report r = {+ .op = "upatch_load",+ .phase = "payload_load",+ .err = EACCES,+ .category = "verifier_rejected",+ .path = "caf\xe9.upatch",+ .name = "\xff",+ };+ size_t cap;+ char *doc;++ report_set_log(&r, "; return na\xefve; @ caf\xe9.bpf.c:7\n");+ ASSERT_TRUE(r.verifier_log);+ ASSERT_EQ(report_encode(&r, "7.4.0-\xfe", &doc, &cap), 0);+ EXPECT_TRUE(str_contains(doc, "\"verifier_log\":\"; return na" FFFD+ "ve; @ caf" FFFD ".bpf.c:7\\n\""));+ EXPECT_TRUE(str_contains(doc, "\"path\":\"caf" FFFD ".upatch\""));+ EXPECT_TRUE(str_contains(doc, "\"name\":\"" FFFD "\""));+ EXPECT_TRUE(str_contains(doc, "\"kernel\":{\"release\":\"7.4.0-" FFFD+ "\"}"));+ xfree(doc, cap);+ report_free(&r);+}++static const struct unit_case cases[] = {+ UNIT_CASE(a_refusal_is_one_document),+ UNIT_CASE(text_that_is_no_utf8_is_written),+ {}+};++UNIT_SUITE(report, cases);diff --git a/util/report.c b/util/report.cindex 66e7f7da..8e3a39a6 100644--- a/util/report.c+++ b/util/report.c@@ -59,7 +59,13 @@ int report_encode(const struct op_report *r, const char *kernel_release,json_kv_string(&e, "category", r->category ? r->category: (r->ok ? "ok" : "unknown"));json_kv_string(&e, "message", r->message);- json_kv_string(&e, "verifier_log", vlog);+ /*+ * The kernel quotes the program's source lines and the names of+ * its source files in the log as the compiler took them: a byte+ * that is no UTF-8 in one must not cost the report of the rejection+ * the log explains.+ */+ json_kv_string_lossy(&e, "verifier_log", vlog);json_kv_int(&e, "loader_return", r->loader_return);json_obj_open_key(&e, "upatch");@@ -74,7 +80,7 @@ int report_encode(const struct op_report *r, const char *kernel_release,json_obj_close(&e);json_obj_open_key(&e, "kernel");- json_kv_string(&e, "release", kernel_release);+ json_kv_string_lossy(&e, "release", kernel_release);json_obj_close(&e);if (r->ok && r->tpm_present) {