From 72e3b70bff2d9b3a8ce678b3169fc232f3dfcff3 Mon Sep 17 00:00:00 2001 From: mothcompute Date: Wed, 13 Dec 2023 21:05:35 -0800 Subject: [PATCH] cleanup - needs linux testing --- .gitignore | 2 ++ build | 2 +- main.c | 40 +++++++++++++++------------------------- 3 files changed, 18 insertions(+), 26 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ffec365 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +th.o +fl diff --git a/build b/build index ee7d2a9..b08d0d0 100755 --- a/build +++ b/build @@ -1,2 +1,2 @@ nasm -felf64 th.s -cc -ggdb3 main.c th.o -o fl -pie #-fno-PIC +cc -ggdb3 main.c th.o -o fl -pie -fPIC diff --git a/main.c b/main.c index 26bf63e..244a648 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,5 @@ #include #include -#include #include #include #include @@ -9,9 +8,12 @@ #include #include +extern void cat(int fd, long sz); + +#ifndef __FreeBSD__ +#include #define SYSCT_FBSD 588 -extern void cat(int fd, long sz); uint8_t sel = SYSCALL_DISPATCH_FILTER_ALLOW; uint16_t scmap[SYSCT_FBSD] = { @@ -62,6 +64,7 @@ void systrap(int n, siginfo_t* s, ucontext_t* c) { //for(int i = 0; i < 23; i++) printf("%i: %lX\n", i, c->uc_mcontext.gregs[i]); sel = SYSCALL_DISPATCH_FILTER_BLOCK; } +#endif typedef struct { uint8_t magic[4]; @@ -85,6 +88,7 @@ typedef struct { uint16_t nameind; } __attribute__((packed)) elf; +#ifndef __FreeBSD__ char** getmaps(int* tsz) { int fd = open("/proc/self/maps", O_RDONLY); char c; @@ -145,8 +149,10 @@ void stomap(char* s, map* m) { m->name = memcpy(malloc(d), s, d); m->len -= m->start; } +#endif int main(int argc, char** argv) { +#ifndef __FreeBSD__ struct sigaction a, oa; sigemptyset(&a.sa_mask); a.sa_flags = SA_SIGINFO; @@ -155,38 +161,22 @@ int main(int argc, char** argv) { int nm; char** smaps = getmaps(&nm); - // TODO RW to only call stomap if strstr libc - saves memory and cycles - map maps[nm-1]; - for(int i = 0; i < nm-1; i++) { - stomap(smaps[i], maps + i); - /* - printf( - "map:\t%s\n" - "name:\t%s\n" - "start:\t%016lX\n" - "length:\t%016lX\n" - "access:\t%c%c%c\n" - , - smaps[i], - maps[i].name, - maps[i].start, - maps[i].len, - ".R"[(maps[i].rwx >> 2) & 1], - ".W"[(maps[i].rwx >> 1) & 1], - ".X"[(maps[i].rwx >> 0) & 1] - ); - */ - if(strstr(maps[i].name, "libc") && (maps[i].rwx & 1)) prctl(PR_SET_SYSCALL_USER_DISPATCH, PR_SYS_DISPATCH_ON, maps[i].start, maps[i].len, &sel); + for(int i = 0; i < nm-1; i++) if(strstr(smaps[i], "libc")) { + map rmap; + stomap(smaps[i], &rmap); + if(rmap.rwx & 1) prctl(PR_SET_SYSCALL_USER_DISPATCH, PR_SYS_DISPATCH_ON, rmap.start, rmap.len, &sel); } for(int i = 0; i < nm; i++) free(smaps[i]); free(smaps); - // END RW +#endif if(argc < 2) return printf("no file provided\n"); int fd = open(argv[1], O_RDONLY); struct stat s; fstat(fd, &s); +#ifndef __FreeBSD__ sigaction(SIGSYS, &a, &oa); sel = SYSCALL_DISPATCH_FILTER_BLOCK; +#endif cat(fd, s.st_size); }