-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracer.c
313 lines (260 loc) · 6.9 KB
/
tracer.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
// SPDX-License-Identifier: GPL-2.0+
/*
* Linux kernel operations surveillant
*
* Author: Cătălin-Alexandru Rîpanu catalin.ripanu@stud.acs.upb.ro
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/sched.h>
#include <linux/wait.h>
#include <linux/hashtable.h>
#include <linux/kprobes.h>
#include <linux/miscdevice.h>
#include <linux/proc_fs.h>
#include <linux/slab.h>
#include <linux/seq_file.h>
#include "tracer.h"
#include "utils.h"
#include "kprobes.h"
static int dev_open(struct inode *inode, struct file *file)
{
return 0;
}
static int dev_release(struct inode *inode, struct file *file)
{
return 0;
}
static ssize_t dev_read(struct file *file, char __user *user_buffer,
size_t size, loff_t *offset)
{
return 0;
}
static ssize_t dev_write(struct file *file, const char __user *user_buffer,
size_t size, loff_t *offset)
{
return size;
}
/**
* ketprobe_ioctl - ioctl interface with two arguments
* @file: file which helps with the kprobes statistics
* @cmd: ioctl command from user space
* @arg: ioctl parameter from user space
*
* If the command is TRACER_ADD_PROCESS then it adds a
* new process structure in process hashtable. This
* operation is thread-safe.
*
* If the command is TRACER_REMOVE_PROCESS then it
* removes a process structure from process hashtable.
* This operation is thread-safe.
*/
static long ketprobe_ioctl(struct file *file, unsigned int cmd,
unsigned long arg)
{
int ret = 0;
ioctl_invk = current->pid;
switch (cmd) {
case TRACER_ADD_PROCESS:
{
struct table_process_node *new_process;
new_process = kmalloc(sizeof(*new_process), GFP_KERNEL);
if (new_process == NULL) {
ret = -ENOMEM;
break;
}
new_process->kmalloc_calls = 0;
new_process->kfree_calls = 0;
new_process->kmalloc_mem = 0;
new_process->kfree_mem = 0;
new_process->sched_calls = 0;
new_process->up_calls = 0;
new_process->down_calls = 0;
new_process->lock_calls = 0;
new_process->unlock_calls = 0;
new_process->pid = (pid_t)arg;
hash_init(new_process->memory_metadata_table);
write_lock(&lock);
hash_add(kernel_process_data_table, &new_process->proc_table, (pid_t)arg);
write_unlock(&lock);
}
break;
case TRACER_REMOVE_PROCESS:
{
struct table_process_node *elem;
struct hlist_node *tmp;
write_lock(&lock);
hash_for_each_possible_safe(kernel_process_data_table, elem, tmp, proc_table, (pid_t)arg) {
if (elem->pid == (pid_t)arg) {
hash_del(&elem->proc_table);
metadata_delete_table(elem);
kfree(elem);
break;
}
}
write_unlock(&lock);
}
break;
default:
ret = -EINVAL;
}
return ret;
}
/**
* Structure for storing all possible operations
* for device.
*/
static const struct file_operations ketprobe_fops = {
.owner = THIS_MODULE,
.open = dev_open,
.release = dev_release,
.read = dev_read,
.write = dev_write,
.unlocked_ioctl = ketprobe_ioctl,
};
/**
* table_proc_show - function for writing kprobes info
* @m: file structure which governs /proc/tracer file
*
* This function prints all of the data from processes in
* hash table. This logic is thread-safe.
*/
static int table_proc_show(struct seq_file *m, void *v)
{
struct table_process_node *elem;
uint32_t val;
read_lock(&lock);
seq_printf(m, PROC_HEADER);
hash_for_each(kernel_process_data_table, val, elem, proc_table) {
seq_printf(m, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
elem->pid, elem->kmalloc_calls, elem->kfree_calls,
elem->kmalloc_mem, elem->kfree_mem,
elem->sched_calls, elem->up_calls, elem->down_calls,
elem->lock_calls, elem->unlock_calls);
}
read_unlock(&lock);
return 0;
}
/**
* list_read_open - uses table_proc_show for writing
* in /proc/tracer
*/
static int list_read_open(struct inode *inode, struct file *file)
{
return single_open(file, table_proc_show, NULL);
}
/**
* Structure for registering procfs operations.
*/
static const struct proc_ops r_pops = {
.proc_open = list_read_open,
.proc_read = seq_read,
.proc_release = single_release,
};
/**
* kprobe_tracer_init - initialize all resources
* used for creating tracing functionality
*
* It frees registered variables if failure is
* present during this init phase.
*/
static int kprobe_tracer_init(void)
{
int ret;
kernel_device_data = (struct miscdevice){
.minor = TRACER_DEV_MINOR,
.name = TRACER_DEV_NAME,
.fops = &ketprobe_fops,
};
ret = misc_register(&kernel_device_data);
if (ret < 0) {
pr_err("device register failed\n");
goto finish_init;
}
ret = register_kretprobe(&kmalloc_kretprobe);
if (ret < 0) {
pr_err("kmalloc register failed\n");
goto reg_kmalloc_failed;
}
ret = register_kretprobe(&kfree_kretprobe);
if (ret < 0) {
pr_err("kfree register failed\n");
goto reg_kfree_failed;
}
ret = register_kretprobe(&schedule_kretprobe);
if (ret < 0) {
pr_err("schedule register failed\n");
goto reg_sched_failed;
}
ret = register_kretprobe(&up_kretprobe);
if (ret < 0) {
pr_err("up register failed\n");
goto reg_up_failed;
}
ret = register_kretprobe(&down_interruptible_kretprobe);
if (ret < 0) {
pr_err("down_interruptible register failed\n");
goto reg_down_failed;
}
ret = register_kretprobe(&mutex_lock_kretprobe);
if (ret < 0) {
pr_err("mutex_lock register failed\n");
goto reg_lock_failed;
}
ret = register_kretprobe(&mutex_unlock_kretprobe);
if (ret < 0) {
pr_err("mutex_unlock register failed\n");
goto reg_unlock_failed;
}
proc_read = proc_create(procfs_file_read, 0000, proc_read, &r_pops);
if (!proc_read) {
ret = -ENOMEM;
goto proc_list_cleanup;
}
goto finish_init;
proc_list_cleanup:
proc_remove(proc_read);
reg_unlock_failed:
unregister_kretprobe(&mutex_unlock_kretprobe);
reg_lock_failed:
unregister_kretprobe(&mutex_lock_kretprobe);
reg_down_failed:
unregister_kretprobe(&down_interruptible_kretprobe);
reg_up_failed:
unregister_kretprobe(&up_kretprobe);
reg_sched_failed:
unregister_kretprobe(&schedule_kretprobe);
reg_kfree_failed:
unregister_kretprobe(&kfree_kretprobe);
reg_kmalloc_failed:
unregister_kretprobe(&kmalloc_kretprobe);
finish_init:
return ret;
}
/**
* kprobe_tracer_exit - function which frees registers
* purges global process hastable, removes misc device
* and destroys procfs file.
*/
static void kprobe_tracer_exit(void)
{
proc_remove(proc_read);
misc_deregister(&kernel_device_data);
unregister_kretprobe(&kmalloc_kretprobe);
unregister_kretprobe(&kfree_kretprobe);
unregister_kretprobe(&schedule_kretprobe);
unregister_kretprobe(&up_kretprobe);
unregister_kretprobe(&down_interruptible_kretprobe);
unregister_kretprobe(&mutex_lock_kretprobe);
unregister_kretprobe(&mutex_unlock_kretprobe);
purge_kernel_table();
}
module_init(kprobe_tracer_init);
module_exit(kprobe_tracer_exit);
MODULE_DESCRIPTION("Kernel operations surveillant");
MODULE_AUTHOR("Catalin-Alexandru Ripanu catalin.ripanu@stud.acs.upb.ro");
MODULE_LICENSE("GPL v2");