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


test/mdc2test.c:49:9: invalidation part of the trace starts here
47.   # endif
48.   
49.       c = EVP_MD_CTX_new();
              ^
50.       if (!TEST_ptr(c)
51.           || !TEST_true(EVP_DigestInit_ex(c, EVP_mdc2(), NULL))

test/mdc2test.c:49:9: passed as argument to `EVP_MD_CTX_new`
  47.   # endif
  48.   
  49.       c = EVP_MD_CTX_new();
                ^
  50.       if (!TEST_ptr(c)
  51.           || !TEST_true(EVP_DigestInit_ex(c, EVP_mdc2(), NULL))

test/mdc2test.c:49:9: return from call to `EVP_MD_CTX_new`
  47.   # endif
  48.   
  49.       c = EVP_MD_CTX_new();
                ^
  50.       if (!TEST_ptr(c)
  51.           || !TEST_true(EVP_DigestInit_ex(c, EVP_mdc2(), NULL))

test/mdc2test.c:49:5: assigned
  47.   # endif
  48.   
  49.       c = EVP_MD_CTX_new();
            ^
  50.       if (!TEST_ptr(c)
  51.           || !TEST_true(EVP_DigestInit_ex(c, EVP_mdc2(), NULL))

test/mdc2test.c:53:13: when calling `EVP_DigestFinal_ex` here
  51.           || !TEST_true(EVP_DigestInit_ex(c, EVP_mdc2(), NULL))
  52.           || !TEST_true(EVP_DigestUpdate(c, (unsigned char *)text, tlen))
  53.           || !TEST_true(EVP_DigestFinal_ex(c, &(md[0]), NULL))
                    ^
  54.           || !TEST_mem_eq(md, MDC2_DIGEST_LENGTH, pad1, MDC2_DIGEST_LENGTH)
  55.           || !TEST_true(EVP_DigestInit_ex(c, EVP_mdc2(), NULL)))

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

crypto/evp/digest.c:313:5: when calling `EVP_MD_CTX_reset` here
    311.       }
    312.   
    313.       EVP_MD_CTX_reset(ctx);
               ^
    314.   
    315.       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.   }

test/mdc2test.c:49:9: use-after-lifetime part of the trace starts here
47.   # endif
48.   
49.       c = EVP_MD_CTX_new();
              ^
50.       if (!TEST_ptr(c)
51.           || !TEST_true(EVP_DigestInit_ex(c, EVP_mdc2(), NULL))

test/mdc2test.c:49:9: passed as argument to `EVP_MD_CTX_new`
  47.   # endif
  48.   
  49.       c = EVP_MD_CTX_new();
                ^
  50.       if (!TEST_ptr(c)
  51.           || !TEST_true(EVP_DigestInit_ex(c, EVP_mdc2(), NULL))

test/mdc2test.c:49:9: return from call to `EVP_MD_CTX_new`
  47.   # endif
  48.   
  49.       c = EVP_MD_CTX_new();
                ^
  50.       if (!TEST_ptr(c)
  51.           || !TEST_true(EVP_DigestInit_ex(c, EVP_mdc2(), NULL))

test/mdc2test.c:49:5: assigned
  47.   # endif
  48.   
  49.       c = EVP_MD_CTX_new();
            ^
  50.       if (!TEST_ptr(c)
  51.           || !TEST_true(EVP_DigestInit_ex(c, EVP_mdc2(), NULL))

test/mdc2test.c:55:13: when calling `EVP_DigestInit_ex` here
  53.           || !TEST_true(EVP_DigestFinal_ex(c, &(md[0]), NULL))
  54.           || !TEST_mem_eq(md, MDC2_DIGEST_LENGTH, pad1, MDC2_DIGEST_LENGTH)
  55.           || !TEST_true(EVP_DigestInit_ex(c, EVP_mdc2(), NULL)))
                    ^
  56.           goto end;
  57.   

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;
}
['static int test_mdc2(void)\n{\n    int testresult = 0;\n    unsigned char md[MDC2_DIGEST_LENGTH];\n    EVP_MD_CTX *c;\n    static char text[] = "Now is the time for all ";\n    size_t tlen = strlen(text);\n# ifdef CHARSET_EBCDIC\n    ebcdic2ascii(text, text, tlen);\n# endif\n    c = EVP_MD_CTX_new();\n    if (!TEST_ptr(c)\n        || !TEST_true(EVP_DigestInit_ex(c, EVP_mdc2(), NULL))\n        || !TEST_true(EVP_DigestUpdate(c, (unsigned char *)text, tlen))\n        || !TEST_true(EVP_DigestFinal_ex(c, &(md[0]), NULL))\n        || !TEST_mem_eq(md, MDC2_DIGEST_LENGTH, pad1, MDC2_DIGEST_LENGTH)\n        || !TEST_true(EVP_DigestInit_ex(c, EVP_mdc2(), NULL)))\n        goto end;\n    ((MDC2_CTX *)EVP_MD_CTX_md_data(c))->pad_type = 2;\n    if (!TEST_true(EVP_DigestUpdate(c, (unsigned char *)text, tlen))\n        || !TEST_true(EVP_DigestFinal_ex(c, &(md[0]), NULL))\n        || !TEST_mem_eq(md, MDC2_DIGEST_LENGTH, pad2, MDC2_DIGEST_LENGTH))\n        goto end;\n    testresult = 1;\n end:\n    EVP_MD_CTX_free(c);\n    return testresult;\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}']