Skip to content

Commit 9303573

Browse files
miss-islingtonserhiy-storchakahugovk
authored
[3.13] gh-119452: Fix a potential virtual memory allocation denial of service in http.server (GH-142216) (#142296)
[3.14] gh-119452: Fix a potential virtual memory allocation denial of service in http.server (GH-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> Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
1 parent ddcd2ac commit 9303573

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
@@ -1234,7 +1238,18 @@ def run_cgi(self):
12341238
env = env
12351239
)
12361240
if self.command.lower() == "post" and nbytes > 0:
1237-
data = self.rfile.read(nbytes)
1241+
cursize = 0
1242+
data = self.rfile.read(min(nbytes, _MIN_READ_BUF_SIZE))
1243+
while len(data) < nbytes and len(data) != cursize:
1244+
cursize = len(data)
1245+
# This is a geometric increase in read size (never more
1246+
# than doubling out the current length of data per loop
1247+
# iteration).
1248+
delta = min(cursize, nbytes - cursize)
1249+
try:
1250+
data += self.rfile.read(delta)
1251+
except TimeoutError:
1252+
break
12381253
else:
12391254
data = None
12401255
# 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
@@ -802,6 +802,20 @@ def test_path_without_leading_slash(self):
802802
print("</pre>")
803803
"""
804804

805+
cgi_file7 = """\
806+
#!%s
807+
import os
808+
import sys
809+
810+
print("Content-type: text/plain")
811+
print()
812+
813+
content_length = int(os.environ["CONTENT_LENGTH"])
814+
body = sys.stdin.buffer.read(content_length)
815+
816+
print(f"{content_length} {len(body)}")
817+
"""
818+
805819

806820
@unittest.skipIf(hasattr(os, 'geteuid') and os.geteuid() == 0,
807821
"This test can't be run reliably as root (issue #13308).")
@@ -841,6 +855,8 @@ def setUp(self):
841855
self.file3_path = None
842856
self.file4_path = None
843857
self.file5_path = None
858+
self.file6_path = None
859+
self.file7_path = None
844860

845861
# The shebang line should be pure ASCII: use symlink if possible.
846862
# See issue #7668.
@@ -895,6 +911,11 @@ def setUp(self):
895911
file6.write(cgi_file6 % self.pythonexe)
896912
os.chmod(self.file6_path, 0o777)
897913

914+
self.file7_path = os.path.join(self.cgi_dir, 'file7.py')
915+
with open(self.file7_path, 'w', encoding='utf-8') as file7:
916+
file7.write(cgi_file7 % self.pythonexe)
917+
os.chmod(self.file7_path, 0o777)
918+
898919
os.chdir(self.parent_dir)
899920

900921
def tearDown(self):
@@ -917,6 +938,8 @@ def tearDown(self):
917938
os.remove(self.file5_path)
918939
if self.file6_path:
919940
os.remove(self.file6_path)
941+
if self.file7_path:
942+
os.remove(self.file7_path)
920943
os.rmdir(self.cgi_child_dir)
921944
os.rmdir(self.cgi_dir)
922945
os.rmdir(self.cgi_dir_in_sub_dir)
@@ -989,6 +1012,22 @@ def test_post(self):
9891012

9901013
self.assertEqual(res.read(), b'1, python, 123456' + self.linesep)
9911014

1015+
def test_large_content_length(self):
1016+
for w in range(15, 25):
1017+
size = 1 << w
1018+
body = b'X' * size
1019+
headers = {'Content-Length' : str(size)}
1020+
res = self.request('/cgi-bin/file7.py', 'POST', body, headers)
1021+
self.assertEqual(res.read(), b'%d %d' % (size, size) + self.linesep)
1022+
1023+
def test_large_content_length_truncated(self):
1024+
with support.swap_attr(self.request_handler, 'timeout', 0.001):
1025+
for w in range(18, 65):
1026+
size = 1 << w
1027+
headers = {'Content-Length' : str(size)}
1028+
res = self.request('/cgi-bin/file1.py', 'POST', b'x', headers)
1029+
self.assertEqual(res.read(), b'Hello World' + self.linesep)
1030+
9921031
def test_invaliduri(self):
9931032
res = self.request('/cgi-bin/invalid')
9941033
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)