TianoCore EDK2 master
Loading...
Searching...
No Matches
DxeSecurityManagementLib.c
Go to the documentation of this file.
1
9#include <PiDxe.h>
10#include <Protocol/LoadFile.h>
11#include <Library/DebugLib.h>
17
18#define SECURITY_HANDLER_TABLE_SIZE 0x10
19
20//
21// Secruity Operation on Image and none Image.
22//
23#define EFI_AUTH_IMAGE_OPERATION_MASK (EFI_AUTH_OPERATION_VERIFY_IMAGE \
24 | EFI_AUTH_OPERATION_DEFER_IMAGE_LOAD \
25 | EFI_AUTH_OPERATION_MEASURE_IMAGE)
26#define EFI_AUTH_NONE_IMAGE_OPERATION_MASK (EFI_AUTH_OPERATION_CONNECT_POLICY \
27 | EFI_AUTH_OPERATION_AUTHENTICATION_STATE)
28
29typedef struct {
30 UINT32 SecurityOperation;
33
34typedef struct {
35 UINT32 Security2Operation;
38
39UINT32 mCurrentAuthOperation = 0;
40UINT32 mNumberOfSecurityHandler = 0;
41UINT32 mMaxNumberOfSecurityHandler = 0;
42SECURITY_INFO *mSecurityTable = NULL;
43
44UINT32 mCurrentAuthOperation2 = 0;
45UINT32 mNumberOfSecurity2Handler = 0;
46UINT32 mMaxNumberOfSecurity2Handler = 0;
47SECURITY2_INFO *mSecurity2Table = NULL;
48
55RETURN_STATUS
56EFIAPI
58 VOID
59 )
60{
61 //
62 // Reallocate memory for security info structure.
63 //
64 mSecurityTable = ReallocatePool (
65 mMaxNumberOfSecurityHandler * sizeof (SECURITY_INFO),
66 (mMaxNumberOfSecurityHandler + SECURITY_HANDLER_TABLE_SIZE) * sizeof (SECURITY_INFO),
67 mSecurityTable
68 );
69
70 //
71 // No enough resource is allocated.
72 //
73 if (mSecurityTable == NULL) {
75 }
76
77 //
78 // Increase max handler number
79 //
80 mMaxNumberOfSecurityHandler = mMaxNumberOfSecurityHandler + SECURITY_HANDLER_TABLE_SIZE;
81 return RETURN_SUCCESS;
82}
83
94BOOLEAN
96 IN UINT32 CurrentAuthOperation,
97 IN UINT32 CheckAuthOperation
98 )
99{
100 //
101 // Make sure new auth operation can be recognized.
102 //
103 ASSERT ((CheckAuthOperation & ~(EFI_AUTH_IMAGE_OPERATION_MASK | EFI_AUTH_OPERATION_AUTHENTICATION_STATE | EFI_AUTH_OPERATION_IMAGE_REQUIRED)) == 0);
104
105 //
106 // When current operation includes measure image operation,
107 // only another measure image operation or none operation will be allowed.
108 //
109 if ((CurrentAuthOperation & EFI_AUTH_OPERATION_MEASURE_IMAGE) == EFI_AUTH_OPERATION_MEASURE_IMAGE) {
110 if (((CheckAuthOperation & EFI_AUTH_OPERATION_MEASURE_IMAGE) == EFI_AUTH_OPERATION_MEASURE_IMAGE) ||
111 ((CheckAuthOperation & EFI_AUTH_IMAGE_OPERATION_MASK) == EFI_AUTH_OPERATION_NONE))
112 {
113 return TRUE;
114 } else {
115 return FALSE;
116 }
117 }
118
119 //
120 // When current operation doesn't include measure image operation,
121 // any new operation will be allowed.
122 //
123 return TRUE;
124}
125
141EFIAPI
144 IN UINT32 AuthenticationOperation
145 )
146{
147 EFI_STATUS Status;
148
149 ASSERT (SecurityHandler != NULL);
150
151 //
152 // Make sure AuthenticationOperation is valid in the register order.
153 //
154 ASSERT (CheckAuthenticationOperation (mCurrentAuthOperation, AuthenticationOperation));
155 mCurrentAuthOperation = mCurrentAuthOperation | AuthenticationOperation;
156
157 //
158 // Check whether the handler lists is enough to store new handler.
159 //
160 if (mNumberOfSecurityHandler == mMaxNumberOfSecurityHandler) {
161 //
162 // Allocate more resources for new handler.
163 //
165 ASSERT_EFI_ERROR (Status);
166 }
167
168 //
169 // Register new handler into the handler list.
170 //
171 mSecurityTable[mNumberOfSecurityHandler].SecurityOperation = AuthenticationOperation;
172 mSecurityTable[mNumberOfSecurityHandler].SecurityHandler = SecurityHandler;
173 mNumberOfSecurityHandler++;
174
175 return EFI_SUCCESS;
176}
177
210EFIAPI
212 IN UINT32 AuthenticationStatus,
214 )
215{
216 UINT32 Index;
217 EFI_STATUS Status;
218 UINT32 HandlerAuthenticationStatus;
219 VOID *FileBuffer;
220 UINTN FileSize;
221 EFI_HANDLE Handle;
223 EFI_DEVICE_PATH_PROTOCOL *FilePathToVerfiy;
224
225 if (FilePath == NULL) {
226 return EFI_INVALID_PARAMETER;
227 }
228
229 //
230 // Directly return successfully when no handler is registered.
231 //
232 if (mNumberOfSecurityHandler == 0) {
233 return EFI_SUCCESS;
234 }
235
236 Status = EFI_SUCCESS;
237 FileBuffer = NULL;
238 FileSize = 0;
239 HandlerAuthenticationStatus = AuthenticationStatus;
240 FilePathToVerfiy = (EFI_DEVICE_PATH_PROTOCOL *)FilePath;
241 //
242 // Run security handler in same order to their registered list
243 //
244 for (Index = 0; Index < mNumberOfSecurityHandler; Index++) {
245 if ((mSecurityTable[Index].SecurityOperation & EFI_AUTH_OPERATION_IMAGE_REQUIRED) == EFI_AUTH_OPERATION_IMAGE_REQUIRED) {
246 //
247 // Try get file buffer when the handler requires image buffer.
248 //
249 if (FileBuffer == NULL) {
250 Node = FilePathToVerfiy;
251 Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &Node, &Handle);
252 //
253 // Try to get image by FALSE boot policy for the exact boot file path.
254 //
255 FileBuffer = GetFileBufferByFilePath (FALSE, FilePath, &FileSize, &AuthenticationStatus);
256 if (FileBuffer == NULL) {
257 //
258 // Try to get image by TRUE boot policy for the inexact boot file path.
259 //
260 FileBuffer = GetFileBufferByFilePath (TRUE, FilePath, &FileSize, &AuthenticationStatus);
261 }
262
263 if ((FileBuffer != NULL) && (!EFI_ERROR (Status))) {
264 //
265 // LoadFile () may cause the device path of the Handle be updated.
266 //
267 FilePathToVerfiy = AppendDevicePath (DevicePathFromHandle (Handle), Node);
268 }
269 }
270 }
271
272 Status = mSecurityTable[Index].SecurityHandler (
273 HandlerAuthenticationStatus,
274 FilePathToVerfiy,
275 FileBuffer,
276 FileSize
277 );
278 if (EFI_ERROR (Status)) {
279 break;
280 }
281 }
282
283 if (FileBuffer != NULL) {
284 FreePool (FileBuffer);
285 }
286
287 if (FilePathToVerfiy != FilePath) {
288 FreePool (FilePathToVerfiy);
289 }
290
291 return Status;
292}
293
300RETURN_STATUS
301EFIAPI
303 VOID
304 )
305{
306 //
307 // Reallocate memory for security info structure.
308 //
309 mSecurity2Table = ReallocatePool (
310 mMaxNumberOfSecurity2Handler * sizeof (SECURITY2_INFO),
311 (mMaxNumberOfSecurity2Handler + SECURITY_HANDLER_TABLE_SIZE) * sizeof (SECURITY2_INFO),
312 mSecurity2Table
313 );
314
315 //
316 // No enough resource is allocated.
317 //
318 if (mSecurity2Table == NULL) {
320 }
321
322 //
323 // Increase max handler number
324 //
325 mMaxNumberOfSecurity2Handler = mMaxNumberOfSecurity2Handler + SECURITY_HANDLER_TABLE_SIZE;
326 return RETURN_SUCCESS;
327}
328
344BOOLEAN
346 IN UINT32 CurrentAuthOperation,
347 IN UINT32 CheckAuthOperation
348 )
349{
350 //
351 // Make sure new auth operation can be recognized.
352 //
353 if (CheckAuthOperation == EFI_AUTH_OPERATION_NONE) {
354 return FALSE;
355 }
356
357 if ((CheckAuthOperation & ~(EFI_AUTH_IMAGE_OPERATION_MASK |
358 EFI_AUTH_NONE_IMAGE_OPERATION_MASK |
360 {
361 return FALSE;
362 }
363
364 //
365 // When current operation includes measure image operation,
366 // only another measure image or none image operation will be allowed.
367 //
368 if ((CurrentAuthOperation & EFI_AUTH_OPERATION_MEASURE_IMAGE) == EFI_AUTH_OPERATION_MEASURE_IMAGE) {
369 if (((CheckAuthOperation & EFI_AUTH_OPERATION_MEASURE_IMAGE) == EFI_AUTH_OPERATION_MEASURE_IMAGE) ||
370 ((CheckAuthOperation & EFI_AUTH_IMAGE_OPERATION_MASK) == 0))
371 {
372 return TRUE;
373 } else {
374 return FALSE;
375 }
376 }
377
378 //
379 // Any other operation will be allowed.
380 //
381 return TRUE;
382}
383
400EFIAPI
403 IN UINT32 AuthenticationOperation
404 )
405{
406 EFI_STATUS Status;
407
408 ASSERT (Security2Handler != NULL);
409
410 //
411 // Make sure AuthenticationOperation is valid in the register order.
412 //
413 ASSERT (CheckAuthentication2Operation (mCurrentAuthOperation2, AuthenticationOperation));
414 mCurrentAuthOperation2 = mCurrentAuthOperation2 | AuthenticationOperation;
415
416 //
417 // Check whether the handler lists is enough to store new handler.
418 //
419 if (mNumberOfSecurity2Handler == mMaxNumberOfSecurity2Handler) {
420 //
421 // Allocate more resources for new handler.
422 //
424 ASSERT_EFI_ERROR (Status);
425 }
426
427 //
428 // Register new handler into the handler list.
429 //
430 mSecurity2Table[mNumberOfSecurity2Handler].Security2Operation = AuthenticationOperation;
431 mSecurity2Table[mNumberOfSecurity2Handler].Security2Handler = Security2Handler;
432 mNumberOfSecurity2Handler++;
433
434 return EFI_SUCCESS;
435}
436
479EFIAPI
481 IN UINT32 AuthenticationOperation,
482 IN UINT32 AuthenticationStatus,
483 IN CONST EFI_DEVICE_PATH_PROTOCOL *File OPTIONAL,
484 IN VOID *FileBuffer,
485 IN UINTN FileSize,
486 IN BOOLEAN BootPolicy
487 )
488{
489 UINT32 Index;
490 EFI_STATUS Status;
491
492 //
493 // Invalid case if File and FileBuffer are both NULL.
494 //
495 if ((File == NULL) && (FileBuffer == NULL)) {
496 return EFI_INVALID_PARAMETER;
497 }
498
499 //
500 // Directly return successfully when no handler is registered.
501 //
502 if (mNumberOfSecurity2Handler == 0) {
503 return EFI_SUCCESS;
504 }
505
506 //
507 // Run security handler in same order to their registered list
508 //
509 for (Index = 0; Index < mNumberOfSecurity2Handler; Index++) {
510 //
511 // If FileBuffer is not NULL, the input is Image, which will be handled by EFI_AUTH_IMAGE_OPERATION_MASK operation.
512 // If FileBuffer is NULL, the input is not Image, which will be handled by EFI_AUTH_NONE_IMAGE_OPERATION_MASK operation.
513 // Other cases are ignored.
514 //
515 if (((FileBuffer != NULL) && ((mSecurity2Table[Index].Security2Operation & EFI_AUTH_IMAGE_OPERATION_MASK) != 0)) ||
516 ((FileBuffer == NULL) && ((mSecurity2Table[Index].Security2Operation & EFI_AUTH_NONE_IMAGE_OPERATION_MASK) != 0)))
517 {
518 //
519 // Execute registered handlers based on input AuthenticationOperation
520 //
521 if ((mSecurity2Table[Index].Security2Operation & AuthenticationOperation) != 0) {
522 Status = mSecurity2Table[Index].Security2Handler (
523 AuthenticationStatus,
524 File,
525 FileBuffer,
526 FileSize,
527 BootPolicy
528 );
529 if (EFI_ERROR (Status)) {
530 return Status;
531 }
532 }
533 }
534 }
535
536 return EFI_SUCCESS;
537}
UINT64 UINTN
EFI_DEVICE_PATH_PROTOCOL *EFIAPI DevicePathFromHandle(IN EFI_HANDLE Handle)
EFI_DEVICE_PATH_PROTOCOL *EFIAPI AppendDevicePath(IN CONST EFI_DEVICE_PATH_PROTOCOL *FirstDevicePath OPTIONAL, IN CONST EFI_DEVICE_PATH_PROTOCOL *SecondDevicePath OPTIONAL)
EFI_STATUS EFIAPI ExecuteSecurity2Handlers(IN UINT32 AuthenticationOperation, IN UINT32 AuthenticationStatus, IN CONST EFI_DEVICE_PATH_PROTOCOL *File OPTIONAL, IN VOID *FileBuffer, IN UINTN FileSize, IN BOOLEAN BootPolicy)
RETURN_STATUS EFIAPI ReallocateSecurity2HandlerTable(VOID)
RETURN_STATUS EFIAPI ReallocateSecurityHandlerTable(VOID)
EFI_STATUS EFIAPI RegisterSecurityHandler(IN SECURITY_FILE_AUTHENTICATION_STATE_HANDLER SecurityHandler, IN UINT32 AuthenticationOperation)
BOOLEAN CheckAuthentication2Operation(IN UINT32 CurrentAuthOperation, IN UINT32 CheckAuthOperation)
BOOLEAN CheckAuthenticationOperation(IN UINT32 CurrentAuthOperation, IN UINT32 CheckAuthOperation)
EFI_STATUS EFIAPI RegisterSecurity2Handler(IN SECURITY2_FILE_AUTHENTICATION_HANDLER Security2Handler, IN UINT32 AuthenticationOperation)
EFI_STATUS EFIAPI ExecuteSecurityHandlers(IN UINT32 AuthenticationStatus, IN CONST EFI_DEVICE_PATH_PROTOCOL *FilePath)
VOID *EFIAPI GetFileBufferByFilePath(IN BOOLEAN BootPolicy, IN CONST EFI_DEVICE_PATH_PROTOCOL *FilePath, OUT UINTN *FileSize, OUT UINT32 *AuthenticationStatus)
VOID *EFIAPI ReallocatePool(IN UINTN OldSize, IN UINTN NewSize, IN VOID *OldBuffer OPTIONAL)
VOID EFIAPI FreePool(IN VOID *Buffer)
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define RETURN_OUT_OF_RESOURCES
Definition: Base.h:1114
#define RETURN_SUCCESS
Definition: Base.h:1066
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define ASSERT_EFI_ERROR(StatusParameter)
Definition: DebugLib.h:462
#define EFI_AUTH_OPERATION_IMAGE_REQUIRED
EFI_STATUS(EFIAPI * SECURITY2_FILE_AUTHENTICATION_HANDLER)(IN UINT32 AuthenticationStatus, IN CONST EFI_DEVICE_PATH_PROTOCOL *File, IN VOID *FileBuffer, IN UINTN FileSize, IN BOOLEAN BootPolicy)
EFI_STATUS(EFIAPI * SECURITY_FILE_AUTHENTICATION_STATE_HANDLER)(IN OUT UINT32 AuthenticationStatus, IN CONST EFI_DEVICE_PATH_PROTOCOL *File, IN VOID *FileBuffer, IN UINTN FileSize)
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
VOID * EFI_HANDLE
Definition: UefiBaseType.h:33
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
EFI_BOOT_SERVICES * gBS