Skip to content

eBPF-based security platform

testing: check every string copy of the tests and fuzz targets

Daniel Borkmann43bd8c5ec5a8
diff --git a/testing/fuzz/fuzz-decoder.c b/testing/fuzz/fuzz-decoder.c
index 32ca9504..f348a94d 100644
--- a/testing/fuzz/fuzz-decoder.c
+++ b/testing/fuzz/fuzz-decoder.c
@@ -47,10 +47,10 @@ int LLVMFuzzerInitialize(int *argc, char ***argv)
abort();
views[i].source = src;
views[i].source_len = (__u32)len;
- strscpy(views[i].source_name, strrchr(sources[i], '/') + 1,
- sizeof(views[i].source_name));
- strscpy(views[i].entrypoint, "decode",
- sizeof(views[i].entrypoint));
+ if (strscpy(views[i].source_name, strrchr(sources[i], '/') + 1,
+ sizeof(views[i].source_name)) < 0)
+ abort();
+ strscpy_literal(views[i].entrypoint, "decode");
}
return 0;
}
@@ -82,7 +82,7 @@ int LLVMFuzzerTestOneInput(const __u8 *data, size_t size)
size -= 9;
/* The longest name, which the decoder gets in its input. */
memset(v.upatch, 'u', UPATCH_NAME_MAX - 1);
- strscpy(v.name, "fuzz_map", sizeof(v.name));
+ strscpy_literal(v.name, "fuzz_map");
page.data = xmalloc(size + 1);
if (!page.data)
diff --git a/testing/fuzz/fuzz-kconfig.c b/testing/fuzz/fuzz-kconfig.c
index 03670b94..6493dc3f 100644
--- a/testing/fuzz/fuzz-kconfig.c
+++ b/testing/fuzz/fuzz-kconfig.c
@@ -24,9 +24,9 @@ int LLVMFuzzerInitialize(int *argc, char ***argv)
struct tree_selector_kv *kv;
if (tree_selector_kv_add(&sel.kconfig, &sel.n_kconfig,
- TREE_MAX_KCONFIG, &kv))
+ TREE_MAX_KCONFIG, &kv) ||
+ strscpy(kv->key, keys[i], sizeof(kv->key)) < 0)
abort();
- strscpy(kv->key, keys[i], sizeof(kv->key));
}
selector_hash_build(&sel, slots);
return 0;
diff --git a/testing/helper/test-image-cli.c b/testing/helper/test-image-cli.c
index b4578655..a502c636 100644
--- a/testing/helper/test-image-cli.c
+++ b/testing/helper/test-image-cli.c
@@ -15,7 +15,7 @@ int __wrap_bpm_tree_fetch_publication(struct bpm_oci_client *client,
char *digest, size_t capacity)
{
puts(certificate);
- strscpy(pub->tree_id, "t", sizeof(pub->tree_id));
+ strscpy_literal(pub->tree_id, "t");
return 0;
}
@@ -45,7 +45,7 @@ int main(int argc, const char **argv)
if (!ret) {
memcpy(bootstrap.tpm_owner_auth, "bootstrap-owner", 15);
bootstrap.tpm_owner_auth_len = 15;
- strscpy(bootstrap.tpm_tcti, "bootstrap-tcti", sizeof(bootstrap.tpm_tcti));
+ strscpy_literal(bootstrap.tpm_tcti, "bootstrap-tcti");
ret = image_config_set(&bootstrap, fd);
}
close(fd);
@@ -85,8 +85,8 @@ int main(int argc, const char **argv)
struct tree_source source = {};
char digest[BPM_MAX_DIGEST];
- strscpy(config.endpoint, "oci://example.invalid/tree", sizeof(config.endpoint));
- strscpy(config.reference, "latest", sizeof(config.reference));
+ strscpy_literal(config.endpoint, "oci://example.invalid/tree");
+ strscpy_literal(config.reference, "latest");
ret = tree_source_open(&config, &source);
if (!ret)
ret = tree_fetch_checked(&source, config.reference,
diff --git a/testing/proofs/cbmc/attest-replay-model.c b/testing/proofs/cbmc/attest-replay-model.c
index 23b0fb02..ac6eef15 100644
--- a/testing/proofs/cbmc/attest-replay-model.c
+++ b/testing/proofs/cbmc/attest-replay-model.c
@@ -190,8 +190,10 @@ static void any_record(struct parse *p, enum kind k, bool word, char digest,
fill(p->chain_hex, 64, 'a');
fill(p->writer_public_hex, 2, 'a');
} else if (k == K_LOAD || k == K_JAVA_UNLOAD) {
- strscpy(p->upatch_name, name_ok ? "n" : "!",
- sizeof(p->upatch_name));
+ if (name_ok)
+ strscpy_literal(p->upatch_name, "n");
+ else
+ strscpy_literal(p->upatch_name, "!");
fill(p->upatch_payload_hex, 64, field);
} else if (k == K_TREE) {
p->tree_seq = 1;
diff --git a/testing/unit/bpm_state.c b/testing/unit/bpm_state.c
index 777f59f0..8dfea33f 100644
--- a/testing/unit/bpm_state.c
+++ b/testing/unit/bpm_state.c
@@ -15,7 +15,7 @@ static void entry(struct bpm_state_entry *e, const char *name,
{
ASSERT_TRUE(strscpy(e->name, name, sizeof(e->name)) > 0);
ASSERT_TRUE(strscpy(e->version, version, sizeof(e->version)) > 0);
- ASSERT_TRUE(strscpy(e->source, "main", sizeof(e->source)) > 0);
+ strscpy_literal(e->source, "main");
ASSERT_TRUE(strsprintf(e->path, sizeof(e->path), "%s/%s/%s",
BPM_PACKAGES_DIR, name, version) > 0);
}
diff --git a/testing/unit/bpm_tree_state.c b/testing/unit/bpm_tree_state.c
index 1b8a92dc..ec084a03 100644
--- a/testing/unit/bpm_tree_state.c
+++ b/testing/unit/bpm_tree_state.c
@@ -64,7 +64,7 @@ static int modules_read(const char *text, size_t len, bool gz,
memset(values, 0, sizeof(values));
ASSERT_EQ(tree_selector_kv_add(&sel.kconfig, &sel.n_kconfig,
TREE_MAX_KCONFIG, &kv), 0);
- strscpy(kv->key, "CONFIG_MODULES", sizeof(kv->key));
+ strscpy_literal(kv->key, "CONFIG_MODULES");
selector_hash_build(&sel, slots);
write_config_bytes(text, len, gz);
ret = read_selector_config_file(path, &sel, slots, values, found, gz);
@@ -131,7 +131,7 @@ static int read_gz(bool gz, off_t cut, const char *extra)
ASSERT_EQ(tree_selector_kv_add(&sel.kconfig, &sel.n_kconfig,
TREE_MAX_KCONFIG, &kv), 0);
- strscpy(kv->key, "CONFIG_MODULES", sizeof(kv->key));
+ strscpy_literal(kv->key, "CONFIG_MODULES");
selector_hash_build(&sel, slots);
write_config("CONFIG_MODULES=y\n", gz);
ASSERT_EQ(stat(path, &st), 0);
diff --git a/testing/unit/json.c b/testing/unit/json.c
index 6608052a..1fc5ea78 100644
--- a/testing/unit/json.c
+++ b/testing/unit/json.c
@@ -28,8 +28,9 @@ static int name_on_elem(void *ctx)
{
struct doc *d = ctx;
- if (!d->names++)
- strscpy(d->first_name, d->name, sizeof(d->first_name));
+ if (!d->names++ &&
+ strscpy(d->first_name, d->name, sizeof(d->first_name)) < 0)
+ return -E2BIG;
return 0;
}
@@ -37,7 +38,8 @@ static int event_on_elem(void *ctx)
{
struct doc *d = ctx;
- strscpy(d->last_type, d->ev_type, sizeof(d->last_type));
+ if (strscpy(d->last_type, d->ev_type, sizeof(d->last_type)) < 0)
+ return -E2BIG;
d->events++;
return 0;
}
diff --git a/testing/unit/keepalive.c b/testing/unit/keepalive.c
index 32bbafe1..9c831089 100644
--- a/testing/unit/keepalive.c
+++ b/testing/unit/keepalive.c
@@ -823,7 +823,7 @@ static int fake_read_state(void *ctx, struct tree_state *state)
*state = (struct tree_state){ .schema = 1, .generation = 1,
.head_seq = KEEPALIVE_MASK_OPS };
- strscpy(state->tree_id, "fleet-base", sizeof(state->tree_id));
+ strscpy_literal(state->tree_id, "fleet-base");
if (!node->n_seqs)
state->last_seq = MOVING_FROM + read;
else
diff --git a/testing/unit/upatch_decode.c b/testing/unit/upatch_decode.c
index fd96ce38..62247435 100644
--- a/testing/unit/upatch_decode.c
+++ b/testing/unit/upatch_decode.c
@@ -76,9 +76,8 @@ static void decode_named(const char *upatch, const char *decoder_upatch,
ASSERT_TRUE(strscpy(f.decoder.upatch, decoder_upatch,
sizeof(f.decoder.upatch)) >= 0);
ASSERT_TRUE(strscpy(f.decoder.map, view.name, sizeof(f.decoder.map)) >= 0);
- ASSERT_TRUE(strscpy(f.decoder.source, "unit.lua", sizeof(f.decoder.source)) >= 0);
- ASSERT_TRUE(strscpy(f.decoder.entrypoint, "decode",
- sizeof(f.decoder.entrypoint)) >= 0);
+ strscpy_literal(f.decoder.source, "unit.lua");
+ strscpy_literal(f.decoder.entrypoint, "decode");
ASSERT_TRUE(strscpy(f.source, source, sizeof(f.source)) >= 0);
f.decoder.source_off = cpu_to_le32(offsetof(struct decoder_fixture, source) - data_off);
f.decoder.source_len = cpu_to_le32(source_len);