Skip to content

Commit 0902a2d

Browse files
miss-islingtontim-onetirandavisjam
authored andcommitted
bpo-32981: Fix catastrophic backtracking vulns (GH-5955)
* Prevent low-grade poplib REDOS (CVE-2018-1060) The regex to test a mail server's timestamp is susceptible to catastrophic backtracking on long evil responses from the server. Happily, the maximum length of malicious inputs is 2K thanks to a limit introduced in the fix for CVE-2013-1752. A 2KB evil response from the mail server would result in small slowdowns (milliseconds vs. microseconds) accumulated over many apop calls. This is a potential DOS vector via accumulated slowdowns. Replace it with a similar non-vulnerable regex. The new regex is RFC compliant. The old regex was non-compliant in edge cases. * Prevent difflib REDOS (CVE-2018-1061) The default regex for IS_LINE_JUNK is susceptible to catastrophic backtracking. This is a potential DOS vector. Replace it with an equivalent non-vulnerable regex. Also introduce unit and REDOS tests for difflib. Co-authored-by: Tim Peters <tim.peters@gmail.com> Co-authored-by: Christian Heimes <christian@python.org> Co-authored-by: Jamie Davis <davisjam@vt.edu> (cherry picked from commit 0e6c8ee)
1 parent 0f7cf7e commit 0902a2d

File tree

6 files changed

+39
-4
lines changed

6 files changed

+39
-4
lines changed

Lib/difflib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,7 @@ def _qformat(self, aline, bline, atags, btags):
10831083

10841084
import re
10851085

1086-
def IS_LINE_JUNK(line, pat=re.compile(r"\s*#?\s*$").match):
1086+
def IS_LINE_JUNK(line, pat=re.compile(r"\s*(?:#\s*)?$").match):
10871087
r"""
10881088
Return 1 for ignorable line: iff `line` is blank or contains a single '#'.
10891089

Lib/poplib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ def rpop(self, user):
308308
return self._shortcmd('RPOP %s' % user)
309309

310310

311-
timestamp = re.compile(br'\+OK.*(<[^>]+>)')
311+
timestamp = re.compile(br'\+OK.[^<]*(<.*>)')
312312

313313
def apop(self, user, password):
314314
"""Authorisation

Lib/test/test_difflib.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,13 +466,33 @@ def _assert_type_error(self, msg, generator, *args):
466466
list(generator(*args))
467467
self.assertEqual(msg, str(ctx.exception))
468468

469+
class TestJunkAPIs(unittest.TestCase):
470+
def test_is_line_junk_true(self):
471+
for line in ['#', ' ', ' #', '# ', ' # ', '']:
472+
self.assertTrue(difflib.IS_LINE_JUNK(line), repr(line))
473+
474+
def test_is_line_junk_false(self):
475+
for line in ['##', ' ##', '## ', 'abc ', 'abc #', 'Mr. Moose is up!']:
476+
self.assertFalse(difflib.IS_LINE_JUNK(line), repr(line))
477+
478+
def test_is_line_junk_REDOS(self):
479+
evil_input = ('\t' * 1000000) + '##'
480+
self.assertFalse(difflib.IS_LINE_JUNK(evil_input))
481+
482+
def test_is_character_junk_true(self):
483+
for char in [' ', '\t']:
484+
self.assertTrue(difflib.IS_CHARACTER_JUNK(char), repr(char))
485+
486+
def test_is_character_junk_false(self):
487+
for char in ['a', '#', '\n', '\f', '\r', '\v']:
488+
self.assertFalse(difflib.IS_CHARACTER_JUNK(char), repr(char))
469489

470490
def test_main():
471491
difflib.HtmlDiff._default_prefix = 0
472492
Doctests = doctest.DocTestSuite(difflib)
473493
run_unittest(
474494
TestWithAscii, TestAutojunk, TestSFpatches, TestSFbugs,
475-
TestOutputFormat, TestBytes, Doctests)
495+
TestOutputFormat, TestBytes, TestJunkAPIs, Doctests)
476496

477497
if __name__ == '__main__':
478498
test_main()

Lib/test/test_poplib.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,19 @@ def test_noop(self):
308308
def test_rpop(self):
309309
self.assertOK(self.client.rpop('foo'))
310310

311-
def test_apop(self):
311+
def test_apop_normal(self):
312312
self.assertOK(self.client.apop('foo', 'dummypassword'))
313313

314+
def test_apop_REDOS(self):
315+
# Replace welcome with very long evil welcome.
316+
# NB The upper bound on welcome length is currently 2048.
317+
# At this length, evil input makes each apop call take
318+
# on the order of milliseconds instead of microseconds.
319+
evil_welcome = b'+OK' + (b'<' * 1000000)
320+
with test_support.swap_attr(self.client, 'welcome', evil_welcome):
321+
# The evil welcome is invalid, so apop should throw.
322+
self.assertRaises(poplib.error_proto, self.client.apop, 'a', 'kb')
323+
314324
def test_top(self):
315325
expected = (b'+OK 116 bytes',
316326
[b'From: postmaster@python.org', b'Content-Type: text/plain',

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ Jonathan Dasteel
356356
Pierre-Yves David
357357
A. Jesse Jiryu Davis
358358
Jake Davis
359+
Jamie (James C.) Davis
359360
Ratnadeep Debnath
360361
Merlijn van Deen
361362
John DeGood
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Regexes in difflib and poplib were vulnerable to catastrophic backtracking.
2+
These regexes formed potential DOS vectors (REDOS). They have been
3+
refactored. This resolves CVE-2018-1060 and CVE-2018-1061.
4+
Patch by Jamie Davis.

0 commit comments

Comments
 (0)