简体   繁体   中英

*** stack smashing detected ***: <unknown> terminated Aborted (core dumped) Error only occurring sometimes?

I am completing a homework assignment (I can't post the code because of this), and I get this runtime error very infrequently:

* stack smashing detected * : terminated Aborted (core dumped)

When I run the executable again after, and everything works fine. Is there a reason why this error only shows up sometimes? For reference, the assignment I am trying to complete has us loading data from files into two vectors, and conducting binary and linear searches on the data, to see if a data from vector 1 appears in vector 2.

Thank you!

EDIT: Additional info: When I am getting this error, nothing about the input data changes. I could run the exact same executable, with the exact same input data one time and have it work, run it a second time, get the stack smashing error, and then run the exact same thing, and have it work fine again.

*** stack smashing detected *** error occurs when as the name suggests, you smash the stack , meaning that you have a buffer overflow and the canary gets overwritten by a different value. This is a security mechanism implemented by gcc/g++ to prevent buffer overflow exploits using -fstack-protector .

To avoid this error, disable fstack-protector in gcc while compiling the code using

g++ myProgram.c -o myProgram -fno-stack-protector

Edit 1
However, disabling stack-protection will remove this error but you might get segmentation fault as a result of overwriting stack.

If it is a computer security assignment where you are working on a buffer overflow exploit then you need to figure out to bybass these security mechanism, if you are not familiar with it, then somehow you are overflowing buffer and without looking at code I can't comment much where exactly the problem is.

to see if a data from vector 1 appears in vector 2 this indeed seems like buffer overflow kind of assignment where you are required to overwrite contents of arrays from one another. The fact this error is not consistent because sometimes the canary does not get overwritten (fine run of program) or overwritten by the same exact value and sometimes the canary gets overwritten with a different value leading to this error.

You need to configure your compiler to make it easier for buffer overflow.

Edit 2

Your program behaviour is random because probably you have not disabled ASLR (Address space layout randomization). When you compile your program your compiler gcc/g++ optimize your executable for security mechanisms to prevent buffer overflow exploits.

Address space layout randomization (ASLR) is a computer security technique involved in preventing exploitation of memory corruption vulnerabilities. In order to prevent an attacker from reliably jumping to, for example, a particular exploited function in memory, ASLR randomly arranges the address space positions of key data areas of a process, including the base of the executable and the positions of the stack, heap and libraries.

It means the address space allocation is random and the bytes between your vectors is different every time. Sometimes the overflown buffer does not reaches to canary and sometimes it does. To get the consistent behaviour every time, you need to disable ASLR as well. ASLR support is provided by your OS. To disable ASLR, on linux it is disabled by setting randomize_va_space to 0. It can be achieved by

echo 0 > /proc/sys/kernel/randomize_va_space

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM