eBPF-based security platform
string: poison the string functions of libc that nothing calls
Daniel Borkmann631949762e4d
diff --git a/.agents/skills/coding/SKILL.md b/.agents/skills/coding/SKILL.mdindex 976f1247..2c1dfedf 100644--- a/.agents/skills/coding/SKILL.md+++ b/.agents/skills/coding/SKILL.md@@ -436,11 +436,17 @@ without a body.`uint32_t` and its kin, and in userspace `unsigned int`, `long` andthe like. System headers that use `uint8_t` and kin go above the firstshirudo header, which poisons them.-- `strtoul()`, `atoi()`, `strcpy()`, `sprintf()`, `snprintf()`,- `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.+- `strtoul()`, `atoi()`, `sscanf()`, `strcpy()`, `sprintf()`,+ `snprintf()`, `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. So are the string functions of+ libc that nothing calls, `strspn()`, `strpbrk()`, `strcoll()`,+ `strxfrm()`, `strcasestr()`, `strchrnul()`, `strverscmp()`,+ `stpcpy()`, `strlcat()`, `memccpy()`, `bcmp()`, `bcopy()`, `bzero()`,+ `rindex()`, `rawmemchr()`, `gets()`, `strdupa()` and their kin: the+ `#pragma GCC poison` lines of `<shirudo/string.h>` are the list, and+ one comes off it with the first caller that needs it.- 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 84073a8f..fbc70f75 100755--- a/.github/scripts/check-style.sh+++ b/.github/scripts/check-style.sh@@ -218,6 +218,10 @@ locate_checkpatch() {# 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.+# 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,+# is reported as STRING_LIBC.check_spellings() {awk 'function c_names_kept(f) {@@ -308,7 +312,7 @@ check_spellings() {bad = 1}if (!parsers_kept(file) &&- match(code, /(^|[^A-Za-z0-9_])(strto(u?ll?|d|f|ld|imax|umax)|ato(i|l|ll|f))[ \t]*\(/)) {+ match(code, /(^|[^A-Za-z0-9_])(strto(u?ll?|u?q|d|f|ld|imax|umax)|ato(i|l|ll|f)|v?[fs]?scanf)[ \t]*\(/)) {fn = substr(code, RSTART, RLENGTH)gsub(/[^A-Za-z0-9_]/, "", fn)printf "ERROR:NUMBER_PARSING: parse numbers with parse_u64(), parse_s64() or parse_x64() from <shirudo/string.h>, not %s()\n#%d: FILE: %s:%d:\n%s\n\n",@@ -316,7 +320,7 @@ check_spellings() {bad = 1}if (!parsers_kept(file) &&- (match(code, /(^|[^A-Za-z0-9_])str(n?cpy|n?cat|lcpy)[ \t]*\(/) ||+ (match(code, /(^|[^A-Za-z0-9_])(str(n?cpy|n?cat|lcpy|lcat)|stpn?cpy|memccpy|bcopy)[ \t]*\(/) ||(match(code, /(^|[^A-Za-z0-9_])snprintf[ \t]*\([^,]*,[^,]*,[ \t]*""[ \t]*,/) &&$0 ~ /"%s"[ \t]*,/))) {fn = substr(code, RSTART, RLENGTH)@@ -349,6 +353,14 @@ check_spellings() {fn, NR, file, line, $0bad = 1}+ if (!parsers_kept(file) &&+ match(code, /(^|[^A-Za-z0-9_])(strcoll|strxfrm|bcmp|strspn|strpbrk|strcasestr|strchrnul|strverscmp|rawmemchr|rindex|strfry|memfrob|bzero|gets|strn?dupa)[ \t]*\(/)) {+ fn = substr(code, RSTART, RLENGTH)+ gsub(/[^A-Za-z0-9_]/, "", fn)+ printf "ERROR:STRING_LIBC: %s() is poisoned in <shirudo/string.h>, which has what the tree uses in its place\n#%d: FILE: %s:%d:\n%s\n\n",+ fn, NR, file, line, $0+ bad = 1+ }}END { exit bad }' "$1"@@ -521,7 +533,8 @@ 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_TOKENS+ STRING_COMPARE STRING_COPY STRING_FORMAT STRING_LIBC \+ STRING_TOKENS} | LC_ALL=C sort}diff --git a/AGENTS.md b/AGENTS.mdindex 3282a00f..a5343ba9 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -122,8 +122,10 @@ commit message that carries a change.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- `util/` with unit cases rather than being copied.+ they replace are poisoned, and so are the string functions of libc+ that nothing calls; `<shirudo/string.h>` has the list. Code that a+ second place needs moves into `util/` with unit cases rather than+ being copied.- Bytes from outside come with a length and become a string in onestep that refuses a NUL among them and terminates them:`span_to_str()` for a file read whole, a reply, a counted field or adiff --git a/Documentation/contribute/code/coding-style.rst b/Documentation/contribute/code/coding-style.rstindex 7e2948ae..d601fdce 100644--- a/Documentation/contribute/code/coding-style.rst+++ b/Documentation/contribute/code/coding-style.rst@@ -16,8 +16,8 @@ its BPF programs, with these rules of its own, whichprints with ``%llu``.- Parse numbers with ``parse_u64()``, ``parse_s64()`` and``parse_x64()`` from ``<shirudo/string.h>``, which take a length and- refuse a sign, a blank or a base prefix. ``strtoul()``, ``atoi()`` and- their kin are refused.+ refuse a sign, a blank or a base prefix. ``strtoul()``, ``atoi()``,+ ``sscanf()`` and their kin are refused.- Copy strings with ``strscpy()`` and format into a fixed buffer with``strsprintf()``, and check their results. ``strcpy()``, ``strncpy()``,``sprintf()``, ``snprintf()`` and their kin are refused.@@ -38,6 +38,11 @@ its BPF programs, with these rules of its own, whichlist with ``for_each_token()``, which gives each token as a pointer andits length and writes nothing. ``strtok_r()``, ``strtok()`` and``strsep()`` are refused.+- The string functions of the C library that nothing in the tree calls+ are refused too, ``strspn()``, ``strpbrk()``, ``strcoll()``,+ ``stpcpy()``, ``strlcat()``, ``bzero()`` and ``strdupa()`` among them;+ the ``#pragma GCC poison`` lines of ``<shirudo/string.h>`` are the+ list. One comes off it with the first caller that needs it.- 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 a2fa6532..93416dcb 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -43,8 +43,9 @@ signed config image.* - ``t0003-check-style``- ``check-style.sh``: checkpatch at 100 columns, the block-commentopener, 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+ 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* - ``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 440bbcc1..5c4e4672 100644--- a/include/shirudo/string.h+++ b/include/shirudo/string.h@@ -270,14 +270,28 @@ static inline int memneq_explicit(const void *a, const void *b, size_t size)* What the functions above replace, refused once the headers included* above have declared it, so that a later include of those still works;* util/string.c and the xasprintf() of util/malloc.c opt out with- * STRING_INTERNAL.+ * STRING_INTERNAL. With them go the functions of the C library that work+ * on a string and that nothing here calls: one comes off the list with+ * the first caller that needs it.*/#ifndef STRING_INTERNAL# pragma GCC poison strtol strtoll strtoul strtoull strtoimax strtoumax# pragma GCC poison strtod strtof strtold atoi atol atoll atof+# pragma GCC poison strtoq strtouq scanf fscanf sscanf vscanf vfscanf vsscanf# pragma GCC poison strcpy strncpy strcat strncat strlcpy-# pragma GCC poison strncmp strncasecmp+# pragma GCC poison stpcpy stpncpy strlcat memccpy bcopy+# pragma GCC poison strncmp strncasecmp strcoll strxfrm bcmp# pragma GCC poison strtok strtok_r strsep+# pragma GCC poison strspn strpbrk strcasestr strchrnul strverscmp+# pragma GCC poison rawmemchr rindex+# pragma GCC poison strfry memfrob bzero gets+/*+ * glibc makes these macros, the last two under C23; poison the names+ * alone.+ */+# undef strdupa+# undef strndupa+# pragma GCC poison strdupa strndupa/* 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 bee49e4a..1ecd0957 100644--- a/testing/ci-checks/t0003-check-style.sh+++ b/testing/ci-checks/t0003-check-style.sh@@ -7,9 +7,10 @@# 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 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.+# copies, counted compares, token walks 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.cd "$(dirname "$0")/..". ./test-lib.sh@@ -188,6 +189,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_LIBC" "$W/out" &&grep -qx "STRING_TOKENS" "$W/out" &&! grep -qx "CAMELCASE" "$W/out" &&! grep -qx "GIT_COMMIT_ID" "$W/out"@@ -212,11 +214,14 @@ test_expect_success 'the C library'"'"'s number parsers are refused outside theprintf "%s\n" "/* strtoul() in a comment */" \"static const char *name = \"atoi(s)\";" "" \"long parse(const char *s)" "{" \- " return strtoll(s, NULL, 10) + atoi(s);" "}" \+ " long kb = 0;" "" \+ " sscanf(s, \"%ld\", &kb);" \+ " return strtoll(s, NULL, 10) + atoi(s) + kb;" "}" \>>"$R/good.c" &&! check --diff &&grep -q "^ERROR:NUMBER_PARSING: parse numbers with parse_u64(), parse_s64() or parse_x64() from <shirudo/string.h>, not strtoll()" "$W/out" &&- [ "$(grep -c "^ERROR:NUMBER_PARSING" "$W/out")" = 1 ] &&+ grep -q "^ERROR:NUMBER_PARSING: .*, not sscanf()" "$W/out" &&+ [ "$(grep -c "^ERROR:NUMBER_PARSING" "$W/out")" = 2 ] &&git -C "$R" checkout -q -- good.c &&put testing/helper/args.c "// SPDX-License-Identifier: BSD-2-Clause" "" \"long arg(const char *s)" "{" " return strtol(s, NULL, 10);" "}" &&@@ -312,6 +317,26 @@ test_expect_success 'tokens are walked with for_each_token() outside the tests'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)\";" "" \+ "size_t run(char *d, const char *s, int index)" "{" \+ " bzero(d, 8);" " stpcpy(d, s);" \+ " return strspn(s, \"ab\") + (size_t)index;" "}" \+ >>"$R/good.c" &&+ ! check --diff &&+ grep -q "^ERROR:STRING_LIBC: bzero() is poisoned in <shirudo/string.h>, which has what the tree uses in its place" "$W/out" &&+ grep -q "^ERROR:STRING_LIBC: strspn() is poisoned" "$W/out" &&+ [ "$(grep -c "^ERROR:STRING_LIBC" "$W/out")" = 2 ] &&+ grep -q "^ERROR:STRING_COPY: copy strings with strscpy() from <shirudo/string.h>, not stpcpy()" "$W/out" &&+ git -C "$R" checkout -q -- good.c &&+ put testing/helper/run.c "// SPDX-License-Identifier: BSD-2-Clause" "" \+ "size_t run(const char *s)" "{" \+ " return strspn(s, \"ab\");" "}" &&+ check --files testing/helper/run.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)" \diff --git a/testing/unit/chain.c b/testing/unit/chain.cindex 9a692733..e6e83f78 100644--- a/testing/unit/chain.c+++ b/testing/unit/chain.c@@ -125,7 +125,8 @@ static void chain_log_anchor_appends_a_systemd_record(void)EXPECT_TRUE(strstr(buf, "\"eventType\":\"nvpcr-init\"}}\n"));boot = strstr(buf, "\"bootId\":\"");ASSERT_TRUE(boot);- EXPECT_EQ(strspn(boot + 10, "0123456789abcdef"), 32);+ EXPECT_TRUE(hex_is_lower(boot + 10, 32));+ EXPECT_EQ(boot[42], '"');ASSERT_EQ(fstat(fd, &st), 0);EXPECT_EQ(st.st_mode & 07777, 0600);