crypto/dh/dh_key.c:110: error: BUFFER_OVERRUN_L3
  Offset added: [8, +oo] Size: [0, 67108856] by call to `BN_MONT_CTX_set_locked`.
Showing all 18 steps of the trace


crypto/dh/dh_key.c:76:1: Parameter `dh->p->top`
74.   }
75.   
76. > static int generate_key(DH *dh)
77.   {
78.       int ok = 0;

crypto/dh/dh_key.c:85:9: Call
83.       BIGNUM *pub_key = NULL, *priv_key = NULL;
84.   
85.       if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) {
              ^
86.           DHerr(DH_F_GENERATE_KEY, DH_R_MODULUS_TOO_LARGE);
87.           return 0;

crypto/bn/bn_lib.c:139:9: Call
  137.       bn_check_top(a);
  138.   
  139.       if (BN_is_zero(a))
                 ^
  140.           return 0;
  141.       return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));

crypto/bn/bn_lib.c:845:1: Parameter `a->top`
    843.   }
    844.   
    845. > int BN_is_zero(const BIGNUM *a)
    846.   {
    847.       return a->top == 0;

crypto/dh/dh_key.c:110:16: Call
108.   
109.       if (dh->flags & DH_FLAG_CACHE_MONT_P) {
110.           mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,
                      ^
111.                                         dh->lock, dh->p, ctx);
112.           if (!mont)

crypto/bn/bn_mont.c:393:1: Parameter `mod->top`
  391.   }
  392.   
  393. > BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, CRYPTO_RWLOCK *lock,
  394.                                       const BIGNUM *mod, BN_CTX *ctx)
  395.   {

crypto/bn/bn_mont.c:415:10: Call
  413.       if (ret == NULL)
  414.           return NULL;
  415.       if (!BN_MONT_CTX_set(ret, mod, ctx)) {
                  ^
  416.           BN_MONT_CTX_free(ret);
  417.           return NULL;

crypto/bn/bn_mont.c:233:1: Parameter `mod->top`
    231.   }
    232.   
    233. > int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)
    234.   {
    235.       int ret = 0;

crypto/bn/bn_mont.c:238:9: Call
    236.       BIGNUM *Ri, *R;
    237.   
    238.       if (BN_is_zero(mod))
                   ^
    239.           return 0;
    240.   

crypto/bn/bn_lib.c:845:1: Parameter `a->top`
      843.   }
      844.   
      845. > int BN_is_zero(const BIGNUM *a)
      846.   {
      847.       return a->top == 0;

crypto/bn/bn_mont.c:245:10: Call
    243.           goto err;
    244.       R = &(mont->RR);            /* grab RR as a temp */
    245.       if (!BN_copy(&(mont->N), mod))
                    ^
    246.           goto err;               /* Set N */
    247.       if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)

