Skip to content

Commit ee14785

Browse files
committed
starting shmap support for unicorn
1 parent c0ed118 commit ee14785

File tree

9 files changed

+56
-36
lines changed

9 files changed

+56
-36
lines changed

include/afl-fuzz.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,8 @@ typedef struct afl_state {
444444
deferred_mode, /* Deferred forkserver mode? */
445445
fixed_seed, /* do not reseed */
446446
fast_cal, /* Try to calibrate faster? */
447-
disable_trim; /* Never trim in fuzz_one */
447+
disable_trim, /* Never trim in fuzz_one */
448+
shmem_testcase_mode; /* If sharedmem testcases are used */
448449

449450
u8 *virgin_bits, /* Regions yet untouched by fuzzing */
450451
*virgin_tmout, /* Bits we haven't seen in tmouts */
@@ -806,6 +807,9 @@ void afl_states_clear_screen(void);
806807
/* Sets the skip flag on all states */
807808
void afl_states_request_skip(void);
808809

810+
/* Setup shmem for testcase delivery */
811+
void setup_testcase_shmem(afl_state_t *afl);
812+
809813
void read_afl_environment(afl_state_t *, char **);
810814

811815
/**** Prototypes ****/

include/forkserver.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ typedef struct afl_forkserver {
8181

8282
u8 qemu_mode; /* if running in qemu mode or not */
8383

84-
u32 shdmem_fuzz_len; /* length of the fuzzing test case */
84+
u32 shmem_fuzz_len; /* length of the fuzzing test case */
8585

86-
u8 *shdmem_fuzz; /* allocated memory for fuzzing */
86+
u8 *shmem_fuzz; /* allocated memory for fuzzing */
8787

8888
char *cmplog_binary; /* the name of the cmplog binary */
8989

src/afl-forkserver.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ static void report_error_and_exit(int error) {
286286

287287
}
288288

289-
/* Spins up fork server (instrumented mode only). The idea is explained here:
289+
/* Spins up fork server. The idea is explained here:
290290
291291
http://lcamtuf.blogspot.com/2014/10/fuzzing-binaries-without-execve.html
292292
@@ -305,7 +305,7 @@ void afl_fsrv_start(afl_forkserver_t *fsrv, char **argv,
305305

306306
if (fsrv->use_fauxsrv) {
307307

308-
/* TODO: Come up with sone nice way to initalize this all */
308+
/* TODO: Come up with sone nice way to initialize this all */
309309

310310
if (fsrv->init_child_func != fsrv_exec_child) {
311311

@@ -823,10 +823,10 @@ static void afl_fsrv_kill(afl_forkserver_t *fsrv) {
823823

824824
void afl_fsrv_write_to_testcase(afl_forkserver_t *fsrv, u8 *buf, size_t len) {
825825

826-
if (fsrv->shdmem_fuzz) {
826+
if (fsrv->shmem_fuzz) {
827827

828-
memcpy(fsrv->shdmem_fuzz, buf, len);
829-
fsrv->shdmem_fuzz_len = len;
828+
memcpy(fsrv->shmem_fuzz, buf, len);
829+
fsrv->shmem_fuzz_len = len;
830830

831831
} else {
832832

@@ -888,7 +888,7 @@ fsrv_run_result_t afl_fsrv_run_target(afl_forkserver_t *fsrv, u32 timeout,
888888

889889
MEM_BARRIER();
890890

891-
if (fsrv->shdmem_fuzz_len) write_value += (fsrv->shdmem_fuzz_len << 8);
891+
if (fsrv->shmem_fuzz_len) write_value += (fsrv->shmem_fuzz_len << 8);
892892

893893
/* we have the fork server (or faux server) up and running
894894
First, tell it if the previous run timed out. */

src/afl-fuzz-init.c

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1949,6 +1949,36 @@ static void handle_skipreq(int sig) {
19491949

19501950
}
19511951

1952+
1953+
/* Setup shared map for fuzzing with input via sharedmem */
1954+
1955+
void setup_testcase_shmem(afl_state_t *afl) {
1956+
1957+
afl->shm_fuzz = ck_alloc(sizeof(sharedmem_t));
1958+
1959+
// we need to set the dumb mode to not overwrite the SHM_ENV_VAR
1960+
if ((afl->fsrv.shmem_fuzz = afl_shm_init(afl->shm_fuzz, MAX_FILE, 1))) {
1961+
1962+
#ifdef USEMMAP
1963+
setenv(SHM_FUZZ_ENV_VAR, afl->shm_fuzz->g_shm_file_path, 1);
1964+
#else
1965+
u8 *shm_str;
1966+
shm_str = alloc_printf("%d", afl->shm_fuzz->shm_id);
1967+
setenv(SHM_FUZZ_ENV_VAR, shm_str, 1);
1968+
ck_free(shm_str);
1969+
#endif
1970+
afl->fsrv.support_shdmen_fuzz = 1;
1971+
1972+
} else {
1973+
1974+
ck_free(afl->shm_fuzz);
1975+
afl->shm_fuzz = NULL;
1976+
1977+
}
1978+
1979+
}
1980+
1981+
19521982
/* Do a PATH search and find target binary to see that it exists and
19531983
isn't a shell script - a common and painful mistake. We also check for
19541984
a valid ELF header and for evidence of AFL instrumentation. */
@@ -2153,30 +2183,8 @@ void check_binary(afl_state_t *afl, u8 *fname) {
21532183
OKF(cPIN "Persistent mode binary detected.");
21542184
setenv(PERSIST_ENV_VAR, "1", 1);
21552185
afl->persistent_mode = 1;
2156-
// do not fail if we can not get the fuzzing shared mem
2157-
if ((afl->shm_fuzz = calloc(1, sizeof(sharedmem_t)))) {
2158-
2159-
// we need to set the dumb mode to not overwrite the SHM_ENV_VAR
2160-
if ((afl->fsrv.shdmem_fuzz = afl_shm_init(afl->shm_fuzz, MAX_FILE, 1))) {
2161-
2162-
#ifdef USEMMAP
2163-
setenv(SHM_FUZZ_ENV_VAR, afl->shm_fuzz->g_shm_file_path, 1);
2164-
#else
2165-
u8 *shm_str;
2166-
shm_str = alloc_printf("%d", afl->shm_fuzz->shm_id);
2167-
setenv(SHM_FUZZ_ENV_VAR, shm_str, 1);
2168-
ck_free(shm_str);
2169-
#endif
2170-
afl->fsrv.support_shdmen_fuzz = 1;
2171-
2172-
} else {
2173-
2174-
free(afl->shm_fuzz);
2175-
afl->shm_fuzz = NULL;
21762186

2177-
}
2178-
2179-
}
2187+
afl->shmem_testcase_mode = 1;
21802188

21812189
} else if (getenv("AFL_PERSISTENT")) {
21822190

src/afl-fuzz-run.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ u8 calibrate_case(afl_state_t *afl, struct queue_entry *q, u8 *use_mem,
237237
free(afl->shm_fuzz);
238238
afl->shm_fuzz = NULL;
239239
afl->fsrv.support_shdmen_fuzz = 0;
240-
afl->fsrv.shdmem_fuzz = NULL;
240+
afl->fsrv.shmem_fuzz = NULL;
241241

242242
}
243243

src/afl-fuzz-stats.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ void write_stats_file(afl_state_t *afl, double bitmap_cvg, double stability,
103103
"afl_banner : %s\n"
104104
"afl_version : " VERSION
105105
"\n"
106-
"target_mode : %s%s%s%s%s%s%s%s\n"
106+
"target_mode : %s%s%s%s%s%s%s%s%s\n"
107107
"command_line : %s\n",
108108
afl->start_time / 1000, cur_time / 1000,
109109
(cur_time - afl->start_time) / 1000, (u32)getpid(),
@@ -128,6 +128,7 @@ void write_stats_file(afl_state_t *afl, double bitmap_cvg, double stability,
128128
afl->dumb_mode ? " dumb " : "", afl->no_forkserver ? "no_fsrv " : "",
129129
afl->crash_mode ? "crash " : "",
130130
afl->persistent_mode ? "persistent " : "",
131+
afl->shmem_testcase_mode ? "shmem_testcase " : "",
131132
afl->deferred_mode ? "deferred " : "",
132133
(afl->unicorn_mode || afl->fsrv.qemu_mode || afl->dumb_mode ||
133134
afl->no_forkserver || afl->crash_mode || afl->persistent_mode ||

src/afl-fuzz.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,7 @@ int main(int argc, char **argv_orig, char **envp) {
572572

573573
if (afl->unicorn_mode) { FATAL("Multiple -U options not supported"); }
574574
afl->unicorn_mode = 1;
575+
afl->shmem_testcase_mode = 1;
575576

576577
if (!mem_limit_given) { afl->fsrv.mem_limit = MEM_LIMIT_UNICORN; }
577578

@@ -1178,6 +1179,12 @@ int main(int argc, char **argv_orig, char **envp) {
11781179

11791180
check_binary(afl, argv[optind]);
11801181

1182+
if (afl->shmem_testcase_mode) {
1183+
1184+
setup_testcase_shmem(afl);
1185+
1186+
}
1187+
11811188
afl->start_time = get_cur_time();
11821189

11831190
if (afl->fsrv.qemu_mode) {

unicorn_mode/UNICORNAFL_VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
212110c
1+
37067ac

unicorn_mode/unicornafl

Submodule unicornafl updated 1 file

0 commit comments

Comments
 (0)