Skip to content
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

Fix agent forwarding for multi-session connections #3613

Merged
merged 1 commit into from
Apr 29, 2020

Conversation

fspmarshall
Copy link
Contributor

This PR changes the lifetime of agent forwarding to be scoped to the underlying ssh connection, instead of the specific ssh channel which initially passed the agent forwarding request.

Previous behavior was inconsistent with how openssh handles agent forwarding, and was causing issues with applications which rely on creating multiple separate exec sessions on a single connection.

Fixes #3471

@fspmarshall fspmarshall force-pushed the fspmarshall/fix-agent-forwarding branch from d74855a to 655b7c0 Compare April 22, 2020 18:21
@fspmarshall fspmarshall marked this pull request as ready for review April 22, 2020 18:21
lib/sshutils/ctx.go Outdated Show resolved Hide resolved
@fspmarshall
Copy link
Contributor Author

retest this please

@fspmarshall fspmarshall force-pushed the fspmarshall/fix-agent-forwarding branch 3 times, most recently from f928384 to 7b1dcb3 Compare April 24, 2020 14:44
@fspmarshall
Copy link
Contributor Author

retest this please

@fspmarshall fspmarshall force-pushed the fspmarshall/fix-agent-forwarding branch from 7b1dcb3 to d24e2ab Compare April 24, 2020 16:00
lib/sshutils/ctx.go Outdated Show resolved Hide resolved
lib/sshutils/server.go Outdated Show resolved Hide resolved
lib/srv/forward/sshserver.go Outdated Show resolved Hide resolved
lib/srv/ctx.go Outdated Show resolved Hide resolved
lib/srv/regular/sshserver.go Outdated Show resolved Hide resolved
@fspmarshall fspmarshall force-pushed the fspmarshall/fix-agent-forwarding branch from d24e2ab to b4bf099 Compare April 24, 2020 22:56
c.RLock()
defer c.RUnlock()
return c.agent
if c.Parent == nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should GetAgent and GetAgentChannel return an error? Because it could potentially segfault here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The fact that agent might be nil seems to be well handled, so I've opted to update the method docs to indicate that it might be nil rather than changing the method to return an error.

lib/sshutils/ctx.go Show resolved Hide resolved
lib/srv/regular/sshserver.go Show resolved Hide resolved
@russjones
Copy link
Contributor

@awly Can you take a look as well?

@@ -1265,11 +1265,11 @@ func (s *discardServer) Stop() {
s.sshServer.Close()
}

func (s *discardServer) HandleNewChan(conn net.Conn, sconn *ssh.ServerConn, newChannel ssh.NewChannel) {
func (s *discardServer) HandleNewChan(ccx *sshutils.ConnectionContext, newChannel ssh.NewChannel) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: ctx?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I deliberately assumed the convention of referring to sshutils.ConnectionContext as ccx to handle the fact that it is frequently in-scope at the same time as srv.ServerContext, which is already conventionally called ctx.

Comment on lines +453 to +456
if c.Parent == nil {
return nil
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't c.Parent always be set?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed nil checks for methods which modify state. All the getters on the zero value of srv.ConnectionContext return zero values themselves instead of panicking (a fact that some tests rely on), so I kept the nil checks on the getters in order to preserve this behavior.

lib/srv/regular/sshserver_test.go Show resolved Hide resolved
// make sure the socket is gone after we closed the connection.
err = s.clt.Close()
c.Assert(err, IsNil)
s.clt = nil
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't seem necessary

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nullifying clt prevents test-failure due to double-close in the suite's cleanup function. Added comment to clarify.

Comment on lines +30 to +31
// ConnectionContext manages connection-level state.
type ConnectionContext struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

Context is overloaded in Go.
How about just ServerConn or Conn?

lib/sshutils/ctx.go Outdated Show resolved Hide resolved
lib/sshutils/ctx.go Outdated Show resolved Hide resolved
Changes the lifetime of agent forwarding to be scoped
to the underlying ssh connection, instead of the
specific ssh channel which initially passed the agent
forwarding request.
@fspmarshall fspmarshall force-pushed the fspmarshall/fix-agent-forwarding branch from b4bf099 to 33b8803 Compare April 28, 2020 20:45
@fspmarshall fspmarshall merged commit c341d2b into master Apr 29, 2020
@fspmarshall fspmarshall deleted the fspmarshall/fix-agent-forwarding branch April 29, 2020 00:47
fspmarshall added a commit that referenced this pull request May 19, 2020
Changes agent channel setup behavior to be consistent
openssh by having servers lazily request agent channels
when they are needed, rather than immediately starting a
single connection-wide channel as soon as forwarding is
requested.  Fixes an issue introduced in #3613 which
caused openssh clients to hang on exit due to persistent
agent channel.
fspmarshall added a commit that referenced this pull request May 28, 2020
Changes agent channel setup behavior to be consistent
openssh by having servers lazily request agent channels
when they are needed, rather than immediately starting a
single connection-wide channel as soon as forwarding is
requested.  Fixes an issue introduced in #3613 which
caused openssh clients to hang on exit due to persistent
agent channel.
fspmarshall added a commit that referenced this pull request Jun 6, 2020
Changes agent channel setup behavior to be consistent
openssh by having servers lazily request agent channels
when they are needed, rather than immediately starting a
single connection-wide channel as soon as forwarding is
requested.  Fixes an issue introduced in #3613 which
caused openssh clients to hang on exit due to persistent
agent channel.
fspmarshall added a commit that referenced this pull request Jun 10, 2020
Changes agent channel setup behavior to be consistent
openssh by having servers lazily request agent channels
when they are needed, rather than immediately starting a
single connection-wide channel as soon as forwarding is
requested.  Fixes an issue introduced in #3613 which
caused openssh clients to hang on exit due to persistent
agent channel.
fspmarshall added a commit that referenced this pull request Jun 10, 2020
Changes agent channel setup behavior to be consistent
openssh by having servers lazily request agent channels
when they are needed, rather than immediately starting a
single connection-wide channel as soon as forwarding is
requested.  Fixes an issue introduced in #3613 which
caused openssh clients to hang on exit due to persistent
agent channel.
fspmarshall added a commit that referenced this pull request Jun 22, 2020
Changes agent channel setup behavior to be consistent
openssh by having servers lazily request agent channels
when they are needed, rather than immediately starting a
single connection-wide channel as soon as forwarding is
requested.  Fixes an issue introduced in #3613 which
caused openssh clients to hang on exit due to persistent
agent channel.
fspmarshall added a commit that referenced this pull request Jun 24, 2020
Changes agent channel setup behavior to be consistent
openssh by having servers lazily request agent channels
when they are needed, rather than immediately starting a
single connection-wide channel as soon as forwarding is
requested.  Fixes an issue introduced in #3613 which
caused openssh clients to hang on exit due to persistent
agent channel.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SSH agent forwarding via Teleport is only respected for the first command in a session
4 participants