crypto/dsa/dsa_gen.c:452: error: BUFFER_OVERRUN_L3
  Offset: [3, +oo] (⇐ [0, +oo] + 3) Size: [0, 8388607] by call to `BN_bin2bn`.
Showing all 14 steps of the trace


crypto/dsa/dsa_gen.c:405:10: Call
403.   
404.       BN_CTX_start(ctx);
405.       r0 = BN_CTX_get(ctx);
                ^
406.       g = BN_CTX_get(ctx);
407.       W = BN_CTX_get(ctx);

crypto/bn/bn_ctx.c:283:5: Call
  281.       }
  282.       /* OK, make sure the returned bignum is "zero" */
  283.       BN_zero(ret);
             ^
  284.       ctx->used++;
  285.       CTXDBG_RET(ctx, ret);

crypto/bn/bn_lib.c:529:1: Parameter `*a->d`
    527.   }
    528.   
    529. > int BN_set_word(BIGNUM *a, BN_ULONG w)
    530.   {
    531.       bn_check_top(a);

crypto/dsa/dsa_gen.c:452:18: Call
450.               pmd[0] |= 0x80;
451.               pmd[qsize - 1] |= 0x01;
452.               if (!BN_bin2bn(pmd, qsize, q))
                        ^
453.                   goto err;
454.   

crypto/bn/bn_lib.c:541:1: Parameter `*ret->d`
  539.   }
  540.   
  541. > BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
  542.   {
  543.       unsigned int i, m;

crypto/bn/bn_lib.c:563:9: Call
  561.       i = ((n - 1) / BN_BYTES) + 1;
  562.       m = ((n - 1) % (BN_BYTES));
  563.       if (bn_wexpand(ret, (int)i) == NULL) {
                 ^
  564.           BN_free(bn);
  565.           return NULL;

crypto/bn/bn_lib.c:994:1: Parameter `*a->d`
    992.   }
    993.   
    994. > BIGNUM *bn_wexpand(BIGNUM *a, int words)
    995.   {
    996.       return (words <= a->dmax) ? a : bn_expand2(a, words);

crypto/bn/bn_lib.c:996:37: Call
    994.   BIGNUM *bn_wexpand(BIGNUM *a, int words)
    995.   {
    996.       return (words <= a->dmax) ? a : bn_expand2(a, words);
                                               ^
    997.   }
    998.   

crypto/bn/bn_lib.c:389:1: Parameter `*b->d`
      387.    */
      388.   
      389. > BIGNUM *bn_expand2(BIGNUM *b, int words)
      390.   {
      391.       bn_check_top(b);

crypto/bn/bn_lib.c:394:23: Call
      392.   
      393.       if (words > b->dmax) {
      394.           BN_ULONG *a = bn_expand_internal(b, words);
                                   ^
      395.           if (!a)
      396.               return NULL;

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

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

crypto/bn/bn_lib.c:334:5: Assignment
        332.   
        333.   #if 1
        334.       B = b->d;
                   ^
        335.       /* Check if the previous number needs to be copied */
        336.       if (B != NULL) {

crypto/bn/bn_lib.c:351:18: Array access: Offset: [3, +oo] (⇐ [0, +oo] + 3) Size: [0, 8388607] by call to `BN_bin2bn` 
        349.               a1 = B[1];
        350.               a2 = B[2];
        351.               a3 = B[3];
                                ^
        352.               A[0] = a0;
        353.               A[1] = a1;

static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
{
    BN_ULONG *A, *a = NULL;
    const BN_ULONG *B;
    int i;
    bn_check_top(b);
    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 = A = OPENSSL_secure_malloc(words * sizeof(*a));
    else
        a = A = OPENSSL_malloc(words * sizeof(*a));
    if (A == NULL) {
        BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
        return (NULL);
    }
#ifdef PURIFY
    memset(a, 0, sizeof(*a) * words);
#endif
#if 1
    B = b->d;
    if (B != NULL) {
        for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {
            BN_ULONG a0, a1, a2, a3;
            a0 = B[0];
            a1 = B[1];
            a2 = B[2];
            a3 = B[3];
            A[0] = a0;
            A[1] = a1;
            A[2] = a2;
            A[3] = a3;
        }
        switch (b->top & 3) {
        case 3:
            A[2] = B[2];
        case 2:
            A[1] = B[1];
        case 1:
            A[0] = B[0];
        case 0:
            ;
        }
    }
#else
    memset(A, 0, sizeof(*A) * words);
    memcpy(A, b->d, sizeof(b->d[0]) * b->top);
#endif
    return (a);
}
['int dsa_builtin_paramgen2(DSA *ret, size_t L, size_t N,\n                          const EVP_MD *evpmd, const unsigned char *seed_in,\n                          size_t seed_len, int idx, unsigned char *seed_out,\n                          int *counter_ret, unsigned long *h_ret,\n                          BN_GENCB *cb)\n{\n    int ok = -1;\n    unsigned char *seed = NULL, *seed_tmp = NULL;\n    unsigned char md[EVP_MAX_MD_SIZE];\n    int mdsize;\n    BIGNUM *r0, *W, *X, *c, *test;\n    BIGNUM *g = NULL, *q = NULL, *p = NULL;\n    BN_MONT_CTX *mont = NULL;\n    int i, k, n = 0, m = 0, qsize = N >> 3;\n    int counter = 0;\n    int r = 0;\n    BN_CTX *ctx = NULL;\n    EVP_MD_CTX *mctx = EVP_MD_CTX_new();\n    unsigned int h = 2;\n    if (mctx == NULL)\n        goto err;\n    if (evpmd == NULL) {\n        if (N == 160)\n            evpmd = EVP_sha1();\n        else if (N == 224)\n            evpmd = EVP_sha224();\n        else\n            evpmd = EVP_sha256();\n    }\n    mdsize = EVP_MD_size(evpmd);\n    if (!ret->p || !ret->q || idx >= 0) {\n        if (seed_len == 0)\n            seed_len = mdsize;\n        seed = OPENSSL_malloc(seed_len);\n        if (seed_out)\n            seed_tmp = seed_out;\n        else\n            seed_tmp = OPENSSL_malloc(seed_len);\n        if (seed == NULL || seed_tmp == NULL)\n            goto err;\n        if (seed_in)\n            memcpy(seed, seed_in, seed_len);\n    }\n    if ((ctx = BN_CTX_new()) == NULL)\n        goto err;\n    if ((mont = BN_MONT_CTX_new()) == NULL)\n        goto err;\n    BN_CTX_start(ctx);\n    r0 = BN_CTX_get(ctx);\n    g = BN_CTX_get(ctx);\n    W = BN_CTX_get(ctx);\n    X = BN_CTX_get(ctx);\n    c = BN_CTX_get(ctx);\n    test = BN_CTX_get(ctx);\n    if (ret->p && ret->q) {\n        p = ret->p;\n        q = ret->q;\n        if (idx >= 0)\n            memcpy(seed_tmp, seed, seed_len);\n        goto g_only;\n    } else {\n        p = BN_CTX_get(ctx);\n        q = BN_CTX_get(ctx);\n    }\n    if (!BN_lshift(test, BN_value_one(), L - 1))\n        goto err;\n    for (;;) {\n        for (;;) {\n            unsigned char *pmd;\n            if (!BN_GENCB_call(cb, 0, m++))\n                goto err;\n            if (!seed_in) {\n                if (RAND_bytes(seed, seed_len) <= 0)\n                    goto err;\n            }\n            if (!EVP_Digest(seed, seed_len, md, NULL, evpmd, NULL))\n                goto err;\n            if (mdsize > qsize)\n                pmd = md + mdsize - qsize;\n            else\n                pmd = md;\n            if (mdsize < qsize)\n                memset(md + mdsize, 0, qsize - mdsize);\n            pmd[0] |= 0x80;\n            pmd[qsize - 1] |= 0x01;\n            if (!BN_bin2bn(pmd, qsize, q))\n                goto err;\n            r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx,\n                                        seed_in ? 1 : 0, cb);\n            if (r > 0)\n                break;\n            if (r != 0)\n                goto err;\n            if (seed_in) {\n                ok = 0;\n                DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN2, DSA_R_Q_NOT_PRIME);\n                goto err;\n            }\n        }\n        if (seed_out)\n            memcpy(seed_out, seed, seed_len);\n        if (!BN_GENCB_call(cb, 2, 0))\n            goto err;\n        if (!BN_GENCB_call(cb, 3, 0))\n            goto err;\n        counter = 0;\n        n = (L - 1) / (mdsize << 3);\n        for (;;) {\n            if ((counter != 0) && !BN_GENCB_call(cb, 0, counter))\n                goto err;\n            BN_zero(W);\n            for (k = 0; k <= n; k++) {\n                for (i = seed_len - 1; i >= 0; i--) {\n                    seed[i]++;\n                    if (seed[i] != 0)\n                        break;\n                }\n                if (!EVP_Digest(seed, seed_len, md, NULL, evpmd, NULL))\n                    goto err;\n                if (!BN_bin2bn(md, mdsize, r0))\n                    goto err;\n                if (!BN_lshift(r0, r0, (mdsize << 3) * k))\n                    goto err;\n                if (!BN_add(W, W, r0))\n                    goto err;\n            }\n            if (!BN_mask_bits(W, L - 1))\n                goto err;\n            if (!BN_copy(X, W))\n                goto err;\n            if (!BN_add(X, X, test))\n                goto err;\n            if (!BN_lshift1(r0, q))\n                goto err;\n            if (!BN_mod(c, X, r0, ctx))\n                goto err;\n            if (!BN_sub(r0, c, BN_value_one()))\n                goto err;\n            if (!BN_sub(p, X, r0))\n                goto err;\n            if (BN_cmp(p, test) >= 0) {\n                r = BN_is_prime_fasttest_ex(p, DSS_prime_checks, ctx, 1, cb);\n                if (r > 0)\n                    goto end;\n                if (r != 0)\n                    goto err;\n            }\n            counter++;\n            if (counter >= (int)(4 * L))\n                break;\n        }\n        if (seed_in) {\n            ok = 0;\n            DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN2, DSA_R_INVALID_PARAMETERS);\n            goto err;\n        }\n    }\n end:\n    if (!BN_GENCB_call(cb, 2, 1))\n        goto err;\n g_only:\n    if (!BN_sub(test, p, BN_value_one()))\n        goto err;\n    if (!BN_div(r0, NULL, test, q, ctx))\n        goto err;\n    if (idx < 0) {\n        if (!BN_set_word(test, h))\n            goto err;\n    } else\n        h = 1;\n    if (!BN_MONT_CTX_set(mont, p, ctx))\n        goto err;\n    for (;;) {\n        static const unsigned char ggen[4] = { 0x67, 0x67, 0x65, 0x6e };\n        if (idx >= 0) {\n            md[0] = idx & 0xff;\n            md[1] = (h >> 8) & 0xff;\n            md[2] = h & 0xff;\n            if (!EVP_DigestInit_ex(mctx, evpmd, NULL))\n                goto err;\n            if (!EVP_DigestUpdate(mctx, seed_tmp, seed_len))\n                goto err;\n            if (!EVP_DigestUpdate(mctx, ggen, sizeof(ggen)))\n                goto err;\n            if (!EVP_DigestUpdate(mctx, md, 3))\n                goto err;\n            if (!EVP_DigestFinal_ex(mctx, md, NULL))\n                goto err;\n            if (!BN_bin2bn(md, mdsize, test))\n                goto err;\n        }\n        if (!BN_mod_exp_mont(g, test, r0, p, ctx, mont))\n            goto err;\n        if (!BN_is_one(g))\n            break;\n        if (idx < 0 && !BN_add(test, test, BN_value_one()))\n            goto err;\n        h++;\n        if (idx >= 0 && h > 0xffff)\n            goto err;\n    }\n    if (!BN_GENCB_call(cb, 3, 1))\n        goto err;\n    ok = 1;\n err:\n    if (ok == 1) {\n        if (p != ret->p) {\n            BN_free(ret->p);\n            ret->p = BN_dup(p);\n        }\n        if (q != ret->q) {\n            BN_free(ret->q);\n            ret->q = BN_dup(q);\n        }\n        BN_free(ret->g);\n        ret->g = BN_dup(g);\n        if (ret->p == NULL || ret->q == NULL || ret->g == NULL) {\n            ok = -1;\n            goto err;\n        }\n        if (counter_ret != NULL)\n            *counter_ret = counter;\n        if (h_ret != NULL)\n            *h_ret = h;\n    }\n    OPENSSL_free(seed);\n    if (seed_out != seed_tmp)\n        OPENSSL_free(seed_tmp);\n    if (ctx)\n        BN_CTX_end(ctx);\n    BN_CTX_free(ctx);\n    BN_MONT_CTX_free(mont);\n    EVP_MD_CTX_free(mctx);\n    return ok;\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}', 'BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)\n{\n    unsigned int i, m;\n    unsigned int n;\n    BN_ULONG l;\n    BIGNUM *bn = NULL;\n    if (ret == NULL)\n        ret = bn = BN_new();\n    if (ret == NULL)\n        return (NULL);\n    bn_check_top(ret);\n    for ( ; len > 0 && *s == 0; s++, len--)\n        continue;\n    n = len;\n    if (n == 0) {\n        ret->top = 0;\n        return (ret);\n    }\n    i = ((n - 1) / BN_BYTES) + 1;\n    m = ((n - 1) % (BN_BYTES));\n    if (bn_wexpand(ret, (int)i) == NULL) {\n        BN_free(bn);\n        return NULL;\n    }\n    ret->top = i;\n    ret->neg = 0;\n    l = 0;\n    while (n--) {\n        l = (l << 8L) | *(s++);\n        if (m-- == 0) {\n            ret->d[--i] = l;\n            l = 0;\n            m = BN_BYTES - 1;\n        }\n    }\n    bn_correct_top(ret);\n    return (ret);\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    bn_check_top(b);\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    bn_check_top(b);\n    return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n    BN_ULONG *A, *a = NULL;\n    const BN_ULONG *B;\n    int i;\n    bn_check_top(b);\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 = A = OPENSSL_secure_malloc(words * sizeof(*a));\n    else\n        a = A = OPENSSL_malloc(words * sizeof(*a));\n    if (A == NULL) {\n        BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n        return (NULL);\n    }\n#ifdef PURIFY\n    memset(a, 0, sizeof(*a) * words);\n#endif\n#if 1\n    B = b->d;\n    if (B != NULL) {\n        for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {\n            BN_ULONG a0, a1, a2, a3;\n            a0 = B[0];\n            a1 = B[1];\n            a2 = B[2];\n            a3 = B[3];\n            A[0] = a0;\n            A[1] = a1;\n            A[2] = a2;\n            A[3] = a3;\n        }\n        switch (b->top & 3) {\n        case 3:\n            A[2] = B[2];\n        case 2:\n            A[1] = B[1];\n        case 1:\n            A[0] = B[0];\n        case 0:\n            ;\n        }\n    }\n#else\n    memset(A, 0, sizeof(*A) * words);\n    memcpy(A, b->d, sizeof(b->d[0]) * b->top);\n#endif\n    return (a);\n}']