-
Notifications
You must be signed in to change notification settings - Fork 96
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
Test the library when malloc(0) returns NULL #264
Conversation
#257 is merged. Restarting CI. |
CI reported failures. I'll fix these and also rebase on top of |
f338f77
to
720e77d
Compare
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.
83e6b39
to
6673464
Compare
The remaining CI failures are unrelated to this PR (random DTLS failures, which is a known thing, and "export keys functionality" in |
There was a problem hiding this 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, sorry!
tests/scripts/all.sh
Outdated
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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
6673464
to
31b0a3c
Compare
CI failures are export keys, to be fixed by other PRs. |
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 wherecalloc
returns NULL when asked to allocate 0 bytes, wrapping around the platformcalloc
.Fix #137: now it's tested.
Should be backported to mbedtls LTS branches.