From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp1.linuxfoundation.org (smtp1.linux-foundation.org [172.17.192.35]) by mail.linuxfoundation.org (Postfix) with ESMTPS id 4ACB2305 for ; Tue, 18 Aug 2015 10:31:59 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from wp059.webpack.hosteurope.de (wp059.webpack.hosteurope.de [80.237.132.66]) by smtp1.linuxfoundation.org (Postfix) with ESMTPS id 9B8FE164 for ; Tue, 18 Aug 2015 10:31:56 +0000 (UTC) Received: from [81.0.112.130] (helo=[192.168.0.109]); authenticated by wp059.webpack.hosteurope.de running ExIM with esmtpsa (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) id 1ZReBO-0007T7-63; Tue, 18 Aug 2015 12:31:54 +0200 Mime-Version: 1.0 (Mac OS X Mail 8.2 \(2102\)) Content-Type: multipart/signed; boundary="Apple-Mail=_ABE2F079-7D85-4296-8776-AF2355709186"; protocol="application/pgp-signature"; micalg=pgp-sha512 X-Pgp-Agent: GPGMail 2.5.1 From: Tamas Blummer In-Reply-To: Date: Tue, 18 Aug 2015 12:31:52 +0200 Message-Id: <68E206FF-4ABD-4547-BF73-8661A7C2F08B@bitsofproof.com> References: <09C8843E-8379-404D-8357-05BDB1F749C1@me.com> <499C1F46-5EB8-4846-86B6-0B3F2E02D972@bitsofproof.com> To: Cory Fields X-Mailer: Apple Mail (2.2102) X-bounce-key: webpack.hosteurope.de; tamas@bitsofproof.com; 1439893916; 24e43bee; X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,HTML_MESSAGE, RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on smtp1.linux-foundation.org Cc: bitcoin-dev@lists.linuxfoundation.org Subject: Re: [bitcoin-dev] libconsensus assertion fails if used in multiple threads X-BeenThere: bitcoin-dev@lists.linuxfoundation.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: Bitcoin Development Discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 18 Aug 2015 10:31:59 -0000 --Apple-Mail=_ABE2F079-7D85-4296-8776-AF2355709186 Content-Type: multipart/alternative; boundary="Apple-Mail=_02C4D528-0B90-446A-B445-C69F5D37CC35" --Apple-Mail=_02C4D528-0B90-446A-B445-C69F5D37CC35 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 Thanks a lot Cory for following through the test case and producing a = patch. I confirm that libconsensus is now running stable within the Bits of = Proof stack, in-line with test cases we use to verify the java implementation of the = script engine, that are BTW borrowed from Bitcoin Core. The performance of libconsensus is surprisingly close to the java one. Validating a 2-of-2 a multi-sig transaction runs at 1021 ops/sec with = java and 1135 ops/sec in libconsensus. This is on a 2.2GH i7 laptop (4 hyper threading cores = used by 8 threads). Another nice demonstration why one should not trade in advances of languages for the last decades for a marginal gain of performance = with C/C++, I assume thereby that Bouncy Castle=E2=80=99 EC lib s not superior to = OpenSSL's. I disagree that the problem was rare in the real-world, it should affect = any modern implementation that validates transactions parallel in multiple threads. Aborting also does not make the problem less severe in my opinion. Therefore hope the pull will be included into Core with next release. I can=E2=80=99t assign a timeline to =E2=80=9Cnear future" secp256k1 = integration. Can you? Tamas Blummer > On Aug 18, 2015, at 07:03, Cory Fields wrote: >=20 > Back to the list (from github) in case anyone finds this via Google. >=20 > The patch that I posted here a few days ago did not fix the issue for = Tamas. >=20 > I spent some time tracking down this edge-case because > libbitcoinconsensus needs to be as bullet-proof as possible. Thanks to > Tamas for creating a bare-bones test case after some discussion. >=20 > I finally managed to reproduce the issue on OSX. It's subtle and > likely rare in the real-world, though obviously not impossible given > the report here. For posterity, here's a rundown (braindump) of the > issue. >=20 > When calling EC_KEY_new_by_curve_name(), openssl internally checks to > see how to setup the curve's EC_METHOD (simple, montgomery, or nist). >=20 > Unfortunately, in all released OpenSSL versions (as far as I can tell > master is the only branch that has fixed this issue), it's tested like > so: >=20 > - Try a method. If it fails, set a global error and return. > - If the global error is set, try a different method. >=20 > Prior to OpenSSL 1.0.0, these were tested in the order: > EC_GFp_nist_method -> EC_GFp_mont_method. The secp256k1 curve fails > the ec_GFp_nist_group_set_curve test and sets the global error. That > error is then checked for failure, and EC_GFp_mont_method is tried > (and succeeds). >=20 > Obviously that global error usage is dangerous, especially since it > happens for _each_ transaction verification in libbitcoinconsensus. In > a multi-threaded environment, a crash is guaranteed within a few > seconds. >=20 > However, OpenSSL 1.0.1 reversed the order, trying EC_GFp_mont_method > first, so that the global error doesn't end up being used: > = https://github.com/openssl/openssl/commit/17674bfdf75bffa4e225f8328b9d42cb= 74504005 >=20 > This was backported from master back to 1.0.1, but not to 1.0.0 or = 0.9.8. >=20 > So that change (accidentally) "solved" the problem. As you can see, > it's still possible to hit the reversed order in the > !defined(OPENSSL_BN_ASM_MONT) case. That's easily tested by building > OpenSSL with the -no-asm config option. It's probably also the case > for obscure architectures and OSs, but I haven't looked deeply into > that. In that case, it's reasonable to assume that this crash would > likely occur on such platforms. >=20 > Also, OSX, even the latest version (10.10 as of now), still ships with > OpenSSL 0.9.8. Which is how Tamas ran into it. >=20 > Since Bitcoin Core and libbitcoinconsensus are switching away from > OpenSSL for verification in the near future, I don't think this is > much of an issue. Especially since the problem manifests as a > controlled assertion failure/abort. However, I've prepared a patch for > anyone who may run into the issue in the short-term: > = https://github.com/theuni/bitcoin/commit/adf0a691ee1c2f02e26828f976cfe5b78= 896b507 >=20 > I'll open a pull-request for Bitcoin Core to discuss whether it's > worth merging or not. >=20 > Regards, > Cory >=20 > On Fri, Aug 14, 2015 at 5:10 PM, Cory Fields = wrote: >> Ugh, what an unfortunate oversight! >>=20 >> The good news is that this issue should be solved in future versions >> when we switch to the new libsecp256k1 lib for validation. >>=20 >> For now, I've thrown together a quick hack to allow a = user-specifiable >> callback for libbitcoinconsensus. I think it's not worth messing with >> the official API since it will be fixed soon, but rather hacked in as >> a temporary work-around as needed. It _should_ be documented as an >> issue with the current version, though. >>=20 >> Please see here for a work-around to try: >> https://github.com/theuni/bitcoin/commits/openssl-consensus-threads >> Unfortunately it's not pretty, but it works fine here. Note that you >> should give this some _serious_ testing before deploying in any real >> way. It should mimic the way we do it in Core, though. >>=20 >> That's on top of current master, but it should be trivial to apply to >> release tags. >>=20 >> Please let me know how it works out. >>=20 >> Regards, >> Cory >>=20 >> On Fri, Aug 14, 2015 at 12:37 PM, Tamas Blummer via bitcoin-dev >> wrote: >>> We integrated libconsensus into bits of proof. It works well, = in-line for all test cases with our Java engine and is about 50% faster = on a single thread. >>>=20 >>> The performance advantage unfortunatelly reverses if libconsensus is = executed on several threads simultaneously as we do with the Java = engine, since an error: >>>=20 >>> Assertion failed: (pkey !=3D NULL), function CECKey, file = ecwrapper.cpp, line 96. >>>=20 >>> arises under that stress. >>>=20 >>> I guess that the cause is that thread callbacks as advised for = OpenSSL on https://www.openssl.org/docs/crypto/threads.html are not = registered. >>> Registering those however would require access to OpenSSL functions, = not exported from the lib. >>>=20 >>> I=E2=80=99d be thankful for a pointer to a workaround. >>>=20 >>> Tamas Blummer >>>=20 >>> _______________________________________________ >>> bitcoin-dev mailing list >>> bitcoin-dev@lists.linuxfoundation.org >>> https://lists.linuxfoundation.org/mailman/listinfo/bitcoin-dev >>>=20 >=20 --Apple-Mail=_02C4D528-0B90-446A-B445-C69F5D37CC35 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8
Thanks a lot Cory for following through the = test case and producing a patch.

