Debian Codegraph @

cmd/attest_svsm.go

raw
1 package cmd
2
3 import (
4 "errors"
5 "fmt"
6 "time"
7
8 "github.com/google/gce-tcb-verifier/extract"
9 "github.com/google/go-configfs-tsm/configfs/configfsi"
10 "github.com/google/go-configfs-tsm/configfs/linuxtsm"
11 "github.com/google/go-configfs-tsm/report"
12 sabi "github.com/google/go-sev-guest/abi"
13 sevpb "github.com/google/go-sev-guest/proto/sevsnp"
14 "github.com/google/go-tpm-tools/client"
15 apb "github.com/google/go-tpm-tools/proto/attest"
16 "github.com/spf13/cobra"
17 "google.golang.org/protobuf/proto"
18 )
19
20 var (
21 errSVSMOnlySupportsAK = errors.New("SVSM currently only support --key=AK")
22 errSvsmOnlySupportedWithSevSnp = errors.New("--svsm is only supported with --tee-technology=sev-snp")
23 )
24
25 var attestSVSMCmd = &cobra.Command{
26 Use: "svsm",
27 Short: `Produce a SevSnpSvsmAttestation that wraps the PCR attestation message.`,
28 RunE: func(*cobra.Command, []string) error {
29 if teeTechnology != SevSnp {
30 return errSvsmOnlySupportedWithSevSnp
31 }
32 rwc, err := openTpm()
33 if err != nil {
34 return err
35 }
36 defer rwc.Close()
37
38 var attestationKey *client.Key
39 if key != "AK" {
40 return errSVSMOnlySupportsAK
41 }
42 algoToCreateAK, ok := attestationKeys[key]
43 if !ok {
44 return fmt.Errorf("%v is an invalid value for --key, only AK is supported", key)
45 }
46 createFunc := algoToCreateAK[keyAlgo]
47 attestationKey, err = createFunc(rwc)
48 if err != nil {
49 return fmt.Errorf("failed to create attestation key: %v", err)
50 }
51 defer attestationKey.Close()
52
53 attestOpts := client.AttestOpts{}
54 attestOpts.Nonce = nonce
55 // Omit requesting TEE attestation for the Attestation message when attesting an SVSM based vTPM.
56 // We instead separately attach a TEE attestation inside the SevSnpSvsmAttestation message
57 attestOpts.SkipTeeAttestation = true
58
59 attestOpts.TCGEventLog, err = client.GetEventLog(rwc)
60 if err != nil {
61 return fmt.Errorf("failed to retrieve TCG Event Log: %w", err)
62 }
63
64 attestation, err := attestationKey.Attest(attestOpts)
65 if err != nil {
66 return fmt.Errorf("failed to collect attestation report : %v", err)
67 }
68
69 if teeTechnology != SevSnp {
70 return errSvsmOnlySupportedWithSevSnp
71 }
72 configfsClient, err := linuxtsm.MakeClient()
73 if err != nil {
74 return fmt.Errorf("failed to create linuxtsm configfs client: %w", err)
75 }
76 svsmAttestation, err := makeSEVSNPSVSMAttestation(attestation, &sevSNPSVSMAttestationOpts{
77 TEENonce: teeNonce,
78 CongfigfsClient: configfsClient,
79 VTPMServiceManifestVersion: "0",
80 ExtractOptions: extract.DefaultOptions(),
81 })
82 if err != nil {
83 return fmt.Errorf("failed to create SEV SNP SVSM attestation: %w", err)
84 }
85 if err := writeProtoToOutput(svsmAttestation); err != nil {
86 return fmt.Errorf("failed to write SEV SNP SVSM attestation report: %w", err)
87 }
88
89 return nil
90 },
91 }
92
93 // sevSNPSVSMAttestationOpts customizes the behavior of makeSEVSNPSVSMAttestation.
94 type sevSNPSVSMAttestationOpts struct {
95 // 64 byte nonce to be mixed into the REPORT_DATA field of the SNP attestation report.
96 TEENonce []byte
97 // Configfs client to use for retrieving the attestation report, certificates, and SVSM service manifest.
98 CongfigfsClient configfsi.Client
99 // The SVSM service manifest and its version is defined by the SVSM spec at
100 // https://www.amd.com/en/developer/sev.html
101 // Failing to specify a value here will result in requesting the default manifest version of "0"
102 // See https://github.com/torvalds/linux/blob/v6.16/Documentation/ABI/testing/configfs-tsm-report
103 VTPMServiceManifestVersion string
104 // Options for configuring how to extract a firmware endorsement.
105 // Leave as nil to skip getting firmware endorsement.
106 ExtractOptions *extract.Options
107 }
108
109 // makeSEVSNPSVSMAttestation fills out the fields of a SevSnpSvsmAttestation message needed to verify an SVSM e-vTPM.
110 // This includes the SNP attestation report and the vtpm service manifest.
111 func makeSEVSNPSVSMAttestation(attestation *apb.Attestation, opts *sevSNPSVSMAttestationOpts) (*apb.SevSnpSvsmAttestation, error) {
112 svsm := &apb.SevSnpSvsmAttestation{
113 Attestation: attestation,
114 }
115 var snpNonce [sabi.ReportDataSize]byte
116 if len(opts.TEENonce) != sabi.ReportDataSize {
117 return nil, fmt.Errorf("the teeNonce size is %d. SEV-SNP device requires 64", len(opts.TEENonce))
118 }
119 copy(snpNonce[:], opts.TEENonce)
120
121 // There is a host ratelimit of 2 requests per 2 seconds on guest message requests
122 // and SVSM will decide to crash if it runs into this ratelimit.
123 // Until we fix this in Coconut SVSM and increase the host ratelimit, ensure a 2
124 // second delay prior to issuing an attestation report request to SVSM.
125 time.Sleep(2 * time.Second)
126 tsmBlobs, err := getSVSMBlobs(opts.CongfigfsClient, snpNonce, opts.VTPMServiceManifestVersion)
127 if err != nil {
128 return nil, fmt.Errorf("failed to get configfs-tsm blobs for SVSM attestation report: %w", err)
129 }
130 report, err := sabi.ReportToProto(tsmBlobs.OutBlob)
131 if err != nil {
132 return nil, fmt.Errorf("failed to convert attestation report to proto: %w", err)
133 }
134
135 certs, err := getCertificates(opts.CongfigfsClient, snpNonce)
136 if err != nil {
137 return nil, fmt.Errorf("failed to retrieve certificates from configfs-tsm: %w", err)
138 }
139
140 svsm.SevSnpAttestation = &sevpb.Attestation{
141 Report: report,
142 CertificateChain: certs,
143 }
144 svsm.VtpmServiceManifest = tsmBlobs.ManifestBlob
145 if opts.VTPMServiceManifestVersion == "" {
146 svsm.VtpmServiceManifestVersion = defaultConfigfsTsmReportServiceManifestVersion
147 }
148 svsm.VtpmServiceManifestVersion = opts.VTPMServiceManifestVersion
149
150 if opts.ExtractOptions != nil {
151 svsm.LaunchEndorsement, err = getEndorsement(svsm.SevSnpAttestation, opts.ExtractOptions)
152 if err != nil {
153 return nil, fmt.Errorf("failed to get endorsement for svsm firmware: %w", err)
154 }
155 }
156 return svsm, nil
157 }
158
159 func getEndorsement(attestation *sevpb.Attestation, extractOpts *extract.Options) ([]byte, error) {
160 if extractOpts == nil {
161 return nil, nil
162 }
163 out, err := proto.Marshal(attestation)
164 if err != nil {
165 return nil, fmt.Errorf("failed to marshal sev snp attestation: %w", err)
166 }
167 extractOpts.Quote = out
168 return extract.Endorsement(extractOpts)
169 }
170
171 // Constants for input to various blobs of configfs-tsm-report defined by linux kernel.
172 // See https://github.com/torvalds/linux/blob/v6.16/Documentation/ABI/testing/configfs-tsm-report
173 const (
174 svsmServiceProvider = "svsm"
175 // GUID for SVSM vTPM attestation defined by SVSM spec.
176 // See https://www.amd.com/en/developer/sev.html for SVSM spec
177 svsmVTPMServiceGUID = "c476f1eb-0123-45a5-9641-b4e7dde5bfe3"
178 leastPrivilegedVMPL = 3
179 defaultConfigfsTsmReportServiceManifestVersion = "0"
180 )
181
182 var (
183 errFailedToRetrieveCertificates = errors.New("failed to retrieve certificates")
184 )
185
186 // SVSM currently doesn't support certificates in its attestation report, so here we collect
187 // the certificate chain by requesting a report without SVSM to get the cached certificates.
188 func getCertificates(configfs configfsi.Client, reportData [sabi.ReportDataSize]byte) (*sevpb.CertificateChain, error) {
189 resp, err := report.Get(configfs, &report.Request{
190 InBlob: reportData[:],
191 GetAuxBlob: true,
192 Privilege: &report.Privilege{
193 Level: uint(leastPrivilegedVMPL),
194 },
195 })
196 if err != nil {
197 return nil, errFailedToRetrieveCertificates
198 }
199 extended, err := sabi.ExtendedPlatformCertTable(resp.AuxBlob)
200 if err != nil {
201 return nil, fmt.Errorf("invalid certificate table: %w", err)
202 }
203 table := new(sabi.CertTable)
204 if err := table.Unmarshal(extended); err != nil {
205 return nil, fmt.Errorf("failed to unmarshal certificates: %w", err)
206 }
207 return table.Proto(), nil
208 }
209
210 func getSVSMBlobs(configfs configfsi.Client, reportData [sabi.ReportDataSize]byte, vtpmServiceManifestVersion string) (*report.Response, error) {
211 resp, err := report.Get(configfs, &report.Request{
212 InBlob: reportData[:],
213 ServiceProvider: svsmServiceProvider,
214 ServiceGuid: svsmVTPMServiceGUID,
215 ServiceManifestVersion: vtpmServiceManifestVersion,
216 })
217 if err != nil {
218 return nil, fmt.Errorf("could not get SVSM attestation report: %w", err)
219 }
220 return resp, nil
221 }