Skip to content

eBPF-based security platform

string: mark strscpy() __must_check

Daniel Borkmann02da692fc9ad
diff --git a/.agents/skills/coding/SKILL.md b/.agents/skills/coding/SKILL.md
index 3fa84a8f..10fb3703 100644
--- a/.agents/skills/coding/SKILL.md
+++ b/.agents/skills/coding/SKILL.md
@@ -390,7 +390,7 @@ already, or should. The basics:
| Need | Use |
|---|---|
| Parse a number | `parse_u64()`, `parse_s64()`, `parse_x64()`, `digits()` (`<shirudo/string.h>`) |
-| Copy or format a string | `strscpy()`, `strsprintf()`, `vstrsprintf()`, and check `-E2BIG` |
+| Copy or format a string | `strscpy()`, `strsprintf()`, `vstrsprintf()`, and check `-E2BIG`; `strscpy_literal()` for a string literal into an array, which fails the build where it does not fit (`<shirudo/string.h>`) |
| 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, 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 |
@@ -448,9 +448,11 @@ without a body.
`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 or
- value is refused, a helper formatting display text returns the error,
- and status shows `"?"`. Constant text is copied, not formatted.
+- A `strsprintf()` that does not fit leaves the buffer empty and a
+ `strscpy()` leaves a cut copy: either way a path or value is refused,
+ a helper formatting display text returns the error, and status shows
+ `"?"`. Constant text is copied, not formatted, and a literal copied
+ into an array takes `strscpy_literal()`, which cannot fail.
- `xstrdup()` measures with `strlen()`, so outside input gets
`xstrndup(s, bound)`. `xfree(p, size)` and `xstrfree()` scrub before
they free.
diff --git a/AGENTS.md b/AGENTS.md
index a5343ba9..71483171 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -116,10 +116,11 @@ 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, `str_has_prefix()`, `str_after_prefix()` and `span_equals()`
- 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()`,
+ checked and `strscpy_literal()` for a literal into an array,
+ `str_has_prefix()`, `str_after_prefix()` and `span_equals()` 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, and so are the string functions of libc
diff --git a/Documentation/contribute/code/coding-style.rst b/Documentation/contribute/code/coding-style.rst
index 774be8d6..32c3b26f 100644
--- a/Documentation/contribute/code/coding-style.rst
+++ b/Documentation/contribute/code/coding-style.rst
@@ -19,7 +19,10 @@ its BPF programs, with these rules of its own, which
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()``,
+ ``strsprintf()``, and check their results: both are
+ ``__must_check``. Copy a string literal into an array with
+ ``strscpy_literal()``, which fails the build where the literal does
+ not fit and so has no result to check. ``strcpy()``, ``strncpy()``,
``sprintf()``, ``snprintf()`` and their kin are refused.
- Take bytes from outside the program as a string with
``span_to_str()`` from ``<shirudo/string.h>``, which takes their
diff --git a/include/shirudo/string.h b/include/shirudo/string.h
index 95725e4c..41a7db40 100644
--- a/include/shirudo/string.h
+++ b/include/shirudo/string.h
@@ -41,9 +41,10 @@ int parse_s64(const char *s, size_t len, __s64 min, __s64 max, __s64 *out);
/*
* Copy the string src into the size bytes at dst, terminated whenever
* size is not 0: its length, or -E2BIG when it did not fit and dst holds
- * as much of it as did.
+ * as much of it as did. A string literal copied into an array takes
+ * strscpy_literal() below, which cannot fail.
*/
-ssize_t strscpy(char *dst, const char *src, size_t size);
+ssize_t strscpy(char *dst, const char *src, size_t size) __must_check;
/*
* Copy the string literal lit, terminator and all, into the array dst.