sshd: Apply read deadline to handler

This commit is contained in:
Andrey Petrov 2020-01-06 20:04:17 -05:00
parent de89c87946
commit 61b525ae54
2 changed files with 7 additions and 5 deletions

2
go.mod
View File

@ -8,3 +8,5 @@ require (
golang.org/x/crypto v0.0.0-20190320223903-b7391e95e576 golang.org/x/crypto v0.0.0-20190320223903-b7391e95e576
golang.org/x/sys v0.0.0-20190321052220-f7bb7a8bee54 golang.org/x/sys v0.0.0-20190321052220-f7bb7a8bee54
) )
go 1.13

View File

@ -33,8 +33,11 @@ func (l *SSHListener) handleConn(conn net.Conn) (*Terminal, error) {
conn = ReadLimitConn(conn, l.RateLimit()) conn = ReadLimitConn(conn, l.RateLimit())
} }
// Handshake shouldn't take more than 10 seconds // If the connection doesn't write anything back for too long before we get
conn.SetReadDeadline(time.Now().Add(10 * time.Second)) // a valid session, it should be dropped.
var handleTimeout = 20 * time.Second
conn.SetReadDeadline(time.Now().Add(handleTimeout))
defer conn.SetReadDeadline(time.Time{})
// Upgrade TCP connection to SSH connection // Upgrade TCP connection to SSH connection
sshConn, channels, requests, err := ssh.NewServerConn(conn, l.config) sshConn, channels, requests, err := ssh.NewServerConn(conn, l.config)
@ -42,9 +45,6 @@ func (l *SSHListener) handleConn(conn net.Conn) (*Terminal, error) {
return nil, err return nil, err
} }
// clear the deadline
conn.SetDeadline(time.Time{})
// FIXME: Disconnect if too many faulty requests? (Avoid DoS.) // FIXME: Disconnect if too many faulty requests? (Avoid DoS.)
go ssh.DiscardRequests(requests) go ssh.DiscardRequests(requests)
return NewSession(sshConn, channels) return NewSession(sshConn, channels)