Skip to content
Memory corruption Disclosed report Intermediate

NULL Pointer Dereference in exif_process_user_comment

A crafted JPEG with a JIS-encoded user comment makes exif_read_data pass a NULL encoding pointer into the mbstring converter, which dereferences it and crashes PHP when mbstring is compiled in.

hoangnguyen Follow

Originally disclosed on HackerOne

  • Fixed in 12 days
  • Paid in 12 days
  • Disclosed after 42 days

There is a bug occur in exif_process_user_comment when trying to encode JIS string.

else if (!memcmp(szValuePtr, "JIS\0\0\0\0\0", 8)) {
			/* JIS should be tanslated to MB or we leave it to the user - leave it to the user */
			*pszEncoding = estrdup((const char*)szValuePtr);
			szValuePtr = szValuePtr+8;
			ByteCount -= 8;
			/* XXX this will fail again if encoding_converter returns on error something different than SIZE_MAX   */
			if (zend_multibyte_encoding_converter(
					(unsigned char**)pszInfoPtr,
					&len,
					(unsigned char*)szValuePtr,
					ByteCount,
					zend_multibyte_fetch_encoding(ImageInfo->encode_jis),
					zend_multibyte_fetch_encoding(ImageInfo->motorola_intel ? ImageInfo->decode_jis_be : ImageInfo->decode_jis_le)
					) == (size_t)-1) {
				len = exif_process_string_raw(pszInfoPtr, szValuePtr, ByteCount);
			}
			return len;

As you can see at function call zend_multibyte_fetch_encoding(ImageInfo->encode_jis). At PHP_INI_BEGIN encode_jis was set at empty string so that the result of this call above return NULL and then pass to zend_multibyte_encoding_converter. If this php version is compiled with mbstring, this NULL pointer is passed to mbfl_buffer_converter_new2 through to pointer.

mbfl_buffer_converter_new2(
	const mbfl_encoding *from,
	const mbfl_encoding *to,
    int buf_initsz)
{
    ******SNIP********
	/* initialize */
	convd->from = from;
	convd->to = to;
	/* create convert filter */
	convd->filter1 = NULL;
	convd->filter2 = NULL;
	if (mbfl_convert_filter_get_vtbl(convd->from->no_encoding, convd->to->no_encoding) != NULL) {
    ******SNIP********

Because of none checking 2 pointers from and to so NULL pointer is passed directly to convd->to and result is the crash when calling mbfl_convert_filter_get_vtbl.

Here crash jpeg file : https://drive.google.com/file/d/0B0D1DYQpkA9URnRROVdLdG5jdFE/view?usp=sharing

This bug also works on Mac OS X and Windows.

Test script:

<?php
	$exif = exif_read_data('null.jpg');
	var_dump($exif);
?>

The bug here : https://bugs.php.net/bug.php?id=72618

The original report as published on HackerOne, by hoangnguyen. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the default empty encode_jis setting makes zend_multibyte_fetch_encoding return NULL, and neither the caller nor mbfl_buffer_converter_new2 checks the encoding pointers before dereferencing them.

Impact

A crafted image could crash PHP processes that parse it, giving a denial of service; the text does not claim memory corruption or code execution.

What the attacker needed

PHP built with mbstring; attacker-supplied JPEG parsed by exif_read_data

How it was found

  1. Create a JPEG whose EXIF user comment is marked as JIS-encoded.
  2. Call exif_read_data on that file from PHP compiled with mbstring.
  3. The empty encode_jis setting yields a NULL encoding that reaches the converter.
  4. The converter dereferences the NULL pointer and the process crashes.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

When PHP reads a photo's metadata it sometimes converts text between encodings. Here a setting was left empty, so the converter got a 'nothing' value instead of a real encoding and crashed trying to use it. A malicious image can trigger this.

What is Memory corruption?

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.

More by hoangnguyen

View all

9 more disclosed reports, best first.

More from Internet Bug Bounty

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.