eBPF-based security platform
upatch: hold the register lock across a purge
Daniel Borkmannf32fa04cc705
diff --git a/AGENTS.md b/AGENTS.mdindex cb66b15e..21dd9943 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -185,7 +185,8 @@ one usually still compiles and passes tests.between its pins and its extend leaves pins without a record: `status`reports them (`UpatchesMeasured`) and the next upatch command undoesthem under the register lock, which a load or an unload holds from- before it touches the pins to its append. `init` restores only an+ before it touches the pins to its append, and a purge from its archive+ read until its pins and archive are gone. `init` restores only anarchive whose load the eventlog records without a later unload; alabeled archive root brings back is reported and removed, neverrestored. The register followsdiff --git a/Documentation/internals/measurement-register.rst b/Documentation/internals/measurement-register.rstindex b51d3b5e..05ba766a 100644--- a/Documentation/internals/measurement-register.rst+++ b/Documentation/internals/measurement-register.rst@@ -259,7 +259,10 @@ of their own could interleave extend, extend, append, append, and leave alog that replays the events in the other order. Every measuring command,an upatch load or unload, a live revocation and a tree operation,therefore holds ``/var/lib/shirudo/eventlog.lock`` from its extension to-its append; a load or an unload holds it from before it touches the pins.+its append; a load or an unload holds it from before it touches the pins,+and a revocation's purge of an upatch from its archive read until its+pins and archive are gone, so that a load of the same name cannot come+between the purge's event and its removal.The order of extension and state change is chosen per operation. A loadloads, archives and then extends; a failed archive copy or a refuseddiff --git a/builtin-upatch.c b/builtin-upatch.cindex 45a9a5d8..1298c140 100644--- a/builtin-upatch.c+++ b/builtin-upatch.c@@ -2402,30 +2402,6 @@ static const char *chain_event_category(int ret, bool measured)return !measured && ret == -EACCES ? "tampered" : "tpm_error";}-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, ret;-- 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;- }- ret = upatch_chain_event_locked(tag, type, op, name, payload, reason,- out_chain, measured);- eventlog_unlock(lock);- return ret;-}-static int append_json_record(const char *path, const char *json, size_t len){struct stat st;@@ -3134,7 +3110,7 @@ done:return ret;}-int upatch_purge(const char *name, const char *reason)+static int upatch_purge_locked(const char *name, const char *reason){__u8 payload[UPATCH_PAYLOAD_HASH_LEN] = {};char persisted[PATH_MAX];@@ -3142,8 +3118,6 @@ int upatch_purge(const char *name, const char *reason)bool loaded;int fd, ret;- if (!upatch_name_valid(name))- return -EINVAL;if (strsprintf(persisted, sizeof(persisted), "%s/%s.upatch",UPATCHES_DIR, name) < 0)return -ENAMETOOLONG;@@ -3161,9 +3135,9 @@ int upatch_purge(const char *name, const char *reason)/* As for unload: the event first, then the detach it describes. */loaded = upatch_is_pinned(name);if (loaded) {- ret = upatch_chain_event("shirudo-unload", "upatch_unload",- "purge", name, payload, reason, NULL,- NULL);+ ret = upatch_chain_event_locked("shirudo-unload",+ "upatch_unload", "purge", name,+ payload, reason, NULL, NULL);if (ret)return ret;ret = remove_pin_dir(name);@@ -3183,6 +3157,29 @@ int upatch_purge(const char *name, const char *reason)return loaded ? 1 : 0;}+/*+ * Under the register lock from the archive read to its unlink, as a+ * load and an unload hold it, so that a load of the name that takes the+ * lock after the purge's event is not the one the purge then removes.+ */+int upatch_purge(const char *name, const char *reason)+{+ int lock, ret;++ if (!upatch_name_valid(name))+ return -EINVAL;+ lock = eventlog_lock();+ if (lock < 0) {+ pr_err("upatch purge: cannot take the register lock: %s "+ "(category: tpm_error); nothing measured or detached",+ strerror(-lock));+ return lock;+ }+ ret = upatch_purge_locked(name, reason);+ eventlog_unlock(lock);+ return ret;+}+/* Package installs take the same path as the upatch commands. */int upatch_install(const char *path){