dbfile: fix use-after-free and leak when recreating a rejected hashfile - #413
Open
martinus wants to merge 1 commit into
Open
dbfile: fix use-after-free and leak when recreating a rejected hashfile#413martinus wants to merge 1 commit into
martinus wants to merge 1 commit into
Conversation
When an existing hashfile fails dbfile_check() (a schema version or hash-type mismatch), dbfile_prepare() takes the "Recreating hashfile" path: it sqlite3_close()s the handle, unlinks the file, reopens a fresh one, and recurses. But the handle was passed by value, so the reopened handle only ever lived in dbfile_prepare()'s local variable. Its caller, __dbfile_open_handle(), kept - and returned - a pointer to the *closed* handle. __dbfile_open_handle()'s caller then ran sqlite3_prepare_v2() on that freed handle (use-after-free; SQLITE_MISUSE / "Database error 21"), and later sqlite3_close()d it again, while the working handle opened during recreation was leaked (~160 KB, "definitely lost" under valgrind). Pass the handle by reference (sqlite3 **) so the reopened handle propagates back to the caller, and bail out cleanly if the reopen fails (the previous code would also have recursed into dbfile_prepare(NULL)). Normal runs happened to read the freed-but-intact memory and passed; valgrind exposes it whenever a hashfile is rebuilt - e.g. opening an older-schema hashfile with a newer duperemove. Co-authored-by: Claude Opus 4.8 <[email protected]>
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.
What this fixes
When an existing hashfile fails
dbfile_check()(a schema-version or hash-type mismatch — e.g. opening an older hashfile with a newerduperemove),dbfile_prepare()takes the "Recreating hashfile" path: itsqlite3_close()s the handle, unlinks the file, reopens a fresh one, and recurses.But the handle was passed by value, so the reopened handle only ever lived in
dbfile_prepare()'s local variable. Its caller,__dbfile_open_handle(), kept — and returned — a pointer to the closed handle.What happens without the fix
__dbfile_open_handle()'s caller then runssqlite3_prepare_v2()on that freed handle — a use-after-free (SQLITE_MISUSE/ "Database error 21") — and latersqlite3_close()s it a second time, while the real working handle opened during recreation is leaked (~160 KB, "definitely lost" under valgrind). Normal runs happen to read the freed-but-intact memory and pass, so it's a latent defect; valgrind surfaces it reliably whenever a hashfile is rebuilt.The fix
Pass the handle by reference (
sqlite3 **) so the freshly-opened handle propagates back to the caller, and bail out cleanly if the reopen fails (the previous code would also have recursed intodbfile_prepare(NULL)).Found via valgrind in the
oansfork, whose strict hashfile branding makes the recreate path easy to hit — but the defect is inherited straight from here.