Skip to content

Commit fbf648e

Browse files
committed
complain when nbytes > buflen to fix possible buffer overflow (closes #20246)
1 parent f60b7df commit fbf648e

File tree

4 files changed

+16
-0
lines changed

4 files changed

+16
-0
lines changed

Lib/test/test_socket.py

+8
Original file line numberDiff line numberDiff line change
@@ -1968,6 +1968,14 @@ def testRecvFromIntoMemoryview(self):
19681968

19691969
_testRecvFromIntoMemoryview = _testRecvFromIntoArray
19701970

1971+
def testRecvFromIntoSmallBuffer(self):
1972+
# See issue #20246.
1973+
buf = bytearray(8)
1974+
self.assertRaises(ValueError, self.cli_conn.recvfrom_into, buf, 1024)
1975+
1976+
def _testRecvFromIntoSmallBuffer(self):
1977+
self.serv_conn.send(MSG*2048)
1978+
19711979

19721980
TIPC_STYPE = 2000
19731981
TIPC_LOWER = 200

Misc/ACKS

+1
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,7 @@ Eric V. Smith
10201020
Christopher Smith
10211021
Gregory P. Smith
10221022
Roy Smith
1023+
Ryan Smith-Roberts
10231024
Rafal Smotrzyk
10241025
Dirk Soede
10251026
Paul Sokolovsky

Misc/NEWS

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ What's New in Python 3.2.6?
1010
Library
1111
-------
1212

13+
- Issue #20246: Fix buffer overflow in socket.recvfrom_into.
14+
1315
- Issue #12226: HTTPS is now used by default when connecting to PyPI.
1416

1517
- Issue #19435: Fix directory traversal attack on CGIHttpRequestHandler.

Modules/socketmodule.c

+5
Original file line numberDiff line numberDiff line change
@@ -2598,6 +2598,11 @@ sock_recvfrom_into(PySocketSockObject *s, PyObject *args, PyObject* kwds)
25982598
if (recvlen == 0) {
25992599
/* If nbytes was not specified, use the buffer's length */
26002600
recvlen = buflen;
2601+
} else if (recvlen > buflen) {
2602+
PyBuffer_Release(&pbuf);
2603+
PyErr_SetString(PyExc_ValueError,
2604+
"nbytes is greater than the length of the buffer");
2605+
return NULL;
26012606
}
26022607

26032608
readlen = sock_recvfrom_guts(s, buf, recvlen, flags, &addr);

0 commit comments

Comments
 (0)