ssl/s3_enc.c:327: error: USE_AFTER_FREE
  call to `EVP_CipherInit_ex()` eventually accesses memory that was invalidated by call to `free()` on line 236 indirectly during the call to `EVP_CIPHER_CTX_reset()`.
Showing all 16 steps of the trace


ssl/s3_enc.c:230:37: invalidation part of the trace starts here
228.           if (s->enc_read_ctx != NULL)
229.               reuse_dd = 1;
230.           else if ((s->enc_read_ctx = EVP_CIPHER_CTX_new()) == NULL)
                                           ^
231.               goto err;
232.           else

ssl/s3_enc.c:230:37: passed as argument to `EVP_CIPHER_CTX_new`
  228.           if (s->enc_read_ctx != NULL)
  229.               reuse_dd = 1;
  230.           else if ((s->enc_read_ctx = EVP_CIPHER_CTX_new()) == NULL)
                                             ^
  231.               goto err;
  232.           else

ssl/s3_enc.c:230:37: return from call to `EVP_CIPHER_CTX_new`
  228.           if (s->enc_read_ctx != NULL)
  229.               reuse_dd = 1;
  230.           else if ((s->enc_read_ctx = EVP_CIPHER_CTX_new()) == NULL)
                                             ^
  231.               goto err;
  232.           else

ssl/s3_enc.c:230:19: assigned
  228.           if (s->enc_read_ctx != NULL)
  229.               reuse_dd = 1;
  230.           else if ((s->enc_read_ctx = EVP_CIPHER_CTX_new()) == NULL)
                           ^
  231.               goto err;
  232.           else

ssl/s3_enc.c:236:13: when calling `EVP_CIPHER_CTX_reset` here
  234.                * make sure it's intialized in case we exit later with an error
  235.                */
  236.               EVP_CIPHER_CTX_reset(s->enc_read_ctx);
                     ^
  237.           dd = s->enc_read_ctx;
  238.   

crypto/evp/evp_enc.c:69:1: parameter `c` of EVP_CIPHER_CTX_reset
    67.   #include "evp_locl.h"
    68.   
    69. > int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *c)
    70.   {
    71.       if (c == NULL)

crypto/evp/evp_enc.c:80:5: when calling `CRYPTO_free` here
    78.               OPENSSL_cleanse(c->cipher_data, c->cipher->ctx_size);
    79.       }
    80.       OPENSSL_free(c->cipher_data);
              ^
    81.   #ifndef OPENSSL_NO_ENGINE
    82.       if (c->engine)

crypto/mem.c:225:1: parameter `str` of CRYPTO_free
      223.   }
      224.   
      225. > void CRYPTO_free(void *str, const char *file, int line)
      226.   {
      227.       if (free_impl != NULL && free_impl != &CRYPTO_free) {

crypto/mem.c:241:5: was invalidated by call to `free()`
      239.       }
      240.   #else
      241.       free(str);
                 ^
      242.   #endif
      243.   }

ssl/s3_enc.c:230:37: use-after-lifetime part of the trace starts here
228.           if (s->enc_read_ctx != NULL)
229.               reuse_dd = 1;
230.           else if ((s->enc_read_ctx = EVP_CIPHER_CTX_new()) == NULL)
                                           ^
231.               goto err;
232.           else

ssl/s3_enc.c:230:37: passed as argument to `EVP_CIPHER_CTX_new`
  228.           if (s->enc_read_ctx != NULL)
  229.               reuse_dd = 1;
  230.           else if ((s->enc_read_ctx = EVP_CIPHER_CTX_new()) == NULL)
                                             ^
  231.               goto err;
  232.           else

ssl/s3_enc.c:230:37: return from call to `EVP_CIPHER_CTX_new`
  228.           if (s->enc_read_ctx != NULL)
  229.               reuse_dd = 1;
  230.           else if ((s->enc_read_ctx = EVP_CIPHER_CTX_new()) == NULL)
                                             ^
  231.               goto err;
  232.           else

