This vulnerability occurs when a function returns a pointer to its own local variable. Since that variable's memory is on the stack, the pointer becomes invalid as soon as the function finishes, leading to crashes or unpredictable behavior.
When a function declares a local variable, it's stored in a temporary memory region called the stack. This stack space is only reserved for the lifetime of that function call. Once the function returns, its stack frame is cleared and that memory is marked as available for the next function call. If you return a pointer to this now-freed location, you're handing the calling code a 'dangling pointer' to a memory address that is no longer guaranteed to hold your intended data. The program may continue to run, but the next function that executes will likely reuse that same stack address for its own local variables, overwriting whatever value was there. Any subsequent attempt to read or write through the old pointer will access this new, unrelated data, causing corruption, logic errors, or most commonly, a sudden segmentation fault when the program tries to dereference the invalid pointer.
Impact: Read MemoryModify MemoryExecute Unauthorized Code or CommandsDoS: Crash, Exit, or Restart
If the returned stack buffer address is dereferenced after the return, then an attacker may be able to modify or read memory, depending on how the address is used. If the address is used for reading, then the address itself may be exposed, or the contents that the address points to. If the address is used for writing, this can lead to a crash and possibly code execution.
c