Is writing to an already freed memory a vulnerability? x = malloc(200); /* do something with x / free (x); / do something else */ strcpy(x, “somedata”);
-
Overwriting freed memory is a security vulnerability
-
Depends on the application and how important “somedata” is
-
This will result in a buffer overflow since the freed memory location cannot handle 8 characters of data “somedata”
-
strcpy() will fail as it cannot write to already freed memory, and the application will crash
Writing to freed memory (use-after-free) is a security vulnerability because the freed memory may have been reallocated for other purposes. An attacker could potentially manipulate the freed memory between free() and the strcpy, causing the strcpy to corrupt critical data structures, inject shellcode, or cause other security issues. Option A is correct. Option B is wrong - it IS a vulnerability regardless of data importance. Option C is wrong - 8 characters fit fine in 200 bytes. Option D is wrong - strcpy doesn't detect freed memory and will succeed, causing the vulnerability.
Writing to memory after it has been free()d is a classic use-after-free bug and is a well-known security vulnerability class, since the memory may have already been reused by the allocator for another object, letting the write corrupt unrelated data or heap metadata. It doesn't reliably crash or fail — strcpy has no way of knowing the buffer was freed — so the outcome is undefined behavior, not a guaranteed overflow or a guaranteed failure.