aboutsummaryrefslogtreecommitdiffstats
path: root/src/libtracelog/libtracelog.c
diff options
context:
space:
mode:
authorLibravatar netblue30 <netblue30@yahoo.com>2015-12-04 07:57:47 -0500
committerLibravatar netblue30 <netblue30@yahoo.com>2015-12-04 07:57:47 -0500
commitb95ecff464742878e19fbf0b7b50274593bf1ec5 (patch)
tree19befdef94299c2a0e311664210e54445dc6e1d2 /src/libtracelog/libtracelog.c
parenttesting (diff)
downloadfirejail-b95ecff464742878e19fbf0b7b50274593bf1ec5.tar.gz
firejail-b95ecff464742878e19fbf0b7b50274593bf1ec5.tar.zst
firejail-b95ecff464742878e19fbf0b7b50274593bf1ec5.zip
--tracelog work
Diffstat (limited to 'src/libtracelog/libtracelog.c')
-rw-r--r--src/libtracelog/libtracelog.c83
1 files changed, 59 insertions, 24 deletions
diff --git a/src/libtracelog/libtracelog.c b/src/libtracelog/libtracelog.c
index c3bbc132b..26787daed 100644
--- a/src/libtracelog/libtracelog.c
+++ b/src/libtracelog/libtracelog.c
@@ -32,7 +32,7 @@
32#include <syslog.h> 32#include <syslog.h>
33#include <dirent.h> 33#include <dirent.h>
34 34
35 35//#define DEBUG
36 36
37// break recursivity on fopen call 37// break recursivity on fopen call
38typedef FILE *(*orig_fopen_t)(const char *pathname, const char *mode); 38typedef FILE *(*orig_fopen_t)(const char *pathname, const char *mode);
@@ -48,7 +48,19 @@ typedef struct list_elem_t {
48 char *path; 48 char *path;
49} ListElem; 49} ListElem;
50 50
51static ListElem *storage; 51#define HMASK 0x0ff
52ListElem *storage[HMASK + 1];
53
54// djb2
55static inline uint32_t hash(const char *str) {
56 uint32_t hash = 5381;
57 int c;
58
59 while ((c = *str++) != '\0')
60 hash = ((hash << 5) + hash) + c; // hash * 33 + c; another variant would be hash * 33 ^ c
61
62 return hash & HMASK;
63}
52 64
53static storage_add(const char *str) { 65static storage_add(const char *str) {
54 ListElem *ptr = malloc(sizeof(ListElem)); 66 ListElem *ptr = malloc(sizeof(ListElem));
@@ -61,8 +73,11 @@ static storage_add(const char *str) {
61 fprintf(stderr, "Error: cannot allocate memory\n"); 73 fprintf(stderr, "Error: cannot allocate memory\n");
62 return; 74 return;
63 } 75 }
64 ptr->next = storage; 76
65 storage = ptr; 77 // insert it into the hash table
78 uint32_t h = hash(ptr->path);
79 ptr->next = storage[h];
80 storage[h] = ptr;
66} 81}
67 82
68static char *storage_find(const char *str) { 83static char *storage_find(const char *str) {
@@ -74,7 +89,8 @@ static char *storage_find(const char *str) {
74 allocated = 1; 89 allocated = 1;
75 } 90 }
76 91
77 ListElem *ptr = storage; 92 uint32_t h = hash(tofind);
93 ListElem *ptr = storage[h];
78 while (ptr) { 94 while (ptr) {
79 if (strcmp(tofind, ptr->path) == 0) { 95 if (strcmp(tofind, ptr->path) == 0) {
80 if (allocated) 96 if (allocated)
@@ -89,6 +105,7 @@ static char *storage_find(const char *str) {
89 return NULL; 105 return NULL;
90} 106}
91 107
108
92// 109//
93// load blacklistst form /run/firejail/mnt/fslogger 110// load blacklistst form /run/firejail/mnt/fslogger
94// 111//
@@ -134,7 +151,25 @@ void load_blacklist(void) {
134 } 151 }
135 fclose(fp); 152 fclose(fp);
136 blacklist_loaded = 1; 153 blacklist_loaded = 1;
154#ifdef DEBUG
137 printf("Monitoring %d blacklists\n", cnt); 155 printf("Monitoring %d blacklists\n", cnt);
156 {
157 int i;
158 for (i = 0; i <= HMASK; i++) {
159 int cnt = 0;
160 ListElem *ptr = storage[i];
161 while (ptr) {
162 cnt++;
163 ptr = ptr->next;
164 }
165
166 if ((i % 16) == 0)
167 printf("\n");
168 printf("%02d ", cnt);
169 }
170 printf("\n");
171 }
172#endif
138} 173}
139 174
140 175
@@ -216,7 +251,7 @@ int open(const char *pathname, int flags, mode_t mode) {
216 if (!orig_open) 251 if (!orig_open)
217 orig_open = (orig_open_t)dlsym(RTLD_NEXT, "open"); 252 orig_open = (orig_open_t)dlsym(RTLD_NEXT, "open");
218 253
219 if (!storage) 254 if (!blacklist_loaded)
220 load_blacklist(); 255 load_blacklist();
221 256
222 int rv = orig_open(pathname, flags, mode); 257 int rv = orig_open(pathname, flags, mode);
@@ -230,7 +265,7 @@ static orig_open64_t orig_open64 = NULL;
230int open64(const char *pathname, int flags, mode_t mode) { 265int open64(const char *pathname, int flags, mode_t mode) {
231 if (!orig_open64) 266 if (!orig_open64)
232 orig_open64 = (orig_open64_t)dlsym(RTLD_NEXT, "open64"); 267 orig_open64 = (orig_open64_t)dlsym(RTLD_NEXT, "open64");
233 if (!storage) 268 if (!blacklist_loaded)
234 load_blacklist(); 269 load_blacklist();
235 270
236 int rv = orig_open64(pathname, flags, mode); 271 int rv = orig_open64(pathname, flags, mode);
@@ -245,7 +280,7 @@ static orig_openat_t orig_openat = NULL;
245int openat(int dirfd, const char *pathname, int flags, mode_t mode) { 280int openat(int dirfd, const char *pathname, int flags, mode_t mode) {
246 if (!orig_openat) 281 if (!orig_openat)
247 orig_openat = (orig_openat_t)dlsym(RTLD_NEXT, "openat"); 282 orig_openat = (orig_openat_t)dlsym(RTLD_NEXT, "openat");
248 if (!storage) 283 if (!blacklist_loaded)
249 load_blacklist(); 284 load_blacklist();
250 285
251 int rv = orig_openat(dirfd, pathname, flags, mode); 286 int rv = orig_openat(dirfd, pathname, flags, mode);
@@ -259,7 +294,7 @@ static orig_openat64_t orig_openat64 = NULL;
259int openat64(int dirfd, const char *pathname, int flags, mode_t mode) { 294int openat64(int dirfd, const char *pathname, int flags, mode_t mode) {
260 if (!orig_openat64) 295 if (!orig_openat64)
261 orig_openat64 = (orig_openat64_t)dlsym(RTLD_NEXT, "openat64"); 296 orig_openat64 = (orig_openat64_t)dlsym(RTLD_NEXT, "openat64");
262 if (!storage) 297 if (!blacklist_loaded)
263 load_blacklist(); 298 load_blacklist();
264 299
265 int rv = orig_openat64(dirfd, pathname, flags, mode); 300 int rv = orig_openat64(dirfd, pathname, flags, mode);
@@ -273,7 +308,7 @@ int openat64(int dirfd, const char *pathname, int flags, mode_t mode) {
273FILE *fopen(const char *pathname, const char *mode) { 308FILE *fopen(const char *pathname, const char *mode) {
274 if (!orig_fopen) 309 if (!orig_fopen)
275 orig_fopen = (orig_fopen_t)dlsym(RTLD_NEXT, "fopen"); 310 orig_fopen = (orig_fopen_t)dlsym(RTLD_NEXT, "fopen");
276 if (!storage) 311 if (!blacklist_loaded)
277 load_blacklist(); 312 load_blacklist();
278 313
279 FILE *rv = orig_fopen(pathname, mode); 314 FILE *rv = orig_fopen(pathname, mode);
@@ -286,7 +321,7 @@ FILE *fopen(const char *pathname, const char *mode) {
286FILE *fopen64(const char *pathname, const char *mode) { 321FILE *fopen64(const char *pathname, const char *mode) {
287 if (!orig_fopen64) 322 if (!orig_fopen64)
288 orig_fopen64 = (orig_fopen_t)dlsym(RTLD_NEXT, "fopen64"); 323 orig_fopen64 = (orig_fopen_t)dlsym(RTLD_NEXT, "fopen64");
289 if (!storage) 324 if (!blacklist_loaded)
290 load_blacklist(); 325 load_blacklist();
291 326
292 FILE *rv = orig_fopen64(pathname, mode); 327 FILE *rv = orig_fopen64(pathname, mode);
@@ -303,7 +338,7 @@ static orig_freopen_t orig_freopen = NULL;
303FILE *freopen(const char *pathname, const char *mode, FILE *stream) { 338FILE *freopen(const char *pathname, const char *mode, FILE *stream) {
304 if (!orig_freopen) 339 if (!orig_freopen)
305 orig_freopen = (orig_freopen_t)dlsym(RTLD_NEXT, "freopen"); 340 orig_freopen = (orig_freopen_t)dlsym(RTLD_NEXT, "freopen");
306 if (!storage) 341 if (!blacklist_loaded)
307 load_blacklist(); 342 load_blacklist();
308 343
309 FILE *rv = orig_freopen(pathname, mode, stream); 344 FILE *rv = orig_freopen(pathname, mode, stream);
@@ -318,7 +353,7 @@ static orig_freopen64_t orig_freopen64 = NULL;
318FILE *freopen64(const char *pathname, const char *mode, FILE *stream) { 353FILE *freopen64(const char *pathname, const char *mode, FILE *stream) {
319 if (!orig_freopen64) 354 if (!orig_freopen64)
320 orig_freopen64 = (orig_freopen64_t)dlsym(RTLD_NEXT, "freopen64"); 355 orig_freopen64 = (orig_freopen64_t)dlsym(RTLD_NEXT, "freopen64");
321 if (!storage) 356 if (!blacklist_loaded)
322 load_blacklist(); 357 load_blacklist();
323 358
324 FILE *rv = orig_freopen64(pathname, mode, stream); 359 FILE *rv = orig_freopen64(pathname, mode, stream);
@@ -334,7 +369,7 @@ static orig_unlink_t orig_unlink = NULL;
334int unlink(const char *pathname) { 369int unlink(const char *pathname) {
335 if (!orig_unlink) 370 if (!orig_unlink)
336 orig_unlink = (orig_unlink_t)dlsym(RTLD_NEXT, "unlink"); 371 orig_unlink = (orig_unlink_t)dlsym(RTLD_NEXT, "unlink");
337 if (!storage) 372 if (!blacklist_loaded)
338 load_blacklist(); 373 load_blacklist();
339 374
340 int rv = orig_unlink(pathname); 375 int rv = orig_unlink(pathname);
@@ -348,7 +383,7 @@ static orig_unlinkat_t orig_unlinkat = NULL;
348int unlinkat(int dirfd, const char *pathname, int flags) { 383int unlinkat(int dirfd, const char *pathname, int flags) {
349 if (!orig_unlinkat) 384 if (!orig_unlinkat)
350 orig_unlinkat = (orig_unlinkat_t)dlsym(RTLD_NEXT, "unlinkat"); 385 orig_unlinkat = (orig_unlinkat_t)dlsym(RTLD_NEXT, "unlinkat");
351 if (!storage) 386 if (!blacklist_loaded)
352 load_blacklist(); 387 load_blacklist();
353 388
354 int rv = orig_unlinkat(dirfd, pathname, flags); 389 int rv = orig_unlinkat(dirfd, pathname, flags);
@@ -363,7 +398,7 @@ static orig_mkdir_t orig_mkdir = NULL;
363int mkdir(const char *pathname, mode_t mode) { 398int mkdir(const char *pathname, mode_t mode) {
364 if (!orig_mkdir) 399 if (!orig_mkdir)
365 orig_mkdir = (orig_mkdir_t)dlsym(RTLD_NEXT, "mkdir"); 400 orig_mkdir = (orig_mkdir_t)dlsym(RTLD_NEXT, "mkdir");
366 if (!storage) 401 if (!blacklist_loaded)
367 load_blacklist(); 402 load_blacklist();
368 403
369 int rv = orig_mkdir(pathname, mode); 404 int rv = orig_mkdir(pathname, mode);
@@ -377,7 +412,7 @@ static orig_mkdirat_t orig_mkdirat = NULL;
377int mkdirat(int dirfd, const char *pathname, mode_t mode) { 412int mkdirat(int dirfd, const char *pathname, mode_t mode) {
378 if (!orig_mkdirat) 413 if (!orig_mkdirat)
379 orig_mkdirat = (orig_mkdirat_t)dlsym(RTLD_NEXT, "mkdirat"); 414 orig_mkdirat = (orig_mkdirat_t)dlsym(RTLD_NEXT, "mkdirat");
380 if (!storage) 415 if (!blacklist_loaded)
381 load_blacklist(); 416 load_blacklist();
382 417
383 int rv = orig_mkdirat(dirfd, pathname, mode); 418 int rv = orig_mkdirat(dirfd, pathname, mode);
@@ -391,7 +426,7 @@ static orig_rmdir_t orig_rmdir = NULL;
391int rmdir(const char *pathname) { 426int rmdir(const char *pathname) {
392 if (!orig_rmdir) 427 if (!orig_rmdir)
393 orig_rmdir = (orig_rmdir_t)dlsym(RTLD_NEXT, "rmdir"); 428 orig_rmdir = (orig_rmdir_t)dlsym(RTLD_NEXT, "rmdir");
394 if (!storage) 429 if (!blacklist_loaded)
395 load_blacklist(); 430 load_blacklist();
396 431
397 int rv = orig_rmdir(pathname); 432 int rv = orig_rmdir(pathname);
@@ -406,7 +441,7 @@ static orig_stat_t orig_stat = NULL;
406int stat(const char *pathname, struct stat *buf) { 441int stat(const char *pathname, struct stat *buf) {
407 if (!orig_stat) 442 if (!orig_stat)
408 orig_stat = (orig_stat_t)dlsym(RTLD_NEXT, "stat"); 443 orig_stat = (orig_stat_t)dlsym(RTLD_NEXT, "stat");
409 if (!storage) 444 if (!blacklist_loaded)
410 load_blacklist(); 445 load_blacklist();
411 446
412 int rv = orig_stat(pathname, buf); 447 int rv = orig_stat(pathname, buf);
@@ -421,7 +456,7 @@ static orig_stat64_t orig_stat64 = NULL;
421int stat64(const char *pathname, struct stat64 *buf) { 456int stat64(const char *pathname, struct stat64 *buf) {
422 if (!orig_stat) 457 if (!orig_stat)
423 orig_stat64 = (orig_stat64_t)dlsym(RTLD_NEXT, "stat64"); 458 orig_stat64 = (orig_stat64_t)dlsym(RTLD_NEXT, "stat64");
424 if (!storage) 459 if (!blacklist_loaded)
425 load_blacklist(); 460 load_blacklist();
426 461
427 int rv = orig_stat64(pathname, buf); 462 int rv = orig_stat64(pathname, buf);
@@ -436,7 +471,7 @@ static orig_lstat_t orig_lstat = NULL;
436int lstat(const char *pathname, struct stat *buf) { 471int lstat(const char *pathname, struct stat *buf) {
437 if (!orig_lstat) 472 if (!orig_lstat)
438 orig_lstat = (orig_lstat_t)dlsym(RTLD_NEXT, "lstat"); 473 orig_lstat = (orig_lstat_t)dlsym(RTLD_NEXT, "lstat");
439 if (!storage) 474 if (!blacklist_loaded)
440 load_blacklist(); 475 load_blacklist();
441 476
442 int rv = orig_lstat(pathname, buf); 477 int rv = orig_lstat(pathname, buf);
@@ -451,7 +486,7 @@ static orig_lstat64_t orig_lstat64 = NULL;
451int lstat64(const char *pathname, struct stat64 *buf) { 486int lstat64(const char *pathname, struct stat64 *buf) {
452 if (!orig_lstat) 487 if (!orig_lstat)
453 orig_lstat64 = (orig_lstat64_t)dlsym(RTLD_NEXT, "lstat64"); 488 orig_lstat64 = (orig_lstat64_t)dlsym(RTLD_NEXT, "lstat64");
454 if (!storage) 489 if (!blacklist_loaded)
455 load_blacklist(); 490 load_blacklist();
456 491
457 int rv = orig_lstat64(pathname, buf); 492 int rv = orig_lstat64(pathname, buf);
@@ -467,7 +502,7 @@ static orig_access_t orig_access = NULL;
467int access(const char *pathname, int mode) { 502int access(const char *pathname, int mode) {
468 if (!orig_access) 503 if (!orig_access)
469 orig_access = (orig_access_t)dlsym(RTLD_NEXT, "access"); 504 orig_access = (orig_access_t)dlsym(RTLD_NEXT, "access");
470 if (!storage) 505 if (!blacklist_loaded)
471 load_blacklist(); 506 load_blacklist();
472 507
473 int rv = orig_access(pathname, mode); 508 int rv = orig_access(pathname, mode);
@@ -482,7 +517,7 @@ static orig_opendir_t orig_opendir = NULL;
482DIR *opendir(const char *pathname) { 517DIR *opendir(const char *pathname) {
483 if (!orig_opendir) 518 if (!orig_opendir)
484 orig_opendir = (orig_opendir_t)dlsym(RTLD_NEXT, "opendir"); 519 orig_opendir = (orig_opendir_t)dlsym(RTLD_NEXT, "opendir");
485 if (!storage) 520 if (!blacklist_loaded)
486 load_blacklist(); 521 load_blacklist();
487 522
488 DIR *rv = orig_opendir(pathname); 523 DIR *rv = orig_opendir(pathname);