I confirm that libconsensus is now = running stable within the Bits of Proof stack, 
in-line with test cases we use to verify the java = implementation of the script engine,
that are BTW = borrowed from Bitcoin Core.

The performance of libconsensus is surprisingly close to the = java one. 
Validating a 2-of-2 a multi-sig =  transaction runs at 1021 ops/sec with java and 1135 = ops/sec 
in libconsensus. This is on a 2.2GH = i7 laptop (4 hyper threading cores used by 8 threads).
Another nice demonstration why one should not trade in = advances
of languages for the last decades for a = marginal gain of performance with C/C++,
I assume = thereby that Bouncy Castle=E2=80=99 EC lib s not superior to = OpenSSL's.

I = disagree that the problem was rare in the real-world, it should affect = any modern 
implementation that validates = transactions parallel in multiple threads.

Aborting also does not make the problem = less severe in my opinion. 
Therefore hope the = pull will be included into Core with next release.

I can=E2=80=99t assign a = timeline to =E2=80=9Cnear future" secp256k1 integration. Can = you?

Tamas Blummer


On Aug 18, 2015, at 07:03, Cory Fields <lists@coryfields.com> wrote:

Back to the list = (from github) in case anyone finds this via Google.

The patch that I posted here a few days ago did not fix the = issue for Tamas.

