ssh: fix deadlock during error path

Fixes golang/go#11882

If an error occurs during handshakeTransport.writePacket the lock may not be
released. Fix this by using defer rather than manually unlocking in all paths.

Change-Id: I0010284b4f7d99907c86b4c0e140ab6cf37b0441
Reviewed-on: https://go-review.googlesource.com/12888
Reviewed-by: Adam Langley <agl@golang.org>
This commit is contained in:
Dave Cheney
2015-07-30 21:10:58 +00:00
committed by Adam Langley
parent fcdb74e78f
commit 7f27901cb9
+5 -6
View File
@@ -253,6 +253,8 @@ func (t *handshakeTransport) sendKexInitLocked() (*kexInitMsg, []byte, error) {
func (t *handshakeTransport) writePacket(p []byte) error {
t.mu.Lock()
defer t.mu.Unlock()
if t.writtenSinceKex > t.config.RekeyThreshold {
t.sendKexInitLocked()
}
@@ -264,17 +266,14 @@ func (t *handshakeTransport) writePacket(p []byte) error {
}
t.writtenSinceKex += uint64(len(p))
var err error
switch p[0] {
case msgKexInit:
err = errors.New("ssh: only handshakeTransport can send kexInit")
return errors.New("ssh: only handshakeTransport can send kexInit")
case msgNewKeys:
err = errors.New("ssh: only handshakeTransport can send newKeys")
return errors.New("ssh: only handshakeTransport can send newKeys")
default:
err = t.conn.writePacket(p)
return t.conn.writePacket(p)
}
t.mu.Unlock()
return err
}
func (t *handshakeTransport) Close() error {