crypto/ec/ecdh_kdf.c:108: error: USE_AFTER_FREE
  call to `EVP_MD_CTX_free()` eventually accesses memory that was invalidated by call to `free()` on line 92 indirectly during the call to `EVP_DigestFinal()`.
Showing all 20 steps of the trace


crypto/ec/ecdh_kdf.c:74:12: invalidation part of the trace starts here
72.           || Zlen > ECDH_KDF_MAX)
73.           return 0;
74.       mctx = EVP_MD_CTX_new();
                 ^
75.       if (mctx == NULL)
76.           return 0;

crypto/ec/ecdh_kdf.c:74:12: passed as argument to `EVP_MD_CTX_new`
  72.           || Zlen > ECDH_KDF_MAX)
  73.           return 0;
  74.       mctx = EVP_MD_CTX_new();
                   ^
  75.       if (mctx == NULL)
  76.           return 0;

crypto/ec/ecdh_kdf.c:74:12: return from call to `EVP_MD_CTX_new`
  72.           || Zlen > ECDH_KDF_MAX)
  73.           return 0;
  74.       mctx = EVP_MD_CTX_new();
                   ^
  75.       if (mctx == NULL)
  76.           return 0;

crypto/ec/ecdh_kdf.c:74:5: assigned
  72.           || Zlen > ECDH_KDF_MAX)
  73.           return 0;
  74.       mctx = EVP_MD_CTX_new();
            ^
  75.       if (mctx == NULL)
  76.           return 0;

crypto/ec/ecdh_kdf.c:92:18: when calling `EVP_DigestFinal` here
  90.               goto err;
  91.           if (outlen >= mdlen) {
  92.               if (!EVP_DigestFinal(mctx, out, NULL))
                         ^
  93.                   goto err;
  94.               outlen -= mdlen;

crypto/evp/digest.c:260:1: parameter `ctx` of EVP_DigestFinal
    258.   
    259.   /* The caller can assume that this removes any secret data from the context */
    260. > int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
    261.   {
    262.       int ret;

crypto/evp/digest.c:264:5: when calling `EVP_MD_CTX_reset` here
    262.       int ret;
    263.       ret = EVP_DigestFinal_ex(ctx, md, size);
    264.       EVP_MD_CTX_reset(ctx);
               ^
    265.       return ret;
    266.   }

crypto/evp/digest.c:122:1: parameter `ctx` of EVP_MD_CTX_reset
      120.   
      121.   /* This call frees resources associated with the context */
      122. > int EVP_MD_CTX_reset(EVP_MD_CTX *ctx)
      123.   {
      124.       if (ctx == NULL)

crypto/evp/digest.c:138:5: when calling `EVP_PKEY_CTX_free` here
      136.           OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);
      137.       }
      138.       EVP_PKEY_CTX_free(ctx->pctx);
                 ^
      139.   #ifndef OPENSSL_NO_ENGINE
      140.       if (ctx->engine)

crypto/evp/pmeth_lib.c:323:1: parameter `ctx` of EVP_PKEY_CTX_free
        321.   }
        322.   
        323. > void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
        324.   {
        325.       if (ctx == NULL)

crypto/evp/pmeth_lib.c:339:5: when calling `CRYPTO_free` here
        337.           ENGINE_finish(ctx->engine);
        338.   #endif
        339.       OPENSSL_free(ctx);
                   ^
        340.   }
        341.   

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.   }

crypto/ec/ecdh_kdf.c:74:12: use-after-lifetime part of the trace starts here
72.           || Zlen > ECDH_KDF_MAX)
73.           return 0;
74.       mctx = EVP_MD_CTX_new();
                 ^
75.       if (mctx == NULL)
76.           return 0;

crypto/ec/ecdh_kdf.c:74:12: passed as argument to `EVP_MD_CTX_new`
  72.           || Zlen > ECDH_KDF_MAX)
  73.           return 0;
  74.       mctx = EVP_MD_CTX_new();
                   ^
  75.       if (mctx == NULL)
  76.           return 0;

crypto/ec/ecdh_kdf.c:74:12: return from call to `EVP_MD_CTX_new`
  72.           || Zlen > ECDH_KDF_MAX)
  73.           return 0;
  74.       mctx = EVP_MD_CTX_new();
                   ^
  75.       if (mctx == NULL)
  76.           return 0;

crypto/ec/ecdh_kdf.c:74:5: assigned
  72.           || Zlen > ECDH_KDF_MAX)
  73.           return 0;
  74.       mctx = EVP_MD_CTX_new();
            ^
  75.       if (mctx == NULL)
  76.           return 0;

crypto/ec/ecdh_kdf.c:108:5: when calling `EVP_MD_CTX_free` here
  106.       rv = 1;
  107.    err:
  108.       EVP_MD_CTX_free(mctx);
             ^
  109.       return rv;
  110.   }

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 ECDH_KDF_X9_62(unsigned char *out, size_t outlen,\n                   const unsigned char *Z, size_t Zlen,\n                   const unsigned char *sinfo, size_t sinfolen,\n                   const EVP_MD *md)\n{\n    EVP_MD_CTX *mctx = NULL;\n    int rv = 0;\n    unsigned int i;\n    size_t mdlen;\n    unsigned char ctr[4];\n    if (sinfolen > ECDH_KDF_MAX || outlen > ECDH_KDF_MAX\n        || Zlen > ECDH_KDF_MAX)\n        return 0;\n    mctx = EVP_MD_CTX_new();\n    if (mctx == NULL)\n        return 0;\n    mdlen = EVP_MD_size(md);\n    for (i = 1;; i++) {\n        unsigned char mtmp[EVP_MAX_MD_SIZE];\n        EVP_DigestInit_ex(mctx, md, NULL);\n        ctr[3] = i & 0xFF;\n        ctr[2] = (i >> 8) & 0xFF;\n        ctr[1] = (i >> 16) & 0xFF;\n        ctr[0] = (i >> 24) & 0xFF;\n        if (!EVP_DigestUpdate(mctx, Z, Zlen))\n            goto err;\n        if (!EVP_DigestUpdate(mctx, ctr, sizeof(ctr)))\n            goto err;\n        if (!EVP_DigestUpdate(mctx, sinfo, sinfolen))\n            goto err;\n        if (outlen >= mdlen) {\n            if (!EVP_DigestFinal(mctx, out, NULL))\n                goto err;\n            outlen -= mdlen;\n            if (outlen == 0)\n                break;\n            out += mdlen;\n        } else {\n            if (!EVP_DigestFinal(mctx, mtmp, NULL))\n                goto err;\n            memcpy(out, mtmp, outlen);\n            OPENSSL_cleanse(mtmp, mdlen);\n            break;\n        }\n    }\n    rv = 1;\n err:\n    EVP_MD_CTX_free(mctx);\n    return rv;\n}', 'int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)\n{\n    int ret;\n    ret = EVP_DigestFinal_ex(ctx, md, size);\n    EVP_MD_CTX_reset(ctx);\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 && 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    EVP_PKEY_CTX_free(ctx->pctx);\n#ifndef OPENSSL_NO_ENGINE\n    if (ctx->engine)\n        ENGINE_finish(ctx->engine);\n#endif\n    memset(ctx, 0, 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    if (ctx->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    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}']