-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathanalysis.c
More file actions
534 lines (428 loc) · 14.6 KB
/
analysis.c
File metadata and controls
534 lines (428 loc) · 14.6 KB
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
#define _POSIX_C_SOURCE 200809L
#include "analysis.h"
#include "modtable.h"
#include "util.h"
#include "log.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#if MII_ENABLE_LUA
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
/* After Lua 5.1, lua_objlen changed name and LUA_OK is defined */
#if LUA_VERSION_NUM <= 501
#define mii_lua_len lua_objlen
#define LUA_OK 0
#else
#define mii_lua_len lua_rawlen
#endif
#endif // MII_ENABLE_LUA
#if MII_ENABLE_SPIDER
#include "cjson/cJSON.h"
#endif
#include <dirent.h>
#include <errno.h>
#include <regex.h>
#include <unistd.h>
#include <sys/stat.h>
#include <wordexp.h>
#if !MII_ENABLE_LUA
static const char* _mii_analysis_lmod_regex_src =
"\\s*(prepend_path|append_path)\\s*\\(\\s*"
"\"PATH\"\\s*,\\s*\"([^\"]+)\"";
static regex_t _mii_analysis_lmod_regex;
#else
/* Lua interpreter state */
static lua_State *lua_state;
/* run lua module code in a sandbox */
int _mii_analysis_lua_run(lua_State* lua_state, const char* code, char*** paths_out, int* num_paths_out);
#endif
static char** ignore_paths = NULL;
static int num_ignore_paths = 0;
static char** ignore_mods = NULL;
static int num_ignore_mods = 0;
/* module filter function */
int _mii_analysis_is_mod_ignored(const char* path, const char* mod);
int _mii_analysis_filter_init(const char* filter, const char* sep, char*** out, int* num_out);
/* word expansion functions */
char* _mii_analysis_expand(const char* expr);
/* module type analysis functions */
int _mii_analysis_lmod(const char* path, char*** bins_out, int* num_bins_out);
int _mii_analysis_tcl(const char* path, char*** bins_out, int* num_bins_out);
/* path scanning functions */
int _mii_analysis_scan_path(char* path, char*** bins_out, int* num_bins_out);
#if MII_ENABLE_SPIDER
int _mii_analysis_parents_from_json(const cJSON* json, char*** parents_out, int* num_parents_out);
#endif
#if !MII_ENABLE_LUA
/*
* compile regexes
*/
int mii_analysis_init() {
if (regcomp(&_mii_analysis_lmod_regex, _mii_analysis_lmod_regex_src, REG_EXTENDED | REG_NEWLINE)) {
mii_error("failed to compile Lmod analysis regex");
return -1;
}
return 0;
}
#else
/*
* initialize Lua interpreter
*/
int mii_analysis_init() {
lua_state = luaL_newstate();
luaL_openlibs(lua_state);
/* sandbox path when mii is installed */
char* lua_path = mii_join_path(MII_PREFIX, "share/mii/lua/sandbox.luac");
if(access(lua_path, F_OK) == 0) {
/* found file, try to execute it and return */
if(luaL_dofile(lua_state, lua_path) == LUA_OK) {
free(lua_path);
return 0;
}
}
free(lua_path);
lua_path = "./sandbox.luac";
if(access(lua_path, F_OK) == 0) {
/* mii is not installed, but should work anyway */
if(luaL_dofile(lua_state, lua_path) == LUA_OK)
return 0;
}
mii_error("failed to load Lua file");
lua_close(lua_state);
return -1;
}
#endif
/*
* cleanup regexes or Lua interpreter
*/
void mii_analysis_free() {
#if !MII_ENABLE_LUA
regfree(&_mii_analysis_lmod_regex);
#else
lua_close(lua_state);
#endif
}
/*
* run analysis for an arbitrary module
*/
int mii_analysis_run(const char* modfile, int modtype, char*** bins_out, int* num_bins_out) {
switch (modtype) {
case MII_MODTABLE_MODTYPE_LMOD:
return _mii_analysis_lmod(modfile, bins_out, num_bins_out);
case MII_MODTABLE_MODTYPE_TCL:
return _mii_analysis_tcl(modfile, bins_out, num_bins_out);
}
return 0;
}
#if MII_ENABLE_LUA
/*
* run a modulefile's code in a Lua sandbox
*/
int _mii_analysis_lua_run(lua_State* lua_state, const char* code, char*** paths_out, int* num_paths_out) {
/* execute modulefile */
int res;
lua_getglobal(lua_state, "sandbox_run");
lua_pushstring(lua_state, code);
res = lua_pcall(lua_state, 1, 1, 0);
if(res != LUA_OK) {
mii_error("Error occured in Lua sandbox : %s", lua_tostring(lua_state, -1));
lua_pop(lua_state, 1);
return -1;
}
luaL_checktype(lua_state, 1, LUA_TTABLE);
/* allocate memory for the paths */
*num_paths_out = mii_lua_len(lua_state, -1);
*paths_out = malloc(*num_paths_out * sizeof(char*));
/* retrieve paths from Lua stack */
for (int i = 1; i <= *num_paths_out; ++i) {
lua_rawgeti(lua_state, -i, i);
(*paths_out)[i-1] = mii_strdup(lua_tostring(lua_state, -1));
}
/* pop every rawgeti + table */
lua_pop(lua_state, *num_paths_out + 1);
return 0;
}
#endif
/*
* extract paths from an lmod file
*/
int _mii_analysis_lmod(const char* path, char*** bins_out, int* num_bins_out) {
FILE* f = fopen(path, "r");
if (!f) {
mii_error("Couldn't open %s for reading : %s", path, strerror(errno));
return -1;
}
#if !MII_ENABLE_LUA
char linebuf[MII_ANALYSIS_LINEBUF_SIZE];
regmatch_t matches[3];
while (fgets(linebuf, sizeof linebuf, f)) {
/* strip off newline */
int len = strlen(linebuf);
if (linebuf[len - 1] == '\n') linebuf[len - 1] = 0;
/* execute regex */
if (!regexec(&_mii_analysis_lmod_regex, linebuf, 3, matches, 0)) {
if (matches[2].rm_so < 0) continue;
linebuf[matches[2].rm_eo] = 0;
_mii_analysis_scan_path(linebuf + matches[2].rm_so, bins_out, num_bins_out);
}
}
fclose(f);
#else
char* buffer;
fseek(f, 0L, SEEK_END);
long s = ftell(f);
rewind(f);
buffer = malloc(s + 1);
if ( buffer != NULL ) {
fread(buffer, s, 1, f);
fclose(f); f = NULL;
buffer[s] = '\0';
/* get binaries paths */
char** bin_paths;
int num_paths;
if(_mii_analysis_lua_run(lua_state, buffer, &bin_paths, &num_paths)) {
mii_error("Error occured when executing %s, skipping", path);
free(buffer);
return -1;
}
/* scan every path returned */
for(int i = 0; i < num_paths; ++i) {
_mii_analysis_scan_path(bin_paths[i], bins_out, num_bins_out);
free(bin_paths[i]);
}
free(bin_paths);
free(buffer);
}
if (f != NULL) fclose(f);
#endif
return 0;
}
/*
* extract paths from a tcl file
*/
int _mii_analysis_tcl(const char* path, char*** bins_out, int* num_bins_out) {
char linebuf[MII_ANALYSIS_LINEBUF_SIZE];
FILE* f = fopen(path, "r");
if (!f) {
mii_error("Couldn't open %s for reading : %s", path, strerror(errno));
return -1;
}
char* cmd, *key, *val, *expanded;
while (fgets(linebuf, sizeof linebuf, f)) {
/* strip off newline */
int len = strlen(linebuf);
if (linebuf[len - 1] == '\n') linebuf[len - 1] = 0;
if (!(cmd = strtok(linebuf, " \t"))) continue;
if (*cmd == '#') continue; /* skip comments */
if (!strcmp(cmd, "set")) {
if (!(key = strtok(NULL, " \t"))) continue;
if (!(val = strtok(NULL, " \t"))) continue;
if (!(expanded = _mii_analysis_expand(val))) continue;
setenv(key, expanded, 1);
free(expanded);
} else if (!strcmp(cmd, "prepend-path") || !strcmp(cmd, "append-path")) {
if (!(key = strtok(NULL, " \t"))) continue;
if (strcmp(key, "PATH")) continue;
if (!(val = strtok(NULL, " \t"))) continue;
if (!(expanded = _mii_analysis_expand(val))) continue;
_mii_analysis_scan_path(expanded, bins_out, num_bins_out);
free(expanded);
}
}
fclose(f);
return 0;
}
/*
* scan a path for commands
*/
int _mii_analysis_scan_path(char* path, char*** bins_out, int* num_bins_out) {
/* paths might contain multiple in one (seperated by ':'),
* break them up here */
DIR* d;
struct dirent* dp;
struct stat st;
for (const char* cur_path = strtok(path, ":"); cur_path; cur_path = strtok(NULL, ":")) {
mii_debug("scanning PATH %s", cur_path);
/* TODO: this could be faster, do some benchmarking to see if it's actually slow */
if (!(d = opendir(cur_path))) {
mii_debug("Failed to open %s, ignoring : %s", cur_path, strerror(errno));
continue;
}
while ((dp = readdir(d))) {
if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) continue;
char* abs_path = mii_join_path(cur_path, dp->d_name);
if (!stat(abs_path, &st)) {
/* check the file is executable by the user */
if ((S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) && !access(abs_path, X_OK)) {
/* found a binary! append it to the list */
++*num_bins_out;
*bins_out = realloc(*bins_out, *num_bins_out * sizeof **bins_out);
(*bins_out)[*num_bins_out - 1] = mii_strdup(dp->d_name);
}
} else {
mii_warn("Couldn't stat %s : %s", abs_path, strerror(errno));
}
free(abs_path);
}
closedir(d);
}
return 0;
}
char* _mii_analysis_expand(const char* expr) {
wordexp_t w;
if (wordexp(expr, &w, WRDE_NOCMD)) {
/* expansion failed. die quietly */
mii_debug("Expansion failed on string \"%s\"!", expr);
return NULL;
}
char* output = NULL;
int len = 0;
for (int i = 0; i < w.we_wordc; ++i) {
int wsize = strlen(w.we_wordv[i]);
len += wsize;
output = realloc(output, len + 1);
memcpy(output + len - wsize, w.we_wordv[i], len + 1);
}
wordfree(&w);
return output;
}
#if MII_ENABLE_SPIDER
/* parse the json and fill module info */
int mii_analysis_parse_module_json(const cJSON* mod_json, mii_modtable_entry** mod) {
/* get the code */
cJSON* code = cJSON_GetObjectItemCaseSensitive(mod_json, "fullName");
if (code == NULL) {
mii_error("Couldn't find the code in the JSON!");
return -1;
}
/* should we filter out the module? */
if (_mii_analysis_is_mod_ignored(mod_json->string, code->valuestring)) {
mii_debug("Filtered out module %s", code->valuestring);
return 0;
}
/* stat the type */
struct stat st;
if (stat(mod_json->string, &st) != 0) {
mii_error("Couldn't stat %s: %s", mod_json->string, strerror(errno));
return -1;
}
*mod = malloc(sizeof **mod);
/* get the parents */
cJSON* parents_arrs = cJSON_GetObjectItemCaseSensitive(mod_json, "parentAA");
if(_mii_analysis_parents_from_json(parents_arrs, &(*mod)->parents, &(*mod)->num_parents)) {
mii_error("Couldn't get parents from JSON!");
return -1;
}
/* fill up some of the info */
(*mod)->bins = NULL;
(*mod)->num_bins = 0;
(*mod)->path = mii_strdup(mod_json->string);
(*mod)->type = MII_MODTABLE_MODTYPE_LMOD;
(*mod)->timestamp = st.st_mtime;
(*mod)->code = mii_strdup(code->valuestring);
(*mod)->analysis_complete = 1;
/* get the bins */
cJSON* bin_paths = cJSON_GetObjectItemCaseSensitive(mod_json, "pathA");
if (bin_paths != NULL) {
for (cJSON* path = bin_paths->child; path != NULL; path = path->next) {
/* analyze the bin paths */
_mii_analysis_scan_path(path->string, &(*mod)->bins, &(*mod)->num_bins);
}
}
return 0;
}
/* get the parents from a json array (can be NULL) */
int _mii_analysis_parents_from_json(const cJSON* json, char*** parents_out, int* num_parents_out) {
/* if NULL, nothing to do */
if (json == NULL) {
*parents_out = NULL;
*num_parents_out = 0;
return 0;
}
/* allocate memory for the parents */
*num_parents_out = cJSON_GetArraySize(json);
*parents_out = malloc(*num_parents_out * sizeof(char*));
if (*parents_out == NULL) {
mii_error("Couldn't allocate memory for parents: %s", strerror(errno));
return -1;
}
size_t i = 0;
for (cJSON* parents = json->child; parents != NULL; parents = parents->next) {
char* codes_tmp = NULL;
size_t parent_len, codes_size = 0;
for (cJSON* parent = parents->child; parent != NULL; parent = parent->next) {
/* allocate memory new code */
parent_len = strlen(parent->valuestring);
codes_tmp = (char*) realloc(codes_tmp, codes_size + parent_len + 1);
if (codes_tmp == NULL) {
mii_error("Couldn't allocate memory for parents: %s", strerror(errno));
free(*parents_out);
return -1;
}
/* add new code */
if (codes_size != 0) strcpy(codes_tmp + codes_size - 1, " ");
strcpy(codes_tmp + codes_size, parent->valuestring);
codes_size += parent_len + 1;
}
(*parents_out)[i++] = codes_tmp;
}
return 0;
}
#endif
int mii_analysis_filters_init(const char* paths, const char* modules) {
/* init path filter */
int res = _mii_analysis_filter_init(paths, MII_ANALYSIS_PATH_SEP, &ignore_paths, &num_ignore_paths);
if (res) return res;
/* init module filter */
return _mii_analysis_filter_init(modules, MII_ANALYSIS_MOD_SEP, &ignore_mods, &num_ignore_mods);
}
int _mii_analysis_filter_init(const char* filter, const char* sep, char*** out, int* num_out) {
/* initialize any filter type */
/* if NULL, nothing to do */
if (filter == NULL) {
*out = NULL;
*num_out = 0;
return 0;
}
char* filter_copy = strdup(filter);
for (char* tok = strtok(filter_copy, sep); tok; tok = strtok(NULL, sep), ++(*num_out)) {
*out = (char**) realloc(*out, sizeof(char*) * (*num_out + 1));
if (*out == NULL) {
mii_error("Couldn't allocate memory for filter: %s", strerror(errno));
free(filter_copy);
return -1;
}
(*out)[*num_out] = strdup(tok);
}
free(filter_copy);
return 0;
}
int mii_analysis_filters_free() {
for (int i = 0; i < num_ignore_paths; ++i) {
free(ignore_paths[i]);
}
for (int i = 0; i < num_ignore_mods; ++i) {
free(ignore_mods[i]);
}
if (ignore_paths) free(ignore_paths);
if (ignore_mods) free(ignore_mods);
return 0;
}
int _mii_analysis_is_mod_ignored(const char* path, const char* mod) {
/* returns a truthy value if the module should be ignored based on the filters */
/* check if the path is ignored */
for (size_t i = 0; i < num_ignore_paths; ++i) {
if (strstr(path, ignore_paths[i]) == path) {
return 1;
}
}
/* check if the module is ignored */
for (size_t i = 0; i < num_ignore_mods; ++i) {
if (strstr(mod, ignore_mods[i]) == mod) {
return 1;
}
}
return 0;
}