From 9081f4001f1ba9354d320c63b4b230f22817904d Mon Sep 17 00:00:00 2001 From: David Carlier Date: Tue, 30 Jul 2019 07:25:56 +0000 Subject: [PATCH] For BSD/APPLE platform using native random calls. Solution not involving file descriptors, seeded upon fork and in a regular basis. --- afl-fuzz.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/afl-fuzz.c b/afl-fuzz.c index 962bbf0b9..da8c2bd03 100644 --- a/afl-fuzz.c +++ b/afl-fuzz.c @@ -68,6 +68,7 @@ #if defined(__APPLE__) || defined(__FreeBSD__) || defined (__OpenBSD__) # include +# define HAVE_ARC4RANDOM 1 #endif /* __APPLE__ || __FreeBSD__ || __OpenBSD__ */ /* For systems that have sched_setaffinity; right now just Linux, but one @@ -135,7 +136,9 @@ EXP_ST u8 skip_deterministic, /* Skip deterministic stages? */ fast_cal; /* Try to calibrate faster? */ static s32 out_fd, /* Persistent fd for out_file */ +#ifndef HAVE_ARC4RANDOM dev_urandom_fd = -1, /* Persistent fd for /dev/urandom */ +#endif dev_null_fd = -1, /* Persistent fd for /dev/null */ fsrv_ctl_fd, /* Fork server control pipe (write) */ fsrv_st_fd; /* Fork server status pipe (read) */ @@ -215,7 +218,9 @@ static u8 stage_val_type; /* Value type (STAGE_VAL_*) */ static u64 stage_finds[32], /* Patterns found per fuzz stage */ stage_cycles[32]; /* Execs per fuzz stage */ +#ifndef HAVE_ARC4RANDOM static u32 rand_cnt; /* Random number counter */ +#endif static u64 total_cal_us, /* Total calibration time (us) */ total_cal_cycles; /* Total calibration cycles */ @@ -364,7 +369,7 @@ static u64 get_cur_time_us(void) { have slight bias. */ static inline u32 UR(u32 limit) { - +#ifndef HAVE_ARC4RANDOM if (unlikely(!rand_cnt--)) { u32 seed[2]; @@ -377,6 +382,11 @@ static inline u32 UR(u32 limit) { } return random() % limit; +#else + /* The boundary not being necessarily a power of 2, + we need to ensure the result uniformity. */ + return arc4random_uniform(limit); +#endif } @@ -2071,7 +2081,9 @@ EXP_ST void init_forkserver(char** argv) { close(out_dir_fd); close(dev_null_fd); +#ifndef HAVE_ARC4RANDOM close(dev_urandom_fd); +#endif close(fileno(plot_file)); /* This should improve performance a bit, since it stops the linker from @@ -2343,7 +2355,9 @@ static u8 run_target(char** argv, u32 timeout) { close(dev_null_fd); close(out_dir_fd); +#ifndef HAVE_ARC4RANDOM close(dev_urandom_fd); +#endif close(fileno(plot_file)); /* Set sane defaults for ASAN if nothing else specified. */ @@ -7189,8 +7203,10 @@ EXP_ST void setup_dirs_fds(void) { dev_null_fd = open("/dev/null", O_RDWR); if (dev_null_fd < 0) PFATAL("Unable to open /dev/null"); +#ifndef HAVE_ARC4RANDOM dev_urandom_fd = open("/dev/urandom", O_RDONLY); if (dev_urandom_fd < 0) PFATAL("Unable to open /dev/urandom"); +#endif /* Gnuplot output file. */