aws / s2n-tls

An implementation of the TLS/SSL protocols
https://aws.github.io/s2n-tls/usage-guide/
Apache License 2.0
4.53k stars 705 forks source link

Memory leak in s2n_realloc #181

Closed keith-rollin closed 8 years ago

keith-rollin commented 9 years ago

I'm just eyeballing this so perhaps I'm missing something, but the following code in s2n_realloc seems to leak any old buffer in b->data:

    ...
    // Allocate new memory block.
    void *data;
    if (posix_memalign(&data, page_size, allocate)) {
        S2N_ERROR(S2N_ERR_ALLOC);
    }

    // Copy old data to new data. Check the size first so that we don't copy
    // from NULL (may want to test b->data instead?)
    if (b->size) {
        memcpy_check(data, b->data, b->size);
    }

    // Assign new data to `b`. NOTE: OLD b->data NOT CLEANED UP FIRST!
    b->data = data;
    b->size = size;
    b->allocated = allocate;
    ...

This code was introduced in 3a418cc as part of aligning the data buffer to a page size.

If there is a problem here that needs to be fixed, please also note the other reported issue regarding a lack of a call to munlock when freeing a block.

colmmacc commented 9 years ago

Thanks Keith. The memory does leak, but this is intentional for now: at the moment the memory strategy is very inefficient. I'm re-aligning the core s2n_connection struct so that the members can fit in and use a single page and plan on cleaning it all up then.