Skip to content

Test the library when malloc(0) returns NULL #264

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

Merged
merged 2 commits into from
Sep 25, 2019

Conversation

gilles-peskine-arm
Copy link
Collaborator

@gilles-peskine-arm gilles-peskine-arm commented Sep 17, 2019

On our main test platforms (Linux, FreeBSD), malloc(0) returns a non-NULL pointer. With MBEDTLS_MEMORY_BUFFER_ALLOC_C, calloc(0,_) does return NULL, but memory_buffer_alloc doesn't play well with UBSan. Add a test component with UBSan where calloc returns NULL when asked to allocate 0 bytes, wrapping around the platform calloc.

Fix #137: now it's tested.

Should be backported to mbedtls LTS branches.

@gilles-peskine-arm gilles-peskine-arm added enhancement New feature or request needs: preceding PR Requires another PR to be merged first needs: review The pull request is ready for review. This generally means that it has no known issues. labels Sep 17, 2019
@gilles-peskine-arm gilles-peskine-arm removed the needs: preceding PR Requires another PR to be merged first label Sep 19, 2019
@Patater
Copy link
Contributor

Patater commented Sep 19, 2019

#257 is merged. Restarting CI.

@gilles-peskine-arm gilles-peskine-arm added needs: work The pull request needs rework before it can be merged. and removed needs: review The pull request is ready for review. This generally means that it has no known issues. labels Sep 19, 2019
@gilles-peskine-arm
Copy link
Collaborator Author

CI reported failures. I'll fix these and also rebase on top of development now that the #257 has been merged.

Add a very basic test of calloc to the selftest program. The selftest
program acts in its capacity as a platform compatibility checker rather
than in its capacity as a test of the library.

The main objective is to report whether calloc returns NULL for a size
of 0. Also observe whether a free/alloc sequence returns the address
that was just freed and whether a size overflow is properly detected.
@Patater Patater added the needs: review The pull request is ready for review. This generally means that it has no known issues. label Sep 23, 2019
@gilles-peskine-arm gilles-peskine-arm removed the needs: work The pull request needs rework before it can be merged. label Sep 23, 2019
@gilles-peskine-arm
Copy link
Collaborator Author

The remaining CI failures are unrelated to this PR (random DTLS failures, which is a known thing, and "export keys functionality" in ssl-opt.sh which is unrelated but I wouldn't have expected to fail here since the crypto CI uses the latest mbedtls).

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.

Please remove the unneeded unset.

component_test_malloc_0_null () {
msg "build: malloc(0) returns NULL (ASan+UBSan build)"
scripts/config.pl full
scripts/config.pl unset MBEDTLS_MEMORY_BUFFER_ALLOC_C
Copy link
Contributor

Choose a reason for hiding this comment

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

Since Mbed-TLS/mbedtls#2469 you don't have to do that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I know, but that's only in mbedtls. The change isn't in mbed-crypto yet and this PR is for mbed-crypto.

Copy link
Contributor

Choose a reason for hiding this comment

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

Right, sorry!

AndrzejKurek
AndrzejKurek previously approved these changes Sep 23, 2019
msg "selftest: malloc(0) returns NULL (ASan+UBSan build)"
# By default ASan terminates the process if called with a size that's
# insanely large. We do this deliberately in selftest, so disable this
# behavior.
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't call calloc with a size that is extremely large anymore, so this comment needs updating.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I amended the offending commit (which was the last one).

Exercise the library functions with calloc returning NULL for a size
of 0. Make this a separate job with UBSan (and ASan) to detect
places where we try to dereference the result of calloc(0) or to do
things like

    buf = calloc(size, 1);
    if (buf == NULL && size != 0) return INSUFFICIENT_MEMORY;
    memcpy(buf, source, size);

which has undefined behavior when buf is NULL at the memcpy call even
if size is 0.

This is needed because other test components jobs either use the system
malloc which returns non-NULL on Linux and FreeBSD, or the
memory_buffer_alloc malloc which returns NULL but does not give as
useful feedback with ASan (because the whole heap is a single C
object).
@Patater
Copy link
Contributor

Patater commented Sep 25, 2019

CI failures are export keys, to be fixed by other PRs.

@Patater Patater merged commit 5a627c5 into ARMmbed:development Sep 25, 2019
@Patater Patater added needs: backports Needs backports to Mbed TLS branches and removed needs: review The pull request is ready for review. This generally means that it has no known issues. labels Sep 25, 2019
@Patater Patater removed the needs: backports Needs backports to Mbed TLS branches label Oct 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

different behavior in calloc on different libraries when trying to allocate 0 bytes
3 participants