Skip to content

eBPF-based security platform

shirudo-build: format the remaining values with strsprintf()

Daniel Borkmannb750f183b6d9
diff --git a/tools/shirudo-build/bpm-build.c b/tools/shirudo-build/bpm-build.c
index 0f905efa..d224e9e0 100644
--- a/tools/shirudo-build/bpm-build.c
+++ b/tools/shirudo-build/bpm-build.c
@@ -766,8 +766,9 @@ static void build_stage_close(struct bpm_build_spec *spec)
if (!u->load_path[0])
continue;
- snprintf(object, sizeof(object), "%s.bpf.o", u->load_path);
- unlink(object);
+ if (strsprintf(object, sizeof(object), "%s.bpf.o",
+ u->load_path) >= 0)
+ unlink(object);
unlink(u->load_path);
unlink(u->unload_path);
}
@@ -930,6 +931,12 @@ static int tar_write(struct bpm_tar *tar, const void *data, size_t len)
return gzwrite(tar->gz, data, (__u32)len) == (int)len ? 0 : -EIO;
}
+static int tar_octal(__u8 *h, size_t off, size_t size, __u64 value)
+{
+ return strsprintf((char *)h + off, size, "%0*llo", (int)(size - 1),
+ value) < 0 ? -E2BIG : 0;
+}
+
static int tar_entry(struct bpm_tar *tar, const char *name, const void *data,
size_t len, mode_t mode, char type)
{
@@ -940,18 +947,18 @@ static int tar_entry(struct bpm_tar *tar, const char *name, const void *data,
if (strlen(name) >= 100 || (type == '0' && len > 077777777777ULL))
return -ENAMETOOLONG;
memcpy(h, name, strlen(name));
- snprintf((char *)h + 100, 8, "%07o", (__u32)(mode & 07777));
- snprintf((char *)h + 108, 8, "%07o", 0u);
- snprintf((char *)h + 116, 8, "%07o", 0u);
- snprintf((char *)h + 124, 12, "%011o", (__u32)len);
- snprintf((char *)h + 136, 12, "%011o", 0u);
+ if (tar_octal(h, 100, 8, mode & 07777) || tar_octal(h, 108, 8, 0) ||
+ tar_octal(h, 116, 8, 0) || tar_octal(h, 124, 12, len) ||
+ tar_octal(h, 136, 12, 0))
+ return -E2BIG;
memset(h + 148, ' ', 8);
h[156] = (__u8)type;
memcpy(h + 257, "ustar\0", 6);
memcpy(h + 263, "00", 2);
for (size_t i = 0; i < sizeof(h); i++)
sum += h[i];
- snprintf((char *)h + 148, 8, "%06o ", sum);
+ if (strsprintf((char *)h + 148, 8, "%06o ", sum) < 0)
+ return -E2BIG;
if (tar_write(tar, h, sizeof(h)) || tar_write(tar, data, len))
return -EIO;
if (type == '0' && (len & 511))
diff --git a/tools/shirudo-build/bpm-publish.c b/tools/shirudo-build/bpm-publish.c
index 687a2b7c..4bc98882 100644
--- a/tools/shirudo-build/bpm-publish.c
+++ b/tools/shirudo-build/bpm-publish.c
@@ -569,7 +569,8 @@ static int tree_push_artifact(struct bpm_oci_client *c, const char *repository,
}
if (strsprintf(tag, sizeof(tag), "upatch-%s-%.16s", id, hex) < 0 || strlen(tag) > 128)
return -ENAMETOOLONG;
- snprintf(title, sizeof(title), "%s.upatch", id);
+ if (strsprintf(title, sizeof(title), "%s.upatch", id) < 0)
+ return -ENAMETOOLONG;
rc = bpm_oci_push_blob(c, repository, body, len, BPM_UPATCH_MEDIA,
&layer);
if (rc)
@@ -606,7 +607,9 @@ static int tree_push_artifact(struct bpm_oci_client *c, const char *repository,
return rc;
bpm_copy_string(out->repository, sizeof(out->repository), raw_repository);
bpm_copy_string(out->layer_digest, sizeof(out->layer_digest), layer.digest);
- snprintf(out->payload_sha256, sizeof(out->payload_sha256), "sha256:%s", hex);
+ if (strsprintf(out->payload_sha256, sizeof(out->payload_sha256),
+ "sha256:%s", hex) < 0)
+ return -E2BIG;
return 0;
}
diff --git a/tools/shirudo-build/compile.c b/tools/shirudo-build/compile.c
index d2ef5cb9..57790237 100644
--- a/tools/shirudo-build/compile.c
+++ b/tools/shirudo-build/compile.c
@@ -92,7 +92,9 @@ int shirudo_build_compile(const char *source, const char *output,
else
strscpy(vmlinux_dir, ".", sizeof(vmlinux_dir));
}
- snprintf(inc_vmlinux, sizeof(inc_vmlinux), "-I%s", vmlinux_dir);
+ if (strsprintf(inc_vmlinux, sizeof(inc_vmlinux), "-I%s",
+ vmlinux_dir) < 0)
+ return -ENAMETOOLONG;
push(argv, &n, SHIRUDO_CLANG_PATH);
push(argv, &n, "-target");
diff --git a/tools/shirudo-build/image.c b/tools/shirudo-build/image.c
index c3adc45d..a525525d 100644
--- a/tools/shirudo-build/image.c
+++ b/tools/shirudo-build/image.c
@@ -2,6 +2,7 @@
/* Copyright (c) 2026 Authors of shirudo */
#include <shirudo/misc.h>
+#include <shirudo/string.h>
#include <errno.h>
#include <fcntl.h>
@@ -45,7 +46,7 @@ static int build_image(int argc, char **argv)
char **command;
ssize_t size;
char *slash;
- int i, n;
+ int i;
size = readlink("/proc/self/exe", executable, sizeof(executable) - 1);
if (size < 0 || (size_t)size >= sizeof(executable) - 1)
@@ -56,16 +57,13 @@ static int build_image(int argc, char **argv)
if (!slash)
return 1;
*slash = '\0';
- n = snprintf(script, sizeof(script),
- "%s/../gen-image/shirudo-gen-verity", directory);
- if (n < 0 || (size_t)n >= sizeof(script))
+ if (strsprintf(script, sizeof(script),
+ "%s/../gen-image/shirudo-gen-verity", directory) < 0)
+ return 1;
+ if (access(script, X_OK) &&
+ strsprintf(script, sizeof(script),
+ "%s/gen-image/shirudo-gen-verity", directory) < 0)
return 1;
- if (access(script, X_OK)) {
- n = snprintf(script, sizeof(script),
- "%s/gen-image/shirudo-gen-verity", directory);
- if (n < 0 || (size_t)n >= sizeof(script))
- return 1;
- }
command = xcalloc((size_t)argc + 2, sizeof(*command));
if (!command)
return 1;
@@ -87,8 +85,11 @@ static int verify_verity(int data_fd, int hash_fd, const char *root)
pid_t pid;
int status;
- snprintf(data_path, sizeof(data_path), "/proc/self/fd/%d", data_fd);
- snprintf(hash_path, sizeof(hash_path), "/proc/self/fd/%d", hash_fd);
+ if (strsprintf(data_path, sizeof(data_path), "/proc/self/fd/%d",
+ data_fd) < 0 ||
+ strsprintf(hash_path, sizeof(hash_path), "/proc/self/fd/%d",
+ hash_fd) < 0)
+ return -ENAMETOOLONG;
pid = fork();
if (pid < 0)
return -errno;
diff --git a/tools/shirudo-build/shirudo-build.c b/tools/shirudo-build/shirudo-build.c
index 5b4dbb00..39fde6a7 100644
--- a/tools/shirudo-build/shirudo-build.c
+++ b/tools/shirudo-build/shirudo-build.c
@@ -312,8 +312,12 @@ int main(int argc, char **argv)
char archive[PATH_MAX];
struct shirudo_upatch u;
- snprintf(archive, sizeof(archive), "%s/%s.upatch",
- SHIRUDO_UPATCHES_DIR, unload_name);
+ if (strsprintf(archive, sizeof(archive), "%s/%s.upatch",
+ SHIRUDO_UPATCHES_DIR, unload_name) < 0) {
+ pr_err("archive path too long for '%s'",
+ unload_name);
+ return 1;
+ }
if (upatch_open(archive, &u) == 0) {
memcpy(payload_sha256, u.hdr.payload_sha256,
sizeof(payload_sha256));