Use After Free
Overview
Use-after-free (UAF) occurs when a program continues to use a pointer after the memory it points to has been freed. This can lead to:
- Memory corruption
- Arbitrary code execution
- Privilege escalation
- Information disclosure
UAF vulnerabilities are common in browsers, kernels, and network services.
Detection Strategy
free(ptr)followed by accessingptrwithout setting it toNULLdelete ptrin C++ followed by further dereference- Returning a pointer to freed memory
Remediation
- Set pointers to
NULLimmediately afterfree() - Use smart pointers in C++ (
unique_ptr,shared_ptr) - Use memory-safe languages for new projects
Vulnerable:
char *buf = malloc(256);
free(buf);
strcpy(buf, input); // Use after free!
Safe:
char *buf = malloc(256);
free(buf);
buf = NULL; // Prevent use after free