[MDEV-12420] pcre stack overflow on match Created: 2017-03-31 Updated: 2017-07-20 Due: 2017-05-05 Resolved: 2017-05-16 |
|
| Status: | Closed |
| Project: | MariaDB Server |
| Component/s: | Server |
| Affects Version/s: | 10.0.30, 10.1.22, 10.2.5 |
| Fix Version/s: | 10.1.24, 10.0.31, 10.2.7 |
| Type: | Bug | Priority: | Critical |
| Reporter: | Don Lindsay | Assignee: | Sergei Golubchik |
| Resolution: | Fixed | Votes: | 0 |
| Labels: | contribution, foundation, patch, security | ||
| Environment: |
Fedora 25 x86_64 |
||
| Attachments: |
|
||||||||
| Issue Links: |
|
||||||||
| Description |
|
The attached table-free query causes a seg fault in 10.2.4, 10.1.21, and 10.0.21 (and presumably all the others). Setting max_recursive_iterations to various values didn't turn that into a polite error, but according to the documenation, it should have. NOTE: setting 'thread_stack=800k' fixes this specific test, so it's definitely a stack issue. Examining a dumpfile showed many recursions of pcre 'match'. |
| Comments |
| Comment by Don Lindsay [ 2017-04-08 ] | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Also segfaults MariaDB-server-10.2.5 (regardless of the setting of max_recursive_iterations, ie the same bug) | ||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Daniel Black [ 2017-04-08 ] | ||||||||||||||||||||||||||||||||||||||||||||||||
|
max_recursive_iterations is for CTE - its not a setting that affects regexes Ubuntu-17.04: libpcre3:amd64 2:8.39-3
| ||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Daniel Black [ 2017-04-08 ] | ||||||||||||||||||||||||||||||||||||||||||||||||
|
proof of concept fix in PR with extended warnings 100 for depth?
If we where to make depth dynamic in Item_func_regex::val_int and use (like check_stack_overrun) the concepts below:
656 could be determined programmatic (from pcretest) and perhaps an extra margin. | ||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Daniel Black [ 2017-04-11 ] | ||||||||||||||||||||||||||||||||||||||||||||||||
|
serg do you want to go down the path of dynamically determining the pcre recursion limit based on available stack? | ||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Sergei Golubchik [ 2017-04-17 ] | ||||||||||||||||||||||||||||||||||||||||||||||||
|
We've added stack size check to pcre (first to our copy, then the patch was merged upstream). May be pcre needs to call it in more places? I'll take a look. Dynamically determining the pcre recursion limit is an interesting idea, thanks! I'll give it a try. | ||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Daniel Black [ 2017-04-17 ] | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Had a bit of a look. How far of using pcre2 (version 10.x). Has a more featured api | ||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Daniel Black [ 2017-04-18 ] | ||||||||||||||||||||||||||||||||||||||||||||||||
|
> May be pcre needs to call it in more places? I think you are right. pcre_stack_guard is only called from pcre_compile currently and not from match. There isn't an upstream bug for it. The version 2 api is more explicit calling it pcre2_set_compile_recursion_guard (which has a recursion value as an argument) however there isn't a match callback. Added https://github.com/MariaDB/server/pull/356/commits/91908884347e69fe8cbc8a9a78c0f77bf5b4e1bc, the pcre_exec function wasn't returning the right value (returned ~20) however the non-exposed match function was. So its in an incomplete state now. |