crypto/bn/bn_lib.c:285:1: <Offset trace>
283.   }
284.   
285. > BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
286.   {
287.       bn_check_top(b);

crypto/bn/bn_lib.c:285:1: Parameter `b->top`
      283.   }
      284.   
      285. > BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
      286.   {
      287.       bn_check_top(b);

crypto/bn/bn_lib.c:285:1: <Length trace>
283.   }
284.   
285. > BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
286.   {
287.       bn_check_top(b);

crypto/bn/bn_lib.c:285:1: Parameter `*a->d`
      283.   }
      284.   
      285. > BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
      286.   {
      287.       bn_check_top(b);

crypto/bn/bn_lib.c:291:9: Call
      289.       if (a == b)
      290.           return a;
      291.       if (bn_wexpand(a, b->top) == NULL)
                     ^
      292.           return NULL;
      293.   

crypto/bn/bn_lib.c:941:1: Parameter `*a->d`
        939.   }
        940.   
        941. > BIGNUM *bn_wexpand(BIGNUM *a, int words)
        942.   {
        943.       return (words <= a->dmax) ? a : bn_expand2(a, words);

crypto/bn/bn_lib.c:295:9: Array access: Offset added: [8, +oo] Size: [0, 67108856] by call to `BN_MONT_CTX_set_locked` 
      293.   
      294.       if (b->top > 0)
      295.           memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);
                     ^
      296.   
      297.       a->top = b->top;

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);
    a->top = b->top;
    a->neg = b->neg;
    bn_check_top(a);
    return a;
}
['static int generate_key(DH *dh)\n{\n    int ok = 0;\n    int generate_new_key = 0;\n    unsigned l;\n    BN_CTX *ctx = NULL;\n    BN_MONT_CTX *mont = NULL;\n    BIGNUM *pub_key = NULL, *priv_key = NULL;\n    if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) {\n        DHerr(DH_F_GENERATE_KEY, DH_R_MODULUS_TOO_LARGE);\n        return 0;\n    }\n    ctx = BN_CTX_new();\n    if (ctx == NULL)\n        goto err;\n    if (dh->priv_key == NULL) {\n        priv_key = BN_secure_new();\n        if (priv_key == NULL)\n            goto err;\n        generate_new_key = 1;\n    } else\n        priv_key = dh->priv_key;\n    if (dh->pub_key == NULL) {\n        pub_key = BN_new();\n        if (pub_key == NULL)\n            goto err;\n    } else\n        pub_key = dh->pub_key;\n    if (dh->flags & DH_FLAG_CACHE_MONT_P) {\n        mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,\n                                      dh->lock, dh->p, ctx);\n        if (!mont)\n            goto err;\n    }\n    if (generate_new_key) {\n        if (dh->q) {\n            do {\n                if (!BN_priv_rand_range(priv_key, dh->q))\n                    goto err;\n            }\n            while (BN_is_zero(priv_key) || BN_is_one(priv_key));\n        } else {\n            l = dh->length ? dh->length : BN_num_bits(dh->p) - 1;\n            if (!BN_priv_rand(priv_key, l, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))\n                goto err;\n        }\n    }\n    {\n        BIGNUM *prk = BN_new();\n        if (prk == NULL)\n            goto err;\n        BN_with_flags(prk, priv_key, BN_FLG_CONSTTIME);\n        if (!dh->meth->bn_mod_exp(dh, pub_key, dh->g, prk, dh->p, ctx, mont)) {\n            BN_free(prk);\n            goto err;\n        }\n        BN_free(prk);\n    }\n    dh->pub_key = pub_key;\n    dh->priv_key = priv_key;\n    ok = 1;\n err:\n    if (ok != 1)\n        DHerr(DH_F_GENERATE_KEY, ERR_R_BN_LIB);\n    if (pub_key != dh->pub_key)\n        BN_free(pub_key);\n    if (priv_key != dh->priv_key)\n        BN_free(priv_key);\n    BN_CTX_free(ctx);\n    return ok;\n}', 'int BN_num_bits(const BIGNUM *a)\n{\n    int i = a->top - 1;\n    bn_check_top(a);\n    if (BN_is_zero(a))\n        return 0;\n    return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));\n}', 'int BN_is_zero(const BIGNUM *a)\n{\n    return a->top == 0;\n}', 'BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, CRYPTO_RWLOCK *lock,\n                                    const BIGNUM *mod, BN_CTX *ctx)\n{\n    BN_MONT_CTX *ret;\n    CRYPTO_THREAD_read_lock(lock);\n    ret = *pmont;\n    CRYPTO_THREAD_unlock(lock);\n    if (ret)\n        return ret;\n    ret = BN_MONT_CTX_new();\n    if (ret == NULL)\n        return NULL;\n    if (!BN_MONT_CTX_set(ret, mod, ctx)) {\n        BN_MONT_CTX_free(ret);\n        return NULL;\n    }\n    CRYPTO_THREAD_write_lock(lock);\n    if (*pmont) {\n        BN_MONT_CTX_free(ret);\n        ret = *pmont;\n    } else\n        *pmont = ret;\n    CRYPTO_THREAD_unlock(lock);\n    return ret;\n}', 'int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)\n{\n    int ret = 0;\n    BIGNUM *Ri, *R;\n    if (BN_is_zero(mod))\n        return 0;\n    BN_CTX_start(ctx);\n    if ((Ri = BN_CTX_get(ctx)) == NULL)\n        goto err;\n    R = &(mont->RR);\n    if (!BN_copy(&(mont->N), mod))\n        goto err;\n    if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)\n        BN_set_flags(&(mont->N), BN_FLG_CONSTTIME);\n    mont->N.neg = 0;\n#ifdef MONT_WORD\n    {\n        BIGNUM tmod;\n        BN_ULONG buf[2];\n        bn_init(&tmod);\n        tmod.d = buf;\n        tmod.dmax = 2;\n        tmod.neg = 0;\n        if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)\n            BN_set_flags(&tmod, BN_FLG_CONSTTIME);\n        mont->ri = (BN_num_bits(mod) + (BN_BITS2 - 1)) / BN_BITS2 * BN_BITS2;\n# if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2<=32)\n        BN_zero(R);\n        if (!(BN_set_bit(R, 2 * BN_BITS2)))\n            goto err;\n        tmod.top = 0;\n        if ((buf[0] = mod->d[0]))\n            tmod.top = 1;\n        if ((buf[1] = mod->top > 1 ? mod->d[1] : 0))\n            tmod.top = 2;\n        if (BN_is_one(&tmod))\n            BN_zero(Ri);\n        else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n            goto err;\n        if (!BN_lshift(Ri, Ri, 2 * BN_BITS2))\n            goto err;\n        if (!BN_is_zero(Ri)) {\n            if (!BN_sub_word(Ri, 1))\n                goto err;\n        } else {\n            if (bn_expand(Ri, (int)sizeof(BN_ULONG) * 2) == NULL)\n                goto err;\n            Ri->neg = 0;\n            Ri->d[0] = BN_MASK2;\n            Ri->d[1] = BN_MASK2;\n            Ri->top = 2;\n        }\n        if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n            goto err;\n        mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n        mont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0;\n# else\n        BN_zero(R);\n        if (!(BN_set_bit(R, BN_BITS2)))\n            goto err;\n        buf[0] = mod->d[0];\n        buf[1] = 0;\n        tmod.top = buf[0] != 0 ? 1 : 0;\n        if (BN_is_one(&tmod))\n            BN_zero(Ri);\n        else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n            goto err;\n        if (!BN_lshift(Ri, Ri, BN_BITS2))\n            goto err;\n        if (!BN_is_zero(Ri)) {\n            if (!BN_sub_word(Ri, 1))\n                goto err;\n        } else {\n            if (!BN_set_word(Ri, BN_MASK2))\n                goto err;\n        }\n        if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n            goto err;\n        mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n        mont->n0[1] = 0;\n# endif\n    }\n#else\n    {\n        mont->ri = BN_num_bits(&mont->N);\n        BN_zero(R);\n        if (!BN_set_bit(R, mont->ri))\n            goto err;\n        if ((BN_mod_inverse(Ri, R, &mont->N, ctx)) == NULL)\n            goto err;\n        if (!BN_lshift(Ri, Ri, mont->ri))\n            goto err;\n        if (!BN_sub_word(Ri, 1))\n            goto err;\n        if (!BN_div(&(mont->Ni), NULL, Ri, &mont->N, ctx))\n            goto err;\n    }\n#endif\n    BN_zero(&(mont->RR));\n    if (!BN_set_bit(&(mont->RR), mont->ri * 2))\n        goto err;\n    if (!BN_mod(&(mont->RR), &(mont->RR), &(mont->N), ctx))\n        goto err;\n    ret = 1;\n err:\n    BN_CTX_end(ctx);\n    return ret;\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    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}']