From 75ff4ece7de8c10838f9fab48abfd72819ba96fa Mon Sep 17 00:00:00 2001 From: Alan Jowett Date: Fri, 10 Jul 2026 15:42:55 -0700 Subject: [PATCH] Add usersim ring mapping helpers Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: e4f2928d-308e-4fec-8aa3-280de1761d0c Signed-off-by: Alan Jowett --- inc/usersim/mm.h | 19 +++++++++++++++++++ inc/usersim/ob.h | 10 ++++++++++ src/mm.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/ob.cpp | 23 +++++++++++++++++++++++ 4 files changed, 97 insertions(+) diff --git a/inc/usersim/mm.h b/inc/usersim/mm.h index 2e6b2ba..77de71d 100644 --- a/inc/usersim/mm.h +++ b/inc/usersim/mm.h @@ -113,6 +113,25 @@ USERSIM_API void MmUnmapLockedPages(_In_ void* base_address, _In_ MDL* memory_descriptor_list); +USERSIM_API +void +MmProbeAndLockPages( + _Inout_ MDL* memory_descriptor_list, + __drv_strictType(KPROCESSOR_MODE / enum _MODE, __drv_typeConst) KPROCESSOR_MODE access_mode, + ULONG operation); + +USERSIM_API +void +MmUnlockPages(_Inout_ MDL* memory_descriptor_list); + +USERSIM_API +NTSTATUS +MmMapViewInSystemSpace(_In_ void* section, _Outptr_ void** mapped_base, _Inout_ SIZE_T* view_size); + +USERSIM_API +NTSTATUS +MmUnmapViewInSystemSpace(_In_ void* mapped_base); + USERSIM_API NTSTATUS MmProtectMdlSystemAddress(_In_ MDL* memory_descriptor_list, ULONG new_protect); diff --git a/inc/usersim/ob.h b/inc/usersim/ob.h index 29df01b..7d4dabd 100644 --- a/inc/usersim/ob.h +++ b/inc/usersim/ob.h @@ -28,6 +28,16 @@ _IRQL_requires_max_(PASSIVE_LEVEL) USERSIM_API NTSTATUS ObReferenceObjectByHandl _Out_ PVOID* object, _Out_opt_ POBJECT_HANDLE_INFORMATION handle_information); +_IRQL_requires_max_(PASSIVE_LEVEL) USERSIM_API NTSTATUS +ObOpenObjectByPointer( + _In_ PVOID object, + _In_ ULONG handle_attributes, + _In_opt_ void* passed_access_state, + _In_ ACCESS_MASK desired_access, + _In_opt_ POBJECT_TYPE object_type, + _In_ KPROCESSOR_MODE access_mode, + _Out_ HANDLE* handle); + USERSIM_API NTSTATUS ObCloseHandle(_In_ _Post_ptr_invalid_ HANDLE handle, _In_ KPROCESSOR_MODE previous_mode); diff --git a/src/mm.cpp b/src/mm.cpp index d84da98..670823c 100644 --- a/src/mm.cpp +++ b/src/mm.cpp @@ -8,6 +8,7 @@ #include "usersim/ex.h" #include "usersim/ke.h" #include "usersim/mm.h" +#include "utilities.h" // Mm* functions. @@ -132,6 +133,50 @@ MmUnmapLockedPages(_In_ void* base_address, _In_ MDL* memory_descriptor_list) return MmUnmapLockedPagesCPP(base_address, memory_descriptor_list); } +void +MmProbeAndLockPages( + _Inout_ MDL* memory_descriptor_list, + __drv_strictType(KPROCESSOR_MODE / enum _MODE, __drv_typeConst) KPROCESSOR_MODE access_mode, + ULONG operation) +{ + UNREFERENCED_PARAMETER(memory_descriptor_list); + UNREFERENCED_PARAMETER(access_mode); + UNREFERENCED_PARAMETER(operation); +} + +void +MmUnlockPages(_Inout_ MDL* memory_descriptor_list) +{ + UNREFERENCED_PARAMETER(memory_descriptor_list); +} + +NTSTATUS +MmMapViewInSystemSpace(_In_ void* section, _Outptr_ void** mapped_base, _Inout_ SIZE_T* view_size) +{ + HANDLE section_handle = (HANDLE)section; + void* view; + + view = MapViewOfFile(section_handle, FILE_MAP_ALL_ACCESS, 0, 0, *view_size); + if (view == nullptr) { + USERSIM_LOG_WIN32_API_FAILURE(USERSIM_TRACELOG_KEYWORD_BASE, MapViewOfFile); + return win32_error_to_usersim_error(GetLastError()); + } + + *mapped_base = view; + return STATUS_SUCCESS; +} + +NTSTATUS +MmUnmapViewInSystemSpace(_In_ void* mapped_base) +{ + if (!UnmapViewOfFile(mapped_base)) { + USERSIM_LOG_WIN32_API_FAILURE(USERSIM_TRACELOG_KEYWORD_BASE, UnmapViewOfFile); + return win32_error_to_usersim_error(GetLastError()); + } + + return STATUS_SUCCESS; +} + NTSTATUS MmProtectMdlSystemAddress(_In_ MDL* memory_descriptor_list, ULONG new_protect) { diff --git a/src/ob.cpp b/src/ob.cpp index 0f27781..18e31f3 100644 --- a/src/ob.cpp +++ b/src/ob.cpp @@ -4,6 +4,7 @@ #include "platform.h" #include "kernel_um.h" #include "usersim/ob.h" +#include "utilities.h" #include static std::map _object_references; @@ -56,6 +57,28 @@ ObReferenceObjectByHandle( return STATUS_SUCCESS; } +_IRQL_requires_max_(PASSIVE_LEVEL) USERSIM_API NTSTATUS +ObOpenObjectByPointer( + _In_ PVOID object, + _In_ ULONG handle_attributes, + _In_opt_ void* passed_access_state, + _In_ ACCESS_MASK desired_access, + _In_opt_ POBJECT_TYPE object_type, + _In_ KPROCESSOR_MODE access_mode, + _Out_ HANDLE* handle) +{ + UNREFERENCED_PARAMETER(handle_attributes); + UNREFERENCED_PARAMETER(passed_access_state); + UNREFERENCED_PARAMETER(object_type); + UNREFERENCED_PARAMETER(access_mode); + + if (!DuplicateHandle(GetCurrentProcess(), (HANDLE)object, GetCurrentProcess(), handle, desired_access, FALSE, 0)) { + return win32_error_to_usersim_error(GetLastError()); + } + + return STATUS_SUCCESS; +} + USERSIM_API NTSTATUS ObCloseHandle(_In_ _Post_ptr_invalid_ HANDLE handle, _In_ KPROCESSOR_MODE previous_mode)