1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
|
/** @file
This driver verifies and reports OBB FVs.
Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include "FvReportPei.h"
STATIC CONST HASH_ALG_INFO mHashAlgInfo[] = {
{TPM_ALG_SHA256, SHA256_DIGEST_SIZE, Sha256Init, Sha256Update, Sha256Final, Sha256HashAll}, // 000B
{TPM_ALG_SHA384, SHA384_DIGEST_SIZE, Sha384Init, Sha384Update, Sha384Final, Sha384HashAll}, // 000C
{TPM_ALG_SHA512, SHA512_DIGEST_SIZE, Sha512Init, Sha512Update, Sha512Final, Sha512HashAll}, // 000D
};
/**
Find hash algorithm information from mHashAlgInfo according to given ID.
@param[in] HashAlgId Hash algorithm type id.
@retval Pointer to HASH_ALG_INFO if given hash algorithm is supported.
@retval NULL if given algorithm is not supported.
**/
STATIC
CONST
HASH_ALG_INFO *
FindHashAlgInfo (
IN UINT16 HashAlgId
)
{
UINTN Index;
for (Index = 0; Index < ARRAY_SIZE (mHashAlgInfo); ++Index) {
if (mHashAlgInfo[Index].HashAlgId == HashAlgId) {
return &mHashAlgInfo[Index];
}
}
return NULL;
}
/**
Install a EDKII_PEI_FIRMWARE_VOLUME_INFO_PREHASHED_FV_PPI instance so that
TCG driver may use to extend PCRs.
@param[in] FvBuffer Buffer containing the whole FV.
@param[in] FvLength Length of the FV.
@param[in] HashAlgoId Hash algorithm type id.
@param[in] HashSize Hash size.
@param[in] HashValue Hash value buffer.
**/
STATIC
VOID
InstallPreHashFvPpi (
IN VOID *FvBuffer,
IN UINTN FvLength,
IN UINT16 HashAlgoId,
IN UINT16 HashSize,
IN UINT8 *HashValue
)
{
EFI_STATUS Status;
EFI_PEI_PPI_DESCRIPTOR *FvInfoPpiDescriptor;
EDKII_PEI_FIRMWARE_VOLUME_INFO_PREHASHED_FV_PPI *PreHashedFvPpi;
UINTN PpiSize;
HASH_INFO *HashInfo;
PpiSize = sizeof (EDKII_PEI_FIRMWARE_VOLUME_INFO_PREHASHED_FV_PPI)
+ sizeof (HASH_INFO)
+ HashSize;
PreHashedFvPpi = AllocatePool (PpiSize);
ASSERT (PreHashedFvPpi != NULL);
PreHashedFvPpi->FvBase = (UINT32)(UINTN)FvBuffer;
PreHashedFvPpi->FvLength = (UINT32)FvLength;
PreHashedFvPpi->Count = 1;
HashInfo = HASH_INFO_PTR (PreHashedFvPpi);
HashInfo->HashAlgoId = HashAlgoId;
HashInfo->HashSize = HashSize;
CopyMem (HASH_VALUE_PTR (HashInfo), HashValue, HashSize);
FvInfoPpiDescriptor = AllocatePool (sizeof (EFI_PEI_PPI_DESCRIPTOR));
ASSERT (FvInfoPpiDescriptor != NULL);
FvInfoPpiDescriptor->Guid = &gEdkiiPeiFirmwareVolumeInfoPrehashedFvPpiGuid;
FvInfoPpiDescriptor->Flags = EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST;
FvInfoPpiDescriptor->Ppi = (VOID *) PreHashedFvPpi;
Status = PeiServicesInstallPpi (FvInfoPpiDescriptor);
ASSERT_EFI_ERROR (Status);
}
/**
Calculate and verify hash value for given FV.
@param[in] HashInfo Hash information of the FV.
@param[in] FvInfo Information of FV used for verification.
@param[in] FvNumber Length of the FV.
@param[in] BootMode Length of the FV.
@retval EFI_SUCCESS The given FV is integrate.
@retval EFI_VOLUME_CORRUPTED The given FV is corrupted (hash mismatch).
@retval EFI_UNSUPPORTED The hash algorithm is not supported.
**/
STATIC
EFI_STATUS
VerifyHashedFv (
IN FV_HASH_INFO *HashInfo,
IN HASHED_FV_INFO *FvInfo,
IN UINTN FvNumber,
IN EFI_BOOT_MODE BootMode
)
{
UINTN FvIndex;
CONST HASH_ALG_INFO *AlgInfo;
UINT8 *HashValue;
UINT8 *FvHashValue;
VOID *FvBuffer;
EFI_STATUS Status;
if (HashInfo == NULL ||
HashInfo->HashSize == 0 ||
HashInfo->HashAlgoId == TPM_ALG_NULL) {
DEBUG ((DEBUG_INFO, "Bypass FV hash verification\r\n"));
return EFI_SUCCESS;
}
AlgInfo = FindHashAlgInfo (HashInfo->HashAlgoId);
if (AlgInfo == NULL || AlgInfo->HashSize != HashInfo->HashSize) {
DEBUG ((DEBUG_ERROR, "Unsupported or wrong hash algorithm: %04X (size=%d)\r\n",
HashInfo->HashAlgoId, HashInfo->HashSize));
return EFI_UNSUPPORTED;
}
ASSERT (FvInfo != NULL);
ASSERT (FvNumber > 0);
//
// We need a hash value for each FV as well as one for all FVs.
//
HashValue = AllocateZeroPool (AlgInfo->HashSize * (FvNumber + 1));
ASSERT (HashValue != NULL);
//
// Calculate hash value for each FV first.
//
FvHashValue = HashValue;
for (FvIndex = 0; FvIndex < FvNumber; ++FvIndex) {
//
// Not meant for verified boot and/or measured boot?
//
if ((FvInfo[FvIndex].Flag & HASHED_FV_FLAG_VERIFIED_BOOT) == 0 &&
(FvInfo[FvIndex].Flag & HASHED_FV_FLAG_MEASURED_BOOT) == 0) {
continue;
}
//
// Skip any FV not meant for current boot mode.
//
if ((FvInfo[FvIndex].Flag & HASHED_FV_FLAG_SKIP_BOOT_MODE (BootMode)) != 0) {
DEBUG ((DEBUG_INFO, "Skip FV[%016lX] for boot mode[%d]\r\n",
FvInfo[FvIndex].Base, BootMode));
continue;
}
DEBUG ((
DEBUG_INFO,
"Pre-hashed[alg=%04X,size=%d,flag=%016lX] FV: 0x%016lX (%08lX) (Flag=%016lX)\r\n",
HashInfo->HashAlgoId,
HashInfo->HashSize,
HashInfo->HashFlag,
FvInfo[FvIndex].Base,
FvInfo[FvIndex].Length,
FvInfo[FvIndex].Flag
));
//
// Copy FV to permanent memory to avoid potential TOC/TOU.
//
FvBuffer = AllocatePages (EFI_SIZE_TO_PAGES((UINTN)FvInfo[FvIndex].Length));
ASSERT (FvBuffer != NULL);
CopyMem (FvBuffer, (CONST VOID *)(UINTN)FvInfo[FvIndex].Base, (UINTN)FvInfo[FvIndex].Length);
if (!AlgInfo->HashAll (FvBuffer, (UINTN)FvInfo[FvIndex].Length, FvHashValue)) {
Status = EFI_ABORTED;
goto Done;
}
//
// Report the FV measurement.
//
if ((FvInfo[FvIndex].Flag & HASHED_FV_FLAG_MEASURED_BOOT) != 0) {
InstallPreHashFvPpi (
FvBuffer,
(UINTN)FvInfo[FvIndex].Length,
HashInfo->HashAlgoId,
HashInfo->HashSize,
FvHashValue
);
}
//
// Don't keep the hash value of current FV if we don't need to verify it.
//
if ((FvInfo[FvIndex].Flag & HASHED_FV_FLAG_VERIFIED_BOOT) != 0) {
FvHashValue += AlgInfo->HashSize;
}
//
// Use memory copy of the FV from now on.
//
FvInfo[FvIndex].Base = (UINT64)(UINTN)FvBuffer;
}
//
// Check final hash for all FVs.
//
if (FvHashValue == HashValue ||
(AlgInfo->HashAll (HashValue, FvHashValue - HashValue, FvHashValue) &&
CompareMem (HashInfo->Hash, FvHashValue, AlgInfo->HashSize) == 0)) {
Status = EFI_SUCCESS;
} else {
Status = EFI_VOLUME_CORRUPTED;
}
Done:
FreePool (HashValue);
return Status;
}
/**
Report FV to PEI and/or DXE core for dispatch.
@param[in] FvInfo Information of a FV.
**/
STATIC
VOID
ReportHashedFv (
IN HASHED_FV_INFO *FvInfo
)
{
CONST EFI_GUID *FvFormat;
if ((FvInfo->Flag & HASHED_FV_FLAG_REPORT_FV_HOB) != 0) {
//
// Require DXE core to process this FV.
//
BuildFvHob (
(EFI_PHYSICAL_ADDRESS)FvInfo->Base,
FvInfo->Length
);
DEBUG ((DEBUG_INFO, "Reported FV HOB: %016lX (%08lX)\r\n", FvInfo->Base, FvInfo->Length));
}
if ((FvInfo->Flag & HASHED_FV_FLAG_REPORT_FV_INFO_PPI) != 0) {
//
// Require PEI core to process this FV.
//
FvFormat = &((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)FvInfo->Base)->FileSystemGuid;
PeiServicesInstallFvInfoPpi (
FvFormat,
(VOID *)(UINTN)FvInfo->Base,
(UINT32)FvInfo->Length,
NULL,
NULL
);
DEBUG ((DEBUG_INFO, "Reported FV PPI: %016lX (%08lX)\r\n", FvInfo->Base, FvInfo->Length));
}
}
/**
Verify and report pre-hashed FVs.
Doing this must be at post-memory to make sure there's enough memory to hold
all FVs to be verified. This is necessary for mitigating TOCTOU issue.
This function will never return if the verification is failed.
@param[in] StoredHashFvPpi Pointer to PPI containing hash information.
@param[in] BootMode Current boot mode.
@retval Pointer to structure containing valid hash information for current boot mode.
@retval NULL if there's no hash associated with current boot mode.
**/
STATIC
FV_HASH_INFO *
GetHashInfo (
IN EDKII_PEI_FIRMWARE_VOLUME_INFO_STORED_HASH_FV_PPI *StoredHashFvPpi,
IN EFI_BOOT_MODE BootMode
)
{
FV_HASH_INFO *HashInfo;
if ((StoredHashFvPpi->HashInfo.HashFlag & FV_HASH_FLAG_BOOT_MODE (BootMode)) != 0) {
HashInfo = &StoredHashFvPpi->HashInfo;
} else {
HashInfo = NULL;
}
return HashInfo;
}
/**
Verify and report pre-hashed FVs.
Doing this must be at post-memory to make sure there's enough memory to hold
all FVs to be verified. This is necessary for mitigating TOCTOU issue.
This function will never return if the verification is failed.
@param[in] PeiServices General purpose services available to every PEIM.
@param[in] BootMode Current boot mode.
@retval EFI_SUCCESS The function completed successfully.
**/
STATIC
EFI_STATUS
CheckStoredHashFv (
IN CONST EFI_PEI_SERVICES **PeiServices,
IN EFI_BOOT_MODE BootMode
)
{
EFI_STATUS Status;
EDKII_PEI_FIRMWARE_VOLUME_INFO_STORED_HASH_FV_PPI *StoredHashFvPpi;
FV_HASH_INFO *HashInfo;
UINTN FvIndex;
//
// Check pre-hashed FV list
//
StoredHashFvPpi = NULL;
Status = PeiServicesLocatePpi (
&gEdkiiPeiFirmwareVolumeInfoStoredHashFvPpiGuid,
0,
NULL,
(VOID**)&StoredHashFvPpi
);
if (!EFI_ERROR(Status) && StoredHashFvPpi != NULL && StoredHashFvPpi->FvNumber > 0) {
HashInfo = GetHashInfo (StoredHashFvPpi, BootMode);
Status = VerifyHashedFv (HashInfo, StoredHashFvPpi->FvInfo,
StoredHashFvPpi->FvNumber, BootMode);
if (!EFI_ERROR (Status)) {
DEBUG ((DEBUG_INFO, "OBB verification passed (%r)\r\n", Status));
//
// Report the FVs to PEI core and/or DXE core.
//
for (FvIndex = 0; FvIndex < StoredHashFvPpi->FvNumber; ++FvIndex) {
if ((StoredHashFvPpi->FvInfo[FvIndex].Flag
& HASHED_FV_FLAG_SKIP_BOOT_MODE (BootMode)) == 0) {
ReportHashedFv (&StoredHashFvPpi->FvInfo[FvIndex]);
}
}
REPORT_STATUS_CODE (
EFI_PROGRESS_CODE,
PcdGet32 (PcdStatusCodeFvVerificationPass)
);
} else {
DEBUG ((DEBUG_ERROR, "ERROR: Failed to verify OBB FVs (%r)\r\n", Status));
REPORT_STATUS_CODE_EX (
EFI_PROGRESS_CODE,
PcdGet32 (PcdStatusCodeFvVerificationFail),
0,
NULL,
&gEdkiiPeiFirmwareVolumeInfoStoredHashFvPpiGuid,
StoredHashFvPpi,
sizeof (*StoredHashFvPpi)
);
ASSERT_EFI_ERROR (Status);
}
} else {
DEBUG ((DEBUG_ERROR, "ERROR: No/invalid StoredHashFvPpi located\r\n"));
ASSERT_EFI_ERROR (Status);
ASSERT (StoredHashFvPpi != NULL && StoredHashFvPpi->FvNumber > 0);
Status = EFI_NOT_FOUND;
}
return Status;
}
/**
Main entry for FvReport PEIM.
@param[in] FileHandle Handle of the file being invoked.
@param[in] PeiServices Pointer to PEI Services table.
@retval EFI_SUCCESS If all FVs reported by StoredHashFvPpi are verified.
**/
EFI_STATUS
EFIAPI
FvReportEntryPoint (
IN EFI_PEI_FILE_HANDLE FileHandle,
IN CONST EFI_PEI_SERVICES **PeiServices
)
{
EFI_STATUS Status;
EFI_BOOT_MODE BootMode;
Status = PeiServicesGetBootMode (&BootMode);
ASSERT_EFI_ERROR (Status);
Status = CheckStoredHashFv (PeiServices, BootMode);
if (EFI_ERROR (Status)) {
//
// Never pass control to left part of BIOS if any error.
//
CpuDeadLoop ();
}
return Status;
}
|