go/src/crypto
Adam Langley 254169d7bb crypto/tls: fix deadlock when racing to complete handshake.
After renegotiation support was added (af125a5193) it's possible for a
Write to block on a Read when racing to complete the handshake:
   1. The Write determines that a handshake is needed and tries to
      take the neccesary locks in the correct order.
   2. The Read also determines that a handshake is needed and wins
      the race to take the locks.
   3. The Read goroutine completes the handshake and wins a race
      to unlock and relock c.in, which it'll hold when waiting for
      more network data.

If the application-level protocol requires the Write to complete before
data can be read then the system as a whole will deadlock.

Unfortunately it doesn't appear possible to reverse the locking order of
c.in and handshakeMutex because we might read a renegotiation request at
any point and need to be able to do a handshake without unlocking.

So this change adds a sync.Cond that indicates that a goroutine has
committed to doing a handshake. Other interested goroutines can wait on
that Cond when needed.

The test for this isn't great. I was able to reproduce the deadlock with
it only when building with -race. (Because -race happened to alter the
timing just enough.)

Fixes #17101.

Change-Id: I4e8757f7b82a84e46c9963a977d089f0fb675495
Reviewed-on: https://go-review.googlesource.com/29164
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-09-22 18:36:58 +00:00
..
aes crypto/cipher: enforce message size limits for GCM. 2016-09-02 16:23:15 +00:00
cipher crypto/cipher: enforce message size limits for GCM. 2016-09-02 16:23:15 +00:00
des all: remove unnecessary type conversions 2016-04-15 07:31:45 +00:00
dsa crypto/dsa: eliminate invalid PublicKey early 2016-04-05 17:41:47 +00:00
ecdsa math/big: add assembly implementation of arith for ppc64{le} 2016-08-29 21:03:21 +00:00
elliptic crypto/ecdsa: reject negative inputs. 2016-05-18 14:18:48 +00:00
hmac crypto/hmac: don't test for length equality in Equal. 2016-08-17 23:23:28 +00:00
md5 crypto/md5, crypto/sha1, crypto/sha256: add examples for checksumming a file 2016-09-19 17:29:19 +00:00
rand all: single space after period. 2016-03-02 00:13:47 +00:00
rc4 all: fix assembly vet issues 2016-08-25 18:52:31 +00:00
rsa crypto/rsa: clarify comment on maximum message length. 2016-09-22 03:06:25 +00:00
sha1 crypto/md5, crypto/sha1, crypto/sha256: add examples for checksumming a file 2016-09-19 17:29:19 +00:00
sha256 crypto/md5, crypto/sha1, crypto/sha256: add examples for checksumming a file 2016-09-19 17:29:19 +00:00
sha512 crypto/sha512: add s390x assembly implementation 2016-04-26 17:37:45 +00:00
subtle crypto/subtle: expand abbreviation to eliminate confusion 2016-06-09 15:30:48 +00:00
tls crypto/tls: fix deadlock when racing to complete handshake. 2016-09-22 18:36:58 +00:00
x509 crypto/x509: Fix bug in UnknownAuthorityError.Error 2016-08-31 00:09:58 +00:00
crypto.go crypto: document that Signer.Sign does not hash 2016-01-26 16:27:16 +00:00