Skip to content

Commit 06037f3

Browse files
sethmlarsonmiss-islington
authored andcommitted
gh-148808: Add boundary check to asyncio.AbstractEventLoop.sock_recvf… (GH-148809)
(cherry picked from commit 1274766) Co-authored-by: Seth Larson <seth@python.org>
1 parent e378eda commit 06037f3

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

Lib/test/test_asyncio/test_sock_lowlevel.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,27 @@ def test_recvfrom_into(self):
427427
self.loop.run_until_complete(
428428
self._basetest_datagram_recvfrom_into(server_address))
429429

430+
async def _basetest_datagram_recvfrom_into_wrong_size(self, server_address):
431+
# Call sock_sendto() with a size larger than the buffer
432+
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
433+
sock.setblocking(False)
434+
435+
buf = bytearray(5000)
436+
data = b'\x01' * 4096
437+
wrong_size = len(buf) + 1
438+
await self.loop.sock_sendto(sock, data, server_address)
439+
with self.assertRaises(ValueError):
440+
await self.loop.sock_recvfrom_into(
441+
sock, buf, wrong_size)
442+
443+
size, addr = await self.loop.sock_recvfrom_into(sock, buf)
444+
self.assertEqual(buf[:size], data)
445+
446+
def test_recvfrom_into_wrong_size(self):
447+
with test_utils.run_udp_echo_server() as server_address:
448+
self.loop.run_until_complete(
449+
self._basetest_datagram_recvfrom_into_wrong_size(server_address))
450+
430451
async def _basetest_datagram_sendto_blocking(self, server_address):
431452
# Sad path, sock.sendto() raises BlockingIOError
432453
# This involves patching sock.sendto() to raise BlockingIOError but
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Added buffer boundary check when using ``nbytes`` parameter with
2+
:meth:`!asyncio.AbstractEventLoop.sock_recvfrom_into`. Only
3+
relevant for Windows and the :class:`asyncio.ProactorEventLoop`.

Modules/overlapped.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1905,6 +1905,11 @@ _overlapped_Overlapped_WSARecvFromInto_impl(OverlappedObject *self,
19051905
}
19061906
#endif
19071907

1908+
if (bufobj->len < (Py_ssize_t)size) {
1909+
PyErr_SetString(PyExc_ValueError, "nbytes is greater than the length of the buffer");
1910+
return NULL;
1911+
}
1912+
19081913
wsabuf.buf = bufobj->buf;
19091914
wsabuf.len = size;
19101915

0 commit comments

Comments
 (0)