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

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