Skip to content

Commit da31abf

Browse files
author
Sergey Vojtovich
committed
Backport from 6.0-codebase.
Bug #36098 Audit plugin (wl 3771) feature disabled in 6.0 avoid recusrive locking of LOCK_plugin
1 parent 94098b2 commit da31abf

File tree

7 files changed

+96
-64
lines changed

7 files changed

+96
-64
lines changed

include/mysql/plugin_audit.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424

2525
#define MYSQL_AUDIT_CLASS_MASK_SIZE 1
2626

27-
#define MYSQL_AUDIT_INTERFACE_VERSION ( 0x010000 | MYSQL_AUDIT_CLASS_MASK_SIZE )
28-
27+
#define MYSQL_AUDIT_INTERFACE_VERSION 0x0100
2928

3029
/*
3130
The first word in every event class struct indicates the specific

sql/log.cc

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343

4444
/* max size of the log message */
4545
#define MAX_LOG_BUFFER_SIZE 1024
46-
#define MAX_USER_HOST_SIZE 512
4746
#define MAX_TIME_SIZE 32
4847
#define MY_OFF_T_UNDEF (~(my_off_t)0UL)
4948

@@ -1069,7 +1068,6 @@ bool LOGGER::general_log_write(THD *thd, enum enum_server_command command,
10691068
bool error= FALSE;
10701069
Log_event_handler **current_handler= general_log_handler_list;
10711070
char user_host_buff[MAX_USER_HOST_SIZE + 1];
1072-
Security_context *sctx= thd->security_ctx;
10731071
uint user_host_len= 0;
10741072
time_t current_time;
10751073

@@ -1081,21 +1079,15 @@ bool LOGGER::general_log_write(THD *thd, enum enum_server_command command,
10811079
unlock();
10821080
return 0;
10831081
}
1084-
user_host_len= strxnmov(user_host_buff, MAX_USER_HOST_SIZE,
1085-
sctx->priv_user ? sctx->priv_user : "", "[",
1086-
sctx->user ? sctx->user : "", "] @ ",
1087-
sctx->host ? sctx->host : "", " [",
1088-
sctx->ip ? sctx->ip : "", "]", NullS) -
1089-
user_host_buff;
1082+
user_host_len= make_user_name(thd, user_host_buff);
10901083

10911084
current_time= my_time(0);
10921085

1093-
mysql_audit_general(thd, MYSQL_AUDIT_GENERAL_LOG, 0, current_time,
1094-
user_host_buff, user_host_len,
1095-
command_name[(uint) command].str,
1096-
command_name[(uint) command].length,
1097-
query, query_length,
1098-
thd->variables.character_set_client,0);
1086+
mysql_audit_general_log(thd, current_time,
1087+
user_host_buff, user_host_len,
1088+
command_name[(uint) command].str,
1089+
command_name[(uint) command].length,
1090+
query, query_length);
10991091

11001092
while (*current_handler)
11011093
error|= (*current_handler++)->

sql/mysqld.cc

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2923,15 +2923,10 @@ void my_message_sql(uint error, const char *str, myf MyFlags)
29232923
error= ER_UNKNOWN_ERROR;
29242924
}
29252925

