Skip to content

eBPF-based security platform

treewide: compare strings without strncmp() and strncasecmp()

Daniel Borkmann44fa1bfbdb76
diff --git a/builtin-bpm.c b/builtin-bpm.c
index 32a9e49c..09630978 100644
--- a/builtin-bpm.c
+++ b/builtin-bpm.c
@@ -908,7 +908,7 @@ static int list_keys(bool json)
continue;
if (lstat(path, &st) || !S_ISREG(st.st_mode) ||
key_file_fingerprint(path, hex) ||
- strncasecmp(entry->d_name, hex, 64))
+ !str_after_prefix_nocase(entry->d_name, hex))
continue;
next = xreallocarray(fingerprints, cap, count + 1,
sizeof(*next));
diff --git a/testing/proofs/lean4/Shirudo/TreeOverlap.lean b/testing/proofs/lean4/Shirudo/TreeOverlap.lean
index 94221bc0..405c0825 100644
--- a/testing/proofs/lean4/Shirudo/TreeOverlap.lean
+++ b/testing/proofs/lean4/Shirudo/TreeOverlap.lean
@@ -58,8 +58,8 @@ def boundary : Str → Bool
| [] => true
| c :: _ => c == dot || c == dash
-/-- `strncmp(release, prefix, strlen(prefix))` finding no difference, and
-then the byte of the release after the prefix. -/
+/-- `str_after_prefix(release, prefix)` finding the prefix, and then the
+byte of the release after it. -/
def continues : Str → Str → Bool
| release, [] => boundary release
| [], _ :: _ => false
diff --git a/tools/gen-vmlinux-h/gen-vmlinux-h.c b/tools/gen-vmlinux-h/gen-vmlinux-h.c
index fef39d49..5e20d5fb 100644
--- a/tools/gen-vmlinux-h/gen-vmlinux-h.c
+++ b/tools/gen-vmlinux-h/gen-vmlinux-h.c
@@ -285,12 +285,12 @@ static int dump_kfuncs(struct btf_dump *d, struct btf *btf)
continue;
name = btf__name_by_offset(btf, t->name_off);
- if (strncmp(name, KFUNC_DECL_TAG, sizeof(KFUNC_DECL_TAG)) == 0) {
+ if (strcmp(name, KFUNC_DECL_TAG) == 0) {
err = ptr_array_push(ft, &kfuncs);
if (err)
goto out;
}
- if (strncmp(name, FASTCALL_DECL_TAG, sizeof(FASTCALL_DECL_TAG)) == 0) {
+ if (strcmp(name, FASTCALL_DECL_TAG) == 0) {
err = ptr_array_push(ft, &fastcalls);
if (err)
goto out;
diff --git a/tools/shirudo-build/bpm-build.c b/tools/shirudo-build/bpm-build.c
index ffa385aa..71dd002b 100644
--- a/tools/shirudo-build/bpm-build.c
+++ b/tools/shirudo-build/bpm-build.c
@@ -288,8 +288,7 @@ static int build_parse_selector_kv(const char *key, const char *value,
if (!key_len || key_len >= TREE_STRING_MAX)
return -EINVAL;
for (int i = 0; i < *n; i++) {
- if (strlen((*kvs)[i].key) == key_len &&
- !strncmp((*kvs)[i].key, key, key_len))
+ if (span_equals_str(key, key_len, (*kvs)[i].key))
return -EEXIST;
}
ret = tree_selector_kv_add(kvs, n, max, &kv);
@@ -809,12 +808,12 @@ void bpm_build_spec_free(struct bpm_build_spec *spec)
static bool build_within(const char *dir, const char *path)
{
char resolved[PATH_MAX];
- size_t n;
+ const char *below;
if (!realpath(path, resolved))
return false;
- n = strlen(dir);
- return !strncmp(dir, resolved, n) && (resolved[n] == '\0' || resolved[n] == '/');
+ below = str_after_prefix(resolved, dir);
+ return below && (*below == '\0' || *below == '/');
}
static int build_resolve_source(const struct bpm_build_spec *spec,
@@ -1051,9 +1050,9 @@ static int build_target_ok(const char *target)
static bool build_target_below(const char *dir, const char *path)
{
- size_t n = strlen(dir);
+ const char *below = str_after_prefix(path, dir);
- return !strncmp(path, dir, n) && path[n] == '/';
+ return below && *below == '/';
}
/*
@@ -2431,8 +2430,10 @@ static int __build_bpm_config_update(int argc, const char **argv,
if (ret)
break;
if (upatch_is_unload(&parsed) ||
- strncmp(parsed.hdr.name, expected_name,
- sizeof(parsed.hdr.name))) {
+ !span_equals_str(parsed.hdr.name,
+ strnlen(parsed.hdr.name,
+ sizeof(parsed.hdr.name)),
+ expected_name)) {
ret = -EINVAL;
upatch_close(&parsed);
break;
diff --git a/tools/shirudo-build/package.c b/tools/shirudo-build/package.c
index 64221588..9a02871c 100644
--- a/tools/shirudo-build/package.c
+++ b/tools/shirudo-build/package.c
@@ -636,13 +636,14 @@ static const char *uprobe_section_target(const char *sec, __u32 *kind)
{ "uretprobe.multi/", UPATCH_ATTACH_URETPROBE_MULTI },
{ "uretprobe.multi.s/", UPATCH_ATTACH_URETPROBE_MULTI },
};
- size_t i, n;
+ const char *target;
+ size_t i;
for (i = 0; i < ARRAY_SIZE(forms); i++) {
- n = strlen(forms[i].prefix);
- if (!strncmp(sec, forms[i].prefix, n) && sec[n]) {
+ target = str_after_prefix(sec, forms[i].prefix);
+ if (target && *target) {
*kind = forms[i].kind;
- return sec + n;
+ return target;
}
}
return NULL;
@@ -721,15 +722,16 @@ static bool default_attach_section(const char *sec)
"fentry/", "fentry.s/", "fexit/", "fexit.s/",
"lsm/", "lsm.s/", "tp_btf/",
};
- size_t i, n;
+ const char *target;
+ size_t i;
if (!sec)
return false;
if (sec[0] == '?')
sec++;
for (i = 0; i < ARRAY_SIZE(prefixes); i++) {
- n = strlen(prefixes[i]);
- if (!strncmp(sec, prefixes[i], n) && sec[n])
+ target = str_after_prefix(sec, prefixes[i]);
+ if (target && *target)
return true;
}
return false;
@@ -1122,11 +1124,10 @@ static bool is_syscall_section(const char *sec)
static const char *serializer_map_name(const struct bpf_program *p)
{
const char *name = bpf_program__name(p);
- size_t prefix_len = strlen(SERIALIZER_PREFIX);
- if (!name || strncmp(name, SERIALIZER_PREFIX, prefix_len) != 0)
+ if (!name || !str_skip_prefix(&name, SERIALIZER_PREFIX))
return NULL;
- return name + prefix_len;
+ return name;
}
static bool object_has_map(struct bpf_object *obj, const char *want)
@@ -1641,11 +1642,10 @@ static int build_prog_metadata(struct bpf_object *obj, __u32 nr_maps,
}
if (is_syscall_section(sec)) {
const char *prog_name = bpf_program__name(p);
- size_t prefix_len = strlen(TEST_RUN_PREFIX);
+ const char *id = prog_name;
- if (!prog_name || strncmp(prog_name, TEST_RUN_PREFIX,
- prefix_len) != 0 ||
- prog_name[prefix_len] == '\0') {
+ if (!id || !str_skip_prefix(&id, TEST_RUN_PREFIX) ||
+ !*id) {
pr_err("SEC(\"syscall\") program '%s' must be named %s<id>",
prog_name ?: "?", TEST_RUN_PREFIX);
err = -EINVAL;
diff --git a/util/bpm_oci.c b/util/bpm_oci.c
index f2a4b209..7f08e414 100644
--- a/util/bpm_oci.c
+++ b/util/bpm_oci.c
@@ -165,20 +165,18 @@ static size_t bpm_header(void *ptr, size_t size, size_t nmemb, void *ud)
return n;
memcpy(line, ptr, n);
line[n] = '\0';
- if (!strncasecmp(line, "WWW-Authenticate:", 17)) {
- v = line + 17;
+ v = line;
+ if (str_skip_prefix_nocase(&v, "WWW-Authenticate:")) {
while (isspace((__u8)*v))
v++;
if (strscpy(r->www_auth, v, sizeof(r->www_auth)) < 0)
r->www_auth[0] = '\0';
- } else if (!strncasecmp(line, "Content-Type:", 13)) {
- v = line + 13;
+ } else if (str_skip_prefix_nocase(&v, "Content-Type:")) {
while (isspace((__u8)*v))
v++;
v[strcspn(v, " ;\r\n")] = '\0';
bpm_copy_string(r->content_type, sizeof(r->content_type), v);
- } else if (!strncasecmp(line, "Location:", 9)) {
- v = line + 9;
+ } else if (str_skip_prefix_nocase(&v, "Location:")) {
while (isspace((__u8)*v))
v++;
v[strcspn(v, "\r\n")] = '\0';
@@ -567,12 +565,12 @@ static int curl_once(struct bpm_oci_client *c, const char *method,
static int parse_challenge(const char *header, char *realm, size_t rsz,
char *service, size_t ssz, char *scope, size_t zsz)
{
- const char *p;
+ const char *p = header;
const char *q;
- if (!header || strncasecmp(header, "Bearer", 6) != 0)
+ if (!p || !str_skip_prefix_nocase(&p, "Bearer"))
return -EINVAL;
- for (p = header + 6; *p;) {
+ while (*p) {
while (*p == ' ' || *p == ',')
p++;
if (str_skip_prefix(&p, "realm=\"")) {
@@ -761,12 +759,13 @@ static int get_memory(struct bpm_oci_client *c, const char *url,
if (ret)
goto out;
if (r.status == 401 && r.www_auth[0]) {
- if (!strncasecmp(r.www_auth, "Bearer", 6)) {
+ if (str_has_prefix_nocase(r.www_auth, "Bearer")) {
ret = get_bearer_token(c, r.www_auth);
if (!ret && strsprintf(auth, sizeof(auth), "Bearer %s",
c->token) < 0)
ret = -E2BIG;
- } else if (!strncasecmp(r.www_auth, "Basic", 5) && c->username[0]) {
+ } else if (str_has_prefix_nocase(r.www_auth, "Basic") &&
+ c->username[0]) {
char encoded[BPM_OCI_BASIC_ENCODED_SIZE];
ret = encode_basic(c->username, c->password, encoded, sizeof(encoded));
@@ -826,7 +825,8 @@ int bpm_oci_get_file(struct bpm_oci_client *c, const char *url,
if (ret)
return ret;
ret = curl_once(c, "GET", url, accept, NULL, &r, &sink, true);
- if (!ret && r.status == 401 && r.www_auth[0] && !strncasecmp(r.www_auth, "Bearer", 6)) {
+ if (!ret && r.status == 401 && r.www_auth[0] &&
+ str_has_prefix_nocase(r.www_auth, "Bearer")) {
ret = sink_restart(&sink, fd, (__u64)size);
if (!ret)
ret = get_bearer_token(c, r.www_auth);
@@ -839,7 +839,7 @@ int bpm_oci_get_file(struct bpm_oci_client *c, const char *url,
ret = curl_once(c, "GET", url, accept, auth, &r, &sink, true);
}
} else if (!ret && r.status == 401 && r.www_auth[0] &&
- !strncasecmp(r.www_auth, "Basic", 5) && c->username[0]) {
+ str_has_prefix_nocase(r.www_auth, "Basic") && c->username[0]) {
char encoded[BPM_OCI_BASIC_ENCODED_SIZE];
ret = sink_restart(&sink, fd, (__u64)size);
@@ -887,12 +887,13 @@ static int bpm_oci_request(struct bpm_oci_client *c, const char *method,
if (ret)
return ret;
if (r->status == 401 && r->www_auth[0]) {
- if (!strncasecmp(r->www_auth, "Bearer", 6)) {
+ if (str_has_prefix_nocase(r->www_auth, "Bearer")) {
ret = get_bearer_token(c, r->www_auth);
if (!ret && strsprintf(auth, sizeof(auth), "Bearer %s",
c->token) < 0)
ret = -E2BIG;
- } else if (!strncasecmp(r->www_auth, "Basic", 5) && c->username[0]) {
+ } else if (str_has_prefix_nocase(r->www_auth, "Basic") &&
+ c->username[0]) {
char encoded[BPM_OCI_BASIC_ENCODED_SIZE];
ret = encode_basic(c->username, c->password, encoded,
@@ -1233,7 +1234,7 @@ bool bpm_digest_matches(const char *want, const char *hex)
static int upload_location(const struct bpm_oci_client *c, const char *location,
char *out, size_t out_sz)
{
- size_t base_len = strlen(c->base_url);
+ const char *path;
if (!location || !*location)
return -EINVAL;
@@ -1242,8 +1243,8 @@ static int upload_location(const struct bpm_oci_client *c, const char *location,
return -ENAMETOOLONG;
return 0;
}
- if (strncmp(location, c->base_url, base_len) ||
- location[base_len] != '/')
+ path = str_after_prefix(location, c->base_url);
+ if (!path || *path != '/')
return -EPERM;
if (strlen(location) >= out_sz)
return -ENAMETOOLONG;
diff --git a/util/bpm_package.c b/util/bpm_package.c
index 70175879..1176f308 100644
--- a/util/bpm_package.c
+++ b/util/bpm_package.c
@@ -463,8 +463,8 @@ static int read_member_gz(gzFile gz, const char *member,
remain = size;
if (package_member_name_ok(name, member) &&
(!package_dir ||
- ((size_t)(strchr(name, '/') - name) == strlen(package_dir) &&
- !strncmp(name, package_dir, strlen(package_dir))))) {
+ span_equals_str(name, (size_t)(strchr(name, '/') - name),
+ package_dir))) {
size_t dir_len = (size_t)(strchr(name, '/') - name);
__u8 *body;
@@ -581,6 +581,7 @@ static int visit_members_gz(gzFile gz, const char *package_dir,
int n = gzread(gz, h, sizeof(h));
__u64 size, remain, pad;
size_t name_len, root_len;
+ const char *below;
bool zero = true;
int type;
@@ -624,9 +625,8 @@ static int visit_members_gz(gzFile gz, const char *package_dir,
memcpy(root, name, root_len);
root[root_len] = '\0';
}
- root_len = strlen(root);
- if (strncmp(name, root, root_len) ||
- (name[root_len] && name[root_len] != '/')) {
+ below = str_after_prefix(name, root);
+ if (!below || (*below && *below != '/')) {
ret = -EINVAL;
break;
}
@@ -638,8 +638,7 @@ static int visit_members_gz(gzFile gz, const char *package_dir,
ret = -EPERM;
break;
}
- strscpy(rel, name + root_len + (name[root_len] == '/'),
- sizeof(rel));
+ strscpy(rel, below + (*below == '/'), sizeof(rel));
if (!*rel) {
if (type != '5') {
ret = -EINVAL;
@@ -731,6 +730,7 @@ int bpm_pkg_extract(const char *archive, const char *dest)
char name[PATH_MAX], rel[PATH_MAX], path[PATH_MAX], *slash;
int n = gzread(gz, h, sizeof(h));
__u64 size, remain, pad;
+ const char *below;
bool zero = true;
int type;
@@ -776,11 +776,12 @@ int bpm_pkg_extract(const char *archive, const char *dest)
memcpy(root, name, slash - name);
root[slash - name] = 0;
}
- if (strncmp(name, root, strlen(root)) || name[strlen(root)] != '/') {
+ below = str_after_prefix(name, root);
+ if (!below || *below != '/') {
ret = -EINVAL;
break;
}
- strscpy(rel, name + strlen(root) + 1, sizeof(rel));
+ strscpy(rel, below + 1, sizeof(rel));
if ((!*rel && type != '5') || (*rel && !bpm_archive_rel_ok(rel))) {
ret = -EINVAL;
break;
diff --git a/util/dm_verity.c b/util/dm_verity.c
index 28c9d747..5f7d5355 100644
--- a/util/dm_verity.c
+++ b/util/dm_verity.c
@@ -261,7 +261,9 @@ static int check_table(union dm_buffer *buf,
if (target->sector_start ||
target->length != image->data_blocks *
(CONFIG_IMAGE_BLOCK_SIZE / 512) ||
- strncmp(target->target_type, "verity", sizeof(target->target_type)))
+ !span_equals(target->target_type,
+ strnlen(target->target_type,
+ sizeof(target->target_type)), "verity"))
return -EKEYREJECTED;
params = (char *)(target + 1);
avail = buf->io.data_size - offset - sizeof(*target);
diff --git a/util/io.c b/util/io.c
index da84da4f..a240d60e 100644
--- a/util/io.c
+++ b/util/io.c
@@ -253,9 +253,9 @@ int read_file(const char *path, size_t max, void *out, size_t *len)
static bool under(const char *path, const char *dir)
{
- size_t n = strlen(dir);
+ const char *below = str_after_prefix(path, dir);
- return !strncmp(path, dir, n) && path[n] == '/';
+ return below && *below == '/';
}
static bool trusted_owner(uid_t uid)
diff --git a/util/kernel_release.c b/util/kernel_release.c
index 8684b212..554e25b2 100644
--- a/util/kernel_release.c
+++ b/util/kernel_release.c
@@ -1,21 +1,19 @@
// SPDX-License-Identifier: BSD-2-Clause
/* Copyright (c) 2026 Authors of shirudo */
-#include <string.h>
+#include <shirudo/string.h>
#include "kernel_release.h"
bool kernel_release_prefix_matches(const char *release,
const char *prefix)
{
- size_t len;
+ const char *rest;
if (!release || !prefix)
return false;
- len = strlen(prefix);
- if (!len)
+ if (!*prefix)
return true;
- return !strncmp(release, prefix, len) &&
- (release[len] == '\0' || release[len] == '.' ||
- release[len] == '-');
+ rest = str_after_prefix(release, prefix);
+ return rest && (*rest == '\0' || *rest == '.' || *rest == '-');
}
diff --git a/util/options.c b/util/options.c
index c6c69539..c666ba3b 100644
--- a/util/options.c
+++ b/util/options.c
@@ -101,7 +101,7 @@ static int spec_find(const struct opt_parser *p, const char *arg,
for (i = 0; i < p->n_specs; i++) {
const char *name = p->specs[i].name;
- if (strlen(name) == len && !strncmp(name, arg, len))
+ if (span_equals_str(arg, len, name))
return (int)i;
}
return -1;
diff --git a/util/similar.c b/util/similar.c
index 3616e9a0..5dcc8376 100644
--- a/util/similar.c
+++ b/util/similar.c
@@ -8,6 +8,7 @@
#include <string.h>
#include <shirudo/misc.h>
+#include <shirudo/string.h>
#include "similar.h"
@@ -108,7 +109,8 @@ size_t resolve(const char *input,
const char *const *candidates, size_t n,
size_t *matches, size_t cap)
{
- size_t i, m = 0, len = strlen(input);
+ const char *more;
+ size_t i, m = 0;
for (i = 0; i < n; i++) {
if (strcmp(input, candidates[i]) == 0) {
@@ -118,10 +120,8 @@ size_t resolve(const char *input,
}
}
for (i = 0; i < n; i++) {
- size_t clen = strlen(candidates[i]);
-
- if (clen > len &&
- strncmp(input, candidates[i], len) == 0) {
+ more = str_after_prefix(candidates[i], input);
+ if (more && *more) {
if (m < cap)
matches[m++] = i;
}
@@ -129,10 +129,8 @@ size_t resolve(const char *input,
if (m)
return m;
for (i = 0; i < n; i++) {
- size_t clen = strlen(candidates[i]);
-
- if (clen < len &&
- strncmp(input, candidates[i], clen) == 0) {
+ more = str_after_prefix(input, candidates[i]);
+ if (more && *more) {
if (m < cap)
matches[m++] = i;
}
diff --git a/util/tls_openssl.c b/util/tls_openssl.c
index 3a6c6e17..2c41c03a 100644
--- a/util/tls_openssl.c
+++ b/util/tls_openssl.c
@@ -78,8 +78,8 @@ static bool tls_classical_host_allowed(CURL *curl)
if (len < sizeof(entry)) {
memcpy(entry, allowlist, len);
entry[len] = '\0';
- if (dns_name_valid(entry) && !strncasecmp(host, entry, len) &&
- host_len == len) {
+ if (dns_name_valid(entry) && host_len == len &&
+ str_after_prefix_nocase(host, entry)) {
allowed = true;
break;
}
diff --git a/util/upatch_maps_otel.c b/util/upatch_maps_otel.c
index 32bc255a..dafc633a 100644
--- a/util/upatch_maps_otel.c
+++ b/util/upatch_maps_otel.c
@@ -968,17 +968,16 @@ static size_t response_write(char *ptr, size_t size, size_t nmemb, void *ctx)
static size_t response_header(char *ptr, size_t size, size_t nmemb, void *ctx)
{
- static const char prefix[] = "Retry-After:";
struct otel_response *r = ctx;
- size_t n;
+ size_t n, skip;
if (check_mul_overflow(size, nmemb, &n))
return 0;
- if (n > sizeof(prefix) - 1 &&
- strncasecmp(ptr, prefix, sizeof(prefix) - 1) == 0) {
+ skip = span_has_prefix_nocase(ptr, n, "Retry-After:");
+ if (skip) {
char value[128];
- char *p = ptr + sizeof(prefix) - 1;
- size_t len = n - (sizeof(prefix) - 1);
+ char *p = ptr + skip;
+ size_t len = n - skip;
__u64 seconds;
while (len && isspace((__u8)*p)) {
diff --git a/util/upatch_pins.c b/util/upatch_pins.c
index d8eb25d3..10efcdb0 100644
--- a/util/upatch_pins.c
+++ b/util/upatch_pins.c
@@ -24,12 +24,11 @@
static bool pin_index(const char *entry, const char *prefix, __u32 *out)
{
- size_t n = strlen(prefix);
__u64 value;
- if (strncmp(entry, prefix, n))
+ entry = str_after_prefix(entry, prefix);
+ if (!entry)
return false;
- entry += n;
if (entry[0] == '0' && entry[1])
return false;
if (parse_u64(entry, strlen(entry), UINT32_MAX, &value))