Skip to content

eBPF-based security platform

proofs: refuse a malformed option table and take the listings

Daniel Borkmann333cfdc1fcc8
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rst
index 4aff4d66..49d06649 100644
--- a/Documentation/contribute/testing/proof-coverage.rst
+++ b/Documentation/contribute/testing/proof-coverage.rst
@@ -1048,6 +1048,14 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.
- One unknown option of 64 to 66 bytes, around the 65 its copy
holds
- Refused, and a name suggested for it only when it fits the copy
+ * - ``options-table``
+ - ``specs_valid()``, ``opt_list()`` through ``opt_next()``
+ - Every table of up to two options built from names ``--a``,
+ ``--b``, ``--a=``, ``--``, ``-a`` and none, a short name or none,
+ every flag, a metavar or none, a summary or none, and a table
+ past ``OPT_MAX``; ``--list-opts``, ``--list-cmds`` and ``-h``
+ - A malformed table refused before any argument is read, a sound
+ one read; each listing printed and the run done
* - ``conditions-verdict``
- ``status_verdict()``, ``status_reason()``
- Every input; reasons into 1, 8, 32 and 160 bytes
diff --git a/testing/proofs/cbmc/options-next.c b/testing/proofs/cbmc/options-next.c
index 4fdf97c8..72ab0c08 100644
--- a/testing/proofs/cbmc/options-next.c
+++ b/testing/proofs/cbmc/options-next.c
@@ -8,8 +8,8 @@
* one argument, so a run ends, what it hands back lies in an argument, a
* flag has no value, a value is there and not empty unless the option
* allows it, and no option comes back twice unless it repeats.
- * options-long.c includes this file for an unknown option too long to
- * suggest one for.
+ * options-table.c includes this file for the table and the listings,
+ * options-long.c for an unknown option too long to suggest one for.
*/
#include <errno.h>
@@ -72,7 +72,82 @@ void help_suggest(const char *bad, const char *const *names, size_t n_names)
__CPROVER_assert(strlen(bad) <= SIMILAR_MAX_LEN, "the word fits");
}
-#if defined(OPTIONS_LONG)
+#if defined(OPTIONS_TABLE)
+/* A table of up to two options, each part one of a few that matter. */
+static const char *const names[] = {
+ "--a", "--b", "--a=", "--", "-a", NULL,
+};
+
+static void any_spec(struct opt_spec *s)
+{
+ size_t name;
+ bool shorted, metavar, summary;
+ __u32 flags;
+
+ __CPROVER_assume(name < ARRAY_SIZE(names));
+ *s = (struct opt_spec){
+ names[name], shorted ? 'a' : 0, flags & 7,
+ metavar ? "<v>" : NULL, summary ? "s" : NULL,
+ };
+}
+
+static bool spec_ok(const struct opt_spec *s)
+{
+ return s->name && s->name[0] == '-' && s->name[1] == '-' &&
+ s->name[2] && !strchr(s->name, '=') && s->summary &&
+ (s->metavar || !(s->flags & (OPT_EMPTY | OPT_OPTIONAL)));
+}
+
+static bool specs_apart(const struct opt_spec *a, const struct opt_spec *b)
+{
+ return strcmp(a->name, b->name) &&
+ (!a->short_name || a->short_name != b->short_name);
+}
+
+/* Any table: OPT_USAGE before any argument exactly when malformed. */
+static void any_table(void)
+{
+ static const char *const none[1];
+ struct opt_spec table[2];
+ struct opt_parser p;
+ size_t n;
+ bool ok;
+ int opt;
+
+ any_spec(&table[0]);
+ any_spec(&table[1]);
+ __CPROVER_assume(n <= 2 || n == OPT_MAX + 1);
+ ok = n <= OPT_MAX && (n < 1 || spec_ok(&table[0])) &&
+ (n < 2 || (spec_ok(&table[1]) &&
+ specs_apart(&table[1], &table[0])));
+ OPT_INIT(&p, "proof", table, NULL, 0, none, 0);
+ p.n_specs = n;
+ opt = opt_next(&p);
+ __CPROVER_assert(opt == (ok ? OPT_END : OPT_USAGE),
+ "a malformed table refused, a sound one read");
+}
+
+/* "--list-opts", "--list-cmds" and "-h" with a help: done, nothing else. */
+static void listings(void)
+{
+ static const char *const asked[] = { "--list-opts", "--list-cmds", "-h" };
+ static const struct help help = {};
+ struct opt_parser p;
+ size_t i;
+
+ for (i = 0; i < ARRAY_SIZE(asked); i++) {
+ OPT_INIT(&p, "proof", specs, &help, 1, &asked[i], 0);
+ __CPROVER_assert(opt_next(&p) == OPT_DONE && p.next == 1,
+ "a listing asked for, and the run done");
+ }
+}
+
+void harness(void)
+{
+ any_table();
+ listings();
+}
+#elif defined(OPTIONS_LONG)
/* One unknown option of LEN bytes, "--" and then a constant byte. */
void harness(void)
{
diff --git a/testing/proofs/cbmc/options-table.c b/testing/proofs/cbmc/options-table.c
new file mode 100644
index 00000000..b6b3d337
--- /dev/null
+++ b/testing/proofs/cbmc/options-table.c
@@ -0,0 +1,14 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * opt_next() of util/options.c on every table of up to two options, each
+ * part of a spec one of the few that decide: refused before any argument
+ * exactly when a name is missing, not "--" and more, holds '=', a summary
+ * is missing, a value-taking flag has no metavar, two options share a
+ * name or a short name, or the table is past OPT_MAX. And "--list-opts",
+ * "--list-cmds" and "-h" with a help: each a listing, and the run done.
+ */
+
+#define OPTIONS_TABLE 1
+#include "options-next.c"
diff --git a/testing/proofs/cbmc/proofs.list b/testing/proofs/cbmc/proofs.list
index aa72b3ab..9c760480 100644
--- a/testing/proofs/cbmc/proofs.list
+++ b/testing/proofs/cbmc/proofs.list
@@ -101,6 +101,7 @@ similar-suggest --object-bits 10 --max-field-sensitivity-array-size 65 --unwind
similar-resolve --object-bits 10 --unwind 8
options-next --object-bits 12 --unwind 8 LEN=0-6
options-long --object-bits 12 --unwind 8 --unwindset strcspn.1:67,strlen.0:67 LEN=64-66
+options-table --object-bits 12 --unwind 8 --unwindset strcmp.0:12
config-evidence-event --unwind 1 --unwindset harness.0:33,same_event.0:33,hex_digits.0:65,room_for.0:10,on_event.0:4,hex_decode.0:33,strlen.0:66 LEN=0,1,15,16,128,4095,4096
config-evidence-parse --unwind 1 --unwindset config_evidence_parse.0:7,on_event.0:4,replay.0:4,hex_decode.0:33,strlen.0:66,strcmp.0:15,named.0:7,json_parse.0:7,json_parse.1:4,give_event.0:4,give_text.0:66,any_document.0:4,whole.0:4,in_order.0:4,hex_digits.0:65,spells.0:33,blank.0:33,check_as_said.0:4,check_as_said.1:33 --memory-leak-check --slice-formula LEN=0-2
config-evidence-roundtrip --unwind 1 --unwindset config_evidence_emit.0:4,hex_encode.0:33,config_evidence_parse.0:7,on_event.0:4,replay.0:4,hex_decode.0:33,strlen.0:66,strcmp.0:15,named.0:7,json_parse.0:7,json_parse.1:4,give_event.0:4,give_text.0:66,json_kv_string.0:66,sequence_overflows.0:4,replays.0:4,same.0:33,blank.0:33,check_read_back.0:4 --memory-leak-check --slice-formula LEN=0-2