Skip to content

Commit f394026

Browse files
Aditya AHery Ramilison
authored andcommitted
Bug #32628376 BACKPORT FIX OF BUG 32335263 TO 5.6+
PROBLEM ------- If the query contains password in plain text, we have the query re-written immediately after parsing and password string is replaced. However, there is a unsafe window before rewrite is done and in such case we should not display the plain text password. Affected queries are SHOW PROCESSLIST; SELECT ... FROM information_schema.processlist; Solution: --------- Fix this is to avoid displaying the query string till re-write is over. #rb 26092 #rb 26070 Reviewed-by: Debarun Bannerjee <debarun.bannerjee@oracle.com> Reviewed-by: Reviewed by: Dyre Tjeldvold <dyre.tjeldvoll@oracle.com> (cherry picked from commit c7d0c5b6ee1cfc2a5328e9adf058857b43e0a091)
1 parent 0975ece commit f394026

File tree

5 files changed

+47
-11
lines changed

5 files changed

+47
-11
lines changed

sql/sql_class.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
2+
Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
33
44
This program is free software; you can redistribute it and/or modify
55
it under the terms of the GNU General Public License, version 2.0,
@@ -1010,6 +1010,7 @@ THD::THD(bool enable_plugins)
10101010
#ifdef SIGNAL_WITH_VIO_SHUTDOWN
10111011
active_vio = 0;
10121012
#endif
1013+
my_atomic_store32(&m_safe_to_display, 0);
10131014
mysql_mutex_init(key_LOCK_thd_data, &LOCK_thd_data, MY_MUTEX_INIT_FAST);
10141015

10151016
/* Variables with default values */

sql/sql_class.h

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights
1+
/* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights
22
reserved.
33
44
This program is free software; you can redistribute it and/or modify
@@ -2243,7 +2243,7 @@ class THD :public MDL_context_owner,
22432243

22442244
private:
22452245
unsigned int m_current_stage_key;
2246-
2246+
volatile int32 m_safe_to_display;
22472247
public:
22482248
void enter_stage(const PSI_stage_info *stage,
22492249
PSI_stage_info *old_stage,
@@ -4040,8 +4040,27 @@ class THD :public MDL_context_owner,
40404040
#ifdef HAVE_PSI_THREAD_INTERFACE
40414041
PSI_THREAD_CALL(set_thread_info)(query_arg, query_length_arg);
40424042
#endif
4043+
set_safe_display(true);
4044+
}
4045+
/**
4046+
Reset query string to be displayed in PFS. Also reset the safety flag
4047+
for information_schema.process_list for next query.
4048+
*/
4049+
void reset_query_for_display(void) {
4050+
set_query_for_display(NULL, 0);
4051+
my_atomic_store32(&m_safe_to_display, 0);
40434052
}
4044-
void reset_query_for_display(void) { set_query_for_display(NULL, 0); }
4053+
4054+
/** Set if the query string to be safe to display.
4055+
@param[in] safe if it is safe to display query string */
4056+
void set_safe_display(bool safe) {
4057+
int32 value = safe ? 1 : 0;
4058+
my_atomic_store32(&m_safe_to_display, value);
4059+
}
4060+
4061+
/** @return true, if safe to display the query string. */
4062+
int32 safe_to_display() { return my_atomic_load32(&m_safe_to_display);}
4063+
40454064
void set_query(char *query_arg, uint32 query_length_arg,
40464065
const CHARSET_INFO *cs_arg)
40474066
{

sql/sql_parse.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2000, 2020, Oracle and/or its affiliates.
1+
/* Copyright (c) 2000, 2021, Oracle and/or its affiliates.
22
33
This program is free software; you can redistribute it and/or modify
44
it under the terms of the GNU General Public License, version 2.0,
@@ -1851,7 +1851,7 @@ bool dispatch_command(enum enum_server_command command, THD *thd,
18511851
MYSQL_END_STATEMENT(thd->m_statement_psi, thd->get_stmt_da());
18521852
thd->m_statement_psi= NULL;
18531853
thd->m_digest= NULL;
1854-
1854+
thd->reset_query_for_display();
18551855
dec_thread_running();
18561856
thd->packet.shrink(thd->variables.net_buffer_length); // Reclaim some memory
18571857
free_root(thd->mem_root,MYF(MY_KEEP_PREALLOC));
@@ -6444,6 +6444,7 @@ void mysql_parse(THD *thd, char *rawbuf, uint length,
64446444
? (found_semicolon - thd->query())
64456445
: thd->query_length();
64466446

6447+
DEBUG_SYNC_C("sql_parse_before_rewrite");
64476448
if (!err)
64486449
{
64496450
/*
@@ -6490,6 +6491,8 @@ void mysql_parse(THD *thd, char *rawbuf, uint length,
64906491
}
64916492
}
64926493

6494+
DEBUG_SYNC_C("sql_parse_after_rewrite");
6495+
64936496
if (!err)
64946497
{
64956498
thd->m_statement_psi= MYSQL_REFINE_STATEMENT(thd->m_statement_psi,

sql/sql_prepare.cc

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved.
1+
/* Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved.
22
33
This program is free software; you can redistribute it and/or modify
44
it under the terms of the GNU General Public License, version 2.0,
@@ -2827,7 +2827,8 @@ void mysqld_stmt_fetch(THD *thd, char *packet, uint packet_length)
28272827

28282828
thd->stmt_arena= stmt;
28292829
thd->set_n_backup_statement(stmt, &stmt_backup);
2830-
2830+
int32 saved_safe_to_display = thd->safe_to_display();
2831+
thd->set_safe_display(true);
28312832
cursor->fetch(num_rows);
28322833

28332834
if (!cursor->is_open())
@@ -2837,6 +2838,7 @@ void mysqld_stmt_fetch(THD *thd, char *packet, uint packet_length)
28372838
}
28382839

28392840
thd->restore_backup_statement(stmt, &stmt_backup);
2841+
thd->set_safe_display(saved_safe_to_display);
28402842
thd->stmt_arena= thd;
28412843

28422844
DBUG_VOID_RETURN;
@@ -3407,11 +3409,14 @@ bool Prepared_statement::prepare(const char *packet, uint packet_len)
34073409
*/
34083410
thd->set_n_backup_statement(this, &stmt_backup);
34093411
thd->set_n_backup_active_arena(this, &stmt_backup);
3412+
int32 saved_safe_to_display = thd->safe_to_display();
3413+
thd->set_safe_display(true);
34103414

34113415
if (alloc_query(thd, packet, packet_len))
34123416
{
34133417
thd->restore_backup_statement(this, &stmt_backup);
34143418
thd->restore_active_arena(this, &stmt_backup);
3419+
thd->set_safe_display(saved_safe_to_display);
34153420
DBUG_RETURN(TRUE);
34163421
}
34173422

@@ -3433,6 +3438,7 @@ bool Prepared_statement::prepare(const char *packet, uint packet_len)
34333438
thd->restore_backup_statement(this, &stmt_backup);
34343439
thd->restore_active_arena(this, &stmt_backup);
34353440
thd->stmt_arena= old_stmt_arena;
3441+
thd->set_safe_display(saved_safe_to_display);
34363442
DBUG_RETURN(TRUE);
34373443
}
34383444

