[MDEV-18195] ASAN use-after-poison in my_strcasecmp_utf8 / Item::eq upon prepared statement with ORDER BY NAME_CONST Created: 2019-01-09  Updated: 2019-01-24  Resolved: 2019-01-24

Status: Closed
Project: MariaDB Server
Component/s: Prepared Statements
Affects Version/s: 10.1, 10.2, 10.3, 10.4
Fix Version/s: 10.4.2, 10.1.38, 10.2.22, 10.3.13

Type: Bug Priority: Major
Reporter: Elena Stepanova Assignee: Alexander Barkov
Resolution: Fixed Votes: 0
Labels: None


 Description   

PREPARE stmt FROM "SELECT 'x' ORDER BY NAME_CONST( 'f', 'foo' )";
EXECUTE stmt;

10.1 ASAN 1d56d875fe2

==17882==ERROR: AddressSanitizer: use-after-poison on address 0x62b0000239c8 at pc 0x558fa040b217 bp 0x7f6549739b10 sp 0x7f6549739b08
READ of size 1 at 0x62b0000239c8 thread T6
    #0 0x558fa040b216 in my_strcasecmp_utf8 /data/src/10.1/strings/ctype-utf8.c:5224
    #1 0x558f9f592aaa in Item::eq(Item const*, bool) const /data/src/10.1/sql/item.cc:1102
    #2 0x558f9ef6a59d in find_item_in_list(Item*, List<Item>&, unsigned int*, find_item_error_report_type, enum_resolution_type*) /data/src/10.1/sql/sql_base.cc:7141
    #3 0x558f9f0cc7b7 in find_order_in_list /data/src/10.1/sql/sql_select.cc:22273
    #4 0x558f9f12a9de in setup_order(THD*, Item**, TABLE_LIST*, List<Item>&, List<Item>&, st_order*) /data/src/10.1/sql/sql_select.cc:22401
    #5 0x558f9f1353ea in setup_without_group /data/src/10.1/sql/sql_select.cc:663
    #6 0x558f9f1353ea in JOIN::prepare(Item***, TABLE_LIST*, unsigned int, Item*, unsigned int, st_order*, bool, st_order*, Item*, st_order*, st_select_lex*, st_select_lex_unit*) /data/src/10.1/sql/sql_select.cc:811
    #7 0x558f9f1609c7 in mysql_select(THD*, Item***, TABLE_LIST*, unsigned int, List<Item>&, Item*, unsigned int, st_order*, st_order*, Item*, st_order*, unsigned long long, select_result*, st_select_lex_unit*, st_select_lex*) /data/src/10.1/sql/sql_select.cc:3477
    #8 0x558f9f161343 in handle_select(THD*, LEX*, select_result*, unsigned long) /data/src/10.1/sql/sql_select.cc:388
    #9 0x558f9f03043b in execute_sqlcom_select /data/src/10.1/sql/sql_parse.cc:5950
    #10 0x558f9f04a64b in mysql_execute_command(THD*) /data/src/10.1/sql/sql_parse.cc:2995
    #11 0x558f9f09ab6c in Prepared_statement::execute(String*, bool) /data/src/10.1/sql/sql_prepare.cc:4322
    #12 0x558f9f09b71d in Prepared_statement::execute_loop(String*, bool, unsigned char*, unsigned char*) /data/src/10.1/sql/sql_prepare.cc:3954
    #13 0x558f9f09cb94 in mysql_sql_stmt_execute(THD*) /data/src/10.1/sql/sql_prepare.cc:3070
    #14 0x558f9f04a674 in mysql_execute_command(THD*) /data/src/10.1/sql/sql_parse.cc:3006
    #15 0x558f9f062877 in mysql_parse(THD*, char*, unsigned int, Parser_state*) /data/src/10.1/sql/sql_parse.cc:7468
    #16 0x558f9f06965b in dispatch_command(enum_server_command, THD*, char*, unsigned int) /data/src/10.1/sql/sql_parse.cc:1496
    #17 0x558f9f06fde7 in do_command(THD*) /data/src/10.1/sql/sql_parse.cc:1124
    #18 0x558f9f317c69 in do_handle_one_connection(THD*) /data/src/10.1/sql/sql_connect.cc:1330
    #19 0x558f9f31817a in handle_one_connection /data/src/10.1/sql/sql_connect.cc:1242
    #20 0x558f9fbe6cb4 in pfs_spawn_thread /data/src/10.1/storage/perfschema/pfs.cc:1861
    #21 0x7f6555e70493 in start_thread (/lib/x86_64-linux-gnu/libpthread.so.0+0x7493)
    #22 0x7f655422993e in __clone (/lib/x86_64-linux-gnu/libc.so.6+0xe893e)
 
0x62b0000239c8 is located 1992 bytes inside of 24716-byte region [0x62b000023200,0x62b00002928c)
allocated by thread T6 here:
    #0 0x7f65560da73f in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.1+0x5473f)
    #1 0x558fa03b068d in sf_malloc /data/src/10.1/mysys/safemalloc.c:115
    #2 0x558fa04a90da (/data/bld/10.1-asan/bin/mysqld+0x1d9b0da)
 
Thread T6 created by T0 here:
    #0 0x7f65560a9bba in pthread_create (/usr/lib/x86_64-linux-gnu/libasan.so.1+0x23bba)
    #1 0x558f9fbf250b in spawn_thread_v1 /data/src/10.1/storage/perfschema/pfs.cc:1911
 
SUMMARY: AddressSanitizer: use-after-poison /data/src/10.1/strings/ctype-utf8.c:5224 my_strcasecmp_utf8
Shadow bytes around the buggy address:
  0x0c567fffc6e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c567fffc6f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c567fffc700: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c567fffc710: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c567fffc720: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c567fffc730: 00 00 00 00 00 00 00 00 00[f7]f7 f7 f7 f7 f7 f7
  0x0c567fffc740: f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7
  0x0c567fffc750: f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7
  0x0c567fffc760: f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7
  0x0c567fffc770: f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7
  0x0c567fffc780: f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:       fa
  Heap right redzone:      fb
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack partial redzone:   f4
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Contiguous container OOB:fc
  ASan internal:           fe
==17882==ABORTING

10.4 30da40bb8c3

==18839==ERROR: AddressSanitizer: use-after-poison on address 0x62b000062b68 at pc 0x5569486415ec bp 0x7f93887f07a0 sp 0x7f93887f0798
READ of size 1 at 0x62b000062b68 thread T5
    #0 0x5569486415eb in my_strcasecmp_utf8 /data/src/10.4/strings/ctype-utf8.c:5109
    #1 0x55694745dd81 in lex_string_cmp /data/src/10.4/sql/lex_string.h:28
    #2 0x556947466cd7 in Item::eq(Item const*, bool) const /data/src/10.4/sql/item.cc:1122
    #3 0x556946b9dd33 in find_item_in_list(Item*, List<Item>&, unsigned int*, find_item_error_report_type, enum_resolution_type*, unsigned int) /data/src/10.4/sql/sql_base.cc:6616
    #4 0x556946e1f271 in find_order_in_list /data/src/10.4/sql/sql_select.cc:23040
    #5 0x556946e200bc in setup_order(THD*, Bounds_checked_array<Item*>, TABLE_LIST*, List<Item>&, List<Item>&, st_order*, bool) /data/src/10.4/sql/sql_select.cc:23180
    #6 0x556946d7c2cd in setup_without_group /data/src/10.4/sql/sql_select.cc:666
    #7 0x556946d80f29 in JOIN::prepare(TABLE_LIST*, unsigned int, Item*, unsigned int, st_order*, bool, st_order*, Item*, st_order*, st_select_lex*, st_select_lex_unit*) /data/src/10.4/sql/sql_select.cc:1113
    #8 0x556946da05f0 in mysql_select(THD*, TABLE_LIST*, unsigned int, List<Item>&, Item*, unsigned int, st_order*, st_order*, Item*, st_order*, unsigned long long, select_result*, st_select_lex_unit*, st_select_lex*) /data/src/10.4/sql/sql_select.cc:4271
    #9 0x556946d7a213 in handle_select(THD*, LEX*, select_result*, unsigned long) /data/src/10.4/sql/sql_select.cc:385
    #10 0x556946cfc3b9 in execute_sqlcom_select /data/src/10.4/sql/sql_parse.cc:6567
    #11 0x556946cea86e in mysql_execute_command(THD*) /data/src/10.4/sql/sql_parse.cc:3776
    #12 0x556946d4d93e in Prepared_statement::execute(String*, bool) /data/src/10.4/sql/sql_prepare.cc:4790
    #13 0x556946d490aa in Prepared_statement::execute_loop(String*, bool, unsigned char*, unsigned char*) /data/src/10.4/sql/sql_prepare.cc:4218
    #14 0x556946d43add in mysql_sql_stmt_execute(THD*) /data/src/10.4/sql/sql_prepare.cc:3326
    #15 0x556946cea8b3 in mysql_execute_command(THD*) /data/src/10.4/sql/sql_parse.cc:3792
    #16 0x556946d04d8e in mysql_parse(THD*, char*, unsigned int, Parser_state*, bool, bool) /data/src/10.4/sql/sql_parse.cc:8104
    #17 0x556946cdf2f4 in dispatch_command(enum_server_command, THD*, char*, unsigned int, bool, bool) /data/src/10.4/sql/sql_parse.cc:1851
    #18 0x556946cdc37c in do_command(THD*) /data/src/10.4/sql/sql_parse.cc:1396
    #19 0x5569470527e2 in do_handle_one_connection(CONNECT*) /data/src/10.4/sql/sql_connect.cc:1402
    #20 0x5569470521ee in handle_one_connection /data/src/10.4/sql/sql_connect.cc:1308
    #21 0x556947bb88c7 in pfs_spawn_thread /data/src/10.4/storage/perfschema/pfs.cc:1862
    #22 0x7f9394c31493 in start_thread (/lib/x86_64-linux-gnu/libpthread.so.0+0x7493)
    #23 0x7f939323293e in __clone (/lib/x86_64-linux-gnu/libc.so.6+0xe893e)
 
0x62b000062b68 is located 2408 bytes inside of 24716-byte region [0x62b000062200,0x62b00006828c)
allocated by thread T5 here:
    #0 0x7f9394e9b73f in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.1+0x5473f)
    #1 0x556948563284 in sf_malloc /data/src/10.4/mysys/safemalloc.c:118
    #2 0x556948533c01 in my_malloc /data/src/10.4/mysys/my_malloc.c:101
    #3 0x556948512df7 in reset_root_defaults /data/src/10.4/mysys/my_alloc.c:151
    #4 0x556946be85fb in THD::init_for_queries() /data/src/10.4/sql/sql_class.cc:1337
    #5 0x556947051ba2 in prepare_new_connection_state(THD*) /data/src/10.4/sql/sql_connect.cc:1239
    #6 0x556947052234 in thd_prepare_connection(THD*) /data/src/10.4/sql/sql_connect.cc:1323
    #7 0x5569470527b8 in do_handle_one_connection(CONNECT*) /data/src/10.4/sql/sql_connect.cc:1393
    #8 0x5569470521ee in handle_one_connection /data/src/10.4/sql/sql_connect.cc:1308
    #9 0x556947bb88c7 in pfs_spawn_thread /data/src/10.4/storage/perfschema/pfs.cc:1862
    #10 0x7f9394c31493 in start_thread (/lib/x86_64-linux-gnu/libpthread.so.0+0x7493)
 
Thread T5 created by T0 here:
    #0 0x7f9394e6abba in pthread_create (/usr/lib/x86_64-linux-gnu/libasan.so.1+0x23bba)
    #1 0x556947bb8e8f in spawn_thread_v1 /data/src/10.4/storage/perfschema/pfs.cc:1912
    #2 0x556946a36ea6 in inline_mysql_thread_create /data/src/10.4/include/mysql/psi/mysql_thread.h:1268
    #3 0x556946a4d0c4 in create_thread_to_handle_connection(CONNECT*) /data/src/10.4/sql/mysqld.cc:6436
    #4 0x556946a4d7c9 in create_new_thread(CONNECT*) /data/src/10.4/sql/mysqld.cc:6506
    #5 0x556946a4db59 in handle_accepted_socket(st_mysql_socket, st_mysql_socket) /data/src/10.4/sql/mysqld.cc:6623
    #6 0x556946a4e7a5 in handle_connections_sockets() /data/src/10.4/sql/mysqld.cc:6788
    #7 0x556946a4c581 in mysqld_main(int, char**) /data/src/10.4/sql/mysqld.cc:6058
    #8 0x556946a34d2f in main /data/src/10.4/sql/main.cc:25
    #9 0x7f939316a2b0 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x202b0)
 
SUMMARY: AddressSanitizer: use-after-poison /data/src/10.4/strings/ctype-utf8.c:5109 my_strcasecmp_utf8
Shadow bytes around the buggy address:
  0x0c5680004510: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c5680004520: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c5680004530: 00 00 00 00 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7
  0x0c5680004540: f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7
  0x0c5680004550: f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7
=>0x0c5680004560: f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7[f7]f7 f7
  0x0c5680004570: f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7
  0x0c5680004580: f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7
  0x0c5680004590: f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7
  0x0c56800045a0: f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7
  0x0c56800045b0: f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7 f7
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:       fa
  Heap right redzone:      fb
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack partial redzone:   f4
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Contiguous container OOB:fc
  ASan internal:           fe
==18839==ABORTING

Not reproducible on 10.0.



 Comments   
Comment by Alexander Barkov [ 2019-01-15 ]

When Item_name_const::fix_fields() is called from find_order_in_list(), execution goes through this code:

  if (is_autogenerated_name)
  {
    set_name(thd, item_name->c_ptr(), (uint) item_name->length(),
             system_charset_info);
  }

(gdb) p thd->mem_root->name
$22 = 0x12df557 "THD::main_mem_root"

thd->mem_root here points already to THD::main_mem_root rather than to Prepared_statement::main_mem_root, so the name string gets created on THD::main_mem_root which gets cleared in the end of dispatch_command() of the PREPARE statement.

Later, when EXECUTE is performed, Item_name_const::name points to an already freed memory.

