-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
109 lines (95 loc) · 2.65 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
cpufuzz - https://github.com/a0rtega/cpufuzz
MIT License
Copyright (c) 2019 Alberto Ortega
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <signal.h>
#include <time.h>
#include <sys/socket.h>
#include <arpa/inet.h>
/* Needed if we use SYS_getrandom syscall
#include <sys/syscall.h>
#include <linux/random.h>
*/
#define TESTCASES_SIZE 16
#define CHILD_MAXTIME 5 /* seconds */
static inline void send_testcase(unsigned char * test_buffer, int sock, char * hex) {
unsigned int i;
for (i = 0; i < TESTCASES_SIZE; i++) {
sprintf(hex+(i*4), "\\x%02X", test_buffer[i]);
}
send(sock, hex, TESTCASES_SIZE * 4, 0);
send(sock, "\r\n", 2, 0);
}
int main(int argc, char * argv[]) {
void * test_buffer;
char * test_hex = malloc(TESTCASES_SIZE * 4);
pid_t pid;
int pid_status;
int t_seconds;
char * output_server;
long int output_server_port = 0;
struct timespec tim;
FILE * dev_urandom;
size_t read_f;
tim.tv_sec = 0;
tim.tv_nsec = 10000000L;
struct sockaddr_in server;
int sock;
if ((argc == 3) && (output_server_port = strtol(argv[2], NULL, 10))) {
output_server = argv[1];
}
else {
fprintf(stdout, "%s <output_ip_addr> <port>\n", argv[0]);
return 1;
}
/* Connect output server socket */
sock = socket(AF_INET , SOCK_STREAM , 0);
server.sin_addr.s_addr = inet_addr(output_server);
server.sin_family = AF_INET;
server.sin_port = htons(output_server_port);
if (connect(sock, (struct sockaddr *)&server, sizeof(server)) < 0) {
fprintf(stderr, "Connect to output server failed, abort!\n");
return 1;
}
dev_urandom = fopen("/dev/urandom", "rb");
fprintf(stderr, "Mapping memory for testcases ... ");
test_buffer = mmap(NULL, TESTCASES_SIZE, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANONYMOUS|MAP_SHARED, -1, 0);
fprintf(stderr, "Done! Mapped at %p\n", test_buffer);
for (;;) {
/* Original method to get random data (SYS_getrandom),
replaced by /dev/urandom for portability.
syscall(SYS_getrandom, test_buffer, TESTCASES_SIZE, NULL);
*/
read_f = fread(test_buffer, TESTCASES_SIZE, 1, dev_urandom);
if (!read_f)
continue;
/* Send buffer to output server */
send_testcase((unsigned char *)test_buffer, sock, test_hex);
/* Fork process */
pid = fork();
if (pid == 0) {
/* Execute random data buffer */
((void(*)())test_buffer)();
}
else {
/* Monitor child process */
t_seconds = (int)time(NULL);
while (!waitpid(pid, &pid_status, WNOHANG)) {
if ((int)time(NULL) >= (t_seconds + CHILD_MAXTIME)) {
printf("Killing %i ...\n", pid);
kill(pid, SIGKILL);
}
nanosleep(&tim , NULL);
}
}
}
return 0;
}