Skip to content

eBPF-based security platform

shirudo-build: format paths with strsprintf()

Daniel Borkmann8cc314f6d321
diff --git a/tools/shirudo-build/bpm-build.c b/tools/shirudo-build/bpm-build.c
index bac74de5..0f905efa 100644
--- a/tools/shirudo-build/bpm-build.c
+++ b/tools/shirudo-build/bpm-build.c
@@ -340,12 +340,12 @@ static int build_default_targets(struct bpm_build_upatch *u)
if (!*base)
return -EINVAL;
if (!u->load_target[0] &&
- snprintf(u->load_target, sizeof(u->load_target),
- "upatches/%s.upatch", base) >= (int)sizeof(u->load_target))
+ strsprintf(u->load_target, sizeof(u->load_target),
+ "upatches/%s.upatch", base) < 0)
return -ENAMETOOLONG;
- if (!u->unload_target[0] && snprintf(u->unload_target,
- sizeof(u->unload_target), "upatches/%s.unload.upatch",
- base) >= (int)sizeof(u->unload_target))
+ if (!u->unload_target[0] && strsprintf(u->unload_target,
+ sizeof(u->unload_target), "upatches/%s.unload.upatch",
+ base) < 0)
return -ENAMETOOLONG;
return 0;
}
@@ -630,7 +630,7 @@ static int build_resolve_source(const struct bpm_build_spec *spec,
if (!source[0] || source[0] == '/' || strstr(source, "../") ||
!strcmp(source, ".."))
return -EINVAL;
- if (snprintf(path, sizeof(path), "%s/%s", spec->scope, source) >= (int)sizeof(path) ||
+ if (strsprintf(path, sizeof(path), "%s/%s", spec->scope, source) < 0 ||
stat(path, st) || !S_ISREG(st->st_mode) || !build_within(spec->scope, path) ||
!realpath(path, out))
return errno ? -errno : -EINVAL;
@@ -700,7 +700,7 @@ static int build_validate(struct bpm_build_spec *spec)
static int build_join(char *out, size_t cap, const char *dir, const char *name)
{
- return snprintf(out, cap, "%s/%s", dir, name) >= (int)cap ? -ENAMETOOLONG : 0;
+ return strsprintf(out, cap, "%s/%s", dir, name) < 0 ? -ENAMETOOLONG : 0;
}
static int build_entry_name(char *out, size_t cap, const char *top,
@@ -749,9 +749,8 @@ static int build_stage_open(struct bpm_build_spec *spec)
if (!tmpdir || !tmpdir[0])
tmpdir = "/tmp";
- if (snprintf(spec->stage, sizeof(spec->stage),
- "%s/shirudo-bpm-build.XXXXXX", tmpdir) >=
- (int)sizeof(spec->stage))
+ if (strsprintf(spec->stage, sizeof(spec->stage),
+ "%s/shirudo-bpm-build.XXXXXX", tmpdir) < 0)
return -ENAMETOOLONG;
if (!mkdtemp(spec->stage))
return -errno;
@@ -796,10 +795,10 @@ static int build_upatches(struct bpm_build_spec *spec, const char *pkey,
if (build_resolve_source(spec, u->source, source, sizeof(source),
&(struct stat){}))
return -EINVAL;
- if (snprintf(load, sizeof(load), "%s/%zu.upatch", spec->stage,
- i) >= (int)sizeof(load) ||
- snprintf(unload, sizeof(unload), "%s/%zu.unload.upatch",
- spec->stage, i) >= (int)sizeof(unload))
+ if (strsprintf(load, sizeof(load), "%s/%zu.upatch", spec->stage,
+ i) < 0 ||
+ 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));
@@ -813,8 +812,7 @@ static int build_upatches(struct bpm_build_spec *spec, const char *pkey,
return -ENOENT;
}
}
- if (snprintf(object, sizeof(object), "%s.bpf.o", load) >=
- (int)sizeof(object))
+ if (strsprintf(object, sizeof(object), "%s.bpf.o", load) < 0)
return -ENAMETOOLONG;
rc = shirudo_build_compile(source, object, vmlinux_h, NULL);
if (rc) {
@@ -990,8 +988,8 @@ static int build_archive(const struct bpm_build_spec *spec, const char *version,
struct bpm_tar tar;
int rc;
- if (snprintf(top, sizeof(top), "%s-%s", spec->name, version) >= (int)sizeof(top) ||
- snprintf(output, sizeof(output), "%s/%s.bee", out_dir, top) >= (int)sizeof(output))
+ if (strsprintf(top, sizeof(top), "%s-%s", spec->name, version) < 0 ||
+ strsprintf(output, sizeof(output), "%s/%s.bee", out_dir, top) < 0)
return -ENAMETOOLONG;
if (build_environment(spec, version, fingerprint, &environment,
&environment_len))
@@ -1091,7 +1089,7 @@ static const char *build_resolve_keys_dir(const char *arg, char *out, size_t cap
if (!arg || !*arg)
arg = keys_dir_resolve(NULL);
if (arg[0] == '~' && (!arg[1] || arg[1] == '/')) {
- if (!home || snprintf(out, cap, "%s%s", home, arg + 1) >= (int)cap)
+ if (!home || strsprintf(out, cap, "%s%s", home, arg + 1) < 0)
return NULL;
return out;
}
diff --git a/tools/shirudo-build/bpm-publish.c b/tools/shirudo-build/bpm-publish.c
index 3e5eab1c..c1f12e61 100644
--- a/tools/shirudo-build/bpm-publish.c
+++ b/tools/shirudo-build/bpm-publish.c
@@ -18,6 +18,7 @@
#include <curl/curl.h>
#include <shirudo/misc.h>
+#include <shirudo/string.h>
#include "bpm.h"
#include "bpm_oci.h"
@@ -401,8 +402,7 @@ static int push_package(const char *package_path, const char *raw_repository,
manifest_len = e.len;
manifest = (__u8 *)manifest_body;
if (bpm_sha256_hex(manifest, manifest_len, hex, sizeof(hex)) ||
- snprintf(package_digest, sizeof(package_digest), "sha256:%s", hex) >=
- (int)sizeof(package_digest)) {
+ strsprintf(package_digest, sizeof(package_digest), "sha256:%s", hex) < 0) {
rc = -EINVAL;
goto out;
}
@@ -460,8 +460,7 @@ static int push_package(const char *package_path, const char *raw_repository,
key_manifest_len = e.len;
if (bpm_sha256_hex(key_manifest_body, key_manifest_len, hex,
sizeof(hex)) ||
- snprintf(key_digest, sizeof(key_digest), "sha256:%s", hex) >=
- (int)sizeof(key_digest)) {
+ strsprintf(key_digest, sizeof(key_digest), "sha256:%s", hex) < 0) {
rc = -EINVAL;
goto out;
}
@@ -482,8 +481,8 @@ static int push_package(const char *package_path, const char *raw_repository,
sizeof(package_digest));
if (rc)
goto out;
- if (snprintf(reference, sizeof(reference), "oci://%s/%s:%s",
- c.registry, repository, info.version) >= (int)sizeof(reference)) {
+ if (strsprintf(reference, sizeof(reference), "oci://%s/%s:%s",
+ c.registry, repository, info.version) < 0) {
rc = -ENAMETOOLONG;
goto out;
}
@@ -568,8 +567,7 @@ static int tree_push_artifact(struct bpm_oci_client *c, const char *repository,
unload ? "load" : "unload", name);
return rc;
}
- if (snprintf(tag, sizeof(tag), "upatch-%s-%.16s", id, hex) >=
- (int)sizeof(tag) || strlen(tag) > 128)
+ if (strsprintf(tag, sizeof(tag), "upatch-%s-%.16s", id, hex) < 0 || strlen(tag) > 128)
return -ENAMETOOLONG;
snprintf(title, sizeof(title), "%s.upatch", id);
rc = bpm_oci_push_blob(c, repository, body, len, BPM_UPATCH_MEDIA,
diff --git a/tools/shirudo-build/shirudo-build.c b/tools/shirudo-build/shirudo-build.c
index be63c159..127802dd 100644
--- a/tools/shirudo-build/shirudo-build.c
+++ b/tools/shirudo-build/shirudo-build.c
@@ -108,14 +108,12 @@ static int ends_with(const char *s, const char *suf)
static int derive_output(const char *input, char *buf, size_t buf_sz)
{
size_t stem = strlen(input);
- int len;
if (stem > 2 && (strcmp(input + stem - 2, ".c") == 0 ||
strcmp(input + stem - 2, ".o") == 0))
stem -= 2;
- len = snprintf(buf, buf_sz, "%.*s.upatch", (int)stem, input);
- if (len < 0 || (size_t)len >= buf_sz)
+ if (strsprintf(buf, buf_sz, "%.*s.upatch", (int)stem, input) < 0)
return -ENAMETOOLONG;
return 0;
}
@@ -124,13 +122,11 @@ static int derive_output(const char *input, char *buf, size_t buf_sz)
static int derive_artifact_o(const char *output, char *buf, size_t buf_sz)
{
size_t stem = strlen(output);
- int len;
if (stem > 7 && strcmp(output + stem - 7, ".upatch") == 0)
stem -= 7;
- len = snprintf(buf, buf_sz, "%.*s.o", (int)stem, output);
- if (len < 0 || (size_t)len >= buf_sz)
+ if (strsprintf(buf, buf_sz, "%.*s.o", (int)stem, output) < 0)
return -ENAMETOOLONG;
return 0;
}
@@ -330,10 +326,8 @@ int main(int argc, char **argv)
}
if (!output) {
- int len = snprintf(output_buf, sizeof(output_buf),
- "%s.unload.upatch", unload_name);
-
- if (len < 0 || (size_t)len >= sizeof(output_buf)) {
+ if (strsprintf(output_buf, sizeof(output_buf),
+ "%s.unload.upatch", unload_name) < 0) {
pr_err("derived output path too long for '%s'",
unload_name);
return 1;
diff --git a/tools/shirudo-build/vouch.c b/tools/shirudo-build/vouch.c
index ef59ea42..e8aa92d2 100644
--- a/tools/shirudo-build/vouch.c
+++ b/tools/shirudo-build/vouch.c
@@ -14,6 +14,7 @@
#include <unistd.h>
#include <shirudo/misc.h>
+#include <shirudo/string.h>
#include "bpm.h"
#include "util/crypto.h"
@@ -148,8 +149,7 @@ int shirudo_build_keys_vouch(int argc, char **argv)
if (shirudo_build_check_keys(pkey, cert))
return 1;
if (!output) {
- if (snprintf(out_path, sizeof(out_path), "%s.vouch", input) >=
- (int)sizeof(out_path)) {
+ if (strsprintf(out_path, sizeof(out_path), "%s.vouch", input) < 0) {
pr_err("keys vouch: output path too long");
return 2;
}