eBPF-based security platform
docs: say where bytes from outside become a string
Daniel Borkmanna54cb9e3c34b
diff --git a/.agents/skills/coding/SKILL.md b/.agents/skills/coding/SKILL.mdindex 43f3d405..35cddbfa 100644--- a/.agents/skills/coding/SKILL.md+++ b/.agents/skills/coding/SKILL.md@@ -179,6 +179,83 @@ a series of its own, not along the way.copies it to the stack, which a BPF program has 512 bytes of. A returnvalue beats an output parameter wherever one value comes back.+### Strings from outside++`strcmp()`, `strlen()`, `strchr()`, `strstr()`, `strcspn()` and their+kin read up to a terminator and take no length. The tree calls them+hundreds of times on strings it terminated itself, so no poison can+refuse them as it refuses the names `<shirudo/string.h>` replaces. What+makes one of these calls right is where its argument came from, and+nothing checks that but you.++Bytes from outside come with a length: a registry's or a collector's+reply, a token server's answer, a package and its members, an index or+a tree publication, a `.upatch` file and its sections, the settings+and labels of a config image, evidence, the eventlog and the other+sealed logs, a TPM, netlink or ioctl reply, an xattr value, `/proc`+and sysfs text, the kernel's config. A NUL among them ends a string+short of the bytes that were read, hashed or signed, so that two+readers take one input differently: `status` listed the eventlog up to+a NUL where the replay refused the whole log, and the registry client+took a token from the first bytes of an answer that was no text.++- Bytes become a string in one step that takes their length, refuses a+ NUL among them and terminates them. `span_to_str()` is that step for+ a file read whole, in place, as `read_file()` leaves room for the+ terminator, for a reply, a counted field and a line cut out of a+ text. `read_text_line()` is it for the lines of a settings file, and+ `json_parse()` and `json_walk()` for a document, whose strings they+ hand out decoded and terminated.+- A buffer `read()`, `pread()`, `recv()`, `readlink()` or `getxattr()`+ filled has no terminator until the code writes one.+- A fixed-size field of a format is a string only once its parser found+ a NUL inside it, `memchr(field, '\0', sizeof(field))`, as+ `parse_header()` of `util/upatch.c` does before anything reads the+ name. Two such fields are compared whole, with `memcmp()` over their+ size, as the signed name is with the header's.+- A pointer with a length stays one where it can: `memchr()`,+ `memcmp()`, `span_equals()`, `name_has_suffix()`, `digits()` and+ `parse_u64()` read no further than they are told. Copy it out only+ for a callee that needs a terminator.+- A log of records is walked by the length of its lines, or refused+ whole for a NUL, and a last line without its newline is no record+ (`lines_len()`).+- A name from outside is checked once, by the function that owns its+ rule (`upatch_name_valid()`, `bpm_name_ok()`, `dns_name_valid()`),+ before it reaches a path, a digest or a strict JSON writer; until+ then it is printed through `text_sanitize()` or a lossy writer.+- A digest over several strings delimits or counts each: a tag with its+ NUL, then the fixed-size fields, then at most one field of any length,+ as `chain_upatch_event()` hashes, or one canonical document, as+ `tree_operation_digest()` does. Two strings set side by side hash+ like one string cut elsewhere.++Instead of++```c+ret = read_file(path, max, &body, &len);+if (ret)+ return ret;+entry = strstr(body, needle);+```++write++```c+ret = read_file(path, max, &body, &len);+if (ret)+ return ret;+ret = span_to_str(body, len + 1, body, len);+if (ret)+ goto out;+entry = strstr(body, needle);+```++The `read_unterminated` and `file_as_string` rules of+`testing/coccinelle` report the first of these and a buffer a read left+unterminated; a field, a pointer with a length and a digest are for+review.+### State that changes under you- Read a value that can change under you once, into a local, and use@@ -318,6 +395,7 @@ already, or should. The basics:| A string's prefix, or a span that must be one literal | `str_has_prefix()` to test a prefix, `str_skip_prefix()` to step past it, `span_equals()` for a span of known length (`<shirudo/string.h>`); never `strncmp()` with a length counted by hand || 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`) || Sizes and lengths | `check_add_overflow()` and kin, `size_add()`, `size_mul()`, `struct_size()`, `DIV_ROUND_UP()` (`<shirudo/overflow.h>`) || Smaller or larger | `min()`, `max()` (`<shirudo/misc.h>`) || Memory | `xmalloc()`, `xcalloc()`, `xreallocarray()`, `xstrndup()`, `xasprintf()`, `xfree(p, size)` (`util/malloc.h`) |diff --git a/.agents/skills/security-audit/SKILL.md b/.agents/skills/security-audit/SKILL.mdindex a6290f2f..3c5d6a37 100644--- a/.agents/skills/security-audit/SKILL.md+++ b/.agents/skills/security-audit/SKILL.md@@ -205,6 +205,27 @@ those shapes are absent, not the class.member or section, shirudo's reading of a loader, BTF or tar againstthe kernel's or the operator's, padding or uninitialized bytes inwhat is hashed, signed or measured.+- **Bytes as strings.** Bytes that came with a length handed to a+ function that reads up to a terminator: `strcmp()`, `strlen()`,+ `strchr()`, `strstr()`, `strcspn()`, `strtok_r()`, a `%s`. For each+ place bytes enter, settle where the length comes from and whether a+ terminator is written before such a function runs, an over-read+ otherwise; what a NUL among the bytes does, refused or the string+ ends short, so that a digest or a signature covers bytes a compare+ never saw, a name on disk is not the name checked, or the kernel+ reads what shirudo did not; whether a fixed-size field is compared+ whole or up to a terminator that need not be there; whether a digest+ over several strings delimits or counts each; whether a length is+ narrowed before it bounds a copy or a compare; and whether a name is+ checked once before it is printed, hashed or put in a path. Bytes+ become a string through `span_to_str()`, `read_text_line()` or+ `json_parse()`, and a field through the `memchr()` of its parser:+ look for the reader that has none of them, and for a strict JSON+ writer given text nobody checked, which fails the whole document. The+ `read_unterminated` and `file_as_string` rules match a buffer a read+ left unterminated and a file searched as a string; a field whose NUL+ another function checked and a copy terminated by hand they do not+ see.- **Hook bypass.** Another syscall reaching what a hook refuses: bothrename endpoints, hard links, truncate, xattrs, idmapped mounts, adescriptor opened before the mark, `bool kernel` misread. Take thediff --git a/AGENTS.md b/AGENTS.mdindex 21b4ae70..f9b0a522 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -121,6 +121,17 @@ commit message that carries a change.`util/json.c` for all JSON and `pr_*()` for all logging. The libc namesthey replace are poisoned. 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 one+ step that refuses a NUL among them and terminates them:+ `span_to_str()` for a file read whole, a reply, a counted field or a+ line cut from a text, `read_text_line()` for the lines of a settings+ file, `json_parse()` for a document. A fixed-size field of a format+ is a string once its parser found a NUL inside it. `strcmp()`,+ `strlen()`, `strchr()` and their kin, which no poison can refuse, are+ for what passed that step, for literals, `argv` and paths; never for+ a buffer a `read()` filled, a field nobody checked or a pointer that+ came with a length. A digest over several strings delimits or counts+ each.- `ret` names a return code and `err` an errno kept beside the value afunction returns. No `(void)` casts: a result that must be checkedcomes from a `__must_check` function, and every call checks it.diff --git a/Documentation/contribute/code/coding-style.rst b/Documentation/contribute/code/coding-style.rstindex f4a4e7dc..195000d6 100644--- a/Documentation/contribute/code/coding-style.rst+++ b/Documentation/contribute/code/coding-style.rst@@ -21,6 +21,14 @@ its BPF programs, with these rules of its own, which- 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.+- Take bytes from outside the program as a string with+ ``span_to_str()`` from ``<shirudo/string.h>``, which takes their+ length and refuses a NUL among them, with ``read_text_line()`` for a+ line of a settings file, or with ``json_parse()``. ``strcmp()``,+ ``strlen()``, ``strchr()`` and their kin read up to a terminator: they+ 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.- 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