diff options
author | 2016-07-06 21:13:07 -0400 | |
---|---|---|
committer | 2016-07-06 21:13:07 -0400 | |
commit | 2e8cd343c1104f69d6d50fa85a2936cea1e819ec (patch) | |
tree | 649d75d55bc6934b528b1dace8298ff02f7f226c | |
parent | Merge pull request #616 from haasn/master (diff) | |
parent | Fix chdir bug in libtracelog (diff) | |
download | firejail-2e8cd343c1104f69d6d50fa85a2936cea1e819ec.tar.gz firejail-2e8cd343c1104f69d6d50fa85a2936cea1e819ec.tar.zst firejail-2e8cd343c1104f69d6d50fa85a2936cea1e819ec.zip |
Merge pull request #617 from manevich/patch-1
Fix chdir bug in libtracelog
-rw-r--r-- | src/libtracelog/libtracelog.c | 66 |
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 | ||
94 | char* cwd = NULL; // global variable for keeping current working directory | 94 | // global variable to keep current working directory |
95 | typedef int (*orig_chdir_t)(const char *pathname); | 95 | char* cwd = NULL; |
96 | static orig_chdir_t orig_chdir = NULL; | 96 | |
97 | static char *storage_find(const char *str) { | 97 | static 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 | 650 | typedef int (*orig_chdir_t)(const char *pathname); |
645 | //typedef int (*orig_chdir_t)(const char *pathname); | 651 | static orig_chdir_t orig_chdir = NULL; |
646 | //static orig_chdir_t orig_chdir = NULL; | ||
647 | int chdir(const char *pathname) { | 652 | int 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 | ||
672 | typedef int (*orig_fchdir_t)(int fd); | ||
673 | static orig_fchdir_t orig_fchdir = NULL; | ||
674 | int 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 | } | ||