public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [PATCH] ShellPkg: Avoid corrupting installed device path protocols
@ 2022-12-07 16:12 Ard Biesheuvel
  2022-12-08 16:51 ` [edk2-devel] " Michael D Kinney
  0 siblings, 1 reply; 13+ messages in thread
From: Ard Biesheuvel @ 2022-12-07 16:12 UTC (permalink / raw)
  To: devel; +Cc: ray.ni, zhichao.gao, Ard Biesheuvel

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


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

end of thread, other threads:[~2022-12-08 22:37 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-12-07 16:12 [PATCH] ShellPkg: Avoid corrupting installed device path protocols Ard Biesheuvel
2022-12-08 16:51 ` [edk2-devel] " 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

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