[DRAFT] Fix bug with not returning all OPTIONAL_HEADER DataDirectory entries #451
+1
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently an assumption is made that the number of address/size pairs in the OPTIONAL_HEADER DataDirectory array is given by OPTIONAL_HEADER.NumberOfRvaAndSizes. From the looks of it, most implementations I've found just have a fixed size of 16 (see references below where this quirk seems to be "documented"). From how it seems to be getting used in practice for various PE files that don't set it to 16, NumberOfRvaAndSizes seems to be getting treated as count of how many of the pointers in the DataDirectory list are "null" (zero address and zero size) -- which is kinda obnoxious since it goes against what several of the Microsoft documentation pages say about using it to avoid probing too far.
This issue is the underlying cause of a bug report I got, LLNL/Surfactant#295, and is related to #264.
I can't share the actual file, but I've attached a screenshot from XPEViewer showing how the first entries are 0's for their address/size, and its only index 6 and onward that are actually set. If you can find a bootable UEFI kernel image, I think that would likely exhibit similar behavior. The NumberOfRvaAndSizes for this file is 6.
Before the changes in this PR, this is what pefile shows when I print OPTIONAL_HEADER.DATA_DIRECTORY, and you can see that only the first 6 (empty) entries are in the list (everything else is inaccessible...):
After the changes in this PR, this is what pefile shows when I print OPTIONAL_HEADER.DATA_DIRECTORY, which matches what XPEViewer shows for the addresses and sizes of the data directory entries:
References for DataDirectory having a fixed size of 16:
_IMAGE_DATA_DIRECTORY DataDirectory[16];
in the box for the optional header layout