Escaping a Python sandbox with a memory corruption bug
An integer overflow in Numpy's resize API allowed an array to be allocated with insufficient size, giving an attacker an arbitrary read/write primitive. In a Python sandbox, this could be used to overwrite a GOT entry and execute commands, breaking out of the sandbox.
medium.com
Published on medium.com
A few weeks ago I decided to scratch an itch I’ve been having for a while — to participate in some bug bounty programs. Perhaps the most daunting task of the bug bounty game is to pick a program which yields the highest return on investment.
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
The resize function multiplied the dimensions together without checking for integer overflow, so a huge shape produced a small allocation.
Impact
An attacker could break out of a Python sandbox and execute arbitrary commands, though the author notes the exploit was not tested against the vendor's updated sandbox.
What the attacker needed
Ability to run Python code in the sandbox; Numpy whitelisted for import; Numpy version before 1.12.0
How it was found
- Find a web application that runs user-submitted Python code in a sandbox.
- Review the whitelisted Python modules and their C code for memory corruption bugs.
- Identify an integer overflow in Numpy's resize when multiplying large dimensions.
- Craft an array with an overflown size to obtain an arbitrary read/write primitive.
- Use the primitive to overwrite fwrite's GOT entry with the address of system.
- Call print with a command to execute it via the hijacked function.
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.
A sandbox is a restricted environment meant to safely run untrusted code. Numpy is a Python library that does heavy maths in fast C code. A bug in that C code let a user trick Numpy into creating an array that was smaller than it claimed, so reading or writing past its end touched other memory. That let the user take control of the program and run their own commands.
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.