Skip to content

fix(cli/ssh): prevent reads/writes to stdin/stdout in stdio mode #12045

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 4 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion cli/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ func (r *RootCmd) ssh() *clibase.Cmd {
}
}()

// In stdio mode, we can't allow any writes to stdin or stdout
// because they are used by the SSH protocol.
stdioReader, stdioWriter := inv.Stdin, inv.Stdout
if stdio {
inv.Stdin = stdioErrLogReader{inv.Logger}
inv.Stdout = inv.Stderr
}

// This WaitGroup solves for a race condition where we were logging
// while closing the log file in a defer. It probably solves
// others too.
Expand Down Expand Up @@ -234,7 +242,7 @@ func (r *RootCmd) ssh() *clibase.Cmd {
if err != nil {
return xerrors.Errorf("connect SSH: %w", err)
}
copier := newRawSSHCopier(logger, rawSSH, inv.Stdin, inv.Stdout)
copier := newRawSSHCopier(logger, rawSSH, stdioReader, stdioWriter)
if err = stack.push("rawSSHCopier", copier); err != nil {
return err
}
Expand Down Expand Up @@ -987,3 +995,12 @@ func sshDisableAutostartOption(src *clibase.Bool) clibase.Option {
Default: "false",
}
}

type stdioErrLogReader struct {
l slog.Logger
}

func (r stdioErrLogReader) Read(_ []byte) (int, error) {
r.l.Error(context.Background(), "reading from stdin in stdio mode is not allowed")
return 0, io.EOF
}
142 changes: 142 additions & 0 deletions cli/ssh_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli_test

import (
"bufio"
"bytes"
"context"
"crypto/ecdsa"
Expand Down Expand Up @@ -338,6 +339,147 @@ func TestSSH(t *testing.T) {
<-cmdDone
})

t.Run("Stdio_StartStoppedWorkspace_CleanStdout", func(t *testing.T) {
t.Parallel()

authToken := uuid.NewString()
ownerClient := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
owner := coderdtest.CreateFirstUser(t, ownerClient)
client, _ := coderdtest.CreateAnotherUser(t, ownerClient, owner.OrganizationID, rbac.RoleTemplateAdmin())
version := coderdtest.CreateTemplateVersion(t, client, owner.OrganizationID, &echo.Responses{
Parse: echo.ParseComplete,
ProvisionPlan: echo.PlanComplete,
ProvisionApply: echo.ProvisionApplyWithAgent(authToken),
})
coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
template := coderdtest.CreateTemplate(t, client, owner.OrganizationID, version.ID)
workspace := coderdtest.CreateWorkspace(t, client, owner.OrganizationID, template.ID)
coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, workspace.LatestBuild.ID)
// Stop the workspace
workspaceBuild := coderdtest.CreateWorkspaceBuild(t, client, workspace, database.WorkspaceTransitionStop)
coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, workspaceBuild.ID)

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()

clientOutput, clientInput := io.Pipe()
serverOutput, serverInput := io.Pipe()
monitorServerOutput, monitorServerInput := io.Pipe()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's pretty confusing what's happening with these 3 pipes, so I think an ASCII diagram would go a long way.

Copy link
Member Author

@mafredri mafredri Feb 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I neither know mermaid or how to turn it into ascii, so I pasted it in there 😅

flowchart LR
	A[ProxyCommand] --> B[captureProxyCommandStdoutW]
	B --> C[captureProxyCommandStdoutR]
	C --> VA[Validate output]
	C --> D[proxyCommandStdoutW]
	D --> E[proxyCommandStdoutR]
	E --> F[SSH Client]
Loading

closePipes := func() {
for _, c := range []io.Closer{clientOutput, clientInput, serverOutput, serverInput, monitorServerOutput, monitorServerInput} {
_ = c.Close()
}
}
defer closePipes()
tGo(t, func() {
<-ctx.Done()
closePipes()
})

// Here we start a monitor for the input going to the server
// (i.e. client stdout) to ensure that the output is clean.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this comment is incorrect. This doesn't monitor data being sent to the server (to monitor that we'd need to be in the tailnet network path and we are not), but rather data being sent to the stdio client.

Underscores a need for a diagram about the pipes.

Copy link
Member Author

@mafredri mafredri Feb 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The stdio client is a representation of a raw network connection, so I don't consider the statement incorrect. Lossy, perhaps.

Addendum: By "client stdout" I'm referring to the SSH command. Using "client" terminology for "stdio client" would cause more confusion IMO. Hence I prefer referring to the "stdio client" as "the connection" instead.

Yeah so after I rewrote the comment I realized what you meant, and you're right, it was incorrect, mb. 😄

serverInputBuf := make(chan byte, 4096)
tGo(t, func() {
defer close(serverInputBuf)

gotHeader := false
buf := bytes.Buffer{}
r := bufio.NewReader(monitorServerOutput)
for {
b, err := r.ReadByte()
if err != nil {
if errors.Is(err, io.ErrClosedPipe) {
return
}
assert.NoError(t, err, "read byte failed")
return
}
if b == '\n' || b == '\r' {
out := buf.Bytes()
t.Logf("monitorServerOutput: %q (%#x)", out, out)
buf.Reset()

// Ideally we would do further verification, but that would
// involve parsing the SSH protocol to look for output that
// doesn't belong. This at least ensures that no garbage is
// being sent to the server before trying to connect.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// being sent to the server before trying to connect.
// being sent to the client before trying to connect.

if !gotHeader {
gotHeader = true
assert.Equal(t, "SSH-2.0-Go", string(out), "invalid header")
}
} else {
_ = buf.WriteByte(b)
}
select {
case serverInputBuf <- b:
case <-ctx.Done():
return
}
}
})
tGo(t, func() {
defer serverInput.Close()

// Range closed by above goroutine.
for b := range serverInputBuf {
_, err := serverInput.Write([]byte{b})
if err != nil {
if errors.Is(err, io.ErrClosedPipe) {
return
}
assert.NoError(t, err, "write byte failed")
return
}
}
})

// Start the SSH stdio command.
inv, root := clitest.New(t, "ssh", "--stdio", workspace.Name)
clitest.SetupConfig(t, client, root)
inv.Stdin = clientOutput
inv.Stdout = monitorServerInput
inv.Stderr = io.Discard

cmdDone := tGo(t, func() {
err := inv.WithContext(ctx).Run()
assert.NoError(t, err)
})

tGo(t, func() {
// When the agent connects, the workspace was started, and we should
// have access to the shell.
_ = agenttest.New(t, client.URL, authToken)
coderdtest.NewWorkspaceAgentWaiter(t, client, workspace.ID).Wait()
})

conn, channels, requests, err := ssh.NewClientConn(&stdioConn{
Reader: serverOutput,
Writer: clientInput,
}, "", &ssh.ClientConfig{
// #nosec
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
})
require.NoError(t, err)
defer conn.Close()

sshClient := ssh.NewClient(conn, channels, requests)
session, err := sshClient.NewSession()
require.NoError(t, err)
defer session.Close()

command := "sh -c exit"
if runtime.GOOS == "windows" {
command = "cmd.exe /c exit"
}
err = session.Run(command)
require.NoError(t, err)
err = sshClient.Close()
require.NoError(t, err)
_ = clientOutput.Close()

<-cmdDone
})

t.Run("Stdio_RemoteForward_Signal", func(t *testing.T) {
t.Parallel()
client, workspace, agentToken := setupWorkspaceForAgent(t)
Expand Down