Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stack corruption in C++

In C++, in which way the stack may get corrupted. One way I guess is to overwriting the stack variables by accessing an array beyond its boundaries. Is there any other way that it can get corrupted?

like image 253
Naveen Avatar asked Apr 05 '09 07:04

Naveen


People also ask

What is stack corruption in C?

Stack corruption can also be suspected when a passed parameter seems to have a value different from the one passed by the calling function. When a stack corruption is detected, one should look at the local variables in the called and calling functions to look for possible sources of memory corruption.

What causes memory corruption in C?

Pointer variable can point to a invalid memory location which can cause access violation and a crash. Memory corruption may occur because of poor array buffer handling or some abnormal runtime use-cases.

Which of the below function can cause stack corruption?

A stack corruption can be caused by buffer overflows on local variables because the return address and data for the calling functions are stored in addresses higher than the addresses for locals.

What causes heap corruption?

Heap corruption occurs when a program damages the allocator's view of the heap. The outcome can be relatively benign and cause a memory leak (where some memory isn't returned to the heap and is inaccessible to the program afterward), or it may be fatal and cause a memory fault, usually within the allocator itself.


1 Answers

  1. You could have a random/undefined pointer that ends up pointing to the stack, and write though that.
  2. An assembly function could incorrectly setup/modify/restore the stack
  3. Cosmic waves could flips bits in the stack.
  4. Radioactive elements in the chip's casing could flip bits.
  5. Anything in the kernel could go wrong and accidentally change your stack memory.

But those are not particular to C++, which doesn't have any idea of the stack.

like image 61
Douglas Leeder Avatar answered Oct 04 '22 07:10

Douglas Leeder