Skip to content

Commit e2e7d52

Browse files
PS-9369: Fix currently processed query comparison in audit_log
https://perconadev.atlassian.net/browse/PS-9369 The audit_log uses stack to keep track of table access operations being performed in scope of one query. It compares last known table access query string stored on top of this stack with actual query in audit event being processed at the moment to decide if new record should be pushed to stack or it is time to clean records from the stack. Currently audit_log simply compares char* variables to decide if this is the same query string. This approach doesn't work. As a result plugin looses control of the stack size and it starts growing with the time consuming memory. This issue is not noticable on short term server connections as memory is freed once connection is closed. At the same time this leads to extra memory consumption for long running server connections. The following is done to fix the issue: - Query is sent along with audit event as MYSQL_LEX_CSTRING structure. It is not correct to ignore MYSQL_LEX_CSTRING.length comparison as sometimes MYSQL_LEX_CSTRING.str pointer may be not iniialised properly. Added string length check to make sure structure contains any valid string. - Used strncmp to compare actual strings instead of comparing char* variables.
1 parent a389a41 commit e2e7d52

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

plugin/audit_log/audit_log.cc

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ struct query_stack_frame {
746746
/* number of accessed databases */
747747
int databases_accessed;
748748
/* query */
749-
const char *query;
749+
MYSQL_LEX_CSTRING query;
750750
};
751751

752752
struct query_stack {
@@ -977,8 +977,12 @@ static bool audit_log_update_thd_local(MYSQL_THD thd,
977977
if (event_general->event_subclass == MYSQL_AUDIT_GENERAL_STATUS) {
978978
local->skip_query = false;
979979

980-
if (local->stack.frames[local->stack.top].query ==
981-
event_general->general_query.str) {
980+
if (event_general->general_query.length != 0 &&
981+
local->stack.frames[local->stack.top].query.length ==
982+
event_general->general_query.length &&
983+
strncmp(local->stack.frames[local->stack.top].query.str,
984+
event_general->general_query.str,
985+
event_general->general_query.length) == 0) {
982986
local->skip_query |=
983987
audit_log_include_databases &&
984988
local->stack.frames[local->stack.top].databases_accessed > 0 &&
@@ -993,7 +997,8 @@ static bool audit_log_update_thd_local(MYSQL_THD thd,
993997
local->stack.frames[local->stack.top].databases_included = 0;
994998
local->stack.frames[local->stack.top].databases_accessed = 0;
995999
local->stack.frames[local->stack.top].databases_excluded = 0;
996-
local->stack.frames[local->stack.top].query = nullptr;
1000+
local->stack.frames[local->stack.top].query.str = nullptr;
1001+
local->stack.frames[local->stack.top].query.length = 0;
9971002

9981003
if (local->stack.top > 0) --local->stack.top;
9991004
}
@@ -1060,12 +1065,15 @@ static bool audit_log_update_thd_local(MYSQL_THD thd,
10601065
const mysql_event_table_access *event_table =
10611066
(const mysql_event_table_access *)event;
10621067

1063-
if (local->stack.frames[local->stack.top].query != event_table->query.str &&
1064-
local->stack.frames[local->stack.top].query != nullptr) {
1068+
if (event_table->query.length != 0 &&
1069+
(local->stack.frames[local->stack.top].query.length !=
1070+
event_table->query.length ||
1071+
strncmp(local->stack.frames[local->stack.top].query.str,
1072+
event_table->query.str, event_table->query.length) != 0)) {
10651073
if (++local->stack.top >= local->stack.size)
10661074
realloc_stack_frames(thd, local->stack.size * 2);
10671075
}
1068-
local->stack.frames[local->stack.top].query = event_table->query.str;
1076+
local->stack.frames[local->stack.top].query = event_table->query;
10691077

10701078
++local->stack.frames[local->stack.top].databases_accessed;
10711079

0 commit comments

Comments
 (0)