Input and a write past the end

The program is running and waiting for a name. Type it in the Program input box under the terminal, not at the GDB prompt.

Program input, and observing an out-of-bounds write

Level 8 of 13 · interactive GDB exercise · runs in your browser

Start this exercise

What you will learn

Give input to a program under the debugger, and use raw memory to see what an out-of-bounds write actually modifies on this build.

name holds 8 bytes, but the code lets 32 in. Extra bytes spill onto whatever sits next to it.

The exercise

Send AAAAAAAAB to the program, then look at is_admin.

You work in a real GDB session on the program below. A breakpoint on line 14 was set and the program was started. It is now running, waiting for input.

#include <stdio.h>
#include <string.h>

int main(void) {
    int is_admin = 0;
    char name[8];

    printf("name: ");
    fflush(stdout);
    /* BUG: reads up to 32 bytes into an 8-byte array. */
    if (fgets(name, 32, stdin) == NULL) {
        return 1;
    }
    name[strcspn(name, "\n")] = '\0';

    printf("hello, %s\n", name);
    if (is_admin != 0) {
        printf("admin access granted (is_admin = %d)\n", is_admin);
    }
    return 0;
}

main.c, compiled with gcc -g -O0 -std=c11 -Wall -fno-omit-frame-pointer -fno-stack-protector -no-pie -fno-pie for 32-bit x86 Linux.

GDB commands you will use

print <expr>
Evaluate and show an expression (p total, p &x, p *a, p/x n).
x/<n><fmt><size> <addr>
Examine raw memory, e.g. x/4xw &values (4 hex words), x/8xb &p (8 hex bytes).
run
Start the program from the beginning (r).
continue
Resume until the next breakpoint or exit (c).
print/x <expr>
Print in hexadecimal.
(Program input field)
Sends a line to the program's stdin. Not a GDB command.

Common mistakes

Try it in your browser