Skip to content

Commit 3bb3f42

Browse files
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 a bad choice because it is outside the range of type "celt" (int32). Characters approaching that limit could lead to infinite loops in logic such as "for (c = a; c <= b; c++)" where c is of type celt but the range bounds are chr. Such loops will work safely only if CHR_MAX+1 is representable in celt, since c must advance to beyond b before the loop 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, and none of our other backend encodings need characters beyond that either. In addition to modifying the macro, we have to explicitly enforce character range restrictions on the values of \u, \U, and \x escape sequences, else the limit is trivially bypassed. Also, the code for expanding case-independent character ranges in bracket expressions had a potential integer overflow in its calculation of the number of characters it could generate, which could lead to allocating too small a character vector and then overwriting memory. An attacker with the ability to supply arbitrary regex patterns could easily cause transient DOS via server crashes, and the possibility for privilege escalation has not been ruled out. Quite aside from the integer-overflow problem, the range expansion code was unnecessarily inefficient in that it always produced a result consisting of individual characters, abandoning the knowledge that we had a range to start 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 on any case-equivalent characters that are outside that range. With this approach, we can bound the number of individual characters allowed without sacrificing much. This patch allows at most 100000 individual characters, which I believe to be more than the number of case pairs existing in Unicode, so that the restriction will never be hit in practice. It's still possible for range() to take awhile given a large character code range, so also add statement-cancel detection to its loop. The downstream function dovec() also lacked cancel detection, and could take a long time given a large output from range(). Per fuzz testing by Greg Stark. Back-patch to all supported branches. Security: CVE-2016-0773
1 parent f8a1c1d commit 3bb3f42

File tree

6 files changed

+53
-18
lines changed

6 files changed

+53
-18
lines changed

src/backend/regex/regc_lex.c

+6-3
Original file line numberDiff line numberDiff line change
@@ -813,13 +813,13 @@ lexescape(struct vars * v)
813813
break;
814814
case CHR('u'):
815815
c = lexdigits(v, 16, 4, 4);
816-
if (ISERR())
816+
if (ISERR() || c < CHR_MIN || c > CHR_MAX)
817817
FAILW(REG_EESCAPE);
818818
RETV(PLAIN, c);
819819
break;
820820
case CHR('U'):
821821
c = lexdigits(v, 16, 8, 8);
822-
if (ISERR())
822+
if (ISERR() || c < CHR_MIN || c > CHR_MAX)
823823
FAILW(REG_EESCAPE);
824824
RETV(PLAIN, c);
825825
break;
@@ -837,7 +837,7 @@ lexescape(struct vars * v)
837837
case CHR('x'):
838838
NOTE(REG_UUNPORT);
839839
c = lexdigits(v, 16, 1, 255); /* REs >255 long outside spec */
840-
if (ISERR())
840+
if (ISERR() || c < CHR_MIN || c > CHR_MAX)
841841
FAILW(REG_EESCAPE);
842842
RETV(PLAIN, c);
843843
break;
@@ -899,6 +899,9 @@ lexescape(struct vars * v)
899899

900900
/*
901901
* lexdigits - slurp up digits and return chr value
902+
*
903+
* This does not account for overflow; callers should range-check the result
904+
* if maxlen is large enough to make that possible.
902905
*/
903906
static chr /* chr value; errors signalled via ERR */
904907
lexdigits(struct vars * v,

src/backend/regex/regc_locale.c

+40-14
Original file line numberDiff line numberDiff line change
@@ -408,8 +408,7 @@ range(struct vars * v, /* context */
408408
int nchrs;
409409
struct cvec *cv;
410410
celt c,
411-
lc,
412-
uc;
411+
cc;
413412

414413
if (a != b && !before(a, b))
415414
{
@@ -427,24 +426,51 @@ range(struct vars * v, /* context */
427426

428427
/*
429428
* When case-independent, it's hard to decide when cvec ranges are usable,
430-
* so for now at least, we won't try. We allocate enough space for two
431-
* case variants plus a little extra for the two title case variants.
429+
* so for now at least, we won't try. We use a range for the originally
430+
* specified chrs and then add on any case-equivalents that are outside
431+
* that range as individual chrs.
432+
*
433+
* To ensure sane behavior if someone specifies a very large range, limit
434+
* the allocation size to 100000 chrs (arbitrary) and check for overrun
435+
* inside the loop below.
432436
*/
437+
nchrs = b - a + 1;
438+
if (nchrs <= 0 || nchrs > 100000)
439+
nchrs = 100000;
433440

434-
nchrs = (b - a + 1) * 2 + 4;
435-
436-
cv = getcvec(v, nchrs, 0);
441+
cv = getcvec(v, nchrs, 1);
437442
NOERRN();
443+
addrange(cv, a, b);
438444

439445
for (c = a; c <= b; c++)
440446
{
441-
addchr(cv, c);
442-
lc = pg_wc_tolower((chr) c);
443-
if (c != lc)
444-
addchr(cv, lc);
445-
uc = pg_wc_toupper((chr) c);
446-
if (c != uc)
447-
addchr(cv, uc);
447+
cc = pg_wc_tolower((chr) c);
448+
if (cc != c &&
449+
(before(cc, a) || before(b, cc)))
450+
{
451+
if (cv->nchrs >= cv->chrspace)
452+
{
453+
ERR(REG_ETOOBIG);
454+
return NULL;
455+
}
456+
addchr(cv, cc);
457+
}
458+
cc = pg_wc_toupper((chr) c);
459+
if (cc != c &&
460+
(before(cc, a) || before(b, cc)))
461+
{
462+
if (cv->nchrs >= cv->chrspace)
463+
{
464+
ERR(REG_ETOOBIG);
465+
return NULL;
466+
}
467+
addchr(cv, cc);
468+
}
469+
if (CANCEL_REQUESTED(v->re))
470+
{
471+
ERR(REG_CANCEL);
472+
return NULL;
473+
}
448474
}
449475

450476
return cv;

src/backend/regex/regcomp.c

+2
Original file line numberDiff line numberDiff line change
@@ -1593,6 +1593,7 @@ dovec(struct vars * v,
15931593
{
15941594
ch = *p;
15951595
newarc(v->nfa, PLAIN, subcolor(v->cm, ch), lp, rp);
1596+
NOERR();
15961597
}
15971598

15981599
/* and the ranges */
@@ -1602,6 +1603,7 @@ dovec(struct vars * v,
16021603
to = *(p + 1);
16031604
if (from <= to)
16041605
subrange(v, from, to, lp, rp);
1606+
NOERR();
16051607
}
16061608
}
16071609

src/include/regex/regcustom.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ typedef int celt; /* type to hold chr, or NOCELT */
6565
#define DIGITVAL(c) ((c)-'0') /* turn chr digit into its value */
6666
#define CHRBITS 32 /* bits in a chr; must not use sizeof */
6767
#define CHR_MIN 0x00000000 /* smallest and largest chr; the value */
68-
#define CHR_MAX 0xfffffffe /* CHR_MAX-CHR_MIN+1 should fit in uchr */
68+
#define CHR_MAX 0x7ffffffe /* CHR_MAX-CHR_MIN+1 must fit in an int, and
69+
* CHR_MAX+1 must fit in both chr and celt */
6970

7071
/* functions operating on chr */
7172
#define iscalnum(x) pg_wc_isalnum(x)

src/test/regress/expected/regex.out

+2
Original file line numberDiff line numberDiff line change
@@ -495,3 +495,5 @@ select 'xyz' ~ 'x(\w)(?=\1)'; -- no backrefs in LACONs
495495
ERROR: invalid regular expression: invalid backreference number
496496
select 'xyz' ~ 'x(\w)(?=(\1))';
497497
ERROR: invalid regular expression: invalid backreference number
498+
select 'a' ~ '\x7fffffff'; -- invalid chr code
499+
ERROR: invalid regular expression: invalid escape \ sequence

src/test/regress/sql/regex.sql

+1
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,4 @@ select 'a' ~ '()+\1';
121121
-- Error conditions
122122
select 'xyz' ~ 'x(\w)(?=\1)'; -- no backrefs in LACONs
123123
select 'xyz' ~ 'x(\w)(?=(\1))';
124+
select 'a' ~ '\x7fffffff'; -- invalid chr code

0 commit comments

Comments
 (0)