Skip to content

ext/pdo_sqlite: Fix use-after-free of bound string and LOB parameters - #25597

Closed
iliaal wants to merge 1 commit into
php:PHP-8.4from
iliaal:fix/gh-25588-pdo-sqlite-bind-uaf
Closed

iliaal wants to merge 1 commit into
php:PHP-8.4from
iliaal:fix/gh-25588-pdo-sqlite-bind-uaf

Conversation

@iliaal

@iliaal iliaal commented Nov 19, 2026

Copy link
Copy Markdown
Member

If a PDO SQLite statement is executed again with an array that omits a parameter, or a bound value is replaced or its bindParam() variable reassigned while rows are still being fetched, SQLite reads the old string after PHP has freed it, because strings and LOBs were bound with SQLITE_STATIC and SQLite keeps a binding across sqlite3_reset(). Bind with SQLITE_TRANSIENT, as ext/sqlite3 does, and report a failed blob bind like a failed text bind. An omitted parameter keeps its previous value, as integer parameters already do.

Fixes GH-25588

pdo_sqlite bound string and LOB parameters with SQLITE_STATIC, so SQLite
kept a pointer into the parameter's zend_string. SQLite holds a binding
across sqlite3_reset() and reads it on later sqlite3_step() calls, including
the ones fetch() makes, but PDO frees the value when execute() replaces the
parameters with an array that omits one, and when bindValue() replaces a
parameter or a bindParam() variable is reassigned while rows are still
being fetched. Bind with SQLITE_TRANSIENT so SQLite owns a copy, as
ext/sqlite3 already does, and report a failed blob bind through PDO as the
text bind already does. Integer, boolean and NULL parameters are bound by
value and were not affected.

Fixes phpGH-25588

@ndossche ndossche 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.

Hm, SQLITE_TRANSIENT will make an extra copy on each execute. A LOB from a stream is already copied, so we get a double copy. Doesn't exist/sqlite3 avoid this somehow?

@iliaal

iliaal commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

ext/sqlite3 makes the same extra copy. It reads the stream into a zend_string, binds it with SQLITE_TRANSIENT, then releases the string.

@iliaal
iliaal requested a review from ndossche November 22, 2026 15:06

@ndossche ndossche 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.

My intuition says it can be avoided by taking a refcount to the zend_string, but that's master-only material.

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.

heap-use-after-free in pdo_sqlite_stmt_execute when a bound param is omitted on re-execute

2 participants