-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmemory.h
More file actions
321 lines (285 loc) · 8.96 KB
/
Copy pathmemory.h
File metadata and controls
321 lines (285 loc) · 8.96 KB
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include <linux/sched.h>
#include <linux/tty.h>
#include <linux/io.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/version.h>
#if(LINUX_VERSION_CODE >= KERNEL_VERSION(4,14,83))
#include <linux/sched/mm.h>
#endif
#include <asm/cpu.h>
#include <asm/io.h>
#include <asm/page.h>
#include <asm/pgtable.h>
#include "process.h"
#undef pgd_offset
#if LINUX_VERSION_CODE <= KERNEL_VERSION(3,10,84)
//来源:#define pgd_offset(mm, addr) ((mm)->pgd+pgd_index(addr))
#define my_pgd_offset(pgd, addr) (pgd+pgd_index(addr))
#define my_pud_offset(dir, addr) ((pud_t *)__va(pud_offset_phys((dir), (addr))))
#endif
#if LINUX_VERSION_CODE < KERNEL_VERSION(5,10,43)
//来源:#define pgd_offset(mm, addr) ((mm)->pgd+pgd_index(addr))
#define my_pgd_offset(pgd, addr) (pgd+pgd_index(addr))
#define my_pud_offset(dir, addr) ((pud_t *)__va(pud_offset_phys((dir), (addr))))
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,10,43)
//来源:#define pgd_offset(mm, address) pgd_offset_pgd((mm)->pgd, (address))
#define my_pgd_offset(pgd, address) pgd_offset_pgd(pgd, address)
#endif
static ssize_t g_pgd_offset_mm_struct = 0;
static bool g_init_pgd_offset_success = false;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6,1,75)
static int init_pgd_offset(struct mm_struct *mm) {
int is_find_pgd_offset = 0;
g_init_pgd_offset_success = false;
for (g_pgd_offset_mm_struct = -40; g_pgd_offset_mm_struct <= 80; g_pgd_offset_mm_struct += 1) {
char *rp;
size_t val;
ssize_t accurate_offset = (ssize_t)((size_t)&mm->pgd - (size_t)mm + g_pgd_offset_mm_struct);
if (accurate_offset >= sizeof(struct mm_struct) - sizeof(ssize_t)) {
return -EFAULT;
}
rp = (char*)((size_t)mm + (size_t)accurate_offset);
val = *(size_t*)(rp);
//printk_debug(KERN_EMERG "init_pgd_offset %zd:%zd:%p:%ld\n", g_pgd_offset_mm_struct, accurate_offset, rp, val);
if (val == TASK_SIZE) {
g_pgd_offset_mm_struct += sizeof(unsigned long);
//printk_debug(KERN_EMERG "found g_init_pgd_offset_success:%zd\n", g_pgd_offset_mm_struct);
is_find_pgd_offset = 1;
break;
}
}
if (!is_find_pgd_offset) {
//printk_debug(KERN_INFO "find pgd offset failed\n");
return -ESPIPE;
}
g_init_pgd_offset_success = true;
//printk_debug(KERN_INFO "g_pgd_offset_mm_struct:%zu\n", g_pgd_offset_mm_struct);
return 0;
}
#else
static int init_pgd_offset(struct mm_struct *mm) {
int is_find_pgd_offset = 0;
g_init_pgd_offset_success = false;
for (g_pgd_offset_mm_struct = -40; g_pgd_offset_mm_struct <= 80; g_pgd_offset_mm_struct += 1) {
char *rp;
size_t val;
ssize_t accurate_offset = (ssize_t)((size_t)&mm->pgd - (size_t)mm + g_pgd_offset_mm_struct);
if (accurate_offset >= sizeof(struct mm_struct) - sizeof(ssize_t)) {
return -EFAULT;
}
rp = (char*)((size_t)mm + (size_t)accurate_offset);
val = *(size_t*)(rp);
//printk_debug(KERN_EMERG "init_pgd_offset %zd:%zd:%p:%ld\n", g_pgd_offset_mm_struct, accurate_offset, rp, val);
if (val == TASK_SIZE) {
g_pgd_offset_mm_struct += sizeof(unsigned long);
g_pgd_offset_mm_struct += sizeof(unsigned long);
printk_debug(KERN_EMERG "found g_init_pgd_offset_success:%zd\n", g_pgd_offset_mm_struct);
is_find_pgd_offset = 1;
break;
}
}
if (!is_find_pgd_offset) {
//printk_debug(KERN_INFO "find pgd offset failed\n");
return -ESPIPE;
}
g_init_pgd_offset_success = true;
//printk_debug(KERN_INFO "g_pgd_offset_mm_struct:%zu\n", g_pgd_offset_mm_struct);
return 0;
}
#endif
static inline pgd_t *x_pgd_offset(struct mm_struct *mm, size_t addr) {
size_t pgd;
ssize_t accurate_offset;
if (g_init_pgd_offset_success == false) {
if (init_pgd_offset(mm) != 0) {
return NULL;
}
}
//精确偏移
accurate_offset = (ssize_t)((size_t)&mm->pgd - (size_t)mm + g_pgd_offset_mm_struct);
//printk_debug(KERN_INFO "x_pgd_offset accurate_offset:%zd\n", accurate_offset);
if (accurate_offset >= sizeof(struct mm_struct) - sizeof(ssize_t)) {
return NULL;
}
//拷贝到我自己的pgd指针变量里去
//写法一(可读性强)
//void * rv = (size_t*)((size_t)mm + (size_t)accurate_offset);
//pgd_t *pgd;
//memcpy(&pgd, rv, sizeof(pgd_t *));
//写法二(快些)
pgd = *(size_t*)((size_t)mm + (size_t)accurate_offset);
return my_pgd_offset((pgd_t*)pgd, addr);
}
//走页表获取物理地址
phys_addr_t translate_linear_address(struct mm_struct* mm, uintptr_t va) {
pgd_t *pgd;
p4d_t *p4d;
pmd_t *pmd;
pte_t *pte;
pud_t *pud;
unsigned long paddr = 0;
unsigned long page_addr = 0;
unsigned long page_offset = 0;
pgd = x_pgd_offset(mm, va);
if(pgd_none(*pgd)) {
return 0;
}
p4d = p4d_offset(pgd, va);
if (p4d_none(*p4d)) {
return 0;
}
pud = pud_offset(p4d,va);
if(pud_none(*pud)) {
return 0;
}
pmd = pmd_offset(pud,va);
if(pmd_none(*pmd)) {
return 0;
}
pte = pte_offset_kernel(pmd,va);
if(pte_none(*pte)) {
return 0;
}
if(!pte_present(*pte)) {
return 0;
}
//页物理地址
page_addr = page_to_phys(pte_page(*pte));
page_offset = va & ~PAGE_MASK;
paddr = page_addr | page_offset;
return paddr;
}
#ifdef ARCH_HAS_VALID_PHYS_ADDR_RANGE
static size_t g_phy_total_memory_size = 0;
//初始化物理内存大小
static int init_phy_total_memory_size(void) {
struct sysinfo si;
si_meminfo(&si);
g_phy_total_memory_size = __pa(si.totalram * si.mem_unit);
return 0;
}
//检查物理地址范围
static inline int check_phys_addr_range(phys_addr_t addr, size_t count) {
if (g_phy_total_memory_size == 0)
init_phy_total_memory_size();
return (addr + count) <= g_phy_total_memory_size;
}
#else
static inline int check_phys_addr_range(phys_addr_t addr, size_t count) {
return 1;
}
#endif
//读写物理地址
static size_t access_physical_address(phys_addr_t pa, void* buffer, size_t size, bool is_write) {
void __iomem *mapped = NULL;
size_t ret = 0;
if (!check_phys_addr_range(pa, size)) // 新增范围检查
return 0;
if (!pfn_valid(__phys_to_pfn(pa)))
return 0;
mapped = ioremap_cache(pa, size);
if (!mapped)
return 0;
if (is_write)
ret = x_copy_from_user(mapped, buffer, size) ? 0 : size;
else
ret = x_copy_to_user(buffer, mapped, size) ? 0 : size;
iounmap(mapped);
return ret;
}
/*对外接口实现 读写进程内存*/
bool read_process_memory(pid_t pid, uintptr_t addr, void *buffer, size_t size)
{
struct task_struct *task = NULL;
struct mm_struct *mm = NULL;
size_t remaining = size;
size_t chunk, copied = 0;
phys_addr_t pa;
char __user *buf = buffer;
bool ret = false;
/* 1. 获取任务结构 */
rcu_read_lock();
task = pid_task(find_vpid(pid), PIDTYPE_PID);
if (!task) {
// pr_debug("Task not found for pid %d\n", pid);
rcu_read_unlock();
return false;
}
rcu_read_unlock();
/* 2. 获取内存管理结构 */
mm = get_task_mm(task);
if (!mm) {
//pr_debug("Failed to get mm_struct for pid %d\n", pid);
goto out;
}
/* 3. 分页读取内存 */
while (remaining > 0) {
chunk = min_t(size_t, PAGE_SIZE - (addr & ~PAGE_MASK), remaining);
pa = translate_linear_address(mm, addr);
if (!pa) {
// pr_debug("Failed to translate VA 0x%lx in pid %d\n", addr, pid);
goto out_mm;
}
if (!access_physical_address(pa, buf + copied, chunk, false)) {
//pr_debug("Failed to read %zu bytes at PA 0x%llx\n", chunk, (u64)pa);
goto out_mm;
}
copied += chunk;
addr += chunk;
remaining -= chunk;
}
ret = (copied == size);
out_mm:
mmput(mm);
out:
return ret;
}
bool write_process_memory(pid_t pid, uintptr_t addr, void *buffer, size_t size)
{
struct task_struct *task = NULL;
struct mm_struct *mm = NULL;
size_t remaining = size;
size_t chunk, written = 0;
phys_addr_t pa;
const char __user *buf = buffer;
bool ret = false;
/* 1. 获取任务结构 */
rcu_read_lock();
task = pid_task(find_vpid(pid), PIDTYPE_PID);
if (!task) {
//pr_debug("Task not found for pid %d\n", pid);
rcu_read_unlock();
return false;
}
rcu_read_unlock();
/* 2. 获取内存管理结构 */
mm = get_task_mm(task);
if (!mm) {
//pr_debug("Failed to get mm_struct for pid %d\n", pid);
goto out;
}
/* 3. 分页写入内存 */
while (remaining > 0) {
chunk = min_t(size_t, PAGE_SIZE - (addr & ~PAGE_MASK), remaining);
pa = translate_linear_address(mm, addr);
if (!pa) {
//pr_debug("Failed to translate VA 0x%lx in pid %d\n", addr, pid);
goto out_mm;
}
if (!access_physical_address(pa, (void *)(buf + written), chunk, true)) {
// pr_debug("Failed to write %zu bytes at PA 0x%llx\n", chunk, (u64)pa);
goto out_mm;
}
written += chunk;
addr += chunk;
remaining -= chunk;
}
ret = (written == size);
out_mm:
mmput(mm);
out:
return ret;
}