-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplugins_manager.c
496 lines (378 loc) · 12.7 KB
/
plugins_manager.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
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
//
// Created by thomas on 5/11/18.
//
#include "plugins_manager.h"
#include <stdint.h>
#include <stdlib.h>
#include <errno.h>
#include "ubpf_manager.h"
#include "bpf_plugin.h"
#include "log.h"
#include "ubpf_context.h"
#include "evt_plugins.h"
#include "plugin_socket.h"
#include <stdio.h>
#include <elf.h>
#include <linux/limits.h>
#include <unistd.h>
#include <assert.h>
#include <sys/mman.h>
static int is_init = 0;
static manager_t master;
static const char *syslog_name = "plugin_manager";
static inline int full_write(int fd, const char *buf, size_t len) {
ssize_t s;
size_t total;
total = 0;
while (total < len) {
s = write(fd, buf + total, len - total);
if (s == 0 || s == -1) return -1;
total += s;
}
return 0;
}
/* used when an insertion point is deleted. */
static void on_delete_vm(void *self) {
vm_container_t *vm = self;
vm_container_t *table = master.vms_table;
if (!table) {
return;
}
HASH_DELETE(hh, table, vm);
master.vms_table = table;
}
//static int is_directory(const char *path) {
// struct stat statbuf;
// if (stat(path, &statbuf) != 0)
// return 0;
// return S_ISDIR(statbuf.st_mode);
//}
/* should be used in the protocol to "pluginize" */
int
init_plugin_manager(proto_ext_fun_t *api_proto, const char *var_state_dir,
insertion_point_info_t *insertion_points_array,
int dbg, struct log_config *logs) {
if (is_init) return 0;
if (!var_state_dir) return -1;
memset(&master, 0, sizeof(master));
strncpy(master.var_state_path, var_state_dir, sizeof(master.var_state_path) - 1);
master.point_info = insertion_points_array;
master.helper_functions = api_proto;
// start logging manager
log_init(dbg, syslog_name, logs);
// start event_loop
if (start_events_loop() != 0) {
return -1;
}
// start plugin message listener
// todo
// :fmoh:
is_init = 1;
return 0;
}
static void flush_manager(manager_t *manager) {
insertion_point_t *point, *tmp_p;
plugin_t *p, *tmp_plugin;
vm_container_t *vm, *tmp_vm;
HASH_ITER(hh, manager->insertion_point_table, point, tmp_p) {
HASH_DELETE(hh, manager->insertion_point_table, point);
free_insertion_point(point);
}
HASH_ITER(hh, manager->plugin_table, p, tmp_plugin) {
HASH_DELETE(hh, manager->plugin_table, p);
plugin_unlock_ref(p);
}
HASH_ITER(hh, manager->vms_table, vm, tmp_vm) {
HASH_DELETE(hh, manager->vms_table, vm);
shutdown_vm(vm);
}
// pthread del TODO
}
void ubpf_terminate() {
is_init = 0;
// cancel events loop
cancel_event_loop();
// CLOSE logger
logs_close();
rm_all_file_descriptions();
// off listener thread TODO
flush_manager(&master);
}
int add_extension_code(const char *plugin_name, size_t plugin_name_len, uint64_t extra_mem, uint64_t shared_mem,
int insertion_point_id, const char *insertion_point, size_t i_pt_name, anchor_t type_anchor,
int seq_anchor, int jit, const char *obj_path_code, size_t len_obj_path_code,
const char *vm_name, size_t vm_name_len, proto_ext_fun_t *api_proto, int permission,
int add_memcheck_insts, mem_type_t memory_mgt, int use_libffi) {
char *bytecode;
size_t bytecode_len;
vm_container_t *vm = NULL;
plugin_t *p;
insertion_point_t *point;
int should_free = 0;
/* 0. Read ELF/Obj file */
// check by the magic number if it is a path or an ELF buffer
if (memcmp(obj_path_code, ELFMAG, SELFMAG) != 0) {
// ELF MAGIC does not match, this is file based
bytecode = readfile(obj_path_code, MAX_SIZE_PLUGIN, &bytecode_len);
should_free = 1;
} else {
bytecode = NULL;
bytecode_len = len_obj_path_code;
}
/* 1. Get Plugin */
p = get_plugin_by_name(&master, plugin_name);
if (!p) {
p = init_plugin(extra_mem, shared_mem, plugin_name, plugin_name_len, permission, memory_mgt);
if (!p) goto fail;
if (register_plugin(&master, p) != 0) goto fail;
}
/* 2. Get insertion point */ // todo check INSERTION POINT NAME !
if (!insertion_point_is_registered_by_id(&master, insertion_point_id)) {
point = new_insertion_point(insertion_point_id, insertion_point, i_pt_name);
if (!point) goto fail;
if (register_insertion_point(&master, point) != 0) goto fail;
} else {
point = get_insertion_point(&master, insertion_point_id);
}
/* 3. Build VM if not present */
if (is_vm_registered_by_name(&master, vm_name)) {
/* the vm is already here, must rebuild it */
vm = unregister_vm(&master, vm_name);
plugin_delete_vm(vm);
rm_vm_insertion_point(vm);
shutdown_vm(vm);
vm = NULL;
}
vm = new_vm(type_anchor, seq_anchor, point, jit, vm_name, vm_name_len, p,
bytecode ? bytecode : obj_path_code,
bytecode_len, api_proto,
on_delete_vm, add_memcheck_insts, use_libffi);
if (should_free) free(bytecode);
if (!vm) goto fail;
if (register_vm(&master, vm) != 0) goto fail;
/* 4. ADD VM to plugin */
if (plugin_add_vm(p, vm) != 0) goto fail;
/* 5. And to the insertion point */
if (add_vm_insertion_point(point, vm, type_anchor, seq_anchor) != 0) goto fail;
return 0;
fail:
if (vm) shutdown_vm(vm);
return -1;
}
int remove_extension_code(const char *name) {
vm_container_t *vm;
vm = unregister_vm(&master, name);
if (!vm) return -1;
/* delete code from insertion point */
if (rm_vm_insertion_point(vm) != 0) return -1;
/* delete code from plugin */
if (plugin_delete_vm(vm) != 0) return -1;
/* shutdown VM */
shutdown_vm(vm);
return 0;
}
int remove_plugin(const char *name) {
return unregister_plugin(&master, name);
}
int remove_insertion_point(int id) {
return unregister_insertion_point(&master, id);
}
inline int is_vm_registered(manager_t *manager, vm_container_t *vm) {
vm_container_t *the_vm;
HASH_FIND_STR(manager->vms_table, vm->vm_name, the_vm);
return the_vm != NULL;
}
inline int is_vm_registered_by_name(manager_t *manager, const char *name) {
vm_container_t *the_vm;
HASH_FIND_STR(manager->vms_table, name, the_vm);
return the_vm != NULL;
}
static inline vm_container_t *get_vm_by_name(manager_t *manager, const char *name) {
vm_container_t *vm;
HASH_FIND_STR(manager->vms_table, name, vm);
return vm;
}
int register_vm(manager_t *manager, vm_container_t *vm) {
if (is_vm_registered(manager, vm)) return -1;
HASH_ADD_STR(manager->vms_table, vm_name, vm);
return 0;
}
vm_container_t *unregister_vm(manager_t *manager, const char *name) {
vm_container_t *vm;
HASH_FIND_STR(manager->vms_table, name, vm);
if (!vm) return NULL;
HASH_DEL(manager->vms_table, vm);
return vm;
}
inline insertion_point_t *get_insertion_point_by_id(manager_t *manager, int id) {
insertion_point_t *point;
HASH_FIND_INT(manager->insertion_point_table, &id, point);
return point;
}
inline int is_plugin_registered(manager_t *manager, plugin_t *p) {
plugin_t *the_plugin;
HASH_FIND_STR(manager->plugin_table, p->name, the_plugin);
return the_plugin != NULL;
}
inline plugin_t *get_plugin_by_name(manager_t *manager, const char *name) {
plugin_t *the_plugin;
HASH_FIND_STR(manager->plugin_table, name, the_plugin);
return the_plugin;
}
inline int is_plugin_registered_by_name(manager_t *manager, const char *name) {
return get_plugin_by_name(manager, name) != NULL;
}
int register_plugin(manager_t *manager, plugin_t *plugin) {
if (is_plugin_registered(manager, plugin)) return -1;
HASH_ADD_STR(manager->plugin_table, name, plugin);
plugin_lock_ref(plugin);
return 0;
}
int unregister_plugin(manager_t *manager, const char *name) {
plugin_t *p;
vm_container_t *vm;
HASH_FIND_STR(manager->plugin_table, name, p);
if (!p) return -1;
HASH_DELETE(hh, manager->plugin_table, p);
/* remove vm from master table since will be deallocated when destroying plugin */
for (vm = p->vms; vm != NULL; vm = vm->hh_plugin.next) {
HASH_DELETE(hh, manager->vms_table, vm);
}
plugin_unlock_ref(p);
return 0;
}
inline int insertion_point_is_registered(manager_t *manager, insertion_point_t *point) {
insertion_point_t *the_point;
HASH_FIND_INT(manager->insertion_point_table, &point->id, the_point);
return the_point != NULL;
}
inline int insertion_point_is_registered_by_id(manager_t *manager, int id) {
insertion_point_t *the_point;
HASH_FIND_INT(manager->insertion_point_table, &id, the_point);
return the_point != NULL;
}
int register_insertion_point(manager_t *manager, insertion_point_t *point) {
if (!point) return -1;
if (insertion_point_is_registered(manager, point)) return -1; // already registered
HASH_ADD_INT(manager->insertion_point_table, id, point);
return 0;
}
int unregister_insertion_point(manager_t *manager, int id) {
insertion_point_t *point;
insertrion_point_iterator_t it;
vm_container_t *current_vm;
HASH_FIND_INT(manager->insertion_point_table, &id, point);
if (!point) return -1;
HASH_DELETE(hh, manager->insertion_point_table, point);
/* unregister VMs from master table */
insertion_point_vm_iterator(point, &it);
while (insertion_point_vm_iterator_hasnext(&it)) {
current_vm = insertion_point_vm_iterator_next(&it);
HASH_DELETE(hh, manager->vms_table, current_vm);
}
free_insertion_point(point);
return 0;
}
insertion_point_info_t *get_insertion_point_info() {
return master.point_info;
}
int str_insertion_point_to_int(manager_t *manager, const char *plugin_str) {
insertion_point_t *point, *tmp;
/* the hashing is based on the integer ID, need to browse the whole hash-table */
HASH_ITER(hh, manager->insertion_point_table, point, tmp) {
if (strncmp(point->name, plugin_str, 256) == 0) return point->id;
}
return -1;
}
const char *id_insertion_point_to_str(manager_t *manager, int id) {
insertion_point_t *point;
point = get_insertion_point(manager, id);
if (!point) return "UNK";
return point->name;
}
inline insertion_point_t *get_insertion_point(manager_t *manager, int id) {
insertion_point_t *point;
if (!manager) return NULL;
HASH_FIND_INT(manager->insertion_point_table, &id, point);
return point;
}
inline insertion_point_t *insertion_point(int id) {
return get_insertion_point(&master, id);
}
inline plugin_t *plugin_by_name(const char *name) {
return get_plugin_by_name(&master, name);
}
inline vm_container_t *vm_by_name(const char *name) {
return get_vm_by_name(&master, name);
}
static inline unsigned long file_size(FILE *file) {
long size;
long offset;
offset = ftell(file);
if (offset < 0) {
perror("ftell curr offset");
return 0;
}
if (fseek(file, 0, SEEK_END) != 0) { // seek to end of file
perror("fseek END");
return 0;
};
size = ftell(file); // get current file pointer
if (size == -1) {
perror("ftell");
return 0;
}
if (fseek(file, offset, SEEK_SET) != 0) { // seek back to the previous offset of file
perror("fseek SET");
}
return size;
}
void *readfile(const char *path, size_t maxlen, size_t *len) {
uint8_t *data;
uint8_t *src_data;
char absolute_path[8192];
const char *sel_path;
unsigned long size;
FILE *file;
if (!strcmp(path, "-")) {
fprintf(stderr, "Stdin is not supported !\n");
return NULL;
} else {
memset(absolute_path, 0, PATH_MAX * sizeof(char));
if (realpath(path, absolute_path) != absolute_path) {
fprintf(stderr, "Realpath error\n");
return NULL;
};
sel_path = absolute_path;
file = fopen(sel_path, "r");
}
if (file == NULL) {
fprintf(stderr, "Failed to open %s: %s (uid %u)\n", sel_path, strerror(errno), getuid());
return NULL;
}
size = file_size(file);
if (size == 0) {
return NULL;
} else if (size > maxlen) {
fprintf(stderr, "File too big: %ld. Maximum allowed %zu\n", size, maxlen);
return NULL;
}
data = calloc(size, 1);
if (!data) {
perror("mem alloc failed");
return NULL;
}
// open mmap
src_data = mmap(0, size, PROT_READ, MAP_PRIVATE, fileno(file), 0);
if (src_data == MAP_FAILED) {
perror("mmap");
return NULL;
}
memcpy(data, src_data, size);
munmap(src_data, size);
fclose(file);
if (len) {
*len = size;
}
return data;
}