@@ -3522,6 +3528,7 @@ bool Prepared_statement::prepare(const char *packet, uint packet_len)
35223528

35233529
cleanup_stmt();
35243530
thd->restore_backup_statement(this, &stmt_backup);
3531+
thd->set_safe_display(saved_safe_to_display);
35253532
thd->stmt_arena= old_stmt_arena;
35263533

35273534
if (error == 0)
@@ -3751,6 +3758,8 @@ Prepared_statement::execute_server_runnable(Server_runnable *server_runnable)
37513758
thd->set_n_backup_statement(this, &stmt_backup);
37523759
thd->set_n_backup_active_arena(this, &stmt_backup);
37533760
thd->stmt_arena= this;
3761+
int32 saved_safe_to_display = thd->safe_to_display();
3762+
thd->set_safe_display(true);
37543763

37553764
error= server_runnable->execute_server_code(thd);
37563765

@@ -3759,6 +3768,7 @@ Prepared_statement::execute_server_runnable(Server_runnable *server_runnable)
37593768
thd->restore_active_arena(this, &stmt_backup);
37603769
thd->restore_backup_statement(this, &stmt_backup);
37613770
thd->stmt_arena= save_stmt_arena;
3771+
thd->set_safe_display(saved_safe_to_display);
37623772

37633773
save_change_list.move_elements_to(&thd->change_list);
37643774

@@ -3993,6 +4003,8 @@ bool Prepared_statement::execute(String *expanded_query, bool open_cursor)
39934003
*/
39944004

39954005
thd->set_n_backup_statement(this, &stmt_backup);
4006+
int32 saved_safe_to_display = thd->safe_to_display();
4007+
thd->set_safe_display(true);
39964008

39974009
/*
39984010
Change the current database (if needed).
@@ -4107,6 +4119,7 @@ bool Prepared_statement::execute(String *expanded_query, bool open_cursor)
41074119

41084120
thd->set_statement(&stmt_backup);
41094121
thd->stmt_arena= old_stmt_arena;
4122+
thd->set_safe_display(saved_safe_to_display);
41104123

41114124
if (state == Query_arena::STMT_PREPARED)
41124125
state= Query_arena::STMT_EXECUTED;

sql/sql_show.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
1+
/* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
22
33
This program is free software; you can redistribute it and/or modify
44
it under the terms of the GNU General Public License, version 2.0,
@@ -2179,7 +2179,7 @@ void mysqld_list_processes(THD *thd,const char *user, bool verbose)
21792179
size_t query_length;
21802180
if ((query_length = tmp->rewritten_query.length()) > 0) {
21812181
query_str = tmp->rewritten_query.c_ptr();
2182-
} else {
2182+
} else if(tmp->safe_to_display()) {
21832183
query_length = tmp->query_length();
21842184
query_str = tmp->query();
21852185
}
@@ -2330,7 +2330,7 @@ int fill_schema_processlist(THD* thd, TABLE_LIST* tables, Item* cond)
23302330

23312331
if ((query_length = tmp->rewritten_query.length()) > 0) {
23322332
query_str = tmp->rewritten_query.c_ptr();
2333-
} else {
2333+
} else if(tmp->safe_to_display()){
23342334
query_length = tmp->query_length();
23352335
query_str = tmp->query();
23362336
}

0 commit comments

Comments
 (0)