public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [patch v2 0/5] Unload image on EFI_SECURITY_VIOLATION
@ 2019-09-18  3:05 Dandan Bi
  2019-09-18  3:05 ` [patch v2 1/5] EmbeddedPkg: " Dandan Bi
                   ` (5 more replies)
  0 siblings, 6 replies; 15+ messages in thread
From: Dandan Bi @ 2019-09-18  3:05 UTC (permalink / raw)
  To: devel
  Cc: Leif Lindholm, Ard Biesheuvel, Jian J Wang, Hao A Wu, Ray Ni,
	Liming Gao, Zhichao Gao, Laszlo Ersek

REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1992

v2:
(1) Just separate the patch in MdeModulePkg into module level, the changes in EmbeddedPkg and ShellPkg are the same with V1.
(2) Drop the update in PciBusDxe module in MdeModulePkg since with EFI_SECURITY_VIOLATION returned, the image may be used later.


For the LoadImage() boot service, with EFI_SECURITY_VIOLATION retval,
the Image was loaded and an ImageHandle was created with a valid
EFI_LOADED_IMAGE_PROTOCOL, but the image can not be started right now.
This follows UEFI Spec.

But if the caller of LoadImage() doesn't have the option to defer
the execution of an image, we can not treat EFI_SECURITY_VIOLATION
like any other LoadImage() error, we should unload image for the
EFI_SECURITY_VIOLATION to avoid resource leak.

This patch is to do error handling for EFI_SECURITY_VIOLATION explicitly
for the callers in edk2 which don't have the policy to defer the
execution of the image.

Cc: Leif Lindholm <leif.lindholm@linaro.org>
Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Hao A Wu <hao.a.wu@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Zhichao Gao <zhichao.gao@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Dandan Bi (3):
  EmbeddedPkg: Unload image on EFI_SECURITY_VIOLATION
  MdeModulePkg/DxeCapsuleLibFmp: Unload image on EFI_SECURITY_VIOLATION
  MdeModulePkg/UefiBootManager: Unload image on EFI_SECURITY_VIOLATION
  MdeModulePkg/PlatformDriOverride: Unload image on
    EFI_SECURITY_VIOLATION
  ShellPkg: Unload image on EFI_SECURITY_VIOLATION

 .../AndroidFastboot/Arm/BootAndroidBootImg.c         |  9 +++++++++
 .../Library/AndroidBootImgLib/AndroidBootImgLib.c    | 12 ++++++++++++
 .../Library/DxeCapsuleLibFmp/DxeCapsuleLib.c         |  9 +++++++++
 MdeModulePkg/Library/UefiBootManagerLib/BmBoot.c     |  9 +++++++++
 .../Library/UefiBootManagerLib/BmLoadOption.c        | 11 ++++++++++-
 MdeModulePkg/Library/UefiBootManagerLib/BmMisc.c     | 11 ++++++++++-
 .../PlatformDriOverrideDxe/PlatDriOverrideLib.c      | 11 ++++++++++-
 ShellPkg/Application/Shell/ShellManParser.c          |  9 +++++++++
 .../Library/UefiShellDebug1CommandsLib/LoadPciRom.c  | 11 ++++++++++-
 ShellPkg/Library/UefiShellLevel2CommandsLib/Load.c   | 11 ++++++++++-
 10 files changed, 98 insertions(+), 5 deletions(-)

-- 
2.18.0.windows.1


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

* [patch v2 1/5] EmbeddedPkg: Unload image on EFI_SECURITY_VIOLATION
  2019-09-18  3:05 [patch v2 0/5] Unload image on EFI_SECURITY_VIOLATION Dandan Bi
@ 2019-09-18  3:05 ` Dandan Bi
  2019-09-24 10:27   ` [edk2-devel] " Philippe Mathieu-Daudé
  2019-09-18  3:05 ` [patch v2 2/5] MdeModulePkg/DxeCapsuleLibFmp: " Dandan Bi
                   ` (4 subsequent siblings)
  5 siblings, 1 reply; 15+ messages in thread
From: Dandan Bi @ 2019-09-18  3:05 UTC (permalink / raw)
  To: devel; +Cc: Leif Lindholm, Ard Biesheuvel, Laszlo Ersek

For the LoadImage() boot service, with EFI_SECURITY_VIOLATION retval,
the Image was loaded and an ImageHandle was created with a valid
EFI_LOADED_IMAGE_PROTOCOL, but the image can not be started right now.
This follows UEFI Spec.

But if the caller of LoadImage() doesn't have the option to defer
the execution of an image, we can not treat EFI_SECURITY_VIOLATION
like any other LoadImage() error, we should unload image for the
EFI_SECURITY_VIOLATION to avoid resource leak.

This patch is to do error handling for EFI_SECURITY_VIOLATION explicitly
for the callers in EmbeddedPkg which don't have the policy to defer the
execution of the image.

Cc: Leif Lindholm <leif.lindholm@linaro.org>
Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: Laszlo Ersek <lersek@redhat.com>
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1992
Signed-off-by: Dandan Bi <dandan.bi@intel.com>
Acked-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Acked-by: Laszlo Ersek <lersek@redhat.com>
---
 .../AndroidFastboot/Arm/BootAndroidBootImg.c         |  9 +++++++++
 .../Library/AndroidBootImgLib/AndroidBootImgLib.c    | 12 ++++++++++++
 2 files changed, 21 insertions(+)

