]> git.proxmox.com Git - mirror_edk2.git/commitdiff
ShellPkg/hexedit: Fix a read-after-free bug
authorRuiyu Ni <ruiyu.ni@intel.com>
Wed, 7 Feb 2018 16:05:10 +0000 (00:05 +0800)
committerRuiyu Ni <ruiyu.ni@intel.com>
Fri, 9 Feb 2018 04:24:54 +0000 (12:24 +0800)
HDiskImageSetDiskNameOffsetSize() and HFileImageSetFileName()
may be called using the current disk name or file name.
When this happens, today's implementation firstly frees the memory
and then accesses the just-freed memory.
The patch fixes this issue by doing nothing when the disk or file
name is the current one.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Ruiyu Ni <ruiyu.ni@intel.com>
Reviewed-by: Jaben Carsey <jaben.carsey@intel.com>
ShellPkg/Library/UefiShellDebug1CommandsLib/HexEdit/DiskImage.c
ShellPkg/Library/UefiShellDebug1CommandsLib/HexEdit/FileImage.c

index 846b102975a1c0f81ad4e73469992fbe3f9fdd51..8deb643f07ccf1b7a2a232b025cf48407ed60dbd 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Functions to deal with Disk buffer.\r
 \r
-  Copyright (c) 2005 - 2016, Intel Corporation. All rights reserved. <BR>\r
+  Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved. <BR>\r
   This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
   which accompanies this distribution.  The full text of the license may be found at\r
@@ -120,27 +120,23 @@ HDiskImageSetDiskNameOffsetSize (
   IN UINTN    Size\r
   )\r
 {\r
-  UINTN Len;\r
-  UINTN Index;\r
+  if (Str == HDiskImage.Name) {\r
+    //\r
+    // This function might be called using HDiskImage.FileName as Str.\r
+    // Directly return without updating HDiskImage.FileName.\r
+    //\r
+    return EFI_SUCCESS;\r
+  }\r
 \r
   //\r
   // free the old file name\r
   //\r
   SHELL_FREE_NON_NULL (HDiskImage.Name);\r
-\r
-  Len             = StrLen (Str);\r
-\r
-  HDiskImage.Name = AllocateZeroPool (2 * (Len + 1));\r
+  HDiskImage.Name = AllocateCopyPool (StrSize (Str), Str);\r
   if (HDiskImage.Name == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
 \r
-  for (Index = 0; Index < Len; Index++) {\r
-    HDiskImage.Name[Index] = Str[Index];\r
-  }\r
-\r
-  HDiskImage.Name[Len]  = L'\0';\r
-\r
   HDiskImage.Offset     = Offset;\r
   HDiskImage.Size       = Size;\r
 \r
index 2517a57f593dd05afb5f5858297cbf4f175e5bef..d9fd72cdd2eea475172cab7a46969b603dc6a7b8 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Functions to deal with file buffer.\r
 \r
-  Copyright (c) 2005 - 2015, Intel Corporation. All rights reserved. <BR>\r
+  Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved. <BR>\r
   This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
   which accompanies this distribution.  The full text of the license may be found at\r
@@ -110,27 +110,22 @@ HFileImageSetFileName (
   IN CONST CHAR16 *Str\r
   )\r
 {\r
-  UINTN Size;\r
-  UINTN Index;\r
-\r
+  if (Str == HFileImage.FileName) {\r
+    //\r
+    // This function might be called using HFileImage.FileName as Str.\r
+    // Directly return without updating HFileImage.FileName.\r
+    //\r
+    return EFI_SUCCESS;\r
+  }\r
   //\r
   // free the old file name\r
   //\r
   SHELL_FREE_NON_NULL (HFileImage.FileName);\r
-\r
-  Size                = StrLen (Str);\r
-\r
-  HFileImage.FileName = AllocateZeroPool (2 * (Size + 1));\r
+  HFileImage.FileName = AllocateCopyPool (StrSize (Str), Str);\r
   if (HFileImage.FileName == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
 \r
-  for (Index = 0; Index < Size; Index++) {\r
-    HFileImage.FileName[Index] = Str[Index];\r
-  }\r
-\r
-  HFileImage.FileName[Size] = L'\0';\r
-\r
   return EFI_SUCCESS;\r
 }\r
 \r