Skip to content

Commit 000cc85

Browse files
committed
Improve parent process check in CEFLauncher
1 parent b4f70cc commit 000cc85

File tree

1 file changed

+59
-10
lines changed

1 file changed

+59
-10
lines changed

Client/ceflauncher_DLL/Main.cpp

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@
99
*
1010
*****************************************************************************/
1111

12+
#define WIN32_NO_STATUS
1213
#define WIN32_LEAN_AND_MEAN
1314
#include <Windows.h>
15+
#undef WIN32_NO_STATUS
16+
#include <ntstatus.h>
17+
#include <winnt.h>
18+
#include <winternl.h>
1419
#include <delayimp.h>
1520
#include "CCefApp.h"
1621
#include <string>
@@ -21,6 +26,8 @@
2126
#pragma comment(lib, "cef_sandbox.lib")
2227
#endif
2328

29+
DWORD WINAPI CheckParentProcessAliveness(LPVOID);
30+
2431
int _declspec(dllexport) InitCEF()
2532
{
2633
// Get absolute CEFLauncher.exe path
@@ -46,20 +53,62 @@ int _declspec(dllexport) InitCEF()
4653
sandboxInfo = scopedSandbox.sandbox_info();
4754
#endif
4855

49-
if (HANDLE job = CreateJobObjectW(nullptr, nullptr); job != nullptr)
56+
const HANDLE parentCheckThread = CreateThread(nullptr, 0, CheckParentProcessAliveness, nullptr, 0, nullptr);
57+
58+
const int exitCode = CefExecuteProcess(mainArgs, app, sandboxInfo);
59+
60+
if (parentCheckThread != nullptr)
5061
{
51-
JOBOBJECT_EXTENDED_LIMIT_INFORMATION limits{};
52-
limits.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
62+
TerminateThread(parentCheckThread, 0);
63+
CloseHandle(parentCheckThread);
64+
}
5365

54-
if (SetInformationJobObject(job, JobObjectExtendedLimitInformation, &limits, sizeof(limits)))
55-
{
56-
AssignProcessToJobObject(job, GetCurrentProcess());
57-
}
58-
else
66+
return exitCode;
67+
}
68+
69+
static DWORD WINAPI CheckParentProcessAliveness(LPVOID)
70+
{
71+
NTSTATUS(NTAPI * queryInformation)(HANDLE, PROCESSINFOCLASS, PVOID, ULONG, PULONG) = nullptr;
72+
73+
if (HMODULE const ntdll = GetModuleHandleW(L"ntdll.dll"); ntdll != nullptr)
74+
{
75+
queryInformation = reinterpret_cast<decltype(queryInformation)>(GetProcAddress(ntdll, "NtQueryInformationProcess"));
76+
}
77+
78+
if (queryInformation == nullptr)
79+
return 1;
80+
81+
PROCESS_BASIC_INFORMATION info{};
82+
83+
ULONG returnLength = 0;
84+
NTSTATUS status = queryInformation(GetCurrentProcess(), ProcessBasicInformation, &info, sizeof(info), &returnLength);
85+
86+
if (!NT_SUCCESS(status) || returnLength < sizeof(PROCESS_BASIC_INFORMATION))
87+
return 2;
88+
89+
const auto parentProcessId = static_cast<DWORD>(reinterpret_cast<ULONG_PTR>(info.Reserved3));
90+
const HANDLE parentProcess = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION, FALSE, parentProcessId);
91+
92+
if (parentProcess == nullptr)
93+
{
94+
if (GetLastError() == ERROR_INVALID_PARAMETER)
95+
ExitProcess(0);
96+
97+
return 3;
98+
}
99+
100+
while (true)
101+
{
102+
DWORD exitCode{};
103+
104+
if (!GetExitCodeProcess(parentProcess, &exitCode) || exitCode != STILL_ACTIVE)
59105
{
60-
CloseHandle(job);
106+
CloseHandle(parentProcess);
107+
ExitProcess(exitCode);
61108
}
109+
110+
Sleep(1000);
62111
}
63112

64-
return CefExecuteProcess(mainArgs, app, sandboxInfo);
113+
return 0;
65114
}

0 commit comments

Comments
 (0)