aboutsummaryrefslogtreecommitdiffstats
path: root/src/libtracelog/libtracelog.c
diff options
context:
space:
mode:
authorLibravatar Aleksey Manevich <manevich.aleksey@gmail.com>2016-07-06 08:00:45 +0300
committerLibravatar GitHub <noreply@github.com>2016-07-06 08:00:45 +0300
commitce72091a8690a6274dcae68bdc4dc5555ae1406e (patch)
tree17506c7b019473b2e2502ca3620776987380eac4 /src/libtracelog/libtracelog.c
parentaudit feature: bash completion (diff)
downloadfirejail-ce72091a8690a6274dcae68bdc4dc5555ae1406e.tar.gz
firejail-ce72091a8690a6274dcae68bdc4dc5555ae1406e.tar.zst
firejail-ce72091a8690a6274dcae68bdc4dc5555ae1406e.zip
Fix chdir bug in libtracelog
Diffstat (limited to 'src/libtracelog/libtracelog.c')
-rw-r--r--src/libtracelog/libtracelog.c66
1 files changed, 50 insertions, 16 deletions
diff --git a/src/libtracelog/libtracelog.c b/src/libtracelog/libtracelog.c
index c3fd40a67..0a6d8299f 100644
--- a/src/libtracelog/libtracelog.c
+++ b/src/libtracelog/libtracelog.c
@@ -91,9 +91,9 @@ static void storage_add(const char *str) {
91 storage[h] = ptr; 91 storage[h] = ptr;
92} 92}
93 93
94char* cwd = NULL; // global variable for keeping current working directory 94// global variable to keep current working directory
95typedef int (*orig_chdir_t)(const char *pathname); 95char* cwd = NULL;
96static orig_chdir_t orig_chdir = NULL; 96
97static char *storage_find(const char *str) { 97static char *storage_find(const char *str) {
98#ifdef DEBUG 98#ifdef DEBUG
99 printf("storage find %s\n", str); 99 printf("storage find %s\n", str);
@@ -107,17 +107,23 @@ static char *storage_find(const char *str) {
107 const char *tofind = str; 107 const char *tofind = str;
108 int allocated = 0; 108 int allocated = 0;
109 109
110 if (strstr(str, "..") || strstr(str, "/./") || strstr(str, "//") || str[0]!='/') { 110 if (strstr(str, "..") || strstr(str, "/./") || strstr(str, "//") || str[0] != '/') {
111 if (!orig_chdir) 111 if (cwd != NULL & str[0] != '/') {
112 orig_chdir = (orig_chdir_t)dlsym(RTLD_NEXT, "chdir"); 112 char *fullpath=malloc(PATH_MAX);
113 if (!orig_chdir(cwd)) { 113 if (!fullpath) {
114#ifdef DEBUG 114 fprintf(stderr, "Error: cannot allocate memory\n");
115 printf("chdir failed\n"); 115 return NULL;
116#endif 116 }
117 return NULL; 117 if (snprintf(fullpath, PATH_MAX, "%s/%s", cwd, str)<3) {
118 fprintf(stderr, "Error: snprintf failed\n");
119 free(fullpath);
120 return NULL;
121 }
122 tofind = realpath(fullpath, NULL);
123 free(fullpath);
124 } else {
125 tofind = realpath(str, NULL);
118 } 126 }
119
120 tofind = realpath(str, NULL);
121 if (!tofind) { 127 if (!tofind) {
122#ifdef DEBUG 128#ifdef DEBUG
123 printf("realpath failed\n"); 129 printf("realpath failed\n");
@@ -641,9 +647,8 @@ DIR *opendir(const char *pathname) {
641} 647}
642 648
643// chdir 649// chdir
644// definition of orig_chdir placed before storage_find function 650typedef int (*orig_chdir_t)(const char *pathname);
645//typedef int (*orig_chdir_t)(const char *pathname); 651static orig_chdir_t orig_chdir = NULL;
646//static orig_chdir_t orig_chdir = NULL;
647int chdir(const char *pathname) { 652int chdir(const char *pathname) {
648#ifdef DEBUG 653#ifdef DEBUG
649 printf("%s %s\n", __FUNCTION__, pathname); 654 printf("%s %s\n", __FUNCTION__, pathname);
@@ -662,3 +667,32 @@ int chdir(const char *pathname) {
662 int rv = orig_chdir(pathname); 667 int rv = orig_chdir(pathname);
663 return rv; 668 return rv;
664} 669}
670
671// fchdir
672typedef int (*orig_fchdir_t)(int fd);
673static orig_fchdir_t orig_fchdir = NULL;
674int fchdir(int fd) {
675#ifdef DEBUG
676 printf("%s %d\n", __FUNCTION__, fd);
677#endif
678 if (!orig_fchdir)
679 orig_fchdir = (orig_fchdir_t)dlsym(RTLD_NEXT, "fchdir");
680
681 free(cwd);
682 char *pathname=malloc(PATH_MAX);
683 if (pathname) {
684 if (snprintf(pathname,PATH_MAX,"/proc/self/fd/%d", fd)>0) {
685 cwd = realpath(pathname, NULL);
686 } else {
687 cwd = NULL;
688 fprintf(stderr, "Error: snprintf failed\n");
689 }
690 free(pathname);
691 } else {
692 fprintf(stderr, "Error: cannot allocate memory\n");
693 cwd = NULL;
694 }
695
696 int rv = orig_fchdir(fd);
697 return rv;
698}