Skip to content

gh-143228: Fix UAF in perf trampoline during finalization#143233

Merged
pablogsal merged 1 commit into
python:mainfrom
pablogsal:gh-143228
Dec 28, 2025
Merged

gh-143228: Fix UAF in perf trampoline during finalization#143233
pablogsal merged 1 commit into
python:mainfrom
pablogsal:gh-143228

Conversation

@pablogsal

@pablogsal pablogsal commented Dec 27, 2025

Copy link
Copy Markdown
Member

When toggling perf trampoline while threads are running, or during
interpreter finalization with daemon threads active, a use-after-free
occurs. The munmap call in free_code_arenas releases executable memory
while other threads may still be executing within trampolines or
unwinding through them, causing SIGSEGV or SystemError.

The fix uses reference counting with a code watcher. Each code object
that receives a trampoline increments a refcount. When code objects are
destroyed, the watcher decrements the refcount and frees arenas only
when it reaches zero. This ensures trampolines are never freed while
any code object could still reference them.

@Fidget-Spinner Fidget-Spinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good, just one question

Comment thread Python/perf_trampoline.c
When toggling perf trampoline while threads are running, or during
interpreter finalization with daemon threads active, a use-after-free
occurs. The munmap call in free_code_arenas releases executable memory
while other threads may still be executing within trampolines or
unwinding through them, causing SIGSEGV or SystemError.

The fix uses reference counting with a code watcher. Each code object
that receives a trampoline increments a refcount. When code objects are
destroyed, the watcher decrements the refcount and frees arenas only
when it reaches zero. This ensures trampolines are never freed while
any code object could still reference them.
@pablogsal pablogsal merged commit 3ccc76f into python:main Dec 28, 2025
50 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @pablogsal for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

@pablogsal pablogsal deleted the gh-143228 branch December 28, 2025 13:50
@miss-islington-app

Copy link
Copy Markdown

Sorry, @pablogsal, I could not cleanly backport this to 3.14 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 3ccc76f036bfaabb5a4631783b966501fe64859a 3.14

@miss-islington-app

Copy link
Copy Markdown

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

cherry_picker 3ccc76f036bfaabb5a4631783b966501fe64859a 3.13

@bedevere-app

bedevere-app Bot commented Dec 28, 2025

Copy link
Copy Markdown

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

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Dec 28, 2025
pablogsal added a commit to pablogsal/cpython that referenced this pull request Dec 28, 2025
pythonGH-143233)

(cherry picked from commit 3ccc76f)

Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
pablogsal added a commit to pablogsal/cpython that referenced this pull request Dec 28, 2025
pythonGH-143233)

(cherry picked from commit 3ccc76f)

Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
@bedevere-app

bedevere-app Bot commented Dec 28, 2025

Copy link
Copy Markdown

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

@bedevere-app bedevere-app Bot removed the needs backport to 3.13 bugs and security fixes label Dec 28, 2025
pablogsal added a commit to pablogsal/cpython that referenced this pull request Dec 28, 2025
pythonGH-143233)

(cherry picked from commit 3ccc76f)

Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
Fix use-after-free in perf trampoline when toggling profiling while
threads are running or during interpreter finalization with daemon threads
active. The fix uses reference counting to ensure trampolines are not freed
while any code object could still reference them. Pach by Pablo Galindo

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Patch :) but I like your new word Pach, we should start using it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

hehe

thunder-coding pushed a commit to thunder-coding/cpython that referenced this pull request Feb 15, 2026
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