Chapter 6: Exploit Mitigations: NX, ASLR, and Stack Canaries

Chapters 4 and 5 built the classic stack-smashing attack: overflow a buffer, overwrite the saved return address, and jump into shellcode that you placed on the stack yourself. To make that work in the lab, we had to switch off a surprising number of things — we compiled with -z execstack -fno-stack-protector -no-pie, and we disabled address randomization in the kernel. Each of those switches turns off a defense that modern systems enable by default.

This chapter turns them back on, one at a time. For each defense we ask the same four questions: what exactly does it do, which step of the attack does it break, what does it cost, and how do attackers get around it? The last question matters as much as the first. None of these mitigations removes the underlying bug — the out-of-bounds write is still there — so each one closes a door and attackers go looking for the next one. That arms race is what drives the following chapters.

Learning objectives. After this chapter you should be able to:

  • Describe the four layers at which buffer-overflow countermeasures are applied: developer, hardware, operating system, and compiler.
  • Explain how a non-executable stack (NX/DEP) stops code injection, how to check and change it for a binary, and why it does not stop code reuse.
  • Explain what ASLR randomizes, what position-independent executables add, and why low entropy and information leaks undermine it.
  • Explain how a stack canary detects an overflow, where the compiler places it, and the attacks a canary does not stop.
  • Use checksec and the relevant gcc flags to determine and control which mitigations a binary has.

6.1 Where Can We Stop the Attack?

Recall the steps of the attack from Chapter 5:

  1. The program copies attacker input into a fixed-size buffer without a length check.
  2. The copy runs past the buffer and overwrites the saved return address.
  3. The attacker knows (or guesses) the address of the shellcode.
  4. When the function returns, the CPU jumps to that address and executes the bytes on the stack as instructions.

Every step is a chance for a defense, and different parts of the system are in a position to provide one:

LayerCountermeasureBreaks step
DeveloperSafer functions, bounds checks, memory-safe languages1
CompilerStack canaries (StackGuard), variable reordering2 (detects the overwrite)
Operating systemAddress space layout randomization (ASLR)3
HardwareNon-executable memory (NX / XD / DEP)4

Defense in depth (Chapter 1) says we should deploy all of them, so that one failure is not fatal. Let us go through them in order.

6.2 Developer Approaches: Don’t Write the Bug

The best fix is not to overflow in the first place. The C standard library contains several functions that copy data with no idea how large the destination is — strcpy, strcat, sprintf, gets, and scanf("%s", ...) — and each has a bounded counterpart that takes a size.

UnboundedBounded alternativePitfall of the alternative
gets(buf)fgets(buf, sizeof buf, stdin)Keeps the trailing newline. (gets was removed from C11 altogether.)
strcpy(d, s)strncpy(d, s, n)Does not NUL-terminate d if strlen(s) >= n.
strcat(d, s)strncat(d, s, n)n is the number of characters to append, not the size of d.
sprintf(d, fmt, ...)snprintf(d, n, fmt, ...)Truncates silently; returns the length it would have written.
strcpy(d, s)strlcpy(d, s, n) (BSD; glibc since 2.38)Truncates silently; always NUL-terminates.

The pitfalls column is the important one. Bounded functions are easy to misuse, and a misused bounded function is often just a different bug:

char buf[16];
strncpy(buf, input, sizeof buf);   /* if input has >= 16 chars, buf has no '\0' */
printf("%s\n", buf);               /* reads past the end of buf: info leak      */

char path[64] = "/home/";
strncat(path, user, sizeof path);  /* wrong: should be sizeof path - strlen(path) - 1 */

The correct idioms are:

strncpy(buf, input, sizeof buf - 1);
buf[sizeof buf - 1] = '\0';

strncat(path, user, sizeof path - strlen(path) - 1);

if (snprintf(out, sizeof out, "%s/%s", dir, file) >= (int)sizeof out)
    /* handle truncation */;

Developer discipline helps, and so do static analyzers and compiler warnings. But decades of experience show that in large C code bases someone will eventually get one length computation wrong. That is why the remaining three layers exist: they assume the bug is there and try to make it unexploitable. The most thorough developer-level fix — a language in which the overflow cannot be expressed, such as Rust — is the topic of the optional Rust lab.

6.3 Hardware: The Non-Executable Stack (NX / DEP)

The idea

