Iconv, Set The Charset To RCE: Exploiting The Glibc To Hack The PHP Engine (Part 1)
A 24-year-old out-of-bounds write exists in the glibc iconv implementation when converting to the ISO-2022-CN-EXT charset, where escape sequences can be written past the end of the output buffer. The author shows this bug is reachable through PHP's php://filter iconv conversions, allowing a file-read primitive to be turned into remote code execution in PHP applications.
Published on ambionics.io
A few months ago, I stumbled upon a 24 years old buffer overflow in the glibc, the base library for linux programs. Despite being reachable in multiple well-known libraries or executables, it proved rarely exploitable — while it didn't provide much leeway, it required hard-to-achieve preconditions.
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 two of the three escape-sequence branches in the ISO-2022-CN-EXT conversion code lack the output-buffer bounds check that the first branch has, so a few bytes can be written out of bounds.
Impact
The overflow is only one to three bytes with non-controlled values, and many candidate targets such as libxml2 and pkexec allocate extra slack in their output buffers, so the bug is rarely exploitable; on PHP it can corrupt a free-list pointer and, per the author, be turned into remote code execution.
What the attacker needed
Control of the output charset; control of part of the input buffer; a suitably sized output buffer; a PHP file-read primitive
The fix
The missing bounds checks in the escape-sequence branches should be added so the conversion stops with a full-output result instead of writing past the buffer.
How it was found
- Open a conversion descriptor from UTF-8 to ISO-2022-CN-EXT.
- Place an exotic character that forces an escape sequence near the end of the input.
- Call iconv with an output buffer the same size as the input.
- Observe that more bytes are written than the output buffer allows.
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.
When a program changes text from one character encoding to another, it uses a conversion library. In one specific encoding, that library can write a few bytes past the end of the memory area it was given, corrupting nearby data. Because PHP uses this library for its filter feature, an attacker who can only read files may be able to corrupt memory and eventually 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.