From a9a5c9f830eb1c44812bb7b3e3270cac9ff3d04f Mon Sep 17 00:00:00 2001 From: Aaron Kaiser Date: Thu, 4 Apr 2024 17:00:02 +0200 Subject: [PATCH] Add syscalls --- Makefile | 7 +- syscall/Makefile | 3 + syscall/jasmin_syscall.c | 145 +++++++++++++++++++++++++++++++++++++++ syscall/jasmin_syscall.h | 20 ++++++ syscall/jasmin_syscall.o | Bin 0 -> 3144 bytes 5 files changed, 173 insertions(+), 2 deletions(-) create mode 100644 syscall/Makefile create mode 100644 syscall/jasmin_syscall.c create mode 100644 syscall/jasmin_syscall.h create mode 100644 syscall/jasmin_syscall.o diff --git a/Makefile b/Makefile index 7d9a786..ed2498a 100644 --- a/Makefile +++ b/Makefile @@ -10,8 +10,8 @@ all: build build: build/agent.o ar -crs build/libagent.a build/agent.o -build/%.o: build/%.S - $(CC) $< -c -o $@ +build/%.o: build/%.S syscall/jasmin_syscall.o + $(CC) $< syscall/jasmin_syscall.o -c -o $@ clean: rm build/* 2> /dev/null || true @@ -21,3 +21,6 @@ $(JC): build/%.S: src/%.jazz $(JC) JASMINPATH="Jade=libjade/src/" $(JC) $< -o $@ + +syscall/jasmin_syscall.o: syscall/jasmin_syscall.c syscall/jasmin_syscall.h + $(MAKE) -C syscall diff --git a/syscall/Makefile b/syscall/Makefile new file mode 100644 index 0000000..e985027 --- /dev/null +++ b/syscall/Makefile @@ -0,0 +1,3 @@ +all: jasmin_syscall.o + +jasmin_syscall.o: jasmin_syscall.c jasmin_syscall.h diff --git a/syscall/jasmin_syscall.c b/syscall/jasmin_syscall.c new file mode 100644 index 0000000..7da50ea --- /dev/null +++ b/syscall/jasmin_syscall.c @@ -0,0 +1,145 @@ + +#include "jasmin_syscall.h" + +#if defined(__linux__) + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + +uint8_t* __jasmin_syscall_randombytes__(uint8_t* _x, uint64_t xlen) +{ + int i; + uint8_t* x = _x; + + printf("%lx:\n", x); + + for (i = 0; i < xlen; i++) + { + printf("%02X", _x[i]); + } + printf("\n"); + + + return _x; +} + +// uint64_t __jasmin_syscall_open__(uint8_t* x, uint64_t xlen) +// { +// uint8_t filename[xlen + 1]; +// memcpy(filename, x, xlen); +// filename[xlen] = 0; +// +// return (uint64_t)open(filename, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR); +// } +// +// uint8_t __jasmin_syscall_close__(uint64_t fd) +// { +// int success = close(fd); +// +// if (success == 0) { +// return 1; +// } else { +// return 0; +// } +// } + +uint64_t __jasmin_syscall_open__(uint8_t* x, uint64_t xlen) +{ + uint8_t* mem = (uint8_t*)mmap(NULL, 1024, PROT_READ|PROT_WRITE, MAP_SHARED, 3, 0); + if (mem == NULL) { + puts("mem error"); + exit(1); + } + uintptr_t addr = (uintptr_t)(mem - 4096); + void* futex_mem = mmap((void*)addr, 1024, PROT_READ|PROT_WRITE, MAP_SHARED, 4, 0); + if ((uintptr_t)futex_mem != addr) { + puts("mem error 2"); + exit(1); + } + + return (uint64_t)mem; +} + +// uint8_t* __jasmin_syscall_write__(uint8_t* _x, uint64_t xlen, uint64_t fd) +// { +// size_t i; +// uint8_t* x = _x; +// +// while (xlen > 0) { +// i = write(fd, x, xlen); +// if (i < 1) { +// continue; +// } +// x += i; +// xlen -= i; +// } +// +// return _x; +// } + +uint8_t* __jasmin_syscall_read__(uint8_t* _x, uint64_t xlen, uint64_t fd) +{ + uint32_t* addr = (uint32_t*)(uintptr_t)(fd - 4096); + long ret = syscall(SYS_futex, addr, FUTEX_WAIT, 0x1, NULL, NULL, 0); + if (ret == -1) { + printf("%d\n", errno); + perror("futex"); + } + + return _x; +} + +uint8_t* __jasmin_syscall_write__(uint8_t* _x, uint64_t xlen, uint64_t fd) +{ + uint32_t* addr = (uint32_t*)(uintptr_t)(fd - 4096); + *addr = 1; + syscall(SYS_futex, addr, FUTEX_WAKE, 1, NULL, NULL, 0); + + return _x; +} + +// uint8_t* __jasmin_syscall_read__(uint8_t* _x, uint64_t xlen, uint64_t fd) +// { +// size_t i; +// uint8_t* x = _x; +// +// i = read(fd, x, xlen); +// if (i < 1) { +// // Do something +// perror("Something went wrong while reading the file"); +// } +// x += i; +// xlen -= i; +// +// memset(x, 0, xlen); +// +// return _x; +// } + +#endif + +#if defined(__APPLE__) + +#include + +#if !(defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101200) +#error "macOS version not supported (>= 10.12)" +#endif + +uint8_t* __jasmin_syscall_randombytes__(uint8_t* x, uint64_t xlen){ + arc4random_buf(x, xlen); + return x; +} + +#endif diff --git a/syscall/jasmin_syscall.h b/syscall/jasmin_syscall.h new file mode 100644 index 0000000..7391622 --- /dev/null +++ b/syscall/jasmin_syscall.h @@ -0,0 +1,20 @@ +#include +#ifndef JASMIN_SYSCALL +#define JASMIN_SYSCALL +/* FIXME this need xlen to be Uptr */ +uint8_t* __jasmin_syscall_randombytes__(uint8_t* x, uint64_t xlen) +asm("__jasmin_syscall_randombytes__"); + +uint64_t __jasmin_syscall_open__(uint8_t* x, uint64_t xlen) +asm("__jasmin_syscall_open__"); + +uint8_t __jasmin_syscall_close__(uint64_t fd) +asm("__jasmin_syscall_close__"); + +uint8_t* __jasmin_syscall_write__(uint8_t* x, uint64_t xlen, uint64_t fd) +asm("__jasmin_syscall_write__"); + +uint8_t* __jasmin_syscall_read__(uint8_t* x, uint64_t xlen, uint64_t fd) +asm("__jasmin_syscall_read__"); + +#endif diff --git a/syscall/jasmin_syscall.o b/syscall/jasmin_syscall.o new file mode 100644 index 0000000000000000000000000000000000000000..ef6829ffb14b809bcc051dc9cb89b036cdb07736 GIT binary patch literal 3144 zcmb<-^>JfjWMqH=Mg}_u1P><4z;J>C!FB*M9T@l+co`f+Jvx6G9&ilt?EK=$IPv<+Z2Ezl* zhZ(^dJdzLlSUxCvxSxxGfdL}Ye1yZJ^F?&5V~k_$;WU{3P*=l~hW{ORFoW#d33B-^ z5OW^`3j+h(dXLrvC6=y+ClT@}7S8_|cMMuNi+92DIGh6kX62-{y@hMNyJ7;F*L^y5t+t3giO1tO3vi}2{Y z5fyz{HK)Rgi$T@ED1sq3HCG|Es3^Y(!BQ|{P)*@tNGmN#tzckqcXqZ?&~Wz))l@Jv z)-%#G00jalSQ!);7#LU?7#Q6BLK&DC7!QB~5G={Sz`zLQgX{;1iGT=@cvTPsV}$^t zG!Hw+1V#o169xtb8K_)2h<4%==w))}ljvdg=2K{6@#NEJW_95+VDjP9aN$#MBip&yBWksopDe>_PP^U4($EOw*<>klcSV zWaj5F6hQJhhLg&RGD}k9!8wD0ALK^{1_n^M4JttY{D%M~s5mH(gUVu4ry_}i(lAW@LL_ls1_lP0_yru|&!FNUdqKGerv5cl9Aqyak~v?I#6f<9 zss95N2blxPYcO$GRfuj5H#9wf#F5Q0LlPH6veyGi9F*5#=KDj%LH4pEsgH$rfnFv`GcbVbMwoh-QVytr51{%%X&=M{ z#U)6M1c+c@U;vfZAaPLMg9?GEcOXG93za|@)?r{^0GUayenU_l&A`9_%hxd5VD>}m z4VWmXo`FBg_2=M-{}&)fF)%PB zKsAEmpA#BaAR$=%gZu-v3M3522G9uo0aC!gz`(=6z<^CZNDQhJt{24602Pu93=DTb a93%`i4lV*JE3oPJfO@nVt`fpP*AD=w1@fx^ literal 0 HcmV?d00001