I spent some time tracking = down this edge-case because
libbitcoinconsensus needs to = be as bullet-proof as possible. Thanks to
Tamas for = creating a bare-bones test case after some discussion.

I finally managed to reproduce the issue on OSX. It's subtle = and
likely rare in the real-world, though obviously not = impossible given
the report here. For posterity, here's a = rundown (braindump) of the
issue.

When calling EC_KEY_new_by_curve_name(), openssl internally = checks to
see how to setup the curve's EC_METHOD (simple, = montgomery, or nist).

Unfortunately, in all = released OpenSSL versions (as far as I can tell
master is = the only branch that has fixed this issue), it's tested like
so:

- Try a method. If it fails, = set a global error and return.
- If the global error is = set, try a different method.

Prior to = OpenSSL 1.0.0, these were tested in the order:
EC_GFp_nist_method -> EC_GFp_mont_method. The secp256k1 = curve fails
the ec_GFp_nist_group_set_curve test and sets = the global error. That
error is then checked for failure, = and EC_GFp_mont_method is tried
(and succeeds).

Obviously that global error usage is = dangerous, especially since it
happens for _each_ = transaction verification in libbitcoinconsensus. In
a = multi-threaded environment, a crash is guaranteed within a few
seconds.

However, OpenSSL 1.0.1 = reversed the order, trying EC_GFp_mont_method
first, so = that the global error doesn't end up being used:
https://github.com/openssl/openssl/commit/17674bfdf75bffa4e225f= 8328b9d42cb74504005

This was backported = from master back to 1.0.1, but not to 1.0.0 or 0.9.8.

So that change (accidentally) "solved" the problem. As you = can see,
it's still possible to hit the reversed order in = the
!defined(OPENSSL_BN_ASM_MONT) case. That's easily = tested by building
OpenSSL with the -no-asm config option. = It's probably also the case
for obscure architectures and = OSs, but I haven't looked deeply into
that. In that case, = it's reasonable to assume that this crash would
likely = occur on such platforms.

Also, OSX, even = the latest version (10.10 as of now), still ships with
OpenSSL 0.9.8. Which is how Tamas ran into it.

