microsoft / microsoft/Detours

detour_alloc_trampoline_allocate_new can allocate a region below the lower jmp bound when targeting an address < 0x00080000

Open
#88 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug help-wanted
Dominant language
C++
Stars
6.4k
Forks
1.2k
PR merge metrics
No merged PRs in 30d

Description

Sample trace output from Detours on an intermittent failure to DetourAttach:

[00080000..000214BB..7FFA14BB]
Looking for free region in 00080000..00020000 from 00010000:
Looking for free region in 00020000..7FFA14BB from 00020000:
 Try 00020000 => 00020000..00020FFF   1000
 Try 00030000 => 00030000..00030FFF   1000
 Try 00040000 => 00040000..00040FFF   1000
 Try 00050000 => 00050000..003BFFFF  10000
 Allocated region 00050000..0005FFFF

When determining jmp bounds, detour_2gb_below has a minimum value of 0x80000. In this sample, the entry point I'm attaching to, 0x214BB, is lower than this. As a result DetourAttach fails when it sanity checks that the region it finds is within the jmp bounds.

My current work around is to change the following in detours.cpp from line 1263:

    // Try anything below.
    if (pbTry == NULL) {
        pbTry = detour_alloc_region_from_hi((PBYTE)pLo, pbTarget);
    }
    // try anything above.
    if (pbTry == NULL) {
        pbTry = detour_alloc_region_from_lo(pbTarget, (PBYTE)pHi);
    }

to

    // Try anything below.
    if (pbTry == NULL) {
        pbTry = detour_alloc_region_from_hi((PBYTE)pLo, min(pbTarget, (PBYTE)pHi));
    }
    // try anything above.
    if (pbTry == NULL) {
        pbTry = detour_alloc_region_from_lo(max(pbTarget, (PBYTE)pLo), (PBYTE)pHi);
    }

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with detours.cpp around line 1263 and compare the allocation trace with detour_2gb_below and the DetourAttach sanity check. Use the reported target 0x214BB to verify that the allocated region stays within the computed jump bounds and that DetourAttach no longer fails.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
devtools, operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.