Topic: [SOLVED] Safe free memory

Hey!

I know wolfSSL gives the ability to override wolfSSL_Free with wolfSSL_SetAllocators.
But i cant understand. Why wolfSSL_Free is defined in this form:

void wolfSSL_Free(void* ptr)

rather than in this:

void wolfSSL_Free(void* ptr, size_t size) //added 'size' parameter

?

My custom function is unabe to determine how many zeroes to write in memory.

What can i do with this?

Thanks for reply:)

Share

Re: [SOLVED] Safe free memory

Hi, it sounds like you are looking for a way to clear allocated buffer with zero's on the free? If you are overriding the allocators for wolfSSL then you could do something like this:

void* my_Malloc(size_t size)
{
    void* ptr;
    ptr = malloc(size + sizeof(size_t));
    if (ptr) {
        *(size_t*)ptr = size;
        ptr += sizeof(size_t);

        memset(ptr, 0, size);
    }
    return ptr;

}
void my_Free(void *ptr)
{
    if (ptr) {
        size_t size = 0;
        ptr -= sizeof(size_t);
        size = *(size_t*)ptr;
        memset(ptr, 0, size);
        free(ptr);
    }
}

wolfSSL_SetAllocators(my_Malloc, my_Free, NULL);

This allocates some extra space at the front and stores the length in it. Let me know if that makes sense or not.

Thanks,
David Garske, wolfSSL

Share

Re: [SOLVED] Safe free memory

dgarske, yeah! Genius idea, it feels a little hacky.

Thank you!

Share

Re: [SOLVED] Safe free memory

You are welcome. Internally we are careful to free any private information using the ForceZero() function. If you find places that you think memory should be cleared please let me know. The ForceZero() function ensures that the compiler doesn't optimize out a memset on a stack variable at the end of a function.

Share

Re: [SOLVED] Safe free memory

Oh yeah, one more thing.

If local variables (i.e. "allocated" on stack) are used during key generation, is it possible to do something with them?

Does wolfSSL have option to zero local variables after their usage?

Though i examined bignum struct implementation and found "dp" pointer field (to hold number data i think). Does it mean even if bignum variable is created statically, it uses dynamic memory all the same?

Share

Re: [SOLVED] Safe free memory

1. It depends on your architecture and where it puts the stack region, but technically if a stack value is used and isn't cleared then it has the potential to be available later. That is why we use ForceZero on any private data after we are done with it.

2. Only the private (sensitive) information is cleared with ForceZero and that is always enabled. We don not have an option to clear all stack variables, but you could change the mp_clear to do a ForceZero and it would clear the dp section of the big integers. Also there is a GCC compiler option in the works for clearing stack. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69976

3. It depends on the math implementation used. We have a "normal" math and "fast" math (USE_FAST_MATH). The fast math uses stack and the normal math uses heap. So only the normal math uses dynamic mem (heap) for the big integer data.

Let me know if you have any other questions,
David Garske, wolfSSL

Share