public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [PATCH 0/4] Defer 3rd party images loading to after EndOfDxe
@ 2016-11-04  0:59 Ruiyu Ni
  2016-11-04  0:59 ` [PATCH 1/4] MdeModulePkg/SecurityStubDxe: Defer 3rd party image before EndOfDxe Ruiyu Ni
                   ` (4 more replies)
  0 siblings, 5 replies; 13+ messages in thread
From: Ruiyu Ni @ 2016-11-04  0:59 UTC (permalink / raw)
  To: edk2-devel

The patches change the default image loading policy by deferring 3rd
party images loading to after EndOfDxe and add a new BDS API to
dispatch the deferred images.

Platform needs to call the new BDS API
EfiBootManagerDispatchDeferredImages after EndOfDxe to ensure that
any deferred images are loaded.

Ruiyu Ni (4):
  MdeModulePkg/SecurityStubDxe: Defer 3rd party image before EndOfDxe
  MdeModulePkg/UefiBootManager: Add EfiBootManagerDispatchDeferredImages
  MdeModulePkg/BdsDxe: Check deferred images before booting to OS
  MdeModulePkg/SecurityStubDxe: Report failure if image is load earlier

 MdeModulePkg/Include/Library/UefiBootManagerLib.h  |  13 +
 MdeModulePkg/Library/UefiBootManagerLib/BmMisc.c   | 113 ++++++
 .../Library/UefiBootManagerLib/InternalBm.h        |   1 +
 .../UefiBootManagerLib/UefiBootManagerLib.inf      |   1 +
 MdeModulePkg/Universal/BdsDxe/Bds.h                |   4 +-
 MdeModulePkg/Universal/BdsDxe/BdsDxe.inf           |   2 +
 MdeModulePkg/Universal/BdsDxe/BdsEntry.c           |  89 +++++
 .../SecurityStubDxe/Defer3rdPartyImageLoad.c       | 413 +++++++++++++++++++++
 .../SecurityStubDxe/Defer3rdPartyImageLoad.h       |  95 +++++
 .../Universal/SecurityStubDxe/SecurityStub.c       |  14 +-
 .../Universal/SecurityStubDxe/SecurityStubDxe.inf  |  11 +-
 11 files changed, 753 insertions(+), 3 deletions(-)
 create mode 100644 MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.c
 create mode 100644 MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.h

-- 
2.9.0.windows.1



^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH 1/4] MdeModulePkg/SecurityStubDxe: Defer 3rd party image before EndOfDxe
  2016-11-04  0:59 [PATCH 0/4] Defer 3rd party images loading to after EndOfDxe Ruiyu Ni
@ 2016-11-04  0:59 ` Ruiyu Ni
  2016-11-10  7:42   ` Zhang, Chao B
  2016-11-04  0:59 ` [PATCH 2/4] MdeModulePkg/UefiBootManager: Add EfiBootManagerDispatchDeferredImages Ruiyu Ni
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 13+ messages in thread
From: Ruiyu Ni @ 2016-11-04  0:59 UTC (permalink / raw)
  To: edk2-devel; +Cc: Liming Gao, Jiewen Yao, Chao B Zhang

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Ruiyu Ni <ruiyu.ni@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Chao B Zhang <chao.b.zhang@intel.com>
---
 .../SecurityStubDxe/Defer3rdPartyImageLoad.c       | 356 +++++++++++++++++++++
 .../SecurityStubDxe/Defer3rdPartyImageLoad.h       |  92 ++++++
 .../Universal/SecurityStubDxe/SecurityStub.c       |  14 +-
 .../Universal/SecurityStubDxe/SecurityStubDxe.inf  |   8 +-
 4 files changed, 468 insertions(+), 2 deletions(-)
 create mode 100644 MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.c
 create mode 100644 MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.h

diff --git a/MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.c b/MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.c
new file mode 100644
index 0000000..ca45d56
--- /dev/null
+++ b/MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.c
@@ -0,0 +1,356 @@
+/** @file
+  Implement defer image load services for user identification in UEFI2.2.
+
+Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
+This program and the accompanying materials
+are licensed and made available under the terms and conditions of the BSD License
+which accompanies this distribution.  The full text of the license may be found at
+http://opensource.org/licenses/bsd-license.php
+
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+#include "Defer3rdPartyImageLoad.h"
+
+//
+// The structure to save the deferred 3rd party image information.
+//
+typedef struct {
+  EFI_DEVICE_PATH_PROTOCOL          *ImageDevicePath;
+  BOOLEAN                           BootOption;
+  BOOLEAN                           Loaded;
+} DEFERRED_3RD_PARTY_IMAGE_INFO;
+
+//
+// The table to save the deferred 3rd party image item.
+//
+typedef struct {
+  UINTN                             Count;         ///< deferred 3rd party image count
+  DEFERRED_3RD_PARTY_IMAGE_INFO     *ImageInfo;    ///< deferred 3rd party image item
+} DEFERRED_3RD_PARTY_IMAGE_TABLE;
+
+BOOLEAN                          mEndOfDxe                   = FALSE;
+DEFERRED_3RD_PARTY_IMAGE_TABLE   mDeferred3rdPartyImage = {
+  0,       // Deferred image count
+  NULL     // The deferred image info
+};
+
+EFI_DEFERRED_IMAGE_LOAD_PROTOCOL mDeferredImageLoad   = {
+  GetDefferedImageInfo
+};
+
+/**
+  Return whether the file comes from FV.
+
+  @param[in]    File    This is a pointer to the device path of the file
+                        that is being dispatched.
+
+  @retval TRUE  File comes from FV.
+  @retval FALSE File doesn't come from FV.
+**/
+BOOLEAN
+FileFromFv (
+  IN  CONST EFI_DEVICE_PATH_PROTOCOL   *File
+  )
+{
+  EFI_STATUS                        Status;
+  EFI_HANDLE                        DeviceHandle;
+  EFI_DEVICE_PATH_PROTOCOL          *TempDevicePath;
+
+  //
+  // First check to see if File is from a Firmware Volume
+  //
+  DeviceHandle   = NULL;
+  TempDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) File;
+  Status = gBS->LocateDevicePath (
+                  &gEfiFirmwareVolume2ProtocolGuid,
+                  &TempDevicePath,
+                  &DeviceHandle
+                  );
+  if (!EFI_ERROR (Status)) {
+    Status = gBS->OpenProtocol (
+                    DeviceHandle,
+                    &gEfiFirmwareVolume2ProtocolGuid,
+                    NULL,
+                    NULL,
+                    NULL,
+                    EFI_OPEN_PROTOCOL_TEST_PROTOCOL
+                    );
+    if (!EFI_ERROR (Status)) {
+      return TRUE;
+    }
+  }
+
+  return FALSE;
+}
+
+/**
+  Find the deferred image which matches the device path.
+
+  @param[in]  ImageDevicePath  A pointer to the device path of a image.
+  @param[in]  BootOption       Whether the image is a boot option.
+
+  @return Pointer to the found deferred image or NULL if not found.
+**/
+DEFERRED_3RD_PARTY_IMAGE_INFO *
+LookupImage (
+  IN  CONST EFI_DEVICE_PATH_PROTOCOL    *ImageDevicePath,
+  IN        BOOLEAN                     BootOption
+  )
+{
+  UINTN                                 Index;
+  UINTN                                 DevicePathSize;
+
+  DevicePathSize = GetDevicePathSize (ImageDevicePath);
+
+  for (Index = 0; Index < mDeferred3rdPartyImage.Count; Index++) {
+    if (CompareMem (ImageDevicePath, mDeferred3rdPartyImage.ImageInfo[Index].ImageDevicePath, DevicePathSize) == 0) {
+      ASSERT (mDeferred3rdPartyImage.ImageInfo[Index].BootOption == BootOption);
+      return &mDeferred3rdPartyImage.ImageInfo[Index];
+    }
+  }
+
+  return NULL;
+}
+
+/**
+  Add the image info to a deferred image list.
+
+  @param[in]  ImageDevicePath  A pointer to the device path of a image.
+  @param[in]  BootOption       Whether the image is a boot option.
+
+**/
+VOID
+QueueImage (
+  IN  CONST EFI_DEVICE_PATH_PROTOCOL    *ImageDevicePath,
+  IN        BOOLEAN                     BootOption
+  )
+{
+  DEFERRED_3RD_PARTY_IMAGE_INFO         *ImageInfo;
+
+  //
+  // Expand memory for the new deferred image.
+  //
+  ImageInfo = ReallocatePool (
+                mDeferred3rdPartyImage.Count * sizeof (DEFERRED_3RD_PARTY_IMAGE_INFO),
+                (mDeferred3rdPartyImage.Count + 1) * sizeof (DEFERRED_3RD_PARTY_IMAGE_INFO),
+                mDeferred3rdPartyImage.ImageInfo
+  );
+  if (ImageInfo == NULL) {
+    return;
+  }
+  mDeferred3rdPartyImage.ImageInfo = ImageInfo;
+
+  //
+  // Save the deferred image information.
+  //
+  ImageInfo = &mDeferred3rdPartyImage.ImageInfo[mDeferred3rdPartyImage.Count];
+  ImageInfo->ImageDevicePath = DuplicateDevicePath (ImageDevicePath);
+  if (ImageInfo->ImageDevicePath == NULL) {
+    return;
+  }
+  ImageInfo->BootOption = BootOption;
+  ImageInfo->Loaded     = FALSE;
+  mDeferred3rdPartyImage.Count++;
+}
+
+
+/**
+  Returns information about a deferred image.
+
+  This function returns information about a single deferred image. The deferred images are
+  numbered consecutively, starting with 0.  If there is no image which corresponds to
+  ImageIndex, then EFI_NOT_FOUND is returned. All deferred images may be returned by
+  iteratively calling this function until EFI_NOT_FOUND is returned.
+  Image may be NULL and ImageSize set to 0 if the decision to defer execution was made
+  because of the location of the executable image, rather than its actual contents.
+
+  @param[in]  This             Points to this instance of the EFI_DEFERRED_IMAGE_LOAD_PROTOCOL.
+  @param[in]  ImageIndex       Zero-based index of the deferred index.
+  @param[out] ImageDevicePath  On return, points to a pointer to the device path of the image.
+                               The device path should not be freed by the caller.
+  @param[out] Image            On return, points to the first byte of the image or NULL if the
+                               image is not available. The image should not be freed by the caller
+                               unless LoadImage() has been successfully called.
+  @param[out] ImageSize        On return, the size of the image, or 0 if the image is not available.
+  @param[out] BootOption       On return, points to TRUE if the image was intended as a boot option
+                               or FALSE if it was not intended as a boot option.
+
+  @retval EFI_SUCCESS           Image information returned successfully.
+  @retval EFI_NOT_FOUND         ImageIndex does not refer to a valid image.
+  @retval EFI_INVALID_PARAMETER ImageDevicePath is NULL or Image is NULL or ImageSize is NULL or
+                                BootOption is NULL.
+
+**/
+EFI_STATUS
+EFIAPI
+GetDefferedImageInfo (
+  IN     EFI_DEFERRED_IMAGE_LOAD_PROTOCOL  *This,
+  IN     UINTN                             ImageIndex,
+     OUT EFI_DEVICE_PATH_PROTOCOL          **ImageDevicePath,
+     OUT VOID                              **Image,
+     OUT UINTN                             *ImageSize,
+     OUT BOOLEAN                           *BootOption
+  )
+{
+  UINTN                                    Index;
+  UINTN                                    NewCount;
+
+  if ((This == NULL) || (ImageSize == NULL) || (Image == NULL)) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  if ((ImageDevicePath == NULL) || (BootOption == NULL)) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  //
+  // Remove the loaded images from the defer list in the first call.
+  //
+  if (ImageIndex == 0) {
+    NewCount = 0;
+    for (Index = 0; Index < mDeferred3rdPartyImage.Count; Index++) {
+      if (!mDeferred3rdPartyImage.ImageInfo[Index].Loaded) {
+        CopyMem (
+          &mDeferred3rdPartyImage.ImageInfo[NewCount],
+          &mDeferred3rdPartyImage.ImageInfo[Index],
+          sizeof (DEFERRED_3RD_PARTY_IMAGE_INFO)
+          );
+        NewCount++;
+      }
+    }
+
+    mDeferred3rdPartyImage.Count = NewCount;
+  }
+
+  if (ImageIndex >= mDeferred3rdPartyImage.Count) {
+    return EFI_NOT_FOUND;
+  }
+
+  //
+  // Get the request deferred image.
+  //
+  *ImageDevicePath = mDeferred3rdPartyImage.ImageInfo[ImageIndex].ImageDevicePath;
+  *BootOption      = mDeferred3rdPartyImage.ImageInfo[ImageIndex].BootOption;
+  *Image           = NULL;
+  *ImageSize       = 0;
+
+  return EFI_SUCCESS;
+}
+
+/**
+  Callback function executed when the EndOfDxe event group is signaled.
+
+  @param[in] Event      Event whose notification function is being invoked.
+  @param[in] Context    The pointer to the notification function's context, which
+                        is implementation-dependent.
+**/
+VOID
+EFIAPI
+EndOfDxe (
+  IN EFI_EVENT  Event,
+  IN VOID       *Context
+  )
+{
+  mEndOfDxe = TRUE;
+}
+
+/**
+  Defer the 3rd party image load and installs Deferred Image Load Protocol.
+
+  @param[in]  File                  This is a pointer to the device path of the file that
+                                    is being dispatched. This will optionally be used for
+                                    logging.
+  @param[in]  BootPolicy            A boot policy that was used to call LoadImage() UEFI service.
+
+  @retval EFI_SUCCESS               The file is not 3rd party image and can be loaded immediately.
+  @retval EFI_ACCESS_DENIED         The file is 3rd party image and needs deferred.
+**/
+EFI_STATUS
+Defer3rdPartyImageLoad (
+  IN  CONST EFI_DEVICE_PATH_PROTOCOL   *File,
+  IN  BOOLEAN                          BootPolicy
+  )
+{
+  DEFERRED_3RD_PARTY_IMAGE_INFO        *ImageInfo;
+
+  //
+  // Ignore if File is NULL.
+  //
+  if (File == NULL) {
+    return EFI_SUCCESS;
+  }
+
+  if (FileFromFv (File)) {
+    return EFI_SUCCESS;
+  }
+
+  ImageInfo = LookupImage (File, BootPolicy);
+
+  DEBUG_CODE (
+    CHAR16 *DevicePathStr;
+    DevicePathStr = ConvertDevicePathToText (File, FALSE, FALSE);
+    DEBUG ((
+      DEBUG_INFO,
+      "[Security] 3rd party image[%p] %s EndOfDxe: %s.\n", ImageInfo,
+      mEndOfDxe ? L"can be loaded after": L"is deferred to load before",
+      DevicePathStr
+      ));
+    if (DevicePathStr != NULL) {
+      FreePool (DevicePathStr);
+    }
+    );
+
+  if (mEndOfDxe) {
+    //
+    // The image might be first time loaded after EndOfDxe,
+    // So ImageInfo can be NULL.
+    //
+    if (ImageInfo != NULL) {
+      ImageInfo->Loaded = TRUE;
+    }
+    return EFI_SUCCESS;
+  } else {
+    //
+    // The image might be second time loaded before EndOfDxe,
+    // So ImageInfo can be non-NULL.
+    //
+    if (ImageInfo == NULL) {
+      QueueImage (File, BootPolicy);
+    }
+    return EFI_ACCESS_DENIED;
+  }
+}
+
+/**
+  Installs DeferredImageLoad Protocol and listens EndOfDxe event.
+**/
+VOID
+Defer3rdPartyImageLoadInitialize (
+  VOID
+  )
+{
+  EFI_STATUS                           Status;
+  EFI_HANDLE                           Handle;
+  EFI_EVENT                            Event;
+
+  Handle = NULL;
+  Status = gBS->InstallMultipleProtocolInterfaces (
+                  &Handle,
+                  &gEfiDeferredImageLoadProtocolGuid,
+                  &mDeferredImageLoad,
+                  NULL
+                  );
+  ASSERT_EFI_ERROR (Status);
+
+  Status = gBS->CreateEventEx (
+                  EVT_NOTIFY_SIGNAL,
+                  TPL_CALLBACK,
+                  EndOfDxe,
+                  NULL,
+                  &gEfiEndOfDxeEventGroupGuid,
+                  &Event
+                  );
+  ASSERT_EFI_ERROR (Status);
+}
diff --git a/MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.h b/MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.h
new file mode 100644
index 0000000..3fab258
--- /dev/null
+++ b/MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.h
@@ -0,0 +1,92 @@
+/** @file
+  Implement defer image load services for user identification in UEFI2.2.
+
+Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
+This program and the accompanying materials
+are licensed and made available under the terms and conditions of the BSD License
+which accompanies this distribution.  The full text of the license may be found at
+http://opensource.org/licenses/bsd-license.php
+
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#ifndef _DEFER_3RD_PARTY_IMAGE_LOAD_H_
+#define _DEFER_3RD_PARTY_IMAGE_LOAD_H_
+
+#include <Uefi.h>
+#include <Guid/EventGroup.h>
+#include <Protocol/DeferredImageLoad.h>
+#include <Protocol/FirmwareVolume2.h>
+
+#include <Library/UefiBootServicesTableLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/MemoryAllocationLib.h>
+#include <Library/DevicePathLib.h>
+#include <Library/DebugLib.h>
+
+/**
+  Returns information about a deferred image.
+
+  This function returns information about a single deferred image. The deferred images are
+  numbered consecutively, starting with 0.  If there is no image which corresponds to
+  ImageIndex, then EFI_NOT_FOUND is returned. All deferred images may be returned by
+  iteratively calling this function until EFI_NOT_FOUND is returned.
+  Image may be NULL and ImageSize set to 0 if the decision to defer execution was made
+  because of the location of the executable image, rather than its actual contents.
+
+  @param[in]  This             Points to this instance of the EFI_DEFERRED_IMAGE_LOAD_PROTOCOL.
+  @param[in]  ImageIndex       Zero-based index of the deferred index.
+  @param[out] ImageDevicePath  On return, points to a pointer to the device path of the image.
+                               The device path should not be freed by the caller.
+  @param[out] Image            On return, points to the first byte of the image or NULL if the
+                               image is not available. The image should not be freed by the caller
+                               unless LoadImage() has been successfully called.
+  @param[out] ImageSize        On return, the size of the image, or 0 if the image is not available.
+  @param[out] BootOption       On return, points to TRUE if the image was intended as a boot option
+                               or FALSE if it was not intended as a boot option.
+
+  @retval EFI_SUCCESS           Image information returned successfully.
+  @retval EFI_NOT_FOUND         ImageIndex does not refer to a valid image.
+  @retval EFI_INVALID_PARAMETER ImageDevicePath is NULL or Image is NULL or ImageSize is NULL or
+                                BootOption is NULL.
+
+**/
+EFI_STATUS
+EFIAPI
+GetDefferedImageInfo (
+  IN     EFI_DEFERRED_IMAGE_LOAD_PROTOCOL  *This,
+  IN     UINTN                             ImageIndex,
+     OUT EFI_DEVICE_PATH_PROTOCOL          **ImageDevicePath,
+     OUT VOID                              **Image,
+     OUT UINTN                             *ImageSize,
+     OUT BOOLEAN                           *BootOption
+  );
+
+/**
+  Defer the 3rd party image load and installs Deferred Image Load Protocol.
+
+  @param[in]  File                  This is a pointer to the device path of the file that
+                                    is being dispatched. This will optionally be used for
+                                    logging.
+  @param[in]  BootPolicy            A boot policy that was used to call LoadImage() UEFI service.
+
+  @retval EFI_SUCCESS               The file is not 3rd party image and can be loaded immediately.
+  @retval EFI_ACCESS_DENIED         The file is 3rd party image and needs deferred.
+**/
+EFI_STATUS
+Defer3rdPartyImageLoad (
+  IN  CONST EFI_DEVICE_PATH_PROTOCOL   *File,
+  IN  BOOLEAN                          BootPolicy
+  );
+
+/**
+  Installs DeferredImageLoad Protocol and listens EndOfDxe event.
+**/
+VOID
+Defer3rdPartyImageLoadInitialize (
+  VOID
+  );
+
+#endif
diff --git a/MdeModulePkg/Universal/SecurityStubDxe/SecurityStub.c b/MdeModulePkg/Universal/SecurityStubDxe/SecurityStub.c
index 22e6c7f..a75d8dc 100644
--- a/MdeModulePkg/Universal/SecurityStubDxe/SecurityStub.c
+++ b/MdeModulePkg/Universal/SecurityStubDxe/SecurityStub.c
@@ -1,7 +1,7 @@
 /** @file
   This driver produces Security2 and Security architectural protocol based on SecurityManagementLib.
  
-  Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
   This program and the accompanying materials                          
   are licensed and made available under the terms and conditions of the BSD License         
   which accompanies this distribution.  The full text of the license may be found at        
@@ -20,6 +20,7 @@
 #include <Library/UefiBootServicesTableLib.h>
 #include <Library/UefiDriverEntryPoint.h>
 #include <Library/SecurityManagementLib.h>
+#include "Defer3rdPartyImageLoad.h"
 
 //
 // Handle for the Security Architectural Protocol instance produced by this driver
@@ -140,6 +141,15 @@ Security2StubAuthenticate (
   IN BOOLEAN                           BootPolicy
   )
 {
+  EFI_STATUS                           Status;
+
+  if (FileBuffer != NULL) {
+    Status = Defer3rdPartyImageLoad (File, BootPolicy);
+    if (EFI_ERROR (Status)) {
+      return Status;
+    }
+  }
+
   return ExecuteSecurity2Handlers (EFI_AUTH_OPERATION_VERIFY_IMAGE | 
                                    EFI_AUTH_OPERATION_DEFER_IMAGE_LOAD | 
                                    EFI_AUTH_OPERATION_MEASURE_IMAGE |
@@ -200,5 +210,7 @@ SecurityStubInitialize (
                   );
   ASSERT_EFI_ERROR (Status);
 
+  Defer3rdPartyImageLoadInitialize ();
+
   return EFI_SUCCESS;
 }
diff --git a/MdeModulePkg/Universal/SecurityStubDxe/SecurityStubDxe.inf b/MdeModulePkg/Universal/SecurityStubDxe/SecurityStubDxe.inf
index 9d1c7b8..be6ce6c 100644
--- a/MdeModulePkg/Universal/SecurityStubDxe/SecurityStubDxe.inf
+++ b/MdeModulePkg/Universal/SecurityStubDxe/SecurityStubDxe.inf
@@ -1,7 +1,7 @@
 ## @file
 #  This driver produces security2 and security architectural protocol based on SecurityManagementLib.
 #
-#  Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
+#  Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
 #  This program and the accompanying materials
 #  are licensed and made available under the terms and conditions of the BSD License
 #  which accompanies this distribution. The full text of the license may be found at
@@ -29,6 +29,8 @@ [Defines]
 
 [Sources]
   SecurityStub.c
+  Defer3rdPartyImageLoad.c
+  Defer3rdPartyImageLoad.h
 
 [Packages]
   MdePkg/MdePkg.dec
@@ -40,9 +42,13 @@ [LibraryClasses]
   DebugLib
   SecurityManagementLib
 
+[Guids]
+  gEfiEndOfDxeEventGroupGuid                    ## CONSUMES ## Event
+
 [Protocols]
   gEfiSecurityArchProtocolGuid                  ## PRODUCES
   gEfiSecurity2ArchProtocolGuid                 ## PRODUCES
+  gEfiDeferredImageLoadProtocolGuid             ## PRODUCES
 
 [Depex]
   TRUE
-- 
2.9.0.windows.1



^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH 2/4] MdeModulePkg/UefiBootManager: Add EfiBootManagerDispatchDeferredImages
  2016-11-04  0:59 [PATCH 0/4] Defer 3rd party images loading to after EndOfDxe Ruiyu Ni
  2016-11-04  0:59 ` [PATCH 1/4] MdeModulePkg/SecurityStubDxe: Defer 3rd party image before EndOfDxe Ruiyu Ni
@ 2016-11-04  0:59 ` Ruiyu Ni
  2016-11-04  0:59 ` [PATCH 3/4] MdeModulePkg/BdsDxe: Check deferred images before booting to OS Ruiyu Ni
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 13+ messages in thread
From: Ruiyu Ni @ 2016-11-04  0:59 UTC (permalink / raw)
  To: edk2-devel; +Cc: Liming Gao, Chao B Zhang

The API dispatches the deferred images that are returned from all
DeferredImageLoad instances.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Ruiyu Ni <ruiyu.ni@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Chao B Zhang <chao.b.zhang@intel.com>
---
 MdeModulePkg/Include/Library/UefiBootManagerLib.h  |  13 +++
 MdeModulePkg/Library/UefiBootManagerLib/BmMisc.c   | 113 +++++++++++++++++++++
 .../Library/UefiBootManagerLib/InternalBm.h        |   1 +
 .../UefiBootManagerLib/UefiBootManagerLib.inf      |   1 +
 4 files changed, 128 insertions(+)

diff --git a/MdeModulePkg/Include/Library/UefiBootManagerLib.h b/MdeModulePkg/Include/Library/UefiBootManagerLib.h
index e333ffd..97ac1f2 100644
--- a/MdeModulePkg/Include/Library/UefiBootManagerLib.h
+++ b/MdeModulePkg/Include/Library/UefiBootManagerLib.h
@@ -777,4 +777,17 @@ EfiBootManagerIsValidLoadOptionVariableName (
   OUT UINT16                            *OptionNumber OPTIONAL
   );
 
+
+/**
+  Dispatch the deferred images that are returned from all DeferredImageLoad instances.
+
+  @retval EFI_SUCCESS       At least one deferred image is loaded successfully and started.
+  @retval EFI_NOT_FOUND     There is no deferred image.
+  @retval EFI_ACCESS_DENIED There are deferred images but all of them are failed to load.
+**/
+EFI_STATUS
+EFIAPI
+EfiBootManagerDispatchDeferredImages (
+  VOID
+  );
 #endif
diff --git a/MdeModulePkg/Library/UefiBootManagerLib/BmMisc.c b/MdeModulePkg/Library/UefiBootManagerLib/BmMisc.c
index 2a60f06..09e4211 100644
--- a/MdeModulePkg/Library/UefiBootManagerLib/BmMisc.c
+++ b/MdeModulePkg/Library/UefiBootManagerLib/BmMisc.c
@@ -417,3 +417,116 @@ BmCharToUint (
   return (UINTN) -1;
 }
 
+/**
+  Dispatch the deferred images that are returned from all DeferredImageLoad instances.
+
+  @retval EFI_SUCCESS       At least one deferred image is loaded successfully and started.
+  @retval EFI_NOT_FOUND     There is no deferred image.
+  @retval EFI_ACCESS_DENIED There are deferred images but all of them are failed to load.
+**/
+EFI_STATUS
+EFIAPI
+EfiBootManagerDispatchDeferredImages (
+  VOID
+  )
+{
+  EFI_STATUS                         Status;
+  EFI_DEFERRED_IMAGE_LOAD_PROTOCOL   *DeferredImage;
+  UINTN                              HandleCount;
+  EFI_HANDLE                         *Handles;
+  UINTN                              Index;
+  UINTN                              ImageIndex;
+  EFI_DEVICE_PATH_PROTOCOL           *ImageDevicePath;
+  VOID                               *Image;
+  UINTN                              ImageSize;
+  BOOLEAN                            BootOption;
+  EFI_HANDLE                         ImageHandle;
+  UINTN                              ExitDataSize;
+  CHAR16                             *ExitData;
+  UINTN                              ImageCount;
+  UINTN                              LoadCount;
+
+  //
+  // Find all the deferred image load protocols.
+  //
+  HandleCount = 0;
+  Handles = NULL;
+  Status = gBS->LocateHandleBuffer (
+    ByProtocol,
+    &gEfiDeferredImageLoadProtocolGuid,
+    NULL,
+    &HandleCount,
+    &Handles
+  );
+  if (EFI_ERROR (Status)) {
+    return EFI_NOT_FOUND;
+  }
+
+  ImageCount = 0;
+  LoadCount  = 0;
+  for (Index = 0; Index < HandleCount; Index++) {
+    Status = gBS->HandleProtocol (Handles[Index], &gEfiDeferredImageLoadProtocolGuid, (VOID **) &DeferredImage);
+    if (EFI_ERROR (Status)) {
+      continue;
+    }
+
+    for (ImageIndex = 0; ;ImageIndex++) {
+      //
+      // Load all the deferred images in this protocol instance.
+      //
+      Status = DeferredImage->GetImageInfo (
+                                DeferredImage,
+                                ImageIndex,
+                                &ImageDevicePath,
+                                (VOID **) &Image,
+                                &ImageSize,
+                                &BootOption
+                                );
+      if (EFI_ERROR (Status)) {
+        break;
+      }
+      ImageCount++;
+      //
+      // Load and start the image.
+      //
+      Status = gBS->LoadImage (
+        BootOption,
+        gImageHandle,
+        ImageDevicePath,
+        NULL,
+        0,
+        &ImageHandle
+      );
+      if (!EFI_ERROR (Status)) {
+        LoadCount++;
+        //
+        // Before calling the image, enable the Watchdog Timer for
+        // a 5 Minute period
+        //
+        gBS->SetWatchdogTimer (5 * 60, 0x0000, 0x00, NULL);
+        Status = gBS->StartImage (ImageHandle, &ExitDataSize, &ExitData);
+        if (ExitData != NULL) {
+          FreePool (ExitData);
+        }
+
+        //
+        // Clear the Watchdog Timer after the image returns.
+        //
+        gBS->SetWatchdogTimer (0x0000, 0x0000, 0x0000, NULL);
+      }
+    }
+  }
+  if (Handles != NULL) {
+    FreePool (Handles);
+  }
+
+  if (ImageCount == 0) {
+    return EFI_NOT_FOUND;
+  } else {
+    if (LoadCount == 0) {
+      return EFI_ACCESS_DENIED;
+    } else {
+      return EFI_SUCCESS;
+    }
+  }
+}
diff --git a/MdeModulePkg/Library/UefiBootManagerLib/InternalBm.h b/MdeModulePkg/Library/UefiBootManagerLib/InternalBm.h
index cb719e9..444d4a5 100644
--- a/MdeModulePkg/Library/UefiBootManagerLib/InternalBm.h
+++ b/MdeModulePkg/Library/UefiBootManagerLib/InternalBm.h
@@ -44,6 +44,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include <Protocol/FormBrowser2.h>
 #include <Protocol/VariableLock.h>
 #include <Protocol/RamDisk.h>
+#include <Protocol/DeferredImageLoad.h>
 
 #include <Guid/MemoryTypeInformation.h>
 #include <Guid/FileInfo.h>
diff --git a/MdeModulePkg/Library/UefiBootManagerLib/UefiBootManagerLib.inf b/MdeModulePkg/Library/UefiBootManagerLib/UefiBootManagerLib.inf
index 8c3fd7f..bb7c00d 100644
--- a/MdeModulePkg/Library/UefiBootManagerLib/UefiBootManagerLib.inf
+++ b/MdeModulePkg/Library/UefiBootManagerLib/UefiBootManagerLib.inf
@@ -109,6 +109,7 @@ [Protocols]
   gEfiDriverHealthProtocolGuid                  ## SOMETIMES_CONSUMES
   gEfiFormBrowser2ProtocolGuid                  ## SOMETIMES_CONSUMES
   gEfiRamDiskProtocolGuid                       ## SOMETIMES_CONSUMES
+  gEfiDeferredImageLoadProtocolGuid             ## CONSUMES
 
 [Pcd]
   gEfiMdeModulePkgTokenSpaceGuid.PcdResetOnMemoryTypeInformationChange      ## SOMETIMES_CONSUMES
-- 
2.9.0.windows.1



^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH 3/4] MdeModulePkg/BdsDxe: Check deferred images before booting to OS
  2016-11-04  0:59 [PATCH 0/4] Defer 3rd party images loading to after EndOfDxe Ruiyu Ni
  2016-11-04  0:59 ` [PATCH 1/4] MdeModulePkg/SecurityStubDxe: Defer 3rd party image before EndOfDxe Ruiyu Ni
  2016-11-04  0:59 ` [PATCH 2/4] MdeModulePkg/UefiBootManager: Add EfiBootManagerDispatchDeferredImages Ruiyu Ni
