Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport 2.16: pk.c: Ensure min hash_len in pk_hashlen_helper #4719

Merged

Conversation

nick-child-ibm
Copy link
Contributor

Backport of #4561 . Related to other backport for development_2.x #4718.

Very similar except rather than requiring the given hashlen to be equal to mbedtls_md_get_size( md_info ), it must be at least mbedtls_md_get_size( md_info ). This will not break the current pk tests so no commits were added like they were in #4561 and #4718 to address test failures.

@nick-child-ibm nick-child-ibm changed the title Backport: Ensure min hash_len in pk_hashlen_helper Backport: pk.c: Ensure min hash_len in pk_hashlen_helper Jun 23, 2021
@yanesca yanesca added Community needs-review Every commit must be reviewed by at least two team members, needs-reviewer This PR needs someone to pick it up for review size-s Estimated task size: small (~2d) labels Jun 24, 2021
@gilles-peskine-arm gilles-peskine-arm changed the title Backport: pk.c: Ensure min hash_len in pk_hashlen_helper Backport 2.16: pk.c: Ensure min hash_len in pk_hashlen_helper Jun 28, 2021
@gilles-peskine-arm gilles-peskine-arm self-requested a review June 28, 2021 08:57
return( 0 );

if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
return( -1 );

if ( *hash_len != 0 && *hash_len < mbedtls_md_get_size( md_info ) )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This guarantees that signature functions won't do a buffer overread when called with hash_len = size of the hash buffer and hash_len is less than the size of the hash indicated by md_alg. But when hash_len is larger, the last bytes are still ignored. So even in an LTS, it might make sense to go all the way?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the concern carried over from #4561 was that many current implementations and even the mbedtls test suites allocate MBEDTLS_MD_MAX_SIZE bytes for the hash buffer and for the hash_len. If we required the exact expected hash length then all of these use cases would fail and need to be updated. Let me know what you think.

The function `pk_hashlen_helper` exists to ensure a valid hash_len is
used in pk_verify and pk_sign functions. This function has been
used to adjust to the corrsponding hash_len if the user passes in 0
for the hash_len argument based on the md algorithm given. If the user
does not pass in 0 as the hash_len, then it is not adjusted. This is
problematic if the user gives a hash_len and hash buffer that is less than the
associated length of the md algorithm. This error would go unchecked
and eventually lead to buffer overread when given to specific pk_sign/verify
functions, since they both ignore the hash_len argument if md_alg is not
MBEDTLS_MD_NONE.

This commit, adds a conditional to `pk_hashlen_helper` so that an
error is thrown if the user specifies a hash_length (not 0) and it is
less than the expected for the associated message digest algorithm.
This aligns better with the api documentation where it states "If
hash_len is 0, then the length associated with md_alg is used instead,
or an error returned if it is invalid"

Signed-off-by: Nick Child <nick.child@ibm.com>
Signed-off-by: Nayna Jain <nayna@linux.ibm.com>
@nick-child-ibm
Copy link
Contributor Author

Rebased to update changelog. Request made from #4718 comment

Copy link
Contributor

@TRodziewicz TRodziewicz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@gilles-peskine-arm gilles-peskine-arm merged commit 90e6c24 into Mbed-TLS:mbedtls-2.16 Jun 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-review Every commit must be reviewed by at least two team members, needs-reviewer This PR needs someone to pick it up for review size-s Estimated task size: small (~2d)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants