-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.bpf.c
74 lines (61 loc) · 1.85 KB
/
main.bpf.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
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h>
#include "main.h"
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 10240);
__type(key, pid_t);
__type(value, struct event_t);
} entries SEC(".maps");
struct {
__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
__uint(key_size, sizeof(u32));
__uint(value_size, sizeof(u32));
} events SEC(".maps");
static void get_file_path(const struct file *file, char *buf, size_t size)
{
struct qstr dname;
dname = BPF_CORE_READ(file, f_path.dentry, d_name);
bpf_probe_read_kernel(buf, size, dname.name);
}
static inline bool str_eq(const char *a, const char *b, int len)
{
for (int i = 0; i < len; i++) {
if (a[i] != b[i])
return false;
if (a[i] == '\0')
break;
}
return true;
}
static __always_inline int str_len(char *s, int max_len)
{
for (int i = 0; i < max_len; i++) {
if (s[i] == '\0')
return i;
}
if (s[max_len - 1] != '\0')
return max_len;
return 0;
}
SEC("lsm/file_open")
int BPF_PROG(lsm_file_open, struct file *file) {
struct event_t event = {};
char target_comm[TASK_COMM_LEN] = "cat";
event.pid = bpf_get_current_pid_tgid() >> 32;
bpf_get_current_comm(&event.comm, sizeof(event.comm));
// 决策是否需要拦截当前事件 ...
if (!str_eq(event.comm, target_comm, str_len(target_comm, TASK_COMM_LEN)))
return 0;
// 获取打开模式
event.fmode = BPF_CORE_READ(file, f_mode);
// 获取文件名称
get_file_path(file, event.filename, sizeof(event.filename));
// 将事件提交到 events 中供用户态程序消费
bpf_perf_event_output(ctx, &events, BPF_F_CURRENT_CPU, &event, sizeof(event));
// 拦截
return -1;
}
char _license[] SEC("license") = "GPL";