@ 2016-11-04  0:59 ` Ruiyu Ni
  2016-11-04  0:59 ` [PATCH 4/4] MdeModulePkg/SecurityStubDxe: Report failure if image is load earlier Ruiyu Ni
  2016-11-04  5:14 ` [PATCH 0/4] Defer 3rd party images loading to after EndOfDxe Gao, Liming
  4 siblings, 0 replies; 13+ messages in thread
From: Ruiyu Ni @ 2016-11-04  0:59 UTC (permalink / raw)
  To: edk2-devel; +Cc: Liming Gao, Chao B Zhang

The patch adds check of deferred images before booting to OS.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Ruiyu Ni <ruiyu.ni@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Chao B Zhang <chao.b.zhang@intel.com>
---
 MdeModulePkg/Universal/BdsDxe/Bds.h      |  4 +-
 MdeModulePkg/Universal/BdsDxe/BdsDxe.inf |  2 +
 MdeModulePkg/Universal/BdsDxe/BdsEntry.c | 89 ++++++++++++++++++++++++++++++++
 3 files changed, 94 insertions(+), 1 deletion(-)

diff --git a/MdeModulePkg/Universal/BdsDxe/Bds.h b/MdeModulePkg/Universal/BdsDxe/Bds.h
index d243932..1f8a192 100644
--- a/MdeModulePkg/Universal/BdsDxe/Bds.h
+++ b/MdeModulePkg/Universal/BdsDxe/Bds.h
@@ -1,7 +1,7 @@
 /** @file
   Head file for BDS Architectural Protocol implementation
 
-Copyright (c) 2004 - 2015, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2004 - 2016, Intel Corporation. All rights reserved.<BR>
 This program and the accompanying materials
 are licensed and made available under the terms and conditions of the BSD License
 which accompanies this distribution.  The full text of the license may be found at
@@ -20,10 +20,12 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include <Guid/ConnectConInEvent.h>
 #include <Guid/Performance.h>
 #include <Guid/StatusCodeDataTypeVariable.h>
+#include <Guid/EventGroup.h>
 
 #include <Protocol/Bds.h>
 #include <Protocol/LoadedImage.h>
 #include <Protocol/VariableLock.h>
+#include <Protocol/DeferredImageLoad.h>
 
 #include <Library/UefiDriverEntryPoint.h>
 #include <Library/DebugLib.h>
diff --git a/MdeModulePkg/Universal/BdsDxe/BdsDxe.inf b/MdeModulePkg/Universal/BdsDxe/BdsDxe.inf
index e03cfde..a00b442 100644
--- a/MdeModulePkg/Universal/BdsDxe/BdsDxe.inf
+++ b/MdeModulePkg/Universal/BdsDxe/BdsDxe.inf
@@ -79,11 +79,13 @@ [Guids]
   gConnectConInEventGuid                        ## SOMETIMES_CONSUMES ## Event
   gEdkiiStatusCodeDataTypeVariableGuid          ## SOMETIMES_CONSUMES ## GUID
   gPerformanceProtocolGuid                      ## SOMETIMES_PRODUCES ## Variable:L"PerfDataMemAddr" (The ACPI address of performance data)
+  gEfiEventReadyToBootGuid                      ## CONSUMES           ## Event
 
 [Protocols]
   gEfiBdsArchProtocolGuid                       ## PRODUCES
   gEfiSimpleTextInputExProtocolGuid             ## CONSUMES
   gEdkiiVariableLockProtocolGuid                ## CONSUMES
+  gEfiDeferredImageLoadProtocolGuid             ## CONSUMES
 
 [FeaturePcd]
   gEfiMdePkgTokenSpaceGuid.PcdUefiVariableDefaultLangDeprecate    ## CONSUMES
diff --git a/MdeModulePkg/Universal/BdsDxe/BdsEntry.c b/MdeModulePkg/Universal/BdsDxe/BdsEntry.c
index aacc4a6..ffa884a 100644
--- a/MdeModulePkg/Universal/BdsDxe/BdsEntry.c
+++ b/MdeModulePkg/Universal/BdsDxe/BdsEntry.c
@@ -86,7 +86,83 @@ BdsDxeOnConnectConInCallBack (
     DEBUG ((EFI_D_WARN, "[Bds] Connect ConIn failed - %r!!!\n", Status));
   }
 }
+/**
+  Notify function for event group EFI_EVENT_GROUP_READY_TO_BOOT. This is used to
+  check whether there is remaining deferred load images.
+
+  @param[in]  Event   The Event that is being processed.
+  @param[in]  Context The Event Context.
+
+**/
+VOID
+EFIAPI
+CheckDeferredLoadImageOnReadyToBoot (
+  IN EFI_EVENT        Event,
+  IN VOID             *Context
+  )
+{
+  EFI_STATUS                         Status;
+  EFI_DEFERRED_IMAGE_LOAD_PROTOCOL   *DeferredImage;
+  UINTN                              HandleCount;
+  EFI_HANDLE                         *Handles;
+  UINTN                              Index;
+  UINTN                              ImageIndex;
+  EFI_DEVICE_PATH_PROTOCOL           *ImageDevicePath;
+  VOID                               *Image;
+  UINTN                              ImageSize;
+  BOOLEAN                            BootOption;
+
+  //
+  // Find all the deferred image load protocols.
+  //
+  HandleCount = 0;
+  Handles = NULL;
+  Status = gBS->LocateHandleBuffer (
+    ByProtocol,
+    &gEfiDeferredImageLoadProtocolGuid,
+    NULL,
+    &HandleCount,
+    &Handles
+  );
+  if (EFI_ERROR (Status)) {
+    return;
+  }
+
+  for (Index = 0; Index < HandleCount; Index++) {
+    Status = gBS->HandleProtocol (Handles[Index], &gEfiDeferredImageLoadProtocolGuid, (VOID **) &DeferredImage);
+    if (EFI_ERROR (Status)) {
+      continue;
+    }
 
+    for (ImageIndex = 0; ; ImageIndex++) {
+      //
+      // Load all the deferred images in this protocol instance.
+      //
+      Status = DeferredImage->GetImageInfo (
+        DeferredImage,
+        ImageIndex,
+        &ImageDevicePath,
+        (VOID **) &Image,
+        &ImageSize,
+        &BootOption
+      );
+      if (EFI_ERROR (Status)) {
+        break;
+      }
+      DEBUG_CODE (
+        CHAR16    *DevicePathStr;
+        DevicePathStr = ConvertDevicePathToText (ImageDevicePath, FALSE, FALSE);
+        DEBUG ((DEBUG_LOAD, "[Bds] Image was deferred but not loaded: %s.\n", DevicePathStr));
+        if (DevicePathStr != NULL) {
+          FreePool (DevicePathStr);
+        }
+        );
+    }
+  }
+  if (Handles != NULL) {
+    FreePool (Handles);
+  }
+}
 /**
 
   Install Boot Device Selection Protocol
@@ -108,6 +184,7 @@ BdsInitialize (
 {
   EFI_STATUS  Status;
   EFI_HANDLE  Handle;
+  EFI_EVENT   Event;
   //
   // Install protocol interface
   //
@@ -119,6 +196,18 @@ BdsInitialize (
                   );
   ASSERT_EFI_ERROR (Status);
 
+  //
+  // Register notify function to check deferred images on ReadyToBoot Event.
+  //
+  Status = gBS->CreateEventEx (
+                  EVT_NOTIFY_SIGNAL,
+                  TPL_CALLBACK,
+                  CheckDeferredLoadImageOnReadyToBoot,
+                  NULL,
+                  &gEfiEventReadyToBootGuid,
+                  &Event
+                  );
+  ASSERT_EFI_ERROR (Status);
   return Status;
 }
 
-- 
2.9.0.windows.1



^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH 4/4] MdeModulePkg/SecurityStubDxe: Report failure if image is load earlier
  2016-11-04  0:59 [PATCH 0/4] Defer 3rd party images loading to after EndOfDxe Ruiyu Ni
                   ` (2 preceding siblings ...)
  2016-11-04  0:59 ` [PATCH 3/4] MdeModulePkg/BdsDxe: Check deferred images before booting to OS Ruiyu Ni
@ 2016-11-04  0:59 ` Ruiyu Ni
  2016-11-07 14:07   ` Laszlo Ersek
  2016-11-10  7:27   ` Yao, Jiewen
  2016-11-04  5:14 ` [PATCH 0/4] Defer 3rd party images loading to after EndOfDxe Gao, Liming
  4 siblings, 2 replies; 13+ messages in thread
From: Ruiyu Ni @ 2016-11-04  0:59 UTC (permalink / raw)
  To: edk2-devel; +Cc: Jiewen Yao

The 3rd party image should be loaded after EndOfDxe event signal and
DxeSmmReadyToLock protocol installation. But non-SMM platform doesn't
published DxeSmmReadyToLock protocol.
So the SecurityStubDxe can only depend on EndOfDxe event.

This patch enhances the SecurityStubDxe to listen on
DxeSmmReadyToLock protocol installation and if any 3rd party image
is loaded before DxeSmmReadyToLock, it reports failure.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Ruiyu Ni <ruiyu.ni@intel.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
---
 .../SecurityStubDxe/Defer3rdPartyImageLoad.c       | 57 ++++++++++++++++++++++
 .../SecurityStubDxe/Defer3rdPartyImageLoad.h       |  5 +-
 .../Universal/SecurityStubDxe/SecurityStubDxe.inf  |  3 ++
 3 files changed, 64 insertions(+), 1 deletion(-)

diff --git a/MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.c b/MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.c
index ca45d56..84d573b 100644
--- a/MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.c
+++ b/MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.c
@@ -30,6 +30,7 @@ typedef struct {
   DEFERRED_3RD_PARTY_IMAGE_INFO     *ImageInfo;    ///< deferred 3rd party image item
 } DEFERRED_3RD_PARTY_IMAGE_TABLE;
 
+BOOLEAN                          mImageLoadedAfterEndOfDxe   = FALSE;
 BOOLEAN                          mEndOfDxe                   = FALSE;
 DEFERRED_3RD_PARTY_IMAGE_TABLE   mDeferred3rdPartyImage = {
   0,       // Deferred image count
@@ -257,6 +258,52 @@ EndOfDxe (
 }
 
 /**
+  Event notification for gEfiDxeSmmReadyToLockProtocolGuid event.
+
+  This function reports failure if any deferred image is loaded before
+  this callback.
+  Platform should publish ReadyToLock protocol immediately after signaling
+  of the End of DXE Event.
+
+  @param  Event                 The Event that is being processed, not used.
+  @param  Context               Event Context, not used.
+
+**/
+VOID
+EFIAPI
+DxdSmmReadyToLock (
+  IN EFI_EVENT  Event,
+  IN VOID       *Context
+  )
+{
+  EFI_STATUS                Status;
+  VOID                      *Interface;
+
+  Status = gBS->LocateProtocol (&gEfiDxeSmmReadyToLockProtocolGuid, NULL, &Interface);
+  if (EFI_ERROR (Status)) {
+    return;
+  }
+
+  gBS->CloseEvent (Event);
+
+  if (mImageLoadedAfterEndOfDxe) {
+    //
+    // Platform should not dispatch the 3rd party images after signaling EndOfDxe event
+    // but before publishing DxeSmmReadyToLock protocol.
+    //
+    DEBUG ((
+      DEBUG_ERROR,
+      "[Security] 3rd party images must be dispatched after DxeSmmReadyToLock Protocol installation!\n"
+      ));
+    REPORT_STATUS_CODE (
+      EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED,
+      (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_EC_ILLEGAL_SOFTWARE_STATE)
+      );
+    ASSERT (FALSE);
+  }
+}
+
+/**
   Defer the 3rd party image load and installs Deferred Image Load Protocol.
 
   @param[in]  File                  This is a pointer to the device path of the file that
@@ -303,6 +350,7 @@ Defer3rdPartyImageLoad (
     );
 
   if (mEndOfDxe) {
+    mImageLoadedAfterEndOfDxe = TRUE;
     //
     // The image might be first time loaded after EndOfDxe,
     // So ImageInfo can be NULL.
@@ -334,6 +382,7 @@ Defer3rdPartyImageLoadInitialize (
   EFI_STATUS                           Status;
   EFI_HANDLE                           Handle;
   EFI_EVENT                            Event;
+  VOID                                 *Registration;
 
   Handle = NULL;
   Status = gBS->InstallMultipleProtocolInterfaces (
@@ -353,4 +402,12 @@ Defer3rdPartyImageLoadInitialize (
                   &Event
                   );
   ASSERT_EFI_ERROR (Status);
+
+  EfiCreateProtocolNotifyEvent (
+    &gEfiDxeSmmReadyToLockProtocolGuid,
+    TPL_CALLBACK,
+    DxdSmmReadyToLock,
+    NULL,
+    &Registration
+    );
 }
diff --git a/MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.h b/MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.h
index 3fab258..75553ba 100644
--- a/MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.h
+++ b/MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.h
@@ -15,16 +15,19 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #ifndef _DEFER_3RD_PARTY_IMAGE_LOAD_H_
 #define _DEFER_3RD_PARTY_IMAGE_LOAD_H_
 
-#include <Uefi.h>
+#include <PiDxe.h>
 #include <Guid/EventGroup.h>
 #include <Protocol/DeferredImageLoad.h>
 #include <Protocol/FirmwareVolume2.h>
+#include <Protocol/DxeSmmReadyToLock.h>
 
 #include <Library/UefiBootServicesTableLib.h>
 #include <Library/BaseMemoryLib.h>
 #include <Library/MemoryAllocationLib.h>
 #include <Library/DevicePathLib.h>
 #include <Library/DebugLib.h>
+#include <Library/UefiLib.h>
+#include <Library/ReportStatusCodeLib.h>
 
 /**
   Returns information about a deferred image.
diff --git a/MdeModulePkg/Universal/SecurityStubDxe/SecurityStubDxe.inf b/MdeModulePkg/Universal/SecurityStubDxe/SecurityStubDxe.inf
index be6ce6c..7f8f6cb 100644
--- a/MdeModulePkg/Universal/SecurityStubDxe/SecurityStubDxe.inf
+++ b/MdeModulePkg/Universal/SecurityStubDxe/SecurityStubDxe.inf
@@ -41,6 +41,8 @@ [LibraryClasses]
   UefiBootServicesTableLib
   DebugLib
   SecurityManagementLib
+  ReportStatusCodeLib
+  UefiLib
 
 [Guids]
   gEfiEndOfDxeEventGroupGuid                    ## CONSUMES ## Event
@@ -49,6 +51,7 @@ [Protocols]
   gEfiSecurityArchProtocolGuid                  ## PRODUCES
   gEfiSecurity2ArchProtocolGuid                 ## PRODUCES
   gEfiDeferredImageLoadProtocolGuid             ## PRODUCES
+  gEfiDxeSmmReadyToLockProtocolGuid             ## CONSUMES
 
 [Depex]
   TRUE
-- 
2.9.0.windows.1



^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH 0/4] Defer 3rd party images loading to after EndOfDxe
  2016-11-04  0:59 [PATCH 0/4] Defer 3rd party images loading to after EndOfDxe Ruiyu Ni
                   ` (3 preceding siblings ...)
  2016-11-04  0:59 ` [PATCH 4/4] MdeModulePkg/SecurityStubDxe: Report failure if image is load earlier Ruiyu Ni
@ 2016-11-04  5:14 ` Gao, Liming
  2016-11-04  6:09   ` Ni, Ruiyu
  4 siblings, 1 reply; 13+ messages in thread
From: Gao, Liming @ 2016-11-04  5:14 UTC (permalink / raw)
  To: Ni, Ruiyu, edk2-devel@lists.01.org

Ray:
  Seemly, PlatformBdsLib library instance should call EfiBootManagerDispatchDeferredImages(), right? Are there patches to update PlatformBdsLib library instance?

Thanks
Liming
> -----Original Message-----
> From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of
> Ruiyu Ni
> Sent: Friday, November 04, 2016 9:00 AM
> To: edk2-devel@lists.01.org
> Subject: [edk2] [PATCH 0/4] Defer 3rd party images loading to after
> EndOfDxe
> 
> The patches change the default image loading policy by deferring 3rd
> party images loading to after EndOfDxe and add a new BDS API to
> dispatch the deferred images.
> 
> Platform needs to call the new BDS API
> EfiBootManagerDispatchDeferredImages after EndOfDxe to ensure that
> any deferred images are loaded.
> 
> Ruiyu Ni (4):
>   MdeModulePkg/SecurityStubDxe: Defer 3rd party image before EndOfDxe
>   MdeModulePkg/UefiBootManager: Add
> EfiBootManagerDispatchDeferredImages
>   MdeModulePkg/BdsDxe: Check deferred images before booting to OS
>   MdeModulePkg/SecurityStubDxe: Report failure if image is load earlier
> 
>  MdeModulePkg/Include/Library/UefiBootManagerLib.h  |  13 +
>  MdeModulePkg/Library/UefiBootManagerLib/BmMisc.c   | 113 ++++++
>  .../Library/UefiBootManagerLib/InternalBm.h        |   1 +
>  .../UefiBootManagerLib/UefiBootManagerLib.inf      |   1 +
>  MdeModulePkg/Universal/BdsDxe/Bds.h                |   4 +-
>  MdeModulePkg/Universal/BdsDxe/BdsDxe.inf           |   2 +
>  MdeModulePkg/Universal/BdsDxe/BdsEntry.c           |  89 +++++
>  .../SecurityStubDxe/Defer3rdPartyImageLoad.c       | 413
> +++++++++++++++++++++
>  .../SecurityStubDxe/Defer3rdPartyImageLoad.h       |  95 +++++
>  .../Universal/SecurityStubDxe/SecurityStub.c       |  14 +-
>  .../Universal/SecurityStubDxe/SecurityStubDxe.inf  |  11 +-
>  11 files changed, 753 insertions(+), 3 deletions(-)
>  create mode 100644
> MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.c
>  create mode 100644
> MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.h
> 
> --
> 2.9.0.windows.1
> 
> _______________________________________________
> edk2-devel mailing list
> edk2-devel@lists.01.org
> https://lists.01.org/mailman/listinfo/edk2-devel


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 0/4] Defer 3rd party images loading to after EndOfDxe
  2016-11-04  5:14 ` [PATCH 0/4] Defer 3rd party images loading to after EndOfDxe Gao, Liming
@ 2016-11-04  6:09   ` Ni, Ruiyu
  2016-11-04 16:48     ` Laszlo Ersek
  0 siblings, 1 reply; 13+ messages in thread
From: Ni, Ruiyu @ 2016-11-04  6:09 UTC (permalink / raw)
  To: Gao, Liming, edk2-devel@lists.01.org

No.
The open source platform patch will be sent out later.

Regards,
Ray

From: Gao, Liming
Sent: Friday, November 4, 2016 1:14 PM
To: Ni, Ruiyu <ruiyu.ni@intel.com>; edk2-devel@lists.01.org
Subject: RE: [edk2] [PATCH 0/4] Defer 3rd party images loading to after EndOfDxe

Ray:
  Seemly, PlatformBdsLib library instance should call EfiBootManagerDispatchDeferredImages(), right? Are there patches to update PlatformBdsLib library instance?

Thanks
Liming
> -----Original Message-----
> From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of
> Ruiyu Ni
> Sent: Friday, November 04, 2016 9:00 AM
> To: edk2-devel@lists.01.org<mailto:edk2-devel@lists.01.org>
> Subject: [edk2] [PATCH 0/4] Defer 3rd party images loading to after
> EndOfDxe
>
> The patches change the default image loading policy by deferring 3rd
> party images loading to after EndOfDxe and add a new BDS API to
> dispatch the deferred images.
>
> Platform needs to call the new BDS API
> EfiBootManagerDispatchDeferredImages after EndOfDxe to ensure that
> any deferred images are loaded.
>
> Ruiyu Ni (4):
>   MdeModulePkg/SecurityStubDxe: Defer 3rd party image before EndOfDxe
>   MdeModulePkg/UefiBootManager: Add
> EfiBootManagerDispatchDeferredImages
>   MdeModulePkg/BdsDxe: Check deferred images before booting to OS
>   MdeModulePkg/SecurityStubDxe: Report failure if image is load earlier
>
>  MdeModulePkg/Include/Library/UefiBootManagerLib.h  |  13 +
>  MdeModulePkg/Library/UefiBootManagerLib/BmMisc.c   | 113 ++++++
>  .../Library/UefiBootManagerLib/InternalBm.h        |   1 +
>  .../UefiBootManagerLib/UefiBootManagerLib.inf      |   1 +
>  MdeModulePkg/Universal/BdsDxe/Bds.h                |   4 +-
>  MdeModulePkg/Universal/BdsDxe/BdsDxe.inf           |   2 +
>  MdeModulePkg/Universal/BdsDxe/BdsEntry.c           |  89 +++++
>  .../SecurityStubDxe/Defer3rdPartyImageLoad.c       | 413
> +++++++++++++++++++++
>  .../SecurityStubDxe/Defer3rdPartyImageLoad.h       |  95 +++++
>  .../Universal/SecurityStubDxe/SecurityStub.c       |  14 +-
>  .../Universal/SecurityStubDxe/SecurityStubDxe.inf  |  11 +-
>  11 files changed, 753 insertions(+), 3 deletions(-)
>  create mode 100644
> MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.c
>  create mode 100644
> MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.h
>
> --
> 2.9.0.windows.1
>
> _______________________________________________
> edk2-devel mailing list
> edk2-devel@lists.01.org<mailto:edk2-devel@lists.01.org>
> https://lists.01.org/mailman/listinfo/edk2-devel


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 0/4] Defer 3rd party images loading to after EndOfDxe
  2016-11-04  6:09   ` Ni, Ruiyu
@ 2016-11-04 16:48     ` Laszlo Ersek
  2016-11-07  2:32       ` Ni, Ruiyu
  0 siblings, 1 reply; 13+ messages in thread
From: Laszlo Ersek @ 2016-11-04 16:48 UTC (permalink / raw)
  To: Ni, Ruiyu, Gao, Liming, edk2-devel@lists.01.org

On 11/04/16 07:09, Ni, Ruiyu wrote:
> No.
> The open source platform patch will be sent out later.

What are the deferred / 3rd party images? Do Driver#### and SysPrep####
qualify?

Or, is this related to value 3 ("Defer execution when there is security
violation") of:
- PcdOptionRomImageVerificationPolicy,
- PcdRemovableMediaImageVerificationPolicy,
- PcdFixedMediaImageVerificationPolicy?

Is the deferral documented somewhere in the UEFI spec, or do we have a
Mantis ticket / ECR about it?

Can we improve the commit messages please? From them, I have no idea how
the deferral is supposed to work. (I.e., what the agents are, and how
they interact.)

Thanks
Laszlo

> From: Gao, Liming
> Sent: Friday, November 4, 2016 1:14 PM
> To: Ni, Ruiyu <ruiyu.ni@intel.com>; edk2-devel@lists.01.org
> Subject: RE: [edk2] [PATCH 0/4] Defer 3rd party images loading to after EndOfDxe
> 
> Ray:
>   Seemly, PlatformBdsLib library instance should call EfiBootManagerDispatchDeferredImages(), right? Are there patches to update PlatformBdsLib library instance?
> 
> Thanks
> Liming
>> -----Original Message-----
>> From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of
>> Ruiyu Ni
>> Sent: Friday, November 04, 2016 9:00 AM
>> To: edk2-devel@lists.01.org<mailto:edk2-devel@lists.01.org>
>> Subject: [edk2] [PATCH 0/4] Defer 3rd party images loading to after
>> EndOfDxe
>>
>> The patches change the default image loading policy by deferring 3rd
>> party images loading to after EndOfDxe and add a new BDS API to
>> dispatch the deferred images.
>>
>> Platform needs to call the new BDS API
>> EfiBootManagerDispatchDeferredImages after EndOfDxe to ensure that
>> any deferred images are loaded.
>>
>> Ruiyu Ni (4):
>>   MdeModulePkg/SecurityStubDxe: Defer 3rd party image before EndOfDxe
>>   MdeModulePkg/UefiBootManager: Add
>> EfiBootManagerDispatchDeferredImages
>>   MdeModulePkg/BdsDxe: Check deferred images before booting to OS
>>   MdeModulePkg/SecurityStubDxe: Report failure if image is load earlier
>>
>>  MdeModulePkg/Include/Library/UefiBootManagerLib.h  |  13 +
>>  MdeModulePkg/Library/UefiBootManagerLib/BmMisc.c   | 113 ++++++
>>  .../Library/UefiBootManagerLib/InternalBm.h        |   1 +
>>  .../UefiBootManagerLib/UefiBootManagerLib.inf      |   1 +
>>  MdeModulePkg/Universal/BdsDxe/Bds.h                |   4 +-
>>  MdeModulePkg/Universal/BdsDxe/BdsDxe.inf           |   2 +
>>  MdeModulePkg/Universal/BdsDxe/BdsEntry.c           |  89 +++++
>>  .../SecurityStubDxe/Defer3rdPartyImageLoad.c       | 413
>> +++++++++++++++++++++
>>  .../SecurityStubDxe/Defer3rdPartyImageLoad.h       |  95 +++++
>>  .../Universal/SecurityStubDxe/SecurityStub.c       |  14 +-
>>  .../Universal/SecurityStubDxe/SecurityStubDxe.inf  |  11 +-
>>  11 files changed, 753 insertions(+), 3 deletions(-)
>>  create mode 100644
>> MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.c
>>  create mode 100644
>> MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.h
>>
>> --
>> 2.9.0.windows.1
>>
>> _______________________________________________
>> edk2-devel mailing list
>> edk2-devel@lists.01.org<mailto:edk2-devel@lists.01.org>
>> https://lists.01.org/mailman/listinfo/edk2-devel
> _______________________________________________
> edk2-devel mailing list
> edk2-devel@lists.01.org
> https://lists.01.org/mailman/listinfo/edk2-devel
> 



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 0/4] Defer 3rd party images loading to after EndOfDxe
  2016-11-04 16:48     ` Laszlo Ersek
