1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <unistd.h>
18#include <pid_filter.h>
19
20
21struct bpf_map SEC("maps") __augmented_syscalls__ = {
22 .type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
23 .key_size = sizeof(int),
24 .value_size = sizeof(u32),
25 .max_entries = __NR_CPUS__,
26};
27
28struct syscall {
29 bool enabled;
30};
31
32struct bpf_map SEC("maps") syscalls = {
33 .type = BPF_MAP_TYPE_ARRAY,
34 .key_size = sizeof(int),
35 .value_size = sizeof(struct syscall),
36 .max_entries = 512,
37};
38
39struct syscall_enter_args {
40 unsigned long long common_tp_fields;
41 long syscall_nr;
42 unsigned long args[6];
43};
44
45struct syscall_exit_args {
46 unsigned long long common_tp_fields;
47 long syscall_nr;
48 long ret;
49};
50
51struct augmented_filename {
52 unsigned int size;
53 int reserved;
54 char value[256];
55};
56
57#define SYS_OPEN 2
58#define SYS_ACCESS 21
59#define SYS_OPENAT 257
60
61pid_filter(pids_filtered);
62
63SEC("raw_syscalls:sys_enter")
64int sys_enter(struct syscall_enter_args *args)
65{
66 struct {
67 struct syscall_enter_args args;
68 struct augmented_filename filename;
69 } augmented_args;
70 struct syscall *syscall;
71 unsigned int len = sizeof(augmented_args);
72 const void *filename_arg = NULL;
73
74 if (pid_filter__has(&pids_filtered, getpid()))
75 return 0;
76
77 probe_read(&augmented_args.args, sizeof(augmented_args.args), args);
78
79 syscall = bpf_map_lookup_elem(&syscalls, &augmented_args.args.syscall_nr);
80 if (syscall == NULL || !syscall->enabled)
81 return 0;
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 switch (augmented_args.args.syscall_nr) {
123 case SYS_ACCESS:
124 case SYS_OPEN: filename_arg = (const void *)args->args[0];
125 __asm__ __volatile__("": : :"memory");
126 break;
127 case SYS_OPENAT: filename_arg = (const void *)args->args[1];
128 break;
129 }
130
131 if (filename_arg != NULL) {
132 augmented_args.filename.reserved = 0;
133 augmented_args.filename.size = probe_read_str(&augmented_args.filename.value,
134 sizeof(augmented_args.filename.value),
135 filename_arg);
136 if (augmented_args.filename.size < sizeof(augmented_args.filename.value)) {
137 len -= sizeof(augmented_args.filename.value) - augmented_args.filename.size;
138 len &= sizeof(augmented_args.filename.value) - 1;
139 }
140 } else {
141 len = sizeof(augmented_args.args);
142 }
143
144 perf_event_output(args, &__augmented_syscalls__, BPF_F_CURRENT_CPU, &augmented_args, len);
145 return 0;
146}
147
148SEC("raw_syscalls:sys_exit")
149int sys_exit(struct syscall_exit_args *args)
150{
151 struct syscall_exit_args exit_args;
152 struct syscall *syscall;
153
154 if (pid_filter__has(&pids_filtered, getpid()))
155 return 0;
156
157 probe_read(&exit_args, sizeof(exit_args), args);
158
159 syscall = bpf_map_lookup_elem(&syscalls, &exit_args.syscall_nr);
160 if (syscall == NULL || !syscall->enabled)
161 return 0;
162
163 return 1;
164}
165
166license(GPL);
167