The vulnerability in sharp stems from multiple critical issues within its libheif dependency (GHSA-g89c-p67h-r497 and GHSA-2jg2-4ch7-h545). The core problem is libheif's incorrect assumption that a HeifPixelImage's logical geometry accurately describes every channel plane it holds, and that each heif_channel is backed by at most one plane. This allowed specially crafted HEIC/HEIF/AVIF files to create inconsistent image states, leading to heap-based buffer overflows and out-of-bounds reads/writes.
Specifically, the GHSA-g89c-p67h-r497 advisory details a heap buffer overflow in HeifPixelImage::scale_nearest_neighbor(). This occurred when a file with nested iden and auxl items caused transfer_channel_from_image_as() to accept duplicate Alpha planes. One Alpha plane would be 8-bit, and a subsequent one 16-bit. When scale_nearest_neighbor() then processed this image, it would allocate an 8-bit buffer (based on the first Alpha plane) but write 16-bit data (from the second Alpha plane), leading to an overflow.
The GHSA-2jg2-4ch7-h545 advisory describes a broader set of 11 vulnerabilities, including out-of-bounds reads and writes, all stemming from the same fundamental flaw in plane size validation. Functions like HeifPixelImage::crop and HeifPixelImage::extract_image_area were also affected because they would perform memory operations based on the image's declared dimensions without verifying that individual planes actually matched those dimensions.
The patches introduce robust validation checks:
HeifPixelImage::transfer_channel_from_image_as now explicitly rejects attempts to add a channel if one already exists for that type, preventing the creation of duplicate Alpha planes.
- The
ImageItem_iden::check_decoded_image_size override, which previously bypassed size validation for iden items, was removed. This ensures that the base class's validation logic is applied, preventing iden items from misrepresenting their dimensions.
- A new helper function,
HeifPixelImage::has_standard_plane_sizes(), was introduced. This function verifies that every stored plane in an image has the expected size for its channel type (e.g., full image size for Y/Alpha, subsampled size for Cb/Cr). This check is now performed by HeifPixelImage::scale_nearest_neighbor, HeifPixelImage::crop, and HeifPixelImage::extract_image_area before they perform memory-intensive operations, preventing OOB access.
When the vulnerability is triggered, a malicious HEIF/AVIF file is parsed. The parsing process would involve calls to ImageItem::decode_image (which might call ImageItem_iden::decode_compressed_image), leading to the creation of an HeifPixelImage with inconsistent plane data due to the lack of checks in transfer_channel_from_image_as and ImageItem_iden::check_decoded_image_size. Subsequently, operations like scaling, cropping, or extracting areas (HeifPixelImage::scale_nearest_neighbor, HeifPixelImage::crop, HeifPixelImage::extract_image_area) would then attempt to process this malformed image, resulting in the heap buffer overflow or out-of-bounds read/write that constitutes the exploitation.