3d5e79
From e44306f00bd12f4dca2db20eaba103ff2f260d87 Mon Sep 17 00:00:00 2001
3d5e79
From: Rod Smith <rodsmith@rodsbooks.com>
3d5e79
Date: Mon, 14 Sep 2020 10:08:18 -0400
3d5e79
Subject: [PATCH 1/2] Fix segfault on some weird data structures
3d5e79
3d5e79
---
3d5e79
 gpt.cc | 13 ++++++++++++-
3d5e79
 1 file changed, 12 insertions(+), 1 deletion(-)
3d5e79
3d5e79
diff --git a/gpt.cc b/gpt.cc
3d5e79
index 03a2156..d0242d3 100644
3d5e79
--- a/gpt.cc
3d5e79
+++ b/gpt.cc
3d5e79
@@ -983,6 +983,14 @@ int GPTData::LoadHeader(struct GPTHeader *header, DiskIO & disk, uint64_t sector
3d5e79
    } // if
3d5e79
    *crcOk = CheckHeaderCRC(&tempHeader);
3d5e79
 
3d5e79
+   if (tempHeader.sizeOfPartitionEntries != sizeof(GPTPart)) {
3d5e79
+       cerr << "Warning: Partition table header claims that the size of partition table\n";
3d5e79
+       cerr << "entries is " << tempHeader.sizeOfPartitionEntries << " bytes, but this program ";
3d5e79
+       cerr << " supports only " << sizeof(GPTPart) << "-byte entries.\n";
3d5e79
+       cerr << "Adjusting accordingly, but partition table may be garbage.\n";
3d5e79
+       tempHeader.sizeOfPartitionEntries = sizeof(GPTPart);
3d5e79
+   }
3d5e79
+
3d5e79
    if (allOK && (numParts != tempHeader.numParts) && *crcOk) {
3d5e79
       allOK = SetGPTSize(tempHeader.numParts, 0);
3d5e79
    }
3d5e79
@@ -1000,7 +1008,10 @@ int GPTData::LoadPartitionTable(const struct GPTHeader & header, DiskIO & disk,
3d5e79
    uint32_t sizeOfParts, newCRC;
3d5e79
    int retval;
3d5e79
 
3d5e79
-   if (disk.OpenForRead()) {
3d5e79
+   if (header.sizeOfPartitionEntries != sizeof(GPTPart)) {
3d5e79
+      cerr << "Error! GPT header contains invalid partition entry size!\n";
3d5e79
+      retval = 0;
3d5e79
+   } else if (disk.OpenForRead()) {
3d5e79
       if (sector == 0) {
3d5e79
          retval = disk.Seek(header.partitionEntriesLBA);
3d5e79
       } else {
3d5e79
-- 
3d5e79
2.35.1
3d5e79