Code injection depends on the CPU being willing to execute bytes that the program wrote as data. But a stack holds return addresses, saved registers, and local variables — there is almost never a legitimate reason to execute it. So: mark the stack’s pages as non-executable, and an attempt to run shellcode there faults.

This is the “keep code and data separate” principle from Chapter 1, enforced by hardware. On x86 the page-table entry for each page has an NX (“no-execute”) bit — AMD’s name; Intel calls it XD (“execute disable”). On 32-bit x86 the bit exists only when the kernel uses PAE page tables, which all modern distributions do. When the bit is set and the CPU tries to fetch an instruction from that page, it raises a page fault, and the kernel kills the process with SIGSEGV. Windows calls the feature DEP (Data Execution Prevention); OpenBSD’s generalization of it is W^X: a page may be writable or executable, never both.

Checking and changing a binary

Whether a program’s stack is executable is recorded in the ELF file itself, in a program header named PT_GNU_STACK. The linker sets it; the kernel’s loader reads it when it maps the stack. You can inspect it with readelf:

$ readelf -W -l stack0x04 | grep GNU_STACK
  GNU_STACK      0x000000 0x00000000 0x00000000 0x00000 0x00000 RWE 0x10

The flags column says RWE: read, write, execute. That binary was compiled with -z execstack, which is how the s2CTF levels in Chapters 4 and 5 kept shellcode working. Without that flag — the default — you get RW:

$ gcc -m32 -o prog prog.c                    # default: non-executable stack
$ readelf -W -l prog | grep GNU_STACK
  GNU_STACK      0x000000 0x00000000 0x00000000 0x00000 0x00000 RW  0x10

The execstack utility (package execstack on Ubuntu 20.04) flips the flag on an existing binary without recompiling:

$ sudo execstack -c stack0x04        # clear: make the stack non-executable
$ readelf -W -l stack0x04 | grep GNU_STACK
  GNU_STACK      0x000000 0x00000000 0x00000000 0x00000 0x00000 RW  0x10
$ sudo execstack -s stack0x04        # set it back

Try it. Take the shellcode exploit you wrote for stack0x04 in Chapter 5. Run execstack -c on the binary and replay exactly the same payload. The return address is still overwritten, and the CPU still jumps to your NOP sled — and then the process dies with Segmentation fault. Under gdb you will see the fault reported at the first address on the stack that the CPU tried to execute, not at some later instruction.

What NX does not stop

Look again at the four steps in Section 6.1. NX breaks only step 4: executing injected bytes. The overflow (step 1) still happens, the return address is still overwritten (step 2), and the attacker still controls where the CPU goes next. If the attacker sends the CPU somewhere that is already executable — a function in the C library, say — NX has nothing to object to.

That is exactly the attack of Chapter 7: code reuse. Rather than injecting execve("/bin/sh") as shellcode, the attacker returns into libc’s own system() with a pointer to "/bin/sh" as its argument. NX killed code injection and in doing so created the incentive to develop code reuse.

Note. NX is essentially free at run time — the check is done by the MMU in parallel with every instruction fetch. Its cost is compatibility: a few programs legitimately generate code into writable memory (JIT compilers, some uses of GCC’s nested functions, which build trampolines on the stack). Those programs must request executable memory explicitly with mmap/mprotect, or opt out with -z execstack.

6.4 Operating System: Address Space Layout Randomization (ASLR)

The idea

Step 3 of the attack needs an address: where is the shellcode, or where is system()? In Chapter 5 we found these by running the program under gdb and reading them off, which only works if they are the same in every run. ASLR makes them different every run. Each time a program is started, the kernel picks random base addresses for the regions of its address space, so an address learned from one run (or from your own copy of the program) is useless against the next.

You can see it directly. /proc/self/maps shows the memory map of whichever process reads it, so running cat twice shows two different processes:

$ cat /proc/self/maps | grep -E 'stack|libc' | head -3
7f3a1c8d2000-7f3a1c8f4000 r--p 00000000 08:05 1835231   /usr/lib/x86_64-linux-gnu/libc-2.31.so
...
7ffd6c2e1000-7ffd6c302000 rw-p 00000000 00:00 0          [stack]
$ cat /proc/self/maps | grep -E 'stack|libc' | head -3
7f0b44e6b000-7f0b44e8d000 r--p 00000000 08:05 1835231   /usr/lib/x86_64-linux-gnu/libc-2.31.so
...
7ffe1a9a6000-7ffe1a9c7000 rw-p 00000000 00:00 0          [stack]

