]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Library/DxeSecurityManagementLib/DxeSecurityManagementLib.c
MdeModulePkg: Clean up source files
[mirror_edk2.git] / MdeModulePkg / Library / DxeSecurityManagementLib / DxeSecurityManagementLib.c
index 1ff1bf6dfef2550950b8acacbbd44d15718dd1ae..65d788bc6863b3c1aa3dae6e49d608a91ed4c049 100644 (file)
@@ -1,8 +1,8 @@
 /** @file\r
   Provides generic security measurement functions for DXE module.\r
 \r
-Copyright (c) 2009 Intel Corporation\r
-All rights reserved. This program and the accompanying materials\r
+Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>\r
+This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
 http://opensource.org/licenses/bsd-license.php\r
@@ -13,27 +13,45 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 **/\r
 \r
 #include <PiDxe.h>\r
+#include <Protocol/LoadFile.h>\r
 #include <Library/DebugLib.h>\r
 #include <Library/DxeServicesLib.h>\r
 #include <Library/MemoryAllocationLib.h>\r
 #include <Library/SecurityManagementLib.h>\r
+#include <Library/DevicePathLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
 \r
 #define SECURITY_HANDLER_TABLE_SIZE   0x10\r
 \r
-#define EFI_AUTH_OPERATION_MASK       EFI_AUTH_OPERATION_VERIFY_IMAGE \\r
-                                      | EFI_AUTH_OPERATION_DEFER_IMAGE_LOAD \\r
-                                      | EFI_AUTH_OPERATION_MEASURE_IMAGE\r
+//\r
+// Secruity Operation on Image and none Image.\r
+//\r
+#define EFI_AUTH_IMAGE_OPERATION_MASK       (EFI_AUTH_OPERATION_VERIFY_IMAGE \\r
+                                              | EFI_AUTH_OPERATION_DEFER_IMAGE_LOAD \\r
+                                              | EFI_AUTH_OPERATION_MEASURE_IMAGE)\r
+#define EFI_AUTH_NONE_IMAGE_OPERATION_MASK  (EFI_AUTH_OPERATION_CONNECT_POLICY \\r
+                                              | EFI_AUTH_OPERATION_AUTHENTICATION_STATE)\r
 \r
 typedef struct {\r
   UINT32  SecurityOperation;\r
   SECURITY_FILE_AUTHENTICATION_STATE_HANDLER  SecurityHandler;\r
 } SECURITY_INFO;\r
 \r
+typedef struct {\r
+  UINT32  Security2Operation;\r
+  SECURITY2_FILE_AUTHENTICATION_HANDLER  Security2Handler;\r
+} SECURITY2_INFO;\r
+\r
 UINT32  mCurrentAuthOperation       = 0;\r
 UINT32  mNumberOfSecurityHandler    = 0;\r
 UINT32  mMaxNumberOfSecurityHandler = 0;\r
 SECURITY_INFO  *mSecurityTable      = NULL;\r
 \r
+UINT32  mCurrentAuthOperation2       = 0;\r
+UINT32  mNumberOfSecurity2Handler    = 0;\r
+UINT32  mMaxNumberOfSecurity2Handler = 0;\r
+SECURITY2_INFO *mSecurity2Table      = NULL;\r
+\r
 /**\r
   Reallocates more global memory to store the registered Handler list.\r
 \r
@@ -43,14 +61,15 @@ SECURITY_INFO  *mSecurityTable      = NULL;
 RETURN_STATUS\r
 EFIAPI\r
 ReallocateSecurityHandlerTable (\r
+  VOID\r
   )\r
 {\r
   //\r
   // Reallocate memory for security info structure.\r
   //\r
   mSecurityTable = ReallocatePool (\r
-                     mMaxNumberOfSecurityHandler * sizeof (SECURITY_INFO), \r
-                     (mMaxNumberOfSecurityHandler + SECURITY_HANDLER_TABLE_SIZE) * sizeof (SECURITY_INFO), \r
+                     mMaxNumberOfSecurityHandler * sizeof (SECURITY_INFO),\r
+                     (mMaxNumberOfSecurityHandler + SECURITY_HANDLER_TABLE_SIZE) * sizeof (SECURITY_INFO),\r
                      mSecurityTable\r
                      );\r
 \r
@@ -69,7 +88,7 @@ ReallocateSecurityHandlerTable (
 }\r
 \r
 /**\r
- Check whether an operation is valid according to the requirement of current operation, \r
+ Check whether an operation is valid according to the requirement of current operation,\r
  which must make sure that the measure image operation is the last one.\r
 \r
  @param CurrentAuthOperation  Current operation.\r
@@ -83,27 +102,27 @@ CheckAuthenticationOperation (
   IN  UINT32    CurrentAuthOperation,\r
   IN  UINT32    CheckAuthOperation\r
   )\r
-{ \r
+{\r
   //\r
   // Make sure new auth operation can be recognized.\r
   //\r
-  ASSERT ((CheckAuthOperation & ~(EFI_AUTH_OPERATION_MASK | EFI_AUTH_OPERATION_IMAGE_REQUIRED)) == 0);\r
-  \r
+  ASSERT ((CheckAuthOperation & ~(EFI_AUTH_IMAGE_OPERATION_MASK | EFI_AUTH_OPERATION_AUTHENTICATION_STATE | EFI_AUTH_OPERATION_IMAGE_REQUIRED)) == 0);\r
+\r
   //\r
-  // When current operation includes measure image operation, \r
+  // When current operation includes measure image operation,\r
   // only another measure image operation or none operation will be allowed.\r
   //\r
   if ((CurrentAuthOperation & EFI_AUTH_OPERATION_MEASURE_IMAGE) == EFI_AUTH_OPERATION_MEASURE_IMAGE) {\r
     if (((CheckAuthOperation & EFI_AUTH_OPERATION_MEASURE_IMAGE) == EFI_AUTH_OPERATION_MEASURE_IMAGE) ||\r
-        ((CheckAuthOperation & EFI_AUTH_OPERATION_MASK) == EFI_AUTH_OPERATION_NONE)) {\r
+        ((CheckAuthOperation & EFI_AUTH_IMAGE_OPERATION_MASK) == EFI_AUTH_OPERATION_NONE)) {\r
       return TRUE;\r
     } else {\r
       return FALSE;\r
     }\r
   }\r
-  \r
+\r
   //\r
-  // When current operation doesn't include measure image operation, \r
+  // When current operation doesn't include measure image operation,\r
   // any new operation will be allowed.\r
   //\r
   return TRUE;\r
@@ -165,12 +184,12 @@ RegisterSecurityHandler (
   Execute registered handlers until one returns an error and that error is returned.\r
   If none of the handlers return an error, then EFI_SUCCESS is returned.\r
 \r
-  Before exectue handler, get the image buffer by file device path if a handler \r
+  Before exectue handler, get the image buffer by file device path if a handler\r
   requires the image file. And return the image buffer to each handler when exectue handler.\r
 \r
   The handlers are executed in same order to their registered order.\r
 \r
-  @param[in]  AuthenticationStatus \r
+  @param[in]  AuthenticationStatus\r
                            This is the authentication type returned from the Section\r
                            Extraction protocol. See the Section Extraction Protocol\r
                            Specification for details on this type.\r
@@ -178,9 +197,9 @@ RegisterSecurityHandler (
                            being dispatched. This will optionally be used for logging.\r
 \r
   @retval EFI_SUCCESS            The file specified by File did authenticate when more\r
-                                 than one security handler services were registered, \r
-                                 or the file did not authenticate when no security \r
-                                 handler service was registered. And the platform policy \r
+                                 than one security handler services were registered,\r
+                                 or the file did not authenticate when no security\r
+                                 handler service was registered. And the platform policy\r
                                  dictates that the DXE Core may use File.\r
   @retval EFI_INVALID_PARAMETER  File is NULL.\r
   @retval EFI_SECURITY_VIOLATION The file specified by File did not authenticate, and\r
@@ -204,7 +223,10 @@ ExecuteSecurityHandlers (
   UINT32        HandlerAuthenticationStatus;\r
   VOID          *FileBuffer;\r
   UINTN         FileSize;\r
-  \r
+  EFI_HANDLE    Handle;\r
+  EFI_DEVICE_PATH_PROTOCOL        *Node;\r
+  EFI_DEVICE_PATH_PROTOCOL        *FilePathToVerfiy;\r
+\r
   if (FilePath == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
@@ -215,11 +237,12 @@ ExecuteSecurityHandlers (
   if (mNumberOfSecurityHandler == 0) {\r
     return EFI_SUCCESS;\r
   }\r
-  \r
+\r
   Status                      = EFI_SUCCESS;\r
   FileBuffer                  = NULL;\r
   FileSize                    = 0;\r
   HandlerAuthenticationStatus = AuthenticationStatus;\r
+  FilePathToVerfiy            = (EFI_DEVICE_PATH_PROTOCOL *) FilePath;\r
   //\r
   // Run security handler in same order to their registered list\r
   //\r
@@ -229,12 +252,29 @@ ExecuteSecurityHandlers (
       // Try get file buffer when the handler requires image buffer.\r
       //\r
       if (FileBuffer == NULL) {\r
+        Node   = FilePathToVerfiy;\r
+        Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &Node, &Handle);\r
+        //\r
+        // Try to get image by FALSE boot policy for the exact boot file path.\r
+        //\r
         FileBuffer = GetFileBufferByFilePath (FALSE, FilePath, &FileSize, &AuthenticationStatus);\r
+        if (FileBuffer == NULL) {\r
+          //\r
+          // Try to get image by TRUE boot policy for the inexact boot file path.\r
+          //\r
+          FileBuffer = GetFileBufferByFilePath (TRUE, FilePath, &FileSize, &AuthenticationStatus);\r
+        }\r
+        if ((FileBuffer != NULL) && (!EFI_ERROR (Status))) {\r
+          //\r
+          // LoadFile () may cause the device path of the Handle be updated.\r
+          //\r
+          FilePathToVerfiy = AppendDevicePath (DevicePathFromHandle (Handle), Node);\r
+        }\r
       }\r
     }\r
     Status = mSecurityTable[Index].SecurityHandler (\r
                HandlerAuthenticationStatus,\r
-               FilePath,\r
+               FilePathToVerfiy,\r
                FileBuffer,\r
                FileSize\r
                );\r
@@ -246,6 +286,250 @@ ExecuteSecurityHandlers (
   if (FileBuffer != NULL) {\r
     FreePool (FileBuffer);\r
   }\r
+  if (FilePathToVerfiy != FilePath) {\r
+    FreePool (FilePathToVerfiy);\r
+  }\r
 \r
   return Status;\r
 }\r
+\r
+/**\r
+  Reallocates more global memory to store the registered Securit2Handler list.\r
+\r
+  @retval  RETURN_SUCCESS            Reallocate memory successfully.\r
+  @retval  RETURN_OUT_OF_RESOURCES   No enough memory to allocated.\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+ReallocateSecurity2HandlerTable (\r
+  VOID\r
+  )\r
+{\r
+  //\r
+  // Reallocate memory for security info structure.\r
+  //\r
+  mSecurity2Table = ReallocatePool (\r
+                     mMaxNumberOfSecurity2Handler * sizeof (SECURITY2_INFO),\r
+                     (mMaxNumberOfSecurity2Handler + SECURITY_HANDLER_TABLE_SIZE) * sizeof (SECURITY2_INFO),\r
+                     mSecurity2Table\r
+                     );\r
+\r
+  //\r
+  // No enough resource is allocated.\r
+  //\r
+  if (mSecurity2Table == NULL) {\r
+    return RETURN_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  //\r
+  // Increase max handler number\r
+  //\r
+  mMaxNumberOfSecurity2Handler = mMaxNumberOfSecurity2Handler + SECURITY_HANDLER_TABLE_SIZE;\r
+  return RETURN_SUCCESS;\r
+}\r
+\r
+/**\r
+  Check whether an operation is valid according to the requirement of current operation,\r
+  which must make sure that the measure image operation is the last one.\r
+\r
+  If AuthenticationOperation is not recongnized, return FALSE.\r
+  If AuthenticationOperation is EFI_AUTH_OPERATION_NONE, return FALSE.\r
+  If AuthenticationOperation includes security operation and authentication operation, return FALSE.\r
+  If the previous register handler can't be executed before the later register handler, return FALSE.\r
+\r
+  @param CurrentAuthOperation  Current operation.\r
+  @param CheckAuthOperation    Operation to be checked.\r
+\r
+  @retval  TRUE   Operation is valid for current operation.\r
+  @retval  FALSE  Operation is invalid for current operation.\r
+**/\r
+BOOLEAN\r
+CheckAuthentication2Operation (\r
+  IN  UINT32    CurrentAuthOperation,\r
+  IN  UINT32    CheckAuthOperation\r
+  )\r
+{\r
+  //\r
+  // Make sure new auth operation can be recognized.\r
+  //\r
+  if (CheckAuthOperation == EFI_AUTH_OPERATION_NONE) {\r
+    return FALSE;\r
+  }\r
+  if ((CheckAuthOperation & ~(EFI_AUTH_IMAGE_OPERATION_MASK |\r
+                              EFI_AUTH_NONE_IMAGE_OPERATION_MASK |\r
+                              EFI_AUTH_OPERATION_IMAGE_REQUIRED)) != 0) {\r
+    return FALSE;\r
+  }\r
+\r
+  //\r
+  // When current operation includes measure image operation,\r
+  // only another measure image or none image operation will be allowed.\r
+  //\r
+  if ((CurrentAuthOperation & EFI_AUTH_OPERATION_MEASURE_IMAGE) == EFI_AUTH_OPERATION_MEASURE_IMAGE) {\r
+    if (((CheckAuthOperation & EFI_AUTH_OPERATION_MEASURE_IMAGE) == EFI_AUTH_OPERATION_MEASURE_IMAGE) ||\r
+        ((CheckAuthOperation & EFI_AUTH_IMAGE_OPERATION_MASK) == 0)) {\r
+      return TRUE;\r
+    } else {\r
+      return FALSE;\r
+    }\r
+  }\r
+\r
+  //\r
+  // Any other operation will be allowed.\r
+  //\r
+  return TRUE;\r
+}\r
+\r
+/**\r
+  Register security measurement handler with its operation type. Different\r
+  handlers with the same operation can all be registered.\r
+\r
+  If Security2Handler is NULL, then ASSERT().\r
+  If no enough resources available to register new handler, then ASSERT().\r
+  If AuthenticationOperation is not recongnized, then ASSERT().\r
+  If AuthenticationOperation is EFI_AUTH_OPERATION_NONE, then ASSERT().\r
+  If the previous register handler can't be executed before the later register handler, then ASSERT().\r
+\r
+  @param[in]  Security2Handler          The security measurement service handler to be registered.\r
+  @param[in]  AuthenticationOperation   The operation type is specified for the registered handler.\r
+\r
+  @retval EFI_SUCCESS              The handlers were registered successfully.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+RegisterSecurity2Handler (\r
+  IN  SECURITY2_FILE_AUTHENTICATION_HANDLER       Security2Handler,\r
+  IN  UINT32                                      AuthenticationOperation\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+\r
+  ASSERT (Security2Handler != NULL);\r
+\r
+  //\r
+  // Make sure AuthenticationOperation is valid in the register order.\r
+  //\r
+  ASSERT (CheckAuthentication2Operation (mCurrentAuthOperation2, AuthenticationOperation));\r
+  mCurrentAuthOperation2 = mCurrentAuthOperation2 | AuthenticationOperation;\r
+\r
+  //\r
+  // Check whether the handler lists is enough to store new handler.\r
+  //\r
+  if (mNumberOfSecurity2Handler == mMaxNumberOfSecurity2Handler) {\r
+    //\r
+    // Allocate more resources for new handler.\r
+    //\r
+    Status = ReallocateSecurity2HandlerTable();\r
+    ASSERT_EFI_ERROR (Status);\r
+  }\r
+\r
+  //\r
+  // Register new handler into the handler list.\r
+  //\r
+  mSecurity2Table[mNumberOfSecurity2Handler].Security2Operation = AuthenticationOperation;\r
+  mSecurity2Table[mNumberOfSecurity2Handler].Security2Handler   = Security2Handler;\r
+  mNumberOfSecurity2Handler ++;\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Execute registered handlers based on input AuthenticationOperation until\r
+  one returns an error and that error is returned.\r
+\r
+  If none of the handlers return an error, then EFI_SUCCESS is returned.\r
+  The handlers those satisfy AuthenticationOperation will only be executed.\r
+  The handlers are executed in same order to their registered order.\r
+\r
+  @param[in]  AuthenticationOperation\r
+                           The operation type specifies which handlers will be executed.\r
+  @param[in]  AuthenticationStatus\r
+                           The authentication status for the input file.\r
+  @param[in]  File         This is a pointer to the device path of the file that is\r
+                           being dispatched. This will optionally be used for logging.\r
+  @param[in]  FileBuffer   A pointer to the buffer with the UEFI file image\r
+  @param[in]  FileSize     The size of File buffer.\r
+  @param[in]  BootPolicy   A boot policy that was used to call LoadImage() UEFI service.\r
+\r
+  @retval EFI_SUCCESS             The file specified by DevicePath and non-NULL\r
+                                  FileBuffer did authenticate, and the platform policy dictates\r
+                                  that the DXE Foundation may use the file.\r
+  @retval EFI_SUCCESS             The device path specified by NULL device path DevicePath\r
+                                  and non-NULL FileBuffer did authenticate, and the platform\r
+                                  policy dictates that the DXE Foundation may execute the image in\r
+                                  FileBuffer.\r
+  @retval EFI_SUCCESS             FileBuffer is NULL and current user has permission to start\r
+                                  UEFI device drivers on the device path specified by DevicePath.\r
+  @retval EFI_SECURITY_VIOLATION  The file specified by File or FileBuffer did not\r
+                                  authenticate, and the platform policy dictates that\r
+                                  the file should be placed in the untrusted state.\r
+  @retval EFI_SECURITY_VIOLATION  FileBuffer FileBuffer is NULL and the user has no\r
+                                  permission to start UEFI device drivers on the device path specified\r
+                                  by DevicePath.\r
+  @retval EFI_SECURITY_VIOLATION  FileBuffer is not NULL and the user has no permission to load\r
+                                  drivers from the device path specified by DevicePath. The\r
+                                  image has been added into the list of the deferred images.\r
+  @retval EFI_ACCESS_DENIED       The file specified by File did not authenticate, and\r
+                                  the platform policy dictates that the DXE\r
+                                  Foundation may not use File.\r
+  @retval EFI_INVALID_PARAMETER   File and FileBuffer are both NULL.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+ExecuteSecurity2Handlers (\r
+  IN  UINT32                           AuthenticationOperation,\r
+  IN  UINT32                           AuthenticationStatus,\r
+  IN  CONST EFI_DEVICE_PATH_PROTOCOL   *File,\r
+  IN  VOID                             *FileBuffer,\r
+  IN  UINTN                            FileSize,\r
+  IN  BOOLEAN                          BootPolicy\r
+  )\r
+{\r
+  UINT32        Index;\r
+  EFI_STATUS    Status;\r
+\r
+  //\r
+  // Invalid case if File and FileBuffer are both NULL.\r
+  //\r
+  if (File == NULL && FileBuffer == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  //\r
+  // Directly return successfully when no handler is registered.\r
+  //\r
+  if (mNumberOfSecurity2Handler == 0) {\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  //\r
+  // Run security handler in same order to their registered list\r
+  //\r
+  for (Index = 0; Index < mNumberOfSecurity2Handler; Index ++) {\r
+    //\r
+    // If FileBuffer is not NULL, the input is Image, which will be handled by EFI_AUTH_IMAGE_OPERATION_MASK operation.\r
+    // If FileBuffer is NULL, the input is not Image, which will be handled by EFI_AUTH_NONE_IMAGE_OPERATION_MASK operation.\r
+    // Other cases are ignored.\r
+    //\r
+    if ((FileBuffer != NULL && (mSecurity2Table[Index].Security2Operation & EFI_AUTH_IMAGE_OPERATION_MASK) != 0) ||\r
+        (FileBuffer == NULL && (mSecurity2Table[Index].Security2Operation & EFI_AUTH_NONE_IMAGE_OPERATION_MASK) != 0)) {\r
+      //\r
+      // Execute registered handlers based on input AuthenticationOperation\r
+      //\r
+      if ((mSecurity2Table[Index].Security2Operation & AuthenticationOperation) != 0) {\r
+        Status = mSecurity2Table[Index].Security2Handler (\r
+                   AuthenticationStatus,\r
+                   File,\r
+                   FileBuffer,\r
+                   FileSize,\r
+                   BootPolicy\r
+                   );\r
+        if (EFI_ERROR (Status)) {\r
+          return Status;\r
+        }\r
+      }\r
+    }\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r