Skip to content

Commit 81b28b7

Browse files
serhiy-storchakamiss-islington
authored andcommitted
[3.14] pythongh-119452: Fix a potential virtual memory allocation denial of service in http.server (pythonGH-142216)
The CGI server on Windows could consume the amount of memory specified in the Content-Length header of the request even if the client does not send such much data. Now it reads the POST request body by chunks, therefore the memory consumption is proportional to the amount of sent data. (cherry picked from commit 0e4f4f1) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 9bbb68a commit 81b28b7

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

Lib/http/server.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@
127127

128128
DEFAULT_ERROR_CONTENT_TYPE = "text/html;charset=utf-8"
129129

130+
# Data larger than this will be read in chunks, to prevent extreme
131+
# overallocation.
132+
_MIN_READ_BUF_SIZE = 1 << 20
133+
130134
class HTTPServer(socketserver.TCPServer):
131135

132136
allow_reuse_address = 1 # Seems to make sense in testing environment
@@ -1217,7 +1221,18 @@ def run_cgi(self):
12171221
env = env
12181222
)
12191223
if self.command.lower() == "post" and nbytes > 0:
1220-
data = self.rfile.read(nbytes)
1224+
cursize = 0
1225+
data = self.rfile.read(min(nbytes, _MIN_READ_BUF_SIZE))
1226+
while len(data) < nbytes and len(data) != cursize:
1227+
cursize = len(data)
1228+
# This is a geometric increase in read size (never more
1229+
# than doubling out the current length of data per loop
1230+
# iteration).
1231+
delta = min(cursize, nbytes - cursize)
1232+
try:
1233+
data += self.rfile.read(delta)
1234+
except TimeoutError:
1235+
break
12211236
else:
12221237
data = None
12231238
# throw away additional data [see bug #427345]

Lib/test/test_httpservers.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,20 @@ def test_html_escape_filename(self):
694694
print("</pre>")
695695
"""
696696

697+
cgi_file7 = """\
698+
#!%s
699+
import os
700+
import sys
701+
702+
print("Content-type: text/plain")
703+
print()
704+
705+
content_length = int(os.environ["CONTENT_LENGTH"])
706+
body = sys.stdin.buffer.read(content_length)
707+
708+
print(f"{content_length} {len(body)}")
709+
"""
710+
697711

698712
@unittest.skipIf(hasattr(os, 'geteuid') and os.geteuid() == 0,
699713
"This test can't be run reliably as root (issue #13308).")
@@ -723,6 +737,8 @@ def setUp(self):
723737
self.file3_path = None
724738
self.file4_path = None
725739
self.file5_path = None
740+
self.file6_path = None
741+
self.file7_path = None
726742

727743
# The shebang line should be pure ASCII: use symlink if possible.
728744
# See issue #7668.
@@ -777,6 +793,11 @@ def setUp(self):
777793
file6.write(cgi_file6 % self.pythonexe)
778794
os.chmod(self.file6_path, 0o777)
779795

796+
self.file7_path = os.path.join(self.cgi_dir, 'file7.py')
797+
with open(self.file7_path, 'w', encoding='utf-8') as file7:
798+
file7.write(cgi_file7 % self.pythonexe)
799+
os.chmod(self.file7_path, 0o777)
800+
780801
os.chdir(self.parent_dir)
781802

782803
def tearDown(self):
@@ -798,6 +819,8 @@ def tearDown(self):
798819
os.remove(self.file5_path)
799820
if self.file6_path:
800821
os.remove(self.file6_path)
822+
if self.file7_path:
823+
os.remove(self.file7_path)
801824
os.rmdir(self.cgi_child_dir)
802825
os.rmdir(self.cgi_dir)
803826
os.rmdir(self.cgi_dir_in_sub_dir)
@@ -867,6 +890,22 @@ def test_post(self):
867890

868891
self.assertEqual(res.read(), b'1, python, 123456' + self.linesep)
869892

893+
def test_large_content_length(self):
894+
for w in range(15, 25):
895+
size = 1 << w
896+
body = b'X' * size
897+
headers = {'Content-Length' : str(size)}
898+
res = self.request('/cgi-bin/file7.py', 'POST', body, headers)
899+
self.assertEqual(res.read(), b'%d %d' % (size, size) + self.linesep)
900+
901+
def test_large_content_length_truncated(self):
902+
with support.swap_attr(self.request_handler, 'timeout', 0.001):
903+
for w in range(18, 65):
904+
size = 1 << w
905+
headers = {'Content-Length' : str(size)}
906+
res = self.request('/cgi-bin/file1.py', 'POST', b'x', headers)
907+
self.assertEqual(res.read(), b'Hello World' + self.linesep)
908+
870909
def test_invaliduri(self):
871910
res = self.request('/cgi-bin/invalid')
872911
res.read()
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Fix a potential memory denial of service in the :mod:`http.server` module.
2+
When a malicious user is connected to the CGI server on Windows, it could cause
3+
an arbitrary amount of memory to be allocated.
4+
This could have led to symptoms including a :exc:`MemoryError`, swapping, out
5+
of memory (OOM) killed processes or containers, or even system crashes.

0 commit comments

Comments
 (0)