]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/UefiDevicePathLibDevicePathProtocol/UefiDevicePathLib.c
MdePkg UefiDevicePathLibDevicePathProtocol: Validate before use.
[mirror_edk2.git] / MdePkg / Library / UefiDevicePathLibDevicePathProtocol / UefiDevicePathLib.c
index 5cc4b35fe492e3ed9fddc40799a109add92684b7..48724160d4b287978611fab75144ba1ecbdba0ca 100644 (file)
@@ -2,7 +2,7 @@
   Library instance that implement UEFI Device Path Library class based on protocol\r
   gEfiDevicePathUtilitiesProtocolGuid.\r
 \r
-  Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2006 - 2016, 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
@@ -103,25 +103,35 @@ IsDevicePathValid (
 \r
   ASSERT (DevicePath != NULL);\r
 \r
-  for (Count = 0, Size = 0; !IsDevicePathEnd (DevicePath); DevicePath = NextDevicePathNode (DevicePath)) {\r
+  if (MaxSize == 0){\r
+    MaxSize = MAX_UINTN;\r
+  }\r
+\r
+  Size = 0;\r
+  Count = 0;\r
+\r
+  while (MaxSize >= sizeof (EFI_DEVICE_PATH_PROTOCOL) &&\r
+        (MaxSize - sizeof (EFI_DEVICE_PATH_PROTOCOL) >= Size) &&\r
+        !IsDevicePathEnd (DevicePath)) {\r
     NodeLength = DevicePathNodeLength (DevicePath);\r
     if (NodeLength < sizeof (EFI_DEVICE_PATH_PROTOCOL)) {\r
       return FALSE;\r
     }\r
 \r
-    if (MaxSize > 0) {\r
-      Size += NodeLength;\r
-      if (Size + END_DEVICE_PATH_LENGTH > MaxSize) {\r
-        return FALSE;\r
-      }\r
+    if (NodeLength > MAX_UINTN - Size) {\r
+      return FALSE;\r
     }\r
 \r
+    Size += NodeLength;\r
+\r
     if (PcdGet32 (PcdMaximumDevicePathNodeCount) > 0) {\r
       Count++;\r
       if (Count >= PcdGet32 (PcdMaximumDevicePathNodeCount)) {\r
         return FALSE;\r
       }\r
     }\r
+\r
+    DevicePath = NextDevicePathNode (DevicePath);\r
   }\r
 \r
   //\r