public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Ard Biesheuvel" <ardb@kernel.org>
To: devel@edk2.groups.io
Cc: ray.ni@intel.com, zhichao.gao@intel.com,
	Ard Biesheuvel <ardb@kernel.org>
Subject: [PATCH] ShellPkg: Avoid corrupting installed device path protocols
Date: Wed,  7 Dec 2022 17:12:45 +0100	[thread overview]
Message-ID: <20221207161245.2554193-1-ardb@kernel.org> (raw)

The Shell locates device path protocol instances from the database and
happily passes them to destructive device path operations, resulting in
the original protocol to get corrupted as well. So take a copy instead,
and discard it once we no longer need it.

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
 ShellPkg/Application/Shell/ShellProtocol.c    | 10 +++-
 .../Library/UefiShellLevel2CommandsLib/Map.c  | 47 +++++++++++--------
 2 files changed, 36 insertions(+), 21 deletions(-)

diff --git a/ShellPkg/Application/Shell/ShellProtocol.c b/ShellPkg/Application/Shell/ShellProtocol.c
index 509eb60e40f4..6dbf344520d0 100644
--- a/ShellPkg/Application/Shell/ShellProtocol.c
+++ b/ShellPkg/Application/Shell/ShellProtocol.c
@@ -838,7 +838,9 @@ EfiShellOpenRootByHandle (
   EFI_STATUS                       Status;
   EFI_SIMPLE_FILE_SYSTEM_PROTOCOL  *SimpleFileSystem;
   EFI_FILE_PROTOCOL                *RealFileHandle;
-  EFI_DEVICE_PATH_PROTOCOL         *DevPath;
+  CONST EFI_DEVICE_PATH_PROTOCOL   *DevPath;
+  VOID                             *DevPathBuf;
+  EFI_DEVICE_PATH_PROTOCOL         *DevPathCopy;
 
   //
   // get the simple file system interface
@@ -875,7 +877,11 @@ EfiShellOpenRootByHandle (
     return Status;
   }
 
-  *FileHandle = ConvertEfiFileProtocolToShellHandle (RealFileHandle, EfiShellGetMapFromDevicePath (&DevPath));
+  DevPathCopy = DevPathBuf = DuplicateDevicePath (DevPath);
+  *FileHandle = ConvertEfiFileProtocolToShellHandle (RealFileHandle,
+                                                     EfiShellGetMapFromDevicePath (&DevPathCopy)
+                                                     );
+  SHELL_FREE_NON_NULL (DevPathBuf);
   return (EFI_SUCCESS);
 }
 
