eBPF-based security platform
string: poison strtok_r(), strtok() and strsep()
Daniel Borkmann3bf868540690
diff --git a/.agents/skills/coding/SKILL.md b/.agents/skills/coding/SKILL.mdindex f4fa95a0..7cbe44b2 100644--- a/.agents/skills/coding/SKILL.md+++ b/.agents/skills/coding/SKILL.md@@ -393,7 +393,7 @@ already, or should. The basics:| 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 |-| 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 |+| 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()` || 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`) |@@ -436,10 +436,10 @@ without a body.the like. System headers that use `uint8_t` and kin go above the firstshirudo header, which poisons them.- `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.+ `strncmp()`, `strncasecmp()`, `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.- A `strsprintf()` that does not fit leaves the buffer empty: a path orvalue 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.shindex 3f5a9465..84073a8f 100755--- a/.github/scripts/check-style.sh+++ b/.github/scripts/check-style.sh@@ -6,8 +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, formatting-# and compares.+# integers and the project's number parsers, string copies, formatting,+# compares and token walks.# The kernel checks that have no meaning in userspace are switched off# below, each with its reason.#@@ -216,6 +216,8 @@ locate_checkpatch() {# 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.+# 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.check_spellings() {awk 'function c_names_kept(f) {@@ -339,6 +341,14 @@ check_spellings() {fn, NR, file, line, $0bad = 1}+ if (!parsers_kept(file) &&+ match(code, /(^|[^A-Za-z0-9_])str(tok(_r)?|sep)[ \t]*\(/)) {+ fn = substr(code, RSTART, RLENGTH)+ gsub(/[^A-Za-z0-9_]/, "", fn)+ printf "ERROR:STRING_TOKENS: walk tokens with for_each_token() 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"@@ -511,7 +521,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_COMPARE STRING_COPY STRING_FORMAT STRING_TOKENS} | LC_ALL=C sort}diff --git a/AGENTS.md b/AGENTS.mdindex 55131d8b..a6ddcbe6 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -117,7 +117,8 @@ 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, the `xmalloc()` family, `min()` and `max()`,+ for a prefix or a span, `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 namesthey replace are poisoned. Code that a second place needs moves intodiff --git a/Documentation/contribute/code/coding-style.rst b/Documentation/contribute/code/coding-style.rstindex 370add33..7e2948ae 100644--- a/Documentation/contribute/code/coding-style.rst+++ b/Documentation/contribute/code/coding-style.rst@@ -34,6 +34,10 @@ its BPF programs, with these rules of its own, which``span_equals()`` or ``span_equals_str()``, which take the length fromthe string, not from a count beside it. ``strncmp()`` and``strncasecmp()`` 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+ ``strsep()`` 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 adiff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex b65cbb6d..a2fa6532 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, format- and compare 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,+ compare and token 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 anda force pushdiff --git a/include/shirudo/string.h b/include/shirudo/string.hindex 7e648d0c..8ef496ba 100644--- a/include/shirudo/string.h+++ b/include/shirudo/string.h@@ -238,6 +238,7 @@ static inline int memneq_explicit(const void *a, const void *b, size_t size)# pragma GCC poison strtod strtof strtold atoi atol atoll atof# pragma GCC poison strcpy strncpy strcat strncat strlcpy# pragma GCC poison strncmp strncasecmp+# pragma GCC poison strtok strtok_r strsep/* Fortified glibc makes these macros for clang; poison the names alone. */# undef sprintf# undef snprintfdiff --git a/testing/ci-checks/t0003-check-style.sh b/testing/ci-checks/t0003-check-style.shindex 0f52f41f..bee49e4a 100644--- a/testing/ci-checks/t0003-check-style.sh+++ b/testing/ci-checks/t0003-check-style.sh@@ -7,9 +7,9 @@# 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 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.+# copies, counted compares and token walks 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@@ -188,6 +188,7 @@ test_expect_success '--types lists what stays enabled' 'grep -qx "STRING_COMPARE" "$W/out" &&grep -qx "STRING_COPY" "$W/out" &&grep -qx "STRING_FORMAT" "$W/out" &&+ grep -qx "STRING_TOKENS" "$W/out" &&! grep -qx "CAMELCASE" "$W/out" &&! grep -qx "GIT_COMMIT_ID" "$W/out"'@@ -291,6 +292,26 @@ test_expect_success 'strings are compared without a count outside the tests' 'rm -r "$R/include" "$R/testing"'+test_expect_success 'tokens are walked with for_each_token() outside the tests' '+ printf "%s\n" "/* strtok_r() in a comment */" \+ "static const char *op = \"strsep(&s, d)\";" "" \+ "void cut(char *s, char **save)" "{" \+ " strtok_r(s, \",\", save);" " strtok(s, \",\");" \+ " strsep(save, \",\");" "}" \+ >>"$R/good.c" &&+ ! check --diff &&+ grep -q "^ERROR:STRING_TOKENS: walk tokens with for_each_token() from <shirudo/string.h>, not strtok_r()" "$W/out" &&+ grep -q "^ERROR:STRING_TOKENS: .*, not strtok()" "$W/out" &&+ grep -q "^ERROR:STRING_TOKENS: .*, not strsep()" "$W/out" &&+ [ "$(grep -c "^ERROR:STRING_TOKENS" "$W/out")" = 3 ] &&+ git -C "$R" checkout -q -- good.c &&+ put testing/helper/cut.c "// SPDX-License-Identifier: BSD-2-Clause" "" \+ "char *cut(char *s, char **save)" "{" \+ " return strtok_r(s, \",\", save);" "}" &&+ check --files testing/helper/cut.c &&+ rm -r "$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)" \