Skip to content

eBPF-based security platform

string: search a span, and compare two strings in either case

Daniel Borkmanneaee3aa52b3f
diff --git a/.agents/skills/coding/SKILL.md b/.agents/skills/coding/SKILL.md
index 7cbe44b2..976f1247 100644
--- a/.agents/skills/coding/SKILL.md
+++ b/.agents/skills/coding/SKILL.md
@@ -392,7 +392,8 @@ 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 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>`); `strncmp()` and `strncasecmp()`, whose length a caller counts by hand, are poisoned |
+| 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; `str_equals_nocase()` for two whole strings in either case (`<shirudo/string.h>`); `strncmp()` and `strncasecmp()`, whose length a caller counts by hand, are poisoned |
+| A byte of a set, or a string, in the bytes you hold | `span_until()` for the length up to the first byte of a set, `span_find()` for where a string lies in a span, `str_contains()` for whether a string holds another (`<shirudo/string.h>`) |
| The lines of a buffer, the words of a line, the entries of a list | `for_each_token()` (`<shirudo/string.h>`), which gives each token as pointer and length and writes nothing; a caller that needs a terminated token ends it in a buffer of its own; `strtok_r()`, `strtok()` and `strsep()` are poisoned |
| 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()` |
diff --git a/AGENTS.md b/AGENTS.md
index a6ddcbe6..3282a00f 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -117,8 +117,9 @@ commit message that carries a change.
code: `parse_u64()` and its kin for numbers, `<shirudo/overflow.h>`
for sizes, `strscpy()` and `strsprintf()` with their `-E2BIG`
checked, `str_has_prefix()`, `str_after_prefix()` and `span_equals()`
- for a prefix or a span, `for_each_token()` for the tokens of a
- buffer, the `xmalloc()` family, `min()` and `max()`,
+ for a prefix or a span, `span_until()`, `span_find()` and
+ `str_contains()` for a search, `for_each_token()` for the tokens of
+ a buffer, the `xmalloc()` family, `min()` and `max()`,
`memzero_explicit()` and `memneq_explicit()` for secrets,
`util/json.c` for all JSON and `pr_*()` for all logging. The libc names
they replace are poisoned. Code that a second place needs moves into
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rst
index 08b514eb..d7741b19 100644
--- a/Documentation/contribute/testing/proof-coverage.rst
+++ b/Documentation/contribute/testing/proof-coverage.rst
@@ -89,6 +89,16 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.
the span's end on either side; the cursor ends past it and past
one delimiter; a walk gives the bytes that are no delimiter in
order, each once, and no empty token
+ * - ``string-search``
+ - ``span_until()``, ``span_find()``, ``str_contains()``,
+ ``str_equals_nocase()``
+ - Lengths 0 to 5, one or two bytes in the set, needles up to 2
+ bytes, strings up to 3
+ - The length of what comes before the first byte of the set; the
+ first place the needle lies in the span, or ``NULL`` exactly
+ where it lies nowhere; nothing past the span is read; a string
+ holds a needle where it lies in it; two strings are equal in
+ either case exactly with the same letters and length
* - ``hex``
- ``hex_encode()``, ``hex_alloc()``, ``hex_decode()``,
``hex_decode_max()``
diff --git a/include/shirudo/string.h b/include/shirudo/string.h
index 8ef496ba..440bbcc1 100644
--- a/include/shirudo/string.h
+++ b/include/shirudo/string.h
@@ -147,6 +147,45 @@ static inline bool span_equals_str(const char *s, size_t len, const char *str)
!strncasecmp(s, "" prefix, sizeof(prefix) - 1)) ? \
sizeof(prefix) - 1 : 0)
+/* Whether a and b are the same string, a letter in either case. */
+static inline bool str_equals_nocase(const char *a, const char *b)
+{
+ return !strcasecmp(a, b);
+}
+
+/*
+ * How many of the len bytes at s, from the first, are none of the bytes
+ * of set: the length of what comes before the first of them, or len.
+ */
+static inline size_t span_until(const char *s, size_t len, const char *set)
+{
+ size_t n, i;
+
+ for (n = 0; n < len; n++)
+ for (i = 0; set[i]; i++)
+ if (s[n] == set[i])
+ return n;
+ return n;
+}
+
+/*
+ * Where the string needle first lies in the len bytes at s, as a pointer
+ * of the type of s, or NULL. Nothing past len is read, a NUL in the span
+ * is a byte as any other, and the empty needle lies at s.
+ */
+#define span_find(s, len, needle) ({ \
+ __auto_type __s = &(s)[0]; \
+ const char *__needle = (needle); \
+ \
+ (typeof(__s))memmem(__s, len, __needle, strlen(__needle)); \
+})
+
+/* Whether the string s holds the string needle. */
+static inline bool str_contains(const char *s, const char *needle)
+{
+ return span_find(s, strlen(s), needle);
+}
+
/*
* The next token of the len bytes at s, from *pos on: a run of bytes that
* are none of delims, after any that are. Its length, with its offset in
diff --git a/testing/coccinelle/option_by_hand.cocci b/testing/coccinelle/option_by_hand.cocci
index 3a8c6d80..79caddab 100644
--- a/testing/coccinelle/option_by_hand.cocci
+++ b/testing/coccinelle/option_by_hand.cocci
@@ -14,7 +14,8 @@ constant char[] S;
identifier f = {strcmp, strncmp, strcasecmp, strncasecmp, str_has_prefix,
str_skip_prefix, str_has_prefix_nocase,
str_skip_prefix_nocase, str_after_prefix,
- str_after_prefix_nocase};
+ str_after_prefix_nocase, str_equals_nocase,
+ str_contains};
identifier g = {span_equals, span_equals_str, span_has_prefix_nocase};
position p;
@@
diff --git a/testing/proofs/cbmc/proof.h b/testing/proofs/cbmc/proof.h
index b8ba91d4..38154f8c 100644
--- a/testing/proofs/cbmc/proof.h
+++ b/testing/proofs/cbmc/proof.h
@@ -32,8 +32,8 @@
#undef strstr
/*
- * CBMC models neither strnlen(), memchr(), strcspn(), strstr() nor
- * vsnprintf(), and a function without a body returns anything.
+ * CBMC models neither strnlen(), memchr(), memmem(), strcspn(), strstr()
+ * nor vsnprintf(), and a function without a body returns anything.
*/
size_t strnlen(const char *s, size_t max)
{
@@ -76,6 +76,20 @@ void *memchr(const void *s, int c, size_t n)
}
#endif
+void *memmem(const void *hay, size_t hay_len, const void *needle,
+ size_t needle_len)
+{
+ const __u8 *h = hay;
+ size_t i;
+
+ if (needle_len > hay_len)
+ return NULL;
+ for (i = 0; i <= hay_len - needle_len; i++)
+ if (!memcmp(h + i, needle, needle_len))
+ return (void *)(h + i);
+ return NULL;
+}
+
size_t strcspn(const char *s, const char *reject)
{
size_t n, i;
diff --git a/testing/proofs/cbmc/proofs.list b/testing/proofs/cbmc/proofs.list
index 12cb46b2..b5ce0f95 100644
--- a/testing/proofs/cbmc/proofs.list
+++ b/testing/proofs/cbmc/proofs.list
@@ -7,6 +7,7 @@ string-span --unwind 9
string-suffix --unwind 8
string-prefix --unwind 8
string-tokens --unwind 8 LEN=0-4
+string-search --unwind 8 LEN=0-5
string-sanitize --unwind 8 LEN=0-6
hex --unwind 12
io-lines --unwind 10
diff --git a/testing/proofs/cbmc/string-search.c b/testing/proofs/cbmc/string-search.c
new file mode 100644
index 00000000..9cc49a84
--- /dev/null
+++ b/testing/proofs/cbmc/string-search.c
@@ -0,0 +1,125 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * The searches of <shirudo/string.h> on every span of LEN bytes:
+ * span_until() gives the length of what comes before the first byte of a
+ * set of one or two, span_find() the first place a needle of up to
+ * NEEDLE_MAX bytes lies in the span or NULL where it lies nowhere, both
+ * reading the span's bytes alone, str_contains() says the same of a
+ * string, and str_equals_nocase() is true exactly of two strings of the
+ * same letters.
+ */
+
+#include <stdbool.h>
+
+#include "proof.h"
+#include "../../../util/string.c"
+
+#define NEEDLE_MAX 2
+
+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;
+}
+
+static void up_to_a_byte_of_a_set(const char *s)
+{
+ char set[3] = {};
+ char first, second;
+ size_t n, i;
+
+ __CPROVER_assume(first);
+ set[0] = first;
+ set[1] = second;
+ n = span_until(s, LEN, set);
+ __CPROVER_assert(n <= LEN, "no longer than the span");
+ for (i = 0; i < LEN; i++) {
+ bool in_set = s[i] == set[0] || (set[1] && s[i] == set[1]);
+
+ if (i < n)
+ __CPROVER_assert(!in_set, "no byte of the set before");
+ if (i == n)
+ __CPROVER_assert(in_set, "a byte of the set ends it");
+ }
+}
+
+/* Whether the m bytes of needle lie at s + at, inside the span. */
+static bool lies_at(const char *s, size_t at, const char *needle, size_t m)
+{
+ size_t i;
+
+ if (m > LEN || at > LEN - m)
+ return false;
+ for (i = 0; i < m; i++)
+ if (s[at + i] != needle[i])
+ return false;
+ return true;
+}
+
+static void first_place_of_a_needle(const char *s)
+{
+ const char *needle, *found;
+ size_t m, i;
+
+ __CPROVER_assume(m <= NEEDLE_MAX);
+ needle = any_string(m);
+ found = span_find(s, LEN, needle);
+ for (i = 0; i <= LEN; i++) {
+ if (found && i < (size_t)(found - s))
+ __CPROVER_assert(!lies_at(s, i, needle, m),
+ "no earlier place");
+ if (!found)
+ __CPROVER_assert(!lies_at(s, i, needle, m),
+ "NULL only where it lies nowhere");
+ }
+ __CPROVER_assert(!found || (found >= s &&
+ lies_at(s, (size_t)(found - s), needle, m)),
+ "the needle, inside the span");
+}
+
+static void strings(void)
+{
+ size_t a_len, b_len, m, i;
+ const char *a, *b, *needle;
+ bool same, lies = false;
+
+ __CPROVER_assume(a_len <= 3 && b_len <= 3 && m <= NEEDLE_MAX);
+ a = any_string(a_len);
+ b = any_string(b_len);
+ needle = any_string(m);
+ same = a_len == b_len;
+ for (i = 0; i < 3; i++)
+ if (i < a_len && i < b_len && lower(a[i]) != lower(b[i]))
+ same = false;
+ __CPROVER_assert(str_equals_nocase(a, b) == same,
+ "the same letters, and as many");
+ for (i = 0; i <= 3; i++) {
+ size_t k;
+ bool here = m <= a_len && i <= a_len - m;
+
+ for (k = 0; k < NEEDLE_MAX; k++)
+ if (here && k < m && a[i + k] != needle[k])
+ here = false;
+ lies = lies || here;
+ }
+ __CPROVER_assert(str_contains(a, needle) == lies,
+ "a string holds the needle where it lies in it");
+}
+
+void harness(void)
+{
+ const char *s = any_bytes(LEN);
+
+ up_to_a_byte_of_a_set(s);
+ first_place_of_a_needle(s);
+ strings();
+}
diff --git a/testing/unit/string.c b/testing/unit/string.c
index 41c5845e..d4839b77 100644
--- a/testing/unit/string.c
+++ b/testing/unit/string.c
@@ -773,6 +773,68 @@ static void token_next_takes_any_cursor(void)
EXPECT_UEQ(pos, 3);
}
+static void str_equals_nocase_wants_the_whole_string(void)
+{
+ EXPECT_TRUE(str_equals_nocase("Content-Type", "content-type"));
+ EXPECT_TRUE(str_equals_nocase("https", "HTTPS"));
+ EXPECT_TRUE(str_equals_nocase("", ""));
+ EXPECT_FALSE(str_equals_nocase("https", "http"));
+ EXPECT_FALSE(str_equals_nocase("http", "https"));
+ EXPECT_FALSE(str_equals_nocase("host", "h0st"));
+ EXPECT_FALSE(str_equals_nocase("", "x"));
+}
+
+static void span_until_stops_at_a_byte_of_the_set(void)
+{
+ char *line = span("text/html; q=1\r\n", 16), *plain = span("abc", 3);
+
+ EXPECT_UEQ(span_until(line, 16, " ;\r\n"), 9);
+ EXPECT_UEQ(span_until(line, 16, "\r\n"), 14);
+ EXPECT_UEQ(span_until(line, 9, ";"), 9);
+ EXPECT_UEQ(span_until(line, 5, "/"), 4);
+ EXPECT_UEQ(span_until(line, 4, "/"), 4);
+ EXPECT_UEQ(span_until(line, 16, "t"), 0);
+ EXPECT_UEQ(span_until(plain, 3, "="), 3);
+ EXPECT_UEQ(span_until(plain, 3, ""), 3);
+ EXPECT_UEQ(span_until(plain, 0, "a"), 0);
+ EXPECT_UEQ(span_until("a\0b=c", 5, "="), 3);
+ xfree(line, 16);
+ xfree(plain, 3);
+}
+
+static void span_find_reads_the_span_alone(void)
+{
+ char *doc = span("{\"token\":\"t\"}", 13), *found;
+ const char *path = "a/../b", *at;
+
+ found = span_find(doc, 13, "\"token\"");
+ EXPECT_TRUE(found == doc + 1);
+ EXPECT_TRUE(span_find(doc, 13, "}") == doc + 12);
+ EXPECT_FALSE(span_find(doc, 12, "}"));
+ EXPECT_FALSE(span_find(doc, 7, "\"token\""));
+ EXPECT_FALSE(span_find(doc, 13, "\"Token\""));
+ EXPECT_FALSE(span_find(doc, 0, "t"));
+ EXPECT_TRUE(span_find(doc, 13, "") == doc);
+ EXPECT_TRUE(span_find(doc, 0, "") == doc);
+ at = span_find(path, strlen(path), "..");
+ EXPECT_TRUE(at == path + 2);
+ EXPECT_TRUE(span_find("a\0b..c", 6, ".."));
+ xfree(doc, 13);
+}
+
+static void str_contains_stops_at_the_terminator(void)
+{
+ EXPECT_TRUE(str_contains("repo//name", "//"));
+ EXPECT_TRUE(str_contains("a/..", ".."));
+ EXPECT_TRUE(str_contains("..", ".."));
+ EXPECT_TRUE(str_contains("x", ""));
+ EXPECT_TRUE(str_contains("", ""));
+ EXPECT_FALSE(str_contains("a/./b", ".."));
+ EXPECT_FALSE(str_contains(".", ".."));
+ EXPECT_FALSE(str_contains("", ".."));
+ EXPECT_FALSE(str_contains("a\0b..c", ".."));
+}
+
static const struct unit_case cases[] = {
UNIT_CASE(digits_counts_leading_digits),
UNIT_CASE(decimal_fills_its_span),
@@ -815,6 +877,10 @@ static const struct unit_case cases[] = {
UNIT_CASE(token_may_be_ended_in_its_own_buffer),
UNIT_CASE(token_walks_nest),
UNIT_CASE(token_next_takes_any_cursor),
+ UNIT_CASE(str_equals_nocase_wants_the_whole_string),
+ UNIT_CASE(span_until_stops_at_a_byte_of_the_set),
+ UNIT_CASE(span_find_reads_the_span_alone),
+ UNIT_CASE(str_contains_stops_at_the_terminator),
{}
};
diff --git a/util/string.c b/util/string.c
index cc8d4313..b1aefc52 100644
--- a/util/string.c
+++ b/util/string.c
@@ -135,12 +135,11 @@ static bool among(char c, const char *set)
size_t token_next(const char *s, size_t len, const char *delims, size_t *pos,
size_t *start)
{
- size_t at = min(*pos, len), n = 0;
+ size_t at = min(*pos, len), n;
while (at < len && among(s[at], delims))
at++;
- while (n < len - at && !among(s[at + n], delims))
- n++;
+ n = span_until(s + at, len - at, delims);
*start = at;
*pos = at + n + (n < len - at);
return n;