diff --git a/EmbeddedPkg/Application/AndroidFastboot/Arm/BootAndroidBootImg.c b/EmbeddedPkg/Application/AndroidFastboot/Arm/BootAndroidBootImg.c
index 591afbe7cc..fe05878b4b 100644
--- a/EmbeddedPkg/Application/AndroidFastboot/Arm/BootAndroidBootImg.c
+++ b/EmbeddedPkg/Application/AndroidFastboot/Arm/BootAndroidBootImg.c
@@ -71,10 +71,19 @@ StartEfiApplication (
 
   // Load the image from the device path with Boot Services function
   Status = gBS->LoadImage (TRUE, ParentImageHandle, DevicePath, NULL, 0,
                   &ImageHandle);
   if (EFI_ERROR (Status)) {
+    //
+    // With EFI_SECURITY_VIOLATION retval, the Image was loaded and an ImageHandle was created
+    // with a valid EFI_LOADED_IMAGE_PROTOCOL, but the image can not be started right now.
+    // If the caller doesn't have the option to defer the execution of an image, we should
+    // unload image for the EFI_SECURITY_VIOLATION to avoid resource leak.
+    //
+    if (Status == EFI_SECURITY_VIOLATION) {
+      gBS->UnloadImage (ImageHandle);
+    }
     return Status;
   }
 
   // Passed LoadOptions to the EFI Application
   if (LoadOptionsSize != 0) {
diff --git a/EmbeddedPkg/Library/AndroidBootImgLib/AndroidBootImgLib.c b/EmbeddedPkg/Library/AndroidBootImgLib/AndroidBootImgLib.c
index d9e7aa7d2b..e1036954ee 100644
--- a/EmbeddedPkg/Library/AndroidBootImgLib/AndroidBootImgLib.c
+++ b/EmbeddedPkg/Library/AndroidBootImgLib/AndroidBootImgLib.c
@@ -439,10 +439,22 @@ AndroidBootImgBoot (
                                            + KernelSize;
 
   Status = gBS->LoadImage (TRUE, gImageHandle,
                            (EFI_DEVICE_PATH *)&KernelDevicePath,
                            (VOID*)(UINTN)Kernel, KernelSize, &ImageHandle);
+  if (EFI_ERROR (Status)) {
+    //
+    // With EFI_SECURITY_VIOLATION retval, the Image was loaded and an ImageHandle was created
+    // with a valid EFI_LOADED_IMAGE_PROTOCOL, but the image can not be started right now.
+    // If the caller doesn't have the option to defer the execution of an image, we should
+    // unload image for the EFI_SECURITY_VIOLATION to avoid resource leak.
+    //
+    if (Status == EFI_SECURITY_VIOLATION) {
+      gBS->UnloadImage (ImageHandle);
+    }
+    return Status;
+  }
 
   // Set kernel arguments
   Status = gBS->HandleProtocol (ImageHandle, &gEfiLoadedImageProtocolGuid,
                                 (VOID **) &ImageInfo);
   ImageInfo->LoadOptions = NewKernelArg;
-- 
2.18.0.windows.1


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

* [patch v2 2/5] MdeModulePkg/DxeCapsuleLibFmp: Unload image on EFI_SECURITY_VIOLATION
  2019-09-18  3:05 [patch v2 0/5] Unload image on EFI_SECURITY_VIOLATION Dandan Bi
  2019-09-18  3:05 ` [patch v2 1/5] EmbeddedPkg: " Dandan Bi
@ 2019-09-18  3:05 ` Dandan Bi
  2019-09-18  3:23   ` [edk2-devel] " Wu, Hao A
  2019-09-24 10:35   ` Philippe Mathieu-Daudé
  2019-09-18  3:05 ` [patch v2 3/5] MdeModulePkg/UefiBootManager: " Dandan Bi
                   ` (3 subsequent siblings)
  5 siblings, 2 replies; 15+ messages in thread
From: Dandan Bi @ 2019-09-18  3:05 UTC (permalink / raw)
  To: devel; +Cc: Jian J Wang, Hao A Wu, Liming Gao, Laszlo Ersek

For the LoadImage() boot service, with EFI_SECURITY_VIOLATION retval,
the Image was loaded and an ImageHandle was created with a valid
EFI_LOADED_IMAGE_PROTOCOL, but the image can not be started right now.
This follows UEFI Spec.

But if the caller of LoadImage() doesn't have the option to defer
the execution of an image, we can not treat EFI_SECURITY_VIOLATION
like any other LoadImage() error, we should unload image for the
EFI_SECURITY_VIOLATION to avoid resource leak.

This patch is to do error handling for EFI_SECURITY_VIOLATION explicitly
for the callers in DxeCapsuleLibFmp which don't have the policy to defer
the execution of the image.

Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Hao A Wu <hao.a.wu@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1992
Signed-off-by: Dandan Bi <dandan.bi@intel.com>
---
 MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.c b/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.c
index 95aa9de087..5dda561a04 100644
--- a/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.c
+++ b/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.c
@@ -1028,10 +1028,19 @@ StartFmpImage (
                   ImageSize,
                   &ImageHandle
                   );
   DEBUG((DEBUG_INFO, "FmpCapsule: LoadImage - %r\n", Status));
   if (EFI_ERROR(Status)) {
+    //
+    // With EFI_SECURITY_VIOLATION retval, the Image was loaded and an ImageHandle was created
+    // with a valid EFI_LOADED_IMAGE_PROTOCOL, but the image can not be started right now.
+    // If the caller doesn't have the option to defer the execution of an image, we should
+    // unload image for the EFI_SECURITY_VIOLATION to avoid resource leak.
+    //
+    if (Status == EFI_SECURITY_VIOLATION) {
+      gBS->UnloadImage (ImageHandle);
+    }
     FreePool(DriverDevicePath);
     return Status;
   }
 
   DEBUG((DEBUG_INFO, "FmpCapsule: StartImage ...\n"));
-- 
2.18.0.windows.1


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

* [patch v2 3/5] MdeModulePkg/UefiBootManager: Unload image on EFI_SECURITY_VIOLATION
  2019-09-18  3:05 [patch v2 0/5] Unload image on EFI_SECURITY_VIOLATION Dandan Bi
  2019-09-18  3:05 ` [patch v2 1/5] EmbeddedPkg: " Dandan Bi
  2019-09-18  3:05 ` [patch v2 2/5] MdeModulePkg/DxeCapsuleLibFmp: " Dandan Bi
@ 2019-09-18  3:05 ` Dandan Bi
  2019-09-18  5:28   ` [edk2-devel] " Gao, Zhichao
  2019-09-24 10:34   ` Philippe Mathieu-Daudé
  2019-09-18  3:05 ` [patch v2 4/5] MdeModulePkg/PlatformDriOverride: " Dandan Bi
                   ` (2 subsequent siblings)
  5 siblings, 2 replies; 15+ messages in thread
From: Dandan Bi @ 2019-09-18  3:05 UTC (permalink / raw)
  To: devel; +Cc: Jian J Wang, Hao A Wu, Ray Ni, Zhichao Gao, Liming Gao,
	Laszlo Ersek

For the LoadImage() boot service, with EFI_SECURITY_VIOLATION retval,
the Image was loaded and an ImageHandle was created with a valid
EFI_LOADED_IMAGE_PROTOCOL, but the image can not be started right now.
This follows UEFI Spec.

But if the caller of LoadImage() doesn't have the option to defer
the execution of an image, we can not treat EFI_SECURITY_VIOLATION
like any other LoadImage() error, we should unload image for the
EFI_SECURITY_VIOLATION to avoid resource leak.

This patch is to do error handling for EFI_SECURITY_VIOLATION explicitly
for the callers in UefiBootManagerLib which don't have the policy to defer
the execution of the image.

Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Hao A Wu <hao.a.wu@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Zhichao Gao <zhichao.gao@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1992
Signed-off-by: Dandan Bi <dandan.bi@intel.com>
---
 MdeModulePkg/Library/UefiBootManagerLib/BmBoot.c      |  9 +++++++++
 .../Library/UefiBootManagerLib/BmLoadOption.c         | 11 ++++++++++-
 MdeModulePkg/Library/UefiBootManagerLib/BmMisc.c      | 11 ++++++++++-
 3 files changed, 29 insertions(+), 2 deletions(-)

diff --git a/MdeModulePkg/Library/UefiBootManagerLib/BmBoot.c b/MdeModulePkg/Library/UefiBootManagerLib/BmBoot.c
index 952033fc82..760d7647b8 100644
--- a/MdeModulePkg/Library/UefiBootManagerLib/BmBoot.c
+++ b/MdeModulePkg/Library/UefiBootManagerLib/BmBoot.c
@@ -1859,10 +1859,19 @@ EfiBootManagerBoot (
     if (FilePath != NULL) {
       FreePool (FilePath);
     }
 
     if (EFI_ERROR (Status)) {
+      //
+      // With EFI_SECURITY_VIOLATION retval, the Image was loaded and an ImageHandle was created
+      // with a valid EFI_LOADED_IMAGE_PROTOCOL, but the image can not be started right now.
+      // If the caller doesn't have the option to defer the execution of an image, we should
+      // unload image for the EFI_SECURITY_VIOLATION to avoid resource leak.
+      //
+      if (Status == EFI_SECURITY_VIOLATION) {
+        gBS->UnloadImage (ImageHandle);
+      }
       //
       // Report Status Code with the failure status to indicate that the failure to load boot option
       //
       BmReportLoadFailure (EFI_SW_DXE_BS_EC_BOOT_OPTION_LOAD_ERROR, Status);
       BootOption->Status = Status;
diff --git a/MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.c b/MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.c
index 07592f8ebd..af47b787d1 100644
--- a/MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.c
+++ b/MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.c
@@ -1,9 +1,9 @@
 /** @file
   Load option library functions which relate with creating and processing load options.
 
-Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2011 - 2019, Intel Corporation. All rights reserved.<BR>
 (C) Copyright 2015-2018 Hewlett Packard Enterprise Development LP<BR>
 SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
 
@@ -1409,10 +1409,19 @@ EfiBootManagerProcessLoadOption (
                     FileSize,
                     &ImageHandle
                     );
     FreePool (FileBuffer);
 
+    //
+    // With EFI_SECURITY_VIOLATION retval, the Image was loaded and an ImageHandle was created
+    // with a valid EFI_LOADED_IMAGE_PROTOCOL, but the image can not be started right now.
+    // If the caller doesn't have the option to defer the execution of an image, we should
+    // unload image for the EFI_SECURITY_VIOLATION to avoid resource leak.
+    //
+    if (Status == EFI_SECURITY_VIOLATION) {
+      gBS->UnloadImage (ImageHandle);
+    }
     if (!EFI_ERROR (Status)) {
       Status = gBS->HandleProtocol (ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **)&ImageInfo);
       ASSERT_EFI_ERROR (Status);
 
       ImageInfo->LoadOptionsSize = LoadOption->OptionalDataSize;
diff --git a/MdeModulePkg/Library/UefiBootManagerLib/BmMisc.c b/MdeModulePkg/Library/UefiBootManagerLib/BmMisc.c
index 6b8fb4d924..833e38c6fe 100644
--- a/MdeModulePkg/Library/UefiBootManagerLib/BmMisc.c
+++ b/MdeModulePkg/Library/UefiBootManagerLib/BmMisc.c
@@ -1,9 +1,9 @@
 /** @file
   Misc library functions.
 
-Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2011 - 2019, Intel Corporation. All rights reserved.<BR>
 (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
 SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
 
@@ -491,10 +491,19 @@ EfiBootManagerDispatchDeferredImages (
         ImageDevicePath,
         NULL,
         0,
         &ImageHandle
       );
+      //
+      // With EFI_SECURITY_VIOLATION retval, the Image was loaded and an ImageHandle was created
+      // with a valid EFI_LOADED_IMAGE_PROTOCOL, but the image can not be started right now.
+      // If the caller doesn't have the option to defer the execution of an image, we should
+      // unload image for the EFI_SECURITY_VIOLATION to avoid resource leak.
+      //
+      if (Status == EFI_SECURITY_VIOLATION) {
+        gBS->UnloadImage (ImageHandle);
+      }
       if (!EFI_ERROR (Status)) {
         LoadCount++;
         //
         // Before calling the image, enable the Watchdog Timer for
         // a 5 Minute period
-- 
2.18.0.windows.1


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

* [patch v2 4/5] MdeModulePkg/PlatformDriOverride: Unload image on EFI_SECURITY_VIOLATION
  2019-09-18  3:05 [patch v2 0/5] Unload image on EFI_SECURITY_VIOLATION Dandan Bi
                   ` (2 preceding siblings ...)
  2019-09-18  3:05 ` [patch v2 3/5] MdeModulePkg/UefiBootManager: " Dandan Bi
@ 2019-09-18  3:05 ` Dandan Bi
  2019-09-18  3:49   ` [edk2-devel] " Wu, Hao A
  2019-09-24 10:37   ` Philippe Mathieu-Daudé
  2019-09-18  3:05 ` [patch v2 5/5] ShellPkg: " Dandan Bi
       [not found] ` <15C569713949E871.11658@groups.io>
  5 siblings, 2 replies; 15+ messages in thread
From: Dandan Bi @ 2019-09-18  3:05 UTC (permalink / raw)
  To: devel; +Cc: Jian J Wang, Hao A Wu, Liming Gao, Laszlo Ersek

For the LoadImage() boot service, with EFI_SECURITY_VIOLATION retval,
the Image was loaded and an ImageHandle was created with a valid
EFI_LOADED_IMAGE_PROTOCOL, but the image can not be started right now.
This follows UEFI Spec.

But if the caller of LoadImage() doesn't have the option to defer
the execution of an image, we can not treat EFI_SECURITY_VIOLATION
like any other LoadImage() error, we should unload image for the
EFI_SECURITY_VIOLATION to avoid resource leak.

This patch is to do error handling for EFI_SECURITY_VIOLATION explicitly
for the caller in PlatformDriOverrideDxe which don't have the policy to
defer the execution of the image.

Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Hao A Wu <hao.a.wu@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1992
Signed-off-by: Dandan Bi <dandan.bi@intel.com>
---
 .../PlatformDriOverrideDxe/PlatDriOverrideLib.c       | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/MdeModulePkg/Universal/PlatformDriOverrideDxe/PlatDriOverrideLib.c b/MdeModulePkg/Universal/PlatformDriOverrideDxe/PlatDriOverrideLib.c
index 2d3736b468..f91f038b7a 100644
--- a/MdeModulePkg/Universal/PlatformDriOverrideDxe/PlatDriOverrideLib.c
+++ b/MdeModulePkg/Universal/PlatformDriOverrideDxe/PlatDriOverrideLib.c
@@ -1,9 +1,9 @@
 /** @file
   Implementation of the shared functions to do the platform driver vverride mapping.
 
-  Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2007 - 2019, Intel Corporation. All rights reserved.<BR>
   SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
 
 #include "InternalPlatDriOverrideDxe.h"
@@ -1484,10 +1484,19 @@ GetDriverFromMapping (
                                    );
                 ASSERT (DriverBinding != NULL);
                 DriverImageInfo->ImageHandle = ImageHandle;
               }
             } else {
+              //
+              // With EFI_SECURITY_VIOLATION retval, the Image was loaded and an ImageHandle was created
+              // with a valid EFI_LOADED_IMAGE_PROTOCOL, but the image can not be started right now.
+              // If the caller doesn't have the option to defer the execution of an image, we should
+              // unload image for the EFI_SECURITY_VIOLATION to avoid resource leak.
+              //
+              if (Status == EFI_SECURITY_VIOLATION) {
+                gBS->UnloadImage (ImageHandle);
+              }
               DriverImageInfo->UnLoadable = TRUE;
               DriverImageInfo->ImageHandle = NULL;
             }
           }
         }
-- 
2.18.0.windows.1


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

* [patch v2 5/5] ShellPkg: Unload image on EFI_SECURITY_VIOLATION
  2019-09-18  3:05 [patch v2 0/5] Unload image on EFI_SECURITY_VIOLATION Dandan Bi
                   ` (3 preceding siblings ...)
  2019-09-18  3:05 ` [patch v2 4/5] MdeModulePkg/PlatformDriOverride: " Dandan Bi
@ 2019-09-18  3:05 ` Dandan Bi
  2019-09-24 10:30   ` [edk2-devel] " Philippe Mathieu-Daudé
       [not found] ` <15C569713949E871.11658@groups.io>
  5 siblings, 1 reply; 15+ messages in thread
From: Dandan Bi @ 2019-09-18  3:05 UTC (permalink / raw)
  To: devel; +Cc: Ray Ni, Zhichao Gao, Laszlo Ersek

For the LoadImage() boot service, with EFI_SECURITY_VIOLATION retval,
the Image was loaded and an ImageHandle was created with a valid
EFI_LOADED_IMAGE_PROTOCOL, but the image can not be started right now.
This follows UEFI Spec.

But if the caller of LoadImage() doesn't have the option to defer
the execution of an image, we can not treat EFI_SECURITY_VIOLATION
like any other LoadImage() error, we should unload image for the
EFI_SECURITY_VIOLATION to avoid resource leak.

This patch is to do error handling for EFI_SECURITY_VIOLATION explicitly
for the callers in ShellPkg which don't have the policy to defer the
execution of the image.

Cc: Ray Ni <ray.ni@intel.com>
Cc: Zhichao Gao <zhichao.gao@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1992
Signed-off-by: Dandan Bi <dandan.bi@intel.com>
Reviewed-by: Zhichao Gao <zhichao.gao@intel.com>
---
 ShellPkg/Application/Shell/ShellManParser.c           |  9 +++++++++
 .../Library/UefiShellDebug1CommandsLib/LoadPciRom.c   | 11 ++++++++++-
 ShellPkg/Library/UefiShellLevel2CommandsLib/Load.c    | 11 ++++++++++-
 3 files changed, 29 insertions(+), 2 deletions(-)

diff --git a/ShellPkg/Application/Shell/ShellManParser.c b/ShellPkg/Application/Shell/ShellManParser.c
index 6909f29441..4d5a5668aa 100644
--- a/ShellPkg/Application/Shell/ShellManParser.c
+++ b/ShellPkg/Application/Shell/ShellManParser.c
@@ -643,10 +643,19 @@ ProcessManFile(
       goto Done;
     }
     DevPath = ShellInfoObject.NewEfiShellProtocol->GetDevicePathFromFilePath(CmdFilePathName);
     Status      = gBS->LoadImage(FALSE, gImageHandle, DevPath, NULL, 0, &CmdFileImgHandle);
     if(EFI_ERROR(Status)) {
+      //
+      // With EFI_SECURITY_VIOLATION retval, the Image was loaded and an ImageHandle was created
+      // with a valid EFI_LOADED_IMAGE_PROTOCOL, but the image can not be started right now.
+      // If the caller doesn't have the option to defer the execution of an image, we should
+      // unload image for the EFI_SECURITY_VIOLATION to avoid the resource leak.
+      //
+      if (Status == EFI_SECURITY_VIOLATION) {
+        gBS->UnloadImage (CmdFileImgHandle);
+      }
       *HelpText = NULL;
       goto Done;
     }
     Status = gBS->OpenProtocol(
                     CmdFileImgHandle,
diff --git a/ShellPkg/Library/UefiShellDebug1CommandsLib/LoadPciRom.c b/ShellPkg/Library/UefiShellDebug1CommandsLib/LoadPciRom.c
index 1b169d0d3c..5b6cba17f3 100644
--- a/ShellPkg/Library/UefiShellDebug1CommandsLib/LoadPciRom.c
+++ b/ShellPkg/Library/UefiShellDebug1CommandsLib/LoadPciRom.c
@@ -1,10 +1,10 @@
 /** @file
   Main file for LoadPciRom shell Debug1 function.
 
   (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
-  Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2005 - 2019, Intel Corporation. All rights reserved.<BR>
   SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
 
 #include "UefiShellDebug1CommandsLib.h"
@@ -332,10 +332,19 @@ LoadEfiDriversFromRomImage (
                         ImageBuffer,
                         ImageLength,
                         &ImageHandle
                        );
           if (EFI_ERROR (Status)) {
+            //
+            // With EFI_SECURITY_VIOLATION retval, the Image was loaded and an ImageHandle was created
+            // with a valid EFI_LOADED_IMAGE_PROTOCOL, but the image can not be started right now.
+            // If the caller doesn't have the option to defer the execution of an image, we should
+            // unload image for the EFI_SECURITY_VIOLATION to avoid resource leak.
+            //
+            if (Status == EFI_SECURITY_VIOLATION) {
+              gBS->UnloadImage (ImageHandle);
+            }
             ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_LOADPCIROM_LOAD_FAIL), gShellDebug1HiiHandle, L"loadpcirom", FileName, ImageIndex);
 //            PrintToken (STRING_TOKEN (STR_LOADPCIROM_LOAD_IMAGE_ERROR), HiiHandle, ImageIndex, Status);
           } else {
             Status = gBS->StartImage (ImageHandle, NULL, NULL);
             if (EFI_ERROR (Status)) {
diff --git a/ShellPkg/Library/UefiShellLevel2CommandsLib/Load.c b/ShellPkg/Library/UefiShellLevel2CommandsLib/Load.c
index 6a94b48c86..b6e7c952fa 100644
--- a/ShellPkg/Library/UefiShellLevel2CommandsLib/Load.c
+++ b/ShellPkg/Library/UefiShellLevel2CommandsLib/Load.c
@@ -1,10 +1,10 @@
 /** @file
   Main file for attrib shell level 2 function.
 
   (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
-  Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2009 - 2019, Intel Corporation. All rights reserved.<BR>
   SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
 
 #include "UefiShellLevel2CommandsLib.h"
@@ -110,10 +110,19 @@ LoadDriver(
     NULL,
     0,
     &LoadedDriverHandle);
 
   if (EFI_ERROR(Status)) {
+    //
+    // With EFI_SECURITY_VIOLATION retval, the Image was loaded and an ImageHandle was created
+    // with a valid EFI_LOADED_IMAGE_PROTOCOL, but the image can not be started right now.
+    // If the caller doesn't have the option to defer the execution of an image, we should
+    // unload image for the EFI_SECURITY_VIOLATION to avoid resource leak.
+    //
+    if (Status == EFI_SECURITY_VIOLATION) {
+      gBS->UnloadImage (LoadedDriverHandle);
+    }
     ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_LOAD_NOT_IMAGE), gShellLevel2HiiHandle, FileName, Status);
   } else {
     //
     // Make sure it is a driver image
     //
-- 
2.18.0.windows.1


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

* Re: [edk2-devel] [patch v2 2/5] MdeModulePkg/DxeCapsuleLibFmp: Unload image on EFI_SECURITY_VIOLATION
  2019-09-18  3:05 ` [patch v2 2/5] MdeModulePkg/DxeCapsuleLibFmp: " Dandan Bi
@ 2019-09-18  3:23   ` Wu, Hao A
  2019-09-24 10:35   ` Philippe Mathieu-Daudé
  1 sibling, 0 replies; 15+ messages in thread
From: Wu, Hao A @ 2019-09-18  3:23 UTC (permalink / raw)
  To: devel@edk2.groups.io, Bi, Dandan; +Cc: Wang, Jian J, Gao, Liming, Laszlo Ersek

> -----Original Message-----
> From: devel@edk2.groups.io [mailto:devel@edk2.groups.io] On Behalf Of
> Dandan Bi
> Sent: Wednesday, September 18, 2019 11:06 AM
> To: devel@edk2.groups.io
> Cc: Wang, Jian J; Wu, Hao A; Gao, Liming; Laszlo Ersek
> Subject: [edk2-devel] [patch v2 2/5] MdeModulePkg/DxeCapsuleLibFmp:
> Unload image on EFI_SECURITY_VIOLATION
> 
> For the LoadImage() boot service, with EFI_SECURITY_VIOLATION retval,
> the Image was loaded and an ImageHandle was created with a valid
> EFI_LOADED_IMAGE_PROTOCOL, but the image can not be started right now.
> This follows UEFI Spec.
> 
> But if the caller of LoadImage() doesn't have the option to defer
> the execution of an image, we can not treat EFI_SECURITY_VIOLATION
> like any other LoadImage() error, we should unload image for the
> EFI_SECURITY_VIOLATION to avoid resource leak.
> 
> This patch is to do error handling for EFI_SECURITY_VIOLATION explicitly
> for the callers in DxeCapsuleLibFmp which don't have the policy to defer
> the execution of the image.
> 
> Cc: Jian J Wang <jian.j.wang@intel.com>
> Cc: Hao A Wu <hao.a.wu@intel.com>
> Cc: Liming Gao <liming.gao@intel.com>
> Cc: Laszlo Ersek <lersek@redhat.com>
> REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1992
> Signed-off-by: Dandan Bi <dandan.bi@intel.com>
> ---
>  MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.c | 9 +++++++++
>  1 file changed, 9 insertions(+)
> 
> diff --git a/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.c
> b/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.c
> index 95aa9de087..5dda561a04 100644
> --- a/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.c
> +++ b/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.c
> @@ -1028,10 +1028,19 @@ StartFmpImage (
>                    ImageSize,
>                    &ImageHandle
>                    );
>    DEBUG((DEBUG_INFO, "FmpCapsule: LoadImage - %r\n", Status));
>    if (EFI_ERROR(Status)) {
> +    //
> +    // With EFI_SECURITY_VIOLATION retval, the Image was loaded and an
> ImageHandle was created
> +    // with a valid EFI_LOADED_IMAGE_PROTOCOL, but the image can not be
> started right now.
> +    // If the caller doesn't have the option to defer the execution of an
> image, we should
> +    // unload image for the EFI_SECURITY_VIOLATION to avoid resource leak.
> +    //
> +    if (Status == EFI_SECURITY_VIOLATION) {
> +      gBS->UnloadImage (ImageHandle);
> +    }


Reviewed-by: Hao A Wu <hao.a.wu@intel.com>

Best Regards,
Hao Wu


>      FreePool(DriverDevicePath);
>      return Status;
>    }
> 
>    DEBUG((DEBUG_INFO, "FmpCapsule: StartImage ...\n"));
> --
> 2.18.0.windows.1
> 
> 
> 


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

* Re: [edk2-devel] [patch v2 4/5] MdeModulePkg/PlatformDriOverride: Unload image on EFI_SECURITY_VIOLATION
  2019-09-18  3:05 ` [patch v2 4/5] MdeModulePkg/PlatformDriOverride: " Dandan Bi
@ 2019-09-18  3:49   ` Wu, Hao A
  2019-09-24 10:37   ` Philippe Mathieu-Daudé
  1 sibling, 0 replies; 15+ messages in thread
From: Wu, Hao A @ 2019-09-18  3:49 UTC (permalink / raw)
  To: devel@edk2.groups.io, Bi, Dandan; +Cc: Wang, Jian J, Gao, Liming, Laszlo Ersek

> -----Original Message-----
> From: devel@edk2.groups.io [mailto:devel@edk2.groups.io] On Behalf Of
> Dandan Bi
> Sent: Wednesday, September 18, 2019 11:06 AM
> To: devel@edk2.groups.io
> Cc: Wang, Jian J; Wu, Hao A; Gao, Liming; Laszlo Ersek
> Subject: [edk2-devel] [patch v2 4/5] MdeModulePkg/PlatformDriOverride:
> Unload image on EFI_SECURITY_VIOLATION
> 
> For the LoadImage() boot service, with EFI_SECURITY_VIOLATION retval,
> the Image was loaded and an ImageHandle was created with a valid
> EFI_LOADED_IMAGE_PROTOCOL, but the image can not be started right now.
> This follows UEFI Spec.
> 
> But if the caller of LoadImage() doesn't have the option to defer
> the execution of an image, we can not treat EFI_SECURITY_VIOLATION
> like any other LoadImage() error, we should unload image for the
> EFI_SECURITY_VIOLATION to avoid resource leak.
> 
> This patch is to do error handling for EFI_SECURITY_VIOLATION explicitly
> for the caller in PlatformDriOverrideDxe which don't have the policy to
> defer the execution of the image.
> 
> Cc: Jian J Wang <jian.j.wang@intel.com>
> Cc: Hao A Wu <hao.a.wu@intel.com>
> Cc: Liming Gao <liming.gao@intel.com>
> Cc: Laszlo Ersek <lersek@redhat.com>
> REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1992
> Signed-off-by: Dandan Bi <dandan.bi@intel.com>
> ---
>  .../PlatformDriOverrideDxe/PlatDriOverrideLib.c       | 11 ++++++++++-
>  1 file changed, 10 insertions(+), 1 deletion(-)
> 
> diff --git
> a/MdeModulePkg/Universal/PlatformDriOverrideDxe/PlatDriOverrideLib.c
> b/MdeModulePkg/Universal/PlatformDriOverrideDxe/PlatDriOverrideLib.c
> index 2d3736b468..f91f038b7a 100644
> ---
> a/MdeModulePkg/Universal/PlatformDriOverrideDxe/PlatDriOverrideLib.c
> +++
> b/MdeModulePkg/Universal/PlatformDriOverrideDxe/PlatDriOverrideLib.c
> @@ -1,9 +1,9 @@
>  /** @file
>    Implementation of the shared functions to do the platform driver vverride
> mapping.
> 
> -  Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2007 - 2019, Intel Corporation. All rights reserved.<BR>
>    SPDX-License-Identifier: BSD-2-Clause-Patent
> 
>  **/
> 
>  #include "InternalPlatDriOverrideDxe.h"
> @@ -1484,10 +1484,19 @@ GetDriverFromMapping (
>                                     );
>                  ASSERT (DriverBinding != NULL);
>                  DriverImageInfo->ImageHandle = ImageHandle;
>                }
>              } else {
> +              //
> +              // With EFI_SECURITY_VIOLATION retval, the Image was loaded and
> an ImageHandle was created
> +              // with a valid EFI_LOADED_IMAGE_PROTOCOL, but the image can
> not be started right now.
> +              // If the caller doesn't have the option to defer the execution of an
> image, we should
> +              // unload image for the EFI_SECURITY_VIOLATION to avoid resource
> leak.
> +              //
> +              if (Status == EFI_SECURITY_VIOLATION) {
> +                gBS->UnloadImage (ImageHandle);
> +              }


Reviewed-by: Hao A Wu <hao.a.wu@intel.com>

Best Regards,
Hao Wu


>                DriverImageInfo->UnLoadable = TRUE;
>                DriverImageInfo->ImageHandle = NULL;
>              }
>            }
>          }
> --
> 2.18.0.windows.1
> 
> 
> 


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

