...
Compliant Solution (POSIX)
The POSIX defines the sigaction(2)
function , which assigns handlers to signals like in a similar manner to the C99 signal(2)
function, but also allows one signal masks to be set explicitly set persistence. One can thus use . Consequently, sigaction(2)
and sidestep )
can be used to eliminate the race window on non-persistent OS'senvironments.
Code Block | ||
---|---|---|
| ||
/* Equivalent to signal( SIGUSR1, handler); but make signal persistent */ struct sigaction act; act.sa_handler = &handler; act.sa_flags = 0; if (sigfillset( &act.sa_mask) != 0) { /* handle error */ } if (sigaction(SIGUSR1, &act, NULL) != 0) { /* handle error */ } |
In fact, POSIX recommends sigaction(2)
and deprecates signal(2)
. Unfortunately, sigaction(2)
is not C99-compliantdefined in C99 and is consequently not as portable a solution.
Non-Persistent Handlers
Errors may also occur when the developer expects the default action to be restored for a signal, but instead, the signal handler persists.
...
There is no race condition that can be utilizied exploited by an attacker in sending a second signal here, because a second signal sent to the handler before it calls signal()
will merely cause it to restart, and call signal()
anyway.
Compliant Solution (POSIX)
The POSIX defines the sigaction(2)
function , which assigns handlers to signals like in a similar manner to the C99 signal(2)
function, but also allows one signal masks to be set explicitly set persistence. One can thus use . Consequently, sigaction(2)
and sidestep )
can be used to eliminate the race window on non-persistent OS'senvironments.
Code Block | ||
---|---|---|
| ||
/* Equivalent to signal( SIGUSR1, handler); */ but make signal non-persistent */ struct sigaction act; act.sa_handler = &handler; act.sa_flags = SA_RESETHAND; if (sigemptyset( &act.sa_mask) != 0) { /* handle error */ } if (sigaction(SIGUSR1, &act, NULL) != 0) { /* handle error */ } |
...
Risk Assessment
Failure to understand implementation-specific details regarding signal handler persistence can lead to unexpected behavior.
...