Skip to content

Commit 2ff496e

Browse files
committed
Handle resource table reallocation during shutdown
New resources may be created while closing resources during shutdown. This may result in a reallocation of arData and use after free. This problem was exposed by 7f7a90b, which creates one resources less, and thus moved the reallocation to shutdown for a number of existing tests. However, the general problem already existed previously. We don't try to also close the newly added resources -- we will later perform a graceful reverse destroy of the table, which will catch any remaining cases.
1 parent 05a2179 commit 2ff496e

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

Zend/zend_list.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -213,13 +213,17 @@ void zend_init_rsrc_plist(void)
213213

214214
void zend_close_rsrc_list(HashTable *ht)
215215
{
216-
zend_resource *res;
217-
218-
ZEND_HASH_REVERSE_FOREACH_PTR(ht, res) {
219-
if (res->type >= 0) {
220-
zend_resource_dtor(res);
216+
/* Reload ht->arData on each iteration, as it may be reallocated. */
217+
uint32_t i = ht->nNumUsed;
218+
while (i-- > 0) {
219+
Bucket *p = &ht->arData[i];
220+
if (Z_TYPE(p->val) != IS_UNDEF) {
221+
zend_resource *res = Z_PTR(p->val);
222+
if (res->type >= 0) {
223+
zend_resource_dtor(res);
224+
}
221225
}
222-
} ZEND_HASH_FOREACH_END();
226+
}
223227
}
224228

225229

0 commit comments

Comments
 (0)