]> granicus.if.org Git - python/commitdiff
complain when nbytes > buflen to fix possible buffer overflow (closes #20246)
authorBenjamin Peterson <benjamin@python.org>
Tue, 14 Jan 2014 03:59:38 +0000 (22:59 -0500)
committerBenjamin Peterson <benjamin@python.org>
Tue, 14 Jan 2014 03:59:38 +0000 (22:59 -0500)
Lib/test/test_socket.py
Misc/ACKS
Misc/NEWS
Modules/socketmodule.c

index 4ecab952d0d7b21f9caf51412c54d9482a4c7a8b..290c4dd0968ee2676cf71543537ccc77eea6c549 100644 (file)
@@ -1968,6 +1968,14 @@ class BufferIOTest(SocketConnectedTest):
 
     _testRecvFromIntoMemoryview = _testRecvFromIntoArray
 
+    def testRecvFromIntoSmallBuffer(self):
+        # See issue #20246.
+        buf = bytearray(8)
+        self.assertRaises(ValueError, self.cli_conn.recvfrom_into, buf, 1024)
+
+    def _testRecvFromIntoSmallBuffer(self):
+        self.serv_conn.send(MSG*2048)
+
 
 TIPC_STYPE = 2000
 TIPC_LOWER = 200
index 6ea3f78d76ceb3d3659baebdb6cd3f29e8ac3e84..5236e1066b28e2d8c28c0341a1206c663a2fe1ed 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1020,6 +1020,7 @@ Eric V. Smith
 Christopher Smith
 Gregory P. Smith
 Roy Smith
+Ryan Smith-Roberts
 Rafal Smotrzyk
 Dirk Soede
 Paul Sokolovsky
index 430bec2cfa2da1c9170745b434b845955dbdce21..693a0c8076326826ac59d08901080ae4730f9468 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,8 @@ What's New in Python 3.2.6?
 Library
 -------
 
+- Issue #20246: Fix buffer overflow in socket.recvfrom_into.
+
 - Issue #12226: HTTPS is now used by default when connecting to PyPI.
 
 - Issue #19435: Fix directory traversal attack on CGIHttpRequestHandler.
index e02762502425a19db1b361f51c9249b3107867c7..903e10c98cc81c251879ed6561b473ce4ffab123 100644 (file)
@@ -2598,6 +2598,11 @@ sock_recvfrom_into(PySocketSockObject *s, PyObject *args, PyObject* kwds)
     if (recvlen == 0) {
         /* If nbytes was not specified, use the buffer's length */
         recvlen = buflen;
+    } else if (recvlen > buflen) {
+        PyBuffer_Release(&pbuf);
+        PyErr_SetString(PyExc_ValueError,
+                        "nbytes is greater than the length of the buffer");
+        return NULL;
     }
 
     readlen = sock_recvfrom_guts(s, buf, recvlen, flags, &addr);