Wiki Markup |
---|
According to \[[ISO/IEC 9899-1999| AA. Bibliography#ISO/IEC 9899-1999]\] (see [undefined behavior 126| CC. Undefined Behavior#ub_126] of Annex J), the behavior a program is [undefined| BB. Definitions#undefined behavior] 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 126 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 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 rule SIG31-C. Do not access or modify 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
takes on an indeterminate value. This 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.
...
The call to perror()
from handler()
also violates rule SIG30-C. Call only asynchronous-safe functions within signal handlers.
...
POSIX is less restrictive than C99 about what applications can do in signal handlers. It has a long list of asynchronous-safe functions that can be called. (see See rule SIG30-C. Call only asynchronous-safe functions within signal handlers.) . Many of these functions set errno
on error. This can lead to a situation where a signal handler is executed in between a call to a failed function and the subsequent inspection of errno
, and, 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 that contain code that might alter errno
always save the value of errno
on entry and restore it before returning.
The signal handler in this noncompliant code example alters the value of errno
, and, as a result, it can cause incorrect error handling if executed in between a failed function call and the subsequent inspection of errno
.
...
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
ERR32-C | low | unlikely | low | P3 | L3 |
Automated Detection
Tool | Version | Checker | Description | ||||
---|---|---|---|---|---|---|---|
|
|
|
|
...
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Other Languages
Related Guidelines
CERT This rule appears in the C++ Secure Coding Standard as : ERR32-CPP. Do not rely on indeterminate values of errno.
...
\[[ISO/IEC 9899:1999|AA. Bibliography#ISO/IEC 9899-1999]\] Section 7.14.1.1, "The signal function" Wiki Markup
Bibliography
...
ERR31-C. Don't redefine errno 12. Error Handling (ERR) ERR33-C. Detect and handle errors