Skip to content

add back explicit termination of preauth child process - #814

Merged
Tess Gauthier (tgauth) merged 1 commit into
PowerShell:latestw_allfrom
tgauth:fix-preauth-child-termination
Dec 1, 2025
Merged

Tess Gauthier (tgauth) merged 1 commit into
PowerShell:latestw_allfrom
tgauth:fix-preauth-child-termination

Conversation

@tgauth

@tgauth Tess Gauthier (tgauth) commented Nov 4, 2025 •

Copy link
Copy Markdown
Collaborator

PR Summary

  • add back code that was removed starting in V8.9 which prevented the pre-auth child process from being terminated when LoginGraceTime expired, potentially leading to a build-up of orphaned child processes until the corresponding client disconnected
  • update LoginGraceTime pester test

PR Context

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes the grace alarm handler behavior on Windows to ensure both session processes exit when LoginGraceTime expires, and includes a minor formatting correction.

  • Added Windows-specific logic to explicitly kill the child process when the login grace period expires
  • Updated test expectations to verify both session processes exit after grace timeout
  • Removed trailing whitespace from a comment

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
sshd-session.c Added Windows-specific grace alarm handler to kill child process via pmonitor PID; removed trailing whitespace from comment
regress/pesterTests/SSHD.Tests.ps1 Updated test assertion to expect both session processes to exit after LoginGraceTime expires

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread sshd-session.c Resolved
@tgauth

Copy link
Copy Markdown
Collaborator Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@o-sdn-o

Dmitry Sapozhnikov (o-sdn-o) commented Nov 4, 2025 •

Copy link
Copy Markdown

I was thinking of making the following changes to kill all child processes (we have a list of their pids in extern struct _children children; at openssh-portable\contrib\win32\win32compat\signal.c:60):

Add to the openssh-portable\contrib\win32\win32compat\signal.c:150 the something like:

...
else if (signum == SIGALRM) {
    if (handler == grace_alarm_handler) {
        set_atexit([]{
            for (auto child : children) {
                TerminateProcess(child.pid, 0);
                CloseHandle(child.handle);
            }
        });
    }
    else if (handler == SIG_DFL) {
        /* reset atexit */
    }
}

@o-sdn-o

Copy link
Copy Markdown

Here's a working fix that terminates all child processes: latestw_all...o-sdn-o:openssh-portable:Fix-child-processes-termination-at-exit

@o-sdn-o

Dmitry Sapozhnikov (o-sdn-o) commented Nov 5, 2025 •

Copy link
Copy Markdown

I have a question - is it legal to state the following (from my fix 72114b9):

static void
grace_alarm_handler(int sig)
{
    #ifdef WINDOWS
        /* We don't have to be signal-safe on Windows. Different native event mechanisms (APC). */
        exit(EXIT_LOGIN_GRACE); /* Perform full C library cleanup and call atexit() registered functions. */
    #else
        ...
        _exit(EXIT_LOGIN_GRACE); /* We have to be signal-safe. */
    #endif
}

@o-sdn-o

Copy link
Copy Markdown

I have a question - is it legal to state the following (from my fix 72114b9):

In the current codebase, exit(0); (not _exit(0)) is called inside kill() (aka w32_kill()) inside w32_raise(sig);:

int
w32_kill(int pid, int sig)
{
int child_index, i;
if (pid == GetCurrentProcessId())
return w32_raise(sig);

default: /* exit process */
exit(0);
}

@o-sdn-o

Copy link
Copy Markdown

I have a question - is it legal to state the following (from my fix 72114b9):

AI Overview

No, you do not need to be signal-safe when using a user-mode Asynchronous Procedure Call (APC) on Windows because APCs are not true signal handlers; they are triggered when a thread enters an alertable wait state, and they don't interrupt execution in the same way as POSIX signals.
Unlike Unix signals that interrupt execution at any point, user-mode APCs are queued to run when a thread voluntarily enters an alertable state (like SleepEx or WaitForSingleObjectEx), and they do not have the same restrictions on using non-async-signal-safe functions.

Comment thread sshd-session.c
send_config_state(int fd, struct sshbuf* conf)
{
/* copied from send_rexec_state() in sshd.c
/* copied from send_rexec_state() in sshd.c

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is this trailing space from upstream? I would leave it unchanged if that is the case just to reduce discrepancies

@o-sdn-o

Dmitry Sapozhnikov (o-sdn-o) commented Nov 6, 2025 •

Copy link
Copy Markdown

I have a question - is it legal to state the following (from my fix 72114b9):

AI Overview

No, you do not need to be signal-safe when using a user-mode Asynchronous Procedure Call (APC) on Windows because APCs are not true signal handlers; they are triggered when a thread enters an alertable wait state, and they don't interrupt execution in the same way as POSIX signals.
Unlike Unix signals that interrupt execution at any point, user-mode APCs are queued to run when a thread voluntarily enters an alertable state (like SleepEx or WaitForSingleObjectEx), and they do not have the same restrictions on using non-async-signal-safe functions.

Perhaps I haven't expressed myself clearly enough here. At first glance, it seems the answer is No. Here's the question and the detailed answer:

I asked:

Should we be signal-safe during signal handling with APC approach on Windows?

Answer:

No, you do not need to worry about signal-safety with a Windows Asynchronous Procedure Call (APC) in the same way you do with POSIX signal handlers, because APCs are not the same as signals. Unlike signal handlers, which can interrupt a program at any point, user-mode APCs are only executed when the thread enters an "alertable state" by calling specific functions like SleepEx.
This means you don't have the same risk of interrupting a function while it's in an unsafe state, and you don't need to restrict your APC callback to only use async-signal-safe functions.

Key differences between APCs and signal handlers

Feature POSIX Signal Handler Windows APC
Execution Model Can interrupt a program at any arbitrary point, requiring careful use of async-signal-safe functions. Executed only when the thread enters an "alertable state," which is a more controlled environment.
Safety Implications Extremely strict rules must be followed to avoid reentrancy and other race conditions. Less restrictive because the execution context is more predictable. You don't have to worry as much about clobbering an active stack in the same way.
Function Restrictions Must only call functions that are guaranteed to be async-signal-safe. No such restriction is needed, as the environment is not subject to the same level of reentrancy risk as a POSIX signal handler.

PS: Should I create I created a separate PR for these conversations so as not to make noise here.

@tgauth
Tess Gauthier (tgauth) merged commit ed76997 into PowerShell:latestw_all Dec 1, 2025
@tgauth
Tess Gauthier (tgauth) deleted the fix-preauth-child-termination branch December 1, 2025 21:44
Hazel Duvall (hazelduvall) pushed a commit to hazelduvall/openssh-portable that referenced this pull request May 6, 2026
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.

4 participants