Skip to content

Commit 75ff4ec

Browse files
committed
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 <alanjo@microsoft.com>
1 parent c0523d0 commit 75ff4ec

4 files changed

Lines changed: 97 additions & 0 deletions

File tree

inc/usersim/mm.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,25 @@ USERSIM_API
113113
void
114114
MmUnmapLockedPages(_In_ void* base_address, _In_ MDL* memory_descriptor_list);
115115

116+
USERSIM_API
117+
void
118+
MmProbeAndLockPages(
119+
_Inout_ MDL* memory_descriptor_list,
120+
__drv_strictType(KPROCESSOR_MODE / enum _MODE, __drv_typeConst) KPROCESSOR_MODE access_mode,
121+
ULONG operation);
122+
123+
USERSIM_API
124+
void
125+
MmUnlockPages(_Inout_ MDL* memory_descriptor_list);
126+
127+
USERSIM_API
128+
NTSTATUS
129+
MmMapViewInSystemSpace(_In_ void* section, _Outptr_ void** mapped_base, _Inout_ SIZE_T* view_size);
130+
131+
USERSIM_API
132+
NTSTATUS
133+
MmUnmapViewInSystemSpace(_In_ void* mapped_base);
134+
116135
USERSIM_API
117136
NTSTATUS
118137
MmProtectMdlSystemAddress(_In_ MDL* memory_descriptor_list, ULONG new_protect);

inc/usersim/ob.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ _IRQL_requires_max_(PASSIVE_LEVEL) USERSIM_API NTSTATUS ObReferenceObjectByHandl
2828
_Out_ PVOID* object,
2929
_Out_opt_ POBJECT_HANDLE_INFORMATION handle_information);
3030

31+
_IRQL_requires_max_(PASSIVE_LEVEL) USERSIM_API NTSTATUS
32+
ObOpenObjectByPointer(
33+
_In_ PVOID object,
34+
_In_ ULONG handle_attributes,
35+
_In_opt_ void* passed_access_state,
36+
_In_ ACCESS_MASK desired_access,
37+
_In_opt_ POBJECT_TYPE object_type,
38+
_In_ KPROCESSOR_MODE access_mode,
39+
_Out_ HANDLE* handle);
40+
3141
USERSIM_API
3242
NTSTATUS
3343
ObCloseHandle(_In_ _Post_ptr_invalid_ HANDLE handle, _In_ KPROCESSOR_MODE previous_mode);

src/mm.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "usersim/ex.h"
99
#include "usersim/ke.h"
1010
#include "usersim/mm.h"
11+
#include "utilities.h"
1112

1213
// Mm* functions.
1314

@@ -132,6 +133,50 @@ MmUnmapLockedPages(_In_ void* base_address, _In_ MDL* memory_descriptor_list)
132133
return MmUnmapLockedPagesCPP(base_address, memory_descriptor_list);
133134
}
134135

136+
void
137+
MmProbeAndLockPages(
138+
_Inout_ MDL* memory_descriptor_list,
139+
__drv_strictType(KPROCESSOR_MODE / enum _MODE, __drv_typeConst) KPROCESSOR_MODE access_mode,
140+
ULONG operation)
141+
{
142+
UNREFERENCED_PARAMETER(memory_descriptor_list);
143+
UNREFERENCED_PARAMETER(access_mode);
144+
UNREFERENCED_PARAMETER(operation);
145+
}
146+
147+
void
148+
MmUnlockPages(_Inout_ MDL* memory_descriptor_list)
149+
{
150+
UNREFERENCED_PARAMETER(memory_descriptor_list);
151+
}
152+
153+
NTSTATUS
154+
MmMapViewInSystemSpace(_In_ void* section, _Outptr_ void** mapped_base, _Inout_ SIZE_T* view_size)
155+
{
156+
HANDLE section_handle = (HANDLE)section;
157+
void* view;
158+
159+
view = MapViewOfFile(section_handle, FILE_MAP_ALL_ACCESS, 0, 0, *view_size);
160+
if (view == nullptr) {
161+
USERSIM_LOG_WIN32_API_FAILURE(USERSIM_TRACELOG_KEYWORD_BASE, MapViewOfFile);
162+
return win32_error_to_usersim_error(GetLastError());
163+
}
164+
165+
*mapped_base = view;
166+
return STATUS_SUCCESS;
167+
}
168+
169+
NTSTATUS
170+
MmUnmapViewInSystemSpace(_In_ void* mapped_base)
171+
{
172+
if (!UnmapViewOfFile(mapped_base)) {
173+
USERSIM_LOG_WIN32_API_FAILURE(USERSIM_TRACELOG_KEYWORD_BASE, UnmapViewOfFile);
174+
return win32_error_to_usersim_error(GetLastError());
175+
}
176+
177+
return STATUS_SUCCESS;
178+
}
179+
135180
NTSTATUS
136181
MmProtectMdlSystemAddress(_In_ MDL* memory_descriptor_list, ULONG new_protect)
137182
{

src/ob.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "platform.h"
55
#include "kernel_um.h"
66
#include "usersim/ob.h"
7+
#include "utilities.h"
78
#include <map>
89

910
static std::map<PVOID, ULONG> _object_references;
@@ -56,6 +57,28 @@ ObReferenceObjectByHandle(
5657
return STATUS_SUCCESS;
5758
}
5859

60+
_IRQL_requires_max_(PASSIVE_LEVEL) USERSIM_API NTSTATUS
61+
ObOpenObjectByPointer(
62+
_In_ PVOID object,
63+
_In_ ULONG handle_attributes,
64+
_In_opt_ void* passed_access_state,
65+
_In_ ACCESS_MASK desired_access,
66+
_In_opt_ POBJECT_TYPE object_type,
67+
_In_ KPROCESSOR_MODE access_mode,
68+
_Out_ HANDLE* handle)
69+
{
70+
UNREFERENCED_PARAMETER(handle_attributes);
71+
UNREFERENCED_PARAMETER(passed_access_state);
72+
UNREFERENCED_PARAMETER(object_type);
73+
UNREFERENCED_PARAMETER(access_mode);
74+
75+
if (!DuplicateHandle(GetCurrentProcess(), (HANDLE)object, GetCurrentProcess(), handle, desired_access, FALSE, 0)) {
76+
return win32_error_to_usersim_error(GetLastError());
77+
}
78+
79+
return STATUS_SUCCESS;
80+
}
81+
5982
USERSIM_API
6083
NTSTATUS
6184
ObCloseHandle(_In_ _Post_ptr_invalid_ HANDLE handle, _In_ KPROCESSOR_MODE previous_mode)

0 commit comments

Comments
 (0)