aboutsummaryrefslogtreecommitdiffstats
path: root/community/php81/CVE-2022-31626.patch
blob: 7c8770bd2be544c0ae4d70995b3204ad60b87a47 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
From 55f6895f4b4c677272fd4ee1113acdbd99c4b5ab Mon Sep 17 00:00:00 2001
From: "Christoph M. Becker" <cmbecker69@gmx.de>
Date: Tue, 17 May 2022 12:59:23 +0200
Subject: [PATCH] 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.
---
 ext/pgsql/pgsql.c             |  6 +++---
 ext/pgsql/tests/bug81720.phpt | 27 +++++++++++++++++++++++++++
 2 files changed, 30 insertions(+), 3 deletions(-)
 create mode 100644 ext/pgsql/tests/bug81720.phpt

diff --git a/ext/pgsql/pgsql.c b/ext/pgsql/pgsql.c
index f52ff884d83c..7dcd56cf1441 100644
--- a/ext/pgsql/pgsql.c
+++ b/ext/pgsql/pgsql.c
 				params[i] = estrndup(Z_STRVAL(tmp_val), Z_STRLEN(tmp_val));
@@ -3920,8 +3920,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));
diff --git a/ext/pgsql/tests/bug81720.phpt b/ext/pgsql/tests/bug81720.phpt
new file mode 100644
index 000000000000..d79f1fcdd612
--- /dev/null
+++ b/ext/pgsql/tests/bug81720.phpt
@@ -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