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

Fix using of uninitialized buffer "t" #2942

Closed
wants to merge 2 commits into from

Conversation

svpcom
Copy link

@svpcom svpcom commented Nov 29, 2019

Status

READY

Requires Backporting

Yes

Which branch?
All branches

Migrations

NO

Additional comments

Security fix

@AndrzejKurek AndrzejKurek self-assigned this Nov 29, 2019
@AndrzejKurek AndrzejKurek self-requested a review November 29, 2019 11:18
Copy link
Contributor

@AndrzejKurek AndrzejKurek left a comment

Choose a reason for hiding this comment

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

Hello, @svpcom, and thanks for the contribution!
Unfortunately, bzero that you've used is Unix-specific, and we're targeting multiple other platforms. You could consider using mbedtls_platform_zeroize instead, but could you please elaborate first about the details of this fix?
When exactly is t used as an uninitialized buffer? What are the preconditions?
Thanks in advance.

@svpcom
Copy link
Author

svpcom commented Nov 29, 2019

In mbedtls_hkdf_expand there are unsigned char t[MBEDTLS_MD_MAX_SIZE];
in loop:

/*
     * Compute T = T(1) | T(2) | T(3) | ... | T(N)
     * Where T(N) is defined in RFC 5869 Section 2.3
     */
    for( i = 1; i <= n; i++ )
    {

HMAC read data from uninitialized t at the first iteration of loop

ret = mbedtls_md_hmac_update( &ctx, t, t_len );

So T(1) can be undefined if compiler doesn't zeroize t

Copy link
Contributor

@Patater Patater left a comment

Choose a reason for hiding this comment

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

Could you please add a regression test? We test with ASan and would expect a failure there if we provide the right test case.

@@ -93,7 +93,7 @@ int mbedtls_hkdf_expand( const mbedtls_md_info_t *md, const unsigned char *prk,
int ret = 0;
mbedtls_md_context_t ctx;
unsigned char t[MBEDTLS_MD_MAX_SIZE];
bzero(t, sizeof(t));
mbedtls_platform_zeroize(t, sizeof(t));
Copy link
Contributor

Choose a reason for hiding this comment

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

memset() should be fine here, since t doesn't contain secrets at this point.

@Patater
Copy link
Contributor

Patater commented Nov 29, 2019

For the mbedtls-2.16 and mbedtls-2.7 branches, we need a contributor license agreement (CLA) in order to take onboard your fix.

If this is a personal contribution, the easiest way to do this is if you create an mbed account and accept this click through agreement. Alternatively, you can find a slightly different agreement to sign here, which can be signed and returned to us, and is applicable if you don't want to create an mbed account or alternatively if this is a corporate contribution.

Thanks!

@Patater Patater added CLA requested component-crypto Crypto primitives and low-level interfaces needs-backports Backports are missing or are pending review and approval. needs-ci Needs to pass CI tests needs-review Every commit must be reviewed by at least two team members, needs-work labels Nov 29, 2019
@AndrzejKurek
Copy link
Contributor

AndrzejKurek commented Nov 29, 2019

@svpcom, looking at the first iteration, mbedtls_md_hmac_update is called with t_len equal to 0.
Implementations shouldn't access a buffer of zero length, and if they do, accessing uninitialized data is just the smaller of their problems - accessing data out of bounds is worse (in my opinion).

Thanks to @yanesca for also taking a look at this.

@danh-arm
Copy link
Contributor

danh-arm commented Apr 1, 2020

Hi @svpcom. It seems this PR stalled since earlier reviewers think there is not an issue here. If you still think there's an issue then please comment otherwise we will close.

Also note that we now require a "Signed-off-by:" line in contributions so if you want to want to submit a reworked PR, please apply this.

Thanks, Dan.

gilles-peskine-arm added a commit to gilles-peskine-arm/mbedtls that referenced this pull request Apr 2, 2020
t is never used uninitialized, since the first loop iteration reads 0
bytes of it and then writes hash_len bytes, and subsequent iterations
read and write hash_len bytes. However this is somewhat fragile, and
it would be legitimate for a static analyzer to be unsure.

Initialize t explicitly, to make the code clearer and more robust, at
negligible cost.

Reported by Vasily Evseenko in
Mbed-TLS#2942
with a slightly different fix.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
@gilles-peskine-arm
Copy link
Contributor

Thank you for reporting this! As Andrzej notes, there's no bug: the part of t that gets read is always initialized. However it would be more robust to do it explicitly. In the interest of moving on, I made a slightly different fix at #3160 which supersedes this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component-crypto Crypto primitives and low-level interfaces needs-backports Backports are missing or are pending review and approval. needs-ci Needs to pass CI tests needs-review Every commit must be reviewed by at least two team members, needs-work
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants