loader: address review of the PE header mapping (#333)

- Check the mapping budget before anything is mapped, and map the header
  bytes straight from the file instead of copying them first.
- Refuse bytes that are not the PE32+ headers that were parsed: the
  optional header magic and size, the section count, the preferred base
  and SizeOfHeaders must agree before anything is patched.
- Write the load base into the mapped ImageBase field after relocations
  are applied, so a DIR64 entry aimed at that field cannot move it twice.
- Describe the ImageBase rewrite as Xenora's policy, and document the
  zero fill past SizeOfHeaders and the stricter page rule.

Signed-off-by: zachdoxsee <zachdoxsee36@gmail.com>
This commit is contained in:
zachdoxsee committed 2026-11-24 19:47:11 +01:00
1 parent d2491dee07
commit 67dfb18166
5 files changed
+197 -51

No files matched your search

+20 -10
View File
@@ -70,16 +70,26 @@ uses a Win64-aligned stack and terminates with HLT. The result includes the load
guest memory and the entry execution outcome.
Loading an image maps its headers as well as its sections. The first
`SizeOfHeaders` bytes of the file are mapped read-only at the image base, as
Windows maps them, in whole 4 KiB pages that are zero past the file bytes. In the
mapped copy the optional header's `ImageBase` field holds the base the image was
loaded at. A guest can read its own DOS header, section table and data
directories there; it cannot write or execute them. The load is refused with
`PeError::InvalidHeaderSize` when `SizeOfHeaders` is zero, ends before the
section table does, runs past the end of the file, or its pages would reach into
a section. The header pages count toward the 256 MiB mapping budget of one
image. `ParsedPe::parse` does not make this check, so static inspection of a
file is unchanged.
`SizeOfHeaders` bytes of the file are mapped read-only at the image base, in
whole 4 KiB pages. Everything in those pages past `SizeOfHeaders` is zero, even
where the file holds more bytes there. A guest can read its own DOS header,
section table and data directories; it cannot write or execute them.
When startup loads an image away from its preferred base, it writes the load
base into the mapped optional header's `ImageBase` field after the relocations
have been applied, so an entry aimed at that field cannot leave any other value
there. This is Xenora's policy: the PE specification defines `ImageBase` as the
preferred base, and the behaviour has not been compared with Windows.
The load is refused with `PeError::InvalidHeaderSize` when `SizeOfHeaders` is
zero, ends before the section table does, runs past the end of the file, or its
pages would reach into a section, and when the bytes given to the loader are not
the PE32+ headers that were parsed. The page rule is stricter than the loader
was before: an image whose section alignment is below 4 KiB, with a section
that starts inside the header page, used to load and is now refused. The header
pages count toward the 256 MiB mapping budget of one image, which is checked
before anything is mapped. `ParsedPe::parse` does not make these checks, so
static inspection of a file is unchanged.
`xenora probe --mode static` inventories ordinary and delay-loaded PE imports
without guest execution. `missing_imports.csv` lists unresolved ordinary keys