Skip to content

Commit

Permalink
feat: add validation for runtime loader in Windows bootstrap
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Jul 21, 2024
1 parent d267cbf commit 625d375
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions python/src/endstone/_internal/bootstrap/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 625d375

Please sign in to comment.