forked from h33p/vmread
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mem.c
267 lines (206 loc) · 7.18 KB
/
mem.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include "mem.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#ifndef NO_ASSERTS
#include <assert.h>
#endif
#if (LMODE() == MODE_EXTERNAL() && !defined(KMOD_MEMMAP))
#define USE_PAGECACHE
#endif
/* For how long should the cached page be valid */
#ifdef USE_PAGECACHE
#ifndef VT_CACHE_TIME_MS
#define VT_CACHE_TIME_MS 1
#endif
#ifndef VT_CACHE_TIME_NS
#define VT_CACHE_TIME_NS VT_CACHE_TIME_MS * 1000000
#endif
#endif
static const uint64_t PMASK = (~0xfull << 8) & 0xfffffffffull;
static void FillRWInfo(const ProcessData* data, uint64_t dirBase, RWInfo* info, int* count, uint64_t local, uint64_t remote, size_t len);
static int FillRWInfoMul(const ProcessData* data, uint64_t dirBase, RWInfo* origData, RWInfo* info, size_t count);
static int CalculateDataCount(RWInfo* info, size_t count);
ssize_t VMemRead(const ProcessData* data, uint64_t dirBase, uint64_t local, uint64_t remote, size_t size)
{
if ((remote >> 12ull) == ((remote + size) >> 12ull))
return MemRead(data, local, VTranslate(data, dirBase, remote), size);
int dataCount = (int)((size - 1) / 0x1000) + 2;
RWInfo rdataStack[MAX_BATCHED_RW];
RWInfo* rdata = rdataStack;
if (dataCount > MAX_BATCHED_RW)
rdata = (RWInfo*)malloc(sizeof(RWInfo) * dataCount);
FillRWInfo(data, dirBase, rdata, &dataCount, local, remote, size);
ssize_t ret = MemReadMul(data, rdata, dataCount);
if (dataCount > MAX_BATCHED_RW)
free(rdata);
return ret;
}
ssize_t VMemWrite(const ProcessData* data, uint64_t dirBase, uint64_t local, uint64_t remote, size_t size)
{
if ((remote >> 12ull) == ((remote + size) >> 12ull))
return MemWrite(data, local, VTranslate(data, dirBase, remote), size);
int dataCount = (int)((size - 1) / 0x1000) + 2;
RWInfo wdataStack[MAX_BATCHED_RW];
RWInfo* wdata = wdataStack;
if (dataCount > MAX_BATCHED_RW)
wdata = (RWInfo*)malloc(sizeof(RWInfo) * dataCount);
FillRWInfo(data, dirBase, wdata, &dataCount, local, remote, size);
ssize_t ret = MemWriteMul(data, wdata, dataCount);
if (dataCount > MAX_BATCHED_RW)
free(wdata);
return ret;
}
uint64_t VMemReadU64(const ProcessData* data, uint64_t dirBase, uint64_t remote)
{
uint64_t dest;
MemRead(data, (uint64_t)&dest, VTranslate(data, dirBase, remote), sizeof(uint64_t));
return dest;
}
uint64_t VMemWriteU64(const ProcessData* data, uint64_t dirBase, uint64_t remote)
{
uint64_t dest;
MemRead(data, (uint64_t)&dest, VTranslate(data, dirBase, remote), sizeof(uint64_t));
return dest;
}
uint64_t MemReadU64(const ProcessData* data, uint64_t remote)
{
uint64_t dest;
MemRead(data, (uint64_t)&dest, remote, sizeof(uint64_t));
return dest;
}
uint64_t MemWriteU64(const ProcessData* data, uint64_t remote)
{
uint64_t dest;
MemRead(data, (uint64_t)&dest, remote, sizeof(uint64_t));
return dest;
}
ssize_t VMemReadMul(const ProcessData* data, uint64_t dirBase, RWInfo* info, size_t num)
{
int dataCount = CalculateDataCount(info, num);
RWInfo readInfoStack[MAX_BATCHED_RW];
RWInfo* readInfo = readInfoStack;
if (num > MAX_BATCHED_RW)
readInfo = (RWInfo*)malloc(sizeof(RWInfo) * num);
dataCount = FillRWInfoMul(data, dirBase, info, readInfo, num);
ssize_t ret = MemReadMul(data, readInfo, dataCount);
if (num > MAX_BATCHED_RW)
free(readInfo);
return ret;
}
ssize_t VMemWriteMul(const ProcessData* data, uint64_t dirBase, RWInfo* info, size_t num)
{
int dataCount = CalculateDataCount(info, num);
RWInfo writeInfoStack[MAX_BATCHED_RW];
RWInfo* writeInfo = writeInfoStack;
if (num > MAX_BATCHED_RW)
writeInfo = (RWInfo*)malloc(sizeof(RWInfo) * num);
dataCount = FillRWInfoMul(data, dirBase, info, writeInfo, num);
ssize_t ret = MemWriteMul(data, writeInfo, dataCount);
if (num > MAX_BATCHED_RW)
free(writeInfo);
return ret;
}
/*
This is used to cache the pages touched last bu reads of VTranslate, this increases the performance of external mode by at least 2x for multiple consequitive reads in common area. Cached page expires after a set interval which should be small enough not to cause very serious harm
*/
#ifdef USE_PAGECACHE
static __thread uint64_t vtCachePage[4] = { 0, 0, 0, 0 };
static __thread char vtCache[4][0x1000];
static __thread struct timespec vtCacheTime[4];
#endif
static uint64_t VtMemReadU64(const ProcessData* data, size_t idx, uint64_t address)
{
#ifdef USE_PAGECACHE
uint64_t page = address & ~0xfff;
struct timespec t;
clock_gettime(CLOCK_REALTIME, &t);
uint64_t timeDiff = (t.tv_sec - vtCacheTime[idx].tv_sec) * (uint64_t)1e9 + (t.tv_nsec - vtCacheTime[idx].tv_nsec);
if (vtCachePage[idx] != page || timeDiff >= VT_CACHE_TIME_NS) {
MemRead(data, (uint64_t)vtCache[idx], page, 0x1000);
vtCachePage[idx] = page;
vtCacheTime[idx] = t;
}
return *(uint64_t*)(void*)(vtCache[idx] + (address & 0xfff));
#else
(void)idx;
return MemReadU64(data, address);
#endif
}
/*
Translates a virtual address to a physical one. This (most likely) is windows specific and might need extra work to work on Linux target.
*/
uint64_t VTranslate(const ProcessData* data, uint64_t dirBase, uint64_t address)
{
dirBase &= ~0xf;
uint64_t pageOffset = address & ~(~0ul << PAGE_OFFSET_SIZE);
uint64_t pte = ((address >> 12) & (0x1ffll));
uint64_t pt = ((address >> 21) & (0x1ffll));
uint64_t pd = ((address >> 30) & (0x1ffll));
uint64_t pdp = ((address >> 39) & (0x1ffll));
uint64_t pdpe = VtMemReadU64(data, 0, dirBase + 8 * pdp);
if (~pdpe & 1)
return 0;
uint64_t pde = VtMemReadU64(data, 1, (pdpe & PMASK) + 8 * pd);
if (~pde & 1)
return 0;
/* 1GB large page, use pde's 12-34 bits */
if (pde & 0x80)
return (pde & (~0ull << 42 >> 12)) + (address & ~(~0ull << 30));
uint64_t pteAddr = VtMemReadU64(data, 2, (pde & PMASK) + 8 * pt);
if (~pteAddr & 1)
return 0;
/* 2MB large page */
if (pteAddr & 0x80)
return (pteAddr & PMASK) + (address & ~(~0ull << 21));
address = VtMemReadU64(data, 3, (pteAddr & PMASK) + 8 * pte) & PMASK;
if (!address)
return 0;
return address + pageOffset;
}
/* Static functions */
static int CalculateDataCount(RWInfo* info, size_t count)
{
int ret = 0;
for (size_t i = 0; i < count; i++)
ret += ((info[i].remote + info[i].size - 1) >> 12ull) - (info[i].remote >> 12ull);
return ret;
}
static int FillRWInfoMul(const ProcessData* data, uint64_t dirBase, RWInfo* origData, RWInfo* info, size_t count)
{
int ret = 0;
for (size_t i = 0; i < count; i++) {
int lcount = 0;
FillRWInfo(data, dirBase, info + ret, &lcount, origData[i].local, origData[i].remote, origData[i].size);
ret += lcount;
}
return ret;
}
static void FillRWInfo(const ProcessData* data, uint64_t dirBase, RWInfo* info, int* count, uint64_t local, uint64_t remote, size_t len)
{
memset(info, 0, sizeof(RWInfo) * *count);
info[0].local = local;
info[0].remote = VTranslate(data, dirBase, remote);
info[0].size = 0x1000 - (remote & 0xfff);
#ifndef NO_ASSERTS
assert(!((remote + info[0].size) & 0xfff));
#endif
if (info[0].size > len)
info[0].size = len;
uint64_t curSize = info[0].size;
uint64_t tlen = 0;
int i = 1;
for (; curSize < len; curSize += 0x1000) {
info[i].local = local + curSize;
info[i].remote = VTranslate(data, dirBase, remote + curSize);
info[i].size = len - curSize;
if (info[i].size > 0x1000)
info[i].size = 0x1000;
if (info[i].remote) {
tlen += info[i].size;
i++;
}
}
*count = i;
}