]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: check RemoteAddr isn't nil before dereferencing
authorAl Cutter <al@google.com>
Thu, 15 Jun 2023 15:23:12 +0000 (15:23 +0000)
committerGopher Robot <gobot@golang.org>
Fri, 16 Jun 2023 18:24:26 +0000 (18:24 +0000)
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: ff3505d1d0b00ca16c68ec2a05f542978b79b170
GitHub-Pull-Request: golang/go#60823
Reviewed-on: https://go-review.googlesource.com/c/go/+/503656
Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org>
Reviewed-by: Damien Neil <dneil@google.com>
Run-TryBot: Damien Neil <dneil@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/net/http/server.go

index 680c5f68f425fa2a4e01af43d5180e3e3aa55389..8f63a9029931b1299d6380f0aa59309fd2c00677 100644 (file)
@@ -1856,7 +1856,9 @@ func isCommonNetReadError(err error) bool {
 
 // Serve a new connection.
 func (c *conn) serve(ctx context.Context) {
-       c.remoteAddr = c.rwc.RemoteAddr().String()
+       if ra := c.rwc.RemoteAddr(); ra != nil {
+               c.remoteAddr = ra.String()
+       }
        ctx = context.WithValue(ctx, LocalAddrContextKey, c.rwc.LocalAddr())
        var inFlightResponse *response
        defer func() {