Skip to content

Commit 5ae20c6

Browse files
committed
Fix bug #66550 (SQLite prepared statement use-after-free)
1 parent bd31cb7 commit 5ae20c6

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ PHP NEWS
99
. Fixed bug #69152 (Type Confusion Infoleak Vulnerability in unserialize()
1010
with SoapFault). (Dmitry)
1111

12+
- Sqlite3:
13+
. Fixed bug #66550 (SQLite prepared statement use-after-free). (Sean Heelan)
14+
1215
- Postgres:
1316
. Fixed bug #68741 (Null pointer deference) (CVE-2015-1352). (Xinchen Hui)
1417

ext/sqlite3/sqlite3.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,6 +1274,8 @@ PHP_METHOD(sqlite3stmt, paramCount)
12741274
php_sqlite3_stmt *stmt_obj;
12751275
zval *object = getThis();
12761276
stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
1277+
1278+
SQLITE3_CHECK_INITIALIZED(stmt_obj->db_obj, stmt_obj->initialised, SQLite3)
12771279

12781280
if (zend_parse_parameters_none() == FAILURE) {
12791281
return;
@@ -1290,6 +1292,8 @@ PHP_METHOD(sqlite3stmt, close)
12901292
php_sqlite3_stmt *stmt_obj;
12911293
zval *object = getThis();
12921294
stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
1295+
1296+
SQLITE3_CHECK_INITIALIZED(stmt_obj->db_obj, stmt_obj->initialised, SQLite3)
12931297

12941298
if (zend_parse_parameters_none() == FAILURE) {
12951299
return;
@@ -1308,6 +1312,8 @@ PHP_METHOD(sqlite3stmt, reset)
13081312
php_sqlite3_stmt *stmt_obj;
13091313
zval *object = getThis();
13101314
stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
1315+
1316+
SQLITE3_CHECK_INITIALIZED(stmt_obj->db_obj, stmt_obj->initialised, SQLite3)
13111317

13121318
if (zend_parse_parameters_none() == FAILURE) {
13131319
return;
@@ -1328,6 +1334,8 @@ PHP_METHOD(sqlite3stmt, clear)
13281334
php_sqlite3_stmt *stmt_obj;
13291335
zval *object = getThis();
13301336
stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
1337+
1338+
SQLITE3_CHECK_INITIALIZED(stmt_obj->db_obj, stmt_obj->initialised, SQLite3)
13311339

13321340
if (zend_parse_parameters_none() == FAILURE) {
13331341
return;
@@ -1349,6 +1357,8 @@ PHP_METHOD(sqlite3stmt, readOnly)
13491357
php_sqlite3_stmt *stmt_obj;
13501358
zval *object = getThis();
13511359
stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
1360+
1361+
SQLITE3_CHECK_INITIALIZED(stmt_obj->db_obj, stmt_obj->initialised, SQLite3)
13521362

13531363
if (zend_parse_parameters_none() == FAILURE) {
13541364
return;
@@ -1416,6 +1426,8 @@ PHP_METHOD(sqlite3stmt, bindParam)
14161426
zval *object = getThis();
14171427
struct php_sqlite3_bound_param param = {0};
14181428
stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
1429+
1430+
SQLITE3_CHECK_INITIALIZED(stmt_obj->db_obj, stmt_obj->initialised, SQLite3)
14191431

14201432
param.param_number = -1;
14211433
param.type = SQLITE3_TEXT;
@@ -1447,6 +1459,8 @@ PHP_METHOD(sqlite3stmt, bindValue)
14471459
zval *object = getThis();
14481460
struct php_sqlite3_bound_param param = {0};
14491461
stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
1462+
1463+
SQLITE3_CHECK_INITIALIZED(stmt_obj->db_obj, stmt_obj->initialised, SQLite3)
14501464

14511465
param.param_number = -1;
14521466
param.type = SQLITE3_TEXT;
@@ -1482,6 +1496,8 @@ PHP_METHOD(sqlite3stmt, execute)
14821496

14831497
stmt_obj = (php_sqlite3_stmt *)zend_object_store_get_object(object TSRMLS_CC);
14841498

1499+
SQLITE3_CHECK_INITIALIZED(stmt_obj->db_obj, stmt_obj->initialised, SQLite3)
1500+
14851501
if (zend_parse_parameters_none() == FAILURE) {
14861502
return;
14871503
}

ext/sqlite3/tests/bug66550.phpt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
Bug #66550 (SQLite prepared statement use-after-free)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('sqlite3')) die('skip');
6+
?>
7+
--FILE--
8+
<?php
9+
10+
$db = new SQLite3(':memory:');
11+
12+
$db->exec('CREATE TABLE foo (id INTEGER, bar STRING)');
13+
14+
$stmt = $db->prepare('SELECT bar FROM foo WHERE id=:id');
15+
// Close the database connection and free the internal sqlite3_stmt object
16+
$db->close();
17+
// Access the sqlite3_stmt object via the php_sqlite3_stmt container
18+
$stmt->reset();
19+
?>
20+
==DONE==
21+
--EXPECTF--
22+
Warning: SQLite3Stmt::reset(): The SQLite3 object has not been correctly initialised in %s
23+
==DONE==

0 commit comments

Comments
 (0)