[MDEV-26590] Stack smashing/buffer overflow in Histogram_json_hb::parse upon UPDATE on table with long VARCHAR Created: 2021-09-11  Updated: 2022-01-19  Resolved: 2021-09-20

Status: Closed
Project: MariaDB Server
Component/s: Optimizer
Affects Version/s: N/A
Fix Version/s: 10.7.0

Type: Bug Priority: Critical
Reporter: Elena Stepanova Assignee: Sergei Petrunia
Resolution: Fixed Votes: 0
Labels: None

Issue Links:
Relates
relates to MDEV-21130 Histograms: use JSON as on-disk format Closed

 Description   

CREATE TABLE t1 (b INT, a VARCHAR(3176));
INSERT INTO t1 VALUES (1,'foo'),(2,'bar');
SET histogram_type= JSON_HB;
ANALYZE TABLE t1 PERSISTENT FOR ALL;
UPDATE t1 SET b = 3;
 
# Cleanup
DROP TABLE t1;

preview-10.7-MDEV-21130-json-histograms 8fc778f3e9c

*** stack smashing detected ***: terminated
210911 16:42:30 [ERROR] mysqld got signal 6 ;
 
#9  0x0000564274667184 in Histogram_json_hb::parse (this=0x7f746424b9c0, mem_root=0x7f746419efa8, field=0x7f74641a1540, type_arg=JSON_HB, hist_data=0x7f746424c708 "{\n  \"histogram_hb_v1\": [\n    \"bar\",\n    \"bar\",\n    \"bar\",\n    \"bar\",\n    \"bar\",\n    \"bar\",\n    \"bar\",\n    \"bar\",\n    \"bar\",\n    \"bar\",\n    \"bar\",\n    \"bar\",\n    \"bar\",\n    \"bar\",\n    \"bar\",\n    \"bar\","..., hist_data_len=2834) at /data/src/preview-10.7-MDEV-21130-json-histograms/sql/opt_histogram_json.cc:219
#10 0x00005642744252f8 in Column_stat::load_histogram (this=0x7f74763d3000, mem_root=0x7f746419efa8) at /data/src/preview-10.7-MDEV-21130-json-histograms/sql/sql_statistics.cc:1235
#11 0x000056427441b42b in read_histograms_for_table (thd=0x7f7464000db8, table=0x7f7464080338, stat_tables=0x7f74763d3100) at /data/src/preview-10.7-MDEV-21130-json-histograms/sql/sql_statistics.cc:3168
#12 0x000056427441bfa2 in read_statistics_for_tables (thd=0x7f7464000db8, tables=0x7f7464015360) at /data/src/preview-10.7-MDEV-21130-json-histograms/sql/sql_statistics.cc:3305
#13 0x000056427441b63c in read_statistics_for_tables_if_needed (thd=0x7f7464000db8, tables=0x7f7464015360) at /data/src/preview-10.7-MDEV-21130-json-histograms/sql/sql_statistics.cc:3214
#14 0x00005642744bdb3a in mysql_update (thd=0x7f7464000db8, table_list=0x7f7464015360, fields=..., values=..., conds=0x0, order_num=0, order=0x0, limit=18446744073709551615, ignore=false, found_return=0x7f74763d5f10, updated_return=0x7f74763d6000) at /data/src/preview-10.7-MDEV-21130-json-histograms/sql/sql_update.cc:444
#15 0x0000564274252f22 in mysql_execute_command (thd=0x7f7464000db8, is_called_from_prepared_stmt=false) at /data/src/preview-10.7-MDEV-21130-json-histograms/sql/sql_parse.cc:4408
#16 0x000056427426c86c in mysql_parse (thd=0x7f7464000db8, rawbuf=0x7f7464015290 "UPDATE t1 SET b = 3", length=19, parser_state=0x7f74763d6480) at /data/src/preview-10.7-MDEV-21130-json-histograms/sql/sql_parse.cc:8030
#17 0x0000564274242b0c in dispatch_command (command=COM_QUERY, thd=0x7f7464000db8, packet=0x7f746400b849 "UPDATE t1 SET b = 3", packet_length=19, blocking=true) at /data/src/preview-10.7-MDEV-21130-json-histograms/sql/sql_parse.cc:1896
#18 0x000056427423fba2 in do_command (thd=0x7f7464000db8, blocking=true) at /data/src/preview-10.7-MDEV-21130-json-histograms/sql/sql_parse.cc:1404
#19 0x00005642745a9493 in do_handle_one_connection (connect=0x564279c3a8c8, put_in_cache=true) at /data/src/preview-10.7-MDEV-21130-json-histograms/sql/sql_connect.cc:1418
#20 0x00005642745a8c5b in handle_one_connection (arg=0x564279c391f8) at /data/src/preview-10.7-MDEV-21130-json-histograms/sql/sql_connect.cc:1312
#21 0x0000564274e8aa83 in pfs_spawn_thread (arg=0x564279c3a418) at /data/src/preview-10.7-MDEV-21130-json-histograms/storage/perfschema/pfs.cc:2201
#22 0x00007f747c093609 in start_thread (arg=<optimized out>) at pthread_create.c:477
#23 0x00007f747bc66293 in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95

