test/dhtest.c:60: error: BUFFER_OVERRUN_L3
  Offset added: [8, +oo] Size: [0, 536870848] by call to `BN_set_word`.
Showing all 15 steps of the trace


test/dhtest.c:59:10: Call
57.   
58.       /* using a small predefined Sophie Germain DH group with generator 3 */
59.       if (!TEST_true(BN_set_word(p, 4079L))
               ^
60.           || !TEST_true(BN_set_word(q, 2039L))
61.           || !TEST_true(BN_set_word(g, 3L))

crypto/bn/bn_lib.c:367:15: Assignment
  365.       a->neg = 0;
  366.       a->d[0] = w;
  367.       a->top = (w ? 1 : 0);
                       ^
  368.       bn_check_top(a);
  369.       return 1;

crypto/bn/bn_lib.c:367:5: Assignment
  365.       a->neg = 0;
  366.       a->d[0] = w;
  367.       a->top = (w ? 1 : 0);
             ^
  368.       bn_check_top(a);
  369.       return 1;

test/dhtest.c:60:13: Call
58.       /* using a small predefined Sophie Germain DH group with generator 3 */
59.       if (!TEST_true(BN_set_word(p, 4079L))
60.           || !TEST_true(BN_set_word(q, 2039L))
                  ^
61.           || !TEST_true(BN_set_word(g, 3L))
62.           || !TEST_true(DH_set0_pqg(dh, p, q, g)))

crypto/bn/bn_lib.c:360:1: Parameter `*a->d`
  358.   }
  359.   
  360. > int BN_set_word(BIGNUM *a, BN_ULONG w)
  361.   {
  362.       bn_check_top(a);

crypto/bn/bn_lib.c:363:9: Call
  361.   {
  362.       bn_check_top(a);
  363.       if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)
                 ^
  364.           return 0;
  365.       a->neg = 0;

crypto/bn/bn_lcl.h:651:1: Parameter `*a->d`
    649.                            const BIGNUM *add, const BIGNUM *rem, BN_CTX *ctx);
    650.   
    651. > static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)
    652.   {
    653.       if (bits > (INT_MAX - BN_BITS2 + 1))

crypto/bn/bn_lcl.h:659:12: Call
    657.           return a;
    658.   
    659.       return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);
                      ^
    660.   }
    661.   

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

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

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

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

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

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