(Your addresses will differ — that is the point.) Notice that the low 12 bits of every base address are zero: randomization happens at page granularity, a fact we will come back to.

The randomize_va_space knob

Linux controls ASLR system-wide with one sysctl:

kernel.randomize_va_spaceEffect
0No randomization. Every run gets the same layout.
1Randomize the stack, the mmap region (and therefore shared libraries), and the vDSO.
2Everything in 1, plus the heap (the brk area). This is the default.
$ cat /proc/sys/kernel/randomize_va_space
2
$ sudo sysctl -w kernel.randomize_va_space=0     # what Labs 2 and 3 ask you to do

To disable randomization for a single process instead of the whole machine, use setarch:

$ setarch "$(uname -m)" -R ./prog        # -R = --addr-no-randomize

Note. gdb does the same thing on your behalf: its disable-randomization setting is on by default, so programs run under gdb always get the same layout even when system-wide ASLR is enabled. This is a classic source of confusion — an exploit that works perfectly inside gdb fails outside it. Use set disable-randomization off inside gdb to see the real behavior.

What about the program itself? PIC and PIE

ASLR as described so far randomizes the stack, heap, and libraries. What about the main executable’s own code and global data? That depends on how the executable was compiled.

Code that will be loaded at an unknown address must avoid hard-coding absolute addresses. Shared libraries have always had to work this way — that is what position-independent code (PIC) means. Calls and jumps on x86 are already PC-relative, so the hard part is data: on 32-bit x86 there is no instruction that says “load from EIP + offset”, so PIC code calls a tiny helper, __x86.get_pc_thunk.bx, to get its own address into %ebx, and then accesses globals and the GOT relative to %ebx. (x86-64 added RIP-relative addressing, which makes PIC nearly free.) Compare a non-PIC and a PIC access to a global variable g:

# gcc -m32 -no-pie -fno-pic                 # gcc -m32 -fpie -pie (default)
mov    0x804c024,%eax     # absolute         call   __x86.get_pc_thunk.bx
                                             add    $0x2e1f,%ebx      # %ebx = GOT base
                                             mov    0x24(%ebx),%eax   # relative to it

An executable compiled this way is a position-independent executable (PIE), and the kernel can load it at a random base like any library. Ubuntu has built PIE by default since 17.10. A non-PIE 32-bit executable always loads at 0x08048000; a PIE loads somewhere random, or — with ASLR disabled — at a fixed address, typically 0x56555000 for 32-bit programs. That is why the gdb listings in the format-string chapter show addresses like 0x56556240, while the s2CTF binaries, built with -no-pie -fno-pic, show addresses like 0x080491a6.

The practical consequence for an attacker: in a non-PIE binary, even with full ASLR, the program’s own code, its PLT, and its GOT are at fixed, known addresses. As Chapter 7 shows, that alone can be enough to build an exploit.

Weaknesses of ASLR

ASLR does not remove the vulnerability; it hides the target. Hiding has two weaknesses.

1. Low entropy (brute force). On 32-bit x86, there is not much room to randomize in. Page alignment fixes the low 12 bits, the kernel needs the high bits to keep regions in their places, and what remains is small: by default Linux uses only 8 bits of randomness for the mmap base on 32-bit x86 (/proc/sys/vm/mmap_rnd_bits; 28 bits on x86-64). With 8 bits, a guessed libc address is right once in 256 tries. If the target restarts after each crash — a network daemon supervised by systemd, a Set-UID program you can run in a loop — simply trying until it works is entirely practical:

# brute-forcing ASLR on 32-bit: run the same exploit until the guess is right
while true; do ./vulnerable "$(cat payload)" && break; done

Going deeper. Shacham et al., “On the Effectiveness of Address-Space Randomization” (CCS 2004), attacked the PaX ASLR implementation on 32-bit Linux. With only 16 bits of randomness in the library base, a remote return-to-libc attack against Apache succeeded by brute force in a few minutes on average. Their conclusion — that ASLR on 32-bit platforms buys little against a patient attacker — is the main reason serious ASLR deployments rely on 64-bit address spaces.

2. Information leaks. Randomization is applied once per process, to the base of each region. The internal layout of a region is unchanged: the distance from the start of libc to system() is a constant determined by the libc binary. So if the attacker learns any single address inside libc — a return address left on the stack, a function pointer, a GOT entry — they can compute every other address in libc by adding a constant:

leaked printf address           0xf7e2d340   (read out of the process somehow)
- offset of printf in libc.so   0x00052340   (constant: read with  nm -D / readelf)
= libc base                     0xf7ddb000
+ offset of system in libc.so   0x00041360
= system                        0xf7e1c360

(The numbers are illustrative.) This is why modern exploits are usually two-stage: first an arbitrary read that leaks a pointer, then the actual control-flow hijack. Chapter 10 shows how a format-string bug provides exactly such a read.

6.5 Compiler: Stack Canaries (StackGuard)

The idea

NX and ASLR try to make a corrupted return address useless. A stack canary instead detects that the return address was corrupted, before the function uses it.

The observation behind it, due to Cowan et al.’s StackGuard (1998), is that a linear overflow like strcpy cannot skip bytes. To reach the return address from a buffer below it, the overflow must overwrite everything in between. So the compiler places a secret value — the canary, after the canaries miners carried to detect gas — between the local variables and the saved frame pointer and return address:

                     high address
          +----------------------+
          |  input (argument)    |  ebp+8
          +----------------------+
          |  return address      |  ebp+4    <- target of the attack
          +----------------------+
  %ebp -> |  saved %ebp          |
          +----------------------+
          |  canary              |  ebp-0xc  <- checked before `ret`
          +----------------------+
          |  (padding / saved    |
          |   registers)         |
          +----------------------+
          |  buf[SIZE]           |  <- overflow starts here, grows upward
  %esp -> +----------------------+
                     low address

At function entry the prologue copies the canary from a protected location onto the stack. Just before the function returns, the epilogue compares the stack copy with the original. If they differ, something has written over the stack frame, and the program aborts instead of executing ret:

*** stack smashing detected ***: terminated
Aborted (core dumped)

For the overflow to survive, the attacker would have to write the canary’s exact value back in the right place — which requires knowing a random value chosen at process startup.

What GCC actually emits

Compile the vulnerable function from Chapter 4 with the protector enabled and look at the assembly (32-bit):

vul:
    push   %ebp
    mov    %esp,%ebp
    push   %ebx
    sub    $0x54,%esp
    ...
    mov    %gs:0x14,%eax          # load the canary from thread-local storage
    mov    %eax,-0xc(%ebp)        # store it just below the saved registers
    xor    %eax,%eax              # don't leave the canary lying in a register
    ...                           #   ... the body, including strcpy ...
    mov    -0xc(%ebp),%eax        # reload the stack copy
    xor    %gs:0x14,%eax          # compare with the original (newer GCC: sub)
    je     .Lok
    call   __stack_chk_fail       # mismatch: abort, never returns
.Lok:
    mov    -0x4(%ebp),%ebx
    leave
    ret

Key details:

  • Where the reference value lives. The original canary is stored in thread-local storage, reached through a segment register: %gs:0x14 on 32-bit x86 and %fs:0x28 on x86-64. It is not on the stack, so a stack overflow cannot reach it.
  • Who picks it. The C library sets it once at process startup from randomness the kernel supplies (the AT_RANDOM auxiliary vector entry). It does not change during the life of the process — or across fork(), which matters below.
  • Its shape. glibc forces the lowest byte of the canary to 0x00. Because x86 is little-endian, that zero is the first canary byte an upward overflow reaches. A string-copy overflow (strcpy, gets, …) cannot write a zero byte in the middle of its payload, so it cannot reproduce the canary even if the attacker knows the remaining bytes. This idea of a canary that contains a string terminator is called a terminator canary; glibc combines it with randomness in the remaining bytes. It also means a string-printing bug cannot leak the canary by reading “past the end” of a buffer — the zero stops the print.
  • __stack_chk_fail. Prints the message above and aborts. Aborting, rather than trying to recover, is the fail-safe default from Chapter 1: once the stack is known to be corrupt, nothing in the frame can be trusted.

Which functions get a canary?

Checking costs two memory loads and a compare per protected call — cheap, but not zero, so GCC offers several levels:

FlagFunctions protected
-fno-stack-protectorNone. Used throughout Labs 2–3 and s2CTF so the overflow is exploitable.
-fstack-protectorFunctions with a char array of at least 8 bytes, or that call alloca.
-fstack-protector-strongAdditionally, functions with any local array, or a local whose address is taken. Ubuntu’s default.
-fstack-protector-allEvery function.

GCC adds a second, quieter protection along with the canary: it reorders locals so that character arrays sit directly below the canary, above scalar variables and pointers. An overflow of buf then runs into the canary rather than into a neighboring flag or function pointer. This is why, when you compile the s2CTF level-1 program with the protector, overflowing buf no longer changes flag — even before the canary check runs.

What canaries do not stop

A canary protects exactly one thing: the saved registers and return address above it, against linear overwrites, checked at return. Each qualification is a way around it.

  1. Corrupting data before the canary. Anything the overflow reaches before it reaches the canary is unprotected, and it is used before the check at ret. If a function pointer or a security-relevant flag lives between the buffer and the canary (in a struct, for example, where the compiler may not reorder fields), the attacker overwrites it and uses it immediately. Level 2 of s2CTF (fp() called right after strcpy) is this situation.
  2. Non-linear writes. An arbitrary-write primitive — a format string’s %n (Chapter 10), an attacker-controlled array index, a corrupted heap pointer — can write the return address directly without touching the canary.
  3. Leaking the canary. The canary is secret only as long as nothing reveals it. A format-string read (%x or %p walking up the stack) or an out-of-bounds read prints it, after which the overflow simply writes the correct value back in place.
  4. Brute force on forking servers. A server that fork()s a child per connection gives every child the same canary as the parent. If a crash only kills the child, the attacker can guess the canary one byte at a time: overflow by exactly one byte into the canary, try all 256 values, and the one that does not crash is correct. Four bytes on 32-bit (really three, since the low byte is known to be zero) cost at most a few hundred connections. Bittau et al.’s “Hacking Blind” (IEEE S&P 2014) used the same trick to read the canary and return addresses from a remote server with no binary available.
  5. Other memory. Canaries protect stack frames only. Heap overflows, use-after-free, and data-only attacks are untouched.

6.6 Two More Hardening Features: RELRO and FORTIFY_SOURCE

Two other defenses appear in every modern Linux binary and in checksec output, so you should recognize them.

RELRO (relocation read-only). Dynamically linked programs call library functions through the Global Offset Table (GOT), a table of function pointers that the dynamic linker fills in (Chapter 7 explains how). Because it is written at run time, the GOT is writable — which makes it a perfect target for an arbitrary-write primitive: overwrite the GOT entry for printf with the address of system, and the next printf(buf) becomes system(buf). RELRO limits that:

  • Partial RELRO (-Wl,-z,relro, the default) makes some linker-written sections read-only after startup, but leaves the GOT entries used for lazy binding writable.
  • Full RELRO (-Wl,-z,relro,-z,now) resolves all symbols at load time and then mprotects the entire GOT read-only. GOT overwrites become impossible, at the cost of slower startup.

FORTIFY_SOURCE. Compiling with optimization and -D_FORTIFY_SOURCE=2 (default on Ubuntu when optimizing) makes the compiler replace calls such as strcpy, memcpy, and sprintf with checking versions (__strcpy_chk, …) whenever it can determine the destination’s size at compile time. An overflow of a known-size buffer then aborts with *** buffer overflow detected ***. It also rejects %n in format strings that live in writable memory — a direct counter to Chapter 10’s attacks.

6.7 Checking a Binary: checksec

You do not need to read assembly to know which mitigations a binary has. The checksec tool, which ships with pwntools (pip3 install pwntools), reads the ELF headers and symbols and reports them:

$ checksec --file=./stack0x04          # or: pwn checksec ./stack0x04
    Arch:     i386-32-little
    RELRO:    Partial RELRO
    Stack:    No canary found
    NX:       NX disabled
    PIE:      No PIE (0x8048000)
    RWX:      Has RWX segments

Every line is a mitigation that is off — this is a deliberately weak training binary. Compare a program compiled with Ubuntu’s defaults:

$ gcc -m32 -o prog prog.c && checksec --file=./prog
    Arch:     i386-32-little
    RELRO:    Full RELRO
    Stack:    Canary found
    NX:       NX enabled
    PIE:      PIE enabled

