securityhacking

Kept in this browser · My desk

Stands for: return-oriented programming

Return-oriented programming: an exploitation technique that reuses short sequences of a program's existing instructions, each ending in a return, chained through return addresses the attacker writes on the stack, so that no new code needs to be injected.

ROP is why non-executable memory was not the end of code-execution exploits. Hovav Shacham showed in 2007 that the standard C library contains enough such sequences, many of them never placed there on purpose by the compiler, to compute anything. Intel and Arm now describe ROP in their architecture manuals and answer it in the instruction set, with shadow stacks, pointer authentication, branch target identification and guarded control stacks.

What it is. Return-oriented programming (ROP) builds an attacker's program out of fragments of the victim's own code. Each fragment, a gadget, is a short instruction sequence ending in a return; the attacker writes a chain of gadget addresses onto the stack, and every return pops the next address and jumps there. No new code is injected, so defences that only stop injected code do not stop it. The technique needs a prior memory-corruption bug that lets the attacker control the stack; it is how such a bug is turned into arbitrary computation.

Before ROP: return-into-libc. On 10 August 1997 Solar Designer posted to the Bugtraq list a message titled Getting around non-executable stack (and fix): "I finally decided to post a return-into-libc overflow exploit." Instead of returning into code placed on the stack, the overwritten return address pointed into an existing library function. That showed non-executable stacks were not enough, but it was limited to the functions the library offered.

The idea. A defence called W-xor-X makes memory writable or executable but never both, so injected code cannot run. Shacham's paper, The Geometry of Innocent Flesh on the Bone, presented at the Association for Computing Machinery's Conference on Computer and Communications Security (ACM CCS) in 2007, showed that an attacker who controls the stack can instead chain gadgets: short instruction sequences ending in ret, each return jumping to the next gadget's address. His thesis was a conjecture that in any sufficiently large body of x86 code there will be sequences allowing such gadgets, and he concluded that W-xor-X "is much less useful than previously thought". He found enough gadgets in the standard C library to say that the set "is Turing complete by inspection, so return-oriented programs can do anything possible with x86 code".

Why x86 makes it easy. x86 instructions have no fixed length, so bytes read from an unintended offset decode as other instructions. Shacham's example: f7 c7 07 00 00 00 and 0f 95 45 c3 (a test and a setnz) contain, one byte later, a mov, an xchg, an inc and a ret, the last being the byte c3 of the original operand. The vocabulary is described here at the level of ideas only.

Not only x86, and not only ret. In 2008 Erik Buchanan, Ryan Roemer, Hovav Shacham and Stefan Savage generalized the technique to RISC in When Good Instructions Go Bad (ACM CCS 2008), arguing that the attack was "both more general and a greater threat" than first appreciated, since it did not depend on decoding the middle of variable-length instructions. In 2010 Stephen Checkoway and colleagues from UC San Diego and Ruhr-Universität Bochum showed Return-Oriented Programming without Returns: on x86 and ARM, sequences that "behave like a return" occur often enough in large libraries on Linux and Android to build Turing-complete gadget sets, which defeated defences that watched for too-frequent return instructions.

The defences in silicon. Intel's Software Developer's Manual (SDM) opens its chapter on Control-flow Enforcement Technology (CET) by calling ROP and its CALL/JMP variants "the prevalent attack methodologies for stealth exploit writers"; CET adds a shadow stack, "a second stack for the program that is used exclusively for control transfer operations", against which every RET is checked, and indirect branch tracking with ENDBR64 (F3 0F 1E FA). Arm adds PACIASP, which inserts a pointer authentication code into the return address in X30 using SP as modifier, BTI (branch target identification) for branch targets, and the Guarded Control Stack, introduced in its manual with the sentence that ROP techniques involve changing the return address of procedure calls.

The defences in software. Address space layout randomization (ASLR), as the PaX project designed it, introduces "randomness into addresses used by a given task", so that an attack needing known gadget addresses fails "with a quantifiable probability" and the failed attempts, which usually crash the task, can be detected. The shadow stack needs the whole stack of software to take part. The Linux kernel documentation describes it as "a secondary stack allocated from memory which cannot be directly modified by applications": on CALL the processor pushes the return address to both stacks, on return it compares them and raises a control-protection fault if they differ; user shadow stacks need supporting hardware, a kernel built with X86_USER_SHADOW_STACK, Binutils 2.29 or LLVM 6 or later, and libraries compiled for it. On Windows, the /CETCOMPAT linker option marks an x64 binary as compatible with the CET shadow stack, which Microsoft describes as defending "against return-oriented programming (ROP) based malware attacks". Stack canaries add a check before the corrupted return is used; none of these removes the root cause, which is memory corruption.

What goes wrong. Each defence covers part of the problem: randomization falls to a single leaked address, shadow stacks protect returns but not indirect jumps and calls (hence indirect branch tracking and BTI), and protections that binaries must opt into leave old binaries exposed. Memory-safe languages remove the bug class that makes ROP possible in the first place.

Neighbours. ROP is an exploitation technique for buffer overflows and other memory-corruption bugs, a reply to the no-execute (NX) bit, made harder by ASLR, stack canaries, shadow stacks and pointer authentication, and found with a disassembler that reads machine code from every offset.

Also known as: return-oriented programming, programação orientada a retorno, ROP chain, gadget, return-into-libc

Sources

← Back to R Back to the glossary