Blog | G5 Cyber Security

Fix Buffer Overflow (Segmentation Fault)

TL;DR

A segmentation fault at 0x90909090 usually means your program tried to write data outside the memory it’s allowed to use – a buffer overflow. This guide shows you how to find and fix these errors, focusing on common causes and practical solutions.

Understanding Buffer Overflows

Buffer overflows happen when you copy more data into a fixed-size block of memory (a ‘buffer’) than it can hold. This overwrites other parts of your program’s memory, leading to crashes or even security vulnerabilities. The address 0x90909090 is often seen because it represents NOP instructions, frequently used in exploit development.

Step-by-Step Fix

  1. Identify the Vulnerable Code: Use a debugger (like GDB) to pinpoint where the crash occurs. Set a breakpoint near the suspected area of memory access.
    gdb ./your_program
    break function_name

    Run your program with sample input that causes the crash.

  2. Examine Stack Frames: When the program crashes, use the debugger to inspect the stack frames and registers.
    bt

    This shows you the call stack – which functions were called before the crash. Look for function calls that handle user input or copy data into buffers.

  3. Check Buffer Sizes: Carefully review the code where data is copied. Make sure the destination buffer is large enough to hold all possible input.
    • C/C++: Use functions like strncpy, snprintf, or fgets instead of strcpy, sprintf, and gets. These safer alternatives allow you to specify the maximum number of characters to copy.
      char dest[10];
      strncpy(dest, src, sizeof(dest) - 1);
      dest[sizeof(dest) - 1] = ' '; // Always null-terminate!
    • Python: Avoid unbounded string concatenation. Use slicing or other methods to control the length of strings.
      # Bad:
      result += user_input  # Potential overflow if user_input is too long
      
      # Good:
      result = result[:max_length] + user_input
  4. Input Validation: Validate all input before copying it into buffers. Check the length, format, and content of the data to ensure it’s within acceptable limits.
    if (strlen(user_input) > MAX_INPUT_LENGTH) {
      // Handle error - don't copy!
    }
  5. Use Compiler Protections: Enable compiler features that help detect and prevent buffer overflows.
    • Stack Canaries: These are random values placed on the stack before function return addresses. If a buffer overflow overwrites the canary, the program detects it.
      gcc -fstack-protector your_program.c -o your_program
    • Address Space Layout Randomization (ASLR): This randomizes the memory addresses of key program components, making it harder for attackers to exploit buffer overflows.
      gcc -fPIE -pie your_program.c -o your_program
  6. Test Thoroughly: Test your code with a variety of inputs, including very long strings and unexpected characters, to ensure the fix is effective.
  7. Consider Static Analysis Tools: Use tools like Coverity or SonarQube to automatically scan your code for potential buffer overflows. These can identify issues you might miss during manual review.

Important Notes

Exit mobile version