diff --git a/ShellPkg/Library/UefiShellLevel2CommandsLib/Map.c b/ShellPkg/Library/UefiShellLevel2CommandsLib/Map.c
index f3c888edd48c..094e08eab4a5 100644
--- a/ShellPkg/Library/UefiShellLevel2CommandsLib/Map.c
+++ b/ShellPkg/Library/UefiShellLevel2CommandsLib/Map.c
@@ -134,7 +134,7 @@ SearchList (
 **/
 CHAR16 *
 GetDeviceMediaType (
-  IN  EFI_DEVICE_PATH_PROTOCOL  *DevicePath
+  IN  CONST EFI_DEVICE_PATH_PROTOCOL  *DevicePath
   )
 {
   ACPI_HID_DEVICE_PATH  *Acpi;
@@ -179,7 +179,7 @@ GetDeviceMediaType (
 **/
 BOOLEAN
 IsRemoveableDevice (
-  IN EFI_DEVICE_PATH_PROTOCOL  *DevicePath
+  IN CONST EFI_DEVICE_PATH_PROTOCOL  *DevicePath
   )
 {
   if (NULL == DevicePath) {
@@ -307,24 +307,29 @@ PerformSingleMappingDisplay (
   IN CONST EFI_HANDLE  Handle
   )
 {
-  EFI_DEVICE_PATH_PROTOCOL  *DevPath;
-  EFI_DEVICE_PATH_PROTOCOL  *DevPathCopy;
-  CONST CHAR16              *MapList;
-  CHAR16                    *CurrentName;
-  CHAR16                    *MediaType;
-  CHAR16                    *DevPathString;
-  CHAR16                    *TempSpot;
-  CHAR16                    *Alias;
-  UINTN                     TempLen;
-  BOOLEAN                   Removable;
-  CONST CHAR16              *TempSpot2;
+  CONST EFI_DEVICE_PATH_PROTOCOL  *DevPath;
+  VOID                            *DevPathBuf;
+  EFI_DEVICE_PATH_PROTOCOL        *DevPathCopy;
+  CONST CHAR16                    *MapList;
+  CHAR16                          *CurrentName;
+  CHAR16                          *MediaType;
+  CHAR16                          *DevPathString;
+  CHAR16                          *TempSpot;
+  CHAR16                          *Alias;
+  UINTN                           TempLen;
+  BOOLEAN                         Removable;
+  CONST CHAR16                    *TempSpot2;
 
   Alias       = NULL;
   TempSpot2   = NULL;
   CurrentName = NULL;
   DevPath     = DevicePathFromHandle (Handle);
-  DevPathCopy = DevPath;
+  DevPathBuf  = DuplicateDevicePath (DevPath);
+  DevPathCopy = DevPathBuf;
   MapList     = gEfiShellProtocol->GetMapFromDevicePath (&DevPathCopy);
+
+  SHELL_FREE_NON_NULL (DevPathBuf);
+
   if (MapList == NULL) {
     return EFI_NOT_FOUND;
   }
@@ -485,16 +490,20 @@ PerformSingleMappingDelete (
   IN CONST EFI_HANDLE  Handle
   )
 {
-  EFI_DEVICE_PATH_PROTOCOL  *DevPath;
-  EFI_DEVICE_PATH_PROTOCOL  *DevPathCopy;
-  CONST CHAR16              *MapList;
-  CHAR16                    *CurrentName;
+  CONST EFI_DEVICE_PATH_PROTOCOL  *DevPath;
+  VOID                            *DevPathBuf;
+  EFI_DEVICE_PATH_PROTOCOL        *DevPathCopy;
+  CONST CHAR16                    *MapList;
+  CHAR16                          *CurrentName;
 
   DevPath     = DevicePathFromHandle (Handle);
-  DevPathCopy = DevPath;
+  DevPathBuf  = DuplicateDevicePath (DevPath);
+  DevPathCopy = DevPathBuf;
   MapList     = gEfiShellProtocol->GetMapFromDevicePath (&DevPathCopy);
   CurrentName = NULL;
 
+  SHELL_FREE_NON_NULL (DevPathBuf);
+
   if (MapList == NULL) {
     return (EFI_NOT_FOUND);
   }
-- 
2.35.1


             reply	other threads:[~2022-12-07 16:12 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-07 16:12 Ard Biesheuvel [this message]
2022-12-08 16:51 ` [edk2-devel] [PATCH] ShellPkg: Avoid corrupting installed device path protocols Michael D Kinney
2022-12-08 17:22   ` Ard Biesheuvel
2022-12-08 18:28     ` Michael D Kinney
2022-12-08 18:44       ` Ard Biesheuvel
2022-12-08 19:19         ` Michael D Kinney
2022-12-08 20:12           ` Ard Biesheuvel
2022-12-08 21:15             ` Michael D Kinney
2022-12-08 21:39               ` Ard Biesheuvel
2022-12-08 21:57                 ` Michael D Kinney
2022-12-08 22:18                   ` Michael D Kinney
2022-12-08 22:35                   ` Ard Biesheuvel
2022-12-08 22:37                     ` Ard Biesheuvel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221207161245.2554193-1-ardb@kernel.org \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox