Validate siglen against modulus_len in PSS decode - #807
Open
afonsojanu wants to merge 1 commit into
Open
Conversation
ltc_pkcs_1_pss_decode_mgf1 checked saltlen and modulus_len but never checked that siglen actually matched modulus_len. It then copies modulus_len - 1 bytes out of sig based on modulus_len alone, so a caller passing a shorter buffer (as pkcs_1_pss_decode does, unlike rsa_verify_hash_v2 which happens to validate this indirectly) reads past the end of it. Fixes libtom#802
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.
Fixes #802.
ltc_pkcs_1_pss_decode_mgf1checkssaltlenandmodulus_lenbut never checks thatsiglenactually matchesmodulus_len. Later on it does:which copies
modulus_len - 1bytes total out ofsig, based purely onmodulus_len. Ifsiglen < modulus_len, this reads past the end of the caller's buffer.rsa_verify_hash_v2happens to avoid triggering this today, because it always passes a buffer of exactlymodulus_len(ormodulus_len - 1whenmodulus_bitlen % 8 == 1, handled via its ownx - 1adjustment) into the decode call. Butltc_pkcs_1_pss_decode_mgf1is also reachable directly through the public (deprecated)pkcs_1_pss_decode()API, which forwards whateversiglenthe caller passes with no validation at all — so a caller of that API supplying a genuinely truncated signature buffer hits the out-of-bounds read.Fix: add
siglen != modulus_lento the existing size-checkif, matching the suggested fix in the issue.Added a regression test in
tests/pkcs_1_pss_test.cthat callspkcs_1_pss_decode()directly with a signature one byte shorter than the modulus and asserts it's rejected withCRYPT_PK_INVALID_SIZErather than being processed. Verified with git-stash: reverting only the fix inpkcs_1_pss_decode.cmakes the new assertion fail (on the very first PSS test vector) while everything else still passes; with the fix restored, the full suite passes (SUCCESS: passed=30 failed=0 nop=1).Built and tested locally against LibTomMath (
USE_LTM/LTM_DESC) on macOS/arm64.