Menu ▾ ▴

#217 gifbuild: stack buffer overflow (1-byte OOB write) in Icon2Gif keyed color-map parsing (gifbuild.c:237)

v1.0_(example)
open
nobody
6
2026-11-04
2026-11-04
Alex
No

Version: giflib 5.2.2, HEAD (commit a8e3114)
Tool: gifbuild
Class: CWE-787 out-of-bounds write (stack-buffer-overflow)

Summary

In Icon2Gif, a keyed color-map line rgb R G B is <char> is parsed with
sscanf, which writes the %c key straight into KeyTable[ColorMapSize]
before ColorMapSize is bounds-checked against PRINTABLES (93). KeyTable
points at GlobalColorKeys/LocalColorKeys, both char[93] on the stack. The
94th keyed entry (ColorMapSize == 93) writes KeyTable[93] — one byte past the
array — and only then does the guard fire. The overflowing byte is
attacker-controlled.

Affected code (gifbuild.c:237)

else if (sscanf(buf, "\trgb %d %d %d is %c", &red, &green,
&blue, &KeyTable[ColorMapSize]) == 4) { // write happens here
if (ColorMapSize >= 256) { ... exit; }
if (ColorMapSize >= PRINTABLES) { ... exit; } // check runs too late

Reproduce

{ printf 'screen width 10\nscreen height 10\nscreen map\n';
for i in $(seq 1 94); do printf '\trgb 1 2 3 is X\n'; done; } > poc.txt
./gifbuild < poc.txt >/dev/null

AddressSanitizer

AddressSanitizer: stack-buffer-overflow WRITE of size 1
#4 sscanf
#5 Icon2Gif gifbuild.c:237
[1824, 1917) 'GlobalColorKeys' (line 117) <== overflows this variable
The write lands exactly at the end of the 93-byte GlobalColorKeys array.

Suggested fix

Parse the key into a local char and store it only after the bounds check:
else if (sscanf(buf, "\trgb %d %d %d is %c", &red, &green, &blue, &KeyChar) == 4) {
if (ColorMapSize >= 256) { ...exit; }
if (ColorMapSize >= PRINTABLES){ ...exit; }
KeyTable[ColorMapSize] = KeyChar; /* only after the bounds check */
...
}
Verified: with this change the PoC exits cleanly ("Too many color entries for
keyed color map.") and ASan reports nothing.

1 Attachments

Discussion


Log in to post a comment.