Background

psd-tools is a Python library for reading and writing Adobe Photoshop files (PSD and PSB). It exposes the layers, masks, text and other contents of a document, and can convert images from it into Pillow or NumPy objects for further processing. Any application that accepts PSD files from users and converts them with this library is a potential target.

A PSD can include patterns, which are small images Photoshop repeats to fill a shape. A pattern record declares a rectangle (its width and height in pixels) followed by the pixel data for each color channel.

Terms used below:

  • Pillow is the standard Python imaging library and the usual target for image conversion.
  • Channel is one layer of color data, such as red, green or blue.
  • Allocation is when a program asks the operating system for memory. An image library normally allocates the full width times height up front.

Summary

A small PSD can declare a pattern rectangle far larger than its actual image data. The Pillow conversion path trusts those dimensions and allocates the image before discovering that the channel data is incomplete. In the proof of concept, a 157-byte file caused a peak memory increase of roughly 4,098 MB before conversion failed.

The NumPy conversion path rejected the same dimensions before allocating. The public Pillow helper therefore provided a separate entry point around checks already present elsewhere. An upload service using this helper could spend gigabytes of memory on a file that easily passes ordinary upload-size limits.

How the Vulnerability Works

File parsers have a recurring weakness. A file describes itself: “my image is this wide and this tall”. The parser can believe those numbers and reserve memory to match, or it can check them against what the file actually contains and against a sensible limit. A file only a few bytes long can claim to hold a very large image.

In this case the claim is a pattern of 65,536 x 65,536 pixels. That is about 4.3 billion pixels, which is roughly 4 GB for a single 8-bit channel. The test file was 157 bytes.

The library’s convert_pattern_to_pil helper reads the rectangle, then for each channel asks Pillow to create an image of that size. Pillow reserves the memory at once. Only afterwards does the code notice that the channel data is far too short and raise an error. The error is correct, but the memory has already been used. A worker process with a smaller memory limit can be killed by the operating system before the Python exception is reached, so the application never gets to handle the failure.

Declared pattern rectangle:   65536 x 65536
Actual pixel data in file:    a few bytes
Library does:                 allocate ~4 GB  ->  discover data is missing  ->  raise error
Should do:                    check size  ->  refuse before allocating

The library already had the right check. A guard named check_pixel_size enforces the PSD format’s maximum of 30,000 pixels per side, and the NumPy conversion path uses it. The Pillow path was a second public door that skipped it. Earlier advisories had fixed similar problems in other entry points, and this public helper was one they had not covered.

An upload service that converts user-supplied PSD files can therefore be exhausted by one tiny file, well under any file-size limit. This is uncontrolled resource consumption (CWE-789).

Prerequisites

The application must process attacker-controlled PSD data through convert_pattern_to_pil. Opening a document or using another conversion path does not, by itself, establish that this allocation is reached.

The library imposes no account requirement. Remote reachability depends on the application exposing the conversion. The relevant resource limit is the processing worker’s memory budget, not just the number of bytes accepted by its upload handler.

Attack Path

Attack route
Attacker routeMitigationCtrl + scroll to zoom
  1. START
    Attacker supplies a small PSD
  2. STEP 01
    Declare an oversized pattern rectangle

    The record claims a 65536 by 65536 pattern with very little channel data.

    MITIGATION Validate dimensions and memory budget before conversion

  3. STEP 02
    Application invokes the Pillow pattern helper
  4. STEP 03
    Image allocation consumes gigabytes

    MITIGATION Isolate document workers with memory limits

  5. STEP 04
    Conversion fails after the allocation

Root Cause

The helper reads pattern.data.rectangle, calculates width and height, then sends the result to _create_image for each written channel. Pillow constructs the image from those declared dimensions before the available channel bytes are checked.

The test rectangle was 65536x65536. Rejecting incomplete data after allocation does not prevent transient memory pressure. A worker can hit its memory limit before the Python exception is reached.

The neighboring NumPy implementation calls check_pixel_size and rejects the same rectangle. Earlier allocation and thumbnail fixes did not cover every conversion entry point. The check must run before the Pillow helper allocates, including when a caller obtained the pattern through a supported parsing API.

Validation

The proof of concept used psd-tools 1.23.0 at revision 9e706d6. It generated the small document, ran both conversion paths and measured peak resident memory.

ConversionExpected boundaryRecorded result
NumPy pattern conversionReject dimensions before allocationDimension guard rejected the file
Pillow pattern conversionApply an equivalent allocation checkAbout 4,098 MB peak memory increase
Pillow conversion after allocationReject insufficient channel dataValueError reporting insufficient image data

Two consecutive runs recorded the allocation followed by the exception. One captured run took about 2.6 seconds. These measurements describe that test machine, not a guaranteed memory or timing value for every consumer.

Impact

Repeated conversions can exhaust a worker’s memory, cause swapping or terminate an upload-processing service. The small file size does not constrain an allocation driven by document metadata.

No confidentiality or integrity effect was established. Overview uses the published GitHub score of 5.9 under CVSS v3.1. The practical exposure depends on whether the consuming application calls this helper on untrusted patterns.

Remediation

Upgrade to 1.23.1 or later. The release includes pre-allocation pattern checks and further bounds for channel sizes and conversion memory. PR #960 contains the related changes.

Keep document workers under explicit memory and execution-time limits as a second boundary. Test the same malformed pattern through each supported conversion API and confirm that rejection happens before a large image is constructed. Upload-size limits alone cannot enforce this property.

References