View | Details | Raw Unified | Return to bug 127172
Collapse All | Expand All

(-)python25/Makefile (-1 / +1 lines)
Lines 6-12 Link Here
6
6
7
PORTNAME=	python25
7
PORTNAME=	python25
8
PORTVERSION=	2.5.2
8
PORTVERSION=	2.5.2
9
PORTREVISION=	2
9
PORTREVISION=	3
10
CATEGORIES=	lang python ipv6
10
CATEGORIES=	lang python ipv6
11
MASTER_SITES=	${PYTHON_MASTER_SITES}
11
MASTER_SITES=	${PYTHON_MASTER_SITES}
12
MASTER_SITE_SUBDIR=	${PYTHON_MASTER_SITE_SUBDIR}
12
MASTER_SITE_SUBDIR=	${PYTHON_MASTER_SITE_SUBDIR}
(-)python25/files/patch-at (+15 lines)
Line 0 Link Here
1
2
3
Part of the fix for CVE-2008-2315 taken from Gentoo.
4
5
--- Modules/mmapmodule.c.orig	2006-08-22 14:57:07.000000000 +0100
6
+++ Modules/mmapmodule.c	2008-08-30 10:16:13.000000000 +0100
7
@@ -223,7 +223,7 @@
8
 		return(NULL);
9
 
10
 	/* silently 'adjust' out-of-range requests */
11
-	if ((self->pos + num_bytes) > self->size) {
12
+	if (num_bytes > self->size - self->pos) {
13
 		num_bytes -= (self->pos+num_bytes) - self->size;
14
 	}
15
 	result = Py_BuildValue("s#", self->data+self->pos, num_bytes);
(-)python25/files/patch-ba (+119 lines)
Line 0 Link Here
1
2
3
Patch for CVE-2008-2315 taken from Gentoo.
4
5
--- Objects/unicodeobject.c.orig	2007-11-02 22:46:38.000000000 +0000
6
+++ Objects/unicodeobject.c	2008-08-30 10:16:13.000000000 +0100
7
@@ -239,6 +239,11 @@
8
         return unicode_empty;
9
     }
10
 
11
+    /* Ensure we won't overflow the size. */
12
+    if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) {
13
+        return (PyUnicodeObject *)PyErr_NoMemory();
14
+    }
15
+
16
     /* Unicode freelist & memory allocation */
17
     if (unicode_freelist) {
18
         unicode = unicode_freelist;
19
@@ -1091,6 +1096,9 @@
20
     char * out;
21
     char * start;
22
 
23
+    if (cbAllocated / 5 != size)
24
+        return PyErr_NoMemory();
25
+
26
     if (size == 0)
27
 		return PyString_FromStringAndSize(NULL, 0);
28
 
29
@@ -1689,8 +1697,9 @@
30
 {
31
     PyObject *v;
32
     unsigned char *p;
33
+    Py_ssize_t nsize, bytesize;
34
 #ifdef Py_UNICODE_WIDE
35
-    int i, pairs;
36
+    Py_ssize_t i, pairs;
37
 #else
38
     const int pairs = 0;
39
 #endif
40
@@ -1713,8 +1722,15 @@
41
 	if (s[i] >= 0x10000)
42
 	    pairs++;
43
 #endif
44
-    v = PyString_FromStringAndSize(NULL,
45
-		  2 * (size + pairs + (byteorder == 0)));
46
+    /* 2 * (size + pairs + (byteorder == 0)) */
47
+    if (size > PY_SSIZE_T_MAX ||
48
+	size > PY_SSIZE_T_MAX - pairs - (byteorder == 0))
49
+	return PyErr_NoMemory();
50
+    nsize = (size + pairs + (byteorder == 0));
51
+    bytesize = nsize * 2;
52
+    if (bytesize / 2 != nsize)
53
+	return PyErr_NoMemory();
54
+    v = PyString_FromStringAndSize(NULL, bytesize);
55
     if (v == NULL)
56
         return NULL;
57
 
58
@@ -2042,6 +2058,11 @@
59
     char *p;
60
 
61
     static const char *hexdigit = "0123456789abcdef";
62
+#ifdef Py_UNICODE_WIDE
63
+    const Py_ssize_t expandsize = 10;
64
+#else
65
+    const Py_ssize_t expandsize = 6;
66
+#endif
67
 
68
     /* Initial allocation is based on the longest-possible unichr
69
        escape.
70
@@ -2057,13 +2078,12 @@
71
        escape.
72
     */
73
 
74
+    if (size > (PY_SSIZE_T_MAX - 2 - 1) / expandsize)
75
+	return PyErr_NoMemory();
76
+
77
     repr = PyString_FromStringAndSize(NULL,
78
         2
79
-#ifdef Py_UNICODE_WIDE
80
-        + 10*size
81
-#else
82
-        + 6*size
83
-#endif
84
+        + expandsize*size
85
         + 1);
86
     if (repr == NULL)
87
         return NULL;
88
@@ -2304,12 +2324,16 @@
89
     char *q;
90
 
91
     static const char *hexdigit = "0123456789abcdef";
92
-
93
 #ifdef Py_UNICODE_WIDE
94
-    repr = PyString_FromStringAndSize(NULL, 10 * size);
95
+    const Py_ssize_t expandsize = 10;
96
 #else
97
-    repr = PyString_FromStringAndSize(NULL, 6 * size);
98
+    const Py_ssize_t expandsize = 6;
99
 #endif
100
+
101
+    if (size > PY_SSIZE_T_MAX / expandsize)
102
+	return PyErr_NoMemory();
103
+
104
+    repr = PyString_FromStringAndSize(NULL, expandsize * size);
105
     if (repr == NULL)
106
         return NULL;
107
     if (size == 0)
108
@@ -4719,6 +4743,11 @@
109
         return self;
110
     }
111
 
112
+    if (left > PY_SSIZE_T_MAX - self->length ||
113
+	right > PY_SSIZE_T_MAX - (left + self->length)) {
114
+        PyErr_SetString(PyExc_OverflowError, "padded string is too long");
115
+        return NULL;
116
+    }
117
     u = _PyUnicode_New(left + self->length + right);
118
     if (u) {
119
         if (left)
(-)python25/files/patch-bb (+21 lines)
Line 0 Link Here
1
2
3
Patch for CVE-2008-2315 taken from Gentoo.
4
5
--- Objects/tupleobject.c.orig	2006-08-12 18:03:09.000000000 +0100
6
+++ Objects/tupleobject.c	2008-08-30 10:16:13.000000000 +0100
7
@@ -60,11 +60,12 @@
8
 		Py_ssize_t nbytes = size * sizeof(PyObject *);
9
 		/* Check for overflow */
10
 		if (nbytes / sizeof(PyObject *) != (size_t)size ||
11
-		    (nbytes += sizeof(PyTupleObject) - sizeof(PyObject *))
12
-		    <= 0)
13
+		    (nbytes > PY_SSIZE_T_MAX - sizeof(PyTupleObject) - sizeof(PyObject *)))
14
 		{
15
 			return PyErr_NoMemory();
16
 		}
17
+		nbytes += sizeof(PyTupleObject) - sizeof(PyObject *);
18
+
19
 		op = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, size);
