GifQuantizeBuffer() (quantize.c) declares a fixed NewColorMapType
NewColorSubdiv[256] array on the stack and passes the caller-supplied
*ColorMapSize straight through to SubdivColorMap() with no check
that it is ≤ 256 anywhere in either function. SubdivColorMap()'s main
loop (while (ColorMapSize > *NewColorMapSize) { ... NewColorSubdiv[*NewColorMapSize] ...; (*NewColorMapSize)++; })
keeps writing further into NewColorSubdiv[] for every value of
*NewColorMapSize up to ColorMapSize - 1. If ColorMapSize > 256 and
the source image actually has enough distinct sampled colors to keep
subdividing that far, this writes past the end of the 256-element stack
array — a stack buffer overflow with attacker-influenced write content
(color-space partition bookkeeping fields, not raw pixel bytes, but
fully influenced by the pixel data and the requested size).
The doc comment above GifQuantizeBuffer even says "ColorMapSize
specifies size of color map up to 256" — this is advisory text only;
nothing in the code enforces it.
gif2rgb -cgif2rgb.c's RGB2GIF() computes ColorMapSize = 1 << ExpNumOfColors
(line 207) and passes it straight to GifQuantizeBuffer() (line 223).
ExpNumOfColors comes directly from the -c command-line option
(getarg control string c%-#Colors!d, default 8, no upper-bound
validation anywhere). -c 9 already gives ColorMapSize = 512 > 256.
So this is not just an internal-API hazard — the shipped gif2rgb utility
reaches it directly from an ordinary command-line flag.
Current master, cloned from git://git.code.sf.net/p/giflib/code,
commit a8e3114 (HEAD at time of testing, "NEWS update." — this is after
the 6.1.3-in-progress fixes for CVE-2026-26740 and the integer-overflow /
GifUnionColorMap-null-deref hardening pass, so this is a currently
unfixed, separate bug from those). quantize.c has not been touched by
any of that recent hardening work.
poc/poc_input.rgb (128×128 random RGB pixels, interleaved R,G,B per
pixel — the format gif2rgb -1 expects) plus poc/make_poc_input.py (the
generator). Build gif2rgb with AddressSanitizer and run:
gif2rgb -1 -c 9 -s 128 128 -o out.gif poc_input.rgb
This crashes immediately with AddressSanitizer: stack-buffer-overflow
in SubdivColorMap (full log in poc/asan_output.txt):
==918==ERROR: AddressSanitizer: stack-buffer-overflow ...
WRITE of size 8 ...
#0 SubdivColorMap quantize.c:316:52
#1 GifQuantizeBuffer quantize.c:139:6
#2 RGB2GIF gif2rgb.c:223:6
#3 main gif2rgb.c:581:3
...
[80, 8272) 'NewColorSubdiv' (line 72) <== Memory access at offset 8296 overflows this variable
A build without sanitizers corrupts adjacent stack memory silently
instead (the overwritten bytes are internal NewColorMapType bookkeeping
fields — RGBMin/RGBWidth/NumEntries/Count/a linked-list pointer —
not raw pixel data, but their values are shaped by the image content and
requested color count, and nothing stops the write from eventually
reaching whatever else is on the stack past the array if the subdivision
runs long enough).
poc/standalone_repro.c is a minimal, giflib-source-only reproduction
that calls GifQuantizeBuffer() directly with ColorMapSize = 1000 and a
256×256 synthetic image with many distinct colors, for anyone who wants
to reproduce it without building the full gif2rgb CLI tool — same crash,
same stack frame.
In GifQuantizeBuffer(), validate *ColorMapSize (reject or clamp to
256) before calling SubdivColorMap() — mirroring the kind of
input-size check the project has already been adding elsewhere this year
(e.g. the GifMakeMapObject power-of-two/size check). Separately,
gif2rgb.c's argument handling for -c should reject #Colors > 256
(equivalently ExpNumOfColors > 8) before calling RGB2GIF(), as a
second line of defense for the shipped tool specifically.
The same unclamped ColorMapSize/ExpNumOfColors > 8 also reaches a
different bug downstream once quantization returns a color count
needing 9+ bits: EGifPutLine() in egif_lib.c indexes the global
CodeMask[] array (9 entries, valid indices 0-8) with a BitsPerPixel
of 9, an out-of-bounds global read caught by ASan as
global-buffer-overflow in the same session's testing (not included as
a separate PoC here since it is a direct consequence of the same missing
upper-bound check — fixing the #Colors/ColorMapSize validation above
should close this one too).