sqlcipher / sqlcipher-android

SQLCipher for Android provides an interface to SQLCipher databases on the Android platform.
Other
89 stars 17 forks source link

File is not a database (code 26) crash after calling changePassword() #22

Open mustafa-antonio opened 7 months ago

mustafa-antonio commented 7 months ago
(26) statement aborts at 8: [SELECT * FROM note] file is not a database
--------- beginning of crash
com.example.sqlcipher_test           E  exception: file is not a database (code 26); query: SELECT * FROM note
 com.example.sqlcipher_test           E  FATAL EXCEPTION: main
Process: com.example.sqlcipher_test, PID: 21229
android.database.sqlite.SQLiteException: file is not a database (code 26)
at net.zetetic.database.sqlcipher.SQLiteConnection.nativeExecuteForCursorWindow(Native Method)                                                                                                      at net.zetetic.database.sqlcipher.SQLiteConnection.executeForCursorWindow(SQLiteConnection.java:929)                                                                                                        at net.zetetic.database.sqlcipher.SQLiteSession.executeForCursorWindow(SQLiteSession.java:871)
at net.zetetic.database.sqlcipher.SQLiteQuery.fillWindow(SQLiteQuery.java:68)
at net.zetetic.database.sqlcipher.SQLiteCursor.fillWindow(SQLiteCursor.java:196)
at net.zetetic.database.sqlcipher.SQLiteCursor.getCount(SQLiteCursor.java:135)
at com.example.sqlcipher_test.db.NoteDao_Impl$3.call(NoteDao_Impl.java:92)
at com.example.sqlcipher_test.db.NoteDao_Impl$3.call(NoteDao_Impl.java:84)
at androidx.room.CoroutinesRoom$Companion$execute$4$job$1.invokeSuspend(CoroutinesRoom.kt:87)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644)
at java.lang.Thread.run(Thread.java:1012)

Looks like changePassword() does something incorrectly and throws this error when we call Room DAO query after. Happens in my production app and could be reproduced in sample project I attached to this issue (tested on Pixel 6a emulator with Android 14).

sqlciphertest.zip

Unless I'm missing some steps when performing password change? Issue appears in 4.5.4 & 4.5.5 version (possibly earlier versions too but haven't checked them).

Issue doesn't appear in legacy counterpart android-database-sqlcipher (4.5.4).

developernotes commented 7 months ago

Hi @mustafa-antonio,

Thank you for your report and sample project, we will look into this and share our feedback once reviewed.

anhndt3 commented 6 months ago

@developernotes is there any update on this? We've met the similar crash from android-database-sqlcipher like https://discuss.zetetic.net/t/fatal-exception-net-sqlcipher-database-sqliteexception-file-is-not-a-database-while-compiling-select-count-from-sqlite-master/4675 We plan to update the library and a bit worry to see this again here.

developernotes commented 6 months ago

Hello @mustafa-antonio,

I have pushed up a fix, and unit test to address the issue of changing a password while using the Room API. The issue was the non-primary connection was not being closed following the rekey operation on the primary connection. Your subsequent query following the rekey would run using the non-primary connection which was previously keyed with the originating, now incorrect password. The fix is here and will be included in our next public release. In the interim, you can close and reopen your connection during a password change event. Thank you again for providing a recreation scenario.

developernotes commented 6 months ago

Hi @anhndt3,

Are you able to provide an isolated recreation of the behavior you are seeing? If so, we would be happy to investigate the issue further. If you are using android-database-sqlcipher, you might consider adding a separate recreation test within the test suite.

anhndt3 commented 6 months ago

Thanks for replying and pushing fix up, I am updating the library, haven't got the issue so far, just wonder the same issue still occurs or not. I will let you know if there's problem and reproduce sample.

mustafa-antonio commented 5 months ago

@developernotes thanks for letting me know. Will check that out in next week after holidays.