From: Matt Fleming <matt.flem...@intel.com>

The PE/COFF specification states that the SizeOfImage field must be a
multiple of the SectionAlignment field. Add checks to verify this when
loading an image in PeCoffLoaderGetPeHeader().

This issue was reported by Linn because he discovered that the Linux
kernel's EFI boot stub violates this alignment requirement, and his
firmware refused to load his kernel image.

Reported-by: Linn Crosetto <l...@hp.com>
Cc: Michael Brown <mbr...@fensystems.co.uk>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Matt Fleming <matt.flem...@intel.com>
---
 MdePkg/Library/BasePeCoffLib/BasePeCoff.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/MdePkg/Library/BasePeCoffLib/BasePeCoff.c 
b/MdePkg/Library/BasePeCoffLib/BasePeCoff.c
index 33cad23..f7b740c 100644
--- a/MdePkg/Library/BasePeCoffLib/BasePeCoff.c
+++ b/MdePkg/Library/BasePeCoffLib/BasePeCoff.c
@@ -275,6 +275,16 @@ PeCoffLoaderGetPeHeader (
       }
 
       //
+      // 4.1 Check that the SizeOfImage field is a multiple of
+      // SectionAlignment, since this is required by the PE/COFF
+      // specification.
+      //
+      if (Hdr.Pe32->OptionalHeader.SizeOfImage % 
Hdr.Pe32->OptionalHeader.SectionAlignment) {
+        ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;
+        return RETURN_UNSUPPORTED;
+      }
+
+      //
       // 4.2 Read last byte of Hdr.Pe32.OptionalHeader.SizeOfHeaders from the 
file.
       //
       Size = 1;
@@ -389,6 +399,16 @@ PeCoffLoaderGetPeHeader (
       }
 
       //
+      // 4.1 Check that the SizeOfImage field is a multiple of
+      // SectionAlignment, since this is required by the PE/COFF
+      // specification.
+      //
+      if (Hdr.Pe32Plus->OptionalHeader.SizeOfImage % 
Hdr.Pe32Plus->OptionalHeader.SectionAlignment) {
+        ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;
+        return RETURN_UNSUPPORTED;
+      }
+
+       //
       // 4.2 Read last byte of Hdr.Pe32Plus.OptionalHeader.SizeOfHeaders from 
the file.
       //
       Size = 1;
-- 
2.1.0


------------------------------------------------------------------------------
_______________________________________________
edk2-devel mailing list
edk2-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/edk2-devel

Reply via email to