Skip to content
Race condition Write-up Advanced

regreSSHion: RCE in OpenSSH's server, on glibc-based Linux systems (CVE-2024-6387)

A signal handler race condition in OpenSSH's server means that when a client fails to authenticate within the login grace time, the SIGALRM handler runs functions that are not async-signal-safe, such as syslog(). On glibc-based Linux systems an unauthenticated attacker could interrupt these calls at the right moment and potentially achieve remote code execution as root.

qualys.com

Published on qualys.com

regreSSHion: RCE in OpenSSH's server, on glibc-based Linux systems (CVE-2024-6387)

The author, on qualys.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

The original is short, so this covers everything it reports, then explains this kind of bug in general: how it works, how to test for it and how it is fixed. The general sections are about the bug class, not this target. Written with AI and checked against the original; the original is the record. 5 min read.

What the report shows

Qualys analysed a signal handler race condition in OpenSSH's server, sshd, and published it as CVE-2024-6387. The affected feature is the authentication timeout: if a client does not authenticate within LoginGraceTime seconds (120 by default, 600 in old versions), sshd's SIGALRM handler runs asynchronously and calls functions that are not async-signal-safe, such as syslog(). The condition affects sshd in its default configuration.

The report frames this as a regression of CVE-2006-5051, reported by Mark Dowd in 2006. The regression was introduced in October 2020 (OpenSSH 8.5p1) by commit 752250c ("revised log infrastructure for OpenSSH"), which accidentally removed an #ifdef DO_LOG_SAFE_IN_SIGHAND from sigdie(), a function called directly by the SIGALRM handler. The affected ranges given are:

  • OpenSSH < 4.4p1, if not backport-patched against CVE-2006-5051 or not patched against CVE-2008-4109 (an incorrect fix for CVE-2006-5051);
  • 4.4p1 <= OpenSSH < 8.5p1, not vulnerable, because the patch for CVE-2006-5051 turned sigdie() into a safe _exit(1) call;
  • 8.5p1 <= OpenSSH < 9.8p1, vulnerable again.

The researchers exploited three versions on i386, starting with old releases to avoid fighting ASLR and NX, then moving to a current one:

  1. SSH-2.0-OpenSSH_3.4p1 Debian 1:3.4p1-1.woody.3 (Debian 3.0r6, 2005). A free() call in the public-key parsing code is interrupted by SIGALRM, leaving the heap inconsistent; the inconsistency is exploited during another free() inside the SIGALRM handler, overwriting glibc's __free_hook with the address of shellcode in the heap. This Debian has neither ASLR nor NX.
  2. SSH-2.0-OpenSSH_4.2p1 Debian-7ubuntu3 (Ubuntu 6.06.1, 2006). Here the SIGALRM handler no longer calls packet_close(), and glibc 2.3.6 takes a mandatory lock in the malloc family, so the target becomes pam_start(): an interrupted call leaves a PAM structure inconsistent, exploited during pam_end() in the handler via the House of Mind technique, overwriting _exit()'s entry in .got.plt.
  3. SSH-2.0-OpenSSH_9.2p1 Debian-2+deb12u2 (Debian 12.5.0, 2024). The handler calls only syslog(), which on glibc 2.36 calls malloc() and free() on its first invocation. An interrupted malloc() leaves a remainder chunk linked but with an uninitialised size field; the enlarged chunk later overlaps a FILE structure allocated by fopen() inside __tzfile_read(), and a single-byte overwrite of _vtable_offset redirects execution to an attacker-controlled __fct pointer.

The report gives timing figures: about 10,000 tries on average to win the race in each case. With 10 connections (MaxStartups) per 600 seconds (LoginGraceTime) on Debian 3.0r6, that is about a week for a remote root shell; with 10 connections per 120 seconds on Ubuntu 6.06.1, one to two days; with 100 connections per 120 seconds on Debian 12.5.0, three to four hours to win the race and six to eight hours for a root shell, because the glibc address can only be guessed correctly half the time. The work targeted virtual machines on a mostly stable link with about 10ms packet jitter, and an amd64 exploit was still in progress.

