Skip to content

Commit 123cc27

Browse files
committed
Fix GH-22046: The unserialize function with Uri\WhatWg\Url leads to NULL pointer dereference when object serialized back
The "C" serialization format is explicitly disabled.
1 parent 61e679d commit 123cc27

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

ext/uri/php_uri.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,13 @@ PHPAPI zend_object *php_uri_object_handler_clone(zend_object *object)
10941094
return &new_uri_object->std;
10951095
}
10961096

1097+
PHPAPI int php_uri_object_handler_unserialize(zval *object, zend_class_entry *ce, const unsigned char *buf, size_t buf_len, zend_unserialize_data *data)
1098+
{
1099+
zend_throw_exception_ex(NULL, 0, "Unserialization of %s using the \"C\" format is unsupported", ZSTR_VAL(ce->name));
1100+
1101+
return FAILURE;
1102+
}
1103+
10971104
PHPAPI zend_result php_uri_parser_register(const php_uri_parser *uri_parser)
10981105
{
10991106
zend_string *key = zend_string_init_interned(uri_parser->name, strlen(uri_parser->name), true);
@@ -1116,6 +1123,7 @@ static PHP_MINIT_FUNCTION(uri)
11161123
php_uri_ce_rfc3986_uri = register_class_Uri_Rfc3986_Uri();
11171124
php_uri_ce_rfc3986_uri->create_object = php_uri_object_create_rfc3986;
11181125
php_uri_ce_rfc3986_uri->default_object_handlers = &object_handlers_rfc3986_uri;
1126+
php_uri_ce_rfc3986_uri->unserialize = &php_uri_object_handler_unserialize;
11191127
memcpy(&object_handlers_rfc3986_uri, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
11201128
object_handlers_rfc3986_uri.offset = offsetof(php_uri_object, std);
11211129
object_handlers_rfc3986_uri.free_obj = php_uri_object_handler_free;
@@ -1127,6 +1135,7 @@ static PHP_MINIT_FUNCTION(uri)
11271135
php_uri_ce_whatwg_url = register_class_Uri_WhatWg_Url();
11281136
php_uri_ce_whatwg_url->create_object = php_uri_object_create_whatwg;
11291137
php_uri_ce_whatwg_url->default_object_handlers = &object_handlers_whatwg_uri;
1138+
php_uri_ce_whatwg_url->unserialize = &php_uri_object_handler_unserialize;
11301139
memcpy(&object_handlers_whatwg_uri, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
11311140
object_handlers_whatwg_uri.offset = offsetof(php_uri_object, std);
11321141
object_handlers_whatwg_uri.free_obj = php_uri_object_handler_free;
@@ -1136,6 +1145,7 @@ static PHP_MINIT_FUNCTION(uri)
11361145
php_uri_ce_exception = register_class_Uri_UriException(zend_ce_exception);
11371146
php_uri_ce_error = register_class_Uri_UriError(zend_ce_error);
11381147
php_uri_ce_invalid_uri_exception = register_class_Uri_InvalidUriException(php_uri_ce_exception);
1148+
php_uri_ce_invalid_uri_exception->unserialize = &php_uri_object_handler_unserialize;
11391149
php_uri_ce_whatwg_invalid_url_exception = register_class_Uri_WhatWg_InvalidUrlException(php_uri_ce_invalid_uri_exception);
11401150
php_uri_ce_whatwg_url_host_type = register_class_Uri_WhatWg_UrlHostType();
11411151
php_uri_ce_whatwg_url_validation_error = register_class_Uri_WhatWg_UrlValidationError();

ext/uri/tests/gh22046.phpt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
GH-22046: The unserialize function with Uri\WhatWg\Url leads to NULL pointer dereference when object serialized back
3+
--FILE--
4+
<?php
5+
6+
$payload = 'C:14:"Uri\WhatWg\Url":0:{}';
7+
try {
8+
unserialize($payload);
9+
} catch (Throwable $e) {
10+
echo $e::class, ": ", $e->getMessage(), PHP_EOL;
11+
}
12+
13+
$payload = 'C:15:"Uri\Rfc3986\Uri":0:{}';
14+
try {
15+
unserialize($payload);
16+
} catch (Throwable $e) {
17+
echo $e::class, ": ", $e->getMessage(), PHP_EOL;
18+
}
19+
?>
20+
--EXPECT--
21+
Exception: Unserialization of Uri\WhatWg\Url using the "C" format is unsupported
22+
Exception: Unserialization of Uri\Rfc3986\Uri using the "C" format is unsupported

0 commit comments

Comments
 (0)