According to the C Standard [ISO/IEC 9899:2011], the behavior a program is undefined when
the value of
errno
is referred to after a signal occurred other than as the result of calling theabort
orraise
function and the corresponding signal handler obtained aSIG_ERR
return from a call to thesignal
function.
(See undefined behavior 133 of Annex J.)
A signal handler is allowed to call signal()
, and if that fails, signal()
returns SIG_ERR
and sets errno
to a positive value. However, if the event that caused a signal was external (not the result of the program calling abort()
or raise()
), the only functions the signal handler may call are _Exit()
or abort()
, or it may call signal()
on the signal currently being handled, and if signal()
fails, the value of errno
is indeterminate.
This rule is a special case of SIG31-C. Do not access shared objects in signal handlers. The object designated by errno
is of static storage duration and is not a volatile sig_atomic_t
. As a result, performing any action that would require errno
to be set would normally cause undefined behavior. The C Standard makes a special exception for errno
in this case, saying the only thing that is allowed to go wrong is that errno
can take on an indeterminate value. This special exception makes it possible to call signal()
from within a signal handler without risking unrestricted undefined behavior, but the handler, and any code executed after the handler returns, must not depend on the value of errno
being meaningful.
...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <signal.h> #include <stdlib.h> #include <string.h> typedef void (*pfv)(int); void handler(int signum) { pfv old_handler = signal(signum, SIG_DFL); if (old_handler == SIG_ERR) { perror("SIGINT handler"); /* undefined behavior */ /* handleHandle error condition */ } } int main(void) { pfv old_handler = signal(SIGINT, handler); if (old_handler == SIG_ERR) { perror("SIGINT handler"); /* handleHandle error condition */ } /* mainMain code loop */ return 0; } |
The call to perror()
from handler()
also violates SIG30-C. Call only asynchronous-safe functions within signal handlers.
Compliant Solution
The compliant solution does not reference errno
and does not return from the signal handler if the signal()
call fails.
Code Block | ||||
---|---|---|---|---|
| ||||
#include <signal.h> #include <stdlib.h> #include <string.h> typedef void (*pfv)(int); void handler(int signum) { pfv old_handler = signal(signum, SIG_DFL); if (old_handler == SIG_ERR) { abort(); } } int main(void) { pfv old_handler = signal(SIGINT, handler); if (old_handler == SIG_ERR) { perror("SIGINT handler"); /* handleHandle error condition */ } /* mainMain code loop */ return 0; } |
Noncompliant Code Example (POSIX)
POSIX is less restrictive than C about what applications can do in signal handlers. It has a long list of asynchronous-safe functions that can be called. (See SIG30-C. Call only asynchronous-safe functions within signal handlers.) Many of these functions set errno
on error, which can lead to a signal handler being executed between a call to a failed function and the subsequent inspection of errno
. Consequently, the value inspected is not the one set by that function but the one set by a function call in the signal handler. POSIX applications can avoid this problem by ensuring that signal handlers containing code that might alter errno
always save the value of errno
on entry and restore it before returning.
...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <stddef.h> #include <signal.h> #include <errno.h> #include <sys/wait.h> void reaper(int signum) { errno = 0; for (;;) { int rc = waitpid(-1, NULL, WNOHANG); if ( (0 == rc) || (-1 == rc && EINTR != errno) ) break; } if (ECHILD != errno) { /* handleHandle error */ } } int main(void) { struct sigaction act; act.sa_handler = reaper; act.sa_flags = 0; if (sigemptyset(&act.sa_mask) != 0) { /* handleHandle error */ } if (sigaction(SIGCHLD, &act, NULL) != 0) { /* handleHandle error */ } /* ... */ return 0; } |
...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <stddef.h> #include <signal.h> #include <errno.h> #include <sys/wait.h> void reaper(int signum) { int save_errno = errno; errno = 0; for (;;) { int rc = waitpid(-1, NULL, WNOHANG); if ( (0 == rc) || (-1 == rc && EINTR != errno) ) break; } if (ECHILD != errno) { /* handleHandle error */ } errno = save_errno; } int main(void) { struct sigaction act; act.sa_handler = reaper; act.sa_flags = 0; if (sigemptyset(&act.sa_mask) != 0) { /* handleHandle error */ } if (sigaction(SIGCHLD, &act, NULL) != 0) { /* handleHandle error */ } /* ... */ return 0; } |
...
Tool | Version | Checker | Description |
---|---|---|---|
Compass/ROSE |
|
| Could detect violations of this rule by looking for signal handlers that themselves call |
...
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Related Guidelines
...
Bibliography
...
] | Section 7.14.1.1, "The signal |
...
Function" |
...
...