File: 31regex_heap_overflow.patch

package info (click to toggle)
nvi 1.81.6-17
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 12,400 kB
  • sloc: ansic: 43,645; sh: 11,207; makefile: 603; perl: 262; tcl: 234; awk: 19
file content (45 lines) | stat: -rw-r--r-- 1,476 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
Description: Disallow an excessively large pattern space
 Apply heap overflow vulnerability patch to bundled regex library.
 Unfortunately nvi uses the BSD-specific REG_NOSPEC flag, so we can't easily
 switch to the system library.
Origin: vendor, https://gitweb.dragonflybsd.org/dragonfly.git/commitdiff/2841837793bd095a82f477e9c370cfe6cfb3862c
Bug-Debian: https://bugs.debian.org/778412
Forwarded: no
Last-Update: 2016-12-31

Index: b/regex/regcomp.c
===================================================================
--- a/regex/regcomp.c
+++ b/regex/regcomp.c
@@ -183,6 +183,7 @@
 	register struct parse *p = &pa;
 	register int i;
 	register size_t len;
+	size_t maxlen;
 #ifdef REDEBUG
 #	define	GOODFLAGS(f)	(f)
 #else
@@ -205,7 +206,23 @@
 							(NC-1)*sizeof(cat_t));
 	if (g == NULL)
 		return(REG_ESPACE);
+	/*
+	 * Limit the pattern space to avoid a 32-bit overflow on buffer
+	 * extension.  Also avoid any signed overflow in case of conversion
+	 * so make the real limit based on a 31-bit overflow.
+	 *
+	 * Likely not applicable on 64-bit systems but handle the case
+	 * generically (who are we to stop people from using ~715MB+
+	 * patterns?).
+	 */
+	maxlen = ((size_t)-1 >> 1) / sizeof(sop) * 2 / 3;
+	if (len >= maxlen) {
+		free((char *)g);
+		return(REG_ESPACE);
+	}
 	p->ssize = len/(size_t)2*(size_t)3 + (size_t)1;	/* ugh */
+	assert(p->ssize >= len);
+
 	p->strip = (sop *)malloc(p->ssize * sizeof(sop));
 	p->slen = 0;
 	if (p->strip == NULL) {