* Re: [edk2-devel] [patch v2 3/5] MdeModulePkg/UefiBootManager: Unload image on EFI_SECURITY_VIOLATION
  2019-09-18  3:05 ` [patch v2 3/5] MdeModulePkg/UefiBootManager: " Dandan Bi
@ 2019-09-18  5:28   ` Gao, Zhichao
  2019-09-24 10:34   ` Philippe Mathieu-Daudé
  1 sibling, 0 replies; 15+ messages in thread
From: Gao, Zhichao @ 2019-09-18  5:28 UTC (permalink / raw)
  To: devel@edk2.groups.io, Bi, Dandan
  Cc: Wang, Jian J, Wu, Hao A, Ni, Ray, Gao, Liming, Laszlo Ersek

Reviewed-by: Zhichao Gao <zhichao.gao@intel.com>

Thanks,
Zhichao

> -----Original Message-----
> From: devel@edk2.groups.io [mailto:devel@edk2.groups.io] On Behalf Of
> Dandan Bi
> Sent: Wednesday, September 18, 2019 11:06 AM
> To: devel@edk2.groups.io
> Cc: Wang, Jian J <jian.j.wang@intel.com>; Wu, Hao A <hao.a.wu@intel.com>;
> Ni, Ray <ray.ni@intel.com>; Gao, Zhichao <zhichao.gao@intel.com>; Gao,
> Liming <liming.gao@intel.com>; Laszlo Ersek <lersek@redhat.com>
> Subject: [edk2-devel] [patch v2 3/5] MdeModulePkg/UefiBootManager:
> Unload image on EFI_SECURITY_VIOLATION
> 
> For the LoadImage() boot service, with EFI_SECURITY_VIOLATION retval, the
> Image was loaded and an ImageHandle was created with a valid
> EFI_LOADED_IMAGE_PROTOCOL, but the image can not be started right now.
> This follows UEFI Spec.
> 
> But if the caller of LoadImage() doesn't have the option to defer the
> execution of an image, we can not treat EFI_SECURITY_VIOLATION like any
> other LoadImage() error, we should unload image for the
> EFI_SECURITY_VIOLATION to avoid resource leak.
> 
> This patch is to do error handling for EFI_SECURITY_VIOLATION explicitly for
> the callers in UefiBootManagerLib which don't have the policy to defer the
> execution of the image.
> 
> Cc: Jian J Wang <jian.j.wang@intel.com>
> Cc: Hao A Wu <hao.a.wu@intel.com>
> Cc: Ray Ni <ray.ni@intel.com>
> Cc: Zhichao Gao <zhichao.gao@intel.com>
> Cc: Liming Gao <liming.gao@intel.com>
> Cc: Laszlo Ersek <lersek@redhat.com>
> REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1992
> Signed-off-by: Dandan Bi <dandan.bi@intel.com>
> ---
>  MdeModulePkg/Library/UefiBootManagerLib/BmBoot.c      |  9 +++++++++
>  .../Library/UefiBootManagerLib/BmLoadOption.c         | 11 ++++++++++-
>  MdeModulePkg/Library/UefiBootManagerLib/BmMisc.c      | 11
> ++++++++++-
>  3 files changed, 29 insertions(+), 2 deletions(-)
> 
> diff --git a/MdeModulePkg/Library/UefiBootManagerLib/BmBoot.c
> b/MdeModulePkg/Library/UefiBootManagerLib/BmBoot.c
> index 952033fc82..760d7647b8 100644
> --- a/MdeModulePkg/Library/UefiBootManagerLib/BmBoot.c
> +++ b/MdeModulePkg/Library/UefiBootManagerLib/BmBoot.c
> @@ -1859,10 +1859,19 @@ EfiBootManagerBoot (
>      if (FilePath != NULL) {
>        FreePool (FilePath);
>      }
> 
>      if (EFI_ERROR (Status)) {
> +      //
> +      // With EFI_SECURITY_VIOLATION retval, the Image was loaded and an
> ImageHandle was created
> +      // with a valid EFI_LOADED_IMAGE_PROTOCOL, but the image can not
> be started right now.
> +      // If the caller doesn't have the option to defer the execution of an
> image, we should
> +      // unload image for the EFI_SECURITY_VIOLATION to avoid resource leak.
> +      //
> +      if (Status == EFI_SECURITY_VIOLATION) {
> +        gBS->UnloadImage (ImageHandle);
> +      }
>        //
>        // Report Status Code with the failure status to indicate that the failure to
> load boot option
>        //
>        BmReportLoadFailure
> (EFI_SW_DXE_BS_EC_BOOT_OPTION_LOAD_ERROR, Status);
>        BootOption->Status = Status;
> diff --git a/MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.c
> b/MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.c
> index 07592f8ebd..af47b787d1 100644
> --- a/MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.c
> +++ b/MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.c
> @@ -1,9 +1,9 @@
>  /** @file
>    Load option library functions which relate with creating and processing load
> options.
> 
> -Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2011 - 2019, Intel Corporation. All rights reserved.<BR>
>  (C) Copyright 2015-2018 Hewlett Packard Enterprise Development LP<BR>
>  SPDX-License-Identifier: BSD-2-Clause-Patent
> 
>  **/
> 
> @@ -1409,10 +1409,19 @@ EfiBootManagerProcessLoadOption (
>                      FileSize,
>                      &ImageHandle
>                      );
>      FreePool (FileBuffer);
> 
> +    //
> +    // With EFI_SECURITY_VIOLATION retval, the Image was loaded and an
> ImageHandle was created
> +    // with a valid EFI_LOADED_IMAGE_PROTOCOL, but the image can not be
> started right now.
> +    // If the caller doesn't have the option to defer the execution of an image,
> we should
> +    // unload image for the EFI_SECURITY_VIOLATION to avoid resource leak.
> +    //
> +    if (Status == EFI_SECURITY_VIOLATION) {
> +      gBS->UnloadImage (ImageHandle);
> +    }
>      if (!EFI_ERROR (Status)) {
>        Status = gBS->HandleProtocol (ImageHandle,
> &gEfiLoadedImageProtocolGuid, (VOID **)&ImageInfo);
>        ASSERT_EFI_ERROR (Status);
> 
>        ImageInfo->LoadOptionsSize = LoadOption->OptionalDataSize; diff --git
> a/MdeModulePkg/Library/UefiBootManagerLib/BmMisc.c
> b/MdeModulePkg/Library/UefiBootManagerLib/BmMisc.c
> index 6b8fb4d924..833e38c6fe 100644
> --- a/MdeModulePkg/Library/UefiBootManagerLib/BmMisc.c
> +++ b/MdeModulePkg/Library/UefiBootManagerLib/BmMisc.c
> @@ -1,9 +1,9 @@
>  /** @file
>    Misc library functions.
> 
> -Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2011 - 2019, Intel Corporation. All rights reserved.<BR>
>  (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
>  SPDX-License-Identifier: BSD-2-Clause-Patent
> 
>  **/
> 
> @@ -491,10 +491,19 @@ EfiBootManagerDispatchDeferredImages (
>          ImageDevicePath,
>          NULL,
>          0,
>          &ImageHandle
>        );
> +      //
> +      // With EFI_SECURITY_VIOLATION retval, the Image was loaded and an
> ImageHandle was created
> +      // with a valid EFI_LOADED_IMAGE_PROTOCOL, but the image can not
> be started right now.
> +      // If the caller doesn't have the option to defer the execution of an
> image, we should
> +      // unload image for the EFI_SECURITY_VIOLATION to avoid resource leak.
> +      //
> +      if (Status == EFI_SECURITY_VIOLATION) {
> +        gBS->UnloadImage (ImageHandle);
> +      }
>        if (!EFI_ERROR (Status)) {
>          LoadCount++;
>          //
>          // Before calling the image, enable the Watchdog Timer for
>          // a 5 Minute period
> --
> 2.18.0.windows.1
> 
> 
> 


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

* Re: [edk2-devel] [patch v2 1/5] EmbeddedPkg: Unload image on EFI_SECURITY_VIOLATION
       [not found] ` <15C569713949E871.11658@groups.io>
