Jelajahi Sumber

Bind the SEV-SNP report's TCB and chip ID to its signing key

The verifier checked the report's signature and the VCEK or VLEK chain,
and then reported the report's TCB fields as they stood. The key's
certificate carries the TCB it was issued for, and a VCEK the chip's id,
but a certificate table from the workload need not match the report: a
report signed by an old VCEK could claim any TCB, so a webhook's TCB floor
did not hold against the holder of an old key, the case it exists for.

go-sev-guest's validate now runs after the verification, with guest
policy options that permit everything, since debugging is refused with
its own error and nothing else of the policy is checked. It refuses a
reported TCB that differs from the certificate's, a current TCB below
it, a committed TCB above the current one, and a CHIP_ID that is not the
VCEK's HWID.

Found by an adversarial review of the branch.

Claude-Session: https://claude.ai/code/session_01CjfEMLGpnHQaWsmxe1zmLF
Josh Drake 1 bulan lalu
induk
melakukan
8bbadaefd4

+ 18 - 1
attestation/sevsnp/sevsnp.go

@@ -17,6 +17,7 @@ import (
 	"github.com/google/go-sev-guest/abi"
 	"github.com/google/go-sev-guest/abi"
 	"github.com/google/go-sev-guest/kds"
 	"github.com/google/go-sev-guest/kds"
 	spb "github.com/google/go-sev-guest/proto/sevsnp"
 	spb "github.com/google/go-sev-guest/proto/sevsnp"
+	sevvalidate "github.com/google/go-sev-guest/validate"
 	sevverify "github.com/google/go-sev-guest/verify"
 	sevverify "github.com/google/go-sev-guest/verify"
 	sevtrust "github.com/google/go-sev-guest/verify/trust"
 	sevtrust "github.com/google/go-sev-guest/verify/trust"
 
 
@@ -31,7 +32,8 @@ const ReportDataSize = 64
 // or ASVK to the ARK of the product line. Certificates the workload does not
 // or ASVK to the ARK of the product line. Certificates the workload does not
 // send are fetched from AMD's key distribution service, as are the revocation
 // send are fetched from AMD's key distribution service, as are the revocation
 // lists. Reports from guests whose policy allows debugging are refused. The TCB
 // lists. Reports from guests whose policy allows debugging are refused. The TCB
-// versions are reported, not checked against a minimum.
+// versions must match the signing key's certificate, and are reported, not
+// checked against a minimum.
 type Verifier struct {
 type Verifier struct {
 	// roots are the ARK, ASK and ASVK certificates to trust per product
 	// roots are the ARK, ASK and ASVK certificates to trust per product
 	// line: the configured ones, or AMD's.
 	// line: the configured ones, or AMD's.
@@ -159,6 +161,21 @@ func (v *Verifier) Verify(ctx context.Context, rawReport, certTable []byte) (*at
 		}
 		}
 		return nil, nil, attestation.Invalidf("error verifying sev-snp report: %w", err)
 		return nil, nil, attestation.Invalidf("error verifying sev-snp report: %w", err)
 	}
 	}
+	// The signature proves only that the VCEK or VLEK signed the report. The
+	// key's certificate carries the TCB it was issued for, and for a VCEK the
+	// chip's id, which a certificate table from the workload need not match:
+	// with an old VCEK, the report's TCB fields would say whatever its signer
+	// chose. validate binds them to the certificate, refusing a reported TCB
+	// that differs from the certificate's, a current TCB below it, a committed
+	// TCB above the current one, and a CHIP_ID that is not the VCEK's. Its
+	// guest policy options permit everything, since debugging is refused below
+	// with its own error and nothing else of the policy is checked.
+	if err := sevvalidate.SnpAttestation(att, &sevvalidate.Options{
+		GuestPolicy:               abi.SnpPolicy{MigrateMA: true, Debug: true, SMT: true, CXLAllowed: true},
+		PermitProvisionalFirmware: true,
+	}); err != nil {
+		return nil, nil, attestation.Invalidf("error validating sev-snp report: %w", err)
+	}
 
 
 	policy, err := abi.ParseSnpPolicy(report.GetPolicy())
 	policy, err := abi.ParseSnpPolicy(report.GetPolicy())
 	if err != nil {
 	if err != nil {

+ 11 - 0
attestation/sevsnp/sevsnp_test.go

@@ -4,6 +4,7 @@ package sevsnp
 
 
 import (
 import (
 	"context"
 	"context"
+	"encoding/binary"
 	"encoding/pem"
 	"encoding/pem"
 	"errors"
 	"errors"
 	"testing"
 	"testing"
@@ -81,6 +82,16 @@ func TestVerifier(t *testing.T) {
 			report[0x50] ^= 1 // the first byte of REPORT_DATA
 			report[0x50] ^= 1 // the first byte of REPORT_DATA
 			return report
 			return report
 		}(), s.CertTable, "error verifying sev-snp report", true},
 		}(), s.CertTable, "error verifying sev-snp report", true},
+		// The VCEK signed the report but was issued for another TCB, so the
+		// report's TCB fields would be whatever its signer chose.
+		{"fail tcb of another vcek", newVerifier(t, s.Roots), s.EditedReport(t, reportData, false, func(report []byte) {
+			const newer = uint64(0x1800000000000a04)
+			binary.LittleEndian.PutUint64(report[0x38:0x40], newer)   // CURRENT_TCB
+			binary.LittleEndian.PutUint64(report[0x180:0x188], newer) // REPORTED_TCB
+		}), s.CertTable, "does not match the TCB of the V[CL]EK certificate", true},
+		{"fail chip id of another vcek", newVerifier(t, s.Roots), s.EditedReport(t, reportData, false, func(report []byte) {
+			report[0x1a0] ^= 1 // the first byte of CHIP_ID
+		}), s.CertTable, "is not the same as the VCEK certificate's HWID", true},
 		{"fail parse", newVerifier(t, s.Roots), []byte("not a report"), nil, "error parsing sev-snp report", true},
 		{"fail parse", newVerifier(t, s.Roots), []byte("not a report"), nil, "error parsing sev-snp report", true},
 		{"fail certificate table", newVerifier(t, s.Roots), s.Report(t, reportData, false), []byte("not a table"),
 		{"fail certificate table", newVerifier(t, s.Roots), s.Report(t, reportData, false), []byte("not a table"),
 			"error parsing sev-snp certificate table", true},
 			"error parsing sev-snp certificate table", true},

+ 10 - 0
attestation/sevsnp/sevsnptest/sevsnptest.go

@@ -84,6 +84,13 @@ func (s *Signer) Asvk() *x509.Certificate { return s.signer.Asvk }
 // Report returns a signed version 3 report with the given report data, from a
 // Report returns a signed version 3 report with the given report data, from a
 // guest whose policy allows debugging if debug is set.
 // guest whose policy allows debugging if debug is set.
 func (s *Signer) Report(t testing.TB, reportData []byte, debug bool) []byte {
 func (s *Signer) Report(t testing.TB, reportData []byte, debug bool) []byte {
+	t.Helper()
+	return s.EditedReport(t, reportData, debug, nil)
+}
+
+// EditedReport is Report with edit, if not nil, applied to the report's bytes
+// before they are signed.
+func (s *Signer) EditedReport(t testing.TB, reportData []byte, debug bool, edit func(report []byte)) []byte {
 	t.Helper()
 	t.Helper()
 	raw := sevtest.CreateRawReport(&sevtest.TestReportOptions{
 	raw := sevtest.CreateRawReport(&sevtest.TestReportOptions{
 		ReportData: reportData,
 		ReportData: reportData,
@@ -94,6 +101,9 @@ func (s *Signer) Report(t testing.TB, reportData []byte, debug bool) []byte {
 	report := append([]byte(nil), raw[:abi.ReportSize]...)
 	report := append([]byte(nil), raw[:abi.ReportSize]...)
 	// CreateRawReport's policy allows debugging.
 	// CreateRawReport's policy allows debugging.
 	binary.LittleEndian.PutUint64(report[0x08:0x10], abi.SnpPolicyToBytes(abi.SnpPolicy{Debug: debug, SMT: true}))
 	binary.LittleEndian.PutUint64(report[0x08:0x10], abi.SnpPolicyToBytes(abi.SnpPolicy{Debug: debug, SMT: true}))
+	if edit != nil {
+		edit(report)
+	}
 	r, sig, err := s.signer.Sign(abi.SignedComponent(report))
 	r, sig, err := s.signer.Sign(abi.SignedComponent(report))
 	if err != nil {
 	if err != nil {
 		t.Fatal(err)
 		t.Fatal(err)