Skip to content

Commit b0d7399

Browse files
Bharathy SatishDaniel Horecki
authored andcommitted
Bug #25717383: MYSQLDUMP MAY EXECUTE ANY ARBITRARY QUERY
While writing comments if database object names has a new line character, then next line is considered a command, rather than a comment. This patch fixes the way comments are constructed in mysqldump. (cherry picked from commit 1099f9d17b1c697c2760f86556f5bae7d202b444)
1 parent 56769a7 commit b0d7399

File tree

3 files changed

+155
-12
lines changed

3 files changed

+155
-12
lines changed

client/mysqldump.c

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,7 @@ static int dump_tablespaces_for_databases(char** databases);
564564
static int dump_tablespaces(char* ts_where);
565565
static void print_comment(FILE *sql_file, my_bool is_error, const char *format,
566566
...);
567+
static const char* fix_identifier_with_newline(char*);
567568

568569

569570
/*
@@ -664,7 +665,7 @@ static void write_header(FILE *sql_file, char *db_name)
664665
MACHINE_TYPE);
665666
print_comment(sql_file, 0, "-- Host: %s Database: %s\n",
666667
current_host ? current_host : "localhost",
667-
db_name ? db_name : "");
668+
db_name ? fix_identifier_with_newline(db_name) : "");
668669
print_comment(sql_file, 0,
669670
"-- ------------------------------------------------------\n"
670671
);
@@ -2000,6 +2001,30 @@ static void print_comment(FILE *sql_file, my_bool is_error, const char *format,
20002001
print_xml_comment(sql_file, strlen(comment_buff), comment_buff);
20012002
}
20022003

2004+
/*
2005+
This function accepts object names and prefixes -- wherever \n
2006+
character is found.
2007+
2008+
@param[in] object_name
2009+
2010+
@return
2011+
@retval fixed object name.
2012+
*/
2013+
2014+
static const char* fix_identifier_with_newline(char* object_name)
2015+
{
2016+
static char buff[COMMENT_LENGTH]= {0};
2017+
char *ptr= buff;
2018+
memset(buff, 0, 255);
2019+
while(*object_name)
2020+
{
2021+
*ptr++ = *object_name;
2022+
if (*object_name == '\n')
2023+
ptr= strmov(ptr, "-- ");
2024+
object_name++;
2025+
}
2026+
return buff;
2027+
}
20032028

20042029
/*
20052030
create_delimiter
@@ -2068,7 +2093,8 @@ static uint dump_events_for_db(char *db)
20682093

20692094
/* nice comments */
20702095
print_comment(sql_file, 0,
2071-
"\n--\n-- Dumping events for database '%s'\n--\n", db);
2096+
"\n--\n-- Dumping events for database '%s'\n--\n",
2097+
fix_identifier_with_newline(db));
20722098

20732099
/*
20742100
not using "mysql_query_with_error_report" because we may have not
@@ -2285,7 +2311,8 @@ static uint dump_routines_for_db(char *db)
22852311

22862312
/* nice comments */
22872313
print_comment(sql_file, 0,
2288-
"\n--\n-- Dumping routines for database '%s'\n--\n", db);
2314+
"\n--\n-- Dumping routines for database '%s'\n--\n",
2315+
fix_identifier_with_newline(db));
22892316

22902317
/*
22912318
not using "mysql_query_with_error_report" because we may have not
@@ -2344,7 +2371,7 @@ static uint dump_routines_for_db(char *db)
23442371
query_buff);
23452372
print_comment(sql_file, 1,
23462373
"-- does %s have permissions on mysql.proc?\n\n",
2347-
current_user);
2374+
fix_identifier_with_newline(current_user));
23482375
maybe_die(EX_MYSQLERR,"%s has insufficent privileges to %s!", current_user, query_buff);
23492376
}
23502377
else if (strlen(row[2]))
@@ -2558,11 +2585,11 @@ static uint get_table_structure(char *table, char *db, char *table_type,
25582585
if (strcmp (table_type, "VIEW") == 0) /* view */
25592586
print_comment(sql_file, 0,
25602587
"\n--\n-- Temporary table structure for view %s\n--\n\n",
2561-
result_table);
2588+
fix_identifier_with_newline(result_table));
25622589
else
25632590
print_comment(sql_file, 0,
25642591
"\n--\n-- Table structure for table %s\n--\n\n",
2565-
result_table);
2592+
fix_identifier_with_newline(result_table));
25662593

25672594
if (opt_drop)
25682595
{
@@ -2804,7 +2831,7 @@ static uint get_table_structure(char *table, char *db, char *table_type,
28042831

28052832
print_comment(sql_file, 0,
28062833
"\n--\n-- Table structure for table %s\n--\n\n",
2807-
result_table);
2834+
fix_identifier_with_newline(result_table));
28082835
if (opt_drop)
28092836
fprintf(sql_file, "DROP TABLE IF EXISTS %s;\n", result_table);
28102837
if (!opt_xml)
@@ -3519,21 +3546,23 @@ static void dump_table(char *table, char *db)
35193546
{
35203547
print_comment(md_result_file, 0,
35213548
"\n--\n-- Dumping data for table %s\n--\n",
3522-
result_table);
3549+
fix_identifier_with_newline(result_table));
35233550

35243551
dynstr_append_checked(&query_string, "SELECT /*!40001 SQL_NO_CACHE */ * FROM ");
35253552
dynstr_append_checked(&query_string, result_table);
35263553

35273554
if (where)
35283555
{
3529-
print_comment(md_result_file, 0, "-- WHERE: %s\n", where);
3556+
print_comment(md_result_file, 0, "-- WHERE: %s\n",
3557+
fix_identifier_with_newline(where));
35303558

35313559
dynstr_append_checked(&query_string, " WHERE ");
35323560
dynstr_append_checked(&query_string, where);
35333561
}
35343562
if (order_by)
35353563
{
3536-
print_comment(md_result_file, 0, "-- ORDER BY: %s\n", order_by);
3564+
print_comment(md_result_file, 0, "-- ORDER BY: %s\n",
3565+
fix_identifier_with_newline(order_by));
35373566

35383567
dynstr_append_checked(&query_string, " ORDER BY ");
35393568
dynstr_append_checked(&query_string, order_by);
@@ -4366,7 +4395,8 @@ static int init_dumping(char *database, int init_func(char*))
43664395
char *qdatabase= quote_name(database,quoted_database_buf,opt_quoted);
43674396

43684397
print_comment(md_result_file, 0,
4369-
"\n--\n-- Current Database: %s\n--\n", qdatabase);
4398+
"\n--\n-- Current Database: %s\n--\n",
4399+
fix_identifier_with_newline(qdatabase));
43704400

43714401
/* Call the view or table specific function */
43724402
init_func(qdatabase);
@@ -5372,7 +5402,7 @@ static my_bool get_view_structure(char *table, char* db)
53725402

53735403
print_comment(sql_file, 0,
53745404
"\n--\n-- Final view structure for view %s\n--\n\n",
5375-
result_table);
5405+
fix_identifier_with_newline(result_table));
53765406

53775407
/* Table might not exist if this view was dumped with --tab. */
53785408
fprintf(sql_file, "/*!50001 DROP TABLE IF EXISTS %s*/;\n", opt_quoted_table);

