Bytecode Breakdown: Unraveling Factorio's Lua Security Flaws
Factorio's Lua interpreter accepted precompiled bytecode from custom maps, and its hand-written bytecode verifier had an off-by-one in the jump opcode that let an attacker jump outside the code block. A malicious server could therefore run crafted bytecode on connecting clients and, by abusing unchecked type handling in the FORLOOP opcode, leak memory addresses and build fake objects towards arbitrary code execution.
Published on memorycorruption.net
Some months ago I exploited a vulnerability in the Lua implementation of Factorio that allowed a malicious server to obtain arbitrary execution on clients. As the vulnerability has been patched for months already (Factorio versions below 1.1.101 are affected), is time to share the details with the community.
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the bytecode verifier mishandled the jump opcode's built-in offset by one, permitting a jump outside the code chunk into the constants section, and the FORLOOP opcode never type-checked its step argument.
Impact
A malicious server could achieve arbitrary code execution on clients that connect to it, affecting Factorio versions below 1.1.101.
What the attacker needed
Attacker hosts a Factorio server; victim connects to that server; game version below 1.1.101
The fix
It was fixed in Factorio 1.1.101, and the text notes that applications running untrusted Lua should reject precompiled scripts.
How it was found
- Host a Factorio server serving a malicious map whose scenario Lua contains the exploit
- Wait for a client to connect, download the map and execute its Lua code
- Use crafted bytecode to jump outside the code block and run constants as bytecode
- Exploit the unchecked FORLOOP step to leak addresses via a type confusion
- Use the leaked addresses to craft fake objects and corrupt memory
- Leverage the primitives to gain code execution on the client
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
Factorio lets a server send a map that contains Lua code, and every connecting player runs that code so their games stay in sync. Lua normally compiles code before running it, and the game's checks on that compiled form were incomplete, so a crafted map could make the game read and write memory it should not. That could let the server run its own code on the player's computer.
What is Memory corruption?
Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.