Skip to content

eBPF-based security platform

proofs: take a compared literal's length from the literal

Daniel Borkmannf91a67961895
diff --git a/testing/proofs/cbmc/bpm-beefile-string.c b/testing/proofs/cbmc/bpm-beefile-string.c
index 34657355..2b086a09 100644
--- a/testing/proofs/cbmc/bpm-beefile-string.c
+++ b/testing/proofs/cbmc/bpm-beefile-string.c
@@ -45,6 +45,7 @@ static bool blank(char c)
*/
static int quoted(const char *v, char *d)
{
+ static const char escapes[] = "nrt\\\"";
size_t from = 0, to = LEN, i;
int n = 0;
char c;
@@ -62,7 +63,8 @@ static int quoted(const char *v, char *d)
return -1;
if (c == '\\' && v[from] == '"') {
c = v[++i];
- if (i == to - 1 || !memchr("nrt\\\"", c, 5))
+ if (i == to - 1 ||
+ !memchr(escapes, c, sizeof(escapes) - 1))
return -1;
c = c == 'n' ? '\n' : c == 'r' ? '\r' : c == 't' ? '\t' : c;
}
diff --git a/testing/proofs/cbmc/bpm-oci-descriptor.c b/testing/proofs/cbmc/bpm-oci-descriptor.c
index 9e282495..158b267a 100644
--- a/testing/proofs/cbmc/bpm-oci-descriptor.c
+++ b/testing/proofs/cbmc/bpm-oci-descriptor.c
@@ -23,7 +23,7 @@ static bool sha256_digest(const char *d)
{
size_t i;
- if (memcmp(d, "sha256:", 7) || d[71])
+ if (!str_has_prefix(d, "sha256:") || d[71])
return false;
for (i = 7; i < 71; i++)
if (!hex_digit(d[i]))
diff --git a/testing/proofs/cbmc/bpm-oci-strings.c b/testing/proofs/cbmc/bpm-oci-strings.c
index 80a26eba..3b7442b2 100644
--- a/testing/proofs/cbmc/bpm-oci-strings.c
+++ b/testing/proofs/cbmc/bpm-oci-strings.c
@@ -75,7 +75,7 @@ static char lower(char c)
static void digest_matches(void)
{
char *want = any_bytes(STR_MAX + 8), *hex = any_string();
- bool same = !memcmp(want, "sha256:", 7);
+ bool same = str_has_prefix(want, "sha256:");
size_t i;
__CPROVER_assume(!want[STR_MAX + 7]);
diff --git a/testing/proofs/cbmc/bpm-tar-writer.c b/testing/proofs/cbmc/bpm-tar-writer.c
index c3a6817e..7ba1c21b 100644
--- a/testing/proofs/cbmc/bpm-tar-writer.c
+++ b/testing/proofs/cbmc/bpm-tar-writer.c
@@ -83,8 +83,9 @@ static void header_reads_back(const __u8 *h)
octal_field(h, 108, 8, 0) &&
octal_field(h, 116, 8, 0) && octal_field(h, 136, 12, 0),
"the permission bits, uid, gid and mtime 0");
- __CPROVER_assert(!memcmp(h + 257, "ustar", 6) &&
- !memcmp(h + 263, "00", 2), "a ustar header");
+ __CPROVER_assert(!memcmp(h + 257, "ustar", sizeof("ustar")) &&
+ !memcmp(h + 263, "00", sizeof("00") - 1),
+ "a ustar header");
}
/*
diff --git a/testing/proofs/cbmc/bpm-tree-state-match.c b/testing/proofs/cbmc/bpm-tree-state-match.c
index 6e92a65c..13c2b242 100644
--- a/testing/proofs/cbmc/bpm-tree-state-match.c
+++ b/testing/proofs/cbmc/bpm-tree-state-match.c
@@ -135,8 +135,10 @@ FILE *fdopen(int fd, const char *mode)
FILE *fopen(const char *path, const char *mode)
{
+ static const char labels[] = "/etc/shirudo/node.labels";
+
__CPROVER_assert(!IMAGE_ACTIVE &&
- !memcmp(path, "/etc/shirudo/node.labels", 25),
+ !memcmp(path, labels, sizeof(labels)),
"the labels of /etc without a config image");
if (open_error) {
errno = -open_error;
diff --git a/testing/proofs/cbmc/bpm-tree-state-pending.c b/testing/proofs/cbmc/bpm-tree-state-pending.c
index cb46858b..b14a92f5 100644
--- a/testing/proofs/cbmc/bpm-tree-state-pending.c
+++ b/testing/proofs/cbmc/bpm-tree-state-pending.c
@@ -161,7 +161,7 @@ static bool digest_text(const char *s)
digits &= (s[i] >= '0' && s[i] <= '9') ||
(s[i] >= 'a' && s[i] <= 'f') ||
(s[i] >= 'A' && s[i] <= 'F');
- return !memcmp(s, "sha256:", 7) && digits && !s[71];
+ return str_has_prefix(s, "sha256:") && digits && !s[71];
}
/* upatch-name proves which names upatch_name_valid() takes. */
diff --git a/testing/proofs/cbmc/config-image.c b/testing/proofs/cbmc/config-image.c
index 328668b9..b1967ff7 100644
--- a/testing/proofs/cbmc/config-image.c
+++ b/testing/proofs/cbmc/config-image.c
@@ -20,9 +20,10 @@ static __u32 le32_at(size_t off)
static bool superblock_valid(void)
{
- static const char sha256[32] = "sha256";
+ static const char signature[8] = "verity", sha256[32] = "sha256";
- return !memcmp(proof_file, "verity\0\0", 8) && le32_at(8) == 1 &&
+ return !memcmp(proof_file, signature, sizeof(signature)) &&
+ le32_at(8) == 1 &&
le32_at(12) == 1 && !memcmp(proof_file + 32, sha256, 32) &&
le32_at(64) == 4096 && le32_at(68) == 4096 &&
get_le16(proof_file + 80) == 32;
diff --git a/testing/proofs/cbmc/json-emit-nest.c b/testing/proofs/cbmc/json-emit-nest.c
index 5aa7dbca..f8d1b855 100644
--- a/testing/proofs/cbmc/json-emit-nest.c
+++ b/testing/proofs/cbmc/json-emit-nest.c
@@ -42,6 +42,7 @@ static bool close_ok(char c, bool *array, size_t *depth, enum state *s)
*/
static bool json_text(const char *t, size_t n)
{
+ static const char escapes[] = "\"\\/bfnrt";
bool array[CALLS + 1], key = false;
size_t i, depth = 0, hex = 0;
const char *word = "";
@@ -63,7 +64,7 @@ static bool json_text(const char *t, size_t n)
if (c == 'u') {
s = HEX;
hex = 0;
- } else if (memchr("\"\\/bfnrt", c, 8)) {
+ } else if (memchr(escapes, c, sizeof(escapes) - 1)) {
s = STRING;
} else {
return false;
diff --git a/testing/proofs/cbmc/json-walk.c b/testing/proofs/cbmc/json-walk.c
index 4502996d..8086c322 100644
--- a/testing/proofs/cbmc/json-walk.c
+++ b/testing/proofs/cbmc/json-walk.c
@@ -69,6 +69,7 @@ static int code_unit(const __u8 *t, size_t n, size_t i)
*/
static size_t string_end(const __u8 *t, size_t n, size_t i)
{
+ static const char escapes[] = "\"\\/bfnrt";
size_t w;
int cu;
@@ -84,7 +85,8 @@ static size_t string_end(const __u8 *t, size_t n, size_t i)
i += w;
continue;
}
- if (i + 1 < n && memchr("\"\\/bfnrt", t[i + 1], 8)) {
+ if (i + 1 < n &&
+ memchr(escapes, t[i + 1], sizeof(escapes) - 1)) {
i += 2;
continue;
}
diff --git a/testing/proofs/cbmc/kernel-notes.c b/testing/proofs/cbmc/kernel-notes.c
index 30ae726e..c6537c3d 100644
--- a/testing/proofs/cbmc/kernel-notes.c
+++ b/testing/proofs/cbmc/kernel-notes.c
@@ -48,7 +48,7 @@ static bool lower_hex(char c)
static bool gnu_build_id(const __u8 *notes)
{
return word(notes + 8) == 3 && word(notes) == 4 &&
- !memcmp(notes + 12, "GNU", 4);
+ !memcmp(notes + 12, "GNU", sizeof("GNU"));
}
/* A section of one note, of a name of up to 4 bytes and a descriptor. */