Thread: Buffer Overflow Question(From a Book)

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    5

    Buffer Overflow Question(From a Book)

    I bought a book to understand how programs work, stacks, heaps, etc. I'm still on the introduction part where it's giving you an example of a buffer overflow. The only problem is that I can't understand why my output won't match the book. I would understand it if it worked but...I can't put the pieces together. Shed some light if you could.
    Code:
    #include <stdio.h>
    #include <string.h>
    int main(int argc, char *argv[]) {
       int value = 5;
       char buffer_one[8], buffer_two[8];
       
       strcpy(buffer_one, "one"); /* Put "one" into buffer_one. */
       strcpy(buffer_two, "two"); /* Put "two" into buffer_two. */
       
       printf("[BEFORE] buffer_two is at %p and contains \'%s\'\n", buffer_two, buffer_two);
       printf("[BEFORE] buffer_one is at %p and contains \'%s\'\n", buffer_one, buffer_one);
       printf("[BEFORE] value is at %p and is %d (0x%08x)\n", &value, value, value);
       
       printf("\n[STRCPY] copying %d bytes into buffer_two\n\n",  strlen(argv[1]));
       strcpy(buffer_two, argv[1]); /* Copy first argument into buffer_two. */
       
       printf("[AFTER] buffer_two is at %p and contains \'%s\'\n", buffer_two, buffer_two);
       printf("[AFTER] buffer_one is at %p and contains \'%s\'\n", buffer_one, buffer_one);
       printf("[AFTER] value is at %p and is %d (0x%08x)\n", &value, value, value); 
    }
    Here are the results I have.

    [BEFORE] buffer_two is at 0x7fff494e9210 and contains 'two'
    [BEFORE] buffer_one is at 0x7fff494e9220 and contains 'one'
    [BEFORE] value is at 0x7fff494e920c and is 5 (0x00000005)

    [STRCPY] copying 10 bytes into buffer_two

    [AFTER] buffer_two is at 0x7fff494e9210 and contains '1234567890'
    [AFTER] buffer_one is at 0x7fff494e9220 and contains 'one'
    [AFTER] value is at 0x7fff494e920c and is 5 (0x00000005)

    Here is what it's supposed to look like.

    [BEFORE] buffer_two is at 0xbffff7f0 and contains 'two'
    [BEFORE] buffer_one is at 0xbffff7f8 and contains 'one'
    [BEFORE] value is at 0xbffff804 and is 5 (0x00000005)
    [STRCPY] copying 10 bytes into buffer_two
    [AFTER] buffer_two is at 0xbffff7f0 and contains '1234567890'
    [AFTER] buffer_one is at 0xbffff7f8 and contains '90'
    [AFTER] value is at 0xbffff804 and is 5 (0x00000005)

    Notice how in the after section buffer_one isn't getting overwritten

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The only way '1234567890' is going to end up there is if you actually called your program with that as an argument from the command line. Did you?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    5
    Yeah I did. You can see in both examples that it started off as two and went to 1234567890 But it doesn't overflow into the next memory char array

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    The problem is that when you have a buffer overflow, you get undefined behavior, but your book is apparently expecting some specific behavior.

    There is absolutely no guarantee that buffer_one and buffer_two will be adjacent, and if they are, there is no requirement that buffer_one be right after buffer_two: it could be the other way around. I have compilers that lay them out both ways.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Mmm, does this book mention any specific compiler in the title, or in the introduction?

    If so, it probably isn't worth reading.
    Books written with reference to one specific compiler all too often document the compiler, and NOT the language. Any decent book would have told you to expect absolutely ANYTHING from your tests, not a specific result.

    Now consider how many other "Well, this compiler does this...." statements which are in the book, but presented as flawed "the language does..." statements. Do you still want to waste time learning these?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Novice
    Join Date
    Jul 2009
    Posts
    568
    You can see by memory addresses that buffer_one and buffer_two are actually 10 bytes apart in your example, but 8 bytes apart in the book's example. I can't say why that is, but this is the reason why you don't see the expected overlap.

    However, the following code can give you a more reproducible behavior.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    typedef struct block
    {
      char buffer_two[8];
      char buffer_one[8];
    } BLOCK;
    
    
    int main(int argc, char **argv)
    {
      BLOCK b;
      strcpy(b.buffer_one, "one");
      strcpy(b.buffer_two, "two");
    
      printf("[BEFORE] buffer_two is at %p and contains \'%s\'\n", &b.buffer_two, b.buffer_two);
      printf("[BEFORE] buffer_one is at %p and contains \'%s\'\n", &b.buffer_one, b.buffer_one);
    
      printf("\n[STRCPY] copying %d bytes into buffer_two\n\n",  strlen(argv[1]));
    
      printf("[AFTER] buffer_two is at %p and contains \'%s\'\n", &b.buffer_two, b.buffer_two);
      printf("[AFTER] buffer_one is at %p and contains \'%s\'\n", &b.buffer_one, b.buffer_one);
    
      return 0;
    }
    This is because a structure will be allocated as a single block of memory, rather then two as in your example. Caveat is that this could again depend on the compiler to work.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The point is, you probably won't get the same output as the book because the behavior is undefined. That is, we cannot be sure what will happen or when and how.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-20-2011, 01:39 PM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. buffer overflow
    By cpp_is_fun in forum C Programming
    Replies: 2
    Last Post: 10-24-2006, 11:04 PM
  4. Console Screen Buffer
    By GaPe in forum Windows Programming
    Replies: 0
    Last Post: 02-06-2003, 05:15 AM
  5. Buffer Overflow - Stopping this
    By RoD in forum Windows Programming
    Replies: 9
    Last Post: 09-25-2002, 09:58 PM