utelle / SQLite3MultipleCiphers

SQLite3 encryption extension with support for multiple ciphers
https://utelle.github.io/SQLite3MultipleCiphers/
MIT License
382 stars 73 forks source link

sqlite3.c:60597: pagerOpenWalIfPresent: Assertion `pPager->eState==PAGER_OPEN' failed. #167

Closed rogerbinns closed 3 months ago

rogerbinns commented 3 months ago

Build apsw-sqlite3mc with --debug so assertions are enabled.

$ python3 -m apsw.mctests -v
APSW debug build: missing sys.apsw_fault_inject_control
testBackup (__main__.MultipleCiphers.testBackup)
Check backup restrictions ... ok
testBasic (__main__.MultipleCiphers.testBasic) ... ok
testCompileOptions (__main__.MultipleCiphers.testCompileOptions)
Check secure compilation flags were used ... ok
testKey (__main__.MultipleCiphers.testKey)
Ensure a key sticks ... ok
testMcIssue156 (__main__.MultipleCiphers.testMcIssue156)
Check memory mapping ... ok
testReadmeApplyEncryption (__main__.MultipleCiphers.testReadmeApplyEncryption)
readme apply_encryption ... python3: /space/mc/sqlite3/sqlite3.c:60597: pagerOpenWalIfPresent: Assertion `pPager->eState==PAGER_OPEN' failed.
Aborted (core dumped)
rogerbinns commented 3 months ago

It was this commit re-enabling sqlicipher that made the assertion failure start happening.

utelle commented 3 months ago

I will look into this.

utelle commented 3 months ago

It was this commit re-enabling sqlicipher that made the assertion failure start happening.

I made a quick test. IMHO the problem has nothing to do with SQLCipher. The reason must be something else. The test crashes already on setting the invalid hexkey. Other tests like the backup test still succeed.

So, something seems to go wrong on applying a key for the second time. At the moment I don't know yet, why.

rogerbinns commented 3 months ago

I used cvise to reduce it. The first user_version pragma is cricitcal, and the second is when the assertion fails.

import apsw
db = apsw.Connection("mcdb")
db.pragma("user_version", "")
db.pragma("hexkey", "1122")
db.pragma("user_version", "")
utelle commented 3 months ago

I used cvise to reduce it. The first user_version pragma is cricitcal, and the second is when the assertion fails.

import apsw
db = apsw.Connection("mcdb")
db.pragma("user_version", "")
db.pragma("hexkey", "1122")
db.pragma("user_version", "")

Thanks. That shows that the SQLCipher configuration is most likely not involved.

AFAICT SQLite deals with the assertion situation correctly later on. For example, the release version of the SQLite shell works as expected and doesn't show any misbehaviour. Of course, this is no excuse for an obvious bug, and it will be fixed. It just will take some time.

utelle commented 3 months ago

After some hours of debugging I think I found the reason for the assertion. Internally the wrong key was correctly detected, but it was not properly communicated to upper level functions. Actually, the error is detected later in SQLite, but in debug mode the assertion is raised first.

Commit https://github.com/utelle/apsw-sqlite3mc/commit/ca146ddc910368afda052f4d73e2bd1f101d2e9d includes an updated amalgamation. Hopefully all tests will now run as expected.