Skip to content

eBPF-based security platform

string: poison strcasecmp(), strcspn() and strstr()

Daniel Borkmannc575169aed7a
diff --git a/.agents/skills/coding/SKILL.md b/.agents/skills/coding/SKILL.md
index 2c1dfedf..3fa84a8f 100644
--- a/.agents/skills/coding/SKILL.md
+++ b/.agents/skills/coding/SKILL.md
@@ -392,8 +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; `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>`) |
+| 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, and `strcasecmp()` 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>`); `strcspn()`, `strpbrk()` and `strstr()`, which search to a terminator, are poisoned |
| 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()` |
@@ -437,8 +437,9 @@ without a body.
the like. System headers that use `uint8_t` and kin go above the first
shirudo header, which poisons them.
- `strtoul()`, `atoi()`, `sscanf()`, `strcpy()`, `sprintf()`,
- `snprintf()`, `strncmp()`, `strncasecmp()`, `strtok_r()`, `strtok()`,
- `strsep()`, the libc allocators and `explicit_bzero()` are poisoned;
+ `snprintf()`, `strncmp()`, `strncasecmp()`, `strcasecmp()`,
+ `strcspn()`, `strstr()`, `strtok_r()`, `strtok()`, `strsep()`, the
+ libc allocators and `explicit_bzero()` are poisoned;
the table above has what replaces each, and a missing one is wrapped
in `util/`, never given an exception. So are the string functions of
libc that nothing calls, `strspn()`, `strpbrk()`, `strcoll()`,
diff --git a/.github/scripts/check-style.sh b/.github/scripts/check-style.sh
index fbc70f75..da84f52b 100755
--- a/.github/scripts/check-style.sh
+++ b/.github/scripts/check-style.sh
@@ -7,7 +7,7 @@
# cilium checks its BPF tree, plus the block-comment opener that checkpatch
# leaves to the netdev trees, the kernel's spelling of fixed-width
# integers and the project's number parsers, string copies, formatting,
-# compares and token walks.
+# compares, token walks and searches.
# The kernel checks that have no meaning in userspace are switched off
# below, each with its reason.
#
@@ -212,12 +212,16 @@ locate_checkpatch() {
# snprintf() or their va_list forms is reported outside the tests and
# the two files that implement strsprintf() and xasprintf() with them:
# strsprintf() and vstrsprintf() refuse output that does not fit rather
-# than cut it. So is a call to strncmp() or strncasecmp() outside the
-# tests and the header that wraps them: str_has_prefix(),
+# than cut it. So is a call to strncmp(), strncasecmp() or strcasecmp()
+# outside the tests and the header that wraps them: str_has_prefix(),
# str_after_prefix(), span_equals() and their kin take the length from
-# the string, where a caller's count can be off by one and still compile.
+# the string, where a caller's count can be off by one and still compile,
+# and str_equals_nocase() says whether two strings are the same.
# And so is strtok_r(), strtok() or strsep(): for_each_token() gives each
# token as pointer and length and leaves the walked buffer as it was.
+# strcspn() and strstr() are reported as STRING_SEARCH: span_until() and
+# span_find() take the length of what they search, and str_contains()
+# answers for a string.
# The scanf() family counts as a number parser and stpcpy(), strlcat(),
# memccpy() and bcopy() as string copies, and a call to one of the other
# functions <shirudo/string.h> poisons, which nothing in the tree calls,
@@ -338,10 +342,18 @@ check_spellings() {
bad = 1
}
if (!compares_kept(file) &&
- match(code, /(^|[^A-Za-z0-9_])strn(case)?cmp[ \t]*\(/)) {
+ match(code, /(^|[^A-Za-z0-9_])str(ncmp|n?casecmp)[ \t]*\(/)) {
fn = substr(code, RSTART, RLENGTH)
gsub(/[^A-Za-z0-9_]/, "", fn)
- printf "ERROR:STRING_COMPARE: compare a prefix with str_has_prefix() or str_after_prefix() and a span with span_equals() or span_equals_str() from <shirudo/string.h>, not %s()\n#%d: FILE: %s:%d:\n%s\n\n",
+ printf "ERROR:STRING_COMPARE: compare a prefix with str_has_prefix() or str_after_prefix(), a span with span_equals() or span_equals_str() and a string in either case with str_equals_nocase() from <shirudo/string.h>, not %s()\n#%d: FILE: %s:%d:\n%s\n\n",
+ fn, NR, file, line, $0
+ bad = 1
+ }
+ if (!parsers_kept(file) &&
+ match(code, /(^|[^A-Za-z0-9_])str(cspn|str)[ \t]*\(/)) {
+ fn = substr(code, RSTART, RLENGTH)
+ gsub(/[^A-Za-z0-9_]/, "", fn)
+ printf "ERROR:STRING_SEARCH: search with span_until(), span_find() or str_contains() from <shirudo/string.h>, not %s()\n#%d: FILE: %s:%d:\n%s\n\n",
fn, NR, file, line, $0
bad = 1
}
@@ -534,7 +546,7 @@ list_types() {
LC_ALL=C comm -23 "$work/all" "$work/ignored"
printf '%s\n' KERNEL_INT_TYPES NETDEV_BLOCK_COMMENT NUMBER_PARSING \
STRING_COMPARE STRING_COPY STRING_FORMAT STRING_LIBC \
- STRING_TOKENS
+ STRING_SEARCH STRING_TOKENS
} | LC_ALL=C sort
}
diff --git a/Documentation/contribute/code/coding-style.rst b/Documentation/contribute/code/coding-style.rst
index d601fdce..774be8d6 100644
--- a/Documentation/contribute/code/coding-style.rst
+++ b/Documentation/contribute/code/coding-style.rst
@@ -32,8 +32,13 @@ its BPF programs, with these rules of its own, which
- Test a string's prefix with ``str_has_prefix()`` or
``str_after_prefix()`` and compare a span of known length with
``span_equals()`` or ``span_equals_str()``, which take the length from
- the string, not from a count beside it. ``strncmp()`` and
- ``strncasecmp()`` are refused.
+ the string, not from a count beside it, and two whole strings in
+ either case with ``str_equals_nocase()``. ``strncmp()``,
+ ``strncasecmp()`` and ``strcasecmp()`` are refused.
+- Search the bytes you have the length of: ``span_until()`` measures up
+ to the first byte of a set, ``span_find()`` finds a string in a span,
+ and ``str_contains()`` says whether a string holds another.
+ ``strcspn()`` and ``strstr()`` are refused.
- Walk the lines of a buffer, the words of a line or the entries of a
list with ``for_each_token()``, which gives each token as a pointer and
its length and writes nothing. ``strtok_r()``, ``strtok()`` and
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index d1f55de5..9855ca8b 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -43,9 +43,9 @@ signed config image.
* - ``t0003-check-style``
- ``check-style.sh``: checkpatch at 100 columns, the block-comment
opener, the subject length, the integer, parser, copy, format,
- compare and token rules and the string functions nothing calls,
- and a fetch of ``checkpatch.pl`` that asks a host which does not
- answer only once
+ compare, token and search rules and the string functions nothing
+ calls, and a fetch of ``checkpatch.pl`` that asks a host which
+ does not answer only once
* - ``t0004-ci-commit-range``
- ``ci-commit-range.sh``: the ranges for a pull request, a push and
a force push
diff --git a/include/shirudo/string.h b/include/shirudo/string.h
index 5c4e4672..3cd683b1 100644
--- a/include/shirudo/string.h
+++ b/include/shirudo/string.h
@@ -280,9 +280,9 @@ static inline int memneq_explicit(const void *a, const void *b, size_t size)
# pragma GCC poison strtoq strtouq scanf fscanf sscanf vscanf vfscanf vsscanf
# pragma GCC poison strcpy strncpy strcat strncat strlcpy
# pragma GCC poison stpcpy stpncpy strlcat memccpy bcopy
-# pragma GCC poison strncmp strncasecmp strcoll strxfrm bcmp
+# pragma GCC poison strncmp strncasecmp strcasecmp strcoll strxfrm bcmp
# pragma GCC poison strtok strtok_r strsep
-# pragma GCC poison strspn strpbrk strcasestr strchrnul strverscmp
+# pragma GCC poison strspn strcspn strcasestr strchrnul strverscmp
# pragma GCC poison rawmemchr rindex
# pragma GCC poison strfry memfrob bzero gets
/*
@@ -291,7 +291,9 @@ static inline int memneq_explicit(const void *a, const void *b, size_t size)
*/
# undef strdupa
# undef strndupa
-# pragma GCC poison strdupa strndupa
+# undef strpbrk
+# undef strstr
+# pragma GCC poison strdupa strndupa strpbrk strstr
/* Fortified glibc makes these macros for clang; poison the names alone. */
# undef sprintf
# undef snprintf
diff --git a/testing/ci-checks/t0003-check-style.sh b/testing/ci-checks/t0003-check-style.sh
index 1ecd0957..01e8d510 100644
--- a/testing/ci-checks/t0003-check-style.sh
+++ b/testing/ci-checks/t0003-check-style.sh
@@ -7,7 +7,7 @@
# opener and the subject length the wrapper adds, the message of a commit
# that touches no C, the sources it leaves alone, the userspace spellings
# it lets through, the C library's integer names, number parsers, string
-# copies, counted compares, token walks and unused string functions it
+# copies, compares, token walks, searches and unused string functions it
# refuses, the list of what stays enabled, and the fetch of checkpatch.pl,
# which asks a host that does not answer only once, on a throwaway
# repository.
@@ -190,6 +190,7 @@ test_expect_success '--types lists what stays enabled' '
grep -qx "STRING_COPY" "$W/out" &&
grep -qx "STRING_FORMAT" "$W/out" &&
grep -qx "STRING_LIBC" "$W/out" &&
+ grep -qx "STRING_SEARCH" "$W/out" &&
grep -qx "STRING_TOKENS" "$W/out" &&
! grep -qx "CAMELCASE" "$W/out" &&
! grep -qx "GIT_COMMIT_ID" "$W/out"
@@ -280,12 +281,14 @@ test_expect_success 'strings are compared without a count outside the tests' '
"int scheme(const char *s)" "{" \
" return !strncmp(s, \"az\", 3) ||" \
" !strncasecmp(s, \"Bearer\", 6) ||" \
+ " !strcasecmp(s, \"https\") ||" \
" !!str_has_prefix(s, \"oci://\");" "}" \
>>"$R/good.c" &&
! check --diff &&
- grep -q "^ERROR:STRING_COMPARE: compare a prefix with str_has_prefix() or str_after_prefix() and a span with span_equals() or span_equals_str() from <shirudo/string.h>, not strncmp()" "$W/out" &&
+ grep -q "^ERROR:STRING_COMPARE: compare a prefix with str_has_prefix() or str_after_prefix(), a span with span_equals() or span_equals_str() and a string in either case with str_equals_nocase() from <shirudo/string.h>, not strncmp()" "$W/out" &&
grep -q "^ERROR:STRING_COMPARE: .*, not strncasecmp()" "$W/out" &&
- [ "$(grep -c "^ERROR:STRING_COMPARE" "$W/out")" = 2 ] &&
+ grep -q "^ERROR:STRING_COMPARE: .*, not strcasecmp()" "$W/out" &&
+ [ "$(grep -c "^ERROR:STRING_COMPARE" "$W/out")" = 3 ] &&
git -C "$R" checkout -q -- good.c &&
put include/shirudo/string.h \
"/* SPDX-License-Identifier: BSD-2-Clause */" "" \
@@ -317,6 +320,29 @@ test_expect_success 'tokens are walked with for_each_token() outside the tests'
rm -r "$R/testing"
'
+test_expect_success 'strings are searched by their length outside the tests' '
+ printf "%s\n" "/* strstr() in a comment */" \
+ "static const char *op = \"strcspn(a, b)\";" "" \
+ "size_t find(const char *s)" "{" \
+ " return strstr(s, \"..\") ? 0 : strcspn(s, \"=\") +" \
+ " span_until(s, 2, \"=\");" "}" \
+ >>"$R/good.c" &&
+ ! check --diff &&
+ grep -q "^ERROR:STRING_SEARCH: search with span_until(), span_find() or str_contains() from <shirudo/string.h>, not strstr()" "$W/out" &&
+ [ "$(grep -c "^ERROR:STRING_SEARCH" "$W/out")" = 1 ] &&
+ git -C "$R" checkout -q -- good.c &&
+ printf "%s\n" "" "size_t cut(const char *s)" "{" \
+ " return strcspn(s, \"=\");" "}" >>"$R/good.c" &&
+ ! check --diff &&
+ grep -q "^ERROR:STRING_SEARCH: .*, not strcspn()" "$W/out" &&
+ git -C "$R" checkout -q -- good.c &&
+ put testing/helper/find.c "// SPDX-License-Identifier: BSD-2-Clause" "" \
+ "int find(const char *s)" "{" \
+ " return !!strstr(s, \"..\");" "}" &&
+ check --files testing/helper/find.c &&
+ rm -r "$R/testing"
+'
+
test_expect_success 'the string functions nothing calls are refused outside the tests' '
printf "%s\n" "/* strspn() in a comment, and what a caller gets */" \
"static const char *op = \"bzero(p, n)\";" "" \