Uploaded image for project: 'MariaDB Server'
  1. MariaDB Server
  2. MDEV-20944

Wrong result of LEAST() and ASAN heap-use-after-free in my_strnncollsp_simple / Item::temporal_precision on TIME()

    XMLWordPrintable

    Details

      Description

      SET NAMES latin1;
      SELECT LEAST( CAST( 0 AS CHAR ), OLD_PASSWORD( 1 ) );
      

      Actual result

      MariaDB [(none)]> SELECT LEAST( CAST( 0 AS CHAR ), OLD_PASSWORD( 1 ) );
      +-----------------------------------------------+
      | LEAST( CAST( 0 AS CHAR ), OLD_PASSWORD( 1 ) ) |
      +-----------------------------------------------+
      | 1                                             |
      +-----------------------------------------------+
      1 row in set (0.000 sec)
      

      Expected result is 0.

      Reproducible on 10.1-10.5 with latin1. Not reproducible with utf8. I didn't try other character sets.

      Further, on 10.1 / 10.2 (but not on 10.3+) the following returns an ASAN error, I suppose it's a side effect of the above:

      SET NAMES latin1;
      SELECT TIME( LEAST( CAST( 0 AS CHAR ), OLD_PASSWORD( 1 ) ) ); 
      

      10.2 1552b254

      ==14630==ERROR: AddressSanitizer: heap-use-after-free on address 0x60d0000318e0 at pc 0x5648a32eac74 bp 0x7f9fd828bc70 sp 0x7f9fd828bc68
      READ of size 1 at 0x60d0000318e0 thread T5
          #0 0x5648a32eac73 in my_strnncollsp_simple /data/src/10.2/strings/ctype-simple.c:182
          #1 0x5648a1dfe409 in sortcmp(String const*, String const*, charset_info_st const*) /data/src/10.2/sql/sql_string.cc:804
          #2 0x5648a233b8e3 in Item_func_min_max::val_str(String*) /data/src/10.2/sql/item_func.cc:2970
          #3 0x5648a2239c5f in Item::temporal_precision(enum_field_types) /data/src/10.2/sql/item.cc:576
          #4 0x5648a2454b0b in Item_temporal_typecast::fix_length_and_dec() /data/src/10.2/sql/item_timefunc.h:1099
          #5 0x5648a231b4c1 in Item_func::fix_fields(THD*, Item**) /data/src/10.2/sql/item_func.cc:229
          #6 0x5648a1b3293c in setup_fields(THD*, Bounds_checked_array<Item*>, List<Item>&, enum_mark_columns, List<Item>*, List<Item>*, bool) /data/src/10.2/sql/sql_base.cc:7186
          #7 0x5648a1cb83f9 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.2/sql/sql_select.cc:811
          #8 0x5648a1cd5549 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.2/sql/sql_select.cc:3796
          #9 0x5648a1cb4aad in handle_select(THD*, LEX*, select_result*, unsigned long) /data/src/10.2/sql/sql_select.cc:361
          #10 0x5648a1c3a2c6 in execute_sqlcom_select /data/src/10.2/sql/sql_parse.cc:6225
          #11 0x5648a1c2861b in mysql_execute_command(THD*) /data/src/10.2/sql/sql_parse.cc:3532
          #12 0x5648a1c42d48 in mysql_parse(THD*, char*, unsigned int, Parser_state*, bool, bool) /data/src/10.2/sql/sql_parse.cc:7740
          #13 0x5648a1c1ee06 in dispatch_command(enum_server_command, THD*, char*, unsigned int, bool, bool) /data/src/10.2/sql/sql_parse.cc:1831
          #14 0x5648a1c1be1c in do_command(THD*) /data/src/10.2/sql/sql_parse.cc:1384
          #15 0x5648a1f562dd in do_handle_one_connection(CONNECT*) /data/src/10.2/sql/sql_connect.cc:1336
          #16 0x5648a1f55cad in handle_one_connection /data/src/10.2/sql/sql_connect.cc:1241
          #17 0x5648a318c7c2 in pfs_spawn_thread /data/src/10.2/storage/perfschema/pfs.cc:1862
          #18 0x7f9fe3b5d4a3 in start_thread (/lib/x86_64-linux-gnu/libpthread.so.0+0x74a3)
          #19 0x7f9fe20a4d0e in __clone (/lib/x86_64-linux-gnu/libc.so.6+0xe8d0e)
       
      0x60d0000318e0 is located 112 bytes inside of 140-byte region [0x60d000031870,0x60d0000318fc)
      freed by thread T5 here:
          #0 0x7f9fe3e34a10 in free (/usr/lib/x86_64-linux-gnu/libasan.so.3+0xc1a10)
          #1 0x5648a3292625 in free_memory /data/src/10.2/mysys/safemalloc.c:279
          #2 0x5648a3291c9f in sf_free /data/src/10.2/mysys/safemalloc.c:197
          #3 0x5648a3262aab in my_free /data/src/10.2/mysys/my_malloc.c:218
          #4 0x5648a1a45eaf in String::free() /data/src/10.2/sql/sql_string.h:347
          #5 0x5648a1a66e7e in String::set(char*, unsigned int, charset_info_st const*) /data/src/10.2/sql/sql_string.h:264
          #6 0x5648a23a5536 in Item_func_password::val_str_ascii(String*) /data/src/10.2/sql/item_strfunc.cc:2167
          #7 0x5648a23920b7 in Item_func::val_str_from_val_str_ascii(String*, String*) /data/src/10.2/sql/item_strfunc.cc:82
          #8 0x5648a2143899 in Item_str_ascii_func::val_str(String*) /data/src/10.2/sql/item_strfunc.h:94
          #9 0x5648a233b894 in Item_func_min_max::val_str(String*) /data/src/10.2/sql/item_func.cc:2967
          #10 0x5648a2239c5f in Item::temporal_precision(enum_field_types) /data/src/10.2/sql/item.cc:576
          #11 0x5648a2454b0b in Item_temporal_typecast::fix_length_and_dec() /data/src/10.2/sql/item_timefunc.h:1099
          #12 0x5648a231b4c1 in Item_func::fix_fields(THD*, Item**) /data/src/10.2/sql/item_func.cc:229
          #13 0x5648a1b3293c in setup_fields(THD*, Bounds_checked_array<Item*>, List<Item>&, enum_mark_columns, List<Item>*, List<Item>*, bool) /data/src/10.2/sql/sql_base.cc:7186
          #14 0x5648a1cb83f9 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.2/sql/sql_select.cc:811
          #15 0x5648a1cd5549 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.2/sql/sql_select.cc:3796
          #16 0x5648a1cb4aad in handle_select(THD*, LEX*, select_result*, unsigned long) /data/src/10.2/sql/sql_select.cc:361
          #17 0x5648a1c3a2c6 in execute_sqlcom_select /data/src/10.2/sql/sql_parse.cc:6225
          #18 0x5648a1c2861b in mysql_execute_command(THD*) /data/src/10.2/sql/sql_parse.cc:3532
          #19 0x5648a1c42d48 in mysql_parse(THD*, char*, unsigned int, Parser_state*, bool, bool) /data/src/10.2/sql/sql_parse.cc:7740
          #20 0x5648a1c1ee06 in dispatch_command(enum_server_command, THD*, char*, unsigned int, bool, bool) /data/src/10.2/sql/sql_parse.cc:1831
          #21 0x5648a1c1be1c in do_command(THD*) /data/src/10.2/sql/sql_parse.cc:1384
          #22 0x5648a1f562dd in do_handle_one_connection(CONNECT*) /data/src/10.2/sql/sql_connect.cc:1336
          #23 0x5648a1f55cad in handle_one_connection /data/src/10.2/sql/sql_connect.cc:1241
          #24 0x5648a318c7c2 in pfs_spawn_thread /data/src/10.2/storage/perfschema/pfs.cc:1862
          #25 0x7f9fe3b5d4a3 in start_thread (/lib/x86_64-linux-gnu/libpthread.so.0+0x74a3)
       
      previously allocated by thread T5 here:
          #0 0x7f9fe3e34d28 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.3+0xc1d28)
          #1 0x5648a3291676 in sf_malloc /data/src/10.2/mysys/safemalloc.c:118
          #2 0x5648a326220e in my_malloc /data/src/10.2/mysys/my_malloc.c:101
          #3 0x5648a1df9b06 in String::real_alloc(unsigned long) /data/src/10.2/sql/sql_string.cc:45
          #4 0x5648a1a66fd9 in String::alloc(unsigned long) /data/src/10.2/sql/sql_string.h:357
          #5 0x5648a1dfa14c in String::set_int(long long, bool, charset_info_st const*) /data/src/10.2/sql/sql_string.cc:128
          #6 0x5648a224cd1a in Item_int::val_str(String*) /data/src/10.2/sql/item.cc:3086
          #7 0x5648a2449d9e in Item_char_typecast::val_str(String*) /data/src/10.2/sql/item_timefunc.cc:2505
          #8 0x5648a233b72a in Item_func_min_max::val_str(String*) /data/src/10.2/sql/item_func.cc:2963
          #9 0x5648a2239c5f in Item::temporal_precision(enum_field_types) /data/src/10.2/sql/item.cc:576
          #10 0x5648a2454b0b in Item_temporal_typecast::fix_length_and_dec() /data/src/10.2/sql/item_timefunc.h:1099
          #11 0x5648a231b4c1 in Item_func::fix_fields(THD*, Item**) /data/src/10.2/sql/item_func.cc:229
          #12 0x5648a1b3293c in setup_fields(THD*, Bounds_checked_array<Item*>, List<Item>&, enum_mark_columns, List<Item>*, List<Item>*, bool) /data/src/10.2/sql/sql_base.cc:7186
          #13 0x5648a1cb83f9 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.2/sql/sql_select.cc:811
          #14 0x5648a1cd5549 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.2/sql/sql_select.cc:3796
          #15 0x5648a1cb4aad in handle_select(THD*, LEX*, select_result*, unsigned long) /data/src/10.2/sql/sql_select.cc:361
          #16 0x5648a1c3a2c6 in execute_sqlcom_select /data/src/10.2/sql/sql_parse.cc:6225
          #17 0x5648a1c2861b in mysql_execute_command(THD*) /data/src/10.2/sql/sql_parse.cc:3532
          #18 0x5648a1c42d48 in mysql_parse(THD*, char*, unsigned int, Parser_state*, bool, bool) /data/src/10.2/sql/sql_parse.cc:7740
          #19 0x5648a1c1ee06 in dispatch_command(enum_server_command, THD*, char*, unsigned int, bool, bool) /data/src/10.2/sql/sql_parse.cc:1831
          #20 0x5648a1c1be1c in do_command(THD*) /data/src/10.2/sql/sql_parse.cc:1384
          #21 0x5648a1f562dd in do_handle_one_connection(CONNECT*) /data/src/10.2/sql/sql_connect.cc:1336
          #22 0x5648a1f55cad in handle_one_connection /data/src/10.2/sql/sql_connect.cc:1241
          #23 0x5648a318c7c2 in pfs_spawn_thread /data/src/10.2/storage/perfschema/pfs.cc:1862
          #24 0x7f9fe3b5d4a3 in start_thread (/lib/x86_64-linux-gnu/libpthread.so.0+0x74a3)
       
      Thread T5 created by T0 here:
          #0 0x7f9fe3da3f59 in __interceptor_pthread_create (/usr/lib/x86_64-linux-gnu/libasan.so.3+0x30f59)
          #1 0x5648a318cbfe in spawn_thread_v1 /data/src/10.2/storage/perfschema/pfs.cc:1912
          #2 0x5648a1a246b2 in inline_mysql_thread_create /data/src/10.2/include/mysql/psi/mysql_thread.h:1239
          #3 0x5648a1a38cac in create_thread_to_handle_connection(CONNECT*) /data/src/10.2/sql/mysqld.cc:6506
          #4 0x5648a1a3938f in create_new_thread /data/src/10.2/sql/mysqld.cc:6576
          #5 0x5648a1a3a3a7 in handle_connections_sockets() /data/src/10.2/sql/mysqld.cc:6851
          #6 0x5648a1a38217 in mysqld_main(int, char**) /data/src/10.2/sql/mysqld.cc:6125
          #7 0x5648a1a2303f in main /data/src/10.2/sql/main.cc:25
          #8 0x7f9fe1fdc2e0 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x202e0)
       
      SUMMARY: AddressSanitizer: heap-use-after-free /data/src/10.2/strings/ctype-simple.c:182 in my_strnncollsp_simple
      Shadow bytes around the buggy address:
        0x0c1a7fffe2c0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
        0x0c1a7fffe2d0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
        0x0c1a7fffe2e0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
        0x0c1a7fffe2f0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
        0x0c1a7fffe300: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fd fd
      =>0x0c1a7fffe310: fd fd fd fd fd fd fd fd fd fd fd fd[fd]fd fd fd
        0x0c1a7fffe320: fa fa fa fa fa fa fa fa fd fd fd fd fd fd fd fd
        0x0c1a7fffe330: fd fd fd fd fd fd fd fd fd fd fa fa fa fa fa fa
        0x0c1a7fffe340: fa fa fd fd fd fd fd fd fd fd fd fd fd fd fd fd
        0x0c1a7fffe350: fd fd fd fd fa fa fa fa fa fa fa fa fd fd fd fd
        0x0c1a7fffe360: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fa fa
      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
        Container overflow:      fc
        Array cookie:            ac
        Intra object redzone:    bb
        ASan internal:           fe
        Left alloca redzone:     ca
        Right alloca redzone:    cb
      ==14630==ABORTING
      

        Attachments

          Activity

            People

            Assignee:
            bar Alexander Barkov
            Reporter:
            elenst Elena Stepanova
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated:

                Git Integration