Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork34k
gh-143228: Fix UAF in perf trampoline during finalization#143233
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
1 change: 0 additions & 1 deletionInclude/internal/pycore_ceval.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletionInclude/internal/pycore_interp_structs.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletionsMisc/NEWS.d/next/Core_and_Builtins/2025-12-27-23-57-43.gh-issue-143228.m3EF9E.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| 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 |
67 changes: 59 additions & 8 deletionsPython/perf_trampoline.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -204,6 +204,42 @@ enum perf_trampoline_type { | ||
| #define persist_after_fork _PyRuntime.ceval.perf.persist_after_fork | ||
| #define perf_trampoline_type _PyRuntime.ceval.perf.perf_trampoline_type | ||
| #define prev_eval_frame _PyRuntime.ceval.perf.prev_eval_frame | ||
| #define trampoline_refcount _PyRuntime.ceval.perf.trampoline_refcount | ||
| #define code_watcher_id _PyRuntime.ceval.perf.code_watcher_id | ||
| static void free_code_arenas(void); | ||
| static void | ||
| perf_trampoline_reset_state(void) | ||
| { | ||
| free_code_arenas(); | ||
| if (code_watcher_id >= 0) { | ||
| PyCode_ClearWatcher(code_watcher_id); | ||
| code_watcher_id = -1; | ||
| } | ||
| extra_code_index = -1; | ||
| } | ||
| static int | ||
| perf_trampoline_code_watcher(PyCodeEvent event, PyCodeObject *co) | ||
| { | ||
| if (event != PY_CODE_EVENT_DESTROY) { | ||
| return 0; | ||
| } | ||
| if (extra_code_index == -1) { | ||
| return 0; | ||
| } | ||
| py_trampoline f = NULL; | ||
| int ret = _PyCode_GetExtra((PyObject *)co, extra_code_index, (void **)&f); | ||
| if (ret != 0 || f == NULL) { | ||
| return 0; | ||
| } | ||
| trampoline_refcount--; | ||
| if (trampoline_refcount == 0) { | ||
pablogsal marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| perf_trampoline_reset_state(); | ||
| } | ||
| return 0; | ||
| } | ||
| static void | ||
| perf_map_write_entry(void *state, const void *code_addr, | ||
| @@ -407,6 +443,7 @@ py_trampoline_evaluator(PyThreadState *ts, _PyInterpreterFrame *frame, | ||
| perf_code_arena->code_size, co); | ||
| _PyCode_SetExtra((PyObject *)co, extra_code_index, | ||
| (void *)new_trampoline); | ||
| trampoline_refcount++; | ||
| f = new_trampoline; | ||
| } | ||
| assert(f != NULL); | ||
| @@ -433,6 +470,7 @@ int PyUnstable_PerfTrampoline_CompileCode(PyCodeObject *co) | ||
| } | ||
| trampoline_api.write_state(trampoline_api.state, new_trampoline, | ||
| perf_code_arena->code_size, co); | ||
| trampoline_refcount++; | ||
| return _PyCode_SetExtra((PyObject *)co, extra_code_index, | ||
| (void *)new_trampoline); | ||
| } | ||
| @@ -487,6 +525,10 @@ _PyPerfTrampoline_Init(int activate) | ||
| { | ||
| #ifdef PY_HAVE_PERF_TRAMPOLINE | ||
| PyThreadState *tstate = _PyThreadState_GET(); | ||
| if (code_watcher_id == 0) { | ||
| // Initialize to -1 since 0 is a valid watcher ID | ||
| code_watcher_id = -1; | ||
| } | ||
| if (!activate) { | ||
| _PyInterpreterState_SetEvalFrameFunc(tstate->interp, prev_eval_frame); | ||
| perf_status = PERF_STATUS_NO_INIT; | ||
| @@ -504,6 +546,13 @@ _PyPerfTrampoline_Init(int activate) | ||
| if (new_code_arena() < 0) { | ||
| return -1; | ||
| } | ||
| code_watcher_id = PyCode_AddWatcher(perf_trampoline_code_watcher); | ||
| if (code_watcher_id < 0) { | ||
| PyErr_FormatUnraisable("Failed to register code watcher for perf trampoline"); | ||
| free_code_arenas(); | ||
| return -1; | ||
| } | ||
| trampoline_refcount = 1; // Base refcount held by the system | ||
| perf_status = PERF_STATUS_OK; | ||
| } | ||
| #endif | ||
| @@ -525,17 +574,19 @@ _PyPerfTrampoline_Fini(void) | ||
| trampoline_api.free_state(trampoline_api.state); | ||
| perf_trampoline_type = PERF_TRAMPOLINE_UNSET; | ||
| } | ||
| // Prevent new trampolines from being created | ||
| perf_status = PERF_STATUS_NO_INIT; | ||
| // Decrement base refcount. If refcount reaches 0, all code objects are already | ||
| // dead so clean up now. Otherwise, watcher remains active to clean up when last | ||
| // code object dies; extra_code_index stays valid so watcher can identify them. | ||
| trampoline_refcount--; | ||
| if (trampoline_refcount == 0) { | ||
| perf_trampoline_reset_state(); | ||
| } | ||
| #endif | ||
| return 0; | ||
| } | ||
| int | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.