Skip to content
Memory corruption Disclosed report Intermediate

Stack Buffer-Overflow in MariaDB Charset_collation_map_st::insert_or_replace()

A stack buffer overflow exists in MariaDB's charset collation map handling, where user-supplied identifiers are copied into fixed-size stack buffers using the attacker-controlled length rather than the buffer size. Any authenticated user can crash the entire server with a single SET statement, and on builds without stack protection it may be exploitable for code execution.

lukas_kupczyk Follow

Originally disclosed on HackerOne

  • Triaged in 11 days
  • Fixed in 3 months
  • Disclosed after 3 months

Description:

Stack-based buffer overflow in MariaDB Server's Charset_collation_map_st::insert_or_replace() function in sql/charset_collations.cc. The function copies user-supplied charset and collation identifiers into fixed-size stack buffers (33 and 65 bytes respectively) using strmake() with the attacker-controlled token length as the size bound instead of the destination buffer size. Any authenticated user can crash the entire mariadbd process with a single SET @@session.character_set_collations statement containing an overlong identifier.

Code path: SET statement -> Sys_var_charset_collation_map::do_check() -> charset_collation_map_from_item() -> from_text() -> insert_or_replace() -> strmake()

The vulnerable function in sql/charset_collations.cc:

C
bool Charset_collation_map_st::insert_or_replace(const LEX_CSTRING &cs_name,
                                                  const LEX_CSTRING &cl_name,
                                                  bool error_on_unknown,
                                                  myf utf8_flag)
{
  // Line 57-58: charset_name buffer overflow
  char charset_name_c[MY_CS_CHARACTER_SET_NAME_SIZE + 1];  // 33 bytes
  strmake(charset_name_c, cs_name.str, cs_name.length);    // VULNERABLE: length is unbounded

  // ...

  // Line 67-68: collation_name buffer overflow
  char collation_name_c[MY_CS_COLLATION_NAME_SIZE + 1];    // 65 bytes
  strmake(collation_name_c, cl_name.str, cl_name.length);  // VULNERABLE: length is unbounded
}

The unbounded tokenizer in sql/simple_tokenizer.h:

C
LEX_CSTRING get_ident() {
  const char *start= m_ptr++;
  for ( ; is_ident_body(); m_ptr++) { }   // unbounded scan over [A-Za-z0-9_]
  return {start, (size_t)(m_ptr - start)};
}

Reproduction Steps:

  1. Connect to a MariaDB server (version 11.2.1 or later) as any authenticated user — even one with only USAGE privilege.

  2. Variant 1 — overflow charset_name_c[33] via a 200-character charset name:

SQL
SET @@session.character_set_collations='AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=x';
  1. Variant 2 — overflow collation_name_c[65] via a 200-character collation name:
SQL
SET @@session.character_set_collations='utf8mb3=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';
  1. Both variants crash the entire mariadbd process immediately. The client receives: ERROR 2013 (HY000): Lost connection to server during query. All other connected clients lose their sessions simultaneously.

  2. On ASAN builds, the error log will contain:

==230437==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x77bd5379eca1
WRITE of size 1 at 0x77bd5379eca1 thread T13
    #0 strmake /home/ubuntu/mariadb-server/strings/strmake.c:36
    #1 Charset_collation_map_st::insert_or_replace(...) /home/ubuntu/mariadb-server/sql/charset_collations.cc:58

Impact

Any authenticated user (USAGE-only privilege is sufficient) can crash the entire mariadbd process with a single SQL statement, disconnecting all clients and rolling back in-flight transactions.

On builds without stack protectors (e.g. custom builds, older distro packages, or platforms where -fstack-protector is not the default), the stack overflow may be exploitable for code execution. The overflow payload is limited to [A-Za-z0-9_] by the tokenizer, which constrains but does not necessarily prevent exploitation depending on the target architecture.

The original report as published on HackerOne, by lukas_kupczyk. 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 code passes the attacker-controlled token length to strmake() as the copy limit instead of the destination buffer size, so an overlong identifier overflows the fixed-size stack buffer.

Impact

Any authenticated user can crash the whole server, disconnecting all clients and rolling back in-flight transactions; on builds without stack protectors the overflow may be exploitable for code execution, though the restricted character set constrains this.

What the attacker needed

A MariaDB account (USAGE is enough); ability to send SQL statements

How it was found

  1. Connect to a MariaDB server as any authenticated user, even one with only USAGE privilege.
  2. Send a SET statement for the session character_set_collations variable with an overlong charset name.
  3. Alternatively send the same statement with an overlong collation name after the equals sign.
  4. Observe the server crash and the client losing its connection.

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.

A buffer is a small reserved area of memory. This code copies a name the user types into a fixed-size box without checking whether the name fits, so a very long name spills over into neighbouring memory and crashes the database server. Because the name is only allowed to contain letters, digits and underscores, turning the crash into running attacker code is harder but not ruled out on some builds.

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 lukas_kupczyk

View all

3 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.