or same revision built as non-debug with ASAN,

==1456272==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7f64b3a29b30 at pc 0x7f64bd6d2f2d bp 0x7f64b3a28b00 sp 0x7f64b3a282a8
WRITE of size 3173 at 0x7f64b3a29b30 thread T5
    #0 0x7f64bd6d2f2c  (/lib/x86_64-linux-gnu/libasan.so.5+0x67f2c)
    #1 0x557098272bfb in Field_varstring::get_key_image(unsigned char*, unsigned int, unsigned char const*, Field::imagetype) const /data/src/preview-10.7-MDEV-21130-json-histograms-rel-asan/sql/field.cc:8213
    #2 0x557097fdbe77 in Field::get_key_image(unsigned char*, unsigned int, Field::imagetype) const /data/src/preview-10.7-MDEV-21130-json-histograms-rel-asan/sql/field.h:1550
    #3 0x557097fdbe77 in Histogram_json_hb::parse(st_mem_root*, Field*, enum_histogram_type, char const*, unsigned long) /data/src/preview-10.7-MDEV-21130-json-histograms-rel-asan/sql/opt_histogram_json.cc:201
    #4 0x557097d83e1d in Column_stat::load_histogram(st_mem_root*) /data/src/preview-10.7-MDEV-21130-json-histograms-rel-asan/sql/sql_statistics.cc:1235
    #5 0x557097d83e1d in read_histograms_for_table /data/src/preview-10.7-MDEV-21130-json-histograms-rel-asan/sql/sql_statistics.cc:3168
    #6 0x557097d83e1d in read_statistics_for_tables(THD*, TABLE_LIST*) /data/src/preview-10.7-MDEV-21130-json-histograms-rel-asan/sql/sql_statistics.cc:3305
    #7 0x557097e50891 in mysql_update(THD*, TABLE_LIST*, List<Item>&, List<Item>&, Item*, unsigned int, st_order*, unsigned long long, bool, unsigned long long*, unsigned long long*) /data/src/preview-10.7-MDEV-21130-json-histograms-rel-asan/sql/sql_update.cc:444
    #8 0x557097b5443e in mysql_execute_command(THD*, bool) /data/src/preview-10.7-MDEV-21130-json-histograms-rel-asan/sql/sql_parse.cc:4408
    #9 0x557097b16e83 in mysql_parse(THD*, char*, unsigned int, Parser_state*) /data/src/preview-10.7-MDEV-21130-json-histograms-rel-asan/sql/sql_parse.cc:8030
    #10 0x557097b431d7 in dispatch_command(enum_server_command, THD*, char*, unsigned int, bool) /data/src/preview-10.7-MDEV-21130-json-histograms-rel-asan/sql/sql_parse.cc:1896
    #11 0x557097b49636 in do_command(THD*, bool) /data/src/preview-10.7-MDEV-21130-json-histograms-rel-asan/sql/sql_parse.cc:1404
    #12 0x557097f379bb in do_handle_one_connection(CONNECT*, bool) /data/src/preview-10.7-MDEV-21130-json-histograms-rel-asan/sql/sql_connect.cc:1418
    #13 0x557097f38224 in handle_one_connection /data/src/preview-10.7-MDEV-21130-json-histograms-rel-asan/sql/sql_connect.cc:1312
    #14 0x557098a2c1bf in pfs_spawn_thread /data/src/preview-10.7-MDEV-21130-json-histograms-rel-asan/storage/perfschema/pfs.cc:2201
    #15 0x7f64bd08f608 in start_thread /build/glibc-eX1tMB/glibc-2.31/nptl/pthread_create.c:477
    #16 0x7f64bcc62292 in __clone (/lib/x86_64-linux-gnu/libc.so.6+0x122292)
 
Address 0x7f64b3a29b30 is located in stack of thread T5 at offset 3744 in frame
    #0 0x557097fdb7cf in Histogram_json_hb::parse(st_mem_root*, Field*, enum_histogram_type, char const*, unsigned long) /data/src/preview-10.7-MDEV-21130-json-histograms-rel-asan/sql/opt_histogram_json.cc:145
 
  This frame has 9 object(s):
    [32, 33) '<unknown>'
    [48, 49) '<unknown>'
    [64, 65) '<unknown>'
    [80, 84) 'val_len' (line 188)
    [96, 104) 'val' (line 187)
    [128, 176) 'key_name' (line 150)
    [208, 240) '<unknown>'
    [272, 504) 'je' (line 149)
    [576, 3744) 'buf' (line 198) <== Memory access at offset 3744 overflows this variable
HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
      (longjmp and C++ exceptions *are* supported)
Thread T5 created by T0 here:
    #0 0x7f64bd6a5805 in pthread_create (/lib/x86_64-linux-gnu/libasan.so.5+0x3a805)
    #1 0x557098a2c45d in my_thread_create /data/src/preview-10.7-MDEV-21130-json-histograms-rel-asan/storage/perfschema/my_thread.h:48
    #2 0x557098a2c45d in pfs_spawn_thread_v1 /data/src/preview-10.7-MDEV-21130-json-histograms-rel-asan/storage/perfschema/pfs.cc:2252
    #3 0x5570977f6570 in inline_mysql_thread_create /data/src/preview-10.7-MDEV-21130-json-histograms-rel-asan/include/mysql/psi/mysql_thread.h:1139
    #4 0x5570977f6570 in create_thread_to_handle_connection(CONNECT*) /data/src/preview-10.7-MDEV-21130-json-histograms-rel-asan/sql/mysqld.cc:5934
    #5 0x5570978027aa in handle_accepted_socket(st_mysql_socket, st_mysql_socket) /data/src/preview-10.7-MDEV-21130-json-histograms-rel-asan/sql/mysqld.cc:6055
    #6 0x55709780337f in handle_connections_sockets() /data/src/preview-10.7-MDEV-21130-json-histograms-rel-asan/sql/mysqld.cc:6179
    #7 0x557097806a88 in mysqld_main(int, char**) /data/src/preview-10.7-MDEV-21130-json-histograms-rel-asan/sql/mysqld.cc:5829
    #8 0x7f64bcb670b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
 
SUMMARY: AddressSanitizer: stack-buffer-overflow (/lib/x86_64-linux-gnu/libasan.so.5+0x67f2c) 
Shadow bytes around the buggy address:
  0x0fed1673d310: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0fed1673d320: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0fed1673d330: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0fed1673d340: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0fed1673d350: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0fed1673d360: 00 00 00 00 00 00[f3]f3 f3 f3 f3 f3 f3 f3 f3 f3
  0x0fed1673d370: f3 f3 f3 f3 f3 f3 00 00 00 00 00 00 00 00 00 00
  0x0fed1673d380: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0fed1673d390: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0fed1673d3a0: 00 00 00 00 00 00 00 00 00 00 f1 f1 f1 f1 f8 f2
  0x0fed1673d3b0: f8 f2 f8 f8 f2 f2 00 00 00 00 f2 f2 f2 f2 f8 f8
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
  Shadow gap:              cc
==1456272==ABORTING
210911 16:38:45 [ERROR] mysqld got signal 6 ;
This could be because you hit a bug. It is also possible that this binary
or one of the libraries it was linked against is corrupt, improperly built,
or misconfigured. This error can also be caused by malfunctioning hardware.
 
To report this bug, see https://mariadb.com/kb/en/reporting-bugs
 
We will try our best to scrape up some info that will hopefully help
diagnose the problem, but since we have already crashed, 
something is definitely wrong and this may fail.
 
Server version: 10.7.0-MariaDB-log
key_buffer_size=1048576
read_buffer_size=131072
max_used_connections=1
max_threads=153
thread_count=1
It is possible that mysqld could use up to 
key_buffer_size + (read_buffer_size + sort_buffer_size)*max_threads = 63732 K  bytes of memory
Hope that's ok; if not, decrease some variables in the equation.
 
Thread pointer: 0x62b00007e218
Attempting backtrace. You can use the following information to find out
where mysqld died. If you see no messages after this, something went
terribly wrong...
stack_bottom = 0x7f64b3a328d0 thread_stack 0x5fc00
??:0(__interceptor_tcgetattr)[0x7f64bd6d7d30]
mysys/stacktrace.c:213(my_print_stacktrace)[0x557099405756]
sql/signal_handler.cc:222(handle_fatal_signal)[0x5570982f6bd4]
sigaction.c:0(__restore_rt)[0x7f64bd09b3c0]
??:0(gsignal)[0x7f64bcb8618b]
??:0(abort)[0x7f64bcb65859]
??:0(__sanitizer_set_report_fd)[0x7f64bd7966a2]
??:0(__sanitizer_get_module_and_offset_for_pc)[0x7f64bd7a124c]
??:0(__sanitizer_ptr_cmp)[0x7f64bd7828ec]
??:0(__asan_on_error)[0x7f64bd782363]
??:0(strlen)[0x7f64bd6d2f4f]
sql/field.cc:8213(Field_varstring::get_key_image(unsigned char*, unsigned int, unsigned char const*, Field::imagetype) const)[0x557098272bfc]
sql/opt_histogram_json.cc:202(Histogram_json_hb::parse(st_mem_root*, Field*, enum_histogram_type, char const*, unsigned long))[0x557097fdbe78]
sql/sql_statistics.cc:1235(Column_stat::load_histogram(st_mem_root*))[0x557097d83e1e]
sql/sql_update.cc:446(mysql_update(THD*, TABLE_LIST*, List<Item>&, List<Item>&, Item*, unsigned int, st_order*, unsigned long long, bool, unsigned long long*, unsigned long long*))[0x557097e50892]
sql/sql_parse.cc:4408(mysql_execute_command(THD*, bool))[0x557097b5443f]
sql/sql_parse.cc:8047(mysql_parse(THD*, char*, unsigned int, Parser_state*))[0x557097b16e84]
sql/sql_parse.cc:1898(dispatch_command(enum_server_command, THD*, char*, unsigned int, bool))[0x557097b431d8]
sql/sql_parse.cc:1406(do_command(THD*, bool))[0x557097b49637]
sql/sql_connect.cc:1418(do_handle_one_connection(CONNECT*, bool))[0x557097f379bc]
sql/sql_connect.cc:1312(handle_one_connection)[0x557097f38225]
perfschema/pfs.cc:2204(pfs_spawn_thread)[0x557098a2c1c0]
nptl/pthread_create.c:478(start_thread)[0x7f64bd08f609]
??:0(clone)[0x7f64bcc62293]
 
Trying to get some variables.
Some pointers may be invalid and cause the dump to abort.
Query (0x6290000aa238): UPDATE t1 SET b = 3
 
Connection ID (thread ID): 4
Status: NOT_KILLED
 
Optimizer switch: index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=on,table_elimination=on,extended_keys=on,exists_to_in=on,orderby_uses_equalities=on,condition_pushdown_for_derived=on,split_materialized=on,condition_pushdown_for_subquery=on,rowid_filter=on,condition_pushdown_from_having=on,not_null_range_scan=off
 
The manual page at https://mariadb.com/kb/en/how-to-produce-a-full-stack-trace-for-mysqld/ contains
information that should help you find out what is causing the crash.
Writing a core file...
Working directory at /dev/shm/var_auto_8YK7/mysqld.1/data
Resource Limits:
Limit                     Soft Limit           Hard Limit           Units     
Max cpu time              unlimited            unlimited            seconds   
Max file size             unlimited            unlimited            bytes     
Max data size             unlimited            unlimited            bytes     
Max stack size            8388608              unlimited            bytes     
Max core file size        unlimited            unlimited            bytes     
Max resident set          unlimited            unlimited            bytes     
Max processes             385736               385736               processes 
Max open files            1024                 1024                 files     
Max locked memory         67108864             67108864             bytes     
Max address space         unlimited            unlimited            bytes     
Max file locks            unlimited            unlimited            locks     
Max pending signals       385736               385736               signals   
Max msgqueue size         819200               819200               bytes     
Max nice priority         0                    0                    
Max realtime priority     0                    0                    
Max realtime timeout      unlimited            unlimited            us        
Core pattern: |/usr/share/apport/apport %p %s %c %d %P %E

Reproducible with at least InnoDB and MyISAM.



 Comments   
Comment by Sergei Petrunia [ 2021-09-12 ]

Reproducible on bb-10.7-mdev26519 branch, revision 0fe2cfd45e616e93d47aa9e224db468f32dd391a.

The reason:

      uchar buf[MAX_KEY_LENGTH];
3     uint len_to_copy= field->key_length();
      field->store_text(val, val_len, &my_charset_bin);
=>    uint bytes= field->get_key_image(buf, len_to_copy, Field::itRAW);  

len_to_copy=3176, while MAX_KEY_LENGTH=3168!

Comment by Sergei Petrunia [ 2021-09-13 ]

Fixed in bb-10.7-mdev26519 tree

Comment by Sergei Petrunia [ 2021-09-20 ]

Closing as it is fixed in preview-10.7-MDEV-26519-improved-json-histograms tree

Generated at Thu Feb 08 09:46:27 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.