Skip to content

Commit 55f6895

Browse files
cmb69smalyshev
authored andcommitted
Fix #81720: Uninitialized array in pg_query_params() leading to RCE
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.
1 parent fbee73d commit 55f6895

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

ext/pgsql/pgsql.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1994,7 +1994,7 @@ PHP_FUNCTION(pg_query_params)
19941994
if (Z_TYPE(tmp_val) != IS_STRING) {
19951995
php_error_docref(NULL, E_WARNING,"Error converting parameter");
19961996
zval_ptr_dtor(&tmp_val);
1997-
_php_pgsql_free_params(params, num_params);
1997+
_php_pgsql_free_params(params, i);
19981998
RETURN_FALSE;
19991999
}
20002000
params[i] = estrndup(Z_STRVAL(tmp_val), Z_STRLEN(tmp_val));
@@ -5175,8 +5175,8 @@ PHP_FUNCTION(pg_send_execute)
51755175
params[i] = NULL;
51765176
} else {
51775177
zend_string *tmp_str = zval_try_get_string(tmp);
5178-
if (UNEXPECTED(!tmp)) {
5179-
_php_pgsql_free_params(params, num_params);
5178+
if (UNEXPECTED(!tmp_str)) {
5179+
_php_pgsql_free_params(params, i);
51805180
return;
51815181
}
51825182
params[i] = estrndup(ZSTR_VAL(tmp_str), ZSTR_LEN(tmp_str));

ext/pgsql/tests/bug81720.phpt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
Bug #81720 (Uninitialized array in pg_query_params() leading to RCE)
3+
--SKIPIF--
4+
<?php include("skipif.inc"); ?>
5+
--FILE--
6+
<?php
7+
include('config.inc');
8+
9+
$conn = pg_connect($conn_str);
10+
11+
try {
12+
pg_query_params($conn, 'SELECT $1, $2', [1, new stdClass()]);
13+
} catch (Throwable $ex) {
14+
echo $ex->getMessage(), PHP_EOL;
15+
}
16+
17+
try {
18+
pg_send_prepare($conn, "my_query", 'SELECT $1, $2');
19+
pg_get_result($conn);
20+
pg_send_execute($conn, "my_query", [1, new stdClass()]);
21+
} catch (Throwable $ex) {
22+
echo $ex->getMessage(), PHP_EOL;
23+
}
24+
?>
25+
--EXPECT--
26+
Object of class stdClass could not be converted to string
27+
Object of class stdClass could not be converted to string

0 commit comments

Comments
 (0)