Skip to content

Commit 88448cc

Browse files
embghswong3i
authored andcommitted
Exercise ZSTD_findDecompressedSize() in the simple decompression fuzzer (facebook#3959)
* Improve decompression fuzzer * Fix legacy frame header fuzzer crash, add unit test
1 parent c4e0656 commit 88448cc

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

lib/decompress/zstd_decompress.c

+9
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,15 @@ size_t ZSTD_decompressMultiFrame(ZSTD_DCtx* dctx,
10931093
decodedSize = ZSTD_decompressLegacy(dst, dstCapacity, src, frameSize, dict, dictSize);
10941094
if (ZSTD_isError(decodedSize)) return decodedSize;
10951095

1096+
{
1097+
unsigned long long const expectedSize = ZSTD_getFrameContentSize(src, srcSize);
1098+
RETURN_ERROR_IF(expectedSize == ZSTD_CONTENTSIZE_ERROR, corruption_detected, "Corrupted frame header!");
1099+
if (expectedSize != ZSTD_CONTENTSIZE_UNKNOWN) {
1100+
RETURN_ERROR_IF(expectedSize != decodedSize, corruption_detected,
1101+
"Frame header size does not match decoded size!");
1102+
}
1103+
}
1104+
10961105
assert(decodedSize <= dstCapacity);
10971106
dst = (BYTE*)dst + decodedSize;
10981107
dstCapacity -= decodedSize;

tests/fuzz/simple_decompress.c

+7-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,13 @@ int LLVMFuzzerTestOneInput(const uint8_t *src, size_t size)
3737
size_t const bufSize = FUZZ_dataProducer_uint32Range(producer, 0, 10 * size);
3838
void *rBuf = FUZZ_malloc(bufSize);
3939

40-
ZSTD_decompressDCtx(dctx, rBuf, bufSize, src, size);
40+
size_t const dSize = ZSTD_decompressDCtx(dctx, rBuf, bufSize, src, size);
41+
if (!ZSTD_isError(dSize)) {
42+
/* If decompression was successful, the content size from the frame header(s) should be valid. */
43+
size_t const expectedSize = ZSTD_findDecompressedSize(src, size);
44+
FUZZ_ASSERT(expectedSize != ZSTD_CONTENTSIZE_ERROR);
45+
FUZZ_ASSERT(expectedSize == ZSTD_CONTENTSIZE_UNKNOWN || expectedSize == dSize);
46+
}
4147
free(rBuf);
4248

4349
FUZZ_dataProducer_free(producer);

tests/zstreamtest.c

+9
Original file line numberDiff line numberDiff line change
@@ -2408,6 +2408,15 @@ static int basicUnitTests(U32 seed, double compressibility, int bigTests)
24082408
}
24092409
DISPLAYLEVEL(3, "OK \n");
24102410

2411+
DISPLAYLEVEL(3, "test%3i : Decoder should reject invalid frame header on legacy frames: ", testNb++);
2412+
{
2413+
const unsigned char compressed[] = { 0x26,0xb5,0x2f,0xfd,0x50,0x91,0xfd,0xd8,0xb5 };
2414+
const size_t compressedSize = 9;
2415+
size_t const dSize = ZSTD_decompress(NULL, 0, compressed, compressedSize);
2416+
CHECK(!ZSTD_isError(dSize), "must reject when legacy frame header is invalid");
2417+
}
2418+
DISPLAYLEVEL(3, "OK \n");
2419+
24112420
_end:
24122421
FUZ_freeDictionary(dictionary);
24132422
ZSTD_freeCStream(zc);

0 commit comments

Comments
 (0)