Skip to content

Commit abf1f8b

Browse files
authored
fix: stack smashing on large string IDs (aflnet#19)
1 parent c8b7e44 commit abf1f8b

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

afl-fuzz.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -782,9 +782,9 @@ void update_state_aware_variables(struct queue_entry *q, u8 dry_run)
782782

783783
for(i=1; i < state_count; i++) {
784784
unsigned int curStateID = state_sequence[i];
785-
char fromState[10], toState[10];
786-
sprintf(fromState, "%d", prevStateID);
787-
sprintf(toState, "%d", curStateID);
785+
char fromState[STATE_STR_LEN], toState[STATE_STR_LEN];
786+
snprintf(fromState, STATE_STR_LEN, "%d", prevStateID);
787+
snprintf(toState, STATE_STR_LEN, "%d", curStateID);
788788

789789
//Check if the prevStateID and curStateID have been added to the state machine as vertices
790790
//Check also if the edge prevStateID->curStateID has been added

aflnet.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -1413,29 +1413,29 @@ u8* state_sequence_to_string(unsigned int *stateSequence, unsigned int stateCoun
14131413

14141414
u8 *out = NULL;
14151415

1416-
char strState[10];
1417-
int len = 0;
1416+
char strState[STATE_STR_LEN];
1417+
size_t len = 0;
14181418
for (i = 0; i < stateCount; i++) {
14191419
//Limit the loop to shorten the output string
14201420
if ((i >= 2) && (stateSequence[i] == stateSequence[i - 1]) && (stateSequence[i] == stateSequence[i - 2])) continue;
14211421
unsigned int stateID = stateSequence[i];
14221422
if (i == stateCount - 1) {
1423-
sprintf(strState, "%d", (int) stateID);
1423+
snprintf(strState, STATE_STR_LEN, "%d", (int) stateID);
14241424
} else {
1425-
sprintf(strState, "%d-", (int) stateID);
1425+
snprintf(strState, STATE_STR_LEN, "%d-", (int) stateID);
14261426
}
14271427
out = (u8 *)ck_realloc(out, len + strlen(strState) + 1);
14281428
memcpy(&out[len], strState, strlen(strState) + 1);
14291429
len=strlen(out);
14301430
//As Linux limit the size of the file name
14311431
//we set a fixed upper bound here
14321432
if (len > 150 && (i + 1 < stateCount)) {
1433-
sprintf(strState, "%s", "end-at-");
1433+
snprintf(strState, STATE_STR_LEN, "%s", "end-at-");
14341434
out = (u8 *)ck_realloc(out, len + strlen(strState) + 1);
14351435
memcpy(&out[len], strState, strlen(strState) + 1);
14361436
len=strlen(out);
14371437

1438-
sprintf(strState, "%d", (int) stateSequence[stateCount - 1]);
1438+
snprintf(strState, STATE_STR_LEN, "%d", (int) stateSequence[stateCount - 1]);
14391439
out = (u8 *)ck_realloc(out, len + strlen(strState) + 1);
14401440
memcpy(&out[len], strState, strlen(strState) + 1);
14411441
len=strlen(out);

config.h

+2
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,8 @@
328328
#define MAP_SIZE_POW2 16
329329
#define MAP_SIZE (1 << MAP_SIZE_POW2)
330330

331+
#define STATE_STR_LEN 12
332+
331333
/* Maximum allocator request size (keep well under INT_MAX): */
332334

333335
#define MAX_ALLOC 0x40000000

0 commit comments

Comments
 (0)