RHEL Engineering is moving the tracking of its product development work on RHEL 6 through RHEL 9 to Red Hat Jira (issues.redhat.com). If you're a Red Hat customer, please continue to file support cases via the Red Hat customer portal. If you're not, please head to the "RHEL project" in Red Hat Jira and file new tickets here. Individual Bugzilla bugs in the statuses "NEW", "ASSIGNED", and "POST" are being migrated throughout September 2023. Bugs of Red Hat partners with an assigned Engineering Partner Manager (EPM) are migrated in late September as per pre-agreed dates. Bugs against components "kernel", "kernel-rt", and "kpatch" are only migrated if still in "NEW" or "ASSIGNED". If you cannot log in to RH Jira, please consult article #7032570. That failing, please send an e-mail to the RH Jira admins at rh-issues@redhat.com to troubleshoot your issue as a user management inquiry. The email creates a ServiceNow ticket with Red Hat. Individual Bugzilla bugs that are migrated will be moved to status "CLOSED", resolution "MIGRATED", and set with "MigratedToJIRA" in "Keywords". The link to the successor Jira issue will be found under "Links", have a little "two-footprint" icon next to it, and direct you to the "RHEL project" in Red Hat Jira (issue links are of type "https://issues.redhat.com/browse/RHEL-XXXX", where "X" is a digit). This same link will be available in a blue banner at the top of the page informing you that that bug has been migrated.
Bug 2218237 - [RHEL8] python3-samba: Python tarfile extraction needs change to avoid a warning (CVE-2007-4559 mitigation)
Summary: [RHEL8] python3-samba: Python tarfile extraction needs change to avoid a warn...
Keywords:
Status: CLOSED ERRATA
Alias: None
Product: Red Hat Enterprise Linux 8
Classification: Red Hat
Component: samba
Version: 8.9
Hardware: Unspecified
OS: Unspecified
unspecified
unspecified
Target Milestone: rc
: ---
Assignee: Andreas Schneider
QA Contact: Denis Karpelevich
URL:
Whiteboard:
Depends On:
Blocks: CVE-2007-4559
TreeView+ depends on / blocked
 
Reported: 2023-06-28 14:05 UTC by Charalampos Stratakis
Modified: 2023-11-14 18:10 UTC (History)
5 users (show)

Fixed In Version: samba-4.18.4-1.el8
Doc Type: If docs needed, set a value
Doc Text:
Clone Of: 2207692
Environment:
Last Closed: 2023-11-14 15:50:31 UTC
Type: Bug
Target Upstream Version:
Embargoed:


Attachments (Terms of Use)


Links
System ID Private Priority Status Summary Last Updated
Red Hat Issue Tracker RHELPLAN-161095 0 None None None 2023-06-28 14:08:09 UTC
Red Hat Issue Tracker SSSD-6362 0 None None None 2023-06-28 14:08:23 UTC
Red Hat Product Errata RHSA-2023:7139 0 None None None 2023-11-14 15:51:05 UTC

Description Charalampos Stratakis 2023-06-28 14:05:09 UTC
+++ This bug was initially created as a clone of Bug #2207692 +++

Hello,
In RHEL 9.3 and 8.9, we're planning to fix the long-standing CVE-2007-4559: Python's `tarfile` module makes it too easy to extract tarballs in an unsafe way.
Unfortunately, for the CVE to be considered fixed, this needs a behavior change. (If you don't think this is the case, let's bring it up with the security team.)
Upstream, Python will emit deprecation warnings for 2 releases, but in RHEL we change the behavior now, emit warnings, and provide ways for customers to restore earlier behavior.
To avoid the warning, software shipped by Red Hat will need a change.

For more details see upstream PEP 706: https://peps.python.org/pep-0706
and the Red Hat knowledge base draft: https://access.redhat.com/articles/7004769

---

