eBPF-based security platform
testing: compare strings without strncmp() and strncasecmp()
Daniel Borkmann9e212868809d
diff --git a/testing/helper/test-conditions.c b/testing/helper/test-conditions.cindex 0244b529..a7d2a9f4 100644--- a/testing/helper/test-conditions.c+++ b/testing/helper/test-conditions.c@@ -101,8 +101,8 @@ int main(int argc, char **argv)in.pins_rc = -ENOENT;} else if (pins && !strcmp(pins, "error")) {in.pins_rc = -EACCES;- } else if (pins && !strncmp(pins, "unmeasured:", 11)) {- in.pins_detail = pins + 11;+ } else if (pins && str_skip_prefix(&pins, "unmeasured:")) {+ in.pins_detail = pins;in.pins_unmeasured = 1;for (pins = in.pins_detail; *pins; pins++)in.pins_unmeasured += *pins == ',';diff --git a/testing/helper/test-config-update.c b/testing/helper/test-config-update.cindex 42f24bec..3c70fa7d 100644--- a/testing/helper/test-config-update.c+++ b/testing/helper/test-config-update.c@@ -145,8 +145,9 @@ static int test_pcr_extend(void *opaque, __u32 pcr,static int test_prepare(void *opaque,const struct config_update_target *target){+ static const char slot_a[] = "/verified/config-A";+ size_t i, len = strlen(target->prefix);struct test_context *ctx = opaque;- size_t i;int ret;ctx->sim.prepares++;@@ -155,13 +156,12 @@ static int test_prepare(void *opaque,return ret;if (getenv("SHIRUDO_TEST_CONFIG_PREPARE_FAIL"))return -EKEYREJECTED;- if (strlen(target->prefix) != sizeof("/verified/config-A") - 1 ||- strncmp(target->prefix, "/verified/config-",- sizeof("/verified/config-") - 1))+ /* Any slot: the letter is what the root hash repeats. */+ if (len != sizeof(slot_a) - 1 ||+ memcmp(target->prefix, slot_a, len - 1))return -EKEYREJECTED;for (i = 0; i < sizeof(target->root_hash); i++)- if (target->root_hash[i] !=- (__u8)target->prefix[strlen(target->prefix) - 1])+ if (target->root_hash[i] != (__u8)target->prefix[len - 1])return -EKEYREJECTED;return 0;}diff --git a/testing/helper/test-keepalive-quote.c b/testing/helper/test-keepalive-quote.cindex 9e2b1bae..77d9a312 100644--- a/testing/helper/test-keepalive-quote.c+++ b/testing/helper/test-keepalive-quote.c@@ -12,6 +12,7 @@#include <string.h>#include <shirudo/misc.h>+#include <shirudo/string.h>#include "crypto.h"#include "hex.h"@@ -32,13 +33,12 @@ static int quote_values(char *quote, const char *values[QUOTE_FIELDS])size_t i;for (i = 0; i < QUOTE_FIELDS; i++) {- size_t n = strlen(keys[i]);char *end;- if (strncmp(field, keys[i], n))+ values[i] = str_after_prefix(field, keys[i]);+ if (!values[i])return -EINVAL;- values[i] = field + n;- end = strchr(field + n, ';');+ end = strchr(field, ';');if (i + 1 == QUOTE_FIELDS) {if (end)return -EINVAL;diff --git a/testing/helper/test-perf-attach.c b/testing/helper/test-perf-attach.cindex 21e46625..4b084bc2 100644--- a/testing/helper/test-perf-attach.c+++ b/testing/helper/test-perf-attach.c@@ -94,7 +94,7 @@ static int find_prog(const char *want, __u32 *id_out)attr.info.info = (__u64)(size_t)&info;if (!bpf(BPF_OBJ_GET_INFO_BY_FD, &attr) &&info.type == BPF_PROG_TYPE_KPROBE &&- (!want || !strncmp(info.name, want, sizeof(info.name)))) {+ (!want || !strcmp(info.name, want))) {*id_out = id;return fd;}diff --git a/testing/helper/test-register.c b/testing/helper/test-register.cindex d3495375..778cc232 100644--- a/testing/helper/test-register.c+++ b/testing/helper/test-register.c@@ -262,8 +262,7 @@ int main(int argc, char **argv)__u8 operation[32];__s64 seq = atoi(argv[2]);- if (strncmp(argv[5], "sha256:", 7) ||- hex_decode(argv[5] + 7, operation, 32))+ if (sha256_digest_decode(argv[5], operation))return 2;if (chain_tree_event(seq, operation, event))return 2;diff --git a/testing/proofs/cbmc/bpm-oci-challenge.c b/testing/proofs/cbmc/bpm-oci-challenge.cindex 89a70a1d..f59a4f21 100644--- a/testing/proofs/cbmc/bpm-oci-challenge.c+++ b/testing/proofs/cbmc/bpm-oci-challenge.c@@ -63,7 +63,7 @@ void harness(void)"every value terminated inside its buffer");if (ret)return;- __CPROVER_assert(!strncasecmp(header, "Bearer", 6),+ __CPROVER_assert(str_has_prefix_nocase(header, "Bearer"),"only a Bearer challenge");__CPROVER_assert(realm[0] && quoted_after(header, len, "realm", realm),"a realm, quoted in the challenge");diff --git a/testing/proofs/cbmc/bpm-tree-reader.c b/testing/proofs/cbmc/bpm-tree-reader.cindex 1a73b098..7439364e 100644--- a/testing/proofs/cbmc/bpm-tree-reader.c+++ b/testing/proofs/cbmc/bpm-tree-reader.c@@ -127,7 +127,7 @@ static bool hex_digits(const char *s)static bool sha256_named(const char *s){- return !strncmp(s, "sha256:", 7) && hex_digits(s + 7);+ return str_skip_prefix(&s, "sha256:") && hex_digits(s);}/* LEN operations, any parent, any base and head, the first at any seq. */diff --git a/testing/proofs/cbmc/bpm-tree-sync-model.c b/testing/proofs/cbmc/bpm-tree-sync-model.cindex 457a6d21..50433a9d 100644--- a/testing/proofs/cbmc/bpm-tree-sync-model.c+++ b/testing/proofs/cbmc/bpm-tree-sync-model.c@@ -411,8 +411,7 @@ int bpm_tree_fetch_publication(struct bpm_oci_client *c,int s = 0;if (!failure && fetches++)- s = served(strncmp(reference, "sha256:", 7) ? reference[0] :- reference[7]);+ s = served(reference[str_has_prefix(reference, "sha256:")]);*pub = *served_pub(s);if (failure)return -EIO;diff --git a/testing/proofs/cbmc/conditions-detail.c b/testing/proofs/cbmc/conditions-detail.cindex db2188be..d2451ef4 100644--- a/testing/proofs/cbmc/conditions-detail.c+++ b/testing/proofs/cbmc/conditions-detail.c@@ -48,8 +48,8 @@ void harness(void)__CPROVER_assert(!strcmp(c.detail, names),"the whole text where the detail holds it");else if (len < TEXT_MAX)- __CPROVER_assert(!strncmp(c.detail, FILLED ? FILLER : names,- room - 3) &&+ __CPROVER_assert(!memcmp(c.detail, FILLED ? FILLER : names,+ room - 3) &&!strcmp(c.detail + room - 3, "..."),"the start of a longer text and three dots");elsediff --git a/testing/proofs/cbmc/conditions-verdict.c b/testing/proofs/cbmc/conditions-verdict.cindex 846c70e9..61ae83e0 100644--- a/testing/proofs/cbmc/conditions-verdict.c+++ b/testing/proofs/cbmc/conditions-verdict.c@@ -169,7 +169,7 @@ static bool cut_to(const char *buf, const char *text, size_t size){size_t n = min(strlen(text), size - 1);- return !strncmp(buf, text, n) && !buf[n];+ return !memcmp(buf, text, n) && !buf[n];}void harness(void)diff --git a/testing/proofs/cbmc/config-auth.c b/testing/proofs/cbmc/config-auth.cindex 5adef7a2..83444f3d 100644--- a/testing/proofs/cbmc/config-auth.c+++ b/testing/proofs/cbmc/config-auth.c@@ -66,7 +66,7 @@ void harness(void)was = cfg.tpm_owner_auth[k];n = strlen(val);set_owner_auth(&cfg, val);- if (strncmp(val, "hex:", 4)) {+ if (!str_has_prefix(val, "hex:")) {if (n > AUTH_MAX) {__CPROVER_assert(cfg.tpm_owner_auth_len == had &&cfg.tpm_owner_auth[k] == was,diff --git a/testing/proofs/cbmc/image-path.c b/testing/proofs/cbmc/image-path.cindex 04fb928f..cbaf9b62 100644--- a/testing/proofs/cbmc/image-path.c+++ b/testing/proofs/cbmc/image-path.c@@ -15,7 +15,7 @@static bool component_is(const char *s, size_t n, const char *what){- return n == strlen(what) && !strncmp(s, what, n);+ return span_equals_str(s, n, what);}void harness(void)diff --git a/testing/proofs/cbmc/upatch-writer-prog-specs.c b/testing/proofs/cbmc/upatch-writer-prog-specs.cindex 60606caf..a4a68400 100644--- a/testing/proofs/cbmc/upatch-writer-prog-specs.c+++ b/testing/proofs/cbmc/upatch-writer-prog-specs.c@@ -601,16 +601,14 @@ static void uprobe_target(const struct prog_spec *s,/* The names of syms, in its order, each NUL-terminated in the record. */static void kprobe_syms(const struct prog_spec *s, const char *syms){- const char *want = syms;+ const char *want = syms, *rest;__u32 i;for (i = 0; i < s->nr_kprobe_syms; i++) {- size_t len = strlen(s->kprobe_syms[i]);-- __CPROVER_assert(*want && !strncmp(s->kprobe_syms[i], want, len) &&- (want[len] == ',' || !want[len]),+ rest = str_after_prefix(want, s->kprobe_syms[i]);+ __CPROVER_assert(*want && rest && (*rest == ',' || !*rest),"the names the section lists, sorted");- want += len + (want[len] == ',');+ want = rest + (*rest == ',');}__CPROVER_assert(s->nr_kprobe_syms && !*want, "every name the section lists");}