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

Channel inactivity timeouts for sshd(8) #16

Closed
wants to merge 6 commits into from
Closed

Conversation

djmdjm
Copy link
Owner

@djmdjm djmdjm commented Dec 20, 2022

These patches implement a channel inactivity timeout mechanism for sshd, that allows different channel types to have different timeouts. Here is an excerpt from the sshd_config.5 manpage:


ChannelTimeouts

Specifies whether and how quickly sshd(8) should close inactive channels. Timeouts for specified as one or more "type=interval" pairs separated by whitespace, where the "type" must be a channel type name (as described in the table below), optionally containing wildcard characters. "interval" must be a value specified in seconds and may use any of the units documented in the TIME FORMATS section. For example, "session:*=5m" would cause all sessions to terminate after five minutes of inactivity.

The available channel types include:

agent-connection

Open connections to ssh-agent(1).

direct-tcpip direct-streamlocal@openssh.com

Open TCP or Unix socket (respectively) connections that have been established from a ssh(1) local forwarding, i.e. LocalForward or DynamicForward.

forwarded-tcpip forwarded-streamlocal@openssh.com

Open TCP or Unix socket (respectively) connections that have been established to a sshd(8) listening on behalf of a ssh(1) remote forwarding, i.e. RemoteForward.

session:command

Command execution sessions.

session:shell

Interactive shell sessions.

session:subsystem:...

Subsystem sessions, e.g. for sftp(1), which could be identified as session:subsystem:sftp.

x11-connection

Open X11 forwarding sessions.

Note that, in all the above cases, terminating an inactive session does not guarantee to remove all resources associated with the session, e.g. shell processes or X11 clients relating to the session may continue to execute.

Moreover, terminating an inactive session does not prevent the client from requesting another session of the same type. In particular, expiring an inactive forwarding session does not prevent another identical forwarding from being subsequently created.

The default is not to expire channels of any type for inactivity.

serverloop.c Outdated Show resolved Hide resolved
@mfriedl
Copy link
Contributor

mfriedl commented Jan 2, 2023

looks fine, one comment inline

This API simplified poll()/ppoll() timeout calculation by remembering the
next requested timeout and returning it in forms that can be used by either
poll() or ppoll().

This gets the fiddly seconds/milliseconds/timespec math out of the mainloops,
and makes adding more timeouts easier.
This will forcibly close an open channel by simulating read/write errors,
draining the IO buffers and calling the detach function.

Previously the detach function was only ever called during channel garbage
collection, but there was no way to signal the user of a channel (e.g.
session.c) that its channel was being closed deliberately (vs. by the
usual state-machine logic). So this adds an extra "force" argument to the
channel cleanup callback to indicate this condition.
These are now used by sshd_config:ChannelTimeouts to specify timeouts by
channel type, so force them all to use a similar format without whitespace.
This sets an "extended" channel type after channel creation (e.g.
"session:subsystem:sftp") that will be used for setting channel inactivity
timeouts.
This adds a sshd_config ChannelTimeouts directive that allows channels that
have not seen traffic in a configurable interval to be automatically closed.
Different timeouts may be applied to session, X11, agent and TCP forwarding
channels.
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.

None yet

2 participants