eBPF-based security platform
tpm: check an attestation's signature and signer in one place
Daniel Borkmann54071bc92b57
diff --git a/Documentation/reference/cli/attest-verify.rst b/Documentation/reference/cli/attest-verify.rstindex 25dc74e9..39791ff1 100644--- a/Documentation/reference/cli/attest-verify.rst+++ b/Documentation/reference/cli/attest-verify.rst@@ -67,9 +67,9 @@ a note. A ``verdict: ok`` then says only that the document is consistentin itself. Pin the keys at enrollment (:doc:`attest-challenge`) and alwaysgive the expected state.-:doc:`quote-check` checks a keepalive quote with code of its own, not-with ``verify``'s, and checks fewer fields of the attestation key; its-page lists the differences.+:doc:`quote-check` checks a keepalive quote's signature and signer+with the same functions as ``verify``, and fewer fields of the+attestation key; its page lists the differences.Options-------diff --git a/Documentation/reference/cli/quote-check.rst b/Documentation/reference/cli/quote-check.rstindex 16635014..54cd6469 100644--- a/Documentation/reference/cli/quote-check.rst+++ b/Documentation/reference/cli/quote-check.rst@@ -123,14 +123,15 @@ to a TPM.Compared with attest verify----------------------------``shirudo attest verify`` checks the signature and the signer of the-quotes in an evidence document with code of its own,-:src:`util/attest.c`, where this program has-:src:`tools/quote-check/row.c`. Both parse the key with the same-``TPMT_PUBLIC`` parser, which refuses a curve other than NIST P-256 and-an ECDSA hash other than SHA-256, and both check the signer as the-key's qualified name under the endorsement hierarchy. They differ in-these:+``shirudo attest verify`` and this program check a signature and its+signer with the same two functions of :src:`util/tpm_wire.c`,+``tpm_attest_verify()`` and ``tpm_attest_signer()``: the ECDSA signature+over the SHA-256 of the ``attest`` bytes, where a key that is no point+of the curve verifies nothing, and the signer as the key's qualified+name under the endorsement hierarchy, compared in constant time. Both+parse the key with the same ``TPMT_PUBLIC`` parser, which refuses a+curve other than NIST P-256 and an ECDSA hash other than SHA-256. What+each asks of the key and how it reports differ:.. list-table:::header-rows: 1@@ -149,16 +150,10 @@ these:fixedParent and sensitiveDataOrigin- No- Yes- * - The signer's name is compared- - With ``memcmp()``- - In constant time, with ``memneq_explicit()``* - After a check fails- Stops: the first failure decides the reason- Goes on and reports every check, unless a structure does notparse- * - A key that is no point of the curve- - ``bad_signature``- - The signature check failsThe attribute check would prove little here. The ``ak`` field travelswith the row from the node, so its attributes are what the node@@ -167,14 +162,6 @@ declares, and a software P-256 key can carry the same bits (U1 ofcredential activation, :doc:`attest-challenge` and ``shirudo attest--challenge``, and from pinning its name in :doc:`attest-verify`; abackend treats every node as ``untrusted`` until it has done that (R17).-The signer's name is public, so the comparison needs no constant time.--.. note::-- The two verifiers are separate code: a check added to one, or a fix- to one, does not reach the other. Change both, and their tests, in- the same commit. D10 of :doc:`/security/threat-model` is the- consolidation into one function.Output------diff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rstindex bf5e079d..89eeb4f7 100644--- a/Documentation/security/threat-model.rst+++ b/Documentation/security/threat-model.rst@@ -44,7 +44,7 @@ Every entry has a stable ID that commits, issues and other pages cite:- Residual risks: known and bounded limits of the mechanisms.* - U1 to U20- Threats to resolve: gaps in the code that are not closed yet.- * - D1 to D10+ * - D1 to D9- Deferred mitigations that are in scope.Each threat is explained in prose and then summarized in a fact box.@@ -1903,15 +1903,6 @@ D9 Perf events across execthat marks it (T3), tracked in `issue 11<https://github.com/shirudo-dev/shirudo/issues/11>`__.-D10 One verifier of a TPM quote- Check a quote's signature, its signer and the attestation key's- public area in one function of :src:`util/tpm_wire.c`, a- ``tpm_quote_signed()``, that ``shirudo attest verify`` and- ``shirudo-quote-check`` both call. Each has its own code today, and- the two check different fields of the key- (:doc:`/reference/cli/quote-check`), so a check or a fix one gains- can miss the other (R17, T9).-Out of scope------------diff --git a/tools/quote-check/row.c b/tools/quote-check/row.cindex 6f941fb8..96a28cdd 100644--- a/tools/quote-check/row.c+++ b/tools/quote-check/row.c@@ -103,52 +103,15 @@ static bool attest_is_keepalive_quote(const struct tpm_attest *attest)return attest->type == TPM_ST_ATTEST_QUOTE && !attest->pcr_mask;}-static int signer_is_endorsement_primary(const struct tpm_attest *attest,- const __u8 ak_name[TPM_NAME_LEN],- bool *is)-{- __u8 qualified[TPM_NAME_LEN];- int ret;-- ret = tpm_qualified_name(TPM_RH_ENDORSEMENT, ak_name, qualified);- if (ret)- return ret;- *is = attest->signer_len == TPM_NAME_LEN &&- !memcmp(attest->signer, qualified, TPM_NAME_LEN);- return 0;-}--/*- * 0 with *valid, or a negative errno. A key that is no point of the- * curve, -EINVAL, verifies nothing, like a signature that does not- * match.- */-static int signature_verify(const struct tpm_public *ak,- const __u8 *attest, size_t attest_len,- const __u8 r[32], const __u8 s[32], bool *valid)-{- __u8 digest[CRYPTO_SHA256_LEN];- int ret;-- ret = sha256_buffer(attest, attest_len, digest);- if (ret)- return ret;- ret = ecdsa_p256_verify(ak->x, ak->y, digest, r, s);- if (ret && ret != -EKEYREJECTED && ret != -EINVAL)- return ret;- *valid = !ret;- return 0;-}-int quote_row_check(const char *row, size_t len, struct quote_result *res){__u8 attest[TPM_ATTEST_MAX], sig[TPM_SIGNATURE_MAX];- __u8 body_sha256[TPM_DIGEST_LEN], r[32], s[32];+ __u8 body_sha256[TPM_DIGEST_LEN];struct field fields[FIELD_COUNT];size_t attest_len, sig_len;struct tpm_attest quote;struct tpm_public ak;- bool passed;+ bool signer;int ret;memset(res, 0, sizeof(*res));@@ -171,24 +134,23 @@ int quote_row_check(const char *row, size_t len, struct quote_result *res)ret = tpm_object_name(res->ak_public, res->ak_public_len,res->ak_name);if (!ret)- ret = signer_is_endorsement_primary("e, res->ak_name,- &passed);+ ret = tpm_attest_signer("e, res->ak_name, &signer);if (ret)return ret;- if (!passed) {+ if (!signer) {res->verdict = QUOTE_BAD_SIGNATURE;return 0;}- if (tpm_ecdsa_signature_parse(sig, sig_len, r, s))+ ret = tpm_attest_verify(attest, attest_len, sig, sig_len, ak.x, ak.y);+ if (ret == -EBADMSG)return 0;- ret = signature_verify(&ak, attest, attest_len, r, s, &passed);- if (ret)- return ret;- if (!passed) {+ if (ret == -EKEYREJECTED) {res->verdict = QUOTE_BAD_SIGNATURE;return 0;}+ if (ret)+ return ret;if (quote.extra_len != sizeof(body_sha256) ||memcmp(quote.extra, body_sha256, sizeof(body_sha256))) {diff --git a/util/attest.c b/util/attest.cindex 56217bd8..fde0ae67 100644--- a/util/attest.c+++ b/util/attest.c@@ -697,8 +697,8 @@ static int check_signed(struct reporter *rep, const char *what,__u8 *attest, size_t attest_cap, size_t *attest_len,struct tpm_attest *parsed){- __u8 sig[TPM_SIGNATURE_MAX], r[32], s[32], digest[32];- __u8 qn[TPM_NAME_LEN];+ __u8 sig[TPM_SIGNATURE_MAX];+ bool signer = false;size_t sig_len;int ret;@@ -709,17 +709,14 @@ static int check_signed(struct reporter *rep, const char *what,say(rep, false, "%s: attestation structure unparseable", what);return -EKEYREJECTED;}- if (tpm_ecdsa_signature_parse(sig, sig_len, r, s)) {+ ret = tpm_attest_verify(attest, *attest_len, sig, sig_len, ak->x,+ ak->y);+ if (ret == -EBADMSG) {say(rep, false, "%s: signature structure unparseable", what);return -EKEYREJECTED;}- ret = sha256_calc(attest, *attest_len, digest);- if (!ret)- ret = ecdsa_p256_verify(ak->x, ak->y, digest, r, s);say(rep, ret == 0, "%s: ECDSA signature by the attestation key", what);- say(rep, !tpm_qualified_name(TPM_RH_ENDORSEMENT, ak_name, qn) &&- parsed->signer_len == TPM_NAME_LEN &&- !memneq_explicit(parsed->signer, qn, TPM_NAME_LEN),+ say(rep, !tpm_attest_signer(parsed, ak_name, &signer) && signer,"%s: signed by the key as an endorsement primary", what);say(rep, parsed->extra_len == nonce_len &&(nonce_len == 0 ||diff --git a/util/tpm_wire.c b/util/tpm_wire.cindex a0adad3d..54ecf3b9 100644--- a/util/tpm_wire.c+++ b/util/tpm_wire.c@@ -624,3 +624,32 @@ int tpm_attest_parse(const __u8 *buf, size_t len,}return -EPROTONOSUPPORT;}++int tpm_attest_signer(const struct tpm_attest *a,+ const __u8 ak_name[TPM_NAME_LEN], bool *is)+{+ __u8 qualified[TPM_NAME_LEN];+ int ret;++ ret = tpm_qualified_name(TPM_RH_ENDORSEMENT, ak_name, qualified);+ if (ret)+ return ret;+ *is = a->signer_len == TPM_NAME_LEN &&+ !memneq_explicit(a->signer, qualified, TPM_NAME_LEN);+ return 0;+}++int tpm_attest_verify(const __u8 *attest, size_t len,+ const __u8 *sig, size_t sig_len,+ const __u8 x[32], const __u8 y[32])+{+ __u8 digest[CRYPTO_SHA256_LEN], r[32], s[32];+ int ret;++ if (tpm_ecdsa_signature_parse(sig, sig_len, r, s))+ return -EBADMSG;+ ret = sha256_buffer(attest, len, digest);+ if (!ret)+ ret = ecdsa_p256_verify(x, y, digest, r, s);+ return ret == -EINVAL ? -EKEYREJECTED : ret;+}diff --git a/util/tpm_wire.h b/util/tpm_wire.hindex 5a828f40..c5bd975c 100644--- a/util/tpm_wire.h+++ b/util/tpm_wire.h@@ -225,4 +225,19 @@ struct tpm_attest {int tpm_attest_parse(const __u8 *buf, size_t len,struct tpm_attest *out) __must_check;+/*+ * The checks of attest verify and of the keepalive quote checker, in one+ * place so that neither gains a check the other lacks. tpm_attest_signer()+ * sets *is when the TPM named the key of ak_name, an endorsement primary,+ * as the signer. tpm_attest_verify() is 0 when the ECDSA signature over+ * SHA-256 of attest verifies with the point x, y, -EKEYREJECTED when it+ * does not, a point off the curve included, and -EBADMSG when sig does+ * not parse.+ */+int tpm_attest_signer(const struct tpm_attest *a,+ const __u8 ak_name[TPM_NAME_LEN], bool *is) __must_check;+int tpm_attest_verify(const __u8 *attest, size_t len,+ const __u8 *sig, size_t sig_len,+ const __u8 x[32], const __u8 y[32]) __must_check;+#endif /* SHIRUDO_TPM_WIRE_H */