mirror of
https://github.com/decompals/wibo.git
synced 2025-10-16 15:15:10 +00:00
Fix OpenProcessToken pseudo-handle regression
This commit is contained in:
parent
0d76e541c1
commit
ff04eb9f41
@ -5,6 +5,7 @@
|
|||||||
#include "errors.h"
|
#include "errors.h"
|
||||||
#include "handles.h"
|
#include "handles.h"
|
||||||
#include "internal.h"
|
#include "internal.h"
|
||||||
|
#include "kernel32/internal.h"
|
||||||
#include "processes.h"
|
#include "processes.h"
|
||||||
|
|
||||||
namespace advapi32 {
|
namespace advapi32 {
|
||||||
@ -16,7 +17,12 @@ BOOL WIN_FUNC OpenProcessToken(HANDLE ProcessHandle, DWORD DesiredAccess, PHANDL
|
|||||||
wibo::lastError = ERROR_INVALID_PARAMETER;
|
wibo::lastError = ERROR_INVALID_PARAMETER;
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
auto obj = wibo::handles().getAs<ProcessObject>(ProcessHandle);
|
Pin<ProcessObject> obj;
|
||||||
|
if (kernel32::isPseudoCurrentProcessHandle(ProcessHandle)) {
|
||||||
|
obj = make_pin<ProcessObject>(getpid(), -1);
|
||||||
|
} else {
|
||||||
|
obj = wibo::handles().getAs<ProcessObject>(ProcessHandle);
|
||||||
|
}
|
||||||
if (!obj) {
|
if (!obj) {
|
||||||
wibo::lastError = ERROR_INVALID_HANDLE;
|
wibo::lastError = ERROR_INVALID_HANDLE;
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
@ -23,7 +23,7 @@ BOOL WIN_FUNC DuplicateHandle(HANDLE hSourceProcessHandle, HANDLE hSourceHandle,
|
|||||||
}
|
}
|
||||||
|
|
||||||
auto validateProcessHandle = [&](HANDLE handle) -> bool {
|
auto validateProcessHandle = [&](HANDLE handle) -> bool {
|
||||||
if (reinterpret_cast<uintptr_t>(handle) == kPseudoCurrentProcessHandleValue) {
|
if (isPseudoCurrentProcessHandle(handle)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
auto proc = wibo::handles().getAs<ProcessObject>(handle);
|
auto proc = wibo::handles().getAs<ProcessObject>(handle);
|
||||||
@ -37,15 +37,14 @@ BOOL WIN_FUNC DuplicateHandle(HANDLE hSourceProcessHandle, HANDLE hSourceHandle,
|
|||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
uintptr_t sourceHandleRaw = reinterpret_cast<uintptr_t>(hSourceHandle);
|
if (isPseudoCurrentProcessHandle(hSourceHandle)) {
|
||||||
if (sourceHandleRaw == kPseudoCurrentProcessHandleValue) {
|
|
||||||
auto po = make_pin<ProcessObject>(getpid(), -1);
|
auto po = make_pin<ProcessObject>(getpid(), -1);
|
||||||
auto handle = wibo::handles().alloc(std::move(po), 0, 0);
|
auto handle = wibo::handles().alloc(std::move(po), 0, 0);
|
||||||
DEBUG_LOG("DuplicateHandle: created process handle for current process -> %p\n", handle);
|
DEBUG_LOG("DuplicateHandle: created process handle for current process -> %p\n", handle);
|
||||||
*lpTargetHandle = handle;
|
*lpTargetHandle = handle;
|
||||||
wibo::lastError = ERROR_SUCCESS;
|
wibo::lastError = ERROR_SUCCESS;
|
||||||
return TRUE;
|
return TRUE;
|
||||||
} else if (sourceHandleRaw == kPseudoCurrentThreadHandleValue) {
|
} else if (isPseudoCurrentThreadHandle(hSourceHandle)) {
|
||||||
auto th = make_pin<ThreadObject>(pthread_self());
|
auto th = make_pin<ThreadObject>(pthread_self());
|
||||||
auto handle = wibo::handles().alloc(std::move(th), 0, 0);
|
auto handle = wibo::handles().alloc(std::move(th), 0, 0);
|
||||||
DEBUG_LOG("DuplicateHandle: created thread handle for current thread -> %p\n", handle);
|
DEBUG_LOG("DuplicateHandle: created thread handle for current thread -> %p\n", handle);
|
||||||
|
@ -166,6 +166,16 @@ struct HeapObject : public ObjectBase {
|
|||||||
inline constexpr uintptr_t kPseudoCurrentProcessHandleValue = static_cast<uintptr_t>(-1);
|
inline constexpr uintptr_t kPseudoCurrentProcessHandleValue = static_cast<uintptr_t>(-1);
|
||||||
inline constexpr uintptr_t kPseudoCurrentThreadHandleValue = static_cast<uintptr_t>(-2);
|
inline constexpr uintptr_t kPseudoCurrentThreadHandleValue = static_cast<uintptr_t>(-2);
|
||||||
|
|
||||||
|
inline bool isPseudoCurrentProcessHandle(HANDLE h) {
|
||||||
|
uintptr_t rawHandle = reinterpret_cast<uintptr_t>(h);
|
||||||
|
return rawHandle == kPseudoCurrentProcessHandleValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool isPseudoCurrentThreadHandle(HANDLE h) {
|
||||||
|
uintptr_t rawHandle = reinterpret_cast<uintptr_t>(h);
|
||||||
|
return rawHandle == kPseudoCurrentThreadHandleValue;
|
||||||
|
}
|
||||||
|
|
||||||
void tryMarkExecutable(void *mem);
|
void tryMarkExecutable(void *mem);
|
||||||
void setLastErrorFromErrno();
|
void setLastErrorFromErrno();
|
||||||
|
|
||||||
|
@ -175,11 +175,6 @@ void *threadTrampoline(void *param) {
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool isPseudoCurrentThreadHandle(HANDLE h) {
|
|
||||||
uintptr_t rawHandle = reinterpret_cast<uintptr_t>(h);
|
|
||||||
return rawHandle == kernel32::kPseudoCurrentThreadHandleValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
namespace kernel32 {
|
namespace kernel32 {
|
||||||
@ -237,9 +232,7 @@ BOOL WIN_FUNC GetProcessAffinityMask(HANDLE hProcess, PDWORD_PTR lpProcessAffini
|
|||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
uintptr_t rawHandle = reinterpret_cast<uintptr_t>(hProcess);
|
if (!isPseudoCurrentProcessHandle(hProcess)) {
|
||||||
bool isPseudoHandle = rawHandle == 0 || rawHandle == kPseudoCurrentProcessHandleValue;
|
|
||||||
if (!isPseudoHandle) {
|
|
||||||
auto obj = wibo::handles().getAs<ProcessObject>(hProcess);
|
auto obj = wibo::handles().getAs<ProcessObject>(hProcess);
|
||||||
if (!obj) {
|
if (!obj) {
|
||||||
wibo::lastError = ERROR_INVALID_HANDLE;
|
wibo::lastError = ERROR_INVALID_HANDLE;
|
||||||
@ -271,9 +264,7 @@ BOOL WIN_FUNC SetProcessAffinityMask(HANDLE hProcess, DWORD_PTR dwProcessAffinit
|
|||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
uintptr_t rawHandle = reinterpret_cast<uintptr_t>(hProcess);
|
if (!isPseudoCurrentProcessHandle(hProcess)) {
|
||||||
bool isPseudoHandle = rawHandle == 0 || rawHandle == kPseudoCurrentProcessHandleValue;
|
|
||||||
if (!isPseudoHandle) {
|
|
||||||
auto obj = wibo::handles().getAs<ProcessObject>(hProcess);
|
auto obj = wibo::handles().getAs<ProcessObject>(hProcess);
|
||||||
if (!obj) {
|
if (!obj) {
|
||||||
wibo::lastError = ERROR_INVALID_HANDLE;
|
wibo::lastError = ERROR_INVALID_HANDLE;
|
||||||
@ -332,7 +323,7 @@ void WIN_FUNC ExitProcess(UINT uExitCode) {
|
|||||||
BOOL WIN_FUNC TerminateProcess(HANDLE hProcess, UINT uExitCode) {
|
BOOL WIN_FUNC TerminateProcess(HANDLE hProcess, UINT uExitCode) {
|
||||||
HOST_CONTEXT_GUARD();
|
HOST_CONTEXT_GUARD();
|
||||||
DEBUG_LOG("TerminateProcess(%p, %u)\n", hProcess, uExitCode);
|
DEBUG_LOG("TerminateProcess(%p, %u)\n", hProcess, uExitCode);
|
||||||
if (hProcess == reinterpret_cast<HANDLE>(static_cast<uintptr_t>(-1))) {
|
if (isPseudoCurrentProcessHandle(hProcess)) {
|
||||||
exit(static_cast<int>(uExitCode));
|
exit(static_cast<int>(uExitCode));
|
||||||
}
|
}
|
||||||
auto process = wibo::handles().getAs<ProcessObject>(hProcess);
|
auto process = wibo::handles().getAs<ProcessObject>(hProcess);
|
||||||
@ -372,6 +363,11 @@ BOOL WIN_FUNC GetExitCodeProcess(HANDLE hProcess, LPDWORD lpExitCode) {
|
|||||||
wibo::lastError = ERROR_INVALID_PARAMETER;
|
wibo::lastError = ERROR_INVALID_PARAMETER;
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
if (isPseudoCurrentProcessHandle(hProcess)) {
|
||||||
|
*lpExitCode = STILL_ACTIVE;
|
||||||
|
wibo::lastError = ERROR_SUCCESS;
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
auto process = wibo::handles().getAs<ProcessObject>(hProcess);
|
auto process = wibo::handles().getAs<ProcessObject>(hProcess);
|
||||||
if (!process) {
|
if (!process) {
|
||||||
wibo::lastError = ERROR_INVALID_HANDLE;
|
wibo::lastError = ERROR_INVALID_HANDLE;
|
||||||
@ -599,9 +595,7 @@ BOOL WIN_FUNC GetThreadTimes(HANDLE hThread, FILETIME *lpCreationTime, FILETIME
|
|||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isPseudoCurrentThread = reinterpret_cast<uintptr_t>(hThread) == kernel32::kPseudoCurrentThreadHandleValue ||
|
if (!isPseudoCurrentThreadHandle(hThread)) {
|
||||||
hThread == nullptr || hThread == reinterpret_cast<HANDLE>(static_cast<uintptr_t>(-1));
|
|
||||||
if (!isPseudoCurrentThread) {
|
|
||||||
DEBUG_LOG("GetThreadTimes: unsupported handle %p\n", hThread);
|
DEBUG_LOG("GetThreadTimes: unsupported handle %p\n", hThread);
|
||||||
wibo::lastError = ERROR_INVALID_HANDLE;
|
wibo::lastError = ERROR_INVALID_HANDLE;
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
@ -34,9 +34,7 @@ BOOL WIN_FUNC IsWow64Process(HANDLE hProcess, PBOOL Wow64Process) {
|
|||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
uintptr_t rawHandle = reinterpret_cast<uintptr_t>(hProcess);
|
if (!isPseudoCurrentProcessHandle(hProcess)) {
|
||||||
bool isPseudoHandle = rawHandle == kPseudoCurrentProcessHandleValue;
|
|
||||||
if (!isPseudoHandle) {
|
|
||||||
if (!hProcess) {
|
if (!hProcess) {
|
||||||
wibo::lastError = ERROR_INVALID_HANDLE;
|
wibo::lastError = ERROR_INVALID_HANDLE;
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
@ -78,8 +78,7 @@ constexpr ULONG kOsPlatformId = 2; // VER_PLATFORM_WIN32_NT
|
|||||||
constexpr BYTE kProductTypeWorkstation = 1; // VER_NT_WORKSTATION
|
constexpr BYTE kProductTypeWorkstation = 1; // VER_NT_WORKSTATION
|
||||||
|
|
||||||
bool resolveProcessDetails(HANDLE processHandle, ProcessHandleDetails &details) {
|
bool resolveProcessDetails(HANDLE processHandle, ProcessHandleDetails &details) {
|
||||||
uintptr_t rawHandle = reinterpret_cast<uintptr_t>(processHandle);
|
if (kernel32::isPseudoCurrentProcessHandle(processHandle)) {
|
||||||
if (rawHandle == static_cast<uintptr_t>(-1)) {
|
|
||||||
details.pid = getpid();
|
details.pid = getpid();
|
||||||
details.exitCode = STILL_ACTIVE;
|
details.exitCode = STILL_ACTIVE;
|
||||||
details.peb = wibo::processPeb;
|
details.peb = wibo::processPeb;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user