crypto/dh/dh_pmeth.c:337: error: BUFFER_OVERRUN_L3
  Offset added: [8, +oo] Size: [0, 67108856] by call to `DSA_dup_DH`.
Showing all 15 steps of the trace


crypto/dh/dh_pmeth.c:333:18: Call
331.       if (dctx->use_dsa) {
332.           DSA *dsa_dh;
333.           dsa_dh = dsa_dh_generate(dctx, pcb);
                        ^
334.           BN_GENCB_free(pcb);
335.           if (dsa_dh == NULL)

crypto/dh/dh_pmeth.c:285:14: Call
  283.                                     NULL, 0, NULL, NULL, NULL, pcb);
  284.       else if (dctx->use_dsa == 2)
  285.           rv = dsa_builtin_paramgen2(ret, prime_len, subprime_len, md,
                      ^
  286.                                      NULL, 0, -1, NULL, NULL, NULL, pcb);
  287.       if (rv <= 0) {

crypto/dsa/dsa_gen.c:300:1: Parameter `ret->q->top`
    298.    */
    299.   
    300. > int dsa_builtin_paramgen2(DSA *ret, size_t L, size_t N,
    301.                             const EVP_MD *evpmd, const unsigned char *seed_in,
    302.                             size_t seed_len, int idx, unsigned char *seed_out,

crypto/dh/dh_pmeth.c:337:14: Call
335.           if (dsa_dh == NULL)
336.               return 0;
337.           dh = DSA_dup_DH(dsa_dh);
                    ^
338.           DSA_free(dsa_dh);
339.           if (!dh)

crypto/dsa/dsa_lib.c:186:1: Parameter `r->p->top`
  184.   
  185.   #ifndef OPENSSL_NO_DH
  186. > DH *DSA_dup_DH(const DSA *r)
  187.   {
  188.       /*

crypto/dsa/dsa_lib.c:206:13: Call
  204.               goto err;
  205.           }
  206.           p = BN_dup(r->p);
                     ^
  207.           g = BN_dup(r->g);
  208.           q = BN_dup(r->q);

crypto/bn/bn_lib.c:304:1: Parameter `a->top`
    302.   }
    303.   
    304. > BIGNUM *BN_dup(const BIGNUM *a)
    305.   {
    306.       BIGNUM *t;

crypto/bn/bn_lib.c:315:10: Call
    313.       if (t == NULL)
    314.           return NULL;
    315.       if (!BN_copy(t, a)) {
                    ^
    316.           BN_free(t);
    317.           return NULL;

crypto/bn/bn_lib.c:323:1: <Offset trace>
321.   }
322.   
323. > BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
324.   {
325.       bn_check_top(b);

crypto/bn/bn_lib.c:323:1: Parameter `b->top`
      321.   }
      322.   
      323. > BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
      324.   {
      325.       bn_check_top(b);

crypto/bn/bn_lib.c:323:1: <Length trace>
321.   }
322.   
323. > BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
324.   {
325.       bn_check_top(b);

crypto/bn/bn_lib.c:323:1: Parameter `*a->d`
      321.   }
      322.   
      323. > BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
      324.   {
      325.       bn_check_top(b);

crypto/bn/bn_lib.c:329:9: Call
      327.       if (a == b)
      328.           return a;
      329.       if (bn_wexpand(a, b->top) == NULL)
                     ^
      330.           return NULL;
      331.   

crypto/bn/bn_lib.c:951:1: Parameter `*a->d`
        949.   }
        950.   
        951. > BIGNUM *bn_wexpand(BIGNUM *a, int words)
        952.   {
        953.       return (words <= a->dmax) ? a : bn_expand2(a, words);

crypto/bn/bn_lib.c:333:9: Array access: Offset added: [8, +oo] Size: [0, 67108856] by call to `DSA_dup_DH` 
      331.   
      332.       if (b->top > 0)
      333.           memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);
                     ^
      334.   
      335.       if (BN_get_flags(b, BN_FLG_CONSTTIME) != 0)

BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
{
    bn_check_top(b);
    if (a == b)
        return a;
    if (bn_wexpand(a, b->top) == NULL)
        return NULL;
    if (b->top > 0)
        memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);
    if (BN_get_flags(b, BN_FLG_CONSTTIME) != 0)
        BN_set_flags(a, BN_FLG_CONSTTIME);
    a->top = b->top;
    a->neg = b->neg;
    bn_check_top(a);
    return a;
}
['static int pkey_dh_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)\n{\n    DH *dh = NULL;\n    DH_PKEY_CTX *dctx = ctx->data;\n    BN_GENCB *pcb;\n    int ret;\n    if (dctx->rfc5114_param) {\n        switch (dctx->rfc5114_param) {\n        case 1:\n            dh = DH_get_1024_160();\n            break;\n        case 2:\n            dh = DH_get_2048_224();\n            break;\n        case 3:\n            dh = DH_get_2048_256();\n            break;\n        default:\n            return -2;\n        }\n        EVP_PKEY_assign(pkey, EVP_PKEY_DHX, dh);\n        return 1;\n    }\n    if (ctx->pkey_gencb) {\n        pcb = BN_GENCB_new();\n        if (pcb == NULL)\n            return 0;\n        evp_pkey_set_cb_translate(pcb, ctx);\n    } else\n        pcb = NULL;\n#ifndef OPENSSL_NO_DSA\n    if (dctx->use_dsa) {\n        DSA *dsa_dh;\n        dsa_dh = dsa_dh_generate(dctx, pcb);\n        BN_GENCB_free(pcb);\n        if (dsa_dh == NULL)\n            return 0;\n        dh = DSA_dup_DH(dsa_dh);\n        DSA_free(dsa_dh);\n        if (!dh)\n            return 0;\n        EVP_PKEY_assign(pkey, EVP_PKEY_DHX, dh);\n        return 1;\n    }\n#endif\n    dh = DH_new();\n    if (dh == NULL) {\n        BN_GENCB_free(pcb);\n        return 0;\n    }\n    ret = DH_generate_parameters_ex(dh,\n                                    dctx->prime_len, dctx->generator, pcb);\n    BN_GENCB_free(pcb);\n    if (ret)\n        EVP_PKEY_assign_DH(pkey, dh);\n    else\n        DH_free(dh);\n    return ret;\n}', 'static DSA *dsa_dh_generate(DH_PKEY_CTX *dctx, BN_GENCB *pcb)\n{\n    DSA *ret;\n    int rv = 0;\n    int prime_len = dctx->prime_len;\n    int subprime_len = dctx->subprime_len;\n    const EVP_MD *md = dctx->md;\n    if (dctx->use_dsa > 2)\n        return NULL;\n    ret = DSA_new();\n    if (ret == NULL)\n        return NULL;\n    if (subprime_len == -1) {\n        if (prime_len >= 2048)\n            subprime_len = 256;\n        else\n            subprime_len = 160;\n    }\n    if (md == NULL) {\n        if (prime_len >= 2048)\n            md = EVP_sha256();\n        else\n            md = EVP_sha1();\n    }\n    if (dctx->use_dsa == 1)\n        rv = dsa_builtin_paramgen(ret, prime_len, subprime_len, md,\n                                  NULL, 0, NULL, NULL, NULL, pcb);\n    else if (dctx->use_dsa == 2)\n        rv = dsa_builtin_paramgen2(ret, prime_len, subprime_len, md,\n                                   NULL, 0, -1, NULL, NULL, NULL, pcb);\n    if (rv <= 0) {\n        DSA_free(ret);\n        return NULL;\n    }\n    return ret;\n}', 'int dsa_builtin_paramgen2(DSA *ret, size_t L, size_t N,\n                          const EVP_MD *evpmd, const unsigned char *seed_in,\n                          size_t seed_len, int idx, unsigned char *seed_out,\n                          int *counter_ret, unsigned long *h_ret,\n                          BN_GENCB *cb)\n{\n    int ok = -1;\n    unsigned char *seed = NULL, *seed_tmp = NULL;\n    unsigned char md[EVP_MAX_MD_SIZE];\n    int mdsize;\n    BIGNUM *r0, *W, *X, *c, *test;\n    BIGNUM *g = NULL, *q = NULL, *p = NULL;\n    BN_MONT_CTX *mont = NULL;\n    int i, k, n = 0, m = 0, qsize = N >> 3;\n    int counter = 0;\n    int r = 0;\n    BN_CTX *ctx = NULL;\n    EVP_MD_CTX *mctx = EVP_MD_CTX_new();\n    unsigned int h = 2;\n    if (mctx == NULL)\n        goto err;\n    if (evpmd == NULL) {\n        if (N == 160)\n            evpmd = EVP_sha1();\n        else if (N == 224)\n            evpmd = EVP_sha224();\n        else\n            evpmd = EVP_sha256();\n    }\n    mdsize = EVP_MD_size(evpmd);\n    if (!ret->p || !ret->q || idx >= 0) {\n        if (seed_len == 0)\n            seed_len = mdsize;\n        seed = OPENSSL_malloc(seed_len);\n        if (seed_out)\n            seed_tmp = seed_out;\n        else\n            seed_tmp = OPENSSL_malloc(seed_len);\n        if (seed == NULL || seed_tmp == NULL)\n            goto err;\n        if (seed_in)\n            memcpy(seed, seed_in, seed_len);\n    }\n    if ((ctx = BN_CTX_new()) == NULL)\n        goto err;\n    if ((mont = BN_MONT_CTX_new()) == NULL)\n        goto err;\n    BN_CTX_start(ctx);\n    r0 = BN_CTX_get(ctx);\n    g = BN_CTX_get(ctx);\n    W = BN_CTX_get(ctx);\n    X = BN_CTX_get(ctx);\n    c = BN_CTX_get(ctx);\n    test = BN_CTX_get(ctx);\n    if (test == NULL)\n        goto err;\n    if (ret->p && ret->q) {\n        p = ret->p;\n        q = ret->q;\n        if (idx >= 0)\n            memcpy(seed_tmp, seed, seed_len);\n        goto g_only;\n    } else {\n        p = BN_CTX_get(ctx);\n        q = BN_CTX_get(ctx);\n        if (q == NULL)\n            goto err;\n    }\n    if (!BN_lshift(test, BN_value_one(), L - 1))\n        goto err;\n    for (;;) {\n        for (;;) {\n            unsigned char *pmd;\n            if (!BN_GENCB_call(cb, 0, m++))\n                goto err;\n            if (!seed_in) {\n                if (RAND_bytes(seed, seed_len) <= 0)\n                    goto err;\n            }\n            if (!EVP_Digest(seed, seed_len, md, NULL, evpmd, NULL))\n                goto err;\n            if (mdsize > qsize)\n                pmd = md + mdsize - qsize;\n            else\n                pmd = md;\n            if (mdsize < qsize)\n                memset(md + mdsize, 0, qsize - mdsize);\n            pmd[0] |= 0x80;\n            pmd[qsize - 1] |= 0x01;\n            if (!BN_bin2bn(pmd, qsize, q))\n                goto err;\n            r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx,\n                                        seed_in ? 1 : 0, cb);\n            if (r > 0)\n                break;\n            if (r != 0)\n                goto err;\n            if (seed_in) {\n                ok = 0;\n                DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN2, DSA_R_Q_NOT_PRIME);\n                goto err;\n            }\n        }\n        if (seed_out)\n            memcpy(seed_out, seed, seed_len);\n        if (!BN_GENCB_call(cb, 2, 0))\n            goto err;\n        if (!BN_GENCB_call(cb, 3, 0))\n            goto err;\n        counter = 0;\n        n = (L - 1) / (mdsize << 3);\n        for (;;) {\n            if ((counter != 0) && !BN_GENCB_call(cb, 0, counter))\n                goto err;\n            BN_zero(W);\n            for (k = 0; k <= n; k++) {\n                for (i = seed_len - 1; i >= 0; i--) {\n                    seed[i]++;\n                    if (seed[i] != 0)\n                        break;\n                }\n                if (!EVP_Digest(seed, seed_len, md, NULL, evpmd, NULL))\n                    goto err;\n                if (!BN_bin2bn(md, mdsize, r0))\n                    goto err;\n                if (!BN_lshift(r0, r0, (mdsize << 3) * k))\n                    goto err;\n                if (!BN_add(W, W, r0))\n                    goto err;\n            }\n            if (!BN_mask_bits(W, L - 1))\n                goto err;\n            if (!BN_copy(X, W))\n                goto err;\n            if (!BN_add(X, X, test))\n                goto err;\n            if (!BN_lshift1(r0, q))\n                goto err;\n            if (!BN_mod(c, X, r0, ctx))\n                goto err;\n            if (!BN_sub(r0, c, BN_value_one()))\n                goto err;\n            if (!BN_sub(p, X, r0))\n                goto err;\n            if (BN_cmp(p, test) >= 0) {\n                r = BN_is_prime_fasttest_ex(p, DSS_prime_checks, ctx, 1, cb);\n                if (r > 0)\n                    goto end;\n                if (r != 0)\n                    goto err;\n            }\n            counter++;\n            if (counter >= (int)(4 * L))\n                break;\n        }\n        if (seed_in) {\n            ok = 0;\n            DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN2, DSA_R_INVALID_PARAMETERS);\n            goto err;\n        }\n    }\n end:\n    if (!BN_GENCB_call(cb, 2, 1))\n        goto err;\n g_only:\n    if (!BN_sub(test, p, BN_value_one()))\n        goto err;\n    if (!BN_div(r0, NULL, test, q, ctx))\n        goto err;\n    if (idx < 0) {\n        if (!BN_set_word(test, h))\n            goto err;\n    } else\n        h = 1;\n    if (!BN_MONT_CTX_set(mont, p, ctx))\n        goto err;\n    for (;;) {\n        static const unsigned char ggen[4] = { 0x67, 0x67, 0x65, 0x6e };\n        if (idx >= 0) {\n            md[0] = idx & 0xff;\n            md[1] = (h >> 8) & 0xff;\n            md[2] = h & 0xff;\n            if (!EVP_DigestInit_ex(mctx, evpmd, NULL))\n                goto err;\n            if (!EVP_DigestUpdate(mctx, seed_tmp, seed_len))\n                goto err;\n            if (!EVP_DigestUpdate(mctx, ggen, sizeof(ggen)))\n                goto err;\n            if (!EVP_DigestUpdate(mctx, md, 3))\n                goto err;\n            if (!EVP_DigestFinal_ex(mctx, md, NULL))\n                goto err;\n            if (!BN_bin2bn(md, mdsize, test))\n                goto err;\n        }\n        if (!BN_mod_exp_mont(g, test, r0, p, ctx, mont))\n            goto err;\n        if (!BN_is_one(g))\n            break;\n        if (idx < 0 && !BN_add(test, test, BN_value_one()))\n            goto err;\n        h++;\n        if (idx >= 0 && h > 0xffff)\n            goto err;\n    }\n    if (!BN_GENCB_call(cb, 3, 1))\n        goto err;\n    ok = 1;\n err:\n    if (ok == 1) {\n        if (p != ret->p) {\n            BN_free(ret->p);\n            ret->p = BN_dup(p);\n        }\n        if (q != ret->q) {\n            BN_free(ret->q);\n            ret->q = BN_dup(q);\n        }\n        BN_free(ret->g);\n        ret->g = BN_dup(g);\n        if (ret->p == NULL || ret->q == NULL || ret->g == NULL) {\n            ok = -1;\n            goto err;\n        }\n        if (counter_ret != NULL)\n            *counter_ret = counter;\n        if (h_ret != NULL)\n            *h_ret = h;\n    }\n    OPENSSL_free(seed);\n    if (seed_out != seed_tmp)\n        OPENSSL_free(seed_tmp);\n    if (ctx)\n        BN_CTX_end(ctx);\n    BN_CTX_free(ctx);\n    BN_MONT_CTX_free(mont);\n    EVP_MD_CTX_free(mctx);\n    return ok;\n}', 'DH *DSA_dup_DH(const DSA *r)\n{\n    DH *ret = NULL;\n    BIGNUM *p = NULL, *q = NULL, *g = NULL, *pub_key = NULL, *priv_key = NULL;\n    if (r == NULL)\n        goto err;\n    ret = DH_new();\n    if (ret == NULL)\n        goto err;\n    if (r->p != NULL || r->g != NULL || r->q != NULL) {\n        if (r->p == NULL || r->g == NULL || r->q == NULL) {\n            goto err;\n        }\n        p = BN_dup(r->p);\n        g = BN_dup(r->g);\n        q = BN_dup(r->q);\n        if (p == NULL || g == NULL || q == NULL || !DH_set0_pqg(ret, p, q, g))\n            goto err;\n        p = g = q = NULL;\n    }\n    if (r->pub_key != NULL) {\n        pub_key = BN_dup(r->pub_key);\n        if (pub_key == NULL)\n            goto err;\n        if (r->priv_key != NULL) {\n            priv_key = BN_dup(r->priv_key);\n            if (priv_key == NULL)\n                goto err;\n        }\n        if (!DH_set0_key(ret, pub_key, priv_key))\n            goto err;\n    } else if (r->priv_key != NULL) {\n        goto err;\n    }\n    return ret;\n err:\n    BN_free(p);\n    BN_free(g);\n    BN_free(q);\n    BN_free(pub_key);\n    BN_free(priv_key);\n    DH_free(ret);\n    return NULL;\n}', 'BIGNUM *BN_dup(const BIGNUM *a)\n{\n    BIGNUM *t;\n    if (a == NULL)\n        return NULL;\n    bn_check_top(a);\n    t = BN_get_flags(a, BN_FLG_SECURE) ? BN_secure_new() : BN_new();\n    if (t == NULL)\n        return NULL;\n    if (!BN_copy(t, a)) {\n        BN_free(t);\n        return NULL;\n    }\n    bn_check_top(t);\n    return t;\n}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\n    bn_check_top(b);\n    if (a == b)\n        return a;\n    if (bn_wexpand(a, b->top) == NULL)\n        return NULL;\n    if (b->top > 0)\n        memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n    if (BN_get_flags(b, BN_FLG_CONSTTIME) != 0)\n        BN_set_flags(a, BN_FLG_CONSTTIME);\n    a->top = b->top;\n    a->neg = b->neg;\n    bn_check_top(a);\n    return a;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n    return (words <= a->dmax) ? a : bn_expand2(a, words);\n}']