forked from falcosecurity/libs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart_tests.cpp
330 lines (295 loc) · 8.75 KB
/
start_tests.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
#include <iostream>
#include <string>
#include <scap.h>
#include <scap_engines.h>
#include <scap_vtable.h>
#include <getopt.h>
#include <gtest/gtest.h>
#include "./event_class/event_class.h"
#include "strl.h"
/* We support only these arguments */
#define HELP_OPTION "help"
#define VERBOSE_OPTION "verbose"
#define KMOD_OPTION "kmod"
#define BPF_OPTION "bpf"
#define MODERN_BPF_OPTION "modern-bpf"
#define BUFFER_OPTION "buffer-dim"
#define BPF_PROBE_DEFAULT_PATH "/driver/bpf/probe.o"
#define KMOD_DEFAULT_PATH "/driver/scap.ko"
#define KMOD_NAME "scap"
scap_t* event_test::s_scap_handle = NULL;
static enum falcosecurity_log_severity severity_level = FALCOSECURITY_LOG_SEV_WARNING;
int remove_kmod()
{
if(syscall(__NR_delete_module, KMOD_NAME, O_NONBLOCK))
{
switch(errno)
{
case ENOENT:
return EXIT_SUCCESS;
/* If a module has a nonzero reference count with `O_NONBLOCK` flag
* the call returns immediately, with `EWOULDBLOCK` code. So in that
* case we wait until the module is detached.
*/
case EWOULDBLOCK:
for(int i = 0; i < 4; i++)
{
int ret = syscall(__NR_delete_module, KMOD_NAME, O_NONBLOCK);
if(ret == 0 || errno == ENOENT)
{
return EXIT_SUCCESS;
}
sleep(1);
}
return EXIT_FAILURE;
case EBUSY:
case EFAULT:
case EPERM:
std::cerr << "Unable to remove kernel module. Errno message: " << strerror(errno) << ", errno: " << errno << std::endl;
return EXIT_FAILURE;
default:
std::cerr << "Unexpected error code. Errno message: " << strerror(errno) << ", errno: " << errno << std::endl;
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
int insert_kmod(const std::string& kmod_path)
{
/* Here we want to insert the module if we fail we need to abort the program. */
int fd = open(kmod_path.c_str(), O_RDONLY);
if(fd < 0)
{
std::cout << "Unable to open the kmod file. Errno message: " << strerror(errno) << ", errno: " << errno << std::endl;
return EXIT_FAILURE;
}
if(syscall(__NR_finit_module, fd, "", 0))
{
std::cerr << "Unable to inject the kmod. Errno message: " << strerror(errno) << ", errno: " << errno << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
void abort_if_already_configured(const struct scap_vtable* vtable)
{
if(vtable != nullptr)
{
std::cerr << "* '" << vtable->name << "' engine is already configured. Please specify just one engine!" << std::endl;
exit(EXIT_FAILURE);
}
}
void test_open_log_fn(const char* component, const char* msg, const enum falcosecurity_log_severity sev)
{
if(sev <= severity_level)
{
if(component!= NULL)
{
printf("%s: %s", component, msg);
}
else
{
// libbpf logs have no components
printf("%s", msg);
}
}
}
void print_message(std::string msg)
{
std::cout << std::endl;
std::cout << "-----------------------------------------------------" << std::endl;
std::cout << "- " << msg << std::endl;
std::cout << "-----------------------------------------------------" << std::endl;
std::cout << std::endl;
}
void print_menu_and_exit()
{
std::string usage = R"(Usage: drivers_test [options]
Overview: The goal of this binary is to run tests against one of our drivers.
Options:
-k, --kmod <path> Run tests against the kernel module. Default path is `./driver/scap.ko`.
-m, --modern-bpf Run tests against the modern bpf probe.
-b, --bpf <path> Run tests against the bpf probe. Default path is `./driver/bpf/probe.o`.
-d, --buffer-dim <dim> Change the dimension of shared buffers between userspace and kernel. You must specify the dimension in bytes.
-v, --verbose <level> Print all available logs. Default level is WARNING (4).
-h, --help This page.
)";
std::cout << usage << std::endl;
exit(EXIT_SUCCESS);
}
int open_engine(int argc, char** argv)
{
static struct option long_options[] = {
{BPF_OPTION, optional_argument, 0, 'b'},
{MODERN_BPF_OPTION, no_argument, 0, 'm'},
{KMOD_OPTION, optional_argument, 0, 'k'},
{BUFFER_OPTION, required_argument, 0, 'd'},
{HELP_OPTION, no_argument, 0, 'h'},
{VERBOSE_OPTION, required_argument, 0, 'v'},
{0, 0, 0, 0}};
// They should live until we call 'scap_open'
struct scap_modern_bpf_engine_params modern_bpf_params = {0};
struct scap_bpf_engine_params bpf_params = {0};
struct scap_kmod_engine_params kmod_params = {0};
int ret = 0;
const struct scap_vtable* vtable = nullptr;
scap_open_args oargs = {};
oargs.log_fn = test_open_log_fn;
unsigned long buffer_bytes_dim = DEFAULT_DRIVER_BUFFER_BYTES_DIM;
std::string kmod_path;
/* Remove kmod if injected, we remove it always even if we use another engine
* in this way we are sure the unique driver in the system is the one we will use.
*/
if(remove_kmod())
{
return EXIT_FAILURE;
}
/* Get current cwd as a base directory for the driver path */
char driver_path[FILENAME_MAX];
if(!getcwd(driver_path, FILENAME_MAX))
{
std::cerr << "Unable to get current dir" << std::endl;
return EXIT_FAILURE;
}
/* Parse CLI options */
int op = 0;
int long_index = 0;
while((op = getopt_long(argc, argv,
"b::mk::d:hv:",
long_options, &long_index)) != -1)
{
switch(op)
{
case 'b':
#ifdef HAS_ENGINE_BPF
{
abort_if_already_configured(vtable);
vtable = &scap_bpf_engine;
bpf_params.buffer_bytes_dim = buffer_bytes_dim;
/* This should handle cases where we pass arguments with the space:
* `-b ./path/to/probe`. Without this `if` case we can accept arguments
* only in this format `-b./path/to/probe`
*/
if(optarg == NULL && optind < argc && argv[optind][0] != '-')
{
bpf_params.bpf_probe = argv[optind++];
}
else if(optarg == NULL)
{
strlcat(driver_path, BPF_PROBE_DEFAULT_PATH, FILENAME_MAX);
bpf_params.bpf_probe = driver_path;
}
else
{
bpf_params.bpf_probe = optarg;
}
oargs.engine_params = &bpf_params;
std::cout << "* Configure BPF probe tests! Probe path: " << bpf_params.bpf_probe << std::endl;
}
#else
std::cerr << "BPF engine is not supported in this build" << std::endl;
return EXIT_FAILURE;
#endif
break;
case 'm':
#ifdef HAS_ENGINE_MODERN_BPF
{
abort_if_already_configured(vtable);
vtable = &scap_modern_bpf_engine;
modern_bpf_params.buffer_bytes_dim = buffer_bytes_dim;
oargs.engine_params = &modern_bpf_params;
std::cout << "* Configure modern BPF probe tests!" << std::endl;
}
#else
std::cerr << "Modern BPF engine is not supported in this build" << std::endl;
return EXIT_FAILURE;
#endif
break;
case 'k':
#ifdef HAS_ENGINE_KMOD
{
abort_if_already_configured(vtable);
vtable = &scap_kmod_engine;
kmod_params.buffer_bytes_dim = buffer_bytes_dim;
if(optarg == NULL && optind < argc && argv[optind][0] != '-')
{
kmod_path = argv[optind++];
}
else if(optarg == NULL)
{
strlcat(driver_path, KMOD_DEFAULT_PATH, FILENAME_MAX);
kmod_path = driver_path;
}
else
{
kmod_path = optarg;
}
oargs.engine_params = &kmod_params;
if(insert_kmod(kmod_path))
{
return EXIT_FAILURE;
}
std::cout << "* Configure kernel module tests! Kernel module path: " << kmod_path << std::endl;
}
#else
std::cerr << "Kernel module engine is not supported in this build" << std::endl;
return EXIT_FAILURE;
#endif
break;
case 'd':
if(vtable != nullptr)
{
std::cerr << "The buffer dim '" << BUFFER_OPTION << "' must be chosen before opening the engine" << std::endl;
return EXIT_FAILURE;
}
buffer_bytes_dim = strtoul(optarg, NULL, 10);
break;
case 'h':
print_menu_and_exit();
break;
case 'v':
{
unsigned long level = strtoul(optarg, NULL, 10);
if(level < FALCOSECURITY_LOG_SEV_FATAL || level > FALCOSECURITY_LOG_SEV_TRACE)
{
std::cerr << "Invalid logging level. Valid range is '" << std::to_string(FALCOSECURITY_LOG_SEV_FATAL) <<"' <= lev <= '" << std::to_string(FALCOSECURITY_LOG_SEV_TRACE) << "'" << std::endl;
return EXIT_FAILURE;
}
severity_level = (enum falcosecurity_log_severity)level;
}
break;
default:
return EXIT_FAILURE;
}
}
std::cout << "* Using buffer dim: " << buffer_bytes_dim << std::endl;
if(vtable == nullptr)
{
std::cerr << "Unsupported engine! Choose between: m, b, k" << std::endl;
return EXIT_FAILURE;
}
char error_buffer[FILENAME_MAX] = {0};
event_test::s_scap_handle = scap_open(&oargs, vtable, error_buffer, &ret);
if(!event_test::s_scap_handle)
{
std::cerr << "Unable to open the engine: " << error_buffer << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
int main(int argc, char** argv)
{
int res = EXIT_SUCCESS;
print_message("Setup phase");
::testing::InitGoogleTest(&argc, argv);
/* Open the requested engine */
if(open_engine(argc, argv))
{
return EXIT_FAILURE;
}
print_message("Testing phase");
res = RUN_ALL_TESTS();
print_message("Teardown phase");
scap_close(event_test::s_scap_handle);
remove_kmod();
return res;
}