From 6c1c0d32ea98694b8bc973507d2ed9f172beb9cb Mon Sep 17 00:00:00 2001 From: Samuel K Date: Wed, 4 Nov 2026 21:05:48 -0600 Subject: [PATCH 1/2] fix(cli): mark inherited file descriptors close-on-exec at startup --- cmd/root.go | 2 + pkg/command/inherited_fds_linux.go | 32 ++++++++++++++ pkg/command/inherited_fds_linux_test.go | 57 +++++++++++++++++++++++++ pkg/command/inherited_fds_other.go | 5 +++ 4 files changed, 96 insertions(+) create mode 100644 pkg/command/inherited_fds_linux.go create mode 100644 pkg/command/inherited_fds_linux_test.go create mode 100644 pkg/command/inherited_fds_other.go diff --git a/cmd/root.go b/cmd/root.go index dab59e846..47ef5805b 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -31,6 +31,7 @@ import ( wsCmdPkg "github.com/devsy-org/devsy/cmd/workspace" "github.com/devsy-org/devsy/pkg/clierr" "github.com/devsy-org/devsy/pkg/clihelp" + "github.com/devsy-org/devsy/pkg/command" "github.com/devsy-org/devsy/pkg/config" devcconfig "github.com/devsy-org/devsy/pkg/devcontainer/config" "github.com/devsy-org/devsy/pkg/exitcode" @@ -144,6 +145,7 @@ func isMachineConsumer(logOutput string, isInternal bool) bool { } func Execute() { + command.MarkInheritedFDsCloseOnExec() os.Exit(run()) } diff --git a/pkg/command/inherited_fds_linux.go b/pkg/command/inherited_fds_linux.go new file mode 100644 index 000000000..3abb4718d --- /dev/null +++ b/pkg/command/inherited_fds_linux.go @@ -0,0 +1,32 @@ +package command + +import ( + "math" + "os" + "strconv" + + "golang.org/x/sys/unix" +) + +const firstInheritedFD = 3 + +func MarkInheritedFDsCloseOnExec() { + err := unix.CloseRange(firstInheritedFD, math.MaxUint32, unix.CLOSE_RANGE_CLOEXEC) + if err != nil { + markProcFDsCloseOnExec() + } +} + +func markProcFDsCloseOnExec() { + entries, err := os.ReadDir("/proc/self/fd") + if err != nil { + return + } + for _, entry := range entries { + fd, err := strconv.Atoi(entry.Name()) + if err != nil || fd < firstInheritedFD { + continue + } + unix.CloseOnExec(fd) + } +} diff --git a/pkg/command/inherited_fds_linux_test.go b/pkg/command/inherited_fds_linux_test.go new file mode 100644 index 000000000..7273a44a1 --- /dev/null +++ b/pkg/command/inherited_fds_linux_test.go @@ -0,0 +1,57 @@ +package command + +import ( + "os" + "os/exec" + "testing" + + "github.com/stretchr/testify/require" + "golang.org/x/sys/unix" +) + +const inheritedFDsHelperEnv = "DEVSY_TEST_INHERITED_FDS_MODE" + +func TestMarkInheritedFDsCloseOnExec(t *testing.T) { + for _, mode := range []string{"close_range", "proc_fallback"} { + t.Run(mode, func(t *testing.T) { + leaked, err := os.Open(os.DevNull) + require.NoError(t, err) + t.Cleanup(func() { _ = leaked.Close() }) + + child := exec.Command( + os.Args[0], + "-test.run=^TestInheritedFDsHelper$", + ) // #nosec G204 G702 -- re-exec this test binary + child.Env = append(os.Environ(), inheritedFDsHelperEnv+"="+mode) + child.ExtraFiles = []*os.File{leaked} + child.Stderr = os.Stderr + output, err := child.Output() + require.NoError(t, err, string(output)) + }) + } +} + +func TestInheritedFDsHelper(t *testing.T) { + mode := os.Getenv(inheritedFDsHelperEnv) + if mode == "" { + return + } + const leakedFD = 3 + + flags, err := unix.FcntlInt(leakedFD, unix.F_GETFD, 0) + require.NoError(t, err) + require.Zero(t, flags&unix.FD_CLOEXEC) + + if mode == "proc_fallback" { + markProcFDsCloseOnExec() + } else { + MarkInheritedFDsCloseOnExec() + } + + flags, err = unix.FcntlInt(leakedFD, unix.F_GETFD, 0) + require.NoError(t, err) + require.NotZero(t, flags&unix.FD_CLOEXEC) + + grandchild := exec.Command("sh", "-c", "test ! -e /proc/$$/fd/3") + require.NoError(t, grandchild.Run()) +} diff --git a/pkg/command/inherited_fds_other.go b/pkg/command/inherited_fds_other.go new file mode 100644 index 000000000..b92f58682 --- /dev/null +++ b/pkg/command/inherited_fds_other.go @@ -0,0 +1,5 @@ +//go:build !linux + +package command + +func MarkInheritedFDsCloseOnExec() {} From f1f3d336d2bcc05c96f3e9c4f9a5b0783d3aef48 Mon Sep 17 00:00:00 2001 From: Samuel K Date: Wed, 4 Nov 2026 23:48:51 -0600 Subject: [PATCH 2/2] test(cli): cover close_range failure fallback --- pkg/command/inherited_fds_linux.go | 4 +++- pkg/command/inherited_fds_linux_test.go | 10 +++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/pkg/command/inherited_fds_linux.go b/pkg/command/inherited_fds_linux.go index 3abb4718d..f56813296 100644 --- a/pkg/command/inherited_fds_linux.go +++ b/pkg/command/inherited_fds_linux.go @@ -10,8 +10,10 @@ import ( const firstInheritedFD = 3 +var closeRange = unix.CloseRange + func MarkInheritedFDsCloseOnExec() { - err := unix.CloseRange(firstInheritedFD, math.MaxUint32, unix.CLOSE_RANGE_CLOEXEC) + err := closeRange(firstInheritedFD, math.MaxUint32, unix.CLOSE_RANGE_CLOEXEC) if err != nil { markProcFDsCloseOnExec() } diff --git a/pkg/command/inherited_fds_linux_test.go b/pkg/command/inherited_fds_linux_test.go index 7273a44a1..b8e075ca6 100644 --- a/pkg/command/inherited_fds_linux_test.go +++ b/pkg/command/inherited_fds_linux_test.go @@ -12,7 +12,7 @@ import ( const inheritedFDsHelperEnv = "DEVSY_TEST_INHERITED_FDS_MODE" func TestMarkInheritedFDsCloseOnExec(t *testing.T) { - for _, mode := range []string{"close_range", "proc_fallback"} { + for _, mode := range []string{"close_range", "proc_fallback", "close_range_fails"} { t.Run(mode, func(t *testing.T) { leaked, err := os.Open(os.DevNull) require.NoError(t, err) @@ -42,9 +42,13 @@ func TestInheritedFDsHelper(t *testing.T) { require.NoError(t, err) require.Zero(t, flags&unix.FD_CLOEXEC) - if mode == "proc_fallback" { + switch mode { + case "proc_fallback": markProcFDsCloseOnExec() - } else { + case "close_range_fails": + closeRange = func(uint, uint, uint) error { return unix.EPERM } + MarkInheritedFDsCloseOnExec() + default: MarkInheritedFDsCloseOnExec() }