test/bntest.c:832: error: BUFFER_OVERRUN_L3
  Offset: [1, +oo] Size: [0, 8388607] by call to `BN_GF2m_mod_div`.
Showing all 25 steps of the trace


test/bntest.c:832:19: Call
830.               goto err;
831.           for (j = 0; j < 2; j++) {
832.               if (!(TEST_true(BN_GF2m_mod_div(d, a, c, b[j], ctx))
                         ^
833.                       && TEST_true(BN_GF2m_mod_mul(e, d, c, b[j], ctx))
834.                       && TEST_true(BN_GF2m_mod_div(f, a, e, b[j], ctx))

crypto/bn/bn_gf2m.c:790:1: Parameter `r->top`
  788.    * or y, x could equal y.
  789.    */
  790. > int BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *y, const BIGNUM *x,
  791.                       const BIGNUM *p, BN_CTX *ctx)
  792.   {

test/bntest.c:832:19: Call
830.               goto err;
831.           for (j = 0; j < 2; j++) {
832.               if (!(TEST_true(BN_GF2m_mod_div(d, a, c, b[j], ctx))
                         ^
833.                       && TEST_true(BN_GF2m_mod_mul(e, d, c, b[j], ctx))
834.                       && TEST_true(BN_GF2m_mod_div(f, a, e, b[j], ctx))

crypto/bn/bn_gf2m.c:790:1: Parameter `p->top`
  788.    * or y, x could equal y.
  789.    */
  790. > int BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *y, const BIGNUM *x,
  791.                       const BIGNUM *p, BN_CTX *ctx)
  792.   {

crypto/bn/bn_gf2m.c:805:10: Call
  803.           goto err;
  804.   
  805.       if (!BN_GF2m_mod_inv(xinv, x, p, ctx))
                  ^
  806.           goto err;
  807.       if (!BN_GF2m_mod_mul(r, y, xinv, p, ctx))

crypto/bn/bn_gf2m.c:724:1: Parameter `p->top`
    722.    * But it does eliminate first order deduction on the input.
    723.    */
    724. > int BN_GF2m_mod_inv(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
    725.   {
    726.       BIGNUM *b = NULL;

crypto/bn/bn_gf2m.c:735:30: Call
    733.       /* generate blinding value */
    734.       do {
    735.           if (!BN_priv_rand(b, BN_num_bits(p) - 1,
                                        ^
    736.                             BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY))
    737.               goto err;

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

crypto/bn/bn_lib.c:864:1: Parameter `a->top`
        862.   }
        863.   
        864. > int BN_is_zero(const BIGNUM *a)
        865.   {
        866.       return a->top == 0;

crypto/bn/bn_gf2m.c:741:10: Call
    739.   
    740.       /* r := a * b */
    741.       if (!BN_GF2m_mod_mul(r, a, b, p, ctx))
                    ^
    742.           goto err;
    743.   

crypto/bn/bn_gf2m.c:465:1: Parameter `p->top`
      463.    * BN_GF2m_mod_mul_arr function.
      464.    */
      465. > int BN_GF2m_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
      466.                       const BIGNUM *p, BN_CTX *ctx)
      467.   {

crypto/bn/bn_gf2m.c:469:21: Call
      467.   {
      468.       int ret = 0;
      469.       const int max = BN_num_bits(p) + 1;
                                 ^
      470.       int *arr = NULL;
      471.       bn_check_top(a);

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

crypto/bn/bn_lib.c:864:1: Parameter `a->top`
          862.   }
          863.   
          864. > int BN_is_zero(const BIGNUM *a)
          865.   {
          866.       return a->top == 0;

crypto/bn/bn_gf2m.c:745:10: Call
    743.   
    744.       /* r := 1/(a * b) */
    745.       if (!BN_GF2m_mod_inv_vartime(r, r, p, ctx))
                    ^
    746.           goto err;
    747.   

crypto/bn/bn_gf2m.c:630:14: <Offset trace>
628.           bdp = b->d;
629.           bdp[0] = 1;
630.           for (i = 1; i < top; i++)
                    ^
631.               bdp[i] = 0;
632.           b->top = top;

crypto/bn/bn_gf2m.c:630:14: Assignment
      628.           bdp = b->d;
      629.           bdp[0] = 1;
      630.           for (i = 1; i < top; i++)
                          ^
      631.               bdp[i] = 0;
      632.           b->top = top;

crypto/bn/bn_gf2m.c:563:9: <Length trace>
561.       BN_CTX_start(ctx);
562.   
563.       b = BN_CTX_get(ctx);
               ^
564.       c = BN_CTX_get(ctx);
565.       u = BN_CTX_get(ctx);

crypto/bn/bn_gf2m.c:563:9: Call
      561.       BN_CTX_start(ctx);
      562.   
      563.       b = BN_CTX_get(ctx);
                     ^
      564.       c = BN_CTX_get(ctx);
      565.       u = BN_CTX_get(ctx);

crypto/bn/bn_ctx.c:219:5: Call
        217.       }
        218.       /* OK, make sure the returned bignum is "zero" */
        219.       BN_zero(ret);
                   ^
        220.       /* clear BN_FLG_CONSTTIME if leaked from previous frames */
        221.       ret->flags &= (~BN_FLG_CONSTTIME);

crypto/bn/bn_lib.c:359:1: Parameter `*a->d`
          357.   }
          358.   
          359. > int BN_set_word(BIGNUM *a, BN_ULONG w)
          360.   {
          361.       bn_check_top(a);

crypto/bn/bn_lib.c:362:9: Call
          360.   {
          361.       bn_check_top(a);
          362.       if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)
                         ^
          363.           return 0;
          364.       a->neg = 0;

crypto/bn/bn_lcl.h:660:1: Parameter `*a->d`
            658.                            const BIGNUM *add, const BIGNUM *rem, BN_CTX *ctx);
            659.   
            660. > static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)
            661.   {
            662.       if (bits > (INT_MAX - BN_BITS2 + 1))

crypto/bn/bn_gf2m.c:628:9: Assignment
      626.           if (!bn_wexpand(b, top))
      627.             goto err;
      628.           bdp = b->d;
                     ^
      629.           bdp[0] = 1;
      630.           for (i = 1; i < top; i++)

crypto/bn/bn_gf2m.c:631:13: Array access: Offset: [1, +oo] Size: [0, 8388607] by call to `BN_GF2m_mod_div` 
      629.           bdp[0] = 1;
      630.           for (i = 1; i < top; i++)
      631.               bdp[i] = 0;
                         ^
      632.           b->top = top;
      633.           if (!bn_wexpand(c, top))

static int BN_GF2m_mod_inv_vartime(BIGNUM *r, const BIGNUM *a,
                                   const BIGNUM *p, BN_CTX *ctx)
{
    BIGNUM *b, *c = NULL, *u = NULL, *v = NULL, *tmp;
    int ret = 0;
    bn_check_top(a);
    bn_check_top(p);
    BN_CTX_start(ctx);
    b = BN_CTX_get(ctx);
    c = BN_CTX_get(ctx);
    u = BN_CTX_get(ctx);
    v = BN_CTX_get(ctx);
    if (v == NULL)
        goto err;
    if (!BN_GF2m_mod(u, a, p))
        goto err;
    if (BN_is_zero(u))
        goto err;
    if (!BN_copy(v, p))
        goto err;
# if 0
    if (!BN_one(b))
        goto err;
    while (1) {
        while (!BN_is_odd(u)) {
            if (BN_is_zero(u))
                goto err;
            if (!BN_rshift1(u, u))
                goto err;
            if (BN_is_odd(b)) {
                if (!BN_GF2m_add(b, b, p))
                    goto err;
            }
            if (!BN_rshift1(b, b))
                goto err;
        }
        if (BN_abs_is_word(u, 1))
            break;
        if (BN_num_bits(u) < BN_num_bits(v)) {
            tmp = u;
            u = v;
            v = tmp;
            tmp = b;
            b = c;
            c = tmp;
        }
        if (!BN_GF2m_add(u, u, v))
            goto err;
        if (!BN_GF2m_add(b, b, c))
            goto err;
    }
# else
    {
        int i;
        int ubits = BN_num_bits(u);
        int vbits = BN_num_bits(v);
        int top = p->top;
        BN_ULONG *udp, *bdp, *vdp, *cdp;
        if (!bn_wexpand(u, top))
            goto err;
        udp = u->d;
        for (i = u->top; i < top; i++)
            udp[i] = 0;
        u->top = top;
        if (!bn_wexpand(b, top))
          goto err;
        bdp = b->d;
        bdp[0] = 1;
        for (i = 1; i < top; i++)
            bdp[i] = 0;
        b->top = top;
        if (!bn_wexpand(c, top))
          goto err;
        cdp = c->d;
        for (i = 0; i < top; i++)
            cdp[i] = 0;
        c->top = top;
        vdp = v->d;
        while (1) {
            while (ubits && !(udp[0] & 1)) {
                BN_ULONG u0, u1, b0, b1, mask;
                u0 = udp[0];
                b0 = bdp[0];
                mask = (BN_ULONG)0 - (b0 & 1);
                b0 ^= p->d[0] & mask;
                for (i = 0; i < top - 1; i++) {
                    u1 = udp[i + 1];
                    udp[i] = ((u0 >> 1) | (u1 << (BN_BITS2 - 1))) & BN_MASK2;
                    u0 = u1;
                    b1 = bdp[i + 1] ^ (p->d[i + 1] & mask);
                    bdp[i] = ((b0 >> 1) | (b1 << (BN_BITS2 - 1))) & BN_MASK2;
                    b0 = b1;
                }
                udp[i] = u0 >> 1;
                bdp[i] = b0 >> 1;
                ubits--;
            }
            if (ubits <= BN_BITS2) {
                if (udp[0] == 0)
                    goto err;
                if (udp[0] == 1)
                    break;
            }
            if (ubits < vbits) {
                i = ubits;
                ubits = vbits;
                vbits = i;
                tmp = u;
                u = v;
                v = tmp;
                tmp = b;
                b = c;
                c = tmp;
                udp = vdp;
                vdp = v->d;
                bdp = cdp;
                cdp = c->d;
            }
            for (i = 0; i < top; i++) {
                udp[i] ^= vdp[i];
                bdp[i] ^= cdp[i];
            }
            if (ubits == vbits) {
                BN_ULONG ul;
                int utop = (ubits - 1) / BN_BITS2;
                while ((ul = udp[utop]) == 0 && utop)
                    utop--;
                ubits = utop * BN_BITS2 + BN_num_bits_word(ul);
            }
        }
        bn_correct_top(b);
    }
# endif
    if (!BN_copy(r, b))
        goto err;
    bn_check_top(r);
    ret = 1;
 err:
# ifdef BN_DEBUG
    bn_correct_top(c);
    bn_correct_top(u);
    bn_correct_top(v);
# endif
    BN_CTX_end(ctx);
    return ret;
}
['static int test_gf2m_moddiv(void)\n{\n    BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;\n    BIGNUM *e = NULL, *f = NULL;\n    int i, j, st = 0;\n    if (!TEST_ptr(a = BN_new())\n            || !TEST_ptr(b[0] = BN_new())\n            || !TEST_ptr(b[1] = BN_new())\n            || !TEST_ptr(c = BN_new())\n            || !TEST_ptr(d = BN_new())\n            || !TEST_ptr(e = BN_new())\n            || !TEST_ptr(f = BN_new()))\n        goto err;\n    if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))\n            && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))\n        goto err;\n    for (i = 0; i < NUM0; i++) {\n        if (!(TEST_true(BN_bntest_rand(a, 512, 0, 0))\n                && TEST_true(BN_bntest_rand(c, 512, 0, 0))))\n            goto err;\n        for (j = 0; j < 2; j++) {\n            if (!(TEST_true(BN_GF2m_mod_div(d, a, c, b[j], ctx))\n                    && TEST_true(BN_GF2m_mod_mul(e, d, c, b[j], ctx))\n                    && TEST_true(BN_GF2m_mod_div(f, a, e, b[j], ctx))\n                    && TEST_BN_eq_one(f)))\n                goto err;\n        }\n    }\n    st = 1;\n err:\n    BN_free(a);\n    BN_free(b[0]);\n    BN_free(b[1]);\n    BN_free(c);\n    BN_free(d);\n    BN_free(e);\n    BN_free(f);\n    return st;\n}', 'int BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *y, const BIGNUM *x,\n                    const BIGNUM *p, BN_CTX *ctx)\n{\n    BIGNUM *xinv = NULL;\n    int ret = 0;\n    bn_check_top(y);\n    bn_check_top(x);\n    bn_check_top(p);\n    BN_CTX_start(ctx);\n    xinv = BN_CTX_get(ctx);\n    if (xinv == NULL)\n        goto err;\n    if (!BN_GF2m_mod_inv(xinv, x, p, ctx))\n        goto err;\n    if (!BN_GF2m_mod_mul(r, y, xinv, p, ctx))\n        goto err;\n    bn_check_top(r);\n    ret = 1;\n err:\n    BN_CTX_end(ctx);\n    return ret;\n}', 'int BN_GF2m_mod_inv(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)\n{\n    BIGNUM *b = NULL;\n    int ret = 0;\n    BN_CTX_start(ctx);\n    if ((b = BN_CTX_get(ctx)) == NULL)\n        goto err;\n    do {\n        if (!BN_priv_rand(b, BN_num_bits(p) - 1,\n                          BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY))\n            goto err;\n    } while (BN_is_zero(b));\n    if (!BN_GF2m_mod_mul(r, a, b, p, ctx))\n        goto err;\n    if (!BN_GF2m_mod_inv_vartime(r, r, p, ctx))\n        goto err;\n    if (!BN_GF2m_mod_mul(r, r, b, p, ctx))\n        goto err;\n    ret = 1;\n err:\n    BN_CTX_end(ctx);\n    return ret;\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}', 'int BN_GF2m_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n                    const BIGNUM *p, BN_CTX *ctx)\n{\n    int ret = 0;\n    const int max = BN_num_bits(p) + 1;\n    int *arr = NULL;\n    bn_check_top(a);\n    bn_check_top(b);\n    bn_check_top(p);\n    if ((arr = OPENSSL_malloc(sizeof(*arr) * max)) == NULL)\n        goto err;\n    ret = BN_GF2m_poly2arr(p, arr, max);\n    if (!ret || ret > max) {\n        BNerr(BN_F_BN_GF2M_MOD_MUL, BN_R_INVALID_LENGTH);\n        goto err;\n    }\n    ret = BN_GF2m_mod_mul_arr(r, a, b, arr, ctx);\n    bn_check_top(r);\n err:\n    OPENSSL_free(arr);\n    return ret;\n}', 'static int BN_GF2m_mod_inv_vartime(BIGNUM *r, const BIGNUM *a,\n                                   const BIGNUM *p, BN_CTX *ctx)\n{\n    BIGNUM *b, *c = NULL, *u = NULL, *v = NULL, *tmp;\n    int ret = 0;\n    bn_check_top(a);\n    bn_check_top(p);\n    BN_CTX_start(ctx);\n    b = BN_CTX_get(ctx);\n    c = BN_CTX_get(ctx);\n    u = BN_CTX_get(ctx);\n    v = BN_CTX_get(ctx);\n    if (v == NULL)\n        goto err;\n    if (!BN_GF2m_mod(u, a, p))\n        goto err;\n    if (BN_is_zero(u))\n        goto err;\n    if (!BN_copy(v, p))\n        goto err;\n# if 0\n    if (!BN_one(b))\n        goto err;\n    while (1) {\n        while (!BN_is_odd(u)) {\n            if (BN_is_zero(u))\n                goto err;\n            if (!BN_rshift1(u, u))\n                goto err;\n            if (BN_is_odd(b)) {\n                if (!BN_GF2m_add(b, b, p))\n                    goto err;\n            }\n            if (!BN_rshift1(b, b))\n                goto err;\n        }\n        if (BN_abs_is_word(u, 1))\n            break;\n        if (BN_num_bits(u) < BN_num_bits(v)) {\n            tmp = u;\n            u = v;\n            v = tmp;\n            tmp = b;\n            b = c;\n            c = tmp;\n        }\n        if (!BN_GF2m_add(u, u, v))\n            goto err;\n        if (!BN_GF2m_add(b, b, c))\n            goto err;\n    }\n# else\n    {\n        int i;\n        int ubits = BN_num_bits(u);\n        int vbits = BN_num_bits(v);\n        int top = p->top;\n        BN_ULONG *udp, *bdp, *vdp, *cdp;\n        if (!bn_wexpand(u, top))\n            goto err;\n        udp = u->d;\n        for (i = u->top; i < top; i++)\n            udp[i] = 0;\n        u->top = top;\n        if (!bn_wexpand(b, top))\n          goto err;\n        bdp = b->d;\n        bdp[0] = 1;\n        for (i = 1; i < top; i++)\n            bdp[i] = 0;\n        b->top = top;\n        if (!bn_wexpand(c, top))\n          goto err;\n        cdp = c->d;\n        for (i = 0; i < top; i++)\n            cdp[i] = 0;\n        c->top = top;\n        vdp = v->d;\n        while (1) {\n            while (ubits && !(udp[0] & 1)) {\n                BN_ULONG u0, u1, b0, b1, mask;\n                u0 = udp[0];\n                b0 = bdp[0];\n                mask = (BN_ULONG)0 - (b0 & 1);\n                b0 ^= p->d[0] & mask;\n                for (i = 0; i < top - 1; i++) {\n                    u1 = udp[i + 1];\n                    udp[i] = ((u0 >> 1) | (u1 << (BN_BITS2 - 1))) & BN_MASK2;\n                    u0 = u1;\n                    b1 = bdp[i + 1] ^ (p->d[i + 1] & mask);\n                    bdp[i] = ((b0 >> 1) | (b1 << (BN_BITS2 - 1))) & BN_MASK2;\n                    b0 = b1;\n                }\n                udp[i] = u0 >> 1;\n                bdp[i] = b0 >> 1;\n                ubits--;\n            }\n            if (ubits <= BN_BITS2) {\n                if (udp[0] == 0)\n                    goto err;\n                if (udp[0] == 1)\n                    break;\n            }\n            if (ubits < vbits) {\n                i = ubits;\n                ubits = vbits;\n                vbits = i;\n                tmp = u;\n                u = v;\n                v = tmp;\n                tmp = b;\n                b = c;\n                c = tmp;\n                udp = vdp;\n                vdp = v->d;\n                bdp = cdp;\n                cdp = c->d;\n            }\n            for (i = 0; i < top; i++) {\n                udp[i] ^= vdp[i];\n                bdp[i] ^= cdp[i];\n            }\n            if (ubits == vbits) {\n                BN_ULONG ul;\n                int utop = (ubits - 1) / BN_BITS2;\n                while ((ul = udp[utop]) == 0 && utop)\n                    utop--;\n                ubits = utop * BN_BITS2 + BN_num_bits_word(ul);\n            }\n        }\n        bn_correct_top(b);\n    }\n# endif\n    if (!BN_copy(r, b))\n        goto err;\n    bn_check_top(r);\n    ret = 1;\n err:\n# ifdef BN_DEBUG\n    bn_correct_top(c);\n    bn_correct_top(u);\n    bn_correct_top(v);\n# endif\n    BN_CTX_end(ctx);\n    return ret;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n    BIGNUM *ret;\n    CTXDBG("ENTER BN_CTX_get()", ctx);\n    if (ctx->err_stack || ctx->too_many)\n        return NULL;\n    if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n        ctx->too_many = 1;\n        BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n        return NULL;\n    }\n    BN_zero(ret);\n    ret->flags &= (~BN_FLG_CONSTTIME);\n    ctx->used++;\n    CTXDBG("LEAVE BN_CTX_get()", ctx);\n    return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n    bn_check_top(a);\n    if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n        return 0;\n    a->neg = 0;\n    a->d[0] = w;\n    a->top = (w ? 1 : 0);\n    a->flags &= ~BN_FLG_FIXED_TOP;\n    bn_check_top(a);\n    return 1;\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n    if (bits > (INT_MAX - BN_BITS2 + 1))\n        return NULL;\n    if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n        return a;\n    return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}']