@ 2016-11-07  2:32       ` Ni, Ruiyu
  2016-11-07 14:03         ` Laszlo Ersek
  0 siblings, 1 reply; 13+ messages in thread
From: Ni, Ruiyu @ 2016-11-07  2:32 UTC (permalink / raw)
  To: Laszlo Ersek, Gao, Liming, edk2-devel@lists.01.org



Thanks/Ray

> -----Original Message-----
> From: Laszlo Ersek [mailto:lersek@redhat.com]
> Sent: Saturday, November 5, 2016 12:48 AM
> To: Ni, Ruiyu <ruiyu.ni@intel.com>; Gao, Liming <liming.gao@intel.com>;
> edk2-devel@lists.01.org <edk2-devel@ml01.01.org>
> Subject: Re: [edk2] [PATCH 0/4] Defer 3rd party images loading to after
> EndOfDxe
> 
> On 11/04/16 07:09, Ni, Ruiyu wrote:
> > No.
> > The open source platform patch will be sent out later.
> 
> What are the deferred / 3rd party images? Do Driver#### and SysPrep####
> qualify?

The images which are not from FV are treated as 3rd party images. And they will be
deferred to dispatch when they are dispatched before EndOfDxe event.
It's a new feature in the BS.LoadImage() path which can disallow executing
3rd party images before EndOfDxe and re-execute them after EndOfDxe.


> 
> Or, is this related to value 3 ("Defer execution when there is security
> violation") of:
> - PcdOptionRomImageVerificationPolicy,
> - PcdRemovableMediaImageVerificationPolicy,
> - PcdFixedMediaImageVerificationPolicy?

No.

> 
> Is the deferral documented somewhere in the UEFI spec, or do we have a
> Mantis ticket / ECR about it?
No.

> 
> Can we improve the commit messages please? From them, I have no idea
> how the deferral is supposed to work. (I.e., what the agents are, and how
> they interact.)
Yes. I will embed my first paragraph of reply in the commit message.

> 
> Thanks
> Laszlo
> 
> > From: Gao, Liming
> > Sent: Friday, November 4, 2016 1:14 PM
> > To: Ni, Ruiyu <ruiyu.ni@intel.com>; edk2-devel@lists.01.org
> > Subject: RE: [edk2] [PATCH 0/4] Defer 3rd party images loading to
> > after EndOfDxe
> >
> > Ray:
> >   Seemly, PlatformBdsLib library instance should call
> EfiBootManagerDispatchDeferredImages(), right? Are there patches to
> update PlatformBdsLib library instance?
> >
> > Thanks
> > Liming
> >> -----Original Message-----
> >> From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf
> >> Of Ruiyu Ni
> >> Sent: Friday, November 04, 2016 9:00 AM
> >> To: edk2-devel@lists.01.org<mailto:edk2-devel@lists.01.org>
> >> Subject: [edk2] [PATCH 0/4] Defer 3rd party images loading to after
> >> EndOfDxe
> >>
> >> The patches change the default image loading policy by deferring 3rd
> >> party images loading to after EndOfDxe and add a new BDS API to
> >> dispatch the deferred images.
> >>
> >> Platform needs to call the new BDS API
> >> EfiBootManagerDispatchDeferredImages after EndOfDxe to ensure that
> >> any deferred images are loaded.
> >>
> >> Ruiyu Ni (4):
> >>   MdeModulePkg/SecurityStubDxe: Defer 3rd party image before
> EndOfDxe
> >>   MdeModulePkg/UefiBootManager: Add
> >> EfiBootManagerDispatchDeferredImages
> >>   MdeModulePkg/BdsDxe: Check deferred images before booting to OS
> >>   MdeModulePkg/SecurityStubDxe: Report failure if image is load
> >> earlier
> >>
> >>  MdeModulePkg/Include/Library/UefiBootManagerLib.h  |  13 +
> >>  MdeModulePkg/Library/UefiBootManagerLib/BmMisc.c   | 113 ++++++
> >>  .../Library/UefiBootManagerLib/InternalBm.h        |   1 +
> >>  .../UefiBootManagerLib/UefiBootManagerLib.inf      |   1 +
> >>  MdeModulePkg/Universal/BdsDxe/Bds.h                |   4 +-
> >>  MdeModulePkg/Universal/BdsDxe/BdsDxe.inf           |   2 +
> >>  MdeModulePkg/Universal/BdsDxe/BdsEntry.c           |  89 +++++
> >>  .../SecurityStubDxe/Defer3rdPartyImageLoad.c       | 413
> >> +++++++++++++++++++++
> >>  .../SecurityStubDxe/Defer3rdPartyImageLoad.h       |  95 +++++
> >>  .../Universal/SecurityStubDxe/SecurityStub.c       |  14 +-
> >>  .../Universal/SecurityStubDxe/SecurityStubDxe.inf  |  11 +-
> >>  11 files changed, 753 insertions(+), 3 deletions(-)  create mode
> >> 100644
> >> MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.c
> >>  create mode 100644
> >> MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.h
> >>
> >> --
> >> 2.9.0.windows.1
> >>
> >> _______________________________________________
> >> edk2-devel mailing list
> >> edk2-devel@lists.01.org<mailto:edk2-devel@lists.01.org>
> >> https://lists.01.org/mailman/listinfo/edk2-devel
> > _______________________________________________
> > edk2-devel mailing list
> > edk2-devel@lists.01.org
> > https://lists.01.org/mailman/listinfo/edk2-devel
> >



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 0/4] Defer 3rd party images loading to after EndOfDxe
  2016-11-07  2:32       ` Ni, Ruiyu
@ 2016-11-07 14:03         ` Laszlo Ersek
  0 siblings, 0 replies; 13+ messages in thread
From: Laszlo Ersek @ 2016-11-07 14:03 UTC (permalink / raw)
  To: Ni, Ruiyu
  Cc: Gao, Liming, edk2-devel@lists.01.org, Ard Biesheuvel,
	Jordan Justen (Intel address)

On 11/07/16 03:32, Ni, Ruiyu wrote:
> 
> 
> Thanks/Ray
> 
>> -----Original Message-----
>> From: Laszlo Ersek [mailto:lersek@redhat.com]
>> Sent: Saturday, November 5, 2016 12:48 AM
>> To: Ni, Ruiyu <ruiyu.ni@intel.com>; Gao, Liming <liming.gao@intel.com>;
>> edk2-devel@lists.01.org <edk2-devel@ml01.01.org>
>> Subject: Re: [edk2] [PATCH 0/4] Defer 3rd party images loading to after
>> EndOfDxe
>>
>> On 11/04/16 07:09, Ni, Ruiyu wrote:
>>> No.
>>> The open source platform patch will be sent out later.
>>
>> What are the deferred / 3rd party images? Do Driver#### and SysPrep####
>> qualify?
> 
> The images which are not from FV are treated as 3rd party images. And they will be
> deferred to dispatch when they are dispatched before EndOfDxe event.
> It's a new feature in the BS.LoadImage() path which can disallow executing
> 3rd party images before EndOfDxe and re-execute them after EndOfDxe.

Thank you for the explanation.

OvmfPkg's and ArmVirtPkg's PlatformBootManagerLib instances signal
EndOfDxe in BeforeConsole().

BdsDxe executes the Driver#### options between BeforeConsole() and
AfterConsole(), so the new deferral feature should not incur observable
changes for OvmfPkg's and ArmVirtPkg's Driver#### handling -- by the
time a Driver is loaded from (say) the ESP, EndOfDxe will have been
signaled anyway.

Also, TryRunningQemuKernel() is called on both platforms from
AfterConsole(). This is esp. relevant for ArmVirtPkg, which uses
LoadImage() to load the kernel (from a synthetic file system). Given
that this again occurs after EndOfDxe (which is signaled in
BeforeConsole), it should continue working fine.

You mentioned that a new PlatformBootManagerLib API --
EfiBootManagerDispatchDeferredImages() -- will be introduced for running
the deferred images after EndOfDxe, and that the Platform BDS code will
be responsible for calling that API. For ArmVirtPkg and OvmfPkg, this
API seems to be a no-op at the moment, but I agree that for completeness
we should call them. (Although, the fallback added in patch #3 would
also handle them.)

I propose that in ArmVirtPkg, we call the new API right after signaling
EndOfDxe.

