fix(sysrand): resume getrandom at correct offset after short reads - #104
Open
cryo2010 wants to merge 3 commits into
Open
fix(sysrand): resume getrandom at correct offset after short reads#104cryo2010 wants to merge 3 commits into
cryo2010 wants to merge 3 commits into
Conversation
Extract the short-read offset-accumulation loop from the Linux getrandom path into a reusable fillRandomBytes helper that takes a reader callback. This is behavior-preserving and introduces a testable seam so the partial-read handling can be covered by unit tests (issue cheatfate#103). The test and the accompanying fix will follow in a later commit.
… offset Drive fillRandomBytes with a mock reader that emulates short reads (at most 8 bytes per call). The offset bug causes each call to overwrite the front of the buffer, leaving the tail unwritten while still reporting full success. This test fails against the current code and will pass once the offset fix lands (issue cheatfate#103).
The Linux getrandom fill loop passed the buffer start (pbytes) to the reader on every iteration instead of the current offset (p). After a partial read, subsequent reads overwrote the beginning of the buffer and left the tail uninitialized, while the routine still reported full success. Callers could receive predictable bytes in the tail of keys, nonces, or salts. Pass the resumed offset p to the reader so the whole buffer is filled. Fixes cheatfate#103.
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.
Summary
Fixes #103.
On Linux,
randomBytesfills the caller's buffer by looping overgetrandom(2), which may return fewer bytes than requested for large buffers. The loop computed the resume pointerpbut passed the buffer startpbytesto the syscall on every iteration:After a partial read, subsequent reads overwrote the beginning of the buffer and left the tail untouched, while
resstill accumulated tonbytesso the routine reported full success. Callers could receive predictable (uninitialized) bytes in the tail of keys, nonces, or salts.Changes
fillRandomByteshelper that takes a reader callback. This is behavior-preserving and creates a testable seam (the rawgetrandomloop was previously not unit-testable, since partial reads are nondeterministic).fillRandomByteswith a mock reader emulating short reads (<= 8 bytes/call) and asserts the whole buffer is written.pto the reader so the buffer is filled from the correct position.Testing
nim c -r --path:. tests/testsysrand.nimpasses all cases, including the new "getrandom partial-read offset test". The test fails on the pre-fix code and passes after the one-line fix.