-
Notifications
You must be signed in to change notification settings - Fork 160
/
interesting_syscalls.cpp
438 lines (338 loc) · 7.6 KB
/
interesting_syscalls.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/*
Copyright (C) 2023 The Falco Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <gtest/gtest.h>
#include <sinsp.h>
#include <sys/syscall.h>
#include "../test_utils.h"
// We need to include syscall compat tables
#ifdef __x86_64__
#include "syscall_compat_x86_64.h"
#elif __aarch64__
#include "syscall_compat_aarch64.h"
#elif __s390x__
#include "syscall_compat_s390x.h"
#endif /* __x86_64__ */
/* Please note this set must be kept in sync if we update the sinsp internal state set
* otherwise some of the following checks will fail.
*/
libsinsp::events::set<ppm_sc_code> state_sc_set_truth = {
#ifdef __NR_accept
PPM_SC_ACCEPT,
#endif
#ifdef __NR_accept4
PPM_SC_ACCEPT4,
#endif
#ifdef __NR_bind
PPM_SC_BIND,
#endif
#ifdef __NR_capset
PPM_SC_CAPSET,
#endif
#ifdef __NR_chdir
PPM_SC_CHDIR,
#endif
#ifdef __NR_chroot
PPM_SC_CHROOT,
#endif
#ifdef __NR_clone
PPM_SC_CLONE,
#endif
#ifdef __NR_clone3
PPM_SC_CLONE3,
#endif
#ifdef __NR_close
PPM_SC_CLOSE,
#endif
#ifdef __NR_connect
PPM_SC_CONNECT,
#endif
#ifdef __NR_creat
PPM_SC_CREAT,
#endif
#ifdef __NR_dup
PPM_SC_DUP,
#endif
#ifdef __NR_dup2
PPM_SC_DUP2,
#endif
#ifdef __NR_dup3
PPM_SC_DUP3,
#endif
#ifdef __NR_eventfd
PPM_SC_EVENTFD,
#endif
#ifdef __NR_eventfd2
PPM_SC_EVENTFD2,
#endif
#ifdef __NR_execve
PPM_SC_EXECVE,
#endif
#ifdef __NR_execveat
PPM_SC_EXECVEAT,
#endif
#ifdef __NR_fchdir
PPM_SC_FCHDIR,
#endif
#ifdef __NR_fcntl
PPM_SC_FCNTL,
#endif
#ifdef __NR_fcntl64
PPM_SC_FCNTL64,
#endif
#ifdef __NR_fork
PPM_SC_FORK,
#endif
#ifdef __NR_inotify_init
PPM_SC_INOTIFY_INIT,
#endif
#ifdef __NR_inotify_init1
PPM_SC_INOTIFY_INIT1,
#endif
#ifdef __NR_io_uring_setup
PPM_SC_IO_URING_SETUP,
#endif
#ifdef __NR_mount
PPM_SC_MOUNT,
#endif
#ifdef __NR_open
PPM_SC_OPEN,
#endif
#ifdef __NR_open_by_handle_at
PPM_SC_OPEN_BY_HANDLE_AT,
#endif
#ifdef __NR_openat
PPM_SC_OPENAT,
#endif
#ifdef __NR_openat2
PPM_SC_OPENAT2,
#endif
#ifdef __NR_pipe
PPM_SC_PIPE,
#endif
#ifdef __NR_pipe2
PPM_SC_PIPE2,
#endif
#ifdef __NR_prlimit64
PPM_SC_PRLIMIT64,
#endif
#ifdef __NR_recvfrom
PPM_SC_RECVFROM,
#endif
#ifdef __NR_recvmsg
PPM_SC_RECVMSG,
#endif
#ifdef __NR_getsockopt
PPM_SC_GETSOCKOPT, /// TODO: In the next future probably we could remove this from the state
#endif
#ifdef __NR_sendmsg
PPM_SC_SENDMSG,
#endif
#ifdef __NR_sendto
PPM_SC_SENDTO,
#endif
#ifdef __NR_setgid
PPM_SC_SETGID,
#endif
#ifdef __NR_setgid32
PPM_SC_SETGID32,
#endif
#ifdef __NR_setpgid
PPM_SC_SETPGID,
#endif
#ifdef __NR_setresgid
PPM_SC_SETRESGID,
#endif
#ifdef __NR_setresgid32
PPM_SC_SETRESGID32,
#endif
#ifdef __NR_setresuid
PPM_SC_SETRESUID,
#endif
#ifdef __NR_setresuid32
PPM_SC_SETRESUID32,
#endif
#ifdef __NR_setrlimit
PPM_SC_SETRLIMIT,
#endif
#ifdef __NR_setsid
PPM_SC_SETSID,
#endif
#ifdef __NR_setuid
PPM_SC_SETUID,
#endif
#ifdef __NR_setuid32
PPM_SC_SETUID32,
#endif
#ifdef __NR_shutdown
PPM_SC_SHUTDOWN,
#endif
#ifdef __NR_signalfd
PPM_SC_SIGNALFD,
#endif
#ifdef __NR_signalfd4
PPM_SC_SIGNALFD4,
#endif
#ifdef __NR_socket
PPM_SC_SOCKET,
#endif
#ifdef __NR_socketpair
PPM_SC_SOCKETPAIR,
#endif
#ifdef __NR_timerfd_create
PPM_SC_TIMERFD_CREATE,
#endif
#ifdef __NR_umount2
PPM_SC_UMOUNT2,
#endif
#ifdef __NR_userfaultfd
PPM_SC_USERFAULTFD,
#endif
#ifdef __NR_vfork
PPM_SC_VFORK,
#endif
#ifdef __NR_epoll_create
PPM_SC_EPOLL_CREATE,
#endif
#ifdef __NR_epoll_create1
PPM_SC_EPOLL_CREATE1,
#endif
};
TEST(interesting_syscalls, sinsp_state_sc_set)
{
auto state_sc_set = libsinsp::events::sinsp_state_sc_set();
ASSERT_PPM_SC_CODES_EQ(state_sc_set_truth, state_sc_set);
}
TEST(interesting_syscalls, sinsp_state_sc_set_additional_syscalls)
{
libsinsp::events::set<ppm_sc_code> additional_syscalls_truth;
auto sc_set_truth = state_sc_set_truth;
#ifdef __NR_kill
additional_syscalls_truth.insert(PPM_SC_KILL);
sc_set_truth.insert(PPM_SC_KILL);
#endif
#ifdef __NR_read
additional_syscalls_truth.insert(PPM_SC_READ);
sc_set_truth.insert(PPM_SC_READ);
#endif
auto sinsp_state_set = libsinsp::events::sinsp_state_sc_set();
auto sc_set = additional_syscalls_truth.merge(sinsp_state_set);
auto additional_syscalls = additional_syscalls_truth.diff(sinsp_state_set);
ASSERT_PPM_SC_CODES_EQ(sc_set_truth, sc_set);
ASSERT_PPM_SC_CODES_EQ(additional_syscalls_truth, additional_syscalls);
}
TEST(interesting_syscalls, io_sc_set)
{
libsinsp::events::set<ppm_sc_code> io_sc_set_truth;
#ifdef __NR_read
io_sc_set_truth.insert(PPM_SC_READ);
#endif
#ifdef __NR_recv
io_sc_set_truth.insert(PPM_SC_RECV);
#endif
#ifdef __NR_recvfrom
io_sc_set_truth.insert(PPM_SC_RECVFROM);
#endif
#ifdef __NR_recvmsg
io_sc_set_truth.insert(PPM_SC_RECVMSG);
#endif
#ifdef __NR_recvmmsg
io_sc_set_truth.insert(PPM_SC_RECVMMSG);
#endif
#ifdef __NR_readv
io_sc_set_truth.insert(PPM_SC_READV);
#endif
#ifdef __NR_preadv
io_sc_set_truth.insert(PPM_SC_PREADV);
#endif
#ifdef __NR_write
io_sc_set_truth.insert(PPM_SC_WRITE);
#endif
#ifdef __NR_pwrite
io_sc_set_truth.insert(PPM_SC_PWRITE);
#endif
#ifdef __NR_writev
io_sc_set_truth.insert(PPM_SC_WRITEV);
#endif
#ifdef __NR_pwritev
io_sc_set_truth.insert(PPM_SC_PWRITEV);
#endif
#ifdef __NR_sendfile
io_sc_set_truth.insert(PPM_SC_SENDFILE);
#endif
#ifdef __NR_send
io_sc_set_truth.insert(PPM_SC_SEND);
#endif
#ifdef __NR_sendto
io_sc_set_truth.insert(PPM_SC_SENDTO);
#endif
#ifdef __NR_sendmsg
io_sc_set_truth.insert(PPM_SC_SENDMSG);
#endif
#ifdef __NR_sendmmsg
io_sc_set_truth.insert(PPM_SC_SENDMMSG);
#endif
#ifdef __NR_pread64
io_sc_set_truth.insert(PPM_SC_PREAD64);
#endif
#ifdef __NR_pwrite64
io_sc_set_truth.insert(PPM_SC_PWRITE64);
#endif
auto io_sc_set = libsinsp::events::io_sc_set();
ASSERT_PPM_SC_CODES_EQ(io_sc_set_truth, io_sc_set);
}
TEST(interesting_syscalls, all_sc_set)
{
auto sc_set = libsinsp::events::all_sc_set();
/* Assert that all the syscalls are taken */
ASSERT_EQ(sc_set.size(), PPM_SC_MAX);
}
TEST(interesting_syscalls, sc_set_to_names)
{
// "syncfs" is a generic event / syscall
static std::set<std::string> names_truth = {"kill", "read", "syncfs"};
static libsinsp::events::set<ppm_sc_code> sc_set = {PPM_SC_KILL, PPM_SC_READ, PPM_SC_SYNCFS};
auto names = test_utils::unordered_set_to_ordered(libsinsp::events::sc_set_to_names(sc_set));
ASSERT_NAMES_EQ(names_truth, names);
}
TEST(interesting_syscalls, names_to_sc_set)
{
// "syncfs" is a generic event / syscall
static libsinsp::events::set<ppm_sc_code> sc_set_truth = {PPM_SC_KILL, PPM_SC_READ, PPM_SC_SYNCFS};
auto sc_set = libsinsp::events::names_to_sc_set(std::unordered_set<std::string>{"kill", "read", "syncfs"});
ASSERT_PPM_SC_CODES_EQ(sc_set_truth, sc_set);
}
// API limitations -> we can only map events to syscalls
// when a 1:1 mapping is defined, we only test for 1:1 mapping cases
TEST(interesting_syscalls, event_set_to_sc_set)
{
libsinsp::events::set<ppm_sc_code> sc_set_truth = {
#ifdef __NR_kill
PPM_SC_KILL,
#endif
#ifdef __NR_sendto
PPM_SC_SENDTO,
#endif
};
libsinsp::events::set<ppm_event_code> event_set = {
#ifdef __NR_kill
PPME_SYSCALL_KILL_E,
PPME_SYSCALL_KILL_X,
#endif
#ifdef __NR_sendto
PPME_SOCKET_SENDTO_E,
PPME_SOCKET_SENDTO_X,
#endif
};
auto sc_set = libsinsp::events::event_set_to_sc_set(event_set);
ASSERT_PPM_SC_CODES_EQ(sc_set_truth, sc_set);
}