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

ASAN heap-use-after-free in THD::set_sent_row_count via select_export destructor at shutdown w/ INTO OUTFILE trigger

    XMLWordPrintable

Details

    • Q3/2026 Server Maintenance

    Description

      CLI Testcase:

      SET sql_mode='';
      CREATE TABLE t4 (c1 INT,c2 INT,c3 INT,c4 VARCHAR(100)) ENGINE=Aria;
      CREATE TRIGGER tr1 BEFORE INSERT ON t4 FOR EACH ROW SELECT * FROM t4 INTO OUTFILE '0';
      INSERT DELAYED INTO t4 VALUES (0,0,0,'0');
      SHUTDOWN;
      

      MTR Testcase:

      SET sql_mode='';
      CREATE TABLE t4 (c1 INT,c2 INT,c3 INT,c4 VARCHAR(100)) ENGINE=Aria;
      CREATE TRIGGER tr1 BEFORE INSERT ON t4 FOR EACH ROW SELECT * FROM t4 INTO OUTFILE '0';
      --error ER_DELAYED_NOT_SUPPORTED
      INSERT DELAYED INTO t4 VALUES (0,0,0,'0');
      SHUTDOWN;
      

      The trigger binds a select_export to the SP lex keeper; the INSERT DELAYED routes the trigger through a delayed-insert worker whose THD is later freed, and the cached trigger's select_export destructor touches that freed THD at SHUTDOWN teardown.

      Leads to:

      CS 13.1.0 02e1853c894906737fe0ea5f836adb087b1a72ad (Debug, UBASAN, Clang 22.1.6-20260529) Build 05/06/2026

      ==2679062==ERROR: AddressSanitizer: heap-use-after-free on address 0x715d423fc298 at pc 0x644d77e874d2 bp 0x7ffce9c7c1c0 sp 0x7ffce9c7c1b8
      WRITE of size 8 at 0x715d423fc298 thread T0
          #0 0x644d77e874d1 in THD::set_sent_row_count(unsigned long long) /test/13.0_dbg_san/sql/sql_class.cc:6480:19
          #1 0x644d77e874d1 in select_export::~select_export() /test/13.0_dbg_san/sql/sql_class.cc:3527:8
          #2 0x644d77e8753d in select_export::~select_export() /test/13.0_dbg_san/sql/sql_class.cc:3526:1
          #3 0x644d77cac09d in sp_lex_keeper::~sp_lex_keeper() /test/13.0_dbg_san/sql/sp_instr.h:332:7
          #4 0x644d77cabe52 in sp_lex_instr::~sp_lex_instr() /test/13.0_dbg_san/sql/sp_instr.h:477:3
          #5 0x644d78e53c4d in sp_instr_stmt::~sp_instr_stmt() /test/13.0_dbg_san/sql/sp_instr.h:624:36
          #6 0x644d77c799a6 in sp_head::~sp_head() /test/13.0_dbg_san/sql/sp_head.cc:910:5
          #7 0x644d77c7e1fd in sp_head::~sp_head() /test/13.0_dbg_san/sql/sp_head.cc:902:1
          #8 0x644d77c779b2 in sp_head::destroy(sp_head*) /test/13.0_dbg_san/sql/sp_head.cc:537:5
          #9 0x644d7886bff1 in Trigger::~Trigger() /test/13.0_dbg_san/sql/sql_trigger.cc:369:3
          #10 0x644d788783ac in Table_triggers_list::~Table_triggers_list() /test/13.0_dbg_san/sql/sql_trigger.cc:1526:11
          #11 0x644d78f8e73a in intern_close_table(TABLE*) /test/13.0_dbg_san/sql/table_cache.cc:228:3
          #12 0x644d78f8e262 in tc_purge() /test/13.0_dbg_san/sql/table_cache.cc:324:5
          #13 0x644d77dbfb95 in purge_tables() /test/13.0_dbg_san/sql/sql_base.cc:338:3
          #14 0x644d77220690 in clean_up(bool) /test/13.0_dbg_san/sql/mysqld.cc:2025:3
          #15 0x644d772255da in mysqld_main(int, char**) /test/13.0_dbg_san/sql/mysqld.cc:6445:3
          #16 0x729d4322a1c9 in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16
          #17 0x729d4322a28a in __libc_start_main csu/../csu/libc-start.c:360:3
          #18 0x644d77134674 in _start (/test/UBASAN_MD050626-mariadb-13.1.0-linux-x86_64-dbg/bin/mariadbd+0x40dd674)
       
      0x715d423fc298 is located 16536 bytes inside of 31664-byte region [0x715d423f8200,0x715d423ffdb0)
      freed by thread T37 here:
          #0 0x644d771d97b6 in free /test/llvm-build/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:51:3
          #1 0x644d781d4e85 in ilink::operator delete(void*, unsigned long) /test/13.0_dbg_san/sql/sql_list.h:697:6
          #2 0x644d781d4e85 in Delayed_insert::~Delayed_insert() /test/13.0_dbg_san/sql/sql_insert.cc:2706:3
          #3 0x644d781a9b5b in handle_delayed_insert /test/13.0_dbg_san/sql/sql_insert.cc:3807:5
          #4 0x644d771d72d6 in asan_thread_start(void*) /test/llvm-build/llvm-project/compiler-rt/lib/asan/asan_interceptors.cpp:246:28
       
      previously allocated by thread T36 here:
          #0 0x644d771d9a54 in malloc /test/llvm-build/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:67:3
          #1 0x644d7a4fee3c in my_malloc /test/13.0_dbg_san/mysys/my_malloc.c:93:29
          #2 0x644d78183fed in ilink::operator new(unsigned long) /test/13.0_dbg_san/sql/sql_list.h:692:19
          #3 0x644d78183fed in delayed_get_table(THD*, MDL_ticket*, TABLE_LIST*) /test/13.0_dbg_san/sql/sql_insert.cc:2859:17
          #4 0x644d78183fed in open_and_lock_for_insert_delayed(THD*, TABLE_LIST*) /test/13.0_dbg_san/sql/sql_insert.cc:600:7
          #5 0x644d78183fed in mysql_insert(THD*, TABLE_LIST*, List<Item>&, List<List<Item>>&, List<Item>&, List<Item>&, enum_duplicates, bool, select_result*) /test/13.0_dbg_san/sql/sql_insert.cc:807:9
          #6 0x644d7830bc1c in mysql_execute_command(THD*, bool) /test/13.0_dbg_san/sql/sql_parse.cc:4502:10
          #7 0x644d782e155a in mysql_parse(THD*, char*, unsigned int, Parser_state*) /test/13.0_dbg_san/sql/sql_parse.cc:7945:18
          #8 0x644d782d93dc in dispatch_command(enum_server_command, THD*, char*, unsigned int, bool) /test/13.0_dbg_san/sql/sql_parse.cc:1903:7
          #9 0x644d782e3946 in do_command(THD*, bool) /test/13.0_dbg_san/sql/sql_parse.cc:1437:17
          #10 0x644d78b0ffbc in do_handle_one_connection(CONNECT*, bool) /test/13.0_dbg_san/sql/sql_connect.cc:1503:11
          #11 0x644d78b0fae5 in handle_one_connection /test/13.0_dbg_san/sql/sql_connect.cc:1415:5
          #12 0x644d771d72d6 in asan_thread_start(void*) /test/llvm-build/llvm-project/compiler-rt/lib/asan/asan_interceptors.cpp:246:28
       
      Thread T37 created by T36 here:
          #0 0x644d771bcf51 in pthread_create /test/llvm-build/llvm-project/compiler-rt/lib/asan/asan_interceptors.cpp:257:3
          #1 0x644d781846af in delayed_get_table(THD*, MDL_ticket*, TABLE_LIST*) /test/13.0_dbg_san/sql/sql_insert.cc:2901:19
          #2 0x644d781846af in open_and_lock_for_insert_delayed(THD*, TABLE_LIST*) /test/13.0_dbg_san/sql/sql_insert.cc:600:7
          #3 0x644d781846af in mysql_insert(THD*, TABLE_LIST*, List<Item>&, List<List<Item>>&, List<Item>&, List<Item>&, enum_duplicates, bool, select_result*) /test/13.0_dbg_san/sql/sql_insert.cc:807:9
          #4 0x644d7830bc1c in mysql_execute_command(THD*, bool) /test/13.0_dbg_san/sql/sql_parse.cc:4502:10
          #5 0x644d782e155a in mysql_parse(THD*, char*, unsigned int, Parser_state*) /test/13.0_dbg_san/sql/sql_parse.cc:7945:18
          #6 0x644d782d93dc in dispatch_command(enum_server_command, THD*, char*, unsigned int, bool) /test/13.0_dbg_san/sql/sql_parse.cc:1903:7
          #7 0x644d782e3946 in do_command(THD*, bool) /test/13.0_dbg_san/sql/sql_parse.cc:1437:17
          #8 0x644d78b0ffbc in do_handle_one_connection(CONNECT*, bool) /test/13.0_dbg_san/sql/sql_connect.cc:1503:11
          #9 0x644d78b0fae5 in handle_one_connection /test/13.0_dbg_san/sql/sql_connect.cc:1415:5
          #10 0x644d771d72d6 in asan_thread_start(void*) /test/llvm-build/llvm-project/compiler-rt/lib/asan/asan_interceptors.cpp:246:28
       
      Thread T36 created by T0 here:
          #0 0x644d771bcf51 in pthread_create /test/llvm-build/llvm-project/compiler-rt/lib/asan/asan_interceptors.cpp:257:3
          #1 0x644d77231c8c in create_thread_to_handle_connection(CONNECT*) /test/13.0_dbg_san/sql/mysqld.cc:6523:19
          #2 0x644d77232d02 in handle_connections_sockets() /test/13.0_dbg_san/sql/mysqld.cc:6759:9
          #3 0x644d7723128a in run_main_loop() /test/13.0_dbg_san/sql/mysqld.cc:5999:3
          #4 0x644d7722547c in mysqld_main(int, char**) /test/13.0_dbg_san/sql/mysqld.cc:6428:3
          #5 0x729d4322a1c9 in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16
          #6 0x729d4322a28a in __libc_start_main csu/../csu/libc-start.c:360:3
          #7 0x644d77134674 in _start (/test/UBASAN_MD050626-mariadb-13.1.0-linux-x86_64-dbg/bin/mariadbd+0x40dd674)
       
      SUMMARY: AddressSanitizer: heap-use-after-free /test/13.0_dbg_san/sql/sql_class.cc:6480:19 in THD::set_sent_row_count(unsigned long long)
      Shadow bytes around the buggy address:
        0x715d423fc000: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
        0x715d423fc080: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
        0x715d423fc100: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
        0x715d423fc180: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
        0x715d423fc200: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
      =>0x715d423fc280: fd fd fd[fd]fd fd fd fd fd fd fd fd fd fd fd fd
        0x715d423fc300: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
        0x715d423fc380: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
        0x715d423fc400: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
        0x715d423fc480: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
        0x715d423fc500: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
      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
        Freed heap region:       fd
        Stack left redzone:      f1
        Stack mid redzone:       f2
        Stack right redzone:     f3
        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
      ==2679062==ABORTING
      

      Setup:

      Compiled with a recent version of Clang and LLVM. Ubuntu instructions for Clang/LLVM 18:
        # Note: It is strongly recommended to uninstall all old Clang & LLVM packages (ref  dpkg --list | grep -iE 'clang|llvm'  and use  apt purge  and  dpkg --purge  to remove the packages), before installing Clang/LLVM 18
           sudo apt install clang llvm-18 llvm-18-linker-tools llvm-18-runtime llvm-18-tools llvm-18-dev libstdc++-14-dev llvm-dev lld-18
      Compiled with: "-DCMAKE_C_COMPILER=/usr/bin/clang -DCMAKE_CXX_COMPILER=/usr/bin/clang++ -DCMAKE_C{,XX}_FLAGS='-march=native -mtune=native'" and:
          -DWITH_ASAN=ON -DWITH_ASAN_SCOPE=ON -DWITH_UBSAN=ON -DWSREP_LIB_WITH_ASAN=ON
      Set before execution:
          export ASAN_OPTIONS=quarantine_size_mb=512:atexit=0:detect_invalid_pointer_pairs=3:dump_instruction_bytes=1:abort_on_error=1:allocator_may_return_null=1
      

      SAN Bug Detection Matrix

          Rel    o/d  Build   Commit                                    UniqueID observed             
      CS  10.6   dbg  100426  f39b634db715cd9dc1835653d1ce544df2aa1613  ASAN|heap-use-after-free|sql/sql_class.cc|THD::set_sent_row_count|select_export::~select_export|select_export::~select_export|sp_lex_keeper::~sp_lex_keeper
      CS  10.6   opt  100426  f39b634db715cd9dc1835653d1ce544df2aa1613  ASAN|heap-use-after-free|sql/sql_class.cc|THD::set_sent_row_count|select_export::~select_export|select_export::~select_export|sp_lex_keeper::~sp_lex_keeper
      CS  10.11  dbg  100426  ba774a0a90fac0163babe9d7a964aa36503e1711  ASAN|heap-use-after-free|sql/sql_class.cc|THD::set_sent_row_count|select_export::~select_export|select_export::~select_export|sp_lex_keeper::~sp_lex_keeper
      CS  10.11  opt  100426  ba774a0a90fac0163babe9d7a964aa36503e1711  ASAN|heap-use-after-free|sql/sql_class.cc|THD::set_sent_row_count|select_export::~select_export|select_export::~select_export|sp_lex_keeper::~sp_lex_keeper
      CS  11.4   dbg  100426  dc89915ad9bf3dcb67e66d2844c77ec0403373de  ASAN|heap-use-after-free|sql/sql_class.cc|THD::set_sent_row_count|select_export::~select_export|select_export::~select_export|sp_lex_keeper::~sp_lex_keeper
      CS  11.4   opt  100426  dc89915ad9bf3dcb67e66d2844c77ec0403373de  ASAN|heap-use-after-free|sql/sql_class.cc|THD::set_sent_row_count|select_export::~select_export|select_export::~select_export|sp_lex_keeper::~sp_lex_keeper
      CS  11.8   dbg  100426  e47db94aea7f0d6e0177e948486fc8860331f05f  ASAN|heap-use-after-free|sql/sql_class.cc|THD::set_sent_row_count|select_export::~select_export|select_export::~select_export|sp_lex_keeper::~sp_lex_keeper
      CS  11.8   opt  100426  e47db94aea7f0d6e0177e948486fc8860331f05f  ASAN|heap-use-after-free|sql/sql_class.cc|THD::set_sent_row_count|select_export::~select_export|select_export::~select_export|sp_lex_keeper::~sp_lex_keeper
      CS  12.3   dbg  100426  f5bb9922107672e88f7b5cbdb3d25151cc5744bb  ASAN|heap-use-after-free|sql/sql_class.cc|THD::set_sent_row_count|select_export::~select_export|select_export::~select_export|sp_lex_keeper::~sp_lex_keeper
      CS  12.3   opt  100426  f5bb9922107672e88f7b5cbdb3d25151cc5744bb  ASAN|heap-use-after-free|sql/sql_class.cc|THD::set_sent_row_count|select_export::~select_export|select_export::~select_export|sp_lex_keeper::~sp_lex_keeper
      CS  13.0   dbg  100426  3a2f8e27981b76b99d2b87cc3bcec5ef022b2b23  ASAN|heap-use-after-free|sql/sql_class.cc|THD::set_sent_row_count|select_export::~select_export|select_export::~select_export|sp_lex_keeper::~sp_lex_keeper
      CS  13.0   opt  100426  3a2f8e27981b76b99d2b87cc3bcec5ef022b2b23  ASAN|heap-use-after-free|sql/sql_class.cc|THD::set_sent_row_count|select_export::~select_export|select_export::~select_export|sp_lex_keeper::~sp_lex_keeper
      CS  13.1   dbg  050626  02e1853c894906737fe0ea5f836adb087b1a72ad  ASAN|heap-use-after-free|sql/sql_class.cc|THD::set_sent_row_count|select_export::~select_export|select_export::~select_export|sp_lex_keeper::~sp_lex_keeper
      CS  13.1   opt  050626  02e1853c894906737fe0ea5f836adb087b1a72ad  ASAN|heap-use-after-free|sql/sql_class.cc|THD::set_sent_row_count|select_export::~select_export|select_export::~select_export|sp_lex_keeper::~sp_lex_keeper
      ES  10.6   dbg  100426  84a80c8b38208d362225496da08d86d8d454e453  ASAN|heap-use-after-free|sql/sql_class.cc|THD::set_sent_row_count|select_export::~select_export|select_export::~select_export|sp_lex_keeper::~sp_lex_keeper
      ES  10.6   opt  100426  84a80c8b38208d362225496da08d86d8d454e453  ASAN|heap-use-after-free|sql/sql_class.cc|THD::set_sent_row_count|select_export::~select_export|select_export::~select_export|sp_lex_keeper::~sp_lex_keeper
      ES  11.4   dbg  100426  8b2bf17b733262409422ce7d039a0c021fc47077  ASAN|heap-use-after-free|sql/sql_class.cc|THD::set_sent_row_count|select_export::~select_export|select_export::~select_export|sp_lex_keeper::~sp_lex_keeper
      ES  11.4   opt  100426  8b2bf17b733262409422ce7d039a0c021fc47077  ASAN|heap-use-after-free|sql/sql_class.cc|THD::set_sent_row_count|select_export::~select_export|select_export::~select_export|sp_lex_keeper::~sp_lex_keeper
      ES  11.8   dbg  100426  854cae81f52e477c7777a51db26ba640d8755b81  ASAN|heap-use-after-free|sql/sql_class.cc|THD::set_sent_row_count|select_export::~select_export|select_export::~select_export|sp_lex_keeper::~sp_lex_keeper
      ES  11.8   opt  100426  854cae81f52e477c7777a51db26ba640d8755b81  ASAN|heap-use-after-free|sql/sql_class.cc|THD::set_sent_row_count|select_export::~select_export|select_export::~select_export|sp_lex_keeper::~sp_lex_keeper
      ES  12.3   dbg  220426  613a6253fe9efc12e166f83a97663ba263db8317  ASAN|heap-use-after-free|sql/sql_class.cc|THD::set_sent_row_count|select_export::~select_export|select_export::~select_export|sp_lex_keeper::~sp_lex_keeper
      ES  12.3   opt  220426  613a6253fe9efc12e166f83a97663ba263db8317  ASAN|heap-use-after-free|sql/sql_class.cc|THD::set_sent_row_count|select_export::~select_export|select_export::~select_export|sp_lex_keeper::~sp_lex_keeper
      

      Attachments

        Activity

          People

            shulga Dmitry Shulga
            Roel Roel Van de Paar
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:

              Time Tracking

                Estimated:
                Original Estimate - 9d 6.55h Original Estimate - 9d 6.55h
                9d 6.55h
                Remaining:
                Time Spent - 2.25h Remaining Estimate - 9d 6.55h
                9d 6.55h
                Logged:
                Time Spent - 2.25h Remaining Estimate - 9d 6.55h
                2.25h

                Git Integration

                  Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.