Skip to content
Memory corruption Write-up Advanced

Iconv, Set The Charset To RCE: Exploiting The Glibc To Hack The PHP Engine (Part 2)

A 24-year-old buffer overflow in glibc's iconv implementation can be triggered through PHP's iconv() function when converting attacker-controlled input to a specific output charset. In Roundcube webmail, a standard user could supply a charset and crafted recipient fields to corrupt the heap, leak memory and ultimately achieve remote code execution.

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 an off-by-a-few-bytes overflow in glibc's iconv conversion to ISO-2022-CN-EXT, reachable because PHP's iconv() allocates an output buffer only 32 bytes larger than the input and lets the caller choose the output charset.

Impact

A standard Roundcube user could turn the overflow into remote code execution, though the exploit depends on heap grooming within a single request and on the worker architecture keeping memory layout stable.

What the attacker needed

A Roundcube account able to send mail; control of the _charset parameter; crafted _to, _cc and _bcc fields

How it was found

  1. Shape the PHP heap using GET, POST and cookie parameters so four chunks are contiguous and free in a chosen order
  2. Send an invalid address in the _to field so Roundcube stores it as the target string
  3. Use the _cc field with a crafted charset to trigger the overflow and displace a free-list pointer
  4. Use the _bcc field to force further allocations that overlap the target string and overwrite its length field
  5. Let Roundcube display the error message so the enlarged string leaks adjacent memory

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 buffer overflow is when a program writes more data into a memory area than it was sized for, so it spills into neighbouring memory. Here a conversion library used by PHP mishandles certain characters, letting an attacker scribble a few bytes past a buffer. In Roundcube, a logged-in user controls the character set used to process email addresses, so they can trigger this and gradually bend the program's memory 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.

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.