Chapter 7: Code-Reuse Attacks: Return-to-libc and ROP

Chapter 6 ended with a non-executable stack: the CPU refuses to execute anything on the stack, so the shellcode from Chapter 5 is dead on arrival. But the overflow is still there, and the attacker still controls the saved return address. The question this chapter asks is simple: if we cannot bring our own code, whose code can we use?

The answer is: the program’s own, and that of every library it loads. A typical process has megabytes of executable code mapped into its address space — the C library alone contains system(), execve(), mprotect(), and thousands of other functions. By pointing the return address at code that is already there, and laying out the stack so that code finds the arguments it expects, the attacker can make the program do their bidding without executing a single injected byte. We start with the simplest form, return-to-libc, and generalize it step by step into return-oriented programming (ROP), a technique powerful enough to perform arbitrary computation.

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

  • Explain why code-reuse attacks defeat a non-executable stack.
  • Construct, on paper, the stack layout that makes a ret behave like a call to system("/bin/sh"), including a clean exit.
  • Find the addresses of libc functions and of the string "/bin/sh" in a process, and explain what makes those addresses shift.
  • Explain how the PLT and GOT implement dynamic linking, and why system@plt is a useful target.
  • Chain several library calls using stack-cleanup gadgets, and trace the chain step by step.
  • Explain what a ROP gadget is, why x86 code contains so many of them, and how tools such as ROPgadget find and assemble them.

7.1 Why Code Reuse?

Look at what the attacker actually needs. The shellcode in Chapter 5 did one thing: it invoked the execve system call with "/bin/sh" as its argument. The C library already contains functions that do that, and much more:

libc functionWhat it gives the attacker
system(const char *cmd)Runs /bin/sh -c cmd — any shell command.
execve / execv / execlRuns any program, with arguments of your choosing.
mprotect(addr, len, prot)Changes page permissions — e.g. makes the stack executable again.
exit(int status)Terminates the process quietly, leaving no crash behind.

libc is linked into almost every program — even a Python interpreter maps twenty-odd shared libraries — so these functions are present and executable in essentially every process. NX cannot mark them non-executable without breaking the program.

So instead of overwriting the return address with the address of injected code, we overwrite it with the address of existing code. This is return-to-libc (often abbreviated ret2libc), described publicly by Solar Designer in 1997, well before NX became widespread.

The attack still needs the same ingredients as before — a control-data overwrite and a known address — so the first two defenses from Chapter 6 still matter. Labs and examples in this chapter assume ASLR is disabled and no stack canary is present, and they focus on the one thing NX changes: we may only point at code, never supply it.

7.2 A ret Is Not a call

The difficulty is that we reach system() the wrong way. Functions expect to be called; we are going to return into them. To make that work, we must understand exactly what the stack looks like when a function begins.

What a normal call leaves behind

Consider an ordinary call:

int main(int argc, char *argv[]) {
    system("/bin/sh");
}

Under the 32-bit cdecl convention (Chapter 3), the caller pushes the argument, and then call pushes the return address and jumps:

   before `call system`                 at the first instruction of system()

        high address                          high address
   +--------------------+               +--------------------+
   |        ...         |               |        ...         |
   +--------------------+               +--------------------+
   | addr of "/bin/sh"  | <- %esp       | addr of "/bin/sh"  |  %esp+4: arg1
   +--------------------+               +--------------------+
                                        | saved return addr  |  <- %esp
                                        +--------------------+
        low address                           low address

call did two things: it pushed the address of the next instruction (the saved return address), and it set %eip to system. So from system’s point of view, at its first instruction:

  • [%esp] is its return address;
  • [%esp+4] is its first argument; [%esp+8] would be the second; and so on.

After system’s prologue runs push %ebp; mov %esp,%ebp, those become the familiar 8(%ebp) for the first argument and 4(%ebp) for the return address.

Faking the call frame with ret

Now look at the vulnerable function from s2CTF level 4:

#define SIZE 64
int vul(char *input)
{
    char buf[SIZE];
    strcpy(buf, input);
    return 0;
}

Its epilogue is leave; ret. leave restores %esp to the frame pointer and pops the saved %ebp; ret then pops the saved return address into %eip. At the moment ret executes, %esp points at the return-address slot. After ret, %esp has moved up by 4.

There is no call site for system("/bin/sh") anywhere in this program. But a ret site whose stack we control can pretend to be one. We overwrite the return address with &system, and arrange the slots above it so that, after the ret has popped &system, the stack looks exactly like the right-hand picture above:

   just before vul's `ret`                 just after `ret` (now inside system)

         high address                             high address
   +----------------------+               +----------------------+
   | addr of "/bin/sh"    | arg1          | addr of "/bin/sh"    | %esp+4: arg1
   +----------------------+               +----------------------+
   | placeholder          | (system's     | placeholder          | <- %esp: system's
   |                      |  return addr) |                      |    "return address"
   +----------------------+               +----------------------+
   | &system()            | <- %esp       | &system()            | (already popped)
   +----------------------+               +----------------------+
   | saved %ebp (junk)    |               | saved %ebp (junk)    |
   +----------------------+               +----------------------+
   | buf[64] (junk)       |               | buf[64] (junk)       |
   +----------------------+               +----------------------+
         low address                              low address

The essential trick: a ret site is not a call site, but if we put one extra word (a placeholder for the return address) between the function address and its arguments, system cannot tell the difference. The only thing missing compared to a real call is a meaningful return address — which is the next thing we fix.

Note. The saved %ebp gets clobbered by the overflow too. That does not matter here: system saves and restores %ebp itself and never relies on the caller’s value. It would matter if we returned somewhere that uses %ebp-relative addressing without setting %ebp first.

A clean exit

What should go in the placeholder? Whatever we put there is where system will return when the spawned shell exits. With junk (0xaaaaaaaa, say), the attack works — we get our shell — but when we leave the shell, system returns to 0xaaaaaaaa and the program crashes with a segmentation fault. A crash leaves traces: a core file, a log entry, an alert in a monitoring system.

The fix is to put the address of exit() there. Then system “returns” into exit, which terminates the process normally. From exit’s point of view, the stack slot above its “return address” is its argument, the exit status — which is why the slides show a 0 in that position:

         high address
   +----------------------+
   | 0x00000000           |  exit's argument (status)        -- optional
   +----------------------+
   | addr of "/bin/sh"    |  system's arg1 / exit's "return address"
   +----------------------+
   | &exit()              |  system's return address
   +----------------------+
   | &system()            |  overwrites vul's return address
   +----------------------+
   | saved %ebp (junk)    |
   +----------------------+
   | buf ... (junk)       |
         low address

Notice the overlap: the word holding &"/bin/sh" is simultaneously system’s first argument and exit’s return address (which exit never uses, since it does not return). And exit’s argument sits in the same position where system’s second argument would be. When we chain functions by returning from one into the next, their frames share stack words. That overlap is harmless for system → exit, but it is exactly what makes longer chains difficult, as Section 7.6 shows.

Note. A strcpy-based overflow cannot write the four zero bytes of the exit status — the copy stops at the first \x00. The status is just whatever happens to be in that slot, which is fine: the process still exits normally, only with an odd exit code.

7.3 Finding the Ingredients

The payload needs three addresses — system, exit, and the string "/bin/sh" — and one offset: the distance from the start of the buffer to the saved return address.

Library function addresses

With ASLR off, a library is loaded at the same address in every run, so we can read function addresses from the program under gdb. Two rules:

  1. Run the program first. Before the program starts, libc is not mapped at all; gdb only knows symbols from the executable. Break at main, run, then print.
  2. Use the real target binary. A Set-UID binary and a non-Set-UID copy of the same program may get different memory layouts (for example, because the loader treats privileged programs differently), so addresses must come from the binary you are attacking.
$ gdb -q ./stack0x04
pwndbg> break main
pwndbg> run
pwndbg> p system
$1 = {<text variable, no debug info>} 0xf7e12420 <system>
pwndbg> p exit
$2 = {<text variable, no debug info>} 0xf7e04f80 <exit>

(Example addresses only.) Alternatively, compute them from the library: vmmap in pwndbg (or info proc mappings) gives the libc base, and readelf -s or nm -D on the libc file gives each function’s offset. Base plus offset equals address — the same arithmetic that an ASLR info-leak exploit uses (Chapter 6).

The string "/bin/sh"

We need the string somewhere in memory and we need its address. Three common sources:

  • libc itself. system is implemented by running /bin/sh -c ..., so libc contains the string "/bin/sh". In pwndbg, search "/bin/sh" finds it; in plain gdb, find &system,+9999999,"/bin/sh" does the same. Offline, strings -a -t x /lib32/libc.so.6 | grep /bin/sh prints its offset in the file, which you add to the libc base.
  • An environment variable. Exported variables are copied onto the top of the stack of every program the shell starts. Export MYSHELL=/bin/sh, and a small helper program can print getenv("MYSHELL") — the address of the string "/bin/sh" just after the =.
  • The payload itself. Put the string in your input and point at the buffer. This works when you know the buffer’s address and the copy does not mangle the string.

The environment-variable approach has a subtle trap. At program start the top of the stack holds, from the top down, the strings of the environment and the arguments, including argv[0], the program’s name:

      high address
   +------------------------------+
   | "./prog"  (program name)     |  <- strings: the program path is near the top
   | "MYSHELL=/bin/sh"            |
   | "PATH=..." ...               |  environment strings
   | argv strings ...             |
   +------------------------------+
   | envp[] pointers, argv[]      |
   | pointers, argc               |
   +------------------------------+
   | main's frame ...             |
      low address

Everything is packed downward from the top, so anything that changes the length of the strings packed before MYSHELL moves it. In particular, a helper program whose name has a different length than the target’s sees the variable at a slightly different address. Lab 2 makes you discover and explain this; keep the picture above in mind when you do.

Try it. Write a helper that prints getenv("MYSHELL"). Copy it to names of length 4, 6, and 10, run each, and plot the reported address against the name length. What is the slope? Now do the same with an extra environment variable defined. Which way does the address move, and why?

The offset to the return address

This is the same problem as in Chapter 4: read the assembly, inspect the stack in gdb, or use a cyclic pattern (cyclic 100, then cyclic -l on the faulting %eip). Once you know the offset Y of the saved return address from the start of the buffer, the layout above fixes the others: exit’s address goes 4 bytes higher, and the "/bin/sh" pointer 8 bytes higher.

Assembling the payload

The instructor’s skeleton fills a buffer with non-zero filler and drops the three addresses at three offsets:

#!/usr/bin/env python3
import sys

content = bytearray(0xaa for i in range(300))   # non-zero filler: strcpy stops at \x00

X = 0
sh_addr = 0x00000000          # address of "/bin/sh"
content[X:X+4] = (sh_addr).to_bytes(4, byteorder='little')

Y = 0
system_addr = 0x00000000      # address of system()
content[Y:Y+4] = (system_addr).to_bytes(4, byteorder='little')

Z = 0
exit_addr = 0x00000000        # address of exit()
content[Z:Z+4] = (exit_addr).to_bytes(4, byteorder='little')

sys.stdout.buffer.write(content)

Y is the return-address offset, Z is where system looks for its return address, and X is where system looks for its argument. Two practical details:

  • The filler is 0xaa, not zero, because strcpy would stop at the first zero. For the same reason, none of the addresses may contain a \x00 byte — a constraint that is easy to satisfy for 32-bit libc addresses like 0xf7e1.... but not, as we will see, on 64-bit.
  • Whatever lies beyond the last word you care about is irrelevant, but its bytes are still copied — harmless here, because nothing reads them.

system and privilege

If the target is a root-owned Set-UID program, system("/bin/sh") starts a shell that inherits the process’s effective UID — unless /bin/sh is dash (or bash), which drops privilege when it notices it is running Set-UID (Chapter 2). Two ways around that: point /bin/sh at a shell without the countermeasure (fine for a first experiment), or bypass system altogether and return into an exec-family function to run /bin/bash -p directly. The second requires building an argv array in memory — and the array must end in a NULL pointer, which strcpy cannot copy. Lab 2’s optional Task 4 is about solving exactly that problem.

7.4 Calling Through the PLT

