ssl/s3_enc.c:563: error: USE_AFTER_FREE
  call to `EVP_DigestInit_ex()` eventually accesses memory that was invalidated by call to `free()` on line 573 indirectly during the call to `EVP_DigestInit_ex()`.
Showing all 18 steps of the trace


ssl/s3_enc.c:551:23: invalidation part of the trace starts here
549.       };
550.       unsigned char buf[EVP_MAX_MD_SIZE];
551.       EVP_MD_CTX *ctx = EVP_MD_CTX_new();
                             ^
552.       int i, ret = 0;
553.       unsigned int n;

ssl/s3_enc.c:551:23: passed as argument to `EVP_MD_CTX_new`
  549.       };
  550.       unsigned char buf[EVP_MAX_MD_SIZE];
  551.       EVP_MD_CTX *ctx = EVP_MD_CTX_new();
                               ^
  552.       int i, ret = 0;
  553.       unsigned int n;

ssl/s3_enc.c:551:23: return from call to `EVP_MD_CTX_new`
  549.       };
  550.       unsigned char buf[EVP_MAX_MD_SIZE];
  551.       EVP_MD_CTX *ctx = EVP_MD_CTX_new();
                               ^
  552.       int i, ret = 0;
  553.       unsigned int n;

ssl/s3_enc.c:551:5: assigned
  549.       };
  550.       unsigned char buf[EVP_MAX_MD_SIZE];
  551.       EVP_MD_CTX *ctx = EVP_MD_CTX_new();
             ^
  552.       int i, ret = 0;
  553.       unsigned int n;

ssl/s3_enc.c:573:20: when calling `EVP_DigestInit_ex` here
  571.                   || EVP_DigestFinal_ex(ctx, buf, &n) <= 0
  572.   
  573.                   || EVP_DigestInit_ex(ctx, s->ctx->md5, NULL) <= 0
                            ^
  574.                   || EVP_DigestUpdate(ctx, p, len) <= 0
  575.                   || EVP_DigestUpdate(ctx, buf, n) <= 0

crypto/evp/digest.c:169:1: parameter `ctx` of EVP_DigestInit_ex
    167.   }
    168.   
    169. > int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
    170.   {
    171.       EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);

crypto/evp/digest.c:228:13: when calling `CRYPTO_free` here
    226.       if (ctx->digest != type) {
    227.           if (ctx->digest && ctx->digest->ctx_size)
    228.               OPENSSL_free(ctx->md_data);
                       ^
    229.           ctx->digest = type;
    230.           if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size) {

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:551:23: use-after-lifetime part of the trace starts here
549.       };
550.       unsigned char buf[EVP_MAX_MD_SIZE];
551.       EVP_MD_CTX *ctx = EVP_MD_CTX_new();
                             ^
552.       int i, ret = 0;
553.       unsigned int n;

ssl/s3_enc.c:551:23: passed as argument to `EVP_MD_CTX_new`
  549.       };
  550.       unsigned char buf[EVP_MAX_MD_SIZE];
  551.       EVP_MD_CTX *ctx = EVP_MD_CTX_new();
                               ^
  552.       int i, ret = 0;
  553.       unsigned int n;

ssl/s3_enc.c:551:23: return from call to `EVP_MD_CTX_new`
  549.       };
  550.       unsigned char buf[EVP_MAX_MD_SIZE];
  551.       EVP_MD_CTX *ctx = EVP_MD_CTX_new();
                               ^
  552.       int i, ret = 0;
  553.       unsigned int n;

ssl/s3_enc.c:551:5: assigned
  549.       };
  550.       unsigned char buf[EVP_MAX_MD_SIZE];
  551.       EVP_MD_CTX *ctx = EVP_MD_CTX_new();
             ^
  552.       int i, ret = 0;
  553.       unsigned int n;

