Skip to content

Commit b471f95

Browse files
committed
Prevent recursive calls in CreateProcess hooks
* In one case the kernel32.dll version called into an API set version, and we don't want to hook twice.
1 parent ecdbfc9 commit b471f95

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

renderdoc/os/win32/sys_win32_hooks.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ class SysHook : LibraryHook
131131
// we start with a refcount of 1 because we initialise WSA ourselves for our own sockets.
132132
m_WSARefCount = 1;
133133

134+
m_RecurseSlot = Threading::AllocateTLSSlot();
135+
Threading::SetTLSValue(m_RecurseSlot, NULL);
136+
134137
if(!success)
135138
return false;
136139

@@ -149,7 +152,19 @@ class SysHook : LibraryHook
149152
bool m_EnabledHooks;
150153

151154
int m_WSARefCount;
155+
uint64_t m_RecurseSlot = 0;
156+
157+
bool CheckRecurse()
158+
{
159+
if(Threading::GetTLSValue(m_RecurseSlot) == NULL)
160+
{
161+
Threading::SetTLSValue(m_RecurseSlot, (void *)1);
162+
return false;
163+
}
152164

165+
return true;
166+
}
167+
void EndRecurse() { Threading::SetTLSValue(m_RecurseSlot, NULL); }
153168
Hook<PFN_CREATE_PROCESS_A> CreateProcessA;
154169
Hook<PFN_CREATE_PROCESS_W> CreateProcessW;
155170

@@ -203,6 +218,11 @@ class SysHook : LibraryHook
203218
std::function<BOOL(DWORD dwCreationFlags, LPPROCESS_INFORMATION lpProcessInformation)> realFunc,
204219
DWORD dwCreationFlags, bool inject, LPPROCESS_INFORMATION lpProcessInformation)
205220
{
221+
bool recursive = syshooks.CheckRecurse();
222+
223+
if(recursive)
224+
return realFunc(dwCreationFlags, lpProcessInformation);
225+
206226
PROCESS_INFORMATION dummy;
207227
RDCEraseEl(dummy);
208228

@@ -219,7 +239,9 @@ class SysHook : LibraryHook
219239
bool resume = (dwCreationFlags & CREATE_SUSPENDED) == 0;
220240
dwCreationFlags |= CREATE_SUSPENDED;
221241

242+
RDCDEBUG("Calling real %s", entryPoint);
222243
BOOL ret = realFunc(dwCreationFlags, lpProcessInformation);
244+
RDCDEBUG("Called real %s", entryPoint);
223245

224246
if(ret && inject)
225247
{
@@ -247,6 +269,8 @@ class SysHook : LibraryHook
247269
CloseHandle(dummy.hThread);
248270
}
249271

272+
syshooks.EndRecurse();
273+
250274
return ret;
251275
}
252276

0 commit comments

Comments
 (0)