- Introduced by
- Hovav Shacham
- Year
- 2007
- Type
- code reuse attack methodology
- Family
- Exploit technique
What does it do?
Return-Oriented Programming (ROP) is an exploit technique that performs computation by chaining short instruction sequences, called gadgets, that already exist in a program's executable code or libraries.
Each gadget ends in a return instruction, so control passes from one gadget to the next according to attacker-supplied stack data. Shacham (2007) showed a return-into-libc attack on x86 that calls no functions and combines short instruction sequences into gadgets allowing arbitrary computation. Roemer et al. (2012) introduced return-oriented programming as a named technique. It remains relevant where data execution prevention blocks the injection of new code.
The intuition behind the method
A return instruction takes its target address from the stack. If an attacker can overwrite the stack, they choose which address is loaded next, and therefore which existing instruction sequence runs. Each gadget performs a small operation and leaves its results in registers or memory, where the next gadget can use them. Because the attacker reuses code that is already executable, nothing new has to be written into memory, which is why defences that forbid writable-and-executable memory do not stop the chain by themselves.
History
Return-into-libc attacks, which redirect control to existing library functions, came before ROP. Shacham's 2007 CCS paper showed a return-into-libc attack on x86 that calls no functions, combining short instruction sequences into gadgets that allow arbitrary computation. Roemer, Buchanan, Shacham and Savage (2012) introduced return-oriented programming as a named technique, described its Turing-complete gadget set, and presented a high-level language and compiler for ROP exploits. They also noted that it defeats W⊕X protections.
When to use it
ROP is relevant when an attacker already has a memory-corruption primitive, such as a stack buffer overflow, in a program whose code cannot be modified by injection. Defenders use ROP mainly to test mitigations such as address space layout randomisation (ASLR) and control-flow checks, and to judge how exploitable their own binaries are under a given configuration. It is not a suitable choice when no memory-corruption flaw exists. Use ROP only on systems you are authorised to test, and handle exploit code in line with coordinated disclosure practice.
Limitations and considerations
- It requires a prior memory-corruption vulnerability to redirect the return address.
- Gadget discovery and chain construction are labour-intensive; automated tools reduce this effort but do not remove it.
- ASLR changes the addresses of gadgets, and code diversity reduces the number of usable gadgets; attackers may need additional information about memory addresses to build a working chain.
- Chains that implement complex computation are long and hard to construct.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- Uses instructions that are already executable, so it does not need writable-and-executable memory and is not stopped by a non-executable-memory policy alone.
- Gadgets come from legitimate binaries and libraries, so the attacker does not need to add code to the target.
- Roemer et al. show that a Turing-complete gadget set can be built from standard C libraries on two different architectures.
- It shows that preventing code injection does not by itself prevent malicious computation, which is the main point of the 2012 analysis.
Applications
- Research on defences against code-reuse attacks, including evaluation of ASLR and control-flow protections.
- Security testing of systems that already block code injection, to check whether code reuse remains possible.
- Proof-of-concept exploits used in vulnerability disclosure and bug bounty programmes.
- Academic work on gadget discovery, exploit automation and exploit description languages.
How it works
How it works
Static analysis of the target executable and the libraries it loads locates instruction sequences that end in a return instruction. Shacham's 2007 work discovers such sequences this way and relies on properties of the x86 instruction set.
Gadgets are chosen for the operations they perform, such as moving data, arithmetic or making system calls, and for whether they can follow one another. Roemer et al. (2012) built a Turing-complete set of gadgets from standard C libraries on Linux/x86 and Solaris/SPARC.
Gadget addresses and their operand values are arranged on the stack so that each return transfers control to the next gadget. The stack contents therefore supply both the control flow and the data each gadget consumes.
A memory-corruption vulnerability, such as a stack buffer overflow, is used to overwrite a return address and redirect control to the first gadget. From that point the chain determines the program's behaviour.
Common pitfalls
- Underestimating the effort needed to build a reliable chain, because gadget addresses and their order change with the build, patch level or layout of the target.
- Assuming that one mitigation such as ASLR or CFG removes ROP risk. The FAQ explains why these measures raise the cost of attacks without eliminating them.
- Neglecting the underlying memory-corruption bug. Mitigating ROP does not replace secure coding and fixing the vulnerability itself.
Frequently asked questions
Is ROP Turing-complete?
Roemer et al. (2012) construct a Turing-complete set of gadgets from standard C libraries on Linux/x86 and Solaris/SPARC. In that sense ROP can implement any computable function, provided suitable gadgets are available in the target. Whether a particular target offers such gadgets depends on its own code and libraries. In practice, building complex computations is difficult.
How does ASLR prevent ROP attacks?
ASLR randomises memory layouts, so gadget addresses are unpredictable. Attackers may need to work around it, for example by obtaining addresses through an information disclosure. ASLR significantly complicates ROP exploitation but does not eliminate it.
What is Control Flow Guard (CFG)?
CFG is a control-flow integrity defence that checks the targets of indirect control-flow transfers against a set of valid destinations, which narrows the addresses an attacker can use. Like other mitigations, it raises the cost of code-reuse attacks without eliminating them.
Can modern defenses completely prevent ROP attacks?
Modern defences such as ASLR, CFG and code diversity raise the cost of ROP exploitation considerably but do not eliminate it. Layering several mitigations, together with fixing the underlying vulnerabilities, is necessary.
What is a ROP gadget?
A ROP gadget is a short instruction sequence that ends in a return instruction and is already present in the program's code or its libraries. Gadgets are the building blocks from which ROP chains are assembled.
Related methods
Sources
Shacham, H. (2007). The geometry of innocent flesh on the bone: Return-into-libc without function calls (on the x86). In Proceedings of the 14th ACM Conference on Computer and Communications Security (CCS 2007), pp. 552-561.
Open DOI referenceRoemer, R., Buchanan, E., Shacham, H., & Savage, S. (2012). Return-oriented programming: Systems, languages, and applications. ACM Transactions on Information and System Security (TISSEC), 15(1), 1-34.
Open DOI reference