Skip to content

Commit

Permalink
introduces DetourUpdateAllOtherThreads
Browse files Browse the repository at this point in the history
  • Loading branch information
adams85 committed Apr 10, 2022
1 parent 3326512 commit b13666a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/detours.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1524,6 +1524,24 @@ struct DetourThread
{
DetourThread * pNext;
HANDLE hThread;
BOOL fCloseThreadHandleOnDestroy;

DetourThread() {
pNext = NULL;
hThread = NULL;
fCloseThreadHandleOnDestroy = FALSE;
}

~DetourThread()
{
if (hThread) {
if (fCloseThreadHandleOnDestroy) {
CloseHandle(hThread);
}

hThread = NULL;
}
}
};

struct DetourOperation
Expand Down Expand Up @@ -1946,6 +1964,11 @@ typedef ULONG_PTR DETOURS_EIP_TYPE;
}

LONG WINAPI DetourUpdateThread(_In_ HANDLE hThread)
{
return DetourUpdateThreadEx(hThread, FALSE);
}

LONG WINAPI DetourUpdateThreadEx(_In_ HANDLE hThread, _In_ BOOL fCloseThreadHandleOnDestroy)
{
LONG error;

Expand Down Expand Up @@ -1980,12 +2003,57 @@ LONG WINAPI DetourUpdateThread(_In_ HANDLE hThread)
}

t->hThread = hThread;
t->fCloseThreadHandleOnDestroy = fCloseThreadHandleOnDestroy;
t->pNext = s_pPendingThreads;
s_pPendingThreads = t;

return NO_ERROR;
}

#ifndef NT_SUCCESS
#define NT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0)
#endif

#define STATUS_NO_MORE_ENTRIES 0x8000001A

typedef NTSTATUS(NTAPI *_NtGetNextThread)(
_In_ HANDLE ProcessHandle,
_In_ HANDLE ThreadHandle,
_In_ ACCESS_MASK DesiredAccess,
_In_ ULONG HandleAttributes,
_In_ ULONG Flags,
_Out_ PHANDLE NewThreadHandle
);

LONG WINAPI DetourUpdateAllOtherThreads()
{
_NtGetNextThread NtGetNextThread = (_NtGetNextThread)GetProcAddress(GetModuleHandle(TEXT("ntdll.dll")), "NtGetNextThread");
if (!NtGetNextThread) {
DETOUR_TRACE("Failed to determine NtGetNextThread address.\r\n");
return GetLastError();
}

DWORD currentThreadId = GetCurrentThreadId();

HANDLE hThread = NULL;
for (;;) {
NTSTATUS status = NtGetNextThread(GetCurrentProcess(), hThread, THREAD_QUERY_LIMITED_INFORMATION | THREAD_SUSPEND_RESUME, 0, 0, &hThread);

if (!NT_SUCCESS(status)) {
if (status != STATUS_NO_MORE_ENTRIES) {
DETOUR_TRACE("Failed to enumerate process threads.\r\n");
return ERROR_FUNCTION_FAILED;
}

return NO_ERROR;
}

if (currentThreadId != GetThreadId(hThread)) {
DetourUpdateThreadEx(hThread, TRUE);
}
}
}

///////////////////////////////////////////////////////////// Transacted APIs.
//
LONG WINAPI DetourAttach(_Inout_ PVOID *ppPointer,
Expand Down
2 changes: 2 additions & 0 deletions src/detours.h
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,8 @@ LONG WINAPI DetourTransactionCommit(VOID);
LONG WINAPI DetourTransactionCommitEx(_Out_opt_ PVOID **pppFailedPointer);

LONG WINAPI DetourUpdateThread(_In_ HANDLE hThread);
LONG WINAPI DetourUpdateThreadEx(_In_ HANDLE hThread, _In_ BOOL fCloseThreadHandleOnDestroy);
LONG WINAPI DetourUpdateAllOtherThreads();

LONG WINAPI DetourAttach(_Inout_ PVOID *ppPointer,
_In_ PVOID pDetour);
Expand Down

0 comments on commit b13666a

Please sign in to comment.