GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-157335: Fix out-of-bounds write in mmap.mmap.__setitem__ by Joekrry · Pull Request #157438 · python/cpython

Conversation

@Joekrry

@Joekrry Joekrry commented Sep 13, 2026

Copy link
Copy Markdown
Contributor

Assigning to a single index in mmap.mmap.__setitem__ validates the index against the object's size, then converts the assigned value via PyNumber_AsSsize_t(). That conversion can invoke arbitrary Python code through __index__(), and if that code calls mmap.resize() to shrink the mapping, the previously validated index can point past the end of the new, smaller buffer — causing an out-of-bounds write.

This re-validates the index against the mmap's current size after the value conversion, right alongside the existing CHECK_VALID() check that already accounts for reentrancy at that point.

Fixes gh-157335.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@Joekrry
pythongh-157335: Fixed out of bounds write in mmap.mmap.__setitem__. …
c9b2513
…Assigning to a single index causes mmap.mmap.__setitem__ to validate the index against the object's size, which then converts the assigned value via PyNumber_AsSsize_t(). This invokes arbitrary python code via __index__(). mmap.resize(), which shrinks mapping could point past the end fo the new buffer causing an out of bounds error write.
@python-cla-bot

python-cla-bot Bot commented Sep 13, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot mentioned this pull request Sep 13, 2026
mmap.mmap.__setitem__ crashes when concurrently resized #157335
Closed
@bedevere-app bedevere-app Bot added the awaiting review label Sep 13, 2026
@Joekrry
Merge branch 'main' into fix-mmap-setitem-resize-reentrancy
884eb9d
picnixz
picnixz reviewed Sep 13, 2026
View reviewed changes
Comment thread Modules/mmapmodule.c Outdated
Comment on lines +1690 to +1696
/* value's __index__ may have resized the mmap, invalidating
* the earlier bounds check on i. */
if (i >= self->size) {
PyErr_SetString(PyExc_IndexError,
"mmap index out of range");
return -1;
}

@picnixz picnixz Sep 13, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I think we can have the same issue in the slice-branch when doing PyObject_GetBuffer(). So instead, we could make the checks inside the safe_byte_copy and safe_memcpy functions. Though I don't know if it's an overkill. Can you verify that the slice pah is also not affected by adding tests.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@Joekrry Joekrry Sep 15, 2026

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Yeah, I fixed the slice path so PyObject_GetBuffer is called first, then computes bounds with PySlice_AdjustIndices against the size after that call. Added a test for this as well.

I didn't move the checks because the check still has to occur at the call.

Hopefully my understanding of this is correct

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
vstinner
vstinner reviewed Sep 13, 2026
View reviewed changes
Comment thread Modules/mmapmodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Joekrry added 3 commits September 15, 2026 13:06
@Joekrry
pythongh-157335: Address review feedback on mmap setitem reentrancy fix
4095226
Fix the same reentrancy issue in the slice-assignment path: acquiring
the value's buffer (e.g. via a __buffer__ method) can also run
arbitrary code that resizes the mmap, invalidating the previously
computed slice bounds. Re-validate the bounds after acquiring the
buffer, before copying into the mapping.

Clarify why the single-item path still needs a bounds check both
before and after converting the value: the earlier check preserves
existing error precedence (IndexError before TypeError, per
test_basic), while the later one is a narrow revalidation of just the
upper bound.
@Joekrry
Merge branch 'main' into fix-mmap-setitem-resize-reentrancy
ed62a31
@Joekrry
gh:157335: fixed CI failure from mmap setitem reentrancy
c9e4016
vstinner
vstinner reviewed Sep 15, 2026
View reviewed changes
Comment thread Lib/test/test_mmap.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/mmapmodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Joekrry added 3 commits September 15, 2026 14:56
@Joekrry
Merge branch 'main' into fix-mmap-setitem-resize-reentrancy
b851d5f
@Joekrry
gh:157335: mmap setitem bound checks altered, as per reviews
c9f67ac
@Joekrry
Merge branch 'fix-mmap-setitem-resize-reentrancy' of https://github.c…
2d7d2aa
…om/Joekrry/cpython into fix-mmap-setitem-resize-reentrancy
vstinner
vstinner reviewed Sep 15, 2026
View reviewed changes

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Thanks, the PR looks better with checks moved after functions which can call arbitrary Python code. New review: you can now remove redundant CHECK_VALID() check.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Modules/mmapmodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/mmapmodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Misc/NEWS.d/next/Library/2026-09-13-15-58-28.gh-issue-157335.efaMah.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/test/test_mmap.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/test/test_mmap.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Joekrry and others added 8 commits September 15, 2026 15:40
@Joekrry @vstinner
Update Modules/mmapmodule.c
0bc662a
Co-authored-by: Victor Stinner <[email protected]>
@Joekrry @vstinner
Update Misc/NEWS.d/next/Library/2026-09-13-15-58-28.gh-issue-157335.e…
06d8548
…faMah.rst

Co-authored-by: Victor Stinner <[email protected]>
@Joekrry @vstinner
Update Lib/test/test_mmap.py
7de2dd3
Co-authored-by: Victor Stinner <[email protected]>
@Joekrry @vstinner
Update Lib/test/test_mmap.py
4c7991b
Co-authored-by: Victor Stinner <[email protected]>
@Joekrry
Merge branch 'main' into fix-mmap-setitem-resize-reentrancy
c2cd539
@Joekrry
pythongh-157335 Removed redundant CHECK_VALID check.
11cbbe9
@Joekrry
Merge branch 'main' into fix-mmap-setitem-resize-reentrancy
b03446e
@Joekrry
Merge branch 'main' into fix-mmap-setitem-resize-reentrancy
adfc2d5
@vstinner vstinner added needs backport to 3.13 only security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes labels Sep 15, 2026
vstinner
vstinner approved these changes Sep 15, 2026
View reviewed changes

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

LGTM.

@serhiy-storchaka: Do you want to double check this mmap fix?

@Joekrry: I'm not sure why you're merging the main branch into your branch so often, it's not need and it makes the review harder to follow :-(

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting review labels Sep 15, 2026
@vstinner
vstinner merged commit 09bf4c5 into python:main Sep 23, 2026
64 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting merge label Sep 23, 2026
@miss-islington-app

miss-islington-app Bot commented Sep 23, 2026

Copy link
Copy Markdown

Thanks @Joekrry for the PR, and @vstinner for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14, 3.15.
🐍🍒⛏🤖

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington-app

miss-islington-app Bot commented Sep 23, 2026

Copy link
Copy Markdown

Sorry, @Joekrry and @vstinner, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 09bf4c525871db3ad02c8a1359da5cf0668f27b0 3.13
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington-app miss-islington-app Bot assigned vstinner Sep 23, 2026
@bedevere-app

bedevere-app Bot commented Sep 23, 2026

Copy link
Copy Markdown

GH-158026 is a backport of this pull request to the 3.15 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 bugs and security fixes label Sep 23, 2026
@bedevere-app

bedevere-app Bot commented Sep 23, 2026

Copy link
Copy Markdown

GH-158027 is a backport of this pull request to the 3.14 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Sep 23, 2026
@bedevere-app

bedevere-app Bot commented Sep 23, 2026

Copy link
Copy Markdown

GH-158028 is a backport of this pull request to the 3.13 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.13 only security fixes label Sep 23, 2026
@vstinner

vstinner commented Sep 23, 2026

Copy link
Copy Markdown
Member

Merged. Thanks for the fix @Joekrry!

Joekrry reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Joekrry
Joekrry deleted the fix-mmap-setitem-resize-reentrancy branch September 23, 2026 20:15
vstinner added a commit that referenced this pull request Sep 23, 2026
@vstinner @Joekrry
[3.13] gh-157335: Fix out-of-bounds write in mmap.mmap.__setitem__ (#…
edabbff
…157438) (#158028)

gh-157335: Fix out-of-bounds write in mmap.mmap.__setitem__ (#157438)

Fix out-of-bounds write in mmap.mmap.__setitem__() that could occur
when converting the index or the assigned value (via __index__()
for a single item, or via the buffer protocol for a slice) resized or closed
the mmap object during the assignment.


(cherry picked from commit 09bf4c5)

Co-authored-by: Joseph Kerry <[email protected]>
vstinner added a commit that referenced this pull request Sep 23, 2026
@miss-islington @Joekrry @vstinner
[3.14] gh-157335: Fix out-of-bounds write in mmap.mmap.__setitem__ (G…
c554143
…H-157438) (#158027)

gh-157335: Fix out-of-bounds write in mmap.mmap.__setitem__ (GH-157438)

Fix out-of-bounds write in mmap.mmap.__setitem__() that could occur
when converting the index or the assigned value (via __index__()
for a single item, or via the buffer protocol for a slice) resized or closed
the mmap object during the assignment.
(cherry picked from commit 09bf4c5)

Co-authored-by: Joseph Kerry <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>
hugovk pushed a commit that referenced this pull request Sep 28, 2026
@miss-islington @Joekrry @vstinner
[3.15] gh-157335: Fix out-of-bounds write in mmap.mmap.__setitem__ (G…
960f69c
…H-157438) (#158026)

Co-authored-by: Joseph Kerry <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@picnixz picnixz picnixz left review comments
@vstinner vstinner vstinner approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@vstinner vstinner

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

mmap.mmap.__setitem__ crashes when concurrently resized

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@Joekrry @vstinner @picnixz
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.