Fleming: Thanks for catching this issue. My minor comment is to change the below code to compare with zero.
if (Hdr.Pe32->OptionalHeader.SizeOfImage % Hdr.Pe32->OptionalHeader.SectionAlignment) ==> if ((Hdr.Pe32->OptionalHeader.SizeOfImage % Hdr.Pe32->OptionalHeader.SectionAlignment) != 0) Thanks Liming -----Original Message----- From: Matt Fleming [mailto:[email protected]] Sent: Friday, June 19, 2015 6:06 AM To: [email protected] Cc: Fleming, Matt; Linn Crosetto; Michael Brown Subject: [edk2] [PATCH] MdePkg/BasePeCoffLib: SizeOfImage must be multiple of SectionAlignment From: Matt Fleming <[email protected]> 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 <[email protected]> Cc: Michael Brown <[email protected]> Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Matt Fleming <[email protected]> --- 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 [email protected] https://lists.sourceforge.net/lists/listinfo/edk2-devel ------------------------------------------------------------------------------ _______________________________________________ edk2-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/edk2-devel
