Repository navigation
libsql-ffi: Fix sqlean sha256() under gcc 11 and later - #2334
Open
colinhacks wants to merge 2 commits into
Open
colinhacks wants to merge 2 commits into
colinhacks wants to merge 2 commits into
Conversation
sha256_final stored the message length through a sha2_word64 pointer into the byte buffer, a strict-aliasing violation that gcc 11 and later reorder at -O2 and -O3, so sha256() returned the wrong hash. Store it with memcpy, and do the same for the two length writes in sha512's last step.
The workspace test run builds libsql-ffi with multiple-ciphers, which doesn't compile sqlean, so the test can't find sha256().
This branch has not been deployed
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.
With the sqlean crypto extension,
sha256()returned the wrong hash when the C code was built with gcc 11 or later at-O2or-O3.sha256_finalwrote the message length into the byte buffer through asha2_word64*cast. That breaks strict aliasing, and gcc 11+ reorders the store around the final transform.The write now goes through
MEMCPY_BCOPY(the file'smemcpymacro). The two length writes in sha512's last step had the same cast, so they use it too.sha512()was right on every compiler I tried, but it no longer depends on that.Testing
There's no test in the PR. The workspace test run builds libsql-ffi with
multiple-ciphers(bindings/cturns onencryption), which doesn't compile sqlean, and it builds the C code at-O0, where the old code was right anyway.I built
bundled/src/sqlite3.candbundled/sqlean/crypto/*.cwith-DSQLITE_CORE, calledcrypto_init, and compared againstshasum:sha256('a')before-O2767DD50D…(wrong)CA978112…-O3767DD50D…(wrong)CA978112…-O3CA978112…CA978112…With gcc 16
-O3, both hashes matchshasumafter the change for inputs of 0, 55, 56, 64, 111, 112 and 1000 bytes, which cover both padding branches.Fixes #2333