OpenSSL_add_all_algorithms() isn't very suitable for library use. As a 
library using OpenSSL doesn't know whether OpenSSL_add_all_algorithms() 
has been called it must call it itself. As multiple independent 
libraries may be using OpenSSL in a similar way 
OpenSSL_add_all_algorithms() (and similar) may be called multiple times. 
Every time OpenSSL_add_all_algorithms() is called it increases the 
amount of memory the process uses. It's not a classic memory leak as 
EVP_cleanup() will properly de-allocate all the memory however for all 
practical purposes it acts as a memory leak as the heap will grow every 
time a library initializes.

It would also be nice if libraries could safely make a corresponding 
call to EVP_cleanup(). This could be done by ref counting the 
initialization/deinitialization and only doing the actual 
allocation/free once.

A small program demonstrating the problem:

#include <openssl/evp.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
        int i, count;

        if (argc != 2) {
                fprintf(stderr, "Usage: %s <count>\n", argv[0]);
                exit(EXIT_FAILURE);
        }

        count = atoi(argv[1]);

        for (i = 0; i < count; i++) {
                ERR_load_crypto_strings();
                OpenSSL_add_all_algorithms();
        }

        EVP_cleanup();

        abort();

         return(0);
}

# export LD_PRELOAD=libumem.so.1
# export UMEM_DEBUG=default
# ./openssl_mem 1
zsh: IOT instruction (core dumped)  ./openssl_mem 1
# ./openssl_mem 100
zsh: IOT instruction (core dumped)  ./openssl_mem 100
# ./openssl_mem 10000
zsh: IOT instruction (core dumped)  ./openssl_mem 10000
# ./openssl_mem 1000000
zsh: IOT instruction (core dumped)  ./openssl_mem 1000000
# pmap  /var/cores/openssl_mem* |grep total
  total      3664K
  total      4048K
  total     30688K
  total   2753532K
# for a in /var/cores/openssl_mem.200*; do echo "::findleaks" | mdb $a; done
findleaks: no memory leaks detected
findleaks: no memory leaks detected
findleaks: no memory leaks detected
findleaks: no memory leaks detected

______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
Development Mailing List                       [email protected]
Automated List Manager                           [email protected]

Reply via email to