]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/C/Common/CommonLib.c
BaseTools/C/Common: Add/refine boundary checks for strcpy/strcat calls
[mirror_edk2.git] / BaseTools / Source / C / Common / CommonLib.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