Skip to content

pngset: Release the previous pCAL/sCAL data when png_set_pCAL and png_set_sCAL_s are called again - #987

Open
takerusaito4187 wants to merge 1 commit into
pnggroup:libpng18from
takerusaito4187:pngset-pcal-scal-leak-fix
Open

takerusaito4187 wants to merge 1 commit into
pnggroup:libpng18from
takerusaito4187:pngset-pcal-scal-leak-fix

Conversation

@takerusaito4187

Copy link
Copy Markdown

Summary

png_set_pCAL and png_set_sCAL_s overwrite the pointers stored in the
info struct with freshly allocated buffers without releasing the previous
allocations. Calling either function twice on the same info struct leaks
the first call's allocations: for pCAL the purpose string, the units
string, the parameter array and each parameter string; for sCAL the width
and height strings.

This change makes both functions release the previous data, so repeated
calls replace the stored values instead of leaking them.

How I found it

I audited all 46 png_set_* functions defined in pngset.c for the
pattern "allocate a replacement value and overwrite the stored pointer
without releasing the old one". The heap-owning setters fall into three
groups:

  • Replace-style setters that already handle this correctly:
    png_set_PLTE, png_set_tRNS and png_set_hIST snapshot the caller's
    data, release the old storage with png_free_data, then copy;
    png_set_iCCP and png_set_eXIf_1 allocate and fill the replacement
    first and release the old storage afterwards.
  • Append-style setters (png_set_text_2, png_set_sPLT,
    png_set_unknown_chunks) grow their arrays and defer releasing the old
    array until after the copy.
  • png_set_pCAL and png_set_sCAL_s were the only two that overwrote
    the stored pointers with no release at all.

The remaining setters store scalars or caller-owned pointers and do not
own heap data in the info struct.

The change

Both functions now follow the order already used by png_set_iCCP and
png_set_eXIf_1:

  1. Allocate and fill the replacement values in local variables.
  2. Release the previously stored data with
    png_free_data(..., PNG_FREE_PCAL / PNG_FREE_SCAL, 0).
  3. Store the new values and set the valid flag.

Consequences beyond fixing the leak:

  • If an allocation fails, the previously stored data is left intact
    (previously a failed call could leave a partially overwritten info
    struct). This matches the failure behaviour of png_set_iCCP and
    png_set_eXIf_1.
  • Passing values obtained from png_get_pCAL / png_get_sCAL_s back
    into the setters (getter-to-setter aliasing) is safe, because the old
    storage is released only after the copies are made. In png_set_pCAL
    the release happens before pcal_nparams is overwritten, since
    png_free_data releases the parameter strings using the stored count
    of the data being replaced.

No public API or ABI changes: only the bodies of the two functions in
pngset.c are modified. png_set_sCAL and png_set_sCAL_fixed are
covered because they delegate to png_set_sCAL_s.

The affected code is identical in the libpng16 branch (verified against
v1.6.59), so the change should apply there unchanged; I am happy to
retarget or provide a second PR if preferred.

Impact and scope

This is a robustness fix with low severity, stated plainly:

  • The leak is not reachable from file input. A PNG file with
    duplicate pCAL or sCAL chunks is rejected by the chunk handlers
    ("pCAL: duplicate" / "sCAL: duplicate"), so the setters are not called
    twice when reading. I verified this dynamically.
  • The leak occurs in applications that call png_set_pCAL or
    png_set_sCAL_s (or the png_set_sCAL wrappers) more than once on
    the same info struct, e.g. when reusing an info struct on the write
    side. Each repeated call leaks the previous chunk's strings.

Testing

All testing was done locally on x86_64 with an ASan+UBSan build
(-fno-sanitize-recover=all):

  • A small reproducer that calls png_set_pCAL twice and
    png_set_sCAL_s twice on one info struct reported 48 bytes leaked in
    6 allocations under LeakSanitizer before the change (allocation sites
    in png_set_pCAL and png_set_sCAL_s). After the change the same
    program exits cleanly with no leak report.
  • A functional check confirms the getters return the most recent values
    after repeated calls, including when the pCAL parameter count changes
    between calls (3 -> 1 -> 3), and when getter results are passed back
    into the setters.
  • The full existing test suite passes: ctest 39/39.

Happy to adjust the approach or add a regression test in the project's
preferred form if the maintainers would like one.

png_set_pCAL and png_set_sCAL_s overwrote the pointers stored in the info struct with freshly allocated buffers without releasing the previous allocations, leaking the first call's strings and parameter array when either function was called twice on the same info struct.

Follow the order already used by png_set_iCCP and png_set_eXIf_1: allocate and fill the replacement values in locals, release the old data with png_free_data, then store. This also leaves the previously stored data intact if an allocation fails, and makes getter-to-setter aliasing safe.
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.

1 participant