ssl/tls13_enc.c:819: error: USE_AFTER_FREE
  call to `EVP_DigestInit_ex()` eventually accesses memory that was invalidated by call to `free()` on line 818 indirectly during the call to `EVP_DigestFinal_ex()`.
Showing all 22 steps of the trace


ssl/tls13_enc.c:785:23: invalidation part of the trace starts here
783.       unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
784.       const EVP_MD *md;
785.       EVP_MD_CTX *ctx = EVP_MD_CTX_new();
                             ^
786.       unsigned int hashsize, datalen;
787.       int ret = 0;

ssl/tls13_enc.c:785:23: passed as argument to `EVP_MD_CTX_new`
  783.       unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
  784.       const EVP_MD *md;
  785.       EVP_MD_CTX *ctx = EVP_MD_CTX_new();
                               ^
  786.       unsigned int hashsize, datalen;
  787.       int ret = 0;

ssl/tls13_enc.c:785:23: return from call to `EVP_MD_CTX_new`
  783.       unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
  784.       const EVP_MD *md;
  785.       EVP_MD_CTX *ctx = EVP_MD_CTX_new();
                               ^
  786.       unsigned int hashsize, datalen;
  787.       int ret = 0;

ssl/tls13_enc.c:785:5: assigned
  783.       unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
  784.       const EVP_MD *md;
  785.       EVP_MD_CTX *ctx = EVP_MD_CTX_new();
             ^
  786.       unsigned int hashsize, datalen;
  787.       int ret = 0;

ssl/tls13_enc.c:818:16: when calling `EVP_DigestFinal_ex` here
  816.       if (EVP_DigestInit_ex(ctx, md, NULL) <= 0
  817.               || EVP_DigestUpdate(ctx, context, contextlen) <= 0
  818.               || EVP_DigestFinal_ex(ctx, hash, &hashsize) <= 0
                        ^
  819.               || EVP_DigestInit_ex(ctx, md, NULL) <= 0
  820.               || EVP_DigestFinal_ex(ctx, data, &datalen) <= 0

crypto/evp/digest.c:286:1: parameter `ctx` of EVP_DigestFinal_ex
    284.   
    285.   /* The caller can assume that this removes any secret data from the context */
    286. > int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *isize)
    287.   {
    288.       int ret;

crypto/evp/digest.c:310:5: when calling `EVP_MD_CTX_reset` here
    308.       }
    309.   
    310.       EVP_MD_CTX_reset(ctx);
               ^
    311.   
    312.       return ret;

crypto/evp/digest.c:20:1: parameter `ctx` of EVP_MD_CTX_reset
      18.   
      19.   /* This call frees resources associated with the context */
      20. > int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)
      21.   {
      22.       if (ctx == NULL)

crypto/evp/digest.c:59:9: when calling `EVP_PKEY_CTX_free` here
      57.        */
      58.       if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))
      59.           EVP_PKEY_CTX_free(ctx->pctx);
                    ^
      60.   #ifndef OPENSSL_NO_ENGINE
      61.       ENGINE_finish(ctx->engine);

crypto/evp/pmeth_lib.c:352:1: parameter `ctx` of EVP_PKEY_CTX_free
        350.   }
        351.   
        352. > void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
        353.   {
        354.       if (ctx == NULL)

crypto/evp/pmeth_lib.c:363:5: when calling `CRYPTO_free` here
        361.       ENGINE_finish(ctx->engine);
        362.   #endif
        363.       OPENSSL_free(ctx);
                   ^
        364.   }
        365.   

crypto/mem.c:295:1: parameter `str` of CRYPTO_free
          293.   }
          294.   
          295. > void CRYPTO_free(void *str, const char *file, int line)
          296.   {
          297.       INCREMENT(free_count);

crypto/mem.c:312:5: was invalidated by call to `free()`
          310.       }
          311.   #else
          312.       free(str);
                     ^
          313.   #endif
          314.   }

ssl/tls13_enc.c:785:23: use-after-lifetime part of the trace starts here
783.       unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
784.       const EVP_MD *md;
785.       EVP_MD_CTX *ctx = EVP_MD_CTX_new();
                             ^
786.       unsigned int hashsize, datalen;
787.       int ret = 0;

ssl/tls13_enc.c:785:23: passed as argument to `EVP_MD_CTX_new`
  783.       unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
  784.       const EVP_MD *md;
  785.       EVP_MD_CTX *ctx = EVP_MD_CTX_new();
                               ^
  786.       unsigned int hashsize, datalen;
  787.       int ret = 0;

ssl/tls13_enc.c:785:23: return from call to `EVP_MD_CTX_new`
  783.       unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
  784.       const EVP_MD *md;
  785.       EVP_MD_CTX *ctx = EVP_MD_CTX_new();
                               ^
  786.       unsigned int hashsize, datalen;
  787.       int ret = 0;

