Skip to content

Commit fdc3139

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 33b2642 commit fdc3139

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
@@ -792,13 +792,13 @@ lexescape(struct vars * v)
792792
break;
793793
case CHR('u'):
794794
c = lexdigits(v, 16, 4, 4);
795-
if (ISERR())
795+
if (ISERR() || c < CHR_MIN || c > CHR_MAX)
796796
FAILW(REG_EESCAPE);
797797
RETV(PLAIN, c);
798798
break;
799799
case CHR('U'):
800800
c = lexdigits(v, 16, 8, 8);
801-
if (ISERR())
801+
if (ISERR() || c < CHR_MIN || c > CHR_MAX)
802802
FAILW(REG_EESCAPE);
803803
RETV(PLAIN, c);
804804
break;
@@ -816,7 +816,7 @@ lexescape(struct vars * v)
816816
case CHR('x'):
817817
NOTE(REG_UUNPORT);
818818
c = lexdigits(v, 16, 1, 255); /* REs >255 long outside spec */
819-
if (ISERR())
819+
if (ISERR() || c < CHR_MIN || c > CHR_MAX)
820820
FAILW(REG_EESCAPE);
821821
RETV(PLAIN, c);
822822
break;
@@ -872,6 +872,9 @@ lexescape(struct vars * v)
872872

873873
/*
874874
* lexdigits - slurp up digits and return chr value
875+
*
876+
* This does not account for overflow; callers should range-check the result
877+
* if maxlen is large enough to make that possible.
875878
*/
876879
static chr /* chr value; errors signalled via ERR */
877880
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
@@ -1586,6 +1586,7 @@ dovec(struct vars * v,
15861586
{
15871587
ch = *p;
15881588
newarc(v->nfa, PLAIN, subcolor(v->cm, ch), lp, rp);
1589+
NOERR();
15891590
}
15901591

15911592
/* and the ranges */
@@ -1595,6 +1596,7 @@ dovec(struct vars * v,
15951596
to = *(p + 1);
15961597
if (from <= to)
15971598
subrange(v, from, to, lp, rp);
1599+
NOERR();
15981600
}
15991601
}
16001602

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
@@ -326,3 +326,5 @@ select 'xyz' ~ 'x(\w)(?=\1)'; -- no backrefs in LACONs
326326
ERROR: invalid regular expression: invalid backreference number
327327
select 'xyz' ~ 'x(\w)(?=(\1))';
328328
ERROR: invalid regular expression: invalid backreference number
329+
select 'a' ~ '\x7fffffff'; -- invalid chr code
330+
ERROR: invalid regular expression: invalid escape \ sequence

src/test/regress/sql/regex.sql

+1
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,4 @@ select 'a' ~ '()+\1';
8686
-- Error conditions
8787
select 'xyz' ~ 'x(\w)(?=\1)'; -- no backrefs in LACONs
8888
select 'xyz' ~ 'x(\w)(?=(\1))';
89+
select 'a' ~ '\x7fffffff'; -- invalid chr code

0 commit comments

Comments
 (0)