VirtualBox

Ignore:
Timestamp:
Oct 28, 2015 8:17:18 PM (9 years ago)
Author:
vboxsync
Message:

EFI/Firmware: 'svn merge /vendor/edk2/UDK2010.SR1 /vendor/edk2/current .', reverting and removing files+dirs listed in ReadMe.vbox, resolving conflicts with help from ../UDK2014.SP1/. This is a raw untested merge.

Location:
trunk/src/VBox/Devices/EFI/Firmware
Files:
15 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Devices/EFI/Firmware

  • trunk/src/VBox/Devices/EFI/Firmware/SecurityPkg/Library/DxeDeferImageLoadLib/DxeDeferImageLoadLib.c

    r48674 r58459  
    22  Implement defer image load services for user identification in UEFI2.2.
    33
    4 Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>
     4Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
    55This program and the accompanying materials
    66are licensed and made available under the terms and conditions of the BSD License
     
    247247    while (CheckLen < Info->InfoSize - sizeof (EFI_USER_INFO)) {
    248248      Access = (EFI_USER_INFO_ACCESS_CONTROL *) ((UINT8 *) (Info + 1) + CheckLen);
    249       if ((Access->Type == AccessType)) {
     249      if (Access->Type == AccessType) {
    250250        *AccessControl = AllocateZeroPool (Access->Size);
    251251        ASSERT (*AccessControl != NULL);
     
    264264}
    265265
    266 
    267 /**
    268   Convert the '/' to '\' in the specified string.
    269 
    270   @param[in, out]  Str       Points to the string to convert.
    271 
    272 **/
    273 VOID
    274 ConvertDPStr (
    275   IN OUT EFI_STRING                     Str
    276   )
    277 {
    278   INTN                                  Count;
    279   INTN                                  Index;
    280  
    281   Count = StrSize(Str) / 2 - 1;
    282 
    283   if (Count < 4) {
    284     return;
    285   }
    286  
    287   //
    288   // Convert device path string.
    289   //
    290   Index = Count - 1;
    291   while (Index > 0) {
    292     //
    293     // Find the last '/'.
    294     //
    295     for (Index = Count - 1; Index > 0; Index--) {
    296       if (Str[Index] == L'/')
    297         break;
    298     }
    299 
    300     //
    301     // Check next char.
    302     //
    303     if (Str[Index + 1] == L'\\')
    304       return;
     266/**
     267  Get file name from device path.
     268
     269  The file name may contain one or more device path node. Save the file name in a
     270  buffer if file name is found. The caller is responsible to free the buffer. 
     271 
     272  @param[in]  DevicePath     A pointer to a device path.
     273  @param[out] FileName       The callee allocated buffer to save the file name if file name is found.
     274  @param[out] FileNameOffset The offset of file name in device path if file name is found.
     275 
     276  @retval     UINTN          The file name length. 0 means file name is not found.
     277
     278**/
     279UINTN
     280GetFileName (
     281  IN  CONST EFI_DEVICE_PATH_PROTOCOL          *DevicePath,
     282  OUT UINT8                                   **FileName,
     283  OUT UINTN                                   *FileNameOffset
     284  )
     285{
     286  UINTN                                       Length;
     287  EFI_DEVICE_PATH_PROTOCOL                    *TmpDevicePath;
     288  EFI_DEVICE_PATH_PROTOCOL                    *RootDevicePath;
     289  CHAR8                                       *NodeStr;
     290  UINTN                                       NodeStrLength;
     291  CHAR16                                      LastNodeChar;
     292  CHAR16                                      FirstNodeChar;
     293
     294  //
     295  // Get the length of DevicePath before file name.
     296  //
     297  Length = 0;
     298  RootDevicePath = (EFI_DEVICE_PATH_PROTOCOL *)DevicePath;
     299  while (!IsDevicePathEnd (RootDevicePath)) {
     300    if ((DevicePathType(RootDevicePath) == MEDIA_DEVICE_PATH) && (DevicePathSubType(RootDevicePath) == MEDIA_FILEPATH_DP)) {
     301      break;
     302    }
     303    Length += DevicePathNodeLength (RootDevicePath);
     304    RootDevicePath = NextDevicePathNode (RootDevicePath);
     305  }
     306
     307  *FileNameOffset = Length;
     308  if (Length == 0) {
     309    return 0;
     310  }
     311
     312  //
     313  // Get the file name length.
     314  //
     315  Length = 0;
     316  TmpDevicePath = RootDevicePath;
     317  while (!IsDevicePathEnd (TmpDevicePath)) {
     318    if ((DevicePathType(TmpDevicePath) != MEDIA_DEVICE_PATH) || (DevicePathSubType(TmpDevicePath) != MEDIA_FILEPATH_DP)) {
     319      break;
     320    }
     321    Length += DevicePathNodeLength (TmpDevicePath) - sizeof (EFI_DEVICE_PATH_PROTOCOL);
     322    TmpDevicePath = NextDevicePathNode (TmpDevicePath);
     323  }
     324  if (Length == 0) {
     325    return 0;
     326  }
     327
     328  *FileName = AllocateZeroPool (Length);
     329  ASSERT (*FileName != NULL);
     330
     331  //
     332  // Copy the file name to the buffer.
     333  //
     334  Length = 0;
     335  LastNodeChar = '\\';
     336  TmpDevicePath = RootDevicePath;
     337  while (!IsDevicePathEnd (TmpDevicePath)) {
     338    if ((DevicePathType(TmpDevicePath) != MEDIA_DEVICE_PATH) || (DevicePathSubType(TmpDevicePath) != MEDIA_FILEPATH_DP)) {
     339      break;
     340    }
     341
     342    FirstNodeChar = (CHAR16) ReadUnaligned16 ((UINT16 *)((UINT8 *)TmpDevicePath + sizeof (EFI_DEVICE_PATH_PROTOCOL)));
     343    NodeStr = (CHAR8 *)TmpDevicePath + sizeof (EFI_DEVICE_PATH_PROTOCOL);
     344    NodeStrLength = DevicePathNodeLength (TmpDevicePath) - sizeof (EFI_DEVICE_PATH_PROTOCOL) - sizeof(CHAR16);
    305345   
    306     Str[Index] = L'\\';
     346    if ((FirstNodeChar == '\\') && (LastNodeChar == '\\')) {
     347      //
     348      // Skip separator "\" when there are two separators.
     349      //
     350      NodeStr += sizeof (CHAR16);
     351      NodeStrLength -= sizeof (CHAR16);     
     352    } else if ((FirstNodeChar != '\\') && (LastNodeChar != '\\')) {
     353      //
     354      // Add separator "\" when there is no separator.
     355      //
     356      WriteUnaligned16 ((UINT16 *)(*FileName + Length), '\\');
     357      Length += sizeof (CHAR16);
     358    }
     359    CopyMem (*FileName + Length, NodeStr, NodeStrLength);
     360    Length += NodeStrLength;
    307361   
    308     //
    309     // Check previous char.
    310     //
    311     if ((Index > 0) && (Str[Index - 1] == L'\\')) {
    312       CopyMem (&Str[Index - 1], &Str[Index], (UINTN) ((Count - Index + 1) * sizeof (CHAR16)));
    313       return;
    314     }
    315     Index--;
    316   }
     362    LastNodeChar  = (CHAR16) ReadUnaligned16 ((UINT16 *) (NodeStr + NodeStrLength - sizeof(CHAR16)));
     363    TmpDevicePath = NextDevicePathNode (TmpDevicePath);
     364  }   
     365
     366  return Length;
    317367}
    318368
     
    343393  )
    344394{
    345   EFI_STATUS                            Status;
    346   EFI_STRING                            DevicePathStr1;
    347   EFI_STRING                            DevicePathStr2;
    348   UINTN                                 StrLen1;
    349   UINTN                                 StrLen2;
    350   EFI_DEVICE_PATH_TO_TEXT_PROTOCOL      *DevicePathText;
    351   BOOLEAN                               DevicePathEqual;
     395  UINTN                                       DevicePathSize;
     396  UINTN                                       FileNameSize1;
     397  UINTN                                       FileNameSize2;
     398  UINT8                                       *FileName1;
     399  UINT8                                       *FileName2;
     400  UINTN                                       FileNameOffset1;
     401  UINTN                                       FileNameOffset2;
     402  BOOLEAN                                     DevicePathEqual;
     403
     404  FileName1       = NULL;
     405  FileName2       = NULL;
     406  DevicePathEqual = TRUE;
    352407
    353408  ASSERT (DevicePath1 != NULL);
    354409  ASSERT (DevicePath2 != NULL);
    355  
    356   DevicePathEqual = FALSE;
    357   DevicePathText  = NULL;
    358   Status = gBS->LocateProtocol (
    359                   &gEfiDevicePathToTextProtocolGuid,
    360                   NULL,
    361                   (VOID **) &DevicePathText
    362                   );
    363   ASSERT (Status == EFI_SUCCESS);
    364  
    365   //
    366   // Get first device path string.
    367   //
    368   DevicePathStr1 = DevicePathText->ConvertDevicePathToText (DevicePath1, TRUE, TRUE);
    369   ConvertDPStr (DevicePathStr1);
    370   //
    371   // Get second device path string.
    372   //
    373   DevicePathStr2 = DevicePathText->ConvertDevicePathToText (DevicePath2, TRUE, TRUE);
    374   ConvertDPStr (DevicePathStr2);
    375  
    376   //
    377   // Compare device path string.
    378   //
    379   StrLen1 = StrSize (DevicePathStr1);
    380   StrLen2 = StrSize (DevicePathStr2);
    381   if (StrLen1 > StrLen2) {
     410  if (IsDevicePathEnd (DevicePath1)) {
     411    return FALSE;
     412  }
     413 
     414  //
     415  // The file name may contain one or more device path node.
     416  // To compare the file name, copy file name to a buffer and compare the buffer.
     417  //
     418  FileNameSize1 = GetFileName (DevicePath1, &FileName1, &FileNameOffset1);
     419  if (FileNameSize1 != 0) {
     420    FileNameSize2 = GetFileName (DevicePath2, &FileName2, &FileNameOffset2);
     421    if (FileNameOffset1 != FileNameOffset2) {
     422      DevicePathEqual = FALSE;
     423      goto Done;
     424    }
     425    if (CompareMem (DevicePath1, DevicePath2, FileNameOffset1) != 0) {     
     426      DevicePathEqual = FALSE;
     427      goto Done;
     428    }
     429    if (FileNameSize1 > FileNameSize2) {
     430      DevicePathEqual = FALSE;
     431      goto Done;
     432    }
     433    if (CompareMem (FileName1, FileName2, FileNameSize1) != 0) {     
     434      DevicePathEqual = FALSE;
     435      goto Done;
     436    }
     437    DevicePathEqual = TRUE;
     438    goto Done;
     439  }
     440
     441  DevicePathSize = GetDevicePathSize (DevicePath1);
     442  if (DevicePathSize > GetDevicePathSize (DevicePath2)) {
     443    return FALSE;
     444  }
     445
     446  //
     447  // Exclude the end of device path node.
     448  //
     449  DevicePathSize -= sizeof (EFI_DEVICE_PATH_PROTOCOL);
     450  if (CompareMem (DevicePath1, DevicePath2, DevicePathSize) != 0) {
    382451    DevicePathEqual = FALSE;
    383     goto Done;
    384   }
    385  
    386   if (CompareMem (DevicePathStr1, DevicePathStr2, StrLen1) == 0) {
    387     DevicePathEqual = TRUE;
    388   }
    389 
    390 Done:
    391   FreePool (DevicePathStr1);
    392   FreePool (DevicePathStr2);
     452  }
     453 
     454Done:
     455  if (FileName1 != NULL) {
     456    FreePool (FileName1);
     457  }
     458  if (FileName2 != NULL) {
     459    FreePool (FileName2);
     460  }
    393461  return DevicePathEqual;
    394462}
     
    533601    //
    534602    UnicodeSPrint (StrTemp, sizeof (StrTemp), L"Boot%04x", Index);
    535     OptionBuffer = GetEfiGlobalVariable (StrTemp);
     603    GetEfiGlobalVariable2 (StrTemp, (VOID**)&OptionBuffer, NULL);
    536604    if (OptionBuffer == NULL) {
    537605      continue;
     
    715783  @param[in]  FileBuffer            File buffer matches the input file device path.
    716784  @param[in]  FileSize              Size of File buffer matches the input file device path.
    717 
    718   @retval EFI_SUCCESS               The file specified by File did authenticate, and the
    719                                     platform policy dictates that the DXE Core may use File.
    720   @retval EFI_INVALID_PARAMETER     File is NULL.
    721   @retval EFI_SECURITY_VIOLATION    The file specified by File did not authenticate, and
    722                                     the platform policy dictates that File should be placed
    723                                     in the untrusted state. A file may be promoted from
    724                                     the untrusted to the trusted state at a future time
    725                                     with a call to the Trust() DXE Service.
    726   @retval EFI_ACCESS_DENIED         The file specified by File did not authenticate, and
    727                                     the platform policy dictates that File should not be
    728                                     used for any purpose.
     785  @param[in]  BootPolicy            A boot policy that was used to call LoadImage() UEFI service.
     786
     787  @retval EFI_SUCCESS               FileBuffer is NULL and current user has permission to start
     788                                    UEFI device drivers on the device path specified by DevicePath.
     789  @retval EFI_SUCCESS               The file specified by DevicePath and non-NULL
     790                                    FileBuffer did authenticate, and the platform policy dictates
     791                                    that the DXE Foundation may use the file.
     792  @retval EFI_SECURITY_VIOLATION    FileBuffer is NULL and the user has no
     793                                    permission to start UEFI device drivers on the device path specified
     794                                    by DevicePath.
     795  @retval EFI_SECURITY_VIOLATION    FileBuffer is not NULL and the user has no permission to load
     796                                    drivers from the device path specified by DevicePath. The
     797                                    image has been added into the list of the deferred images.
     798  @retval EFI_ACCESS_DENIED         The file specified by File and FileBuffer did not
     799                                    authenticate, and the platform policy dictates that the DXE
     800                                    Foundation many not use File.
    729801
    730802**/
     
    735807  IN  CONST EFI_DEVICE_PATH_PROTOCOL   *File,
    736808  IN  VOID                             *FileBuffer,
    737   IN  UINTN                            FileSize
    738   )
    739 
     809  IN  UINTN                            FileSize,
     810  IN  BOOLEAN                          BootPolicy
     811  )
    740812{
    741813  EFI_STATUS                           Status;
     
    744816  UINT32                               FileType;
    745817
     818  //
     819  // Ignore if File is NULL.
     820  //
    746821  if (File == NULL) {
    747     return EFI_INVALID_PARAMETER;
     822    return EFI_SUCCESS;
    748823  }
    749824
     
    760835      if (!VerifyDevicePath (File)) {
    761836        DEBUG ((EFI_D_ERROR, "[Security] The image is forbidden to load!\n"));
    762         return EFI_ACCESS_DENIED;
     837        return EFI_SECURITY_VIOLATION;
    763838      }
    764839      return EFI_SUCCESS;
     
    779854  }
    780855 
    781   DEBUG ((EFI_D_ERROR, "[Security] No user identified, the image is deferred to load!\n"));
    782   PutDefferedImageInfo (File, NULL, 0);
     856  DEBUG ((EFI_D_INFO, "[Security] No user identified, the image is deferred to load!\n"));
     857  PutDefferedImageInfo (File, FileBuffer, FileSize);
    783858
    784859  //
     
    850925    );
    851926 
    852   return RegisterSecurityHandler (
     927  return RegisterSecurity2Handler (
    853928           DxeDeferImageLoadHandler,
    854929           EFI_AUTH_OPERATION_DEFER_IMAGE_LOAD
  • trunk/src/VBox/Devices/EFI/Firmware/SecurityPkg/Library/DxeDeferImageLoadLib/DxeDeferImageLoadLib.h

    r48674 r58459  
    33  internal structure and functions used by DeferImageLoadLib.
    44
    5 Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>
     5Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.<BR>
    66This program and the accompanying materials
    77are licensed and made available under the terms and conditions of the BSD License
     
    3535#include <Protocol/UserCredential.h>
    3636#include <Protocol/UserManager.h>
    37 #include <Protocol/DevicePathToText.h>
    3837
    3938#include <Guid/GlobalVariable.h>
  • trunk/src/VBox/Devices/EFI/Firmware/SecurityPkg/Library/DxeDeferImageLoadLib/DxeDeferImageLoadLib.inf

    r48674 r58459  
    11## @file
    2 The library instance provides security service of deferring image load.
     2Provides security service of deferred image load
    33#
    4 # Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>
     4#  The platform may need to defer the execution of an image because of security
     5#  considerations. These deferred images will be recorded and then reported by
     6#  installing an instance of the EFI_DEFERRED_IMAGE_LOAD_PROTOCOL.
     7#
     8# Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
    59# This program and the accompanying materials
    610# are licensed and made available under the terms and conditions of the BSD License
     
    1418[Defines]
    1519  INF_VERSION                    = 0x00010005
    16   BASE_NAME                      = DxeDeferImageLoadLib   
     20  BASE_NAME                      = DxeDeferImageLoadLib
     21  MODULE_UNI_FILE                = DxeDeferImageLoadLib.uni
    1722  FILE_GUID                      = 5E2FAE1F-41DA-4fbd-BC81-603CE5CD8497
    1823  MODULE_TYPE                    = DXE_DRIVER
     
    4954
    5055[Protocols]
    51   gEfiFirmwareVolume2ProtocolGuid
    52   gEfiBlockIoProtocolGuid
    53   gEfiSimpleFileSystemProtocolGuid
     56  gEfiFirmwareVolume2ProtocolGuid                        ## SOMETIMES_CONSUMES
     57  gEfiBlockIoProtocolGuid                                ## SOMETIMES_CONSUMES
     58  gEfiSimpleFileSystemProtocolGuid                       ## SOMETIMES_CONSUMES
     59  gEfiDeferredImageLoadProtocolGuid                      ## SOMETIMES_PRODUCES
     60  ## SOMETIMES_CONSUMES
     61  ## NOTIFY
    5462  gEfiUserManagerProtocolGuid
    55   gEfiDeferredImageLoadProtocolGuid
    56   gEfiDevicePathToTextProtocolGuid
    57  
     63   
    5864[Guids]
    59   gEfiGlobalVariableGuid
     65  gEfiGlobalVariableGuid                                  ## SOMETIMES_CONSUMES  ## Variable:L"BootOrder"
    6066 
    6167[Pcd]
    62   gEfiSecurityPkgTokenSpaceGuid.PcdDeferImageLoadPolicy
     68  gEfiSecurityPkgTokenSpaceGuid.PcdDeferImageLoadPolicy   ## SOMETIMES_CONSUMES
  • trunk/src/VBox/Devices/EFI/Firmware/SecurityPkg/Library/DxeImageVerificationLib/DxeImageVerificationLib.c

    r48674 r58459  
    22  Implement image verification services for secure boot service in UEFI2.3.1.
    33
    4 Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>
     4  Caution: This file requires additional review when modified.
     5  This library will have external input - PE/COFF image.
     6  This external input must be validated carefully to avoid security issue like
     7  buffer overflow, integer overflow.
     8
     9  DxeImageVerificationLibImageRead() function will make sure the PE/COFF image content
     10  read is within the image buffer.
     11
     12  DxeImageVerificationHandler(), HashPeImageByType(), HashPeImage() function will accept
     13  untrusted PE/COFF image and validate its data structure within this image buffer before use.
     14
     15Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
    516This program and the accompanying materials
    617are licensed and made available under the terms and conditions of the BSD License
     
    1526#include "DxeImageVerificationLib.h"
    1627
     28//
     29// Caution: This is used by a function which may receive untrusted input.
     30// These global variables hold PE/COFF image data, and they should be validated before use.
     31//
    1732EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION mNtHeader;
     33UINT32                              mPeCoffHeaderOffset;
     34EFI_GUID                            mCertType;
     35
     36//
     37// Information on current PE/COFF image
     38//
    1839UINTN                               mImageSize;
    19 UINT32                              mPeCoffHeaderOffset;
     40UINT8                               *mImageBase       = NULL;
    2041UINT8                               mImageDigest[MAX_DIGEST_SIZE];
    2142UINTN                               mImageDigestSize;
    22 EFI_IMAGE_DATA_DIRECTORY            *mSecDataDir      = NULL;
    23 UINT8                               *mImageBase       = NULL;
    24 EFI_GUID                            mCertType;
    2543
    2644//
     
    3957//
    4058UINT8 mHashOidValue[] = {
    41   0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x02, 0x05,         // OBJ_md5
    4259  0x2B, 0x0E, 0x03, 0x02, 0x1A,                           // OBJ_sha1
    4360  0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04,   // OBJ_sha224
     
    4865
    4966HASH_TABLE mHash[] = {
    50   { L"SHA1",   20, &mHashOidValue[8],  5, Sha1GetContextSize,  Sha1Init,   Sha1Update,    Sha1Final  },
    51   { L"SHA224", 28, &mHashOidValue[13], 9, NULL,                NULL,       NULL,          NULL       },
    52   { L"SHA256", 32, &mHashOidValue[22], 9, Sha256GetContextSize,Sha256Init, Sha256Update,  Sha256Final},
    53   { L"SHA384", 48, &mHashOidValue[31], 9, NULL,                NULL,       NULL,          NULL       },
    54   { L"SHA512", 64, &mHashOidValue[40], 9, NULL,                NULL,       NULL,          NULL       }
     67  { L"SHA1",   20, &mHashOidValue[0],  5, Sha1GetContextSize,  Sha1Init,   Sha1Update,    Sha1Final  },
     68  { L"SHA224", 28, &mHashOidValue[5], 9, NULL,                NULL,       NULL,          NULL       },
     69  { L"SHA256", 32, &mHashOidValue[14], 9, Sha256GetContextSize,Sha256Init, Sha256Update,  Sha256Final},
     70  { L"SHA384", 48, &mHashOidValue[23], 9, NULL,                NULL,       NULL,          NULL       },
     71  { L"SHA512", 64, &mHashOidValue[32], 9, NULL,                NULL,       NULL,          NULL       }
    5572};
     73
     74/**
     75  SecureBoot Hook for processing image verification.
     76
     77  @param[in] VariableName                 Name of Variable to be found.
     78  @param[in] VendorGuid                   Variable vendor GUID.
     79  @param[in] DataSize                     Size of Data found. If size is less than the
     80                                          data, this value contains the required size.
     81  @param[in] Data                         Data pointer.
     82
     83**/
     84VOID
     85EFIAPI
     86SecureBootHook (
     87  IN CHAR16                                 *VariableName,
     88  IN EFI_GUID                               *VendorGuid,
     89  IN UINTN                                  DataSize,
     90  IN VOID                                   *Data
     91  );
    5692
    5793/**
    5894  Reads contents of a PE/COFF image in memory buffer.
     95
     96  Caution: This function may receive untrusted input.
     97  PE/COFF image is external input, so this function will make sure the PE/COFF image content
     98  read is within the image buffer.
    5999
    60100  @param  FileHandle      Pointer to the file handle to read the PE/COFF image.
     
    118158  EFI_DEVICE_PATH_PROTOCOL          *TempDevicePath;
    119159  EFI_BLOCK_IO_PROTOCOL             *BlockIo;
     160
     161  if (File == NULL) {
     162    return IMAGE_UNKNOWN;
     163  }
    120164
    121165  //
     
    230274  PE/COFF Specification 8.0 Appendix A
    231275
     276  Caution: This function may receive untrusted input.
     277  PE/COFF image is external input, so this function will validate its data structure
     278  within this image buffer before use.
     279
    232280  @param[in]    HashAlg   Hash algorithm type.
    233281
     
    298346  // But CheckSum field and SECURITY data directory (certificate) are excluded
    299347  //
    300   Magic = mNtHeader.Pe32->OptionalHeader.Magic;
     348  if (mNtHeader.Pe32->FileHeader.Machine == IMAGE_FILE_MACHINE_IA64 && mNtHeader.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
     349    //
     350    // NOTE: Some versions of Linux ELILO for Itanium have an incorrect magic value
     351    //       in the PE/COFF Header. If the MachineType is Itanium(IA64) and the
     352    //       Magic value in the OptionalHeader is EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
     353    //       then override the magic value to EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
     354    //
     355    Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;
     356  } else {
     357    //
     358    // Get the magic value from the PE/COFF Optional Header
     359    //
     360    Magic =  mNtHeader.Pe32->OptionalHeader.Magic;
     361  }
     362 
    301363  //
    302364  // 3.  Calculate the distance from the base of the image header to the image checksum address.
     
    537599  8.0 Appendix A
    538600
     601  Caution: This function may receive untrusted input.
     602  PE/COFF image is external input, so this function will validate its data structure
     603  within this image buffer before use.
     604
     605  @param[in]  AuthData            Pointer to the Authenticode Signature retrieved from signed image.
     606  @param[in]  AuthDataSize        Size of the Authenticode Signature in bytes.
     607 
    539608  @retval EFI_UNSUPPORTED             Hash algorithm is not supported.
    540609  @retval EFI_SUCCESS                 Hash successfully.
     
    543612EFI_STATUS
    544613HashPeImageByType (
    545   VOID
     614  IN UINT8              *AuthData,
     615  IN UINTN              AuthDataSize
    546616  )
    547617{
    548618  UINT8                     Index;
    549   WIN_CERTIFICATE_EFI_PKCS  *PkcsCertData;
    550 
    551   PkcsCertData = (WIN_CERTIFICATE_EFI_PKCS *) (mImageBase + mSecDataDir->VirtualAddress);
    552 
    553   if (PkcsCertData->Hdr.dwLength < sizeof (WIN_CERTIFICATE_EFI_PKCS) + 32) {
    554     return EFI_UNSUPPORTED;
    555   }
    556619
    557620  for (Index = 0; Index < HASHALG_MAX; Index++) {
     
    568631    //    Fixed offset (+32) is calculated based on two bytes of length encoding.
    569632    //
    570     if ((*(PkcsCertData->CertData + 1) & TWO_BYTE_ENCODE) != TWO_BYTE_ENCODE) {
     633    if ((*(AuthData + 1) & TWO_BYTE_ENCODE) != TWO_BYTE_ENCODE) {
    571634      //
    572635      // Only support two bytes of Long Form of Length Encoding.
     
    575638    }
    576639
    577     if (PkcsCertData->Hdr.dwLength < sizeof (WIN_CERTIFICATE_EFI_PKCS) + 32 + mHash[Index].OidLength) {
     640    if (AuthDataSize < 32 + mHash[Index].OidLength) {
    578641      return EFI_UNSUPPORTED;
    579642    }
    580643
    581     if (CompareMem (PkcsCertData->CertData + 32, mHash[Index].OidValue, mHash[Index].OidLength) == 0) {
     644    if (CompareMem (AuthData + 32, mHash[Index].OidValue, mHash[Index].OidLength) == 0) {
    582645      break;
    583646    }
     
    672735  if (Name != NULL) {
    673736    NameStringLen = StrSize (Name);
    674   }
    675 
    676   ImageExeInfoTable = NULL;
     737  } else {
     738    NameStringLen = sizeof (CHAR16);
     739  }
     740
    677741  EfiGetSystemConfigurationTable (&gEfiImageSecurityDatabaseGuid, (VOID **) &ImageExeInfoTable);
    678742  if (ImageExeInfoTable != NULL) {
    679743    //
    680744    // The table has been found!
    681     // We must enlarge the table to accmodate the new exe info entry.
     745    // We must enlarge the table to accomodate the new exe info entry.
    682746    //
    683747    ImageExeInfoTableSize = GetImageExeInfoTableSize (ImageExeInfoTable);
     
    691755
    692756  DevicePathSize            = GetDevicePathSize (DevicePath);
    693   NewImageExeInfoEntrySize  = sizeof (EFI_IMAGE_EXECUTION_INFO) + NameStringLen + DevicePathSize + SignatureSize;
     757  NewImageExeInfoEntrySize  = sizeof (EFI_IMAGE_EXECUTION_INFO) - sizeof (EFI_SIGNATURE_LIST) + NameStringLen + DevicePathSize + SignatureSize;
    694758  NewImageExeInfoTable      = (EFI_IMAGE_EXECUTION_INFO_TABLE *) AllocateRuntimePool (ImageExeInfoTableSize + NewImageExeInfoEntrySize);
    695759  if (NewImageExeInfoTable == NULL) {
     
    705769  ImageExeInfoEntry = (EFI_IMAGE_EXECUTION_INFO *) ((UINT8 *) NewImageExeInfoTable + ImageExeInfoTableSize);
    706770  //
    707   // Update new item's infomation.
    708   //
    709   WriteUnaligned32 ((UINT32 *) &ImageExeInfoEntry->Action, Action);
    710   WriteUnaligned32 ((UINT32 *) &ImageExeInfoEntry->InfoSize, (UINT32) NewImageExeInfoEntrySize);
     771  // Update new item's information.
     772  //
     773  WriteUnaligned32 ((UINT32 *) ImageExeInfoEntry, Action);
     774  WriteUnaligned32 ((UINT32 *) ((UINT8 *) ImageExeInfoEntry + sizeof (EFI_IMAGE_EXECUTION_ACTION)), (UINT32) NewImageExeInfoEntrySize);
    711775
    712776  if (Name != NULL) {
    713     CopyMem ((UINT8 *) &ImageExeInfoEntry->InfoSize + sizeof (UINT32), Name, NameStringLen);
     777    CopyMem ((UINT8 *) ImageExeInfoEntry + sizeof (EFI_IMAGE_EXECUTION_ACTION) + sizeof (UINT32), Name, NameStringLen);
     778  } else {
     779    ZeroMem ((UINT8 *) ImageExeInfoEntry + sizeof (EFI_IMAGE_EXECUTION_ACTION) + sizeof (UINT32), sizeof (CHAR16));
    714780  }
    715781  CopyMem (
    716     (UINT8 *) &ImageExeInfoEntry->InfoSize + sizeof (UINT32) + NameStringLen,
     782    (UINT8 *) ImageExeInfoEntry + sizeof (EFI_IMAGE_EXECUTION_ACTION) + sizeof (UINT32) + NameStringLen,
    717783    DevicePath,
    718784    DevicePathSize
     
    720786  if (Signature != NULL) {
    721787    CopyMem (
    722       (UINT8 *) &ImageExeInfoEntry->InfoSize + sizeof (UINT32) + NameStringLen + DevicePathSize,
     788      (UINT8 *) ImageExeInfoEntry + sizeof (EFI_IMAGE_EXECUTION_ACTION) + sizeof (UINT32) + NameStringLen + DevicePathSize,
    723789      Signature,
    724790      SignatureSize
     
    736802    FreePool (ImageExeInfoTable);
    737803  }
    738 }
    739 
    740 /**
    741   Discover if the UEFI image is authorized by user's policy setting.
    742 
    743   @param[in]    Policy            Specify platform's policy setting.
    744 
    745   @retval EFI_ACCESS_DENIED       Image is not allowed to run.
    746   @retval EFI_SECURITY_VIOLATION  Image is deferred.
    747   @retval EFI_SUCCESS             Image is authorized to run.
    748 
    749 **/
    750 EFI_STATUS
    751 ImageAuthorization (
    752   IN UINT32     Policy
    753   )
    754 {
    755   EFI_STATUS    Status;
    756   EFI_INPUT_KEY Key;
    757 
    758   Status = EFI_ACCESS_DENIED;
    759 
    760   switch (Policy) {
    761 
    762   case QUERY_USER_ON_SECURITY_VIOLATION:
    763     do {
    764       CreatePopUp (EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, &Key, mNotifyString1, mNotifyString2, NULL);
    765       if (Key.UnicodeChar == L'Y' || Key.UnicodeChar == L'y') {
    766         Status = EFI_SUCCESS;
    767         break;
    768       } else if (Key.UnicodeChar == L'N' || Key.UnicodeChar == L'n') {
    769         Status = EFI_ACCESS_DENIED;
    770         break;
    771       } else if (Key.UnicodeChar == L'D' || Key.UnicodeChar == L'd') {
    772         Status = EFI_SECURITY_VIOLATION;
    773         break;
    774       }
    775     } while (TRUE);
    776     break;
    777 
    778   case ALLOW_EXECUTE_ON_SECURITY_VIOLATION:
    779     Status = EFI_SUCCESS;
    780     break;
    781 
    782   case DEFER_EXECUTE_ON_SECURITY_VIOLATION:
    783     Status = EFI_SECURITY_VIOLATION;
    784     break;
    785 
    786   case DENY_EXECUTE_ON_SECURITY_VIOLATION:
    787     Status = EFI_ACCESS_DENIED;
    788     break;
    789   }
    790 
    791   return Status;
    792804}
    793805
     
    845857  CertList = (EFI_SIGNATURE_LIST *) Data;
    846858  while ((DataSize > 0) && (DataSize >= CertList->SignatureListSize)) {
    847     CertCount = (CertList->SignatureListSize - CertList->SignatureHeaderSize) / CertList->SignatureSize;
     859    CertCount = (CertList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - CertList->SignatureHeaderSize) / CertList->SignatureSize;
    848860    Cert      = (EFI_SIGNATURE_DATA *) ((UINT8 *) CertList + sizeof (EFI_SIGNATURE_LIST) + CertList->SignatureHeaderSize);
    849861    if ((CertList->SignatureSize == sizeof(EFI_SIGNATURE_DATA) - 1 + SignatureSize) && (CompareGuid(&CertList->SignatureType, CertType))) {
     
    854866          //
    855867          IsFound = TRUE;
     868          SecureBootHook (VariableName, &gEfiImageSecurityDatabaseGuid, CertList->SignatureSize, Cert);
    856869          break;
    857870        }
     
    881894  as EFI_SIGNATURE_LIST. The Variable may be PK, KEK, DB or DBX.
    882895
    883   @param VariableName  Name of Variable to search for Certificate.
    884   @param VendorGuid    Variable vendor GUID.
     896  @param[in]  AuthData      Pointer to the Authenticode Signature retrieved from signed image.
     897  @param[in]  AuthDataSize  Size of the Authenticode Signature in bytes.
     898  @param[in]  VariableName  Name of Variable to search for Certificate.
     899  @param[in]  VendorGuid    Variable vendor GUID.
    885900
    886901  @retval TRUE         Image pass verification.
     
    890905BOOLEAN
    891906IsPkcsSignedDataVerifiedBySignatureList (
     907  IN UINT8              *AuthData,
     908  IN UINTN              AuthDataSize,
    892909  IN CHAR16             *VariableName,
    893910  IN EFI_GUID           *VendorGuid
     
    896913  EFI_STATUS                Status;
    897914  BOOLEAN                   VerifyStatus;
    898   WIN_CERTIFICATE_EFI_PKCS  *PkcsCertData;
    899915  EFI_SIGNATURE_LIST        *CertList;
    900916  EFI_SIGNATURE_DATA        *Cert;
     
    912928  RootCertSize = 0;
    913929  VerifyStatus = FALSE;
    914   PkcsCertData = (WIN_CERTIFICATE_EFI_PKCS *) (mImageBase + mSecDataDir->VirtualAddress);
    915930
    916931  DataSize = 0;
     
    940955          //
    941956          RootCert      = Cert->SignatureData;
    942           RootCertSize  = CertList->SignatureSize;
     957          RootCertSize  = CertList->SignatureSize - sizeof (EFI_GUID);
    943958
    944959          //
     
    946961          //
    947962          VerifyStatus = AuthenticodeVerify (
    948                            PkcsCertData->CertData,
    949                            mSecDataDir->Size - sizeof(PkcsCertData->Hdr),
     963                           AuthData,
     964                           AuthDataSize,
    950965                           RootCert,
    951966                           RootCertSize,
     
    954969                           );
    955970          if (VerifyStatus) {
     971            SecureBootHook (VariableName, VendorGuid, CertList->SignatureSize, Cert);
    956972            goto Done;
    957973          }
     
    973989
    974990/**
    975   Verify certificate in WIN_CERT_TYPE_PKCS_SIGNED_DATA format.
    976 
    977   @retval EFI_SUCCESS                 Image pass verification.
    978   @retval EFI_SECURITY_VIOLATION      Image fail verification.
    979 
    980 **/
    981 EFI_STATUS
    982 VerifyCertPkcsSignedData (
    983   VOID
    984   )
    985 {
    986   //
    987   // 1: Find certificate from DBX forbidden database for revoked certificate.
    988   //
    989   if (IsPkcsSignedDataVerifiedBySignatureList (EFI_IMAGE_SECURITY_DATABASE1, &gEfiImageSecurityDatabaseGuid)) {
    990     //
    991     // DBX is forbidden database, if Authenticode verification pass with
    992     // one of the certificate in DBX, this image should be rejected.
    993     //
    994     return EFI_SECURITY_VIOLATION;
    995   }
    996 
    997   //
    998   // 2: Find certificate from KEK database and try to verify authenticode struct.
    999   //
    1000   if (IsPkcsSignedDataVerifiedBySignatureList (EFI_KEY_EXCHANGE_KEY_NAME, &gEfiGlobalVariableGuid)) {
    1001     return EFI_SUCCESS;
    1002   }
    1003 
    1004   //
    1005   // 3: Find certificate from DB database and try to verify authenticode struct.
    1006   //
    1007   if (IsPkcsSignedDataVerifiedBySignatureList (EFI_IMAGE_SECURITY_DATABASE, &gEfiImageSecurityDatabaseGuid)) {
    1008     return EFI_SUCCESS;
    1009   } else {
    1010     return EFI_SECURITY_VIOLATION;
    1011   }
    1012 }
    1013 
    1014 /**
    1015   Verify certificate in WIN_CERTIFICATE_UEFI_GUID format.
    1016 
    1017   @retval EFI_SUCCESS                 Image pass verification.
    1018   @retval EFI_SECURITY_VIOLATION      Image fail verification.
    1019   @retval other error value
    1020 
    1021 **/
    1022 EFI_STATUS
    1023 VerifyCertUefiGuid (
    1024   VOID
    1025   )
    1026 {
    1027   BOOLEAN                         Status;
    1028   WIN_CERTIFICATE_UEFI_GUID       *EfiCert;
    1029   EFI_SIGNATURE_LIST              *KekList;
    1030   EFI_SIGNATURE_DATA              *KekItem;
    1031   EFI_CERT_BLOCK_RSA_2048_SHA256  *CertBlock;
    1032   VOID                            *Rsa;
    1033   UINTN                           KekCount;
    1034   UINTN                           Index;
    1035   UINTN                           KekDataSize;
    1036   BOOLEAN                         IsFound;
    1037   EFI_STATUS                      Result;
    1038 
    1039   EfiCert   = NULL;
    1040   KekList   = NULL;
    1041   KekItem   = NULL;
    1042   CertBlock = NULL;
    1043   Rsa       = NULL;
    1044   Status    = FALSE;
    1045   IsFound   = FALSE;
    1046   KekDataSize = 0;
    1047 
    1048   EfiCert   = (WIN_CERTIFICATE_UEFI_GUID *) (mImageBase + mSecDataDir->VirtualAddress);
    1049   CertBlock = (EFI_CERT_BLOCK_RSA_2048_SHA256 *) EfiCert->CertData;
    1050   if (!CompareGuid (&EfiCert->CertType, &gEfiCertTypeRsa2048Sha256Guid)) {
    1051     //
    1052     // Invalid Certificate Data Type.
    1053     //
    1054     return EFI_SECURITY_VIOLATION;
    1055   }
    1056 
    1057   //
    1058   // Get KEK database variable data size
    1059   //
    1060   Result = gRT->GetVariable (EFI_KEY_EXCHANGE_KEY_NAME, &gEfiGlobalVariableGuid, NULL, &KekDataSize, NULL);
    1061   if (Result != EFI_BUFFER_TOO_SMALL) {
    1062     return EFI_SECURITY_VIOLATION;
    1063   }
    1064 
    1065   //
    1066   // Get KEK database variable.
    1067   //
    1068   KekList = GetEfiGlobalVariable (EFI_KEY_EXCHANGE_KEY_NAME);
    1069   if (KekList == NULL) {
    1070     return EFI_SECURITY_VIOLATION;
    1071   }
    1072 
    1073   //
    1074   // Enumerate all Kek items in this list to verify the variable certificate data.
    1075   // If anyone is authenticated successfully, it means the variable is correct!
    1076   //
    1077   while ((KekDataSize > 0) && (KekDataSize >= KekList->SignatureListSize)) {
    1078     if (CompareGuid (&KekList->SignatureType, &gEfiCertRsa2048Guid)) {
    1079       KekItem   = (EFI_SIGNATURE_DATA *) ((UINT8 *) KekList + sizeof (EFI_SIGNATURE_LIST) + KekList->SignatureHeaderSize);
    1080       KekCount  = (KekList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - KekList->SignatureHeaderSize) / KekList->SignatureSize;
    1081       for (Index = 0; Index < KekCount; Index++) {
    1082         if (CompareMem (KekItem->SignatureData, CertBlock->PublicKey, EFI_CERT_TYPE_RSA2048_SIZE) == 0) {
    1083           IsFound = TRUE;
    1084           break;
    1085         }
    1086         KekItem = (EFI_SIGNATURE_DATA *) ((UINT8 *) KekItem + KekList->SignatureSize);
    1087       }
    1088     }
    1089     KekDataSize -= KekList->SignatureListSize;
    1090     KekList = (EFI_SIGNATURE_LIST *) ((UINT8 *) KekList + KekList->SignatureListSize);
    1091   }
    1092 
    1093   if (!IsFound) {
    1094     //
    1095     // Signed key is not a trust one.
    1096     //
    1097     goto Done;
    1098   }
    1099 
    1100   //
    1101   // Now, we found the corresponding security policy.
    1102   // Verify the data payload.
    1103   //
    1104   Rsa = RsaNew ();
    1105   if (Rsa == NULL) {
    1106     Status = FALSE;
    1107     goto Done;
    1108   }
    1109 
    1110   //
    1111   // Set RSA Key Components.
    1112   // NOTE: Only N and E are needed to be set as RSA public key for signature verification.
    1113   //
    1114   Status = RsaSetKey (Rsa, RsaKeyN, CertBlock->PublicKey, EFI_CERT_TYPE_RSA2048_SIZE);
    1115   if (!Status) {
    1116     goto Done;
    1117   }
    1118   Status = RsaSetKey (Rsa, RsaKeyE, mRsaE, sizeof (mRsaE));
    1119   if (!Status) {
    1120     goto Done;
    1121   }
    1122   //
    1123   // Verify the signature.
    1124   //
    1125   Status = RsaPkcs1Verify (
    1126              Rsa,
    1127              mImageDigest,
    1128              mImageDigestSize,
    1129              CertBlock->Signature,
    1130              EFI_CERT_TYPE_RSA2048_SHA256_SIZE
    1131              );
    1132 
    1133 Done:
    1134   if (KekList != NULL) {
    1135     FreePool (KekList);
    1136   }
    1137   if (Rsa != NULL ) {
    1138     RsaFree (Rsa);
    1139   }
    1140   if (Status) {
    1141     return EFI_SUCCESS;
    1142   } else {
    1143     return EFI_SECURITY_VIOLATION;
    1144   }
    1145 }
    1146 
    1147 /**
    1148991  Provide verification service for signed images, which include both signature validation
    1149992  and platform policy control. For signature types, both UEFI WIN_CERTIFICATE_UEFI_GUID and
     
    1153996  Executables from FV is bypass, so pass in AuthenticationStatus is ignored.
    1154997
    1155   The image verification process is:
    1156     Is the Image signed?
    1157       If yes,
    1158         Does the image verify against a certificate (root or intermediate) in the allowed db?
    1159           Run it
    1160         Image verification fail
    1161           Is the Image's Hash not in forbidden database and the Image's Hash in allowed db?
    1162             Run it
    1163       If no,
    1164         Is the Image's Hash in the forbidden database (DBX)?
    1165           if yes,
    1166             Error out
    1167         Is the Image's Hash in the allowed database (DB)?
    1168           If yes,
    1169             Run it
    1170           If no,
    1171             Error out
     998  The image verification policy is:
     999    If the image is signed,
     1000      At least one valid signature or at least one hash value of the image must match a record
     1001      in the security database "db", and no valid signature nor any hash value of the image may
     1002      be reflected in the security database "dbx".
     1003    Otherwise, the image is not signed,
     1004      The SHA256 hash value of the image must match a record in the security database "db", and
     1005      not be reflected in the security data base "dbx".
     1006
     1007  Caution: This function may receive untrusted input.
     1008  PE/COFF image is external input, so this function will validate its data structure
     1009  within this image buffer before use.
    11721010
    11731011  @param[in]    AuthenticationStatus
     
    11781016  @param[in]    FileBuffer File buffer matches the input file device path.
    11791017  @param[in]    FileSize   Size of File buffer matches the input file device path.
    1180 
    1181   @retval EFI_SUCCESS            The file specified by File did authenticate, and the
    1182                                  platform policy dictates that the DXE Core may use File.
    1183   @retval EFI_INVALID_PARAMETER  Input argument is incorrect.
     1018  @param[in]    BootPolicy A boot policy that was used to call LoadImage() UEFI service.
     1019
     1020  @retval EFI_SUCCESS            The file specified by DevicePath and non-NULL
     1021                                 FileBuffer did authenticate, and the platform policy dictates
     1022                                 that the DXE Foundation may use the file.
     1023  @retval EFI_SUCCESS            The device path specified by NULL device path DevicePath
     1024                                 and non-NULL FileBuffer did authenticate, and the platform
     1025                                 policy dictates that the DXE Foundation may execute the image in
     1026                                 FileBuffer.
    11841027  @retval EFI_OUT_RESOURCE       Fail to allocate memory.
    11851028  @retval EFI_SECURITY_VIOLATION The file specified by File did not authenticate, and
    11861029                                 the platform policy dictates that File should be placed
    1187                                  in the untrusted state. A file may be promoted from
    1188                                  the untrusted to the trusted state at a future time
    1189                                  with a call to the Trust() DXE Service.
    1190   @retval EFI_ACCESS_DENIED      The file specified by File did not authenticate, and
    1191                                  the platform policy dictates that File should not be
    1192                                  used for any purpose.
     1030                                 in the untrusted state. The image has been added to the file
     1031                                 execution table.
     1032  @retval EFI_ACCESS_DENIED      The file specified by File and FileBuffer did not
     1033                                 authenticate, and the platform policy dictates that the DXE
     1034                                 Foundation many not use File.
    11931035
    11941036**/
     
    11991041  IN  CONST EFI_DEVICE_PATH_PROTOCOL   *File,
    12001042  IN  VOID                             *FileBuffer,
    1201   IN  UINTN                            FileSize
     1043  IN  UINTN                            FileSize,
     1044  IN  BOOLEAN                          BootPolicy
    12021045  )
    12031046{
     
    12061049  EFI_IMAGE_DOS_HEADER                 *DosHdr;
    12071050  EFI_STATUS                           VerifyStatus;
    1208   UINT8                                *SetupMode;
    12091051  EFI_SIGNATURE_LIST                   *SignatureList;
    12101052  UINTN                                SignatureListSize;
     
    12131055  WIN_CERTIFICATE                      *WinCertificate;
    12141056  UINT32                               Policy;
    1215   UINT8                                *SecureBootEnable;
     1057  UINT8                                *SecureBoot;
    12161058  PE_COFF_LOADER_IMAGE_CONTEXT         ImageContext;
    12171059  UINT32                               NumberOfRvaAndSizes;
    1218   UINT32                               CertSize;
    1219 
    1220   if (File == NULL) {
    1221     return EFI_INVALID_PARAMETER;
    1222   }
     1060  WIN_CERTIFICATE_EFI_PKCS             *PkcsCertData;
     1061  WIN_CERTIFICATE_UEFI_GUID            *WinCertUefiGuid;
     1062  UINT8                                *AuthData;
     1063  UINTN                                AuthDataSize;
     1064  EFI_IMAGE_DATA_DIRECTORY             *SecDataDir;
     1065  UINT32                               OffSet;
     1066  CHAR16                               *NameStr;
    12231067
    12241068  SignatureList     = NULL;
    12251069  SignatureListSize = 0;
    12261070  WinCertificate    = NULL;
     1071  SecDataDir        = NULL;
     1072  PkcsCertData      = NULL;
    12271073  Action            = EFI_IMAGE_EXECUTION_AUTH_UNTESTED;
    12281074  Status            = EFI_ACCESS_DENIED;
     1075  VerifyStatus      = EFI_ACCESS_DENIED;
     1076
    12291077  //
    12301078  // Check the image type and get policy setting.
     
    12611109  }
    12621110
    1263   SecureBootEnable = GetVariable (EFI_SECURE_BOOT_ENABLE_NAME, &gEfiSecureBootEnableDisableGuid);
    1264   //
    1265   // Skip verification if SecureBootEnable variable doesn't exist.
    1266   //
    1267   if (SecureBootEnable == NULL) {
     1111  //
     1112  // The policy QUERY_USER_ON_SECURITY_VIOLATION and ALLOW_EXECUTE_ON_SECURITY_VIOLATION
     1113  // violates the UEFI spec and has been removed.
     1114  //
     1115  ASSERT (Policy != QUERY_USER_ON_SECURITY_VIOLATION && Policy != ALLOW_EXECUTE_ON_SECURITY_VIOLATION);
     1116  if (Policy == QUERY_USER_ON_SECURITY_VIOLATION || Policy == ALLOW_EXECUTE_ON_SECURITY_VIOLATION) {
     1117    CpuDeadLoop ();
     1118  }
     1119
     1120  GetEfiGlobalVariable2 (EFI_SECURE_BOOT_MODE_NAME, (VOID**)&SecureBoot, NULL);
     1121  //
     1122  // Skip verification if SecureBoot variable doesn't exist.
     1123  //
     1124  if (SecureBoot == NULL) {
    12681125    return EFI_SUCCESS;
    12691126  }
    12701127
    12711128  //
    1272   // Skip verification if SecureBootEnable is disabled.
    1273   //
    1274   if (*SecureBootEnable == SECURE_BOOT_DISABLE) {
    1275     FreePool (SecureBootEnable);
     1129  // Skip verification if SecureBoot is disabled.
     1130  //
     1131  if (*SecureBoot == SECURE_BOOT_MODE_DISABLE) {
     1132    FreePool (SecureBoot);
    12761133    return EFI_SUCCESS;
    12771134  }
    1278 
    1279   FreePool (SecureBootEnable);
    1280 
    1281   SetupMode = GetEfiGlobalVariable (EFI_SETUP_MODE_NAME);
    1282 
    1283   //
    1284   // SetupMode doesn't exist means no AuthVar driver is dispatched,
    1285   // skip verification.
    1286   //
    1287   if (SetupMode == NULL) {
    1288     return EFI_SUCCESS;
    1289   }
    1290 
    1291   //
    1292   // If platform is in SETUP MODE, skip verification.
    1293   //
    1294   if (*SetupMode == SETUP_MODE) {
    1295     FreePool (SetupMode);
    1296     return EFI_SUCCESS;
    1297   }
    1298 
    1299   FreePool (SetupMode);
     1135  FreePool (SecureBoot);
    13001136
    13011137  //
     
    13471183  }
    13481184
    1349   Magic = mNtHeader.Pe32->OptionalHeader.Magic;
     1185  if (mNtHeader.Pe32->FileHeader.Machine == IMAGE_FILE_MACHINE_IA64 && mNtHeader.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
     1186    //
     1187    // NOTE: Some versions of Linux ELILO for Itanium have an incorrect magic value
     1188    //       in the PE/COFF Header. If the MachineType is Itanium(IA64) and the
     1189    //       Magic value in the OptionalHeader is EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
     1190    //       then override the magic value to EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
     1191    //
     1192    Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;
     1193  } else {
     1194    //
     1195    // Get the magic value from the PE/COFF Optional Header
     1196    //
     1197    Magic = mNtHeader.Pe32->OptionalHeader.Magic;
     1198  }
     1199 
    13501200  if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
    13511201    //
     
    13541204    NumberOfRvaAndSizes = mNtHeader.Pe32->OptionalHeader.NumberOfRvaAndSizes;
    13551205    if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_SECURITY) {
    1356       mSecDataDir = (EFI_IMAGE_DATA_DIRECTORY *) &mNtHeader.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY];
     1206      SecDataDir = (EFI_IMAGE_DATA_DIRECTORY *) &mNtHeader.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY];
    13571207    }       
    13581208  } else {
     
    13621212    NumberOfRvaAndSizes = mNtHeader.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;
    13631213    if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_SECURITY) {
    1364       mSecDataDir = (EFI_IMAGE_DATA_DIRECTORY *) &mNtHeader.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY];
    1365     }
    1366   }
    1367 
    1368   if ((mSecDataDir == NULL) || ((mSecDataDir != NULL) && (mSecDataDir->Size == 0))) {
    1369     //
    1370     // This image is not signed.
     1214      SecDataDir = (EFI_IMAGE_DATA_DIRECTORY *) &mNtHeader.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY];
     1215    }
     1216  }
     1217
     1218  //
     1219  // Start Image Validation.
     1220  //
     1221  if (SecDataDir == NULL || SecDataDir->Size == 0) {
     1222    //
     1223    // This image is not signed. The SHA256 hash value of the image must match a record in the security database "db",
     1224    // and not be reflected in the security data base "dbx".
    13711225    //
    13721226    if (!HashPeImage (HASHALG_SHA256)) {
     
    13951249
    13961250  //
    1397   // Verify signature of executables.
    1398   //
    1399   WinCertificate = (WIN_CERTIFICATE *) (mImageBase + mSecDataDir->VirtualAddress);
    1400 
    1401   CertSize = sizeof (WIN_CERTIFICATE);
    1402 
    1403   if ((mSecDataDir->Size <= CertSize) || (mSecDataDir->Size < WinCertificate->dwLength)) {
    1404     goto Done;
    1405   }
    1406 
    1407   switch (WinCertificate->wCertificateType) {
    1408 
    1409   case WIN_CERT_TYPE_EFI_GUID:
    1410     CertSize = sizeof (WIN_CERTIFICATE_UEFI_GUID) + sizeof (EFI_CERT_BLOCK_RSA_2048_SHA256) - sizeof (UINT8);
    1411     if (WinCertificate->dwLength < CertSize) {
    1412       goto Done;
    1413     }
    1414 
    1415     //
    1416     // Verify UEFI GUID type.
    1417     //
    1418     if (!HashPeImage (HASHALG_SHA256)) {
    1419       goto Done;
    1420     }
    1421 
    1422     VerifyStatus = VerifyCertUefiGuid ();
    1423     break;
    1424 
    1425   case WIN_CERT_TYPE_PKCS_SIGNED_DATA:
    1426     //
    1427     // Verify Pkcs signed data type.
    1428     //
    1429     Status = HashPeImageByType();
     1251  // Verify the signature of the image, multiple signatures are allowed as per PE/COFF Section 4.7
     1252  // "Attribute Certificate Table".
     1253  // The first certificate starts at offset (SecDataDir->VirtualAddress) from the start of the file.
     1254  //
     1255  for (OffSet = SecDataDir->VirtualAddress;
     1256       OffSet < (SecDataDir->VirtualAddress + SecDataDir->Size);
     1257       OffSet += (WinCertificate->dwLength + ALIGN_SIZE (WinCertificate->dwLength))) {
     1258    WinCertificate = (WIN_CERTIFICATE *) (mImageBase + OffSet);
     1259    if ((SecDataDir->VirtualAddress + SecDataDir->Size - OffSet) <= sizeof (WIN_CERTIFICATE) ||
     1260        (SecDataDir->VirtualAddress + SecDataDir->Size - OffSet) < WinCertificate->dwLength) {
     1261      break;
     1262    }
     1263   
     1264    //
     1265    // Verify the image's Authenticode signature, only DER-encoded PKCS#7 signed data is supported.
     1266    //
     1267    if (WinCertificate->wCertificateType == WIN_CERT_TYPE_PKCS_SIGNED_DATA) {
     1268      //
     1269      // The certificate is formatted as WIN_CERTIFICATE_EFI_PKCS which is described in the
     1270      // Authenticode specification.
     1271      //
     1272      PkcsCertData = (WIN_CERTIFICATE_EFI_PKCS *) WinCertificate;
     1273      if (PkcsCertData->Hdr.dwLength <= sizeof (PkcsCertData->Hdr)) {
     1274        break;
     1275      }
     1276      AuthData   = PkcsCertData->CertData;
     1277      AuthDataSize = PkcsCertData->Hdr.dwLength - sizeof(PkcsCertData->Hdr);
     1278    } else if (WinCertificate->wCertificateType == WIN_CERT_TYPE_EFI_GUID) {
     1279      //
     1280      // The certificate is formatted as WIN_CERTIFICATE_UEFI_GUID which is described in UEFI Spec.
     1281      //
     1282      WinCertUefiGuid = (WIN_CERTIFICATE_UEFI_GUID *) WinCertificate;
     1283      if (WinCertUefiGuid->Hdr.dwLength <= OFFSET_OF(WIN_CERTIFICATE_UEFI_GUID, CertData)) {
     1284        break;
     1285      }
     1286      if (!CompareGuid (&WinCertUefiGuid->CertType, &gEfiCertPkcs7Guid)) {
     1287        continue;
     1288      }
     1289      AuthData = WinCertUefiGuid->CertData;
     1290      AuthDataSize = WinCertUefiGuid->Hdr.dwLength - OFFSET_OF(WIN_CERTIFICATE_UEFI_GUID, CertData);
     1291    } else {
     1292      if (WinCertificate->dwLength < sizeof (WIN_CERTIFICATE)) {
     1293        break;
     1294      }
     1295      continue;
     1296    }
     1297
     1298    Status = HashPeImageByType (AuthData, AuthDataSize);
    14301299    if (EFI_ERROR (Status)) {
    1431       goto Done;
    1432     }
    1433 
    1434     VerifyStatus = VerifyCertPkcsSignedData ();
    1435 
    1436     //
    1437     // For image verification against enrolled certificate(root or intermediate),
    1438     // no need to check image's hash in the allowed database.
    1439     //
    1440     if (!EFI_ERROR (VerifyStatus)) {
    1441       if (!IsSignatureFoundInDatabase (EFI_IMAGE_SECURITY_DATABASE1, mImageDigest, &mCertType, mImageDigestSize)) {
    1442         return EFI_SUCCESS;
    1443       }
    1444     }
    1445     break;
    1446 
    1447   default:
    1448     goto Done;
    1449   }
    1450   //
    1451   // Get image hash value as executable's signature.
    1452   //
    1453   SignatureListSize = sizeof (EFI_SIGNATURE_LIST) + sizeof (EFI_SIGNATURE_DATA) - 1 + mImageDigestSize;
    1454   SignatureList     = (EFI_SIGNATURE_LIST *) AllocateZeroPool (SignatureListSize);
    1455   if (SignatureList == NULL) {
    1456     Status = EFI_OUT_OF_RESOURCES;
    1457     goto Done;
    1458   }
    1459   SignatureList->SignatureHeaderSize  = 0;
    1460   SignatureList->SignatureListSize    = (UINT32) SignatureListSize;
    1461   SignatureList->SignatureSize        = (UINT32) mImageDigestSize;
    1462   CopyMem (&SignatureList->SignatureType, &mCertType, sizeof (EFI_GUID));
    1463   Signature = (EFI_SIGNATURE_DATA *) ((UINT8 *) SignatureList + sizeof (EFI_SIGNATURE_LIST));
    1464   CopyMem (Signature->SignatureData, mImageDigest, mImageDigestSize);
    1465   //
    1466   // Signature database check after verification.
    1467   //
    1468   if (EFI_ERROR (VerifyStatus)) {
    1469     //
    1470     // Verification failure.
    1471     //
    1472     if (!IsSignatureFoundInDatabase (EFI_IMAGE_SECURITY_DATABASE1, mImageDigest, &mCertType, mImageDigestSize) &&
    1473         IsSignatureFoundInDatabase (EFI_IMAGE_SECURITY_DATABASE, mImageDigest, &mCertType, mImageDigestSize)) {
    1474       //
    1475       // Verification fail, Image Hash is not in forbidden database (DBX),
    1476       // and Image Hash is in allowed database (DB).
    1477       //
    1478       Status = EFI_SUCCESS;
    1479     } else {
     1300      continue;
     1301    }
     1302   
     1303    //
     1304    // Check the digital signature against the revoked certificate in forbidden database (dbx).
     1305    //
     1306    if (IsPkcsSignedDataVerifiedBySignatureList (AuthData, AuthDataSize, EFI_IMAGE_SECURITY_DATABASE1, &gEfiImageSecurityDatabaseGuid)) {
    14801307      Action = EFI_IMAGE_EXECUTION_AUTH_SIG_FAILED;
    1481       Status = EFI_ACCESS_DENIED;
    1482     }
    1483   } else if (IsSignatureFoundInDatabase (EFI_IMAGE_SECURITY_DATABASE1, Signature->SignatureData, &mCertType, mImageDigestSize)) {
    1484     //
    1485     // Executable signature verification passes, but is found in forbidden signature database.
    1486     //
    1487     Action = EFI_IMAGE_EXECUTION_AUTH_SIG_FOUND;
     1308      VerifyStatus = EFI_ACCESS_DENIED;
     1309      break;
     1310    }
     1311
     1312    //
     1313    // Check the digital signature against the valid certificate in allowed database (db).
     1314    //
     1315    if (EFI_ERROR (VerifyStatus)) {
     1316      if (IsPkcsSignedDataVerifiedBySignatureList (AuthData, AuthDataSize, EFI_IMAGE_SECURITY_DATABASE, &gEfiImageSecurityDatabaseGuid)) {
     1317        VerifyStatus = EFI_SUCCESS;
     1318      }
     1319    }
     1320
     1321    //
     1322    // Check the image's hash value.
     1323    //
     1324    if (IsSignatureFoundInDatabase (EFI_IMAGE_SECURITY_DATABASE1, mImageDigest, &mCertType, mImageDigestSize)) {
     1325      Action = EFI_IMAGE_EXECUTION_AUTH_SIG_FOUND;
     1326      VerifyStatus = EFI_ACCESS_DENIED;
     1327      break;
     1328    } else if (EFI_ERROR (VerifyStatus)) {
     1329      if (IsSignatureFoundInDatabase (EFI_IMAGE_SECURITY_DATABASE, mImageDigest, &mCertType, mImageDigestSize)) {
     1330        VerifyStatus = EFI_SUCCESS;
     1331      }
     1332    }
     1333  }
     1334
     1335  if (OffSet != (SecDataDir->VirtualAddress + SecDataDir->Size)) {
     1336    //
     1337    // The Size in Certificate Table or the attribute certicate table is corrupted.
     1338    //
     1339    VerifyStatus = EFI_ACCESS_DENIED;
     1340  }
     1341 
     1342  if (!EFI_ERROR (VerifyStatus)) {
     1343    return EFI_SUCCESS;
     1344  } else {
    14881345    Status = EFI_ACCESS_DENIED;
    1489   } else if (IsSignatureFoundInDatabase (EFI_IMAGE_SECURITY_DATABASE, Signature->SignatureData, &mCertType, mImageDigestSize)) {
    1490     //
    1491     // Executable signature is found in authorized signature database.
    1492     //
    1493     Status = EFI_SUCCESS;
    1494   } else {
    1495     //
    1496     // Executable signature verification passes, but cannot be found in authorized signature database.
    1497     // Get platform policy to determine the action.
    1498     //
    1499     Action = EFI_IMAGE_EXECUTION_AUTH_SIG_PASSED;
    1500     Status = ImageAuthorization (Policy);
     1346    if (Action == EFI_IMAGE_EXECUTION_AUTH_SIG_FAILED || Action == EFI_IMAGE_EXECUTION_AUTH_SIG_FOUND) {
     1347      //
     1348      // Get image hash value as executable's signature.
     1349      //
     1350      SignatureListSize = sizeof (EFI_SIGNATURE_LIST) + sizeof (EFI_SIGNATURE_DATA) - 1 + mImageDigestSize;
     1351      SignatureList     = (EFI_SIGNATURE_LIST *) AllocateZeroPool (SignatureListSize);
     1352      if (SignatureList == NULL) {
     1353        Status = EFI_OUT_OF_RESOURCES;
     1354        goto Done;
     1355      }
     1356      SignatureList->SignatureHeaderSize  = 0;
     1357      SignatureList->SignatureListSize    = (UINT32) SignatureListSize;
     1358      SignatureList->SignatureSize        = (UINT32) mImageDigestSize;
     1359      CopyMem (&SignatureList->SignatureType, &mCertType, sizeof (EFI_GUID));
     1360      Signature = (EFI_SIGNATURE_DATA *) ((UINT8 *) SignatureList + sizeof (EFI_SIGNATURE_LIST));
     1361      CopyMem (Signature->SignatureData, mImageDigest, mImageDigestSize);
     1362    }
    15011363  }
    15021364
     
    15061368    // Policy decides to defer or reject the image; add its information in image executable information table.
    15071369    //
    1508     AddImageExeInfo (Action, NULL, File, SignatureList, SignatureListSize);
     1370    NameStr = ConvertDevicePathToText (File, FALSE, TRUE);
     1371    AddImageExeInfo (Action, NameStr, File, SignatureList, SignatureListSize);
     1372    if (NameStr != NULL) {
     1373      DEBUG((EFI_D_INFO, "The image doesn't pass verification: %s\n", NameStr));
     1374      FreePool(NameStr);
     1375    }
     1376    Status = EFI_SECURITY_VIOLATION;
    15091377  }
    15101378
     
    15171385
    15181386/**
    1519   When VariableWriteArchProtocol install, create "SecureBoot" variable.
    1520 
    1521   @param[in] Event    Event whose notification function is being invoked.
    1522   @param[in] Context  Pointer to the notification function's context.
     1387  On Ready To Boot Services Event notification handler.
     1388
     1389  Add the image execution information table if it is not in system configuration table.
     1390
     1391  @param[in]  Event     Event whose notification function is being invoked
     1392  @param[in]  Context   Pointer to the notification function's context
    15231393
    15241394**/
    15251395VOID
    15261396EFIAPI
    1527 VariableWriteCallBack (
    1528   IN  EFI_EVENT                           Event,
    1529   IN  VOID                                *Context
     1397OnReadyToBoot (
     1398  IN      EFI_EVENT               Event,
     1399  IN      VOID                    *Context
    15301400  )
    15311401{
    1532   UINT8                       SecureBootMode;
    1533   UINT8                       *SecureBootModePtr;
    1534   EFI_STATUS                  Status;
    1535   VOID                        *ProtocolPointer;
    1536 
    1537   Status = gBS->LocateProtocol (&gEfiVariableWriteArchProtocolGuid, NULL, &ProtocolPointer);
    1538   if (EFI_ERROR (Status)) {
     1402  EFI_IMAGE_EXECUTION_INFO_TABLE  *ImageExeInfoTable;
     1403  UINTN                           ImageExeInfoTableSize;
     1404
     1405  EfiGetSystemConfigurationTable (&gEfiImageSecurityDatabaseGuid, (VOID **) &ImageExeInfoTable);
     1406  if (ImageExeInfoTable != NULL) {
    15391407    return;
    15401408  }
    15411409
    1542   //
    1543   // Check whether "SecureBoot" variable exists.
    1544   // If this library is built-in, it means firmware has capability to perform
    1545   // driver signing verification.
    1546   //
    1547   SecureBootModePtr = GetEfiGlobalVariable (EFI_SECURE_BOOT_MODE_NAME);
    1548   if (SecureBootModePtr == NULL) {
    1549     SecureBootMode   = SECURE_BOOT_MODE_DISABLE;
    1550     //
    1551     // Authenticated variable driver will update "SecureBoot" depending on SetupMode variable.
    1552     //
    1553     gRT->SetVariable (
    1554            EFI_SECURE_BOOT_MODE_NAME,
    1555            &gEfiGlobalVariableGuid,
    1556            EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
    1557            sizeof (UINT8),
    1558            &SecureBootMode
    1559            );
    1560   } else {
    1561     FreePool (SecureBootModePtr);
    1562   }
     1410  ImageExeInfoTableSize = sizeof (EFI_IMAGE_EXECUTION_INFO_TABLE);
     1411  ImageExeInfoTable     = (EFI_IMAGE_EXECUTION_INFO_TABLE *) AllocateRuntimePool (ImageExeInfoTableSize);
     1412  if (ImageExeInfoTable == NULL) {
     1413    return ;
     1414  }
     1415
     1416  ImageExeInfoTable->NumberOfImages = 0; 
     1417  gBS->InstallConfigurationTable (&gEfiImageSecurityDatabaseGuid, (VOID *) ImageExeInfoTable);
     1418
    15631419}
    15641420
     
    15781434  )
    15791435{
    1580   VOID                *Registration;
    1581 
    1582   //
    1583   // Register callback function upon VariableWriteArchProtocol.
    1584   //
    1585   EfiCreateProtocolNotifyEvent (
    1586     &gEfiVariableWriteArchProtocolGuid,
     1436  EFI_EVENT            Event;
     1437
     1438  //
     1439  // Register the event to publish the image execution table.
     1440  //
     1441  EfiCreateEventReadyToBootEx (
    15871442    TPL_CALLBACK,
    1588     VariableWriteCallBack,
    1589     NULL,
    1590     &Registration
    1591     );
    1592 
    1593   return RegisterSecurityHandler (
     1443    OnReadyToBoot,
     1444    NULL, 
     1445    &Event
     1446    ); 
     1447
     1448  return RegisterSecurity2Handler (
    15941449          DxeImageVerificationHandler,
    15951450          EFI_AUTH_OPERATION_VERIFY_IMAGE | EFI_AUTH_OPERATION_IMAGE_REQUIRED
  • trunk/src/VBox/Devices/EFI/Firmware/SecurityPkg/Library/DxeImageVerificationLib/DxeImageVerificationLib.h

    r48674 r58459  
    33  internal structure and functions used by ImageVerificationLib.
    44
    5 Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>
     5Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.<BR>
    66This program and the accompanying materials
    77are licensed and made available under the terms and conditions of the BSD License
     
    4444#define TWO_BYTE_ENCODE                   0x82
    4545
     46#define ALIGNMENT_SIZE                    8
     47#define ALIGN_SIZE(a) (((a) % ALIGNMENT_SIZE) ? ALIGNMENT_SIZE - ((a) % ALIGNMENT_SIZE) : 0)
     48
    4649//
    4750// Image type definitions
  • trunk/src/VBox/Devices/EFI/Firmware/SecurityPkg/Library/DxeImageVerificationLib/DxeImageVerificationLib.inf

    r48674 r58459  
    11## @file
    2 #  The library instance provides security service of image verification.
    3 #  Image verification Library module supports UEFI2.3.1
     2#  Provides security service of image verification
    43#
    5 # Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>
     4#  This library hooks LoadImage() API to verify every image by the verification policy.
     5#
     6#  Caution: This module requires additional review when modified.
     7#  This library will have external input - PE/COFF image.
     8#  This external input must be validated carefully to avoid security issues such as
     9#  buffer overflow or integer overflow.
     10#
     11# Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
    612# This program and the accompanying materials
    713# are licensed and made available under the terms and conditions of the BSD License
     
    1521[Defines]
    1622  INF_VERSION                    = 0x00010005
    17   BASE_NAME                      = DxeImageVerificationLib   
     23  BASE_NAME                      = DxeImageVerificationLib
     24  MODULE_UNI_FILE                = DxeImageVerificationLib.uni
    1825  FILE_GUID                      = 0CA970E1-43FA-4402-BC0A-81AF336BFFD6
    1926  MODULE_TYPE                    = DXE_DRIVER
     
    3138  DxeImageVerificationLib.c
    3239  DxeImageVerificationLib.h
     40  Measurement.c
    3341
    3442[Packages]
     
    5058  SecurityManagementLib
    5159  PeCoffLib
     60  TpmMeasurementLib
    5261
    5362[Protocols]
    54   gEfiFirmwareVolume2ProtocolGuid
    55   gEfiBlockIoProtocolGuid
    56   gEfiSimpleFileSystemProtocolGuid
    57   gEfiVariableWriteArchProtocolGuid
     63  gEfiFirmwareVolume2ProtocolGuid       ## SOMETIMES_CONSUMES
     64  gEfiBlockIoProtocolGuid               ## SOMETIMES_CONSUMES
     65  gEfiSimpleFileSystemProtocolGuid      ## SOMETIMES_CONSUMES
     66
     67[Guids]
     68  ## SOMETIMES_CONSUMES   ## Variable:L"DB"
     69  ## SOMETIMES_CONSUMES   ## Variable:L"DBX"
     70  ## PRODUCES             ## SystemTable
     71  ## CONSUMES             ## SystemTable
     72  gEfiImageSecurityDatabaseGuid
     73
     74  ## SOMETIMES_CONSUMES   ## GUID       # Unique ID for the type of the signature.
     75  ## SOMETIMES_PRODUCES   ## GUID       # Unique ID for the type of the signature.
     76  gEfiCertSha1Guid
    5877 
    59 [Guids]
    60   gEfiCertTypeRsa2048Sha256Guid
    61   gEfiImageSecurityDatabaseGuid
    62   gEfiCertSha1Guid
     78  ## SOMETIMES_CONSUMES   ## GUID       # Unique ID for the type of the signature.
     79  ## SOMETIMES_PRODUCES   ## GUID       # Unique ID for the type of the signature.
    6380  gEfiCertSha256Guid
    64   gEfiCertX509Guid
    65   gEfiCertRsa2048Guid
    66   gEfiSecureBootEnableDisableGuid
     81
     82  gEfiCertX509Guid                      ## SOMETIMES_CONSUMES    ## GUID     # Unique ID for the type of the signature.
     83  gEfiCertPkcs7Guid                     ## SOMETIMES_CONSUMES    ## GUID     # Unique ID for the type of the certificate.
    6784 
    6885[Pcd]
    69   gEfiSecurityPkgTokenSpaceGuid.PcdOptionRomImageVerificationPolicy
    70   gEfiSecurityPkgTokenSpaceGuid.PcdRemovableMediaImageVerificationPolicy
    71   gEfiSecurityPkgTokenSpaceGuid.PcdFixedMediaImageVerificationPolicy
    72 
     86  gEfiSecurityPkgTokenSpaceGuid.PcdOptionRomImageVerificationPolicy          ## SOMETIMES_CONSUMES
     87  gEfiSecurityPkgTokenSpaceGuid.PcdRemovableMediaImageVerificationPolicy     ## SOMETIMES_CONSUMES
     88  gEfiSecurityPkgTokenSpaceGuid.PcdFixedMediaImageVerificationPolicy         ## SOMETIMES_CONSUMES
    7389 
    74 
    75 
  • trunk/src/VBox/Devices/EFI/Firmware/SecurityPkg/Library/DxeTcgPhysicalPresenceLib/DxeTcgPhysicalPresenceLib.c

    r48674 r58459  
    33  Execute pending TPM requests from OS or BIOS and Lock TPM.
    44
    5 Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>
     5  Caution: This module requires additional review when modified.
     6  This driver will have external input - variable.
     7  This external input must be validated carefully to avoid security issue.
     8
     9  ExecutePendingTpmRequest() will receive untrusted input and do validation.
     10
     11Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>
    612This program and the accompanying materials
    713are licensed and made available under the terms and conditions of the BSD License
     
    1723
    1824#include <Protocol/TcgService.h>
     25#include <Protocol/VariableLock.h>
    1926#include <Library/DebugLib.h>
    2027#include <Library/BaseMemoryLib.h>
     
    2835#include <Guid/EventGroup.h>
    2936#include <Guid/PhysicalPresenceData.h>
    30 
    31 #define TPM_PP_USER_ABORT           ((TPM_RESULT)(-0x10))
    32 #define TPM_PP_BIOS_FAILURE         ((TPM_RESULT)(-0x0f))
     37#include <Library/TcgPpVendorLib.h>
     38
    3339#define CONFIRM_BUFFER_SIZE         4096
    3440
     
    178184  @param[in] AdditionalParameters     Pointer to the Additional paramaters. 
    179185 
    180   @retval TPM_PP_BIOS_FAILURE         Error occurred during sending command to TPM or
    181                                       receiving response from TPM.
    182   @retval Others                      Return code from the TPM device after command execution.
     186  @retval TCG_PP_OPERATION_RESPONSE_BIOS_FAILURE  Error occurred during sending command to TPM or
     187                                                  receiving response from TPM.
     188  @retval Others                                  Return code from the TPM device after command execution.
    183189
    184190**/
    185 TPM_RESULT
     191UINT32
    186192TpmCommandNoReturnData (
    187193  IN      EFI_TCG_PROTOCOL          *TcgProtocol,
     
    198204  TpmRqu = (TPM_RQU_COMMAND_HDR*) AllocatePool (sizeof (*TpmRqu) + AdditionalParameterSize);
    199205  if (TpmRqu == NULL) {
    200     return TPM_PP_BIOS_FAILURE;
     206    return TCG_PP_OPERATION_RESPONSE_BIOS_FAILURE;
    201207  }
    202208
     
    216222  FreePool (TpmRqu);
    217223  if (EFI_ERROR (Status) || (TpmRsp.tag != SwapBytes16 (TPM_TAG_RSP_COMMAND))) {
    218     return TPM_PP_BIOS_FAILURE;
     224    return TCG_PP_OPERATION_RESPONSE_BIOS_FAILURE;
    219225  }
    220226  return SwapBytes32 (TpmRsp.returnCode);
     
    228234  @param[in, out] PpiFlags            The physical presence interface flags.
    229235 
    230   @retval TPM_PP_BIOS_FAILURE         Unknown physical presence operation.
    231   @retval TPM_PP_BIOS_FAILURE         Error occurred during sending command to TPM or
    232                                       receiving response from TPM.
    233   @retval Others                      Return code from the TPM device after command execution.
     236  @retval TCG_PP_OPERATION_RESPONSE_BIOS_FAILURE  Unknown physical presence operation.
     237  @retval TCG_PP_OPERATION_RESPONSE_BIOS_FAILURE  Error occurred during sending command to TPM or
     238                                                  receiving response from TPM.
     239  @retval Others                                  Return code from the TPM device after command execution.
    234240
    235241**/
    236 TPM_RESULT
     242UINT32
    237243ExecutePhysicalPresence (
    238   IN      EFI_TCG_PROTOCOL          *TcgProtocol,
    239   IN      UINT8                     CommandCode,
    240   IN OUT  UINT8                    *PpiFlags
     244  IN      EFI_TCG_PROTOCOL            *TcgProtocol,
     245  IN      UINT32                      CommandCode,
     246  IN OUT  EFI_PHYSICAL_PRESENCE_FLAGS *PpiFlags
    241247  )
    242248{
    243249  BOOLEAN                           BoolVal;
    244   TPM_RESULT                        TpmResponse;
     250  UINT32                            TpmResponse;
    245251  UINT32                            InData[5];
    246252
     
    325331      // PHYSICAL_PRESENCE_SET_OWNER_INSTALL_TRUE will be executed after reboot
    326332      //
    327       if ((*PpiFlags & FLAG_RESET_TRACK) == 0) {
     333      if ((PpiFlags->PPFlags & TCG_VENDOR_LIB_FLAG_RESET_TRACK) == 0) {
    328334        TpmResponse = ExecutePhysicalPresence (TcgProtocol, PHYSICAL_PRESENCE_ENABLE_ACTIVATE, PpiFlags);
    329         *PpiFlags |= FLAG_RESET_TRACK;
     335        PpiFlags->PPFlags |= TCG_VENDOR_LIB_FLAG_RESET_TRACK;
    330336      } else {
    331337        TpmResponse = ExecutePhysicalPresence (TcgProtocol, PHYSICAL_PRESENCE_SET_OWNER_INSTALL_TRUE, PpiFlags);
    332         *PpiFlags &= ~FLAG_RESET_TRACK;
     338        PpiFlags->PPFlags &= ~TCG_VENDOR_LIB_FLAG_RESET_TRACK;
    333339      }
    334340      return TpmResponse;
     
    360366      // Here it is NOT implemented
    361367      //
    362       return TPM_PP_BIOS_FAILURE;
     368      return TCG_PP_OPERATION_RESPONSE_BIOS_FAILURE;
    363369
    364370    case PHYSICAL_PRESENCE_CLEAR_ENABLE_ACTIVATE:
     
    370376
    371377    case PHYSICAL_PRESENCE_SET_NO_PPI_PROVISION_FALSE:
    372       *PpiFlags &= ~FLAG_NO_PPI_PROVISION;
     378      PpiFlags->PPFlags &= ~TCG_BIOS_TPM_MANAGEMENT_FLAG_NO_PPI_PROVISION;
    373379      return 0;
    374380
    375381    case PHYSICAL_PRESENCE_SET_NO_PPI_PROVISION_TRUE:
    376       *PpiFlags |= FLAG_NO_PPI_PROVISION;
     382      PpiFlags->PPFlags |= TCG_BIOS_TPM_MANAGEMENT_FLAG_NO_PPI_PROVISION;
    377383      return 0;
    378384
    379385    case PHYSICAL_PRESENCE_SET_NO_PPI_CLEAR_FALSE:
    380       *PpiFlags &= ~FLAG_NO_PPI_CLEAR;
     386      PpiFlags->PPFlags &= ~TCG_BIOS_TPM_MANAGEMENT_FLAG_NO_PPI_CLEAR;
    381387      return 0;
    382388
    383389    case PHYSICAL_PRESENCE_SET_NO_PPI_CLEAR_TRUE:
    384       *PpiFlags |= FLAG_NO_PPI_CLEAR;
     390      PpiFlags->PPFlags |= TCG_BIOS_TPM_MANAGEMENT_FLAG_NO_PPI_CLEAR;
    385391      return 0;
    386392
    387393    case PHYSICAL_PRESENCE_SET_NO_PPI_MAINTENANCE_FALSE:
    388       *PpiFlags &= ~FLAG_NO_PPI_MAINTENANCE;
     394      PpiFlags->PPFlags &= ~TCG_BIOS_TPM_MANAGEMENT_FLAG_NO_PPI_MAINTENANCE;
    389395      return 0;
    390396
    391397    case PHYSICAL_PRESENCE_SET_NO_PPI_MAINTENANCE_TRUE:
    392       *PpiFlags |= FLAG_NO_PPI_MAINTENANCE;
     398      PpiFlags->PPFlags |= TCG_BIOS_TPM_MANAGEMENT_FLAG_NO_PPI_MAINTENANCE;
    393399      return 0;
    394400 
    395401    case PHYSICAL_PRESENCE_ENABLE_ACTIVATE_CLEAR:
    396       TpmResponse = ExecutePhysicalPresence (TcgProtocol, PHYSICAL_PRESENCE_ENABLE_ACTIVATE, PpiFlags);
    397       if (TpmResponse == 0) {
     402      //
     403      // PHYSICAL_PRESENCE_ENABLE_ACTIVATE + PHYSICAL_PRESENCE_CLEAR
     404      // PHYSICAL_PRESENCE_CLEAR will be executed after reboot.
     405      //
     406      if ((PpiFlags->PPFlags & TCG_VENDOR_LIB_FLAG_RESET_TRACK) == 0) {
     407        TpmResponse = ExecutePhysicalPresence (TcgProtocol, PHYSICAL_PRESENCE_ENABLE_ACTIVATE, PpiFlags);
     408        PpiFlags->PPFlags |= TCG_VENDOR_LIB_FLAG_RESET_TRACK;
     409      } else {
    398410        TpmResponse = ExecutePhysicalPresence (TcgProtocol, PHYSICAL_PRESENCE_CLEAR, PpiFlags);
     411        PpiFlags->PPFlags &= ~TCG_VENDOR_LIB_FLAG_RESET_TRACK;
    399412      }
    400413      return TpmResponse;
     
    403416      //
    404417      // PHYSICAL_PRESENCE_ENABLE_ACTIVATE + PHYSICAL_PRESENCE_CLEAR_ENABLE_ACTIVATE
    405       // PHYSICAL_PRESENCE_CLEAR_ENABLE_ACTIVATE will be executed atfer reboot.
    406       //
    407       if ((*PpiFlags & FLAG_RESET_TRACK) == 0) {
     418      // PHYSICAL_PRESENCE_CLEAR_ENABLE_ACTIVATE will be executed after reboot.
     419      //
     420      if ((PpiFlags->PPFlags & TCG_VENDOR_LIB_FLAG_RESET_TRACK) == 0) {
    408421        TpmResponse = ExecutePhysicalPresence (TcgProtocol, PHYSICAL_PRESENCE_ENABLE_ACTIVATE, PpiFlags);
    409         *PpiFlags |= FLAG_RESET_TRACK;
     422        PpiFlags->PPFlags |= TCG_VENDOR_LIB_FLAG_RESET_TRACK;
    410423      } else {
    411424        TpmResponse = ExecutePhysicalPresence (TcgProtocol, PHYSICAL_PRESENCE_CLEAR_ENABLE_ACTIVATE, PpiFlags);
    412         *PpiFlags &= ~FLAG_RESET_TRACK;
     425        PpiFlags->PPFlags &= ~TCG_VENDOR_LIB_FLAG_RESET_TRACK;
    413426      }
    414427      return TpmResponse;
     
    417430      ;
    418431  }
    419   return TPM_PP_BIOS_FAILURE;
     432  return TCG_PP_OPERATION_RESPONSE_BIOS_FAILURE;
    420433}
    421434
     
    428441
    429442  @retval     TRUE        User confirmed the changes by input.
    430   @retval     FALSE       User discarded the changes.
     443  @retval     FALSE       User discarded the changes or device error.
    431444
    432445**/
     
    439452  EFI_INPUT_KEY                     Key;
    440453  UINT16                            InputKey;
    441      
     454  UINTN                             Index;
     455
    442456  InputKey = 0;
    443457  do {
    444     Status = gBS->CheckEvent (gST->ConIn->WaitForKey);
    445     if (!EFI_ERROR (Status)) {
    446       Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
    447       if (Key.ScanCode == SCAN_ESC) {
    448         InputKey = Key.ScanCode;
    449       }
    450       if ((Key.ScanCode == SCAN_F10) && !CautionKey) {
    451         InputKey = Key.ScanCode;
    452       }
    453       if ((Key.ScanCode == SCAN_F12) && CautionKey) {
    454         InputKey = Key.ScanCode;
    455       }
    456     }     
     458    Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
     459    if (Status == EFI_NOT_READY) {
     460      gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &Index);
     461      continue;
     462    }
     463
     464    if (Status == EFI_DEVICE_ERROR) {
     465      return FALSE;
     466    }
     467
     468    if (Key.ScanCode == SCAN_ESC) {
     469      InputKey = Key.ScanCode;
     470    }
     471    if ((Key.ScanCode == SCAN_F10) && !CautionKey) {
     472      InputKey = Key.ScanCode;
     473    }
     474    if ((Key.ScanCode == SCAN_F12) && CautionKey) {
     475      InputKey = Key.ScanCode;
     476    }
    457477  } while (InputKey == 0);
    458478
     
    499519BOOLEAN
    500520UserConfirm (
    501   IN      UINT8                     TpmPpCommand
     521  IN      UINT32                    TpmPpCommand
    502522  )
    503523{
     
    525545
    526546      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ACCEPT_KEY));
    527       StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
     547      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
    528548      FreePool (TmpStr1);
    529549      break;
     
    537557
    538558      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING));
    539       StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
     559      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
    540560      FreePool (TmpStr1);
    541561
    542562      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ACCEPT_KEY));
    543       StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
     563      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
    544564      FreePool (TmpStr1);
    545565      break;
     
    553573
    554574      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ACCEPT_KEY));
    555       StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
     575      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
    556576      FreePool (TmpStr1);
    557577      break;
     
    565585
    566586      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING));
    567       StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
     587      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
    568588      FreePool (TmpStr1);
    569589
    570590      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ACCEPT_KEY));
    571       StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
     591      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
    572592      FreePool (TmpStr1);
    573593      break;
     
    582602
    583603      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING_CLEAR));
    584       StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
    585       StrnCat (ConfirmText, L" \n\n", (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
     604      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
     605      StrnCat (ConfirmText, L" \n\n", (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
    586606      FreePool (TmpStr1);     
    587607
    588608      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_CAUTION_KEY));
    589       StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
     609      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
    590610      FreePool (TmpStr1);
    591611      break;
     
    599619
    600620      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_NOTE_ON));
    601       StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
     621      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
    602622      FreePool (TmpStr1);
    603623
    604624      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ACCEPT_KEY));
    605       StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
     625      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
    606626      FreePool (TmpStr1);
    607627      break;
     
    615635
    616636      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_NOTE_OFF));
    617       StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
     637      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
    618638      FreePool (TmpStr1);
    619639     
    620640      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING));
    621       StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
     641      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
    622642      FreePool (TmpStr1);
    623643
    624644      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ACCEPT_KEY));
    625       StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
     645      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
    626646      FreePool (TmpStr1);
    627647      break;
     
    635655
    636656      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ACCEPT_KEY));
    637       StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
     657      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
    638658      FreePool (TmpStr1);
    639659      break;
     
    647667
    648668      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ACCEPT_KEY));
    649       StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
     669      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
    650670      FreePool (TmpStr1);
    651671      break;
     
    659679
    660680      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_NOTE_ON));
    661       StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
     681      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
    662682      FreePool (TmpStr1);
    663683
    664684      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ACCEPT_KEY));
    665       StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
     685      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
    666686      FreePool (TmpStr1);
    667687      break;
     
    675695
    676696      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_NOTE_OFF));
    677       StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
     697      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
    678698      FreePool (TmpStr1);
    679699     
    680700      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING));
    681       StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
     701      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
    682702      FreePool (TmpStr1);
    683703
    684704      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ACCEPT_KEY));
    685       StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
     705      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
    686706      FreePool (TmpStr1);
    687707      break;
     
    696716     
    697717      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING_MAINTAIN));
    698       StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
     718      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
    699719      FreePool (TmpStr1);
    700720
    701721      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_CAUTION_KEY));
    702       StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
     722      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
    703723      FreePool (TmpStr1);
    704724      break;
     
    721741
    722742      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_NOTE_ON));
    723       StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
     743      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
    724744      FreePool (TmpStr1);
    725745
    726746      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING_CLEAR));
    727       StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
     747      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
    728748      FreePool (TmpStr1);
    729749
    730750      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING_CLEAR_CONT));
    731       StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
     751      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
    732752      FreePool (TmpStr1);
    733753
    734754      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_CAUTION_KEY));
    735       StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
     755      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
    736756      FreePool (TmpStr1);
    737757      break;
     
    745765
    746766      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ACCEPT_KEY));
    747       StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
     767      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
    748768      FreePool (TmpStr1);
    749769
    750770      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_NO_PPI_INFO));
    751       StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
     771      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
    752772      FreePool (TmpStr1);
    753773      break;
     
    762782
    763783      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_NOTE_CLEAR));
    764       StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
     784      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
    765785      FreePool (TmpStr1);
    766786
    767787      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING_CLEAR));
    768       StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
    769       StrnCat (ConfirmText, L" \n\n", (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
     788      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
     789      StrnCat (ConfirmText, L" \n\n", (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
    770790      FreePool (TmpStr1);
    771791
    772792      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_CAUTION_KEY));
    773       StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
     793      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
    774794      FreePool (TmpStr1);
    775795
    776796      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_NO_PPI_INFO));
    777       StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
     797      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
    778798      FreePool (TmpStr1);
    779799      break;
     
    788808
    789809      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING_MAINTAIN));
    790       StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
     810      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
    791811      FreePool (TmpStr1);
    792812
    793813      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_CAUTION_KEY));
    794       StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
     814      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
    795815      FreePool (TmpStr1);
    796816
    797817      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_NO_PPI_INFO));
    798       StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
     818      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
    799819      FreePool (TmpStr1);
    800820      break;
     
    809829
    810830      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING_CLEAR));
    811       StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
    812       StrnCat (ConfirmText, L" \n\n", (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
     831      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
     832      StrnCat (ConfirmText, L" \n\n", (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
    813833      FreePool (TmpStr1);
    814834
    815835      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_CAUTION_KEY));
    816       StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
     836      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
    817837      FreePool (TmpStr1);
    818838      break;
     
    827847
    828848      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_NOTE_ON));
    829       StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
     849      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
    830850      FreePool (TmpStr1);
    831851
    832852      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING_CLEAR));
    833       StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
     853      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
    834854      FreePool (TmpStr1);
    835855
    836856      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING_CLEAR_CONT));
    837       StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
     857      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
    838858      FreePool (TmpStr1);
    839859
    840860      TmpStr1 = PhysicalPresenceGetStringById (STRING_TOKEN (TPM_CAUTION_KEY));
    841       StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16 *)) - StrLen (ConfirmText) - 1);
     861      StrnCat (ConfirmText, TmpStr1, (BufSize / sizeof (CHAR16)) - StrLen (ConfirmText) - 1);
    842862      FreePool (TmpStr1);
    843863      break;
     
    874894
    875895/**
    876   Check and execute the requested physical presence command.
    877 
    878   @param[in] TcgProtocol          EFI TCG Protocol instance.
    879   @param[in] TcgPpData            Point to the physical presence NV variable.
     896  Check if there is a valid physical presence command request. Also updates parameter value
     897  to whether the requested physical presence command already confirmed by user
     898 
     899   @param[in]  TcgPpData           EFI TCG Physical Presence request data.
     900   @param[in]  Flags               The physical presence interface flags.
     901   @param[out] RequestConfirmed    If the physical presence operation command required user confirm from UI.
     902                                   True, it indicates the command doesn't require user confirm, or already confirmed
     903                                   in last boot cycle by user.
     904                                   False, it indicates the command need user confirm from UI.
     905
     906   @retval  TRUE        Physical Presence operation command is valid.
     907   @retval  FALSE       Physical Presence operation command is invalid.
    880908
    881909**/
    882 VOID
    883 ExecutePendingTpmRequest (
    884   IN      EFI_TCG_PROTOCOL          *TcgProtocol,
    885   IN      EFI_PHYSICAL_PRESENCE     *TcgPpData
     910BOOLEAN
     911HaveValidTpmRequest  (
     912  IN      EFI_PHYSICAL_PRESENCE       *TcgPpData,
     913  IN      EFI_PHYSICAL_PRESENCE_FLAGS Flags,
     914  OUT     BOOLEAN                     *RequestConfirmed
    886915  )
    887916{
    888   EFI_STATUS                        Status;
    889   UINTN                             DataSize;
    890   UINT8                             Flags;
    891   BOOLEAN                           RequestConfirmed;
    892 
    893   Flags            = TcgPpData->Flags;
    894   RequestConfirmed = FALSE; 
     917  BOOLEAN  IsRequestValid;
     918
     919  *RequestConfirmed = FALSE;
     920
    895921  switch (TcgPpData->PPRequest) {
    896922    case PHYSICAL_PRESENCE_NO_ACTION:
    897       return;
     923      *RequestConfirmed = TRUE;
     924      return TRUE;
    898925    case PHYSICAL_PRESENCE_ENABLE:
    899926    case PHYSICAL_PRESENCE_DISABLE:
     
    907934    case PHYSICAL_PRESENCE_DEACTIVATE_DISABLE_OWNER_FALSE:
    908935    case PHYSICAL_PRESENCE_SET_OPERATOR_AUTH:
    909       if ((Flags & FLAG_NO_PPI_PROVISION) != 0) {
    910         RequestConfirmed = TRUE;
     936      if ((Flags.PPFlags & TCG_BIOS_TPM_MANAGEMENT_FLAG_NO_PPI_PROVISION) != 0) {
     937        *RequestConfirmed = TRUE;
    911938      }
    912939      break;
     
    914941    case PHYSICAL_PRESENCE_CLEAR:
    915942    case PHYSICAL_PRESENCE_ENABLE_ACTIVATE_CLEAR:
    916       if ((Flags & FLAG_NO_PPI_CLEAR) != 0) {
    917         RequestConfirmed = TRUE;
     943      if ((Flags.PPFlags & TCG_BIOS_TPM_MANAGEMENT_FLAG_NO_PPI_CLEAR) != 0) {
     944        *RequestConfirmed = TRUE;
    918945      }
    919946      break;
    920947
    921948    case PHYSICAL_PRESENCE_DEFERRED_PP_UNOWNERED_FIELD_UPGRADE:
    922       if ((Flags & FLAG_NO_PPI_MAINTENANCE) != 0) {
    923         RequestConfirmed = TRUE;
     949      if ((Flags.PPFlags & TCG_BIOS_TPM_MANAGEMENT_FLAG_NO_PPI_MAINTENANCE) != 0) {
     950        *RequestConfirmed = TRUE;
    924951      }
    925952      break;
     
    927954    case PHYSICAL_PRESENCE_CLEAR_ENABLE_ACTIVATE:
    928955    case PHYSICAL_PRESENCE_ENABLE_ACTIVATE_CLEAR_ENABLE_ACTIVATE:
    929       if ((Flags & FLAG_NO_PPI_CLEAR) != 0 && (Flags & FLAG_NO_PPI_PROVISION) != 0) {
    930         RequestConfirmed = TRUE;
     956      if ((Flags.PPFlags & TCG_BIOS_TPM_MANAGEMENT_FLAG_NO_PPI_CLEAR) != 0 && (Flags.PPFlags & TCG_BIOS_TPM_MANAGEMENT_FLAG_NO_PPI_PROVISION) != 0) {
     957        *RequestConfirmed = TRUE;
    931958      }
    932       break; 
     959      break;
    933960
    934961    case PHYSICAL_PRESENCE_SET_NO_PPI_PROVISION_FALSE:
    935962    case PHYSICAL_PRESENCE_SET_NO_PPI_CLEAR_FALSE:
    936963    case PHYSICAL_PRESENCE_SET_NO_PPI_MAINTENANCE_FALSE:
    937       RequestConfirmed = TRUE;
    938       break;
    939      
     964      *RequestConfirmed = TRUE;
     965      break;
     966
     967    case PHYSICAL_PRESENCE_SET_NO_PPI_PROVISION_TRUE:
     968    case PHYSICAL_PRESENCE_SET_NO_PPI_CLEAR_TRUE:
     969    case PHYSICAL_PRESENCE_SET_NO_PPI_MAINTENANCE_TRUE:
     970      break;
     971
    940972    default:
    941       //
    942       // Invalid operation request.
    943       //
    944       TcgPpData->PPResponse = TPM_PP_BIOS_FAILURE;
    945       TcgPpData->LastPPRequest = TcgPpData->PPRequest;
    946       TcgPpData->PPRequest = PHYSICAL_PRESENCE_NO_ACTION;
    947       DataSize = sizeof (EFI_PHYSICAL_PRESENCE);
    948       Status = gRT->SetVariable (
    949                       PHYSICAL_PRESENCE_VARIABLE,
     973      if (TcgPpData->PPRequest >= TCG_PHYSICAL_PRESENCE_VENDOR_SPECIFIC_OPERATION) {
     974        IsRequestValid = TcgPpVendorLibHasValidRequest (TcgPpData->PPRequest, Flags.PPFlags, RequestConfirmed);
     975        if (!IsRequestValid) {
     976          return FALSE;
     977        } else {
     978          break;
     979        }
     980      } else {
     981        //
     982        // Wrong Physical Presence command
     983        //
     984        return FALSE;
     985      }
     986  }
     987
     988  if ((Flags.PPFlags & TCG_VENDOR_LIB_FLAG_RESET_TRACK) != 0) {
     989    //
     990    // It had been confirmed in last boot, it doesn't need confirm again.
     991    //
     992    *RequestConfirmed = TRUE;
     993  }
     994
     995  //
     996  // Physical Presence command is correct
     997  //
     998  return TRUE;
     999}
     1000
     1001
     1002/**
     1003  Check and execute the requested physical presence command.
     1004
     1005  Caution: This function may receive untrusted input.
     1006  TcgPpData variable is external input, so this function will validate
     1007  its data structure to be valid value.
     1008
     1009  @param[in] TcgProtocol          EFI TCG Protocol instance.
     1010  @param[in] TcgPpData            Point to the physical presence NV variable.
     1011  @param[in] Flags                The physical presence interface flags.
     1012
     1013**/
     1014VOID
     1015ExecutePendingTpmRequest (
     1016  IN      EFI_TCG_PROTOCOL            *TcgProtocol,
     1017  IN      EFI_PHYSICAL_PRESENCE       *TcgPpData,
     1018  IN      EFI_PHYSICAL_PRESENCE_FLAGS Flags
     1019  )
     1020{
     1021  EFI_STATUS                        Status;
     1022  UINTN                             DataSize;
     1023  BOOLEAN                           RequestConfirmed;
     1024  EFI_PHYSICAL_PRESENCE_FLAGS       NewFlags;
     1025  BOOLEAN                           ResetRequired;
     1026  UINT32                            NewPPFlags;
     1027
     1028  if (!HaveValidTpmRequest(TcgPpData, Flags, &RequestConfirmed)) {
     1029    //
     1030    // Invalid operation request.
     1031    //
     1032    TcgPpData->PPResponse = TCG_PP_OPERATION_RESPONSE_BIOS_FAILURE;
     1033    TcgPpData->LastPPRequest = TcgPpData->PPRequest;
     1034    TcgPpData->PPRequest = PHYSICAL_PRESENCE_NO_ACTION;
     1035    DataSize = sizeof (EFI_PHYSICAL_PRESENCE);
     1036    Status = gRT->SetVariable (
     1037                    PHYSICAL_PRESENCE_VARIABLE,
     1038                    &gEfiPhysicalPresenceGuid,
     1039                    EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
     1040                    DataSize,
     1041                    TcgPpData
     1042                    );
     1043    return;
     1044  }
     1045
     1046  ResetRequired = FALSE;
     1047  if (TcgPpData->PPRequest >= TCG_PHYSICAL_PRESENCE_VENDOR_SPECIFIC_OPERATION) {
     1048    NewFlags = Flags;
     1049    NewPPFlags = NewFlags.PPFlags;
     1050    TcgPpData->PPResponse = TcgPpVendorLibExecutePendingRequest (TcgPpData->PPRequest, &NewPPFlags, &ResetRequired);
     1051    NewFlags.PPFlags = (UINT8)NewPPFlags;
     1052  } else {
     1053    if (!RequestConfirmed) {
     1054      //
     1055      // Print confirm text and wait for approval.
     1056      //
     1057      RequestConfirmed = UserConfirm (TcgPpData->PPRequest);
     1058    }
     1059
     1060    //
     1061    // Execute requested physical presence command
     1062    //
     1063    TcgPpData->PPResponse = TCG_PP_OPERATION_RESPONSE_USER_ABORT;
     1064    NewFlags = Flags;
     1065    if (RequestConfirmed) {
     1066      TcgPpData->PPResponse = ExecutePhysicalPresence (TcgProtocol, TcgPpData->PPRequest, &NewFlags);
     1067    }
     1068  }
     1069
     1070  //
     1071  // Save the flags if it is updated.
     1072  //
     1073  if (CompareMem (&Flags, &NewFlags, sizeof(EFI_PHYSICAL_PRESENCE_FLAGS)) != 0) {
     1074    Status   = gRT->SetVariable (
     1075                      PHYSICAL_PRESENCE_FLAGS_VARIABLE,
    9501076                      &gEfiPhysicalPresenceGuid,
    9511077                      EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
    952                       DataSize,
    953                       TcgPpData
    954                       );
     1078                      sizeof (EFI_PHYSICAL_PRESENCE_FLAGS),
     1079                      &NewFlags
     1080                      );
     1081    if (EFI_ERROR (Status)) {
    9551082      return;
    956   }
    957 
    958   if ((Flags & FLAG_RESET_TRACK) != 0) {
    959     //
    960     // It had been confirmed in last boot, it doesn't need confirm again.
    961     //
    962     RequestConfirmed = TRUE;
    963   }
    964 
    965   if (!RequestConfirmed) {
    966     //
    967     // Print confirm text and wait for approval.
    968     //
    969     RequestConfirmed = UserConfirm (TcgPpData->PPRequest);
    970   }
    971 
    972   //
    973   // Execute requested physical presence command
    974   //
    975   TcgPpData->PPResponse = TPM_PP_USER_ABORT;
    976   if (RequestConfirmed) {
    977     TcgPpData->PPResponse = ExecutePhysicalPresence (TcgProtocol, TcgPpData->PPRequest, &TcgPpData->Flags);
    978   }
    979 
     1083    }
     1084  }
     1085 
    9801086  //
    9811087  // Clear request
    9821088  //
    983   if ((TcgPpData->Flags & FLAG_RESET_TRACK) == 0) {
     1089  if ((NewFlags.PPFlags & TCG_VENDOR_LIB_FLAG_RESET_TRACK) == 0) {
    9841090    TcgPpData->LastPPRequest = TcgPpData->PPRequest;
    9851091    TcgPpData->PPRequest = PHYSICAL_PRESENCE_NO_ACTION;   
     
    10011107  }
    10021108
    1003   if (TcgPpData->PPResponse == TPM_PP_USER_ABORT) {
     1109  if (TcgPpData->PPResponse == TCG_PP_OPERATION_RESPONSE_USER_ABORT) {
    10041110    return;
    10051111  }
     
    10221128      break;
    10231129    default:
     1130      if (TcgPpData->LastPPRequest >= TCG_PHYSICAL_PRESENCE_VENDOR_SPECIFIC_OPERATION) {
     1131        if (ResetRequired) {
     1132          break;
     1133        } else {
     1134          return ;
     1135        }
     1136      }
    10241137      if (TcgPpData->PPRequest != PHYSICAL_PRESENCE_NO_ACTION) {
    10251138        break;
     
    10581171  EFI_PHYSICAL_PRESENCE             TcgPpData;
    10591172  EFI_TCG_PROTOCOL                  *TcgProtocol;
     1173  EDKII_VARIABLE_LOCK_PROTOCOL      *VariableLockProtocol;
     1174  EFI_PHYSICAL_PRESENCE_FLAGS       PpiFlags;
    10601175 
    10611176  Status = gBS->LocateProtocol (&gEfiTcgProtocolGuid, NULL, (VOID **)&TcgProtocol);
    10621177  if (EFI_ERROR (Status)) {
    10631178    return ;
     1179  }
     1180
     1181  //
     1182  // Initialize physical presence flags.
     1183  //
     1184  DataSize = sizeof (EFI_PHYSICAL_PRESENCE_FLAGS);
     1185  Status = gRT->GetVariable (
     1186                  PHYSICAL_PRESENCE_FLAGS_VARIABLE,
     1187                  &gEfiPhysicalPresenceGuid,
     1188                  NULL,
     1189                  &DataSize,
     1190                  &PpiFlags
     1191                  );
     1192  if (EFI_ERROR (Status)) {
     1193    PpiFlags.PPFlags = TCG_BIOS_TPM_MANAGEMENT_FLAG_NO_PPI_PROVISION;
     1194    Status   = gRT->SetVariable (
     1195                      PHYSICAL_PRESENCE_FLAGS_VARIABLE,
     1196                      &gEfiPhysicalPresenceGuid,
     1197                      EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
     1198                      sizeof (EFI_PHYSICAL_PRESENCE_FLAGS),
     1199                      &PpiFlags
     1200                      );
     1201    if (EFI_ERROR (Status)) {
     1202      DEBUG ((EFI_D_ERROR, "[TPM] Set physical presence flag failed, Status = %r\n", Status));
     1203      return ;
     1204    }
     1205  }
     1206  DEBUG ((EFI_D_INFO, "[TPM] PpiFlags = %x\n", PpiFlags.PPFlags));
     1207
     1208  //
     1209  // This flags variable controls whether physical presence is required for TPM command.
     1210  // It should be protected from malicious software. We set it as read-only variable here.
     1211  //
     1212  Status = gBS->LocateProtocol (&gEdkiiVariableLockProtocolGuid, NULL, (VOID **)&VariableLockProtocol);
     1213  if (!EFI_ERROR (Status)) {
     1214    Status = VariableLockProtocol->RequestToLock (
     1215                                     VariableLockProtocol,
     1216                                     PHYSICAL_PRESENCE_FLAGS_VARIABLE,
     1217                                     &gEfiPhysicalPresenceGuid
     1218                                     );
     1219    if (EFI_ERROR (Status)) {
     1220      DEBUG ((EFI_D_ERROR, "[TPM] Error when lock variable %s, Status = %r\n", PHYSICAL_PRESENCE_FLAGS_VARIABLE, Status));
     1221      ASSERT_EFI_ERROR (Status);
     1222    }
    10641223  }
    10651224 
     
    10761235                  );
    10771236  if (EFI_ERROR (Status)) {
    1078     if (Status == EFI_NOT_FOUND) {
    1079       ZeroMem ((VOID*)&TcgPpData, sizeof (TcgPpData));
    1080       TcgPpData.Flags |= FLAG_NO_PPI_PROVISION;
    1081       DataSize = sizeof (EFI_PHYSICAL_PRESENCE);
    1082       Status   = gRT->SetVariable (
    1083                         PHYSICAL_PRESENCE_VARIABLE,
    1084                         &gEfiPhysicalPresenceGuid,
    1085                         EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
    1086                         DataSize,
    1087                         &TcgPpData
    1088                         );
     1237    ZeroMem ((VOID*)&TcgPpData, sizeof (TcgPpData));
     1238    DataSize = sizeof (EFI_PHYSICAL_PRESENCE);
     1239    Status   = gRT->SetVariable (
     1240                      PHYSICAL_PRESENCE_VARIABLE,
     1241                      &gEfiPhysicalPresenceGuid,
     1242                      EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
     1243                      DataSize,
     1244                      &TcgPpData
     1245                      );
     1246    if (EFI_ERROR (Status)) {
     1247      DEBUG ((EFI_D_ERROR, "[TPM] Set physical presence variable failed, Status = %r\n", Status));
     1248      return;
    10891249    }
    1090     ASSERT_EFI_ERROR (Status);
    1091   }
    1092 
    1093   DEBUG ((EFI_D_INFO, "[TPM] Flags=%x, PPRequest=%x\n", TcgPpData.Flags, TcgPpData.PPRequest));
     1250  }
     1251
     1252  DEBUG ((EFI_D_INFO, "[TPM] Flags=%x, PPRequest=%x\n", PpiFlags.PPFlags, TcgPpData.PPRequest));
     1253
     1254  if (TcgPpData.PPRequest == PHYSICAL_PRESENCE_NO_ACTION) {
     1255    //
     1256    // No operation request
     1257    //
     1258    return;
     1259  }
    10941260
    10951261  Status = GetTpmCapability (TcgProtocol, &LifetimeLock, &CmdEnable);
     
    11191285  // Execute pending TPM request.
    11201286  // 
    1121   ExecutePendingTpmRequest (TcgProtocol, &TcgPpData);
     1287  ExecutePendingTpmRequest (TcgProtocol, &TcgPpData, PpiFlags);
    11221288  DEBUG ((EFI_D_INFO, "[TPM] PPResponse = %x\n", TcgPpData.PPResponse));
    11231289
     
    11281294}
    11291295
     1296/**
     1297  Check if the pending TPM request needs user input to confirm.
     1298
     1299  The TPM request may come from OS. This API will check if TPM request exists and need user
     1300  input to confirmation.
     1301 
     1302  @retval    TRUE        TPM needs input to confirm user physical presence.
     1303  @retval    FALSE       TPM doesn't need input to confirm user physical presence.
     1304
     1305**/
     1306BOOLEAN
     1307EFIAPI
     1308TcgPhysicalPresenceLibNeedUserConfirm(
     1309  VOID
     1310  )
     1311{
     1312  EFI_STATUS                   Status;
     1313  EFI_PHYSICAL_PRESENCE        TcgPpData;
     1314  UINTN                        DataSize;
     1315  BOOLEAN                      RequestConfirmed;
     1316  BOOLEAN                      LifetimeLock;
     1317  BOOLEAN                      CmdEnable;
     1318  EFI_TCG_PROTOCOL             *TcgProtocol;
     1319  EFI_PHYSICAL_PRESENCE_FLAGS  PpiFlags;
     1320 
     1321  Status = gBS->LocateProtocol (&gEfiTcgProtocolGuid, NULL, (VOID **)&TcgProtocol);
     1322  if (EFI_ERROR (Status)) {
     1323    return FALSE;
     1324  }
     1325
     1326  //
     1327  // Check Tpm requests
     1328  //
     1329  DataSize = sizeof (EFI_PHYSICAL_PRESENCE);
     1330  Status = gRT->GetVariable (
     1331                  PHYSICAL_PRESENCE_VARIABLE,
     1332                  &gEfiPhysicalPresenceGuid,
     1333                  NULL,
     1334                  &DataSize,
     1335                  &TcgPpData
     1336                  );
     1337  if (EFI_ERROR (Status)) {
     1338    return FALSE;
     1339  }
     1340
     1341  DataSize = sizeof (EFI_PHYSICAL_PRESENCE_FLAGS);
     1342  Status = gRT->GetVariable (
     1343                  PHYSICAL_PRESENCE_FLAGS_VARIABLE,
     1344                  &gEfiPhysicalPresenceGuid,
     1345                  NULL,
     1346                  &DataSize,
     1347                  &PpiFlags
     1348                  );
     1349  if (EFI_ERROR (Status)) {
     1350    return FALSE;
     1351  }
     1352 
     1353  if (TcgPpData.PPRequest == PHYSICAL_PRESENCE_NO_ACTION) {
     1354    //
     1355    // No operation request
     1356    //
     1357    return FALSE;
     1358  }
     1359
     1360  if (!HaveValidTpmRequest(&TcgPpData, PpiFlags, &RequestConfirmed)) {
     1361    //
     1362    // Invalid operation request.
     1363    //
     1364    return FALSE;
     1365  }
     1366
     1367  //
     1368  // Check Tpm Capability
     1369  //
     1370  Status = GetTpmCapability (TcgProtocol, &LifetimeLock, &CmdEnable);
     1371  if (EFI_ERROR (Status)) {
     1372    return FALSE;
     1373  }
     1374
     1375  if (!CmdEnable) {
     1376    if (LifetimeLock) {
     1377      //
     1378      // physicalPresenceCMDEnable is locked, can't execute physical presence command.
     1379      //
     1380      return FALSE;
     1381    }
     1382  }
     1383
     1384  if (!RequestConfirmed) {
     1385    //
     1386    // Need UI to confirm
     1387    //
     1388    return TRUE;
     1389  }
     1390
     1391  return FALSE;
     1392}
     1393
  • trunk/src/VBox/Devices/EFI/Firmware/SecurityPkg/Library/DxeTcgPhysicalPresenceLib/DxeTcgPhysicalPresenceLib.inf

    r48674 r58459  
    11## @file
    2 # TCG physical presence library instance. This library will lock
    3 # TPM after executing TPM request.
     2#  Executes pending TPM 1.2 requests from OS or BIOS and Locks TPM
    43#
    5 # Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>
     4#  This library will check and execute TPM 1.2 request from OS or BIOS. The request may
     5#  ask for user confirmation before execution. This Library will also lock TPM physical
     6#  presence at last.
     7#
     8#  Caution: This module requires additional review when modified.
     9#  This driver will have external input - variable.
     10#  This external input must be validated carefully to avoid security issue.
     11#
     12# Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>
    613# This program and the accompanying materials
    714# are licensed and made available under the terms and conditions of the BSD License
     
    1522[Defines]
    1623  INF_VERSION                    = 0x00010005
    17   BASE_NAME                      = DxeTcgPhysicalPresenceLib   
     24  BASE_NAME                      = DxeTcgPhysicalPresenceLib
     25  MODULE_UNI_FILE                = DxeTcgPhysicalPresenceLib.uni
    1826  FILE_GUID                      = EBC43A46-34AC-4F07-A7F5-A5394619361C
    1927  MODULE_TYPE                    = DXE_DRIVER
     
    4755  PrintLib
    4856  HiiLib
     57  TcgPpVendorLib
    4958
    5059[Protocols]
    51   gEfiTcgProtocolGuid
     60  gEfiTcgProtocolGuid                   ## CONSUMES
     61  gEdkiiVariableLockProtocolGuid        ## CONSUMES
    5262
    5363[Guids]
     64  ## CONSUMES           ## HII
     65  ## SOMETIMES_PRODUCES ## Variable:L"PhysicalPresence"
     66  ## SOMETIMES_CONSUMES ## Variable:L"PhysicalPresence"
     67  ## SOMETIMES_PRODUCES ## Variable:L"PhysicalPresenceFlags"
     68  ## SOMETIMES_CONSUMES ## Variable:L"PhysicalPresenceFlags"
    5469  gEfiPhysicalPresenceGuid
  • trunk/src/VBox/Devices/EFI/Firmware/SecurityPkg/Library/DxeTpmMeasureBootLib/DxeTpmMeasureBootLib.c

    r48674 r58459  
    22  The library instance provides security service of TPM measure boot. 
    33
    4 Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>
     4  Caution: This file requires additional review when modified.
     5  This library will have external input - PE/COFF image and GPT partition.
     6  This external input must be validated carefully to avoid security issue like
     7  buffer overflow, integer overflow.
     8
     9  DxeTpmMeasureBootLibImageRead() function will make sure the PE/COFF image content
     10  read is within the image buffer.
     11
     12  TcgMeasurePeImage() function will accept untrusted PE/COFF image and validate its
     13  data structure within this image buffer before use.
     14
     15  TcgMeasureGptTable() function will receive untrusted GPT partition table, and parse
     16  partition data carefully.
     17
     18Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>
    519This program and the accompanying materials
    620are licensed and made available under the terms and conditions of the BSD License
     
    1630
    1731#include <Protocol/TcgService.h>
    18 #include <Protocol/FirmwareVolume2.h>
    1932#include <Protocol/BlockIo.h>
    2033#include <Protocol/DiskIo.h>
    21 #include <Protocol/DevicePathToText.h>
     34#include <Protocol/FirmwareVolumeBlock.h>
     35
     36#include <Guid/MeasuredFvHob.h>
    2237
    2338#include <Library/BaseLib.h>
     
    3045#include <Library/PeCoffLib.h>
    3146#include <Library/SecurityManagementLib.h>
     47#include <Library/HobLib.h>
    3248
    3349//
     
    3955VOID                              *mFileBuffer;
    4056UINTN                             mImageSize;
     57//
     58// Measured FV handle cache
     59//
     60EFI_HANDLE                        mCacheMeasuredHandle  = NULL;
     61MEASURED_HOB_DATA                 *mMeasuredHobData     = NULL;
    4162
    4263/**
    4364  Reads contents of a PE/COFF image in memory buffer.
     65
     66  Caution: This function may receive untrusted input.
     67  PE/COFF image is external input, so this function will make sure the PE/COFF image content
     68  read is within the image buffer.
    4469
    4570  @param  FileHandle      Pointer to the file handle to read the PE/COFF image.
     
    86111/**
    87112  Measure GPT table data into TPM log.
     113
     114  Caution: This function may receive untrusted input.
     115  The GPT partition table is external input, so this function should parse partition data carefully.
    88116
    89117  @param TcgProtocol             Pointer to the located TCG protocol instance.
     
    178206      NumberOfPartition++; 
    179207    }
    180     PartitionEntry++;
    181   }
    182 
    183   //
    184   // Parepare Data for Measurement
     208    PartitionEntry = (EFI_PARTITION_ENTRY *)((UINT8 *)PartitionEntry + PrimaryHeader->SizeOfPartitionEntry);
     209  }
     210
     211  //
     212  // Prepare Data for Measurement
    185213  //
    186214  EventSize = (UINT32)(sizeof (EFI_GPT_DATA) - sizeof (GptData->Partitions)
    187215                        + NumberOfPartition * PrimaryHeader->SizeOfPartitionEntry);
    188   TcgEvent = (TCG_PCR_EVENT *) AllocateZeroPool (EventSize + sizeof (TCG_PCR_EVENT));
     216  TcgEvent = (TCG_PCR_EVENT *) AllocateZeroPool (EventSize + sizeof (TCG_PCR_EVENT_HDR));
    189217  if (TcgEvent == NULL) {
    190218    FreePool (PrimaryHeader);
     
    211239    if (!CompareGuid (&PartitionEntry->PartitionTypeGUID, &mZeroGuid)) {
    212240      CopyMem (
    213         (UINT8 *)&GptData->Partitions + NumberOfPartition * sizeof (EFI_PARTITION_ENTRY),
     241        (UINT8 *)&GptData->Partitions + NumberOfPartition * PrimaryHeader->SizeOfPartitionEntry,
    214242        (UINT8 *)PartitionEntry,
    215         sizeof (EFI_PARTITION_ENTRY)
     243        PrimaryHeader->SizeOfPartitionEntry
    216244        );
    217245      NumberOfPartition++;
    218246    }
    219     PartitionEntry++;
     247    PartitionEntry =(EFI_PARTITION_ENTRY *)((UINT8 *)PartitionEntry + PrimaryHeader->SizeOfPartitionEntry);
    220248  }
    221249
     
    247275  Measure PE image into TPM log based on the authenticode image hashing in
    248276  PE/COFF Specification 8.0 Appendix A.
     277
     278  Caution: This function may receive untrusted input.
     279  PE/COFF image is external input, so this function will validate its data structure
     280  within this image buffer before use.
    249281
    250282  @param[in] TcgProtocol    Pointer to the located TCG protocol instance.
     
    340372  ImageLoad->ImageLinkTimeAddress  = LinkTimeBase;
    341373  ImageLoad->LengthOfDevicePath    = FilePathSize;
    342   CopyMem (ImageLoad->DevicePath, FilePath, FilePathSize);
     374  if ((FilePath != NULL) && (FilePathSize != 0)) {
     375    CopyMem (ImageLoad->DevicePath, FilePath, FilePathSize);
     376  }
    343377
    344378  //
     
    383417  // But CheckSum field and SECURITY data directory (certificate) are excluded
    384418  //
    385   Magic = Hdr.Pe32->OptionalHeader.Magic;
     419  if (Hdr.Pe32->FileHeader.Machine == IMAGE_FILE_MACHINE_IA64 && Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
     420    //
     421    // NOTE: Some versions of Linux ELILO for Itanium have an incorrect magic value
     422    //       in the PE/COFF Header. If the MachineType is Itanium(IA64) and the
     423    //       Magic value in the OptionalHeader is EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
     424    //       then override the magic value to EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
     425    //
     426    Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;
     427  } else {
     428    //
     429    // Get the magic value from the PE/COFF Optional Header
     430    //
     431    Magic = Hdr.Pe32->OptionalHeader.Magic;
     432  }
    386433 
    387434  //
     
    619666             &EventLogLastEntry
    620667             );
     668  if (Status == EFI_OUT_OF_RESOURCES) {
     669    //
     670    // Out of resource here means the image is hashed and its result is extended to PCR.
     671    // But the event log cann't be saved since log area is full.
     672    // Just return EFI_SUCCESS in order not to block the image load.
     673    //
     674    Status = EFI_SUCCESS;
     675  }
    621676
    622677Finish:
     
    657712  returned.
    658713
    659   @param[in, out] AuthenticationStatus  This is the authentication status returned
     714  @param[in]      AuthenticationStatus  This is the authentication status returned
    660715                                        from the securitymeasurement services for the
    661716                                        input file.
     
    664719  @param[in]      FileBuffer File buffer matches the input file device path.
    665720  @param[in]      FileSize   Size of File buffer matches the input file device path.
    666 
    667   @retval EFI_SUCCESS            The file specified by File did authenticate, and the
    668                                  platform policy dictates that the DXE Core may use File.
    669   @retval EFI_INVALID_PARAMETER  File is NULL.
    670   @retval EFI_SECURITY_VIOLATION The file specified by File did not authenticate, and
    671                                  the platform policy dictates that File should be placed
    672                                  in the untrusted state. A file may be promoted from
    673                                  the untrusted to the trusted state at a future time
    674                                  with a call to the Trust() DXE Service.
    675   @retval EFI_ACCESS_DENIED      The file specified by File did not authenticate, and
    676                                  the platform policy dictates that File should not be
    677                                  used for any purpose.
    678 
     721  @param[in]      BootPolicy A boot policy that was used to call LoadImage() UEFI service.
     722
     723  @retval EFI_SUCCESS             The file specified by DevicePath and non-NULL
     724                                  FileBuffer did authenticate, and the platform policy dictates
     725                                  that the DXE Foundation may use the file.
     726  @retval other error value
    679727**/
    680728EFI_STATUS
    681729EFIAPI
    682730DxeTpmMeasureBootHandler (
    683   IN  OUT   UINT32                     AuthenticationStatus,
     731  IN  UINT32                           AuthenticationStatus,
    684732  IN  CONST EFI_DEVICE_PATH_PROTOCOL   *File,
    685   IN  VOID                             *FileBuffer OPTIONAL,
    686   IN  UINTN                            FileSize OPTIONAL
     733  IN  VOID                             *FileBuffer,
     734  IN  UINTN                            FileSize,
     735  IN  BOOLEAN                          BootPolicy
    687736  )
    688737{
    689   EFI_TCG_PROTOCOL                  *TcgProtocol;
    690   EFI_STATUS                        Status;
    691   TCG_EFI_BOOT_SERVICE_CAPABILITY   ProtocolCapability;
    692   UINT32                            TCGFeatureFlags;
    693   EFI_PHYSICAL_ADDRESS              EventLogLocation;
    694   EFI_PHYSICAL_ADDRESS              EventLogLastEntry;
    695   EFI_DEVICE_PATH_PROTOCOL          *DevicePathNode;
    696   EFI_DEVICE_PATH_PROTOCOL          *OrigDevicePathNode;
    697   EFI_HANDLE                        Handle;
    698   BOOLEAN                           ApplicationRequired;
    699   PE_COFF_LOADER_IMAGE_CONTEXT      ImageContext;
    700 
    701   if (File == NULL) {
    702     return EFI_INVALID_PARAMETER;
    703   }
     738  EFI_TCG_PROTOCOL                    *TcgProtocol;
     739  EFI_STATUS                          Status;
     740  TCG_EFI_BOOT_SERVICE_CAPABILITY     ProtocolCapability;
     741  UINT32                              TCGFeatureFlags;
     742  EFI_PHYSICAL_ADDRESS                EventLogLocation;
     743  EFI_PHYSICAL_ADDRESS                EventLogLastEntry;
     744  EFI_DEVICE_PATH_PROTOCOL            *DevicePathNode;
     745  EFI_DEVICE_PATH_PROTOCOL            *OrigDevicePathNode;
     746  EFI_HANDLE                          Handle;
     747  EFI_HANDLE                          TempHandle;
     748  BOOLEAN                             ApplicationRequired;
     749  PE_COFF_LOADER_IMAGE_CONTEXT        ImageContext;
     750  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *FvbProtocol;
     751  EFI_PHYSICAL_ADDRESS                FvAddress;
     752  UINT32                              Index;
    704753
    705754  Status = gBS->LocateProtocol (&gEfiTcgProtocolGuid, NULL, (VOID **) &TcgProtocol);
     
    720769             &EventLogLastEntry
    721770           );
    722   if (EFI_ERROR (Status) || ProtocolCapability.TPMDeactivatedFlag) {
     771  if (EFI_ERROR (Status) || ProtocolCapability.TPMDeactivatedFlag || (!ProtocolCapability.TPMPresentFlag)) {
    723772    //
    724773    // TPM device doesn't work or activate.
     
    731780  //
    732781  OrigDevicePathNode = DuplicateDevicePath (File);
    733   ASSERT (OrigDevicePathNode != NULL);
    734782 
    735783  //
     
    744792    //
    745793    DevicePathNode = OrigDevicePathNode;
     794    ASSERT (DevicePathNode != NULL);
    746795    while (!IsDevicePathEnd (DevicePathNode)) {
    747796      //
     
    795844
    796845  //
    797   // Check whether this device path support FV2 protocol.
     846  // Check whether this device path support FVB protocol.
    798847  //
    799848  DevicePathNode = OrigDevicePathNode;
    800   Status = gBS->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid, &DevicePathNode, &Handle);
     849  Status = gBS->LocateDevicePath (&gEfiFirmwareVolumeBlockProtocolGuid, &DevicePathNode, &Handle);
    801850  if (!EFI_ERROR (Status)) {
    802851    //
     
    808857    }
    809858    //
    810     // The image from Firmware image will not be mearsured.
    811     // Current policy doesn't measure PeImage from Firmware if it is driver
    812     // If the got PeImage is application, it will be still be measured.
     859    // The PE image from unmeasured Firmware volume need be measured
     860    // The PE image from measured Firmware volume will be mearsured according to policy below.
     861    //   If it is driver, do not measure
     862    //   If it is application, still measure.
    813863    //
    814864    ApplicationRequired = TRUE;
    815   }
    816  
     865
     866    if (mCacheMeasuredHandle != Handle && mMeasuredHobData != NULL) {
     867      //
     868      // Search for Root FV of this PE image
     869      //
     870      TempHandle = Handle;
     871      do {
     872        Status = gBS->HandleProtocol(
     873                        TempHandle,
     874                        &gEfiFirmwareVolumeBlockProtocolGuid,
     875                        (VOID**)&FvbProtocol
     876                        );
     877        TempHandle = FvbProtocol->ParentHandle;
     878      } while (!EFI_ERROR(Status) && FvbProtocol->ParentHandle != NULL);
     879
     880      //
     881      // Search in measured FV Hob
     882      //
     883      Status = FvbProtocol->GetPhysicalAddress(FvbProtocol, &FvAddress);
     884      if (EFI_ERROR(Status)){
     885        return Status;
     886      }
     887
     888      ApplicationRequired = FALSE;
     889
     890      for (Index = 0; Index < mMeasuredHobData->Num; Index++) {
     891        if(mMeasuredHobData->MeasuredFvBuf[Index].BlobBase == FvAddress) {
     892          //
     893          // Cache measured FV for next measurement
     894          //
     895          mCacheMeasuredHandle = Handle;
     896          ApplicationRequired  = TRUE;
     897          break;
     898        }
     899      }
     900    }
     901  }
     902
    817903  //
    818904  // File is not found.
     
    856942    DEBUG_CODE_BEGIN ();
    857943      CHAR16                            *ToText;
    858       EFI_DEVICE_PATH_TO_TEXT_PROTOCOL  *DevPathToText;
    859       Status = gBS->LocateProtocol (
    860                       &gEfiDevicePathToTextProtocolGuid,
    861                       NULL,
    862                       (VOID **) &DevPathToText
    863                       );
    864       if (!EFI_ERROR (Status)) {
    865         ToText = DevPathToText->ConvertDevicePathToText (
    866                                   DevicePathNode,
    867                                   FALSE,
    868                                   TRUE
    869                                   );
    870         if (ToText != NULL) {
    871           DEBUG ((DEBUG_INFO, "The measured image path is %s.\n", ToText));
    872         }
     944      ToText = ConvertDevicePathToText (
     945                 DevicePathNode,
     946                 FALSE,
     947                 TRUE
     948                 );
     949      if (ToText != NULL) {
     950        DEBUG ((DEBUG_INFO, "The measured image path is %s.\n", ToText));
     951        FreePool (ToText);
    873952      }
    874953    DEBUG_CODE_END ();
     
    891970  //
    892971Finish:
    893   FreePool (OrigDevicePathNode);
     972  if (OrigDevicePathNode != NULL) {
     973    FreePool (OrigDevicePathNode);
     974  }
    894975
    895976  return Status;
     
    912993  )
    913994{
    914   return RegisterSecurityHandler (
     995  EFI_HOB_GUID_TYPE  *GuidHob;
     996
     997  GuidHob = NULL;
     998
     999  GuidHob = GetFirstGuidHob (&gMeasuredFvHobGuid);
     1000
     1001  if (GuidHob != NULL) {
     1002    mMeasuredHobData = GET_GUID_HOB_DATA (GuidHob);
     1003  }
     1004
     1005  return RegisterSecurity2Handler (
    9151006          DxeTpmMeasureBootHandler,
    9161007          EFI_AUTH_OPERATION_MEASURE_IMAGE | EFI_AUTH_OPERATION_IMAGE_REQUIRED
  • trunk/src/VBox/Devices/EFI/Firmware/SecurityPkg/Library/DxeTpmMeasureBootLib/DxeTpmMeasureBootLib.inf

    r48674 r58459  
    11## @file
    2 The library instance provides security service of TPM measure boot.
     2Provides security service for TPM 1.2 measured boot
    33#
    4 # Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>
     4#  This library instance hooks LoadImage() API to measure every image that
     5#  is not measured in PEI phase. And, it will also measure GPT partition.
     6#
     7#  Caution: This module requires additional review when modified.
     8#  This library will have external input - PE/COFF image and GPT partition.
     9#  This external input must be validated carefully to avoid security issues such
     10#  as buffer overflow or integer overflow.
     11#
     12# Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
    513# This program and the accompanying materials
    614# are licensed and made available under the terms and conditions of the BSD License
     
    1523  INF_VERSION                    = 0x00010005
    1624  BASE_NAME                      = DxeTpmMeasureBootLib
     25  MODULE_UNI_FILE                = DxeTpmMeasureBootLib.uni
    1726  FILE_GUID                      = 6C60C7D0-922A-4b7c-87D7-E503EDD73BBF
    1827  MODULE_TYPE                    = DXE_DRIVER
     
    4655  BaseLib
    4756  SecurityManagementLib
     57  HobLib
     58
     59[Guids]
     60  gMeasuredFvHobGuid                    ## SOMETIMES_CONSUMES ## HOB
    4861
    4962[Protocols]
    50   gEfiTcgProtocolGuid                   ## CONSUMES
    51   gEfiFirmwareVolume2ProtocolGuid       ## CONSUMES
    52   gEfiBlockIoProtocolGuid               ## CONSUMES
    53   gEfiDiskIoProtocolGuid                ## CONSUMES
    54   gEfiDevicePathToTextProtocolGuid      ## SOMETIMES_CONSUMES (Only used in debug mode)
     63  gEfiTcgProtocolGuid                   ## SOMETIMES_CONSUMES
     64  gEfiFirmwareVolumeBlockProtocolGuid   ## SOMETIMES_CONSUMES
     65  gEfiBlockIoProtocolGuid               ## SOMETIMES_CONSUMES
     66  gEfiDiskIoProtocolGuid                ## SOMETIMES_CONSUMES
     67
  • trunk/src/VBox/Devices/EFI/Firmware/SecurityPkg/Library/PlatformSecureLibNull/PlatformSecureLibNull.inf

    r48674 r58459  
    11## @file
     2#  NULL platform secure library instance that alway returns TRUE for a user physical present
     3#
    24#  NULL PlatformSecureLib instance does NOT really detect whether a physical present
    3 #  user exists but return TRUE directly. This instance can be used to verify security
     5#  user exists but returns TRUE directly. This instance can be used to verify security
    46#  related features during platform enabling and development. It should be replaced
    57#  by a platform-specific method(e.g. Button pressed) in a real platform for product.
    68#
    7 # Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>
     9# Copyright (c) 2011 - 2014, Intel Corporation. All rights reserved.<BR>
    810# This program and the accompanying materials
    911# are licensed and made available under the terms and conditions of the BSD License
     
    1820  INF_VERSION                    = 0x00010005
    1921  BASE_NAME                      = PlatformSecureLibNull
     22  MODULE_UNI_FILE                = PlatformSecureLibNull.uni
    2023  FILE_GUID                      = 7FA68D82-10A4-4e71-9524-D3D9500D3CDF
    2124  MODULE_TYPE                    = DXE_DRIVER
  • trunk/src/VBox/Devices/EFI/Firmware/SecurityPkg/Library/TpmCommLib/TisPc.c

    r48674 r58459  
    22  Basic TIS (TPM Interface Specification) functions.
    33
    4 Copyright (c) 2005 - 2011, Intel Corporation. All rights reserved.<BR>
     4Copyright (c) 2005 - 2012, Intel Corporation. All rights reserved.<BR>
    55This program and the accompanying materials
    66are licensed and made available under the terms and conditions of the BSD License
     
    145145/**
    146146  Get the control of TPM chip by sending requestUse command TIS_PC_ACC_RQUUSE
    147   to ACCESS Register in the time of default TIS_TIMEOUT_D.
     147  to ACCESS Register in the time of default TIS_TIMEOUT_A.
    148148
    149149  @param[in] TisReg                Pointer to TIS register.
     
    171171
    172172  MmioWrite8((UINTN)&TisReg->Access, TIS_PC_ACC_RQUUSE);
     173  //
     174  // No locality set before, ACCESS_X.activeLocality MUST be valid within TIMEOUT_A
     175  //
    173176  Status = TisPcWaitRegisterBits (
    174177             &TisReg->Access,
    175178             (UINT8)(TIS_PC_ACC_ACTIVE |TIS_PC_VALID),
    176179             0,
    177              TIS_TIMEOUT_D
     180             TIS_TIMEOUT_A
    178181             );
    179182  return Status;
  • trunk/src/VBox/Devices/EFI/Firmware/SecurityPkg/Library/TpmCommLib/TpmCommLib.inf

    r48674 r58459  
    11## @file
    2 TpmCommLib instance implements basis TPM Interface Specification (TIS) and TPM command functions.
     2Provides some common functions for the TCG feature
    33#
    4 # Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
     4#  This instance provides basic TPM Interface Specification (TIS) functions
     5#  and TPM hashall function.
     6#
     7# Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
    58# This program and the accompanying materials
    69# are licensed and made available under the terms and conditions of the BSD License
     
    1518  INF_VERSION                    = 0x00010005
    1619  BASE_NAME                      = TpmCommLib
     20  MODULE_UNI_FILE                = TpmCommLib.uni
    1721  FILE_GUID                      = 7d9fe32e-a6a9-4cdf-abff-10cc7f22e1c9
    1822  MODULE_TYPE                    = PEIM
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette