From 67d67ac4d3675f43cc288ec10559021df769a652 Mon Sep 17 00:00:00 2001 From: Charalampos Stratakis Date: Mon, 7 Aug 2023 12:04:56 +0200 Subject: [PATCH] Update to 3.8.17 Security fix for CVE-2023-24329 Add filters for tarfile extraction (CVE-2007-4559, PEP-706) Resolves: rhbz#2173917, rhbz#263261 --- 00189-use-rpm-wheels.patch | 2 +- 00397-tarfile-filter.patch | 247 +++++++++++++++++++++++++++++++++++++ python38.spec | 23 +++- sources | 2 +- 4 files changed, 267 insertions(+), 7 deletions(-) create mode 100644 00397-tarfile-filter.patch diff --git a/00189-use-rpm-wheels.patch b/00189-use-rpm-wheels.patch index 15021bf..521efef 100644 --- a/00189-use-rpm-wheels.patch +++ b/00189-use-rpm-wheels.patch @@ -29,7 +29,7 @@ index b291e9a..798d0f4 100644 __all__ = ["version", "bootstrap"] _PACKAGE_NAMES = ('setuptools', 'pip') -_SETUPTOOLS_VERSION = "56.0.0" --_PIP_VERSION = "22.0.4" +-_PIP_VERSION = "23.0.1" + +_WHEEL_DIR = "/usr/share/python38-wheels/" + diff --git a/00397-tarfile-filter.patch b/00397-tarfile-filter.patch new file mode 100644 index 0000000..0bf2f50 --- /dev/null +++ b/00397-tarfile-filter.patch @@ -0,0 +1,247 @@ +From dc84087083c5ad99a5016e8349c96d9654a08f46 Mon Sep 17 00:00:00 2001 +From: Petr Viktorin +Date: Mon, 6 Mar 2023 17:24:24 +0100 +Subject: [PATCH 2/2] CVE-2007-4559, PEP-706: Add filters for tarfile + extraction (downstream) + +Add and test RHEL-specific ways of configuring the default behavior: environment +variable and config file. +--- + Lib/tarfile.py | 42 +++++++++++++ + Lib/test/test_shutil.py | 3 +- + Lib/test/test_tarfile.py | 124 ++++++++++++++++++++++++++++++++++++++- + 3 files changed, 165 insertions(+), 4 deletions(-) + +diff --git a/Lib/tarfile.py b/Lib/tarfile.py +index 5291622ab8e..12ab00d748a 100755 +--- a/Lib/tarfile.py ++++ b/Lib/tarfile.py +@@ -72,6 +72,13 @@ __all__ = ["TarFile", "TarInfo", "is_tarfile", "TarError", "ReadError", + "ENCODING", "USTAR_FORMAT", "GNU_FORMAT", "PAX_FORMAT", + "DEFAULT_FORMAT", "open"] + ++# If true, use the safer (but backwards-incompatible) 'tar' extraction filter, ++# rather than 'fully_trusted', by default. ++# The emitted warning is changed to match. ++_RH_SAFER_DEFAULT = True ++ ++# System-wide configuration file ++_CONFIG_FILENAME = '/etc/python/tarfile.cfg' + + #--------------------------------------------------------- + # tar constants +@@ -2188,6 +2195,41 @@ class TarFile(object): + if filter is None: + filter = self.extraction_filter + if filter is None: ++ name = os.environ.get('PYTHON_TARFILE_EXTRACTION_FILTER') ++ if name is None: ++ try: ++ file = bltn_open(_CONFIG_FILENAME) ++ except FileNotFoundError: ++ pass ++ else: ++ import configparser ++ conf = configparser.ConfigParser( ++ interpolation=None, ++ comment_prefixes=('#', ), ++ ) ++ with file: ++ conf.read_file(file) ++ name = conf.get('tarfile', ++ 'PYTHON_TARFILE_EXTRACTION_FILTER', ++ fallback='') ++ if name: ++ try: ++ filter = _NAMED_FILTERS[name] ++ except KeyError: ++ raise ValueError(f"filter {filter!r} not found") from None ++ self.extraction_filter = filter ++ return filter ++ if _RH_SAFER_DEFAULT: ++ warnings.warn( ++ 'The default behavior of tarfile extraction has been ' ++ + 'changed to disallow common exploits ' ++ + '(including CVE-2007-4559). ' ++ + 'By default, absolute/parent paths are disallowed ' ++ + 'and some mode bits are cleared. ' ++ + 'See https://access.redhat.com/articles/7004769 ' ++ + 'for more details.', ++ RuntimeWarning) ++ return tar_filter + return fully_trusted_filter + if isinstance(filter, str): + raise TypeError( +diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py +index 5cef59ea9c6..73fffe0fd33 100644 +--- a/Lib/test/test_shutil.py ++++ b/Lib/test/test_shutil.py +@@ -1494,7 +1494,8 @@ class TestShutil(unittest.TestCase): + def check_unpack_tarball(self, format): + self.check_unpack_archive(format, filter='fully_trusted') + self.check_unpack_archive(format, filter='data') +- with support.check_no_warnings(self): ++ with support.check_warnings( ++ ('.*CVE-2007-4559', RuntimeWarning)): + self.check_unpack_archive(format) + + def test_unpack_archive_tar(self): +diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py +index 03be10b1fee..15df6a9ced6 100644 +--- a/Lib/test/test_tarfile.py ++++ b/Lib/test/test_tarfile.py +@@ -2,7 +2,7 @@ + import os + import io + from hashlib import sha256 +-from contextlib import contextmanager ++from contextlib import contextmanager, ExitStack + from random import Random + import pathlib + import shutil +@@ -2744,7 +2744,11 @@ class NoneInfoExtractTests(ReadTest): + tar = tarfile.open(tarname, mode='r', encoding="iso8859-1") + cls.control_dir = pathlib.Path(TEMPDIR) / "extractall_ctrl" + tar.errorlevel = 0 +- tar.extractall(cls.control_dir, filter=cls.extraction_filter) ++ with ExitStack() as cm: ++ if cls.extraction_filter is None: ++ cm.enter_context(warnings.catch_warnings()) ++ warnings.simplefilter(action="ignore", category=RuntimeWarning) ++ tar.extractall(cls.control_dir, filter=cls.extraction_filter) + tar.close() + cls.control_paths = set( + p.relative_to(cls.control_dir) +@@ -3407,7 +3411,8 @@ class TestExtractionFilters(unittest.TestCase): + """Ensure the default filter does not warn (like in 3.12)""" + with ArchiveMaker() as arc: + arc.add('foo') +- with support.check_no_warnings(self): ++ with support.check_warnings( ++ ('.*CVE-2007-4559', RuntimeWarning)): + with self.check_context(arc.open(), None): + self.expect_file('foo') + +@@ -3577,6 +3582,119 @@ class TestExtractionFilters(unittest.TestCase): + self.expect_exception(TypeError) # errorlevel is not int + + ++ @contextmanager ++ def rh_config_context(self, config_lines=None): ++ """Set up for testing various ways of overriding the default filter ++ ++ return a triple with: ++ - temporary directory ++ - EnvironmentVarGuard() ++ - a test archive for use with check_* methods below ++ ++ If config_lines is given, write them to the config file. Otherwise ++ the config file is missing. ++ """ ++ tempdir = pathlib.Path(TEMPDIR) / 'tmp' ++ configfile = tempdir / 'tarfile.cfg' ++ with ArchiveMaker() as arc: ++ arc.add('good') ++ arc.add('ugly', symlink_to='/etc/passwd') ++ arc.add('../bad') ++ with ExitStack() as cm: ++ cm.enter_context(support.temp_dir(tempdir)) ++ cm.enter_context(support.swap_attr(tarfile, '_CONFIG_FILENAME', str(configfile))) ++ env = cm.enter_context(support.EnvironmentVarGuard()) ++ tar = cm.enter_context(arc.open()) ++ if config_lines is not None: ++ with configfile.open('w') as f: ++ for line in config_lines: ++ print(line, file=f) ++ yield tempdir, env, tar ++ ++ def check_rh_default_behavior(self, tar, tempdir): ++ """Check RH default: warn and refuse to extract dangerous files.""" ++ with ExitStack() as cm: ++ cm.enter_context(support.check_warnings( ++ ('.*CVE-2007-4559', RuntimeWarning))) ++ cm.enter_context(self.assertRaises(tarfile.OutsideDestinationError)) ++ tar.extractall(tempdir / 'outdir') ++ ++ def check_trusted_default(self, tar, tempdir): ++ """Check 'fully_trusted' is configured as the default filter.""" ++ with support.check_no_warnings(self): ++ tar.extractall(tempdir / 'outdir') ++ self.assertTrue((tempdir / 'outdir/good').exists()) ++ self.assertEqual(os.readlink(str(tempdir / 'outdir/ugly')), ++ '/etc/passwd') ++ self.assertTrue((tempdir / 'bad').exists()) ++ ++ def test_rh_default_no_conf(self): ++ with self.rh_config_context() as (tempdir, env, tar): ++ self.check_rh_default_behavior(tar, tempdir) ++ ++ def test_rh_default_from_file(self): ++ lines = ['[tarfile]', 'PYTHON_TARFILE_EXTRACTION_FILTER=fully_trusted'] ++ with self.rh_config_context(lines) as (tempdir, env, tar): ++ self.check_trusted_default(tar, tempdir) ++ ++ def test_rh_empty_config_file(self): ++ """Empty config file -> default behavior""" ++ lines = [] ++ with self.rh_config_context(lines) as (tempdir, env, tar): ++ self.check_rh_default_behavior(tar, tempdir) ++ ++ def test_empty_config_section(self): ++ """Empty section in config file -> default behavior""" ++ lines = ['[tarfile]'] ++ with self.rh_config_context(lines) as (tempdir, env, tar): ++ self.check_rh_default_behavior(tar, tempdir) ++ ++ def test_rh_default_empty_config_option(self): ++ """Empty option value in config file -> default behavior""" ++ lines = ['[tarfile]', 'PYTHON_TARFILE_EXTRACTION_FILTER='] ++ with self.rh_config_context(lines) as (tempdir, env, tar): ++ self.check_rh_default_behavior(tar, tempdir) ++ ++ def test_bad_config_option(self): ++ """Bad option value in config file -> ValueError""" ++ lines = ['[tarfile]', 'PYTHON_TARFILE_EXTRACTION_FILTER=unknown!'] ++ with self.rh_config_context(lines) as (tempdir, env, tar): ++ with self.assertRaises(ValueError): ++ tar.extractall(tempdir / 'outdir') ++ ++ def test_default_from_envvar(self): ++ with self.rh_config_context() as (tempdir, env, tar): ++ env['PYTHON_TARFILE_EXTRACTION_FILTER'] = 'fully_trusted' ++ self.check_trusted_default(tar, tempdir) ++ ++ def test_empty_envvar(self): ++ """Empty env variable -> default behavior""" ++ with self.rh_config_context() as (tempdir, env, tar): ++ env['PYTHON_TARFILE_EXTRACTION_FILTER'] = '' ++ self.check_rh_default_behavior(tar, tempdir) ++ ++ def test_bad_envvar(self): ++ with self.rh_config_context() as (tempdir, env, tar): ++ env['PYTHON_TARFILE_EXTRACTION_FILTER'] = 'unknown!' ++ with self.assertRaises(ValueError): ++ tar.extractall(tempdir / 'outdir') ++ ++ def test_envvar_overrides_file(self): ++ lines = ['[tarfile]', 'PYTHON_TARFILE_EXTRACTION_FILTER=data'] ++ with self.rh_config_context(lines) as (tempdir, env, tar): ++ env['PYTHON_TARFILE_EXTRACTION_FILTER'] = 'fully_trusted' ++ self.check_trusted_default(tar, tempdir) ++ ++ def test_monkeypatch_overrides_envvar(self): ++ with self.rh_config_context(None) as (tempdir, env, tar): ++ env['PYTHON_TARFILE_EXTRACTION_FILTER'] = 'data' ++ with support.swap_attr( ++ tarfile.TarFile, 'extraction_filter', ++ staticmethod(tarfile.fully_trusted_filter) ++ ): ++ self.check_trusted_default(tar, tempdir) ++ ++ + def setUpModule(): + support.unlink(TEMPDIR) + os.makedirs(TEMPDIR) +-- +2.41.0 + diff --git a/python38.spec b/python38.spec index bfd2eba..a0b6b82 100644 --- a/python38.spec +++ b/python38.spec @@ -13,11 +13,11 @@ URL: https://www.python.org/ # WARNING When rebasing to a new Python version, # remember to update the python3-docs package as well -%global general_version %{pybasever}.16 +%global general_version %{pybasever}.17 #global prerel ... %global upstream_version %{general_version}%{?prerel} Version: %{general_version}%{?prerel:~%{prerel}} -Release: 2%{?dist} +Release: 1%{?dist} License: Python # Exclude i686 arch. Due to a modularity issue it's being added to the @@ -382,6 +382,12 @@ Patch359: 00359-CVE-2021-23336.patch # Upstream: https://bugs.python.org/issue46811 Patch378: 00378-support-expat-2-4-5.patch +# 00397 # +# Add Red Hat configuration for tarfile extraction (CVE-2007-4559, PEP-706) +# See KB for documentation: +# - https://access.redhat.com/articles/7004769 +Patch397: 00397-tarfile-filter.patch + # (New patches go here ^^^) # # When adding new patches to "python" and "python3" in Fedora, EL, etc., @@ -505,8 +511,8 @@ Summary: Python runtime libraries Requires: python38-setuptools-wheel Requires: python38-pip-wheel %else -Provides: bundled(python38-pip) = 19.2.3 -Provides: bundled(python38-setuptools) = 41.2.0 +Provides: bundled(python38-pip) = 23.0.1 +Provides: bundled(python38-setuptools) = 56.0.0 %endif %{?python_provide:%python_provide python38-libs} @@ -674,7 +680,7 @@ The debug runtime additionally supports debug builds of C-API extensions Requires: python38-setuptools-wheel Requires: python38-pip-wheel %else -Provides: bundled(python38-pip) = 21.1.1 +Provides: bundled(python38-pip) = 23.0.1 Provides: bundled(python38-setuptools) = 56.0.0 %endif @@ -739,6 +745,7 @@ rm Lib/ensurepip/_bundled/*.whl %patch353 -p1 %patch359 -p1 %patch378 -p1 +%patch397 -p1 # Remove files that should be generated by the build # (This is after patching, so that we can use patches directly from upstream) @@ -1832,6 +1839,12 @@ fi # ====================================================== %changelog +* Mon Aug 07 2023 Charalampos Stratakis - 3.8.17-1 +- Update to 3.8.17 +- Security fix for CVE-2023-24329 +- Add filters for tarfile extraction (CVE-2007-4559, PEP-706) +Resolves: rhbz#2173917, rhbz#263261 + * Tue Jul 18 2023 Charalampos Stratakis - 3.8.16-2 - Strip the LTO bytecode from python.o Resolves: rhbz#2213526 diff --git a/sources b/sources index 254949e..88aafb1 100644 --- a/sources +++ b/sources @@ -1 +1 @@ -SHA512 (Python-3.8.16-noexe.tar.xz) = a8f2f4e1dd3423efbd3636909c1dce0486721909ebdaf68581f8a554ee7c4722b8a0c813439d48842dd41a05a5452db492350824b83cae92100453ae69686d4d +SHA512 (Python-3.8.17-noexe.tar.xz) = bce80c6c481f1c8677210f352f5a1c48bbcdbf8f5db4656c6e8bcf08ea6a66d541a837d0c4fe32fd7dd1d72e8f245c4e8b8c1cbe704e44d428a1faec7c6f46c2