forked from 3intermute/linux_syscall_hook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtmp.txt
More file actions
111 lines (95 loc) · 3.03 KB
/
tmp.txt
File metadata and controls
111 lines (95 loc) · 3.03 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
#include <linux/ftrace.h>
#include <linux/linkage.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/kprobes.h>
#include "debug.h"
// struct ftrace_hook {
// const char *name;
// void *new;
// void *orig;
//
// unsigned long addr;
// struct ftrace_ops ops;
// };
//
// typedef unsigned long (*kallsyms_lookup_name_t)(const char *name);
// // in ftrace.h but not exported lol
// kallsyms_lookup_name_t kallsyms_lookup_name_ = NULL;
//
// int get_kallsyms_lookup_name(void) {
// static struct kprobe kp = {.symbol_name = "kallsyms_lookup_name"};
// if (register_kprobe(&kp) < 0) {
// return -ENOENT;
// }
//
// kallsyms_lookup_name_ = (kallsyms_lookup_name_t) kp.addr;
// pr_info("debug: kallsyms_lookup_name_ %p\n", kallsyms_lookup_name_);
// unregister_kprobe(&kp);
// return 0;
// }
int get_addr_of_symbol(const char *symbol_name) {
static struct kprobe kp = {.symbol_name = symbol_name};
if (register_kprobe(&kp) < 0) {
return -ENOENT;
}
unsigned long tmp = kp.addr;
pr_info("debug: get_addr_of_symbol %p\n", tmp);
unregister_kprobe(&kp);
return tmp;
}
static int fh_get_func_addr(struct ftrace_hook *hook) {
// if (!kallsyms_lookup_name_) {
// get_kallsyms_lookup_name();
// }
hook->addr = get_addr_of_symbol(hook->name);
pr_info("debug: hook->name %s\n", hook->name);
pr_info("debug: hook->addr %p\n", hook->addr);
if (!hook->addr) {
return -ENOENT;
}
*((unsigned long*) hook->orig) = hook->addr;
return 0;
}
static void fh_callback(unsigned long ip, unsigned long parent_ip, struct ftrace_ops *ops, struct pt_regs *regs) {
struct ftrace_hook *hook = container_of(ops, struct ftrace_hook, ops);
if (!within_module(parent_ip, THIS_MODULE)) {
regs->pc = (unsigned long) hook->new;
pr_info("debug: reached end of fh_callback\n");
}
}
int fh_install_hook(struct ftrace_hook *hook) {
if (!hook) {
return -ENOENT;
}
int err;
err = fh_get_func_addr(hook);
if (err) {
pr_info("debug: fh_get_func_addr failed\n");
return err;
}
hook->ops.func = fh_callback;
hook->ops.flags = FTRACE_OPS_FL_SAVE_REGS
| FTRACE_OPS_FL_RECURSION_SAFE
| FTRACE_OPS_FL_IPMODIFY;
pr_info("debug: hook state after set func + flag");
debug_fh_hook(hook);
// https://www.kernel.org/doc/html/v5.4/trace/ftrace-uses.html
// hook->addr doesnt match /proc/kallsym
err = ftrace_set_filter_ip(&hook->ops, hook->addr, 0, 0);
// err = ftrace_set_filter(&(hook->ops), hook->name, strlen(hook->name), 0);
if (err) {
pr_info("debug: ftrace_set_filter_ip failed\n");
return err;
}
err = register_ftrace_function(&hook->ops);
if (err) {
pr_info("debug: register_ftrace_function failed\n");
return err;
}
return 0;
}
void fh_remove_hook(struct ftrace_hook *hook) {
unregister_ftrace_function(&hook->ops);
ftrace_set_filter_ip(&hook->ops, hook->addr, 1, 0);
}