Thread: Stack buffer overflow - how does it work?

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    6

    Stack buffer overflow - how does it work?

    hi, I've encountered this piece of code:

    Code:
    #include <stdio.h>
    #include <windows.h>
    void g() {
      printf("In g()!\n");
      system("pause");
      exit(0);
    }
     
    int f(int x)
    { 
    	int localStack;
    	int *pStack = &localStack;
     
    	for(int i=0; i<x; i++)
    	{
           *pStack = (int)&g;	
    		pStack++; 
    	}
     
     return 0;
    } 
     
    int main() {   
    f(3);
    system("pause"); 
    return 0; 
    }
    I ddin't really get how the whole thing works.
    I'd like to get an explanation.

    Thank you

  2. #2
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    A stack is essentially a program or a routine's "to-do list". Values it needs to use, as well as local variables, are stored on the stack. The program and each subroutine has its own stack, for global and local variables. When you put something on the stack, it's called a "push", and when you take something off the stack, it's called a "pop". Consider the following piece of code.

    Code:
    a() {
        b();
    }
    
    b() {
        a();
    }
    These two functions call each other recursively, and every time a function is called, memory (a stack) is allocated for it. When you run out of memory, a stack overflow occurs. Read more about it here.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by 0xIDE View Post
    hi, I've encountered this piece of code:

    I ddin't really get how the whole thing works.
    That does not work at all for me, but I'm pretty sure I know what someone (with very poor C skills) was trying to do.

    Code:
    	int localStack;
    	int *pStack = &localStack, i;
                                 
    	for(i=0; i<x; i++)
    	{
    		*pStack = (int)&g;
    		pStack++; 
    	}
    Initially, pStack is set to the address of a local variable, "localStack". That's fine, but pointless since it is then set to a completely different address, that of the function g, which is not a stack address, it's a heap address. If you add this to the beginning of f():

    Code:
    	fprintf(stderr,"%p %p\n", pStack, &g);
    You'll (probably) see they are very different. So now pStack (really, pHeap, lol) starts to step through the program's memory. Most likely causing a segmentation fault.

    Try this:

    Code:
    #include <stdio.h>
    #include <unistd.h> // something else on windows
    
    void g() {
    	int x = 0xdeadbeef;
    }
    
    int main() {
    	unsigned char *p = &g;
    
    	while (1) {
    		fprintf(stderr,"%p %x\n", p, *p);
    		p++;
    		sleep(1);  // on windows, use sleep(500)
    	}
    
    	return 0; 
    }
    This will cause a seg fault too eventually, when p steps off the end of the program's memory and the OS shuts it down. But before that, you will see something like this:

    0x4005d4 55
    0x4005d5 48
    0x4005d6 89
    [...]
    0x4005db ef
    0x4005dc be
    0x4005dd ad
    0x4005de de

    0x4005df c9
    0x4005e0 c3
    [...]

    Notice that the part in red is the value of x from g(). The bytes appear backward because that's how ints are stored ("little endian"). This is not really the stack, however, because this:

    Quote Originally Posted by Babkockdood View Post
    The program and each subroutine has its own stack,
    is not literally true. The program only has one stack, and when a function is called, that stack is filled with the values for that function, which come from the heap.

    Now try this:

    Code:
    #include <stdio.h>
    
    void g() {
    	int x = 0xdeadbeef, n;
    	unsigned char *p = &x;
    
    	while (1) {
    		n = *p;
    		fprintf(stderr,"stack %p %x\n", p, n);
    		if (*p == 0xde) break;
    		p++;
    	}
    }
    
    int main() {
    	unsigned char *p = &g;
    
    	while (1) {
    		fprintf(stderr,"heap %p %x\n", p, *p);
    		if (*p == 0xde) {  // end of x
    			g();
    			return 0;
    		}
    		p++;
    	}
    
    	return 0; 
    }
    Example output:

    [...]
    heap 0x40058d c7
    heap 0x40058e 45
    heap 0x40058f e0
    heap 0x400590 ef
    heap 0x400591 be
    heap 0x400592 ad
    heap 0x400593 de

    stack 0x7fff9187f510 ef
    stack 0x7fff9187f511 be
    stack 0x7fff9187f512 ad
    stack 0x7fff9187f513 de



    Notice we are looking at the same value in memory with two different addresses, one when the variable is considered on the heap and then when it has been "placed" on the stack (or, more literally I think, the stack has been "placed" on it).
    Last edited by MK27; 08-28-2011 at 11:13 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    We dont generally allow these topics to get discussed on these boards, so I'm going to close the thread.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Buffer overflow gone wrong
    By Wolf` in forum C Programming
    Replies: 13
    Last Post: 08-09-2009, 10:00 AM
  2. Best way to prevent buffer-overflow
    By Siphon in forum C Programming
    Replies: 1
    Last Post: 01-01-2007, 11:53 AM
  3. buffer overflow
    By cpp_is_fun in forum C Programming
    Replies: 2
    Last Post: 10-24-2006, 11:04 PM
  4. Buffer Overflow - Stopping this
    By RoD in forum Windows Programming
    Replies: 9
    Last Post: 09-25-2002, 09:58 PM