Skip to content

eBPF-based security platform

keys: check the signer of a purged upatch under the register lock

Daniel Borkmannb662dee38c6b
diff --git a/Documentation/internals/config-images.rst b/Documentation/internals/config-images.rst
index 052b177c..759370dd 100644
--- a/Documentation/internals/config-images.rst
+++ b/Documentation/internals/config-images.rst
@@ -210,7 +210,9 @@ It then activates it:
#. Apply the revocations: unlink each revoked certificate that is in the
``.bpf`` keyring, extend the register with a ``key_revoke`` event,
and purge the archived upatches the certificate signed, unloading
- those that are loaded.
+ those that are loaded. Each purge checks the signer again under the
+ register lock, so an upatch an upgrade replaced in the meantime with
+ one another key signed stays.
#. Mount the candidate at ``/run/shirudo/config``.
#. Record the selection as complete.
diff --git a/Documentation/internals/measurement-register.rst b/Documentation/internals/measurement-register.rst
index c6e1d29d..be04c920 100644
--- a/Documentation/internals/measurement-register.rst
+++ b/Documentation/internals/measurement-register.rst
@@ -262,7 +262,9 @@ 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,
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.
+between the purge's event and its removal. The purge checks the signer
+on what that read finds: an upgrade that replaced the upatch since the
+revocation's scan with one another key signed leaves it in place.
The order of extension and state change is chosen per operation. A load
loads, archives and then extends; a failed archive copy or a refused
diff --git a/builtin-keys.c b/builtin-keys.c
index 70e078fd..e4f89a08 100644
--- a/builtin-keys.c
+++ b/builtin-keys.c
@@ -879,7 +879,12 @@ static int purge_signed_by(const __u8 *der, size_t der_sz,
upatch_close(&u);
if (signed_by != 1)
continue;
- prc = upatch_purge(name, "key_revoke");
+ prc = upatch_purge(name, &cert, "key_revoke");
+ if (prc == -ESTALE) {
+ pr_info("keys revoke: %s was replaced by an upatch the key did not sign; left alone",
+ name);
+ continue;
+ }
if (prc < 0) {
pr_err("keys revoke: purge %s: %s", name,
strerror(-prc));
diff --git a/builtin-upatch.c b/builtin-upatch.c
index aad6391d..1542cd0f 100644
--- a/builtin-upatch.c
+++ b/builtin-upatch.c
@@ -3147,7 +3147,8 @@ done:
return ret;
}
-static int upatch_purge_locked(const char *name, const char *reason)
+static int upatch_purge_locked(const char *name, const struct cert *signer,
+ const char *reason)
{
__u8 payload[UPATCH_PAYLOAD_HASH_LEN] = {};
char persisted[PATH_MAX];
@@ -3166,8 +3167,11 @@ static int upatch_purge_locked(const char *name, const char *reason)
close(fd);
if (ret)
return ret;
+ ret = cms_signed_by(upatch_signature(&u), u.hdr.signature_len, signer);
memcpy(payload, u.hdr.payload_sha256, sizeof(payload));
upatch_close(&u);
+ if (ret != 1)
+ return ret < 0 ? ret : -ESTALE;
/* As for unload: the event first, then the detach it describes. */
loaded = upatch_is_pinned(name);
@@ -3197,9 +3201,13 @@ static int upatch_purge_locked(const char *name, const char *reason)
/*
* 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.
+ * lock after the purge's event is not the one the purge then removes,
+ * and with the signer checked on what that read finds: a caller's scan
+ * ran before the lock, and an upgrade may have replaced the upatch since
+ * with one another key signed.
*/
-int upatch_purge(const char *name, const char *reason)
+int upatch_purge(const char *name, const struct cert *signer,
+ const char *reason)
{
int lock, ret;
@@ -3212,7 +3220,7 @@ int upatch_purge(const char *name, const char *reason)
strerror(-lock));
return lock;
}
- ret = upatch_purge_locked(name, reason);
+ ret = upatch_purge_locked(name, signer, reason);
eventlog_unlock(lock);
return ret;
}
diff --git a/util/upatch.h b/util/upatch.h
index 60e74cf5..3f25b585 100644
--- a/util/upatch.h
+++ b/util/upatch.h
@@ -184,11 +184,15 @@ int upatch_remove(const char *path);
*/
#define UPATCH_INSTALL_TARGET_MISSING 2
+struct cert;
+
/*
* Detach, unpin and unarchive without a directive, because the key
* that signed the upatch was revoked: 1 when the unload was measured,
- * 0 when only the archive went, -errno otherwise.
+ * 0 when only the archive went, -ESTALE when the archive under the name
+ * is no longer one signer signed and stays, -errno otherwise.
*/
-int upatch_purge(const char *name, const char *reason);
+int upatch_purge(const char *name, const struct cert *signer,
+ const char *reason);
#endif /* SHIRUDO_UTIL_UPATCH_H */