diff options
Diffstat (limited to 'src/libtracelog/libtracelog.c')
-rw-r--r-- | src/libtracelog/libtracelog.c | 81 |
1 files changed, 59 insertions, 22 deletions
diff --git a/src/libtracelog/libtracelog.c b/src/libtracelog/libtracelog.c index c3fd40a67..90fe726de 100644 --- a/src/libtracelog/libtracelog.c +++ b/src/libtracelog/libtracelog.c | |||
@@ -31,6 +31,7 @@ | |||
31 | #include <sys/stat.h> | 31 | #include <sys/stat.h> |
32 | #include <syslog.h> | 32 | #include <syslog.h> |
33 | #include <dirent.h> | 33 | #include <dirent.h> |
34 | #include <limits.h> | ||
34 | 35 | ||
35 | //#define DEBUG | 36 | //#define DEBUG |
36 | 37 | ||
@@ -91,9 +92,9 @@ static void storage_add(const char *str) { | |||
91 | storage[h] = ptr; | 92 | storage[h] = ptr; |
92 | } | 93 | } |
93 | 94 | ||
94 | char* cwd = NULL; // global variable for keeping current working directory | 95 | // global variable to keep current working directory |
95 | typedef int (*orig_chdir_t)(const char *pathname); | 96 | static char* cwd = NULL; |
96 | static orig_chdir_t orig_chdir = NULL; | 97 | |
97 | static char *storage_find(const char *str) { | 98 | static char *storage_find(const char *str) { |
98 | #ifdef DEBUG | 99 | #ifdef DEBUG |
99 | printf("storage find %s\n", str); | 100 | printf("storage find %s\n", str); |
@@ -107,17 +108,23 @@ static char *storage_find(const char *str) { | |||
107 | const char *tofind = str; | 108 | const char *tofind = str; |
108 | int allocated = 0; | 109 | int allocated = 0; |
109 | 110 | ||
110 | if (strstr(str, "..") || strstr(str, "/./") || strstr(str, "//") || str[0]!='/') { | 111 | if (strstr(str, "..") || strstr(str, "/./") || strstr(str, "//") || str[0] != '/') { |
111 | if (!orig_chdir) | 112 | if (cwd != NULL && str[0] != '/') { |
112 | orig_chdir = (orig_chdir_t)dlsym(RTLD_NEXT, "chdir"); | 113 | char *fullpath=malloc(PATH_MAX); |
113 | if (!orig_chdir(cwd)) { | 114 | if (!fullpath) { |
114 | #ifdef DEBUG | 115 | fprintf(stderr, "Error: cannot allocate memory\n"); |
115 | printf("chdir failed\n"); | 116 | return NULL; |
116 | #endif | 117 | } |
117 | return NULL; | 118 | if (snprintf(fullpath, PATH_MAX, "%s/%s", cwd, str)<3) { |
119 | fprintf(stderr, "Error: snprintf failed\n"); | ||
120 | free(fullpath); | ||
121 | return NULL; | ||
122 | } | ||
123 | tofind = realpath(fullpath, NULL); | ||
124 | free(fullpath); | ||
125 | } else { | ||
126 | tofind = realpath(str, NULL); | ||
118 | } | 127 | } |
119 | |||
120 | tofind = realpath(str, NULL); | ||
121 | if (!tofind) { | 128 | if (!tofind) { |
122 | #ifdef DEBUG | 129 | #ifdef DEBUG |
123 | printf("realpath failed\n"); | 130 | printf("realpath failed\n"); |
@@ -156,9 +163,9 @@ static char *storage_find(const char *str) { | |||
156 | #define RUN_FSLOGGER_FILE "/run/firejail/mnt/fslogger" | 163 | #define RUN_FSLOGGER_FILE "/run/firejail/mnt/fslogger" |
157 | #define MAXBUF 4096 | 164 | #define MAXBUF 4096 |
158 | static int blacklist_loaded = 0; | 165 | static int blacklist_loaded = 0; |
159 | static char *sandbox_pid_str = 0; | 166 | static char *sandbox_pid_str = NULL; |
160 | static char *sandbox_name_str = NULL; | 167 | static char *sandbox_name_str = NULL; |
161 | void load_blacklist(void) { | 168 | static void load_blacklist(void) { |
162 | if (blacklist_loaded) | 169 | if (blacklist_loaded) |
163 | return; | 170 | return; |
164 | 171 | ||
@@ -177,13 +184,15 @@ void load_blacklist(void) { | |||
177 | char *ptr = strchr(buf, '\n'); | 184 | char *ptr = strchr(buf, '\n'); |
178 | if (ptr) | 185 | if (ptr) |
179 | *ptr = '\0'; | 186 | *ptr = '\0'; |
180 | sandbox_pid_str = strdup(buf + 13); | 187 | if (sandbox_pid_str == NULL) |
188 | sandbox_pid_str = strdup(buf + 13); | ||
181 | } | 189 | } |
182 | else if (strncmp(buf, "sandbox name: ", 14) == 0) { | 190 | else if (strncmp(buf, "sandbox name: ", 14) == 0) { |
183 | char *ptr = strchr(buf, '\n'); | 191 | char *ptr = strchr(buf, '\n'); |
184 | if (ptr) | 192 | if (ptr) |
185 | *ptr = '\0'; | 193 | *ptr = '\0'; |
186 | sandbox_name_str = strdup(buf + 14); | 194 | if (sandbox_name_str == NULL) |
195 | sandbox_name_str = strdup(buf + 14); | ||
187 | } | 196 | } |
188 | else if (strncmp(buf, "blacklist ", 10) == 0) { | 197 | else if (strncmp(buf, "blacklist ", 10) == 0) { |
189 | char *ptr = strchr(buf, '\n'); | 198 | char *ptr = strchr(buf, '\n'); |
@@ -556,7 +565,7 @@ int stat64(const char *pathname, struct stat64 *buf) { | |||
556 | #ifdef DEBUG | 565 | #ifdef DEBUG |
557 | printf("%s %s\n", __FUNCTION__, pathname); | 566 | printf("%s %s\n", __FUNCTION__, pathname); |
558 | #endif | 567 | #endif |
559 | if (!orig_stat) | 568 | if (!orig_stat64) |
560 | orig_stat64 = (orig_stat64_t)dlsym(RTLD_NEXT, "stat64"); | 569 | orig_stat64 = (orig_stat64_t)dlsym(RTLD_NEXT, "stat64"); |
561 | if (!blacklist_loaded) | 570 | if (!blacklist_loaded) |
562 | load_blacklist(); | 571 | load_blacklist(); |
@@ -592,7 +601,7 @@ int lstat64(const char *pathname, struct stat64 *buf) { | |||
592 | #ifdef DEBUG | 601 | #ifdef DEBUG |
593 | printf("%s %s\n", __FUNCTION__, pathname); | 602 | printf("%s %s\n", __FUNCTION__, pathname); |
594 | #endif | 603 | #endif |
595 | if (!orig_lstat) | 604 | if (!orig_lstat64) |
596 | orig_lstat64 = (orig_lstat64_t)dlsym(RTLD_NEXT, "lstat64"); | 605 | orig_lstat64 = (orig_lstat64_t)dlsym(RTLD_NEXT, "lstat64"); |
597 | if (!blacklist_loaded) | 606 | if (!blacklist_loaded) |
598 | load_blacklist(); | 607 | load_blacklist(); |
@@ -641,9 +650,8 @@ DIR *opendir(const char *pathname) { | |||
641 | } | 650 | } |
642 | 651 | ||
643 | // chdir | 652 | // chdir |
644 | // definition of orig_chdir placed before storage_find function | 653 | typedef int (*orig_chdir_t)(const char *pathname); |
645 | //typedef int (*orig_chdir_t)(const char *pathname); | 654 | static orig_chdir_t orig_chdir = NULL; |
646 | //static orig_chdir_t orig_chdir = NULL; | ||
647 | int chdir(const char *pathname) { | 655 | int chdir(const char *pathname) { |
648 | #ifdef DEBUG | 656 | #ifdef DEBUG |
649 | printf("%s %s\n", __FUNCTION__, pathname); | 657 | printf("%s %s\n", __FUNCTION__, pathname); |
@@ -662,3 +670,32 @@ int chdir(const char *pathname) { | |||
662 | int rv = orig_chdir(pathname); | 670 | int rv = orig_chdir(pathname); |
663 | return rv; | 671 | return rv; |
664 | } | 672 | } |
673 | |||
674 | // fchdir | ||
675 | typedef int (*orig_fchdir_t)(int fd); | ||
676 | static orig_fchdir_t orig_fchdir = NULL; | ||
677 | int fchdir(int fd) { | ||
678 | #ifdef DEBUG | ||
679 | printf("%s %d\n", __FUNCTION__, fd); | ||
680 | #endif | ||
681 | if (!orig_fchdir) | ||
682 | orig_fchdir = (orig_fchdir_t)dlsym(RTLD_NEXT, "fchdir"); | ||
683 | |||
684 | free(cwd); | ||
685 | char *pathname=malloc(PATH_MAX); | ||
686 | if (pathname) { | ||
687 | if (snprintf(pathname,PATH_MAX,"/proc/self/fd/%d", fd)>0) { | ||
688 | cwd = realpath(pathname, NULL); | ||
689 | } else { | ||
690 | cwd = NULL; | ||
691 | fprintf(stderr, "Error: snprintf failed\n"); | ||
692 | } | ||
693 | free(pathname); | ||
694 | } else { | ||
695 | fprintf(stderr, "Error: cannot allocate memory\n"); | ||
696 | cwd = NULL; | ||
697 | } | ||
698 | |||
699 | int rv = orig_fchdir(fd); | ||
700 | return rv; | ||
701 | } | ||