eBPF-based security platform
proofs: hold each bpm source and tree setting to the text after its '='
Daniel Borkmann50e1c6f74751
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rstindex ef9b0615..6d7eb1c3 100644--- a/Documentation/contribute/testing/proof-coverage.rst+++ b/Documentation/contribute/testing/proof-coverage.rst@@ -206,9 +206,12 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.hex in either case* - ``bpm-source-config``- ``bpm_parse_source_line()``, ``bpm_parse_tree_line()``- - Lines up to 12 bytes- - A setting written only for its exact key and terminated, without- CR or LF+ - Lines up to 22 bytes, settings of 4 bytes with their NUL+ - A setting written only for its exact key, holding the text after+ the first ``=``: a source one without the blanks before it, cut+ at CR or LF, a tree one without the blanks around it or its+ quotes; ``-E2BIG`` exactly when it does not fit, ``-EINVAL`` for+ an open quote* - ``bpm-state-entry``- ``on_state_entry()`` of :src:`util/bpm_state.c`- Any entry with a name and version of up to 6 bytes, into an emptydiff --git a/testing/proofs/cbmc/bpm-source-config.c b/testing/proofs/cbmc/bpm-source-config.cindex f938c129..f46b5f1d 100644--- a/testing/proofs/cbmc/bpm-source-config.c+++ b/testing/proofs/cbmc/bpm-source-config.c@@ -4,8 +4,9 @@** The bpm source and tree config lines util/bpm_source_config.c reads, for* every line of up to LINE_MAX bytes: a setting is written only for its own- * key and terminated inside its buffer, a source one led by no blank and- * without a line break, a tree one unquoted without blanks around it.+ * key and holds the text after the first '=', a source one without the+ * blanks before it and cut at a line break, a tree one without the blanks+ * around it or the quotes that enclose it; one that does not fit is refused.*//* util/string.c is included behind a file that includes its header. */@@ -15,7 +16,8 @@#include "../../../util/bpm_source_config.c"#include "../../../util/string.c"-#define LINE_MAX 12+/* "index_repository=" and a value of up to 5 bytes, one past OUT. */+#define LINE_MAX 22#define OUT 4static bool blank(char c)@@ -23,32 +25,45 @@ static bool blank(char c)return c == ' ' || (c >= '\t' && c <= '\r');}-static bool has(const char *s, char c)+/* Where a setting's text lies in the line: from, and up to before to. */+struct span {+ size_t from, to;+};++/* After the first '=', without the blanks before it, up to CR or LF. */+static struct span source_value(const char *line, size_t eq){- return memchr(s, c, strnlen(s, OUT));+ struct span v = { eq + 1, 0 };++ while (blank(line[v.from]))+ v.from++;+ for (v.to = v.from; line[v.to] && line[v.to] != '\r' &&+ line[v.to] != '\n'; v.to++)+ ;+ return v;}-/* Written for its key alone, and then a terminated value. */-static bool setting(const char *out, const char *was, bool mine, bool lead,- bool trail)+/* After the first '=', without the blanks around it. */+static struct span tree_value(const char *line, size_t eq){- size_t n;+ struct span v = { eq + 1, strlen(line) };- if (!mine)- return !memcmp(out, was, OUT);- n = strnlen(out, OUT);- return n < OUT && (!n || ((!lead || !blank(out[0])) &&- (!trail || !blank(out[n - 1]))));+ while (v.from < v.to && blank(line[v.from]))+ v.from++;+ while (v.to > v.from && blank(line[v.to - 1]))+ v.to--;+ return v;}-/* A value that starts with a quote, which keeps the blanks inside it. */-static bool quoted(const char *eq)+/* Written for its key alone, and then the text of the span, terminated. */+static bool setting(const char *out, const char *was, bool mine,+ const char *line, struct span v){- if (!eq)- return false;- for (eq++; blank(*eq); eq++)- ;- return *eq == '"';+ size_t n = v.to - v.from;++ if (!mine)+ return !memcmp(out, was, OUT);+ return n < OUT && !memcmp(out, line + v.from, n) && !out[n];}/* The key a parsed line leaves at its start, the blanks around it gone. */@@ -59,60 +74,89 @@ static bool key_is(const char *line, const char *eq, const char *name)return eq && !strcmp(line, name);}-static char *any_line(void)+static char *any_line(char *was){char *line = any_bytes(LINE_MAX + 1);__CPROVER_assume(!line[LINE_MAX]);+ memcpy(was, line, LINE_MAX + 1);return line;}static void source_line(void){char endpoint[OUT], repo[OUT], key[OUT], e[OUT], r[OUT], k[OUT];- char *line = any_line(), *eq = strchr(line, '=');+ char was[LINE_MAX + 1], *line = any_line(was);+ char *eq = strchr(line, '=');bool is_e, is_r, is_k;+ struct span v;+ int ret;memcpy(e, endpoint, OUT);memcpy(r, repo, OUT);memcpy(k, key, OUT);- if (bpm_parse_source_line(line, endpoint, OUT, repo, OUT, key, OUT))- return;+ ret = bpm_parse_source_line(line, endpoint, OUT, repo, OUT, key, OUT);is_e = key_is(line, eq, "endpoint");is_r = key_is(line, eq, "index_repository");is_k = key_is(line, eq, "key_file");- __CPROVER_assert(setting(endpoint, e, is_e, true, false) &&- setting(repo, r, is_r, true, false) &&- setting(key, k, is_k, true, false),- "a setting for its key, led by no blank");- __CPROVER_assert((!is_e || (!has(endpoint, '\r') &&- !has(endpoint, '\n'))) &&- (!is_r || (!has(repo, '\r') && !has(repo, '\n'))) &&- (!is_k || (!has(key, '\r') && !has(key, '\n'))),- "no line break in a setting");+ if (!eq) {+ __CPROVER_assert(!ret && !memcmp(endpoint, e, OUT) &&+ !memcmp(repo, r, OUT) && !memcmp(key, k, OUT),+ "a line without '=' sets nothing");+ return;+ }+ v = source_value(was, (size_t)(eq - line));+ __CPROVER_assert(ret == ((is_e || is_r || is_k) &&+ v.to - v.from >= OUT ? -E2BIG : 0),+ "refused exactly when the value does not fit");+ if (ret)+ return;+ __CPROVER_assert(setting(endpoint, e, is_e, was, v) &&+ setting(repo, r, is_r, was, v) &&+ setting(key, k, is_k, was, v),+ "a setting for its key, the text after '=' up to a "+ "line break, without blanks before it");}static void tree_line(void){char endpoint[OUT], reference[OUT], e[OUT], r[OUT];- char *line = any_line(), *eq = strchr(line, '=');- bool is_e, is_r, bare = !quoted(eq);+ char was[LINE_MAX + 1], *line = any_line(was);+ char *eq = strchr(line, '=');+ bool is_e, is_r, quoted;+ struct span v;int ret;memcpy(e, endpoint, OUT);memcpy(r, reference, OUT);ret = bpm_parse_tree_line(line, endpoint, OUT, reference, OUT);- if (ret == -EINVAL) {- __CPROVER_assert(eq, "a refused line has a value");+ is_e = key_is(line, eq, "endpoint");+ is_r = key_is(line, eq, "reference");+ if (!eq) {+ __CPROVER_assert(!ret && !memcmp(endpoint, e, OUT) &&+ !memcmp(reference, r, OUT),+ "a line without '=' sets nothing");return;}+ v = tree_value(was, (size_t)(eq - line));+ quoted = v.to > v.from && was[v.from] == '"';+ if (quoted && (v.to - v.from < 2 || was[v.to - 1] != '"')) {+ __CPROVER_assert(ret == -EINVAL, "an open quote refused");+ return;+ }+ if (quoted) {+ v.from++;+ v.to--;+ }+ __CPROVER_assert(ret == ((is_e || is_r) && v.to - v.from >= OUT ?+ -E2BIG : 0),+ "refused exactly when the value does not fit");if (ret)return;- is_e = key_is(line, eq, "endpoint");- is_r = key_is(line, eq, "reference");- __CPROVER_assert(setting(endpoint, e, is_e, bare, bare) &&- setting(reference, r, is_r, bare, bare),- "a setting for its key, unquoted without blanks around");+ __CPROVER_assert(setting(endpoint, e, is_e, was, v) &&+ setting(reference, r, is_r, was, v),+ "a setting for its key, the text after '=' without "+ "the blanks around it or its quotes");}void harness(void)diff --git a/testing/proofs/cbmc/proofs.list b/testing/proofs/cbmc/proofs.listindex a4fb68f3..1e734d6e 100644--- a/testing/proofs/cbmc/proofs.list+++ b/testing/proofs/cbmc/proofs.list@@ -82,7 +82,7 @@ bpm-oci-descriptor --unwind 130 LEN=0,1bpm-names --unwind 131bpm-tree-selector --unwind 10 --malloc-may-fail --malloc-fail-null LEN=0,1bpm-tree-reader --unwind 6 --unwindset hex_digits.0:65,hex_decode.0:33,strlen.0:129,strncmp.0:8 --malloc-may-fail --malloc-fail-null LEN=0,1-bpm-source-config --unwind 14+bpm-source-config --unwind 24bpm-oci-strings --unwind 9bpm-semver-model --slice-formula --unwind 1 --unwindset strlen.0:10,strcmp.0:10,strcspn.0:2,strcspn.1:4,digits.0:10,parse_base.0:10,parse_base.1:10,semver_parse.0:4,semver_identifiers.0:4,semver_identifiers.1:3,prerelease_compare.0:3,memchr.0:4,memcmp.0:4,bpm_semver_compare.0:4,strnlen.0:10,any_string.0:10,break_at.0:10,split_on.0:10,next_ident.0:4,numeral.0:8,numeric.0:8,ident_ok.0:4,identifiers.0:3,lex_bytes.0:10,memcmp_model.0:4,lex_idents.0:3,semver_model_compare.0:4 LEN=9chain-events --unwind 258 LEN=0-8,255-257