The fix, commit 81c1099 of 6 June 2024, moves the async-signal-unsafe code from the SIGALRM handler to sshd's listener process, where it is handled synchronously. Because it sits on top of a larger commit (03e3de4), backporting may be difficult; the report offers two alternatives. The first is to remove or comment out the unsafe code in sshsigdie():

Text
sshsigdie(const char *file, const char *func, int line, int showfunc,
    LogLevel level, const char *suffix, const char *fmt, ...)
{
#if 0
        va_list args;

        va_start(args, fmt);
        sshlogv(file, func, line, showfunc, SYSLOG_LEVEL_FATAL,
            suffix, fmt, args);
        va_end(args);
#endif
        _exit(1);
}

The second is to set LoginGraceTime to 0, which trades the remote code execution for a denial of service through exhaustion of all MaxStartups connections. The coordinated release date was 1 July 2024.

Root cause

The root cause is a signal handler that calls non-async-signal-safe functions. When SIGALRM interrupts the main flow, the handler can observe, and act on, data structures left half-updated by the interrupted code. On glibc, syslog() reaches malloc() and free(), so the handler itself mutates the heap while the interrupted code may be mid-way through a heap operation. The regression is the accidental removal of the guard that had made sigdie() safe.

Impact

On glibc-based Linux systems the report describes unauthenticated remote code execution as root, because the vulnerable code is in sshd's privileged, unsandboxed child. The authors note that OpenBSD is not vulnerable, since its handler calls syslog_r(), and that they did not investigate other libcs or operating systems. Exploitation is probabilistic and slow, and the amd64 work was unfinished.

How this kind of bug works

A race condition exists when correctness depends on the order or atomicity of concurrent operations. Signals are a special case: a handler can preempt the main thread at almost any instruction, including in the middle of a multi-step update such as linking a chunk into a free list and then writing its size. If the handler then calls library code that assumes those structures are consistent, the program can be steered into using attacker-influenced values. Defences are to keep handlers minimal and async-signal-safe (set a flag, write to a pipe, call _exit), to defer logging and allocation to the main loop, and to harden allocators and FILE structures so that a corrupted structure cannot be turned into a function call.

How to test for it

  1. Identify code paths where a signal, alarm or timeout handler calls logging, allocation, PAM, or other library functions. Grep for signal, sigaction, alarm, and the handler bodies.
  2. Map which inputs reach the interrupted code. Here it was public-key parsing and PAM initialisation, both reachable before authentication.
  3. Send the triggering input so that it arrives just before the timeout fires, repeatedly, and watch for crashes, hangs or unexpected log output. Vary the timing deliberately and record round-trip times.
  4. A positive result is a reproducible crash or state corruption tied to the timeout boundary, ideally with a debugger showing the handler acting on inconsistent data. A single crash under load is not enough.

Common false positives are ordinary timeouts, resource exhaustion from many connections, and crashes that also occur without the timing pressure. Confirm that the crash disappears when the handler is made safe or the timeout is disabled.

How it is fixed

Move unsafe work out of the signal handler and perform it synchronously elsewhere, as the upstream commit does. Where that is impractical, guard the unsafe call so the handler only calls _exit(). Disabling the timeout removes the trigger at the cost of a denial of service.

Hunting notes

Look for timeout and signal handlers in daemons that accept unauthenticated input: SSH, mail, FTP, TLS terminators, and anything with a grace period. Recon signals include old or unpatched versions, custom backports, and handlers that log directly.

The original is short, so this covers everything it reports, then explains this kind of bug in general: how it works, how to test for it and how it is fixed. The general sections are about the bug class, not this target. Written with AI and checked against the original; the original is the record. 7 min read.

What happened, in plain words

OpenSSH is the software that lets you log in to a Linux server over the network, and sshd is the part of it that runs on the server and waits for connections. The security firm Qualys studied a bug in sshd and published it as CVE-2024-6387. The bug lives in the login timeout: if a client does not finish logging in within LoginGraceTime seconds (120 by default, 600 in old versions), the server gives up and runs a small piece of code called a signal handler. That handler calls functions that are not safe to call at that moment, such as syslog() (the function that writes log messages). The report says the bug affects sshd in its default configuration.

