]> git.proxmox.com Git - mirror_edk2.git/commitdiff
BaseTools/C/Common: Add/refine boundary checks for strcpy/strcat calls
authorHao Wu <hao.a.wu@intel.com>
Mon, 18 Dec 2017 00:57:27 +0000 (08:57 +0800)
committerHao Wu <hao.a.wu@intel.com>
Mon, 25 Dec 2017 01:54:10 +0000 (09:54 +0800)
Add checks to ensure when the destination string buffer is of fixed
size, the strcpy/strcat functions calls will not access beyond the
boundary.

Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Hao Wu <hao.a.wu@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
BaseTools/Source/C/Common/CommonLib.c
BaseTools/Source/C/Common/EfiUtilityMsgs.c

index 2f0aecf252409778090e33127ce482a5d2de80c5..4a62bec85e03d3816a823de8331b035c5fa3b460 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
 Common basic Library Functions\r
 \r
-Copyright (c) 2004 - 2016, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2004 - 2017, 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
@@ -638,12 +638,22 @@ Returns:
       //\r
       RootPath = getcwd (NULL, 0);\r
       if (RootPath != NULL) {\r
-        strcat (mCommonLibFullPath, RootPath);\r
+        if (strlen (mCommonLibFullPath) + strlen (RootPath) > MAX_LONG_FILE_PATH - 1) {\r
+          Error (NULL, 0, 2000, "Invalid parameter", "RootPath is too long!");\r
+          free (RootPath);\r
+          return NULL;\r
+        }\r
+        strncat (mCommonLibFullPath, RootPath, MAX_LONG_FILE_PATH - strlen (mCommonLibFullPath) - 1);\r
         if (FileName[0] != '\\' && FileName[0] != '/') {\r
+          if (strlen (mCommonLibFullPath) + 1 > MAX_LONG_FILE_PATH - 1) {\r
+            Error (NULL, 0, 2000, "Invalid parameter", "RootPath is too long!");\r
+            free (RootPath);\r
+            return NULL;\r
+          }\r
           //\r
           // Attach directory separator\r
           //\r
-          strcat (mCommonLibFullPath, "\\");\r
+          strncat (mCommonLibFullPath, "\\", MAX_LONG_FILE_PATH - strlen (mCommonLibFullPath) - 1);\r
         }\r
         free (RootPath);\r
       }\r
@@ -673,7 +683,7 @@ Returns:
     //\r
     if ((PathPointer = strstr (mCommonLibFullPath, ":\\\\")) != NULL) {\r
       *(PathPointer + 2) = '\0';\r
-      strcat (mCommonLibFullPath, PathPointer + 3);\r
+      strncat (mCommonLibFullPath, PathPointer + 3, MAX_LONG_FILE_PATH - strlen (mCommonLibFullPath) - 1);\r
     }\r
     \r
     //\r
@@ -681,7 +691,7 @@ Returns:
     //\r
     while ((PathPointer = strstr (mCommonLibFullPath, ".\\")) != NULL) {\r
       *PathPointer = '\0';\r
-      strcat (mCommonLibFullPath, PathPointer + 2);\r
+      strncat (mCommonLibFullPath, PathPointer + 2, MAX_LONG_FILE_PATH - strlen (mCommonLibFullPath) - 1);\r
     }\r
         \r
     //\r
@@ -689,7 +699,7 @@ Returns:
     //\r
     while ((PathPointer = strstr (mCommonLibFullPath, "\\.\\")) != NULL) {\r
       *PathPointer = '\0';\r
-      strcat (mCommonLibFullPath, PathPointer + 2);\r
+      strncat (mCommonLibFullPath, PathPointer + 2, MAX_LONG_FILE_PATH - strlen (mCommonLibFullPath) - 1);\r
     }\r
 \r
     //\r
@@ -706,7 +716,7 @@ Returns:
         // Skip one directory\r
         //\r
         *PathPointer = '\0';\r
-        strcat (mCommonLibFullPath, NextPointer);\r
+        strncat (mCommonLibFullPath, NextPointer, MAX_LONG_FILE_PATH - strlen (mCommonLibFullPath) - 1);\r
       } else {\r
         //\r
         // No directory is found. Just break.\r
index 7b4c2310ca9fd2c974fe6d4ba370f89e056ca43d..5496a439e279989776c09d8c3d5dccd11f8b3402 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
 EFI tools utility functions to display warning, error, and informational messages\r
 \r
-Copyright (c) 2004 - 2016, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2004 - 2017, 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
@@ -608,12 +608,9 @@ Returns:
   if (UtilityName != NULL) {\r
     if (strlen (UtilityName) >= sizeof (mUtilityName)) {\r
       Error (UtilityName, 0, 0, "application error", "utility name length exceeds internal buffer size");\r
-      strncpy (mUtilityName, UtilityName, sizeof (mUtilityName) - 1);\r
-      mUtilityName[sizeof (mUtilityName) - 1] = 0;\r
-      return ;\r
-    } else {\r
-      strcpy (mUtilityName, UtilityName);\r
     }\r
+    strncpy (mUtilityName, UtilityName, sizeof (mUtilityName) - 1);\r
+    mUtilityName[sizeof (mUtilityName) - 1] = 0;\r
   } else {\r
     Error (NULL, 0, 0, "application error", "SetUtilityName() called with NULL utility name");\r
   }\r