Do not drop read errors of the real file - #272
Merged
Merged
Conversation
mcIoRead returned early only for SQLITE_IOERR_SHORT_READ. Every other error of the real file fell through to the handler of the file type, where rc is initialised with SQLITE_OK and then overwritten, so a failed read was reported as success and SQLite used the buffer as it was. For files that have no codec, a statement journal for example, this always happened. A read error while rolling back to a savepoint could therefore put page images that were never read into the main database. Return every error of the real file right away.
Owner
|
Ouch, that is indeed a critical bug. And it is actually surprising that no one seems to have encountered problems with it so far—at least, I haven't heard of any error reports. Thank you very much for discovering the issue. Fortunately, the fix is very simple. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hi Ulrich,
I ran into this while testing something else, and it looked serious enough to send you right away.
Type: Bug fix
When a read of the real file fails,
mcIoReaddoesn't always pass the error on. It returns early forSQLITE_IOERR_SHORT_READ, but any other error runs into the handler for the file type, andmcReadMainDb,mcReadMainJournal,mcReadSubJournalandmcReadWalall start withint rc = SQLITE_OK;and then assign torc. The failed read reaches SQLite as a success, and SQLite reads on with whatever was left in the buffer.If the file has no codec, that happens every single time, and statement journals are exactly that case: they are opened without a filename, so
pMainDbstays 0 andmcReadSubJournalreturnsSQLITE_OKno matter what.That matters because a statement journal holds the original pages of the main database, and
ROLLBACK TOwrites them back. I simulated a failing read there. SQLite took the untouched buffer for the old page content and wrote it into the database, the transaction was committed, andPRAGMA integrity_checkthen reported "Rowid out of order". The application never saw an error, and that is the part I find worrying.With an encrypted main database you usually notice it a moment later, because decrypting the untouched buffer fails. Without a checksum in the cipher scheme, or with an unencrypted database, the error is simply gone.
The fix is one line, returning every error of the real file right away:
A short read still takes the same path, since it isn't
SQLITE_OKeither, and a successful read is unaffected.How I tested it
I put a VFS below the SQLite3 Multiple Ciphers VFS that lets exactly one operation fail, a read, a write, half a write, a truncate or a memory allocation, and moved that single fault step by step through a workload of a temporary table, a sort, savepoints and a
VACUUM. After every run I checkedPRAGMA integrity_checkand whether the tables were in the state before or after the statement that failed.Without the change, a failing read of the statement journal repeatedly left the main database corrupted, and the application saw no error. With the change, the read error is reported, the transaction is rolled back and the database stays consistent. I ran this with page sizes of 4 KiB and 1 KiB.
I also let one read of the main database file fail at every position of a query, for an unencrypted database and for the
chacha20,sqlcipherandaes256cbcschemes. Whenever the page really has to be read, the error now reaches the application, and no query returned a wrong result without an error.make, the SQL tests andcryptotestpass, and there are no new compiler warnings.Best regards,
Markus