Skip to content

Commit 0ce4b80

Browse files
committed
Fix legacy frame header fuzzer crash, add unit test
1 parent 9e8da5f commit 0ce4b80

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
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/zstreamtest.c

+9
Original file line numberDiff line numberDiff line change
@@ -2407,6 +2407,15 @@ static int basicUnitTests(U32 seed, double compressibility, int bigTests)
24072407
ZSTD_freeCCtxParams(params);
24082408
}
24092409
DISPLAYLEVEL(3, "OK \n");
2410+
2411+
DISPLAYLEVEL(3, "test%3i : Decoder should reject invalid frame header on legacy frames: ", testNb++);
2412+
{
2413+
const 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");
24102419

24112420
_end:
24122421
FUZ_freeDictionary(dictionary);

0 commit comments

Comments
 (0)