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
|
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/list.h>
#include "proc_filters.h"
#include <img/img_proc.h>
#include <img/img_file.h>
#include <img/img_ip.h>
#include <sspt/sspt_proc.h>
#include <helper.h>
#include "../../driver/us_def_handler.h"
struct pf_group {
struct list_head list;
struct img_proc *i_proc;
struct proc_filter *filter;
/* TODO: proc_list*/
struct list_head proc_list;
};
struct pl_struct {
struct list_head list;
struct sspt_proc *proc;
};
static LIST_HEAD(pfg_list);
/* struct pl_struct */
static struct pl_struct *create_pl_struct(struct sspt_proc *proc)
{
struct pl_struct *pls = kmalloc(sizeof(*pls), GFP_KERNEL);
INIT_LIST_HEAD(&pls->list);
pls->proc = proc;
return pls;
}
static void free_pl_struct(struct pl_struct *pls)
{
/* FIXME: free */
}
static void add_pl_struct(struct pf_group *pfg, struct pl_struct *pls)
{
list_add(&pls->list, &pfg->proc_list);
}
static void del_pl_struct(struct pl_struct *pls)
{
list_del(&pls->list);
}
void copy_proc_form_img_to_sspt(struct img_proc *i_proc, struct sspt_proc *proc)
{
char *file_name;
struct sspt_file *file;
struct ip_data ip_d;
struct dentry *dentry;
struct img_file *i_file;
struct img_ip *i_ip;
list_for_each_entry(i_file, &i_proc->file_list, list) {
dentry = i_file->dentry;
file_name = dentry->d_iname;
file = sspt_proc_find_file_or_new(proc, dentry, file_name);
list_for_each_entry(i_ip, &i_file->ip_list, list) {
ip_d.flag_retprobe = 1;
ip_d.got_addr = 0;
ip_d.jp_handler = ujprobe_event_handler;
ip_d.offset = i_ip->addr;
ip_d.pre_handler = ujprobe_event_pre_handler;
ip_d.rp_handler = uretprobe_event_handler;
sspt_file_add_ip(file, &ip_d);
}
}
}
static struct pl_struct *find_pl_struct(struct pf_group *pfg,
struct task_struct *task)
{
struct pl_struct *pls;
list_for_each_entry(pls, &pfg->proc_list, list) {
if (pls->proc->tgid == task->tgid)
return pls;
}
return NULL;
}
static struct sspt_proc *get_proc_by_pfg(struct pf_group *pfg,
struct task_struct *task)
{
struct pl_struct *pls;
pls = find_pl_struct(pfg, task);
if (pls)
return pls->proc;
return NULL;
}
static struct sspt_proc *get_proc_by_pfg_or_new(struct pf_group *pfg,
struct task_struct *task)
{
struct sspt_proc *proc;
proc = get_proc_by_pfg(pfg, task);
if (proc == NULL) {
struct pl_struct *pls;
/* or find?! */
proc = sspt_proc_get_new(task);
copy_proc_form_img_to_sspt(pfg->i_proc, proc);
pls = create_pl_struct(proc);
add_pl_struct(pfg, pls);
}
return proc;
}
/* struct pl_struct */
static struct pf_group *create_pfg(struct proc_filter *filter)
{
struct pf_group *pfg = kmalloc(sizeof(*pfg), GFP_KERNEL);
INIT_LIST_HEAD(&pfg->list);
pfg->filter = filter;
pfg->i_proc = create_img_proc();
INIT_LIST_HEAD(&pfg->proc_list);
return pfg;
}
static void free_pfg(struct pf_group *pfg)
{
/* FIXME: */
kfree(pfg);
}
static void add_pfg_by_list(struct pf_group *pfg)
{
list_add(&pfg->list, &pfg_list);
}
static void del_pfg_by_list(struct pf_group *pfg)
{
list_del(&pfg->list);
}
struct pf_group *get_pf_group_by_dentry(struct dentry *dentry)
{
struct pf_group *pfg;
struct proc_filter *filter;
list_for_each_entry(pfg, &pfg_list, list) {
if (check_pf_by_dentry(pfg->filter, dentry))
return pfg;
}
filter = create_pf_by_dentry(dentry);
pfg = create_pfg(filter);
add_pfg_by_list(pfg);
return pfg;
}
EXPORT_SYMBOL_GPL(get_pf_group_by_dentry);
struct pf_group *get_pf_group_by_tgid(pid_t tgid)
{
struct pf_group *pfg;
struct proc_filter *filter;
list_for_each_entry(pfg, &pfg_list, list) {
if (check_pf_by_tgid(pfg->filter, tgid))
return pfg;
}
filter = create_pf_by_tgid(tgid);
pfg = create_pfg(filter);
add_pfg_by_list(pfg);
return pfg;
}
void put_pf_group(struct pf_group *pfg)
{
}
int pf_register_probe(struct pf_group *pfg, struct dentry *dentry,
unsigned long offset, void *pre_handler,
void *jp_handler, void *rp_handler)
{
return img_proc_add_ip(pfg->i_proc, dentry, offset);
}
EXPORT_SYMBOL_GPL(pf_register_probe);
int pf_unregister_probe(struct pf_group *pfg, struct dentry *dentry,
unsigned long offset)
{
return img_proc_del_ip(pfg->i_proc, dentry, offset);
}
void install_all(void)
{
}
void uninstall_all(void)
{
// struct pf_group *pfg;
// list_for_each_entry(pfg, &pfg_list, list) {
// struct task_struct *task, ts;
//
// rcu_read_lock();
// for_each_process(task) {
// if (task->tgid != task->pid)
// continue;
//
// if (is_kthread(task))
// continue;
//
// ts = check_task_f(pfg->filter, task);
//
// if (ts) {
// proc = sspt_proc_get_by_task_or_new(ts);
// sspt_proc_install(proc);
// }
// }
// rcu_read_unlock();
// }
}
static void install_page_by_pfg(unsigned long addr, struct task_struct *task,
struct pf_group *pfg)
{
struct sspt_proc *proc;
proc = get_proc_by_pfg(pfg, task);
if (proc)
goto install_proc;
task = check_task_f(pfg->filter, task);
if (task) {
proc = get_proc_by_pfg_or_new(pfg, task);
goto install_proc;
}
return;
install_proc:
if (proc->first_install)
sspt_proc_install_page(proc, addr & PAGE_MASK);
else
sspt_proc_install(proc);
}
void call_page_fault(unsigned long addr)
{
struct pf_group *pfg;
struct task_struct *task, *ts;
task = current->group_leader;
if (is_kthread(task))
return;
list_for_each_entry(pfg, &pfg_list, list) {
install_page_by_pfg(addr, task, pfg);
}
}
void call_mm_release(struct task_struct *task)
{
struct sspt_struct *proc;
struct pf_group *pfg;
struct pls_struct *pls;
proc = sspt_proc_get_by_task(task);
if (proc == NULL)
return;
list_for_each_entry(pfg, &pfg_list, list) {
pls = find_pl_struct(pfg, task);
if (pls == NULL)
continue;
sspt_proc_uninstall(proc, task, US_UNREGS_PROBE);
/* FIXME: for many filters */
sspt_proc_free(proc);
free_pl_struct(pls);
}
}
void uninstall_page(unsigned long addr)
{
}
/* debug */
void pfg_print(struct pf_group *pfg)
{
img_proc_print(pfg->i_proc);
}
EXPORT_SYMBOL_GPL(pfg_print);
/* debug */
|