Fix out-of-bounds read in ValidateSalt for short salts#1230
Open
Hashim1999164 wants to merge 1 commit into
Open
Fix out-of-bounds read in ValidateSalt for short salts#1230Hashim1999164 wants to merge 1 commit into
Hashim1999164 wants to merge 1 commit into
Conversation
ValidateSalt walked the salt pointer and read fixed offsets (salt[1], salt[2]) after only checking the leading '$', so a truncated salt such as "$", "$2b" or "$2b$" caused reads past the terminating NUL byte. Reject these short salts before advancing the pointer: bail out when the version byte is NUL, require the '$' separator after the version, and guard the cost digits against an early NUL. Valid salts are unaffected. Fixes kelektiv#1228.
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
ValidateSaltinsrc/bcrypt_node.cconly checked the leading$and then read fixed offsets (salt[1],salt[2]) while advancing the pointer. A truncated$-prefixed salt such as"$","$2b", or"$2b$"therefore read one or two bytes past the terminating NUL, an out-of-bounds read (CWE-125) reachable fromcompare/compareSync/encrypt/encryptSync.This adds bounds checks so short salts are rejected before the pointer is advanced:
\0(handles"$")$separator after the version (handles"$2b","$2b$")\0before reading the trailing separatorValid salts are unaffected.
Fixes #1228.
Verification
npx node-gyp rebuild && npx jest-> all 76 tests pass, including a newhash_short_salt_prefixregression test intest/sync.test.jsthat feeds truncated salts tohashSync/compareSync.ValidateSaltinto a standalone harness built with-fsanitize=address, using exact-sized heap buffers (so no small-string-optimization slack hides the read):heap-buffer-overflow(READ of size 1) for"$","$2b", and"$2b$"at thesalt[1]/salt[2]reads.falsewith no ASAN error; a valid$2a$10$...salt still validates.Test plan
npx node-gyp rebuildnpx jest(76 passing)Made with Cursor