The output above comes from flags. Here is the mapping from the s2CTF Makefile flags (https://github.com/sysec-uic/s2ctf-playground) to what they disable:

FlagEffectMitigation disabled
-z execstackMarks PT_GNU_STACK as RWENX on the stack
-fno-stack-protectorNo canaries, no local reorderingStack canaries
-no-pie -fno-picFixed load address 0x08048000, absolute addressingPIE (randomized code base)
-m3232-bit binary— (but low ASLR entropy, simpler calling convention)
-gDebug symbols— (just makes gdb friendlier)
sysctl kernel.randomize_va_space=0Not a compiler flag: system-wideASLR

When an exploit that “should” work does not, checksec and cat /proc/sys/kernel/randomize_va_space are the first two things to check.

6.8 Defense in Depth and the Arms Race

With all four mitigations enabled, a single stack overflow is far less dangerous than in 1996: the canary detects the linear overwrite, and even if it is bypassed, NX blocks injected code and ASLR hides the addresses of existing code. The attacker now needs several things to go right at once, which is exactly what defense in depth is supposed to achieve.

But notice the pattern in each “what it does not stop” section. Every mitigation targets one step of one technique, and attackers respond by changing technique:

  defense                      attacker response                         where
  -------------------------    --------------------------------------    -----------
  non-executable stack     ->  reuse code that is already executable     Chapter 7
  ASLR                     ->  leak one pointer, compute the rest;       Chapter 10
                               brute force on 32-bit
  stack canary             ->  leak the canary; write around it with     Chapter 10
                               an arbitrary-write primitive
  code reuse (ROP)         ->  restrict where indirect jumps may go      Chapter 8
                               (control-flow integrity)

None of these defenses fixes the root cause, which is that C lets a program write outside the bounds of an object. They are exploit mitigations, not bug fixes. The bug-fix side of the story — finding the bugs before attackers do, and writing code in languages where they cannot occur — is the subject of Chapter 9 and the Rust lab.

Check Your Understanding

1. Match each countermeasure to the step of the stack-smashing attack it breaks: (a) -fstack-protector, (b) NX, (c) ASLR, (d) replacing strcpy with a correct bounds check.

Answer

(d) breaks step 1, the overflow itself — the only one that removes the bug. (a) breaks step 2 by detecting that the return address region was overwritten before ret uses it. (c) breaks step 3: the attacker no longer knows the address of the shellcode or of library code. (b) breaks step 4: even if the CPU jumps to injected bytes on the stack, it refuses to execute them.

2. A student’s shellcode exploit for stack0x04 works in gdb but crashes with Segmentation fault when run from the shell. checksec shows NX disabled. What is the most likely cause, and how can they confirm it?

Answer

ASLR. gdb disables randomization for the programs it runs by default, so the stack address the student read in gdb is stable there but different in every normal run. They can confirm by checking cat /proc/sys/kernel/randomize_va_space (non-zero means ASLR is on), or by running set disable-randomization off in gdb and watching the stack address change between runs. (Smaller differences — a few dozen bytes — can also come from gdb adding environment variables such as LINES and COLUMNS; a NOP sled tolerates those.)

3. Why does a non-executable stack not prevent the attack in which the return address is overwritten with the address of system()?

Answer

NX only prevents instruction fetches from non-executable pages. In a return-to-libc attack the stack holds only data — addresses and an argument pointer — and the CPU executes system()’s instructions from libc’s code pages, which are executable by necessity. No byte on the stack is ever executed, so there is nothing for NX to stop.

4. On a 32-bit system with ASLR enabled, an attacker can run a vulnerable Set-UID program as often as they like. Why is ASLR a weak defense here, and how does the picture change on x86-64?

Answer

On 32-bit Linux the library base has only about 8 bits of randomness by default, so a fixed guess is correct roughly once in 256 runs; a shell loop rerunning the program finds the right layout in seconds. Each failed attempt only crashes one process, and nothing stops the attacker from trying again. On x86-64 the mmap base has 28 bits of randomness, so blind guessing would need on the order of 2^27 attempts on average, which is impractical — there the attacker needs an information leak instead.

5. glibc’s canary always has a zero low byte. Explain why that helps against strcpy-based overflows, and name an attack that the zero byte does not help against.

Answer

Because x86 is little-endian, the zero byte is the first canary byte an upward overflow writes. strcpy stops copying at the first zero in its source, so the attacker cannot write a zero there and continue on to the return address; the overflow either stops short of the return address or writes a non-zero byte into the canary and is detected. The zero byte also stops %s-style reads from printing past a buffer into the canary. It does not help against copies that are not string-based (memcpy, read, fread into a fixed buffer), which happily copy zero bytes; nor against attacks that leak the canary with a format string and then write it back with such a copy; nor against arbitrary writes that skip the canary entirely.

6. A function stores a function pointer fp in a struct right after a char name[32] field, copies user input into name with strcpy, and then calls fp(). The binary was compiled with -fstack-protector-strong. Is it exploitable? Why or why not?

Answer

Yes, very likely. GCC can reorder separate local variables, but it cannot reorder fields inside a struct, because the struct’s layout is fixed by the C language. The overflow from name therefore reaches fp before it reaches the canary, and fp() is called before the function returns — before the canary is ever checked. The canary would detect the corruption only at ret, which comes too late. NX and ASLR may still make it hard to choose a useful value for fp, but the canary does not help.

7. What is the difference between Partial and Full RELRO, and which attack does Full RELRO prevent?

Answer

Partial RELRO makes some relocation-related sections read-only after startup but leaves the GOT entries used for lazy binding writable, since the dynamic linker fills them in on first use. Full RELRO (-z now in addition to -z relro) resolves every symbol at load time and then makes the whole GOT read-only. It prevents GOT overwrites — using an arbitrary-write primitive to replace, say, the GOT entry of printf with the address of system so that a later library call is redirected.

Summary

  • Countermeasures can be applied by the developer (safer functions, bounds checks — but bounded functions have their own pitfalls), the compiler (stack canaries), the OS (ASLR), and the hardware (NX).
  • NX/DEP marks data pages non-executable, stopping injected shellcode. It is recorded per binary in PT_GNU_STACK (readelf -l, execstack, -z execstack). It does nothing against code reuse.
  • ASLR randomizes region base addresses at each execution (kernel.randomize_va_space). The main executable is randomized only if it is PIE. ASLR is weak on 32-bit (little entropy, brute force) and is defeated by a single information leak, since offsets within a region are fixed.
  • Stack canaries (StackGuard; -fstack-protector[-strong|-all]) place a random value — in glibc, with a zero low byte — below the saved registers and check it before ret. They do not protect data below the canary, non-linear writes, or a canary that has been leaked or brute-forced.
  • RELRO protects the GOT from overwrite; FORTIFY_SOURCE adds bounds checks to common library calls. checksec reports all of these at a glance.
  • Every mitigation blocks a technique, not the bug, which drives the arms race: NX → code reuse (Chapter 7); ASLR and canaries → information leaks (Chapter 10); code reuse → control-flow integrity (Chapter 8).

Connection to the Labs

  • Lab 2: Return-to-libc Attack begins with a “Setting up the environment” section that switches off ASLR and StackGuard but deliberately leaves the non-executable stack on. After this chapter you should be able to explain why each setting is needed — several of the lab’s understanding checks ask exactly that, including what would change if you re-enabled ASLR.
  • Lab 3: Format String and Reverse Shell also disables ASLR. Its “How the program is built” section uses the flags from Section 6.7; read its Makefile with the table above in hand.
  • The s2CTF playground binaries are compiled with every mitigation off. Try rebuilding one level at a time with a mitigation turned back on, and see which of your exploits stop working and why.

Further Reading

  • Crispin Cowan et al. “StackGuard: Automatic Adaptive Detection and Prevention of Buffer-Overflow Attacks.” 7th USENIX Security Symposium, 1998.
  • Hovav Shacham, Matthew Page, Ben Pfaff, Eu-Jin Goh, Nagendra Modadugu, and Dan Boneh. “On the Effectiveness of Address-Space Randomization.” ACM CCS, 2004.
  • Andrea Bittau, Adam Belay, Ali Mashtizadeh, David Mazières, and Dan Boneh. “Hacking Blind.” IEEE Symposium on Security and Privacy, 2014.
  • Eli Bendersky. “Position Independent Code (PIC) in shared libraries.” https://eli.thegreenplace.net/2011/11/03/position-independent-code-pic-in-shared-libraries
  • The s2CTF playground and its Makefile: https://github.com/sysec-uic/s2ctf-playground