Repository navigation
Expand file tree
/
Copy pathextra.c
More file actions
248 lines (208 loc) · 5.12 KB
/
Copy pathextra.c
File metadata and controls
248 lines (208 loc) · 5.12 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
/*
Declaration for Linux kernel compatibility
*/
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ptrace.h>
#include "extra.h"
pid_t victim_pid= -1;
char *victim_exe= NULL;
#define TRAP_BIT (0x80000000)
static struct nlist *exe_nlist;
static int exe_nlist_n;
/* unsigned long __get_free_page(int type) { assert(0); } */
/* void *kmalloc(size_t size, int type) { assert(0); } */
void free_page(unsigned long page) { assert(0); }
/* void kfree(void *mem) { assert(0); } */
void vfree(void *mem) { assert(0); }
size_t strncpy_from_user(char *addr, const char *user_name, size_t size)
{ assert(0); return 0; }
/* void lock_kernel(void) { assert(0); } */
/* void unlock_kernel(void) { assert(0); } */
/* void __asm__(char *str) { assert(0); } */
extern void *__vmalloc(unsigned long size, int gfp_mask, pgprot_t prot)
{ assert(0); return NULL; }
#if 0
void kallsyms_sections(void *infop,
int (*fp)(void *token, const char *modname, const char *secname,
ElfW(Addr) secstart, ElfW(Addr) secend, ElfW(Word) secflags))
{ assert(0); }
#endif
unsigned long __generic_copy_to_user(void *x, const void *y, unsigned long z)
{ assert(0); return -1; }
unsigned long __generic_copy_from_user(void *x, const void *y, unsigned long z)
{ assert(0); return -1; }
/* void read_lock(struct lock *lock) { assert(0); } */
/* void read_unlock(struct lock *lock) { assert(0); } */
void udelay(unsigned long usecs) { assert(0); }
int copy_to_user(void * result_record, void *res, size_t size)
{
memcpy(result_record, res, size);
return 0;
}
void panic(char *str) { assert(0); }
void printk(char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
}
int kallsyms_address_to_symbol(db_expr_t off,
const char * *mod_name, unsigned long *mod_start, unsigned long *mod_end,
const char * *sec_name, unsigned long *sec_start, unsigned long *sec_end,
const char * *sym_name, unsigned long *sym_start, unsigned long *sym_end)
{
static char name[64];
int i;
unsigned long btext, etext;
struct nlist *below, *above;
off &= ~TRAP_BIT;
load_nlist(victim_exe, &btext, &etext);
below= above= NULL;
for (i= 0; i<exe_nlist_n; i++)
{
if (exe_nlist[i].n_type != N_TEXT)
continue;
if (exe_nlist[i].n_value <= off)
{
if (!below || exe_nlist[i].n_value > below->n_value)
below= &exe_nlist[i];
}
if (exe_nlist[i].n_value > off)
{
if (!above || exe_nlist[i].n_value < above->n_value)
above= &exe_nlist[i];
}
}
#if 0
if (below)
{
printf("found '%s' at 0x%x\n", below->n_name, below->n_value);
}
if (above)
{
printf("found '%s' at 0x%x\n", above->n_name, above->n_value);
}
#endif
btext |= TRAP_BIT;
etext |= TRAP_BIT;
*mod_name = victim_exe;
*mod_start = btext;
*mod_end = etext;
*sec_name = ".text";
*sec_start = btext;
*sec_end = etext;
assert(below && above);
strncpy(name, below->n_name, sizeof(name)-1);
name[sizeof(name)-1]= '\0';
*sym_name= name;
*sym_start= below->n_value | TRAP_BIT;
*sym_end= above->n_value | TRAP_BIT;
return 1;
}
struct module *module_list;
struct task_struct *task_list;
struct lock tasklist_lock;
unsigned long text_read_ul(void *addr)
{
int i;
unsigned long value;
for (i= 0; i<sizeof(value); i++)
{
((unsigned char *)&value)[i]= text_read_ub((char *)addr+i);
}
return value;
}
unsigned char text_read_ub(void *addr)
{
int v;
unsigned long vaddr;
vaddr= (unsigned long)addr;
vaddr &= ~TRAP_BIT;
v= ptrace(T_READB_INS, victim_pid, vaddr, 0);
if (v < 0)
{
fprintf(stderr,
"text_read_ub: trace T_READB_INS failed on pid %d, addr 0x%lx: %s\n",
victim_pid, vaddr, strerror(errno));
exit(1);
}
return v;
}
void text_write_ul(void *addr, unsigned long value)
{
int i;
for (i= 0; i<sizeof(value); i++)
{
text_write_ub((char *)addr+i, ((unsigned char *)&value)[i]);
}
}
void text_write_ub(void *addr, unsigned char value)
{
int v;
unsigned long vaddr;
vaddr= (unsigned long)addr;
vaddr &= ~TRAP_BIT;
v= ptrace(T_WRITEB_INS, victim_pid, vaddr, value);
if (v < 0)
{
fprintf(stderr,
"text_read_ub: trace T_WRITEB_INS failed on pid %d, addr 0x%lx: %s\n",
victim_pid, vaddr, strerror(errno));
exit(1);
}
}
void load_nlist(exe_name, btextp, etextp)
char *exe_name;
unsigned long *btextp;
unsigned long *etextp;
{
int i;
unsigned long btext, etext;
if (!exe_nlist)
{
exe_nlist_n= read_nlist(exe_name, &exe_nlist);
if (exe_nlist_n <= 0)
{
if (exe_nlist_n == -1)
{
fprintf(stderr,
"error reading name list from '%s': %s\n",
exe_name, strerror(errno));
}
else
fprintf(stderr, "no name list in '%s'\n",
exe_name);
exit(1);
}
}
if (!btextp && !etextp)
return;
etext= 0;
btext= (unsigned long)-1;
for (i= 0; i<exe_nlist_n; i++)
{
if (exe_nlist[i].n_type != N_TEXT)
continue;
if (exe_nlist[i].n_value < btext)
btext= exe_nlist[i].n_value;
if (exe_nlist[i].n_value > etext)
etext= exe_nlist[i].n_value;
}
if (btext >= etext)
{
fprintf(stderr, "Bad btext (0x%lx) or etext (0x%lx) in %s\n",
btext, etext, exe_name);
exit(1);
}
btext |= TRAP_BIT;
etext |= TRAP_BIT;
if (btextp)
*btextp= btext;
if (etextp)
*etextp= etext;
}