Signal Handler with Functionality that is not Asynchronous-Safe

Incomplete Variant
Structure: Simple
Description

This weakness occurs when a program's signal handler contains code that is not asynchronous-safe. This means the handler can be interrupted or can corrupt shared data, leading to unpredictable program behavior.

Extended Description

Signal handlers interrupt your program's normal flow to process events. If these handlers use global variables, call non-reentrant functions like malloc(), or modify shared state, they can corrupt memory or program logic when interrupted. This corruption often creates race conditions, making your application vulnerable to crashes (denial of service) or, in some cases, allowing an attacker to execute arbitrary code. A common pitfall is writing handlers that assume they run only once, but signals can fire repeatedly or share the same handler. If your main code and the signal handler both access the same data, an incoming signal can leave that data in a broken, inconsistent state. Since very few functions are truly reentrant, you must carefully design handlers to use only asynchronous-safe operations to maintain system stability.

Common Consequences 1
Scope: IntegrityConfidentialityAvailability

Impact: DoS: Crash, Exit, or RestartExecute Unauthorized Code or Commands

The most common consequence will be a corruption of the state of the product, possibly leading to a crash or exit. However, if the signal handler is operating on state variables for security relevant libraries or protection mechanisms, the consequences can be far more severe, including protection mechanism bypass, privilege escalation, or information exposure.

Potential Mitigations 2
Phase: ImplementationArchitecture and Design
Eliminate the usage of non-reentrant functionality inside of signal handlers. This includes replacing all non-reentrant library calls with reentrant calls. Note: This will not always be possible and may require large portions of the product to be rewritten or even redesigned. Sometimes reentrant-safe library alternatives will not be available. Sometimes non-reentrant interaction between the state of the system and the signal handler will be required by design.

Effectiveness: High

Phase: Implementation
Where non-reentrant functionality must be leveraged within a signal handler, be sure to block or mask signals appropriately. This includes blocking other signals within the signal handler itself that may also leverage the functionality. It also includes blocking all signals reliant upon the functionality when it is being accessed or modified by the normal behaviors of the product.
Demonstrative Examples 2

ID : DX-26

This code registers the same signal handler function with two different signals (Signal Handler Function Associated with Multiple Signals). If those signals are sent to the process, the handler creates a log message (specified in the first argument to the program) and exits.

Code Example:

Bad
C
c

/* artificially increase the size of the timing window to make demonstration of this weakness easier. /

c
c
The handler function uses global state (globalVar and logMessage), and it can be called by both the SIGHUP and SIGTERM signals. An attack scenario might follow these lines:
- The program begins execution, initializes logMessage, and registers the signal handlers for SIGHUP and SIGTERM. - The program begins its "normal" functionality, which is simplified as sleep(), but could be any functionality that consumes some time. - The attacker sends SIGHUP, which invokes handler (call this "SIGHUP-handler"). - SIGHUP-handler begins to execute, calling syslog(). - syslog() calls malloc(), which is non-reentrant. malloc() begins to modify metadata to manage the heap. - The attacker then sends SIGTERM. - SIGHUP-handler is interrupted, but syslog's malloc call is still executing and has not finished modifying its metadata. - The SIGTERM handler is invoked. - SIGTERM-handler records the log message using syslog(), then frees the logMessage variable.
At this point, the state of the heap is uncertain, because malloc is still modifying the metadata for the heap; the metadata might be in an inconsistent state. The SIGTERM-handler call to free() is assuming that the metadata is inconsistent, possibly causing it to write data to the wrong location while managing the heap. The result is memory corruption, which could lead to a crash or even code execution, depending on the circumstances under which the code is running.
Note that this is an adaptation of a classic example as originally presented by Michal Zalewski [REF-360]; the original example was shown to be exploitable for code execution.
Also note that the strdup(argv[1]) call contains a potential buffer over-read (Buffer Over-read) if the program is called without any arguments, because argc would be 0, and argv[1] would point outside the bounds of the array.

ID : DX-48

The following code registers a signal handler with multiple signals in order to log when a specific event occurs and to free associated memory before exiting.

Code Example:

Bad
C
c

/* Sleep statements added to expand timing window for race condition /

c
c
However, the following sequence of events may result in a double-free (Double Free):
1. a SIGHUP is delivered to the process 1. sh() is invoked to process the SIGHUP 1. This first invocation of sh() reaches the point where global1 is freed 1. At this point, a SIGTERM is sent to the process 1. the second invocation of sh() might do another free of global1 1. this results in a double-free (Double Free)
This is just one possible exploitation of the above code. As another example, the syslog call may use malloc calls which are not async-signal safe. This could cause corruption of the heap management structures. For more details, consult the example within "Delivering Signals for Fun and Profit" [REF-360].
Observed Examples 6
CVE-2008-4109Signal handler uses functions that ultimately call the unsafe syslog/malloc/s*printf, leading to denial of service via multiple login attempts
CVE-2006-5051Chain: Signal handler contains too much functionality (Signal Handler with Functionality that is not Asynchronous-Safe), introducing a race condition (Concurrent Execution using Shared Resource with Improper Synchronization ('Race Condition')) that leads to a double free (Double Free).
CVE-2001-1349unsafe calls to library functions from signal handler
CVE-2004-0794SIGURG can be used to remotely interrupt signal handler; other variants exist.
CVE-2004-2259SIGCHLD signal to FTP server can cause crash under heavy load while executing non-reentrant functions like malloc/free.
CVE-2002-1563SIGCHLD not blocked in a daemon loop while counter is modified, causing counter to get out of sync.
Related Weaknesses
Taxonomy Mapping
  • CERT C Secure Coding