]> git.proxmox.com Git - mirror_edk2.git/commitdiff
ShellPkg: Refine code to use Strn**S safe functions instead of Str**S ones in some...
authorQiu Shumin <shumin.qiu@intel.com>
Thu, 9 Jul 2015 03:19:06 +0000 (03:19 +0000)
committershenshushi <shenshushi@Edk2>
Thu, 9 Jul 2015 03:19:06 +0000 (03:19 +0000)
Safe string functions may ASSERT when the source length is larger than the MaxDest. This patch use Strn**S to indicate the copy length.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Qiu Shumin <shumin.qiu@intel.com>
Reviewed-by: Tapan Shah <<tapandshah@hp.com>>
git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@17894 6f19259b-4bc3-4df7-8a09-765794883524

ShellPkg/Application/Shell/FileHandleWrappers.c
ShellPkg/Application/Shell/Shell.c
ShellPkg/Library/UefiDpLib/DpUtilities.c
ShellPkg/Library/UefiShellDebug1CommandsLib/DmpStore.c
ShellPkg/Library/UefiShellDebug1CommandsLib/SmbiosView/QueryTable.c

index 8a71502459995bd18bd8c6ecf0ae32b2ff1850c5..18b6c3e76829dfdf17ea595ea7c98241908d51e3 100644 (file)
@@ -509,7 +509,7 @@ FileInterfaceStdInRead(
         if (StrStr(CurrentString + TabPos, L":") == NULL) {\r
           Cwd = ShellInfoObject.NewEfiShellProtocol->GetCurDir(NULL);\r
           if (Cwd != NULL) {\r
-            StrCpyS(TabStr, (*BufferSize)/sizeof(CHAR16), Cwd);\r
+            StrnCpyS(TabStr, (*BufferSize)/sizeof(CHAR16), Cwd, (*BufferSize)/sizeof(CHAR16) - 1);\r
             if (TabStr[StrLen(TabStr)-1] == L'\\' && *(CurrentString + TabPos) == L'\\' ) {\r
               TabStr[StrLen(TabStr)-1] = CHAR_NULL;\r
             }\r
@@ -523,7 +523,7 @@ FileInterfaceStdInRead(
             StrnCatS(TabStr, (*BufferSize)/sizeof(CHAR16), CurrentString + TabPos, StringLen - TabPos);\r
           }\r
         } else {\r
-          StrCpyS(TabStr, (*BufferSize)/sizeof(CHAR16), CurrentString + TabPos);\r
+          StrnCpyS(TabStr, (*BufferSize)/sizeof(CHAR16), CurrentString + TabPos, (*BufferSize)/sizeof(CHAR16) - 1);\r
         }\r
         StrnCatS(TabStr, (*BufferSize)/sizeof(CHAR16), L"*", (*BufferSize)/sizeof(CHAR16) - 1 - StrLen(TabStr));\r
         FoundFileList = NULL;\r
index 779bb53df64f1d167f7cea3ab67ae48ae0ae6d84..78a64de19eae2d606def95b7ca57f003a9662588 100644 (file)
@@ -2564,6 +2564,7 @@ RunScriptFileHandle (
   EFI_STATUS          Status;\r
   SCRIPT_FILE         *NewScriptFile;\r
   UINTN               LoopVar;\r
+  UINTN               PrintBuffSize;\r
   CHAR16              *CommandLine;\r
   CHAR16              *CommandLine2;\r
   CHAR16              *CommandLine3;\r
@@ -2578,6 +2579,7 @@ RunScriptFileHandle (
   ASSERT(!ShellCommandGetScriptExit());\r
 \r
   PreScriptEchoState = ShellCommandGetEchoState();\r
+  PrintBuffSize = PcdGet16(PcdShellPrintBufferSize);\r
 \r
   NewScriptFile = (SCRIPT_FILE*)AllocateZeroPool(sizeof(SCRIPT_FILE));\r
   if (NewScriptFile == NULL) {\r
@@ -2652,12 +2654,12 @@ RunScriptFileHandle (
   //\r
   // Now enumerate through the commands and run each one.\r
   //\r
-  CommandLine = AllocateZeroPool(PcdGet16(PcdShellPrintBufferSize));\r
+  CommandLine = AllocateZeroPool(PrintBuffSize);\r
   if (CommandLine == NULL) {\r
     DeleteScriptFileStruct(NewScriptFile);\r
     return (EFI_OUT_OF_RESOURCES);\r
   }\r
-  CommandLine2 = AllocateZeroPool(PcdGet16(PcdShellPrintBufferSize));\r
+  CommandLine2 = AllocateZeroPool(PrintBuffSize);\r
   if (CommandLine2 == NULL) {\r
     FreePool(CommandLine);\r
     DeleteScriptFileStruct(NewScriptFile);\r
@@ -2669,9 +2671,10 @@ RunScriptFileHandle (
       ; // conditional increment in the body of the loop\r
   ){\r
     ASSERT(CommandLine2 != NULL);\r
-    StrCpyS( CommandLine2, \r
-              PcdGet16(PcdShellPrintBufferSize)/sizeof(CHAR16), \r
-              NewScriptFile->CurrentCommand->Cl\r
+    StrnCpyS( CommandLine2, \r
+              PrintBuffSize/sizeof(CHAR16), \r
+              NewScriptFile->CurrentCommand->Cl,\r
+              PrintBuffSize/sizeof(CHAR16) - 1\r
               );\r
 \r
     //\r
@@ -2693,9 +2696,10 @@ RunScriptFileHandle (
       //\r
       // Due to variability in starting the find and replace action we need to have both buffers the same.\r
       //\r
-      StrCpyS( CommandLine, \r
-                PcdGet16(PcdShellPrintBufferSize)/sizeof(CHAR16), \r
-                CommandLine2\r
+      StrnCpyS( CommandLine, \r
+                PrintBuffSize/sizeof(CHAR16), \r
+                CommandLine2,\r
+                PrintBuffSize/sizeof(CHAR16) - 1\r
                 );\r
 \r
       //\r
@@ -2704,53 +2708,54 @@ RunScriptFileHandle (
       if (NewScriptFile->Argv != NULL) {\r
         switch (NewScriptFile->Argc) {\r
           default:\r
-            Status = ShellCopySearchAndReplace(CommandLine2,  CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%9", NewScriptFile->Argv[9], FALSE, TRUE);\r
+            Status = ShellCopySearchAndReplace(CommandLine2,  CommandLine, PrintBuffSize, L"%9", NewScriptFile->Argv[9], FALSE, TRUE);\r
             ASSERT_EFI_ERROR(Status);\r
           case 9:\r
-            Status = ShellCopySearchAndReplace(CommandLine,  CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%8", NewScriptFile->Argv[8], FALSE, TRUE);\r
+            Status = ShellCopySearchAndReplace(CommandLine,  CommandLine2, PrintBuffSize, L"%8", NewScriptFile->Argv[8], FALSE, TRUE);\r
             ASSERT_EFI_ERROR(Status);\r
           case 8:\r
-            Status = ShellCopySearchAndReplace(CommandLine2,  CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%7", NewScriptFile->Argv[7], FALSE, TRUE);\r
+            Status = ShellCopySearchAndReplace(CommandLine2,  CommandLine, PrintBuffSize, L"%7", NewScriptFile->Argv[7], FALSE, TRUE);\r
             ASSERT_EFI_ERROR(Status);\r
           case 7:\r
-            Status = ShellCopySearchAndReplace(CommandLine,  CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%6", NewScriptFile->Argv[6], FALSE, TRUE);\r
+            Status = ShellCopySearchAndReplace(CommandLine,  CommandLine2, PrintBuffSize, L"%6", NewScriptFile->Argv[6], FALSE, TRUE);\r
             ASSERT_EFI_ERROR(Status);\r
           case 6:\r
-            Status = ShellCopySearchAndReplace(CommandLine2,  CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%5", NewScriptFile->Argv[5], FALSE, TRUE);\r
+            Status = ShellCopySearchAndReplace(CommandLine2,  CommandLine, PrintBuffSize, L"%5", NewScriptFile->Argv[5], FALSE, TRUE);\r
             ASSERT_EFI_ERROR(Status);\r
           case 5:\r
-            Status = ShellCopySearchAndReplace(CommandLine,  CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%4", NewScriptFile->Argv[4], FALSE, TRUE);\r
+            Status = ShellCopySearchAndReplace(CommandLine,  CommandLine2, PrintBuffSize, L"%4", NewScriptFile->Argv[4], FALSE, TRUE);\r
             ASSERT_EFI_ERROR(Status);\r
           case 4:\r
-            Status = ShellCopySearchAndReplace(CommandLine2,  CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%3", NewScriptFile->Argv[3], FALSE, TRUE);\r
+            Status = ShellCopySearchAndReplace(CommandLine2,  CommandLine, PrintBuffSize, L"%3", NewScriptFile->Argv[3], FALSE, TRUE);\r
             ASSERT_EFI_ERROR(Status);\r
           case 3:\r
-            Status = ShellCopySearchAndReplace(CommandLine,  CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%2", NewScriptFile->Argv[2], FALSE, TRUE);\r
+            Status = ShellCopySearchAndReplace(CommandLine,  CommandLine2, PrintBuffSize, L"%2", NewScriptFile->Argv[2], FALSE, TRUE);\r
             ASSERT_EFI_ERROR(Status);\r
           case 2:\r
-            Status = ShellCopySearchAndReplace(CommandLine2,  CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%1", NewScriptFile->Argv[1], FALSE, TRUE);\r
+            Status = ShellCopySearchAndReplace(CommandLine2,  CommandLine, PrintBuffSize, L"%1", NewScriptFile->Argv[1], FALSE, TRUE);\r
             ASSERT_EFI_ERROR(Status);\r
           case 1:\r
-            Status = ShellCopySearchAndReplace(CommandLine,  CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%0", NewScriptFile->Argv[0], FALSE, TRUE);\r
+            Status = ShellCopySearchAndReplace(CommandLine,  CommandLine2, PrintBuffSize, L"%0", NewScriptFile->Argv[0], FALSE, TRUE);\r
             ASSERT_EFI_ERROR(Status);\r
             break;\r
           case 0:\r
             break;\r
         }\r
       }\r
-      Status = ShellCopySearchAndReplace(CommandLine2,  CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%1", L"\"\"", FALSE, FALSE);\r
-      Status = ShellCopySearchAndReplace(CommandLine,  CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%2", L"\"\"", FALSE, FALSE);\r
-      Status = ShellCopySearchAndReplace(CommandLine2,  CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%3", L"\"\"", FALSE, FALSE);\r
-      Status = ShellCopySearchAndReplace(CommandLine,  CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%4", L"\"\"", FALSE, FALSE);\r
-      Status = ShellCopySearchAndReplace(CommandLine2,  CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%5", L"\"\"", FALSE, FALSE);\r
-      Status = ShellCopySearchAndReplace(CommandLine,  CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%6", L"\"\"", FALSE, FALSE);\r
-      Status = ShellCopySearchAndReplace(CommandLine2,  CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%7", L"\"\"", FALSE, FALSE);\r
-      Status = ShellCopySearchAndReplace(CommandLine,  CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%8", L"\"\"", FALSE, FALSE);\r
-      Status = ShellCopySearchAndReplace(CommandLine2,  CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%9", L"\"\"", FALSE, FALSE);\r
-\r
-      StrCpyS( CommandLine2, \r
-                PcdGet16(PcdShellPrintBufferSize)/sizeof(CHAR16), \r
-                CommandLine\r
+      Status = ShellCopySearchAndReplace(CommandLine2,  CommandLine, PrintBuffSize, L"%1", L"\"\"", FALSE, FALSE);\r
+      Status = ShellCopySearchAndReplace(CommandLine,  CommandLine2, PrintBuffSize, L"%2", L"\"\"", FALSE, FALSE);\r
+      Status = ShellCopySearchAndReplace(CommandLine2,  CommandLine, PrintBuffSize, L"%3", L"\"\"", FALSE, FALSE);\r
+      Status = ShellCopySearchAndReplace(CommandLine,  CommandLine2, PrintBuffSize, L"%4", L"\"\"", FALSE, FALSE);\r
+      Status = ShellCopySearchAndReplace(CommandLine2,  CommandLine, PrintBuffSize, L"%5", L"\"\"", FALSE, FALSE);\r
+      Status = ShellCopySearchAndReplace(CommandLine,  CommandLine2, PrintBuffSize, L"%6", L"\"\"", FALSE, FALSE);\r
+      Status = ShellCopySearchAndReplace(CommandLine2,  CommandLine, PrintBuffSize, L"%7", L"\"\"", FALSE, FALSE);\r
+      Status = ShellCopySearchAndReplace(CommandLine,  CommandLine2, PrintBuffSize, L"%8", L"\"\"", FALSE, FALSE);\r
+      Status = ShellCopySearchAndReplace(CommandLine2,  CommandLine, PrintBuffSize, L"%9", L"\"\"", FALSE, FALSE);\r
+\r
+      StrnCpyS( CommandLine2, \r
+                PrintBuffSize/sizeof(CHAR16), \r
+                CommandLine,\r
+                PrintBuffSize/sizeof(CHAR16) - 1\r
                 );\r
 \r
       LastCommand = NewScriptFile->CurrentCommand;\r
index 7290285e23da6ae143273ef1cfb4bc57e0f6fe92..063eb658c8961ea14121acbadbfceb621c16c420 100644 (file)
@@ -261,7 +261,7 @@ GetNameFromHandle (
                                );\r
     if (!EFI_ERROR (Status)) {\r
       SHELL_FREE_NON_NULL (PlatformLanguage);\r
-      StrCpyS (mGaugeString, DP_GAUGE_STRING_LENGTH + 1, StringPtr);\r
+      StrnCpyS (mGaugeString, DP_GAUGE_STRING_LENGTH + 1, StringPtr, DP_GAUGE_STRING_LENGTH);\r
       mGaugeString[DP_GAUGE_STRING_LENGTH] = 0;\r
       return;\r
     }\r
@@ -305,7 +305,7 @@ GetNameFromHandle (
         //\r
         // Method 3. Get the name string from FFS UI section\r
         //\r
-        StrCpyS (mGaugeString, DP_GAUGE_STRING_LENGTH + 1, NameString);\r
+        StrnCpyS (mGaugeString, DP_GAUGE_STRING_LENGTH + 1, NameString, DP_GAUGE_STRING_LENGTH);\r
         mGaugeString[DP_GAUGE_STRING_LENGTH] = 0;\r
         FreePool (NameString);\r
       } else {\r
@@ -321,7 +321,7 @@ GetNameFromHandle (
       //\r
       NameString = ConvertDevicePathToText (LoadedImageDevicePath, TRUE, FALSE);\r
       if (NameString != NULL) {\r
-        StrCpyS (mGaugeString, DP_GAUGE_STRING_LENGTH + 1, NameString);\r
+        StrnCpyS (mGaugeString, DP_GAUGE_STRING_LENGTH + 1, NameString, DP_GAUGE_STRING_LENGTH);\r
         mGaugeString[DP_GAUGE_STRING_LENGTH] = 0;\r
         FreePool (NameString);\r
         return;\r
@@ -334,7 +334,7 @@ GetNameFromHandle (
   //\r
   StringPtr = HiiGetString (gDpHiiHandle, STRING_TOKEN (STR_DP_ERROR_NAME), NULL);\r
   ASSERT (StringPtr != NULL);\r
-  StrCpyS (mGaugeString, DP_GAUGE_STRING_LENGTH + 1, StringPtr);\r
+  StrnCpyS (mGaugeString, DP_GAUGE_STRING_LENGTH + 1, StringPtr, DP_GAUGE_STRING_LENGTH);\r
   FreePool (StringPtr);\r
 }\r
 \r
index d818b9b8d4707d9814dc677c5eef9adf5135bd44..ac6d0bdb551d290a7e552263c3441dee039d8dc2 100644 (file)
@@ -406,7 +406,7 @@ CascadeProcessVariables (
     FoundVarName = AllocateZeroPool (NameSize);\r
     if (FoundVarName != NULL) {\r
       if (PrevName != NULL) {\r
-        StrCpyS(FoundVarName, NameSize/sizeof(CHAR16), PrevName);\r
+        StrnCpyS(FoundVarName, NameSize/sizeof(CHAR16), PrevName, NameSize/sizeof(CHAR16) - 1);\r
       }\r
 \r
       Status = gRT->GetNextVariableName (&NameSize, FoundVarName, &FoundVarGuid);\r
index dd878c4cb7fc2b4677001861464ea00e9d63f34c..759f486a3295cccfa2c90d4bf6cefd6cb608f0a0 100644 (file)
@@ -3229,8 +3229,8 @@ QueryTable (
     //\r
     if ((High > Low && Key >= Low && Key <= High) \r
       || (Table[Index].Key == Key)) {\r
-      StrCpyS (Info, InfoLen, Table[Index].Info);\r
-      StrCatS (Info, InfoLen, L"\n");\r
+      StrnCpyS (Info, InfoLen, Table[Index].Info, InfoLen - 1);\r
+      StrnCatS (Info, InfoLen, L"\n", InfoLen - 1 - StrLen(Info));\r
       return Key;\r
     }\r
   }\r