Skip to content
Memory corruption Disclosed report Advanced

Out of bound when verify signature of zip phar in phar_parse_zipfile

The phar_parse_zipfile function did not validate the size of the signature entry before using it, allowing an attacker to supply a ZIP file with a signature smaller than 8 bytes. This caused an integer underflow when calculating the signature length, leading to a heap out-of-bounds read during signature verification.

hoangnguyen Follow

Originally disclosed on HackerOne

  • Fixed in 7 days
  • Paid in 7 days
  • Disclosed after 3.2 years

https://bugs.php.net/bug.php?id=72928

There was a security code in phar_parse_zipfile

sig = (char *) emalloc(entry.uncompressed_filesize);
read = php_stream_read(fp, sig, entry.uncompressed_filesize);
if (read != entry.uncompressed_filesize) {
	php_stream_close(sigfile);
	efree(sig);
	PHAR_ZIP_FAIL("signature cannot be read");
}
mydata->sig_flags = PHAR_GET_32(sig);
if (FAILURE == phar_verify_signature(sigfile,
	php_stream_tell(sigfile),
	mydata->sig_flags,
	sig + 8,
	entry.uncompressed_filesize - 8,
	fname,
	&mydata->signature,
	&mydata->sig_len,
	error)
	) {

There are no checking entry.uncompressed_filesize attacker can create a signature.bin with size less than 8 and then this value is passed to phar_verify_signature as sig_len as you can see entry.uncompressed_filesize - 8 as result sig_len is overflow. And the third param is sig buffer as you can see sig + 8, because entry.uncompressed_filesize is less than 8 by default emalloc will return 16 bytes this result may lead to heap out of bound.

phar zip file : https://drive.google.com/file/d/0B0D1DYQpkA9Ud3I2OFlfeFRqbEU/view?usp=sharing

Test script:

<?php
	$phar = new PharData('phars/signature.zip');
	var_dump($phar);
?>

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 missing check that the signature entry's uncompressed size is at least 8 bytes before subtracting 8 and using the result as a length.

Impact

An attacker could trigger a heap out-of-bounds condition in the signature buffer.

What the attacker needed

Ability to supply a crafted ZIP file

How it was found

  1. Create a ZIP file containing a signature entry with a size less than 8 bytes.
  2. Open the ZIP file using the PharData class in PHP.

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.

A PHP feature for reading ZIP-based archives (phar) reads a digital signature to check integrity. The code assumed the signature was always at least 8 bytes long, but an attacker could make it shorter. When the code subtracted 8 from that small size, it got a huge number, causing it to read past the end of a memory buffer.

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.