hashicorp / mdns

Simple mDNS client/server library in Golang
MIT License
1.19k stars 210 forks source link

Prevent a nil pointer dereference segfault #118

Closed boydjeff closed 6 months ago

boydjeff commented 8 months ago

A nil pointer dereference segfault may occur when [ipv4, ipv6].NewPacketConn() is called with an argument that is nil. The four arguments to the four calls to NewPacketConn() may be nil if the functions that set them, inside the newClient() function, return a non-nil error.

In newClient() there are two calls to net.ListenUDP that set uconn4 and uconn6, and two calls to net.ListenMulticastUDP that set mconn4 and mconn6. There are network scenarios where multicast may be allowed for IPv4 addresses, but not IPv6 addresses, for example. In this case the check in newClient() that returns an error, (if mconn4 == nil && mconn6 == nil), is not hit because only mconn6 is nil. Then, ipv6MulticastConn is set to nil in the returned client object. This nil value for ipv6MulticastConn is then used as an argument to ipv6.NewPacketConn in setInterface() and a nil pointer dereference segfault occurs.

This PR prevents a segfault by only calling NewPacketConn if the argument to it is not nil.

boydjeff commented 8 months ago

See https://github.com/hashicorp/mdns/issues/119 for an alternate solution.

boydjeff commented 6 months ago

This PR has been superseded by https://github.com/hashicorp/mdns/pull/120.