From c47175b9a92c4d9b0a206f53162650b06d3bcd7c Mon Sep 17 00:00:00 2001 From: netblue30 Date: Sat, 17 Aug 2019 20:37:50 -0400 Subject: fix --trace --- src/libtrace/libtrace.c | 154 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) (limited to 'src/libtrace/libtrace.c') diff --git a/src/libtrace/libtrace.c b/src/libtrace/libtrace.c index 60fdb5470..71a1234cc 100644 --- a/src/libtrace/libtrace.c +++ b/src/libtrace/libtrace.c @@ -43,7 +43,16 @@ static orig_fopen64_t orig_fopen64 = NULL; // // library constructor/destructor // +//#define PRINTF_DEVTTY +#ifdef PRINTF_DEVTTY +// Replacing printf with fprintf to /dev/tty in order to fix #561 +// In some cases this crashes the program running in the sandbox. +// Example: +// $ firejail --trace mkdir ttt +// logs the following error in syslog: +// debian kernel: [18521.399073] mkdir[12206]: segfault at 0 ip 00007f77ebf41f6b sp 00007ffe1a0161e0 error 4 in libc-2.24.so[7f77ebe4b000+195000] static FILE *ftty = NULL; +#endif static pid_t mypid = 0; #define MAXNAME 16 static char myname[MAXNAME] = {'\0', }; @@ -53,7 +62,10 @@ void init(void) { orig_fopen = (orig_fopen_t)dlsym(RTLD_NEXT, "fopen"); // tty +#ifdef PRINTF_DEVTTY ftty = orig_fopen("/dev/tty", "w"); +printf("*** ftty %p ***\n", ftty); +#endif // pid mypid = getpid(); @@ -79,7 +91,9 @@ void init(void) { static void fini(void) __attribute__((destructor)); void fini(void) { +#ifdef PRINTF_DEVTTY fclose(ftty); +#endif } // @@ -226,23 +240,43 @@ static char *translate(XTable *table, int val) { static void print_sockaddr(int sockfd, const char *call, const struct sockaddr *addr, int rv) { if (addr->sa_family == AF_INET) { struct sockaddr_in *a = (struct sockaddr_in *) addr; +#ifdef PRINTF_DEVTTY fprintf(ftty, "%u:%s:%s %d %s port %u:%d\n", mypid, myname, call, sockfd, inet_ntoa(a->sin_addr), ntohs(a->sin_port), rv); +#else + printf("%u:%s:%s %d %s port %u:%d\n", mypid, myname, call, sockfd, inet_ntoa(a->sin_addr), ntohs(a->sin_port), rv); +#endif } else if (addr->sa_family == AF_INET6) { struct sockaddr_in6 *a = (struct sockaddr_in6 *) addr; char str[INET6_ADDRSTRLEN]; inet_ntop(AF_INET6, &(a->sin6_addr), str, INET6_ADDRSTRLEN); +#ifdef PRINTF_DEVTTY fprintf(ftty, "%u:%s:%s %d %s:%d\n", mypid, myname, call, sockfd, str, rv); +#else + printf("%u:%s:%s %d %s:%d\n", mypid, myname, call, sockfd, str, rv); +#endif } else if (addr->sa_family == AF_UNIX) { struct sockaddr_un *a = (struct sockaddr_un *) addr; if (a->sun_path[0]) +#ifdef PRINTF_DEVTTY fprintf(ftty, "%u:%s:%s %d %s:%d\n", mypid, myname, call, sockfd, a->sun_path, rv); +#else + printf("%u:%s:%s %d %s:%d\n", mypid, myname, call, sockfd, a->sun_path, rv); +#endif else +#ifdef PRINTF_DEVTTY fprintf(ftty, "%u:%s:%s %d @%s:%d\n", mypid, myname, call, sockfd, a->sun_path + 1, rv); +#else + printf("%u:%s:%s %d @%s:%d\n", mypid, myname, call, sockfd, a->sun_path + 1, rv); +#endif } else { +#ifdef PRINTF_DEVTTY fprintf(ftty, "%u:%s:%s %d family %d:%d\n", mypid, myname, call, sockfd, addr->sa_family, rv); +#else + printf("%u:%s:%s %d family %d:%d\n", mypid, myname, call, sockfd, addr->sa_family, rv); +#endif } } @@ -258,7 +292,11 @@ int open(const char *pathname, int flags, mode_t mode) { orig_open = (orig_open_t)dlsym(RTLD_NEXT, "open"); int rv = orig_open(pathname, flags, mode); +#ifdef PRINTF_DEVTTY fprintf(ftty, "%u:%s:open %s:%d\n", mypid, myname, pathname, rv); +#else + printf("%u:%s:open %s:%d\n", mypid, myname, pathname, rv); +#endif return rv; } @@ -269,7 +307,11 @@ int open64(const char *pathname, int flags, mode_t mode) { orig_open64 = (orig_open64_t)dlsym(RTLD_NEXT, "open64"); int rv = orig_open64(pathname, flags, mode); +#ifdef PRINTF_DEVTTY fprintf(ftty, "%u:%s:open64 %s:%d\n", mypid, myname, pathname, rv); +#else + printf("%u:%s:open64 %s:%d\n", mypid, myname, pathname, rv); +#endif return rv; } @@ -281,7 +323,11 @@ int openat(int dirfd, const char *pathname, int flags, mode_t mode) { orig_openat = (orig_openat_t)dlsym(RTLD_NEXT, "openat"); int rv = orig_openat(dirfd, pathname, flags, mode); +#ifdef PRINTF_DEVTTY fprintf(ftty, "%u:%s:openat %s:%d\n", mypid, myname, pathname, rv); +#else + printf("%u:%s:openat %s:%d\n", mypid, myname, pathname, rv); +#endif return rv; } @@ -292,7 +338,11 @@ int openat64(int dirfd, const char *pathname, int flags, mode_t mode) { orig_openat64 = (orig_openat64_t)dlsym(RTLD_NEXT, "openat64"); int rv = orig_openat64(dirfd, pathname, flags, mode); +#ifdef PRINTF_DEVTTY fprintf(ftty, "%u:%s:openat64 %s:%d\n", mypid, myname, pathname, rv); +#else + printf("%u:%s:openat64 %s:%d\n", mypid, myname, pathname, rv); +#endif return rv; } @@ -303,7 +353,11 @@ FILE *fopen(const char *pathname, const char *mode) { orig_fopen = (orig_fopen_t)dlsym(RTLD_NEXT, "fopen"); FILE *rv = orig_fopen(pathname, mode); +#ifdef PRINTF_DEVTTY fprintf(ftty, "%u:%s:fopen %s:%p\n", mypid, myname, pathname, rv); +#else + printf("%u:%s:fopen %s:%p\n", mypid, myname, pathname, rv); +#endif return rv; } @@ -313,7 +367,11 @@ FILE *fopen64(const char *pathname, const char *mode) { orig_fopen64 = (orig_fopen_t)dlsym(RTLD_NEXT, "fopen64"); FILE *rv = orig_fopen64(pathname, mode); +#ifdef PRINTF_DEVTTY fprintf(ftty, "%u:%s:fopen64 %s:%p\n", mypid, myname, pathname, rv); +#else + printf("%u:%s:fopen64 %s:%p\n", mypid, myname, pathname, rv); +#endif return rv; } #endif /* __GLIBC__ */ @@ -327,7 +385,11 @@ FILE *freopen(const char *pathname, const char *mode, FILE *stream) { orig_freopen = (orig_freopen_t)dlsym(RTLD_NEXT, "freopen"); FILE *rv = orig_freopen(pathname, mode, stream); +#ifdef PRINTF_DEVTTY fprintf(ftty, "%u:%s:freopen %s:%p\n", mypid, myname, pathname, rv); +#else + printf("%u:%s:freopen %s:%p\n", mypid, myname, pathname, rv); +#endif return rv; } @@ -339,7 +401,11 @@ FILE *freopen64(const char *pathname, const char *mode, FILE *stream) { orig_freopen64 = (orig_freopen64_t)dlsym(RTLD_NEXT, "freopen64"); FILE *rv = orig_freopen64(pathname, mode, stream); +#ifdef PRINTF_DEVTTY fprintf(ftty, "%u:%s:freopen64 %s:%p\n", mypid, myname, pathname, rv); +#else + printf("%u:%s:freopen64 %s:%p\n", mypid, myname, pathname, rv); +#endif return rv; } #endif /* __GLIBC__ */ @@ -352,7 +418,11 @@ int unlink(const char *pathname) { orig_unlink = (orig_unlink_t)dlsym(RTLD_NEXT, "unlink"); int rv = orig_unlink(pathname); +#ifdef PRINTF_DEVTTY fprintf(ftty, "%u:%s:unlink %s:%d\n", mypid, myname, pathname, rv); +#else + printf("%u:%s:unlink %s:%d\n", mypid, myname, pathname, rv); +#endif return rv; } @@ -363,7 +433,11 @@ int unlinkat(int dirfd, const char *pathname, int flags) { orig_unlinkat = (orig_unlinkat_t)dlsym(RTLD_NEXT, "unlinkat"); int rv = orig_unlinkat(dirfd, pathname, flags); +#ifdef PRINTF_DEVTTY fprintf(ftty, "%u:%s:unlinkat %s:%d\n", mypid, myname, pathname, rv); +#else + printf("%u:%s:unlinkat %s:%d\n", mypid, myname, pathname, rv); +#endif return rv; } @@ -375,7 +449,11 @@ int mkdir(const char *pathname, mode_t mode) { orig_mkdir = (orig_mkdir_t)dlsym(RTLD_NEXT, "mkdir"); int rv = orig_mkdir(pathname, mode); +#ifdef PRINTF_DEVTTY fprintf(ftty, "%u:%s:mkdir %s:%d\n", mypid, myname, pathname, rv); +#else + printf("%u:%s:mkdir %s:%d\n", mypid, myname, pathname, rv); +#endif return rv; } @@ -386,7 +464,11 @@ int mkdirat(int dirfd, const char *pathname, mode_t mode) { orig_mkdirat = (orig_mkdirat_t)dlsym(RTLD_NEXT, "mkdirat"); int rv = orig_mkdirat(dirfd, pathname, mode); +#ifdef PRINTF_DEVTTY fprintf(ftty, "%u:%s:mkdirat %s:%d\n", mypid, myname, pathname, rv); +#else + printf("%u:%s:mkdirat %s:%d\n", mypid, myname, pathname, rv); +#endif return rv; } @@ -397,7 +479,11 @@ int rmdir(const char *pathname) { orig_rmdir = (orig_rmdir_t)dlsym(RTLD_NEXT, "rmdir"); int rv = orig_rmdir(pathname); +#ifdef PRINTF_DEVTTY fprintf(ftty, "%u:%s:rmdir %s:%d\n", mypid, myname, pathname, rv); +#else + printf("%u:%s:rmdir %s:%d\n", mypid, myname, pathname, rv); +#endif return rv; } @@ -409,7 +495,11 @@ int stat(const char *pathname, struct stat *statbuf) { orig_stat = (orig_stat_t)dlsym(RTLD_NEXT, "stat"); int rv = orig_stat(pathname, statbuf); +#ifdef PRINTF_DEVTTY fprintf(ftty, "%u:%s:stat %s:%d\n", mypid, myname, pathname, rv); +#else + printf("%u:%s:stat %s:%d\n", mypid, myname, pathname, rv); +#endif return rv; } @@ -421,7 +511,11 @@ int stat64(const char *pathname, struct stat64 *statbuf) { orig_stat64 = (orig_stat64_t)dlsym(RTLD_NEXT, "stat64"); int rv = orig_stat64(pathname, statbuf); +#ifdef PRINTF_DEVTTY fprintf(ftty, "%u:%s:stat64 %s:%d\n", mypid, myname, pathname, rv); +#else + printf("%u:%s:stat64 %s:%d\n", mypid, myname, pathname, rv); +#endif return rv; } #endif /* __GLIBC__ */ @@ -434,7 +528,11 @@ int lstat(const char *pathname, struct stat *statbuf) { orig_lstat = (orig_lstat_t)dlsym(RTLD_NEXT, "lstat"); int rv = orig_lstat(pathname, statbuf); +#ifdef PRINTF_DEVTTY fprintf(ftty, "%u:%s:lstat %s:%d\n", mypid, myname, pathname, rv); +#else + printf("%u:%s:lstat %s:%d\n", mypid, myname, pathname, rv); +#endif return rv; } @@ -446,7 +544,11 @@ int lstat64(const char *pathname, struct stat64 *statbuf) { orig_lstat64 = (orig_lstat64_t)dlsym(RTLD_NEXT, "lstat64"); int rv = orig_lstat64(pathname, statbuf); +#ifdef PRINTF_DEVTTY fprintf(ftty, "%u:%s:lstat64 %s:%d\n", mypid, myname, pathname, rv); +#else + printf("%u:%s:lstat64 %s:%d\n", mypid, myname, pathname, rv); +#endif return rv; } #endif /* __GLIBC__ */ @@ -459,7 +561,11 @@ DIR *opendir(const char *pathname) { orig_opendir = (orig_opendir_t)dlsym(RTLD_NEXT, "opendir"); DIR *rv = orig_opendir(pathname); +#ifdef PRINTF_DEVTTY fprintf(ftty, "%u:%s:opendir %s:%p\n", mypid, myname, pathname, rv); +#else + printf("%u:%s:opendir %s:%p\n", mypid, myname, pathname, rv); +#endif return rv; } @@ -471,7 +577,11 @@ int access(const char *pathname, int mode) { orig_access = (orig_access_t)dlsym(RTLD_NEXT, "access"); int rv = orig_access(pathname, mode); +#ifdef PRINTF_DEVTTY fprintf(ftty, "%u:%s:access %s:%d\n", mypid, myname, pathname, rv); +#else + printf("%u:%s:access %s:%d\n", mypid, myname, pathname, rv); +#endif return rv; } @@ -529,7 +639,11 @@ int socket(int domain, int type, int protocol) { sprintf(ptr, "%s", str); } +#ifdef PRINTF_DEVTTY fprintf(ftty, "%s:%d\n", socketbuf, rv); +#else + printf("%s:%d\n", socketbuf, rv); +#endif return rv; } @@ -567,7 +681,11 @@ int system(const char *command) { orig_system = (orig_system_t)dlsym(RTLD_NEXT, "system"); int rv = orig_system(command); +#ifdef PRINTF_DEVTTY fprintf(ftty, "%u:%s:system %s:%d\n", mypid, myname, command, rv); +#else + printf("%u:%s:system %s:%d\n", mypid, myname, command, rv); +#endif return rv; } @@ -579,7 +697,11 @@ int setuid(uid_t uid) { orig_setuid = (orig_setuid_t)dlsym(RTLD_NEXT, "setuid"); int rv = orig_setuid(uid); +#ifdef PRINTF_DEVTTY fprintf(ftty, "%u:%s:setuid %d:%d\n", mypid, myname, uid, rv); +#else + printf("%u:%s:setuid %d:%d\n", mypid, myname, uid, rv); +#endif return rv; } @@ -591,7 +713,11 @@ int setgid(gid_t gid) { orig_setgid = (orig_setgid_t)dlsym(RTLD_NEXT, "setgid"); int rv = orig_setgid(gid); +#ifdef PRINTF_DEVTTY fprintf(ftty, "%u:%s:setgid %d:%d\n", mypid, myname, gid, rv); +#else + printf("%u:%s:setgid %d:%d\n", mypid, myname, gid, rv); +#endif return rv; } @@ -603,7 +729,11 @@ int setfsuid(uid_t uid) { orig_setfsuid = (orig_setfsuid_t)dlsym(RTLD_NEXT, "setfsuid"); int rv = orig_setfsuid(uid); +#ifdef PRINTF_DEVTTY fprintf(ftty, "%u:%s:setfsuid %d:%d\n", mypid, myname, uid, rv); +#else + printf("%u:%s:setfsuid %d:%d\n", mypid, myname, uid, rv); +#endif return rv; } @@ -615,7 +745,11 @@ int setfsgid(gid_t gid) { orig_setfsgid = (orig_setfsgid_t)dlsym(RTLD_NEXT, "setfsgid"); int rv = orig_setfsgid(gid); +#ifdef PRINTF_DEVTTY fprintf(ftty, "%u:%s:setfsgid %d:%d\n", mypid, myname, gid, rv); +#else + printf("%u:%s:setfsgid %d:%d\n", mypid, myname, gid, rv); +#endif return rv; } @@ -627,7 +761,11 @@ int setreuid(uid_t ruid, uid_t euid) { orig_setreuid = (orig_setreuid_t)dlsym(RTLD_NEXT, "setreuid"); int rv = orig_setreuid(ruid, euid); +#ifdef PRINTF_DEVTTY fprintf(ftty, "%u:%s:setreuid %d %d:%d\n", mypid, myname, ruid, euid, rv); +#else + printf("%u:%s:setreuid %d %d:%d\n", mypid, myname, ruid, euid, rv); +#endif return rv; } @@ -639,7 +777,11 @@ int setregid(gid_t rgid, gid_t egid) { orig_setregid = (orig_setregid_t)dlsym(RTLD_NEXT, "setregid"); int rv = orig_setregid(rgid, egid); +#ifdef PRINTF_DEVTTY fprintf(ftty, "%u:%s:setregid %d %d:%d\n", mypid, myname, rgid, egid, rv); +#else + printf("%u:%s:setregid %d %d:%d\n", mypid, myname, rgid, egid, rv); +#endif return rv; } @@ -651,7 +793,11 @@ int setresuid(uid_t ruid, uid_t euid, uid_t suid) { orig_setresuid = (orig_setresuid_t)dlsym(RTLD_NEXT, "setresuid"); int rv = orig_setresuid(ruid, euid, suid); +#ifdef PRINTF_DEVTTY fprintf(ftty, "%u:%s:setresuid %d %d %d:%d\n", mypid, myname, ruid, euid, suid, rv); +#else + printf("%u:%s:setresuid %d %d %d:%d\n", mypid, myname, ruid, euid, suid, rv); +#endif return rv; } @@ -663,7 +809,11 @@ int setresgid(gid_t rgid, gid_t egid, gid_t sgid) { orig_setresgid = (orig_setresgid_t)dlsym(RTLD_NEXT, "setresgid"); int rv = orig_setresgid(rgid, egid, sgid); +#ifdef PRINTF_DEVTTY fprintf(ftty, "%u:%s:setresgid %d %d %d:%d\n", mypid, myname, rgid, egid, sgid, rv); +#else + printf("%u:%s:setresgid %d %d %d:%d\n", mypid, myname, rgid, egid, sgid, rv); +#endif return rv; } @@ -678,6 +828,10 @@ static void log_exec(int argc, char** argv) { int rv = readlink("/proc/self/exe", buf, PATH_MAX); if (rv != -1) { buf[rv] = '\0'; // readlink does not add a '\0' at the end +#ifdef PRINTF_DEVTTY fprintf(ftty, "%u:%s:exec %s:0\n", mypid, myname, buf); +#else + printf("%u:%s:exec %s:0\n", mypid, myname, buf); +#endif } } -- cgit v1.2.3-70-g09d2