Skip to content

Commit

Permalink
Fix #81720: Uninitialized array in pg_query_params() leading to RCE
Browse files Browse the repository at this point in the history
We must not free parameters which we haven't initialized yet.

We also fix the not directly related issue, that we checked for the
wrong value being `NULL`, potentially causing a segfault.
  • Loading branch information
cmb69 authored and ramsey committed Jun 7, 2022
1 parent 74383b6 commit 5ed5954
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
6 changes: 3 additions & 3 deletions ext/pgsql/pgsql.c
Original file line number Diff line number Diff line change
Expand Up @@ -1201,7 +1201,7 @@ PHP_FUNCTION(pg_query_params)
} else {
zend_string *param_str = zval_try_get_string(tmp);
if (!param_str) {
_php_pgsql_free_params(params, num_params);
_php_pgsql_free_params(params, i);
RETURN_THROWS();
}
params[i] = estrndup(ZSTR_VAL(param_str), ZSTR_LEN(param_str));
Expand Down Expand Up @@ -3918,8 +3918,8 @@ PHP_FUNCTION(pg_send_execute)
params[i] = NULL;
} else {
zend_string *tmp_str = zval_try_get_string(tmp);
if (UNEXPECTED(!tmp)) {
_php_pgsql_free_params(params, num_params);
if (UNEXPECTED(!tmp_str)) {
_php_pgsql_free_params(params, i);
return;
}
params[i] = estrndup(ZSTR_VAL(tmp_str), ZSTR_LEN(tmp_str));
Expand Down
27 changes: 27 additions & 0 deletions ext/pgsql/tests/bug81720.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
Bug #81720 (Uninitialized array in pg_query_params() leading to RCE)
--SKIPIF--
<?php include("skipif.inc"); ?>
--FILE--
<?php
include('config.inc');

$conn = pg_connect($conn_str);

try {
pg_query_params($conn, 'SELECT $1, $2', [1, new stdClass()]);
} catch (Throwable $ex) {
echo $ex->getMessage(), PHP_EOL;
}

try {
pg_send_prepare($conn, "my_query", 'SELECT $1, $2');
pg_get_result($conn);
pg_send_execute($conn, "my_query", [1, new stdClass()]);
} catch (Throwable $ex) {
echo $ex->getMessage(), PHP_EOL;
}
?>
--EXPECT--
Object of class stdClass could not be converted to string
Object of class stdClass could not be converted to string

0 comments on commit 5ed5954

Please sign in to comment.