Blame SOURCES/00193-buffer-overflow.patch

4082f8
4082f8
# HG changeset patch
4082f8
# User Benjamin Peterson <benjamin@python.org>
4082f8
# Date 1389671978 18000
4082f8
# Node ID 87673659d8f7ba1623cd4914f09ad3d2ade034e9
4082f8
# Parent  2631d33ee7fbd5f0288931ef37872218d511d2e8
4082f8
complain when nbytes > buflen to fix possible buffer overflow (closes #20246)
4082f8
4082f8
diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py
4082f8
--- a/Lib/test/test_socket.py
4082f8
+++ b/Lib/test/test_socket.py
4082f8
@@ -1620,6 +1620,16 @@ class BufferIOTest(SocketConnectedTest):
4082f8
 
4082f8
     _testRecvFromIntoMemoryview = _testRecvFromIntoArray
4082f8
 
4082f8
+    def testRecvFromIntoSmallBuffer(self):
4082f8
+        # See issue #20246.
4082f8
+        buf = bytearray(8)
4082f8
+        self.assertRaises(ValueError, self.cli_conn.recvfrom_into, buf, 1024)
4082f8
+
4082f8
+    def _testRecvFromIntoSmallBuffer(self):
4082f8
+        with test_support.check_py3k_warnings():
4082f8
+            buf = buffer(MSG*2048)
4082f8
+        self.serv_conn.send(buf)
4082f8
+
4082f8
 
4082f8
 TIPC_STYPE = 2000
4082f8
 TIPC_LOWER = 200
4082f8
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
4082f8
--- a/Modules/socketmodule.c
4082f8
+++ b/Modules/socketmodule.c
4082f8
@@ -2742,6 +2742,10 @@ sock_recvfrom_into(PySocketSockObject *s
4082f8
     if (recvlen == 0) {
4082f8
         /* If nbytes was not specified, use the buffer's length */
4082f8
         recvlen = buflen;
4082f8
+    } else if (recvlen > buflen) {
4082f8
+        PyErr_SetString(PyExc_ValueError,
4082f8
+                        "nbytes is greater than the length of the buffer");
4082f8
+        goto error;
4082f8
     }
4082f8
 
4082f8
     readlen = sock_recvfrom_guts(s, buf.buf, recvlen, flags, &addr);
4082f8