TExcelTAD
Home › Memory Safety & Binary Exploitation Concepts › Memory Safety: Introduction and Scope

Memory Safety: Introduction and Scope

A C program is trusted to stay inside the memory it was given. When it writes one byte past the end of a buffer, reads a pointer after the object behind it was freed, or multiplies two lengths into a number that no longer fits, nothing stops it: the bytes next door belong to another variable, the allocator's bookkeeping or the address the function will return to. Those bugs are called memory-safety bugs, and they are the largest single source of serious vulnerabilities in operating systems, browsers and network software. This course teaches you to recognise them in code, to make a tool point at them, to understand why an attacker cares, and to fix them so they stay fixed.

Before anything else: authorization and scope

This course belongs to the offensive half of the track. It explains how memory corruption is turned into control of a program, because defenders who don't understand that can't judge how bad a crash is. The same knowledge aimed at software you have no right to test is a crime in most countries, whatever the intention:

Only test programs and systems you own, or that you have written permission to test, within the agreed scope.

Read these lessons of Security Foundations first; this course builds on them and doesn't repeat them:

Fuzzing or debugging a program you downloaded is usually legal on your own machine, but licences, anti-circumvention law and bug-bounty rules vary. When in doubt, ask the vendor or stay with the toy programs of this course.

How this course stays on the right side of the line

The track's safety line is binding, and this course applies it strictly:

  1. Every bug is a toy the lesson writes. Each bug class is a short C or C++ program of a few lines, compiled in your sandbox and caught by a sanitizer. No lesson targets real software.
  2. Exploitation is a concept, not code. You will see what an overflow overwrites (on a pure-Python model of a stack frame whose bytes are labelled) and which mitigation notices. There is no shellcode, no ROP chain or gadget list, no payload with real addresses, and no exploit for any real CVE.
  3. Every attack idea ends at detection and the fix. A lesson goes bug → sanitizer report → root cause → fix → the CWE entry, and the fix is compiled and run again.

How the examples run

  • ```bash run blocks are recorded sandbox sessions made with real gcc 13.3, clang 18, gdb 15 and checksec 2.6 in a container with no network and no root. Run-time addresses change every run (ASLR); they are masked when the sessions are verified, so your own run will show other numbers in the same places.
  • ```c run and ```cpp run run on Compiler Explorer (gcc 15.2) for programs whose output is deterministic: checked arithmetic, safe string handling, smart pointers.
  • ```python run runs in your browser. It models frames and heaps as bytearrays, so you can watch bytes move without anything being executed.

Here is the whole idea of the course in one sandbox session. A function copies a name into a 16-byte stack buffer without checking its length. AddressSanitizer (-fsanitize=address) watches every memory access and stops the program at the first byte that lands outside the buffer:

bash · try it yourself
cat > hello.c <<'SRC'
#include <stdio.h>
#include <string.h>

static void greet(const char *name) {
    char buf[16];
    strcpy(buf, name);                     /* BUG: no length check */
    printf("hello %s\n", buf);
}

int main(int argc, char **argv) {
    greet(argc > 1 ? argv[1] : "lab");
    return 0;
}
SRC
gcc -g -O0 -fsanitize=address,undefined hello.c -o hello
./hello alice
./hello this-name-is-longer-than-sixteen 2>&1 | grep -E 'ERROR|WRITE|#1 |located in stack|^=>'
echo "exit code: ${PIPESTATUS[0]}"
hello alice
==17==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x796a2a900030 at pc 0x796a2d6f3923 bp 0x7ffe518c8440 sp 0x7ffe518c7be8
WRITE of size 33 at 0x796a2a900030 thread T0
    #1 0x646ea198f365 in greet /home/learner/hello.c:6
Address 0x796a2a900030 is located in stack of thread T0 at offset 48 in frame
=>0x796a2a900000: f1 f1 f1 f1 00 00[f3]f3 00 00 00 00 00 00 00 00
exit code: 1

Four lines tell the story. The error kind is stack-buffer-overflow. The write was 33 bytes: the 32 characters of the name plus the terminating NUL, into a 16-byte buffer. Frame #1 is the line of the bug, hello.c:6, the strcpy. The shadow bytes (00 00 = 16 addressable bytes, f1/f3 = poisoned guard zones around the buffer) show the buffer and the walls that ASan built around it. You will learn to read every part of these reports in Your Toolkit.

The map

Section What you learn
Getting started why memory safety dominates the CVE lists, and the toolkit: sanitizers, gdb, checksec
Memory layout the process address space, stack frames with the saved frame pointer and return address, the heap and glibc's allocator at a conceptual level, how C objects become bytes
Bug classes stack and heap overflows, out-of-bounds reads (Heartbleed as history), off-by-one, use-after-free, double free, leaks, uninitialised memory, integer overflow into allocation sizes, signedness and truncation, format strings, C++ type confusion, data races; each with its report, root cause, fix and CWE
Mitigations canaries, NX and why return-oriented programming exists, ASLR, PIE, RELRO, FORTIFY_SOURCE, CFI, shadow stacks, pointer authentication, memory tagging, hardened allocators, and a table of what each stops and doesn't
Finding bugs compiler warnings, static analysers, sanitizers in CI, a first look at fuzzing
Safer code safe C patterns, modern C++ (RAII, smart pointers, std::span, hardened standard libraries), Rust's ownership model, unsafe and FFI, migration roadmaps
How CVEs get fixed from crash to patch: minimal fix, regression test, advisory, coordinated disclosure
Practice exercises and a full audit of a toy program with seeded bugs

What you should know first

Read C fluently: pointers, arrays and pointers, strings and memory management. The security view of C begins in Programming for Security's C Buffers and C Integers lessons, and Security Foundations' Memory Protection shows NX, ASLR and canaries at the level of an operating-system user. Reading x86-64 machine code and stack frames in a disassembler is taught in Reverse Engineering & Binary Analysis; here you need only the frame layout, which the Stack Frames lesson explains again. If any of this is new, the survey course Cybersecurity is the gentlest start.

Start with Why Memory Safety Dominates Serious CVEs.

Test yourself

What may the runnable examples of this course target?

  1. Any program installed on your machine
  2. Only toy programs the lesson itself writes and compiles in the sandbox
  3. Public CTF servers
  4. Old versions of real software with known CVEs

In the ASan report above, why is the write 33 bytes long?

  1. ASan always reports 33
  2. strcpy copied the 32-character name plus its NUL terminator
  3. The buffer is 33 bytes
  4. The stack is aligned to 32 bytes

How does this course treat exploitation?

  1. As working exploits against toy programs
  2. As concepts: what a bug overwrites, shown on a Python model, and which mitigation detects it
  3. It skips the subject
  4. As shellcode exercises

Which line of an ASan report points you to the bug in your own code?

  1. The shadow-byte legend
  2. The first stack frame in your source file, such as greet /home/learner/hello.c:6
  3. The pc and sp values
  4. The exit code