eBPF-based security platform
string: test a file name's suffix in one place
Daniel Borkmann0ab344ba2bf1
diff --git a/.agents/skills/coding/SKILL.md b/.agents/skills/coding/SKILL.mdindex 7ba9202c..14789bf0 100644--- a/.agents/skills/coding/SKILL.md+++ b/.agents/skills/coding/SKILL.md@@ -100,6 +100,7 @@ extra check left to the caller, and move one found in a builtin into|---|---|| 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 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()` || Sizes and lengths | `check_add_overflow()` and kin, `size_add()`, `size_mul()`, `struct_size()` (`<shirudo/overflow.h>`) || Smaller or larger | `min()`, `max()` (`<shirudo/misc.h>`) |diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rstindex eef3e766..7276f30e 100644--- a/Documentation/contribute/testing/proof-coverage.rst+++ b/Documentation/contribute/testing/proof-coverage.rst@@ -57,6 +57,11 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.- Strings and buffers up to 8 bytes- Terminates what it writes; the whole string and its length, orwhat fits and ``-E2BIG``+ * - ``string-suffix``+ - ``name_has_suffix()``+ - Names up to 6 bytes, suffixes up to 3+ - True exactly when the name's last bytes are the suffix and at+ least one byte comes before them* - ``hex``- ``hex_encode()``, ``hex_decode()``, ``hex_decode_max()``- Up to 4 bytes, strings up to 9 charactersdiff --git a/Makefile.shirudo b/Makefile.shirudoindex c2d65793..3d09ff7e 100644--- a/Makefile.shirudo+++ b/Makefile.shirudo@@ -814,7 +814,7 @@ testing/helper/test-upatch-map-types: testing/helper/test-upatch-map-types.c$(Q)$(CC) $(CFLAGS) -o $@ $^testing/helper/test-upatch-prog-metadata: testing/helper/test-upatch-prog-metadata.c \- util/upatch.o util/malloc.o+ util/upatch.o util/malloc.o util/string.o@printf ' CC $@\n'$(Q)$(CC) $(CFLAGS) -o $@ $^diff --git a/builtin-bpm.c b/builtin-bpm.cindex efb902ed..ed24e51e 100644--- a/builtin-bpm.c+++ b/builtin-bpm.c@@ -137,8 +137,7 @@ static bool bee_title_ok(const char *title){size_t n = strlen(title);- return n > strlen(".bee") && bpm_package_name_ok(title) &&- !strcmp(title + n - strlen(".bee"), ".bee");+ return bpm_package_name_ok(title) && name_has_suffix(title, n, ".bee");}/*@@ -862,7 +861,7 @@ static int key_source_file(const char *source, char *temp, size_t temp_sz)unlink(archive);return ret;}- if (len >= 4 && !strcmp(source + len - 4, ".bee"))+ if (name_has_suffix(source, len, ".bee"))return extract_package_cert(source, temp, temp_sz);return 0;}@@ -1574,7 +1573,7 @@ static int cmd_bpm_config(int argc, const char **argv)size_t n = strlen(e->d_name);char name[NAME_MAX + 1];- if (n <= 5 || strcmp(e->d_name + n - 5, ".conf") ||+ if (!name_has_suffix(e->d_name, n, ".conf") ||strsprintf(name, sizeof(name), "%.*s",(int)(n - 5), e->d_name) < 0)continue;@@ -2970,7 +2969,7 @@ int cmd_bpm(int argc, const char **argv)size_t len = strlen(entry->d_name);char source[BPM_MAX_NAME];- if (len <= 5 || strcmp(entry->d_name + len - 5, ".conf"))+ if (!name_has_suffix(entry->d_name, len, ".conf"))continue;if (strsprintf(source, sizeof(source), "%.*s",(int)(len - 5), entry->d_name) < 0) {diff --git a/builtin-status.c b/builtin-status.cindex 4fea2e43..511dc0d6 100644--- a/builtin-status.c+++ b/builtin-status.c@@ -53,6 +53,7 @@#include "util/sysinfo.h"#include "util/timefmt.h"#include "util/tpm.h"+#include "util/upatch.h"#include "util/upatch_maps.h"#include "util/upatch_maps_otel.h"#include "util/tamper.h"@@ -132,11 +133,7 @@ static int count_dir(const char *dir, bool upatch_only)if (n < 0)return -1; /* directory absent */for (i = 0; i < n; i++) {- const char *nm = list[i]->d_name;- size_t len = strlen(nm);-- if (!upatch_only ||- (len > 7 && strcmp(nm + len - 7, ".upatch") == 0))+ if (!upatch_only || upatch_select(list[i]))count++;libc_free(list[i]);}diff --git a/builtin-upatch.c b/builtin-upatch.cindex bb7b2064..86c17c8d 100644--- a/builtin-upatch.c+++ b/builtin-upatch.c@@ -482,13 +482,6 @@ out:return ret;}-static int is_upatch_file(const struct dirent *e)-{- size_t len = strlen(e->d_name);-- return len > 7 && strcmp(e->d_name + len - 7, ".upatch") == 0;-}-static int is_not_dot(const struct dirent *e){return strcmp(e->d_name, ".") && strcmp(e->d_name, "..");@@ -542,7 +535,7 @@ static int cmd_upatch_list(int argc, const char **argv)}}- np = scandir(UPATCHES_DIR, &persisted, is_upatch_file,+ np = scandir(UPATCHES_DIR, &persisted, upatch_select,alphasort);if (np < 0)np = 0;diff --git a/include/shirudo/string.h b/include/shirudo/string.hindex 8fa7194e..b0cb4c06 100644--- a/include/shirudo/string.h+++ b/include/shirudo/string.h@@ -2,9 +2,9 @@/** Copyright (c) 2026 Authors of shirudo*- * Bounded number parsing, string copies and formatting, DNS names,- * UTF-8 characters and text for a terminal, from util/string.c, and the- * scrub and compare for secrets.+ * Bounded number parsing, string copies and formatting, file name+ * suffixes, DNS names, UTF-8 characters and text for a terminal, from+ * util/string.c, and the scrub and compare for secrets.*/#ifndef SHIRUDO_STRING_H@@ -53,6 +53,12 @@ int strsprintf(char *buf, size_t size, const char *fmt, ...)int vstrsprintf(char *buf, size_t size, const char *fmt, va_list ap)__printf(3, 0) __must_check;+/*+ * Whether the len bytes at name are a stem of at least one byte and then+ * suffix, as a file name and its extension are.+ */+bool name_has_suffix(const char *name, size_t len, const char *suffix);+/** A DNS host name: labels of letters, digits and inner hyphens, 1 to 63* bytes each and 253 in all. A name whose labels are all decimal or 0xdiff --git a/testing/proofs/cbmc/bpm-upatch-member.c b/testing/proofs/cbmc/bpm-upatch-member.cindex a505744e..6b0608b1 100644--- a/testing/proofs/cbmc/bpm-upatch-member.c+++ b/testing/proofs/cbmc/bpm-upatch-member.c@@ -8,9 +8,15 @@*/#include <stdbool.h>+#include <zlib.h>+#include <openssl/bio.h>+#include <openssl/evp.h>+#include <openssl/pem.h>+#include <openssl/x509.h>#define PROOF_MEMCPY_BYTEWISE#include "proof.h"+#include "../../../util/string.c"#include "../../../util/bpm_package.c"static bool ends_with(const char *s, size_t len, const char *suffix)diff --git a/testing/proofs/cbmc/proofs.list b/testing/proofs/cbmc/proofs.listindex 14fdf49d..94692311 100644--- a/testing/proofs/cbmc/proofs.list+++ b/testing/proofs/cbmc/proofs.list@@ -3,6 +3,7 @@string-number --unwind 24string-utf8 --unwind 10string-copy --unwind 10+string-suffix --unwind 8string-sanitize --unwind 8 LEN=0-6hex --unwind 12io-lines --unwind 10diff --git a/testing/proofs/cbmc/string-suffix.c b/testing/proofs/cbmc/string-suffix.cnew file mode 100644index 00000000..71321d46--- /dev/null+++ b/testing/proofs/cbmc/string-suffix.c@@ -0,0 +1,38 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * name_has_suffix() of util/string.c on every name of up to LEN_MAX bytes+ * and every suffix of up to SUFFIX_MAX: true exactly when the name is a+ * stem of at least one byte and then the suffix, reading only the name's+ * len bytes.+ */++#include <stdbool.h>++#include "proof.h"+#include "../../../util/string.c"++#define LEN_MAX 6+#define SUFFIX_MAX 3++void harness(void)+{+ bool ret, differ = false;+ char *name, *suffix;+ size_t len, n, i;++ __CPROVER_assume(len <= LEN_MAX && n <= SUFFIX_MAX);+ name = any_bytes(len);+ suffix = any_bytes(n + 1);+ __CPROVER_assume(!suffix[n] && strnlen(suffix, n) == n);+ ret = name_has_suffix(name, len, suffix);+ if (len <= n) {+ __CPROVER_assert(!ret, "never without a stem of a byte or more");+ return;+ }+ for (i = 0; i < n; i++)+ if (name[len - n + i] != suffix[i])+ differ = true;+ __CPROVER_assert(ret == !differ, "the name's last bytes are the suffix");+}diff --git a/testing/unit/string.c b/testing/unit/string.cindex ac552e4d..72bdac31 100644--- a/testing/unit/string.c+++ b/testing/unit/string.c@@ -478,6 +478,17 @@ static void memneq_explicit_tells_every_bit_apart(void)}}+static void name_has_suffix_wants_a_stem(void)+{+ EXPECT_TRUE(name_has_suffix("a.upatch", 8, ".upatch"));+ EXPECT_TRUE(name_has_suffix("a.upatch.x", 8, ".upatch"));+ EXPECT_FALSE(name_has_suffix(".upatch", 7, ".upatch"));+ EXPECT_FALSE(name_has_suffix("a.upatch.bak", 12, ".upatch"));+ EXPECT_FALSE(name_has_suffix("a.UPATCH", 8, ".upatch"));+ EXPECT_FALSE(name_has_suffix("", 0, ""));+ EXPECT_TRUE(name_has_suffix("a", 1, ""));+}+static const struct unit_case cases[] = {UNIT_CASE(digits_counts_leading_digits),UNIT_CASE(decimal_fills_its_span),@@ -500,6 +511,7 @@ static const struct unit_case cases[] = {UNIT_CASE(text_sanitize_passes_all_but_the_controls),UNIT_CASE(memzero_explicit_clears_its_span_only),UNIT_CASE(memneq_explicit_tells_every_bit_apart),+ UNIT_CASE(name_has_suffix_wants_a_stem),{}};diff --git a/tools/shirudo-build/bpm-build.c b/tools/shirudo-build/bpm-build.cindex 36ece70c..9b812bd8 100644--- a/tools/shirudo-build/bpm-build.c+++ b/tools/shirudo-build/bpm-build.c@@ -1129,13 +1129,6 @@ static int build_entry_name(char *out, size_t cap, const char *top,return 0;}-static bool build_ends_with(const char *s, const char *suffix)-{- size_t n = strlen(s), m = strlen(suffix);-- return n >= m && !strcmp(s + n - m, suffix);-}-static void build_upatch_name(const char *source, char *out, size_t cap){const char *base = strrchr(source, '/');@@ -1289,7 +1282,7 @@ static int build_upatches(struct bpm_build_spec *spec, const char *pkey,return -ENAMETOOLONG;strscpy(u->load_path, load, sizeof(u->load_path));strscpy(u->unload_path, unload, sizeof(u->unload_path));- if (build_ends_with(source, ".bpf.c")) {+ if (name_has_suffix(source, strlen(source), ".bpf.c")) {if (!vmlinux_h) {vmlinux_h = build_locate_vmlinux(NULL);if (!vmlinux_h) {@@ -1309,8 +1302,7 @@ static int build_upatches(struct bpm_build_spec *spec, const char *pkey,}bpf_o = object;src_path = source;- } else if (!build_ends_with(source, ".bpf.o") &&- !build_ends_with(source, ".o")) {+ } else if (!name_has_suffix(source, strlen(source), ".o")) {pr_err("bpm build: %s is neither .bpf.c nor .bpf.o", u->source);return -EINVAL;}diff --git a/tools/shirudo-build/shirudo-build.c b/tools/shirudo-build/shirudo-build.cindex 53d8fa6c..56fe0670 100644--- a/tools/shirudo-build/shirudo-build.c+++ b/tools/shirudo-build/shirudo-build.c@@ -98,21 +98,13 @@ static const char *derive_name(const char *input, char *buf, size_t buf_sz)return buf;}-static int ends_with(const char *s, const char *suf)-{- size_t ns = strlen(s);- size_t nf = strlen(suf);-- return ns >= nf && strcmp(s + ns - nf, suf) == 0;-}-/* foo/bar.bpf.c lands at foo/bar.bpf.upatch beside its input. */static int derive_output(const char *input, char *buf, size_t buf_sz){size_t stem = strlen(input);- if (stem > 2 && (strcmp(input + stem - 2, ".c") == 0 ||- strcmp(input + stem - 2, ".o") == 0))+ if (name_has_suffix(input, stem, ".c") ||+ name_has_suffix(input, stem, ".o"))stem -= 2;if (strsprintf(buf, buf_sz, "%.*s.upatch", (int)stem, input) < 0)@@ -125,7 +117,7 @@ static int derive_artifact_o(const char *output, char *buf, size_t buf_sz){size_t stem = strlen(output);- if (stem > 7 && strcmp(output + stem - 7, ".upatch") == 0)+ if (name_has_suffix(output, stem, ".upatch"))stem -= 7;if (strsprintf(buf, buf_sz, "%.*s.o", (int)stem, output) < 0)@@ -375,7 +367,7 @@ int main(int argc, char **argv)pr_info("writing %s (no -o given)", output);}- if (ends_with(input, ".bpf.c")) {+ if (name_has_suffix(input, strlen(input), ".bpf.c")) {const char *vmlinux_h =build_locate_vmlinux(vmlinux_override);if (!vmlinux_h) {@@ -411,7 +403,7 @@ int main(int argc, char **argv)bpf_o = temp_bpf_o;}src_path = input; /* anchors relative decoder paths */- } else if (ends_with(input, ".bpf.o") || ends_with(input, ".o")) {+ } else if (name_has_suffix(input, strlen(input), ".o")) {bpf_o = input; /* decoder paths resolve from the cwd */} else {pr_err("input must be .bpf.c or .bpf.o");diff --git a/util/bpm_package.c b/util/bpm_package.cindex 5b0f1d75..a0fd30f6 100644--- a/util/bpm_package.c+++ b/util/bpm_package.c@@ -339,20 +339,13 @@ bool bpm_archive_rel_ok(const char *name)return true;}-static bool has_suffix(const char *name, size_t len, const char *suffix)-{- size_t n = strlen(suffix);-- return len > n && !memcmp(name + len - n, suffix, n);-}-enum bpm_upatch_member bpm_upatch_member(const char *name, size_t len){if (!len || name[0] == '.' || memchr(name, '\0', len))return BPM_UPATCH_OTHER;- if (has_suffix(name, len, ".unload.upatch"))+ if (name_has_suffix(name, len, ".unload.upatch"))return BPM_UPATCH_UNLOAD;- if (has_suffix(name, len, ".upatch"))+ if (name_has_suffix(name, len, ".upatch"))return BPM_UPATCH_LOAD;return BPM_UPATCH_OTHER;}diff --git a/util/keys.c b/util/keys.cindex ac1a72f3..46dd4cc8 100644--- a/util/keys.c+++ b/util/keys.c@@ -349,9 +349,7 @@ static int set_add(struct trusted_set *set, const char *path,static int select_der(const struct dirent *e){- size_t n = strlen(e->d_name);-- return n > 4 && strcmp(e->d_name + n - 4, ".der") == 0;+ return name_has_suffix(e->d_name, strlen(e->d_name), ".der");}static size_t count_der(const char *dir)diff --git a/util/string.c b/util/string.cindex 77df0812..50356be6 100644--- a/util/string.c+++ b/util/string.c@@ -135,6 +135,13 @@ static bool numeric_dns_label(const char *label, size_t len)return !hex || i > 2;}+bool name_has_suffix(const char *name, size_t len, const char *suffix)+{+ size_t n = strlen(suffix);++ return len > n && !memcmp(name + len - n, suffix, n);+}+bool dns_name_valid(const char *name){size_t len = strlen(name), label_len = 0, label_start = 0;diff --git a/util/upatch.c b/util/upatch.cindex 98add6de..9a950ede 100644--- a/util/upatch.c+++ b/util/upatch.c@@ -15,6 +15,7 @@#include <shirudo/misc.h>#include <shirudo/overflow.h>+#include <shirudo/string.h>#include "malloc.h"#include "upatch.h"@@ -525,9 +526,7 @@ bool upatch_name_valid(const char *name)int upatch_select(const struct dirent *e){- size_t n = strlen(e->d_name);-- return n > 7 && strcmp(e->d_name + n - 7, ".upatch") == 0;+ return name_has_suffix(e->d_name, strlen(e->d_name), ".upatch");}void prog_specs_free(struct prog_spec *specs, __u32 nr_progs)