trellix-enterprise / mysql-audit

AUDIT Plugin for MySQL. See wiki and readme for description. If you find the plugin useful, please star us on GitHub. We love stars and it's a great way to show your feedback.
Other
239 stars 57 forks source link

An issue on trampoline function. #176

Closed RingsC closed 7 years ago

RingsC commented 7 years ago

Hi, I have a customer changed in audit_mysql_execute_command function, therefore, i will make some changes in this function, such as following code: thd->get_stmt_da()->set_ok_status(count, id, msg) ; By adding some code to deal with the objections which are not belongs to this trampoline function. for example, the Diagonstics_area as code illustrated above. But there is a crash on this line. When i do debugging , I found that, thd has a correct value, but the it can not get the Dianostic_area properly.

Diagonstics_area* da = thd->get_stmt_da() ; (Here, thd value is right, but da is not have correct value. so it will crash at next line code)
 da->set_status_ok(count, id,msg) ;

Does anyone have any clues ?

the call stacks:

617 Diagnostics_area da (NULL) ; (gdb) n 618 da = thd->get_stmt_da () ; (gdb) n 620 da->set_ok_status (1,0,NULL) ; //this line code will lead to crash. (gdb) p thd //HAVING A CORRECT thd VALUE $19 = (THD ) 0x7fffd4001a00 (gdb) p *thd $20 = { = {_vptr.MDL_context_owner = 0x2b86ad0}, = {_vptr.Query_arena = 0x2b86b38, free_list = 0x0, mem_root = 0x7fffd4004710, is_backup_arena = false, is_reprepared = false, state = Query_arena::STMT_CONVENTIONAL_EXECUTION}, = {m_reprepare_observers = {m_size = 0, m_capacity = 4, m_buff = {{data = '\000' <repeats 31 times>, align = {}}}, m_array_ptr = 0x7fffd4001a38, m_psi_key = 0}, open_tables = 0x0, temporary_tables = 0x0, derived_tables = 0x0, lock = 0x0, extra_lock = 0x0, locked_tables_mode = LTM_NONE, state_flags = 0}, mdl_context = {m_wait = { m_LOCK_wait_status = {m_mutex = {global = {data = {lock = 0, count = 0, owner = 0, nusers = 0, kind = 2, spins = 0, list = {prev = 0x0, next = 0x0}}, size = '\000' <repeats 16 times>, "\002", '\000' <repeats 22 times>, align = 0}, mutex = {data = {lock = 0, count = 0, owner = 0, nusers = 0, kind = 0, spins = 0, list = {prev = 0x0, next = 0x0}}, size = '\000' <repeats 39 times>, align = 0}, file = 0x1f995e0 "/home/homli/work/MySQL-Audit/sql/mdl.cc", line = 1773, count = 0, thread = 0}, m_psi = 0x7ffff06db640}, m_COND_wait_status = {m_cond = { data = {lock = 0, futex = 0, total_seq = 0, wakeup_seq = 0, __woken_seq = 0, mutex = 0x0, nwaiters = 0, broadcast_seq = 0},

(gdb) p thd->m_stmt_da //m_stmt_da is also correct. $21 = (Diagnostics_area *) 0x7fffd4004768 (gdb)

(gdb) p thd->get_stmt_da() //in gdb, thd->get_stmt_da() also show a correct value as i think $23 = (Diagnostics_area *) 0x7fffd4004768 (gdb)

/da= thd->get_stmt_da()/ (gdb) p da //but after a "=" assign operator, the da can not get correct value. $24 = (Diagnostics_area *) 0x0

aharonrobbins commented 7 years ago

What version of MySQL are you compiling against? As long as you compile against the source code for the same version as the one into which you are loading the plugin, it should work OK.

RingsC commented 7 years ago

All mysql and plugin are compiled from source. I think all them used same version.

the g++ version: [homli@homli include]$ g++ --version g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-18) Copyright © 2010 Free Software Foundation, Inc.

and gcc version: gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-18) Copyright © 2010 Free Software Foundation, Inc.

mysql compiling conf: cmake -DCMAKE_INSTALL_PREFIX=../bin -DMYSQL_DATADIR=../bin/data -DSYSCONFDIR=../bin/etc -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_BOOST=../../boost_1_59_0/ -DDOWNLOAD_BOOST=0 -DCMAKE_BUILD_TYPE=Debug ..

and plugin compling conf: ./configure --with-mysql=/the_mysql_source_directory --with-mysql-libservices=/the_mysql_source_directory_install_dir/libservices/libmysqlservices.a --with-mysql-installdir=/the_mysql_source_directory_install_dir

CXX='gcc -static-libgcc' CC='gcc -static-libgcc' ./configure --with-mysql=/home/homli/work/mysql-server/ --with-mysql-libservices=/home/homli/work/mysql-server/build_cmk/libservices/libmysqlservices.a --with-mysql-plugindir=/home/homli/work/mysql-server/build_cmk/bin/lib/plugin --with-mysql-installdir=/home/homli/work/mysql-server/build_cmk/

make && make install

the_mysql_source_directory: is my mysql source dir

aharonrobbins commented 7 years ago

Please make sure that you are not loading a plugin compiled for one version of MySQL into another. Even between two minor releases (e.g., 5.6.25 vs. 5.6.26) it can make a difference. The members of class THD often move around between releases, and if the compiled code is expecting the Diagnostics_area pointer to be in one place and it's in another, then things can go wrong. (get_stmt_da is an inline function, so the location of the Diagnostics_area pointer is resolved at compile time against the headers of the version you are compiling against.)

What version of MySQL are you compiling against?

RingsC commented 7 years ago

mysql> select version(); +--------------+ | version() | +--------------+ | 5.7.18-debug |

aharonrobbins commented 7 years ago

So, it's not clear to me why this isn't working. I will try to find some time to experiment locally.

aharonrobbins commented 7 years ago

Hi. I have not had a chance to experiment. However, I have just uploaded new code that may help you. We now include THD offsets for the diagnostic area, in order to get the error code value. Take a look at the thd_error_code() function in include/audit_handler.h and see if it's of any use to you. Thanks!