Skip to content

fix: Limit the decompressed size of text chunks separately - #982

Open
Dimaa099 wants to merge 1 commit into
pnggroup:libpng18from
Dimaa099:limit-text-chunk-decompression
Open

Dimaa099 wants to merge 1 commit into
pnggroup:libpng18from
Dimaa099:limit-text-chunk-decompression

Conversation

@Dimaa099

Copy link
Copy Markdown

Motivation

png_decompress_chunk() caps every decompressed chunk at the single
application-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_chunk and at the zTXt/iTXt call sites have been
asking for.

Design

  • New compile-time setting PNG_USER_CHUNK_TEXT_MAX, default
    1,000,000, declared in scripts/pnglibconf/pnglibconf.dfa and
    pnglibconf.h.prebuilt, next to PNG_USER_CHUNK_MALLOC_MAX.
  • New png_struct field user_chunk_text_max, initialized in
    png_create_png_struct with the same fallback ladder as
    user_chunk_malloc_max (compile-time default → 64K legacy → SIZE_MAX).
  • New internal helper png_chunk_text_max(png_ptr) in pngpriv.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 path
    is untouched and keeps the general limit, as do sPLT/unknown chunks.
    The three TODOs recording this plan are removed.
  • New public API, mirroring the 1.4.1 pair exactly:
    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 matches
    png_set_chunk_malloc_max: 0 means "no text-specific limit" and
    stores the system limit, so the general limit still applies.
  • Documentation: new section in manuals/libpng-manual.txt and
    manuals/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 pngreadend pattern),
with three crafted files in contrib/testpngs/textlimit/:

Scenario Result
zTXt → 7,900,000 B, default limits ignored, 0 bytes stored
same, png_set_chunk_text_max(..., 8000000) stored in full; getter round-trips
same, png_set_chunk_text_max(..., 0) stored in full (general limit governs)
iCCP profile 2,097,152 B, default limits stored in full (not a text chunk)
small tEXt + zTXt (16 B), default limits stored unchanged

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: truncated benign
warning before and after — the failure mode is unchanged.

Full suite: 39/39 ctest tests pass on the patched build, including
the new pngtextlimit test (baseline at the same commit: 38/38).
Base commit: 7ef5852db99ec23c1ee8a0638d2ad4111871087c (libpng18).

Notes / limitations

  • The autotools wiring mirrors pngreadend entry-for-entry but was
    validated by inspection only (no autoconf/automake on the build VM);
    the CMake build and full ctest run are the executed validation.
  • No CHANGES entry: release notes are maintainer-generated.

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.
@jbowler

jbowler commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

I think you are misunderstanding the code:

png_decompress_chunk() caps every decompressed chunk at the single
application-level limit png_chunk_max() (default 8,000,000 bytes).
That budget exists because ICC profiles legitimately run to megabytes

Notice that, as you correctly observe, this is png_chunk_max. This is not a size limit on decompressed data, it is a size limit on uncompressed data.

libpng stores known chunks in a completely uncompressed form regardless of the chunk. So that limit applies to tEXt chunks; it happens in png_read_buffer. Failing to apply the same limit to any compressed chunk that is stored in its decompressed form (i.e. a known chunk) by libpng would certainly break the contract.

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.

@Dimaa099

Copy link
Copy Markdown
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants