GHSA-h35g-vwh6-m678 / CVE-2024-8929: [Mysqlnd] Leak partial content of the heap through heap buffer over-read
- Published:
- Fetched:
- Updated:
- Affects:
- php php-src
https://github.com/php/php-src/security/advisories/GHSA-h35g-vwh6-m678 ↗
[Mysqlnd] Leak partial content of the heap through heap buffer over-read ### Summary By connecting to a fake MySQL server or tampering with network packets and initiating a SQL Query, it is possible to abuse the function `static enum_func_status php_mysqlnd_rset_field_read` when parsing MySQL fields packets in order to include the rest of the heap content starting from the address of the cursor of the currently read buffer. Using PHP-FPM which stays alive between request, and between two different SQL query requests, as the previous buffer used to store received data from MySQL is not emptied and `malloc` allocates a memory region which is very near the previous one, one is able to extract the response content of the previous MySQL request from the PHP-FPM worker. ### Details After the connection against a MySQL database is established, when initiating a SQL query, for example through `mysqli_query`, the expected response from the server is of the following format : - A `column count` packet, describing the number of fields that are expected to be received; - As much as `fields packets` as there are fields to be transmitted; - An `intermediate EOF` packet, telling it is the end of the "fields packets"; - As much as `row packet` as there are entries to be transmitted. The MySQL `fields` packets, are parsed by the `static enum_func_status php_mysqlnd_rset_field_read` defined in `/ext/mysqlnd/mysqlnd_wireprotocol.c`. Here are the interesting parts : ``` C static enum_func_status php_mysqlnd_rset_field_read(MYSQLND_CONN_DATA * conn, void * _packet) { MYSQLND_PACKET_RES_FIELD *packet = (MYSQLND_PACKET_RES_FIELD *) _packet; MYSQLND_ERROR_INFO * error_info = conn->error_info; MYSQLND_PFC * pfc = conn->protocol_frame_codec; MYSQLND_VIO * vio = conn->vio; MYSQLND_STATS * stats = conn->stats; MYSQLND_CONNECTION_STATE * connection_state = &conn->state; const size_t buf_len = pfc->cmd_buffer.length; size_t total_len = 0; zend_uchar * const buf = (zend_uchar *) pfc->cmd_buffer.buffer; const zend_uchar * p = buf; const zend_uchar * const begin = buf; char *root_ptr; zend_ulong len; MYSQLND_FIELD *meta; ... // We learn here that it is not supposed to support COM_FIELD_LIST anymore. if (ERROR_MARKER == *p) { /* Error */ p++; BAIL_IF_NO_MORE_DATA; php_mysqlnd_read_error_from_line(p, packet->header.size - 1, packet->error_info.error, sizeof(packet->error_info.error), &packet->error_info.error_no, packet->error_info.sqlstate ); DBG_ERR_FMT("Server error : (%u) %s", packet->error_info.error_no, packet->error_info.error); DBG_RETURN(PASS); } else if (EODATA_MARKER == *p && packet->header.size < 8) { /* Premature EOF. That should be COM_FIELD_LIST. But we don't support COM_FIELD_LIST anymore, thus this should not happen */ DBG_ERR("Premature EOF. That should be COM_FIELD_LIST"); php_error_docref(NULL, E_WARNING, "Premature EOF in result field metadata"); DBG_RETURN(FAIL); } ... // Par Patched: 8.1.31, 8.2.26, 8.3.14