From: Jan Kiszka <[email protected]>

If the stub image comes with a too small alignment, it's easy to grow
the PE header into the section data. This unfortunately happens on ARM
with current toolchains.

Resolve that by moving the sections up as needed when a new section is
adding that causes an overflow. As the code has been prepared to account
for changing layouts between stub parsing, section adding and final
image writing, the actual changes are now small.

Signed-off-by: Jan Kiszka <[email protected]>
---
 tools/bg_gen_unified_kernel | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)

diff --git a/tools/bg_gen_unified_kernel b/tools/bg_gen_unified_kernel
index 28c8f0d..e954fa5 100755
--- a/tools/bg_gen_unified_kernel
+++ b/tools/bg_gen_unified_kernel
@@ -54,6 +54,7 @@ class PEHeaders:
     OPT_OFFS_SECTION_ALIGNMENT = 0x20
     OPT_OFFS_FILE_ALIGNMENT = 0x24
     OPT_OFFS_SIZE_OF_IMAGE = 0x38
+    OPT_OFFS_SIZE_OF_HEADERS = 0x3C
 
     def __init__(self, name, blob):
         # Parse headers: DOS, COFF, optional header
@@ -164,13 +165,19 @@ class PEHeaders:
     def set_size_of_image(self, size):
         self.set_opt_header_field(PEHeaders.OPT_OFFS_SIZE_OF_IMAGE, size)
 
+    def get_size_of_headers(self):
+        return self.get_opt_header_field(PEHeaders.OPT_OFFS_SIZE_OF_HEADERS)
+
+    def set_size_of_headers(self, size):
+        self.set_opt_header_field(PEHeaders.OPT_OFFS_SIZE_OF_HEADERS, size)
+
     def add_section(self, section):
         self.header_size += 0x28
-
-        # check space for adding extra sections
-        if self.first_data < self.header_size:
-            print("FIXME: section data requires relocation", file=sys.stderr)
-            exit(1)
+        size_of_headers = self.get_size_of_headers()
+        if self.header_size > size_of_headers:
+            size_of_headers = align(self.header_size,
+                                    self.get_file_alignment())
+            self.set_size_of_headers(size_of_headers)
 
         self.sections.append(section)
         self.coff_header = struct.pack('<6sH16s', self.coff_header[:6],
@@ -186,6 +193,13 @@ class PEHeaders:
             new_size = self.get_size_of_init_data() + section.data_size
             self.set_size_of_init_data(new_size)
 
+        if size_of_headers > self.first_data:
+            file_relocation = size_of_headers - self.first_data
+            self.first_data += file_relocation
+            for sect in self.sections:
+                if sect.data_size > 0:
+                    sect.data_offs += file_relocation
+
 
 def main():
     parser = argparse.ArgumentParser(
-- 
2.35.3

-- 
You received this message because you are subscribed to the Google Groups "EFI 
Boot Guard" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/efibootguard-dev/93561fa9fd3408f58df095c979fac662f271d6ea.1655731805.git.jan.kiszka%40siemens.com.

Reply via email to