apps/prime.c:89: error: BUFFER_OVERRUN_L3
  Offset: [-1, +oo] Size: [1, +oo] by call to `BN_generate_prime_ex`.
Showing all 17 steps of the trace


apps/prime.c:37:5: Assignment
35.   {
36.       BIGNUM *bn = NULL;
37.       int hex = 0, checks = 20, generate = 0, bits = 0, safe = 0, ret = 1;
          ^
38.       char *prog;
39.       OPTION_CHOICE o;

apps/prime.c:89:14: Call
87.               goto end;
88.           }
89.           if (!BN_generate_prime_ex(bn, bits, safe, NULL, NULL, NULL)) {
                   ^
90.               BIO_printf(bio_err, "Failed to generate prime.\n");
91.               goto end;

crypto/bn/bn_prime.c:52:1: Parameter `bits`
  50.   }
  51.   
  52. > int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe,
  53.                            const BIGNUM *add, const BIGNUM *rem, BN_GENCB *cb)
  54.   {

crypto/bn/bn_prime.c:86:14: Call
  84.       /* make a random number and set the top and bottom bits */
  85.       if (add == NULL) {
  86.           if (!probable_prime(ret, bits, mods))
                     ^
  87.               goto err;
  88.       } else {

crypto/bn/bn_prime.c:274:1: Parameter `bits`
    272.   }
    273.   
    274. > static int probable_prime(BIGNUM *rnd, int bits, prime_t *mods)
    275.   {
    276.       int i;

crypto/bn/bn_prime.c:282:10: Call
    280.   
    281.    again:
    282.       if (!BN_rand(rnd, bits, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ODD))
                    ^
    283.           return (0);
    284.       /* we now have a random number 'rnd' to test. */

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 testing, 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 testing, 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:177:9: Assignment
          175.   
          176.       if (num == 0)
          177.           return NULL;
                         ^
          178.   
          179.       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_generate_prime_ex` 
        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 testing, 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 (testing) {
        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;
}
['int prime_main(int argc, char **argv)\n{\n    BIGNUM *bn = NULL;\n    int hex = 0, checks = 20, generate = 0, bits = 0, safe = 0, ret = 1;\n    char *prog;\n    OPTION_CHOICE o;\n    prog = opt_init(argc, argv, prime_options);\n    while ((o = opt_next()) != OPT_EOF) {\n        switch (o) {\n        case OPT_EOF:\n        case OPT_ERR:\n            BIO_printf(bio_err, "%s: Use -help for summary.\\n", prog);\n            goto end;\n        case OPT_HELP:\n            opt_help(prime_options);\n            ret = 0;\n            goto end;\n        case OPT_HEX:\n            hex = 1;\n            break;\n        case OPT_GENERATE:\n            generate = 1;\n            break;\n        case OPT_BITS:\n            bits = atoi(opt_arg());\n            break;\n        case OPT_SAFE:\n            safe = 1;\n            break;\n        case OPT_CHECKS:\n            checks = atoi(opt_arg());\n            break;\n        }\n    }\n    argc = opt_num_rest();\n    argv = opt_rest();\n    if (argc == 0 && !generate) {\n        BIO_printf(bio_err, "%s: No prime specified\\n", prog);\n        goto end;\n    }\n    if (generate) {\n        char *s;\n        if (!bits) {\n            BIO_printf(bio_err, "Specify the number of bits.\\n");\n            goto end;\n        }\n        bn = BN_new();\n        if (bn == NULL) {\n            BIO_printf(bio_err, "Out of memory.\\n");\n            goto end;\n        }\n        if (!BN_generate_prime_ex(bn, bits, safe, NULL, NULL, NULL)) {\n            BIO_printf(bio_err, "Failed to generate prime.\\n");\n            goto end;\n        }\n        s = hex ? BN_bn2hex(bn) : BN_bn2dec(bn);\n        if (s == NULL) {\n            BIO_printf(bio_err, "Out of memory.\\n");\n            goto end;\n        }\n        BIO_printf(bio_out, "%s\\n", s);\n        OPENSSL_free(s);\n    } else {\n        for ( ; *argv; argv++) {\n            int r;\n            if (hex)\n                r = BN_hex2bn(&bn, argv[0]);\n            else\n                r = BN_dec2bn(&bn, argv[0]);\n            if (!r) {\n                BIO_printf(bio_err, "Failed to process value (%s)\\n", argv[0]);\n                goto end;\n            }\n            BN_print(bio_out, bn);\n            BIO_printf(bio_out, " (%s) %s prime\\n",\n                       argv[0],\n                       BN_is_prime_ex(bn, checks, NULL, NULL)\n                           ? "is" : "is not");\n        }\n    }\n    ret = 0;\n end:\n    BN_free(bn);\n    return ret;\n}', 'int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe,\n                         const BIGNUM *add, const BIGNUM *rem, BN_GENCB *cb)\n{\n    BIGNUM *t;\n    int found = 0;\n    int i, j, c1 = 0;\n    BN_CTX *ctx = NULL;\n    prime_t *mods = NULL;\n    int checks = BN_prime_checks_for_size(bits);\n    if (bits < 2) {\n        BNerr(BN_F_BN_GENERATE_PRIME_EX, BN_R_BITS_TOO_SMALL);\n        return 0;\n    } else if (bits == 2 && safe) {\n        BNerr(BN_F_BN_GENERATE_PRIME_EX, BN_R_BITS_TOO_SMALL);\n        return 0;\n    }\n    mods = OPENSSL_zalloc(sizeof(*mods) * NUMPRIMES);\n    if (mods == NULL)\n        goto err;\n    ctx = BN_CTX_new();\n    if (ctx == NULL)\n        goto err;\n    BN_CTX_start(ctx);\n    t = BN_CTX_get(ctx);\n    if (t == NULL)\n        goto err;\n loop:\n    if (add == NULL) {\n        if (!probable_prime(ret, bits, mods))\n            goto err;\n    } else {\n        if (safe) {\n            if (!probable_prime_dh_safe(ret, bits, add, rem, ctx))\n                goto err;\n        } else {\n            if (!bn_probable_prime_dh(ret, bits, add, rem, ctx))\n                goto err;\n        }\n    }\n    if (!BN_GENCB_call(cb, 0, c1++))\n        goto err;\n    if (!safe) {\n        i = BN_is_prime_fasttest_ex(ret, checks, ctx, 0, cb);\n        if (i == -1)\n            goto err;\n        if (i == 0)\n            goto loop;\n    } else {\n        if (!BN_rshift1(t, ret))\n            goto err;\n        for (i = 0; i < checks; i++) {\n            j = BN_is_prime_fasttest_ex(ret, 1, ctx, 0, cb);\n            if (j == -1)\n                goto err;\n            if (j == 0)\n                goto loop;\n            j = BN_is_prime_fasttest_ex(t, 1, ctx, 0, cb);\n            if (j == -1)\n                goto err;\n            if (j == 0)\n                goto loop;\n            if (!BN_GENCB_call(cb, 2, c1 - 1))\n                goto err;\n        }\n    }\n    found = 1;\n err:\n    OPENSSL_free(mods);\n    if (ctx != NULL)\n        BN_CTX_end(ctx);\n    BN_CTX_free(ctx);\n    bn_check_top(ret);\n    return found;\n}', 'static int probable_prime(BIGNUM *rnd, int bits, prime_t *mods)\n{\n    int i;\n    BN_ULONG delta;\n    BN_ULONG maxdelta = BN_MASK2 - primes[NUMPRIMES - 1];\n    char is_single_word = bits <= BN_BITS2;\n again:\n    if (!BN_rand(rnd, bits, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ODD))\n        return (0);\n    for (i = 1; i < NUMPRIMES; i++) {\n        BN_ULONG mod = BN_mod_word(rnd, (BN_ULONG)primes[i]);\n        if (mod == (BN_ULONG)-1)\n            return 0;\n        mods[i] = (prime_t) mod;\n    }\n    if (is_single_word) {\n        BN_ULONG size_limit;\n        if (bits == BN_BITS2) {\n            size_limit = ~((BN_ULONG)0) - BN_get_word(rnd);\n        } else {\n            size_limit = (((BN_ULONG)1) << bits) - BN_get_word(rnd) - 1;\n        }\n        if (size_limit < maxdelta)\n            maxdelta = size_limit;\n    }\n    delta = 0;\n loop:\n    if (is_single_word) {\n        BN_ULONG rnd_word = BN_get_word(rnd);\n        for (i = 1; i < NUMPRIMES && primes[i] < rnd_word; i++) {\n            if ((mods[i] + delta) % primes[i] == 0) {\n                delta += 2;\n                if (delta > maxdelta)\n                    goto again;\n                goto loop;\n            }\n        }\n    } else {\n        for (i = 1; i < NUMPRIMES; i++) {\n            if (((mods[i] + delta) % primes[i]) <= 1) {\n                delta += 2;\n                if (delta > maxdelta)\n                    goto again;\n                goto loop;\n            }\n        }\n    }\n    if (!BN_add_word(rnd, delta))\n        return (0);\n    if (BN_num_bits(rnd) != bits)\n        goto again;\n    bn_check_top(rnd);\n    return (1);\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 testing, 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 (testing) {\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}']