Skip to content

Commit 286162e

Browse files
cmb69smalyshev
authored andcommitted
Fix #76452: Crash while parsing blob data in firebird_fetch_blob
We need to prevent integer overflow when calling `erealloc()` with `len+1`.
1 parent a5538c6 commit 286162e

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

ext/pdo_firebird/firebird_statement.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,11 @@ static int firebird_fetch_blob(pdo_stmt_t *stmt, int colno, char **ptr, /* {{{ *
299299
unsigned short seg_len;
300300
ISC_STATUS stat;
301301

302+
/* prevent overflow */
303+
if (*len == ZEND_ULONG_MAX) {
304+
result = 0;
305+
goto fetch_blob_end;
306+
}
302307
*ptr = S->fetch_buf[colno] = erealloc(S->fetch_buf[colno], *len+1);
303308

304309
for (cur_len = stat = 0; (!stat || stat == isc_segment) && cur_len < *len; cur_len += seg_len) {

ext/pdo_firebird/tests/bug_76452.data

856 Bytes
Binary file not shown.

ext/pdo_firebird/tests/bug_76452.phpt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
Bug ##76452 (Crash while parsing blob data in firebird_fetch_blob)
3+
--SKIPIF--
4+
<?php require('skipif.inc'); ?>
5+
--FILE--
6+
<?php
7+
require_once "payload_server.inc";
8+
9+
$address = run_server(__DIR__ . "/bug_76452.data");
10+
11+
// no need to change the credentials; we're running against a falke server
12+
$dsn = "firebird:dbname=inet://$address/test";
13+
$username = 'SYSDBA';
14+
$password = 'masterkey';
15+
16+
$dbh = new PDO($dsn, $username, $password, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
17+
$query = $dbh->prepare("select * from test");
18+
$query->execute();
19+
var_dump($query->fetch());
20+
?>
21+
--EXPECT--
22+
array(4) {
23+
["AAA"]=>
24+
string(4) "hihi"
25+
[0]=>
26+
string(4) "hihi"
27+
["BBBB"]=>
28+
NULL
29+
[1]=>
30+
NULL
31+
}

0 commit comments

Comments
 (0)