| 1 | #define _GNU_SOURCE |
| 2 | /* |
| 3 | #include <stdlib.h> |
| 4 | #include <sys/types.h> |
| 5 | #include <unistd.h> |
| 6 | #include <stdarg.h> |
| 7 | #include <time.h> |
| 8 | #include <string.h> |
| 9 | |
| 10 | #include <sys/types.h> |
| 11 | #include <sys/stat.h> |
| 12 | #include <fcntl.h> |
| 13 | */ |
| 14 | #include <dlfcn.h> |
| 15 | #include <stdio.h> |
| 16 | #include <sys/syscall.h> |
| 17 | #include <linux/limits.h> |
| 18 | |
| 19 | #define i { srand(23); fprintf(stderr,"%s();\n",__func__); } |
| 20 | //Catch open() calls (while redirecting filename): |
| 21 | |
| 22 | static const char *fd_to_path(int fd) { |
| 23 | static char path[PATH_MAX]; |
| 24 | sprintf(path,"/proc/self/fd/%d",fd); |
| 25 | int l = (int) readlink((char * __restrict__) path, path, PATH_MAX); |
| 26 | path[l]=0; |
| 27 | return path; |
| 28 | } |
| 29 | |
| 30 | int close(int fd) { |
| 31 | //fputs(fd_to_path(fd),stderr); |
| 32 | fprintf(stderr,"%s(%d,%s);\n",__func__,fd,fd_to_path(fd)); |
| 33 | if(fd>2) return syscall(SYS_close,fd); |
| 34 | } |
| 35 | int fclose(FILE *fd) { |
| 36 | //fputs(fd_to_path(fd),stderr); |
| 37 | fprintf(stderr,"%s(%d,%d,%s);\n",__func__,fileno(fd),fd,fd_to_path(fileno(fd))); |
| 38 | static ssize_t (*fclose_orig)(FILE *fd); |
| 39 | if (!fclose_orig) { |
| 40 | fclose_orig = dlsym(RTLD_NEXT, "fclose"); |
| 41 | } |
| 42 | return fclose_orig(fd); |
| 43 | //return syscall(SYS_close,fileno(fd)); |
| 44 | } |
| 45 | /* |
| 46 | ssize_t write(int fd, const void *buf, size_t count) { |
| 47 | //fputs(fd_to_path(fd),stderr); |
| 48 | printf("%d",fd); |
| 49 | i; |
| 50 | return syscall(SYS_write,fd,buf,count); |
| 51 | } |
| 52 | */ |
| 53 | /* |
| 54 | static const char *redirect_name(const char *name) |
| 55 | { |
| 56 | if( |
| 57 | (strcmp(name,"/dev/random") == 0) || |
| 58 | (strcmp(name,"/dev/urandom") == 0) |
| 59 | ) { |
| 60 | printf("REDIRECT HIT: %s\n", name); |
| 61 | return "/dev/zero"; |
| 62 | } |
| 63 | return name; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | int open(const char *filename, int flags, ...) |
| 68 | { |
| 69 | //srand(23); |
| 70 | static int (*open_orig)(const char *, int, mode_t); |
| 71 | int ret; |
| 72 | va_list ap; |
| 73 | mode_t mode; |
| 74 | |
| 75 | if (!open_orig) { |
| 76 | open_orig = dlsym(RTLD_NEXT, "open"); |
| 77 | } |
| 78 | |
| 79 | va_start(ap, flags); |
| 80 | mode = va_arg(ap, mode_t); |
| 81 | va_end(ap); |
| 82 | |
| 83 | if(strcmp(filename,"/tmp/lol.txt.asc") != 0) { |
| 84 | ret = open_orig(redirect_name(filename), flags, mode); |
| 85 | //ret = syscall(SYS_open,redirect_name(filename),flags,mode); |
| 86 | } else { |
| 87 | if(flags & O_RDONLY) { |
| 88 | ret = fileno(popen("cat aaaa", "r")); |
| 89 | } else { |
| 90 | ret = fileno(popen("tee aaaa", "w")); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | printf("open(\"%s\", 0x%x, %o) -> %d\n", filename, flags, mode, ret); |
| 95 | |
| 96 | return ret; |
| 97 | } |
| 98 | |
| 99 | */ |