From f30598dd71b5bb7109eda56b02ea10d2b6cc1362 Mon Sep 17 00:00:00 2001 From: Joonas Kuorilehto Date: Sat, 10 Sep 2016 22:07:33 +0300 Subject: [PATCH] crypto/tls: Add mutex to protect KeyLogWriter Concurrent use of tls.Config is allowed, and may lead to KeyLogWriter being written to concurrently. Without a mutex to protect it, corrupted output may occur. A mutex is added for correctness. The mutex is made global to save size of the config struct as KeyLogWriter is rarely enabled. Related to #13057. Change-Id: I5ee55b6d8b43a191ec21f06e2aaae5002a71daef Reviewed-on: https://go-review.googlesource.com/29016 Reviewed-by: Brad Fitzpatrick Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- src/crypto/tls/common.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/crypto/tls/common.go b/src/crypto/tls/common.go index 46bc2aa03a..28b3d4c6ce 100644 --- a/src/crypto/tls/common.go +++ b/src/crypto/tls/common.go @@ -643,10 +643,16 @@ func (c *Config) writeKeyLog(clientRandom, masterSecret []byte) error { if c.KeyLogWriter == nil { return nil } + writerMutex.Lock() _, err := fmt.Fprintf(c.KeyLogWriter, "CLIENT_RANDOM %x %x\n", clientRandom, masterSecret) + writerMutex.Unlock() return err } +// writerMutex protects all KeyLogWriters globally. It is rarely enabled, +// and is only for debugging, so a global mutex saves space. +var writerMutex sync.Mutex + // A Certificate is a chain of one or more certificates, leaf first. type Certificate struct { Certificate [][]byte