In OvmfPkg, we should call the new API after installing
gEfiDxeSmmReadyToLockProtocolGuid. (The relevance of
gEfiDxeSmmReadyToLockProtocolGuid is discussed in your patch #4 as well.)

I'll comment on patch #4 separately too.

Thank you,
Laszlo


>> Or, is this related to value 3 ("Defer execution when there is security
>> violation") of:
>> - PcdOptionRomImageVerificationPolicy,
>> - PcdRemovableMediaImageVerificationPolicy,
>> - PcdFixedMediaImageVerificationPolicy?
> 
> No.
> 
>>
>> Is the deferral documented somewhere in the UEFI spec, or do we have a
>> Mantis ticket / ECR about it?
> No.
> 
>>
>> Can we improve the commit messages please? From them, I have no idea
>> how the deferral is supposed to work. (I.e., what the agents are, and how
>> they interact.)
> Yes. I will embed my first paragraph of reply in the commit message.
> 
>>
>> Thanks
>> Laszlo
>>
>>> From: Gao, Liming
>>> Sent: Friday, November 4, 2016 1:14 PM
>>> To: Ni, Ruiyu <ruiyu.ni@intel.com>; edk2-devel@lists.01.org
>>> Subject: RE: [edk2] [PATCH 0/4] Defer 3rd party images loading to
>>> after EndOfDxe
>>>
>>> Ray:
>>>   Seemly, PlatformBdsLib library instance should call
>> EfiBootManagerDispatchDeferredImages(), right? Are there patches to
>> update PlatformBdsLib library instance?
>>>
>>> Thanks
>>> Liming
>>>> -----Original Message-----
>>>> From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf
>>>> Of Ruiyu Ni
>>>> Sent: Friday, November 04, 2016 9:00 AM
>>>> To: edk2-devel@lists.01.org<mailto:edk2-devel@lists.01.org>
>>>> Subject: [edk2] [PATCH 0/4] Defer 3rd party images loading to after
>>>> EndOfDxe
>>>>
>>>> The patches change the default image loading policy by deferring 3rd
>>>> party images loading to after EndOfDxe and add a new BDS API to
>>>> dispatch the deferred images.
>>>>
>>>> Platform needs to call the new BDS API
>>>> EfiBootManagerDispatchDeferredImages after EndOfDxe to ensure that
>>>> any deferred images are loaded.
>>>>
>>>> Ruiyu Ni (4):
>>>>   MdeModulePkg/SecurityStubDxe: Defer 3rd party image before
>> EndOfDxe
>>>>   MdeModulePkg/UefiBootManager: Add
>>>> EfiBootManagerDispatchDeferredImages
>>>>   MdeModulePkg/BdsDxe: Check deferred images before booting to OS
>>>>   MdeModulePkg/SecurityStubDxe: Report failure if image is load
>>>> earlier
>>>>
>>>>  MdeModulePkg/Include/Library/UefiBootManagerLib.h  |  13 +
>>>>  MdeModulePkg/Library/UefiBootManagerLib/BmMisc.c   | 113 ++++++
>>>>  .../Library/UefiBootManagerLib/InternalBm.h        |   1 +
>>>>  .../UefiBootManagerLib/UefiBootManagerLib.inf      |   1 +
>>>>  MdeModulePkg/Universal/BdsDxe/Bds.h                |   4 +-
>>>>  MdeModulePkg/Universal/BdsDxe/BdsDxe.inf           |   2 +
>>>>  MdeModulePkg/Universal/BdsDxe/BdsEntry.c           |  89 +++++
>>>>  .../SecurityStubDxe/Defer3rdPartyImageLoad.c       | 413
>>>> +++++++++++++++++++++
>>>>  .../SecurityStubDxe/Defer3rdPartyImageLoad.h       |  95 +++++
>>>>  .../Universal/SecurityStubDxe/SecurityStub.c       |  14 +-
>>>>  .../Universal/SecurityStubDxe/SecurityStubDxe.inf  |  11 +-
>>>>  11 files changed, 753 insertions(+), 3 deletions(-)  create mode
>>>> 100644
>>>> MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.c
>>>>  create mode 100644
>>>> MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.h
>>>>
>>>> --
>>>> 2.9.0.windows.1
>>>>
>>>> _______________________________________________
>>>> edk2-devel mailing list
>>>> edk2-devel@lists.01.org<mailto:edk2-devel@lists.01.org>
>>>> https://lists.01.org/mailman/listinfo/edk2-devel
>>> _______________________________________________
>>> edk2-devel mailing list
>>> edk2-devel@lists.01.org
>>> https://lists.01.org/mailman/listinfo/edk2-devel
>>>
> 



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 4/4] MdeModulePkg/SecurityStubDxe: Report failure if image is load earlier
  2016-11-04  0:59 ` [PATCH 4/4] MdeModulePkg/SecurityStubDxe: Report failure if image is load earlier Ruiyu Ni
@ 2016-11-07 14:07   ` Laszlo Ersek
  2016-11-10  7:27   ` Yao, Jiewen
  1 sibling, 0 replies; 13+ messages in thread
From: Laszlo Ersek @ 2016-11-07 14:07 UTC (permalink / raw)
  To: Ruiyu Ni; +Cc: edk2-devel, Jiewen Yao

On 11/04/16 01:59, Ruiyu Ni wrote:
> The 3rd party image should be loaded after EndOfDxe event signal and
> DxeSmmReadyToLock protocol installation. But non-SMM platform doesn't
> published DxeSmmReadyToLock protocol.
> So the SecurityStubDxe can only depend on EndOfDxe event.
> 
> This patch enhances the SecurityStubDxe to listen on
> DxeSmmReadyToLock protocol installation and if any 3rd party image
> is loaded before DxeSmmReadyToLock, it reports failure.
> 
> Contributed-under: TianoCore Contribution Agreement 1.0
> Signed-off-by: Ruiyu Ni <ruiyu.ni@intel.com>
> Cc: Jiewen Yao <jiewen.yao@intel.com>
> ---
>  .../SecurityStubDxe/Defer3rdPartyImageLoad.c       | 57 ++++++++++++++++++++++
>  .../SecurityStubDxe/Defer3rdPartyImageLoad.h       |  5 +-
>  .../Universal/SecurityStubDxe/SecurityStubDxe.inf  |  3 ++
>  3 files changed, 64 insertions(+), 1 deletion(-)
> 
> diff --git a/MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.c b/MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.c
> index ca45d56..84d573b 100644
> --- a/MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.c
> +++ b/MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.c
> @@ -30,6 +30,7 @@ typedef struct {
>    DEFERRED_3RD_PARTY_IMAGE_INFO     *ImageInfo;    ///< deferred 3rd party image item
>  } DEFERRED_3RD_PARTY_IMAGE_TABLE;
>  
> +BOOLEAN                          mImageLoadedAfterEndOfDxe   = FALSE;
>  BOOLEAN                          mEndOfDxe                   = FALSE;
>  DEFERRED_3RD_PARTY_IMAGE_TABLE   mDeferred3rdPartyImage = {
>    0,       // Deferred image count
> @@ -257,6 +258,52 @@ EndOfDxe (
>  }
>  
>  /**
> +  Event notification for gEfiDxeSmmReadyToLockProtocolGuid event.
> +
> +  This function reports failure if any deferred image is loaded before
> +  this callback.
> +  Platform should publish ReadyToLock protocol immediately after signaling
> +  of the End of DXE Event.
> +
> +  @param  Event                 The Event that is being processed, not used.
> +  @param  Context               Event Context, not used.
> +
> +**/
> +VOID
> +EFIAPI
> +DxdSmmReadyToLock (

"Dxd" is a typo, it should be "Dxe".

> +  IN EFI_EVENT  Event,
> +  IN VOID       *Context
> +  )
> +{
> +  EFI_STATUS                Status;
> +  VOID                      *Interface;
> +
> +  Status = gBS->LocateProtocol (&gEfiDxeSmmReadyToLockProtocolGuid, NULL, &Interface);
> +  if (EFI_ERROR (Status)) {
> +    return;
> +  }
> +
> +  gBS->CloseEvent (Event);
> +
> +  if (mImageLoadedAfterEndOfDxe) {
> +    //
> +    // Platform should not dispatch the 3rd party images after signaling EndOfDxe event
> +    // but before publishing DxeSmmReadyToLock protocol.
> +    //
> +    DEBUG ((
> +      DEBUG_ERROR,
> +      "[Security] 3rd party images must be dispatched after DxeSmmReadyToLock Protocol installation!\n"
> +      ));
> +    REPORT_STATUS_CODE (
> +      EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED,
> +      (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_EC_ILLEGAL_SOFTWARE_STATE)
> +      );
> +    ASSERT (FALSE);

I suggest to add an explicit CpuDeadLoop() call here, after the
ASSERT(FALSE). I learned this pattern from Mike:

https://www.mail-archive.com/edk2-devel@lists.01.org/msg04590.html

Thanks!
Laszlo

> +  }
> +}
> +
> +/**
>    Defer the 3rd party image load and installs Deferred Image Load Protocol.
>  
>    @param[in]  File                  This is a pointer to the device path of the file that
> @@ -303,6 +350,7 @@ Defer3rdPartyImageLoad (
>      );
>  
>    if (mEndOfDxe) {
> +    mImageLoadedAfterEndOfDxe = TRUE;
>      //
>      // The image might be first time loaded after EndOfDxe,
>      // So ImageInfo can be NULL.
> @@ -334,6 +382,7 @@ Defer3rdPartyImageLoadInitialize (
>    EFI_STATUS                           Status;
>    EFI_HANDLE                           Handle;
>    EFI_EVENT                            Event;
> +  VOID                                 *Registration;
>  
>    Handle = NULL;
>    Status = gBS->InstallMultipleProtocolInterfaces (
> @@ -353,4 +402,12 @@ Defer3rdPartyImageLoadInitialize (
>                    &Event
>                    );
>    ASSERT_EFI_ERROR (Status);
> +
> +  EfiCreateProtocolNotifyEvent (
> +    &gEfiDxeSmmReadyToLockProtocolGuid,
> +    TPL_CALLBACK,
> +    DxdSmmReadyToLock,
> +    NULL,
> +    &Registration
> +    );
>  }
> diff --git a/MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.h b/MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.h
> index 3fab258..75553ba 100644
> --- a/MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.h
> +++ b/MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.h
> @@ -15,16 +15,19 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
>  #ifndef _DEFER_3RD_PARTY_IMAGE_LOAD_H_
>  #define _DEFER_3RD_PARTY_IMAGE_LOAD_H_
>  
> -#include <Uefi.h>
> +#include <PiDxe.h>
>  #include <Guid/EventGroup.h>
>  #include <Protocol/DeferredImageLoad.h>
>  #include <Protocol/FirmwareVolume2.h>
> +#include <Protocol/DxeSmmReadyToLock.h>
>  
>  #include <Library/UefiBootServicesTableLib.h>
>  #include <Library/BaseMemoryLib.h>
>  #include <Library/MemoryAllocationLib.h>
>  #include <Library/DevicePathLib.h>
>  #include <Library/DebugLib.h>
> +#include <Library/UefiLib.h>
> +#include <Library/ReportStatusCodeLib.h>
>  
>  /**
>    Returns information about a deferred image.
> diff --git a/MdeModulePkg/Universal/SecurityStubDxe/SecurityStubDxe.inf b/MdeModulePkg/Universal/SecurityStubDxe/SecurityStubDxe.inf
> index be6ce6c..7f8f6cb 100644
> --- a/MdeModulePkg/Universal/SecurityStubDxe/SecurityStubDxe.inf
> +++ b/MdeModulePkg/Universal/SecurityStubDxe/SecurityStubDxe.inf
> @@ -41,6 +41,8 @@ [LibraryClasses]
>    UefiBootServicesTableLib
>    DebugLib
>    SecurityManagementLib
> +  ReportStatusCodeLib
> +  UefiLib
>  
>  [Guids]
>    gEfiEndOfDxeEventGroupGuid                    ## CONSUMES ## Event
> @@ -49,6 +51,7 @@ [Protocols]
>    gEfiSecurityArchProtocolGuid                  ## PRODUCES
>    gEfiSecurity2ArchProtocolGuid                 ## PRODUCES
>    gEfiDeferredImageLoadProtocolGuid             ## PRODUCES
> +  gEfiDxeSmmReadyToLockProtocolGuid             ## CONSUMES
>  
>  [Depex]
>    TRUE
> 



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 4/4] MdeModulePkg/SecurityStubDxe: Report failure if image is load earlier
  2016-11-04  0:59 ` [PATCH 4/4] MdeModulePkg/SecurityStubDxe: Report failure if image is load earlier Ruiyu Ni
  2016-11-07 14:07   ` Laszlo Ersek
@ 2016-11-10  7:27   ` Yao, Jiewen
  1 sibling, 0 replies; 13+ messages in thread
From: Yao, Jiewen @ 2016-11-10  7:27 UTC (permalink / raw)
  To: Ni, Ruiyu, edk2-devel@lists.01.org

Reviewed-by: Jiewen.yao@intel.com

> -----Original Message-----
> From: Ni, Ruiyu
> Sent: Friday, November 4, 2016 9:00 AM
> To: edk2-devel@lists.01.org
> Cc: Yao, Jiewen <jiewen.yao@intel.com>
> Subject: [PATCH 4/4] MdeModulePkg/SecurityStubDxe: Report failure if
> image is load earlier
> 
> The 3rd party image should be loaded after EndOfDxe event signal and
> DxeSmmReadyToLock protocol installation. But non-SMM platform doesn't
> published DxeSmmReadyToLock protocol.
> So the SecurityStubDxe can only depend on EndOfDxe event.
> 
> This patch enhances the SecurityStubDxe to listen on
> DxeSmmReadyToLock protocol installation and if any 3rd party image
> is loaded before DxeSmmReadyToLock, it reports failure.
> 
> Contributed-under: TianoCore Contribution Agreement 1.0
> Signed-off-by: Ruiyu Ni <ruiyu.ni@intel.com>
> Cc: Jiewen Yao <jiewen.yao@intel.com>
> ---
>  .../SecurityStubDxe/Defer3rdPartyImageLoad.c       | 57
> ++++++++++++++++++++++
>  .../SecurityStubDxe/Defer3rdPartyImageLoad.h       |  5 +-
>  .../Universal/SecurityStubDxe/SecurityStubDxe.inf  |  3 ++
>  3 files changed, 64 insertions(+), 1 deletion(-)
> 
> diff --git
> a/MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.c
> b/MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.c
> index ca45d56..84d573b 100644
> --- a/MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.c
> +++
> b/MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.c
> @@ -30,6 +30,7 @@ typedef struct {
>    DEFERRED_3RD_PARTY_IMAGE_INFO     *ImageInfo;    ///<
> deferred 3rd party image item
>  } DEFERRED_3RD_PARTY_IMAGE_TABLE;
> 
> +BOOLEAN                          mImageLoadedAfterEndOfDxe
> = FALSE;
>  BOOLEAN                          mEndOfDxe
> = FALSE;
>  DEFERRED_3RD_PARTY_IMAGE_TABLE   mDeferred3rdPartyImage = {
>    0,       // Deferred image count
> @@ -257,6 +258,52 @@ EndOfDxe (
>  }
> 
>  /**
> +  Event notification for gEfiDxeSmmReadyToLockProtocolGuid event.
> +
> +  This function reports failure if any deferred image is loaded before
> +  this callback.
> +  Platform should publish ReadyToLock protocol immediately after
> signaling
> +  of the End of DXE Event.
> +
> +  @param  Event                 The Event that is being processed,
> not used.
> +  @param  Context               Event Context, not used.
> +
> +**/
> +VOID
> +EFIAPI
> +DxdSmmReadyToLock (
> +  IN EFI_EVENT  Event,
> +  IN VOID       *Context
> +  )
> +{
> +  EFI_STATUS                Status;
> +  VOID                      *Interface;
> +
> +  Status = gBS->LocateProtocol (&gEfiDxeSmmReadyToLockProtocolGuid,
> NULL, &Interface);
> +  if (EFI_ERROR (Status)) {
> +    return;
> +  }
> +
> +  gBS->CloseEvent (Event);
> +
> +  if (mImageLoadedAfterEndOfDxe) {
> +    //
> +    // Platform should not dispatch the 3rd party images after signaling
> EndOfDxe event
> +    // but before publishing DxeSmmReadyToLock protocol.
> +    //
> +    DEBUG ((
> +      DEBUG_ERROR,
> +      "[Security] 3rd party images must be dispatched after
> DxeSmmReadyToLock Protocol installation!\n"
> +      ));
> +    REPORT_STATUS_CODE (
> +      EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED,
> +      (EFI_SOFTWARE_DXE_BS_DRIVER |
> EFI_SW_EC_ILLEGAL_SOFTWARE_STATE)
> +      );
> +    ASSERT (FALSE);
> +  }
> +}
> +
> +/**
>    Defer the 3rd party image load and installs Deferred Image Load
> Protocol.
> 
>    @param[in]  File                  This is a pointer to the device
> path of the file that
> @@ -303,6 +350,7 @@ Defer3rdPartyImageLoad (
>      );
> 
>    if (mEndOfDxe) {
> +    mImageLoadedAfterEndOfDxe = TRUE;
>      //
>      // The image might be first time loaded after EndOfDxe,
>      // So ImageInfo can be NULL.
> @@ -334,6 +382,7 @@ Defer3rdPartyImageLoadInitialize (
>    EFI_STATUS                           Status;
>    EFI_HANDLE                           Handle;
>    EFI_EVENT                            Event;
> +  VOID                                 *Registration;
> 
>    Handle = NULL;
>    Status = gBS->InstallMultipleProtocolInterfaces (
> @@ -353,4 +402,12 @@ Defer3rdPartyImageLoadInitialize (
>                    &Event
>                    );
>    ASSERT_EFI_ERROR (Status);
> +
> +  EfiCreateProtocolNotifyEvent (
> +    &gEfiDxeSmmReadyToLockProtocolGuid,
> +    TPL_CALLBACK,
> +    DxdSmmReadyToLock,
> +    NULL,
> +    &Registration
> +    );
>  }
> diff --git
> a/MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.h
> b/MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.h
> index 3fab258..75553ba 100644
> --- a/MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.h
> +++
> b/MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.h
> @@ -15,16 +15,19 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF
> ANY KIND, EITHER EXPRESS OR IMPLIED.
>  #ifndef _DEFER_3RD_PARTY_IMAGE_LOAD_H_
>  #define _DEFER_3RD_PARTY_IMAGE_LOAD_H_
> 
> -#include <Uefi.h>
> +#include <PiDxe.h>
>  #include <Guid/EventGroup.h>
>  #include <Protocol/DeferredImageLoad.h>
>  #include <Protocol/FirmwareVolume2.h>
> +#include <Protocol/DxeSmmReadyToLock.h>
> 
>  #include <Library/UefiBootServicesTableLib.h>
>  #include <Library/BaseMemoryLib.h>
>  #include <Library/MemoryAllocationLib.h>
>  #include <Library/DevicePathLib.h>
>  #include <Library/DebugLib.h>
> +#include <Library/UefiLib.h>
> +#include <Library/ReportStatusCodeLib.h>
> 
>  /**
>    Returns information about a deferred image.
> diff --git a/MdeModulePkg/Universal/SecurityStubDxe/SecurityStubDxe.inf
> b/MdeModulePkg/Universal/SecurityStubDxe/SecurityStubDxe.inf
> index be6ce6c..7f8f6cb 100644
> --- a/MdeModulePkg/Universal/SecurityStubDxe/SecurityStubDxe.inf
> +++ b/MdeModulePkg/Universal/SecurityStubDxe/SecurityStubDxe.inf
> @@ -41,6 +41,8 @@ [LibraryClasses]
>    UefiBootServicesTableLib
>    DebugLib
>    SecurityManagementLib
> +  ReportStatusCodeLib
> +  UefiLib
> 
>  [Guids]
>    gEfiEndOfDxeEventGroupGuid                    ## CONSUMES ##
> Event
> @@ -49,6 +51,7 @@ [Protocols]
>    gEfiSecurityArchProtocolGuid                  ## PRODUCES
>    gEfiSecurity2ArchProtocolGuid                 ## PRODUCES
>    gEfiDeferredImageLoadProtocolGuid             ## PRODUCES
> +  gEfiDxeSmmReadyToLockProtocolGuid             ## CONSUMES
> 
>  [Depex]
>    TRUE
> --
> 2.9.0.windows.1



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 1/4] MdeModulePkg/SecurityStubDxe: Defer 3rd party image before EndOfDxe
  2016-11-04  0:59 ` [PATCH 1/4] MdeModulePkg/SecurityStubDxe: Defer 3rd party image before EndOfDxe Ruiyu Ni
@ 2016-11-10  7:42   ` Zhang, Chao B
  0 siblings, 0 replies; 13+ messages in thread
From: Zhang, Chao B @ 2016-11-10  7:42 UTC (permalink / raw)
  To: Ni, Ruiyu, edk2-devel@lists.01.org; +Cc: Gao, Liming, Yao, Jiewen

Reviewed-by : Chao Zhang <chao.b.zhang@intel.com>





Thanks & Best regards
Chao Zhang


-----Original Message-----
From: Ni, Ruiyu 
Sent: Friday, November 04, 2016 9:00 AM
To: edk2-devel@lists.01.org
Cc: Gao, Liming; Yao, Jiewen; Zhang, Chao B
Subject: [PATCH 1/4] MdeModulePkg/SecurityStubDxe: Defer 3rd party image before EndOfDxe

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Ruiyu Ni <ruiyu.ni@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Chao B Zhang <chao.b.zhang@intel.com>
---
 .../SecurityStubDxe/Defer3rdPartyImageLoad.c       | 356 +++++++++++++++++++++
 .../SecurityStubDxe/Defer3rdPartyImageLoad.h       |  92 ++++++
 .../Universal/SecurityStubDxe/SecurityStub.c       |  14 +-
 .../Universal/SecurityStubDxe/SecurityStubDxe.inf  |   8 +-
 4 files changed, 468 insertions(+), 2 deletions(-)
 create mode 100644 MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.c
 create mode 100644 MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.h

diff --git a/MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.c b/MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.c
new file mode 100644
index 0000000..ca45d56
--- /dev/null
+++ b/MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.c
@@ -0,0 +1,356 @@
+/** @file
+  Implement defer image load services for user identification in UEFI2.2.
+
+Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
+This program and the accompanying materials
+are licensed and made available under the terms and conditions of the BSD License
+which accompanies this distribution.  The full text of the license may be found at
+http://opensource.org/licenses/bsd-license.php
+
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+#include "Defer3rdPartyImageLoad.h"
+
+//
+// The structure to save the deferred 3rd party image information.
+//
+typedef struct {
+  EFI_DEVICE_PATH_PROTOCOL          *ImageDevicePath;
+  BOOLEAN                           BootOption;
+  BOOLEAN                           Loaded;
+} DEFERRED_3RD_PARTY_IMAGE_INFO;
+
+//
+// The table to save the deferred 3rd party image item.
+//
+typedef struct {
+  UINTN                             Count;         ///< deferred 3rd party image count
+  DEFERRED_3RD_PARTY_IMAGE_INFO     *ImageInfo;    ///< deferred 3rd party image item
+} DEFERRED_3RD_PARTY_IMAGE_TABLE;
+
+BOOLEAN                          mEndOfDxe                   = FALSE;
+DEFERRED_3RD_PARTY_IMAGE_TABLE   mDeferred3rdPartyImage = {
+  0,       // Deferred image count
+  NULL     // The deferred image info
+};
+
+EFI_DEFERRED_IMAGE_LOAD_PROTOCOL mDeferredImageLoad   = {
+  GetDefferedImageInfo
+};
+
+/**
+  Return whether the file comes from FV.
+
+  @param[in]    File    This is a pointer to the device path of the file
+                        that is being dispatched.
+
+  @retval TRUE  File comes from FV.
+  @retval FALSE File doesn't come from FV.
+**/
+BOOLEAN
+FileFromFv (
+  IN  CONST EFI_DEVICE_PATH_PROTOCOL   *File
+  )
+{
+  EFI_STATUS                        Status;
+  EFI_HANDLE                        DeviceHandle;
+  EFI_DEVICE_PATH_PROTOCOL          *TempDevicePath;
+
+  //
+  // First check to see if File is from a Firmware Volume
+  //
+  DeviceHandle   = NULL;
+  TempDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) File;
+  Status = gBS->LocateDevicePath (
+                  &gEfiFirmwareVolume2ProtocolGuid,
+                  &TempDevicePath,
+                  &DeviceHandle
+                  );
+  if (!EFI_ERROR (Status)) {
+    Status = gBS->OpenProtocol (
+                    DeviceHandle,
+                    &gEfiFirmwareVolume2ProtocolGuid,
+                    NULL,
+                    NULL,
+                    NULL,
+                    EFI_OPEN_PROTOCOL_TEST_PROTOCOL
+                    );
+    if (!EFI_ERROR (Status)) {
+      return TRUE;
+    }
+  }
+
+  return FALSE;
+}
+
+/**
+  Find the deferred image which matches the device path.
+
+  @param[in]  ImageDevicePath  A pointer to the device path of a image.
+  @param[in]  BootOption       Whether the image is a boot option.
+
+  @return Pointer to the found deferred image or NULL if not found.
+**/
+DEFERRED_3RD_PARTY_IMAGE_INFO *
+LookupImage (
+  IN  CONST EFI_DEVICE_PATH_PROTOCOL    *ImageDevicePath,
+  IN        BOOLEAN                     BootOption
+  )
+{
+  UINTN                                 Index;
+  UINTN                                 DevicePathSize;
+
+  DevicePathSize = GetDevicePathSize (ImageDevicePath);
+
+  for (Index = 0; Index < mDeferred3rdPartyImage.Count; Index++) {
+    if (CompareMem (ImageDevicePath, mDeferred3rdPartyImage.ImageInfo[Index].ImageDevicePath, DevicePathSize) == 0) {
+      ASSERT (mDeferred3rdPartyImage.ImageInfo[Index].BootOption == BootOption);
+      return &mDeferred3rdPartyImage.ImageInfo[Index];
+    }
+  }
+
+  return NULL;
+}
+
+/**
+  Add the image info to a deferred image list.
+
+  @param[in]  ImageDevicePath  A pointer to the device path of a image.
+  @param[in]  BootOption       Whether the image is a boot option.
+
+**/
+VOID
+QueueImage (
+  IN  CONST EFI_DEVICE_PATH_PROTOCOL    *ImageDevicePath,
+  IN        BOOLEAN                     BootOption
+  )
+{
+  DEFERRED_3RD_PARTY_IMAGE_INFO         *ImageInfo;
+
+  //
+  // Expand memory for the new deferred image.
+  //
+  ImageInfo = ReallocatePool (
+                mDeferred3rdPartyImage.Count * sizeof (DEFERRED_3RD_PARTY_IMAGE_INFO),
+                (mDeferred3rdPartyImage.Count + 1) * sizeof (DEFERRED_3RD_PARTY_IMAGE_INFO),
+                mDeferred3rdPartyImage.ImageInfo
+  );
+  if (ImageInfo == NULL) {
+    return;
+  }
+  mDeferred3rdPartyImage.ImageInfo = ImageInfo;
+
+  //
+  // Save the deferred image information.
+  //
+  ImageInfo = &mDeferred3rdPartyImage.ImageInfo[mDeferred3rdPartyImage.Count];
+  ImageInfo->ImageDevicePath = DuplicateDevicePath (ImageDevicePath);
+  if (ImageInfo->ImageDevicePath == NULL) {
+    return;
+  }
+  ImageInfo->BootOption = BootOption;
+  ImageInfo->Loaded     = FALSE;
+  mDeferred3rdPartyImage.Count++;
+}
+
+
+/**
+  Returns information about a deferred image.
+
+  This function returns information about a single deferred image. The deferred images are
+  numbered consecutively, starting with 0.  If there is no image which corresponds to
+  ImageIndex, then EFI_NOT_FOUND is returned. All deferred images may be returned by
+  iteratively calling this function until EFI_NOT_FOUND is returned.
+  Image may be NULL and ImageSize set to 0 if the decision to defer execution was made
+  because of the location of the executable image, rather than its actual contents.
+
+  @param[in]  This             Points to this instance of the EFI_DEFERRED_IMAGE_LOAD_PROTOCOL.
+  @param[in]  ImageIndex       Zero-based index of the deferred index.
+  @param[out] ImageDevicePath  On return, points to a pointer to the device path of the image.
+                               The device path should not be freed by the caller.
+  @param[out] Image            On return, points to the first byte of the image or NULL if the
+                               image is not available. The image should not be freed by the caller
+                               unless LoadImage() has been successfully called.
+  @param[out] ImageSize        On return, the size of the image, or 0 if the image is not available.
+  @param[out] BootOption       On return, points to TRUE if the image was intended as a boot option
+                               or FALSE if it was not intended as a boot option.
+
+  @retval EFI_SUCCESS           Image information returned successfully.
+  @retval EFI_NOT_FOUND         ImageIndex does not refer to a valid image.
+  @retval EFI_INVALID_PARAMETER ImageDevicePath is NULL or Image is NULL or ImageSize is NULL or
+                                BootOption is NULL.
+
+**/
+EFI_STATUS
+EFIAPI
+GetDefferedImageInfo (
+  IN     EFI_DEFERRED_IMAGE_LOAD_PROTOCOL  *This,
+  IN     UINTN                             ImageIndex,
+     OUT EFI_DEVICE_PATH_PROTOCOL          **ImageDevicePath,
+     OUT VOID                              **Image,
+     OUT UINTN                             *ImageSize,
+     OUT BOOLEAN                           *BootOption
+  )
+{
+  UINTN                                    Index;
+  UINTN                                    NewCount;
+
+  if ((This == NULL) || (ImageSize == NULL) || (Image == NULL)) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  if ((ImageDevicePath == NULL) || (BootOption == NULL)) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  //
+  // Remove the loaded images from the defer list in the first call.
+  //
+  if (ImageIndex == 0) {
+    NewCount = 0;
+    for (Index = 0; Index < mDeferred3rdPartyImage.Count; Index++) {
+      if (!mDeferred3rdPartyImage.ImageInfo[Index].Loaded) {
+        CopyMem (
+          &mDeferred3rdPartyImage.ImageInfo[NewCount],
+          &mDeferred3rdPartyImage.ImageInfo[Index],
+          sizeof (DEFERRED_3RD_PARTY_IMAGE_INFO)
+          );
+        NewCount++;
+      }
+    }
+
+    mDeferred3rdPartyImage.Count = NewCount;
+  }
+
+  if (ImageIndex >= mDeferred3rdPartyImage.Count) {
+    return EFI_NOT_FOUND;
+  }
+
+  //
+  // Get the request deferred image.
+  //
+  *ImageDevicePath = mDeferred3rdPartyImage.ImageInfo[ImageIndex].ImageDevicePath;
+  *BootOption      = mDeferred3rdPartyImage.ImageInfo[ImageIndex].BootOption;
+  *Image           = NULL;
+  *ImageSize       = 0;
+
+  return EFI_SUCCESS;
+}
+
+/**
+  Callback function executed when the EndOfDxe event group is signaled.
+
+  @param[in] Event      Event whose notification function is being invoked.
+  @param[in] Context    The pointer to the notification function's context, which
+                        is implementation-dependent.
+**/
+VOID
+EFIAPI
+EndOfDxe (
+  IN EFI_EVENT  Event,
+  IN VOID       *Context
+  )
+{
+  mEndOfDxe = TRUE;
+}
+
+/**
+  Defer the 3rd party image load and installs Deferred Image Load Protocol.
+
+  @param[in]  File                  This is a pointer to the device path of the file that
+                                    is being dispatched. This will optionally be used for
+                                    logging.
+  @param[in]  BootPolicy            A boot policy that was used to call LoadImage() UEFI service.
+
+  @retval EFI_SUCCESS               The file is not 3rd party image and can be loaded immediately.
+  @retval EFI_ACCESS_DENIED         The file is 3rd party image and needs deferred.
+**/
+EFI_STATUS
+Defer3rdPartyImageLoad (
+  IN  CONST EFI_DEVICE_PATH_PROTOCOL   *File,
+  IN  BOOLEAN                          BootPolicy
+  )
+{
+  DEFERRED_3RD_PARTY_IMAGE_INFO        *ImageInfo;
+
+  //
+  // Ignore if File is NULL.
+  //
+  if (File == NULL) {
+    return EFI_SUCCESS;
+  }
+
+  if (FileFromFv (File)) {
+    return EFI_SUCCESS;
+  }
+
+  ImageInfo = LookupImage (File, BootPolicy);
+
+  DEBUG_CODE (
+    CHAR16 *DevicePathStr;
+    DevicePathStr = ConvertDevicePathToText (File, FALSE, FALSE);
+    DEBUG ((
+      DEBUG_INFO,
+      "[Security] 3rd party image[%p] %s EndOfDxe: %s.\n", ImageInfo,
+      mEndOfDxe ? L"can be loaded after": L"is deferred to load before",
+      DevicePathStr
+      ));
+    if (DevicePathStr != NULL) {
+      FreePool (DevicePathStr);
+    }
+    );
+
+  if (mEndOfDxe) {
+    //
+    // The image might be first time loaded after EndOfDxe,
+    // So ImageInfo can be NULL.
+    //
+    if (ImageInfo != NULL) {
+      ImageInfo->Loaded = TRUE;
+    }
+    return EFI_SUCCESS;
+  } else {
+    //
+    // The image might be second time loaded before EndOfDxe,
+    // So ImageInfo can be non-NULL.
+    //
+    if (ImageInfo == NULL) {
+      QueueImage (File, BootPolicy);
+    }
+    return EFI_ACCESS_DENIED;
+  }
+}
+
+/**
+  Installs DeferredImageLoad Protocol and listens EndOfDxe event.
+**/
+VOID
+Defer3rdPartyImageLoadInitialize (
+  VOID
+  )
+{
+  EFI_STATUS                           Status;
+  EFI_HANDLE                           Handle;
+  EFI_EVENT                            Event;
+
+  Handle = NULL;
+  Status = gBS->InstallMultipleProtocolInterfaces (
+                  &Handle,
+                  &gEfiDeferredImageLoadProtocolGuid,
+                  &mDeferredImageLoad,
+                  NULL
+                  );
+  ASSERT_EFI_ERROR (Status);
+
+  Status = gBS->CreateEventEx (
+                  EVT_NOTIFY_SIGNAL,
+                  TPL_CALLBACK,
+                  EndOfDxe,
+                  NULL,
+                  &gEfiEndOfDxeEventGroupGuid,
+                  &Event
+                  );
+  ASSERT_EFI_ERROR (Status);
+}
diff --git a/MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.h b/MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.h
new file mode 100644
index 0000000..3fab258
--- /dev/null
+++ b/MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.h
@@ -0,0 +1,92 @@
+/** @file
+  Implement defer image load services for user identification in UEFI2.2.
+
+Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
+This program and the accompanying materials
+are licensed and made available under the terms and conditions of the BSD License
+which accompanies this distribution.  The full text of the license may be found at
+http://opensource.org/licenses/bsd-license.php
+
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#ifndef _DEFER_3RD_PARTY_IMAGE_LOAD_H_
+#define _DEFER_3RD_PARTY_IMAGE_LOAD_H_
+
+#include <Uefi.h>
+#include <Guid/EventGroup.h>
+#include <Protocol/DeferredImageLoad.h>
+#include <Protocol/FirmwareVolume2.h>
+
+#include <Library/UefiBootServicesTableLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/MemoryAllocationLib.h>
+#include <Library/DevicePathLib.h>
+#include <Library/DebugLib.h>
+
+/**
+  Returns information about a deferred image.
+
+  This function returns information about a single deferred image. The deferred images are
+  numbered consecutively, starting with 0.  If there is no image which corresponds to
+  ImageIndex, then EFI_NOT_FOUND is returned. All deferred images may be returned by
+  iteratively calling this function until EFI_NOT_FOUND is returned.
+  Image may be NULL and ImageSize set to 0 if the decision to defer execution was made
+  because of the location of the executable image, rather than its actual contents.
+
+  @param[in]  This             Points to this instance of the EFI_DEFERRED_IMAGE_LOAD_PROTOCOL.
+  @param[in]  ImageIndex       Zero-based index of the deferred index.
+  @param[out] ImageDevicePath  On return, points to a pointer to the device path of the image.
+                               The device path should not be freed by the caller.
+  @param[out] Image            On return, points to the first byte of the image or NULL if the
+                               image is not available. The image should not be freed by the caller
+                               unless LoadImage() has been successfully called.
+  @param[out] ImageSize        On return, the size of the image, or 0 if the image is not available.
+  @param[out] BootOption       On return, points to TRUE if the image was intended as a boot option
+                               or FALSE if it was not intended as a boot option.
+
+  @retval EFI_SUCCESS           Image information returned successfully.
+  @retval EFI_NOT_FOUND         ImageIndex does not refer to a valid image.
+  @retval EFI_INVALID_PARAMETER ImageDevicePath is NULL or Image is NULL or ImageSize is NULL or
+                                BootOption is NULL.
+
+**/
+EFI_STATUS
+EFIAPI
+GetDefferedImageInfo (
+  IN     EFI_DEFERRED_IMAGE_LOAD_PROTOCOL  *This,
+  IN     UINTN                             ImageIndex,
+     OUT EFI_DEVICE_PATH_PROTOCOL          **ImageDevicePath,
+     OUT VOID                              **Image,
+     OUT UINTN                             *ImageSize,
+     OUT BOOLEAN                           *BootOption
+  );
+
+/**
+  Defer the 3rd party image load and installs Deferred Image Load Protocol.
+
+  @param[in]  File                  This is a pointer to the device path of the file that
+                                    is being dispatched. This will optionally be used for
+                                    logging.
+  @param[in]  BootPolicy            A boot policy that was used to call LoadImage() UEFI service.
+
+  @retval EFI_SUCCESS               The file is not 3rd party image and can be loaded immediately.
+  @retval EFI_ACCESS_DENIED         The file is 3rd party image and needs deferred.
+**/
+EFI_STATUS
+Defer3rdPartyImageLoad (
+  IN  CONST EFI_DEVICE_PATH_PROTOCOL   *File,
+  IN  BOOLEAN                          BootPolicy
+  );
+
+/**
+  Installs DeferredImageLoad Protocol and listens EndOfDxe event.
+**/
+VOID
+Defer3rdPartyImageLoadInitialize (
+  VOID
+  );
+
+#endif
diff --git a/MdeModulePkg/Universal/SecurityStubDxe/SecurityStub.c b/MdeModulePkg/Universal/SecurityStubDxe/SecurityStub.c
index 22e6c7f..a75d8dc 100644
--- a/MdeModulePkg/Universal/SecurityStubDxe/SecurityStub.c
+++ b/MdeModulePkg/Universal/SecurityStubDxe/SecurityStub.c
@@ -1,7 +1,7 @@
 /** @file
   This driver produces Security2 and Security architectural protocol based on SecurityManagementLib.
  
-  Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
   This program and the accompanying materials                          
   are licensed and made available under the terms and conditions of the BSD License         
   which accompanies this distribution.  The full text of the license may be found at        
@@ -20,6 +20,7 @@
 #include <Library/UefiBootServicesTableLib.h>
 #include <Library/UefiDriverEntryPoint.h>
 #include <Library/SecurityManagementLib.h>
+#include "Defer3rdPartyImageLoad.h"
 
 //
 // Handle for the Security Architectural Protocol instance produced by this driver
@@ -140,6 +141,15 @@ Security2StubAuthenticate (
   IN BOOLEAN                           BootPolicy
   )
 {
+  EFI_STATUS                           Status;
+
+  if (FileBuffer != NULL) {
+    Status = Defer3rdPartyImageLoad (File, BootPolicy);
+    if (EFI_ERROR (Status)) {
+      return Status;
+    }
+  }
+
   return ExecuteSecurity2Handlers (EFI_AUTH_OPERATION_VERIFY_IMAGE | 
                                    EFI_AUTH_OPERATION_DEFER_IMAGE_LOAD | 
                                    EFI_AUTH_OPERATION_MEASURE_IMAGE |
@@ -200,5 +210,7 @@ SecurityStubInitialize (
                   );
   ASSERT_EFI_ERROR (Status);
 
+  Defer3rdPartyImageLoadInitialize ();
+
   return EFI_SUCCESS;
 }
diff --git a/MdeModulePkg/Universal/SecurityStubDxe/SecurityStubDxe.inf b/MdeModulePkg/Universal/SecurityStubDxe/SecurityStubDxe.inf
index 9d1c7b8..be6ce6c 100644
--- a/MdeModulePkg/Universal/SecurityStubDxe/SecurityStubDxe.inf
+++ b/MdeModulePkg/Universal/SecurityStubDxe/SecurityStubDxe.inf
@@ -1,7 +1,7 @@
 ## @file
 #  This driver produces security2 and security architectural protocol based on SecurityManagementLib.
 #
-#  Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
+#  Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
 #  This program and the accompanying materials
 #  are licensed and made available under the terms and conditions of the BSD License
 #  which accompanies this distribution. The full text of the license may be found at
@@ -29,6 +29,8 @@ [Defines]
 
 [Sources]
   SecurityStub.c
+  Defer3rdPartyImageLoad.c
+  Defer3rdPartyImageLoad.h
 
 [Packages]
   MdePkg/MdePkg.dec
@@ -40,9 +42,13 @@ [LibraryClasses]
   DebugLib
   SecurityManagementLib
 
+[Guids]
+  gEfiEndOfDxeEventGroupGuid                    ## CONSUMES ## Event
+
 [Protocols]
   gEfiSecurityArchProtocolGuid                  ## PRODUCES
   gEfiSecurity2ArchProtocolGuid                 ## PRODUCES
+  gEfiDeferredImageLoadProtocolGuid             ## PRODUCES
 
 [Depex]
   TRUE
-- 
2.9.0.windows.1



^ permalink raw reply related	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2016-11-10  7:42 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-11-04  0:59 [PATCH 0/4] Defer 3rd party images loading to after EndOfDxe Ruiyu Ni
2016-11-04  0:59 ` [PATCH 1/4] MdeModulePkg/SecurityStubDxe: Defer 3rd party image before EndOfDxe Ruiyu Ni
2016-11-10  7:42   ` Zhang, Chao B
2016-11-04  0:59 ` [PATCH 2/4] MdeModulePkg/UefiBootManager: Add EfiBootManagerDispatchDeferredImages Ruiyu Ni
2016-11-04  0:59 ` [PATCH 3/4] MdeModulePkg/BdsDxe: Check deferred images before booting to OS Ruiyu Ni
2016-11-04  0:59 ` [PATCH 4/4] MdeModulePkg/SecurityStubDxe: Report failure if image is load earlier Ruiyu Ni
2016-11-07 14:07   ` Laszlo Ersek
2016-11-10  7:27   ` Yao, Jiewen
2016-11-04  5:14 ` [PATCH 0/4] Defer 3rd party images loading to after EndOfDxe Gao, Liming
2016-11-04  6:09   ` Ni, Ruiyu
2016-11-04 16:48     ` Laszlo Ersek
2016-11-07  2:32       ` Ni, Ruiyu
2016-11-07 14:03         ` Laszlo Ersek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox