crypto/bn/bn_mont.c:370: error: BUFFER_OVERRUN_L3
  Offset added: [8, +oo] Size: [0, 67108856] by call to `BN_set_bit`.
Showing all 32 steps of the trace


crypto/bn/bn_mont.c:247:1: Parameter `mont->RR.top`
245.   }
246.   
247. > int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)
248.   {
249.       int ret = 0;

crypto/bn/bn_mont.c:318:9: Call
316.           mont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0;
317.   # else
318.           BN_zero(R);
               ^
319.           if (!(BN_set_bit(R, BN_BITS2)))
320.               goto err;           /* R */

crypto/bn/bn_lib.c:402:15: Assignment
  400.       a->neg = 0;
  401.       a->d[0] = w;
  402.       a->top = (w ? 1 : 0);
                       ^
  403.       bn_check_top(a);
  404.       return (1);

crypto/bn/bn_lib.c:402:5: Assignment
  400.       a->neg = 0;
  401.       a->d[0] = w;
  402.       a->top = (w ? 1 : 0);
             ^
  403.       bn_check_top(a);
  404.       return (1);

crypto/bn/bn_mont.c:319:15: Call
317.   # else
318.           BN_zero(R);
319.           if (!(BN_set_bit(R, BN_BITS2)))
                     ^
320.               goto err;           /* R */
321.   

crypto/bn/bn_lib.c:624:1: Parameter `a->top`
  622.   }
  623.   
  624. > int BN_set_bit(BIGNUM *a, int n)
  625.   {
  626.       int i, j, k;

crypto/bn/bn_mont.c:326:14: Call
324.           tmod.top = buf[0] != 0 ? 1 : 0;
325.           /* Ri = R^-1 mod N */
326.           if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)
                    ^
327.               goto err;
328.           if (!BN_lshift(Ri, Ri, BN_BITS2))

crypto/bn/bn_gcd.c:124:1: Parameter `a->top`
  122.                                           BN_CTX *ctx);
  123.   
  124. > BIGNUM *BN_mod_inverse(BIGNUM *in,
  125.                          const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)
  126.   {

crypto/bn/bn_gcd.c:129:10: Call
  127.       BIGNUM *rv;
  128.       int noinv;
  129.       rv = int_bn_mod_inverse(in, a, n, ctx, &noinv);
                  ^
  130.       if (noinv)
  131.           BNerr(BN_F_BN_MOD_INVERSE, BN_R_NO_INVERSE);

crypto/bn/bn_gcd.c:135:1: Parameter `a->top`
    133.   }
    134.   
    135. > BIGNUM *int_bn_mod_inverse(BIGNUM *in,
    136.                              const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,
    137.                              int *pnoinv)

crypto/bn/bn_mont.c:369:5: Call
367.   
368.       /* setup RR for conversions */
369.       BN_zero(&(mont->RR));
           ^
370.       if (!BN_set_bit(&(mont->RR), mont->ri * 2))
371.           goto err;

crypto/bn/bn_lib.c:402:15: Assignment
  400.       a->neg = 0;
  401.       a->d[0] = w;
  402.       a->top = (w ? 1 : 0);
                       ^
  403.       bn_check_top(a);
  404.       return (1);

crypto/bn/bn_lib.c:402:5: Assignment
  400.       a->neg = 0;
  401.       a->d[0] = w;
  402.       a->top = (w ? 1 : 0);
             ^
  403.       bn_check_top(a);
  404.       return (1);

crypto/bn/bn_mont.c:370:10: Call
368.       /* setup RR for conversions */
369.       BN_zero(&(mont->RR));
370.       if (!BN_set_bit(&(mont->RR), mont->ri * 2))
                ^
371.           goto err;
372.       if (!BN_mod(&(mont->RR), &(mont->RR), &(mont->N), ctx))

crypto/bn/bn_lib.c:624:1: Parameter `a->top`
  622.   }
  623.   
  624. > int BN_set_bit(BIGNUM *a, int n)
  625.   {
  626.       int i, j, k;

crypto/bn/bn_lib.c:634:13: Call
  632.       j = n % BN_BITS2;
  633.       if (a->top <= i) {
  634.           if (bn_wexpand(a, i + 1) == NULL)
                     ^
  635.               return (0);
  636.           for (k = a->top; k < i + 1; k++)

crypto/bn/bn_lib.c:948:1: Parameter `a->top`
    946.   }
    947.   
    948. > BIGNUM *bn_wexpand(BIGNUM *a, int words)
    949.   {
    950.       return (words <= a->dmax) ? a : bn_expand2(a, words);

crypto/bn/bn_lib.c:950:37: Call
    948.   BIGNUM *bn_wexpand(BIGNUM *a, int words)
    949.   {
    950.       return (words <= a->dmax) ? a : bn_expand2(a, words);
                                               ^
    951.   }
    952.   

crypto/bn/bn_lib.c:284:1: Parameter `b->top`
      282.    */
      283.   
      284. > BIGNUM *bn_expand2(BIGNUM *b, int words)
      285.   {
      286.       bn_check_top(b);

crypto/bn/bn_lib.c:289:23: Call
      287.   
      288.       if (words > b->dmax) {
      289.           BN_ULONG *a = bn_expand_internal(b, words);
                                   ^
      290.           if (!a)
      291.               return NULL;

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

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

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

crypto/bn/bn_lib.c:246:1: Parameter `words`
        244.   /* This is used by bn_expand2() */
        245.   /* The caller MUST check that words > b->dmax before calling this */
        246. > static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
        247.   {
        248.       BN_ULONG *a = NULL;

crypto/bn/bn_lib.c:263:13: Call
        261.           a = OPENSSL_secure_zalloc(words * sizeof(*a));
        262.       else
        263.           a = OPENSSL_zalloc(words * sizeof(*a));
                           ^
        264.       if (a == NULL) {
        265.           BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);

crypto/mem.c:186:1: Parameter `num`
          184.   }
          185.   
          186. > void *CRYPTO_zalloc(size_t num, const char *file, int line)
          187.   {
          188.       void *ret = CRYPTO_malloc(num, file, line);

crypto/mem.c:188:17: Call
          186.   void *CRYPTO_zalloc(size_t num, const char *file, int line)
          187.   {
          188.       void *ret = CRYPTO_malloc(num, file, line);
                                 ^
          189.   
          190.       FAILTEST();

crypto/mem.c:166:9: Assignment
            164.   
            165.       if (num == 0)
            166.           return NULL;
                           ^
            167.   
            168.       FAILTEST();

crypto/mem.c:188:5: Assignment
          186.   void *CRYPTO_zalloc(size_t num, const char *file, int line)
          187.   {
          188.       void *ret = CRYPTO_malloc(num, file, line);
                     ^
          189.   
          190.       FAILTEST();

crypto/mem.c:193:5: Assignment
          191.       if (ret != NULL)
          192.           memset(ret, 0, num);
          193.       return ret;
                     ^
          194.   }
          195.   

crypto/bn/bn_lib.c:263:9: Assignment
        261.           a = OPENSSL_secure_zalloc(words * sizeof(*a));
        262.       else
        263.           a = OPENSSL_zalloc(words * sizeof(*a));
                       ^
        264.       if (a == NULL) {
        265.           BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);

crypto/bn/bn_lib.c:271:9: Array access: Offset added: [8, +oo] Size: [0, 67108856] by call to `BN_set_bit` 
        269.       assert(b->top <= words);
        270.       if (b->top > 0)
        271.           memcpy(a, b->d, sizeof(*a) * b->top);
                       ^
        272.   
        273.       return a;

static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
{
    BN_ULONG *a = NULL;
    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 = 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;
}
['int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)\n{\n    int ret = 0;\n    BIGNUM *Ri, *R;\n    if (BN_is_zero(mod))\n        return 0;\n    BN_CTX_start(ctx);\n    if ((Ri = BN_CTX_get(ctx)) == NULL)\n        goto err;\n    R = &(mont->RR);\n    if (!BN_copy(&(mont->N), mod))\n        goto err;\n    mont->N.neg = 0;\n#ifdef MONT_WORD\n    {\n        BIGNUM tmod;\n        BN_ULONG buf[2];\n        bn_init(&tmod);\n        tmod.d = buf;\n        tmod.dmax = 2;\n        tmod.neg = 0;\n        mont->ri = (BN_num_bits(mod) + (BN_BITS2 - 1)) / BN_BITS2 * BN_BITS2;\n# if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2<=32)\n        BN_zero(R);\n        if (!(BN_set_bit(R, 2 * BN_BITS2)))\n            goto err;\n        tmod.top = 0;\n        if ((buf[0] = mod->d[0]))\n            tmod.top = 1;\n        if ((buf[1] = mod->top > 1 ? mod->d[1] : 0))\n            tmod.top = 2;\n        if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n            goto err;\n        if (!BN_lshift(Ri, Ri, 2 * BN_BITS2))\n            goto err;\n        if (!BN_is_zero(Ri)) {\n            if (!BN_sub_word(Ri, 1))\n                goto err;\n        } else {\n            if (bn_expand(Ri, (int)sizeof(BN_ULONG) * 2) == NULL)\n                goto err;\n            Ri->neg = 0;\n            Ri->d[0] = BN_MASK2;\n            Ri->d[1] = BN_MASK2;\n            Ri->top = 2;\n        }\n        if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n            goto err;\n        mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n        mont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0;\n# else\n        BN_zero(R);\n        if (!(BN_set_bit(R, BN_BITS2)))\n            goto err;\n        buf[0] = mod->d[0];\n        buf[1] = 0;\n        tmod.top = buf[0] != 0 ? 1 : 0;\n        if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n            goto err;\n        if (!BN_lshift(Ri, Ri, BN_BITS2))\n            goto err;\n        if (!BN_is_zero(Ri)) {\n            if (!BN_sub_word(Ri, 1))\n                goto err;\n        } else {\n            if (!BN_set_word(Ri, BN_MASK2))\n                goto err;\n        }\n        if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n            goto err;\n        mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n        mont->n0[1] = 0;\n# endif\n    }\n#else\n    {\n        mont->ri = BN_num_bits(&mont->N);\n        BN_zero(R);\n        if (!BN_set_bit(R, mont->ri))\n            goto err;\n        if ((BN_mod_inverse(Ri, R, &mont->N, ctx)) == NULL)\n            goto err;\n        if (!BN_lshift(Ri, Ri, mont->ri))\n            goto err;\n        if (!BN_sub_word(Ri, 1))\n            goto err;\n        if (!BN_div(&(mont->Ni), NULL, Ri, &mont->N, ctx))\n            goto err;\n    }\n#endif\n    BN_zero(&(mont->RR));\n    if (!BN_set_bit(&(mont->RR), mont->ri * 2))\n        goto err;\n    if (!BN_mod(&(mont->RR), &(mont->RR), &(mont->N), ctx))\n        goto err;\n    ret = 1;\n err:\n    BN_CTX_end(ctx);\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_set_bit(BIGNUM *a, int n)\n{\n    int i, j, k;\n    if (n < 0)\n        return 0;\n    i = n / BN_BITS2;\n    j = n % BN_BITS2;\n    if (a->top <= i) {\n        if (bn_wexpand(a, i + 1) == NULL)\n            return (0);\n        for (k = a->top; k < i + 1; k++)\n            a->d[k] = 0;\n        a->top = i + 1;\n    }\n    a->d[i] |= (((BN_ULONG)1) << j);\n    bn_check_top(a);\n    return (1);\n}', 'BIGNUM *BN_mod_inverse(BIGNUM *in,\n                       const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n{\n    BIGNUM *rv;\n    int noinv;\n    rv = int_bn_mod_inverse(in, a, n, ctx, &noinv);\n    if (noinv)\n        BNerr(BN_F_BN_MOD_INVERSE, BN_R_NO_INVERSE);\n    return rv;\n}', 'BIGNUM *int_bn_mod_inverse(BIGNUM *in,\n                           const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,\n                           int *pnoinv)\n{\n    BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n    BIGNUM *ret = NULL;\n    int sign;\n    if (pnoinv)\n        *pnoinv = 0;\n    if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0)\n        || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0)) {\n        return BN_mod_inverse_no_branch(in, a, n, ctx);\n    }\n    bn_check_top(a);\n    bn_check_top(n);\n    BN_CTX_start(ctx);\n    A = BN_CTX_get(ctx);\n    B = BN_CTX_get(ctx);\n    X = BN_CTX_get(ctx);\n    D = BN_CTX_get(ctx);\n    M = BN_CTX_get(ctx);\n    Y = BN_CTX_get(ctx);\n    T = BN_CTX_get(ctx);\n    if (T == NULL)\n        goto err;\n    if (in == NULL)\n        R = BN_new();\n    else\n        R = in;\n    if (R == NULL)\n        goto err;\n    BN_one(X);\n    BN_zero(Y);\n    if (BN_copy(B, a) == NULL)\n        goto err;\n    if (BN_copy(A, n) == NULL)\n        goto err;\n    A->neg = 0;\n    if (B->neg || (BN_ucmp(B, A) >= 0)) {\n        if (!BN_nnmod(B, B, A, ctx))\n            goto err;\n    }\n    sign = -1;\n    if (BN_is_odd(n) && (BN_num_bits(n) <= 2048)) {\n        int shift;\n        while (!BN_is_zero(B)) {\n            shift = 0;\n            while (!BN_is_bit_set(B, shift)) {\n                shift++;\n                if (BN_is_odd(X)) {\n                    if (!BN_uadd(X, X, n))\n                        goto err;\n                }\n                if (!BN_rshift1(X, X))\n                    goto err;\n            }\n            if (shift > 0) {\n                if (!BN_rshift(B, B, shift))\n                    goto err;\n            }\n            shift = 0;\n            while (!BN_is_bit_set(A, shift)) {\n                shift++;\n                if (BN_is_odd(Y)) {\n                    if (!BN_uadd(Y, Y, n))\n                        goto err;\n                }\n                if (!BN_rshift1(Y, Y))\n                    goto err;\n            }\n            if (shift > 0) {\n                if (!BN_rshift(A, A, shift))\n                    goto err;\n            }\n            if (BN_ucmp(B, A) >= 0) {\n                if (!BN_uadd(X, X, Y))\n                    goto err;\n                if (!BN_usub(B, B, A))\n                    goto err;\n            } else {\n                if (!BN_uadd(Y, Y, X))\n                    goto err;\n                if (!BN_usub(A, A, B))\n                    goto err;\n            }\n        }\n    } else {\n        while (!BN_is_zero(B)) {\n            BIGNUM *tmp;\n            if (BN_num_bits(A) == BN_num_bits(B)) {\n                if (!BN_one(D))\n                    goto err;\n                if (!BN_sub(M, A, B))\n                    goto err;\n            } else if (BN_num_bits(A) == BN_num_bits(B) + 1) {\n                if (!BN_lshift1(T, B))\n                    goto err;\n                if (BN_ucmp(A, T) < 0) {\n                    if (!BN_one(D))\n                        goto err;\n                    if (!BN_sub(M, A, B))\n                        goto err;\n                } else {\n                    if (!BN_sub(M, A, T))\n                        goto err;\n                    if (!BN_add(D, T, B))\n                        goto err;\n                    if (BN_ucmp(A, D) < 0) {\n                        if (!BN_set_word(D, 2))\n                            goto err;\n                    } else {\n                        if (!BN_set_word(D, 3))\n                            goto err;\n                        if (!BN_sub(M, M, B))\n                            goto err;\n                    }\n                }\n            } else {\n                if (!BN_div(D, M, A, B, ctx))\n                    goto err;\n            }\n            tmp = A;\n            A = B;\n            B = M;\n            if (BN_is_one(D)) {\n                if (!BN_add(tmp, X, Y))\n                    goto err;\n            } else {\n                if (BN_is_word(D, 2)) {\n                    if (!BN_lshift1(tmp, X))\n                        goto err;\n                } else if (BN_is_word(D, 4)) {\n                    if (!BN_lshift(tmp, X, 2))\n                        goto err;\n                } else if (D->top == 1) {\n                    if (!BN_copy(tmp, X))\n                        goto err;\n                    if (!BN_mul_word(tmp, D->d[0]))\n                        goto err;\n                } else {\n                    if (!BN_mul(tmp, D, X, ctx))\n                        goto err;\n                }\n                if (!BN_add(tmp, tmp, Y))\n                    goto err;\n            }\n            M = Y;\n            Y = X;\n            X = tmp;\n            sign = -sign;\n        }\n    }\n    if (sign < 0) {\n        if (!BN_sub(Y, n, Y))\n            goto err;\n    }\n    if (BN_is_one(A)) {\n        if (!Y->neg && BN_ucmp(Y, n) < 0) {\n            if (!BN_copy(R, Y))\n                goto err;\n        } else {\n            if (!BN_nnmod(R, Y, n, ctx))\n                goto err;\n        }\n    } else {\n        if (pnoinv)\n            *pnoinv = 1;\n        goto err;\n    }\n    ret = R;\n err:\n    if ((ret == NULL) && (in == NULL))\n        BN_free(R);\n    BN_CTX_end(ctx);\n    bn_check_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 = NULL;\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 = 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}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n    void *ret = CRYPTO_malloc(num, file, line);\n    FAILTEST();\n    if (ret != NULL)\n        memset(ret, 0, num);\n    return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n    void *ret = NULL;\n    if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n        return malloc_impl(num, file, line);\n    if (num == 0)\n        return NULL;\n    FAILTEST();\n    allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n    if (call_malloc_debug) {\n        CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n        ret = malloc(num);\n        CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n    } else {\n        ret = malloc(num);\n    }\n#else\n    osslargused(file); osslargused(line);\n    ret = malloc(num);\n#endif\n    return ret;\n}']