CL 10: C Development Environment (15 pts)

What You Need for This Project

Purpose

To prepare a Linux machine with the required tools to compile C programs for both 32-bit and 64-bit code Intel architectures, and to debug them.

Installing

On your Debian Linux machine, open a Terminal window, execute these commands:
sudo apt update
sudo apt install build-essential gcc-multilib gdb -y

Creating a Simple Program

In a Terminal window, execute this command:
nano hello.c
Copy and paste in this code, as shown below:
#include <stdio.h>

int main(void){
        char name[10];
        printf("What is your name?\n");
        scanf("%s", name);
        printf("Hello, %s!\n", name);
}

Save the file with Ctrl+X, Y, Enter.

Execute these commands to compile and run the program:

gcc -fno-stack-protector -o hello hello.c
./hello
Enter the name FRED and press Enter.

The program runs normally, as shown below.

Run the program again, but enter this name:

MY-NAME-IS-RUMPLESTILTSKIN
You see a "Segmentation fault" message, as shown below.

This is a "buffer overflow" error, caused by storing a long name which wrote data past the end of the 10-character array reserved for that purpose, and overwrote important data used to control program flow.

Debugging the Program

Execute these commands to debug and run the program:
gdb -q hello
run
AAAABBBBCCCCDDDDEEEE
info registers
q
y

CL 10.1: rbx Value (15 pts)

The flag is covered by a green rectangle in the image below.

Posted 1-4-25
Alpine instructions added, including -fno-stack-protector switch 1-10-25
Alpine instructions removed 1-11-25