ukify: fix insertion of padding in merged sections

The padding was done to expand the new section contents to the expected size of
the new section. And this then would be used for the content in the existing
section. The new section cannot be larger than the old section, but it can be
smaller. If the new section was smaller, then we'd not write enough padding and
the output file would be corrupted.

This was observed in CI when the .sbat section in the stub was padded to 1k.
The UKI with an .sbat section that was merged and was fairly short would hit
this scenario and be corrupted.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2025-08-19 11:02:44 +02:00
parent 2ddbbb22db
commit ec1d031f3d

View File

@@ -1050,14 +1050,13 @@ def pe_add_sections(opts: UkifyConfig, uki: UKI, output: str) -> None:
f' (need {new_section.Misc_VirtualSize}, have {s.SizeOfRawData})'
)
padding = bytes(new_section.SizeOfRawData - new_section.Misc_VirtualSize)
padding = bytes(s.SizeOfRawData - new_section.Misc_VirtualSize)
pe.__data__ = (
pe.__data__[: s.PointerToRawData]
+ data
+ padding
+ pe.__data__[pe.sections[i + 1].PointerToRawData :]
)
s.SizeOfRawData = new_section.SizeOfRawData
s.Misc_VirtualSize = new_section.Misc_VirtualSize
break
else: