Skip to content

MemoryProtector on pointers created by new() #206

@enginelesscc

Description

@enginelesscc

Hi, this screams error:

auto tmpTrampoline = (uint64_t) new uint8_t[m_trampolineSz];

MemoryProtector prot(m_trampoline, m_trampolineSz, ProtFlag::R | ProtFlag::W | ProtFlag::X, *this, false);

Similarly on x86:

m_trampoline = (uint64_t) new unsigned char[m_trampolineSz];

MemoryProtector prot(m_trampoline, m_trampolineSz, ProtFlag::R | ProtFlag::W | ProtFlag::X, *this, false);

We are changing protection on memory we dont own.
This has unwanted side effects because it shares pages with other allocs, and may race with other threads or other detours that are done at the same time.

Solution to this is to replace any memory new/delete that has its protection mutated with VirtualAlloc/VirtualFree, given those require a full page at minimum, giving us absolute ownership.
At that point we dont need MemoryProtector there anymore, which also fixes the trampoline losing execute permission at the end of scope

And given these are full pages, they should be cached/reused if possible so not every trampoline requires its own page.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions