eBPF-based security platform
proofs: prove the image identity parsers of sysinfo
Daniel Borkmann783b877647bd
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rstindex 265ad05a..1ebb3f87 100644--- a/Documentation/contribute/testing/proof-coverage.rst+++ b/Documentation/contribute/testing/proof-coverage.rst@@ -984,6 +984,17 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.first highest, the last byte padded with zeros. Each condition'spair at the place of its type name, from the first of that type,``00`` for a missing one or an unknown status, none past seven+ * - ``sysinfo-image``+ - ``os_release_unquote()``, ``os_release_value()``,+ ``cmdline_image_root()``+ - Values and texts of 0 to 6 bytes; values of 255, 256 and 257+ bytes; command lines of a word, ``usrhash=`` and ``roothash=``,+ each there or not, with hashes of any 64 bytes but blanks+ - A value taken exactly when one or more printable bytes remain+ after one pair of quotes and the escapes of double quotes, at+ most 255 of them; of a key's lines the last; the root+ ``roothash=`` names, else ``usrhash=``, in lowercase when it is+ 64 hex digits, else noneLean 4 proofs-------------@@ -1253,7 +1264,7 @@ a proof covers.:src:`util/image_guards.c`, :src:`util/image_keys.c`,:src:`util/image_runtime.c`, :src:`util/initrd.c`,:src:`util/keyring.c`, :src:`util/preflight.c`,- :src:`util/seal.c`, :src:`util/sysinfo.c`, :src:`util/tamper.c`,+ :src:`util/seal.c`, :src:`util/tamper.c`,:src:`util/upatch_maps.c`, :src:`util/upatch_pins.c`,:src:`util/upatch_resolve.c`, :src:`util/upatch_verify.c`,:src:`util/xdp.c`@@ -1324,7 +1335,10 @@ of :src:`util/upatch_maps.c` that puts a page together from the chunks``serial_chunk_validate()`` accepted. ``edit_distance()`` of:src:`util/similar.c` is proven on strings of up to 3 bytes: at itslimit of 64 CBMC ends a run on the code as it is and none on the code-broken, so no proof of the limit is kept.+broken, so no proof of the limit is kept. Of :src:`util/sysinfo.c` the+proofs cover the image identity's parsers; reading the files they parse,+and the readers of ``/proc`` and the root file system, go to the kernel+and have none.Within the operator's package builder and publisher, the proofs cover thetar header, the member names, the Beefile's values, lines, names anddiff --git a/testing/proofs/cbmc/proofs.list b/testing/proofs/cbmc/proofs.listindex c7560bc7..286a3075 100644--- a/testing/proofs/cbmc/proofs.list+++ b/testing/proofs/cbmc/proofs.list@@ -136,3 +136,4 @@ bpm-tree-state-pending --unwind 5 --unwindset upatch_name_valid.0:10,strnlen.0:7bpm-tree-state-match --unwind 4 --unwindset read_config_stream.0:7,read_config_stream.1:9,read_config_stream.2:9,read_config_stream.3:9,read_config_stream.4:9,read_text_line.0:9,strlen.0:9,strchr.0:9,strnlen.0:9,memcpy.0:9,memcmp.0:26 LEN=0-3bpm-tree-state-options --unwind 4keepalive-pack --unwind 9 --unwindset strcmp.0:18,keepalive_mask.0:514,mask.0:514,mask.1:514 LEN=0-9,512,513+sysinfo-image --unwind 8 --unwindset strcmp.0:8,memcmp.0:10,os_release_unquote.0:258,memset.0:259,memcpy.0:66,cmdline_image_root.0:8,cmdline_image_root.1:76,cmdline_image_root.2:5,strlen.0:11,all_hex.0:65,image_root_normalise.0:65,command_line.0:65,command_line.1:65 LEN=0-6diff --git a/testing/proofs/cbmc/sysinfo-image.c b/testing/proofs/cbmc/sysinfo-image.cnew file mode 100644index 00000000..bb913933--- /dev/null+++ b/testing/proofs/cbmc/sysinfo-image.c@@ -0,0 +1,165 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The image identity parsers of util/sysinfo.c, whose values a keepalive+ * carries: an os-release value of LEN bytes unquoted, the last assignment+ * of a key in a text of LEN bytes, and the root a command line names.+ */++#include "proof.h"+#include "../../../util/sysinfo.c"++#define HASH_LEN (SYSINFO_IMAGE_ROOT_MAX - 1)++static bool printable(char c)+{+ return (__u8)c >= 0x20 && (__u8)c <= 0x7e;+}++static bool escapable(char c)+{+ return c == '"' || c == '\\' || c == '$' || c == '`';+}++/* os-release(5) on short values: one quote pair, escapes in double quotes. */+static void unquotes(void)+{+ char value[LEN ? LEN : 1], out[SYSINFO_IMAGE_VALUE_MAX];+ char want[LEN ? LEN : 1];+ size_t start = 0, end = LEN, n = 0;+ bool dq = false, ok = true;+ int ret;++ if (LEN >= 2 && value[0] == value[LEN - 1] &&+ (value[0] == '"' || value[0] == '\'')) {+ dq = value[0] == '"';+ start = 1;+ end = LEN - 1;+ }+ for (size_t i = start; i < end; i++) {+ char c = value[i];++ if (dq && c == '\\' && i + 1 < end && escapable(value[i + 1]))+ c = value[++i];+ ok = ok && printable(c);+ want[n++] = c;+ }+ ret = os_release_unquote(value, LEN, out);+ __CPROVER_assert(!ret == (ok && n > 0),+ "taken exactly when 1 or more printable bytes remain");+ if (ret)+ return;+ __CPROVER_assert(!out[n], "terminated after the value");+ for (size_t i = 0; i < n; i++)+ __CPROVER_assert(out[i] == want[i], "the unquoted value");+}++/* The 255 bytes a value holds, and no more. */+static void bounds(void)+{+ static char value[SYSINFO_IMAGE_VALUE_MAX + 2];+ char out[SYSINFO_IMAGE_VALUE_MAX];++ memset(value, 'a', sizeof(value));+ __CPROVER_assert(!os_release_unquote(value, SYSINFO_IMAGE_VALUE_MAX - 1,+ out) &&+ !out[SYSINFO_IMAGE_VALUE_MAX - 1],+ "255 bytes taken");+ __CPROVER_assert(os_release_unquote(value, SYSINFO_IMAGE_VALUE_MAX,+ out) == -EINVAL,+ "256 bytes refused");+ value[0] = '"';+ value[SYSINFO_IMAGE_VALUE_MAX] = '"';+ __CPROVER_assert(!os_release_unquote(value, SYSINFO_IMAGE_VALUE_MAX + 1,+ out),+ "255 bytes in quotes taken");+}++/* Of the lines "K=..." of a text the last one counts, or none. */+static void last_assignment(void)+{+ char text[LEN ? LEN : 1], out[SYSINFO_IMAGE_VALUE_MAX];+ char want[SYSINFO_IMAGE_VALUE_MAX] = "";+ const char *value = NULL;+ size_t value_len = 0, line = 0, i;++ for (i = 0; i <= LEN; i++) {+ if (i < LEN && text[i] != '\n')+ continue;+ if (i - line >= 2 && text[line] == 'K' &&+ text[line + 1] == '=') {+ value = text + line + 2;+ value_len = i - line - 2;+ }+ line = i + 1;+ }+ if (value && os_release_unquote(value, value_len, want))+ want[0] = '\0';+ os_release_value(text, LEN, "K", out);+ __CPROVER_assert(!strcmp(out, want), "the last assignment's value");+}++static bool all_hex(const char *h)+{+ for (size_t i = 0; i < HASH_LEN; i++)+ if (!isxdigit((__u8)h[i]))+ return false;+ return true;+}++static size_t put(char *dst, size_t at, const char *src, size_t n)+{+ memcpy(dst + at, src, n);+ return at + n;+}++/*+ * roothash= wins over usrhash= whether or not it is a hash, and the root+ * is the one it names in lowercase, or none. A value holds no separator,+ * so that the arguments are those the line was built from.+ */+static void command_line(void)+{+ char root[HASH_LEN], usr[HASH_LEN], out[SYSINFO_IMAGE_ROOT_MAX];+ char line[2 + 8 + HASH_LEN + 1 + 9 + HASH_LEN];+ bool junk, has_usr, has_root;+ const char *named;+ size_t at = 0;++ for (size_t i = 0; i < HASH_LEN; i++)+ __CPROVER_assume(!cmdline_space(root[i]) &&+ !cmdline_space(usr[i]));++ if (junk)+ at = put(line, at, "x ", 2);+ if (has_usr) {+ at = put(line, at, "usrhash=", 8);+ at = put(line, at, usr, HASH_LEN);+ at = put(line, at, " ", 1);+ }+ if (has_root) {+ at = put(line, at, "roothash=", 9);+ at = put(line, at, root, HASH_LEN);+ }+ cmdline_image_root(line, at, out);+ named = has_root ? root : has_usr ? usr : NULL;+ if (!named || !all_hex(named)) {+ __CPROVER_assert(!out[0], "no root");+ return;+ }+ __CPROVER_assert(!out[HASH_LEN], "64 digits");+ for (size_t i = 0; i < HASH_LEN; i++)+ __CPROVER_assert(out[i] == tolower((__u8)named[i]),+ "the named root in lowercase");+}++void harness(void)+{+ unquotes();+ last_assignment();+ if (!LEN) {+ bounds();+ command_line();+ }+}