Skip to content

Fix GH-22665: pdo_odbc OOB write on oversized diagnostic length#22675

Open
iliaal wants to merge 1 commit into
php:PHP-8.4from
iliaal:fix/gh-22665-pdo-odbc-diag-oob
Open

Fix GH-22665: pdo_odbc OOB write on oversized diagnostic length#22675
iliaal wants to merge 1 commit into
php:PHP-8.4from
iliaal:fix/gh-22665-pdo-odbc-diag-oob

Conversation

@iliaal

@iliaal iliaal commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

pdo_odbc_error() NUL-terminates the diagnostic buffer at the length SQLGetDiagRec reports, which on a truncated message is the full untruncated length and can reach or exceed the 512-byte last_err_msg buffer, writing the terminator out of bounds into the adjacent last_error field. Clamps the index to sizeof-1; the size_t cast also covers a negative driver-reported length. Reproduces with the SQLite3 ODBC driver (reports 512) via a failing query whose long identifier lengthens the message; the test skips without that driver.

Fixes #22665

@NattyNarwhal NattyNarwhal left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks OK, but see the driver comment in GH-22672. I think this is easy to reproduce regardless of driver versus the other issues, so should be trivial to the SQL Server driver.

pdo_odbc_error() writes the NUL terminator at
einfo->last_err_msg[errmsgsize], where errmsgsize is the diagnostic length
SQLGetDiagRec reports. On a truncated message (SQL_SUCCESS_WITH_INFO) the
driver reports the full untruncated length, which can reach or exceed the
512-byte buffer, so the terminator lands out of bounds in the adjacent
last_error field. Clamp the index to sizeof(last_err_msg) - 1; the size_t
cast also folds a negative driver-reported length into the same guard.

Fixes phpGH-22665
@iliaal iliaal force-pushed the fix/gh-22665-pdo-odbc-diag-oob branch from b218c57 to 0ee0bae Compare July 10, 2026 14:18
@iliaal

iliaal commented Jul 10, 2026

Copy link
Copy Markdown
Contributor Author

Right, this one is driver-agnostic: any failing query whose diagnostic message reaches the buffer size trips it, no truncation contract needed. Switched the test to :memory: to match #22672.

@iliaal iliaal requested a review from NattyNarwhal July 10, 2026 14:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Out-of-bounds write in ext/pdo_odbc (pdo_odbc_error()) when the ODBC driver reports a diagnostic message length beyond the fixed last_err_msg buffer

2 participants