crypto/bn/bn_gf2m.c:776: error: BUFFER_OVERRUN_L3
  Offset: [1, +oo] Size: [0, 8388607] by call to `BN_GF2m_mod_inv`.
Showing all 25 steps of the trace


crypto/bn/bn_gf2m.c:771:18: Call
769.       bn_check_top(xx);
770.       BN_CTX_start(ctx);
771.       if ((field = BN_CTX_get(ctx)) == NULL)
                        ^
772.           goto err;
773.       if (!BN_GF2m_arr2poly(p, field))

crypto/bn/bn_ctx.c:229:5: Call
  227.       }
  228.       /* OK, make sure the returned bignum is "zero" */
  229.       BN_zero(ret);
             ^
  230.       ctx->used++;
  231.       CTXDBG_RET(ctx, ret);

crypto/bn/bn_lib.c:367:15: Assignment
    365.       a->neg = 0;
    366.       a->d[0] = w;
    367.       a->top = (w ? 1 : 0);
                         ^
    368.       bn_check_top(a);
    369.       return 1;

crypto/bn/bn_lib.c:367:5: Assignment
    365.       a->neg = 0;
    366.       a->d[0] = w;
    367.       a->top = (w ? 1 : 0);
               ^
    368.       bn_check_top(a);
    369.       return 1;

crypto/bn/bn_gf2m.c:776:11: Call
774.           goto err;
775.   
776.       ret = BN_GF2m_mod_inv(r, xx, field, ctx);
                 ^
777.       bn_check_top(r);
778.   

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

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

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

crypto/bn/bn_lib.c:845:1: Parameter `a->top`
      843.   }
      844.   
      845. > int BN_is_zero(const BIGNUM *a)
      846.   {
      847.       return a->top == 0;

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

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

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

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

crypto/bn/bn_lib.c:845:1: Parameter `a->top`
        843.   }
        844.   
        845. > int BN_is_zero(const BIGNUM *a)
        846.   {
        847.       return a->top == 0;

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

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

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

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

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

crypto/bn/bn_ctx.c:229:5: Call
      227.       }
      228.       /* OK, make sure the returned bignum is "zero" */
      229.       BN_zero(ret);
                 ^
      230.       ctx->used++;
      231.       CTXDBG_RET(ctx, ret);

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

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

crypto/bn/bn_lcl.h:651:1: Parameter `*a->d`
          649.                            const BIGNUM *add, const BIGNUM *rem, BN_CTX *ctx);
          650.   
          651. > static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)
          652.   {
          653.       if (bits > (INT_MAX - BN_BITS2 + 1))

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

crypto/bn/bn_gf2m.c:629:13: Array access: Offset: [1, +oo] Size: [0, 8388607] by call to `BN_GF2m_mod_inv` 
    627.           bdp[0] = 1;
    628.           for (i = 1; i < top; i++)
    629.               bdp[i] = 0;
                       ^
    630.           b->top = top;
    631.           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;
}
['int BN_GF2m_mod_inv_arr(BIGNUM *r, const BIGNUM *xx, const int p[],\n                        BN_CTX *ctx)\n{\n    BIGNUM *field;\n    int ret = 0;\n    bn_check_top(xx);\n    BN_CTX_start(ctx);\n    if ((field = BN_CTX_get(ctx)) == NULL)\n        goto err;\n    if (!BN_GF2m_arr2poly(p, field))\n        goto err;\n    ret = BN_GF2m_mod_inv(r, xx, field, ctx);\n    bn_check_top(r);\n err:\n    BN_CTX_end(ctx);\n    return ret;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n    BIGNUM *ret;\n    CTXDBG_ENTRY("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    ctx->used++;\n    CTXDBG_RET(ctx, ret);\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    bn_check_top(a);\n    return 1;\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}', '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}']