From 625d3759351d5dde325fdd70ebde703cc7d6e226 Mon Sep 17 00:00:00 2001 From: Vincent Date: Mon, 22 Jul 2024 00:36:57 +0100 Subject: [PATCH] feat: add validation for runtime loader in Windows bootstrap --- python/src/endstone/_internal/bootstrap/windows.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/python/src/endstone/_internal/bootstrap/windows.py b/python/src/endstone/_internal/bootstrap/windows.py index 105cab070..205b9ea95 100644 --- a/python/src/endstone/_internal/bootstrap/windows.py +++ b/python/src/endstone/_internal/bootstrap/windows.py @@ -45,6 +45,7 @@ class THREADENTRY32(ctypes.Structure): TH32CS_SNAPTHREAD = 0x00000004 THREAD_ALL_ACCESS = 0x000F0000 | 0x00100000 | 0xFFFF INVALID_HANDLE_VALUE = ctypes.c_void_p(-1).value +DONT_RESOLVE_DLL_REFERENCES = 0x00000001 # https://learn.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-virtualallocex VirtualAllocEx = kernel32.VirtualAllocEx @@ -76,6 +77,11 @@ class THREADENTRY32(ctypes.Structure): GetProcAddress.restype = LPVOID GetProcAddress.argtypes = (HMODULE, LPCSTR) +# https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexw +LoadLibraryExW = kernel32.LoadLibraryExW +LoadLibraryExW.restype = HMODULE +LoadLibraryExW.argtypes = (LPCWSTR, HANDLE, DWORD) + class WindowsBootstrap(Bootstrap): @property @@ -125,6 +131,11 @@ def _create_process(self, *args, **kwargs) -> None: handle_proc = int(self._process._handle) lib_path = str(self._endstone_runtime_path.absolute()) + # Validate dll + dll = kernel32.LoadLibraryExW(lib_path, None, DONT_RESOLVE_DLL_REFERENCES) + if not dll: + raise ValueError(f"LoadLibraryExW failed with error {get_last_error()}.") + # Allocate memory for lib_path address = kernel32.VirtualAllocEx( handle_proc, # hProcess