Skip to content

Commit 6fa5e08

Browse files
Bharathy Satishgipulla
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 87e37ee commit 6fa5e08

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
@@ -549,6 +549,7 @@ static int dump_tablespaces_for_databases(char** databases);
549549
static int dump_tablespaces(char* ts_where);
550550
static void print_comment(FILE *sql_file, my_bool is_error, const char *format,
551551
...);
552+
static const char* fix_identifier_with_newline(char*);
552553

553554

554555
/*
@@ -649,7 +650,7 @@ static void write_header(FILE *sql_file, char *db_name)
649650
MACHINE_TYPE);
650651
print_comment(sql_file, 0, "-- Host: %s Database: %s\n",
651652
current_host ? current_host : "localhost",
652-
db_name ? db_name : "");
653+
db_name ? fix_identifier_with_newline(db_name) : "");
653654
print_comment(sql_file, 0,
654655
"-- ------------------------------------------------------\n"
655656
);
@@ -1981,6 +1982,30 @@ static void print_comment(FILE *sql_file, my_bool is_error, const char *format,
19811982
print_xml_comment(sql_file, strlen(comment_buff), comment_buff);
19821983
}
19831984

1985+
/*
1986+
This function accepts object names and prefixes -- wherever \n
1987+
character is found.
1988+
1989+
@param[in] object_name
1990+
1991+
@return
1992+
@retval fixed object name.
1993+
*/
1994+
1995+
static const char* fix_identifier_with_newline(char* object_name)
1996+
{
1997+
static char buff[COMMENT_LENGTH]= {0};
1998+
char *ptr= buff;
1999+
memset(buff, 0, 255);
2000+
while(*object_name)
2001+
{
2002+
*ptr++ = *object_name;
2003+
if (*object_name == '\n')
2004+
ptr= strmov(ptr, "-- ");
2005+
object_name++;
2006+
}
2007+
return buff;
2008+
}
19842009

19852010
/*
19862011
create_delimiter
@@ -2049,7 +2074,8 @@ static uint dump_events_for_db(char *db)
20492074

20502075
/* nice comments */
20512076
print_comment(sql_file, 0,
2052-
"\n--\n-- Dumping events for database '%s'\n--\n", db);
2077+
"\n--\n-- Dumping events for database '%s'\n--\n",
2078+
fix_identifier_with_newline(db));
20532079

20542080
/*
20552081
not using "mysql_query_with_error_report" because we may have not
@@ -2266,7 +2292,8 @@ static uint dump_routines_for_db(char *db)
22662292

22672293
/* nice comments */
22682294
print_comment(sql_file, 0,
2269-
"\n--\n-- Dumping routines for database '%s'\n--\n", db);
2295+
"\n--\n-- Dumping routines for database '%s'\n--\n",
2296+
fix_identifier_with_newline(db));
22702297

22712298
/*
22722299
not using "mysql_query_with_error_report" because we may have not
@@ -2325,7 +2352,7 @@ static uint dump_routines_for_db(char *db)
23252352
query_buff);
23262353
print_comment(sql_file, 1,
23272354
"-- does %s have permissions on mysql.proc?\n\n",
2328-
current_user);
2355+
fix_identifier_with_newline(current_user));
23292356
maybe_die(EX_MYSQLERR,"%s has insufficent privileges to %s!", current_user, query_buff);
23302357
}
23312358
else if (strlen(row[2]))
@@ -2539,11 +2566,11 @@ static uint get_table_structure(char *table, char *db, char *table_type,
25392566
if (strcmp (table_type, "VIEW") == 0) /* view */
25402567
print_comment(sql_file, 0,
25412568
"\n--\n-- Temporary table structure for view %s\n--\n\n",
2542-
result_table);
2569+
fix_identifier_with_newline(result_table));
25432570
else
25442571
print_comment(sql_file, 0,
25452572
"\n--\n-- Table structure for table %s\n--\n\n",
2546-
result_table);
2573+
fix_identifier_with_newline(result_table));
25472574

25482575
if (opt_drop)
25492576
{
@@ -2785,7 +2812,7 @@ static uint get_table_structure(char *table, char *db, char *table_type,
27852812

27862813
print_comment(sql_file, 0,
27872814
"\n--\n-- Table structure for table %s\n--\n\n",
2788-
result_table);
2815+
fix_identifier_with_newline(result_table));
27892816
if (opt_drop)
27902817
fprintf(sql_file, "DROP TABLE IF EXISTS %s;\n", result_table);
27912818
if (!opt_xml)
@@ -3490,21 +3517,23 @@ static void dump_table(char *table, char *db)
34903517
{
34913518
print_comment(md_result_file, 0,
34923519
"\n--\n-- Dumping data for table %s\n--\n",
3493-
result_table);
3520+
fix_identifier_with_newline(result_table));
34943521

34953522
dynstr_append_checked(&query_string, "SELECT /*!40001 SQL_NO_CACHE */ * FROM ");
34963523
dynstr_append_checked(&query_string, result_table);
34973524

34983525
if (where)
34993526
{
3500-
print_comment(md_result_file, 0, "-- WHERE: %s\n", where);
3527+
print_comment(md_result_file, 0, "-- WHERE: %s\n",
3528+
fix_identifier_with_newline(where));
35013529

35023530
dynstr_append_checked(&query_string, " WHERE ");
35033531
dynstr_append_checked(&query_string, where);
35043532
}
35053533
if (order_by)
35063534
{
3507-
print_comment(md_result_file, 0, "-- ORDER BY: %s\n", order_by);
3535+
print_comment(md_result_file, 0, "-- ORDER BY: %s\n",
3536+
fix_identifier_with_newline(order_by));
35083537

35093538
dynstr_append_checked(&query_string, " ORDER BY ");
35103539
dynstr_append_checked(&query_string, order_by);
@@ -4275,7 +4304,8 @@ static int init_dumping(char *database, int init_func(char*))
42754304
char *qdatabase= quote_name(database,quoted_database_buf,opt_quoted);
42764305

42774306
print_comment(md_result_file, 0,
4278-
"\n--\n-- Current Database: %s\n--\n", qdatabase);
4307+
"\n--\n-- Current Database: %s\n--\n",
4308+
fix_identifier_with_newline(qdatabase));
42794309

42804310
/* Call the view or table specific function */
42814311
init_func(qdatabase);
@@ -5281,7 +5311,7 @@ static my_bool get_view_structure(char *table, char* db)
52815311

52825312
print_comment(sql_file, 0,
52835313
"\n--\n-- Final view structure for view %s\n--\n\n",
5284-
result_table);
5314+
fix_identifier_with_newline(result_table));
52855315

52865316
/* Table might not exist if this view was dumped with --tab. */
52875317
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
@@ -5283,3 +5283,66 @@ a
52835283
DROP TABLE t1;
52845284
DROP TABLE t2;
52855285
DROP DATABASE db_20772273;
5286+
#
5287+
# Bug #25717383: MYSQLDUMP MAY EXECUTE ANY ARBITRARY QUERY
5288+
#
5289+
CREATE DATABASE bug25717383;
5290+
use bug25717383;
5291+
CREATE TABLE `tab
5292+
one` (a int);
5293+
CREATE VIEW `view
5294+
one` as SELECT * FROM `tab
5295+
one`;
5296+
CREATE PROCEDURE `proc
5297+
one`() SELECT * from `tab
5298+
one`;
5299+
CREATE TEMPORARY TABLE `temp
5300+
one` (id INT);
5301+
CREATE TRIGGER `trig
5302+
one` BEFORE INSERT ON `tab
5303+
one` FOR EACH ROW SET NEW.a = 1;
5304+
CREATE EVENT `event
5305+
one` ON SCHEDULE AT '2030-01-01 00:00:00' DO SET @a=5;
5306+
SHOW TABLES FROM bug25717383;
5307+
Tables_in_bug25717383
5308+
tab
5309+
one
5310+
view
5311+
one
5312+
SHOW TRIGGERS FROM bug25717383;
5313+
Trigger Event Table Statement Timing Created sql_mode Definer character_set_client collation_connection Database Collation
5314+
trig
5315+
one INSERT tab
5316+
one SET NEW.a = 1 BEFORE NULL root@localhost utf8 utf8_general_ci latin1_swedish_ci
5317+
SHOW EVENTS FROM bug25717383;
5318+
Db Name Definer Time zone Type Execute at Interval value Interval field Starts Ends Status Originator character_set_client collation_connection Database Collation
5319+
bug25717383 event
5320+
one root@localhost SYSTEM ONE TIME # NULL NULL NULL NULL ENABLED 1 utf8 utf8_general_ci latin1_swedish_ci
5321+
SELECT ROUTINE_NAME FROM INFORMATION_SCHEMA.ROUTINES
5322+
WHERE ROUTINE_SCHEMA='bug25717383' AND ROUTINE_TYPE= 'PROCEDURE'
5323+
ORDER BY ROUTINE_NAME;
5324+
ROUTINE_NAME
5325+
proc
5326+
one
5327+
SHOW TABLES FROM bug25717383;
5328+
Tables_in_bug25717383
5329+
tab
5330+
one
5331+
view
5332+
one
5333+
SHOW TRIGGERS FROM bug25717383;
5334+
Trigger Event Table Statement Timing Created sql_mode Definer character_set_client collation_connection Database Collation
5335+
trig
5336+
one INSERT tab
5337+
one SET NEW.a = 1 BEFORE NULL root@localhost utf8 utf8_general_ci latin1_swedish_ci
5338+
SHOW EVENTS FROM bug25717383;
5339+
Db Name Definer Time zone Type Execute at Interval value Interval field Starts Ends Status Originator character_set_client collation_connection Database Collation
5340+
bug25717383 event
5341+
one root@localhost SYSTEM ONE TIME # NULL NULL NULL NULL ENABLED 1 utf8 utf8_general_ci latin1_swedish_ci
5342+
SELECT ROUTINE_NAME FROM INFORMATION_SCHEMA.ROUTINES
5343+
WHERE ROUTINE_SCHEMA='bug25717383' AND ROUTINE_TYPE= 'PROCEDURE'
5344+
ORDER BY ROUTINE_NAME;
5345+
ROUTINE_NAME
5346+
proc
5347+
one
5348+
DROP DATABASE bug25717383;

mysql-test/t/mysqldump.test

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

0 commit comments

Comments
 (0)