The following code is for security research and authorized testing only.
python
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Simulated PoC for Double Free Vulnerability
// This code demonstrates the logic leading to a double free condition.
void trigger_vulnerability(char* user_input) {
char* buffer = (char*)malloc(100);
if (buffer == NULL) return;
printf("Buffer allocated at: %p\n", buffer);
// First free: Freed under certain condition
if (strlen(user_input) > 0) {
free(buffer);
printf("Buffer freed for the first time.\n");
}
// Second free: Logic error causes double free
// In a real scenario, this might happen in a different function path
if (strcmp(user_input, "crash") == 0) {
free(buffer); // Double Free occurs here
printf("Buffer freed for the second time - CRASH!\n");
}
}
int main() {
// Malicious input triggering the vulnerable path
char input[] = "crash";
trigger_vulnerability(input);
return 0;
}