Repository navigation
Let the connector run on macOS #812
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
76fe2eb
Let the connector run on macOS
robzolkos 6897300
On macOS, a peer that left before it was named is undelivered, not re…
robzolkos 653ee24
Address review on macOS support
robzolkos 2c13d3f
Address the second review on macOS support
robzolkos fe73f8f
Fail closed on macOS when /dev/fd can't be read
robzolkos 352932e
Name the token's taker before writing the token
robzolkos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| //go:build darwin | ||
|
|
||
| package cli | ||
|
|
||
| // sealInheritedDescriptors keeps every descriptor this process inherited out | ||
| // of the processes it starts, by marking each one close-on-exec. It is the | ||
| // Linux one's (inherited_fds_linux.go) for macOS, where there is no | ||
| // close_range: the process's own list of descriptors is /dev/fd, and each is | ||
| // marked as the Linux fallback marks /proc/self/fd's. | ||
| // | ||
| // /dev/fd is a filesystem macOS always mounts. Where it cannot be read this | ||
| // fails closed: no other source says which descriptors are open, and the | ||
| // descriptor limit is no bound, since a parent can lower it after opening one | ||
| // above it (Copilot on #812). | ||
| func sealInheritedDescriptors() error { | ||
| return sealListedDescriptors(devFD) | ||
| } | ||
|
|
||
| // devFD is where macOS lists the descriptors a process holds. | ||
| const devFD = "/dev/fd" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| //go:build darwin | ||
|
|
||
| package cli | ||
|
|
||
| import ( | ||
| "os" | ||
| "os/exec" | ||
| "strconv" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "golang.org/x/sys/unix" | ||
| ) | ||
|
|
||
| // The descriptor number is high on purpose: a child picks the lowest free one | ||
| // for its own files. | ||
| const inheritedFDDarwin = 200 | ||
|
|
||
| // inheritDarwin puts a readable pipe at fd, the way an exec'd child receives | ||
| // one: open, with no close-on-exec flag of its own. | ||
| func inheritDarwin(t *testing.T, fd int) { | ||
| t.Helper() | ||
| reader, writer, err := os.Pipe() | ||
| require.NoError(t, err) | ||
| _, err = writer.WriteString("a-task-token\n") | ||
| require.NoError(t, err) | ||
| require.NoError(t, writer.Close()) | ||
| t.Cleanup(func() { _ = reader.Close() }) | ||
|
|
||
| require.NoError(t, unix.Dup2(int(reader.Fd()), fd)) | ||
| t.Cleanup(func() { _ = unix.Close(fd) }) | ||
| flags, err := unix.FcntlInt(uintptr(fd), unix.F_GETFD, 0) | ||
| require.NoError(t, err) | ||
| require.Zero(t, flags&unix.FD_CLOEXEC, "an inherited descriptor arrives without it") | ||
| } | ||
|
|
||
| func cloexec(t *testing.T, fd int) bool { | ||
| t.Helper() | ||
| flags, err := unix.FcntlInt(uintptr(fd), unix.F_GETFD, 0) | ||
| require.NoError(t, err) | ||
| return flags&unix.FD_CLOEXEC != 0 | ||
| } | ||
|
|
||
| // On macOS an inherited descriptor does not reach the processes this one | ||
| // starts, and stays readable here. | ||
| func TestSealedDescriptorsDoNotReachChildrenOnMacOS(t *testing.T) { | ||
| inheritDarwin(t, inheritedFDDarwin) | ||
|
|
||
| require.NoError(t, sealInheritedDescriptors()) | ||
|
|
||
| assert.True(t, cloexec(t, inheritedFDDarwin)) | ||
| fd := strconv.Itoa(inheritedFDDarwin) | ||
| out, err := exec.CommandContext(t.Context(), "/bin/sh", "-c", | ||
| "if (: <&"+fd+") 2>/dev/null; then echo inherited; else echo sealed; fi").Output() //nolint:gosec // G204: the descriptor number is the test's own constant | ||
| require.NoError(t, err) | ||
| assert.Equal(t, "sealed\n", string(out), "the child inherited the descriptor") | ||
|
|
||
| buffer := make([]byte, len("a-task-token\n")) | ||
| read, err := unix.Read(inheritedFDDarwin, buffer) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, "a-task-token\n", string(buffer[:read]), "marked, not closed or consumed") | ||
| } | ||
|
|
||
| // /dev/fd is how macOS lists a process's descriptors, and the walk seals what | ||
| // it names. | ||
| func TestTheDevFDWalkSealsWhatItNames(t *testing.T) { | ||
| inheritDarwin(t, inheritedFDDarwin) | ||
| require.NoError(t, sealListedDescriptors(devFD)) | ||
| assert.True(t, cloexec(t, inheritedFDDarwin)) | ||
| } | ||
|
|
||
| // A listing that cannot be read is an error, never a quiet success. | ||
| func TestTheDevFDWalkRefusesAListingItCannotRead(t *testing.T) { | ||
| require.Error(t, sealListedDescriptors(t.TempDir()+"/missing")) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,10 @@ | ||
| //go:build !linux | ||
| //go:build !linux && !darwin | ||
|
|
||
| package cli | ||
|
|
||
| // sealInheritedDescriptors has nothing to seal where the handover it protects | ||
| // cannot happen: a task token arrives on an inherited descriptor only on | ||
| // Linux, which is what internal/commands/mcp_token_linux.go accepts and | ||
| // internal/commands/mcp_token_other.go refuses. Elsewhere this keeps the | ||
| // Linux and macOS, which is what internal/commands/mcp_token_sealed.go accepts | ||
| // and internal/commands/mcp_token_other.go refuses. Elsewhere this keeps the | ||
| // program building and starting the same way. | ||
| func sealInheritedDescriptors() error { return nil } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| //go:build linux || darwin | ||
|
|
||
| package cli | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "golang.org/x/sys/unix" | ||
|
|
||
| "github.com/basecamp/basecamp-cli/internal/sysfd" | ||
| ) | ||
|
|
||
| // sealListedDescriptors marks close-on-exec every descriptor listingDir names: | ||
| // /proc/self/fd on Linux, /dev/fd on macOS, each the process's own list of | ||
| // what it holds. | ||
| func sealListedDescriptors(listingDir string) error { | ||
| dir, err := os.Open(listingDir) | ||
| if err != nil { | ||
| return fmt.Errorf("could not open %s to seal the descriptors this process inherited: %w", listingDir, err) | ||
| } | ||
| defer func() { _ = dir.Close() }() | ||
| names, err := dir.Readdirnames(-1) | ||
| if err != nil { | ||
| return fmt.Errorf("could not read %s to seal the descriptors this process inherited: %w", listingDir, err) | ||
| } | ||
| listing, err := sysfd.Of(dir.Fd()) | ||
| if err != nil { | ||
| return fmt.Errorf("could not seal the descriptors this process inherited: %w", err) | ||
| } | ||
| for _, name := range names { | ||
| fd, err := sysfd.Parse(name) | ||
| if err != nil { | ||
| return fmt.Errorf("could not seal the descriptors this process inherited: %s holds %q, which is not a descriptor", listingDir, name) | ||
| } | ||
| if fd < sysfd.FirstNonStandard || fd == listing { | ||
| continue | ||
| } | ||
| if err := sealDescriptor(fd); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // sealDescriptor marks one descriptor close-on-exec. | ||
| // | ||
| // A descriptor that is no longer open is the one failure that is not a | ||
| // failure: the listing is a snapshot, and a number that has been closed since | ||
| // it was taken is already out of reach of every child. Anything else means | ||
| // the descriptor is open and still inheritable, which is the thing this | ||
| // refuses to let happen quietly. | ||
| func sealDescriptor(fd sysfd.Descriptor) error { | ||
| flags, err := unix.FcntlInt(fd.Uintptr(), unix.F_GETFD, 0) | ||
| if err != nil { | ||
| if errors.Is(err, unix.EBADF) { | ||
| return nil | ||
| } | ||
| return fmt.Errorf("could not read the flags of inherited descriptor %s: %w", fd, err) | ||
| } | ||
| if flags&unix.FD_CLOEXEC != 0 { | ||
| return nil | ||
| } | ||
| if _, err := unix.FcntlInt(fd.Uintptr(), unix.F_SETFD, flags|unix.FD_CLOEXEC); err != nil { | ||
| if errors.Is(err, unix.EBADF) { | ||
| return nil | ||
| } | ||
| return fmt.Errorf("could not keep inherited descriptor %s from the processes this one starts: %w", fd, err) | ||
| } | ||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.