b74969
From 22882a9d89712ff2b6ebc20a689a89452bba4dcd Mon Sep 17 00:00:00 2001
b74969
From: Xinchen Hui <laruence@php.net>
b74969
Date: Wed, 2 Jul 2014 17:57:42 +0800
b74969
Subject: [PATCH] Fixed bug #67539 (ArrayIterator use-after-free due to object
b74969
 change during sorting)
b74969
b74969
---
b74969
 NEWS                        |  2 ++
b74969
 ext/spl/spl_array.c         |  7 +++++++
b74969
 ext/spl/tests/bug67539.phpt | 15 +++++++++++++++
b74969
 3 files changed, 24 insertions(+)
b74969
 create mode 100644 ext/spl/tests/bug67539.phpt
b74969
b74969
diff --git a/ext/spl/spl_array.c b/ext/spl/spl_array.c
b74969
index 8392e72..0fe47b6 100644
b74969
--- a/ext/spl/spl_array.c
b74969
+++ b/ext/spl/spl_array.c
b74969
@@ -1738,6 +1738,7 @@ SPL_METHOD(Array, unserialize)
b74969
 	const unsigned char *p, *s;
b74969
 	php_unserialize_data_t var_hash;
b74969
 	zval *pmembers, *pflags = NULL;
b74969
+	HashTable *aht;
b74969
 	long flags;
b74969
 	
b74969
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &buf, &buf_len) == FAILURE) {
b74969
@@ -1749,6 +1750,12 @@ SPL_METHOD(Array, unserialize)
b74969
 		return;
b74969
 	}
b74969
 
b74969
+	aht = spl_array_get_hash_table(intern, 0 TSRMLS_CC);
b74969
+	if (aht->nApplyCount > 0) {
b74969
+		zend_error(E_WARNING, "Modification of ArrayObject during sorting is prohibited");
b74969
+		return;
b74969
+	}
b74969
+
b74969
 	/* storage */
b74969
 	s = p = (const unsigned char*)buf;
b74969
 	PHP_VAR_UNSERIALIZE_INIT(var_hash);
b74969
diff --git a/ext/spl/tests/bug67539.phpt b/ext/spl/tests/bug67539.phpt
b74969
new file mode 100644
b74969
index 0000000..8bab2a8
b74969
--- /dev/null
b74969
+++ b/ext/spl/tests/bug67539.phpt
b74969
@@ -0,0 +1,15 @@
b74969
+--TEST--
b74969
+Bug #67539 (ArrayIterator use-after-free due to object change during sorting)
b74969
+--FILE--
b74969
+
b74969
+
b74969
+$it = new ArrayIterator(array_fill(0,2,'X'), 1 );
b74969
+
b74969
+function badsort($a, $b) {
b74969
+        $GLOBALS['it']->unserialize($GLOBALS['it']->serialize());
b74969
+        return TRUE;
b74969
+}
b74969
+
b74969
+$it->uksort('badsort');
b74969
+--EXPECTF--
b74969
+Warning: Modification of ArrayObject during sorting is prohibited in %sbug67539.php on line %d
b74969
-- 
b74969
1.9.2
b74969