eBPF-based security platform
util: share the wire helpers
Daniel Borkmannb7ff024f4267
diff --git a/builtin-upatch.c b/builtin-upatch.cindex 515bac29..92f240ef 100644--- a/builtin-upatch.c+++ b/builtin-upatch.c@@ -710,22 +710,6 @@ static int prog_load_signed(const struct bpf_insn *insns, size_t insn_cnt,return (int)fd;}-static int prog_test_run_ctx(int prog_fd, void *ctx, uint32_t ctx_sz,- uint32_t *retval)-{- union bpf_attr attr = {};- long ret;-- attr.test.prog_fd = prog_fd;- attr.test.ctx_in = (uint64_t)(uintptr_t)ctx;- attr.test.ctx_size_in = ctx_sz;- ret = syscall(__NR_bpf, BPF_PROG_TEST_RUN, &attr, sizeof(attr));- if (ret < 0)- return -errno;- *retval = attr.test.retval;- return 0;-}-/* A NULL name attaches at the attach_btf_id the signed program carries. */static int prog_attach_raw_tp(int prog_fd){@@ -1606,8 +1590,8 @@ static int attach_and_pin_progs(const struct shirudo_loader_ctx *cx,uint32_t retval = 0;/* A zero context is the load-time arena initialization call. */- rc = prog_test_run_ctx(test_fd, init_ctx, sizeof(init_ctx),- &retval);+ rc = shirudo_prog_test_run(test_fd, init_ctx, sizeof(init_ctx),+ &retval);if (rc || (int32_t)retval < 0) {pr_err("initialize test-run arena: %s",rc ? strerror(-rc) : strerror(-(int32_t)retval));@@ -2049,7 +2033,7 @@ static int upatch_apply(const char *path, bool verify_only,cx->log_size = SHIRUDO_VERIFIER_LOG_MAX;cx->log_buf = (uint64_t)(uintptr_t)vlog;- rc = prog_test_run_ctx(prog_fd, cx, (uint32_t)ctx_sz, &retval);+ rc = shirudo_prog_test_run(prog_fd, cx, (uint32_t)ctx_sz, &retval);if (rc) {pr_err("BPF_PROG_TEST_RUN: %s", strerror(errno));if (vlog[0])diff --git a/include/shirudo/upatch.h b/include/shirudo/upatch.hindex 7656fad3..b31ebd6c 100644--- a/include/shirudo/upatch.h+++ b/include/shirudo/upatch.h@@ -8,7 +8,9 @@#ifndef SHIRUDO_UPATCH_H#define SHIRUDO_UPATCH_H+#include <endian.h>#include <stdint.h>+#include <string.h>#define SHIRUDO_UPATCH_MAGIC 0x53485244554e4150ULL /* "SHRDUNAP" *//*@@ -199,4 +201,28 @@ struct shirudo_upatch_uprobe_target {uint32_t nr_offsets;};+static inline uint16_t rd_le16(const void *p)+{+ uint16_t v;++ memcpy(&v, p, sizeof(v));+ return le16toh(v);+}++static inline uint32_t rd_le32(const void *p)+{+ uint32_t v;++ memcpy(&v, p, sizeof(v));+ return le32toh(v);+}++static inline uint64_t rd_le64(const void *p)+{+ uint64_t v;++ memcpy(&v, p, sizeof(v));+ return le64toh(v);+}+#endif /* SHIRUDO_UPATCH_H */diff --git a/tools/shirudo-build/package.c b/tools/shirudo-build/package.cindex 7a0e293d..cc1db687 100644--- a/tools/shirudo-build/package.c+++ b/tools/shirudo-build/package.c@@ -105,22 +105,6 @@ static void pack_header(struct shirudo_upatch_header *h,h->reserved1 = 0;}-static uint16_t rd_le16(const void *p)-{- uint16_t v;-- memcpy(&v, p, sizeof(v));- return le16toh(v);-}--static uint32_t rd_le32(const void *p)-{- uint32_t v;-- memcpy(&v, p, sizeof(v));- return le32toh(v);-}-static int read_small_file(const char *path, char **out, uint32_t *out_len){struct stat st;diff --git a/util/tpm.c b/util/tpm.cindex c3542e8b..da22a13e 100644--- a/util/tpm.c+++ b/util/tpm.c@@ -501,19 +501,6 @@ int shirudo_tpm_nv_extend(struct shirudo_tpm *t, uint32_t index,return err;}-static void put16(uint8_t *p, uint16_t v)-{- p[0] = (uint8_t)(v >> 8);- p[1] = (uint8_t)v;-}--static size_t put2b(uint8_t *p, const uint8_t *data, uint16_t len)-{- put16(p, len);- memcpy(p + 2, data, len);- return 2u + len;-}-static int public_from_tss(const TPMT_PUBLIC *p, uint8_t *out, size_t cap,size_t *len){@@ -574,12 +561,12 @@ static int marshal_signature(const TPMT_SIGNATURE *sig, uint8_t *out,if (sig->sigAlg != TPM2_ALG_ECDSA || e->signatureR.size > 64 ||e->signatureS.size > 64 || cap < 8u + 128u)return -EINVAL;- put16(out + off, sig->sigAlg);+ tpm_put16(out + off, sig->sigAlg);off += 2;- put16(out + off, e->hash);+ tpm_put16(out + off, e->hash);off += 2;- off += put2b(out + off, e->signatureR.buffer, e->signatureR.size);- off += put2b(out + off, e->signatureS.buffer, e->signatureS.size);+ off += tpm_put2b(out + off, e->signatureR.buffer, e->signatureR.size);+ off += tpm_put2b(out + off, e->signatureS.buffer, e->signatureS.size);*len = off;return 0;}diff --git a/util/tpm_wire.c b/util/tpm_wire.cindex 179225a3..3d0f3e2f 100644--- a/util/tpm_wire.c+++ b/util/tpm_wire.c@@ -20,20 +20,6 @@ struct reader {size_t left;};-static void put16(uint8_t *p, uint16_t v)-{- p[0] = (uint8_t)(v >> 8);- p[1] = (uint8_t)v;-}--static void put32(uint8_t *p, uint32_t v)-{- p[0] = (uint8_t)(v >> 24);- p[1] = (uint8_t)(v >> 16);- p[2] = (uint8_t)(v >> 8);- p[3] = (uint8_t)v;-}-static int get8(struct reader *r, uint8_t *v){if (r->left < 1)@@ -107,7 +93,7 @@ static void policy_hash(const uint8_t prev[32], uint32_t cc,struct shirudo_sha256_ctx ctx;uint8_t cc_be[4];- put32(cc_be, cc);+ tpm_put32(cc_be, cc);if (shirudo_sha256_init(&ctx) != 0) {memset(out, 0, 32);return;@@ -134,8 +120,8 @@ static voidshirudo_tpm_pcr_selection(uint32_t pcr_mask,uint8_t out[SHIRUDO_TPM_PCR_SELECTION_LEN]){- put32(out, 1);- put16(out + 4, SHIRUDO_TPM_ALG_SHA256);+ tpm_put32(out, 1);+ tpm_put16(out + 4, SHIRUDO_TPM_ALG_SHA256);out[6] = SHIRUDO_TPM_PCR_SELECT_LEN;out[7] = (uint8_t)pcr_mask;out[8] = (uint8_t)(pcr_mask >> 8);@@ -181,13 +167,13 @@ void shirudo_tpm_nv_name(uint32_t index, uint32_t attrs,{uint8_t pub[4 + 2 + 4 + 2 + 32 + 2];- put32(pub, index);- put16(pub + 4, SHIRUDO_TPM_ALG_SHA256);- put32(pub + 6, attrs);- put16(pub + 10, 32);+ tpm_put32(pub, index);+ tpm_put16(pub + 4, SHIRUDO_TPM_ALG_SHA256);+ tpm_put32(pub + 6, attrs);+ tpm_put16(pub + 10, 32);memcpy(pub + 12, policy, 32);- put16(pub + 44, data_size);- put16(name, SHIRUDO_TPM_ALG_SHA256);+ tpm_put16(pub + 44, data_size);+ tpm_put16(name, SHIRUDO_TPM_ALG_SHA256);shirudo_sha256_buffer(pub, sizeof(pub), name + 2);}@@ -200,7 +186,7 @@ int shirudo_tpm_object_name(const uint8_t *public, size_t len,if (get16(&r, &type) || get16(&r, &alg) ||alg != SHIRUDO_TPM_ALG_SHA256)return -EINVAL;- put16(name, SHIRUDO_TPM_ALG_SHA256);+ tpm_put16(name, SHIRUDO_TPM_ALG_SHA256);shirudo_sha256_buffer(public, len, name + 2);return 0;}@@ -211,9 +197,9 @@ void shirudo_tpm_qualified_name(uint32_t hierarchy,{uint8_t buf[4 + SHIRUDO_TPM_NAME_LEN];- put32(buf, hierarchy);+ tpm_put32(buf, hierarchy);memcpy(buf + 4, name, SHIRUDO_TPM_NAME_LEN);- put16(out, SHIRUDO_TPM_ALG_SHA256);+ tpm_put16(out, SHIRUDO_TPM_ALG_SHA256);shirudo_sha256_buffer(buf, sizeof(buf), out + 2);}@@ -275,13 +261,6 @@ int shirudo_tpm_public_parse(const uint8_t *buf, size_t len,return 0;}-static size_t put2b(uint8_t *p, const uint8_t *data, size_t len)-{- put16(p, (uint16_t)len);- memcpy(p + 2, data, len);- return 2 + len;-}-int shirudo_tpm_public_marshal(const struct shirudo_tpm_public *pub,uint8_t *out, size_t cap, size_t *len){@@ -301,37 +280,37 @@ int shirudo_tpm_public_marshal(const struct shirudo_tpm_public *pub,return -EINVAL;if (cap < need)return -E2BIG;- put16(out + off, pub->type);+ tpm_put16(out + off, pub->type);off += 2;- put16(out + off, SHIRUDO_TPM_ALG_SHA256);+ tpm_put16(out + off, SHIRUDO_TPM_ALG_SHA256);off += 2;- put32(out + off, pub->attrs);+ tpm_put32(out + off, pub->attrs);off += 4;- off += put2b(out + off, pub->policy, pub->policy_len);- put16(out + off, pub->sym_alg);+ off += tpm_put2b(out + off, pub->policy, pub->policy_len);+ tpm_put16(out + off, pub->sym_alg);off += 2;if (pub->sym_alg != SHIRUDO_TPM_ALG_NULL) {- put16(out + off, pub->sym_bits);- put16(out + off + 2, pub->sym_mode);+ tpm_put16(out + off, pub->sym_bits);+ tpm_put16(out + off + 2, pub->sym_mode);off += 4;}- put16(out + off, pub->scheme);+ tpm_put16(out + off, pub->scheme);off += 2;if (pub->scheme != SHIRUDO_TPM_ALG_NULL) {- put16(out + off, pub->scheme_hash);+ tpm_put16(out + off, pub->scheme_hash);off += 2;}if (pub->type == SHIRUDO_TPM_ALG_ECC) {- put16(out + off, pub->curve);- put16(out + off + 2, SHIRUDO_TPM_ALG_NULL);+ tpm_put16(out + off, pub->curve);+ tpm_put16(out + off + 2, SHIRUDO_TPM_ALG_NULL);off += 4;- off += put2b(out + off, pub->x, 32);- off += put2b(out + off, pub->y, 32);+ off += tpm_put2b(out + off, pub->x, 32);+ off += tpm_put2b(out + off, pub->y, 32);} else {- put16(out + off, pub->rsa_bits);- put32(out + off + 2, pub->rsa_exponent);+ tpm_put16(out + off, pub->rsa_bits);+ tpm_put32(out + off + 2, pub->rsa_exponent);off += 6;- off += put2b(out + off, pub->n, pub->n_len);+ off += tpm_put2b(out + off, pub->n, pub->n_len);}*len = off;return 0;@@ -373,7 +352,7 @@ int shirudo_tpm_kdfa(const uint8_t *key, size_t key_len, const char *label,v_len > SHIRUDO_TPM_NAME_LEN || out_len > 4096)return -EINVAL;for (i = 1; done < out_len; i++) {- put32(buf, i);+ tpm_put32(buf, i);off = 4;memcpy(buf + off, label, label_len);off += label_len;@@ -384,7 +363,7 @@ int shirudo_tpm_kdfa(const uint8_t *key, size_t key_len, const char *label,if (v_len)memcpy(buf + off, ctx_v, v_len);off += v_len;- put32(buf + off, (uint32_t)(out_len * 8));+ tpm_put32(buf + off, (uint32_t)(out_len * 8));off += 4;if (shirudo_hmac_sha256(key, key_len, buf, off, block))return -EIO;@@ -407,7 +386,7 @@ void shirudo_tpm_kdfe(const uint8_t z[32], const char *label,memset(out, 0, 32);return;}- put32(buf, 1);+ tpm_put32(buf, 1);memcpy(buf + off, z, 32);off += 32;memcpy(buf + off, label, label_len);@@ -436,7 +415,7 @@ int shirudo_tpm_credential_protect(const uint8_t seed[32],if (shirudo_tpm_kdfa(seed, 32, "STORAGE", name, SHIRUDO_TPM_NAME_LEN,NULL, 0, sym_key, sizeof(sym_key)))goto out;- put16(plain, SHIRUDO_TPM_CREDENTIAL_LEN);+ tpm_put16(plain, SHIRUDO_TPM_CREDENTIAL_LEN);memcpy(plain + 2, credential, SHIRUDO_TPM_CREDENTIAL_LEN);if (shirudo_aes128_cfb_encrypt(sym_key, plain, sizeof(plain), enc) ||shirudo_tpm_kdfa(seed, 32, "INTEGRITY", NULL, 0, NULL, 0, hmac_key,@@ -447,7 +426,7 @@ int shirudo_tpm_credential_protect(const uint8_t seed[32],if (shirudo_hmac_sha256(hmac_key, sizeof(hmac_key), mac_in,sizeof(mac_in), mac))goto out;- put16(out, 32);+ tpm_put16(out, 32);memcpy(out + 2, mac, 32);memcpy(out + 34, enc, sizeof(enc));rc = 0;diff --git a/util/tpm_wire.h b/util/tpm_wire.hindex e699f6b7..4aab2ce1 100644--- a/util/tpm_wire.h+++ b/util/tpm_wire.h@@ -12,6 +12,29 @@#include <stdbool.h>#include <stddef.h>#include <stdint.h>+#include <string.h>++static inline void tpm_put16(uint8_t *p, uint16_t v)+{+ p[0] = (uint8_t)(v >> 8);+ p[1] = (uint8_t)v;+}++static inline void tpm_put32(uint8_t *p, uint32_t v)+{+ p[0] = (uint8_t)(v >> 24);+ p[1] = (uint8_t)(v >> 16);+ p[2] = (uint8_t)(v >> 8);+ p[3] = (uint8_t)v;+}++/* A TPM2B: the size, then the bytes. */+static inline size_t tpm_put2b(uint8_t *p, const uint8_t *data, size_t len)+{+ tpm_put16(p, (uint16_t)len);+ memcpy(p + 2, data, len);+ return 2 + len;+}#define SHIRUDO_TPM_DIGEST_LEN 32#define SHIRUDO_TPM_NAME_LEN (2 + SHIRUDO_TPM_DIGEST_LEN)diff --git a/util/upatch.c b/util/upatch.cindex a831d1ec..87897358 100644--- a/util/upatch.c+++ b/util/upatch.c@@ -17,22 +17,6 @@#include "upatch.h"-static uint32_t rd_le32(const void *p)-{- uint32_t v;-- memcpy(&v, p, sizeof(v));- return le32toh(v);-}--static uint64_t rd_le64(const void *p)-{- uint64_t v;-- memcpy(&v, p, sizeof(v));- return le64toh(v);-}-static int parse_header(const unsigned char *m, size_t n,struct shirudo_upatch_header *out, size_t *header_size){diff --git a/util/upatch_maps.c b/util/upatch_maps.cindex 461d3914..8184e30f 100644--- a/util/upatch_maps.c+++ b/util/upatch_maps.c@@ -702,8 +702,8 @@ static int open_serializer_pin(const char *upatch, const char *map_name)return fd;}-static int prog_test_run_ctx(int prog_fd, void *ctx, uint32_t ctx_sz,- uint32_t *retval)+int shirudo_prog_test_run(int prog_fd, void *ctx, uint32_t ctx_sz,+ uint32_t *retval){union bpf_attr attr = {};long ret;@@ -928,7 +928,7 @@ static int try_read_serialized(const char *upatch, const char *map,ctx.cursor = cursor;ctx.invocation_id = invocation_id;- rc = prog_test_run_ctx(prog_fd, &ctx, sizeof(ctx), &retval);+ rc = shirudo_prog_test_run(prog_fd, &ctx, sizeof(ctx), &retval);if (rc)goto out;if ((int32_t)retval < 0) {diff --git a/util/upatch_maps.h b/util/upatch_maps.hindex 60dfbea8..5c5385d1 100644--- a/util/upatch_maps.h+++ b/util/upatch_maps.h@@ -139,4 +139,7 @@ char *shirudo_upatch_map_format_percpu_sum(const struct shirudo_map_view *v,const void *values_per_cpu,const char *btf_type);+int shirudo_prog_test_run(int prog_fd, void *ctx, uint32_t ctx_sz,+ uint32_t *retval);+#endif /* SHIRUDO_UTIL_UPATCH_MAPS_H */