mysql-test/r/mysqldump.result

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5334,3 +5334,66 @@ a
53345334
DROP TABLE t1;
53355335
DROP TABLE t2;
53365336
DROP DATABASE db_20772273;
5337+
#
5338+
# Bug #25717383: MYSQLDUMP MAY EXECUTE ANY ARBITRARY QUERY
5339+
#
5340+
CREATE DATABASE bug25717383;
5341+
use bug25717383;
5342+
CREATE TABLE `tab
5343+
one` (a int);
5344+
CREATE VIEW `view
5345+
one` as SELECT * FROM `tab
5346+
one`;
5347+
CREATE PROCEDURE `proc
5348+
one`() SELECT * from `tab
5349+
one`;
5350+
CREATE TEMPORARY TABLE `temp
5351+
one` (id INT);
5352+
CREATE TRIGGER `trig
5353+
one` BEFORE INSERT ON `tab
5354+
one` FOR EACH ROW SET NEW.a = 1;
5355+
CREATE EVENT `event
5356+
one` ON SCHEDULE AT '2030-01-01 00:00:00' DO SET @a=5;
5357+
SHOW TABLES FROM bug25717383;
5358+
Tables_in_bug25717383
5359+
tab
5360+
one
5361+
view
5362+
one
5363+
SHOW TRIGGERS FROM bug25717383;
5364+
Trigger Event Table Statement Timing Created sql_mode Definer character_set_client collation_connection Database Collation
5365+
trig
5366+
one INSERT tab
5367+
one SET NEW.a = 1 BEFORE NULL root@localhost utf8 utf8_general_ci latin1_swedish_ci
5368+
SHOW EVENTS FROM bug25717383;
5369+
Db Name Definer Time zone Type Execute at Interval value Interval field Starts Ends Status Originator character_set_client collation_connection Database Collation
5370+
bug25717383 event
5371+
one root@localhost SYSTEM ONE TIME # NULL NULL NULL NULL ENABLED 1 utf8 utf8_general_ci latin1_swedish_ci
5372+
SELECT ROUTINE_NAME FROM INFORMATION_SCHEMA.ROUTINES
5373+
WHERE ROUTINE_SCHEMA='bug25717383' AND ROUTINE_TYPE= 'PROCEDURE'
5374+
ORDER BY ROUTINE_NAME;
5375+
ROUTINE_NAME
5376+
proc
5377+
one
5378+
SHOW TABLES FROM bug25717383;
5379+
Tables_in_bug25717383
5380+
tab
5381+
one
5382+
view
5383+
one
5384+
SHOW TRIGGERS FROM bug25717383;
5385+
Trigger Event Table Statement Timing Created sql_mode Definer character_set_client collation_connection Database Collation
5386+
trig
5387+
one INSERT tab
5388+
one SET NEW.a = 1 BEFORE NULL root@localhost utf8 utf8_general_ci latin1_swedish_ci
5389+
SHOW EVENTS FROM bug25717383;
5390+
Db Name Definer Time zone Type Execute at Interval value Interval field Starts Ends Status Originator character_set_client collation_connection Database Collation
5391+
bug25717383 event
5392+
one root@localhost SYSTEM ONE TIME # NULL NULL NULL NULL ENABLED 1 utf8 utf8_general_ci latin1_swedish_ci
5393+
SELECT ROUTINE_NAME FROM INFORMATION_SCHEMA.ROUTINES
5394+
WHERE ROUTINE_SCHEMA='bug25717383' AND ROUTINE_TYPE= 'PROCEDURE'
5395+
ORDER BY ROUTINE_NAME;
5396+
ROUTINE_NAME
5397+
proc
5398+
one
5399+
DROP DATABASE bug25717383;

mysql-test/t/mysqldump.test

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2440,3 +2440,53 @@ SELECT * FROM t2;
24402440
DROP TABLE t1;
24412441
DROP TABLE t2;
24422442
DROP DATABASE db_20772273;
2443+
2444+
--echo #
2445+
--echo # Bug #25717383: MYSQLDUMP MAY EXECUTE ANY ARBITRARY QUERY
2446+
--echo #
2447+
2448+
2449+
CREATE DATABASE bug25717383;
2450+
use bug25717383;
2451+
2452+
CREATE TABLE `tab
2453+
one` (a int);
2454+
CREATE VIEW `view
2455+
one` as SELECT * FROM `tab
2456+
one`;
2457+
2458+
CREATE PROCEDURE `proc
2459+
one`() SELECT * from `tab
2460+
one`;
2461+
2462+
CREATE TEMPORARY TABLE `temp
2463+
one` (id INT);
2464+
2465+
CREATE TRIGGER `trig
2466+
one` BEFORE INSERT ON `tab
2467+
one` FOR EACH ROW SET NEW.a = 1;
2468+
2469+
CREATE EVENT `event
2470+
one` ON SCHEDULE AT '2030-01-01 00:00:00' DO SET @a=5;
2471+
2472+
SHOW TABLES FROM bug25717383;
2473+
SHOW TRIGGERS FROM bug25717383;
2474+
--replace_column 6 #
2475+
SHOW EVENTS FROM bug25717383;
2476+
2477+
SELECT ROUTINE_NAME FROM INFORMATION_SCHEMA.ROUTINES
2478+
WHERE ROUTINE_SCHEMA='bug25717383' AND ROUTINE_TYPE= 'PROCEDURE'
2479+
ORDER BY ROUTINE_NAME;
2480+
2481+
--exec $MYSQL_DUMP --triggers --events --routines --add-drop-database --databases bug25717383 > $MYSQLTEST_VARDIR/tmp/bug25717383.sql
2482+
2483+
SHOW TABLES FROM bug25717383;
2484+
SHOW TRIGGERS FROM bug25717383;
2485+
--replace_column 6 #
2486+
SHOW EVENTS FROM bug25717383;
2487+
2488+
SELECT ROUTINE_NAME FROM INFORMATION_SCHEMA.ROUTINES
2489+
WHERE ROUTINE_SCHEMA='bug25717383' AND ROUTINE_TYPE= 'PROCEDURE'
2490+
ORDER BY ROUTINE_NAME;
2491+
2492+
DROP DATABASE bug25717383;

0 commit comments

Comments
 (0)