test/ecdsatest.c:359: error: BUFFER_OVERRUN_L3
  Offset added: [8, +oo] Size: [0, 536870848] by call to `BN_bin2bn`.
Showing all 14 steps of the trace


test/ecdsatest.c:358:14: Call
356.           if (!TEST_ptr(modified_sig = ECDSA_SIG_new()))
357.               goto builtin_err;
358.           if (!TEST_ptr(modified_r = BN_bin2bn(raw_buf, bn_len, NULL))
                    ^
359.                   || !TEST_ptr(modified_s = BN_bin2bn(raw_buf + bn_len,
360.                                                       bn_len, NULL))

crypto/bn/bn_lib.c:407:1: Parameter `*ret->d`
  405.   }
  406.   
  407. > BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
  408.   {
  409.       unsigned int i, m;

test/ecdsatest.c:359:21: Call
357.               goto builtin_err;
358.           if (!TEST_ptr(modified_r = BN_bin2bn(raw_buf, bn_len, NULL))
359.                   || !TEST_ptr(modified_s = BN_bin2bn(raw_buf + bn_len,
                           ^
360.                                                       bn_len, NULL))
361.                   || !TEST_true(ECDSA_SIG_set0(modified_sig,

crypto/bn/bn_lib.c:407:1: Parameter `*ret->d`
  405.   }
  406.   
  407. > BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
  408.   {
  409.       unsigned int i, m;

crypto/bn/bn_lib.c:429:9: Call
  427.       i = ((n - 1) / BN_BYTES) + 1;
  428.       m = ((n - 1) % (BN_BYTES));
  429.       if (bn_wexpand(ret, (int)i) == NULL) {
                 ^
  430.           BN_free(bn);
  431.           return NULL;

crypto/bn/bn_lib.c:948:1: Parameter `*a->d`
    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->d`
      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 `*b->d`
        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:271:9: Array access: Offset added: [8, +oo] Size: [0, 536870848] by call to `BN_bin2bn` 
        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;
}
['static int test_builtin(void)\n{\n    EC_builtin_curve *curves = NULL;\n    size_t crv_len = 0, n = 0;\n    EC_KEY *eckey = NULL, *wrong_eckey = NULL;\n    EC_GROUP *group;\n    ECDSA_SIG *ecdsa_sig = NULL, *modified_sig = NULL;\n    unsigned char digest[20], wrong_digest[20];\n    unsigned char *signature = NULL;\n    const unsigned char *sig_ptr;\n    unsigned char *sig_ptr2;\n    unsigned char *raw_buf = NULL;\n    const BIGNUM *sig_r, *sig_s;\n    BIGNUM *modified_r = NULL, *modified_s = NULL;\n    BIGNUM *unmodified_r = NULL, *unmodified_s = NULL;\n    unsigned int sig_len, degree, r_len, s_len, bn_len, buf_len;\n    int nid, ret = 0;\n    if (!TEST_true(RAND_bytes(digest, 20))\n            || !TEST_true(RAND_bytes(wrong_digest, 20)))\n        goto builtin_err;\n    crv_len = EC_get_builtin_curves(NULL, 0);\n    if (!TEST_ptr(curves = OPENSSL_malloc(sizeof(*curves) * crv_len))\n            || !TEST_true(EC_get_builtin_curves(curves, crv_len)))\n        goto builtin_err;\n    for (n = 0; n < crv_len; n++) {\n        unsigned char dirt, offset;\n        nid = curves[n].nid;\n        if (nid == NID_ipsec4 || nid == NID_X25519)\n            continue;\n        if (!TEST_ptr(eckey = EC_KEY_new())\n                || !TEST_ptr(group = EC_GROUP_new_by_curve_name(nid))\n                || !TEST_true(EC_KEY_set_group(eckey, group)))\n            goto builtin_err;\n        EC_GROUP_free(group);\n        degree = EC_GROUP_get_degree(EC_KEY_get0_group(eckey));\n        if (degree < 160) {\n            EC_KEY_free(eckey);\n            eckey = NULL;\n            continue;\n        }\n        TEST_info("testing %s", OBJ_nid2sn(nid));\n        if (!TEST_true(EC_KEY_generate_key(eckey)))\n            goto builtin_err;\n        if (!TEST_ptr(wrong_eckey = EC_KEY_new())\n                || !TEST_ptr(group = EC_GROUP_new_by_curve_name(nid))\n                || !TEST_true(EC_KEY_set_group(wrong_eckey, group)))\n            goto builtin_err;\n        EC_GROUP_free(group);\n        if (!TEST_true(EC_KEY_generate_key(wrong_eckey)))\n            goto builtin_err;\n        if (!TEST_true(EC_KEY_check_key(eckey)))\n            goto builtin_err;\n        sig_len = ECDSA_size(eckey);\n        if (!TEST_ptr(signature = OPENSSL_malloc(sig_len))\n                || !TEST_true(ECDSA_sign(0, digest, 20, signature, &sig_len,\n                                         eckey)))\n            goto builtin_err;\n        if (!TEST_int_eq(ECDSA_verify(0, digest, 20, signature, sig_len,\n                                      eckey), 1))\n            goto builtin_err;\n        if (!TEST_int_ne(ECDSA_verify(0, digest, 20, signature, sig_len,\n                                      wrong_eckey), 1))\n            goto builtin_err;\n        if (!TEST_int_ne(ECDSA_verify(0, wrong_digest, 20, signature,\n                                      sig_len, eckey), 1))\n            goto builtin_err;\n        if (!TEST_int_ne(ECDSA_verify(0, digest, 20, signature,\n                                      sig_len - 1, eckey), 1))\n            goto builtin_err;\n        sig_ptr = signature;\n        if (!TEST_ptr(ecdsa_sig = d2i_ECDSA_SIG(NULL, &sig_ptr, sig_len)))\n            goto builtin_err;\n        ECDSA_SIG_get0(ecdsa_sig, &sig_r, &sig_s);\n        r_len = BN_num_bytes(sig_r);\n        s_len = BN_num_bytes(sig_s);\n        bn_len = (degree + 7) / 8;\n        if (!TEST_false(r_len > bn_len)\n                || !TEST_false(s_len > bn_len))\n            goto builtin_err;\n        buf_len = 2 * bn_len;\n        if (!TEST_ptr(raw_buf = OPENSSL_zalloc(buf_len)))\n            goto builtin_err;\n        BN_bn2bin(sig_r, raw_buf + bn_len - r_len);\n        BN_bn2bin(sig_s, raw_buf + buf_len - s_len);\n        offset = raw_buf[10] % buf_len;\n        dirt = raw_buf[11] ? raw_buf[11] : 1;\n        raw_buf[offset] ^= dirt;\n        if (!TEST_ptr(modified_sig = ECDSA_SIG_new()))\n            goto builtin_err;\n        if (!TEST_ptr(modified_r = BN_bin2bn(raw_buf, bn_len, NULL))\n                || !TEST_ptr(modified_s = BN_bin2bn(raw_buf + bn_len,\n                                                    bn_len, NULL))\n                || !TEST_true(ECDSA_SIG_set0(modified_sig,\n                                             modified_r, modified_s))) {\n            BN_free(modified_r);\n            BN_free(modified_s);\n            goto builtin_err;\n        }\n        sig_ptr2 = signature;\n        sig_len = i2d_ECDSA_SIG(modified_sig, &sig_ptr2);\n        if (!TEST_false(ECDSA_verify(0, digest, 20, signature, sig_len, eckey)))\n            goto builtin_err;\n        raw_buf[offset] ^= dirt;\n        if (!TEST_ptr(unmodified_r = BN_bin2bn(raw_buf, bn_len, NULL))\n                || !TEST_ptr(unmodified_s = BN_bin2bn(raw_buf + bn_len,\n                                                       bn_len, NULL))\n                || !TEST_true(ECDSA_SIG_set0(modified_sig, unmodified_r,\n                                             unmodified_s))) {\n            BN_free(unmodified_r);\n            BN_free(unmodified_s);\n            goto builtin_err;\n        }\n        sig_ptr2 = signature;\n        sig_len = i2d_ECDSA_SIG(modified_sig, &sig_ptr2);\n        if (!TEST_true(ECDSA_verify(0, digest, 20, signature, sig_len, eckey)))\n            goto builtin_err;\n        ERR_clear_error();\n        OPENSSL_free(signature);\n        signature = NULL;\n        EC_KEY_free(eckey);\n        eckey = NULL;\n        EC_KEY_free(wrong_eckey);\n        wrong_eckey = NULL;\n        ECDSA_SIG_free(ecdsa_sig);\n        ecdsa_sig = NULL;\n        ECDSA_SIG_free(modified_sig);\n        modified_sig = NULL;\n        OPENSSL_free(raw_buf);\n        raw_buf = NULL;\n    }\n    ret = 1;\n builtin_err:\n    EC_KEY_free(eckey);\n    EC_KEY_free(wrong_eckey);\n    ECDSA_SIG_free(ecdsa_sig);\n    ECDSA_SIG_free(modified_sig);\n    OPENSSL_free(signature);\n    OPENSSL_free(raw_buf);\n    OPENSSL_free(curves);\n    return ret;\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 = 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}']