In ntacls.py and in netcmd/domain_backup.py in /usr/lib*/python*/site-packages/samba/, python3-samba calls `tf.extractall(targetdir)` and `f.extractall(path=tempdir)`.
The calls will emit a warning by default, and tar features deemed too dangerous for general use will even be blocked by default.

I am not sure what archive is being extracted. If this is a trusted backup, please add the following before the extract call:

    # The archive is fully trusted, override warnings and "safe" defaults
    # see https://docs.python.org/3.12/library/tarfile.html#supporting-older-python-versions
    tf.extraction_filter = (lambda member, path: member)

This will restore previous behaviour, and it's a no-op on earlier Python versions.

In case this tarball is not always fully trusted, please let me know privately.

---

Let me know if you have any questions!

--- Additional comment from Andreas Schneider on 2023-06-03 15:23:27 CEST ---

Samba has a python/samba/safe_tarfile.py [1] which is used by netcmd/domain_backup.py and others. However it doesn't overwrite extractall() only extract(). But looking at the test at [2] extratall seems to be covered as well.

So if we pass filter='data'


I think if we just pass filter='data' always to tarfile extract() we are fine? Or do we need to also add extractall() as it will print the warning otherwise, then we need to also implement extractall() in python/samba/safe_tarfile.py



 python/samba/safe_tarfile.py | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/python/samba/safe_tarfile.py b/python/samba/safe_tarfile.py
index cc19770d73f..489ccb1a65b 100644
--- a/python/samba/safe_tarfile.py
+++ b/python/samba/safe_tarfile.py
@@ -24,7 +24,8 @@ class TarFile(UnsafeTarFile):
     using '../../'.
     """
 
-    def extract(self, member, path="", set_attrs=True, *, numeric_owner=False):
+    def extract(self, member, path="", set_attrs=True, *, numeric_owner=False,
+                filter="data"):
         if isinstance(member, TarInfo):
             name = member.name
         else:
@@ -37,7 +38,7 @@ class TarFile(UnsafeTarFile):
             raise ExtractError(f"path '{name}' should not start with '/'")
 
         super().extract(member, path, set_attrs=set_attrs,
-                        numeric_owner=numeric_owner)
+                        numeric_owner=numeric_owner, filter=filter)
 
 
 open = TarFile.open


[1] https://gitlab.com/samba-team/samba/-/blob/master/python/samba/safe_tarfile.py
[2] https://gitlab.com/samba-team/samba/-/blob/master/python/samba/tests/safe_tarfile.py

--- Additional comment from Petr Viktorin on 2023-06-06 10:07:37 CEST ---

Note that SafeTarFile will not prevent symlink attacks, e.g. one tar member setting up `./etc -> /etc`, and the next one extracting to `./etc/passwd`.

--- Additional comment from Petr Viktorin on 2023-06-06 10:24:57 CEST ---

For passing the filter, you'd also need to provide `extractall`. Beware that `extractall` does *not* call `extract`, they're speparate "frontends" for the internals.
Also, the filter argument isn't available on older/unpatched versions of Python, so you probably can't add it unconditionally.

But since you have a subclass, things are easier: you can just add an attribute to it:

    import tarfile

    class TarFile(UnsafeTarFile):
        try:
            extraction_filter = staticmethod(tarfile.data_filter)
        except AttributeError:
            ... # override extract/extractall for earlier/unpatched versions

See the docs: https://docs.python.org/3/library/tarfile.html#tarfile.TarFile.extraction_filter
(Please let me know if anything is unclear/missing, Samba would be the first project I know doing it this way.)


Note that the errors raised are subclasses of TarError, but *not* ExtractError like your existing safe TarFile.

--- Additional comment from Andreas Schneider on 2023-06-07 11:05:25 CEST ---

Upstream Merge Request: https://gitlab.com/samba-team/samba/-/merge_requests/3114

Comment 5 errata-xmlrpc 2023-11-14 15:50:31 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory (Moderate: samba security, bug fix, and enhancement update), and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

https://access.redhat.com/errata/RHSA-2023:7139


Note You need to log in before you can comment on or make changes to this bug.