crypto/bn/bn_lib.c:233:9: Array access: Offset added: [8, +oo] Size: [0, 536870848] by call to `BN_set_word` 
        231.       assert(b->top <= words);
        232.       if (b->top > 0)
        233.           memcpy(a, b->d, sizeof(*a) * b->top);
                       ^
        234.   
        235.       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;
}
['static int dh_test(void)\n{\n    DH *dh;\n    BIGNUM *p, *q, *g;\n    const BIGNUM *p2, *q2, *g2;\n    BIGNUM *priv_key;\n    const BIGNUM *pub_key2, *priv_key2;\n    BN_GENCB *_cb = NULL;\n    DH *a = NULL;\n    DH *b = NULL;\n    DH *c = NULL;\n    const BIGNUM *ap = NULL, *ag = NULL, *apub_key = NULL;\n    const BIGNUM *bpub_key = NULL, *bpriv_key = NULL;\n    BIGNUM *bp = NULL, *bg = NULL, *cpriv_key = NULL;\n    unsigned char *abuf = NULL;\n    unsigned char *bbuf = NULL;\n    unsigned char *cbuf = NULL;\n    int i, alen, blen, clen, aout, bout, cout;\n    int ret = 0;\n    if (!TEST_ptr(dh = DH_new())\n        || !TEST_ptr(p = BN_new())\n        || !TEST_ptr(q = BN_new())\n        || !TEST_ptr(g = BN_new())\n        || !TEST_ptr(priv_key = BN_new()))\n        goto err;\n    if (!TEST_true(BN_set_word(p, 4079L))\n        || !TEST_true(BN_set_word(q, 2039L))\n        || !TEST_true(BN_set_word(g, 3L))\n        || !TEST_true(DH_set0_pqg(dh, p, q, g)))\n        goto err;\n    DH_get0_pqg(dh, &p2, &q2, &g2);\n    if (!TEST_ptr_eq(p2, p)\n        || !TEST_ptr_eq(q2, q)\n        || !TEST_ptr_eq(g2, g))\n        goto err;\n    if (!TEST_ptr_eq(DH_get0_p(dh), p2)\n        || !TEST_ptr_eq(DH_get0_q(dh), q2)\n        || !TEST_ptr_eq(DH_get0_g(dh), g2))\n        goto err;\n    if (!TEST_true(BN_set_word(priv_key, 1234L))\n        || !TEST_true(DH_set0_key(dh, NULL, priv_key)))\n        goto err;\n    DH_get0_key(dh, &pub_key2, &priv_key2);\n    if (!TEST_ptr_eq(pub_key2, NULL)\n        || !TEST_ptr_eq(priv_key2, priv_key))\n        goto err;\n    if (!TEST_ptr_eq(DH_get0_pub_key(dh), pub_key2)\n        || !TEST_ptr_eq(DH_get0_priv_key(dh), priv_key2))\n        goto err;\n    if (!DH_generate_key(dh))\n        goto err;\n    DH_get0_key(dh, &pub_key2, &priv_key2);\n    if (!TEST_ptr(pub_key2)\n        || !TEST_ptr_eq(priv_key2, priv_key))\n        goto err;\n    if (!TEST_ptr_eq(DH_get0_pub_key(dh), pub_key2)\n        || !TEST_ptr_eq(DH_get0_priv_key(dh), priv_key2))\n        goto err;\n    TEST_uint_eq(BN_get_word(pub_key2), 3331L);\n    if (!TEST_ptr(_cb = BN_GENCB_new()))\n        goto err;\n    BN_GENCB_set(_cb, &cb, NULL);\n    if (!TEST_ptr(a = DH_new())\n            || !TEST_true(DH_generate_parameters_ex(a, 64,\n                                                    DH_GENERATOR_5, _cb)))\n        goto err;\n    if (!DH_check(a, &i))\n        goto err;\n    if (!TEST_false(i & DH_CHECK_P_NOT_PRIME)\n            || !TEST_false(i & DH_CHECK_P_NOT_SAFE_PRIME)\n            || !TEST_false(i & DH_UNABLE_TO_CHECK_GENERATOR)\n            || !TEST_false(i & DH_NOT_SUITABLE_GENERATOR))\n        goto err;\n    DH_get0_pqg(a, &ap, NULL, &ag);\n    if (!TEST_ptr(b = DH_new()))\n        goto err;\n    if (!TEST_ptr(bp = BN_dup(ap))\n            || !TEST_ptr(bg = BN_dup(ag))\n            || !TEST_true(DH_set0_pqg(b, bp, NULL, bg)))\n        goto err;\n    bp = bg = NULL;\n    if (!DH_generate_key(a))\n        goto err;\n    DH_get0_key(a, &apub_key, NULL);\n    if (!DH_generate_key(b))\n        goto err;\n    DH_get0_key(b, &bpub_key, &bpriv_key);\n    if (!TEST_ptr(c = DHparams_dup(b))\n            || !TEST_ptr(cpriv_key = BN_dup(bpriv_key))\n            || !TEST_true(DH_set0_key(c, NULL, cpriv_key)))\n        goto err;\n    cpriv_key = NULL;\n    alen = DH_size(a);\n    if (!TEST_ptr(abuf = OPENSSL_malloc(alen))\n            || !TEST_true((aout = DH_compute_key(abuf, bpub_key, a)) != -1))\n        goto err;\n    blen = DH_size(b);\n    if (!TEST_ptr(bbuf = OPENSSL_malloc(blen))\n            || !TEST_true((bout = DH_compute_key(bbuf, apub_key, b)) != -1))\n        goto err;\n    clen = DH_size(c);\n    if (!TEST_ptr(cbuf = OPENSSL_malloc(clen))\n            || !TEST_true((cout = DH_compute_key(cbuf, apub_key, c)) != -1))\n        goto err;\n    if (!TEST_true(aout >= 4)\n            || !TEST_mem_eq(abuf, aout, bbuf, bout)\n            || !TEST_mem_eq(abuf, aout, cbuf, cout))\n        goto err;\n    ret = 1;\n err:\n    OPENSSL_free(abuf);\n    OPENSSL_free(bbuf);\n    OPENSSL_free(cbuf);\n    DH_free(b);\n    DH_free(a);\n    DH_free(c);\n    BN_free(bp);\n    BN_free(bg);\n    BN_free(cpriv_key);\n    BN_GENCB_free(_cb);\n    DH_free(dh);\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}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n    if (bits > (INT_MAX - BN_BITS2 + 1))\n        return NULL;\n    if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n        return a;\n    return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\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}']