Skip to content

eBPF-based security platform

string: poison strncmp() and strncasecmp()

Daniel Borkmann69e1d9586583
diff --git a/.agents/skills/coding/SKILL.md b/.agents/skills/coding/SKILL.md
index 5015afb3..a132ba1f 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 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 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 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`) |
@@ -434,10 +434,11 @@ without a body.
`uint32_t` and its kin, and in userspace `unsigned int`, `long` and
the like. System headers that use `uint8_t` and kin go above the first
shirudo header, which poisons them.
-- `strtoul()`, `atoi()`, `strcpy()`, `sprintf()`, `snprintf()`, 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.
+- `strtoul()`, `atoi()`, `strcpy()`, `sprintf()`, `snprintf()`,
+ `strncmp()`, `strncasecmp()`, 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.
- A `strsprintf()` that does not fit leaves the buffer empty: a path or
value is refused, a helper formatting display text returns the error,
and status shows `"?"`. Constant text is copied, not formatted.
diff --git a/.github/scripts/check-style.sh b/.github/scripts/check-style.sh
index 2dd88858..3f5a9465 100755
--- a/.github/scripts/check-style.sh
+++ b/.github/scripts/check-style.sh
@@ -6,7 +6,8 @@
# mode, over the C sources a commit touches and over its message, the way
# 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 and formatting.
+# integers and the project's number parsers, string copies, formatting
+# and compares.
# The kernel checks that have no meaning in userspace are switched off
# below, each with its reason.
#
@@ -211,7 +212,10 @@ 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.
+# than cut it. So is a call to strncmp() or strncasecmp() 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.
check_spellings() {
awk '
function c_names_kept(f) {
@@ -226,6 +230,9 @@ check_spellings() {
return parsers_kept(f) || f == "util/string.c" ||
f == "util/malloc.c"
}
+ function compares_kept(f) {
+ return parsers_kept(f) || f == "include/shirudo/string.h"
+ }
/^\+\+\+ / {
file = $0
sub(/^\+\+\+ b\//, "", file)
@@ -324,6 +331,14 @@ check_spellings() {
fn, NR, file, line, $0
bad = 1
}
+ if (!compares_kept(file) &&
+ match(code, /(^|[^A-Za-z0-9_])strn(case)?cmp[ \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",
+ fn, NR, file, line, $0
+ bad = 1
+ }
}
END { exit bad }
' "$1"
@@ -496,7 +511,7 @@ list_types() {
{
LC_ALL=C comm -23 "$work/all" "$work/ignored"
printf '%s\n' KERNEL_INT_TYPES NETDEV_BLOCK_COMMENT NUMBER_PARSING \
- STRING_COPY STRING_FORMAT
+ STRING_COMPARE STRING_COPY STRING_FORMAT
} | LC_ALL=C sort
}
diff --git a/AGENTS.md b/AGENTS.md
index f9b0a522..55131d8b 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -116,7 +116,8 @@ commit message that carries a change.
- Use the helpers of `util/` and `include/shirudo/` over libc and open
code: `parse_u64()` and its kin for numbers, `<shirudo/overflow.h>`
for sizes, `strscpy()` and `strsprintf()` with their `-E2BIG`
- checked, the `xmalloc()` family, `min()` and `max()`,
+ checked, `str_has_prefix()`, `str_after_prefix()` and `span_equals()`
+ for a prefix or a span, 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/code/coding-style.rst b/Documentation/contribute/code/coding-style.rst
index 195000d6..370add33 100644
--- a/Documentation/contribute/code/coding-style.rst
+++ b/Documentation/contribute/code/coding-style.rst
@@ -29,6 +29,11 @@ its BPF programs, with these rules of its own, which
are for strings that came through one of these, for literals,
``argv`` and paths, not for a buffer a ``read()`` filled, a fixed-size
field of a format or a pointer that came with a length.
+- 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.
- Check size arithmetic with the helpers of ``<shirudo/overflow.h>``,
not by hand.
- No ``(void)`` cast. A result a caller must not drop belongs to a
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index 44ac55fa..b65cbb6d 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -42,9 +42,9 @@ signed config image.
licenses, the upstream tag of a carried file, and the tree itself
* - ``t0003-check-style``
- ``check-style.sh``: checkpatch at 100 columns, the block-comment
- opener, the subject length, the integer, parser, copy and
- format rules, and a fetch of ``checkpatch.pl`` that asks a host
- which does not answer only once
+ opener, the subject length, the integer, parser, copy, format
+ and compare rules, 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 300043ec..17bc9638 100644
--- a/include/shirudo/string.h
+++ b/include/shirudo/string.h
@@ -210,6 +210,7 @@ static inline int memneq_explicit(const void *a, const void *b, size_t size)
# pragma GCC poison strtol strtoll strtoul strtoull strtoimax strtoumax
# pragma GCC poison strtod strtof strtold atoi atol atoll atof
# pragma GCC poison strcpy strncpy strcat strncat strlcpy
+# pragma GCC poison strncmp strncasecmp
/* 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 ff692c0b..0f52f41f 100644
--- a/testing/ci-checks/t0003-check-style.sh
+++ b/testing/ci-checks/t0003-check-style.sh
@@ -6,10 +6,10 @@
# enforces on the C a commit touches at 100 columns, the block-comment
# 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 and
-# string copies 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.
+# it lets through, the C library's integer names, number parsers, string
+# copies and counted compares 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.
cd "$(dirname "$0")/.."
. ./test-lib.sh
@@ -185,6 +185,7 @@ test_expect_success '--types lists what stays enabled' '
grep -qx "BLOCK_COMMENT_STYLE" "$W/out" &&
grep -qx "KERNEL_INT_TYPES" "$W/out" &&
grep -qx "NUMBER_PARSING" "$W/out" &&
+ grep -qx "STRING_COMPARE" "$W/out" &&
grep -qx "STRING_COPY" "$W/out" &&
grep -qx "STRING_FORMAT" "$W/out" &&
! grep -qx "CAMELCASE" "$W/out" &&
@@ -267,6 +268,29 @@ test_expect_success 'strings are formatted with strsprintf() outside the tests'
rm -r "$R/util" "$R/testing"
'
+test_expect_success 'strings are compared without a count outside the tests' '
+ printf "%s\n" "/* strncmp() in a comment */" \
+ "static const char *op = \"strncasecmp(a, b, 2)\";" "" \
+ "int scheme(const char *s)" "{" \
+ " return !strncmp(s, \"az\", 3) ||" \
+ " !strncasecmp(s, \"Bearer\", 6) ||" \
+ " !!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: .*, not strncasecmp()" "$W/out" &&
+ [ "$(grep -c "^ERROR:STRING_COMPARE" "$W/out")" = 2 ] &&
+ git -C "$R" checkout -q -- good.c &&
+ put include/shirudo/string.h \
+ "/* SPDX-License-Identifier: BSD-2-Clause */" "" \
+ "#define starts(s) (!strncmp(s, \"az\", 2))" &&
+ put testing/helper/cmp.c "// SPDX-License-Identifier: BSD-2-Clause" "" \
+ "int cmp(const char *s)" "{" \
+ " return strncmp(s, \"az\", 2);" "}" &&
+ check --files include/shirudo/string.h testing/helper/cmp.c &&
+ rm -r "$R/include" "$R/testing"
+'
+
test_expect_success 'a BPF program keeps the kernel'"'"'s argument types' '
put hook.bpf.c "// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause" "" \
"int hook(unsigned long arg, unsigned short port, unsigned char c)" \