163 lines
3.2 KiB
C
163 lines
3.2 KiB
C
|
|
#include "jasmin_syscall.h"
|
|
|
|
#if defined(__linux__)
|
|
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <linux/futex.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/mman.h>
|
|
#include <sys/random.h>
|
|
#include <sys/syscall.h>
|
|
#include <unistd.h>
|
|
|
|
// 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;
|
|
// }
|
|
//
|
|
uint8_t* __jasmin_syscall_randombytes__(uint8_t* _x, uint64_t xlen)
|
|
{
|
|
int i;
|
|
uint8_t* x = _x;
|
|
|
|
while (xlen > 0) {
|
|
if (xlen < 1048576) i = xlen; else i = 1048576;
|
|
|
|
i = getrandom(x,i,0);
|
|
if (i < 1) {
|
|
sleep(1);
|
|
continue;
|
|
}
|
|
x += i;
|
|
xlen -= i;
|
|
}
|
|
|
|
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;
|
|
long ret = syscall(SYS_futex, addr, FUTEX_WAIT, 0, NULL, NULL, 0);
|
|
if (ret == -1) {
|
|
printf("Agent futex error: %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;
|
|
int woken_up = 0;
|
|
while (woken_up == 0) {
|
|
woken_up = 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 <stdlib.h>
|
|
|
|
#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
|