ZSTD_CCtx_reset(session_only) keeps pending stable input: next frame reads before the caller's buffer
Los mantenedores suelen responder en 2 días
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Aptitud para principiantes
- 72/100
Línea de trabajo
Empieza en lib/compress/zstd_compress.c en ZSTD_CCtx_reset, donde la rama exclusiva de sesión restablece streamStage y pledgedSrcSizePlusOne pero deja stableIn_notConsumed establecido; lee también los lugares que lo restan en ZSTD_compressStream_generic y la rama ZSTDMT para confirmar la invariante. Construye el reproductor del issue contra una compilación con sanitizer y observa las pruebas native bajo tests/ para las rutas de streaming. Terminado se ve como el segundo frame descomprimiendo exactamente a 50000 bytes sin desbordamiento de ASan y con la aserción de depuración silenciosa; una prueba de regresión en las pruebas unitarias de streaming sería el acompañante natural.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
Describe the bug
With ZSTD_c_stableInBuffer=1, ZSTD_compressStream2(..., ZSTD_e_continue) on less than one block of input "pretends" to consume it and records the amount in cctx->stableIn_notConsumed (transparent initialization stage of ZSTD_compressStream2 in lib/compress/zstd_compress.c). If the frame is then abandoned with ZSTD_CCtx_reset(cctx, ZSTD_reset_session_only), stableIn_notConsumed is not cleared. The next frame adds it to its input size and later subtracts it from the new input->pos (input->pos -= cctx->stableIn_notConsumed in ZSTD_compressStream_generic and in the ZSTDMT branch). Compression therefore starts stableIn_notConsumed bytes before the caller's new buffer.
Effects:
- out-of-bounds read of up to one block (128 KB) before the new input buffer (AddressSanitizer: heap-buffer-overflow);
- in release builds the call succeeds and the frame silently contains those bytes, so the output is wrong, adjacent memory is disclosed, and the frame content size is too large;
assert(input->pos >= cctx->stableIn_notConsumed)fires in debug builds.
ZSTD_CCtx_reset documents a session reset as abandoning the current frame, so starting a new frame afterwards is a valid sequence.
To Reproduce (zstd dev at 49cf517, Linux x86-64, gcc 13)
#define ZSTD_STATIC_LINKING_ONLY
#include <zstd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
size_t const n1 = 1000, n2 = 50000, cap = ZSTD_compressBound(n1 + n2);
char* first = malloc(n1); char* second = malloc(n2);
char* dst = malloc(cap); char* back = malloc(n1 + n2);
ZSTD_CCtx* cctx = ZSTD_createCCtx();
memset(first, 'a', n1); memset(second, 'b', n2);
ZSTD_CCtx_setParameter(cctx, ZSTD_c_stableInBuffer, 1);
{ ZSTD_inBuffer in = { first, n1, 0 }; ZSTD_outBuffer out = { dst, cap, 0 };
ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_continue); /* pending, not compressed */
}
ZSTD_CCtx_reset(cctx, ZSTD_reset_session_only); /* abandon frame 1 */
{ ZSTD_inBuffer in = { second, n2, 0 }; ZSTD_outBuffer out = { dst, cap, 0 };
size_t const r = ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_end);
size_t const d = ZSTD_decompress(back, n1 + n2, dst, out.pos);
printf("r=%zu content size %llu, decompressed %zu (expected %zu)\n", r,
ZSTD_getFrameContentSize(dst, out.pos), d, n2);
}
return 0;
}
Release build output:
r=0 content size 51000, decompressed 51000 (expected 50000)
With -fsanitize=address:
ERROR: AddressSanitizer: heap-buffer-overflow ... READ of size 8
#0 MEM_read64 lib/common/mem.h:184
...
#4 ZSTD_compressBlock_doubleFast_noDict_generic lib/compress/zstd_double_fast.c:176
...
#10 ZSTD_compressContinue_internal lib/compress/zstd_compress.c:4863
#11 ZSTD_compressEnd_public lib/compress/zstd_compress.c:5441
#12 ZSTD_compressStream_generic lib/compress/zstd_compress.c:6180
#13 ZSTD_compressStream2 lib/compress/zstd_compress.c:6569
Expected behavior
The second frame contains exactly the second buffer (50000 bytes), and nothing outside it is read.
Suggested fix
Clear the pending stable input when the session is reset:
size_t ZSTD_CCtx_reset(ZSTD_CCtx* cctx, ZSTD_ResetDirective reset)
{
if ( (reset == ZSTD_reset_session_only)
|| (reset == ZSTD_reset_session_and_parameters) ) {
cctx->streamStage = zcss_init;
cctx->pledgedSrcSizePlusOne = 0;
+ cctx->stableIn_notConsumed = 0;
}
With this change the reproducer prints content size 50000, decompressed 50000 (expected 50000).
Environment
- zstd
devbranch, commit 49cf517 (v1.6.0-dev) - Ubuntu 24.04, x86-64, gcc 13.3
Found while stress-testing a match-finder change with randomized streaming cases; reproduced on unmodified dev.
- Lenguaje dominante
- C
- Estrellas
- 28.1k
- Forks
- 2.7k
- Merge medio
- 2 d 9 h
- PR fusionados (30 d)
- 6
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de facebook/zstd
-
cmake: AddZstdCompilationFlags still depends on C++ after #4357Posiblemente ocupada @Tyagiquamar la tomó hace 27 días. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Los mantenedores suelen responder en 2 días
-
ZSTD_compressSequencesAndLiterals() does not enforce its documented litCapacity >= litSize + 8 contract — the implemented check is 8 bytes weaker than the API documentation, its own error message, and the introducing commitPosiblemente ocupada @KSAGlory la tomó hace 47 días. Abierto
Dificultad 1/5 Menos de una hora Aptitud para principiantes 88/100
facebook/zstd#4779 · 1 comentario ·
Los mantenedores suelen responder en 2 días
-
seekable_format warning with -DNDEBUG: potential uninitialised local variable newOffset usedPosiblemente ocupada @i098 la tomó hace 17 días. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Los mantenedores suelen responder en 2 días
-
COVER_selectDict() uses stale customDictContentEnd after ZDICT_finalizeDictionary modifies dictContentSizePosiblemente ocupada @VirajMishra1 la tomó hace 51 días. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
Los mantenedores suelen responder en 2 días
-
cli-tests: levels.sh --max test fails on systems with < 10GB RAMPosiblemente ocupada @JohannesGezachew la tomó hace 47 días. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 85/100
facebook/zstd#4748 · 1 comentario ·
Los mantenedores suelen responder en 2 días
Todos los issues de facebook/zstd
Issues similares
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 66/100
Los mantenedores suelen responder en 2 días
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
zephyrproject-rtos/zephyr#123881 ·
Los mantenedores suelen responder en 2 días
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 77/100
eclipse-zenoh/zenoh-pico#1354 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 65/100
ly05010419/MacNativeGames#1252 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
Los mantenedores suelen responder en 2 días