From 7d2161f807773f3ef00a5b6b91d3e6ff2125116c Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Mon, 28 Jul 2025 16:55:52 -0700 Subject: [PATCH 1/3] setupIO: simplify getting net.UnixConn The typecast can't fail, so it doesn't make sense checking for errors here. Signed-off-by: Kir Kolyshkin --- utils_linux.go | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/utils_linux.go b/utils_linux.go index 9c9e1e83b..557aeee5b 100644 --- a/utils_linux.go +++ b/utils_linux.go @@ -121,12 +121,8 @@ func setupIO(process *libcontainer.Process, container *libcontainer.Container, c if err != nil { return nil, err } - uc, ok := conn.(*net.UnixConn) - if !ok { - return nil, errors.New("casting to UnixConn failed") - } - t.postStart = append(t.postStart, uc) - socket, err := uc.File() + t.postStart = append(t.postStart, conn) + socket, err := conn.(*net.UnixConn).File() if err != nil { return nil, err } @@ -432,13 +428,7 @@ func setupPidfdSocket(process *libcontainer.Process, sockpath string) (_clean fu return nil, fmt.Errorf("failed to dail %s: %w", sockpath, err) } - uc, ok := conn.(*net.UnixConn) - if !ok { - conn.Close() - return nil, errors.New("failed to cast to UnixConn") - } - - socket, err := uc.File() + socket, err := conn.(*net.UnixConn).File() if err != nil { conn.Close() return nil, fmt.Errorf("failed to dup socket: %w", err) From 87b8f974c8cfac25a9d4f8dd5fdef63f050a965b Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Mon, 28 Jul 2025 17:05:49 -0700 Subject: [PATCH 2/3] setupIO: close conn on error While it does not make much sense practically, as runc is going to exit soon and all fds will be closed anyway, various linters (including SVACE) keep reporting this. Let's make them happy. Reported-by: Tigran Sogomonian Reported-by: Mikhail Dmitrichenko Signed-off-by: Kir Kolyshkin --- utils_linux.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/utils_linux.go b/utils_linux.go index 557aeee5b..bd45bf059 100644 --- a/utils_linux.go +++ b/utils_linux.go @@ -95,7 +95,7 @@ func newProcess(p *specs.Process) (*libcontainer.Process, error) { } // setupIO modifies the given process config according to the options. -func setupIO(process *libcontainer.Process, container *libcontainer.Container, createTTY, detach bool, sockpath string) (*tty, error) { +func setupIO(process *libcontainer.Process, container *libcontainer.Container, createTTY, detach bool, sockpath string) (_ *tty, Err error) { if createTTY { process.Stdin = nil process.Stdout = nil @@ -121,6 +121,11 @@ func setupIO(process *libcontainer.Process, container *libcontainer.Container, c if err != nil { return nil, err } + defer func() { + if Err != nil { + conn.Close() + } + }() t.postStart = append(t.postStart, conn) socket, err := conn.(*net.UnixConn).File() if err != nil { From 314dd812f520b5be5683f6282fb9d67161bc7f40 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Mon, 28 Jul 2025 16:57:03 -0700 Subject: [PATCH 3/3] tests/cmd: simplify getting net.UnixConn The typecast can't fail, so it doesn't make sense checking for errors here. Signed-off-by: Kir Kolyshkin --- tests/cmd/pidfd-kill/pidfd-kill.go | 7 +------ tests/cmd/recvtty/recvtty.go | 16 ++-------------- 2 files changed, 3 insertions(+), 20 deletions(-) diff --git a/tests/cmd/pidfd-kill/pidfd-kill.go b/tests/cmd/pidfd-kill/pidfd-kill.go index ea9a9df8d..764fa883a 100644 --- a/tests/cmd/pidfd-kill/pidfd-kill.go +++ b/tests/cmd/pidfd-kill/pidfd-kill.go @@ -99,12 +99,7 @@ func recvPidfd(socketFile string) (*os.File, error) { } defer conn.Close() - unixconn, ok := conn.(*net.UnixConn) - if !ok { - return nil, errors.New("failed to cast to unixconn") - } - - socket, err := unixconn.File() + socket, err := conn.(*net.UnixConn).File() if err != nil { return nil, err } diff --git a/tests/cmd/recvtty/recvtty.go b/tests/cmd/recvtty/recvtty.go index 2c82665ab..5e5720cc5 100644 --- a/tests/cmd/recvtty/recvtty.go +++ b/tests/cmd/recvtty/recvtty.go @@ -85,13 +85,7 @@ func handleSingle(path string, noStdin bool) error { // Close ln, to allow for other instances to take over. ln.Close() - // Get the fd of the connection. - unixconn, ok := conn.(*net.UnixConn) - if !ok { - return errors.New("failed to cast to unixconn") - } - - socket, err := unixconn.File() + socket, err := conn.(*net.UnixConn).File() if err != nil { return err } @@ -158,13 +152,7 @@ func handleNull(path string) error { // Don't leave references lying around. defer conn.Close() - // Get the fd of the connection. - unixconn, ok := conn.(*net.UnixConn) - if !ok { - return - } - - socket, err := unixconn.File() + socket, err := conn.(*net.UnixConn).File() if err != nil { return }