Since Bitcoin Core and libbitcoinconsensus are = switching away from
OpenSSL for verification in the near = future, I don't think this is
much of an issue. Especially = since the problem manifests as a
controlled assertion = failure/abort. However, I've prepared a patch for
anyone = who may run into the issue in the short-term:
https://github.com/theuni/bitcoin/commit/adf0a691ee1c2f02e26828= f976cfe5b78896b507

I'll open a pull-request = for Bitcoin Core to discuss whether it's
worth merging or = not.

Regards,
Cory

On Fri, Aug 14, 2015 at 5:10 PM, Cory Fields = <lists@coryfields.com> wrote:
Ugh, what an unfortunate oversight!

The good news is that this issue should be = solved in future versions
when we switch to the new = libsecp256k1 lib for validation.

For now, = I've thrown together a quick hack to allow a user-specifiable
callback for libbitcoinconsensus. I think it's not worth = messing with
the official API since it will be fixed soon, = but rather hacked in as
a temporary work-around as needed. = It _should_ be documented as an
issue with the current = version, though.

Please see here for a = work-around to try:
https://github.com/theuni/bitcoin/commits/openssl-consensus-thr= eads
Unfortunately it's not pretty, but it works fine = here. Note that you
should give this some _serious_ = testing before deploying in any real
way. It should mimic = the way we do it in Core, though.

That's on = top of current master, but it should be trivial to apply to
release tags.

Please let me know = how it works out.

Regards,
Cory

On Fri, Aug 14, 2015 at = 12:37 PM, Tamas Blummer via bitcoin-dev
<bitcoin-dev@lists.linuxfoundation.org> wrote:
We integrated = libconsensus into bits of proof. It works well, in-line for all test = cases with our Java engine and is about 50% faster on a single = thread.

The performance advantage = unfortunatelly reverses if libconsensus is executed on several threads = simultaneously as we do with the Java engine, since an error:

=        Assertion failed: (pkey !=3D = NULL), function CECKey, file ecwrapper.cpp, line 96.

arises under that stress.

I = guess that the cause is that thread callbacks as advised for OpenSSL on = https://www.openssl.org/docs/crypto/threads.html are not registered.
Registering those however would require access to OpenSSL = functions, not exported from the lib.

I=E2=80= =99d be thankful for a pointer to a workaround.

Tamas Blummer

_______________________________________________
bitcoin-dev mailing list
bitcoin-dev@lists.linuxfoundation.org
https://lists.linuxfoundation.org/mailman/listinfo/bitcoin-dev<= br class=3D"">


= --Apple-Mail=_02C4D528-0B90-446A-B445-C69F5D37CC35-- --Apple-Mail=_ABE2F079-7D85-4296-8776-AF2355709186 Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename=signature.asc Content-Type: application/pgp-signature; name=signature.asc Content-Description: Message signed with OpenPGP using GPGMail -----BEGIN PGP SIGNATURE----- Comment: GPGTools - https://gpgtools.org iQEcBAEBCgAGBQJV0wmZAAoJEPZykcUXcTkcJaMH/iHf6zl+V7YVXtd5Ouvl7/pS YSsFM36QaI/fbsOauAtXoy20OSF27r+WSx2lXTqCR5AMsx/JNgb+zmkmidYD1rGO X3w2/5+9TXyAPr3PfZ/TFqPl5ns0h/a/Aaoq7BkFiHVz95ebv2QXd5/MUonBnq2H xc+FvBUXm3j1sdTEFyTFfECuU7JEkPc9ctQmveC9YA9tLcQgx/honLXMsmU+IKHT h0cBcNdEAkcnqMkJmdC8V+MAMcgnRjicSTtp7kBmkIW7NpwCuo43q42pAUkrz1kq ASJyiAG+8W5gbBlQeArNRXQQe0Ca71lWW9KF2eqQnyFgJ0T+pG5bhdy+c2qnZsE= =Lxv0 -----END PGP SIGNATURE----- --Apple-Mail=_ABE2F079-7D85-4296-8776-AF2355709186--