Skip to content

Commit d47537c

Browse files
committed
Merge branch 'PHP-8.5' into PHP-8.6
* PHP-8.5: ext/pdo_firebird: Fix DPB stack overflow on long connection parameters
2 parents 18ece89 + 9099edc commit d47537c

3 files changed

Lines changed: 49 additions & 3 deletions

File tree

‎NEWS‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ PHP NEWS
5959
. Fixed an exception thrown by the session_get_cb stream context callback
6060
being replaced with a TypeError. (Ilia Alshanetsky)
6161

62+
- PDO_Firebird:
63+
. Fixed stack buffer overflow with long connection user names, passwords,
64+
charsets, or roles. (Ilia Alshanetsky)
65+
6266
- SOAP:
6367
. Fixed bug GH-25267 (NULL dereference in to_xml_any() with integer key).
6468
(lazerg)

‎ext/pdo_firebird/firebird_driver.c‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,7 +1355,6 @@ static int pdo_firebird_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /*
13551355
{ "password", NULL, 0 }
13561356
};
13571357
int i, ret = 0;
1358-
short buf_len = 256, dpb_len;
13591358

13601359
pdo_firebird_db_handle *H = dbh->driver_data = pecalloc(1,sizeof(*H),dbh->is_persistent);
13611360

@@ -1386,8 +1385,12 @@ static int pdo_firebird_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /*
13861385
do {
13871386
static char const dpb_flags[] = {
13881387
isc_dpb_user_name, isc_dpb_password, isc_dpb_lc_ctype, isc_dpb_sql_role_name };
1388+
static char const *const dpb_names[] = { "user", "password", "charset", "role" };
13891389
char const *dpb_values[] = { dbh->username, dbh->password, vars[1].optval, vars[2].optval };
13901390
char dpb_buffer[256] = { isc_dpb_version1 }, *dpb;
1391+
size_t buf_len = sizeof(dpb_buffer) - 1;
1392+
size_t dpb_value_len;
1393+
int dpb_len;
13911394

13921395
if (EG(exception)) {
13931396
break;
@@ -1397,14 +1400,26 @@ static int pdo_firebird_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /*
13971400

13981401
/* loop through all the provided arguments and set dpb fields accordingly */
13991402
for (i = 0; i < sizeof(dpb_flags); ++i) {
1400-
if (dpb_values[i] && buf_len > 0) {
1401-
dpb_len = slprintf(dpb, buf_len, "%c%c%s", dpb_flags[i], (unsigned char)strlen(dpb_values[i]),
1403+
if (dpb_values[i]) {
1404+
dpb_value_len = strlen(dpb_values[i]);
1405+
if (dpb_value_len + 2 >= buf_len) {
1406+
zend_throw_exception_ex(php_pdo_get_exception(), 0,
1407+
"Firebird database parameter \"%s\" is too long", dpb_names[i]);
1408+
break;
1409+
}
1410+
ZEND_ASSERT(dpb_value_len <= UCHAR_MAX);
1411+
1412+
dpb_len = slprintf(dpb, buf_len, "%c%c%s", dpb_flags[i], (unsigned char)dpb_value_len,
14021413
dpb_values[i]);
14031414
dpb += dpb_len;
14041415
buf_len -= dpb_len;
14051416
}
14061417
}
14071418

1419+
if (EG(exception)) {
1420+
break;
1421+
}
1422+
14081423
H->sql_dialect = PDO_FB_DIALECT;
14091424
if (vars[3].optval) {
14101425
H->sql_dialect = atoi(vars[3].optval);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
PDO_Firebird rejects values that overflow the database parameter buffer
3+
--EXTENSIONS--
4+
pdo_firebird
5+
--SKIPIF--
6+
<?php require('skipif.inc'); ?>
7+
--FILE--
8+
<?php
9+
require("testdb.inc");
10+
11+
$tests = [
12+
'one-byte length overflow' => [str_repeat('u', 256), PDO_FIREBIRD_TEST_PASS],
13+
'DPB capacity overflow' => [str_repeat('u', 200), str_repeat('p', 52)],
14+
];
15+
16+
foreach ($tests as $description => [$username, $password]) {
17+
try {
18+
new PDO(PDO_FIREBIRD_TEST_DSN, $username, $password);
19+
echo "$description: accepted\n";
20+
} catch (PDOException $e) {
21+
echo "$description: ", $e::class, ": ", $e->getMessage(), "\n";
22+
}
23+
}
24+
?>
25+
--EXPECT--
26+
one-byte length overflow: PDOException: Firebird database parameter "user" is too long
27+
DPB capacity overflow: PDOException: Firebird database parameter "password" is too long

0 commit comments

Comments
 (0)