@ 2019-09-24  1:28   ` Dandan Bi
  0 siblings, 0 replies; 15+ messages in thread
From: Dandan Bi @ 2019-09-24  1:28 UTC (permalink / raw)
  To: devel@edk2.groups.io, Ard Biesheuvel, Leif Lindholm
  Cc: Laszlo Ersek, Gao, Liming, Bi, Dandan

Hi EmbeddedPkg maintainers ,

Could you help push this patch?


Thanks,
Dandan
> -----Original Message-----
> From: devel@edk2.groups.io [mailto:devel@edk2.groups.io] On Behalf Of
> Dandan Bi
> Sent: Wednesday, September 18, 2019 11:06 AM
> To: devel@edk2.groups.io
> Cc: Leif Lindholm <leif.lindholm@linaro.org>; Ard Biesheuvel
> <ard.biesheuvel@linaro.org>; Laszlo Ersek <lersek@redhat.com>
> Subject: [edk2-devel] [patch v2 1/5] EmbeddedPkg: Unload image on
> EFI_SECURITY_VIOLATION
> 
> For the LoadImage() boot service, with EFI_SECURITY_VIOLATION retval, the
> Image was loaded and an ImageHandle was created with a valid
> EFI_LOADED_IMAGE_PROTOCOL, but the image can not be started right now.
> This follows UEFI Spec.
> 
> But if the caller of LoadImage() doesn't have the option to defer the
> execution of an image, we can not treat EFI_SECURITY_VIOLATION like any
> other LoadImage() error, we should unload image for the
> EFI_SECURITY_VIOLATION to avoid resource leak.
> 
> This patch is to do error handling for EFI_SECURITY_VIOLATION explicitly for
> the callers in EmbeddedPkg which don't have the policy to defer the
> execution of the image.
> 
> Cc: Leif Lindholm <leif.lindholm@linaro.org>
> Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> Cc: Laszlo Ersek <lersek@redhat.com>
> REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1992
> Signed-off-by: Dandan Bi <dandan.bi@intel.com>
> Acked-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> Acked-by: Laszlo Ersek <lersek@redhat.com>
> ---
>  .../AndroidFastboot/Arm/BootAndroidBootImg.c         |  9 +++++++++
>  .../Library/AndroidBootImgLib/AndroidBootImgLib.c    | 12 ++++++++++++
>  2 files changed, 21 insertions(+)
> 
> diff --git
> a/EmbeddedPkg/Application/AndroidFastboot/Arm/BootAndroidBootImg.c
> b/EmbeddedPkg/Application/AndroidFastboot/Arm/BootAndroidBootImg.c
> index 591afbe7cc..fe05878b4b 100644
> ---
> a/EmbeddedPkg/Application/AndroidFastboot/Arm/BootAndroidBootImg.c
> +++
> b/EmbeddedPkg/Application/AndroidFastboot/Arm/BootAndroidBootImg.c
> @@ -71,10 +71,19 @@ StartEfiApplication (
> 
>    // Load the image from the device path with Boot Services function
>    Status = gBS->LoadImage (TRUE, ParentImageHandle, DevicePath, NULL, 0,
>                    &ImageHandle);
>    if (EFI_ERROR (Status)) {
> +    //
> +    // With EFI_SECURITY_VIOLATION retval, the Image was loaded and an
> ImageHandle was created
> +    // with a valid EFI_LOADED_IMAGE_PROTOCOL, but the image can not be
> started right now.
> +    // If the caller doesn't have the option to defer the execution of an image,
> we should
> +    // unload image for the EFI_SECURITY_VIOLATION to avoid resource leak.
> +    //
> +    if (Status == EFI_SECURITY_VIOLATION) {
> +      gBS->UnloadImage (ImageHandle);
> +    }
>      return Status;
>    }
> 
>    // Passed LoadOptions to the EFI Application
>    if (LoadOptionsSize != 0) {
> diff --git a/EmbeddedPkg/Library/AndroidBootImgLib/AndroidBootImgLib.c
> b/EmbeddedPkg/Library/AndroidBootImgLib/AndroidBootImgLib.c
> index d9e7aa7d2b..e1036954ee 100644
> --- a/EmbeddedPkg/Library/AndroidBootImgLib/AndroidBootImgLib.c
> +++ b/EmbeddedPkg/Library/AndroidBootImgLib/AndroidBootImgLib.c
> @@ -439,10 +439,22 @@ AndroidBootImgBoot (
>                                             + KernelSize;
> 
>    Status = gBS->LoadImage (TRUE, gImageHandle,
>                             (EFI_DEVICE_PATH *)&KernelDevicePath,
>                             (VOID*)(UINTN)Kernel, KernelSize, &ImageHandle);
> +  if (EFI_ERROR (Status)) {
> +    //
> +    // With EFI_SECURITY_VIOLATION retval, the Image was loaded and an
> ImageHandle was created
> +    // with a valid EFI_LOADED_IMAGE_PROTOCOL, but the image can not be
> started right now.
> +    // If the caller doesn't have the option to defer the execution of an image,
> we should
> +    // unload image for the EFI_SECURITY_VIOLATION to avoid resource leak.
> +    //
> +    if (Status == EFI_SECURITY_VIOLATION) {
> +      gBS->UnloadImage (ImageHandle);
> +    }
> +    return Status;
> +  }
> 
>    // Set kernel arguments
>    Status = gBS->HandleProtocol (ImageHandle,
> &gEfiLoadedImageProtocolGuid,
>                                  (VOID **) &ImageInfo);
>    ImageInfo->LoadOptions = NewKernelArg;
> --
> 2.18.0.windows.1
> 
> 
> 


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

* Re: [edk2-devel] [patch v2 1/5] EmbeddedPkg: Unload image on EFI_SECURITY_VIOLATION
  2019-09-18  3:05 ` [patch v2 1/5] EmbeddedPkg: " Dandan Bi
@ 2019-09-24 10:27   ` Philippe Mathieu-Daudé
  0 siblings, 0 replies; 15+ messages in thread
From: Philippe Mathieu-Daudé @ 2019-09-24 10:27 UTC (permalink / raw)
  To: devel, dandan.bi; +Cc: Leif Lindholm, Ard Biesheuvel, Laszlo Ersek

On 9/18/19 5:05 AM, Dandan Bi wrote:
> For the LoadImage() boot service, with EFI_SECURITY_VIOLATION retval,
> the Image was loaded and an ImageHandle was created with a valid
> EFI_LOADED_IMAGE_PROTOCOL, but the image can not be started right now.
> This follows UEFI Spec.
> 
> But if the caller of LoadImage() doesn't have the option to defer
> the execution of an image, we can not treat EFI_SECURITY_VIOLATION
> like any other LoadImage() error, we should unload image for the
> EFI_SECURITY_VIOLATION to avoid resource leak.
> 
> This patch is to do error handling for EFI_SECURITY_VIOLATION explicitly
> for the callers in EmbeddedPkg which don't have the policy to defer the
> execution of the image.
> 
> Cc: Leif Lindholm <leif.lindholm@linaro.org>
> Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> Cc: Laszlo Ersek <lersek@redhat.com>
> REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1992
> Signed-off-by: Dandan Bi <dandan.bi@intel.com>
> Acked-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> Acked-by: Laszlo Ersek <lersek@redhat.com>
> ---
>  .../AndroidFastboot/Arm/BootAndroidBootImg.c         |  9 +++++++++
>  .../Library/AndroidBootImgLib/AndroidBootImgLib.c    | 12 ++++++++++++
>  2 files changed, 21 insertions(+)
> 
> diff --git a/EmbeddedPkg/Application/AndroidFastboot/Arm/BootAndroidBootImg.c b/EmbeddedPkg/Application/AndroidFastboot/Arm/BootAndroidBootImg.c
> index 591afbe7cc..fe05878b4b 100644
> --- a/EmbeddedPkg/Application/AndroidFastboot/Arm/BootAndroidBootImg.c
> +++ b/EmbeddedPkg/Application/AndroidFastboot/Arm/BootAndroidBootImg.c
> @@ -71,10 +71,19 @@ StartEfiApplication (
>  
>    // Load the image from the device path with Boot Services function
>    Status = gBS->LoadImage (TRUE, ParentImageHandle, DevicePath, NULL, 0,
>                    &ImageHandle);
>    if (EFI_ERROR (Status)) {
> +    //
> +    // With EFI_SECURITY_VIOLATION retval, the Image was loaded and an ImageHandle was created
> +    // with a valid EFI_LOADED_IMAGE_PROTOCOL, but the image can not be started right now.
> +    // If the caller doesn't have the option to defer the execution of an image, we should
> +    // unload image for the EFI_SECURITY_VIOLATION to avoid resource leak.
> +    //
> +    if (Status == EFI_SECURITY_VIOLATION) {
> +      gBS->UnloadImage (ImageHandle);
> +    }
>      return Status;
>    }
>  
>    // Passed LoadOptions to the EFI Application
>    if (LoadOptionsSize != 0) {
> diff --git a/EmbeddedPkg/Library/AndroidBootImgLib/AndroidBootImgLib.c b/EmbeddedPkg/Library/AndroidBootImgLib/AndroidBootImgLib.c
> index d9e7aa7d2b..e1036954ee 100644
> --- a/EmbeddedPkg/Library/AndroidBootImgLib/AndroidBootImgLib.c
> +++ b/EmbeddedPkg/Library/AndroidBootImgLib/AndroidBootImgLib.c
> @@ -439,10 +439,22 @@ AndroidBootImgBoot (
>                                             + KernelSize;
>  
>    Status = gBS->LoadImage (TRUE, gImageHandle,
>                             (EFI_DEVICE_PATH *)&KernelDevicePath,
>                             (VOID*)(UINTN)Kernel, KernelSize, &ImageHandle);
> +  if (EFI_ERROR (Status)) {
> +    //
> +    // With EFI_SECURITY_VIOLATION retval, the Image was loaded and an ImageHandle was created
> +    // with a valid EFI_LOADED_IMAGE_PROTOCOL, but the image can not be started right now.
> +    // If the caller doesn't have the option to defer the execution of an image, we should
> +    // unload image for the EFI_SECURITY_VIOLATION to avoid resource leak.
> +    //
> +    if (Status == EFI_SECURITY_VIOLATION) {
> +      gBS->UnloadImage (ImageHandle);
> +    }
> +    return Status;
> +  }
>  
>    // Set kernel arguments
>    Status = gBS->HandleProtocol (ImageHandle, &gEfiLoadedImageProtocolGuid,
>                                  (VOID **) &ImageInfo);
>    ImageInfo->LoadOptions = NewKernelArg;
> 

Reviewed-by: Philippe Mathieu-Daude <philmd@redhat.com>

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

* Re: [edk2-devel] [patch v2 5/5] ShellPkg: Unload image on EFI_SECURITY_VIOLATION
  2019-09-18  3:05 ` [patch v2 5/5] ShellPkg: " Dandan Bi
