cleanup - needs linux testing

This commit is contained in:
'mr software' 2023-12-13 21:05:35 -08:00
parent 0e433c11a2
commit 72e3b70bff
3 changed files with 18 additions and 26 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
th.o
fl

2
build
View File

@ -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

40
main.c
View File

@ -1,6 +1,5 @@
#include <stdint.h>
#include <sys/syscall.h>
#include <sys/prctl.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
@ -9,9 +8,12 @@
#include <string.h>
#include <unistd.h>
extern void cat(int fd, long sz);
#ifndef __FreeBSD__
#include <sys/prctl.h>
#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);
}