2926+
mysql_audit_general(thd, MYSQL_AUDIT_GENERAL_ERROR, error, str);
2927+
29262928
if (thd)
29272929
{
2928-
mysql_audit_general(thd,MYSQL_AUDIT_GENERAL_ERROR,error,my_time(0),
2929-
0,0,str,str ? strlen(str) : 0,
2930-
thd->query(), thd->query_length(),
2931-
thd->variables.character_set_client,
2932-
thd->warning_info->current_row_for_warning());
2933-
2934-
29352930
if (MyFlags & ME_FATALERROR)
29362931
thd->is_fatal_error= 1;
29372932
(void) thd->raise_condition(error,

sql/set_var.cc

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3519,15 +3519,14 @@ void set_var_free()
35193519
@param str Name of system variable to find
35203520
@param length Length of variable. zero means that we should use strlen()
35213521
on the variable
3522-
@param no_error Refuse to emit an error, even if one occurred.
35233522
35243523
@retval
35253524
pointer pointer to variable definitions
35263525
@retval
35273526
0 Unknown variable (error message is given)
35283527
*/
35293528

3530-
sys_var *intern_find_sys_var(const char *str, uint length, bool no_error)
3529+
sys_var *intern_find_sys_var(const char *str, uint length)
35313530
{
35323531
sys_var *var;
35333532

@@ -3537,9 +3536,6 @@ sys_var *intern_find_sys_var(const char *str, uint length, bool no_error)
35373536
*/
35383537
var= (sys_var*) my_hash_search(&system_variable_hash,
35393538
(uchar*) str, length ? length : strlen(str));
3540-
if (!(var || no_error))
3541-
my_error(ER_UNKNOWN_SYSTEM_VARIABLE, MYF(0), (char*) str);
3542-
35433539
return var;
35443540
}
35453541

sql/sql_audit.h

Lines changed: 70 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,43 +34,96 @@ extern void mysql_audit_notify(THD *thd, uint event_class,
3434
uint event_subtype, ...);
3535
extern void mysql_audit_release(THD *thd);
3636

37-
37+
#define MAX_USER_HOST_SIZE 512
38+
static inline uint make_user_name(THD *thd, char *buf)
39+
{
40+
Security_context *sctx= thd->security_ctx;
41+
return strxnmov(buf, MAX_USER_HOST_SIZE,
42+
sctx->priv_user ? sctx->priv_user : "", "[",
43+
sctx->user ? sctx->user : "", "] @ ",
44+
sctx->host ? sctx->host : "", " [",
45+
sctx->ip ? sctx->ip : "", "]", NullS) - buf;
46+
}
3847

3948
/**
40-
Call audit plugins of GENERAL audit class.
41-
event_subtype should be set to one of:
42-
MYSQL_AUDIT_GENERAL_LOG
43-
MYSQL_AUDIT_GENERAL_ERROR
44-
MYSQL_AUDIT_GENERAL_RESULT
49+
Call audit plugins of GENERAL audit class, MYSQL_AUDIT_GENERAL_LOG subtype.
4550
4651
@param[in] thd
47-
@param[in] event_subtype Type of general audit event.
48-
@param[in] error_code Error code
4952
@param[in] time time that event occurred
5053
@param[in] user User name
5154
@param[in] userlen User name length
5255
@param[in] cmd Command name
5356
@param[in] cmdlen Command name length
5457
@param[in] query Query string
5558
@param[in] querylen Query string length
56-
@param[in] clientcs Charset of query string
57-
@param[in] rows Number of affected rows
5859
*/
5960

61+
static inline
62+
void mysql_audit_general_log(THD *thd, time_t time,
63+
const char *user, uint userlen,
64+
const char *cmd, uint cmdlen,
65+
const char *query, uint querylen)
66+
{
67+
#ifndef EMBEDDED_LIBRARY
68+
if (mysql_global_audit_mask[0] & MYSQL_AUDIT_GENERAL_CLASSMASK)
69+
{
70+
CHARSET_INFO *clientcs= thd ? thd->variables.character_set_client
71+
: global_system_variables.character_set_client;
72+
73+
mysql_audit_notify(thd, MYSQL_AUDIT_GENERAL_CLASS, MYSQL_AUDIT_GENERAL_LOG,
74+
0, time, user, userlen, cmd, cmdlen,
75+
query, querylen, clientcs, 0);
76+
}
77+
#endif
78+
}
79+
80+
/**
81+
Call audit plugins of GENERAL audit class.
82+
event_subtype should be set to one of:
83+
MYSQL_AUDIT_GENERAL_ERROR
84+
MYSQL_AUDIT_GENERAL_RESULT
85+
86+
@param[in] thd
87+
@param[in] event_subtype Type of general audit event.
88+
@param[in] error_code Error code
89+
@param[in] msg Message
90+
*/
6091
static inline
6192
void mysql_audit_general(THD *thd, uint event_subtype,
62-
int error_code, time_t time,
63-
const char *user, uint userlen,
64-
const char *cmd, uint cmdlen,
65-
const char *query, uint querylen,
66-
CHARSET_INFO *clientcs,
67-
ha_rows rows)
93+
int error_code, const char *msg)
6894
{
6995
#ifndef EMBEDDED_LIBRARY
7096
if (mysql_global_audit_mask[0] & MYSQL_AUDIT_GENERAL_CLASSMASK)
97+
{
98+
time_t time= my_time(0);
99+
uint msglen= msg ? strlen(msg) : 0;
100+
const char *query, *user;
101+
uint querylen, userlen;
102+
char user_buff[MAX_USER_HOST_SIZE];
103+
CHARSET_INFO *clientcs;
104+
ha_rows rows;
105+
106+
if (thd)
107+
{
108+
query= thd->query();
109+
querylen= thd->query_length();
110+
user= user_buff;
111+
userlen= make_user_name(thd, user_buff);
112+
clientcs= thd->variables.character_set_client;
113+
rows= thd->warning_info->current_row_for_warning();
114+
}
115+
else
116+
{
117+
query= user= 0;
118+
querylen= userlen= 0;
119+
clientcs= global_system_variables.character_set_client;
120+
rows= 0;
121+
}
122+
71123
mysql_audit_notify(thd, MYSQL_AUDIT_GENERAL_CLASS, event_subtype,
72-
error_code, time, user, userlen, cmd, cmdlen,
124+
error_code, time, user, userlen, msg, msglen,
73125
query, querylen, clientcs, rows);
126+
}
74127
#endif
75128
}
76129

sql/sql_parse.cc

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,13 +1485,7 @@ bool dispatch_command(enum enum_server_command command, THD *thd,
14851485
close_thread_tables(thd);
14861486

14871487
if (!thd->is_error() && !thd->killed_errno())
1488-
{
1489-
mysql_audit_general(thd,MYSQL_AUDIT_GENERAL_RESULT,0,my_time(0),
1490-
0,0,0,0,
1491-
thd->query(), thd->query_length(),
1492-
thd->variables.character_set_client,
1493-
thd->warning_info->current_row_for_warning());
1494-
}
1488+
mysql_audit_general(thd, MYSQL_AUDIT_GENERAL_RESULT, 0, 0);
14951489

14961490
log_slow_statement(thd);
14971491

sql/sql_plugin.cc

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ static void reap_plugins(void);
229229

230230

231231
/* declared in set_var.cc */
232-
extern sys_var *intern_find_sys_var(const char *str, uint length, bool no_error);
232+
extern sys_var *intern_find_sys_var(const char *str, uint length);
233233
extern bool throw_bounds_warning(THD *thd, bool fixed, bool unsignd,
234234
const char *name, longlong val);
235235

@@ -1014,6 +1014,9 @@ static int plugin_initialize(struct st_plugin_int *plugin)
10141014
DBUG_ENTER("plugin_initialize");
10151015

10161016
safe_mutex_assert_owner(&LOCK_plugin);
1017+
DBUG_ASSERT(plugin->state == PLUGIN_IS_UNINITIALIZED);
1018+
1019+
pthread_mutex_unlock(&LOCK_plugin);
10171020
if (plugin_type_initialize[plugin->plugin->type])
10181021
{
10191022
if ((*plugin_type_initialize[plugin->plugin->type])(plugin))
@@ -1033,6 +1036,8 @@ static int plugin_initialize(struct st_plugin_int *plugin)
10331036
}
10341037
}
10351038

1039+
pthread_mutex_lock(&LOCK_plugin);
1040+
10361041
plugin->state= PLUGIN_IS_READY;
10371042

10381043
if (plugin->plugin->status_vars)
@@ -1050,9 +1055,10 @@ static int plugin_initialize(struct st_plugin_int *plugin)
10501055
{0, 0, SHOW_UNDEF}
10511056
};
10521057
if (add_status_vars(array)) // add_status_vars makes a copy
1053-
goto err;
1058+
goto err1;
10541059
#else
1055-
add_status_vars(plugin->plugin->status_vars); // add_status_vars makes a copy
1060+
if (add_status_vars(plugin->plugin->status_vars))
1061+
goto err1;
10561062
#endif /* FIX_LATER */
10571063
}
10581064

