Skip to content

The returned value from ZSTD_getFrameContentSize is not checked. #46

@mkitti

Description

@mkitti

The return value of ZSTD_getFrameContentSize is not checked before being passed to malloc.

int dest_size = ZSTD_getFrameContentSize(source_ptr, source_size);
dest_ptr = (char *)malloc((size_t)dest_size);

Here is the relevant excerpt from the Zstd manual as of version 1.5.1

#define ZSTD_CONTENTSIZE_UNKNOWN (0ULL - 1)
#define ZSTD_CONTENTSIZE_ERROR (0ULL - 2)
unsigned long long ZSTD_getFrameContentSize(const void *src, size_t srcSize);
src should point to the start of a ZSTD encoded frame.
srcSize must be at least as large as the frame header.
hint : any size >= ZSTD_frameHeaderSize_max is large enough.
@return : - decompressed size of src frame content, if known
- ZSTD_CONTENTSIZE_UNKNOWN if the size cannot be determined
- ZSTD_CONTENTSIZE_ERROR if an error occurred (e.g. invalid magic number, srcSize too small)
note 1 : a 0 return value means the frame is valid but "empty".
note 2 : decompressed size is an optional field, it may not be present, typically in streaming mode.
When return==ZSTD_CONTENTSIZE_UNKNOWN, data to decompress could be any size.
In which case, it's necessary to use streaming mode to decompress data.
Optionally, application can rely on some implicit limit,
as ZSTD_decompress() only needs an upper bound of decompressed size.
(For example, data could be necessarily cut into blocks <= 16 KB).
note 3 : decompressed size is always present when compression is completed using single-pass functions,
such as ZSTD_compress(), ZSTD_compressCCtx() ZSTD_compress_usingDict() or ZSTD_compress_usingCDict().
note 4 : decompressed size can be very large (64-bits value),
potentially larger than what local system can handle as a single memory segment.
In which case, it's necessary to use streaming mode to decompress data.
note 5 : If source is untrusted, decompressed size could be wrong or intentionally modified.
Always ensure return value fits within application's authorized limits.
Each application can set its own limits.
note 6 : This function replaces ZSTD_getDecompressedSize()

The return value from ZSTD_getFrameContentSize could be either ZSTD_CONTENTSIZE_UNKNOWN or ZSTD_CONTENTSIZE_ERROR. Preferably the value should be bounded by the expected destination buffer size rather than letting this be unconstrained from the data.

xref: google/neuroglancer#625

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions