Skip to content

Commit 52aa200

Browse files
committed
MDEV-12420 max_recursive_iterations did not prevent a stack-overflow and segfault
post-review fixes * move pcre-specific variable out of mysys * don't use current_thd * move a commonly used macro to my_sys.h * remove new sysvar
1 parent 602b5e4 commit 52aa200

File tree

10 files changed

+24
-46
lines changed

10 files changed

+24
-46
lines changed

include/my_sys.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,6 @@ extern void (*fatal_error_handler_hook)(uint my_err, const char *str,
227227
myf MyFlags);
228228
extern uint my_file_limit;
229229
extern ulonglong my_thread_stack_size;
230-
#ifndef EMBEDDED_LIBRARY
231-
extern ulonglong my_pcre_frame_size;
232-
#endif
233230
extern int sf_leaking_memory; /* set to 1 to disable memleak detection */
234231

235232
extern void (*proc_info_hook)(void *, const PSI_stage_info *, PSI_stage_info *,
@@ -909,6 +906,12 @@ extern ulonglong my_getcputime(void);
909906
#define hrtime_sec_part(X) ((ulong)((X).val % HRTIME_RESOLUTION))
910907
#define my_time(X) hrtime_to_time(my_hrtime())
911908

909+
#if STACK_DIRECTION < 0
910+
#define available_stack_size(CUR,END) (long) ((char*)(CUR) - (char*)(END))
911+
#else
912+
#define available_stack_size(CUR,END) (long) ((char*)(END) - (char*)(CUR))
913+
#endif
914+
912915
#ifdef HAVE_SYS_MMAN_H
913916
#include <sys/mman.h>
914917

mysys/lf_alloc-pin.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -328,12 +328,6 @@ static int match_pins(LF_PINS *el, void *addr)
328328
return 0;
329329
}
330330

331-
#if STACK_DIRECTION < 0
332-
#define available_stack_size(CUR,END) (long) ((char*)(CUR) - (char*)(END))
333-
#else
334-
#define available_stack_size(CUR,END) (long) ((char*)(END) - (char*)(CUR))
335-
#endif
336-
337331
#define next_node(P, X) (*((uchar * volatile *)(((uchar *)(X)) + (P)->free_ptr_offset)))
338332
#define anext_node(X) next_node(&allocator->pinbox, (X))
339333

mysys/my_init.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ my_bool my_init_done= 0;
4545
uintmysys_usage_id= 0; /* Incremented for each my_init() */
4646

4747
ulonglong my_thread_stack_size= (sizeof(void*) <= 4)? 65536: ((256-16)*1024);
48-
/* http://pcre.org/original/doc/html/pcrestack.html - replaced by init_pcre value */
49-
ulonglong my_pcre_frame_size= 640 + 16;
5048

5149
static ulong atoi_octal(const char *str)
5250
{

sql/item_cmpfunc.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5104,6 +5104,15 @@ int Regexp_processor_pcre::default_regex_flags()
51045104
return default_regex_flags_pcre(current_thd);
51055105
}
51065106

5107+
void Regexp_processor_pcre::set_recursion_limit(THD *thd)
5108+
{
5109+
long stack_used;
5110+
DBUG_ASSERT(thd == current_thd);
5111+
stack_used= available_stack_size(thd->thread_stack, &stack_used);
5112+
m_pcre_extra.match_limit_recursion=
5113+
(my_thread_stack_size - stack_used)/my_pcre_frame_size;
5114+
}
5115+
51075116

51085117
/**
51095118
Convert string to lib_charset, if needed.

sql/item_cmpfunc.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
#include "thr_malloc.h" /* sql_calloc */
2727
#include "item_func.h" /* Item_int_func, Item_bool_func */
28-
long check_stack_available(long margin, uchar *dummy);
2928
#define PCRE_STATIC 1 /* Important on Windows */
3029
#include "pcre.h" /* pcre header file */
3130

@@ -1577,15 +1576,11 @@ class Regexp_processor_pcre
15771576
m_library_charset(&my_charset_utf8_general_ci),
15781577
m_subpatterns_needed(0)
15791578
{
1580-
#ifndef EMBEDDED_LIBRARY
1581-
uchar dummy;
1582-
m_pcre_extra.flags= PCRE_EXTRA_MATCH_LIMIT_RECURSION;
1583-
m_pcre_extra.match_limit_recursion= check_stack_available(100, &dummy) / my_pcre_frame_size;
1584-
#else
1585-
m_pcre_extra.flags= 0L;
1586-
#endif
1579+
m_pcre_extra.flags= PCRE_EXTRA_MATCH_LIMIT_RECURSION;
1580+
m_pcre_extra.match_limit_recursion= 100L;
15871581
}
15881582
int default_regex_flags();
1583+
void set_recursion_limit(THD *);
15891584
void init(CHARSET_INFO *data_charset, int extra_flags, uint nsubpatterns)
15901585
{
15911586
m_library_flags= default_regex_flags() | extra_flags |

sql/mysqld.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3497,16 +3497,16 @@ static void init_libstrings()
34973497
#endif
34983498
}
34993499

3500+
ulonglong my_pcre_frame_size;
35003501

35013502
static void init_pcre()
35023503
{
35033504
pcre_malloc= pcre_stack_malloc= my_str_malloc_mysqld;
35043505
pcre_free= pcre_stack_free= my_str_free_mysqld;
35053506
#ifndef EMBEDDED_LIBRARY
35063507
pcre_stack_guard= check_enough_stack_size_slow;
3507-
/* my_pcre_frame_size= -pcre_exec(NULL, NULL, NULL, -999, -999, 0, NULL, 0) + 16;
3508-
http://pcre.org/original/doc/html/pcrestack.html has reason for + 16
3509-
my_pcre_frame_size= -pcre_match(NULL, NULL, NULL, 0, NULL, NULL, 0) + 16; */
3508+
/* See http://pcre.org/original/doc/html/pcrestack.html */
3509+
my_pcre_frame_size= -pcre_exec(NULL, NULL, NULL, -999, -999, 0, NULL, 0) + 16;
35103510
#endif
35113511
}
35123512

sql/mysqld.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,8 @@ extern pthread_t signal_thread;
488488
extern struct st_VioSSLFd * ssl_acceptor_fd;
489489
#endif /* HAVE_OPENSSL */
490490

491+
extern ulonglong my_pcre_frame_size;
492+
491493
/*
492494
The following variables were under INNODB_COMPABILITY_HOOKS
493495
*/

sql/sql_parse.cc

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6163,12 +6163,6 @@ bool check_fk_parent_table_access(THD *thd,
61636163
****************************************************************************/
61646164

61656165

6166-
#if STACK_DIRECTION < 0
6167-
#define used_stack(A,B) (long) (A - B)
6168-
#else
6169-
#define used_stack(A,B) (long) (B - A)
6170-
#endif
6171-
61726166
#ifndef DBUG_OFF
61736167
long max_stack_used;
61746168
#endif
@@ -6185,7 +6179,7 @@ bool check_stack_overrun(THD *thd, long margin,
61856179
{
61866180
long stack_used;
61876181
DBUG_ASSERT(thd == current_thd);
6188-
if ((stack_used=used_stack(thd->thread_stack,(char*) &stack_used)) >=
6182+
if ((stack_used= available_stack_size(thd->thread_stack, &stack_used)) >=
61896183
(long) (my_thread_stack_size - margin))
61906184
{
61916185
thd->is_fatal_error= 1;
@@ -6208,14 +6202,6 @@ bool check_stack_overrun(THD *thd, long margin,
62086202
return 0;
62096203
}
62106204

6211-
long check_stack_available(long margin,
6212-
uchar *buf __attribute__((unused)))
6213-
{
6214-
long stack_top;
6215-
DBUG_ASSERT(current_thd);
6216-
return my_thread_stack_size - margin \
6217-
- used_stack(current_thd->thread_stack,(char*) &stack_top);
6218-
}
62196205

62206206
#define MY_YACC_INIT 1000// Start with big alloc
62216207
#define MY_YACC_MAX 32000// Because of 'short'

sql/sql_parse.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ bool check_simple_select();
134134
Item *normalize_cond(Item *cond);
135135
Item *negate_expression(THD *thd, Item *expr);
136136
bool check_stack_overrun(THD *thd, long margin, uchar *dummy);
137-
long check_stack_available(long margin, uchar *dummy);
138137

139138
/* Variables */
140139

sql/sys_vars.cc

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2493,14 +2493,6 @@ static Sys_var_ulonglong Sys_thread_stack(
24932493
VALID_RANGE(128*1024, ULONGLONG_MAX), DEFAULT(DEFAULT_THREAD_STACK),
24942494
BLOCK_SIZE(1024));
24952495

2496-
#ifndef EMBEDDED_LIBRARY
2497-
static Sys_var_ulonglong Sys_my_pcre_frame_size(
2498-
"pcre_frame_size", "Frame size for pcre_recursion",
2499-
GLOBAL_VAR(my_pcre_frame_size), NO_CMD_LINE,
2500-
VALID_RANGE(500,1024), DEFAULT(640 + 16), 1, NO_MUTEX_GUARD,
2501-
NOT_IN_BINLOG, ON_CHECK(0), ON_UPDATE(0));
2502-
#endif
2503-
25042496
static Sys_var_charptr Sys_tmpdir(
25052497
"tmpdir", "Path for temporary files. Several paths may "
25062498
"be specified, separated by a "

0 commit comments

Comments
 (0)