Exploiting a Go Binary
A Go compiler bug allowed an uninitialized embedded struct pointer to be used as a base for member access, enabling an arbitrary write primitive. An attacker could use this to overwrite a function pointer and execute shellcode, though the specific target was sandboxed and the issue was fixed.
codearcana.com
Published on codearcana.com
Earlier this year, tylerni7 showed us a proof of concept for a 32 bit Go exploit using this issue. geohot and I had a wager over who could get the first remote code execution on play.golang.org: he won, but just barely ;-). Props also to ricky for helping to find the underlying cause/writing the patch.
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 Go compiler failed to emit a nil-pointer check when accessing a field through an embedded pointer that is the first element of a struct, assuming it would always segfault, but a large struct offset can make the access valid.
Impact
An attacker could achieve arbitrary code execution, but the specific target was sandboxed and the exploit does not work in later Go versions with non-executable heaps.
What the attacker needed
Ability to run Go code on the target; target uses vulnerable Go version
How it was found
- Define a struct with an embedded pointer to a very large struct containing an offset array and a target field.
- Create an instance of the outer struct without initialising the embedded pointer.
- Access the target field through the instance to write to an address determined by the offset.
- Use the arbitrary write to overwrite a function pointer in the data segment with the address of shellcode.
- Trigger the overwritten function pointer to execute the shellcode.
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.
In Go, you can embed one struct inside another to reuse its fields. If the embedded struct is a pointer and is not initialised, accessing its fields should crash, but due to a compiler mistake it instead reads or writes memory at a low address. By making the embedded struct very large, an attacker can turn this into a write to any address, which can then be used to run their own code.
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.