Skip to content

Differential Handling of Out-of-Range Palette Indices in PNG Decoders #822

Description

@fasrm

Summary

This research demonstrates that a paletted PNG image containing a reconstructed palette index outside the declared PLTE range is accepted by multiple PNG decoders but produces different pixel outputs depending on the implementation.

Specifically:

  • A PNG image declares 255 palette entries (PLTE).
  • The reconstructed scanline contains palette index 255, which is outside the valid range 0–254.
  • The image is accepted by both libpng and ImageMagick without error.
  • The resulting decoded pixel differs across implementations.
Decoder Output Pixel
libpng (0,0,0)
ImageMagick (255,255,255)

Both decoders:

  • accept the PNG
  • emit no error
  • produce valid output

But the resulting pixel differs.

Sentinel Experiment

To test for potential out-of-bounds palette access, the last palette entry was replaced with:

(1,2,3)

Modified palette tail:

..., (6,145,1), (1,2,3)

After decoding:

libpng

first_pixel=0,0,0

ImageMagick

(255,255,255)

This indicates the pixel value is not derived from the adjacent palette entry, suggesting internal fallback handling.

Security Analysis

The behavior indicates:

invalid palette index accepted
decoder-dependent resolution

However, the experiment did not demonstrate:

memory corruption
heap overflow
out-of-bounds read
crash

Therefore the issue is best classified as:

Parser robustness issue
Specification compliance divergence

Possible CWE classifications:

CWE-20 Improper Input Validation
CWE-754 Improper Check for Exceptional Conditions

Impact

Practical impact is limited to:

inconsistent rendering across implementations

Potential implications:

  • image processing inconsistencies
  • undefined behavior in downstream pipelines
  • subtle visual discrepancies

No direct exploit scenario was identified.

Recommendations

PNG decoders should explicitly validate palette indices.

Recommended check:

if palette_index >= palette_entries:
    return error

Alternatively, emit a warning:

libpng warning: palette index out of range

Explicit validation improves:

decoder consistency
spec compliance
defensive parsing

References

PNG Specification:

https://www.w3.org/TR/PNG/

libpng project:

https://libpng.org/pub/png/libpng.html

pngcheck:

http://www.libpng.org/pub/png/apps/pngcheck.html

ImageMagick:

https://imagemagick.org

Conclusion

This research demonstrates that out-of-range palette indices reconstructed during PNG scanline decoding are accepted silently by multiple decoders but resolved differently across implementations.

Although no exploitable memory issue was identified, the findings highlight inconsistent parser behavior and incomplete validation of semantically invalid palette indices, which may warrant further investigation in PNG decoding pipelines.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions