crypto/rsa/rsa_ossl.c:671: error: BUFFER_OVERRUN_L3
  Offset: [1, +oo] Size: [0, 8388607] by call to `BN_div`.
Showing all 13 steps of the trace


crypto/rsa/rsa_ossl.c:599:1: Parameter `I->top`
597.   }
598.   
599. > static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
600.   {
601.       BIGNUM *r1, *m1, *vrfy;

crypto/rsa/rsa_ossl.c:671:14: Call
669.               c = I;
670.           }
671.           if (!BN_mod(r1, c, rsa->q, ctx)) {
                    ^
672.               BN_free(local_c);
673.               goto err;

crypto/bn/bn_div.c:140:1: Parameter `num->top`
  138.    * If 'dv' or 'rm' is NULL, the respective value is not returned.
  139.    */
  140. > int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,
  141.              BN_CTX *ctx)
  142.   {

crypto/bn/bn_div.c:210:11: Call
  208.       sdiv->neg = 0;
  209.       norm_shift += BN_BITS2;
  210.       if (!(BN_lshift(snum, num, norm_shift)))
                   ^
  211.           goto err;
  212.       snum->neg = 0;

crypto/bn/bn_shift.c:81:1: <Offset trace>
79.   }
80.   
81. > int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)
82.   {
83.       int i, nw, lb, rb;

crypto/bn/bn_shift.c:81:1: Parameter `n`
    79.   }
    80.   
    81. > int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)
    82.   {
    83.       int i, nw, lb, rb;

crypto/bn/bn_shift.c:96:5: Assignment
    94.   
    95.       r->neg = a->neg;
    96.       nw = n / BN_BITS2;
              ^
    97.       if (bn_wexpand(r, a->top + nw + 1) == NULL)
    98.           return (0);

crypto/bn/bn_shift.c:81:1: <Length trace>
79.   }
80.   
81. > int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)
82.   {
83.       int i, nw, lb, rb;

crypto/bn/bn_shift.c:81:1: Parameter `*r->d`
    79.   }
    80.   
    81. > int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)
    82.   {
    83.       int i, nw, lb, rb;

crypto/bn/bn_shift.c:97:9: Call
    95.       r->neg = a->neg;
    96.       nw = n / BN_BITS2;
    97.       if (bn_wexpand(r, a->top + nw + 1) == NULL)
                   ^
    98.           return (0);
    99.       lb = n % BN_BITS2;

crypto/bn/bn_lib.c:1014:1: Parameter `*a->d`
      1012.   }
      1013.   
      1014. > BIGNUM *bn_wexpand(BIGNUM *a, int words)
      1015.   {
      1016.       return (words <= a->dmax) ? a : bn_expand2(a, words);

crypto/bn/bn_shift.c:102:5: Assignment
    100.       rb = BN_BITS2 - lb;
    101.       f = a->d;
    102.       t = r->d;
               ^
    103.       t[a->top + nw] = 0;
    104.       if (lb == 0)

crypto/bn/bn_shift.c:110:13: Array access: Offset: [1, +oo] Size: [0, 8388607] by call to `BN_div` 
    108.           for (i = a->top - 1; i >= 0; i--) {
    109.               l = f[i];
    110.               t[nw + i + 1] |= (l >> rb) & BN_MASK2;
                       ^
    111.               t[nw + i] = (l << lb) & BN_MASK2;
    112.           }

int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)
{
    int i, nw, lb, rb;
    BN_ULONG *t, *f;
    BN_ULONG l;
    bn_check_top(r);
    bn_check_top(a);
    if (n < 0) {
        BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);
        return 0;
    }
    r->neg = a->neg;
    nw = n / BN_BITS2;
    if (bn_wexpand(r, a->top + nw + 1) == NULL)
        return (0);
    lb = n % BN_BITS2;
    rb = BN_BITS2 - lb;
    f = a->d;
    t = r->d;
    t[a->top + nw] = 0;
    if (lb == 0)
        for (i = a->top - 1; i >= 0; i--)
            t[nw + i] = f[i];
    else
        for (i = a->top - 1; i >= 0; i--) {
            l = f[i];
            t[nw + i + 1] |= (l >> rb) & BN_MASK2;
            t[nw + i] = (l << lb) & BN_MASK2;
        }
    memset(t, 0, sizeof(*t) * nw);
    r->top = a->top + nw + 1;
    bn_correct_top(r);
    bn_check_top(r);
    return (1);
}
['static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)\n{\n    BIGNUM *r1, *m1, *vrfy;\n    int ret = 0;\n    BN_CTX_start(ctx);\n    r1 = BN_CTX_get(ctx);\n    m1 = BN_CTX_get(ctx);\n    vrfy = BN_CTX_get(ctx);\n    {\n        BIGNUM *local_p = NULL, *local_q = NULL;\n        BIGNUM *p = NULL, *q = NULL;\n        if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) {\n            local_p = p = BN_new();\n            if (p == NULL)\n                goto err;\n            BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);\n            local_q = q = BN_new();\n            if (q == NULL) {\n                BN_free(local_p);\n                goto err;\n            }\n            BN_with_flags(q, rsa->q, BN_FLG_CONSTTIME);\n        } else {\n            p = rsa->p;\n            q = rsa->q;\n        }\n        if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) {\n            if (!BN_MONT_CTX_set_locked\n                (&rsa->_method_mod_p, rsa->lock, p, ctx)\n                || !BN_MONT_CTX_set_locked(&rsa->_method_mod_q,\n                                           rsa->lock, q, ctx)) {\n                BN_free(local_p);\n                BN_free(local_q);\n                goto err;\n            }\n        }\n        BN_free(local_p);\n        BN_free(local_q);\n    }\n    if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)\n        if (!BN_MONT_CTX_set_locked\n            (&rsa->_method_mod_n, rsa->lock, rsa->n, ctx))\n            goto err;\n    {\n        BIGNUM *local_c = NULL;\n        const BIGNUM *c;\n        if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) {\n            local_c = BN_new();\n            if (local_c == NULL)\n                goto err;\n            BN_with_flags(local_c, I, BN_FLG_CONSTTIME);\n            c = local_c;\n        } else {\n            c = I;\n        }\n        if (!BN_mod(r1, c, rsa->q, ctx)) {\n            BN_free(local_c);\n            goto err;\n        }\n        {\n            BIGNUM *local_dmq1 = NULL, *dmq1;\n            if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) {\n                dmq1 = local_dmq1 = BN_new();\n                if (local_dmq1 == NULL) {\n                    BN_free(local_c);\n                    goto err;\n                }\n                BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);\n            } else {\n                dmq1 = rsa->dmq1;\n            }\n            if (!rsa->meth->bn_mod_exp(m1, r1, dmq1, rsa->q, ctx,\n                rsa->_method_mod_q)) {\n                BN_free(local_c);\n                BN_free(local_dmq1);\n                goto err;\n            }\n            BN_free(local_dmq1);\n        }\n        if (!BN_mod(r1, c, rsa->p, ctx)) {\n            BN_free(local_c);\n            goto err;\n        }\n        BN_free(local_c);\n    }\n    {\n        BIGNUM *local_dmp1 = NULL, *dmp1;\n        if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) {\n            dmp1 = local_dmp1 = BN_new();\n            if (local_dmp1 == NULL)\n                goto err;\n            BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);\n        } else {\n            dmp1 = rsa->dmp1;\n        }\n        if (!rsa->meth->bn_mod_exp(r0, r1, dmp1, rsa->p, ctx,\n                                   rsa->_method_mod_p)) {\n            BN_free(local_dmp1);\n            goto err;\n        }\n        BN_free(local_dmp1);\n    }\n    if (!BN_sub(r0, r0, m1))\n        goto err;\n    if (BN_is_negative(r0))\n        if (!BN_add(r0, r0, rsa->p))\n            goto err;\n    if (!BN_mul(r1, r0, rsa->iqmp, ctx))\n        goto err;\n    {\n        BIGNUM *local_r1 = NULL, *pr1;\n        if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) {\n            pr1 = local_r1 = BN_new();\n            if (local_r1 == NULL)\n                goto err;\n            BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);\n        } else {\n            pr1 = r1;\n        }\n        if (!BN_mod(r0, pr1, rsa->p, ctx)) {\n            BN_free(local_r1);\n            goto err;\n        }\n        BN_free(local_r1);\n    }\n    if (BN_is_negative(r0))\n        if (!BN_add(r0, r0, rsa->p))\n            goto err;\n    if (!BN_mul(r1, r0, rsa->q, ctx))\n        goto err;\n    if (!BN_add(r0, r1, m1))\n        goto err;\n    if (rsa->e && rsa->n) {\n        if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx,\n                                   rsa->_method_mod_n))\n            goto err;\n        if (!BN_sub(vrfy, vrfy, I))\n            goto err;\n        if (!BN_mod(vrfy, vrfy, rsa->n, ctx))\n            goto err;\n        if (BN_is_negative(vrfy))\n            if (!BN_add(vrfy, vrfy, rsa->n))\n                goto err;\n        if (!BN_is_zero(vrfy)) {\n            BIGNUM *local_d = NULL;\n            BIGNUM *d = NULL;\n            if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) {\n                local_d = d = BN_new();\n                if (d == NULL)\n                    goto err;\n                BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);\n            } else {\n                d = rsa->d;\n            }\n            if (!rsa->meth->bn_mod_exp(r0, I, d, rsa->n, ctx,\n                                       rsa->_method_mod_n)) {\n                BN_free(local_d);\n                goto err;\n            }\n            BN_free(local_d);\n        }\n    }\n    ret = 1;\n err:\n    BN_CTX_end(ctx);\n    return (ret);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n           BN_CTX *ctx)\n{\n    int norm_shift, i, loop;\n    BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n    BN_ULONG *resp, *wnump;\n    BN_ULONG d0, d1;\n    int num_n, div_n;\n    int no_branch = 0;\n    if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n        (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n        BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n        return 0;\n    }\n    bn_check_top(num);\n    bn_check_top(divisor);\n    if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n        || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n        no_branch = 1;\n    }\n    bn_check_top(dv);\n    bn_check_top(rm);\n    if (BN_is_zero(divisor)) {\n        BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n        return (0);\n    }\n    if (!no_branch && BN_ucmp(num, divisor) < 0) {\n        if (rm != NULL) {\n            if (BN_copy(rm, num) == NULL)\n                return (0);\n        }\n        if (dv != NULL)\n            BN_zero(dv);\n        return (1);\n    }\n    BN_CTX_start(ctx);\n    tmp = BN_CTX_get(ctx);\n    snum = BN_CTX_get(ctx);\n    sdiv = BN_CTX_get(ctx);\n    if (dv == NULL)\n        res = BN_CTX_get(ctx);\n    else\n        res = dv;\n    if (sdiv == NULL || res == NULL || tmp == NULL || snum == NULL)\n        goto err;\n    norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n    if (!(BN_lshift(sdiv, divisor, norm_shift)))\n        goto err;\n    sdiv->neg = 0;\n    norm_shift += BN_BITS2;\n    if (!(BN_lshift(snum, num, norm_shift)))\n        goto err;\n    snum->neg = 0;\n    if (no_branch) {\n        if (snum->top <= sdiv->top + 1) {\n            if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n                goto err;\n            for (i = snum->top; i < sdiv->top + 2; i++)\n                snum->d[i] = 0;\n            snum->top = sdiv->top + 2;\n        } else {\n            if (bn_wexpand(snum, snum->top + 1) == NULL)\n                goto err;\n            snum->d[snum->top] = 0;\n            snum->top++;\n        }\n    }\n    div_n = sdiv->top;\n    num_n = snum->top;\n    loop = num_n - div_n;\n    wnum.neg = 0;\n    wnum.d = &(snum->d[loop]);\n    wnum.top = div_n;\n    wnum.dmax = snum->dmax - loop;\n    d0 = sdiv->d[div_n - 1];\n    d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n    wnump = &(snum->d[num_n - 1]);\n    res->neg = (num->neg ^ divisor->neg);\n    if (!bn_wexpand(res, (loop + 1)))\n        goto err;\n    res->top = loop - no_branch;\n    resp = &(res->d[loop - 1]);\n    if (!bn_wexpand(tmp, (div_n + 1)))\n        goto err;\n    if (!no_branch) {\n        if (BN_ucmp(&wnum, sdiv) >= 0) {\n            bn_clear_top2max(&wnum);\n            bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n            *resp = 1;\n        } else\n            res->top--;\n    }\n    resp++;\n    if (res->top == 0)\n        res->neg = 0;\n    else\n        resp--;\n    for (i = 0; i < loop - 1; i++, wnump--) {\n        BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n        BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n        q = bn_div_3_words(wnump, d1, d0);\n# else\n        BN_ULONG n0, n1, rem = 0;\n        n0 = wnump[0];\n        n1 = wnump[-1];\n        if (n0 == d0)\n            q = BN_MASK2;\n        else {\n#  ifdef BN_LLONG\n            BN_ULLONG t2;\n#   if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n            q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n#   else\n            q = bn_div_words(n0, n1, d0);\n#   endif\n#   ifndef REMAINDER_IS_ALREADY_CALCULATED\n            rem = (n1 - q * d0) & BN_MASK2;\n#   endif\n            t2 = (BN_ULLONG) d1 *q;\n            for (;;) {\n                if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n                    break;\n                q--;\n                rem += d0;\n                if (rem < d0)\n                    break;\n                t2 -= d1;\n            }\n#  else\n            BN_ULONG t2l, t2h;\n            q = bn_div_words(n0, n1, d0);\n#   ifndef REMAINDER_IS_ALREADY_CALCULATED\n            rem = (n1 - q * d0) & BN_MASK2;\n#   endif\n#   if defined(BN_UMULT_LOHI)\n            BN_UMULT_LOHI(t2l, t2h, d1, q);\n#   elif defined(BN_UMULT_HIGH)\n            t2l = d1 * q;\n            t2h = BN_UMULT_HIGH(d1, q);\n#   else\n            {\n                BN_ULONG ql, qh;\n                t2l = LBITS(d1);\n                t2h = HBITS(d1);\n                ql = LBITS(q);\n                qh = HBITS(q);\n                mul64(t2l, t2h, ql, qh);\n            }\n#   endif\n            for (;;) {\n                if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n                    break;\n                q--;\n                rem += d0;\n                if (rem < d0)\n                    break;\n                if (t2l < d1)\n                    t2h--;\n                t2l -= d1;\n            }\n#  endif\n        }\n# endif\n        l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n        tmp->d[div_n] = l0;\n        wnum.d--;\n        if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n            q--;\n            if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n                (*wnump)++;\n        }\n        resp--;\n        *resp = q;\n    }\n    bn_correct_top(snum);\n    if (rm != NULL) {\n        int neg = num->neg;\n        BN_rshift(rm, snum, norm_shift);\n        if (!BN_is_zero(rm))\n            rm->neg = neg;\n        bn_check_top(rm);\n    }\n    if (no_branch)\n        bn_correct_top(res);\n    BN_CTX_end(ctx);\n    return (1);\n err:\n    bn_check_top(rm);\n    BN_CTX_end(ctx);\n    return (0);\n}', 'int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)\n{\n    int i, nw, lb, rb;\n    BN_ULONG *t, *f;\n    BN_ULONG l;\n    bn_check_top(r);\n    bn_check_top(a);\n    if (n < 0) {\n        BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);\n        return 0;\n    }\n    r->neg = a->neg;\n    nw = n / BN_BITS2;\n    if (bn_wexpand(r, a->top + nw + 1) == NULL)\n        return (0);\n    lb = n % BN_BITS2;\n    rb = BN_BITS2 - lb;\n    f = a->d;\n    t = r->d;\n    t[a->top + nw] = 0;\n    if (lb == 0)\n        for (i = a->top - 1; i >= 0; i--)\n            t[nw + i] = f[i];\n    else\n        for (i = a->top - 1; i >= 0; i--) {\n            l = f[i];\n            t[nw + i + 1] |= (l >> rb) & BN_MASK2;\n            t[nw + i] = (l << lb) & BN_MASK2;\n        }\n    memset(t, 0, sizeof(*t) * nw);\n    r->top = a->top + nw + 1;\n    bn_correct_top(r);\n    bn_check_top(r);\n    return (1);\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n    return (words <= a->dmax) ? a : bn_expand2(a, words);\n}']