ssl/s3_enc.c:563:13: when calling `EVP_DigestInit_ex` here
  561.       }
  562.       for (i = 0; i < 3; i++) {
  563.           if (EVP_DigestInit_ex(ctx, s->ctx->sha1, NULL) <= 0
                     ^
  564.                   || EVP_DigestUpdate(ctx, salt[i],
  565.                                       strlen((const char *)salt[i])) <= 0

crypto/evp/digest.c:169:1: parameter `ctx` of EVP_DigestInit_ex
    167.   }
    168.   
    169. > int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
    170.   {
    171.       EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);

crypto/evp/digest.c:228:13: when calling `CRYPTO_free` here
    226.       if (ctx->digest != type) {
    227.           if (ctx->digest && ctx->digest->ctx_size)
    228.               OPENSSL_free(ctx->md_data);
                       ^
    229.           ctx->digest = type;
    230.           if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size) {

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_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p,\n                                int len)\n{\n    static const unsigned char *salt[3] = {\n#ifndef CHARSET_EBCDIC\n        (const unsigned char *)"A",\n        (const unsigned char *)"BB",\n        (const unsigned char *)"CCC",\n#else\n        (const unsigned char *)"\\x41",\n        (const unsigned char *)"\\x42\\x42",\n        (const unsigned char *)"\\x43\\x43\\x43",\n#endif\n    };\n    unsigned char buf[EVP_MAX_MD_SIZE];\n    EVP_MD_CTX *ctx = EVP_MD_CTX_new();\n    int i, ret = 0;\n    unsigned int n;\n#ifdef OPENSSL_SSL_TRACE_CRYPTO\n    unsigned char *tmpout = out;\n#endif\n    if (ctx == NULL) {\n        SSLerr(SSL_F_SSL3_GENERATE_MASTER_SECRET, ERR_R_MALLOC_FAILURE);\n        return 0;\n    }\n    for (i = 0; i < 3; i++) {\n        if (EVP_DigestInit_ex(ctx, s->ctx->sha1, NULL) <= 0\n                || EVP_DigestUpdate(ctx, salt[i],\n                                    strlen((const char *)salt[i])) <= 0\n                || EVP_DigestUpdate(ctx, p, len) <= 0\n                || EVP_DigestUpdate(ctx, &(s->s3->client_random[0]),\n                                    SSL3_RANDOM_SIZE) <= 0\n                || EVP_DigestUpdate(ctx, &(s->s3->server_random[0]),\n                                    SSL3_RANDOM_SIZE) <= 0\n                || EVP_DigestFinal_ex(ctx, buf, &n) <= 0\n                || EVP_DigestInit_ex(ctx, s->ctx->md5, NULL) <= 0\n                || EVP_DigestUpdate(ctx, p, len) <= 0\n                || EVP_DigestUpdate(ctx, buf, n) <= 0\n                || EVP_DigestFinal_ex(ctx, out, &n) <= 0) {\n            SSLerr(SSL_F_SSL3_GENERATE_MASTER_SECRET, ERR_R_INTERNAL_ERROR);\n            ret = 0;\n            break;\n        }\n        out += n;\n        ret += n;\n    }\n    EVP_MD_CTX_free(ctx);\n#ifdef OPENSSL_SSL_TRACE_CRYPTO\n    if (ret > 0 && s->msg_callback) {\n        s->msg_callback(2, s->version, TLS1_RT_CRYPTO_PREMASTER,\n                        p, len, s, s->msg_callback_arg);\n        s->msg_callback(2, s->version, TLS1_RT_CRYPTO_CLIENT_RANDOM,\n                        s->s3->client_random, SSL3_RANDOM_SIZE,\n                        s, s->msg_callback_arg);\n        s->msg_callback(2, s->version, TLS1_RT_CRYPTO_SERVER_RANDOM,\n                        s->s3->server_random, SSL3_RANDOM_SIZE,\n                        s, s->msg_callback_arg);\n        s->msg_callback(2, s->version, TLS1_RT_CRYPTO_MASTER,\n                        tmpout, SSL3_MASTER_SECRET_SIZE,\n                        s, s->msg_callback_arg);\n    }\n#endif\n    OPENSSL_cleanse(buf, sizeof(buf));\n    return (ret);\n}', 'int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)\n{\n    EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);\n#ifndef OPENSSL_NO_ENGINE\n    if (ctx->engine && ctx->digest && (!type ||\n                                       (type\n                                        && (type->type ==\n                                            ctx->digest->type))))\n        goto skip_to_init;\n    if (type) {\n        if (ctx->engine)\n            ENGINE_finish(ctx->engine);\n        if (impl) {\n            if (!ENGINE_init(impl)) {\n                EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);\n                return 0;\n            }\n        } else\n            impl = ENGINE_get_digest_engine(type->type);\n        if (impl) {\n            const EVP_MD *d = ENGINE_get_digest(impl, type->type);\n            if (!d) {\n                EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);\n                ENGINE_finish(impl);\n                return 0;\n            }\n            type = d;\n            ctx->engine = impl;\n        } else\n            ctx->engine = NULL;\n    } else {\n        if (!ctx->digest) {\n            EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_NO_DIGEST_SET);\n            return 0;\n        }\n        type = ctx->digest;\n    }\n#endif\n    if (ctx->digest != type) {\n        if (ctx->digest && ctx->digest->ctx_size)\n            OPENSSL_free(ctx->md_data);\n        ctx->digest = type;\n        if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size) {\n            ctx->update = type->update;\n            ctx->md_data = OPENSSL_zalloc(type->ctx_size);\n            if (ctx->md_data == NULL) {\n                EVPerr(EVP_F_EVP_DIGESTINIT_EX, ERR_R_MALLOC_FAILURE);\n                return 0;\n            }\n        }\n    }\n#ifndef OPENSSL_NO_ENGINE\n skip_to_init:\n#endif\n    if (ctx->pctx) {\n        int r;\n        r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG,\n                              EVP_PKEY_CTRL_DIGESTINIT, 0, ctx);\n        if (r <= 0 && (r != -2))\n            return 0;\n    }\n    if (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT)\n        return 1;\n    return ctx->digest->init(ctx);\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}']