]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Library/DxeSecurityManagementLib/DxeSecurityManagementLib.c
MdeModulePkg/SecurityManagementLib: Mark the File parameter as OPTIONAL
[mirror_edk2.git] / MdeModulePkg / Library / DxeSecurityManagementLib / DxeSecurityManagementLib.c
... / ...
CommitLineData
1/** @file\r
2 Provides generic security measurement functions for DXE module.\r
3\r
4Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>\r
5SPDX-License-Identifier: BSD-2-Clause-Patent\r
6\r
7**/\r
8\r
9#include <PiDxe.h>\r
10#include <Protocol/LoadFile.h>\r
11#include <Library/DebugLib.h>\r
12#include <Library/DxeServicesLib.h>\r
13#include <Library/MemoryAllocationLib.h>\r
14#include <Library/SecurityManagementLib.h>\r
15#include <Library/DevicePathLib.h>\r
16#include <Library/UefiBootServicesTableLib.h>\r
17\r
18#define SECURITY_HANDLER_TABLE_SIZE 0x10\r
19\r
20//\r
21// Secruity Operation on Image and none Image.\r
22//\r
23#define EFI_AUTH_IMAGE_OPERATION_MASK (EFI_AUTH_OPERATION_VERIFY_IMAGE \\r
24 | EFI_AUTH_OPERATION_DEFER_IMAGE_LOAD \\r
25 | EFI_AUTH_OPERATION_MEASURE_IMAGE)\r
26#define EFI_AUTH_NONE_IMAGE_OPERATION_MASK (EFI_AUTH_OPERATION_CONNECT_POLICY \\r
27 | EFI_AUTH_OPERATION_AUTHENTICATION_STATE)\r
28\r
29typedef struct {\r
30 UINT32 SecurityOperation;\r
31 SECURITY_FILE_AUTHENTICATION_STATE_HANDLER SecurityHandler;\r
32} SECURITY_INFO;\r
33\r
34typedef struct {\r
35 UINT32 Security2Operation;\r
36 SECURITY2_FILE_AUTHENTICATION_HANDLER Security2Handler;\r
37} SECURITY2_INFO;\r
38\r
39UINT32 mCurrentAuthOperation = 0;\r
40UINT32 mNumberOfSecurityHandler = 0;\r
41UINT32 mMaxNumberOfSecurityHandler = 0;\r
42SECURITY_INFO *mSecurityTable = NULL;\r
43\r
44UINT32 mCurrentAuthOperation2 = 0;\r
45UINT32 mNumberOfSecurity2Handler = 0;\r
46UINT32 mMaxNumberOfSecurity2Handler = 0;\r
47SECURITY2_INFO *mSecurity2Table = NULL;\r
48\r
49/**\r
50 Reallocates more global memory to store the registered Handler list.\r
51\r
52 @retval RETURN_SUCCESS Reallocate memory successfully.\r
53 @retval RETURN_OUT_OF_RESOURCES No enough memory to allocated.\r
54**/\r
55RETURN_STATUS\r
56EFIAPI\r
57ReallocateSecurityHandlerTable (\r
58 VOID\r
59 )\r
60{\r
61 //\r
62 // Reallocate memory for security info structure.\r
63 //\r
64 mSecurityTable = ReallocatePool (\r
65 mMaxNumberOfSecurityHandler * sizeof (SECURITY_INFO),\r
66 (mMaxNumberOfSecurityHandler + SECURITY_HANDLER_TABLE_SIZE) * sizeof (SECURITY_INFO),\r
67 mSecurityTable\r
68 );\r
69\r
70 //\r
71 // No enough resource is allocated.\r
72 //\r
73 if (mSecurityTable == NULL) {\r
74 return RETURN_OUT_OF_RESOURCES;\r
75 }\r
76\r
77 //\r
78 // Increase max handler number\r
79 //\r
80 mMaxNumberOfSecurityHandler = mMaxNumberOfSecurityHandler + SECURITY_HANDLER_TABLE_SIZE;\r
81 return RETURN_SUCCESS;\r
82}\r
83\r
84/**\r
85 Check whether an operation is valid according to the requirement of current operation,\r
86 which must make sure that the measure image operation is the last one.\r
87\r
88 @param CurrentAuthOperation Current operation.\r
89 @param CheckAuthOperation Operation to be checked.\r
90\r
91 @retval TRUE Operation is valid for current operation.\r
92 @retval FALSE Operation is invalid for current operation.\r
93**/\r
94BOOLEAN\r
95CheckAuthenticationOperation (\r
96 IN UINT32 CurrentAuthOperation,\r
97 IN UINT32 CheckAuthOperation\r
98 )\r
99{\r
100 //\r
101 // Make sure new auth operation can be recognized.\r
102 //\r
103 ASSERT ((CheckAuthOperation & ~(EFI_AUTH_IMAGE_OPERATION_MASK | EFI_AUTH_OPERATION_AUTHENTICATION_STATE | EFI_AUTH_OPERATION_IMAGE_REQUIRED)) == 0);\r
104\r
105 //\r
106 // When current operation includes measure image operation,\r
107 // only another measure image operation or none operation will be allowed.\r
108 //\r
109 if ((CurrentAuthOperation & EFI_AUTH_OPERATION_MEASURE_IMAGE) == EFI_AUTH_OPERATION_MEASURE_IMAGE) {\r
110 if (((CheckAuthOperation & EFI_AUTH_OPERATION_MEASURE_IMAGE) == EFI_AUTH_OPERATION_MEASURE_IMAGE) ||\r
111 ((CheckAuthOperation & EFI_AUTH_IMAGE_OPERATION_MASK) == EFI_AUTH_OPERATION_NONE)) {\r
112 return TRUE;\r
113 } else {\r
114 return FALSE;\r
115 }\r
116 }\r
117\r
118 //\r
119 // When current operation doesn't include measure image operation,\r
120 // any new operation will be allowed.\r
121 //\r
122 return TRUE;\r
123}\r
124\r
125/**\r
126 Register security measurement handler with its operation type. The different\r
127 handler with the same operation can all be registered.\r
128\r
129 If SecurityHandler is NULL, then ASSERT().\r
130 If no enough resources available to register new handler, then ASSERT().\r
131 If AuthenticationOperation is not recongnized, then ASSERT().\r
132 If the previous register handler can't be executed before the later register handler, then ASSERT().\r
133\r
134 @param[in] SecurityHandler Security measurement service handler to be registered.\r
135 @param[in] AuthenticationOperation Operation type is specified for the registered handler.\r
136\r
137 @retval EFI_SUCCESS The handlers were registered successfully.\r
138**/\r
139EFI_STATUS\r
140EFIAPI\r
141RegisterSecurityHandler (\r
142 IN SECURITY_FILE_AUTHENTICATION_STATE_HANDLER SecurityHandler,\r
143 IN UINT32 AuthenticationOperation\r
144 )\r
145{\r
146 EFI_STATUS Status;\r
147\r
148 ASSERT (SecurityHandler != NULL);\r
149\r
150 //\r
151 // Make sure AuthenticationOperation is valid in the register order.\r
152 //\r
153 ASSERT (CheckAuthenticationOperation (mCurrentAuthOperation, AuthenticationOperation));\r
154 mCurrentAuthOperation = mCurrentAuthOperation | AuthenticationOperation;\r
155\r
156 //\r
157 // Check whether the handler lists is enough to store new handler.\r
158 //\r
159 if (mNumberOfSecurityHandler == mMaxNumberOfSecurityHandler) {\r
160 //\r
161 // Allocate more resources for new handler.\r
162 //\r
163 Status = ReallocateSecurityHandlerTable();\r
164 ASSERT_EFI_ERROR (Status);\r
165 }\r
166\r
167 //\r
168 // Register new handler into the handler list.\r
169 //\r
170 mSecurityTable[mNumberOfSecurityHandler].SecurityOperation = AuthenticationOperation;\r
171 mSecurityTable[mNumberOfSecurityHandler].SecurityHandler = SecurityHandler;\r
172 mNumberOfSecurityHandler ++;\r
173\r
174 return EFI_SUCCESS;\r
175}\r
176\r
177/**\r
178 Execute registered handlers until one returns an error and that error is returned.\r
179 If none of the handlers return an error, then EFI_SUCCESS is returned.\r
180\r
181 Before exectue handler, get the image buffer by file device path if a handler\r
182 requires the image file. And return the image buffer to each handler when exectue handler.\r
183\r
184 The handlers are executed in same order to their registered order.\r
185\r
186 @param[in] AuthenticationStatus\r
187 This is the authentication type returned from the Section\r
188 Extraction protocol. See the Section Extraction Protocol\r
189 Specification for details on this type.\r
190 @param[in] FilePath This is a pointer to the device path of the file that is\r
191 being dispatched. This will optionally be used for logging.\r
192\r
193 @retval EFI_SUCCESS The file specified by File did authenticate when more\r
194 than one security handler services were registered,\r
195 or the file did not authenticate when no security\r
196 handler service was registered. And the platform policy\r
197 dictates that the DXE Core may use File.\r
198 @retval EFI_INVALID_PARAMETER File is NULL.\r
199 @retval EFI_SECURITY_VIOLATION The file specified by File did not authenticate, and\r
200 the platform policy dictates that File should be placed\r
201 in the untrusted state. A file may be promoted from\r
202 the untrusted to the trusted state at a future time\r
203 with a call to the Trust() DXE Service.\r
204 @retval EFI_ACCESS_DENIED The file specified by File did not authenticate, and\r
205 the platform policy dictates that File should not be\r
206 used for any purpose.\r
207**/\r
208EFI_STATUS\r
209EFIAPI\r
210ExecuteSecurityHandlers (\r
211 IN UINT32 AuthenticationStatus,\r
212 IN CONST EFI_DEVICE_PATH_PROTOCOL *FilePath\r
213 )\r
214{\r
215 UINT32 Index;\r
216 EFI_STATUS Status;\r
217 UINT32 HandlerAuthenticationStatus;\r
218 VOID *FileBuffer;\r
219 UINTN FileSize;\r
220 EFI_HANDLE Handle;\r
221 EFI_DEVICE_PATH_PROTOCOL *Node;\r
222 EFI_DEVICE_PATH_PROTOCOL *FilePathToVerfiy;\r
223\r
224 if (FilePath == NULL) {\r
225 return EFI_INVALID_PARAMETER;\r
226 }\r
227\r
228 //\r
229 // Directly return successfully when no handler is registered.\r
230 //\r
231 if (mNumberOfSecurityHandler == 0) {\r
232 return EFI_SUCCESS;\r
233 }\r
234\r
235 Status = EFI_SUCCESS;\r
236 FileBuffer = NULL;\r
237 FileSize = 0;\r
238 HandlerAuthenticationStatus = AuthenticationStatus;\r
239 FilePathToVerfiy = (EFI_DEVICE_PATH_PROTOCOL *) FilePath;\r
240 //\r
241 // Run security handler in same order to their registered list\r
242 //\r
243 for (Index = 0; Index < mNumberOfSecurityHandler; Index ++) {\r
244 if ((mSecurityTable[Index].SecurityOperation & EFI_AUTH_OPERATION_IMAGE_REQUIRED) == EFI_AUTH_OPERATION_IMAGE_REQUIRED) {\r
245 //\r
246 // Try get file buffer when the handler requires image buffer.\r
247 //\r
248 if (FileBuffer == NULL) {\r
249 Node = FilePathToVerfiy;\r
250 Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &Node, &Handle);\r
251 //\r
252 // Try to get image by FALSE boot policy for the exact boot file path.\r
253 //\r
254 FileBuffer = GetFileBufferByFilePath (FALSE, FilePath, &FileSize, &AuthenticationStatus);\r
255 if (FileBuffer == NULL) {\r
256 //\r
257 // Try to get image by TRUE boot policy for the inexact boot file path.\r
258 //\r
259 FileBuffer = GetFileBufferByFilePath (TRUE, FilePath, &FileSize, &AuthenticationStatus);\r
260 }\r
261 if ((FileBuffer != NULL) && (!EFI_ERROR (Status))) {\r
262 //\r
263 // LoadFile () may cause the device path of the Handle be updated.\r
264 //\r
265 FilePathToVerfiy = AppendDevicePath (DevicePathFromHandle (Handle), Node);\r
266 }\r
267 }\r
268 }\r
269 Status = mSecurityTable[Index].SecurityHandler (\r
270 HandlerAuthenticationStatus,\r
271 FilePathToVerfiy,\r
272 FileBuffer,\r
273 FileSize\r
274 );\r
275 if (EFI_ERROR (Status)) {\r
276 break;\r
277 }\r
278 }\r
279\r
280 if (FileBuffer != NULL) {\r
281 FreePool (FileBuffer);\r
282 }\r
283 if (FilePathToVerfiy != FilePath) {\r
284 FreePool (FilePathToVerfiy);\r
285 }\r
286\r
287 return Status;\r
288}\r
289\r
290/**\r
291 Reallocates more global memory to store the registered Securit2Handler list.\r
292\r
293 @retval RETURN_SUCCESS Reallocate memory successfully.\r
294 @retval RETURN_OUT_OF_RESOURCES No enough memory to allocated.\r
295**/\r
296RETURN_STATUS\r
297EFIAPI\r
298ReallocateSecurity2HandlerTable (\r
299 VOID\r
300 )\r
301{\r
302 //\r
303 // Reallocate memory for security info structure.\r
304 //\r
305 mSecurity2Table = ReallocatePool (\r
306 mMaxNumberOfSecurity2Handler * sizeof (SECURITY2_INFO),\r
307 (mMaxNumberOfSecurity2Handler + SECURITY_HANDLER_TABLE_SIZE) * sizeof (SECURITY2_INFO),\r
308 mSecurity2Table\r
309 );\r
310\r
311 //\r
312 // No enough resource is allocated.\r
313 //\r
314 if (mSecurity2Table == NULL) {\r
315 return RETURN_OUT_OF_RESOURCES;\r
316 }\r
317\r
318 //\r
319 // Increase max handler number\r
320 //\r
321 mMaxNumberOfSecurity2Handler = mMaxNumberOfSecurity2Handler + SECURITY_HANDLER_TABLE_SIZE;\r
322 return RETURN_SUCCESS;\r
323}\r
324\r
325/**\r
326 Check whether an operation is valid according to the requirement of current operation,\r
327 which must make sure that the measure image operation is the last one.\r
328\r
329 If AuthenticationOperation is not recongnized, return FALSE.\r
330 If AuthenticationOperation is EFI_AUTH_OPERATION_NONE, return FALSE.\r
331 If AuthenticationOperation includes security operation and authentication operation, return FALSE.\r
332 If the previous register handler can't be executed before the later register handler, return FALSE.\r
333\r
334 @param CurrentAuthOperation Current operation.\r
335 @param CheckAuthOperation Operation to be checked.\r
336\r
337 @retval TRUE Operation is valid for current operation.\r
338 @retval FALSE Operation is invalid for current operation.\r
339**/\r
340BOOLEAN\r
341CheckAuthentication2Operation (\r
342 IN UINT32 CurrentAuthOperation,\r
343 IN UINT32 CheckAuthOperation\r
344 )\r
345{\r
346 //\r
347 // Make sure new auth operation can be recognized.\r
348 //\r
349 if (CheckAuthOperation == EFI_AUTH_OPERATION_NONE) {\r
350 return FALSE;\r
351 }\r
352 if ((CheckAuthOperation & ~(EFI_AUTH_IMAGE_OPERATION_MASK |\r
353 EFI_AUTH_NONE_IMAGE_OPERATION_MASK |\r
354 EFI_AUTH_OPERATION_IMAGE_REQUIRED)) != 0) {\r
355 return FALSE;\r
356 }\r
357\r
358 //\r
359 // When current operation includes measure image operation,\r
360 // only another measure image or none image operation will be allowed.\r
361 //\r
362 if ((CurrentAuthOperation & EFI_AUTH_OPERATION_MEASURE_IMAGE) == EFI_AUTH_OPERATION_MEASURE_IMAGE) {\r
363 if (((CheckAuthOperation & EFI_AUTH_OPERATION_MEASURE_IMAGE) == EFI_AUTH_OPERATION_MEASURE_IMAGE) ||\r
364 ((CheckAuthOperation & EFI_AUTH_IMAGE_OPERATION_MASK) == 0)) {\r
365 return TRUE;\r
366 } else {\r
367 return FALSE;\r
368 }\r
369 }\r
370\r
371 //\r
372 // Any other operation will be allowed.\r
373 //\r
374 return TRUE;\r
375}\r
376\r
377/**\r
378 Register security measurement handler with its operation type. Different\r
379 handlers with the same operation can all be registered.\r
380\r
381 If Security2Handler is NULL, then ASSERT().\r
382 If no enough resources available to register new handler, then ASSERT().\r
383 If AuthenticationOperation is not recongnized, then ASSERT().\r
384 If AuthenticationOperation is EFI_AUTH_OPERATION_NONE, then ASSERT().\r
385 If the previous register handler can't be executed before the later register handler, then ASSERT().\r
386\r
387 @param[in] Security2Handler The security measurement service handler to be registered.\r
388 @param[in] AuthenticationOperation The operation type is specified for the registered handler.\r
389\r
390 @retval EFI_SUCCESS The handlers were registered successfully.\r
391**/\r
392EFI_STATUS\r
393EFIAPI\r
394RegisterSecurity2Handler (\r
395 IN SECURITY2_FILE_AUTHENTICATION_HANDLER Security2Handler,\r
396 IN UINT32 AuthenticationOperation\r
397 )\r
398{\r
399 EFI_STATUS Status;\r
400\r
401 ASSERT (Security2Handler != NULL);\r
402\r
403 //\r
404 // Make sure AuthenticationOperation is valid in the register order.\r
405 //\r
406 ASSERT (CheckAuthentication2Operation (mCurrentAuthOperation2, AuthenticationOperation));\r
407 mCurrentAuthOperation2 = mCurrentAuthOperation2 | AuthenticationOperation;\r
408\r
409 //\r
410 // Check whether the handler lists is enough to store new handler.\r
411 //\r
412 if (mNumberOfSecurity2Handler == mMaxNumberOfSecurity2Handler) {\r
413 //\r
414 // Allocate more resources for new handler.\r
415 //\r
416 Status = ReallocateSecurity2HandlerTable();\r
417 ASSERT_EFI_ERROR (Status);\r
418 }\r
419\r
420 //\r
421 // Register new handler into the handler list.\r
422 //\r
423 mSecurity2Table[mNumberOfSecurity2Handler].Security2Operation = AuthenticationOperation;\r
424 mSecurity2Table[mNumberOfSecurity2Handler].Security2Handler = Security2Handler;\r
425 mNumberOfSecurity2Handler ++;\r
426\r
427 return EFI_SUCCESS;\r
428}\r
429\r
430/**\r
431 Execute registered handlers based on input AuthenticationOperation until\r
432 one returns an error and that error is returned.\r
433\r
434 If none of the handlers return an error, then EFI_SUCCESS is returned.\r
435 The handlers those satisfy AuthenticationOperation will only be executed.\r
436 The handlers are executed in same order to their registered order.\r
437\r
438 @param[in] AuthenticationOperation\r
439 The operation type specifies which handlers will be executed.\r
440 @param[in] AuthenticationStatus\r
441 The authentication status for the input file.\r
442 @param[in] File This is a pointer to the device path of the file that is\r
443 being dispatched. This will optionally be used for logging.\r
444 @param[in] FileBuffer A pointer to the buffer with the UEFI file image\r
445 @param[in] FileSize The size of File buffer.\r
446 @param[in] BootPolicy A boot policy that was used to call LoadImage() UEFI service.\r
447\r
448 @retval EFI_SUCCESS The file specified by DevicePath and non-NULL\r
449 FileBuffer did authenticate, and the platform policy dictates\r
450 that the DXE Foundation may use the file.\r
451 @retval EFI_SUCCESS The device path specified by NULL device path DevicePath\r
452 and non-NULL FileBuffer did authenticate, and the platform\r
453 policy dictates that the DXE Foundation may execute the image in\r
454 FileBuffer.\r
455 @retval EFI_SUCCESS FileBuffer is NULL and current user has permission to start\r
456 UEFI device drivers on the device path specified by DevicePath.\r
457 @retval EFI_SECURITY_VIOLATION The file specified by File or FileBuffer did not\r
458 authenticate, and the platform policy dictates that\r
459 the file should be placed in the untrusted state.\r
460 @retval EFI_SECURITY_VIOLATION FileBuffer FileBuffer is NULL and the user has no\r
461 permission to start UEFI device drivers on the device path specified\r
462 by DevicePath.\r
463 @retval EFI_SECURITY_VIOLATION FileBuffer is not NULL and the user has no permission to load\r
464 drivers from the device path specified by DevicePath. The\r
465 image has been added into the list of the deferred images.\r
466 @retval EFI_ACCESS_DENIED The file specified by File did not authenticate, and\r
467 the platform policy dictates that the DXE\r
468 Foundation may not use File.\r
469 @retval EFI_INVALID_PARAMETER File and FileBuffer are both NULL.\r
470**/\r
471EFI_STATUS\r
472EFIAPI\r
473ExecuteSecurity2Handlers (\r
474 IN UINT32 AuthenticationOperation,\r
475 IN UINT32 AuthenticationStatus,\r
476 IN CONST EFI_DEVICE_PATH_PROTOCOL *File, OPTIONAL\r
477 IN VOID *FileBuffer,\r
478 IN UINTN FileSize,\r
479 IN BOOLEAN BootPolicy\r
480 )\r
481{\r
482 UINT32 Index;\r
483 EFI_STATUS Status;\r
484\r
485 //\r
486 // Invalid case if File and FileBuffer are both NULL.\r
487 //\r
488 if (File == NULL && FileBuffer == NULL) {\r
489 return EFI_INVALID_PARAMETER;\r
490 }\r
491\r
492 //\r
493 // Directly return successfully when no handler is registered.\r
494 //\r
495 if (mNumberOfSecurity2Handler == 0) {\r
496 return EFI_SUCCESS;\r
497 }\r
498\r
499 //\r
500 // Run security handler in same order to their registered list\r
501 //\r
502 for (Index = 0; Index < mNumberOfSecurity2Handler; Index ++) {\r
503 //\r
504 // If FileBuffer is not NULL, the input is Image, which will be handled by EFI_AUTH_IMAGE_OPERATION_MASK operation.\r
505 // If FileBuffer is NULL, the input is not Image, which will be handled by EFI_AUTH_NONE_IMAGE_OPERATION_MASK operation.\r
506 // Other cases are ignored.\r
507 //\r
508 if ((FileBuffer != NULL && (mSecurity2Table[Index].Security2Operation & EFI_AUTH_IMAGE_OPERATION_MASK) != 0) ||\r
509 (FileBuffer == NULL && (mSecurity2Table[Index].Security2Operation & EFI_AUTH_NONE_IMAGE_OPERATION_MASK) != 0)) {\r
510 //\r
511 // Execute registered handlers based on input AuthenticationOperation\r
512 //\r
513 if ((mSecurity2Table[Index].Security2Operation & AuthenticationOperation) != 0) {\r
514 Status = mSecurity2Table[Index].Security2Handler (\r
515 AuthenticationStatus,\r
516 File,\r
517 FileBuffer,\r
518 FileSize,\r
519 BootPolicy\r
520 );\r
521 if (EFI_ERROR (Status)) {\r
522 return Status;\r
523 }\r
524 }\r
525 }\r
526 }\r
527\r
528 return EFI_SUCCESS;\r
529}\r