eBPF-based security platform
util: one hex codec
Daniel Borkmannce9e4f59b57a
diff --git a/Makefile.shirudo b/Makefile.shirudoindex 60d78298..aa70f902 100644--- a/Makefile.shirudo+++ b/Makefile.shirudo@@ -512,7 +512,7 @@ TEST_HELPERS := testing/helper/test-json-parser \testing/helper/test-exec-churn \testing/kernel-obs/exec-paths-testing/helper/test-json-parser: testing/helper/test-json-parser.c util/json.o+testing/helper/test-json-parser: testing/helper/test-json-parser.c util/json.o util/hex.o util/malloc.o@printf ' CC $@\n'$(Q)$(CC) $(CFLAGS) -o $@ $^@@ -560,7 +560,7 @@ testing/helper/tpmsim-cmd: testing/helper/tpmsim-cmd.c@printf ' CC $@\n'$(Q)$(CC) $(CFLAGS_BASE) $(TPMSIM_CFLAGS) -o $@ $< $(TPMSIM_LIBS) -lcrypto-testing/helper/test-config: testing/helper/test-config.c util/config.o util/io.o util/log.o+testing/helper/test-config: testing/helper/test-config.c util/config.o util/io.o util/log.o util/hex.o util/malloc.o@printf ' CC $@\n'$(Q)$(CC) $(CFLAGS) -o $@ $^@@ -601,7 +601,7 @@ testing/helper/test-otel-map: testing/helper/test-otel-map.c util/upatch_maps.outil/tls_openssl.o util/serializer.o util/seal.o \util/upatch_decode.o util/upatch_resolve.o \util/upatch.o util/json.o util/log.o \- util/io.o util/malloc.o $(LIBBPF_A)+ util/io.o util/malloc.o $(LIBBPF_A) util/hex.o@printf ' CC $@\n'$(Q)$(CC) $(CFLAGS) -o $@ $^ $(DYNAMIC_LIBS)@@ -618,12 +618,12 @@ testing/helper/test-upatch-serializer-pages: testing/helper/test-upatch-serializutil/upatch_maps.o util/upatch_resolve.o \util/serializer.o util/seal.o \util/json.o util/malloc.o util/io.o \- util/log.o+ util/log.o util/hex.o@printf ' CC $@\n'$(Q)$(CC) $(CFLAGS) -o $@ $^ $(STATIC_LIBS) $(DYNAMIC_LIBS)testing/helper/test-upatch-decode: testing/helper/test-upatch-decode.c \- util/upatch_decode.o util/upatch.o util/malloc.o+ util/upatch_decode.o util/upatch.o util/malloc.o util/hex.o@printf ' CC $@\n'$(Q)$(CC) $(CFLAGS) -o $@ $^ $(DYNAMIC_LIBS)diff --git a/builtin-bpm.c b/builtin-bpm.cindex 67fcc894..59fd9ddd 100644--- a/builtin-bpm.c+++ b/builtin-bpm.c@@ -312,8 +312,8 @@ static int sha256_file_hex(const char *path, char *hex, size_t hex_sz)while ((nr = read(fd, buf, sizeof(buf))) > 0)if (shirudo_sha256_update(&ctx, buf, (size_t)nr) != 0) goto done;if (nr < 0 || shirudo_sha256_final(&ctx, digest) != 0) goto done;- for (unsigned int i = 0; i < sizeof(digest); i++) snprintf(hex + i * 2, 3, "%02x", digest[i]);- hex[64] = 0; rc = 0;+ shirudo_hex_encode(digest, sizeof(digest), hex);+ rc = 0;done:close(fd); shirudo_sha256_free(&ctx); return rc;}diff --git a/builtin-upatch.c b/builtin-upatch.cindex 2d79e3a1..515bac29 100644--- a/builtin-upatch.c+++ b/builtin-upatch.c@@ -45,6 +45,7 @@#include "util/identity.h"#include "util/keyring.h"#include "util/log.h"+#include "util/hex.h"#include "util/malloc.h"#include "util/paths.h"#include "util/report.h"@@ -124,10 +125,10 @@ static const char *decoder_signal_str(uint32_t signal)static int print_sha256_hex(const unsigned char hash[32]){- for (int i = 0; i < 32; i++)- if (printf("%02x", hash[i]) < 0)- return -1;- return 0;+ char hex[65];++ shirudo_hex_encode(hash, 32, hex);+ return fputs(hex, stdout) < 0 ? -1 : 0;}static const struct shirudo_help upatch_info_help = {@@ -3328,21 +3329,6 @@ static int cmd_upatch_map(int argc, const char **argv)return ret;}-static void payload_hex(const char *upatch, char *out, size_t cap)-{- uint8_t raw[32];- int rc = get_pin_payload(upatch, raw);- size_t i;-- out[0] = '\0';- if (rc != 0)- return;- if (cap < 65)- return;- for (i = 0; i < 32; i++)- snprintf(out + 2 * i, cap - 2 * i, "%02x", raw[i]);-}-struct upatch_get_ctx {const char *upatch;uint32_t prog_id;@@ -3400,7 +3386,7 @@ static int upatch_get_collect(const char *want, struct upatch_get_ctx *c)return -EINVAL;c->prog_id = linfo.prog_id;shirudo_upatch_loaded_at(want, c->loaded_at, sizeof(c->loaded_at));- payload_hex(want, c->payload, sizeof(c->payload));+ shirudo_upatch_payload_sha256(want, c->payload, sizeof(c->payload));vbtf = btf__load_vmlinux_btf();shirudo_upatch_resolve_location(vbtf, want, c->location,diff --git a/tools/shirudo-build/bpm-build.c b/tools/shirudo-build/bpm-build.cindex 515164b7..d4febc3d 100644--- a/tools/shirudo-build/bpm-build.c+++ b/tools/shirudo-build/bpm-build.c@@ -27,6 +27,7 @@#include "crypto.h"#include "json.h"#include "log.h"+#include "hex.h"#include "malloc.h"#include "package.h"#include "paths.h"@@ -682,17 +683,6 @@ static int build_entry_name(char *out, size_t cap, const char *top,return 0;}-static void build_hex(const unsigned char *in, char *out, size_t n)-{- static const char hex[] = "0123456789abcdef";-- for (size_t i = 0; i < n; i++) {- out[i * 2] = hex[in[i] >> 4];- out[i * 2 + 1] = hex[in[i] & 15];- }- out[n * 2] = '\0';-}-static bool build_ends_with(const char *s, const char *suffix){size_t n = strlen(s), m = strlen(suffix);@@ -782,8 +772,9 @@ static int build_upatches(struct bpm_build_spec *spec, const char *pkey,return -EINVAL;}build_copy(u->name, sizeof(u->name), parsed.hdr.name);- build_hex(parsed.hdr.payload_sha256, u->payload_sha256,- SHIRUDO_CRYPTO_SHA256_LEN);+ shirudo_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), shirudo_upatch_type_str(&parsed));shirudo_upatch_close(&parsed);@@ -1148,7 +1139,7 @@ static int shirudo_build_bpm_build(int argc, char **argv)rc = shirudo_cert_pubkey_digest(&cert, digest);if (rc)goto out_cert;- build_hex(digest, fingerprint, sizeof(digest));+ shirudo_hex_encode(digest, sizeof(digest), fingerprint);/** Compile in the package directory so ./vmlinux.h and the vendored* headers resolve the way they do for `shirudo build` run there.diff --git a/tools/shirudo-build/shirudo-build.c b/tools/shirudo-build/shirudo-build.cindex 0eaf17c7..bd047e01 100644--- a/tools/shirudo-build/shirudo-build.c+++ b/tools/shirudo-build/shirudo-build.c@@ -19,6 +19,7 @@#include "bpm.h"#include "compile.h"#include "log.h"+#include "hex.h"#include "malloc.h"#include "package.h"#include "paths.h"@@ -29,7 +30,6 @@#define SHIRUDO_VMLINUX_PATH "/usr/local/share/shirudo/vmlinux.h"#endif-#define SHIRUDO_SHA256_HEX_LEN 64static void usage(FILE *fp){@@ -78,22 +78,6 @@ static void usage(FILE *fp)SHIRUDO_CERT_PATH, SHIRUDO_VMLINUX_PATH);}-static int parse_hex_sha256(const char *s, uint8_t out[32])-{- int i;-- if (strlen(s) != SHIRUDO_SHA256_HEX_LEN)- return -EINVAL;- for (i = 0; i < 32; i++) {- unsigned int b;-- if (sscanf(s + 2 * i, "%2x", &b) != 1)- return -EINVAL;- out[i] = (uint8_t)b;- }- return 0;-}-static const char *derive_name(const char *input, char *buf, size_t buf_sz){const char *base;@@ -335,8 +319,8 @@ int main(int argc, char **argv)return 2;}if (payload_sha256_hex) {- if (parse_hex_sha256(payload_sha256_hex,- payload_sha256)) {+ if (shirudo_hex_decode(payload_sha256_hex,+ payload_sha256, 32)) {pr_err("--payload-sha256 must be 64 hex chars");return 2;}diff --git a/util/config.c b/util/config.cindex f99146de..f35684d2 100644--- a/util/config.c+++ b/util/config.c@@ -13,6 +13,7 @@#include <string.h>#include "config.h"+#include "hex.h"#include "json.h"#include "log.h"#include "paths.h"@@ -67,17 +68,6 @@ static void config_defaults(struct shirudo_config *cfg)cfg->map_export_otlp_max_bytes = SHIRUDO_MAP_EXPORT_OTLP_MAX_DEFAULT;}-static int hexval(char c)-{- if (c >= '0' && c <= '9')- return c - '0';- if (c >= 'a' && c <= 'f')- return c - 'a' + 10;- if (c >= 'A' && c <= 'F')- return c - 'A' + 10;- return -1;-}-/** Owner auth is a literal passphrase, or "hex:<even hex>" for a binary* auth value (what provisioning tools that randomise owner auth emit).@@ -96,7 +86,8 @@ static void set_owner_auth(struct shirudo_config *cfg, const char *val)return;}for (i = 0; i < hl / 2; i++) {- int hi = hexval(h[2 * i]), lo = hexval(h[2 * i + 1]);+ int hi = shirudo_hex_nibble(h[2 * i]);+ int lo = shirudo_hex_nibble(h[2 * i + 1]);if (hi < 0 || lo < 0) {pr_warn("config: tpm.owner_auth: bad hex "diff --git a/util/hex.c b/util/hex.cindex 53044a71..22891508 100644--- a/util/hex.c+++ b/util/hex.c@@ -5,6 +5,7 @@#include <string.h>#include "hex.h"+#include "malloc.h"void shirudo_hex_encode(const uint8_t *in, size_t n, char *out){@@ -18,7 +19,16 @@ void shirudo_hex_encode(const uint8_t *in, size_t n, char *out)out[2 * n] = '\0';}-static int hex_nibble(char c)+char *shirudo_hex_alloc(const void *in, size_t n)+{+ char *out = xmalloc(2 * n + 1);++ if (out)+ shirudo_hex_encode(in, n, out);+ return out;+}++int shirudo_hex_nibble(int c){if (c >= '0' && c <= '9')return c - '0';@@ -36,8 +46,8 @@ int shirudo_hex_decode(const char *hex, uint8_t *out, size_t n)if (strlen(hex) != 2 * n)return -EINVAL;for (i = 0; i < n; i++) {- int hi = hex_nibble(hex[2 * i]);- int lo = hex_nibble(hex[2 * i + 1]);+ int hi = shirudo_hex_nibble(hex[2 * i]);+ int lo = shirudo_hex_nibble(hex[2 * i + 1]);if (hi < 0 || lo < 0)return -EINVAL;diff --git a/util/hex.h b/util/hex.hindex b90a8c08..1220a924 100644--- a/util/hex.h+++ b/util/hex.h@@ -13,6 +13,11 @@/* out needs 2 * n + 1 bytes. */void shirudo_hex_encode(const uint8_t *in, size_t n, char *out);+/* The same, xmalloc'd to 2 * n + 1 bytes. */+char *shirudo_hex_alloc(const void *in, size_t n);++/* The value of one hex digit, -1 for any other byte. */+int shirudo_hex_nibble(int c);/* Exactly 2 * n hex digits, either case. 0 or -EINVAL. */int shirudo_hex_decode(const char *hex, uint8_t *out, size_t n);diff --git a/util/json.c b/util/json.cindex 1126d4a1..8ab6fe7e 100644--- a/util/json.c+++ b/util/json.c@@ -12,6 +12,7 @@#include <stdlib.h>#include <string.h>+#include "hex.h"#include "json.h"static void put(struct shirudo_json_emit *e, char c)@@ -243,23 +244,12 @@ static void skip_ws(struct pctx *c)}}-static int hex_nibble(char ch)-{- if (ch >= '0' && ch <= '9')- return ch - '0';- if (ch >= 'a' && ch <= 'f')- return ch - 'a' + 10;- if (ch >= 'A' && ch <= 'F')- return ch - 'A' + 10;- return -1;-}-static int hex4(const char *s){int v = 0, i;for (i = 0; i < 4; i++) {- int n = hex_nibble(s[i]);+ int n = shirudo_hex_nibble(s[i]);if (n < 0)return -1;diff --git a/util/upatch_decode.c b/util/upatch_decode.cindex d6785224..19abf900 100644--- a/util/upatch_decode.c+++ b/util/upatch_decode.c@@ -80,6 +80,7 @@ int shirudo_upatch_decode_map_file_otel(const char *path,#include <luajit.h>#include "json.h"+#include "hex.h"#include "malloc.h"#include "paths.h"#include "upatch.h"@@ -549,17 +550,6 @@ static int lua_json_emit_value(lua_State *L, int idx, struct json_out *out,return out->err;}-static int hex_nibble(int c)-{- if (c >= '0' && c <= '9')- return c - '0';- if (c >= 'a' && c <= 'f')- return c - 'a' + 10;- if (c >= 'A' && c <= 'F')- return c - 'A' + 10;- return -1;-}-static int hex_decode_alloc(const char *hex, size_t len,unsigned char **out, size_t *out_len){@@ -583,14 +573,14 @@ static int hex_decode_alloc(const char *hex, size_t len,i++;if (i >= len)break;- hi = hex_nibble((unsigned char)hex[i++]);+ hi = shirudo_hex_nibble((unsigned char)hex[i++]);while (i < len && isspace((unsigned char)hex[i]))i++;if (i >= len) {xfree(buf, digits / 2);return -EINVAL;}- lo = hex_nibble((unsigned char)hex[i++]);+ lo = shirudo_hex_nibble((unsigned char)hex[i++]);if (hi < 0 || lo < 0) {xfree(buf, digits / 2);return -EINVAL;@@ -602,24 +592,6 @@ static int hex_decode_alloc(const char *hex, size_t len,return 0;}-static char *hex_alloc(const void *data, uint32_t len)-{- static const char hexdigits[] = "0123456789abcdef";- const unsigned char *p = data;- char *out;- uint32_t i;-- out = xmalloc((size_t)len * 2 + 1);- if (!out)- return NULL;- for (i = 0; i < len; i++) {- out[2 * i] = hexdigits[p[i] >> 4];- out[2 * i + 1] = hexdigits[p[i] & 0xf];- }- out[(size_t)len * 2] = '\0';- return out;-}-static struct lua_slot *lua_check_slot(lua_State *L){return luaL_checkudata(L, 1, SHIRUDO_LUA_SLOT_MT);@@ -898,7 +870,8 @@ static int lua_shirudo_hex_u64(lua_State *L)}for (i = 7; i >= 0; i--) {const char *p = hex + (size_t)off * 2 + (size_t)i * 2;- int hi = hex_nibble(p[0]), lo = hex_nibble(p[1]);+ int hi = shirudo_hex_nibble(p[0]);+ int lo = shirudo_hex_nibble(p[1]);if (hi < 0 || lo < 0) {lua_push_u64_string(L, 0);@@ -1095,8 +1068,8 @@ static void lua_push_map_input(lua_State *L, const struct shirudo_map_view *v)for (i = 0; i < v->n_entries; i++) {const struct shirudo_map_entry *entry = &v->entries[i];size_t value_bytes = (size_t)v->value_stride * v->n_cpus;- char *key_hex = hex_alloc(entry->key, v->key_size);- char *value_hex = hex_alloc(entry->value, value_bytes);+ char *key_hex = shirudo_hex_alloc(entry->key, v->key_size);+ char *value_hex = shirudo_hex_alloc(entry->value, value_bytes);if (!key_hex || !value_hex) {xfree(key_hex, (size_t)v->key_size * 2 + 1);@@ -1117,7 +1090,7 @@ static void lua_push_map_input(lua_State *L, const struct shirudo_map_view *v)lua_newtable(L);for (p = 0; p < v->n_serial_pages; p++) {const struct shirudo_serial_page *page = &v->serial_pages[p];- char *hex = hex_alloc(page->data, page->bytes);+ char *hex = shirudo_hex_alloc(page->data, page->bytes);if (!hex)luaL_error(L, "decoder page data is too large");diff --git a/util/upatch_maps.c b/util/upatch_maps.cindex ec2424cd..461d3914 100644--- a/util/upatch_maps.c+++ b/util/upatch_maps.c@@ -25,6 +25,7 @@#include <shirudo/upatch.h>#include "json.h"+#include "hex.h"#include "malloc.h"#include "paths.h"#include "serializer.h"@@ -1404,24 +1405,6 @@ static void print_type_def(const char *label, const char *def, uint32_t size)printf(" (%u B)\n", size);}-char *shirudo_upatch_map_hex_alloc(const void *data, uint32_t len)-{- static const char hexdigits[] = "0123456789abcdef";- const unsigned char *p = data;- char *out;- uint32_t i;-- out = xmalloc((size_t)len * 2 + 1);- if (!out)- return NULL;- for (i = 0; i < len; i++) {- out[2 * i] = hexdigits[p[i] >> 4];- out[2 * i + 1] = hexdigits[p[i] & 0xf];- }- out[(size_t)len * 2] = '\0';- return out;-}-static void print_serialized_hex(const unsigned char *data, uint32_t len){uint32_t i;@@ -1572,7 +1555,7 @@ void shirudo_upatch_maps_emit_json_fields(struct shirudo_json_emit *e,for (p = 0; p < v->n_serial_pages; p++) {const struct shirudo_serial_page *page =&v->serial_pages[p];- char *hex = shirudo_upatch_map_hex_alloc(page->data,+ char *hex = shirudo_hex_alloc(page->data,page->bytes);shirudo_json_obj_open(e);diff --git a/util/upatch_maps.h b/util/upatch_maps.hindex 9b5eb024..60dfbea8 100644--- a/util/upatch_maps.h+++ b/util/upatch_maps.h@@ -138,6 +138,5 @@ char *shirudo_upatch_map_format_cell(const struct shirudo_map_view *v,char *shirudo_upatch_map_format_percpu_sum(const struct shirudo_map_view *v,const void *values_per_cpu,const char *btf_type);-char *shirudo_upatch_map_hex_alloc(const void *data, uint32_t len);#endif /* SHIRUDO_UTIL_UPATCH_MAPS_H */diff --git a/util/upatch_maps_otel.c b/util/upatch_maps_otel.cindex 8cf5eaf6..0daea814 100644--- a/util/upatch_maps_otel.c+++ b/util/upatch_maps_otel.c@@ -32,6 +32,7 @@#include <zlib.h>#include "crypto.h"+#include "hex.h"#include "json.h"#include "config.h"#include "malloc.h"@@ -635,7 +636,7 @@ static __attribute__((unused)) void emit_log_map_data(struct shirudo_json_emit *otlp_array_begin(e, "pages");for (p = 0; p < v->n_serial_pages; p++) {const struct shirudo_serial_page *page = &v->serial_pages[p];- char *hex = shirudo_upatch_map_hex_alloc(page->data,+ char *hex = shirudo_hex_alloc(page->data,page->bytes);otlp_array_kvlist_begin(e);@@ -1316,14 +1317,6 @@ static int append_header(struct otel_state *st, const char *line)return 0;}-static int hex_digit(char c)-{- if (c >= '0' && c <= '9') return c - '0';- if (c >= 'a' && c <= 'f') return c - 'a' + 10;- if (c >= 'A' && c <= 'F') return c - 'A' + 10;- return -1;-}-static int percent_decode(char *s){char *src = s, *dst = s;@@ -1332,8 +1325,8 @@ static int percent_decode(char *s)if (*src == '%') {if (!src[1] || !src[2])return -EINVAL;- int hi = hex_digit(src[1]);- int lo = hex_digit(src[2]);+ int hi = shirudo_hex_nibble(src[1]);+ int lo = shirudo_hex_nibble(src[2]);if (hi < 0 || lo < 0)return -EINVAL;diff --git a/util/upatch_resolve.c b/util/upatch_resolve.cindex 1d6c5657..34fcd595 100644--- a/util/upatch_resolve.c+++ b/util/upatch_resolve.c@@ -24,6 +24,7 @@#include "malloc.h"#include "paths.h"+#include "hex.h"#include "upatch_resolve.h"#ifndef NFPROTO_IPV6@@ -207,7 +208,6 @@ void shirudo_upatch_payload_sha256(const char *name, char *out, size_t cap)char bpffs_name[SHIRUDO_UPATCH_BPFFS_NAME_MAX + 1];uint8_t raw[32];ssize_t n;- size_t i;out[0] = '\0';if (cap < 65)@@ -220,6 +220,5 @@ void shirudo_upatch_payload_sha256(const char *name, char *out, size_t cap)n = getxattr(dir, SHIRUDO_XATTR_PAYLOAD, raw, sizeof(raw));if (n != (ssize_t)sizeof(raw))return;- for (i = 0; i < sizeof(raw); i++)- snprintf(out + 2 * i, cap - 2 * i, "%02x", raw[i]);+ shirudo_hex_encode(raw, sizeof(raw), out);}