Skip to content

ZDICT_trainFromBuffer crashes with a segmentation fault #4827

Open

Description

@neuecc

Describe the bug
ZDICT_trainFromBuffer crashes with a segmentation fault (access violation on Windows) for a small, well formed set of samples.

FASTCOVER_ctx_init validates totalSamplesSize, but nbDmers is computed from trainingSamplesSize, which only covers the first splitPoint (default 0.75) of the samples. When that training part holds fewer than MAX(d, sizeof(U64)) bytes, the subtraction underflows and FASTCOVER_computeFrequency walks far past the sample buffer.

lib/dictBuilder/fastcover.c, FASTCOVER_ctx_init (v1.5.7, lines 320 to 359, unchanged on dev):

const size_t totalSamplesSize = COVER_sum(samplesSizes, nbSamples);
const unsigned nbTrainSamples = splitPoint < 1.0 ? (unsigned)((double)nbSamples * splitPoint) : nbSamples;
const size_t trainingSamplesSize = splitPoint < 1.0 ? COVER_sum(samplesSizes, nbTrainSamples) : totalSamplesSize;

/* Checks */
if (totalSamplesSize < MAX(d, sizeof(U64)) || totalSamplesSize >= (size_t)FASTCOVER_MAX_SAMPLES_SIZE) {
    return ERROR(srcSize_wrong);
}
...
ctx->nbDmers = trainingSamplesSize - MAX(d, sizeof(U64)) + 1;

With the input below and the default splitPoint of 0.75, nbTrainSamples is 5 and trainingSamplesSize is 5, while totalSamplesSize is 106 and passes the check. nbDmers becomes 5 - 8 + 1, which wraps to SIZE_MAX - 1.

ZDICT_trainFromBuffer reaches this through ZDICT_optimizeTrainFromBuffer_fastCover with d = 8. Any sample set where the first 75% of the samples sum to fewer than 8 bytes while the whole set sums to 8 bytes or more triggers it. Zero length samples make it easy to hit (for example 58 empty samples followed by one of 15 bytes), but all positive lengths work as well, as in the reproduction.

To Reproduce
Steps to reproduce the behavior:

  1. Save the following as repro.c in a checkout of zstd v1.5.7:
#include <stdio.h>
#include <string.h>
#include "zstd.h"
#include "zdict.h"

int main(void)
{
    /* seven samples: six of one byte and one of 100 bytes */
    unsigned char samples[106];
    size_t sizes[7] = { 1, 1, 1, 1, 1, 1, 100 };
    unsigned char dict[256];
    memset(samples, 'a', sizeof(samples));

    size_t r = ZDICT_trainFromBuffer(dict, sizeof(dict), samples, sizes, 7);
    if (ZDICT_isError(r)) printf("error: %s\n", ZDICT_getErrorName(r));
    else printf("dictionary of %zu bytes\n", r);
    return 0;
}
  1. Build it together with the library sources:
clang -O1 -DZSTD_DISABLE_ASM=1 -DZSTD_LEGACY_SUPPORT=0 -I lib -I lib/common -I lib/dictBuilder \
    lib/common/*.c lib/compress/*.c lib/decompress/*.c lib/dictBuilder/*.c repro.c -o repro
  1. Run ./repro
  2. See Segmentation fault (exit code 139). Under a debugger the faulting frame is FASTCOVER_computeFrequency, called from FASTCOVER_ctx_init.

Expected behavior
ZDICT_trainFromBuffer returns an error code, srcSize_wrong, as it does for other sample sets that are too small, instead of reading out of bounds.

Screenshots and charts
Not applicable.

Desktop (please complete the following information):

  • OS: Windows 11 Pro (10.0.26200), x64
  • Version: zstd 1.5.7 (ZSTD_versionString()), the same lines are present on dev
  • Compiler: clang 18.1.8
  • Flags: -O1 -DZSTD_DISABLE_ASM=1 -DZSTD_LEGACY_SUPPORT=0
  • Other relevant hardware specs: x64, not hardware specific
  • Build system: direct clang invocation over lib/**/*.c, see step 2. Also reproduced with the prebuilt libzstd.dll of a .NET binding and with .NET 11 (RC 1) System.IO.Compression.ZstandardDictionary.Train, which bundles zstd.

Additional context

lib/dictBuilder/cover.c, COVER_ctx_init, has the same arithmetic: ctx->suffixSize = trainingSamplesSize - MAX(d, sizeof(U64)) + 1; after a check on totalSamplesSize only. The same seven samples passed through ZDICT_optimizeTrainFromBuffer_cover did not crash in my run, but the underflow looks reachable there too.

I am currently building zstd bindings for .NET, and I found this during fuzzing. The issue text and the reproduction code were written by Claude Code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions