From 7f27901cb9636a61b9b0b1bab2679b2a9a12eb38 Mon Sep 17 00:00:00 2001 From: Dave Cheney Date: Thu, 30 Jul 2015 17:02:59 +1000 Subject: [PATCH] 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 --- ssh/handshake.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/ssh/handshake.go b/ssh/handshake.go index a1e2c23..4acc1a0 100644 --- a/ssh/handshake.go +++ b/ssh/handshake.go @@ -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 {