From 4aa74c429cabef6aaf91b2b0d4c2163778b1249d Mon Sep 17 00:00:00 2001 From: Berker Peksag Date: Wed, 14 Sep 2016 08:09:48 +0300 Subject: [PATCH 1/2] Issue #28131: Fix a regression in zipimport's compile_source() zipimport should use the same optimization level as the interpreter. --- Lib/test/test_zipimport.py | 13 +++++++++++++ Misc/NEWS | 3 +++ Modules/zipimport.c | 2 +- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_zipimport.py b/Lib/test/test_zipimport.py index 2bb7230c26a..d5b3b22ae1d 100644 --- a/Lib/test/test_zipimport.py +++ b/Lib/test/test_zipimport.py @@ -513,6 +513,19 @@ def get_file(): "some.data": (NOW, "some data")} self.doTest(pyc_ext, files, TESTMOD) + def testDefaultOptimizationLevel(self): + # zipimport should use the default optimization level (#28131) + src = """if 1: # indent hack + def test(val): + assert(val) + return val\n""" + files = {TESTMOD + '.py': (NOW, src)} + self.makeZip(files) + sys.path.insert(0, TEMP_ZIP) + mod = importlib.import_module(TESTMOD) + self.assertEqual(mod.test(1), 1) + self.assertRaises(AssertionError, mod.test, False) + def testImport_WithStuff(self): # try importing from a zipfile which contains additional # stuff at the beginning of the file diff --git a/Misc/NEWS b/Misc/NEWS index 246ac72e9f9..e61e98b9c7e 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,6 +10,9 @@ Release date: TBA Core and Builtins ----------------- +- Issue #28131: Fix a regression in zipimport's compile_source(). zipimport + should use the same optimization level as the interpreter. + - Issue #25221: Fix corrupted result from PyLong_FromLong(0) when Python is compiled with NSMALLPOSINTS = 0. diff --git a/Modules/zipimport.c b/Modules/zipimport.c index 92a82e6df26..7473a8fe877 100644 --- a/Modules/zipimport.c +++ b/Modules/zipimport.c @@ -1370,7 +1370,7 @@ compile_source(PyObject *pathname, PyObject *source) } code = Py_CompileStringObject(PyBytes_AsString(fixed_source), - pathname, Py_file_input, NULL, 1); + pathname, Py_file_input, NULL, -1); Py_DECREF(fixed_source); return code; From d751040b1a4e35fd3b01fc919cd8f9374ed714fd Mon Sep 17 00:00:00 2001 From: Berker Peksag Date: Wed, 14 Sep 2016 08:37:28 +0300 Subject: [PATCH 2/2] Issue #26171: Prevent buffer overflow in get_data Backport of 01ddd608b85c. --- Misc/NEWS | 3 +++ Modules/zipimport.c | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/Misc/NEWS b/Misc/NEWS index a38d8beeb1d..731cd0f1ed0 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,6 +10,9 @@ What's New in Python 3.3.7? Core and Builtins ----------------- +- Issue #26171: Fix possible integer overflow and heap corruption in + zipimporter.get_data(). + - Issue #25709: Fixed problem with in-place string concatenation and utf-8 cache. - Issue #24407: Fix crash when dict is mutated while being updated. diff --git a/Modules/zipimport.c b/Modules/zipimport.c index 2feb2a827c8..dad699e7e93 100644 --- a/Modules/zipimport.c +++ b/Modules/zipimport.c @@ -1089,6 +1089,11 @@ get_data(PyObject *archive, PyObject *toc_entry) PyMarshal_ReadShortFromFile(fp); /* local header size */ file_offset += l; /* Start of file data */ + if (data_size > LONG_MAX - 1) { + fclose(fp); + PyErr_NoMemory(); + return NULL; + } bytes_size = compress == 0 ? data_size : data_size + 1; if (bytes_size == 0) bytes_size++;