-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfilter.cpp
248 lines (205 loc) · 5.67 KB
/
filter.cpp
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
#include <string>
#include <errno.h>
#include <stdlib.h>
#include <sys/time.h>
#include "config.h"
#include "filter.h"
#include "sjail.h"
#include "memory.h"
#include "process_state.h"
#include "report.h"
static bool first_call = true;
static filter_action filter_syscall_enter(pid_data& pdata, process_state& st) {
if(get_report() && get_log_level() >= 5) {
log_info(pdata.pid, 5, std::string("syscall ") +
st.get_syscall_name(st.get_syscall()));
}
bool block = false;
bool permit = false;
bool save = false;
for(auto i = pdata.filters.begin();
i != pdata.filters.end() && !block && !permit; ++i) {
switch((*i)->filter_syscall_enter(pdata, st)) {
case FILTER_KILL_PID: return FILTER_KILL_PID;
case FILTER_KILL_ALL: return FILTER_KILL_ALL;
case FILTER_BLOCK_SYSCALL: save = block = true; break;
case FILTER_PERMIT_SYSCALL: permit = true; break;
case FILTER_CHANGED_SYSCALL: save = true; break;
default: break;
}
}
block |= !permit;
if(save && !get_passive()) {
st.set_result(-EPERM);
pdata.restore_state = new process_state(st);
}
if(block && !get_passive()) {
log_blocked_syscall(st);
st.set_syscall(sys_getpid);
st.save();
if(st.error()) {
log_error(pdata.pid, "failed to block syscall");
return FILTER_KILL_PID;
}
}
return FILTER_NO_ACTION;
}
static filter_action filter_syscall_exit(pid_data& pdata, process_state& st) {
for(auto i : pdata.filters) {
switch(i->filter_syscall_exit(pdata, st)) {
case FILTER_KILL_PID: return FILTER_KILL_PID;
case FILTER_KILL_ALL: return FILTER_KILL_ALL;
default: break;
}
}
if(pdata.restore_state) {
st = *pdata.restore_state;
delete pdata.restore_state;
pdata.restore_state = NULL;
st.save();
if(st.error()) {
return FILTER_KILL_PID;
}
}
return FILTER_NO_ACTION;
}
filter_action filter_system_call(pid_data& pdata) {
process_state st(pdata.pid);
if(st.error()) {
log_error(pdata.pid, "ptrace_getregs failed");
return get_passive() ? FILTER_NO_ACTION : FILTER_KILL_PID;
}
/* We expect the very first filtered system call to be execve. */
if(first_call) {
/* TODO: I'm not totally sure what happens when the execution mode of jail
* and the execution mode of the client process don't match on this first
* exec. For now we'll just assume nothing went wrong and the first result
* is alwasy from exec. */
if(true || st.get_syscall() == sys_execve) {
first_call = false;
pdata.enter_call = false;
return FILTER_NO_ACTION;
}
log_error(pdata.pid, "first system call not execve");
return FILTER_KILL_ALL;
}
safemem_reset(pdata);
filter_action action;
pdata.enter_call = !pdata.enter_call;
if(pdata.enter_call) {
action = filter_syscall_enter(pdata, st);
} else {
action = filter_syscall_exit(pdata, st);
}
return get_passive() ? FILTER_NO_ACTION : action;
}
std::list<filter*> create_root_filters() {
std::list<filter*> filters;
filters.push_back(new memory_filter());
if(!get_files().empty()) {
filters.push_back(new file_filter());
}
if(!get_exec_match().empty() || get_processes() != 0 || get_threads() != 0) {
filters.push_back(new exec_filter());
}
if(get_net()) {
filters.push_back(new net_filter());
}
filters.push_back(new base_filter());
return filters;
}
std::list<filter*> clone_filters(const std::list<filter*>& filters) {
std::list<filter*> res;
for(std::list<filter*>::const_iterator it = filters.begin();
it != filters.end(); it++) {
res.push_back((*it)->on_clone());
}
return res;
}
std::list<filter*> fork_filters(const std::list<filter*>& filters) {
std::list<filter*> res;
for(std::list<filter*>::const_iterator it = filters.begin();
it != filters.end(); it++) {
res.push_back((*it)->on_fork());
}
return res;
}
filter::filter() : refs(1) {
}
filter::~filter() {
}
filter* filter::ref() {
++refs;
return this;
}
bool filter::unref() {
return --refs == 0;
}
void filter::on_exit(pid_data& pdata, exit_data& data) {
}
filter* filter::on_clone() {
return ref();
}
filter* filter::on_fork() {
return ref();
}
filter_action filter::filter_syscall_enter(pid_data& pdata, process_state& st) {
return FILTER_NO_ACTION;
}
filter_action filter::filter_syscall_exit(pid_data& pdata, process_state& st) {
return FILTER_NO_ACTION;
}
static unsigned long long query_wall_time_us() {
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000000LL + tv.tv_usec;
}
base_filter::base_filter() {
start_wall_time = query_wall_time_us();
}
base_filter::~base_filter() {
}
void base_filter::on_exit(pid_data& pdata, exit_data& data) {
data.wall_time_us = query_wall_time_us() - start_wall_time;
}
filter* base_filter::on_clone() {
return ref();
}
filter* base_filter::on_fork() {
return new base_filter();
}
filter_action base_filter::filter_syscall_enter(pid_data& pdata,
process_state& st) {
switch(st.get_syscall()) {
case sys_brk: break;
case sys_exit: break;
case sys_exit_group: break;
case sys_close: break;
case sys_read:
case sys_write:
case sys_readv:
case sys_writev:
case sys_preadv:
case sys_pwritev:
break;
case sys_arch_prctl:
case sys_set_thread_area:
case sys_get_thread_area:
break;
case sys_getpid:
case sys_getppid:
case sys_getuid:
case sys_geteuid:
case sys_getresuid:
case sys_getgid:
case sys_getegid:
case sys_getresgid:
break;
case sys_futex:
case sys_nanosleep:
break;
default:
return FILTER_NO_ACTION;
}
return FILTER_PERMIT_SYSCALL;
}