Skip to content
Memory corruption Write-up Advanced

Exploiting Lua 5.1 on 32-bit Windows

A researcher showed that hand-crafted Lua 5.1 bytecode can break the type assumptions of the interpreter's numeric for-loop opcodes, letting a script reinterpret a string as a closure and overwrite a C function pointer. This allowed loading an arbitrary DLL from inside a locked-down Lua sandbox in a game, escaping the sandbox.

gist.github.com

Published on gist.github.com

Clone this repository at <script src="https://gist.github.com/corsix/6575486.js"></script> - Save corsix/6575486 to your computer and use it in GitHub Desktop.

The author, on gist.github.com

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 interpreter's for-loop opcodes trust that a preceding preparation instruction validated the loop values as numbers, so a hand-written bytecode stream that skips that check makes the loop treat arbitrary value bytes as a double, enabling type confusion.

What the attacker needed

32-bit Lua 5.1 runtime; ability to supply a compiled script; assertions disabled in the build

How it was found

  1. Confirm the runtime is 32-bit Lua 5.1 by checking the bytecode header.
  2. Patch the generated bytecode so the numeric for-loop preparation instruction is removed, producing a function that reinterprets any value as a number.
  3. Use stack slot reuse and string concatenation to build fake string and closure structures in memory.
  4. Point a closure's upvalue at the stack slot holding the running function so the call frame can be manipulated.
  5. Overwrite the C closure's function pointer with the address of the library loader.
  6. Call the closure with the DLL name to load it.

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.

Lua is a small scripting language often embedded in games and apps to run add-ons. The program that runs Lua code is meant to keep add-ons in a safe box, but the author found that a specially built compiled script can confuse the interpreter about what kind of value it is holding. That confusion lets the script reach into the program's memory and run its own native code, which is the escape.

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.

Similar reports

View all

Same bug class and techniques, clearest first.

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.