eBPF-based security platform
shirudo-build: copy strings with strscpy()
Daniel Borkmanna9f75d93cacf
diff --git a/tools/shirudo-build/bpm-build.c b/tools/shirudo-build/bpm-build.cindex be8dcbe2..bac74de5 100644--- a/tools/shirudo-build/bpm-build.c+++ b/tools/shirudo-build/bpm-build.c@@ -23,6 +23,7 @@#include <zlib.h>#include <shirudo/misc.h>+#include <shirudo/string.h>#include "bpm.h"#include "compile.h"@@ -46,15 +47,6 @@#define BPM_BUILD_MAX_JSON (256u * 1024u)#define BPM_BUILD_MAX_VALUE 1024-static void build_copy(char *dst, size_t cap, const char *src)-{- if (!cap)- return;- if (!src)- src = "";- snprintf(dst, cap, "%s", src);-}-static char *build_trim(char *s){char *end;@@ -331,7 +323,7 @@ static int build_default_targets(struct bpm_build_upatch *u)char base[PATH_MAX];char *slash;- build_copy(base, sizeof(base), u->source);+ strscpy(base, u->source, sizeof(base));slash = strrchr(base, '/');if (slash)memmove(base, slash + 1, strlen(slash + 1) + 1);@@ -544,8 +536,8 @@ static int build_parse_spec(struct bpm_build_spec *spec, char *body)if (!spec->files[i].source[0])return -EINVAL;if (!spec->files[i].target[0])- build_copy(spec->files[i].target, sizeof(spec->files[i].target),- spec->files[i].source);+ strscpy(spec->files[i].target, spec->files[i].source,+ sizeof(spec->files[i].target));}for (size_t i = 0; i < spec->n_upatches; i++) {struct bpm_build_upatch *u = &spec->upatches[i];@@ -554,7 +546,7 @@ static int build_parse_spec(struct bpm_build_spec *spec, char *body)if (!u->source[0])return -EINVAL;if (!u->type[0])- build_copy(u->type, sizeof(u->type), "bpf");+ strscpy(u->type, "bpf", sizeof(u->type));for (p = u->type; *p; p++)*p = (char)tolower((__u8)*p);if (strcmp(u->type, "bpf") || build_default_targets(u))@@ -564,11 +556,11 @@ static int build_parse_spec(struct bpm_build_spec *spec, char *body)if (!u->tree.id[0] || !upatch_name_valid(u->tree.id))return -EINVAL;if (!u->tree.load_artifact[0])- build_copy(u->tree.load_artifact, sizeof(u->tree.load_artifact),- u->load_target);+ strscpy(u->tree.load_artifact, u->load_target,+ sizeof(u->tree.load_artifact));if (!u->tree.unload_artifact[0])- build_copy(u->tree.unload_artifact,- sizeof(u->tree.unload_artifact), u->unload_target);+ strscpy(u->tree.unload_artifact, u->unload_target,+ sizeof(u->tree.unload_artifact));if (build_target_ok(u->tree.load_artifact) ||build_target_ok(u->tree.unload_artifact))return -EINVAL;@@ -678,7 +670,7 @@ static int build_add_target(char seen[][PATH_MAX], size_t *n, const char *target}if (*n >= BPM_BUILD_MAX_ENTRIES * 3)return -E2BIG;- build_copy(seen[(*n)++], PATH_MAX, target);+ strscpy(seen[(*n)++], target, PATH_MAX);return 0;}@@ -738,7 +730,7 @@ static void build_upatch_name(const char *source, char *out, size_t cap)const char *base = strrchr(source, '/');size_t n;- build_copy(out, cap, base ? base + 1 : source);+ strscpy(out, base ? base + 1 : source, cap);n = strlen(out);if (n > 6 && (!strcmp(out + n - 6, ".bpf.c") || !strcmp(out + n - 6, ".bpf.o")))out[n - 6] = '\0';@@ -809,8 +801,8 @@ static int build_upatches(struct bpm_build_spec *spec, const char *pkey,snprintf(unload, sizeof(unload), "%s/%zu.unload.upatch",spec->stage, i) >= (int)sizeof(unload))return -ENAMETOOLONG;- build_copy(u->load_path, sizeof(u->load_path), load);- build_copy(u->unload_path, sizeof(u->unload_path), unload);+ strscpy(u->load_path, load, sizeof(u->load_path));+ strscpy(u->unload_path, unload, sizeof(u->unload_path));if (build_ends_with(source, ".bpf.c")) {if (!vmlinux_h) {vmlinux_h = shirudo_build_locate_vmlinux(NULL);@@ -849,12 +841,12 @@ static int build_upatches(struct bpm_build_spec *spec, const char *pkey,upatch_close(&parsed);return -EINVAL;}- build_copy(u->name, sizeof(u->name), parsed.hdr.name);+ strscpy(u->name, parsed.hdr.name, sizeof(u->name));hex_encode(parsed.hdr.payload_sha256,SHIRUDO_CRYPTO_SHA256_LEN,u->payload_sha256);memcpy(payload, parsed.hdr.payload_sha256, sizeof(payload));- build_copy(u->type, sizeof(u->type), upatch_type_str(&parsed));+ strscpy(u->type, upatch_type_str(&parsed), sizeof(u->type));upatch_close(&parsed);rc = shirudo_build_package_unload(u->name, payload, unload, pkey,cert);@@ -1103,9 +1095,8 @@ static const char *build_resolve_keys_dir(const char *arg, char *out, size_t capreturn NULL;return out;}- if (strlen(arg) >= cap)+ if (strscpy(out, arg, cap) < 0)return NULL;- build_copy(out, cap, arg);return out;}@@ -1180,16 +1171,19 @@ static int shirudo_build_bpm_build(int argc, char **argv)return -EACCES;if (!getcwd(out_dir, sizeof(out_dir)))return -errno;- if (file_arg)- build_copy(build_file, sizeof(build_file), file_arg);- else if (build_join(build_file, sizeof(build_file), context, "Beefile"))+ if (file_arg) {+ if (strscpy(build_file, file_arg, sizeof(build_file)) < 0)+ return -ENAMETOOLONG;+ } else if (build_join(build_file, sizeof(build_file), context,+ "Beefile")) {return -ENAMETOOLONG;+ }if (!realpath(build_file, resolved_file))return -errno;spec = xmalloc(sizeof(*spec));if (!spec)return -ENOMEM;- build_copy(spec->scope, sizeof(spec->scope), resolved_file);+ strscpy(spec->scope, resolved_file, sizeof(spec->scope));{char *slash = strrchr(spec->scope, '/');diff --git a/tools/shirudo-build/compile.c b/tools/shirudo-build/compile.cindex dd8f7403..4234df02 100644--- a/tools/shirudo-build/compile.c+++ b/tools/shirudo-build/compile.c@@ -16,6 +16,8 @@#include <sys/wait.h>#include <unistd.h>+#include <shirudo/string.h>+#include "compile.h"#include "log.h"#include "malloc.h"@@ -79,7 +81,10 @@ int shirudo_build_compile(const char *source, const char *output,return -ENOENT;}- snprintf(vmlinux_dir, sizeof(vmlinux_dir), "%s", vmlinux_h);+ if (strscpy(vmlinux_dir, vmlinux_h, sizeof(vmlinux_dir)) < 0) {+ pr_err("vmlinux.h path too long: %s", vmlinux_h);+ return -ENAMETOOLONG;+ }{char *slash = strrchr(vmlinux_dir, '/');diff --git a/tools/shirudo-build/package.c b/tools/shirudo-build/package.cindex c5ad8719..ed395aff 100644--- a/tools/shirudo-build/package.c+++ b/tools/shirudo-build/package.c@@ -89,7 +89,7 @@ static void pack_header(struct shirudo_upatch_header *h,h->magic = htole64(SHIRUDO_UPATCH_MAGIC);h->version = htole32(SHIRUDO_UPATCH_VERSION);h->flags = htole32(flags);- strncpy(h->name, name, sizeof(h->name) - 1);+ strscpy(h->name, name, sizeof(h->name));memcpy(h->payload_sha256, payload_sha256,SHIRUDO_UPATCH_PAYLOAD_HASH_LEN);h->loader_insns_off = htole32(loader_off);@@ -603,13 +603,11 @@ static int build_decoder_blob(const struct decoder_set *set,d->api_version = htole32(file->manifest.api_version);d->input_format = htole32(file->manifest.input_format);d->export_signal = htole32(file->manifest.export_signal);- snprintf(d->upatch, sizeof(d->upatch), "%s",- file->manifest.upatch);- snprintf(d->map, sizeof(d->map), "%s", file->manifest.map);- snprintf(d->source, sizeof(d->source), "%s",- file->manifest.source);- snprintf(d->entrypoint, sizeof(d->entrypoint), "%s",- file->manifest.entrypoint);+ 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));d->source_off = htole32(off);d->source_len = htole32(file->source_len);memcpy((__u8 *)blob + off, file->source,@@ -799,12 +797,11 @@ static int build_prog_metadata(struct bpf_object *obj, __u32 nr_progs,goto err_free;}}- snprintf(seen_maps[n_seen++], sizeof(seen_maps[0]),- "%s", ser_map);+ strscpy(seen_maps[n_seen++], ser_map, sizeof(seen_maps[0]));md->progs[i].role =htole32(SHIRUDO_UPATCH_PROG_ROLE_SERIALIZER);- snprintf(md->progs[i].map, sizeof(md->progs[i].map),- "%s", ser_map);+ strscpy(md->progs[i].map, ser_map,+ sizeof(md->progs[i].map));i++;continue;}@@ -1313,7 +1310,7 @@ int shirudo_build_package_unload(const char *upatch_name,return -EINVAL;}- strncpy(params.name, upatch_name, sizeof(params.name) - 1);+ strscpy(params.name, upatch_name, sizeof(params.name));memcpy(params.expected_sha256, payload_sha256,sizeof(params.expected_sha256));err = bpf_map__set_initial_value(rodata, ¶ms, sizeof(params));diff --git a/tools/shirudo-build/shirudo-build.c b/tools/shirudo-build/shirudo-build.cindex 925f369f..be63c159 100644--- a/tools/shirudo-build/shirudo-build.c+++ b/tools/shirudo-build/shirudo-build.c@@ -7,7 +7,6 @@#include <errno.h>#include <fcntl.h>-#include <libgen.h>#include <limits.h>#include <stdio.h>#include <stdlib.h>@@ -15,6 +14,7 @@#include <unistd.h>#include <shirudo/misc.h>+#include <shirudo/string.h>#include "bpm.h"#include "compile.h"@@ -81,13 +81,10 @@ static void usage(FILE *fp)static const char *derive_name(const char *input, char *buf, size_t buf_sz){- const char *base;- char tmp[1024];+ const char *base = strrchr(input, '/');size_t n;- snprintf(tmp, sizeof(tmp), "%s", input);- base = basename(tmp);- snprintf(buf, buf_sz, "%s", base);+ strscpy(buf, base ? base + 1 : input, buf_sz);n = strlen(buf);if (n > 6 && strcmp(buf + n - 6, ".bpf.c") == 0)