Skip to content

eBPF-based security platform

shirudo-build: check every string copy of the package builder

Daniel Borkmann3d441880ae4b
diff --git a/builtin-build.c b/builtin-build.c
index a98118e8..a27df1b2 100644
--- a/builtin-build.c
+++ b/builtin-build.c
@@ -66,12 +66,12 @@ static bool on_path(const char *name)
if (!end)
end = path + strlen(path);
len = (size_t)(end - path);
- if (len && len + 1 + strlen(name) < sizeof(candidate)) {
+ if (len && len + 1 < sizeof(candidate)) {
memcpy(candidate, path, len);
candidate[len] = '/';
- strscpy(candidate + len + 1, name,
- sizeof(candidate) - len - 1);
- if (access(candidate, X_OK) == 0)
+ if (strscpy(candidate + len + 1, name,
+ sizeof(candidate) - len - 1) >= 0 &&
+ access(candidate, X_OK) == 0)
return true;
}
path = *end ? end + 1 : end;
diff --git a/testing/proofs/cbmc/upatch-writer-header.c b/testing/proofs/cbmc/upatch-writer-header.c
index c69d24d4..ade01ade 100644
--- a/testing/proofs/cbmc/upatch-writer-header.c
+++ b/testing/proofs/cbmc/upatch-writer-header.c
@@ -38,9 +38,12 @@ void harness(void)
*/
__CPROVER_assume(len < sizeof(name) && !name[len] &&
strnlen(name, len) == len);
- pack_header(&h, name, type, flags, payload, loader_off, loader_len,
- sig_off, sig_len, meta_off, meta_len, data_off, data_len,
- log_buf_sz, rodata_data_off, nr_maps, nr_progs);
+ __CPROVER_assert(!pack_header(&h, name, type, flags, payload,
+ loader_off, loader_len, sig_off, sig_len,
+ meta_off, meta_len, data_off, data_len,
+ log_buf_sz, rodata_data_off, nr_maps,
+ nr_progs),
+ "a name shorter than the field is packed");
__CPROVER_assume(n >= sizeof(h));
__CPROVER_assert(!parse_header((const __u8 *)&h, n, &out),
"the reader takes the header");
diff --git a/tools/shirudo-build/bpm-build.c b/tools/shirudo-build/bpm-build.c
index 8891bb82..ae8d0aa3 100644
--- a/tools/shirudo-build/bpm-build.c
+++ b/tools/shirudo-build/bpm-build.c
@@ -400,7 +400,8 @@ static int build_default_targets(struct bpm_build_upatch *u)
char base[PATH_MAX];
char *slash;
- strscpy(base, u->source, sizeof(base));
+ if (strscpy(base, u->source, sizeof(base)) < 0)
+ return -ENAMETOOLONG;
slash = strrchr(base, '/');
if (slash)
memmove(base, slash + 1, strlen(slash + 1) + 1);
@@ -724,9 +725,10 @@ static int build_parse_spec(struct bpm_build_spec *spec, char *body)
for (size_t i = 0; i < spec->n_files; i++) {
if (!spec->files[i].source[0])
return -EINVAL;
- if (!spec->files[i].target[0])
- strscpy(spec->files[i].target, spec->files[i].source,
- sizeof(spec->files[i].target));
+ if (!spec->files[i].target[0] &&
+ strscpy(spec->files[i].target, spec->files[i].source,
+ sizeof(spec->files[i].target)) < 0)
+ return -EINVAL;
}
for (size_t i = 0; i < spec->n_upatches; i++) {
struct bpm_build_upatch *u = &spec->upatches[i];
@@ -735,7 +737,7 @@ static int build_parse_spec(struct bpm_build_spec *spec, char *body)
if (!u->source[0])
return -EINVAL;
if (!u->type[0])
- strscpy(u->type, "bpf", sizeof(u->type));
+ strscpy_literal(u->type, "bpf");
for (p = u->type; *p; p++)
*p = (char)tolower((__u8)*p);
if (!build_upatch_type_ok(u) || build_default_targets(u))
@@ -753,12 +755,13 @@ static int build_parse_spec(struct bpm_build_spec *spec, char *body)
continue;
if (!u->tree.id[0] || !upatch_name_valid(u->tree.id))
return -EINVAL;
- if (!u->tree.load_artifact[0])
- strscpy(u->tree.load_artifact, u->load_target,
- sizeof(u->tree.load_artifact));
- if (!u->tree.unload_artifact[0])
- strscpy(u->tree.unload_artifact, u->unload_target,
- sizeof(u->tree.unload_artifact));
+ if ((!u->tree.load_artifact[0] &&
+ strscpy(u->tree.load_artifact, u->load_target,
+ sizeof(u->tree.load_artifact)) < 0) ||
+ (!u->tree.unload_artifact[0] &&
+ strscpy(u->tree.unload_artifact, u->unload_target,
+ sizeof(u->tree.unload_artifact)) < 0))
+ return -EINVAL;
if (build_target_ok(u->tree.load_artifact) ||
build_target_ok(u->tree.unload_artifact))
return -EINVAL;
@@ -1086,7 +1089,9 @@ static int build_add_target(char seen[][PATH_MAX], size_t *n, const char *target
}
if (*n >= BPM_BUILD_MAX_ENTRIES * 3)
return -E2BIG;
- strscpy(seen[(*n)++], target, PATH_MAX);
+ if (strscpy(seen[*n], target, sizeof(seen[0])) < 0)
+ return -ENAMETOOLONG;
+ (*n)++;
return 0;
}
@@ -1374,9 +1379,12 @@ static int build_upatch_unload(struct bpm_build_upatch *u, const char *load,
hex_encode(parsed.hdr.payload_sha256, CRYPTO_SHA256_LEN,
u->payload_sha256);
memcpy(payload, parsed.hdr.payload_sha256, sizeof(payload));
- strscpy(u->type, upatch_type_str(&parsed), sizeof(u->type));
+ ret = strscpy(u->type, upatch_type_str(&parsed), sizeof(u->type)) < 0 ?
+ -EINVAL : 0;
java = parsed.hdr.type == UPATCH_TYPE_JAVA;
upatch_close(&parsed);
+ if (ret)
+ return ret;
if (java)
ret = build_package_java_unload(u->name, payload, unload, pkey,
cert);
@@ -1405,6 +1413,27 @@ static int build_java_upatch(struct bpm_build_upatch *u, const char *source,
* The unload directive is bound to the payload digest the load artifact
* ended up with.
*/
+/* One label for each path of each policy of u, into room for them all. */
+static int build_policy_labels(const struct bpm_build_upatch *u,
+ struct upatch_policy_label_entry *labels)
+{
+ size_t n = 0;
+
+ for (size_t j = 0; j < u->n_policies; j++) {
+ const struct bpm_build_policy *p = &u->policies[j];
+
+ for (int k = 0; k < p->n_paths; k++) {
+ struct upatch_policy_label_entry *e = &labels[n++];
+
+ e->bit = cpu_to_le64(p->bit);
+ if (strscpy(e->policy, p->id, sizeof(e->policy)) < 0 ||
+ strscpy(e->path, p->paths[k], sizeof(e->path)) < 0)
+ return -ENAMETOOLONG;
+ }
+ }
+ return 0;
+}
+
static int build_upatches(struct bpm_build_spec *spec, const char *pkey,
const char *cert, const char *vmlinux_override,
const char *shirudo_bpf_incdir,
@@ -1430,8 +1459,9 @@ static int build_upatches(struct bpm_build_spec *spec, const char *pkey,
strsprintf(unload, sizeof(unload), "%s/%zu.unload.upatch",
spec->stage, i) < 0)
return -ENAMETOOLONG;
- strscpy(u->load_path, load, sizeof(u->load_path));
- strscpy(u->unload_path, unload, sizeof(u->unload_path));
+ if (strscpy(u->load_path, load, sizeof(u->load_path)) < 0 ||
+ strscpy(u->unload_path, unload, sizeof(u->unload_path)) < 0)
+ return -ENAMETOOLONG;
if (!strcmp(u->type, "java")) {
ret = build_java_upatch(u, source, load, unload, pkey,
cert);
@@ -1471,18 +1501,10 @@ static int build_upatches(struct bpm_build_spec *spec, const char *pkey,
labels = xcalloc(nr_labels, sizeof(*labels));
if (!labels)
return -ENOMEM;
- nr_labels = 0;
- for (size_t j = 0; j < u->n_policies; j++) {
- struct bpm_build_policy *p = &u->policies[j];
-
- for (int k = 0; k < p->n_paths; k++) {
- struct upatch_policy_label_entry *e =
- &labels[nr_labels++];
-
- e->bit = cpu_to_le64(p->bit);
- strscpy(e->policy, p->id, sizeof(e->policy));
- strscpy(e->path, p->paths[k], sizeof(e->path));
- }
+ ret = build_policy_labels(u, labels);
+ if (ret) {
+ xfree(labels, size_mul(nr_labels, sizeof(*labels)));
+ return ret;
}
}
ret = build_package_load(bpf_o, src_path, load, name,
@@ -1958,7 +1980,10 @@ static int __build_bpm_build(int argc, const char **argv,
spec = xmalloc(sizeof(*spec));
if (!spec)
return -ENOMEM;
- strscpy(spec->scope, resolved_file, sizeof(spec->scope));
+ if (strscpy(spec->scope, resolved_file, sizeof(spec->scope)) < 0) {
+ ret = -ENAMETOOLONG;
+ goto out;
+ }
{
char *slash = strrchr(spec->scope, '/');
@@ -2326,7 +2351,10 @@ static int __build_bpm_config_update(int argc, const char **argv,
ret = -errno;
goto out;
}
- strscpy(spec->scope, resolved_file, sizeof(spec->scope));
+ if (strscpy(spec->scope, resolved_file, sizeof(spec->scope)) < 0) {
+ ret = -ENAMETOOLONG;
+ goto out;
+ }
{
char *slash = strrchr(spec->scope, '/');
@@ -2449,8 +2477,13 @@ static int __build_bpm_config_update(int argc, const char **argv,
overrides, n_overrides, true);
if (ret)
break;
- strscpy(upatch->load_path, load, sizeof(upatch->load_path));
- strscpy(upatch->unload_path, unload, sizeof(upatch->unload_path));
+ if (strscpy(upatch->load_path, load,
+ sizeof(upatch->load_path)) < 0 ||
+ strscpy(upatch->unload_path, unload,
+ sizeof(upatch->unload_path)) < 0) {
+ ret = -ENAMETOOLONG;
+ break;
+ }
ret = build_upatch_unload(upatch, load, unload, pkey,
cert_path);
}
diff --git a/tools/shirudo-build/compile.c b/tools/shirudo-build/compile.c
index 185a1f45..c596b47a 100644
--- a/tools/shirudo-build/compile.c
+++ b/tools/shirudo-build/compile.c
@@ -96,7 +96,7 @@ int build_compile(const char *source, const char *output,
if (slash)
*slash = '\0';
else
- strscpy(vmlinux_dir, ".", sizeof(vmlinux_dir));
+ strscpy_literal(vmlinux_dir, ".");
}
if (strsprintf(inc_vmlinux, sizeof(inc_vmlinux), "-I%s",
vmlinux_dir) < 0)
diff --git a/tools/shirudo-build/package.c b/tools/shirudo-build/package.c
index d6699b54..15727cb1 100644
--- a/tools/shirudo-build/package.c
+++ b/tools/shirudo-build/package.c
@@ -87,21 +87,23 @@ struct decoder_set {
__u32 nr_items;
};
-static void pack_header(struct upatch_header *h,
- const char *name, __u32 type, __u32 flags,
- const __u8 *payload_sha256,
- __u32 loader_off, __u32 loader_len,
- __u32 sig_off, __u32 sig_len,
- __u32 meta_off, __u32 meta_len,
- __u32 data_off, __u32 data_len,
- __u32 log_buf_sz,
- __u32 rodata_data_off, __u32 nr_maps, __u32 nr_progs)
+static __must_check int
+pack_header(struct upatch_header *h,
+ const char *name, __u32 type, __u32 flags,
+ const __u8 *payload_sha256,
+ __u32 loader_off, __u32 loader_len,
+ __u32 sig_off, __u32 sig_len,
+ __u32 meta_off, __u32 meta_len,
+ __u32 data_off, __u32 data_len,
+ __u32 log_buf_sz,
+ __u32 rodata_data_off, __u32 nr_maps, __u32 nr_progs)
{
memset(h, 0, sizeof(*h));
+ if (strscpy(h->name, name, sizeof(h->name)) < 0)
+ return -ENAMETOOLONG;
h->magic = cpu_to_le64(UPATCH_MAGIC);
h->version = cpu_to_le32(UPATCH_VERSION);
h->flags = cpu_to_le32(flags);
- strscpy(h->name, name, sizeof(h->name));
memcpy(h->payload_sha256, payload_sha256,
UPATCH_PAYLOAD_HASH_LEN);
h->loader_insns_off = cpu_to_le32(loader_off);
@@ -118,6 +120,7 @@ static void pack_header(struct upatch_header *h,
h->rodata_data_off = cpu_to_le32(rodata_data_off);
h->type = cpu_to_le32(type);
h->reserved1 = 0;
+ return 0;
}
/*
@@ -298,14 +301,17 @@ static int package_resign_symbols(const char *original,
goto out;
}
upatch_header_name(&mod, name);
- pack_header(&hdr, name, mod.hdr.type, mod.hdr.flags,
- sio.payload_sha256,
- loader_off, (__u32)sio.insns_sz,
- sig_off, (__u32)sio.signature_sz,
- mod.hdr.metadata_len ? meta_off : 0, mod.hdr.metadata_len,
- sio.data_sz ? data_off : 0, (__u32)sio.data_sz,
- mod.hdr.log_buf_sz, mod.hdr.rodata_data_off,
- mod.hdr.nr_maps, mod.hdr.nr_progs);
+ ret = pack_header(&hdr, name, mod.hdr.type, mod.hdr.flags,
+ sio.payload_sha256,
+ loader_off, (__u32)sio.insns_sz,
+ sig_off, (__u32)sio.signature_sz,
+ mod.hdr.metadata_len ? meta_off : 0,
+ mod.hdr.metadata_len,
+ sio.data_sz ? data_off : 0, (__u32)sio.data_sz,
+ mod.hdr.log_buf_sz, mod.hdr.rodata_data_off,
+ mod.hdr.nr_maps, mod.hdr.nr_progs);
+ if (ret)
+ goto out;
fd = open(output, O_WRONLY | O_CREAT | O_CLOEXEC, 0644);
if (fd < 0) {
ret = -errno;
@@ -1375,6 +1381,17 @@ err_free:
return err;
}
+static int decoder_entry_names(struct upatch_decoder_entry *d,
+ const struct decoder_manifest_view *m)
+{
+ if (strscpy(d->upatch, m->upatch, sizeof(d->upatch)) < 0 ||
+ strscpy(d->map, m->map, sizeof(d->map)) < 0 ||
+ strscpy(d->source, m->source, sizeof(d->source)) < 0 ||
+ strscpy(d->entrypoint, m->entrypoint, sizeof(d->entrypoint)) < 0)
+ return -E2BIG;
+ return 0;
+}
+
static int build_decoder_blob(const struct decoder_set *set,
void **out, __u32 *out_len)
{
@@ -1413,11 +1430,10 @@ static int build_decoder_blob(const struct decoder_set *set,
d->api_version = cpu_to_le32(file->manifest.api_version);
d->input_format = cpu_to_le32(file->manifest.input_format);
d->export_signal = cpu_to_le32(file->manifest.export_signal);
- strscpy(d->upatch, file->manifest.upatch, sizeof(d->upatch));
- strscpy(d->map, file->manifest.map, sizeof(d->map));
- strscpy(d->source, file->manifest.source, sizeof(d->source));
- strscpy(d->entrypoint, file->manifest.entrypoint,
- sizeof(d->entrypoint));
+ if (decoder_entry_names(d, &file->manifest)) {
+ xfree(blob, (size_t)total);
+ return -E2BIG;
+ }
d->source_off = cpu_to_le32(off);
d->source_len = cpu_to_le32(file->source_len);
memcpy((__u8 *)blob + off, file->source,
@@ -1632,11 +1648,16 @@ static int build_prog_metadata(struct bpf_object *obj, __u32 nr_maps,
goto err_free;
}
}
- strscpy(seen_maps[n_seen++], ser_map, sizeof(seen_maps[0]));
+ if (strscpy(seen_maps[n_seen], ser_map,
+ sizeof(seen_maps[0])) < 0 ||
+ strscpy(md->progs[i].map, ser_map,
+ sizeof(md->progs[i].map)) < 0) {
+ err = -EINVAL;
+ goto err_free;
+ }
+ n_seen++;
md->progs[i].role =
cpu_to_le32(UPATCH_PROG_ROLE_SERIALIZER);
- strscpy(md->progs[i].map, ser_map,
- sizeof(md->progs[i].map));
i++;
continue;
}
@@ -2160,14 +2181,16 @@ static int finalize_object(struct bpf_object *obj, const char *out_path,
goto out;
}
- pack_header(&hdr, upatch_name, UPATCH_TYPE_BPF, flags,
- sio.payload_sha256,
- loader_off, opts.insns_sz,
- sig_off, (__u32)sio.signature_sz,
- 0, 0,
- data_off, data_len,
- 1u * 1024u * 1024u,
- rodata_data_off, nr_maps, nr_progs);
+ err = pack_header(&hdr, upatch_name, UPATCH_TYPE_BPF, flags,
+ sio.payload_sha256,
+ loader_off, opts.insns_sz,
+ sig_off, (__u32)sio.signature_sz,
+ 0, 0,
+ data_off, data_len,
+ 1u * 1024u * 1024u,
+ rodata_data_off, nr_maps, nr_progs);
+ if (err)
+ goto out;
err = write_artifact(out_path, &hdr, opts.insns, opts.insns_sz,
sig_buf, sio.signature_sz, signed_data, data_len);
@@ -2437,12 +2460,14 @@ static int finalize_java(const char *out_path, const char *upatch_name,
err = -E2BIG;
goto out;
}
- pack_header(&hdr, upatch_name, UPATCH_TYPE_JAVA, flags,
- sio.payload_sha256, 0, 0,
- (__u32)sizeof(hdr), (__u32)sio.signature_sz, 0, 0,
- data_off, signed_data_len, 0, 0, 0, 0);
- err = write_artifact(out_path, &hdr, NULL, 0, sig_buf,
- sio.signature_sz, signed_data, signed_data_len);
+ err = pack_header(&hdr, upatch_name, UPATCH_TYPE_JAVA, flags,
+ sio.payload_sha256, 0, 0,
+ (__u32)sizeof(hdr), (__u32)sio.signature_sz, 0, 0,
+ data_off, signed_data_len, 0, 0, 0, 0);
+ if (!err)
+ err = write_artifact(out_path, &hdr, NULL, 0, sig_buf,
+ sio.signature_sz, signed_data,
+ signed_data_len);
if (!err)
pr_info("wrote %s (java sig=%zu data=%u name=%s%s)", out_path,
sio.signature_sz, signed_data_len, upatch_name,