I am trying out my hand at libfuzzer and I am facing 'heap overflow' at malloc. The code snippet is as follows
int LLVMFuzzerTestOneInput(
const unsigned char * Data,
size_t Size
) {
initialize_state_before_fuzzing();
size_t charOffset = 0;
size_t testValueSize = 0;
size_t arrayLength = 0;
size_t arrayLength2 = 0;
const size_t FIXED_BUFFER_SIZE = 4096;
if (Size == 0)
return 0;
uint8_t *testValue_1 = malloc(Size);
testValueSize = Size;
for (size_t i = 0; i < testValueSize && charOffset < Size; i++) {
testValue_1[i] = (uint8_t) Data[charOffset];
charOffset++;
}
The overflow happens when Data="" and Size = 7. My question is why does libfuzzer give data that is not equal to the size? How to avoid this?
Also, even if Data is NULL, why does malloc cause heap overflow?
Based on your description, I think the key point where the error occurred is that you did not check if
Datais NULL when executing functionLLVMFuzzerTestOneInputand without guaranteesizeof(Data) / sizeof(Data[0]) >= size.When you check if
Datais NULL at execute functionLLVMFuzzerTestOneInputbefore, and the value of size is suitable, at here such asSize = 7, the problem may be disappear.You can call function
LLVMFuzzerTestOneInput(Data, Size)only whenDatais not equal to NULL andsizeof(Data) / sizeof(Data[0]) >= size.like this:
And check if
testValue_1is NULL that return value bymallocinLLVMFuzzerTestOneInputfunction.Like this:
If possible, please provide additional information, such as code snippets and error messages