Blame SOURCES/0015-Fix-for-Section-0-has-negative-size-error-when-loadi.patch

d84fc6
From 3d04aef8d80293d701f7efee6b5300f9f528ddfc Mon Sep 17 00:00:00 2001
d84fc6
From: Maran Wilson <maran.wilson@oracle.com>
d84fc6
Date: Tue, 7 Aug 2018 15:32:29 -0700
d84fc6
Subject: [PATCH 15/62] Fix for "Section 0 has negative size" error when
d84fc6
 loading fbaa64.efi
d84fc6
d84fc6
The current code is incorrectly failing to load the fbaa64.efi image found
d84fc6
in Arm servers even though the UEFI shell code is able to properly load
d84fc6
and execute the same image.
d84fc6
d84fc6
The problem is due to the presence of a section header that has zero size
d84fc6
and address and marked "discardable" in the fbaa64.efi image.
d84fc6
d84fc6
Although there is already a check further down in the code to look for
d84fc6
the discardable bit and skip further verification checks if set, we never
d84fc6
get to that point due to the "end < base" check at the start of the loop.
d84fc6
d84fc6
Here is a dump of the fbaa64.efi image as compiled on an Arm machine
d84fc6
from the latest code in this repo:
d84fc6
d84fc6
% # First I used hexedit to change header byte from 'AA' to '86'
d84fc6
% # so that objdump was able to correctly parse the file:
d84fc6
% objdump -x -m aarch64 fbaa64.efi
d84fc6
d84fc6
fbaa64.efi:     file format pei-x86-64
d84fc6
fbaa64.efi
d84fc6
architecture: i386:x86-64, flags 0x00000103:
d84fc6
HAS_RELOC, EXEC_P, D_PAGED
d84fc6
start address 0x0000000000000148
d84fc6
d84fc6
Characteristics 0x20e
d84fc6
        executable
d84fc6
        line numbers stripped
d84fc6
        symbols stripped
d84fc6
        debugging information removed
d84fc6
d84fc6
Time/Date               Wed Dec 31 16:00:00 1969
d84fc6
Magic                   020b    (PE32+)
d84fc6
MajorLinkerVersion      2
d84fc6
MinorLinkerVersion      20
d84fc6
SizeOfCode              000b15d0
d84fc6
SizeOfInitializedData   00000000
d84fc6
SizeOfUninitializedData 00000000
d84fc6
AddressOfEntryPoint     0000000000000148
d84fc6
BaseOfCode              0000000000000148
d84fc6
ImageBase               0000000000000000
d84fc6
SectionAlignment        0000000000000020
d84fc6
FileAlignment           0000000000000008
d84fc6
MajorOSystemVersion     0
d84fc6
MinorOSystemVersion     0
d84fc6
MajorImageVersion       0
d84fc6
MinorImageVersion       0
d84fc6
MajorSubsystemVersion   0
d84fc6
MinorSubsystemVersion   0
d84fc6
Win32Version            00000000
d84fc6
SizeOfImage             000b1718
d84fc6
SizeOfHeaders           00000148
d84fc6
CheckSum                00000000
d84fc6
Subsystem               0000000a        (EFI application)
d84fc6
DllCharacteristics      00000000
d84fc6
SizeOfStackReserve      0000000000000000
d84fc6
SizeOfStackCommit       0000000000000000
d84fc6
SizeOfHeapReserve       0000000000000000
d84fc6
SizeOfHeapCommit        0000000000000000
d84fc6
LoaderFlags             00000000
d84fc6
NumberOfRvaAndSizes     00000006
d84fc6
d84fc6
The Data Directory
d84fc6
Entry 0 0000000000000000 00000000 Export Directory [.edata (or where ever we found it)]
d84fc6
Entry 1 0000000000000000 00000000 Import Directory [parts of .idata]
d84fc6
Entry 2 0000000000000000 00000000 Resource Directory [.rsrc]
d84fc6
Entry 3 0000000000000000 00000000 Exception Directory [.pdata]
d84fc6
Entry 4 0000000000000000 00000000 Security Directory
d84fc6
Entry 5 0000000000000000 00000000 Base Relocation Directory [.reloc]
d84fc6
Entry 6 0000000000000000 00000000 Debug Directory
d84fc6
Entry 7 0000000000000000 00000000 Description Directory
d84fc6
Entry 8 0000000000000000 00000000 Special Directory
d84fc6
Entry 9 0000000000000000 00000000 Thread Storage Directory [.tls]
d84fc6
Entry a 0000000000000000 00000000 Load Configuration Directory
d84fc6
Entry b 0000000000000000 00000000 Bound Import Directory
d84fc6
Entry c 0000000000000000 00000000 Import Address Table Directory
d84fc6
Entry d 0000000000000000 00000000 Delay Import Directory
d84fc6
Entry e 0000000000000000 00000000 CLR Runtime Header
d84fc6
Entry f 0000000000000000 00000000 Reserved
d84fc6
d84fc6
Sections:
d84fc6
Idx Name          Size      VMA               LMA               File off  Algn
d84fc6
  0 .reloc        00000000  0000000000000000  0000000000000000  00000000  2**0
d84fc6
                  ALLOC, LOAD, READONLY, DATA
d84fc6
  1 .text         000b15d0  0000000000000148  0000000000000148  00000148  2**4
d84fc6
                  CONTENTS, ALLOC, LOAD, CODE
d84fc6
SYMBOL TABLE:
d84fc6
no symbols
d84fc6
d84fc6
Signed-off-by: Maran Wilson <maran.wilson@oracle.com>
d84fc6
Reviewed-by: Aaron Young <aaron.young@oracle.com>
d84fc6
Reviewed-by: Jack Schwartz <jack.schwartz@oracle.com>
d84fc6
Upstream-commit-id: 6df7a8f5609
d84fc6
---
d84fc6
 shim.c | 5 +++++
d84fc6
 1 file changed, 5 insertions(+)
d84fc6
d84fc6
diff --git a/shim.c b/shim.c
d84fc6
index ae03da7eddf..d980cadacfc 100644
d84fc6
--- a/shim.c
d84fc6
+++ b/shim.c
d84fc6
@@ -1347,6 +1347,11 @@ static EFI_STATUS handle_image (void *data, unsigned int datasize,
d84fc6
 	 */
d84fc6
 	Section = context.FirstSection;
d84fc6
 	for (i = 0; i < context.NumberOfSections; i++, Section++) {
d84fc6
+		/* Don't try to copy discardable sections with zero size */
d84fc6
+		if ((Section->Characteristics & EFI_IMAGE_SCN_MEM_DISCARDABLE) &&
d84fc6
+		    !Section->Misc.VirtualSize)
d84fc6
+			continue;
d84fc6
+
d84fc6
 		base = ImageAddress (buffer, context.ImageSize,
d84fc6
 				     Section->VirtualAddress);
d84fc6
 		end = ImageAddress (buffer, context.ImageSize,
d84fc6
-- 
d84fc6
2.26.2
d84fc6