Repository navigation
Conversation
The decompression of ancillary chunks is governed by a single limit, png_set_chunk_malloc_max (8,000,000 bytes by default). ICC profiles legitimately occupy megabytes, so the limit has to stay that large; but the same budget is also granted to text chunks, where a compressed chunk of a few kilobytes can expand to the full limit (a crafted zTXt of 7,691 compressed bytes expands 1027x, to 7,900,000 bytes, and is stored in full by an unmodified build). The TODO comments in png_decompress_chunk and at its two call sites have long noted that the limit should be split to different values for iCCP and text chunks. Split it. A new limit, PNG_USER_CHUNK_TEXT_MAX (1,000,000 bytes by default), applies to the decompression of zTXt and iTXt chunks in png_decompress_chunk, in addition to the existing chunk limit: the effective limit is the smaller of the two, so the text limit can only tighten, never loosen, the general limit. iCCP and the other chunks are unchanged. The new limit mirrors the existing one throughout: a pnglibconf setting, a png_struct field, the png_chunk_text_max helper in pngpriv.h, and the run-time accessors png_set_chunk_text_max and png_get_chunk_text_max, with the same "0 means unlimited" convention as png_set_chunk_malloc_max (the general limit still applies in that case). An over-limit text chunk is ignored with the same benign error as an over-limit chunk before this change. Applications that legitimately store larger text (for example very large XMP packets in iTXt) can raise or remove the limit with the new setter. Add a regression test, pngtextlimit, with crafted PNG files: a zTXt that expands to 7,900,000 bytes is ignored by default and stored when the limit is raised or removed; a 2,097,152-byte ICC profile is stored by default; small text chunks are unaffected. The full test suite passes.
|
I think you are misunderstanding the code:
Notice that, as you correctly observe, this is libpng stores known chunks in a completely uncompressed form regardless of the chunk. So that limit applies to tEXt chunks; it happens in The problem is interesting but an attacker can always craft a valid iCCP chunk to exploit any hole that may appear with zTXt. On the other hand imagemagick did embed zTXt chunks with binhex encodings of ICC profiles; i.e. valid zTXt encodings of ICC profiles. An app which can't handle an 8MByte allocation for a text chunk can't handle one for an ICC profile; you seem to have created complexity for no benefit. |
|
Thanks for the careful read — you're right that png_chunk_max governs the uncompressed size and that iCCP legitimately needs the full budget, and the PR description's 'decompressed' wording was imprecise on that point. The narrow case for the split is for apps that DO want the 8 MB ICC budget but don't want a ~7 KB zTXt expanding to ~8 MB (1027x, measured in the PR evidence): today their only knob is the shared cap, so tightening it to stop that amplification also breaks their legit ICC profiles. The split lets them cap text at 1 MB while keeping ICC at 8 MB — which is also what the in-tree TODOs at png_decompress_chunk and both text call sites ask for. Happy to shrink this if the new public API is the objection — e.g. keep just the internal default split with no new symbols — or to close it if you still see no benefit. Your call. |
Motivation
png_decompress_chunk()caps every decompressed chunk at the singleapplication-level limit
png_chunk_max()(default 8,000,000 bytes).That budget exists because ICC profiles legitimately run to megabytes —
but it is also granted to text chunks, which have no comparable
legitimate need. A zTXt of 7,691 compressed bytes expands to
7,900,000 bytes (~1027×) and is stored in full by an unmodified
build: a ~8 KB file forces an ~8 MB allocation (and retention, in the
stored text array) in every application that reads it with default
settings. This implements the split that the in-tree TODOs in
png_decompress_chunkand at the zTXt/iTXt call sites have beenasking for.
Design
PNG_USER_CHUNK_TEXT_MAX, default1,000,000, declared in
scripts/pnglibconf/pnglibconf.dfaandpnglibconf.h.prebuilt, next toPNG_USER_CHUNK_MALLOC_MAX.png_structfielduser_chunk_text_max, initialized inpng_create_png_structwith the same fallback ladder asuser_chunk_malloc_max(compile-time default → 64K legacy → SIZE_MAX).png_chunk_text_max(png_ptr)inpngpriv.h,variant-for-variant parallel to
png_chunk_max().png_decompress_chunk()(used only by the zTXt and iTXt handlers)now applies
min(png_chunk_max, png_chunk_text_max). The iCCP pathis untouched and keeps the general limit, as do sPLT/unknown chunks.
The three TODOs recording this plan are removed.
png_set_chunk_text_max()/png_get_chunk_text_max()(pngset.c / pngget.c, declared in png.h, added to
scripts/symbols.def). Setter convention matchespng_set_chunk_malloc_max:0means "no text-specific limit" andstores the system limit, so the general limit still applies.
manuals/libpng-manual.txtandmanuals/libpng.3(prose + synopsis entries).The default of 1,000,000 is a starting point for discussion — it
matches the order of the other user limits (width/height default to
1,000,000) and is one eighth of the general chunk budget. If the
maintainers prefer a different value, it is a one-line change in the
dfa; the mechanism does not depend on it.
Test evidence
New regression test
pngtextlimit(contrib/libtests/pngtextlimit.c,wired into CMake and autotools following the
pngreadendpattern),with three crafted files in
contrib/testpngs/textlimit/:png_set_chunk_text_max(..., 8000000)png_set_chunk_text_max(..., 0)Measured with a minimal reader (peak RSS, before → after): the crafted
zTXt file goes from storing 7,900,000 bytes at 17.2 MB peak RSS to
storing 0 bytes at 11.2 MB peak RSS (the empty-file baseline); the ICC
and normal files are byte-for-byte identical in behaviour. A 9 MB zTXt
(over the old cap) is rejected with the same
zTXt: truncatedbenignwarning before and after — the failure mode is unchanged.
Full suite: 39/39 ctest tests pass on the patched build, including
the new
pngtextlimittest (baseline at the same commit: 38/38).Base commit:
7ef5852db99ec23c1ee8a0638d2ad4111871087c(libpng18).Notes / limitations
pngreadendentry-for-entry but wasvalidated by inspection only (no autoconf/automake on the build VM);
the CMake build and full ctest run are the executed validation.