Comment by Alexander Barkov [ 2019-01-15 ]

Stack trace when Item::name gets set to a value allocated on a wrong memory root:

#0  Item::set_name (this=0x7ffe6c01dd60, thd=0x7ffe6c000b00, 
    str=0x7ffe6c01dbe8 "f", length=1, 
    cs=0x1f02580 <my_charset_utf8_general_ci>)
    at /home/bar/maria-git/server.10.4/sql/item.cc:1030
#1  0x0000000000aaf03c in Item_name_const::fix_fields (this=0x7ffe6c01dd60, 
    thd=0x7ffe6c000b00, ref=0x7ffe6c01de18)
    at /home/bar/maria-git/server.10.4/sql/item.cc:2008
#2  0x000000000069bd83 in Item::fix_fields_if_needed (this=0x7ffe6c01dd60, 
    thd=0x7ffe6c000b00, ref=0x7ffe6c01de18)
    at /home/bar/maria-git/server.10.4/sql/item.h:954
#3  0x000000000069bdb1 in Item::fix_fields_if_needed_for_scalar (
    this=0x7ffe6c01dd60, thd=0x7ffe6c000b00, ref=0x7ffe6c01de18)
    at /home/bar/maria-git/server.10.4/sql/item.h:958
#4  0x000000000081f7ad in Item::fix_fields_if_needed_for_order_by (
    this=0x7ffe6c01dd60, thd=0x7ffe6c000b00, ref=0x7ffe6c01de18)
    at /home/bar/maria-git/server.10.4/sql/item.h:966
#5  0x0000000000811d9b in find_order_in_list (thd=0x7ffe6c000b00, 
    ref_pointer_array=..., tables=0x0, order=0x7ffe6c01de08, fields=..., 
    all_fields=..., is_group_field=false, add_to_all_fields=true, 
    from_window_spec=false)
    at /home/bar/maria-git/server.10.4/sql/sql_select.cc:23136
#6  0x0000000000811f29 in setup_order (thd=0x7ffe6c000b00, 
    ref_pointer_array=..., tables=0x0, fields=..., all_fields=..., 
    order=0x7ffe6c01de08, from_window_spec=false)
    at /home/bar/maria-git/server.10.4/sql/sql_select.cc:23180
#7  0x00000000007d53f3 in setup_without_group (thd=0x7ffe6c000b00, 
    ref_pointer_array=..., tables=0x0, leaves=..., fields=..., all_fields=..., 
    conds=0x7ffe6c0161f8, order=0x7ffe6c01de08, group=0x0, win_specs=..., 
    win_funcs=..., hidden_group_fields=0x7ffe6c0160d7, reserved=0x7ffe6c01d9dc)
    at /home/bar/maria-git/server.10.4/sql/sql_select.cc:665
#8  0x00000000007d7607 in JOIN::prepare (this=0x7ffe6c015df0, tables_init=0x0, 
    wild_num=0, conds_init=0x0, og_num=1, order_init=0x7ffe6c01de08, 
    skip_order_by=false, group_init=0x0, having_init=0x0, proc_param_init=0x0, 
    select_lex_arg=0x7ffe6c01d6f0, unit_arg=0x7ffe6c01bd88)
    at /home/bar/maria-git/server.10.4/sql/sql_select.cc:1107
#9  0x0000000000882639 in st_select_lex_unit::prepare_join (
    this=0x7ffe6c01bd88, thd_arg=0x7ffe6c000b00, sl=0x7ffe6c01d6f0, 
    tmp_result=0x0, additional_options=0, is_union_select=false)
    at /home/bar/maria-git/server.10.4/sql/sql_union.cc:654
#10 0x0000000000883ab0 in st_select_lex_unit::prepare (this=0x7ffe6c01bd88, 
    derived_arg=0x0, sel_result=0x0, additional_options=0)
    at /home/bar/maria-git/server.10.4/sql/sql_union.cc:977
#11 0x00000000007b9c7b in mysql_test_select (stmt=0x7ffe6c014ae0, tables=0x0)
    at /home/bar/maria-git/server.10.4/sql/sql_prepare.cc:1545
#12 0x00000000007bb6ae in check_prepared_statement (stmt=0x7ffe6c014ae0)
    at /home/bar/maria-git/server.10.4/sql/sql_prepare.cc:2336
#13 0x00000000007bf412 in Prepared_statement::prepare (this=0x7ffe6c014ae0, 
    packet=0x7ffe6c015d08 "SELECT 'x' ORDER BY NAME_CONST( 'f', 'foo' )", 
    packet_len=44) at /home/bar/maria-git/server.10.4/sql/sql_prepare.cc:4011

Comment by Oleksandr Byelkin [ 2019-01-21 ]

OK to push!

Generated at Thu Feb 08 08:42:13 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.