@@ -1074,6 +1080,8 @@ static int plugin_initialize(struct st_plugin_int *plugin)
10741080

10751081
DBUG_RETURN(0);
10761082
err:
1083+
pthread_mutex_lock(&LOCK_plugin);
1084+
err1:
10771085
DBUG_RETURN(1);
10781086
}
10791087

@@ -1686,7 +1694,6 @@ bool mysql_install_plugin(THD *thd, const LEX_STRING *name, const LEX_STRING *dl
16861694
}
16871695
else
16881696
{
1689-
DBUG_ASSERT(tmp->state == PLUGIN_IS_UNINITIALIZED);
16901697
if (plugin_initialize(tmp))
16911698
{
16921699
my_error(ER_CANT_INITIALIZE_UDF, MYF(0), name->str,
@@ -2164,7 +2171,7 @@ sys_var *find_sys_var(THD *thd, const char *str, uint length)
21642171

21652172
pthread_mutex_lock(&LOCK_plugin);
21662173
rw_rdlock(&LOCK_system_variables_hash);
2167-
if ((var= intern_find_sys_var(str, length, false)) &&
2174+
if ((var= intern_find_sys_var(str, length)) &&
21682175
(pi= var->cast_pluginvar()))
21692176
{
21702177
rw_unlock(&LOCK_system_variables_hash);
@@ -2183,11 +2190,7 @@ sys_var *find_sys_var(THD *thd, const char *str, uint length)
21832190
rw_unlock(&LOCK_system_variables_hash);
21842191
pthread_mutex_unlock(&LOCK_plugin);
21852192

2186-
/*
2187-
If the variable exists but the plugin it is associated with is not ready
2188-
then the intern_plugin_lock did not raise an error, so we do it here.
2189-
*/
2190-
if (pi && !var)
2193+
if (!var)
21912194
my_error(ER_UNKNOWN_SYSTEM_VARIABLE, MYF(0), (char*) str);
21922195
DBUG_RETURN(var);
21932196
}
@@ -2390,7 +2393,7 @@ static uchar *intern_sys_var_ptr(THD* thd, int offset, bool global_lock)
23902393
st_bookmark *v= (st_bookmark*) my_hash_element(&bookmark_hash,idx);
23912394

23922395
if (v->version <= thd->variables.dynamic_variables_version ||
2393-
!(var= intern_find_sys_var(v->key + 1, v->name_len, true)) ||
2396+
!(var= intern_find_sys_var(v->key + 1, v->name_len)) ||
23942397
!(pi= var->cast_pluginvar()) ||
23952398
v->key[0] != (pi->plugin_var->flags & PLUGIN_VAR_TYPEMASK))
23962399
continue;
@@ -2483,7 +2486,7 @@ static void cleanup_variables(THD *thd, struct system_variables *vars)
24832486
{
24842487
v= (st_bookmark*) my_hash_element(&bookmark_hash, idx);
24852488
if (v->version > vars->dynamic_variables_version ||
2486-
!(var= intern_find_sys_var(v->key + 1, v->name_len, true)) ||
2489+
!(var= intern_find_sys_var(v->key + 1, v->name_len)) ||
24872490
!(pivar= var->cast_pluginvar()) ||
24882491
v->key[0] != (pivar->plugin_var->flags & PLUGIN_VAR_TYPEMASK))
24892492
continue;

0 commit comments

Comments
 (0)