Debian Codegraph @

attest/secureboot.go

raw
1 // Copyright 2020 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14
15 package attest
16
17 import (
18 "bytes"
19 "crypto/x509"
20 "errors"
21 "fmt"
22
23 "github.com/google/go-attestation/attest/internal"
24 )
25
26 // SecurebootState describes the secure boot status of a machine, as determined
27 // by processing its event log.
28 type SecurebootState struct {
29 Enabled bool
30
31 // PlatformKeys enumerates keys which can sign a key exchange key.
32 PlatformKeys []x509.Certificate
33 // PlatformKeys enumerates key hashes which can sign a key exchange key.
34 PlatformKeyHashes [][]byte
35
36 // ExchangeKeys enumerates keys which can sign a database of permitted or
37 // forbidden keys.
38 ExchangeKeys []x509.Certificate
39 // ExchangeKeyHashes enumerates key hashes which can sign a database or
40 // permitted or forbidden keys.
41 ExchangeKeyHashes [][]byte
42
43 // PermittedKeys enumerates keys which may sign binaries to run.
44 PermittedKeys []x509.Certificate
45 // PermittedHashes enumerates hashes which permit binaries to run.
46 PermittedHashes [][]byte
47
48 // ForbiddenKeys enumerates keys which must not permit a binary to run.
49 ForbiddenKeys []x509.Certificate
50 // ForbiddenKeys enumerates hashes which must not permit a binary to run.
51 ForbiddenHashes [][]byte
52
53 // PreSeparatorAuthority describes the use of a secure-boot key to authorize
54 // the execution of a binary before the separator.
55 PreSeparatorAuthority []x509.Certificate
56 // PostSeparatorAuthority describes the use of a secure-boot key to authorize
57 // the execution of a binary after the separator.
58 PostSeparatorAuthority []x509.Certificate
59
60 // DriverLoadSourceHints describes the origin of boot services drivers.
61 // This data is not tamper-proof and must only be used as a hint.
62 DriverLoadSourceHints []DriverLoadSource
63
64 // DMAProtectionDisabled is true if the platform reports during boot that
65 // DMA protection is supported but disabled.
66 //
67 // See: https://docs.microsoft.com/en-us/windows-hardware/design/device-experiences/oem-kernel-dma-protection
68 DMAProtectionDisabled bool
69 }
70
71 // DriverLoadSource describes the logical origin of a boot services driver.
72 type DriverLoadSource uint8
73
74 const (
75 // UnknownSource indicates that the driver load source is unknown.
76 UnknownSource DriverLoadSource = iota
77 // PciMmioSource indicates that the driver load source is PCI-MMIO.
78 PciMmioSource
79 )
80
81 // ParseSecurebootState parses a series of events to determine the
82 // configuration of secure boot on a device. An error is returned if
83 // the state cannot be determined, or if the event log is structured
84 // in such a way that it may have been tampered post-execution of
85 // platform firmware.
86 func ParseSecurebootState(events []Event) (*SecurebootState, error) {
87 // This algorithm verifies the following:
88 // - All events in PCR 7 have event types which are expected in PCR 7.
89 // - All events are parsable according to their event type.
90 // - All events have digests values corresponding to their data/event type.
91 // - No unverifiable events were present.
92 // - All variables are specified before the separator and never duplicated.
93 // - The SecureBoot variable has a value of 0 or 1.
94 // - If SecureBoot was 1 (enabled), authority events were present indicating
95 // keys were used to perform verification.
96 // - If SecureBoot was 1 (enabled), platform + exchange + database keys
97 // were specified.
98 // - No UEFI debugger was attached.
99
100 var (
101 out SecurebootState
102 seenSeparator7 bool
103 seenSeparator2 bool
104 seenAuthority bool
105 seenVars = map[string]bool{}
106 driverSources [][]internal.EFIDevicePathElement
107 )
108
109 for _, e := range events {
110 if e.Index != 7 && e.Index != 2 {
111 continue
112 }
113
114 et, err := internal.UntrustedParseEventType(uint32(e.Type))
115 if err != nil {
116 return nil, fmt.Errorf("unrecognised event type: %v", err)
117 }
118 digestVerify := e.digestEquals(e.Data)
119
120 switch e.Index {
121 case 7:
122 switch et {
123 case internal.Separator:
124 if seenSeparator7 {
125 return nil, fmt.Errorf("duplicate separator at event %d", e.sequence)
126 }
127 seenSeparator7 = true
128 if !bytes.Equal(e.Data, []byte{0, 0, 0, 0}) {
129 return nil, fmt.Errorf("invalid separator data at event %d: %v", e.sequence, e.Data)
130 }
131 if digestVerify != nil {
132 return nil, fmt.Errorf("invalid separator digest at event %d: %v", e.sequence, digestVerify)
133 }
134
135 case internal.EFIAction:
136 switch string(e.Data) {
137 case "UEFI Debug Mode":
138 return nil, errors.New("a UEFI debugger was present during boot")
139 case "DMA Protection Disabled":
140 if digestVerify != nil {
141 return nil, fmt.Errorf("invalid digest for EFI Action 'DMA Protection Disabled' on event %d: %v", e.sequence, digestVerify)
142 }
143 out.DMAProtectionDisabled = true
144 default:
145 return nil, fmt.Errorf("event %d: unexpected EFI action event", e.sequence)
146 }
147
148 case internal.EFIVariableDriverConfig:
149 v, err := internal.ParseUEFIVariableData(bytes.NewReader(e.Data))
150 if err != nil {
151 return nil, fmt.Errorf("failed parsing EFI variable at event %d: %v", e.sequence, err)
152 }
153 if _, seenBefore := seenVars[v.VarName()]; seenBefore {
154 return nil, fmt.Errorf("duplicate EFI variable %q at event %d", v.VarName(), e.sequence)
155 }
156 seenVars[v.VarName()] = true
157 if seenSeparator7 {
158 return nil, fmt.Errorf("event %d: variable %q specified after separator", e.sequence, v.VarName())
159 }
160
161 if digestVerify != nil {
162 return nil, fmt.Errorf("invalid digest for variable %q on event %d: %v", v.VarName(), e.sequence, digestVerify)
163 }
164
165 switch v.VarName() {
166 case "SecureBoot":
167 if len(v.VariableData) != 1 {
168 return nil, fmt.Errorf("event %d: SecureBoot data len is %d, expected 1", e.sequence, len(v.VariableData))
169 }
170 out.Enabled = v.VariableData[0] == 1
171 case "PK":
172 if out.PlatformKeys, out.PlatformKeyHashes, err = v.SignatureData(); err != nil {
173 return nil, fmt.Errorf("event %d: failed parsing platform keys: %v", e.sequence, err)
174 }
175 case "KEK":
176 if out.ExchangeKeys, out.ExchangeKeyHashes, err = v.SignatureData(); err != nil {
177 return nil, fmt.Errorf("event %d: failed parsing key exchange keys: %v", e.sequence, err)
178 }
179 case "db":
180 if out.PermittedKeys, out.PermittedHashes, err = v.SignatureData(); err != nil {
181 return nil, fmt.Errorf("event %d: failed parsing signature database: %v", e.sequence, err)
182 }
183 case "dbx":
184 if out.ForbiddenKeys, out.ForbiddenHashes, err = v.SignatureData(); err != nil {
185 return nil, fmt.Errorf("event %d: failed parsing forbidden signature database: %v", e.sequence, err)
186 }
187 }
188
189 case internal.EFIVariableAuthority:
190 v, err := internal.ParseUEFIVariableData(bytes.NewReader(e.Data))
191 if err != nil {
192 return nil, fmt.Errorf("failed parsing UEFI variable data: %v", err)
193 }
194
195 a, err := internal.ParseUEFIVariableAuthority(v)
196 if err != nil {
197 // Workaround for: https://github.com/google/go-attestation/issues/157
198 if err == internal.ErrSigMissingGUID {
199 // Versions of shim which do not carry
200 // https://github.com/rhboot/shim/commit/8a27a4809a6a2b40fb6a4049071bf96d6ad71b50
201 // have an erroneous additional byte in the event, which breaks digest
202 // verification. If verification failed, we try removing the last byte.
203 if digestVerify != nil && len(e.Data) > 0 {
204 digestVerify = e.digestEquals(e.Data[:len(e.Data)-1])
205 }
206 } else {
207 return nil, fmt.Errorf("failed parsing EFI variable authority at event %d: %v", e.sequence, err)
208 }
209 }
210 seenAuthority = true
211 if digestVerify != nil {
212 return nil, fmt.Errorf("invalid digest for authority on event %d: %v", e.sequence, digestVerify)
213 }
214 if !seenSeparator7 {
215 out.PreSeparatorAuthority = append(out.PreSeparatorAuthority, a.Certs...)
216 } else {
217 out.PostSeparatorAuthority = append(out.PostSeparatorAuthority, a.Certs...)
218 }
219
220 default:
221 return nil, fmt.Errorf("unexpected event type in PCR7: %v", et)
222 }
223
224 case 2:
225 switch et {
226 case internal.Separator:
227 if seenSeparator2 {
228 return nil, fmt.Errorf("duplicate separator at event %d", e.sequence)
229 }
230 seenSeparator2 = true
231 if !bytes.Equal(e.Data, []byte{0, 0, 0, 0}) {
232 return nil, fmt.Errorf("invalid separator data at event %d: %v", e.sequence, e.Data)
233 }
234 if digestVerify != nil {
235 return nil, fmt.Errorf("invalid separator digest at event %d: %v", e.sequence, digestVerify)
236 }
237
238 case internal.EFIBootServicesDriver:
239 if !seenSeparator2 {
240 imgLoad, err := internal.ParseEFIImageLoad(bytes.NewReader(e.Data))
241 if err != nil {
242 return nil, fmt.Errorf("failed parsing EFI image load at boot services driver event %d: %v", e.sequence, err)
243 }
244 dp, err := imgLoad.DevicePath()
245 if err != nil {
246 return nil, fmt.Errorf("failed to parse device path for driver load event %d: %v", e.sequence, err)
247 }
248 driverSources = append(driverSources, dp)
249 }
250 }
251 }
252 }
253
254 // Compute driver source hints based on the EFI device path observed in
255 // EFI Boot-services driver-load events.
256 sourceLoop:
257 for _, source := range driverSources {
258 // We consider a driver to have originated from PCI-MMIO if any number
259 // of elements in the device path [1] were PCI devices, and are followed by
260 // an element representing a "relative offset range" read.
261 // In the wild, we have typically observed 4-tuple device paths for such
262 // devices: ACPI device -> PCI device -> PCI device -> relative offset.
263 //
264 // [1]: See section 9 of the UEFI specification v2.6 or greater.
265 var seenPCI bool
266 for _, e := range source {
267 // subtype 0x1 corresponds to a PCI device (See: 9.3.2.1)
268 if e.Type == internal.HardwareDevice && e.Subtype == 0x1 {
269 seenPCI = true
270 }
271 // subtype 0x8 corresponds to "relative offset range" (See: 9.3.6.8)
272 if seenPCI && e.Type == internal.MediaDevice && e.Subtype == 0x8 {
273 out.DriverLoadSourceHints = append(out.DriverLoadSourceHints, PciMmioSource)
274 continue sourceLoop
275 }
276 }
277 out.DriverLoadSourceHints = append(out.DriverLoadSourceHints, UnknownSource)
278 }
279
280 if !out.Enabled {
281 return &out, nil
282 }
283
284 if !seenAuthority {
285 return nil, errors.New("secure boot was enabled but no key was used")
286 }
287 if len(out.PlatformKeys) == 0 && len(out.PlatformKeyHashes) == 0 {
288 return nil, errors.New("secure boot was enabled but no platform keys were known")
289 }
290 if len(out.ExchangeKeys) == 0 && len(out.ExchangeKeyHashes) == 0 {
291 return nil, errors.New("secure boot was enabled but no key exchange keys were known")
292 }
293 if len(out.PermittedKeys) == 0 && len(out.PermittedHashes) == 0 {
294 return nil, errors.New("secure boot was enabled but no keys or hashes were permitted")
295 }
296 return &out, nil
297 }