Repository navigation
Conversation
libsodium is called with the GIL released, so concurrent update() calls on a shared blake2b state race on its buffer length inside blake2b_update and can write past the end of the state buffer. Hold a per-state lock around update, final and copy, as hashlib does.
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.
generichash_blake2b_update,generichash_blake2b_finalandBlake2State.copypass the shared state buffer to libsodium without any locking, and cffi releases the GIL for the call. When several threads callupdate()on onenacl.hashlib.blake2bobject they race onS->bufleninsideblake2b_update, and once it goes past 256 the nextmemcpy(S->buf + left, in, inlen)writes beyond the end of the state buffer. Eight threads each feeding 100 KB chunks into one shared object segfault CPython 3.13 on every run, while stdlibhashlib.blake2breturns the correct digest for the same workload because it locks each hash object. Free-threaded 3.14t is affected the same way.The fix adds a per-state
threading.LocktoBlake2Stateand holds it around the update, final and copy calls, sonacl.hashlib.blake2band direct users of the bindings are both covered. The new test hashes from 8 threads and compares the result withhashlib. It crashes on main and passes with the fix on 3.13 and 3.14t.