Skip to content

Conversation

@gilles-peskine-arm
Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm commented Feb 11, 2020

Side-port changes made in mbed-crypto under programs/. The goal of this pull request is to bring in bug fixes and enhancements made in mbed-crypto to files that exist both in mbed-crypto and in mbedtls. The goal of this pull request is not to be exhaustive, though it is close apart from the build scripts, generated files, and references to files that only exist on one side.

git diff --diff-filter=M HEAD mbed-crypto/development -- programs ':(exclude)programs/test/query_config.c'

(excluding a very long automatically generated file) is now 576 lines long, which approaches something manageable.

gilles-peskine-arm and others added 10 commits February 11, 2020 19:26
This makes it easier to add or remove programs as well as see which
programs were added or removed in diffs.

Side port of 30fae8e in mbed-crypto.
There is some commented out X.509 certificate writing code present in
rsa_genkey. It looks like it has been commented out since the beginning
of time. Let's remove it, since commented out code is not in good style.
As the SSL programs, like ssl_client2 and ssl_server2, are dependent on
SSL and therefore about to be removed, the only consumer of query_config
is the query_compile_time_config test. As such, it makes sense to move
query_config to be next to what uses it.
When building with CMake, for sample programs that only use
functionality in libmbedcrypto (i.e. crypto and platform), link with
libmbedcrypto, not with libmbedtls.

This doesn't change the result, because the linker skips libraries in
which no symbol is used, but it changes the build dependencies, and it
has the advantage of bringing programs/*/CMakeLists.txt closer to the
corresponding files under crypto/.

The programs concerned are crypto sample and test programs, and
programs that only use (potential) platform functions such as
mbedtls_printf. dh_client and dh_server keep linking with mbedtls
because they use functions from the net_sockets module.
When building with CMake, for sample programs that only use
functionality in libmbedcrypto and libmbedx509, link with
libmbedx509, not with libmbedtls.

cert_app makes a TLS connection, so do link it with libmbedtls.
You can't reuse a CTR_DRBG context without free()ing it and
re-init()ing. This generally happened to work, but was never
guaranteed. It could have failed with alternative implementations of
the AES module because mbedtls_ctr_drbg_seed() calls
mbedtls_aes_init() on a context which is already initialized if
mbedtls_ctr_drbg_seed() hasn't been called before, plausibly causing a
memory leak. Calling free() and seed() with no intervening init fails
when MBEDTLS_THREADING_C is enabled and all-bits-zero is not a valid
mutex representation. So add the missing free() and init().
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.
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).
@gilles-peskine-arm gilles-peskine-arm added enhancement mbed TLS team component-crypto Crypto primitives and low-level interfaces component-platform Portability layer and build scripts needs-ci Needs to pass CI tests labels Feb 11, 2020
@mpg
Copy link
Contributor

mpg commented Feb 14, 2020

[git diff programs] is now 576 lines long, which approaches something manageable.

I think this figure makes things look less encouraging that they are, by counting context lines. Using git diff stat makes things look brighter: 9 files changed, 49 insertions(+), 241 deletions(-) and most of the time we'll only care about additions (even though we still need to review deletions to be sure).

Copy link
Contributor

@mpg mpg 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 added needs-review Every commit must be reviewed by at least two team members, and removed needs-ci Needs to pass CI tests labels Feb 17, 2020
Copy link
Contributor

@yanesca yanesca left a comment

Choose a reason for hiding this comment

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

LGTM.

@yanesca yanesca 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 Feb 24, 2020
@yanesca yanesca merged commit 6fc816a into Mbed-TLS:development Feb 24, 2020
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 component-crypto Crypto primitives and low-level interfaces component-platform Portability layer and build scripts enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants