diff --git a/NEWS b/NEWS index e42393462d83..b8025388d1fc 100644 --- a/NEWS +++ b/NEWS @@ -520,6 +520,8 @@ PHP NEWS certificates with CRLs). (Alexander Danilov) - PDO: + . Fixed use-after-free when a destructor of an old PDO::ATTR_STATEMENT_CLASS + constructor argument reads the attribute. (Roman Ondráček) . Fixed invalid ATTR_ORACLE_NULLS values being accepted and truncated. (Ilia Alshanetsky) . Fixed a duplicate warning when ATTR_STATEMENT_CLASS is rejected on a diff --git a/ext/pdo/pdo_dbh.c b/ext/pdo/pdo_dbh.c index 804cbb644077..0ed321d0a500 100644 --- a/ext/pdo/pdo_dbh.c +++ b/ext/pdo/pdo_dbh.c @@ -937,14 +937,17 @@ static bool pdo_dbh_attribute_set(pdo_dbh_t *dbh, zend_long attr, zval *value) / zend_zval_value_name(item)); return false; } + /* Store the new arguments before releasing the old ones, so that + * destructors of the old arguments do not observe them being destroyed. */ + zval old_ctor_args; + ZVAL_COPY_VALUE(&old_ctor_args, &dbh->def_stmt_ctor_args); dbh->def_stmt_ce = pce; - if (!Z_ISUNDEF(dbh->def_stmt_ctor_args)) { - zval_ptr_dtor(&dbh->def_stmt_ctor_args); - ZVAL_UNDEF(&dbh->def_stmt_ctor_args); - } if (item != NULL) { ZVAL_COPY(&dbh->def_stmt_ctor_args, item); + } else { + ZVAL_UNDEF(&dbh->def_stmt_ctor_args); } + zval_ptr_dtor(&old_ctor_args); return true; } /* Don't throw a ValueError as the attribute might be a driver specific one */ diff --git a/ext/pdo_sqlite/tests/pdo_statement_class_ctor_args_release.phpt b/ext/pdo_sqlite/tests/pdo_statement_class_ctor_args_release.phpt new file mode 100644 index 000000000000..6ab6f5c6d04f --- /dev/null +++ b/ext/pdo_sqlite/tests/pdo_statement_class_ctor_args_release.phpt @@ -0,0 +1,30 @@ +--TEST-- +PDO::ATTR_STATEMENT_CLASS: destructors of old constructor arguments do not observe them being released +--EXTENSIONS-- +pdo_sqlite +--FILE-- +getAttribute(PDO::ATTR_STATEMENT_CLASS); + } +} +$db = new PDO('sqlite::memory:'); +$db->setAttribute(PDO::ATTR_STATEMENT_CLASS, ['St', [str_repeat('A', 64), new D, str_repeat('B', 64)]]); +$db->setAttribute(PDO::ATTR_STATEMENT_CLASS, ['St', [1]]); +var_dump($keep); +?> +--EXPECT-- +array(2) { + [0]=> + string(2) "St" + [1]=> + array(1) { + [0]=> + int(1) + } +}