crypto/rsa/rsa_gen.c:189: error: BUFFER_OVERRUN_L3
  Offset added: [8, +oo] Size: [0, 536870848] by call to `BN_gcd`.
Showing all 12 steps of the trace


crypto/rsa/rsa_gen.c:143:9: Call
141.       }
142.   
143.       if (BN_copy(rsa->e, e_value) == NULL)
               ^
144.           goto err;
145.   

crypto/bn/bn_lib.c:312:1: Parameter `a->top`
  310.   }
  311.   
  312. > BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
  313.   {
  314.       bn_check_top(b);

crypto/rsa/rsa_gen.c:189:18: Call
187.               if (!BN_sub(r2, prime, BN_value_one()))
188.                   goto err;
189.               if (!BN_gcd(r1, r2, rsa->e, ctx))
                        ^
190.                   goto err;
191.               if (BN_is_one(r1))

crypto/bn/bn_gcd.c:15:1: Parameter `in_b->top`
  13.   static BIGNUM *euclid(BIGNUM *a, BIGNUM *b);
  14.   
  15. > int BN_gcd(BIGNUM *r, const BIGNUM *in_a, const BIGNUM *in_b, BN_CTX *ctx)
  16.   {
  17.       BIGNUM *a, *b, *t;

crypto/bn/bn_gcd.c:31:9: Call
  29.       if (BN_copy(a, in_a) == NULL)
  30.           goto err;
  31.       if (BN_copy(b, in_b) == NULL)
                ^
  32.           goto err;
  33.       a->neg = 0;

crypto/bn/bn_lib.c:312:1: <Offset trace>
310.   }
311.   
312. > BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
313.   {
314.       bn_check_top(b);

crypto/bn/bn_lib.c:312:1: Parameter `b->top`
    310.   }
    311.   
    312. > BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
    313.   {
    314.       bn_check_top(b);

crypto/bn/bn_lib.c:312:1: <Length trace>
310.   }
311.   
312. > BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
313.   {
314.       bn_check_top(b);

crypto/bn/bn_lib.c:312:1: Parameter `*a->d`
    310.   }
    311.   
    312. > BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
    313.   {
    314.       bn_check_top(b);

crypto/bn/bn_lib.c:318:9: Call
    316.       if (a == b)
    317.           return a;
    318.       if (bn_wexpand(a, b->top) == NULL)
                   ^
    319.           return NULL;
    320.   

crypto/bn/bn_lib.c:937:1: Parameter `*a->d`
      935.   }
      936.   
      937. > BIGNUM *bn_wexpand(BIGNUM *a, int words)
      938.   {
      939.       return (words <= a->dmax) ? a : bn_expand2(a, words);

crypto/bn/bn_lib.c:322:9: Array access: Offset added: [8, +oo] Size: [0, 536870848] by call to `BN_gcd` 
    320.   
    321.       if (b->top > 0)
    322.           memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);
                   ^
    323.   
    324.       a->top = b->top;

BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
{
    bn_check_top(b);
    if (a == b)
        return a;
    if (bn_wexpand(a, b->top) == NULL)
        return NULL;
    if (b->top > 0)
        memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);
    a->top = b->top;
    a->neg = b->neg;
    bn_check_top(a);
    return a;
}
['static int rsa_builtin_keygen(RSA *rsa, int bits, int primes, BIGNUM *e_value,\n                              BN_GENCB *cb)\n{\n    BIGNUM *r0 = NULL, *r1 = NULL, *r2 = NULL, *tmp, *prime;\n    int ok = -1, n = 0, bitsr[RSA_MAX_PRIME_NUM], bitse = 0;\n    int i = 0, quo = 0, rmd = 0, adj = 0, retries = 0;\n    RSA_PRIME_INFO *pinfo = NULL;\n    STACK_OF(RSA_PRIME_INFO) *prime_infos = NULL;\n    BN_CTX *ctx = NULL;\n    BN_ULONG bitst = 0;\n    if (bits < RSA_MIN_MODULUS_BITS) {\n        ok = 0;\n        RSAerr(RSA_F_RSA_BUILTIN_KEYGEN, RSA_R_KEY_SIZE_TOO_SMALL);\n        goto err;\n    }\n    if (primes < RSA_DEFAULT_PRIME_NUM || primes > rsa_multip_cap(bits)) {\n        ok = 0;\n        RSAerr(RSA_F_RSA_BUILTIN_KEYGEN, RSA_R_KEY_PRIME_NUM_INVALID);\n        goto err;\n    }\n    ctx = BN_CTX_new();\n    if (ctx == NULL)\n        goto err;\n    BN_CTX_start(ctx);\n    r0 = BN_CTX_get(ctx);\n    r1 = BN_CTX_get(ctx);\n    r2 = BN_CTX_get(ctx);\n    if (r2 == NULL)\n        goto err;\n    quo = bits / primes;\n    rmd = bits % primes;\n    for (i = 0; i < primes; i++)\n        bitsr[i] = (i < rmd) ? quo + 1 : quo;\n    if (!rsa->n && ((rsa->n = BN_new()) == NULL))\n        goto err;\n    if (!rsa->d && ((rsa->d = BN_secure_new()) == NULL))\n        goto err;\n    if (!rsa->e && ((rsa->e = BN_new()) == NULL))\n        goto err;\n    if (!rsa->p && ((rsa->p = BN_secure_new()) == NULL))\n        goto err;\n    if (!rsa->q && ((rsa->q = BN_secure_new()) == NULL))\n        goto err;\n    if (!rsa->dmp1 && ((rsa->dmp1 = BN_secure_new()) == NULL))\n        goto err;\n    if (!rsa->dmq1 && ((rsa->dmq1 = BN_secure_new()) == NULL))\n        goto err;\n    if (!rsa->iqmp && ((rsa->iqmp = BN_secure_new()) == NULL))\n        goto err;\n    if (primes > RSA_DEFAULT_PRIME_NUM) {\n        rsa->version = RSA_ASN1_VERSION_MULTI;\n        prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, primes - 2);\n        if (prime_infos == NULL)\n            goto err;\n        if (rsa->prime_infos != NULL) {\n            sk_RSA_PRIME_INFO_pop_free(rsa->prime_infos, rsa_multip_info_free);\n        }\n        rsa->prime_infos = prime_infos;\n        for (i = 2; i < primes; i++) {\n            pinfo = rsa_multip_info_new();\n            if (pinfo == NULL)\n                goto err;\n            (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);\n        }\n    }\n    if (BN_copy(rsa->e, e_value) == NULL)\n        goto err;\n    for (i = 0; i < primes; i++) {\n        adj = 0;\n        retries = 0;\n        if (i == 0) {\n            prime = rsa->p;\n        } else if (i == 1) {\n            prime = rsa->q;\n        } else {\n            pinfo = sk_RSA_PRIME_INFO_value(prime_infos, i - 2);\n            prime = pinfo->r;\n        }\n        for (;;) {\n redo:\n            if (!BN_generate_prime_ex(prime, bitsr[i] + adj, 0, NULL, NULL, cb))\n                goto err;\n            {\n                int j;\n                for (j = 0; j < i; j++) {\n                    BIGNUM *prev_prime;\n                    if (j == 0)\n                        prev_prime = rsa->p;\n                    else if (j == 1)\n                        prev_prime = rsa->q;\n                    else\n                        prev_prime = sk_RSA_PRIME_INFO_value(prime_infos,\n                                                             j - 2)->r;\n                    if (!BN_cmp(prime, prev_prime)) {\n                        goto redo;\n                    }\n                }\n            }\n            if (!BN_sub(r2, prime, BN_value_one()))\n                goto err;\n            if (!BN_gcd(r1, r2, rsa->e, ctx))\n                goto err;\n            if (BN_is_one(r1))\n                break;\n            if (!BN_GENCB_call(cb, 2, n++))\n                goto err;\n        }\n        bitse += bitsr[i];\n        if (i == 1) {\n            if (!BN_mul(r1, rsa->p, rsa->q, ctx))\n                goto err;\n        } else if (i != 0) {\n            if (!BN_mul(r1, rsa->n, prime, ctx))\n                goto err;\n        } else {\n            if (!BN_GENCB_call(cb, 3, i))\n                goto err;\n            continue;\n        }\n        if (!BN_rshift(r2, r1, bitse - 4))\n            goto err;\n        bitst = BN_get_word(r2);\n        if (bitst < 0x9 || bitst > 0xF) {\n            bitse -= bitsr[i];\n            if (!BN_GENCB_call(cb, 2, n++))\n                goto err;\n            if (primes > 4) {\n                if (bitst < 0x9)\n                    adj++;\n                else\n                    adj--;\n            } else if (retries == 4) {\n                i = -1;\n                bitse = 0;\n                continue;\n            }\n            retries++;\n            goto redo;\n        }\n        if (i > 1 && BN_copy(pinfo->pp, rsa->n) == NULL)\n            goto err;\n        if (BN_copy(rsa->n, r1) == NULL)\n            goto err;\n        if (!BN_GENCB_call(cb, 3, i))\n            goto err;\n    }\n    if (BN_cmp(rsa->p, rsa->q) < 0) {\n        tmp = rsa->p;\n        rsa->p = rsa->q;\n        rsa->q = tmp;\n    }\n    if (!BN_sub(r1, rsa->p, BN_value_one()))\n        goto err;\n    if (!BN_sub(r2, rsa->q, BN_value_one()))\n        goto err;\n    if (!BN_mul(r0, r1, r2, ctx))\n        goto err;\n    for (i = 2; i < primes; i++) {\n        pinfo = sk_RSA_PRIME_INFO_value(prime_infos, i - 2);\n        if (!BN_sub(pinfo->d, pinfo->r, BN_value_one()))\n            goto err;\n        if (!BN_mul(r0, r0, pinfo->d, ctx))\n            goto err;\n    }\n    {\n        BIGNUM *pr0 = BN_new();\n        if (pr0 == NULL)\n            goto err;\n        BN_with_flags(pr0, r0, BN_FLG_CONSTTIME);\n        if (!BN_mod_inverse(rsa->d, rsa->e, pr0, ctx)) {\n            BN_free(pr0);\n            goto err;\n        }\n        BN_free(pr0);\n    }\n    {\n        BIGNUM *d = BN_new();\n        if (d == NULL)\n            goto err;\n        BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);\n        if (!BN_mod(rsa->dmp1, d, r1, ctx)\n            || !BN_mod(rsa->dmq1, d, r2, ctx)) {\n            BN_free(d);\n            goto err;\n        }\n        for (i = 2; i < primes; i++) {\n            pinfo = sk_RSA_PRIME_INFO_value(prime_infos, i - 2);\n            if (!BN_mod(pinfo->d, d, pinfo->d, ctx)) {\n                BN_free(d);\n                goto err;\n            }\n        }\n        BN_free(d);\n    }\n    {\n        BIGNUM *p = BN_new();\n        if (p == NULL)\n            goto err;\n        BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);\n        if (!BN_mod_inverse(rsa->iqmp, rsa->q, p, ctx)) {\n            BN_free(p);\n            goto err;\n        }\n        for (i = 2; i < primes; i++) {\n            pinfo = sk_RSA_PRIME_INFO_value(prime_infos, i - 2);\n            BN_with_flags(p, pinfo->r, BN_FLG_CONSTTIME);\n            if (!BN_mod_inverse(pinfo->t, pinfo->pp, p, ctx)) {\n                BN_free(p);\n                goto err;\n            }\n        }\n        BN_free(p);\n    }\n    ok = 1;\n err:\n    if (ok == -1) {\n        RSAerr(RSA_F_RSA_BUILTIN_KEYGEN, ERR_LIB_BN);\n        ok = 0;\n    }\n    if (ctx != NULL)\n        BN_CTX_end(ctx);\n    BN_CTX_free(ctx);\n    return ok;\n}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\n    bn_check_top(b);\n    if (a == b)\n        return a;\n    if (bn_wexpand(a, b->top) == NULL)\n        return NULL;\n    if (b->top > 0)\n        memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n    a->top = b->top;\n    a->neg = b->neg;\n    bn_check_top(a);\n    return a;\n}', 'int BN_gcd(BIGNUM *r, const BIGNUM *in_a, const BIGNUM *in_b, BN_CTX *ctx)\n{\n    BIGNUM *a, *b, *t;\n    int ret = 0;\n    bn_check_top(in_a);\n    bn_check_top(in_b);\n    BN_CTX_start(ctx);\n    a = BN_CTX_get(ctx);\n    b = BN_CTX_get(ctx);\n    if (b == NULL)\n        goto err;\n    if (BN_copy(a, in_a) == NULL)\n        goto err;\n    if (BN_copy(b, in_b) == NULL)\n        goto err;\n    a->neg = 0;\n    b->neg = 0;\n    if (BN_cmp(a, b) < 0) {\n        t = a;\n        a = b;\n        b = t;\n    }\n    t = euclid(a, b);\n    if (t == NULL)\n        goto err;\n    if (BN_copy(r, t) == NULL)\n        goto err;\n    ret = 1;\n err:\n    BN_CTX_end(ctx);\n    bn_check_top(r);\n    return ret;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n    return (words <= a->dmax) ? a : bn_expand2(a, words);\n}']