From 1fa6ef2bc7cee1c8e088dd8b397d9b2d54036dbc Mon Sep 17 00:00:00 2001 From: Rajarishi Devarajan Date: Sun, 12 Jul 2020 23:47:42 +0200 Subject: [PATCH] bpo-39017 Fix infinite loop in the tarfile module Add a check for length = 0 in the _proc_pax function to avoid running into an infinite loop Signed-off-by:Rajarishi Devarajan --- Lib/tarfile.py | 2 + Lib/test/recursion.tar | 1017 +++++++++++++++++ Lib/test/test_tarfile.py | 7 + .../2020-07-12-22-16-58.bpo-39017.x3Cg-9.rst | 1 + 4 files changed, 1027 insertions(+) create mode 100644 Lib/test/recursion.tar create mode 100644 Misc/NEWS.d/next/Library/2020-07-12-22-16-58.bpo-39017.x3Cg-9.rst diff --git a/Lib/tarfile.py b/Lib/tarfile.py index 85119a4..00b102b 100755 --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -1231,6 +1231,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 0000000..a56ed9c --- /dev/null +++ b/Lib/test/recursion.tar @@ -0,0 +1,1017 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + cpython/recursion.tar at master · python/cpython · GitHub + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ Skip to content + + + + + + + + + + + +
+ +
+ + + + +
+ + + +
+ + + + + + + + +
+
+
+ + + + + + + + + + + + + + + + +
+ +
+ +
+

+ + + / + + cpython + + +

+ + +
+ + + +
+ + +
+ +
+
+ + + + + + + + Permalink + + + + + +
+ +
+ + + + Branch: + master + + + + +
+ + + +
+
+
+ + + + Go to file + + +
+ + + + + + + + +
+ + + +
+ +
+
+ + @rishi93 + + +
+ + Latest commit + 5a8d121 + Jul 15, 2020 + + + + + + + History + + +
+
+
Avoid infinite loop when reading specially crafted TAR files using the tarfile module
+(CVE-2019-20907).
+ +
+ +
+
+ + + + 1 + + contributor + + +
+ +

+ Users who have contributed to this file +

+
+ +
+
+
+
+ + + + + + +
+ +
+
+ + + 1 lines (1 sloc) + + 516 Bytes +
+ +
+ +
+ Raw + Blame +
+ +
+ + + + + + +
+
+
+ + + + + +
+ + + + + + +
bcaller00010002755 g00000 X=
+ + + +
+ +
+ + + + +
+ + +
+ + +
+
+ + + + +
+
+ +
+
+ + +
+ + + + + + +
+ + + You can’t perform that action at this time. +
+ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py index 7d2eec8..ce820d1 100644 --- a/Lib/test/test_tarfile.py +++ b/Lib/test/test_tarfile.py @@ -395,6 +395,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 0000000..ad26676 --- /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). -- 2.19.1