1 #include <linux/module.h>
4 #include <dbi_kprobes.h>
5 #include "ks_features.h"
6 #include "syscall_list.h"
7 #include "features_data.c"
16 #define CREATE_RP(name) \
18 .entry_handler = NULL, \
22 #define X(name, args) #name
23 static const char *const syscall_name[] = {
29 syscall_name_cnt = sizeof(syscall_name) / sizeof(char *)
33 #define X(name, args__) \
35 .rp = CREATE_RP(name), \
40 static struct ks_probe ksp[] = {
46 static char *get_sys_name(size_t id)
48 return syscall_name[id];
51 static int get_counter(size_t id)
53 return ksp[id].counter;
56 static void inc_counter(size_t id)
61 static void dec_counter(size_t id)
66 /* ========================= HEANDLERS ========================= */
72 static int entry_handler(struct kretprobe_instance *ri, struct pt_regs *regs, void *priv_arg)
74 struct ks_probe *ksp = (struct ks_probe *)priv_arg;
76 pack_event_info(KS_PROBE_ID, RECORD_ENTRY, "ps", ksp->rp.kp.addr, ksp->args);
81 static int ret_handler(struct kretprobe_instance *ri, struct pt_regs *regs, void *priv_arg)
84 struct ks_probe *ksp = (struct ks_probe *)priv_arg;
86 ret_val = regs_return_value(regs);
87 pack_event_info(KS_PROBE_ID, RECORD_RET, "pd", ksp->rp.kp.addr, ret_val);
91 /* ========================= HEANDLERS ========================= */
95 static int register_syscall(size_t id)
98 printk("register_syscall: %s\n", get_sys_name(id));
100 if (ksp[id].rp.kp.addr == NULL)
103 ksp[id].rp.entry_handler = entry_handler;
104 ksp[id].rp.handler = ret_handler;
105 ksp[id].rp.priv_arg = &ksp[id];
107 ret = dbi_register_kretprobe(&ksp[id].rp);
112 static int unregister_syscall(size_t id)
114 printk("unregister_syscall: %s\n", get_sys_name(id));
116 if (ksp[id].rp.kp.addr == NULL)
119 dbi_unregister_kretprobe(&ksp[id].rp);
124 static int install_features(struct feature *f)
128 for (i = 0; i < f->cnt; ++i) {
129 id = f->feature_list[i];
131 if (get_counter(id) == 0) {
132 int ret = register_syscall(id);
145 static int uninstall_features(struct feature *f)
149 for (i = 0; i < f->cnt; ++i) {
150 id = f->feature_list[i];
152 if (get_counter(id) == 0) {
159 if (get_counter(id) == 0) {
160 int ret = unregister_syscall(id);
171 static struct feature *get_feature(enum feature_id id)
173 if (id < 0 || id >= (int)feature_cnt)
176 return &features[id];
179 int set_feature(enum feature_id id)
181 struct feature *f = get_feature(id);
186 return install_features(f);
188 EXPORT_SYMBOL_GPL(set_feature);
190 int unset_feature(enum feature_id id)
192 struct feature *f = get_feature(id);
197 return uninstall_features(f);
199 EXPORT_SYMBOL_GPL(unset_feature);
201 static int __init init_ks_feature(void)
204 unsigned long addr, ni_syscall;
207 ni_syscall = swap_ksyms("sys_ni_syscall");
209 for (i = 0; i < syscall_name_cnt; ++i) {
210 name = get_sys_name(i);
211 addr = swap_ksyms(name);
213 printk("%s() not found\n", name);
217 if (ni_syscall == addr) {
218 printk("INFO: %s is not install\n", get_sys_name(i));
222 ksp[i].rp.kp.addr = addr;
228 static void __exit exit_ks_feature(void)
232 module_init(init_ks_feature);
233 module_exit(exit_ks_feature);
235 MODULE_LICENSE("GPL");
238 static void print_feature(struct feature *f)
242 for (i = 0; i < f->cnt; ++i) {
243 printk(" feature[%3u]: %s\n", i, get_sys_name(f->feature_list[i]));
247 void print_features(void)
251 printk("print_features:\n");
252 for (i = 0; i < feature_cnt; ++i) {
253 printk("feature: %d\n", i);
254 print_feature(&features[i]);
258 void print_all_syscall(void)
262 printk("SYSCALL:\n");
263 for (i = 0; i < syscall_name_cnt; ++i) {
264 printk(" [%2d] %s\n", get_counter(i), get_sys_name(i));