The report calls this a regression of CVE-2006-5051, a bug reported by Mark Dowd in 2006. A regression means an old bug that was fixed and then came back. It returned in October 2020 (OpenSSH 8.5p1) through commit 752250c ("revised log infrastructure for OpenSSH"), which accidentally removed an #ifdef DO_LOG_SAFE_IN_SIGHAND from sigdie(), a function the timeout handler calls directly. The affected versions are:

  • OpenSSH < 4.4p1, if not backport-patched against CVE-2006-5051 or not patched against CVE-2008-4109 (an incorrect fix for CVE-2006-5051);
  • 4.4p1 <= OpenSSH < 8.5p1, not vulnerable, because the patch for CVE-2006-5051 turned sigdie() into a safe _exit(1) call;
  • 8.5p1 <= OpenSSH < 9.8p1, vulnerable again.

The researchers built working attacks against three versions on i386, starting with old releases so they did not have to fight modern protections, then moving to a current one:

  1. SSH-2.0-OpenSSH_3.4p1 Debian 1:3.4p1-1.woody.3 (Debian 3.0r6, 2005). A free() call in the public-key parsing code is interrupted by the timeout signal, leaving the heap (the memory area where the program keeps its working data) inconsistent. That inconsistency is then exploited during another free() inside the handler, overwriting glibc's __free_hook with the address of shellcode in the heap. This Debian has neither ASLR nor NX.
  2. SSH-2.0-OpenSSH_4.2p1 Debian-7ubuntu3 (Ubuntu 6.06.1, 2006). Here the handler no longer calls packet_close(), and glibc 2.3.6 takes a mandatory lock in the malloc family, so the target becomes pam_start(): an interrupted call leaves a PAM structure inconsistent, exploited during pam_end() in the handler via the House of Mind technique, overwriting _exit()'s entry in .got.plt.
  3. SSH-2.0-OpenSSH_9.2p1 Debian-2+deb12u2 (Debian 12.5.0, 2024). The handler calls only syslog(), which on glibc 2.36 calls malloc() and free() on its first invocation. An interrupted malloc() leaves a remainder chunk linked but with an uninitialised size field; the enlarged chunk later overlaps a FILE structure allocated by fopen() inside __tzfile_read(), and a single-byte overwrite of _vtable_offset redirects execution to an attacker-controlled __fct pointer.

The report gives timing figures: about 10,000 tries on average to win the race in each case. With 10 connections (MaxStartups) per 600 seconds (LoginGraceTime) on Debian 3.0r6, that is about a week for a remote root shell; with 10 connections per 120 seconds on Ubuntu 6.06.1, one to two days; with 100 connections per 120 seconds on Debian 12.5.0, three to four hours to win the race and six to eight hours for a root shell, because the glibc address can only be guessed correctly half the time. The work targeted virtual machines on a mostly stable link with about 10ms packet jitter, and an amd64 exploit was still in progress.

The fix, commit 81c1099 of 6 June 2024, moves the unsafe code from the timeout handler to sshd's listener process, where it is handled synchronously. Because it sits on top of a larger commit (03e3de4), backporting may be difficult; the report offers two alternatives. The first is to remove or comment out the unsafe code in sshsigdie():

Text
sshsigdie(const char *file, const char *func, int line, int showfunc,
    LogLevel level, const char *suffix, const char *fmt, ...)
{
#if 0
        va_list args;

        va_start(args, fmt);
        sshlogv(file, func, line, showfunc, SYSLOG_LEVEL_FATAL,
            suffix, fmt, args);
        va_end(args);
#endif
        _exit(1);
}

Line by line: the function signature takes the source file, function name, line number and a log level, plus a format string and arguments. The #if 0 block is switched off by the preprocessor, so the logging calls inside it (va_start, sshlogv, va_end) are never compiled in. What remains is _exit(1), which ends the process immediately and is safe to call from a signal handler. The second alternative is to set LoginGraceTime to 0, which trades the remote code execution for a denial of service through exhaustion of all MaxStartups connections. The coordinated release date was 1 July 2024.

Root cause

The root cause is a signal handler that calls functions which are not async-signal-safe. When the timeout signal interrupts the main flow, the handler can observe, and act on, data structures left half-updated by the interrupted code. On glibc, syslog() reaches malloc() and free(), so the handler itself changes the heap while the interrupted code may be mid-way through a heap operation. The regression is the accidental removal of the guard that had made sigdie() safe.

Impact

On glibc-based Linux systems the report describes unauthenticated remote code execution as root, because the vulnerable code is in sshd's privileged, unsandboxed child. The authors note that OpenBSD is not vulnerable, since its handler calls syslog_r(), and that they did not investigate other libcs or operating systems. Exploitation is probabilistic and slow, and the amd64 work was unfinished.

In general, how this kind of bug works

A race condition exists when correctness depends on the order or atomicity of concurrent operations. Signals are a special case: a handler can preempt the main thread at almost any instruction, including in the middle of a multi-step update such as linking a chunk into a free list and then writing its size. If the handler then calls library code that assumes those structures are consistent, the program can be steered into using attacker-influenced values. Defences are to keep handlers minimal and async-signal-safe (set a flag, write to a pipe, call _exit), to defer logging and allocation to the main loop, and to harden allocators and FILE structures so that a corrupted structure cannot be turned into a function call.

In general, how to test for it

  1. Identify code paths where a signal, alarm or timeout handler calls logging, allocation, PAM, or other library functions. Grep for signal, sigaction, alarm, and the handler bodies.
  2. Map which inputs reach the interrupted code. Here it was public-key parsing and PAM initialisation, both reachable before authentication.
  3. Send the triggering input so that it arrives just before the timeout fires, repeatedly, and watch for crashes, hangs or unexpected log output. Vary the timing deliberately and record round-trip times.
  4. A positive result is a reproducible crash or state corruption tied to the timeout boundary, ideally with a debugger showing the handler acting on inconsistent data. A single crash under load is not enough.

Common false positives are ordinary timeouts, resource exhaustion from many connections, and crashes that also occur without the timing pressure. Confirm that the crash disappears when the handler is made safe or the timeout is disabled.

In general, how it is fixed

Move unsafe work out of the signal handler and perform it synchronously elsewhere, as the upstream commit does. Where that is impractical, guard the unsafe call so the handler only calls _exit(). Disabling the timeout removes the trigger at the cost of a denial of service.

In general, what to look for when hunting

Look for timeout and signal handlers in daemons that accept unauthenticated input: SSH, mail, FTP, TLS terminators, and anything with a grace period. Recon signals include old or unpatched versions, custom backports, and handlers that log directly.

Glossary

  • OpenSSH / sshd: the software that provides encrypted remote login; sshd is the server side that listens for connections.
  • CVE: a public identifier for a known security vulnerability.
  • Regression: a bug that was fixed earlier and then reappeared.
  • Signal handler: a small function the operating system runs when a signal (such as a timeout alarm) arrives, interrupting whatever the program was doing.
  • SIGALRM: the signal used for alarms and timeouts.
  • LoginGraceTime: how long sshd waits for a client to authenticate before giving up.
  • MaxStartups: the maximum number of unauthenticated connections sshd accepts at once.
  • Async-signal-safe: a function that is safe to call from a signal handler, because it does not rely on shared state that may be half-updated.
  • syslog(): the standard function for writing log messages.
  • malloc() / free(): the functions that hand out and release heap memory.
  • Heap: the memory area a program uses for data created while it runs.
  • glibc: the standard C library on most Linux systems.
  • ASLR: address space layout randomisation, which places memory at unpredictable addresses to make exploitation harder.
  • NX: a protection that marks memory as non-executable so injected code cannot run.
  • PAM: the pluggable authentication module framework Linux uses to check logins.
  • Shellcode: small machine code an attacker wants the program to run.
  • Race condition: a bug where the result depends on the exact timing of two operations.
  • Denial of service: making a service unavailable to legitimate users.
  • Remote code execution: running an attacker's code on a remote machine.
  • Root: the all-powerful administrator account on Linux.
  • Backport: applying a fix to an older software version.

Similar reports

View all

Same bug class and techniques, clearest first.

Race conditions

The lesson behind this report.

Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.