Skip to content

eBPF-based security platform

treewide: hex-encode digests with hex_encode()

Daniel Borkmann570b8a15e35e
diff --git a/Makefile.shirudo b/Makefile.shirudo
index 80681d62..494b83e8 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -696,7 +696,8 @@ testing/helper/test-kernel-release: testing/helper/test-kernel-release.c util/ke
@printf ' CC $@\n'
$(Q)$(CC) $(CFLAGS) -o $@ $^
-testing/helper/test-kernel-build-id: testing/helper/test-kernel-build-id.c util/kernel_build_id.o
+testing/helper/test-kernel-build-id: testing/helper/test-kernel-build-id.c util/kernel_build_id.o \
+ util/hex.o util/malloc.o
@printf ' CC $@\n'
$(Q)$(CC) $(CFLAGS) -o $@ $^
diff --git a/builtin-bpm.c b/builtin-bpm.c
index 195797d5..5e4cff21 100644
--- a/builtin-bpm.c
+++ b/builtin-bpm.c
@@ -465,8 +465,7 @@ static int key_file_fingerprint(const char *path, char hex[65])
cert_free(&cert);
if (rc)
return rc;
- for (size_t i = 0; i < sizeof(digest); i++)
- snprintf(hex + 2 * i, 3, "%02x", digest[i]);
+ hex_encode(digest, sizeof(digest), hex);
return 0;
}
diff --git a/util/bpm_oci.c b/util/bpm_oci.c
index f2d0fa71..950c8b24 100644
--- a/util/bpm_oci.c
+++ b/util/bpm_oci.c
@@ -25,6 +25,7 @@
#include "bpm_oci.h"
#include "crypto.h"
+#include "hex.h"
#include "io.h"
#include "seal.h"
#include "json.h"
@@ -1059,9 +1060,7 @@ int bpm_sha256_hex(const void *data, size_t len,
if (hex_sz < 65 || sha256_buffer(data, len, digest) != 0)
return -EINVAL;
- for (__u32 i = 0; i < sizeof(digest); i++)
- snprintf(hex + i * 2, 3, "%02x", digest[i]);
- hex[64] = 0;
+ hex_encode(digest, sizeof(digest), hex);
return 0;
}
diff --git a/util/bpm_package.c b/util/bpm_package.c
index c20c6985..59e806da 100644
--- a/util/bpm_package.c
+++ b/util/bpm_package.c
@@ -26,6 +26,7 @@
#include "bpm_package.h"
#include "bpm_tree.h"
#include "crypto.h"
+#include "hex.h"
#include "json.h"
#include "log.h"
#include "macros.h"
@@ -601,9 +602,7 @@ int bpm_pkg_load_info(const char *archive, struct bpm_package_info *info)
rc = -EINVAL;
goto out;
}
- for (__u32 i = 0; i < sizeof(digest); i++)
- snprintf(hex + i * 2, 3, "%02x", digest[i]);
- hex[sizeof(digest) * 2] = '\0';
+ hex_encode(digest, sizeof(digest), hex);
if (parsed.signer[0] && strcasecmp(parsed.signer, hex)) {
pr_err("bpm: package signer fingerprint mismatch: "
"certificate=%s build-environment.json=%s",
diff --git a/util/kernel_build_id.c b/util/kernel_build_id.c
index a123f7ca..933797bf 100644
--- a/util/kernel_build_id.c
+++ b/util/kernel_build_id.c
@@ -9,6 +9,7 @@
#include <shirudo/misc.h>
+#include "hex.h"
#include "kernel_build_id.h"
#define KERNEL_NOTES_PATH "/sys/kernel/notes"
@@ -55,8 +56,7 @@ int kernel_build_id_from_notes(const void *notes, size_t notes_len,
if (type == 3 && namesz >= 3 && !memcmp(p + name_off, "GNU", 3)) {
if (!descsz || descsz > SHIRUDO_KERNEL_BUILD_ID_MAX)
return -E2BIG;
- for (__u32 i = 0; i < descsz; i++)
- snprintf(out + i * 2, 3, "%02x", p[desc_off + i]);
+ hex_encode(p + desc_off, descsz, out);
return 0;
}
p += next;
diff --git a/util/upatch_maps.c b/util/upatch_maps.c
index cf9266bd..94de05b4 100644
--- a/util/upatch_maps.c
+++ b/util/upatch_maps.c
@@ -641,18 +641,15 @@ static char *format_value(const void *value, __u32 value_size,
size_t buf_sz = (size_t)cap * 2 + 8;
char *out = xmalloc(buf_sz);
size_t off = 0;
- __u32 i;
if (!out)
return NULL;
out[off++] = '0';
out[off++] = 'x';
- for (i = 0; i < cap; i++) {
- snprintf(out + off, buf_sz - off, "%02x", p[i]);
- off += 2;
- }
+ hex_encode(p, cap, out + off);
+ off += (size_t)cap * 2;
if (value_size > cap) {
- snprintf(out + off, buf_sz - off, "...");
+ memcpy(out + off, "...", 3);
off += 3;
}
out[off] = '\0';
diff --git a/util/upatch_maps_otel.c b/util/upatch_maps_otel.c
index 5880a061..c8ecf3ce 100644
--- a/util/upatch_maps_otel.c
+++ b/util/upatch_maps_otel.c
@@ -2219,9 +2219,8 @@ static int snapshot_writer_finish(struct snapshot_writer *w,
return -EIO;
snapshot->json_bytes = w->json_bytes;
snapshot->object_bytes = w->object_bytes;
- for (size_t i = 0; i < sizeof(snapshot->sha256); i++)
- snprintf(snapshot->sha256_hex + i * 2, 3, "%02x",
- snapshot->sha256[i]);
+ hex_encode(snapshot->sha256, sizeof(snapshot->sha256),
+ snapshot->sha256_hex);
if (base64_encode(snapshot->sha256, sizeof(snapshot->sha256),
snapshot->sha256_b64,
sizeof(snapshot->sha256_b64)) != 0)