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.1: Fix corner case uses of memory_buffer_alloc.c #1009

Conversation

andresag01
Copy link
Contributor

This change fixes the following corner cases in memory_buffer_alloc.c:

  • Calling verify_chain() on an uninitialized chain. Would previously cause a NULL dereference.
  • Allocating a memory block large enough to overflow the length variable.
  • Initializing the allocator with a buffer smaller than the size of a memory header.

This PR is based on the information recorded in #639.

NOTES:

Copy link
Contributor

@RonEld RonEld left a comment

Choose a reason for hiding this comment

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

Why is this PR has much more changes (in the test code) than #778 ?

@andresag01
Copy link
Contributor Author

@RonEld: It seems that overtime, more tests have been added to memory_buffer_alloc module. However, these were never backported to mbed TLS 2.1 and 1.3. So I backported the tests and also some changes to the generate_code.pl script as the tests were not working correctly because changes to the script werent backported either. I tried to do a similar operation in mbed TLS 1.3, but there are too many changes introduced in the API meaning that it was very difficult to backport the tests, so I did not do that.

@RonEld
Copy link
Contributor

RonEld commented Jul 13, 2017

@andresag01 Thanks for the clarification. In this case, we should have a task to write the tests for 1.3

@andresag01
Copy link
Contributor Author

@RonEld: I have created the new GitHub issue for backporting the tests to mbed TLS 1.3: #1014

@Patater Patater added the needs-review Every commit must be reviewed by at least two team members, label Sep 28, 2017
@simonbutcher
Copy link
Contributor

Ok - the backporting of additional test cases is worthwhile, but it would have been better to do it in a separate PR. PR's should be as simple as they can be, self-contained and aim to change only one feature or function, whether big or small.

However, I'm less keen on backporting test framework changes. You've done that work now, and I see little risk in it, but it's not something I would have planned.

These are mainly observations, and otherwise I approved the PR.

@simonbutcher simonbutcher added needs-design-approval and removed needs-review Every commit must be reviewed by at least two team members, labels Oct 13, 2017
@andresag01
Copy link
Contributor Author

andresag01 commented Oct 17, 2017

@sbutcher-arm @RonEld: Thanks for the reviews! With regards to:

I'm less keen on backporting test framework changes. You've done that work now, and I see little risk in it, but it's not something I would have planned.

I agree that backporting large infrastructure changes is, perhaps, not ideal as it increases the risk of breaking something. However, I like to backport simple changes like this one because in future PRs it is much easier to backport other changes around this area as the development and the maintenance branch has not diverged so much. Just a thought...

@gilles-peskine-arm
Copy link
Contributor

Failure in x509parse-suite (https://jenkins-internal.mbed.com/job/mbedtls-pr/424/execution/node/205/log/). DO NOT MERGE pending investigation.

@@ -294,7 +294,7 @@ int mbedtls_x509_crl_parse_der( mbedtls_x509_crl *chain,
/*
* Copy raw DER-encoded CRL
*/
if( ( p = mbedtls_calloc( 1, buflen ) ) == NULL )
if( buflen != 0 && ( ( p = mbedtls_calloc( 1, buflen ) ) == NULL ) )
return( MBEDTLS_ERR_X509_ALLOC_FAILED );

memcpy( p, buf, buflen );
Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm Nov 24, 2017

Choose a reason for hiding this comment

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

The test suite test_suite_x509parse fails in the clang-asan build (https://jenkins-internal.mbed.com/job/mbedtls-pr/425/execution/node/192/log/). I can reproduce this locally and identified the culprit as the change above.

X509 CRL ASN1 (Incorrect first tag) ............................... /home/gilpes01/z/dev/mbedtls/gate/library/x509_crl.c:300:13: runtime error: null pointer passed as argument 1, which is declared to never be null
/usr/include/string.h:43:28: note: nonnull attribute specified here
SUMMARY: AddressSanitizer: undefined-behavior /home/gilpes01/z/dev/mbedtls/gate/library/x509_crl.c:300:13 in 

This test case calls mbedtls_x509_crl_parse with buflen = 0. This in turn calls mbedtls_x509_crl_parse_der with buflen = 0, and this PR changed the way the function behaves in this case.

It's good that we don't rely on the behavior of calloc with a size of 0, but the way this was fixed, we're now calling memcpy with a size of 0. Calling memcpy with a null pointer and a size of 0 probably works on most platforms, but it's technically undefined behavior, and Clang-Asan rejects it.

I think that memcpy(NULL, buf, 0) is poor practice even if it works, and a rejection from Asan is not good, so I am not merging this PR.

Technically this is a pre-existing bug, since we returned MBEDTLS_ERR_X509_ALLOC_FAILED instead of MBEDTLS_ERR_X509_INVALID_FORMAT on platforms where calloc(…, 0) returns NULL. But I think the best solution would be to add

    if( buflen == 0 )
        return(  MBEDTLS_ERR_X509_INVALID_FORMAT );

near the top of this function and remove the check for buflen != 0 here.

Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm left a comment

Choose a reason for hiding this comment

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

I am rejecting this PR because it causes the Asan check to fail and highlights a bug on exotic platforms. Please fix as described here or however you see fit.

@andresag01
Copy link
Contributor Author

@gilles-peskine-arm: Thanks for pointing this out. I have now fixed the issue.

Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm left a comment

Choose a reason for hiding this comment

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

Approved, but with the same suggested improvement as the main PR: #778 (comment)

@andresag01
Copy link
Contributor Author

Changed the code in x509_crl.c according to the latest review comments.

@gilles-peskine-arm
Copy link
Contributor

gilles-peskine-arm commented Dec 14, 2017

Automatic CI run picked an old version. Running manually: https://jenkins-internal.mbed.com/job/mbedtls-commit-tests/45/ → only failed due to known issues (timing tests on FreeBSD, compat.sh slow start), ok to merge.

Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm left a comment

Choose a reason for hiding this comment

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

PR held due to a test failure after merging into mbedtls-2.1. See #778 (review)

Andres AG and others added 12 commits January 23, 2018 21:03
The corner cases fixed include:
    * Allocating a buffer of size 0. With this change, the allocator now
      returns a NULL pointer in this case. Note that changes in pem.c and
      x509_crl.c were required to fix tests that did not work under this
      assumption.
    * Initialising the allocator with less memory than required for headers.
    * Fix header chain checks for uninitialised allocator.
Adds additional tests to the test suite for
memory_buffer_alloc.c
Added comments to explain purpose and usage of generate_code.pl
Added to generate_code.pl:
    - support for per test suite helper functions
    - description of the structure of the files the script uses to construct
      the test suite file
    - delimiters through the source code to make the machine generated code
      easier to understand
Restructed test suite helper and main code to support tests suite helper
functions, changed C++ comments to C-style, and made the generated
source code more navigable.
@andresag01 andresag01 force-pushed the mbedtls-2.1-iotssl-1033-memory-buffer-alloc branch from ede3f1b to 133ab2c Compare January 23, 2018 21:22
@andresag01
Copy link
Contributor Author

@gilles-peskine-arm: Rebased and fixed reported issue in pkparse.c

Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm left a comment

Choose a reason for hiding this comment

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

  1. I approve the rebasing from my previous approval (pr_1009-1) to af77213 based on the output of interdiff <(git diff mbedtls-2.1...af77213) <(git diff mbedtls-2.1...pr_1009-1) which shows only benign differences.
  2. I approve the one additional commit 133ab2c.
  3. This fixes the build failure Fix corner case uses of memory_buffer_alloc.c #778 (review).

@gilles-peskine-arm gilles-peskine-arm added needs-review Every commit must be reviewed by at least two team members, and removed needs-design-approval runCI labels Mar 9, 2018
Copy link
Contributor

@RonEld RonEld left a comment

Choose a reason for hiding this comment

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

Approving after the rebase and additional changes done after my last review

@gilles-peskine-arm gilles-peskine-arm added approved Design and code approved - may be waiting for CI or backports and removed needs-review Every commit must be reviewed by at least two team members, labels Mar 11, 2018
@gilles-peskine-arm gilles-peskine-arm merged commit 133ab2c into Mbed-TLS:mbedtls-2.1 Mar 13, 2018
paul-elliott-arm added a commit that referenced this pull request Mar 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Design and code approved - may be waiting for CI or backports bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants