Skip to content
Memory corruption Write-up Advanced

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.

Charles Fol Follow

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.

Charles Fol (@cfreal_), on ambionics.io

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

  1. Open a conversion descriptor from UTF-8 to ISO-2022-CN-EXT.
  2. Place an exotic character that forces an escape sequence near the end of the input.
  3. Call iconv with an output buffer the same size as the input.
  4. 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.

More by Charles Fol

View all

8 more disclosed reports, best first.

More from GNU C Library (glibc)

View all

The newest disclosed reports for this program.

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.