Skip to content

Fix GH-16589: UAF in SplDoublyLinked->serialize() #16611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion ext/spl/spl_dllist.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,13 @@ PHPAPI zend_class_entry *spl_ce_SplStack;
efree(elem); \
}

#define SPL_LLIST_CHECK_DELREF(elem) if ((elem) && !--SPL_LLIST_RC(elem)) { \
#define SPL_LLIST_CHECK_DELREF_EX(elem, on_free) if ((elem) && !--SPL_LLIST_RC(elem)) { \
efree(elem); \
on_free \
}

#define SPL_LLIST_CHECK_DELREF(elem) SPL_LLIST_CHECK_DELREF_EX(elem, ;)

#define SPL_LLIST_ADDREF(elem) SPL_LLIST_RC(elem)++
#define SPL_LLIST_CHECK_ADDREF(elem) if (elem) SPL_LLIST_RC(elem)++

Expand Down Expand Up @@ -1024,8 +1027,12 @@ PHP_METHOD(SplDoublyLinkedList, serialize)
smart_str_appendc(&buf, ':');
next = current->next;

SPL_LLIST_CHECK_ADDREF(next);

php_var_serialize(&buf, &current->data, &var_hash);

SPL_LLIST_CHECK_DELREF_EX(next, break;);

current = next;
}

Expand Down
23 changes: 23 additions & 0 deletions ext/spl/tests/gh16589.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--TEST--
GH-16589 (UAF in SplDoublyLinked->serialize())
--CREDITS--
chibinz
--FILE--
<?php

class C {
function __serialize(): array {
global $list;
$list->pop();
return [];
}
}

$list = new SplDoublyLinkedList;
$list->add(0, new C);
$list->add(1, 1);
var_dump($list->serialize());

?>
--EXPECT--
string(17) "i:0;:O:1:"C":0:{}"
Loading