- Notifications
You must be signed in to change notification settings - Fork4.9k
Commitfdc3139
committed
Fix some regex issues with out-of-range characters and large char ranges.
Previously, our regex code defined CHR_MAX as 0xfffffffe, which is abad choice because it is outside the range of type "celt" (int32).Characters approaching that limit could lead to infinite loops in logicsuch as "for (c = a; c <= b; c++)" where c is of type celt but therange bounds are chr. Such loops will work safely only if CHR_MAX+1is representable in celt, since c must advance to beyond b before theloop will exit.Fortunately, there seems no reason not to restrict CHR_MAX to 0x7ffffffe.It's highly unlikely that Unicode will ever assign codes that high, andnone of our other backend encodings need characters beyond that either.In addition to modifying the macro, we have to explicitly enforce characterrange restrictions on the values of \u, \U, and \x escape sequences, elsethe limit is trivially bypassed.Also, the code for expanding case-independent character ranges in bracketexpressions had a potential integer overflow in its calculation of thenumber of characters it could generate, which could lead to allocating toosmall a character vector and then overwriting memory. An attacker with theability to supply arbitrary regex patterns could easily cause transient DOSvia server crashes, and the possibility for privilege escalation has notbeen ruled out.Quite aside from the integer-overflow problem, the range expansion code wasunnecessarily inefficient in that it always produced a result consisting ofindividual characters, abandoning the knowledge that we had a range tostart with. If the input range is large, this requires excessive memory.Change it so that the original range is reported as-is, and then we add onany case-equivalent characters that are outside that range. With thisapproach, we can bound the number of individual characters allowed withoutsacrificing much. This patch allows at most 100000 individual characters,which I believe to be more than the number of case pairs existing inUnicode, so that the restriction will never be hit in practice.It's still possible for range() to take awhile given a large character coderange, so also add statement-cancel detection to its loop. The downstreamfunction dovec() also lacked cancel detection, and could take a long timegiven a large output from range().Per fuzz testing by Greg Stark. Back-patch to all supported branches.Security:CVE-2016-07731 parent33b2642 commitfdc3139
File tree
6 files changed
+53
-18
lines changed- src
- backend/regex
- include/regex
- test/regress
- expected
- sql
6 files changed
+53
-18
lines changedLines changed: 6 additions & 3 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
792 | 792 |
| |
793 | 793 |
| |
794 | 794 |
| |
795 |
| - | |
| 795 | + | |
796 | 796 |
| |
797 | 797 |
| |
798 | 798 |
| |
799 | 799 |
| |
800 | 800 |
| |
801 |
| - | |
| 801 | + | |
802 | 802 |
| |
803 | 803 |
| |
804 | 804 |
| |
| |||
816 | 816 |
| |
817 | 817 |
| |
818 | 818 |
| |
819 |
| - | |
| 819 | + | |
820 | 820 |
| |
821 | 821 |
| |
822 | 822 |
| |
| |||
872 | 872 |
| |
873 | 873 |
| |
874 | 874 |
| |
| 875 | + | |
| 876 | + | |
| 877 | + | |
875 | 878 |
| |
876 | 879 |
| |
877 | 880 |
| |
|
Lines changed: 40 additions & 14 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
408 | 408 |
| |
409 | 409 |
| |
410 | 410 |
| |
411 |
| - | |
412 |
| - | |
| 411 | + | |
413 | 412 |
| |
414 | 413 |
| |
415 | 414 |
| |
| |||
427 | 426 |
| |
428 | 427 |
| |
429 | 428 |
| |
430 |
| - | |
431 |
| - | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
432 | 436 |
| |
| 437 | + | |
| 438 | + | |
| 439 | + | |
433 | 440 |
| |
434 |
| - | |
435 |
| - | |
436 |
| - | |
| 441 | + | |
437 | 442 |
| |
| 443 | + | |
438 | 444 |
| |
439 | 445 |
| |
440 | 446 |
| |
441 |
| - | |
442 |
| - | |
443 |
| - | |
444 |
| - | |
445 |
| - | |
446 |
| - | |
447 |
| - | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
448 | 474 |
| |
449 | 475 |
| |
450 | 476 |
| |
|
Lines changed: 2 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
1586 | 1586 |
| |
1587 | 1587 |
| |
1588 | 1588 |
| |
| 1589 | + | |
1589 | 1590 |
| |
1590 | 1591 |
| |
1591 | 1592 |
| |
| |||
1595 | 1596 |
| |
1596 | 1597 |
| |
1597 | 1598 |
| |
| 1599 | + | |
1598 | 1600 |
| |
1599 | 1601 |
| |
1600 | 1602 |
| |
|
Lines changed: 2 additions & 1 deletion
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
65 | 65 |
| |
66 | 66 |
| |
67 | 67 |
| |
68 |
| - | |
| 68 | + | |
| 69 | + | |
69 | 70 |
| |
70 | 71 |
| |
71 | 72 |
| |
|
Lines changed: 2 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
326 | 326 |
| |
327 | 327 |
| |
328 | 328 |
| |
| 329 | + | |
| 330 | + |
Lines changed: 1 addition & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
86 | 86 |
| |
87 | 87 |
| |
88 | 88 |
| |
| 89 | + |
0 commit comments
Comments
(0)