@ 2019-09-24 10:30   ` Philippe Mathieu-Daudé
  0 siblings, 0 replies; 15+ messages in thread
From: Philippe Mathieu-Daudé @ 2019-09-24 10:30 UTC (permalink / raw)
  To: devel, dandan.bi; +Cc: Ray Ni, Zhichao Gao, Laszlo Ersek

On 9/18/19 5:05 AM, Dandan Bi wrote:
> For the LoadImage() boot service, with EFI_SECURITY_VIOLATION retval,
> the Image was loaded and an ImageHandle was created with a valid
> EFI_LOADED_IMAGE_PROTOCOL, but the image can not be started right now.
> This follows UEFI Spec.
> 
> But if the caller of LoadImage() doesn't have the option to defer
> the execution of an image, we can not treat EFI_SECURITY_VIOLATION
> like any other LoadImage() error, we should unload image for the
> EFI_SECURITY_VIOLATION to avoid resource leak.
> 
> This patch is to do error handling for EFI_SECURITY_VIOLATION explicitly
> for the callers in ShellPkg which don't have the policy to defer the
> execution of the image.
> 
> Cc: Ray Ni <ray.ni@intel.com>
> Cc: Zhichao Gao <zhichao.gao@intel.com>
> Cc: Laszlo Ersek <lersek@redhat.com>
> REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1992
> Signed-off-by: Dandan Bi <dandan.bi@intel.com>
> Reviewed-by: Zhichao Gao <zhichao.gao@intel.com>
> ---
>  ShellPkg/Application/Shell/ShellManParser.c           |  9 +++++++++
>  .../Library/UefiShellDebug1CommandsLib/LoadPciRom.c   | 11 ++++++++++-
>  ShellPkg/Library/UefiShellLevel2CommandsLib/Load.c    | 11 ++++++++++-
>  3 files changed, 29 insertions(+), 2 deletions(-)
> 
> diff --git a/ShellPkg/Application/Shell/ShellManParser.c b/ShellPkg/Application/Shell/ShellManParser.c
> index 6909f29441..4d5a5668aa 100644
> --- a/ShellPkg/Application/Shell/ShellManParser.c
> +++ b/ShellPkg/Application/Shell/ShellManParser.c
> @@ -643,10 +643,19 @@ ProcessManFile(
>        goto Done;
>      }
>      DevPath = ShellInfoObject.NewEfiShellProtocol->GetDevicePathFromFilePath(CmdFilePathName);
>      Status      = gBS->LoadImage(FALSE, gImageHandle, DevPath, NULL, 0, &CmdFileImgHandle);
>      if(EFI_ERROR(Status)) {
> +      //
> +      // With EFI_SECURITY_VIOLATION retval, the Image was loaded and an ImageHandle was created
> +      // with a valid EFI_LOADED_IMAGE_PROTOCOL, but the image can not be started right now.
> +      // If the caller doesn't have the option to defer the execution of an image, we should
> +      // unload image for the EFI_SECURITY_VIOLATION to avoid the resource leak.
> +      //
> +      if (Status == EFI_SECURITY_VIOLATION) {
> +        gBS->UnloadImage (CmdFileImgHandle);
> +      }

OK

>        *HelpText = NULL;
>        goto Done;
>      }
>      Status = gBS->OpenProtocol(
>                      CmdFileImgHandle,
> diff --git a/ShellPkg/Library/UefiShellDebug1CommandsLib/LoadPciRom.c b/ShellPkg/Library/UefiShellDebug1CommandsLib/LoadPciRom.c
> index 1b169d0d3c..5b6cba17f3 100644
> --- a/ShellPkg/Library/UefiShellDebug1CommandsLib/LoadPciRom.c
> +++ b/ShellPkg/Library/UefiShellDebug1CommandsLib/LoadPciRom.c
> @@ -1,10 +1,10 @@
>  /** @file
>    Main file for LoadPciRom shell Debug1 function.
>  
>    (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
> -  Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2005 - 2019, Intel Corporation. All rights reserved.<BR>
>    SPDX-License-Identifier: BSD-2-Clause-Patent
>  
>  **/
>  
>  #include "UefiShellDebug1CommandsLib.h"
> @@ -332,10 +332,19 @@ LoadEfiDriversFromRomImage (
>                          ImageBuffer,
>                          ImageLength,
>                          &ImageHandle
>                         );
>            if (EFI_ERROR (Status)) {
> +            //
> +            // With EFI_SECURITY_VIOLATION retval, the Image was loaded and an ImageHandle was created
> +            // with a valid EFI_LOADED_IMAGE_PROTOCOL, but the image can not be started right now.
> +            // If the caller doesn't have the option to defer the execution of an image, we should
> +            // unload image for the EFI_SECURITY_VIOLATION to avoid resource leak.
> +            //
> +            if (Status == EFI_SECURITY_VIOLATION) {
> +              gBS->UnloadImage (ImageHandle);
> +            }

OK

>              ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_LOADPCIROM_LOAD_FAIL), gShellDebug1HiiHandle, L"loadpcirom", FileName, ImageIndex);
>  //            PrintToken (STRING_TOKEN (STR_LOADPCIROM_LOAD_IMAGE_ERROR), HiiHandle, ImageIndex, Status);
>            } else {
>              Status = gBS->StartImage (ImageHandle, NULL, NULL);
>              if (EFI_ERROR (Status)) {
> diff --git a/ShellPkg/Library/UefiShellLevel2CommandsLib/Load.c b/ShellPkg/Library/UefiShellLevel2CommandsLib/Load.c
> index 6a94b48c86..b6e7c952fa 100644
> --- a/ShellPkg/Library/UefiShellLevel2CommandsLib/Load.c
> +++ b/ShellPkg/Library/UefiShellLevel2CommandsLib/Load.c
> @@ -1,10 +1,10 @@
>  /** @file
>    Main file for attrib shell level 2 function.
>  
>    (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
> -  Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2009 - 2019, Intel Corporation. All rights reserved.<BR>
>    SPDX-License-Identifier: BSD-2-Clause-Patent
>  
>  **/
>  
>  #include "UefiShellLevel2CommandsLib.h"
> @@ -110,10 +110,19 @@ LoadDriver(
>      NULL,
>      0,
>      &LoadedDriverHandle);
>  
>    if (EFI_ERROR(Status)) {
> +    //
> +    // With EFI_SECURITY_VIOLATION retval, the Image was loaded and an ImageHandle was created
> +    // with a valid EFI_LOADED_IMAGE_PROTOCOL, but the image can not be started right now.
> +    // If the caller doesn't have the option to defer the execution of an image, we should
> +    // unload image for the EFI_SECURITY_VIOLATION to avoid resource leak.
> +    //
> +    if (Status == EFI_SECURITY_VIOLATION) {
> +      gBS->UnloadImage (LoadedDriverHandle);
> +    }

OK.

Reviewed-by: Philippe Mathieu-Daude <philmd@redhat.com>

>      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_LOAD_NOT_IMAGE), gShellLevel2HiiHandle, FileName, Status);
>    } else {
>      //
>      // Make sure it is a driver image
>      //
> 

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

* Re: [edk2-devel] [patch v2 3/5] MdeModulePkg/UefiBootManager: Unload image on EFI_SECURITY_VIOLATION
  2019-09-18  3:05 ` [patch v2 3/5] MdeModulePkg/UefiBootManager: " Dandan Bi
  2019-09-18  5:28   ` [edk2-devel] " Gao, Zhichao
@ 2019-09-24 10:34   ` Philippe Mathieu-Daudé
  1 sibling, 0 replies; 15+ messages in thread
From: Philippe Mathieu-Daudé @ 2019-09-24 10:34 UTC (permalink / raw)
  To: devel, dandan.bi
  Cc: Jian J Wang, Hao A Wu, Ray Ni, Zhichao Gao, Liming Gao,
	Laszlo Ersek

Hi Dandan,

On 9/18/19 5:05 AM, Dandan Bi wrote:
> For the LoadImage() boot service, with EFI_SECURITY_VIOLATION retval,
> the Image was loaded and an ImageHandle was created with a valid
> EFI_LOADED_IMAGE_PROTOCOL, but the image can not be started right now.
> This follows UEFI Spec.
> 
> But if the caller of LoadImage() doesn't have the option to defer
> the execution of an image, we can not treat EFI_SECURITY_VIOLATION
> like any other LoadImage() error, we should unload image for the
> EFI_SECURITY_VIOLATION to avoid resource leak.
> 
> This patch is to do error handling for EFI_SECURITY_VIOLATION explicitly
> for the callers in UefiBootManagerLib which don't have the policy to defer
> the execution of the image.
> 
> Cc: Jian J Wang <jian.j.wang@intel.com>
> Cc: Hao A Wu <hao.a.wu@intel.com>
> Cc: Ray Ni <ray.ni@intel.com>
> Cc: Zhichao Gao <zhichao.gao@intel.com>
> Cc: Liming Gao <liming.gao@intel.com>
> Cc: Laszlo Ersek <lersek@redhat.com>
> REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1992
> Signed-off-by: Dandan Bi <dandan.bi@intel.com>
> ---
>  MdeModulePkg/Library/UefiBootManagerLib/BmBoot.c      |  9 +++++++++
>  .../Library/UefiBootManagerLib/BmLoadOption.c         | 11 ++++++++++-
>  MdeModulePkg/Library/UefiBootManagerLib/BmMisc.c      | 11 ++++++++++-
>  3 files changed, 29 insertions(+), 2 deletions(-)
> 
> diff --git a/MdeModulePkg/Library/UefiBootManagerLib/BmBoot.c b/MdeModulePkg/Library/UefiBootManagerLib/BmBoot.c
> index 952033fc82..760d7647b8 100644
> --- a/MdeModulePkg/Library/UefiBootManagerLib/BmBoot.c
> +++ b/MdeModulePkg/Library/UefiBootManagerLib/BmBoot.c
> @@ -1859,10 +1859,19 @@ EfiBootManagerBoot (
>      if (FilePath != NULL) {
>        FreePool (FilePath);
>      }
>  
>      if (EFI_ERROR (Status)) {
> +      //
> +      // With EFI_SECURITY_VIOLATION retval, the Image was loaded and an ImageHandle was created
> +      // with a valid EFI_LOADED_IMAGE_PROTOCOL, but the image can not be started right now.
> +      // If the caller doesn't have the option to defer the execution of an image, we should
> +      // unload image for the EFI_SECURITY_VIOLATION to avoid resource leak.
> +      //
> +      if (Status == EFI_SECURITY_VIOLATION) {
> +        gBS->UnloadImage (ImageHandle);

OK

> +      }
>        //
>        // Report Status Code with the failure status to indicate that the failure to load boot option
>        //
>        BmReportLoadFailure (EFI_SW_DXE_BS_EC_BOOT_OPTION_LOAD_ERROR, Status);
>        BootOption->Status = Status;
> diff --git a/MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.c b/MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.c
> index 07592f8ebd..af47b787d1 100644
> --- a/MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.c
> +++ b/MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.c
> @@ -1,9 +1,9 @@
>  /** @file
>    Load option library functions which relate with creating and processing load options.
>  
> -Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2011 - 2019, Intel Corporation. All rights reserved.<BR>
>  (C) Copyright 2015-2018 Hewlett Packard Enterprise Development LP<BR>
>  SPDX-License-Identifier: BSD-2-Clause-Patent
>  
>  **/
>  
> @@ -1409,10 +1409,19 @@ EfiBootManagerProcessLoadOption (
>                      FileSize,
>                      &ImageHandle
>                      );
>      FreePool (FileBuffer);
>  

What about:

       if (EFI_ERROR (Status)) {

> +    //
> +    // With EFI_SECURITY_VIOLATION retval, the Image was loaded and an ImageHandle was created
> +    // with a valid EFI_LOADED_IMAGE_PROTOCOL, but the image can not be started right now.
> +    // If the caller doesn't have the option to defer the execution of an image, we should
> +    // unload image for the EFI_SECURITY_VIOLATION to avoid resource leak.
> +    //
> +    if (Status == EFI_SECURITY_VIOLATION) {
> +      gBS->UnloadImage (ImageHandle);
> +    }

-      if (!EFI_ERROR (Status)) {
       } else {

>        Status = gBS->HandleProtocol (ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **)&ImageInfo);
>        ASSERT_EFI_ERROR (Status);
>  
>        ImageInfo->LoadOptionsSize = LoadOption->OptionalDataSize;
> diff --git a/MdeModulePkg/Library/UefiBootManagerLib/BmMisc.c b/MdeModulePkg/Library/UefiBootManagerLib/BmMisc.c
> index 6b8fb4d924..833e38c6fe 100644
> --- a/MdeModulePkg/Library/UefiBootManagerLib/BmMisc.c
> +++ b/MdeModulePkg/Library/UefiBootManagerLib/BmMisc.c
> @@ -1,9 +1,9 @@
>  /** @file
>    Misc library functions.
>  
> -Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2011 - 2019, Intel Corporation. All rights reserved.<BR>
>  (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
>  SPDX-License-Identifier: BSD-2-Clause-Patent
>  
>  **/
>  
> @@ -491,10 +491,19 @@ EfiBootManagerDispatchDeferredImages (
>          ImageDevicePath,
>          NULL,
>          0,
>          &ImageHandle
>        );
> +      //
> +      // With EFI_SECURITY_VIOLATION retval, the Image was loaded and an ImageHandle was created
> +      // with a valid EFI_LOADED_IMAGE_PROTOCOL, but the image can not be started right now.
> +      // If the caller doesn't have the option to defer the execution of an image, we should
> +      // unload image for the EFI_SECURITY_VIOLATION to avoid resource leak.
> +      //
> +      if (Status == EFI_SECURITY_VIOLATION) {
> +        gBS->UnloadImage (ImageHandle);
> +      }
>        if (!EFI_ERROR (Status)) {

Ditto.

The logic is correct, but the code workflow is now odd.

>          LoadCount++;
>          //
>          // Before calling the image, enable the Watchdog Timer for
>          // a 5 Minute period
> 

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

* Re: [edk2-devel] [patch v2 2/5] MdeModulePkg/DxeCapsuleLibFmp: Unload image on EFI_SECURITY_VIOLATION
  2019-09-18  3:05 ` [patch v2 2/5] MdeModulePkg/DxeCapsuleLibFmp: " Dandan Bi
  2019-09-18  3:23   ` [edk2-devel] " Wu, Hao A
@ 2019-09-24 10:35   ` Philippe Mathieu-Daudé
  1 sibling, 0 replies; 15+ messages in thread
From: Philippe Mathieu-Daudé @ 2019-09-24 10:35 UTC (permalink / raw)
  To: devel, dandan.bi; +Cc: Jian J Wang, Hao A Wu, Liming Gao, Laszlo Ersek

On 9/18/19 5:05 AM, Dandan Bi wrote:
> For the LoadImage() boot service, with EFI_SECURITY_VIOLATION retval,
> the Image was loaded and an ImageHandle was created with a valid
> EFI_LOADED_IMAGE_PROTOCOL, but the image can not be started right now.
> This follows UEFI Spec.
> 
> But if the caller of LoadImage() doesn't have the option to defer
> the execution of an image, we can not treat EFI_SECURITY_VIOLATION
> like any other LoadImage() error, we should unload image for the
> EFI_SECURITY_VIOLATION to avoid resource leak.
> 
> This patch is to do error handling for EFI_SECURITY_VIOLATION explicitly
> for the callers in DxeCapsuleLibFmp which don't have the policy to defer
> the execution of the image.
> 
> Cc: Jian J Wang <jian.j.wang@intel.com>
> Cc: Hao A Wu <hao.a.wu@intel.com>
> Cc: Liming Gao <liming.gao@intel.com>
> Cc: Laszlo Ersek <lersek@redhat.com>
> REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1992
> Signed-off-by: Dandan Bi <dandan.bi@intel.com>
> ---
>  MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.c | 9 +++++++++
>  1 file changed, 9 insertions(+)
> 
> diff --git a/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.c b/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.c
> index 95aa9de087..5dda561a04 100644
> --- a/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.c
> +++ b/MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.c
> @@ -1028,10 +1028,19 @@ StartFmpImage (
>                    ImageSize,
>                    &ImageHandle
>                    );
>    DEBUG((DEBUG_INFO, "FmpCapsule: LoadImage - %r\n", Status));
>    if (EFI_ERROR(Status)) {
> +    //
> +    // With EFI_SECURITY_VIOLATION retval, the Image was loaded and an ImageHandle was created
> +    // with a valid EFI_LOADED_IMAGE_PROTOCOL, but the image can not be started right now.
> +    // If the caller doesn't have the option to defer the execution of an image, we should
> +    // unload image for the EFI_SECURITY_VIOLATION to avoid resource leak.
> +    //
> +    if (Status == EFI_SECURITY_VIOLATION) {
> +      gBS->UnloadImage (ImageHandle);
> +    }
>      FreePool(DriverDevicePath);
>      return Status;
>    }
>  
>    DEBUG((DEBUG_INFO, "FmpCapsule: StartImage ...\n"));
> 

Reviewed-by: Philippe Mathieu-Daude <philmd@redhat.com>

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

* Re: [edk2-devel] [patch v2 4/5] MdeModulePkg/PlatformDriOverride: Unload image on EFI_SECURITY_VIOLATION
  2019-09-18  3:05 ` [patch v2 4/5] MdeModulePkg/PlatformDriOverride: " Dandan Bi
  2019-09-18  3:49   ` [edk2-devel] " Wu, Hao A
@ 2019-09-24 10:37   ` Philippe Mathieu-Daudé
  1 sibling, 0 replies; 15+ messages in thread
From: Philippe Mathieu-Daudé @ 2019-09-24 10:37 UTC (permalink / raw)
  To: devel, dandan.bi; +Cc: Jian J Wang, Hao A Wu, Liming Gao, Laszlo Ersek

On 9/18/19 5:05 AM, Dandan Bi wrote:
> For the LoadImage() boot service, with EFI_SECURITY_VIOLATION retval,
> the Image was loaded and an ImageHandle was created with a valid
> EFI_LOADED_IMAGE_PROTOCOL, but the image can not be started right now.
> This follows UEFI Spec.
> 
> But if the caller of LoadImage() doesn't have the option to defer
> the execution of an image, we can not treat EFI_SECURITY_VIOLATION
> like any other LoadImage() error, we should unload image for the
> EFI_SECURITY_VIOLATION to avoid resource leak.
> 
> This patch is to do error handling for EFI_SECURITY_VIOLATION explicitly
> for the caller in PlatformDriOverrideDxe which don't have the policy to
> defer the execution of the image.
> 
> Cc: Jian J Wang <jian.j.wang@intel.com>
> Cc: Hao A Wu <hao.a.wu@intel.com>
> Cc: Liming Gao <liming.gao@intel.com>
> Cc: Laszlo Ersek <lersek@redhat.com>
> REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1992
> Signed-off-by: Dandan Bi <dandan.bi@intel.com>
> ---
>  .../PlatformDriOverrideDxe/PlatDriOverrideLib.c       | 11 ++++++++++-
>  1 file changed, 10 insertions(+), 1 deletion(-)
> 
> diff --git a/MdeModulePkg/Universal/PlatformDriOverrideDxe/PlatDriOverrideLib.c b/MdeModulePkg/Universal/PlatformDriOverrideDxe/PlatDriOverrideLib.c
> index 2d3736b468..f91f038b7a 100644
> --- a/MdeModulePkg/Universal/PlatformDriOverrideDxe/PlatDriOverrideLib.c
> +++ b/MdeModulePkg/Universal/PlatformDriOverrideDxe/PlatDriOverrideLib.c
> @@ -1,9 +1,9 @@
>  /** @file
>    Implementation of the shared functions to do the platform driver vverride mapping.
>  
> -  Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2007 - 2019, Intel Corporation. All rights reserved.<BR>
>    SPDX-License-Identifier: BSD-2-Clause-Patent
>  
>  **/
>  
>  #include "InternalPlatDriOverrideDxe.h"
> @@ -1484,10 +1484,19 @@ GetDriverFromMapping (
>                                     );
>                  ASSERT (DriverBinding != NULL);
>                  DriverImageInfo->ImageHandle = ImageHandle;
>                }
>              } else {
> +              //
> +              // With EFI_SECURITY_VIOLATION retval, the Image was loaded and an ImageHandle was created
> +              // with a valid EFI_LOADED_IMAGE_PROTOCOL, but the image can not be started right now.
> +              // If the caller doesn't have the option to defer the execution of an image, we should
> +              // unload image for the EFI_SECURITY_VIOLATION to avoid resource leak.
> +              //
> +              if (Status == EFI_SECURITY_VIOLATION) {
> +                gBS->UnloadImage (ImageHandle);
> +              }
>                DriverImageInfo->UnLoadable = TRUE;
>                DriverImageInfo->ImageHandle = NULL;
>              }
>            }
>          }
> 

Reviewed-by: Philippe Mathieu-Daude <philmd@redhat.com>

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

end of thread, other threads:[~2019-09-24 10:37 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-09-18  3:05 [patch v2 0/5] Unload image on EFI_SECURITY_VIOLATION Dandan Bi
2019-09-18  3:05 ` [patch v2 1/5] EmbeddedPkg: " Dandan Bi
2019-09-24 10:27   ` [edk2-devel] " Philippe Mathieu-Daudé
2019-09-18  3:05 ` [patch v2 2/5] MdeModulePkg/DxeCapsuleLibFmp: " Dandan Bi
2019-09-18  3:23   ` [edk2-devel] " Wu, Hao A
2019-09-24 10:35   ` Philippe Mathieu-Daudé
2019-09-18  3:05 ` [patch v2 3/5] MdeModulePkg/UefiBootManager: " Dandan Bi
2019-09-18  5:28   ` [edk2-devel] " Gao, Zhichao
2019-09-24 10:34   ` Philippe Mathieu-Daudé
2019-09-18  3:05 ` [patch v2 4/5] MdeModulePkg/PlatformDriOverride: " Dandan Bi
2019-09-18  3:49   ` [edk2-devel] " Wu, Hao A
2019-09-24 10:37   ` Philippe Mathieu-Daudé
2019-09-18  3:05 ` [patch v2 5/5] ShellPkg: " Dandan Bi
2019-09-24 10:30   ` [edk2-devel] " Philippe Mathieu-Daudé
     [not found] ` <15C569713949E871.11658@groups.io>
2019-09-24  1:28   ` [edk2-devel] [patch v2 1/5] EmbeddedPkg: " Dandan Bi

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