Skip to content

Commit c78caca

Browse files
committed
ssh: reject RekeyThresholds over MaxInt64
This fixes weirdness when users use int64(-1) as sentinel value. Also, really use cipher specific default thresholds. These were added in a59c127, but weren't taking effect. Add a test. Fixes golang/go#19639 Change-Id: Ie9518a0ff12fded2fca35465abb427d7a9f84340 Reviewed-on: https://go-review.googlesource.com/39431 Run-TryBot: Han-Wen Nienhuys <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 573951c commit c78caca

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

ssh/common.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"crypto/rand"
1010
"fmt"
1111
"io"
12+
"math"
1213
"sync"
1314

1415
_ "crypto/sha1"
@@ -186,7 +187,7 @@ type Config struct {
186187

187188
// The maximum number of bytes sent or received after which a
188189
// new key is negotiated. It must be at least 256. If
189-
// unspecified, 1 gigabyte is used.
190+
// unspecified, a size suitable for the chosen cipher is used.
190191
RekeyThreshold uint64
191192

192193
// The allowed key exchanges algorithms. If unspecified then a
@@ -230,11 +231,12 @@ func (c *Config) SetDefaults() {
230231
}
231232

232233
if c.RekeyThreshold == 0 {
233-
// RFC 4253, section 9 suggests rekeying after 1G.
234-
c.RekeyThreshold = 1 << 30
235-
}
236-
if c.RekeyThreshold < minRekeyThreshold {
234+
// cipher specific default
235+
} else if c.RekeyThreshold < minRekeyThreshold {
237236
c.RekeyThreshold = minRekeyThreshold
237+
} else if c.RekeyThreshold >= math.MaxInt64 {
238+
// Avoid weirdness if somebody uses -1 as a threshold.
239+
c.RekeyThreshold = math.MaxInt64
238240
}
239241
}
240242

ssh/handshake_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,3 +526,31 @@ func TestDisconnect(t *testing.T) {
526526
t.Errorf("readPacket 3 succeeded")
527527
}
528528
}
529+
530+
func TestHandshakeRekeyDefault(t *testing.T) {
531+
clientConf := &ClientConfig{
532+
Config: Config{
533+
Ciphers: []string{"aes128-ctr"},
534+
},
535+
HostKeyCallback: InsecureIgnoreHostKey(),
536+
}
537+
trC, trS, err := handshakePair(clientConf, "addr", false)
538+
if err != nil {
539+
t.Fatalf("handshakePair: %v", err)
540+
}
541+
defer trC.Close()
542+
defer trS.Close()
543+
544+
trC.writePacket([]byte{msgRequestSuccess, 0, 0})
545+
trC.Close()
546+
547+
rgb := (1024 + trC.readBytesLeft) >> 30
548+
wgb := (1024 + trC.writeBytesLeft) >> 30
549+
550+
if rgb != 64 {
551+
t.Errorf("got rekey after %dG read, want 64G", rgb)
552+
}
553+
if wgb != 64 {
554+
t.Errorf("got rekey after %dG write, want 64G", wgb)
555+
}
556+
}

0 commit comments

Comments
 (0)