1

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:

#include<stdio.h>

CanNeverExecute()
{
    printf("I can never execute\n");
    exit(0);
}

GetInput()
{
    char buffer[8];

    gets(buffer);
    puts(buffer);
}

main()
{
    GetInput();

    return 0;
}

4 Answers 4

2

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.

1
  • 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, 2012 at 11:41
0

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.

1
  • 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, 2012 at 8:54
0

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.

1
  • 1
    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, 2012 at 8:57
0

This question on Stack Overflow lists almost all possible ways to cause Stack overflows in many languages.

1
  • 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, 2012 at 9:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .