eBPF-based security platform
upatch, keys, bpm: write an event's record out before the extend
Daniel Borkmanne1dc250e1f9b
diff --git a/builtin-bpm.c b/builtin-bpm.cindex 2f47e2df..ac7a0d4f 100644--- a/builtin-bpm.c+++ b/builtin-bpm.c@@ -1978,34 +1978,58 @@ static int tree_status_update(const char *id, bool module_absent)* tree's own projection stays a file chain; the register is what an* operator attests.*/+/* The record of a tree operation up to the register value that ends it. */+static void tree_record_open(struct json_emit *e,+ const struct tree_operation *op,+ const __u8 event[32])+{+ 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);+ json_kv_string(e, "id", op->id);+ json_kv_string(e, "operation_digest", op->operation_digest);+ json_obj_close(e);+}++/* On a copy of the emitter, whose buffer is fixed, to end the record twice. */+static int tree_record_close(struct json_emit e, const __u8 chain[32])+{+ char chain_hex[65];++ hex_encode(chain, 32, chain_hex);+ json_kv_string(&e, "chain", chain_hex);+ json_obj_close(&e);+ return e.err;+}+static int tree_measure_locked(const struct tree_operation *op,const __u8 operation_digest[32]){- __u8 event[32], chain[32];+ __u8 event[32], chain[32] = {};struct json_emit e;- char line[4096], chain_hex[65];+ char line[4096];int ret;ret = chain_tree_event(op->seq, operation_digest, event);+ if (ret)+ return ret;+ /*+ * The record is written out before the extend, which nothing takes+ * back, though it ends in the value the extend gives: ended here+ * with a value of the same length, it ends with the real one the+ * same way. A record that could fail after the extend would leave+ * the register ahead of the log.+ */+ e = (struct json_emit){ .buf = line, .cap = sizeof(line) };+ tree_record_open(&e, op, event);+ ret = tree_record_close(e, chain);if (!ret)ret = chain_extend(event, chain);if (ret)return ret;-- hex_encode(chain, 32, chain_hex);- e = (struct json_emit){ .buf = line, .cap = sizeof(line) };- 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);- json_kv_string(&e, "id", op->id);- json_kv_string(&e, "operation_digest", op->operation_digest);- json_obj_close(&e);- json_kv_string(&e, "chain", chain_hex);- json_obj_close(&e);- if (e.err)- return e.err;- return eventlog_append(line);+ ret = tree_record_close(e, chain);+ return ret ? ret : eventlog_append(line);}static int tree_measure_operation(const struct tree_operation *op,diff --git a/builtin-keys.c b/builtin-keys.cindex e2c9b4ee..ea9767ff 100644--- a/builtin-keys.c+++ b/builtin-keys.c@@ -744,24 +744,24 @@ static const struct help keys_revoke_help = {"in a signed config image and activate it with image update.",};-static int append_key_event(const char *type, const char *fp_hex,- const char *sha_hex, const __u8 event[32])+static int format_key_event(const char *type, const char *fp_hex,+ const __u8 cert_sha[32], const __u8 event[32],+ char *buf, size_t size){- char buf[1024];- struct json_emit e = { .buf = buf, .cap = sizeof(buf) };+ struct json_emit e = { .buf = buf, .cap = size };+ char sha_hex[65];+ hex_encode(cert_sha, 32, sha_hex);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);json_obj_close(&e);json_obj_close(&e);- if (e.err) {+ if (e.err)pr_err("eventlog: %s entry encode failed: %s", type,strerror(-e.err));- return e.err;- }- return eventlog_append(buf);+ return e.err;}/*@@ -773,8 +773,8 @@ static int append_key_event(const char *type, const char *fp_hex,static int measure_revoke_locked(const struct trusted_cert *c,const char *fp_hex){+ char record[1024], chain_hex[65];__u8 event[32], chain[32];- char sha_hex[65], chain_hex[65];int ret;ret = chain_key_event("shirudo-key-revoke", c->sha256, event);@@ -783,6 +783,17 @@ static int measure_revoke_locked(const struct trusted_cert *c,strerror(-ret));return ret;}+ /*+ * The record is written out before the extend, which nothing takes+ * back: one that cannot be encoded after it would leave the register+ * ahead of the log.+ */+ ret = format_key_event("key_revoke", fp_hex, c->sha256, event, record,+ sizeof(record));+ if (ret) {+ pr_err("keys revoke: cannot write the record of the revocation; the key is revoked but the revocation is NOT measured; operator must reconcile");+ return ret;+ }ret = chain_extend(event, chain);if (ret) {pr_err("keys revoke: register %s (category: %s); the key is "@@ -794,8 +805,7 @@ static int measure_revoke_locked(const struct trusted_cert *c,ret == -EACCES ? "tampered" : "tpm_error");return ret;}- hex_encode(c->sha256, 32, sha_hex);- ret = append_key_event("key_revoke", fp_hex, sha_hex, event);+ ret = eventlog_append(record);if (ret) {pr_err("keys revoke: eventlog append failed after the extend: ""register and log disagree; the operator must reconcile");diff --git a/builtin-upatch.c b/builtin-upatch.cindex 7ca771bb..1808e829 100644--- a/builtin-upatch.c+++ b/builtin-upatch.c@@ -2856,18 +2856,6 @@ static int format_upatch_event(const char *type, const char *name,return 0;}-static int append_upatch_event(const char *type, const char *name,- const __u8 payload[32],- const __u8 event[32], const char *reason)-{- char buf[4096];- int ret;-- ret = format_upatch_event(type, name, payload, event, reason,- buf, sizeof(buf));- return ret ? ret : eventlog_append(buf);-}-/** Each operator operation is its own register event, where boot restore* coalesces into one. A missing TPM or register is an error, never an@@ -2881,8 +2869,8 @@ static int upatch_chain_event_locked(__u32 payload_type, bool load,bool *measured){const char *type = eventlog_upatch_record(payload_type, load);+ char record[4096], chain_hex[65];__u8 event[32], chain[32];- char chain_hex[65];int ret;if (out_chain)@@ -2896,6 +2884,16 @@ static int upatch_chain_event_locked(__u32 payload_type, bool load,op);return ret;}+ /*+ * The record is written out before the extend, which nothing takes+ * back: one that cannot be encoded then stops the event unmeasured,+ * where after the extend it would leave the register ahead of the+ * log.+ */+ ret = format_upatch_event(type, name, payload, event, reason, record,+ sizeof(record));+ if (ret)+ return ret;ret = chain_extend(event, chain);if (ret == -ENODEV) {@@ -2916,7 +2914,7 @@ static int upatch_chain_event_locked(__u32 payload_type, bool load,}if (measured)*measured = true;- ret = append_upatch_event(type, name, payload, event, reason);+ ret = eventlog_append(record);if (ret) {pr_err("upatch %s: eventlog append failed after the extend: ""register and log disagree; the operator must reconcile",