ssl/s3_enc.c:230:19: assigned
  228.           if (s->enc_read_ctx != NULL)
  229.               reuse_dd = 1;
  230.           else if ((s->enc_read_ctx = EVP_CIPHER_CTX_new()) == NULL)
                           ^
  231.               goto err;
  232.           else

ssl/s3_enc.c:327:5: when calling `EVP_CipherInit_ex` here
  325.       memcpy(mac_secret, ms, i);
  326.   
  327.       EVP_CipherInit_ex(dd, c, NULL, key, iv, (which & SSL3_CC_WRITE));
             ^
  328.   
  329.   #ifdef OPENSSL_SSL_TRACE_CRYPTO

crypto/mem.c:225:1: parameter `str` of CRYPTO_free
    223.   }
    224.   
    225. > void CRYPTO_free(void *str, const char *file, int line)
    226.   {
    227.       if (free_impl != NULL && free_impl != &CRYPTO_free) {

crypto/mem.c:241:5: invalid access occurs here
    239.       }
    240.   #else
    241.       free(str);
               ^
    242.   #endif
    243.   }

void CRYPTO_free(void *str, const char *file, int line)
{
    if (free_impl != NULL && free_impl != &CRYPTO_free) {
        free_impl(str, file, line);
        return;
    }
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
    if (call_malloc_debug) {
        CRYPTO_mem_debug_free(str, 0, file, line);
        free(str);
        CRYPTO_mem_debug_free(str, 1, file, line);
    } else {
        free(str);
    }
#else
    free(str);
#endif
}
['int ssl3_change_cipher_state(SSL *s, int which)\n{\n    unsigned char *p, *mac_secret;\n    unsigned char exp_key[EVP_MAX_KEY_LENGTH];\n    unsigned char exp_iv[EVP_MAX_IV_LENGTH];\n    unsigned char *ms, *key, *iv;\n    EVP_CIPHER_CTX *dd;\n    const EVP_CIPHER *c;\n#ifndef OPENSSL_NO_COMP\n    COMP_METHOD *comp;\n#endif\n    const EVP_MD *m;\n    int n, i, j, k, cl;\n    int reuse_dd = 0;\n    c = s->s3->tmp.new_sym_enc;\n    m = s->s3->tmp.new_hash;\n    OPENSSL_assert(m);\n#ifndef OPENSSL_NO_COMP\n    if (s->s3->tmp.new_compression == NULL)\n        comp = NULL;\n    else\n        comp = s->s3->tmp.new_compression->method;\n#endif\n    if (which & SSL3_CC_READ) {\n        if (s->enc_read_ctx != NULL)\n            reuse_dd = 1;\n        else if ((s->enc_read_ctx = EVP_CIPHER_CTX_new()) == NULL)\n            goto err;\n        else\n            EVP_CIPHER_CTX_reset(s->enc_read_ctx);\n        dd = s->enc_read_ctx;\n        if (ssl_replace_hash(&s->read_hash, m) == NULL) {\n                SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);\n                goto err2;\n        }\n#ifndef OPENSSL_NO_COMP\n        COMP_CTX_free(s->expand);\n        s->expand = NULL;\n        if (comp != NULL) {\n            s->expand = COMP_CTX_new(comp);\n            if (s->expand == NULL) {\n                SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE,\n                       SSL_R_COMPRESSION_LIBRARY_ERROR);\n                goto err2;\n            }\n            if (!RECORD_LAYER_setup_comp_buffer(&s->rlayer))\n                goto err;\n        }\n#endif\n        RECORD_LAYER_reset_read_sequence(&s->rlayer);\n        mac_secret = &(s->s3->read_mac_secret[0]);\n    } else {\n        if (s->enc_write_ctx != NULL)\n            reuse_dd = 1;\n        else if ((s->enc_write_ctx = EVP_CIPHER_CTX_new()) == NULL)\n            goto err;\n        else\n            EVP_CIPHER_CTX_reset(s->enc_write_ctx);\n        dd = s->enc_write_ctx;\n        if (ssl_replace_hash(&s->write_hash, m) == NULL) {\n                SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);\n                goto err2;\n        }\n#ifndef OPENSSL_NO_COMP\n        COMP_CTX_free(s->compress);\n        s->compress = NULL;\n        if (comp != NULL) {\n            s->compress = COMP_CTX_new(comp);\n            if (s->compress == NULL) {\n                SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE,\n                       SSL_R_COMPRESSION_LIBRARY_ERROR);\n                goto err2;\n            }\n        }\n#endif\n        RECORD_LAYER_reset_write_sequence(&s->rlayer);\n        mac_secret = &(s->s3->write_mac_secret[0]);\n    }\n    if (reuse_dd)\n        EVP_CIPHER_CTX_reset(dd);\n    p = s->s3->tmp.key_block;\n    i = EVP_MD_size(m);\n    if (i < 0)\n        goto err2;\n    cl = EVP_CIPHER_key_length(c);\n    j = cl;\n    k = EVP_CIPHER_iv_length(c);\n    if ((which == SSL3_CHANGE_CIPHER_CLIENT_WRITE) ||\n        (which == SSL3_CHANGE_CIPHER_SERVER_READ)) {\n        ms = &(p[0]);\n        n = i + i;\n        key = &(p[n]);\n        n += j + j;\n        iv = &(p[n]);\n        n += k + k;\n    } else {\n        n = i;\n        ms = &(p[n]);\n        n += i + j;\n        key = &(p[n]);\n        n += j + k;\n        iv = &(p[n]);\n        n += k;\n    }\n    if (n > s->s3->tmp.key_block_length) {\n        SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);\n        goto err2;\n    }\n    memcpy(mac_secret, ms, i);\n    EVP_CipherInit_ex(dd, c, NULL, key, iv, (which & SSL3_CC_WRITE));\n#ifdef OPENSSL_SSL_TRACE_CRYPTO\n    if (s->msg_callback) {\n        int wh = which & SSL3_CC_WRITE ?\n            TLS1_RT_CRYPTO_WRITE : TLS1_RT_CRYPTO_READ;\n        s->msg_callback(2, s->version, wh | TLS1_RT_CRYPTO_MAC,\n                        mac_secret, EVP_MD_size(m), s, s->msg_callback_arg);\n        if (c->key_len)\n            s->msg_callback(2, s->version, wh | TLS1_RT_CRYPTO_KEY,\n                            key, c->key_len, s, s->msg_callback_arg);\n        if (k) {\n            s->msg_callback(2, s->version, wh | TLS1_RT_CRYPTO_IV,\n                            iv, k, s, s->msg_callback_arg);\n        }\n    }\n#endif\n    OPENSSL_cleanse(exp_key, sizeof(exp_key));\n    OPENSSL_cleanse(exp_iv, sizeof(exp_iv));\n    return (1);\n err:\n    SSLerr(SSL_F_SSL3_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);\n err2:\n    OPENSSL_cleanse(exp_key, sizeof(exp_key));\n    OPENSSL_cleanse(exp_iv, sizeof(exp_iv));\n    return (0);\n}', 'int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *c)\n{\n    if (c == NULL)\n        return 1;\n    if (c->cipher != NULL) {\n        if (c->cipher->cleanup && !c->cipher->cleanup(c))\n            return 0;\n        if (c->cipher_data && c->cipher->ctx_size)\n            OPENSSL_cleanse(c->cipher_data, c->cipher->ctx_size);\n    }\n    OPENSSL_free(c->cipher_data);\n#ifndef OPENSSL_NO_ENGINE\n    if (c->engine)\n        ENGINE_finish(c->engine);\n#endif\n    memset(c, 0, sizeof(*c));\n    return 1;\n}', 'void CRYPTO_free(void *str, const char *file, int line)\n{\n    if (free_impl != NULL && free_impl != &CRYPTO_free) {\n        free_impl(str, file, line);\n        return;\n    }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n    if (call_malloc_debug) {\n        CRYPTO_mem_debug_free(str, 0, file, line);\n        free(str);\n        CRYPTO_mem_debug_free(str, 1, file, line);\n    } else {\n        free(str);\n    }\n#else\n    free(str);\n#endif\n}']