gh-143988: tation crashes in socket sendmsg/recvmsg_into via __buffer__
#143987
+103
−10
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes two heap out-of-bounds read vulnerabilities in
socket.sendmsg()andsocket.recvmsg_into()that are related to gh-143637.While gh-143637 addresses the
__index__re-entrancy issue insendmsg()ancillary data parsing (argument 2), there are two additional vulnerable code paths that use the__buffer__protocol:socket.sendmsg()argument 1 (data buffers) - insock_sendmsg_iovec()socket.recvmsg_into()argument 1 (buffers) - insock_recvmsg_into()Root Cause
The vulnerability occurs because:
PySequence_Fast()returns the original list object when the input is already a list (not a copy)PyObject_GetBuffer()triggers__buffer__protocol callbacks which may clear the listProof of Concept
sendmsg()data buffers crash:recvmsg_into()buffers crash:Fix
Replace
PySequence_Fast()withPySequence_Tuple()which always creates a new tuple, ensuring the sequence cannot be mutated during iteration.Testing
Added
Lib/test/test_socket_reentrant.pywith regression tests for both vulnerable code paths.Note: This is related to but separate from PR #143892 which fixes the
__index__issue.