|
|
|
@ -234,15 +234,18 @@ static u8 its_fuzz(afl_state_t *afl, u8 *buf, u32 len, u8 *status) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static u8 cmp_extend_encoding(afl_state_t *afl, struct cmp_header *h, u64 pattern, u64 repl, u64 o_pattern, u32 idx, u8 *orig_buf, u8 *buf, u32 len, u8 do_reverse, u8 *status) {
|
|
|
|
|
static u8 cmp_extend_encoding(afl_state_t *afl, struct cmp_header *h,
|
|
|
|
|
u64 pattern, u64 repl, u64 o_pattern, u32 idx,
|
|
|
|
|
u8 *orig_buf, u8 *buf, u32 len, u8 do_reverse,
|
|
|
|
|
u8 *status) {
|
|
|
|
|
|
|
|
|
|
u64 *buf_64 = (u64 *)&buf[idx];
|
|
|
|
|
u32 *buf_32 = (u32 *)&buf[idx];
|
|
|
|
|
u16 *buf_16 = (u16 *)&buf[idx];
|
|
|
|
|
// u8* buf_8 = &buf[idx];
|
|
|
|
|
u64* o_buf_64 = (u64*)&orig_buf[idx];
|
|
|
|
|
u32* o_buf_32 = (u32*)&orig_buf[idx];
|
|
|
|
|
u16* o_buf_16 = (u16*)&orig_buf[idx];
|
|
|
|
|
u64 *o_buf_64 = (u64 *)&orig_buf[idx];
|
|
|
|
|
u32 *o_buf_32 = (u32 *)&orig_buf[idx];
|
|
|
|
|
u16 *o_buf_16 = (u16 *)&orig_buf[idx];
|
|
|
|
|
// u8* o_buf_8 = &orig_buf[idx];
|
|
|
|
|
|
|
|
|
|
u32 its_len = len - idx;
|
|
|
|
@ -260,15 +263,17 @@ static u8 cmp_extend_encoding(afl_state_t *afl, struct cmp_header *h, u64 patter
|
|
|
|
|
|
|
|
|
|
// reverse encoding
|
|
|
|
|
if (do_reverse)
|
|
|
|
|
if (unlikely(cmp_extend_encoding(afl, h, SWAP64(pattern), SWAP64(repl), SWAP64(o_pattern), idx, orig_buf, buf, len, 0, status)))
|
|
|
|
|
if (unlikely(cmp_extend_encoding(afl, h, SWAP64(pattern), SWAP64(repl),
|
|
|
|
|
SWAP64(o_pattern), idx, orig_buf, buf,
|
|
|
|
|
len, 0, status)))
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (SHAPE_BYTES(h->shape) == 4 || *status == 2) {
|
|
|
|
|
|
|
|
|
|
if (its_len >= 4 &&
|
|
|
|
|
*buf_32 == (u32)pattern && *o_buf_32 == (u32)o_pattern) {
|
|
|
|
|
if (its_len >= 4 && *buf_32 == (u32)pattern &&
|
|
|
|
|
*o_buf_32 == (u32)o_pattern) {
|
|
|
|
|
|
|
|
|
|
*buf_32 = (u32)repl;
|
|
|
|
|
if (unlikely(its_fuzz(afl, buf, len, status))) return 1;
|
|
|
|
@ -278,15 +283,17 @@ static u8 cmp_extend_encoding(afl_state_t *afl, struct cmp_header *h, u64 patter
|
|
|
|
|
|
|
|
|
|
// reverse encoding
|
|
|
|
|
if (do_reverse)
|
|
|
|
|
if (unlikely(cmp_extend_encoding(afl, h, SWAP32(pattern), SWAP32(repl), SWAP32(o_pattern), idx, orig_buf, buf, len, 0, status)))
|
|
|
|
|
if (unlikely(cmp_extend_encoding(afl, h, SWAP32(pattern), SWAP32(repl),
|
|
|
|
|
SWAP32(o_pattern), idx, orig_buf, buf,
|
|
|
|
|
len, 0, status)))
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (SHAPE_BYTES(h->shape) == 2 || *status == 2) {
|
|
|
|
|
|
|
|
|
|
if (its_len >= 2 &&
|
|
|
|
|
*buf_16 == (u16)pattern && *o_buf_16 == (u16)o_pattern) {
|
|
|
|
|
if (its_len >= 2 && *buf_16 == (u16)pattern &&
|
|
|
|
|
*o_buf_16 == (u16)o_pattern) {
|
|
|
|
|
|
|
|
|
|
*buf_16 = (u16)repl;
|
|
|
|
|
if (unlikely(its_fuzz(afl, buf, len, status))) return 1;
|
|
|
|
@ -296,7 +303,9 @@ static u8 cmp_extend_encoding(afl_state_t *afl, struct cmp_header *h, u64 patter
|
|
|
|
|
|
|
|
|
|
// reverse encoding
|
|
|
|
|
if (do_reverse)
|
|
|
|
|
if (unlikely(cmp_extend_encoding(afl, h, SWAP16(pattern), SWAP16(repl), SWAP16(o_pattern), idx, orig_buf, buf, len, 0, status)))
|
|
|
|
|
if (unlikely(cmp_extend_encoding(afl, h, SWAP16(pattern), SWAP16(repl),
|
|
|
|
|
SWAP16(o_pattern), idx, orig_buf, buf,
|
|
|
|
|
len, 0, status)))
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
@ -371,13 +380,13 @@ static u8 cmp_fuzz(afl_state_t *afl, u32 key, u8 *orig_buf, u8 *buf, u32 len) {
|
|
|
|
|
u8 status;
|
|
|
|
|
// opt not in the paper
|
|
|
|
|
u32 fails;
|
|
|
|
|
u8 found_one = 0;
|
|
|
|
|
u8 found_one = 0;
|
|
|
|
|
|
|
|
|
|
/* loop cmps are useless, detect and blacklist them */
|
|
|
|
|
u64 s_v0, s_v1;
|
|
|
|
|
u8 s_v0_fixed = 1, s_v1_fixed = 1;
|
|
|
|
|
u8 s_v0_inc = 1, s_v1_inc = 1;
|
|
|
|
|
u8 s_v0_dec = 1, s_v1_dec = 1;
|
|
|
|
|
u8 s_v0_fixed = 1, s_v1_fixed = 1;
|
|
|
|
|
u8 s_v0_inc = 1, s_v1_inc = 1;
|
|
|
|
|
u8 s_v0_dec = 1, s_v1_dec = 1;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < loggeds; ++i) {
|
|
|
|
|
|
|
|
|
@ -387,17 +396,21 @@ static u8 cmp_fuzz(afl_state_t *afl, u32 key, u8 *orig_buf, u8 *buf, u32 len) {
|
|
|
|
|
|
|
|
|
|
// loop detection code
|
|
|
|
|
if (i == 0) {
|
|
|
|
|
|
|
|
|
|
s_v0 = o->v0;
|
|
|
|
|
s_v1 = o->v1;
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
if (s_v0 != o->v0) s_v0_fixed = 0;
|
|
|
|
|
if (s_v1 != o->v1) s_v1_fixed = 0;
|
|
|
|
|
if (s_v0 +1 != o->v0) s_v0_inc = 0;
|
|
|
|
|
if (s_v1 +1 != o->v1) s_v1_inc = 0;
|
|
|
|
|
if (s_v0 -1 != o->v0) s_v0_dec = 0;
|
|
|
|
|
if (s_v1 -1 != o->v1) s_v1_dec = 0;
|
|
|
|
|
if (s_v0 + 1 != o->v0) s_v0_inc = 0;
|
|
|
|
|
if (s_v1 + 1 != o->v1) s_v1_inc = 0;
|
|
|
|
|
if (s_v0 - 1 != o->v0) s_v0_dec = 0;
|
|
|
|
|
if (s_v1 - 1 != o->v1) s_v1_dec = 0;
|
|
|
|
|
s_v0 = o->v0;
|
|
|
|
|
s_v1 = o->v1;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct cmp_operands *orig_o = &afl->orig_cmp_map->log[key][i];
|
|
|
|
@ -410,14 +423,16 @@ static u8 cmp_fuzz(afl_state_t *afl, u32 key, u8 *orig_buf, u8 *buf, u32 len) {
|
|
|
|
|
|
|
|
|
|
for (idx = 0; idx < len && fails < 8; ++idx) {
|
|
|
|
|
|
|
|
|
|
if (unlikely(cmp_extend_encoding(afl, h, o->v0, o->v1, orig_o->v0, idx, orig_buf, buf, len, 1, &status)))
|
|
|
|
|
if (unlikely(cmp_extend_encoding(afl, h, o->v0, o->v1, orig_o->v0, idx,
|
|
|
|
|
orig_buf, buf, len, 1, &status)))
|
|
|
|
|
return 1;
|
|
|
|
|
if (status == 2)
|
|
|
|
|
++fails;
|
|
|
|
|
else if (status == 1)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
if (unlikely(cmp_extend_encoding(afl, h, o->v1, o->v0, orig_o->v1, idx, orig_buf, buf, len, 1, &status)))
|
|
|
|
|
if (unlikely(cmp_extend_encoding(afl, h, o->v1, o->v0, orig_o->v1, idx,
|
|
|
|
|
orig_buf, buf, len, 1, &status)))
|
|
|
|
|
return 1;
|
|
|
|
|
if (status == 2)
|
|
|
|
|
++fails;
|
|
|
|
@ -426,15 +441,16 @@ static u8 cmp_fuzz(afl_state_t *afl, u32 key, u8 *orig_buf, u8 *buf, u32 len) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (status == 1)
|
|
|
|
|
found_one = 1;
|
|
|
|
|
if (status == 1) found_one = 1;
|
|
|
|
|
|
|
|
|
|
// If failed, add to dictionary
|
|
|
|
|
if (fails == 8) {
|
|
|
|
|
|
|
|
|
|
if (afl->pass_stats[key].total == 0) {
|
|
|
|
|
|
|
|
|
|
try_to_add_to_dict(afl, o->v0, SHAPE_BYTES(h->shape));
|
|
|
|
|
try_to_add_to_dict(afl, o->v1, SHAPE_BYTES(h->shape));
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
@ -446,19 +462,26 @@ static u8 cmp_fuzz(afl_state_t *afl, u32 key, u8 *orig_buf, u8 *buf, u32 len) {
|
|
|
|
|
|
|
|
|
|
if (loggeds > 3 && ((s_v0_fixed && s_v1_inc) || (s_v1_fixed && s_v0_inc) ||
|
|
|
|
|
(s_v0_fixed && s_v1_dec) || (s_v1_fixed && s_v0_dec))) {
|
|
|
|
|
|
|
|
|
|
afl->pass_stats[key].total = afl->pass_stats[key].faileds = 0xff;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!found_one && afl->pass_stats[key].faileds < 0xff) {
|
|
|
|
|
|
|
|
|
|
afl->pass_stats[key].faileds++;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (afl->pass_stats[key].total < 0xff) afl->pass_stats[key].total++;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static u8 rtn_extend_encoding(afl_state_t *afl, struct cmp_header *h, u8 *pattern, u8 *repl, u8* o_pattern, u32 idx, u8 *orig_buf, u8 *buf, u32 len, u8 *status) {
|
|
|
|
|
static u8 rtn_extend_encoding(afl_state_t *afl, struct cmp_header *h,
|
|
|
|
|
u8 *pattern, u8 *repl, u8 *o_pattern, u32 idx,
|
|
|
|
|
u8 *orig_buf, u8 *buf, u32 len, u8 *status) {
|
|
|
|
|
|
|
|
|
|
u32 i;
|
|
|
|
|
u32 its_len = MIN(32, len - idx);
|
|
|
|
@ -470,7 +493,9 @@ static u8 rtn_extend_encoding(afl_state_t *afl, struct cmp_header *h, u8 *patter
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < its_len; ++i) {
|
|
|
|
|
|
|
|
|
|
if (pattern[idx + i] != buf[idx + i] || o_pattern[idx + i] != orig_buf[idx + i] || *status == 1) break;
|
|
|
|
|
if (pattern[idx + i] != buf[idx + i] ||
|
|
|
|
|
o_pattern[idx + i] != orig_buf[idx + i] || *status == 1)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
buf[idx + i] = repl[idx + i];
|
|
|
|
|
if (unlikely(its_fuzz(afl, buf, len, status))) return 1;
|
|
|
|
@ -493,7 +518,7 @@ static u8 rtn_fuzz(afl_state_t *afl, u32 key, u8 *orig_buf, u8 *buf, u32 len) {
|
|
|
|
|
u8 status = 0;
|
|
|
|
|
// opt not in the paper
|
|
|
|
|
u32 fails = 0;
|
|
|
|
|
u8 found_one = 0;
|
|
|
|
|
u8 found_one = 0;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < loggeds; ++i) {
|
|
|
|
|
|
|
|
|
@ -513,14 +538,16 @@ static u8 rtn_fuzz(afl_state_t *afl, u32 key, u8 *orig_buf, u8 *buf, u32 len) {
|
|
|
|
|
|
|
|
|
|
for (idx = 0; idx < len && fails < 8; ++idx) {
|
|
|
|
|
|
|
|
|
|
if (unlikely(rtn_extend_encoding(afl, h, o->v0, o->v1, orig_o->v0, idx, orig_buf, buf, len, &status)))
|
|
|
|
|
if (unlikely(rtn_extend_encoding(afl, h, o->v0, o->v1, orig_o->v0, idx,
|
|
|
|
|
orig_buf, buf, len, &status)))
|
|
|
|
|
return 1;
|
|
|
|
|
if (status == 2)
|
|
|
|
|
++fails;
|
|
|
|
|
else if (status == 1)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
if (unlikely(rtn_extend_encoding(afl, h, o->v1, o->v0, orig_o->v1, idx, orig_buf, buf, len, &status)))
|
|
|
|
|
if (unlikely(rtn_extend_encoding(afl, h, o->v1, o->v0, orig_o->v1, idx,
|
|
|
|
|
orig_buf, buf, len, &status)))
|
|
|
|
|
return 1;
|
|
|
|
|
if (status == 2)
|
|
|
|
|
++fails;
|
|
|
|
@ -529,15 +556,16 @@ static u8 rtn_fuzz(afl_state_t *afl, u32 key, u8 *orig_buf, u8 *buf, u32 len) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (status == 1)
|
|
|
|
|
found_one = 1;
|
|
|
|
|
if (status == 1) found_one = 1;
|
|
|
|
|
|
|
|
|
|
// If failed, add to dictionary
|
|
|
|
|
if (fails == 8) {
|
|
|
|
|
|
|
|
|
|
if (afl->pass_stats[key].total == 0) {
|
|
|
|
|
|
|
|
|
|
maybe_add_auto((u8 *)afl, o->v0, SHAPE_BYTES(h->shape));
|
|
|
|
|
maybe_add_auto((u8 *)afl, o->v1, SHAPE_BYTES(h->shape));
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
@ -548,8 +576,11 @@ static u8 rtn_fuzz(afl_state_t *afl, u32 key, u8 *orig_buf, u8 *buf, u32 len) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!found_one && afl->pass_stats[key].faileds < 0xff) {
|
|
|
|
|
|
|
|
|
|
afl->pass_stats[key].faileds++;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (afl->pass_stats[key].total < 0xff) afl->pass_stats[key].total++;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
@ -598,9 +629,10 @@ u8 input_to_state_stage(afl_state_t *afl, u8 *orig_buf, u8 *buf, u32 len,
|
|
|
|
|
if (!afl->shm.cmp_map->headers[k].hits) continue;
|
|
|
|
|
|
|
|
|
|
if (afl->pass_stats[k].total &&
|
|
|
|
|
(rand_below(afl, afl->pass_stats[k].total) >= afl->pass_stats[k].faileds
|
|
|
|
|
|| afl->pass_stats[k].total == 0xff))
|
|
|
|
|
afl->shm.cmp_map->headers[k].hits = 0; // blacklist this cmp
|
|
|
|
|
(rand_below(afl, afl->pass_stats[k].total) >=
|
|
|
|
|
afl->pass_stats[k].faileds ||
|
|
|
|
|
afl->pass_stats[k].total == 0xff))
|
|
|
|
|
afl->shm.cmp_map->headers[k].hits = 0; // blacklist this cmp
|
|
|
|
|
|
|
|
|
|
if (afl->shm.cmp_map->headers[k].type == CMP_TYPE_INS)
|
|
|
|
|
afl->stage_max += MIN((u32)afl->shm.cmp_map->headers[k].hits, CMP_MAP_H);
|
|
|
|
@ -616,13 +648,11 @@ u8 input_to_state_stage(afl_state_t *afl, u8 *orig_buf, u8 *buf, u32 len,
|
|
|
|
|
|
|
|
|
|
if (afl->shm.cmp_map->headers[k].type == CMP_TYPE_INS) {
|
|
|
|
|
|
|
|
|
|
if (unlikely(cmp_fuzz(afl, k, orig_buf, buf, len)))
|
|
|
|
|
goto exit_its;
|
|
|
|
|
if (unlikely(cmp_fuzz(afl, k, orig_buf, buf, len))) goto exit_its;
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
if (unlikely(rtn_fuzz(afl, k, orig_buf, buf, len)))
|
|
|
|
|
goto exit_its;
|
|
|
|
|
if (unlikely(rtn_fuzz(afl, k, orig_buf, buf, len))) goto exit_its;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|