Skip to content
Memory corruption Write-up Advanced

mysqlnd/pdo password buffer overflow leading to RCE (CVE 2022-31626)

A buffer overflow in PHP's mysqlnd extension occurs when a rogue MySQL server requests cleartext authentication, causing the client to copy more data than allocated. An attacker controlling the MySQL server can trigger this overflow, potentially leading to remote code execution.

Charles Fol Follow

Published on bugs.php.net

There's a buffer overflow here: https://github.com/php/php-src/blob/master/ext/mysqlnd/mysqlndwireprotocol.c#L785

Charles Fol (@cfreal_), on bugs.php.net

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

Impact

An attacker controlling a MySQL server can trigger a buffer overflow in PHP clients, potentially leading to remote code execution.

What the attacker needed

PHP connects to attacker-controlled MySQL server; Attacker can run a rogue MySQL server

The fix

The fix was to include the header size when calculating the total packet size for allocation.

How it was found

  1. Start a fake MySQL server that requests cleartext authentication.
  2. Run PHP with a PDO connection to the fake server, providing a long password.
  3. Observe the out-of-bounds write in a debugger.

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 PHP connects to a MySQL database, it sends the password. A bug in how it prepares that password can cause it to write past the end of a memory buffer. If an attacker runs a fake database server, they can exploit this to run their own code on the PHP server.

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 PHP

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.