Skip to content

Commit 3c8b7a9

Browse files
AlCuttergopherbot
authored andcommitted
net/http: check RemoteAddr isn't nil before dereferencing
RemoteAddr can return nil in some cases, this fix prevents a panic. I chatted with @neild about this beforehand, but what's happening in our case is that a connection comes in to the HTTP server which is then immediately closed (we discovered this issue by accident using nmap). The network implementation that we're using (it happens to be gVisor via its gonet adaptor) is returning nil from RemoteAddr(), presumably as there is no remote at that point. But, ultimately, since RemoteAddr returns an interface it is always possible for it to return nil, and indeed conn.RemoteAddr in this file does exactly that if the conn is not ok. Change-Id: Ibe67ae6e30b68e2776df5ee2911bf5f1dc539641 GitHub-Last-Rev: ff3505d GitHub-Pull-Request: #60823 Reviewed-on: https://go-review.googlesource.com/c/go/+/503656 Auto-Submit: Dmitri Shuralyov <[email protected]> Reviewed-by: Damien Neil <[email protected]> Run-TryBot: Damien Neil <[email protected]> Reviewed-by: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 548790e commit 3c8b7a9

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

src/net/http/server.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1856,7 +1856,9 @@ func isCommonNetReadError(err error) bool {
18561856

18571857
// Serve a new connection.
18581858
func (c *conn) serve(ctx context.Context) {
1859-
c.remoteAddr = c.rwc.RemoteAddr().String()
1859+
if ra := c.rwc.RemoteAddr(); ra != nil {
1860+
c.remoteAddr = ra.String()
1861+
}
18601862
ctx = context.WithValue(ctx, LocalAddrContextKey, c.rwc.LocalAddr())
18611863
var inFlightResponse *response
18621864
defer func() {

0 commit comments

Comments
 (0)