1740
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 (CWE-126) if the
program is called without any arguments, because argc would be 0, and argv[1] would point
outside the bounds of the array.
Example 2:
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.
Example Language: C (Bad)
#include <signal.h>
#include <syslog.h>
#include <string.h>
#include <stdlib.h>
void *global1, *global2;
char *what;
void sh (int dummy) {
syslog(LOG_NOTICE,"%s\n",what);
free(global2);
free(global1);
/* Sleep statements added to expand timing window for race condition */
sleep(10);
exit(0);
}
int main (int argc,char* argv[]) {
what=argv[1];
global1=strdup(argv[2]);
global2=malloc(340);
signal(SIGHUP,sh);
signal(SIGTERM,sh);
/* Sleep statements added to expand timing window for race condition */
sleep(10);
exit(0);
}
However, the following sequence of events may result in a double-free (CWE-415):
1. a SIGHUP is delivered to the process
2. sh() is invoked to process the SIGHUP
3. This first invocation of sh() reaches the point where global1 is freed
4. At this point, a SIGTERM is sent to the process
5. the second invocation of sh() might do another free of global1
6. this results in a double-free (CWE-415)
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
Reference Description
CVE-2008-4109 Signal handler uses functions that ultimately call the unsafe syslog/malloc/
s*printf, leading to denial of service via multiple login attempts
https://www.cve.org/CVERecord?id=CVE-2008-4109
CWE Version 4.14
CWE-829: Inclusion of Functionality from Untrusted Control Sphere
CWE-829: Inclusion of Functionality from Untrusted Control Sphere