-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
359 lines (282 loc) · 7.93 KB
/
utils.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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
* bugficks
* (c) 2013-inf
*
* License: GPLv3
*
*/
//////////////////////////////////////////////////////////////////////////
#include <linux/slab.h>
#include <linux/kthread.h>
#include <linux/syscalls.h>
#include <linux/uaccess.h>
#include <asm/cacheflush.h>
#include <linux/un.h>
#include <linux/net.h>
#include <net/sock.h>
#include <linux/socket.h>
#include "utils.h"
#include "llist.h"
//////////////////////////////////////////////////////////////////////////
#define LOG_ERR KERN_ERR LOG_PREFIX
//////////////////////////////////////////////////////////////////////////
void cacheflush ( void *begin, unsigned long size )
{
flush_icache_range((uintptr_t)begin, (uintptr_t)begin + size);
}
//////////////////////////////////////////////////////////////////////////
unsigned long **aquire_sys_call_table(void)
{
uintptr_t offset = PAGE_OFFSET;
static unsigned long **sct = 0;
if(sct)
return sct;
while (offset < ULLONG_MAX) {
sct = (unsigned long **)offset;
if((sct[__NR_close] == (unsigned long *) sys_close)
&& (sct[__NR_unlink] == (unsigned long *) sys_unlink)
&& (sct[__NR_mknod] == (unsigned long *) sys_mknod))
return sct;
offset += sizeof(void *);
}
sct = 0;
return sct;
}
int task_kill(
const char *task_name)
{
typedef struct task_list_entry
{
struct llist_node list;
struct task_struct *tsk;
} task_list_entry_t;
struct task_struct *tsk = 0;
int ret = -1;
int task_name_len = strlen(task_name);
LLIST_HEAD(tsk_list);
int cnt = 0;
//read_lock(&tasklist_lock);
rcu_read_lock();
for_each_process(tsk)
{
task_lock(tsk);
//struct pid *pid = task_pid(tsk);
// printk(KERN_ERR"*** task: %s pid: %d, kt: %d\n", tsk->comm, tsk->pid, tsk->flags & PF_KTHREAD);
if((tsk->flags & PF_EXITING) == 0)
{
if(!strncmp(tsk->comm, task_name, task_name_len))
{
task_list_entry_t *tle = kmalloc(sizeof(task_list_entry_t), GFP_KERNEL);
if(!tle)
panic("can't allocate task_list_entry_t buffer");
tle->tsk = tsk;
llist_add(&tle->list, &tsk_list);
}
}
task_unlock(tsk);
}
rcu_read_unlock();
//read_unlock(&tasklist_lock);
if(!llist_empty(&tsk_list))
{
struct llist_node *head = llist_del_all(&tsk_list);
while(head)
{
task_list_entry_t *node = llist_entry(head, task_list_entry_t, list);
head = llist_next(head);
if(node)
{
struct task_struct *tsk_tokill = node->tsk;
kfree(node);
get_task_struct(tsk_tokill);
if(tsk_tokill->flags & PF_KTHREAD)
ret = kthread_stop(tsk_tokill);
else
ret = send_sig(SIGKILL, tsk_tokill, 0);
put_task_struct(tsk_tokill);
printk(LOG_ERR" task_kill(%s): %d\n", task_name, ret);
if(ret >= 0)
cnt++;
}
}
}
return cnt;
}
bool is_module_loaded(
const char *name)
{
struct module *mod = 0;
mutex_lock(&module_mutex);
mod = find_module(name);
mutex_unlock(&module_mutex);
return (mod != 0);
}
//////////////////////////////////////////////////////////////////////////
int mod_delete_module(
const char *modname)
{
mm_segment_t old_fs;
int ret = -1;
unsigned long **sct = aquire_sys_call_table();
typedef long (*delete_module_t)(const char __user *name_user, unsigned int flags);
static delete_module_t delete_module = 0;
if(!delete_module)
delete_module = (delete_module_t)sct[__NR_delete_module];
old_fs = get_fs();
set_fs(KERNEL_DS);
//set_fs(get_ds());
ret = delete_module(modname, O_TRUNC);
printk(LOG_ERR" delete_module(%s) -> %d\n", modname, ret);
set_fs(old_fs);
return ret;
}
int mod_touch(
const char *path)
{
struct file *filp = filp_open(path, O_WRONLY|O_CREAT, 0);
printk(LOG_ERR" mod_touch(%s) -> %p \n", path, filp);
if(IS_ERR(filp))
return PTR_ERR(filp);
filp_close(filp, 0);
return 0;
}
int mod_unlink(
const char *path)
{
mm_segment_t old_fs;
int ret = -1;
old_fs = get_fs();
set_fs(KERNEL_DS);
ret = sys_unlink(path);
printk(LOG_ERR" mod_unlink(%s) -> %d \n", path, ret);
set_fs(old_fs);
return ret;
}
long mod_mount(
const char *dev_name, const char *dir_name, const char *type, unsigned long flags, void *data)
{
typedef long (*sys_mount_t)(const char *dev_name, const char *dir_name, const char *type, unsigned long flags, void __user *data);
int err;
mm_segment_t old_fs;
sys_mount_t mount = 0;
unsigned long **sct = aquire_sys_call_table();
if(!sct)
return -EINVAL;
mount = (sys_mount_t)sct[__NR_mount];
if(!mount)
return -EINVAL;
old_fs = get_fs();
set_fs(KERNEL_DS);
err = mount(dev_name, dir_name, type, flags, data);
set_fs(old_fs);
return err;
}
//////////////////////////////////////////////////////////////////////////
uint32_t *do_patch(
bool set, const char *lbl, uint32_t *p, uint32_t *insn_new, uint32_t *insn_save,
uint32_t cnt)
{
uint32_t ncopy = sizeof(*insn_new) * cnt;
if(!p)
{
printk(LOG_ERR" %s patch location not found.\n", lbl);
return 0;
}
//printk(KERN_ERR" *** %s *** @ 0x%p 0x%08x.\n", lbl, p, p[0]);
if(set)
{
if(memcmp(p, insn_new, ncopy))
{
printk(LOG_ERR" patching %s [0x%p : %08x = %08x]\n", lbl, p, p[0], insn_new[0]);
memcpy(insn_save, p, ncopy);
memcpy(p, insn_new, ncopy);
cacheflush(p, ncopy);
}
else
{
printk(LOG_ERR" %s already patched [0x%p : %08x]\n", lbl, p, p[0]);
}
}
else
{
printk(LOG_ERR" reverting %s [0x%p : %08x = %08x]\n", lbl, p, p[0], insn_save[0]);
memcpy(p, insn_save, ncopy);
cacheflush(p, 4);
}
return p;
}
//////////////////////////////////////////////////////////////////////////
static void *resolve_symbol(char *name)
{
void *ret = 0;
const struct kernel_symbol *sym;
mutex_lock(&module_mutex);
sym = find_symbol(name, NULL, NULL, 1, true);
mutex_unlock(&module_mutex);
if (sym) {
ret = (void *)sym->value;
}
return ret;
}
void *mod_ADDR(const char *name, uint32_t offs)
{
ptrdiff_t mod_addr = 0;
struct module *mod = find_module(name);
if(mod)
mod_addr = (ptrdiff_t)mod->module_core;
//printk(KERN_ERR"**** %s: %08x\n", name, mod_addr);
if(!mod_addr)
return 0;
return (void*)(mod_addr + offs);
}
//////////////////////////////////////////////////////////////////////////
// https://gist.github.com/llj098/752417
int ktcp_send(struct socket *sock, const void *buf, int len)
{
if(sock==NULL)
return -1;
struct msghdr msg;
struct iovec iov;
int size;
mm_segment_t oldfs;
iov.iov_base=(void*)buf;
iov.iov_len=len;
msg.msg_control=NULL;
msg.msg_controllen=0;
msg.msg_flags=0;
msg.msg_iov=&iov;
msg.msg_iovlen=1;
msg.msg_name=0;
msg.msg_namelen=0;
oldfs=get_fs();
set_fs(KERNEL_DS);
size=sock_sendmsg(sock,&msg,len);
set_fs(oldfs);
return size;
}
int ktcp_recv(struct socket *sock, void *buf, int len)
{
if(sock==NULL)
return -1;
struct msghdr msg;
struct iovec iov;
mm_segment_t oldfs;
int size=0;
if(sock->sk==NULL)
return 0;
iov.iov_base=buf;
iov.iov_len=len;
msg.msg_control=NULL;
msg.msg_controllen=0;
msg.msg_flags=0;
msg.msg_name=0;
msg.msg_namelen=0;
msg.msg_iov=&iov;
msg.msg_iovlen=1;
oldfs=get_fs();
set_fs(KERNEL_DS);
size=sock_recvmsg(sock,&msg,len,msg.msg_flags);
set_fs(oldfs);
return size;
}
//////////////////////////////////////////////////////////////////////////