crypto/rsa/rsa_ossl.c:689: error: BUFFER_OVERRUN_L3
  Offset added: [8, +oo] Size: [0, 536870848] by call to `bn_mod_add_fixed_top`.
Showing all 14 steps of the trace


crypto/rsa/rsa_ossl.c:688:17: Call
686.                                         ctx)
687.               /* r0 = r1 * q + m1 */
688.               || !bn_mul_fixed_top(r0, r1, rsa->q, ctx)
                       ^
689.               || !bn_mod_add_fixed_top(r0, r0, m1, rsa->n))
690.               goto err;

crypto/bn/bn_mul.c:507:1: Parameter `*r->d`
  505.   }
  506.   
  507. > int bn_mul_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
  508.   {
  509.       int ret = 0;

crypto/rsa/rsa_ossl.c:689:17: Call
687.               /* r0 = r1 * q + m1 */
688.               || !bn_mul_fixed_top(r0, r1, rsa->q, ctx)
689.               || !bn_mod_add_fixed_top(r0, r0, m1, rsa->n))
                       ^
690.               goto err;
691.   

crypto/bn/bn_mod.c:48:1: Parameter `*r->d`
  46.    * move depending on whether or not subtraction borrowed.
  47.    */
  48. > int bn_mod_add_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  49.                            const BIGNUM *m)
  50.   {

crypto/bn/bn_mod.c:56:9: Call
  54.       const BN_ULONG *ap, *bp;
  55.   
  56.       if (bn_wexpand(r, mtop) == NULL)
                ^
  57.           return 0;
  58.   

crypto/bn/bn_lib.c:962:1: Parameter `*a->d`
    960.   }
    961.   
    962. > BIGNUM *bn_wexpand(BIGNUM *a, int words)
    963.   {
    964.       return (words <= a->dmax) ? a : bn_expand2(a, words);

crypto/bn/bn_lib.c:964:37: Call
    962.   BIGNUM *bn_wexpand(BIGNUM *a, int words)
    963.   {
    964.       return (words <= a->dmax) ? a : bn_expand2(a, words);
                                               ^
    965.   }
    966.   

crypto/bn/bn_lib.c:245:1: Parameter `*b->d`
      243.    */
      244.   
      245. > BIGNUM *bn_expand2(BIGNUM *b, int words)
      246.   {
      247.       if (words > b->dmax) {

crypto/bn/bn_lib.c:248:23: Call
      246.   {
      247.       if (words > b->dmax) {
      248.           BN_ULONG *a = bn_expand_internal(b, words);
                                   ^
      249.           if (!a)
      250.               return NULL;

crypto/bn/bn_lib.c:209:1: <Offset trace>
207.   /* This is used by bn_expand2() */
208.   /* The caller MUST check that words > b->dmax before calling this */
209. > static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
210.   {
211.       BN_ULONG *a = NULL;

crypto/bn/bn_lib.c:209:1: Parameter `b->top`
        207.   /* This is used by bn_expand2() */
        208.   /* The caller MUST check that words > b->dmax before calling this */
        209. > static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
        210.   {
        211.       BN_ULONG *a = NULL;

crypto/bn/bn_lib.c:209:1: <Length trace>
207.   /* This is used by bn_expand2() */
208.   /* The caller MUST check that words > b->dmax before calling this */
209. > static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
210.   {
211.       BN_ULONG *a = NULL;

crypto/bn/bn_lib.c:209:1: Parameter `*b->d`
        207.   /* This is used by bn_expand2() */
        208.   /* The caller MUST check that words > b->dmax before calling this */
        209. > static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
        210.   {
        211.       BN_ULONG *a = NULL;

crypto/bn/bn_lib.c:232:9: Array access: Offset added: [8, +oo] Size: [0, 536870848] by call to `bn_mod_add_fixed_top` 
        230.       assert(b->top <= words);
        231.       if (b->top > 0)
        232.           memcpy(a, b->d, sizeof(*a) * b->top);
                       ^
        233.   
        234.       return a;

static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
{
    BN_ULONG *a = NULL;
    if (words > (INT_MAX / (4 * BN_BITS2))) {
        BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);
        return NULL;
    }
    if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
        BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
        return NULL;
    }
    if (BN_get_flags(b, BN_FLG_SECURE))
        a = OPENSSL_secure_zalloc(words * sizeof(*a));
    else
        a = OPENSSL_zalloc(words * sizeof(*a));
    if (a == NULL) {
        BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
        return NULL;
    }
    assert(b->top <= words);
    if (b->top > 0)
        memcpy(a, b->d, sizeof(*a) * b->top);
    return a;
}
['static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)\n{\n    BIGNUM *r1, *m1, *vrfy, *r2, *m[RSA_MAX_PRIME_NUM - 2];\n    int ret = 0, i, ex_primes = 0, smooth = 0;\n    RSA_PRIME_INFO *pinfo;\n    BN_CTX_start(ctx);\n    r1 = BN_CTX_get(ctx);\n    r2 = BN_CTX_get(ctx);\n    m1 = BN_CTX_get(ctx);\n    vrfy = BN_CTX_get(ctx);\n    if (vrfy == NULL)\n        goto err;\n    if (rsa->version == RSA_ASN1_VERSION_MULTI\n        && ((ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos)) <= 0\n             || ex_primes > RSA_MAX_PRIME_NUM - 2))\n        goto err;\n    if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) {\n        BIGNUM *factor = BN_new();\n        if (factor == NULL)\n            goto err;\n        if (!(BN_with_flags(factor, rsa->p, BN_FLG_CONSTTIME),\n              BN_MONT_CTX_set_locked(&rsa->_method_mod_p, rsa->lock,\n                                     factor, ctx))\n            || !(BN_with_flags(factor, rsa->q, BN_FLG_CONSTTIME),\n                 BN_MONT_CTX_set_locked(&rsa->_method_mod_q, rsa->lock,\n                                        factor, ctx))) {\n            BN_free(factor);\n            goto err;\n        }\n        for (i = 0; i < ex_primes; i++) {\n            pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);\n            BN_with_flags(factor, pinfo->r, BN_FLG_CONSTTIME);\n            if (!BN_MONT_CTX_set_locked(&pinfo->m, rsa->lock, factor, ctx)) {\n                BN_free(factor);\n                goto err;\n            }\n        }\n        BN_free(factor);\n        smooth = (ex_primes == 0)\n                 && (rsa->meth->bn_mod_exp == BN_mod_exp_mont)\n                 && (BN_num_bits(rsa->q) == BN_num_bits(rsa->p));\n    }\n    if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)\n        if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,\n                                    rsa->n, ctx))\n            goto err;\n    if (smooth) {\n        if (\n            !bn_from_mont_fixed_top(m1, I, rsa->_method_mod_q, ctx)\n            || !bn_to_mont_fixed_top(m1, m1, rsa->_method_mod_q, ctx)\n            || !BN_mod_exp_mont_consttime(m1, m1, rsa->dmq1, rsa->q, ctx,\n                                          rsa->_method_mod_q)\n            || !bn_from_mont_fixed_top(r1, I, rsa->_method_mod_p, ctx)\n            || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx)\n            || !BN_mod_exp_mont_consttime(r1, r1, rsa->dmp1, rsa->p, ctx,\n                                          rsa->_method_mod_p)\n            || !bn_mod_sub_fixed_top(r1, r1, m1, rsa->p)\n            || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx)\n            || !bn_mul_mont_fixed_top(r1, r1, rsa->iqmp, rsa->_method_mod_p,\n                                      ctx)\n            || !bn_mul_fixed_top(r0, r1, rsa->q, ctx)\n            || !bn_mod_add_fixed_top(r0, r0, m1, rsa->n))\n            goto err;\n        goto tail;\n    }\n    {\n        BIGNUM *c = BN_new();\n        if (c == NULL)\n            goto err;\n        BN_with_flags(c, I, BN_FLG_CONSTTIME);\n        if (!BN_mod(r1, c, rsa->q, ctx)) {\n            BN_free(c);\n            goto err;\n        }\n        {\n            BIGNUM *dmq1 = BN_new();\n            if (dmq1 == NULL) {\n                BN_free(c);\n                goto err;\n            }\n            BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);\n            if (!rsa->meth->bn_mod_exp(m1, r1, dmq1, rsa->q, ctx,\n                                       rsa->_method_mod_q)) {\n                BN_free(c);\n                BN_free(dmq1);\n                goto err;\n            }\n            BN_free(dmq1);\n        }\n        if (!BN_mod(r1, c, rsa->p, ctx)) {\n            BN_free(c);\n            goto err;\n        }\n        BN_free(c);\n    }\n    {\n        BIGNUM *dmp1 = BN_new();\n        if (dmp1 == NULL)\n            goto err;\n        BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);\n        if (!rsa->meth->bn_mod_exp(r0, r1, dmp1, rsa->p, ctx,\n                                   rsa->_method_mod_p)) {\n            BN_free(dmp1);\n            goto err;\n        }\n        BN_free(dmp1);\n    }\n    if (ex_primes > 0) {\n        BIGNUM *di = BN_new(), *cc = BN_new();\n        if (cc == NULL || di == NULL) {\n            BN_free(cc);\n            BN_free(di);\n            goto err;\n        }\n        for (i = 0; i < ex_primes; i++) {\n            if ((m[i] = BN_CTX_get(ctx)) == NULL) {\n                BN_free(cc);\n                BN_free(di);\n                goto err;\n            }\n            pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);\n            BN_with_flags(cc, I, BN_FLG_CONSTTIME);\n            BN_with_flags(di, pinfo->d, BN_FLG_CONSTTIME);\n            if (!BN_mod(r1, cc, pinfo->r, ctx)) {\n                BN_free(cc);\n                BN_free(di);\n                goto err;\n            }\n            if (!rsa->meth->bn_mod_exp(m[i], r1, di, pinfo->r, ctx, pinfo->m)) {\n                BN_free(cc);\n                BN_free(di);\n                goto err;\n            }\n        }\n        BN_free(cc);\n        BN_free(di);\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 *pr1 = BN_new();\n        if (pr1 == NULL)\n            goto err;\n        BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);\n        if (!BN_mod(r0, pr1, rsa->p, ctx)) {\n            BN_free(pr1);\n            goto err;\n        }\n        BN_free(pr1);\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 (ex_primes > 0) {\n        BIGNUM *pr2 = BN_new();\n        if (pr2 == NULL)\n            goto err;\n        for (i = 0; i < ex_primes; i++) {\n            pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);\n            if (!BN_sub(r1, m[i], r0)) {\n                BN_free(pr2);\n                goto err;\n            }\n            if (!BN_mul(r2, r1, pinfo->t, ctx)) {\n                BN_free(pr2);\n                goto err;\n            }\n            BN_with_flags(pr2, r2, BN_FLG_CONSTTIME);\n            if (!BN_mod(r1, pr2, pinfo->r, ctx)) {\n                BN_free(pr2);\n                goto err;\n            }\n            if (BN_is_negative(r1))\n                if (!BN_add(r1, r1, pinfo->r)) {\n                    BN_free(pr2);\n                    goto err;\n                }\n            if (!BN_mul(r1, r1, pinfo->pp, ctx)) {\n                BN_free(pr2);\n                goto err;\n            }\n            if (!BN_add(r0, r0, r1)) {\n                BN_free(pr2);\n                goto err;\n            }\n        }\n        BN_free(pr2);\n    }\n tail:\n    if (rsa->e && rsa->n) {\n        if (rsa->meth->bn_mod_exp == BN_mod_exp_mont) {\n            if (!BN_mod_exp_mont(vrfy, r0, rsa->e, rsa->n, ctx,\n                                 rsa->_method_mod_n))\n                goto err;\n        } else {\n            bn_correct_top(r0);\n            if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx,\n                                       rsa->_method_mod_n))\n                goto err;\n        }\n        if (!BN_sub(vrfy, vrfy, I))\n            goto err;\n        if (BN_is_zero(vrfy)) {\n            bn_correct_top(r0);\n            ret = 1;\n            goto err;\n        }\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 *d = BN_new();\n            if (d == NULL)\n                goto err;\n            BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);\n            if (!rsa->meth->bn_mod_exp(r0, I, d, rsa->n, ctx,\n                                       rsa->_method_mod_n)) {\n                BN_free(d);\n                goto err;\n            }\n            BN_free(d);\n        }\n    }\n    bn_correct_top(r0);\n    ret = 1;\n err:\n    BN_CTX_end(ctx);\n    return ret;\n}', 'int bn_mul_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n    int ret = 0;\n    int top, al, bl;\n    BIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n    int i;\n#endif\n#ifdef BN_RECURSION\n    BIGNUM *t = NULL;\n    int j = 0, k;\n#endif\n    bn_check_top(a);\n    bn_check_top(b);\n    bn_check_top(r);\n    al = a->top;\n    bl = b->top;\n    if ((al == 0) || (bl == 0)) {\n        BN_zero(r);\n        return 1;\n    }\n    top = al + bl;\n    BN_CTX_start(ctx);\n    if ((r == a) || (r == b)) {\n        if ((rr = BN_CTX_get(ctx)) == NULL)\n            goto err;\n    } else\n        rr = r;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n    i = al - bl;\n#endif\n#ifdef BN_MUL_COMBA\n    if (i == 0) {\n# if 0\n        if (al == 4) {\n            if (bn_wexpand(rr, 8) == NULL)\n                goto err;\n            rr->top = 8;\n            bn_mul_comba4(rr->d, a->d, b->d);\n            goto end;\n        }\n# endif\n        if (al == 8) {\n            if (bn_wexpand(rr, 16) == NULL)\n                goto err;\n            rr->top = 16;\n            bn_mul_comba8(rr->d, a->d, b->d);\n            goto end;\n        }\n    }\n#endif\n#ifdef BN_RECURSION\n    if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {\n        if (i >= -1 && i <= 1) {\n            if (i >= 0) {\n                j = BN_num_bits_word((BN_ULONG)al);\n            }\n            if (i == -1) {\n                j = BN_num_bits_word((BN_ULONG)bl);\n            }\n            j = 1 << (j - 1);\n            assert(j <= al || j <= bl);\n            k = j + j;\n            t = BN_CTX_get(ctx);\n            if (t == NULL)\n                goto err;\n            if (al > j || bl > j) {\n                if (bn_wexpand(t, k * 4) == NULL)\n                    goto err;\n                if (bn_wexpand(rr, k * 4) == NULL)\n                    goto err;\n                bn_mul_part_recursive(rr->d, a->d, b->d,\n                                      j, al - j, bl - j, t->d);\n            } else {\n                if (bn_wexpand(t, k * 2) == NULL)\n                    goto err;\n                if (bn_wexpand(rr, k * 2) == NULL)\n                    goto err;\n                bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);\n            }\n            rr->top = top;\n            goto end;\n        }\n    }\n#endif\n    if (bn_wexpand(rr, top) == NULL)\n        goto err;\n    rr->top = top;\n    bn_mul_normal(rr->d, a->d, al, b->d, bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n end:\n#endif\n    rr->neg = a->neg ^ b->neg;\n    rr->flags |= BN_FLG_FIXED_TOP;\n    if (r != rr && BN_copy(r, rr) == NULL)\n        goto err;\n    ret = 1;\n err:\n    bn_check_top(r);\n    BN_CTX_end(ctx);\n    return ret;\n}', 'int bn_mod_add_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n                         const BIGNUM *m)\n{\n    size_t i, ai, bi, mtop = m->top;\n    BN_ULONG storage[1024 / BN_BITS2];\n    BN_ULONG carry, temp, mask, *rp, *tp = storage;\n    const BN_ULONG *ap, *bp;\n    if (bn_wexpand(r, mtop) == NULL)\n        return 0;\n    if (mtop > sizeof(storage) / sizeof(storage[0])\n        && (tp = OPENSSL_malloc(mtop * sizeof(BN_ULONG))) == NULL)\n        return 0;\n    ap = a->d != NULL ? a->d : tp;\n    bp = b->d != NULL ? b->d : tp;\n    for (i = 0, ai = 0, bi = 0, carry = 0; i < mtop;) {\n        mask = (BN_ULONG)0 - ((i - a->top) >> (8 * sizeof(i) - 1));\n        temp = ((ap[ai] & mask) + carry) & BN_MASK2;\n        carry = (temp < carry);\n        mask = (BN_ULONG)0 - ((i - b->top) >> (8 * sizeof(i) - 1));\n        tp[i] = ((bp[bi] & mask) + temp) & BN_MASK2;\n        carry += (tp[i] < temp);\n        i++;\n        ai += (i - a->dmax) >> (8 * sizeof(i) - 1);\n        bi += (i - b->dmax) >> (8 * sizeof(i) - 1);\n    }\n    rp = r->d;\n    carry -= bn_sub_words(rp, tp, m->d, mtop);\n    for (i = 0; i < mtop; i++) {\n        rp[i] = (carry & tp[i]) | (~carry & rp[i]);\n        ((volatile BN_ULONG *)tp)[i] = 0;\n    }\n    r->top = mtop;\n    r->flags |= BN_FLG_FIXED_TOP;\n    r->neg = 0;\n    if (tp != storage)\n        OPENSSL_free(tp);\n    return 1;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n    return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n    if (words > b->dmax) {\n        BN_ULONG *a = bn_expand_internal(b, words);\n        if (!a)\n            return NULL;\n        if (b->d) {\n            OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n            bn_free_d(b);\n        }\n        b->d = a;\n        b->dmax = words;\n    }\n    return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n    BN_ULONG *a = NULL;\n    if (words > (INT_MAX / (4 * BN_BITS2))) {\n        BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n        return NULL;\n    }\n    if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n        BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n        return NULL;\n    }\n    if (BN_get_flags(b, BN_FLG_SECURE))\n        a = OPENSSL_secure_zalloc(words * sizeof(*a));\n    else\n        a = OPENSSL_zalloc(words * sizeof(*a));\n    if (a == NULL) {\n        BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n        return NULL;\n    }\n    assert(b->top <= words);\n    if (b->top > 0)\n        memcpy(a, b->d, sizeof(*a) * b->top);\n    return a;\n}']