Skip to content
Memory corruption Disclosed report Advanced

Connector/ODBC: malicious server's long version string wraps size_t PrefixLen via negative _snprintf return, causing heap out-of-bounds write

The ODBC driver builds an error-message prefix from the server-supplied version string using a custom snprintf that returns a negative value on truncation; adding that negative to an unsigned size_t length wraps it to a huge value. A malicious or man-in-the-middle server sending a very long version string therefore causes an out-of-bounds heap write in the client when a statement is allocated, demonstrated as a crash.

fg0x0 Follow

Originally disclosed on HackerOne

Medium 4.6 View original
  • Triaged in 9 days
  • Fixed in 3 months
  • Disclosed after 3 months

MariaDB Connector/ODBC formats every diagnostic message with a prefix that embeds the server version string. The prefix is built in MADB_PutErrorPrefix (driver/ma_error.cpp:165) with the driver's custom _snprintf, which returns a NEGATIVE value when the output is truncated (driver/ma_platform_posix.cpp:41). The return value is added to error->PrefixLen, a size_t (unsigned, driver/ma_c_stuff.h:70). A malicious or MITM MariaDB server that sends a handshake server_version string longer than ~500 bytes causes _snprintf to truncate and return negative, wrapping PrefixLen to ~2^64. Every subsequent use of PrefixLen as an offset into the fixed 513-byte SqlErrorMsg buffer then writes out of bounds at a server-chosen backward offset.

This is an unauthenticated, remotely triggered client-side memory corruption that fires as soon as the application allocates a statement on the poisoned connection. Confirmed end-to-end with ASAN against the real driver built from HEAD 4e175d9 (v3.2.10).

Root cause

  1. Custom _snprintf returns negative on truncation (driver/ma_platform_posix.cpp:41):
C
int _snprintf(char *buffer, size_t count, const char *format, ...) {
    int result = vsnprintf(buffer, count, format, list);
    if (result > count) { return count - result - 1; }   // NEGATIVE
    return result;
}
  1. MADB_PutErrorPrefix adds the negative return to a size_t (driver/ma_error.cpp:155-170):
C
error->PrefixLen = strlen(MARIADB_ODBC_ERR_PREFIX);   // "[ma-3.2.10]" ~ 11
strcpy_s(error->SqlErrorMsg, SQL_MAX_MESSAGE_LENGTH + 1, MARIADB_ODBC_ERR_PREFIX);
if (dbc != NULL && dbc->mariadb != NULL) {
  error->PrefixLen += _snprintf(error->SqlErrorMsg + error->PrefixLen,
    SQL_MAX_MESSAGE_LENGTH + 1 - error->PrefixLen,
    "[%s]", mysql_get_server_info(dbc->mariadb));
    // ^^ attacker-controlled, uncapped version string from handshake
}

With a version string of length L > ~500, the _snprintf return is negative, and PrefixLen += <negative> wraps to approximately 2^64 - (L - 510). The backward distance is chosen by the attacker via the version-string length.

  1. Two OOB write sinks use the corrupted PrefixLen:
  • MADB_CLEAR_ERROR / MA_ClearError (driver/ma_error.h:168, driver/ma_api_internal.cpp:58): SqlErrorMsg[PrefixLen] = 0; - one-byte OOB write
  • MADB_SetNativeError (driver/ma_error.cpp:231,235): strcpy_s(SqlErrorMsg + PrefixLen, 513 - PrefixLen, msg) - multi-byte OOB write with server-controlled error text

Steps To Reproduce

Environment: Linux x86-64, unixODBC 2.3.14, driver built from HEAD 4e175d9 with AddressSanitizer (cmake -DWITH_ASAN=ON).

Step 0 - Confirm libmariadb exposes the long version (direct_version_check.c attached):

Bash
$ ./direct_version_check 3402   # rogue server version_len=2000
server_info_len=2000  first16=AAAAAAAAAAAAAAAA

mysql_get_server_info() returns the full 2000-byte handshake version uncapped.

Step 1 - Start the rogue server (rogue_server.py attached):

Bash
python3 rogue_server.py 3404 4000 &   # port 3404, version string = 4000 bytes

The rogue server speaks enough MariaDB wire protocol for the connection to succeed and advertises a 4000-byte server_version in the handshake.

Step 2 - POSITIVE (attack) - real ASAN-built driver + real unixODBC (odbc_client.c attached):

Bash
./odbc_client "DRIVER=.../build_asan/driver/libmaodbc.so;SERVER=127.0.0.1;PORT=3404;UID=u;PWD=p;SSLVERIFY=0;"

Output:

Bash
SQLDriverConnect r=0                        <- connection SUCCEEDS
allocating statement (MADB_StmtInit -> PutErrorPrefix)
==ERROR: AddressSanitizer: heap-buffer-overflow ... WRITE of size 1
    #0 MA_ClearError(short, void*)  driver/ma_api_internal.cpp:58
    #1 SQLGetStmtAttr               driver/odbc_3_api.cpp:997
    #2 libodbc.so.2                 (unixODBC Driver Manager)
    #3 main                         odbc_client.c:32

The OOB write fires at statement allocation. No application query needed.

Step 3 - NEGATIVE CONTROL - identical path, normal version string (20 bytes):

Bash
python3 rogue_server.py 3405 20 &
./odbc_client "DRIVER=...;SERVER=127.0.0.1;PORT=3405;UID=u;PWD=p;SSLVERIFY=0;"

Output:

Bash
SQLDriverConnect r=0
SQLAllocHandle(STMT) r=0
SQLExecDirect r=0 (survived)
clean exit    EXIT=0

Normal-length version string causes no corruption. The long version is the sole difference.

Automated reproduction: bash run_poc.sh (attached) builds the driver with ASAN, starts the rogue server, runs both positive and negative cases.

Supporting Material/References

  • Vulnerable function: MADB_PutErrorPrefix at driver/ma_error.cpp:165
  • Negative-return source: _snprintf at driver/ma_platform_posix.cpp:41
  • PrefixLen type: size_t at driver/ma_c_stuff.h:70
  • OOB sink 1: MA_ClearError at driver/ma_api_internal.cpp:58
  • OOB sink 2: MADB_SetNativeError at driver/ma_error.cpp:231
  • Trigger path: MADB_StmtInit at driver/ma_statement.cpp:4113
  • The same _snprintf negative-return footgun exists in MADB_DsnToString (Windows prompt path) and catalog-query builders - should be reviewed as part of the same fix
  • Attachments: rogue_server.py, odbc_client.c, direct_version_check.c, harness.cpp, run_poc.sh, poc_evidence.txt

Impact

A malicious or MITM MariaDB/MySQL server corrupts client-process memory (out-of-bounds write) in any application using MariaDB Connector/ODBC, the moment it opens a connection and allocates a statement. The demonstrated effect is a reliable crash (denial of service) via an AddressSanitizer-confirmed heap out-of-bounds write.

The write offset is chosen by the attacker through the version-string length, and the strcpy_s sink (MADB_SetNativeError) copies attacker-influenced content (server error text) to that attacker-chosen backward offset, so corruption of adjacent heap objects - and potentially impact beyond DoS - is plausible on non-hardened builds. Only the crash is demonstrated here.

Blast radius: every application (BI tools, ETL jobs, application servers, isql, etc.) that connects through MariaDB Connector/ODBC. Preconditions: (a) client connects to a server the attacker controls or can MITM (driver default is plaintext - no TLS), and (b) the application allocates a statement, which every ODBC workflow does. No credentials, privileges, or human interaction beyond initiating the connection are required.

The original report as published on HackerOne, by fg0x0. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

The custom _snprintf returns a negative value on truncation, and that value is added to an unsigned size_t prefix length, wrapping it to a huge offset used for later buffer writes.

Impact

A malicious or man-in-the-middle server can corrupt client memory and reliably crash any application using the driver; the text says corruption beyond denial of service is plausible but only the crash is demonstrated, and the default plaintext connection is what allows the man-in-the-middle case.

What the attacker needed

Client connects to attacker-controlled server; or attacker can MITM the plaintext connection; application allocates a statement

How it was found

  1. Confirm that the client library returns the full, uncapped handshake version string.
  2. Start a rogue server that advertises a very long server_version in its handshake.
  3. Connect the ASAN-built driver through unixODBC to the rogue server; the connection succeeds.
  4. Allocate a statement, which triggers the error-prefix code and the out-of-bounds write.
  5. Repeat against a server with a normal-length version string and observe a clean run.

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 report has not been written yet. The Original tab links to the source.

When a program connects to a database server, the server first sends a greeting that includes its version text. The driver copies that text into a fixed-size error buffer, but its own string-formatting helper reports a negative length when the text is too long, and that negative number is stored in an unsigned counter, so it becomes enormous. Later writes then land far outside the buffer, corrupting memory in the client program.

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 fg0x0

View all

17 more disclosed reports, best first.

More from MariaDB

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.