Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GetReturnAddress call does not preserve 16-byte stack alignment for Linux #1997

Open
qubka opened this issue Apr 27, 2023 · 1 comment
Open

Comments

@qubka
Copy link

qubka commented Apr 27, 2023

Description

Not so long ago, I delved into the DynamicHooks/DHook library in order to understand its inner workings. After thoroughly examining the library, I decided to create my own version that would be compatible with both x64 and x32 and would include the ability to work with Virtual Hook.

As I was working on this project, I encountered a small issue on 32bit. While it may not be a serious problem, I noticed that calling GetReturnAddress does not preserve 16-byte stack alignment for Linux, when HookHandler call took this into account.

Problematic Code

// Get the original return address
void* (__cdecl CHook::*GetReturnAddress)(void*) = &CHook::GetReturnAddress;
masm.push(esp);
masm.push(intptr_t(this));
masm.call(ExternalAddress((void *&)GetReturnAddress));
masm.addl(esp, 8);

->

// Get the original return address
void* (__cdecl CHook::*GetReturnAddress)(void*) = &CHook::GetReturnAddress;
masm.movl(eax, esp);
masm.subl(esp, 4);
masm.push(eax);
masm.push(intptr_t(this));
masm.call(ExternalAddress((void *&)GetReturnAddress));
masm.addl(esp, 12);
@sapphonie
Copy link
Contributor

vaguely related to #1849

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants