🚀 New: chi (χ) — an open-source autoresearch harness for fleets of LLM coding agents. Read the announcement.

A Reused zstd Decompression Context Could Hand One Request's Bytes to the Next (CVE-2026-124851)

CVE-2026-124851: in zstd 1.5.1 to 1.5.7, a malformed frame fed through a reused streaming decompression context could copy uninitialized memory or literals from earlier decompressions into its own output. Why the decoder trusted the wrong history bound, why a fresh context per request is not a full fix, and what upgrading to 1.6.0 changes besides the CVE.

Contents

zstd’s own header says that when you decompress many times, you should “allocate a context only once, and reuse it for each successive compression operation.” In zstd 1.5.1 through 1.5.7, a crafted frame decompressed through a streaming context could pull bytes into its own output that were left over from earlier decompressions on the same context, or that were never initialized at all. The advisory puts the exposed internal buffer at 64 KiB in the default build. The fix is CVE-2026-124851, and the only release that has it is v1.6.0, tagged on November 23.

To get anything out of this, the attacker needs two things. They have to be able to submit compressed data, and they have to see what it decompresses to. If you decompress uploads and serve them back, run an endpoint that echoes or transforms a request body, or proxy Content-Encoding: zstd bodies after inflating them, you meet both. If you only decompress your own files, you don’t.

The decoder checked offsets against the wrong bound

A zstd frame is a sequence of literals plus back-references, each saying “copy N bytes from offset back”. The frame header declares a window size, which is the furthest back any offset may legally reach. You’d expect the decoder to reject offsets larger than that. In 1.5.7 the check is this, in zstd_decompress_block.c:

RETURN_ERROR_IF(sequence.offset > (size_t)(oLitEnd - virtualStart), corruption_detected, "");

virtualStart is the start of whatever history the decoder currently has pointers to: the current contiguous output (the prefix), plus an “external dictionary” segment behind it. The offset is bounded by how much history the decoder thinks it holds, not by the window the frame declared.

The external dictionary is set in ZSTD_checkContinuity(). Whenever output stops being contiguous, the whole previous segment becomes external history:

if (dst != dctx->previousDstEnd && dstSize > 0) {   /* not contiguous */
    dctx->dictEnd = dctx->previousDstEnd;
    dctx->virtualStart = (const char*)dst - ((const char*)(dctx->previousDstEnd) - (const char*)(dctx->prefixStart));
    dctx->prefixStart = dst;
    dctx->previousDstEnd = dst;
}

In the default streaming mode, ZSTD_decompressStream() doesn’t decode into your buffer. It decodes into an internal ring buffer, outBuff, sized for the window plus a block, and copies out from there. Each frame starts writing at offset 0 of that buffer, and when it nears the end it wraps back to 0. At the wrap, the previous pass becomes the external dictionary in full, and the new prefix starts growing over the same memory. The claimed history and the bytes being written now overlap, and the offset check above doesn’t know it.

What sits in that overlapping memory is what leaks. Two properties of the 1.5.x decoder decide that.

Two buffers the context keeps between frames

The first is the ring buffer itself. outBuff is allocated with plain malloc (via ZSTD_customMalloc), and it is only reallocated when a frame needs a bigger one or when it has been oversized for too long. Otherwise the next frame on the same context gets the same memory with the previous frame’s output still in it. On a context’s first use, it holds whatever the heap had there before.

The second explains why the range starts at 1.5.1. That release merged #2751, “Reduce size of dctx by reutilizing dst buffer”. After decoding a block’s literals, the decoder stages them in spare space at the end of the destination buffer when they fit there. When they only partly fit, the rest goes into litExtraBuffer, a fixed array inside the context:

BYTE litExtraBuffer[ZSTD_LITBUFFEREXTRASIZE + WILDCOPY_OVERLENGTH];

ZSTD_LITBUFFEREXTRASIZE defaults to 1 << 16. That’s the 64 KiB in the advisory. Literal copies use a “wildcopy” that is allowed to read up to WILDCOPY_OVERLENGTH (32) bytes past the end of the data, which is why the array carries that much padding. Before the fix, the bytes after a block’s decoded literals were never cleared, so they could still hold literals from an earlier decompression, or bytes nobody had written since the context was allocated.

Taken together: a frame whose offsets point back past its declared window, after a wrap, could make the decoder copy from memory that held another frame’s literals or output, and those bytes come out as ordinary decompressed data. A content checksum doesn’t stop this either. The frame’s author decides whether a checksum is present, and an attacker who can’t predict the leaked bytes simply leaves it off.

The fix is 35 lines

The patch is commit 0c9d297, by Nick Terrell, merged the day of the release. It does two things.

It trims the external dictionary once a full window has been decoded, so that external history plus the current prefix never exceed the frame’s window. The code comment says the old behavior “could let corrupt offsets alias the destination and expose stale literals”:

if (dstSize > 0 && dctx->isFrameDecompression && dctx->decodedSize > 0 && dctx->decodedSize >= dctx->fParams.windowSize) {
    size_t const prefixSize = (size_t)((const char*)dctx->previousDstEnd - (const char*)dctx->prefixStart);
    size_t const extDictSize = (size_t)((const char*)dctx->prefixStart - (const char*)dctx->virtualStart);
    U64 const maxExtDictSize = prefixSize < dctx->fParams.windowSize ? dctx->fParams.windowSize - prefixSize : 0;
    dctx->virtualStart = (const char*)dctx->prefixStart - (size_t)MIN((U64)extDictSize, maxExtDictSize);
}

It also adds ZSTD_zeroLiteralsBufferPadding(), which zeroes the WILDCOPY_OVERLENGTH bytes after the decoded literals, in both halves when they are split between the destination and litExtraBuffer. Over-reads still happen. They just read zeros now.

The offset check itself is unchanged. The fix makes virtualStart mean what the check assumed it meant.

A fresh context per request is not enough

The obvious stopgap is to stop pooling contexts across trust boundaries, giving each tenant or request its own ZSTD_DCtx. That removes the cross-request half. It doesn’t remove the uninitialized-memory half, because a new context is a new malloc, and in a long-running server freed heap often holds the previous request’s data anyway. Per-request contexts make the leak less targeted. They don’t close it.

Upgrading to 1.6.0 changes more than the CVE

The fix is only in 1.6.0, a release with 626 commits since 1.5.7, and the release notes list API changes you should read before bumping:

Change in 1.6.0Who notices
Legacy format support defaults to off in Make, CMake and Meson (#4554). 1.5.7 defaulted to level 5, which decodes frames from zstd v0.5 onward.Anyone reading pre-1.0 zstd data, which means files written in 2016 or earlier. Rebuild with ZSTD_LEGACY_SUPPORT=5 if you need them.
ZSTD_decompressDCtx() and the _usingDict variants now reset an unfinished streaming session (#4975).Code that mixed one-shot and streaming calls on one context.
ZSTD_d_refMultipleDDicts requires an exact dictionary ID match (#4929).Users of that experimental parameter.
Experimental ZSTD_copyDCtx() and ZSTD_copyCCtx() are removed (#4960).Static-linking users of the experimental API.

If you can’t take all of that, the patch does apply to the v1.5.7 tag. patch --dry-run accepts all five hunks, four of them at shifted line numbers, with no fuzz. I haven’t built or tested that backport, and it covers only this CVE. The same release fixes a use-after-free of a ZSTD_DCtx_refPrefix() prefix after ZSTD_DCtx_reset() on a malformed frame header (#4848), out-of-bounds access with ZSTD_d_refMultipleDDicts (#4731), and several decoder bugs in the legacy formats. 1.6.0 is the better target.

Your distro’s libzstd may not be the copy that runs

Many consumers ship their own libzstd instead of linking the system one. As of today, these all bundle 1.5.7 on their main branches:

A distro update to libzstd won’t touch any of them. You’ll need each project’s own release, or a rebuild against a fixed system library where the project supports one. Go programs on klauspost/compress don’t use libzstd at all. That’s a separate implementation, and this CVE doesn’t apply to it.

When you audit your own services, look for the places where you decompress data someone else wrote, through a context that outlives one request, and send the result somewhere that person can read it. Upgrade those first.

Sources