ssl/tls13_enc.c:785:5: assigned
  783.       unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
  784.       const EVP_MD *md;
  785.       EVP_MD_CTX *ctx = EVP_MD_CTX_new();
             ^
  786.       unsigned int hashsize, datalen;
  787.       int ret = 0;

ssl/tls13_enc.c:819:16: when calling `EVP_DigestInit_ex` here
  817.               || EVP_DigestUpdate(ctx, context, contextlen) <= 0
  818.               || EVP_DigestFinal_ex(ctx, hash, &hashsize) <= 0
  819.               || EVP_DigestInit_ex(ctx, md, NULL) <= 0
                        ^
  820.               || EVP_DigestFinal_ex(ctx, data, &datalen) <= 0
  821.               || !tls13_hkdf_expand(s, md, s->early_exporter_master_secret,

crypto/evp/digest.c:102:1: parameter `ctx` of EVP_DigestInit_ex
    100.   }
    101.   
    102. > int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
    103.   {
    104.       EVP_MD *provmd;

crypto/evp/digest.c:250:13: when calling `EVP_PKEY_CTX_ctrl` here
    248.       if (ctx->pctx) {
    249.           int r;
    250.           r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG,
                       ^
    251.                                 EVP_PKEY_CTRL_DIGESTINIT, 0, ctx);
    252.           if (r <= 0 && (r != -2))

crypto/evp/pmeth_lib.c:366:1: parameter `ctx` of EVP_PKEY_CTX_ctrl
      364.   }
      365.   
      366. > int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
      367.                         int cmd, int p1, void *p2)
      368.   {

crypto/evp/pmeth_lib.c:393:11: invalid access occurs here
      391.   
      392.    doit:
      393.       ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
                       ^
      394.   
      395.       if (ret == -2)

int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
                      int cmd, int p1, void *p2)
{
    int ret;
    if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) {
        EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
        return -2;
    }
    if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
        return -1;
    if (ctx->pmeth->digest_custom != NULL)
        goto doit;
    if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
        EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);
        return -1;
    }
    if ((optype != -1) && !(ctx->operation & optype)) {
        EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);
        return -1;
    }
 doit:
    ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
    if (ret == -2)
        EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
    return ret;
}
['int tls13_export_keying_material_early(SSL *s, unsigned char *out, size_t olen,\n                                       const char *label, size_t llen,\n                                       const unsigned char *context,\n                                       size_t contextlen)\n{\n    static const unsigned char exporterlabel[] = "exporter";\n    unsigned char exportsecret[EVP_MAX_MD_SIZE];\n    unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];\n    const EVP_MD *md;\n    EVP_MD_CTX *ctx = EVP_MD_CTX_new();\n    unsigned int hashsize, datalen;\n    int ret = 0;\n    const SSL_CIPHER *sslcipher;\n    if (ctx == NULL || !ossl_statem_export_early_allowed(s))\n        goto err;\n    if (!s->server && s->max_early_data > 0\n            && s->session->ext.max_early_data == 0)\n        sslcipher = SSL_SESSION_get0_cipher(s->psksession);\n    else\n        sslcipher = SSL_SESSION_get0_cipher(s->session);\n    md = ssl_md(sslcipher->algorithm2);\n    if (EVP_DigestInit_ex(ctx, md, NULL) <= 0\n            || EVP_DigestUpdate(ctx, context, contextlen) <= 0\n            || EVP_DigestFinal_ex(ctx, hash, &hashsize) <= 0\n            || EVP_DigestInit_ex(ctx, md, NULL) <= 0\n            || EVP_DigestFinal_ex(ctx, data, &datalen) <= 0\n            || !tls13_hkdf_expand(s, md, s->early_exporter_master_secret,\n                                  (const unsigned char *)label, llen,\n                                  data, datalen, exportsecret, hashsize, 0)\n            || !tls13_hkdf_expand(s, md, exportsecret, exporterlabel,\n                                  sizeof(exporterlabel) - 1, hash, hashsize,\n                                  out, olen, 0))\n        goto err;\n    ret = 1;\n err:\n    EVP_MD_CTX_free(ctx);\n    return ret;\n}', 'int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *isize)\n{\n    int ret;\n    size_t size = 0;\n    if (ctx->digest == NULL || ctx->digest->prov == NULL)\n        goto legacy;\n    if (ctx->digest->dfinal == NULL) {\n        EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR);\n        return 0;\n    }\n    ret = ctx->digest->dfinal(ctx->provctx, md, &size);\n    if (isize != NULL) {\n        if (size <= UINT_MAX) {\n            *isize = (int)size;\n        } else {\n            EVPerr(EVP_F_EVP_DIGESTFINAL_EX, EVP_R_FINAL_ERROR);\n            ret = 0;\n        }\n    }\n    EVP_MD_CTX_reset(ctx);\n    return ret;\n legacy:\n    OPENSSL_assert(ctx->digest->md_size <= EVP_MAX_MD_SIZE);\n    ret = ctx->digest->final(ctx, md);\n    if (isize != NULL)\n        *isize = ctx->digest->md_size;\n    if (ctx->digest->cleanup) {\n        ctx->digest->cleanup(ctx);\n        EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);\n    }\n    OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);\n    return ret;\n}', 'int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)\n{\n    if (ctx == NULL)\n        return 1;\n    if (ctx->digest == NULL || ctx->digest->prov == NULL)\n        goto legacy;\n    if (ctx->provctx != NULL) {\n        if (ctx->digest->freectx != NULL)\n            ctx->digest->freectx(ctx->provctx);\n        ctx->provctx = NULL;\n        EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);\n    }\n    if (ctx->pctx != NULL)\n        goto legacy;\n    return 1;\n legacy:\n    if (ctx->digest && ctx->digest->cleanup\n        && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_CLEANED))\n        ctx->digest->cleanup(ctx);\n    if (ctx->digest && ctx->digest->ctx_size && ctx->md_data\n        && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE)) {\n        OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);\n    }\n    if (!EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX))\n        EVP_PKEY_CTX_free(ctx->pctx);\n#ifndef OPENSSL_NO_ENGINE\n    ENGINE_finish(ctx->engine);\n#endif\n    OPENSSL_cleanse(ctx, sizeof(*ctx));\n    return 1;\n}', 'void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)\n{\n    if (ctx == NULL)\n        return;\n    if (ctx->pmeth && ctx->pmeth->cleanup)\n        ctx->pmeth->cleanup(ctx);\n    EVP_PKEY_free(ctx->pkey);\n    EVP_PKEY_free(ctx->peerkey);\n#ifndef OPENSSL_NO_ENGINE\n    ENGINE_finish(ctx->engine);\n#endif\n    OPENSSL_free(ctx);\n}', 'void CRYPTO_free(void *str, const char *file, int line)\n{\n    INCREMENT(free_count);\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}', 'int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)\n{\n    EVP_MD *provmd;\n    ENGINE *tmpimpl = NULL;\n    EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);\n#ifndef OPENSSL_NO_ENGINE\n    if (ctx->engine && ctx->digest &&\n        (type == NULL || (type->type == ctx->digest->type)))\n        goto skip_to_init;\n    if (type != NULL && impl == NULL)\n        tmpimpl = ENGINE_get_digest_engine(type->type);\n#endif\n    if (ctx->engine != NULL\n            || impl != NULL\n            || tmpimpl != NULL\n            || ctx->pctx != NULL\n            || (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) != 0) {\n        if (ctx->digest == ctx->fetched_digest)\n            ctx->digest = NULL;\n        EVP_MD_meth_free(ctx->fetched_digest);\n        ctx->fetched_digest = NULL;\n        goto legacy;\n    }\n    if (type->prov == NULL) {\n        switch(type->type) {\n        case NID_sha256:\n            break;\n        default:\n            goto legacy;\n        }\n    }\n    if (ctx->digest != NULL && ctx->digest->ctx_size > 0) {\n        OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);\n        ctx->md_data = NULL;\n    }\n    if (type->prov == NULL) {\n        provmd = EVP_MD_fetch(NULL, OBJ_nid2sn(type->type), "");\n        if (provmd == NULL) {\n            EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);\n            return 0;\n        }\n        type = provmd;\n        EVP_MD_meth_free(ctx->fetched_digest);\n        ctx->fetched_digest = provmd;\n    }\n    ctx->digest = type;\n    if (ctx->provctx == NULL) {\n        ctx->provctx = ctx->digest->newctx();\n        if (ctx->provctx == NULL) {\n            EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);\n            return 0;\n        }\n    }\n    if (ctx->digest->dinit == NULL) {\n        EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);\n        return 0;\n    }\n    return ctx->digest->dinit(ctx->provctx);\n legacy:\n#ifndef OPENSSL_NO_ENGINE\n    if (type) {\n        ENGINE_finish(ctx->engine);\n        if (impl != NULL) {\n            if (!ENGINE_init(impl)) {\n                EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);\n                return 0;\n            }\n        } else {\n            impl = tmpimpl;\n        }\n        if (impl != NULL) {\n            const EVP_MD *d = ENGINE_get_digest(impl, type->type);\n            if (d == NULL) {\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_clear_free(ctx->md_data, ctx->digest->ctx_size);\n            ctx->md_data = NULL;\n        }\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}', 'int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,\n                      int cmd, int p1, void *p2)\n{\n    int ret;\n    if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) {\n        EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);\n        return -2;\n    }\n    if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))\n        return -1;\n    if (ctx->pmeth->digest_custom != NULL)\n        goto doit;\n    if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {\n        EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);\n        return -1;\n    }\n    if ((optype != -1) && !(ctx->operation & optype)) {\n        EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);\n        return -1;\n    }\n doit:\n    ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);\n    if (ret == -2)\n        EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);\n    return ret;\n}']