Thread: C Buffer Overflow Problem

  1. #1
    Registered User
    Join Date
    May 2016
    Posts
    1

    Question C Buffer Overflow Problem

    Hey,
    I am having a problem with a very simple code I have written to test buffer overflows in C. There are two versions of my code, a version that should be (and is) vulnerable to buffer overflows and one that should not be vulnerable.
    This is the vulnerable version:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int check(char *password) {
        
        int auth_flag = 0;
        char password_buffer[16];
        
        strcpy(password_buffer, password);
        
        
        if(strcmp(password_buffer, "password") == 0)
            auth_flag = 1;
        if(strcmp(password_buffer, "testest") == 0)
            auth_flag = 1;
        
        return auth_flag;
        
    }
    
    int main(int argc, char *argv[]) {
        if(argc < 2) {
            printf("Syntax: %s <password>\n", argv[0]);
            exit(0);
        }
        if(check(argv[1])) {
            printf("Access Granted.\n");
        } else {
            printf("\nAccess has been Denied.\n");
        }
    }
    The program actually works, when I enter the correct passwords it prints "Access Granted" like it is supposed to do and when I enter a long string, my password_buffer overflows into the auth_flag, changing its value and also granting access.
    Then I tried to create a version without that vulnerability by switching my variables like that:
    Code:
    int check(char *password) {
        
        char password_buffer[16];
        int auth_flag = 0;
        
        strcpy(password_buffer, password);
        (...)
    My idea was, that when I switch the variables in my code, auth_flag should be located in memory before password_buffer, so it could not be overwritten.
    And that is my problem: it does not work. I can still overflow into the flag and when I look into the assembler code, nothing really changes. I know that I could fix this problem by simply making both variables global, but I have already seen in someone else's code, who did the same thing to prevent overflows, that it should work the way I tried it.
    Has anybody an idea what is wrong?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It is implementation-defined how variables will be laid out in memory, so the correct fix is to check the length, not to reorder the declaration of variables.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    The compiler isn't obligated to order variables in any particular way. It's possible that it orders them the same way in both versions. In fact, that's what it does for me (Ubuntu 14.04, gcc 4.8.4), putting auth_flag before password_buffer so I can't overflow into it in either case. Try putting the following before the strcpy:
    Code:
        printf("%p %p\n", (void*)&password_buffer, (void*)&auth_flag);
    An obvious way to avoid overflow in this case is:
    Code:
    int check(char *password) {
        return strcmp(password, "password") == 0
            || strcmp(password, "testest") == 0;
    }
    (But you probably know that!)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 01-03-2014, 01:05 PM
  2. Buffer Overflow
    By AlexTank853 in forum C Programming
    Replies: 3
    Last Post: 09-25-2013, 04:14 PM
  3. buffer overflow
    By cpp_is_fun in forum C Programming
    Replies: 2
    Last Post: 10-24-2006, 11:04 PM
  4. buffer overflow problem
    By Renski in forum C++ Programming
    Replies: 2
    Last Post: 05-22-2003, 08:15 AM

Tags for this Thread