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
|
#include<linux/fs.h>
#include<linux/slab.h>
#include<linux/sched.h>
#include<linux/sort.h>
#include<asm/msr.h>
#include<asm/unistd.h>
#include<asm/uaccess.h>
#include"judge_def.h"
#include"judgk.h"
#include"judgk_com.h"
#include"judgk_syscall.h"
int judgk_syscall_hook(){
int i;
int j;
unsigned int size;
unsigned int restore;
syscall_init_hook();
syscall_addr_write((unsigned long)syscall_table,&size,&restore);
for(i = 0,j = 0;i < syscall_max;i++){
if(size == 0){
syscall_addr_restore((unsigned long)(syscall_table + i - 1),restore);
syscall_addr_write((unsigned long)(syscall_table + i),&size,&restore);
}
size -= sizeof(unsigned long);
if(i == syscall_whitelist[j]){
j++;
continue;
}
syscall_table[i] = (unsigned long)hook_sys_block;
}
syscall_addr_restore((unsigned long)(&syscall_table[i - 1]),restore);
return 0;
}
int judgk_syscall_unhook(){
int i;
unsigned int size;
unsigned int restore;
syscall_addr_write((unsigned long)syscall_table,&size,&restore);
for(i = 0;i < syscall_max;i++){
if(size == 0){
syscall_addr_restore((unsigned long)(&syscall_table[i - 1]),restore);
syscall_addr_write((unsigned long)(&syscall_table[i]),&size,&restore);
}
size -= sizeof(unsigned long);
syscall_table[i] = (unsigned long)judgk_syscall_ori_table[i];
}
syscall_addr_restore((unsigned long)(&syscall_table[i - 1]),restore);
kfree(judgk_syscall_ori_table);
return 0;
}
static int syscall_init_hook(){
ssize_t ret;
int i;
int j;
struct file *f;
char line[128];
unsigned char code[3] = {0xff,0x14,0xc5};
unsigned long addr;
f = filp_open("/proc/kallsyms",O_RDONLY,0);
set_fs(KERNEL_DS);
i = 0;
addr = 0;
while(true){
ret = f->f_op->read(f,&line[i],1,&f->f_pos);
if(line[i] == '\n' || ret <= 0){
line[i] = '\0';
addr = 0;
for(j = 0;j < i;j++){
if(line[j] == ' '){
j++;
break;
}
addr *= 16UL;
if(line[j] >= '0' && line[j] <= '9'){
addr += (unsigned long)(line[j] - '0');
}else{
addr += (unsigned long)(line[j] - 'a' + 10);
}
}
for(;j < i;j++){
if(line[j] == ' '){
j++;
break;
}
}
if(j < i){
if(strcmp("system_call",line + j) == 0){
break;
}
}
i = 0;
}else{
i++;
}
if(ret <= 0){
break;
}
}
set_fs(USER_DS);
filp_close(f,NULL);
while(true){
for(i = 0;i < 3;i++){
if(*(unsigned char*)addr != code[i]){
addr++;
break;
}
addr++;
}
if(i == 3){
break;
}
}
syscall_table = (unsigned long*)(0xffffffff00000000 + *((unsigned int*)addr));
addr -= 4L;
while(true){
if(*(unsigned char*)addr == 0x3d){
addr++;
break;
}
addr--;
}
syscall_max = *(unsigned int*)addr;
judgk_syscall_ori_table = kmalloc(sizeof(unsigned long) * (syscall_max + 1),GFP_KERNEL);
memcpy(judgk_syscall_ori_table,syscall_table,sizeof(unsigned long) * syscall_max);
sort(syscall_whitelist,SYSCALL_WHITELIST_SIZE,sizeof(unsigned int),syscall_whitelist_cmp,NULL);
return 0;
}
static int syscall_whitelist_cmp(const void *a,const void *b){
if(*(unsigned int*)a < *(unsigned int*)b){
return -1;
}else if(*(unsigned int*)a == *(unsigned int*)b){
return 0;
}else{
return 1;
}
}
static int syscall_addr_write(unsigned long addr,unsigned int *size,int *restore){
unsigned int level;
pte_t *pte;
pte = lookup_address(addr,&level);
if(pte->pte & _PAGE_RW){
*restore = 0;
}else{
pte->pte |= _PAGE_RW;
*restore = 1;
}
switch(level){
case PG_LEVEL_4K:
*size = 4096;
break;
case PG_LEVEL_2M:
*size = 2097152;
break;
case PG_LEVEL_1G:
*size = 1073741824;
break;
}
*size -= (((unsigned int)addr) & (*size - 1));
return 0;
}
static int syscall_addr_restore(unsigned long addr,int restore){
unsigned int level;
pte_t *pte;
if(restore){
pte = lookup_address(addr,&level);
pte->pte ^= _PAGE_RW;
}
return 0;
}
int judgk_syscall_check(){
if(judgk_proc_task_lookup(current)){
return 1;
}
return 0;
}
int judgk_syscall_block(){
struct judgk_proc_info *info;
if((info = judgk_proc_task_lookup(current)) == NULL){
return 0;
}
info->status = JUDGE_RF;
send_sig(SIGKILL,current,0);
return 0;
}
/*asmlinkage long hook_sys_nanosleep(struct timespec __user *rqtp,struct timespec __user *rmtp){
long ret;
struct judgm_proc_info *info;
atomic64_inc(&syscall_pending);
info = judgm_proc_task_lookup(current);
if(info == NULL){
ret = ori_sys_nanosleep(rqtp,rmtp);
atomic64_dec(&syscall_pending);
return ret;
}
pr_alert("judgm:PID %d nanosleep\n",current->tgid);
info->status = JUDGE_RF;
send_sig(SIGKILL,current,0);
atomic64_dec(&syscall_pending);
return -EACCES;
}*/
|