Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Lib/test/test_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -7491,6 +7491,32 @@ def detach():
pass


class SendmsgReentrancyTests(unittest.TestCase):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not adding the test to SendmsgStreamTests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tried it but the test are failing so I decided it move it into a seperate class.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was the error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AttributeError: 'SendmsgUnixStreamTest' object has no attribute '_test_sendmsg_reentrant_ancillary_mutation'. Did you mean: 'test_sendmsg_reentrant_ancillary_mutation'?

AttributeError: 'SendmsgTCPTest' object has no attribute '_test_sendmsg_reentrant_ancillary_mutation'. Did you mean: 'test_sendmsg_reentrant_ancillary_mutation'?

got these two errors instead removing private method completely.

Copy link
Contributor Author

@priyanshu2282-cyber priyanshu2282-cyber Jan 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to what I got is SendmsgStreamTests the class adds _ prefix internally before the method name, just to confirm. I mean by looking the class, Is this class expects _test_* implementation for each test_* method.


@unittest.skipUnless(hasattr(socket.socket, "sendmsg"),
"sendmsg not supported")
def test_sendmsg_reentrant_ancillary_mutation(self):

class Mut:
def __index__(self):
seq.clear()
return 0

seq = [
(socket.SOL_SOCKET, Mut(), b'x'),
(socket.SOL_SOCKET, 0, b'x'),
]

left, right = socket.socketpair()
self.addCleanup(left.close)
self.addCleanup(right.close)
self.assertRaises(
(TypeError, OSError),
left.sendmsg,
[b'x'],
seq,
)

def setUpModule():
thread_info = threading_helper.threading_setup()
unittest.addModuleCleanup(threading_helper.threading_cleanup, *thread_info)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed a crash in socket.sendmsg() that could occur if ancillary data is mutated re-entrantly during argument parsing.
15 changes: 10 additions & 5 deletions Modules/socketmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -4977,11 +4977,13 @@ _socket_socket_sendmsg_impl(PySocketSockObject *s, PyObject *data_arg,
if (cmsg_arg == NULL)
ncmsgs = 0;
else {
if ((cmsg_fast = PySequence_Fast(cmsg_arg,
"sendmsg() argument 2 must be an "
"iterable")) == NULL)
cmsg_fast = PySequence_Tuple(cmsg_arg);
if (cmsg_fast == NULL) {
PyErr_SetString(PyExc_TypeError,
"sendmsg() argument 2 must be an iterable");
goto finally;
ncmsgs = PySequence_Fast_GET_SIZE(cmsg_fast);
}
ncmsgs = PyTuple_GET_SIZE(cmsg_fast);
}

#ifndef CMSG_SPACE
Expand All @@ -5001,8 +5003,11 @@ _socket_socket_sendmsg_impl(PySocketSockObject *s, PyObject *data_arg,
controllen = controllen_last = 0;
while (ncmsgbufs < ncmsgs) {
size_t bufsize, space;
PyObject *item;

item = PyTuple_GET_ITEM(cmsg_fast, ncmsgbufs);

if (!PyArg_Parse(PySequence_Fast_GET_ITEM(cmsg_fast, ncmsgbufs),
if (!PyArg_Parse(item,
"(iiy*):[sendmsg() ancillary data items]",
&cmsgs[ncmsgbufs].level,
&cmsgs[ncmsgbufs].type,
Expand Down
Loading