CVE-2022-0137 is a vulnerability discovered in HTMLDOC, a widely-used software program for converting HTML documents into indexed HTML, PDF, and PostScript files. Specifically, the vulnerability arises from a heap buffer overflow in the image_set_mask function of the program. When exploited, an attacker can write data outside the buffer boundaries, potentially leading to a system crash or the execution of arbitrary code.

In this long-read post, we will delve into the details of the vulnerability, examine the exploitable code snippet, and provide relevant reference links to understand the exploit better. Before proceeding, it's essential to note that this vulnerability affects HTMLDOC versions prior to 1.9.15.

The Vulnerability: Heap Buffer Overflow in image_set_mask Function

HTMLDOC uses the image_set_mask function to handle image masks, which are used to provide transparency effects in images. The vulnerability occurs when an attacker is able to provide an image with a crafted mask, causing the program to write data beyond the buffer's allocated memory. This, in turn, triggers a heap buffer overflow that can be exploited to cause a range of consequences, such as data corruption, information disclosure, or arbitrary code execution.

Here is the code snippet from HTMLDOC version 1.9.14 (the version before the fix) that demonstrates the issue:

static void
image_set_mask(image *img,	/* I - Image to set mask in */
	       int left,		/* I - Left position */
	       int  top,		/* I - Top position */
	       int  right,		/* I - Right position */
	       int  bottom)	/* I - Bottle position */
{
  unsigned	i, j;		/* Looping vars */
  uchar		*maskptr,	/* Pointer to mask image */
		*ptr;		/* Pointer to image */

  /* Range check input */
  if (left < )
    left = ;
  if (left >= img->width)
    return;

  if (top < )
    top = ;
  if (top >= img->height)
    return;

  if (right < )
    right = ;
  if (right >= img->width)
    return;

  if (bottom < )
    bottom = ;
  if (bottom >= img->height)
    return;

  /* Allocate memory for the mask */
  img->mask = (uchar *)calloc(sizeof(uchar), img->width * img->height);

  /* Now copy bits in the mask */
  for (j = (unsigned)top, maskptr = img->mask + top * img->width + left, ptr = img->pixels + top * img->width * 3;
       j < (unsigned)bottom;
       j ++, maskptr += img->width - (right - left), ptr += img->width * 3)
    for (i = (unsigned)left; i < (unsigned)right; i ++, maskptr ++, ptr += 3)
      *maskptr = (255 * ptr[] + 255 * ptr[1] + 255 * ptr[2]) / (3 * 255);
}

In this code, the program reads the image mask provided by the attacker and processes it using the image_set_mask function. However, the function does not properly validate the boundaries of the image mask, allowing an attacker to provide a specially crafted image that triggers a heap buffer overflow.

Changelog entry for the fixed version (1.9.15)

CHANGES IN HTMLDOC v1.9.15

1. Security: Fixed a heap buffer overflow in the image_set_mask() function
   used for image masks (Issue #419)

The issue was resolved in the subsequent version of HTMLDOC, version 1.9.15, which includes proper boundary checks and handling for the image mask.

Exploit Details

An attacker may exploit the vulnerability by including crafted images that trigger a heap buffer overflow in a target's system software running HTMLDOC version prior to 1.9.15. This may lead to a denial of service (leading to a system crash), information disclosure, or arbitrary code execution.

It is crucial for developers and users to update their systems to HTMLDOC 1.9.15 to fix this vulnerability.

Relevant References

1. The affected software, HTMLDOC: https://www.msweet.org/htmldoc
2. The HTMLDOC Github Repository: https://github.com/michaelrsweet/htmldoc
3. The Issue Reporting the Vulnerability: https://github.com/michaelrsweet/htmldoc/issues/419
4. The Commit Fixing the Vulnerability: https://github.com/michaelrsweet/htmldoc/commit/61a771cba8e34d409619ff99e142ea3d965bf461
5. The CVE Details of the Vulnerability: https://nvd.nist.gov/vuln/detail/CVE-2022-0137

Conclusion

In conclusion, CVE-2022-0137 is a heap buffer overflow vulnerability affecting the image_set_mask function in HTMLDOC versions prior to 1.9.15. By exploiting this vulnerability, an attacker can cause denial of service, information disclosure, or arbitrary code execution on a target's system. To protect your system or application, it is important to update HTMLDOC to version 1.9.15 or later.

Timeline

Published on: 11/14/2022 18:15:00 UTC
Last modified on: 02/02/2023 18:31:00 UTC