So far we called system at its address inside libc. There is another way that matters in practice, and understanding it requires a short detour into dynamic linking.

When a dynamically linked program calls printf, the compiler does not know where libc will be loaded, so the call goes through two tables:

  • The Procedure Linkage Table (PLT) — a small stub per external function, inside the executable’s code: printf@plt, system@plt, …
  • The Global Offset Table (GOT) — a table of pointers, inside the executable’s data, one per external function. The PLT stub jumps through it.
   main:                          printf@plt:                     GOT
     ...                            jmp  *GOT[printf]  ------->   [printf] -----+
     call printf@plt  -------->     push $reloc_index               (initially   |
     ...                            jmp  PLT0 (resolver)             points back |
                                                                     into stub)  |
                                                                                 v
                                                                   libc: printf()

With lazy binding, the GOT entry initially points back into the PLT stub, which calls the dynamic linker’s resolver. The resolver looks up printf in libc, writes its real address into the GOT entry, and jumps there. Every later call goes straight from the stub through the GOT to libc. (Full RELRO, from Chapter 6, does all resolution at load time and then makes the GOT read-only.)

Two consequences for attackers:

  1. If the program itself calls system, it has a system@plt stub. In a non-PIE executable that stub sits at a fixed address in the program’s own code — even with ASLR randomizing libc. Returning to system@plt reaches the real system without knowing where libc is. The fake frame is identical to Section 7.2: [&system@plt][return address][arg].
  2. GOT entries are pointers to libc. Reading one (with an info leak) reveals the libc base; writing one (with an arbitrary write, if RELRO is not full) hijacks every later call to that function. Both tricks reappear in Chapter 10.

Find PLT entries with objdump -d prog | grep @plt, or plt in pwndbg.

7.5 Beyond system: Re-enabling Execution with mprotect

Return-to-libc is not limited to running a shell command. A particularly elegant use is to turn NX off for the attacker’s own buffer and then run ordinary shellcode.

int mprotect(void *addr, size_t len, int prot);

mprotect changes the permissions of the pages covering [addr, addr+len-1]. With prot = PROT_READ|PROT_WRITE|PROT_EXEC (= 7), the stack page holding our shellcode becomes executable. And we can choose mprotect’s return address to be the shellcode itself:

         high address
   +----------------------+
   | 7                    |  prot   = PROT_READ|PROT_WRITE|PROT_EXEC
   +----------------------+
   | len                  |  e.g. 0x1000 (one page)
   +----------------------+
   | page address         |  addr: the buffer's address rounded down to 4096
   +----------------------+
   | &shellcode           |  mprotect's return address -> jump into shellcode
   +----------------------+
   | &mprotect()          |  overwrites the saved return address
   +----------------------+
   | saved %ebp (junk)    |
   +----------------------+
   | buf: shellcode ...   |  becomes rwx after mprotect
         low address

When vul returns, mprotect runs with three arguments taken from the stack, makes the page executable, and then “returns” — straight into the shellcode. Two details make this harder than it looks: addr must be page-aligned (its low 12 bits zero), and the values 0x1000 and 7 contain zero bytes, which a strcpy overflow cannot write. Real exploits solve the latter by using a non-string copy, by placing the arguments somewhere else, or by computing them with gadgets (Section 7.7).

7.6 Chaining Calls and the Overlap Problem

Returning into one function and then exit was a chain of length two. Can we chain arbitrarily many calls? Let us try to call f(a) and then g(b, c).

Following the rule from Section 7.2 — each function finds its return address directly above its own address, and its arguments above that — we get:

         high address
   +----------------------+
   | c                    |  g's arg2
   +----------------------+
   | b                    |  g's arg1
   +----------------------+
   | a  (??)              |  f's arg1 -- AND g's return address
   +----------------------+
   | &g()                 |  f's return address
   +----------------------+
   | &f()                 |  <- %esp at vul's ret
   +----------------------+
         low address

When vul returns, %esp moves up 4 and f runs, finding its return address &g at [%esp] and its argument a at [%esp+4]. When f returns, %esp moves up 4 again and g runs — but now g’s “return address” is the word holding a, and its arguments start right after it. The two frames overlap: a must be both f’s argument and the address g returns to. For two calls we can sometimes live with that. For a third call we cannot, because g’s return address must be the address of the third function, which is also f’s argument. We would need a to equal &h. In general, chaining functions with arguments by plain returns fails after the first one.

