eBPF-based security platform
keys: require signer attribution before completing revocation
KP Singhed72be982145
diff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rstindex ae01f771..110a343f 100644--- a/Documentation/security/threat-model.rst+++ b/Documentation/security/threat-model.rst@@ -1809,6 +1809,10 @@ 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.+ New BPF loads require a complete CMS envelope. Revocation also reads+ older archived signatures with zero padding.At boot the revoked key is not enrolled, and the init event'scertificate list shows it. A target that has not activated theconfig still trusts the key. Historical vouches are U13.diff --git a/builtin-keys.c b/builtin-keys.cindex e3903894..50f31fb1 100644--- a/builtin-keys.c+++ b/builtin-keys.c@@ -852,32 +852,29 @@ static int purge_signed_by(const __u8 *der, size_t der_sz,n = scandir(UPATCHES_DIR, &names, upatch_select,alphasort);if (n < 0) {+ ret = errno == ENOENT ? 0 : -errno;cert_free(&cert);- return errno == ENOENT ? 0 : -errno;+ return ret;}for (i = 0; i < n; i++) {char name[UPATCH_NAME_MAX + 1];- struct upatch u;- char path[PATH_MAX];- int signed_by, prc;-- if (strsprintf(path, sizeof(path), "%s/%s", UPATCHES_DIR,- names[i]->d_name) < 0)- continue;- if (upatch_open(path, &u))- continue;- signed_by = cms_signed_by(upatch_signature(&u),- u.hdr.signature_len, &cert);- upatch_header_name(&u, name);- upatch_close(&u);- if (signed_by != 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)) {+ pr_err("keys revoke: invalid archive filename");+ ret = -EINVAL;continue;+ }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);+ if (prc == UPATCH_PURGE_OTHER_SIGNER)continue;- }if (prc < 0) {pr_err("keys revoke: purge %s: %s", name,strerror(-prc));diff --git a/builtin-upatch.c b/builtin-upatch.cindex 0c0266c5..85a45d41 100644--- a/builtin-upatch.c+++ b/builtin-upatch.c@@ -2564,6 +2564,12 @@ static int loader_prog_load(struct signed_load *sl)insns = upatch_loader(u);sig = upatch_signature(u);+ if (cms_check_signers(sig, u->hdr.signature_len)) {+ pr_err("upatch: unreadable CMS signer information");+ rep_fail(rep, "signed_loader_load", "parser_rejected", EBADMSG,+ "invalid CMS signature envelope");+ return 1;+ }insn_cnt = u->hdr.loader_insns_len / sizeof(struct bpf_insn);upatch_prog_name(u, prog_name, sizeof(prog_name));@@ -4074,7 +4080,7 @@ 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];+ char archive_name[UPATCH_NAME_MAX + 1], persisted[PATH_MAX];int fd, loaded, ret;struct upatch u;__u32 type;@@ -4090,12 +4096,18 @@ static int upatch_purge_locked(const char *name, const struct cert *signer,close(fd);if (ret)return ret;- ret = cms_signed_by(upatch_signature(&u), u.hdr.signature_len, signer);+ upatch_header_name(&u, archive_name);+ if (!upatch_name_signed(&u) || strcmp(name, archive_name)) {+ upatch_close(&u);+ return -EBADMSG;+ }+ ret = cms_archive_signed_by(upatch_signature(&u), u.hdr.signature_len,+ signer);memcpy(payload, u.hdr.payload_sha256, sizeof(payload));type = u.hdr.type;upatch_close(&u);if (ret != 1)- return ret < 0 ? ret : -ESTALE;+ return ret < 0 ? ret : UPATCH_PURGE_OTHER_SIGNER;/* As for unload: the event first, then the detach it describes. */loaded = purge_is_loaded(name, type);@@ -4130,9 +4142,8 @@ static int upatch_purge_locked(const char *name, const struct cert *signer,* 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,- * 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.+ * and with the signer checked on what that read finds. An upgrade may+ * have replaced the archive since the directory scan.*/int upatch_purge(const char *name, const struct cert *signer,const char *reason)diff --git a/testing/unit/crypto_openssl.c b/testing/unit/crypto_openssl.cindex 4172863c..830e7a05 100644--- a/testing/unit/crypto_openssl.c+++ b/testing/unit/crypto_openssl.c@@ -1,6 +1,8 @@// SPDX-License-Identifier: BSD-2-Clause/* Copyright (c) 2026 Authors of shirudo */+#include <errno.h>+#include <stdio.h>#include <string.h>#include "crypto.h"@@ -28,7 +30,41 @@ static void sha256_buffer_zeroes_a_failed_digest(void)EXPECT_MEMEQ(out, zero, sizeof(out));}+/* The existing corpus entry holds a certificate and its detached CMS. */+static void archived_signers_remain_identifiable(void)+{+ const char *path = "fuzz/corpus/cms/cert-and-signature";+ static __u8 data[32768];+ struct cert cert = {};+ size_t size, der_len, sig_len;+ __u8 *sig;+ FILE *f;++ f = fopen(path, "rb");+ if (!f)+ f = fopen("testing/fuzz/corpus/cms/cert-and-signature", "rb");+ ASSERT_TRUE(f);+ size = fread(data, 1, sizeof(data), f);+ fclose(f);+ ASSERT_TRUE(size > 2 && size < sizeof(data));+ der_len = data[0] | (size_t)data[1] << 8;+ ASSERT_TRUE(der_len < size - 2);+ ASSERT_EQ(cert_read_der(data + 2, der_len, &cert), 0);+ sig = data + 2 + der_len;+ sig_len = size - 2 - der_len;+ EXPECT_EQ(cms_check_signers(sig, sig_len), 0);+ EXPECT_EQ(cms_signed_by(sig, sig_len, &cert), 1);+ data[size] = 0;+ EXPECT_EQ(cms_check_signers(sig, sig_len + 1), -EBADMSG);+ EXPECT_EQ(cms_signed_by(sig, sig_len + 1, &cert), -EINVAL);+ EXPECT_EQ(cms_archive_signed_by(sig, sig_len + 1, &cert), 1);+ data[size] = 1;+ EXPECT_EQ(cms_archive_signed_by(sig, sig_len + 1, &cert), -EINVAL);+ cert_free(&cert);+}+static const struct unit_case cases[] = {+ UNIT_CASE(archived_signers_remain_identifiable),UNIT_CASE(sha256_matches_fips_180_vector),UNIT_CASE(sha256_buffer_zeroes_a_failed_digest),{}diff --git a/util/crypto.h b/util/crypto.hindex b4419856..3fe93c14 100644--- a/util/crypto.h+++ b/util/crypto.h@@ -67,14 +67,20 @@ int cert_not_before(const struct cert *cert, char out[11]) __must_check;int verify_detached(const __u8 *payload, size_t payload_len,const __u8 *signature, size_t signature_len,const struct cert *cert) __must_check;+/* A complete CMS envelope with a nonempty set of signers. */+int cms_check_signers(const __u8 *signature, size_t len) __must_check;/** Whether a signer of the CMS blob names this certificate, by key id* or by issuer and serial: 1 when one does, 0 when none, -EINVAL when- * the blob does not parse. Says nothing about the signature's validity.+ * the blob does not parse. Says nothing about signature validity.*/int cms_signed_by(const __u8 *signature, size_t signature_len,const struct cert *cert) __must_check;+/* Attribute zero-padded signatures admitted by older targets during purge. */+int cms_archive_signed_by(const __u8 *signature, size_t signature_len,+ const struct cert *cert) __must_check;+struct tls_ca {const char *path;void *impl;diff --git a/util/crypto_openssl.c b/util/crypto_openssl.cindex 3b44faab..c92f76d1 100644--- a/util/crypto_openssl.c+++ b/util/crypto_openssl.c@@ -353,7 +353,27 @@ int cert_not_before(const struct cert *cert, char out[11])return asn1_date(X509_get0_notBefore(cert->impl), out);}-static CMS_ContentInfo *read_cms(const __u8 *signature, size_t len)+static bool cms_has_signers(CMS_ContentInfo *cms)+{+ const struct stack_st_CMS_SignerInfo *signers = CMS_get0_SignerInfos(cms);+ int count = sk_CMS_SignerInfo_num(signers);+ int i;++ for (i = 0; i < count; i++) {+ CMS_SignerInfo *si = sk_CMS_SignerInfo_value(signers, i);+ ASN1_OCTET_STRING *key = NULL;+ ASN1_INTEGER *serial = NULL;+ X509_NAME *issuer = NULL;++ if (!CMS_SignerInfo_get0_signer_id(si, &key, &issuer, &serial) ||+ (key ? ASN1_STRING_length(key) <= 0 : !issuer || !serial))+ return false;+ }+ return count > 0;+}++static CMS_ContentInfo *read_cms(const __u8 *signature, size_t len,+ bool archive){const __u8 *next = signature;CMS_ContentInfo *cms;@@ -361,7 +381,13 @@ static CMS_ContentInfo *read_cms(const __u8 *signature, size_t len)if (!signature || !len || len > INT_MAX)return NULL;cms = d2i_CMS_ContentInfo(NULL, &next, (long)len);- if (cms && next != signature + len) {+ /*+ * Older targets admitted a zero-padded envelope. Only attribution+ * of an already archived signature accepts that representation.+ */+ while (cms && archive && next < signature + len && !*next)+ next++;+ if (cms && (next != signature + len || !cms_has_signers(cms))) {CMS_ContentInfo_free(cms);cms = NULL;}@@ -396,7 +422,7 @@ int verify_detached(const __u8 *payload, size_t payload_len,signers = sk_X509_new_null();if (!signers || !sk_X509_push(signers, cert->impl))goto done;- cms = read_cms(signature, signature_len);+ cms = read_cms(signature, signature_len, false);if (cms && CMS_verify(cms, signers, store, data, out,CMS_BINARY | CMS_DETACHED | CMS_NOINTERN) == 1)ret = 0;@@ -411,8 +437,20 @@ done:return ret;}-int cms_signed_by(const __u8 *signature, size_t signature_len,- const struct cert *cert)+int cms_check_signers(const __u8 *signature, size_t len)+{+ CMS_ContentInfo *cms = read_cms(signature, len, false);++ if (!cms) {+ ERR_clear_error();+ return -EBADMSG;+ }+ CMS_ContentInfo_free(cms);+ return 0;+}++static int signer_matches(const __u8 *signature, size_t signature_len,+ const struct cert *cert, bool archive){STACK_OF(CMS_SignerInfo) *sis;CMS_ContentInfo *cms = NULL;@@ -420,7 +458,7 @@ int cms_signed_by(const __u8 *signature, size_t signature_len,if (!signature || !cert || !cert->impl || signature_len > INT_MAX)return -EINVAL;- cms = read_cms(signature, signature_len);+ cms = read_cms(signature, signature_len, archive);if (!cms)goto done;sis = CMS_get0_SignerInfos(cms);@@ -439,6 +477,18 @@ done:return ret;}+int cms_signed_by(const __u8 *signature, size_t signature_len,+ const struct cert *cert)+{+ return signer_matches(signature, signature_len, cert, false);+}++int cms_archive_signed_by(const __u8 *signature, size_t signature_len,+ const struct cert *cert)+{+ return signer_matches(signature, signature_len, cert, true);+}+int hmac_sha256(const __u8 *key, size_t key_len,const void *data, size_t len, __u8 out[32]){diff --git a/util/upatch.h b/util/upatch.hindex 1d37e67a..e27f236d 100644--- a/util/upatch.h+++ b/util/upatch.h@@ -245,9 +245,10 @@ 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, -ESTALE when the archive under the name- * is no longer one signer signed and stays, -errno otherwise.+ * 0 when only the archive went, UPATCH_PURGE_OTHER_SIGNER when the archive+ * stays because another key signed it, -errno on failure.*/+#define UPATCH_PURGE_OTHER_SIGNER 2int upatch_purge(const char *name, const struct cert *signer,const char *reason);