Skip to content

Commit f724f8e

Browse files
cmb69sgolemon
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 6fcb534 commit f724f8e

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
@@ -1122,7 +1122,7 @@ PHP_FUNCTION(pg_query_params)
11221122
} else {
11231123
zend_string *param_str = zval_try_get_string(tmp);
11241124
if (!param_str) {
1125-
_php_pgsql_free_params(params, num_params);
1125+
_php_pgsql_free_params(params, i);
11261126
RETURN_THROWS();
11271127
}
11281128
params[i] = estrndup(ZSTR_VAL(param_str), ZSTR_LEN(param_str));
@@ -3920,8 +3920,8 @@ PHP_FUNCTION(pg_send_execute)
39203920
params[i] = NULL;
39213921
} else {
39223922
zend_string *tmp_str = zval_try_get_string(tmp);
3923-
if (UNEXPECTED(!tmp)) {
3924-
_php_pgsql_free_params(params, num_params);
3923+
if (UNEXPECTED(!tmp_str)) {
3924+
_php_pgsql_free_params(params, i);
39253925
return;
39263926
}
39273927
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)