Repository: moby/moby (language: go)

Issue:
Invalid PTR query causes resolver panic
### Description

`dockerd` crashed with this trace: 

```
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x55a9fa3227df]

goroutine 306 [running]:
github.com/docker/docker/libnetwork.(*resolver).ServeDNS(0xc0009e7b20, {0x55a9fb8b90e0, 0xc000a0f300}, 0xc0012297a0)
        /go/src/github.com/docker/docker/libnetwork/resolver.go:399 +0x6ff
github.com/docker/docker/vendor/github.com/miekg/dns.(*Server).serveDNS(0xc000eea6c0, {0xc000b8a800, 0x31, 0x200}, 0xc000a0f300)
        /go/src/github.com/docker/docker/vendor/github.com/miekg/dns/server.go:651 +0x4e2
github.com/docker/docker/vendor/github.com/miekg/dns.(*Server).serveUDPPacket(0xc000eea6c0, 0xc000eea7e0?, {0xc000b8a800, 0x31, 0x200}, {0x55a9fb8b5af0?, 0xc000d6c9d8}, 0xc001232f60, {0x0, 0x0})
        /go/src/github.com/docker/docker/vendor/github.com/miekg/dns/server.go:591 +0x185
created by github.com/docker/docker/vendor/github.com/miekg/dns.(*Server).serveUDP
        /go/src/github.com/docker/docker/vendor/github.com/miekg/dns/server.go:521 +0x485
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x55a9fa3227df]

goroutine 271 [running]:
github.com/docker/docker/libnetwork.(*resolver).ServeDNS(0xc0009e7b20, {0x55a9fb8b90e0, 0xc0011bb400}, 0xc000fb2ab0)
        /go/src/github.com/docker/docker/libnetwork/resolver.go:399 +0x6ff
github.com/docker/docker/vendor/github.com/miekg/dns.(*Server).serveDNS(0xc000eea6c0, {0xc0006d2000, 0x35, 0x200}, 0xc0011bb400)
        /go/src/github.com/docker/docker/vendor/github.com/miekg/dns/server.go:651 +0x4e2
github.com/docker/docker/vendor/github.com/miekg/dns.(*Server).serveUDPPacket(0xc000eea6c0, 0x55a9fb88bbc0?, {0xc0006d2000, 0x35, 0x200}, {0x55a9fb8b5af0?, 0xc000d6c9d8}, 0xc001232f00, {0x0, 0x0})
        /go/src/github.com/docker/docker/vendor/github.com/miekg/dns/server.go:591 +0x185
created by github.com/docker/docker/vendor/github.com/miekg/dns.(*Server).serveUDP
        /go/src/github.com/docker/docker/vendor/github.com/miekg/dns/server.go:521 +0x485
```

### Reproduce

I'm not totally sure where these bad DNS queries originate from yet :sweat_smile:  but these PTR queries very consistently crash the docker daemon. They look something like this:
 ```
invalid PTR query, lb._dns-sd._udp.foo.er0k.net.
invalid PTR query, lb._dns-sd._udp.bar.er0k.net.
```

It looks like when the resolver encounters an error in `ServeDNS()`, it [tries to log the error](
https://github.com/moby/moby/blob/master/libnetwork/resolver.go#L397) along with the local address of the external connection, but `extConn` is `nil` 


### Expected behavior

dockerd should log the error and not crash

### docker version

```bash
Client: Docker Engine - Community
 Version:           23.0.1
 API version:       1.42
 Go version:        go1.19.5
 Git commit:        a5ee5b1
 Built:             Thu Feb  9 19:46:54 2023
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          23.0.1
  API version:      1.42 (minimum version 1.12)
  Go version:       go1.19.5
  Git commit:       bc3805a
  Built:            Thu Feb  9 19:46:54 2023
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.16
  GitCommit:        31aa4358a36870b21a992d3ad2bef29e1d693bec
 runc:
  Version:          1.1.4
  GitCommit:        v1.1.4-0-g5fd4c4d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
```


### docker info

```bash
Client:
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.10.2
    Path:     /usr/libexec/docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v2.16.0
    Path:     /usr/libexec/docker/cli-plugins/docker-compose
  scan: Docker Scan (Docker Inc.)
    Version:  v0.23.0
    Path:     /usr/libexec/docker/cli-plugins/docker-scan

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 1
 Server Version: 23.0.1
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Using metacopy: false
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: systemd
 Cgroup Version: 2
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 31aa4358a36870b21a992d3ad2bef29e1d693bec
 runc version: v1.1.4-0-g5fd4c4d
 init version: de40ad0
 Security Options:
  apparmor
  seccomp
   Profile: builtin
  cgroupns
 Kernel Version: 5.10.0-20-amd64
 Operating System: Debian GNU/Linux 11 (bullseye)
 OSType: linux
 Architecture: x86_64
 CPUs: 2
 Total Memory: 473.2MiB
 Name: bug
 ID: 12a513eb-f4d8-4ebf-a995-75d7ade340b5
 Docker Root Dir: /var/lib/docker
 Debug Mode: true
  File Descriptors: 22
  Goroutines: 32
  System Time: 2023-02-11T19:22:25.521287084Z
  EventsListeners: 0
 Registry: https://index.docker.io/v1/
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false
```


### Additional Info

_No response_


Discussion / hints:

Fix patch:
diff --git a/libnetwork/resolver.go b/libnetwork/resolver.go
index ff578356e5ef2..1c05afc6085a1 100644
--- a/libnetwork/resolver.go
+++ b/libnetwork/resolver.go
@@ -396,7 +396,7 @@ func (r *resolver) ServeDNS(w dns.ResponseWriter, query *dns.Msg) {
 	}
 
 	if err != nil {
-		logrus.WithError(err).Errorf("[resolver] failed to handle query: %s (%s) from %s", queryName, dns.TypeToString[queryType], extConn.LocalAddr().String())
+		logrus.WithError(err).Errorf("[resolver] failed to handle query: %s (%s)", queryName, dns.TypeToString[queryType])
 		return
 	}
 


Test patch: