Commit 066f18f4f56 for php
commit 066f18f4f56ab58501b805dfd93f2251079d4fb3
Author: Ilia Alshanetsky <ilia@ilia.ws>
Date: Thu Nov 19 07:46:59 2026 -0500
ext/pdo_sqlite: Fix use-after-free of bound string and LOB parameters
SQLite keeps bindings across sqlite3_reset() and can still read them
while fetching rows. Bind strings and LOBs with SQLITE_TRANSIENT so the
statement owns a copy when PDO releases or replaces the parameter. Report
blob binding errors through PDO as for text.
Fixes GH-25588
Closes GH-25597
diff --git a/NEWS b/NEWS
index 02558b5a277..6b173f14018 100644
--- a/NEWS
+++ b/NEWS
@@ -137,6 +137,10 @@ PHP NEWS
. Fixed a crash when executing a prepared statement again after a failed
execute() with libmysqlclient. (Ilia Alshanetsky)
+- PDO_SQLite:
+ . Fixed bug GH-25588 (Use-after-free of a string parameter omitted on a later
+ execute() or rebound while fetching). (Ilia Alshanetsky)
+
- POSIX:
. Fixed bug GH-25530 (posix_get_last_error() returns an error from a
previous request). (Artem Ukrainskiy)
diff --git a/ext/pdo_sqlite/sqlite_statement.c b/ext/pdo_sqlite/sqlite_statement.c
index 8c0d9069ee6..b118b9b8310 100644
--- a/ext/pdo_sqlite/sqlite_statement.c
+++ b/ext/pdo_sqlite/sqlite_statement.c
@@ -159,9 +159,10 @@ static int pdo_sqlite_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_d
if (SQLITE_OK == sqlite3_bind_blob(S->stmt, param->paramno + 1,
Z_STRVAL_P(parameter),
Z_STRLEN_P(parameter),
- SQLITE_STATIC)) {
+ SQLITE_TRANSIENT)) {
return 1;
}
+ pdo_sqlite_error_stmt(stmt);
return 0;
case PDO_PARAM_STR:
@@ -182,7 +183,7 @@ static int pdo_sqlite_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_d
if (SQLITE_OK == sqlite3_bind_text(S->stmt, param->paramno + 1,
Z_STRVAL_P(parameter),
Z_STRLEN_P(parameter),
- SQLITE_STATIC)) {
+ SQLITE_TRANSIENT)) {
return 1;
}
}
diff --git a/ext/pdo_sqlite/tests/gh25588.phpt b/ext/pdo_sqlite/tests/gh25588.phpt
new file mode 100644
index 00000000000..b5bbe925f56
--- /dev/null
+++ b/ext/pdo_sqlite/tests/gh25588.phpt
@@ -0,0 +1,101 @@
+--TEST--
+GH-25588 (Use-after-free of a bound string after its PDO parameter is released)
+--EXTENSIONS--
+pdo_sqlite
+--FILE--
+<?php
+$db = new PDO('sqlite::memory:');
+$db->exec('CREATE TABLE t (a TEXT, b TEXT)');
+$db->exec("INSERT INTO t VALUES ('xxxxxxxxxx', NULL), ('xxxxxxxxxx', NULL), ('xxxxxxxxxx', NULL)");
+
+$stmt = $db->prepare('INSERT INTO t VALUES (:a, :b)');
+$stmt->execute([':a' => str_repeat('a', 10), ':b' => str_repeat('b', 10)]);
+$stmt->execute([':a' => 1234567890]);
+echo "execute() with fewer params:\n";
+var_dump($db->query('SELECT a, b FROM t WHERE b IS NOT NULL ORDER BY rowid')->fetchAll(PDO::FETCH_NUM));
+
+$stmt = $db->prepare('SELECT rowid FROM t WHERE a = :a ORDER BY rowid');
+$stmt->bindValue(':a', str_repeat('x', 10));
+$stmt->execute();
+echo "bindValue() during fetch:\n";
+var_dump($stmt->fetchColumn());
+$stmt->bindValue(':a', str_repeat('q', 10));
+$reuse = str_repeat('w', 10);
+while (($id = $stmt->fetchColumn()) !== false) {
+ var_dump($id);
+}
+
+$value = str_repeat('x', 10);
+$stmt = $db->prepare('SELECT rowid FROM t WHERE a = :a ORDER BY rowid');
+$stmt->bindParam(':a', $value);
+$stmt->execute();
+echo "bindParam() variable reassigned during fetch:\n";
+var_dump($stmt->fetchColumn());
+$value = str_repeat('q', 10);
+$reuse = str_repeat('w', 10);
+while (($id = $stmt->fetchColumn()) !== false) {
+ var_dump($id);
+}
+
+$stmt = $db->prepare('INSERT INTO t VALUES (:a, :b)');
+$stmt->bindValue(':a', str_repeat('c', 10));
+$stmt->bindValue(':b', str_repeat('d', 10), PDO::PARAM_LOB);
+$stmt->execute();
+$stmt->execute([':a' => 1234567891]);
+echo "PARAM_LOB omitted on execute():\n";
+var_dump($db->query("SELECT a, b FROM t WHERE a IN ('cccccccccc', '1234567891') ORDER BY rowid")->fetchAll(PDO::FETCH_NUM));
+
+echo "PARAM_LOB bind failure:\n";
+$stmt = $db->prepare('SELECT :a');
+$stmt->bindValue(':nope', 'x', PDO::PARAM_LOB);
+try {
+ $stmt->execute();
+} catch (PDOException $e) {
+ echo $e::class, ": ", $e->getMessage(), PHP_EOL;
+}
+?>
+--EXPECT--
+execute() with fewer params:
+array(2) {
+ [0]=>
+ array(2) {
+ [0]=>
+ string(10) "aaaaaaaaaa"
+ [1]=>
+ string(10) "bbbbbbbbbb"
+ }
+ [1]=>
+ array(2) {
+ [0]=>
+ string(10) "1234567890"
+ [1]=>
+ string(10) "bbbbbbbbbb"
+ }
+}
+bindValue() during fetch:
+int(1)
+int(2)
+int(3)
+bindParam() variable reassigned during fetch:
+int(1)
+int(2)
+int(3)
+PARAM_LOB omitted on execute():
+array(2) {
+ [0]=>
+ array(2) {
+ [0]=>
+ string(10) "cccccccccc"
+ [1]=>
+ string(10) "dddddddddd"
+ }
+ [1]=>
+ array(2) {
+ [0]=>
+ string(10) "1234567891"
+ [1]=>
+ string(10) "dddddddddd"
+ }
+}
+PARAM_LOB bind failure:
+PDOException: SQLSTATE[HY000]: General error: 25 column index out of range