test/ectest.c:924: error: BUFFER_OVERRUN_L3
  Offset: [-1, +oo] Size: [1, +oo] by call to `BN_rand`.
Showing all 14 steps of the trace


test/ectest.c:924:14: Call
922.               goto err;
923.   
924.           if (!TEST_true(BN_rand(y, BN_num_bits(y), 0, 0))
                    ^
925.               || !TEST_true(BN_add(z, z, y)))
926.               goto err;

crypto/bn/bn_lib.c:167:9: Assignment
  165.   
  166.       if (BN_is_zero(a))
  167.           return 0;
                 ^
  168.       return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));
  169.   }

test/ectest.c:924:14: Call
922.               goto err;
923.   
924.           if (!TEST_true(BN_rand(y, BN_num_bits(y), 0, 0))
                    ^
925.               || !TEST_true(BN_add(z, z, y)))
926.               goto err;

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_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 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;
}
['static int char2_curve_test(int n)\n{\n    int r = 0;\n    BN_CTX *ctx = NULL;\n    BIGNUM *p = NULL, *a = NULL, *b = NULL;\n    BIGNUM *x = NULL, *y = NULL, *z = NULL, *cof = NULL, *yplusone = NULL;\n    EC_GROUP *group = NULL, *variable = NULL;\n    EC_POINT *P = NULL, *Q = NULL, *R = NULL;\n    const EC_POINT *points[3];\n    const BIGNUM *scalars[3];\n    struct c2_curve_test *const test = char2_curve_tests + n;\n    if (!TEST_ptr(ctx = BN_CTX_new())\n        || !TEST_ptr(p = BN_new())\n        || !TEST_ptr(a = BN_new())\n        || !TEST_ptr(b = BN_new())\n        || !TEST_ptr(x = BN_new())\n        || !TEST_ptr(y = BN_new())\n        || !TEST_ptr(z = BN_new())\n        || !TEST_ptr(yplusone = BN_new())\n        || !TEST_true(BN_hex2bn(&p, test->p))\n        || !TEST_true(BN_hex2bn(&a, test->a))\n        || !TEST_true(BN_hex2bn(&b, test->b))\n        || !TEST_true(group = EC_GROUP_new(EC_GF2m_simple_method()))\n        || !TEST_true(EC_GROUP_set_curve_GF2m(group, p, a, b, ctx))\n        || !TEST_ptr(P = EC_POINT_new(group))\n        || !TEST_ptr(Q = EC_POINT_new(group))\n        || !TEST_ptr(R = EC_POINT_new(group))\n        || !TEST_true(BN_hex2bn(&x, test->x))\n        || !TEST_true(BN_hex2bn(&y, test->y))\n        || !TEST_true(BN_add(yplusone, y, BN_value_one())))\n        goto err;\n# ifdef OPENSSL_EC_BIN_PT_COMP\n    if (!TEST_false(EC_POINT_set_affine_coordinates_GF2m(group, P, x, yplusone,\n                                                         ctx))\n        || !TEST_true(EC_POINT_set_compressed_coordinates_GF2m(group, P, x,\n                                                               test->y_bit,\n                                                               ctx))\n        || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)\n        || !TEST_true(BN_hex2bn(&z, test->order))\n        || !TEST_true(BN_hex2bn(&cof, test->cof))\n        || !TEST_true(EC_GROUP_set_generator(group, P, z, cof))\n        || !TEST_true(EC_POINT_get_affine_coordinates_GF2m(group, P, x, y,\n                                                           ctx)))\n        goto err;\n    TEST_info("%s -- Generator", test->name);\n    test_output_bignum("x", x);\n    test_output_bignum("y", y);\n    if (!TEST_true(BN_hex2bn(&z, test->y))\n        || !TEST_BN_eq(y, z))\n        goto err;\n# else\n    if (!TEST_false(EC_POINT_set_affine_coordinates_GF2m(group, P, x, yplusone,\n                    ctx))\n        || !TEST_true(EC_POINT_set_affine_coordinates_GF2m(group, P, x, y, ctx))\n        || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)\n        || !TEST_true(BN_hex2bn(&z, test->order))\n        || !TEST_true(BN_hex2bn(&cof, test->cof))\n        || !TEST_true(EC_GROUP_set_generator(group, P, z, cof)))\n        goto err;\n    TEST_info("%s -- Generator:", test->name);\n    test_output_bignum("x", x);\n    test_output_bignum("y", y);\n# endif\n    if (!TEST_int_eq(EC_GROUP_get_degree(group), test->degree)\n        || !group_order_tests(group)\n        || !TEST_ptr(variable = EC_GROUP_new(EC_GROUP_method_of(group)))\n        || !TEST_true(EC_GROUP_copy(variable, group)))\n        goto err;\n    if (n == OSSL_NELEM(char2_curve_tests) - 1) {\n        if (!TEST_true(EC_POINT_set_affine_coordinates_GF2m(group, P, x, y,\n                                                                ctx))\n            || !TEST_true(EC_POINT_copy(Q, P))\n            || !TEST_false(EC_POINT_is_at_infinity(group, Q))\n            || !TEST_true(EC_POINT_dbl(group, P, P, ctx))\n            || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)\n            || !TEST_true(EC_POINT_invert(group, Q, ctx))\n            || !TEST_true(EC_POINT_add(group, R, P, Q, ctx))\n            || !TEST_true(EC_POINT_add(group, R, R, Q, ctx))\n            || !TEST_true(EC_POINT_is_at_infinity(group, R))\n            || !TEST_false(EC_POINT_is_at_infinity(group, Q)))\n            goto err;\n        points[0] = Q;\n        points[1] = Q;\n        points[2] = Q;\n        if (!TEST_true(BN_add(y, z, BN_value_one()))\n            || !TEST_BN_even(y)\n            || !TEST_true(BN_rshift1(y, y)))\n            goto err;\n        scalars[0] = y;\n        scalars[1] = y;\n        TEST_note("combined multiplication ...");\n        if (!TEST_true(EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))\n            || !TEST_true(EC_POINTs_mul(group, R, z, 2, points, scalars, ctx))\n            || !TEST_int_eq(0, EC_POINT_cmp(group, P, R, ctx))\n            || !TEST_int_eq(0, EC_POINT_cmp(group, R, Q, ctx)))\n            goto err;\n        if (!TEST_true(BN_rand(y, BN_num_bits(y), 0, 0))\n            || !TEST_true(BN_add(z, z, y)))\n            goto err;\n        BN_set_negative(z, 1);\n        scalars[0] = y;\n        scalars[1] = z;\n        if (!TEST_true(EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))\n            || !TEST_true(EC_POINT_is_at_infinity(group, P)))\n            goto err;\n        if (!TEST_true(BN_rand(x, BN_num_bits(y) - 1, 0, 0))\n            || !TEST_true(BN_add(z, x, y)))\n            goto err;\n        BN_set_negative(z, 1);\n        scalars[0] = x;\n        scalars[1] = y;\n        scalars[2] = z;\n        if (!TEST_true(EC_POINTs_mul(group, P, NULL, 3, points, scalars, ctx))\n            || !TEST_true(EC_POINT_is_at_infinity(group, P)))\n            goto err;;\n    }\n    r = 1;\nerr:\n    BN_CTX_free(ctx);\n    BN_free(p);\n    BN_free(a);\n    BN_free(b);\n    BN_free(x);\n    BN_free(y);\n    BN_free(z);\n    BN_free(yplusone);\n    BN_free(cof);\n    EC_POINT_free(P);\n    EC_POINT_free(Q);\n    EC_POINT_free(R);\n    EC_GROUP_free(group);\n    EC_GROUP_free(variable);\n    return r;\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_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}']