Repository navigation
Conversation
…ming entry points
Add defensive parameter validation against NULL pointers across public CCtx,
DCtx, and streaming APIs:
1. In lib/compress/zstd_compress.c:
- Validate cctx != NULL in ZSTD_CCtx_reset, ZSTD_CCtx_setParameter,
ZSTD_CCtx_getParameter, and ZSTD_CCtx_setPledgedSrcSize before accessing
streamStage or struct members.
- Validate CCtxParams != NULL and value != NULL in ZSTD_CCtxParams_setParameter
and ZSTD_CCtxParams_getParameter.
- Validate cctx, output, and input pointers in ZSTD_compressStream2 before
checking pos and size bounds.
- Validate dstPos != NULL and srcPos != NULL in ZSTD_compressStream2_simpleArgs.
- Validate cctx != NULL in ZSTD_compress2 and return 0 for NULL cctx in
ZSTD_getBlockSize_deprecated.
- Validate zcs != NULL and output != NULL in ZSTD_flushStream and ZSTD_endStream.
2. In lib/decompress/zstd_decompress.c:
- Validate dctx != NULL in ZSTD_DCtx_reset, ZSTD_DCtx_setParameter,
ZSTD_DCtx_getParameter, ZSTD_decompressBegin, and ZSTD_decompressBegin_usingDDict.
- Validate zds, output, and input pointers in ZSTD_decompressStream prior
to reading buffer fields.
- Validate dstPos != NULL and srcPos != NULL in ZSTD_decompressStream_simpleArgs.
- Return 0 on NULL dctx in ZSTD_nextSrcSizeToDecompress and
ZSTD_nextSrcSizeToDecompressWithInputSize; return ZSTDnit_frameHeader on NULL
dctx in ZSTD_nextInputType.
3. In tests/zstreamtest.c:
- Add unit test coverage in basicUnitTests verifying that NULL pointer arguments
are rejected with appropriate error codes rather than crashing.
Signed-off-by: Filippo Tedeschi <filippotedeschi98@gmail.com>
Author
|
Closing. This adds NULL / overflow checks for arguments the callers are not |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This patch hardens core public compression and decompression APIs across
lib/compress/zstd_compress.candlib/decompress/zstd_decompress.cagainst crashes caused byNULLpointer dereferences:lib/compress/zstd_compress.c:ZSTD_CCtx_reset(): EnforceRETURN_ERROR_IF(!cctx, GENERIC, "NULL pointer!");before accessingcctx->streamStage.ZSTD_CCtx_setParameter(): Validatecctx != NULLbefore accessingcctx->streamStage.ZSTD_CCtxParams_setParameter(): ValidateCCtxParams != NULLbefore switching on parameters and writing struct fields.ZSTD_CCtx_getParameter()&ZSTD_CCtxParams_getParameter(): Validatecctx != NULL,CCtxParams != NULL, andvalue != NULLout-pointer.ZSTD_CCtx_setPledgedSrcSize(): Validatecctx != NULLbefore readingstreamStage.ZSTD_compressStream2(): Enforce defensive NULL checks oncctx,output, andinputpointers prior to checkingposandsizeoffsets.ZSTD_compressStream2_simpleArgs(): Enforce defensive NULL checks on out-pointersdstPosandsrcPosbefore dereferencing them.ZSTD_compress2(): Validatecctx != NULLbefore reading buffer modes.ZSTD_getBlockSize_deprecated(): Safely return0whencctx == NULL.ZSTD_flushStream()&ZSTD_endStream(): EnforceRETURN_ERROR_IF(!zcs || !output, GENERIC, "NULL pointer!");before invokinginBuffer_forEndFlush(zcs).lib/decompress/zstd_decompress.c:ZSTD_DCtx_reset(): EnforceRETURN_ERROR_IF(!dctx, GENERIC, "NULL pointer!");before accessingdctx->streamStage.ZSTD_DCtx_setParameter(): EnforceRETURN_ERROR_IF(!dctx, GENERIC, "NULL pointer!");before accessingdctx->streamStage.ZSTD_DCtx_getParameter(): Validatedctx != NULLandvalue != NULL.ZSTD_decompressBegin()&ZSTD_decompressBegin_usingDDict(): Validatedctx != NULL.ZSTD_nextSrcSizeToDecompress()&ZSTD_nextSrcSizeToDecompressWithInputSize(): Gracefully return0whendctx == NULL.ZSTD_nextInputType(): Gracefully returnZSTDnit_frameHeaderwhendctx == NULL.ZSTD_decompressStream(): EnforceRETURN_ERROR_IF(!zds || !output || !input, GENERIC, "NULL pointer!");before readinginput->src,input->pos,output->dst,output->pos.ZSTD_decompressStream_simpleArgs(): EnforceRETURN_ERROR_IF(!dstPos || !srcPos, GENERIC, "NULL pointer!");before dereferencing.tests/zstreamtest.c:basicUnitTestsasserting that passingNULLarguments safely returns error codes without segmentation faults.