test/bntest.c:163: error: BUFFER_OVERRUN_L3
  Offset: [-1, 1030] Size: [1, 1031] by call to `BN_bntest_rand`.
Showing all 12 steps of the trace


test/bntest.c:163:11: Call
161.           goto err;
162.   
163.       if (!(TEST_true(BN_bntest_rand(a, 1024, 1, 0))
                 ^
164.               && TEST_true(BN_bntest_rand(b, 1024, 1, 0))
165.               && TEST_ptr(BN_copy(c, a))

crypto/bn/bn_rand.c:111:1: Parameter `bits`
  109.   }
  110.   
  111. > int BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom)
  112.   {
  113.       return bnrand(TESTING, rnd, bits, top, bottom, NULL);

crypto/bn/bn_rand.c:113:12: Call
  111.   int BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom)
  112.   {
  113.       return bnrand(TESTING, rnd, bits, top, bottom, NULL);
                    ^
  114.   }
  115.   

crypto/bn/bn_rand.c:62:14: <Offset trace>
60.           unsigned char c;
61.   
62.           for (i = 0; i < bytes; i++) {
                   ^
63.               if (rand_bytes_ex(libctx, &c, 1) <= 0)
64.                   goto err;

crypto/bn/bn_rand.c:62:14: Assignment
    60.           unsigned char c;
    61.   
    62.           for (i = 0; i < bytes; i++) {
                       ^
    63.               if (rand_bytes_ex(libctx, &c, 1) <= 0)
    64.                   goto err;

crypto/bn/bn_rand.c:23:1: <Length trace>
21.   } BNRAND_FLAG;
22.   
23. > static int bnrand(BNRAND_FLAG flag, BIGNUM *rnd, int bits, int top, int bottom,
24.                     BN_CTX *ctx)
25.   {

crypto/bn/bn_rand.c:23:1: Parameter `bits`
    21.   } BNRAND_FLAG;
    22.   
    23. > static int bnrand(BNRAND_FLAG flag, BIGNUM *rnd, int bits, int top, int bottom,
    24.                     BN_CTX *ctx)
    25.   {

crypto/bn/bn_rand.c:39:5: Assignment
    37.           goto toosmall;
    38.   
    39.       bytes = (bits + 7) / 8;
              ^
    40.       bit = (bits - 1) % 8;
    41.       mask = 0xff << (bit + 1);

crypto/bn/bn_rand.c:43:11: Call
    41.       mask = 0xff << (bit + 1);
    42.   
    43.       buf = OPENSSL_malloc(bytes);
                    ^
    44.       if (buf == NULL) {
    45.           BNerr(BN_F_BNRAND, ERR_R_MALLOC_FAILURE);

crypto/mem.c:201:9: Assignment
      199.   
      200.       if (num == 0)
      201.           return NULL;
                     ^
      202.   
      203.       FAILTEST();

crypto/bn/bn_rand.c:43:5: Assignment
    41.       mask = 0xff << (bit + 1);
    42.   
    43.       buf = OPENSSL_malloc(bytes);
              ^
    44.       if (buf == NULL) {
    45.           BNerr(BN_F_BNRAND, ERR_R_MALLOC_FAILURE);

crypto/bn/bn_rand.c:88:9: Array access: Offset: [-1, 1030] Size: [1, 1031] by call to `BN_bntest_rand` 
    86.       buf[0] &= ~mask;
    87.       if (bottom)                 /* set bottom bit if requested */
    88.           buf[bytes - 1] |= 1;
                  ^
    89.       if (!BN_bin2bn(buf, bytes, rnd))
    90.           goto err;

static int bnrand(BNRAND_FLAG flag, BIGNUM *rnd, int bits, int top, int bottom,
                  BN_CTX *ctx)
{
    unsigned char *buf = NULL;
    int b, ret = 0, bit, bytes, mask;
    OPENSSL_CTX *libctx = bn_get_lib_ctx(ctx);
    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;
    }
    b = flag == NORMAL ? rand_bytes_ex(libctx, buf, bytes)
                       : rand_priv_bytes_ex(libctx, buf, bytes);
    if (b <= 0)
        goto err;
    if (flag == TESTING) {
        int i;
        unsigned char c;
        for (i = 0; i < bytes; i++) {
            if (rand_bytes_ex(libctx, &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 test_swap(void)\n{\n    BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;\n    int top, cond, st = 0;\n    if (!TEST_ptr(a = BN_new())\n            || !TEST_ptr(b = BN_new())\n            || !TEST_ptr(c = BN_new())\n            || !TEST_ptr(d = BN_new()))\n        goto err;\n    if (!(TEST_true(BN_bntest_rand(a, 1024, 1, 0))\n            && TEST_true(BN_bntest_rand(b, 1024, 1, 0))\n            && TEST_ptr(BN_copy(c, a))\n            && TEST_ptr(BN_copy(d, b))))\n        goto err;\n    top = BN_num_bits(a) / BN_BITS2;\n    BN_swap(a, b);\n    if (!equalBN("swap", a, d)\n            || !equalBN("swap", b, c))\n        goto err;\n    cond = 1;\n    BN_consttime_swap(cond, a, b, top);\n    if (!equalBN("cswap true", a, c)\n            || !equalBN("cswap true", b, d))\n        goto err;\n    cond = 0;\n    BN_consttime_swap(cond, a, b, top);\n    if (!equalBN("cswap false", a, c)\n            || !equalBN("cswap false", b, d))\n        goto err;\n    BN_set_flags(a, BN_FLG_CONSTTIME);\n    BN_swap(a, b);\n    if (!equalBN("swap, flags", a, d)\n            || !equalBN("swap, flags", b, c)\n            || !TEST_true(BN_get_flags(b, BN_FLG_CONSTTIME))\n            || !TEST_false(BN_get_flags(a, BN_FLG_CONSTTIME)))\n        goto err;\n    cond = 1;\n    BN_consttime_swap(cond, a, b, top);\n    if (!equalBN("cswap true, flags", a, c)\n            || !equalBN("cswap true, flags", b, d)\n            || !TEST_true(BN_get_flags(a, BN_FLG_CONSTTIME))\n            || !TEST_false(BN_get_flags(b, BN_FLG_CONSTTIME)))\n        goto err;\n    cond = 0;\n    BN_consttime_swap(cond, a, b, top);\n    if (!equalBN("cswap false, flags", a, c)\n            || !equalBN("cswap false, flags", b, d)\n            || !TEST_true(BN_get_flags(a, BN_FLG_CONSTTIME))\n            || !TEST_false(BN_get_flags(b, BN_FLG_CONSTTIME)))\n        goto err;\n    st = 1;\n err:\n    BN_free(a);\n    BN_free(b);\n    BN_free(c);\n    BN_free(d);\n    return st;\n}', 'int BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom)\n{\n    return bnrand(TESTING, rnd, bits, top, bottom, NULL);\n}', 'static int bnrand(BNRAND_FLAG flag, BIGNUM *rnd, int bits, int top, int bottom,\n                  BN_CTX *ctx)\n{\n    unsigned char *buf = NULL;\n    int b, ret = 0, bit, bytes, mask;\n    OPENSSL_CTX *libctx = bn_get_lib_ctx(ctx);\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    b = flag == NORMAL ? rand_bytes_ex(libctx, buf, bytes)\n                       : rand_priv_bytes_ex(libctx, buf, bytes);\n    if (b <= 0)\n        goto err;\n    if (flag == TESTING) {\n        int i;\n        unsigned char c;\n        for (i = 0; i < bytes; i++) {\n            if (rand_bytes_ex(libctx, &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    INCREMENT(malloc_count);\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    if (allow_customize) {\n        allow_customize = 0;\n    }\n#if !defined(OPENSSL_NO_CRYPTO_MDEBUG) && !defined(FIPS_MODE)\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    (void)(file); (void)(line);\n    ret = malloc(num);\n#endif\n    return ret;\n}']