Replies: 2 comments 12 replies
-
Yes indeed, this is an epic puzzle. As far as I know, the most safe way in practice is never unhook and unload, let detour function do nothing instead: int MessageBoxW_Hook(...) {
if (g_Enable) { ... }
int result = MessageBoxW_Original(...);
if (g_Enable) { ... }
return result;
} I thought about this problem before and thought of the same solution as you 🤷♂️. And this solution will replace the return address in |
Beta Was this translation helpful? Give feedback.
-
Now that I think about it, if stack walking is reliable (I'm not sure it is in all platforms), maybe it's enough to have the following: bool IsSafeToUnload(HMODULE* modulesWithHooks, ULONG modulesWithHooksCount) {
bool safeToUnload = true;
SuspendThreads();
// Iterate all threads but the current one.
for (auto thread : threads) {
if (IsRipInTrampoline(thread->rip)) {
safeToUnload = false;
goto done;
}
for (auto address : GetCallStack(thread)) {
for (HMODULE module : {modulesWithHooks, modulesWithHooksCount}) {
if (AddressInModule(address, module)) {
safeToUnload = false;
goto done;
}
}
}
}
done:
ResumeThreads();
return safeToUnload;
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Let's say I load a dll which hooks MessageBoxW. At some point, I need to unload the dll (e.g. to exit or update). SlimDetours offers no way of knowing when it's safe do to so. Given the chain of MessageBoxW calls:
If a thread is running in any of the 1-6 stages, unloading the dll will cause a crash.
One way the library could mitigate this is to use a reference counter, something like:
But it puts the burden on the library user. It's also not a fully correct solution, as a thread's instruction pointer could be just before InterlockedIncrement or just after InterlockedDecrement.
One solution I thought of is to perpend code similar to the following to rbCodeIn (pseudo-code):
Then add a new function as follows:
But it's not a quick fix. Let me know if you have better ideas.
Beta Was this translation helpful? Give feedback.
All reactions