Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I just installed 12.04 (WUBI actually) and I wanted to learn a bit about buffer overflow. Unfortunately when I try to use a function like puts() and gets() in an incorrect manner (that is, to cause a buffer overflow), gcc tells me that it detected smashing the stack and terminate my program.

How do I enable stack overflow?

For more clarity, This is the program:


    printf("I can never execute\n");

    char buffer[8];



    return 0;
share|improve this question
up vote 1 down vote accepted

I don't think what you are doing is a stack overflow. That is what happens when you recurse too deeply into a function, causing you to run out of stack frames.

int main(int argc, char *argv[]) {
    return main(argc, argv);

What you are trying to achieve is a buffer overflow on the stack. I think you are hoping to write to the return address and then make it call CanNeverExecute.

Then you get stack smashing detected, from gcc's stack protector. You can disable it using gcc -fno-stack-protector. Of course, don't ever do that with programs you intend on running for production.

share|improve this answer
Thank you very very much, that's what i needed! Ofc i won't do it with normal programs, i'm just try to learn this right now :) – AscaL Aug 13 '12 at 11:41

First of all, using any function in an incorrect manner is not recommended. Even if you do so, most of the times the code will not work.

Also, as far as I know "Stack Overflow" is a runtime error, which usually occurs when you try to store something which has a size larger than the size of the allotted stack.

share|improve this answer
Maybe i didn't explain myself, i DO want a buffer overflow to happen, because i want to learn how it works. i do know is not recommended, and that's why there is a restriction on it. Thanks for the answer – AscaL Aug 13 '12 at 8:54

You can not "enable stack overflow" - this is a name of a class of errors which can happen in C and other languages. See Wikipedia article for details.

So, basically, stack overflow is what's happening with your program and what makes the program to terminate.

In a simple program, either staring at the code long enough until you see the error or using a debugger (called gdb) to go through the program step by step should help you to find the problem.

share|improve this answer
No no, my problem is that I WANT the buffer overflow to happen! I'm trying to learn something and i want to be able to access the memory from a program. I wanna go outside the buffer size and write in the stack. The problem is that gcc is preventing me to do it since it recognize the error! i need a way around that. Thank you for the answer. – AscaL Aug 13 '12 at 8:57

Do check this string to find almost all possible ways to cause Stack overflows in many languages-

share|improve this answer
Thank you very much, but i am doing it in C and need a simple program that i can understand... Is a very fun read tho :) – AscaL Aug 13 '12 at 9:06
By the way, this question looks more like one for stackoverflow. You will get more answers there. You could upvote my answer if it helped you in any way. ;) – saji89 Aug 13 '12 at 9:25
Unfortunately i cannot up-vote as i don't have enough rep, or I would, sorry man. – AscaL Aug 13 '12 at 12:33
No problems man. Glad to have helped in some way. :) – saji89 Aug 14 '12 at 3:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.