63
63
#include <sys/stat.h>
64
64
#include <sys/types.h>
65
65
#include <sys/resource.h>
66
+ #ifdef __linux__
67
+ #include <poll.h>
68
+ #include <unistd.h>
69
+ #endif
66
70
67
71
static afl_state_t * afl ;
68
72
@@ -537,10 +541,58 @@ static u32 read_file(u8 *in_file) {
537
541
}
538
542
539
543
#ifdef __linux__
540
- /* Execute the target application with an empty input (in Nyx mode). */
544
+ #define NYX_STDIN_TMP_BUFFER_SIZE 256
545
+
546
+ size_t read_stdin_input (u8 * * out ) {
547
+ size_t len = 0 ;
548
+ u8 * input = NULL ;
549
+
550
+ if (!isatty (STDIN_FILENO )) { /* only process input when passed via pipe */
551
+
552
+ struct pollfd pfd = { .fd = STDIN_FILENO , .events = POLLIN };
553
+ int ready = poll (& pfd , 1 , 0 );
554
+
555
+ if (ready == -1 ) {
556
+ FATAL ("%s: poll failed\n" , __func__ );
557
+ }
558
+ else if (pfd .revents & POLLIN ) { // check if there is any data on stdin
559
+
560
+ u8 tmp [NYX_STDIN_TMP_BUFFER_SIZE ];
561
+ size_t capacity = NYX_STDIN_TMP_BUFFER_SIZE ;
562
+ size_t bytes_read ;
563
+ input = malloc (capacity );
564
+
565
+ /* copy input from stdin into a buffer to pass it to Nyx */
566
+ while ((bytes_read = read (STDIN_FILENO , tmp , NYX_STDIN_TMP_BUFFER_SIZE )) > 0 ) {
567
+ /* resize buffer if needed */
568
+ if (len + bytes_read > capacity ) {
569
+ while (len + bytes_read > capacity )
570
+ capacity *= 2 ;
571
+
572
+ u8 * new_input = realloc (input , capacity );
573
+ if (!new_input ) {
574
+ free (input );
575
+ FATAL ("%s: realloc failed\n" , __func__ );
576
+ }
577
+ input = new_input ;
578
+ }
579
+
580
+ memcpy (input + len , tmp , bytes_read );
581
+ len += bytes_read ;
582
+ }
583
+ * out = input ;
584
+ }
585
+ }
586
+ return len ;
587
+ }
588
+
589
+ /* Execute the target application in Nyx mode with piped input from stdin (passed as a buffer). */
541
590
static void showmap_run_target_nyx_mode (afl_forkserver_t * fsrv ) {
542
591
543
- afl_fsrv_write_to_testcase (fsrv , NULL , 0 );
592
+ u8 * input = NULL ;
593
+ size_t len = read_stdin_input (& input );
594
+
595
+ afl_fsrv_write_to_testcase (fsrv , input , len );
544
596
545
597
if (afl_fsrv_run_target (fsrv , fsrv -> exec_tmout , & stop_soon ) ==
546
598
FSRV_RUN_ERROR ) {
@@ -549,6 +601,9 @@ static void showmap_run_target_nyx_mode(afl_forkserver_t *fsrv) {
549
601
550
602
}
551
603
604
+ if (input != NULL ) {
605
+ free (input );
606
+ }
552
607
}
553
608
554
609
#endif
0 commit comments