The root problem is that a function’s ret advances %esp by only 4 bytes, leaving it pointing at the previous function’s arguments. We need something that moves %esp past those arguments before the next ret.

Stack-cleanup gadgets

Programs are full of instruction sequences that do just that — at the end of almost any function that saved callee-saved registers:

pop  %ebp; ret                       # skip 1 word
pop  %ebx; pop %ebp; ret             # skip 2 words
pop  %esi; pop %edi; pop %ebp; ret   # skip 3 words
add  $0x10,%esp; ret                 # skip 4 words (other immediates exist too)

Each is a short sequence ending in ret that adjusts %esp. We call such a sequence a gadget. If, instead of returning directly into the next function, each function returns into a gadget that pops exactly as many words as it has arguments, the gadget’s own ret then lands on the next function’s address. The overlap disappears.

Here is a chain that calls fun1(t), then fun2(u, v), then fun3(x, y, z):

         high address
   +--------------------+
   | z                  |
   +--------------------+
   | y                  |
   +--------------------+
   | x                  |  fun3's arguments
   +--------------------+
   | &(pop;pop;pop;ret) |  fun3's return address  (or the next link of the chain)
   +--------------------+
   | &fun3()            |
   +--------------------+
   | v                  |
   +--------------------+
   | u                  |  fun2's arguments
   +--------------------+
   | &(pop;pop;ret)     |  fun2's return address: e.g. pop %ebx; pop %ebp; ret
   +--------------------+
   | &fun2()            |
   +--------------------+
   | t                  |  fun1's argument
   +--------------------+
   | &(pop;ret)         |  fun1's return address: e.g. pop %ebp; ret
   +--------------------+
   | &fun1()            |  <- %esp just before vul's `ret`
   +--------------------+
         low address

Let us trace it step by step:

StepInstruction executingEffect%esp now points at
1vul’s ret%eip = &fun1; %esp += 4&(pop;ret) — fun1’s return address, with t above it
2fun1 runs, then its ret%eip = &(pop;ret); %esp += 4t
3pop %ebpdiscards t; %esp += 4&fun2
4ret (of the gadget)%eip = &fun2; %esp += 4&(pop;pop;ret) — fun2 sees u, v above it
5fun2 runs, then its ret%eip = &(pop;pop;ret); %esp += 4u
6pop %ebx; pop %ebp; retdiscards u, v; %eip = &fun3fun3’s return address, with x, y, z above it
7fun3 runs, then returns into its cleanup gadgetdiscards x, y, zwhatever comes next in the chain

Each gadget “lifts” %esp over the arguments of the function that just returned, and its trailing ret dispatches the next function. With gadgets of the right sizes, we can chain an arbitrary number of function calls with constant arguments, limited only by how much we can write onto the stack.

Note. The pop instructions load garbage into %ebp, %ebx, and so on. That is fine as long as nothing later depends on those registers holding meaningful values. Functions called by the chain set up their own frames.

7.7 Return-Oriented Programming

From function calls to gadgets

Look again at what the cleanup gadgets were doing. pop %ebp; ret is not a library function — it is the last two instructions of some function, and we jumped into its middle. Nothing restricts us to gadgets that merely discard words. Consider:

pop %eax; ret          # load a value from the stack into %eax
pop %ebx; ret          # load a value from the stack into %ebx
mov %eax,(%ebx); ret   # store %eax to memory at address %ebx

Placed on the stack as [&g1][v1][&g2][v2][&g3], these execute:

%eax = v1;   %ebx = v2;   mem[v2] = v1

— a memory write of an attacker-chosen value to an attacker-chosen address, assembled entirely from existing code. Add gadgets for arithmetic (add, xor), for conditional moves of %esp (branches), and for system calls (int $0x80; ret), and we can compute anything. This is return-oriented programming (ROP), introduced by Hovav Shacham in 2007. His paper showed that the gadgets present in a standard libc are Turing-complete: any computation can be expressed as a chain of them, without calling a single libc function.

ROP as a programming model

It helps to see a ROP chain as a program for a strange machine, a framing Stephen Checkoway uses:

Ordinary programROP program
Instructions in a code segmentGadget addresses (and immediate values) on the stack
%eip points to the next instruction%esp points to the next gadget address
The CPU fetches an instruction and advances %eipret fetches a gadget address and advances %esp
A no-op is nopA no-op is the address of a bare ret
A jump changes %eipA “jump” changes %esp (e.g. pop %esp; ret)
Immediates are encoded in the instructionImmediates are words on the stack, loaded with pop

Programs that exhibit such unintended computational power are sometimes called weird machines: the attacker’s input “programs” a machine that the original developer never intended to exist, built out of the program’s own parts.

Why x86 is full of gadgets

Where do the gadgets come from? Some are the honest ends of functions. But on x86 there are vastly more, because instructions have variable length and can start at any byte. If you jump into the middle of an instruction, the CPU decodes the remaining bytes as something else entirely. The only requirement for a gadget is that the byte stream eventually hits a 0xc3 (ret). For example:

bytes:      b8 5f c3 00 00
intended:   b8 5f c3 00 00          mov  $0xc35f,%eax
from +1:       5f                   pop  %edi
               c3                   ret

The program contains a harmless mov with an immediate that happens to include 5f c3. Jump one byte into it and you get pop %edi; ret — an unintended gadget. Any 0xc3 byte anywhere in executable memory — in an immediate, a displacement, an offset — is a potential gadget ending. Fixed-length architectures such as ARM and RISC-V do not have this property, but they still have plenty of intended gadgets, and ROP has been demonstrated on them as well.

Tools: ROPgadget

Searching for gadgets by hand is tedious; tools disassemble starting at every byte offset and report the useful sequences. ROPgadget (pip3 install ROPgadget) is one:

$ ROPgadget --binary ./stack0x04                         # list gadgets in the program
$ ROPgadget --binary ./stack0x04 --only "pop|ret"        # only pops and rets
$ ROPgadget --binary /usr/lib32/libc.so.6 --string "/bin/sh"
$ ROPgadget --binary ./stack0x04 --ropchain              # try to build an execve chain

When you search a library, the addresses printed are offsets within the file. To get run-time addresses, tell ROPgadget where the library is loaded (from vmmap in pwndbg) with --offset:

$ ROPgadget --binary /usr/lib32/libc.so.6 --offset 0xf7d81000 --ropchain

With --ropchain, ROPgadget tries to assemble a complete chain that invokes execve("/bin/sh", ...) through the system-call interface and prints Python code that builds it. A statically linked or large binary usually has enough gadgets; a tiny dynamically linked one often does not, which is why attackers turn to libc.

Going deeper. ROP does not even need ret. Checkoway et al., “Return-Oriented Programming without Returns” (CCS 2010), built Turing-complete gadget sets that end in indirect jumps through a register, defeating defenses that only monitored ret instructions. Bittau et al.’s “Hacking Blind” (IEEE S&P 2014) built ROP chains against a remote server without a copy of the binary, discovering gadgets by observing which guessed addresses crashed the server.

7.8 Code Reuse on 64-bit x86

Everything above used the 32-bit convention, where arguments live on the stack. On x86-64 the System V calling convention passes the first six integer arguments in registers: %rdi, %rsi, %rdx, %rcx, %r8, %r9. That changes the attack in three ways.

  1. Arguments need gadgets. Writing &"/bin/sh" onto the stack does nothing; system reads its argument from %rdi. So even the simplest ret2libc needs a gadget, typically pop %rdi; ret:

    [ &(pop %rdi; ret) ][ &"/bin/sh" ][ &system ]
    

    The gadget loads %rdi from the stack and then returns into system. 64-bit ret2libc is a (very short) ROP chain.

  2. Zero bytes. User-space addresses on x86-64 look like 0x00007ffff7e1a000 — the upper two bytes are always zero. Written little-endian, every address ends in \x00\x00. A strcpy overflow can therefore write only one address, and only as the last thing in the payload. Copies that do not stop at zero (read, memcpy, fread) are needed for longer chains.

  3. Stack alignment. The ABI requires %rsp to be 16-byte aligned at a call. glibc’s system uses SSE instructions such as movaps that fault on a misaligned stack, so a chain that is off by 8 bytes crashes inside system. The usual fix is to insert one extra bare ret gadget before system, which shifts %rsp by 8.

7.9 Defending Against Code Reuse

NX cannot stop code reuse, so what can?

  • ASLR (Chapter 6) hides the addresses of libc functions and gadgets. A ROP chain is a list of absolute addresses, so it is useless if the attacker does not know where the code is. With PIE, even the program’s own gadgets move. The counter, as always, is an information leak.
  • Stack canaries detect the linear overwrite that plants the chain. The counter is to leak the canary or to use a non-linear write.
  • Control-flow integrity (CFI) attacks the problem at its root. A ROP chain makes ret instructions return to places no call ever returned to, and makes indirect jumps land in the middle of instructions. If every indirect control transfer is checked against the set of targets the program could legitimately use — a ret may only go to an instruction right after a call site, an indirect call may only target a function entry — most gadgets become unreachable. Hardware support such as Intel CET’s shadow stack makes return checking nearly free. That is the subject of Chapter 8.

Check Your Understanding

1. In one sentence each: why does a non-executable stack stop shellcode but not return-to-libc? What does the attacker put on the stack in each case?

Answer

Shellcode attacks put instructions on the stack and jump to them, which NX blocks because the stack page is not executable. Return-to-libc puts only data on the stack — the address of system, a return address, and a pointer to "/bin/sh" — and the CPU executes libc’s instructions from libc’s executable pages, so NX never fires.

2. In the payload [&system][&exit][&"/bin/sh"], why is there a word between &system and &"/bin/sh"? What happens if it is filled with junk instead of &exit?

Answer

system is entered by a ret, not a call, but it expects the stack a call would leave: its return address at [%esp] and its first argument at [%esp+4]. The extra word is that return address. With junk there, the attack still spawns the shell, but when the shell exits system returns to the junk address and the program crashes (typically a segmentation fault), leaving evidence. With &exit there, the program terminates normally.

3. A student finds the address of MYSHELL using a helper program called env55, then attacks a program called retlib. The attack fails: system runs but reports a “not found” error for a strange command. What went wrong?

Answer

The environment strings and the program name are packed together at the top of the stack, so the address of MYSHELL depends on the length of the program name. env55 and retlib have different name lengths (5 vs. 6 characters), so the variable sits at a slightly different address in retlib than the helper reported. The student’s pointer lands a byte or so off — in the middle of MYSHELL=/bin/sh or a neighboring string — and system tries to run that fragment as a command. The helper’s name must have the same length as the target’s, or the student must measure the address in the target itself.

4. Why can an attacker use system@plt in a non-PIE binary even when ASLR is fully enabled?

Answer

ASLR randomizes where libc is loaded, but a non-PIE executable is always loaded at its fixed link address (0x08048000 on 32-bit), so its PLT stubs are at fixed, known addresses. Returning into system@plt jumps through the GOT, which the dynamic linker has filled with the current address of system in libc. The attacker never needs to know where libc is. (This only works if the program itself references system, so that a PLT entry exists.)

5. You want to call f(1) and then g(2, 3) by returning from one into the other, without any gadgets. Draw the stack and explain the problem. How does a pop; ret gadget fix it?

Answer

Without gadgets the layout is [&f][&g][1][2][3] (low to high). When f returns, %esp moves past &g and points at the word holding 1 — which g then treats as its return address, and its arguments become 2 and 3. So 1 must serve both as f’s argument and as g’s return address; we cannot choose them independently, and the chain cannot continue meaningfully after g. With a gadget, the layout becomes [&f][&(pop;ret)][1][&g][ret-for-g][2][3]: f returns into the gadget, pop discards 1, and the gadget’s ret jumps to g, which now has its own return-address slot and its own arguments.

6. What is an “unintended” gadget, and why does x86 have so many of them compared with a fixed-width ISA such as ARM64?

Answer

An unintended gadget is an instruction sequence that the compiler never emitted: it appears when execution starts in the middle of a legitimate instruction, so the CPU decodes the remaining bytes (immediates, displacements, opcodes of later instructions) as different instructions that happen to end in a ret byte (0xc3). x86 instructions have variable length and no alignment requirement, so every byte offset is a possible decoding start. On a fixed-width ISA, instructions must be aligned, so execution can only start at intended instruction boundaries, and only the compiler’s own instruction sequences are available.

7. Why is ret2libc against a 64-bit program already a ROP attack, and what two other obstacles appear on 64-bit that do not exist on 32-bit?

Answer

On x86-64 the first argument is passed in %rdi, not on the stack, so returning into system with &"/bin/sh" on the stack does nothing; a pop %rdi; ret gadget is needed to load the argument, making even the simplest attack a two-gadget chain. The other obstacles: every user-space address contains zero bytes in its upper half, so a string-copy overflow can only write one address, at the very end of the payload; and the stack must be 16-byte aligned when system runs (it uses movaps), which often requires an extra bare ret gadget for alignment.

Summary

  • When NX blocks injected code, attackers reuse code that is already executable, most commonly functions in libc such as system, execv, mprotect, and exit.
  • A ret can impersonate a call if the attacker lays out the stack as [&func][return address][arg1][arg2].... Using &exit as system’s return address makes the process exit cleanly.
  • The needed addresses come from gdb (after the program has started), from the libc file plus its base address, or from environment variables — whose addresses shift with anything stored above them, including the program name.
  • The PLT/GOT mechanism resolves library calls at run time. In a non-PIE binary, system@plt is at a fixed address even under ASLR; GOT entries are both a leak source and an overwrite target.
  • Chaining calls with arguments needs stack-cleanup gadgets (pop ...; ret, add $n,%esp; ret) to lift %esp over each function’s arguments.
  • Return-oriented programming generalizes this: gadgets are the instruction set, %esp is the program counter. x86’s variable-length encoding provides abundant unintended gadgets, and libc’s gadgets are Turing-complete. Tools such as ROPgadget find gadgets and build chains automatically.
  • On x86-64, arguments live in registers (so pop %rdi; ret is required), addresses contain zero bytes, and the stack must stay 16-byte aligned.
  • Defenses: ASLR and canaries make code reuse harder; control-flow integrity (Chapter 8) targets it directly.

Connection to the Labs

Lab 2: Return-to-libc Attack is this chapter, hands-on:

  • Task 1 asks for the addresses of system() and exit(); Section 7.3 explains why you must run the program first and why you must debug the Set-UID binary itself.
  • Task 2 places "/bin/sh" in an environment variable. Use the stack-top picture in Section 7.3 to explain why the helper’s name length matters.
  • Task 3 is the [&system][&exit][&"/bin/sh"] payload from Section 7.2. The lab asks you to derive the offsets — do that from your own stack layout rather than from the example numbers in this chapter. Its two “attack variations” test exactly the two subtleties discussed here: the role of exit, and the program-name effect.
  • Task 4 (optional) replaces system with an exec-family call to keep root privilege, which requires solving the zero-byte problem for the argv array.
  • Task 5 builds a chain of returns into foo(). Before you start, revisit the overlap problem in Section 7.6 and convince yourself why a function with no arguments can be chained without gadgets.

Further Reading

  • Hovav Shacham. “The Geometry of Innocent Flesh on the Bone: Return-into-libc without Function Calls (on the x86).” ACM CCS, 2007.
  • Stephen Checkoway, Lucas Davi, Alexandra Dmitrienko, Ahmad-Reza Sadeghi, Hovav Shacham, and Marcel Winandy. “Return-Oriented Programming without Returns.” ACM CCS, 2010.
  • 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” — a clear walkthrough of the PLT and GOT. https://eli.thegreenplace.net/2011/11/03/position-independent-code-pic-in-shared-libraries
  • Sergey Bratus et al. “What are Weird Machines?” https://www.cs.dartmouth.edu/~sergey/wm/
  • ROPgadget: https://github.com/JonathanSalwan/ROPgadget
  • Chapter 5 of Wenliang Du, Computer & Internet Security: A Hands-on Approach.