Skip to content

fix(psd): fixes against corrupt files with better validation#5089

Open
lgritz wants to merge 1 commit intoAcademySoftwareFoundation:mainfrom
lgritz:lg-psdfix
Open

fix(psd): fixes against corrupt files with better validation#5089
lgritz wants to merge 1 commit intoAcademySoftwareFoundation:mainfrom
lgritz:lg-psdfix

Conversation

@lgritz
Copy link
Collaborator

@lgritz lgritz commented Mar 13, 2026

  1. Validate channel count against color mode to prevent heap buffer
    overflow.

  2. Corrupted PSD files can declare a color mode (e.g. RGB, needing 3
    channels) with transparency (needing +1) but report fewer channels
    in the header. This caused an out-of-bounds read in
    read_native_scanline when setup() built channel pointer arrays
    using mode_channel_count, which exceeded the actual
    m_image_data.channel_info size.

  3. Additional buffer overflow protection, error checking and propagation.

  4. Avoid leak by properly freeing z_stream resources in decompress_zip.

Used Claude Code / Opus 4.6 to help narrow down and confirm the bugs 1 and 2.

Assisted-by: Claude Opus 4.6 noreply@anthropic.com

1. Validate channel count against color mode to prevent heap buffer
   overflow.

2. Corrupted PSD files can declare a color mode (e.g. RGB, needing 3
   channels) with transparency (needing +1) but report fewer channels
   in the header.  This caused an out-of-bounds read in
   read_native_scanline when setup() built channel pointer arrays
   using mode_channel_count, which exceeded the actual
   m_image_data.channel_info size.

3. Additional buffer overflow protection, error checking and
   propragation.

4. Avoid leak by properly freeing z_stream resources in decompress_zip

Used Claude Code / Opus 4.6 to help narrow down and confirm the bugs
1 and 2.

Signed-off-by: Larry Gritz <lg@larrygritz.com>
Assisted-by: Claude Opus 4.6 <noreply@anthropic.com>
@lgritz
Copy link
Collaborator Author

lgritz commented Mar 14, 2026

Amended with more PSD fixes. In its final form now.

Copy link
Contributor

@jessey-git jessey-git left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes look alright. As for additional concerns, it looks like PSD v2 files (Large Document Format) allow widths/heights up to 300'000 which will overflow in some places (I think just the ones that do row_length * height)

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