Skip to content
Memory corruption Disclosed report Advanced

Out of bound when verify signature of tar phar in phar_parse_tarfile

The phar_parse_tarfile function in PHP did not validate the uncompressed size of a tar entry before using it to calculate a signature length, allowing an attacker to create a tar-based phar with a signature file smaller than 8 bytes. This could cause an integer underflow, leading to a heap out-of-bounds read when verifying the signature.

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=73035 There was a security code in phar_parse_tarfile

if (FAILURE == phar_verify_signature(fp, php_stream_tell(fp) - size - 512, myphar->sig_flags, buf + 8, size - 8, fname, &myphar->signature, &myphar->sig_len, error)) {
	if (error) {
		char *save = *error;
		spprintf(error, 4096, "phar error: tar-based phar \"%s\" signature cannot be verified: %s", fname, save);
		efree(save);
		}
	goto bail;
}

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.

tarfile : https://drive.google.com/open?id=0B0D1DYQpkA9UVkV1Q0tTeGpVYk0

Test script:

<?php
	$phar = new PharData('phars/signature.tar');
	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

Impact

This may lead to a heap out-of-bounds read.

What the attacker needed

Ability to supply a crafted tar-based phar file

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 tar-based phar file can contain a signature file. The code that reads this signature did not check if the signature file was at least 8 bytes long. If an attacker makes a very short signature file, the program calculates a negative length and then reads memory outside the intended buffer, which can crash the program or leak data.

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.