Skip to content

pdo_pgsql: use-after-free when a lazy statement follows a failed query #25838

Done

Description

@dixyes

Description

pgsql_stmt_dtor() clears H->running_stmt only when php_pdo_stmt_valid_db_obj_handle() is true. The PDO::query() failure path detaches database_object_handle before it releases the statement, so that check is false while the handle object is still alive. The pointer stays on the connection, the statement is freed, and the next lazy statement dereferences the stale pointer in pgsql_stmt_execute().

A detached handle is not a destroyed one: the statement is owned by dbh->query_stmt_obj, which PDO releases before the driver frees the handle. Only bug #72294, where the handle object is already destroyed, has to leave H untouched.

--TEST--
PDO PgSQL a failed PDO::query() leaves a dangling running statement
--EXTENSIONS--
pdo_pgsql
--SKIPIF--
<?php
require __DIR__ . '/config.inc';
require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
PDOTest::skip();
?>
--FILE--
<?php

require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
$pdo = PDOTest::test_factory(__DIR__ . '/common.phpt');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

/* PDO::query() detaches database_object_handle from the statement before
 * destroying it, so pgsql_stmt_dtor() sees an unusable handle and skips
 * clearing H->running_stmt. The connection keeps a pointer to freed memory. */
try {
    $pdo->query('SELECT 1 / 0');
} catch (PDOException $e) {
    echo $e->getCode(), PHP_EOL;
}

/* the next lazy statement dereferences H->running_stmt in pgsql_stmt_execute() */
$pdo->setAttribute(PDO::ATTR_PREFETCH, 0);
echo $pdo->query('SELECT 43')->fetchColumn(), PHP_EOL;
?>
--EXPECT--
22012
43

Observed on an ASan build of an unpatched tree:

==16414==ERROR: AddressSanitizer: heap-use-after-free
READ of size 8
    #0 pgsql_stmt_finish ext/pdo_pgsql/pgsql_statement.c:121
    #1 pgsql_stmt_execute ext/pdo_pgsql/pgsql_statement.c:277
    #2 zim_PDO_query ext/pdo/pdo_dbh.c:1233
0x... is located 0 bytes inside of 104-byte region
freed by thread T0 here:
    #3 pgsql_stmt_dtor ext/pdo_pgsql/pgsql_statement.c:258
    #4 php_pdo_free_statement ext/pdo/pdo_stmt.c:2111

Fix for reference:

diff --git a/ext/pdo_pgsql/pgsql_statement.c b/ext/pdo_pgsql/pgsql_statement.c
index 43aabecc7b5..496bd123154 100644
--- a/ext/pdo_pgsql/pgsql_statement.c
+++ b/ext/pdo_pgsql/pgsql_statement.c
@@ -206,7 +206,12 @@ static int pgsql_stmt_dtor(pdo_stmt_t *stmt)
 
 	pgsql_stmt_finish(S, FIN_DISCARD|(server_obj_usable ? FIN_CLOSE|FIN_ABORT : 0));
 
-	if (server_obj_usable && S->H->running_stmt == S) {
+	/* PDO::query() detaches database_object_handle before dropping a failed
+	 * statement, so the handle is alive while server_obj_usable is false. Its
+	 * query_stmt_obj is released before the driver frees the handle */
+	bool handle_detached = stmt->database_object_handle == NULL;
+
+	if ((server_obj_usable || handle_detached) && S->H->running_stmt == S) {
 		S->H->running_stmt = NULL;
 	}
 

PHP Version

PHP-8.5@76fbffd02c49c0a511b1edf2463a04a9f4c8af1a
PHP-8.6@286c2dcb39b02d431997ee19b8fd692e5a890a3d
master@33ab4fe448f50690bf3f753d9a64671057158d2d

Operating System

No response

Activity

  1. self-assigned this
    on Nov 27, 2026
  2. devnexen commented on Nov 27, 2026

    @devnexen
    Member

    thanks it s valid both repro and fix. PR on its way thanks !

  3. added a commit that references this issue on Nov 27, 2026
    8952a37
  4. added a commit that references this issue on Nov 29, 2026
    e32e32b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions