Skip to content

eBPF-based security platform

string: compare a prefix in either case, at run time, and a span

Daniel Borkmann9f37156a6297
diff --git a/.agents/skills/coding/SKILL.md b/.agents/skills/coding/SKILL.md
index 97bbd1e8..5015afb3 100644
--- a/.agents/skills/coding/SKILL.md
+++ b/.agents/skills/coding/SKILL.md
@@ -392,7 +392,7 @@ already, or should. The basics:
| Parse a number | `parse_u64()`, `parse_s64()`, `parse_x64()`, `digits()` (`<shirudo/string.h>`) |
| Copy or format a string | `strscpy()`, `strsprintf()`, `vstrsprintf()`, and check `-E2BIG` |
| A upatch's pin directory or a pin in it | `upatch_pin_path()` (`util/paths.h`) |
-| A string's prefix, or a span that must be one literal | `str_has_prefix()` to test a prefix, `str_skip_prefix()` to step past it, `span_equals()` for a span of known length (`<shirudo/string.h>`); never `strncmp()` with a length counted by hand |
+| A string's prefix, or a span that must be one string | `str_has_prefix()` to test a literal prefix and `str_skip_prefix()` to step past it, `str_after_prefix()` for the rest after a prefix known only at run time, each with a `_nocase` form for a letter in either case; `span_equals()` for a span of known length against a literal, `span_equals_str()` against a string, `span_has_prefix_nocase()` for a span's start (`<shirudo/string.h>`); never `strncmp()` or `strncasecmp()` with a length counted by hand |
| A file name's extension | `name_has_suffix()` (`<shirudo/string.h>`), `upatch_select()` for a scandir of upatches |
| Text from outside on a terminal | `text_sanitize()`; UTF-8 with `utf8_width()` |
| Bytes from outside as a string | `span_to_str()` (`<shirudo/string.h>`), which takes their length and refuses a NUL among them; `read_text_line()` for a line of a settings file (`util/io.h`) |
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rst
index d102406e..c54e9dcc 100644
--- a/Documentation/contribute/testing/proof-coverage.rst
+++ b/Documentation/contribute/testing/proof-coverage.rst
@@ -73,6 +73,15 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.
- Names up to 6 bytes, suffixes up to 3
- True exactly when the name's last bytes are the suffix and at
least one byte comes before them
+ * - ``string-prefix``
+ - ``str_after_prefix()``, ``str_has_prefix_nocase()``,
+ ``str_skip_prefix_nocase()``, ``span_equals_str()``,
+ ``span_has_prefix_nocase()``
+ - Strings and spans up to 5 bytes, prefixes up to 3
+ - The rest of the string exactly where it starts with the prefix,
+ in either case for the ``_nocase`` forms, and the empty prefix
+ starts every string; a span equals a string of its length and
+ bytes alone; nothing past a span's length is read
* - ``hex``
- ``hex_encode()``, ``hex_alloc()``, ``hex_decode()``,
``hex_decode_max()``
diff --git a/include/shirudo/string.h b/include/shirudo/string.h
index a53e53b1..300043ec 100644
--- a/include/shirudo/string.h
+++ b/include/shirudo/string.h
@@ -4,8 +4,8 @@
*
* Bounded number parsing, string copies and formatting, file name
* suffixes, DNS names, UTF-8 characters and text for a terminal, from
- * util/string.c, compares with a string literal, and the scrub and compare
- * for secrets.
+ * util/string.c, the compares of a prefix and of a span, and the scrub
+ * and compare for secrets.
*/
#ifndef SHIRUDO_STRING_H
@@ -18,6 +18,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <strings.h>
#include <sys/types.h>
#include <shirudo/misc.h>
@@ -77,9 +78,12 @@ bool name_has_suffix(const char *name, size_t len, const char *suffix);
* The length of prefix where s starts with it, else 0. prefix is a string
* literal, whose length the compiler takes from its size: anything else
* fails the build, so that the size of a pointer never stands in for it.
+ * The _nocase form takes a letter in either case.
*/
#define str_has_prefix(s, prefix) \
(strncmp(s, "" prefix, sizeof(prefix) - 1) ? 0 : sizeof(prefix) - 1)
+#define str_has_prefix_nocase(s, prefix) \
+ (strncasecmp(s, "" prefix, sizeof(prefix) - 1) ? 0 : sizeof(prefix) - 1)
/*
* Whether *sp starts with the string literal prefix; where it does, *sp
@@ -93,6 +97,33 @@ bool name_has_suffix(const char *name, size_t len, const char *suffix);
*__sp += __n; \
__n != 0; \
})
+#define str_skip_prefix_nocase(sp, prefix) ({ \
+ __auto_type __sp = (sp); \
+ size_t __n = str_has_prefix_nocase(*__sp, prefix); \
+ \
+ *__sp += __n; \
+ __n != 0; \
+})
+
+/*
+ * Where s goes on after prefix, or NULL where it does not start with it.
+ * prefix is any string, the empty one included, which every string starts
+ * with. The _nocase form takes a letter in either case.
+ */
+static inline const char *str_after_prefix(const char *s, const char *prefix)
+{
+ size_t n = strlen(prefix);
+
+ return strncmp(s, prefix, n) ? NULL : s + n;
+}
+
+static inline const char *str_after_prefix_nocase(const char *s,
+ const char *prefix)
+{
+ size_t n = strlen(prefix);
+
+ return strncasecmp(s, prefix, n) ? NULL : s + n;
+}
/*
* Whether the len bytes at s are the string literal lit, no more and no
@@ -101,6 +132,21 @@ bool name_has_suffix(const char *name, size_t len, const char *suffix);
#define span_equals(s, len, lit) \
((len) == sizeof(lit) - 1 && !strncmp(s, "" lit, sizeof(lit) - 1))
+/* The same for a string that is no literal. */
+static inline bool span_equals_str(const char *s, size_t len, const char *str)
+{
+ return strlen(str) == len && !strncmp(s, str, len);
+}
+
+/*
+ * The length of the string literal prefix where the len bytes at s, which
+ * need no terminator, start with it in either case, else 0.
+ */
+#define span_has_prefix_nocase(s, len, prefix) \
+ (((len) >= sizeof(prefix) - 1 && \
+ !strncasecmp(s, "" prefix, sizeof(prefix) - 1)) ? \
+ sizeof(prefix) - 1 : 0)
+
/*
* A DNS host name: labels of letters, digits and inner hyphens, 1 to 63
* bytes each and 253 in all. A name whose labels are all decimal or 0x
diff --git a/testing/coccinelle/option_by_hand.cocci b/testing/coccinelle/option_by_hand.cocci
index 355190af..3a8c6d80 100644
--- a/testing/coccinelle/option_by_hand.cocci
+++ b/testing/coccinelle/option_by_hand.cocci
@@ -12,7 +12,10 @@ attribute name SEC;
expression E, F;
constant char[] S;
identifier f = {strcmp, strncmp, strcasecmp, strncasecmp, str_has_prefix,
- str_skip_prefix};
+ str_skip_prefix, str_has_prefix_nocase,
+ str_skip_prefix_nocase, str_after_prefix,
+ str_after_prefix_nocase};
+identifier g = {span_equals, span_equals_str, span_has_prefix_nocase};
position p;
@@
(
@@ -20,7 +23,7 @@ position p;
|
f@p(S, E, ...)
|
- span_equals@p(E, F, S)
+ g@p(E, F, S)
)
@script:python depends on report@
diff --git a/testing/proofs/cbmc/proofs.list b/testing/proofs/cbmc/proofs.list
index c9820600..96840920 100644
--- a/testing/proofs/cbmc/proofs.list
+++ b/testing/proofs/cbmc/proofs.list
@@ -5,6 +5,7 @@ string-utf8 --unwind 10
string-copy --unwind 10
string-span --unwind 9
string-suffix --unwind 8
+string-prefix --unwind 8
string-sanitize --unwind 8 LEN=0-6
hex --unwind 12
io-lines --unwind 10
diff --git a/testing/proofs/cbmc/string-prefix.c b/testing/proofs/cbmc/string-prefix.c
new file mode 100644
index 00000000..034a3890
--- /dev/null
+++ b/testing/proofs/cbmc/string-prefix.c
@@ -0,0 +1,102 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * The prefix and span compares of <shirudo/string.h> on every string of
+ * up to LEN_MAX bytes and every prefix of up to PREFIX_MAX:
+ * str_after_prefix() gives the rest exactly where the string starts with
+ * the prefix, in either case for the _nocase forms, span_equals_str() is
+ * true exactly for the string's own bytes, and the span forms read the
+ * span's len bytes alone.
+ */
+
+#include <stdbool.h>
+
+#include "proof.h"
+#include "../../../util/string.c"
+
+#define LEN_MAX 5
+#define PREFIX_MAX 3
+
+static char *any_string(size_t len)
+{
+ char *s = any_bytes(len + 1);
+
+ __CPROVER_assume(!s[len] && strnlen(s, len) == len);
+ return s;
+}
+
+static char lower(char c)
+{
+ return c >= 'A' && c <= 'Z' ? (char)(c - 'A' + 'a') : c;
+}
+
+/* Whether the n bytes at a and b are the same, or the same letters. */
+static bool same(const char *a, const char *b, size_t n, bool nocase)
+{
+ size_t i;
+
+ for (i = 0; i < n; i++)
+ if (nocase ? lower(a[i]) != lower(b[i]) : a[i] != b[i])
+ return false;
+ return true;
+}
+
+static void rest_after_a_prefix(void)
+{
+ const char *s, *prefix, *rest;
+ size_t len, n;
+
+ __CPROVER_assume(len <= LEN_MAX && n <= PREFIX_MAX);
+ s = any_string(len);
+ prefix = any_string(n);
+ rest = str_after_prefix(s, prefix);
+ __CPROVER_assert(rest == (n <= len && same(s, prefix, n, false) ?
+ s + n : NULL),
+ "the rest where the string starts with the prefix");
+ rest = str_after_prefix_nocase(s, prefix);
+ __CPROVER_assert(rest == (n <= len && same(s, prefix, n, true) ?
+ s + n : NULL),
+ "the rest where it does in either case");
+}
+
+static void literal_prefix_in_either_case(void)
+{
+ const char *s, *p;
+ size_t len, n;
+ bool starts;
+
+ __CPROVER_assume(len <= LEN_MAX);
+ s = any_string(len);
+ starts = len >= 3 && same(s, "a-z", 3, true);
+ n = str_has_prefix_nocase(s, "A-z");
+ __CPROVER_assert(n == (starts ? 3 : 0), "the literal's length, or 0");
+ p = s;
+ __CPROVER_assert(str_skip_prefix_nocase(&p, "a-Z") == starts &&
+ p == s + n, "past the prefix on a match alone");
+}
+
+static void span_of_exactly_its_bytes(void)
+{
+ size_t len, n, has;
+ const char *str;
+ char *span;
+
+ __CPROVER_assume(len <= LEN_MAX && n <= PREFIX_MAX);
+ span = any_bytes(len);
+ str = any_string(n);
+ __CPROVER_assert(span_equals_str(span, len, str) ==
+ (len == n && same(span, str, n, false)),
+ "the span is the string, no more and no fewer");
+ has = span_has_prefix_nocase(span, len, "a-z");
+ __CPROVER_assert(has == (len >= 3 && same(span, "A-Z", 3, true) ?
+ 3 : 0),
+ "the literal's length where the span starts with it");
+}
+
+void harness(void)
+{
+ rest_after_a_prefix();
+ literal_prefix_in_either_case();
+ span_of_exactly_its_bytes();
+}
diff --git a/testing/unit/string.c b/testing/unit/string.c
index dd29e531..df7360fe 100644
--- a/testing/unit/string.c
+++ b/testing/unit/string.c
@@ -582,6 +582,87 @@ static void span_to_str_takes_a_buffer_in_place(void)
EXPECT_EQ(span_to_str(text, 4, text, 4), -E2BIG);
}
+static void nocase_prefix_takes_a_letter_in_either_case(void)
+{
+ const char *p = "BEARER realm=\"x\"";
+
+ EXPECT_UEQ(str_has_prefix_nocase("Location: /v2/", "location:"), 9);
+ EXPECT_UEQ(str_has_prefix_nocase("LOCATION:", "Location:"), 9);
+ EXPECT_UEQ(str_has_prefix_nocase("Locatio", "Location:"), 0);
+ EXPECT_UEQ(str_has_prefix_nocase("Location;", "Location:"), 0);
+ EXPECT_UEQ(str_has_prefix_nocase("", "Location:"), 0);
+ EXPECT_FALSE(str_skip_prefix_nocase(&p, "Basic"));
+ EXPECT_STREQ(p, "BEARER realm=\"x\"");
+ EXPECT_TRUE(str_skip_prefix_nocase(&p, "Bearer"));
+ EXPECT_STREQ(p, " realm=\"x\"");
+}
+
+static void str_after_prefix_gives_the_rest(void)
+{
+ const char *path = "/etc/shirudo/keys", *dir = "/etc/shirudo";
+
+ EXPECT_TRUE(str_after_prefix(path, dir) == path + 12);
+ EXPECT_STREQ(str_after_prefix(dir, dir), "");
+ EXPECT_FALSE(str_after_prefix(dir, path));
+ EXPECT_FALSE(str_after_prefix("/etc/shirudp/keys", dir));
+ EXPECT_FALSE(str_after_prefix("/ETC/shirudo", dir));
+ EXPECT_FALSE(str_after_prefix("", dir));
+}
+
+static void every_string_starts_with_the_empty_one(void)
+{
+ const char *s = "x", *none = "";
+
+ EXPECT_TRUE(str_after_prefix(s, none) == s);
+ EXPECT_TRUE(str_after_prefix(none, none) == none);
+ EXPECT_TRUE(str_after_prefix_nocase(s, none) == s);
+}
+
+static void str_after_prefix_nocase_takes_either_case(void)
+{
+ const char *name = "ABCDEF01.der", *hex = "abcdef01";
+
+ EXPECT_TRUE(str_after_prefix_nocase(name, hex) == name + 8);
+ EXPECT_FALSE(str_after_prefix(name, hex));
+ EXPECT_FALSE(str_after_prefix_nocase("ABCDEF0", hex));
+ EXPECT_FALSE(str_after_prefix_nocase("ABCDEF02.der", hex));
+}
+
+static void span_equals_str_wants_the_whole_string(void)
+{
+ const char *arg = "--name=value", *name = "--name";
+ char *exact = span("json", 4);
+
+ EXPECT_TRUE(span_equals_str(arg, 6, name));
+ EXPECT_FALSE(span_equals_str(arg, 5, name));
+ EXPECT_FALSE(span_equals_str(arg, 7, name));
+ EXPECT_FALSE(span_equals_str(arg, 6, "--nome"));
+ EXPECT_FALSE(span_equals_str(arg, 6, "--NAME"));
+ EXPECT_TRUE(span_equals_str(arg, 0, ""));
+ EXPECT_FALSE(span_equals_str(arg, 0, name));
+ EXPECT_TRUE(span_equals_str(exact, 4, "json"));
+ EXPECT_FALSE(span_equals_str(exact, 4, "jsonl"));
+ EXPECT_FALSE(span_equals_str(exact, 4, "jso"));
+ xfree(exact, 4);
+}
+
+static void span_prefix_reads_no_more_than_the_span(void)
+{
+ char *line = span("retry-after: 3", 14), *cut = span("Retry-Afte", 10);
+ char *exact = span("RETRY-AFTER:", 12);
+
+ EXPECT_UEQ(span_has_prefix_nocase(line, 14, "Retry-After:"), 12);
+ EXPECT_UEQ(span_has_prefix_nocase(line, 12, "Retry-After:"), 12);
+ EXPECT_UEQ(span_has_prefix_nocase(line, 11, "Retry-After:"), 0);
+ EXPECT_UEQ(span_has_prefix_nocase(line, 14, "Retry-Later:"), 0);
+ EXPECT_UEQ(span_has_prefix_nocase(cut, 10, "Retry-After:"), 0);
+ EXPECT_UEQ(span_has_prefix_nocase(cut, 0, "Retry-After:"), 0);
+ EXPECT_UEQ(span_has_prefix_nocase(exact, 12, "Retry-After:"), 12);
+ xfree(line, 14);
+ xfree(cut, 10);
+ xfree(exact, 12);
+}
+
static const struct unit_case cases[] = {
UNIT_CASE(digits_counts_leading_digits),
UNIT_CASE(decimal_fills_its_span),
@@ -612,6 +693,12 @@ static const struct unit_case cases[] = {
UNIT_CASE(span_to_str_refuses_a_nul_among_the_bytes),
UNIT_CASE(span_to_str_wants_room_for_the_terminator),
UNIT_CASE(span_to_str_takes_a_buffer_in_place),
+ UNIT_CASE(nocase_prefix_takes_a_letter_in_either_case),
+ UNIT_CASE(str_after_prefix_gives_the_rest),
+ UNIT_CASE(every_string_starts_with_the_empty_one),
+ UNIT_CASE(str_after_prefix_nocase_takes_either_case),
+ UNIT_CASE(span_equals_str_wants_the_whole_string),
+ UNIT_CASE(span_prefix_reads_no_more_than_the_span),
{}
};