eBPF-based security platform
eventlog: write the fields every record starts with in one place
Daniel Borkmannd7886ae615b4
diff --git a/builtin-bpm.c b/builtin-bpm.cindex 18c6a4a1..52638066 100644--- a/builtin-bpm.c+++ b/builtin-bpm.c@@ -1874,8 +1874,7 @@ static int tree_measure_locked(const struct shirudo_tree_operation *op,{__u8 event[32], chain[32];struct shirudo_json_emit e;- char line[4096], ts[32], boot_id[64], nv_index[16];- char digest_hex[65], chain_hex[65];+ char line[4096], chain_hex[65];int rc;chain_tree_event(op->seq, operation_digest, event);@@ -1883,22 +1882,9 @@ static int tree_measure_locked(const struct shirudo_tree_operation *op,if (rc)return rc;- eventlog_timestamp(ts, sizeof(ts));- eventlog_boot_id(boot_id, sizeof(boot_id));- if (strsprintf(nv_index, sizeof(nv_index), "0x%08X",- SHIRUDO_NV_INDEX) < 0)- return -E2BIG;- hex_encode(event, 32, digest_hex);hex_encode(chain, 32, chain_hex);e = (struct shirudo_json_emit){ .buf = line, .cap = sizeof(line) };- json_obj_open(&e);- json_kv_int(&e, "schema", 1);- json_kv_string(&e, "ts", ts);- json_kv_string(&e, "nv_index", nv_index);- json_kv_string(&e, "alg", "sha256");- json_kv_string(&e, "digest", digest_hex);- json_kv_string(&e, "type", "tree_op");- json_kv_string(&e, "boot_id", boot_id);+ eventlog_record_open(&e, "tree_op", event);json_obj_open_key(&e, "tree");json_kv_int(&e, "seq", op->seq);json_kv_string(&e, "op", op->op);diff --git a/builtin-init.c b/builtin-init.cindex 64066428..bfb92962 100644--- a/builtin-init.c+++ b/builtin-init.c@@ -1043,22 +1043,15 @@ static int append_init_event(const struct shirudo_measured_upatch *set,char writer_hex[HEXLEN(SHIRUDO_TPM_NV_RSA_BYTES)];char buf[64 * 1024];struct shirudo_json_emit e = { .buf = buf, .cap = sizeof(buf) };- char cert_hex[HEXLEN(32)], set_hex[HEXLEN(32)], digest_hex[HEXLEN(32)];char pcr_hex[HEXLEN(32)], anchor_hex[HEXLEN(32)], chain_hex[HEXLEN(32)];- char phase_hex[HEXLEN(32)];char name_hex[HEXLEN(SHIRUDO_TPM_NAME_LEN)], tree_hex[HEXLEN(32)];- char ts[32], boot_id[64], nv_index[16];+ char cert_hex[HEXLEN(32)], set_hex[HEXLEN(32)];+ char phase_hex[HEXLEN(32)];size_t i;- eventlog_timestamp(ts, sizeof(ts));- eventlog_boot_id(boot_id, sizeof(boot_id));- if (strsprintf(nv_index, sizeof(nv_index), "0x%08X",- SHIRUDO_NV_INDEX) < 0)- return -E2BIG;hex_encode(cert_sha, 32, cert_hex);hex_encode(set_digest, 32, set_hex);hex_encode(tree_root, 32, tree_hex);- hex_encode(init_digest, 32, digest_hex);hex_encode(info->pcr_before, 32, pcr_hex);hex_encode(info->phase_before, 32, phase_hex);hex_encode(info->name, SHIRUDO_TPM_NAME_LEN, name_hex);@@ -1066,14 +1059,7 @@ static int append_init_event(const struct shirudo_measured_upatch *set,hex_encode(info->value, 32, chain_hex);hex_encode(info->writer_public, sizeof(info->writer_public), writer_hex);- json_obj_open(&e);- json_kv_int(&e, "schema", 1);- json_kv_string(&e, "ts", ts);- json_kv_string(&e, "nv_index", nv_index);- json_kv_string(&e, "alg", "sha256");- json_kv_string(&e, "digest", digest_hex);- json_kv_string(&e, "type", "init");- json_kv_string(&e, "boot_id", boot_id);+ eventlog_record_open(&e, "init", init_digest);json_kv_int(&e, "tpm_reset_count", info->reset_count);json_kv_string(&e, "cert_sha256", cert_hex);json_arr_open(&e, "certs");diff --git a/builtin-keys.c b/builtin-keys.cindex aa67c1a8..f0001b6c 100644--- a/builtin-keys.c+++ b/builtin-keys.c@@ -683,23 +683,8 @@ static int append_key_event(const char *type, const char *fp_hex,{char buf[1024];struct shirudo_json_emit e = { .buf = buf, .cap = sizeof(buf) };- char ts[32], boot_id[64], nv_index[16], digest_hex[65];-- eventlog_timestamp(ts, sizeof(ts));- eventlog_boot_id(boot_id, sizeof(boot_id));- if (strsprintf(nv_index, sizeof(nv_index), "0x%08X",- SHIRUDO_NV_INDEX) < 0)- return -E2BIG;- hex_encode(event, 32, digest_hex);-- json_obj_open(&e);- json_kv_int(&e, "schema", 1);- json_kv_string(&e, "ts", ts);- json_kv_string(&e, "nv_index", nv_index);- json_kv_string(&e, "alg", "sha256");- json_kv_string(&e, "digest", digest_hex);- json_kv_string(&e, "type", type);- json_kv_string(&e, "boot_id", boot_id);++ eventlog_record_open(&e, type, event);json_obj_open_key(&e, "key");json_kv_string(&e, "fingerprint", fp_hex);json_kv_string(&e, "cert_sha256", sha_hex);diff --git a/builtin-upatch.c b/builtin-upatch.cindex 81fab9b9..e3f3a0a1 100644--- a/builtin-upatch.c+++ b/builtin-upatch.c@@ -2175,24 +2175,10 @@ static int format_upatch_event(const char *type, const char *name,char *buf, size_t size){struct shirudo_json_emit e = { .buf = buf, .cap = size };- char ts[32], boot_id[64], nv_index[16], payload_hex[65], digest_hex[65];+ char payload_hex[65];- eventlog_timestamp(ts, sizeof(ts));- eventlog_boot_id(boot_id, sizeof(boot_id));- if (strsprintf(nv_index, sizeof(nv_index), "0x%08X",- SHIRUDO_NV_INDEX) < 0)- return -E2BIG;hex_encode(payload, 32, payload_hex);- hex_encode(event, 32, digest_hex);-- json_obj_open(&e);- json_kv_int(&e, "schema", 1);- json_kv_string(&e, "ts", ts);- json_kv_string(&e, "nv_index", nv_index);- json_kv_string(&e, "alg", "sha256");- json_kv_string(&e, "digest", digest_hex);- json_kv_string(&e, "type", type);- json_kv_string(&e, "boot_id", boot_id);+ eventlog_record_open(&e, type, event);json_obj_open_key(&e, "upatch");json_kv_string(&e, "name", name);json_kv_string(&e, "payload_sha256", payload_hex);diff --git a/util/eventlog.c b/util/eventlog.cindex 6904d9a5..2ac7504e 100644--- a/util/eventlog.c+++ b/util/eventlog.c@@ -22,6 +22,7 @@#include "chain.h"#include "eventlog.h"+#include "hex.h"#include "timefmt.h"#include "io.h"#include "json.h"@@ -496,3 +497,25 @@ void eventlog_boot_id(char *out, size_t cap)while (n > 0 && (out[n - 1] == '\n' || out[n - 1] == '\r'))out[--n] = '\0';}++void eventlog_record_open(struct shirudo_json_emit *e, const char *type,+ const __u8 digest[32])+{+ char ts[32], boot_id[64], nv_index[16], digest_hex[65];++ eventlog_timestamp(ts, sizeof(ts));+ eventlog_boot_id(boot_id, sizeof(boot_id));+ if (strsprintf(nv_index, sizeof(nv_index), "0x%08X",+ SHIRUDO_NV_INDEX) < 0 && !e->err)+ e->err = -E2BIG;+ hex_encode(digest, 32, digest_hex);++ json_obj_open(e);+ json_kv_int(e, "schema", 1);+ json_kv_string(e, "ts", ts);+ json_kv_string(e, "nv_index", nv_index);+ json_kv_string(e, "alg", "sha256");+ json_kv_string(e, "digest", digest_hex);+ json_kv_string(e, "type", type);+ json_kv_string(e, "boot_id", boot_id);+}diff --git a/util/eventlog.h b/util/eventlog.hindex 787c5043..5484778a 100644--- a/util/eventlog.h+++ b/util/eventlog.h@@ -85,4 +85,14 @@ int eventlog_would_exceed(__u64 max_bytes);void eventlog_timestamp(char *out, size_t cap); /* ISO-8601 UTC */void eventlog_boot_id(char *out, size_t cap); /* /proc boot_id */+struct shirudo_json_emit;++/*+ * Open a record and write the fields every record starts with, through+ * boot_id; the caller adds its own and closes the object. A failure+ * lands in e->err like any other.+ */+void eventlog_record_open(struct shirudo_json_emit *e, const char *type,+ const __u8 digest[32]);+#endif /* SHIRUDO_EVENTLOG_H */