Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ PHP NEWS
- PDO_ODBC:
. Fixed bug GH-20726 (Crash with ODBC connection pooling when the DSN
carries no credentials). (iliaal)
. Fixed bug GH-22666 (Heap buffer overflow when an output parameter value is
longer than the declared maxlen). (iliaal)

- Phar:
. Fixed inconsistent handling of the magic ".phar" directory. Paths such as
Expand Down
11 changes: 11 additions & 0 deletions ext/pdo_odbc/odbc_stmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ static int odbc_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *p
param->driver_data = P;

P->len = 0; /* is re-populated each EXEC_PRE */
P->outbuflen = 0;
P->outbuf = NULL;

P->is_unicode = pdo_odbc_sqltype_is_unicode(S, sqltype);
Expand All @@ -384,6 +385,7 @@ static int odbc_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *p
P->len *= 2;
}
P->outbuf = emalloc(P->len + (P->is_unicode ? 2:1));
P->outbuflen = P->len;
}
}

Expand Down Expand Up @@ -481,10 +483,16 @@ static int odbc_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *p
case PDO_ODBC_CONV_FAIL:
case PDO_ODBC_CONV_NOT_REQUIRED:
P->len = Z_STRLEN_P(parameter);
if (P->len > P->outbuflen) {
P->len = P->outbuflen;
}
memcpy(P->outbuf, Z_STRVAL_P(parameter), P->len);
break;
case PDO_ODBC_CONV_OK:
P->len = ulen;
if (P->len > P->outbuflen) {
P->len = P->outbuflen;
}
memcpy(P->outbuf, S->convbuf, P->len);
break;
}
Expand All @@ -506,6 +514,9 @@ static int odbc_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *p
zval_ptr_dtor(parameter);

if (P->len >= 0) {
if (P->len > P->outbuflen) {
P->len = P->outbuflen;
}
ZVAL_STRINGL(parameter, P->outbuf, P->len);
switch (pdo_odbc_ucs22utf8(stmt, P->is_unicode, parameter)) {
case PDO_ODBC_CONV_FAIL:
Expand Down
1 change: 1 addition & 0 deletions ext/pdo_odbc/php_pdo_odbc_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ typedef struct {

typedef struct {
SQLLEN len;
SQLLEN outbuflen;
SQLSMALLINT paramtype;
char *outbuf;
unsigned is_unicode:1;
Expand Down
33 changes: 33 additions & 0 deletions ext/pdo_odbc/tests/gh22666.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--TEST--
GH-22666 (Heap buffer overflow when an output param value exceeds its maxlen)
--EXTENSIONS--
pdo_odbc
--SKIPIF--
<?php
$file = tempnam(sys_get_temp_dir(), "gh22666");
try {
$pdo = new PDO("odbc:Driver=SQLite3;Database=$file");
} catch (PDOException $e) {
@unlink($file);
die("skip requires the SQLite3 ODBC driver");
}
?>
--FILE--
<?php
$file = tempnam(sys_get_temp_dir(), "gh22666");
$pdo = new PDO("odbc:Driver=SQLite3;Database=$file");

// An INPUT_OUTPUT parameter declares a maxlen of 4, so the output buffer is 4
// bytes, but the runtime value is longer. The copy into that buffer must be
// bounded to the declared capacity, not overflow it.
$value = str_repeat('A', 64);
$stmt = $pdo->prepare('SELECT ?');
$stmt->bindParam(1, $value, PDO::PARAM_STR | PDO::PARAM_INPUT_OUTPUT, 4);
$stmt->execute();

echo "bounded to maxlen: "; var_dump($value);

@unlink($file);
?>
--EXPECT--
bounded to maxlen: string(4) "AAAA"
Loading