]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Disk/PartitionDxe/Gpt.c
Add DiskIo2 protocol definition to MdePkg.
[mirror_edk2.git] / MdeModulePkg / Universal / Disk / PartitionDxe / Gpt.c
index 6caec01a6cef0043eacee5a478799378de44b791..35860515c13b9da3284b2a5f0d5c57cad2032016 100644 (file)
@@ -2,7 +2,18 @@
   Decode a hard disk partitioned with the GPT scheme in the UEFI 2.0\r
   specification.\r
 \r
-Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>\r
+  Caution: This file requires additional review when modified.\r
+  This driver will have external input - disk partition.\r
+  This external input must be validated carefully to avoid security issue like\r
+  buffer overflow, integer overflow.\r
+\r
+  PartitionInstallGptChildHandles() routine will read disk partition content and\r
+  do basic validation before PartitionInstallChildHandle().\r
+\r
+  PartitionValidGptTable(), PartitionCheckGptEntry() routine will accept disk\r
+  partition content and validate the GPT table and GPT entry.\r
+\r
+Copyright (c) 2006 - 2013, 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
@@ -19,6 +30,10 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 /**\r
   Install child handles if the Handle supports GPT partition structure.\r
 \r
+  Caution: This function may receive untrusted input.\r
+  The GPT partition table header is external input, so this routine\r
+  will do basic validation for GPT partition table header before return.\r
+\r
   @param[in]  BlockIo     Parent BlockIo interface.\r
   @param[in]  DiskIo      Disk Io protocol.\r
   @param[in]  Lba         The starting Lba of the Partition Table\r
@@ -77,8 +92,11 @@ PartitionRestoreGptTable (
 \r
 \r
 /**\r
-  Restore Partition Table to its alternate place.\r
-  (Primary -> Backup or Backup -> Primary)\r
+  This routine will check GPT partition entry and return entry status.\r
+\r
+  Caution: This function may receive untrusted input.\r
+  The GPT partition entry is external input, so this routine\r
+  will do basic validation for GPT partition entry and report status.\r
 \r
   @param[in]    PartHeader    Partition table header structure\r
   @param[in]    PartEntry     The partition entry array\r
@@ -158,9 +176,15 @@ PartitionSetCrc (
 /**\r
   Install child handles if the Handle supports GPT partition structure.\r
 \r
+  Caution: This function may receive untrusted input.\r
+  The GPT partition table is external input, so this routine\r
+  will do basic validation for GPT partition table before install\r
+  child handle for each GPT partition.\r
+\r
   @param[in]  This       Calling context.\r
   @param[in]  Handle     Parent Handle.\r
   @param[in]  DiskIo     Parent DiskIo interface.\r
+  @param[in]  DiskIo2    Parent DiskIo2 interface.\r
   @param[in]  BlockIo    Parent BlockIo interface.\r
   @param[in]  BlockIo2   Parent BlockIo2 interface.\r
   @param[in]  DevicePath Parent Device Path.\r
@@ -175,6 +199,7 @@ PartitionInstallGptChildHandles (
   IN  EFI_DRIVER_BINDING_PROTOCOL  *This,\r
   IN  EFI_HANDLE                   Handle,\r
   IN  EFI_DISK_IO_PROTOCOL         *DiskIo,\r
+  IN  EFI_DISK_IO2_PROTOCOL        *DiskIo2,\r
   IN  EFI_BLOCK_IO_PROTOCOL        *BlockIo,\r
   IN  EFI_BLOCK_IO2_PROTOCOL       *BlockIo2,\r
   IN  EFI_DEVICE_PATH_PROTOCOL     *DevicePath\r
@@ -284,7 +309,7 @@ PartitionInstallGptChildHandles (
     DEBUG ((EFI_D_INFO, " Valid primary and !Valid backup partition table\n"));\r
     DEBUG ((EFI_D_INFO, " Restore backup partition table by the primary\n"));\r
     if (!PartitionRestoreGptTable (BlockIo, DiskIo, PrimaryHeader)) {\r
-      DEBUG ((EFI_D_INFO, " Restore  backup partition table error\n"));\r
+      DEBUG ((EFI_D_INFO, " Restore backup partition table error\n"));\r
     }\r
 \r
     if (PartitionValidGptTable (BlockIo, DiskIo, PrimaryHeader->AlternateLBA, BackupHeader)) {\r
@@ -377,6 +402,7 @@ PartitionInstallGptChildHandles (
                This,\r
                Handle,\r
                DiskIo,\r
+               DiskIo2,\r
                BlockIo,\r
                BlockIo2,\r
                DevicePath,\r
@@ -411,7 +437,11 @@ Done:
 }\r
 \r
 /**\r
-  Install child handles if the Handle supports GPT partition structure.\r
+  This routine will read GPT partition table header and return it.\r
+\r
+  Caution: This function may receive untrusted input.\r
+  The GPT partition table header is external input, so this routine\r
+  will do basic validation for GPT partition table header before return.\r
 \r
   @param[in]  BlockIo     Parent BlockIo interface.\r
   @param[in]  DiskIo      Disk Io protocol.\r
@@ -460,13 +490,22 @@ PartitionValidGptTable (
 \r
   if ((PartHdr->Header.Signature != EFI_PTAB_HEADER_ID) ||\r
       !PartitionCheckCrc (BlockSize, &PartHdr->Header) ||\r
-      PartHdr->MyLBA != Lba\r
+      PartHdr->MyLBA != Lba ||\r
+      (PartHdr->SizeOfPartitionEntry < sizeof (EFI_PARTITION_ENTRY))\r
       ) {\r
     DEBUG ((EFI_D_INFO, "Invalid efi partition table header\n"));\r
     FreePool (PartHdr);\r
     return FALSE;\r
   }\r
 \r
+  //\r
+  // Ensure the NumberOfPartitionEntries * SizeOfPartitionEntry doesn't overflow.\r
+  //\r
+  if (PartHdr->NumberOfPartitionEntries > DivU64x32 (MAX_UINTN, PartHdr->SizeOfPartitionEntry)) {\r
+    FreePool (PartHdr);\r
+    return FALSE;\r
+  }\r
+\r
   CopyMem (PartHeader, PartHdr, sizeof (EFI_PARTITION_TABLE_HEADER));\r
   if (!PartitionCheckGptEntryArrayCRC (BlockIo, DiskIo, PartHeader)) {\r
     FreePool (PartHdr);\r
@@ -640,8 +679,11 @@ Done:
 }\r
 \r
 /**\r
-  Restore Partition Table to its alternate place.\r
-  (Primary -> Backup or Backup -> Primary)\r
+  This routine will check GPT partition entry and return entry status.\r
+\r
+  Caution: This function may receive untrusted input.\r
+  The GPT partition entry is external input, so this routine\r
+  will do basic validation for GPT partition entry and report status.\r
 \r
   @param[in]    PartHeader    Partition table header structure\r
   @param[in]    PartEntry     The partition entry array\r