Skip to content

Commit 8074faa

Browse files
author
Tatjana Azundris Nuernberg
committed
Bug#16467055: GRANT STATEMENTS LOGGED TWICE IN SLOW QUERY LOG
Under very specific circumstances, an obfuscated log-line could be logged twice. What happens is this: - statement is one that is rewritten (password obfuscation etc.) - since we're executing that statement with mysql -e ..., we next send COM_QUIT - COM_QUIT is not a query. In current server, the string that holds the rewritten_query from the previous query (GRANT ...), if any, is not reset in this case since we don't go through the code path where that happens. We DO however go through the path where we check whether a rewrite happened, and if so, prefer it to any original string when logging. - In other words, on a non-query command following a query that was rewritten, we might log the stale rewritten string again. - This is amended by resetting the rewritten_query in a place that guarantees the reset will also happen for non-COM_QUERY queries.
1 parent 9979e3e commit 8074faa

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
lines changed

mysql-test/r/rewrite_general_log.result

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,21 @@ id select_type table partitions type possible_keys key key_len ref rows filtered
102102
Warnings:
103103
Warning 1681 'EXTENDED' is deprecated and will be removed in a future release.
104104
Note 1003 /* select#1 */ select ((@`a`) = 5) AS `@a=5`,(@b:=10) AS `@b:=10`,(@c:=20) AS `@c:=20`,(@d:=(40 + 5)) AS `@d:=40+5`,((@e:=80) + 5) AS `(@e:=80)+5`
105+
End of 5.6 tests!
106+
#
107+
# Bug#16467055: GRANT STATEMENTS LOGGED TWICE IN SLOW QUERY LOG
108+
#
109+
SET GLOBAL slow_query_log=1;
110+
SET GLOBAL log_output='TABLE';
111+
TRUNCATE mysql.slow_log;
112+
SELECT sql_text FROM mysql.slow_log;
113+
sql_text
114+
SET SESSION long_query_time=0
115+
GRANT ALL PRIVILEGES ON *.* TO 'test_user1'@'%' IDENTIFIED BY PASSWORD '*82DC221D557298F6CE9961037DB1C90604792F5C'
116+
Quit
117+
DROP USER test_user1;
118+
End of 5.7 tests!
105119
DROP TABLE test_log;
106120
SET GLOBAL general_log_file= @old_general_log_file;
107121
SET GLOBAL general_log= @old_general_log;
108122
SET GLOBAL log_output= @old_log_output;
109-
End of 5.6 tests!

mysql-test/t/rewrite_general_log.test

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
# (see sql/sql_rewrite.cc for bulk of implementation)
66
#
77

8+
# For Bug#16467055 and friends:
9+
--source include/not_embedded.inc
10+
811
# make sure we start with a clean slate. log_tables.test says this is OK.
912
TRUNCATE TABLE mysql.general_log;
1013

@@ -108,12 +111,29 @@ SELECT argument FROM mysql.general_log WHERE argument LIKE 'GRANT SELECT%' AND a
108111
# VIEWs do not accepted variables at this time.
109112
EXPLAIN EXTENDED SELECT @a=5,@b:=10,@c:=20,@d:=40+5,(@e:=80)+5;
110113

114+
115+
--echo End of 5.6 tests!
116+
117+
118+
--echo #
119+
--echo # Bug#16467055: GRANT STATEMENTS LOGGED TWICE IN SLOW QUERY LOG
120+
--echo #
121+
122+
SET GLOBAL slow_query_log=1;
123+
SET GLOBAL log_output='TABLE';
124+
TRUNCATE mysql.slow_log;
125+
--exec $MYSQL -e "SET SESSION long_query_time=0; GRANT ALL PRIVILEGES ON *.* TO test_user1 IDENTIFIED BY 'meow';"
126+
SELECT sql_text FROM mysql.slow_log;
127+
DROP USER test_user1;
128+
129+
130+
--echo End of 5.7 tests!
131+
132+
# cleanup
111133
DROP TABLE test_log;
112134

113135
--remove_file $MYSQLTEST_VARDIR/log/rewrite_general.log
114136

115137
SET GLOBAL general_log_file= @old_general_log_file;
116138
SET GLOBAL general_log= @old_general_log;
117139
SET GLOBAL log_output= @old_log_output;
118-
119-
--echo End of 5.6 tests!

sql/sp_head.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,14 @@ bool sp_head::execute(THD *thd, bool merge_da_on_success)
753753
this->m_sp_share);
754754
#endif
755755

756+
/*
757+
For now, we're mostly concerned with sp_instr_stmt, but that's
758+
likely to change in the future, so we'll do it right from the
759+
start.
760+
*/
761+
if (thd->rewritten_query.length())
762+
thd->rewritten_query.free();
763+
756764
err_status= i->execute(thd, &ip);
757765

758766
#ifdef HAVE_PSI_STATEMENT_INTERFACE

sql/sql_parse.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1074,6 +1074,7 @@ bool dispatch_command(enum enum_server_command command, THD *thd,
10741074
command= COM_SHUTDOWN;
10751075
}
10761076
thd->set_query_id(next_query_id());
1077+
thd->rewritten_query.free();
10771078
thd_manager->inc_thread_running();
10781079

10791080
if (!(server_command_flags[command] & CF_SKIP_QUESTIONS))
@@ -1863,7 +1864,6 @@ bool alloc_query(THD *thd, const char *packet, size_t packet_length)
18631864
query[packet_length]= '\0';
18641865

18651866
thd->set_query(query, packet_length);
1866-
thd->rewritten_query.free(); // free here lest PS break
18671867

18681868
/* Reclaim some memory */
18691869
thd->packet.shrink(thd->variables.net_buffer_length);

0 commit comments

Comments
 (0)