Blame SOURCES/00193-buffer-overflow.patch

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