all: fix resource leaks with missing .Close() calls

Fixes #5706

Signed-off-by: Emmanuel T Odeke <emmanuel@orijtech.com>
pull/5779/head
Emmanuel T Odeke 2 years ago committed by Brad Fitzpatrick
parent 9bdf0cd8cd
commit f981b1d9da

@ -24,11 +24,17 @@ func New(socket string) (*BIRDClient, error) {
return newWithTimeout(socket, responseTimeout) return newWithTimeout(socket, responseTimeout)
} }
func newWithTimeout(socket string, timeout time.Duration) (*BIRDClient, error) { func newWithTimeout(socket string, timeout time.Duration) (_ *BIRDClient, err error) {
conn, err := net.Dial("unix", socket) conn, err := net.Dial("unix", socket)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to connect to BIRD: %w", err) return nil, fmt.Errorf("failed to connect to BIRD: %w", err)
} }
defer func() {
if err != nil {
conn.Close()
}
}()
b := &BIRDClient{ b := &BIRDClient{
socket: socket, socket: socket,
conn: conn, conn: conn,

@ -48,7 +48,7 @@ func runConfigureHost(ctx context.Context, args []string) error {
if uid := os.Getuid(); uid != 0 { if uid := os.Getuid(); uid != 0 {
return fmt.Errorf("must be run as root, not %q (%v)", os.Getenv("USER"), uid) return fmt.Errorf("must be run as root, not %q (%v)", os.Getenv("USER"), uid)
} }
hi:= hostinfo.New() hi := hostinfo.New()
isDSM6 := strings.HasPrefix(hi.DistroVersion, "6.") isDSM6 := strings.HasPrefix(hi.DistroVersion, "6.")
isDSM7 := strings.HasPrefix(hi.DistroVersion, "7.") isDSM7 := strings.HasPrefix(hi.DistroVersion, "7.")
if !isDSM6 && !isDSM7 { if !isDSM6 && !isDSM7 {

@ -1188,6 +1188,8 @@ func (c *Client) measureHTTPSLatency(ctx context.Context, reg *tailcfg.DERPRegio
var ip netip.Addr var ip netip.Addr
dc := derphttp.NewNetcheckClient(c.logf) dc := derphttp.NewNetcheckClient(c.logf)
defer dc.Close()
tlsConn, tcpConn, node, err := dc.DialRegionTLS(ctx, reg) tlsConn, tcpConn, node, err := dc.DialRegionTLS(ctx, reg)
if err != nil { if err != nil {
return 0, ip, err return 0, ip, err

@ -61,7 +61,7 @@ func (l *Listener) Accept() (net.Conn, error) {
// The provided Context must be non-nil. If the context expires before the // The provided Context must be non-nil. If the context expires before the
// connection is complete, an error is returned. Once successfully connected // connection is complete, an error is returned. Once successfully connected
// any expiration of the context will not affect the connection. // any expiration of the context will not affect the connection.
func (l *Listener) Dial(ctx context.Context, network, addr string) (net.Conn, error) { func (l *Listener) Dial(ctx context.Context, network, addr string) (_ net.Conn, err error) {
if !strings.HasSuffix(network, "tcp") { if !strings.HasSuffix(network, "tcp") {
return nil, net.UnknownNetworkError(network) return nil, net.UnknownNetworkError(network)
} }
@ -72,6 +72,13 @@ func (l *Listener) Dial(ctx context.Context, network, addr string) (net.Conn, er
} }
} }
c, s := NewConn(addr, bufferSize) c, s := NewConn(addr, bufferSize)
defer func() {
if err != nil {
c.Close()
s.Close()
}
}()
select { select {
case <-ctx.Done(): case <-ctx.Done():
return nil, ctx.Err() return nil, ctx.Err()

@ -987,16 +987,19 @@ func (ns *Impl) acceptUDP(r *udp.ForwarderRequest) {
} }
dstAddr, ok := ipPortOfNetstackAddr(sess.LocalAddress, sess.LocalPort) dstAddr, ok := ipPortOfNetstackAddr(sess.LocalAddress, sess.LocalPort)
if !ok { if !ok {
ep.Close()
return return
} }
srcAddr, ok := ipPortOfNetstackAddr(sess.RemoteAddress, sess.RemotePort) srcAddr, ok := ipPortOfNetstackAddr(sess.RemoteAddress, sess.RemotePort)
if !ok { if !ok {
ep.Close()
return return
} }
// Handle magicDNS traffic (via UDP) here. // Handle magicDNS traffic (via UDP) here.
if dst := dstAddr.Addr(); dst == magicDNSIP || dst == magicDNSIPv6 { if dst := dstAddr.Addr(); dst == magicDNSIP || dst == magicDNSIPv6 {
if dstAddr.Port() != 53 { if dstAddr.Port() != 53 {
ep.Close()
return // Only MagicDNS traffic runs on the service IPs for now. return // Only MagicDNS traffic runs on the service IPs for now.
} }

Loading…
Cancel
Save