Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-144475: Fix use-after-free in functools.partial.__repr__()#145395

Closed
Nicolas0315 wants to merge 3 commits intopython:mainfrom
Nicolas0315:fix/partial-repr-use-after-free
Closed

gh-144475: Fix use-after-free in functools.partial.__repr__()#145395
Nicolas0315 wants to merge 3 commits intopython:mainfrom
Nicolas0315:fix/partial-repr-use-after-free

Conversation

@Nicolas0315
Copy link

@Nicolas0315Nicolas0315 commentedMar 1, 2026
edited by bedevere-appbot
Loading

Summary

Fix a heap-buffer-overflow (use-after-free) infunctools.partial.__repr__() where a user-defined__repr__() on an argument could mutate the partial object via__setstate__(), freeing the args tuple whilepartial_repr() was still iterating over it.

Root Cause

partial_repr() captured the size ofpto->args before the loop and accessed tuple items via borrowed references (PyTuple_GET_ITEM). If a__repr__() called during%R formatting invokedpto.__setstate__() with a new (smaller) args tuple, the original tuple was freed while iteration continued, causing an out-of-bounds read.

Fix

Hold strong references (Py_NewRef) topto->args,pto->kw, andpto->fn before iterating/using them. This ensures the underlying objects remain alive even if user code mutates the partial via__setstate__() during formatting.

The same pattern is applied to:

  • pto->args: The positional arguments tuple iterated in the loop
  • pto->kw: The keyword arguments dict iterated viaPyDict_Next
  • pto->fn: The callable whose__repr__ is invoked via%R

Reproducer (from the issue)

importgcfromfunctoolsimportpartialg_partial=NoneclassEvilObject:def__init__(self,name,is_trigger=False):self.name=nameself.is_trigger=is_triggerself.triggered=Falsedef__repr__(self):globalg_partialifself.is_triggerandnotself.triggeredandg_partialisnotNone:self.triggered=Truenew_state= (lambdax:x, ("replaced",), {},None)g_partial.__setstate__(new_state)gc.collect()returnf"EvilObject({self.name})"evil1=EvilObject("trigger",is_trigger=True)evil2=EvilObject("victim1")evil3=EvilObject("victim2")p=partial(lambda:None,evil1,evil2,evil3)g_partial=pdelevil1,evil2,evil3repr(p)# heap-buffer-overflow without fix

Fixes#144475.

Hold strong references to pto->args, pto->kw, and pto->fn duringpartial_repr() to prevent them from being freed by a user-defined__repr__() that mutates the partial object via __setstate__().Previously, partial_repr() iterated over pto->args using a size 'n'captured before the loop, and accessed tuple items via borrowedreferences. If a __repr__() called during formatting invokedpto.__setstate__() with a new (smaller) args tuple, the originaltuple could be freed while the loop was still iterating, leading toa heap-buffer-overflow (out-of-bounds read).The fix takes a new reference (Py_NewRef) to the args tuple, kw dict,and fn callable before using them, ensuring they stay alive regardlessof any mutations to the partial object during formatting.
@python-cla-bot
Copy link

python-cla-botbot commentedMar 1, 2026
edited
Loading

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app
Copy link

Most changes to Pythonrequire a NEWS entry. Add one using theblurb_it web app or theblurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply theskip news label instead.

pythongh-144475: Add NEWS entry for functools.partial.__repr__ fix
Use inline code markup instead of :func: and :meth: roles forpartial.__repr__ and __setstate__ to avoid Sphinx referenceresolution failures in the docs CI.
@aisk
Copy link
Member

aisk commentedMar 2, 2026

There is already an active PR#145362 for this issue, so please don’t open another PR for the same fix.

@encukouencukou closed thisMar 3, 2026
@encukou
Copy link
Member

Also, if this is LLM-generated, please reviewhttps://devguide.python.org/getting-started/generative-ai/

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@rhettingerrhettingerAwaiting requested review from rhettingerrhettinger is a code owner

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

heap-buffer-overflow in functools.partial.__repr__()

3 participants

@Nicolas0315@aisk@encukou

[8]ページ先頭

©2009-2026 Movatter.jp