eBPF-based security platform
keys: check every archive before the first revocation
John Fastabend1ed6f43ce809
diff --git a/Documentation/operate/maintain/keys.rst b/Documentation/operate/maintain/keys.rstindex 2303225b..39317581 100644--- a/Documentation/operate/maintain/keys.rst+++ b/Documentation/operate/maintain/keys.rst@@ -453,6 +453,11 @@ Limitsconfig makes init refuse that config.- Revoking a BPF signer purges upatches it signed. Plan their replacementbefore retiring the signer.+- A config with revocation records is not activated while+ ``/var/lib/shirudo/upatches`` holds an archive no signer can be read+ from. ``shirudo image update`` then prints ``keys revoke:+ <name>.upatch names no signer: <error>; nothing revoked`` and changes+ nothing: no key is unlinked and nothing is measured.- Vouches are historical authorizations. Revoking A does notautomatically revoke B merely because A vouched for it. After acompromise, the config signer must explicitly revoke any certificatediff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rstindex f6f8cb43..ab750534 100644--- a/Documentation/security/threat-model.rst+++ b/Documentation/security/threat-model.rst@@ -1834,8 +1834,14 @@ R13 Revocation reaches a target only through a configmeasures a ``key_revoke`` event and then purges the key's upatches,each with an unload event. If that extension fails, the key staysunlinked, the revocation is unmeasured and the purge does not run.- The archive scan checks each signer under the register lock. A read or- signature-format error fails the purge so activation can retry it.+ The archive scan checks each signer under the register lock. An+ archive that does not parse, is filed under another name than it+ signs or carries no signer cannot be purged by key, so an activation+ with revocation records checks every archive first, before it extends+ the config PCR and again before it unlinks the first key, and refuses+ without changing anything while one is there. An error that the check+ could not foresee still fails the purge part-way, and the activation+ stays pending.BPF loads require a complete CMS envelope, and the purge reads anarchived signature by the same rule.At boot the revoked key is not enrolled, and the init event'sdiff --git a/builtin-keys.c b/builtin-keys.cindex 50f31fb1..95c9cd5f 100644--- a/builtin-keys.c+++ b/builtin-keys.c@@ -833,6 +833,54 @@ static int measure_revoke(const struct trusted_cert *c,return ret;}+/* The upatch name an archive is filed under; scandir selected the suffix. */+static int archive_upatch_name(const char *file, char name[UPATCH_NAME_MAX + 1])+{+ size_t len = strlen(file) - strlen(".upatch");++ if (span_to_str(name, UPATCH_NAME_MAX + 1, file, len) ||+ !upatch_name_valid(name))+ return -EINVAL;+ return 0;+}++/*+ * A revocation purges what its key signed, and cannot say what that is+ * while one archive names no signer. Unlinking a key and measuring it+ * are not undone, so every archive is checked before the first of them.+ */+int keys_revoke_check(void)+{+ struct dirent **names = NULL;+ int n, i, ret = 0;++ n = scandir(UPATCHES_DIR, &names, upatch_select, alphasort);+ if (n < 0)+ return errno == ENOENT ? 0 : -errno;+ for (i = 0; i < n; i++) {+ char name[UPATCH_NAME_MAX + 1];+ int err;++ err = archive_upatch_name(names[i]->d_name, name);+ if (err) {+ pr_err("keys revoke: invalid archive filename");+ } else {+ /* An unload since the scan left nothing to attribute. */+ err = upatch_purge_check(name);+ if (err == UPATCH_PURGE_NO_ARCHIVE)+ err = 0;+ if (err)+ pr_err("keys revoke: %s.upatch names no signer: %s; nothing revoked",+ name, strerror(-err));+ }+ if (err && !ret)+ ret = err;+ libc_free(names[i]);+ }+ libc_free(names);+ return ret;+}+/** Every archived upatch the revoked certificate signed goes: the kernel* would refuse to restore it at the next boot, and keeping it loaded@@ -858,22 +906,21 @@ static int purge_signed_by(const __u8 *der, size_t der_sz,}for (i = 0; i < n; i++) {char name[UPATCH_NAME_MAX + 1];- const char *file = names[i]->d_name;- size_t len = strlen(file) - strlen(".upatch");int prc;- /*- * scandir selected the suffix. Attribute the open archive under- * the lock, even if its previous contents were replaced.- */- prc = span_to_str(name, sizeof(name), file, len);- if (prc || !upatch_name_valid(name)) {+ if (archive_upatch_name(names[i]->d_name, name)) {pr_err("keys revoke: invalid archive filename");ret = -EINVAL;continue;}+ /*+ * Attribute the open archive under the lock, even if its+ * previous contents were replaced. One an unload took since+ * the scan has nothing left to purge.+ */prc = upatch_purge(name, &cert, "key_revoke");- if (prc == UPATCH_PURGE_OTHER_SIGNER)+ if (prc == UPATCH_PURGE_OTHER_SIGNER ||+ prc == UPATCH_PURGE_NO_ARCHIVE)continue;if (prc < 0) {pr_err("keys revoke: purge %s: %s", name,diff --git a/builtin-upatch.c b/builtin-upatch.cindex ed45ba73..88de1265 100644--- a/builtin-upatch.c+++ b/builtin-upatch.c@@ -4048,31 +4048,73 @@ static int purge_is_loaded(const char *name, __u32 type)return ret ? ret : 1;}+/*+ * The archive of name, open, once it is one a signer can be read from:+ * it parses and its signed name is the one it is filed under.+ * UPATCH_PURGE_NO_ARCHIVE when the name has none.+ */+static int purge_archive_open(const char *persisted, const char *name,+ struct upatch *u)+{+ char archive_name[UPATCH_NAME_MAX + 1];+ int fd, ret;++ fd = seal_open_read(persisted);+ if (fd == -ENOENT)+ return UPATCH_PURGE_NO_ARCHIVE;+ if (fd < 0)+ return fd;+ ret = upatch_open_fd(fd, u);+ close(fd);+ if (ret)+ return ret;+ upatch_header_name(u, archive_name);+ if (!upatch_name_signed(u) || strcmp(name, archive_name)) {+ upatch_close(u);+ return -EBADMSG;+ }+ return 0;+}++int upatch_purge_check(const char *name)+{+ char persisted[PATH_MAX];+ struct upatch u;+ int lock, ret;++ if (!upatch_name_valid(name))+ return -EINVAL;+ if (strsprintf(persisted, sizeof(persisted), "%s/%s.upatch",+ UPATCHES_DIR, name) < 0)+ return -ENAMETOOLONG;+ lock = eventlog_lock();+ if (lock < 0)+ return lock;+ ret = purge_archive_open(persisted, name, &u);+ if (!ret) {+ ret = cms_check_signers(upatch_signature(&u),+ u.hdr.signature_len);+ upatch_close(&u);+ }+ eventlog_unlock(lock);+ return ret;+}+static int upatch_purge_locked(const char *name, const struct cert *signer,const char *reason){__u8 payload[UPATCH_PAYLOAD_HASH_LEN] = {};- char archive_name[UPATCH_NAME_MAX + 1], persisted[PATH_MAX];- int fd, loaded, ret;+ char persisted[PATH_MAX];+ int loaded, ret;struct upatch u;__u32 type;if (strsprintf(persisted, sizeof(persisted), "%s/%s.upatch",UPATCHES_DIR, name) < 0)return -ENAMETOOLONG;- ret = seal_open_read(persisted);- if (ret < 0)- return ret;- fd = ret;- ret = upatch_open_fd(fd, &u);- close(fd);+ ret = purge_archive_open(persisted, name, &u);if (ret)return ret;- upatch_header_name(&u, archive_name);- if (!upatch_name_signed(&u) || strcmp(name, archive_name)) {- upatch_close(&u);- return -EBADMSG;- }ret = cms_signed_by(upatch_signature(&u), u.hdr.signature_len, signer);memcpy(payload, u.hdr.payload_sha256, sizeof(payload));type = u.hdr.type;diff --git a/testing/fuzz/fuzz-bpm-info.c b/testing/fuzz/fuzz-bpm-info.cindex 70f99cf6..a5f5aad6 100644--- a/testing/fuzz/fuzz-bpm-info.c+++ b/testing/fuzz/fuzz-bpm-info.c@@ -42,6 +42,11 @@ int keys_revoke_image(const __u8 *der, size_t size, bool boot)abort();}+int keys_revoke_check(void)+{+ abort();+}+bool build_available(void){abort();diff --git a/testing/helper/test-image-keys.c b/testing/helper/test-image-keys.cindex 84e37ede..883107f0 100644--- a/testing/helper/test-image-keys.c+++ b/testing/helper/test-image-keys.c@@ -62,6 +62,11 @@ int keys_revoke_image(const __u8 *der, size_t size, bool boot)return -EPERM;}+int keys_revoke_check(void)+{+ return 0;+}+static int boot_count(void *opaque, __u32 *reset, __u32 *restart){*reset = 1;diff --git a/util/image_keys.c b/util/image_keys.cindex f25c329c..34eb7496 100644--- a/util/image_keys.c+++ b/util/image_keys.c@@ -22,7 +22,7 @@#include "malloc.h"#include "paths.h"-static int revocations(int root_fd, bool apply, bool boot)+static int revocations(int root_fd, bool apply, bool boot, size_t *records){struct dirent *entry;struct stat root, st;@@ -94,6 +94,8 @@ static int revocations(int root_fd, bool apply, bool boot)ret = -EKEYREJECTED;if (!ret && apply)ret = keys_revoke_image(der, size, boot);+ if (!ret)+ (*records)++;xfree(der, size);if (ret)break;@@ -108,6 +110,7 @@ static int revocations(int root_fd, bool apply, bool boot)int keys_validate_image_set(const char *primary, int root_fd,struct trusted_set *set){+ size_t records = 0;int ret;ret = image_config_validate(root_fd);@@ -116,7 +119,7 @@ int keys_validate_image_set(const char *primary, int root_fd,strerror(-ret));return ret;}- ret = revocations(root_fd, false, false);+ ret = revocations(root_fd, false, false, &records);if (ret) {pr_err("image trust set: validate revocations: %s",strerror(-ret));@@ -169,10 +172,24 @@ int keys_validate_image(int root_fd)return ret ? ret : keys_check_image_set(&set);}+int keys_check_image_purge(int root_fd)+{+ size_t records = 0;+ int ret;++ ret = revocations(root_fd, false, false, &records);+ if (ret || !records)+ return ret;+ return keys_revoke_check();+}+int keys_apply_image(int root_fd, bool boot){+ size_t records = 0;int ret;ret = keys_validate_image(root_fd);- return ret ? ret : revocations(root_fd, true, boot);+ if (!ret)+ ret = keys_check_image_purge(root_fd);+ return ret ? ret : revocations(root_fd, true, boot, &records);}diff --git a/util/image_keys.h b/util/image_keys.hindex 47cba67a..e920e637 100644--- a/util/image_keys.h+++ b/util/image_keys.h@@ -16,7 +16,13 @@ int keys_validate_image_set(const char *primary, int root_fd,int keys_check_image_set(const struct trusted_set *set);int keys_validate_image(int root_fd);+/*+ * For an image with revocation records, keys_revoke_check(): whether+ * applying them could purge every archive. 0 without a record.+ */+int keys_check_image_purge(int root_fd);int keys_apply_image(int root_fd, bool boot);int keys_revoke_image(const __u8 *der, size_t size, bool boot);+int keys_revoke_check(void);#endif /* SHIRUDO_IMAGE_KEYS_H */diff --git a/util/image_runtime.c b/util/image_runtime.cindex 3402dde2..31319890 100644--- a/util/image_runtime.c+++ b/util/image_runtime.c@@ -621,6 +621,9 @@ static noinline_for_stack int candidate_keys(int fd, bool enrolled)ret = keys_validate_image_set(RUNTIME_CERT_PATH, fd, &set);if (!ret && enrolled)ret = keys_check_image_set(&set);+ /* Before the activation PCR is extended, which nothing takes back. */+ if (!ret && enrolled)+ ret = keys_check_image_purge(fd);if (ret == -ENOKEY)pr_err("image: BPF signer was not enrolled at boot; ""reboot with the new trusted key set required");diff --git a/util/upatch.h b/util/upatch.hindex c9224ac2..360a48af 100644--- a/util/upatch.h+++ b/util/upatch.h@@ -244,10 +244,21 @@ 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, UPATCH_PURGE_OTHER_SIGNER when the archive- * stays because another key signed it, -errno on failure.+ * stays because another key signed it, UPATCH_PURGE_NO_ARCHIVE when the+ * name has none, -errno on failure. An -ENOENT is a failure like any+ * other: the register lock or a pin was not there.*/#define UPATCH_PURGE_OTHER_SIGNER 2+#define UPATCH_PURGE_NO_ARCHIVE 3int upatch_purge(const char *name, const struct cert *signer,const char *reason);+/*+ * Whether upatch_purge() could tell who signed the archive of name: 0,+ * UPATCH_PURGE_NO_ARCHIVE without an archive, -errno for one that does+ * not parse, is filed under another name than it signs or has no+ * signer, and when the register lock cannot be taken.+ */+int upatch_purge_check(const char *name);+#endif /* SHIRUDO_UTIL_UPATCH_H */