20
 		if (op == NULL)
21
 			return NULL;
(-)python25/files/patch-bc (+17 lines)
Line 0 Link Here
1
2
3
Patch for CVE-2008-2315 taken from Gentoo.
4
5
--- Objects/bufferobject.c.orig	2008-02-14 11:26:18.000000000 +0000
6
+++ Objects/bufferobject.c	2008-08-30 10:16:13.000000000 +0100
7
@@ -427,6 +427,10 @@
8
 		count = 0;
9
 	if (!get_buf(self, &ptr, &size, ANY_BUFFER))
10
 		return NULL;
11
+	if (count > PY_SSIZE_T_MAX / size) {
12
+		PyErr_SetString(PyExc_MemoryError, "result too large");
13
+		return NULL;
14
+	}
15
 	ob = PyString_FromStringAndSize(NULL, size * count);
16
 	if ( ob == NULL )
17
 		return NULL;
(-)python25/files/patch-bd (+15 lines)
Line 0 Link Here
1
2
3
Patch for CVE-2008-2315 taken from Gentoo.
4
5
--- Objects/longobject.c.orig	2007-05-07 19:30:48.000000000 +0100
6
+++ Objects/longobject.c	2008-08-30 10:16:13.000000000 +0100
7
@@ -70,6 +70,8 @@
8
 		PyErr_NoMemory();
9
 		return NULL;
10
 	}
11
+	/* XXX(nnorwitz): This can overflow --
12
+	   PyObject_NEW_VAR /  _PyObject_VAR_SIZE need to detect overflow */
13
 	return PyObject_NEW_VAR(PyLongObject, &PyLong_Type, size);
14
 }
15
 
(-)python25/files/patch-be (+53 lines)
Line 0 Link Here
1
2
3
Patch for CVE-2008-2315 taken from Gentoo.
4
5
--- Objects/stringobject.c.orig	2007-11-07 01:19:49.000000000 +0000
6
+++ Objects/stringobject.c	2008-08-30 10:16:13.000000000 +0100
7
@@ -71,6 +71,11 @@
8
 		return (PyObject *)op;
9
 	}
10
 
11
+	if (size > PY_SSIZE_T_MAX - sizeof(PyStringObject)) {
12
+		PyErr_SetString(PyExc_OverflowError, "string is too large");
13
+		return NULL;
14
+	}
15
+
16
 	/* Inline PyObject_NewVar */
17
 	op = (PyStringObject *)PyObject_MALLOC(sizeof(PyStringObject) + size);
18
 	if (op == NULL)
19
@@ -106,7 +111,7 @@
20
 
21
 	assert(str != NULL);
22
 	size = strlen(str);
23
-	if (size > PY_SSIZE_T_MAX) {
24
+	if (size > PY_SSIZE_T_MAX - sizeof(PyStringObject)) {
25
 		PyErr_SetString(PyExc_OverflowError,
26
 			"string is too long for a Python string");
27
 		return NULL;
28
@@ -967,14 +972,24 @@
29
 		Py_INCREF(a);
30
 		return (PyObject *)a;
31
 	}
32
+	/* Check that string sizes are not negative, to prevent an
33
+	   overflow in cases where we are passed incorrectly-created
34
+	   strings with negative lengths (due to a bug in other code).
35
+	*/
36
 	size = a->ob_size + b->ob_size;
37
-	if (size < 0) {
38
+	if (a->ob_size < 0 || b->ob_size < 0 ||
39
+	    a->ob_size > PY_SSIZE_T_MAX - b->ob_size) {
40
 		PyErr_SetString(PyExc_OverflowError,
41
 				"strings are too large to concat");
42
 		return NULL;
43
 	}
44
 	  
45
 	/* Inline PyObject_NewVar */
46
+	if (size > PY_SSIZE_T_MAX - sizeof(PyStringObject)) {
47
+		PyErr_SetString(PyExc_OverflowError,
48
+				"strings are too large to concat");
49
+		return NULL;
50
+	}
51
 	op = (PyStringObject *)PyObject_MALLOC(sizeof(PyStringObject) + size);
52
 	if (op == NULL)
53
 		return PyErr_NoMemory();
(-)python25/files/patch-bf (+25 lines)
Line 0 Link Here
1
2
3
Patch for CVE-2008-2315 taken from Gentoo.
4
5
--- Lib/test/seq_tests.py.orig	2007-11-12 20:04:41.000000000 +0000
6
+++ Lib/test/seq_tests.py	2008-08-30 10:16:13.000000000 +0100
7
@@ -307,11 +307,13 @@
8
             self.assertEqual(id(s), id(s*1))
9
 
10
     def test_bigrepeat(self):
11
-        x = self.type2test([0])
12
-        x *= 2**16
13
-        self.assertRaises(MemoryError, x.__mul__, 2**16)
14
-        if hasattr(x, '__imul__'):
15
-            self.assertRaises(MemoryError, x.__imul__, 2**16)
16
+        import sys
17
+        if sys.maxint <= 2147483647:
18
+            x = self.type2test([0])
19
+            x *= 2**16
20
+            self.assertRaises(MemoryError, x.__mul__, 2**16)
21
+            if hasattr(x, '__imul__'):
22
+                self.assertRaises(MemoryError, x.__imul__, 2**16)
23
 
24
     def test_subscript(self):
25
         a = self.type2test([10, 11])
(-)python25/files/patch-bg (+32 lines)
Line 0 Link Here
1
2
3
Patch for CVE-2008-2315 taken from Gentoo.
4
5
--- Lib/test/test_strop.py.orig	2002-07-31 00:27:12.000000000 +0100
6
+++ Lib/test/test_strop.py	2008-08-30 10:16:13.000000000 +0100
7
@@ -115,6 +115,25 @@
8
         strop.uppercase
9
         strop.whitespace
10
 
11
+    @test_support.precisionbigmemtest(size=test_support._2G - 1, memuse=5)
12
+    def test_stropjoin_huge_list(self, size):
13
+        a = "A" * size
14
+        try:
15
+            r = strop.join([a, a], a)
16
+        except OverflowError:
17
+            pass
18
+        else:
19
+            self.assertEquals(len(r), len(a) * 3)
20
+
21
+    @test_support.precisionbigmemtest(size=test_support._2G - 1, memuse=1)
22
+    def test_stropjoin_huge_tup(self, size):
23
+        a = "A" * size
24
+        try:
25
+            r = strop.join((a, a), a)
26
+        except OverflowError:
27
+            pass # acceptable on 32-bit
28
+        else:
29
+            self.assertEquals(len(r), len(a) * 3)
30
 
31
 transtable = '\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037 !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`xyzdefghijklmnopqrstuvwxyz{|}~\177\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237\240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257\260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377'
32
 
(-)python25/files/patch-bh (+167 lines)
Line 0 Link Here
1
2
3
Patch for CVE-2008-2315 taken from Gentoo.
4
5
--- Lib/test/test_bigmem.py.orig	2007-11-30 21:53:17.000000000 +0000
6
+++ Lib/test/test_bigmem.py	2008-08-30 10:16:13.000000000 +0100
7
@@ -1,5 +1,5 @@
8
 from test import test_support
9
-from test.test_support import bigmemtest, _1G, _2G
10
+from test.test_support import bigmemtest, _1G, _2G, _4G, precisionbigmemtest
11
 
12
 import unittest
13
 import operator
14
@@ -54,6 +54,22 @@
15
         self.assertEquals(s[lpadsize:-rpadsize], SUBSTR)
16
         self.assertEquals(s.strip(), SUBSTR.strip())
17
 
18
+    @precisionbigmemtest(size=_2G - 1, memuse=1)
19
+    def test_center_unicode(self, size):
20
+        SUBSTR = u' abc def ghi'
21
+        try:
22
+            s = SUBSTR.center(size)
23
+        except OverflowError:
24
+            pass # acceptable on 32-bit
25
+        else:
26
+            self.assertEquals(len(s), size)
27
+            lpadsize = rpadsize = (len(s) - len(SUBSTR)) // 2
28
+            if len(s) % 2:
29
+                lpadsize += 1
30
+            self.assertEquals(s[lpadsize:-rpadsize], SUBSTR)
31
+            self.assertEquals(s.strip(), SUBSTR.strip())
32
+            del s
33
+
34
     @bigmemtest(minsize=_2G, memuse=2)
35
     def test_count(self, size):
36
         SUBSTR = ' abc def ghi'
37
@@ -70,10 +86,44 @@
38
         s = '.' * size
39
         self.assertEquals(len(s.decode('utf-8')), size)
40
 
41
+    def basic_encode_test(self, size, enc, c=u'.', expectedsize=None):
42
+        if expectedsize is None:
43
+            expectedsize = size
44
+
45
+        s = c * size
46
+        self.assertEquals(len(s.encode(enc)), expectedsize)
47
+
48
     @bigmemtest(minsize=_2G + 2, memuse=3)
49
     def test_encode(self, size):
50
-        s = u'.' * size
51
-        self.assertEquals(len(s.encode('utf-8')), size)
52
+        return self.basic_encode_test(size, 'utf-8')
53
+
54
+    @precisionbigmemtest(size=_4G / 6 + 2, memuse=2)
55
+    def test_encode_raw_unicode_escape(self, size):
56
+        try:
57
+            return self.basic_encode_test(size, 'raw_unicode_escape')
58
+        except MemoryError:
59
+            pass # acceptable on 32-bit
60
+
61
+    @precisionbigmemtest(size=_4G / 5 + 70, memuse=3)
62
+    def test_encode_utf7(self, size):
63
+        try:
64
+            return self.basic_encode_test(size, 'utf7')
65
+        except MemoryError:
66
+            pass # acceptable on 32-bit
67
+
68
+    @precisionbigmemtest(size=_2G-1, memuse=2)
69
+    def test_decodeascii(self, size):
70
+        return self.basic_encode_test(size, 'ascii', c='A')
71
+
72
+    @precisionbigmemtest(size=_4G / 5, memuse=6+2)
73
+    def test_unicode_repr_oflw(self, size):
74
+        try:
75
+            s = u"\uAAAA"*size
76
+            r = repr(s)
77
+        except MemoryError:
78
+            pass # acceptable on 32-bit
79
+        else:
80
+            self.failUnless(s == eval(r))
81
 
82
     @bigmemtest(minsize=_2G, memuse=2)
83
     def test_endswith(self, size):
84
@@ -459,6 +509,11 @@
85
         self.assertEquals(s.count('\\'), size)
86
         self.assertEquals(s.count('0'), size * 2)
87
 
88
+    @bigmemtest(minsize=2**32 / 5, memuse=6+2)
89
+    def test_unicode_repr(self, size):
90
+        s = u"\uAAAA" * size
91
+        self.failUnless(len(repr(s)) > size)
92
+
93
     # This test is meaningful even with size < 2G, as long as the
94
     # doubled string is > 2G (but it tests more if both are > 2G :)
95
     @bigmemtest(minsize=_1G + 2, memuse=3)
96
@@ -642,6 +697,35 @@
97
     def test_repeat_large(self, size):
98
         return self.basic_test_repeat(size)
99
 
100
+    @bigmemtest(minsize=_1G - 1, memuse=12)
101
+    def test_repeat_large_2(self, size):
102
+        return self.basic_test_repeat(size)
103
+
104
+    @precisionbigmemtest(size=_1G - 1, memuse=9)
105
+    def test_from_2G_generator(self, size):
106
+        try:
107
+            t = tuple(xrange(size))
108
+        except MemoryError:
109
+            pass # acceptable on 32-bit
110
+        else:
111
+            count = 0
112
+            for item in t:
113
+                self.assertEquals(item, count)
114
+                count += 1
115
+            self.assertEquals(count, size)
116
+
117
+    @precisionbigmemtest(size=_1G - 25, memuse=9)
118
+    def test_from_almost_2G_generator(self, size):
119
+        try:
120
+            t = tuple(xrange(size))
121
+            count = 0
122
+            for item in t:
123
+                self.assertEquals(item, count)
124
+                count += 1
125
+            self.assertEquals(count, size)
126
+        except MemoryError:
127
+            pass # acceptable, expected on 32-bit
128
+
129
     # Like test_concat, split in two.
130
     def basic_test_repr(self, size):
131
         t = (0,) * size
132
@@ -957,8 +1041,34 @@
133
         self.assertEquals(l[:10], [1] * 10)
134
         self.assertEquals(l[-10:], [5] * 10)
135
 
136
+class BufferTest(unittest.TestCase):
137
+
138
+    @precisionbigmemtest(size=_1G, memuse=4)
139
+    def test_repeat(self, size):
140
+        try:
141
+            b = buffer("AAAA")*size
142
+        except MemoryError:
143
+            pass # acceptable on 32-bit
144
+        else:
145
+            count = 0
146
+            for c in b:
147
+                self.assertEquals(c, 'A')
148
+                count += 1
149
+            self.assertEquals(count, size*4)
150
+
151
 def test_main():
152
-    test_support.run_unittest(StrTest, TupleTest, ListTest)
153
+    test_support.run_unittest(StrTest, TupleTest, ListTest, BufferTest)
154
+
155
+# Expected failures (crashers)
156
+# del StrTest.test_center_unicode
157
+del StrTest.test_decodeascii
158
+# del StrTest.test_encode_utf32
159
+# del StrTest.test_encode_utf7
160
+# del StrTest.test_encode_raw_unicode_escape
161
+#
162
+# del TupleTest.test_from_2G_generator
163
+#
164
+# del BufferTest.test_repeat
165
 
166
 if __name__ == '__main__':
167
     if len(sys.argv) > 1:
(-)python25/files/patch-bi (+66 lines)
Line 0 Link Here
1
2
3
Patch for CVE-2008-2315 taken from Gentoo.
4
5
--- Lib/test/test_support.py.orig	2008-01-27 01:24:44.000000000 +0000
6
+++ Lib/test/test_support.py	2008-08-30 10:16:13.000000000 +0100
7
@@ -33,6 +33,7 @@
8
 use_resources = None     # Flag set to [] by regrtest.py
9
 max_memuse = 0           # Disable bigmem tests (they will still be run with
10
                          # small sizes, to make sure they work.)
11
+real_max_memuse = 0
12
 
13
 # _original_stdout is meant to hold stdout at the time regrtest began.
14
 # This may be "the real" stdout, or IDLE's emulation of stdout, or whatever.
15
@@ -323,6 +324,7 @@
16
 _1M = 1024*1024
17
 _1G = 1024 * _1M
18
 _2G = 2 * _1G
19
+_4G = 4 * _1G
20
 
21
 # Hack to get at the maximum value an internal index can take.
22
 class _Dummy:
23
@@ -333,6 +335,7 @@
24
 def set_memlimit(limit):
25
     import re
26
     global max_memuse
27
+    global real_max_memuse
28
     sizes = {
29
         'k': 1024,
30
         'm': _1M,
31
@@ -344,6 +347,7 @@
32
     if m is None:
33
         raise ValueError('Invalid memory limit %r' % (limit,))
34
     memlimit = int(float(m.group(1)) * sizes[m.group(3).lower()])
35
+    real_max_memuse = memlimit
36
     if memlimit > MAX_Py_ssize_t:
37
         memlimit = MAX_Py_ssize_t
38
     if memlimit < _2G - 1:
39
@@ -389,6 +393,27 @@
40
         return wrapper
41
     return decorator
42
 
43
+def precisionbigmemtest(size, memuse, overhead=5*_1M):
44
+    def decorator(f):
45
+        def wrapper(self):
46
+            if not real_max_memuse:
47
+                maxsize = 5147
48
+            else:
49
+                maxsize = size
50
+
51
+                if real_max_memuse and real_max_memuse < maxsize * memuse:
52
+                    if verbose:
53
+                        sys.stderr.write("Skipping %s because of memory "
54
+                                         "constraint\n" % (f.__name__,))
55
+                    return
56
+
57
+            return f(self, maxsize)
58
+        wrapper.size = size
59
+        wrapper.memuse = memuse
60
+        wrapper.overhead = overhead
61
+        return wrapper
62
+    return decorator
63
+
64
 def bigaddrspacetest(f):
65
     """Decorator for tests that fill the address space."""
66
     def wrapper(self):
(-)python25/files/patch-bj (+35 lines)
Line 0 Link Here
1
2
3
Patch for CVE-2008-2315 taken from Gentoo.
4
5
--- Modules/stropmodule.c.orig	2008-02-14 11:26:18.000000000 +0000
6
+++ Modules/stropmodule.c	2008-08-30 10:16:13.000000000 +0100
7
@@ -216,6 +216,13 @@
8
 				return NULL;
9
 			}
10
 			slen = PyString_GET_SIZE(item);
11
+			if (slen > PY_SSIZE_T_MAX - reslen ||
12
+			    seplen > PY_SSIZE_T_MAX - reslen - seplen) {
13
+				PyErr_SetString(PyExc_OverflowError,
14
+						"input too long");
15
+				Py_DECREF(res);
16
+				return NULL;
17
+			}
18
 			while (reslen + slen + seplen >= sz) {
19
 				if (_PyString_Resize(&res, sz * 2) < 0)
20
 					return NULL;
21
@@ -253,6 +260,14 @@
22
 			return NULL;
23
 		}
24
 		slen = PyString_GET_SIZE(item);
25
+		if (slen > PY_SSIZE_T_MAX - reslen ||
26
+		    seplen > PY_SSIZE_T_MAX - reslen - seplen) {
27
+			PyErr_SetString(PyExc_OverflowError,
28
+					"input too long");
29
+			Py_DECREF(res);
30
+			Py_XDECREF(item);
31
+			return NULL;
32
+		}
33
 		while (reslen + slen + seplen >= sz) {
34
 			if (_PyString_Resize(&res, sz * 2) < 0) {
35
 				Py_DECREF(item);
(-)python25/files/patch-bk (+27 lines)
Line 0 Link Here
1
2
3
Patch for CVE-2008-2315 taken from Gentoo.
4
5
--- Modules/gcmodule.c.orig	2006-10-09 20:42:33.000000000 +0100
6
+++ Modules/gcmodule.c	2008-08-30 10:16:13.000000000 +0100
7
@@ -1318,7 +1318,10 @@
8
 _PyObject_GC_Malloc(size_t basicsize)
9
 {
10
 	PyObject *op;
11
-	PyGC_Head *g = (PyGC_Head *)PyObject_MALLOC(
12
+	PyGC_Head *g;
13
+	if (basicsize > PY_SSIZE_T_MAX - sizeof(PyGC_Head))
14
+		return PyErr_NoMemory();
15
+	g = (PyGC_Head *)PyObject_MALLOC(
16
                 sizeof(PyGC_Head) + basicsize);
17
 	if (g == NULL)
18
 		return PyErr_NoMemory();
19
@@ -1361,6 +1364,8 @@
20
 {
21
 	const size_t basicsize = _PyObject_VAR_SIZE(op->ob_type, nitems);
22
 	PyGC_Head *g = AS_GC(op);
23
+	if (basicsize > PY_SSIZE_T_MAX - sizeof(PyGC_Head))
24
+		return (PyVarObject *)PyErr_NoMemory();
25
 	g = (PyGC_Head *)PyObject_REALLOC(g,  sizeof(PyGC_Head) + basicsize);
26
 	if (g == NULL)
27
 		return (PyVarObject *)PyErr_NoMemory();
(-)python25/files/patch-ca (+62 lines)
Line 0 Link Here
1
2
3
Patch for CVE-2008-3142 taken from Gentoo.
4
5
--- Include/pymem.h.orig	2008-02-14 11:26:18.000000000 +0000
6
+++ Include/pymem.h	2008-08-30 10:39:43.000000000 +0100
7
@@ -67,8 +67,12 @@
8
    for malloc(0), which would be treated as an error. Some platforms
9
    would return a pointer with no memory behind it, which would break
10
    pymalloc. To solve these problems, allocate an extra byte. */
11
-#define PyMem_MALLOC(n)         malloc((n) ? (n) : 1)
12
-#define PyMem_REALLOC(p, n)     realloc((p), (n) ? (n) : 1)
13
+/* Returns NULL to indicate error if a negative size or size larger than
14
+   Py_ssize_t can represent is supplied.  Helps prevents security holes. */
15
+#define PyMem_MALLOC(n)		(((n) < 0 || (n) > PY_SSIZE_T_MAX) ? NULL \
16
+				: malloc((n) ? (n) : 1))
17
+#define PyMem_REALLOC(p, n)	(((n) < 0 || (n) > PY_SSIZE_T_MAX) ? NULL \
18
+				: realloc((p), (n) ? (n) : 1))
19
 #define PyMem_FREE		free
20
 
21
 #endif	/* PYMALLOC_DEBUG */
22
@@ -77,24 +81,31 @@
23
  * Type-oriented memory interface
24
  * ==============================
25
  *
26
- * These are carried along for historical reasons.  There's rarely a good
27
- * reason to use them anymore (you can just as easily do the multiply and
28
- * cast yourself).
29
+ * Allocate memory for n objects of the given type.  Returns a new pointer
30
+ * or NULL if the request was too large or memory allocation failed.  Use
31
+ * these macros rather than doing the multiplication yourself so that proper
32
+ * overflow checking is always done.
33
  */
34
 
35
 #define PyMem_New(type, n) \
36
-  ( assert((n) <= PY_SIZE_MAX / sizeof(type)) , \
37
+  ( ((n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
38
 	( (type *) PyMem_Malloc((n) * sizeof(type)) ) )
39
 #define PyMem_NEW(type, n) \
40
-  ( assert((n) <= PY_SIZE_MAX / sizeof(type)) , \
41
+  ( ((n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
42
 	( (type *) PyMem_MALLOC((n) * sizeof(type)) ) )
43
 
44
+/*
45
+ * The value of (p) is always clobbered by this macro regardless of success.
46
+ * The caller MUST check if (p) is NULL afterwards and deal with the memory
47
+ * error if so.  This means the original value of (p) MUST be saved for the
48
+ * caller's memory error handler to not lose track of it.
49
+ */
50
 #define PyMem_Resize(p, type, n) \
51
-  ( assert((n) <= PY_SIZE_MAX / sizeof(type)) , \
52
-	( (p) = (type *) PyMem_Realloc((p), (n) * sizeof(type)) ) )
53
+  ( (p) = ((n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
54
+	(type *) PyMem_Realloc((p), (n) * sizeof(type)) )
55
 #define PyMem_RESIZE(p, type, n) \
56
-  ( assert((n) <= PY_SIZE_MAX / sizeof(type)) , \
57
-	( (p) = (type *) PyMem_REALLOC((p), (n) * sizeof(type)) ) )
58
+  ( (p) = ((n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
59
+	(type *) PyMem_REALLOC((p), (n) * sizeof(type)) )
60
 
61
 /* PyMem{Del,DEL} are left over from ancient days, and shouldn't be used
62
  * anymore.  They're just confusing aliases for PyMem_{Free,FREE} now.
(-)python25/files/patch-cb (+38 lines)
Line 0 Link Here
1
2
3
Patch for CVE-2008-3142 taken from Gentoo.
4
5
--- Objects/obmalloc.c.orig	2008-02-14 11:26:18.000000000 +0000
6
+++ Objects/obmalloc.c	2008-08-30 10:39:43.000000000 +0100
7
@@ -727,6 +727,15 @@
8
 	uint size;
9
 
10
 	/*
11
+	 * Limit ourselves to PY_SSIZE_T_MAX bytes to prevent security holes.
12
+	 * Most python internals blindly use a signed Py_ssize_t to track
13
+	 * things without checking for overflows or negatives.
14
+	 * As size_t is unsigned, checking for nbytes < 0 is not required.
15
+	 */
16
+	if (nbytes > PY_SSIZE_T_MAX)
17
+		return NULL;
18
+
19
+	/*
20
 	 * This implicitly redirects malloc(0).
21
 	 */
22
 	if ((nbytes - 1) < SMALL_REQUEST_THRESHOLD) {
23
@@ -1130,6 +1139,15 @@
24
 	if (p == NULL)
25
 		return PyObject_Malloc(nbytes);
26
 
27
+	/*
28
+	 * Limit ourselves to PY_SSIZE_T_MAX bytes to prevent security holes.
29
+	 * Most python internals blindly use a signed Py_ssize_t to track
30
+	 * things without checking for overflows or negatives.
31
+	 * As size_t is unsigned, checking for nbytes < 0 is not required.
32
+	 */
33
+	if (nbytes > PY_SSIZE_T_MAX)
34
+		return NULL;
35
+
36
 	pool = POOL_ADDR(p);
37
 	if (Py_ADDRESS_IN_RANGE(p, pool)) {
38
 		/* We're in charge of this block */
(-)python25/files/patch-cc (+18 lines)
Line 0 Link Here
1
2
3
Patch for CVE-2008-3142 taken from Gentoo.
4
5
--- Modules/almodule.c.orig	2006-09-25 07:53:42.000000000 +0100
6
+++ Modules/almodule.c	2008-08-30 10:39:43.000000000 +0100
7
@@ -1633,9 +1633,11 @@
8
 	if (nvals < 0)
9
 		goto cleanup;
10
 	if (nvals > setsize) {
11
+		ALvalue *old_return_set = return_set;
12
 		setsize = nvals;
13
 		PyMem_RESIZE(return_set, ALvalue, setsize);
14
 		if (return_set == NULL) {
15
+			return_set = old_return_set;
16
 			PyErr_NoMemory();
17
 			goto cleanup;
18
 		}
(-)python25/files/patch-cd (+37 lines)
Line 0 Link Here
1
2
3
Patch for CVE-2008-3142 taken from Gentoo.
4
5
--- Modules/arraymodule.c.orig	2008-02-15 19:11:46.000000000 +0000
6
+++ Modules/arraymodule.c	2008-08-30 10:39:43.000000000 +0100
7
@@ -816,6 +816,7 @@
8
 array_do_extend(arrayobject *self, PyObject *bb)
9
 {
10
 	Py_ssize_t size;
11
+	char *old_item;
12
 
13
 	if (!array_Check(bb))
14
 		return array_iter_extend(self, bb);
15
@@ -831,10 +832,11 @@
16
 			return -1;
17
 	}
18
 	size = self->ob_size + b->ob_size;
19
+	old_item = self->ob_item;
20
         PyMem_RESIZE(self->ob_item, char, size*self->ob_descr->itemsize);
21
         if (self->ob_item == NULL) {
22
-                PyObject_Del(self);
23
-                PyErr_NoMemory();
24
+		self->ob_item = old_item;
25
+		PyErr_NoMemory();
26
 		return -1;
27
         }
28
 	memcpy(self->ob_item + self->ob_size*self->ob_descr->itemsize,
29
@@ -886,7 +888,7 @@
30
 			if (size > PY_SSIZE_T_MAX / n) {
31
 				return PyErr_NoMemory();
32
 			}
33
-			PyMem_Resize(items, char, n * size);
34
+			PyMem_RESIZE(items, char, n * size);
35
 			if (items == NULL)
36
 				return PyErr_NoMemory();
37
 			p = items;
(-)python25/files/patch-ce (+20 lines)
Line 0 Link Here
1
2
3
Patch for CVE-2008-3142 taken from Gentoo.
4
5
--- Modules/selectmodule.c.orig	2006-07-10 02:18:57.000000000 +0100
6
+++ Modules/selectmodule.c	2008-08-30 10:39:43.000000000 +0100
7
@@ -349,10 +349,12 @@
8
 {
9
 	Py_ssize_t i, pos;
10
 	PyObject *key, *value;
11
+        struct pollfd *old_ufds = self->ufds;
12
 
13
 	self->ufd_len = PyDict_Size(self->dict);
14
-	PyMem_Resize(self->ufds, struct pollfd, self->ufd_len);
15
+	PyMem_RESIZE(self->ufds, struct pollfd, self->ufd_len);
16
 	if (self->ufds == NULL) {
17
+                self->ufds = old_ufds;
18
 		PyErr_NoMemory();
19
 		return 0;
20
 	}
(-)python25/files/patch-da (+45 lines)
Line 0 Link Here
1
2
3
Patch for CVE-2008-2316 taken from Gentoo.
4
5
--- Lib/test/test_hashlib.py.orig	2005-08-21 19:45:59.000000000 +0100
6
+++ Lib/test/test_hashlib.py	2008-08-30 10:43:27.000000000 +0100
7
@@ -9,7 +9,7 @@
8
 import hashlib
9
 import unittest
10
 from test import test_support
11
-
12
+from test.test_support import _4G, precisionbigmemtest
13
 
14
 def hexstr(s):
15
     import string
16
@@ -55,7 +55,6 @@
17
             m2.update(aas + bees + cees)
18
             self.assertEqual(m1.digest(), m2.digest())
19
 
20
-
21
     def check(self, name, data, digest):
22
         # test the direct constructors
23
         computed = getattr(hashlib, name)(data).hexdigest()
24
@@ -75,6 +74,21 @@
25
         self.check('md5', 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
26
                    'd174ab98d277d9f5a5611c2c9f419d9f')
27
 
28
+    @precisionbigmemtest(size=_4G + 5, memuse=1)
29
+    def test_case_md5_huge(self, size):
30
+        if size == _4G + 5:
31
+            try:
32
+                self.check('md5', 'A'*size, 'c9af2dff37468ce5dfee8f2cfc0a9c6d')
33
+            except OverflowError:
34
+                pass # 32-bit arch
35
+
36
+    @precisionbigmemtest(size=_4G - 1, memuse=1)
37
+    def test_case_md5_uintmax(self, size):
38
+        if size == _4G - 1:
39
+            try:
40
+                self.check('md5', 'A'*size, '28138d306ff1b8281f1a9067e1a1a2b3')
41
+            except OverflowError:
42
+                pass # 32-bit arch
43
 
44
     # use the three examples from Federal Information Processing Standards
45
     # Publication 180-1, Secure Hash Standard,  1995 April 17
(-)python25/files/patch-db (+108 lines)
Line 0 Link Here
1
2
3
Patch for CVE-2008-2316 taken from Gentoo.
4
5
--- Modules/_hashopenssl.c.orig	2006-05-29 22:04:52.000000000 +0100
6
+++ Modules/_hashopenssl.c	2008-08-30 10:43:27.000000000 +0100
7
@@ -19,6 +19,8 @@
8
 /* EVP is the preferred interface to hashing in OpenSSL */
9
 #include <openssl/evp.h>
10
 
11
+#define MUNCH_SIZE INT_MAX
12
+
13
 
14
 #ifndef HASH_OBJ_CONSTRUCTOR
15
 #define HASH_OBJ_CONSTRUCTOR 0
16
@@ -164,9 +166,18 @@
17
     if (!PyArg_ParseTuple(args, "s#:update", &cp, &len))
18
         return NULL;
19
 
20
+    if (len > 0 && len <= MUNCH_SIZE) {
21
     EVP_DigestUpdate(&self->ctx, cp, Py_SAFE_DOWNCAST(len, Py_ssize_t,
22
                                                       unsigned int));
23
-
24
+    } else {
25
+        Py_ssize_t offset = 0;
26
+        while (len) {
27
+            unsigned int process = len > MUNCH_SIZE ? MUNCH_SIZE : len;
28
+            EVP_DigestUpdate(&self->ctx, cp + offset, process);
29
+            len -= process;
30
+            offset += process;
31
+        }
32
+    }
33
     Py_INCREF(Py_None);
34
     return Py_None;
35
 }
36
@@ -255,9 +266,20 @@
37
     self->name = name_obj;
38
     Py_INCREF(self->name);
39
 
40
-    if (cp && len)
41
+    if (cp && len) {
42
+        if (len > 0 && len <= MUNCH_SIZE) {
43
         EVP_DigestUpdate(&self->ctx, cp, Py_SAFE_DOWNCAST(len, Py_ssize_t,
44
                                                           unsigned int));
45
+        } else {
46
+            Py_ssize_t offset = 0;
47
+            while (len) {
48
+                unsigned int process = len > MUNCH_SIZE ? MUNCH_SIZE : len;
49
+                EVP_DigestUpdate(&self->ctx, cp + offset, process);
50
+                len -= process;
51
+                offset += process;
52
+            }
53
+        }
54
+    }
55
 
56
     return 0;
57
 }
58
@@ -328,7 +350,7 @@
59
 static PyObject *
60
 EVPnew(PyObject *name_obj,
61
        const EVP_MD *digest, const EVP_MD_CTX *initial_ctx,
62
-       const unsigned char *cp, unsigned int len)
63
+       const unsigned char *cp, Py_ssize_t len)
64
 {
65
     EVPobject *self;
66
 
67
@@ -346,8 +368,20 @@
68
         EVP_DigestInit(&self->ctx, digest);
69
     }
70
 
71
-    if (cp && len)
72
-        EVP_DigestUpdate(&self->ctx, cp, len);
73
+    if (cp && len) {
74
+        if (len > 0 && len <= MUNCH_SIZE) {
75
+            EVP_DigestUpdate(&self->ctx, cp, Py_SAFE_DOWNCAST(len, Py_ssize_t,
76
+                                                              unsigned int));
77
+        } else {
78
+            Py_ssize_t offset = 0;
79
+            while (len) {
80
+                unsigned int process = len > MUNCH_SIZE ? MUNCH_SIZE : len;
81
+                EVP_DigestUpdate(&self->ctx, cp + offset, process);
82
+                len -= process;
83
+                offset += process;
84
+            }
85
+        }
86
+    }
87
 
88
     return (PyObject *)self;
89
 }
90
@@ -384,8 +418,7 @@
91
 
92
     digest = EVP_get_digestbyname(name);
93
 
94
-    return EVPnew(name_obj, digest, NULL, cp, Py_SAFE_DOWNCAST(len, Py_ssize_t,
95
-                                                               unsigned int));
96
+    return EVPnew(name_obj, digest, NULL, cp, len);
97
 }
98
 
99
 /*
100
@@ -410,7 +443,7 @@
101
                 CONST_ ## NAME ## _name_obj, \
102
                 NULL, \
103
                 CONST_new_ ## NAME ## _ctx_p, \
104
-                cp, Py_SAFE_DOWNCAST(len, Py_ssize_t, unsigned int)); \
105
+                cp, len); \
106
     }
107
 
108
 /* a PyMethodDef structure for the constructor */
(-)python25/files/patch-ea (+59 lines)
Line 0 Link Here
1
2
3
Patch for CVE-2008-3144 taken from Gentoo.
4
5
--- Python/mysnprintf.c.orig	2001-12-21 16:32:15.000000000 +0000
6
+++ Python/mysnprintf.c	2008-08-30 10:46:31.000000000 +0100
7
@@ -54,18 +54,28 @@
8
 PyOS_vsnprintf(char *str, size_t size, const char  *format, va_list va)
9
 {
10
 	int len;  /* # bytes written, excluding \0 */
11
-#ifndef HAVE_SNPRINTF
12
+#ifdef HAVE_SNPRINTF
13
+#define _PyOS_vsnprintf_EXTRA_SPACE 1
14
+#else
15
+#define _PyOS_vsnprintf_EXTRA_SPACE 512
16
 	char *buffer;
17
 #endif
18
 	assert(str != NULL);
19
 	assert(size > 0);
20
 	assert(format != NULL);
21
+	/* We take a size_t as input but return an int.  Sanity check
22
+	 * our input so that it won't cause an overflow in the
23
+         * vsnprintf return value or the buffer malloc size.  */
24
+	if (size > INT_MAX - _PyOS_vsnprintf_EXTRA_SPACE) {
25
+		len = -666;
26
+		goto Done;
27
+	}
28
 
29
 #ifdef HAVE_SNPRINTF
30
 	len = vsnprintf(str, size, format, va);
31
 #else
32
 	/* Emulate it. */
33
-	buffer = PyMem_MALLOC(size + 512);
34
+	buffer = PyMem_MALLOC(size + _PyOS_vsnprintf_EXTRA_SPACE);
35
 	if (buffer == NULL) {
36
 		len = -666;
37
 		goto Done;
38
@@ -75,7 +85,7 @@
39
 	if (len < 0)
40
 		/* ignore the error */;
41
 
42
-	else if ((size_t)len >= size + 512)
43
+	else if ((size_t)len >= size + _PyOS_vsnprintf_EXTRA_SPACE)
44
 		Py_FatalError("Buffer overflow in PyOS_snprintf/PyOS_vsnprintf");
45
 
46
 	else {
47
@@ -86,8 +96,10 @@
48
 		str[to_copy] = '\0';
49
 	}
50
 	PyMem_FREE(buffer);
51
-Done:
52
 #endif
53
-	str[size-1] = '\0';
54
+Done:
55
+	if (size > 0)
56
+		str[size-1] = '\0';
57
 	return len;
58
+#undef _PyOS_vsnprintf_EXTRA_SPACE
59
 }

Return to bug 127172