pe_map_image: reject an empty relocation directory when mapping away from ImageBase #76

Closed
opened 2026-11-21 15:37:27 +00:00 by vanya890 · 4 comments
vanya890 commented 2026-11-21 15:37:27 +00:00 (Migrated from github.com)
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by vanya890 on 2026-11-21T15:37:27Z.


Affected file

tools/pe_map.cpp (current default-branch blob: ad88af2714d5086205122728e68f41437ab72111)

Description

The mapper explicitly allocates the image away from its preferred base:

auto allocate = [&] {
    return static_cast<std::uint8_t*>(
        VirtualAlloc(nullptr, opt.SizeOfImage, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE));
};

It then computes a relocation delta and rejects images with IMAGE_FILE_RELOCS_STRIPPED. However, it does not reject an image whose base relocation directory has Size == 0 while that characteristic bit is clear.

For such an image, the relocation loop executes zero iterations and the function can continue to return the mapped image even though absolute references have not been adjusted for the non-zero mapping delta.

Expected behavior

If the actual mapping base differs from ImageBase, the mapper should require a valid relocation directory capable of applying the required relocations. An empty directory must not be treated as successful relocation.

Suggested fix

After obtaining delta and the relocation directory, reject the image when delta != 0 && relocs.Size == 0. Also ensure the directory contains complete, valid relocation blocks before reporting mapping success.

Reproduction

Provide a PE32+ image with:

  • IMAGE_FILE_RELOCS_STRIPPED clear;
  • an empty base-relocation directory (VirtualAddress == 0, Size == 0);
  • a preferred ImageBase different from the address returned by VirtualAlloc(nullptr, ...).

The current code passes the stripped-relocations check, iterates zero relocation blocks, and can return the image as successfully mapped.

Impact

The mapper may report success for an image whose absolute addresses still assume its preferred base, leading to invalid pointers and crashes when the image is used.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6530852819 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/77). Originally posted by [vanya890](https://github.com/vanya890) on 2026-11-21T15:37:27Z. --- ### Affected file `tools/pe_map.cpp` (current default-branch blob: `ad88af2714d5086205122728e68f41437ab72111`) ### Description The mapper explicitly allocates the image away from its preferred base: ```cpp auto allocate = [&] { return static_cast<std::uint8_t*>( VirtualAlloc(nullptr, opt.SizeOfImage, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); }; ``` It then computes a relocation delta and rejects images with `IMAGE_FILE_RELOCS_STRIPPED`. However, it does not reject an image whose base relocation directory has `Size == 0` while that characteristic bit is clear. For such an image, the relocation loop executes zero iterations and the function can continue to return the mapped image even though absolute references have not been adjusted for the non-zero mapping delta. ### Expected behavior If the actual mapping base differs from `ImageBase`, the mapper should require a valid relocation directory capable of applying the required relocations. An empty directory must not be treated as successful relocation. ### Suggested fix After obtaining `delta` and the relocation directory, reject the image when `delta != 0 && relocs.Size == 0`. Also ensure the directory contains complete, valid relocation blocks before reporting mapping success. ### Reproduction Provide a PE32+ image with: - `IMAGE_FILE_RELOCS_STRIPPED` clear; - an empty base-relocation directory (`VirtualAddress == 0`, `Size == 0`); - a preferred `ImageBase` different from the address returned by `VirtualAlloc(nullptr, ...)`. The current code passes the stripped-relocations check, iterates zero relocation blocks, and can return the image as successfully mapped. ### Impact The mapper may report success for an image whose absolute addresses still assume its preferred base, leading to invalid pointers and crashes when the image is used.
Leyukaka commented 2026-11-24 06:47:34 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-21T15:41:11Z.


Thanks for looking at this. Before changing pe_map I compared it with the Windows loader on an image that has no relocation directory (Size 0) and does not set IMAGE_FILE_RELOCS_STRIPPED, with the preferred base made unavailable.

I reserved 0x180000000 with VirtualAlloc, then loaded cpu_register_probe.dll (ImageBase 0x180000000, base-relocation directory Size 0) with LoadLibraryExW. I also loaded a copy with IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE cleared; that row is the meaningful one, since ASLR would move the as-built image anyway. This was on Windows 11 build 26300, and the addresses vary between runs:

Image LoadLibraryExW pe_map_image
as built (DYNAMIC_BASE set) loaded at 0x7fffd0fe0000 mapped, status 0
DYNAMIC_BASE cleared loaded at 0x1d6af0c0000 mapped, status 0

So Windows accepts a relocation-free image at a different base as long as the stripped flag is clear, and pe_map currently does the same. Several repository fixtures have a relocation directory of Size 0 because they contain no absolute addresses: cpu_import_original, cpu_ordinal_original, cpu_destroy and cpu_register_probe. cpu_native_map maps the first two at a non-preferred base and runs them, and also maps a copy of the register probe. Rejecting delta != 0 && relocs.Size == 0 would refuse images that Windows loads, and would break that existing test. Malformed relocation blocks are already rejected ("bad relocation block").

An image that does contain absolute addresses but omits its relocations is malformed. Windows maps it too, so it would fail at run time under either loader. If that is acceptable, I can add a CTest that pins this behaviour: the same image mapped by both loaders with the preferred base reserved. I can also add a sentence to the pe_map section stating that a missing relocation directory is treated like Windows does, not rejected. If you'd prefer pe_map to be stricter than Windows here, I can do that instead and document the difference.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7065066363 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/77#issuecomment-7065066363). Originally posted by [Leyukaka](https://github.com/Leyukaka) on 2026-11-21T15:41:11Z. --- Thanks for looking at this. Before changing `pe_map` I compared it with the Windows loader on an image that has no relocation directory (Size 0) and does not set `IMAGE_FILE_RELOCS_STRIPPED`, with the preferred base made unavailable. I reserved `0x180000000` with `VirtualAlloc`, then loaded `cpu_register_probe.dll` (`ImageBase 0x180000000`, base-relocation directory Size 0) with `LoadLibraryExW`. I also loaded a copy with `IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE` cleared; that row is the meaningful one, since ASLR would move the as-built image anyway. This was on Windows 11 build 26300, and the addresses vary between runs: | Image | `LoadLibraryExW` | `pe_map_image` | |---|---|---| | as built (DYNAMIC_BASE set) | loaded at `0x7fffd0fe0000` | mapped, status 0 | | DYNAMIC_BASE cleared | loaded at `0x1d6af0c0000` | mapped, status 0 | So Windows accepts a relocation-free image at a different base as long as the stripped flag is clear, and `pe_map` currently does the same. Several repository fixtures have a relocation directory of Size 0 because they contain no absolute addresses: `cpu_import_original`, `cpu_ordinal_original`, `cpu_destroy` and `cpu_register_probe`. `cpu_native_map` maps the first two at a non-preferred base and runs them, and also maps a copy of the register probe. Rejecting `delta != 0 && relocs.Size == 0` would refuse images that Windows loads, and would break that existing test. Malformed relocation blocks are already rejected ("bad relocation block"). An image that does contain absolute addresses but omits its relocations is malformed. Windows maps it too, so it would fail at run time under either loader. If that is acceptable, I can add a CTest that pins this behaviour: the same image mapped by both loaders with the preferred base reserved. I can also add a sentence to the `pe_map` section stating that a missing relocation directory is treated like Windows does, not rejected. If you'd prefer `pe_map` to be stricter than Windows here, I can do that instead and document the difference. _AI-assisted._
R0Y00 commented 2026-11-24 06:47:35 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T15:46:17Z.


Thanks for comparing against the Windows loader. Please add the proposed CTest using the same source-built position-independent fixture with the preferred base reserved for both loaders. Verify the actual mapping bases and callable result in both cases, and document the observed behavior. Let's use that result to settle the empty-directory case.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7065130222 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/77#issuecomment-7065130222). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T15:46:17Z. --- Thanks for comparing against the Windows loader. Please add the proposed CTest using the same source-built position-independent fixture with the preferred base reserved for both loaders. Verify the actual mapping bases and callable result in both cases, and document the observed behavior. Let's use that result to settle the empty-directory case.
R0Y00 commented 2026-11-24 06:47:38 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T16:47:11Z.


The requested comparison is now in #115. With the preferred base reserved, both loaders placed the source-built fixture elsewhere and matched all 249 call results. Both rejected the stripped-relocations control. This supports retaining Windows-compatible behavior for this position-independent fixture. Keeping the issue open until the regression is on main.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7065919044 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/77#issuecomment-7065919044). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T16:47:11Z. --- The requested comparison is now in #115. With the preferred base reserved, both loaders placed the source-built fixture elsewhere and matched all 249 call results. Both rejected the stripped-relocations control. This supports retaining Windows-compatible behavior for this position-independent fixture. Keeping the issue open until the regression is on main.
R0Y00 commented 2026-11-24 06:47:46 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T18:14:01Z.


#115 is on main at e44af731 and the main Windows workflow passed. With the preferred base reserved, the source-built position-independent fixture maps elsewhere under both loaders, with matching call results and a separate stripped-relocations control. Closing the measured empty-size case. #171 covers the distinct zero-address/nonzero-size combination.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7067101202 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/77#issuecomment-7067101202). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T18:14:01Z. --- #115 is on main at `e44af731` and the [main Windows workflow passed](https://github.com/R0Y00/Xbox2Windows/actions/runs/44932787527). With the preferred base reserved, the source-built position-independent fixture maps elsewhere under both loaders, with matching call results and a separate stripped-relocations control. Closing the measured empty-size case. #171 covers the distinct zero-address/nonzero-size combination.
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows#76
No description provided.