eBPF-based security platform
eventlog: hold a register lock from the extend to the append
Daniel Borkmannfd44d23998c2
diff --git a/Documentation/threat-model.rst b/Documentation/threat-model.rstindex 636f1ede..4559371e 100644--- a/Documentation/threat-model.rst+++ b/Documentation/threat-model.rst@@ -258,7 +258,11 @@ T5 Silent state changehappens. Every state-changing command reads the register first andrefuses ``tampered`` when ``--expect-chain`` disagrees or the indexis not the one ``init`` recorded, which also serialises concurrent- operators. ``shirudo status`` replays this boot's records and flags+ operators. A measuring command holds a lock beside the eventlog from+ its extend to its append, so the register and the log advance in one+ order even where the lock on whole commands is not taken: ``init``+ adopts the initrd's record without it, and two adopters both found+ the record missing and appended it twice. ``shirudo status`` replays this boot's records and flagsa register it cannot explain. There is no unmeasured mode: a targetwithout a TPM 2.0 does not initialise. The TPM behind the devicepath is root's to replace in a mount namespace of its own, and andiff --git a/builtin-bpm.c b/builtin-bpm.cindex 9e3afd38..12b91e68 100644--- a/builtin-bpm.c+++ b/builtin-bpm.c@@ -1856,8 +1856,8 @@ static int tree_read_pending(struct bpm_tree_pending *pending)* tree's own projection stays a file chain; the register is what an* operator attests.*/-static int tree_measure_operation(const struct shirudo_tree_operation *op,- const __u8 operation_digest[32])+static int tree_measure_locked(const struct shirudo_tree_operation *op,+ const __u8 operation_digest[32]){__u8 event[32], chain[32];struct shirudo_json_emit e;@@ -1899,6 +1899,19 @@ static int tree_measure_operation(const struct shirudo_tree_operation *op,return eventlog_append(line);}+static int tree_measure_operation(const struct shirudo_tree_operation *op,+ const __u8 operation_digest[32])+{+ int lock, rc;++ lock = eventlog_lock();+ if (lock < 0)+ return lock;+ rc = tree_measure_locked(op, operation_digest);+ eventlog_unlock(lock);+ return rc;+}+/** Complete the only non-idempotent window: the process may have* appended the event before the sealed cursor was committed.diff --git a/builtin-keys.c b/builtin-keys.cindex e54a0141..f6205671 100644--- a/builtin-keys.c+++ b/builtin-keys.c@@ -716,8 +716,8 @@ static int append_key_event(const char *type, const char *fp_hex,* fail-closed treatment as a upatch event: no TPM is an error, not an* unmeasured success.*/-static int measure_revoke(const struct shirudo_trusted_cert *c,- const char *fp_hex)+static int measure_revoke_locked(const struct shirudo_trusted_cert *c,+ const char *fp_hex){__u8 event[32], chain[32];char sha_hex[65], chain_hex[65];@@ -748,6 +748,22 @@ static int measure_revoke(const struct shirudo_trusted_cert *c,return 0;}+static int measure_revoke(const struct shirudo_trusted_cert *c,+ const char *fp_hex)+{+ int lock, rc;++ lock = eventlog_lock();+ if (lock < 0) {+ pr_err("keys revoke: cannot take the register lock: %s; the "+ "revocation is NOT measured", strerror(-lock));+ return lock;+ }+ rc = measure_revoke_locked(c, fp_hex);+ eventlog_unlock(lock);+ return rc;+}+/** Every archived upatch the revoked certificate signed goes: the kernel* would refuse to restore it at the next boot, and keeping it loadeddiff --git a/builtin-upatch.c b/builtin-upatch.cindex 0c480361..4f50953f 100644--- a/builtin-upatch.c+++ b/builtin-upatch.c@@ -2222,10 +2222,11 @@ static int append_upatch_event(const char *type, const char *name,* unmeasured success: init guarantees the register exists. *measured* tells a failure after the register moved from one before it.*/-static int upatch_chain_event(const char *tag, const char *type,- const char *op, const char *name,- const __u8 payload[32], const char *reason,- char out_chain[65], bool *measured)+static int upatch_chain_event_locked(const char *tag, const char *type,+ const char *op, const char *name,+ const __u8 payload[32],+ const char *reason, char out_chain[65],+ bool *measured){__u8 event[32], chain[32];char chain_hex[65];@@ -2277,6 +2278,30 @@ static int upatch_chain_event(const char *tag, const char *type,return 0;}+static int upatch_chain_event(const char *tag, const char *type,+ const char *op, const char *name,+ const __u8 payload[32], const char *reason,+ char out_chain[65], bool *measured)+{+ int lock, rc;++ if (out_chain)+ out_chain[0] = '\0';+ if (measured)+ *measured = false;+ lock = eventlog_lock();+ if (lock < 0) {+ pr_err("upatch %s: cannot take the register lock: %s "+ "(category: tpm_error) -- nothing measured", op,+ strerror(-lock));+ return lock;+ }+ rc = upatch_chain_event_locked(tag, type, op, name, payload, reason,+ out_chain, measured);+ eventlog_unlock(lock);+ return rc;+}+/** The name is not covered by the signature and becomes a path, so only* one safe component is allowed.diff --git a/testing/tpm/t0035-eventlog-pending.sh b/testing/tpm/t0035-eventlog-pending.shindex db05134e..031a691f 100755--- a/testing/tpm/t0035-eventlog-pending.sh+++ b/testing/tpm/t0035-eventlog-pending.sh@@ -50,4 +50,22 @@ test_expect_success 'a pending record follows the log'"'"'s records of this boot./$H boot-init | grep -qx "records=2"'+# init adopts while a command may append under the register lock: both+# adopters found the record missing and appended it twice.+test_expect_success 'adopters wait for the register lock and append the record once' '+ command -v flock >/dev/null 2>&1 || exit 0+ rm -f "$SHIRUDO_EVENTLOG_PATH" &&+ cp "$W/saved" "$SHIRUDO_EVENTLOG_PENDING_PATH" &&+ touch "$SHIRUDO_EVENTLOG_PATH.lock" &&+ { flock "$SHIRUDO_EVENTLOG_PATH.lock" sleep 2 & holder=$!; } &&+ sleep 0.5 &&+ { ./$H adopt >"$W/a1" & a1=$!; } &&+ { ./$H adopt >"$W/a2" & a2=$!; } &&+ sleep 0.5 &&+ [ ! -e "$SHIRUDO_EVENTLOG_PATH" ] &&+ wait "$holder" && wait "$a1" && wait "$a2" &&+ [ "$(wc -l < "$SHIRUDO_EVENTLOG_PATH")" = 1 ] &&+ [ ! -e "$SHIRUDO_EVENTLOG_PENDING_PATH" ]+'+test_donediff --git a/util/eventlog.c b/util/eventlog.cindex 1cd2029a..6904d9a5 100644--- a/util/eventlog.c+++ b/util/eventlog.c@@ -8,6 +8,7 @@#include <errno.h>#include <fcntl.h>+#include <limits.h>#include <stdint.h>#include <stdlib.h>#include <string.h>@@ -17,6 +18,7 @@#include <unistd.h>#include <shirudo/misc.h>+#include <shirudo/string.h>#include "chain.h"#include "eventlog.h"@@ -298,9 +300,57 @@ static int adopt_pending(void)return 0;}+/*+ * The register and the eventlog advance together only while one process+ * at a time extends and appends: two measuring commands under locks of+ * their own interleaved extend, extend, append, append and left a log+ * that replays the events in the other order. Every measuring path holds+ * this from its extend to its append; it lives beside the log it guards.+ */+int eventlog_lock(void)+{+ char path[PATH_MAX];+ int fd, rc;++ if (strsprintf(path, sizeof(path), "%s.lock", eventlog_path()) < 0)+ return -ENAMETOOLONG;+ fd = seal_open(path, O_RDWR | O_CREAT, 0600);+ if (fd < 0)+ return fd;+ while (flock(fd, LOCK_EX)) {+ if (errno != EINTR) {+ rc = -errno;+ close(fd);+ return rc;+ }+ }+ return fd;+}++void eventlog_unlock(int fd)+{+ if (fd >= 0)+ close(fd);+}++/*+ * init adopts the initrd's record while a command may append, and both+ * would find it missing and append it. Without a record there is+ * nothing to adopt, nor yet a state directory for the lock on a first+ * boot.+ */int eventlog_adopt_pending(void){- return adopt_pending();+ int lock, rc;++ if (access(eventlog_pending_path(), F_OK) && errno == ENOENT)+ return 0;+ lock = eventlog_lock();+ if (lock < 0)+ return lock;+ rc = adopt_pending();+ eventlog_unlock(lock);+ return rc;}static int replay_line(const char *line, size_t len, bool is_init, void *ctx)diff --git a/util/eventlog.h b/util/eventlog.hindex 043936cb..787c5043 100644--- a/util/eventlog.h+++ b/util/eventlog.h@@ -38,6 +38,10 @@ int eventlog_append_to(const char *path, const char *json_line);/* 0 when nothing was pending. */int eventlog_adopt_pending(void);+/* Held from a register extend to its append; -errno when not taken. */+int eventlog_lock(void);+void eventlog_unlock(int fd);+/** Replay this boot's records, from its last "init" record on, the way* the register accumulates them. -ENOENT when this boot has no init