From 777ab85d30297cabde56cc7eb544e65ddcda5fc7 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Mon, 18 Nov 2024 17:38:27 -0500 Subject: [PATCH] Consistently connect to Unix sockets - Always use net.ResolveUnixAddr rather than constructing net.UnixAddr directly. - Use net.Dialer.DialContext in usernet. - Avoid type assertion on darwin. Signed-off-by: Tamir Duberstein --- pkg/networks/usernet/client.go | 5 +++-- pkg/qemu/qemu_driver.go | 6 +++--- pkg/vz/network_darwin.go | 8 ++++++-- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/pkg/networks/usernet/client.go b/pkg/networks/usernet/client.go index 1d6b4bf90d6..94e372ab72b 100644 --- a/pkg/networks/usernet/client.go +++ b/pkg/networks/usernet/client.go @@ -139,8 +139,9 @@ func NewClient(endpointSock string, subnet net.IP) *Client { func create(sock string, subnet net.IP, base string) *Client { client := &http.Client{ Transport: &http.Transport{ - DialContext: func(context.Context, string, string) (net.Conn, error) { - return net.Dial("unix", sock) + DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) { + var d net.Dialer + return d.DialContext(ctx, "unix", sock) }, }, } diff --git a/pkg/qemu/qemu_driver.go b/pkg/qemu/qemu_driver.go index 84561306621..5593695665f 100644 --- a/pkg/qemu/qemu_driver.go +++ b/pkg/qemu/qemu_driver.go @@ -417,9 +417,9 @@ func (a *qArgTemplateApplier) applyTemplate(qArg string) (string, error) { if !ok { return "", fmt.Errorf("non-string argument %+v", v) } - addr := &net.UnixAddr{ - Net: "unix", - Name: s, + addr, err := net.ResolveUnixAddr("unix", s) + if err != nil { + return "", err } conn, err := net.DialUnix("unix", nil, addr) if err != nil { diff --git a/pkg/vz/network_darwin.go b/pkg/vz/network_darwin.go index d7c8fbfb845..cbd506a4fc3 100644 --- a/pkg/vz/network_darwin.go +++ b/pkg/vz/network_darwin.go @@ -18,7 +18,11 @@ import ( ) func PassFDToUnix(unixSock string) (*os.File, error) { - unixConn, err := net.Dial("unix", unixSock) + unixAddr, err := net.ResolveUnixAddr("unix", unixSock) + if err != nil { + return nil, err + } + unixConn, err := net.DialUnix("unix", nil, unixAddr) if err != nil { return nil, err } @@ -27,7 +31,7 @@ func PassFDToUnix(unixSock string) (*os.File, error) { if err != nil { return nil, err } - err = fd.Put(unixConn.(*net.UnixConn), server) + err = fd.Put(unixConn, server) if err != nil { return nil, err }