Skip to content

[3.15] gh-143988: Fix re-entrant mutation crashes in socket sendmsg/recvmsg_into (GH-143987)#151246

Merged
vstinner merged 1 commit into
python:3.15from
miss-islington:backport-896f7fd-3.15
Jun 10, 2026
Merged

[3.15] gh-143988: Fix re-entrant mutation crashes in socket sendmsg/recvmsg_into (GH-143987)#151246
vstinner merged 1 commit into
python:3.15from
miss-islington:backport-896f7fd-3.15

Conversation

@miss-islington

@miss-islington miss-islington commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

Fix crashes in socket.sendmsg() and socket.recvmsg_into() that could
occur if buffer sequences are mutated re-entrantly during argument
parsing via buffer protocol callbacks.

The bug occurs because:

  1. PySequence_Fast() returns the original list object when the input
    is already a list (not a copy).
  2. During iteration, PyObject_GetBuffer() triggers buffer
    callbacks which may clear the list.
  3. Subsequent iterations access invalid memory (heap OOB read).

The fix replaces PySequence_Fast() with PySequence_Tuple() which
always creates a new tuple, ensuring the sequence cannot be mutated
during iteration.
(cherry picked from commit 896f7fd)

Co-authored-by: tonghuaroot (童话) tonghuaroot@gmail.com
Co-authored-by: tonghuaroot 23011166+tonghuaroot@users.noreply.github.com

…cvmsg_into (pythonGH-143987)

Fix crashes in socket.sendmsg() and socket.recvmsg_into() that could
occur if buffer sequences are mutated re-entrantly during argument
parsing via __buffer__ protocol callbacks.

The bug occurs because:

1. PySequence_Fast() returns the original list object when the input
   is already a list (not a copy).
2. During iteration, PyObject_GetBuffer() triggers __buffer__
   callbacks which may clear the list.
3. Subsequent iterations access invalid memory (heap OOB read).

The fix replaces PySequence_Fast() with PySequence_Tuple() which
always creates a new tuple, ensuring the sequence cannot be mutated
during iteration.
(cherry picked from commit 896f7fd)

Co-authored-by: tonghuaroot (童话) <tonghuaroot@gmail.com>
Co-authored-by: tonghuaroot <23011166+tonghuaroot@users.noreply.github.com>
@vstinner

Copy link
Copy Markdown
Member

Tests / Sanitizers / TSan (free-threading) (pull_request): Failing after 23m

test_ssl failed: 0:14:46 load avg: 7.31 [71/78/1] test_ssl worker non-zero exit code (Exit code 66) -- running (1): test_socket (1 min 25 sec)

I re-ran the job.

@vstinner vstinner merged commit 9261f8b into python:3.15 Jun 10, 2026
98 of 100 checks passed
@miss-islington miss-islington deleted the backport-896f7fd-3.15 branch June 10, 2026 14:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants