crypto/dh/dh_key.c:109: error: BUFFER_OVERRUN_L3
  Offset: [-1, +oo] Size: [1, +oo] by call to `BN_rand`.
Showing all 15 steps of the trace


crypto/dh/dh_key.c:64:1: Parameter `dh->length`
62.   }
63.   
64. > static int generate_key(DH *dh)
65.   {
66.       int ok = 0;

crypto/dh/dh_key.c:108:17: Assignment
106.           } else {
107.               /* secret exponent length */
108.               l = dh->length ? dh->length : BN_num_bits(dh->p) - 1;
                       ^
109.               if (!BN_rand(priv_key, l, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))
110.                   goto err;

crypto/dh/dh_key.c:108:13: Assignment
106.           } else {
107.               /* secret exponent length */
108.               l = dh->length ? dh->length : BN_num_bits(dh->p) - 1;
                   ^
109.               if (!BN_rand(priv_key, l, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))
110.                   goto err;

crypto/dh/dh_key.c:109:18: Call
107.               /* secret exponent length */
108.               l = dh->length ? dh->length : BN_num_bits(dh->p) - 1;
109.               if (!BN_rand(priv_key, l, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))
                        ^
110.                   goto err;
111.           }

crypto/bn/bn_rand.c:96:1: Parameter `bits`
  94.   }
  95.   
  96. > int BN_rand(BIGNUM *rnd, int bits, int top, int bottom)
  97.   {
  98.       return bnrand(0, rnd, bits, top, bottom);

crypto/bn/bn_rand.c:98:12: Call
  96.   int BN_rand(BIGNUM *rnd, int bits, int top, int bottom)
  97.   {
  98.       return bnrand(0, rnd, bits, top, bottom);
                    ^
  99.   }
  100.   

crypto/bn/bn_rand.c:56:14: <Offset trace>
54.           unsigned char c;
55.   
56.           for (i = 0; i < bytes; i++) {
                   ^
57.               if (RAND_bytes(&c, 1) <= 0)
58.                   goto err;

crypto/bn/bn_rand.c:56:14: Assignment
    54.           unsigned char c;
    55.   
    56.           for (i = 0; i < bytes; i++) {
                       ^
    57.               if (RAND_bytes(&c, 1) <= 0)
    58.                   goto err;

crypto/bn/bn_rand.c:17:1: <Length trace>
15.   #include <openssl/sha.h>
16.   
17. > static int bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom)
18.   {
19.       unsigned char *buf = NULL;

crypto/bn/bn_rand.c:17:1: Parameter `bits`
    15.   #include <openssl/sha.h>
    16.   
    17. > static int bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom)
    18.   {
    19.       unsigned char *buf = NULL;

crypto/bn/bn_rand.c:32:5: Assignment
    30.           goto toosmall;
    31.   
    32.       bytes = (bits + 7) / 8;
              ^
    33.       bit = (bits - 1) % 8;
    34.       mask = 0xff << (bit + 1);

crypto/bn/bn_rand.c:36:11: Call
    34.       mask = 0xff << (bit + 1);
    35.   
    36.       buf = OPENSSL_malloc(bytes);
                    ^
    37.       if (buf == NULL) {
    38.           BNerr(BN_F_BNRAND, ERR_R_MALLOC_FAILURE);

crypto/mem.c:166:9: Assignment
      164.   
      165.       if (num == 0)
      166.           return NULL;
                     ^
      167.   
      168.       FAILTEST();

crypto/bn/bn_rand.c:36:5: Assignment
    34.       mask = 0xff << (bit + 1);
    35.   
    36.       buf = OPENSSL_malloc(bytes);
              ^
    37.       if (buf == NULL) {
    38.           BNerr(BN_F_BNRAND, ERR_R_MALLOC_FAILURE);

crypto/bn/bn_rand.c:82:9: Array access: Offset: [-1, +oo] Size: [1, +oo] by call to `BN_rand` 
    80.       buf[0] &= ~mask;
    81.       if (bottom)                 /* set bottom bit if requested */
    82.           buf[bytes - 1] |= 1;
                  ^
    83.       if (!BN_bin2bn(buf, bytes, rnd))
    84.           goto err;

static int bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom)
{
    unsigned char *buf = NULL;
    int ret = 0, bit, bytes, mask;
    time_t tim;
    if (bits == 0) {
        if (top != BN_RAND_TOP_ANY || bottom != BN_RAND_BOTTOM_ANY)
            goto toosmall;
        BN_zero(rnd);
        return 1;
    }
    if (bits < 0 || (bits == 1 && top > 0))
        goto toosmall;
    bytes = (bits + 7) / 8;
    bit = (bits - 1) % 8;
    mask = 0xff << (bit + 1);
    buf = OPENSSL_malloc(bytes);
    if (buf == NULL) {
        BNerr(BN_F_BNRAND, ERR_R_MALLOC_FAILURE);
        goto err;
    }
    time(&tim);
    RAND_add(&tim, sizeof(tim), 0.0);
    if (RAND_bytes(buf, bytes) <= 0)
        goto err;
    if (pseudorand == 2) {
        int i;
        unsigned char c;
        for (i = 0; i < bytes; i++) {
            if (RAND_bytes(&c, 1) <= 0)
                goto err;
            if (c >= 128 && i > 0)
                buf[i] = buf[i - 1];
            else if (c < 42)
                buf[i] = 0;
            else if (c < 84)
                buf[i] = 255;
        }
    }
    if (top >= 0) {
        if (top) {
            if (bit == 0) {
                buf[0] = 1;
                buf[1] |= 0x80;
            } else {
                buf[0] |= (3 << (bit - 1));
            }
        } else {
            buf[0] |= (1 << bit);
        }
    }
    buf[0] &= ~mask;
    if (bottom)
        buf[bytes - 1] |= 1;
    if (!BN_bin2bn(buf, bytes, rnd))
        goto err;
    ret = 1;
 err:
    OPENSSL_clear_free(buf, bytes);
    bn_check_top(rnd);
    return (ret);
toosmall:
    BNerr(BN_F_BNRAND, BN_R_BITS_TOO_SMALL);
    return 0;
}
['static int generate_key(DH *dh)\n{\n    int ok = 0;\n    int generate_new_key = 0;\n    unsigned l;\n    BN_CTX *ctx;\n    BN_MONT_CTX *mont = NULL;\n    BIGNUM *pub_key = NULL, *priv_key = NULL;\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_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_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_rand(BIGNUM *rnd, int bits, int top, int bottom)\n{\n    return bnrand(0, rnd, bits, top, bottom);\n}', 'static int bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom)\n{\n    unsigned char *buf = NULL;\n    int ret = 0, bit, bytes, mask;\n    time_t tim;\n    if (bits == 0) {\n        if (top != BN_RAND_TOP_ANY || bottom != BN_RAND_BOTTOM_ANY)\n            goto toosmall;\n        BN_zero(rnd);\n        return 1;\n    }\n    if (bits < 0 || (bits == 1 && top > 0))\n        goto toosmall;\n    bytes = (bits + 7) / 8;\n    bit = (bits - 1) % 8;\n    mask = 0xff << (bit + 1);\n    buf = OPENSSL_malloc(bytes);\n    if (buf == NULL) {\n        BNerr(BN_F_BNRAND, ERR_R_MALLOC_FAILURE);\n        goto err;\n    }\n    time(&tim);\n    RAND_add(&tim, sizeof(tim), 0.0);\n    if (RAND_bytes(buf, bytes) <= 0)\n        goto err;\n    if (pseudorand == 2) {\n        int i;\n        unsigned char c;\n        for (i = 0; i < bytes; i++) {\n            if (RAND_bytes(&c, 1) <= 0)\n                goto err;\n            if (c >= 128 && i > 0)\n                buf[i] = buf[i - 1];\n            else if (c < 42)\n                buf[i] = 0;\n            else if (c < 84)\n                buf[i] = 255;\n        }\n    }\n    if (top >= 0) {\n        if (top) {\n            if (bit == 0) {\n                buf[0] = 1;\n                buf[1] |= 0x80;\n            } else {\n                buf[0] |= (3 << (bit - 1));\n            }\n        } else {\n            buf[0] |= (1 << bit);\n        }\n    }\n    buf[0] &= ~mask;\n    if (bottom)\n        buf[bytes - 1] |= 1;\n    if (!BN_bin2bn(buf, bytes, rnd))\n        goto err;\n    ret = 1;\n err:\n    OPENSSL_clear_free(buf, bytes);\n    bn_check_top(rnd);\n    return (ret);\ntoosmall:\n    BNerr(BN_F_BNRAND, BN_R_BITS_TOO_SMALL);\n    return 0;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n    void *ret = NULL;\n    if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n        return malloc_impl(num, file, line);\n    if (num == 0)\n        return NULL;\n    FAILTEST();\n    allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n    if (call_malloc_debug) {\n        CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n        ret = malloc(num);\n        CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n    } else {\n        ret = malloc(num);\n    }\n#else\n    osslargused(file); osslargused(line);\n    ret = malloc(num);\n#endif\n    return ret;\n}']