sharp icon indicating copy to clipboard operation
sharp copied to clipboard

Hardware Integer Divide-by-Zero (`SIGFPE`) Remote DoS in `PipelineWorker`

Open codeCraft-Ritik opened this issue 1 month ago • 1 comments

  • Severity: 🔴 Critical (Process Crash / Denial of Service)
  • Target File: src/pipeline.cc
  • Affected Lines: Lines 326–346, 554–560, 580–585, 631–636, 681–685

Description & Root Cause

In src/pipeline.cc, sharp decodes gain map metadata for Ultra HDR (UHDR) images and assigns gainMapScaleFactor:

VImage gainMap;
int gainMapScaleFactor = 1;
if (sharp::HasGainMap(image)) {
  if (baton->keepGainMap) {
    gainMap = image.gainmap().copy_memory();
    if (image.get_typeof("gainmap-scale-factor") == G_TYPE_INT) {
      gainMapScaleFactor = image.get_int("gainmap-scale-factor");
    }

In subsequent operations (cropping, resizing, embedding, pre/post extracting):

if (baton->topOffsetPre != -1) {
  gainMap = gainMap.extract_area(
    baton->leftOffsetPre / gainMapScaleFactor, baton->topOffsetPre / gainMapScaleFactor,
    baton->widthPre / gainMapScaleFactor, baton->heightPre / gainMapScaleFactor);
}

There is no validation verifying that gainMapScaleFactor > 0. If an image carries a gainmap-scale-factor of 0 or negative (which can be embedded in untrusted or corrupted JPEG/UHDR files):

  • Integer division leftOffset / 0 triggers an immediate hardware integer division-by-zero fault (SIGFPE on Linux/macOS, STATUS_INTEGER_DIVIDE_BY_ZERO on Windows).
  • Because CPU hardware faults are not C++ exceptions, they cannot be caught by catch (std::runtime_error const &err).
  • The Node.js process terminates immediately without an error callback, crashing web servers processing user-uploaded images.

Recommended Fix

In src/pipeline.cc:

if (image.get_typeof("gainmap-scale-factor") == G_TYPE_INT) {
  int const scaleFactor = image.get_int("gainmap-scale-factor");
  if (scaleFactor > 0) {
    gainMapScaleFactor = scaleFactor;
  }
}

codeCraft-Ritik avatar Oct 11 '26 04:10 codeCraft-Ritik

libvips guarantees that if gainmap-scale-factor is set then it is an integer with a minimum value of 1.

https://github.com/libvips/libvips/blob/6b5dcce25fbf2642647bc180eb19c35de864eb4a/libvips/foreign/uhdrload.c#L476-L477

lovell avatar Oct 11 '26 10:10 lovell