summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRishi <rishi_devan@mail.com>2020-07-15 13:51:00 +0200
committerPetr Viktorin <pviktori@redhat.com>2020-07-15 17:28:12 +0200
commitbde4372aa1f39803f5c33f1415e88b000d403d00 (patch)
tree9532089be9064f42f8e6a4a22bcac186fe8c3445
parentf52bf62fe12d46267e958f80dbe1f4425b55cd0f (diff)
downloadcpython-git-backport-5a8d121-3.5.tar.gz
[3.5] bpo-39017: Avoid infinite loop in the tarfile module (GH-21454)backport-5a8d121-3.5
Avoid infinite loop when reading specially crafted TAR files using the tarfile module (CVE-2019-20907). (cherry picked from commit 5a8d121a1f3ef5ad7c105ee378cc79a3eac0c7d4) Co-authored-by: Rishi <rishi_devan@mail.com>
-rwxr-xr-xLib/tarfile.py2
-rw-r--r--Lib/test/recursion.tarbin0 -> 516 bytes
-rw-r--r--Lib/test/test_tarfile.py7
-rw-r--r--Misc/NEWS.d/next/Library/2020-07-12-22-16-58.bpo-39017.x3Cg-9.rst1
4 files changed, 10 insertions, 0 deletions
diff --git a/Lib/tarfile.py b/Lib/tarfile.py
index 999a99b978..8015fae7a6 100755
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -1221,6 +1221,8 @@ class TarInfo(object):
length, keyword = match.groups()
length = int(length)
+ if length == 0:
+ raise InvalidHeaderError("invalid header")
value = buf[match.end(2) + 1:match.start(1) + length - 1]
# Normally, we could just use "utf-8" as the encoding and "strict"
diff --git a/Lib/test/recursion.tar b/Lib/test/recursion.tar
new file mode 100644
index 0000000000..b823725196
--- /dev/null
+++ b/Lib/test/recursion.tar
Binary files differ
diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py
index 1efb841bb2..2438fd3abf 100644
--- a/Lib/test/test_tarfile.py
+++ b/Lib/test/test_tarfile.py
@@ -394,6 +394,13 @@ class CommonReadTest(ReadTest):
with self.assertRaisesRegex(tarfile.ReadError, "unexpected end of data"):
tar.extractfile(t).read()
+ def test_length_zero_header(self):
+ # bpo-39017 (CVE-2019-20907): reading a zero-length header should fail
+ # with an exception
+ with self.assertRaisesRegex(tarfile.ReadError, "file could not be opened successfully"):
+ with tarfile.open(support.findfile('recursion.tar')) as tar:
+ pass
+
class MiscReadTestBase(CommonReadTest):
def requires_name_attribute(self):
pass
diff --git a/Misc/NEWS.d/next/Library/2020-07-12-22-16-58.bpo-39017.x3Cg-9.rst b/Misc/NEWS.d/next/Library/2020-07-12-22-16-58.bpo-39017.x3Cg-9.rst
new file mode 100644
index 0000000000..ad26676f8b
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-07-12-22-16-58.bpo-39017.x3Cg-9.rst
@@ -0,0 +1 @@
+Avoid infinite loop when reading specially crafted TAR files using the tarfile module (CVE-2019-20907).