-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathutil.c
182 lines (163 loc) · 3.94 KB
/
util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "project.h"
#include "debug.h"
//***********************************************************************************
// Name: UF_LockUserBuffer
//
// Routine Description:
//
// Locks user buffer in memory and maps it into system space.
//
// Return Value:
//
// Operation status.
//
// Parameters:
//
//***********************************************************************************
NTSTATUS
UF_LockUserBuffer(
IN PVOID UserBuffer,
IN ULONG UserBufferSize,
IN LOCK_OPERATION LockOperation,
OUT PMDL *pMdl,
OUT PVOID* SystemBuffer
)
{
NTSTATUS ntStatus = STATUS_SUCCESS;
PVOID LockedAddress = NULL;
BOOLEAN bLocked;
PMDL Mdl;
SIZE_T MdlSize = 0;
// sanity checks
ASSERT(LockOperation==IoReadAccess || LockOperation == IoWriteAccess || LockOperation == IoModifyAccess );
/*
* Allocate and initialize MDL
*/
MdlSize = MmSizeOfMdl(UserBuffer,UserBufferSize);
if ( !MdlSize ) {
DBGLOG((LError,"MmSizeOfMdl failed\n"));
return STATUS_UNSUCCESSFUL;
}
Mdl = ExAllocatePoolWithTag( NonPagedPool, MdlSize, 'UBPL' );
ntStatus = Mdl ? STATUS_SUCCESS : STATUS_INSUFFICIENT_RESOURCES;
if(!NT_SUCCESS(ntStatus)) {
return ntStatus;
}
MmInitializeMdl(Mdl,UserBuffer,UserBufferSize);
/*
* Lock user pages
*/
try {
MmProbeAndLockPages(
Mdl,
UserMode,
LockOperation
);
bLocked = TRUE;
} except (EXCEPTION_EXECUTE_HANDLER) {
ntStatus = GetExceptionCode();
}
if(!NT_SUCCESS(ntStatus)){
DBGLOG((LError,"%08X=MmProbeAndLockPages failed\n",ntStatus));
IoFreeMdl( Mdl );
return STATUS_INSUFFICIENT_RESOURCES;
}
*SystemBuffer = MmGetSystemAddressForMdlSafe( Mdl, NormalPagePriority );
if ( *SystemBuffer == NULL ){
DBGLOG((LError,"MmGetSystemAddressForMdlPrettySafe failed\n"));
MmUnlockPages( Mdl );
IoFreeMdl( Mdl );
}else{
*pMdl = Mdl;
}
return STATUS_SUCCESS;
}
//***********************************************************************************
// Name: UF_LockUserBufferLight
//
// Routine Description:
//
// Locks user buffer in memory and maps it into system space.
//
// Return Value:
//
// Operation status.
//
// Parameters:
//
//***********************************************************************************
NTSTATUS
UF_LockUserBufferLight(
IN PVOID UserBuffer,
IN ULONG UserBufferSize,
OUT PMDL *pMdl,
OUT PVOID* SystemBuffer
)
{
NTSTATUS ntStatus = STATUS_SUCCESS;
PVOID LockedAddress = NULL;
BOOLEAN bLocked;
PMDL Mdl;
SIZE_T MdlSize = 0;
/*
* Allocate and initialize MDL
*/
Mdl = IoAllocateMdl(UserBuffer,UserBufferSize,FALSE,FALSE,NULL);
if ( Mdl == NULL ) {
DBGLOG((LError,"MmSizeOfMdl failed\n"));
return STATUS_INSUFFICIENT_RESOURCES;
}
/*
* Lock user pages
*/
try {
MmProbeAndLockPages(
Mdl,
KernelMode,
IoModifyAccess
);
bLocked = TRUE;
} except (EXCEPTION_EXECUTE_HANDLER) {
ntStatus = GetExceptionCode();
}
if(!NT_SUCCESS(ntStatus)){
DBGLOG((LError,"%08X=MmProbeAndLockPages failed\n",ntStatus));
IoFreeMdl( Mdl );
return STATUS_INSUFFICIENT_RESOURCES;
}
*SystemBuffer = MmGetSystemAddressForMdlSafe( Mdl, NormalPagePriority );
if ( *SystemBuffer == NULL ){
DBGLOG((LError,"MmGetSystemAddressForMdlPrettySafe failed\n"));
MmUnlockPages( Mdl );
IoFreeMdl( Mdl );
}else{
*pMdl = Mdl;
}
return STATUS_SUCCESS;
}
//***********************************************************************************
// Name: UF_UnlockUserBuffer
//
// Routine Description:
//
// Unlocks user buffer locked with UF_LockUserBuffer function.
//
// Return Value:
//
// Operation status.
//
// Parameters:
//
//***********************************************************************************
NTSTATUS
UF_UnlockUserBuffer(
IN PMDL Mdl,
IN PVOID SystemBuffer
)
{
if ( Mdl ) {
MmUnlockPages( Mdl );
ExFreePoolWithTag( Mdl, 'UBPL' );
}
return STATUS_SUCCESS;
}